2 * ss.c "sockstat", socket statistics
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version
7 * 2 of the License, or (at your option) any later version.
9 * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
17 #include <sys/ioctl.h>
18 #include <sys/socket.h>
20 #include <netinet/in.h>
24 #include <arpa/inet.h>
32 #include "libnetlink.h"
35 #include <asm/byteorder.h>
36 #include <linux/tcp.h>
37 #include <linux/tcp_diag.h>
39 int resolve_hosts = 0;
40 int resolve_services = 1;
41 int preferred_family = AF_UNSPEC;
55 static const char *TCP_PROTO = "tcp";
56 static const char *UDP_PROTO = "udp";
57 static const char *RAW_PROTO = "raw";
58 static const char *dg_proto = NULL;
73 #define PACKET_DBM ((1<<PACKET_DG_DB)|(1<<PACKET_R_DB))
74 #define UNIX_DBM ((1<<UNIX_DG_DB)|(1<<UNIX_ST_DB))
75 #define ALL_DB ((1<<MAX_DB)-1)
93 #define SS_ALL ((1<<SS_MAX)-1)
105 struct filter default_filter = {
107 states: SS_ALL & ~((1<<SS_LISTEN)|(1<<SS_CLOSE)|(1<<SS_TIME_WAIT)|(1<<SS_SYN_RECV)),
108 families: (1<<AF_INET)|(1<<AF_INET6),
111 struct filter current_filter;
113 int generic_proc_open(char *env, char *name)
116 char *p = getenv(env);
118 p = getenv("PROC_ROOT") ? : "/proc";
119 snprintf(store, sizeof(store)-1, "%s/%s", p, name);
122 return open(store, O_RDONLY);
125 int net_tcp_open(void)
127 return generic_proc_open("PROC_NET_TCP", "net/tcp");
130 int net_tcp6_open(void)
132 return generic_proc_open("PROC_NET_TCP6", "net/tcp6");
135 int net_udp_open(void)
137 return generic_proc_open("PROC_NET_UDP", "net/udp");
140 int net_udp6_open(void)
142 return generic_proc_open("PROC_NET_UDP6", "net/udp6");
145 int net_raw_open(void)
147 return generic_proc_open("PROC_NET_RAW", "net/raw");
150 int net_raw6_open(void)
152 return generic_proc_open("PROC_NET_RAW6", "net/raw6");
155 int net_unix_open(void)
157 return generic_proc_open("PROC_NET_UNIX", "net/unix");
160 int net_packet_open(void)
162 return generic_proc_open("PROC_NET_PACKET", "net/packet");
165 int net_netlink_open(void)
167 return generic_proc_open("PROC_NET_NETLINK", "net/netlink");
170 int slabinfo_open(void)
172 return generic_proc_open("PROC_SLABINFO", "slabinfo");
175 int net_sockstat_open(void)
177 return generic_proc_open("PROC_NET_SOCKSTAT", "net/sockstat");
180 int net_sockstat6_open(void)
182 return generic_proc_open("PROC_NET_SOCKSTAT6", "net/sockstat6");
185 int net_snmp_open(void)
187 return generic_proc_open("PROC_NET_SNMP", "net/snmp");
190 int net_netstat_open(void)
192 return generic_proc_open("PROC_NET_NETSTAT", "net/netstat");
195 int ephemeral_ports_open(void)
197 return generic_proc_open("PROC_IP_LOCAL_PORT_RANGE", "sys/net/ipv4/ip_local_port_range");
200 int find_users(int ino, char *buf, int buflen)
214 sprintf(pattern, "socket:[%d]", ino);
215 pattern_len = strlen(pattern);
217 strncpy(name, getenv("PROC_ROOT") ? : "/proc/", sizeof(name)/2);
218 name[sizeof(name)/2] = 0;
219 if (strlen(name) == 0 ||
220 name[strlen(name)-1] != '/')
222 nameoff = strlen(name);
223 if ((dir = opendir(name)) == NULL)
226 while ((d = readdir(dir)) != NULL) {
234 if (sscanf(d->d_name, "%d%c", &pid, &crap) != 1)
237 sprintf(name+nameoff, "%d/fd/", pid);
239 if ((dir1 = opendir(name)) == NULL)
244 while ((d1 = readdir(dir1)) != NULL) {
248 if (sscanf(d1->d_name, "%d%c", &fd, &crap) != 1)
251 sprintf(name+pos, "%d", fd);
252 n = readlink(name, lnk, sizeof(lnk)-1);
253 if (n != pattern_len ||
254 memcmp(lnk, pattern, n))
257 if (ptr-buf >= buflen-1)
260 if (process[0] == 0) {
263 snprintf(tmp, sizeof(tmp), "%s/%d/stat",
264 getenv("PROC_ROOT") ? : "/proc", pid);
265 if ((fp = fopen(tmp, "r")) != NULL) {
266 fscanf(fp, "%*d (%[^)])", process);
271 snprintf(ptr, buflen-(ptr-buf), "(\"%s\",%d,%d),", process, pid, fd);
284 /* Get stats from slab */
295 struct slabstat slabstat;
297 const char *slabstat_ids[] =
306 int get_slabstat(struct slabstat *s)
312 memset(s, 0, sizeof(*s));
314 if ((fp = fdopen(slabinfo_open(), "r")) == NULL)
317 cnt = sizeof(*s)/sizeof(int);
319 fgets(buf, sizeof(buf), fp);
320 while(fgets(buf, sizeof(buf), fp) != NULL) {
322 for (i=0; i<sizeof(slabstat_ids)/sizeof(slabstat_ids[0]); i++) {
323 if (memcmp(buf, slabstat_ids[i], strlen(slabstat_ids[i])) == 0) {
324 sscanf(buf, "%*s%d", ((int *)s) + i);
337 static const char *sstate_name[] = {
339 [TCP_ESTABLISHED] = "ESTAB",
340 [TCP_SYN_SENT] = "SYN-SENT",
341 [TCP_SYN_RECV] = "SYN-RECV",
342 [TCP_FIN_WAIT1] = "FIN-WAIT-1",
343 [TCP_FIN_WAIT2] = "FIN-WAIT-2",
344 [TCP_TIME_WAIT] = "TIME-WAIT",
345 [TCP_CLOSE] = "UNCONN",
346 [TCP_CLOSE_WAIT] = "CLOSE-WAIT",
347 [TCP_LAST_ACK] = "LAST-ACK",
348 [TCP_LISTEN] = "LISTEN",
349 [TCP_CLOSING] = "CLOSING",
352 static const char *sstate_namel[] = {
354 [TCP_ESTABLISHED] = "established",
355 [TCP_SYN_SENT] = "syn-sent",
356 [TCP_SYN_RECV] = "syn-recv",
357 [TCP_FIN_WAIT1] = "fin-wait-1",
358 [TCP_FIN_WAIT2] = "fin-wait-2",
359 [TCP_TIME_WAIT] = "time-wait",
360 [TCP_CLOSE] = "unconnected",
361 [TCP_CLOSE_WAIT] = "close-wait",
362 [TCP_LAST_ACK] = "last-ack",
363 [TCP_LISTEN] = "listening",
364 [TCP_CLOSING] = "closing",
382 unsigned long long sk;
383 int rto, ato, qack, cwnd, ssthresh;
386 static const char *tmr_name[] = {
395 char *print_ms_timer(int timeout)
398 int secs, msecs, minutes;
404 msecs = timeout%1000;
408 snprintf(buf, sizeof(buf)-16, "%dmin", minutes);
415 sprintf(buf+strlen(buf), "%d%s", secs, msecs ? "." : "sec");
418 sprintf(buf+strlen(buf), "%03dms", msecs);
422 char *print_hz_timer(int timeout)
425 return print_ms_timer(((timeout*1000) + hz-1)/hz);
436 struct scache *rlist;
438 void init_service_resolver(void)
441 FILE *fp = popen("/usr/sbin/rpcinfo -p 2>/dev/null", "r");
443 fgets(buf, sizeof(buf), fp);
444 while (fgets(buf, sizeof(buf), fp) != NULL) {
445 unsigned int progn, port;
446 char proto[128], prog[128];
447 if (sscanf(buf, "%u %*d %s %u %s", &progn, proto,
448 &port, prog+4) == 4) {
449 struct scache *c = malloc(sizeof(*c));
452 memcpy(prog, "rpc.", 4);
453 c->name = strdup(prog);
454 if (strcmp(proto, TCP_PROTO) == 0)
455 c->proto = TCP_PROTO;
456 else if (strcmp(proto, UDP_PROTO) == 0)
457 c->proto = UDP_PROTO;
468 const char *__resolve_service(int port)
472 for (c = rlist; c; c = c->next) {
473 if (c->port == port && c->proto == dg_proto)
477 /* Even do not try default linux ephemeral port ranges:
478 * default /etc/services contains so much of useless crap
479 * wouldbe "allocated" to this area that resolution
480 * is really harmful. I shrug each time when seeing
481 * "socks" or "cfinger" in dumps.
483 if (port < 32768 && (port < 1024 || port > 4999)) {
490 se = getservbyport(htons(port), dg_proto);
499 const char *resolve_service(int port)
501 static char buf[128];
502 static struct scache cache[256];
510 if (resolve_services) {
511 if (dg_proto == RAW_PROTO) {
512 return inet_proto_n2a(port, buf, sizeof(buf));
516 int hash = (port^(((unsigned long)dg_proto)>>2))&255;
518 for (c = &cache[hash]; c; c = c->next) {
519 if (c->port == port &&
520 c->proto == dg_proto) {
527 if ((res = __resolve_service(port)) != NULL) {
528 if ((c = malloc(sizeof(*c))) == NULL)
539 c->name = strdup(res);
540 c->next = cache[hash].next;
541 cache[hash].next = c;
549 sprintf(buf, "%u", port);
553 void formatted_print(const inet_prefix *a, int port)
556 const char *ap = buf;
559 est_len = addr_width;
561 if (a->family == AF_INET) {
562 if (a->data[0] == 0) {
566 ap = format_host(AF_INET, 4, a->data, buf, sizeof(buf));
569 ap = format_host(a->family, 16, a->data, buf, sizeof(buf));
570 est_len = strlen(ap);
571 if (est_len <= addr_width)
572 est_len = addr_width;
574 est_len = addr_width + ((est_len-addr_width+3)/4)*4;
576 printf("%*s:%-*s ", est_len, ap, serv_width, resolve_service(port));
583 struct aafilter *next;
586 int inet2_addr_match(const inet_prefix *a, const inet_prefix *p, int plen)
588 if (!inet_addr_match(a, p, plen))
591 /* Cursed "v4 mapped" addresses: v4 mapped socket matches
592 * pure IPv4 rule, but v4-mapped rule selects only v4-mapped
594 if (p->family == AF_INET && a->family == AF_INET6) {
595 if (a->data[0] == 0 && a->data[1] == 0 &&
596 a->data[2] == htonl(0xffff)) {
597 inet_prefix tmp = *a;
598 tmp.data[0] = a->data[3];
599 return inet_addr_match(&tmp, p, plen);
605 int unix_match(const inet_prefix *a, const inet_prefix *p)
607 char *addr, *pattern;
608 memcpy(&addr, a->data, sizeof(addr));
609 memcpy(&pattern, p->data, sizeof(pattern));
614 return !fnmatch(pattern, addr, 0);
617 int run_ssfilter(struct ssfilter *f, struct tcpstat *s)
622 static int low, high=65535;
624 if (s->local.family == AF_UNIX) {
626 memcpy(&p, s->local.data, sizeof(p));
627 return p == NULL || (p[0] == '@' && strlen(p) == 6 &&
628 strspn(p+1, "0123456789abcdef") == 5);
630 if (s->local.family == AF_PACKET)
631 return s->lport == 0 && s->local.data == 0;
632 if (s->local.family == AF_NETLINK)
636 FILE *fp = fdopen(ephemeral_ports_open(), "r");
638 fscanf(fp, "%d%d", &low, &high);
642 return s->lport >= low && s->lport <= high;
646 struct aafilter *a = (void*)f->pred;
647 if (a->addr.family == AF_UNIX)
648 return unix_match(&s->remote, &a->addr);
649 if (a->port != -1 && a->port != s->rport)
651 if (a->addr.bitlen) {
653 if (!inet2_addr_match(&s->remote, &a->addr, a->addr.bitlen))
655 } while ((a = a->next) != NULL);
662 struct aafilter *a = (void*)f->pred;
663 if (a->addr.family == AF_UNIX)
664 return unix_match(&s->local, &a->addr);
665 if (a->port != -1 && a->port != s->lport)
667 if (a->addr.bitlen) {
669 if (!inet2_addr_match(&s->local, &a->addr, a->addr.bitlen))
671 } while ((a = a->next) != NULL);
678 struct aafilter *a = (void*)f->pred;
679 return s->rport >= a->port;
683 struct aafilter *a = (void*)f->pred;
684 return s->rport <= a->port;
688 struct aafilter *a = (void*)f->pred;
689 return s->lport >= a->port;
693 struct aafilter *a = (void*)f->pred;
694 return s->lport <= a->port;
697 /* Yup. It is recursion. Sorry. */
699 return run_ssfilter(f->pred, s) && run_ssfilter(f->post, s);
701 return run_ssfilter(f->pred, s) || run_ssfilter(f->post, s);
703 return !run_ssfilter(f->pred, s);
709 /* Relocate external jumps by reloc. */
710 void ssfilter_patch(char *a, int len, int reloc)
713 struct tcpdiag_bc_op *op = (struct tcpdiag_bc_op*)a;
723 int ssfilter_bytecompile(struct ssfilter *f, char **bytecode)
728 if (!(*bytecode=malloc(4))) abort();
729 ((struct tcpdiag_bc_op*)*bytecode)[0] = (struct tcpdiag_bc_op){ TCPDIAG_BC_AUTO, 4, 8 };
735 struct aafilter *a = (void*)f->pred;
738 int code = (f->type == SSF_DCOND ? TCPDIAG_BC_D_COND : TCPDIAG_BC_S_COND);
741 for (b=a; b; b=b->next) {
742 len += 4 + sizeof(struct tcpdiag_hostcond);
743 if (a->addr.family == AF_INET6)
750 if (!(ptr = malloc(len))) abort();
752 for (b=a; b; b=b->next) {
753 struct tcpdiag_bc_op *op = (struct tcpdiag_bc_op *)ptr;
754 int alen = (a->addr.family == AF_INET6 ? 16 : 4);
755 int oplen = alen + 4 + sizeof(struct tcpdiag_hostcond);
756 struct tcpdiag_hostcond *cond = (struct tcpdiag_hostcond*)(ptr+4);
758 *op = (struct tcpdiag_bc_op){ code, oplen, oplen+4 };
759 cond->family = a->addr.family;
760 cond->port = a->port;
761 cond->prefix_len = a->addr.bitlen;
762 memcpy(cond->addr, a->addr.data, alen);
765 op = (struct tcpdiag_bc_op *)ptr;
766 *op = (struct tcpdiag_bc_op){ TCPDIAG_BC_JMP, 4, len - (ptr-*bytecode)};
770 return ptr - *bytecode;
774 struct aafilter *x = (void*)f->pred;
775 if (!(*bytecode=malloc(8))) abort();
776 ((struct tcpdiag_bc_op*)*bytecode)[0] = (struct tcpdiag_bc_op){ TCPDIAG_BC_D_GE, 8, 12 };
777 ((struct tcpdiag_bc_op*)*bytecode)[1] = (struct tcpdiag_bc_op){ 0, 0, x->port };
782 struct aafilter *x = (void*)f->pred;
783 if (!(*bytecode=malloc(8))) abort();
784 ((struct tcpdiag_bc_op*)*bytecode)[0] = (struct tcpdiag_bc_op){ TCPDIAG_BC_D_LE, 8, 12 };
785 ((struct tcpdiag_bc_op*)*bytecode)[1] = (struct tcpdiag_bc_op){ 0, 0, x->port };
790 struct aafilter *x = (void*)f->pred;
791 if (!(*bytecode=malloc(8))) abort();
792 ((struct tcpdiag_bc_op*)*bytecode)[0] = (struct tcpdiag_bc_op){ TCPDIAG_BC_S_GE, 8, 12 };
793 ((struct tcpdiag_bc_op*)*bytecode)[1] = (struct tcpdiag_bc_op){ 0, 0, x->port };
798 struct aafilter *x = (void*)f->pred;
799 if (!(*bytecode=malloc(8))) abort();
800 ((struct tcpdiag_bc_op*)*bytecode)[0] = (struct tcpdiag_bc_op){ TCPDIAG_BC_S_LE, 8, 12 };
801 ((struct tcpdiag_bc_op*)*bytecode)[1] = (struct tcpdiag_bc_op){ 0, 0, x->port };
807 char *a1, *a2, *a, l1, l2;
808 l1 = ssfilter_bytecompile(f->pred, &a1);
809 l2 = ssfilter_bytecompile(f->post, &a2);
810 if (!(a = malloc(l1+l2))) abort();
812 memcpy(a+l1, a2, l2);
814 ssfilter_patch(a, l1, l2);
820 char *a1, *a2, *a, l1, l2;
821 l1 = ssfilter_bytecompile(f->pred, &a1);
822 l2 = ssfilter_bytecompile(f->post, &a2);
823 if (!(a = malloc(l1+l2+4))) abort();
825 memcpy(a+l1+4, a2, l2);
827 *(struct tcpdiag_bc_op*)(a+l1) = (struct tcpdiag_bc_op){ TCPDIAG_BC_JMP, 4, l2+4 };
834 l1 = ssfilter_bytecompile(f->pred, &a1);
835 if (!(a = malloc(l1+4))) abort();
838 *(struct tcpdiag_bc_op*)(a+l1) = (struct tcpdiag_bc_op){ TCPDIAG_BC_JMP, 4, 8 };
847 int remember_he(struct aafilter *a, struct hostent *he)
849 char **ptr = he->h_addr_list;
853 if (he->h_addrtype == AF_INET)
855 else if (he->h_addrtype == AF_INET6)
861 struct aafilter *b = a;
862 if (a->addr.bitlen) {
863 if ((b = malloc(sizeof(*b))) == NULL)
869 memcpy(b->addr.data, *ptr, len);
870 b->addr.bytelen = len;
871 b->addr.bitlen = len*8;
872 b->addr.family = he->h_addrtype;
879 int get_dns_host(struct aafilter *a, char *addr, int fam)
890 he = gethostbyname2(addr, fam == AF_UNSPEC ? AF_INET : fam);
892 cnt = remember_he(a, he);
893 if (fam == AF_UNSPEC) {
894 he = gethostbyname2(addr, AF_INET6);
896 cnt += remember_he(a, he);
905 struct rtnl_handle rth;
912 const char *xll_index_to_name(int index)
916 return ll_index_to_name(index);
919 int xll_name_to_index(char *dev)
923 return ll_name_to_index(dev);
926 void *parse_hostcond(char *addr)
930 struct aafilter *res;
931 int fam = preferred_family;
933 memset(&a, 0, sizeof(a));
936 if (fam == AF_UNIX || strncmp(addr, "unix:", 5) == 0) {
938 a.addr.family = AF_UNIX;
939 if (strncmp(addr, "unix:", 5) == 0)
942 a.addr.bitlen = 8*strlen(p);
943 memcpy(a.addr.data, &p, sizeof(p));
947 if (fam == AF_PACKET || strncmp(addr, "link:", 5) == 0) {
948 a.addr.family = AF_PACKET;
950 if (strncmp(addr, "link:", 5) == 0)
952 port = strchr(addr, ':');
955 if (port[1] && strcmp(port+1, "*")) {
956 if (get_integer(&a.port, port+1, 0)) {
957 if ((a.port = xll_name_to_index(port+1)) <= 0)
962 if (addr[0] && strcmp(addr, "*")) {
965 if (ll_proto_a2n(&tmp, addr))
967 a.addr.data[0] = ntohs(tmp);
972 if (fam == AF_NETLINK || strncmp(addr, "netlink:", 8) == 0) {
973 a.addr.family = AF_NETLINK;
975 if (strncmp(addr, "netlink:", 8) == 0)
977 port = strchr(addr, ':');
980 if (port[1] && strcmp(port+1, "*")) {
981 if (get_integer(&a.port, port+1, 0)) {
982 if (strcmp(port+1, "kernel") == 0)
989 if (addr[0] && strcmp(addr, "*")) {
991 if (get_u32(a.addr.data, addr, 0)) {
992 if (strcmp(addr, "rtnl") == 0)
994 else if (strcmp(addr, "fw") == 0)
996 else if (strcmp(addr, "tcpdiag") == 0)
1005 if (strncmp(addr, "inet:", 5) == 0) {
1008 } else if (strncmp(addr, "inet6:", 6) == 0) {
1013 /* URL-like literal [] */
1014 if (addr[0] == '[') {
1016 if ((port = strchr(addr, ']')) == NULL)
1019 } else if (addr[0] == '*') {
1022 port = strrchr(strchr(addr, '/') ? : addr, ':');
1024 if (port && *port) {
1028 if (*port && *port != '*') {
1029 if (get_integer(&a.port, port, 0)) {
1030 struct servent *se1 = NULL;
1031 struct servent *se2 = NULL;
1032 if (current_filter.dbs&(1<<UDP_DB))
1033 se1 = getservbyname(port, UDP_PROTO);
1034 if (current_filter.dbs&(1<<TCP_DB))
1035 se2 = getservbyname(port, TCP_PROTO);
1036 if (se1 && se2 && se1->s_port != se2->s_port) {
1037 fprintf(stderr, "Error: ambiguous port \"%s\".\n", port);
1043 a.port = ntohs(se1->s_port);
1046 for (s = rlist; s; s = s->next) {
1047 if ((s->proto == UDP_PROTO &&
1048 (current_filter.dbs&(1<<UDP_DB))) ||
1049 (s->proto == TCP_PROTO &&
1050 (current_filter.dbs&(1<<TCP_DB)))) {
1051 if (s->name && strcmp(s->name, port) == 0) {
1052 if (a.port > 0 && a.port != s->port) {
1053 fprintf(stderr, "Error: ambiguous port \"%s\".\n", port);
1061 fprintf(stderr, "Error: \"%s\" does not look like a port.\n", port);
1068 if (addr && *addr && *addr != '*') {
1069 if (get_prefix_1(&a.addr, addr, fam)) {
1070 if (get_dns_host(&a, addr, fam)) {
1071 fprintf(stderr, "Error: an inet prefix is expected rather than \"%s\".\n", addr);
1078 res = malloc(sizeof(*res));
1080 memcpy(res, &a, sizeof(a));
1084 int tcp_show_line(char *line, struct filter *f, int family)
1087 char *loc, *rem, *data;
1092 if ((p = strchr(line, ':')) == NULL)
1096 if ((p = strchr(loc, ':')) == NULL)
1101 if ((p = strchr(rem, ':')) == NULL)
1107 int state = (data[1] >= 'A') ? (data[1] - 'A' + 10) : (data[1] - '0');
1109 if (!(f->states & (1<<state)))
1113 s.local.family = s.remote.family = family;
1114 if (family == AF_INET) {
1115 sscanf(loc, "%x:%x", s.local.data, (unsigned*)&s.lport);
1116 sscanf(rem, "%x:%x", s.remote.data, (unsigned*)&s.rport);
1117 s.local.bytelen = s.remote.bytelen = 4;
1119 sscanf(loc, "%08x%08x%08x%08x:%x",
1125 sscanf(rem, "%08x%08x%08x%08x:%x",
1131 s.local.bytelen = s.remote.bytelen = 16;
1134 if (f->f && run_ssfilter(f->f, &s) == 0)
1138 n = sscanf(data, "%x %x:%x %x:%x %x %d %d %d %d %llx %d %d %d %d %d %[^\n]\n",
1139 &s.state, &s.wq, &s.rq,
1140 &s.timer, &s.timeout, &s.retrs, &s.uid, &s.probes, &s.ino,
1141 &s.refcnt, &s.sk, &s.rto, &s.ato, &s.qack,
1142 &s.cwnd, &s.ssthresh, opt);
1155 printf("%-*s ", netid_width, "tcp");
1157 printf("%-*s ", state_width, sstate_name[s.state]);
1159 printf("%-6d %-6d ", s.rq, s.wq);
1161 formatted_print(&s.local, s.lport);
1162 formatted_print(&s.remote, s.rport);
1168 printf(" timer:(%s,%s,%d)",
1170 print_hz_timer(s.timeout),
1171 s.timer != 1 ? s.probes : s.retrs);
1175 if (s.rto && s.rto != 3*get_hz())
1176 printf(" rto:%g", (double)s.rto/get_hz());
1178 printf(" ato:%g", (double)s.ato/get_hz());
1180 printf(" cwnd:%d", s.cwnd);
1181 if (s.ssthresh != -1)
1182 printf(" ssthresh:%d", s.ssthresh);
1184 printf(" qack:%d", s.qack/2);
1190 if (find_users(s.ino, ubuf, sizeof(ubuf)) > 0)
1191 printf(" users:(%s)", ubuf);
1195 printf(" uid:%u", (unsigned)s.uid);
1196 printf(" ino:%u", (unsigned)s.ino);
1197 printf(" sk:%llx", s.sk);
1199 printf(" opt:\"%s\"", opt);
1206 int generic_record_read(int fd, char *buf, int bufsize,
1207 int (*worker)(char*, struct filter *, int),
1208 struct filter *f, int fam)
1215 /* Load the first chunk and calculate record length from it. */
1216 n = read(fd, buf, bufsize);
1219 /* I _know_ that this is wrong, do not remind. :-)
1220 * But this works nowadays. */
1223 p = memchr(buf, '\n', n);
1224 if (p == NULL || (p-buf) >= n)
1225 goto outwrongformat;
1226 recsize = (p-buf)+1;
1230 while ((p+recsize) - buf <= n) {
1231 if (p[recsize-1] != '\n')
1232 goto outwrongformat;
1234 if (worker(p, f, fam) < 0)
1239 int remains = (buf+bufsize) - p;
1240 memcpy(buf, p, remains);
1242 n = read(fd, p, (buf+bufsize) - p);
1245 if (n < (buf+bufsize) - p) {
1249 goto outwrongformat;
1257 goto outwrongformat;
1270 void tcp_show_info(struct nlmsghdr *nlh, struct tcpdiagmsg *r)
1272 struct rtattr * tb[TCPDIAG_MAX+1];
1273 const struct tcpdiag_meminfo *minfo = NULL;
1274 const struct tcp_info *info = NULL;
1275 const struct tcpvegas_info *vinfo = NULL;
1277 memset(tb, 0, sizeof(tb));
1278 parse_rtattr(tb, TCPDIAG_MAX, (struct rtattr*)(r+1),
1279 nlh->nlmsg_len - NLMSG_LENGTH(sizeof(*r)));
1281 if (tb[TCPDIAG_MEMINFO])
1282 minfo = RTA_DATA(tb[TCPDIAG_MEMINFO]);
1283 if (tb[TCPDIAG_INFO])
1284 info = RTA_DATA(tb[TCPDIAG_INFO]);
1285 if (tb[TCPDIAG_VEGASINFO])
1286 vinfo = RTA_DATA(tb[TCPDIAG_VEGASINFO]);
1289 printf(" mem:(r%u,w%u,f%u,t%u)",
1290 minfo->tcpdiag_rmem,
1291 minfo->tcpdiag_wmem,
1292 minfo->tcpdiag_fmem,
1293 minfo->tcpdiag_tmem);
1298 if (info->tcpi_rto && info->tcpi_rto != 3000000)
1299 printf(" rto:%g", (double)info->tcpi_rto/1000);
1301 printf(" rtt:%g/%g", (double)info->tcpi_rtt/1000,
1302 (double)info->tcpi_rttvar/1000);
1304 printf(" ato:%g", (double)info->tcpi_ato/1000);
1305 if (info->tcpi_snd_cwnd != 2)
1306 printf(" cwnd:%d", info->tcpi_snd_cwnd);
1307 if (info->tcpi_snd_ssthresh < 0xFFFF)
1308 printf(" ssthresh:%d", info->tcpi_snd_ssthresh);
1311 if (vinfo->tcpv_enabled)
1313 if (vinfo->tcpv_rtt &&
1314 vinfo->tcpv_rtt != 0x7fffffff &&
1315 info->tcpi_snd_mss &&
1316 info->tcpi_snd_cwnd) {
1318 (double) info->tcpi_snd_cwnd *
1319 (double) info->tcpi_snd_mss *
1320 8000000. / (double) vinfo->tcpv_rtt);
1325 #warning No TCP_INFO. Please, do not repeat this experiment, use right kernel.
1326 printf(" MORE_INFO_PROVIDED_YOU_COMPILED_SS_RIGHT");
1330 int tcp_show_sock(struct nlmsghdr *nlh, struct filter *f)
1332 struct tcpdiagmsg *r = NLMSG_DATA(nlh);
1335 s.state = r->tcpdiag_state;
1336 s.local.family = s.remote.family = r->tcpdiag_family;
1337 s.lport = ntohs(r->id.tcpdiag_sport);
1338 s.rport = ntohs(r->id.tcpdiag_dport);
1339 if (s.local.family == AF_INET) {
1340 s.local.bytelen = s.remote.bytelen = 4;
1342 s.local.bytelen = s.remote.bytelen = 16;
1344 memcpy(s.local.data, r->id.tcpdiag_src, s.local.bytelen);
1345 memcpy(s.remote.data, r->id.tcpdiag_dst, s.local.bytelen);
1347 if (f && f->f && run_ssfilter(f->f, &s) == 0)
1351 printf("%-*s ", netid_width, "tcp");
1353 printf("%-*s ", state_width, sstate_name[s.state]);
1355 printf("%-6d %-6d ", r->tcpdiag_rqueue, r->tcpdiag_wqueue);
1357 formatted_print(&s.local, s.lport);
1358 formatted_print(&s.remote, s.rport);
1361 if (r->tcpdiag_timer) {
1362 if (r->tcpdiag_timer > 4)
1363 r->tcpdiag_timer = 5;
1364 printf(" timer:(%s,%s,%d)",
1365 tmr_name[r->tcpdiag_timer],
1366 print_ms_timer(r->tcpdiag_expires),
1367 r->tcpdiag_retrans);
1372 if (find_users(r->tcpdiag_inode, ubuf, sizeof(ubuf)) > 0)
1373 printf(" users:(%s)", ubuf);
1377 printf(" uid:%u", (unsigned)r->tcpdiag_uid);
1378 printf(" ino:%u", (unsigned)r->tcpdiag_inode);
1379 printf(" sk:%08x", r->id.tcpdiag_cookie[0]);
1380 if (r->id.tcpdiag_cookie[1] != 0)
1381 printf("%08x", r->id.tcpdiag_cookie[1]);
1383 if (show_mem || show_tcpinfo) {
1385 tcp_show_info(nlh, r);
1394 int tcp_show_netlink(struct filter *f, FILE *dump_fp)
1397 struct sockaddr_nl nladdr;
1399 struct nlmsghdr nlh;
1400 struct tcpdiagreq r;
1407 struct iovec iov[3];
1409 if ((fd = socket(AF_NETLINK, SOCK_RAW, NETLINK_TCPDIAG)) < 0)
1412 memset(&nladdr, 0, sizeof(nladdr));
1413 nladdr.nl_family = AF_NETLINK;
1415 req.nlh.nlmsg_len = sizeof(req);
1416 req.nlh.nlmsg_type = TCPDIAG_GETSOCK;
1417 req.nlh.nlmsg_flags = NLM_F_ROOT|NLM_F_MATCH|NLM_F_REQUEST;
1418 req.nlh.nlmsg_pid = 0;
1419 req.nlh.nlmsg_seq = 123456;
1420 memset(&req.r, 0, sizeof(req.r));
1421 req.r.tcpdiag_family = AF_INET;
1422 req.r.tcpdiag_states = f->states;
1424 req.r.tcpdiag_ext |= (1<<(TCPDIAG_MEMINFO-1));
1426 req.r.tcpdiag_ext |= (1<<(TCPDIAG_INFO-1));
1427 #ifdef TCPDIAG_VEGASINFO
1428 req.r.tcpdiag_ext |= (1<<(TCPDIAG_VEGASINFO-1));
1432 iov[0] = (struct iovec){ &req, sizeof(req) };
1434 bclen = ssfilter_bytecompile(f->f, &bc);
1435 rta.rta_type = TCPDIAG_REQ_BYTECODE;
1436 rta.rta_len = RTA_LENGTH(bclen);
1437 iov[1] = (struct iovec){ &rta, sizeof(rta) };
1438 iov[2] = (struct iovec){ bc, bclen };
1439 req.nlh.nlmsg_len += RTA_LENGTH(bclen);
1442 msg = (struct msghdr) {
1443 (void*)&nladdr, sizeof(nladdr),
1449 if (sendmsg(fd, &msg, 0) < 0)
1453 iov[0] = (struct iovec){ buf, sizeof(buf) };
1459 msg = (struct msghdr) {
1460 (void*)&nladdr, sizeof(nladdr),
1466 status = recvmsg(fd, &msg, 0);
1475 fprintf(stderr, "EOF on netlink\n");
1480 fwrite(buf, 1, NLMSG_ALIGN(status), dump_fp);
1482 h = (struct nlmsghdr*)buf;
1483 while (NLMSG_OK(h, status)) {
1486 if (/*h->nlmsg_pid != rth->local.nl_pid ||*/
1487 h->nlmsg_seq != 123456)
1490 if (h->nlmsg_type == NLMSG_DONE)
1492 if (h->nlmsg_type == NLMSG_ERROR) {
1493 struct nlmsgerr *err = (struct nlmsgerr*)NLMSG_DATA(h);
1494 if (h->nlmsg_len < NLMSG_LENGTH(sizeof(struct nlmsgerr))) {
1495 fprintf(stderr, "ERROR truncated\n");
1497 errno = -err->error;
1498 perror("TCPDIAG answers");
1503 err = tcp_show_sock(h, NULL);
1509 h = NLMSG_NEXT(h, status);
1511 if (msg.msg_flags & MSG_TRUNC) {
1512 fprintf(stderr, "Message truncated\n");
1516 fprintf(stderr, "!!!Remnant of size %d\n", status);
1523 int tcp_show_netlink_file(struct filter *f)
1528 if ((fp = fopen(getenv("TCPDIAG_FILE"), "r")) == NULL) {
1529 perror("fopen($TCPDIAG_FILE)");
1535 struct nlmsghdr *h = (struct nlmsghdr*)buf;
1537 status = fread(buf, 1, sizeof(*h), fp);
1539 perror("Reading header from $TCPDIAG_FILE");
1542 if (status != sizeof(*h)) {
1543 perror("Unexpected EOF reading $TCPDIAG_FILE");
1547 status = fread(h+1, 1, NLMSG_ALIGN(h->nlmsg_len-sizeof(*h)), fp);
1550 perror("Reading $TCPDIAG_FILE");
1553 if (status + sizeof(*h) < h->nlmsg_len) {
1554 perror("Unexpected EOF reading $TCPDIAG_FILE");
1558 /* The only legal exit point */
1559 if (h->nlmsg_type == NLMSG_DONE)
1562 if (h->nlmsg_type == NLMSG_ERROR) {
1563 struct nlmsgerr *err = (struct nlmsgerr*)NLMSG_DATA(h);
1564 if (h->nlmsg_len < NLMSG_LENGTH(sizeof(struct nlmsgerr))) {
1565 fprintf(stderr, "ERROR truncated\n");
1567 errno = -err->error;
1568 perror("TCPDIAG answered");
1573 err = tcp_show_sock(h, f);
1579 int tcp_show(struct filter *f)
1583 int bufsize = 64*1024;
1585 dg_proto = TCP_PROTO;
1587 if (getenv("TCPDIAG_FILE"))
1588 return tcp_show_netlink_file(f);
1590 if (!getenv("PROC_NET_TCP") && !getenv("PROC_ROOT")
1591 && tcp_show_netlink(f, NULL) == 0)
1594 /* Sigh... We have to parse /proc/net/tcp... */
1596 /* Estimate amount of sockets and try to allocate
1597 * huge buffer to read all the table at one read.
1598 * Limit it by 16MB though. The assumption is: as soon as
1599 * kernel was able to hold information about N connections,
1600 * it is able to give us some memory for snapshot.
1603 int guess = slabstat.socks+slabstat.tcp_syns;
1604 if (f->states&(1<<SS_TIME_WAIT))
1605 guess += slabstat.tcp_tws;
1606 if (guess > (16*1024*1024)/128)
1607 guess = (16*1024*1024)/128;
1609 if (guess > bufsize)
1612 while (bufsize >= 64*1024) {
1613 if ((buf = malloc(bufsize)) != NULL)
1622 if (f->families & (1<<AF_INET)) {
1623 if ((fd = net_tcp_open()) < 0)
1625 if (generic_record_read(fd, buf, bufsize, tcp_show_line, f, AF_INET))
1630 if ((f->families & (1<<AF_INET6)) &&
1631 (fd = net_tcp6_open()) >= 0) {
1632 if (generic_record_read(fd, buf, bufsize, tcp_show_line, f, AF_INET6))
1642 int saved_errno = errno;
1647 errno = saved_errno;
1653 int dgram_show_line(char *line, struct filter *f, int family)
1656 char *loc, *rem, *data;
1661 if ((p = strchr(line, ':')) == NULL)
1665 if ((p = strchr(loc, ':')) == NULL)
1670 if ((p = strchr(rem, ':')) == NULL)
1676 int state = (data[1] >= 'A') ? (data[1] - 'A' + 10) : (data[1] - '0');
1678 if (!(f->states & (1<<state)))
1682 s.local.family = s.remote.family = family;
1683 if (family == AF_INET) {
1684 sscanf(loc, "%x:%x", s.local.data, (unsigned*)&s.lport);
1685 sscanf(rem, "%x:%x", s.remote.data, (unsigned*)&s.rport);
1686 s.local.bytelen = s.remote.bytelen = 4;
1688 sscanf(loc, "%08x%08x%08x%08x:%x",
1694 sscanf(rem, "%08x%08x%08x%08x:%x",
1700 s.local.bytelen = s.remote.bytelen = 16;
1703 if (f->f && run_ssfilter(f->f, &s) == 0)
1707 n = sscanf(data, "%x %x:%x %*x:%*x %*x %d %*d %d %d %llx %[^\n]\n",
1708 &s.state, &s.wq, &s.rq,
1710 &s.refcnt, &s.sk, opt);
1716 printf("%-*s ", netid_width, dg_proto);
1718 printf("%-*s ", state_width, sstate_name[s.state]);
1720 printf("%-6d %-6d ", s.rq, s.wq);
1722 formatted_print(&s.local, s.lport);
1723 formatted_print(&s.remote, s.rport);
1727 if (find_users(s.ino, ubuf, sizeof(ubuf)) > 0)
1728 printf(" users:(%s)", ubuf);
1733 printf(" uid=%u", (unsigned)s.uid);
1734 printf(" ino=%u", (unsigned)s.ino);
1735 printf(" sk=%llx", s.sk);
1737 printf(" opt:\"%s\"", opt);
1745 int udp_show(struct filter *f)
1749 int bufsize = sizeof(buf);
1751 dg_proto = UDP_PROTO;
1753 if (f->families&(1<<AF_INET)) {
1754 if ((fd = net_udp_open()) < 0)
1756 if (generic_record_read(fd, buf, bufsize, dgram_show_line, f, AF_INET))
1761 if ((f->families&(1<<AF_INET6)) &&
1762 (fd = net_udp6_open()) >= 0) {
1763 if (generic_record_read(fd, buf, bufsize, dgram_show_line, f, AF_INET6))
1771 int saved_errno = errno;
1774 errno = saved_errno;
1779 int raw_show(struct filter *f)
1783 int bufsize = sizeof(buf);
1785 dg_proto = RAW_PROTO;
1787 if (f->families&(1<<AF_INET)) {
1788 if ((fd = net_raw_open()) < 0)
1790 if (generic_record_read(fd, buf, bufsize, dgram_show_line, f, AF_INET))
1795 if ((f->families&(1<<AF_INET6)) &&
1796 (fd = net_raw6_open()) >= 0) {
1797 if (generic_record_read(fd, buf, bufsize, dgram_show_line, f, AF_INET6))
1805 int saved_errno = errno;
1808 errno = saved_errno;
1816 struct unixstat *next;
1828 int unix_state_map[] = { SS_CLOSE, SS_SYN_SENT,
1829 SS_ESTABLISHED, SS_CLOSING };
1832 #define MAX_UNIX_REMEMBER (1024*1024/sizeof(struct unixstat))
1834 void unix_list_free(struct unixstat *list)
1837 struct unixstat *s = list;
1845 void unix_list_print(struct unixstat *list, struct filter *f)
1850 for (s = list; s; s = s->next) {
1851 if (!(f->states & (1<<s->state)))
1853 if (s->type == SOCK_STREAM && !(f->dbs&(1<<UNIX_ST_DB)))
1855 if (s->type == SOCK_DGRAM && !(f->dbs&(1<<UNIX_DG_DB)))
1861 for (p = list; p; p = p->next) {
1862 if (s->peer == p->ino)
1868 peer = p->name ? : "*";
1874 tst.local.family = AF_UNIX;
1875 tst.remote.family = AF_UNIX;
1876 memcpy(tst.local.data, &s->name, sizeof(s->name));
1877 if (strcmp(peer, "*") == 0)
1878 memset(tst.remote.data, 0, sizeof(peer));
1880 memcpy(tst.remote.data, &peer, sizeof(peer));
1881 if (run_ssfilter(f->f, &tst) == 0)
1886 printf("%-*s ", netid_width,
1887 s->type == SOCK_STREAM ? "u_str" : "u_dgr");
1889 printf("%-*s ", state_width, sstate_name[s->state]);
1890 printf("%-6d %-6d ", s->rq, s->wq);
1891 printf("%*s %-*d %*s %-*d",
1892 addr_width, s->name ? : "*", serv_width, s->ino,
1893 addr_width, peer, serv_width, s->peer);
1896 if (find_users(s->ino, ubuf, sizeof(ubuf)) > 0)
1897 printf(" users:(%s)", ubuf);
1903 int unix_show(struct filter *f)
1910 struct unixstat *list = NULL;
1912 if ((fp = fdopen(net_unix_open(), "r")) == NULL)
1914 fgets(buf, sizeof(buf)-1, fp);
1916 if (memcmp(buf, "Peer", 4) == 0)
1920 while (fgets(buf, sizeof(buf)-1, fp)) {
1921 struct unixstat *u, **insp;
1924 if (!(u = malloc(sizeof(*u))))
1928 if (sscanf(buf, "%x: %x %x %x %x %x %d %s",
1929 &u->peer, &u->rq, &u->wq, &flags, &u->type,
1930 &u->state, &u->ino, name) < 8)
1933 if (flags&(1<<16)) {
1934 u->state = SS_LISTEN;
1936 u->state = unix_state_map[u->state-1];
1937 if (u->type == SOCK_DGRAM &&
1938 u->state == SS_CLOSE &&
1940 u->state = SS_ESTABLISHED;
1951 if (u->type < (*insp)->type ||
1952 (u->type == (*insp)->type &&
1953 u->ino < (*insp)->ino))
1955 insp = &(*insp)->next;
1961 if ((u->name = malloc(strlen(name)+1)) == NULL)
1963 strcpy(u->name, name);
1965 if (++cnt > MAX_UNIX_REMEMBER) {
1966 unix_list_print(list, f);
1967 unix_list_free(list);
1974 unix_list_print(list, f);
1975 unix_list_free(list);
1984 int packet_show(struct filter *f)
1995 unsigned long long sk;
1997 if (!(f->states & (1<<SS_CLOSE)))
2000 if ((fp = fdopen(net_packet_open(), "r")) == NULL)
2002 fgets(buf, sizeof(buf)-1, fp);
2004 while (fgets(buf, sizeof(buf)-1, fp)) {
2005 sscanf(buf, "%llx %*d %d %x %d %d %u %u %u",
2007 &type, &prot, &iface, &state,
2010 if (type == SOCK_RAW && !(f->dbs&(1<<PACKET_R_DB)))
2012 if (type == SOCK_DGRAM && !(f->dbs&(1<<PACKET_DG_DB)))
2016 tst.local.family = AF_PACKET;
2017 tst.remote.family = AF_PACKET;
2020 tst.local.data[0] = prot;
2021 tst.remote.data[0] = 0;
2022 if (run_ssfilter(f->f, &tst) == 0)
2027 printf("%-*s ", netid_width,
2028 type == SOCK_RAW ? "p_raw" : "p_dgr");
2030 printf("%-*s ", state_width, "UNCONN");
2031 printf("%-6d %-6d ", rq, 0);
2033 printf("%*s:", addr_width, "*");
2036 printf("%*s:", addr_width,
2037 ll_proto_n2a(htons(prot), tb, sizeof(tb)));
2040 printf("%-*s ", serv_width, "*");
2042 printf("%-*s ", serv_width, xll_index_to_name(iface));
2045 addr_width, "", serv_width, "");
2049 if (find_users(ino, ubuf, sizeof(ubuf)) > 0)
2050 printf(" users:(%s)", ubuf);
2053 printf(" ino=%u uid=%u sk=%llx", ino, uid, sk);
2061 int netlink_show(struct filter *f)
2068 unsigned long long sk, cb;
2070 if (!(f->states & (1<<SS_CLOSE)))
2073 if ((fp = fdopen(net_netlink_open(), "r")) == NULL)
2075 fgets(buf, sizeof(buf)-1, fp);
2077 while (fgets(buf, sizeof(buf)-1, fp)) {
2078 sscanf(buf, "%llx %d %d %x %d %d %llx %d",
2080 &prot, &pid, &groups, &rq, &wq, &cb, &rc);
2084 tst.local.family = AF_NETLINK;
2085 tst.remote.family = AF_NETLINK;
2088 tst.local.data[0] = prot;
2089 tst.remote.data[0] = 0;
2090 if (run_ssfilter(f->f, &tst) == 0)
2095 printf("%-*s ", netid_width, "nl");
2097 printf("%-*s ", state_width, "UNCONN");
2098 printf("%-6d %-6d ", rq, wq);
2099 if (resolve_services && prot == 0)
2100 printf("%*s:", addr_width, "rtnl");
2101 else if (resolve_services && prot == 3)
2102 printf("%*s:", addr_width, "fw");
2103 else if (resolve_services && prot == 4)
2104 printf("%*s:", addr_width, "tcpdiag");
2106 printf("%*d:", addr_width, prot);
2108 printf("%-*s ", serv_width, "*");
2109 } else if (resolve_services) {
2113 printf("%-*s ", serv_width, "kernel");
2114 } else if (pid > 0) {
2117 sprintf(procname, "%s/%d/stat",
2118 getenv("PROC_ROOT") ? : "/proc", pid);
2119 if ((fp = fopen(procname, "r")) != NULL) {
2120 if (fscanf(fp, "%*d (%[^)])", procname) == 1) {
2121 sprintf(procname+strlen(procname), "/%d", pid);
2122 printf("%-*s ", serv_width, procname);
2129 printf("%-*d ", serv_width, pid);
2131 printf("%-*d ", serv_width, pid);
2134 addr_width, "", serv_width, "");
2137 printf(" sk=%llx cb=%llx groups=0x%08x", sk, cb, groups);
2150 int get_snmp_int(char *proto, char *key, int *result)
2154 int protolen = strlen(proto);
2155 int keylen = strlen(key);
2159 if ((fp = fdopen(net_snmp_open(), "r")) == NULL)
2162 while (fgets(buf, sizeof(buf), fp) != NULL) {
2165 if (memcmp(buf, proto, protolen))
2167 while ((p = strchr(p, ' ')) != NULL) {
2170 if (memcmp(p, key, keylen) == 0 &&
2171 (p[keylen] == ' ' || p[keylen] == '\n'))
2174 if (fgets(buf, sizeof(buf), fp) == NULL)
2176 if (memcmp(buf, proto, protolen))
2179 while ((p = strchr(p, ' ')) != NULL) {
2182 sscanf(p, "%d", result);
2195 /* Get stats from sockstat */
2216 static void get_sockstat_line(char *line, struct sockstat *s)
2218 char id[256], rem[256];
2220 if (sscanf(line, "%[^ ] %[^\n]\n", id, rem) != 2)
2223 if (strcmp(id, "sockets:") == 0)
2224 sscanf(rem, "%*s%d", &s->socks);
2225 else if (strcmp(id, "UDP:") == 0)
2226 sscanf(rem, "%*s%d", &s->udp4);
2227 else if (strcmp(id, "UDP6:") == 0)
2228 sscanf(rem, "%*s%d", &s->udp6);
2229 else if (strcmp(id, "RAW:") == 0)
2230 sscanf(rem, "%*s%d", &s->raw4);
2231 else if (strcmp(id, "RAW6:") == 0)
2232 sscanf(rem, "%*s%d", &s->raw6);
2233 else if (strcmp(id, "TCP6:") == 0)
2234 sscanf(rem, "%*s%d", &s->tcp6_hashed);
2235 else if (strcmp(id, "FRAG:") == 0)
2236 sscanf(rem, "%*s%d%*s%d", &s->frag4, &s->frag4_mem);
2237 else if (strcmp(id, "FRAG6:") == 0)
2238 sscanf(rem, "%*s%d%*s%d", &s->frag6, &s->frag6_mem);
2239 else if (strcmp(id, "TCP:") == 0)
2240 sscanf(rem, "%*s%d%*s%d%*s%d%*s%d%*s%d",
2242 &s->tcp_orphans, &s->tcp_tws, &s->tcp_total, &s->tcp_mem);
2245 int get_sockstat(struct sockstat *s)
2250 memset(s, 0, sizeof(*s));
2252 if ((fp = fdopen(net_sockstat_open(), "r")) == NULL)
2254 while(fgets(buf, sizeof(buf), fp) != NULL)
2255 get_sockstat_line(buf, s);
2258 if ((fp = fdopen(net_sockstat6_open(), "r")) == NULL)
2260 while(fgets(buf, sizeof(buf), fp) != NULL)
2261 get_sockstat_line(buf, s);
2267 int print_summary(void)
2272 if (get_sockstat(&s) < 0)
2273 perror("ss: get_sockstat");
2274 if (get_snmp_int("Tcp:", "CurrEstab", &sn.tcp_estab) < 0)
2275 perror("ss: get_snmpstat");
2277 printf("Total: %d (kernel %d)\n", s.socks, slabstat.socks);
2279 printf("TCP: %d (estab %d, closed %d, orphaned %d, synrecv %d, timewait %d/%d), ports %d\n",
2280 s.tcp_total + slabstat.tcp_syns + s.tcp_tws,
2282 s.tcp_total - (s.tcp4_hashed+s.tcp6_hashed-s.tcp_tws),
2285 s.tcp_tws, slabstat.tcp_tws,
2290 printf("Transport Total IP IPv6\n");
2291 printf("* %-9d %-9s %-9s\n", slabstat.socks, "-", "-");
2292 printf("RAW %-9d %-9d %-9d\n", s.raw4+s.raw6, s.raw4, s.raw6);
2293 printf("UDP %-9d %-9d %-9d\n", s.udp4+s.udp6, s.udp4, s.udp6);
2294 printf("TCP %-9d %-9d %-9d\n", s.tcp4_hashed+s.tcp6_hashed, s.tcp4_hashed, s.tcp6_hashed);
2295 printf("INET %-9d %-9d %-9d\n",
2296 s.raw4+s.udp4+s.tcp4_hashed+
2297 s.raw6+s.udp6+s.tcp6_hashed,
2298 s.raw4+s.udp4+s.tcp4_hashed,
2299 s.raw6+s.udp6+s.tcp6_hashed);
2300 printf("FRAG %-9d %-9d %-9d\n", s.frag4+s.frag6, s.frag4, s.frag6);
2308 static void usage(void) __attribute__((noreturn));
2310 static void usage(void)
2313 "Usage: ss [ OPTIONS ]\n"
2314 " ss [ OPTIONS ] [ FILTER ]\n"
2315 "where OPTIONS := { -h[elp] | -V[ersion] | -n[umeric] | -r[esolve] |\n"
2316 " -a[ll] -l[istening] -o[ptions] -e[xtended] -p[rocesses]\n"
2317 " -A QUERY -i[nfo] } -s[ummary]\n"
2318 " -f[amily] { inet | inet6 | link | unix } }\n"
2319 " QUERY := {all|inet|tcp|udp|raw|unix|packet|netlink}[,QUERY]\n"
2320 " FILTER := [ state TCP-STATE ] [ EXPRESSION ]\n"
2326 int scan_state(const char *state)
2329 if (strcasecmp(state, "close") == 0 ||
2330 strcasecmp(state, "closed") == 0)
2331 return (1<<SS_CLOSE);
2332 if (strcasecmp(state, "syn-rcv") == 0)
2333 return (1<<SS_SYN_RECV);
2334 if (matches(state, "established") == 0)
2335 return (1<<SS_ESTABLISHED);
2336 if (strcasecmp(state, "all") == 0)
2338 if (strcasecmp(state, "connected") == 0)
2339 return SS_ALL & ~((1<<SS_CLOSE)|(1<<SS_LISTEN));
2340 if (matches(state, "synchronized") == 0)
2341 return SS_ALL & ~((1<<SS_CLOSE)|(1<<SS_LISTEN)|(1<<SS_SYN_SENT));
2342 if (strcasecmp(state, "bucket") == 0)
2343 return (1<<SS_SYN_RECV)|(1<<SS_TIME_WAIT);
2344 if (strcasecmp(state, "big") == 0)
2345 return SS_ALL & ~((1<<SS_SYN_RECV)|(1<<SS_TIME_WAIT));
2346 for (i=0; i<SS_MAX; i++) {
2347 if (matches(state, sstate_namel[i]) == 0)
2353 int main(int argc, char *argv[])
2359 const char *dump_tcpdiag = NULL;
2360 FILE *filter_fp = NULL;
2363 memset(¤t_filter, 0, sizeof(current_filter));
2365 current_filter.states = default_filter.states;
2367 while ((ch = getopt(argc, argv, "h?aletuwxnro460spf:miA:D:F:vV")) != EOF) {
2370 resolve_services = 0;
2392 current_filter.dbs |= (1<<TCP_DB);
2396 current_filter.dbs |= (1<<UDP_DB);
2400 current_filter.dbs |= (1<<RAW_DB);
2404 current_filter.dbs |= UNIX_DBM;
2408 current_filter.states = SS_ALL;
2411 current_filter.states = (1<<SS_LISTEN);
2414 preferred_family = AF_INET;
2417 preferred_family = AF_INET6;
2420 preferred_family = AF_PACKET;
2423 if (strcmp(optarg, "inet") == 0)
2424 preferred_family = AF_INET;
2425 else if (strcmp(optarg, "inet6") == 0)
2426 preferred_family = AF_INET6;
2427 else if (strcmp(optarg, "link") == 0)
2428 preferred_family = AF_PACKET;
2429 else if (strcmp(optarg, "unix") == 0)
2430 preferred_family = AF_UNIX;
2431 else if (strcmp(optarg, "netlink") == 0)
2432 preferred_family = AF_NETLINK;
2433 else if (strcmp(optarg, "help") == 0)
2436 fprintf(stderr, "ss: \"%s\" is invalid family\n", optarg);
2444 current_filter.dbs = 0;
2450 if ((p1 = strchr(p, ',')) != NULL)
2452 if (strcmp(p, "all") == 0) {
2453 current_filter.dbs = ALL_DB;
2454 } else if (strcmp(p, "inet") == 0) {
2455 current_filter.dbs |= (1<<TCP_DB)|(1<<UDP_DB)|(1<<RAW_DB);
2456 } else if (strcmp(p, "udp") == 0) {
2457 current_filter.dbs |= (1<<UDP_DB);
2458 } else if (strcmp(p, "tcp") == 0) {
2459 current_filter.dbs |= (1<<TCP_DB);
2460 } else if (strcmp(p, "raw") == 0) {
2461 current_filter.dbs |= (1<<RAW_DB);
2462 } else if (strcmp(p, "unix") == 0) {
2463 current_filter.dbs |= UNIX_DBM;
2464 } else if (matches(p, "unix_stream") == 0 ||
2465 strcmp(p, "u_str") == 0) {
2466 current_filter.dbs |= (1<<UNIX_ST_DB);
2467 } else if (matches(p, "unix_dgram") == 0 ||
2468 strcmp(p, "u_dgr") == 0) {
2469 current_filter.dbs |= (1<<UNIX_DG_DB);
2470 } else if (strcmp(p, "packet") == 0) {
2471 current_filter.dbs |= PACKET_DBM;
2472 } else if (strcmp(p, "packet_raw") == 0 ||
2473 strcmp(p, "p_raw") == 0) {
2474 current_filter.dbs |= (1<<PACKET_R_DB);
2475 } else if (strcmp(p, "packet_dgram") == 0 ||
2476 strcmp(p, "p_dgr") == 0) {
2477 current_filter.dbs |= (1<<PACKET_DG_DB);
2478 } else if (strcmp(p, "netlink") == 0) {
2479 current_filter.dbs |= (1<<NETLINK_DB);
2481 fprintf(stderr, "ss: \"%s\" is illegal socket table id\n", p);
2492 dump_tcpdiag = optarg;
2496 fprintf(stderr, "More than one filter file\n");
2499 if (optarg[0] == '-')
2502 filter_fp = fopen(optarg, "r");
2504 perror("fopen filter file");
2510 printf("ss utility, iproute2-ss%s\n", SNAPSHOT);
2522 get_slabstat(&slabstat);
2526 if (do_default && argc == 0)
2531 current_filter.dbs = default_filter.dbs;
2533 if (preferred_family == AF_UNSPEC) {
2534 if (!(current_filter.dbs&~UNIX_DBM))
2535 preferred_family = AF_UNIX;
2536 else if (!(current_filter.dbs&~PACKET_DBM))
2537 preferred_family = AF_PACKET;
2538 else if (!(current_filter.dbs&~(1<<NETLINK_DB)))
2539 preferred_family = AF_NETLINK;
2542 if (preferred_family != AF_UNSPEC) {
2544 if (preferred_family == AF_INET ||
2545 preferred_family == AF_INET6) {
2548 mask2 = (1<<UDP_DB)|(1<<RAW_DB);
2549 } else if (preferred_family == AF_PACKET) {
2551 } else if (preferred_family == AF_UNIX) {
2553 } else if (preferred_family == AF_NETLINK) {
2554 mask2 = (1<<NETLINK_DB);
2560 current_filter.dbs = mask2;
2562 current_filter.dbs &= mask2;
2563 current_filter.families = (1<<preferred_family);
2566 current_filter.families = ~0;
2568 current_filter.families = default_filter.families;
2570 if (current_filter.dbs == 0) {
2571 fprintf(stderr, "ss: no socket tables to show with such filter.\n");
2574 if (current_filter.families == 0) {
2575 fprintf(stderr, "ss: no families to show with such filter.\n");
2579 if (resolve_services && resolve_hosts &&
2580 (current_filter.dbs&(UNIX_DBM|(1<<TCP_DB)|(1<<UDP_DB))))
2581 init_service_resolver();
2583 /* Now parse filter... */
2584 if (argc == 0 && filter_fp) {
2585 if (ssfilter_parse(¤t_filter.f, 0, NULL, filter_fp))
2590 if (strcmp(*argv, "state") == 0) {
2593 current_filter.states = 0;
2594 current_filter.states |= scan_state(*argv);
2596 } else if (strcmp(*argv, "exclude") == 0 ||
2597 strcmp(*argv, "excl") == 0) {
2600 current_filter.states = SS_ALL;
2601 current_filter.states &= ~scan_state(*argv);
2604 if (ssfilter_parse(¤t_filter.f, argc, argv, filter_fp))
2611 if (current_filter.states == 0) {
2612 fprintf(stderr, "ss: no socket states to show with such filter.\n");
2617 FILE *dump_fp = stdout;
2618 if (!(current_filter.dbs & (1<<TCP_DB))) {
2619 fprintf(stderr, "ss: tcpdiag dump requested and no tcp in filter.\n");
2622 if (dump_tcpdiag[0] != '-') {
2623 dump_fp = fopen(dump_tcpdiag, "w");
2624 if (!dump_tcpdiag) {
2625 perror("fopen dump file");
2629 tcp_show_netlink(¤t_filter, dump_fp);
2635 if (current_filter.dbs&(current_filter.dbs-1))
2639 if (current_filter.states&(current_filter.states-1))
2643 if (isatty(STDOUT_FILENO)) {
2646 if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &w) != -1) {
2648 screen_width = w.ws_col;
2652 addrp_width = screen_width;
2653 addrp_width -= netid_width+1;
2654 addrp_width -= state_width+1;
2657 if (addrp_width&1) {
2660 else if (state_width)
2667 serv_width = resolve_services ? 7 : 5;
2669 if (addrp_width < 15+serv_width+1)
2670 addrp_width = 15+serv_width+1;
2672 addr_width = addrp_width - serv_width - 1;
2675 printf("%-*s ", netid_width, "Netid");
2677 printf("%-*s ", state_width, "State");
2678 printf("%-6s %-6s ", "Recv-Q", "Send-Q");
2680 printf("%*s:%-*s %*s:%-*s\n",
2681 addr_width, "Local Address", serv_width, "Port",
2682 addr_width, "Peer Address", serv_width, "Port");
2684 //printf("%08x %08x %08x\n", current_filter.dbs, current_filter.states, current_filter.families);
2687 if (current_filter.dbs & (1<<NETLINK_DB))
2688 netlink_show(¤t_filter);
2689 if (current_filter.dbs & PACKET_DBM)
2690 packet_show(¤t_filter);
2691 if (current_filter.dbs & UNIX_DBM)
2692 unix_show(¤t_filter);
2693 if (current_filter.dbs & (1<<RAW_DB))
2694 raw_show(¤t_filter);
2695 if (current_filter.dbs & (1<<UDP_DB))
2696 udp_show(¤t_filter);
2697 if (current_filter.dbs & (1<<TCP_DB))
2698 tcp_show(¤t_filter);