Skip to content

Commit

Permalink
app/netstatistics: Add net statistics api for user
Browse files Browse the repository at this point in the history
Signed-off-by: meijian <[email protected]>
  • Loading branch information
Meissi-jian authored and xiaoxiang781216 committed Sep 12, 2024
1 parent 7bf20f1 commit e25f89d
Show file tree
Hide file tree
Showing 4 changed files with 166 additions and 0 deletions.
6 changes: 6 additions & 0 deletions include/netutils/netlib.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@

#include <net/if.h>
#include <netinet/in.h>
#include <nuttx/net/netdev.h>
#include <nuttx/net/netconfig.h>

/****************************************************************************
Expand Down Expand Up @@ -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
}
Expand Down
4 changes: 4 additions & 0 deletions netutils/netlib/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 4 additions & 0 deletions netutils/netlib/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
152 changes: 152 additions & 0 deletions netutils/netlib/netlib_getifstatistics.c
Original file line number Diff line number Diff line change
@@ -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 <nuttx/config.h>

#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <debug.h>

#include <sys/types.h>

#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 */

0 comments on commit e25f89d

Please sign in to comment.