Skip to content

Commit

Permalink
ethdev: deprecate port count function
Browse files Browse the repository at this point in the history
Some DPDK applications wrongly assume these requirements:
    - no hotplug, i.e. ports are never detached
    - all allocated ports are available to the application

Such application iterates over ports by its own mean.
The most common pattern is to request the port count and
assume ports with index in the range [0..count[ can be used.

In order to fix this common mistake in all external applications,
the function rte_eth_dev_count is deprecated, while introducing
the new functions rte_eth_dev_count_avail and rte_eth_dev_count_total.

Signed-off-by: Thomas Monjalon <[email protected]>
  • Loading branch information
tmonjalo committed Apr 17, 2018
1 parent a9dbe18 commit d9a42a6
Show file tree
Hide file tree
Showing 76 changed files with 151 additions and 79 deletions.
2 changes: 1 addition & 1 deletion app/proc-info/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -628,7 +628,7 @@ main(int argc, char **argv)
return 0;
}

nb_ports = rte_eth_dev_count();
nb_ports = rte_eth_dev_count_avail();
if (nb_ports == 0)
rte_exit(EXIT_FAILURE, "No Ethernet ports - bye\n");

Expand Down
2 changes: 1 addition & 1 deletion app/test-eventdev/evt_options.h
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,7 @@ evt_dump_producer_type(struct evt_options *opt)
case EVT_PROD_TYPE_ETH_RX_ADPTR:
snprintf(name, EVT_PROD_MAX_NAME_LEN,
"Ethdev Rx Adapter producers");
evt_dump("nb_ethdev", "%d", rte_eth_dev_count());
evt_dump("nb_ethdev", "%d", rte_eth_dev_count_avail());
break;
case EVT_PROD_TYPE_EVENT_TIMER_ADPTR:
if (opt->timdev_use_burst)
Expand Down
2 changes: 1 addition & 1 deletion app/test-eventdev/test_perf_atq.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ atq_nb_event_queues(struct evt_options *opt)
{
/* nb_queues = number of producers */
return opt->prod_type == EVT_PROD_TYPE_ETH_RX_ADPTR ?
rte_eth_dev_count() : evt_nr_active_lcores(opt->plcores);
rte_eth_dev_count_avail() : evt_nr_active_lcores(opt->plcores);
}

static inline __attribute__((always_inline)) void
Expand Down
2 changes: 1 addition & 1 deletion app/test-eventdev/test_perf_common.c
Original file line number Diff line number Diff line change
Expand Up @@ -700,7 +700,7 @@ perf_ethdev_setup(struct evt_test *test, struct evt_options *opt)
opt->prod_type == EVT_PROD_TYPE_EVENT_TIMER_ADPTR)
return 0;

