VirtualBox

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

Last change on this file since 30016 was 30016, checked in by vboxsync, 14 years ago

NAT: clean up.

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