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