Debian Codegraph @

testsuite/systemtap.examples/network/tcpipstat.stp

raw
1 #! /usr/bin/env stap
2 /*
3 * Copyright (C) 2009 IBM Corp.
4 * This file is part of systemtap, and is free software. You can
5 * redistribute it and/or modify it under the terms of the GNU General
6 * Public License (GPL); either version 2, or (at your option) any
7 * later version.
8 *
9 * Version 1.0 [email protected] 2009-07-06
10 *
11 * Name:
12 * tcpipstat.stp - Per-socket tcpip statistics collection facility.
13 * (netstat -s on steroids)
14 *
15 * Description:
16 * The purpose of tcpipstat is to collect and display network statistics
17 * related to individual TCP sockets or groups of sockets. The statistics
18 * that are collected are simmer to that of the command netstat -s, only
19 * sorted and grouped by individual sockets. The statistics collected are
20 * based on the SNMP TCP and IP MIBS. As the name implies this command
21 * collects data relating to TCP sockets, not UDP sockets. I plan to
22 * support a UDP tool in a later version. The tcpipstat tool uses
23 * systemtap to collect data from the linux kernel; see the stap command
24 * manual page for instructions and options available in systemtap. For
25 * more information on the systemtap project see:
26 * http://sourceware.org/systemtap/documentation.html
27 *
28 *
29 * Synopsis:
30 * tcpstat.stp [index=laddr|raddr|lport|rport|tuple]\
31 * [timeout=<N sec>]\
32 * [nozeros=1|0]\
33 * [filters...]
34 *
35 * index (optional) tcpipstat collects, sorts and displays network
36 * statistics into buckets. The buckets are indexed based on
37 * one of several index types. By default the index is
38 * constructed from all four elements of the socket tuple,
39 * providing a separate bucket for each socket. However, any
40 * individual element of the tuple can be used as the index.
41 * Valid values for index are laddr (local address), raddr
42 * (remote address) , lport, rport or tuple. For example if
43 * index=lport is specified network statistics will be collected
44 * and reported based on the value of the local port thus
45 * providing statistics related to each network service provided
46 * by the system.
47 *
48 * timeout (optional) When a timeout value (in seconds) is specified
49 * tcpstat will automatically terminate it's run at the end of
50 * the specified time and produce a report. When timeout is
51 * omitted the script will run until the user terminates it by
52 * typing a ^c.
53 *
54 * onclose (optional) In onclose mode (onclose=1) statistics are reported
55 * only when a socket closes. The generated report reflect
56 * statistics for the entire life on the socket, assuming
57 * tcpipstat was running at the time the socket was opened.
58 * The option index=tuple must be set for onclode to work
59 * (index=tuple is default).
60 *
61 * nozeros (optional) When nonzero=1 is specified statistics with
62 * a value of zero will not be printed.
63 *
64 * filters (optional) By default statistics are counted for every socket
65 * on the system. Address filters can be used to limit what
66 * sockets data is collected for. All filters are
67 * pass-through filters. Multiple filters may be given separated
68 * by a space. When multiple filters are given an event only
69 * needs to be accepted by one filter to be counted. A wild-card
70 * (*) value can be used for any component of the filter.
71 *
72 * The format of a filter is:
73 * <local-address>:<local-port>-<remote address>:<remote-port>
74 *
75 * Addresses are specified as ipv4 dot notation address. Ports
76 * are specified as decimal numbers. A "*" character can be used
77 * in any field to indicate a wild-card value. If no filters are
78 * given the following wild-card filter is automatically used:
79 * *.*.*.*:*-*.*.*.*:*
80 *
81 * Known Bugs:
82 * Not all MIB values are currently supported.
83 * Ipv6 in not yet supported.
84 *
85 * Examples:
86 * Here are some ways that tcpipstat could be used.
87 *
88 * List per-socket statistics for every active socket for the
89 * next 60 seconds.
90 * $ tcpipstat timeout=60
91 *
92 * Who is talking to my webserver?
93 * $ tcpipstat.stp index=raddr *.*.*.*:80-*.*.*.*:*
94 *
95 * What services (on this system) is host 192.168.1.103 using
96 * $ tcpipstat.stp index=rport *.*.*.*:*-192.168.1.103:*
97 *
98 * What hosts in the 9.0.0.0 network is this system communicating
99 * with?
100 * $ tcpipstat.stp index=raddr *.*.*.*:*-9.*.*.*:*
101 *
102 * What ports are people connecting to? or which services are busy?
103 * $ tcpipstat.stp index=lport
104 *
105 * Show network statistics related to a single run of netperf.
106 * $ stap -c netperf -H <server-ip>\
107 * tcpipstat.stp *.*.*.*:*-<server-ip>:*
108 *
109 */
110
111 global filter;
112 global number_of_filters;
113
114 global key_list;
115 global lastkey;
116
117 // Command arguments and default values
118 global index= "tuple";
119 global timeout = -1;
120 global nozeros = 1;
121 global onclose = 0;
122
123 probe begin {
124
125 number_of_filters=process_cmdline()
126
127 if ( index != "laddr" &&
128 index != "raddr" &&
129 index != "lport" &&
130 index != "rport" &&
131 index != "tuple" ) usage("Invalid index value!");
132
133 if ( onclose && (index!="tuple") )
134 usage("onclose must be used with index=tuple!");
135
136 /* The user did not supply a filter, build a wild-card filter */
137 if ( number_of_filters == 0 ){
138 number_of_filters++;
139 j=6;
140 filter[j] = ipv4_pton("*.*.*.*",0)
141 filter[j+1] = ipv4_pton("*.*.*.*",1)
142 filter[j+2] = ipv4_portton("*")
143 filter[j+3] = ipv4_pton("*.*.*.*",0)
144 filter[j+4] = ipv4_pton("*.*.*.*",1)
145 filter[j+5] = ipv4_portton("*")
146 }
147
148 if ( number_of_filters < 0 ){
149 usage("Bad filter format!");
150 }
151
152 printf("Indexing collected stats using %s values\n",index);
153 for (i=1; i <= number_of_filters; i++){
154 print_filter(i);
155 }
156 }
157
158 /* All command line arguments other than the filters are processed
159 * first and must be placed on the command line prior to any filters.
160 */
161 function process_cmdline:long ()
162 {
163 filter_number=0;
164 for (i=1; i <= argc; i++){
165 argument= tokenize(argv[i], "=")
166
167 if ( argument == "index" ){
168 argv[i]=""
169 index=tokenize(argv[i], "=")
170 continue;
171 }
172
173 if ( argument == "timeout" ){
174 argv[i]=""
175 timeout=strtol(tokenize(argv[i], "="),10)
176 continue;
177 }
178
179 if ( argument == "nozeros" ){
180 argv[i]=""
181 nozeros=strtol(tokenize(argv[i], "="),10)
182 continue;
183 }
184
185 if ( argument == "onclose" ){
186 argv[i]=""
187 onclose=strtol(tokenize(argv[i], "="),10)
188 continue;
189 }
190
191 /* Example: adding more option flags
192 if ( argument == "stuff" ){
193 argv[i]=""
194 stuff=tokenize(argv[i], "=")
195 continue;
196 }
197 */
198
199 /* Anything on the command line after this point must
200 * be a filter.
201 */
202 local = tokenize(argv[i], "-")
203 argv[i] = ""
204 remote = tokenize(argv[i], "-")
205
206 local_addr = tokenize(local, ":")
207 local=""
208 local_port = tokenize(local, ":")
209
210 remote_addr = tokenize(remote, ":")
211 remote=""
212 remote_port = tokenize(remote, ":")
213
214 /* stap bug */
215 if ( remote_port == "fobar") i=i;
216
217 ++filter_number;
218 j=filter_number*6;
219 filter[j] = ipv4_pton(local_addr,0) // Local address
220 filter[j+1] = ipv4_pton(local_addr,1) // Local address mask
221 filter[j+2] = ipv4_portton(local_port) // Local port
222 filter[j+3] = ipv4_pton(remote_addr,0) // Remote address
223 filter[j+4] = ipv4_pton(remote_addr,1) // Remote address mask
224 filter[j+5] = ipv4_portton(remote_port) // Remote port
225
226 if (filter[j]< -1 ||
227 filter[j+1] < -1 ||
228 filter[j+2] < -1 ||
229 filter[j+3] < -1 ||
230 filter[j+4] < -1 ||
231 filter[j+5] < -1 ) return -1;
232 }
233 return filter_number;
234 }
235
236 /*
237 * Convert an ascii integer values between 0 and 65534 to a u16 port number.
238 * "*" are treated as wildcards and will be converted to 0xffff (65535).
239 */
240 function ipv4_portton:long (port:string)
241 {
242 if ( port == "*" ) port="65535";
243 pport=strtol(port,10);
244 if ( pport > 0xffff ){
245 printf("Bad port number %s\n",port)
246 return -22;
247 }
248 return pport
249 }
250
251 /*
252 * Convert an ipv4 dot notation address into longs.
253 * Supports "*" in any field treating it as a wildcard by making the byte=0.
254 * If make_mask is set, it creates a mask based on the "*" fields. All non='*'
255 * bytes are set to 0xff all * fields are set to 0x0;.
256 */
257 function ipv4_pton:long (addr:string, make_mask:long)
258 {
259 i=32;
260 ip=0;
261 ips=addr;
262 while(strlen(byte = tokenize(ips, ".")) != 0) {
263 i-=8;
264 ips="";
265
266 if ( byte == "*" ){
267 byte = "0"
268 } else {
269 if ( make_mask ) byte = "255";
270 }
271
272 j=strtol(byte,10);
273 if ( j > 255 ){
274 printf("bad address %s\n",addr)
275 return -22;
276 }
277 ip=ip+(j<<i) // left shift the byte into the address
278 }
279 if ( i != 0 ){
280 printf("bad address %s\n",addr)
281 return -22;
282 }
283 return ip;
284 }
285
286 function usage (msg:string)
287 {
288 printf("\nUsage:\n");
289 printf("\ttcpipstat.stp\t[index=laddr|raddr|lport|rport|tuple]\n");
290 printf("\t\t\t[timeout=<sec>] [nozeros=0|1] [onclose=0|1]\n");
291 printf("\t\t\t[filter ......]\n\n");
292 printf("\tfilter format:\n");
293 printf("\t<local ip-address>:<local-port>-<remote ip-address>:<remote-port>\n\n");
294 error(msg);
295 }
296
297 /* Print filter number n. This is helpful for debugging */
298 function print_filter (n:long)
299 {
300 j=n*6;
301 printf("Processed filter #%d = %s[0x%x]:%d --> %s[0x%x]:%d \n",
302 n,ip_ntop(htonl(filter[j])), filter[j+1], filter[j+2],
303 ip_ntop(htonl(filter[j+3])), filter[j+4], filter[j+5]);
304 }
305
306 /*
307 * Returns a unique value (stored in the global key_list) based on the socket
308 * address tuple and the global collection index value. A new value is created
309 * if one does not already exist.
310 */
311 function build_key:long (laddr:long, raddr:long, lport:long, rport:long)
312 {
313 if ( index == "laddr" ) raddr = lport = rport = 0
314 if ( index == "raddr" ) laddr = lport = rport = 0
315 if ( index == "lport" ) laddr = raddr = rport = 0
316 if ( index == "rport" ) laddr = raddr = lport = 0
317
318 if ( key_list[laddr, raddr, lport, rport] )
319 return key_list[laddr, raddr, lport, rport]
320 else
321 return key_list[laddr, raddr, lport, rport] = ++lastkey
322 }
323
324 /*
325 * This is where the real work of the probe filtering is done.
326 * Important: this function is run for every probe hit so
327 * keep it small and fast!
328 *
329 * If the probe passes through the filters a "key" value is
330 * returned otherwise it returns zero.
331 */
332 function filter_key:long (laddr:long, raddr:long, lport:long, rport:long)
333 {
334 for (i=1; i <= number_of_filters; i++){
335 j=i*6;
336
337 /*
338 printf("\n %s\n",pp())
339 print_filter(i);
340 printf("local=0x%x:0x%x remote=0x%x:0x%x ",
341 laddr, lport, raddr, rport);
342 */
343
344 // Local filter
345 local_filter=remote_filter=0;
346 if ( (laddr&filter[j+1]) == filter[j] ) {
347 if ( (filter[j+2] == 0xffff) || (lport == filter[j+2]))
348 local_filter = 1;
349 }
350 // Remote filter
351 if ( (raddr&filter[j+4]) == filter[j+3] ) {
352 if ( (filter[j+5] == 0xffff) || (rport == filter[j+5]))
353 remote_filter = 1;
354 }
355
356 // printf("local=%d remote=%d ",local_filter,remote_filter);
357
358 if(local_filter && remote_filter){
359 // key = build_key(laddr, raddr, lport, rport);
360 // printf("|Collected: key=%d\n",key);
361 // return key;
362 return build_key(laddr, raddr, lport, rport);
363 }
364 }
365 // printf("\n");
366 return 0;
367 }
368
369 /* This function is called by every ipmib probe handler.
370 * Returns a "key" value to be used as an index into the collection arrays.
371 */
372 function ipmib_filter_key:long (skb:long, op:long, SourceIsLocal:long)
373 {
374 if ( !skb ) return 0;
375 // We only care about events with protocol IPPROTO_TCP(=6)
376 if( !(ipmib_get_proto(skb) == 6) ) return 0;
377 raddr = ipmib_remote_addr(skb, SourceIsLocal);
378 laddr = ipmib_local_addr(skb, SourceIsLocal);
379 rport = ipmib_tcp_remote_port(skb, SourceIsLocal);
380 lport = ipmib_tcp_local_port(skb, SourceIsLocal);
381
382 return filter_key(laddr, raddr, lport, rport);
383 }
384
385 /* This function is called by every tcpmib probe handler.
386 * Returns a "key" value to be used as an index into the collection arrays.
387 */
388 function tcpmib_filter_key:long (sk:long, op:long)
389 {
390 if ( !sk ) return 0;
391 laddr = tcpmib_local_addr(sk);
392 raddr = tcpmib_remote_addr(sk);
393 lport = tcpmib_local_port(sk);
394 rport = tcpmib_remote_port(sk);
395 return filter_key(laddr, raddr, lport, rport);
396 }
397
398 /* This function is called by every linuxmib probe handler.
399 * Returns a "key" value to be used as an index into the collection arrays.
400 * For now this is just the same as the tcpmib_filter_key.
401 */
402 function linuxmib_filter_key:long (sk:long, op:long)
403 {
404 return tcpmib_filter_key(sk,op);
405 }
406
407 function print_sockmib (key:long)
408 {
409 printf("Socket:\n");
410 if (SockSendbytes[key]||!nozeros)
411 printf("\tBytes Sent = %d\n", SockSendbytes[key]);
412 if (SockSendmsg[key]||!nozeros)
413 printf("\tMessages sent = %d\n", SockSendmsg[key]);
414 if (SockRecvbytes[key]||!nozeros)
415 printf("\tBytes Received = %d\n", SockRecvbytes[key]);
416 if (SockRecvmsg[key]||!nozeros)
417 printf("\tMessages Received = %d\n", SockRecvmsg[key]);
418 }
419
420
421 /*
422 * Prints the collected values for the IP mib.
423 */
424 function print_ipmib (key:long)
425 {
426 printf("Ip:\n");
427 if (InReceives[key]||!nozeros)
428 printf("\tInReceives = %d\n", InReceives[key]);
429 if (OutRequests[key]||!nozeros)
430 printf("\tOutRequests = %d\n", OutRequests[key]);
431 if (ReasmTimeout[key]||!nozeros)
432 printf("\tReasmTimeout = %d\n", ReasmTimeout[key]);
433 if (ReasmReqds[key]||!nozeros)
434 printf("\tReasmReqds = %d\n", ReasmReqds[key]);
435 if (FragOKs[key]||!nozeros)
436 printf("\tFragOKs = %d\n", FragOKs[key]);
437 if (FragFails[key]||!nozeros)
438 printf("\tFragFails = %d\n", FragFails[key]);
439 }
440
441 /*
442 * Prints the collected values for the TCP mib.
443 */
444 function print_tcpmib (key:long)
445 {
446 printf("Tcp:\n");
447 if (ActiveOpens[key]||!nozeros)
448 printf("\tActiveOpens = %d\n", ActiveOpens[key]);
449 %( kernel_v > "2.6.21" %?
450 if (AttemptFails[key]||!nozeros)
451 printf("\tAttemptFails = %d\n", AttemptFails[key]);
452 %)
453 %( kernel_v > "2.6.24" %?
454 if (CurrEstab[key]||!nozeros)
455 printf("\tCurrEstab = %d\n", CurrEstab[key]);
456 %)
457 %( kernel_v > "2.6.24" %?
458 if (EstabResets[key]||!nozeros)
459 printf("\tEstabResets = %d\n", EstabResets[key]);
460 %)
461 // if (InErrs[key]||!nozeros)
462 // printf("\tInErrs = %d\n", InErrs[key]);
463 if (InSegs[key]||!nozeros)
464 printf("\tInSegs = %d\n", InSegs[key]);
465 if (OutRsts[key]||!nozeros)
466 printf("\tOutRsts = %d\n", OutRsts[key]);
467 if (OutSegs[key]||!nozeros)
468 printf("\tOutSegs = %d\n", OutSegs[key]);
469 if (PassiveOpens[key]||!nozeros)
470 printf("\tPassiveOpens = %d\n", PassiveOpens[key]);
471 if (RetransSegs[key]||!nozeros)
472 printf("\tRetransSegs = %d\n", RetransSegs[key]);
473 }
474
475 /*
476 * Prints the collected values for the linux mib.
477 */
478 function print_linuxmib (key:long)
479 {
480 printf("TcpExt:\n");
481 if (DelayedACKs[key]||!nozeros)
482 printf("\tdelayed acks sent = %d\n", DelayedACKs[key]);
483 if (ListenOverflows[key]||!nozeros)
484 printf("\ttimes the listen queue of a socket overflowed = %d\n", ListenOverflows[key]);
485 if (ListenDrops[key]||!nozeros)
486 printf("\tSYNs to LISTEN sockets ignored = %d\n", ListenDrops[key]);
487 if (TCPMemoryPressures[key]||!nozeros)
488 printf("\ttcp memory pressure = %d\n", TCPMemoryPressures[key]);
489 }
490
491 function report ()
492 {
493 number_of_keys=0
494
495 foreach ([laddr, raddr, lport, rport] in key_list) {
496 ++number_of_keys;
497 _report(laddr, raddr, lport, rport);
498 }
499 if ( !number_of_keys )
500 printf("\nNo packets were accepted by the filters.\n");
501 }
502
503
504 function _report(laddr:long, raddr:long, lport:long, rport:long)
505 {
506 printf("\n-----------------------------------------\n");
507 if ( laddr && raddr && lport && rport ) // index=tuple
508 printf("%s:%d <-> %s:%d\n",
509 ip_ntop(htonl(laddr)), lport,
510 ip_ntop(htonl(raddr)), rport)
511 else {
512 if ( laddr )
513 printf("Local address: %s\n",ip_ntop(htonl(laddr)));
514 if ( raddr )
515 printf("Remote address: %s\n",ip_ntop(htonl(raddr)));
516 if ( lport )
517 printf("Local port: %d\n",lport);
518 if ( rport )
519 printf("Remote port: %d\n",rport);
520 }
521
522 key = key_list[laddr, raddr, lport, rport]
523 %( kernel_v > "2.6.24" %?
524 if ( onclose && (CurrEstab[key] < 0) )
525 printf("Stats were not collected for the entire socket life.\n")
526 %)
527 printf("-----------------------------------------\n");
528
529 print_sockmib(key)
530 print_ipmib(key)
531 print_tcpmib(key)
532 print_linuxmib(key)
533 printf("\n\n")
534 }
535
536 /* Terminates the run in timeout seconds, using global timeout value */
537 probe timer.s(1) {
538 if ( timeout == -1 ) next
539 if ( !timeout-- ) exit()
540 }
541
542 /* We are done, print a report and exit. */
543 probe end {
544 if ( !onclose )
545 report()
546 }
547
548 /* Enable the probes for the statictics we want to count.
549 *
550 * The impact of running this script on your system can
551 * be reduced by enabling only the probes corresponding to the
552 * statictics you are interested in. For example, if all you care about
553 * is counting the number of incoming connections that are established use:
554 * "probe tcpmib.PassiveOpens {}"
555 */
556
557 /* Collect all tcpmib stats */
558 probe tcpmib.* {}
559
560 /* Collect all ip stats */
561 probe ipmib.* {}
562
563 /* Collect the extended linux stats */
564 probe linuxmib.* {}
565
566 /* This probe supports the onclose option. When a connection closes
567 * CurrEstab will return an op of -1, we use that condition to trigger
568 * the dump off all stats for this socket. This feature only makes sense
569 * when index=tuple.
570 */
571 %( kernel_v > "2.6.24" %?
572 probe tcpmib.CurrEstab
573 {
574 if (!onclose) next
575 if (!key) next
576 if ( op != -1 ) next;
577 if ( !sk ) next;
578
579 laddr = tcpmib_local_addr(sk);
580 raddr = tcpmib_remote_addr(sk);
581 lport = tcpmib_local_port(sk);
582 rport = tcpmib_remote_port(sk);
583
584 _report(laddr, raddr, lport, rport)
585 delete key_list[laddr, raddr, lport, rport]
586 delete CurrEstab[key]
587 }
588 %)
589
590
591 /* SNMP has no counter for the number of bytes sent or received by TCP.
592 * The next two probes give us these raw byte counts.
593 */
594 global SockSendbytes;
595 global SockSendmsg;
596 probe tcp.sendmsg.return {
597 sk = @defined(@entry($sk)) ? @entry($sk) : @entry($sock->sk);
598 op = size; // $return
599 if ( op <= 0 ) next;
600 if ( !sk ) next;
601 key = tcpmib_filter_key(sk, op);
602 if ( key ) {
603 SockSendbytes[key] += op;
604 ++SockSendmsg[key];
605 }
606 }
607
608 global SockRecvbytes;
609 global SockRecvmsg;
610 probe tcp.recvmsg.return{
611 sk = @entry($sk)
612 op = size // $return
613 if ( op <= 0 ) next;
614 if ( !sk ) next;
615 key = tcpmib_filter_key(sk, op);
616 if ( key ) {
617 SockRecvbytes[key] += op;
618 ++SockRecvmsg[key];
619 }
620 }