netdev.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: "Huascar Tejeda" <htejeda@gmail.com>
To: netdev@vger.kernel.org
Subject: [PATCH] net: reset network device counters on the fly
Date: Tue, 6 Jan 2009 22:08:39 -0400	[thread overview]
Message-ID: <3375b4020901061808g527cb872ia64b47147783b91c@mail.gmail.com> (raw)

[-- Attachment #1: Type: text/plain, Size: 444 bytes --]

Hello everyone,

This is my first post to the mailing list and also my first kernel patch.

There was an scenario where I needed to clear network device counters
without shutting down the interface.
With this patch I add this functionality to /proc/net/dev.

Usage example:
echo clear eth0 > /proc/net/dev
echo clear all > /proc/net/dev

Thanks for your comments and please feel free to reply with any
suggestions you may have.

Huascar Tejeda

[-- Attachment #2: reset_netdev_counters.patch --]
[-- Type: application/octet-stream, Size: 2488 bytes --]

--- net/core/dev.c.orig	2009-01-05 17:38:51.000000000 -0400
+++ net/core/dev.c	2009-01-06 16:36:35.000000000 -0400
@@ -161,6 +161,11 @@
 #define PTYPE_HASH_SIZE	(16)
 #define PTYPE_HASH_MASK	(PTYPE_HASH_SIZE - 1)
 
+/*
+ * 	Maximum length of the command sent by the user
+ */
+#define RSTDEV_COMMAND_MAX_SIZE (15)
+
 static DEFINE_SPINLOCK(ptype_lock);
 static struct list_head ptype_base[PTYPE_HASH_SIZE] __read_mostly;
 static struct list_head ptype_all __read_mostly;	/* Taps */
@@ -2671,10 +2676,78 @@
 			    sizeof(struct seq_net_private));
 }
 
+
+/*	
+ *	Called when /proc/net/dev is written
+ */
+static ssize_t reset_device_counters(struct file *file, const char *buffer, size_t len, loff_t * off)
+{
+	struct net_device *dev;
+	struct net_device_stats *stats;
+	char cmd[RSTDEV_COMMAND_MAX_SIZE];
+	unsigned long cmd_size = 0;
+	char *cmd_ptr = cmd;
+	char **argv = (char **) kmalloc(sizeof(char *), GFP_KERNEL);
+	char *tmp_token, *action, *ifname;
+	int argc = 0;
+
+	cmd_size = ( len > RSTDEV_COMMAND_MAX_SIZE ) ? RSTDEV_COMMAND_MAX_SIZE : len;
+	if ( copy_from_user(cmd, buffer, cmd_size) ) {
+		return -EFAULT;
+	}
+
+	//Parse user input
+	while ( (tmp_token = strsep(&cmd_ptr, " \n")) ) {
+		if ( *tmp_token ) {
+			argv[argc] = (char *) kmalloc((strlen(tmp_token) + 1) * sizeof(char), GFP_KERNEL);
+			strcpy(argv[argc++], tmp_token);
+		}
+	}
+
+	if ( !argv[0] || !argv[1] ) {
+		printk(KERN_INFO "Please usage: reset \"[network interface]\" or \"reset all\"\n");
+		return -EINVAL;
+	}
+
+	//TODO: create a struct for this.
+	action = argv[0];
+	ifname = argv[1];
+	
+	kfree(argv);
+
+	if ( strstr(action, "reset") ) {
+		if ( !strcmp(ifname, "all") ) {
+			for_each_netdev(&init_net, dev) {
+				stats = dev->get_stats(dev);
+				if ( stats ) {
+					memset(stats, 0, sizeof(struct net_device_stats));
+				}
+			}
+		} else {
+			dev = dev_get_by_name(&init_net, ifname);
+			if ( dev ) {
+				stats = dev->get_stats(dev);
+				if ( stats ) {
+					memset(stats, 0, sizeof(struct net_device_stats));
+				}
+			} else {
+				printk(KERN_INFO "Device %s not found!\n", ifname);
+				return -ENODEV;
+			}
+		}
+	} else {
+		printk(KERN_INFO "\"reset\" is the only supported command!\n");
+		return -EINVAL;
+	}
+
+	return cmd_size;
+}
+
 static const struct file_operations dev_seq_fops = {
 	.owner	 = THIS_MODULE,
 	.open    = dev_seq_open,
 	.read    = seq_read,
+        .write   = reset_device_counters,
 	.llseek  = seq_lseek,
 	.release = seq_release_net,
 };

             reply	other threads:[~2009-01-07  2:08 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2009-01-07  2:08 Huascar Tejeda [this message]
2009-01-07  2:29 ` [PATCH] net: reset network device counters on the fly Rick Jones
2009-01-07  3:17   ` Huascar Tejeda
2009-01-07  3:54     ` Stephen Hemminger
2009-01-07  7:23 ` 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=3375b4020901061808g527cb872ia64b47147783b91c@mail.gmail.com \
    --to=htejeda@gmail.com \
    --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).