common/mlx5: share kernel interface name getter
authorMatan Azrad <matan@mellanox.com>
Thu, 18 Jun 2020 19:06:02 +0000 (19:06 +0000)
committerFerruh Yigit <ferruh.yigit@intel.com>
Tue, 30 Jun 2020 12:52:29 +0000 (14:52 +0200)
Some configuration of the mlx5 port are done by the kernel net device
associated to the IB device represents the PCI device.

The DPDK mlx5 driver uses Linux system calls, for example ioctl, in
order to configure per port configurations requested by the DPDK user.

One of the basic knowledges required to access the correct kernel net
device is its name.

Move function to get interface name from IB device path to the common
library.

Signed-off-by: Matan Azrad <matan@mellanox.com>
Reviewed-by: Maxime Coquelin <maxime.coquelin@redhat.com>
drivers/common/mlx5/linux/mlx5_common_os.c
drivers/common/mlx5/mlx5_common.h
drivers/common/mlx5/rte_common_mlx5_version.map
drivers/net/mlx5/linux/mlx5_ethdev_os.c
drivers/net/mlx5/linux/mlx5_os.c
drivers/net/mlx5/mlx5.h

index 4e04d70..cffa326 100644 (file)
@@ -8,8 +8,11 @@
 #ifdef RTE_IBVERBS_LINK_DLOPEN
 #include <dlfcn.h>
 #endif
+#include <dirent.h>
+#include <net/if.h>
 
 #include <rte_errno.h>
+#include <rte_string_fns.h>
 
 #include "mlx5_common.h"
 #include "mlx5_common_utils.h"
@@ -125,6 +128,92 @@ mlx5_translate_port_name(const char *port_name_in,
        port_info_out->name_type = MLX5_PHYS_PORT_NAME_TYPE_UNKNOWN;
 }
 
+/**
+ * Get kernel interface name from IB device path.
+ *
+ * @param[in] ibdev_path
+ *   Pointer to IB device path.
+ * @param[out] ifname
+ *   Interface name output buffer.
+ *
+ * @return
+ *   0 on success, a negative errno value otherwise and rte_errno is set.
+ */
+int
+mlx5_get_ifname_sysfs(const char *ibdev_path, char *ifname)
+{
+       DIR *dir;
+       struct dirent *dent;
+       unsigned int dev_type = 0;
+       unsigned int dev_port_prev = ~0u;
+       char match[IF_NAMESIZE] = "";
+
+       MLX5_ASSERT(ibdev_path);
+       {
+               MKSTR(path, "%s/device/net", ibdev_path);
+
+               dir = opendir(path);
+               if (dir == NULL) {
+                       rte_errno = errno;
+                       return -rte_errno;
+               }
+       }
+       while ((dent = readdir(dir)) != NULL) {
+               char *name = dent->d_name;
+               FILE *file;
+               unsigned int dev_port;
+               int r;
+
+               if ((name[0] == '.') &&
+                   ((name[1] == '\0') ||
+                    ((name[1] == '.') && (name[2] == '\0'))))
+                       continue;
+
+               MKSTR(path, "%s/device/net/%s/%s",
+                     ibdev_path, name,
+                     (dev_type ? "dev_id" : "dev_port"));
+
+               file = fopen(path, "rb");
+               if (file == NULL) {
+                       if (errno != ENOENT)
+                               continue;
+                       /*
+                        * Switch to dev_id when dev_port does not exist as
+                        * is the case with Linux kernel versions < 3.15.
+                        */
+try_dev_id:
+                       match[0] = '\0';
+                       if (dev_type)
+                               break;
+                       dev_type = 1;
+                       dev_port_prev = ~0u;
+                       rewinddir(dir);
+                       continue;
+               }
+               r = fscanf(file, (dev_type ? "%x" : "%u"), &dev_port);
+               fclose(file);
+               if (r != 1)
+                       continue;
+               /*
+                * Switch to dev_id when dev_port returns the same value for
+                * all ports. May happen when using a MOFED release older than
+                * 3.0 with a Linux kernel >= 3.15.
+                */
+               if (dev_port == dev_port_prev)
+                       goto try_dev_id;
+               dev_port_prev = dev_port;
+               if (dev_port == 0)
+                       strlcpy(match, name, IF_NAMESIZE);
+       }
+       closedir(dir);
+       if (match[0] == '\0') {
+               rte_errno = ENOENT;
+               return -rte_errno;
+       }
+       strncpy(ifname, match, IF_NAMESIZE);
+       return 0;
+}
+
 #ifdef MLX5_GLUE
 
 /**
index 8e679c6..1aeac4e 100644 (file)
@@ -198,6 +198,9 @@ check_cqe(volatile struct mlx5_cqe *cqe, const uint16_t cqes_n,
 
 __rte_internal
 int mlx5_dev_to_pci_addr(const char *dev_path, struct rte_pci_addr *pci_addr);
+__rte_internal
+int mlx5_get_ifname_sysfs(const char *ibdev_path, char *ifname);
+
 
 #define MLX5_CLASS_ARG_NAME "class"
 
index 3f62400..514d7c5 100644 (file)
@@ -36,6 +36,8 @@ INTERNAL {
        mlx5_devx_cmd_query_virtq;
        mlx5_devx_get_out_command_status;
 
+       mlx5_get_ifname_sysfs;
+
        mlx5_mp_init_primary;
        mlx5_mp_uninit_primary;
        mlx5_mp_init_secondary;
index ab47cb5..21105f6 100644 (file)
@@ -128,96 +128,11 @@ struct ethtool_link_settings {
 #define ETHTOOL_LINK_MODE_200000baseCR4_Full_BIT 2 /* 66 - 64 */
 #endif
 
