netdev.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: igorm@etf.rs
To: netdev@vger.kernel.org
Cc: davem@davemloft.net, Igor Maravic <igorm@etf.rs>
Subject: [PATCH 09/10 net-next] net:ipv4:proc: Introduce proc files for ipv4 per interface stats
Date: Fri, 16 Dec 2011 16:26:02 +0100	[thread overview]
Message-ID: <1324049163-11207-10-git-send-email-igorm@etf.rs> (raw)
In-Reply-To: <1324049163-11207-1-git-send-email-igorm@etf.rs>

From: Igor Maravic <igorm@etf.rs>

In ip_proc_init_net dev_snmp proc directory is created.

Functions snmp_(un)register_dev are for creating/deleting proc files
that have same names as interfaces for which they are created.

Per device proc files for ipv4 interfaces have the same form as
per device proc files for ipv6 interfaces.

Signed-off-by: Igor Maravic <igorm@etf.rs>
---
 net/ipv4/proc.c |  121 +++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 121 insertions(+), 0 deletions(-)

diff --git a/net/ipv4/proc.c b/net/ipv4/proc.c
index 3569d8e..c6202d9 100644
--- a/net/ipv4/proc.c
+++ b/net/ipv4/proc.c
@@ -128,6 +128,14 @@ static const struct snmp_mib snmp4_ipextstats_list[] = {
 	SNMP_MIB_SENTINEL
 };
 
