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