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 /* IPv4 */ 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 /* IPv6 */ 144 u32_t ip6reasmoks; 145 146 /* TCP */ 147 u32_t tcpactiveopens; 148 u32_t tcppassiveopens; 149 u32_t tcpattemptfails; 150 u32_t tcpestabresets; 151 u32_t tcpoutsegs; 152 u32_t tcpretranssegs; 153 u32_t tcpinsegs; 154 u32_t tcpinerrs; 155 u32_t tcpoutrsts; 156 157 /* UDP */ 158 u32_t udpindatagrams; 159 u32_t udpnoports; 160 u32_t udpinerrors; 161 u32_t udpoutdatagrams; 162 163 /* ICMP */ 164 u32_t icmpinmsgs; 165 u32_t icmpinerrors; 166 u32_t icmpindestunreachs; 167 u32_t icmpintimeexcds; 168 u32_t icmpinparmprobs; 169 u32_t icmpinsrcquenchs; 170 u32_t icmpinredirects; 171 u32_t icmpinechos; 172 u32_t icmpinechoreps; 173 u32_t icmpintimestamps; 174 u32_t icmpintimestampreps; 175 u32_t icmpinaddrmasks; 176 u32_t icmpinaddrmaskreps; 177 u32_t icmpoutmsgs; 178 u32_t icmpouterrors; 179 u32_t icmpoutdestunreachs; 180 u32_t icmpouttimeexcds; 181 u32_t icmpoutechos; /* can be incremented by user application ('ping') */ 182 u32_t icmpoutechoreps; 183 }; 184 185 /** 186 * @ingroup netif_mib2 187 * SNMP MIB2 interface stats 188 */ 189 struct stats_mib2_netif_ctrs { 190 /** The total number of octets received on the interface, including framing characters */ 191 u32_t ifinoctets; 192 /** The number of packets, delivered by this sub-layer to a higher (sub-)layer, which were 193 * not addressed to a multicast or broadcast address at this sub-layer */ 194 u32_t ifinucastpkts; 195 /** The number of packets, delivered by this sub-layer to a higher (sub-)layer, which were 196 * addressed to a multicast or broadcast address at this sub-layer */ 197 u32_t ifinnucastpkts; 198 /** The number of inbound packets which were chosen to be discarded even though no errors had 199 * been detected to prevent their being deliverable to a higher-layer protocol. One possible 200 * reason for discarding such a packet could be to free up buffer space */ 201 u32_t ifindiscards; 202 /** For packet-oriented interfaces, the number of inbound packets that contained errors 203 * preventing them from being deliverable to a higher-layer protocol. For character- 204 * oriented or fixed-length interfaces, the number of inbound transmission units that 205 * contained errors preventing them from being deliverable to a higher-layer protocol. */ 206 u32_t ifinerrors; 207 /** For packet-oriented interfaces, the number of packets received via the interface which 208 * were discarded because of an unknown or unsupported protocol. For character-oriented 209 * or fixed-length interfaces that support protocol multiplexing the number of transmission 210 * units received via the interface which were discarded because of an unknown or unsupported 211 * protocol. For any interface that does not support protocol multiplexing, this counter will 212 * always be 0 */ 213 u32_t ifinunknownprotos; 214 /** The total number of octets transmitted out of the interface, including framing characters. */ 215 u32_t ifoutoctets; 216 /** The total number of packets that higher-level protocols requested be transmitted, and 217 * which were not addressed to a multicast or broadcast address at this sub-layer, including 218 * those that were discarded or not sent. */ 219 u32_t ifoutucastpkts; 220 /** The total number of packets that higher-level protocols requested be transmitted, and which 221 * were addressed to a multicast or broadcast address at this sub-layer, including 222 * those that were discarded or not sent. */ 223 u32_t ifoutnucastpkts; 224 /** The number of outbound packets which were chosen to be discarded even though no errors had 225 * been detected to prevent their being transmitted. One possible reason for discarding 226 * such a packet could be to free up buffer space. */ 227 u32_t ifoutdiscards; 228 /** For packet-oriented interfaces, the number of outbound packets that could not be transmitted 229 * because of errors. For character-oriented or fixed-length interfaces, the number of outbound 230 * transmission units that could not be transmitted because of errors. */ 231 u32_t ifouterrors; 232 }; 233 234 /** lwIP stats container */ 235 struct stats_ { 236 #if LINK_STATS 237 /** Link level */ 238 struct stats_proto link; 239 #endif 240 #if ETHARP_STATS 241 /** ARP */ 242 struct stats_proto etharp; 243 #endif 244 #if IPFRAG_STATS 245 /** Fragmentation */ 246 struct stats_proto ip_frag; 247 #endif 248 #if IP_STATS 249 /** IP */ 250 struct stats_proto ip; 251 #endif 252 #if ICMP_STATS 253 /** ICMP */ 254 struct stats_proto icmp; 255 #endif 256 #if IGMP_STATS 257 /** IGMP */ 258 struct stats_igmp igmp; 259 #endif 260 #if UDP_STATS 261 /** UDP */ 262 struct stats_proto udp; 263 #endif 264 #if TCP_STATS 265 /** TCP */ 266 struct stats_proto tcp; 267 #endif 268 #if MEM_STATS 269 /** Heap */ 270 struct stats_mem mem; 271 #endif 272 #if MEMP_STATS 273 /** Internal memory pools */ 274 struct stats_mem *memp[MEMP_MAX]; 275 #endif 276 #if SYS_STATS 277 /** System */ 278 struct stats_sys sys; 279 #endif 280 #if IP6_STATS 281 /** IPv6 */ 282 struct stats_proto ip6; 283 #endif 284 #if ICMP6_STATS 285 /** ICMP6 */ 286 struct stats_proto icmp6; 287 #endif 288 #if IP6_FRAG_STATS 289 /** IPv6 fragmentation */ 290 struct stats_proto ip6_frag; 291 #endif 292 #if MLD6_STATS 293 /** Multicast listener discovery */ 294 struct stats_igmp mld6; 295 #endif 296 #if ND6_STATS 297 /** Neighbor discovery */ 298 struct stats_proto nd6; 299 #endif 300 #if MIB2_STATS 301 /** SNMP MIB2 */ 302 struct stats_mib2 mib2; 303 #endif 304 }; 305 306 /** Global variable containing lwIP internal statistics. Add this to your debugger's watchlist. */ 307 extern struct stats_ lwip_stats; 308 309 /** Init statistics */ 310 void stats_init(void); 311 312 #define STATS_INC(x) ++lwip_stats.x 313 #define STATS_DEC(x) --lwip_stats.x 314 #define STATS_INC_USED(x, y, type) do { lwip_stats.x.used = (type)(lwip_stats.x.used + y); \ 315 if (lwip_stats.x.max < lwip_stats.x.used) { \ 316 lwip_stats.x.max = lwip_stats.x.used; \ 317 } \ 318 } while(0) 319 #define STATS_GET(x) lwip_stats.x 320 #else /* LWIP_STATS */ 321 #define stats_init() 322 #define STATS_INC(x) 323 #define STATS_DEC(x) 324 #define STATS_INC_USED(x, y, type) 325 #endif /* LWIP_STATS */ 326 327 #if TCP_STATS 328 #define TCP_STATS_INC(x) STATS_INC(x) 329 #define TCP_STATS_DISPLAY() stats_display_proto(&lwip_stats.tcp, "TCP") 330 #else 331 #define TCP_STATS_INC(x) 332 #define TCP_STATS_DISPLAY() 333 #endif 334 335 #if UDP_STATS 336 #define UDP_STATS_INC(x) STATS_INC(x) 337 #define UDP_STATS_DISPLAY() stats_display_proto(&lwip_stats.udp, "UDP") 338 #else 339 #define UDP_STATS_INC(x) 340 #define UDP_STATS_DISPLAY() 341 #endif 342 343 #if ICMP_STATS 344 #define ICMP_STATS_INC(x) STATS_INC(x) 345 #define ICMP_STATS_DISPLAY() stats_display_proto(&lwip_stats.icmp, "ICMP") 346 #else 347 #define ICMP_STATS_INC(x) 348 #define ICMP_STATS_DISPLAY() 349 #endif 350 351 #if IGMP_STATS 352 #define IGMP_STATS_INC(x) STATS_INC(x) 353 #define IGMP_STATS_DISPLAY() stats_display_igmp(&lwip_stats.igmp, "IGMP") 354 #else 355 #define IGMP_STATS_INC(x) 356 #define IGMP_STATS_DISPLAY() 357 #endif 358 359 #if IP_STATS 360 #define IP_STATS_INC(x) STATS_INC(x) 361 #define IP_STATS_DISPLAY() stats_display_proto(&lwip_stats.ip, "IP") 362 #else 363 #define IP_STATS_INC(x) 364 #define IP_STATS_DISPLAY() 365 #endif 366 367 #if IPFRAG_STATS 368 #define IPFRAG_STATS_INC(x) STATS_INC(x) 369 #define IPFRAG_STATS_DISPLAY() stats_display_proto(&lwip_stats.ip_frag, "IP_FRAG") 370 #else 371 #define IPFRAG_STATS_INC(x) 372 #define IPFRAG_STATS_DISPLAY() 373 #endif 374 375 #if ETHARP_STATS 376 #define ETHARP_STATS_INC(x) STATS_INC(x) 377 #define ETHARP_STATS_DISPLAY() stats_display_proto(&lwip_stats.etharp, "ETHARP") 378 #else 379 #define ETHARP_STATS_INC(x) 380 #define ETHARP_STATS_DISPLAY() 381 #endif 382 383 #if LINK_STATS 384 #define LINK_STATS_INC(x) STATS_INC(x) 385 #define LINK_STATS_DISPLAY() stats_display_proto(&lwip_stats.link, "LINK") 386 #else 387 #define LINK_STATS_INC(x) 388 #define LINK_STATS_DISPLAY() 389 #endif 390 391 #if MEM_STATS 392 #define MEM_STATS_AVAIL(x, y) lwip_stats.mem.x = y 393 #define MEM_STATS_INC(x) STATS_INC(mem.x) 394 #define MEM_STATS_INC_USED(x, y) STATS_INC_USED(mem, y, mem_size_t) 395 #define MEM_STATS_DEC_USED(x, y) lwip_stats.mem.x = (mem_size_t)((lwip_stats.mem.x) - (y)) 396 #define MEM_STATS_DISPLAY() stats_display_mem(&lwip_stats.mem, "HEAP") 397 #else 398 #define MEM_STATS_AVAIL(x, y) 399 #define MEM_STATS_INC(x) 400 #define MEM_STATS_INC_USED(x, y) 401 #define MEM_STATS_DEC_USED(x, y) 402 #define MEM_STATS_DISPLAY() 403 #endif 404 405 #if MEMP_STATS 406 #define MEMP_STATS_DEC(x, i) STATS_DEC(memp[i]->x) 407 #define MEMP_STATS_DISPLAY(i) stats_display_memp(lwip_stats.memp[i], i) 408 #define MEMP_STATS_GET(x, i) STATS_GET(memp[i]->x) 409 #else 410 #define MEMP_STATS_DEC(x, i) 411 #define MEMP_STATS_DISPLAY(i) 412 #define MEMP_STATS_GET(x, i) 0 413 #endif 414 415 #if SYS_STATS 416 #define SYS_STATS_INC(x) STATS_INC(sys.x) 417 #define SYS_STATS_DEC(x) STATS_DEC(sys.x) 418 #define SYS_STATS_INC_USED(x) STATS_INC_USED(sys.x, 1, STAT_COUNTER) 419 #define SYS_STATS_DISPLAY() stats_display_sys(&lwip_stats.sys) 420 #else 421 #define SYS_STATS_INC(x) 422 #define SYS_STATS_DEC(x) 423 #define SYS_STATS_INC_USED(x) 424 #define SYS_STATS_DISPLAY() 425 #endif 426 427 #if IP6_STATS 428 #define IP6_STATS_INC(x) STATS_INC(x) 429 #define IP6_STATS_DISPLAY() stats_display_proto(&lwip_stats.ip6, "IPv6") 430 #else 431 #define IP6_STATS_INC(x) 432 #define IP6_STATS_DISPLAY() 433 #endif 434 435 #if ICMP6_STATS 436 #define ICMP6_STATS_INC(x) STATS_INC(x) 437 #define ICMP6_STATS_DISPLAY() stats_display_proto(&lwip_stats.icmp6, "ICMPv6") 438 #else 439 #define ICMP6_STATS_INC(x) 440 #define ICMP6_STATS_DISPLAY() 441 #endif 442 443 #if IP6_FRAG_STATS 444 #define IP6_FRAG_STATS_INC(x) STATS_INC(x) 445 #define IP6_FRAG_STATS_DISPLAY() stats_display_proto(&lwip_stats.ip6_frag, "IPv6 FRAG") 446 #else 447 #define IP6_FRAG_STATS_INC(x) 448 #define IP6_FRAG_STATS_DISPLAY() 449 #endif 450 451 #if MLD6_STATS 452 #define MLD6_STATS_INC(x) STATS_INC(x) 453 #define MLD6_STATS_DISPLAY() stats_display_igmp(&lwip_stats.mld6, "MLDv1") 454 #else 455 #define MLD6_STATS_INC(x) 456 #define MLD6_STATS_DISPLAY() 457 #endif 458 459 #if ND6_STATS 460 #define ND6_STATS_INC(x) STATS_INC(x) 461 #define ND6_STATS_DISPLAY() stats_display_proto(&lwip_stats.nd6, "ND") 462 #else 463 #define ND6_STATS_INC(x) 464 #define ND6_STATS_DISPLAY() 465 #endif 466 467 #if MIB2_STATS 468 #define MIB2_STATS_INC(x) STATS_INC(x) 469 #else 470 #define MIB2_STATS_INC(x) 471 #endif 472 473 /* Display of statistics */ 474 #if LWIP_STATS_DISPLAY 475 void stats_display(void); 476 void stats_display_proto(struct stats_proto *proto, const char *name); 477 void stats_display_igmp(struct stats_igmp *igmp, const char *name); 478 void stats_display_mem(struct stats_mem *mem, const char *name); 479 void stats_display_memp(struct stats_mem *mem, int index); 480 void stats_display_sys(struct stats_sys *sys); 481 #else /* LWIP_STATS_DISPLAY */ 482 #define stats_display() 483 #define stats_display_proto(proto, name) 484 #define stats_display_igmp(igmp, name) 485 #define stats_display_mem(mem, name) 486 #define stats_display_memp(mem, index) 487 #define stats_display_sys(sys) 488 #endif /* LWIP_STATS_DISPLAY */ 489 490 #ifdef __cplusplus 491 } 492 #endif 493 494 #endif /* LWIP_HDR_STATS_H */ 495