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 daemon->shm_info = NULL; 134 135 return 0; 136 } 137 138 daemon->shm_info->id_arr = shmget(daemon->shm_info->key + 1, shm_size, IPC_CREAT | 0644); 139 140 if (daemon->shm_info->id_arr < 0) 141 { 142 log_err("SHM failed(id_arr) cannot shmget(key %d + 1) %s", 143 daemon->shm_info->key, strerror(errno)); 144 145 /* Just release memory unused */ 146 free(daemon->shm_info); 147 daemon->shm_info = NULL; 148 149 return 0; 150 } 151 152 /* SHM: attach the segment */ 153 daemon->shm_info->ptr_ctl = (struct ub_shm_stat_info*) 154 shmat(daemon->shm_info->id_ctl, NULL, 0); 155 if(daemon->shm_info->ptr_ctl == (void *) -1) { 156 log_err("SHM failed(ctl) cannot shmat(%d) %s", 157 daemon->shm_info->id_ctl, strerror(errno)); 158 159 /* Just release memory unused */ 160 free(daemon->shm_info); 161 daemon->shm_info = NULL; 162 163 return 0; 164 } 165 166 daemon->shm_info->ptr_arr = (struct ub_stats_info*) 167 shmat(daemon->shm_info->id_arr, NULL, 0); 168 169 if (daemon->shm_info->ptr_arr == (void *) -1) 170 { 171 log_err("SHM failed(arr) cannot shmat(%d) %s", 172 daemon->shm_info->id_arr, strerror(errno)); 173 174 /* Just release memory unused */ 175 free(daemon->shm_info); 176 daemon->shm_info = NULL; 177 178 return 0; 179 } 180 181 /* Zero fill SHM to stand clean while is not filled by other events */ 182 memset(daemon->shm_info->ptr_ctl, 0, sizeof(struct ub_shm_stat_info)); 183 memset(daemon->shm_info->ptr_arr, 0, shm_size); 184 185 shm_stat = daemon->shm_info->ptr_ctl; 186 shm_stat->num_threads = daemon->num; 187 188 #else 189 (void)daemon; 190 #endif /* HAVE_SHMGET */ 191 return 1; 192 } 193 194 void shm_main_shutdown(struct daemon* daemon) 195 { 196 #ifdef HAVE_SHMGET 197 /* web are OK, just disabled */ 198 if(!daemon->cfg->shm_enable) 199 return; 200 201 verbose(VERB_DETAIL, "SHM shutdown - KEY [%d] - ID CTL [%d] ARR [%d] - PTR CTL [%p] ARR [%p]", 202 daemon->shm_info->key, daemon->shm_info->id_ctl, daemon->shm_info->id_arr, daemon->shm_info->ptr_ctl, daemon->shm_info->ptr_arr); 203 204 /* Destroy previous SHM */ 205 if (daemon->shm_info->id_ctl >= 0) 206 shmctl(daemon->shm_info->id_ctl, IPC_RMID, NULL); 207 208 if (daemon->shm_info->id_arr >= 0) 209 shmctl(daemon->shm_info->id_arr, IPC_RMID, NULL); 210 211 if (daemon->shm_info->ptr_ctl) 212 shmdt(daemon->shm_info->ptr_ctl); 213 214 if (daemon->shm_info->ptr_arr) 215 shmdt(daemon->shm_info->ptr_arr); 216 217 free(daemon->shm_info); 218 daemon->shm_info = NULL; 219 #else 220 (void)daemon; 221 #endif /* HAVE_SHMGET */ 222 } 223 224 void shm_main_run(struct worker *worker) 225 { 226 #ifdef HAVE_SHMGET 227 struct ub_shm_stat_info *shm_stat; 228 struct ub_stats_info *stat_total; 229 struct ub_stats_info *stat_info; 230 int offset; 231 232 #ifndef S_SPLINT_S 233 verbose(VERB_DETAIL, "SHM run - worker [%d] - daemon [%p] - timenow(%u) - timeboot(%u)", 234 worker->thread_num, worker->daemon, (unsigned)worker->env.now_tv->tv_sec, (unsigned)worker->daemon->time_boot.tv_sec); 235 #endif 236 237 offset = worker->thread_num + 1; 238 stat_total = worker->daemon->shm_info->ptr_arr; 239 stat_info = worker->daemon->shm_info->ptr_arr + offset; 240 241 /* Copy data to the current position */ 242 server_stats_compile(worker, stat_info, 0); 243 244 /* First thread, zero fill total, and copy general info */ 245 if (worker->thread_num == 0) { 246 247 /* Copy data to the current position */ 248 memset(stat_total, 0, sizeof(struct ub_stats_info)); 249 250 /* Point to data into SHM */ 251 #ifndef S_SPLINT_S 252 shm_stat = worker->daemon->shm_info->ptr_ctl; 253 shm_stat->time.now_sec = (long long)worker->env.now_tv->tv_sec; 254 shm_stat->time.now_usec = (long long)worker->env.now_tv->tv_usec; 255 #endif 256 257 stat_timeval_subtract(&shm_stat->time.up_sec, &shm_stat->time.up_usec, worker->env.now_tv, &worker->daemon->time_boot); 258 stat_timeval_subtract(&shm_stat->time.elapsed_sec, &shm_stat->time.elapsed_usec, worker->env.now_tv, &worker->daemon->time_last_stat); 259 260 shm_stat->mem.msg = (long long)slabhash_get_mem(worker->env.msg_cache); 261 shm_stat->mem.rrset = (long long)slabhash_get_mem(&worker->env.rrset_cache->table); 262 shm_stat->mem.dnscrypt_shared_secret = 0; 263 #ifdef USE_DNSCRYPT 264 if(worker->daemon->dnscenv) { 265 shm_stat->mem.dnscrypt_shared_secret = (long long)slabhash_get_mem( 266 worker->daemon->dnscenv->shared_secrets_cache); 267 shm_stat->mem.dnscrypt_nonce = (long long)slabhash_get_mem( 268 worker->daemon->dnscenv->nonces_cache); 269 } 270 #endif 271 shm_stat->mem.val = (long long)mod_get_mem(&worker->env, 272 "validator"); 273 shm_stat->mem.iter = (long long)mod_get_mem(&worker->env, 274 "iterator"); 275 shm_stat->mem.respip = (long long)mod_get_mem(&worker->env, 276 "respip"); 277 278 /* subnet mem value is available in shm, also when not enabled, 279 * to make the struct easier to memmap by other applications, 280 * independent of the configuration of unbound */ 281 shm_stat->mem.subnet = 0; 282 #ifdef CLIENT_SUBNET 283 shm_stat->mem.subnet = (long long)mod_get_mem(&worker->env, 284 "subnetcache"); 285 #endif 286 /* ipsecmod mem value is available in shm, also when not enabled, 287 * to make the struct easier to memmap by other applications, 288 * independent of the configuration of unbound */ 289 shm_stat->mem.ipsecmod = 0; 290 #ifdef USE_IPSECMOD 291 shm_stat->mem.ipsecmod = (long long)mod_get_mem(&worker->env, 292 "ipsecmod"); 293 #endif 294 #ifdef WITH_DYNLIBMODULE 295 shm_stat->mem.dynlib = (long long)mod_get_mem(&worker->env, 296 "dynlib"); 297 #endif 298 } 299 300 server_stats_add(stat_total, stat_info); 301 302 /* print the thread statistics */ 303 stat_total->mesh_time_median /= (double)worker->daemon->num; 304 305 #else 306 (void)worker; 307 #endif /* HAVE_SHMGET */ 308 } 309