Skip to contentSkip to conversation

tun: dynamically set speed of tun

PATCH Superseded Zhu Yanjun 4 participants 7 messages Feb 12, 2015 – Feb 13, 2015

Subsystems (3)

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Zhu Yanjun

author
#0 lore raw

cover tun: dynamically set speed of tun

The default speed of tun is 10M while the normal nic speed is 1000M. When the speed of tun is accessed by the userspace application, the default 10M is not proper. In this case, the default tun speed is changed to 1000M. And this speed can be dynamically configured by the command "ethtool -s tunX speed 10/100/1000".

Zhu Yanjun (1):
tun: change speed from 10M to dynamically configured

 drivers/net/tun.c           | 36 +++++++++++++++++++++++++++++++++++-
 include/uapi/linux/if_tun.h |  5 +++++
 2 files changed, 40 insertions(+), 1 deletion(-)
Signature · 1 line
1.9.1
author
#1 lore raw

PATCH 1/1 tun: change speed from 10M to dynamically configured

From: Zhu Yanjun <[email protected]>

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 <[email protected]>
CC Jason Wang <[email protected]>
CC Al Viro <[email protected]>
---
 drivers/net/tun.c           | 36 +++++++++++++++++++++++++++++++++++-
 include/uapi/linux/if_tun.h |  5 +++++
 2 files changed, 40 insertions(+), 1 deletion(-)
