* [PATCH] net: export device speed and duplex via sysfs
@ 2009-10-02 18:07 Andy Gospodarek
2009-10-02 18:15 ` Eric Dumazet
2009-10-02 18:19 ` [PATCH] " Ben Hutchings
0 siblings, 2 replies; 6+ messages in thread
From: Andy Gospodarek @ 2009-10-02 18:07 UTC (permalink / raw)
To: netdev
This exports the link-speed (in Mbps) and duplex of an interface via
sysfs. This eliminates the need to use ethtool just to check the
link-speed. Not requiring 'ethtool' and not relying on the SIOCETHTOOL
ioctl should be helpful in an embedded environment where space is at a
premium as well.
NOTE: This patch also intentionally allows non-root users to check the link
speed and duplex -- something not possible with ethtool.
Here's some sample output:
# cat /sys/class/net/eth0/speed
100
# cat /sys/class/net/eth0/duplex
half
# ethtool eth0
Settings for eth0:
Supported ports: [ TP ]
Supported link modes: 10baseT/Half 10baseT/Full
100baseT/Half 100baseT/Full
1000baseT/Half 1000baseT/Full
Supports auto-negotiation: Yes
Advertised link modes: Not reported
Advertised auto-negotiation: No
Speed: 100Mb/s
Duplex: Half
Port: Twisted Pair
PHYAD: 1
Transceiver: internal
Auto-negotiation: off
Supports Wake-on: g
Wake-on: g
Current message level: 0x000000ff (255)
Link detected: yes
Signed-off-by: Andy Gospodarek <andy@greyhouse•net>
---
net/core/net-sysfs.c | 42 ++++++++++++++++++++++++++++++++++++++++++
1 files changed, 42 insertions(+), 0 deletions(-)
diff --git a/net/core/net-sysfs.c b/net/core/net-sysfs.c
index ad91e9e..d5964b2 100644
--- a/net/core/net-sysfs.c
+++ b/net/core/net-sysfs.c
@@ -130,6 +130,46 @@ static ssize_t show_carrier(struct device *dev,
return -EINVAL;
}
+static ssize_t show_speed(struct device *dev,
+ struct device_attribute *attr, char *buf)
+{
+ struct net_device *netdev = to_net_dev(dev);
+ int ret = -EINVAL;
+
+ if (!rtnl_trylock())
+ return restart_syscall();
+
+ if (netif_running(netdev) && netdev->ethtool_ops->get_settings) {
+ struct ethtool_cmd cmd = { ETHTOOL_GSET };
+
+ if (netdev->ethtool_ops->get_settings(netdev, &cmd) < 0)
+ return -EINVAL;
+ ret = sprintf(buf, fmt_dec, cmd.speed);
+ }
+ rtnl_unlock();
+ return ret;
+}
+
+static ssize_t show_duplex(struct device *dev,
+ struct device_attribute *attr, char *buf)
+{
+ struct net_device *netdev = to_net_dev(dev);
+ int ret = -EINVAL;
+
+ if (!rtnl_trylock())
+ return restart_syscall();
+
+ if (netif_running(netdev) && netdev->ethtool_ops->get_settings) {
+ struct ethtool_cmd cmd = { ETHTOOL_GSET };
+
+ if (netdev->ethtool_ops->get_settings(netdev, &cmd) < 0)
+ return -EINVAL;
+ ret = sprintf(buf, "%s\n", cmd.duplex ? "full" : "half");
+ }
+ rtnl_unlock();
+ return ret;
+}
+
static ssize_t show_dormant(struct device *dev,
struct device_attribute *attr, char *buf)
{
@@ -259,6 +299,8 @@ static struct device_attribute net_class_attributes[] = {
__ATTR(address, S_IRUGO, show_address, NULL),
__ATTR(broadcast, S_IRUGO, show_broadcast, NULL),
__ATTR(carrier, S_IRUGO, show_carrier, NULL),
+ __ATTR(speed, S_IRUGO, show_speed, NULL),
+ __ATTR(duplex, S_IRUGO, show_duplex, NULL),
__ATTR(dormant, S_IRUGO, show_dormant, NULL),
__ATTR(operstate, S_IRUGO, show_operstate, NULL),
__ATTR(mtu, S_IRUGO | S_IWUSR, show_mtu, store_mtu),
--
1.6.2.5
^ permalink raw reply related [flat|nested] 6+ messages in thread* Re: [PATCH] net: export device speed and duplex via sysfs 2009-10-02 18:07 [PATCH] net: export device speed and duplex via sysfs Andy Gospodarek @ 2009-10-02 18:15 ` Eric Dumazet 2009-10-02 19:26 ` [PATCH v2] " Andy Gospodarek 2009-10-02 18:19 ` [PATCH] " Ben Hutchings 1 sibling, 1 reply; 6+ messages in thread From: Eric Dumazet @ 2009-10-02 18:15 UTC (permalink / raw) To: Andy Gospodarek; +Cc: netdev Andy Gospodarek a écrit : > +static ssize_t show_speed(struct device *dev, > + struct device_attribute *attr, char *buf) > +{ > + struct net_device *netdev = to_net_dev(dev); > + int ret = -EINVAL; > + > + if (!rtnl_trylock()) > + return restart_syscall(); > + > + if (netif_running(netdev) && netdev->ethtool_ops->get_settings) { > + struct ethtool_cmd cmd = { ETHTOOL_GSET }; > + > + if (netdev->ethtool_ops->get_settings(netdev, &cmd) < 0) rtnl lock leak ? > + return -EINVAL; > + ret = sprintf(buf, fmt_dec, cmd.speed); > + } > + rtnl_unlock(); > + return ret; > +} > + > +static ssize_t show_duplex(struct device *dev, > + struct device_attribute *attr, char *buf) > +{ > + struct net_device *netdev = to_net_dev(dev); > + int ret = -EINVAL; > + > + if (!rtnl_trylock()) > + return restart_syscall(); > + > + if (netif_running(netdev) && netdev->ethtool_ops->get_settings) { > + struct ethtool_cmd cmd = { ETHTOOL_GSET }; > + > + if (netdev->ethtool_ops->get_settings(netdev, &cmd) < 0) rtnl lock leak ? > + return -EINVAL; > + ret = sprintf(buf, "%s\n", cmd.duplex ? "full" : "half"); > + } > + rtnl_unlock(); > + return ret; > +} > + ^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [PATCH v2] net: export device speed and duplex via sysfs 2009-10-02 18:15 ` Eric Dumazet @ 2009-10-02 19:26 ` Andy Gospodarek 0 siblings, 0 replies; 6+ messages in thread From: Andy Gospodarek @ 2009-10-02 19:26 UTC (permalink / raw) To: Eric Dumazet; +Cc: netdev On Fri, Oct 02, 2009 at 08:15:08PM +0200, Eric Dumazet wrote: > Andy Gospodarek a écrit : > > +static ssize_t show_speed(struct device *dev, > > + struct device_attribute *attr, char *buf) > > +{ > > + struct net_device *netdev = to_net_dev(dev); > > + int ret = -EINVAL; > > + > > + if (!rtnl_trylock()) > > + return restart_syscall(); > > + > > + if (netif_running(netdev) && netdev->ethtool_ops->get_settings) { > > + struct ethtool_cmd cmd = { ETHTOOL_GSET }; > > + > > + if (netdev->ethtool_ops->get_settings(netdev, &cmd) < 0) > > rtnl lock leak ? > > > > + return -EINVAL; > > + ret = sprintf(buf, fmt_dec, cmd.speed); > > + } > > + rtnl_unlock(); > > + return ret; > > +} > > + > > +static ssize_t show_duplex(struct device *dev, > > + struct device_attribute *attr, char *buf) > > +{ > > + struct net_device *netdev = to_net_dev(dev); > > + int ret = -EINVAL; > > + > > + if (!rtnl_trylock()) > > + return restart_syscall(); > > + > > + if (netif_running(netdev) && netdev->ethtool_ops->get_settings) { > > + struct ethtool_cmd cmd = { ETHTOOL_GSET }; > > + > > + if (netdev->ethtool_ops->get_settings(netdev, &cmd) < 0) > > rtnl lock leak ? > > > + return -EINVAL; > > + ret = sprintf(buf, "%s\n", cmd.duplex ? "full" : "half"); > > + } > > + rtnl_unlock(); > > + return ret; > > +} > > + > Thanks for spotting that, Eric. Here's an updated (and tested patch). I also switched to using ethtool_cmd_speed to get link speed to get the 'entire' speed. [PATCH] net: export device speed and duplex via sysfs This patch exports the link-speed (in Mbps) and duplex of an interface via sysfs. This eliminates the need to use ethtool just to check the link-speed. Not requiring 'ethtool' and not relying on the SIOCETHTOOL ioctl should be helpful in an embedded environment where space is at a premium as well. NOTE: This patch also intentionally allows non-root users to check the link speed and duplex -- something not possible with ethtool. Here's some sample output: # cat /sys/class/net/eth0/speed 100 # cat /sys/class/net/eth0/duplex half # ethtool eth0 Settings for eth0: Supported ports: [ TP ] Supported link modes: 10baseT/Half 10baseT/Full 100baseT/Half 100baseT/Full 1000baseT/Half 1000baseT/Full Supports auto-negotiation: Yes Advertised link modes: Not reported Advertised auto-negotiation: No Speed: 100Mb/s Duplex: Half Port: Twisted Pair PHYAD: 1 Transceiver: internal Auto-negotiation: off Supports Wake-on: g Wake-on: g Current message level: 0x000000ff (255) Link detected: yes --- net/core/net-sysfs.c | 40 ++++++++++++++++++++++++++++++++++++++++ 1 files changed, 40 insertions(+), 0 deletions(-) diff --git a/net/core/net-sysfs.c b/net/core/net-sysfs.c index 3994680..133dbc4 100644 --- a/net/core/net-sysfs.c +++ b/net/core/net-sysfs.c @@ -130,6 +130,44 @@ static ssize_t show_carrier(struct device *dev, return -EINVAL; } +static ssize_t show_speed(struct device *dev, + struct device_attribute *attr, char *buf) +{ + struct net_device *netdev = to_net_dev(dev); + int ret = -EINVAL; + + if (!rtnl_trylock()) + return restart_syscall(); + + if (netif_running(netdev) && netdev->ethtool_ops->get_settings) { + struct ethtool_cmd cmd = { ETHTOOL_GSET }; + + if (!netdev->ethtool_ops->get_settings(netdev, &cmd)) + ret = sprintf(buf, fmt_dec, ethtool_cmd_speed(&cmd)); + } + rtnl_unlock(); + return ret; +} + +static ssize_t show_duplex(struct device *dev, + struct device_attribute *attr, char *buf) +{ + struct net_device *netdev = to_net_dev(dev); + int ret = -EINVAL; + + if (!rtnl_trylock()) + return restart_syscall(); + + if (netif_running(netdev) && netdev->ethtool_ops->get_settings) { + struct ethtool_cmd cmd = { ETHTOOL_GSET }; + + if (!netdev->ethtool_ops->get_settings(netdev, &cmd)) + ret = sprintf(buf, "%s\n", cmd.duplex ? "full" : "half"); + } + rtnl_unlock(); + return ret; +} + static ssize_t show_dormant(struct device *dev, struct device_attribute *attr, char *buf) { @@ -259,6 +297,8 @@ static struct device_attribute net_class_attributes[] = { __ATTR(address, S_IRUGO, show_address, NULL), __ATTR(broadcast, S_IRUGO, show_broadcast, NULL), __ATTR(carrier, S_IRUGO, show_carrier, NULL), + __ATTR(speed, S_IRUGO, show_speed, NULL), + __ATTR(duplex, S_IRUGO, show_duplex, NULL), __ATTR(dormant, S_IRUGO, show_dormant, NULL), __ATTR(operstate, S_IRUGO, show_operstate, NULL), __ATTR(mtu, S_IRUGO | S_IWUSR, show_mtu, store_mtu), ^ permalink raw reply related [flat|nested] 6+ messages in thread
* Re: [PATCH] net: export device speed and duplex via sysfs 2009-10-02 18:07 [PATCH] net: export device speed and duplex via sysfs Andy Gospodarek 2009-10-02 18:15 ` Eric Dumazet @ 2009-10-02 18:19 ` Ben Hutchings 2009-10-02 20:01 ` Andy Gospodarek 1 sibling, 1 reply; 6+ messages in thread From: Ben Hutchings @ 2009-10-02 18:19 UTC (permalink / raw) To: Andy Gospodarek; +Cc: netdev On Fri, 2009-10-02 at 14:07 -0400, Andy Gospodarek wrote: > This exports the link-speed (in Mbps) and duplex of an interface via > sysfs. This eliminates the need to use ethtool just to check the > link-speed. Not requiring 'ethtool' and not relying on the SIOCETHTOOL > ioctl should be helpful in an embedded environment where space is at a > premium as well. It's trivial to write an ethtool-lite that does this. That might be worth adding to busybox. > NOTE: This patch also intentionally allows non-root users to check the link > speed and duplex -- something not possible with ethtool. [...] Assuming this is desirable (I'm not sure), wouldn't it would make more sense to move the permissions check for SIOCETHTOOL so that get_settings is non-privileged? Ben. -- Ben Hutchings, Senior Software Engineer, Solarflare Communications Not speaking for my employer; that's the marketing department's job. They asked us to note that Solarflare product names are trademarked. ^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [PATCH] net: export device speed and duplex via sysfs 2009-10-02 18:19 ` [PATCH] " Ben Hutchings @ 2009-10-02 20:01 ` Andy Gospodarek 2009-10-05 7:44 ` David Miller 0 siblings, 1 reply; 6+ messages in thread From: Andy Gospodarek @ 2009-10-02 20:01 UTC (permalink / raw) To: Ben Hutchings; +Cc: netdev On Fri, Oct 02, 2009 at 07:19:14PM +0100, Ben Hutchings wrote: > On Fri, 2009-10-02 at 14:07 -0400, Andy Gospodarek wrote: > > This exports the link-speed (in Mbps) and duplex of an interface via > > sysfs. This eliminates the need to use ethtool just to check the > > link-speed. Not requiring 'ethtool' and not relying on the SIOCETHTOOL > > ioctl should be helpful in an embedded environment where space is at a > > premium as well. > > It's trivial to write an ethtool-lite that does this. That might be > worth adding to busybox. > It probably would be. I was just using this as an example of another use for it. Embedded usage was not the primary purpose. > > NOTE: This patch also intentionally allows non-root users to check the link > > speed and duplex -- something not possible with ethtool. > [...] > > Assuming this is desirable (I'm not sure), wouldn't it would make more > sense to move the permissions check for SIOCETHTOOL so that get_settings > is non-privileged? > That could be done as well I just chose to go a slightly different direction. I took a look at /sys/class/net/ethX/ and felt like the information was pretty complete with the exception of the link speed and duplex, so I thought it would be a good place to add it. I personally wouldn't mind having most of the information presented in ethtool available via sysfs, but I figured I would walk before running. ^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [PATCH] net: export device speed and duplex via sysfs 2009-10-02 20:01 ` Andy Gospodarek @ 2009-10-05 7:44 ` David Miller 0 siblings, 0 replies; 6+ messages in thread From: David Miller @ 2009-10-05 7:44 UTC (permalink / raw) To: andy; +Cc: bhutchings, netdev From: Andy Gospodarek <andy@greyhouse•net> Date: Fri, 2 Oct 2009 16:01:41 -0400 > I took a look at /sys/class/net/ethX/ and felt like the information was > pretty complete with the exception of the link speed and duplex, so I > thought it would be a good place to add it. I personally wouldn't mind > having most of the information presented in ethtool available via sysfs, > but I figured I would walk before running. I have no objections to this, I applied v2 of your patch to net-next-2.6, thanks! ^ permalink raw reply [flat|nested] 6+ messages in thread
end of thread, other threads:[~2009-10-05 7:44 UTC | newest] Thread overview: 6+ messages (download: mbox.gz follow: Atom feed -- links below jump to the message on this page -- 2009-10-02 18:07 [PATCH] net: export device speed and duplex via sysfs Andy Gospodarek 2009-10-02 18:15 ` Eric Dumazet 2009-10-02 19:26 ` [PATCH v2] " Andy Gospodarek 2009-10-02 18:19 ` [PATCH] " Ben Hutchings 2009-10-02 20:01 ` Andy Gospodarek 2009-10-05 7:44 ` David Miller
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox