1 1.1 christos /* 2 1.1 christos * daemon/worker.h - worker that handles a pending list of requests. 3 1.1 christos * 4 1.1 christos * Copyright (c) 2007, NLnet Labs. All rights reserved. 5 1.1 christos * 6 1.1 christos * This software is open source. 7 1.1 christos * 8 1.1 christos * Redistribution and use in source and binary forms, with or without 9 1.1 christos * modification, are permitted provided that the following conditions 10 1.1 christos * are met: 11 1.1 christos * 12 1.1 christos * Redistributions of source code must retain the above copyright notice, 13 1.1 christos * this list of conditions and the following disclaimer. 14 1.1 christos * 15 1.1 christos * Redistributions in binary form must reproduce the above copyright notice, 16 1.1 christos * this list of conditions and the following disclaimer in the documentation 17 1.1 christos * and/or other materials provided with the distribution. 18 1.1 christos * 19 1.1 christos * Neither the name of the NLNET LABS nor the names of its contributors may 20 1.1 christos * be used to endorse or promote products derived from this software without 21 1.1 christos * specific prior written permission. 22 1.1 christos * 23 1.1 christos * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 24 1.1 christos * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 25 1.1 christos * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 26 1.1 christos * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 27 1.1 christos * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 28 1.1 christos * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED 29 1.1 christos * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 30 1.1 christos * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 31 1.1 christos * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 32 1.1 christos * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 33 1.1 christos * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 34 1.1 christos */ 35 1.1 christos 36 1.1 christos /** 37 1.1 christos * \file 38 1.1 christos * 39 1.1 christos * This file describes the worker structure that holds a list of 40 1.1 christos * pending requests and handles them. 41 1.1 christos */ 42 1.1 christos 43 1.1 christos #ifndef DAEMON_WORKER_H 44 1.1 christos #define DAEMON_WORKER_H 45 1.1 christos 46 1.1 christos #include "libunbound/worker.h" 47 1.1 christos #include "util/netevent.h" 48 1.1 christos #include "util/locks.h" 49 1.1 christos #include "util/alloc.h" 50 1.1 christos #include "util/data/msgreply.h" 51 1.1 christos #include "util/data/msgparse.h" 52 1.1 christos #include "daemon/stats.h" 53 1.1 christos #include "util/module.h" 54 1.1 christos #include "dnstap/dnstap.h" 55 1.1 christos struct listen_dnsport; 56 1.1 christos struct outside_network; 57 1.1 christos struct config_file; 58 1.1 christos struct daemon; 59 1.1 christos struct listen_port; 60 1.1 christos struct ub_randstate; 61 1.1 christos struct regional; 62 1.1 christos struct tube; 63 1.1 christos struct daemon_remote; 64 1.1.1.2 christos struct query_info; 65 1.1 christos 66 1.1 christos /** worker commands */ 67 1.1 christos enum worker_commands { 68 1.1 christos /** make the worker quit */ 69 1.1 christos worker_cmd_quit, 70 1.1 christos /** obtain statistics */ 71 1.1 christos worker_cmd_stats, 72 1.1 christos /** obtain statistics without statsclear */ 73 1.1 christos worker_cmd_stats_noreset, 74 1.1 christos /** execute remote control command */ 75 1.1.1.4 christos worker_cmd_remote, 76 1.1.1.4 christos /** for fast-reload, perform stop */ 77 1.1.1.4 christos worker_cmd_reload_stop, 78 1.1.1.4 christos /** for fast-reload, start again */ 79 1.1.1.4 christos worker_cmd_reload_start, 80 1.1.1.4 christos /** for fast-reload, poll to make sure worker has released data */ 81 1.1.1.4 christos worker_cmd_reload_poll 82 1.1 christos }; 83 1.1 christos 84 1.1 christos /** 85 1.1 christos * Structure holding working information for unbound. 86 1.1 christos * Holds globally visible information. 87 1.1 christos */ 88 1.1 christos struct worker { 89 1.1 christos /** the thread number (in daemon array). First in struct for debug. */ 90 1.1 christos int thread_num; 91 1.1 christos /** global shared daemon structure */ 92 1.1 christos struct daemon* daemon; 93 1.1 christos /** thread id */ 94 1.1.1.2 christos ub_thread_type thr_id; 95 1.1.1.3 christos #ifdef HAVE_GETTID 96 1.1.1.3 christos /** thread tid, the LWP id. */ 97 1.1.1.3 christos pid_t thread_tid; 98 1.1.1.3 christos #endif 99 1.1 christos /** pipe, for commands for this worker */ 100 1.1 christos struct tube* cmd; 101 1.1 christos /** the event base this worker works with */ 102 1.1 christos struct comm_base* base; 103 1.1 christos /** the frontside listening interface where request events come in */ 104 1.1 christos struct listen_dnsport* front; 105 1.1 christos /** the backside outside network interface to the auth servers */ 106 1.1 christos struct outside_network* back; 107 1.1 christos /** ports to be used by this worker. */ 108 1.1 christos int* ports; 109 1.1 christos /** number of ports for this worker */ 110 1.1 christos int numports; 111 1.1 christos /** the signal handler */ 112 1.1 christos struct comm_signal* comsig; 113 1.1 christos /** commpoint to listen to commands. */ 114 1.1 christos struct comm_point* cmd_com; 115 1.1 christos /** timer for statistics */ 116 1.1 christos struct comm_timer* stat_timer; 117 1.1 christos /** ratelimit for errors, time value */ 118 1.1 christos time_t err_limit_time; 119 1.1 christos /** ratelimit for errors, packet count */ 120 1.1 christos unsigned int err_limit_count; 121 1.1 christos 122 1.1 christos /** random() table for this worker. */ 123 1.1 christos struct ub_randstate* rndstate; 124 1.1 christos /** do we need to restart or quit (on signal) */ 125 1.1 christos int need_to_exit; 126 1.1 christos /** allocation cache for this thread */ 127 1.1.1.3 christos struct alloc_cache *alloc; 128 1.1 christos /** per thread statistics */ 129 1.1.1.2 christos struct ub_server_stats stats; 130 1.1 christos /** thread scratch regional */ 131 1.1 christos struct regional* scratchpad; 132 1.1 christos 133 1.1 christos /** module environment passed to modules, changed for this thread */ 134 1.1 christos struct module_env env; 135 1.1 christos 136 1.1 christos #ifdef USE_DNSTAP 137 1.1 christos /** dnstap environment, changed for this thread */ 138 1.1 christos struct dt_env dtenv; 139 1.1 christos #endif 140 1.1.1.3 christos /** reuse existing cache on reload if other conditions allow it. */ 141 1.1.1.3 christos int reuse_cache; 142 1.1 christos }; 143 1.1 christos 144 1.1 christos /** 145 1.1 christos * Create the worker structure. Bare bones version, zeroed struct, 146 1.1 christos * with backpointers only. Use worker_init on it later. 147 1.1 christos * @param daemon: the daemon that this worker thread is part of. 148 1.1 christos * @param id: the thread number from 0.. numthreads-1. 149 1.1 christos * @param ports: the ports it is allowed to use, array. 150 1.1 christos * @param n: the number of ports. 151 1.1 christos * @return: the new worker or NULL on alloc failure. 152 1.1 christos */ 153 1.1 christos struct worker* worker_create(struct daemon* daemon, int id, int* ports, int n); 154 1.1 christos 155 1.1 christos /** 156 1.1 christos * Initialize worker. 157 1.1 christos * Allocates event base, listens to ports 158 1.1 christos * @param worker: worker to initialize, created with worker_create. 159 1.1 christos * @param cfg: configuration settings. 160 1.1 christos * @param ports: list of shared query ports. 161 1.1 christos * @param do_sigs: if true, worker installs signal handlers. 162 1.1 christos * @return: false on error. 163 1.1 christos */ 164 1.1 christos int worker_init(struct worker* worker, struct config_file *cfg, 165 1.1 christos struct listen_port* ports, int do_sigs); 166 1.1 christos 167 1.1 christos /** 168 1.1 christos * Make worker work. 169 1.1 christos */ 170 1.1 christos void worker_work(struct worker* worker); 171 1.1 christos 172 1.1 christos /** 173 1.1 christos * Delete worker. 174 1.1 christos */ 175 1.1 christos void worker_delete(struct worker* worker); 176 1.1 christos 177 1.1 christos /** 178 1.1 christos * Send a command to a worker. Uses blocking writes. 179 1.1 christos * @param worker: worker to send command to. 180 1.1 christos * @param cmd: command to send. 181 1.1 christos */ 182 1.1 christos void worker_send_cmd(struct worker* worker, enum worker_commands cmd); 183 1.1 christos 184 1.1 christos /** 185 1.1 christos * Init worker stats - includes server_stats_init, outside network and mesh. 186 1.1 christos * @param worker: the worker to init 187 1.1 christos */ 188 1.1 christos void worker_stats_clear(struct worker* worker); 189 1.1 christos 190 1.1 christos #endif /* DAEMON_WORKER_H */ 191