drivers/net/tun.c +35 -1
index 8c8dc16..64f4dcc 100644
@@ -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,27 @@ 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);
+
+	if (10 == speed) {
+		tun->flags &= ~TUN_CTRL_SPD_100;
+		tun->flags &= ~TUN_CTRL_SPD_1000;
+		tun->flags |= TUN_CTRL_SPD_10;
+	} else if (100 == speed) {
+		tun->flags &= ~TUN_CTRL_SPD_10;
+		tun->flags &= ~TUN_CTRL_SPD_1000;
+		tun->flags |= TUN_CTRL_SPD_100;
+	} else {
+		tun->flags &= ~TUN_CTRL_SPD_10;
+		tun->flags &= ~TUN_CTRL_SPD_100;
+		tun->flags |= TUN_CTRL_SPD_1000;
+	}
+	return 0;
+}
+
 static u32 tun_get_msglevel(struct net_device *dev)
 {
 #ifdef TUN_DEBUG
@@ -2307,6 +2340,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,
include/uapi/linux/if_tun.h +5 -0
index 50ae243..78a09a7 100644
@@ -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
 
Signature · 1 line
1.9.1
#2 lore raw

Re: [PATCH 1/1] tun: change speed from 10M to dynamically configured

Quoted 8 lines · On Thu, 12 Feb 2015 13:35:23 +0800 Zhu Yanjun <[email protected]> wrote:

From: Zhu Yanjun <[email protected]>

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.

Quoted 5 lines
CC Michael S. Tsirkin <[email protected]>
CC Jason Wang <[email protected]>
CC Al Viro <[email protected]>

Why limit to 10/100/1000 speed?
Ethtool speed can be any value

Quoted 1 line
#6 lore raw

Re: [PATCH 1/1] tun: change speed from 10M to dynamically configured

In reply to Zhu Yanjun

Hello.

Quoted 22 lines · Zhu Yanjun

From: Zhu Yanjun <[email protected]>

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 <[email protected]>
CC Jason Wang <[email protected]>
CC Al Viro <[email protected]>
---
  drivers/net/tun.c           | 36 +++++++++++++++++++++++++++++++++++-
  include/uapi/linux/if_tun.h |  5 +++++
  2 files changed, 40 insertions(+), 1 deletion(-)
drivers/net/tun.c +0 -0
index 8c8dc16..64f4dcc 100644

[...]

drivers/net/tun.c static struct miscdevice tun_miscdev = { · line 2265

Quoted 7 lines
drivers/net/tun.c +3 -0
@@ -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*/

Please add spaces after /* and before */.

[...]

drivers/net/tun.c static void tun_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info · line 2309

Quoted 12 lines
drivers/net/tun.c +8 -0
@@ -2287,6 +2299,27 @@ 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);
+
+	if (10 == speed) {
+		tun->flags &= ~TUN_CTRL_SPD_100;
+		tun->flags &= ~TUN_CTRL_SPD_1000;

Can't you clear both bits at once?

drivers/net/tun.c static void tun_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info · line 2319

Quoted 10 lines
drivers/net/tun.c +10 -0
+		tun->flags |= TUN_CTRL_SPD_10;
+	} else if (100 == speed) {
+		tun->flags &= ~TUN_CTRL_SPD_10;
+		tun->flags &= ~TUN_CTRL_SPD_1000;
+		tun->flags |= TUN_CTRL_SPD_100;
+	} else {
+		tun->flags &= ~TUN_CTRL_SPD_10;
+		tun->flags &= ~TUN_CTRL_SPD_100;
+		tun->flags |= TUN_CTRL_SPD_1000;
+	}

This is asking to be a *switch* statement.

[...]

include/uapi/linux/if_tun.h line 69

Quoted 9 lines
include/uapi/linux/if_tun.h +1 -0
index 50ae243..78a09a7 100644
@@ -66,6 +66,11 @@
  #define IFF_PERSIST	0x0800
  #define IFF_NOFILTER	0x1000
 
+/*add speed control, default 1000M*/

Same remark about the comment style as above.

[...]

WBR, Sergei

author
#3 lore raw

Re: [PATCH 1/1] tun: change speed from 10M to dynamically configured

In reply to Stephen Hemminger
Quoted 11 lines · Stephen Hemminger
Quoted 8 lines · On Thu, 12 Feb 2015 13:35:23 +0800 Zhu Yanjun <[email protected]> wrote:

From: Zhu Yanjun <[email protected]>

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.

Quoted 7 lines
Quoted 5 lines
CC Michael S. Tsirkin <[email protected]>
CC Jason Wang <[email protected]>
CC Al Viro <[email protected]>

Why limit to 10/100/1000 speed?
Ethtool speed can be any value

Thanks for your comments.

Yes. But the real physical nic speed often 10M, 100M and 1000M. This simulates the physical nic.

Zhu Yanjun

Quoted 4 lines
Quoted 2 lines
#4 lore raw

Re: [PATCH 1/1] tun: change speed from 10M to dynamically configured

Quoted 17 lines · 于 2015年02月13日 10:45, yzhu1 写道:
Quoted 16 lines · Stephen Hemminger
Quoted 13 lines · On Thu, 12 Feb 2015 13:35:23 +0800 Zhu Yanjun <[email protected]> wrote:

From: Zhu Yanjun <[email protected]>

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 <[email protected]>
CC Jason Wang <[email protected]>
CC Al Viro <[email protected]>
Quoted 6 lines
Quoted 2 lines

Why limit to 10/100/1000 speed?
Ethtool speed can be any value

Thanks for your comments.

Yes. But the real physical nic speed often 10M, 100M and 1000M. This simulates the physical nic.

^^^^^^^^^^^^^^^
then a speed control logical would be needed to effect the corresponding speed when tun/tap gets/puts packets corresponding to the speed setting.

What's the benefit when changing speed from 10Mb/s to 1000Mb/s?

Just xia bibi ;)

author
#5 lore raw

Re: [PATCH 1/1] tun: change speed from 10M to dynamically configured

Quoted 24 lines · Fan Du
Quoted 17 lines · 于 2015年02月13日 10:45, yzhu1 写道:
Quoted 16 lines · Stephen Hemminger
Quoted 13 lines · On Thu, 12 Feb 2015 13:35:23 +0800 Zhu Yanjun <[email protected]> wrote:

From: Zhu Yanjun <[email protected]>

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 <[email protected]>
CC Jason Wang <[email protected]>
CC Al Viro <[email protected]>
Quoted 6 lines
Quoted 2 lines

Why limit to 10/100/1000 speed?
Ethtool speed can be any value

Thanks for your comments.

Yes. But the real physical nic speed often 10M, 100M and 1000M. This simulates the physical nic.

Quoted 7 lines

^^^^^^^^^^^^^^^
then a speed control logical would be needed to effect the corresponding speed when tun/tap gets/puts packets corresponding to the speed setting.

What's the benefit when changing speed from 10Mb/s to 1000Mb/s?

Just xia bibi ;)

:-P
Thanks for your reply.

This speed is just for user space application. There is no any speed control logic here.:-(

Zhu Yanjun

Quoted 4 lines