1 /** @file
2  * @brief Network statistics
3  *
4  * Network statistics data. This should only be enabled when
5  * debugging as it consumes memory.
6  */
7 
8 /*
9  * Copyright (c) 2016 Intel Corporation
10  *
11  * SPDX-License-Identifier: Apache-2.0
12  */
13 
14 #ifndef ZEPHYR_INCLUDE_NET_NET_STATS_H_
15 #define ZEPHYR_INCLUDE_NET_NET_STATS_H_
16 
17 #include <zephyr/types.h>
18 #include <zephyr/net/net_core.h>
19 #include <zephyr/net/net_mgmt.h>
20 
21 #ifdef __cplusplus
22 extern "C" {
23 #endif
24 
25 /**
26  * @brief Network statistics library
27  * @defgroup net_stats Network Statistics Library
28  * @ingroup networking
29  * @{
30  */
31 
32 /**
33  * @typedef net_stats_t
34  * @brief Network statistics counter
35  */
36 typedef uint32_t net_stats_t;
37 
38 /**
39  * @brief Number of bytes sent and received.
40  */
41 struct net_stats_bytes {
42 	/** Number of bytes sent */
43 	net_stats_t sent;
44 	/** Number of bytes received */
45 	net_stats_t received;
46 };
47 
48 /**
49  * @brief Number of network packets sent and received.
50  */
51 struct net_stats_pkts {
52 	/** Number of packets sent */
53 	net_stats_t tx;
54 	/** Number of packets received */
55 	net_stats_t rx;
56 };
57 
58 /**
59  * @brief IP layer statistics
60  */
61 struct net_stats_ip {
62 	/** Number of received packets at the IP layer. */
63 	net_stats_t recv;
64 
65 	/** Number of sent packets at the IP layer. */
66 	net_stats_t sent;
67 
68 	/** Number of forwarded packets at the IP layer. */
69 	net_stats_t forwarded;
70 
71 	/** Number of dropped packets at the IP layer. */
72 	net_stats_t drop;
73 };
74 
75 /**
76  * @brief IP layer error statistics
77  */
78 struct net_stats_ip_errors {
79 	/** Number of packets dropped due to wrong IP version
80 	 * or header length.
81 	 */
82 	net_stats_t vhlerr;
83 
84 	 /** Number of packets dropped due to wrong IP length, high byte. */
85 	net_stats_t hblenerr;
86 
87 	/** Number of packets dropped due to wrong IP length, low byte. */
88 	net_stats_t lblenerr;
89 
90 	/** Number of packets dropped because they were IP fragments. */
91 	net_stats_t fragerr;
92 
93 	/** Number of packets dropped due to IP checksum errors. */
94 	net_stats_t chkerr;
95 
96 	/** Number of packets dropped because they were neither ICMP,
97 	 * UDP nor TCP.
98 	 */
99 	net_stats_t protoerr;
100 };
101 
102 /**
103  * @brief ICMP statistics
104  */
105 struct net_stats_icmp {
106 	/** Number of received ICMP packets. */
107 	net_stats_t recv;
108 
109 	/** Number of sent ICMP packets. */
110 	net_stats_t sent;
111 
112 	/** Number of dropped ICMP packets. */
113 	net_stats_t drop;
114 
115 	/** Number of ICMP packets with a wrong type. */
116 	net_stats_t typeerr;
117 
118 	/** Number of ICMP packets with a bad checksum. */
119 	net_stats_t chkerr;
120 };
121 
122 /**
123  * @brief TCP statistics
124  */
125 struct net_stats_tcp {
126 	/** Amount of received and sent TCP application data. */
127 	struct net_stats_bytes bytes;
128 
129 	/** Amount of retransmitted data. */
130 	net_stats_t resent;
131 
132 	/** Number of dropped packets at the TCP layer. */
133 	net_stats_t drop;
134 
135 	/** Number of received TCP segments. */
136 	net_stats_t recv;
137 
138 	/** Number of sent TCP segments. */
139 	net_stats_t sent;
140 
141 	/** Number of dropped TCP segments. */
142 	net_stats_t seg_drop;
143 
144 	/** Number of TCP segments with a bad checksum. */
145 	net_stats_t chkerr;
146 
147 	/** Number of received TCP segments with a bad ACK number. */
148 	net_stats_t ackerr;
149 
150 	/** Number of received bad TCP RST (reset) segments. */
151 	net_stats_t rsterr;
152 
153 	/** Number of received TCP RST (reset) segments. */
154 	net_stats_t rst;
155 
156 	/** Number of retransmitted TCP segments. */
157 	net_stats_t rexmit;
158 
159 	/** Number of dropped connection attempts because too few connections
160 	 * were available.
161 	 */
162 	net_stats_t conndrop;
163 
164 	/** Number of connection attempts for closed ports, triggering a RST. */
165 	net_stats_t connrst;
166 };
167 
168 /**
169  * @brief UDP statistics
170  */
171 struct net_stats_udp {
172 	/** Number of dropped UDP segments. */
173 	net_stats_t drop;
174 
175 	/** Number of received UDP segments. */
176 	net_stats_t recv;
177 
178 	/** Number of sent UDP segments. */
179 	net_stats_t sent;
180 
181 	/** Number of UDP segments with a bad checksum. */
182 	net_stats_t chkerr;
183 };
184 
185 /**
186  * @brief IPv6 neighbor discovery statistics
187  */
188 struct net_stats_ipv6_nd {
189 	net_stats_t drop;
190 	net_stats_t recv;
191 	net_stats_t sent;
192 };
193 
194 /**
195  * @brief IPv6 multicast listener daemon statistics
196  */
197 struct net_stats_ipv6_mld {
198 	/** Number of received IPv6 MLD queries */
199 	net_stats_t recv;
200 
201 	/** Number of sent IPv6 MLD reports */
202 	net_stats_t sent;
203 
204 	/** Number of dropped IPv6 MLD packets */
205 	net_stats_t drop;
206 };
207 
208 /**
209  * @brief IPv4 IGMP daemon statistics
210  */
211 struct net_stats_ipv4_igmp {
212 	/** Number of received IPv4 IGMP queries */
213 	net_stats_t recv;
214 
215 	/** Number of sent IPv4 IGMP reports */
216 	net_stats_t sent;
217 
218 	/** Number of dropped IPv4 IGMP packets */
219 	net_stats_t drop;
220 };
221 
222 /**
223  * @brief Network packet transfer times for calculating average TX time
224  */
225 struct net_stats_tx_time {
226 	uint64_t sum;
227 	net_stats_t count;
228 };
229 
230 /**
231  * @brief Network packet receive times for calculating average RX time
232  */
233 struct net_stats_rx_time {
234 	uint64_t sum;
235 	net_stats_t count;
236 };
237 
238 #if NET_TC_TX_COUNT == 0
239 #define NET_TC_TX_STATS_COUNT 1
240 #else
241 #define NET_TC_TX_STATS_COUNT NET_TC_TX_COUNT
242 #endif
243 
244 #if NET_TC_RX_COUNT == 0
245 #define NET_TC_RX_STATS_COUNT 1
246 #else
247 #define NET_TC_RX_STATS_COUNT NET_TC_RX_COUNT
248 #endif
249 
250 /**
251  * @brief Traffic class statistics
252  */
253 struct net_stats_tc {
254 	struct {
255 		struct net_stats_tx_time tx_time;
256 #if defined(CONFIG_NET_PKT_TXTIME_STATS_DETAIL)
257 		struct net_stats_tx_time
258 				tx_time_detail[NET_PKT_DETAIL_STATS_COUNT];
259 #endif
260 		net_stats_t pkts;
261 		net_stats_t bytes;
262 		uint8_t priority;
263 	} sent[NET_TC_TX_STATS_COUNT];
264 
265 	struct {
266 		struct net_stats_rx_time rx_time;
267 #if defined(CONFIG_NET_PKT_RXTIME_STATS_DETAIL)
268 		struct net_stats_rx_time
269 				rx_time_detail[NET_PKT_DETAIL_STATS_COUNT];
270 #endif
271 		net_stats_t pkts;
272 		net_stats_t bytes;
273 		uint8_t priority;
274 	} recv[NET_TC_RX_STATS_COUNT];
275 };
276 
277 
278 /**
279  * @brief Power management statistics
280  */
281 struct net_stats_pm {
282 	uint64_t overall_suspend_time;
283 	net_stats_t suspend_count;
284 	uint32_t last_suspend_time;
285 	uint32_t start_time;
286 };
287 
288 
289 /**
290  * @brief All network statistics in one struct.
291  */
292 struct net_stats {
293 	/** Count of malformed packets or packets we do not have handler for */
294 	net_stats_t processing_error;
295 
296 	/**
297 	 * This calculates amount of data transferred through all the
298 	 * network interfaces.
299 	 */
300 	struct net_stats_bytes bytes;
301 
302 	/** IP layer errors */
303 	struct net_stats_ip_errors ip_errors;
304 
305 #if defined(CONFIG_NET_STATISTICS_IPV6)
306 	/** IPv6 statistics */
307 	struct net_stats_ip ipv6;
308 #endif
309 
310 #if defined(CONFIG_NET_STATISTICS_IPV4)
311 	/** IPv4 statistics */
312 	struct net_stats_ip ipv4;
313 #endif
314 
315 #if defined(CONFIG_NET_STATISTICS_ICMP)
316 	/** ICMP statistics */
317 	struct net_stats_icmp icmp;
318 #endif
319 
320 #if defined(CONFIG_NET_STATISTICS_TCP)
321 	/** TCP statistics */
322 	struct net_stats_tcp tcp;
323 #endif
324 
325 #if defined(CONFIG_NET_STATISTICS_UDP)
326 	/** UDP statistics */
327 	struct net_stats_udp udp;
328 #endif
329 
330 #if defined(CONFIG_NET_STATISTICS_IPV6_ND)
331 	/** IPv6 neighbor discovery statistics */
332 	struct net_stats_ipv6_nd ipv6_nd;
333 #endif
334 
335 #if defined(CONFIG_NET_STATISTICS_MLD)
336 	/** IPv6 MLD statistics */
337 	struct net_stats_ipv6_mld ipv6_mld;
338 #endif
339 
340 #if defined(CONFIG_NET_STATISTICS_IGMP)
341 	/** IPv4 IGMP statistics */
342 	struct net_stats_ipv4_igmp ipv4_igmp;
343 #endif
344 
345 #if NET_TC_COUNT > 1
346 	/** Traffic class statistics */
347 	struct net_stats_tc tc;
348 #endif
349 
350 #if defined(CONFIG_NET_PKT_TXTIME_STATS)
351 	/** Network packet TX time statistics */
352 	struct net_stats_tx_time tx_time;
353 #endif
354 
355 #if defined(CONFIG_NET_PKT_RXTIME_STATS)
356 	/** Network packet RX time statistics */
357 	struct net_stats_rx_time rx_time;
358 #endif
359 
360 #if defined(CONFIG_NET_PKT_TXTIME_STATS_DETAIL)
361 	/** Network packet TX time detail statistics */
362 	struct net_stats_tx_time tx_time_detail[NET_PKT_DETAIL_STATS_COUNT];
363 #endif
364 #if defined(CONFIG_NET_PKT_RXTIME_STATS_DETAIL)
365 	/** Network packet RX time detail statistics */
366 	struct net_stats_rx_time rx_time_detail[NET_PKT_DETAIL_STATS_COUNT];
367 #endif
368 
369 #if defined(CONFIG_NET_STATISTICS_POWER_MANAGEMENT)
370 	struct net_stats_pm pm;
371 #endif
372 };
373 
374 /**
375  * @brief Ethernet error statistics
376  */
377 struct net_stats_eth_errors {
378 	net_stats_t rx_length_errors;
379 	net_stats_t rx_over_errors;
380 	net_stats_t rx_crc_errors;
381 	net_stats_t rx_frame_errors;
382 	net_stats_t rx_no_buffer_count;
383 	net_stats_t rx_missed_errors;
384 	net_stats_t rx_long_length_errors;
385 	net_stats_t rx_short_length_errors;
386 	net_stats_t rx_align_errors;
387 	net_stats_t rx_dma_failed;
388 	net_stats_t rx_buf_alloc_failed;
389 
390 	net_stats_t tx_aborted_errors;
391 	net_stats_t tx_carrier_errors;
392 	net_stats_t tx_fifo_errors;
393 	net_stats_t tx_heartbeat_errors;
394 	net_stats_t tx_window_errors;
395 	net_stats_t tx_dma_failed;
396 
397 	net_stats_t uncorr_ecc_errors;
398 	net_stats_t corr_ecc_errors;
399 };
400 
401 /**
402  * @brief Ethernet flow control statistics
403  */
404 struct net_stats_eth_flow {
405 	net_stats_t rx_flow_control_xon;
406 	net_stats_t rx_flow_control_xoff;
407 	net_stats_t tx_flow_control_xon;
408 	net_stats_t tx_flow_control_xoff;
409 };
410 
411 /**
412  * @brief Ethernet checksum statistics
413  */
414 struct net_stats_eth_csum {
415 	net_stats_t rx_csum_offload_good;
416 	net_stats_t rx_csum_offload_errors;
417 };
418 
419 /**
420  * @brief Ethernet hardware timestamp statistics
421  */
422 struct net_stats_eth_hw_timestamp {
423 	net_stats_t rx_hwtstamp_cleared;
424 	net_stats_t tx_hwtstamp_timeouts;
425 	net_stats_t tx_hwtstamp_skipped;
426 };
427 
428 #ifdef CONFIG_NET_STATISTICS_ETHERNET_VENDOR
429 /**
430  * @brief Ethernet vendor specific statistics
431  */
432 struct net_stats_eth_vendor {
433 	const char * const key;
434 	uint32_t value;
435 };
436 #endif
437 
438 /**
439  * @brief All Ethernet specific statistics
440  */
441 struct net_stats_eth {
442 	struct net_stats_bytes bytes;
443 	struct net_stats_pkts pkts;
444 	struct net_stats_pkts broadcast;
445 	struct net_stats_pkts multicast;
446 	struct net_stats_pkts errors;
447 	struct net_stats_eth_errors error_details;
448 	struct net_stats_eth_flow flow_control;
449 	struct net_stats_eth_csum csum;
450 	struct net_stats_eth_hw_timestamp hw_timestamp;
451 	net_stats_t collisions;
452 	net_stats_t tx_dropped;
453 	net_stats_t tx_timeout_count;
454 	net_stats_t tx_restart_queue;
455 	net_stats_t unknown_protocol;
456 #ifdef CONFIG_NET_STATISTICS_ETHERNET_VENDOR
457 	/** Array is terminated with an entry containing a NULL key */
458 	struct net_stats_eth_vendor *vendor;
459 #endif
460 };
461 
462 /**
463  * @brief All PPP specific statistics
464  */
465 struct net_stats_ppp {
466 	struct net_stats_bytes bytes;
467 	struct net_stats_pkts pkts;
468 
469 	/** Number of received and dropped PPP frames. */
470 	net_stats_t drop;
471 
472 	/** Number of received PPP frames with a bad checksum. */
473 	net_stats_t chkerr;
474 };
475 
476 /**
477  * @brief All Wi-Fi management statistics
478  */
479 struct net_stats_sta_mgmt {
480 	/** Number of received beacons */
481 	net_stats_t beacons_rx;
482 
483 	/** Number of missed beacons */
484 	net_stats_t beacons_miss;
485 };
486 
487 /**
488  * @brief All Wi-Fi specific statistics
489  */
490 struct net_stats_wifi {
491 	struct net_stats_sta_mgmt sta_mgmt;
492 	struct net_stats_bytes bytes;
493 	struct net_stats_pkts pkts;
494 	struct net_stats_pkts broadcast;
495 	struct net_stats_pkts multicast;
496 	struct net_stats_pkts errors;
497 };
498 
499 #if defined(CONFIG_NET_STATISTICS_USER_API)
500 /* Management part definitions */
501 
502 #define _NET_STATS_LAYER	NET_MGMT_LAYER_L3
503 #define _NET_STATS_CODE		0x101
504 #define _NET_STATS_BASE		(NET_MGMT_LAYER(_NET_STATS_LAYER) |	\
505 				 NET_MGMT_LAYER_CODE(_NET_STATS_CODE))
506 
507 enum net_request_stats_cmd {
508 	NET_REQUEST_STATS_CMD_GET_ALL = 1,
509 	NET_REQUEST_STATS_CMD_GET_PROCESSING_ERROR,
510 	NET_REQUEST_STATS_CMD_GET_BYTES,
511 	NET_REQUEST_STATS_CMD_GET_IP_ERRORS,
512 	NET_REQUEST_STATS_CMD_GET_IPV4,
513 	NET_REQUEST_STATS_CMD_GET_IPV6,
514 	NET_REQUEST_STATS_CMD_GET_IPV6_ND,
515 	NET_REQUEST_STATS_CMD_GET_ICMP,
516 	NET_REQUEST_STATS_CMD_GET_UDP,
517 	NET_REQUEST_STATS_CMD_GET_TCP,
518 	NET_REQUEST_STATS_CMD_GET_ETHERNET,
519 	NET_REQUEST_STATS_CMD_GET_PPP,
520 	NET_REQUEST_STATS_CMD_GET_PM,
521 	NET_REQUEST_STATS_CMD_GET_WIFI,
522 };
523 
524 #define NET_REQUEST_STATS_GET_ALL				\
525 	(_NET_STATS_BASE | NET_REQUEST_STATS_CMD_GET_ALL)
526 
527 NET_MGMT_DEFINE_REQUEST_HANDLER(NET_REQUEST_STATS_GET_ALL);
528 
529 #define NET_REQUEST_STATS_GET_PROCESSING_ERROR				\
530 	(_NET_STATS_BASE | NET_REQUEST_STATS_CMD_GET_PROCESSING_ERROR)
531 
532 NET_MGMT_DEFINE_REQUEST_HANDLER(NET_REQUEST_STATS_GET_PROCESSING_ERROR);
533 
534 #define NET_REQUEST_STATS_GET_BYTES				\
535 	(_NET_STATS_BASE | NET_REQUEST_STATS_CMD_GET_BYTES)
536 
537 NET_MGMT_DEFINE_REQUEST_HANDLER(NET_REQUEST_STATS_GET_BYTES);
538 
539 #define NET_REQUEST_STATS_GET_IP_ERRORS				\
540 	(_NET_STATS_BASE | NET_REQUEST_STATS_CMD_GET_IP_ERRORS)
541 
542 NET_MGMT_DEFINE_REQUEST_HANDLER(NET_REQUEST_STATS_GET_IP_ERRORS);
543 
544 #if defined(CONFIG_NET_STATISTICS_IPV4)
545 #define NET_REQUEST_STATS_GET_IPV4				\
546 	(_NET_STATS_BASE | NET_REQUEST_STATS_CMD_GET_IPV4)
547 
548 NET_MGMT_DEFINE_REQUEST_HANDLER(NET_REQUEST_STATS_GET_IPV4);
549 #endif /* CONFIG_NET_STATISTICS_IPV4 */
550 
551 #if defined(CONFIG_NET_STATISTICS_IPV6)
552 #define NET_REQUEST_STATS_GET_IPV6				\
553 	(_NET_STATS_BASE | NET_REQUEST_STATS_CMD_GET_IPV6)
554 
555 NET_MGMT_DEFINE_REQUEST_HANDLER(NET_REQUEST_STATS_GET_IPV6);
556 #endif /* CONFIG_NET_STATISTICS_IPV6 */
557 
558 #if defined(CONFIG_NET_STATISTICS_IPV6_ND)
559 #define NET_REQUEST_STATS_GET_IPV6_ND				\
560 	(_NET_STATS_BASE | NET_REQUEST_STATS_CMD_GET_IPV6_ND)
561 
562 NET_MGMT_DEFINE_REQUEST_HANDLER(NET_REQUEST_STATS_GET_IPV6_ND);
563 #endif /* CONFIG_NET_STATISTICS_IPV6_ND */
564 
565 #if defined(CONFIG_NET_STATISTICS_ICMP)
566 #define NET_REQUEST_STATS_GET_ICMP				\
567 	(_NET_STATS_BASE | NET_REQUEST_STATS_CMD_GET_ICMP)
568 
569 NET_MGMT_DEFINE_REQUEST_HANDLER(NET_REQUEST_STATS_GET_ICMP);
570 #endif /* CONFIG_NET_STATISTICS_ICMP */
571 
572 #if defined(CONFIG_NET_STATISTICS_UDP)
573 #define NET_REQUEST_STATS_GET_UDP				\
574 	(_NET_STATS_BASE | NET_REQUEST_STATS_CMD_GET_UDP)
575 
576 NET_MGMT_DEFINE_REQUEST_HANDLER(NET_REQUEST_STATS_GET_UDP);
577 #endif /* CONFIG_NET_STATISTICS_UDP */
578 
579 #if defined(CONFIG_NET_STATISTICS_TCP)
580 #define NET_REQUEST_STATS_GET_TCP				\
581 	(_NET_STATS_BASE | NET_REQUEST_STATS_CMD_GET_TCP)
582 
583 NET_MGMT_DEFINE_REQUEST_HANDLER(NET_REQUEST_STATS_GET_TCP);
584 #endif /* CONFIG_NET_STATISTICS_TCP */
585 
586 #if defined(CONFIG_NET_STATISTICS_ETHERNET)
587 #define NET_REQUEST_STATS_GET_ETHERNET				\
588 	(_NET_STATS_BASE | NET_REQUEST_STATS_CMD_GET_ETHERNET)
589 
590 NET_MGMT_DEFINE_REQUEST_HANDLER(NET_REQUEST_STATS_GET_ETHERNET);
591 #endif /* CONFIG_NET_STATISTICS_ETHERNET */
592 
593 #if defined(CONFIG_NET_STATISTICS_PPP)
594 #define NET_REQUEST_STATS_GET_PPP				\
595 	(_NET_STATS_BASE | NET_REQUEST_STATS_CMD_GET_PPP)
596 
597 NET_MGMT_DEFINE_REQUEST_HANDLER(NET_REQUEST_STATS_GET_PPP);
598 #endif /* CONFIG_NET_STATISTICS_PPP */
599 
600 #endif /* CONFIG_NET_STATISTICS_USER_API */
601 
602 #if defined(CONFIG_NET_STATISTICS_POWER_MANAGEMENT)
603 #define NET_REQUEST_STATS_GET_PM				\
604 	(_NET_STATS_BASE | NET_REQUEST_STATS_CMD_GET_PM)
605 
606 NET_MGMT_DEFINE_REQUEST_HANDLER(NET_REQUEST_STATS_GET_PM);
607 #endif /* CONFIG_NET_STATISTICS_POWER_MANAGEMENT */
608 
609 #if defined(CONFIG_NET_STATISTICS_WIFI)
610 #define NET_REQUEST_STATS_GET_WIFI				\
611 	(_NET_STATS_BASE | NET_REQUEST_STATS_CMD_GET_WIFI)
612 
613 NET_MGMT_DEFINE_REQUEST_HANDLER(NET_REQUEST_STATS_GET_WIFI);
614 #endif /* CONFIG_NET_STATISTICS_WIFI */
615 
616 /**
617  * @}
618  */
619 
620 #ifdef __cplusplus
621 }
622 #endif
623 
624 #endif /* ZEPHYR_INCLUDE_NET_NET_STATS_H_ */
625