From 2f961ab36aa080c5bc1b9992f27989beb4ff0daf Mon Sep 17 00:00:00 2001 From: Miroslav Lichvar Date: Thu, 16 Mar 2023 16:56:28 +0100 Subject: [PATCH] clientlog: count RX and TX timestamps for each source Count served timestamps in all combinations of RX/TX and daemon/kernel/hardware. Repurpose CLG_LogAuthNtpRequest() to update all NTP-specific stats in one call per accepted request and response. --- clientlog.c | 17 +++++++++++++++-- clientlog.h | 3 ++- ntp_core.c | 6 ++++-- reports.h | 6 ++++++ 4 files changed, 27 insertions(+), 5 deletions(-) diff --git a/clientlog.c b/clientlog.c index a6b199e..611e753 100644 --- a/clientlog.c +++ b/clientlog.c @@ -165,6 +165,8 @@ static uint32_t total_drops[MAX_SERVICES]; static uint32_t total_ntp_auth_hits; static uint32_t total_ntp_interleaved_hits; static uint32_t total_record_drops; +static uint32_t total_ntp_rx_timestamps[MAX_NTP_TS + 1]; +static uint32_t total_ntp_tx_timestamps[MAX_NTP_TS + 1]; #define NSEC_PER_SEC 1000000000U @@ -643,9 +645,14 @@ CLG_LimitServiceRate(CLG_Service service, int index) /* ================================================== */ void -CLG_LogAuthNtpRequest(void) +CLG_UpdateNtpStats(int auth, NTP_Timestamp_Source rx_ts_src, NTP_Timestamp_Source tx_ts_src) { - total_ntp_auth_hits++; + if (auth) + total_ntp_auth_hits++; + if (rx_ts_src >= 0 && rx_ts_src <= MAX_NTP_TS) + total_ntp_rx_timestamps[rx_ts_src]++; + if (tx_ts_src >= 0 && tx_ts_src <= MAX_NTP_TS) + total_ntp_tx_timestamps[tx_ts_src]++; } /* ================================================== */ @@ -1095,4 +1102,10 @@ CLG_GetServerStatsReport(RPT_ServerStatsReport *report) report->ntp_span_seconds = ntp_ts_map.size > 1 ? (get_ntp_tss(ntp_ts_map.size - 1)->rx_ts - get_ntp_tss(0)->rx_ts) >> 32 : 0; + report->ntp_daemon_rx_timestamps = total_ntp_rx_timestamps[NTP_TS_DAEMON]; + report->ntp_daemon_tx_timestamps = total_ntp_tx_timestamps[NTP_TS_DAEMON]; + report->ntp_kernel_rx_timestamps = total_ntp_rx_timestamps[NTP_TS_KERNEL]; + report->ntp_kernel_tx_timestamps = total_ntp_tx_timestamps[NTP_TS_KERNEL]; + report->ntp_hw_rx_timestamps = total_ntp_rx_timestamps[NTP_TS_HARDWARE]; + report->ntp_hw_tx_timestamps = total_ntp_tx_timestamps[NTP_TS_HARDWARE]; } diff --git a/clientlog.h b/clientlog.h index f7d8a48..9ea0a3f 100644 --- a/clientlog.h +++ b/clientlog.h @@ -42,7 +42,8 @@ extern void CLG_Finalise(void); extern int CLG_GetClientIndex(IPAddr *client); extern int CLG_LogServiceAccess(CLG_Service service, IPAddr *client, struct timespec *now); extern int CLG_LimitServiceRate(CLG_Service service, int index); -extern void CLG_LogAuthNtpRequest(void); +extern void CLG_UpdateNtpStats(int auth, NTP_Timestamp_Source rx_ts_src, + NTP_Timestamp_Source tx_ts_src); extern int CLG_GetNtpMinPoll(void); /* Functions to save and retrieve timestamps for server interleaved mode */ diff --git a/ntp_core.c b/ntp_core.c index 05eab94..a6e47ac 100644 --- a/ntp_core.c +++ b/ntp_core.c @@ -2455,8 +2455,6 @@ NCR_ProcessRxUnknown(NTP_Remote_Address *remote_addr, NTP_Local_Address *local_a /* Don't respond unless a non-zero KoD was returned */ if (kod == 0) return; - } else if (info.auth.mode != NTP_AUTH_NONE && info.auth.mode != NTP_AUTH_MSSNTP) { - CLG_LogAuthNtpRequest(); } local_ntp_rx = NULL; @@ -2485,6 +2483,10 @@ NCR_ProcessRxUnknown(NTP_Remote_Address *remote_addr, NTP_Local_Address *local_a CLG_DisableNtpTimestamps(&ntp_rx); } + CLG_UpdateNtpStats(kod != 0 && info.auth.mode != NTP_AUTH_NONE && + info.auth.mode != NTP_AUTH_MSSNTP, + rx_ts->source, interleaved ? tx_ts->source : NTP_TS_DAEMON); + /* Suggest the client to increase its polling interval if it indicates the interval is shorter than the rate limiting interval */ poll = CLG_GetNtpMinPoll(); diff --git a/reports.h b/reports.h index 6674dd9..68164a6 100644 --- a/reports.h +++ b/reports.h @@ -120,6 +120,12 @@ typedef struct { uint32_t ntp_interleaved_hits; uint32_t ntp_timestamps; uint32_t ntp_span_seconds; + uint32_t ntp_daemon_rx_timestamps; + uint32_t ntp_daemon_tx_timestamps; + uint32_t ntp_kernel_rx_timestamps; + uint32_t ntp_kernel_tx_timestamps; + uint32_t ntp_hw_rx_timestamps; + uint32_t ntp_hw_tx_timestamps; } RPT_ServerStatsReport; typedef struct {