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