1 /*
2  * util/shm_side/shm_main.c - SHM for statistics transport
3  *
4  * Copyright (c) 2017, 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 contains functions for the SHM implementation.
40  */
41 
42 #include "config.h"
43 #include <ctype.h>
44 #include <stdarg.h>
45 #ifdef HAVE_SYS_IPC_H
46 #include <sys/ipc.h>
47 #endif
48 #ifdef HAVE_SYS_SHM_H
49 #include <sys/shm.h>
50 #endif
51 #include <sys/time.h>
52 #include <errno.h>
53 #include "shm_main.h"
54 #include "daemon/daemon.h"
55 #include "daemon/worker.h"
56 #include "daemon/stats.h"
57 #include "services/mesh.h"
58 #include "services/cache/rrset.h"
59 #include "services/cache/infra.h"
60 #include "validator/validator.h"
61 #include "util/config_file.h"
62 #include "util/fptr_wlist.h"
63 #include "util/log.h"
64 
65 #ifdef HAVE_SHMGET
66 /** subtract timers and the values do not overflow or become negative */
67 static void
68 stat_timeval_subtract(long long *d_sec, long long *d_usec, const struct timeval* end,
69 	const struct timeval* start)
70 {
71 #ifndef S_SPLINT_S
72 	time_t end_usec = end->tv_usec;
73 	*d_sec = end->tv_sec - start->tv_sec;
74 	if(end_usec < start->tv_usec) {
75 		end_usec += 1000000;
76 		(*d_sec)--;
77 	}
78 	*d_usec = end_usec - start->tv_usec;
79 #endif
80 }
81 #endif /* HAVE_SHMGET */
82 
83 int shm_main_init(struct daemon* daemon)
84 {
85 #ifdef HAVE_SHMGET
86 	struct ub_shm_stat_info *shm_stat;
87 	size_t shm_size;
88 
89 	/* sanitize */
90 	if(!daemon)
91 		return 0;
92 	if(!daemon->cfg->shm_enable)
93 		return 1;
94 	if(daemon->cfg->stat_interval == 0)
95 		log_warn("shm-enable is yes but statistics-interval is 0");
96 
97 	/* Statistics to maintain the number of thread + total */
98 	shm_size = (sizeof(struct ub_stats_info) * (daemon->num + 1));
99 
100 	/* Allocation of needed memory */
101 	daemon->shm_info = (struct shm_main_info*)calloc(1, shm_size);
102 
103 	/* Sanitize */
104 	if(!daemon->shm_info) {
105 		log_err("shm fail: malloc failure");
106 		return 0;
107 	}
108 
109 	daemon->shm_info->key = daemon->cfg->shm_key;
110 
111 	/* Check for previous create SHM */
112 	daemon->shm_info->id_ctl = shmget(daemon->shm_info->key, sizeof(int), SHM_R);
113 	daemon->shm_info->id_arr = shmget(daemon->shm_info->key + 1, sizeof(int), SHM_R);
114 
115 	/* Destroy previous SHM */
116 	if (daemon->shm_info->id_ctl >= 0)
117 		shmctl(daemon->shm_info->id_ctl, IPC_RMID, NULL);
118 
119 	/* Destroy previous SHM */
120 	if (daemon->shm_info->id_arr >= 0)
121 		shmctl(daemon->shm_info->id_arr, IPC_RMID, NULL);
122 
123 	/* SHM: Create the segment */
124 	daemon->shm_info->id_ctl = shmget(daemon->shm_info->key, sizeof(struct ub_shm_stat_info), IPC_CREAT | 0644);
125 
126 	if (daemon->shm_info->id_ctl < 0)
127 	{
128 		log_err("SHM failed(id_ctl) cannot shmget(key %d) %s",
129 			daemon->shm_info->key, strerror(errno));
130 
131 		/* Just release memory unused */
132 		free(daemon->shm_info);
133 
134 		return 0;
135 	}
136 
137 	daemon->shm_info->id_arr = shmget(daemon->shm_info->key + 1, shm_size, IPC_CREAT | 0644);
138 
139 	if (daemon->shm_info->id_arr < 0)
140 	{
141 		log_err("SHM failed(id_arr) cannot shmget(key %d + 1) %s",
142 			daemon->shm_info->key, strerror(errno));
143 
144 		/* Just release memory unused */
145 		free(daemon->shm_info);
146 
147 		return 0;
148 	}
149 
150 	/* SHM: attach the segment  */
151 	daemon->shm_info->ptr_ctl = (struct ub_shm_stat_info*)
152 		shmat(daemon->shm_info->id_ctl, NULL, 0);
153 	if(daemon->shm_info->ptr_ctl == (void *) -1) {
154 		log_err("SHM failed(ctl) cannot shmat(%d) %s",
155 			daemon->shm_info->id_ctl, strerror(errno));
156 
157 		/* Just release memory unused */
158 		free(daemon->shm_info);
159 
160 		return 0;
161 	}
162 
163 	daemon->shm_info->ptr_arr = (struct ub_stats_info*)
164 		shmat(daemon->shm_info->id_arr, NULL, 0);
165 
166 	if (daemon->shm_info->ptr_arr == (void *) -1)
167 	{
168 		log_err("SHM failed(arr) cannot shmat(%d) %s",
169 			daemon->shm_info->id_arr, strerror(errno));
170 
171 		/* Just release memory unused */
172 		free(daemon->shm_info);
173 
174 		return 0;
175 	}
176 
177 	/* Zero fill SHM to stand clean while is not filled by other events */
178 	memset(daemon->shm_info->ptr_ctl, 0, sizeof(struct ub_shm_stat_info));
179 	memset(daemon->shm_info->ptr_arr, 0, shm_size);
180 
181 	shm_stat = daemon->shm_info->ptr_ctl;
182 	shm_stat->num_threads = daemon->num;
183 
184 #else
185 	(void)daemon;
186 #endif /* HAVE_SHMGET */
187 	return 1;
188 }
189 
190 void shm_main_shutdown(struct daemon* daemon)
191 {
192 #ifdef HAVE_SHMGET
193 	/* web are OK, just disabled */
194 	if(!daemon->cfg->shm_enable)
195 		return;
196 
197 	verbose(VERB_DETAIL, "SHM shutdown - KEY [%d] - ID CTL [%d] ARR [%d] - PTR CTL [%p] ARR [%p]",
198 		daemon->shm_info->key, daemon->shm_info->id_ctl, daemon->shm_info->id_arr, daemon->shm_info->ptr_ctl, daemon->shm_info->ptr_arr);
199 
200 	/* Destroy previous SHM */
201 	if (daemon->shm_info->id_ctl >= 0)
202 		shmctl(daemon->shm_info->id_ctl, IPC_RMID, NULL);
203 
204 	if (daemon->shm_info->id_arr >= 0)
205 		shmctl(daemon->shm_info->id_arr, IPC_RMID, NULL);
206 
207 	if (daemon->shm_info->ptr_ctl)
208 		shmdt(daemon->shm_info->ptr_ctl);
209 
210 	if (daemon->shm_info->ptr_arr)
211 		shmdt(daemon->shm_info->ptr_arr);
212 
213 #else
214 	(void)daemon;
215 #endif /* HAVE_SHMGET */
216 }
217 
218 void shm_main_run(struct worker *worker)
219 {
220 #ifdef HAVE_SHMGET
221 	struct ub_shm_stat_info *shm_stat;
222 	struct ub_stats_info *stat_total;
223 	struct ub_stats_info *stat_info;
224 	int offset;
225 
226 #ifndef S_SPLINT_S
227 	verbose(VERB_DETAIL, "SHM run - worker [%d] - daemon [%p] - timenow(%u) - timeboot(%u)",
228 		worker->thread_num, worker->daemon, (unsigned)worker->env.now_tv->tv_sec, (unsigned)worker->daemon->time_boot.tv_sec);
229 #endif
230 
231 	offset = worker->thread_num + 1;
232 	stat_total = worker->daemon->shm_info->ptr_arr;
233 	stat_info = worker->daemon->shm_info->ptr_arr + offset;
234 
235 	/* Copy data to the current position */
236 	server_stats_compile(worker, stat_info, 0);
237 
238 	/* First thread, zero fill total, and copy general info */
239 	if (worker->thread_num == 0) {
240 
241 		/* Copy data to the current position */
242 		memset(stat_total, 0, sizeof(struct ub_stats_info));
243 
244 		/* Point to data into SHM */
245 #ifndef S_SPLINT_S
246 		shm_stat = worker->daemon->shm_info->ptr_ctl;
247 		shm_stat->time.now_sec = (long long)worker->env.now_tv->tv_sec;
248 		shm_stat->time.now_usec = (long long)worker->env.now_tv->tv_usec;
249 #endif
250 
251 		stat_timeval_subtract(&shm_stat->time.up_sec, &shm_stat->time.up_usec, worker->env.now_tv, &worker->daemon->time_boot);
252 		stat_timeval_subtract(&shm_stat->time.elapsed_sec, &shm_stat->time.elapsed_usec, worker->env.now_tv, &worker->daemon->time_last_stat);
253 
254 		shm_stat->mem.msg = (long long)slabhash_get_mem(worker->env.msg_cache);
255 		shm_stat->mem.rrset = (long long)slabhash_get_mem(&worker->env.rrset_cache->table);
256 		shm_stat->mem.dnscrypt_shared_secret = 0;
257 #ifdef USE_DNSCRYPT
258 		if(worker->daemon->dnscenv) {
259 			shm_stat->mem.dnscrypt_shared_secret = (long long)slabhash_get_mem(
260 				worker->daemon->dnscenv->shared_secrets_cache);
261 			shm_stat->mem.dnscrypt_nonce = (long long)slabhash_get_mem(
262 				worker->daemon->dnscenv->nonces_cache);
263 		}
264 #endif
265 		shm_stat->mem.val = (long long)mod_get_mem(&worker->env,
266 			"validator");
267 		shm_stat->mem.iter = (long long)mod_get_mem(&worker->env,
268 			"iterator");
269 		shm_stat->mem.respip = (long long)mod_get_mem(&worker->env,
270 			"respip");
271 
272 		/* subnet mem value is available in shm, also when not enabled,
273 		 * to make the struct easier to memmap by other applications,
274 		 * independent of the configuration of unbound */
275 		shm_stat->mem.subnet = 0;
276 #ifdef CLIENT_SUBNET
277 		shm_stat->mem.subnet = (long long)mod_get_mem(&worker->env,
278 			"subnet");
279 #endif
280 		/* ipsecmod mem value is available in shm, also when not enabled,
281 		 * to make the struct easier to memmap by other applications,
282 		 * independent of the configuration of unbound */
283 		shm_stat->mem.ipsecmod = 0;
284 #ifdef USE_IPSECMOD
285 		shm_stat->mem.ipsecmod = (long long)mod_get_mem(&worker->env,
286 			"ipsecmod");
287 #endif
288 #ifdef WITH_DYNLIBMODULE
289 		shm_stat->mem.dynlib = (long long)mod_get_mem(&worker->env,
290 			"dynlib");
291 #endif
292 	}
293 
294 	server_stats_add(stat_total, stat_info);
295 
296 	/* print the thread statistics */
297 	stat_total->mesh_time_median /= (double)worker->daemon->num;
298 
299 #else
300 	(void)worker;
301 #endif /* HAVE_SHMGET */
302 }
303