DPDK patches and discussions
 help / color / mirror / Atom feed
From: Stephen Hemminger <stephen@networkplumber.org>
To: Matan Azrad <matan@mellanox.com>
Cc: "dev@dpdk.org" <dev@dpdk.org>,
	Stephen Hemminger <sthemmin@microsoft.com>
Subject: Re: [dpdk-dev] [PATCH v4 1/4] examples/multi_process/client_server_mp: check port validity
Date: Tue, 30 Jul 2019 09:50:32 -0700	[thread overview]
Message-ID: <20190730095032.57e1fd05@hermes.lan> (raw)
In-Reply-To: <AM0PR0502MB401936C7EBAFEA40D43BB8FCD2DC0@AM0PR0502MB4019.eurprd05.prod.outlook.com>

On Tue, 30 Jul 2019 16:39:52 +0000
Matan Azrad <matan@mellanox.com> wrote:

> Hi
> 
> From: Stephen Hemminger
> > On Tue, 30 Jul 2019 09:21:02 +0000
> > Matan Azrad <matan@mellanox.com> wrote:
> >   
> > > Hi Stephen
> > >
> > > From:  Stephen Hemminger  
> > > > From: Stephen Hemminger <sthemmin@microsoft.com>
> > > >
> > > > The mp_server incorrectly allows a port mask that included hidden
> > > > ports and which later caused either lost packets or failed initialization.
> > > >
> > > > This fixes explicitly checking that each bit in portmask is a valid
> > > > port before using it.
> > > >
> > > > Fixes: 5b7ba31148a8 ("ethdev: add port ownership")
> > > > Signed-off-by: Stephen Hemminger <sthemmin@microsoft.com>
> > > > ---
> > > >  .../client_server_mp/mp_server/args.c         | 46 +++++++++++++------
> > > >  .../client_server_mp/mp_server/args.h         |  2 +-
> > > >  .../client_server_mp/mp_server/init.c         |  7 +--
> > > >  3 files changed, 35 insertions(+), 20 deletions(-)
> > > >
> > > > diff --git
> > > > a/examples/multi_process/client_server_mp/mp_server/args.c
> > > > b/examples/multi_process/client_server_mp/mp_server/args.c
> > > > index b0d8d7665c85..c1ab12ad00d1 100644
> > > > --- a/examples/multi_process/client_server_mp/mp_server/args.c
> > > > +++ b/examples/multi_process/client_server_mp/mp_server/args.c
> > > > @@ -10,6 +10,7 @@
> > > >  #include <errno.h>
> > > >
> > > >  #include <rte_memory.h>
> > > > +#include <rte_ethdev.h>
> > > >  #include <rte_string_fns.h>
> > > >
> > > >  #include "common.h"
> > > > @@ -34,6 +35,22 @@ usage(void)
> > > >  	    , progname);
> > > >  }
> > > >
> > > > +/**
> > > > + * Check if port is present in the system
> > > > + * It maybe owned by a device and should not be used.
> > > > + */
> > > > +static int
> > > > +port_is_present(uint16_t portid)
> > > > +{
> > > > +	uint16_t id;
> > > > +
> > > > +	RTE_ETH_FOREACH_DEV(id) {
> > > > +		if (id == portid)
> > > > +			return 1;
> > > > +	}
> > > > +	return 0;
> > > > +}
> > > > +
> > > >  /**
> > > >   * The ports to be used by the application are passed in
> > > >   * the form of a bitmask. This function parses the bitmask @@
> > > > -41,31 +58,32 @@ usage(void)
> > > >   * array variable
> > > >   */
> > > >  static int
> > > > -parse_portmask(uint8_t max_ports, const char *portmask)
> > > > +parse_portmask(const char *portmask)
> > > >  {
> > > >  	char *end = NULL;
> > > >  	unsigned long pm;
> > > > -	uint16_t count = 0;
> > > > +	uint16_t count;
> > > >
> > > >  	if (portmask == NULL || *portmask == '\0')
> > > >  		return -1;
> > > >
> > > >  	/* convert parameter to a number and verify */
> > > >  	pm = strtoul(portmask, &end, 16);
> > > > -	if (end == NULL || *end != '\0' || pm == 0)
> > > > +	if (end == NULL || *end != '\0' || pm > UINT16_MAX || pm == 0)
> > > >  		return -1;
> > > >
> > > >  	/* loop through bits of the mask and mark ports */
> > > > -	while (pm != 0){
> > > > -		if (pm & 0x01){ /* bit is set in mask, use port */
> > > > -			if (count >= max_ports)
> > > > -				printf("WARNING: requested port %u not
> > > > present"
> > > > -				" - ignoring\n", (unsigned)count);
> > > > -			else
> > > > -			    ports->id[ports->num_ports++] = count;
> > > > +	for (count = 0; pm != 0; pm >>= 1, ++count) {
> > > > +		if ((pm & 0x1) == 0)
> > > > +			continue;
> > > > +
> > > > +		if (!port_is_present(count)) {
> > > > +			printf("WARNING: requested port %u not present -
> > > > ignoring\n",
> > > > +				count);
> > > > +			continue;
> > > >  		}
> > > > -		pm = (pm >> 1);
> > > > -		count++;
> > > > +
> > > > +		ports->id[ports->num_ports++] = count;
> > > >  	}  
> > >
> > > Why not just to do something like:
> > >
> > > RTE_ETH_FOREACH_DEV(id) {
> > > 	If (pm & (1 << id)) {
> > > 		pm &= ~(1 << id)
> > > 		put in list
> > > 	}
> > > }
> > > 	If (pm)
> > > 		Warning
> > >  
> > 
> > No real reason, was just trying to keep existing logic flow  
> 
> This is an example for the users\developers, and you on it,
> I think this is a good chance to show how to do it in the correct way - O(n) instead O(n^2).
> 
> Don't you think so?
>  
> I suggest to adjust. 
> 

This is a toy example short loops, it doesn't matter.
It is important to catch case where portmask contains hidden ports.

  reply	other threads:[~2019-07-30 16:50 UTC|newest]

Thread overview: 40+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-07-09 15:09 [dpdk-dev] [PATCH v3 0/2] examples/client_server_mp: fix port issues Stephen Hemminger
2019-07-09 15:09 ` [dpdk-dev] [PATCH v3 1/2] examples/multi_process/client_server_mp: check port validity Stephen Hemminger
2019-07-09 15:09 ` [dpdk-dev] [PATCH v3 2/2] examples/multi_process - fix crash in mp_client with sparse ports Stephen Hemminger
2019-07-26 16:50 ` [dpdk-dev] [PATCH v4 0/4] examples/client_server_mp: fix port issues Stephen Hemminger
2019-07-26 16:50   ` [dpdk-dev] [PATCH v4 1/4] examples/multi_process/client_server_mp: check port validity Stephen Hemminger
2019-07-30  9:21     ` Matan Azrad
2019-07-30 15:56       ` Stephen Hemminger
2019-07-30 16:39         ` Matan Azrad
2019-07-30 16:50           ` Stephen Hemminger [this message]
2019-07-26 16:50   ` [dpdk-dev] [PATCH v4 2/4] examples/multi_process/client_server_mp - fix crash in mp_client with sparse ports Stephen Hemminger
2019-07-26 16:50   ` [dpdk-dev] [PATCH v4 3/4] examples/multi_process/client_server_mp/mp_server: fix style Stephen Hemminger
2019-07-26 16:50   ` [dpdk-dev] [PATCH v4 4/4] examples/multi_process/client_server_mp/mp_server: use ether format address Stephen Hemminger
2019-08-02  2:58   ` [dpdk-dev] [PATCH v5 0/4] examples/client_server_mp: port id fixes Stephen Hemminger
2019-08-02  2:58     ` [dpdk-dev] [PATCH v5 1/4] examples/multi_process/client_server_mp: check port validity Stephen Hemminger
2019-08-02  5:33       ` Matan Azrad
2019-08-02 15:53         ` Stephen Hemminger
2019-08-04  8:31           ` Matan Azrad
2019-08-05 16:00             ` Stephen Hemminger
2019-08-06  8:19               ` Matan Azrad
2019-08-06 15:39                 ` Stephen Hemminger
2019-08-06 20:03                   ` Matan Azrad
2019-08-06 23:09                     ` Stephen Hemminger
2019-08-07  5:38                       ` Matan Azrad
2019-08-07  5:53                         ` Stephen Hemminger
2019-08-07  7:02                           ` Matan Azrad
2019-08-07 15:15                             ` Stephen Hemminger
2019-08-02  2:58     ` [dpdk-dev] [PATCH v5 2/4] examples/multi_process/client_server_mp - fix crash in mp_client with sparse ports Stephen Hemminger
2019-08-02  2:58     ` [dpdk-dev] [PATCH v5 3/4] examples/multi_process/client_server_mp/mp_server: fix style Stephen Hemminger
2019-08-02 11:09       ` David Marchand
2019-08-02  2:58     ` [dpdk-dev] [PATCH v5 4/4] examples/multi_process/client_server_mp/mp_server: use ether format address Stephen Hemminger
2019-08-02 23:52   ` [dpdk-dev] [PATCH v6 0/2] examples/client_server_mp: port id (fixes only) Stephen Hemminger
2019-08-02 23:52     ` [dpdk-dev] [PATCH v6 1/2] examples/multi_process/client_server_mp: check port validity Stephen Hemminger
2019-08-02 23:52     ` [dpdk-dev] [PATCH v6 2/2] examples/multi_process/client_server_mp - fix crash in mp_client with sparse ports Stephen Hemminger
2019-08-05 16:38   ` [dpdk-dev] [PATCH v7 0/2] examples/client_server_mp: port id (fixes only) Stephen Hemminger
2019-08-05 16:38     ` [dpdk-dev] [PATCH v7 1/2] examples/multi_process/client_server_mp: check port validity Stephen Hemminger
2019-08-06 12:07       ` Matan Azrad
2019-11-13 18:53         ` Stephen Hemminger
2019-08-05 16:38     ` [dpdk-dev] [PATCH v7 2/2] examples/multi_process/client_server_mp - fix crash in mp_client with sparse ports Stephen Hemminger
2019-08-07  5:40     ` [dpdk-dev] [PATCH v7 0/2] examples/client_server_mp: port id (fixes only) Matan Azrad
2019-11-25 22:51       ` Thomas Monjalon

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20190730095032.57e1fd05@hermes.lan \
    --to=stephen@networkplumber.org \
    --cc=dev@dpdk.org \
    --cc=matan@mellanox.com \
    --cc=sthemmin@microsoft.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).