Home | History | Annotate | Line # | Download | only in daemon
      1 /*
      2  * daemon/stats.c - collect runtime performance indicators.
      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 data structure used to collect runtime performance
     40  * numbers. These 'statistics' may be of interest to the operator.
     41  */
     42 #include "config.h"
     43 #ifdef HAVE_TIME_H
     44 #include <time.h>
     45 #endif
     46 #include <sys/time.h>
     47 #include <sys/types.h>
     48 #include "daemon/stats.h"
     49 #include "daemon/worker.h"
     50 #include "daemon/daemon.h"
     51 #include "services/mesh.h"
     52 #include "services/outside_network.h"
     53 #include "services/listen_dnsport.h"
     54 #include "util/config_file.h"
     55 #include "util/tube.h"
     56 #include "util/timehist.h"
     57 #include "util/net_help.h"
     58 #include "validator/validator.h"
     59 #include "iterator/iterator.h"
     60 #include "sldns/sbuffer.h"
     61 #include "services/cache/rrset.h"
     62 #include "services/cache/infra.h"
     63 #include "services/authzone.h"
     64 #include "validator/val_kcache.h"
     65 #include "validator/val_neg.h"
     66 #ifdef CLIENT_SUBNET
     67 #include "edns-subnet/subnetmod.h"
     68 #endif
     69 #ifdef HAVE_SSL
     70 #include <openssl/ssl.h>
     71 #endif
     72 
     73 /** How long to wait for threads to transmit statistics, in msec. */
     74 #define STATS_THREAD_WAIT 60000
     75 
     76 /** add timers and the values do not overflow or become negative */
     77 static void
     78 stats_timeval_add(long long* d_sec, long long* d_usec, long long add_sec, long long add_usec)
     79 {
     80 #ifndef S_SPLINT_S
     81 	(*d_sec) += add_sec;
     82 	(*d_usec) += add_usec;
     83 	if((*d_usec) >= 1000000) {
     84 		(*d_usec) -= 1000000;
     85 		(*d_sec)++;
     86 	}
     87 #endif
     88 }
     89 
     90 void server_stats_init(struct ub_server_stats* stats, struct config_file* cfg)
     91 {
     92 	memset(stats, 0, sizeof(*stats));
     93 	stats->extended = cfg->stat_extended;
     94 }
     95 
     96 void server_stats_querymiss(struct ub_server_stats* stats, struct worker* worker)
     97 {
     98 	stats->num_queries_missed_cache++;
     99 	stats->sum_query_list_size += worker->env.mesh->all.count;
    100 	if((long long)worker->env.mesh->all.count > stats->max_query_list_size)
    101 		stats->max_query_list_size = (long long)worker->env.mesh->all.count;
    102 }
    103 
    104 void server_stats_prefetch(struct ub_server_stats* stats, struct worker* worker)
    105 {
    106 	stats->num_queries_prefetch++;
    107 	/* changes the query list size so account that, like a querymiss */
    108 	stats->sum_query_list_size += worker->env.mesh->all.count;
    109 	if((long long)worker->env.mesh->all.count > stats->max_query_list_size)
    110 		stats->max_query_list_size = (long long)worker->env.mesh->all.count;
    111 }
    112 
    113 void server_stats_log(struct ub_server_stats* stats, struct worker* worker,
    114 	int threadnum)
    115 {
    116 	log_info("server stats for thread %d: %u queries, "
    117 		"%u answers from cache, %u recursions, %u prefetch, %u rejected by "
    118 		"ip ratelimiting",
    119 		threadnum, (unsigned)stats->num_queries,
    120 		(unsigned)(stats->num_queries -
    121 			stats->num_queries_missed_cache),
    122 		(unsigned)stats->num_queries_missed_cache,
    123 		(unsigned)stats->num_queries_prefetch,
    124 		(unsigned)stats->num_queries_ip_ratelimited);
    125 	log_info("server stats for thread %d: requestlist max %u avg %g "
    126 		"exceeded %u jostled %u", threadnum,
    127 		(unsigned)stats->max_query_list_size,
    128 		(stats->num_queries_missed_cache+stats->num_queries_prefetch)?
    129 			(double)stats->sum_query_list_size/
    130 			(double)(stats->num_queries_missed_cache+
    131 			stats->num_queries_prefetch) : 0.0,
    132 		(unsigned)worker->env.mesh->stats_dropped,
    133 		(unsigned)worker->env.mesh->stats_jostled);
    134 }
    135 
    136 
    137 #ifdef CLIENT_SUBNET
    138 /** Set the EDNS Subnet stats. */
    139 static void
    140 set_subnet_stats(struct worker* worker, struct ub_server_stats* svr,
    141 	int reset)
    142 {
    143 	int m = modstack_find(&worker->env.mesh->mods, "subnetcache");
    144 	struct subnet_env* sne;
    145 	if(m == -1)
    146 		return;
    147 	sne = (struct subnet_env*)worker->env.modinfo[m];
    148 	if(reset && !worker->env.cfg->stat_cumulative) {
    149 		lock_rw_wrlock(&sne->biglock);
    150 	} else {
    151 		lock_rw_rdlock(&sne->biglock);
    152 	}
    153 	svr->num_query_subnet = (long long)(sne->num_msg_nocache + sne->num_msg_cache);
    154 	svr->num_query_subnet_cache = (long long)sne->num_msg_cache;
    155 	if(reset && !worker->env.cfg->stat_cumulative) {
    156 		sne->num_msg_cache = 0;
    157 		sne->num_msg_nocache = 0;
    158 	}
    159 	lock_rw_unlock(&sne->biglock);
    160 }
    161 #endif /* CLIENT_SUBNET */
    162 
    163 /** Set the neg cache stats. */
    164 static void
    165 set_neg_cache_stats(struct worker* worker, struct ub_server_stats* svr,
    166 	int reset)
    167 {
    168 	int m = modstack_find(&worker->env.mesh->mods, "validator");
    169 	struct val_env* ve;
    170 	struct val_neg_cache* neg;
    171 	if(m == -1)
    172 		return;
    173 	ve = (struct val_env*)worker->env.modinfo[m];
    174 	if(!ve->neg_cache)
    175 		return;
    176 	neg = ve->neg_cache;
    177 	lock_basic_lock(&neg->lock);
    178 	svr->num_neg_cache_noerror = (long long)neg->num_neg_cache_noerror;
    179 	svr->num_neg_cache_nxdomain = (long long)neg->num_neg_cache_nxdomain;
    180 	if(reset && !worker->env.cfg->stat_cumulative) {
    181 		neg->num_neg_cache_noerror = 0;
    182 		neg->num_neg_cache_nxdomain = 0;
    183 	}
    184 	lock_basic_unlock(&neg->lock);
    185 }
    186 
    187 /** get rrsets bogus number from validator */
    188 static size_t
    189 get_rrset_bogus(struct worker* worker, int reset)
    190 {
    191 	int m = modstack_find(&worker->env.mesh->mods, "validator");
    192 	struct val_env* ve;
    193 	size_t r;
    194 	if(m == -1)
    195 		return 0;
    196 	ve = (struct val_env*)worker->env.modinfo[m];
    197 	lock_basic_lock(&ve->bogus_lock);
    198 	r = ve->num_rrset_bogus;
    199 	if(reset && !worker->env.cfg->stat_cumulative)
    200 		ve->num_rrset_bogus = 0;
    201 	lock_basic_unlock(&ve->bogus_lock);
    202 	return r;
    203 }
    204 
    205 /** get number of ratelimited queries from iterator */
    206 static size_t
    207 get_queries_ratelimit(struct worker* worker, int reset)
    208 {
    209 	int m = modstack_find(&worker->env.mesh->mods, "iterator");
    210 	struct iter_env* ie;
    211 	size_t r;
    212 	if(m == -1)
    213 		return 0;
    214 	ie = (struct iter_env*)worker->env.modinfo[m];
    215 	lock_basic_lock(&ie->queries_ratelimit_lock);
    216 	r = ie->num_queries_ratelimited;
    217 	if(reset && !worker->env.cfg->stat_cumulative)
    218 		ie->num_queries_ratelimited = 0;
    219 	lock_basic_unlock(&ie->queries_ratelimit_lock);
    220 	return r;
    221 }
    222 
    223 #ifdef USE_DNSCRYPT
    224 /** get the number of shared secret cache miss */
    225 static size_t
    226 get_dnscrypt_cache_miss(struct worker* worker, int reset)
    227 {
    228 	size_t r;
    229 	struct dnsc_env* de = worker->daemon->dnscenv;
    230 	if(!de) return 0;
    231 
    232 	lock_basic_lock(&de->shared_secrets_cache_lock);
    233 	r = de->num_query_dnscrypt_secret_missed_cache;
    234 	if(reset && !worker->env.cfg->stat_cumulative)
    235 		de->num_query_dnscrypt_secret_missed_cache = 0;
    236 	lock_basic_unlock(&de->shared_secrets_cache_lock);
    237 	return r;
    238 }
    239 
    240 /** get the number of replayed queries */
    241 static size_t
    242 get_dnscrypt_replay(struct worker* worker, int reset)
    243 {
    244 	size_t r;
    245 	struct dnsc_env* de = worker->daemon->dnscenv;
    246 
    247 	lock_basic_lock(&de->nonces_cache_lock);
    248 	r = de->num_query_dnscrypt_replay;
    249 	if(reset && !worker->env.cfg->stat_cumulative)
    250 		de->num_query_dnscrypt_replay = 0;
    251 	lock_basic_unlock(&de->nonces_cache_lock);
    252 	return r;
    253 }
    254 #endif /* USE_DNSCRYPT */
    255 
    256 void
    257 server_stats_compile(struct worker* worker, struct ub_stats_info* s, int reset)
    258 {
    259 	int i;
    260 	struct listen_list* lp;
    261 
    262 	s->svr = worker->stats;
    263 	s->mesh_num_states = (long long)worker->env.mesh->all.count;
    264 	s->mesh_num_reply_states = (long long)worker->env.mesh->num_reply_states;
    265 	s->mesh_num_reply_addrs = (long long)worker->env.mesh->num_reply_addrs;
    266 	s->mesh_jostled = (long long)worker->env.mesh->stats_jostled;
    267 	s->mesh_dropped = (long long)worker->env.mesh->stats_dropped;
    268 	s->mesh_replies_sent = (long long)worker->env.mesh->replies_sent;
    269 	s->mesh_replies_sum_wait_sec = (long long)worker->env.mesh->replies_sum_wait.tv_sec;
    270 	s->mesh_replies_sum_wait_usec = (long long)worker->env.mesh->replies_sum_wait.tv_usec;
    271 	s->mesh_time_median = timehist_quartile(worker->env.mesh->histogram,
    272 		0.50);
    273 
    274 	/* add in the values from the mesh */
    275 	s->svr.ans_secure += (long long)worker->env.mesh->ans_secure;
    276 	s->svr.ans_bogus += (long long)worker->env.mesh->ans_bogus;
    277 	s->svr.val_ops += (long long)worker->env.mesh->val_ops;
    278 	s->svr.ans_rcode_nodata += (long long)worker->env.mesh->ans_nodata;
    279 	s->svr.ans_expired += (long long)worker->env.mesh->ans_expired;
    280 	for(i=0; i<UB_STATS_RCODE_NUM; i++)
    281 		s->svr.ans_rcode[i] += (long long)worker->env.mesh->ans_rcode[i];
    282 	for(i=0; i<UB_STATS_RPZ_ACTION_NUM; i++)
    283 		s->svr.rpz_action[i] += (long long)worker->env.mesh->rpz_action[i];
    284 	timehist_export(worker->env.mesh->histogram, s->svr.hist,
    285 		NUM_BUCKETS_HIST);
    286 	s->svr.num_queries_discard_timeout +=
    287 		(long long)worker->env.mesh->num_queries_discard_timeout;
    288 	s->svr.num_queries_replyaddr_limit +=
    289 		(long long)worker->env.mesh->num_queries_replyaddr_limit;
    290 	s->svr.num_queries_wait_limit +=
    291 		(long long)worker->env.mesh->num_queries_wait_limit;
    292 	s->svr.num_dns_error_reports +=
    293 		(long long)worker->env.mesh->num_dns_error_reports;
    294 	/* values from outside network */
    295 	s->svr.unwanted_replies = (long long)worker->back->unwanted_replies;
    296 	s->svr.qtcp_outgoing = (long long)worker->back->num_tcp_outgoing;
    297 	s->svr.qudp_outgoing = (long long)worker->back->num_udp_outgoing;
    298 
    299 	/* get and reset validator rrset bogus number */
    300 	s->svr.rrset_bogus = (long long)get_rrset_bogus(worker, reset);
    301 
    302 	/* get and reset iterator query ratelimit number */
    303 	s->svr.queries_ratelimited = (long long)get_queries_ratelimit(worker, reset);
    304 
    305 	/* get cache sizes */
    306 	get_slabhash_stats(worker->env.msg_cache,
    307 		&s->svr.msg_cache_count, &s->svr.msg_cache_max_collisions);
    308 	get_slabhash_stats(&worker->env.rrset_cache->table,
    309 		&s->svr.rrset_cache_count, &s->svr.rrset_cache_max_collisions);
    310 	s->svr.infra_cache_count = (long long)count_slabhash_entries(worker->env.infra_cache->hosts);
    311 	if(worker->env.key_cache)
    312 		s->svr.key_cache_count = (long long)count_slabhash_entries(worker->env.key_cache->slab);
    313 	else	s->svr.key_cache_count = 0;
    314 
    315 #ifdef USE_DNSCRYPT
    316 	if(worker->daemon->dnscenv) {
    317 		s->svr.num_query_dnscrypt_secret_missed_cache =
    318 			(long long)get_dnscrypt_cache_miss(worker, reset);
    319 		s->svr.shared_secret_cache_count = (long long)count_slabhash_entries(
    320 			worker->daemon->dnscenv->shared_secrets_cache);
    321 		s->svr.nonce_cache_count = (long long)count_slabhash_entries(
    322 			worker->daemon->dnscenv->nonces_cache);
    323 		s->svr.num_query_dnscrypt_replay =
    324 			(long long)get_dnscrypt_replay(worker, reset);
    325 	} else {
    326 		s->svr.num_query_dnscrypt_secret_missed_cache = 0;
    327 		s->svr.shared_secret_cache_count = 0;
    328 		s->svr.nonce_cache_count = 0;
    329 		s->svr.num_query_dnscrypt_replay = 0;
    330 	}
    331 #else
    332 	s->svr.num_query_dnscrypt_secret_missed_cache = 0;
    333 	s->svr.shared_secret_cache_count = 0;
    334 	s->svr.nonce_cache_count = 0;
    335 	s->svr.num_query_dnscrypt_replay = 0;
    336 #endif /* USE_DNSCRYPT */
    337 	if(worker->env.auth_zones) {
    338 		s->svr.num_query_authzone_up += (long long)worker->env.mesh->num_query_authzone_up;
    339 		s->svr.num_query_authzone_down += (long long)worker->env.mesh->num_query_authzone_down;
    340 	}
    341 	s->svr.mem_stream_wait =
    342 		(long long)tcp_req_info_get_stream_buffer_size();
    343 	s->svr.mem_http2_query_buffer =
    344 		(long long)http2_get_query_buffer_size();
    345 	s->svr.mem_http2_response_buffer =
    346 		(long long)http2_get_response_buffer_size();
    347 #ifdef HAVE_NGTCP2
    348 	s->svr.mem_quic = (long long)doq_table_quic_size_get(
    349 		worker->daemon->doq_table);
    350 #else
    351 	s->svr.mem_quic = 0;
    352 #endif /* HAVE_NGTCP2 */
    353 
    354 	/* Set neg cache usage numbers */
    355 	set_neg_cache_stats(worker, &s->svr, reset);
    356 #ifdef CLIENT_SUBNET
    357 	/* EDNS Subnet usage numbers */
    358 	set_subnet_stats(worker, &s->svr, reset);
    359 #else
    360 	s->svr.num_query_subnet = 0;
    361 	s->svr.num_query_subnet_cache = 0;
    362 #endif
    363 #ifdef USE_CACHEDB
    364 	s->svr.num_query_cachedb = (long long)worker->env.mesh->ans_cachedb;
    365 #else
    366 	s->svr.num_query_cachedb = 0;
    367 #endif
    368 
    369 	/* get tcp accept usage */
    370 	s->svr.tcp_accept_usage = 0;
    371 	for(lp = worker->front->cps; lp; lp = lp->next) {
    372 		if(lp->com->type == comm_tcp_accept)
    373 			s->svr.tcp_accept_usage += (long long)lp->com->cur_tcp_count;
    374 	}
    375 
    376 	if(reset && !worker->env.cfg->stat_cumulative) {
    377 		worker_stats_clear(worker);
    378 	}
    379 }
    380 
    381 void server_stats_obtain(struct worker* worker, struct worker* who,
    382 	struct ub_stats_info* s, int reset)
    383 {
    384 	uint8_t *reply = NULL;
    385 	uint32_t len = 0;
    386 	if(worker == who) {
    387 		/* just fill it in */
    388 		server_stats_compile(worker, s, reset);
    389 		return;
    390 	}
    391 	/* communicate over tube */
    392 	verbose(VERB_ALGO, "write stats cmd");
    393 	if(reset)
    394 		worker_send_cmd(who, worker_cmd_stats);
    395 	else 	worker_send_cmd(who, worker_cmd_stats_noreset);
    396 	verbose(VERB_ALGO, "wait for stats reply");
    397 	if(tube_wait_timeout(worker->cmd, STATS_THREAD_WAIT) == 0) {
    398 #if defined(HAVE_PTHREAD) && defined(SIZEOF_PTHREAD_T) && defined(SIZEOF_UNSIGNED_LONG)
    399 #  if SIZEOF_PTHREAD_T == SIZEOF_UNSIGNED_LONG
    400 		unsigned long pthid = 0;
    401 		if(verbosity >= VERB_OPS)
    402 			memcpy(&pthid, &who->thr_id, sizeof(unsigned long));
    403 #  endif
    404 #endif
    405 		verbose(VERB_OPS, "no response from thread %d"
    406 #ifdef HAVE_GETTID
    407 			" LWP %u"
    408 #endif
    409 #if defined(HAVE_PTHREAD) && defined(SIZEOF_PTHREAD_T) && defined(SIZEOF_UNSIGNED_LONG)
    410 #  if SIZEOF_PTHREAD_T == SIZEOF_UNSIGNED_LONG
    411 			" pthread 0x%lx"
    412 #  endif
    413 #endif
    414 			,
    415 			who->thread_num
    416 #ifdef HAVE_GETTID
    417 			, (unsigned)who->thread_tid
    418 #endif
    419 #if defined(HAVE_PTHREAD) && defined(SIZEOF_PTHREAD_T) && defined(SIZEOF_UNSIGNED_LONG)
    420 #  if SIZEOF_PTHREAD_T == SIZEOF_UNSIGNED_LONG
    421 			, pthid
    422 #  endif
    423 #endif
    424 			);
    425 		log_err("server_stats_obtain: no response from worker %d "
    426 			"(stats timeout); returning zero stats for this worker",
    427 			who->thread_num);
    428 		/* A later reply from the worker, would be sizeof stats reply,
    429 		 * and the worker_handle_control_cmd routine discards if
    430 		 * it is not a 4byte command, when that is received here. */
    431 		memset(s, 0, sizeof(*s));
    432 		return;
    433 	}
    434 	if(!tube_read_msg(worker->cmd, &reply, &len, 0)) {
    435 		log_err("server_stats_obtain: failed to read stats from worker "
    436 			"(tube read error); returning zero stats for this worker");
    437 		memset(s, 0, sizeof(*s));
    438 		return;
    439 	}
    440 	if(len != (uint32_t)sizeof(*s)) {
    441 		log_err("server_stats_obtain: wrong stats length %d (expected %d); "
    442 			"discarding", (int)len, (int)sizeof(*s));
    443 		free(reply);
    444 		memset(s, 0, sizeof(*s));
    445 		return;
    446 	}
    447 	memcpy(s, reply, (size_t)len);
    448 	free(reply);
    449 }
    450 
    451 void server_stats_reply(struct worker* worker, int reset)
    452 {
    453 	struct ub_stats_info s;
    454 	server_stats_compile(worker, &s, reset);
    455 	verbose(VERB_ALGO, "write stats replymsg");
    456 	if(!tube_write_msg(worker->daemon->workers[0]->cmd,
    457 		(uint8_t*)&s, sizeof(s), 0))
    458 		log_err("could not write stat values over cmd channel");
    459 }
    460 
    461 void server_stats_add(struct ub_stats_info* total, struct ub_stats_info* a)
    462 {
    463 	total->svr.num_queries += a->svr.num_queries;
    464 	total->svr.num_queries_ip_ratelimited += a->svr.num_queries_ip_ratelimited;
    465 	total->svr.num_queries_cookie_valid += a->svr.num_queries_cookie_valid;
    466 	total->svr.num_queries_cookie_client += a->svr.num_queries_cookie_client;
    467 	total->svr.num_queries_cookie_invalid += a->svr.num_queries_cookie_invalid;
    468 	total->svr.num_queries_discard_timeout +=
    469 		a->svr.num_queries_discard_timeout;
    470 	total->svr.num_queries_replyaddr_limit +=
    471 		a->svr.num_queries_replyaddr_limit;
    472 	total->svr.num_queries_wait_limit += a->svr.num_queries_wait_limit;
    473 	total->svr.num_dns_error_reports += a->svr.num_dns_error_reports;
    474 	total->svr.num_queries_missed_cache += a->svr.num_queries_missed_cache;
    475 	total->svr.num_queries_prefetch += a->svr.num_queries_prefetch;
    476 	total->svr.num_queries_timed_out += a->svr.num_queries_timed_out;
    477 	total->svr.num_query_authzone_up += a->svr.num_query_authzone_up;
    478 	total->svr.num_query_authzone_down += a->svr.num_query_authzone_down;
    479 	if (total->svr.max_query_time_us < a->svr.max_query_time_us)
    480 		total->svr.max_query_time_us = a->svr.max_query_time_us;
    481 	total->svr.sum_query_list_size += a->svr.sum_query_list_size;
    482 	total->svr.ans_expired += a->svr.ans_expired;
    483 #ifdef USE_DNSCRYPT
    484 	total->svr.num_query_dnscrypt_crypted += a->svr.num_query_dnscrypt_crypted;
    485 	total->svr.num_query_dnscrypt_cert += a->svr.num_query_dnscrypt_cert;
    486 	total->svr.num_query_dnscrypt_cleartext +=
    487 		a->svr.num_query_dnscrypt_cleartext;
    488 	total->svr.num_query_dnscrypt_crypted_malformed +=
    489 		a->svr.num_query_dnscrypt_crypted_malformed;
    490 #endif /* USE_DNSCRYPT */
    491 	/* the max size reached is upped to higher of both */
    492 	if(a->svr.max_query_list_size > total->svr.max_query_list_size)
    493 		total->svr.max_query_list_size = a->svr.max_query_list_size;
    494 
    495 	if(a->svr.extended) {
    496 		int i;
    497 		total->svr.qtype_big += a->svr.qtype_big;
    498 		total->svr.qclass_big += a->svr.qclass_big;
    499 		total->svr.qtcp += a->svr.qtcp;
    500 		total->svr.qtcp_outgoing += a->svr.qtcp_outgoing;
    501 		total->svr.qudp_outgoing += a->svr.qudp_outgoing;
    502 		total->svr.qtls += a->svr.qtls;
    503 		total->svr.qtls_resume += a->svr.qtls_resume;
    504 		total->svr.qhttps += a->svr.qhttps;
    505 		total->svr.qquic += a->svr.qquic;
    506 		total->svr.qipv6 += a->svr.qipv6;
    507 		total->svr.qbit_QR += a->svr.qbit_QR;
    508 		total->svr.qbit_AA += a->svr.qbit_AA;
    509 		total->svr.qbit_TC += a->svr.qbit_TC;
    510 		total->svr.qbit_RD += a->svr.qbit_RD;
    511 		total->svr.qbit_RA += a->svr.qbit_RA;
    512 		total->svr.qbit_Z += a->svr.qbit_Z;
    513 		total->svr.qbit_AD += a->svr.qbit_AD;
    514 		total->svr.qbit_CD += a->svr.qbit_CD;
    515 		total->svr.qEDNS += a->svr.qEDNS;
    516 		total->svr.qEDNS_DO += a->svr.qEDNS_DO;
    517 		total->svr.ans_rcode_nodata += a->svr.ans_rcode_nodata;
    518 		total->svr.ans_secure += a->svr.ans_secure;
    519 		total->svr.ans_bogus += a->svr.ans_bogus;
    520 		total->svr.val_ops += a->svr.val_ops;
    521 		total->svr.unwanted_replies += a->svr.unwanted_replies;
    522 		total->svr.unwanted_queries += a->svr.unwanted_queries;
    523 		total->svr.tcp_accept_usage += a->svr.tcp_accept_usage;
    524 #ifdef USE_CACHEDB
    525 		total->svr.num_query_cachedb += a->svr.num_query_cachedb;
    526 #endif
    527 		for(i=0; i<UB_STATS_QTYPE_NUM; i++)
    528 			total->svr.qtype[i] += a->svr.qtype[i];
    529 		for(i=0; i<UB_STATS_QCLASS_NUM; i++)
    530 			total->svr.qclass[i] += a->svr.qclass[i];
    531 		for(i=0; i<UB_STATS_OPCODE_NUM; i++)
    532 			total->svr.qopcode[i] += a->svr.qopcode[i];
    533 		for(i=0; i<UB_STATS_RCODE_NUM; i++)
    534 			total->svr.ans_rcode[i] += a->svr.ans_rcode[i];
    535 		for(i=0; i<NUM_BUCKETS_HIST; i++)
    536 			total->svr.hist[i] += a->svr.hist[i];
    537 		for(i=0; i<UB_STATS_RPZ_ACTION_NUM; i++)
    538 			total->svr.rpz_action[i] += a->svr.rpz_action[i];
    539 	}
    540 
    541 	total->mesh_num_states += a->mesh_num_states;
    542 	total->mesh_num_reply_states += a->mesh_num_reply_states;
    543 	total->mesh_num_reply_addrs += a->mesh_num_reply_addrs;
    544 	total->mesh_jostled += a->mesh_jostled;
    545 	total->mesh_dropped += a->mesh_dropped;
    546 	total->mesh_replies_sent += a->mesh_replies_sent;
    547 	stats_timeval_add(&total->mesh_replies_sum_wait_sec, &total->mesh_replies_sum_wait_usec, a->mesh_replies_sum_wait_sec, a->mesh_replies_sum_wait_usec);
    548 	/* the medians are averaged together, this is not as accurate as
    549 	 * taking the median over all of the data, but is good and fast
    550 	 * added up here, division later*/
    551 	total->mesh_time_median += a->mesh_time_median;
    552 }
    553 
    554 void server_stats_insquery(struct ub_server_stats* stats, struct comm_point* c,
    555 	uint16_t qtype, uint16_t qclass, struct edns_data* edns,
    556 	struct comm_reply* repinfo)
    557 {
    558 	uint16_t flags = sldns_buffer_read_u16_at(c->buffer, 2);
    559 	if(qtype < UB_STATS_QTYPE_NUM)
    560 		stats->qtype[qtype]++;
    561 	else	stats->qtype_big++;
    562 	if(qclass < UB_STATS_QCLASS_NUM)
    563 		stats->qclass[qclass]++;
    564 	else	stats->qclass_big++;
    565 	stats->qopcode[ LDNS_OPCODE_WIRE(sldns_buffer_begin(c->buffer)) ]++;
    566 	if(c->type != comm_udp) {
    567 		if(c->type != comm_doq)
    568 			stats->qtcp++;
    569 		if(c->ssl != NULL) {
    570 			stats->qtls++;
    571 #ifdef HAVE_SSL
    572 			if(SSL_session_reused(c->ssl))
    573 				stats->qtls_resume++;
    574 #endif
    575 			if(c->type == comm_http)
    576 				stats->qhttps++;
    577 #ifdef HAVE_NGTCP2
    578 			else if(c->type == comm_doq)
    579 				stats->qquic++;
    580 #endif
    581 		}
    582 	}
    583 	if(repinfo && addr_is_ip6(&repinfo->remote_addr, repinfo->remote_addrlen))
    584 		stats->qipv6++;
    585 	if( (flags&BIT_QR) )
    586 		stats->qbit_QR++;
    587 	if( (flags&BIT_AA) )
    588 		stats->qbit_AA++;
    589 	if( (flags&BIT_TC) )
    590 		stats->qbit_TC++;
    591 	if( (flags&BIT_RD) )
    592 		stats->qbit_RD++;
    593 	if( (flags&BIT_RA) )
    594 		stats->qbit_RA++;
    595 	if( (flags&BIT_Z) )
    596 		stats->qbit_Z++;
    597 	if( (flags&BIT_AD) )
    598 		stats->qbit_AD++;
    599 	if( (flags&BIT_CD) )
    600 		stats->qbit_CD++;
    601 	if(edns->edns_present) {
    602 		stats->qEDNS++;
    603 		if( (edns->bits & EDNS_DO) )
    604 			stats->qEDNS_DO++;
    605 	}
    606 }
    607 
    608 void server_stats_insrcode(struct ub_server_stats* stats, sldns_buffer* buf)
    609 {
    610 	if(stats->extended && sldns_buffer_limit(buf) != 0) {
    611 		int r = (int)LDNS_RCODE_WIRE( sldns_buffer_begin(buf) );
    612 		stats->ans_rcode[r] ++;
    613 		if(r == 0 && LDNS_ANCOUNT( sldns_buffer_begin(buf) ) == 0)
    614 			stats->ans_rcode_nodata ++;
    615 	}
    616 }
    617 
    618 void server_stats_downstream_cookie(struct ub_server_stats* stats,
    619 	struct edns_data* edns)
    620 {
    621 	if(!(edns->edns_present && edns->cookie_present)) return;
    622 	if(edns->cookie_valid) {
    623 		stats->num_queries_cookie_valid++;
    624 	} else if(edns->cookie_client) {
    625 		stats->num_queries_cookie_client++;
    626 	} else {
    627 		stats->num_queries_cookie_invalid++;
    628 	}
    629 }
    630