common/mlx5: fix the sysfs port name translation
Checks
Commit Message
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
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.
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
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.
@@ -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;