netdev.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Zhu Yanjun <Yanjun.Zhu@windriver.com>
To: <netdev@vger.kernel.org>, <mst@redhat.com>, <jasowang@redhat.com>,
	<viro@zeniv.linux.org.uk>, <davem@davemloft.net>,
	<sergei.shtylyov@cogentembedded.com>,
	<jonathon.reinhart@gmail.com>
Subject: [PATCH 1/1] tun: change speed from 10M to dynamically configured
Date: Fri, 13 Feb 2015 14:19:13 +0800	[thread overview]
Message-ID: <1423808353-8722-2-git-send-email-Yanjun.Zhu@windriver.com> (raw)
In-Reply-To: <1423808353-8722-1-git-send-email-Yanjun.Zhu@windriver.com>

The default speed of normal nic is 1000M while the default speed
of tun is 10M. Now the default speed of tun is changed to 1000M.
And there are 3 options: 10M, 100M and 1000M to the speed of tun.
The command "ethtool -s tun0 speed 10/100/1000" can configure the
speed of tun dynamically.

CC: Michael S. Tsirkin <mst@redhat.com>
CC: Jason Wang <jasowang@redhat.com>
CC: Al Viro <viro@zeniv.linux.org.uk>
Reviewed-by: Sergei Shtylyov <sergei.shtylyov@cogentembedded.com>
Reviewed-by: Jonathon Reinhart <jonathon.reinhart@gmail.com>
Signed-off-by: Zhu Yanjun <Yanjun.Zhu@windriver.com>
---
 drivers/net/tun.c           | 43 ++++++++++++++++++++++++++++++++++++++++++-
 include/uapi/linux/if_tun.h |  5 +++++
 2 files changed, 47 insertions(+), 1 deletion(-)

diff --git a/drivers/net/tun.c b/drivers/net/tun.c
index 8c8dc16..423b276 100644
--- a/drivers/net/tun.c
+++ b/drivers/net/tun.c
@@ -950,6 +950,9 @@ static void tun_net_init(struct net_device *dev)
 		dev->addr_len = 0;
 		dev->mtu = 1500;
 
+		/* Set default speed 1000M */
+		tun->flags |= TUN_CTRL_SPD_1000;
+
 		/* Zero header length */
 		dev->type = ARPHRD_NONE;
 		dev->flags = IFF_POINTOPOINT | IFF_NOARP | IFF_MULTICAST;
@@ -2257,9 +2260,18 @@ static struct miscdevice tun_miscdev = {
 
 static int tun_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
 {
+	struct tun_struct *tun = netdev_priv(dev);
+
+	/* Get the speed of tun */
+	if (tun->flags & TUN_CTRL_SPD_1000) {
+		ethtool_cmd_speed_set(cmd, SPEED_1000);
+	} else if (tun->flags & TUN_CTRL_SPD_100) {
+		ethtool_cmd_speed_set(cmd, SPEED_100);
+	} else
+		ethtool_cmd_speed_set(cmd, SPEED_10);
+
 	cmd->supported		= 0;
 	cmd->advertising	= 0;
-	ethtool_cmd_speed_set(cmd, SPEED_10);
 	cmd->duplex		= DUPLEX_FULL;
 	cmd->port		= PORT_TP;
 	cmd->phy_address	= 0;
@@ -2287,6 +2299,34 @@ static void tun_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info
 	}
 }
 
