xref: /openbsd/usr.sbin/unbound/daemon/worker.h (revision 91f110e0)
1 /*
2  * daemon/worker.h - worker that handles a pending list of requests.
3  *
4  * Copyright (c) 2007, NLnet Labs. All rights reserved.
5  *
6  * This software is open source.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * Redistributions of source code must retain the above copyright notice,
13  * this list of conditions and the following disclaimer.
14  *
15  * Redistributions in binary form must reproduce the above copyright notice,
16  * this list of conditions and the following disclaimer in the documentation
17  * and/or other materials provided with the distribution.
18  *
19  * Neither the name of the NLNET LABS nor the names of its contributors may
20  * be used to endorse or promote products derived from this software without
21  * specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
26  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
27  * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
29  * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
30  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
31  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
32  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
33  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34  */
35 
36 /**
37  * \file
38  *
39  * This file describes the worker structure that holds a list of
40  * pending requests and handles them.
41  */
42 
43 #ifndef DAEMON_WORKER_H
44 #define DAEMON_WORKER_H
45 
46 #include "util/netevent.h"
47 #include "util/locks.h"
48 #include "util/alloc.h"
49 #include "util/data/msgreply.h"
50 #include "util/data/msgparse.h"
51 #include "daemon/stats.h"
52 #include "util/module.h"
53 struct listen_dnsport;
54 struct outside_network;
55 struct config_file;
56 struct daemon;
57 struct listen_port;
58 struct ub_randstate;
59 struct regional;
60 struct tube;
61 struct daemon_remote;
62 
63 /** worker commands */
64 enum worker_commands {
65 	/** make the worker quit */
66 	worker_cmd_quit,
67 	/** obtain statistics */
68 	worker_cmd_stats,
69 	/** obtain statistics without statsclear */
70 	worker_cmd_stats_noreset,
71 	/** execute remote control command */
72 	worker_cmd_remote
73 };
74 
75 /**
76  * Structure holding working information for unbound.
77  * Holds globally visible information.
78  */
79 struct worker {
80 	/** the thread number (in daemon array). First in struct for debug. */
81 	int thread_num;
82 	/** global shared daemon structure */
83 	struct daemon* daemon;
84 	/** thread id */
85 	ub_thread_t thr_id;
86 	/** pipe, for commands for this worker */
87 	struct tube* cmd;
88 	/** the event base this worker works with */
89 	struct comm_base* base;
90 	/** the frontside listening interface where request events come in */
91 	struct listen_dnsport* front;
92 	/** the backside outside network interface to the auth servers */
93 	struct outside_network* back;
94 	/** ports to be used by this worker. */
95 	int* ports;
96 	/** number of ports for this worker */
97 	int numports;
98 	/** the signal handler */
99 	struct comm_signal* comsig;
100 	/** commpoint to listen to commands. */
101 	struct comm_point* cmd_com;
102 	/** timer for statistics */
103 	struct comm_timer* stat_timer;
104 
105 	/** random() table for this worker. */
106 	struct ub_randstate* rndstate;
107 	/** do we need to restart or quit (on signal) */
108 	int need_to_exit;
109 	/** allocation cache for this thread */
110 	struct alloc_cache alloc;
111 	/** per thread statistics */
112 	struct server_stats stats;
113 	/** thread scratch regional */
114 	struct regional* scratchpad;
115 
116 	/** module environment passed to modules, changed for this thread */
117 	struct module_env env;
118 };
119 
120 /**
121  * Create the worker structure. Bare bones version, zeroed struct,
122  * with backpointers only. Use worker_init on it later.
123  * @param daemon: the daemon that this worker thread is part of.
124  * @param id: the thread number from 0.. numthreads-1.
125  * @param ports: the ports it is allowed to use, array.
126  * @param n: the number of ports.
127  * @return: the new worker or NULL on alloc failure.
128  */
129 struct worker* worker_create(struct daemon* daemon, int id, int* ports, int n);
130 
131 /**
132  * Initialize worker.
133  * Allocates event base, listens to ports
134  * @param worker: worker to initialize, created with worker_create.
135  * @param cfg: configuration settings.
136  * @param ports: list of shared query ports.
137  * @param do_sigs: if true, worker installs signal handlers.
138  * @return: false on error.
139  */
140 int worker_init(struct worker* worker, struct config_file *cfg,
141 	struct listen_port* ports, int do_sigs);
142 
143 /**
144  * Make worker work.
145  */
146 void worker_work(struct worker* worker);
147 
148 /**
149  * Delete worker.
150  */
151 void worker_delete(struct worker* worker);
152 
153 /**
154  * Send a command to a worker. Uses blocking writes.
155  * @param worker: worker to send command to.
156  * @param cmd: command to send.
157  */
158 void worker_send_cmd(struct worker* worker, enum worker_commands cmd);
159 
160 /**
161  * Worker signal handler function. User argument is the worker itself.
162  * @param sig: signal number.
163  * @param arg: the worker (main worker) that handles signals.
164  */
165 void worker_sighandler(int sig, void* arg);
166 
167 /**
168  * Worker service routine to send serviced queries to authoritative servers.
169  * @param qname: query name. (host order)
170  * @param qnamelen: length in bytes of qname, including trailing 0.
171  * @param qtype: query type. (host order)
172  * @param qclass: query class. (host order)
173  * @param flags: host order flags word, with opcode and CD bit.
174  * @param dnssec: if set, EDNS record will have DO bit set.
175  * @param want_dnssec: signatures needed.
176  * @param addr: where to.
177  * @param addrlen: length of addr.
178  * @param zone: wireformat dname of the zone.
179  * @param zonelen: length of zone name.
180  * @param q: wich query state to reactivate upon return.
181  * @return: false on failure (memory or socket related). no query was
182  *      sent.
183  */
184 struct outbound_entry* worker_send_query(uint8_t* qname, size_t qnamelen,
185 	uint16_t qtype, uint16_t qclass, uint16_t flags, int dnssec,
186 	int want_dnssec, struct sockaddr_storage* addr, socklen_t addrlen,
187 	uint8_t* zone, size_t zonelen, struct module_qstate* q);
188 
189 /**
190  * process control messages from the main thread. Frees the control
191  * command message.
192  * @param tube: tube control message came on.
193  * @param msg: message contents.  Is freed.
194  * @param len: length of message.
195  * @param error: if error (NETEVENT_*) happened.
196  * @param arg: user argument
197  */
198 void worker_handle_control_cmd(struct tube* tube, uint8_t* msg, size_t len,
199 	int error, void* arg);
200 
201 /** handles callbacks from listening event interface */
202 int worker_handle_request(struct comm_point* c, void* arg, int error,
203 	struct comm_reply* repinfo);
204 
205 /** process incoming replies from the network */
206 int worker_handle_reply(struct comm_point* c, void* arg, int error,
207 	struct comm_reply* reply_info);
208 
209 /** process incoming serviced query replies from the network */
210 int worker_handle_service_reply(struct comm_point* c, void* arg, int error,
211 	struct comm_reply* reply_info);
212 
213 /** cleanup the cache to remove all rrset IDs from it, arg is worker */
214 void worker_alloc_cleanup(void* arg);
215 
216 /**
217  * Init worker stats - includes server_stats_init, outside network and mesh.
218  * @param worker: the worker to init
219  */
220 void worker_stats_clear(struct worker* worker);
221 
222 /** statistics timer callback handler */
223 void worker_stat_timer_cb(void* arg);
224 
225 /** probe timer callback handler */
226 void worker_probe_timer_cb(void* arg);
227 
228 /** start accept callback handler */
229 void worker_start_accept(void* arg);
230 
231 /** stop accept callback handler */
232 void worker_stop_accept(void* arg);
233 
234 #endif /* DAEMON_WORKER_H */
235