Skip to content

Commit

Permalink
net: stats: dns: Collect DNS statistics
Browse files Browse the repository at this point in the history
If DNS statistics is enabled in Kconfig, then start to collect it.
This is useful in order to see how many DNS requests/responses
received or sent, and also see the amount of dropped DNS packets.

Signed-off-by: Jukka Rissanen <jukka.rissanen@nordicsemi.no>
  • Loading branch information
jukkar authored and mmahadevan108 committed Sep 25, 2024
1 parent ca0bd2c commit 765bfbb
Show file tree
Hide file tree
Showing 8 changed files with 107 additions and 0 deletions.
19 changes: 19 additions & 0 deletions include/zephyr/net/net_stats.h
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,20 @@ struct net_stats_ipv4_igmp {
net_stats_t drop;
};

/**
* @brief DNS statistics
*/
struct net_stats_dns {
/** Number of received DNS queries */
net_stats_t recv;

/** Number of sent DNS responses */
net_stats_t sent;

/** Number of dropped DNS packets */
net_stats_t drop;
};

/**
* @brief Network packet transfer times for calculating average TX time
*/
Expand Down Expand Up @@ -375,6 +389,11 @@ struct net_stats {
struct net_stats_ipv4_igmp ipv4_igmp;
#endif

#if defined(CONFIG_NET_STATISTICS_DNS)
/** DNS statistics */
struct net_stats_dns dns;
#endif

#if NET_TC_COUNT > 1
/** Traffic class statistics */
struct net_stats_tc tc;
Expand Down
7 changes: 7 additions & 0 deletions subsys/net/ip/Kconfig.stats
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,13 @@ config NET_STATISTICS_IGMP
help
Keep track of IGMP related statistics

config NET_STATISTICS_DNS
bool "Domain Name Service (DNS) statistics"
depends on DNS_RESOLVER || MDNS_RESPONDER || LLMNR_RESPONDER
default y
help
Keep track of DNS related statistics

config NET_STATISTICS_PPP
bool "Point-to-point (PPP) statistics"
depends on NET_L2_PPP
Expand Down
21 changes: 21 additions & 0 deletions subsys/net/ip/net_stats.h
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,27 @@ static inline void net_stats_update_ipv4_igmp_drop(struct net_if *iface)
#define net_stats_update_ipv4_igmp_drop(iface)
#endif /* CONFIG_NET_STATISTICS_IGMP */

#if defined(CONFIG_NET_STATISTICS_DNS)
static inline void net_stats_update_dns_recv(struct net_if *iface)
{
UPDATE_STAT(iface, stats.dns.recv++);
}

static inline void net_stats_update_dns_sent(struct net_if *iface)
{
UPDATE_STAT(iface, stats.dns.sent++);
}

static inline void net_stats_update_dns_drop(struct net_if *iface)
{
UPDATE_STAT(iface, stats.dns.drop++);
}
#else
#define net_stats_update_dns_recv(iface)
#define net_stats_update_dns_sent(iface)
#define net_stats_update_dns_drop(iface)
#endif /* CONFIG_NET_STATISTICS_DNS */

#if defined(CONFIG_NET_PKT_TXTIME_STATS) && defined(CONFIG_NET_STATISTICS)
static inline void net_stats_update_tx_time(struct net_if *iface,
uint32_t start_time,
Expand Down
20 changes: 20 additions & 0 deletions subsys/net/lib/dns/dispatcher.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,11 @@ LOG_MODULE_REGISTER(net_dns_dispatcher, CONFIG_DNS_SOCKET_DISPATCHER_LOG_LEVEL);
#include <zephyr/sys/check.h>
#include <zephyr/sys/slist.h>
#include <zephyr/net_buf.h>
#include <zephyr/net/net_if.h>
#include <zephyr/net/dns_resolve.h>
#include <zephyr/net/socket_service.h>

#include "../../ip/net_stats.h"
#include "dns_pack.h"

static K_MUTEX_DEFINE(lock);
Expand Down Expand Up @@ -95,6 +97,24 @@ static int dns_dispatch(struct dns_socket_dispatcher *dispatcher,
}

done:
if (IS_ENABLED(CONFIG_NET_STATISTICS_DNS)) {
struct net_if *iface = NULL;

if (IS_ENABLED(CONFIG_NET_IPV6) && addr->sa_family == AF_INET6) {
iface = net_if_ipv6_select_src_iface(&net_sin6(addr)->sin6_addr);
} else if (IS_ENABLED(CONFIG_NET_IPV4) && addr->sa_family == AF_INET) {
iface = net_if_ipv4_select_src_iface(&net_sin(addr)->sin_addr);
}

if (iface != NULL) {
if (ret < 0) {
net_stats_update_dns_drop(iface);
} else {
net_stats_update_dns_recv(iface);
}
}
}

return ret;
}

Expand Down
14 changes: 14 additions & 0 deletions subsys/net/lib/dns/llmnr_responder.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ LOG_MODULE_REGISTER(net_llmnr_responder, CONFIG_LLMNR_RESPONDER_LOG_LEVEL);

#include "dns_pack.h"
#include "ipv6.h"
#include "../../ip/net_stats.h"

#include "net_private.h"

Expand Down Expand Up @@ -401,6 +402,19 @@ static int send_response(int sock,
net_sprint_ipv4_addr(&net_sin((struct sockaddr *)&dst)->sin_addr) :
net_sprint_ipv6_addr(&net_sin6((struct sockaddr *)&dst)->sin6_addr),
ret);
} else {
struct net_if *iface = NULL;
struct sockaddr *addr = (struct sockaddr *)&dst;

if (IS_ENABLED(CONFIG_NET_IPV6) && src_addr->sa_family == AF_INET6) {
iface = net_if_ipv6_select_src_iface(&net_sin6(addr)->sin6_addr);
} else if (IS_ENABLED(CONFIG_NET_IPV4) && src_addr->sa_family == AF_INET) {
iface = net_if_ipv4_select_src_iface(&net_sin(addr)->sin_addr);
}

if (iface != NULL) {
net_stats_update_dns_sent(iface);
}
}

