Skip to content

Commit a79f9bc

Browse files
author
Tomasz Bursztyka
committed
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 <[email protected]>
1 parent 88b99fa commit a79f9bc

File tree

3 files changed

+220
-0
lines changed

3 files changed

+220
-0
lines changed

include/net/net_stats.h

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,95 @@ struct net_stats {
240240
#endif
241241
};
242242

243+
#if defined(CONFIG_NET_STATISTICS_USER_API)
244+
/* Management part definitions */
245+
246+
#include <net/net_mgmt.h>
247+
248+
#define _NET_STATS_LAYER NET_MGMT_LAYER_L3
249+
#define _NET_STATS_CODE 0x101
250+
#define _NET_STATS_BASE (NET_MGMT_LAYER(_NET_STATS_LAYER) | \
251+
NET_MGMT_LAYER_CODE(_NET_STATS_CODE))
252+
253+
enum net_request_stats_cmd {
254+
NET_REQUEST_STATS_CMD_GET_ALL = 1,
255+
NET_REQUEST_STATS_CMD_GET_PROCESSING_ERROR,
256+
NET_REQUEST_STATS_CMD_GET_IP_ERRORS,
257+
NET_REQUEST_STATS_CMD_GET_IPV4,
258+
NET_REQUEST_STATS_CMD_GET_IPV6,
259+
NET_REQUEST_STATS_CMD_GET_IPV6_ND,
260+
NET_REQUEST_STATS_CMD_GET_ICMP,
261+
NET_REQUEST_STATS_CMD_GET_UDP,
262+
NET_REQUEST_STATS_CMD_GET_TCP,
263+
NET_REQUEST_STATS_CMD_GET_RPL,
264+
};
265+
266+
#define NET_REQUEST_STATS_GET_ALL \
267+
(_NET_STATS_BASE | NET_REQUEST_STATS_CMD_GET_ALL)
268+
269+
//NET_MGMT_DEFINE_REQUEST_HANDLER(NET_REQUEST_STATS_GET_ALL);
270+
271+
#define NET_REQUEST_STATS_GET_PROCESSING_ERROR \
272+
(_NET_STATS_BASE | NET_REQUEST_STATS_CMD_GET_PROCESSING_ERROR)
273+
274+
//NET_MGMT_DEFINE_REQUEST_HANDLER(NET_REQUEST_STATS_GET_PROCESSING_ERROR);
275+
276+
#define NET_REQUEST_STATS_GET_IP_ERRORS \
277+
(_NET_STATS_BASE | NET_REQUEST_STATS_CMD_GET_IP_ERRORS)
278+
279+
//NET_MGMT_DEFINE_REQUEST_HANDLER(NET_REQUEST_STATS_GET_IP_ERRORS);
280+
281+
#if defined(CONFIG_NET_STATISTICS_IPV4)
282+
#define NET_REQUEST_STATS_GET_IPV4 \
283+
(_NET_STATS_BASE | NET_REQUEST_STATS_CMD_GET_IPV4)
284+
285+
NET_MGMT_DEFINE_REQUEST_HANDLER(NET_REQUEST_STATS_GET_IPV4);
286+
#endif /* CONFIG_NET_STATISTICS_IPV4 */
287+
288+
#if defined(CONFIG_NET_STATISTICS_IPV6)
289+
#define NET_REQUEST_STATS_GET_IPV6 \
290+
(_NET_STATS_BASE | NET_REQUEST_STATS_CMD_GET_IPV6)
291+
292+
NET_MGMT_DEFINE_REQUEST_HANDLER(NET_REQUEST_STATS_GET_IPV6);
293+
#endif /* CONFIG_NET_STATISTICS_IPV6 */
294+
295+
#if defined(CONFIG_NET_STATISTICS_IPV6_ND)
296+
#define NET_REQUEST_STATS_GET_IPV6_ND \
297+
(_NET_STATS_BASE | NET_REQUEST_STATS_CMD_GET_IPV6_ND)
298+
299+
NET_MGMT_DEFINE_REQUEST_HANDLER(NET_REQUEST_STATS_GET_IPV6_ND);
300+
#endif /* CONFIG_NET_STATISTICS_IPV6_ND */
301+
302+
#if defined(CONFIG_NET_STATISTICS_ICMP)
303+
#define NET_REQUEST_STATS_GET_ICMP \
304+
(_NET_STATS_BASE | NET_REQUEST_STATS_CMD_GET_ICMP)
305+
306+
NET_MGMT_DEFINE_REQUEST_HANDLER(NET_REQUEST_STATS_GET_ICMP);
307+
#endif /* CONFIG_NET_STATISTICS_ICMP */
308+
309+
#if defined(CONFIG_NET_STATISTICS_UDP)
310+
#define NET_REQUEST_STATS_GET_UDP \
311+
(_NET_STATS_BASE | NET_REQUEST_STATS_CMD_GET_UDP)
312+
313+
NET_MGMT_DEFINE_REQUEST_HANDLER(NET_REQUEST_STATS_GET_UDP);
314+
#endif /* CONFIG_NET_STATISTICS_UDP */
315+
316+
#if defined(CONFIG_NET_STATISTICS_TCP)
317+
#define NET_REQUEST_STATS_GET_TCP \
318+
(_NET_STATS_BASE | NET_REQUEST_STATS_CMD_GET_TCP)
319+
320+
NET_MGMT_DEFINE_REQUEST_HANDLER(NET_REQUEST_STATS_GET_TCP);
321+
#endif /* CONFIG_NET_STATISTICS_TCP */
322+
323+
#if defined(CONFIG_NET_STATISTICS_RPL)
324+
#define NET_REQUEST_STATS_GET_RPL \
325+
(_NET_STATS_BASE | NET_REQUEST_STATS_CMD_GET_RPL)
326+
327+
NET_MGMT_DEFINE_REQUEST_HANDLER(NET_REQUEST_STATS_GET_RPL);
328+
#endif /* CONFIG_NET_STATISTICS_RPL */
329+
330+
#endif /* CONFIG_NET_STATISTICS_USER_API */
331+
243332
#ifdef __cplusplus
244333
}
245334
#endif

