VirtualBox

source: vbox/trunk/src/VBox/Devices/Network/slirp/debug.c@ 28520

Last change on this file since 28520 was 28449, checked in by vboxsync, 15 years ago

NAT: slirp file headers

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 14.0 KB
Line 
1/* $Id: debug.c 28449 2010-04-19 09:52:59Z vboxsync $ */
2/** @file
3 * NAT - debug helpers.
4 */
5
6/*
7 * Copyright (C) 2006-2010 Sun Microsystems, Inc.
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 *
17 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
18 * Clara, CA 95054 USA or visit http://www.sun.com if you need
19 * additional information or have any questions.
20 */
21
22
23/*
24 * This code is based on:
25 *
26 * Copyright (c) 1995 Danny Gasparovski.
27 * Portions copyright (c) 2000 Kelly Price.
28 *
29 * Please read the file COPYRIGHT for the
30 * terms and conditions of the copyright.
31 */
32
33#include <slirp.h>
34#include <iprt/string.h>
35#include <iprt/stream.h>
36
37#ifdef DEBUG
38void dump_packet(void *, int);
39#endif
40
41#define IP4_ADDR_PRINTF_DECOMP(ip) ((ip) >> 24), ((ip) >> 16) & 0xff, ((ip) >> 8) & 0xff, (ip) & 0xff
42#define IP4_ADDR_PRINTF_FORMAT "%u.%u.%u.%u"
43/*
44 * Dump a packet in the same format as tcpdump -x
45 */
46#ifdef DEBUG
47void
48dump_packet(void *dat, int n)
49{
50 Log(("nat: PACKET DUMPED:\n%.*Rhxd\n", n, dat));
51}
52#endif
53
54#ifdef LOG_ENABLED
55static void
56lprint(const char *pszFormat, ...)
57{
58 va_list args;
59 va_start(args, pszFormat);
60 RTLogPrintfV(pszFormat, args);
61 va_end(args);
62}
63
64void
65ipstats(PNATState pData)
66{
67 lprint(" \n");
68
69 lprint("IP stats:\n");
70 lprint(" %6d total packets received (%d were unaligned)\n",
71 ipstat.ips_total, ipstat.ips_unaligned);
72 lprint(" %6d with incorrect version\n", ipstat.ips_badvers);
73 lprint(" %6d with bad header checksum\n", ipstat.ips_badsum);
74 lprint(" %6d with length too short (len < sizeof(iphdr))\n", ipstat.ips_tooshort);
75 lprint(" %6d with length too small (len < ip->len)\n", ipstat.ips_toosmall);
76 lprint(" %6d with bad header length\n", ipstat.ips_badhlen);
77 lprint(" %6d with bad packet length\n", ipstat.ips_badlen);
78 lprint(" %6d fragments received\n", ipstat.ips_fragments);
79 lprint(" %6d fragments dropped\n", ipstat.ips_fragdropped);
80 lprint(" %6d fragments timed out\n", ipstat.ips_fragtimeout);
81 lprint(" %6d packets reassembled ok\n", ipstat.ips_reassembled);
82 lprint(" %6d outgoing packets fragmented\n", ipstat.ips_fragmented);
83 lprint(" %6d total outgoing fragments\n", ipstat.ips_ofragments);
84 lprint(" %6d with bad protocol field\n", ipstat.ips_noproto);
85 lprint(" %6d total packets delivered\n", ipstat.ips_delivered);
86}
87
88void
89tcpstats(PNATState pData)
90{
91 lprint(" \n");
92
93 lprint("TCP stats:\n");
94
95 lprint(" %6d packets sent\n", tcpstat.tcps_sndtotal);
96 lprint(" %6d data packets (%d bytes)\n",
97 tcpstat.tcps_sndpack, tcpstat.tcps_sndbyte);
98 lprint(" %6d data packets retransmitted (%d bytes)\n",
99 tcpstat.tcps_sndrexmitpack, tcpstat.tcps_sndrexmitbyte);
100 lprint(" %6d ack-only packets (%d delayed)\n",
101 tcpstat.tcps_sndacks, tcpstat.tcps_delack);
102 lprint(" %6d URG only packets\n", tcpstat.tcps_sndurg);
103 lprint(" %6d window probe packets\n", tcpstat.tcps_sndprobe);
104 lprint(" %6d window update packets\n", tcpstat.tcps_sndwinup);
105 lprint(" %6d control (SYN/FIN/RST) packets\n", tcpstat.tcps_sndctrl);
106 lprint(" %6d times tcp_output did nothing\n", tcpstat.tcps_didnuttin);
107
108 lprint(" %6d packets received\n", tcpstat.tcps_rcvtotal);
109 lprint(" %6d acks (for %d bytes)\n",
110 tcpstat.tcps_rcvackpack, tcpstat.tcps_rcvackbyte);
111 lprint(" %6d duplicate acks\n", tcpstat.tcps_rcvdupack);
112 lprint(" %6d acks for unsent data\n", tcpstat.tcps_rcvacktoomuch);
113 lprint(" %6d packets received in sequence (%d bytes)\n",
114 tcpstat.tcps_rcvpack, tcpstat.tcps_rcvbyte);
115 lprint(" %6d completely duplicate packets (%d bytes)\n",
116 tcpstat.tcps_rcvduppack, tcpstat.tcps_rcvdupbyte);
117
118 lprint(" %6d packets with some duplicate data (%d bytes duped)\n",
119 tcpstat.tcps_rcvpartduppack, tcpstat.tcps_rcvpartdupbyte);
120 lprint(" %6d out-of-order packets (%d bytes)\n",
121 tcpstat.tcps_rcvoopack, tcpstat.tcps_rcvoobyte);
122 lprint(" %6d packets of data after window (%d bytes)\n",
123 tcpstat.tcps_rcvpackafterwin, tcpstat.tcps_rcvbyteafterwin);
124 lprint(" %6d window probes\n", tcpstat.tcps_rcvwinprobe);
125 lprint(" %6d window update packets\n", tcpstat.tcps_rcvwinupd);
126 lprint(" %6d packets received after close\n", tcpstat.tcps_rcvafterclose);
127 lprint(" %6d discarded for bad checksums\n", tcpstat.tcps_rcvbadsum);
128 lprint(" %6d discarded for bad header offset fields\n",
129 tcpstat.tcps_rcvbadoff);
130
131 lprint(" %6d connection requests\n", tcpstat.tcps_connattempt);
132 lprint(" %6d connection accepts\n", tcpstat.tcps_accepts);
133 lprint(" %6d connections established (including accepts)\n", tcpstat.tcps_connects);
134 lprint(" %6d connections closed (including %d drop)\n",
135 tcpstat.tcps_closed, tcpstat.tcps_drops);
136 lprint(" %6d embryonic connections dropped\n", tcpstat.tcps_conndrops);
137 lprint(" %6d segments we tried to get rtt (%d succeeded)\n",
138 tcpstat.tcps_segstimed, tcpstat.tcps_rttupdated);
139 lprint(" %6d retransmit timeouts\n", tcpstat.tcps_rexmttimeo);
140 lprint(" %6d connections dropped by rxmt timeout\n",
141 tcpstat.tcps_timeoutdrop);
142 lprint(" %6d persist timeouts\n", tcpstat.tcps_persisttimeo);
143 lprint(" %6d keepalive timeouts\n", tcpstat.tcps_keeptimeo);
144 lprint(" %6d keepalive probes sent\n", tcpstat.tcps_keepprobe);
145 lprint(" %6d connections dropped by keepalive\n", tcpstat.tcps_keepdrops);
146 lprint(" %6d correct ACK header predictions\n", tcpstat.tcps_predack);
147 lprint(" %6d correct data packet header predictions\n", tcpstat.tcps_preddat);
148 lprint(" %6d TCP cache misses\n", tcpstat.tcps_socachemiss);
149
150/* lprint(" Packets received too short: %d\n", tcpstat.tcps_rcvshort); */
151/* lprint(" Segments dropped due to PAWS: %d\n", tcpstat.tcps_pawsdrop); */
152
153}
154
155void
156udpstats(PNATState pData)
157{
158 lprint(" \n");
159
160 lprint("UDP stats:\n");
161 lprint(" %6d datagrams received\n", udpstat.udps_ipackets);
162 lprint(" %6d with packets shorter than header\n", udpstat.udps_hdrops);
163 lprint(" %6d with bad checksums\n", udpstat.udps_badsum);
164 lprint(" %6d with data length larger than packet\n", udpstat.udps_badlen);
165 lprint(" %6d UDP socket cache misses\n", udpstat.udpps_pcbcachemiss);
166 lprint(" %6d datagrams sent\n", udpstat.udps_opackets);
167}
168
169void
170icmpstats(PNATState pData)
171{
172 lprint(" \n");
173 lprint("ICMP stats:\n");
174 lprint(" %6d ICMP packets received\n", icmpstat.icps_received);
175 lprint(" %6d were too short\n", icmpstat.icps_tooshort);
176 lprint(" %6d with bad checksums\n", icmpstat.icps_checksum);
177 lprint(" %6d with type not supported\n", icmpstat.icps_notsupp);
178 lprint(" %6d with bad type feilds\n", icmpstat.icps_badtype);
179 lprint(" %6d ICMP packets sent in reply\n", icmpstat.icps_reflect);
180}
181
182void
183mbufstats(PNATState pData)
184{
185#ifndef VBOX_WITH_SLIRP_BSD_MBUF
186 /*
187 * (vvl) this static code can't work with mbuf zone anymore
188 * @todo: make statistic correct
189 */
190#if 0
191 struct mbuf *m;
192 int i;
193
194 lprint(" \n");
195
196 lprint("Mbuf stats:\n");
197
198 lprint(" %6d mbufs allocated (%d max)\n", mbuf_alloced, mbuf_max);
199
200 i = 0;
201 for (m = m_freelist.m_next; m != &m_freelist; m = m->m_next)
202 i++;
203 lprint(" %6d mbufs on free list\n", i);
204
205 i = 0;
206 for (m = m_usedlist.m_next; m != &m_usedlist; m = m->m_next)
207 i++;
208 lprint(" %6d mbufs on used list\n", i);
209 lprint(" %6d mbufs queued as packets\n\n", if_queued);
210#endif
211#endif
212}
213
214void
215sockstats(PNATState pData)
216{
217 char buff[256];
218 size_t n;
219 struct socket *so, *so_next;
220
221 lprint(" \n");
222
223 lprint(
224 "Proto[state] Sock Local Address, Port Remote Address, Port RecvQ SendQ\n");
225
226 QSOCKET_FOREACH(so, so_next, tcp)
227 /* { */
228 n = RTStrPrintf(buff, sizeof(buff), "tcp[%s]", so->so_tcpcb?tcpstates[so->so_tcpcb->t_state]:"NONE");
229 while (n < 17)
230 buff[n++] = ' ';
231 buff[17] = 0;
232 lprint("%s %3d %15s %5d ",
233 buff, so->s, inet_ntoa(so->so_laddr), RT_N2H_U16(so->so_lport));
234 lprint("%15s %5d %5d %5d\n",
235 inet_ntoa(so->so_faddr), RT_N2H_U16(so->so_fport),
236 so->so_rcv.sb_cc, so->so_snd.sb_cc);
237 LOOP_LABEL(tcp, so, so_next);
238 }
239
240 QSOCKET_FOREACH(so, so_next, udp)
241 /* { */
242 n = RTStrPrintf(buff, sizeof(buff), "udp[%d sec]", (so->so_expire - curtime) / 1000);
243 while (n < 17)
244 buff[n++] = ' ';
245 buff[17] = 0;
246 lprint("%s %3d %15s %5d ",
247 buff, so->s, inet_ntoa(so->so_laddr), RT_N2H_U16(so->so_lport));
248 lprint("%15s %5d %5d %5d\n",
249 inet_ntoa(so->so_faddr), RT_N2H_U16(so->so_fport),
250 so->so_rcv.sb_cc, so->so_snd.sb_cc);
251 LOOP_LABEL(udp, so, so_next);
252 }
253}
254#endif
255
256static DECLCALLBACK(size_t)
257print_ipv4_address(PFNRTSTROUTPUT pfnOutput, void *pvArgOutput,
258 const char *pszType, void const *pvValue,
259 int cchWidth, int cchPrecision, unsigned fFlags,
260 void *pvUser)
261{
262 uint32_t ip;
263
264 AssertReturn(strcmp(pszType, "IP4") == 0, 0);
265
266 ip = RT_N2H_U32(*(uint32_t*)pvValue);
267 return RTStrFormat(pfnOutput, pvArgOutput, NULL, 0, IP4_ADDR_PRINTF_FORMAT,
268 IP4_ADDR_PRINTF_DECOMP(ip));
269}
270
271static DECLCALLBACK(size_t)
272print_ether_address(PFNRTSTROUTPUT pfnOutput, void *pvArgOutput,
273 const char *pszType, void const *pvValue,
274 int cchWidth, int cchPrecision, unsigned fFlags,
275 void *pvUser)
276{
277 char *ether = (char *)pvUser;
278
279 AssertReturn(strcmp(pszType, "ether") == 0, 0);
280 if (ether != NULL)
281 return RTStrFormat(pfnOutput, pvArgOutput, NULL, 0,
282 "[ether %hhx:%hhx:%hhx:%hhx:%hhx:%hhx]",
283 ether[0], ether[1], ether[2],
284 ether[3], ether[4], ether[5]);
285 else
286 return RTStrFormat(pfnOutput, pvArgOutput, NULL, 0, "[ether null]");
287}
288
289static DECLCALLBACK(size_t)
290print_socket(PFNRTSTROUTPUT pfnOutput, void *pvArgOutput,
291 const char *pszType, void const *pvValue,
292 int cchWidth, int cchPrecision, unsigned fFlags,
293 void *pvUser)
294{
295 struct socket *so = (struct socket*)pvValue;
296 uint32_t ip;
297 struct sockaddr addr;
298 struct sockaddr_in *in_addr;
299 socklen_t socklen = sizeof(struct sockaddr);
300 int status = 0;
301
302 AssertReturn(strcmp(pszType, "natsock") == 0, 0);
303 if (so == NULL)
304 return RTStrFormat(pfnOutput, pvArgOutput, NULL, 0,
305 "socket is null");
306 if (so->so_state == SS_NOFDREF || so->s == -1)
307 return RTStrFormat(pfnOutput, pvArgOutput, NULL, 0,
308 "socket(%d) SS_NODREF",so->s);
309 status = getsockname(so->s, &addr, &socklen);
310
311 if(status != 0 || addr.sa_family != AF_INET)
312 {
313 return RTStrFormat(pfnOutput, pvArgOutput, NULL, 0,
314 "socket(%d) is invalid(probably closed)",so->s);
315 }
316
317 in_addr = (struct sockaddr_in *)&addr;
318 ip = RT_N2H_U32(so->so_faddr.s_addr);
319 return RTStrFormat(pfnOutput, pvArgOutput, NULL, 0, "socket %4d:(proto:%u) "
320 "state=%04x ip=" IP4_ADDR_PRINTF_FORMAT ":%d "
321 "name=" IP4_ADDR_PRINTF_FORMAT ":%d",
322 so->s, so->so_type, so->so_state, IP4_ADDR_PRINTF_DECOMP(ip),
323 RT_N2H_U16(so->so_fport),
324 IP4_ADDR_PRINTF_DECOMP(RT_N2H_U32(in_addr->sin_addr.s_addr)),
325 RT_N2H_U16(in_addr->sin_port));
326}
327
328static DECLCALLBACK(size_t)
329print_networkevents(PFNRTSTROUTPUT pfnOutput, void *pvArgOutput,
330 const char *pszType, void const *pvValue,
331 int cchWidth, int cchPrecision, unsigned fFlags,
332 void *pvUser)
333{
334 size_t cb = 0;
335#ifdef RT_OS_WINDOWS
336 WSANETWORKEVENTS *pNetworkEvents = (WSANETWORKEVENTS*)pvValue;
337 bool fDelim = false;
338
339 AssertReturn(strcmp(pszType, "natwinnetevents") == 0, 0);
340
341 cb += RTStrFormat(pfnOutput, pvArgOutput, NULL, 0, "events=%02x (",
342 pNetworkEvents->lNetworkEvents);
343# define DO_BIT(bit) \
344 if (pNetworkEvents->lNetworkEvents & FD_ ## bit) \
345 { \
346 cb += RTStrFormat(pfnOutput, pvArgOutput, NULL, 0, \
347 "%s" #bit "(%d)", fDelim ? "," : "", \
348 pNetworkEvents->iErrorCode[FD_ ## bit ## _BIT]); \
349 fDelim = true; \
350 }
351 DO_BIT(READ);
352 DO_BIT(WRITE);
353 DO_BIT(OOB);
354 DO_BIT(ACCEPT);
355 DO_BIT(CONNECT);
356 DO_BIT(CLOSE);
357 DO_BIT(QOS);
358# undef DO_BIT
359 cb += RTStrFormat(pfnOutput, pvArgOutput, NULL, 0, ")");
360#endif
361 return cb;
362}
363
364#if 0
365/*
366 * Debugging
367 */
368int errno_func(const char *file, int line)
369{
370 int err = WSAGetLastError();
371 LogRel(("errno=%d (%s:%d)\n", err, file, line));
372 return err;
373}
374#endif
375
376int
377debug_init()
378{
379 int rc = VINF_SUCCESS;
380
381 static int g_fFormatRegistered;
382
383 if (!g_fFormatRegistered)
384 {
385 /*
386 * XXX(r - frank): Move this to IPRT using RTNETADDRIPV4.
387 * Use the specifier %RNAipv4.
388 */
389 rc = RTStrFormatTypeRegister("IP4", print_ipv4_address, NULL);
390 AssertRC(rc);
391 rc = RTStrFormatTypeRegister("ether", print_ether_address, NULL);
392 AssertRC(rc);
393 rc = RTStrFormatTypeRegister("natsock", print_socket, NULL);
394 AssertRC(rc);
395 rc = RTStrFormatTypeRegister("natwinnetevents",
396 print_networkevents, NULL);
397 AssertRC(rc);
398 g_fFormatRegistered = 1;
399 }
400
401 return rc;
402}
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette