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