[PATCH iproute2 0/7] ss: Unify socket info output to common funcs
From: Vadim Kochan <hidden>
Date: 2015-02-13 20:25:11
Subsystems: library code, the rest · Maintainers: Andrew Morton, Linus Torvalds
Series: 7 patches touching 3 files, +317/-284
8 messages, 1 author, 2015-02-13 · open the first message on its own page
From: Vadim Kochan <hidden>
Date: 2015-02-13 20:25:11
From: Vadim Kochan <redacted> Split tcpstat struct into sockstat & tcpstat, old sockstat renamed to sksummary. Refactored printing socket info to common funcs. Also fixed wrong setting tcp ato value. Vadim Kochan (7): ss: Split tcpstap struct to sockstat & tcpstat ss: Replace pktstat struct by new sockstat struct ss: Replace unixstat struct by new sockstat struct ss: Unify state socket output:netid, state, rq, wq ss: Unify details info output:ino,uid,sk ss: Unify socket address output by one generic func ss: Fixed wrong tcp ato value from netlink include/utils.h | 2 + lib/utils.c | 6 + misc/ss.c | 539 +++++++++++++++++++++++++++++--------------------------- 3 files changed, 290 insertions(+), 257 deletions(-) -- 2.2.2
From: Vadim Kochan <hidden>
Date: 2015-02-13 20:25:12
From: Vadim Kochan <redacted> Signed-off-by: Vadim Kochan <redacted> --- misc/ss.c | 154 +++++++++++++++++++++++++++++++++++--------------------------- 1 file changed, 86 insertions(+), 68 deletions(-)
diff --git a/misc/ss.c b/misc/ss.c
index 0a6a65e..7e11fc0 100644
--- a/misc/ss.c
+++ b/misc/ss.c@@ -690,16 +690,7 @@ static const char *sstate_namel[] = { [SS_CLOSING] = "closing", }; -struct dctcpstat -{ - unsigned int ce_state; - unsigned int alpha; - unsigned int ab_ecn; - unsigned int ab_tot; - bool enabled; -}; - -struct tcpstat +struct sockstat { inet_prefix local; inet_prefix remote;
@@ -712,6 +703,20 @@ struct tcpstat int refcnt; unsigned int iface; unsigned long long sk; +}; + +struct dctcpstat +{ + unsigned int ce_state; + unsigned int alpha; + unsigned int ab_ecn; + unsigned int ab_tot; + bool enabled; +}; + +struct tcpstat +{ + struct sockstat ss; int timer; int timeout; int probes;
@@ -999,7 +1004,7 @@ static int unix_match(const inet_prefix *a, const inet_prefix *p) return !fnmatch(pattern, addr, 0); } -static int run_ssfilter(struct ssfilter *f, struct tcpstat *s) +static int run_ssfilter(struct ssfilter *f, struct sockstat *s) { switch (f->type) { case SSF_S_AUTO:
@@ -1484,7 +1489,7 @@ static char *proto_name(int protocol) return "???"; } -static void inet_stats_print(struct tcpstat *s, int protocol) +static void inet_stats_print(struct sockstat *s, int protocol) { char *buf = NULL;
@@ -1498,17 +1503,6 @@ static void inet_stats_print(struct tcpstat *s, int protocol) formatted_print(&s->local, s->lport, s->iface); formatted_print(&s->remote, s->rport, 0); - if (show_options) { - if (s->timer) { - if (s->timer > 4) - s->timer = 5; - printf(" timer:(%s,%s,%d)", - tmr_name[s->timer], - print_ms_timer(s->timeout), - s->retrans); - } - } - if (show_proc_ctx || show_sock_ctx) { if (find_entry(s->ino, &buf, (show_proc_ctx & show_sock_ctx) ?
@@ -1524,7 +1518,8 @@ static void inet_stats_print(struct tcpstat *s, int protocol) } } -static int proc_parse_inet_addr(char *loc, char *rem, int family, struct tcpstat *s) +static int proc_parse_inet_addr(char *loc, char *rem, int family, struct + sockstat *s) { s->local.family = s->remote.family = family; if (family == AF_INET) {
@@ -1655,7 +1650,7 @@ static void tcp_stats_print(struct tcpstat *s) printf(" retrans:%u/%u", s->retrans, s->retrans_total); if (s->lost) printf(" lost:%u", s->lost); - if (s->sacked && s->state != SS_LISTEN) + if (s->sacked && s->ss.state != SS_LISTEN) printf(" sacked:%u", s->sacked); if (s->fackets) printf(" fackets:%u", s->fackets);
@@ -1667,6 +1662,18 @@ static void tcp_stats_print(struct tcpstat *s) printf(" rcv_space:%d", s->rcv_space); } +static void tcp_timer_print(struct tcpstat *s) +{ + if (s->timer) { + if (s->timer > 4) + s->timer = 5; + printf(" timer:(%s,%s,%d)", + tmr_name[s->timer], + print_ms_timer(s->timeout), + s->retrans); + } +} + static int tcp_show_line(char *line, const struct filter *f, int family) { int rto = 0, ato = 0;
@@ -1683,17 +1690,17 @@ static int tcp_show_line(char *line, const struct filter *f, int family) if (!(f->states & (1 << state))) return 0; - proc_parse_inet_addr(loc, rem, family, &s); + proc_parse_inet_addr(loc, rem, family, &s.ss); - if (f->f && run_ssfilter(f->f, &s) == 0) + if (f->f && run_ssfilter(f->f, &s.ss) == 0) return 0; opt[0] = 0; n = sscanf(data, "%x %x:%x %x:%x %x %d %d %u %d %llx %d %d %d %d %d %[^\n]\n", - &s.state, &s.wq, &s.rq, - &s.timer, &s.timeout, &s.retrans, &s.uid, &s.probes, &s.ino, - &s.refcnt, &s.sk, &rto, &ato, &s.qack, - &s.cwnd, &s.ssthresh, opt); + &s.ss.state, &s.ss.wq, &s.ss.rq, + &s.timer, &s.timeout, &s.retrans, &s.ss.uid, &s.probes, + &s.ss.ino, &s.ss.refcnt, &s.ss.sk, &rto, &ato, &s.qack, &s.cwnd, + &s.ssthresh, opt); if (n < 17) opt[0] = 0;
@@ -1713,13 +1720,16 @@ static int tcp_show_line(char *line, const struct filter *f, int family) s.ssthresh = s.ssthresh == -1 ? 0 : s.ssthresh; s.rto = s.rto != 3 * hz ? s.rto / hz : 0; - inet_stats_print(&s, IPPROTO_TCP); + inet_stats_print(&s.ss, IPPROTO_TCP); + + if (show_options) + tcp_timer_print(&s); if (show_details) { - if (s.uid) - printf(" uid:%u", (unsigned)s.uid); - printf(" ino:%u", s.ino); - printf(" sk:%llx", s.sk); + if (s.ss.uid) + printf(" uid:%u", (unsigned)s.ss.uid); + printf(" ino:%u", s.ss.ino); + printf(" sk:%llx", s.ss.sk); if (opt[0]) printf(" opt:\"%s\"", opt); }
@@ -1804,6 +1814,8 @@ static void tcp_show_info(const struct nlmsghdr *nlh, struct inet_diag_msg *r, double rtt = 0; struct tcpstat s = {}; + s.ss.state = r->idiag_state; + print_skmeminfo(tb, INET_DIAG_SKMEMINFO); if (tb[INET_DIAG_INFO]) {
@@ -1913,7 +1925,7 @@ static int inet_show_sock(struct nlmsghdr *nlh, struct filter *f, int protocol) { struct rtattr * tb[INET_DIAG_MAX+1]; struct inet_diag_msg *r = NLMSG_DATA(nlh); - struct tcpstat s = {}; + struct sockstat s = {}; parse_rtattr(tb, INET_DIAG_MAX, (struct rtattr*)(r+1), nlh->nlmsg_len - NLMSG_LENGTH(sizeof(*r)));
@@ -1924,9 +1936,6 @@ static int inet_show_sock(struct nlmsghdr *nlh, struct filter *f, int protocol) s.rport = ntohs(r->id.idiag_dport); s.wq = r->idiag_wqueue; s.rq = r->idiag_rqueue; - s.timer = r->idiag_timer; - s.timeout = r->idiag_expires; - s.retrans = r->idiag_retrans; s.ino = r->idiag_inode; s.uid = r->idiag_uid; s.iface = r->id.idiag_if;
@@ -1945,6 +1954,15 @@ static int inet_show_sock(struct nlmsghdr *nlh, struct filter *f, int protocol) inet_stats_print(&s, protocol); + if (show_options) { + struct tcpstat t = {}; + + t.timer = r->idiag_timer; + t.timeout = r->idiag_expires; + t.retrans = r->idiag_retrans; + tcp_timer_print(&t); + } + if (show_details) { if (r->idiag_uid) printf(" uid:%u", (unsigned)r->idiag_uid);
@@ -2286,7 +2304,7 @@ outerr: static int dgram_show_line(char *line, const struct filter *f, int family) { - struct tcpstat s = {}; + struct sockstat s = {}; char *loc, *rem, *data; char opt[256]; int n;
@@ -2483,15 +2501,15 @@ static void unix_stats_print(struct unixstat *list, struct filter *f) } if (f->f) { - struct tcpstat tst; - tst.local.family = AF_UNIX; - tst.remote.family = AF_UNIX; - memcpy(tst.local.data, &s->name, sizeof(s->name)); + struct sockstat st; + st.local.family = AF_UNIX; + st.remote.family = AF_UNIX; + memcpy(st.local.data, &s->name, sizeof(s->name)); if (strcmp(peer, "*") == 0) - memset(tst.remote.data, 0, sizeof(peer)); + memset(st.remote.data, 0, sizeof(peer)); else - memcpy(tst.remote.data, &peer, sizeof(peer)); - if (run_ssfilter(f->f, &tst) == 0) + memcpy(st.remote.data, &peer, sizeof(peer)); + if (run_ssfilter(f->f, &st) == 0) continue; }
@@ -2716,14 +2734,14 @@ static int packet_stats_print(struct pktstat *s, const struct filter *f) char *buf = NULL; if (f->f) { - struct tcpstat tst; - tst.local.family = AF_PACKET; - tst.remote.family = AF_PACKET; - tst.rport = 0; - tst.lport = s->iface; - tst.local.data[0] = s->prot; - tst.remote.data[0] = 0; - if (run_ssfilter(f->f, &tst) == 0) + struct sockstat st; + st.local.family = AF_PACKET; + st.remote.family = AF_PACKET; + st.rport = 0; + st.lport = s->iface; + st.local.data[0] = s->prot; + st.remote.data[0] = 0; + if (run_ssfilter(f->f, &st) == 0) return 1; }
@@ -2899,14 +2917,14 @@ static void netlink_show_one(struct filter *f, SPRINT_BUF(prot_name); if (f->f) { - struct tcpstat tst; - tst.local.family = AF_NETLINK; - tst.remote.family = AF_NETLINK; - tst.rport = -1; - tst.lport = pid; - tst.local.data[0] = prot; - tst.remote.data[0] = 0; - if (run_ssfilter(f->f, &tst) == 0) + struct sockstat st; + st.local.family = AF_NETLINK; + st.remote.family = AF_NETLINK; + st.rport = -1; + st.lport = pid; + st.local.data[0] = prot; + st.remote.data[0] = 0; + if (run_ssfilter(f->f, &st) == 0) return; }
@@ -3117,7 +3135,7 @@ static int get_snmp_int(char *proto, char *key, int *result) /* Get stats from sockstat */ -struct sockstat +struct ssummary { int socks; int tcp_mem;
@@ -3136,7 +3154,7 @@ struct sockstat int frag6_mem; }; -static void get_sockstat_line(char *line, struct sockstat *s) +static void get_sockstat_line(char *line, struct ssummary *s) { char id[256], rem[256];
@@ -3165,7 +3183,7 @@ static void get_sockstat_line(char *line, struct sockstat *s) &s->tcp_orphans, &s->tcp_tws, &s->tcp_total, &s->tcp_mem); } -static int get_sockstat(struct sockstat *s) +static int get_sockstat(struct ssummary *s) { char buf[256]; FILE *fp;
@@ -3189,7 +3207,7 @@ static int get_sockstat(struct sockstat *s) static int print_summary(void) { - struct sockstat s; + struct ssummary s; struct snmpstat sn; if (get_sockstat(&s) < 0)
--
2.2.2
From: Vadim Kochan <hidden>
Date: 2015-02-13 20:25:13
From: Vadim Kochan <redacted> Signed-off-by: Vadim Kochan <redacted> --- misc/ss.c | 33 ++++++++++++--------------------- 1 file changed, 12 insertions(+), 21 deletions(-)
diff --git a/misc/ss.c b/misc/ss.c
index 7e11fc0..f033892 100644
--- a/misc/ss.c
+++ b/misc/ss.c@@ -692,6 +692,8 @@ static const char *sstate_namel[] = { struct sockstat { + uint8_t type; + uint16_t prot; inet_prefix local; inet_prefix remote; int lport;
@@ -2719,29 +2721,18 @@ static int unix_show(struct filter *f) return 0; } -struct pktstat { - uint8_t type; - uint16_t prot; - uint32_t iface; - int state; - uint32_t rq; - uid_t uid; - ino_t ino; -}; - -static int packet_stats_print(struct pktstat *s, const struct filter *f) +static int packet_stats_print(struct sockstat *s, const struct filter *f) { char *buf = NULL; if (f->f) { - struct sockstat st; - st.local.family = AF_PACKET; - st.remote.family = AF_PACKET; - st.rport = 0; - st.lport = s->iface; - st.local.data[0] = s->prot; - st.remote.data[0] = 0; - if (run_ssfilter(f->f, &st) == 0) + s->local.family = AF_PACKET; + s->remote.family = AF_PACKET; + s->rport = 0; + s->lport = s->iface; + s->local.data[0] = s->prot; + s->remote.data[0] = 0; + if (run_ssfilter(f->f, s) == 0) return 1; }
@@ -2790,7 +2781,7 @@ static int packet_show_sock(const struct sockaddr_nl *addr, const struct filter *f = arg; struct packet_diag_msg *r = NLMSG_DATA(nlh); struct rtattr *tb[PACKET_DIAG_MAX+1]; - struct pktstat stat = {}; + struct sockstat stat = {}; parse_rtattr(tb, PACKET_DIAG_MAX, (struct rtattr*)(r+1), nlh->nlmsg_len - NLMSG_LENGTH(sizeof(*r)));
@@ -2859,7 +2850,7 @@ static int packet_show_netlink(struct filter *f) static int packet_show_line(char *buf, const struct filter *f, int fam) { unsigned long long sk; - struct pktstat stat = {}; + struct sockstat stat = {}; int type, prot, iface, state, rq, uid, ino; sscanf(buf, "%llx %*d %d %x %d %d %u %u %u",
--
2.2.2
From: Vadim Kochan <hidden>
Date: 2015-02-13 20:25:14
From: Vadim Kochan <redacted> Signed-off-by: Vadim Kochan <redacted> --- misc/ss.c | 141 +++++++++++++++++++++++++++++++++----------------------------- 1 file changed, 75 insertions(+), 66 deletions(-)
diff --git a/misc/ss.c b/misc/ss.c
index f033892..54f2e21 100644
--- a/misc/ss.c
+++ b/misc/ss.c@@ -660,6 +660,18 @@ static int get_slabstat(struct slabstat *s) return 0; } +static inline void sock_addr_set_str(inet_prefix *prefix, char **ptr) +{ + memcpy(prefix->data, ptr, sizeof(char *)); +} + +static inline char *sock_addr_get_str(const inet_prefix *prefix) +{ + char *tmp ; + memcpy(&tmp, prefix->data, sizeof(char *)); + return tmp; +} + static const char *sstate_name[] = { "UNKNOWN", [SS_ESTABLISHED] = "ESTAB",
@@ -692,7 +704,8 @@ static const char *sstate_namel[] = { struct sockstat { - uint8_t type; + struct sockstat *next; + unsigned int type; uint16_t prot; inet_prefix local; inet_prefix remote;
@@ -996,9 +1009,9 @@ static int inet2_addr_match(const inet_prefix *a, const inet_prefix *p, static int unix_match(const inet_prefix *a, const inet_prefix *p) { - char *addr, *pattern; - memcpy(&addr, a->data, sizeof(addr)); - memcpy(&pattern, p->data, sizeof(pattern)); + char *addr = sock_addr_get_str(a); + char *pattern = sock_addr_get_str(p); + if (pattern == NULL) return 1; if (addr == NULL)
@@ -1014,8 +1027,7 @@ static int run_ssfilter(struct ssfilter *f, struct sockstat *s) static int low, high=65535; if (s->local.family == AF_UNIX) { - char *p; - memcpy(&p, s->local.data, sizeof(p)); + char *p = sock_addr_get_str(&s->local); return p == NULL || (p[0] == '@' && strlen(p) == 6 && strspn(p+1, "0123456789abcdef") == 5); }
@@ -1338,7 +1350,7 @@ void *parse_hostcond(char *addr) addr+=5; p = strdup(addr); a.addr.bitlen = 8*strlen(p); - memcpy(a.addr.data, &p, sizeof(p)); + sock_addr_set_str(&a.addr, &p); fam = AF_UNIX; goto out; }
@@ -2415,31 +2427,21 @@ outerr: } while (0); } -struct unixstat -{ - struct unixstat *next; - int ino; - int peer; - char *peer_name; - int rq; - int wq; - int state; - int type; - char *name; -}; - int unix_state_map[] = { SS_CLOSE, SS_SYN_SENT, SS_ESTABLISHED, SS_CLOSING }; -#define MAX_UNIX_REMEMBER (1024*1024/sizeof(struct unixstat)) +#define MAX_UNIX_REMEMBER (1024*1024/sizeof(struct sockstat)) -static void unix_list_free(struct unixstat *list) +static void unix_list_free(struct sockstat *list) { while (list) { - struct unixstat *s = list; + struct sockstat *s = list; + char *name = sock_addr_get_str(&s->local); + list = list->next; - if (s->name) - free(s->name); + + if (name) + free(name); free(s); } }
@@ -2463,7 +2465,7 @@ static const char *unix_netid_name(int type) return netid; } -static bool unix_type_skip(struct unixstat *s, struct filter *f) +static bool unix_type_skip(struct sockstat *s, struct filter *f) { if (s->type == SOCK_STREAM && !(f->dbs&(1<<UNIX_ST_DB))) return true;
@@ -2474,44 +2476,49 @@ static bool unix_type_skip(struct unixstat *s, struct filter *f) return false; } -static void unix_stats_print(struct unixstat *list, struct filter *f) +static bool unix_use_proc(void) +{ + return getenv("PROC_NET_UNIX") || getenv("PROC_ROOT"); +} + +static void unix_stats_print(struct sockstat *list, struct filter *f) { - struct unixstat *s; - char *peer; + struct sockstat *s; + char *local, *peer; + bool use_proc = unix_use_proc(); for (s = list; s; s = s->next) { - if (!(f->states & (1<<s->state))) + if (!(f->states & (1 << s->state))) continue; if (unix_type_skip(s, f)) continue; - peer = "*"; - if (s->peer_name) - peer = s->peer_name; + local = sock_addr_get_str(&s->local); + peer = "*"; + + if (s->rport && use_proc) { + struct sockstat *p; - if (s->peer && !s->peer_name) { - struct unixstat *p; for (p = list; p; p = p->next) { - if (s->peer == p->ino) + if (s->rport == p->lport) break; } + if (!p) { peer = "?"; } else { - peer = p->name ? : "*"; + peer = sock_addr_get_str(&p->local); + peer = peer ? : "*"; } } if (f->f) { - struct sockstat st; - st.local.family = AF_UNIX; - st.remote.family = AF_UNIX; - memcpy(st.local.data, &s->name, sizeof(s->name)); if (strcmp(peer, "*") == 0) - memset(st.remote.data, 0, sizeof(peer)); + memset(s->remote.data, 0, sizeof(char *)); else - memcpy(st.remote.data, &peer, sizeof(peer)); - if (run_ssfilter(f->f, &st) == 0) + sock_addr_set_str(&s->remote, &peer); + + if (run_ssfilter(f->f, s) == 0) continue; }
@@ -2522,8 +2529,8 @@ static void unix_stats_print(struct unixstat *list, struct filter *f) printf("%-*s ", state_width, sstate_name[s->state]); printf("%-6d %-6d ", s->rq, s->wq); printf("%*s %-*d %*s %-*d", - addr_width, s->name ? : "*", serv_width, s->ino, - addr_width, peer, serv_width, s->peer); + addr_width, local ? : "*", serv_width, + s->lport, addr_width, peer, serv_width, s->rport); char *buf = NULL; if (show_proc_ctx || show_sock_ctx) {
@@ -2549,15 +2556,16 @@ static int unix_show_sock(const struct sockaddr_nl *addr, struct nlmsghdr *nlh, struct filter *f = (struct filter *)arg; struct unix_diag_msg *r = NLMSG_DATA(nlh); struct rtattr *tb[UNIX_DIAG_MAX+1]; - char name[128]; - struct unixstat stat = { .name = "*" , .peer_name = "*" }; + char *name = NULL; + struct sockstat stat = {}; parse_rtattr(tb, UNIX_DIAG_MAX, (struct rtattr*)(r+1), nlh->nlmsg_len - NLMSG_LENGTH(sizeof(*r))); stat.type = r->udiag_type; stat.state = r->udiag_state; - stat.ino = r->udiag_ino; + stat.ino = stat.lport = r->udiag_ino; + stat.local.family = stat.remote.family = AF_UNIX; if (unix_type_skip(&stat, f)) return 0;
@@ -2570,14 +2578,15 @@ static int unix_show_sock(const struct sockaddr_nl *addr, struct nlmsghdr *nlh, if (tb[UNIX_DIAG_NAME]) { int len = RTA_PAYLOAD(tb[UNIX_DIAG_NAME]); + name = malloc(len + 1); memcpy(name, RTA_DATA(tb[UNIX_DIAG_NAME]), len); name[len] = '\0'; if (name[0] == '\0') name[0] = '@'; - stat.name = &name[0]; + sock_addr_set_str(&stat.local, &name); } if (tb[UNIX_DIAG_PEER]) - stat.peer = rta_getattr_u32(tb[UNIX_DIAG_PEER]); + stat.rport = rta_getattr_u32(tb[UNIX_DIAG_PEER]); unix_stats_print(&stat, f);
@@ -2594,6 +2603,9 @@ static int unix_show_sock(const struct sockaddr_nl *addr, struct nlmsghdr *nlh, } if (show_mem || show_details) printf("\n"); + + if (name) + free(name); return 0; }
@@ -2640,13 +2652,12 @@ static int unix_show(struct filter *f) char name[128]; int newformat = 0; int cnt; - struct unixstat *list = NULL; + struct sockstat *list = NULL; if (!filter_af_get(f, AF_UNIX)) return 0; - if (!getenv("PROC_NET_UNIX") && !getenv("PROC_ROOT") - && unix_show_netlink(f) == 0) + if (!unix_use_proc() && unix_show_netlink(f) == 0) return 0; if ((fp = net_unix_open()) == NULL)
@@ -2658,31 +2669,30 @@ static int unix_show(struct filter *f) cnt = 0; while (fgets(buf, sizeof(buf)-1, fp)) { - struct unixstat *u, **insp; + struct sockstat *u, **insp; int flags; if (!(u = malloc(sizeof(*u)))) break; - u->name = NULL; - u->peer_name = NULL; if (sscanf(buf, "%x: %x %x %x %x %x %d %s", - &u->peer, &u->rq, &u->wq, &flags, &u->type, + &u->rport, &u->rq, &u->wq, &flags, &u->type, &u->state, &u->ino, name) < 8) name[0] = 0; - if (flags&(1<<16)) { + u->lport = u->ino; + u->local.family = u->remote.family = AF_UNIX; + + if (flags & (1 << 16)) { u->state = SS_LISTEN; } else { u->state = unix_state_map[u->state-1]; - if (u->type == SOCK_DGRAM && - u->state == SS_CLOSE && - u->peer) + if (u->type == SOCK_DGRAM && u->state == SS_CLOSE && u->rport) u->state = SS_ESTABLISHED; } if (!newformat) { - u->peer = 0; + u->rport = 0; u->rq = 0; u->wq = 0; }
@@ -2699,9 +2709,8 @@ static int unix_show(struct filter *f) *insp = u; if (name[0]) { - if ((u->name = malloc(strlen(name)+1)) == NULL) - break; - strcpy(u->name, name); + char *tmp = strdup(name); + sock_addr_set_str(&u->local, &tmp); } if (++cnt > MAX_UNIX_REMEMBER) { unix_stats_print(list, f);
--
2.2.2
From: Vadim Kochan <hidden>
Date: 2015-02-13 20:25:15
From: Vadim Kochan <redacted> Signed-off-by: Vadim Kochan <redacted> --- misc/ss.c | 61 ++++++++++++++++++++++++++++--------------------------------- 1 file changed, 28 insertions(+), 33 deletions(-)
diff --git a/misc/ss.c b/misc/ss.c
index 54f2e21..305ab38 100644
--- a/misc/ss.c
+++ b/misc/ss.c@@ -765,6 +765,16 @@ struct tcpstat struct dctcpstat *dctcp; }; +static void sock_state_print(struct sockstat *s, const char *sock_name) +{ + if (netid_width) + printf("%-*s ", netid_width, sock_name); + if (state_width) + printf("%-*s ", state_width, sstate_name[s->state]); + + printf("%-6d %-6d ", s->rq, s->wq); +} + static const char *tmr_name[] = { "off", "on",
@@ -1507,12 +1517,7 @@ static void inet_stats_print(struct sockstat *s, int protocol) { char *buf = NULL; - if (netid_width) - printf("%-*s ", netid_width, proto_name(protocol)); - if (state_width) - printf("%-*s ", state_width, sstate_name[s->state]); - - printf("%-6d %-6d ", s->rq, s->wq); + sock_state_print(s, proto_name(protocol)); formatted_print(&s->local, s->lport, s->iface); formatted_print(&s->remote, s->rport, 0);
@@ -2522,12 +2527,8 @@ static void unix_stats_print(struct sockstat *list, struct filter *f) continue; } - if (netid_width) - printf("%-*s ", netid_width, - unix_netid_name(s->type)); - if (state_width) - printf("%-*s ", state_width, sstate_name[s->state]); - printf("%-6d %-6d ", s->rq, s->wq); + sock_state_print(s, unix_netid_name(s->type)); + printf("%*s %-*d %*s %-*d", addr_width, local ? : "*", serv_width, s->lport, addr_width, peer, serv_width, s->rport);
@@ -2737,21 +2738,13 @@ static int packet_stats_print(struct sockstat *s, const struct filter *f) if (f->f) { s->local.family = AF_PACKET; s->remote.family = AF_PACKET; - s->rport = 0; - s->lport = s->iface; s->local.data[0] = s->prot; - s->remote.data[0] = 0; if (run_ssfilter(f->f, s) == 0) return 1; } - if (netid_width) - printf("%-*s ", netid_width, - s->type == SOCK_RAW ? "p_raw" : "p_dgr"); - if (state_width) - printf("%-*s ", state_width, "UNCONN"); + sock_state_print(s, s->type == SOCK_RAW ? "p_raw" : "p_dgr"); - printf("%-6d %-6d ", s->rq, 0); if (s->prot == 3) { printf("%*s:", addr_width, "*"); } else {
@@ -2799,9 +2792,10 @@ static int packet_show_sock(const struct sockaddr_nl *addr, if (!tb[PACKET_DIAG_MEMINFO]) return -1; - stat.type = r->pdiag_type; - stat.prot = r->pdiag_num; - stat.ino = r->pdiag_ino; + stat.type = r->pdiag_type; + stat.prot = r->pdiag_num; + stat.ino = r->pdiag_ino; + stat.state = SS_CLOSE; if (tb[PACKET_DIAG_MEMINFO]) { __u32 *skmeminfo = RTA_DATA(tb[PACKET_DIAG_MEMINFO]);
@@ -2810,7 +2804,7 @@ static int packet_show_sock(const struct sockaddr_nl *addr, if (tb[PACKET_DIAG_INFO]) { struct packet_diag_info *pinfo = RTA_DATA(tb[PACKET_DIAG_INFO]); - stat.iface = pinfo->pdi_index; + stat.lport = stat.iface = pinfo->pdi_index; } if (packet_stats_print(&stat, f))
@@ -2874,11 +2868,13 @@ static int packet_show_line(char *buf, const struct filter *f, int fam) stat.type = type; stat.prot = prot; - stat.iface = iface; + stat.lport = stat.iface = iface; stat.state = state; stat.rq = rq; stat.uid = uid; stat.ino = ino; + stat.state = SS_CLOSE; + if (packet_stats_print(&stat, f)) return 0;
@@ -2914,25 +2910,24 @@ static void netlink_show_one(struct filter *f, int rq, int wq, unsigned long long sk, unsigned long long cb) { + struct sockstat st; SPRINT_BUF(prot_name); + st.state = SS_CLOSE; + st.rq = rq; + st.wq = wq; + if (f->f) { - struct sockstat st; st.local.family = AF_NETLINK; st.remote.family = AF_NETLINK; st.rport = -1; st.lport = pid; st.local.data[0] = prot; - st.remote.data[0] = 0; if (run_ssfilter(f->f, &st) == 0) return; } - if (netid_width) - printf("%-*s ", netid_width, "nl"); - if (state_width) - printf("%-*s ", state_width, "UNCONN"); - printf("%-6d %-6d ", rq, wq); + sock_state_print(&st, "nl"); if (resolve_services) { printf("%*s:", addr_width, nl_proto_n2a(prot, prot_name,
--
2.2.2
From: Vadim Kochan <hidden>
Date: 2015-02-13 20:25:17
From: Vadim Kochan <redacted> Signed-off-by: Vadim Kochan <redacted> --- misc/ss.c | 78 ++++++++++++++++++++++++++++----------------------------------- 1 file changed, 35 insertions(+), 43 deletions(-)
diff --git a/misc/ss.c b/misc/ss.c
index 305ab38..905cec9 100644
--- a/misc/ss.c
+++ b/misc/ss.c@@ -672,6 +672,11 @@ static inline char *sock_addr_get_str(const inet_prefix *prefix) return tmp; } +static unsigned long cookie_sk_get(uint32_t *cookie) +{ + return (((unsigned long)cookie[1] << 31) << 1) | cookie[0]; +} + static const char *sstate_name[] = { "UNKNOWN", [SS_ESTABLISHED] = "ESTAB",
@@ -775,6 +780,15 @@ static void sock_state_print(struct sockstat *s, const char *sock_name) printf("%-6d %-6d ", s->rq, s->wq); } +static void sock_details_print(struct sockstat *s) +{ + if (s->uid) + printf(" uid:%u", s->uid); + + printf(" ino:%u", s->ino); + printf(" sk:%llx", s->sk); +} + static const char *tmr_name[] = { "off", "on",
@@ -1745,10 +1759,7 @@ static int tcp_show_line(char *line, const struct filter *f, int family) tcp_timer_print(&s); if (show_details) { - if (s.ss.uid) - printf(" uid:%u", (unsigned)s.ss.uid); - printf(" ino:%u", s.ss.ino); - printf(" sk:%llx", s.ss.sk); + sock_details_print(&s.ss); if (opt[0]) printf(" opt:\"%s\"", opt); }
@@ -1949,15 +1960,16 @@ static int inet_show_sock(struct nlmsghdr *nlh, struct filter *f, int protocol) parse_rtattr(tb, INET_DIAG_MAX, (struct rtattr*)(r+1), nlh->nlmsg_len - NLMSG_LENGTH(sizeof(*r))); - s.state = r->idiag_state; - s.local.family = s.remote.family = r->idiag_family; - s.lport = ntohs(r->id.idiag_sport); - s.rport = ntohs(r->id.idiag_dport); - s.wq = r->idiag_wqueue; - s.rq = r->idiag_rqueue; - s.ino = r->idiag_inode; - s.uid = r->idiag_uid; - s.iface = r->id.idiag_if; + s.state = r->idiag_state; + s.local.family = s.remote.family = r->idiag_family; + s.lport = ntohs(r->id.idiag_sport); + s.rport = ntohs(r->id.idiag_dport); + s.wq = r->idiag_wqueue; + s.rq = r->idiag_rqueue; + s.ino = r->idiag_inode; + s.uid = r->idiag_uid; + s.iface = r->id.idiag_if; + s.sk = cookie_sk_get(&r->id.idiag_cookie[0]); if (s.local.family == AF_INET) { s.local.bytelen = s.remote.bytelen = 4;
@@ -1983,13 +1995,7 @@ static int inet_show_sock(struct nlmsghdr *nlh, struct filter *f, int protocol) } if (show_details) { - if (r->idiag_uid) - printf(" uid:%u", (unsigned)r->idiag_uid); - printf(" ino:%u", r->idiag_inode); - printf(" sk:"); - if (r->id.idiag_cookie[1] != 0) - printf("%08x", r->id.idiag_cookie[1]); - printf("%08x", r->id.idiag_cookie[0]); + sock_details_print(&s); if (tb[INET_DIAG_SHUTDOWN]) { unsigned char mask; mask = *(__u8 *)RTA_DATA(tb[INET_DIAG_SHUTDOWN]);
@@ -2351,14 +2357,8 @@ static int dgram_show_line(char *line, const struct filter *f, int family) inet_stats_print(&s, IPPROTO_UDP); - if (show_details) { - if (s.uid) - printf(" uid=%u", (unsigned)s.uid); - printf(" ino=%u", s.ino); - printf(" sk=%llx", s.sk); - if (opt[0]) - printf(" opt:\"%s\"", opt); - } + if (show_details && opt[0]) + printf(" opt:\"%s\"", opt); printf("\n"); return 0;
@@ -2774,6 +2774,9 @@ static int packet_stats_print(struct sockstat *s, const struct filter *f) } } + if (show_details) + sock_details_print(s); + return 0; }
@@ -2796,6 +2799,7 @@ static int packet_show_sock(const struct sockaddr_nl *addr, stat.prot = r->pdiag_num; stat.ino = r->pdiag_ino; stat.state = SS_CLOSE; + stat.sk = cookie_sk_get(&r->pdiag_cookie[0]); if (tb[PACKET_DIAG_MEMINFO]) { __u32 *skmeminfo = RTA_DATA(tb[PACKET_DIAG_MEMINFO]);
@@ -2807,21 +2811,12 @@ static int packet_show_sock(const struct sockaddr_nl *addr, stat.lport = stat.iface = pinfo->pdi_index; } + if (tb[PACKET_DIAG_UID]) + stat.uid = *(__u32 *)RTA_DATA(tb[PACKET_DIAG_UID]); + if (packet_stats_print(&stat, f)) return 0; - if (show_details) { - __u32 uid = 0; - - if (tb[PACKET_DIAG_UID]) - uid = *(__u32 *)RTA_DATA(tb[PACKET_DIAG_UID]); - - printf(" ino=%u uid=%u sk=", r->pdiag_ino, uid); - if (r->pdiag_cookie[1] != 0) - printf("%08x", r->pdiag_cookie[1]); - printf("%08x", r->pdiag_cookie[0]); - } - if (show_bpf && tb[PACKET_DIAG_FILTER]) { struct sock_filter *fil = RTA_DATA(tb[PACKET_DIAG_FILTER]);
@@ -2878,9 +2873,6 @@ static int packet_show_line(char *buf, const struct filter *f, int fam) if (packet_stats_print(&stat, f)) return 0; - if (show_details) { - printf(" ino=%u uid=%u sk=%llx", ino, uid, sk); - } printf("\n"); return 0; }
--
2.2.2
From: Vadim Kochan <hidden>
Date: 2015-02-13 20:25:18
From: Vadim Kochan <redacted> Signed-off-by: Vadim Kochan <redacted> --- include/utils.h | 2 + lib/utils.c | 6 +++ misc/ss.c | 124 ++++++++++++++++++++++++++++++++------------------------ 3 files changed, 80 insertions(+), 52 deletions(-)
diff --git a/include/utils.h b/include/utils.h
index 3da2283..fec9ef4 100644
--- a/include/utils.h
+++ b/include/utils.h@@ -167,4 +167,6 @@ int iplink_parse(int argc, char **argv, struct iplink_req *req, extern int do_each_netns(int (*func)(char *nsname, void *arg), void *arg, bool show_label); +char *int_to_str(int val, char *buf); + #endif /* __UTILS_H__ */
diff --git a/lib/utils.c b/lib/utils.c
index efebe18..e2b05bc 100644
--- a/lib/utils.c
+++ b/lib/utils.c@@ -906,3 +906,9 @@ int do_each_netns(int (*func)(char *nsname, void *arg), void *arg, return netns_foreach(on_netns, &nsf); } + +char *int_to_str(int val, char *buf) +{ + sprintf(buf, "%d", val); + return buf; +}
diff --git a/misc/ss.c b/misc/ss.c
index 905cec9..9cf0055 100644
--- a/misc/ss.c
+++ b/misc/ss.c@@ -789,6 +789,24 @@ static void sock_details_print(struct sockstat *s) printf(" sk:%llx", s->sk); } +static void sock_addr_print_width(int addr_len, const char *addr, char *delim, + int port_len, const char *port, const char *ifname) +{ + if (ifname) { + printf("%*s%%%s%s%-*s ", addr_len, addr, ifname, delim, + port_len, port); + } + else { + printf("%*s%s%-*s ", addr_len, addr, delim, port_len, port); + } +} + +static void sock_addr_print(const char *addr, char *delim, const char *port, + const char *ifname) +{ + sock_addr_print_width(addr_width, addr, delim, serv_width, port, ifname); +} + static const char *tmr_name[] = { "off", "on",
@@ -971,13 +989,12 @@ static const char *resolve_service(int port) return buf; } -static void formatted_print(const inet_prefix *a, int port, unsigned int ifindex) +static void inet_addr_print(const inet_prefix *a, int port, unsigned int ifindex) { char buf[1024]; const char *ap = buf; - int est_len; - - est_len = addr_width; + int est_len = addr_width; + const char *ifname = NULL; if (a->family == AF_INET) { if (a->data[0] == 0) {
@@ -994,14 +1011,14 @@ static void formatted_print(const inet_prefix *a, int port, unsigned int ifindex else est_len = addr_width + ((est_len-addr_width+3)/4)*4; } + if (ifindex) { - const char *ifname = ll_index_to_name(ifindex); - const int len = strlen(ifname) + 1; /* +1 for percent char */ + ifname = ll_index_to_name(ifindex); + est_len -= strlen(ifname) + 1; /* +1 for percent char */ + } - printf("%*s%%%s:%-*s ", est_len - len, ap, ifname, serv_width, - resolve_service(port)); - } else - printf("%*s:%-*s ", est_len, ap, serv_width, resolve_service(port)); + sock_addr_print_width(est_len, ap, ":", serv_width, resolve_service(port), + ifname); } struct aafilter
@@ -1533,8 +1550,8 @@ static void inet_stats_print(struct sockstat *s, int protocol) sock_state_print(s, proto_name(protocol)); - formatted_print(&s->local, s->lport, s->iface); - formatted_print(&s->remote, s->rport, 0); + inet_addr_print(&s->local, s->lport, s->iface); + inet_addr_print(&s->remote, s->rport, 0); if (show_proc_ctx || show_sock_ctx) { if (find_entry(s->ino, &buf,
@@ -2490,7 +2507,9 @@ static void unix_stats_print(struct sockstat *list, struct filter *f) { struct sockstat *s; char *local, *peer; + char *ctx_buf = NULL; bool use_proc = unix_use_proc(); + char port_name[30] = {}; for (s = list; s; s = s->next) { if (!(f->states & (1 << s->state)))
@@ -2529,22 +2548,22 @@ static void unix_stats_print(struct sockstat *list, struct filter *f) sock_state_print(s, unix_netid_name(s->type)); - printf("%*s %-*d %*s %-*d", - addr_width, local ? : "*", serv_width, - s->lport, addr_width, peer, serv_width, s->rport); - char *buf = NULL; + sock_addr_print(local ?: "*", " ", + int_to_str(s->lport, port_name), NULL); + sock_addr_print(peer, " ", int_to_str(s->rport, port_name), + NULL); if (show_proc_ctx || show_sock_ctx) { - if (find_entry(s->ino, &buf, + if (find_entry(s->ino, &ctx_buf, (show_proc_ctx & show_sock_ctx) ? PROC_SOCK_CTX : PROC_CTX) > 0) { - printf(" users:(%s)", buf); - free(buf); + printf(" users:(%s)", ctx_buf); + free(ctx_buf); } } else if (show_users) { - if (find_entry(s->ino, &buf, USERS) > 0) { - printf(" users:(%s)", buf); - free(buf); + if (find_entry(s->ino, &ctx_buf, USERS) > 0) { + printf(" users:(%s)", ctx_buf); + free(ctx_buf); } } printf("\n");
@@ -2734,6 +2753,8 @@ static int unix_show(struct filter *f) static int packet_stats_print(struct sockstat *s, const struct filter *f) { char *buf = NULL; + const char *addr, *port; + char ll_name[16]; if (f->f) { s->local.family = AF_PACKET;
@@ -2745,20 +2766,18 @@ static int packet_stats_print(struct sockstat *s, const struct filter *f) sock_state_print(s, s->type == SOCK_RAW ? "p_raw" : "p_dgr"); - if (s->prot == 3) { - printf("%*s:", addr_width, "*"); - } else { - char tb[16]; - printf("%*s:", addr_width, - ll_proto_n2a(htons(s->prot), tb, sizeof(tb))); - } - if (s->iface == 0) { - printf("%-*s ", serv_width, "*"); - } else { - printf("%-*s ", serv_width, xll_index_to_name(s->iface)); - } + if (s->prot == 3) + addr = "*"; + else + addr = ll_proto_n2a(htons(s->prot), ll_name, sizeof(ll_name)); + + if (s->iface == 0) + port = "*"; + else + port = xll_index_to_name(s->iface); - printf("%*s*%-*s", addr_width, "", serv_width, ""); + sock_addr_print(addr, ":", port, NULL); + sock_addr_print("", "*", "", NULL); if (show_proc_ctx || show_sock_ctx) { if (find_entry(s->ino, &buf,
@@ -2903,7 +2922,9 @@ static void netlink_show_one(struct filter *f, unsigned long long sk, unsigned long long cb) { struct sockstat st; - SPRINT_BUF(prot_name); + SPRINT_BUF(prot_buf) = {}; + const char *prot_name; + char procname[64] = {}; st.state = SS_CLOSE; st.rq = rq;
@@ -2921,46 +2942,45 @@ static void netlink_show_one(struct filter *f, sock_state_print(&st, "nl"); - if (resolve_services) { - printf("%*s:", addr_width, nl_proto_n2a(prot, prot_name, - sizeof(prot_name))); - } else { - printf("%*d:", addr_width, prot); - } + if (resolve_services) + prot_name = nl_proto_n2a(prot, prot_buf, sizeof(prot_buf)); + else + prot_name = int_to_str(prot, prot_buf); if (pid == -1) { - printf("%-*s ", serv_width, "*"); + procname[0] = '*'; } else if (resolve_services) { int done = 0; if (!pid) { done = 1; - printf("%-*s ", serv_width, "kernel"); + strncpy(procname, "kernel", 6); } else if (pid > 0) { - char procname[64]; FILE *fp; sprintf(procname, "%s/%d/stat", getenv("PROC_ROOT") ? : "/proc", pid); if ((fp = fopen(procname, "r")) != NULL) { if (fscanf(fp, "%*d (%[^)])", procname) == 1) { sprintf(procname+strlen(procname), "/%d", pid); - printf("%-*s ", serv_width, procname); done = 1; } fclose(fp); } } if (!done) - printf("%-*d ", serv_width, pid); + int_to_str(pid, procname); } else { - printf("%-*d ", serv_width, pid); + int_to_str(pid, procname); } + sock_addr_print(prot_name, ":", procname, NULL); + if (state == NETLINK_CONNECTED) { - printf("%*d:%-*d", - addr_width, dst_group, serv_width, dst_pid); + char dst_group_buf[30]; + char dst_pid_buf[30]; + sock_addr_print(int_to_str(dst_group, dst_group_buf), ":", + int_to_str(dst_pid, dst_pid_buf), NULL); } else { - printf("%*s*%-*s", - addr_width, "", serv_width, ""); + sock_addr_print("", "*", "", NULL); } char *pid_context = NULL;
--
2.2.2
From: Vadim Kochan <hidden>
Date: 2015-02-13 20:25:19
From: Vadim Kochan <redacted> Signed-off-by: Vadim Kochan <redacted> --- misc/ss.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/misc/ss.c b/misc/ss.c
index 9cf0055..69794ea 100644
--- a/misc/ss.c
+++ b/misc/ss.c@@ -1903,7 +1903,7 @@ static void tcp_show_info(const struct nlmsghdr *nlh, struct inet_diag_msg *r, s.backoff = info->tcpi_backoff; s.rtt = (double)info->tcpi_rtt / 1000; s.rttvar = (double)info->tcpi_rttvar / 1000; - s.ato = (double)info->tcpi_rttvar / 1000; + s.ato = (double)info->tcpi_ato / 1000; s.mss = info->tcpi_snd_mss; s.rcv_space = info->tcpi_rcv_space; s.rcv_rtt = (double)info->tcpi_rcv_rtt / 1000;
--
2.2.2