net-sysfs: expose number of carrier on/off changes
authordavid decotigny <decot@googlers.com>
Sat, 29 Mar 2014 16:48:35 +0000 (09:48 -0700)
committerDavid S. Miller <davem@davemloft.net>
Mon, 31 Mar 2014 20:24:52 +0000 (16:24 -0400)
This allows to monitor carrier on/off transitions and detect link
flapping issues:
 - new /sys/class/net/X/carrier_changes
 - new rtnetlink IFLA_CARRIER_CHANGES (getlink)

Tested:
  - grep . /sys/class/net/*/carrier_changes
    + ip link set dev X down/up
    + plug/unplug cable
  - updated iproute2: prints IFLA_CARRIER_CHANGES
  - iproute2 20121211-2 (debian): unchanged behavior

Signed-off-by: David Decotigny <decot@googlers.com>
Signed-off-by: David S. Miller <davem@davemloft.net>
include/linux/netdevice.h
include/uapi/linux/if_link.h
net/core/net-sysfs.c
net/core/rtnetlink.c
net/sched/sch_generic.c

index 34cae3ee74f1789cbd249fb78f122e29ef1b32d5..45537ed7a5b31b95931aa8302639bf102f5b0246 100644 (file)
@@ -1308,6 +1308,9 @@ struct net_device {
        atomic_long_t           rx_dropped;
        atomic_long_t           tx_dropped;
 
+       /* Stats to monitor carrier on<->off transitions */
+       atomic_t                carrier_changes;
+
 #ifdef CONFIG_WIRELESS_EXT
        /* List of functions to handle Wireless Extensions (instead of ioctl).
         * See <net/iw_handler.h> for details. Jean II */
index 16410b6e7819fa93f242665b036812f44fd5fa41..9a7f7ace66494e144c55c08d5bbba085de3a2b7c 100644 (file)
@@ -144,6 +144,7 @@ enum {
        IFLA_NUM_RX_QUEUES,
        IFLA_CARRIER,
        IFLA_PHYS_PORT_ID,
+       IFLA_CARRIER_CHANGES,
        __IFLA_MAX
 };
 
index daed9a64c6f6b350ef4518fc91b8b6718d58624d..46239627848436938cbd1efb743f60ffb0b15289 100644 (file)
@@ -253,6 +253,16 @@ static ssize_t operstate_show(struct device *dev,
 }
 static DEVICE_ATTR_RO(operstate);
 
+static ssize_t carrier_changes_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_changes));
+}
+static DEVICE_ATTR_RO(carrier_changes);
+
 /* read-write attributes */
 
 static int change_mtu(struct net_device *net, unsigned long new_mtu)
@@ -386,6 +396,7 @@ static struct attribute *net_class_attrs[] = {
        &dev_attr_duplex.attr,
        &dev_attr_dormant.attr,
        &dev_attr_operstate.attr,
+       &dev_attr_carrier_changes.attr,
        &dev_attr_ifalias.attr,
        &dev_attr_carrier.attr,
        &dev_attr_mtu.attr,
index e7c6006bc3eaf72e4a22d729d6d8643bff6bef58..d4ff41739b0f23fcb572905dd34288cb1d8ebd49 100644 (file)
@@ -822,6 +822,7 @@ static noinline size_t if_nlmsg_size(const struct net_device *dev,
               + nla_total_size(4) /* IFLA_NUM_RX_QUEUES */
               + nla_total_size(1) /* IFLA_OPERSTATE */
               + nla_total_size(1) /* IFLA_LINKMODE */
+              + nla_total_size(4) /* IFLA_CARRIER_CHANGES */
               + nla_total_size(ext_filter_mask
                                & RTEXT_FILTER_VF ? 4 : 0) /* IFLA_NUM_VF */
               + rtnl_vfinfo_size(dev, ext_filter_mask) /* IFLA_VFINFO_LIST */
@@ -970,7 +971,9 @@ static int rtnl_fill_ifinfo(struct sk_buff *skb, struct net_device *dev,
            (dev->qdisc &&
             nla_put_string(skb, IFLA_QDISC, dev->qdisc->ops->id)) ||
            (dev->ifalias &&
-            nla_put_string(skb, IFLA_IFALIAS, dev->ifalias)))
+            nla_put_string(skb, IFLA_IFALIAS, dev->ifalias)) ||
+           nla_put_u32(skb, IFLA_CARRIER_CHANGES,
+                       atomic_read(&dev->carrier_changes)))
                goto nla_put_failure;
 
        if (1) {
@@ -1147,6 +1150,7 @@ static const struct nla_policy ifla_policy[IFLA_MAX+1] = {
        [IFLA_NUM_TX_QUEUES]    = { .type = NLA_U32 },
        [IFLA_NUM_RX_QUEUES]    = { .type = NLA_U32 },
        [IFLA_PHYS_PORT_ID]     = { .type = NLA_BINARY, .len = MAX_PHYS_PORT_ID_LEN },
+       [IFLA_CARRIER_CHANGES]  = { .type = NLA_U32 },  /* ignored */
 };
 
 static const struct nla_policy ifla_info_policy[IFLA_INFO_MAX+1] = {
index e82e43b69c335bccbd58e6ba4ef10b56acd8f5a6..e1543b03e39d10c9b952a7ccf454cbd76dd09f09 100644 (file)
@@ -310,6 +310,7 @@ void netif_carrier_on(struct net_device *dev)
        if (test_and_clear_bit(__LINK_STATE_NOCARRIER, &dev->state)) {
                if (dev->reg_state == NETREG_UNINITIALIZED)
                        return;
+               atomic_inc(&dev->carrier_changes);
                linkwatch_fire_event(dev);
                if (netif_running(dev))
                        __netdev_watchdog_up(dev);
@@ -328,6 +329,7 @@ void netif_carrier_off(struct net_device *dev)
        if (!test_and_set_bit(__LINK_STATE_NOCARRIER, &dev->state)) {
                if (dev->reg_state == NETREG_UNINITIALIZED)
                        return;
+               atomic_inc(&dev->carrier_changes);
                linkwatch_fire_event(dev);
        }
 }