if (!rte_eth_dev_count()) {
if (!rte_eth_dev_count_avail()) {
evt_err("No ethernet ports found.");
return -ENODEV;
}
Expand Down
2 changes: 1 addition & 1 deletion app/test-eventdev/test_perf_queue.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ perf_queue_nb_event_queues(struct evt_options *opt)
{
/* nb_queues = number of producers * number of stages */
uint8_t nb_prod = opt->prod_type == EVT_PROD_TYPE_ETH_RX_ADPTR ?
rte_eth_dev_count() : evt_nr_active_lcores(opt->plcores);
rte_eth_dev_count_avail() : evt_nr_active_lcores(opt->plcores);
return nb_prod * opt->nb_stages;
}

Expand Down
4 changes: 2 additions & 2 deletions app/test-eventdev/test_pipeline_atq.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ pipeline_atq_nb_event_queues(struct evt_options *opt)
{
RTE_SET_USED(opt);

return rte_eth_dev_count();
return rte_eth_dev_count_avail();
}

static int
Expand Down Expand Up @@ -324,7 +324,7 @@ pipeline_atq_eventdev_setup(struct evt_test *test, struct evt_options *opt)
uint8_t nb_worker_queues = 0;

nb_ports = evt_nr_active_lcores(opt->wlcores);
nb_queues = rte_eth_dev_count();
nb_queues = rte_eth_dev_count_avail();

/* One extra port and queueu for Tx service */
if (t->mt_unsafe) {
Expand Down
6 changes: 3 additions & 3 deletions app/test-eventdev/test_pipeline_common.c
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ pipeline_opt_check(struct evt_options *opt, uint64_t nb_queues)
*/
lcores = 2;

if (!rte_eth_dev_count()) {
if (!rte_eth_dev_count_avail()) {
evt_err("test needs minimum 1 ethernet dev");
return -1;
}
Expand Down Expand Up @@ -234,7 +234,7 @@ pipeline_ethdev_setup(struct evt_test *test, struct evt_options *opt)
};

RTE_SET_USED(opt);
if (!rte_eth_dev_count()) {
if (!rte_eth_dev_count_avail()) {
evt_err("No ethernet ports found.\n");
return -ENODEV;
}
Expand Down Expand Up @@ -419,7 +419,7 @@ pipeline_event_tx_service_setup(struct evt_test *test, struct evt_options *opt,
tx->dev_id = opt->dev_id;
tx->queue_id = tx_queue_id;
tx->port_id = tx_port_id;
tx->nb_ethports = rte_eth_dev_count();
tx->nb_ethports = rte_eth_dev_count_avail();
tx->t = t;

/* Register Tx service */
Expand Down
6 changes: 3 additions & 3 deletions app/test-eventdev/test_pipeline_queue.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
static __rte_always_inline int
pipeline_queue_nb_event_queues(struct evt_options *opt)
{
uint16_t eth_count = rte_eth_dev_count();
uint16_t eth_count = rte_eth_dev_count_avail();

return (eth_count * opt->nb_stages) + eth_count;
}
Expand Down Expand Up @@ -333,15 +333,15 @@ pipeline_queue_eventdev_setup(struct evt_test *test, struct evt_options *opt)
uint8_t nb_worker_queues = 0;

nb_ports = evt_nr_active_lcores(opt->wlcores);
nb_queues = rte_eth_dev_count() * (nb_stages);
nb_queues = rte_eth_dev_count_avail() * (nb_stages);

/* Extra port for Tx service. */
if (t->mt_unsafe) {
tx_evqueue_id = nb_queues;
nb_ports++;
nb_queues++;
} else
nb_queues += rte_eth_dev_count();
nb_queues += rte_eth_dev_count_avail();

rte_event_dev_info_get(opt->dev_id, &info);

Expand Down
2 changes: 1 addition & 1 deletion app/test-pmd/cmdline.c
Original file line number Diff line number Diff line change
Expand Up @@ -5546,7 +5546,7 @@ static void cmd_create_bonded_device_parsed(void *parsed_result,
port_id);

/* Update number of ports */
nb_ports = rte_eth_dev_count();
nb_ports = rte_eth_dev_count_avail();
reconfig(port_id, res->socket);
rte_eth_promiscuous_enable(port_id);
}
Expand Down
6 changes: 3 additions & 3 deletions app/test-pmd/testpmd.c
Original file line number Diff line number Diff line change
Expand Up @@ -1916,7 +1916,7 @@ attach_port(char *identifier)
reconfig(pi, socket_id);
rte_eth_promiscuous_enable(pi);

nb_ports = rte_eth_dev_count();
nb_ports = rte_eth_dev_count_avail();

ports[pi].port_status = RTE_PORT_STOPPED;

Expand Down Expand Up @@ -1944,7 +1944,7 @@ detach_port(portid_t port_id)
return;
}

nb_ports = rte_eth_dev_count();
nb_ports = rte_eth_dev_count_avail();

printf("Port '%s' is detached. Now total ports is %d\n",
name, nb_ports);
Expand Down Expand Up @@ -2585,7 +2585,7 @@ main(int argc, char** argv)
rte_pdump_init(NULL);
#endif

nb_ports = (portid_t) rte_eth_dev_count();
nb_ports = (portid_t) rte_eth_dev_count_avail();
if (nb_ports == 0)
TESTPMD_LOG(WARNING, "No probed ethernet devices\n");

Expand Down
7 changes: 7 additions & 0 deletions doc/guides/rel_notes/release_18_05.rst
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,13 @@ API Changes
memory footprint which helps in better cache utilization when large number
of meter objects are used.

* ethdev: The function ``rte_eth_dev_count``, often mis-used to iterate
over ports, is deprecated and replaced by ``rte_eth_dev_count_avail``.
There is also a new function ``rte_eth_dev_count_total`` to get the
total number of allocated ports, available or not.
The hotplug-proof applications should use ``RTE_ETH_FOREACH_DEV`` or
``RTE_ETH_FOREACH_DEV_OWNED_BY`` as port iterators.

* ethdev, in struct ``struct rte_eth_dev_info``, field ``rte_pci_device *pci_dev``
replaced with field ``struct rte_device *device``.

Expand Down
2 changes: 1 addition & 1 deletion doc/guides/sample_app_ug/quota_watermark.rst
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ Then, a call to init_dpdk(), defined in init.c, is made to initialize the poll m
if (ret < 0)
rte_exit(EXIT_FAILURE, "rte_pci_probe(): error %d\n", ret);
if (rte_eth_dev_count() < 2)
if (rte_eth_dev_count_avail() < 2)
rte_exit(EXIT_FAILURE, "Not enough Ethernet port available\n");
}
Expand Down
2 changes: 1 addition & 1 deletion examples/bond/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -748,7 +748,7 @@ main(int argc, char *argv[])
argc -= ret;
argv += ret;

nb_ports = rte_eth_dev_count();
nb_ports = rte_eth_dev_count_avail();
if (nb_ports == 0)
rte_exit(EXIT_FAILURE, "Give at least one port\n");
else if (nb_ports > MAX_PORTS)
Expand Down
6 changes: 3 additions & 3 deletions examples/distributor/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ struct lcore_params {
static int
lcore_rx(struct lcore_params *p)
{
const uint16_t nb_ports = rte_eth_dev_count();
const uint16_t nb_ports = rte_eth_dev_count_avail();
const int socket_id = rte_socket_id();
uint16_t port;
struct rte_mbuf *bufs[BURST_SIZE*2];
Expand Down Expand Up @@ -542,7 +542,7 @@ lcore_worker(struct lcore_params *p)
* for single port, xor_val will be zero so we won't modify the output
* port, otherwise we send traffic from 0 to 1, 2 to 3, and vice versa
*/
const unsigned xor_val = (rte_eth_dev_count() > 1);
const unsigned xor_val = (rte_eth_dev_count_avail() > 1);
struct rte_mbuf *buf[8] __rte_cache_aligned;

for (i = 0; i < 8; i++)
Expand Down Expand Up @@ -678,7 +678,7 @@ main(int argc, char *argv[])
"1 lcore for packet TX\n"
"and at least 1 lcore for worker threads\n");

nb_ports = rte_eth_dev_count();
nb_ports = rte_eth_dev_count_avail();
if (nb_ports == 0)
rte_exit(EXIT_FAILURE, "Error: no ethernet ports detected\n");
if (nb_ports != 1 && (nb_ports & 1))
Expand Down
2 changes: 1 addition & 1 deletion examples/ethtool/ethtool-app/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ int main(int argc, char **argv)
if (cnt_args_parsed < 0)
rte_exit(EXIT_FAILURE, "rte_eal_init(): Failed");

cnt_ports = rte_eth_dev_count();
cnt_ports = rte_eth_dev_count_avail();
printf("Number of NICs: %i\n", cnt_ports);
if (cnt_ports == 0)
rte_exit(EXIT_FAILURE, "No available NIC ports!\n");
Expand Down
4 changes: 2 additions & 2 deletions examples/eventdev_pipeline/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -429,7 +429,7 @@ int
main(int argc, char **argv)
{
struct worker_data *worker_data;
unsigned int num_ports;
uint16_t num_ports;
int lcore_id;
int err;

Expand All @@ -451,7 +451,7 @@ main(int argc, char **argv)
/* Parse cli options*/
parse_app_args(argc, argv);

num_ports = rte_eth_dev_count();
num_ports = rte_eth_dev_count_avail();
if (num_ports == 0)
rte_panic("No ethernet ports found\n");

Expand Down
4 changes: 2 additions & 2 deletions examples/eventdev_pipeline/pipeline_worker_tx.c
Original file line number Diff line number Diff line change
Expand Up @@ -422,7 +422,7 @@ setup_eventdev_worker_tx(struct cons_data *cons_data,
const uint8_t dev_id = 0;
const uint8_t nb_ports = cdata.num_workers;
uint8_t nb_slots = 0;
uint8_t nb_queues = rte_eth_dev_count();
uint8_t nb_queues = rte_eth_dev_count_avail();

/*
* In case where all type queues are not enabled, use queues equal to
Expand All @@ -431,7 +431,7 @@ setup_eventdev_worker_tx(struct cons_data *cons_data,
*/
if (!atq) {
nb_queues *= cdata.num_stages;
nb_queues += rte_eth_dev_count();
nb_queues += rte_eth_dev_count_avail();
}

struct rte_event_dev_config config = {
Expand Down
2 changes: 1 addition & 1 deletion examples/exception_path/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -559,7 +559,7 @@ main(int argc, char** argv)
}

/* Get number of ports found in scan */
nb_sys_ports = rte_eth_dev_count();
nb_sys_ports = rte_eth_dev_count_avail();
if (nb_sys_ports == 0)
FATAL_ERROR("No supported Ethernet device found");
/* Find highest port set in portmask */
Expand Down
4 changes: 2 additions & 2 deletions examples/flow_classify/flow_classify.c
Original file line number Diff line number Diff line change
Expand Up @@ -752,7 +752,7 @@ int
main(int argc, char *argv[])
{
struct rte_mempool *mbuf_pool;
uint8_t nb_ports;
uint16_t nb_ports;
uint16_t portid;
int ret;
int socket_id;
Expand All @@ -776,7 +776,7 @@ main(int argc, char *argv[])
rte_exit(EXIT_FAILURE, "Invalid flow_classify parameters\n");

/* Check that there is an even number of ports to send/receive on. */
nb_ports = rte_eth_dev_count();
nb_ports = rte_eth_dev_count_avail();
if (nb_ports < 2 || (nb_ports & 1))
rte_exit(EXIT_FAILURE, "Error: number of ports must be even\n");

Expand Down
3 changes: 3 additions & 0 deletions examples/flow_filtering/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ CFLAGS += -O3 $(shell pkg-config --cflags libdpdk)
LDFLAGS_SHARED = $(shell pkg-config --libs libdpdk)
LDFLAGS_STATIC = -Wl,-Bstatic $(shell pkg-config --static --libs libdpdk)

CFLAGS += -DALLOW_EXPERIMENTAL_API

build/$(APP)-shared: $(SRCS-y) Makefile $(PC_FILE) | build
$(CC) $(CFLAGS) $(SRCS-y) -o $@ $(LDFLAGS) $(LDFLAGS_SHARED)

Expand All @@ -75,6 +77,7 @@ RTE_TARGET ?= x86_64-native-linuxapp-gcc

include $(RTE_SDK)/mk/rte.vars.mk

CFLAGS += -DALLOW_EXPERIMENTAL_API
CFLAGS += -O3
CFLAGS += $(WERROR_FLAGS)

Expand Down
4 changes: 2 additions & 2 deletions examples/flow_filtering/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ int
main(int argc, char **argv)
{
int ret;
uint8_t nr_ports;
uint16_t nr_ports;
struct rte_flow_error error;

ret = rte_eal_init(argc, argv);
Expand All @@ -243,7 +243,7 @@ main(int argc, char **argv)
signal(SIGINT, signal_handler);
signal(SIGTERM, signal_handler);

nr_ports = rte_eth_dev_count();
nr_ports = rte_eth_dev_count_avail();
if (nr_ports == 0)
rte_exit(EXIT_FAILURE, ":: no Ethernet ports found\n");
port_id = 0;
Expand Down
1 change: 1 addition & 0 deletions examples/flow_filtering/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@
sources = files(
'main.c',
)
allow_experimental_apis = true
4 changes: 2 additions & 2 deletions examples/ip_fragmentation/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -843,7 +843,7 @@ main(int argc, char **argv)
struct rte_eth_txconf *txconf;
struct rx_queue *rxq;
int socket, ret;
unsigned nb_ports;
uint16_t nb_ports;
uint16_t queueid = 0;
unsigned lcore_id = 0, rx_lcore_id = 0;
uint32_t n_tx_queue, nb_lcores;
Expand All @@ -861,7 +861,7 @@ main(int argc, char **argv)
if (ret < 0)
rte_exit(EXIT_FAILURE, "Invalid arguments");

nb_ports = rte_eth_dev_count();
nb_ports = rte_eth_dev_count_avail();
if (nb_ports == 0)
rte_exit(EXIT_FAILURE, "No ports found!\n");

Expand Down
2 changes: 1 addition & 1 deletion examples/ip_reassembly/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -1008,7 +1008,7 @@ main(int argc, char **argv)
if (ret < 0)
rte_exit(EXIT_FAILURE, "Invalid IP reassembly parameters\n");

nb_ports = rte_eth_dev_count();
nb_ports = rte_eth_dev_count_avail();
if (nb_ports == 0)
rte_exit(EXIT_FAILURE, "No ports found!\n");

Expand Down
2 changes: 1 addition & 1 deletion examples/ipv4_multicast/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -674,7 +674,7 @@ main(int argc, char **argv)
if (clone_pool == NULL)
rte_exit(EXIT_FAILURE, "Cannot init clone mbuf pool\n");

nb_ports = rte_eth_dev_count();
nb_ports = rte_eth_dev_count_avail();
if (nb_ports == 0)
rte_exit(EXIT_FAILURE, "No physical ports!\n");
if (nb_ports > MAX_PORTS)
Expand Down
2 changes: 1 addition & 1 deletion examples/kni/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -926,7 +926,7 @@ main(int argc, char** argv)
}

/* Get number of ports found in scan */
nb_sys_ports = rte_eth_dev_count();
nb_sys_ports = rte_eth_dev_count_avail();
if (nb_sys_ports == 0)
rte_exit(EXIT_FAILURE, "No supported Ethernet device found\n");

Expand Down
2 changes: 1 addition & 1 deletion examples/l2fwd-cat/l2fwd-cat.c
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ main(int argc, char *argv[])
argv += ret;

/* Check that there is an even number of ports to send/receive on. */
nb_ports = rte_eth_dev_count();
nb_ports = rte_eth_dev_count_avail();
if (nb_ports < 2 || (nb_ports & 1))
rte_exit(EXIT_FAILURE, "Error: number of ports must be even\n");

Expand Down
2 changes: 1 addition & 1 deletion examples/l2fwd-crypto/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -2311,7 +2311,7 @@ initialize_ports(struct l2fwd_crypto_options *options)
{
uint16_t last_portid = 0, portid;
unsigned enabled_portcount = 0;
unsigned nb_ports = rte_eth_dev_count();
unsigned nb_ports = rte_eth_dev_count_avail();

if (nb_ports == 0) {
printf("No Ethernet ports - bye\n");
Expand Down
2 changes: 1 addition & 1 deletion examples/l2fwd-jobstats/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -770,7 +770,7 @@ main(int argc, char **argv)
if (l2fwd_pktmbuf_pool == NULL)
rte_exit(EXIT_FAILURE, "Cannot init mbuf pool\n");

nb_ports = rte_eth_dev_count();
nb_ports = rte_eth_dev_count_avail();
if (nb_ports == 0)
rte_exit(EXIT_FAILURE, "No Ethernet ports - bye\n");

Expand Down
Loading

0 comments on commit d9a42a6

Please sign in to comment.