1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2015 Intel Corporation
12 #include <rte_cycles.h>
13 #include <sys/queue.h>
15 #include <rte_byteorder.h>
16 #include <rte_common.h>
17 #include <rte_debug.h>
18 #include <rte_ethdev.h>
20 #include <rte_lcore.h>
21 #include <rte_memory.h>
22 #include <rte_bus_vdev.h>
24 #include <rte_string_fns.h>
25 #include <rte_errno.h>
26 #include <rte_eth_bond.h>
30 #define SLAVE_COUNT (4)
32 #define RXTX_RING_SIZE 1024
33 #define RXTX_QUEUE_COUNT 4
35 #define BONDED_DEV_NAME ("net_bonding_rss")
37 #define SLAVE_DEV_NAME_FMT ("net_null%d")
38 #define SLAVE_RXTX_QUEUE_FMT ("rssconf_slave%d_q%d")
40 #define NUM_MBUFS 8191
41 #define MBUF_SIZE (1600 + RTE_PKTMBUF_HEADROOM)
42 #define MBUF_CACHE_SIZE 250
45 #define INVALID_SOCKET_ID (-1)
46 #define INVALID_PORT_ID (0xFF)
47 #define INVALID_BONDING_MODE (-1)
51 struct rte_eth_dev_info dev_info;
53 struct rte_eth_rss_conf rss_conf;
55 struct rte_eth_rss_reta_entry64 reta_conf[512 / RTE_RETA_GROUP_SIZE];
58 struct rte_ring *rxtx_queue[RXTX_QUEUE_COUNT];
61 struct link_bonding_rssconf_unittest_params {
63 struct rte_eth_dev_info bond_dev_info;
64 struct rte_eth_rss_reta_entry64 bond_reta_conf[512 / RTE_RETA_GROUP_SIZE];
65 struct slave_conf slave_ports[SLAVE_COUNT];
67 struct rte_mempool *mbuf_pool;
70 static struct link_bonding_rssconf_unittest_params test_params = {
71 .bond_port_id = INVALID_PORT_ID,
73 [0 ... SLAVE_COUNT - 1] = { .port_id = INVALID_PORT_ID, .is_slave = 0}
79 * Default port configuration with RSS turned off
81 static struct rte_eth_conf default_pmd_conf = {
83 .mq_mode = ETH_MQ_RX_NONE,
84 .max_rx_pkt_len = RTE_ETHER_MAX_LEN,
88 .mq_mode = ETH_MQ_TX_NONE,
93 static struct rte_eth_conf rss_pmd_conf = {
95 .mq_mode = ETH_MQ_RX_RSS,
96 .max_rx_pkt_len = RTE_ETHER_MAX_LEN,
100 .mq_mode = ETH_MQ_TX_NONE,
105 .rss_hf = ETH_RSS_IPV6,
111 #define FOR_EACH(_i, _item, _array, _size) \
112 for (_i = 0, _item = &_array[0]; _i < _size && (_item = &_array[_i]); _i++)
114 /* Macro for iterating over every port that can be used as a slave
116 * _i variable used as an index in test_params->slave_ports
117 * _slave pointer to &test_params->slave_ports[_idx]
119 #define FOR_EACH_PORT(_i, _port) \
120 FOR_EACH(_i, _port, test_params.slave_ports, \
121 RTE_DIM(test_params.slave_ports))
124 configure_ethdev(uint16_t port_id, struct rte_eth_conf *eth_conf,
129 TEST_ASSERT(rte_eth_dev_configure(port_id, RXTX_QUEUE_COUNT,
130 RXTX_QUEUE_COUNT, eth_conf) == 0, "Failed to configure device %u",
133 for (rxq = 0; rxq < RXTX_QUEUE_COUNT; rxq++) {
134 TEST_ASSERT(rte_eth_rx_queue_setup(port_id, rxq, RXTX_RING_SIZE,
135 rte_eth_dev_socket_id(port_id), NULL,
136 test_params.mbuf_pool) == 0, "Failed to setup rx queue.");
139 for (txq = 0; txq < RXTX_QUEUE_COUNT; txq++) {
140 TEST_ASSERT(rte_eth_tx_queue_setup(port_id, txq, RXTX_RING_SIZE,
141 rte_eth_dev_socket_id(port_id), NULL) == 0,
142 "Failed to setup tx queue.");
146 TEST_ASSERT(rte_eth_dev_start(port_id) == 0,
147 "Failed to start device (%d).", port_id);
154 * Remove all slaves from bonding
160 struct slave_conf *port;
162 FOR_EACH_PORT(n, port) {
163 port = &test_params.slave_ports[n];
164 if (port->is_slave) {
165 TEST_ASSERT_SUCCESS(rte_eth_bond_slave_remove(
166 test_params.bond_port_id, port->port_id),
167 "Cannot remove slave %d from bonding", port->port_id);
176 remove_slaves_and_stop_bonded_device(void)
178 TEST_ASSERT_SUCCESS(remove_slaves(), "Removing slaves");
179 TEST_ASSERT_SUCCESS(rte_eth_dev_stop(test_params.bond_port_id),
180 "Failed to stop port %u", test_params.bond_port_id);
185 * Add all slaves to bonding
191 struct slave_conf *port;
193 FOR_EACH_PORT(n, port) {
194 port = &test_params.slave_ports[n];
195 if (!port->is_slave) {
196 TEST_ASSERT_SUCCESS(rte_eth_bond_slave_add(test_params.bond_port_id,
197 port->port_id), "Cannot attach slave %d to the bonding",
207 * Set all RETA values in port_id to value
210 reta_set(uint16_t port_id, uint8_t value, int reta_size)
212 struct rte_eth_rss_reta_entry64 reta_conf[512/RTE_RETA_GROUP_SIZE];
215 for (i = 0; i < reta_size / RTE_RETA_GROUP_SIZE; i++) {
216 /* select all fields to set */
217 reta_conf[i].mask = ~0LL;
218 for (j = 0; j < RTE_RETA_GROUP_SIZE; j++)
219 reta_conf[i].reta[j] = value;
222 return rte_eth_dev_rss_reta_update(port_id, reta_conf, reta_size);
226 * Check if slaves RETA is synchronized with bonding port. Returns 1 if slave
227 * port is synced with bonding port.
230 reta_check_synced(struct slave_conf *port)
234 for (i = 0; i < test_params.bond_dev_info.reta_size;
237 int index = i / RTE_RETA_GROUP_SIZE;
238 int shift = i % RTE_RETA_GROUP_SIZE;
240 if (port->reta_conf[index].reta[shift] !=
241 test_params.bond_reta_conf[index].reta[shift])
250 * Fetch bonding ports RETA
253 bond_reta_fetch(void) {
256 for (j = 0; j < test_params.bond_dev_info.reta_size / RTE_RETA_GROUP_SIZE;
258 test_params.bond_reta_conf[j].mask = ~0LL;
260 TEST_ASSERT_SUCCESS(rte_eth_dev_rss_reta_query(test_params.bond_port_id,
261 test_params.bond_reta_conf, test_params.bond_dev_info.reta_size),
262 "Cannot take bonding ports RSS configuration");
270 slave_reta_fetch(struct slave_conf *port) {
273 for (j = 0; j < port->dev_info.reta_size / RTE_RETA_GROUP_SIZE; j++)
274 port->reta_conf[j].mask = ~0LL;
276 TEST_ASSERT_SUCCESS(rte_eth_dev_rss_reta_query(port->port_id,
277 port->reta_conf, port->dev_info.reta_size),
278 "Cannot take bonding ports RSS configuration");
283 * Remove and add slave to check if slaves configuration is synced with
284 * the bonding ports values after adding new slave.
287 slave_remove_and_add(void)
289 struct slave_conf *port = &(test_params.slave_ports[0]);
291 /* 1. Remove first slave from bonding */
292 TEST_ASSERT_SUCCESS(rte_eth_bond_slave_remove(test_params.bond_port_id,
293 port->port_id), "Cannot remove slave #d from bonding");
295 /* 2. Change removed (ex-)slave and bonding configuration to different
298 reta_set(test_params.bond_port_id, 1, test_params.bond_dev_info.reta_size);
301 reta_set(port->port_id, 2, port->dev_info.reta_size);
302 slave_reta_fetch(port);
304 TEST_ASSERT(reta_check_synced(port) == 0,
305 "Removed slave didn't should be synchronized with bonding port");
307 /* 3. Add (ex-)slave and check if configuration changed*/
308 TEST_ASSERT_SUCCESS(rte_eth_bond_slave_add(test_params.bond_port_id,
309 port->port_id), "Cannot add slave");
312 slave_reta_fetch(port);
314 return reta_check_synced(port);
318 * Test configuration propagation over slaves.
325 struct slave_conf *port;
326 uint8_t bond_rss_key[40];
327 struct rte_eth_rss_conf bond_rss_conf;
331 uint64_t default_rss_hf = 0;
333 retval = rte_eth_dev_info_get(test_params.bond_port_id,
334 &test_params.bond_dev_info);
335 TEST_ASSERT((retval == 0),
336 "Error during getting device (port %u) info: %s\n",
337 test_params.bond_port_id, strerror(-retval));
340 * Test hash function propagation
342 for (i = 0; i < sizeof(test_params.bond_dev_info.flow_type_rss_offloads)*8;
345 rss_hf = test_params.bond_dev_info.flow_type_rss_offloads & (1<<i);
347 bond_rss_conf.rss_key = NULL;
348 bond_rss_conf.rss_hf = rss_hf;
350 retval = rte_eth_dev_rss_hash_update(test_params.bond_port_id,
352 TEST_ASSERT_SUCCESS(retval, "Cannot set slaves hash function");
354 FOR_EACH_PORT(n, port) {
355 port = &test_params.slave_ports[n];
357 retval = rte_eth_dev_rss_hash_conf_get(port->port_id,
359 TEST_ASSERT_SUCCESS(retval,
360 "Cannot take slaves RSS configuration");
362 TEST_ASSERT(port->rss_conf.rss_hf == rss_hf,
363 "Hash function not propagated for slave %d",
367 default_rss_hf = rss_hf;
373 * Test key propagation
375 for (i = 1; i < 10; i++) {
377 /* Set all keys to zero */
378 FOR_EACH_PORT(n, port) {
379 port = &test_params.slave_ports[n];
380 memset(port->rss_conf.rss_key, 0, 40);
381 retval = rte_eth_dev_rss_hash_update(port->port_id,
383 TEST_ASSERT_SUCCESS(retval, "Cannot set slaves RSS keys");
386 memset(bond_rss_key, i, sizeof(bond_rss_key));
387 bond_rss_conf.rss_hf = default_rss_hf,
388 bond_rss_conf.rss_key = bond_rss_key;
389 bond_rss_conf.rss_key_len = 40;
391 retval = rte_eth_dev_rss_hash_update(test_params.bond_port_id,
393 TEST_ASSERT_SUCCESS(retval, "Cannot set bonded port RSS keys");
395 FOR_EACH_PORT(n, port) {
396 port = &test_params.slave_ports[n];
398 retval = rte_eth_dev_rss_hash_conf_get(port->port_id,
401 TEST_ASSERT_SUCCESS(retval,
402 "Cannot take slaves RSS configuration");
405 retval = memcmp(port->rss_conf.rss_key, bond_rss_key,
406 sizeof(bond_rss_key));
407 TEST_ASSERT(retval == 0, "Key value not propagated for slave %d",
413 * Test RETA propagation
415 for (i = 0; i < RXTX_QUEUE_COUNT; i++) {
417 /* Set all keys to zero */
418 FOR_EACH_PORT(n, port) {
419 port = &test_params.slave_ports[n];
420 retval = reta_set(port->port_id, (i + 1) % RXTX_QUEUE_COUNT,
421 port->dev_info.reta_size);
422 TEST_ASSERT_SUCCESS(retval, "Cannot set slaves RETA");
425 TEST_ASSERT_SUCCESS(reta_set(test_params.bond_port_id,
426 i % RXTX_QUEUE_COUNT, test_params.bond_dev_info.reta_size),
427 "Cannot set bonded port RETA");
431 FOR_EACH_PORT(n, port) {
432 port = &test_params.slave_ports[n];
434 slave_reta_fetch(port);
435 TEST_ASSERT(reta_check_synced(port) == 1, "RETAs inconsistent");
443 * Test propagation logic, when RX_RSS mq_mode is turned on for bonding port
449 * Configure bonding port in RSS mq mode
453 TEST_ASSERT_SUCCESS(configure_ethdev(test_params.bond_port_id,
454 &rss_pmd_conf, 0), "Failed to configure bonding device\n");
456 ret = rte_eth_dev_info_get(test_params.bond_port_id,
457 &test_params.bond_dev_info);
458 TEST_ASSERT((ret == 0),
459 "Error during getting device (port %u) info: %s\n",
460 test_params.bond_port_id, strerror(-ret));
462 TEST_ASSERT_SUCCESS(bond_slaves(), "Bonding slaves failed");
464 TEST_ASSERT_SUCCESS(rte_eth_dev_start(test_params.bond_port_id),
465 "Failed to start bonding port (%d).", test_params.bond_port_id);
467 TEST_ASSERT_SUCCESS(test_propagate(), "Propagation test failed");
469 TEST_ASSERT(slave_remove_and_add() == 1, "New slave should be synced");
471 remove_slaves_and_stop_bonded_device();
477 * Test propagation logic, when RX_RSS mq_mode is turned off for bonding port
484 TEST_ASSERT_SUCCESS(configure_ethdev(test_params.bond_port_id,
485 &default_pmd_conf, 0), "Failed to configure bonding device\n");
487 ret = rte_eth_dev_info_get(test_params.bond_port_id,
488 &test_params.bond_dev_info);
489 TEST_ASSERT((ret == 0),
490 "Error during getting device (port %u) info: %s\n",
491 test_params.bond_port_id, strerror(-ret));
493 TEST_ASSERT_SUCCESS(bond_slaves(), "Bonding slaves failed");
495 TEST_ASSERT_SUCCESS(rte_eth_dev_start(test_params.bond_port_id),
496 "Failed to start bonding port (%d).", test_params.bond_port_id);
498 TEST_ASSERT_SUCCESS(test_propagate(), "Propagation test failed");
500 TEST_ASSERT(slave_remove_and_add() == 0, "New slave shouldn't be synced");
502 remove_slaves_and_stop_bonded_device();
514 struct slave_conf *port;
515 struct rte_ether_addr mac_addr = { .addr_bytes = {0} };
517 if (test_params.mbuf_pool == NULL) {
519 test_params.mbuf_pool = rte_pktmbuf_pool_create(
520 "RSS_MBUF_POOL", NUM_MBUFS * SLAVE_COUNT,
521 MBUF_CACHE_SIZE, 0, MBUF_SIZE, rte_socket_id());
523 TEST_ASSERT(test_params.mbuf_pool != NULL,
524 "rte_pktmbuf_pool_create failed\n");
527 /* Create / initialize ring eth devs. */
528 FOR_EACH_PORT(n, port) {
529 port = &test_params.slave_ports[n];
531 port_id = rte_eth_dev_count_avail();
532 snprintf(name, sizeof(name), SLAVE_DEV_NAME_FMT, port_id);
534 retval = rte_vdev_init(name, "size=64,copy=0");
535 TEST_ASSERT_SUCCESS(retval, "Failed to create null device '%s'\n",
538 port->port_id = port_id;
540 port->rss_conf.rss_key = port->rss_key;
541 port->rss_conf.rss_key_len = 40;
543 retval = configure_ethdev(port->port_id, &default_pmd_conf, 0);
544 TEST_ASSERT_SUCCESS(retval, "Failed to configure virtual ethdev %s\n",
547 /* assign a non-zero MAC */
548 mac_addr.addr_bytes[5] = 0x10 + port->port_id;
549 rte_eth_dev_default_mac_addr_set(port->port_id, &mac_addr);
551 rte_eth_dev_info_get(port->port_id, &port->dev_info);
552 retval = rte_eth_dev_info_get(port->port_id, &port->dev_info);
553 TEST_ASSERT((retval == 0),
554 "Error during getting device (port %u) info: %s\n",
555 test_params.bond_port_id, strerror(-retval));
558 if (test_params.bond_port_id == INVALID_PORT_ID) {
559 retval = rte_eth_bond_create(BONDED_DEV_NAME, 0, rte_socket_id());
561 TEST_ASSERT(retval >= 0, "Failed to create bonded ethdev %s",
564 test_params.bond_port_id = retval;
566 TEST_ASSERT_SUCCESS(configure_ethdev(test_params.bond_port_id,
567 &default_pmd_conf, 0), "Failed to configure bonding device\n");
569 retval = rte_eth_dev_info_get(test_params.bond_port_id,
570 &test_params.bond_dev_info);
571 TEST_ASSERT((retval == 0),
572 "Error during getting device (port %u) info: %s\n",
573 test_params.bond_port_id, strerror(-retval));
580 testsuite_teardown(void)
582 struct slave_conf *port;
586 * Any cleanup/reset state is done when particular test is
589 rte_eth_dev_stop(test_params.bond_port_id);
591 FOR_EACH_PORT(i, port)
592 rte_eth_dev_stop(port->port_id);
596 check_environment(void)
602 test_rssconf_executor(int (*test_func)(void))
606 /* Check if environment is clean. Fail to launch a test if there was
607 * a critical error before that prevented to reset environment. */
608 TEST_ASSERT_SUCCESS(check_environment(),
609 "Refusing to launch test in dirty environment.");
611 RTE_VERIFY(test_func != NULL);
612 test_result = (*test_func)();
614 /* If test succeed check if environment wast left in good condition. */
615 if (test_result == TEST_SUCCESS)
616 test_result = check_environment();
618 /* Reset environment in case test failed to do that. */
619 if (test_result != TEST_SUCCESS) {
620 TEST_ASSERT_SUCCESS(remove_slaves_and_stop_bonded_device(),
621 "Failed to stop bonded device");
628 test_setup_wrapper(void)
630 return test_rssconf_executor(&test_setup);
634 test_rss_wrapper(void)
636 return test_rssconf_executor(&test_rss);
640 test_rss_lazy_wrapper(void)
642 return test_rssconf_executor(&test_rss_lazy);
645 static struct unit_test_suite link_bonding_rssconf_test_suite = {
646 .suite_name = "RSS Dynamic Configuration for Bonding Unit Test Suite",
647 .teardown = testsuite_teardown,
649 TEST_CASE_NAMED("test_setup", test_setup_wrapper),
650 TEST_CASE_NAMED("test_rss", test_rss_wrapper),
651 TEST_CASE_NAMED("test_rss_lazy", test_rss_lazy_wrapper),
658 test_link_bonding_rssconf(void)
660 return unit_test_suite_runner(&link_bonding_rssconf_test_suite);
663 REGISTER_TEST_COMMAND(link_bonding_rssconf_autotest, test_link_bonding_rssconf);