From e25f89d2d6ec3f588557aa0adc9f52324bbd7a01 Mon Sep 17 00:00:00 2001 From: meijian Date: Fri, 6 Sep 2024 15:22:35 +0800 Subject: [PATCH] app/netstatistics: Add net statistics api for user Signed-off-by: meijian --- include/netutils/netlib.h | 6 + netutils/netlib/CMakeLists.txt | 4 + netutils/netlib/Makefile | 4 + netutils/netlib/netlib_getifstatistics.c | 152 +++++++++++++++++++++++ 4 files changed, 166 insertions(+) create mode 100644 netutils/netlib/netlib_getifstatistics.c diff --git a/include/netutils/netlib.h b/include/netutils/netlib.h index acab1ba01..b92c95696 100644 --- a/include/netutils/netlib.h +++ b/include/netutils/netlib.h @@ -58,6 +58,7 @@ #include #include +#include #include /**************************************************************************** @@ -495,6 +496,11 @@ int netlib_set_ipv6dnsaddr(FAR const struct in6_addr *inaddr); int netlib_set_mtu(FAR const char *ifname, int mtu); +#if defined(CONFIG_NETDEV_STATISTICS) +int netlib_getifstatistics(FAR const char *ifname, + FAR struct netdev_statistics_s *stat); +#endif + #undef EXTERN #ifdef __cplusplus } diff --git a/netutils/netlib/CMakeLists.txt b/netutils/netlib/CMakeLists.txt index 87958ffd6..068fa68fe 100644 --- a/netutils/netlib/CMakeLists.txt +++ b/netutils/netlib/CMakeLists.txt @@ -123,6 +123,10 @@ if(CONFIG_NETUTILS_NETLIB) netlib_eaddrconv.c) endif() + if(CONFIG_NETDEV_STATISTICS) + list(APPEND SRCS netlib_getifstatistics.c) + endif() + if(CONFIG_WIRELESS_PKTRADIO) list(APPEND SRCS netlib_getproperties.c netlib_getnodeaddr.c netlib_setnodeaddr.c) diff --git a/netutils/netlib/Makefile b/netutils/netlib/Makefile index a088160e6..dc8be99d2 100644 --- a/netutils/netlib/Makefile +++ b/netutils/netlib/Makefile @@ -91,6 +91,10 @@ CSRCS += netlib_getroute.c endif endif +ifeq ($(CONFIG_NETDEV_STATISTICS),y) +CSRCS += netlib_getifstatistics.c +endif + # Netfilter connection support ifeq ($(CONFIG_NETLINK_NETFILTER),y) diff --git a/netutils/netlib/netlib_getifstatistics.c b/netutils/netlib/netlib_getifstatistics.c new file mode 100644 index 000000000..a6ac33e6c --- /dev/null +++ b/netutils/netlib/netlib_getifstatistics.c @@ -0,0 +1,152 @@ +/**************************************************************************** + * apps/netutils/netlib/netlib_getifstatistics.c + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. The + * ASF licenses this file to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance with the + * License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + * + ****************************************************************************/ + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include + +#include +#include +#include +#include + +#include + +#include "netutils/netlib.h" + +#ifdef CONFIG_NETDEV_STATISTICS + +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +/* Determines the size of an intermediate buffer that must be large enough + * to handle the longest line generated by this logic. + */ + +#define PROCFS_LINELEN 80 +#define PROCFS_NET_PATH "/proc/net/" + +/* The form of the entry from the netstat file: + * + * wlan0 Link encap:Ethernet HWaddr 42:37:46:02:16:07 at UP mtu 1500 + * inet addr:10.0.1.2 DRaddr:10.0.1.1 Mask:255.255.255.0 + * inet6 DRaddr: :: + * + * RX: Received Fragment Errors Bytes + * 000008c1 00000000 00000000 331ca8 + * IPv4 IPv6 ARP Dropped + * 000008a7 00000018 00000002 00000000 + * TX: Queued Sent Errors Timeouts Bytes + * 00000973 00000973 00000000 00000000 1b8d3 + * Total Errors: 00000000 + */ + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: netlib_getifstatistics + * + * Description: + * Read the DEV RX/TX statistics. + * + * Input Parameters: + * ifname - The interface to read. + * stat - The pointer to return dev statistics. + * + * Returned Value: + * 0 on success. A negated errno value is returned + * on any failure. + ****************************************************************************/ + +int netlib_getifstatistics(FAR const char *ifname, + FAR struct netdev_statistics_s *stat) +{ + FAR FILE *stream; + char path[32]; + char line[PROCFS_LINELEN]; + int ret = OK; + + snprintf(path, sizeof(path), "%s%s", PROCFS_NET_PATH, ifname); + ninfo("get statistics from %s \n", path); + + stream = fopen(path, "r"); + if (stream == NULL) + { + fprintf(stderr, "ERROR: Failed to open path:%s \n", path); + return -ENOTDIR; + } + + while (fgets(line, sizeof(line), stream) != NULL) + { + /* Read RX header and next is the data */ + + if (strstr(line, "RX:") != NULL) + { + if (fgets(line, sizeof(line), stream) == NULL) + { + ret = -EINVAL; + break; + } + + if (sscanf(line, "%" SCNx32 "%" SCNx32 "%" SCNx32 "%" SCNx64, + &stat->rx_packets, + &stat->rx_fragments, + &stat->rx_errors, + &stat->rx_bytes) <= 0) + { + ret = -EINVAL; + break; + } + } + + /* Read TX header and next is the data */ + + if (strstr(line, "TX:") != NULL) + { + if (fgets(line, sizeof(line), stream) == NULL) + { + ret = -EINVAL; + break; + } + + if (sscanf(line, "%" SCNx32 "%" SCNx32 "%" SCNx32 \ + "%" SCNx32 "%" SCNx64, + &stat->tx_packets, + &stat->tx_done, + &stat->tx_errors, + &stat->tx_timeouts, + &stat->tx_bytes) <= 0) + { + ret = -EINVAL; + break; + } + } + } + + fclose(stream); + return ret; +} + +#endif /* CONFIG_NETDEV_STATISTICS */