Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

ethtool: linkstate: add a statistic for PHY down events

The previous attempt to augment carrier_down (see Link)
was not met with much enthusiasm so let's do the simple
thing of exposing what some devices already maintain.
Add a common ethtool statistic for link going down.
Currently users have to maintain per-driver mapping
to extract the right stat from the vendor-specific ethtool -S
stats. carrier_down does not fit the bill because it counts
a lot of software related false positives.

Add the statistic to the extended link state API to steer
vendors towards implementing all of it.

Implement for bnxt and all Linux-controlled PHYs. mlx5 and (possibly)
enic also have a counter for this but I leave the implementation
to their maintainers.

Link: https://lore.kernel.org/r/20220520004500.2250674-1-kuba@kernel.org
Reviewed-by: Florian Fainelli <f.fainelli@gmail.com>
Reviewed-by: Michael Chan <michael.chan@broadcom.com>
Reviewed-by: Andrew Lunn <andrew@lunn.ch>
Signed-off-by: Jakub Kicinski <kuba@kernel.org>
Link: https://lore.kernel.org/r/20221104190125.684910-1-kuba@kernel.org
Signed-off-by: Paolo Abeni <pabeni@redhat.com>

authored by

Jakub Kicinski and committed by
Paolo Abeni
9a0f830f 91c596cc

