Skip to content

Commit e25f89d

Browse files
Meissi-jianxiaoxiang781216
authored andcommitted
app/netstatistics: Add net statistics api for user
Signed-off-by: meijian <[email protected]>
1 parent 7bf20f1 commit e25f89d

File tree

4 files changed

+166
-0
lines changed

4 files changed

+166
-0
lines changed

include/netutils/netlib.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@
5858

5959
#include <net/if.h>
6060
#include <netinet/in.h>
61+
#include <nuttx/net/netdev.h>
6162
#include <nuttx/net/netconfig.h>
6263

6364
/****************************************************************************
@@ -495,6 +496,11 @@ int netlib_set_ipv6dnsaddr(FAR const struct in6_addr *inaddr);
495496

496497
int netlib_set_mtu(FAR const char *ifname, int mtu);
497498

499+
#if defined(CONFIG_NETDEV_STATISTICS)
500+
int netlib_getifstatistics(FAR const char *ifname,
501+
FAR struct netdev_statistics_s *stat);
502+
#endif
503+
498504
#undef EXTERN
499505
#ifdef __cplusplus
500506
}

netutils/netlib/CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,10 @@ if(CONFIG_NETUTILS_NETLIB)
123123
netlib_eaddrconv.c)
124124
endif()
125125

126+
if(CONFIG_NETDEV_STATISTICS)
127+
list(APPEND SRCS netlib_getifstatistics.c)
128+
endif()
129+
126130
if(CONFIG_WIRELESS_PKTRADIO)
127131
list(APPEND SRCS netlib_getproperties.c netlib_getnodeaddr.c
128132
netlib_setnodeaddr.c)

netutils/netlib/Makefile

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,10 @@ CSRCS += netlib_getroute.c
9191
endif
9292
endif
9393

94+
ifeq ($(CONFIG_NETDEV_STATISTICS),y)
95+
CSRCS += netlib_getifstatistics.c
96+
endif
97+
9498
# Netfilter connection support
9599

96100
ifeq ($(CONFIG_NETLINK_NETFILTER),y)
Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
1+
/****************************************************************************
2+
* apps/netutils/netlib/netlib_getifstatistics.c
3+
*
4+
* Licensed to the Apache Software Foundation (ASF) under one or more
5+
* contributor license agreements. See the NOTICE file distributed with
6+
* this work for additional information regarding copyright ownership. The
7+
* ASF licenses this file to you under the Apache License, Version 2.0 (the
8+
* "License"); you may not use this file except in compliance with the
9+
* License. You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
16+
* License for the specific language governing permissions and limitations
17+
* under the License.
18+
*
19+
****************************************************************************/
20+
21+
/****************************************************************************
22+
* Included Files
23+
****************************************************************************/
24+
25+
#include <nuttx/config.h>
26+
27+
#include <stdio.h>
28+
#include <string.h>
29+
#include <errno.h>
30+
#include <debug.h>
31+
32+
#include <sys/types.h>
33+
34+
#include "netutils/netlib.h"
35+
36+
#ifdef CONFIG_NETDEV_STATISTICS
37+
38+
/****************************************************************************
39+
* Pre-processor Definitions
40+
****************************************************************************/
41+
42+
/* Determines the size of an intermediate buffer that must be large enough
43+
* to handle the longest line generated by this logic.
44+
*/
45+
46+
#define PROCFS_LINELEN 80
47+
#define PROCFS_NET_PATH "/proc/net/"
48+
49+
/* The form of the entry from the netstat file:
50+
*
51+
* wlan0 Link encap:Ethernet HWaddr 42:37:46:02:16:07 at UP mtu 1500
52+
* inet addr:10.0.1.2 DRaddr:10.0.1.1 Mask:255.255.255.0
53+
* inet6 DRaddr: ::
54+
*
55+
* RX: Received Fragment Errors Bytes
56+
* 000008c1 00000000 00000000 331ca8
57+
* IPv4 IPv6 ARP Dropped
58+
* 000008a7 00000018 00000002 00000000
59+
* TX: Queued Sent Errors Timeouts Bytes
60+
* 00000973 00000973 00000000 00000000 1b8d3
61+
* Total Errors: 00000000
62+
*/
63+
64+
/****************************************************************************
65+
* Public Functions
66+
****************************************************************************/
67+
68+
/****************************************************************************
69+
* Name: netlib_getifstatistics
70+
*
71+
* Description:
72+
* Read the DEV RX/TX statistics.
73+
*
74+
* Input Parameters:
75+
* ifname - The interface to read.
76+
* stat - The pointer to return dev statistics.
77+
*
78+
* Returned Value:
79+
* 0 on success. A negated errno value is returned
80+
* on any failure.
81+
****************************************************************************/
82+
83+
int netlib_getifstatistics(FAR const char *ifname,
84+
FAR struct netdev_statistics_s *stat)
85+
{
86+
FAR FILE *stream;
87+
char path[32];
88+
char line[PROCFS_LINELEN];
89+
int ret = OK;
90+
91+
snprintf(path, sizeof(path), "%s%s", PROCFS_NET_PATH, ifname);
92+
ninfo("get statistics from %s \n", path);
93+
94+
stream = fopen(path, "r");
95+
if (stream == NULL)
96+
{
97+
fprintf(stderr, "ERROR: Failed to open path:%s \n", path);
98+
return -ENOTDIR;
99+
}
100+
101+
while (fgets(line, sizeof(line), stream) != NULL)
102+
{
103+
/* Read RX header and next is the data */
104+
105+
if (strstr(line, "RX:") != NULL)
106+
{
107+
if (fgets(line, sizeof(line), stream) == NULL)
108+
{
109+
ret = -EINVAL;
110+
break;
111+
}
112+
113+
if (sscanf(line, "%" SCNx32 "%" SCNx32 "%" SCNx32 "%" SCNx64,
114+
&stat->rx_packets,
115+
&stat->rx_fragments,
116+
&stat->rx_errors,
117+
&stat->rx_bytes) <= 0)
118+
{
119+
ret = -EINVAL;
120+
break;
121+
}
122+
}
123+
124+
/* Read TX header and next is the data */
125+
126+
if (strstr(line, "TX:") != NULL)
127+
{
128+
if (fgets(line, sizeof(line), stream) == NULL)
129+
{
130+
ret = -EINVAL;
131+
break;
132+
}
133+
134+
if (sscanf(line, "%" SCNx32 "%" SCNx32 "%" SCNx32 \
135+
"%" SCNx32 "%" SCNx64,
136+
&stat->tx_packets,
137+
&stat->tx_done,
138+
&stat->tx_errors,
139+
&stat->tx_timeouts,
140+
&stat->tx_bytes) <= 0)
141+
{
142+
ret = -EINVAL;
143+
break;
144+
}
145+
}
146+
}
147+
148+
fclose(stream);
149+
return ret;
150+
}
151+
152+
#endif /* CONFIG_NETDEV_STATISTICS */

0 commit comments

Comments
 (0)