Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 1 | /* |
| 2 | * HTTP/2 mux-demux for connections |
| 3 | * |
| 4 | * Copyright 2017 Willy Tarreau <w@1wt.eu> |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or |
| 7 | * modify it under the terms of the GNU General Public License |
| 8 | * as published by the Free Software Foundation; either version |
| 9 | * 2 of the License, or (at your option) any later version. |
| 10 | * |
| 11 | */ |
| 12 | |
| 13 | #include <common/cfgparse.h> |
| 14 | #include <common/config.h> |
Willy Tarreau | 5ab6b57 | 2017-09-22 08:05:00 +0200 | [diff] [blame] | 15 | #include <common/h2.h> |
| 16 | #include <common/hpack-tbl.h> |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 17 | #include <proto/connection.h> |
| 18 | #include <proto/stream.h> |
Willy Tarreau | 5ab6b57 | 2017-09-22 08:05:00 +0200 | [diff] [blame] | 19 | #include <eb32tree.h> |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 20 | |
| 21 | |
Willy Tarreau | 5ab6b57 | 2017-09-22 08:05:00 +0200 | [diff] [blame] | 22 | /* the h2c connection pool */ |
| 23 | static struct pool_head *pool2_h2c; |
Willy Tarreau | 1831264 | 2017-10-11 07:57:07 +0200 | [diff] [blame^] | 24 | /* the h2s stream pool */ |
| 25 | static struct pool_head *pool2_h2s; |
Willy Tarreau | 5ab6b57 | 2017-09-22 08:05:00 +0200 | [diff] [blame] | 26 | |
| 27 | /* Connection flags (32 bit), in h2c->flags */ |
| 28 | #define H2_CF_NONE 0x00000000 |
| 29 | |
| 30 | /* H2 connection state, in h2c->st0 */ |
| 31 | enum h2_cs { |
| 32 | H2_CS_PREFACE, // init done, waiting for connection preface |
| 33 | H2_CS_SETTINGS1, // preface OK, waiting for first settings frame |
| 34 | H2_CS_FRAME_H, // first settings frame ok, waiting for frame header |
| 35 | H2_CS_FRAME_P, // frame header OK, waiting for frame payload |
| 36 | H2_CS_FRAME_A, // frame payload OK, trying to send ACK/RST frame |
| 37 | H2_CS_ERROR, // send GOAWAY(errcode) and close the connection ASAP |
| 38 | H2_CS_ERROR2, // GOAWAY(errcode) sent, close the connection ASAP |
| 39 | H2_CS_ENTRIES // must be last |
| 40 | } __attribute__((packed)); |
| 41 | |
| 42 | /* H2 connection descriptor */ |
| 43 | struct h2c { |
| 44 | struct connection *conn; |
| 45 | |
| 46 | enum h2_cs st0; /* mux state */ |
| 47 | enum h2_err errcode; /* H2 err code (H2_ERR_*) */ |
| 48 | |
| 49 | /* 16 bit hole here */ |
| 50 | uint32_t flags; /* connection flags: H2_CF_* */ |
| 51 | int32_t max_id; /* highest ID known on this connection, <0 before preface */ |
| 52 | uint32_t rcvd_c; /* newly received data to ACK for the connection */ |
| 53 | uint32_t rcvd_s; /* newly received data to ACK for the current stream (dsi) */ |
| 54 | |
| 55 | /* states for the demux direction */ |
| 56 | struct hpack_dht *ddht; /* demux dynamic header table */ |
| 57 | struct buffer *dbuf; /* demux buffer */ |
| 58 | |
| 59 | int32_t dsi; /* demux stream ID (<0 = idle) */ |
| 60 | int32_t dfl; /* demux frame length (if dsi >= 0) */ |
| 61 | int8_t dft; /* demux frame type (if dsi >= 0) */ |
| 62 | int8_t dff; /* demux frame flags (if dsi >= 0) */ |
| 63 | /* 16 bit hole here */ |
| 64 | int32_t last_sid; /* last processed stream ID for GOAWAY, <0 before preface */ |
| 65 | |
| 66 | /* states for the mux direction */ |
| 67 | struct buffer *mbuf; /* mux buffer */ |
| 68 | int32_t msi; /* mux stream ID (<0 = idle) */ |
| 69 | int32_t mfl; /* mux frame length (if dsi >= 0) */ |
| 70 | int8_t mft; /* mux frame type (if dsi >= 0) */ |
| 71 | int8_t mff; /* mux frame flags (if dsi >= 0) */ |
| 72 | /* 16 bit hole here */ |
| 73 | int32_t miw; /* mux initial window size for all new streams */ |
| 74 | int32_t mws; /* mux window size. Can be negative. */ |
| 75 | int32_t mfs; /* mux's max frame size */ |
| 76 | |
| 77 | struct eb_root streams_by_id; /* all active streams by their ID */ |
| 78 | struct list send_list; /* list of blocked streams requesting to send */ |
| 79 | struct list fctl_list; /* list of streams blocked by connection's fctl */ |
| 80 | }; |
| 81 | |
Willy Tarreau | 1831264 | 2017-10-11 07:57:07 +0200 | [diff] [blame^] | 82 | /* H2 stream state, in h2s->st */ |
| 83 | enum h2_ss { |
| 84 | H2_SS_IDLE = 0, // idle |
| 85 | H2_SS_RLOC, // reserved(local) |
| 86 | H2_SS_RREM, // reserved(remote) |
| 87 | H2_SS_OPEN, // open |
| 88 | H2_SS_HREM, // half-closed(remote) |
| 89 | H2_SS_HLOC, // half-closed(local) |
| 90 | H2_SS_CLOSED, // closed |
| 91 | H2_SS_ENTRIES // must be last |
| 92 | } __attribute__((packed)); |
| 93 | |
| 94 | /* HTTP/2 stream flags (32 bit), in h2s->flags */ |
| 95 | #define H2_SF_NONE 0x00000000 |
| 96 | #define H2_SF_ES_RCVD 0x00000001 |
| 97 | #define H2_SF_ES_SENT 0x00000002 |
| 98 | |
| 99 | #define H2_SF_RST_RCVD 0x00000004 // received RST_STREAM |
| 100 | #define H2_SF_RST_SENT 0x00000008 // sent RST_STREAM |
| 101 | |
| 102 | /* H2 stream descriptor, describing the stream as it appears in the H2C, and as |
| 103 | * it is being processed in the internal HTTP representation (H1 for now). |
| 104 | */ |
| 105 | struct h2s { |
| 106 | struct conn_stream *cs; |
| 107 | struct h2c *h2c; |
| 108 | struct h1m req, res; /* request and response parser state for H1 */ |
| 109 | struct eb32_node by_id; /* place in h2c's streams_by_id */ |
| 110 | struct list list; /* position in active/blocked lists if blocked>0 */ |
| 111 | int32_t id; /* stream ID */ |
| 112 | uint32_t flags; /* H2_SF_* */ |
| 113 | int mws; /* mux window size for this stream */ |
| 114 | enum h2_err errcode; /* H2 err code (H2_ERR_*) */ |
| 115 | enum h2_ss st; |
| 116 | }; |
Willy Tarreau | 5ab6b57 | 2017-09-22 08:05:00 +0200 | [diff] [blame] | 117 | |
Willy Tarreau | fe20e5b | 2017-07-27 11:42:14 +0200 | [diff] [blame] | 118 | /* a few settings from the global section */ |
| 119 | static int h2_settings_header_table_size = 4096; /* initial value */ |
Willy Tarreau | e6baec0 | 2017-07-27 11:45:11 +0200 | [diff] [blame] | 120 | static int h2_settings_initial_window_size = 65535; /* initial value */ |
Willy Tarreau | 5242ef8 | 2017-07-27 11:47:28 +0200 | [diff] [blame] | 121 | static int h2_settings_max_concurrent_streams = 100; |
Willy Tarreau | fe20e5b | 2017-07-27 11:42:14 +0200 | [diff] [blame] | 122 | |
| 123 | |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 124 | /*****************************************************************/ |
| 125 | /* functions below are dedicated to the mux setup and management */ |
| 126 | /*****************************************************************/ |
| 127 | |
| 128 | /* Initialize the mux once it's attached. For outgoing connections, the context |
| 129 | * is already initialized before installing the mux, so we detect incoming |
| 130 | * connections from the fact that the context is still NULL. Returns < 0 on |
| 131 | * error. |
| 132 | */ |
| 133 | static int h2_init(struct connection *conn) |
| 134 | { |
| 135 | if (conn->mux_ctx) { |
| 136 | /* we don't support outgoing connections for now */ |
| 137 | return -1; |
| 138 | } |
| 139 | |
| 140 | /* not implemented yet */ |
| 141 | return -1; |
| 142 | } |
| 143 | |
| 144 | /* release function for a connection. This one should be called to free all |
| 145 | * resources allocated to the mux. |
| 146 | */ |
| 147 | static void h2_release(struct connection *conn) |
| 148 | { |
| 149 | } |
| 150 | |
| 151 | |
| 152 | /*********************************************************/ |
| 153 | /* functions below are I/O callbacks from the connection */ |
| 154 | /*********************************************************/ |
| 155 | |
| 156 | /* callback called on recv event by the connection handler */ |
| 157 | static void h2_recv(struct connection *conn) |
| 158 | { |
| 159 | } |
| 160 | |
| 161 | /* callback called on send event by the connection handler */ |
| 162 | static void h2_send(struct connection *conn) |
| 163 | { |
| 164 | } |
| 165 | |
| 166 | /* callback called on any event by the connection handler. |
| 167 | * It applies changes and returns zero, or < 0 if it wants immediate |
| 168 | * destruction of the connection (which normally doesn not happen in h2). |
| 169 | */ |
| 170 | static int h2_wake(struct connection *conn) |
| 171 | { |
| 172 | return 0; |
| 173 | } |
| 174 | |
| 175 | /*******************************************/ |
| 176 | /* functions below are used by the streams */ |
| 177 | /*******************************************/ |
| 178 | |
| 179 | /* |
| 180 | * Attach a new stream to a connection |
| 181 | * (Used for outgoing connections) |
| 182 | */ |
| 183 | static struct conn_stream *h2_attach(struct connection *conn) |
| 184 | { |
| 185 | return NULL; |
| 186 | } |
| 187 | |
| 188 | /* callback used to update the mux's polling flags after changing a cs' status. |
| 189 | * The caller (cs_update_mux_polling) will take care of propagating any changes |
| 190 | * to the transport layer. |
| 191 | */ |
| 192 | static void h2_update_poll(struct conn_stream *cs) |
| 193 | { |
| 194 | } |
| 195 | |
| 196 | /* |
| 197 | * Detach the stream from the connection and possibly release the connection. |
| 198 | */ |
| 199 | static void h2_detach(struct conn_stream *cs) |
| 200 | { |
| 201 | } |
| 202 | |
| 203 | static void h2_shutr(struct conn_stream *cs, enum cs_shr_mode mode) |
| 204 | { |
| 205 | } |
| 206 | |
| 207 | static void h2_shutw(struct conn_stream *cs, enum cs_shw_mode mode) |
| 208 | { |
| 209 | } |
| 210 | |
| 211 | /* |
| 212 | * Called from the upper layer, to get more data |
| 213 | */ |
| 214 | static int h2_rcv_buf(struct conn_stream *cs, struct buffer *buf, int count) |
| 215 | { |
| 216 | /* FIXME: not handled for now */ |
| 217 | cs->flags |= CS_FL_ERROR; |
| 218 | return 0; |
| 219 | } |
| 220 | |
| 221 | /* Called from the upper layer, to send data */ |
| 222 | static int h2_snd_buf(struct conn_stream *cs, struct buffer *buf, int flags) |
| 223 | { |
| 224 | /* FIXME: not handled for now */ |
| 225 | cs->flags |= CS_FL_ERROR; |
| 226 | return 0; |
| 227 | } |
| 228 | |
| 229 | |
| 230 | /*******************************************************/ |
| 231 | /* functions below are dedicated to the config parsers */ |
| 232 | /*******************************************************/ |
| 233 | |
Willy Tarreau | fe20e5b | 2017-07-27 11:42:14 +0200 | [diff] [blame] | 234 | /* config parser for global "tune.h2.header-table-size" */ |
| 235 | static int h2_parse_header_table_size(char **args, int section_type, struct proxy *curpx, |
| 236 | struct proxy *defpx, const char *file, int line, |
| 237 | char **err) |
| 238 | { |
| 239 | if (too_many_args(1, args, err, NULL)) |
| 240 | return -1; |
| 241 | |
| 242 | h2_settings_header_table_size = atoi(args[1]); |
| 243 | if (h2_settings_header_table_size < 4096 || h2_settings_header_table_size > 65536) { |
| 244 | memprintf(err, "'%s' expects a numeric value between 4096 and 65536.", args[0]); |
| 245 | return -1; |
| 246 | } |
| 247 | return 0; |
| 248 | } |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 249 | |
Willy Tarreau | e6baec0 | 2017-07-27 11:45:11 +0200 | [diff] [blame] | 250 | /* config parser for global "tune.h2.initial-window-size" */ |
| 251 | static int h2_parse_initial_window_size(char **args, int section_type, struct proxy *curpx, |
| 252 | struct proxy *defpx, const char *file, int line, |
| 253 | char **err) |
| 254 | { |
| 255 | if (too_many_args(1, args, err, NULL)) |
| 256 | return -1; |
| 257 | |
| 258 | h2_settings_initial_window_size = atoi(args[1]); |
| 259 | if (h2_settings_initial_window_size < 0) { |
| 260 | memprintf(err, "'%s' expects a positive numeric value.", args[0]); |
| 261 | return -1; |
| 262 | } |
| 263 | return 0; |
| 264 | } |
| 265 | |
Willy Tarreau | 5242ef8 | 2017-07-27 11:47:28 +0200 | [diff] [blame] | 266 | /* config parser for global "tune.h2.max-concurrent-streams" */ |
| 267 | static int h2_parse_max_concurrent_streams(char **args, int section_type, struct proxy *curpx, |
| 268 | struct proxy *defpx, const char *file, int line, |
| 269 | char **err) |
| 270 | { |
| 271 | if (too_many_args(1, args, err, NULL)) |
| 272 | return -1; |
| 273 | |
| 274 | h2_settings_max_concurrent_streams = atoi(args[1]); |
| 275 | if (h2_settings_max_concurrent_streams < 0) { |
| 276 | memprintf(err, "'%s' expects a positive numeric value.", args[0]); |
| 277 | return -1; |
| 278 | } |
| 279 | return 0; |
| 280 | } |
| 281 | |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 282 | |
| 283 | /****************************************/ |
| 284 | /* MUX initialization and instanciation */ |
| 285 | /***************************************/ |
| 286 | |
| 287 | /* The mux operations */ |
| 288 | const struct mux_ops h2_ops = { |
| 289 | .init = h2_init, |
| 290 | .recv = h2_recv, |
| 291 | .send = h2_send, |
| 292 | .wake = h2_wake, |
| 293 | .update_poll = h2_update_poll, |
| 294 | .rcv_buf = h2_rcv_buf, |
| 295 | .snd_buf = h2_snd_buf, |
| 296 | .attach = h2_attach, |
| 297 | .detach = h2_detach, |
| 298 | .shutr = h2_shutr, |
| 299 | .shutw = h2_shutw, |
| 300 | .release = h2_release, |
| 301 | .name = "H2", |
| 302 | }; |
| 303 | |
| 304 | /* ALPN selection : this mux registers ALPN tolen "h2" */ |
| 305 | static struct alpn_mux_list alpn_mux_h2 = |
| 306 | { .token = IST("h2"), .mode = ALPN_MODE_HTTP, .mux = &h2_ops }; |
| 307 | |
| 308 | /* config keyword parsers */ |
| 309 | static struct cfg_kw_list cfg_kws = {ILH, { |
Willy Tarreau | fe20e5b | 2017-07-27 11:42:14 +0200 | [diff] [blame] | 310 | { CFG_GLOBAL, "tune.h2.header-table-size", h2_parse_header_table_size }, |
Willy Tarreau | e6baec0 | 2017-07-27 11:45:11 +0200 | [diff] [blame] | 311 | { CFG_GLOBAL, "tune.h2.initial-window-size", h2_parse_initial_window_size }, |
Willy Tarreau | 5242ef8 | 2017-07-27 11:47:28 +0200 | [diff] [blame] | 312 | { CFG_GLOBAL, "tune.h2.max-concurrent-streams", h2_parse_max_concurrent_streams }, |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 313 | { 0, NULL, NULL } |
| 314 | }}; |
| 315 | |
Willy Tarreau | 5ab6b57 | 2017-09-22 08:05:00 +0200 | [diff] [blame] | 316 | static void __h2_deinit(void) |
| 317 | { |
Willy Tarreau | 1831264 | 2017-10-11 07:57:07 +0200 | [diff] [blame^] | 318 | pool_destroy2(pool2_h2s); |
Willy Tarreau | 5ab6b57 | 2017-09-22 08:05:00 +0200 | [diff] [blame] | 319 | pool_destroy2(pool2_h2c); |
| 320 | } |
| 321 | |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 322 | __attribute__((constructor)) |
| 323 | static void __h2_init(void) |
| 324 | { |
| 325 | alpn_register_mux(&alpn_mux_h2); |
| 326 | cfg_register_keywords(&cfg_kws); |
Willy Tarreau | 5ab6b57 | 2017-09-22 08:05:00 +0200 | [diff] [blame] | 327 | hap_register_post_deinit(__h2_deinit); |
| 328 | pool2_h2c = create_pool("h2c", sizeof(struct h2c), MEM_F_SHARED); |
Willy Tarreau | 1831264 | 2017-10-11 07:57:07 +0200 | [diff] [blame^] | 329 | pool2_h2s = create_pool("h2s", sizeof(struct h2s), MEM_F_SHARED); |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 330 | } |