VirtualBox

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

最後變更 在這個檔案從39285是 39285,由 vboxsync 提交於 13 年 前

NAT: debug removes SS_NODEREF from condition preventing detailed dumping state of socket. In UDP case it's rather frequent situation,

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

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