VirtualBox

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

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

NAT: routines printing TCB and TCP segment.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 15.0 KB
Line 
1/* $Id: debug.c 34305 2010-11-24 06:21:52Z 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 SBUF_LEN(&so->so_rcv), SBUF_LEN(&so->so_snd));
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 SBUF_LEN(&so->so_rcv), SBUF_LEN(&so->so_snd));
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_NOFDREF", so->s);
282
283 status = getsockname(so->s, &addr, &socklen);
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 %d:(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
301/**
302 * Print callback dumping TCP Control Block in terms of RFC 793.
303 */
304static DECLCALLBACK(size_t)
305printTcpcbRfc793(PFNRTSTROUTPUT pfnOutput, void *pvArgOutput,
306 const char *pszType, void const *pvValue,
307 int cchWidth, int cchPrecision, unsigned fFlags,
308 void *pvUser)
309{
310 size_t cb = 0;
311 const struct tcpcb *tp = (const struct tcpcb *)pvValue;
312 AssertReturn(RTStrCmp(pszType, "tcpcb793") == 0 && tp, 0);
313 cb += RTStrFormat(pfnOutput, pvArgOutput, NULL, 0, "TCB793[ SND(UNA: %x, NXT: %x, UP: %x, WND: %x, WL1:%x, WL2:%x, ISS:%x), ",
314 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 return cb;
317}
318/*
319 * Prints TCP segment in terms of RFC 793.
320 */
321static DECLCALLBACK(size_t)
322printTcpSegmentRfc793(PFNRTSTROUTPUT pfnOutput, void *pvArgOutput,
323 const char *pszType, void const *pvValue,
324 int cchWidth, int cchPrecision, unsigned fFlags,
325 void *pvUser)
326{
327 size_t cb = 0;
328 const struct tcpiphdr *ti = (const struct tcpiphdr *)pvValue;
329 AssertReturn(RTStrCmp(pszType, "tcpseg793") == 0 && ti, 0);
330 cb += RTStrFormat(pfnOutput, pvArgOutput, NULL, 0, "SEG[ACK: %x, SEQ: %x, LEN: %x, WND: %x, UP: %x]",
331 ti->ti_ack, ti->ti_seq, ti->ti_len, ti->ti_win, ti->ti_urp);
332 return cb;
333}
334
335static DECLCALLBACK(size_t)
336print_networkevents(PFNRTSTROUTPUT pfnOutput, void *pvArgOutput,
337 const char *pszType, void const *pvValue,
338 int cchWidth, int cchPrecision, unsigned fFlags,
339 void *pvUser)
340{
341 size_t cb = 0;
342#ifdef RT_OS_WINDOWS
343 WSANETWORKEVENTS *pNetworkEvents = (WSANETWORKEVENTS*)pvValue;
344 bool fDelim = false;
345
346 AssertReturn(strcmp(pszType, "natwinnetevents") == 0, 0);
347
348 cb += RTStrFormat(pfnOutput, pvArgOutput, NULL, 0, "events=%02x (",
349 pNetworkEvents->lNetworkEvents);
350# define DO_BIT(bit) \
351 if (pNetworkEvents->lNetworkEvents & FD_ ## bit) \
352 { \
353 cb += RTStrFormat(pfnOutput, pvArgOutput, NULL, 0, \
354 "%s" #bit "(%d)", fDelim ? "," : "", \
355 pNetworkEvents->iErrorCode[FD_ ## bit ## _BIT]); \
356 fDelim = true; \
357 }
358 DO_BIT(READ);
359 DO_BIT(WRITE);
360 DO_BIT(OOB);
361 DO_BIT(ACCEPT);
362 DO_BIT(CONNECT);
363 DO_BIT(CLOSE);
364 DO_BIT(QOS);
365# undef DO_BIT
366 cb += RTStrFormat(pfnOutput, pvArgOutput, NULL, 0, ")");
367#endif
368 return cb;
369}
370
371#if 0
372/*
373 * Debugging
374 */
375int errno_func(const char *file, int line)
376{
377 int err = WSAGetLastError();
378 LogRel(("errno=%d (%s:%d)\n", err, file, line));
379 return err;
380}
381#endif
382
383int
384debug_init()
385{
386 int rc = VINF_SUCCESS;
387
388 static int g_fFormatRegistered;
389
390 if (!g_fFormatRegistered)
391 {
392 /*
393 * XXX(r - frank): Move this to IPRT using RTNETADDRIPV4.
394 * Use the specifier %RNAipv4.
395 */
396 rc = RTStrFormatTypeRegister("IP4", print_ipv4_address, NULL);
397 AssertRC(rc);
398 rc = RTStrFormatTypeRegister("ether", print_ether_address, NULL);
399 AssertRC(rc);
400 rc = RTStrFormatTypeRegister("natsock", print_socket, NULL);
401 AssertRC(rc);
402 rc = RTStrFormatTypeRegister("natwinnetevents",
403 print_networkevents, NULL);
404 AssertRC(rc);
405 rc = RTStrFormatTypeRegister("tcpcb793", printTcpcbRfc793, NULL);
406 AssertRC(rc);
407 rc = RTStrFormatTypeRegister("tcpseg793", printTcpSegmentRfc793, NULL);
408 AssertRC(rc);
409 g_fFormatRegistered = 1;
410 }
411
412 return rc;
413}
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