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