1 /**
2  * @file
3  * Statistics API (to be used from TCPIP thread)
4  */
5 
6 /*
7  * Copyright (c) 2001-2004 Swedish Institute of Computer Science.
8  * All rights reserved.
9  *
10  * Redistribution and use in source and binary forms, with or without modification,
11  * are permitted provided that the following conditions are met:
12  *
13  * 1. Redistributions of source code must retain the above copyright notice,
14  *    this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright notice,
16  *    this list of conditions and the following disclaimer in the documentation
17  *    and/or other materials provided with the distribution.
18  * 3. The name of the author may not be used to endorse or promote products
19  *    derived from this software without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
22  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
23  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
24  * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
26  * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
29  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
30  * OF SUCH DAMAGE.
31  *
32  * This file is part of the lwIP TCP/IP stack.
33  *
34  * Author: Adam Dunkels <adam@sics.se>
35  *
36  */
37 #ifndef LWIP_HDR_STATS_H
38 #define LWIP_HDR_STATS_H
39 
40 #include "lwip/opt.h"
41 
42 #include "lwip/mem.h"
43 #include "lwip/memp.h"
44 
45 #ifdef __cplusplus
46 extern "C" {
47 #endif
48 
49 #if LWIP_STATS
50 
51 #ifndef LWIP_STATS_LARGE
52 #define LWIP_STATS_LARGE 0
53 #endif
54 
55 #if LWIP_STATS_LARGE
56 #define STAT_COUNTER     u32_t
57 #define STAT_COUNTER_F   U32_F
58 #else
59 #define STAT_COUNTER     u16_t
60 #define STAT_COUNTER_F   U16_F
61 #endif
62 
63 /** Protocol related stats */
64 struct stats_proto {
65   STAT_COUNTER xmit;             /* Transmitted packets. */
66   STAT_COUNTER recv;             /* Received packets. */
67   STAT_COUNTER fw;               /* Forwarded packets. */
68   STAT_COUNTER drop;             /* Dropped packets. */
69   STAT_COUNTER chkerr;           /* Checksum error. */
70   STAT_COUNTER lenerr;           /* Invalid length error. */
71   STAT_COUNTER memerr;           /* Out of memory error. */
72   STAT_COUNTER rterr;            /* Routing error. */
73   STAT_COUNTER proterr;          /* Protocol error. */
74   STAT_COUNTER opterr;           /* Error in options. */
75   STAT_COUNTER err;              /* Misc error. */
76   STAT_COUNTER cachehit;
77 };
78 
79 /** IGMP stats */
80 struct stats_igmp {
81   STAT_COUNTER xmit;             /* Transmitted packets. */
82   STAT_COUNTER recv;             /* Received packets. */
83   STAT_COUNTER drop;             /* Dropped packets. */
84   STAT_COUNTER chkerr;           /* Checksum error. */
85   STAT_COUNTER lenerr;           /* Invalid length error. */
86   STAT_COUNTER memerr;           /* Out of memory error. */
87   STAT_COUNTER proterr;          /* Protocol error. */
88   STAT_COUNTER rx_v1;            /* Received v1 frames. */
89   STAT_COUNTER rx_group;         /* Received group-specific queries. */
90   STAT_COUNTER rx_general;       /* Received general queries. */
91   STAT_COUNTER rx_report;        /* Received reports. */
92   STAT_COUNTER tx_join;          /* Sent joins. */
93   STAT_COUNTER tx_leave;         /* Sent leaves. */
94   STAT_COUNTER tx_report;        /* Sent reports. */
95 };
96 
97 /** Memory stats */
98 struct stats_mem {
99 #if defined(LWIP_DEBUG) || LWIP_STATS_DISPLAY
100   const char *name;
101 #endif /* defined(LWIP_DEBUG) || LWIP_STATS_DISPLAY */
102   STAT_COUNTER err;
103   mem_size_t avail;
104   mem_size_t used;
105   mem_size_t max;
106   STAT_COUNTER illegal;
107 };
108 
109 /** System element stats */
110 struct stats_syselem {
111   STAT_COUNTER used;
112   STAT_COUNTER max;
113   STAT_COUNTER err;
114 };
115 
116 /** System stats */
117 struct stats_sys {
118   struct stats_syselem sem;
119   struct stats_syselem mutex;
120   struct stats_syselem mbox;
121 };
122 
123 /** SNMP MIB2 stats */
124 struct stats_mib2 {
125   /* IP */
126   u32_t ipinhdrerrors;
127   u32_t ipinaddrerrors;
128   u32_t ipinunknownprotos;
129   u32_t ipindiscards;
130   u32_t ipindelivers;
131   u32_t ipoutrequests;
132   u32_t ipoutdiscards;
133   u32_t ipoutnoroutes;
134   u32_t ipreasmoks;
135   u32_t ipreasmfails;
136   u32_t ipfragoks;
137   u32_t ipfragfails;
138   u32_t ipfragcreates;
139   u32_t ipreasmreqds;
140   u32_t ipforwdatagrams;
141   u32_t ipinreceives;
142 
143   /* TCP */
144   u32_t tcpactiveopens;
145   u32_t tcppassiveopens;
146   u32_t tcpattemptfails;
147   u32_t tcpestabresets;
148   u32_t tcpoutsegs;
149   u32_t tcpretranssegs;
150   u32_t tcpinsegs;
151   u32_t tcpinerrs;
152   u32_t tcpoutrsts;
153 
154   /* UDP */
155   u32_t udpindatagrams;
156   u32_t udpnoports;
157   u32_t udpinerrors;
158   u32_t udpoutdatagrams;
159 
160   /* ICMP */
161   u32_t icmpinmsgs;
162   u32_t icmpinerrors;
163   u32_t icmpindestunreachs;
164   u32_t icmpintimeexcds;
165   u32_t icmpinparmprobs;
166   u32_t icmpinsrcquenchs;
167   u32_t icmpinredirects;
168   u32_t icmpinechos;
169   u32_t icmpinechoreps;
170   u32_t icmpintimestamps;
171   u32_t icmpintimestampreps;
172   u32_t icmpinaddrmasks;
173   u32_t icmpinaddrmaskreps;
174   u32_t icmpoutmsgs;
175   u32_t icmpouterrors;
176   u32_t icmpoutdestunreachs;
177   u32_t icmpouttimeexcds;
178   u32_t icmpoutechos; /* can be incremented by user application ('ping') */
179   u32_t icmpoutechoreps;
180 };
181 
182 /**
183  * @ingroup netif_mib2
184  * SNMP MIB2 interface stats
185  */
186 struct stats_mib2_netif_ctrs {
187   /** The total number of octets received on the interface, including framing characters */
188   u32_t ifinoctets;
189   /** The number of packets, delivered by this sub-layer to a higher (sub-)layer, which were
190    * not addressed to a multicast or broadcast address at this sub-layer */
191   u32_t ifinucastpkts;
192   /** The number of packets, delivered by this sub-layer to a higher (sub-)layer, which were
193    * addressed to a multicast or broadcast address at this sub-layer */
194   u32_t ifinnucastpkts;
195   /** The number of inbound packets which were chosen to be discarded even though no errors had
196    * been detected to prevent their being deliverable to a higher-layer protocol. One possible
197    * reason for discarding such a packet could be to free up buffer space */
198   u32_t ifindiscards;
199   /** For packet-oriented interfaces, the number of inbound packets that contained errors
200    * preventing them from being deliverable to a higher-layer protocol.  For character-
201    * oriented or fixed-length interfaces, the number of inbound transmission units that
202    * contained errors preventing them from being deliverable to a higher-layer protocol. */
203   u32_t ifinerrors;
204   /** For packet-oriented interfaces, the number of packets received via the interface which
205    * were discarded because of an unknown or unsupported protocol.  For character-oriented
206    * or fixed-length interfaces that support protocol multiplexing the number of transmission
207    * units received via the interface which were discarded because of an unknown or unsupported
208    * protocol. For any interface that does not support protocol multiplexing, this counter will
209    * always be 0 */
210   u32_t ifinunknownprotos;
211   /** The total number of octets transmitted out of the interface, including framing characters. */
212   u32_t ifoutoctets;
213   /** The total number of packets that higher-level protocols requested be transmitted, and
214    * which were not addressed to a multicast or broadcast address at this sub-layer, including
215    * those that were discarded or not sent. */
216   u32_t ifoutucastpkts;
217   /** The total number of packets that higher-level protocols requested be transmitted, and which
218    * were addressed to a multicast or broadcast address at this sub-layer, including
219    * those that were discarded or not sent. */
220   u32_t ifoutnucastpkts;
221   /** The number of outbound packets which were chosen to be discarded even though no errors had
222    * been detected to prevent their being transmitted.  One possible reason for discarding
223    * such a packet could be to free up buffer space. */
224   u32_t ifoutdiscards;
225   /** For packet-oriented interfaces, the number of outbound packets that could not be transmitted
226    * because of errors. For character-oriented or fixed-length interfaces, the number of outbound
227    * transmission units that could not be transmitted because of errors. */
228   u32_t ifouterrors;
229 };
230 
231 /** lwIP stats container */
232 struct stats_ {
233 #if LINK_STATS
234   /** Link level */
235   struct stats_proto link;
236 #endif
237 #if ETHARP_STATS
238   /** ARP */
239   struct stats_proto etharp;
240 #endif
241 #if IPFRAG_STATS
242   /** Fragmentation */
243   struct stats_proto ip_frag;
244 #endif
245 #if IP_STATS
246   /** IP */
247   struct stats_proto ip;
248 #endif
249 #if ICMP_STATS
250   /** ICMP */
251   struct stats_proto icmp;
252 #endif
253 #if IGMP_STATS
254   /** IGMP */
255   struct stats_igmp igmp;
256 #endif
257 #if UDP_STATS
258   /** UDP */
259   struct stats_proto udp;
260 #endif
261 #if TCP_STATS
262   /** TCP */
263   struct stats_proto tcp;
264 #endif
265 #if MEM_STATS
266   /** Heap */
267   struct stats_mem mem;
268 #endif
269 #if MEMP_STATS
270   /** Internal memory pools */
271   struct stats_mem *memp[MEMP_MAX];
272 #endif
273 #if SYS_STATS
274   /** System */
275   struct stats_sys sys;
276 #endif
277 #if IP6_STATS
278   /** IPv6 */
279   struct stats_proto ip6;
280 #endif
281 #if ICMP6_STATS
282   /** ICMP6 */
283   struct stats_proto icmp6;
284 #endif
285 #if IP6_FRAG_STATS
286   /** IPv6 fragmentation */
287   struct stats_proto ip6_frag;
288 #endif
289 #if MLD6_STATS
290   /** Multicast listener discovery */
291   struct stats_igmp mld6;
292 #endif
293 #if ND6_STATS
294   /** Neighbor discovery */
295   struct stats_proto nd6;
296 #endif
297 #if MIB2_STATS
298   /** SNMP MIB2 */
299   struct stats_mib2 mib2;
300 #endif
301 };
302 
303 /** Global variable containing lwIP internal statistics. Add this to your debugger's watchlist. */
304 extern struct stats_ lwip_stats;
305 
306 /** Init statistics */
307 void stats_init(void);
308 
309 #define STATS_INC(x) ++lwip_stats.x
310 #define STATS_DEC(x) --lwip_stats.x
311 #define STATS_INC_USED(x, y, type) do { lwip_stats.x.used = (type)(lwip_stats.x.used + y); \
312                                 if (lwip_stats.x.max < lwip_stats.x.used) { \
313                                     lwip_stats.x.max = lwip_stats.x.used; \
314                                 } \
315                              } while(0)
316 #define STATS_GET(x) lwip_stats.x
317 #else /* LWIP_STATS */
318 #define stats_init()
319 #define STATS_INC(x)
320 #define STATS_DEC(x)
321 #define STATS_INC_USED(x, y, type)
322 #endif /* LWIP_STATS */
323 
324 #if TCP_STATS
325 #define TCP_STATS_INC(x) STATS_INC(x)
326 #define TCP_STATS_DISPLAY() stats_display_proto(&lwip_stats.tcp, "TCP")
327 #else
328 #define TCP_STATS_INC(x)
329 #define TCP_STATS_DISPLAY()
330 #endif
331 
332 #if UDP_STATS
333 #define UDP_STATS_INC(x) STATS_INC(x)
334 #define UDP_STATS_DISPLAY() stats_display_proto(&lwip_stats.udp, "UDP")
335 #else
336 #define UDP_STATS_INC(x)
337 #define UDP_STATS_DISPLAY()
338 #endif
339 
340 #if ICMP_STATS
341 #define ICMP_STATS_INC(x) STATS_INC(x)
342 #define ICMP_STATS_DISPLAY() stats_display_proto(&lwip_stats.icmp, "ICMP")
343 #else
344 #define ICMP_STATS_INC(x)
345 #define ICMP_STATS_DISPLAY()
346 #endif
347 
348 #if IGMP_STATS
349 #define IGMP_STATS_INC(x) STATS_INC(x)
350 #define IGMP_STATS_DISPLAY() stats_display_igmp(&lwip_stats.igmp, "IGMP")
351 #else
352 #define IGMP_STATS_INC(x)
353 #define IGMP_STATS_DISPLAY()
354 #endif
355 
356 #if IP_STATS
357 #define IP_STATS_INC(x) STATS_INC(x)
358 #define IP_STATS_DISPLAY() stats_display_proto(&lwip_stats.ip, "IP")
359 #else
360 #define IP_STATS_INC(x)
361 #define IP_STATS_DISPLAY()
362 #endif
363 
364 #if IPFRAG_STATS
365 #define IPFRAG_STATS_INC(x) STATS_INC(x)
366 #define IPFRAG_STATS_DISPLAY() stats_display_proto(&lwip_stats.ip_frag, "IP_FRAG")
367 #else
368 #define IPFRAG_STATS_INC(x)
369 #define IPFRAG_STATS_DISPLAY()
370 #endif
371 
372 #if ETHARP_STATS
373 #define ETHARP_STATS_INC(x) STATS_INC(x)
374 #define ETHARP_STATS_DISPLAY() stats_display_proto(&lwip_stats.etharp, "ETHARP")
375 #else
376 #define ETHARP_STATS_INC(x)
377 #define ETHARP_STATS_DISPLAY()
378 #endif
379 
380 #if LINK_STATS
381 #define LINK_STATS_INC(x) STATS_INC(x)
382 #define LINK_STATS_DISPLAY() stats_display_proto(&lwip_stats.link, "LINK")
383 #else
384 #define LINK_STATS_INC(x)
385 #define LINK_STATS_DISPLAY()
386 #endif
387 
388 #if MEM_STATS
389 #define MEM_STATS_AVAIL(x, y) lwip_stats.mem.x = y
390 #define MEM_STATS_INC(x) STATS_INC(mem.x)
391 #define MEM_STATS_INC_USED(x, y) STATS_INC_USED(mem, y, mem_size_t)
392 #define MEM_STATS_DEC_USED(x, y) lwip_stats.mem.x = (mem_size_t)((lwip_stats.mem.x) - (y))
393 #define MEM_STATS_DISPLAY() stats_display_mem(&lwip_stats.mem, "HEAP")
394 #else
395 #define MEM_STATS_AVAIL(x, y)
396 #define MEM_STATS_INC(x)
397 #define MEM_STATS_INC_USED(x, y)
398 #define MEM_STATS_DEC_USED(x, y)
399 #define MEM_STATS_DISPLAY()
400 #endif
401 
402  #if MEMP_STATS
403 #define MEMP_STATS_DEC(x, i) STATS_DEC(memp[i]->x)
404 #define MEMP_STATS_DISPLAY(i) stats_display_memp(lwip_stats.memp[i], i)
405 #define MEMP_STATS_GET(x, i) STATS_GET(memp[i]->x)
406  #else
407 #define MEMP_STATS_DEC(x, i)
408 #define MEMP_STATS_DISPLAY(i)
409 #define MEMP_STATS_GET(x, i) 0
410 #endif
411 
412 #if SYS_STATS
413 #define SYS_STATS_INC(x) STATS_INC(sys.x)
414 #define SYS_STATS_DEC(x) STATS_DEC(sys.x)
415 #define SYS_STATS_INC_USED(x) STATS_INC_USED(sys.x, 1, STAT_COUNTER)
416 #define SYS_STATS_DISPLAY() stats_display_sys(&lwip_stats.sys)
417 #else
418 #define SYS_STATS_INC(x)
419 #define SYS_STATS_DEC(x)
420 #define SYS_STATS_INC_USED(x)
421 #define SYS_STATS_DISPLAY()
422 #endif
423 
424 #if IP6_STATS
425 #define IP6_STATS_INC(x) STATS_INC(x)
426 #define IP6_STATS_DISPLAY() stats_display_proto(&lwip_stats.ip6, "IPv6")
427 #else
428 #define IP6_STATS_INC(x)
429 #define IP6_STATS_DISPLAY()
430 #endif
431 
432 #if ICMP6_STATS
433 #define ICMP6_STATS_INC(x) STATS_INC(x)
434 #define ICMP6_STATS_DISPLAY() stats_display_proto(&lwip_stats.icmp6, "ICMPv6")
435 #else
436 #define ICMP6_STATS_INC(x)
437 #define ICMP6_STATS_DISPLAY()
438 #endif
439 
440 #if IP6_FRAG_STATS
441 #define IP6_FRAG_STATS_INC(x) STATS_INC(x)
442 #define IP6_FRAG_STATS_DISPLAY() stats_display_proto(&lwip_stats.ip6_frag, "IPv6 FRAG")
443 #else
444 #define IP6_FRAG_STATS_INC(x)
445 #define IP6_FRAG_STATS_DISPLAY()
446 #endif
447 
448 #if MLD6_STATS
449 #define MLD6_STATS_INC(x) STATS_INC(x)
450 #define MLD6_STATS_DISPLAY() stats_display_igmp(&lwip_stats.mld6, "MLDv1")
451 #else
452 #define MLD6_STATS_INC(x)
453 #define MLD6_STATS_DISPLAY()
454 #endif
455 
456 #if ND6_STATS
457 #define ND6_STATS_INC(x) STATS_INC(x)
458 #define ND6_STATS_DISPLAY() stats_display_proto(&lwip_stats.nd6, "ND")
459 #else
460 #define ND6_STATS_INC(x)
461 #define ND6_STATS_DISPLAY()
462 #endif
463 
464 #if MIB2_STATS
465 #define MIB2_STATS_INC(x) STATS_INC(x)
466 #else
467 #define MIB2_STATS_INC(x)
468 #endif
469 
470 /* Display of statistics */
471 #if LWIP_STATS_DISPLAY
472 void stats_display(void);
473 void stats_display_proto(struct stats_proto *proto, const char *name);
474 void stats_display_igmp(struct stats_igmp *igmp, const char *name);
475 void stats_display_mem(struct stats_mem *mem, const char *name);
476 void stats_display_memp(struct stats_mem *mem, int index);
477 void stats_display_sys(struct stats_sys *sys);
478 #else /* LWIP_STATS_DISPLAY */
479 #define stats_display()
480 #define stats_display_proto(proto, name)
481 #define stats_display_igmp(igmp, name)
482 #define stats_display_mem(mem, name)
483 #define stats_display_memp(mem, index)
484 #define stats_display_sys(sys)
485 #endif /* LWIP_STATS_DISPLAY */
486 
487 #ifdef __cplusplus
488 }
489 #endif
490 
491 #endif /* LWIP_HDR_STATS_H */
492