VirtualBox

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

Last change on this file since 37938 was 37938, checked in by vboxsync, 13 years ago

NAT: logs, don't assert.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 15.5 KB
Line 
1/* $Id: debug.c 37938 2011-07-14 04:28:46Z 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#ifndef STRINGIFY
38# define STRINGIFY(x) #x
39#endif
40
41static char *g_apszTcpStates[TCP_NSTATES] =
42{
43 STRINGIFY(TCPS_CLOSED),
44 STRINGIFY(TCPS_LISTEN),
45 STRINGIFY(TCPS_SYN_SENT),
46 STRINGIFY(TCPS_SYN_RECEIVED),
47 STRINGIFY(TCPS_ESTABLISHED),
48 STRINGIFY(TCPS_CLOSE_WAIT),
49 STRINGIFY(TCPS_FIN_WAIT_1),
50 STRINGIFY(TCPS_CLOSING),
51 STRINGIFY(TCPS_LAST_ACK),
52 STRINGIFY(TCPS_FIN_WAIT_2),
53 STRINGIFY(TCPS_TIME_WAIT)
54};
55
56/*
57 * Dump a packet in the same format as tcpdump -x
58 */
59#ifdef DEBUG
60void
61dump_packet(void *dat, int n)
62{
63 Log(("nat: PACKET DUMPED:\n%.*Rhxd\n", n, dat));
64}
65#endif
66
67#ifdef LOG_ENABLED
68static void
69lprint(const char *pszFormat, ...)
70{
71 va_list args;
72 va_start(args, pszFormat);
73 RTLogPrintfV(pszFormat, args);
74 va_end(args);
75}
76
77void
78ipstats(PNATState pData)
79{
80 lprint("\n");
81
82 lprint("IP stats:\n");
83 lprint(" %6d total packets received (%d were unaligned)\n",
84 ipstat.ips_total, ipstat.ips_unaligned);
85 lprint(" %6d with incorrect version\n", ipstat.ips_badvers);
86 lprint(" %6d with bad header checksum\n", ipstat.ips_badsum);
87 lprint(" %6d with length too short (len < sizeof(iphdr))\n", ipstat.ips_tooshort);
88 lprint(" %6d with length too small (len < ip->len)\n", ipstat.ips_toosmall);
89 lprint(" %6d with bad header length\n", ipstat.ips_badhlen);
90 lprint(" %6d with bad packet length\n", ipstat.ips_badlen);
91 lprint(" %6d fragments received\n", ipstat.ips_fragments);
92 lprint(" %6d fragments dropped\n", ipstat.ips_fragdropped);
93 lprint(" %6d fragments timed out\n", ipstat.ips_fragtimeout);
94 lprint(" %6d packets reassembled ok\n", ipstat.ips_reassembled);
95 lprint(" %6d outgoing packets fragmented\n", ipstat.ips_fragmented);
96 lprint(" %6d total outgoing fragments\n", ipstat.ips_ofragments);
97 lprint(" %6d with bad protocol field\n", ipstat.ips_noproto);
98 lprint(" %6d total packets delivered\n", ipstat.ips_delivered);
99}
100
101void
102tcpstats(PNATState pData)
103{
104 lprint("\n");
105
106 lprint("TCP stats:\n");
107
108 lprint(" %6d packets sent\n", tcpstat.tcps_sndtotal);
109 lprint(" %6d data packets (%d bytes)\n",
110 tcpstat.tcps_sndpack, tcpstat.tcps_sndbyte);
111 lprint(" %6d data packets retransmitted (%d bytes)\n",
112 tcpstat.tcps_sndrexmitpack, tcpstat.tcps_sndrexmitbyte);
113 lprint(" %6d ack-only packets (%d delayed)\n",
114 tcpstat.tcps_sndacks, tcpstat.tcps_delack);
115 lprint(" %6d URG only packets\n", tcpstat.tcps_sndurg);
116 lprint(" %6d window probe packets\n", tcpstat.tcps_sndprobe);
117 lprint(" %6d window update packets\n", tcpstat.tcps_sndwinup);
118 lprint(" %6d control (SYN/FIN/RST) packets\n", tcpstat.tcps_sndctrl);
119 lprint(" %6d times tcp_output did nothing\n", tcpstat.tcps_didnuttin);
120
121 lprint(" %6d packets received\n", tcpstat.tcps_rcvtotal);
122 lprint(" %6d acks (for %d bytes)\n",
123 tcpstat.tcps_rcvackpack, tcpstat.tcps_rcvackbyte);
124 lprint(" %6d duplicate acks\n", tcpstat.tcps_rcvdupack);
125 lprint(" %6d acks for unsent data\n", tcpstat.tcps_rcvacktoomuch);
126 lprint(" %6d packets received in sequence (%d bytes)\n",
127 tcpstat.tcps_rcvpack, tcpstat.tcps_rcvbyte);
128 lprint(" %6d completely duplicate packets (%d bytes)\n",
129 tcpstat.tcps_rcvduppack, tcpstat.tcps_rcvdupbyte);
130
131 lprint(" %6d packets with some duplicate data (%d bytes duped)\n",
132 tcpstat.tcps_rcvpartduppack, tcpstat.tcps_rcvpartdupbyte);
133 lprint(" %6d out-of-order packets (%d bytes)\n",
134 tcpstat.tcps_rcvoopack, tcpstat.tcps_rcvoobyte);
135 lprint(" %6d packets of data after window (%d bytes)\n",
136 tcpstat.tcps_rcvpackafterwin, tcpstat.tcps_rcvbyteafterwin);
137 lprint(" %6d window probes\n", tcpstat.tcps_rcvwinprobe);
138 lprint(" %6d window update packets\n", tcpstat.tcps_rcvwinupd);
139 lprint(" %6d packets received after close\n", tcpstat.tcps_rcvafterclose);
140 lprint(" %6d discarded for bad checksums\n", tcpstat.tcps_rcvbadsum);
141 lprint(" %6d discarded for bad header offset fields\n",
142 tcpstat.tcps_rcvbadoff);
143
144 lprint(" %6d connection requests\n", tcpstat.tcps_connattempt);
145 lprint(" %6d connection accepts\n", tcpstat.tcps_accepts);
146 lprint(" %6d connections established (including accepts)\n", tcpstat.tcps_connects);
147 lprint(" %6d connections closed (including %d drop)\n",
148 tcpstat.tcps_closed, tcpstat.tcps_drops);
149 lprint(" %6d embryonic connections dropped\n", tcpstat.tcps_conndrops);
150 lprint(" %6d segments we tried to get rtt (%d succeeded)\n",
151 tcpstat.tcps_segstimed, tcpstat.tcps_rttupdated);
152 lprint(" %6d retransmit timeouts\n", tcpstat.tcps_rexmttimeo);
153 lprint(" %6d connections dropped by rxmt timeout\n",
154 tcpstat.tcps_timeoutdrop);
155 lprint(" %6d persist timeouts\n", tcpstat.tcps_persisttimeo);
156 lprint(" %6d keepalive timeouts\n", tcpstat.tcps_keeptimeo);
157 lprint(" %6d keepalive probes sent\n", tcpstat.tcps_keepprobe);
158 lprint(" %6d connections dropped by keepalive\n", tcpstat.tcps_keepdrops);
159 lprint(" %6d correct ACK header predictions\n", tcpstat.tcps_predack);
160 lprint(" %6d correct data packet header predictions\n", tcpstat.tcps_preddat);
161 lprint(" %6d TCP cache misses\n", tcpstat.tcps_socachemiss);
162
163/* lprint(" Packets received too short: %d\n", tcpstat.tcps_rcvshort); */
164/* lprint(" Segments dropped due to PAWS: %d\n", tcpstat.tcps_pawsdrop); */
165
166}
167
168void
169udpstats(PNATState pData)
170{
171 lprint("\n");
172
173 lprint("UDP stats:\n");
174 lprint(" %6d datagrams received\n", udpstat.udps_ipackets);
175 lprint(" %6d with packets shorter than header\n", udpstat.udps_hdrops);
176 lprint(" %6d with bad checksums\n", udpstat.udps_badsum);
177 lprint(" %6d with data length larger than packet\n", udpstat.udps_badlen);
178 lprint(" %6d UDP socket cache misses\n", udpstat.udpps_pcbcachemiss);
179 lprint(" %6d datagrams sent\n", udpstat.udps_opackets);
180}
181
182void
183icmpstats(PNATState pData)
184{
185 lprint("\n");
186 lprint("ICMP stats:\n");
187 lprint(" %6d ICMP packets received\n", icmpstat.icps_received);
188 lprint(" %6d were too short\n", icmpstat.icps_tooshort);
189 lprint(" %6d with bad checksums\n", icmpstat.icps_checksum);
190 lprint(" %6d with type not supported\n", icmpstat.icps_notsupp);
191 lprint(" %6d with bad type feilds\n", icmpstat.icps_badtype);
192 lprint(" %6d ICMP packets sent in reply\n", icmpstat.icps_reflect);
193}
194
195void
196mbufstats(PNATState pData)
197{
198 /*
199 * (vvl) this static code can't work with mbuf zone anymore
200 * @todo: make statistic correct
201 */
202}
203
204void
205sockstats(PNATState pData)
206{
207 char buff[256];
208 size_t n;
209 struct socket *so, *so_next;
210
211 lprint("\n");
212
213 lprint(
214 "Proto[state] Sock Local Address, Port Remote Address, Port RecvQ SendQ\n");
215
216 QSOCKET_FOREACH(so, so_next, tcp)
217 /* { */
218 n = RTStrPrintf(buff, sizeof(buff), "tcp[%s]", so->so_tcpcb?tcpstates[so->so_tcpcb->t_state]:"NONE");
219 while (n < 17)
220 buff[n++] = ' ';
221 buff[17] = 0;
222 lprint("%s %3d %15s %5d ",
223 buff, so->s, inet_ntoa(so->so_laddr), RT_N2H_U16(so->so_lport));
224 lprint("%15s %5d %5d %5d\n",
225 inet_ntoa(so->so_faddr), RT_N2H_U16(so->so_fport),
226 SBUF_LEN(&so->so_rcv), SBUF_LEN(&so->so_snd));
227 LOOP_LABEL(tcp, so, so_next);
228 }
229
230 QSOCKET_FOREACH(so, so_next, udp)
231 /* { */
232 n = RTStrPrintf(buff, sizeof(buff), "udp[%d sec]", (so->so_expire - curtime) / 1000);
233 while (n < 17)
234 buff[n++] = ' ';
235 buff[17] = 0;
236 lprint("%s %3d %15s %5d ",
237 buff, so->s, inet_ntoa(so->so_laddr), RT_N2H_U16(so->so_lport));
238 lprint("%15s %5d %5d %5d\n",
239 inet_ntoa(so->so_faddr), RT_N2H_U16(so->so_fport),
240 SBUF_LEN(&so->so_rcv), SBUF_LEN(&so->so_snd));
241 LOOP_LABEL(udp, so, so_next);
242 }
243}
244#endif
245
246static DECLCALLBACK(size_t)
247print_socket(PFNRTSTROUTPUT pfnOutput, void *pvArgOutput,
248 const char *pszType, void const *pvValue,
249 int cchWidth, int cchPrecision, unsigned fFlags,
250 void *pvUser)
251{
252 struct socket *so = (struct socket*)pvValue;
253 uint32_t ip;
254 struct sockaddr addr;
255 struct sockaddr_in *in_addr;
256 socklen_t socklen = sizeof(struct sockaddr);
257 int status = 0;
258
259 AssertReturn(strcmp(pszType, "natsock") == 0, 0);
260 if (so == NULL)
261 return RTStrFormat(pfnOutput, pvArgOutput, NULL, 0,
262 "socket is null");
263 if (so->so_state == SS_NOFDREF || so->s == -1)
264 return RTStrFormat(pfnOutput, pvArgOutput, NULL, 0,
265 "socket(%d) SS_NOFDREF", so->s);
266
267 status = getsockname(so->s, &addr, &socklen);
268 if(status != 0 || addr.sa_family != AF_INET)
269 {
270 return RTStrFormat(pfnOutput, pvArgOutput, NULL, 0,
271 "socket(%d) is invalid(probably closed)", so->s);
272 }
273
274 in_addr = (struct sockaddr_in *)&addr;
275 return RTStrFormat(pfnOutput, pvArgOutput, NULL, 0, "socket %d:(proto:%u) "
276 "state=%04x "
277 "f_(addr:port)=%RTnaipv4:%d "
278 "l_(addr:port)=%RTnaipv4:%d "
279 "name=%RTnaipv4:%d",
280 so->s, so->so_type, so->so_state,
281 so->so_faddr.s_addr,
282 RT_N2H_U16(so->so_fport),
283 so->so_laddr.s_addr,
284 RT_N2H_U16(so->so_lport),
285 in_addr->sin_addr.s_addr,
286 RT_N2H_U16(in_addr->sin_port));
287}
288
289/**
290 * Print callback dumping TCP Control Block in terms of RFC 793.
291 */
292static DECLCALLBACK(size_t)
293printTcpcbRfc793(PFNRTSTROUTPUT pfnOutput, void *pvArgOutput,
294 const char *pszType, void const *pvValue,
295 int cchWidth, int cchPrecision, unsigned fFlags,
296 void *pvUser)
297{
298 size_t cb = 0;
299 const struct tcpcb *tp = (const struct tcpcb *)pvValue;
300 AssertReturn(RTStrCmp(pszType, "tcpcb793") == 0, 0);
301 if (tp)
302 {
303 cb += RTStrFormat(pfnOutput, pvArgOutput, NULL, 0, "TCB793[ state:%R[tcpstate] SND(UNA: %x, NXT: %x, UP: %x, WND: %x, WL1:%x, WL2:%x, ISS:%x), ",
304 tp->t_state, tp->snd_una, tp->snd_nxt, tp->snd_up, tp->snd_wnd, tp->snd_wl1, tp->snd_wl2, tp->iss);
305 cb += RTStrFormat(pfnOutput, pvArgOutput, NULL, 0, "RCV(WND: %x, NXT: %x, UP: %x, IRS:%x)]", tp->rcv_wnd, tp->rcv_nxt, tp->rcv_up, tp->irs);
306 }
307 else
308 {
309 cb += RTStrFormat(pfnOutput, pvArgOutput, NULL, 0, "TCB793[ NULL ]");
310 }
311 return cb;
312}
313/*
314 * Prints TCP segment in terms of RFC 793.
315 */
316static DECLCALLBACK(size_t)
317printTcpSegmentRfc793(PFNRTSTROUTPUT pfnOutput, void *pvArgOutput,
318 const char *pszType, void const *pvValue,
319 int cchWidth, int cchPrecision, unsigned fFlags,
320 void *pvUser)
321{
322 size_t cb = 0;
323 const struct tcpiphdr *ti = (const struct tcpiphdr *)pvValue;
324 AssertReturn(RTStrCmp(pszType, "tcpseg793") == 0 && ti, 0);
325 cb += RTStrFormat(pfnOutput, pvArgOutput, NULL, 0, "SEG[ACK: %x, SEQ: %x, LEN: %x, WND: %x, UP: %x]",
326 ti->ti_ack, ti->ti_seq, ti->ti_len, ti->ti_win, ti->ti_urp);
327 return cb;
328}
329
330/*
331 * Prints TCP state
332 */
333static DECLCALLBACK(size_t)
334printTcpState(PFNRTSTROUTPUT pfnOutput, void *pvArgOutput,
335 const char *pszType, void const *pvValue,
336 int cchWidth, int cchPrecision, unsigned fFlags,
337 void *pvUser)
338{
339 size_t cb = 0;
340 const int idxTcpState = (int)(uintptr_t)pvValue;
341 char *pszTcpStateName = (idxTcpState >= 0 && idxTcpState < TCP_NSTATES) ? g_apszTcpStates[idxTcpState] : "TCPS_INVALIDE_STATE";
342 AssertReturn(RTStrCmp(pszType, "tcpstate") == 0, 0);
343 cb += RTStrFormat(pfnOutput, pvArgOutput, NULL, 0, "%s", pszTcpStateName);
344 return cb;
345}
346
347/*
348 * Prints sbuf state
349 */
350static DECLCALLBACK(size_t)
351printSbuf(PFNRTSTROUTPUT pfnOutput, void *pvArgOutput,
352 const char *pszType, void const *pvValue,
353 int cchWidth, int cchPrecision, unsigned fFlags,
354 void *pvUser)
355{
356 size_t cb = 0;
357 const struct sbuf *sb = (struct sbuf *)pvValue;
358 AssertReturn(RTStrCmp(pszType, "sbuf") == 0, 0);
359 cb += RTStrFormat(pfnOutput, pvArgOutput, NULL, 0, "[sbuf:%p cc:%d, datalen:%d, wprt:%p, rptr:%p data:%p]",
360 sb, sb->sb_cc, sb->sb_datalen, sb->sb_wptr, sb->sb_rptr, sb->sb_data);
361 return cb;
362}
363
364static DECLCALLBACK(size_t)
365print_networkevents(PFNRTSTROUTPUT pfnOutput, void *pvArgOutput,
366 const char *pszType, void const *pvValue,
367 int cchWidth, int cchPrecision, unsigned fFlags,
368 void *pvUser)
369{
370 size_t cb = 0;
371#ifdef RT_OS_WINDOWS
372 WSANETWORKEVENTS *pNetworkEvents = (WSANETWORKEVENTS*)pvValue;
373 bool fDelim = false;
374
375 AssertReturn(strcmp(pszType, "natwinnetevents") == 0, 0);
376
377 cb += RTStrFormat(pfnOutput, pvArgOutput, NULL, 0, "events=%02x (",
378 pNetworkEvents->lNetworkEvents);
379# define DO_BIT(bit) \
380 if (pNetworkEvents->lNetworkEvents & FD_ ## bit) \
381 { \
382 cb += RTStrFormat(pfnOutput, pvArgOutput, NULL, 0, \
383 "%s" #bit "(%d)", fDelim ? "," : "", \
384 pNetworkEvents->iErrorCode[FD_ ## bit ## _BIT]); \
385 fDelim = true; \
386 }
387 DO_BIT(READ);
388 DO_BIT(WRITE);
389 DO_BIT(OOB);
390 DO_BIT(ACCEPT);
391 DO_BIT(CONNECT);
392 DO_BIT(CLOSE);
393 DO_BIT(QOS);
394# undef DO_BIT
395 cb += RTStrFormat(pfnOutput, pvArgOutput, NULL, 0, ")");
396#endif
397 return cb;
398}
399
400#if 0
401/*
402 * Debugging
403 */
404int errno_func(const char *file, int line)
405{
406 int err = WSAGetLastError();
407 LogRel(("errno=%d (%s:%d)\n", err, file, line));
408 return err;
409}
410#endif
411
412int
413debug_init()
414{
415 int rc = VINF_SUCCESS;
416
417 static int g_fFormatRegistered;
418
419 if (!g_fFormatRegistered)
420 {
421
422 rc = RTStrFormatTypeRegister("natsock", print_socket, NULL);
423 AssertRC(rc);
424 rc = RTStrFormatTypeRegister("natwinnetevents",
425 print_networkevents, NULL);
426 AssertRC(rc);
427 rc = RTStrFormatTypeRegister("tcpcb793", printTcpcbRfc793, NULL);
428 AssertRC(rc);
429 rc = RTStrFormatTypeRegister("tcpseg793", printTcpSegmentRfc793, NULL);
430 AssertRC(rc);
431 rc = RTStrFormatTypeRegister("tcpstate", printTcpState, NULL);
432 AssertRC(rc);
433 rc = RTStrFormatTypeRegister("sbuf", printSbuf, NULL);
434 AssertRC(rc);
435 g_fFormatRegistered = 1;
436 }
437
438 return rc;
439}
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