1 /**
2 * @file
3 * Statistics module
4 *
5 */
6
7 /*
8 * Copyright (c) 2001-2004 Swedish Institute of Computer Science.
9 * All rights reserved.
10 *
11 * Redistribution and use in source and binary forms, with or without modification,
12 * are permitted provided that the following conditions are met:
13 *
14 * 1. Redistributions of source code must retain the above copyright notice,
15 * this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright notice,
17 * this list of conditions and the following disclaimer in the documentation
18 * and/or other materials provided with the distribution.
19 * 3. The name of the author may not be used to endorse or promote products
20 * derived from this software without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
23 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
24 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
25 * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
26 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
27 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
30 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
31 * OF SUCH DAMAGE.
32 *
33 * This file is part of the lwIP TCP/IP stack.
34 *
35 * Author: Adam Dunkels <adam@sics.se>
36 *
37 */
38
39 #include "lwip/opt.h"
40
41 #if LWIP_STATS /* don't build if not configured for use in lwipopts.h */
42
43 #include "lwip/def.h"
44 #include "lwip/stats.h"
45 #include "lwip/mem.h"
46 #include "lwip/debug.h"
47
48 #include <string.h>
49
50 struct stats_ lwip_stats;
51
52 void
stats_init(void)53 stats_init(void)
54 {
55 #ifdef LWIP_DEBUG
56 #if MEM_STATS
57 lwip_stats.mem.name = "MEM";
58 #endif /* MEM_STATS */
59 #endif /* LWIP_DEBUG */
60 }
61
62 #if LWIP_STATS_DISPLAY
63 void
stats_display_proto(struct stats_proto * proto,const char * name)64 stats_display_proto(struct stats_proto *proto, const char *name)
65 {
66 LWIP_PLATFORM_DIAG(("\n%s\n\t", name));
67 LWIP_PLATFORM_DIAG(("xmit: %"STAT_COUNTER_F"\n\t", proto->xmit));
68 LWIP_PLATFORM_DIAG(("recv: %"STAT_COUNTER_F"\n\t", proto->recv));
69 LWIP_PLATFORM_DIAG(("fw: %"STAT_COUNTER_F"\n\t", proto->fw));
70 LWIP_PLATFORM_DIAG(("drop: %"STAT_COUNTER_F"\n\t", proto->drop));
71 LWIP_PLATFORM_DIAG(("chkerr: %"STAT_COUNTER_F"\n\t", proto->chkerr));
72 LWIP_PLATFORM_DIAG(("lenerr: %"STAT_COUNTER_F"\n\t", proto->lenerr));
73 LWIP_PLATFORM_DIAG(("memerr: %"STAT_COUNTER_F"\n\t", proto->memerr));
74 LWIP_PLATFORM_DIAG(("rterr: %"STAT_COUNTER_F"\n\t", proto->rterr));
75 LWIP_PLATFORM_DIAG(("proterr: %"STAT_COUNTER_F"\n\t", proto->proterr));
76 LWIP_PLATFORM_DIAG(("opterr: %"STAT_COUNTER_F"\n\t", proto->opterr));
77 LWIP_PLATFORM_DIAG(("err: %"STAT_COUNTER_F"\n\t", proto->err));
78 LWIP_PLATFORM_DIAG(("cachehit: %"STAT_COUNTER_F"\n\t", proto->cachehit));
79 LWIP_PLATFORM_DIAG(("cacheMiss: %"STAT_COUNTER_F"\n\t", proto->cache_miss));
80 LWIP_PLATFORM_DIAG(("cacheFull: %"STAT_COUNTER_F"\n", proto->cache_full));
81
82 #ifdef LWIP_IPV6
83 LWIP_PLATFORM_DIAG(("destunreachs: %"STAT_COUNTER_F"\n", proto->destunreachs));
84 LWIP_PLATFORM_DIAG(("timeexcds: %"STAT_COUNTER_F"\n", proto->timeexcds));
85 LWIP_PLATFORM_DIAG(("parmprobs: %"STAT_COUNTER_F"\n", proto->parmprobs));
86 #endif
87
88 #if LWIP_NAT64
89 LWIP_PLATFORM_DIAG(("natfw: %"STAT_COUNTER_F"\n", proto->natfw));
90 LWIP_PLATFORM_DIAG(("natdrop: %"STAT_COUNTER_F"\n", proto->natdrop));
91 #endif
92 }
93
94 #if IGMP_STATS || MLD6_STATS
95 void
stats_display_igmp(struct stats_igmp * igmp,const char * name)96 stats_display_igmp(struct stats_igmp *igmp, const char *name)
97 {
98 LWIP_PLATFORM_DIAG(("\n%s\n\t", name));
99 LWIP_PLATFORM_DIAG(("xmit: %"STAT_COUNTER_F"\n\t", igmp->xmit));
100 LWIP_PLATFORM_DIAG(("recv: %"STAT_COUNTER_F"\n\t", igmp->recv));
101 LWIP_PLATFORM_DIAG(("drop: %"STAT_COUNTER_F"\n\t", igmp->drop));
102 LWIP_PLATFORM_DIAG(("chkerr: %"STAT_COUNTER_F"\n\t", igmp->chkerr));
103 LWIP_PLATFORM_DIAG(("lenerr: %"STAT_COUNTER_F"\n\t", igmp->lenerr));
104 LWIP_PLATFORM_DIAG(("memerr: %"STAT_COUNTER_F"\n\t", igmp->memerr));
105 LWIP_PLATFORM_DIAG(("proterr: %"STAT_COUNTER_F"\n\t", igmp->proterr));
106 LWIP_PLATFORM_DIAG(("rx_v1: %"STAT_COUNTER_F"\n\t", igmp->rx_v1));
107 LWIP_PLATFORM_DIAG(("rx_group: %"STAT_COUNTER_F"\n\t", igmp->rx_group));
108 LWIP_PLATFORM_DIAG(("rx_general: %"STAT_COUNTER_F"\n\t", igmp->rx_general));
109 LWIP_PLATFORM_DIAG(("rx_report: %"STAT_COUNTER_F"\n\t", igmp->rx_report));
110 LWIP_PLATFORM_DIAG(("tx_join: %"STAT_COUNTER_F"\n\t", igmp->tx_join));
111 LWIP_PLATFORM_DIAG(("tx_leave: %"STAT_COUNTER_F"\n\t", igmp->tx_leave));
112 LWIP_PLATFORM_DIAG(("tx_report: %"STAT_COUNTER_F"\n", igmp->tx_report));
113 }
114 #endif /* IGMP_STATS || MLD6_STATS */
115
116 #if MEM_STATS || MEMP_STATS
117 void
stats_display_mem(struct stats_mem * mem,const char * name)118 stats_display_mem(struct stats_mem *mem, const char *name)
119 {
120 LWIP_PLATFORM_DIAG(("\nMEM %s\n\t", name));
121 LWIP_PLATFORM_DIAG(("avail: %"MEM_SIZE_F"\n\t", mem->avail));
122 LWIP_PLATFORM_DIAG(("used: %"MEM_SIZE_F"\n\t", mem->used));
123 LWIP_PLATFORM_DIAG(("max: %"MEM_SIZE_F"\n\t", mem->max));
124 LWIP_PLATFORM_DIAG(("err: %"STAT_COUNTER_F"\n", mem->err));
125 }
126
127 #if MEMP_STATS
128 void
stats_display_memp(struct stats_mem * mem,int idx)129 stats_display_memp(struct stats_mem *mem, int idx)
130 {
131 if (idx < MEMP_MAX) {
132 stats_display_mem(mem, mem->name);
133 }
134 }
135 #endif /* MEMP_STATS */
136 #endif /* MEM_STATS || MEMP_STATS */
137
138 #if SYS_STATS
139 void
stats_display_sys(struct stats_sys * sys)140 stats_display_sys(struct stats_sys *sys)
141 {
142 LWIP_PLATFORM_DIAG(("\nSYS\n\t"));
143 LWIP_PLATFORM_DIAG(("sem.used: %"STAT_COUNTER_F"\n\t", sys->sem.used));
144 LWIP_PLATFORM_DIAG(("sem.max: %"STAT_COUNTER_F"\n\t", sys->sem.max));
145 LWIP_PLATFORM_DIAG(("sem.err: %"STAT_COUNTER_F"\n\t", sys->sem.err));
146 LWIP_PLATFORM_DIAG(("mutex.used: %"STAT_COUNTER_F"\n\t", sys->mutex.used));
147 LWIP_PLATFORM_DIAG(("mutex.max: %"STAT_COUNTER_F"\n\t", sys->mutex.max));
148 LWIP_PLATFORM_DIAG(("mutex.err: %"STAT_COUNTER_F"\n\t", sys->mutex.err));
149 LWIP_PLATFORM_DIAG(("mbox.used: %"STAT_COUNTER_F"\n\t", sys->mbox.used));
150 LWIP_PLATFORM_DIAG(("mbox.max: %"STAT_COUNTER_F"\n\t", sys->mbox.max));
151 LWIP_PLATFORM_DIAG(("mbox.err: %"STAT_COUNTER_F"\n", sys->mbox.err));
152 }
153 #endif /* SYS_STATS */
154
155 void
stats_display(void)156 stats_display(void)
157 {
158 s16_t i;
159
160 LINK_STATS_DISPLAY();
161 ETHARP_STATS_DISPLAY();
162 IPFRAG_STATS_DISPLAY();
163 IP6_FRAG_STATS_DISPLAY();
164 IP_STATS_DISPLAY();
165 ND6_STATS_DISPLAY();
166 IP6_STATS_DISPLAY();
167 DHCP6_STATS_DISPLAY();
168 IGMP_STATS_DISPLAY();
169 MLD6_STATS_DISPLAY();
170 ICMP_STATS_DISPLAY();
171 ICMP6_STATS_DISPLAY();
172 UDP_STATS_DISPLAY();
173 TCP_STATS_DISPLAY();
174 MEM_STATS_DISPLAY();
175 for (i = 0; i < MEMP_MAX; i++) {
176 MEMP_STATS_DISPLAY(i);
177 }
178 SYS_STATS_DISPLAY();
179 }
180 #endif /* LWIP_STATS_DISPLAY */
181
182 #define VALIDATE_PARAM(para) \
183 if (para == NULL) { \
184 return ERR_ARG; \
185 }
186
187 err_t
lwip_statsapi_get_ipv6_stats(struct stats_proto * ipv6)188 lwip_statsapi_get_ipv6_stats(struct stats_proto *ipv6)
189 {
190 VALIDATE_PARAM(ipv6);
191
192 #if IP6_STATS
193 (void)memcpy_s(ipv6, sizeof(struct stats_proto), &lwip_stats.ip6,
194 sizeof(struct stats_proto));
195 #endif
196
197 return ERR_OK;
198 }
199
200 err_t
lwip_statsapi_get_icmpv6_stats(struct stats_proto * icmpv6)201 lwip_statsapi_get_icmpv6_stats(struct stats_proto *icmpv6)
202 {
203 VALIDATE_PARAM(icmpv6);
204
205 #if ICMP6_STATS
206 (void)memcpy_s(icmpv6, sizeof(struct stats_proto), &lwip_stats.icmp6,
207 sizeof(struct stats_proto));
208
209 /* Since Incase of ND6/MLD6, stats are not increased by handling function, Need to add manually. */
210 #if ND6_STATS
211 icmpv6->xmit += lwip_stats.nd6.xmit;
212 #endif
213 #if LWIP_IPV6_MLD
214 icmpv6->xmit += lwip_stats.mld6.xmit;
215 #endif
216 #endif
217
218 return ERR_OK;
219 }
220
221 err_t
lwip_statsapi_get_nd6_stats(struct stats_proto * nd6)222 lwip_statsapi_get_nd6_stats(struct stats_proto *nd6)
223 {
224 VALIDATE_PARAM(nd6);
225
226 #if ND6_STATS
227 (void)memcpy_s(nd6, sizeof(struct stats_proto), &lwip_stats.nd6,
228 sizeof(struct stats_proto));
229 #endif
230
231 return ERR_OK;
232 }
233
234 err_t
lwip_statsapi_get_tcp_stats(struct stats_proto * tcp)235 lwip_statsapi_get_tcp_stats(struct stats_proto *tcp)
236 {
237 VALIDATE_PARAM(tcp);
238
239 #if TCP_STATS
240 (void)memcpy_s(tcp, sizeof(struct stats_proto), &lwip_stats.tcp,
241 sizeof(struct stats_proto));
242 #endif
243
244 return ERR_OK;
245 }
246
247 err_t
lwip_statsapi_get_udp_stats(struct stats_proto * udp)248 lwip_statsapi_get_udp_stats(struct stats_proto *udp)
249 {
250 VALIDATE_PARAM(udp);
251
252 #if UDP_STATS
253 (void)memcpy_s(udp, sizeof(struct stats_proto), &lwip_stats.udp,
254 sizeof(struct stats_proto));
255 #endif
256
257 return ERR_OK;
258 }
259
260 #endif /* LWIP_STATS */
261
262