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