Skip to content

Commit

Permalink
examples/multi_process: check server port validity
Browse files Browse the repository at this point in the history
[ upstream commit 1f41d98 ]

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: 5b7ba31 ("ethdev: add port ownership")

Signed-off-by: Stephen Hemminger <sthemmin@microsoft.com>
Acked-by: Matan Azrad <matan@mellanox.com>
  • Loading branch information
shemminger authored and kevintraynor committed Dec 11, 2019
1 parent 7280388 commit 573bdb4
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 24 deletions.
40 changes: 22 additions & 18 deletions examples/multi_process/client_server_mp/mp_server/args.c
Expand Up @@ -10,6 +10,7 @@
#include <errno.h>

#include <rte_memory.h>
#include <rte_ethdev.h>
#include <rte_string_fns.h>

#include "common.h"
Expand Down Expand Up @@ -41,31 +42,34 @@ 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;
unsigned long long pm;
uint16_t id;

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)
errno = 0;
pm = strtoull(portmask, &end, 16);
if (errno != 0 || end == NULL || *end != '\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;
}
pm = (pm >> 1);
count++;
RTE_ETH_FOREACH_DEV(id) {
unsigned long msk = 1u << id;

if ((pm & msk) == 0)
continue;

pm &= ~msk;
ports->id[ports->num_ports++] = id;
}

if (pm != 0) {
printf("WARNING: leftover ports in mask %#llx - ignoring\n",
pm);
}

return 0;
Expand Down Expand Up @@ -99,7 +103,7 @@ parse_num_clients(const char *clients)
* on error.
*/
int
parse_app_args(uint16_t max_ports, int argc, char *argv[])
parse_app_args(int argc, char *argv[])
{
int option_index, opt;
char **argvopt = argv;
Expand All @@ -112,7 +116,7 @@ parse_app_args(uint16_t max_ports, int argc, char *argv[])
&option_index)) != EOF){
switch (opt){
case 'p':
if (parse_portmask(max_ports, optarg) != 0){
if (parse_portmask(optarg) != 0) {
usage();
return -1;
}
Expand Down
2 changes: 1 addition & 1 deletion examples/multi_process/client_server_mp/mp_server/args.h
Expand Up @@ -5,6 +5,6 @@
#ifndef _ARGS_H_
#define _ARGS_H_

int parse_app_args(uint16_t max_ports, int argc, char *argv[]);
int parse_app_args(int argc, char *argv[]);

#endif /* ifndef _ARGS_H_ */
7 changes: 2 additions & 5 deletions examples/multi_process/client_server_mp/mp_server/init.c
Expand Up @@ -238,7 +238,7 @@ init(int argc, char *argv[])
{
int retval;
const struct rte_memzone *mz;
uint16_t i, total_ports;
uint16_t i;

/* init EAL, parsing EAL args */
retval = rte_eal_init(argc, argv);
Expand All @@ -247,9 +247,6 @@ init(int argc, char *argv[])
argc -= retval;
argv += retval;

/* get total number of ports */
total_ports = rte_eth_dev_count_total();

/* set up array for port data */
mz = rte_memzone_reserve(MZ_PORT_INFO, sizeof(*ports),
rte_socket_id(), NO_FLAGS);
Expand All @@ -259,7 +256,7 @@ init(int argc, char *argv[])
ports = mz->addr;

/* parse additional, application arguments */
retval = parse_app_args(total_ports, argc, argv);
retval = parse_app_args(argc, argv);
if (retval != 0)
return -1;

Expand Down

0 comments on commit 573bdb4

Please sign in to comment.