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 | 5ab6b57 | 2017-09-22 08:05:00 +0200 | [diff] [blame] | 15 | #include <common/h2.h> |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 16 | #include <common/hpack-dec.h> |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 17 | #include <common/hpack-enc.h> |
Willy Tarreau | 5ab6b57 | 2017-09-22 08:05:00 +0200 | [diff] [blame] | 18 | #include <common/hpack-tbl.h> |
Willy Tarreau | e482074 | 2017-07-27 13:37:23 +0200 | [diff] [blame] | 19 | #include <common/net_helper.h> |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 20 | #include <proto/connection.h> |
Willy Tarreau | 3ccf4b2 | 2017-10-13 19:07:26 +0200 | [diff] [blame] | 21 | #include <proto/h1.h> |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 22 | #include <proto/stream.h> |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 23 | #include <types/session.h> |
Willy Tarreau | 5ab6b57 | 2017-09-22 08:05:00 +0200 | [diff] [blame] | 24 | #include <eb32tree.h> |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 25 | |
| 26 | |
Willy Tarreau | 2a85618 | 2017-05-16 15:20:39 +0200 | [diff] [blame] | 27 | /* dummy streams returned for idle and closed states */ |
| 28 | static const struct h2s *h2_closed_stream; |
| 29 | static const struct h2s *h2_idle_stream; |
| 30 | |
Willy Tarreau | 5ab6b57 | 2017-09-22 08:05:00 +0200 | [diff] [blame] | 31 | /* the h2c connection pool */ |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 32 | static struct pool_head *pool_head_h2c; |
Willy Tarreau | 1831264 | 2017-10-11 07:57:07 +0200 | [diff] [blame] | 33 | /* the h2s stream pool */ |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 34 | static struct pool_head *pool_head_h2s; |
Willy Tarreau | 5ab6b57 | 2017-09-22 08:05:00 +0200 | [diff] [blame] | 35 | |
| 36 | /* Connection flags (32 bit), in h2c->flags */ |
| 37 | #define H2_CF_NONE 0x00000000 |
| 38 | |
Willy Tarreau | 2e5b60e | 2017-09-25 11:49:03 +0200 | [diff] [blame] | 39 | /* Flags indicating why writing to the mux is blocked. */ |
| 40 | #define H2_CF_MUX_MALLOC 0x00000001 // mux blocked on lack of connection's mux buffer |
| 41 | #define H2_CF_MUX_MFULL 0x00000002 // mux blocked on connection's mux buffer full |
| 42 | #define H2_CF_MUX_BLOCK_ANY 0x00000003 // aggregate of the mux flags above |
| 43 | |
Willy Tarreau | 315d807 | 2017-12-10 22:17:57 +0100 | [diff] [blame] | 44 | /* Flags indicating why writing to the demux is blocked. |
| 45 | * The first two ones directly affect the ability for the mux to receive data |
| 46 | * from the connection. The other ones affect the mux's ability to demux |
| 47 | * received data. |
| 48 | */ |
Willy Tarreau | 2e5b60e | 2017-09-25 11:49:03 +0200 | [diff] [blame] | 49 | #define H2_CF_DEM_DALLOC 0x00000004 // demux blocked on lack of connection's demux buffer |
| 50 | #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] | 51 | |
Willy Tarreau | 2e5b60e | 2017-09-25 11:49:03 +0200 | [diff] [blame] | 52 | #define H2_CF_DEM_MBUSY 0x00000010 // demux blocked on connection's mux side busy |
| 53 | #define H2_CF_DEM_MROOM 0x00000020 // demux blocked on lack of room in mux buffer |
| 54 | #define H2_CF_DEM_SALLOC 0x00000040 // demux blocked on lack of stream's request buffer |
| 55 | #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] | 56 | #define H2_CF_DEM_TOOMANY 0x00000100 // demux blocked waiting for some conn_streams to leave |
| 57 | #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] | 58 | |
Willy Tarreau | 081d472 | 2017-05-16 21:51:05 +0200 | [diff] [blame] | 59 | /* other flags */ |
Willy Tarreau | f210191 | 2018-07-19 10:11:38 +0200 | [diff] [blame] | 60 | #define H2_CF_GOAWAY_SENT 0x00001000 // a GOAWAY frame was successfully sent |
| 61 | #define H2_CF_GOAWAY_FAILED 0x00002000 // a GOAWAY frame failed to be sent |
| 62 | #define H2_CF_WAIT_FOR_HS 0x00004000 // We did check that at least a stream was waiting for handshake |
Willy Tarreau | 081d472 | 2017-05-16 21:51:05 +0200 | [diff] [blame] | 63 | |
| 64 | |
Willy Tarreau | 5ab6b57 | 2017-09-22 08:05:00 +0200 | [diff] [blame] | 65 | /* H2 connection state, in h2c->st0 */ |
| 66 | enum h2_cs { |
| 67 | H2_CS_PREFACE, // init done, waiting for connection preface |
| 68 | H2_CS_SETTINGS1, // preface OK, waiting for first settings frame |
| 69 | H2_CS_FRAME_H, // first settings frame ok, waiting for frame header |
| 70 | H2_CS_FRAME_P, // frame header OK, waiting for frame payload |
Willy Tarreau | a20a519 | 2017-12-27 11:02:06 +0100 | [diff] [blame] | 71 | H2_CS_FRAME_A, // frame payload OK, trying to send ACK frame |
| 72 | H2_CS_FRAME_E, // frame payload OK, trying to send RST frame |
Willy Tarreau | 5ab6b57 | 2017-09-22 08:05:00 +0200 | [diff] [blame] | 73 | H2_CS_ERROR, // send GOAWAY(errcode) and close the connection ASAP |
| 74 | H2_CS_ERROR2, // GOAWAY(errcode) sent, close the connection ASAP |
| 75 | H2_CS_ENTRIES // must be last |
| 76 | } __attribute__((packed)); |
| 77 | |
| 78 | /* H2 connection descriptor */ |
| 79 | struct h2c { |
| 80 | struct connection *conn; |
| 81 | |
| 82 | enum h2_cs st0; /* mux state */ |
| 83 | enum h2_err errcode; /* H2 err code (H2_ERR_*) */ |
| 84 | |
| 85 | /* 16 bit hole here */ |
| 86 | uint32_t flags; /* connection flags: H2_CF_* */ |
| 87 | int32_t max_id; /* highest ID known on this connection, <0 before preface */ |
| 88 | uint32_t rcvd_c; /* newly received data to ACK for the connection */ |
| 89 | uint32_t rcvd_s; /* newly received data to ACK for the current stream (dsi) */ |
| 90 | |
| 91 | /* states for the demux direction */ |
| 92 | struct hpack_dht *ddht; /* demux dynamic header table */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 93 | struct buffer dbuf; /* demux buffer */ |
Willy Tarreau | 5ab6b57 | 2017-09-22 08:05:00 +0200 | [diff] [blame] | 94 | |
| 95 | int32_t dsi; /* demux stream ID (<0 = idle) */ |
| 96 | int32_t dfl; /* demux frame length (if dsi >= 0) */ |
| 97 | int8_t dft; /* demux frame type (if dsi >= 0) */ |
| 98 | int8_t dff; /* demux frame flags (if dsi >= 0) */ |
Willy Tarreau | 05e5daf | 2017-12-11 15:17:36 +0100 | [diff] [blame] | 99 | uint8_t dpl; /* demux pad length (part of dfl), init to 0 */ |
| 100 | /* 8 bit hole here */ |
Willy Tarreau | 5ab6b57 | 2017-09-22 08:05:00 +0200 | [diff] [blame] | 101 | int32_t last_sid; /* last processed stream ID for GOAWAY, <0 before preface */ |
| 102 | |
| 103 | /* states for the mux direction */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 104 | struct buffer mbuf; /* mux buffer */ |
Willy Tarreau | 5ab6b57 | 2017-09-22 08:05:00 +0200 | [diff] [blame] | 105 | int32_t msi; /* mux stream ID (<0 = idle) */ |
| 106 | int32_t mfl; /* mux frame length (if dsi >= 0) */ |
| 107 | int8_t mft; /* mux frame type (if dsi >= 0) */ |
| 108 | int8_t mff; /* mux frame flags (if dsi >= 0) */ |
| 109 | /* 16 bit hole here */ |
| 110 | int32_t miw; /* mux initial window size for all new streams */ |
| 111 | int32_t mws; /* mux window size. Can be negative. */ |
| 112 | int32_t mfs; /* mux's max frame size */ |
| 113 | |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 114 | int timeout; /* idle timeout duration in ticks */ |
Willy Tarreau | 599391a | 2017-11-24 10:16:00 +0100 | [diff] [blame] | 115 | int shut_timeout; /* idle timeout duration in ticks after GOAWAY was sent */ |
Willy Tarreau | 4974561 | 2017-12-03 18:56:02 +0100 | [diff] [blame] | 116 | unsigned int nb_streams; /* number of streams in the tree */ |
Willy Tarreau | 7ac60e8 | 2018-07-19 09:04:05 +0200 | [diff] [blame] | 117 | unsigned int nb_cs; /* number of attached conn_streams */ |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 118 | struct task *task; /* timeout management task */ |
Willy Tarreau | 5ab6b57 | 2017-09-22 08:05:00 +0200 | [diff] [blame] | 119 | struct eb_root streams_by_id; /* all active streams by their ID */ |
| 120 | struct list send_list; /* list of blocked streams requesting to send */ |
| 121 | struct list fctl_list; /* list of streams blocked by connection's fctl */ |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 122 | struct buffer_wait buf_wait; /* wait list for buffer allocations */ |
Olivier Houchard | 6ff2039 | 2018-07-17 18:46:31 +0200 | [diff] [blame] | 123 | struct list send_wait_list; /* list of tasks to wake when we're ready to send */ |
Olivier Houchard | 910b2bc | 2018-07-17 18:49:38 +0200 | [diff] [blame] | 124 | struct wait_list wait_list; /* We're in a wait list, to send */ |
Willy Tarreau | 5ab6b57 | 2017-09-22 08:05:00 +0200 | [diff] [blame] | 125 | }; |
| 126 | |
Willy Tarreau | 1831264 | 2017-10-11 07:57:07 +0200 | [diff] [blame] | 127 | /* H2 stream state, in h2s->st */ |
| 128 | enum h2_ss { |
| 129 | H2_SS_IDLE = 0, // idle |
| 130 | H2_SS_RLOC, // reserved(local) |
| 131 | H2_SS_RREM, // reserved(remote) |
| 132 | H2_SS_OPEN, // open |
| 133 | H2_SS_HREM, // half-closed(remote) |
| 134 | H2_SS_HLOC, // half-closed(local) |
Willy Tarreau | 96060ba | 2017-10-16 18:34:34 +0200 | [diff] [blame] | 135 | H2_SS_ERROR, // an error needs to be sent using RST_STREAM |
Willy Tarreau | 1831264 | 2017-10-11 07:57:07 +0200 | [diff] [blame] | 136 | H2_SS_CLOSED, // closed |
| 137 | H2_SS_ENTRIES // must be last |
| 138 | } __attribute__((packed)); |
| 139 | |
| 140 | /* HTTP/2 stream flags (32 bit), in h2s->flags */ |
| 141 | #define H2_SF_NONE 0x00000000 |
| 142 | #define H2_SF_ES_RCVD 0x00000001 |
| 143 | #define H2_SF_ES_SENT 0x00000002 |
| 144 | |
| 145 | #define H2_SF_RST_RCVD 0x00000004 // received RST_STREAM |
| 146 | #define H2_SF_RST_SENT 0x00000008 // sent RST_STREAM |
| 147 | |
Willy Tarreau | 2e5b60e | 2017-09-25 11:49:03 +0200 | [diff] [blame] | 148 | /* stream flags indicating the reason the stream is blocked */ |
| 149 | #define H2_SF_BLK_MBUSY 0x00000010 // blocked waiting for mux access (transient) |
| 150 | #define H2_SF_BLK_MROOM 0x00000020 // blocked waiting for room in the mux |
| 151 | #define H2_SF_BLK_MFCTL 0x00000040 // blocked due to mux fctl |
| 152 | #define H2_SF_BLK_SFCTL 0x00000080 // blocked due to stream fctl |
| 153 | #define H2_SF_BLK_ANY 0x000000F0 // any of the reasons above |
| 154 | |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 155 | /* stream flags indicating how data is supposed to be sent */ |
| 156 | #define H2_SF_DATA_CLEN 0x00000100 // data sent using content-length |
| 157 | #define H2_SF_DATA_CHNK 0x00000200 // data sent using chunked-encoding |
| 158 | |
| 159 | /* step we're currently in when sending chunks. This is needed because we may |
| 160 | * have to transfer chunks as large as a full buffer so there's no room left |
| 161 | * for size nor crlf around. |
| 162 | */ |
| 163 | #define H2_SF_CHNK_SIZE 0x00000000 // trying to send chunk size |
| 164 | #define H2_SF_CHNK_DATA 0x00000400 // trying to send chunk data |
| 165 | #define H2_SF_CHNK_CRLF 0x00000800 // trying to send chunk crlf after data |
| 166 | |
| 167 | #define H2_SF_CHNK_MASK 0x00000C00 // trying to send chunk size |
| 168 | |
Willy Tarreau | 6743420 | 2017-11-06 20:20:51 +0100 | [diff] [blame] | 169 | #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] | 170 | #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] | 171 | |
Willy Tarreau | 1831264 | 2017-10-11 07:57:07 +0200 | [diff] [blame] | 172 | /* H2 stream descriptor, describing the stream as it appears in the H2C, and as |
| 173 | * it is being processed in the internal HTTP representation (H1 for now). |
| 174 | */ |
| 175 | struct h2s { |
| 176 | struct conn_stream *cs; |
| 177 | struct h2c *h2c; |
| 178 | struct h1m req, res; /* request and response parser state for H1 */ |
| 179 | struct eb32_node by_id; /* place in h2c's streams_by_id */ |
| 180 | struct list list; /* position in active/blocked lists if blocked>0 */ |
| 181 | int32_t id; /* stream ID */ |
| 182 | uint32_t flags; /* H2_SF_* */ |
| 183 | int mws; /* mux window size for this stream */ |
| 184 | enum h2_err errcode; /* H2 err code (H2_ERR_*) */ |
| 185 | enum h2_ss st; |
| 186 | }; |
Willy Tarreau | 5ab6b57 | 2017-09-22 08:05:00 +0200 | [diff] [blame] | 187 | |
Willy Tarreau | c640514 | 2017-09-21 20:23:50 +0200 | [diff] [blame] | 188 | /* descriptor for an h2 frame header */ |
| 189 | struct h2_fh { |
| 190 | uint32_t len; /* length, host order, 24 bits */ |
| 191 | uint32_t sid; /* stream id, host order, 31 bits */ |
| 192 | uint8_t ft; /* frame type */ |
| 193 | uint8_t ff; /* frame flags */ |
| 194 | }; |
| 195 | |
Willy Tarreau | fe20e5b | 2017-07-27 11:42:14 +0200 | [diff] [blame] | 196 | /* a few settings from the global section */ |
| 197 | static int h2_settings_header_table_size = 4096; /* initial value */ |
Willy Tarreau | e6baec0 | 2017-07-27 11:45:11 +0200 | [diff] [blame] | 198 | static int h2_settings_initial_window_size = 65535; /* initial value */ |
Willy Tarreau | 5242ef8 | 2017-07-27 11:47:28 +0200 | [diff] [blame] | 199 | static int h2_settings_max_concurrent_streams = 100; |
Willy Tarreau | fe20e5b | 2017-07-27 11:42:14 +0200 | [diff] [blame] | 200 | |
Willy Tarreau | 2a85618 | 2017-05-16 15:20:39 +0200 | [diff] [blame] | 201 | /* a dmumy closed stream */ |
| 202 | static const struct h2s *h2_closed_stream = &(const struct h2s){ |
| 203 | .cs = NULL, |
| 204 | .h2c = NULL, |
| 205 | .st = H2_SS_CLOSED, |
Willy Tarreau | 8c0ea7d | 2017-11-10 10:05:24 +0100 | [diff] [blame] | 206 | .errcode = H2_ERR_STREAM_CLOSED, |
Willy Tarreau | ab83750 | 2017-12-27 15:07:30 +0100 | [diff] [blame] | 207 | .flags = H2_SF_RST_RCVD, |
Willy Tarreau | 2a85618 | 2017-05-16 15:20:39 +0200 | [diff] [blame] | 208 | .id = 0, |
| 209 | }; |
| 210 | |
| 211 | /* and a dummy idle stream for use with any unannounced stream */ |
| 212 | static const struct h2s *h2_idle_stream = &(const struct h2s){ |
| 213 | .cs = NULL, |
| 214 | .h2c = NULL, |
| 215 | .st = H2_SS_IDLE, |
Willy Tarreau | 8c0ea7d | 2017-11-10 10:05:24 +0100 | [diff] [blame] | 216 | .errcode = H2_ERR_STREAM_CLOSED, |
Willy Tarreau | 2a85618 | 2017-05-16 15:20:39 +0200 | [diff] [blame] | 217 | .id = 0, |
| 218 | }; |
| 219 | |
Olivier Houchard | 9f6af33 | 2018-05-25 14:04:04 +0200 | [diff] [blame] | 220 | static struct task *h2_timeout_task(struct task *t, void *context, unsigned short state); |
Olivier Houchard | 910b2bc | 2018-07-17 18:49:38 +0200 | [diff] [blame] | 221 | static struct task *h2_send(struct task *t, void *ctx, unsigned short state); |
Willy Tarreau | fe20e5b | 2017-07-27 11:42:14 +0200 | [diff] [blame] | 222 | |
Willy Tarreau | 35dbd5d | 2017-09-22 09:13:49 +0200 | [diff] [blame] | 223 | /*****************************************************/ |
| 224 | /* functions below are for dynamic buffer management */ |
| 225 | /*****************************************************/ |
| 226 | |
Willy Tarreau | 315d807 | 2017-12-10 22:17:57 +0100 | [diff] [blame] | 227 | /* indicates whether or not the we may call the h2_recv() function to attempt |
| 228 | * to receive data into the buffer and/or demux pending data. The condition is |
| 229 | * a bit complex due to some API limits for now. The rules are the following : |
| 230 | * - if an error or a shutdown was detected on the connection and the buffer |
| 231 | * is empty, we must not attempt to receive |
| 232 | * - if the demux buf failed to be allocated, we must not try to receive and |
| 233 | * we know there is nothing pending |
Willy Tarreau | 6042aeb | 2017-12-12 11:01:44 +0100 | [diff] [blame] | 234 | * - if no flag indicates a blocking condition, we may attempt to receive, |
| 235 | * regardless of whether the demux buffer is full or not, so that only |
| 236 | * de demux part decides whether or not to block. This is needed because |
| 237 | * the connection API indeed prevents us from re-enabling receipt that is |
| 238 | * already enabled in a polled state, so we must always immediately stop |
| 239 | * as soon as the demux can't proceed so as never to hit an end of read |
| 240 | * with data pending in the buffers. |
Willy Tarreau | 315d807 | 2017-12-10 22:17:57 +0100 | [diff] [blame] | 241 | * - otherwise must may not attempt |
| 242 | */ |
| 243 | static inline int h2_recv_allowed(const struct h2c *h2c) |
| 244 | { |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 245 | if (b_data(&h2c->dbuf) == 0 && |
Willy Tarreau | 315d807 | 2017-12-10 22:17:57 +0100 | [diff] [blame] | 246 | (h2c->st0 >= H2_CS_ERROR || |
| 247 | h2c->conn->flags & CO_FL_ERROR || |
| 248 | conn_xprt_read0_pending(h2c->conn))) |
| 249 | return 0; |
| 250 | |
| 251 | if (!(h2c->flags & H2_CF_DEM_DALLOC) && |
Willy Tarreau | 6042aeb | 2017-12-12 11:01:44 +0100 | [diff] [blame] | 252 | !(h2c->flags & H2_CF_DEM_BLOCK_ANY)) |
Willy Tarreau | 315d807 | 2017-12-10 22:17:57 +0100 | [diff] [blame] | 253 | return 1; |
| 254 | |
| 255 | return 0; |
| 256 | } |
| 257 | |
Willy Tarreau | f210191 | 2018-07-19 10:11:38 +0200 | [diff] [blame] | 258 | /* returns true if the connection has too many conn_streams attached */ |
| 259 | static inline int h2_has_too_many_cs(const struct h2c *h2c) |
| 260 | { |
| 261 | return h2c->nb_cs >= h2_settings_max_concurrent_streams; |
| 262 | } |
| 263 | |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 264 | /* Tries to grab a buffer and to re-enable processing on mux <target>. The h2c |
| 265 | * flags are used to figure what buffer was requested. It returns 1 if the |
| 266 | * allocation succeeds, in which case the connection is woken up, or 0 if it's |
| 267 | * impossible to wake up and we prefer to be woken up later. |
Willy Tarreau | 35dbd5d | 2017-09-22 09:13:49 +0200 | [diff] [blame] | 268 | */ |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 269 | static int h2_buf_available(void *target) |
Willy Tarreau | 35dbd5d | 2017-09-22 09:13:49 +0200 | [diff] [blame] | 270 | { |
| 271 | struct h2c *h2c = target; |
| 272 | |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 273 | 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] | 274 | h2c->flags &= ~H2_CF_DEM_DALLOC; |
Willy Tarreau | 315d807 | 2017-12-10 22:17:57 +0100 | [diff] [blame] | 275 | if (h2_recv_allowed(h2c)) |
Willy Tarreau | 1b62c5c | 2017-09-25 11:55:01 +0200 | [diff] [blame] | 276 | conn_xprt_want_recv(h2c->conn); |
Willy Tarreau | 35dbd5d | 2017-09-22 09:13:49 +0200 | [diff] [blame] | 277 | return 1; |
| 278 | } |
Willy Tarreau | 35dbd5d | 2017-09-22 09:13:49 +0200 | [diff] [blame] | 279 | |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 280 | if ((h2c->flags & H2_CF_MUX_MALLOC) && b_alloc_margin(&h2c->mbuf, 0)) { |
| 281 | h2c->flags &= ~H2_CF_MUX_MALLOC; |
| 282 | if (!(h2c->flags & H2_CF_MUX_BLOCK_ANY)) |
| 283 | conn_xprt_want_send(h2c->conn); |
Willy Tarreau | 1b62c5c | 2017-09-25 11:55:01 +0200 | [diff] [blame] | 284 | |
| 285 | if (h2c->flags & H2_CF_DEM_MROOM) { |
| 286 | h2c->flags &= ~H2_CF_DEM_MROOM; |
Willy Tarreau | 315d807 | 2017-12-10 22:17:57 +0100 | [diff] [blame] | 287 | if (h2_recv_allowed(h2c)) |
Willy Tarreau | 1b62c5c | 2017-09-25 11:55:01 +0200 | [diff] [blame] | 288 | conn_xprt_want_recv(h2c->conn); |
| 289 | } |
Willy Tarreau | 1439812 | 2017-09-22 14:26:04 +0200 | [diff] [blame] | 290 | return 1; |
| 291 | } |
| 292 | return 0; |
| 293 | } |
| 294 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 295 | 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] | 296 | { |
| 297 | struct buffer *buf = NULL; |
| 298 | |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 299 | if (likely(LIST_ISEMPTY(&h2c->buf_wait.list)) && |
| 300 | unlikely((buf = b_alloc_margin(bptr, 0)) == NULL)) { |
| 301 | h2c->buf_wait.target = h2c; |
| 302 | h2c->buf_wait.wakeup_cb = h2_buf_available; |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 303 | HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock); |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 304 | LIST_ADDQ(&buffer_wq, &h2c->buf_wait.list); |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 305 | HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock); |
Willy Tarreau | 1439812 | 2017-09-22 14:26:04 +0200 | [diff] [blame] | 306 | __conn_xprt_stop_recv(h2c->conn); |
| 307 | } |
| 308 | return buf; |
| 309 | } |
| 310 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 311 | static inline void h2_release_buf(struct h2c *h2c, struct buffer *bptr) |
Willy Tarreau | 1439812 | 2017-09-22 14:26:04 +0200 | [diff] [blame] | 312 | { |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 313 | if (bptr->size) { |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 314 | b_free(bptr); |
Olivier Houchard | 673867c | 2018-05-25 16:58:52 +0200 | [diff] [blame] | 315 | offer_buffers(h2c->buf_wait.target, tasks_run_queue); |
Willy Tarreau | 1439812 | 2017-09-22 14:26:04 +0200 | [diff] [blame] | 316 | } |
| 317 | } |
| 318 | |
Willy Tarreau | 35dbd5d | 2017-09-22 09:13:49 +0200 | [diff] [blame] | 319 | |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 320 | /*****************************************************************/ |
| 321 | /* functions below are dedicated to the mux setup and management */ |
| 322 | /*****************************************************************/ |
| 323 | |
Willy Tarreau | 32218eb | 2017-09-22 08:07:25 +0200 | [diff] [blame] | 324 | /* tries to initialize the inbound h2c mux. Returns < 0 in case of failure. */ |
| 325 | static int h2c_frt_init(struct connection *conn) |
| 326 | { |
| 327 | struct h2c *h2c; |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 328 | struct task *t = NULL; |
| 329 | struct session *sess = conn->owner; |
Willy Tarreau | 32218eb | 2017-09-22 08:07:25 +0200 | [diff] [blame] | 330 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 331 | h2c = pool_alloc(pool_head_h2c); |
Willy Tarreau | 32218eb | 2017-09-22 08:07:25 +0200 | [diff] [blame] | 332 | if (!h2c) |
| 333 | goto fail; |
| 334 | |
Willy Tarreau | 3f13357 | 2017-10-31 19:21:06 +0100 | [diff] [blame] | 335 | |
Willy Tarreau | 599391a | 2017-11-24 10:16:00 +0100 | [diff] [blame] | 336 | h2c->shut_timeout = h2c->timeout = sess->fe->timeout.client; |
| 337 | if (tick_isset(sess->fe->timeout.clientfin)) |
| 338 | h2c->shut_timeout = sess->fe->timeout.clientfin; |
| 339 | |
Willy Tarreau | 3340029 | 2017-11-05 11:23:40 +0100 | [diff] [blame] | 340 | h2c->task = NULL; |
Willy Tarreau | 3f13357 | 2017-10-31 19:21:06 +0100 | [diff] [blame] | 341 | if (tick_isset(h2c->timeout)) { |
| 342 | t = task_new(tid_bit); |
| 343 | if (!t) |
| 344 | goto fail; |
| 345 | |
| 346 | h2c->task = t; |
| 347 | t->process = h2_timeout_task; |
| 348 | t->context = h2c; |
| 349 | t->expire = tick_add(now_ms, h2c->timeout); |
| 350 | } |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 351 | |
Olivier Houchard | 910b2bc | 2018-07-17 18:49:38 +0200 | [diff] [blame] | 352 | h2c->wait_list.task = tasklet_new(); |
| 353 | if (!h2c->wait_list.task) |
| 354 | goto fail; |
| 355 | h2c->wait_list.task->process = h2_send; |
| 356 | h2c->wait_list.task->context = conn; |
| 357 | |
Willy Tarreau | 32218eb | 2017-09-22 08:07:25 +0200 | [diff] [blame] | 358 | h2c->ddht = hpack_dht_alloc(h2_settings_header_table_size); |
| 359 | if (!h2c->ddht) |
| 360 | goto fail; |
| 361 | |
| 362 | /* Initialise the context. */ |
| 363 | h2c->st0 = H2_CS_PREFACE; |
| 364 | h2c->conn = conn; |
| 365 | h2c->max_id = -1; |
| 366 | h2c->errcode = H2_ERR_NO_ERROR; |
| 367 | h2c->flags = H2_CF_NONE; |
| 368 | h2c->rcvd_c = 0; |
| 369 | h2c->rcvd_s = 0; |
Willy Tarreau | 4974561 | 2017-12-03 18:56:02 +0100 | [diff] [blame] | 370 | h2c->nb_streams = 0; |
Willy Tarreau | 7ac60e8 | 2018-07-19 09:04:05 +0200 | [diff] [blame] | 371 | h2c->nb_cs = 0; |
Willy Tarreau | 32218eb | 2017-09-22 08:07:25 +0200 | [diff] [blame] | 372 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 373 | h2c->dbuf = BUF_NULL; |
Willy Tarreau | 32218eb | 2017-09-22 08:07:25 +0200 | [diff] [blame] | 374 | h2c->dsi = -1; |
| 375 | h2c->msi = -1; |
| 376 | h2c->last_sid = -1; |
| 377 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 378 | h2c->mbuf = BUF_NULL; |
Willy Tarreau | 32218eb | 2017-09-22 08:07:25 +0200 | [diff] [blame] | 379 | h2c->miw = 65535; /* mux initial window size */ |
| 380 | h2c->mws = 65535; /* mux window size */ |
| 381 | h2c->mfs = 16384; /* initial max frame size */ |
| 382 | h2c->streams_by_id = EB_ROOT_UNIQUE; |
| 383 | LIST_INIT(&h2c->send_list); |
| 384 | LIST_INIT(&h2c->fctl_list); |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 385 | LIST_INIT(&h2c->buf_wait.list); |
Willy Tarreau | 32218eb | 2017-09-22 08:07:25 +0200 | [diff] [blame] | 386 | conn->mux_ctx = h2c; |
| 387 | |
Willy Tarreau | 3f13357 | 2017-10-31 19:21:06 +0100 | [diff] [blame] | 388 | if (t) |
| 389 | task_queue(t); |
Willy Tarreau | 32218eb | 2017-09-22 08:07:25 +0200 | [diff] [blame] | 390 | conn_xprt_want_recv(conn); |
Olivier Houchard | 6ff2039 | 2018-07-17 18:46:31 +0200 | [diff] [blame] | 391 | LIST_INIT(&h2c->send_wait_list); |
Olivier Houchard | 910b2bc | 2018-07-17 18:49:38 +0200 | [diff] [blame] | 392 | LIST_INIT(&h2c->wait_list.list); |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 393 | |
Willy Tarreau | 32218eb | 2017-09-22 08:07:25 +0200 | [diff] [blame] | 394 | /* mux->wake will be called soon to complete the operation */ |
| 395 | return 0; |
| 396 | fail: |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 397 | if (t) |
| 398 | task_free(t); |
Olivier Houchard | 910b2bc | 2018-07-17 18:49:38 +0200 | [diff] [blame] | 399 | if (h2c->wait_list.task) |
| 400 | tasklet_free(h2c->wait_list.task); |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 401 | pool_free(pool_head_h2c, h2c); |
Willy Tarreau | 32218eb | 2017-09-22 08:07:25 +0200 | [diff] [blame] | 402 | return -1; |
| 403 | } |
| 404 | |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 405 | /* Initialize the mux once it's attached. For outgoing connections, the context |
| 406 | * is already initialized before installing the mux, so we detect incoming |
| 407 | * connections from the fact that the context is still NULL. Returns < 0 on |
| 408 | * error. |
| 409 | */ |
| 410 | static int h2_init(struct connection *conn) |
| 411 | { |
| 412 | if (conn->mux_ctx) { |
| 413 | /* we don't support outgoing connections for now */ |
| 414 | return -1; |
| 415 | } |
| 416 | |
Willy Tarreau | 32218eb | 2017-09-22 08:07:25 +0200 | [diff] [blame] | 417 | return h2c_frt_init(conn); |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 418 | } |
| 419 | |
Willy Tarreau | 2373acc | 2017-10-12 17:35:14 +0200 | [diff] [blame] | 420 | /* returns the stream associated with id <id> or NULL if not found */ |
| 421 | static inline struct h2s *h2c_st_by_id(struct h2c *h2c, int id) |
| 422 | { |
| 423 | struct eb32_node *node; |
| 424 | |
Willy Tarreau | 2a85618 | 2017-05-16 15:20:39 +0200 | [diff] [blame] | 425 | if (id > h2c->max_id) |
| 426 | return (struct h2s *)h2_idle_stream; |
| 427 | |
Willy Tarreau | 2373acc | 2017-10-12 17:35:14 +0200 | [diff] [blame] | 428 | node = eb32_lookup(&h2c->streams_by_id, id); |
| 429 | if (!node) |
Willy Tarreau | 2a85618 | 2017-05-16 15:20:39 +0200 | [diff] [blame] | 430 | return (struct h2s *)h2_closed_stream; |
Willy Tarreau | 2373acc | 2017-10-12 17:35:14 +0200 | [diff] [blame] | 431 | |
| 432 | return container_of(node, struct h2s, by_id); |
| 433 | } |
| 434 | |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 435 | /* release function for a connection. This one should be called to free all |
| 436 | * resources allocated to the mux. |
| 437 | */ |
| 438 | static void h2_release(struct connection *conn) |
| 439 | { |
Willy Tarreau | 32218eb | 2017-09-22 08:07:25 +0200 | [diff] [blame] | 440 | struct h2c *h2c = conn->mux_ctx; |
| 441 | |
| 442 | LIST_DEL(&conn->list); |
| 443 | |
| 444 | if (h2c) { |
| 445 | hpack_dht_free(h2c->ddht); |
Willy Tarreau | 1439812 | 2017-09-22 14:26:04 +0200 | [diff] [blame] | 446 | |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 447 | HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock); |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 448 | LIST_DEL(&h2c->buf_wait.list); |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 449 | HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock); |
Willy Tarreau | 1439812 | 2017-09-22 14:26:04 +0200 | [diff] [blame] | 450 | |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 451 | h2_release_buf(h2c, &h2c->dbuf); |
| 452 | h2_release_buf(h2c, &h2c->mbuf); |
| 453 | |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 454 | if (h2c->task) { |
Willy Tarreau | 0975f11 | 2018-03-29 15:22:59 +0200 | [diff] [blame] | 455 | h2c->task->context = NULL; |
| 456 | task_wakeup(h2c->task, TASK_WOKEN_OTHER); |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 457 | h2c->task = NULL; |
| 458 | } |
Olivier Houchard | 910b2bc | 2018-07-17 18:49:38 +0200 | [diff] [blame] | 459 | if (h2c->wait_list.task) |
| 460 | tasklet_free(h2c->wait_list.task); |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 461 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 462 | pool_free(pool_head_h2c, h2c); |
Willy Tarreau | 32218eb | 2017-09-22 08:07:25 +0200 | [diff] [blame] | 463 | } |
| 464 | |
| 465 | conn->mux = NULL; |
| 466 | conn->mux_ctx = NULL; |
| 467 | |
| 468 | conn_stop_tracking(conn); |
| 469 | conn_full_close(conn); |
| 470 | if (conn->destroy_cb) |
| 471 | conn->destroy_cb(conn); |
| 472 | conn_free(conn); |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 473 | } |
| 474 | |
| 475 | |
Willy Tarreau | 7168117 | 2017-10-23 14:39:06 +0200 | [diff] [blame] | 476 | /******************************************************/ |
| 477 | /* functions below are for the H2 protocol processing */ |
| 478 | /******************************************************/ |
| 479 | |
| 480 | /* 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] | 481 | static inline __maybe_unused int h2s_id(const struct h2s *h2s) |
Willy Tarreau | 7168117 | 2017-10-23 14:39:06 +0200 | [diff] [blame] | 482 | { |
| 483 | return h2s ? h2s->id : 0; |
| 484 | } |
| 485 | |
Willy Tarreau | 5b5e687 | 2017-09-25 16:17:25 +0200 | [diff] [blame] | 486 | /* 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] | 487 | 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] | 488 | { |
| 489 | if (h2c->msi < 0) |
| 490 | return 0; |
| 491 | |
| 492 | if (h2c->msi == h2s_id(h2s)) |
| 493 | return 0; |
| 494 | |
| 495 | return 1; |
| 496 | } |
| 497 | |
Willy Tarreau | 741d6df | 2017-10-17 08:00:59 +0200 | [diff] [blame] | 498 | /* marks an error on the connection */ |
Willy Tarreau | 1f09467 | 2017-11-20 21:27:45 +0100 | [diff] [blame] | 499 | 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] | 500 | { |
| 501 | h2c->errcode = err; |
| 502 | h2c->st0 = H2_CS_ERROR; |
| 503 | } |
| 504 | |
Willy Tarreau | 2e43f08 | 2017-10-17 08:03:59 +0200 | [diff] [blame] | 505 | /* marks an error on the stream */ |
Willy Tarreau | 1f09467 | 2017-11-20 21:27:45 +0100 | [diff] [blame] | 506 | 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] | 507 | { |
| 508 | if (h2s->st > H2_SS_IDLE && h2s->st < H2_SS_ERROR) { |
| 509 | h2s->errcode = err; |
| 510 | h2s->st = H2_SS_ERROR; |
| 511 | if (h2s->cs) |
| 512 | h2s->cs->flags |= CS_FL_ERROR; |
| 513 | } |
| 514 | } |
| 515 | |
Willy Tarreau | e482074 | 2017-07-27 13:37:23 +0200 | [diff] [blame] | 516 | /* writes the 24-bit frame size <len> at address <frame> */ |
Willy Tarreau | 1f09467 | 2017-11-20 21:27:45 +0100 | [diff] [blame] | 517 | 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] | 518 | { |
| 519 | uint8_t *out = frame; |
| 520 | |
| 521 | *out = len >> 16; |
| 522 | write_n16(out + 1, len); |
| 523 | } |
| 524 | |
Willy Tarreau | 54c1506 | 2017-10-10 17:10:03 +0200 | [diff] [blame] | 525 | /* reads <bytes> bytes from buffer <b> starting at relative offset <o> from the |
| 526 | * current pointer, dealing with wrapping, and stores the result in <dst>. It's |
| 527 | * 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] | 528 | * available in the buffer's input prior to calling this function. The buffer |
| 529 | * is assumed not to hold any output data. |
Willy Tarreau | 54c1506 | 2017-10-10 17:10:03 +0200 | [diff] [blame] | 530 | */ |
Willy Tarreau | 1f09467 | 2017-11-20 21:27:45 +0100 | [diff] [blame] | 531 | 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] | 532 | const struct buffer *b, int o) |
| 533 | { |
Willy Tarreau | 591d445 | 2018-06-15 17:21:00 +0200 | [diff] [blame] | 534 | 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] | 535 | } |
| 536 | |
Willy Tarreau | 1f09467 | 2017-11-20 21:27:45 +0100 | [diff] [blame] | 537 | 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] | 538 | { |
Willy Tarreau | 591d445 | 2018-06-15 17:21:00 +0200 | [diff] [blame] | 539 | 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] | 540 | } |
| 541 | |
Willy Tarreau | 1f09467 | 2017-11-20 21:27:45 +0100 | [diff] [blame] | 542 | 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] | 543 | { |
Willy Tarreau | 591d445 | 2018-06-15 17:21:00 +0200 | [diff] [blame] | 544 | 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] | 545 | } |
| 546 | |
Willy Tarreau | 1f09467 | 2017-11-20 21:27:45 +0100 | [diff] [blame] | 547 | 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] | 548 | { |
Willy Tarreau | 591d445 | 2018-06-15 17:21:00 +0200 | [diff] [blame] | 549 | 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] | 550 | } |
| 551 | |
| 552 | |
Willy Tarreau | 715d531 | 2017-07-11 15:20:24 +0200 | [diff] [blame] | 553 | /* Peeks an H2 frame header from buffer <b> into descriptor <h>. The algorithm |
| 554 | * is not obvious. It turns out that H2 headers are neither aligned nor do they |
| 555 | * use regular sizes. And to add to the trouble, the buffer may wrap so each |
| 556 | * byte read must be checked. The header is formed like this : |
| 557 | * |
| 558 | * b0 b1 b2 b3 b4 b5..b8 |
| 559 | * +----------+---------+--------+----+----+----------------------+ |
| 560 | * |len[23:16]|len[15:8]|len[7:0]|type|flag|sid[31:0] (big endian)| |
| 561 | * +----------+---------+--------+----+----+----------------------+ |
| 562 | * |
| 563 | * Here we read a big-endian 64 bit word from h[1]. This way in a single read |
| 564 | * we get the sid properly aligned and ordered, and 16 bits of len properly |
| 565 | * ordered as well. The type and flags can be extracted using bit shifts from |
| 566 | * 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] | 567 | * Returns zero if some bytes are missing, otherwise non-zero on success. The |
| 568 | * buffer is assumed not to contain any output data. |
Willy Tarreau | 715d531 | 2017-07-11 15:20:24 +0200 | [diff] [blame] | 569 | */ |
Willy Tarreau | 1f09467 | 2017-11-20 21:27:45 +0100 | [diff] [blame] | 570 | static __maybe_unused int h2_peek_frame_hdr(const struct buffer *b, struct h2_fh *h) |
Willy Tarreau | 715d531 | 2017-07-11 15:20:24 +0200 | [diff] [blame] | 571 | { |
| 572 | uint64_t w; |
| 573 | |
Willy Tarreau | b7b5fe1 | 2018-06-18 13:33:09 +0200 | [diff] [blame] | 574 | if (b_data(b) < 9) |
Willy Tarreau | 715d531 | 2017-07-11 15:20:24 +0200 | [diff] [blame] | 575 | return 0; |
| 576 | |
Willy Tarreau | 9c7f2d1 | 2018-06-15 11:51:32 +0200 | [diff] [blame] | 577 | w = h2_get_n64(b, 1); |
Willy Tarreau | b7b5fe1 | 2018-06-18 13:33:09 +0200 | [diff] [blame] | 578 | h->len = *(uint8_t*)b_head(b) << 16; |
Willy Tarreau | 715d531 | 2017-07-11 15:20:24 +0200 | [diff] [blame] | 579 | h->sid = w & 0x7FFFFFFF; /* RFC7540#4.1: R bit must be ignored */ |
| 580 | h->ff = w >> 32; |
| 581 | h->ft = w >> 40; |
| 582 | h->len += w >> 48; |
| 583 | return 1; |
| 584 | } |
| 585 | |
| 586 | /* skip the next 9 bytes corresponding to the frame header possibly parsed by |
| 587 | * h2_peek_frame_hdr() above. |
| 588 | */ |
Willy Tarreau | 1f09467 | 2017-11-20 21:27:45 +0100 | [diff] [blame] | 589 | static inline __maybe_unused void h2_skip_frame_hdr(struct buffer *b) |
Willy Tarreau | 715d531 | 2017-07-11 15:20:24 +0200 | [diff] [blame] | 590 | { |
Willy Tarreau | e5f12ce | 2018-06-15 10:28:05 +0200 | [diff] [blame] | 591 | b_del(b, 9); |
Willy Tarreau | 715d531 | 2017-07-11 15:20:24 +0200 | [diff] [blame] | 592 | } |
| 593 | |
| 594 | /* same as above, automatically advances the buffer on success */ |
Willy Tarreau | 1f09467 | 2017-11-20 21:27:45 +0100 | [diff] [blame] | 595 | 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] | 596 | { |
| 597 | int ret; |
| 598 | |
| 599 | ret = h2_peek_frame_hdr(b, h); |
| 600 | if (ret > 0) |
| 601 | h2_skip_frame_hdr(b); |
| 602 | return ret; |
| 603 | } |
| 604 | |
Willy Tarreau | 00dd078 | 2018-03-01 16:31:34 +0100 | [diff] [blame] | 605 | /* marks stream <h2s> as CLOSED and decrement the number of active streams for |
| 606 | * its connection if the stream was not yet closed. Please use this exclusively |
| 607 | * before closing a stream to ensure stream count is well maintained. |
Willy Tarreau | 91bfdd7 | 2017-12-14 12:00:14 +0100 | [diff] [blame] | 608 | */ |
Willy Tarreau | 00dd078 | 2018-03-01 16:31:34 +0100 | [diff] [blame] | 609 | static inline void h2s_close(struct h2s *h2s) |
Willy Tarreau | 91bfdd7 | 2017-12-14 12:00:14 +0100 | [diff] [blame] | 610 | { |
| 611 | if (h2s->st != H2_SS_CLOSED) |
| 612 | h2s->h2c->nb_streams--; |
| 613 | h2s->st = H2_SS_CLOSED; |
| 614 | } |
| 615 | |
Willy Tarreau | 71049cc | 2018-03-28 13:56:39 +0200 | [diff] [blame] | 616 | /* detaches an H2 stream from its H2C and releases it to the H2S pool. */ |
| 617 | static void h2s_destroy(struct h2s *h2s) |
Willy Tarreau | 0a10de6 | 2018-03-01 16:27:53 +0100 | [diff] [blame] | 618 | { |
| 619 | h2s_close(h2s); |
Willy Tarreau | 4a333d3 | 2018-03-28 11:29:04 +0200 | [diff] [blame] | 620 | LIST_DEL(&h2s->list); |
| 621 | LIST_INIT(&h2s->list); |
Willy Tarreau | 0a10de6 | 2018-03-01 16:27:53 +0100 | [diff] [blame] | 622 | eb32_delete(&h2s->by_id); |
Willy Tarreau | 0a10de6 | 2018-03-01 16:27:53 +0100 | [diff] [blame] | 623 | pool_free(pool_head_h2s, h2s); |
| 624 | } |
| 625 | |
Willy Tarreau | 3ccf4b2 | 2017-10-13 19:07:26 +0200 | [diff] [blame] | 626 | /* creates a new stream <id> on the h2c connection and returns it, or NULL in |
| 627 | * case of memory allocation error. |
| 628 | */ |
| 629 | static struct h2s *h2c_stream_new(struct h2c *h2c, int id) |
| 630 | { |
| 631 | struct conn_stream *cs; |
| 632 | struct h2s *h2s; |
| 633 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 634 | h2s = pool_alloc(pool_head_h2s); |
Willy Tarreau | 3ccf4b2 | 2017-10-13 19:07:26 +0200 | [diff] [blame] | 635 | if (!h2s) |
| 636 | goto out; |
| 637 | |
| 638 | h2s->h2c = h2c; |
| 639 | h2s->mws = h2c->miw; |
| 640 | h2s->flags = H2_SF_NONE; |
| 641 | h2s->errcode = H2_ERR_NO_ERROR; |
| 642 | h2s->st = H2_SS_IDLE; |
| 643 | h1m_init(&h2s->req); |
| 644 | h1m_init(&h2s->res); |
| 645 | h2s->by_id.key = h2s->id = id; |
| 646 | h2c->max_id = id; |
| 647 | LIST_INIT(&h2s->list); |
| 648 | |
| 649 | eb32_insert(&h2c->streams_by_id, &h2s->by_id); |
Willy Tarreau | 4974561 | 2017-12-03 18:56:02 +0100 | [diff] [blame] | 650 | h2c->nb_streams++; |
| 651 | if (h2c->nb_streams > h2_settings_max_concurrent_streams) |
| 652 | goto out_close; |
Willy Tarreau | 3ccf4b2 | 2017-10-13 19:07:26 +0200 | [diff] [blame] | 653 | |
| 654 | cs = cs_new(h2c->conn); |
| 655 | if (!cs) |
| 656 | goto out_close; |
| 657 | |
| 658 | h2s->cs = cs; |
| 659 | cs->ctx = h2s; |
Willy Tarreau | 7ac60e8 | 2018-07-19 09:04:05 +0200 | [diff] [blame] | 660 | h2c->nb_cs++; |
Willy Tarreau | 3ccf4b2 | 2017-10-13 19:07:26 +0200 | [diff] [blame] | 661 | |
| 662 | if (stream_create_from_cs(cs) < 0) |
| 663 | goto out_free_cs; |
| 664 | |
| 665 | /* OK done, the stream lives its own life now */ |
Willy Tarreau | f210191 | 2018-07-19 10:11:38 +0200 | [diff] [blame] | 666 | if (h2_has_too_many_cs(h2c)) |
| 667 | h2c->flags |= H2_CF_DEM_TOOMANY; |
Willy Tarreau | 3ccf4b2 | 2017-10-13 19:07:26 +0200 | [diff] [blame] | 668 | return h2s; |
| 669 | |
| 670 | out_free_cs: |
Willy Tarreau | 7ac60e8 | 2018-07-19 09:04:05 +0200 | [diff] [blame] | 671 | h2c->nb_cs--; |
Willy Tarreau | 3ccf4b2 | 2017-10-13 19:07:26 +0200 | [diff] [blame] | 672 | cs_free(cs); |
| 673 | out_close: |
Willy Tarreau | 71049cc | 2018-03-28 13:56:39 +0200 | [diff] [blame] | 674 | h2s_destroy(h2s); |
Willy Tarreau | 3ccf4b2 | 2017-10-13 19:07:26 +0200 | [diff] [blame] | 675 | h2s = NULL; |
| 676 | out: |
| 677 | return h2s; |
| 678 | } |
| 679 | |
Willy Tarreau | be5b715 | 2017-09-25 16:25:39 +0200 | [diff] [blame] | 680 | /* try to send a settings frame on the connection. Returns > 0 on success, 0 if |
| 681 | * it couldn't do anything. It may return an error in h2c. See RFC7540#11.3 for |
| 682 | * the various settings codes. |
| 683 | */ |
| 684 | static int h2c_snd_settings(struct h2c *h2c) |
| 685 | { |
| 686 | struct buffer *res; |
| 687 | char buf_data[100]; // enough for 15 settings |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 688 | struct buffer buf; |
Willy Tarreau | be5b715 | 2017-09-25 16:25:39 +0200 | [diff] [blame] | 689 | int ret; |
| 690 | |
| 691 | if (h2c_mux_busy(h2c, NULL)) { |
| 692 | h2c->flags |= H2_CF_DEM_MBUSY; |
| 693 | return 0; |
| 694 | } |
| 695 | |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 696 | res = h2_get_buf(h2c, &h2c->mbuf); |
Willy Tarreau | be5b715 | 2017-09-25 16:25:39 +0200 | [diff] [blame] | 697 | if (!res) { |
| 698 | h2c->flags |= H2_CF_MUX_MALLOC; |
| 699 | h2c->flags |= H2_CF_DEM_MROOM; |
| 700 | return 0; |
| 701 | } |
| 702 | |
| 703 | chunk_init(&buf, buf_data, sizeof(buf_data)); |
| 704 | chunk_memcpy(&buf, |
| 705 | "\x00\x00\x00" /* length : 0 for now */ |
| 706 | "\x04\x00" /* type : 4 (settings), flags : 0 */ |
| 707 | "\x00\x00\x00\x00", /* stream ID : 0 */ |
| 708 | 9); |
| 709 | |
| 710 | if (h2_settings_header_table_size != 4096) { |
| 711 | char str[6] = "\x00\x01"; /* header_table_size */ |
| 712 | |
| 713 | write_n32(str + 2, h2_settings_header_table_size); |
| 714 | chunk_memcat(&buf, str, 6); |
| 715 | } |
| 716 | |
| 717 | if (h2_settings_initial_window_size != 65535) { |
| 718 | char str[6] = "\x00\x04"; /* initial_window_size */ |
| 719 | |
| 720 | write_n32(str + 2, h2_settings_initial_window_size); |
| 721 | chunk_memcat(&buf, str, 6); |
| 722 | } |
| 723 | |
| 724 | if (h2_settings_max_concurrent_streams != 0) { |
| 725 | char str[6] = "\x00\x03"; /* max_concurrent_streams */ |
| 726 | |
| 727 | /* Note: 0 means "unlimited" for haproxy's config but not for |
| 728 | * the protocol, so never send this value! |
| 729 | */ |
| 730 | write_n32(str + 2, h2_settings_max_concurrent_streams); |
| 731 | chunk_memcat(&buf, str, 6); |
| 732 | } |
| 733 | |
| 734 | if (global.tune.bufsize != 16384) { |
| 735 | char str[6] = "\x00\x05"; /* max_frame_size */ |
| 736 | |
| 737 | /* note: similarly we could also emit MAX_HEADER_LIST_SIZE to |
| 738 | * match bufsize - rewrite size, but at the moment it seems |
| 739 | * that clients don't take care of it. |
| 740 | */ |
| 741 | write_n32(str + 2, global.tune.bufsize); |
| 742 | chunk_memcat(&buf, str, 6); |
| 743 | } |
| 744 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 745 | h2_set_frame_size(buf.area, buf.data - 9); |
| 746 | ret = b_istput(res, ist2(buf.area, buf.data)); |
Willy Tarreau | be5b715 | 2017-09-25 16:25:39 +0200 | [diff] [blame] | 747 | if (unlikely(ret <= 0)) { |
| 748 | if (!ret) { |
| 749 | h2c->flags |= H2_CF_MUX_MFULL; |
| 750 | h2c->flags |= H2_CF_DEM_MROOM; |
| 751 | return 0; |
| 752 | } |
| 753 | else { |
| 754 | h2c_error(h2c, H2_ERR_INTERNAL_ERROR); |
| 755 | return 0; |
| 756 | } |
| 757 | } |
| 758 | return ret; |
| 759 | } |
| 760 | |
Willy Tarreau | 52eed75 | 2017-09-22 15:05:09 +0200 | [diff] [blame] | 761 | /* Try to receive a connection preface, then upon success try to send our |
| 762 | * preface which is a SETTINGS frame. Returns > 0 on success or zero on |
| 763 | * missing data. It may return an error in h2c. |
| 764 | */ |
| 765 | static int h2c_frt_recv_preface(struct h2c *h2c) |
| 766 | { |
| 767 | int ret1; |
Willy Tarreau | be5b715 | 2017-09-25 16:25:39 +0200 | [diff] [blame] | 768 | int ret2; |
Willy Tarreau | 52eed75 | 2017-09-22 15:05:09 +0200 | [diff] [blame] | 769 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 770 | 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] | 771 | |
| 772 | if (unlikely(ret1 <= 0)) { |
| 773 | if (ret1 < 0 || conn_xprt_read0_pending(h2c->conn)) |
| 774 | h2c_error(h2c, H2_ERR_PROTOCOL_ERROR); |
| 775 | return 0; |
| 776 | } |
| 777 | |
Willy Tarreau | be5b715 | 2017-09-25 16:25:39 +0200 | [diff] [blame] | 778 | ret2 = h2c_snd_settings(h2c); |
| 779 | if (ret2 > 0) |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 780 | b_del(&h2c->dbuf, ret1); |
Willy Tarreau | 52eed75 | 2017-09-22 15:05:09 +0200 | [diff] [blame] | 781 | |
Willy Tarreau | be5b715 | 2017-09-25 16:25:39 +0200 | [diff] [blame] | 782 | return ret2; |
Willy Tarreau | 52eed75 | 2017-09-22 15:05:09 +0200 | [diff] [blame] | 783 | } |
| 784 | |
Willy Tarreau | 081d472 | 2017-05-16 21:51:05 +0200 | [diff] [blame] | 785 | /* try to send a GOAWAY frame on the connection to report an error or a graceful |
| 786 | * shutdown, with h2c->errcode as the error code. Returns > 0 on success or zero |
| 787 | * if nothing was done. It uses h2c->last_sid as the advertised ID, or copies it |
| 788 | * from h2c->max_id if it's not set yet (<0). In case of lack of room to write |
| 789 | * the message, it subscribes the requester (either <h2s> or <h2c>) to future |
| 790 | * notifications. It sets H2_CF_GOAWAY_SENT on success, and H2_CF_GOAWAY_FAILED |
| 791 | * on unrecoverable failure. It will not attempt to send one again in this last |
| 792 | * case so that it is safe to use h2c_error() to report such errors. |
| 793 | */ |
| 794 | static int h2c_send_goaway_error(struct h2c *h2c, struct h2s *h2s) |
| 795 | { |
| 796 | struct buffer *res; |
| 797 | char str[17]; |
| 798 | int ret; |
| 799 | |
| 800 | if (h2c->flags & H2_CF_GOAWAY_FAILED) |
| 801 | return 1; // claim that it worked |
| 802 | |
| 803 | if (h2c_mux_busy(h2c, h2s)) { |
| 804 | if (h2s) |
| 805 | h2s->flags |= H2_SF_BLK_MBUSY; |
| 806 | else |
| 807 | h2c->flags |= H2_CF_DEM_MBUSY; |
| 808 | return 0; |
| 809 | } |
| 810 | |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 811 | res = h2_get_buf(h2c, &h2c->mbuf); |
Willy Tarreau | 081d472 | 2017-05-16 21:51:05 +0200 | [diff] [blame] | 812 | if (!res) { |
| 813 | h2c->flags |= H2_CF_MUX_MALLOC; |
| 814 | if (h2s) |
| 815 | h2s->flags |= H2_SF_BLK_MROOM; |
| 816 | else |
| 817 | h2c->flags |= H2_CF_DEM_MROOM; |
| 818 | return 0; |
| 819 | } |
| 820 | |
| 821 | /* len: 8, type: 7, flags: none, sid: 0 */ |
| 822 | memcpy(str, "\x00\x00\x08\x07\x00\x00\x00\x00\x00", 9); |
| 823 | |
| 824 | if (h2c->last_sid < 0) |
| 825 | h2c->last_sid = h2c->max_id; |
| 826 | |
| 827 | write_n32(str + 9, h2c->last_sid); |
| 828 | write_n32(str + 13, h2c->errcode); |
Willy Tarreau | ea1b06d | 2018-07-12 09:02:47 +0200 | [diff] [blame] | 829 | ret = b_istput(res, ist2(str, 17)); |
Willy Tarreau | 081d472 | 2017-05-16 21:51:05 +0200 | [diff] [blame] | 830 | if (unlikely(ret <= 0)) { |
| 831 | if (!ret) { |
| 832 | h2c->flags |= H2_CF_MUX_MFULL; |
| 833 | if (h2s) |
| 834 | h2s->flags |= H2_SF_BLK_MROOM; |
| 835 | else |
| 836 | h2c->flags |= H2_CF_DEM_MROOM; |
| 837 | return 0; |
| 838 | } |
| 839 | else { |
| 840 | /* we cannot report this error using GOAWAY, so we mark |
| 841 | * it and claim a success. |
| 842 | */ |
| 843 | h2c_error(h2c, H2_ERR_INTERNAL_ERROR); |
| 844 | h2c->flags |= H2_CF_GOAWAY_FAILED; |
| 845 | return 1; |
| 846 | } |
| 847 | } |
| 848 | h2c->flags |= H2_CF_GOAWAY_SENT; |
| 849 | return ret; |
| 850 | } |
| 851 | |
Willy Tarreau | 8c0ea7d | 2017-11-10 10:05:24 +0100 | [diff] [blame] | 852 | /* Try to send an RST_STREAM frame on the connection for the indicated stream |
| 853 | * during mux operations. This stream must be valid and cannot be closed |
| 854 | * already. h2s->id will be used for the stream ID and h2s->errcode will be |
| 855 | * used for the error code. h2s->st will be update to H2_SS_CLOSED if it was |
| 856 | * not yet. |
| 857 | * |
| 858 | * Returns > 0 on success or zero if nothing was done. In case of lack of room |
| 859 | * to write the message, it subscribes the stream to future notifications. |
| 860 | */ |
| 861 | static int h2s_send_rst_stream(struct h2c *h2c, struct h2s *h2s) |
| 862 | { |
| 863 | struct buffer *res; |
| 864 | char str[13]; |
| 865 | int ret; |
| 866 | |
| 867 | if (!h2s || h2s->st == H2_SS_CLOSED) |
| 868 | return 1; |
| 869 | |
Willy Tarreau | 8adae7c | 2018-03-22 17:37:05 +0100 | [diff] [blame] | 870 | /* RFC7540#5.4.2: To avoid looping, an endpoint MUST NOT send a |
| 871 | * RST_STREAM in response to a RST_STREAM frame. |
| 872 | */ |
| 873 | if (h2c->dft == H2_FT_RST_STREAM) { |
| 874 | ret = 1; |
| 875 | goto ignore; |
| 876 | } |
| 877 | |
Willy Tarreau | 8c0ea7d | 2017-11-10 10:05:24 +0100 | [diff] [blame] | 878 | if (h2c_mux_busy(h2c, h2s)) { |
| 879 | h2s->flags |= H2_SF_BLK_MBUSY; |
| 880 | return 0; |
| 881 | } |
| 882 | |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 883 | res = h2_get_buf(h2c, &h2c->mbuf); |
Willy Tarreau | 8c0ea7d | 2017-11-10 10:05:24 +0100 | [diff] [blame] | 884 | if (!res) { |
| 885 | h2c->flags |= H2_CF_MUX_MALLOC; |
| 886 | h2s->flags |= H2_SF_BLK_MROOM; |
| 887 | return 0; |
| 888 | } |
| 889 | |
| 890 | /* len: 4, type: 3, flags: none */ |
| 891 | memcpy(str, "\x00\x00\x04\x03\x00", 5); |
| 892 | write_n32(str + 5, h2s->id); |
| 893 | write_n32(str + 9, h2s->errcode); |
Willy Tarreau | ea1b06d | 2018-07-12 09:02:47 +0200 | [diff] [blame] | 894 | ret = b_istput(res, ist2(str, 13)); |
Willy Tarreau | 8c0ea7d | 2017-11-10 10:05:24 +0100 | [diff] [blame] | 895 | |
| 896 | if (unlikely(ret <= 0)) { |
| 897 | if (!ret) { |
| 898 | h2c->flags |= H2_CF_MUX_MFULL; |
| 899 | h2s->flags |= H2_SF_BLK_MROOM; |
| 900 | return 0; |
| 901 | } |
| 902 | else { |
| 903 | h2c_error(h2c, H2_ERR_INTERNAL_ERROR); |
| 904 | return 0; |
| 905 | } |
| 906 | } |
| 907 | |
Willy Tarreau | 8adae7c | 2018-03-22 17:37:05 +0100 | [diff] [blame] | 908 | ignore: |
Willy Tarreau | 8c0ea7d | 2017-11-10 10:05:24 +0100 | [diff] [blame] | 909 | h2s->flags |= H2_SF_RST_SENT; |
Willy Tarreau | 00dd078 | 2018-03-01 16:31:34 +0100 | [diff] [blame] | 910 | h2s_close(h2s); |
Willy Tarreau | 8c0ea7d | 2017-11-10 10:05:24 +0100 | [diff] [blame] | 911 | return ret; |
| 912 | } |
| 913 | |
| 914 | /* Try to send an RST_STREAM frame on the connection for the stream being |
| 915 | * demuxed using h2c->dsi for the stream ID. It will use h2s->errcode as the |
| 916 | * error code unless the stream's state already is IDLE or CLOSED in which |
| 917 | * case STREAM_CLOSED will be used, and will update h2s->st to H2_SS_CLOSED if |
| 918 | * it was not yet. |
| 919 | * |
| 920 | * Returns > 0 on success or zero if nothing was done. In case of lack of room |
| 921 | * to write the message, it blocks the demuxer and subscribes it to future |
Willy Tarreau | 27a84c9 | 2017-10-17 08:10:17 +0200 | [diff] [blame] | 922 | * notifications. It's worth mentionning that an RST may even be sent for a |
Willy Tarreau | 8c0ea7d | 2017-11-10 10:05:24 +0100 | [diff] [blame] | 923 | * closed stream. |
Willy Tarreau | 27a84c9 | 2017-10-17 08:10:17 +0200 | [diff] [blame] | 924 | */ |
| 925 | static int h2c_send_rst_stream(struct h2c *h2c, struct h2s *h2s) |
| 926 | { |
| 927 | struct buffer *res; |
| 928 | char str[13]; |
| 929 | int ret; |
| 930 | |
Willy Tarreau | 8adae7c | 2018-03-22 17:37:05 +0100 | [diff] [blame] | 931 | /* RFC7540#5.4.2: To avoid looping, an endpoint MUST NOT send a |
| 932 | * RST_STREAM in response to a RST_STREAM frame. |
| 933 | */ |
| 934 | if (h2c->dft == H2_FT_RST_STREAM) { |
| 935 | ret = 1; |
| 936 | goto ignore; |
| 937 | } |
| 938 | |
Willy Tarreau | 27a84c9 | 2017-10-17 08:10:17 +0200 | [diff] [blame] | 939 | if (h2c_mux_busy(h2c, h2s)) { |
Willy Tarreau | 8c0ea7d | 2017-11-10 10:05:24 +0100 | [diff] [blame] | 940 | h2c->flags |= H2_CF_DEM_MBUSY; |
Willy Tarreau | 27a84c9 | 2017-10-17 08:10:17 +0200 | [diff] [blame] | 941 | return 0; |
| 942 | } |
| 943 | |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 944 | res = h2_get_buf(h2c, &h2c->mbuf); |
Willy Tarreau | 27a84c9 | 2017-10-17 08:10:17 +0200 | [diff] [blame] | 945 | if (!res) { |
| 946 | h2c->flags |= H2_CF_MUX_MALLOC; |
Willy Tarreau | 8c0ea7d | 2017-11-10 10:05:24 +0100 | [diff] [blame] | 947 | h2c->flags |= H2_CF_DEM_MROOM; |
Willy Tarreau | 27a84c9 | 2017-10-17 08:10:17 +0200 | [diff] [blame] | 948 | return 0; |
| 949 | } |
| 950 | |
| 951 | /* len: 4, type: 3, flags: none */ |
| 952 | memcpy(str, "\x00\x00\x04\x03\x00", 5); |
Willy Tarreau | 8c0ea7d | 2017-11-10 10:05:24 +0100 | [diff] [blame] | 953 | |
Willy Tarreau | 27a84c9 | 2017-10-17 08:10:17 +0200 | [diff] [blame] | 954 | write_n32(str + 5, h2c->dsi); |
Willy Tarreau | 721c974 | 2017-11-07 11:05:42 +0100 | [diff] [blame] | 955 | write_n32(str + 9, (h2s->st > H2_SS_IDLE && h2s->st < H2_SS_CLOSED) ? |
Willy Tarreau | 27a84c9 | 2017-10-17 08:10:17 +0200 | [diff] [blame] | 956 | h2s->errcode : H2_ERR_STREAM_CLOSED); |
Willy Tarreau | ea1b06d | 2018-07-12 09:02:47 +0200 | [diff] [blame] | 957 | ret = b_istput(res, ist2(str, 13)); |
Willy Tarreau | 8c0ea7d | 2017-11-10 10:05:24 +0100 | [diff] [blame] | 958 | |
Willy Tarreau | 27a84c9 | 2017-10-17 08:10:17 +0200 | [diff] [blame] | 959 | if (unlikely(ret <= 0)) { |
| 960 | if (!ret) { |
| 961 | h2c->flags |= H2_CF_MUX_MFULL; |
Willy Tarreau | 8c0ea7d | 2017-11-10 10:05:24 +0100 | [diff] [blame] | 962 | h2c->flags |= H2_CF_DEM_MROOM; |
Willy Tarreau | 27a84c9 | 2017-10-17 08:10:17 +0200 | [diff] [blame] | 963 | return 0; |
| 964 | } |
| 965 | else { |
| 966 | h2c_error(h2c, H2_ERR_INTERNAL_ERROR); |
| 967 | return 0; |
| 968 | } |
| 969 | } |
| 970 | |
Willy Tarreau | 8adae7c | 2018-03-22 17:37:05 +0100 | [diff] [blame] | 971 | ignore: |
Willy Tarreau | 8c0ea7d | 2017-11-10 10:05:24 +0100 | [diff] [blame] | 972 | if (h2s->st > H2_SS_IDLE && h2s->st < H2_SS_CLOSED) { |
Willy Tarreau | 27a84c9 | 2017-10-17 08:10:17 +0200 | [diff] [blame] | 973 | h2s->flags |= H2_SF_RST_SENT; |
Willy Tarreau | 00dd078 | 2018-03-01 16:31:34 +0100 | [diff] [blame] | 974 | h2s_close(h2s); |
Willy Tarreau | 8c0ea7d | 2017-11-10 10:05:24 +0100 | [diff] [blame] | 975 | } |
| 976 | |
Willy Tarreau | 27a84c9 | 2017-10-17 08:10:17 +0200 | [diff] [blame] | 977 | return ret; |
| 978 | } |
| 979 | |
Willy Tarreau | c7576ea | 2017-10-29 22:00:09 +0100 | [diff] [blame] | 980 | /* try to send an empty DATA frame with the ES flag set to notify about the |
| 981 | * end of stream and match a shutdown(write). If an ES was already sent as |
| 982 | * indicated by HLOC/ERROR/RESET/CLOSED states, nothing is done. Returns > 0 |
| 983 | * on success or zero if nothing was done. In case of lack of room to write the |
| 984 | * message, it subscribes the requesting stream to future notifications. |
| 985 | */ |
| 986 | static int h2_send_empty_data_es(struct h2s *h2s) |
| 987 | { |
| 988 | struct h2c *h2c = h2s->h2c; |
| 989 | struct buffer *res; |
| 990 | char str[9]; |
| 991 | int ret; |
| 992 | |
Willy Tarreau | 721c974 | 2017-11-07 11:05:42 +0100 | [diff] [blame] | 993 | 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] | 994 | return 1; |
| 995 | |
| 996 | if (h2c_mux_busy(h2c, h2s)) { |
| 997 | h2s->flags |= H2_SF_BLK_MBUSY; |
| 998 | return 0; |
| 999 | } |
| 1000 | |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 1001 | res = h2_get_buf(h2c, &h2c->mbuf); |
Willy Tarreau | c7576ea | 2017-10-29 22:00:09 +0100 | [diff] [blame] | 1002 | if (!res) { |
| 1003 | h2c->flags |= H2_CF_MUX_MALLOC; |
| 1004 | h2s->flags |= H2_SF_BLK_MROOM; |
| 1005 | return 0; |
| 1006 | } |
| 1007 | |
| 1008 | /* len: 0x000000, type: 0(DATA), flags: ES=1 */ |
| 1009 | memcpy(str, "\x00\x00\x00\x00\x01", 5); |
| 1010 | write_n32(str + 5, h2s->id); |
Willy Tarreau | ea1b06d | 2018-07-12 09:02:47 +0200 | [diff] [blame] | 1011 | ret = b_istput(res, ist2(str, 9)); |
Willy Tarreau | 6d8b682 | 2017-11-07 14:39:09 +0100 | [diff] [blame] | 1012 | if (likely(ret > 0)) { |
| 1013 | h2s->flags |= H2_SF_ES_SENT; |
| 1014 | } |
| 1015 | else if (!ret) { |
| 1016 | h2c->flags |= H2_CF_MUX_MFULL; |
| 1017 | h2s->flags |= H2_SF_BLK_MROOM; |
| 1018 | return 0; |
| 1019 | } |
| 1020 | else { |
| 1021 | h2c_error(h2c, H2_ERR_INTERNAL_ERROR); |
| 1022 | return 0; |
Willy Tarreau | c7576ea | 2017-10-29 22:00:09 +0100 | [diff] [blame] | 1023 | } |
| 1024 | return ret; |
| 1025 | } |
| 1026 | |
Willy Tarreau | 23b92aa | 2017-10-30 00:26:54 +0100 | [diff] [blame] | 1027 | /* wake the streams attached to the connection, whose id is greater than <last>, |
| 1028 | * and assign their conn_stream the CS_FL_* flags <flags> in addition to |
| 1029 | * CS_FL_ERROR in case of error and CS_FL_EOS in case of closed connection. The |
| 1030 | * stream's state is automatically updated accordingly. |
| 1031 | */ |
| 1032 | static void h2_wake_some_streams(struct h2c *h2c, int last, uint32_t flags) |
| 1033 | { |
| 1034 | struct eb32_node *node; |
| 1035 | struct h2s *h2s; |
| 1036 | |
| 1037 | if (h2c->st0 >= H2_CS_ERROR || h2c->conn->flags & CO_FL_ERROR) |
| 1038 | flags |= CS_FL_ERROR; |
| 1039 | |
| 1040 | if (conn_xprt_read0_pending(h2c->conn)) |
| 1041 | flags |= CS_FL_EOS; |
| 1042 | |
| 1043 | node = eb32_lookup_ge(&h2c->streams_by_id, last + 1); |
| 1044 | while (node) { |
| 1045 | h2s = container_of(node, struct h2s, by_id); |
| 1046 | if (h2s->id <= last) |
| 1047 | break; |
| 1048 | node = eb32_next(node); |
Willy Tarreau | 22cf59b | 2017-11-10 11:42:33 +0100 | [diff] [blame] | 1049 | |
| 1050 | if (!h2s->cs) { |
| 1051 | /* this stream was already orphaned */ |
Willy Tarreau | 71049cc | 2018-03-28 13:56:39 +0200 | [diff] [blame] | 1052 | h2s_destroy(h2s); |
Willy Tarreau | 22cf59b | 2017-11-10 11:42:33 +0100 | [diff] [blame] | 1053 | continue; |
Willy Tarreau | 23b92aa | 2017-10-30 00:26:54 +0100 | [diff] [blame] | 1054 | } |
Willy Tarreau | 22cf59b | 2017-11-10 11:42:33 +0100 | [diff] [blame] | 1055 | |
| 1056 | h2s->cs->flags |= flags; |
| 1057 | /* recv is used to force to detect CS_FL_EOS that wake() |
| 1058 | * doesn't handle in the stream int code. |
| 1059 | */ |
| 1060 | h2s->cs->data_cb->recv(h2s->cs); |
| 1061 | h2s->cs->data_cb->wake(h2s->cs); |
| 1062 | |
Willy Tarreau | 23b92aa | 2017-10-30 00:26:54 +0100 | [diff] [blame] | 1063 | if (flags & CS_FL_ERROR && h2s->st < H2_SS_ERROR) |
| 1064 | h2s->st = H2_SS_ERROR; |
| 1065 | else if (flags & CS_FL_EOS && h2s->st == H2_SS_OPEN) |
| 1066 | h2s->st = H2_SS_HREM; |
| 1067 | else if (flags & CS_FL_EOS && h2s->st == H2_SS_HLOC) |
Willy Tarreau | 00dd078 | 2018-03-01 16:31:34 +0100 | [diff] [blame] | 1068 | h2s_close(h2s); |
Willy Tarreau | 23b92aa | 2017-10-30 00:26:54 +0100 | [diff] [blame] | 1069 | } |
| 1070 | } |
| 1071 | |
Willy Tarreau | 3421aba | 2017-07-27 15:41:03 +0200 | [diff] [blame] | 1072 | /* Increase all streams' outgoing window size by the difference passed in |
| 1073 | * argument. This is needed upon receipt of the settings frame if the initial |
| 1074 | * window size is different. The difference may be negative and the resulting |
| 1075 | * window size as well, for the time it takes to receive some window updates. |
| 1076 | */ |
| 1077 | static void h2c_update_all_ws(struct h2c *h2c, int diff) |
| 1078 | { |
| 1079 | struct h2s *h2s; |
| 1080 | struct eb32_node *node; |
| 1081 | |
| 1082 | if (!diff) |
| 1083 | return; |
| 1084 | |
| 1085 | node = eb32_first(&h2c->streams_by_id); |
| 1086 | while (node) { |
| 1087 | h2s = container_of(node, struct h2s, by_id); |
| 1088 | h2s->mws += diff; |
| 1089 | node = eb32_next(node); |
| 1090 | } |
| 1091 | } |
| 1092 | |
| 1093 | /* processes a SETTINGS frame whose payload is <payload> for <plen> bytes, and |
| 1094 | * ACKs it if needed. Returns > 0 on success or zero on missing data. It may |
| 1095 | * return an error in h2c. Described in RFC7540#6.5. |
| 1096 | */ |
| 1097 | static int h2c_handle_settings(struct h2c *h2c) |
| 1098 | { |
| 1099 | unsigned int offset; |
| 1100 | int error; |
| 1101 | |
| 1102 | if (h2c->dff & H2_F_SETTINGS_ACK) { |
| 1103 | if (h2c->dfl) { |
| 1104 | error = H2_ERR_FRAME_SIZE_ERROR; |
| 1105 | goto fail; |
| 1106 | } |
| 1107 | return 1; |
| 1108 | } |
| 1109 | |
| 1110 | if (h2c->dsi != 0) { |
| 1111 | error = H2_ERR_PROTOCOL_ERROR; |
| 1112 | goto fail; |
| 1113 | } |
| 1114 | |
| 1115 | if (h2c->dfl % 6) { |
| 1116 | error = H2_ERR_FRAME_SIZE_ERROR; |
| 1117 | goto fail; |
| 1118 | } |
| 1119 | |
| 1120 | /* that's the limit we can process */ |
| 1121 | if (h2c->dfl > global.tune.bufsize) { |
| 1122 | error = H2_ERR_FRAME_SIZE_ERROR; |
| 1123 | goto fail; |
| 1124 | } |
| 1125 | |
| 1126 | /* process full frame only */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 1127 | if (b_data(&h2c->dbuf) < h2c->dfl) |
Willy Tarreau | 3421aba | 2017-07-27 15:41:03 +0200 | [diff] [blame] | 1128 | return 0; |
| 1129 | |
| 1130 | /* parse the frame */ |
| 1131 | for (offset = 0; offset < h2c->dfl; offset += 6) { |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 1132 | uint16_t type = h2_get_n16(&h2c->dbuf, offset); |
| 1133 | int32_t arg = h2_get_n32(&h2c->dbuf, offset + 2); |
Willy Tarreau | 3421aba | 2017-07-27 15:41:03 +0200 | [diff] [blame] | 1134 | |
| 1135 | switch (type) { |
| 1136 | case H2_SETTINGS_INITIAL_WINDOW_SIZE: |
| 1137 | /* we need to update all existing streams with the |
| 1138 | * difference from the previous iws. |
| 1139 | */ |
| 1140 | if (arg < 0) { // RFC7540#6.5.2 |
| 1141 | error = H2_ERR_FLOW_CONTROL_ERROR; |
| 1142 | goto fail; |
| 1143 | } |
| 1144 | h2c_update_all_ws(h2c, arg - h2c->miw); |
| 1145 | h2c->miw = arg; |
| 1146 | break; |
| 1147 | case H2_SETTINGS_MAX_FRAME_SIZE: |
| 1148 | if (arg < 16384 || arg > 16777215) { // RFC7540#6.5.2 |
| 1149 | error = H2_ERR_PROTOCOL_ERROR; |
| 1150 | goto fail; |
| 1151 | } |
| 1152 | h2c->mfs = arg; |
| 1153 | break; |
Willy Tarreau | 1b38b46 | 2017-12-03 19:02:28 +0100 | [diff] [blame] | 1154 | case H2_SETTINGS_ENABLE_PUSH: |
| 1155 | if (arg < 0 || arg > 1) { // RFC7540#6.5.2 |
| 1156 | error = H2_ERR_PROTOCOL_ERROR; |
| 1157 | goto fail; |
| 1158 | } |
| 1159 | break; |
Willy Tarreau | 3421aba | 2017-07-27 15:41:03 +0200 | [diff] [blame] | 1160 | } |
| 1161 | } |
| 1162 | |
| 1163 | /* need to ACK this frame now */ |
| 1164 | h2c->st0 = H2_CS_FRAME_A; |
| 1165 | return 1; |
| 1166 | fail: |
| 1167 | h2c_error(h2c, error); |
| 1168 | return 0; |
| 1169 | } |
| 1170 | |
| 1171 | /* try to send an ACK for a settings frame on the connection. Returns > 0 on |
| 1172 | * success or one of the h2_status values. |
| 1173 | */ |
| 1174 | static int h2c_ack_settings(struct h2c *h2c) |
| 1175 | { |
| 1176 | struct buffer *res; |
| 1177 | char str[9]; |
| 1178 | int ret = -1; |
| 1179 | |
| 1180 | if (h2c_mux_busy(h2c, NULL)) { |
| 1181 | h2c->flags |= H2_CF_DEM_MBUSY; |
| 1182 | return 0; |
| 1183 | } |
| 1184 | |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 1185 | res = h2_get_buf(h2c, &h2c->mbuf); |
Willy Tarreau | 3421aba | 2017-07-27 15:41:03 +0200 | [diff] [blame] | 1186 | if (!res) { |
| 1187 | h2c->flags |= H2_CF_MUX_MALLOC; |
| 1188 | h2c->flags |= H2_CF_DEM_MROOM; |
| 1189 | return 0; |
| 1190 | } |
| 1191 | |
| 1192 | memcpy(str, |
| 1193 | "\x00\x00\x00" /* length : 0 (no data) */ |
| 1194 | "\x04" "\x01" /* type : 4, flags : ACK */ |
| 1195 | "\x00\x00\x00\x00" /* stream ID */, 9); |
| 1196 | |
Willy Tarreau | ea1b06d | 2018-07-12 09:02:47 +0200 | [diff] [blame] | 1197 | ret = b_istput(res, ist2(str, 9)); |
Willy Tarreau | 3421aba | 2017-07-27 15:41:03 +0200 | [diff] [blame] | 1198 | if (unlikely(ret <= 0)) { |
| 1199 | if (!ret) { |
| 1200 | h2c->flags |= H2_CF_MUX_MFULL; |
| 1201 | h2c->flags |= H2_CF_DEM_MROOM; |
| 1202 | return 0; |
| 1203 | } |
| 1204 | else { |
| 1205 | h2c_error(h2c, H2_ERR_INTERNAL_ERROR); |
| 1206 | return 0; |
| 1207 | } |
| 1208 | } |
| 1209 | return ret; |
| 1210 | } |
| 1211 | |
Willy Tarreau | cf68c78 | 2017-10-10 17:11:41 +0200 | [diff] [blame] | 1212 | /* processes a PING frame and schedules an ACK if needed. The caller must pass |
| 1213 | * the pointer to the payload in <payload>. Returns > 0 on success or zero on |
| 1214 | * missing data. It may return an error in h2c. |
| 1215 | */ |
| 1216 | static int h2c_handle_ping(struct h2c *h2c) |
| 1217 | { |
| 1218 | /* frame length must be exactly 8 */ |
| 1219 | if (h2c->dfl != 8) { |
| 1220 | h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR); |
| 1221 | return 0; |
| 1222 | } |
| 1223 | |
| 1224 | /* schedule a response */ |
Willy Tarreau | 68ed641 | 2017-12-03 18:15:56 +0100 | [diff] [blame] | 1225 | if (!(h2c->dff & H2_F_PING_ACK)) |
Willy Tarreau | cf68c78 | 2017-10-10 17:11:41 +0200 | [diff] [blame] | 1226 | h2c->st0 = H2_CS_FRAME_A; |
| 1227 | return 1; |
| 1228 | } |
| 1229 | |
Willy Tarreau | cc0b8c3 | 2017-10-26 16:55:59 +0200 | [diff] [blame] | 1230 | /* Try to send a window update for stream id <sid> and value <increment>. |
| 1231 | * Returns > 0 on success or zero on missing room or failure. It may return an |
| 1232 | * error in h2c. |
| 1233 | */ |
| 1234 | static int h2c_send_window_update(struct h2c *h2c, int sid, uint32_t increment) |
| 1235 | { |
| 1236 | struct buffer *res; |
| 1237 | char str[13]; |
| 1238 | int ret = -1; |
| 1239 | |
| 1240 | if (h2c_mux_busy(h2c, NULL)) { |
| 1241 | h2c->flags |= H2_CF_DEM_MBUSY; |
| 1242 | return 0; |
| 1243 | } |
| 1244 | |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 1245 | res = h2_get_buf(h2c, &h2c->mbuf); |
Willy Tarreau | cc0b8c3 | 2017-10-26 16:55:59 +0200 | [diff] [blame] | 1246 | if (!res) { |
| 1247 | h2c->flags |= H2_CF_MUX_MALLOC; |
| 1248 | h2c->flags |= H2_CF_DEM_MROOM; |
| 1249 | return 0; |
| 1250 | } |
| 1251 | |
| 1252 | /* length: 4, type: 8, flags: none */ |
| 1253 | memcpy(str, "\x00\x00\x04\x08\x00", 5); |
| 1254 | write_n32(str + 5, sid); |
| 1255 | write_n32(str + 9, increment); |
| 1256 | |
Willy Tarreau | ea1b06d | 2018-07-12 09:02:47 +0200 | [diff] [blame] | 1257 | ret = b_istput(res, ist2(str, 13)); |
Willy Tarreau | cc0b8c3 | 2017-10-26 16:55:59 +0200 | [diff] [blame] | 1258 | |
| 1259 | if (unlikely(ret <= 0)) { |
| 1260 | if (!ret) { |
| 1261 | h2c->flags |= H2_CF_MUX_MFULL; |
| 1262 | h2c->flags |= H2_CF_DEM_MROOM; |
| 1263 | return 0; |
| 1264 | } |
| 1265 | else { |
| 1266 | h2c_error(h2c, H2_ERR_INTERNAL_ERROR); |
| 1267 | return 0; |
| 1268 | } |
| 1269 | } |
| 1270 | return ret; |
| 1271 | } |
| 1272 | |
| 1273 | /* try to send pending window update for the connection. It's safe to call it |
| 1274 | * with no pending updates. Returns > 0 on success or zero on missing room or |
| 1275 | * failure. It may return an error in h2c. |
| 1276 | */ |
| 1277 | static int h2c_send_conn_wu(struct h2c *h2c) |
| 1278 | { |
| 1279 | int ret = 1; |
| 1280 | |
| 1281 | if (h2c->rcvd_c <= 0) |
| 1282 | return 1; |
| 1283 | |
| 1284 | /* send WU for the connection */ |
| 1285 | ret = h2c_send_window_update(h2c, 0, h2c->rcvd_c); |
| 1286 | if (ret > 0) |
| 1287 | h2c->rcvd_c = 0; |
| 1288 | |
| 1289 | return ret; |
| 1290 | } |
| 1291 | |
| 1292 | /* try to send pending window update for the current dmux stream. It's safe to |
| 1293 | * call it with no pending updates. Returns > 0 on success or zero on missing |
| 1294 | * room or failure. It may return an error in h2c. |
| 1295 | */ |
| 1296 | static int h2c_send_strm_wu(struct h2c *h2c) |
| 1297 | { |
| 1298 | int ret = 1; |
| 1299 | |
| 1300 | if (h2c->rcvd_s <= 0) |
| 1301 | return 1; |
| 1302 | |
| 1303 | /* send WU for the stream */ |
| 1304 | ret = h2c_send_window_update(h2c, h2c->dsi, h2c->rcvd_s); |
| 1305 | if (ret > 0) |
| 1306 | h2c->rcvd_s = 0; |
| 1307 | |
| 1308 | return ret; |
| 1309 | } |
| 1310 | |
Willy Tarreau | cf68c78 | 2017-10-10 17:11:41 +0200 | [diff] [blame] | 1311 | /* try to send an ACK for a ping frame on the connection. Returns > 0 on |
| 1312 | * success, 0 on missing data or one of the h2_status values. |
| 1313 | */ |
| 1314 | static int h2c_ack_ping(struct h2c *h2c) |
| 1315 | { |
| 1316 | struct buffer *res; |
| 1317 | char str[17]; |
| 1318 | int ret = -1; |
| 1319 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 1320 | if (b_data(&h2c->dbuf) < 8) |
Willy Tarreau | cf68c78 | 2017-10-10 17:11:41 +0200 | [diff] [blame] | 1321 | return 0; |
| 1322 | |
| 1323 | if (h2c_mux_busy(h2c, NULL)) { |
| 1324 | h2c->flags |= H2_CF_DEM_MBUSY; |
| 1325 | return 0; |
| 1326 | } |
| 1327 | |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 1328 | res = h2_get_buf(h2c, &h2c->mbuf); |
Willy Tarreau | cf68c78 | 2017-10-10 17:11:41 +0200 | [diff] [blame] | 1329 | if (!res) { |
| 1330 | h2c->flags |= H2_CF_MUX_MALLOC; |
| 1331 | h2c->flags |= H2_CF_DEM_MROOM; |
| 1332 | return 0; |
| 1333 | } |
| 1334 | |
| 1335 | memcpy(str, |
| 1336 | "\x00\x00\x08" /* length : 8 (same payload) */ |
| 1337 | "\x06" "\x01" /* type : 6, flags : ACK */ |
| 1338 | "\x00\x00\x00\x00" /* stream ID */, 9); |
| 1339 | |
| 1340 | /* copy the original payload */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 1341 | h2_get_buf_bytes(str + 9, 8, &h2c->dbuf, 0); |
Willy Tarreau | cf68c78 | 2017-10-10 17:11:41 +0200 | [diff] [blame] | 1342 | |
Willy Tarreau | ea1b06d | 2018-07-12 09:02:47 +0200 | [diff] [blame] | 1343 | ret = b_istput(res, ist2(str, 17)); |
Willy Tarreau | cf68c78 | 2017-10-10 17:11:41 +0200 | [diff] [blame] | 1344 | if (unlikely(ret <= 0)) { |
| 1345 | if (!ret) { |
| 1346 | h2c->flags |= H2_CF_MUX_MFULL; |
| 1347 | h2c->flags |= H2_CF_DEM_MROOM; |
| 1348 | return 0; |
| 1349 | } |
| 1350 | else { |
| 1351 | h2c_error(h2c, H2_ERR_INTERNAL_ERROR); |
| 1352 | return 0; |
| 1353 | } |
| 1354 | } |
| 1355 | return ret; |
| 1356 | } |
| 1357 | |
Willy Tarreau | 26f9595 | 2017-07-27 17:18:30 +0200 | [diff] [blame] | 1358 | /* processes a WINDOW_UPDATE frame whose payload is <payload> for <plen> bytes. |
| 1359 | * Returns > 0 on success or zero on missing data. It may return an error in |
| 1360 | * h2c or h2s. Described in RFC7540#6.9. |
| 1361 | */ |
| 1362 | static int h2c_handle_window_update(struct h2c *h2c, struct h2s *h2s) |
| 1363 | { |
| 1364 | int32_t inc; |
| 1365 | int error; |
| 1366 | |
| 1367 | if (h2c->dfl != 4) { |
| 1368 | error = H2_ERR_FRAME_SIZE_ERROR; |
| 1369 | goto conn_err; |
| 1370 | } |
| 1371 | |
| 1372 | /* process full frame only */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 1373 | if (b_data(&h2c->dbuf) < h2c->dfl) |
Willy Tarreau | 26f9595 | 2017-07-27 17:18:30 +0200 | [diff] [blame] | 1374 | return 0; |
| 1375 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 1376 | inc = h2_get_n32(&h2c->dbuf, 0); |
Willy Tarreau | 26f9595 | 2017-07-27 17:18:30 +0200 | [diff] [blame] | 1377 | |
| 1378 | if (h2c->dsi != 0) { |
| 1379 | /* stream window update */ |
Willy Tarreau | 26f9595 | 2017-07-27 17:18:30 +0200 | [diff] [blame] | 1380 | |
| 1381 | /* it's not an error to receive WU on a closed stream */ |
| 1382 | if (h2s->st == H2_SS_CLOSED) |
| 1383 | return 1; |
| 1384 | |
| 1385 | if (!inc) { |
| 1386 | error = H2_ERR_PROTOCOL_ERROR; |
| 1387 | goto strm_err; |
| 1388 | } |
| 1389 | |
| 1390 | if (h2s->mws >= 0 && h2s->mws + inc < 0) { |
| 1391 | error = H2_ERR_FLOW_CONTROL_ERROR; |
| 1392 | goto strm_err; |
| 1393 | } |
| 1394 | |
| 1395 | h2s->mws += inc; |
| 1396 | if (h2s->mws > 0 && (h2s->flags & H2_SF_BLK_SFCTL)) { |
| 1397 | h2s->flags &= ~H2_SF_BLK_SFCTL; |
| 1398 | if (h2s->cs && LIST_ISEMPTY(&h2s->list) && |
| 1399 | (h2s->cs->flags & CS_FL_DATA_WR_ENA)) { |
| 1400 | /* This stream wanted to send but could not due to its |
| 1401 | * own flow control. We can put it back into the send |
| 1402 | * list now, it will be handled upon next send() call. |
| 1403 | */ |
| 1404 | LIST_ADDQ(&h2c->send_list, &h2s->list); |
| 1405 | } |
| 1406 | } |
| 1407 | } |
| 1408 | else { |
| 1409 | /* connection window update */ |
| 1410 | if (!inc) { |
| 1411 | error = H2_ERR_PROTOCOL_ERROR; |
| 1412 | goto conn_err; |
| 1413 | } |
| 1414 | |
| 1415 | if (h2c->mws >= 0 && h2c->mws + inc < 0) { |
| 1416 | error = H2_ERR_FLOW_CONTROL_ERROR; |
| 1417 | goto conn_err; |
| 1418 | } |
| 1419 | |
| 1420 | h2c->mws += inc; |
| 1421 | } |
| 1422 | |
| 1423 | return 1; |
| 1424 | |
| 1425 | conn_err: |
| 1426 | h2c_error(h2c, error); |
| 1427 | return 0; |
| 1428 | |
| 1429 | strm_err: |
| 1430 | if (h2s) { |
| 1431 | h2s_error(h2s, error); |
Willy Tarreau | a20a519 | 2017-12-27 11:02:06 +0100 | [diff] [blame] | 1432 | h2c->st0 = H2_CS_FRAME_E; |
Willy Tarreau | 26f9595 | 2017-07-27 17:18:30 +0200 | [diff] [blame] | 1433 | } |
| 1434 | else |
| 1435 | h2c_error(h2c, error); |
| 1436 | return 0; |
| 1437 | } |
| 1438 | |
Willy Tarreau | e96b092 | 2017-10-30 00:28:29 +0100 | [diff] [blame] | 1439 | /* processes a GOAWAY frame, and signals all streams whose ID is greater than |
| 1440 | * the last ID. Returns > 0 on success or zero on missing data. It may return |
| 1441 | * an error in h2c. Described in RFC7540#6.8. |
| 1442 | */ |
| 1443 | static int h2c_handle_goaway(struct h2c *h2c) |
| 1444 | { |
| 1445 | int error; |
| 1446 | int last; |
| 1447 | |
| 1448 | if (h2c->dsi != 0) { |
| 1449 | error = H2_ERR_PROTOCOL_ERROR; |
| 1450 | goto conn_err; |
| 1451 | } |
| 1452 | |
| 1453 | if (h2c->dfl < 8) { |
| 1454 | error = H2_ERR_FRAME_SIZE_ERROR; |
| 1455 | goto conn_err; |
| 1456 | } |
| 1457 | |
| 1458 | /* process full frame only */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 1459 | if (b_data(&h2c->dbuf) < h2c->dfl) |
Willy Tarreau | e96b092 | 2017-10-30 00:28:29 +0100 | [diff] [blame] | 1460 | return 0; |
| 1461 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 1462 | last = h2_get_n32(&h2c->dbuf, 0); |
| 1463 | h2c->errcode = h2_get_n32(&h2c->dbuf, 4); |
Willy Tarreau | e96b092 | 2017-10-30 00:28:29 +0100 | [diff] [blame] | 1464 | h2_wake_some_streams(h2c, last, CS_FL_ERROR); |
Willy Tarreau | 11cc2d6 | 2017-12-03 10:27:47 +0100 | [diff] [blame] | 1465 | if (h2c->last_sid < 0) |
| 1466 | h2c->last_sid = last; |
Willy Tarreau | e96b092 | 2017-10-30 00:28:29 +0100 | [diff] [blame] | 1467 | return 1; |
| 1468 | |
| 1469 | conn_err: |
| 1470 | h2c_error(h2c, error); |
| 1471 | return 0; |
| 1472 | } |
| 1473 | |
Willy Tarreau | 92153fc | 2017-12-03 19:46:19 +0100 | [diff] [blame] | 1474 | /* processes a PRIORITY frame, and either skips it or rejects if it is |
| 1475 | * invalid. Returns > 0 on success or zero on missing data. It may return |
| 1476 | * an error in h2c. Described in RFC7540#6.3. |
| 1477 | */ |
| 1478 | static int h2c_handle_priority(struct h2c *h2c) |
| 1479 | { |
| 1480 | int error; |
| 1481 | |
| 1482 | if (h2c->dsi == 0) { |
| 1483 | error = H2_ERR_PROTOCOL_ERROR; |
| 1484 | goto conn_err; |
| 1485 | } |
| 1486 | |
| 1487 | if (h2c->dfl != 5) { |
| 1488 | error = H2_ERR_FRAME_SIZE_ERROR; |
| 1489 | goto conn_err; |
| 1490 | } |
| 1491 | |
| 1492 | /* process full frame only */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 1493 | if (b_data(&h2c->dbuf) < h2c->dfl) |
Willy Tarreau | 92153fc | 2017-12-03 19:46:19 +0100 | [diff] [blame] | 1494 | return 0; |
| 1495 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 1496 | if (h2_get_n32(&h2c->dbuf, 0) == h2c->dsi) { |
Willy Tarreau | 92153fc | 2017-12-03 19:46:19 +0100 | [diff] [blame] | 1497 | /* 7540#5.3 : can't depend on itself */ |
| 1498 | error = H2_ERR_PROTOCOL_ERROR; |
| 1499 | goto conn_err; |
| 1500 | } |
| 1501 | return 1; |
| 1502 | |
| 1503 | conn_err: |
| 1504 | h2c_error(h2c, error); |
| 1505 | return 0; |
| 1506 | } |
| 1507 | |
Willy Tarreau | cd234e9 | 2017-08-18 10:59:39 +0200 | [diff] [blame] | 1508 | /* processes an RST_STREAM frame, and sets the 32-bit error code on the stream. |
| 1509 | * Returns > 0 on success or zero on missing data. It may return an error in |
| 1510 | * h2c. Described in RFC7540#6.4. |
| 1511 | */ |
| 1512 | static int h2c_handle_rst_stream(struct h2c *h2c, struct h2s *h2s) |
| 1513 | { |
| 1514 | int error; |
| 1515 | |
| 1516 | if (h2c->dsi == 0) { |
| 1517 | error = H2_ERR_PROTOCOL_ERROR; |
| 1518 | goto conn_err; |
| 1519 | } |
| 1520 | |
Willy Tarreau | cd234e9 | 2017-08-18 10:59:39 +0200 | [diff] [blame] | 1521 | if (h2c->dfl != 4) { |
| 1522 | error = H2_ERR_FRAME_SIZE_ERROR; |
| 1523 | goto conn_err; |
| 1524 | } |
| 1525 | |
| 1526 | /* process full frame only */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 1527 | if (b_data(&h2c->dbuf) < h2c->dfl) |
Willy Tarreau | cd234e9 | 2017-08-18 10:59:39 +0200 | [diff] [blame] | 1528 | return 0; |
| 1529 | |
| 1530 | /* late RST, already handled */ |
| 1531 | if (h2s->st == H2_SS_CLOSED) |
| 1532 | return 1; |
| 1533 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 1534 | h2s->errcode = h2_get_n32(&h2c->dbuf, 0); |
Willy Tarreau | 00dd078 | 2018-03-01 16:31:34 +0100 | [diff] [blame] | 1535 | h2s_close(h2s); |
Willy Tarreau | cd234e9 | 2017-08-18 10:59:39 +0200 | [diff] [blame] | 1536 | |
| 1537 | if (h2s->cs) { |
Willy Tarreau | 2153d3c | 2017-12-15 11:56:29 +0100 | [diff] [blame] | 1538 | h2s->cs->flags |= CS_FL_EOS | CS_FL_ERROR; |
Willy Tarreau | cd234e9 | 2017-08-18 10:59:39 +0200 | [diff] [blame] | 1539 | /* recv is used to force to detect CS_FL_EOS that wake() |
| 1540 | * doesn't handle in the stream-int code. |
| 1541 | */ |
| 1542 | h2s->cs->data_cb->recv(h2s->cs); |
| 1543 | h2s->cs->data_cb->wake(h2s->cs); |
| 1544 | } |
| 1545 | |
| 1546 | h2s->flags |= H2_SF_RST_RCVD; |
| 1547 | return 1; |
| 1548 | |
| 1549 | conn_err: |
| 1550 | h2c_error(h2c, error); |
| 1551 | return 0; |
| 1552 | } |
| 1553 | |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 1554 | /* processes a HEADERS frame. Returns > 0 on success or zero on missing data. |
| 1555 | * It may return an error in h2c or h2s. Described in RFC7540#6.2. Most of the |
| 1556 | * errors here are reported as connection errors since it's impossible to |
| 1557 | * recover from such errors after the compression context has been altered. |
| 1558 | */ |
| 1559 | static int h2c_frt_handle_headers(struct h2c *h2c, struct h2s *h2s) |
| 1560 | { |
| 1561 | int error; |
| 1562 | |
| 1563 | if (!h2c->dfl) { |
| 1564 | error = H2_ERR_PROTOCOL_ERROR; // empty headers frame! |
| 1565 | goto strm_err; |
| 1566 | } |
| 1567 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 1568 | if (!b_size(&h2c->dbuf)) |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 1569 | return 0; // empty buffer |
| 1570 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 1571 | if (b_data(&h2c->dbuf) < h2c->dfl && !b_full(&h2c->dbuf)) |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 1572 | return 0; // incomplete frame |
| 1573 | |
Willy Tarreau | f210191 | 2018-07-19 10:11:38 +0200 | [diff] [blame] | 1574 | if (h2c->flags & H2_CF_DEM_TOOMANY) |
| 1575 | return 0; // too many cs still present |
| 1576 | |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 1577 | /* now either the frame is complete or the buffer is complete */ |
| 1578 | if (h2s->st != H2_SS_IDLE) { |
| 1579 | /* FIXME: stream already exists, this is only allowed for |
| 1580 | * trailers (not supported for now). |
| 1581 | */ |
| 1582 | error = H2_ERR_PROTOCOL_ERROR; |
| 1583 | goto conn_err; |
| 1584 | } |
| 1585 | else if (h2c->dsi <= h2c->max_id || !(h2c->dsi & 1)) { |
| 1586 | /* RFC7540#5.1.1 stream id > prev ones, and must be odd here */ |
| 1587 | error = H2_ERR_PROTOCOL_ERROR; |
| 1588 | goto conn_err; |
| 1589 | } |
| 1590 | |
| 1591 | h2s = h2c_stream_new(h2c, h2c->dsi); |
| 1592 | if (!h2s) { |
| 1593 | error = H2_ERR_INTERNAL_ERROR; |
| 1594 | goto conn_err; |
| 1595 | } |
| 1596 | |
| 1597 | h2s->st = H2_SS_OPEN; |
| 1598 | if (h2c->dff & H2_F_HEADERS_END_STREAM) { |
| 1599 | h2s->st = H2_SS_HREM; |
| 1600 | h2s->flags |= H2_SF_ES_RCVD; |
| 1601 | } |
| 1602 | |
| 1603 | /* call the upper layers to process the frame, then let the upper layer |
| 1604 | * notify the stream about any change. |
| 1605 | */ |
| 1606 | h2s->cs->data_cb->recv(h2s->cs); |
| 1607 | |
| 1608 | if (h2s->cs->data_cb->wake(h2s->cs) < 0) { |
| 1609 | /* FIXME: cs has already been destroyed, but we have to kill h2s. */ |
| 1610 | error = H2_ERR_INTERNAL_ERROR; |
| 1611 | goto conn_err; |
| 1612 | } |
| 1613 | |
Willy Tarreau | 8f650c3 | 2017-11-21 19:36:21 +0100 | [diff] [blame] | 1614 | if (h2c->st0 >= H2_CS_ERROR) |
| 1615 | return 0; |
| 1616 | |
Willy Tarreau | 721c974 | 2017-11-07 11:05:42 +0100 | [diff] [blame] | 1617 | if (h2s->st >= H2_SS_ERROR) { |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 1618 | /* stream error : send RST_STREAM */ |
Willy Tarreau | a20a519 | 2017-12-27 11:02:06 +0100 | [diff] [blame] | 1619 | h2c->st0 = H2_CS_FRAME_E; |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 1620 | } |
| 1621 | else { |
| 1622 | /* update the max stream ID if the request is being processed */ |
| 1623 | if (h2s->id > h2c->max_id) |
| 1624 | h2c->max_id = h2s->id; |
| 1625 | } |
| 1626 | |
| 1627 | return 1; |
| 1628 | |
| 1629 | conn_err: |
| 1630 | h2c_error(h2c, error); |
| 1631 | return 0; |
| 1632 | |
| 1633 | strm_err: |
| 1634 | if (h2s) { |
| 1635 | h2s_error(h2s, error); |
Willy Tarreau | a20a519 | 2017-12-27 11:02:06 +0100 | [diff] [blame] | 1636 | h2c->st0 = H2_CS_FRAME_E; |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 1637 | } |
| 1638 | else |
| 1639 | h2c_error(h2c, error); |
| 1640 | return 0; |
| 1641 | } |
| 1642 | |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 1643 | /* processes a DATA frame. Returns > 0 on success or zero on missing data. |
| 1644 | * It may return an error in h2c or h2s. Described in RFC7540#6.1. |
| 1645 | */ |
| 1646 | static int h2c_frt_handle_data(struct h2c *h2c, struct h2s *h2s) |
| 1647 | { |
| 1648 | int error; |
| 1649 | |
| 1650 | /* note that empty DATA frames are perfectly valid and sometimes used |
| 1651 | * to signal an end of stream (with the ES flag). |
| 1652 | */ |
| 1653 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 1654 | if (!b_size(&h2c->dbuf) && h2c->dfl) |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 1655 | return 0; // empty buffer |
| 1656 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 1657 | if (b_data(&h2c->dbuf) < h2c->dfl && !b_full(&h2c->dbuf)) |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 1658 | return 0; // incomplete frame |
| 1659 | |
| 1660 | /* now either the frame is complete or the buffer is complete */ |
| 1661 | |
| 1662 | if (!h2c->dsi) { |
| 1663 | /* RFC7540#6.1 */ |
| 1664 | error = H2_ERR_PROTOCOL_ERROR; |
| 1665 | goto conn_err; |
| 1666 | } |
| 1667 | |
| 1668 | if (h2s->st != H2_SS_OPEN && h2s->st != H2_SS_HLOC) { |
| 1669 | /* RFC7540#6.1 */ |
| 1670 | error = H2_ERR_STREAM_CLOSED; |
| 1671 | goto strm_err; |
| 1672 | } |
| 1673 | |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 1674 | /* call the upper layers to process the frame, then let the upper layer |
| 1675 | * notify the stream about any change. |
| 1676 | */ |
| 1677 | if (!h2s->cs) { |
| 1678 | error = H2_ERR_STREAM_CLOSED; |
| 1679 | goto strm_err; |
| 1680 | } |
| 1681 | |
| 1682 | h2s->cs->data_cb->recv(h2s->cs); |
Willy Tarreau | 8f650c3 | 2017-11-21 19:36:21 +0100 | [diff] [blame] | 1683 | |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 1684 | if (h2s->cs->data_cb->wake(h2s->cs) < 0) { |
| 1685 | /* cs has just been destroyed, we have to kill h2s. */ |
| 1686 | error = H2_ERR_STREAM_CLOSED; |
| 1687 | goto strm_err; |
| 1688 | } |
| 1689 | |
Willy Tarreau | 8f650c3 | 2017-11-21 19:36:21 +0100 | [diff] [blame] | 1690 | if (h2c->st0 >= H2_CS_ERROR) |
| 1691 | return 0; |
| 1692 | |
Willy Tarreau | 721c974 | 2017-11-07 11:05:42 +0100 | [diff] [blame] | 1693 | if (h2s->st >= H2_SS_ERROR) { |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 1694 | /* stream error : send RST_STREAM */ |
Willy Tarreau | a20a519 | 2017-12-27 11:02:06 +0100 | [diff] [blame] | 1695 | h2c->st0 = H2_CS_FRAME_E; |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 1696 | } |
| 1697 | |
| 1698 | /* check for completion : the callee will change this to FRAME_A or |
| 1699 | * FRAME_H once done. |
| 1700 | */ |
| 1701 | if (h2c->st0 == H2_CS_FRAME_P) |
| 1702 | return 0; |
| 1703 | |
Willy Tarreau | c4134ba | 2017-12-11 18:45:08 +0100 | [diff] [blame] | 1704 | |
| 1705 | /* last frame */ |
| 1706 | if (h2c->dff & H2_F_DATA_END_STREAM) { |
| 1707 | h2s->st = H2_SS_HREM; |
| 1708 | h2s->flags |= H2_SF_ES_RCVD; |
| 1709 | } |
| 1710 | |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 1711 | return 1; |
| 1712 | |
| 1713 | conn_err: |
| 1714 | h2c_error(h2c, error); |
| 1715 | return 0; |
| 1716 | |
| 1717 | strm_err: |
| 1718 | if (h2s) { |
| 1719 | h2s_error(h2s, error); |
Willy Tarreau | a20a519 | 2017-12-27 11:02:06 +0100 | [diff] [blame] | 1720 | h2c->st0 = H2_CS_FRAME_E; |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 1721 | } |
| 1722 | else |
| 1723 | h2c_error(h2c, error); |
| 1724 | return 0; |
| 1725 | } |
| 1726 | |
Willy Tarreau | bc93393 | 2017-10-09 16:21:43 +0200 | [diff] [blame] | 1727 | /* process Rx frames to be demultiplexed */ |
| 1728 | static void h2_process_demux(struct h2c *h2c) |
| 1729 | { |
Willy Tarreau | f3ee069 | 2017-10-17 08:18:25 +0200 | [diff] [blame] | 1730 | struct h2s *h2s; |
| 1731 | |
Willy Tarreau | 081d472 | 2017-05-16 21:51:05 +0200 | [diff] [blame] | 1732 | if (h2c->st0 >= H2_CS_ERROR) |
| 1733 | return; |
Willy Tarreau | 52eed75 | 2017-09-22 15:05:09 +0200 | [diff] [blame] | 1734 | |
| 1735 | if (unlikely(h2c->st0 < H2_CS_FRAME_H)) { |
| 1736 | if (h2c->st0 == H2_CS_PREFACE) { |
| 1737 | if (unlikely(h2c_frt_recv_preface(h2c) <= 0)) { |
| 1738 | /* RFC7540#3.5: a GOAWAY frame MAY be omitted */ |
| 1739 | if (h2c->st0 == H2_CS_ERROR) |
| 1740 | h2c->st0 = H2_CS_ERROR2; |
| 1741 | goto fail; |
| 1742 | } |
| 1743 | |
| 1744 | h2c->max_id = 0; |
| 1745 | h2c->st0 = H2_CS_SETTINGS1; |
| 1746 | } |
Willy Tarreau | 4c3690b | 2017-10-10 15:16:55 +0200 | [diff] [blame] | 1747 | |
| 1748 | if (h2c->st0 == H2_CS_SETTINGS1) { |
| 1749 | struct h2_fh hdr; |
| 1750 | |
| 1751 | /* ensure that what is pending is a valid SETTINGS frame |
| 1752 | * without an ACK. |
| 1753 | */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 1754 | if (!h2_get_frame_hdr(&h2c->dbuf, &hdr)) { |
Willy Tarreau | 4c3690b | 2017-10-10 15:16:55 +0200 | [diff] [blame] | 1755 | /* RFC7540#3.5: a GOAWAY frame MAY be omitted */ |
| 1756 | if (h2c->st0 == H2_CS_ERROR) |
| 1757 | h2c->st0 = H2_CS_ERROR2; |
| 1758 | goto fail; |
| 1759 | } |
| 1760 | |
| 1761 | if (hdr.sid || hdr.ft != H2_FT_SETTINGS || hdr.ff & H2_F_SETTINGS_ACK) { |
| 1762 | /* RFC7540#3.5: a GOAWAY frame MAY be omitted */ |
| 1763 | h2c_error(h2c, H2_ERR_PROTOCOL_ERROR); |
| 1764 | h2c->st0 = H2_CS_ERROR2; |
| 1765 | goto fail; |
| 1766 | } |
| 1767 | |
Willy Tarreau | 3f0e1ec | 2018-04-17 10:28:27 +0200 | [diff] [blame] | 1768 | if ((int)hdr.len < 0 || (int)hdr.len > global.tune.bufsize) { |
Willy Tarreau | 4c3690b | 2017-10-10 15:16:55 +0200 | [diff] [blame] | 1769 | /* RFC7540#3.5: a GOAWAY frame MAY be omitted */ |
| 1770 | h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR); |
| 1771 | h2c->st0 = H2_CS_ERROR2; |
| 1772 | goto fail; |
| 1773 | } |
| 1774 | |
| 1775 | /* that's OK, switch to FRAME_P to process it */ |
| 1776 | h2c->dfl = hdr.len; |
| 1777 | h2c->dsi = hdr.sid; |
| 1778 | h2c->dft = hdr.ft; |
| 1779 | h2c->dff = hdr.ff; |
Willy Tarreau | 05e5daf | 2017-12-11 15:17:36 +0100 | [diff] [blame] | 1780 | h2c->dpl = 0; |
Willy Tarreau | 4c3690b | 2017-10-10 15:16:55 +0200 | [diff] [blame] | 1781 | h2c->st0 = H2_CS_FRAME_P; |
| 1782 | } |
Willy Tarreau | 52eed75 | 2017-09-22 15:05:09 +0200 | [diff] [blame] | 1783 | } |
Willy Tarreau | 7e98c05 | 2017-10-10 15:56:59 +0200 | [diff] [blame] | 1784 | |
| 1785 | /* process as many incoming frames as possible below */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 1786 | while (b_data(&h2c->dbuf)) { |
Willy Tarreau | 7e98c05 | 2017-10-10 15:56:59 +0200 | [diff] [blame] | 1787 | int ret = 0; |
| 1788 | |
| 1789 | if (h2c->st0 >= H2_CS_ERROR) |
| 1790 | break; |
| 1791 | |
| 1792 | if (h2c->st0 == H2_CS_FRAME_H) { |
| 1793 | struct h2_fh hdr; |
| 1794 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 1795 | if (!h2_peek_frame_hdr(&h2c->dbuf, &hdr)) |
Willy Tarreau | 7e98c05 | 2017-10-10 15:56:59 +0200 | [diff] [blame] | 1796 | break; |
| 1797 | |
Willy Tarreau | 3f0e1ec | 2018-04-17 10:28:27 +0200 | [diff] [blame] | 1798 | if ((int)hdr.len < 0 || (int)hdr.len > global.tune.bufsize) { |
Willy Tarreau | 7e98c05 | 2017-10-10 15:56:59 +0200 | [diff] [blame] | 1799 | h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR); |
| 1800 | h2c->st0 = H2_CS_ERROR; |
| 1801 | break; |
| 1802 | } |
| 1803 | |
| 1804 | h2c->dfl = hdr.len; |
| 1805 | h2c->dsi = hdr.sid; |
| 1806 | h2c->dft = hdr.ft; |
| 1807 | h2c->dff = hdr.ff; |
Willy Tarreau | 05e5daf | 2017-12-11 15:17:36 +0100 | [diff] [blame] | 1808 | h2c->dpl = 0; |
Willy Tarreau | 7e98c05 | 2017-10-10 15:56:59 +0200 | [diff] [blame] | 1809 | h2c->st0 = H2_CS_FRAME_P; |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 1810 | h2_skip_frame_hdr(&h2c->dbuf); |
Willy Tarreau | 7e98c05 | 2017-10-10 15:56:59 +0200 | [diff] [blame] | 1811 | } |
| 1812 | |
| 1813 | /* Only H2_CS_FRAME_P and H2_CS_FRAME_A here */ |
Willy Tarreau | f3ee069 | 2017-10-17 08:18:25 +0200 | [diff] [blame] | 1814 | h2s = h2c_st_by_id(h2c, h2c->dsi); |
Willy Tarreau | 7e98c05 | 2017-10-10 15:56:59 +0200 | [diff] [blame] | 1815 | |
Willy Tarreau | d790143 | 2017-12-29 11:34:40 +0100 | [diff] [blame] | 1816 | if (h2c->st0 == H2_CS_FRAME_E) |
| 1817 | goto strm_err; |
| 1818 | |
Willy Tarreau | f65b80d | 2017-10-30 11:46:49 +0100 | [diff] [blame] | 1819 | if (h2s->st == H2_SS_IDLE && |
| 1820 | h2c->dft != H2_FT_HEADERS && h2c->dft != H2_FT_PRIORITY) { |
| 1821 | /* RFC7540#5.1: any frame other than HEADERS or PRIORITY in |
| 1822 | * this state MUST be treated as a connection error |
| 1823 | */ |
| 1824 | h2c_error(h2c, H2_ERR_PROTOCOL_ERROR); |
| 1825 | h2c->st0 = H2_CS_ERROR; |
| 1826 | break; |
| 1827 | } |
| 1828 | |
Willy Tarreau | f182a9a | 2017-10-30 12:03:50 +0100 | [diff] [blame] | 1829 | if (h2s->st == H2_SS_HREM && h2c->dft != H2_FT_WINDOW_UPDATE && |
| 1830 | h2c->dft != H2_FT_RST_STREAM && h2c->dft != H2_FT_PRIORITY) { |
| 1831 | /* RFC7540#5.1: any frame other than WU/PRIO/RST in |
| 1832 | * this state MUST be treated as a stream error |
| 1833 | */ |
| 1834 | h2s_error(h2s, H2_ERR_STREAM_CLOSED); |
Willy Tarreau | a20a519 | 2017-12-27 11:02:06 +0100 | [diff] [blame] | 1835 | h2c->st0 = H2_CS_FRAME_E; |
Willy Tarreau | f182a9a | 2017-10-30 12:03:50 +0100 | [diff] [blame] | 1836 | goto strm_err; |
| 1837 | } |
| 1838 | |
Willy Tarreau | ab83750 | 2017-12-27 15:07:30 +0100 | [diff] [blame] | 1839 | /* Below the management of frames received in closed state is a |
| 1840 | * bit hackish because the spec makes strong differences between |
| 1841 | * streams closed by receiving RST, sending RST, and seeing ES |
| 1842 | * in both directions. In addition to this, the creation of a |
| 1843 | * new stream reusing the identifier of a closed one will be |
| 1844 | * detected here. Given that we cannot keep track of all closed |
| 1845 | * streams forever, we consider that unknown closed streams were |
| 1846 | * closed on RST received, which allows us to respond with an |
| 1847 | * RST without breaking the connection (eg: to abort a transfer). |
| 1848 | * Some frames have to be silently ignored as well. |
| 1849 | */ |
| 1850 | if (h2s->st == H2_SS_CLOSED && h2c->dsi) { |
| 1851 | if (h2c->dft == H2_FT_HEADERS || h2c->dft == H2_FT_PUSH_PROMISE) { |
| 1852 | /* #5.1.1: The identifier of a newly |
| 1853 | * established stream MUST be numerically |
| 1854 | * greater than all streams that the initiating |
| 1855 | * endpoint has opened or reserved. This |
| 1856 | * governs streams that are opened using a |
| 1857 | * HEADERS frame and streams that are reserved |
| 1858 | * using PUSH_PROMISE. An endpoint that |
| 1859 | * receives an unexpected stream identifier |
| 1860 | * MUST respond with a connection error. |
| 1861 | */ |
| 1862 | h2c_error(h2c, H2_ERR_STREAM_CLOSED); |
| 1863 | goto strm_err; |
| 1864 | } |
| 1865 | |
| 1866 | if (h2s->flags & H2_SF_RST_RCVD) { |
| 1867 | /* RFC7540#5.1:closed: an endpoint that |
| 1868 | * receives any frame other than PRIORITY after |
| 1869 | * receiving a RST_STREAM MUST treat that as a |
| 1870 | * stream error of type STREAM_CLOSED. |
| 1871 | * |
| 1872 | * Note that old streams fall into this category |
| 1873 | * and will lead to an RST being sent. |
| 1874 | */ |
| 1875 | h2s_error(h2s, H2_ERR_STREAM_CLOSED); |
| 1876 | h2c->st0 = H2_CS_FRAME_E; |
| 1877 | goto strm_err; |
| 1878 | } |
| 1879 | |
| 1880 | /* RFC7540#5.1:closed: if this state is reached as a |
| 1881 | * result of sending a RST_STREAM frame, the peer that |
| 1882 | * receives the RST_STREAM might have already sent |
| 1883 | * frames on the stream that cannot be withdrawn. An |
| 1884 | * endpoint MUST ignore frames that it receives on |
| 1885 | * closed streams after it has sent a RST_STREAM |
| 1886 | * frame. An endpoint MAY choose to limit the period |
| 1887 | * over which it ignores frames and treat frames that |
| 1888 | * arrive after this time as being in error. |
| 1889 | */ |
| 1890 | if (!(h2s->flags & H2_SF_RST_SENT)) { |
| 1891 | /* RFC7540#5.1:closed: any frame other than |
| 1892 | * PRIO/WU/RST in this state MUST be treated as |
| 1893 | * a connection error |
| 1894 | */ |
| 1895 | if (h2c->dft != H2_FT_RST_STREAM && |
| 1896 | h2c->dft != H2_FT_PRIORITY && |
| 1897 | h2c->dft != H2_FT_WINDOW_UPDATE) { |
| 1898 | h2c_error(h2c, H2_ERR_STREAM_CLOSED); |
| 1899 | goto strm_err; |
| 1900 | } |
| 1901 | } |
| 1902 | } |
| 1903 | |
Willy Tarreau | c0da196 | 2017-10-30 18:38:00 +0100 | [diff] [blame] | 1904 | #if 0 |
| 1905 | // problem below: it is not possible to completely ignore such |
| 1906 | // streams as we need to maintain the compression state as well |
| 1907 | // and for this we need to completely process these frames (eg: |
| 1908 | // HEADERS frames) as well as counting DATA frames to emit |
| 1909 | // proper WINDOW UPDATES and ensure the connection doesn't stall. |
| 1910 | // This is a typical case of layer violation where the |
| 1911 | // transported contents are critical to the connection's |
| 1912 | // validity and must be ignored at the same time :-( |
| 1913 | |
| 1914 | /* graceful shutdown, ignore streams whose ID is higher than |
| 1915 | * the one advertised in GOAWAY. RFC7540#6.8. |
| 1916 | */ |
| 1917 | if (unlikely(h2c->last_sid >= 0) && h2c->dsi > h2c->last_sid) { |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 1918 | ret = MIN(b_data(&h2c->dbuf), h2c->dfl); |
| 1919 | b_del(&h2c->dbuf, ret); |
Willy Tarreau | c0da196 | 2017-10-30 18:38:00 +0100 | [diff] [blame] | 1920 | h2c->dfl -= ret; |
| 1921 | ret = h2c->dfl == 0; |
| 1922 | goto strm_err; |
| 1923 | } |
| 1924 | #endif |
| 1925 | |
Willy Tarreau | 7e98c05 | 2017-10-10 15:56:59 +0200 | [diff] [blame] | 1926 | switch (h2c->dft) { |
Willy Tarreau | 3421aba | 2017-07-27 15:41:03 +0200 | [diff] [blame] | 1927 | case H2_FT_SETTINGS: |
| 1928 | if (h2c->st0 == H2_CS_FRAME_P) |
| 1929 | ret = h2c_handle_settings(h2c); |
| 1930 | |
| 1931 | if (h2c->st0 == H2_CS_FRAME_A) |
| 1932 | ret = h2c_ack_settings(h2c); |
| 1933 | break; |
| 1934 | |
Willy Tarreau | cf68c78 | 2017-10-10 17:11:41 +0200 | [diff] [blame] | 1935 | case H2_FT_PING: |
| 1936 | if (h2c->st0 == H2_CS_FRAME_P) |
| 1937 | ret = h2c_handle_ping(h2c); |
| 1938 | |
| 1939 | if (h2c->st0 == H2_CS_FRAME_A) |
| 1940 | ret = h2c_ack_ping(h2c); |
| 1941 | break; |
| 1942 | |
Willy Tarreau | 26f9595 | 2017-07-27 17:18:30 +0200 | [diff] [blame] | 1943 | case H2_FT_WINDOW_UPDATE: |
| 1944 | if (h2c->st0 == H2_CS_FRAME_P) |
| 1945 | ret = h2c_handle_window_update(h2c, h2s); |
| 1946 | break; |
| 1947 | |
Willy Tarreau | 61290ec | 2017-10-17 08:19:21 +0200 | [diff] [blame] | 1948 | case H2_FT_CONTINUATION: |
| 1949 | /* we currently don't support CONTINUATION frames since |
| 1950 | * we have nowhere to store the partial HEADERS frame. |
| 1951 | * Let's abort the stream on an INTERNAL_ERROR here. |
| 1952 | */ |
Willy Tarreau | a20a519 | 2017-12-27 11:02:06 +0100 | [diff] [blame] | 1953 | if (h2c->st0 == H2_CS_FRAME_P) { |
Willy Tarreau | 61290ec | 2017-10-17 08:19:21 +0200 | [diff] [blame] | 1954 | h2s_error(h2s, H2_ERR_INTERNAL_ERROR); |
Willy Tarreau | a20a519 | 2017-12-27 11:02:06 +0100 | [diff] [blame] | 1955 | h2c->st0 = H2_CS_FRAME_E; |
| 1956 | } |
Willy Tarreau | 61290ec | 2017-10-17 08:19:21 +0200 | [diff] [blame] | 1957 | break; |
| 1958 | |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 1959 | case H2_FT_HEADERS: |
| 1960 | if (h2c->st0 == H2_CS_FRAME_P) |
| 1961 | ret = h2c_frt_handle_headers(h2c, h2s); |
| 1962 | break; |
| 1963 | |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 1964 | case H2_FT_DATA: |
| 1965 | if (h2c->st0 == H2_CS_FRAME_P) |
| 1966 | ret = h2c_frt_handle_data(h2c, h2s); |
| 1967 | |
| 1968 | if (h2c->st0 == H2_CS_FRAME_A) |
| 1969 | ret = h2c_send_strm_wu(h2c); |
| 1970 | break; |
Willy Tarreau | cd234e9 | 2017-08-18 10:59:39 +0200 | [diff] [blame] | 1971 | |
Willy Tarreau | 92153fc | 2017-12-03 19:46:19 +0100 | [diff] [blame] | 1972 | case H2_FT_PRIORITY: |
| 1973 | if (h2c->st0 == H2_CS_FRAME_P) |
| 1974 | ret = h2c_handle_priority(h2c); |
| 1975 | break; |
| 1976 | |
Willy Tarreau | cd234e9 | 2017-08-18 10:59:39 +0200 | [diff] [blame] | 1977 | case H2_FT_RST_STREAM: |
| 1978 | if (h2c->st0 == H2_CS_FRAME_P) |
| 1979 | ret = h2c_handle_rst_stream(h2c, h2s); |
| 1980 | break; |
| 1981 | |
Willy Tarreau | e96b092 | 2017-10-30 00:28:29 +0100 | [diff] [blame] | 1982 | case H2_FT_GOAWAY: |
| 1983 | if (h2c->st0 == H2_CS_FRAME_P) |
| 1984 | ret = h2c_handle_goaway(h2c); |
| 1985 | break; |
| 1986 | |
Willy Tarreau | 1c66198 | 2017-10-30 13:52:01 +0100 | [diff] [blame] | 1987 | case H2_FT_PUSH_PROMISE: |
| 1988 | /* not permitted here, RFC7540#5.1 */ |
| 1989 | h2c_error(h2c, H2_ERR_PROTOCOL_ERROR); |
Willy Tarreau | 1c66198 | 2017-10-30 13:52:01 +0100 | [diff] [blame] | 1990 | break; |
| 1991 | |
| 1992 | /* implement all extra frame types here */ |
Willy Tarreau | 7e98c05 | 2017-10-10 15:56:59 +0200 | [diff] [blame] | 1993 | default: |
| 1994 | /* drop frames that we ignore. They may be larger than |
| 1995 | * the buffer so we drain all of their contents until |
| 1996 | * we reach the end. |
| 1997 | */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 1998 | ret = MIN(b_data(&h2c->dbuf), h2c->dfl); |
| 1999 | b_del(&h2c->dbuf, ret); |
Willy Tarreau | 7e98c05 | 2017-10-10 15:56:59 +0200 | [diff] [blame] | 2000 | h2c->dfl -= ret; |
| 2001 | ret = h2c->dfl == 0; |
| 2002 | } |
| 2003 | |
Willy Tarreau | f182a9a | 2017-10-30 12:03:50 +0100 | [diff] [blame] | 2004 | strm_err: |
Willy Tarreau | a20a519 | 2017-12-27 11:02:06 +0100 | [diff] [blame] | 2005 | /* We may have to send an RST if not done yet */ |
| 2006 | if (h2s->st == H2_SS_ERROR) |
| 2007 | h2c->st0 = H2_CS_FRAME_E; |
Willy Tarreau | 27a84c9 | 2017-10-17 08:10:17 +0200 | [diff] [blame] | 2008 | |
Willy Tarreau | a20a519 | 2017-12-27 11:02:06 +0100 | [diff] [blame] | 2009 | if (h2c->st0 == H2_CS_FRAME_E) |
| 2010 | ret = h2c_send_rst_stream(h2c, h2s); |
Willy Tarreau | 27a84c9 | 2017-10-17 08:10:17 +0200 | [diff] [blame] | 2011 | |
Willy Tarreau | 7e98c05 | 2017-10-10 15:56:59 +0200 | [diff] [blame] | 2012 | /* error or missing data condition met above ? */ |
| 2013 | if (ret <= 0) |
| 2014 | break; |
| 2015 | |
| 2016 | if (h2c->st0 != H2_CS_FRAME_H) { |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2017 | b_del(&h2c->dbuf, h2c->dfl); |
Willy Tarreau | 7e98c05 | 2017-10-10 15:56:59 +0200 | [diff] [blame] | 2018 | h2c->st0 = H2_CS_FRAME_H; |
| 2019 | } |
| 2020 | } |
Willy Tarreau | 52eed75 | 2017-09-22 15:05:09 +0200 | [diff] [blame] | 2021 | |
Willy Tarreau | cc0b8c3 | 2017-10-26 16:55:59 +0200 | [diff] [blame] | 2022 | if (h2c->rcvd_c > 0 && |
| 2023 | !(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_DEM_MBUSY | H2_CF_DEM_MROOM))) |
| 2024 | h2c_send_conn_wu(h2c); |
| 2025 | |
Willy Tarreau | 52eed75 | 2017-09-22 15:05:09 +0200 | [diff] [blame] | 2026 | fail: |
| 2027 | /* we can go here on missing data, blocked response or error */ |
| 2028 | return; |
Willy Tarreau | bc93393 | 2017-10-09 16:21:43 +0200 | [diff] [blame] | 2029 | } |
| 2030 | |
| 2031 | /* process Tx frames from streams to be multiplexed. Returns > 0 if it reached |
| 2032 | * the end. |
| 2033 | */ |
| 2034 | static int h2_process_mux(struct h2c *h2c) |
| 2035 | { |
Willy Tarreau | bacdf5a | 2017-10-17 10:57:04 +0200 | [diff] [blame] | 2036 | struct h2s *h2s, *h2s_back; |
| 2037 | |
Willy Tarreau | cc0b8c3 | 2017-10-26 16:55:59 +0200 | [diff] [blame] | 2038 | /* start by sending possibly pending window updates */ |
| 2039 | if (h2c->rcvd_c > 0 && |
| 2040 | !(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_MUX_MALLOC)) && |
| 2041 | h2c_send_conn_wu(h2c) < 0) |
| 2042 | goto fail; |
| 2043 | |
Willy Tarreau | bacdf5a | 2017-10-17 10:57:04 +0200 | [diff] [blame] | 2044 | /* First we always process the flow control list because the streams |
| 2045 | * waiting there were already elected for immediate emission but were |
| 2046 | * blocked just on this. |
| 2047 | */ |
| 2048 | |
| 2049 | list_for_each_entry_safe(h2s, h2s_back, &h2c->fctl_list, list) { |
| 2050 | if (h2c->mws <= 0 || h2c->flags & H2_CF_MUX_BLOCK_ANY || |
| 2051 | h2c->st0 >= H2_CS_ERROR) |
| 2052 | break; |
| 2053 | |
| 2054 | /* In theory it's possible that h2s->cs == NULL here : |
| 2055 | * - client sends crap that causes a parse error |
| 2056 | * - RST_STREAM is produced and CS_FL_ERROR at the same time |
| 2057 | * - RST_STREAM cannot be emitted because mux is busy/full |
| 2058 | * - stream gets notified, detaches and quits |
| 2059 | * - mux buffer gets ready and wakes pending streams up |
| 2060 | * - bam! |
| 2061 | */ |
| 2062 | h2s->flags &= ~H2_SF_BLK_ANY; |
| 2063 | |
| 2064 | if (h2s->cs) { |
Willy Tarreau | bacdf5a | 2017-10-17 10:57:04 +0200 | [diff] [blame] | 2065 | h2s->cs->data_cb->wake(h2s->cs); |
Willy Tarreau | 27a84c9 | 2017-10-17 08:10:17 +0200 | [diff] [blame] | 2066 | } else { |
Willy Tarreau | 8c0ea7d | 2017-11-10 10:05:24 +0100 | [diff] [blame] | 2067 | h2s_send_rst_stream(h2c, h2s); |
Willy Tarreau | bacdf5a | 2017-10-17 10:57:04 +0200 | [diff] [blame] | 2068 | } |
| 2069 | |
| 2070 | /* depending on callee's blocking reasons, we may queue in send |
| 2071 | * list or completely dequeue. |
| 2072 | */ |
| 2073 | if ((h2s->flags & H2_SF_BLK_MFCTL) == 0) { |
| 2074 | if (h2s->flags & H2_SF_BLK_ANY) { |
| 2075 | LIST_DEL(&h2s->list); |
| 2076 | LIST_ADDQ(&h2c->send_list, &h2s->list); |
| 2077 | } |
| 2078 | else { |
| 2079 | LIST_DEL(&h2s->list); |
| 2080 | LIST_INIT(&h2s->list); |
| 2081 | if (h2s->cs) |
| 2082 | h2s->cs->flags &= ~CS_FL_DATA_WR_ENA; |
Willy Tarreau | 22cf59b | 2017-11-10 11:42:33 +0100 | [diff] [blame] | 2083 | else { |
| 2084 | /* just sent the last frame for this orphaned stream */ |
Willy Tarreau | 71049cc | 2018-03-28 13:56:39 +0200 | [diff] [blame] | 2085 | h2s_destroy(h2s); |
Willy Tarreau | 22cf59b | 2017-11-10 11:42:33 +0100 | [diff] [blame] | 2086 | } |
Willy Tarreau | bacdf5a | 2017-10-17 10:57:04 +0200 | [diff] [blame] | 2087 | } |
| 2088 | } |
| 2089 | } |
| 2090 | |
| 2091 | list_for_each_entry_safe(h2s, h2s_back, &h2c->send_list, list) { |
| 2092 | if (h2c->st0 >= H2_CS_ERROR || h2c->flags & H2_CF_MUX_BLOCK_ANY) |
| 2093 | break; |
| 2094 | |
| 2095 | /* In theory it's possible that h2s->cs == NULL here : |
| 2096 | * - client sends crap that causes a parse error |
| 2097 | * - RST_STREAM is produced and CS_FL_ERROR at the same time |
| 2098 | * - RST_STREAM cannot be emitted because mux is busy/full |
| 2099 | * - stream gets notified, detaches and quits |
| 2100 | * - mux buffer gets ready and wakes pending streams up |
| 2101 | * - bam! |
| 2102 | */ |
| 2103 | h2s->flags &= ~H2_SF_BLK_ANY; |
| 2104 | |
| 2105 | if (h2s->cs) { |
Willy Tarreau | bacdf5a | 2017-10-17 10:57:04 +0200 | [diff] [blame] | 2106 | h2s->cs->data_cb->wake(h2s->cs); |
Willy Tarreau | 27a84c9 | 2017-10-17 08:10:17 +0200 | [diff] [blame] | 2107 | } else { |
Willy Tarreau | 8c0ea7d | 2017-11-10 10:05:24 +0100 | [diff] [blame] | 2108 | h2s_send_rst_stream(h2c, h2s); |
Willy Tarreau | bacdf5a | 2017-10-17 10:57:04 +0200 | [diff] [blame] | 2109 | } |
| 2110 | /* depending on callee's blocking reasons, we may queue in fctl |
| 2111 | * list or completely dequeue. |
| 2112 | */ |
| 2113 | if (h2s->flags & H2_SF_BLK_MFCTL) { |
| 2114 | /* stream hit the connection's flow control */ |
| 2115 | LIST_DEL(&h2s->list); |
| 2116 | LIST_ADDQ(&h2c->fctl_list, &h2s->list); |
| 2117 | } |
| 2118 | else if (!(h2s->flags & H2_SF_BLK_ANY)) { |
| 2119 | LIST_DEL(&h2s->list); |
| 2120 | LIST_INIT(&h2s->list); |
| 2121 | if (h2s->cs) |
| 2122 | h2s->cs->flags &= ~CS_FL_DATA_WR_ENA; |
Willy Tarreau | 22cf59b | 2017-11-10 11:42:33 +0100 | [diff] [blame] | 2123 | else { |
| 2124 | /* just sent the last frame for this orphaned stream */ |
Willy Tarreau | 71049cc | 2018-03-28 13:56:39 +0200 | [diff] [blame] | 2125 | h2s_destroy(h2s); |
Willy Tarreau | 22cf59b | 2017-11-10 11:42:33 +0100 | [diff] [blame] | 2126 | } |
Willy Tarreau | bacdf5a | 2017-10-17 10:57:04 +0200 | [diff] [blame] | 2127 | } |
| 2128 | } |
| 2129 | |
Willy Tarreau | cc0b8c3 | 2017-10-26 16:55:59 +0200 | [diff] [blame] | 2130 | fail: |
Willy Tarreau | 3eabe9b | 2017-11-07 11:03:01 +0100 | [diff] [blame] | 2131 | if (unlikely(h2c->st0 >= H2_CS_ERROR)) { |
Willy Tarreau | 081d472 | 2017-05-16 21:51:05 +0200 | [diff] [blame] | 2132 | if (h2c->st0 == H2_CS_ERROR) { |
| 2133 | if (h2c->max_id >= 0) { |
| 2134 | h2c_send_goaway_error(h2c, NULL); |
| 2135 | if (h2c->flags & H2_CF_MUX_BLOCK_ANY) |
| 2136 | return 0; |
| 2137 | } |
| 2138 | |
| 2139 | h2c->st0 = H2_CS_ERROR2; // sent (or failed hard) ! |
| 2140 | } |
| 2141 | return 1; |
| 2142 | } |
Willy Tarreau | bacdf5a | 2017-10-17 10:57:04 +0200 | [diff] [blame] | 2143 | return (h2c->mws <= 0 || LIST_ISEMPTY(&h2c->fctl_list)) && LIST_ISEMPTY(&h2c->send_list); |
Willy Tarreau | bc93393 | 2017-10-09 16:21:43 +0200 | [diff] [blame] | 2144 | } |
| 2145 | |
Willy Tarreau | 7168117 | 2017-10-23 14:39:06 +0200 | [diff] [blame] | 2146 | |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 2147 | /*********************************************************/ |
| 2148 | /* functions below are I/O callbacks from the connection */ |
| 2149 | /*********************************************************/ |
| 2150 | |
| 2151 | /* callback called on recv event by the connection handler */ |
| 2152 | static void h2_recv(struct connection *conn) |
| 2153 | { |
Willy Tarreau | a2af512 | 2017-10-09 11:56:46 +0200 | [diff] [blame] | 2154 | struct h2c *h2c = conn->mux_ctx; |
Willy Tarreau | 35dbd5d | 2017-09-22 09:13:49 +0200 | [diff] [blame] | 2155 | struct buffer *buf; |
Willy Tarreau | a2af512 | 2017-10-09 11:56:46 +0200 | [diff] [blame] | 2156 | int max; |
| 2157 | |
Willy Tarreau | 315d807 | 2017-12-10 22:17:57 +0100 | [diff] [blame] | 2158 | if (!h2_recv_allowed(h2c)) |
Willy Tarreau | fbe3b4f | 2017-10-09 15:14:19 +0200 | [diff] [blame] | 2159 | return; |
Willy Tarreau | a2af512 | 2017-10-09 11:56:46 +0200 | [diff] [blame] | 2160 | |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 2161 | buf = h2_get_buf(h2c, &h2c->dbuf); |
Willy Tarreau | 1b62c5c | 2017-09-25 11:55:01 +0200 | [diff] [blame] | 2162 | if (!buf) { |
| 2163 | h2c->flags |= H2_CF_DEM_DALLOC; |
Willy Tarreau | 35dbd5d | 2017-09-22 09:13:49 +0200 | [diff] [blame] | 2164 | return; |
Willy Tarreau | 1b62c5c | 2017-09-25 11:55:01 +0200 | [diff] [blame] | 2165 | } |
Willy Tarreau | 35dbd5d | 2017-09-22 09:13:49 +0200 | [diff] [blame] | 2166 | |
Willy Tarreau | b7b5fe1 | 2018-06-18 13:33:09 +0200 | [diff] [blame] | 2167 | max = buf->size - b_data(buf); |
Willy Tarreau | 315d807 | 2017-12-10 22:17:57 +0100 | [diff] [blame] | 2168 | if (max) |
Willy Tarreau | 7f3225f | 2018-06-19 06:15:17 +0200 | [diff] [blame] | 2169 | conn->xprt->rcv_buf(conn, buf, max, 0); |
Willy Tarreau | a2af512 | 2017-10-09 11:56:46 +0200 | [diff] [blame] | 2170 | |
Willy Tarreau | b7b5fe1 | 2018-06-18 13:33:09 +0200 | [diff] [blame] | 2171 | if (!b_data(buf)) { |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 2172 | h2_release_buf(h2c, &h2c->dbuf); |
Willy Tarreau | a2af512 | 2017-10-09 11:56:46 +0200 | [diff] [blame] | 2173 | return; |
| 2174 | } |
| 2175 | |
Willy Tarreau | b7b5fe1 | 2018-06-18 13:33:09 +0200 | [diff] [blame] | 2176 | if (b_data(buf) == buf->size) |
Willy Tarreau | fbe3b4f | 2017-10-09 15:14:19 +0200 | [diff] [blame] | 2177 | h2c->flags |= H2_CF_DEM_DFULL; |
Willy Tarreau | a2af512 | 2017-10-09 11:56:46 +0200 | [diff] [blame] | 2178 | return; |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 2179 | } |
| 2180 | |
Olivier Houchard | 910b2bc | 2018-07-17 18:49:38 +0200 | [diff] [blame] | 2181 | /* Try to send data if possible */ |
| 2182 | static struct task *h2_send(struct task *t, void *ctx, unsigned short state) |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 2183 | { |
Olivier Houchard | 910b2bc | 2018-07-17 18:49:38 +0200 | [diff] [blame] | 2184 | struct connection *conn = ctx; |
Willy Tarreau | a2af512 | 2017-10-09 11:56:46 +0200 | [diff] [blame] | 2185 | struct h2c *h2c = conn->mux_ctx; |
Willy Tarreau | bc93393 | 2017-10-09 16:21:43 +0200 | [diff] [blame] | 2186 | int done; |
Willy Tarreau | a2af512 | 2017-10-09 11:56:46 +0200 | [diff] [blame] | 2187 | |
| 2188 | if (conn->flags & CO_FL_ERROR) |
Olivier Houchard | 910b2bc | 2018-07-17 18:49:38 +0200 | [diff] [blame] | 2189 | return NULL; |
Willy Tarreau | a2af512 | 2017-10-09 11:56:46 +0200 | [diff] [blame] | 2190 | |
| 2191 | if (conn->flags & (CO_FL_HANDSHAKE|CO_FL_WAIT_L4_CONN|CO_FL_WAIT_L6_CONN)) { |
| 2192 | /* a handshake was requested */ |
Olivier Houchard | 910b2bc | 2018-07-17 18:49:38 +0200 | [diff] [blame] | 2193 | return NULL; |
Willy Tarreau | a2af512 | 2017-10-09 11:56:46 +0200 | [diff] [blame] | 2194 | } |
| 2195 | |
Willy Tarreau | bc93393 | 2017-10-09 16:21:43 +0200 | [diff] [blame] | 2196 | /* This loop is quite simple : it tries to fill as much as it can from |
| 2197 | * pending streams into the existing buffer until it's reportedly full |
| 2198 | * or the end of send requests is reached. Then it tries to send this |
| 2199 | * buffer's contents out, marks it not full if at least one byte could |
| 2200 | * be sent, and tries again. |
| 2201 | * |
| 2202 | * The snd_buf() function normally takes a "flags" argument which may |
| 2203 | * be made of a combination of CO_SFL_MSG_MORE to indicate that more |
| 2204 | * data immediately comes and CO_SFL_STREAMER to indicate that the |
| 2205 | * connection is streaming lots of data (used to increase TLS record |
| 2206 | * size at the expense of latency). The former can be sent any time |
| 2207 | * there's a buffer full flag, as it indicates at least one stream |
| 2208 | * attempted to send and failed so there are pending data. An |
| 2209 | * alternative would be to set it as long as there's an active stream |
| 2210 | * but that would be problematic for ACKs until we have an absolute |
| 2211 | * guarantee that all waiters have at least one byte to send. The |
| 2212 | * latter should possibly not be set for now. |
| 2213 | */ |
| 2214 | |
| 2215 | done = 0; |
| 2216 | while (!done) { |
| 2217 | unsigned int flags = 0; |
| 2218 | |
| 2219 | /* fill as much as we can into the current buffer */ |
| 2220 | while (((h2c->flags & (H2_CF_MUX_MFULL|H2_CF_MUX_MALLOC)) == 0) && !done) |
| 2221 | done = h2_process_mux(h2c); |
| 2222 | |
| 2223 | if (conn->flags & CO_FL_ERROR) |
| 2224 | break; |
| 2225 | |
| 2226 | if (h2c->flags & (H2_CF_MUX_MFULL | H2_CF_DEM_MBUSY | H2_CF_DEM_MROOM)) |
| 2227 | flags |= CO_SFL_MSG_MORE; |
| 2228 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2229 | if (b_data(&h2c->mbuf)) { |
| 2230 | int ret = conn->xprt->snd_buf(conn, &h2c->mbuf, b_data(&h2c->mbuf), flags); |
Willy Tarreau | 787db9a | 2018-06-14 18:31:46 +0200 | [diff] [blame] | 2231 | if (!ret) |
| 2232 | break; |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2233 | b_del(&h2c->mbuf, ret); |
| 2234 | b_realign_if_empty(&h2c->mbuf); |
Willy Tarreau | 787db9a | 2018-06-14 18:31:46 +0200 | [diff] [blame] | 2235 | } |
Willy Tarreau | bc93393 | 2017-10-09 16:21:43 +0200 | [diff] [blame] | 2236 | |
| 2237 | /* wrote at least one byte, the buffer is not full anymore */ |
| 2238 | h2c->flags &= ~(H2_CF_MUX_MFULL | H2_CF_DEM_MROOM); |
| 2239 | } |
| 2240 | |
Willy Tarreau | a2af512 | 2017-10-09 11:56:46 +0200 | [diff] [blame] | 2241 | if (conn->flags & CO_FL_SOCK_WR_SH) { |
| 2242 | /* output closed, nothing to send, clear the buffer to release it */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2243 | b_reset(&h2c->mbuf); |
Willy Tarreau | a2af512 | 2017-10-09 11:56:46 +0200 | [diff] [blame] | 2244 | } |
Olivier Houchard | 6ff2039 | 2018-07-17 18:46:31 +0200 | [diff] [blame] | 2245 | /* We're not full anymore, so we can wake any task that are waiting |
| 2246 | * for us. |
| 2247 | */ |
| 2248 | if (!(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_DEM_MROOM))) { |
| 2249 | while (!LIST_ISEMPTY(&h2c->send_wait_list)) { |
| 2250 | struct wait_list *sw = LIST_ELEM(h2c->send_wait_list.n, |
| 2251 | struct wait_list *, list); |
| 2252 | LIST_DEL(&sw->list); |
| 2253 | LIST_INIT(&sw->list); |
| 2254 | tasklet_wakeup(sw->task); |
| 2255 | } |
| 2256 | |
| 2257 | } |
Olivier Houchard | 910b2bc | 2018-07-17 18:49:38 +0200 | [diff] [blame] | 2258 | /* We're done, no more to send */ |
| 2259 | if (!b_data(&h2c->mbuf)) |
| 2260 | return NULL; |
| 2261 | schedule: |
| 2262 | if (LIST_ISEMPTY(&h2c->wait_list.list)) |
| 2263 | conn->xprt->subscribe(conn, SUB_CAN_SEND, &h2c->wait_list); |
| 2264 | return NULL; |
Willy Tarreau | fbe3b4f | 2017-10-09 15:14:19 +0200 | [diff] [blame] | 2265 | } |
Willy Tarreau | a2af512 | 2017-10-09 11:56:46 +0200 | [diff] [blame] | 2266 | |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 2267 | /* callback called on any event by the connection handler. |
| 2268 | * It applies changes and returns zero, or < 0 if it wants immediate |
| 2269 | * destruction of the connection (which normally doesn not happen in h2). |
| 2270 | */ |
| 2271 | static int h2_wake(struct connection *conn) |
| 2272 | { |
Willy Tarreau | a2af512 | 2017-10-09 11:56:46 +0200 | [diff] [blame] | 2273 | struct h2c *h2c = conn->mux_ctx; |
Willy Tarreau | 8ec1406 | 2017-12-30 18:08:13 +0100 | [diff] [blame] | 2274 | struct session *sess = conn->owner; |
Willy Tarreau | a2af512 | 2017-10-09 11:56:46 +0200 | [diff] [blame] | 2275 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2276 | if (b_data(&h2c->dbuf) && !(h2c->flags & H2_CF_DEM_BLOCK_ANY)) { |
Willy Tarreau | d13bf27 | 2017-12-14 10:34:52 +0100 | [diff] [blame] | 2277 | h2_process_demux(h2c); |
| 2278 | |
| 2279 | if (h2c->st0 >= H2_CS_ERROR || conn->flags & CO_FL_ERROR) |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2280 | b_reset(&h2c->dbuf); |
Willy Tarreau | d13bf27 | 2017-12-14 10:34:52 +0100 | [diff] [blame] | 2281 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2282 | if (!b_full(&h2c->dbuf)) |
Willy Tarreau | d13bf27 | 2017-12-14 10:34:52 +0100 | [diff] [blame] | 2283 | h2c->flags &= ~H2_CF_DEM_DFULL; |
| 2284 | } |
| 2285 | |
Willy Tarreau | 8ec1406 | 2017-12-30 18:08:13 +0100 | [diff] [blame] | 2286 | if (sess && unlikely(sess->fe->state == PR_STSTOPPED)) { |
| 2287 | /* frontend is stopping, reload likely in progress, let's try |
| 2288 | * to announce a graceful shutdown if not yet done. We don't |
| 2289 | * care if it fails, it will be tried again later. |
| 2290 | */ |
| 2291 | if (!(h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED))) { |
| 2292 | if (h2c->last_sid < 0) |
| 2293 | h2c->last_sid = (1U << 31) - 1; |
| 2294 | h2c_send_goaway_error(h2c, NULL); |
| 2295 | } |
| 2296 | } |
| 2297 | |
Olivier Houchard | 7fc96d5 | 2017-11-23 18:25:47 +0100 | [diff] [blame] | 2298 | /* |
Olivier Houchard | 6fa63d9 | 2017-11-27 18:41:32 +0100 | [diff] [blame] | 2299 | * If we received early data, and the handshake is done, wake |
| 2300 | * any stream that was waiting for it. |
Olivier Houchard | 7fc96d5 | 2017-11-23 18:25:47 +0100 | [diff] [blame] | 2301 | */ |
Olivier Houchard | 6fa63d9 | 2017-11-27 18:41:32 +0100 | [diff] [blame] | 2302 | if (!(h2c->flags & H2_CF_WAIT_FOR_HS) && |
| 2303 | (conn->flags & (CO_FL_EARLY_SSL_HS | CO_FL_HANDSHAKE | CO_FL_EARLY_DATA)) == CO_FL_EARLY_DATA) { |
| 2304 | struct eb32_node *node; |
| 2305 | struct h2s *h2s; |
| 2306 | |
| 2307 | h2c->flags |= H2_CF_WAIT_FOR_HS; |
| 2308 | node = eb32_lookup_ge(&h2c->streams_by_id, 1); |
| 2309 | |
| 2310 | while (node) { |
| 2311 | h2s = container_of(node, struct h2s, by_id); |
| 2312 | if (h2s->cs->flags & CS_FL_WAIT_FOR_HS) |
| 2313 | h2s->cs->data_cb->wake(h2s->cs); |
| 2314 | node = eb32_next(node); |
| 2315 | } |
Olivier Houchard | 7fc96d5 | 2017-11-23 18:25:47 +0100 | [diff] [blame] | 2316 | } |
Olivier Houchard | 6fa63d9 | 2017-11-27 18:41:32 +0100 | [diff] [blame] | 2317 | |
Willy Tarreau | 26bd761 | 2017-10-09 16:47:04 +0200 | [diff] [blame] | 2318 | if (conn->flags & CO_FL_ERROR || conn_xprt_read0_pending(conn) || |
Willy Tarreau | 29a9824 | 2017-10-31 06:59:15 +0100 | [diff] [blame] | 2319 | h2c->st0 == H2_CS_ERROR2 || h2c->flags & H2_CF_GOAWAY_FAILED || |
| 2320 | (eb_is_empty(&h2c->streams_by_id) && h2c->last_sid >= 0 && |
| 2321 | h2c->max_id >= h2c->last_sid)) { |
Willy Tarreau | 23b92aa | 2017-10-30 00:26:54 +0100 | [diff] [blame] | 2322 | h2_wake_some_streams(h2c, 0, 0); |
Willy Tarreau | fbe3b4f | 2017-10-09 15:14:19 +0200 | [diff] [blame] | 2323 | |
| 2324 | if (eb_is_empty(&h2c->streams_by_id)) { |
| 2325 | /* no more stream, kill the connection now */ |
| 2326 | h2_release(conn); |
| 2327 | return -1; |
| 2328 | } |
| 2329 | else { |
| 2330 | /* some streams still there, we need to signal them all and |
| 2331 | * wait for their departure. |
| 2332 | */ |
| 2333 | __conn_xprt_stop_recv(conn); |
| 2334 | __conn_xprt_stop_send(conn); |
| 2335 | return 0; |
| 2336 | } |
| 2337 | } |
| 2338 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2339 | if (!b_data(&h2c->dbuf)) |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 2340 | h2_release_buf(h2c, &h2c->dbuf); |
Willy Tarreau | fbe3b4f | 2017-10-09 15:14:19 +0200 | [diff] [blame] | 2341 | |
| 2342 | /* stop being notified of incoming data if we can't process them */ |
Willy Tarreau | 315d807 | 2017-12-10 22:17:57 +0100 | [diff] [blame] | 2343 | if (!h2_recv_allowed(h2c)) { |
Willy Tarreau | fbe3b4f | 2017-10-09 15:14:19 +0200 | [diff] [blame] | 2344 | __conn_xprt_stop_recv(conn); |
| 2345 | } |
| 2346 | else { |
Willy Tarreau | fbe3b4f | 2017-10-09 15:14:19 +0200 | [diff] [blame] | 2347 | __conn_xprt_want_recv(conn); |
| 2348 | } |
| 2349 | |
| 2350 | /* adjust output polling */ |
Willy Tarreau | 5160683 | 2017-10-17 15:30:07 +0200 | [diff] [blame] | 2351 | if (!(conn->flags & CO_FL_SOCK_WR_SH) && |
| 2352 | (h2c->st0 == H2_CS_ERROR || |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2353 | b_data(&h2c->mbuf) || |
Willy Tarreau | 5160683 | 2017-10-17 15:30:07 +0200 | [diff] [blame] | 2354 | (h2c->mws > 0 && !LIST_ISEMPTY(&h2c->fctl_list)) || |
| 2355 | (!(h2c->flags & H2_CF_MUX_BLOCK_ANY) && !LIST_ISEMPTY(&h2c->send_list)))) { |
Willy Tarreau | fbe3b4f | 2017-10-09 15:14:19 +0200 | [diff] [blame] | 2356 | __conn_xprt_want_send(conn); |
| 2357 | } |
| 2358 | else { |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 2359 | h2_release_buf(h2c, &h2c->mbuf); |
Willy Tarreau | fbe3b4f | 2017-10-09 15:14:19 +0200 | [diff] [blame] | 2360 | __conn_xprt_stop_send(conn); |
Willy Tarreau | a2af512 | 2017-10-09 11:56:46 +0200 | [diff] [blame] | 2361 | } |
| 2362 | |
Willy Tarreau | 3f13357 | 2017-10-31 19:21:06 +0100 | [diff] [blame] | 2363 | if (h2c->task) { |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2364 | if (eb_is_empty(&h2c->streams_by_id) || b_data(&h2c->mbuf)) { |
Willy Tarreau | 599391a | 2017-11-24 10:16:00 +0100 | [diff] [blame] | 2365 | h2c->task->expire = tick_add(now_ms, h2c->last_sid < 0 ? h2c->timeout : h2c->shut_timeout); |
Willy Tarreau | 3f13357 | 2017-10-31 19:21:06 +0100 | [diff] [blame] | 2366 | task_queue(h2c->task); |
| 2367 | } |
| 2368 | else |
| 2369 | h2c->task->expire = TICK_ETERNITY; |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 2370 | } |
Olivier Houchard | 910b2bc | 2018-07-17 18:49:38 +0200 | [diff] [blame] | 2371 | |
| 2372 | h2_send(NULL, conn, 0); |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 2373 | return 0; |
| 2374 | } |
| 2375 | |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 2376 | /* Connection timeout management. The principle is that if there's no receipt |
| 2377 | * nor sending for a certain amount of time, the connection is closed. If the |
| 2378 | * MUX buffer still has lying data or is not allocatable, the connection is |
| 2379 | * immediately killed. If it's allocatable and empty, we attempt to send a |
| 2380 | * GOAWAY frame. |
| 2381 | */ |
Olivier Houchard | 9f6af33 | 2018-05-25 14:04:04 +0200 | [diff] [blame] | 2382 | 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] | 2383 | { |
Olivier Houchard | 9f6af33 | 2018-05-25 14:04:04 +0200 | [diff] [blame] | 2384 | struct h2c *h2c = context; |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 2385 | int expired = tick_is_expired(t->expire, now_ms); |
| 2386 | |
Willy Tarreau | 0975f11 | 2018-03-29 15:22:59 +0200 | [diff] [blame] | 2387 | if (!expired && h2c) |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 2388 | return t; |
| 2389 | |
Willy Tarreau | 0975f11 | 2018-03-29 15:22:59 +0200 | [diff] [blame] | 2390 | task_delete(t); |
| 2391 | task_free(t); |
| 2392 | |
| 2393 | if (!h2c) { |
| 2394 | /* resources were already deleted */ |
| 2395 | return NULL; |
| 2396 | } |
| 2397 | |
| 2398 | h2c->task = NULL; |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 2399 | h2c_error(h2c, H2_ERR_NO_ERROR); |
| 2400 | h2_wake_some_streams(h2c, 0, 0); |
| 2401 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2402 | if (b_data(&h2c->mbuf)) { |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 2403 | /* don't even try to send a GOAWAY, the buffer is stuck */ |
| 2404 | h2c->flags |= H2_CF_GOAWAY_FAILED; |
| 2405 | } |
| 2406 | |
| 2407 | /* try to send but no need to insist */ |
Willy Tarreau | 599391a | 2017-11-24 10:16:00 +0100 | [diff] [blame] | 2408 | h2c->last_sid = h2c->max_id; |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 2409 | if (h2c_send_goaway_error(h2c, NULL) <= 0) |
| 2410 | h2c->flags |= H2_CF_GOAWAY_FAILED; |
| 2411 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2412 | if (b_data(&h2c->mbuf) && !(h2c->flags & H2_CF_GOAWAY_FAILED) && conn_xprt_ready(h2c->conn)) { |
| 2413 | int ret = h2c->conn->xprt->snd_buf(h2c->conn, &h2c->mbuf, b_data(&h2c->mbuf), 0); |
Willy Tarreau | 787db9a | 2018-06-14 18:31:46 +0200 | [diff] [blame] | 2414 | if (ret > 0) { |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2415 | b_del(&h2c->mbuf, ret); |
| 2416 | b_realign_if_empty(&h2c->mbuf); |
Willy Tarreau | 787db9a | 2018-06-14 18:31:46 +0200 | [diff] [blame] | 2417 | } |
| 2418 | } |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 2419 | |
Willy Tarreau | 0975f11 | 2018-03-29 15:22:59 +0200 | [diff] [blame] | 2420 | /* either we can release everything now or it will be done later once |
| 2421 | * the last stream closes. |
| 2422 | */ |
| 2423 | if (eb_is_empty(&h2c->streams_by_id)) |
| 2424 | h2_release(h2c->conn); |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 2425 | |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 2426 | return NULL; |
| 2427 | } |
| 2428 | |
| 2429 | |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 2430 | /*******************************************/ |
| 2431 | /* functions below are used by the streams */ |
| 2432 | /*******************************************/ |
| 2433 | |
| 2434 | /* |
| 2435 | * Attach a new stream to a connection |
| 2436 | * (Used for outgoing connections) |
| 2437 | */ |
| 2438 | static struct conn_stream *h2_attach(struct connection *conn) |
| 2439 | { |
| 2440 | return NULL; |
| 2441 | } |
| 2442 | |
| 2443 | /* callback used to update the mux's polling flags after changing a cs' status. |
| 2444 | * The caller (cs_update_mux_polling) will take care of propagating any changes |
| 2445 | * to the transport layer. |
| 2446 | */ |
| 2447 | static void h2_update_poll(struct conn_stream *cs) |
| 2448 | { |
Willy Tarreau | 1d39322 | 2017-10-17 10:26:19 +0200 | [diff] [blame] | 2449 | struct h2s *h2s = cs->ctx; |
| 2450 | |
| 2451 | if (!h2s) |
| 2452 | return; |
| 2453 | |
Willy Tarreau | d7739c8 | 2017-10-30 15:38:23 +0100 | [diff] [blame] | 2454 | /* we may unblock a blocked read */ |
| 2455 | |
Willy Tarreau | 315d807 | 2017-12-10 22:17:57 +0100 | [diff] [blame] | 2456 | if (cs->flags & CS_FL_DATA_RD_ENA) { |
| 2457 | /* the stream indicates it's willing to read */ |
Willy Tarreau | d7739c8 | 2017-10-30 15:38:23 +0100 | [diff] [blame] | 2458 | h2s->h2c->flags &= ~H2_CF_DEM_SFULL; |
Willy Tarreau | d13bf27 | 2017-12-14 10:34:52 +0100 | [diff] [blame] | 2459 | if (h2s->h2c->dsi == h2s->id) { |
Willy Tarreau | 315d807 | 2017-12-10 22:17:57 +0100 | [diff] [blame] | 2460 | conn_xprt_want_recv(cs->conn); |
Willy Tarreau | d13bf27 | 2017-12-14 10:34:52 +0100 | [diff] [blame] | 2461 | conn_xprt_want_send(cs->conn); |
| 2462 | } |
Willy Tarreau | d7739c8 | 2017-10-30 15:38:23 +0100 | [diff] [blame] | 2463 | } |
| 2464 | |
Willy Tarreau | 1d39322 | 2017-10-17 10:26:19 +0200 | [diff] [blame] | 2465 | /* Note: the stream and stream-int code doesn't allow us to perform a |
| 2466 | * synchronous send() here unfortunately, because this code is called |
| 2467 | * as si_update() from the process_stream() context. This means that |
| 2468 | * we have to queue the current cs and defer its processing after the |
| 2469 | * connection's cs list is processed anyway. |
| 2470 | */ |
| 2471 | |
| 2472 | if (cs->flags & CS_FL_DATA_WR_ENA) { |
| 2473 | if (LIST_ISEMPTY(&h2s->list)) { |
| 2474 | if (LIST_ISEMPTY(&h2s->h2c->send_list) && |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2475 | !b_data(&h2s->h2c->mbuf) && // not yet subscribed |
Willy Tarreau | 1d39322 | 2017-10-17 10:26:19 +0200 | [diff] [blame] | 2476 | !(cs->conn->flags & CO_FL_SOCK_WR_SH)) |
| 2477 | conn_xprt_want_send(cs->conn); |
| 2478 | LIST_ADDQ(&h2s->h2c->send_list, &h2s->list); |
| 2479 | } |
| 2480 | } |
| 2481 | else if (!LIST_ISEMPTY(&h2s->list)) { |
| 2482 | LIST_DEL(&h2s->list); |
| 2483 | LIST_INIT(&h2s->list); |
| 2484 | h2s->flags &= ~(H2_SF_BLK_MBUSY | H2_SF_BLK_MROOM | H2_SF_BLK_MFCTL); |
| 2485 | } |
| 2486 | |
| 2487 | /* this can happen from within si_chk_snd() */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2488 | if (b_data(&h2s->h2c->mbuf) && !(cs->conn->flags & CO_FL_XPRT_WR_ENA)) |
Willy Tarreau | 1d39322 | 2017-10-17 10:26:19 +0200 | [diff] [blame] | 2489 | conn_xprt_want_send(cs->conn); |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 2490 | } |
| 2491 | |
| 2492 | /* |
| 2493 | * Detach the stream from the connection and possibly release the connection. |
| 2494 | */ |
| 2495 | static void h2_detach(struct conn_stream *cs) |
| 2496 | { |
Willy Tarreau | 6093514 | 2017-10-16 18:11:19 +0200 | [diff] [blame] | 2497 | struct h2s *h2s = cs->ctx; |
| 2498 | struct h2c *h2c; |
| 2499 | |
| 2500 | cs->ctx = NULL; |
| 2501 | if (!h2s) |
| 2502 | return; |
| 2503 | |
| 2504 | h2c = h2s->h2c; |
| 2505 | h2s->cs = NULL; |
Willy Tarreau | 7ac60e8 | 2018-07-19 09:04:05 +0200 | [diff] [blame] | 2506 | h2c->nb_cs--; |
Willy Tarreau | f210191 | 2018-07-19 10:11:38 +0200 | [diff] [blame] | 2507 | if (h2c->flags & H2_CF_DEM_TOOMANY && |
| 2508 | !h2_has_too_many_cs(h2c)) { |
| 2509 | h2c->flags &= ~H2_CF_DEM_TOOMANY; |
| 2510 | if (h2_recv_allowed(h2c)) { |
| 2511 | __conn_xprt_want_recv(h2c->conn); |
| 2512 | conn_xprt_want_send(h2c->conn); |
| 2513 | } |
| 2514 | } |
Willy Tarreau | 6093514 | 2017-10-16 18:11:19 +0200 | [diff] [blame] | 2515 | |
Willy Tarreau | 22cf59b | 2017-11-10 11:42:33 +0100 | [diff] [blame] | 2516 | /* this stream may be blocked waiting for some data to leave (possibly |
| 2517 | * an ES or RST frame), so orphan it in this case. |
| 2518 | */ |
Willy Tarreau | 3041fcc | 2018-03-29 15:41:32 +0200 | [diff] [blame] | 2519 | if (!(cs->conn->flags & CO_FL_ERROR) && |
| 2520 | (h2s->flags & (H2_SF_BLK_MBUSY | H2_SF_BLK_MROOM | H2_SF_BLK_MFCTL))) |
Willy Tarreau | 22cf59b | 2017-11-10 11:42:33 +0100 | [diff] [blame] | 2521 | return; |
| 2522 | |
Willy Tarreau | 45f752e | 2017-10-30 15:44:59 +0100 | [diff] [blame] | 2523 | if ((h2c->flags & H2_CF_DEM_BLOCK_ANY && h2s->id == h2c->dsi) || |
| 2524 | (h2c->flags & H2_CF_MUX_BLOCK_ANY && h2s->id == h2c->msi)) { |
| 2525 | /* unblock the connection if it was blocked on this |
| 2526 | * stream. |
| 2527 | */ |
| 2528 | h2c->flags &= ~H2_CF_DEM_BLOCK_ANY; |
| 2529 | h2c->flags &= ~H2_CF_MUX_BLOCK_ANY; |
| 2530 | conn_xprt_want_recv(cs->conn); |
| 2531 | conn_xprt_want_send(cs->conn); |
| 2532 | } |
| 2533 | |
Willy Tarreau | 71049cc | 2018-03-28 13:56:39 +0200 | [diff] [blame] | 2534 | h2s_destroy(h2s); |
Willy Tarreau | 6093514 | 2017-10-16 18:11:19 +0200 | [diff] [blame] | 2535 | |
Willy Tarreau | e323f34 | 2018-03-28 13:51:45 +0200 | [diff] [blame] | 2536 | /* We don't want to close right now unless we're removing the |
| 2537 | * last stream, and either the connection is in error, or it |
| 2538 | * reached the ID already specified in a GOAWAY frame received |
| 2539 | * or sent (as seen by last_sid >= 0). |
| 2540 | */ |
| 2541 | if (eb_is_empty(&h2c->streams_by_id) && /* don't close if streams exist */ |
| 2542 | ((h2c->conn->flags & CO_FL_ERROR) || /* errors close immediately */ |
Willy Tarreau | 42d55b9 | 2018-06-13 14:24:56 +0200 | [diff] [blame] | 2543 | (h2c->st0 >= H2_CS_ERROR && !h2c->task) || /* a timeout stroke earlier */ |
Willy Tarreau | e323f34 | 2018-03-28 13:51:45 +0200 | [diff] [blame] | 2544 | (h2c->flags & H2_CF_GOAWAY_FAILED) || |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2545 | (!b_data(&h2c->mbuf) && /* mux buffer empty, also process clean events below */ |
Willy Tarreau | e323f34 | 2018-03-28 13:51:45 +0200 | [diff] [blame] | 2546 | (conn_xprt_read0_pending(h2c->conn) || |
| 2547 | (h2c->last_sid >= 0 && h2c->max_id >= h2c->last_sid))))) { |
| 2548 | /* no more stream will come, kill it now */ |
| 2549 | h2_release(h2c->conn); |
| 2550 | } |
| 2551 | else if (h2c->task) { |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2552 | if (eb_is_empty(&h2c->streams_by_id) || b_data(&h2c->mbuf)) { |
Willy Tarreau | e323f34 | 2018-03-28 13:51:45 +0200 | [diff] [blame] | 2553 | h2c->task->expire = tick_add(now_ms, h2c->last_sid < 0 ? h2c->timeout : h2c->shut_timeout); |
| 2554 | task_queue(h2c->task); |
Willy Tarreau | e6ae77f | 2017-11-07 11:59:51 +0100 | [diff] [blame] | 2555 | } |
Willy Tarreau | e323f34 | 2018-03-28 13:51:45 +0200 | [diff] [blame] | 2556 | else |
| 2557 | h2c->task->expire = TICK_ETERNITY; |
Willy Tarreau | 6093514 | 2017-10-16 18:11:19 +0200 | [diff] [blame] | 2558 | } |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 2559 | } |
| 2560 | |
| 2561 | static void h2_shutr(struct conn_stream *cs, enum cs_shr_mode mode) |
| 2562 | { |
Willy Tarreau | c7576ea | 2017-10-29 22:00:09 +0100 | [diff] [blame] | 2563 | struct h2s *h2s = cs->ctx; |
| 2564 | |
| 2565 | if (!mode) |
| 2566 | return; |
| 2567 | |
Willy Tarreau | 721c974 | 2017-11-07 11:05:42 +0100 | [diff] [blame] | 2568 | 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] | 2569 | return; |
| 2570 | |
Willy Tarreau | 926fa4c | 2017-11-07 14:42:12 +0100 | [diff] [blame] | 2571 | /* if no outgoing data was seen on this stream, it means it was |
| 2572 | * closed with a "tcp-request content" rule that is normally |
| 2573 | * used to kill the connection ASAP (eg: limit abuse). In this |
| 2574 | * case we send a goaway to close the connection. |
| 2575 | */ |
Willy Tarreau | 90c3232 | 2017-11-24 08:00:30 +0100 | [diff] [blame] | 2576 | if (!(h2s->flags & H2_SF_RST_SENT) && |
| 2577 | h2s_send_rst_stream(h2s->h2c, h2s) <= 0) |
Willy Tarreau | b2e290a | 2018-03-30 17:35:38 +0200 | [diff] [blame] | 2578 | goto add_to_list; |
Willy Tarreau | 90c3232 | 2017-11-24 08:00:30 +0100 | [diff] [blame] | 2579 | |
Willy Tarreau | 926fa4c | 2017-11-07 14:42:12 +0100 | [diff] [blame] | 2580 | if (!(h2s->flags & H2_SF_OUTGOING_DATA) && |
| 2581 | !(h2s->h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED)) && |
| 2582 | h2c_send_goaway_error(h2s->h2c, h2s) <= 0) |
Willy Tarreau | b2e290a | 2018-03-30 17:35:38 +0200 | [diff] [blame] | 2583 | goto add_to_list; |
Willy Tarreau | 926fa4c | 2017-11-07 14:42:12 +0100 | [diff] [blame] | 2584 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2585 | if (b_data(&h2s->h2c->mbuf) && !(cs->conn->flags & CO_FL_XPRT_WR_ENA)) |
Willy Tarreau | c7576ea | 2017-10-29 22:00:09 +0100 | [diff] [blame] | 2586 | conn_xprt_want_send(cs->conn); |
| 2587 | |
Willy Tarreau | 00dd078 | 2018-03-01 16:31:34 +0100 | [diff] [blame] | 2588 | h2s_close(h2s); |
Willy Tarreau | b2e290a | 2018-03-30 17:35:38 +0200 | [diff] [blame] | 2589 | |
| 2590 | add_to_list: |
| 2591 | if (LIST_ISEMPTY(&h2s->list)) { |
| 2592 | if (h2s->flags & H2_SF_BLK_MFCTL) |
| 2593 | LIST_ADDQ(&h2s->h2c->fctl_list, &h2s->list); |
| 2594 | else if (h2s->flags & (H2_SF_BLK_MBUSY|H2_SF_BLK_MROOM)) |
| 2595 | LIST_ADDQ(&h2s->h2c->send_list, &h2s->list); |
| 2596 | } |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 2597 | } |
| 2598 | |
| 2599 | static void h2_shutw(struct conn_stream *cs, enum cs_shw_mode mode) |
| 2600 | { |
Willy Tarreau | c7576ea | 2017-10-29 22:00:09 +0100 | [diff] [blame] | 2601 | struct h2s *h2s = cs->ctx; |
| 2602 | |
Willy Tarreau | 721c974 | 2017-11-07 11:05:42 +0100 | [diff] [blame] | 2603 | 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] | 2604 | return; |
| 2605 | |
Willy Tarreau | 6743420 | 2017-11-06 20:20:51 +0100 | [diff] [blame] | 2606 | if (h2s->flags & H2_SF_HEADERS_SENT) { |
Willy Tarreau | 58e3208 | 2017-11-07 14:41:09 +0100 | [diff] [blame] | 2607 | /* we can cleanly close using an empty data frame only after headers */ |
| 2608 | |
| 2609 | if (!(h2s->flags & (H2_SF_ES_SENT|H2_SF_RST_SENT)) && |
| 2610 | h2_send_empty_data_es(h2s) <= 0) |
Willy Tarreau | b2e290a | 2018-03-30 17:35:38 +0200 | [diff] [blame] | 2611 | goto add_to_list; |
Willy Tarreau | 58e3208 | 2017-11-07 14:41:09 +0100 | [diff] [blame] | 2612 | |
| 2613 | if (h2s->st == H2_SS_HREM) |
Willy Tarreau | 00dd078 | 2018-03-01 16:31:34 +0100 | [diff] [blame] | 2614 | h2s_close(h2s); |
Willy Tarreau | 58e3208 | 2017-11-07 14:41:09 +0100 | [diff] [blame] | 2615 | else |
| 2616 | h2s->st = H2_SS_HLOC; |
Willy Tarreau | c7576ea | 2017-10-29 22:00:09 +0100 | [diff] [blame] | 2617 | } else { |
Willy Tarreau | 926fa4c | 2017-11-07 14:42:12 +0100 | [diff] [blame] | 2618 | /* if no outgoing data was seen on this stream, it means it was |
| 2619 | * closed with a "tcp-request content" rule that is normally |
| 2620 | * used to kill the connection ASAP (eg: limit abuse). In this |
| 2621 | * case we send a goaway to close the connection. |
Willy Tarreau | a1349f0 | 2017-10-31 07:41:55 +0100 | [diff] [blame] | 2622 | */ |
Willy Tarreau | 90c3232 | 2017-11-24 08:00:30 +0100 | [diff] [blame] | 2623 | if (!(h2s->flags & H2_SF_RST_SENT) && |
| 2624 | h2s_send_rst_stream(h2s->h2c, h2s) <= 0) |
Willy Tarreau | b2e290a | 2018-03-30 17:35:38 +0200 | [diff] [blame] | 2625 | goto add_to_list; |
Willy Tarreau | 90c3232 | 2017-11-24 08:00:30 +0100 | [diff] [blame] | 2626 | |
Willy Tarreau | 926fa4c | 2017-11-07 14:42:12 +0100 | [diff] [blame] | 2627 | if (!(h2s->flags & H2_SF_OUTGOING_DATA) && |
| 2628 | !(h2s->h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED)) && |
Willy Tarreau | a1349f0 | 2017-10-31 07:41:55 +0100 | [diff] [blame] | 2629 | h2c_send_goaway_error(h2s->h2c, h2s) <= 0) |
Willy Tarreau | b2e290a | 2018-03-30 17:35:38 +0200 | [diff] [blame] | 2630 | goto add_to_list; |
Willy Tarreau | a1349f0 | 2017-10-31 07:41:55 +0100 | [diff] [blame] | 2631 | |
Willy Tarreau | 00dd078 | 2018-03-01 16:31:34 +0100 | [diff] [blame] | 2632 | h2s_close(h2s); |
Willy Tarreau | c7576ea | 2017-10-29 22:00:09 +0100 | [diff] [blame] | 2633 | } |
| 2634 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2635 | if (b_data(&h2s->h2c->mbuf) && !(cs->conn->flags & CO_FL_XPRT_WR_ENA)) |
Willy Tarreau | c7576ea | 2017-10-29 22:00:09 +0100 | [diff] [blame] | 2636 | conn_xprt_want_send(cs->conn); |
Willy Tarreau | b2e290a | 2018-03-30 17:35:38 +0200 | [diff] [blame] | 2637 | |
| 2638 | add_to_list: |
| 2639 | if (LIST_ISEMPTY(&h2s->list)) { |
| 2640 | if (h2s->flags & H2_SF_BLK_MFCTL) |
| 2641 | LIST_ADDQ(&h2s->h2c->fctl_list, &h2s->list); |
| 2642 | else if (h2s->flags & (H2_SF_BLK_MBUSY|H2_SF_BLK_MROOM)) |
| 2643 | LIST_ADDQ(&h2s->h2c->send_list, &h2s->list); |
| 2644 | } |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 2645 | } |
| 2646 | |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 2647 | /* Decode the payload of a HEADERS frame and produce the equivalent HTTP/1 |
| 2648 | * request. Returns the number of bytes emitted if > 0, or 0 if it couldn't |
| 2649 | * proceed. Stream errors are reported in h2s->errcode and connection errors |
Willy Tarreau | 6847262 | 2017-12-11 18:36:37 +0100 | [diff] [blame] | 2650 | * in h2c->errcode. |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 2651 | */ |
Willy Tarreau | 337ea57 | 2018-06-19 06:23:38 +0200 | [diff] [blame] | 2652 | static int h2_frt_decode_headers(struct h2s *h2s, struct buffer *buf, int count, int flags) |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 2653 | { |
| 2654 | struct h2c *h2c = h2s->h2c; |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2655 | const uint8_t *hdrs = (uint8_t *)b_head(&h2c->dbuf); |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 2656 | struct buffer *tmp = get_trash_chunk(); |
Willy Tarreau | 59a10fb | 2017-11-21 20:03:02 +0100 | [diff] [blame] | 2657 | struct http_hdr list[MAX_HTTP_HDR * 2]; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 2658 | struct buffer *copy = NULL; |
Willy Tarreau | 174b06a | 2018-04-25 18:13:58 +0200 | [diff] [blame] | 2659 | unsigned int msgf; |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 2660 | int flen = h2c->dfl; |
| 2661 | int outlen = 0; |
| 2662 | int wrap; |
| 2663 | int try; |
| 2664 | |
| 2665 | if (!h2c->dfl) { |
| 2666 | h2s_error(h2s, H2_ERR_PROTOCOL_ERROR); // empty headers frame! |
Willy Tarreau | a20a519 | 2017-12-27 11:02:06 +0100 | [diff] [blame] | 2667 | h2c->st0 = H2_CS_FRAME_E; |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 2668 | return 0; |
| 2669 | } |
| 2670 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2671 | if (b_data(&h2c->dbuf) < h2c->dfl && !b_full(&h2c->dbuf)) |
Willy Tarreau | 6847262 | 2017-12-11 18:36:37 +0100 | [diff] [blame] | 2672 | return 0; // incomplete input frame |
| 2673 | |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 2674 | /* if the input buffer wraps, take a temporary copy of it (rare) */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2675 | wrap = b_wrap(&h2c->dbuf) - b_head(&h2c->dbuf); |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 2676 | if (wrap < h2c->dfl) { |
Willy Tarreau | 68dd985 | 2017-07-03 14:44:26 +0200 | [diff] [blame] | 2677 | copy = alloc_trash_chunk(); |
| 2678 | if (!copy) { |
| 2679 | h2c_error(h2c, H2_ERR_INTERNAL_ERROR); |
| 2680 | goto fail; |
| 2681 | } |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2682 | memcpy(copy->area, b_head(&h2c->dbuf), wrap); |
| 2683 | memcpy(copy->area + wrap, b_orig(&h2c->dbuf), h2c->dfl - wrap); |
| 2684 | hdrs = (uint8_t *) copy->area; |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 2685 | } |
| 2686 | |
| 2687 | /* The padlen is the first byte before data, and the padding appears |
| 2688 | * after data. padlen+data+padding are included in flen. |
| 2689 | */ |
| 2690 | if (h2c->dff & H2_F_HEADERS_PADDED) { |
Willy Tarreau | 05e5daf | 2017-12-11 15:17:36 +0100 | [diff] [blame] | 2691 | h2c->dpl = *hdrs; |
| 2692 | if (h2c->dpl >= flen) { |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 2693 | /* RFC7540#6.2 : pad length = length of frame payload or greater */ |
| 2694 | h2c_error(h2c, H2_ERR_PROTOCOL_ERROR); |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 2695 | return 0; |
| 2696 | } |
Willy Tarreau | 05e5daf | 2017-12-11 15:17:36 +0100 | [diff] [blame] | 2697 | flen -= h2c->dpl + 1; |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 2698 | hdrs += 1; // skip Pad Length |
| 2699 | } |
| 2700 | |
| 2701 | /* Skip StreamDep and weight for now (we don't support PRIORITY) */ |
| 2702 | if (h2c->dff & H2_F_HEADERS_PRIORITY) { |
Willy Tarreau | 18b86cd | 2017-12-03 19:24:50 +0100 | [diff] [blame] | 2703 | if (read_n32(hdrs) == h2s->id) { |
| 2704 | /* RFC7540#5.3.1 : stream dep may not depend on itself */ |
| 2705 | h2c_error(h2c, H2_ERR_PROTOCOL_ERROR); |
| 2706 | return 0;//goto fail_stream; |
| 2707 | } |
| 2708 | |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 2709 | hdrs += 5; // stream dep = 4, weight = 1 |
| 2710 | flen -= 5; |
| 2711 | } |
| 2712 | |
| 2713 | /* FIXME: lack of END_HEADERS means there's a continuation frame, we |
| 2714 | * don't support this for now and can't even decompress so we have to |
| 2715 | * break the connection. |
| 2716 | */ |
| 2717 | if (!(h2c->dff & H2_F_HEADERS_END_HEADERS)) { |
| 2718 | h2c_error(h2c, H2_ERR_INTERNAL_ERROR); |
Willy Tarreau | 68dd985 | 2017-07-03 14:44:26 +0200 | [diff] [blame] | 2719 | goto fail; |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 2720 | } |
| 2721 | |
Willy Tarreau | 59a10fb | 2017-11-21 20:03:02 +0100 | [diff] [blame] | 2722 | /* we can't retry a failed decompression operation so we must be very |
| 2723 | * careful not to take any risks. In practice the output buffer is |
| 2724 | * always empty except maybe for trailers, so these operations almost |
| 2725 | * never happen. |
| 2726 | */ |
Willy Tarreau | 337ea57 | 2018-06-19 06:23:38 +0200 | [diff] [blame] | 2727 | if (flags & CO_RFL_BUF_WET) { |
Willy Tarreau | 59a10fb | 2017-11-21 20:03:02 +0100 | [diff] [blame] | 2728 | /* need to let the output buffer flush and |
| 2729 | * mark the buffer for later wake up. |
| 2730 | */ |
| 2731 | goto fail; |
| 2732 | } |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 2733 | |
Willy Tarreau | aa7af72 | 2018-07-12 10:33:12 +0200 | [diff] [blame] | 2734 | if (unlikely(b_space_wraps(buf))) { |
Willy Tarreau | 59a10fb | 2017-11-21 20:03:02 +0100 | [diff] [blame] | 2735 | /* it doesn't fit and the buffer is fragmented, |
| 2736 | * so let's defragment it and try again. |
| 2737 | */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2738 | b_slow_realign(buf, trash.area, 0); |
Willy Tarreau | 59a10fb | 2017-11-21 20:03:02 +0100 | [diff] [blame] | 2739 | } |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 2740 | |
Willy Tarreau | b7b5fe1 | 2018-06-18 13:33:09 +0200 | [diff] [blame] | 2741 | try = b_contig_space(buf); |
| 2742 | if (!try) |
| 2743 | goto fail; |
Willy Tarreau | 59a10fb | 2017-11-21 20:03:02 +0100 | [diff] [blame] | 2744 | |
Willy Tarreau | 59a10fb | 2017-11-21 20:03:02 +0100 | [diff] [blame] | 2745 | if (try > count) |
| 2746 | try = count; |
| 2747 | |
| 2748 | outlen = hpack_decode_frame(h2c->ddht, hdrs, flen, list, |
| 2749 | sizeof(list)/sizeof(list[0]), tmp); |
| 2750 | if (outlen < 0) { |
| 2751 | h2c_error(h2c, H2_ERR_COMPRESSION_ERROR); |
| 2752 | goto fail; |
| 2753 | } |
| 2754 | |
| 2755 | /* OK now we have our header list in <list> */ |
Willy Tarreau | 174b06a | 2018-04-25 18:13:58 +0200 | [diff] [blame] | 2756 | msgf = (h2c->dff & H2_F_DATA_END_STREAM) ? 0 : H2_MSGF_BODY; |
Willy Tarreau | 8f9c72d | 2018-06-07 18:46:28 +0200 | [diff] [blame] | 2757 | outlen = h2_make_h1_request(list, b_tail(buf), try, &msgf); |
Willy Tarreau | 59a10fb | 2017-11-21 20:03:02 +0100 | [diff] [blame] | 2758 | |
| 2759 | if (outlen < 0) { |
| 2760 | h2c_error(h2c, H2_ERR_COMPRESSION_ERROR); |
| 2761 | goto fail; |
| 2762 | } |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 2763 | |
Willy Tarreau | 174b06a | 2018-04-25 18:13:58 +0200 | [diff] [blame] | 2764 | if (msgf & H2_MSGF_BODY) { |
| 2765 | /* a payload is present */ |
| 2766 | if (msgf & H2_MSGF_BODY_CL) |
| 2767 | h2s->flags |= H2_SF_DATA_CLEN; |
| 2768 | else if (!(msgf & H2_MSGF_BODY_TUNNEL)) |
| 2769 | h2s->flags |= H2_SF_DATA_CHNK; |
| 2770 | } |
| 2771 | |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 2772 | /* now consume the input data */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2773 | b_del(&h2c->dbuf, h2c->dfl); |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 2774 | h2c->st0 = H2_CS_FRAME_H; |
Olivier Houchard | acd1403 | 2018-06-28 18:17:23 +0200 | [diff] [blame] | 2775 | b_add(buf, outlen); |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 2776 | |
| 2777 | /* don't send it before returning data! |
| 2778 | * FIXME: should we instead try to send it much later, after the |
| 2779 | * response ? This would require that we keep a copy of it in h2s. |
| 2780 | */ |
| 2781 | if (h2c->dff & H2_F_HEADERS_END_STREAM) { |
| 2782 | h2s->cs->flags |= CS_FL_EOS; |
| 2783 | h2s->flags |= H2_SF_ES_RCVD; |
| 2784 | } |
| 2785 | |
Willy Tarreau | 68dd985 | 2017-07-03 14:44:26 +0200 | [diff] [blame] | 2786 | leave: |
| 2787 | free_trash_chunk(copy); |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 2788 | return outlen; |
Willy Tarreau | 68dd985 | 2017-07-03 14:44:26 +0200 | [diff] [blame] | 2789 | fail: |
| 2790 | outlen = 0; |
| 2791 | goto leave; |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 2792 | } |
| 2793 | |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 2794 | /* Transfer the payload of a DATA frame to the HTTP/1 side. When content-length |
| 2795 | * or a tunnel is used, the contents are copied as-is. When chunked encoding is |
| 2796 | * in use, a new chunk is emitted for each frame. This is supposed to fit |
| 2797 | * because the smallest chunk takes 1 byte for the size, 2 for CRLF, X for the |
| 2798 | * data, 2 for the extra CRLF, so that's 5+X, while on the H2 side the smallest |
| 2799 | * frame will be 9+X bytes based on the same buffer size. The HTTP/2 frame |
| 2800 | * parser state is automatically updated. Returns the number of bytes emitted |
Willy Tarreau | 8fc016d | 2017-12-11 18:27:15 +0100 | [diff] [blame] | 2801 | * if > 0, or 0 if it couldn't proceed, in which case CS_FL_RCV_MORE must be |
| 2802 | * checked to know if some data remain pending (an empty DATA frame can return |
| 2803 | * 0 as a valid result). Stream errors are reported in h2s->errcode and |
| 2804 | * connection errors in h2c->errcode. The caller must already have checked the |
| 2805 | * frame header and ensured that the frame was complete or the buffer full. It |
| 2806 | * changes the frame state to FRAME_A once done. |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 2807 | */ |
Willy Tarreau | 337ea57 | 2018-06-19 06:23:38 +0200 | [diff] [blame] | 2808 | static int h2_frt_transfer_data(struct h2s *h2s, struct buffer *buf, int count, int flags) |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 2809 | { |
| 2810 | struct h2c *h2c = h2s->h2c; |
| 2811 | int block1, block2; |
| 2812 | unsigned int flen = h2c->dfl; |
Willy Tarreau | eba10f2 | 2018-04-25 20:44:22 +0200 | [diff] [blame] | 2813 | unsigned int chklen = 0; |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 2814 | |
Willy Tarreau | c9ede6c | 2017-12-10 21:28:43 +0100 | [diff] [blame] | 2815 | h2s->cs->flags &= ~CS_FL_RCV_MORE; |
Willy Tarreau | 8fc016d | 2017-12-11 18:27:15 +0100 | [diff] [blame] | 2816 | h2c->flags &= ~H2_CF_DEM_SFULL; |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 2817 | |
| 2818 | /* The padlen is the first byte before data, and the padding appears |
| 2819 | * after data. padlen+data+padding are included in flen. |
| 2820 | */ |
Willy Tarreau | 7912781 | 2017-12-03 21:06:59 +0100 | [diff] [blame] | 2821 | if (h2c->dff & H2_F_DATA_PADDED) { |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2822 | if (b_data(&h2c->dbuf) < 1) |
Willy Tarreau | 8fc016d | 2017-12-11 18:27:15 +0100 | [diff] [blame] | 2823 | return 0; |
| 2824 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2825 | h2c->dpl = *(uint8_t *)b_head(&h2c->dbuf); |
Willy Tarreau | 05e5daf | 2017-12-11 15:17:36 +0100 | [diff] [blame] | 2826 | if (h2c->dpl >= h2c->dfl) { |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 2827 | /* RFC7540#6.1 : pad length = length of frame payload or greater */ |
| 2828 | h2c_error(h2c, H2_ERR_PROTOCOL_ERROR); |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 2829 | return 0; |
| 2830 | } |
Willy Tarreau | 8fc016d | 2017-12-11 18:27:15 +0100 | [diff] [blame] | 2831 | |
| 2832 | /* skip the padlen byte */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2833 | b_del(&h2c->dbuf, 1); |
Willy Tarreau | 8fc016d | 2017-12-11 18:27:15 +0100 | [diff] [blame] | 2834 | h2c->dfl--; |
| 2835 | h2c->rcvd_c++; h2c->rcvd_s++; |
| 2836 | h2c->dff &= ~H2_F_DATA_PADDED; |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 2837 | } |
| 2838 | |
Willy Tarreau | 8fc016d | 2017-12-11 18:27:15 +0100 | [diff] [blame] | 2839 | flen = h2c->dfl - h2c->dpl; |
| 2840 | if (!flen) |
Willy Tarreau | 4a28da1 | 2018-01-04 14:41:00 +0100 | [diff] [blame] | 2841 | goto end_transfer; |
Willy Tarreau | 8fc016d | 2017-12-11 18:27:15 +0100 | [diff] [blame] | 2842 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2843 | if (flen > b_data(&h2c->dbuf)) { |
| 2844 | flen = b_data(&h2c->dbuf); |
Willy Tarreau | 8fc016d | 2017-12-11 18:27:15 +0100 | [diff] [blame] | 2845 | if (!flen) |
| 2846 | return 0; |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 2847 | } |
| 2848 | |
Willy Tarreau | eba10f2 | 2018-04-25 20:44:22 +0200 | [diff] [blame] | 2849 | /* chunked-encoding requires more room */ |
| 2850 | if (h2s->flags & H2_SF_DATA_CHNK) { |
| 2851 | chklen = MIN(flen, count); |
| 2852 | chklen = (chklen < 16) ? 1 : (chklen < 256) ? 2 : |
| 2853 | (chklen < 4096) ? 3 : (chklen < 65536) ? 4 : |
| 2854 | (chklen < 1048576) ? 4 : 8; |
| 2855 | chklen += 4; // CRLF, CRLF |
| 2856 | } |
| 2857 | |
Willy Tarreau | 8fc016d | 2017-12-11 18:27:15 +0100 | [diff] [blame] | 2858 | /* does it fit in output buffer or should we wait ? */ |
Willy Tarreau | eba10f2 | 2018-04-25 20:44:22 +0200 | [diff] [blame] | 2859 | if (flen + chklen > count) { |
| 2860 | if (chklen >= count) |
| 2861 | goto full; |
| 2862 | flen = count - chklen; |
| 2863 | } |
| 2864 | |
| 2865 | if (h2s->flags & H2_SF_DATA_CHNK) { |
| 2866 | /* emit the chunk size */ |
| 2867 | unsigned int chksz = flen; |
| 2868 | char str[10]; |
| 2869 | char *beg; |
| 2870 | |
| 2871 | beg = str + sizeof(str); |
| 2872 | *--beg = '\n'; |
| 2873 | *--beg = '\r'; |
| 2874 | do { |
| 2875 | *--beg = hextab[chksz & 0xF]; |
| 2876 | } while (chksz >>= 4); |
Willy Tarreau | 55372f6 | 2018-07-10 10:04:02 +0200 | [diff] [blame] | 2877 | b_putblk(buf, beg, str + sizeof(str) - beg); |
Willy Tarreau | 8fc016d | 2017-12-11 18:27:15 +0100 | [diff] [blame] | 2878 | } |
| 2879 | |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 2880 | /* Block1 is the length of the first block before the buffer wraps, |
| 2881 | * block2 is the optional second block to reach the end of the frame. |
| 2882 | */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2883 | block1 = b_contig_data(&h2c->dbuf, 0); |
Willy Tarreau | 8fc016d | 2017-12-11 18:27:15 +0100 | [diff] [blame] | 2884 | if (block1 > flen) |
| 2885 | block1 = flen; |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 2886 | block2 = flen - block1; |
| 2887 | |
| 2888 | if (block1) |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2889 | b_putblk(buf, b_head(&h2c->dbuf), block1); |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 2890 | |
| 2891 | if (block2) |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2892 | b_putblk(buf, b_peek(&h2c->dbuf, block1), block2); |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 2893 | |
Willy Tarreau | eba10f2 | 2018-04-25 20:44:22 +0200 | [diff] [blame] | 2894 | if (h2s->flags & H2_SF_DATA_CHNK) { |
| 2895 | /* emit the CRLF */ |
Willy Tarreau | 55372f6 | 2018-07-10 10:04:02 +0200 | [diff] [blame] | 2896 | b_putblk(buf, "\r\n", 2); |
Willy Tarreau | eba10f2 | 2018-04-25 20:44:22 +0200 | [diff] [blame] | 2897 | } |
| 2898 | |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 2899 | /* now mark the input data as consumed (will be deleted from the buffer |
| 2900 | * by the caller when seeing FRAME_A after sending the window update). |
| 2901 | */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2902 | b_del(&h2c->dbuf, flen); |
Willy Tarreau | 8fc016d | 2017-12-11 18:27:15 +0100 | [diff] [blame] | 2903 | h2c->dfl -= flen; |
| 2904 | h2c->rcvd_c += flen; |
| 2905 | h2c->rcvd_s += flen; // warning, this can also affect the closed streams! |
| 2906 | |
| 2907 | if (h2c->dfl > h2c->dpl) { |
| 2908 | /* more data available, transfer stalled on stream full */ |
Willy Tarreau | eba10f2 | 2018-04-25 20:44:22 +0200 | [diff] [blame] | 2909 | goto more; |
Willy Tarreau | 8fc016d | 2017-12-11 18:27:15 +0100 | [diff] [blame] | 2910 | } |
| 2911 | |
Willy Tarreau | 4a28da1 | 2018-01-04 14:41:00 +0100 | [diff] [blame] | 2912 | end_transfer: |
Willy Tarreau | 8fc016d | 2017-12-11 18:27:15 +0100 | [diff] [blame] | 2913 | /* here we're done with the frame, all the payload (except padding) was |
| 2914 | * transferred. |
| 2915 | */ |
Willy Tarreau | eba10f2 | 2018-04-25 20:44:22 +0200 | [diff] [blame] | 2916 | |
| 2917 | if (h2c->dff & H2_F_DATA_END_STREAM && h2s->flags & H2_SF_DATA_CHNK) { |
| 2918 | /* emit the trailing 0 CRLF CRLF */ |
| 2919 | if (count < 5) |
| 2920 | goto more; |
| 2921 | chklen += 5; |
Willy Tarreau | 55372f6 | 2018-07-10 10:04:02 +0200 | [diff] [blame] | 2922 | b_putblk(buf, "0\r\n\r\n", 5); |
Willy Tarreau | eba10f2 | 2018-04-25 20:44:22 +0200 | [diff] [blame] | 2923 | } |
| 2924 | |
Willy Tarreau | d1023bb | 2018-03-22 16:53:12 +0100 | [diff] [blame] | 2925 | h2c->rcvd_c += h2c->dpl; |
| 2926 | h2c->rcvd_s += h2c->dpl; |
| 2927 | h2c->dpl = 0; |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 2928 | h2c->st0 = H2_CS_FRAME_A; // send the corresponding window update |
| 2929 | |
| 2930 | /* don't send it before returning data! |
| 2931 | * FIXME: should we instead try to send it much later, after the |
| 2932 | * response ? This would require that we keep a copy of it in h2s. |
| 2933 | */ |
Willy Tarreau | 7912781 | 2017-12-03 21:06:59 +0100 | [diff] [blame] | 2934 | if (h2c->dff & H2_F_DATA_END_STREAM) { |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 2935 | h2s->cs->flags |= CS_FL_EOS; |
| 2936 | h2s->flags |= H2_SF_ES_RCVD; |
| 2937 | } |
| 2938 | |
Willy Tarreau | eba10f2 | 2018-04-25 20:44:22 +0200 | [diff] [blame] | 2939 | return flen + chklen; |
| 2940 | full: |
| 2941 | flen = chklen = 0; |
| 2942 | more: |
| 2943 | h2c->flags |= H2_CF_DEM_SFULL; |
| 2944 | h2s->cs->flags |= CS_FL_RCV_MORE; |
| 2945 | return flen + chklen; |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 2946 | } |
| 2947 | |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 2948 | /* |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 2949 | * Called from the upper layer to get more data, up to <count> bytes. The |
| 2950 | * caller is responsible for never asking for more data than what is available |
| 2951 | * in the buffer. |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 2952 | */ |
Willy Tarreau | 7f3225f | 2018-06-19 06:15:17 +0200 | [diff] [blame] | 2953 | static size_t h2_rcv_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] | 2954 | { |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 2955 | struct h2s *h2s = cs->ctx; |
| 2956 | struct h2c *h2c = h2s->h2c; |
Willy Tarreau | d9cf540 | 2018-07-18 11:29:06 +0200 | [diff] [blame] | 2957 | size_t ret = 0; |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 2958 | |
| 2959 | if (h2c->st0 != H2_CS_FRAME_P) |
| 2960 | return 0; // no pre-parsed frame yet |
| 2961 | |
| 2962 | if (h2c->dsi != h2s->id) |
| 2963 | return 0; // not for us |
| 2964 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2965 | if (!b_size(&h2c->dbuf)) |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 2966 | return 0; // empty buffer |
| 2967 | |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 2968 | switch (h2c->dft) { |
| 2969 | case H2_FT_HEADERS: |
Willy Tarreau | 337ea57 | 2018-06-19 06:23:38 +0200 | [diff] [blame] | 2970 | ret = h2_frt_decode_headers(h2s, buf, count, flags); |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 2971 | break; |
| 2972 | |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 2973 | case H2_FT_DATA: |
Willy Tarreau | 337ea57 | 2018-06-19 06:23:38 +0200 | [diff] [blame] | 2974 | ret = h2_frt_transfer_data(h2s, buf, count, flags); |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 2975 | break; |
| 2976 | |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 2977 | default: |
| 2978 | ret = 0; |
| 2979 | } |
| 2980 | return ret; |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 2981 | } |
| 2982 | |
Willy Tarreau | 5dd1735 | 2018-06-14 13:33:30 +0200 | [diff] [blame] | 2983 | /* Try to send a HEADERS frame matching HTTP/1 response present at offset <ofs> |
| 2984 | * and for <max> bytes in buffer <buf> for the H2 stream <h2s>. Returns the |
| 2985 | * number of bytes sent. The caller must check the stream's status to detect |
| 2986 | * any error which might have happened subsequently to a successful send. |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 2987 | */ |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 2988 | 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] | 2989 | { |
| 2990 | struct http_hdr list[MAX_HTTP_HDR]; |
| 2991 | struct h2c *h2c = h2s->h2c; |
| 2992 | struct h1m *h1m = &h2s->res; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 2993 | struct buffer outbuf; |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 2994 | int es_now = 0; |
| 2995 | int ret = 0; |
| 2996 | int hdr; |
| 2997 | |
| 2998 | if (h2c_mux_busy(h2c, h2s)) { |
| 2999 | h2s->flags |= H2_SF_BLK_MBUSY; |
| 3000 | return 0; |
| 3001 | } |
| 3002 | |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 3003 | if (!h2_get_buf(h2c, &h2c->mbuf)) { |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 3004 | h2c->flags |= H2_CF_MUX_MALLOC; |
| 3005 | h2s->flags |= H2_SF_BLK_MROOM; |
| 3006 | return 0; |
| 3007 | } |
| 3008 | |
| 3009 | /* First, try to parse the H1 response and index it into <list>. |
| 3010 | * NOTE! Since it comes from haproxy, we *know* that a response header |
| 3011 | * block does not wrap and we can safely read it this way without |
| 3012 | * having to realign the buffer. |
| 3013 | */ |
Willy Tarreau | 5dd1735 | 2018-06-14 13:33:30 +0200 | [diff] [blame] | 3014 | ret = h1_headers_to_hdr_list(b_peek(buf, ofs), b_peek(buf, ofs) + max, |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 3015 | list, sizeof(list)/sizeof(list[0]), h1m); |
| 3016 | if (ret <= 0) { |
Willy Tarreau | f13ef96 | 2017-11-02 15:14:19 +0100 | [diff] [blame] | 3017 | /* incomplete or invalid response, this is abnormal coming from |
| 3018 | * haproxy and may only result in a bad errorfile or bad Lua code |
| 3019 | * so that won't be fixed, raise an error now. |
| 3020 | * |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 3021 | * FIXME: we should instead add the ability to only return a |
| 3022 | * 502 bad gateway. But in theory this is not supposed to |
| 3023 | * happen. |
| 3024 | */ |
| 3025 | h2s_error(h2s, H2_ERR_INTERNAL_ERROR); |
| 3026 | ret = 0; |
| 3027 | goto end; |
| 3028 | } |
| 3029 | |
| 3030 | chunk_reset(&outbuf); |
| 3031 | |
| 3032 | while (1) { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3033 | outbuf.area = b_tail(&h2c->mbuf); |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3034 | outbuf.size = b_contig_space(&h2c->mbuf); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3035 | outbuf.data = 0; |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 3036 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3037 | if (outbuf.size >= 9 || !b_space_wraps(&h2c->mbuf)) |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 3038 | break; |
| 3039 | realign_again: |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3040 | b_slow_realign(&h2c->mbuf, trash.area, b_data(&h2c->mbuf)); |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 3041 | } |
| 3042 | |
| 3043 | if (outbuf.size < 9) { |
| 3044 | h2c->flags |= H2_CF_MUX_MFULL; |
| 3045 | h2s->flags |= H2_SF_BLK_MROOM; |
| 3046 | ret = 0; |
| 3047 | goto end; |
| 3048 | } |
| 3049 | |
| 3050 | /* len: 0x000000 (fill later), type: 1(HEADERS), flags: ENDH=4 */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3051 | memcpy(outbuf.area, "\x00\x00\x00\x01\x04", 5); |
| 3052 | write_n32(outbuf.area + 5, h2s->id); // 4 bytes |
| 3053 | outbuf.data = 9; |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 3054 | |
| 3055 | /* encode status, which necessarily is the first one */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3056 | if (outbuf.data < outbuf.size && h1m->status == 200) |
| 3057 | outbuf.area[outbuf.data++] = 0x88; // indexed field : idx[08]=(":status", "200") |
| 3058 | else if (outbuf.data < outbuf.size && h1m->status == 304) |
| 3059 | outbuf.area[outbuf.data++] = 0x8b; // indexed field : idx[11]=(":status", "304") |
Willy Tarreau | a87f202 | 2017-11-09 11:23:00 +0100 | [diff] [blame] | 3060 | else if (unlikely(list[0].v.len != 3)) { |
| 3061 | /* this is an unparsable response */ |
| 3062 | h2s_error(h2s, H2_ERR_INTERNAL_ERROR); |
| 3063 | ret = 0; |
| 3064 | goto end; |
| 3065 | } |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3066 | else if (unlikely(outbuf.data + 2 + 3 <= outbuf.size)) { |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 3067 | /* basic encoding of the status code */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3068 | outbuf.area[outbuf.data++] = 0x48; // indexed name -- name=":status" (idx 8) |
| 3069 | outbuf.area[outbuf.data++] = 0x03; // 3 bytes status |
| 3070 | outbuf.area[outbuf.data++] = list[0].v.ptr[0]; |
| 3071 | outbuf.area[outbuf.data++] = list[0].v.ptr[1]; |
| 3072 | outbuf.area[outbuf.data++] = list[0].v.ptr[2]; |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 3073 | } |
| 3074 | else { |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3075 | if (b_space_wraps(&h2c->mbuf)) |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 3076 | goto realign_again; |
| 3077 | |
| 3078 | h2c->flags |= H2_CF_MUX_MFULL; |
| 3079 | h2s->flags |= H2_SF_BLK_MROOM; |
| 3080 | ret = 0; |
| 3081 | goto end; |
| 3082 | } |
| 3083 | |
| 3084 | /* encode all headers, stop at empty name */ |
| 3085 | for (hdr = 1; hdr < sizeof(list)/sizeof(list[0]); hdr++) { |
Willy Tarreau | a76e4c2 | 2017-11-24 08:17:28 +0100 | [diff] [blame] | 3086 | /* these ones do not exist in H2 and must be dropped. */ |
| 3087 | if (isteq(list[hdr].n, ist("connection")) || |
| 3088 | isteq(list[hdr].n, ist("proxy-connection")) || |
| 3089 | isteq(list[hdr].n, ist("keep-alive")) || |
| 3090 | isteq(list[hdr].n, ist("upgrade")) || |
| 3091 | isteq(list[hdr].n, ist("transfer-encoding"))) |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 3092 | continue; |
| 3093 | |
| 3094 | if (isteq(list[hdr].n, ist(""))) |
| 3095 | break; // end |
| 3096 | |
| 3097 | if (!hpack_encode_header(&outbuf, list[hdr].n, list[hdr].v)) { |
| 3098 | /* output full */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3099 | if (b_space_wraps(&h2c->mbuf)) |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 3100 | goto realign_again; |
| 3101 | |
| 3102 | h2c->flags |= H2_CF_MUX_MFULL; |
| 3103 | h2s->flags |= H2_SF_BLK_MROOM; |
| 3104 | ret = 0; |
| 3105 | goto end; |
| 3106 | } |
| 3107 | } |
| 3108 | |
| 3109 | /* we may need to add END_STREAM */ |
| 3110 | if (((h1m->flags & H1_MF_CLEN) && !h1m->body_len) || h2s->cs->flags & CS_FL_SHW) |
| 3111 | es_now = 1; |
| 3112 | |
| 3113 | /* update the frame's size */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3114 | h2_set_frame_size(outbuf.area, outbuf.data - 9); |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 3115 | |
| 3116 | if (es_now) |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3117 | outbuf.area[4] |= H2_F_HEADERS_END_STREAM; |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 3118 | |
| 3119 | /* consume incoming H1 response */ |
Willy Tarreau | 5dd1735 | 2018-06-14 13:33:30 +0200 | [diff] [blame] | 3120 | max -= ret; |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 3121 | |
| 3122 | /* commit the H2 response */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3123 | b_add(&h2c->mbuf, outbuf.data); |
Willy Tarreau | 6743420 | 2017-11-06 20:20:51 +0100 | [diff] [blame] | 3124 | h2s->flags |= H2_SF_HEADERS_SENT; |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 3125 | |
| 3126 | /* for now we don't implemented CONTINUATION, so we wait for a |
| 3127 | * body or directly end in TRL2. |
| 3128 | */ |
| 3129 | if (es_now) { |
Willy Tarreau | 35a6270 | 2018-02-27 15:37:25 +0100 | [diff] [blame] | 3130 | // trim any possibly pending data (eg: inconsistent content-length) |
Willy Tarreau | 5dd1735 | 2018-06-14 13:33:30 +0200 | [diff] [blame] | 3131 | ret += max; |
Willy Tarreau | 35a6270 | 2018-02-27 15:37:25 +0100 | [diff] [blame] | 3132 | |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 3133 | h1m->state = HTTP_MSG_DONE; |
| 3134 | h2s->flags |= H2_SF_ES_SENT; |
| 3135 | if (h2s->st == H2_SS_OPEN) |
| 3136 | h2s->st = H2_SS_HLOC; |
| 3137 | else |
Willy Tarreau | 00dd078 | 2018-03-01 16:31:34 +0100 | [diff] [blame] | 3138 | h2s_close(h2s); |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 3139 | } |
Willy Tarreau | c199faf | 2017-10-31 08:35:27 +0100 | [diff] [blame] | 3140 | else if (h1m->status >= 100 && h1m->status < 200) { |
Willy Tarreau | 8728559 | 2017-11-29 15:41:32 +0100 | [diff] [blame] | 3141 | /* we'll let the caller check if it has more headers to send */ |
Willy Tarreau | c199faf | 2017-10-31 08:35:27 +0100 | [diff] [blame] | 3142 | h1m->state = HTTP_MSG_RPBEFORE; |
| 3143 | h1m->status = 0; |
| 3144 | h1m->flags = 0; |
Willy Tarreau | 8728559 | 2017-11-29 15:41:32 +0100 | [diff] [blame] | 3145 | goto end; |
Willy Tarreau | c199faf | 2017-10-31 08:35:27 +0100 | [diff] [blame] | 3146 | } |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 3147 | else |
Willy Tarreau | 13e4e94 | 2017-12-14 10:55:21 +0100 | [diff] [blame] | 3148 | h1m->state = (h1m->flags & H1_MF_CHNK) ? HTTP_MSG_CHUNK_SIZE : HTTP_MSG_BODY; |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 3149 | |
| 3150 | end: |
| 3151 | //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, h1_msg_state_str(h1m->err_state)); |
| 3152 | return ret; |
| 3153 | } |
| 3154 | |
Willy Tarreau | 5dd1735 | 2018-06-14 13:33:30 +0200 | [diff] [blame] | 3155 | /* Try to send a DATA frame matching HTTP/1 response present at offset <ofs> |
| 3156 | * for up to <max> bytes in response buffer <buf>, for stream <h2s>. Returns |
| 3157 | * the number of bytes sent. The caller must check the stream's status to |
| 3158 | * detect any error which might have happened subsequently to a successful send. |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 3159 | */ |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 3160 | 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] | 3161 | { |
| 3162 | struct h2c *h2c = h2s->h2c; |
| 3163 | struct h1m *h1m = &h2s->res; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 3164 | struct buffer outbuf; |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 3165 | int ret = 0; |
Willy Tarreau | 1dc41e7 | 2018-06-14 13:21:28 +0200 | [diff] [blame] | 3166 | size_t total = 0; |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 3167 | int es_now = 0; |
| 3168 | int size = 0; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 3169 | const char *blk1, *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 3170 | size_t len1, len2; |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 3171 | |
| 3172 | if (h2c_mux_busy(h2c, h2s)) { |
| 3173 | h2s->flags |= H2_SF_BLK_MBUSY; |
| 3174 | goto end; |
| 3175 | } |
| 3176 | |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 3177 | if (!h2_get_buf(h2c, &h2c->mbuf)) { |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 3178 | h2c->flags |= H2_CF_MUX_MALLOC; |
| 3179 | h2s->flags |= H2_SF_BLK_MROOM; |
| 3180 | goto end; |
| 3181 | } |
| 3182 | |
| 3183 | new_frame: |
Willy Tarreau | 5dd1735 | 2018-06-14 13:33:30 +0200 | [diff] [blame] | 3184 | if (!max) |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 3185 | goto end; |
| 3186 | |
| 3187 | chunk_reset(&outbuf); |
| 3188 | |
| 3189 | while (1) { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3190 | outbuf.area = b_tail(&h2c->mbuf); |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3191 | outbuf.size = b_contig_space(&h2c->mbuf); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3192 | outbuf.data = 0; |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 3193 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3194 | if (outbuf.size >= 9 || !b_space_wraps(&h2c->mbuf)) |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 3195 | break; |
| 3196 | realign_again: |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3197 | b_slow_realign(&h2c->mbuf, trash.area, b_data(&h2c->mbuf)); |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 3198 | } |
| 3199 | |
| 3200 | if (outbuf.size < 9) { |
| 3201 | h2c->flags |= H2_CF_MUX_MFULL; |
| 3202 | h2s->flags |= H2_SF_BLK_MROOM; |
| 3203 | goto end; |
| 3204 | } |
| 3205 | |
| 3206 | /* len: 0x000000 (fill later), type: 0(DATA), flags: none=0 */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3207 | memcpy(outbuf.area, "\x00\x00\x00\x00\x00", 5); |
| 3208 | write_n32(outbuf.area + 5, h2s->id); // 4 bytes |
| 3209 | outbuf.data = 9; |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 3210 | |
| 3211 | switch (h1m->flags & (H1_MF_CLEN|H1_MF_CHNK)) { |
| 3212 | case 0: /* no content length, read till SHUTW */ |
Willy Tarreau | 5dd1735 | 2018-06-14 13:33:30 +0200 | [diff] [blame] | 3213 | size = max; |
Willy Tarreau | 13e4e94 | 2017-12-14 10:55:21 +0100 | [diff] [blame] | 3214 | h1m->curr_len = size; |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 3215 | break; |
| 3216 | case H1_MF_CLEN: /* content-length: read only h2m->body_len */ |
Willy Tarreau | 5dd1735 | 2018-06-14 13:33:30 +0200 | [diff] [blame] | 3217 | size = max; |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 3218 | if ((long long)size > h1m->curr_len) |
| 3219 | size = h1m->curr_len; |
| 3220 | break; |
| 3221 | default: /* te:chunked : parse chunks */ |
| 3222 | if (h1m->state == HTTP_MSG_CHUNK_CRLF) { |
Willy Tarreau | c0973c6 | 2018-06-14 15:53:21 +0200 | [diff] [blame] | 3223 | ret = h1_skip_chunk_crlf(buf, ofs, ofs + max); |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 3224 | if (!ret) |
| 3225 | goto end; |
| 3226 | |
| 3227 | if (ret < 0) { |
| 3228 | /* FIXME: bad contents. how to proceed here when we're in H2 ? */ |
| 3229 | h1m->err_pos = ret; |
| 3230 | h2s_error(h2s, H2_ERR_INTERNAL_ERROR); |
| 3231 | goto end; |
| 3232 | } |
Willy Tarreau | 5dd1735 | 2018-06-14 13:33:30 +0200 | [diff] [blame] | 3233 | max -= ret; |
| 3234 | ofs += ret; |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 3235 | total += ret; |
| 3236 | h1m->state = HTTP_MSG_CHUNK_SIZE; |
| 3237 | } |
| 3238 | |
| 3239 | if (h1m->state == HTTP_MSG_CHUNK_SIZE) { |
| 3240 | unsigned int chunk; |
Willy Tarreau | 84d6b7a | 2018-06-14 15:59:05 +0200 | [diff] [blame] | 3241 | ret = h1_parse_chunk_size(buf, ofs, ofs + max, &chunk); |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 3242 | if (!ret) |
| 3243 | goto end; |
| 3244 | |
| 3245 | if (ret < 0) { |
| 3246 | /* FIXME: bad contents. how to proceed here when we're in H2 ? */ |
| 3247 | h1m->err_pos = ret; |
| 3248 | h2s_error(h2s, H2_ERR_INTERNAL_ERROR); |
| 3249 | goto end; |
| 3250 | } |
| 3251 | |
| 3252 | size = chunk; |
| 3253 | h1m->curr_len = chunk; |
| 3254 | h1m->body_len += chunk; |
Willy Tarreau | 5dd1735 | 2018-06-14 13:33:30 +0200 | [diff] [blame] | 3255 | max -= ret; |
| 3256 | ofs += ret; |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 3257 | total += ret; |
| 3258 | h1m->state = size ? HTTP_MSG_DATA : HTTP_MSG_TRAILERS; |
| 3259 | if (!size) |
| 3260 | goto send_empty; |
| 3261 | } |
| 3262 | |
| 3263 | /* in MSG_DATA state, continue below */ |
| 3264 | size = h1m->curr_len; |
| 3265 | break; |
| 3266 | } |
| 3267 | |
| 3268 | /* we have in <size> the exact number of bytes we need to copy from |
| 3269 | * the H1 buffer. We need to check this against the connection's and |
| 3270 | * the stream's send windows, and to ensure that this fits in the max |
| 3271 | * frame size and in the buffer's available space minus 9 bytes (for |
| 3272 | * the frame header). The connection's flow control is applied last so |
| 3273 | * that we can use a separate list of streams which are immediately |
| 3274 | * unblocked on window opening. Note: we don't implement padding. |
| 3275 | */ |
| 3276 | |
Willy Tarreau | 5dd1735 | 2018-06-14 13:33:30 +0200 | [diff] [blame] | 3277 | if (size > max) |
| 3278 | size = max; |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 3279 | |
| 3280 | if (size > h2s->mws) |
| 3281 | size = h2s->mws; |
| 3282 | |
| 3283 | if (size <= 0) { |
| 3284 | h2s->flags |= H2_SF_BLK_SFCTL; |
| 3285 | goto end; |
| 3286 | } |
| 3287 | |
| 3288 | if (h2c->mfs && size > h2c->mfs) |
| 3289 | size = h2c->mfs; |
| 3290 | |
| 3291 | if (size + 9 > outbuf.size) { |
| 3292 | /* we have an opportunity for enlarging the too small |
| 3293 | * available space, let's try. |
| 3294 | */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3295 | if (b_space_wraps(&h2c->mbuf)) |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 3296 | goto realign_again; |
| 3297 | size = outbuf.size - 9; |
| 3298 | } |
| 3299 | |
| 3300 | if (size <= 0) { |
| 3301 | h2c->flags |= H2_CF_MUX_MFULL; |
| 3302 | h2s->flags |= H2_SF_BLK_MROOM; |
| 3303 | goto end; |
| 3304 | } |
| 3305 | |
| 3306 | if (size > h2c->mws) |
| 3307 | size = h2c->mws; |
| 3308 | |
| 3309 | if (size <= 0) { |
| 3310 | h2s->flags |= H2_SF_BLK_MFCTL; |
| 3311 | goto end; |
| 3312 | } |
| 3313 | |
| 3314 | /* copy whatever we can */ |
| 3315 | blk1 = blk2 = NULL; // silence a maybe-uninitialized warning |
Willy Tarreau | 5dd1735 | 2018-06-14 13:33:30 +0200 | [diff] [blame] | 3316 | ret = b_getblk_nc(buf, &blk1, &len1, &blk2, &len2, ofs, max); |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 3317 | if (ret == 1) |
| 3318 | len2 = 0; |
| 3319 | |
| 3320 | if (!ret || len1 + len2 < size) { |
| 3321 | /* FIXME: must normally never happen */ |
| 3322 | h2s_error(h2s, H2_ERR_INTERNAL_ERROR); |
| 3323 | goto end; |
| 3324 | } |
| 3325 | |
| 3326 | /* limit len1/len2 to size */ |
| 3327 | if (len1 + len2 > size) { |
| 3328 | int sub = len1 + len2 - size; |
| 3329 | |
| 3330 | if (len2 > sub) |
| 3331 | len2 -= sub; |
| 3332 | else { |
| 3333 | sub -= len2; |
| 3334 | len2 = 0; |
| 3335 | len1 -= sub; |
| 3336 | } |
| 3337 | } |
| 3338 | |
| 3339 | /* now let's copy this this into the output buffer */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3340 | memcpy(outbuf.area + 9, blk1, len1); |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 3341 | if (len2) |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3342 | memcpy(outbuf.area + 9 + len1, blk2, len2); |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 3343 | |
| 3344 | send_empty: |
| 3345 | /* we may need to add END_STREAM */ |
| 3346 | /* FIXME: we should also detect shutdown(w) below, but how ? Maybe we |
| 3347 | * could rely on the MSG_MORE flag as a hint for this ? |
Willy Tarreau | 0061096 | 2018-07-19 10:58:28 +0200 | [diff] [blame] | 3348 | * |
| 3349 | * FIXME: what we do here is not correct because we send end_stream |
| 3350 | * before knowing if we'll have to send a HEADERS frame for the |
| 3351 | * trailers. More importantly we're not consuming the trailing CRLF |
| 3352 | * after the end of trailers, so it will be left to the caller to |
| 3353 | * eat it. The right way to do it would be to measure trailers here |
| 3354 | * and to send ES only if there are no trailers. |
| 3355 | * |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 3356 | */ |
| 3357 | if (((h1m->flags & H1_MF_CLEN) && !(h1m->curr_len - size)) || |
| 3358 | !h1m->curr_len || h1m->state >= HTTP_MSG_DONE) |
| 3359 | es_now = 1; |
| 3360 | |
| 3361 | /* update the frame's size */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3362 | h2_set_frame_size(outbuf.area, size); |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 3363 | |
| 3364 | if (es_now) |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3365 | outbuf.area[4] |= H2_F_DATA_END_STREAM; |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 3366 | |
| 3367 | /* commit the H2 response */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3368 | b_add(&h2c->mbuf, size + 9); |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 3369 | |
| 3370 | /* consume incoming H1 response */ |
| 3371 | if (size > 0) { |
Willy Tarreau | 5dd1735 | 2018-06-14 13:33:30 +0200 | [diff] [blame] | 3372 | max -= size; |
| 3373 | ofs += size; |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 3374 | total += size; |
| 3375 | h1m->curr_len -= size; |
| 3376 | h2s->mws -= size; |
| 3377 | h2c->mws -= size; |
| 3378 | |
| 3379 | if (size && !h1m->curr_len && (h1m->flags & H1_MF_CHNK)) { |
| 3380 | h1m->state = HTTP_MSG_CHUNK_CRLF; |
| 3381 | goto new_frame; |
| 3382 | } |
| 3383 | } |
| 3384 | |
| 3385 | if (es_now) { |
| 3386 | if (h2s->st == H2_SS_OPEN) |
| 3387 | h2s->st = H2_SS_HLOC; |
| 3388 | else |
Willy Tarreau | 00dd078 | 2018-03-01 16:31:34 +0100 | [diff] [blame] | 3389 | h2s_close(h2s); |
Willy Tarreau | 9d89ac8 | 2017-10-31 17:15:59 +0100 | [diff] [blame] | 3390 | |
Willy Tarreau | 35a6270 | 2018-02-27 15:37:25 +0100 | [diff] [blame] | 3391 | if (!(h1m->flags & H1_MF_CHNK)) { |
| 3392 | // trim any possibly pending data (eg: inconsistent content-length) |
Willy Tarreau | 5dd1735 | 2018-06-14 13:33:30 +0200 | [diff] [blame] | 3393 | total += max; |
| 3394 | ofs += max; |
| 3395 | max = 0; |
Willy Tarreau | 35a6270 | 2018-02-27 15:37:25 +0100 | [diff] [blame] | 3396 | |
Willy Tarreau | 9d89ac8 | 2017-10-31 17:15:59 +0100 | [diff] [blame] | 3397 | h1m->state = HTTP_MSG_DONE; |
Willy Tarreau | 35a6270 | 2018-02-27 15:37:25 +0100 | [diff] [blame] | 3398 | } |
Willy Tarreau | 9d89ac8 | 2017-10-31 17:15:59 +0100 | [diff] [blame] | 3399 | |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 3400 | h2s->flags |= H2_SF_ES_SENT; |
| 3401 | } |
| 3402 | |
| 3403 | end: |
Willy Tarreau | b7b5fe1 | 2018-06-18 13:33:09 +0200 | [diff] [blame] | 3404 | 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, h1_msg_state_str(h1m->state), h1m->err_pos, h1_msg_state_str(h1m->err_state), h2c->mws, h2s->mws, (unsigned int)b_data(buf)); |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 3405 | return total; |
| 3406 | } |
| 3407 | |
Olivier Houchard | 6ff2039 | 2018-07-17 18:46:31 +0200 | [diff] [blame] | 3408 | /* Called from the upper layer, to subscribe to events, such as being able to send */ |
| 3409 | static int h2_subscribe(struct conn_stream *cs, int event_type, void *param) |
| 3410 | { |
| 3411 | struct wait_list *sw; |
| 3412 | struct h2s *h2s = cs->ctx; |
| 3413 | |
| 3414 | switch (event_type) { |
| 3415 | case SUB_CAN_SEND: |
| 3416 | sw = param; |
| 3417 | if (LIST_ISEMPTY(&h2s->list) && LIST_ISEMPTY(&sw->list)) |
| 3418 | LIST_ADDQ(&h2s->h2c->send_wait_list, &sw->list); |
| 3419 | return 0; |
| 3420 | default: |
| 3421 | break; |
| 3422 | } |
| 3423 | return -1; |
| 3424 | |
| 3425 | |
| 3426 | } |
| 3427 | |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 3428 | /* Called from the upper layer, to send data */ |
Willy Tarreau | deccd11 | 2018-06-14 18:38:55 +0200 | [diff] [blame] | 3429 | static size_t h2_snd_buf(struct conn_stream *cs, const struct buffer *buf, size_t count, int flags) |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 3430 | { |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 3431 | struct h2s *h2s = cs->ctx; |
Willy Tarreau | 1dc41e7 | 2018-06-14 13:21:28 +0200 | [diff] [blame] | 3432 | size_t total = 0; |
Willy Tarreau | 5dd1735 | 2018-06-14 13:33:30 +0200 | [diff] [blame] | 3433 | size_t ret; |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 3434 | |
Willy Tarreau | 0bad043 | 2018-06-14 16:54:01 +0200 | [diff] [blame] | 3435 | if (!(h2s->flags & H2_SF_OUTGOING_DATA) && count) |
Willy Tarreau | c4312d3 | 2017-11-07 12:01:53 +0100 | [diff] [blame] | 3436 | h2s->flags |= H2_SF_OUTGOING_DATA; |
| 3437 | |
Willy Tarreau | 0bad043 | 2018-06-14 16:54:01 +0200 | [diff] [blame] | 3438 | while (h2s->res.state < HTTP_MSG_DONE && count) { |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 3439 | if (h2s->res.state < HTTP_MSG_BODY) { |
Willy Tarreau | 0bad043 | 2018-06-14 16:54:01 +0200 | [diff] [blame] | 3440 | ret = h2s_frt_make_resp_headers(h2s, buf, total, count); |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 3441 | } |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 3442 | else if (h2s->res.state < HTTP_MSG_TRAILERS) { |
Willy Tarreau | 0bad043 | 2018-06-14 16:54:01 +0200 | [diff] [blame] | 3443 | ret = h2s_frt_make_resp_data(h2s, buf, total, count); |
Willy Tarreau | 9d89ac8 | 2017-10-31 17:15:59 +0100 | [diff] [blame] | 3444 | } |
| 3445 | else if (h2s->res.state == HTTP_MSG_TRAILERS) { |
| 3446 | /* consume the trailers if any (we don't forward them for now) */ |
Willy Tarreau | 0bad043 | 2018-06-14 16:54:01 +0200 | [diff] [blame] | 3447 | ret = h1_measure_trailers(buf, total, count); |
Willy Tarreau | 9d89ac8 | 2017-10-31 17:15:59 +0100 | [diff] [blame] | 3448 | |
Willy Tarreau | 5dd1735 | 2018-06-14 13:33:30 +0200 | [diff] [blame] | 3449 | if (unlikely((int)ret <= 0)) { |
| 3450 | if ((int)ret < 0) |
Willy Tarreau | 9d89ac8 | 2017-10-31 17:15:59 +0100 | [diff] [blame] | 3451 | h2s_error(h2s, H2_ERR_INTERNAL_ERROR); |
| 3452 | break; |
| 3453 | } |
Willy Tarreau | 35a6270 | 2018-02-27 15:37:25 +0100 | [diff] [blame] | 3454 | // trim any possibly pending data (eg: extra CR-LF, ...) |
Willy Tarreau | 0bad043 | 2018-06-14 16:54:01 +0200 | [diff] [blame] | 3455 | total += count; |
| 3456 | count = 0; |
Willy Tarreau | 9d89ac8 | 2017-10-31 17:15:59 +0100 | [diff] [blame] | 3457 | h2s->res.state = HTTP_MSG_DONE; |
| 3458 | break; |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 3459 | } |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 3460 | else { |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 3461 | cs->flags |= CS_FL_ERROR; |
| 3462 | break; |
| 3463 | } |
Willy Tarreau | 0bad043 | 2018-06-14 16:54:01 +0200 | [diff] [blame] | 3464 | |
| 3465 | total += ret; |
| 3466 | count -= ret; |
| 3467 | |
| 3468 | if (h2s->st >= H2_SS_ERROR) |
| 3469 | break; |
| 3470 | |
| 3471 | if (h2s->flags & H2_SF_BLK_ANY) |
| 3472 | break; |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 3473 | } |
| 3474 | |
Willy Tarreau | 0061096 | 2018-07-19 10:58:28 +0200 | [diff] [blame] | 3475 | if (h2s->st >= H2_SS_ERROR) { |
| 3476 | /* trim any possibly pending data after we close (extra CR-LF, |
| 3477 | * unprocessed trailers, abnormal extra data, ...) |
| 3478 | */ |
Willy Tarreau | 0bad043 | 2018-06-14 16:54:01 +0200 | [diff] [blame] | 3479 | total += count; |
| 3480 | count = 0; |
Willy Tarreau | 0061096 | 2018-07-19 10:58:28 +0200 | [diff] [blame] | 3481 | } |
| 3482 | |
Willy Tarreau | c6795ca | 2017-11-07 09:43:06 +0100 | [diff] [blame] | 3483 | /* RST are sent similarly to frame acks */ |
Willy Tarreau | 0249219 | 2017-12-07 15:59:29 +0100 | [diff] [blame] | 3484 | if (h2s->st == H2_SS_ERROR || h2s->flags & H2_SF_RST_RCVD) { |
Willy Tarreau | c6795ca | 2017-11-07 09:43:06 +0100 | [diff] [blame] | 3485 | cs->flags |= CS_FL_ERROR; |
Willy Tarreau | 8c0ea7d | 2017-11-10 10:05:24 +0100 | [diff] [blame] | 3486 | if (h2s_send_rst_stream(h2s->h2c, h2s) > 0) |
Willy Tarreau | 00dd078 | 2018-03-01 16:31:34 +0100 | [diff] [blame] | 3487 | h2s_close(h2s); |
Willy Tarreau | c6795ca | 2017-11-07 09:43:06 +0100 | [diff] [blame] | 3488 | } |
| 3489 | |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 3490 | if (h2s->flags & H2_SF_BLK_SFCTL) { |
| 3491 | /* stream flow control, quit the list */ |
| 3492 | LIST_DEL(&h2s->list); |
| 3493 | LIST_INIT(&h2s->list); |
| 3494 | } |
Willy Tarreau | b2e290a | 2018-03-30 17:35:38 +0200 | [diff] [blame] | 3495 | else if (LIST_ISEMPTY(&h2s->list)) { |
| 3496 | if (h2s->flags & H2_SF_BLK_MFCTL) |
| 3497 | LIST_ADDQ(&h2s->h2c->fctl_list, &h2s->list); |
Willy Tarreau | b2e290a | 2018-03-30 17:35:38 +0200 | [diff] [blame] | 3498 | } |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 3499 | |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 3500 | return total; |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 3501 | } |
| 3502 | |
Willy Tarreau | e3f36cd | 2018-03-30 14:43:13 +0200 | [diff] [blame] | 3503 | /* for debugging with CLI's "show fd" command */ |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 3504 | static void h2_show_fd(struct buffer *msg, struct connection *conn) |
Willy Tarreau | e3f36cd | 2018-03-30 14:43:13 +0200 | [diff] [blame] | 3505 | { |
| 3506 | struct h2c *h2c = conn->mux_ctx; |
| 3507 | struct h2s *h2s; |
| 3508 | struct eb32_node *node; |
| 3509 | int fctl_cnt = 0; |
| 3510 | int send_cnt = 0; |
| 3511 | int tree_cnt = 0; |
| 3512 | int orph_cnt = 0; |
| 3513 | |
| 3514 | if (!h2c) |
| 3515 | return; |
| 3516 | |
| 3517 | list_for_each_entry(h2s, &h2c->fctl_list, list) |
| 3518 | fctl_cnt++; |
| 3519 | |
| 3520 | list_for_each_entry(h2s, &h2c->send_list, list) |
| 3521 | send_cnt++; |
| 3522 | |
| 3523 | node = eb32_first(&h2c->streams_by_id); |
| 3524 | while (node) { |
| 3525 | h2s = container_of(node, struct h2s, by_id); |
| 3526 | tree_cnt++; |
| 3527 | if (!h2s->cs) |
| 3528 | orph_cnt++; |
| 3529 | node = eb32_next(node); |
| 3530 | } |
| 3531 | |
Willy Tarreau | c65edac | 2018-07-19 10:54:43 +0200 | [diff] [blame] | 3532 | chunk_appendf(msg, " st0=%d flg=0x%08x nbst=%u nbcs=%u fctl_cnt=%d send_cnt=%d tree_cnt=%d orph_cnt=%d dbuf=%u/%u mbuf=%u/%u", |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3533 | h2c->st0, h2c->flags, h2c->nb_streams, h2c->nb_cs, fctl_cnt, send_cnt, tree_cnt, orph_cnt, (unsigned int)b_data(&h2c->dbuf), (unsigned int)b_size(&h2c->dbuf), (unsigned int)b_data(&h2c->mbuf), (unsigned int)b_size(&h2c->mbuf)); |
Willy Tarreau | e3f36cd | 2018-03-30 14:43:13 +0200 | [diff] [blame] | 3534 | } |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 3535 | |
| 3536 | /*******************************************************/ |
| 3537 | /* functions below are dedicated to the config parsers */ |
| 3538 | /*******************************************************/ |
| 3539 | |
Willy Tarreau | fe20e5b | 2017-07-27 11:42:14 +0200 | [diff] [blame] | 3540 | /* config parser for global "tune.h2.header-table-size" */ |
| 3541 | static int h2_parse_header_table_size(char **args, int section_type, struct proxy *curpx, |
| 3542 | struct proxy *defpx, const char *file, int line, |
| 3543 | char **err) |
| 3544 | { |
| 3545 | if (too_many_args(1, args, err, NULL)) |
| 3546 | return -1; |
| 3547 | |
| 3548 | h2_settings_header_table_size = atoi(args[1]); |
| 3549 | if (h2_settings_header_table_size < 4096 || h2_settings_header_table_size > 65536) { |
| 3550 | memprintf(err, "'%s' expects a numeric value between 4096 and 65536.", args[0]); |
| 3551 | return -1; |
| 3552 | } |
| 3553 | return 0; |
| 3554 | } |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 3555 | |
Willy Tarreau | e6baec0 | 2017-07-27 11:45:11 +0200 | [diff] [blame] | 3556 | /* config parser for global "tune.h2.initial-window-size" */ |
| 3557 | static int h2_parse_initial_window_size(char **args, int section_type, struct proxy *curpx, |
| 3558 | struct proxy *defpx, const char *file, int line, |
| 3559 | char **err) |
| 3560 | { |
| 3561 | if (too_many_args(1, args, err, NULL)) |
| 3562 | return -1; |
| 3563 | |
| 3564 | h2_settings_initial_window_size = atoi(args[1]); |
| 3565 | if (h2_settings_initial_window_size < 0) { |
| 3566 | memprintf(err, "'%s' expects a positive numeric value.", args[0]); |
| 3567 | return -1; |
| 3568 | } |
| 3569 | return 0; |
| 3570 | } |
| 3571 | |
Willy Tarreau | 5242ef8 | 2017-07-27 11:47:28 +0200 | [diff] [blame] | 3572 | /* config parser for global "tune.h2.max-concurrent-streams" */ |
| 3573 | static int h2_parse_max_concurrent_streams(char **args, int section_type, struct proxy *curpx, |
| 3574 | struct proxy *defpx, const char *file, int line, |
| 3575 | char **err) |
| 3576 | { |
| 3577 | if (too_many_args(1, args, err, NULL)) |
| 3578 | return -1; |
| 3579 | |
| 3580 | h2_settings_max_concurrent_streams = atoi(args[1]); |
| 3581 | if (h2_settings_max_concurrent_streams < 0) { |
| 3582 | memprintf(err, "'%s' expects a positive numeric value.", args[0]); |
| 3583 | return -1; |
| 3584 | } |
| 3585 | return 0; |
| 3586 | } |
| 3587 | |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 3588 | |
| 3589 | /****************************************/ |
| 3590 | /* MUX initialization and instanciation */ |
| 3591 | /***************************************/ |
| 3592 | |
| 3593 | /* The mux operations */ |
| 3594 | const struct mux_ops h2_ops = { |
| 3595 | .init = h2_init, |
| 3596 | .recv = h2_recv, |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 3597 | .wake = h2_wake, |
| 3598 | .update_poll = h2_update_poll, |
| 3599 | .rcv_buf = h2_rcv_buf, |
| 3600 | .snd_buf = h2_snd_buf, |
Olivier Houchard | 6ff2039 | 2018-07-17 18:46:31 +0200 | [diff] [blame] | 3601 | .subscribe = h2_subscribe, |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 3602 | .attach = h2_attach, |
| 3603 | .detach = h2_detach, |
| 3604 | .shutr = h2_shutr, |
| 3605 | .shutw = h2_shutw, |
Willy Tarreau | e3f36cd | 2018-03-30 14:43:13 +0200 | [diff] [blame] | 3606 | .show_fd = h2_show_fd, |
Willy Tarreau | 28f1cb9 | 2017-12-20 16:14:44 +0100 | [diff] [blame] | 3607 | .flags = MX_FL_CLEAN_ABRT, |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 3608 | .name = "H2", |
| 3609 | }; |
| 3610 | |
| 3611 | /* ALPN selection : this mux registers ALPN tolen "h2" */ |
| 3612 | static struct alpn_mux_list alpn_mux_h2 = |
| 3613 | { .token = IST("h2"), .mode = ALPN_MODE_HTTP, .mux = &h2_ops }; |
| 3614 | |
| 3615 | /* config keyword parsers */ |
| 3616 | static struct cfg_kw_list cfg_kws = {ILH, { |
Willy Tarreau | fe20e5b | 2017-07-27 11:42:14 +0200 | [diff] [blame] | 3617 | { CFG_GLOBAL, "tune.h2.header-table-size", h2_parse_header_table_size }, |
Willy Tarreau | e6baec0 | 2017-07-27 11:45:11 +0200 | [diff] [blame] | 3618 | { CFG_GLOBAL, "tune.h2.initial-window-size", h2_parse_initial_window_size }, |
Willy Tarreau | 5242ef8 | 2017-07-27 11:47:28 +0200 | [diff] [blame] | 3619 | { CFG_GLOBAL, "tune.h2.max-concurrent-streams", h2_parse_max_concurrent_streams }, |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 3620 | { 0, NULL, NULL } |
| 3621 | }}; |
| 3622 | |
Willy Tarreau | 5ab6b57 | 2017-09-22 08:05:00 +0200 | [diff] [blame] | 3623 | static void __h2_deinit(void) |
| 3624 | { |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 3625 | pool_destroy(pool_head_h2s); |
| 3626 | pool_destroy(pool_head_h2c); |
Willy Tarreau | 5ab6b57 | 2017-09-22 08:05:00 +0200 | [diff] [blame] | 3627 | } |
| 3628 | |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 3629 | __attribute__((constructor)) |
| 3630 | static void __h2_init(void) |
| 3631 | { |
| 3632 | alpn_register_mux(&alpn_mux_h2); |
| 3633 | cfg_register_keywords(&cfg_kws); |
Willy Tarreau | 5ab6b57 | 2017-09-22 08:05:00 +0200 | [diff] [blame] | 3634 | hap_register_post_deinit(__h2_deinit); |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 3635 | pool_head_h2c = create_pool("h2c", sizeof(struct h2c), MEM_F_SHARED); |
| 3636 | pool_head_h2s = create_pool("h2s", sizeof(struct h2s), MEM_F_SHARED); |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 3637 | } |