Commit b2d3bcfa authored by David Decotigny's avatar David Decotigny Committed by David S. Miller

net: core: Expose number of link up/down transitions

Expose the number of times the link has been going UP or DOWN, and
update the "carrier_changes" counter to be the sum of these two events.
While at it, also update the sysfs-class-net documentation to cover:
carrier_changes (3.15), carrier_up_count (4.16) and carrier_down_count
(4.16)
Signed-off-by: default avatarDavid Decotigny <decot@googlers.com>
[Florian:
* rebase
* add documentation
* merge carrier_changes with up/down counters]
Signed-off-by: default avatarFlorian Fainelli <f.fainelli@gmail.com>
Signed-off-by: default avatarDavid S. Miller <davem@davemloft.net>
parent e8660ded
...@@ -259,3 +259,27 @@ Contact: netdev@vger.kernel.org ...@@ -259,3 +259,27 @@ Contact: netdev@vger.kernel.org
Description: Description:
Symbolic link to the PHY device this network device is attached Symbolic link to the PHY device this network device is attached
to. to.
What: /sys/class/net/<iface>/carrier_changes
Date: Mar 2014
KernelVersion: 3.15
Contact: netdev@vger.kernel.org
Description:
32-bit unsigned integer counting the number of times the link has
seen a change from UP to DOWN and vice versa
What: /sys/class/net/<iface>/carrier_up_count
Date: Jan 2018
KernelVersion: 4.16
Contact: netdev@vger.kernel.org
Description:
32-bit unsigned integer counting the number of times the link has
been up
What: /sys/class/net/<iface>/carrier_down_count
Date: Jan 2018
KernelVersion: 4.16
Contact: netdev@vger.kernel.org
Description:
32-bit unsigned integer counting the number of times the link has
been down
...@@ -1680,8 +1680,6 @@ struct net_device { ...@@ -1680,8 +1680,6 @@ struct net_device {
unsigned long base_addr; unsigned long base_addr;
int irq; int irq;
atomic_t carrier_changes;
/* /*
* Some hardware also needs these fields (state,dev_list, * Some hardware also needs these fields (state,dev_list,
* napi_list,unreg_list,close_list) but they are not * napi_list,unreg_list,close_list) but they are not
...@@ -1719,6 +1717,10 @@ struct net_device { ...@@ -1719,6 +1717,10 @@ struct net_device {
atomic_long_t tx_dropped; atomic_long_t tx_dropped;
atomic_long_t rx_nohandler; atomic_long_t rx_nohandler;
/* Stats to monitor link on/off, flapping */
atomic_t carrier_up_count;
atomic_t carrier_down_count;
#ifdef CONFIG_WIRELESS_EXT #ifdef CONFIG_WIRELESS_EXT
const struct iw_handler_def *wireless_handlers; const struct iw_handler_def *wireless_handlers;
struct iw_public_data *wireless_data; struct iw_public_data *wireless_data;
......
...@@ -161,6 +161,8 @@ enum { ...@@ -161,6 +161,8 @@ enum {
IFLA_EVENT, IFLA_EVENT,
IFLA_NEW_NETNSID, IFLA_NEW_NETNSID,
IFLA_IF_NETNSID, IFLA_IF_NETNSID,
IFLA_CARRIER_UP_COUNT,
IFLA_CARRIER_DOWN_COUNT,
__IFLA_MAX __IFLA_MAX
}; };
......
...@@ -295,10 +295,31 @@ static ssize_t carrier_changes_show(struct device *dev, ...@@ -295,10 +295,31 @@ static ssize_t carrier_changes_show(struct device *dev,
struct net_device *netdev = to_net_dev(dev); struct net_device *netdev = to_net_dev(dev);
return sprintf(buf, fmt_dec, return sprintf(buf, fmt_dec,
atomic_read(&netdev->carrier_changes)); atomic_read(&netdev->carrier_up_count) +
atomic_read(&netdev->carrier_down_count));
} }
static DEVICE_ATTR_RO(carrier_changes); static DEVICE_ATTR_RO(carrier_changes);
static ssize_t carrier_up_count_show(struct device *dev,
struct device_attribute *attr,
char *buf)
{
struct net_device *netdev = to_net_dev(dev);
return sprintf(buf, fmt_dec, atomic_read(&netdev->carrier_up_count));
}
static DEVICE_ATTR_RO(carrier_up_count);
static ssize_t carrier_down_count_show(struct device *dev,
struct device_attribute *attr,
char *buf)
{
struct net_device *netdev = to_net_dev(dev);
return sprintf(buf, fmt_dec, atomic_read(&netdev->carrier_down_count));
}
static DEVICE_ATTR_RO(carrier_down_count);
/* read-write attributes */ /* read-write attributes */
static int change_mtu(struct net_device *dev, unsigned long new_mtu) static int change_mtu(struct net_device *dev, unsigned long new_mtu)
...@@ -547,6 +568,8 @@ static struct attribute *net_class_attrs[] __ro_after_init = { ...@@ -547,6 +568,8 @@ static struct attribute *net_class_attrs[] __ro_after_init = {
&dev_attr_phys_port_name.attr, &dev_attr_phys_port_name.attr,
&dev_attr_phys_switch_id.attr, &dev_attr_phys_switch_id.attr,
&dev_attr_proto_down.attr, &dev_attr_proto_down.attr,
&dev_attr_carrier_up_count.attr,
&dev_attr_carrier_down_count.attr,
NULL, NULL,
}; };
ATTRIBUTE_GROUPS(net_class); ATTRIBUTE_GROUPS(net_class);
......
...@@ -990,6 +990,8 @@ static noinline size_t if_nlmsg_size(const struct net_device *dev, ...@@ -990,6 +990,8 @@ static noinline size_t if_nlmsg_size(const struct net_device *dev,
+ nla_total_size(4) /* IFLA_NEW_NETNSID */ + nla_total_size(4) /* IFLA_NEW_NETNSID */
+ nla_total_size(1) /* IFLA_PROTO_DOWN */ + nla_total_size(1) /* IFLA_PROTO_DOWN */
+ nla_total_size(4) /* IFLA_IF_NETNSID */ + nla_total_size(4) /* IFLA_IF_NETNSID */
+ nla_total_size(4) /* IFLA_CARRIER_UP_COUNT */
+ nla_total_size(4) /* IFLA_CARRIER_DOWN_COUNT */
+ 0; + 0;
} }
...@@ -1551,8 +1553,13 @@ static int rtnl_fill_ifinfo(struct sk_buff *skb, ...@@ -1551,8 +1553,13 @@ static int rtnl_fill_ifinfo(struct sk_buff *skb,
nla_put_string(skb, IFLA_QDISC, dev->qdisc->ops->id)) || nla_put_string(skb, IFLA_QDISC, dev->qdisc->ops->id)) ||
nla_put_ifalias(skb, dev) || nla_put_ifalias(skb, dev) ||
nla_put_u32(skb, IFLA_CARRIER_CHANGES, nla_put_u32(skb, IFLA_CARRIER_CHANGES,
atomic_read(&dev->carrier_changes)) || atomic_read(&dev->carrier_up_count) +
nla_put_u8(skb, IFLA_PROTO_DOWN, dev->proto_down)) atomic_read(&dev->carrier_down_count)) ||
nla_put_u8(skb, IFLA_PROTO_DOWN, dev->proto_down) ||
nla_put_u32(skb, IFLA_CARRIER_UP_COUNT,
atomic_read(&dev->carrier_up_count)) ||
nla_put_u32(skb, IFLA_CARRIER_DOWN_COUNT,
atomic_read(&dev->carrier_down_count)))
goto nla_put_failure; goto nla_put_failure;
if (event != IFLA_EVENT_NONE) { if (event != IFLA_EVENT_NONE) {
...@@ -1656,6 +1663,8 @@ static const struct nla_policy ifla_policy[IFLA_MAX+1] = { ...@@ -1656,6 +1663,8 @@ static const struct nla_policy ifla_policy[IFLA_MAX+1] = {
[IFLA_EVENT] = { .type = NLA_U32 }, [IFLA_EVENT] = { .type = NLA_U32 },
[IFLA_GROUP] = { .type = NLA_U32 }, [IFLA_GROUP] = { .type = NLA_U32 },
[IFLA_IF_NETNSID] = { .type = NLA_S32 }, [IFLA_IF_NETNSID] = { .type = NLA_S32 },
[IFLA_CARRIER_UP_COUNT] = { .type = NLA_U32 },
[IFLA_CARRIER_DOWN_COUNT] = { .type = NLA_U32 },
}; };
static const struct nla_policy ifla_info_policy[IFLA_INFO_MAX+1] = { static const struct nla_policy ifla_info_policy[IFLA_INFO_MAX+1] = {
......
...@@ -510,7 +510,7 @@ void netif_carrier_on(struct net_device *dev) ...@@ -510,7 +510,7 @@ void netif_carrier_on(struct net_device *dev)
if (test_and_clear_bit(__LINK_STATE_NOCARRIER, &dev->state)) { if (test_and_clear_bit(__LINK_STATE_NOCARRIER, &dev->state)) {
if (dev->reg_state == NETREG_UNINITIALIZED) if (dev->reg_state == NETREG_UNINITIALIZED)
return; return;
atomic_inc(&dev->carrier_changes); atomic_inc(&dev->carrier_up_count);
linkwatch_fire_event(dev); linkwatch_fire_event(dev);
if (netif_running(dev)) if (netif_running(dev))
__netdev_watchdog_up(dev); __netdev_watchdog_up(dev);
...@@ -529,7 +529,7 @@ void netif_carrier_off(struct net_device *dev) ...@@ -529,7 +529,7 @@ void netif_carrier_off(struct net_device *dev)
if (!test_and_set_bit(__LINK_STATE_NOCARRIER, &dev->state)) { if (!test_and_set_bit(__LINK_STATE_NOCARRIER, &dev->state)) {
if (dev->reg_state == NETREG_UNINITIALIZED) if (dev->reg_state == NETREG_UNINITIALIZED)
return; return;
atomic_inc(&dev->carrier_changes); atomic_inc(&dev->carrier_down_count);
linkwatch_fire_event(dev); linkwatch_fire_event(dev);
} }
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment