net: statistics: Expose relevant information through net mgmt API

User application can request the information it wants via the generic
net_mgmt() call, following the NET_REQUEST_STATS_* codes.

Change-Id: Ia9e7d318cf11b7bf8bfaf1ad63c8c985be846cc1
Signed-off-by: Tomasz Bursztyka <tomasz.bursztyka@linux.intel.com>
This commit is contained in:
Tomasz Bursztyka 2016-12-14 14:31:58 +01:00
commit a79f9bcbc4
3 changed files with 220 additions and 0 deletions

View file

@ -240,6 +240,95 @@ struct net_stats {
#endif
};
#if defined(CONFIG_NET_STATISTICS_USER_API)
/* Management part definitions */
#include <net/net_mgmt.h>
#define _NET_STATS_LAYER NET_MGMT_LAYER_L3
#define _NET_STATS_CODE 0x101
#define _NET_STATS_BASE (NET_MGMT_LAYER(_NET_STATS_LAYER) | \
NET_MGMT_LAYER_CODE(_NET_STATS_CODE))
enum net_request_stats_cmd {
NET_REQUEST_STATS_CMD_GET_ALL = 1,
NET_REQUEST_STATS_CMD_GET_PROCESSING_ERROR,
NET_REQUEST_STATS_CMD_GET_IP_ERRORS,
NET_REQUEST_STATS_CMD_GET_IPV4,
NET_REQUEST_STATS_CMD_GET_IPV6,
NET_REQUEST_STATS_CMD_GET_IPV6_ND,
NET_REQUEST_STATS_CMD_GET_ICMP,
NET_REQUEST_STATS_CMD_GET_UDP,
NET_REQUEST_STATS_CMD_GET_TCP,
NET_REQUEST_STATS_CMD_GET_RPL,
};
#define NET_REQUEST_STATS_GET_ALL \
(_NET_STATS_BASE | NET_REQUEST_STATS_CMD_GET_ALL)
//NET_MGMT_DEFINE_REQUEST_HANDLER(NET_REQUEST_STATS_GET_ALL);
#define NET_REQUEST_STATS_GET_PROCESSING_ERROR \
(_NET_STATS_BASE | NET_REQUEST_STATS_CMD_GET_PROCESSING_ERROR)
//NET_MGMT_DEFINE_REQUEST_HANDLER(NET_REQUEST_STATS_GET_PROCESSING_ERROR);
#define NET_REQUEST_STATS_GET_IP_ERRORS \
(_NET_STATS_BASE | NET_REQUEST_STATS_CMD_GET_IP_ERRORS)
//NET_MGMT_DEFINE_REQUEST_HANDLER(NET_REQUEST_STATS_GET_IP_ERRORS);
#if defined(CONFIG_NET_STATISTICS_IPV4)
#define NET_REQUEST_STATS_GET_IPV4 \
(_NET_STATS_BASE | NET_REQUEST_STATS_CMD_GET_IPV4)
NET_MGMT_DEFINE_REQUEST_HANDLER(NET_REQUEST_STATS_GET_IPV4);
#endif /* CONFIG_NET_STATISTICS_IPV4 */
#if defined(CONFIG_NET_STATISTICS_IPV6)
#define NET_REQUEST_STATS_GET_IPV6 \
(_NET_STATS_BASE | NET_REQUEST_STATS_CMD_GET_IPV6)
NET_MGMT_DEFINE_REQUEST_HANDLER(NET_REQUEST_STATS_GET_IPV6);
#endif /* CONFIG_NET_STATISTICS_IPV6 */
#if defined(CONFIG_NET_STATISTICS_IPV6_ND)
#define NET_REQUEST_STATS_GET_IPV6_ND \
(_NET_STATS_BASE | NET_REQUEST_STATS_CMD_GET_IPV6_ND)
NET_MGMT_DEFINE_REQUEST_HANDLER(NET_REQUEST_STATS_GET_IPV6_ND);
#endif /* CONFIG_NET_STATISTICS_IPV6_ND */
#if defined(CONFIG_NET_STATISTICS_ICMP)
#define NET_REQUEST_STATS_GET_ICMP \
(_NET_STATS_BASE | NET_REQUEST_STATS_CMD_GET_ICMP)
NET_MGMT_DEFINE_REQUEST_HANDLER(NET_REQUEST_STATS_GET_ICMP);
#endif /* CONFIG_NET_STATISTICS_ICMP */
#if defined(CONFIG_NET_STATISTICS_UDP)
#define NET_REQUEST_STATS_GET_UDP \
(_NET_STATS_BASE | NET_REQUEST_STATS_CMD_GET_UDP)
NET_MGMT_DEFINE_REQUEST_HANDLER(NET_REQUEST_STATS_GET_UDP);
#endif /* CONFIG_NET_STATISTICS_UDP */
#if defined(CONFIG_NET_STATISTICS_TCP)
#define NET_REQUEST_STATS_GET_TCP \
(_NET_STATS_BASE | NET_REQUEST_STATS_CMD_GET_TCP)
NET_MGMT_DEFINE_REQUEST_HANDLER(NET_REQUEST_STATS_GET_TCP);
#endif /* CONFIG_NET_STATISTICS_TCP */
#if defined(CONFIG_NET_STATISTICS_RPL)
#define NET_REQUEST_STATS_GET_RPL \
(_NET_STATS_BASE | NET_REQUEST_STATS_CMD_GET_RPL)
NET_MGMT_DEFINE_REQUEST_HANDLER(NET_REQUEST_STATS_GET_RPL);
#endif /* CONFIG_NET_STATISTICS_RPL */
#endif /* CONFIG_NET_STATISTICS_USER_API */
#ifdef __cplusplus
}
#endif

View file

@ -24,6 +24,14 @@ menuconfig NET_STATISTICS
if NET_STATISTICS
config NET_STATISTICS_USER_API
bool "Expose statistics through NET MGMT API"
select NET_MGMT
default n
help
Enable this if you need to grab relevant statistics in your code,
via calling net_mgmt() with relevant NET_REQUEST_STATS_GET_* command.
config NET_STATISTICS_PERIODIC_OUTPUT
bool "Simple periodic output"
depends on NET_LOG

View file

@ -20,6 +20,7 @@
#include <kernel.h>
#include <string.h>
#include <errno.h>
#include <net/net_core.h>
#include "net_stats.h"
@ -135,3 +136,125 @@ void net_print_statistics(void)
}
#endif /* CONFIG_NET_STATISTICS_PERIODIC_OUTPUT */
#if defined(CONFIG_NET_STATISTICS_USER_API)
static int net_stats_get(uint32_t mgmt_request, struct net_if *iface,
void *data, size_t len)
{
size_t len_chk = 0;
void *src = NULL;
ARG_UNUSED(iface);
switch (NET_MGMT_GET_COMMAND(mgmt_request)) {
case NET_REQUEST_STATS_CMD_GET_ALL:
len_chk = sizeof(struct net_stats);
src = &net_stats;
break;
case NET_REQUEST_STATS_CMD_GET_PROCESSING_ERROR:
len_chk = sizeof(net_stats_t);
src = &net_stats.processing_error;
break;
case NET_REQUEST_STATS_CMD_GET_IP_ERRORS:
len_chk = sizeof(struct net_stats_ip_errors);
src = &net_stats.ip_errors;
break;
#if defined(CONFIG_NET_STATISTICS_IPV4)
case NET_REQUEST_STATS_CMD_GET_IPV4:
len_chk = sizeof(struct net_stats_ip);
src = &net_stats.ipv4;
break;
#endif
#if defined(CONFIG_NET_STATISTICS_IPV6)
case NET_REQUEST_STATS_CMD_GET_IPV6:
len_chk = sizeof(struct net_stats_ip);
src = &net_stats.ipv6;
break;
#endif
#if defined(CONFIG_NET_STATISTICS_IPV6_ND)
case NET_REQUEST_STATS_CMD_GET_IPV6_ND:
len_chk = sizeof(struct net_stats_ipv6_nd);
src = &net_stats.ipv6_nd;
break;
#endif
#if defined(CONFIG_NET_STATISTICS_ICMP)
case NET_REQUEST_STATS_CMD_GET_ICMP:
len_chk = sizeof(struct net_stats_icmp);
src = &net_stats.icmp;
break;
#endif
#if defined(CONFIG_NET_STATISTICS_UDP)
case NET_REQUEST_STATS_CMD_GET_UDP:
len_chk = sizeof(struct net_stats_udp);
src = &net_stats.udp;
break;
#endif
#if defined(CONFIG_NET_STATISTICS_TCP)
case NET_REQUEST_STATS_CMD_GET_TCP:
len_chk = sizeof(struct net_stats_tcp);
src = &net_stats.tcp;
break;
#endif
#if defined(CONFIG_NET_STATISTICS_RPL)
case NET_REQUEST_STATS_CMD_GET_RPL:
len_chk = sizeof(struct net_stats_rpl);
src = &net_stats.rpl;
break;
#endif
}
if (len != len_chk || !src) {
return -EINVAL;
}
memcpy(src, data, len);
return 0;
}
NET_MGMT_REGISTER_REQUEST_HANDLER(NET_REQUEST_STATS_GET_ALL,
net_stats_get);
NET_MGMT_REGISTER_REQUEST_HANDLER(NET_REQUEST_STATS_GET_PROCESSING_ERROR,
net_stats_get);
NET_MGMT_REGISTER_REQUEST_HANDLER(NET_REQUEST_STATS_GET_IP_ERRORS,
net_stats_get);
#if defined(CONFIG_NET_STATISTICS_IPV4)
NET_MGMT_REGISTER_REQUEST_HANDLER(NET_REQUEST_STATS_GET_IPV4,
net_stats_get);
#endif
#if defined(CONFIG_NET_STATISTICS_IPV6)
NET_MGMT_REGISTER_REQUEST_HANDLER(NET_REQUEST_STATS_GET_IPV6,
net_stats_get);
#endif
#if defined(CONFIG_NET_STATISTICS_IPV6_ND)
NET_MGMT_REGISTER_REQUEST_HANDLER(NET_REQUEST_STATS_GET_IPV6_ND,
net_stats_get);
#endif
#if defined(CONFIG_NET_STATISTICS_ICMP)
NET_MGMT_REGISTER_REQUEST_HANDLER(NET_REQUEST_STATS_GET_ICMP,
net_stats_get);
#endif
#if defined(CONFIG_NET_STATISTICS_UDP)
NET_MGMT_REGISTER_REQUEST_HANDLER(NET_REQUEST_STATS_GET_UDP,
net_stats_get);
#endif
#if defined(CONFIG_NET_STATISTICS_TCP)
NET_MGMT_REGISTER_REQUEST_HANDLER(NET_REQUEST_STATS_GET_TCP,
net_stats_get);
#endif
#if defined(CONFIG_NET_STATISTICS_RPL)
NET_MGMT_REGISTER_REQUEST_HANDLER(NET_REQUEST_STATS_GET_RPL,
net_stats_get);
#endif
#endif /* CONFIG_NET_STATISTICS_USER_API */