+61 -1
+1
Documentation/networking/ethtool-netlink.rst
··· 491 491 ``ETHTOOL_A_LINKSTATE_SQI_MAX`` u32 Max support SQI value 492 492 ``ETHTOOL_A_LINKSTATE_EXT_STATE`` u8 link extended state 493 493 ``ETHTOOL_A_LINKSTATE_EXT_SUBSTATE`` u8 link extended substate 494 + ``ETHTOOL_A_LINKSTATE_EXT_DOWN_CNT`` u32 count of link down events 494 495 ==================================== ====== ============================ 495 496 496 497 For most NIC drivers, the value of ``ETHTOOL_A_LINKSTATE_LINK`` returns
+15
drivers/net/ethernet/broadcom/bnxt/bnxt_ethtool.c
··· 4112 4112 *ranges = bnxt_rmon_ranges; 4113 4113 } 4114 4114 4115 + static void bnxt_get_link_ext_stats(struct net_device *dev, 4116 + struct ethtool_link_ext_stats *stats) 4117 + { 4118 + struct bnxt *bp = netdev_priv(dev); 4119 + u64 *rx; 4120 + 4121 + if (BNXT_VF(bp) || !(bp->flags & BNXT_FLAG_PORT_STATS_EXT)) 4122 + return; 4123 + 4124 + rx = bp->rx_port_stats_ext.sw_stats; 4125 + stats->link_down_events = 4126 + *(rx + BNXT_RX_STATS_EXT_OFFSET(link_down_events)); 4127 + } 4128 + 4115 4129 void bnxt_ethtool_free(struct bnxt *bp) 4116 4130 { 4117 4131 kfree(bp->test_info); ··· 4175 4161 .get_eeprom = bnxt_get_eeprom, 4176 4162 .set_eeprom = bnxt_set_eeprom, 4177 4163 .get_link = bnxt_get_link, 4164 + .get_link_ext_stats = bnxt_get_link_ext_stats, 4178 4165 .get_eee = bnxt_get_eee, 4179 4166 .set_eee = bnxt_set_eee, 4180 4167 .get_module_info = bnxt_get_module_info,
+1
drivers/net/phy/phy.c
··· 67 67 { 68 68 phydev->phy_link_change(phydev, false); 69 69 phy_led_trigger_change_speed(phydev); 70 + WRITE_ONCE(phydev->link_down_events, phydev->link_down_events + 1); 70 71 } 71 72 72 73 static const char *phy_pause_str(struct phy_device *phydev)
+17
include/linux/ethtool.h
··· 125 125 }; 126 126 }; 127 127 128 + struct ethtool_link_ext_stats { 129 + /* Custom Linux statistic for PHY level link down events. 130 + * In a simpler world it should be equal to netdev->carrier_down_count 131 + * unfortunately netdev also counts local reconfigurations which don't 132 + * actually take the physical link down, not to mention NC-SI which, 133 + * if present, keeps the link up regardless of host state. 134 + * This statistic counts when PHY _actually_ went down, or lost link. 135 + * 136 + * Note that we need u64 for ethtool_stats_init() and comparisons 137 + * to ETHTOOL_STAT_NOT_SET, but only u32 is exposed to the user. 138 + */ 139 + u64 link_down_events; 140 + }; 141 + 128 142 /** 129 143 * ethtool_rxfh_indir_default - get default value for RX flow hash indirection 130 144 * @index: Index in RX flow hash indirection table ··· 495 481 * do not attach ext_substate attribute to netlink message). If link_ext_state 496 482 * and link_ext_substate are unknown, return -ENODATA. If not implemented, 497 483 * link_ext_state and link_ext_substate will not be sent to userspace. 484 + * @get_link_ext_stats: Read extra link-related counters. 498 485 * @get_eeprom_len: Read range of EEPROM addresses for validation of 499 486 * @get_eeprom and @set_eeprom requests. 500 487 * Returns 0 if device does not support EEPROM access. ··· 667 652 u32 (*get_link)(struct net_device *); 668 653 int (*get_link_ext_state)(struct net_device *, 669 654 struct ethtool_link_ext_state_info *); 655 + void (*get_link_ext_stats)(struct net_device *dev, 656 + struct ethtool_link_ext_stats *stats); 670 657 int (*get_eeprom_len)(struct net_device *); 671 658 int (*get_eeprom)(struct net_device *, 672 659 struct ethtool_eeprom *, u8 *);
+3
include/linux/phy.h
··· 600 600 * @psec: Pointer to Power Sourcing Equipment control struct 601 601 * @lock: Mutex for serialization access to PHY 602 602 * @state_queue: Work queue for state machine 603 + * @link_down_events: Number of times link was lost 603 604 * @shared: Pointer to private data shared by phys in one package 604 605 * @priv: Pointer to driver private data 605 606 * ··· 723 722 u8 mdix_ctrl; 724 723 725 724 int pma_extable; 725 + 726 + unsigned int link_down_events; 726 727 727 728 void (*phy_link_change)(struct phy_device *phydev, bool up); 728 729 void (*adjust_link)(struct net_device *dev);
+1
include/uapi/linux/ethtool_netlink.h
··· 262 262 ETHTOOL_A_LINKSTATE_SQI_MAX, /* u32 */ 263 263 ETHTOOL_A_LINKSTATE_EXT_STATE, /* u8 */ 264 264 ETHTOOL_A_LINKSTATE_EXT_SUBSTATE, /* u8 */ 265 + ETHTOOL_A_LINKSTATE_EXT_DOWN_CNT, /* u32 */ 265 266 266 267 /* add new constants above here */ 267 268 __ETHTOOL_A_LINKSTATE_CNT,
+23 -1
net/ethtool/linkstate.c
··· 13 13 int link; 14 14 int sqi; 15 15 int sqi_max; 16 + struct ethtool_link_ext_stats link_stats; 16 17 bool link_ext_state_provided; 17 18 struct ethtool_link_ext_state_info ethtool_link_ext_state_info; 18 19 }; ··· 23 22 24 23 const struct nla_policy ethnl_linkstate_get_policy[] = { 25 24 [ETHTOOL_A_LINKSTATE_HEADER] = 26 - NLA_POLICY_NESTED(ethnl_header_policy), 25 + NLA_POLICY_NESTED(ethnl_header_policy_stats), 27 26 }; 28 27 29 28 static int linkstate_get_sqi(struct net_device *dev) ··· 108 107 goto out; 109 108 } 110 109 110 + ethtool_stats_init((u64 *)&data->link_stats, 111 + sizeof(data->link_stats) / 8); 112 + 113 + if (req_base->flags & ETHTOOL_FLAG_STATS) { 114 + if (dev->phydev) 115 + data->link_stats.link_down_events = 116 + READ_ONCE(dev->phydev->link_down_events); 117 + 118 + if (dev->ethtool_ops->get_link_ext_stats) 119 + dev->ethtool_ops->get_link_ext_stats(dev, 120 + &data->link_stats); 121 + } 122 + 111 123 ret = 0; 112 124 out: 113 125 ethnl_ops_complete(dev); ··· 147 133 148 134 if (data->ethtool_link_ext_state_info.__link_ext_substate) 149 135 len += nla_total_size(sizeof(u8)); /* LINKSTATE_EXT_SUBSTATE */ 136 + 137 + if (data->link_stats.link_down_events != ETHTOOL_STAT_NOT_SET) 138 + len += nla_total_size(sizeof(u32)); 150 139 151 140 return len; 152 141 } ··· 182 165 data->ethtool_link_ext_state_info.__link_ext_substate)) 183 166 return -EMSGSIZE; 184 167 } 168 + 169 + if (data->link_stats.link_down_events != ETHTOOL_STAT_NOT_SET) 170 + if (nla_put_u32(skb, ETHTOOL_A_LINKSTATE_EXT_DOWN_CNT, 171 + data->link_stats.link_down_events)) 172 + return -EMSGSIZE; 185 173 186 174 return 0; 187 175 }