Home | History | Annotate | Line # | Download | only in daemon
worker.h revision 1.1
      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  christos 
     65  1.1  christos /** worker commands */
     66  1.1  christos enum worker_commands {
     67  1.1  christos 	/** make the worker quit */
     68  1.1  christos 	worker_cmd_quit,
     69  1.1  christos 	/** obtain statistics */
     70  1.1  christos 	worker_cmd_stats,
     71  1.1  christos 	/** obtain statistics without statsclear */
     72  1.1  christos 	worker_cmd_stats_noreset,
     73  1.1  christos 	/** execute remote control command */
     74  1.1  christos 	worker_cmd_remote
     75  1.1  christos };
     76  1.1  christos 
     77  1.1  christos /**
     78  1.1  christos  * Structure holding working information for unbound.
     79  1.1  christos  * Holds globally visible information.
     80  1.1  christos  */
     81  1.1  christos struct worker {
     82  1.1  christos 	/** the thread number (in daemon array). First in struct for debug. */
     83  1.1  christos 	int thread_num;
     84  1.1  christos 	/** global shared daemon structure */
     85  1.1  christos 	struct daemon* daemon;
     86  1.1  christos 	/** thread id */
     87  1.1  christos 	ub_thread_t thr_id;
     88  1.1  christos 	/** pipe, for commands for this worker */
     89  1.1  christos 	struct tube* cmd;
     90  1.1  christos 	/** the event base this worker works with */
     91  1.1  christos 	struct comm_base* base;
     92  1.1  christos 	/** the frontside listening interface where request events come in */
     93  1.1  christos 	struct listen_dnsport* front;
     94  1.1  christos 	/** the backside outside network interface to the auth servers */
     95  1.1  christos 	struct outside_network* back;
     96  1.1  christos 	/** ports to be used by this worker. */
     97  1.1  christos 	int* ports;
     98  1.1  christos 	/** number of ports for this worker */
     99  1.1  christos 	int numports;
    100  1.1  christos 	/** the signal handler */
    101  1.1  christos 	struct comm_signal* comsig;
    102  1.1  christos 	/** commpoint to listen to commands. */
    103  1.1  christos 	struct comm_point* cmd_com;
    104  1.1  christos 	/** timer for statistics */
    105  1.1  christos 	struct comm_timer* stat_timer;
    106  1.1  christos 	/** ratelimit for errors, time value */
    107  1.1  christos 	time_t err_limit_time;
    108  1.1  christos 	/** ratelimit for errors, packet count */
    109  1.1  christos 	unsigned int err_limit_count;
    110  1.1  christos 
    111  1.1  christos 	/** random() table for this worker. */
    112  1.1  christos 	struct ub_randstate* rndstate;
    113  1.1  christos 	/** do we need to restart or quit (on signal) */
    114  1.1  christos 	int need_to_exit;
    115  1.1  christos 	/** allocation cache for this thread */
    116  1.1  christos 	struct alloc_cache alloc;
    117  1.1  christos 	/** per thread statistics */
    118  1.1  christos 	struct server_stats stats;
    119  1.1  christos 	/** thread scratch regional */
    120  1.1  christos 	struct regional* scratchpad;
    121  1.1  christos 
    122  1.1  christos 	/** module environment passed to modules, changed for this thread */
    123  1.1  christos 	struct module_env env;
    124  1.1  christos 
    125  1.1  christos #ifdef USE_DNSTAP
    126  1.1  christos 	/** dnstap environment, changed for this thread */
    127  1.1  christos 	struct dt_env dtenv;
    128  1.1  christos #endif
    129  1.1  christos };
    130  1.1  christos 
    131  1.1  christos /**
    132  1.1  christos  * Create the worker structure. Bare bones version, zeroed struct,
    133  1.1  christos  * with backpointers only. Use worker_init on it later.
    134  1.1  christos  * @param daemon: the daemon that this worker thread is part of.
    135  1.1  christos  * @param id: the thread number from 0.. numthreads-1.
    136  1.1  christos  * @param ports: the ports it is allowed to use, array.
    137  1.1  christos  * @param n: the number of ports.
    138  1.1  christos  * @return: the new worker or NULL on alloc failure.
    139  1.1  christos  */
    140  1.1  christos struct worker* worker_create(struct daemon* daemon, int id, int* ports, int n);
    141  1.1  christos 
    142  1.1  christos /**
    143  1.1  christos  * Initialize worker.
    144  1.1  christos  * Allocates event base, listens to ports
    145  1.1  christos  * @param worker: worker to initialize, created with worker_create.
    146  1.1  christos  * @param cfg: configuration settings.
    147  1.1  christos  * @param ports: list of shared query ports.
    148  1.1  christos  * @param do_sigs: if true, worker installs signal handlers.
    149  1.1  christos  * @return: false on error.
    150  1.1  christos  */
    151  1.1  christos int worker_init(struct worker* worker, struct config_file *cfg,
    152  1.1  christos 	struct listen_port* ports, int do_sigs);
    153  1.1  christos 
    154  1.1  christos /**
    155  1.1  christos  * Make worker work.
    156  1.1  christos  */
    157  1.1  christos void worker_work(struct worker* worker);
    158  1.1  christos 
    159  1.1  christos /**
    160  1.1  christos  * Delete worker.
    161  1.1  christos  */
    162  1.1  christos void worker_delete(struct worker* worker);
    163  1.1  christos 
    164  1.1  christos /**
    165  1.1  christos  * Send a command to a worker. Uses blocking writes.
    166  1.1  christos  * @param worker: worker to send command to.
    167  1.1  christos  * @param cmd: command to send.
    168  1.1  christos  */
    169  1.1  christos void worker_send_cmd(struct worker* worker, enum worker_commands cmd);
    170  1.1  christos 
    171  1.1  christos /**
    172  1.1  christos  * Init worker stats - includes server_stats_init, outside network and mesh.
    173  1.1  christos  * @param worker: the worker to init
    174  1.1  christos  */
    175  1.1  christos void worker_stats_clear(struct worker* worker);
    176  1.1  christos 
    177  1.1  christos #endif /* DAEMON_WORKER_H */
    178