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> |
Amaury Denoyelle | 4d29504 | 2022-01-19 16:18:44 +0100 | [diff] [blame] | 20 | #include <haproxy/quic_sock.h> |
Amaury Denoyelle | eb01f59 | 2021-10-07 16:44:05 +0200 | [diff] [blame] | 21 | #include <haproxy/session.h> |
Amaury Denoyelle | 777969c | 2022-03-24 16:06:26 +0100 | [diff] [blame] | 22 | #include <haproxy/tools.h> |
Frédéric Lécaille | 026a792 | 2020-11-23 15:46:36 +0100 | [diff] [blame] | 23 | #include <haproxy/xprt_quic.h> |
| 24 | |
| 25 | /* This function is called from the protocol layer accept() in order to |
| 26 | * instantiate a new session on behalf of a given listener and frontend. It |
| 27 | * returns a positive value upon success, 0 if the connection can be ignored, |
| 28 | * or a negative value upon critical failure. The accepted connection is |
| 29 | * closed if we return <= 0. If no handshake is needed, it immediately tries |
| 30 | * to instantiate a new stream. The connection must already have been filled |
| 31 | * with the incoming connection handle (a fd), a target (the listener) and a |
| 32 | * source address. |
| 33 | */ |
| 34 | int quic_session_accept(struct connection *cli_conn) |
| 35 | { |
| 36 | struct listener *l = __objt_listener(cli_conn->target); |
| 37 | struct proxy *p = l->bind_conf->frontend; |
| 38 | struct session *sess; |
| 39 | |
| 40 | cli_conn->proxy_netns = l->rx.settings->netns; |
Frédéric Lécaille | 026a792 | 2020-11-23 15:46:36 +0100 | [diff] [blame] | 41 | /* This flag is ordinarily set by conn_ctrl_init() which cannot |
| 42 | * be called for now. |
| 43 | */ |
| 44 | cli_conn->flags |= CO_FL_CTRL_READY; |
| 45 | |
| 46 | /* wait for a PROXY protocol header */ |
| 47 | if (l->options & LI_O_ACC_PROXY) |
| 48 | cli_conn->flags |= CO_FL_ACCEPT_PROXY; |
| 49 | |
| 50 | /* wait for a NetScaler client IP insertion protocol header */ |
| 51 | if (l->options & LI_O_ACC_CIP) |
| 52 | cli_conn->flags |= CO_FL_ACCEPT_CIP; |
| 53 | |
Frédéric Lécaille | 026a792 | 2020-11-23 15:46:36 +0100 | [diff] [blame] | 54 | /* Add the handshake pseudo-XPRT */ |
| 55 | if (cli_conn->flags & (CO_FL_ACCEPT_PROXY | CO_FL_ACCEPT_CIP)) { |
| 56 | if (xprt_add_hs(cli_conn) != 0) |
| 57 | goto out_free_conn; |
| 58 | } |
Olivier Houchard | 1b3c931 | 2021-03-05 23:37:48 +0100 | [diff] [blame] | 59 | |
Frédéric Lécaille | 026a792 | 2020-11-23 15:46:36 +0100 | [diff] [blame] | 60 | sess = session_new(p, l, &cli_conn->obj_type); |
| 61 | if (!sess) |
| 62 | goto out_free_conn; |
| 63 | |
| 64 | conn_set_owner(cli_conn, sess, NULL); |
| 65 | |
Frédéric Lécaille | ecb5872 | 2021-05-27 17:12:36 +0200 | [diff] [blame] | 66 | if (conn_complete_session(cli_conn) < 0) |
| 67 | goto out_free_sess; |
| 68 | |
| 69 | if (conn_xprt_start(cli_conn) >= 0) |
Frédéric Lécaille | 27faba7 | 2021-03-03 16:21:00 +0100 | [diff] [blame] | 70 | return 1; |
| 71 | |
Frédéric Lécaille | 026a792 | 2020-11-23 15:46:36 +0100 | [diff] [blame] | 72 | out_free_sess: |
| 73 | /* prevent call to listener_release during session_free. It will be |
| 74 | * done below, for all errors. */ |
| 75 | sess->listener = NULL; |
| 76 | session_free(sess); |
| 77 | out_free_conn: |
| 78 | cli_conn->qc->conn = NULL; |
| 79 | conn_stop_tracking(cli_conn); |
| 80 | conn_xprt_close(cli_conn); |
| 81 | conn_free(cli_conn); |
| 82 | out: |
| 83 | |
Frédéric Lécaille | e8139f3 | 2021-03-11 17:06:30 +0100 | [diff] [blame] | 84 | return -1; |
Frédéric Lécaille | 026a792 | 2020-11-23 15:46:36 +0100 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | /* |
| 88 | * Inspired from session_accept_fd(). |
| 89 | * Instantiate a new connection (connection struct) to be attached to <qc> |
| 90 | * QUIC connection of <l> listener. |
| 91 | * Returns 1 if succeeded, 0 if not. |
| 92 | */ |
| 93 | static int new_quic_cli_conn(struct quic_conn *qc, struct listener *l, |
| 94 | struct sockaddr_storage *saddr) |
| 95 | { |
| 96 | struct connection *cli_conn; |
Frédéric Lécaille | 026a792 | 2020-11-23 15:46:36 +0100 | [diff] [blame] | 97 | |
Frédéric Lécaille | 026a792 | 2020-11-23 15:46:36 +0100 | [diff] [blame] | 98 | if (unlikely((cli_conn = conn_new(&l->obj_type)) == NULL)) |
| 99 | goto out; |
| 100 | |
Willy Tarreau | 9cc88c3 | 2022-04-08 14:34:31 +0200 | [diff] [blame] | 101 | if (!sockaddr_alloc(&cli_conn->src, saddr, sizeof *saddr)) |
Frédéric Lécaille | 026a792 | 2020-11-23 15:46:36 +0100 | [diff] [blame] | 102 | goto out_free_conn; |
| 103 | |
Willy Tarreau | 9cc88c3 | 2022-04-08 14:34:31 +0200 | [diff] [blame] | 104 | cli_conn->flags |= CO_FL_ADDR_FROM_SET; |
Frédéric Lécaille | 026a792 | 2020-11-23 15:46:36 +0100 | [diff] [blame] | 105 | qc->conn = cli_conn; |
| 106 | cli_conn->qc = qc; |
| 107 | |
Frédéric Lécaille | 026a792 | 2020-11-23 15:46:36 +0100 | [diff] [blame] | 108 | cli_conn->handle.fd = l->rx.fd; |
Frédéric Lécaille | 026a792 | 2020-11-23 15:46:36 +0100 | [diff] [blame] | 109 | cli_conn->target = &l->obj_type; |
| 110 | |
Frédéric Lécaille | 01ab661 | 2021-06-14 10:31:43 +0200 | [diff] [blame] | 111 | /* We need the xprt context before accepting (->accept()) the connection: |
| 112 | * we may receive packet before this connection acception. |
| 113 | */ |
| 114 | if (conn_prepare(cli_conn, l->rx.proto, l->bind_conf->xprt) < 0) |
| 115 | goto out_free_conn; |
Frédéric Lécaille | 026a792 | 2020-11-23 15:46:36 +0100 | [diff] [blame] | 116 | |
| 117 | return 1; |
| 118 | |
| 119 | out_free_conn: |
Frédéric Lécaille | 01ab661 | 2021-06-14 10:31:43 +0200 | [diff] [blame] | 120 | qc->conn = NULL; |
Frédéric Lécaille | 026a792 | 2020-11-23 15:46:36 +0100 | [diff] [blame] | 121 | conn_stop_tracking(cli_conn); |
| 122 | conn_xprt_close(cli_conn); |
| 123 | conn_free(cli_conn); |
Frédéric Lécaille | 026a792 | 2020-11-23 15:46:36 +0100 | [diff] [blame] | 124 | out: |
| 125 | |
| 126 | return 0; |
| 127 | } |
Frédéric Lécaille | 70da889 | 2020-11-06 15:49:49 +0100 | [diff] [blame] | 128 | |
| 129 | /* Tests if the receiver supports accepting connections. Returns positive on |
| 130 | * success, 0 if not possible |
| 131 | */ |
| 132 | int quic_sock_accepting_conn(const struct receiver *rx) |
| 133 | { |
| 134 | return 1; |
| 135 | } |
| 136 | |
| 137 | /* Accept an incoming connection from listener <l>, and return it, as well as |
| 138 | * a CO_AC_* status code into <status> if not null. Null is returned on error. |
| 139 | * <l> must be a valid listener with a valid frontend. |
| 140 | */ |
| 141 | struct connection *quic_sock_accept_conn(struct listener *l, int *status) |
| 142 | { |
Frédéric Lécaille | 026a792 | 2020-11-23 15:46:36 +0100 | [diff] [blame] | 143 | struct quic_conn *qc; |
Amaury Denoyelle | cfa2d56 | 2022-01-19 16:01:05 +0100 | [diff] [blame] | 144 | struct li_per_thread *lthr = &l->per_thr[tid]; |
Frédéric Lécaille | 026a792 | 2020-11-23 15:46:36 +0100 | [diff] [blame] | 145 | |
Amaury Denoyelle | cfa2d56 | 2022-01-19 16:01:05 +0100 | [diff] [blame] | 146 | qc = MT_LIST_POP(<hr->quic_accept.conns, struct quic_conn *, accept_list); |
| 147 | if (!qc) |
| 148 | goto done; |
Frédéric Lécaille | 026a792 | 2020-11-23 15:46:36 +0100 | [diff] [blame] | 149 | |
Amaury Denoyelle | cfa2d56 | 2022-01-19 16:01:05 +0100 | [diff] [blame] | 150 | 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] | 151 | goto err; |
| 152 | |
Frédéric Lécaille | 026a792 | 2020-11-23 15:46:36 +0100 | [diff] [blame] | 153 | done: |
Amaury Denoyelle | cfa2d56 | 2022-01-19 16:01:05 +0100 | [diff] [blame] | 154 | *status = CO_AC_DONE; |
Frédéric Lécaille | 026a792 | 2020-11-23 15:46:36 +0100 | [diff] [blame] | 155 | return qc ? qc->conn : NULL; |
| 156 | |
| 157 | err: |
Amaury Denoyelle | cfa2d56 | 2022-01-19 16:01:05 +0100 | [diff] [blame] | 158 | /* in case of error reinsert the element to process it later. */ |
| 159 | MT_LIST_INSERT(<hr->quic_accept.conns, &qc->accept_list); |
| 160 | |
| 161 | *status = CO_AC_PAUSE; |
| 162 | return NULL; |
Frédéric Lécaille | 70da889 | 2020-11-06 15:49:49 +0100 | [diff] [blame] | 163 | } |
| 164 | |
| 165 | /* Function called on a read event from a listening socket. It tries |
| 166 | * to handle as many connections as possible. |
| 167 | */ |
| 168 | void quic_sock_fd_iocb(int fd) |
| 169 | { |
| 170 | ssize_t ret; |
Frédéric Lécaille | 324ecda | 2021-11-02 10:14:44 +0100 | [diff] [blame] | 171 | struct rxbuf *rxbuf; |
Frédéric Lécaille | 70da889 | 2020-11-06 15:49:49 +0100 | [diff] [blame] | 172 | struct buffer *buf; |
| 173 | struct listener *l = objt_listener(fdtab[fd].owner); |
Frédéric Lécaille | c4becf5 | 2021-11-08 11:23:17 +0100 | [diff] [blame] | 174 | struct quic_transport_params *params; |
Frédéric Lécaille | 70da889 | 2020-11-06 15:49:49 +0100 | [diff] [blame] | 175 | /* Source address */ |
| 176 | struct sockaddr_storage saddr = {0}; |
Frédéric Lécaille | 320744b | 2022-01-27 12:19:28 +0100 | [diff] [blame] | 177 | size_t max_sz, cspace; |
Frédéric Lécaille | 70da889 | 2020-11-06 15:49:49 +0100 | [diff] [blame] | 178 | socklen_t saddrlen; |
Frédéric Lécaille | 37ae505 | 2022-01-27 11:31:50 +0100 | [diff] [blame] | 179 | struct quic_dgram *dgram, *dgramp, *new_dgram; |
Frédéric Lécaille | f6f7520 | 2022-02-02 09:44:22 +0100 | [diff] [blame] | 180 | unsigned char *dgram_buf; |
Frédéric Lécaille | 70da889 | 2020-11-06 15:49:49 +0100 | [diff] [blame] | 181 | |
Tim Duesterhus | 1655424 | 2021-09-15 13:58:49 +0200 | [diff] [blame] | 182 | BUG_ON(!l); |
Frédéric Lécaille | 70da889 | 2020-11-06 15:49:49 +0100 | [diff] [blame] | 183 | |
Frédéric Lécaille | c4becf5 | 2021-11-08 11:23:17 +0100 | [diff] [blame] | 184 | if (!l) |
| 185 | return; |
| 186 | |
Willy Tarreau | f509065 | 2021-04-06 17:23:40 +0200 | [diff] [blame] | 187 | 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] | 188 | return; |
| 189 | |
Frédéric Lécaille | 324ecda | 2021-11-02 10:14:44 +0100 | [diff] [blame] | 190 | rxbuf = MT_LIST_POP(&l->rx.rxbuf_list, typeof(rxbuf), mt_list); |
Amaury Denoyelle | ee72a43 | 2021-11-19 15:49:29 +0100 | [diff] [blame] | 191 | if (!rxbuf) |
Frédéric Lécaille | c4becf5 | 2021-11-08 11:23:17 +0100 | [diff] [blame] | 192 | goto out; |
Frédéric Lécaille | 37ae505 | 2022-01-27 11:31:50 +0100 | [diff] [blame] | 193 | |
Amaury Denoyelle | ee72a43 | 2021-11-19 15:49:29 +0100 | [diff] [blame] | 194 | buf = &rxbuf->buf; |
Frédéric Lécaille | c4becf5 | 2021-11-08 11:23:17 +0100 | [diff] [blame] | 195 | |
Frédéric Lécaille | 37ae505 | 2022-01-27 11:31:50 +0100 | [diff] [blame] | 196 | new_dgram = NULL; |
| 197 | /* Remove all consumed datagrams of this buffer */ |
| 198 | list_for_each_entry_safe(dgram, dgramp, &rxbuf->dgrams, list) { |
| 199 | if (HA_ATOMIC_LOAD(&dgram->buf)) |
| 200 | break; |
| 201 | |
| 202 | LIST_DELETE(&dgram->list); |
| 203 | b_del(buf, dgram->len); |
| 204 | if (!new_dgram) |
| 205 | new_dgram = dgram; |
| 206 | else |
| 207 | pool_free(pool_head_quic_dgram, dgram); |
| 208 | } |
| 209 | |
Frédéric Lécaille | c4becf5 | 2021-11-08 11:23:17 +0100 | [diff] [blame] | 210 | params = &l->bind_conf->quic_params; |
Frédéric Lécaille | 324ecda | 2021-11-02 10:14:44 +0100 | [diff] [blame] | 211 | max_sz = params->max_udp_payload_size; |
Frédéric Lécaille | 320744b | 2022-01-27 12:19:28 +0100 | [diff] [blame] | 212 | cspace = b_contig_space(buf); |
| 213 | if (cspace < max_sz) { |
Frédéric Lécaille | 1712b1d | 2022-01-28 13:10:24 +0100 | [diff] [blame] | 214 | struct quic_dgram *dgram; |
| 215 | |
| 216 | /* Allocate a fake datagram, without data to locate |
| 217 | * the end of the RX buffer (required during purging). |
| 218 | */ |
| 219 | dgram = pool_zalloc(pool_head_quic_dgram); |
| 220 | if (!dgram) |
| 221 | goto out; |
| 222 | |
| 223 | dgram->len = cspace; |
| 224 | LIST_APPEND(&rxbuf->dgrams, &dgram->list); |
Frédéric Lécaille | 320744b | 2022-01-27 12:19:28 +0100 | [diff] [blame] | 225 | /* Consume the remaining space */ |
| 226 | b_add(buf, cspace); |
Frédéric Lécaille | 324ecda | 2021-11-02 10:14:44 +0100 | [diff] [blame] | 227 | if (b_contig_space(buf) < max_sz) |
| 228 | goto out; |
Frédéric Lécaille | f6f7520 | 2022-02-02 09:44:22 +0100 | [diff] [blame] | 229 | |
Frédéric Lécaille | 324ecda | 2021-11-02 10:14:44 +0100 | [diff] [blame] | 230 | } |
| 231 | |
Frédéric Lécaille | f6f7520 | 2022-02-02 09:44:22 +0100 | [diff] [blame] | 232 | dgram_buf = (unsigned char *)b_tail(buf); |
Frédéric Lécaille | 70da889 | 2020-11-06 15:49:49 +0100 | [diff] [blame] | 233 | saddrlen = sizeof saddr; |
| 234 | do { |
Frédéric Lécaille | f6f7520 | 2022-02-02 09:44:22 +0100 | [diff] [blame] | 235 | ret = recvfrom(fd, dgram_buf, max_sz, 0, |
Frédéric Lécaille | 70da889 | 2020-11-06 15:49:49 +0100 | [diff] [blame] | 236 | (struct sockaddr *)&saddr, &saddrlen); |
Frédéric Lécaille | 439c464 | 2022-02-02 14:33:10 +0100 | [diff] [blame] | 237 | if (ret < 0 && errno == EAGAIN) { |
| 238 | fd_cant_recv(fd); |
Frédéric Lécaille | 324ecda | 2021-11-02 10:14:44 +0100 | [diff] [blame] | 239 | goto out; |
Frédéric Lécaille | 70da889 | 2020-11-06 15:49:49 +0100 | [diff] [blame] | 240 | } |
Frédéric Lécaille | 439c464 | 2022-02-02 14:33:10 +0100 | [diff] [blame] | 241 | } while (ret < 0 && errno == EINTR); |
Frédéric Lécaille | 70da889 | 2020-11-06 15:49:49 +0100 | [diff] [blame] | 242 | |
Frédéric Lécaille | 324ecda | 2021-11-02 10:14:44 +0100 | [diff] [blame] | 243 | b_add(buf, ret); |
Frédéric Lécaille | f6f7520 | 2022-02-02 09:44:22 +0100 | [diff] [blame] | 244 | if (!quic_lstnr_dgram_dispatch(dgram_buf, ret, l, &saddr, |
| 245 | new_dgram, &rxbuf->dgrams)) { |
Frédéric Lécaille | 37ae505 | 2022-01-27 11:31:50 +0100 | [diff] [blame] | 246 | /* If wrong, consume this datagram */ |
| 247 | b_del(buf, ret); |
| 248 | } |
Frédéric Lécaille | 324ecda | 2021-11-02 10:14:44 +0100 | [diff] [blame] | 249 | out: |
| 250 | 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] | 251 | } |
Amaury Denoyelle | 2ce99fe | 2022-01-19 15:46:11 +0100 | [diff] [blame] | 252 | |
Amaury Denoyelle | 58a7704 | 2022-02-09 15:43:07 +0100 | [diff] [blame] | 253 | /* TODO standardize this function for a generic UDP sendto wrapper. This can be |
| 254 | * done by removing the <qc> arg and replace it with address/port. |
| 255 | */ |
| 256 | size_t qc_snd_buf(struct quic_conn *qc, const struct buffer *buf, size_t count, |
| 257 | int flags) |
| 258 | { |
| 259 | ssize_t ret; |
| 260 | size_t try, done; |
| 261 | int send_flag; |
| 262 | |
| 263 | done = 0; |
| 264 | /* send the largest possible block. For this we perform only one call |
| 265 | * to send() unless the buffer wraps and we exactly fill the first hunk, |
| 266 | * in which case we accept to do it once again. |
| 267 | */ |
| 268 | while (count) { |
| 269 | try = b_contig_data(buf, done); |
| 270 | if (try > count) |
| 271 | try = count; |
| 272 | |
| 273 | send_flag = MSG_DONTWAIT | MSG_NOSIGNAL; |
| 274 | if (try < count || flags & CO_SFL_MSG_MORE) |
| 275 | send_flag |= MSG_MORE; |
| 276 | |
| 277 | ret = sendto(qc->li->rx.fd, b_peek(buf, done), try, send_flag, |
| 278 | (struct sockaddr *)&qc->peer_addr, get_addr_len(&qc->peer_addr)); |
| 279 | if (ret > 0) { |
| 280 | /* TODO remove partial sending support for UDP */ |
| 281 | count -= ret; |
| 282 | done += ret; |
| 283 | |
| 284 | if (ret < try) |
| 285 | break; |
| 286 | } |
| 287 | else if (ret == 0 || errno == EAGAIN || errno == ENOTCONN || errno == EINPROGRESS) { |
| 288 | /* TODO must be handle properly. It is justified for UDP ? */ |
| 289 | ABORT_NOW(); |
| 290 | } |
| 291 | else if (errno != EINTR) { |
| 292 | /* TODO must be handle properly. It is justified for UDP ? */ |
| 293 | ABORT_NOW(); |
| 294 | } |
| 295 | } |
| 296 | |
| 297 | if (done > 0) { |
| 298 | /* we count the total bytes sent, and the send rate for 32-byte |
| 299 | * blocks. The reason for the latter is that freq_ctr are |
| 300 | * limited to 4GB and that it's not enough per second. |
| 301 | */ |
| 302 | _HA_ATOMIC_ADD(&global.out_bytes, done); |
| 303 | update_freq_ctr(&global.out_32bps, (done + 16) / 32); |
| 304 | } |
| 305 | return done; |
| 306 | } |
| 307 | |
Amaury Denoyelle | 2ce99fe | 2022-01-19 15:46:11 +0100 | [diff] [blame] | 308 | |
| 309 | /*********************** QUIC accept queue management ***********************/ |
| 310 | /* per-thread accept queues */ |
| 311 | struct quic_accept_queue *quic_accept_queues; |
| 312 | |
Amaury Denoyelle | cfa2d56 | 2022-01-19 16:01:05 +0100 | [diff] [blame] | 313 | /* 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] | 314 | * up. If <qc> accept is already scheduled or done, nothing is done. |
Amaury Denoyelle | cfa2d56 | 2022-01-19 16:01:05 +0100 | [diff] [blame] | 315 | */ |
| 316 | void quic_accept_push_qc(struct quic_conn *qc) |
| 317 | { |
| 318 | struct quic_accept_queue *queue = &quic_accept_queues[qc->tid]; |
| 319 | struct li_per_thread *lthr = &qc->li->per_thr[qc->tid]; |
| 320 | |
Frédéric Lécaille | 91f083a | 2022-01-28 21:43:48 +0100 | [diff] [blame] | 321 | /* early return if accept is already in progress/done for this |
| 322 | * connection |
| 323 | */ |
Frédéric Lécaille | fc79006 | 2022-03-28 17:10:31 +0200 | [diff] [blame] | 324 | if (qc->flags & QUIC_FL_CONN_ACCEPT_REGISTERED) |
Frédéric Lécaille | 91f083a | 2022-01-28 21:43:48 +0100 | [diff] [blame] | 325 | return; |
| 326 | |
Amaury Denoyelle | cfa2d56 | 2022-01-19 16:01:05 +0100 | [diff] [blame] | 327 | BUG_ON(MT_LIST_INLIST(&qc->accept_list)); |
| 328 | |
Frédéric Lécaille | fc79006 | 2022-03-28 17:10:31 +0200 | [diff] [blame] | 329 | qc->flags |= QUIC_FL_CONN_ACCEPT_REGISTERED; |
Amaury Denoyelle | cfa2d56 | 2022-01-19 16:01:05 +0100 | [diff] [blame] | 330 | /* 1. insert the listener in the accept queue |
| 331 | * |
| 332 | * Use TRY_APPEND as there is a possible race even with INLIST if |
| 333 | * multiple threads try to add the same listener instance from several |
| 334 | * quic_conn. |
| 335 | */ |
| 336 | if (!MT_LIST_INLIST(&(lthr->quic_accept.list))) |
| 337 | MT_LIST_TRY_APPEND(&queue->listeners, &(lthr->quic_accept.list)); |
| 338 | |
| 339 | /* 2. insert the quic_conn in the listener per-thread queue. */ |
| 340 | MT_LIST_APPEND(<hr->quic_accept.conns, &qc->accept_list); |
| 341 | |
| 342 | /* 3. wake up the queue tasklet */ |
| 343 | tasklet_wakeup(quic_accept_queues[qc->tid].tasklet); |
| 344 | } |
| 345 | |
Amaury Denoyelle | 2ce99fe | 2022-01-19 15:46:11 +0100 | [diff] [blame] | 346 | /* Tasklet handler to accept QUIC connections. Call listener_accept on every |
| 347 | * listener instances registered in the accept queue. |
| 348 | */ |
| 349 | static struct task *quic_accept_run(struct task *t, void *ctx, unsigned int i) |
| 350 | { |
| 351 | struct li_per_thread *lthr; |
| 352 | struct mt_list *elt1, elt2; |
| 353 | struct quic_accept_queue *queue = &quic_accept_queues[tid]; |
| 354 | |
| 355 | mt_list_for_each_entry_safe(lthr, &queue->listeners, quic_accept.list, elt1, elt2) { |
| 356 | listener_accept(lthr->li); |
| 357 | MT_LIST_DELETE_SAFE(elt1); |
| 358 | } |
| 359 | |
| 360 | return NULL; |
| 361 | } |
| 362 | |
| 363 | static int quic_alloc_accept_queues(void) |
| 364 | { |
| 365 | int i; |
| 366 | |
| 367 | quic_accept_queues = calloc(global.nbthread, sizeof(struct quic_accept_queue)); |
| 368 | if (!quic_accept_queues) { |
| 369 | ha_alert("Failed to allocate the quic accept queues.\n"); |
| 370 | return 0; |
| 371 | } |
| 372 | |
| 373 | for (i = 0; i < global.nbthread; ++i) { |
| 374 | struct tasklet *task; |
| 375 | if (!(task = tasklet_new())) { |
| 376 | ha_alert("Failed to allocate the quic accept queue on thread %d.\n", i); |
| 377 | return 0; |
| 378 | } |
| 379 | |
| 380 | tasklet_set_tid(task, i); |
| 381 | task->process = quic_accept_run; |
| 382 | quic_accept_queues[i].tasklet = task; |
| 383 | |
| 384 | MT_LIST_INIT(&quic_accept_queues[i].listeners); |
| 385 | } |
| 386 | |
| 387 | return 1; |
| 388 | } |
| 389 | REGISTER_POST_CHECK(quic_alloc_accept_queues); |
| 390 | |
| 391 | static int quic_deallocate_accept_queues(void) |
| 392 | { |
| 393 | int i; |
| 394 | |
| 395 | if (quic_accept_queues) { |
| 396 | for (i = 0; i < global.nbthread; ++i) |
| 397 | tasklet_free(quic_accept_queues[i].tasklet); |
| 398 | free(quic_accept_queues); |
| 399 | } |
| 400 | |
| 401 | return 1; |
| 402 | } |
| 403 | REGISTER_POST_DEINIT(quic_deallocate_accept_queues); |