+static const struct snmp_mib snmp4_icmp_list[] = {
+	SNMP_MIB_ITEM("InMsgs", ICMP_MIB_INMSGS),
+	SNMP_MIB_ITEM("InErrors", ICMP_MIB_INERRORS),
+	SNMP_MIB_ITEM("OutMsgs", ICMP_MIB_OUTMSGS),
+	SNMP_MIB_ITEM("OutErrors", ICMP_MIB_OUTERRORS),
+	SNMP_MIB_SENTINEL
+};
+
 static const struct {
 	const char *name;
 	int index;
@@ -459,6 +467,113 @@ static const struct file_operations netstat_seq_fops = {
 	.release = single_release_net,
 };
 
+static void snmp_seq_show_item(struct seq_file *seq, void __percpu **pcpumib,
+				atomic_long_t *smib,
+				const struct snmp_mib *itemlist,
+				char *prefix)
+{
+	char name[32];
+	int i;
+	unsigned long val;
+
+	for (i = 0; itemlist[i].name; i++) {
+		val = pcpumib ?
+			snmp_fold_field64(pcpumib, itemlist[i].entry,
+				offsetof(struct ipstats_mib, syncp)) :
+			atomic_long_read(smib + itemlist[i].entry);
+		snprintf(name, sizeof(name), "%s%s",
+				prefix, itemlist[i].name);
+		seq_printf(seq, "%-32s\t%lu\n", name, val);
+	}
+}
+
+static void snmp_seq_show_icmpmsg(struct seq_file *seq, atomic_long_t *smib)
+{
+	char name[32];
+	int i;
+	unsigned long val;
+	for (i = 0; i < ICMPMSG_MIB_MAX; i++) {		
+		val = atomic_long_read(smib + i);
+		if (val) {
+			snprintf(name, sizeof(name), "Icmp%sType%u",
+				i & 0x100 ? "Out" : "In", i & 0xff);
+			seq_printf(seq, "%-32s\t%lu\n", name, val);
+		}
+	}
+}
+
+static int snmp_dev_seq_show(struct seq_file *seq, void *v)
+{
+	struct in_device *idev = (struct in_device *)seq->private;
+
+	seq_printf(seq, "%-32s\t%u\n", "ifIndex", idev->dev->ifindex);
+	seq_printf(seq, "%-32s\t%u\n", "Forwarding",
+		IN_DEV_FORWARD(idev));
+	seq_printf(seq, "%-32s\t%u\n", "McForwarding",
+		IN_DEV_MFORWARD(idev));
+	seq_printf(seq, "%-32s\t%u\n", "DefaultTTL",
+		   sysctl_ip_default_ttl);
+
+	BUILD_BUG_ON(offsetof(struct ipstats_mib, mibs) != 0);
+
+	snmp_seq_show_item(seq, (void __percpu **)idev->stats.ip, NULL,
+				snmp4_ipstats_list, "Ip");
+	snmp_seq_show_item(seq, (void __percpu **)idev->stats.ip, NULL,
+				snmp4_ipextstats_list, "Ip");
+	snmp_seq_show_item(seq, NULL, idev->stats.icmpdev->mibs,
+				snmp4_icmp_list, "Icmp");
+	snmp_seq_show_icmpmsg(seq, idev->stats.icmpmsgdev->mibs);
+	return 0;
+}
+
+static int snmp_dev_seq_open(struct inode *inode, struct file *file)
+{
+	return single_open(file, snmp_dev_seq_show, PDE(inode)->data);
+}
+
+static const struct file_operations snmp_dev_seq_fops = {
+	.owner   = THIS_MODULE,
+	.open    = snmp_dev_seq_open,
+	.read    = seq_read,
+	.llseek  = seq_lseek,
+	.release = single_release,
+};
+
+int snmp_register_dev(struct in_device *idev)
+{
+	struct proc_dir_entry *p;
+	struct net *net;
+
+	if (!idev || !idev->dev)
+		return -EINVAL;
+
+	net = dev_net(idev->dev);
+	if (!net->mib.proc_net_devsnmp)
+		return -ENOENT;
+
+	p = proc_create_data(idev->dev->name, S_IRUGO,
+			net->mib.proc_net_devsnmp,
+			&snmp_dev_seq_fops, idev);
+	if (!p) 
+		return -ENOMEM;
+
+	idev->stats.proc_dir_entry = p;
+	return 0;
+}
+
+int snmp_unregister_dev(struct in_device *idev)
+{
+	struct net *net = dev_net(idev->dev);
+	if (!net->mib.proc_net_devsnmp)
+		return -ENOENT;
+	if (!idev->stats.proc_dir_entry)
+		return -EINVAL;
+	remove_proc_entry(idev->stats.proc_dir_entry->name,
+		net->mib.proc_net_devsnmp);
+	idev->stats.proc_dir_entry = NULL;
+	return 0;
+}
+
 static __net_init int ip_proc_init_net(struct net *net)
 {
 	if (!proc_net_fops_create(net, "sockstat", S_IRUGO, &sockstat_seq_fops))
@@ -467,9 +582,14 @@ static __net_init int ip_proc_init_net(struct net *net)
 		goto out_netstat;
 	if (!proc_net_fops_create(net, "snmp", S_IRUGO, &snmp_seq_fops))
 		goto out_snmp;
+	net->mib.proc_net_devsnmp = proc_mkdir("dev_snmp", net->proc_net);
+	if (!net->mib.proc_net_devsnmp)
+		goto out_dev_snmp;
 
 	return 0;
 
+out_dev_snmp:
+	proc_net_remove(net, "snmp");
 out_snmp:
 	proc_net_remove(net, "netstat");
 out_netstat:
@@ -483,6 +603,7 @@ static __net_exit void ip_proc_exit_net(struct net *net)
 	proc_net_remove(net, "snmp");
 	proc_net_remove(net, "netstat");
 	proc_net_remove(net, "sockstat");
+	proc_net_remove(net, "dev_snmp");
 }
 
 static __net_initdata struct pernet_operations ip_proc_ops = {
-- 
1.7.5.4

  parent reply	other threads:[~2011-12-16 15:26 UTC|newest]

Thread overview: 26+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-12-16 15:25 [PATCH 00/10 net-next] Introduce per interface ipv4 statistics igorm
2011-12-16 15:25 ` [PATCH 01/10 net-next] include: net: netns: mib: Add proc_dir_entry for ipv4 per interface stats igorm
2011-12-16 15:25 ` [PATCH 02/10 net-next] include: net: snmp: Create icmp per device counters and add macros for per device stats igorm
2011-12-16 15:25 ` [PATCH 03/10 net-next] include:linux:inetdevice: Add struct ipv4_devstat and func __in_dev_get_rcu_safely igorm
2011-12-16 15:25 ` [PATCH 04/10 net-next] include:net:ipv6: Moved _DEV* macros igorm
2011-12-16 15:25 ` [PATCH 05/10 net-next] include:net:ip: Tuned up IP_*_STATS macros for per device statistics and added functions for (un)registering per device proc entries igorm
2011-12-16 15:25 ` [PATCH 06/10 net-next] include:net:icmp: Tuned up ICMP_*_STATS macros for per device statistics and changed prototype for icmp_out_count igorm
2011-12-16 15:26 ` [PATCH 07/10 net-next] net:ipv4:devinet: Add support for alloc/free of per device stats and (un)register of per device proc files igorm
2011-12-16 15:26 ` [PATCH 08/10 net-next] net:ipv4:af_inet: Init proc fs before ip_init igorm
2011-12-16 15:26 ` igorm [this message]
2011-12-16 15:26 ` [PATCH 10/10 net-next] net: Enable ipv4 per interface statistics igorm
2011-12-16 15:41 ` [PATCH 00/10 net-next] Introduce per interface ipv4 statistics Eric Dumazet
2011-12-16 15:58   ` Igor Maravić
2011-12-16 16:33     ` Eric Dumazet
2011-12-16 16:44       ` Christoph Lameter
2011-12-16 16:50         ` Eric Dumazet
2011-12-16 16:55           ` Christoph Lameter
2011-12-16 17:14             ` Eric Dumazet
2011-12-16 17:29               ` Christoph Lameter
2011-12-16 18:08                 ` Eric Dumazet
2011-12-16 18:30                   ` Christoph Lameter
2011-12-16 18:39                 ` David Miller
2011-12-16 17:19             ` Stephen Hemminger
2011-12-16 18:31       ` David Miller
2011-12-16 18:28     ` David Miller
2011-12-16 18:27   ` David Miller

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1324049163-11207-10-git-send-email-igorm@etf.rs \
    --to=igorm@etf.rs \
    --cc=davem@davemloft.net \
    --cc=netdev@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).