10cd9f4ecSchristos /*
20cd9f4ecSchristos * util/shm_side/shm_main.c - SHM for statistics transport
30cd9f4ecSchristos *
40cd9f4ecSchristos * Copyright (c) 2017, NLnet Labs. All rights reserved.
50cd9f4ecSchristos *
60cd9f4ecSchristos * This software is open source.
70cd9f4ecSchristos *
80cd9f4ecSchristos * Redistribution and use in source and binary forms, with or without
90cd9f4ecSchristos * modification, are permitted provided that the following conditions
100cd9f4ecSchristos * are met:
110cd9f4ecSchristos *
120cd9f4ecSchristos * Redistributions of source code must retain the above copyright notice,
130cd9f4ecSchristos * this list of conditions and the following disclaimer.
140cd9f4ecSchristos *
150cd9f4ecSchristos * Redistributions in binary form must reproduce the above copyright notice,
160cd9f4ecSchristos * this list of conditions and the following disclaimer in the documentation
170cd9f4ecSchristos * and/or other materials provided with the distribution.
180cd9f4ecSchristos *
190cd9f4ecSchristos * Neither the name of the NLNET LABS nor the names of its contributors may
200cd9f4ecSchristos * be used to endorse or promote products derived from this software without
210cd9f4ecSchristos * specific prior written permission.
220cd9f4ecSchristos *
230cd9f4ecSchristos * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
240cd9f4ecSchristos * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
250cd9f4ecSchristos * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
260cd9f4ecSchristos * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
270cd9f4ecSchristos * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
280cd9f4ecSchristos * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
290cd9f4ecSchristos * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
300cd9f4ecSchristos * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
310cd9f4ecSchristos * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
320cd9f4ecSchristos * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
330cd9f4ecSchristos * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
340cd9f4ecSchristos */
350cd9f4ecSchristos
360cd9f4ecSchristos /**
370cd9f4ecSchristos * \file
380cd9f4ecSchristos *
390cd9f4ecSchristos * This file contains functions for the SHM implementation.
400cd9f4ecSchristos */
410cd9f4ecSchristos
420cd9f4ecSchristos #include "config.h"
430cd9f4ecSchristos #include <ctype.h>
440cd9f4ecSchristos #include <stdarg.h>
450cd9f4ecSchristos #ifdef HAVE_SYS_IPC_H
460cd9f4ecSchristos #include <sys/ipc.h>
470cd9f4ecSchristos #endif
480cd9f4ecSchristos #ifdef HAVE_SYS_SHM_H
490cd9f4ecSchristos #include <sys/shm.h>
500cd9f4ecSchristos #endif
510cd9f4ecSchristos #include <sys/time.h>
520cd9f4ecSchristos #include <errno.h>
530cd9f4ecSchristos #include "shm_main.h"
540cd9f4ecSchristos #include "daemon/daemon.h"
550cd9f4ecSchristos #include "daemon/worker.h"
560cd9f4ecSchristos #include "daemon/stats.h"
570cd9f4ecSchristos #include "services/mesh.h"
580cd9f4ecSchristos #include "services/cache/rrset.h"
590cd9f4ecSchristos #include "services/cache/infra.h"
600cd9f4ecSchristos #include "validator/validator.h"
610cd9f4ecSchristos #include "util/config_file.h"
620cd9f4ecSchristos #include "util/fptr_wlist.h"
630cd9f4ecSchristos #include "util/log.h"
640cd9f4ecSchristos
650cd9f4ecSchristos #ifdef HAVE_SHMGET
660cd9f4ecSchristos /** subtract timers and the values do not overflow or become negative */
670cd9f4ecSchristos static void
stat_timeval_subtract(long long * d_sec,long long * d_usec,const struct timeval * end,const struct timeval * start)680cd9f4ecSchristos stat_timeval_subtract(long long *d_sec, long long *d_usec, const struct timeval* end,
690cd9f4ecSchristos const struct timeval* start)
700cd9f4ecSchristos {
710cd9f4ecSchristos #ifndef S_SPLINT_S
720cd9f4ecSchristos time_t end_usec = end->tv_usec;
730cd9f4ecSchristos *d_sec = end->tv_sec - start->tv_sec;
740cd9f4ecSchristos if(end_usec < start->tv_usec) {
750cd9f4ecSchristos end_usec += 1000000;
760cd9f4ecSchristos (*d_sec)--;
770cd9f4ecSchristos }
780cd9f4ecSchristos *d_usec = end_usec - start->tv_usec;
790cd9f4ecSchristos #endif
800cd9f4ecSchristos }
810cd9f4ecSchristos #endif /* HAVE_SHMGET */
820cd9f4ecSchristos
shm_main_init(struct daemon * daemon)830cd9f4ecSchristos int shm_main_init(struct daemon* daemon)
840cd9f4ecSchristos {
850cd9f4ecSchristos #ifdef HAVE_SHMGET
860cd9f4ecSchristos struct ub_shm_stat_info *shm_stat;
870cd9f4ecSchristos size_t shm_size;
880cd9f4ecSchristos
890cd9f4ecSchristos /* sanitize */
900cd9f4ecSchristos if(!daemon)
910cd9f4ecSchristos return 0;
920cd9f4ecSchristos if(!daemon->cfg->shm_enable)
930cd9f4ecSchristos return 1;
940cd9f4ecSchristos if(daemon->cfg->stat_interval == 0)
950cd9f4ecSchristos log_warn("shm-enable is yes but statistics-interval is 0");
960cd9f4ecSchristos
970cd9f4ecSchristos /* Statistics to maintain the number of thread + total */
980cd9f4ecSchristos shm_size = (sizeof(struct ub_stats_info) * (daemon->num + 1));
990cd9f4ecSchristos
1000cd9f4ecSchristos /* Allocation of needed memory */
1010cd9f4ecSchristos daemon->shm_info = (struct shm_main_info*)calloc(1, shm_size);
1020cd9f4ecSchristos
1030cd9f4ecSchristos /* Sanitize */
1040cd9f4ecSchristos if(!daemon->shm_info) {
1050cd9f4ecSchristos log_err("shm fail: malloc failure");
1060cd9f4ecSchristos return 0;
1070cd9f4ecSchristos }
1080cd9f4ecSchristos
1090cd9f4ecSchristos daemon->shm_info->key = daemon->cfg->shm_key;
1100cd9f4ecSchristos
1110cd9f4ecSchristos /* Check for previous create SHM */
1120cd9f4ecSchristos daemon->shm_info->id_ctl = shmget(daemon->shm_info->key, sizeof(int), SHM_R);
1130cd9f4ecSchristos daemon->shm_info->id_arr = shmget(daemon->shm_info->key + 1, sizeof(int), SHM_R);
1140cd9f4ecSchristos
1150cd9f4ecSchristos /* Destroy previous SHM */
1160cd9f4ecSchristos if (daemon->shm_info->id_ctl >= 0)
1170cd9f4ecSchristos shmctl(daemon->shm_info->id_ctl, IPC_RMID, NULL);
1180cd9f4ecSchristos
1190cd9f4ecSchristos /* Destroy previous SHM */
1200cd9f4ecSchristos if (daemon->shm_info->id_arr >= 0)
1210cd9f4ecSchristos shmctl(daemon->shm_info->id_arr, IPC_RMID, NULL);
1220cd9f4ecSchristos
1230cd9f4ecSchristos /* SHM: Create the segment */
12401049ae6Schristos daemon->shm_info->id_ctl = shmget(daemon->shm_info->key, sizeof(struct ub_shm_stat_info), IPC_CREAT | 0644);
1250cd9f4ecSchristos
1260cd9f4ecSchristos if (daemon->shm_info->id_ctl < 0)
1270cd9f4ecSchristos {
1280cd9f4ecSchristos log_err("SHM failed(id_ctl) cannot shmget(key %d) %s",
1290cd9f4ecSchristos daemon->shm_info->key, strerror(errno));
1300cd9f4ecSchristos
1310cd9f4ecSchristos /* Just release memory unused */
1320cd9f4ecSchristos free(daemon->shm_info);
133*7a540f2bSchristos daemon->shm_info = NULL;
1340cd9f4ecSchristos
1350cd9f4ecSchristos return 0;
1360cd9f4ecSchristos }
1370cd9f4ecSchristos
13801049ae6Schristos daemon->shm_info->id_arr = shmget(daemon->shm_info->key + 1, shm_size, IPC_CREAT | 0644);
1390cd9f4ecSchristos
1400cd9f4ecSchristos if (daemon->shm_info->id_arr < 0)
1410cd9f4ecSchristos {
1420cd9f4ecSchristos log_err("SHM failed(id_arr) cannot shmget(key %d + 1) %s",
1430cd9f4ecSchristos daemon->shm_info->key, strerror(errno));
1440cd9f4ecSchristos
1450cd9f4ecSchristos /* Just release memory unused */
1460cd9f4ecSchristos free(daemon->shm_info);
147*7a540f2bSchristos daemon->shm_info = NULL;
1480cd9f4ecSchristos
1490cd9f4ecSchristos return 0;
1500cd9f4ecSchristos }
1510cd9f4ecSchristos
1520cd9f4ecSchristos /* SHM: attach the segment */
1530cd9f4ecSchristos daemon->shm_info->ptr_ctl = (struct ub_shm_stat_info*)
1540cd9f4ecSchristos shmat(daemon->shm_info->id_ctl, NULL, 0);
1550cd9f4ecSchristos if(daemon->shm_info->ptr_ctl == (void *) -1) {
1560cd9f4ecSchristos log_err("SHM failed(ctl) cannot shmat(%d) %s",
1570cd9f4ecSchristos daemon->shm_info->id_ctl, strerror(errno));
1580cd9f4ecSchristos
1590cd9f4ecSchristos /* Just release memory unused */
1600cd9f4ecSchristos free(daemon->shm_info);
161*7a540f2bSchristos daemon->shm_info = NULL;
1620cd9f4ecSchristos
1630cd9f4ecSchristos return 0;
1640cd9f4ecSchristos }
1650cd9f4ecSchristos
1660cd9f4ecSchristos daemon->shm_info->ptr_arr = (struct ub_stats_info*)
1670cd9f4ecSchristos shmat(daemon->shm_info->id_arr, NULL, 0);
1680cd9f4ecSchristos
1690cd9f4ecSchristos if (daemon->shm_info->ptr_arr == (void *) -1)
1700cd9f4ecSchristos {
1710cd9f4ecSchristos log_err("SHM failed(arr) cannot shmat(%d) %s",
1720cd9f4ecSchristos daemon->shm_info->id_arr, strerror(errno));
1730cd9f4ecSchristos
1740cd9f4ecSchristos /* Just release memory unused */
1750cd9f4ecSchristos free(daemon->shm_info);
176*7a540f2bSchristos daemon->shm_info = NULL;
1770cd9f4ecSchristos
1780cd9f4ecSchristos return 0;
1790cd9f4ecSchristos }
1800cd9f4ecSchristos
1810cd9f4ecSchristos /* Zero fill SHM to stand clean while is not filled by other events */
1820cd9f4ecSchristos memset(daemon->shm_info->ptr_ctl, 0, sizeof(struct ub_shm_stat_info));
1830cd9f4ecSchristos memset(daemon->shm_info->ptr_arr, 0, shm_size);
1840cd9f4ecSchristos
1850cd9f4ecSchristos shm_stat = daemon->shm_info->ptr_ctl;
1860cd9f4ecSchristos shm_stat->num_threads = daemon->num;
1870cd9f4ecSchristos
1880cd9f4ecSchristos #else
1890cd9f4ecSchristos (void)daemon;
1900cd9f4ecSchristos #endif /* HAVE_SHMGET */
1910cd9f4ecSchristos return 1;
1920cd9f4ecSchristos }
1930cd9f4ecSchristos
shm_main_shutdown(struct daemon * daemon)1940cd9f4ecSchristos void shm_main_shutdown(struct daemon* daemon)
1950cd9f4ecSchristos {
1960cd9f4ecSchristos #ifdef HAVE_SHMGET
1970cd9f4ecSchristos /* web are OK, just disabled */
1980cd9f4ecSchristos if(!daemon->cfg->shm_enable)
1990cd9f4ecSchristos return;
2000cd9f4ecSchristos
2010cd9f4ecSchristos verbose(VERB_DETAIL, "SHM shutdown - KEY [%d] - ID CTL [%d] ARR [%d] - PTR CTL [%p] ARR [%p]",
2020cd9f4ecSchristos daemon->shm_info->key, daemon->shm_info->id_ctl, daemon->shm_info->id_arr, daemon->shm_info->ptr_ctl, daemon->shm_info->ptr_arr);
2030cd9f4ecSchristos
2040cd9f4ecSchristos /* Destroy previous SHM */
2050cd9f4ecSchristos if (daemon->shm_info->id_ctl >= 0)
2060cd9f4ecSchristos shmctl(daemon->shm_info->id_ctl, IPC_RMID, NULL);
2070cd9f4ecSchristos
2080cd9f4ecSchristos if (daemon->shm_info->id_arr >= 0)
2090cd9f4ecSchristos shmctl(daemon->shm_info->id_arr, IPC_RMID, NULL);
2100cd9f4ecSchristos
2110cd9f4ecSchristos if (daemon->shm_info->ptr_ctl)
2120cd9f4ecSchristos shmdt(daemon->shm_info->ptr_ctl);
2130cd9f4ecSchristos
2140cd9f4ecSchristos if (daemon->shm_info->ptr_arr)
2150cd9f4ecSchristos shmdt(daemon->shm_info->ptr_arr);
2160cd9f4ecSchristos
217*7a540f2bSchristos free(daemon->shm_info);
218*7a540f2bSchristos daemon->shm_info = NULL;
2190cd9f4ecSchristos #else
2200cd9f4ecSchristos (void)daemon;
2210cd9f4ecSchristos #endif /* HAVE_SHMGET */
2220cd9f4ecSchristos }
2230cd9f4ecSchristos
shm_main_run(struct worker * worker)2240cd9f4ecSchristos void shm_main_run(struct worker *worker)
2250cd9f4ecSchristos {
2260cd9f4ecSchristos #ifdef HAVE_SHMGET
2270cd9f4ecSchristos struct ub_shm_stat_info *shm_stat;
2280cd9f4ecSchristos struct ub_stats_info *stat_total;
2290cd9f4ecSchristos struct ub_stats_info *stat_info;
2300cd9f4ecSchristos int offset;
2310cd9f4ecSchristos
23201049ae6Schristos #ifndef S_SPLINT_S
2330cd9f4ecSchristos verbose(VERB_DETAIL, "SHM run - worker [%d] - daemon [%p] - timenow(%u) - timeboot(%u)",
2340cd9f4ecSchristos worker->thread_num, worker->daemon, (unsigned)worker->env.now_tv->tv_sec, (unsigned)worker->daemon->time_boot.tv_sec);
23501049ae6Schristos #endif
2360cd9f4ecSchristos
2370cd9f4ecSchristos offset = worker->thread_num + 1;
2380cd9f4ecSchristos stat_total = worker->daemon->shm_info->ptr_arr;
2390cd9f4ecSchristos stat_info = worker->daemon->shm_info->ptr_arr + offset;
2400cd9f4ecSchristos
2410cd9f4ecSchristos /* Copy data to the current position */
2420cd9f4ecSchristos server_stats_compile(worker, stat_info, 0);
2430cd9f4ecSchristos
2440cd9f4ecSchristos /* First thread, zero fill total, and copy general info */
2450cd9f4ecSchristos if (worker->thread_num == 0) {
2460cd9f4ecSchristos
2470cd9f4ecSchristos /* Copy data to the current position */
2480cd9f4ecSchristos memset(stat_total, 0, sizeof(struct ub_stats_info));
2490cd9f4ecSchristos
2500cd9f4ecSchristos /* Point to data into SHM */
25101049ae6Schristos #ifndef S_SPLINT_S
2520cd9f4ecSchristos shm_stat = worker->daemon->shm_info->ptr_ctl;
2530cd9f4ecSchristos shm_stat->time.now_sec = (long long)worker->env.now_tv->tv_sec;
2540cd9f4ecSchristos shm_stat->time.now_usec = (long long)worker->env.now_tv->tv_usec;
25501049ae6Schristos #endif
2560cd9f4ecSchristos
2570cd9f4ecSchristos stat_timeval_subtract(&shm_stat->time.up_sec, &shm_stat->time.up_usec, worker->env.now_tv, &worker->daemon->time_boot);
2580cd9f4ecSchristos stat_timeval_subtract(&shm_stat->time.elapsed_sec, &shm_stat->time.elapsed_usec, worker->env.now_tv, &worker->daemon->time_last_stat);
2590cd9f4ecSchristos
2600cd9f4ecSchristos shm_stat->mem.msg = (long long)slabhash_get_mem(worker->env.msg_cache);
2610cd9f4ecSchristos shm_stat->mem.rrset = (long long)slabhash_get_mem(&worker->env.rrset_cache->table);
2620cd9f4ecSchristos shm_stat->mem.dnscrypt_shared_secret = 0;
2630cd9f4ecSchristos #ifdef USE_DNSCRYPT
2640cd9f4ecSchristos if(worker->daemon->dnscenv) {
2650cd9f4ecSchristos shm_stat->mem.dnscrypt_shared_secret = (long long)slabhash_get_mem(
2660cd9f4ecSchristos worker->daemon->dnscenv->shared_secrets_cache);
2670cd9f4ecSchristos shm_stat->mem.dnscrypt_nonce = (long long)slabhash_get_mem(
2680cd9f4ecSchristos worker->daemon->dnscenv->nonces_cache);
2690cd9f4ecSchristos }
2700cd9f4ecSchristos #endif
2710cd9f4ecSchristos shm_stat->mem.val = (long long)mod_get_mem(&worker->env,
2720cd9f4ecSchristos "validator");
2730cd9f4ecSchristos shm_stat->mem.iter = (long long)mod_get_mem(&worker->env,
2740cd9f4ecSchristos "iterator");
2750cd9f4ecSchristos shm_stat->mem.respip = (long long)mod_get_mem(&worker->env,
2760cd9f4ecSchristos "respip");
2770cd9f4ecSchristos
2780cd9f4ecSchristos /* subnet mem value is available in shm, also when not enabled,
2790cd9f4ecSchristos * to make the struct easier to memmap by other applications,
2800cd9f4ecSchristos * independent of the configuration of unbound */
2810cd9f4ecSchristos shm_stat->mem.subnet = 0;
2820cd9f4ecSchristos #ifdef CLIENT_SUBNET
2830cd9f4ecSchristos shm_stat->mem.subnet = (long long)mod_get_mem(&worker->env,
284*7a540f2bSchristos "subnetcache");
2850cd9f4ecSchristos #endif
2860cd9f4ecSchristos /* ipsecmod mem value is available in shm, also when not enabled,
2870cd9f4ecSchristos * to make the struct easier to memmap by other applications,
2880cd9f4ecSchristos * independent of the configuration of unbound */
2890cd9f4ecSchristos shm_stat->mem.ipsecmod = 0;
2900cd9f4ecSchristos #ifdef USE_IPSECMOD
2910cd9f4ecSchristos shm_stat->mem.ipsecmod = (long long)mod_get_mem(&worker->env,
2920cd9f4ecSchristos "ipsecmod");
2930cd9f4ecSchristos #endif
294d0eba39bSchristos #ifdef WITH_DYNLIBMODULE
295d0eba39bSchristos shm_stat->mem.dynlib = (long long)mod_get_mem(&worker->env,
296d0eba39bSchristos "dynlib");
297d0eba39bSchristos #endif
2980cd9f4ecSchristos }
2990cd9f4ecSchristos
3000cd9f4ecSchristos server_stats_add(stat_total, stat_info);
3010cd9f4ecSchristos
3020cd9f4ecSchristos /* print the thread statistics */
3030cd9f4ecSchristos stat_total->mesh_time_median /= (double)worker->daemon->num;
3040cd9f4ecSchristos
3050cd9f4ecSchristos #else
3060cd9f4ecSchristos (void)worker;
3070cd9f4ecSchristos #endif /* HAVE_SHMGET */
3080cd9f4ecSchristos }
309