-/**
- * Get master interface name from private structure.
- *
- * @param[in] dev
- *   Pointer to Ethernet device.
- * @param[out] ifname
- *   Interface name output buffer.
- *
- * @return
- *   0 on success, a negative errno value otherwise and rte_errno is set.
- */
-int
-mlx5_get_master_ifname(const char *ibdev_path, char (*ifname)[IF_NAMESIZE])
-{
-       DIR *dir;
-       struct dirent *dent;
-       unsigned int dev_type = 0;
-       unsigned int dev_port_prev = ~0u;
-       char match[IF_NAMESIZE] = "";
-
-       MLX5_ASSERT(ibdev_path);
-       {
-               MKSTR(path, "%s/device/net", ibdev_path);
-
-               dir = opendir(path);
-               if (dir == NULL) {
-                       rte_errno = errno;
-                       return -rte_errno;
-               }
-       }
-       while ((dent = readdir(dir)) != NULL) {
-               char *name = dent->d_name;
-               FILE *file;
-               unsigned int dev_port;
-               int r;
-
-               if ((name[0] == '.') &&
-                   ((name[1] == '\0') ||
-                    ((name[1] == '.') && (name[2] == '\0'))))
-                       continue;
-
-               MKSTR(path, "%s/device/net/%s/%s",
-                     ibdev_path, name,
-                     (dev_type ? "dev_id" : "dev_port"));
-
-               file = fopen(path, "rb");
-               if (file == NULL) {
-                       if (errno != ENOENT)
-                               continue;
-                       /*
-                        * Switch to dev_id when dev_port does not exist as
-                        * is the case with Linux kernel versions < 3.15.
-                        */
-try_dev_id:
-                       match[0] = '\0';
-                       if (dev_type)
-                               break;
-                       dev_type = 1;
-                       dev_port_prev = ~0u;
-                       rewinddir(dir);
-                       continue;
-               }
-               r = fscanf(file, (dev_type ? "%x" : "%u"), &dev_port);
-               fclose(file);
-               if (r != 1)
-                       continue;
-               /*
-                * Switch to dev_id when dev_port returns the same value for
-                * all ports. May happen when using a MOFED release older than
-                * 3.0 with a Linux kernel >= 3.15.
-                */
-               if (dev_port == dev_port_prev)
-                       goto try_dev_id;
-               dev_port_prev = dev_port;
-               if (dev_port == 0)
-                       strlcpy(match, name, sizeof(match));
-       }
-       closedir(dir);
-       if (match[0] == '\0') {
-               rte_errno = ENOENT;
-               return -rte_errno;
-       }
-       strncpy(*ifname, match, sizeof(*ifname));
-       return 0;
-}
 
 /**
  * Get interface name from private structure.
  *
- * This is a port representor-aware version of mlx5_get_master_ifname().
+ * This is a port representor-aware version of mlx5_get_ifname_sysfs().
  *
  * @param[in] dev
  *   Pointer to Ethernet device.
@@ -238,8 +153,8 @@ mlx5_get_ifname(const struct rte_eth_dev *dev, char (*ifname)[IF_NAMESIZE])
        ifindex = mlx5_ifindex(dev);
        if (!ifindex) {
                if (!priv->representor)
-                       return mlx5_get_master_ifname(priv->sh->ibdev_path,
-                                                     ifname);
+                       return mlx5_get_ifname_sysfs(priv->sh->ibdev_path,
+                                                    *ifname);
                rte_errno = ENXIO;
                return -rte_errno;
        }
index 3792371..b330cd5 100644 (file)
@@ -1637,8 +1637,8 @@ mlx5_os_pci_probe(struct rte_pci_driver *pci_drv __rte_unused,
                                         */
                                        continue;
                                }
-                               ret = mlx5_get_master_ifname
-                                       (ibv_match[i]->ibdev_path, &ifname);
+                               ret = mlx5_get_ifname_sysfs
+                                       (ibv_match[i]->ibdev_path, ifname);
                                if (!ret)
                                        list[ns].ifindex =
                                                if_nametoindex(ifname);
index 5bd5acd..8ecb59c 100644 (file)
@@ -731,7 +731,6 @@ int mlx5_hairpin_cap_get(struct rte_eth_dev *dev,
 /* mlx5_ethdev_os.c */
 
 int mlx5_get_ifname(const struct rte_eth_dev *dev, char (*ifname)[IF_NAMESIZE]);
-int mlx5_get_master_ifname(const char *ibdev_path, char (*ifname)[IF_NAMESIZE]);
 unsigned int mlx5_ifindex(const struct rte_eth_dev *dev);
 int mlx5_ifreq(const struct rte_eth_dev *dev, int req, struct ifreq *ifr);
 int mlx5_get_mtu(struct rte_eth_dev *dev, uint16_t *mtu);