Skip to content

Commit f8a226e

Browse files
zorrohahaharaslandarawsheh
authored andcommitted
net/mlx5: fix sysfs port name translation
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") Cc: [email protected] Signed-off-by: Bing Zhao <[email protected]> Acked-by: Matan Azrad <[email protected]>
1 parent cd41581 commit f8a226e

File tree

1 file changed

+17
-2
lines changed

1 file changed

+17
-2
lines changed

drivers/net/mlx5/linux/mlx5_ethdev_os.c

+17-2
Original file line numberDiff line numberDiff line change
@@ -1035,7 +1035,8 @@ int
10351035
mlx5_sysfs_switch_info(unsigned int ifindex, struct mlx5_switch_info *info)
10361036
{
10371037
char ifname[IF_NAMESIZE];
1038-
char port_name[IF_NAMESIZE];
1038+
char *port_name = NULL;
1039+
size_t port_name_size = 0;
10391040
FILE *file;
10401041
struct mlx5_switch_info data = {
10411042
.master = 0,
@@ -1048,6 +1049,7 @@ mlx5_sysfs_switch_info(unsigned int ifindex, struct mlx5_switch_info *info)
10481049
bool port_switch_id_set = false;
10491050
bool device_dir = false;
10501051
char c;
1052+
ssize_t line_size;
10511053

10521054
if (!if_indextoname(ifindex, ifname)) {
10531055
rte_errno = errno;
@@ -1063,8 +1065,21 @@ mlx5_sysfs_switch_info(unsigned int ifindex, struct mlx5_switch_info *info)
10631065

10641066
file = fopen(phys_port_name, "rb");
10651067
if (file != NULL) {
1066-
if (fgets(port_name, IF_NAMESIZE, file) != NULL)
1068+
char *tail_nl;
1069+
1070+
line_size = getline(&port_name, &port_name_size, file);
1071+
if (line_size < 0) {
1072+
fclose(file);
1073+
rte_errno = errno;
1074+
return -rte_errno;
1075+
} else if (line_size > 0) {
1076+
/* Remove tailing newline character. */
1077+
tail_nl = strchr(port_name, '\n');
1078+
if (tail_nl)
1079+
*tail_nl = '\0';
10671080
mlx5_translate_port_name(port_name, &data);
1081+
}
1082+
free(port_name);
10681083
fclose(file);
10691084
}
10701085
file = fopen(phys_switch_id, "rb");

0 commit comments

Comments
 (0)