Querying link parameters in mlx5 driver is implemented using
either ETHTOOL_GLINKSETTINGS or ETHTOOL_GSET ioctl commands.
Link status reported by the above can be stale, due to a delay
introduced by Linux kernel's linkwatch mechanism.

This patch fixes that by reading link carrier status through
ETHTOOL_GLINK command. This ethtool command,
implemented by mlx5 since kernel 4.14, will always get
the current link status. It also flushes the pending link watch
events, so following ETHTOOL_GLINKSETTINGS/ETHTOOL_GSET will
return actual link carrier status.

Fixes: 17f95513adca ("net/mlx5: fix link status change detection")
Cc: [email protected]
Cc: [email protected]

Signed-off-by: Cosmin Ratiu <[email protected]>
Signed-off-by: Dariusz Sosnowski <[email protected]>
---
 .mailmap                                |  1 +
 drivers/net/mlx5/linux/mlx5_ethdev_os.c | 46 ++++++++++++++++++++++---
 drivers/net/mlx5/mlx5.h                 |  1 +
 3 files changed, 44 insertions(+), 4 deletions(-)

diff --git a/.mailmap b/.mailmap
index c350b8f2b5..1723ef8fa7 100644
--- a/.mailmap
+++ b/.mailmap
@@ -316,6 +316,7 @@ Congjie Zhou <[email protected]>
 Congwen Zhang <[email protected]>
 Conor Fogarty <[email protected]>
 Conor Walsh <[email protected]>
+Cosmin Ratiu <[email protected]>
 Cristian Bidea <[email protected]>
 Cristian Dumitrescu <[email protected]>
 Cristian Sovaiala <[email protected]>
diff --git a/drivers/net/mlx5/linux/mlx5_ethdev_os.c 
b/drivers/net/mlx5/linux/mlx5_ethdev_os.c
index f6394ebd19..f30fb6da5c 100644
--- a/drivers/net/mlx5/linux/mlx5_ethdev_os.c
+++ b/drivers/net/mlx5/linux/mlx5_ethdev_os.c
@@ -317,6 +317,40 @@ mlx5_find_master_dev(struct rte_eth_dev *dev)
        return NULL;
 }
 
+/**
+ * Retrieve the carrier state of a device.
+ *
+ * The IFF_RUNNING flag reported by SIOCGIFFLAGS reflects the RFC 2863
+ * operational state, which the kernel only refreshes from its link watch
+ * work queue and which may therefore lag the actual carrier by up to a
+ * second. ETHTOOL_GLINK reports the carrier directly, and additionally
+ * flushes any link watch event still pending for the device.
+ *
+ * @param dev
+ *   Pointer to Ethernet device structure.
+ *
+ * @return
+ *   1 if the carrier is up, 0 otherwise.
+ *   Negative errno value is returned on error.
+ */
+int
+mlx5_link_carrier_get(struct rte_eth_dev *dev)
+{
+       struct ethtool_value ecmd = {
+               .cmd = ETHTOOL_GLINK,
+       };
+       struct ifreq ifr = {
+               .ifr_data = (void *)&ecmd,
+       };
+       int ret;
+
+       ret = mlx5_ifreq(dev, SIOCETHTOOL, &ifr);
+       if (ret != 0)
+               return ret;
+
+       return !!ecmd.data;
+}
+
 /**
  * DPDK callback to retrieve physical link information.
  *
@@ -347,9 +381,11 @@ mlx5_link_update_unlocked_gset(struct rte_eth_dev *dev,
                        dev->data->port_id, strerror(rte_errno));
                return ret;
        }
+       ret = mlx5_link_carrier_get(dev);
+       if (ret < 0)
+               return ret;
        dev_link = (struct rte_eth_link) {
-               .link_status = ((ifr.ifr_flags & IFF_UP) &&
-                               (ifr.ifr_flags & IFF_RUNNING)),
+               .link_status = ((ifr.ifr_flags & IFF_UP) && ret),
        };
        ifr = (struct ifreq) {
                .ifr_data = (void *)&edata,
@@ -428,9 +464,11 @@ mlx5_link_update_unlocked_gs(struct rte_eth_dev *dev,
                        dev->data->port_id, strerror(rte_errno));
                return ret;
        }
+       ret = mlx5_link_carrier_get(dev);
+       if (ret < 0)
+               return ret;
        dev_link = (struct rte_eth_link) {
-               .link_status = ((ifr.ifr_flags & IFF_UP) &&
-                               (ifr.ifr_flags & IFF_RUNNING)),
+               .link_status = ((ifr.ifr_flags & IFF_UP) && ret),
        };
        ifr = (struct ifreq) {
                .ifr_data = (void *)&gcmd,
diff --git a/drivers/net/mlx5/mlx5.h b/drivers/net/mlx5/mlx5.h
index 7923125c5c..5ea49ca807 100644
--- a/drivers/net/mlx5/mlx5.h
+++ b/drivers/net/mlx5/mlx5.h
@@ -2391,6 +2391,7 @@ int mlx5_get_mtu(struct rte_eth_dev *dev, uint16_t *mtu);
 int mlx5_set_mtu(struct rte_eth_dev *dev, uint16_t mtu);
 int mlx5_read_clock(struct rte_eth_dev *dev, uint64_t *clock);
 int mlx5_link_update(struct rte_eth_dev *dev, int wait_to_complete);
+int mlx5_link_carrier_get(struct rte_eth_dev *dev);
 int mlx5_dev_get_flow_ctrl(struct rte_eth_dev *dev,
                           struct rte_eth_fc_conf *fc_conf);
 int mlx5_dev_set_flow_ctrl(struct rte_eth_dev *dev,
-- 
2.43.0

Reply via email to