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