subsys/net/ip/Kconfig.stats

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,14 @@ menuconfig NET_STATISTICS
2424

2525
if NET_STATISTICS
2626

27+
config NET_STATISTICS_USER_API
28+
bool "Expose statistics through NET MGMT API"
29+
select NET_MGMT
30+
default n
31+
help
32+
Enable this if you need to grab relevant statistics in your code,
33+
via calling net_mgmt() with relevant NET_REQUEST_STATS_GET_* command.
34+
2735
config NET_STATISTICS_PERIODIC_OUTPUT
2836
bool "Simple periodic output"
2937
depends on NET_LOG

subsys/net/ip/net_stats.c

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
#include <kernel.h>
2222
#include <string.h>
23+
#include <errno.h>
2324
#include <net/net_core.h>
2425

2526
#include "net_stats.h"
@@ -135,3 +136,125 @@ void net_print_statistics(void)
135136
}
136137

137138
#endif /* CONFIG_NET_STATISTICS_PERIODIC_OUTPUT */
139+
140+
#if defined(CONFIG_NET_STATISTICS_USER_API)
141+
142+
static int net_stats_get(uint32_t mgmt_request, struct net_if *iface,
143+
void *data, size_t len)
144+
{
145+
size_t len_chk = 0;
146+
void *src = NULL;
147+
148+
ARG_UNUSED(iface);
149+
150+
switch (NET_MGMT_GET_COMMAND(mgmt_request)) {
151+
case NET_REQUEST_STATS_CMD_GET_ALL:
152+
len_chk = sizeof(struct net_stats);
153+
src = &net_stats;
154+
break;
155+
case NET_REQUEST_STATS_CMD_GET_PROCESSING_ERROR:
156+
len_chk = sizeof(net_stats_t);
157+
src = &net_stats.processing_error;
158+
break;
159+
case NET_REQUEST_STATS_CMD_GET_IP_ERRORS:
160+
len_chk = sizeof(struct net_stats_ip_errors);
161+
src = &net_stats.ip_errors;
162+
break;
163+
#if defined(CONFIG_NET_STATISTICS_IPV4)
164+
case NET_REQUEST_STATS_CMD_GET_IPV4:
165+
len_chk = sizeof(struct net_stats_ip);
166+
src = &net_stats.ipv4;
167+
break;
168+
#endif
169+
#if defined(CONFIG_NET_STATISTICS_IPV6)
170+
case NET_REQUEST_STATS_CMD_GET_IPV6:
171+
len_chk = sizeof(struct net_stats_ip);
172+
src = &net_stats.ipv6;
173+
break;
174+
#endif
175+
#if defined(CONFIG_NET_STATISTICS_IPV6_ND)
176+
case NET_REQUEST_STATS_CMD_GET_IPV6_ND:
177+
len_chk = sizeof(struct net_stats_ipv6_nd);
178+
src = &net_stats.ipv6_nd;
179+
break;
180+
#endif
181+
#if defined(CONFIG_NET_STATISTICS_ICMP)
182+
case NET_REQUEST_STATS_CMD_GET_ICMP:
183+
len_chk = sizeof(struct net_stats_icmp);
184+
src = &net_stats.icmp;
185+
break;
186+
#endif
187+
#if defined(CONFIG_NET_STATISTICS_UDP)
188+
case NET_REQUEST_STATS_CMD_GET_UDP:
189+
len_chk = sizeof(struct net_stats_udp);
190+
src = &net_stats.udp;
191+
break;
192+
#endif
193+
#if defined(CONFIG_NET_STATISTICS_TCP)
194+
case NET_REQUEST_STATS_CMD_GET_TCP:
195+
len_chk = sizeof(struct net_stats_tcp);
196+
src = &net_stats.tcp;
197+
break;
198+
#endif
199+
#if defined(CONFIG_NET_STATISTICS_RPL)
200+
case NET_REQUEST_STATS_CMD_GET_RPL:
201+
len_chk = sizeof(struct net_stats_rpl);
202+
src = &net_stats.rpl;
203+
break;
204+
#endif
205+
}
206+
207+
if (len != len_chk || !src) {
208+
return -EINVAL;
209+
}
210+
211+
memcpy(src, data, len);
212+
213+
return 0;
214+
}
215+
216+
NET_MGMT_REGISTER_REQUEST_HANDLER(NET_REQUEST_STATS_GET_ALL,
217+
net_stats_get);
218+
219+
NET_MGMT_REGISTER_REQUEST_HANDLER(NET_REQUEST_STATS_GET_PROCESSING_ERROR,
220+
net_stats_get);
221+
222+
NET_MGMT_REGISTER_REQUEST_HANDLER(NET_REQUEST_STATS_GET_IP_ERRORS,
223+
net_stats_get);
224+
225+
#if defined(CONFIG_NET_STATISTICS_IPV4)
226+
NET_MGMT_REGISTER_REQUEST_HANDLER(NET_REQUEST_STATS_GET_IPV4,
227+
net_stats_get);
228+
#endif
229+
230+
#if defined(CONFIG_NET_STATISTICS_IPV6)
231+
NET_MGMT_REGISTER_REQUEST_HANDLER(NET_REQUEST_STATS_GET_IPV6,
232+
net_stats_get);
233+
#endif
234+
235+
#if defined(CONFIG_NET_STATISTICS_IPV6_ND)
236+
NET_MGMT_REGISTER_REQUEST_HANDLER(NET_REQUEST_STATS_GET_IPV6_ND,
237+
net_stats_get);
238+
#endif
239+
240+
#if defined(CONFIG_NET_STATISTICS_ICMP)
241+
NET_MGMT_REGISTER_REQUEST_HANDLER(NET_REQUEST_STATS_GET_ICMP,
242+
net_stats_get);
243+
#endif
244+
245+
#if defined(CONFIG_NET_STATISTICS_UDP)
246+
NET_MGMT_REGISTER_REQUEST_HANDLER(NET_REQUEST_STATS_GET_UDP,
247+
net_stats_get);
248+
#endif
249+
250+
#if defined(CONFIG_NET_STATISTICS_TCP)
251+
NET_MGMT_REGISTER_REQUEST_HANDLER(NET_REQUEST_STATS_GET_TCP,
252+
net_stats_get);
253+
#endif
254+
255+
#if defined(CONFIG_NET_STATISTICS_RPL)
256+
NET_MGMT_REGISTER_REQUEST_HANDLER(NET_REQUEST_STATS_GET_RPL,
257+
net_stats_get);
258+
#endif
259+
260+
#endif /* CONFIG_NET_STATISTICS_USER_API */

0 commit comments

Comments
 (0)