net/proc: added sockets details statistics

Added detailed statistics of sockets separated by commas
to facilitate parsing.

Signed-off-by: Chun Liu<kaicliu@tencent.com>
This commit is contained in:
Liu Chun 2022-12-20 10:41:03 +08:00 committed by Jianping Liu
parent 39d0341de3
commit 71fea53537
4 changed files with 66 additions and 2 deletions

View File

@ -44,6 +44,34 @@
#define TCPUDP_MIB_MAX max_t(u32, UDP_MIB_MAX, TCP_MIB_MAX)
extern struct proto unix_proto;
extern struct proto netlink_proto;
/*
* Report socket allocation statistics
*/
static int sockets_seq_show(struct seq_file *seq, void *v)
{
int lite;
struct net *net = seq->private;
int sockets, tcp_sock, udp_sock;
int raw_sock, unix_sock, netlink_sock;
sockets = sock_inuse_get(net);
lite = sock_prot_inuse_get(net, &udplite_prot);
tcp_sock = sock_prot_inuse_get(net, &tcp_prot);
udp_sock = sock_prot_inuse_get(net, &udp_prot);
raw_sock = sock_prot_inuse_get(net, &raw_prot);
unix_sock = sock_prot_inuse_get(net, &unix_proto);
netlink_sock = sock_prot_inuse_get(net, &netlink_proto);
udp_sock += lite;
seq_printf (seq, "%d,%d,%d,%d,%d,%d\n", sockets, tcp_sock, udp_sock,
raw_sock, unix_sock, netlink_sock);
return 0;
}
/*
* Report socket allocation statistics [mea@utu.fi]
*/
@ -70,6 +98,9 @@ static int sockstat_seq_show(struct seq_file *seq, void *v)
seq_printf(seq, "FRAG: inuse %u memory %lu\n",
atomic_read(&net->ipv4.fqdir->rhashtable.nelems),
frag_mem_limit(net->ipv4.fqdir));
seq_printf(seq, "UNIX: inuse %d\n", sock_prot_inuse_get(net, &unix_proto));
seq_printf(seq, "NETLINK: inuse %d\n", sock_prot_inuse_get(net, &netlink_proto));
return 0;
}
@ -540,10 +571,15 @@ static __net_init int ip_proc_init_net(struct net *net)
if (!proc_create_net_single("dropstat", 0444, net->proc_net,
dropstat_seq_show, NULL))
goto out_dropstat;
if (!proc_create_net_single("sockets", 0444, net->proc_net,
sockets_seq_show, NULL))
goto out_dropsockets;
return 0;
out_dropsockets:
remove_proc_entry("dropstat", net->proc_net);
out_dropstat:
remove_proc_entry("snmp", net->proc_net);
out_snmp:
@ -560,6 +596,7 @@ static __net_exit void ip_proc_exit_net(struct net *net)
remove_proc_entry("netstat", net->proc_net);
remove_proc_entry("sockstat", net->proc_net);
remove_proc_entry("dropstat", net->proc_net);
remove_proc_entry("sockets", net->proc_net);
}
static __net_initdata struct pernet_operations ip_proc_ops = {

View File

@ -30,6 +30,23 @@
max_t(u32, max_t(u32, a, b), max_t(u32, c, d))
#define SNMP_MIB_MAX MAX4(UDP_MIB_MAX, TCP_MIB_MAX, \
IPSTATS_MIB_MAX, ICMP_MIB_MAX)
static int sockets6_seq_show(struct seq_file *seq, void *v)
{
int lite;
struct net *net = seq->private;
int sockets, tcp_sock, udp_sock, raw_sock;
sockets = sock_inuse_get(net);
lite = sock_prot_inuse_get(net, &udplitev6_prot);
tcp_sock = sock_prot_inuse_get(net, &tcpv6_prot);
udp_sock = sock_prot_inuse_get(net, &udpv6_prot);
raw_sock = sock_prot_inuse_get(net, &rawv6_prot);
udp_sock += lite;
seq_printf (seq, "%d,%d,%d,%d\n", sockets, tcp_sock, udp_sock,
raw_sock);
return 0;
}
static int sockstat6_seq_show(struct seq_file *seq, void *v)
{
@ -285,8 +302,15 @@ static int __net_init ipv6_proc_init_net(struct net *net)
net->mib.proc_net_devsnmp6 = proc_mkdir("dev_snmp6", net->proc_net);
if (!net->mib.proc_net_devsnmp6)
goto proc_dev_snmp6_fail;
if (!proc_create_net_single("sockets6", 0444, net->proc_net,
sockets6_seq_show, NULL))
goto out_dropsockets;
return 0;
out_dropsockets:
remove_proc_entry("dev_snmp6", net->proc_net);
proc_dev_snmp6_fail:
remove_proc_entry("snmp6", net->proc_net);
proc_snmp6_fail:
@ -299,6 +323,7 @@ static void __net_exit ipv6_proc_exit_net(struct net *net)
remove_proc_entry("sockstat6", net->proc_net);
remove_proc_entry("dev_snmp6", net->proc_net);
remove_proc_entry("snmp6", net->proc_net);
remove_proc_entry("sockets6", net->proc_net);
}
static struct pernet_operations ipv6_proc_ops = {

View File

@ -612,11 +612,12 @@ static void netlink_remove(struct sock *sk)
netlink_table_ungrab();
}
static struct proto netlink_proto = {
struct proto netlink_proto = {
.name = "NETLINK",
.owner = THIS_MODULE,
.obj_size = sizeof(struct netlink_sock),
};
EXPORT_SYMBOL(netlink_proto);
static int __netlink_create(struct net *net, struct socket *sock,
struct mutex *cb_mutex, int protocol,

View File

@ -761,11 +761,12 @@ static const struct proto_ops unix_seqpacket_ops = {
.set_peek_off = unix_set_peek_off,
};
static struct proto unix_proto = {
struct proto unix_proto = {
.name = "UNIX",
.owner = THIS_MODULE,
.obj_size = sizeof(struct unix_sock),
};
EXPORT_SYMBOL(unix_proto);
static struct sock *unix_create1(struct net *net, struct socket *sock, int kern)
{