common/mlx5: fix the sysfs port name translation

Message ID 20221110155726.649190-1-bingz@nvidia.com (mailing list archive)
State Superseded, archived
Delegated to: Raslan Darawsheh
Headers
Series common/mlx5: fix the sysfs port name translation |

Checks

Context Check Description
ci/checkpatch success coding style OK
ci/Intel-compilation success Compilation OK
ci/intel-Testing success Testing PASS
ci/iol-mellanox-Performance success Performance Testing PASS
ci/iol-intel-Functional success Functional Testing PASS
ci/iol-intel-Performance success Performance Testing PASS
ci/github-robot: build success github build: passed
ci/iol-aarch64-unit-testing success Testing PASS
ci/iol-x86_64-unit-testing success Testing PASS
ci/iol-x86_64-compile-testing success Testing PASS
ci/iol-aarch64-compile-testing success Testing PASS

Commit Message

Bing Zhao Nov. 10, 2022, 3:57 p.m. UTC
  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.

By changing the condition from "equal to" to "more than or equal
to", the port type can be recognized successfully.

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

Signed-off-by: Bing Zhao <bingz@nvidia.com>
Acked-by: Viacheslav Ovsiienko <viacheslavo@nvidia.com>
---
 drivers/common/mlx5/linux/mlx5_common_os.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
  

Comments

Stephen Hemminger Nov. 10, 2022, 4:21 p.m. UTC | #1
On Thu, 10 Nov 2022 17:57:26 +0200
Bing Zhao <bingz@nvidia.com> wrote:

> 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.

Why not fix the DPDK driver to strip off the new line when the port
name is read? The code in mlx5_os.c should do it there.
  
Bing Zhao Nov. 11, 2022, 5:41 a.m. UTC | #2
Hi Stephen,

> -----Original Message-----
> From: Stephen Hemminger <stephen@networkplumber.org>
> Sent: Friday, November 11, 2022 12:22 AM
> To: Bing Zhao <bingz@nvidia.com>
> Cc: Slava Ovsiienko <viacheslavo@nvidia.com>; Matan Azrad
> <matan@nvidia.com>; dev@dpdk.org; Raslan Darawsheh
> <rasland@nvidia.com>; stable@dpdk.org
> Subject: Re: [PATCH] common/mlx5: fix the sysfs port name
> translation
> 
> External email: Use caution opening links or attachments
> 
> 
> On Thu, 10 Nov 2022 17:57:26 +0200
> Bing Zhao <bingz@nvidia.com> wrote:
> 
> > 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.
> 
> Why not fix the DPDK driver to strip off the new line when the port
> name is read? The code in mlx5_os.c should do it there.

Thanks for your comments. Yes, IF_NAMESIZE is large enough to read all the characters including the newline into the buffer.
After fgets(), the strlen() or strchr() can be used to remove the tailing newline character.


BR. Bing
  
Stephen Hemminger Nov. 11, 2022, 5:38 p.m. UTC | #3
On Fri, 11 Nov 2022 05:41:05 +0000
Bing Zhao <bingz@nvidia.com> wrote:

> Hi Stephen,
> 
> > -----Original Message-----
> > From: Stephen Hemminger <stephen@networkplumber.org>
> > Sent: Friday, November 11, 2022 12:22 AM
> > To: Bing Zhao <bingz@nvidia.com>
> > Cc: Slava Ovsiienko <viacheslavo@nvidia.com>; Matan Azrad
> > <matan@nvidia.com>; dev@dpdk.org; Raslan Darawsheh
> > <rasland@nvidia.com>; stable@dpdk.org
> > Subject: Re: [PATCH] common/mlx5: fix the sysfs port name
> > translation
> > 
> > External email: Use caution opening links or attachments
> > 
> > 
> > On Thu, 10 Nov 2022 17:57:26 +0200
> > Bing Zhao <bingz@nvidia.com> wrote:
> >   
> > > 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.
> > 
> > Why not fix the DPDK driver to strip off the new line when the port
> > name is read? The code in mlx5_os.c should do it there.  
> 
> Thanks for your comments. Yes, IF_NAMESIZE is large enough to read all the characters including the newline into the buffer.
> After fgets(), the strlen() or strchr() can be used to remove the tailing newline character.
> 
> 
> BR. Bing

Another alternative would be to use getline() which doesn't have annoying length restrictions
to deal with.
  

Patch

diff --git a/drivers/common/mlx5/linux/mlx5_common_os.c b/drivers/common/mlx5/linux/mlx5_common_os.c
index aafff60eeb..655347a7c8 100644
--- a/drivers/common/mlx5/linux/mlx5_common_os.c
+++ b/drivers/common/mlx5/linux/mlx5_common_os.c
@@ -108,7 +108,7 @@  mlx5_translate_port_name(const char *port_name_in,
 	sc_items = sscanf(port_name_in, "%c%c%d%c%c%d%c",
 			  &pf_c1, &pf_c2, &port_info_out->pf_num,
 			  &vf_c1, &vf_c2, &port_info_out->port_name, &eol);
-	if (sc_items == 6 && pf_c1 == 'p' && pf_c2 == 'f') {
+	if (sc_items >= 6 && pf_c1 == 'p' && pf_c2 == 'f') {
 		if (vf_c1 == 'v' && vf_c2 == 'f') {
 			/* Kernel ver >= 5.0 or OFED ver >= 4.6 */
 			port_info_out->name_type =
@@ -128,7 +128,7 @@  mlx5_translate_port_name(const char *port_name_in,
 	 */
 	sc_items = sscanf(port_name_in, "%c%d%c",
 			  &pf_c1, &port_info_out->port_name, &eol);
-	if (sc_items == 2 && pf_c1 == 'p') {
+	if (sc_items >= 2 && pf_c1 == 'p') {
 		port_info_out->name_type = MLX5_PHYS_PORT_NAME_TYPE_UPLINK;
 		return;
 	}
@@ -138,7 +138,7 @@  mlx5_translate_port_name(const char *port_name_in,
 	 */
 	sc_items = sscanf(port_name_in, "%c%c%d%c",
 			  &pf_c1, &pf_c2, &port_info_out->pf_num, &eol);
-	if (sc_items == 3 && pf_c1 == 'p' && pf_c2 == 'f') {
+	if (sc_items >= 3 && pf_c1 == 'p' && pf_c2 == 'f') {
 		port_info_out->port_name = -1;
 		port_info_out->name_type = MLX5_PHYS_PORT_NAME_TYPE_PFHPF;
 		return;