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