VirtualBox

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

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

Automated rebranding to Oracle copyright/license strings via filemuncher

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 13.8 KB
Line 
1/* $Id: debug.c 28800 2010-04-27 08:22:32Z 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#ifndef VBOX_WITH_SLIRP_BSD_MBUF
182 /*
183 * (vvl) this static code can't work with mbuf zone anymore
184 * @todo: make statistic correct
185 */
186#if 0
187 struct mbuf *m;
188 int i;
189
190 lprint(" \n");
191
192 lprint("Mbuf stats:\n");
193
194 lprint(" %6d mbufs allocated (%d max)\n", mbuf_alloced, mbuf_max);
195
196 i = 0;
197 for (m = m_freelist.m_next; m != &m_freelist; m = m->m_next)
198 i++;
199 lprint(" %6d mbufs on free list\n", i);
200
201 i = 0;
202 for (m = m_usedlist.m_next; m != &m_usedlist; m = m->m_next)
203 i++;
204 lprint(" %6d mbufs on used list\n", i);
205 lprint(" %6d mbufs queued as packets\n\n", if_queued);
206#endif
207#endif
208}
209
210void
211sockstats(PNATState pData)
212{
213 char buff[256];
214 size_t n;
215 struct socket *so, *so_next;
216
217 lprint(" \n");
218
219 lprint(
220 "Proto[state] Sock Local Address, Port Remote Address, Port RecvQ SendQ\n");
221
222 QSOCKET_FOREACH(so, so_next, tcp)
223 /* { */
224 n = RTStrPrintf(buff, sizeof(buff), "tcp[%s]", so->so_tcpcb?tcpstates[so->so_tcpcb->t_state]:"NONE");
225 while (n < 17)
226 buff[n++] = ' ';
227 buff[17] = 0;
228 lprint("%s %3d %15s %5d ",
229 buff, so->s, inet_ntoa(so->so_laddr), RT_N2H_U16(so->so_lport));
230 lprint("%15s %5d %5d %5d\n",
231 inet_ntoa(so->so_faddr), RT_N2H_U16(so->so_fport),
232 so->so_rcv.sb_cc, so->so_snd.sb_cc);
233 LOOP_LABEL(tcp, so, so_next);
234 }
235
236 QSOCKET_FOREACH(so, so_next, udp)
237 /* { */
238 n = RTStrPrintf(buff, sizeof(buff), "udp[%d sec]", (so->so_expire - curtime) / 1000);
239 while (n < 17)
240 buff[n++] = ' ';
241 buff[17] = 0;
242 lprint("%s %3d %15s %5d ",
243 buff, so->s, inet_ntoa(so->so_laddr), RT_N2H_U16(so->so_lport));
244 lprint("%15s %5d %5d %5d\n",
245 inet_ntoa(so->so_faddr), RT_N2H_U16(so->so_fport),
246 so->so_rcv.sb_cc, so->so_snd.sb_cc);
247 LOOP_LABEL(udp, so, so_next);
248 }
249}
250#endif
251
252static DECLCALLBACK(size_t)
253print_ipv4_address(PFNRTSTROUTPUT pfnOutput, void *pvArgOutput,
254 const char *pszType, void const *pvValue,
255 int cchWidth, int cchPrecision, unsigned fFlags,
256 void *pvUser)
257{
258 uint32_t ip;
259
260 AssertReturn(strcmp(pszType, "IP4") == 0, 0);
261
262 ip = RT_N2H_U32(*(uint32_t*)pvValue);
263 return RTStrFormat(pfnOutput, pvArgOutput, NULL, 0, IP4_ADDR_PRINTF_FORMAT,
264 IP4_ADDR_PRINTF_DECOMP(ip));
265}
266
267static DECLCALLBACK(size_t)
268print_ether_address(PFNRTSTROUTPUT pfnOutput, void *pvArgOutput,
269 const char *pszType, void const *pvValue,
270 int cchWidth, int cchPrecision, unsigned fFlags,
271 void *pvUser)
272{
273 char *ether = (char *)pvUser;
274
275 AssertReturn(strcmp(pszType, "ether") == 0, 0);
276 if (ether != NULL)
277 return RTStrFormat(pfnOutput, pvArgOutput, NULL, 0,
278 "[ether %hhx:%hhx:%hhx:%hhx:%hhx:%hhx]",
279 ether[0], ether[1], ether[2],
280 ether[3], ether[4], ether[5]);
281 else
282 return RTStrFormat(pfnOutput, pvArgOutput, NULL, 0, "[ether null]");
283}
284
285static DECLCALLBACK(size_t)
286print_socket(PFNRTSTROUTPUT pfnOutput, void *pvArgOutput,
287 const char *pszType, void const *pvValue,
288 int cchWidth, int cchPrecision, unsigned fFlags,
289 void *pvUser)
290{
291 struct socket *so = (struct socket*)pvValue;
292 uint32_t ip;
293 struct sockaddr addr;
294 struct sockaddr_in *in_addr;
295 socklen_t socklen = sizeof(struct sockaddr);
296 int status = 0;
297
298 AssertReturn(strcmp(pszType, "natsock") == 0, 0);
299 if (so == NULL)
300 return RTStrFormat(pfnOutput, pvArgOutput, NULL, 0,
301 "socket is null");
302 if (so->so_state == SS_NOFDREF || so->s == -1)
303 return RTStrFormat(pfnOutput, pvArgOutput, NULL, 0,
304 "socket(%d) SS_NODREF",so->s);
305 status = getsockname(so->s, &addr, &socklen);
306
307 if(status != 0 || addr.sa_family != AF_INET)
308 {
309 return RTStrFormat(pfnOutput, pvArgOutput, NULL, 0,
310 "socket(%d) is invalid(probably closed)",so->s);
311 }
312
313 in_addr = (struct sockaddr_in *)&addr;
314 ip = RT_N2H_U32(so->so_faddr.s_addr);
315 return RTStrFormat(pfnOutput, pvArgOutput, NULL, 0, "socket %4d:(proto:%u) "
316 "state=%04x ip=" IP4_ADDR_PRINTF_FORMAT ":%d "
317 "name=" IP4_ADDR_PRINTF_FORMAT ":%d",
318 so->s, so->so_type, so->so_state, IP4_ADDR_PRINTF_DECOMP(ip),
319 RT_N2H_U16(so->so_fport),
320 IP4_ADDR_PRINTF_DECOMP(RT_N2H_U32(in_addr->sin_addr.s_addr)),
321 RT_N2H_U16(in_addr->sin_port));
322}
323
324static DECLCALLBACK(size_t)
325print_networkevents(PFNRTSTROUTPUT pfnOutput, void *pvArgOutput,
326 const char *pszType, void const *pvValue,
327 int cchWidth, int cchPrecision, unsigned fFlags,
328 void *pvUser)
329{
330 size_t cb = 0;
331#ifdef RT_OS_WINDOWS
332 WSANETWORKEVENTS *pNetworkEvents = (WSANETWORKEVENTS*)pvValue;
333 bool fDelim = false;
334
335 AssertReturn(strcmp(pszType, "natwinnetevents") == 0, 0);
336
337 cb += RTStrFormat(pfnOutput, pvArgOutput, NULL, 0, "events=%02x (",
338 pNetworkEvents->lNetworkEvents);
339# define DO_BIT(bit) \
340 if (pNetworkEvents->lNetworkEvents & FD_ ## bit) \
341 { \
342 cb += RTStrFormat(pfnOutput, pvArgOutput, NULL, 0, \
343 "%s" #bit "(%d)", fDelim ? "," : "", \
344 pNetworkEvents->iErrorCode[FD_ ## bit ## _BIT]); \
345 fDelim = true; \
346 }
347 DO_BIT(READ);
348 DO_BIT(WRITE);
349 DO_BIT(OOB);
350 DO_BIT(ACCEPT);
351 DO_BIT(CONNECT);
352 DO_BIT(CLOSE);
353 DO_BIT(QOS);
354# undef DO_BIT
355 cb += RTStrFormat(pfnOutput, pvArgOutput, NULL, 0, ")");
356#endif
357 return cb;
358}
359
360#if 0
361/*
362 * Debugging
363 */
364int errno_func(const char *file, int line)
365{
366 int err = WSAGetLastError();
367 LogRel(("errno=%d (%s:%d)\n", err, file, line));
368 return err;
369}
370#endif
371
372int
373debug_init()
374{
375 int rc = VINF_SUCCESS;
376
377 static int g_fFormatRegistered;
378
379 if (!g_fFormatRegistered)
380 {
381 /*
382 * XXX(r - frank): Move this to IPRT using RTNETADDRIPV4.
383 * Use the specifier %RNAipv4.
384 */
385 rc = RTStrFormatTypeRegister("IP4", print_ipv4_address, NULL);
386 AssertRC(rc);
387 rc = RTStrFormatTypeRegister("ether", print_ether_address, NULL);
388 AssertRC(rc);
389 rc = RTStrFormatTypeRegister("natsock", print_socket, NULL);
390 AssertRC(rc);
391 rc = RTStrFormatTypeRegister("natwinnetevents",
392 print_networkevents, NULL);
393 AssertRC(rc);
394 g_fFormatRegistered = 1;
395 }
396
397 return rc;
398}
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