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 = ETHER_MAX_LEN,
86 .header_split = 0, /**< Header Split disabled */
87 .hw_ip_checksum = 0, /**< IP checksum offload enabled */
88 .hw_vlan_filter = 0, /**< VLAN filtering disabled */
89 .jumbo_frame = 0, /**< Jumbo Frame Support disabled */
90 .hw_strip_crc = 1, /**< CRC stripped by hardware */
93 .mq_mode = ETH_MQ_TX_NONE,
98 static struct rte_eth_conf rss_pmd_conf = {
100 .mq_mode = ETH_MQ_RX_RSS,
101 .max_rx_pkt_len = ETHER_MAX_LEN,
103 .header_split = 0, /**< Header Split disabled */
104 .hw_ip_checksum = 0, /**< IP checksum offload enabled */
105 .hw_vlan_filter = 0, /**< VLAN filtering disabled */
106 .jumbo_frame = 0, /**< Jumbo Frame Support disabled */
107 .hw_strip_crc = 1, /**< CRC stripped by hardware */
110 .mq_mode = ETH_MQ_TX_NONE,
115 .rss_hf = ETH_RSS_IPV6,
121 #define FOR_EACH(_i, _item, _array, _size) \
122 for (_i = 0, _item = &_array[0]; _i < _size && (_item = &_array[_i]); _i++)
124 /* Macro for iterating over every port that can be used as a slave
126 * _i variable used as an index in test_params->slave_ports
127 * _slave pointer to &test_params->slave_ports[_idx]
129 #define FOR_EACH_PORT(_i, _port) \
130 FOR_EACH(_i, _port, test_params.slave_ports, \
131 RTE_DIM(test_params.slave_ports))
134 configure_ethdev(uint16_t port_id, struct rte_eth_conf *eth_conf,
139 TEST_ASSERT(rte_eth_dev_configure(port_id, RXTX_QUEUE_COUNT,
140 RXTX_QUEUE_COUNT, eth_conf) == 0, "Failed to configure device %u",
143 for (rxq = 0; rxq < RXTX_QUEUE_COUNT; rxq++) {
144 TEST_ASSERT(rte_eth_rx_queue_setup(port_id, rxq, RXTX_RING_SIZE,
145 rte_eth_dev_socket_id(port_id), NULL,
146 test_params.mbuf_pool) == 0, "Failed to setup rx queue.");
149 for (txq = 0; txq < RXTX_QUEUE_COUNT; txq++) {
150 TEST_ASSERT(rte_eth_tx_queue_setup(port_id, txq, RXTX_RING_SIZE,
151 rte_eth_dev_socket_id(port_id), NULL) == 0,
152 "Failed to setup tx queue.");
156 TEST_ASSERT(rte_eth_dev_start(port_id) == 0,
157 "Failed to start device (%d).", port_id);
164 * Remove all slaves from bonding
170 struct slave_conf *port;
172 FOR_EACH_PORT(n, port) {
173 port = &test_params.slave_ports[n];
174 if (port->is_slave) {
175 TEST_ASSERT_SUCCESS(rte_eth_bond_slave_remove(
176 test_params.bond_port_id, port->port_id),
177 "Cannot remove slave %d from bonding", port->port_id);
186 remove_slaves_and_stop_bonded_device(void)
188 TEST_ASSERT_SUCCESS(remove_slaves(), "Removing slaves");
189 rte_eth_dev_stop(test_params.bond_port_id);
194 * Add all slaves to bonding
200 struct slave_conf *port;
202 FOR_EACH_PORT(n, port) {
203 port = &test_params.slave_ports[n];
204 if (!port->is_slave) {
205 TEST_ASSERT_SUCCESS(rte_eth_bond_slave_add(test_params.bond_port_id,
206 port->port_id), "Cannot attach slave %d to the bonding",
216 * Set all RETA values in port_id to value
219 reta_set(uint16_t port_id, uint8_t value, int reta_size)
221 struct rte_eth_rss_reta_entry64 reta_conf[512/RTE_RETA_GROUP_SIZE];
224 for (i = 0; i < reta_size / RTE_RETA_GROUP_SIZE; i++) {
225 /* select all fields to set */
226 reta_conf[i].mask = ~0LL;
227 for (j = 0; j < RTE_RETA_GROUP_SIZE; j++)
228 reta_conf[i].reta[j] = value;
231 return rte_eth_dev_rss_reta_update(port_id, reta_conf, reta_size);
235 * Check if slaves RETA is synchronized with bonding port. Returns 1 if slave
236 * port is synced with bonding port.
239 reta_check_synced(struct slave_conf *port)
243 for (i = 0; i < test_params.bond_dev_info.reta_size;
246 int index = i / RTE_RETA_GROUP_SIZE;
247 int shift = i % RTE_RETA_GROUP_SIZE;
249 if (port->reta_conf[index].reta[shift] !=
250 test_params.bond_reta_conf[index].reta[shift])
259 * Fetch bonding ports RETA
262 bond_reta_fetch(void) {
265 for (j = 0; j < test_params.bond_dev_info.reta_size / RTE_RETA_GROUP_SIZE;
267 test_params.bond_reta_conf[j].mask = ~0LL;
269 TEST_ASSERT_SUCCESS(rte_eth_dev_rss_reta_query(test_params.bond_port_id,
270 test_params.bond_reta_conf, test_params.bond_dev_info.reta_size),
271 "Cannot take bonding ports RSS configuration");
279 slave_reta_fetch(struct slave_conf *port) {
282 for (j = 0; j < port->dev_info.reta_size / RTE_RETA_GROUP_SIZE; j++)
283 port->reta_conf[j].mask = ~0LL;
285 TEST_ASSERT_SUCCESS(rte_eth_dev_rss_reta_query(port->port_id,
286 port->reta_conf, port->dev_info.reta_size),
287 "Cannot take bonding ports RSS configuration");
292 * Remove and add slave to check if slaves configuration is synced with
293 * the bonding ports values after adding new slave.
296 slave_remove_and_add(void)
298 struct slave_conf *port = &(test_params.slave_ports[0]);
300 /* 1. Remove first slave from bonding */
301 TEST_ASSERT_SUCCESS(rte_eth_bond_slave_remove(test_params.bond_port_id,
302 port->port_id), "Cannot remove slave #d from bonding");
304 /* 2. Change removed (ex-)slave and bonding configuration to different
307 reta_set(test_params.bond_port_id, 1, test_params.bond_dev_info.reta_size);
310 reta_set(port->port_id, 2, port->dev_info.reta_size);
311 slave_reta_fetch(port);
313 TEST_ASSERT(reta_check_synced(port) == 0,
314 "Removed slave didn't should be synchronized with bonding port");
316 /* 3. Add (ex-)slave and check if configuration changed*/
317 TEST_ASSERT_SUCCESS(rte_eth_bond_slave_add(test_params.bond_port_id,
318 port->port_id), "Cannot add slave");
321 slave_reta_fetch(port);
323 return reta_check_synced(port);
327 * Test configuration propagation over slaves.
334 struct slave_conf *port;
335 uint8_t bond_rss_key[40];
336 struct rte_eth_rss_conf bond_rss_conf;
340 uint64_t default_rss_hf = 0;
342 rte_eth_dev_info_get(test_params.bond_port_id, &test_params.bond_dev_info);
345 * Test hash function propagation
347 for (i = 0; i < sizeof(test_params.bond_dev_info.flow_type_rss_offloads)*8;
350 rss_hf = test_params.bond_dev_info.flow_type_rss_offloads & (1<<i);
352 bond_rss_conf.rss_key = NULL;
353 bond_rss_conf.rss_hf = rss_hf;
355 retval = rte_eth_dev_rss_hash_update(test_params.bond_port_id,
357 TEST_ASSERT_SUCCESS(retval, "Cannot set slaves hash function");
359 FOR_EACH_PORT(n, port) {
360 port = &test_params.slave_ports[n];
362 retval = rte_eth_dev_rss_hash_conf_get(port->port_id,
364 TEST_ASSERT_SUCCESS(retval,
365 "Cannot take slaves RSS configuration");
367 TEST_ASSERT(port->rss_conf.rss_hf == rss_hf,
368 "Hash function not propagated for slave %d",
372 default_rss_hf = rss_hf;
378 * Test key propagation
380 for (i = 1; i < 10; i++) {
382 /* Set all keys to zero */
383 FOR_EACH_PORT(n, port) {
384 port = &test_params.slave_ports[n];
385 memset(port->rss_conf.rss_key, 0, 40);
386 retval = rte_eth_dev_rss_hash_update(port->port_id,
388 TEST_ASSERT_SUCCESS(retval, "Cannot set slaves RSS keys");
391 memset(bond_rss_key, i, sizeof(bond_rss_key));
392 bond_rss_conf.rss_hf = default_rss_hf,
393 bond_rss_conf.rss_key = bond_rss_key;
394 bond_rss_conf.rss_key_len = 40;
396 retval = rte_eth_dev_rss_hash_update(test_params.bond_port_id,
398 TEST_ASSERT_SUCCESS(retval, "Cannot set bonded port RSS keys");
400 FOR_EACH_PORT(n, port) {
401 port = &test_params.slave_ports[n];
403 retval = rte_eth_dev_rss_hash_conf_get(port->port_id,
406 TEST_ASSERT_SUCCESS(retval,
407 "Cannot take slaves RSS configuration");
410 retval = memcmp(port->rss_conf.rss_key, bond_rss_key,
411 sizeof(bond_rss_key));
412 TEST_ASSERT(retval == 0, "Key value not propagated for slave %d",
418 * Test RETA propagation
420 for (i = 0; i < RXTX_QUEUE_COUNT; i++) {
422 /* Set all keys to zero */
423 FOR_EACH_PORT(n, port) {
424 port = &test_params.slave_ports[n];
425 retval = reta_set(port->port_id, (i + 1) % RXTX_QUEUE_COUNT,
426 port->dev_info.reta_size);
427 TEST_ASSERT_SUCCESS(retval, "Cannot set slaves RETA");
430 TEST_ASSERT_SUCCESS(reta_set(test_params.bond_port_id,
431 i % RXTX_QUEUE_COUNT, test_params.bond_dev_info.reta_size),
432 "Cannot set bonded port RETA");
436 FOR_EACH_PORT(n, port) {
437 port = &test_params.slave_ports[n];
439 slave_reta_fetch(port);
440 TEST_ASSERT(reta_check_synced(port) == 1, "RETAs inconsistent");
448 * Test propagation logic, when RX_RSS mq_mode is turned on for bonding port
454 * Configure bonding port in RSS mq mode
456 TEST_ASSERT_SUCCESS(configure_ethdev(test_params.bond_port_id,
457 &rss_pmd_conf, 0), "Failed to configure bonding device\n");
459 rte_eth_dev_info_get(test_params.bond_port_id, &test_params.bond_dev_info);
461 TEST_ASSERT_SUCCESS(bond_slaves(), "Bonding slaves failed");
463 TEST_ASSERT_SUCCESS(rte_eth_dev_start(test_params.bond_port_id),
464 "Failed to start bonding port (%d).", test_params.bond_port_id);
466 TEST_ASSERT_SUCCESS(test_propagate(), "Propagation test failed");
468 TEST_ASSERT(slave_remove_and_add() == 1, "New slave should be synced");
470 remove_slaves_and_stop_bonded_device();
476 * Test propagation logic, when RX_RSS mq_mode is turned off for bonding port
481 TEST_ASSERT_SUCCESS(configure_ethdev(test_params.bond_port_id,
482 &default_pmd_conf, 0), "Failed to configure bonding device\n");
484 rte_eth_dev_info_get(test_params.bond_port_id, &test_params.bond_dev_info);
486 TEST_ASSERT_SUCCESS(bond_slaves(), "Bonding slaves failed");
488 TEST_ASSERT_SUCCESS(rte_eth_dev_start(test_params.bond_port_id),
489 "Failed to start bonding port (%d).", test_params.bond_port_id);
491 TEST_ASSERT_SUCCESS(test_propagate(), "Propagation test failed");
493 TEST_ASSERT(slave_remove_and_add() == 0, "New slave shouldn't be synced");
495 remove_slaves_and_stop_bonded_device();
507 struct slave_conf *port;
509 if (test_params.mbuf_pool == NULL) {
511 test_params.mbuf_pool = rte_pktmbuf_pool_create(
512 "RSS_MBUF_POOL", NUM_MBUFS * SLAVE_COUNT,
513 MBUF_CACHE_SIZE, 0, MBUF_SIZE, rte_socket_id());
515 TEST_ASSERT(test_params.mbuf_pool != NULL,
516 "rte_pktmbuf_pool_create failed\n");
519 /* Create / initialize ring eth devs. */
520 FOR_EACH_PORT(n, port) {
521 port = &test_params.slave_ports[n];
523 port_id = rte_eth_dev_count();
524 snprintf(name, sizeof(name), SLAVE_DEV_NAME_FMT, port_id);
526 retval = rte_vdev_init(name, "size=64,copy=0");
527 TEST_ASSERT_SUCCESS(retval, "Failed to create null device '%s'\n",
530 port->port_id = port_id;
532 port->rss_conf.rss_key = port->rss_key;
533 port->rss_conf.rss_key_len = 40;
535 retval = configure_ethdev(port->port_id, &default_pmd_conf, 0);
536 TEST_ASSERT_SUCCESS(retval, "Failed to configure virtual ethdev %s\n",
539 rte_eth_dev_info_get(port->port_id, &port->dev_info);
542 if (test_params.bond_port_id == INVALID_PORT_ID) {
543 retval = rte_eth_bond_create(BONDED_DEV_NAME, 0, rte_socket_id());
545 TEST_ASSERT(retval >= 0, "Failed to create bonded ethdev %s",
548 test_params.bond_port_id = retval;
550 TEST_ASSERT_SUCCESS(configure_ethdev(test_params.bond_port_id,
551 &default_pmd_conf, 0), "Failed to configure bonding device\n");
553 rte_eth_dev_info_get(test_params.bond_port_id,
554 &test_params.bond_dev_info);
561 testsuite_teardown(void)
563 struct slave_conf *port;
567 * Any cleanup/reset state is done when particular test is
570 rte_eth_dev_stop(test_params.bond_port_id);
572 FOR_EACH_PORT(i, port)
573 rte_eth_dev_stop(port->port_id);
577 check_environment(void)
583 test_rssconf_executor(int (*test_func)(void))
587 /* Check if environment is clean. Fail to launch a test if there was
588 * a critical error before that prevented to reset environment. */
589 TEST_ASSERT_SUCCESS(check_environment(),
590 "Refusing to launch test in dirty environment.");
592 RTE_VERIFY(test_func != NULL);
593 test_result = (*test_func)();
595 /* If test succeed check if environment wast left in good condition. */
596 if (test_result == TEST_SUCCESS)
597 test_result = check_environment();
599 /* Reset environment in case test failed to do that. */
600 if (test_result != TEST_SUCCESS) {
601 TEST_ASSERT_SUCCESS(remove_slaves_and_stop_bonded_device(),
602 "Failed to stop bonded device");
609 test_setup_wrapper(void)
611 return test_rssconf_executor(&test_setup);
615 test_rss_wrapper(void)
617 return test_rssconf_executor(&test_rss);
621 test_rss_lazy_wrapper(void)
623 return test_rssconf_executor(&test_rss_lazy);
626 static struct unit_test_suite link_bonding_rssconf_test_suite = {
627 .suite_name = "RSS Dynamic Configuration for Bonding Unit Test Suite",
628 .teardown = testsuite_teardown,
630 TEST_CASE_NAMED("test_setup", test_setup_wrapper),
631 TEST_CASE_NAMED("test_rss", test_rss_wrapper),
632 TEST_CASE_NAMED("test_rss_lazy", test_rss_lazy_wrapper),
639 test_link_bonding_rssconf(void)
641 return unit_test_suite_runner(&link_bonding_rssconf_test_suite);
644 REGISTER_TEST_COMMAND(link_bonding_rssconf_autotest, test_link_bonding_rssconf);