blob: bfef580694b3062dbd204bd9a4d35a3944acdad6 [file] [log] [blame]
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -08001/*
2 * Copyright (c) 1995 Danny Gasparovski.
3 * Portions copyright (c) 2000 Kelly Price.
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07004 *
5 * Please read the file COPYRIGHT for the
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -08006 * terms and conditions of the copyright.
7 */
8
9#include <slirp.h>
10
11FILE *dfd = NULL;
12#ifdef DEBUG
13int dostats = 1;
14#else
15int dostats = 0;
16#endif
17int slirp_debug = 0;
18
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -070019/* Carry over one item from main.c so that the tty's restored.
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -080020 * Only done when the tty being used is /dev/tty --RedWolf */
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -070021#ifndef CONFIG_QEMU
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -080022extern struct termios slirp_tty_settings;
23extern int slirp_tty_restore;
24
25
26void
27debug_init(file, dbg)
28 char *file;
29 int dbg;
30{
31 /* Close the old debugging file */
32 if (dfd)
33 fclose(dfd);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -070034
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -080035 dfd = fopen(file,"w");
36 if (dfd != NULL) {
37#if 0
38 fprintf(dfd,"Slirp %s - Debugging Started.\n", SLIRP_VERSION);
39#endif
40 fprintf(dfd,"Debugging Started level %i.\r\n",dbg);
41 fflush(dfd);
42 slirp_debug = dbg;
43 } else {
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -070044 lprint("Error: Debugging file \"%s\" could not be opened: %s\r\n",
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -080045 file, strerror(errno));
46 }
47}
48
49/*
50 * Dump a packet in the same format as tcpdump -x
51 */
52#ifdef DEBUG
53void
54dump_packet(dat, n)
55 void *dat;
56 int n;
57{
58 u_char *pptr = (u_char *)dat;
59 int j,k;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -070060
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -080061 n /= 16;
62 n++;
63 DEBUG_MISC((dfd, "PACKET DUMPED: \n"));
64 for(j = 0; j < n; j++) {
65 for(k = 0; k < 6; k++)
66 DEBUG_MISC((dfd, "%02x ", *pptr++));
67 DEBUG_MISC((dfd, "\n"));
68 fflush(dfd);
69 }
70}
71#endif
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -070072#endif
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -080073
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -070074#ifdef LOG_ENABLED
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -080075#if 0
76/*
77 * Statistic routines
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -070078 *
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -080079 * These will print statistics to the screen, the debug file (dfd), or
80 * a buffer, depending on "type", so that the stats can be sent over
81 * the link as well.
82 */
83
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -070084static void
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -080085ttystats(ttyp)
86 struct ttys *ttyp;
87{
88 struct slirp_ifstats *is = &ttyp->ifstats;
89 char buff[512];
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -070090
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -080091 lprint(" \r\n");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -070092
93 if (IF_COMP & IF_COMPRESS)
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -080094 strcpy(buff, "on");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -070095 else if (IF_COMP & IF_NOCOMPRESS)
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -080096 strcpy(buff, "off");
97 else
98 strcpy(buff, "off (for now)");
99 lprint("Unit %d:\r\n", ttyp->unit);
100 lprint(" using %s encapsulation (VJ compression is %s)\r\n", (
101#ifdef USE_PPP
102 ttyp->proto==PROTO_PPP?"PPP":
103#endif
104 "SLIP"), buff);
105 lprint(" %d baudrate\r\n", ttyp->baud);
106 lprint(" interface is %s\r\n", ttyp->up?"up":"down");
107 lprint(" using fd %d, guardian pid is %d\r\n", ttyp->fd, ttyp->pid);
108#ifndef FULL_BOLT
109 lprint(" towrite is %d bytes\r\n", ttyp->towrite);
110#endif
111 if (ttyp->zeros)
112 lprint(" %d zeros have been typed\r\n", ttyp->zeros);
113 else if (ttyp->ones)
114 lprint(" %d ones have been typed\r\n", ttyp->ones);
115 lprint("Interface stats:\r\n");
116 lprint(" %6d output packets sent (%d bytes)\r\n", is->out_pkts, is->out_bytes);
117 lprint(" %6d output packets dropped (%d bytes)\r\n", is->out_errpkts, is->out_errbytes);
118 lprint(" %6d input packets received (%d bytes)\r\n", is->in_pkts, is->in_bytes);
119 lprint(" %6d input packets dropped (%d bytes)\r\n", is->in_errpkts, is->in_errbytes);
120 lprint(" %6d bad input packets\r\n", is->in_mbad);
121}
122
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700123static void
124allttystats(void)
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -0800125{
126 struct ttys *ttyp;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700127
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -0800128 for (ttyp = ttys; ttyp; ttyp = ttyp->next)
129 ttystats(ttyp);
130}
131#endif
132
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700133static void
134ipstats(void)
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -0800135{
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700136 lprint(" \r\n");
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -0800137
138 lprint("IP stats:\r\n");
139 lprint(" %6d total packets received (%d were unaligned)\r\n",
140 ipstat.ips_total, ipstat.ips_unaligned);
141 lprint(" %6d with incorrect version\r\n", ipstat.ips_badvers);
142 lprint(" %6d with bad header checksum\r\n", ipstat.ips_badsum);
143 lprint(" %6d with length too short (len < sizeof(iphdr))\r\n", ipstat.ips_tooshort);
144 lprint(" %6d with length too small (len < ip->len)\r\n", ipstat.ips_toosmall);
145 lprint(" %6d with bad header length\r\n", ipstat.ips_badhlen);
146 lprint(" %6d with bad packet length\r\n", ipstat.ips_badlen);
147 lprint(" %6d fragments received\r\n", ipstat.ips_fragments);
148 lprint(" %6d fragments dropped\r\n", ipstat.ips_fragdropped);
149 lprint(" %6d fragments timed out\r\n", ipstat.ips_fragtimeout);
150 lprint(" %6d packets reassembled ok\r\n", ipstat.ips_reassembled);
151 lprint(" %6d outgoing packets fragmented\r\n", ipstat.ips_fragmented);
152 lprint(" %6d total outgoing fragments\r\n", ipstat.ips_ofragments);
153 lprint(" %6d with bad protocol field\r\n", ipstat.ips_noproto);
154 lprint(" %6d total packets delivered\r\n", ipstat.ips_delivered);
155}
156
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700157#ifndef CONFIG_QEMU
158static void
159vjstats(void)
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -0800160{
161 lprint(" \r\n");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700162
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -0800163 lprint("VJ compression stats:\r\n");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700164
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -0800165 lprint(" %6d outbound packets (%d compressed)\r\n",
166 comp_s.sls_packets, comp_s.sls_compressed);
167 lprint(" %6d searches for connection stats (%d misses)\r\n",
168 comp_s.sls_searches, comp_s.sls_misses);
169 lprint(" %6d inbound uncompressed packets\r\n", comp_s.sls_uncompressedin);
170 lprint(" %6d inbound compressed packets\r\n", comp_s.sls_compressedin);
171 lprint(" %6d inbound unknown type packets\r\n", comp_s.sls_errorin);
172 lprint(" %6d inbound packets tossed due to error\r\n", comp_s.sls_tossed);
173}
174#endif
175
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700176static void
177tcpstats(void)
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -0800178{
179 lprint(" \r\n");
180
181 lprint("TCP stats:\r\n");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700182
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -0800183 lprint(" %6d packets sent\r\n", tcpstat.tcps_sndtotal);
184 lprint(" %6d data packets (%d bytes)\r\n",
185 tcpstat.tcps_sndpack, tcpstat.tcps_sndbyte);
186 lprint(" %6d data packets retransmitted (%d bytes)\r\n",
187 tcpstat.tcps_sndrexmitpack, tcpstat.tcps_sndrexmitbyte);
188 lprint(" %6d ack-only packets (%d delayed)\r\n",
189 tcpstat.tcps_sndacks, tcpstat.tcps_delack);
190 lprint(" %6d URG only packets\r\n", tcpstat.tcps_sndurg);
191 lprint(" %6d window probe packets\r\n", tcpstat.tcps_sndprobe);
192 lprint(" %6d window update packets\r\n", tcpstat.tcps_sndwinup);
193 lprint(" %6d control (SYN/FIN/RST) packets\r\n", tcpstat.tcps_sndctrl);
194 lprint(" %6d times tcp_output did nothing\r\n", tcpstat.tcps_didnuttin);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700195
196 lprint(" %6d packets received\r\n", tcpstat.tcps_rcvtotal);
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -0800197 lprint(" %6d acks (for %d bytes)\r\n",
198 tcpstat.tcps_rcvackpack, tcpstat.tcps_rcvackbyte);
199 lprint(" %6d duplicate acks\r\n", tcpstat.tcps_rcvdupack);
200 lprint(" %6d acks for unsent data\r\n", tcpstat.tcps_rcvacktoomuch);
201 lprint(" %6d packets received in sequence (%d bytes)\r\n",
202 tcpstat.tcps_rcvpack, tcpstat.tcps_rcvbyte);
203 lprint(" %6d completely duplicate packets (%d bytes)\r\n",
204 tcpstat.tcps_rcvduppack, tcpstat.tcps_rcvdupbyte);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700205
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -0800206 lprint(" %6d packets with some duplicate data (%d bytes duped)\r\n",
207 tcpstat.tcps_rcvpartduppack, tcpstat.tcps_rcvpartdupbyte);
208 lprint(" %6d out-of-order packets (%d bytes)\r\n",
209 tcpstat.tcps_rcvoopack, tcpstat.tcps_rcvoobyte);
210 lprint(" %6d packets of data after window (%d bytes)\r\n",
211 tcpstat.tcps_rcvpackafterwin, tcpstat.tcps_rcvbyteafterwin);
212 lprint(" %6d window probes\r\n", tcpstat.tcps_rcvwinprobe);
213 lprint(" %6d window update packets\r\n", tcpstat.tcps_rcvwinupd);
214 lprint(" %6d packets received after close\r\n", tcpstat.tcps_rcvafterclose);
215 lprint(" %6d discarded for bad checksums\r\n", tcpstat.tcps_rcvbadsum);
216 lprint(" %6d discarded for bad header offset fields\r\n",
217 tcpstat.tcps_rcvbadoff);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700218
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -0800219 lprint(" %6d connection requests\r\n", tcpstat.tcps_connattempt);
220 lprint(" %6d connection accepts\r\n", tcpstat.tcps_accepts);
221 lprint(" %6d connections established (including accepts)\r\n", tcpstat.tcps_connects);
222 lprint(" %6d connections closed (including %d drop)\r\n",
223 tcpstat.tcps_closed, tcpstat.tcps_drops);
224 lprint(" %6d embryonic connections dropped\r\n", tcpstat.tcps_conndrops);
225 lprint(" %6d segments we tried to get rtt (%d succeeded)\r\n",
226 tcpstat.tcps_segstimed, tcpstat.tcps_rttupdated);
227 lprint(" %6d retransmit timeouts\r\n", tcpstat.tcps_rexmttimeo);
228 lprint(" %6d connections dropped by rxmt timeout\r\n",
229 tcpstat.tcps_timeoutdrop);
230 lprint(" %6d persist timeouts\r\n", tcpstat.tcps_persisttimeo);
231 lprint(" %6d keepalive timeouts\r\n", tcpstat.tcps_keeptimeo);
232 lprint(" %6d keepalive probes sent\r\n", tcpstat.tcps_keepprobe);
233 lprint(" %6d connections dropped by keepalive\r\n", tcpstat.tcps_keepdrops);
234 lprint(" %6d correct ACK header predictions\r\n", tcpstat.tcps_predack);
235 lprint(" %6d correct data packet header predictions\n", tcpstat.tcps_preddat);
236 lprint(" %6d TCP cache misses\r\n", tcpstat.tcps_socachemiss);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700237
238
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -0800239/* lprint(" Packets received too short: %d\r\n", tcpstat.tcps_rcvshort); */
240/* lprint(" Segments dropped due to PAWS: %d\r\n", tcpstat.tcps_pawsdrop); */
241
242}
243
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700244static void
245udpstats(void)
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -0800246{
247 lprint(" \r\n");
248
249 lprint("UDP stats:\r\n");
250 lprint(" %6d datagrams received\r\n", udpstat.udps_ipackets);
251 lprint(" %6d with packets shorter than header\r\n", udpstat.udps_hdrops);
252 lprint(" %6d with bad checksums\r\n", udpstat.udps_badsum);
253 lprint(" %6d with data length larger than packet\r\n", udpstat.udps_badlen);
254 lprint(" %6d UDP socket cache misses\r\n", udpstat.udpps_pcbcachemiss);
255 lprint(" %6d datagrams sent\r\n", udpstat.udps_opackets);
256}
257
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700258static void
259icmpstats(void)
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -0800260{
261 lprint(" \r\n");
262 lprint("ICMP stats:\r\n");
263 lprint(" %6d ICMP packets received\r\n", icmpstat.icps_received);
264 lprint(" %6d were too short\r\n", icmpstat.icps_tooshort);
265 lprint(" %6d with bad checksums\r\n", icmpstat.icps_checksum);
266 lprint(" %6d with type not supported\r\n", icmpstat.icps_notsupp);
267 lprint(" %6d with bad type feilds\r\n", icmpstat.icps_badtype);
268 lprint(" %6d ICMP packets sent in reply\r\n", icmpstat.icps_reflect);
269}
270
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700271static void
272mbufstats(void)
273{
274 struct mbuf *m;
275 int i;
276
277 lprint(" \r\n");
278
279 lprint("Mbuf stats:\r\n");
280
281 lprint(" %6d mbufs allocated (%d max)\r\n", mbuf_alloced, mbuf_max);
282
283 i = 0;
284 for (m = m_freelist.m_next; m != &m_freelist; m = m->m_next)
285 i++;
286 lprint(" %6d mbufs on free list\r\n", i);
287
288 i = 0;
289 for (m = m_usedlist.m_next; m != &m_usedlist; m = m->m_next)
290 i++;
291 lprint(" %6d mbufs on used list\r\n", i);
292 lprint(" %6d mbufs queued as packets\r\n\r\n", if_queued);
293}
294
295static void
296sockstats(void)
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -0800297{
298 char buff[256];
299 int n;
300 struct socket *so;
301
302 lprint(" \r\n");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700303
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -0800304 lprint(
305 "Proto[state] Sock Local Address, Port Remote Address, Port RecvQ SendQ\r\n");
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700306
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -0800307 for (so = tcb.so_next; so != &tcb; so = so->so_next) {
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700308
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -0800309 n = sprintf(buff, "tcp[%s]", so->so_tcpcb?tcpstates[so->so_tcpcb->t_state]:"NONE");
310 while (n < 17)
311 buff[n++] = ' ';
312 buff[17] = 0;
313 lprint("%s %3d %15s %5d ",
314 buff, so->s,
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700315 inet_ntoa(so->so_laddr), ntohs(so->so_lport));
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -0800316 lprint("%15s %5d %5d %5d\r\n",
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700317 inet_ntoa(so->so_faddr), ntohs(so->so_fport),
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -0800318 so->so_rcv.sb_cc, so->so_snd.sb_cc);
319 }
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700320
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -0800321 for (so = udb.so_next; so != &udb; so = so->so_next) {
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700322
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -0800323 n = sprintf(buff, "udp[%d sec]", (so->so_expire - curtime) / 1000);
324 while (n < 17)
325 buff[n++] = ' ';
326 buff[17] = 0;
327 lprint("%s %3d %15s %5d ",
328 buff, so->s,
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700329 inet_ntoa(so->so_laddr), ntohs(so->so_lport));
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -0800330 lprint("%15s %5d %5d %5d\r\n",
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700331 inet_ntoa(so->so_faddr), ntohs(so->so_fport),
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -0800332 so->so_rcv.sb_cc, so->so_snd.sb_cc);
333 }
334}
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700335#endif
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -0800336
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700337#ifndef CONFIG_QEMU
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -0800338void
339slirp_exit(exit_status)
340 int exit_status;
341{
342 struct ttys *ttyp;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700343
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -0800344 DEBUG_CALL("slirp_exit");
345 DEBUG_ARG("exit_status = %d", exit_status);
346
347 if (dostats) {
348 lprint_print = (int (*) _P((void *, const char *, va_list)))vfprintf;
349 if (!dfd)
350 debug_init("slirp_stats", 0xf);
351 lprint_arg = (char **)&dfd;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700352
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -0800353 ipstats();
354 tcpstats();
355 udpstats();
356 icmpstats();
357 mbufstats();
358 sockstats();
359 allttystats();
360 vjstats();
361 }
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700362
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -0800363 for (ttyp = ttys; ttyp; ttyp = ttyp->next)
364 tty_detached(ttyp, 1);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700365
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -0800366 if (slirp_forked) {
367 /* Menendez time */
368 if (kill(getppid(), SIGQUIT) < 0)
369 lprint("Couldn't kill parent process %ld!\n",
370 (long) getppid());
371 }
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700372
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -0800373 /* Restore the terminal if we gotta */
374 if(slirp_tty_restore)
375 tcsetattr(0,TCSANOW, &slirp_tty_settings); /* NOW DAMMIT! */
376 exit(exit_status);
377}
378#endif
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700379
380void
381slirp_stats(void)
382{
383#ifdef LOG_ENABLED
384 ipstats();
385 tcpstats();
386 udpstats();
387 icmpstats();
388 mbufstats();
389 sockstats();
390#else
391 lprint("SLIRP statistics code not compiled.\n");
392#endif
393}