Skip to content

Commit

Permalink
net/mlx5: fix sysfs port name translation
Browse files Browse the repository at this point in the history
[ upstream commit f8a226ed65fa1c6e085fb0d5a91bab7fb5034aec ]

With some OFED or upstream kernel of mlx5, the port name fetched from
"/sys/class/net/[DEV]/phys_port_name" may have a tailing "\n" as the
EOL. The sscanf() will return the scanned items number with this EOL.

In such case, the "equal to" condition is considered as false and
the function mlx5_translate_port_name() will recognize the port type
wrongly with UNKNOWN result.

The tailing carriage return character should be removed before
calling the mlx5_translate_port_name(), this was already done in the
NL message handling. In the meanwhile, the possible incorrect line
feed character is also taken into consideration.

Fixes: 654810b ("common/mlx5: share Netlink commands")
Fixes: 420bbda ("net/mlx5: fix host physical function representor naming")

Signed-off-by: Bing Zhao <bingz@nvidia.com>
Acked-by: Matan Azrad <matan@nvidia.com>
  • Loading branch information
zorrohahaha authored and kevintraynor committed Mar 29, 2023
1 parent 663a819 commit 80a5769
Showing 1 changed file with 17 additions and 2 deletions.
19 changes: 17 additions & 2 deletions drivers/net/mlx5/linux/mlx5_ethdev_os.c
Expand Up @@ -1105,7 +1105,8 @@ int
mlx5_sysfs_switch_info(unsigned int ifindex, struct mlx5_switch_info *info)
{
char ifname[IF_NAMESIZE];
char port_name[IF_NAMESIZE];
char *port_name = NULL;
size_t port_name_size = 0;
FILE *file;
struct mlx5_switch_info data = {
.master = 0,
Expand All @@ -1118,6 +1119,7 @@ mlx5_sysfs_switch_info(unsigned int ifindex, struct mlx5_switch_info *info)
bool port_switch_id_set = false;
bool device_dir = false;
char c;
ssize_t line_size;

if (!if_indextoname(ifindex, ifname)) {
rte_errno = errno;
Expand All @@ -1133,8 +1135,21 @@ mlx5_sysfs_switch_info(unsigned int ifindex, struct mlx5_switch_info *info)

file = fopen(phys_port_name, "rb");
if (file != NULL) {
if (fgets(port_name, IF_NAMESIZE, file) != NULL)
char *tail_nl;

line_size = getline(&port_name, &port_name_size, file);
if (line_size < 0) {
fclose(file);
rte_errno = errno;
return -rte_errno;
} else if (line_size > 0) {
/* Remove tailing newline character. */
tail_nl = strchr(port_name, '\n');
if (tail_nl)
*tail_nl = '\0';
mlx5_translate_port_name(port_name, &data);
}
free(port_name);
fclose(file);
}
file = fopen(phys_switch_id, "rb");
Expand Down

0 comments on commit 80a5769

Please sign in to comment.