blob: c4b2af0beb0b8de7414c5d17ebc0213e0cbb7000 [file] [log] [blame]
Baptiste Assmann325137d2015-04-13 23:40:55 +02001/*
2 * Name server resolution
3 *
Willy Tarreau714f3452021-05-09 06:47:26 +02004 * Copyright 2020 HAProxy Technologies
Baptiste Assmann325137d2015-04-13 23:40:55 +02005 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 *
11 */
12
13#include <errno.h>
14#include <fcntl.h>
15#include <stdio.h>
16#include <stdlib.h>
17#include <string.h>
18#include <unistd.h>
19
20#include <sys/types.h>
21
Willy Tarreau122eba92020-06-04 10:15:32 +020022#include <haproxy/action.h>
Willy Tarreau4c7e4b72020-05-27 12:58:42 +020023#include <haproxy/api.h>
Willy Tarreau6be78492020-06-05 00:00:29 +020024#include <haproxy/cfgparse.h>
Willy Tarreauf1d32c42020-06-04 21:07:02 +020025#include <haproxy/channel.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020026#include <haproxy/check.h>
Willy Tarreau83487a82020-06-04 20:19:54 +020027#include <haproxy/cli.h>
Willy Tarreau7c18b542020-06-11 09:23:02 +020028#include <haproxy/dgram.h>
Willy Tarreaueb92deb2020-06-04 10:53:16 +020029#include <haproxy/dns.h>
Willy Tarreau8d366972020-05-27 16:10:29 +020030#include <haproxy/errors.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020031#include <haproxy/fd.h>
Willy Tarreauaeed4a82020-06-04 22:01:04 +020032#include <haproxy/log.h>
Emeric Brund26a6232021-01-04 13:32:20 +010033#include <haproxy/ring.h>
Emeric Brunfd647d52021-02-12 20:03:38 +010034#include <haproxy/stream.h>
35#include <haproxy/stream_interface.h>
Willy Tarreau9f9e9fc2021-05-08 13:09:46 +020036#include <haproxy/tools.h>
Baptiste Assmann325137d2015-04-13 23:40:55 +020037
Emeric Brund26a6232021-01-04 13:32:20 +010038static THREAD_LOCAL char *dns_msg_trash;
Baptiste Assmann325137d2015-04-13 23:40:55 +020039
Emeric Brunfd647d52021-02-12 20:03:38 +010040DECLARE_STATIC_POOL(dns_session_pool, "dns_session", sizeof(struct dns_session));
41DECLARE_STATIC_POOL(dns_query_pool, "dns_query", sizeof(struct dns_query));
42DECLARE_STATIC_POOL(dns_msg_buf, "dns_msg_buf", DNS_TCP_MSG_RING_MAX_SIZE);
43
Christopher Faulet67957bd2017-09-27 11:00:59 +020044/* Opens an UDP socket on the namesaver's IP/Port, if required. Returns 0 on
Christopher Faulet1e711be2021-03-04 16:58:35 +010045 * success, -1 otherwise. ns->dgram must be defined.
Baptiste Assmann325137d2015-04-13 23:40:55 +020046 */
Emeric Brund26a6232021-01-04 13:32:20 +010047static int dns_connect_nameserver(struct dns_nameserver *ns)
Baptiste Assmann325137d2015-04-13 23:40:55 +020048{
Christopher Faulet1e711be2021-03-04 16:58:35 +010049 struct dgram_conn *dgram = &ns->dgram->conn;
50 int fd;
Baptiste Assmann325137d2015-04-13 23:40:55 +020051
Christopher Faulet1e711be2021-03-04 16:58:35 +010052 /* Already connected */
53 if (dgram->t.sock.fd != -1)
Emeric Brun526b7922021-02-15 14:28:27 +010054 return 0;
Christopher Faulet1e711be2021-03-04 16:58:35 +010055
56 /* Create an UDP socket and connect it on the nameserver's IP/Port */
57 if ((fd = socket(dgram->addr.to.ss_family, SOCK_DGRAM, IPPROTO_UDP)) == -1) {
58 send_log(NULL, LOG_WARNING,
59 "DNS : section '%s': can't create socket for nameserver '%s'.\n",
60 ns->counters->pid, ns->id);
61 return -1;
62 }
63 if (connect(fd, (struct sockaddr*)&dgram->addr.to, get_addr_len(&dgram->addr.to)) == -1) {
64 send_log(NULL, LOG_WARNING,
65 "DNS : section '%s': can't connect socket for nameserver '%s'.\n",
66 ns->counters->id, ns->id);
67 close(fd);
68 return -1;
Emeric Brunc9437992021-02-12 19:42:55 +010069 }
Emeric Brun526b7922021-02-15 14:28:27 +010070
Christopher Faulet1e711be2021-03-04 16:58:35 +010071 /* Make the socket non blocking */
72 fcntl(fd, F_SETFL, O_NONBLOCK);
73
74 /* Add the fd in the fd list and update its parameters */
75 dgram->t.sock.fd = fd;
76 fd_insert(fd, dgram, dgram_fd_handler, MAX_THREADS_MASK);
77 fd_want_recv(fd);
78 return 0;
Baptiste Assmann325137d2015-04-13 23:40:55 +020079}
80
Emeric Brund26a6232021-01-04 13:32:20 +010081/* Sends a message to a name server
82 * It returns message length on success
83 * or -1 in error case
84 * 0 is returned in case of output ring buffer is full
85 */
86int dns_send_nameserver(struct dns_nameserver *ns, void *buf, size_t len)
87{
88 int ret = -1;
89
90 if (ns->dgram) {
91 struct dgram_conn *dgram = &ns->dgram->conn;
Emeric Brunf1d38be2022-05-10 11:35:48 +020092 int fd;
Emeric Brund26a6232021-01-04 13:32:20 +010093
Emeric Brunf1d38be2022-05-10 11:35:48 +020094 HA_SPIN_LOCK(DNS_LOCK, &dgram->lock);
95 fd = dgram->t.sock.fd;
96 if (fd == -1) {
97 if (dns_connect_nameserver(ns) == -1) {
98 HA_SPIN_UNLOCK(DNS_LOCK, &dgram->lock);
Emeric Brund26a6232021-01-04 13:32:20 +010099 return -1;
Emeric Brunf1d38be2022-05-10 11:35:48 +0200100 }
Emeric Brund26a6232021-01-04 13:32:20 +0100101 fd = dgram->t.sock.fd;
102 }
103
104 ret = send(fd, buf, len, 0);
105 if (ret < 0) {
106 if (errno == EAGAIN) {
107 struct ist myist;
108
Tim Duesterhus92c696e2021-02-28 16:11:36 +0100109 myist = ist2(buf, len);
Emeric Brund26a6232021-01-04 13:32:20 +0100110 ret = ring_write(ns->dgram->ring_req, DNS_TCP_MSG_MAX_SIZE, NULL, 0, &myist, 1);
111 if (!ret) {
112 ns->counters->snd_error++;
Emeric Brunf1d38be2022-05-10 11:35:48 +0200113 HA_SPIN_UNLOCK(DNS_LOCK, &dgram->lock);
Emeric Brund26a6232021-01-04 13:32:20 +0100114 return -1;
115 }
116 fd_cant_send(fd);
Emeric Brunf1d38be2022-05-10 11:35:48 +0200117 HA_SPIN_UNLOCK(DNS_LOCK, &dgram->lock);
Emeric Brund26a6232021-01-04 13:32:20 +0100118 return ret;
119 }
120 ns->counters->snd_error++;
121 fd_delete(fd);
Emeric Brund26a6232021-01-04 13:32:20 +0100122 dgram->t.sock.fd = -1;
Emeric Brunf1d38be2022-05-10 11:35:48 +0200123 HA_SPIN_UNLOCK(DNS_LOCK, &dgram->lock);
Emeric Brund26a6232021-01-04 13:32:20 +0100124 return -1;
125 }
126 ns->counters->sent++;
Emeric Brunf1d38be2022-05-10 11:35:48 +0200127 HA_SPIN_UNLOCK(DNS_LOCK, &dgram->lock);
Emeric Brund26a6232021-01-04 13:32:20 +0100128 }
Emeric Brunfd647d52021-02-12 20:03:38 +0100129 else if (ns->stream) {
130 struct ist myist;
131
Tim Duesterhus92c696e2021-02-28 16:11:36 +0100132 myist = ist2(buf, len);
Emeric Brunfd647d52021-02-12 20:03:38 +0100133 ret = ring_write(ns->stream->ring_req, DNS_TCP_MSG_MAX_SIZE, NULL, 0, &myist, 1);
134 if (!ret) {
135 ns->counters->snd_error++;
136 return -1;
137 }
138 task_wakeup(ns->stream->task_req, TASK_WOKEN_MSG);
139 return ret;
140 }
Emeric Brund26a6232021-01-04 13:32:20 +0100141
142 return ret;
143}
144
Emeric Brunfd647d52021-02-12 20:03:38 +0100145void dns_session_free(struct dns_session *);
146
Emeric Brund26a6232021-01-04 13:32:20 +0100147/* Receives a dns message
148 * Returns message length
149 * 0 is returned if no more message available
150 * -1 in error case
151 */
152ssize_t dns_recv_nameserver(struct dns_nameserver *ns, void *data, size_t size)
153{
154 ssize_t ret = -1;
155
156 if (ns->dgram) {
157 struct dgram_conn *dgram = &ns->dgram->conn;
Emeric Brunf1d38be2022-05-10 11:35:48 +0200158 int fd;
Emeric Brund26a6232021-01-04 13:32:20 +0100159
Emeric Brunf1d38be2022-05-10 11:35:48 +0200160 HA_SPIN_LOCK(DNS_LOCK, &dgram->lock);
161 fd = dgram->t.sock.fd;
162 if (fd == -1) {
163 HA_SPIN_UNLOCK(DNS_LOCK, &dgram->lock);
Emeric Brund26a6232021-01-04 13:32:20 +0100164 return -1;
Emeric Brunf1d38be2022-05-10 11:35:48 +0200165 }
Emeric Brund26a6232021-01-04 13:32:20 +0100166
167 if ((ret = recv(fd, data, size, 0)) < 0) {
168 if (errno == EAGAIN) {
169 fd_cant_recv(fd);
Emeric Brunf1d38be2022-05-10 11:35:48 +0200170 HA_SPIN_UNLOCK(DNS_LOCK, &dgram->lock);
Emeric Brund26a6232021-01-04 13:32:20 +0100171 return 0;
172 }
173 fd_delete(fd);
Emeric Brund26a6232021-01-04 13:32:20 +0100174 dgram->t.sock.fd = -1;
Emeric Brunf1d38be2022-05-10 11:35:48 +0200175 HA_SPIN_UNLOCK(DNS_LOCK, &dgram->lock);
Emeric Brund26a6232021-01-04 13:32:20 +0100176 return -1;
177 }
Emeric Brunf1d38be2022-05-10 11:35:48 +0200178 HA_SPIN_UNLOCK(DNS_LOCK, &dgram->lock);
Emeric Brund26a6232021-01-04 13:32:20 +0100179 }
Emeric Brunfd647d52021-02-12 20:03:38 +0100180 else if (ns->stream) {
181 struct dns_stream_server *dss = ns->stream;
182 struct dns_session *ds;
183
184 HA_SPIN_LOCK(DNS_LOCK, &dss->lock);
185
186 if (!LIST_ISEMPTY(&dss->wait_sess)) {
187 ds = LIST_NEXT(&dss->wait_sess, struct dns_session *, waiter);
Emeric Brunfd647d52021-02-12 20:03:38 +0100188 ret = ds->rx_msg.len < size ? ds->rx_msg.len : size;
189 memcpy(data, ds->rx_msg.area, ret);
190
191 ds->rx_msg.len = 0;
192
Emeric Brunfd647d52021-02-12 20:03:38 +0100193 LIST_DEL_INIT(&ds->waiter);
194
195 if (ds->appctx) {
Ilya Shipitsin0de36ad2021-02-20 00:23:36 +0500196 /* awake appctx because it may have other
Emeric Brunfd647d52021-02-12 20:03:38 +0100197 * message to receive
198 */
199 appctx_wakeup(ds->appctx);
200
201 /* dns_session could already be into free_sess list
202 * so we firstly remove it */
203 LIST_DEL_INIT(&ds->list);
204
205 /* decrease nb_queries to free a slot for a new query on that sess */
206 ds->nb_queries--;
207 if (ds->nb_queries) {
208 /* it remains pipelined unanswered request
209 * into this session but we just decrease
210 * the counter so the session
211 * can not be full of pipelined requests
212 * so we can add if to free_sess list
213 * to receive a new request
214 */
Willy Tarreau2b718102021-04-21 07:32:39 +0200215 LIST_INSERT(&ds->dss->free_sess, &ds->list);
Emeric Brunfd647d52021-02-12 20:03:38 +0100216 }
217 else {
218 /* there is no more pipelined requests
219 * into this session, so we move it
220 * to idle_sess list */
Willy Tarreau2b718102021-04-21 07:32:39 +0200221 LIST_INSERT(&ds->dss->idle_sess, &ds->list);
Emeric Brunfd647d52021-02-12 20:03:38 +0100222
223 /* update the counter of idle sessions */
224 ds->dss->idle_conns++;
225
226 /* Note: this is useless there to update
227 * the max_active_conns since we increase
228 * the idle count */
229 }
230 }
231 else {
232 /* there is no more appctx for this session
233 * it means it is ready to die
234 */
235 dns_session_free(ds);
236 }
237
238
239 }
240
241 HA_SPIN_UNLOCK(DNS_LOCK, &dss->lock);
242 }
Emeric Brund26a6232021-01-04 13:32:20 +0100243
244 return ret;
245}
246
247static void dns_resolve_recv(struct dgram_conn *dgram)
248{
249 struct dns_nameserver *ns;
250 int fd;
251
Emeric Brunf1d38be2022-05-10 11:35:48 +0200252 HA_SPIN_LOCK(DNS_LOCK, &dgram->lock);
253
Emeric Brund26a6232021-01-04 13:32:20 +0100254 fd = dgram->t.sock.fd;
255
256 /* check if ready for reading */
Emeric Brunf1d38be2022-05-10 11:35:48 +0200257 if ((fd == -1) || !fd_recv_ready(fd)) {
258 HA_SPIN_UNLOCK(DNS_LOCK, &dgram->lock);
Emeric Brund26a6232021-01-04 13:32:20 +0100259 return;
Emeric Brunf1d38be2022-05-10 11:35:48 +0200260 }
Emeric Brund26a6232021-01-04 13:32:20 +0100261
262 /* no need to go further if we can't retrieve the nameserver */
263 if ((ns = dgram->owner) == NULL) {
Willy Tarreauf5090652021-04-06 17:23:40 +0200264 _HA_ATOMIC_AND(&fdtab[fd].state, ~(FD_POLL_HUP|FD_POLL_ERR));
Emeric Brund26a6232021-01-04 13:32:20 +0100265 fd_stop_recv(fd);
Emeric Brunf1d38be2022-05-10 11:35:48 +0200266 HA_SPIN_UNLOCK(DNS_LOCK, &dgram->lock);
Emeric Brund26a6232021-01-04 13:32:20 +0100267 return;
268 }
269
Emeric Brunf1d38be2022-05-10 11:35:48 +0200270 HA_SPIN_UNLOCK(DNS_LOCK, &dgram->lock);
271
Emeric Brund26a6232021-01-04 13:32:20 +0100272 ns->process_responses(ns);
273}
274
275/* Called when a dns network socket is ready to send data */
276static void dns_resolve_send(struct dgram_conn *dgram)
277{
278 int fd;
279 struct dns_nameserver *ns;
280 struct ring *ring;
281 struct buffer *buf;
282 uint64_t msg_len;
283 size_t len, cnt, ofs;
284
Emeric Brunf1d38be2022-05-10 11:35:48 +0200285 HA_SPIN_LOCK(DNS_LOCK, &dgram->lock);
286
Emeric Brund26a6232021-01-04 13:32:20 +0100287 fd = dgram->t.sock.fd;
288
289 /* check if ready for sending */
Emeric Brunf1d38be2022-05-10 11:35:48 +0200290 if ((fd == -1) || !fd_send_ready(fd)) {
291 HA_SPIN_UNLOCK(DNS_LOCK, &dgram->lock);
Emeric Brund26a6232021-01-04 13:32:20 +0100292 return;
Emeric Brunf1d38be2022-05-10 11:35:48 +0200293 }
Emeric Brund26a6232021-01-04 13:32:20 +0100294
295 /* no need to go further if we can't retrieve the nameserver */
296 if ((ns = dgram->owner) == NULL) {
Willy Tarreauf5090652021-04-06 17:23:40 +0200297 _HA_ATOMIC_AND(&fdtab[fd].state, ~(FD_POLL_HUP|FD_POLL_ERR));
Emeric Brund26a6232021-01-04 13:32:20 +0100298 fd_stop_send(fd);
Emeric Brunf1d38be2022-05-10 11:35:48 +0200299 HA_SPIN_UNLOCK(DNS_LOCK, &dgram->lock);
Emeric Brund26a6232021-01-04 13:32:20 +0100300 return;
301 }
302
303 ring = ns->dgram->ring_req;
304 buf = &ring->buf;
305
306 HA_RWLOCK_RDLOCK(DNS_LOCK, &ring->lock);
307 ofs = ns->dgram->ofs_req;
308
309 /* explanation for the initialization below: it would be better to do
310 * this in the parsing function but this would occasionally result in
311 * dropped events because we'd take a reference on the oldest message
312 * and keep it while being scheduled. Thus instead let's take it the
313 * first time we enter here so that we have a chance to pass many
314 * existing messages before grabbing a reference to a location. This
315 * value cannot be produced after initialization.
316 */
317 if (unlikely(ofs == ~0)) {
318 ofs = 0;
Willy Tarreau4781b152021-04-06 13:53:36 +0200319 HA_ATOMIC_INC(b_peek(buf, ofs));
Emeric Brund26a6232021-01-04 13:32:20 +0100320 ofs += ring->ofs;
321 }
322
323 /* we were already there, adjust the offset to be relative to
324 * the buffer's head and remove us from the counter.
325 */
326 ofs -= ring->ofs;
327 BUG_ON(ofs >= buf->size);
Willy Tarreau4781b152021-04-06 13:53:36 +0200328 HA_ATOMIC_DEC(b_peek(buf, ofs));
Emeric Brund26a6232021-01-04 13:32:20 +0100329
330 while (ofs + 1 < b_data(buf)) {
331 int ret;
332
333 cnt = 1;
334 len = b_peek_varint(buf, ofs + cnt, &msg_len);
335 if (!len)
336 break;
337 cnt += len;
338 BUG_ON(msg_len + ofs + cnt + 1 > b_data(buf));
339 if (unlikely(msg_len > DNS_TCP_MSG_MAX_SIZE)) {
340 /* too large a message to ever fit, let's skip it */
341 ofs += cnt + msg_len;
342 continue;
343 }
344
345 len = b_getblk(buf, dns_msg_trash, msg_len, ofs + cnt);
346
347 ret = send(fd, dns_msg_trash, len, 0);
348 if (ret < 0) {
349 if (errno == EAGAIN) {
350 fd_cant_send(fd);
351 goto out;
352 }
353 ns->counters->snd_error++;
354 fd_delete(fd);
Emeric Brund26a6232021-01-04 13:32:20 +0100355 fd = dgram->t.sock.fd = -1;
356 goto out;
357 }
358 ns->counters->sent++;
359
360 ofs += cnt + len;
361 }
362
363 /* we don't want/need to be waked up any more for sending
364 * because all ring content is sent */
365 fd_stop_send(fd);
366
367out:
368
Willy Tarreau4781b152021-04-06 13:53:36 +0200369 HA_ATOMIC_INC(b_peek(buf, ofs));
Emeric Brund26a6232021-01-04 13:32:20 +0100370 ofs += ring->ofs;
371 ns->dgram->ofs_req = ofs;
372 HA_RWLOCK_RDUNLOCK(DNS_LOCK, &ring->lock);
Emeric Brunf1d38be2022-05-10 11:35:48 +0200373 HA_SPIN_UNLOCK(DNS_LOCK, &dgram->lock);
Emeric Brund26a6232021-01-04 13:32:20 +0100374
375}
376
Emeric Brunc9437992021-02-12 19:42:55 +0100377/* proto_udp callback functions for a DNS resolution */
378struct dgram_data_cb dns_dgram_cb = {
379 .recv = dns_resolve_recv,
380 .send = dns_resolve_send,
381};
Baptiste Assmann325137d2015-04-13 23:40:55 +0200382
Emeric Brunc9437992021-02-12 19:42:55 +0100383int dns_dgram_init(struct dns_nameserver *ns, struct sockaddr_storage *sk)
Baptiste Assmann325137d2015-04-13 23:40:55 +0200384{
Emeric Brunc9437992021-02-12 19:42:55 +0100385 struct dns_dgram_server *dgram;
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200386
Emeric Brunc9437992021-02-12 19:42:55 +0100387 if ((dgram = calloc(1, sizeof(*dgram))) == NULL)
Christopher Faulet67957bd2017-09-27 11:00:59 +0200388 return -1;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200389
Emeric Brunc9437992021-02-12 19:42:55 +0100390 /* Leave dgram partially initialized, no FD attached for
391 * now. */
392 dgram->conn.owner = ns;
393 dgram->conn.data = &dns_dgram_cb;
394 dgram->conn.t.sock.fd = -1;
395 dgram->conn.addr.to = *sk;
Emeric Brunf1d38be2022-05-10 11:35:48 +0200396 HA_SPIN_INIT(&dgram->conn.lock);
Emeric Brunc9437992021-02-12 19:42:55 +0100397 ns->dgram = dgram;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200398
Emeric Brunc9437992021-02-12 19:42:55 +0100399 dgram->ofs_req = ~0; /* init ring offset */
400 dgram->ring_req = ring_new(2*DNS_TCP_MSG_RING_MAX_SIZE);
401 if (!dgram->ring_req) {
402 ha_alert("memory allocation error initializing the ring for nameserver.\n");
403 goto out;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200404 }
405
Emeric Brunc9437992021-02-12 19:42:55 +0100406 /* attach the task as reader */
407 if (!ring_attach(dgram->ring_req)) {
408 /* mark server attached to the ring */
409 ha_alert("nameserver sets too many watchers > 255 on ring. This is a bug and should not happen.\n");
410 goto out;
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200411 }
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200412 return 0;
Emeric Brunc9437992021-02-12 19:42:55 +0100413out:
414 if (dgram->ring_req)
415 ring_free(dgram->ring_req);
Christopher Fauletd6c6b5f2020-09-08 10:27:24 +0200416
Emeric Brunc9437992021-02-12 19:42:55 +0100417 free(dgram);
Olivier Houchard2ec2db92018-01-08 16:28:57 +0100418
Emeric Brunfd647d52021-02-12 20:03:38 +0100419 return -1;
420}
421
422/*
423 * IO Handler to handle message push to dns tcp server
424 */
425static void dns_session_io_handler(struct appctx *appctx)
426{
427 struct stream_interface *si = appctx->owner;
428 struct dns_session *ds = appctx->ctx.sft.ptr;
429 struct ring *ring = &ds->ring;
430 struct buffer *buf = &ring->buf;
431 uint64_t msg_len;
432 int available_room;
433 size_t len, cnt, ofs;
434 int ret = 0;
435
436 /* if stopping was requested, close immediately */
437 if (unlikely(stopping))
438 goto close;
439
440 /* we want to be sure to not miss that we have been awaked for a shutdown */
441 __ha_barrier_load();
442
443 /* that means the connection was requested to shutdown
444 * for instance idle expire */
445 if (ds->shutdown)
446 goto close;
447
448 /* an error was detected */
449 if (unlikely(si_ic(si)->flags & (CF_WRITE_ERROR|CF_SHUTW)))
450 goto close;
451
452 /* con closed by server side, we will skip data write and drain data from channel */
453 if ((si_oc(si)->flags & CF_SHUTW)) {
454 goto read;
455 }
456
457 /* if the connection is not established, inform the stream that we want
458 * to be notified whenever the connection completes.
459 */
460 if (si_opposite(si)->state < SI_ST_EST) {
461 si_cant_get(si);
462 si_rx_conn_blk(si);
463 si_rx_endp_more(si);
464 return;
465 }
466
467
468 ofs = ds->ofs;
469
470 HA_RWLOCK_WRLOCK(DNS_LOCK, &ring->lock);
471 LIST_DEL_INIT(&appctx->wait_entry);
472 HA_RWLOCK_WRUNLOCK(DNS_LOCK, &ring->lock);
473
474 HA_RWLOCK_RDLOCK(DNS_LOCK, &ring->lock);
475
476 /* explanation for the initialization below: it would be better to do
477 * this in the parsing function but this would occasionally result in
478 * dropped events because we'd take a reference on the oldest message
479 * and keep it while being scheduled. Thus instead let's take it the
480 * first time we enter here so that we have a chance to pass many
481 * existing messages before grabbing a reference to a location. This
482 * value cannot be produced after initialization.
483 */
484 if (unlikely(ofs == ~0)) {
485 ofs = 0;
486
Willy Tarreau4781b152021-04-06 13:53:36 +0200487 HA_ATOMIC_INC(b_peek(buf, ofs));
Emeric Brunfd647d52021-02-12 20:03:38 +0100488 ofs += ring->ofs;
489 }
490
491 /* in this loop, ofs always points to the counter byte that precedes
492 * the message so that we can take our reference there if we have to
493 * stop before the end (ret=0).
494 */
495 if (si_opposite(si)->state == SI_ST_EST) {
496 /* we were already there, adjust the offset to be relative to
497 * the buffer's head and remove us from the counter.
498 */
499 ofs -= ring->ofs;
500 BUG_ON(ofs >= buf->size);
Willy Tarreau4781b152021-04-06 13:53:36 +0200501 HA_ATOMIC_DEC(b_peek(buf, ofs));
Emeric Brunfd647d52021-02-12 20:03:38 +0100502
503 ret = 1;
504 while (ofs + 1 < b_data(buf)) {
505 struct dns_query *query;
506 uint16_t original_qid;
507 uint16_t new_qid;
508
509 cnt = 1;
510 len = b_peek_varint(buf, ofs + cnt, &msg_len);
511 if (!len)
512 break;
513 cnt += len;
514 BUG_ON(msg_len + ofs + cnt + 1 > b_data(buf));
515
516 /* retrieve available room on output channel */
517 available_room = channel_recv_max(si_ic(si));
518
519 /* tx_msg_offset null means we are at the start of a new message */
520 if (!ds->tx_msg_offset) {
521 uint16_t slen;
522
523 /* check if there is enough room to put message len and query id */
524 if (available_room < sizeof(slen) + sizeof(new_qid)) {
525 si_rx_room_blk(si);
526 ret = 0;
527 break;
528 }
529
530 /* put msg len into then channel */
531 slen = (uint16_t)msg_len;
532 slen = htons(slen);
533 ci_putblk(si_ic(si), (char *)&slen, sizeof(slen));
534 available_room -= sizeof(slen);
535
536 /* backup original query id */
537 len = b_getblk(buf, (char *)&original_qid, sizeof(original_qid), ofs + cnt);
Emeric Brun538bb042021-02-15 13:58:06 +0100538 if (!len) {
539 /* should never happen since messages are atomically
540 * written into ring
541 */
542 ret = 0;
543 break;
544 }
Emeric Brunfd647d52021-02-12 20:03:38 +0100545
546 /* generates new query id */
547 new_qid = ++ds->query_counter;
548 new_qid = htons(new_qid);
549
550 /* put new query id into the channel */
551 ci_putblk(si_ic(si), (char *)&new_qid, sizeof(new_qid));
552 available_room -= sizeof(new_qid);
553
554 /* keep query id mapping */
555
556 query = pool_alloc(dns_query_pool);
557 if (query) {
558 query->qid.key = new_qid;
559 query->original_qid = original_qid;
560 query->expire = tick_add(now_ms, 5000);
561 LIST_INIT(&query->list);
562 if (LIST_ISEMPTY(&ds->queries)) {
563 /* enable task to handle expire */
564 ds->task_exp->expire = query->expire;
565 /* ensure this will be executed by the same
566 * thread than ds_session_release
567 * to ensure session_release is free
568 * to destroy the task */
569 task_queue(ds->task_exp);
570 }
Willy Tarreau2b718102021-04-21 07:32:39 +0200571 LIST_APPEND(&ds->queries, &query->list);
Emeric Brunfd647d52021-02-12 20:03:38 +0100572 eb32_insert(&ds->query_ids, &query->qid);
573 ds->onfly_queries++;
574 }
575
576 /* update the tx_offset to handle output in 16k streams */
577 ds->tx_msg_offset = sizeof(original_qid);
578
579 }
580
581 /* check if it remains available room on output chan */
582 if (unlikely(!available_room)) {
583 si_rx_room_blk(si);
584 ret = 0;
585 break;
586 }
587
588 chunk_reset(&trash);
589 if ((msg_len - ds->tx_msg_offset) > available_room) {
590 /* remaining msg data is too large to be written in output channel at one time */
591
592 len = b_getblk(buf, trash.area, available_room, ofs + cnt + ds->tx_msg_offset);
593
594 /* update offset to complete mesg forwarding later */
595 ds->tx_msg_offset += len;
596 }
597 else {
598 /* remaining msg data can be written in output channel at one time */
599 len = b_getblk(buf, trash.area, msg_len - ds->tx_msg_offset, ofs + cnt + ds->tx_msg_offset);
600
601 /* reset tx_msg_offset to mark forward fully processed */
602 ds->tx_msg_offset = 0;
603 }
604 trash.data += len;
605
Emeric Brun743afee2021-02-15 14:12:06 +0100606 if (ci_putchk(si_ic(si), &trash) == -1) {
607 /* should never happen since we
608 * check available_room is large
Ilya Shipitsin0de36ad2021-02-20 00:23:36 +0500609 * enough here.
Emeric Brun743afee2021-02-15 14:12:06 +0100610 */
611 si_rx_room_blk(si);
612 ret = 0;
613 break;
614 }
Emeric Brunfd647d52021-02-12 20:03:38 +0100615
616 if (ds->tx_msg_offset) {
617 /* msg was not fully processed, we must be awake to drain pending data */
618
619 si_rx_room_blk(si);
620 ret = 0;
621 break;
622 }
623 /* switch to next message */
624 ofs += cnt + msg_len;
625 }
626
Willy Tarreau4781b152021-04-06 13:53:36 +0200627 HA_ATOMIC_INC(b_peek(buf, ofs));
Emeric Brunfd647d52021-02-12 20:03:38 +0100628 ofs += ring->ofs;
629 ds->ofs = ofs;
630 }
631 HA_RWLOCK_RDUNLOCK(DNS_LOCK, &ring->lock);
632
633 if (ret) {
634 /* let's be woken up once new request to write arrived */
635 HA_RWLOCK_WRLOCK(DNS_LOCK, &ring->lock);
Willy Tarreau2b718102021-04-21 07:32:39 +0200636 LIST_APPEND(&ring->waiters, &appctx->wait_entry);
Emeric Brunfd647d52021-02-12 20:03:38 +0100637 HA_RWLOCK_WRUNLOCK(DNS_LOCK, &ring->lock);
638 si_rx_endp_done(si);
639 }
640
641read:
642
Ilya Shipitsin0de36ad2021-02-20 00:23:36 +0500643 /* if session is not a waiter it means there is no committed
Emeric Brunfd647d52021-02-12 20:03:38 +0100644 * message into rx_buf and we are free to use it
645 * Note: we need a load barrier here to not miss the
646 * delete from the list
647 */
Emeric Brun9a6ac572021-10-20 10:49:53 +0200648
649 /* lock the dns_stream_server containing lists heads */
650 HA_SPIN_LOCK(DNS_LOCK, &ds->dss->lock);
651
Willy Tarreau2b718102021-04-21 07:32:39 +0200652 if (!LIST_INLIST(&ds->waiter)) {
Emeric Brunfd647d52021-02-12 20:03:38 +0100653 while (1) {
654 uint16_t query_id;
655 struct eb32_node *eb;
656 struct dns_query *query;
657
658 if (!ds->rx_msg.len) {
659 /* next message len is not fully available into the channel */
660 if (co_data(si_oc(si)) < 2)
661 break;
662
663 /* retrieve message len */
664 co_getblk(si_oc(si), (char *)&msg_len, 2, 0);
665
666 /* mark as consumed */
667 co_skip(si_oc(si), 2);
668
669 /* store message len */
670 ds->rx_msg.len = ntohs(msg_len);
671 }
672
673 if (!co_data(si_oc(si))) {
674 /* we need more data but nothing is available */
675 break;
676 }
677
678 if (co_data(si_oc(si)) + ds->rx_msg.offset < ds->rx_msg.len) {
679 /* message only partially available */
680
681 /* read available data */
682 co_getblk(si_oc(si), ds->rx_msg.area + ds->rx_msg.offset, co_data(si_oc(si)), 0);
683
684 /* update message offset */
685 ds->rx_msg.offset += co_data(si_oc(si));
686
687 /* consume all pending data from the channel */
688 co_skip(si_oc(si), co_data(si_oc(si)));
689
690 /* we need to wait for more data */
691 break;
692 }
693
Ilya Shipitsin0de36ad2021-02-20 00:23:36 +0500694 /* enough data is available into the channel to read the message until the end */
Emeric Brunfd647d52021-02-12 20:03:38 +0100695
696 /* read from the channel until the end of the message */
697 co_getblk(si_oc(si), ds->rx_msg.area + ds->rx_msg.offset, ds->rx_msg.len - ds->rx_msg.offset, 0);
698
699 /* consume all data until the end of the message from the channel */
700 co_skip(si_oc(si), ds->rx_msg.len - ds->rx_msg.offset);
701
702 /* reset reader offset to 0 for next message reand */
703 ds->rx_msg.offset = 0;
704
705 /* try remap query id to original */
706 memcpy(&query_id, ds->rx_msg.area, sizeof(query_id));
707 eb = eb32_lookup(&ds->query_ids, query_id);
708 if (!eb) {
709 /* query id not found means we have an unknown corresponding
710 * request, perhaps server's bug or or the query reached
711 * timeout
712 */
713 ds->rx_msg.len = 0;
714 continue;
715 }
716
717 /* re-map the original query id set by the requester */
718 query = eb32_entry(eb, struct dns_query, qid);
719 memcpy(ds->rx_msg.area, &query->original_qid, sizeof(query->original_qid));
720
721 /* remove query ids mapping from pending queries list/tree */
722 eb32_delete(&query->qid);
Willy Tarreau2b718102021-04-21 07:32:39 +0200723 LIST_DELETE(&query->list);
Emeric Brunfd647d52021-02-12 20:03:38 +0100724 pool_free(dns_query_pool, query);
725 ds->onfly_queries--;
726
Emeric Brunfd647d52021-02-12 20:03:38 +0100727 /* the dns_session is also added in queue of the
728 * wait_sess list where the task processing
729 * response will pop available responses
730 */
Willy Tarreau2b718102021-04-21 07:32:39 +0200731 LIST_APPEND(&ds->dss->wait_sess, &ds->waiter);
Emeric Brunfd647d52021-02-12 20:03:38 +0100732
Emeric Brunfd647d52021-02-12 20:03:38 +0100733 /* awake the task processing the responses */
734 task_wakeup(ds->dss->task_rsp, TASK_WOKEN_INIT);
735
736 break;
737 }
738
Willy Tarreau2b718102021-04-21 07:32:39 +0200739 if (!LIST_INLIST(&ds->waiter)) {
Emeric Brunfd647d52021-02-12 20:03:38 +0100740 /* there is no more pending data to read and the con was closed by the server side */
741 if (!co_data(si_oc(si)) && (si_oc(si)->flags & CF_SHUTW)) {
Emeric Brun9a6ac572021-10-20 10:49:53 +0200742 HA_SPIN_UNLOCK(DNS_LOCK, &ds->dss->lock);
Emeric Brunfd647d52021-02-12 20:03:38 +0100743 goto close;
744 }
745 }
746
747 }
748
Emeric Brun9a6ac572021-10-20 10:49:53 +0200749 HA_SPIN_UNLOCK(DNS_LOCK, &ds->dss->lock);
Emeric Brunfd647d52021-02-12 20:03:38 +0100750 return;
751close:
752 si_shutw(si);
753 si_shutr(si);
754 si_ic(si)->flags |= CF_READ_NULL;
755}
756
757void dns_queries_flush(struct dns_session *ds)
758{
759 struct dns_query *query, *queryb;
760
761 list_for_each_entry_safe(query, queryb, &ds->queries, list) {
762 eb32_delete(&query->qid);
Willy Tarreau2b718102021-04-21 07:32:39 +0200763 LIST_DELETE(&query->list);
Emeric Brunfd647d52021-02-12 20:03:38 +0100764 pool_free(dns_query_pool, query);
765 }
766}
767
768void dns_session_free(struct dns_session *ds)
769{
770 if (ds->rx_msg.area)
771 pool_free(dns_msg_buf, ds->rx_msg.area);
772 if (ds->tx_ring_area)
773 pool_free(dns_msg_buf, ds->tx_ring_area);
774 if (ds->task_exp)
775 task_destroy(ds->task_exp);
776
777 dns_queries_flush(ds);
778
Emeric Brunb18a95b2021-10-19 15:40:10 +0200779 /* Ensure to remove this session from external lists
780 * Note: we are under the lock of dns_stream_server
781 * which own the heads of those lists.
782 */
783 LIST_DEL_INIT(&ds->waiter);
784 LIST_DEL_INIT(&ds->list);
785
Emeric Brunfd647d52021-02-12 20:03:38 +0100786 ds->dss->cur_conns--;
787 /* Note: this is useless to update
788 * max_active_conns here because
789 * we decrease the value
790 */
791 pool_free(dns_session_pool, ds);
792}
793
794static struct appctx *dns_session_create(struct dns_session *ds);
795
796/*
797 * Function to release a DNS tcp session
798 */
799static void dns_session_release(struct appctx *appctx)
800{
801 struct dns_session *ds = appctx->ctx.sft.ptr;
Willy Tarreaue3e648c2021-02-24 17:38:46 +0100802 struct dns_stream_server *dss __maybe_unused;
Emeric Brunfd647d52021-02-12 20:03:38 +0100803
804 if (!ds)
805 return;
806
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
840 /* We do not call ring_appctx_detach here
841 * because we want to keep readers counters
842 * to retry a con with a different appctx*/
843 HA_RWLOCK_WRLOCK(DNS_LOCK, &ds->ring.lock);
844 LIST_DEL_INIT(&appctx->wait_entry);
845 HA_RWLOCK_WRUNLOCK(DNS_LOCK, &ds->ring.lock);
846
847 /* if there is no pending complete response
848 * message, ensure to reset
849 * message offsets if the session
850 * was closed with an incomplete pending response
851 */
Willy Tarreau2b718102021-04-21 07:32:39 +0200852 if (!LIST_INLIST(&ds->waiter))
Emeric Brunfd647d52021-02-12 20:03:38 +0100853 ds->rx_msg.len = ds->rx_msg.offset = 0;
854
855 /* we flush pending sent queries because we never
856 * have responses
857 */
858 ds->nb_queries -= ds->onfly_queries;
859 dns_queries_flush(ds);
860
861 /* reset offset to be sure to start from message start */
862 ds->tx_msg_offset = 0;
863
864 /* here the ofs and the attached counter
865 * are kept unchanged
866 */
867
868 /* Create a new appctx, We hope we can
869 * create from the release callback! */
870 ds->appctx = dns_session_create(ds);
871 if (!ds->appctx) {
872 dns_session_free(ds);
873 HA_SPIN_UNLOCK(DNS_LOCK, &dss->lock);
874 return;
875 }
876
877 if (ds->nb_queries < DNS_STREAM_MAX_PIPELINED_REQ)
Willy Tarreau2b718102021-04-21 07:32:39 +0200878 LIST_INSERT(&ds->dss->free_sess, &ds->list);
Emeric Brunfd647d52021-02-12 20:03:38 +0100879
880 HA_SPIN_UNLOCK(DNS_LOCK, &dss->lock);
881}
882
883/* DNS tcp session applet */
884static struct applet dns_session_applet = {
885 .obj_type = OBJ_TYPE_APPLET,
886 .name = "<STRMDNS>", /* used for logging */
887 .fct = dns_session_io_handler,
888 .release = dns_session_release,
889};
890
891/*
892 * Function used to create an appctx for a DNS session
893 */
894static struct appctx *dns_session_create(struct dns_session *ds)
895{
896 struct appctx *appctx;
897 struct session *sess;
898 struct stream *s;
899 struct applet *applet = &dns_session_applet;
900
901 appctx = appctx_new(applet, tid_bit);
902 if (!appctx)
903 goto out_close;
904
905 appctx->ctx.sft.ptr = (void *)ds;
906
907 sess = session_new(ds->dss->srv->proxy, NULL, &appctx->obj_type);
908 if (!sess) {
909 ha_alert("out of memory in peer_session_create().\n");
910 goto out_free_appctx;
911 }
912
913 if ((s = stream_new(sess, &appctx->obj_type, &BUF_NULL)) == NULL) {
914 ha_alert("Failed to initialize stream in peer_session_create().\n");
915 goto out_free_sess;
916 }
917
918
919 s->target = &ds->dss->srv->obj_type;
920 if (!sockaddr_alloc(&s->target_addr, &ds->dss->srv->addr, sizeof(ds->dss->srv->addr)))
921 goto out_free_strm;
922 s->flags = SF_ASSIGNED|SF_ADDR_SET;
923 s->si[1].flags |= SI_FL_NOLINGER;
924
925 s->do_log = NULL;
926 s->uniq_id = 0;
927
928 s->res.flags |= CF_READ_DONTWAIT;
929 /* for rto and rex to eternity to not expire on idle recv:
930 * We are using a syslog server.
931 */
932 s->res.rto = TICK_ETERNITY;
933 s->res.rex = TICK_ETERNITY;
934 ds->appctx = appctx;
935 task_wakeup(s->task, TASK_WOKEN_INIT);
936 return appctx;
937
938 /* Error unrolling */
939 out_free_strm:
Willy Tarreau2b718102021-04-21 07:32:39 +0200940 LIST_DELETE(&s->list);
Emeric Brunfd647d52021-02-12 20:03:38 +0100941 pool_free(pool_head_stream, s);
942 out_free_sess:
943 session_free(sess);
944 out_free_appctx:
945 appctx_free(appctx);
946 out_close:
947 return NULL;
948}
949
950/* Task processing expiration of unresponded queries, this one is supposed
951 * to be stuck on the same thread than the appctx handler
952 */
Willy Tarreau144f84a2021-03-02 16:09:26 +0100953static struct task *dns_process_query_exp(struct task *t, void *context, unsigned int state)
Emeric Brunfd647d52021-02-12 20:03:38 +0100954{
955 struct dns_session *ds = (struct dns_session *)context;
956 struct dns_query *query, *queryb;
957
958 t->expire = TICK_ETERNITY;
959
960 list_for_each_entry_safe(query, queryb, &ds->queries, list) {
961 if (tick_is_expired(query->expire, now_ms)) {
962 eb32_delete(&query->qid);
Willy Tarreau2b718102021-04-21 07:32:39 +0200963 LIST_DELETE(&query->list);
Emeric Brunfd647d52021-02-12 20:03:38 +0100964 pool_free(dns_query_pool, query);
965 ds->onfly_queries--;
966 }
967 else {
968 t->expire = query->expire;
969 break;
970 }
971 }
972
973 return t;
974}
975
976/* Task processing expiration of idle sessions */
Willy Tarreau144f84a2021-03-02 16:09:26 +0100977static struct task *dns_process_idle_exp(struct task *t, void *context, unsigned int state)
Emeric Brunfd647d52021-02-12 20:03:38 +0100978{
979 struct dns_stream_server *dss = (struct dns_stream_server *)context;
980 struct dns_session *ds, *dsb;
981 int target = 0;
982 int cur_active_conns;
983
984 HA_SPIN_LOCK(DNS_LOCK, &dss->lock);
985
986
987 cur_active_conns = dss->cur_conns - dss->idle_conns;
988 if (cur_active_conns > dss->max_active_conns)
989 dss->max_active_conns = cur_active_conns;
990
991 target = (dss->max_active_conns - cur_active_conns) / 2;
992 list_for_each_entry_safe(ds, dsb, &dss->idle_sess, list) {
993 if (!target)
994 break;
995
996 /* remove conn to pending list to ensure it won't be reused */
997 LIST_DEL_INIT(&ds->list);
998
999 /* force session shutdown */
1000 ds->shutdown = 1;
1001
Ilya Shipitsin0de36ad2021-02-20 00:23:36 +05001002 /* to be sure that the appctx won't miss shutdown */
Emeric Brunfd647d52021-02-12 20:03:38 +01001003 __ha_barrier_store();
1004
1005 /* wake appctx to perform the shutdown */
1006 appctx_wakeup(ds->appctx);
1007 }
1008
1009 /* reset max to current active conns */
1010 dss->max_active_conns = cur_active_conns;
1011
1012 HA_SPIN_UNLOCK(DNS_LOCK, &dss->lock);
1013
1014 t->expire = tick_add(now_ms, 5000);
1015
1016 return t;
1017}
1018
1019struct dns_session *dns_session_new(struct dns_stream_server *dss)
1020{
1021 struct dns_session *ds;
1022
1023 if (dss->maxconn && (dss->maxconn <= dss->cur_conns))
1024 return NULL;
1025
Christopher Faulet5dce25f2022-08-03 10:30:06 +02001026 ds = pool_zalloc(dns_session_pool);
Emeric Brunfd647d52021-02-12 20:03:38 +01001027 if (!ds)
1028 return NULL;
1029
1030 ds->ofs = ~0;
1031 ds->dss = dss;
1032 LIST_INIT(&ds->list);
1033 LIST_INIT(&ds->queries);
1034 LIST_INIT(&ds->waiter);
1035 ds->rx_msg.offset = ds->rx_msg.len = 0;
1036 ds->rx_msg.area = NULL;
1037 ds->tx_ring_area = NULL;
1038 ds->task_exp = NULL;
1039 ds->appctx = NULL;
1040 ds->shutdown = 0;
1041 ds->nb_queries = 0;
1042 ds->query_ids = EB_ROOT_UNIQUE;
1043 ds->rx_msg.area = pool_alloc(dns_msg_buf);
1044 if (!ds->rx_msg.area)
1045 goto error;
1046
1047 ds->tx_ring_area = pool_alloc(dns_msg_buf);
1048 if (!ds->tx_ring_area)
1049 goto error;
1050
1051 ring_init(&ds->ring, ds->tx_ring_area, DNS_TCP_MSG_RING_MAX_SIZE);
Christopher Faulet1a1b6742021-03-04 16:53:27 +01001052 /* never fail because it is the first watcher attached to the ring */
1053 DISGUISE(ring_attach(&ds->ring));
Emeric Brunfd647d52021-02-12 20:03:38 +01001054
1055 if ((ds->task_exp = task_new(tid_bit)) == NULL)
1056 goto error;
1057
1058 ds->task_exp->process = dns_process_query_exp;
1059 ds->task_exp->context = ds;
1060
1061 ds->appctx = dns_session_create(ds);
1062 if (!ds->appctx)
1063 goto error;
1064
1065 dss->cur_conns++;
1066
1067 return ds;
1068
1069error:
1070 if (ds->task_exp)
1071 task_destroy(ds->task_exp);
1072 if (ds->rx_msg.area)
1073 pool_free(dns_msg_buf, ds->rx_msg.area);
1074 if (ds->tx_ring_area)
1075 pool_free(dns_msg_buf, ds->tx_ring_area);
1076
1077 pool_free(dns_session_pool, ds);
1078
1079 return NULL;
1080}
1081
1082/*
1083 * Task used to consume pending messages from nameserver ring
1084 * and forward them to dns_session ring.
1085 * Note: If no slot found a new dns_session is allocated
1086 */
Willy Tarreau144f84a2021-03-02 16:09:26 +01001087static struct task *dns_process_req(struct task *t, void *context, unsigned int state)
Emeric Brunfd647d52021-02-12 20:03:38 +01001088{
1089 struct dns_nameserver *ns = (struct dns_nameserver *)context;
1090 struct dns_stream_server *dss = ns->stream;
1091 struct ring *ring = dss->ring_req;
1092 struct buffer *buf = &ring->buf;
1093 uint64_t msg_len;
1094 size_t len, cnt, ofs;
1095 struct dns_session *ds, *ads;
1096 HA_SPIN_LOCK(DNS_LOCK, &dss->lock);
1097
1098 ofs = dss->ofs_req;
1099
1100 HA_RWLOCK_RDLOCK(DNS_LOCK, &ring->lock);
1101
1102 /* explanation for the initialization below: it would be better to do
1103 * this in the parsing function but this would occasionally result in
1104 * dropped events because we'd take a reference on the oldest message
1105 * and keep it while being scheduled. Thus instead let's take it the
1106 * first time we enter here so that we have a chance to pass many
1107 * existing messages before grabbing a reference to a location. This
1108 * value cannot be produced after initialization.
1109 */
1110 if (unlikely(ofs == ~0)) {
1111 ofs = 0;
Willy Tarreau4781b152021-04-06 13:53:36 +02001112 HA_ATOMIC_INC(b_peek(buf, ofs));
Emeric Brunfd647d52021-02-12 20:03:38 +01001113 ofs += ring->ofs;
1114 }
1115
1116 /* we were already there, adjust the offset to be relative to
1117 * the buffer's head and remove us from the counter.
1118 */
1119 ofs -= ring->ofs;
1120 BUG_ON(ofs >= buf->size);
Willy Tarreau4781b152021-04-06 13:53:36 +02001121 HA_ATOMIC_DEC(b_peek(buf, ofs));
Emeric Brunfd647d52021-02-12 20:03:38 +01001122
1123 while (ofs + 1 < b_data(buf)) {
1124 struct ist myist;
1125
1126 cnt = 1;
1127 len = b_peek_varint(buf, ofs + cnt, &msg_len);
1128 if (!len)
1129 break;
1130 cnt += len;
1131 BUG_ON(msg_len + ofs + cnt + 1 > b_data(buf));
1132 if (unlikely(msg_len > DNS_TCP_MSG_MAX_SIZE)) {
1133 /* too large a message to ever fit, let's skip it */
1134 ofs += cnt + msg_len;
1135 continue;
1136 }
1137
1138 len = b_getblk(buf, dns_msg_trash, msg_len, ofs + cnt);
1139
Tim Duesterhus92c696e2021-02-28 16:11:36 +01001140 myist = ist2(dns_msg_trash, len);
Emeric Brunfd647d52021-02-12 20:03:38 +01001141
1142 ads = NULL;
Ilya Shipitsin0de36ad2021-02-20 00:23:36 +05001143 /* try to push request into active sess with free slot */
Emeric Brunfd647d52021-02-12 20:03:38 +01001144 if (!LIST_ISEMPTY(&dss->free_sess)) {
1145 ds = LIST_NEXT(&dss->free_sess, struct dns_session *, list);
1146
1147 if (ring_write(&ds->ring, DNS_TCP_MSG_MAX_SIZE, NULL, 0, &myist, 1) > 0) {
1148 ds->nb_queries++;
1149 if (ds->nb_queries >= DNS_STREAM_MAX_PIPELINED_REQ)
1150 LIST_DEL_INIT(&ds->list);
1151 ads = ds;
1152 }
1153 else {
1154 /* it means we were unable to put a request in this slot,
1155 * it may be close to be full so we put it at the end
1156 * of free conn list */
1157 LIST_DEL_INIT(&ds->list);
Willy Tarreau2b718102021-04-21 07:32:39 +02001158 LIST_APPEND(&dss->free_sess, &ds->list);
Emeric Brunfd647d52021-02-12 20:03:38 +01001159 }
1160 }
1161
1162 if (!ads) {
Ilya Shipitsin0de36ad2021-02-20 00:23:36 +05001163 /* try to push request into idle, this one should have enough free space */
Emeric Brunfd647d52021-02-12 20:03:38 +01001164 if (!LIST_ISEMPTY(&dss->idle_sess)) {
1165 ds = LIST_NEXT(&dss->idle_sess, struct dns_session *, list);
1166
1167 /* ring is empty so this ring_write should never fail */
1168 ring_write(&ds->ring, DNS_TCP_MSG_MAX_SIZE, NULL, 0, &myist, 1);
1169 ds->nb_queries++;
1170 LIST_DEL_INIT(&ds->list);
1171
1172 ds->dss->idle_conns--;
1173
1174 /* we may have to update the max_active_conns */
1175 if (ds->dss->max_active_conns < ds->dss->cur_conns - ds->dss->idle_conns)
1176 ds->dss->max_active_conns = ds->dss->cur_conns - ds->dss->idle_conns;
1177
1178 /* since we may unable to find a free list to handle
1179 * this request, this request may be large and fill
1180 * the ring buffer so we prefer to put at the end of free
1181 * list. */
Willy Tarreau2b718102021-04-21 07:32:39 +02001182 LIST_APPEND(&dss->free_sess, &ds->list);
Emeric Brunfd647d52021-02-12 20:03:38 +01001183 ads = ds;
1184 }
1185 }
1186
Ilya Shipitsin0de36ad2021-02-20 00:23:36 +05001187 /* we didn't find a session available with large enough room */
Emeric Brunfd647d52021-02-12 20:03:38 +01001188 if (!ads) {
1189 /* allocate a new session */
1190 ads = dns_session_new(dss);
1191 if (ads) {
1192 /* ring is empty so this ring_write should never fail */
1193 ring_write(&ads->ring, DNS_TCP_MSG_MAX_SIZE, NULL, 0, &myist, 1);
1194 ads->nb_queries++;
Willy Tarreau2b718102021-04-21 07:32:39 +02001195 LIST_INSERT(&dss->free_sess, &ads->list);
Emeric Brunfd647d52021-02-12 20:03:38 +01001196 }
1197 else
1198 ns->counters->snd_error++;
1199 }
1200
1201 if (ads)
1202 ns->counters->sent++;
1203
1204 ofs += cnt + len;
1205 }
1206
Willy Tarreau4781b152021-04-06 13:53:36 +02001207 HA_ATOMIC_INC(b_peek(buf, ofs));
Emeric Brunfd647d52021-02-12 20:03:38 +01001208 ofs += ring->ofs;
1209 dss->ofs_req = ofs;
1210 HA_RWLOCK_RDUNLOCK(DNS_LOCK, &ring->lock);
1211
1212
1213 HA_SPIN_UNLOCK(DNS_LOCK, &dss->lock);
1214 return t;
1215}
1216
1217/*
1218 * Task used to consume response
1219 * Note: upper layer callback is called
1220 */
Willy Tarreau144f84a2021-03-02 16:09:26 +01001221static struct task *dns_process_rsp(struct task *t, void *context, unsigned int state)
Emeric Brunfd647d52021-02-12 20:03:38 +01001222{
1223 struct dns_nameserver *ns = (struct dns_nameserver *)context;
1224
1225 ns->process_responses(ns);
1226
1227 return t;
1228}
1229
1230/* Function used to initialize an TCP nameserver */
1231int dns_stream_init(struct dns_nameserver *ns, struct server *srv)
1232{
1233 struct dns_stream_server *dss = NULL;
1234
1235 dss = calloc(1, sizeof(*dss));
1236 if (!dss) {
1237 ha_alert("memory allocation error initializing dns tcp server '%s'.\n", srv->id);
1238 goto out;
1239 }
1240
1241 dss->srv = srv;
1242 dss->maxconn = srv->maxconn;
1243
1244 dss->ofs_req = ~0; /* init ring offset */
1245 dss->ring_req = ring_new(2*DNS_TCP_MSG_RING_MAX_SIZE);
1246 if (!dss->ring_req) {
1247 ha_alert("memory allocation error initializing the ring for dns tcp server '%s'.\n", srv->id);
1248 goto out;
1249 }
1250 /* Create the task associated to the resolver target handling conns */
1251 if ((dss->task_req = task_new(MAX_THREADS_MASK)) == NULL) {
1252 ha_alert("memory allocation error initializing the ring for dns tcp server '%s'.\n", srv->id);
1253 goto out;
1254 }
1255
1256 /* Update task's parameters */
1257 dss->task_req->process = dns_process_req;
1258 dss->task_req->context = ns;
1259
1260 /* attach the task as reader */
1261 if (!ring_attach(dss->ring_req)) {
1262 /* mark server attached to the ring */
1263 ha_alert("server '%s': too many watchers for ring. this should never happen.\n", srv->id);
1264 goto out;
1265 }
1266
1267 /* Create the task associated to the resolver target handling conns */
1268 if ((dss->task_rsp = task_new(MAX_THREADS_MASK)) == NULL) {
1269 ha_alert("memory allocation error initializing the ring for dns tcp server '%s'.\n", srv->id);
1270 goto out;
1271 }
1272
1273 /* Update task's parameters */
1274 dss->task_rsp->process = dns_process_rsp;
1275 dss->task_rsp->context = ns;
1276
1277 /* Create the task associated to the resolver target handling conns */
1278 if ((dss->task_idle = task_new(MAX_THREADS_MASK)) == NULL) {
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_idle->process = dns_process_idle_exp;
1285 dss->task_idle->context = dss;
1286 dss->task_idle->expire = tick_add(now_ms, 5000);
1287
Ilya Shipitsin0de36ad2021-02-20 00:23:36 +05001288 /* let start the task to free idle conns immediately */
Emeric Brunfd647d52021-02-12 20:03:38 +01001289 task_queue(dss->task_idle);
1290
1291 LIST_INIT(&dss->free_sess);
1292 LIST_INIT(&dss->idle_sess);
1293 LIST_INIT(&dss->wait_sess);
1294 HA_SPIN_INIT(&dss->lock);
1295 ns->stream = dss;
1296 return 0;
1297out:
1298 if (dss && dss->task_rsp)
1299 task_destroy(dss->task_rsp);
1300 if (dss && dss->task_req)
1301 task_destroy(dss->task_req);
1302 if (dss && dss->ring_req)
1303 ring_free(dss->ring_req);
1304
1305 free(dss);
Emeric Brunc9437992021-02-12 19:42:55 +01001306 return -1;
Christopher Faulet67957bd2017-09-27 11:00:59 +02001307}
1308
Emeric Brunc9437992021-02-12 19:42:55 +01001309int init_dns_buffers()
Baptiste Assmann325137d2015-04-13 23:40:55 +02001310{
Emeric Brunc9437992021-02-12 19:42:55 +01001311 dns_msg_trash = malloc(DNS_TCP_MSG_MAX_SIZE);
1312 if (!dns_msg_trash)
1313 return 0;
Baptiste Assmann325137d2015-04-13 23:40:55 +02001314
Emeric Brunc9437992021-02-12 19:42:55 +01001315 return 1;
1316}
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +02001317
Emeric Brunc9437992021-02-12 19:42:55 +01001318void deinit_dns_buffers()
1319{
Willy Tarreau61cfdf42021-02-20 10:46:51 +01001320 ha_free(&dns_msg_trash);
Emeric Brunc9437992021-02-12 19:42:55 +01001321}
Emeric Brund26a6232021-01-04 13:32:20 +01001322
1323REGISTER_PER_THREAD_ALLOC(init_dns_buffers);
1324REGISTER_PER_THREAD_FREE(deinit_dns_buffers);