Baptiste Assmann | 325137d | 2015-04-13 23:40:55 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Name server resolution |
| 3 | * |
Willy Tarreau | 714f345 | 2021-05-09 06:47:26 +0200 | [diff] [blame] | 4 | * Copyright 2020 HAProxy Technologies |
Baptiste Assmann | 325137d | 2015-04-13 23:40:55 +0200 | [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> |
Baptiste Assmann | 325137d | 2015-04-13 23:40:55 +0200 | [diff] [blame] | 14 | #include <stdio.h> |
| 15 | #include <stdlib.h> |
| 16 | #include <string.h> |
| 17 | #include <unistd.h> |
| 18 | |
| 19 | #include <sys/types.h> |
| 20 | |
Willy Tarreau | 122eba9 | 2020-06-04 10:15:32 +0200 | [diff] [blame] | 21 | #include <haproxy/action.h> |
Willy Tarreau | 4c7e4b7 | 2020-05-27 12:58:42 +0200 | [diff] [blame] | 22 | #include <haproxy/api.h> |
Christopher Faulet | 6b0a0fb | 2022-04-04 11:29:28 +0200 | [diff] [blame] | 23 | #include <haproxy/applet.h> |
Willy Tarreau | 6be7849 | 2020-06-05 00:00:29 +0200 | [diff] [blame] | 24 | #include <haproxy/cfgparse.h> |
Willy Tarreau | f1d32c4 | 2020-06-04 21:07:02 +0200 | [diff] [blame] | 25 | #include <haproxy/channel.h> |
Willy Tarreau | b255105 | 2020-06-09 09:07:15 +0200 | [diff] [blame] | 26 | #include <haproxy/check.h> |
Willy Tarreau | 83487a8 | 2020-06-04 20:19:54 +0200 | [diff] [blame] | 27 | #include <haproxy/cli.h> |
Christopher Faulet | 908628c | 2022-03-25 16:43:49 +0100 | [diff] [blame] | 28 | #include <haproxy/conn_stream.h> |
| 29 | #include <haproxy/cs_utils.h> |
Willy Tarreau | 7c18b54 | 2020-06-11 09:23:02 +0200 | [diff] [blame] | 30 | #include <haproxy/dgram.h> |
Willy Tarreau | eb92deb | 2020-06-04 10:53:16 +0200 | [diff] [blame] | 31 | #include <haproxy/dns.h> |
Willy Tarreau | 8d36697 | 2020-05-27 16:10:29 +0200 | [diff] [blame] | 32 | #include <haproxy/errors.h> |
Willy Tarreau | b255105 | 2020-06-09 09:07:15 +0200 | [diff] [blame] | 33 | #include <haproxy/fd.h> |
Willy Tarreau | aeed4a8 | 2020-06-04 22:01:04 +0200 | [diff] [blame] | 34 | #include <haproxy/log.h> |
Emeric Brun | d26a623 | 2021-01-04 13:32:20 +0100 | [diff] [blame] | 35 | #include <haproxy/ring.h> |
Emeric Brun | fd647d5 | 2021-02-12 20:03:38 +0100 | [diff] [blame] | 36 | #include <haproxy/stream.h> |
Willy Tarreau | 9f9e9fc | 2021-05-08 13:09:46 +0200 | [diff] [blame] | 37 | #include <haproxy/tools.h> |
Baptiste Assmann | 325137d | 2015-04-13 23:40:55 +0200 | [diff] [blame] | 38 | |
Emeric Brun | d26a623 | 2021-01-04 13:32:20 +0100 | [diff] [blame] | 39 | static THREAD_LOCAL char *dns_msg_trash; |
Baptiste Assmann | 325137d | 2015-04-13 23:40:55 +0200 | [diff] [blame] | 40 | |
Emeric Brun | fd647d5 | 2021-02-12 20:03:38 +0100 | [diff] [blame] | 41 | DECLARE_STATIC_POOL(dns_session_pool, "dns_session", sizeof(struct dns_session)); |
| 42 | DECLARE_STATIC_POOL(dns_query_pool, "dns_query", sizeof(struct dns_query)); |
| 43 | DECLARE_STATIC_POOL(dns_msg_buf, "dns_msg_buf", DNS_TCP_MSG_RING_MAX_SIZE); |
| 44 | |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 45 | /* Opens an UDP socket on the namesaver's IP/Port, if required. Returns 0 on |
Christopher Faulet | 1e711be | 2021-03-04 16:58:35 +0100 | [diff] [blame] | 46 | * success, -1 otherwise. ns->dgram must be defined. |
Baptiste Assmann | 325137d | 2015-04-13 23:40:55 +0200 | [diff] [blame] | 47 | */ |
Emeric Brun | d26a623 | 2021-01-04 13:32:20 +0100 | [diff] [blame] | 48 | static int dns_connect_nameserver(struct dns_nameserver *ns) |
Baptiste Assmann | 325137d | 2015-04-13 23:40:55 +0200 | [diff] [blame] | 49 | { |
Christopher Faulet | 1e711be | 2021-03-04 16:58:35 +0100 | [diff] [blame] | 50 | struct dgram_conn *dgram = &ns->dgram->conn; |
| 51 | int fd; |
Baptiste Assmann | 325137d | 2015-04-13 23:40:55 +0200 | [diff] [blame] | 52 | |
Christopher Faulet | 1e711be | 2021-03-04 16:58:35 +0100 | [diff] [blame] | 53 | /* Already connected */ |
| 54 | if (dgram->t.sock.fd != -1) |
Emeric Brun | 526b792 | 2021-02-15 14:28:27 +0100 | [diff] [blame] | 55 | return 0; |
Christopher Faulet | 1e711be | 2021-03-04 16:58:35 +0100 | [diff] [blame] | 56 | |
| 57 | /* Create an UDP socket and connect it on the nameserver's IP/Port */ |
| 58 | if ((fd = socket(dgram->addr.to.ss_family, SOCK_DGRAM, IPPROTO_UDP)) == -1) { |
| 59 | send_log(NULL, LOG_WARNING, |
| 60 | "DNS : section '%s': can't create socket for nameserver '%s'.\n", |
| 61 | ns->counters->pid, ns->id); |
| 62 | return -1; |
| 63 | } |
| 64 | if (connect(fd, (struct sockaddr*)&dgram->addr.to, get_addr_len(&dgram->addr.to)) == -1) { |
| 65 | send_log(NULL, LOG_WARNING, |
| 66 | "DNS : section '%s': can't connect socket for nameserver '%s'.\n", |
| 67 | ns->counters->id, ns->id); |
| 68 | close(fd); |
| 69 | return -1; |
Emeric Brun | c943799 | 2021-02-12 19:42:55 +0100 | [diff] [blame] | 70 | } |
Emeric Brun | 526b792 | 2021-02-15 14:28:27 +0100 | [diff] [blame] | 71 | |
Christopher Faulet | 1e711be | 2021-03-04 16:58:35 +0100 | [diff] [blame] | 72 | /* Make the socket non blocking */ |
Willy Tarreau | 3824743 | 2022-04-26 10:24:14 +0200 | [diff] [blame] | 73 | fd_set_nonblock(fd); |
Christopher Faulet | 1e711be | 2021-03-04 16:58:35 +0100 | [diff] [blame] | 74 | |
| 75 | /* Add the fd in the fd list and update its parameters */ |
| 76 | dgram->t.sock.fd = fd; |
| 77 | fd_insert(fd, dgram, dgram_fd_handler, MAX_THREADS_MASK); |
| 78 | fd_want_recv(fd); |
| 79 | return 0; |
Baptiste Assmann | 325137d | 2015-04-13 23:40:55 +0200 | [diff] [blame] | 80 | } |
| 81 | |
Emeric Brun | d26a623 | 2021-01-04 13:32:20 +0100 | [diff] [blame] | 82 | /* Sends a message to a name server |
| 83 | * It returns message length on success |
| 84 | * or -1 in error case |
| 85 | * 0 is returned in case of output ring buffer is full |
| 86 | */ |
| 87 | int dns_send_nameserver(struct dns_nameserver *ns, void *buf, size_t len) |
| 88 | { |
| 89 | int ret = -1; |
| 90 | |
| 91 | if (ns->dgram) { |
| 92 | struct dgram_conn *dgram = &ns->dgram->conn; |
Emeric Brun | 314e6ec | 2022-05-10 11:35:48 +0200 | [diff] [blame] | 93 | int fd; |
Emeric Brun | d26a623 | 2021-01-04 13:32:20 +0100 | [diff] [blame] | 94 | |
Emeric Brun | 314e6ec | 2022-05-10 11:35:48 +0200 | [diff] [blame] | 95 | HA_SPIN_LOCK(DNS_LOCK, &dgram->lock); |
| 96 | fd = dgram->t.sock.fd; |
| 97 | if (fd == -1) { |
| 98 | if (dns_connect_nameserver(ns) == -1) { |
| 99 | HA_SPIN_UNLOCK(DNS_LOCK, &dgram->lock); |
Emeric Brun | d26a623 | 2021-01-04 13:32:20 +0100 | [diff] [blame] | 100 | return -1; |
Emeric Brun | 314e6ec | 2022-05-10 11:35:48 +0200 | [diff] [blame] | 101 | } |
Emeric Brun | d26a623 | 2021-01-04 13:32:20 +0100 | [diff] [blame] | 102 | fd = dgram->t.sock.fd; |
| 103 | } |
| 104 | |
| 105 | ret = send(fd, buf, len, 0); |
| 106 | if (ret < 0) { |
Willy Tarreau | acef5e2 | 2022-04-25 20:32:15 +0200 | [diff] [blame] | 107 | if (errno == EAGAIN || errno == EWOULDBLOCK) { |
Emeric Brun | d26a623 | 2021-01-04 13:32:20 +0100 | [diff] [blame] | 108 | struct ist myist; |
| 109 | |
Tim Duesterhus | 92c696e | 2021-02-28 16:11:36 +0100 | [diff] [blame] | 110 | myist = ist2(buf, len); |
Emeric Brun | d26a623 | 2021-01-04 13:32:20 +0100 | [diff] [blame] | 111 | ret = ring_write(ns->dgram->ring_req, DNS_TCP_MSG_MAX_SIZE, NULL, 0, &myist, 1); |
| 112 | if (!ret) { |
| 113 | ns->counters->snd_error++; |
Emeric Brun | 314e6ec | 2022-05-10 11:35:48 +0200 | [diff] [blame] | 114 | HA_SPIN_UNLOCK(DNS_LOCK, &dgram->lock); |
Emeric Brun | d26a623 | 2021-01-04 13:32:20 +0100 | [diff] [blame] | 115 | return -1; |
| 116 | } |
| 117 | fd_cant_send(fd); |
Emeric Brun | 314e6ec | 2022-05-10 11:35:48 +0200 | [diff] [blame] | 118 | HA_SPIN_UNLOCK(DNS_LOCK, &dgram->lock); |
Emeric Brun | d26a623 | 2021-01-04 13:32:20 +0100 | [diff] [blame] | 119 | return ret; |
| 120 | } |
| 121 | ns->counters->snd_error++; |
| 122 | fd_delete(fd); |
Emeric Brun | d26a623 | 2021-01-04 13:32:20 +0100 | [diff] [blame] | 123 | dgram->t.sock.fd = -1; |
Emeric Brun | 314e6ec | 2022-05-10 11:35:48 +0200 | [diff] [blame] | 124 | HA_SPIN_UNLOCK(DNS_LOCK, &dgram->lock); |
Emeric Brun | d26a623 | 2021-01-04 13:32:20 +0100 | [diff] [blame] | 125 | return -1; |
| 126 | } |
| 127 | ns->counters->sent++; |
Emeric Brun | 314e6ec | 2022-05-10 11:35:48 +0200 | [diff] [blame] | 128 | HA_SPIN_UNLOCK(DNS_LOCK, &dgram->lock); |
Emeric Brun | d26a623 | 2021-01-04 13:32:20 +0100 | [diff] [blame] | 129 | } |
Emeric Brun | fd647d5 | 2021-02-12 20:03:38 +0100 | [diff] [blame] | 130 | else if (ns->stream) { |
| 131 | struct ist myist; |
| 132 | |
Tim Duesterhus | 92c696e | 2021-02-28 16:11:36 +0100 | [diff] [blame] | 133 | myist = ist2(buf, len); |
Emeric Brun | fd647d5 | 2021-02-12 20:03:38 +0100 | [diff] [blame] | 134 | ret = ring_write(ns->stream->ring_req, DNS_TCP_MSG_MAX_SIZE, NULL, 0, &myist, 1); |
| 135 | if (!ret) { |
| 136 | ns->counters->snd_error++; |
| 137 | return -1; |
| 138 | } |
| 139 | task_wakeup(ns->stream->task_req, TASK_WOKEN_MSG); |
| 140 | return ret; |
| 141 | } |
Emeric Brun | d26a623 | 2021-01-04 13:32:20 +0100 | [diff] [blame] | 142 | |
| 143 | return ret; |
| 144 | } |
| 145 | |
Emeric Brun | fd647d5 | 2021-02-12 20:03:38 +0100 | [diff] [blame] | 146 | void dns_session_free(struct dns_session *); |
| 147 | |
Emeric Brun | d26a623 | 2021-01-04 13:32:20 +0100 | [diff] [blame] | 148 | /* Receives a dns message |
| 149 | * Returns message length |
| 150 | * 0 is returned if no more message available |
| 151 | * -1 in error case |
| 152 | */ |
| 153 | ssize_t dns_recv_nameserver(struct dns_nameserver *ns, void *data, size_t size) |
| 154 | { |
| 155 | ssize_t ret = -1; |
| 156 | |
| 157 | if (ns->dgram) { |
| 158 | struct dgram_conn *dgram = &ns->dgram->conn; |
Emeric Brun | 314e6ec | 2022-05-10 11:35:48 +0200 | [diff] [blame] | 159 | int fd; |
Emeric Brun | d26a623 | 2021-01-04 13:32:20 +0100 | [diff] [blame] | 160 | |
Emeric Brun | 314e6ec | 2022-05-10 11:35:48 +0200 | [diff] [blame] | 161 | HA_SPIN_LOCK(DNS_LOCK, &dgram->lock); |
| 162 | fd = dgram->t.sock.fd; |
| 163 | if (fd == -1) { |
| 164 | HA_SPIN_UNLOCK(DNS_LOCK, &dgram->lock); |
Emeric Brun | d26a623 | 2021-01-04 13:32:20 +0100 | [diff] [blame] | 165 | return -1; |
Emeric Brun | 314e6ec | 2022-05-10 11:35:48 +0200 | [diff] [blame] | 166 | } |
Emeric Brun | d26a623 | 2021-01-04 13:32:20 +0100 | [diff] [blame] | 167 | |
| 168 | if ((ret = recv(fd, data, size, 0)) < 0) { |
Willy Tarreau | acef5e2 | 2022-04-25 20:32:15 +0200 | [diff] [blame] | 169 | if (errno == EAGAIN || errno == EWOULDBLOCK) { |
Emeric Brun | d26a623 | 2021-01-04 13:32:20 +0100 | [diff] [blame] | 170 | fd_cant_recv(fd); |
Emeric Brun | 314e6ec | 2022-05-10 11:35:48 +0200 | [diff] [blame] | 171 | HA_SPIN_UNLOCK(DNS_LOCK, &dgram->lock); |
Emeric Brun | d26a623 | 2021-01-04 13:32:20 +0100 | [diff] [blame] | 172 | return 0; |
| 173 | } |
| 174 | fd_delete(fd); |
Emeric Brun | d26a623 | 2021-01-04 13:32:20 +0100 | [diff] [blame] | 175 | dgram->t.sock.fd = -1; |
Emeric Brun | 314e6ec | 2022-05-10 11:35:48 +0200 | [diff] [blame] | 176 | HA_SPIN_UNLOCK(DNS_LOCK, &dgram->lock); |
Emeric Brun | d26a623 | 2021-01-04 13:32:20 +0100 | [diff] [blame] | 177 | return -1; |
| 178 | } |
Emeric Brun | 314e6ec | 2022-05-10 11:35:48 +0200 | [diff] [blame] | 179 | HA_SPIN_UNLOCK(DNS_LOCK, &dgram->lock); |
Emeric Brun | d26a623 | 2021-01-04 13:32:20 +0100 | [diff] [blame] | 180 | } |
Emeric Brun | fd647d5 | 2021-02-12 20:03:38 +0100 | [diff] [blame] | 181 | else if (ns->stream) { |
| 182 | struct dns_stream_server *dss = ns->stream; |
| 183 | struct dns_session *ds; |
| 184 | |
| 185 | HA_SPIN_LOCK(DNS_LOCK, &dss->lock); |
| 186 | |
| 187 | if (!LIST_ISEMPTY(&dss->wait_sess)) { |
| 188 | ds = LIST_NEXT(&dss->wait_sess, struct dns_session *, waiter); |
Emeric Brun | fd647d5 | 2021-02-12 20:03:38 +0100 | [diff] [blame] | 189 | ret = ds->rx_msg.len < size ? ds->rx_msg.len : size; |
| 190 | memcpy(data, ds->rx_msg.area, ret); |
| 191 | |
| 192 | ds->rx_msg.len = 0; |
| 193 | |
Willy Tarreau | dde1b44 | 2021-10-21 14:33:38 +0200 | [diff] [blame] | 194 | /* This barrier is here to ensure that all data is |
| 195 | * stored if the appctx detect the elem is out of the |
| 196 | * list. |
| 197 | */ |
| 198 | __ha_barrier_store(); |
| 199 | |
Emeric Brun | fd647d5 | 2021-02-12 20:03:38 +0100 | [diff] [blame] | 200 | LIST_DEL_INIT(&ds->waiter); |
| 201 | |
| 202 | if (ds->appctx) { |
Willy Tarreau | dde1b44 | 2021-10-21 14:33:38 +0200 | [diff] [blame] | 203 | /* This second barrier is here to ensure that |
| 204 | * the waked up appctx won't miss that the elem |
| 205 | * is removed from the list. |
| 206 | */ |
| 207 | __ha_barrier_store(); |
| 208 | |
Ilya Shipitsin | 0de36ad | 2021-02-20 00:23:36 +0500 | [diff] [blame] | 209 | /* awake appctx because it may have other |
Emeric Brun | fd647d5 | 2021-02-12 20:03:38 +0100 | [diff] [blame] | 210 | * message to receive |
| 211 | */ |
| 212 | appctx_wakeup(ds->appctx); |
| 213 | |
| 214 | /* dns_session could already be into free_sess list |
| 215 | * so we firstly remove it */ |
| 216 | LIST_DEL_INIT(&ds->list); |
| 217 | |
| 218 | /* decrease nb_queries to free a slot for a new query on that sess */ |
| 219 | ds->nb_queries--; |
| 220 | if (ds->nb_queries) { |
| 221 | /* it remains pipelined unanswered request |
| 222 | * into this session but we just decrease |
| 223 | * the counter so the session |
| 224 | * can not be full of pipelined requests |
| 225 | * so we can add if to free_sess list |
| 226 | * to receive a new request |
| 227 | */ |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 228 | LIST_INSERT(&ds->dss->free_sess, &ds->list); |
Emeric Brun | fd647d5 | 2021-02-12 20:03:38 +0100 | [diff] [blame] | 229 | } |
| 230 | else { |
| 231 | /* there is no more pipelined requests |
| 232 | * into this session, so we move it |
| 233 | * to idle_sess list */ |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 234 | LIST_INSERT(&ds->dss->idle_sess, &ds->list); |
Emeric Brun | fd647d5 | 2021-02-12 20:03:38 +0100 | [diff] [blame] | 235 | |
| 236 | /* update the counter of idle sessions */ |
| 237 | ds->dss->idle_conns++; |
| 238 | |
| 239 | /* Note: this is useless there to update |
| 240 | * the max_active_conns since we increase |
| 241 | * the idle count */ |
| 242 | } |
| 243 | } |
| 244 | else { |
| 245 | /* there is no more appctx for this session |
| 246 | * it means it is ready to die |
| 247 | */ |
| 248 | dns_session_free(ds); |
| 249 | } |
| 250 | |
| 251 | |
| 252 | } |
| 253 | |
| 254 | HA_SPIN_UNLOCK(DNS_LOCK, &dss->lock); |
| 255 | } |
Emeric Brun | d26a623 | 2021-01-04 13:32:20 +0100 | [diff] [blame] | 256 | |
| 257 | return ret; |
| 258 | } |
| 259 | |
| 260 | static void dns_resolve_recv(struct dgram_conn *dgram) |
| 261 | { |
| 262 | struct dns_nameserver *ns; |
| 263 | int fd; |
| 264 | |
Emeric Brun | 314e6ec | 2022-05-10 11:35:48 +0200 | [diff] [blame] | 265 | HA_SPIN_LOCK(DNS_LOCK, &dgram->lock); |
| 266 | |
Emeric Brun | d26a623 | 2021-01-04 13:32:20 +0100 | [diff] [blame] | 267 | fd = dgram->t.sock.fd; |
| 268 | |
| 269 | /* check if ready for reading */ |
Emeric Brun | 314e6ec | 2022-05-10 11:35:48 +0200 | [diff] [blame] | 270 | if ((fd == -1) || !fd_recv_ready(fd)) { |
| 271 | HA_SPIN_UNLOCK(DNS_LOCK, &dgram->lock); |
Emeric Brun | d26a623 | 2021-01-04 13:32:20 +0100 | [diff] [blame] | 272 | return; |
Emeric Brun | 314e6ec | 2022-05-10 11:35:48 +0200 | [diff] [blame] | 273 | } |
Emeric Brun | d26a623 | 2021-01-04 13:32:20 +0100 | [diff] [blame] | 274 | |
| 275 | /* no need to go further if we can't retrieve the nameserver */ |
| 276 | if ((ns = dgram->owner) == NULL) { |
Willy Tarreau | f509065 | 2021-04-06 17:23:40 +0200 | [diff] [blame] | 277 | _HA_ATOMIC_AND(&fdtab[fd].state, ~(FD_POLL_HUP|FD_POLL_ERR)); |
Emeric Brun | d26a623 | 2021-01-04 13:32:20 +0100 | [diff] [blame] | 278 | fd_stop_recv(fd); |
Emeric Brun | 314e6ec | 2022-05-10 11:35:48 +0200 | [diff] [blame] | 279 | HA_SPIN_UNLOCK(DNS_LOCK, &dgram->lock); |
Emeric Brun | d26a623 | 2021-01-04 13:32:20 +0100 | [diff] [blame] | 280 | return; |
| 281 | } |
| 282 | |
Emeric Brun | 314e6ec | 2022-05-10 11:35:48 +0200 | [diff] [blame] | 283 | HA_SPIN_UNLOCK(DNS_LOCK, &dgram->lock); |
| 284 | |
Emeric Brun | d26a623 | 2021-01-04 13:32:20 +0100 | [diff] [blame] | 285 | ns->process_responses(ns); |
| 286 | } |
| 287 | |
| 288 | /* Called when a dns network socket is ready to send data */ |
| 289 | static void dns_resolve_send(struct dgram_conn *dgram) |
| 290 | { |
| 291 | int fd; |
| 292 | struct dns_nameserver *ns; |
| 293 | struct ring *ring; |
| 294 | struct buffer *buf; |
| 295 | uint64_t msg_len; |
| 296 | size_t len, cnt, ofs; |
| 297 | |
Emeric Brun | 314e6ec | 2022-05-10 11:35:48 +0200 | [diff] [blame] | 298 | HA_SPIN_LOCK(DNS_LOCK, &dgram->lock); |
| 299 | |
Emeric Brun | d26a623 | 2021-01-04 13:32:20 +0100 | [diff] [blame] | 300 | fd = dgram->t.sock.fd; |
| 301 | |
| 302 | /* check if ready for sending */ |
Emeric Brun | 314e6ec | 2022-05-10 11:35:48 +0200 | [diff] [blame] | 303 | if ((fd == -1) || !fd_send_ready(fd)) { |
| 304 | HA_SPIN_UNLOCK(DNS_LOCK, &dgram->lock); |
Emeric Brun | d26a623 | 2021-01-04 13:32:20 +0100 | [diff] [blame] | 305 | return; |
Emeric Brun | 314e6ec | 2022-05-10 11:35:48 +0200 | [diff] [blame] | 306 | } |
Emeric Brun | d26a623 | 2021-01-04 13:32:20 +0100 | [diff] [blame] | 307 | |
| 308 | /* no need to go further if we can't retrieve the nameserver */ |
| 309 | if ((ns = dgram->owner) == NULL) { |
Willy Tarreau | f509065 | 2021-04-06 17:23:40 +0200 | [diff] [blame] | 310 | _HA_ATOMIC_AND(&fdtab[fd].state, ~(FD_POLL_HUP|FD_POLL_ERR)); |
Emeric Brun | d26a623 | 2021-01-04 13:32:20 +0100 | [diff] [blame] | 311 | fd_stop_send(fd); |
Emeric Brun | 314e6ec | 2022-05-10 11:35:48 +0200 | [diff] [blame] | 312 | HA_SPIN_UNLOCK(DNS_LOCK, &dgram->lock); |
Emeric Brun | d26a623 | 2021-01-04 13:32:20 +0100 | [diff] [blame] | 313 | return; |
| 314 | } |
| 315 | |
| 316 | ring = ns->dgram->ring_req; |
| 317 | buf = &ring->buf; |
| 318 | |
| 319 | HA_RWLOCK_RDLOCK(DNS_LOCK, &ring->lock); |
| 320 | ofs = ns->dgram->ofs_req; |
| 321 | |
| 322 | /* explanation for the initialization below: it would be better to do |
| 323 | * this in the parsing function but this would occasionally result in |
| 324 | * dropped events because we'd take a reference on the oldest message |
| 325 | * and keep it while being scheduled. Thus instead let's take it the |
| 326 | * first time we enter here so that we have a chance to pass many |
| 327 | * existing messages before grabbing a reference to a location. This |
| 328 | * value cannot be produced after initialization. |
| 329 | */ |
| 330 | if (unlikely(ofs == ~0)) { |
| 331 | ofs = 0; |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 332 | HA_ATOMIC_INC(b_peek(buf, ofs)); |
Emeric Brun | d26a623 | 2021-01-04 13:32:20 +0100 | [diff] [blame] | 333 | ofs += ring->ofs; |
| 334 | } |
| 335 | |
| 336 | /* we were already there, adjust the offset to be relative to |
| 337 | * the buffer's head and remove us from the counter. |
| 338 | */ |
| 339 | ofs -= ring->ofs; |
| 340 | BUG_ON(ofs >= buf->size); |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 341 | HA_ATOMIC_DEC(b_peek(buf, ofs)); |
Emeric Brun | d26a623 | 2021-01-04 13:32:20 +0100 | [diff] [blame] | 342 | |
| 343 | while (ofs + 1 < b_data(buf)) { |
| 344 | int ret; |
| 345 | |
| 346 | cnt = 1; |
| 347 | len = b_peek_varint(buf, ofs + cnt, &msg_len); |
| 348 | if (!len) |
| 349 | break; |
| 350 | cnt += len; |
| 351 | BUG_ON(msg_len + ofs + cnt + 1 > b_data(buf)); |
| 352 | if (unlikely(msg_len > DNS_TCP_MSG_MAX_SIZE)) { |
| 353 | /* too large a message to ever fit, let's skip it */ |
| 354 | ofs += cnt + msg_len; |
| 355 | continue; |
| 356 | } |
| 357 | |
| 358 | len = b_getblk(buf, dns_msg_trash, msg_len, ofs + cnt); |
| 359 | |
| 360 | ret = send(fd, dns_msg_trash, len, 0); |
| 361 | if (ret < 0) { |
Willy Tarreau | acef5e2 | 2022-04-25 20:32:15 +0200 | [diff] [blame] | 362 | if (errno == EAGAIN || errno == EWOULDBLOCK) { |
Emeric Brun | d26a623 | 2021-01-04 13:32:20 +0100 | [diff] [blame] | 363 | fd_cant_send(fd); |
| 364 | goto out; |
| 365 | } |
| 366 | ns->counters->snd_error++; |
| 367 | fd_delete(fd); |
Emeric Brun | d26a623 | 2021-01-04 13:32:20 +0100 | [diff] [blame] | 368 | fd = dgram->t.sock.fd = -1; |
| 369 | goto out; |
| 370 | } |
| 371 | ns->counters->sent++; |
| 372 | |
| 373 | ofs += cnt + len; |
| 374 | } |
| 375 | |
| 376 | /* we don't want/need to be waked up any more for sending |
| 377 | * because all ring content is sent */ |
| 378 | fd_stop_send(fd); |
| 379 | |
| 380 | out: |
| 381 | |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 382 | HA_ATOMIC_INC(b_peek(buf, ofs)); |
Emeric Brun | d26a623 | 2021-01-04 13:32:20 +0100 | [diff] [blame] | 383 | ofs += ring->ofs; |
| 384 | ns->dgram->ofs_req = ofs; |
| 385 | HA_RWLOCK_RDUNLOCK(DNS_LOCK, &ring->lock); |
Emeric Brun | 314e6ec | 2022-05-10 11:35:48 +0200 | [diff] [blame] | 386 | HA_SPIN_UNLOCK(DNS_LOCK, &dgram->lock); |
Emeric Brun | d26a623 | 2021-01-04 13:32:20 +0100 | [diff] [blame] | 387 | |
| 388 | } |
| 389 | |
Emeric Brun | c943799 | 2021-02-12 19:42:55 +0100 | [diff] [blame] | 390 | /* proto_udp callback functions for a DNS resolution */ |
| 391 | struct dgram_data_cb dns_dgram_cb = { |
| 392 | .recv = dns_resolve_recv, |
| 393 | .send = dns_resolve_send, |
| 394 | }; |
Baptiste Assmann | 325137d | 2015-04-13 23:40:55 +0200 | [diff] [blame] | 395 | |
Emeric Brun | c943799 | 2021-02-12 19:42:55 +0100 | [diff] [blame] | 396 | int dns_dgram_init(struct dns_nameserver *ns, struct sockaddr_storage *sk) |
Baptiste Assmann | 325137d | 2015-04-13 23:40:55 +0200 | [diff] [blame] | 397 | { |
Emeric Brun | c943799 | 2021-02-12 19:42:55 +0100 | [diff] [blame] | 398 | struct dns_dgram_server *dgram; |
Baptiste Assmann | 201c07f | 2017-05-22 15:17:15 +0200 | [diff] [blame] | 399 | |
Emeric Brun | c943799 | 2021-02-12 19:42:55 +0100 | [diff] [blame] | 400 | if ((dgram = calloc(1, sizeof(*dgram))) == NULL) |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 401 | return -1; |
Baptiste Assmann | c1ce5f3 | 2016-05-14 11:26:22 +0200 | [diff] [blame] | 402 | |
Emeric Brun | c943799 | 2021-02-12 19:42:55 +0100 | [diff] [blame] | 403 | /* Leave dgram partially initialized, no FD attached for |
| 404 | * now. */ |
| 405 | dgram->conn.owner = ns; |
| 406 | dgram->conn.data = &dns_dgram_cb; |
| 407 | dgram->conn.t.sock.fd = -1; |
| 408 | dgram->conn.addr.to = *sk; |
Emeric Brun | 314e6ec | 2022-05-10 11:35:48 +0200 | [diff] [blame] | 409 | HA_SPIN_INIT(&dgram->conn.lock); |
Emeric Brun | c943799 | 2021-02-12 19:42:55 +0100 | [diff] [blame] | 410 | ns->dgram = dgram; |
Baptiste Assmann | c1ce5f3 | 2016-05-14 11:26:22 +0200 | [diff] [blame] | 411 | |
Emeric Brun | c943799 | 2021-02-12 19:42:55 +0100 | [diff] [blame] | 412 | dgram->ofs_req = ~0; /* init ring offset */ |
| 413 | dgram->ring_req = ring_new(2*DNS_TCP_MSG_RING_MAX_SIZE); |
| 414 | if (!dgram->ring_req) { |
| 415 | ha_alert("memory allocation error initializing the ring for nameserver.\n"); |
| 416 | goto out; |
Baptiste Assmann | c1ce5f3 | 2016-05-14 11:26:22 +0200 | [diff] [blame] | 417 | } |
| 418 | |
Emeric Brun | c943799 | 2021-02-12 19:42:55 +0100 | [diff] [blame] | 419 | /* attach the task as reader */ |
| 420 | if (!ring_attach(dgram->ring_req)) { |
| 421 | /* mark server attached to the ring */ |
| 422 | ha_alert("nameserver sets too many watchers > 255 on ring. This is a bug and should not happen.\n"); |
| 423 | goto out; |
Baptiste Assmann | c1ce5f3 | 2016-05-14 11:26:22 +0200 | [diff] [blame] | 424 | } |
Baptiste Assmann | c1ce5f3 | 2016-05-14 11:26:22 +0200 | [diff] [blame] | 425 | return 0; |
Emeric Brun | c943799 | 2021-02-12 19:42:55 +0100 | [diff] [blame] | 426 | out: |
| 427 | if (dgram->ring_req) |
| 428 | ring_free(dgram->ring_req); |
Christopher Faulet | d6c6b5f | 2020-09-08 10:27:24 +0200 | [diff] [blame] | 429 | |
Emeric Brun | c943799 | 2021-02-12 19:42:55 +0100 | [diff] [blame] | 430 | free(dgram); |
Olivier Houchard | 2ec2db9 | 2018-01-08 16:28:57 +0100 | [diff] [blame] | 431 | |
Emeric Brun | fd647d5 | 2021-02-12 20:03:38 +0100 | [diff] [blame] | 432 | return -1; |
| 433 | } |
| 434 | |
| 435 | /* |
| 436 | * IO Handler to handle message push to dns tcp server |
Willy Tarreau | 0d626a5 | 2022-05-04 20:41:19 +0200 | [diff] [blame] | 437 | * It takes its context from appctx->svcctx. |
Emeric Brun | fd647d5 | 2021-02-12 20:03:38 +0100 | [diff] [blame] | 438 | */ |
| 439 | static void dns_session_io_handler(struct appctx *appctx) |
| 440 | { |
Willy Tarreau | 4596fe2 | 2022-05-17 19:07:51 +0200 | [diff] [blame^] | 441 | struct stconn *cs = appctx_cs(appctx); |
Willy Tarreau | 0d626a5 | 2022-05-04 20:41:19 +0200 | [diff] [blame] | 442 | struct dns_session *ds = appctx->svcctx; |
Emeric Brun | fd647d5 | 2021-02-12 20:03:38 +0100 | [diff] [blame] | 443 | struct ring *ring = &ds->ring; |
| 444 | struct buffer *buf = &ring->buf; |
| 445 | uint64_t msg_len; |
| 446 | int available_room; |
| 447 | size_t len, cnt, ofs; |
| 448 | int ret = 0; |
| 449 | |
| 450 | /* if stopping was requested, close immediately */ |
| 451 | if (unlikely(stopping)) |
| 452 | goto close; |
| 453 | |
| 454 | /* we want to be sure to not miss that we have been awaked for a shutdown */ |
| 455 | __ha_barrier_load(); |
| 456 | |
| 457 | /* that means the connection was requested to shutdown |
| 458 | * for instance idle expire */ |
| 459 | if (ds->shutdown) |
| 460 | goto close; |
| 461 | |
| 462 | /* an error was detected */ |
Christopher Faulet | 908628c | 2022-03-25 16:43:49 +0100 | [diff] [blame] | 463 | if (unlikely(cs_ic(cs)->flags & (CF_WRITE_ERROR|CF_SHUTW))) |
Emeric Brun | fd647d5 | 2021-02-12 20:03:38 +0100 | [diff] [blame] | 464 | goto close; |
| 465 | |
| 466 | /* con closed by server side, we will skip data write and drain data from channel */ |
Christopher Faulet | 908628c | 2022-03-25 16:43:49 +0100 | [diff] [blame] | 467 | if ((cs_oc(cs)->flags & CF_SHUTW)) { |
Emeric Brun | fd647d5 | 2021-02-12 20:03:38 +0100 | [diff] [blame] | 468 | goto read; |
| 469 | } |
| 470 | |
| 471 | /* if the connection is not established, inform the stream that we want |
| 472 | * to be notified whenever the connection completes. |
| 473 | */ |
Christopher Faulet | 62e7574 | 2022-03-31 09:16:34 +0200 | [diff] [blame] | 474 | if (cs_opposite(cs)->state < CS_ST_EST) { |
Christopher Faulet | a0bdec3 | 2022-04-04 07:51:21 +0200 | [diff] [blame] | 475 | cs_cant_get(cs); |
| 476 | cs_rx_conn_blk(cs); |
| 477 | cs_rx_endp_more(cs); |
Emeric Brun | fd647d5 | 2021-02-12 20:03:38 +0100 | [diff] [blame] | 478 | return; |
| 479 | } |
| 480 | |
| 481 | |
| 482 | ofs = ds->ofs; |
| 483 | |
| 484 | HA_RWLOCK_WRLOCK(DNS_LOCK, &ring->lock); |
| 485 | LIST_DEL_INIT(&appctx->wait_entry); |
| 486 | HA_RWLOCK_WRUNLOCK(DNS_LOCK, &ring->lock); |
| 487 | |
| 488 | HA_RWLOCK_RDLOCK(DNS_LOCK, &ring->lock); |
| 489 | |
| 490 | /* explanation for the initialization below: it would be better to do |
| 491 | * this in the parsing function but this would occasionally result in |
| 492 | * dropped events because we'd take a reference on the oldest message |
| 493 | * and keep it while being scheduled. Thus instead let's take it the |
| 494 | * first time we enter here so that we have a chance to pass many |
| 495 | * existing messages before grabbing a reference to a location. This |
| 496 | * value cannot be produced after initialization. |
| 497 | */ |
| 498 | if (unlikely(ofs == ~0)) { |
| 499 | ofs = 0; |
| 500 | |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 501 | HA_ATOMIC_INC(b_peek(buf, ofs)); |
Emeric Brun | fd647d5 | 2021-02-12 20:03:38 +0100 | [diff] [blame] | 502 | ofs += ring->ofs; |
| 503 | } |
| 504 | |
| 505 | /* in this loop, ofs always points to the counter byte that precedes |
| 506 | * the message so that we can take our reference there if we have to |
| 507 | * stop before the end (ret=0). |
| 508 | */ |
Christopher Faulet | 62e7574 | 2022-03-31 09:16:34 +0200 | [diff] [blame] | 509 | if (cs_opposite(cs)->state == CS_ST_EST) { |
Emeric Brun | fd647d5 | 2021-02-12 20:03:38 +0100 | [diff] [blame] | 510 | /* we were already there, adjust the offset to be relative to |
| 511 | * the buffer's head and remove us from the counter. |
| 512 | */ |
| 513 | ofs -= ring->ofs; |
| 514 | BUG_ON(ofs >= buf->size); |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 515 | HA_ATOMIC_DEC(b_peek(buf, ofs)); |
Emeric Brun | fd647d5 | 2021-02-12 20:03:38 +0100 | [diff] [blame] | 516 | |
| 517 | ret = 1; |
| 518 | while (ofs + 1 < b_data(buf)) { |
| 519 | struct dns_query *query; |
| 520 | uint16_t original_qid; |
| 521 | uint16_t new_qid; |
| 522 | |
| 523 | cnt = 1; |
| 524 | len = b_peek_varint(buf, ofs + cnt, &msg_len); |
| 525 | if (!len) |
| 526 | break; |
| 527 | cnt += len; |
| 528 | BUG_ON(msg_len + ofs + cnt + 1 > b_data(buf)); |
| 529 | |
| 530 | /* retrieve available room on output channel */ |
Christopher Faulet | 908628c | 2022-03-25 16:43:49 +0100 | [diff] [blame] | 531 | available_room = channel_recv_max(cs_ic(cs)); |
Emeric Brun | fd647d5 | 2021-02-12 20:03:38 +0100 | [diff] [blame] | 532 | |
| 533 | /* tx_msg_offset null means we are at the start of a new message */ |
| 534 | if (!ds->tx_msg_offset) { |
| 535 | uint16_t slen; |
| 536 | |
| 537 | /* check if there is enough room to put message len and query id */ |
| 538 | if (available_room < sizeof(slen) + sizeof(new_qid)) { |
Christopher Faulet | a0bdec3 | 2022-04-04 07:51:21 +0200 | [diff] [blame] | 539 | cs_rx_room_blk(cs); |
Emeric Brun | fd647d5 | 2021-02-12 20:03:38 +0100 | [diff] [blame] | 540 | ret = 0; |
| 541 | break; |
| 542 | } |
| 543 | |
| 544 | /* put msg len into then channel */ |
| 545 | slen = (uint16_t)msg_len; |
| 546 | slen = htons(slen); |
Christopher Faulet | 908628c | 2022-03-25 16:43:49 +0100 | [diff] [blame] | 547 | ci_putblk(cs_ic(cs), (char *)&slen, sizeof(slen)); |
Emeric Brun | fd647d5 | 2021-02-12 20:03:38 +0100 | [diff] [blame] | 548 | available_room -= sizeof(slen); |
| 549 | |
| 550 | /* backup original query id */ |
| 551 | len = b_getblk(buf, (char *)&original_qid, sizeof(original_qid), ofs + cnt); |
Emeric Brun | 538bb04 | 2021-02-15 13:58:06 +0100 | [diff] [blame] | 552 | if (!len) { |
| 553 | /* should never happen since messages are atomically |
| 554 | * written into ring |
| 555 | */ |
| 556 | ret = 0; |
| 557 | break; |
| 558 | } |
Emeric Brun | fd647d5 | 2021-02-12 20:03:38 +0100 | [diff] [blame] | 559 | |
| 560 | /* generates new query id */ |
| 561 | new_qid = ++ds->query_counter; |
| 562 | new_qid = htons(new_qid); |
| 563 | |
| 564 | /* put new query id into the channel */ |
Christopher Faulet | 908628c | 2022-03-25 16:43:49 +0100 | [diff] [blame] | 565 | ci_putblk(cs_ic(cs), (char *)&new_qid, sizeof(new_qid)); |
Emeric Brun | fd647d5 | 2021-02-12 20:03:38 +0100 | [diff] [blame] | 566 | available_room -= sizeof(new_qid); |
| 567 | |
| 568 | /* keep query id mapping */ |
| 569 | |
| 570 | query = pool_alloc(dns_query_pool); |
| 571 | if (query) { |
| 572 | query->qid.key = new_qid; |
| 573 | query->original_qid = original_qid; |
| 574 | query->expire = tick_add(now_ms, 5000); |
| 575 | LIST_INIT(&query->list); |
| 576 | if (LIST_ISEMPTY(&ds->queries)) { |
| 577 | /* enable task to handle expire */ |
| 578 | ds->task_exp->expire = query->expire; |
| 579 | /* ensure this will be executed by the same |
| 580 | * thread than ds_session_release |
| 581 | * to ensure session_release is free |
| 582 | * to destroy the task */ |
| 583 | task_queue(ds->task_exp); |
| 584 | } |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 585 | LIST_APPEND(&ds->queries, &query->list); |
Emeric Brun | fd647d5 | 2021-02-12 20:03:38 +0100 | [diff] [blame] | 586 | eb32_insert(&ds->query_ids, &query->qid); |
| 587 | ds->onfly_queries++; |
| 588 | } |
| 589 | |
| 590 | /* update the tx_offset to handle output in 16k streams */ |
| 591 | ds->tx_msg_offset = sizeof(original_qid); |
| 592 | |
| 593 | } |
| 594 | |
| 595 | /* check if it remains available room on output chan */ |
| 596 | if (unlikely(!available_room)) { |
Christopher Faulet | a0bdec3 | 2022-04-04 07:51:21 +0200 | [diff] [blame] | 597 | cs_rx_room_blk(cs); |
Emeric Brun | fd647d5 | 2021-02-12 20:03:38 +0100 | [diff] [blame] | 598 | ret = 0; |
| 599 | break; |
| 600 | } |
| 601 | |
| 602 | chunk_reset(&trash); |
| 603 | if ((msg_len - ds->tx_msg_offset) > available_room) { |
| 604 | /* remaining msg data is too large to be written in output channel at one time */ |
| 605 | |
| 606 | len = b_getblk(buf, trash.area, available_room, ofs + cnt + ds->tx_msg_offset); |
| 607 | |
| 608 | /* update offset to complete mesg forwarding later */ |
| 609 | ds->tx_msg_offset += len; |
| 610 | } |
| 611 | else { |
| 612 | /* remaining msg data can be written in output channel at one time */ |
| 613 | len = b_getblk(buf, trash.area, msg_len - ds->tx_msg_offset, ofs + cnt + ds->tx_msg_offset); |
| 614 | |
| 615 | /* reset tx_msg_offset to mark forward fully processed */ |
| 616 | ds->tx_msg_offset = 0; |
| 617 | } |
| 618 | trash.data += len; |
| 619 | |
Christopher Faulet | 908628c | 2022-03-25 16:43:49 +0100 | [diff] [blame] | 620 | if (ci_putchk(cs_ic(cs), &trash) == -1) { |
Emeric Brun | 743afee | 2021-02-15 14:12:06 +0100 | [diff] [blame] | 621 | /* should never happen since we |
| 622 | * check available_room is large |
Ilya Shipitsin | 0de36ad | 2021-02-20 00:23:36 +0500 | [diff] [blame] | 623 | * enough here. |
Emeric Brun | 743afee | 2021-02-15 14:12:06 +0100 | [diff] [blame] | 624 | */ |
Christopher Faulet | a0bdec3 | 2022-04-04 07:51:21 +0200 | [diff] [blame] | 625 | cs_rx_room_blk(cs); |
Emeric Brun | 743afee | 2021-02-15 14:12:06 +0100 | [diff] [blame] | 626 | ret = 0; |
| 627 | break; |
| 628 | } |
Emeric Brun | fd647d5 | 2021-02-12 20:03:38 +0100 | [diff] [blame] | 629 | |
| 630 | if (ds->tx_msg_offset) { |
| 631 | /* msg was not fully processed, we must be awake to drain pending data */ |
| 632 | |
Christopher Faulet | a0bdec3 | 2022-04-04 07:51:21 +0200 | [diff] [blame] | 633 | cs_rx_room_blk(cs); |
Emeric Brun | fd647d5 | 2021-02-12 20:03:38 +0100 | [diff] [blame] | 634 | ret = 0; |
| 635 | break; |
| 636 | } |
| 637 | /* switch to next message */ |
| 638 | ofs += cnt + msg_len; |
| 639 | } |
| 640 | |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 641 | HA_ATOMIC_INC(b_peek(buf, ofs)); |
Emeric Brun | fd647d5 | 2021-02-12 20:03:38 +0100 | [diff] [blame] | 642 | ofs += ring->ofs; |
| 643 | ds->ofs = ofs; |
| 644 | } |
| 645 | HA_RWLOCK_RDUNLOCK(DNS_LOCK, &ring->lock); |
| 646 | |
| 647 | if (ret) { |
| 648 | /* let's be woken up once new request to write arrived */ |
| 649 | HA_RWLOCK_WRLOCK(DNS_LOCK, &ring->lock); |
Willy Tarreau | 62e467c | 2021-10-20 11:02:13 +0200 | [diff] [blame] | 650 | BUG_ON(LIST_INLIST(&appctx->wait_entry)); |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 651 | LIST_APPEND(&ring->waiters, &appctx->wait_entry); |
Emeric Brun | fd647d5 | 2021-02-12 20:03:38 +0100 | [diff] [blame] | 652 | HA_RWLOCK_WRUNLOCK(DNS_LOCK, &ring->lock); |
Christopher Faulet | a0bdec3 | 2022-04-04 07:51:21 +0200 | [diff] [blame] | 653 | cs_rx_endp_done(cs); |
Emeric Brun | fd647d5 | 2021-02-12 20:03:38 +0100 | [diff] [blame] | 654 | } |
| 655 | |
| 656 | read: |
| 657 | |
Ilya Shipitsin | 0de36ad | 2021-02-20 00:23:36 +0500 | [diff] [blame] | 658 | /* if session is not a waiter it means there is no committed |
Emeric Brun | fd647d5 | 2021-02-12 20:03:38 +0100 | [diff] [blame] | 659 | * message into rx_buf and we are free to use it |
| 660 | * Note: we need a load barrier here to not miss the |
| 661 | * delete from the list |
| 662 | */ |
Emeric Brun | 7045590 | 2021-10-20 10:49:53 +0200 | [diff] [blame] | 663 | |
Willy Tarreau | dde1b44 | 2021-10-21 14:33:38 +0200 | [diff] [blame] | 664 | __ha_barrier_load(); |
| 665 | if (!LIST_INLIST_ATOMIC(&ds->waiter)) { |
Emeric Brun | fd647d5 | 2021-02-12 20:03:38 +0100 | [diff] [blame] | 666 | while (1) { |
| 667 | uint16_t query_id; |
| 668 | struct eb32_node *eb; |
| 669 | struct dns_query *query; |
| 670 | |
| 671 | if (!ds->rx_msg.len) { |
| 672 | /* next message len is not fully available into the channel */ |
Christopher Faulet | 908628c | 2022-03-25 16:43:49 +0100 | [diff] [blame] | 673 | if (co_data(cs_oc(cs)) < 2) |
Emeric Brun | fd647d5 | 2021-02-12 20:03:38 +0100 | [diff] [blame] | 674 | break; |
| 675 | |
| 676 | /* retrieve message len */ |
Christopher Faulet | 908628c | 2022-03-25 16:43:49 +0100 | [diff] [blame] | 677 | co_getblk(cs_oc(cs), (char *)&msg_len, 2, 0); |
Emeric Brun | fd647d5 | 2021-02-12 20:03:38 +0100 | [diff] [blame] | 678 | |
| 679 | /* mark as consumed */ |
Christopher Faulet | 908628c | 2022-03-25 16:43:49 +0100 | [diff] [blame] | 680 | co_skip(cs_oc(cs), 2); |
Emeric Brun | fd647d5 | 2021-02-12 20:03:38 +0100 | [diff] [blame] | 681 | |
| 682 | /* store message len */ |
| 683 | ds->rx_msg.len = ntohs(msg_len); |
| 684 | } |
| 685 | |
Christopher Faulet | 908628c | 2022-03-25 16:43:49 +0100 | [diff] [blame] | 686 | if (!co_data(cs_oc(cs))) { |
Emeric Brun | fd647d5 | 2021-02-12 20:03:38 +0100 | [diff] [blame] | 687 | /* we need more data but nothing is available */ |
| 688 | break; |
| 689 | } |
| 690 | |
Christopher Faulet | 908628c | 2022-03-25 16:43:49 +0100 | [diff] [blame] | 691 | if (co_data(cs_oc(cs)) + ds->rx_msg.offset < ds->rx_msg.len) { |
Emeric Brun | fd647d5 | 2021-02-12 20:03:38 +0100 | [diff] [blame] | 692 | /* message only partially available */ |
| 693 | |
| 694 | /* read available data */ |
Christopher Faulet | 908628c | 2022-03-25 16:43:49 +0100 | [diff] [blame] | 695 | co_getblk(cs_oc(cs), ds->rx_msg.area + ds->rx_msg.offset, co_data(cs_oc(cs)), 0); |
Emeric Brun | fd647d5 | 2021-02-12 20:03:38 +0100 | [diff] [blame] | 696 | |
| 697 | /* update message offset */ |
Christopher Faulet | 908628c | 2022-03-25 16:43:49 +0100 | [diff] [blame] | 698 | ds->rx_msg.offset += co_data(cs_oc(cs)); |
Emeric Brun | fd647d5 | 2021-02-12 20:03:38 +0100 | [diff] [blame] | 699 | |
| 700 | /* consume all pending data from the channel */ |
Christopher Faulet | 908628c | 2022-03-25 16:43:49 +0100 | [diff] [blame] | 701 | co_skip(cs_oc(cs), co_data(cs_oc(cs))); |
Emeric Brun | fd647d5 | 2021-02-12 20:03:38 +0100 | [diff] [blame] | 702 | |
| 703 | /* we need to wait for more data */ |
| 704 | break; |
| 705 | } |
| 706 | |
Ilya Shipitsin | 0de36ad | 2021-02-20 00:23:36 +0500 | [diff] [blame] | 707 | /* enough data is available into the channel to read the message until the end */ |
Emeric Brun | fd647d5 | 2021-02-12 20:03:38 +0100 | [diff] [blame] | 708 | |
| 709 | /* read from the channel until the end of the message */ |
Christopher Faulet | 908628c | 2022-03-25 16:43:49 +0100 | [diff] [blame] | 710 | co_getblk(cs_oc(cs), ds->rx_msg.area + ds->rx_msg.offset, ds->rx_msg.len - ds->rx_msg.offset, 0); |
Emeric Brun | fd647d5 | 2021-02-12 20:03:38 +0100 | [diff] [blame] | 711 | |
| 712 | /* consume all data until the end of the message from the channel */ |
Christopher Faulet | 908628c | 2022-03-25 16:43:49 +0100 | [diff] [blame] | 713 | co_skip(cs_oc(cs), ds->rx_msg.len - ds->rx_msg.offset); |
Emeric Brun | fd647d5 | 2021-02-12 20:03:38 +0100 | [diff] [blame] | 714 | |
| 715 | /* reset reader offset to 0 for next message reand */ |
| 716 | ds->rx_msg.offset = 0; |
| 717 | |
| 718 | /* try remap query id to original */ |
| 719 | memcpy(&query_id, ds->rx_msg.area, sizeof(query_id)); |
| 720 | eb = eb32_lookup(&ds->query_ids, query_id); |
| 721 | if (!eb) { |
| 722 | /* query id not found means we have an unknown corresponding |
| 723 | * request, perhaps server's bug or or the query reached |
| 724 | * timeout |
| 725 | */ |
| 726 | ds->rx_msg.len = 0; |
| 727 | continue; |
| 728 | } |
| 729 | |
| 730 | /* re-map the original query id set by the requester */ |
| 731 | query = eb32_entry(eb, struct dns_query, qid); |
| 732 | memcpy(ds->rx_msg.area, &query->original_qid, sizeof(query->original_qid)); |
| 733 | |
| 734 | /* remove query ids mapping from pending queries list/tree */ |
| 735 | eb32_delete(&query->qid); |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 736 | LIST_DELETE(&query->list); |
Emeric Brun | fd647d5 | 2021-02-12 20:03:38 +0100 | [diff] [blame] | 737 | pool_free(dns_query_pool, query); |
| 738 | ds->onfly_queries--; |
| 739 | |
Emeric Brun | fd647d5 | 2021-02-12 20:03:38 +0100 | [diff] [blame] | 740 | /* the dns_session is also added in queue of the |
| 741 | * wait_sess list where the task processing |
| 742 | * response will pop available responses |
| 743 | */ |
Willy Tarreau | dde1b44 | 2021-10-21 14:33:38 +0200 | [diff] [blame] | 744 | HA_SPIN_LOCK(DNS_LOCK, &ds->dss->lock); |
| 745 | |
Willy Tarreau | 62e467c | 2021-10-20 11:02:13 +0200 | [diff] [blame] | 746 | BUG_ON(LIST_INLIST(&ds->waiter)); |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 747 | LIST_APPEND(&ds->dss->wait_sess, &ds->waiter); |
Emeric Brun | fd647d5 | 2021-02-12 20:03:38 +0100 | [diff] [blame] | 748 | |
Willy Tarreau | dde1b44 | 2021-10-21 14:33:38 +0200 | [diff] [blame] | 749 | HA_SPIN_UNLOCK(DNS_LOCK, &ds->dss->lock); |
| 750 | |
Emeric Brun | fd647d5 | 2021-02-12 20:03:38 +0100 | [diff] [blame] | 751 | /* awake the task processing the responses */ |
| 752 | task_wakeup(ds->dss->task_rsp, TASK_WOKEN_INIT); |
| 753 | |
| 754 | break; |
| 755 | } |
| 756 | |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 757 | if (!LIST_INLIST(&ds->waiter)) { |
Emeric Brun | fd647d5 | 2021-02-12 20:03:38 +0100 | [diff] [blame] | 758 | /* there is no more pending data to read and the con was closed by the server side */ |
Christopher Faulet | 908628c | 2022-03-25 16:43:49 +0100 | [diff] [blame] | 759 | if (!co_data(cs_oc(cs)) && (cs_oc(cs)->flags & CF_SHUTW)) { |
Emeric Brun | fd647d5 | 2021-02-12 20:03:38 +0100 | [diff] [blame] | 760 | goto close; |
| 761 | } |
| 762 | } |
| 763 | |
| 764 | } |
| 765 | |
Emeric Brun | fd647d5 | 2021-02-12 20:03:38 +0100 | [diff] [blame] | 766 | return; |
| 767 | close: |
Christopher Faulet | da098e6 | 2022-03-31 17:44:45 +0200 | [diff] [blame] | 768 | cs_shutw(cs); |
| 769 | cs_shutr(cs); |
Christopher Faulet | 908628c | 2022-03-25 16:43:49 +0100 | [diff] [blame] | 770 | cs_ic(cs)->flags |= CF_READ_NULL; |
Emeric Brun | fd647d5 | 2021-02-12 20:03:38 +0100 | [diff] [blame] | 771 | } |
| 772 | |
| 773 | void dns_queries_flush(struct dns_session *ds) |
| 774 | { |
| 775 | struct dns_query *query, *queryb; |
| 776 | |
| 777 | list_for_each_entry_safe(query, queryb, &ds->queries, list) { |
| 778 | eb32_delete(&query->qid); |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 779 | LIST_DELETE(&query->list); |
Emeric Brun | fd647d5 | 2021-02-12 20:03:38 +0100 | [diff] [blame] | 780 | pool_free(dns_query_pool, query); |
| 781 | } |
| 782 | } |
| 783 | |
| 784 | void dns_session_free(struct dns_session *ds) |
| 785 | { |
| 786 | if (ds->rx_msg.area) |
| 787 | pool_free(dns_msg_buf, ds->rx_msg.area); |
| 788 | if (ds->tx_ring_area) |
| 789 | pool_free(dns_msg_buf, ds->tx_ring_area); |
| 790 | if (ds->task_exp) |
| 791 | task_destroy(ds->task_exp); |
| 792 | |
| 793 | dns_queries_flush(ds); |
| 794 | |
Emeric Brun | d20dc21 | 2021-10-19 15:40:10 +0200 | [diff] [blame] | 795 | /* Ensure to remove this session from external lists |
| 796 | * Note: we are under the lock of dns_stream_server |
| 797 | * which own the heads of those lists. |
| 798 | */ |
| 799 | LIST_DEL_INIT(&ds->waiter); |
| 800 | LIST_DEL_INIT(&ds->list); |
| 801 | |
Emeric Brun | fd647d5 | 2021-02-12 20:03:38 +0100 | [diff] [blame] | 802 | ds->dss->cur_conns--; |
| 803 | /* Note: this is useless to update |
| 804 | * max_active_conns here because |
| 805 | * we decrease the value |
| 806 | */ |
Willy Tarreau | 62e467c | 2021-10-20 11:02:13 +0200 | [diff] [blame] | 807 | |
| 808 | BUG_ON(!LIST_ISEMPTY(&ds->list)); |
| 809 | BUG_ON(!LIST_ISEMPTY(&ds->waiter)); |
| 810 | BUG_ON(!LIST_ISEMPTY(&ds->queries)); |
| 811 | BUG_ON(!LIST_ISEMPTY(&ds->ring.waiters)); |
| 812 | BUG_ON(!eb_is_empty(&ds->query_ids)); |
Emeric Brun | fd647d5 | 2021-02-12 20:03:38 +0100 | [diff] [blame] | 813 | pool_free(dns_session_pool, ds); |
| 814 | } |
| 815 | |
| 816 | static struct appctx *dns_session_create(struct dns_session *ds); |
| 817 | |
Christopher Faulet | 9223851 | 2022-05-12 15:24:46 +0200 | [diff] [blame] | 818 | static int dns_session_init(struct appctx *appctx) |
| 819 | { |
| 820 | struct dns_session *ds = appctx->svcctx; |
| 821 | struct stream *s; |
| 822 | struct sockaddr_storage *addr = NULL; |
| 823 | |
| 824 | if (!sockaddr_alloc(&addr, &ds->dss->srv->addr, sizeof(ds->dss->srv->addr))) |
| 825 | goto error; |
| 826 | |
| 827 | if (appctx_finalize_startup(appctx, ds->dss->srv->proxy, &BUF_NULL) == -1) |
| 828 | goto error; |
| 829 | |
| 830 | s = appctx_strm(appctx); |
| 831 | s->csb->dst = addr; |
| 832 | s->csb->flags |= CS_FL_NOLINGER; |
| 833 | s->target = &ds->dss->srv->obj_type; |
| 834 | s->flags = SF_ASSIGNED; |
| 835 | |
| 836 | s->do_log = NULL; |
| 837 | s->uniq_id = 0; |
| 838 | |
| 839 | s->res.flags |= CF_READ_DONTWAIT; |
| 840 | /* for rto and rex to eternity to not expire on idle recv: |
| 841 | * We are using a syslog server. |
| 842 | */ |
| 843 | s->res.rto = TICK_ETERNITY; |
| 844 | s->res.rex = TICK_ETERNITY; |
| 845 | |
| 846 | ds->appctx = appctx; |
| 847 | return 0; |
| 848 | |
| 849 | error: |
| 850 | return -1; |
| 851 | } |
| 852 | |
Emeric Brun | fd647d5 | 2021-02-12 20:03:38 +0100 | [diff] [blame] | 853 | /* |
| 854 | * Function to release a DNS tcp session |
| 855 | */ |
| 856 | static void dns_session_release(struct appctx *appctx) |
| 857 | { |
Willy Tarreau | 0d626a5 | 2022-05-04 20:41:19 +0200 | [diff] [blame] | 858 | struct dns_session *ds = appctx->svcctx; |
Willy Tarreau | e3e648c | 2021-02-24 17:38:46 +0100 | [diff] [blame] | 859 | struct dns_stream_server *dss __maybe_unused; |
Emeric Brun | fd647d5 | 2021-02-12 20:03:38 +0100 | [diff] [blame] | 860 | |
| 861 | if (!ds) |
| 862 | return; |
| 863 | |
Willy Tarreau | b56a878 | 2021-10-20 14:38:43 +0200 | [diff] [blame] | 864 | /* We do not call ring_appctx_detach here |
| 865 | * because we want to keep readers counters |
| 866 | * to retry a conn with a different appctx. |
| 867 | */ |
| 868 | HA_RWLOCK_WRLOCK(DNS_LOCK, &ds->ring.lock); |
| 869 | LIST_DEL_INIT(&appctx->wait_entry); |
| 870 | HA_RWLOCK_WRUNLOCK(DNS_LOCK, &ds->ring.lock); |
| 871 | |
Emeric Brun | fd647d5 | 2021-02-12 20:03:38 +0100 | [diff] [blame] | 872 | dss = ds->dss; |
| 873 | |
| 874 | HA_SPIN_LOCK(DNS_LOCK, &dss->lock); |
| 875 | LIST_DEL_INIT(&ds->list); |
| 876 | |
| 877 | if (stopping) { |
| 878 | dns_session_free(ds); |
| 879 | HA_SPIN_UNLOCK(DNS_LOCK, &dss->lock); |
| 880 | return; |
| 881 | } |
| 882 | |
| 883 | if (!ds->nb_queries) { |
| 884 | /* this is an idle session */ |
| 885 | /* Note: this is useless to update max_active_sess |
| 886 | * here because we decrease idle_conns but |
| 887 | * dns_session_free decrease curconns |
| 888 | */ |
| 889 | |
| 890 | ds->dss->idle_conns--; |
| 891 | dns_session_free(ds); |
| 892 | HA_SPIN_UNLOCK(DNS_LOCK, &dss->lock); |
| 893 | return; |
| 894 | } |
| 895 | |
| 896 | if (ds->onfly_queries == ds->nb_queries) { |
| 897 | /* the session can be released because |
| 898 | * it means that all queries AND |
| 899 | * responses are in fly */ |
| 900 | dns_session_free(ds); |
| 901 | HA_SPIN_UNLOCK(DNS_LOCK, &dss->lock); |
| 902 | return; |
| 903 | } |
| 904 | |
Emeric Brun | fd647d5 | 2021-02-12 20:03:38 +0100 | [diff] [blame] | 905 | /* if there is no pending complete response |
| 906 | * message, ensure to reset |
| 907 | * message offsets if the session |
| 908 | * was closed with an incomplete pending response |
| 909 | */ |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 910 | if (!LIST_INLIST(&ds->waiter)) |
Emeric Brun | fd647d5 | 2021-02-12 20:03:38 +0100 | [diff] [blame] | 911 | ds->rx_msg.len = ds->rx_msg.offset = 0; |
| 912 | |
| 913 | /* we flush pending sent queries because we never |
| 914 | * have responses |
| 915 | */ |
| 916 | ds->nb_queries -= ds->onfly_queries; |
| 917 | dns_queries_flush(ds); |
| 918 | |
| 919 | /* reset offset to be sure to start from message start */ |
| 920 | ds->tx_msg_offset = 0; |
| 921 | |
| 922 | /* here the ofs and the attached counter |
| 923 | * are kept unchanged |
| 924 | */ |
| 925 | |
| 926 | /* Create a new appctx, We hope we can |
| 927 | * create from the release callback! */ |
| 928 | ds->appctx = dns_session_create(ds); |
| 929 | if (!ds->appctx) { |
| 930 | dns_session_free(ds); |
| 931 | HA_SPIN_UNLOCK(DNS_LOCK, &dss->lock); |
| 932 | return; |
| 933 | } |
| 934 | |
| 935 | if (ds->nb_queries < DNS_STREAM_MAX_PIPELINED_REQ) |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 936 | LIST_INSERT(&ds->dss->free_sess, &ds->list); |
Emeric Brun | fd647d5 | 2021-02-12 20:03:38 +0100 | [diff] [blame] | 937 | |
| 938 | HA_SPIN_UNLOCK(DNS_LOCK, &dss->lock); |
| 939 | } |
| 940 | |
| 941 | /* DNS tcp session applet */ |
| 942 | static struct applet dns_session_applet = { |
| 943 | .obj_type = OBJ_TYPE_APPLET, |
| 944 | .name = "<STRMDNS>", /* used for logging */ |
| 945 | .fct = dns_session_io_handler, |
Christopher Faulet | 9223851 | 2022-05-12 15:24:46 +0200 | [diff] [blame] | 946 | .init = dns_session_init, |
Emeric Brun | fd647d5 | 2021-02-12 20:03:38 +0100 | [diff] [blame] | 947 | .release = dns_session_release, |
| 948 | }; |
| 949 | |
| 950 | /* |
| 951 | * Function used to create an appctx for a DNS session |
Willy Tarreau | 0d626a5 | 2022-05-04 20:41:19 +0200 | [diff] [blame] | 952 | * It sets its context into appctx->svcctx. |
Emeric Brun | fd647d5 | 2021-02-12 20:03:38 +0100 | [diff] [blame] | 953 | */ |
| 954 | static struct appctx *dns_session_create(struct dns_session *ds) |
| 955 | { |
| 956 | struct appctx *appctx; |
Emeric Brun | fd647d5 | 2021-02-12 20:03:38 +0100 | [diff] [blame] | 957 | |
Christopher Faulet | 6095d57 | 2022-05-16 17:09:48 +0200 | [diff] [blame] | 958 | appctx = appctx_new_here(&dns_session_applet, NULL); |
Christopher Faulet | 2479e5f | 2022-01-19 14:50:11 +0100 | [diff] [blame] | 959 | if (!appctx) |
Christopher Faulet | a9e8b39 | 2022-03-23 11:01:09 +0100 | [diff] [blame] | 960 | goto out_close; |
Willy Tarreau | 0d626a5 | 2022-05-04 20:41:19 +0200 | [diff] [blame] | 961 | appctx->svcctx = (void *)ds; |
Emeric Brun | fd647d5 | 2021-02-12 20:03:38 +0100 | [diff] [blame] | 962 | |
Christopher Faulet | 9223851 | 2022-05-12 15:24:46 +0200 | [diff] [blame] | 963 | if (appctx_init(appctx) == -1) { |
Christopher Faulet | 13a35e5 | 2021-12-20 15:34:16 +0100 | [diff] [blame] | 964 | ha_alert("out of memory in dns_session_create().\n"); |
Emeric Brun | fd647d5 | 2021-02-12 20:03:38 +0100 | [diff] [blame] | 965 | goto out_free_appctx; |
Christopher Faulet | 13a35e5 | 2021-12-20 15:34:16 +0100 | [diff] [blame] | 966 | } |
| 967 | |
Emeric Brun | fd647d5 | 2021-02-12 20:03:38 +0100 | [diff] [blame] | 968 | return appctx; |
| 969 | |
| 970 | /* Error unrolling */ |
Emeric Brun | fd647d5 | 2021-02-12 20:03:38 +0100 | [diff] [blame] | 971 | out_free_appctx: |
Christopher Faulet | 9223851 | 2022-05-12 15:24:46 +0200 | [diff] [blame] | 972 | appctx_free_on_early_error(appctx); |
Emeric Brun | fd647d5 | 2021-02-12 20:03:38 +0100 | [diff] [blame] | 973 | out_close: |
| 974 | return NULL; |
| 975 | } |
| 976 | |
| 977 | /* Task processing expiration of unresponded queries, this one is supposed |
| 978 | * to be stuck on the same thread than the appctx handler |
| 979 | */ |
Willy Tarreau | 144f84a | 2021-03-02 16:09:26 +0100 | [diff] [blame] | 980 | static struct task *dns_process_query_exp(struct task *t, void *context, unsigned int state) |
Emeric Brun | fd647d5 | 2021-02-12 20:03:38 +0100 | [diff] [blame] | 981 | { |
| 982 | struct dns_session *ds = (struct dns_session *)context; |
| 983 | struct dns_query *query, *queryb; |
| 984 | |
| 985 | t->expire = TICK_ETERNITY; |
| 986 | |
| 987 | list_for_each_entry_safe(query, queryb, &ds->queries, list) { |
| 988 | if (tick_is_expired(query->expire, now_ms)) { |
| 989 | eb32_delete(&query->qid); |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 990 | LIST_DELETE(&query->list); |
Emeric Brun | fd647d5 | 2021-02-12 20:03:38 +0100 | [diff] [blame] | 991 | pool_free(dns_query_pool, query); |
| 992 | ds->onfly_queries--; |
| 993 | } |
| 994 | else { |
| 995 | t->expire = query->expire; |
| 996 | break; |
| 997 | } |
| 998 | } |
| 999 | |
| 1000 | return t; |
| 1001 | } |
| 1002 | |
| 1003 | /* Task processing expiration of idle sessions */ |
Willy Tarreau | 144f84a | 2021-03-02 16:09:26 +0100 | [diff] [blame] | 1004 | static struct task *dns_process_idle_exp(struct task *t, void *context, unsigned int state) |
Emeric Brun | fd647d5 | 2021-02-12 20:03:38 +0100 | [diff] [blame] | 1005 | { |
| 1006 | struct dns_stream_server *dss = (struct dns_stream_server *)context; |
| 1007 | struct dns_session *ds, *dsb; |
| 1008 | int target = 0; |
| 1009 | int cur_active_conns; |
| 1010 | |
| 1011 | HA_SPIN_LOCK(DNS_LOCK, &dss->lock); |
| 1012 | |
| 1013 | |
| 1014 | cur_active_conns = dss->cur_conns - dss->idle_conns; |
| 1015 | if (cur_active_conns > dss->max_active_conns) |
| 1016 | dss->max_active_conns = cur_active_conns; |
| 1017 | |
| 1018 | target = (dss->max_active_conns - cur_active_conns) / 2; |
| 1019 | list_for_each_entry_safe(ds, dsb, &dss->idle_sess, list) { |
| 1020 | if (!target) |
| 1021 | break; |
| 1022 | |
| 1023 | /* remove conn to pending list to ensure it won't be reused */ |
| 1024 | LIST_DEL_INIT(&ds->list); |
| 1025 | |
| 1026 | /* force session shutdown */ |
| 1027 | ds->shutdown = 1; |
| 1028 | |
Ilya Shipitsin | 0de36ad | 2021-02-20 00:23:36 +0500 | [diff] [blame] | 1029 | /* to be sure that the appctx won't miss shutdown */ |
Emeric Brun | fd647d5 | 2021-02-12 20:03:38 +0100 | [diff] [blame] | 1030 | __ha_barrier_store(); |
| 1031 | |
| 1032 | /* wake appctx to perform the shutdown */ |
| 1033 | appctx_wakeup(ds->appctx); |
| 1034 | } |
| 1035 | |
| 1036 | /* reset max to current active conns */ |
| 1037 | dss->max_active_conns = cur_active_conns; |
| 1038 | |
| 1039 | HA_SPIN_UNLOCK(DNS_LOCK, &dss->lock); |
| 1040 | |
| 1041 | t->expire = tick_add(now_ms, 5000); |
| 1042 | |
| 1043 | return t; |
| 1044 | } |
| 1045 | |
| 1046 | struct dns_session *dns_session_new(struct dns_stream_server *dss) |
| 1047 | { |
| 1048 | struct dns_session *ds; |
| 1049 | |
| 1050 | if (dss->maxconn && (dss->maxconn <= dss->cur_conns)) |
| 1051 | return NULL; |
| 1052 | |
| 1053 | ds = pool_alloc(dns_session_pool); |
| 1054 | if (!ds) |
| 1055 | return NULL; |
| 1056 | |
| 1057 | ds->ofs = ~0; |
| 1058 | ds->dss = dss; |
| 1059 | LIST_INIT(&ds->list); |
| 1060 | LIST_INIT(&ds->queries); |
| 1061 | LIST_INIT(&ds->waiter); |
| 1062 | ds->rx_msg.offset = ds->rx_msg.len = 0; |
| 1063 | ds->rx_msg.area = NULL; |
| 1064 | ds->tx_ring_area = NULL; |
| 1065 | ds->task_exp = NULL; |
| 1066 | ds->appctx = NULL; |
| 1067 | ds->shutdown = 0; |
| 1068 | ds->nb_queries = 0; |
| 1069 | ds->query_ids = EB_ROOT_UNIQUE; |
| 1070 | ds->rx_msg.area = pool_alloc(dns_msg_buf); |
| 1071 | if (!ds->rx_msg.area) |
| 1072 | goto error; |
| 1073 | |
| 1074 | ds->tx_ring_area = pool_alloc(dns_msg_buf); |
| 1075 | if (!ds->tx_ring_area) |
| 1076 | goto error; |
| 1077 | |
| 1078 | ring_init(&ds->ring, ds->tx_ring_area, DNS_TCP_MSG_RING_MAX_SIZE); |
Christopher Faulet | 1a1b674 | 2021-03-04 16:53:27 +0100 | [diff] [blame] | 1079 | /* never fail because it is the first watcher attached to the ring */ |
| 1080 | DISGUISE(ring_attach(&ds->ring)); |
Emeric Brun | fd647d5 | 2021-02-12 20:03:38 +0100 | [diff] [blame] | 1081 | |
Willy Tarreau | beeabf5 | 2021-10-01 18:23:30 +0200 | [diff] [blame] | 1082 | if ((ds->task_exp = task_new_here()) == NULL) |
Emeric Brun | fd647d5 | 2021-02-12 20:03:38 +0100 | [diff] [blame] | 1083 | goto error; |
| 1084 | |
| 1085 | ds->task_exp->process = dns_process_query_exp; |
| 1086 | ds->task_exp->context = ds; |
| 1087 | |
| 1088 | ds->appctx = dns_session_create(ds); |
| 1089 | if (!ds->appctx) |
| 1090 | goto error; |
| 1091 | |
| 1092 | dss->cur_conns++; |
| 1093 | |
| 1094 | return ds; |
| 1095 | |
| 1096 | error: |
| 1097 | if (ds->task_exp) |
| 1098 | task_destroy(ds->task_exp); |
| 1099 | if (ds->rx_msg.area) |
| 1100 | pool_free(dns_msg_buf, ds->rx_msg.area); |
| 1101 | if (ds->tx_ring_area) |
| 1102 | pool_free(dns_msg_buf, ds->tx_ring_area); |
| 1103 | |
| 1104 | pool_free(dns_session_pool, ds); |
| 1105 | |
| 1106 | return NULL; |
| 1107 | } |
| 1108 | |
| 1109 | /* |
| 1110 | * Task used to consume pending messages from nameserver ring |
| 1111 | * and forward them to dns_session ring. |
| 1112 | * Note: If no slot found a new dns_session is allocated |
| 1113 | */ |
Willy Tarreau | 144f84a | 2021-03-02 16:09:26 +0100 | [diff] [blame] | 1114 | static struct task *dns_process_req(struct task *t, void *context, unsigned int state) |
Emeric Brun | fd647d5 | 2021-02-12 20:03:38 +0100 | [diff] [blame] | 1115 | { |
| 1116 | struct dns_nameserver *ns = (struct dns_nameserver *)context; |
| 1117 | struct dns_stream_server *dss = ns->stream; |
| 1118 | struct ring *ring = dss->ring_req; |
| 1119 | struct buffer *buf = &ring->buf; |
| 1120 | uint64_t msg_len; |
| 1121 | size_t len, cnt, ofs; |
| 1122 | struct dns_session *ds, *ads; |
| 1123 | HA_SPIN_LOCK(DNS_LOCK, &dss->lock); |
| 1124 | |
| 1125 | ofs = dss->ofs_req; |
| 1126 | |
| 1127 | HA_RWLOCK_RDLOCK(DNS_LOCK, &ring->lock); |
| 1128 | |
| 1129 | /* explanation for the initialization below: it would be better to do |
| 1130 | * this in the parsing function but this would occasionally result in |
| 1131 | * dropped events because we'd take a reference on the oldest message |
| 1132 | * and keep it while being scheduled. Thus instead let's take it the |
| 1133 | * first time we enter here so that we have a chance to pass many |
| 1134 | * existing messages before grabbing a reference to a location. This |
| 1135 | * value cannot be produced after initialization. |
| 1136 | */ |
| 1137 | if (unlikely(ofs == ~0)) { |
| 1138 | ofs = 0; |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 1139 | HA_ATOMIC_INC(b_peek(buf, ofs)); |
Emeric Brun | fd647d5 | 2021-02-12 20:03:38 +0100 | [diff] [blame] | 1140 | ofs += ring->ofs; |
| 1141 | } |
| 1142 | |
| 1143 | /* we were already there, adjust the offset to be relative to |
| 1144 | * the buffer's head and remove us from the counter. |
| 1145 | */ |
| 1146 | ofs -= ring->ofs; |
| 1147 | BUG_ON(ofs >= buf->size); |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 1148 | HA_ATOMIC_DEC(b_peek(buf, ofs)); |
Emeric Brun | fd647d5 | 2021-02-12 20:03:38 +0100 | [diff] [blame] | 1149 | |
| 1150 | while (ofs + 1 < b_data(buf)) { |
| 1151 | struct ist myist; |
| 1152 | |
| 1153 | cnt = 1; |
| 1154 | len = b_peek_varint(buf, ofs + cnt, &msg_len); |
| 1155 | if (!len) |
| 1156 | break; |
| 1157 | cnt += len; |
| 1158 | BUG_ON(msg_len + ofs + cnt + 1 > b_data(buf)); |
| 1159 | if (unlikely(msg_len > DNS_TCP_MSG_MAX_SIZE)) { |
| 1160 | /* too large a message to ever fit, let's skip it */ |
| 1161 | ofs += cnt + msg_len; |
| 1162 | continue; |
| 1163 | } |
| 1164 | |
| 1165 | len = b_getblk(buf, dns_msg_trash, msg_len, ofs + cnt); |
| 1166 | |
Tim Duesterhus | 92c696e | 2021-02-28 16:11:36 +0100 | [diff] [blame] | 1167 | myist = ist2(dns_msg_trash, len); |
Emeric Brun | fd647d5 | 2021-02-12 20:03:38 +0100 | [diff] [blame] | 1168 | |
| 1169 | ads = NULL; |
Ilya Shipitsin | 0de36ad | 2021-02-20 00:23:36 +0500 | [diff] [blame] | 1170 | /* try to push request into active sess with free slot */ |
Emeric Brun | fd647d5 | 2021-02-12 20:03:38 +0100 | [diff] [blame] | 1171 | if (!LIST_ISEMPTY(&dss->free_sess)) { |
| 1172 | ds = LIST_NEXT(&dss->free_sess, struct dns_session *, list); |
| 1173 | |
| 1174 | if (ring_write(&ds->ring, DNS_TCP_MSG_MAX_SIZE, NULL, 0, &myist, 1) > 0) { |
| 1175 | ds->nb_queries++; |
| 1176 | if (ds->nb_queries >= DNS_STREAM_MAX_PIPELINED_REQ) |
| 1177 | LIST_DEL_INIT(&ds->list); |
| 1178 | ads = ds; |
| 1179 | } |
| 1180 | else { |
| 1181 | /* it means we were unable to put a request in this slot, |
| 1182 | * it may be close to be full so we put it at the end |
| 1183 | * of free conn list */ |
| 1184 | LIST_DEL_INIT(&ds->list); |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 1185 | LIST_APPEND(&dss->free_sess, &ds->list); |
Emeric Brun | fd647d5 | 2021-02-12 20:03:38 +0100 | [diff] [blame] | 1186 | } |
| 1187 | } |
| 1188 | |
| 1189 | if (!ads) { |
Ilya Shipitsin | 0de36ad | 2021-02-20 00:23:36 +0500 | [diff] [blame] | 1190 | /* try to push request into idle, this one should have enough free space */ |
Emeric Brun | fd647d5 | 2021-02-12 20:03:38 +0100 | [diff] [blame] | 1191 | if (!LIST_ISEMPTY(&dss->idle_sess)) { |
| 1192 | ds = LIST_NEXT(&dss->idle_sess, struct dns_session *, list); |
| 1193 | |
| 1194 | /* ring is empty so this ring_write should never fail */ |
| 1195 | ring_write(&ds->ring, DNS_TCP_MSG_MAX_SIZE, NULL, 0, &myist, 1); |
| 1196 | ds->nb_queries++; |
| 1197 | LIST_DEL_INIT(&ds->list); |
| 1198 | |
| 1199 | ds->dss->idle_conns--; |
| 1200 | |
| 1201 | /* we may have to update the max_active_conns */ |
| 1202 | if (ds->dss->max_active_conns < ds->dss->cur_conns - ds->dss->idle_conns) |
| 1203 | ds->dss->max_active_conns = ds->dss->cur_conns - ds->dss->idle_conns; |
| 1204 | |
| 1205 | /* since we may unable to find a free list to handle |
| 1206 | * this request, this request may be large and fill |
| 1207 | * the ring buffer so we prefer to put at the end of free |
| 1208 | * list. */ |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 1209 | LIST_APPEND(&dss->free_sess, &ds->list); |
Emeric Brun | fd647d5 | 2021-02-12 20:03:38 +0100 | [diff] [blame] | 1210 | ads = ds; |
| 1211 | } |
| 1212 | } |
| 1213 | |
Ilya Shipitsin | 0de36ad | 2021-02-20 00:23:36 +0500 | [diff] [blame] | 1214 | /* we didn't find a session available with large enough room */ |
Emeric Brun | fd647d5 | 2021-02-12 20:03:38 +0100 | [diff] [blame] | 1215 | if (!ads) { |
| 1216 | /* allocate a new session */ |
| 1217 | ads = dns_session_new(dss); |
| 1218 | if (ads) { |
| 1219 | /* ring is empty so this ring_write should never fail */ |
| 1220 | ring_write(&ads->ring, DNS_TCP_MSG_MAX_SIZE, NULL, 0, &myist, 1); |
| 1221 | ads->nb_queries++; |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 1222 | LIST_INSERT(&dss->free_sess, &ads->list); |
Emeric Brun | fd647d5 | 2021-02-12 20:03:38 +0100 | [diff] [blame] | 1223 | } |
| 1224 | else |
| 1225 | ns->counters->snd_error++; |
| 1226 | } |
| 1227 | |
| 1228 | if (ads) |
| 1229 | ns->counters->sent++; |
| 1230 | |
| 1231 | ofs += cnt + len; |
| 1232 | } |
| 1233 | |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 1234 | HA_ATOMIC_INC(b_peek(buf, ofs)); |
Emeric Brun | fd647d5 | 2021-02-12 20:03:38 +0100 | [diff] [blame] | 1235 | ofs += ring->ofs; |
| 1236 | dss->ofs_req = ofs; |
| 1237 | HA_RWLOCK_RDUNLOCK(DNS_LOCK, &ring->lock); |
| 1238 | |
| 1239 | |
| 1240 | HA_SPIN_UNLOCK(DNS_LOCK, &dss->lock); |
| 1241 | return t; |
| 1242 | } |
| 1243 | |
| 1244 | /* |
| 1245 | * Task used to consume response |
| 1246 | * Note: upper layer callback is called |
| 1247 | */ |
Willy Tarreau | 144f84a | 2021-03-02 16:09:26 +0100 | [diff] [blame] | 1248 | static struct task *dns_process_rsp(struct task *t, void *context, unsigned int state) |
Emeric Brun | fd647d5 | 2021-02-12 20:03:38 +0100 | [diff] [blame] | 1249 | { |
| 1250 | struct dns_nameserver *ns = (struct dns_nameserver *)context; |
| 1251 | |
| 1252 | ns->process_responses(ns); |
| 1253 | |
| 1254 | return t; |
| 1255 | } |
| 1256 | |
| 1257 | /* Function used to initialize an TCP nameserver */ |
| 1258 | int dns_stream_init(struct dns_nameserver *ns, struct server *srv) |
| 1259 | { |
| 1260 | struct dns_stream_server *dss = NULL; |
| 1261 | |
| 1262 | dss = calloc(1, sizeof(*dss)); |
| 1263 | if (!dss) { |
| 1264 | ha_alert("memory allocation error initializing dns tcp server '%s'.\n", srv->id); |
| 1265 | goto out; |
| 1266 | } |
| 1267 | |
| 1268 | dss->srv = srv; |
| 1269 | dss->maxconn = srv->maxconn; |
| 1270 | |
| 1271 | dss->ofs_req = ~0; /* init ring offset */ |
| 1272 | dss->ring_req = ring_new(2*DNS_TCP_MSG_RING_MAX_SIZE); |
| 1273 | if (!dss->ring_req) { |
| 1274 | ha_alert("memory allocation error initializing the ring for dns tcp server '%s'.\n", srv->id); |
| 1275 | goto out; |
| 1276 | } |
| 1277 | /* Create the task associated to the resolver target handling conns */ |
Willy Tarreau | beeabf5 | 2021-10-01 18:23:30 +0200 | [diff] [blame] | 1278 | if ((dss->task_req = task_new_anywhere()) == NULL) { |
Emeric Brun | fd647d5 | 2021-02-12 20:03:38 +0100 | [diff] [blame] | 1279 | ha_alert("memory allocation error initializing the ring for dns tcp server '%s'.\n", srv->id); |
| 1280 | goto out; |
| 1281 | } |
| 1282 | |
| 1283 | /* Update task's parameters */ |
| 1284 | dss->task_req->process = dns_process_req; |
| 1285 | dss->task_req->context = ns; |
| 1286 | |
| 1287 | /* attach the task as reader */ |
| 1288 | if (!ring_attach(dss->ring_req)) { |
| 1289 | /* mark server attached to the ring */ |
| 1290 | ha_alert("server '%s': too many watchers for ring. this should never happen.\n", srv->id); |
| 1291 | goto out; |
| 1292 | } |
| 1293 | |
| 1294 | /* Create the task associated to the resolver target handling conns */ |
Willy Tarreau | beeabf5 | 2021-10-01 18:23:30 +0200 | [diff] [blame] | 1295 | if ((dss->task_rsp = task_new_anywhere()) == NULL) { |
Emeric Brun | fd647d5 | 2021-02-12 20:03:38 +0100 | [diff] [blame] | 1296 | ha_alert("memory allocation error initializing the ring for dns tcp server '%s'.\n", srv->id); |
| 1297 | goto out; |
| 1298 | } |
| 1299 | |
| 1300 | /* Update task's parameters */ |
| 1301 | dss->task_rsp->process = dns_process_rsp; |
| 1302 | dss->task_rsp->context = ns; |
| 1303 | |
| 1304 | /* Create the task associated to the resolver target handling conns */ |
Willy Tarreau | beeabf5 | 2021-10-01 18:23:30 +0200 | [diff] [blame] | 1305 | if ((dss->task_idle = task_new_anywhere()) == NULL) { |
Emeric Brun | fd647d5 | 2021-02-12 20:03:38 +0100 | [diff] [blame] | 1306 | ha_alert("memory allocation error initializing the ring for dns tcp server '%s'.\n", srv->id); |
| 1307 | goto out; |
| 1308 | } |
| 1309 | |
| 1310 | /* Update task's parameters */ |
| 1311 | dss->task_idle->process = dns_process_idle_exp; |
| 1312 | dss->task_idle->context = dss; |
| 1313 | dss->task_idle->expire = tick_add(now_ms, 5000); |
| 1314 | |
Ilya Shipitsin | 0de36ad | 2021-02-20 00:23:36 +0500 | [diff] [blame] | 1315 | /* let start the task to free idle conns immediately */ |
Emeric Brun | fd647d5 | 2021-02-12 20:03:38 +0100 | [diff] [blame] | 1316 | task_queue(dss->task_idle); |
| 1317 | |
| 1318 | LIST_INIT(&dss->free_sess); |
| 1319 | LIST_INIT(&dss->idle_sess); |
| 1320 | LIST_INIT(&dss->wait_sess); |
| 1321 | HA_SPIN_INIT(&dss->lock); |
| 1322 | ns->stream = dss; |
| 1323 | return 0; |
| 1324 | out: |
| 1325 | if (dss && dss->task_rsp) |
| 1326 | task_destroy(dss->task_rsp); |
| 1327 | if (dss && dss->task_req) |
| 1328 | task_destroy(dss->task_req); |
| 1329 | if (dss && dss->ring_req) |
| 1330 | ring_free(dss->ring_req); |
| 1331 | |
| 1332 | free(dss); |
Emeric Brun | c943799 | 2021-02-12 19:42:55 +0100 | [diff] [blame] | 1333 | return -1; |
Christopher Faulet | 67957bd | 2017-09-27 11:00:59 +0200 | [diff] [blame] | 1334 | } |
| 1335 | |
Emeric Brun | c943799 | 2021-02-12 19:42:55 +0100 | [diff] [blame] | 1336 | int init_dns_buffers() |
Baptiste Assmann | 325137d | 2015-04-13 23:40:55 +0200 | [diff] [blame] | 1337 | { |
Emeric Brun | c943799 | 2021-02-12 19:42:55 +0100 | [diff] [blame] | 1338 | dns_msg_trash = malloc(DNS_TCP_MSG_MAX_SIZE); |
| 1339 | if (!dns_msg_trash) |
| 1340 | return 0; |
Baptiste Assmann | 325137d | 2015-04-13 23:40:55 +0200 | [diff] [blame] | 1341 | |
Emeric Brun | c943799 | 2021-02-12 19:42:55 +0100 | [diff] [blame] | 1342 | return 1; |
| 1343 | } |
Baptiste Assmann | c1ce5f3 | 2016-05-14 11:26:22 +0200 | [diff] [blame] | 1344 | |
Emeric Brun | c943799 | 2021-02-12 19:42:55 +0100 | [diff] [blame] | 1345 | void deinit_dns_buffers() |
| 1346 | { |
Willy Tarreau | 61cfdf4 | 2021-02-20 10:46:51 +0100 | [diff] [blame] | 1347 | ha_free(&dns_msg_trash); |
Emeric Brun | c943799 | 2021-02-12 19:42:55 +0100 | [diff] [blame] | 1348 | } |
Emeric Brun | d26a623 | 2021-01-04 13:32:20 +0100 | [diff] [blame] | 1349 | |
| 1350 | REGISTER_PER_THREAD_ALLOC(init_dns_buffers); |
| 1351 | REGISTER_PER_THREAD_FREE(deinit_dns_buffers); |