mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: "Cammarata, James" <James.Cammarata@savvis.net>
To: <linux-kernel@vger.kernel.org>
Subject: [PATCH] net: add ability to clear per-interface network statistics via procfs
Date: Tue, 13 May 2008 15:25:27 -0500	[thread overview]
Message-ID: <08C08CA018ADA046A6350BB9CC749CB5027201B7@sl6exchbe2.savvis.ad.savvis.net> (raw)

From: James Cammarata <jimi@sngx.net>

Added the ability to write to /proc/net/dev in order to clear the
interface counters for a given interface.  The ability to zero out
counters (especially the error counters) is extremely useful when
troubleshooting interface issues.

Syntax:
  echo 'net clear-stats ifdev' > /proc/net/dev
Where "ifdev" is the device name you wish to clear.

This code is based mainly on the code found in drivers/scsi/scsi_proc.c

Signed-off-by: James Cammarata <jimi@sngx.net>
---

--- net/core/dev.original       2008-05-13 12:48:46.000000000 -0400
+++ net/core/dev.c      2008-05-13 13:17:15.000000000 -0400
@@ -2453,6 +2453,87 @@ static struct netif_rx_stats *softnet_ge
        return rc;
 }

+/**
+ * proc_net_dev_write - handle writes to /proc/net/dev
+ * @file: not used
+ * @buf: buffer to write
+ * @length: length of buf, at most PAGE_SIZE
+ * @ppos: not used
+ *
+ * Description: this provides a mechanism to clear statistics on a
+ * per-interface basis
+ * "echo 'net clear-stats ifdev' >/proc/net/dev"
+ * with  "ifdev" replaced by the device name you wish to clear.
+ *
+ */
+static ssize_t proc_net_dev_write(struct file *file, const char __user
*buf,
+                                  size_t length, loff_t *ppos)
+{
+        char *buffer, *p;
+        char devname[IFNAMSIZ];
+        struct net *net;
+        struct net_device *dev;
+        int err;
+
+        if (!buf || length > PAGE_SIZE)
+                return -EINVAL;
+
+        buffer = (char *)__get_free_page(GFP_KERNEL);
+        if (!buffer)
+                return -ENOMEM;
+
+        err = -EFAULT;
+        if (copy_from_user(buffer, buf, length))
+                goto out;
+
+        err = -EINVAL;
+        if (length < PAGE_SIZE)
+                buffer[length] = '\0';
+        else if (buffer[PAGE_SIZE-1])
+                goto out;
+
+        err = -ENXIO;
+        net = get_proc_net(file->f_dentry->d_inode);
+        if (!net)
+                goto out;
+
+        /*
+         * Usage: echo "net clear-stats ifdev" >/proc/net/dev
+         * with  "ifdev" replaced by the device name you wish to clear.
+         */
+        if (!strncmp("net clear-stats",buffer,15)) {
+                p = buffer + 16;
+                sscanf(p,"%s",devname);
+                printk(KERN_INFO "net clearing stats - device is:
%s\n", devname);
+                dev = dev_get_by_name(net,devname);
+                if (!dev) {
+                   printk(KERN_INFO "no interface named '%s'\n",
devname);
+                } else {
+                        if (dev->get_stats) {
+                                struct net_device_stats *stats =
dev->get_stats(dev);
+                                memset(stats,0,sizeof(struct
net_device_stats));
+                                printk(KERN_INFO "stats cleared for
interface %s\n", devname);
+                        } else {
+                                printk(KERN_INFO "%s has no stats to
clear\n", devname);
+                        }
+                        dev_put(dev);
+                }
+        }
+
+        /*
+         * convert success returns so that we return the
+         * number of bytes consumed.
+         */
+        if (!err)
+                err = length;
+
+ out:
+        free_page((unsigned long)buffer);
+        return err;
+}
+
 static void *softnet_seq_start(struct seq_file *seq, loff_t *pos)
 {
        return softnet_get_online(pos);
@@ -2496,6 +2577,7 @@ static const struct file_operations dev_
        .owner   = THIS_MODULE,
        .open    = dev_seq_open,
        .read    = seq_read,
+        .write   = proc_net_dev_write,
        .llseek  = seq_lseek,
        .release = seq_release_net,
 };

______________________________________________________________________
This message contains information which may be confidential and/or privileged. Unless you are the intended recipient (or authorized to receive for the intended recipient), you may not read, use, copy or disclose to anyone the message or any information contained in the message. If you have received the message in error, please advise the sender by reply e-mail and delete the message and any attachment(s) thereto without retaining any copies.
______________________________________________________________________

                 reply	other threads:[~2008-05-13 20:28 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

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=08C08CA018ADA046A6350BB9CC749CB5027201B7@sl6exchbe2.savvis.ad.savvis.net \
    --to=james.cammarata@savvis.net \
    --cc=linux-kernel@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

all inboxes | Powered by JetHome®