diff --git a/usr.bin/netstat/inet.c b/usr.bin/netstat/inet.c index b0a0a7fca050..47932294b22e 100644 --- a/usr.bin/netstat/inet.c +++ b/usr.bin/netstat/inet.c @@ -82,6 +82,7 @@ __FBSDID("$FreeBSD$"); #include #include #include "netstat.h" +#include "nl_defs.h" char *inetname(struct in_addr *); void inetprint(const char *, struct in_addr *, int, const char *, int, @@ -638,6 +639,7 @@ void tcp_stats(u_long off, const char *name, int af1 __unused, int proto __unused) { struct tcpstat tcpstat; + uint64_t tcps_states[TCP_NSTATES]; #ifdef INET6 if (tcp_done != 0) @@ -650,6 +652,10 @@ tcp_stats(u_long off, const char *name, int af1 __unused, int proto __unused) sizeof(tcpstat), kread_counters) != 0) return; + if (fetch_stats_ro("net.inet.tcp.states", nl[N_TCPS_STATES].n_value, + &tcps_states, sizeof(tcps_states), kread_counters) != 0) + return; + xo_open_container("tcp"); xo_emit("{T:/%s}:\n", name); @@ -853,6 +859,28 @@ tcp_stats(u_long off, const char *name, int af1 __unused, int proto __unused) #undef p2a #undef p3 xo_close_container("ecn"); + + xo_open_container("TCP connection count by state"); + xo_emit("{T:/TCP connection count by state}:\n"); + for (int i = 0; i < TCP_NSTATES; i++) { + /* + * XXXGL: is there a way in libxo to use %s + * in the "content string" of a format + * string? I failed to do that, that's why + * a temporary buffer is used to construct + * format string for xo_emit(). + */ + char fmtbuf[80]; + + if (sflag > 1 && tcps_states[i] == 0) + continue; + snprintf(fmtbuf, sizeof(fmtbuf), "\t{:%s/%%ju} " + "{Np:/connection ,connections} in %s state\n", + tcpstates[i], tcpstates[i]); + xo_emit(fmtbuf, (uintmax_t )tcps_states[i]); + } + xo_close_container("TCP connection count by state"); + xo_close_container("tcp"); } diff --git a/usr.bin/netstat/main.c b/usr.bin/netstat/main.c index e8cf914ae505..4e4428d9d087 100644 --- a/usr.bin/netstat/main.c +++ b/usr.bin/netstat/main.c @@ -551,15 +551,15 @@ main(int argc, char *argv[]) exit(0); } -int -fetch_stats(const char *sysctlname, u_long off, void *stats, size_t len, - int (*kreadfn)(u_long, void *, size_t)) +static int +fetch_stats_internal(const char *sysctlname, u_long off, void *stats, + size_t len, kreadfn_t kreadfn, int zero) { int error; if (live) { memset(stats, 0, len); - if (zflag) + if (zero) error = sysctlbyname(sysctlname, NULL, NULL, stats, len); else @@ -574,6 +574,23 @@ fetch_stats(const char *sysctlname, u_long off, void *stats, size_t len, return (error); } +int +fetch_stats(const char *sysctlname, u_long off, void *stats, + size_t len, kreadfn_t kreadfn) +{ + + return (fetch_stats_internal(sysctlname, off, stats, len, kreadfn, + zflag)); +} + +int +fetch_stats_ro(const char *sysctlname, u_long off, void *stats, + size_t len, kreadfn_t kreadfn) +{ + + return (fetch_stats_internal(sysctlname, off, stats, len, kreadfn, 0)); +} + /* * Print out protocol statistics or control blocks (per sflag). * If the interface was not specifically requested, and the symbol diff --git a/usr.bin/netstat/netstat.h b/usr.bin/netstat/netstat.h index 776c4d43e76b..4db28445e02d 100644 --- a/usr.bin/netstat/netstat.h +++ b/usr.bin/netstat/netstat.h @@ -63,8 +63,10 @@ extern int unit; /* unit number for above */ extern int live; /* true if we are examining a live system */ -int fetch_stats(const char *sysctlname, u_long addr, void *stats, - size_t len, int (*kreadfn)(u_long, void *, size_t)); +typedef int kreadfn_t(u_long, void *, size_t); +int fetch_stats(const char *, u_long, void *, size_t, kreadfn_t); +int fetch_stats_ro(const char *, u_long, void *, size_t, kreadfn_t); + int kread(u_long addr, void *buf, size_t size); uint64_t kread_counter(u_long addr); int kread_counters(u_long addr, void *buf, size_t size); diff --git a/usr.bin/netstat/nlist_symbols b/usr.bin/netstat/nlist_symbols index e2b7e2f25a36..afad45d353bd 100644 --- a/usr.bin/netstat/nlist_symbols +++ b/usr.bin/netstat/nlist_symbols @@ -44,6 +44,7 @@ all _sctpstat all _sfstat all _tcbinfo all _tcpstat +all _tcps_states all _udbinfo all _udpstat all _unp_count