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