return ret;
Expand Down
5 changes: 5 additions & 0 deletions subsys/net/lib/dns/mdns_responder.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ LOG_MODULE_REGISTER(net_mdns_responder, CONFIG_MDNS_RESPONDER_LOG_LEVEL);
#include "dns_sd.h"
#include "dns_pack.h"
#include "ipv6.h"
#include "../../ip/net_stats.h"

#include "net_private.h"

Expand Down Expand Up @@ -336,6 +337,8 @@ static int send_response(int sock,
(struct sockaddr *)&dst, dst_len);
if (ret < 0) {
NET_DBG("Cannot send %s reply (%d)", "mDNS", ret);
} else {
net_stats_update_dns_sent(iface);
}

return ret;
Expand Down Expand Up @@ -496,6 +499,8 @@ static void send_sd_response(int sock,
if (ret < 0) {
NET_DBG("Cannot send %s reply (%d)", "mDNS", ret);
continue;
} else {
net_stats_update_dns_sent(iface);
}
}
}
Expand Down
15 changes: 15 additions & 0 deletions subsys/net/lib/dns/resolve.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ LOG_MODULE_REGISTER(net_dns_resolve, CONFIG_DNS_RESOLVER_LOG_LEVEL);
#include "dns_pack.h"
#include "dns_internal.h"
#include "dns_cache.h"
#include "../../ip/net_stats.h"

#define DNS_SERVER_COUNT CONFIG_DNS_RESOLVER_MAX_SERVERS
#define SERVER_COUNT (DNS_SERVER_COUNT + DNS_MAX_MCAST_SERVERS)
Expand Down Expand Up @@ -1041,6 +1042,20 @@ static int dns_write(struct dns_resolve_context *ctx,
if (ret < 0) {
NET_DBG("Cannot send query (%d)", -errno);
return ret;
} else {
if (IS_ENABLED(CONFIG_NET_STATISTICS_DNS)) {
struct net_if *iface = NULL;

if (IS_ENABLED(CONFIG_NET_IPV6) && server->sa_family == AF_INET6) {
iface = net_if_ipv6_select_src_iface(&net_sin6(server)->sin6_addr);
} else if (IS_ENABLED(CONFIG_NET_IPV4) && server->sa_family == AF_INET) {
iface = net_if_ipv4_select_src_iface(&net_sin(server)->sin_addr);
}

if (iface != NULL) {
net_stats_update_dns_sent(iface);
}
}
}

return 0;
Expand Down
6 changes: 6 additions & 0 deletions subsys/net/lib/shell/stats.c
Original file line number Diff line number Diff line change
Expand Up @@ -537,6 +537,12 @@ static void net_shell_print_statistics(struct net_if *iface, void *user_data)
GET_STAT(iface, tcp.connrst));
PR("TCP pkt drop %d\n", GET_STAT(iface, tcp.drop));
#endif
#if defined(CONFIG_NET_STATISTICS_DNS)
PR("DNS recv %d\tsent\t%d\tdrop\t%d\n",
GET_STAT(iface, dns.recv),
GET_STAT(iface, dns.sent),
GET_STAT(iface, dns.drop));
#endif /* CONFIG_NET_STATISTICS_DNS */

PR("Bytes received %u\n", GET_STAT(iface, bytes.received));
PR("Bytes sent %u\n", GET_STAT(iface, bytes.sent));
Expand Down

0 comments on commit 765bfbb

Please sign in to comment.