2011-07-06 05:05:52 +00:00
|
|
|
/*
|
2018-02-23 09:53:12 +01:00
|
|
|
* Copyright (C) Internet Systems Consortium, Inc. ("ISC")
|
2011-07-06 05:05:52 +00:00
|
|
|
*
|
2016-06-27 14:56:38 +10:00
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
2020-09-14 16:20:40 -07:00
|
|
|
* file, you can obtain one at https://mozilla.org/MPL/2.0/.
|
2018-02-23 09:53:12 +01:00
|
|
|
*
|
|
|
|
* See the COPYRIGHT file distributed with this work for additional
|
|
|
|
* information regarding copyright ownership.
|
2011-07-06 05:05:52 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
/*! \file */
|
|
|
|
|
2020-02-12 13:59:18 +01:00
|
|
|
#include "isctest.h"
|
2018-03-28 14:19:37 +02:00
|
|
|
#include <inttypes.h>
|
2018-04-17 08:29:14 -07:00
|
|
|
#include <stdbool.h>
|
2018-02-27 14:29:49 -08:00
|
|
|
#include <stdlib.h>
|
2011-09-02 21:15:39 +00:00
|
|
|
#include <time.h>
|
|
|
|
|
2011-07-06 05:05:52 +00:00
|
|
|
#include <isc/buffer.h>
|
|
|
|
#include <isc/hash.h>
|
2020-07-14 14:24:10 +02:00
|
|
|
#include <isc/hp.h>
|
2011-07-06 05:05:52 +00:00
|
|
|
#include <isc/mem.h>
|
|
|
|
#include <isc/os.h>
|
2011-07-28 04:04:37 +00:00
|
|
|
#include <isc/socket.h>
|
2011-07-06 05:05:52 +00:00
|
|
|
#include <isc/string.h>
|
2011-07-28 04:04:37 +00:00
|
|
|
#include <isc/task.h>
|
|
|
|
#include <isc/timer.h>
|
2011-07-06 05:05:52 +00:00
|
|
|
#include <isc/util.h>
|
|
|
|
|
2020-02-13 14:44:37 -08:00
|
|
|
isc_mem_t *test_mctx = NULL;
|
|
|
|
isc_log_t *test_lctx = NULL;
|
|
|
|
isc_taskmgr_t *taskmgr = NULL;
|
|
|
|
isc_timermgr_t *timermgr = NULL;
|
2011-07-28 04:04:37 +00:00
|
|
|
isc_socketmgr_t *socketmgr = NULL;
|
2020-02-13 14:44:37 -08:00
|
|
|
isc_nm_t *netmgr = NULL;
|
|
|
|
isc_task_t *maintask = NULL;
|
|
|
|
int ncpus;
|
2011-07-06 05:05:52 +00:00
|
|
|
|
2018-10-25 17:02:46 -07:00
|
|
|
static bool test_running = false;
|
|
|
|
|
2011-07-06 05:05:52 +00:00
|
|
|
/*
|
|
|
|
* Logging categories: this needs to match the list in bin/named/log.c.
|
|
|
|
*/
|
2020-02-12 13:59:18 +01:00
|
|
|
static isc_logcategory_t categories[] = { { "", 0 },
|
|
|
|
{ "client", 0 },
|
|
|
|
{ "network", 0 },
|
|
|
|
{ "update", 0 },
|
|
|
|
{ "queries", 0 },
|
|
|
|
{ "unmatched", 0 },
|
|
|
|
{ "update-security", 0 },
|
|
|
|
{ "query-errors", 0 },
|
|
|
|
{ NULL, 0 } };
|
2011-07-06 05:05:52 +00:00
|
|
|
|
2011-07-28 04:04:37 +00:00
|
|
|
static void
|
2020-02-13 14:44:37 -08:00
|
|
|
cleanup_managers(void) {
|
2018-10-25 17:02:46 -07:00
|
|
|
if (maintask != NULL) {
|
|
|
|
isc_task_shutdown(maintask);
|
2012-07-19 23:00:21 +10:00
|
|
|
isc_task_destroy(&maintask);
|
2018-10-25 17:02:46 -07:00
|
|
|
}
|
|
|
|
if (socketmgr != NULL) {
|
2011-07-28 04:04:37 +00:00
|
|
|
isc_socketmgr_destroy(&socketmgr);
|
2018-10-25 17:02:46 -07:00
|
|
|
}
|
|
|
|
if (taskmgr != NULL) {
|
2011-07-28 04:04:37 +00:00
|
|
|
isc_taskmgr_destroy(&taskmgr);
|
2018-10-25 17:02:46 -07:00
|
|
|
}
|
Refactor taskmgr to run on top of netmgr
This commit changes the taskmgr to run the individual tasks on the
netmgr internal workers. While an effort has been put into keeping the
taskmgr interface intact, couple of changes have been made:
* The taskmgr has no concept of universal privileged mode - rather the
tasks are either privileged or unprivileged (normal). The privileged
tasks are run as a first thing when the netmgr is unpaused. There
are now four different queues in in the netmgr:
1. priority queue - netievent on the priority queue are run even when
the taskmgr enter exclusive mode and netmgr is paused. This is
needed to properly start listening on the interfaces, free
resources and resume.
2. privileged task queue - only privileged tasks are queued here and
this is the first queue that gets processed when network manager
is unpaused using isc_nm_resume(). All netmgr workers need to
clean the privileged task queue before they all proceed normal
operation. Both task queues are processed when the workers are
finished.
3. task queue - only (traditional) task are scheduled here and this
queue along with privileged task queues are process when the
netmgr workers are finishing. This is needed to process the task
shutdown events.
4. normal queue - this is the queue with netmgr events, e.g. reading,
sending, callbacks and pretty much everything is processed here.
* The isc_taskmgr_create() now requires initialized netmgr (isc_nm_t)
object.
* The isc_nm_destroy() function now waits for indefinite time, but it
will print out the active objects when in tracing mode
(-DNETMGR_TRACE=1 and -DNETMGR_TRACE_VERBOSE=1), the netmgr has been
made a little bit more asynchronous and it might take longer time to
shutdown all the active networking connections.
* Previously, the isc_nm_stoplistening() was a synchronous operation.
This has been changed and the isc_nm_stoplistening() just schedules
the child sockets to stop listening and exits. This was needed to
prevent a deadlock as the the (traditional) tasks are now executed on
the netmgr threads.
* The socket selection logic in isc__nm_udp_send() was flawed, but
fortunatelly, it was broken, so we never hit the problem where we
created uvreq_t on a socket from nmhandle_t, but then a different
socket could be picked up and then we were trying to run the send
callback on a socket that had different threadid than currently
running.
2021-04-09 11:31:19 +02:00
|
|
|
if (netmgr != NULL) {
|
|
|
|
isc_nm_destroy(&netmgr);
|
|
|
|
}
|
2018-10-25 17:02:46 -07:00
|
|
|
if (timermgr != NULL) {
|
2011-07-28 04:04:37 +00:00
|
|
|
isc_timermgr_destroy(&timermgr);
|
2018-10-25 17:02:46 -07:00
|
|
|
}
|
2011-07-28 04:04:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static isc_result_t
|
2020-02-13 14:44:37 -08:00
|
|
|
create_managers(unsigned int workers) {
|
2011-07-28 04:04:37 +00:00
|
|
|
isc_result_t result;
|
2020-02-13 14:44:37 -08:00
|
|
|
char *p;
|
2018-02-27 14:29:49 -08:00
|
|
|
|
|
|
|
if (workers == 0) {
|
|
|
|
workers = isc_os_ncpus();
|
|
|
|
}
|
|
|
|
p = getenv("ISC_TASK_WORKERS");
|
|
|
|
if (p != NULL) {
|
|
|
|
workers = atoi(p);
|
|
|
|
}
|
Improve reliability of the netmgr unit tests
The netmgr unit tests were designed to push the system limits to maximum
by sending as many queries as possible in the busy loop from multiple
threads. This mostly works with UDP, but in the stateful protocol where
establishing the connection takes more time, it failed quite often in
the CI. On FreeBSD, this happened more often, because the socket() call
would fail spuriosly making the problem even worse.
This commit does several things to improve reliability:
* return value of isc_nm_<proto>connect() is always checked and retried
when scheduling the connection fails
* The busy while loop has been slowed down with usleep(1000); so the
netmgr threads could schedule the work and get executed.
* The isc_thread_yield() was replaced with usleep(1000); also to allow
the other threads to do any work.
* Instead of waiting on just one variable, we wait for multiple
variables to reach the final value
* We are wrapping the netmgr operations (connects, reads, writes,
accepts) with reference counting and waiting for all the callbacks to
be accounted for.
This has two effects:
a) the isc_nm_t is always clean of active sockets and handles when
destroyed, so it will prevent the spurious INSIST(references == 1)
from isc_nm_destroy()
b) the unit test now ensures that all the callbacks are always called
when they should be called, so any stuck test means that there was
a missing callback call and it is always a real bug
These changes allows us to remove the workaround that would not run
certain tests on systems without port load-balancing.
2021-01-29 13:00:46 +01:00
|
|
|
INSIST(workers != 0);
|
2011-07-28 04:04:37 +00:00
|
|
|
|
2021-03-10 14:30:16 +02:00
|
|
|
isc_hp_init(6 * workers);
|
2020-07-14 14:24:10 +02:00
|
|
|
|
2019-11-12 23:13:49 -08:00
|
|
|
netmgr = isc_nm_start(test_mctx, workers);
|
Refactor taskmgr to run on top of netmgr
This commit changes the taskmgr to run the individual tasks on the
netmgr internal workers. While an effort has been put into keeping the
taskmgr interface intact, couple of changes have been made:
* The taskmgr has no concept of universal privileged mode - rather the
tasks are either privileged or unprivileged (normal). The privileged
tasks are run as a first thing when the netmgr is unpaused. There
are now four different queues in in the netmgr:
1. priority queue - netievent on the priority queue are run even when
the taskmgr enter exclusive mode and netmgr is paused. This is
needed to properly start listening on the interfaces, free
resources and resume.
2. privileged task queue - only privileged tasks are queued here and
this is the first queue that gets processed when network manager
is unpaused using isc_nm_resume(). All netmgr workers need to
clean the privileged task queue before they all proceed normal
operation. Both task queues are processed when the workers are
finished.
3. task queue - only (traditional) task are scheduled here and this
queue along with privileged task queues are process when the
netmgr workers are finishing. This is needed to process the task
shutdown events.
4. normal queue - this is the queue with netmgr events, e.g. reading,
sending, callbacks and pretty much everything is processed here.
* The isc_taskmgr_create() now requires initialized netmgr (isc_nm_t)
object.
* The isc_nm_destroy() function now waits for indefinite time, but it
will print out the active objects when in tracing mode
(-DNETMGR_TRACE=1 and -DNETMGR_TRACE_VERBOSE=1), the netmgr has been
made a little bit more asynchronous and it might take longer time to
shutdown all the active networking connections.
* Previously, the isc_nm_stoplistening() was a synchronous operation.
This has been changed and the isc_nm_stoplistening() just schedules
the child sockets to stop listening and exits. This was needed to
prevent a deadlock as the the (traditional) tasks are now executed on
the netmgr threads.
* The socket selection logic in isc__nm_udp_send() was flawed, but
fortunatelly, it was broken, so we never hit the problem where we
created uvreq_t on a socket from nmhandle_t, but then a different
socket could be picked up and then we were trying to run the send
callback on a socket that had different threadid than currently
running.
2021-04-09 11:31:19 +02:00
|
|
|
CHECK(isc_taskmgr_create(test_mctx, 0, netmgr, &taskmgr));
|
2012-07-19 23:00:21 +10:00
|
|
|
CHECK(isc_task_create(taskmgr, 0, &maintask));
|
|
|
|
isc_taskmgr_setexcltask(taskmgr, maintask);
|
|
|
|
|
2019-11-09 14:01:08 +01:00
|
|
|
CHECK(isc_timermgr_create(test_mctx, &timermgr));
|
|
|
|
CHECK(isc_socketmgr_create(test_mctx, &socketmgr));
|
2011-07-28 04:04:37 +00:00
|
|
|
return (ISC_R_SUCCESS);
|
|
|
|
|
2020-02-12 13:59:18 +01:00
|
|
|
cleanup:
|
2011-07-28 04:04:37 +00:00
|
|
|
cleanup_managers();
|
|
|
|
return (result);
|
|
|
|
}
|
|
|
|
|
2011-07-06 05:05:52 +00:00
|
|
|
isc_result_t
|
2020-02-13 14:44:37 -08:00
|
|
|
isc_test_begin(FILE *logfile, bool start_managers, unsigned int workers) {
|
2011-07-06 05:05:52 +00:00
|
|
|
isc_result_t result;
|
|
|
|
|
2018-10-25 17:02:46 -07:00
|
|
|
INSIST(!test_running);
|
|
|
|
test_running = true;
|
|
|
|
|
2011-07-06 05:05:52 +00:00
|
|
|
isc_mem_debugging |= ISC_MEM_DEBUGRECORD;
|
2018-10-25 17:02:46 -07:00
|
|
|
|
2019-11-09 14:01:08 +01:00
|
|
|
INSIST(test_mctx == NULL);
|
|
|
|
isc_mem_create(&test_mctx);
|
2011-07-06 05:05:52 +00:00
|
|
|
|
|
|
|
if (logfile != NULL) {
|
|
|
|
isc_logdestination_t destination;
|
2020-02-13 14:44:37 -08:00
|
|
|
isc_logconfig_t *logconfig = NULL;
|
2011-07-06 05:05:52 +00:00
|
|
|
|
2019-11-09 14:01:08 +01:00
|
|
|
INSIST(test_lctx == NULL);
|
2020-03-18 14:17:55 +11:00
|
|
|
isc_log_create(test_mctx, &test_lctx, &logconfig);
|
2019-11-09 14:01:08 +01:00
|
|
|
isc_log_registercategories(test_lctx, categories);
|
|
|
|
isc_log_setcontext(test_lctx);
|
2011-07-06 05:05:52 +00:00
|
|
|
|
|
|
|
destination.file.stream = logfile;
|
|
|
|
destination.file.name = NULL;
|
|
|
|
destination.file.versions = ISC_LOG_ROLLNEVER;
|
|
|
|
destination.file.maximum_size = 0;
|
2020-03-18 14:17:55 +11:00
|
|
|
isc_log_createchannel(logconfig, "stderr", ISC_LOG_TOFILEDESC,
|
|
|
|
ISC_LOG_DYNAMIC, &destination, 0);
|
2011-07-06 05:05:52 +00:00
|
|
|
CHECK(isc_log_usechannel(logconfig, "stderr", NULL, NULL));
|
|
|
|
}
|
|
|
|
|
|
|
|
ncpus = isc_os_ncpus();
|
|
|
|
|
2018-02-27 14:29:49 -08:00
|
|
|
if (start_managers) {
|
|
|
|
CHECK(create_managers(workers));
|
|
|
|
}
|
2011-07-06 05:05:52 +00:00
|
|
|
|
|
|
|
return (ISC_R_SUCCESS);
|
|
|
|
|
2020-02-12 13:59:18 +01:00
|
|
|
cleanup:
|
2011-07-06 05:05:52 +00:00
|
|
|
isc_test_end();
|
|
|
|
return (result);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2020-02-13 14:44:37 -08:00
|
|
|
isc_test_end(void) {
|
2018-10-25 17:02:46 -07:00
|
|
|
if (maintask != NULL) {
|
2012-07-19 23:00:21 +10:00
|
|
|
isc_task_detach(&maintask);
|
2018-10-25 17:02:46 -07:00
|
|
|
}
|
|
|
|
if (taskmgr != NULL) {
|
2011-07-06 05:05:52 +00:00
|
|
|
isc_taskmgr_destroy(&taskmgr);
|
2018-10-25 17:02:46 -07:00
|
|
|
}
|
2011-07-28 04:04:37 +00:00
|
|
|
|
|
|
|
cleanup_managers();
|
|
|
|
|
2019-11-09 14:01:08 +01:00
|
|
|
if (test_lctx != NULL) {
|
|
|
|
isc_log_destroy(&test_lctx);
|
2018-10-25 17:02:46 -07:00
|
|
|
}
|
2019-11-09 14:01:08 +01:00
|
|
|
if (test_mctx != NULL) {
|
|
|
|
isc_mem_destroy(&test_mctx);
|
2018-10-25 17:02:46 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
test_running = false;
|
2011-07-06 05:05:52 +00:00
|
|
|
}
|
|
|
|
|
2011-09-02 21:15:39 +00:00
|
|
|
/*
|
|
|
|
* Sleep for 'usec' microseconds.
|
|
|
|
*/
|
|
|
|
void
|
2020-02-13 14:44:37 -08:00
|
|
|
isc_test_nap(uint32_t usec) {
|
2011-09-02 21:15:39 +00:00
|
|
|
struct timespec ts;
|
|
|
|
|
|
|
|
ts.tv_sec = usec / 1000000;
|
|
|
|
ts.tv_nsec = (usec % 1000000) * 1000;
|
|
|
|
nanosleep(&ts, NULL);
|
|
|
|
}
|