1933707f3Ssthen /* 2933707f3Ssthen * daemon/worker.h - worker that handles a pending list of requests. 3933707f3Ssthen * 4933707f3Ssthen * Copyright (c) 2007, NLnet Labs. All rights reserved. 5933707f3Ssthen * 6933707f3Ssthen * This software is open source. 7933707f3Ssthen * 8933707f3Ssthen * Redistribution and use in source and binary forms, with or without 9933707f3Ssthen * modification, are permitted provided that the following conditions 10933707f3Ssthen * are met: 11933707f3Ssthen * 12933707f3Ssthen * Redistributions of source code must retain the above copyright notice, 13933707f3Ssthen * this list of conditions and the following disclaimer. 14933707f3Ssthen * 15933707f3Ssthen * Redistributions in binary form must reproduce the above copyright notice, 16933707f3Ssthen * this list of conditions and the following disclaimer in the documentation 17933707f3Ssthen * and/or other materials provided with the distribution. 18933707f3Ssthen * 19933707f3Ssthen * Neither the name of the NLNET LABS nor the names of its contributors may 20933707f3Ssthen * be used to endorse or promote products derived from this software without 21933707f3Ssthen * specific prior written permission. 22933707f3Ssthen * 23933707f3Ssthen * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 240b68ff31Ssthen * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 250b68ff31Ssthen * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 260b68ff31Ssthen * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 270b68ff31Ssthen * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 280b68ff31Ssthen * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED 290b68ff31Ssthen * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 300b68ff31Ssthen * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 310b68ff31Ssthen * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 320b68ff31Ssthen * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 330b68ff31Ssthen * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 34933707f3Ssthen */ 35933707f3Ssthen 36933707f3Ssthen /** 37933707f3Ssthen * \file 38933707f3Ssthen * 39933707f3Ssthen * This file describes the worker structure that holds a list of 40933707f3Ssthen * pending requests and handles them. 41933707f3Ssthen */ 42933707f3Ssthen 43933707f3Ssthen #ifndef DAEMON_WORKER_H 44933707f3Ssthen #define DAEMON_WORKER_H 45933707f3Ssthen 46e10d3884Sbrad #include "libunbound/worker.h" 47933707f3Ssthen #include "util/netevent.h" 48933707f3Ssthen #include "util/locks.h" 49933707f3Ssthen #include "util/alloc.h" 50933707f3Ssthen #include "util/data/msgreply.h" 51933707f3Ssthen #include "util/data/msgparse.h" 52933707f3Ssthen #include "daemon/stats.h" 53933707f3Ssthen #include "util/module.h" 54e10d3884Sbrad #include "dnstap/dnstap.h" 55933707f3Ssthen struct listen_dnsport; 56933707f3Ssthen struct outside_network; 57933707f3Ssthen struct config_file; 58933707f3Ssthen struct daemon; 59933707f3Ssthen struct listen_port; 60933707f3Ssthen struct ub_randstate; 61933707f3Ssthen struct regional; 62933707f3Ssthen struct tube; 63933707f3Ssthen struct daemon_remote; 6477079be7Ssthen struct query_info; 65933707f3Ssthen 66933707f3Ssthen /** worker commands */ 67933707f3Ssthen enum worker_commands { 68933707f3Ssthen /** make the worker quit */ 69933707f3Ssthen worker_cmd_quit, 70933707f3Ssthen /** obtain statistics */ 71933707f3Ssthen worker_cmd_stats, 72933707f3Ssthen /** obtain statistics without statsclear */ 73933707f3Ssthen worker_cmd_stats_noreset, 74933707f3Ssthen /** execute remote control command */ 75933707f3Ssthen worker_cmd_remote 76933707f3Ssthen }; 77933707f3Ssthen 78933707f3Ssthen /** 79933707f3Ssthen * Structure holding working information for unbound. 80933707f3Ssthen * Holds globally visible information. 81933707f3Ssthen */ 82933707f3Ssthen struct worker { 83933707f3Ssthen /** the thread number (in daemon array). First in struct for debug. */ 84933707f3Ssthen int thread_num; 85933707f3Ssthen /** global shared daemon structure */ 86933707f3Ssthen struct daemon* daemon; 87933707f3Ssthen /** thread id */ 8877079be7Ssthen ub_thread_type thr_id; 8945872187Ssthen #ifdef HAVE_GETTID 9045872187Ssthen /** thread tid, the LWP id. */ 9145872187Ssthen pid_t thread_tid; 9245872187Ssthen #endif 93933707f3Ssthen /** pipe, for commands for this worker */ 94933707f3Ssthen struct tube* cmd; 95933707f3Ssthen /** the event base this worker works with */ 96933707f3Ssthen struct comm_base* base; 97933707f3Ssthen /** the frontside listening interface where request events come in */ 98933707f3Ssthen struct listen_dnsport* front; 99933707f3Ssthen /** the backside outside network interface to the auth servers */ 100933707f3Ssthen struct outside_network* back; 101933707f3Ssthen /** ports to be used by this worker. */ 102933707f3Ssthen int* ports; 103933707f3Ssthen /** number of ports for this worker */ 104933707f3Ssthen int numports; 105933707f3Ssthen /** the signal handler */ 106933707f3Ssthen struct comm_signal* comsig; 107933707f3Ssthen /** commpoint to listen to commands. */ 108933707f3Ssthen struct comm_point* cmd_com; 109933707f3Ssthen /** timer for statistics */ 110933707f3Ssthen struct comm_timer* stat_timer; 111a58bff56Ssthen /** ratelimit for errors, time value */ 112a58bff56Ssthen time_t err_limit_time; 113a58bff56Ssthen /** ratelimit for errors, packet count */ 114a58bff56Ssthen unsigned int err_limit_count; 115933707f3Ssthen 116933707f3Ssthen /** random() table for this worker. */ 117933707f3Ssthen struct ub_randstate* rndstate; 118933707f3Ssthen /** do we need to restart or quit (on signal) */ 119933707f3Ssthen int need_to_exit; 120933707f3Ssthen /** allocation cache for this thread */ 121*8b7325afSsthen struct alloc_cache *alloc; 122933707f3Ssthen /** per thread statistics */ 1232be9e038Ssthen struct ub_server_stats stats; 124933707f3Ssthen /** thread scratch regional */ 125933707f3Ssthen struct regional* scratchpad; 126933707f3Ssthen 127933707f3Ssthen /** module environment passed to modules, changed for this thread */ 128933707f3Ssthen struct module_env env; 129e10d3884Sbrad 130e10d3884Sbrad #ifdef USE_DNSTAP 131e10d3884Sbrad /** dnstap environment, changed for this thread */ 132e10d3884Sbrad struct dt_env dtenv; 133e10d3884Sbrad #endif 134*8b7325afSsthen /** reuse existing cache on reload if other conditions allow it. */ 135*8b7325afSsthen int reuse_cache; 136933707f3Ssthen }; 137933707f3Ssthen 138933707f3Ssthen /** 139933707f3Ssthen * Create the worker structure. Bare bones version, zeroed struct, 140933707f3Ssthen * with backpointers only. Use worker_init on it later. 141933707f3Ssthen * @param daemon: the daemon that this worker thread is part of. 142933707f3Ssthen * @param id: the thread number from 0.. numthreads-1. 143933707f3Ssthen * @param ports: the ports it is allowed to use, array. 144933707f3Ssthen * @param n: the number of ports. 145933707f3Ssthen * @return: the new worker or NULL on alloc failure. 146933707f3Ssthen */ 147933707f3Ssthen struct worker* worker_create(struct daemon* daemon, int id, int* ports, int n); 148933707f3Ssthen 149933707f3Ssthen /** 150933707f3Ssthen * Initialize worker. 151933707f3Ssthen * Allocates event base, listens to ports 152933707f3Ssthen * @param worker: worker to initialize, created with worker_create. 153933707f3Ssthen * @param cfg: configuration settings. 154933707f3Ssthen * @param ports: list of shared query ports. 155933707f3Ssthen * @param do_sigs: if true, worker installs signal handlers. 156933707f3Ssthen * @return: false on error. 157933707f3Ssthen */ 158933707f3Ssthen int worker_init(struct worker* worker, struct config_file *cfg, 159933707f3Ssthen struct listen_port* ports, int do_sigs); 160933707f3Ssthen 161933707f3Ssthen /** 162933707f3Ssthen * Make worker work. 163933707f3Ssthen */ 164933707f3Ssthen void worker_work(struct worker* worker); 165933707f3Ssthen 166933707f3Ssthen /** 167933707f3Ssthen * Delete worker. 168933707f3Ssthen */ 169933707f3Ssthen void worker_delete(struct worker* worker); 170933707f3Ssthen 171933707f3Ssthen /** 172933707f3Ssthen * Send a command to a worker. Uses blocking writes. 173933707f3Ssthen * @param worker: worker to send command to. 174933707f3Ssthen * @param cmd: command to send. 175933707f3Ssthen */ 176933707f3Ssthen void worker_send_cmd(struct worker* worker, enum worker_commands cmd); 177933707f3Ssthen 178933707f3Ssthen /** 179933707f3Ssthen * Init worker stats - includes server_stats_init, outside network and mesh. 180933707f3Ssthen * @param worker: the worker to init 181933707f3Ssthen */ 182933707f3Ssthen void worker_stats_clear(struct worker* worker); 183933707f3Ssthen 184933707f3Ssthen #endif /* DAEMON_WORKER_H */ 185