Frédéric Lécaille | 70da889 | 2020-11-06 15:49:49 +0100 | [diff] [blame] | 1 | /* |
| 2 | * QUIC socket management. |
| 3 | * |
Willy Tarreau | 3dfb7da | 2022-03-02 22:33:39 +0100 | [diff] [blame] | 4 | * Copyright 2020 HAProxy Technologies, Frederic Lecaille <flecaille@haproxy.com> |
Frédéric Lécaille | 70da889 | 2020-11-06 15:49:49 +0100 | [diff] [blame] | 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 <errno.h> |
| 14 | |
| 15 | #include <sys/socket.h> |
| 16 | #include <sys/types.h> |
| 17 | |
| 18 | #include <haproxy/connection.h> |
| 19 | #include <haproxy/listener.h> |
Frédéric Lécaille | 6492e66 | 2022-05-17 17:23:16 +0200 | [diff] [blame] | 20 | #include <haproxy/proto_quic.h> |
Amaury Denoyelle | 4d29504 | 2022-01-19 16:18:44 +0100 | [diff] [blame] | 21 | #include <haproxy/quic_sock.h> |
Amaury Denoyelle | eb01f59 | 2021-10-07 16:44:05 +0200 | [diff] [blame] | 22 | #include <haproxy/session.h> |
Amaury Denoyelle | 777969c | 2022-03-24 16:06:26 +0100 | [diff] [blame] | 23 | #include <haproxy/tools.h> |
Frédéric Lécaille | 026a792 | 2020-11-23 15:46:36 +0100 | [diff] [blame] | 24 | #include <haproxy/xprt_quic.h> |
| 25 | |
| 26 | /* This function is called from the protocol layer accept() in order to |
| 27 | * instantiate a new session on behalf of a given listener and frontend. It |
| 28 | * returns a positive value upon success, 0 if the connection can be ignored, |
| 29 | * or a negative value upon critical failure. The accepted connection is |
| 30 | * closed if we return <= 0. If no handshake is needed, it immediately tries |
| 31 | * to instantiate a new stream. The connection must already have been filled |
| 32 | * with the incoming connection handle (a fd), a target (the listener) and a |
| 33 | * source address. |
| 34 | */ |
| 35 | int quic_session_accept(struct connection *cli_conn) |
| 36 | { |
| 37 | struct listener *l = __objt_listener(cli_conn->target); |
| 38 | struct proxy *p = l->bind_conf->frontend; |
| 39 | struct session *sess; |
| 40 | |
| 41 | cli_conn->proxy_netns = l->rx.settings->netns; |
Frédéric Lécaille | 026a792 | 2020-11-23 15:46:36 +0100 | [diff] [blame] | 42 | /* This flag is ordinarily set by conn_ctrl_init() which cannot |
| 43 | * be called for now. |
| 44 | */ |
| 45 | cli_conn->flags |= CO_FL_CTRL_READY; |
| 46 | |
| 47 | /* wait for a PROXY protocol header */ |
| 48 | if (l->options & LI_O_ACC_PROXY) |
| 49 | cli_conn->flags |= CO_FL_ACCEPT_PROXY; |
| 50 | |
| 51 | /* wait for a NetScaler client IP insertion protocol header */ |
| 52 | if (l->options & LI_O_ACC_CIP) |
| 53 | cli_conn->flags |= CO_FL_ACCEPT_CIP; |
| 54 | |
Frédéric Lécaille | 026a792 | 2020-11-23 15:46:36 +0100 | [diff] [blame] | 55 | /* Add the handshake pseudo-XPRT */ |
| 56 | if (cli_conn->flags & (CO_FL_ACCEPT_PROXY | CO_FL_ACCEPT_CIP)) { |
| 57 | if (xprt_add_hs(cli_conn) != 0) |
| 58 | goto out_free_conn; |
| 59 | } |
Olivier Houchard | 1b3c931 | 2021-03-05 23:37:48 +0100 | [diff] [blame] | 60 | |
Frédéric Lécaille | 026a792 | 2020-11-23 15:46:36 +0100 | [diff] [blame] | 61 | sess = session_new(p, l, &cli_conn->obj_type); |
| 62 | if (!sess) |
| 63 | goto out_free_conn; |
| 64 | |
| 65 | conn_set_owner(cli_conn, sess, NULL); |
| 66 | |
Frédéric Lécaille | ecb5872 | 2021-05-27 17:12:36 +0200 | [diff] [blame] | 67 | if (conn_complete_session(cli_conn) < 0) |
| 68 | goto out_free_sess; |
| 69 | |
Amaury Denoyelle | 622ec41 | 2022-04-13 16:58:26 +0200 | [diff] [blame] | 70 | if (conn_xprt_start(cli_conn) < 0) { |
| 71 | /* conn_complete_session has succeeded : conn is the owner of |
| 72 | * the session and the MUX is initialized. |
| 73 | * Let the MUX free all resources on error. |
| 74 | */ |
| 75 | cli_conn->mux->destroy(cli_conn->ctx); |
| 76 | return -1; |
| 77 | } |
| 78 | |
| 79 | return 1; |
Frédéric Lécaille | 27faba7 | 2021-03-03 16:21:00 +0100 | [diff] [blame] | 80 | |
Frédéric Lécaille | 026a792 | 2020-11-23 15:46:36 +0100 | [diff] [blame] | 81 | out_free_sess: |
| 82 | /* prevent call to listener_release during session_free. It will be |
| 83 | * done below, for all errors. */ |
| 84 | sess->listener = NULL; |
| 85 | session_free(sess); |
| 86 | out_free_conn: |
Willy Tarreau | 784b868 | 2022-04-11 14:18:10 +0200 | [diff] [blame] | 87 | cli_conn->handle.qc->conn = NULL; |
Frédéric Lécaille | 026a792 | 2020-11-23 15:46:36 +0100 | [diff] [blame] | 88 | conn_stop_tracking(cli_conn); |
| 89 | conn_xprt_close(cli_conn); |
| 90 | conn_free(cli_conn); |
| 91 | out: |
| 92 | |
Frédéric Lécaille | e8139f3 | 2021-03-11 17:06:30 +0100 | [diff] [blame] | 93 | return -1; |
Frédéric Lécaille | 026a792 | 2020-11-23 15:46:36 +0100 | [diff] [blame] | 94 | } |
| 95 | |
Willy Tarreau | cdf7c8e | 2022-04-11 16:20:00 +0200 | [diff] [blame] | 96 | /* Retrieve a connection's source address. Returns -1 on failure. */ |
| 97 | int quic_sock_get_src(struct connection *conn, struct sockaddr *addr, socklen_t len) |
| 98 | { |
| 99 | struct quic_conn *qc; |
| 100 | |
Willy Tarreau | 784b868 | 2022-04-11 14:18:10 +0200 | [diff] [blame] | 101 | if (!conn || !conn->handle.qc) |
Willy Tarreau | cdf7c8e | 2022-04-11 16:20:00 +0200 | [diff] [blame] | 102 | return -1; |
| 103 | |
Willy Tarreau | 784b868 | 2022-04-11 14:18:10 +0200 | [diff] [blame] | 104 | qc = conn->handle.qc; |
Willy Tarreau | cdf7c8e | 2022-04-11 16:20:00 +0200 | [diff] [blame] | 105 | if (conn_is_back(conn)) { |
| 106 | /* no source address defined for outgoing connections for now */ |
| 107 | return -1; |
| 108 | } else { |
| 109 | /* front connection, return the peer's address */ |
| 110 | if (len > sizeof(qc->peer_addr)) |
| 111 | len = sizeof(qc->peer_addr); |
| 112 | memcpy(addr, &qc->peer_addr, len); |
| 113 | return 0; |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | /* Retrieve a connection's destination address. Returns -1 on failure. */ |
| 118 | int quic_sock_get_dst(struct connection *conn, struct sockaddr *addr, socklen_t len) |
| 119 | { |
| 120 | struct quic_conn *qc; |
| 121 | |
Willy Tarreau | 784b868 | 2022-04-11 14:18:10 +0200 | [diff] [blame] | 122 | if (!conn || !conn->handle.qc) |
Willy Tarreau | cdf7c8e | 2022-04-11 16:20:00 +0200 | [diff] [blame] | 123 | return -1; |
| 124 | |
Willy Tarreau | 784b868 | 2022-04-11 14:18:10 +0200 | [diff] [blame] | 125 | qc = conn->handle.qc; |
Willy Tarreau | cdf7c8e | 2022-04-11 16:20:00 +0200 | [diff] [blame] | 126 | if (conn_is_back(conn)) { |
| 127 | /* back connection, return the peer's address */ |
| 128 | if (len > sizeof(qc->peer_addr)) |
| 129 | len = sizeof(qc->peer_addr); |
| 130 | memcpy(addr, &qc->peer_addr, len); |
| 131 | } else { |
| 132 | /* FIXME: front connection, no local address for now, we'll |
| 133 | * return the listener's address instead. |
| 134 | */ |
| 135 | BUG_ON(!qc->li); |
| 136 | |
| 137 | if (len > sizeof(qc->li->rx.addr)) |
| 138 | len = sizeof(qc->li->rx.addr); |
| 139 | memcpy(addr, &qc->li->rx.addr, len); |
| 140 | } |
| 141 | return 0; |
| 142 | } |
| 143 | |
Frédéric Lécaille | 026a792 | 2020-11-23 15:46:36 +0100 | [diff] [blame] | 144 | /* |
| 145 | * Inspired from session_accept_fd(). |
| 146 | * Instantiate a new connection (connection struct) to be attached to <qc> |
| 147 | * QUIC connection of <l> listener. |
| 148 | * Returns 1 if succeeded, 0 if not. |
| 149 | */ |
| 150 | static int new_quic_cli_conn(struct quic_conn *qc, struct listener *l, |
| 151 | struct sockaddr_storage *saddr) |
| 152 | { |
| 153 | struct connection *cli_conn; |
Frédéric Lécaille | 026a792 | 2020-11-23 15:46:36 +0100 | [diff] [blame] | 154 | |
Frédéric Lécaille | 026a792 | 2020-11-23 15:46:36 +0100 | [diff] [blame] | 155 | if (unlikely((cli_conn = conn_new(&l->obj_type)) == NULL)) |
| 156 | goto out; |
| 157 | |
Willy Tarreau | 9cc88c3 | 2022-04-08 14:34:31 +0200 | [diff] [blame] | 158 | if (!sockaddr_alloc(&cli_conn->src, saddr, sizeof *saddr)) |
Frédéric Lécaille | 026a792 | 2020-11-23 15:46:36 +0100 | [diff] [blame] | 159 | goto out_free_conn; |
| 160 | |
Willy Tarreau | 030b3e6 | 2022-05-02 17:47:46 +0200 | [diff] [blame] | 161 | cli_conn->flags |= CO_FL_FDLESS; |
Frédéric Lécaille | 026a792 | 2020-11-23 15:46:36 +0100 | [diff] [blame] | 162 | qc->conn = cli_conn; |
Willy Tarreau | 784b868 | 2022-04-11 14:18:10 +0200 | [diff] [blame] | 163 | cli_conn->handle.qc = qc; |
Frédéric Lécaille | 026a792 | 2020-11-23 15:46:36 +0100 | [diff] [blame] | 164 | |
Frédéric Lécaille | 026a792 | 2020-11-23 15:46:36 +0100 | [diff] [blame] | 165 | cli_conn->target = &l->obj_type; |
| 166 | |
Frédéric Lécaille | 01ab661 | 2021-06-14 10:31:43 +0200 | [diff] [blame] | 167 | /* We need the xprt context before accepting (->accept()) the connection: |
| 168 | * we may receive packet before this connection acception. |
| 169 | */ |
| 170 | if (conn_prepare(cli_conn, l->rx.proto, l->bind_conf->xprt) < 0) |
| 171 | goto out_free_conn; |
Frédéric Lécaille | 026a792 | 2020-11-23 15:46:36 +0100 | [diff] [blame] | 172 | |
| 173 | return 1; |
| 174 | |
| 175 | out_free_conn: |
Frédéric Lécaille | 01ab661 | 2021-06-14 10:31:43 +0200 | [diff] [blame] | 176 | qc->conn = NULL; |
Frédéric Lécaille | 026a792 | 2020-11-23 15:46:36 +0100 | [diff] [blame] | 177 | conn_stop_tracking(cli_conn); |
| 178 | conn_xprt_close(cli_conn); |
| 179 | conn_free(cli_conn); |
Frédéric Lécaille | 026a792 | 2020-11-23 15:46:36 +0100 | [diff] [blame] | 180 | out: |
| 181 | |
| 182 | return 0; |
| 183 | } |
Frédéric Lécaille | 70da889 | 2020-11-06 15:49:49 +0100 | [diff] [blame] | 184 | |
| 185 | /* Tests if the receiver supports accepting connections. Returns positive on |
| 186 | * success, 0 if not possible |
| 187 | */ |
| 188 | int quic_sock_accepting_conn(const struct receiver *rx) |
| 189 | { |
| 190 | return 1; |
| 191 | } |
| 192 | |
| 193 | /* Accept an incoming connection from listener <l>, and return it, as well as |
| 194 | * a CO_AC_* status code into <status> if not null. Null is returned on error. |
| 195 | * <l> must be a valid listener with a valid frontend. |
| 196 | */ |
| 197 | struct connection *quic_sock_accept_conn(struct listener *l, int *status) |
| 198 | { |
Frédéric Lécaille | 026a792 | 2020-11-23 15:46:36 +0100 | [diff] [blame] | 199 | struct quic_conn *qc; |
Amaury Denoyelle | cfa2d56 | 2022-01-19 16:01:05 +0100 | [diff] [blame] | 200 | struct li_per_thread *lthr = &l->per_thr[tid]; |
Frédéric Lécaille | 026a792 | 2020-11-23 15:46:36 +0100 | [diff] [blame] | 201 | |
Amaury Denoyelle | cfa2d56 | 2022-01-19 16:01:05 +0100 | [diff] [blame] | 202 | qc = MT_LIST_POP(<hr->quic_accept.conns, struct quic_conn *, accept_list); |
| 203 | if (!qc) |
| 204 | goto done; |
Frédéric Lécaille | 026a792 | 2020-11-23 15:46:36 +0100 | [diff] [blame] | 205 | |
Amaury Denoyelle | cfa2d56 | 2022-01-19 16:01:05 +0100 | [diff] [blame] | 206 | if (!new_quic_cli_conn(qc, l, &qc->peer_addr)) |
Frédéric Lécaille | 026a792 | 2020-11-23 15:46:36 +0100 | [diff] [blame] | 207 | goto err; |
| 208 | |
Frédéric Lécaille | 026a792 | 2020-11-23 15:46:36 +0100 | [diff] [blame] | 209 | done: |
Amaury Denoyelle | cfa2d56 | 2022-01-19 16:01:05 +0100 | [diff] [blame] | 210 | *status = CO_AC_DONE; |
Frédéric Lécaille | 026a792 | 2020-11-23 15:46:36 +0100 | [diff] [blame] | 211 | return qc ? qc->conn : NULL; |
| 212 | |
| 213 | err: |
Amaury Denoyelle | cfa2d56 | 2022-01-19 16:01:05 +0100 | [diff] [blame] | 214 | /* in case of error reinsert the element to process it later. */ |
| 215 | MT_LIST_INSERT(<hr->quic_accept.conns, &qc->accept_list); |
| 216 | |
| 217 | *status = CO_AC_PAUSE; |
| 218 | return NULL; |
Frédéric Lécaille | 70da889 | 2020-11-06 15:49:49 +0100 | [diff] [blame] | 219 | } |
| 220 | |
Frédéric Lécaille | 6492e66 | 2022-05-17 17:23:16 +0200 | [diff] [blame] | 221 | /* Retrieve the DCID from the datagram found in <buf> and deliver it to the |
| 222 | * correct datagram handler. |
| 223 | * Return 1 if a correct datagram could be found, 0 if not. |
| 224 | */ |
| 225 | static int quic_lstnr_dgram_dispatch(unsigned char *buf, size_t len, void *owner, |
| 226 | struct sockaddr_storage *saddr, |
| 227 | struct quic_dgram *new_dgram, struct list *dgrams) |
| 228 | { |
| 229 | struct quic_dgram *dgram; |
| 230 | unsigned char *dcid; |
| 231 | size_t dcid_len; |
| 232 | int cid_tid; |
| 233 | |
| 234 | if (!len || !quic_get_dgram_dcid(buf, buf + len, &dcid, &dcid_len)) |
| 235 | goto err; |
| 236 | |
| 237 | dgram = new_dgram ? new_dgram : pool_alloc(pool_head_quic_dgram); |
| 238 | if (!dgram) |
| 239 | goto err; |
| 240 | |
| 241 | cid_tid = quic_get_cid_tid(dcid); |
| 242 | |
| 243 | /* All the members must be initialized! */ |
| 244 | dgram->owner = owner; |
| 245 | dgram->buf = buf; |
| 246 | dgram->len = len; |
| 247 | dgram->dcid = dcid; |
| 248 | dgram->dcid_len = dcid_len; |
| 249 | dgram->saddr = *saddr; |
| 250 | dgram->qc = NULL; |
| 251 | LIST_APPEND(dgrams, &dgram->list); |
| 252 | MT_LIST_APPEND(&quic_dghdlrs[cid_tid].dgrams, &dgram->mt_list); |
| 253 | |
Willy Tarreau | f9d4a7d | 2022-08-05 08:45:56 +0200 | [diff] [blame] | 254 | /* typically quic_lstnr_dghdlr() */ |
Frédéric Lécaille | 6492e66 | 2022-05-17 17:23:16 +0200 | [diff] [blame] | 255 | tasklet_wakeup(quic_dghdlrs[cid_tid].task); |
| 256 | |
| 257 | return 1; |
| 258 | |
| 259 | err: |
Frédéric Lécaille | bfb077a | 2022-08-12 11:55:20 +0200 | [diff] [blame] | 260 | pool_free(pool_head_quic_dgram, new_dgram); |
Frédéric Lécaille | 6492e66 | 2022-05-17 17:23:16 +0200 | [diff] [blame] | 261 | return 0; |
| 262 | } |
| 263 | |
Frédéric Lécaille | 70da889 | 2020-11-06 15:49:49 +0100 | [diff] [blame] | 264 | /* Function called on a read event from a listening socket. It tries |
| 265 | * to handle as many connections as possible. |
| 266 | */ |
| 267 | void quic_sock_fd_iocb(int fd) |
| 268 | { |
| 269 | ssize_t ret; |
Frédéric Lécaille | 324ecda | 2021-11-02 10:14:44 +0100 | [diff] [blame] | 270 | struct rxbuf *rxbuf; |
Frédéric Lécaille | 70da889 | 2020-11-06 15:49:49 +0100 | [diff] [blame] | 271 | struct buffer *buf; |
| 272 | struct listener *l = objt_listener(fdtab[fd].owner); |
Frédéric Lécaille | c4becf5 | 2021-11-08 11:23:17 +0100 | [diff] [blame] | 273 | struct quic_transport_params *params; |
Frédéric Lécaille | 70da889 | 2020-11-06 15:49:49 +0100 | [diff] [blame] | 274 | /* Source address */ |
| 275 | struct sockaddr_storage saddr = {0}; |
Frédéric Lécaille | 320744b | 2022-01-27 12:19:28 +0100 | [diff] [blame] | 276 | size_t max_sz, cspace; |
Frédéric Lécaille | 70da889 | 2020-11-06 15:49:49 +0100 | [diff] [blame] | 277 | socklen_t saddrlen; |
Frédéric Lécaille | 2bed1f1 | 2022-06-23 21:05:05 +0200 | [diff] [blame] | 278 | struct quic_dgram *new_dgram; |
Frédéric Lécaille | f6f7520 | 2022-02-02 09:44:22 +0100 | [diff] [blame] | 279 | unsigned char *dgram_buf; |
Frédéric Lécaille | 1b0707f | 2022-06-30 11:28:56 +0200 | [diff] [blame] | 280 | int max_dgrams; |
Frédéric Lécaille | 70da889 | 2020-11-06 15:49:49 +0100 | [diff] [blame] | 281 | |
Tim Duesterhus | 1655424 | 2021-09-15 13:58:49 +0200 | [diff] [blame] | 282 | BUG_ON(!l); |
Frédéric Lécaille | 70da889 | 2020-11-06 15:49:49 +0100 | [diff] [blame] | 283 | |
Frédéric Lécaille | 19ef636 | 2022-06-23 18:00:37 +0200 | [diff] [blame] | 284 | new_dgram = NULL; |
Frédéric Lécaille | c4becf5 | 2021-11-08 11:23:17 +0100 | [diff] [blame] | 285 | if (!l) |
| 286 | return; |
| 287 | |
Willy Tarreau | f509065 | 2021-04-06 17:23:40 +0200 | [diff] [blame] | 288 | if (!(fdtab[fd].state & FD_POLL_IN) || !fd_recv_ready(fd)) |
Frédéric Lécaille | 70da889 | 2020-11-06 15:49:49 +0100 | [diff] [blame] | 289 | return; |
| 290 | |
Frédéric Lécaille | 324ecda | 2021-11-02 10:14:44 +0100 | [diff] [blame] | 291 | rxbuf = MT_LIST_POP(&l->rx.rxbuf_list, typeof(rxbuf), mt_list); |
Amaury Denoyelle | ee72a43 | 2021-11-19 15:49:29 +0100 | [diff] [blame] | 292 | if (!rxbuf) |
Frédéric Lécaille | c4becf5 | 2021-11-08 11:23:17 +0100 | [diff] [blame] | 293 | goto out; |
Frédéric Lécaille | 37ae505 | 2022-01-27 11:31:50 +0100 | [diff] [blame] | 294 | |
Amaury Denoyelle | ee72a43 | 2021-11-19 15:49:29 +0100 | [diff] [blame] | 295 | buf = &rxbuf->buf; |
Frédéric Lécaille | c4becf5 | 2021-11-08 11:23:17 +0100 | [diff] [blame] | 296 | |
Frédéric Lécaille | 1b0707f | 2022-06-30 11:28:56 +0200 | [diff] [blame] | 297 | max_dgrams = global.tune.maxpollevents; |
| 298 | start: |
Ilya Shipitsin | 3b64a28 | 2022-07-29 22:26:53 +0500 | [diff] [blame] | 299 | /* Try to reuse an existing dgram. Note that there is always at |
Frédéric Lécaille | 2bed1f1 | 2022-06-23 21:05:05 +0200 | [diff] [blame] | 300 | * least one datagram to pick, except the first time we enter |
| 301 | * this function for this <rxbuf> buffer. |
| 302 | */ |
| 303 | if (!LIST_ISEMPTY(&rxbuf->dgrams)) { |
| 304 | struct quic_dgram *dg = |
| 305 | LIST_ELEM(rxbuf->dgrams.n, struct quic_dgram *, list); |
Frédéric Lécaille | 37ae505 | 2022-01-27 11:31:50 +0100 | [diff] [blame] | 306 | |
Frédéric Lécaille | 2bed1f1 | 2022-06-23 21:05:05 +0200 | [diff] [blame] | 307 | if (!dg->buf) { |
| 308 | LIST_DELETE(&dg->list); |
| 309 | b_del(buf, dg->len); |
| 310 | new_dgram = dg; |
| 311 | } |
Frédéric Lécaille | 37ae505 | 2022-01-27 11:31:50 +0100 | [diff] [blame] | 312 | } |
| 313 | |
Frédéric Lécaille | c4becf5 | 2021-11-08 11:23:17 +0100 | [diff] [blame] | 314 | params = &l->bind_conf->quic_params; |
Frédéric Lécaille | 324ecda | 2021-11-02 10:14:44 +0100 | [diff] [blame] | 315 | max_sz = params->max_udp_payload_size; |
Frédéric Lécaille | 320744b | 2022-01-27 12:19:28 +0100 | [diff] [blame] | 316 | cspace = b_contig_space(buf); |
| 317 | if (cspace < max_sz) { |
Frédéric Lécaille | 1712b1d | 2022-01-28 13:10:24 +0100 | [diff] [blame] | 318 | struct quic_dgram *dgram; |
| 319 | |
Frédéric Lécaille | 0c53568 | 2022-06-23 17:47:10 +0200 | [diff] [blame] | 320 | /* Do no mark <buf> as full, and do not try to consume it |
Frédéric Lécaille | ba19acd | 2022-08-08 21:10:58 +0200 | [diff] [blame] | 321 | * if the contiguous remaining space is not at the end |
Frédéric Lécaille | 0c53568 | 2022-06-23 17:47:10 +0200 | [diff] [blame] | 322 | */ |
| 323 | if (b_tail(buf) + cspace < b_wrap(buf)) |
| 324 | goto out; |
| 325 | |
Frédéric Lécaille | 1712b1d | 2022-01-28 13:10:24 +0100 | [diff] [blame] | 326 | /* Allocate a fake datagram, without data to locate |
| 327 | * the end of the RX buffer (required during purging). |
| 328 | */ |
Frédéric Lécaille | ba19acd | 2022-08-08 21:10:58 +0200 | [diff] [blame] | 329 | dgram = pool_alloc(pool_head_quic_dgram); |
Frédéric Lécaille | 1712b1d | 2022-01-28 13:10:24 +0100 | [diff] [blame] | 330 | if (!dgram) |
| 331 | goto out; |
| 332 | |
Frédéric Lécaille | ba19acd | 2022-08-08 21:10:58 +0200 | [diff] [blame] | 333 | /* Initialize only the useful members of this fake datagram. */ |
| 334 | dgram->buf = NULL; |
Frédéric Lécaille | 1712b1d | 2022-01-28 13:10:24 +0100 | [diff] [blame] | 335 | dgram->len = cspace; |
Frédéric Lécaille | ba19acd | 2022-08-08 21:10:58 +0200 | [diff] [blame] | 336 | /* Append this datagram only to the RX buffer list. It will |
| 337 | * not be treated by any datagram handler. |
| 338 | */ |
Frédéric Lécaille | 1712b1d | 2022-01-28 13:10:24 +0100 | [diff] [blame] | 339 | LIST_APPEND(&rxbuf->dgrams, &dgram->list); |
Frédéric Lécaille | 0c53568 | 2022-06-23 17:47:10 +0200 | [diff] [blame] | 340 | |
Frédéric Lécaille | 320744b | 2022-01-27 12:19:28 +0100 | [diff] [blame] | 341 | /* Consume the remaining space */ |
| 342 | b_add(buf, cspace); |
Frédéric Lécaille | 324ecda | 2021-11-02 10:14:44 +0100 | [diff] [blame] | 343 | if (b_contig_space(buf) < max_sz) |
| 344 | goto out; |
| 345 | } |
| 346 | |
Frédéric Lécaille | f6f7520 | 2022-02-02 09:44:22 +0100 | [diff] [blame] | 347 | dgram_buf = (unsigned char *)b_tail(buf); |
Frédéric Lécaille | 70da889 | 2020-11-06 15:49:49 +0100 | [diff] [blame] | 348 | saddrlen = sizeof saddr; |
| 349 | do { |
Frédéric Lécaille | f6f7520 | 2022-02-02 09:44:22 +0100 | [diff] [blame] | 350 | ret = recvfrom(fd, dgram_buf, max_sz, 0, |
Frédéric Lécaille | 70da889 | 2020-11-06 15:49:49 +0100 | [diff] [blame] | 351 | (struct sockaddr *)&saddr, &saddrlen); |
Willy Tarreau | acef5e2 | 2022-04-25 20:32:15 +0200 | [diff] [blame] | 352 | if (ret < 0 && (errno == EAGAIN || errno == EWOULDBLOCK)) { |
Frédéric Lécaille | 439c464 | 2022-02-02 14:33:10 +0100 | [diff] [blame] | 353 | fd_cant_recv(fd); |
Frédéric Lécaille | 324ecda | 2021-11-02 10:14:44 +0100 | [diff] [blame] | 354 | goto out; |
Frédéric Lécaille | 70da889 | 2020-11-06 15:49:49 +0100 | [diff] [blame] | 355 | } |
Frédéric Lécaille | 439c464 | 2022-02-02 14:33:10 +0100 | [diff] [blame] | 356 | } while (ret < 0 && errno == EINTR); |
Frédéric Lécaille | 70da889 | 2020-11-06 15:49:49 +0100 | [diff] [blame] | 357 | |
Frédéric Lécaille | 324ecda | 2021-11-02 10:14:44 +0100 | [diff] [blame] | 358 | b_add(buf, ret); |
Frédéric Lécaille | f6f7520 | 2022-02-02 09:44:22 +0100 | [diff] [blame] | 359 | if (!quic_lstnr_dgram_dispatch(dgram_buf, ret, l, &saddr, |
| 360 | new_dgram, &rxbuf->dgrams)) { |
Frédéric Lécaille | 37ae505 | 2022-01-27 11:31:50 +0100 | [diff] [blame] | 361 | /* If wrong, consume this datagram */ |
| 362 | b_del(buf, ret); |
| 363 | } |
Frédéric Lécaille | 19ef636 | 2022-06-23 18:00:37 +0200 | [diff] [blame] | 364 | new_dgram = NULL; |
Frédéric Lécaille | 1b0707f | 2022-06-30 11:28:56 +0200 | [diff] [blame] | 365 | if (--max_dgrams > 0) |
| 366 | goto start; |
Frédéric Lécaille | 324ecda | 2021-11-02 10:14:44 +0100 | [diff] [blame] | 367 | out: |
Frédéric Lécaille | 19ef636 | 2022-06-23 18:00:37 +0200 | [diff] [blame] | 368 | pool_free(pool_head_quic_dgram, new_dgram); |
Frédéric Lécaille | 324ecda | 2021-11-02 10:14:44 +0100 | [diff] [blame] | 369 | MT_LIST_APPEND(&l->rx.rxbuf_list, &rxbuf->mt_list); |
Frédéric Lécaille | 70da889 | 2020-11-06 15:49:49 +0100 | [diff] [blame] | 370 | } |
Amaury Denoyelle | 2ce99fe | 2022-01-19 15:46:11 +0100 | [diff] [blame] | 371 | |
Frédéric Lécaille | 48bb875 | 2022-08-03 20:52:20 +0200 | [diff] [blame] | 372 | /* Send a datagram stored into <buf> buffer with <sz> as size. |
| 373 | * The caller must ensure there is at least <sz> bytes in this buffer. |
Amaury Denoyelle | 6715cbf | 2022-08-05 11:56:36 +0200 | [diff] [blame] | 374 | * |
| 375 | * Returns 0 on success else non-zero. |
| 376 | * |
Frédéric Lécaille | 48bb875 | 2022-08-03 20:52:20 +0200 | [diff] [blame] | 377 | * TODO standardize this function for a generic UDP sendto wrapper. This can be |
Amaury Denoyelle | 58a7704 | 2022-02-09 15:43:07 +0100 | [diff] [blame] | 378 | * done by removing the <qc> arg and replace it with address/port. |
| 379 | */ |
Amaury Denoyelle | 6715cbf | 2022-08-05 11:56:36 +0200 | [diff] [blame] | 380 | int qc_snd_buf(struct quic_conn *qc, const struct buffer *buf, size_t sz, |
| 381 | int flags) |
Amaury Denoyelle | 58a7704 | 2022-02-09 15:43:07 +0100 | [diff] [blame] | 382 | { |
| 383 | ssize_t ret; |
Amaury Denoyelle | 58a7704 | 2022-02-09 15:43:07 +0100 | [diff] [blame] | 384 | |
Frédéric Lécaille | 48bb875 | 2022-08-03 20:52:20 +0200 | [diff] [blame] | 385 | do { |
| 386 | ret = sendto(qc->li->rx.fd, b_peek(buf, b_head_ofs(buf)), sz, |
| 387 | MSG_DONTWAIT | MSG_NOSIGNAL, |
Amaury Denoyelle | 58a7704 | 2022-02-09 15:43:07 +0100 | [diff] [blame] | 388 | (struct sockaddr *)&qc->peer_addr, get_addr_len(&qc->peer_addr)); |
Frédéric Lécaille | 48bb875 | 2022-08-03 20:52:20 +0200 | [diff] [blame] | 389 | } while (ret < 0 && errno == EINTR); |
Amaury Denoyelle | 58a7704 | 2022-02-09 15:43:07 +0100 | [diff] [blame] | 390 | |
Amaury Denoyelle | 6715cbf | 2022-08-05 11:56:36 +0200 | [diff] [blame] | 391 | if (ret < 0 || ret != sz) { |
| 392 | /* TODO adjust errno for UDP context. */ |
| 393 | if (errno == EAGAIN || errno == EWOULDBLOCK || |
| 394 | errno == ENOTCONN || errno == EINPROGRESS || errno == EBADF) { |
| 395 | struct proxy *prx = qc->li->bind_conf->frontend; |
| 396 | struct quic_counters *prx_counters = |
| 397 | EXTRA_COUNTERS_GET(prx->extra_counters_fe, |
| 398 | &quic_stats_module); |
Amaury Denoyelle | 58a7704 | 2022-02-09 15:43:07 +0100 | [diff] [blame] | 399 | |
Amaury Denoyelle | 6715cbf | 2022-08-05 11:56:36 +0200 | [diff] [blame] | 400 | if (errno == EAGAIN || errno == EWOULDBLOCK) |
| 401 | HA_ATOMIC_INC(&prx_counters->socket_full); |
| 402 | else |
| 403 | HA_ATOMIC_INC(&prx_counters->sendto_err); |
| 404 | } |
| 405 | else if (errno) { |
Ilya Shipitsin | 3b64a28 | 2022-07-29 22:26:53 +0500 | [diff] [blame] | 406 | /* TODO unlisted errno : handle it explicitly. */ |
Amaury Denoyelle | 6715cbf | 2022-08-05 11:56:36 +0200 | [diff] [blame] | 407 | ABORT_NOW(); |
| 408 | } |
| 409 | |
| 410 | return 1; |
Frédéric Lécaille | 48bb875 | 2022-08-03 20:52:20 +0200 | [diff] [blame] | 411 | } |
| 412 | |
Amaury Denoyelle | 6715cbf | 2022-08-05 11:56:36 +0200 | [diff] [blame] | 413 | /* we count the total bytes sent, and the send rate for 32-byte blocks. |
| 414 | * The reason for the latter is that freq_ctr are limited to 4GB and |
| 415 | * that it's not enough per second. |
| 416 | */ |
| 417 | _HA_ATOMIC_ADD(&global.out_bytes, ret); |
| 418 | update_freq_ctr(&global.out_32bps, (ret + 16) / 32); |
| 419 | |
| 420 | return 0; |
Amaury Denoyelle | 58a7704 | 2022-02-09 15:43:07 +0100 | [diff] [blame] | 421 | } |
| 422 | |
Amaury Denoyelle | 2ce99fe | 2022-01-19 15:46:11 +0100 | [diff] [blame] | 423 | |
| 424 | /*********************** QUIC accept queue management ***********************/ |
| 425 | /* per-thread accept queues */ |
| 426 | struct quic_accept_queue *quic_accept_queues; |
| 427 | |
Amaury Denoyelle | cfa2d56 | 2022-01-19 16:01:05 +0100 | [diff] [blame] | 428 | /* Install <qc> on the queue ready to be accepted. The queue task is then woken |
Frédéric Lécaille | 91f083a | 2022-01-28 21:43:48 +0100 | [diff] [blame] | 429 | * up. If <qc> accept is already scheduled or done, nothing is done. |
Amaury Denoyelle | cfa2d56 | 2022-01-19 16:01:05 +0100 | [diff] [blame] | 430 | */ |
| 431 | void quic_accept_push_qc(struct quic_conn *qc) |
| 432 | { |
| 433 | struct quic_accept_queue *queue = &quic_accept_queues[qc->tid]; |
| 434 | struct li_per_thread *lthr = &qc->li->per_thr[qc->tid]; |
| 435 | |
Frédéric Lécaille | 91f083a | 2022-01-28 21:43:48 +0100 | [diff] [blame] | 436 | /* early return if accept is already in progress/done for this |
| 437 | * connection |
| 438 | */ |
Frédéric Lécaille | fc79006 | 2022-03-28 17:10:31 +0200 | [diff] [blame] | 439 | if (qc->flags & QUIC_FL_CONN_ACCEPT_REGISTERED) |
Frédéric Lécaille | 91f083a | 2022-01-28 21:43:48 +0100 | [diff] [blame] | 440 | return; |
| 441 | |
Amaury Denoyelle | cfa2d56 | 2022-01-19 16:01:05 +0100 | [diff] [blame] | 442 | BUG_ON(MT_LIST_INLIST(&qc->accept_list)); |
| 443 | |
Frédéric Lécaille | fc79006 | 2022-03-28 17:10:31 +0200 | [diff] [blame] | 444 | qc->flags |= QUIC_FL_CONN_ACCEPT_REGISTERED; |
Amaury Denoyelle | cfa2d56 | 2022-01-19 16:01:05 +0100 | [diff] [blame] | 445 | /* 1. insert the listener in the accept queue |
| 446 | * |
| 447 | * Use TRY_APPEND as there is a possible race even with INLIST if |
| 448 | * multiple threads try to add the same listener instance from several |
| 449 | * quic_conn. |
| 450 | */ |
| 451 | if (!MT_LIST_INLIST(&(lthr->quic_accept.list))) |
| 452 | MT_LIST_TRY_APPEND(&queue->listeners, &(lthr->quic_accept.list)); |
| 453 | |
| 454 | /* 2. insert the quic_conn in the listener per-thread queue. */ |
| 455 | MT_LIST_APPEND(<hr->quic_accept.conns, &qc->accept_list); |
| 456 | |
| 457 | /* 3. wake up the queue tasklet */ |
| 458 | tasklet_wakeup(quic_accept_queues[qc->tid].tasklet); |
| 459 | } |
| 460 | |
Amaury Denoyelle | 2ce99fe | 2022-01-19 15:46:11 +0100 | [diff] [blame] | 461 | /* Tasklet handler to accept QUIC connections. Call listener_accept on every |
| 462 | * listener instances registered in the accept queue. |
| 463 | */ |
| 464 | static struct task *quic_accept_run(struct task *t, void *ctx, unsigned int i) |
| 465 | { |
| 466 | struct li_per_thread *lthr; |
| 467 | struct mt_list *elt1, elt2; |
| 468 | struct quic_accept_queue *queue = &quic_accept_queues[tid]; |
| 469 | |
| 470 | mt_list_for_each_entry_safe(lthr, &queue->listeners, quic_accept.list, elt1, elt2) { |
| 471 | listener_accept(lthr->li); |
| 472 | MT_LIST_DELETE_SAFE(elt1); |
| 473 | } |
| 474 | |
| 475 | return NULL; |
| 476 | } |
| 477 | |
| 478 | static int quic_alloc_accept_queues(void) |
| 479 | { |
| 480 | int i; |
| 481 | |
Tim Duesterhus | 9fb57e8 | 2022-06-01 21:58:37 +0200 | [diff] [blame] | 482 | quic_accept_queues = calloc(global.nbthread, |
| 483 | sizeof(*quic_accept_queues)); |
Amaury Denoyelle | 2ce99fe | 2022-01-19 15:46:11 +0100 | [diff] [blame] | 484 | if (!quic_accept_queues) { |
| 485 | ha_alert("Failed to allocate the quic accept queues.\n"); |
| 486 | return 0; |
| 487 | } |
| 488 | |
| 489 | for (i = 0; i < global.nbthread; ++i) { |
| 490 | struct tasklet *task; |
| 491 | if (!(task = tasklet_new())) { |
| 492 | ha_alert("Failed to allocate the quic accept queue on thread %d.\n", i); |
| 493 | return 0; |
| 494 | } |
| 495 | |
| 496 | tasklet_set_tid(task, i); |
| 497 | task->process = quic_accept_run; |
| 498 | quic_accept_queues[i].tasklet = task; |
| 499 | |
| 500 | MT_LIST_INIT(&quic_accept_queues[i].listeners); |
| 501 | } |
| 502 | |
| 503 | return 1; |
| 504 | } |
| 505 | REGISTER_POST_CHECK(quic_alloc_accept_queues); |
| 506 | |
| 507 | static int quic_deallocate_accept_queues(void) |
| 508 | { |
| 509 | int i; |
| 510 | |
| 511 | if (quic_accept_queues) { |
| 512 | for (i = 0; i < global.nbthread; ++i) |
| 513 | tasklet_free(quic_accept_queues[i].tasklet); |
| 514 | free(quic_accept_queues); |
| 515 | } |
| 516 | |
| 517 | return 1; |
| 518 | } |
| 519 | REGISTER_POST_DEINIT(quic_deallocate_accept_queues); |