* Get interface name from private structure. * * @param[in] priv * Pointer to private structure. * @param[out] ifname * Interface name output buffer. * * @return * 0 on success, negative errno value otherwise and rte_errno is set. */
| 58 | * 0 on success, negative errno value otherwise and rte_errno is set. |
| 59 | */ |
| 60 | int |
| 61 | mlx4_get_ifname(const struct mlx4_priv *priv, char (*ifname)[IF_NAMESIZE]) |
| 62 | { |
| 63 | DIR *dir; |
| 64 | struct dirent *dent; |
| 65 | unsigned int dev_type = 0; |
| 66 | unsigned int dev_port_prev = ~0u; |
| 67 | char match[IF_NAMESIZE] = ""; |
| 68 | |
| 69 | { |
| 70 | MKSTR(path, "%s/device/net", priv->ctx->device->ibdev_path); |
| 71 | |
| 72 | dir = opendir(path); |
| 73 | if (dir == NULL) { |
| 74 | rte_errno = errno; |
| 75 | return -rte_errno; |
| 76 | } |
| 77 | } |
| 78 | while ((dent = readdir(dir)) != NULL) { |
| 79 | char *name = dent->d_name; |
| 80 | FILE *file; |
| 81 | unsigned int dev_port; |
| 82 | int r; |
| 83 | |
| 84 | if ((name[0] == '.') && |
| 85 | ((name[1] == '\0') || |
| 86 | ((name[1] == '.') && (name[2] == '\0')))) |
| 87 | continue; |
| 88 | |
| 89 | MKSTR(path, "%s/device/net/%s/%s", |
| 90 | priv->ctx->device->ibdev_path, name, |
| 91 | (dev_type ? "dev_id" : "dev_port")); |
| 92 | |
| 93 | file = fopen(path, "rb"); |
| 94 | if (file == NULL) { |
| 95 | if (errno != ENOENT) |
| 96 | continue; |
| 97 | /* |
| 98 | * Switch to dev_id when dev_port does not exist as |
| 99 | * is the case with Linux kernel versions < 3.15. |
| 100 | */ |
| 101 | try_dev_id: |
| 102 | match[0] = '\0'; |
| 103 | if (dev_type) |
| 104 | break; |
| 105 | dev_type = 1; |
| 106 | dev_port_prev = ~0u; |
| 107 | rewinddir(dir); |
| 108 | continue; |
| 109 | } |
| 110 | r = fscanf(file, (dev_type ? "%x" : "%u"), &dev_port); |
| 111 | fclose(file); |
| 112 | if (r != 1) |
| 113 | continue; |
| 114 | /* |
| 115 | * Switch to dev_id when dev_port returns the same value for |
| 116 | * all ports. May happen when using a MOFED release older than |
| 117 | * 3.0 with a Linux kernel >= 3.15. |