+static int tun_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
+{
+	struct tun_struct *tun = netdev_priv(dev);
+	u32 speed = ethtool_cmd_speed(cmd);
+	int ret = 0;
+
+	/* Set speed flag */
+	switch (speed) {
+	case 10:
+		tun->flags &= ~(TUN_CTRL_SPD_100 | TUN_CTRL_SPD_1000);
+		tun->flags |= TUN_CTRL_SPD_10;
+		break;
+	case 100:
+		tun->flags &= ~(TUN_CTRL_SPD_10 | TUN_CTRL_SPD_1000);
+		tun->flags |= TUN_CTRL_SPD_100;
+		break;
+	case 1000:
+		tun->flags &= ~(TUN_CTRL_SPD_10 | TUN_CTRL_SPD_100);
+		tun->flags |= TUN_CTRL_SPD_1000;
+		break;
+	default:
+		ret = -EINVAL;
+		tun_debug(KERN_INFO, tun, "wrong speed!\n");
+	}
+
+	return ret;
+}
+
 static u32 tun_get_msglevel(struct net_device *dev)
 {
 #ifdef TUN_DEBUG
@@ -2307,6 +2347,7 @@ static void tun_set_msglevel(struct net_device *dev, u32 value)
 
 static const struct ethtool_ops tun_ethtool_ops = {
 	.get_settings	= tun_get_settings,
+	.set_settings	= tun_set_settings,
 	.get_drvinfo	= tun_get_drvinfo,
 	.get_msglevel	= tun_get_msglevel,
 	.set_msglevel	= tun_set_msglevel,
diff --git a/include/uapi/linux/if_tun.h b/include/uapi/linux/if_tun.h
index 50ae243..78a09a7 100644
--- a/include/uapi/linux/if_tun.h
+++ b/include/uapi/linux/if_tun.h
@@ -66,6 +66,11 @@
 #define IFF_PERSIST	0x0800
 #define IFF_NOFILTER	0x1000
 
+/*add speed control, default 1000M*/
+#define TUN_CTRL_SPD_10         0x0020
+#define TUN_CTRL_SPD_100        0x0040
+#define TUN_CTRL_SPD_1000       0x0080
+
 /* Socket options */
 #define TUN_TX_TIMESTAMP 1
 
-- 
1.9.1

  reply	other threads:[~2015-02-13  6:20 UTC|newest]

Thread overview: 25+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-02-13  6:19 [PATCH V3 0/1] tun: dynamically set speed of tun Zhu Yanjun
2015-02-13  6:19 ` Zhu Yanjun [this message]
2015-02-13 10:03   ` [PATCH 1/1] tun: change speed from 10M to dynamically configured yzhu1
2015-02-13 10:45   ` Sergei Shtylyov
2015-02-13 21:28   ` Francois Romieu
2015-02-13 21:32     ` Rick Jones
2015-02-13 23:15       ` Francois Romieu
2015-02-16  2:31     ` yzhu1
2015-02-16 17:03       ` Stephen Hemminger
2015-02-18 19:29         ` Andy Gospodarek
2015-02-18 19:39         ` Andy Gospodarek
2015-02-19 20:37           ` David Miller
2015-02-19 20:40         ` David Miller
2015-02-20  0:41           ` Simon Horman
2015-02-20  2:35           ` Andy Gospodarek
2015-02-20 20:07             ` David Miller
2015-02-21  3:07               ` Andy Gospodarek
2015-02-20 10:01           ` David Laight
2015-02-19 20:56         ` roopa
  -- strict thread matches above, loose matches on Subject: below --
2015-02-12  5:35 [PATCH 0/1] tun: dynamically set speed of tun Zhu Yanjun
2015-02-12  5:35 ` [PATCH 1/1] tun: change speed from 10M to dynamically configured Zhu Yanjun
2015-02-12 12:55   ` Stephen Hemminger
2015-02-13  2:45     ` yzhu1
2015-02-13  3:25       ` Fan Du
2015-02-13  3:34         ` yzhu1
2015-02-12 13:52   ` Sergei Shtylyov

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=1423808353-8722-2-git-send-email-Yanjun.Zhu@windriver.com \
    --to=yanjun.zhu@windriver.com \
    --cc=davem@davemloft.net \
    --cc=jasowang@redhat.com \
    --cc=jonathon.reinhart@gmail.com \
    --cc=mst@redhat.com \
    --cc=netdev@vger.kernel.org \
    --cc=sergei.shtylyov@cogentembedded.com \
    --cc=viro@zeniv.linux.org.uk \
    /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).