e6e798f5b16c2ac5c8c454130e1001c98e1779db
[dpdk.git] / test / test / test_link_bonding_rssconf.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2015 Intel Corporation
3  */
4
5 #include <string.h>
6 #include <stdarg.h>
7 #include <stdio.h>
8 #include <stdlib.h>
9 #include <stdint.h>
10 #include <inttypes.h>
11 #include <errno.h>
12 #include <rte_cycles.h>
13 #include <sys/queue.h>
14
15 #include <rte_byteorder.h>
16 #include <rte_common.h>
17 #include <rte_debug.h>
18 #include <rte_ethdev.h>
19 #include <rte_log.h>
20 #include <rte_lcore.h>
21 #include <rte_memory.h>
22 #include <rte_bus_vdev.h>
23
24 #include <rte_string_fns.h>
25 #include <rte_errno.h>
26 #include <rte_eth_bond.h>
27
28 #include "test.h"
29
30 #define SLAVE_COUNT (4)
31
32 #define RXTX_RING_SIZE                  1024
33 #define RXTX_QUEUE_COUNT                4
34
35 #define BONDED_DEV_NAME         ("net_bonding_rss")
36
37 #define SLAVE_DEV_NAME_FMT      ("net_null%d")
38 #define SLAVE_RXTX_QUEUE_FMT      ("rssconf_slave%d_q%d")
39
40 #define NUM_MBUFS 8191
41 #define MBUF_SIZE (1600 + RTE_PKTMBUF_HEADROOM)
42 #define MBUF_CACHE_SIZE 250
43 #define BURST_SIZE 32
44
45 #define INVALID_SOCKET_ID       (-1)
46 #define INVALID_PORT_ID         (0xFF)
47 #define INVALID_BONDING_MODE    (-1)
48
49 struct slave_conf {
50         uint16_t port_id;
51         struct rte_eth_dev_info dev_info;
52
53         struct rte_eth_rss_conf rss_conf;
54         uint8_t rss_key[40];
55         struct rte_eth_rss_reta_entry64 reta_conf[512 / RTE_RETA_GROUP_SIZE];
56
57         uint8_t is_slave;
58         struct rte_ring *rxtx_queue[RXTX_QUEUE_COUNT];
59 };
60
61 struct link_bonding_rssconf_unittest_params {
62         uint8_t bond_port_id;
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];
66
67         struct rte_mempool *mbuf_pool;
68 };
69
70 static struct link_bonding_rssconf_unittest_params test_params  = {
71         .bond_port_id = INVALID_PORT_ID,
72         .slave_ports = {
73                 [0 ... SLAVE_COUNT - 1] = { .port_id = INVALID_PORT_ID, .is_slave = 0}
74         },
75         .mbuf_pool = NULL,
76 };
77
78 /**
79  * Default port configuration with RSS turned off
80  */
81 static struct rte_eth_conf default_pmd_conf = {
82         .rxmode = {
83                 .mq_mode = ETH_MQ_RX_NONE,
84                 .max_rx_pkt_len = ETHER_MAX_LEN,
85                 .split_hdr_size = 0,
86                 .offloads = DEV_RX_OFFLOAD_CRC_STRIP,
87         },
88         .txmode = {
89                 .mq_mode = ETH_MQ_TX_NONE,
90         },
91         .lpbk_mode = 0,
92 };
93
94 static struct rte_eth_conf rss_pmd_conf = {
95         .rxmode = {
96                 .mq_mode = ETH_MQ_RX_RSS,
97                 .max_rx_pkt_len = ETHER_MAX_LEN,
98                 .split_hdr_size = 0,
99                 .offloads = DEV_RX_OFFLOAD_CRC_STRIP,
100         },
101         .txmode = {
102                 .mq_mode = ETH_MQ_TX_NONE,
103         },
104         .rx_adv_conf = {
105                 .rss_conf = {
106                         .rss_key = NULL,
107                         .rss_hf = ETH_RSS_IPV6,
108                 },
109         },
110         .lpbk_mode = 0,
111 };
112
113 #define FOR_EACH(_i, _item, _array, _size) \
114         for (_i = 0, _item = &_array[0]; _i < _size && (_item = &_array[_i]); _i++)
115
116 /* Macro for iterating over every port that can be used as a slave
117  * in this test.
118  * _i variable used as an index in test_params->slave_ports
119  * _slave pointer to &test_params->slave_ports[_idx]
120  */
121 #define FOR_EACH_PORT(_i, _port) \
122         FOR_EACH(_i, _port, test_params.slave_ports, \
123                 RTE_DIM(test_params.slave_ports))
124
125 static int
126 configure_ethdev(uint16_t port_id, struct rte_eth_conf *eth_conf,
127                  uint8_t start)
128 {
129         int rxq, txq;
130
131         TEST_ASSERT(rte_eth_dev_configure(port_id, RXTX_QUEUE_COUNT,
132                         RXTX_QUEUE_COUNT, eth_conf) == 0, "Failed to configure device %u",
133                         port_id);
134
135         for (rxq = 0; rxq < RXTX_QUEUE_COUNT; rxq++) {
136                 TEST_ASSERT(rte_eth_rx_queue_setup(port_id, rxq, RXTX_RING_SIZE,
137                                 rte_eth_dev_socket_id(port_id), NULL,
138                                 test_params.mbuf_pool) == 0, "Failed to setup rx queue.");
139         }
140
141         for (txq = 0; txq < RXTX_QUEUE_COUNT; txq++) {
142                 TEST_ASSERT(rte_eth_tx_queue_setup(port_id, txq, RXTX_RING_SIZE,
143                                 rte_eth_dev_socket_id(port_id), NULL) == 0,
144                                 "Failed to setup tx queue.");
145         }
146
147         if (start) {
148                 TEST_ASSERT(rte_eth_dev_start(port_id) == 0,
149                 "Failed to start device (%d).", port_id);
150         }
151
152         return 0;
153 }
154
155 /**
156  * Remove all slaves from bonding
157  */
158 static int
159 remove_slaves(void)
160 {
161         unsigned n;
162         struct slave_conf *port;
163
164         FOR_EACH_PORT(n, port) {
165                 port = &test_params.slave_ports[n];
166                 if (port->is_slave) {
167                         TEST_ASSERT_SUCCESS(rte_eth_bond_slave_remove(
168                                         test_params.bond_port_id, port->port_id),
169                                         "Cannot remove slave %d from bonding", port->port_id);
170                         port->is_slave = 0;
171                 }
172         }
173
174         return 0;
175 }
176
177 static int
178 remove_slaves_and_stop_bonded_device(void)
179 {
180         TEST_ASSERT_SUCCESS(remove_slaves(), "Removing slaves");
181         rte_eth_dev_stop(test_params.bond_port_id);
182         return TEST_SUCCESS;
183 }
184
185 /**
186  * Add all slaves to bonding
187  */
188 static int
189 bond_slaves(void)
190 {
191         unsigned n;
192         struct slave_conf *port;
193
194         FOR_EACH_PORT(n, port) {
195                 port = &test_params.slave_ports[n];
196                 if (!port->is_slave) {
197                         TEST_ASSERT_SUCCESS(rte_eth_bond_slave_add(test_params.bond_port_id,
198                                         port->port_id), "Cannot attach slave %d to the bonding",
199                                         port->port_id);
200                         port->is_slave = 1;
201                 }
202         }
203
204         return 0;
205 }
206
207 /**
208  * Set all RETA values in port_id to value
209  */
210 static int
211 reta_set(uint16_t port_id, uint8_t value, int reta_size)
212 {
213         struct rte_eth_rss_reta_entry64 reta_conf[512/RTE_RETA_GROUP_SIZE];
214         int i, j;
215
216         for (i = 0; i < reta_size / RTE_RETA_GROUP_SIZE; i++) {
217                 /* select all fields to set */
218                 reta_conf[i].mask = ~0LL;
219                 for (j = 0; j < RTE_RETA_GROUP_SIZE; j++)
220                         reta_conf[i].reta[j] = value;
221         }
222
223         return rte_eth_dev_rss_reta_update(port_id, reta_conf, reta_size);
224 }
225
226 /**
227  * Check if slaves RETA is synchronized with bonding port. Returns 1 if slave
228  * port is synced with bonding port.
229  */
230 static int
231 reta_check_synced(struct slave_conf *port)
232 {
233         unsigned i;
234
235         for (i = 0; i < test_params.bond_dev_info.reta_size;
236                         i++) {
237
238                 int index = i / RTE_RETA_GROUP_SIZE;
239                 int shift = i % RTE_RETA_GROUP_SIZE;
240
241                 if (port->reta_conf[index].reta[shift] !=
242                                 test_params.bond_reta_conf[index].reta[shift])
243                         return 0;
244
245         }
246
247         return 1;
248 }
249
250 /**
251  * Fetch bonding ports RETA
252  */
253 static int
254 bond_reta_fetch(void) {
255         unsigned j;
256
257         for (j = 0; j < test_params.bond_dev_info.reta_size / RTE_RETA_GROUP_SIZE;
258                         j++)
259                 test_params.bond_reta_conf[j].mask = ~0LL;
260
261         TEST_ASSERT_SUCCESS(rte_eth_dev_rss_reta_query(test_params.bond_port_id,
262                         test_params.bond_reta_conf, test_params.bond_dev_info.reta_size),
263                         "Cannot take bonding ports RSS configuration");
264         return 0;
265 }
266
267 /**
268  * Fetch slaves RETA
269  */
270 static int
271 slave_reta_fetch(struct slave_conf *port) {
272         unsigned j;
273
274         for (j = 0; j < port->dev_info.reta_size / RTE_RETA_GROUP_SIZE; j++)
275                 port->reta_conf[j].mask = ~0LL;
276
277         TEST_ASSERT_SUCCESS(rte_eth_dev_rss_reta_query(port->port_id,
278                         port->reta_conf, port->dev_info.reta_size),
279                         "Cannot take bonding ports RSS configuration");
280         return 0;
281 }
282
283 /**
284  * Remove and add slave to check if slaves configuration is synced with
285  * the bonding ports values after adding new slave.
286  */
287 static int
288 slave_remove_and_add(void)
289 {
290         struct slave_conf *port = &(test_params.slave_ports[0]);
291
292         /* 1. Remove first slave from bonding */
293         TEST_ASSERT_SUCCESS(rte_eth_bond_slave_remove(test_params.bond_port_id,
294                         port->port_id), "Cannot remove slave #d from bonding");
295
296         /* 2. Change removed (ex-)slave and bonding configuration to different
297          *    values
298          */
299         reta_set(test_params.bond_port_id, 1, test_params.bond_dev_info.reta_size);
300         bond_reta_fetch();
301
302         reta_set(port->port_id, 2, port->dev_info.reta_size);
303         slave_reta_fetch(port);
304
305         TEST_ASSERT(reta_check_synced(port) == 0,
306                         "Removed slave didn't should be synchronized with bonding port");
307
308         /* 3. Add (ex-)slave and check if configuration changed*/
309         TEST_ASSERT_SUCCESS(rte_eth_bond_slave_add(test_params.bond_port_id,
310                         port->port_id), "Cannot add slave");
311
312         bond_reta_fetch();
313         slave_reta_fetch(port);
314
315         return reta_check_synced(port);
316 }
317
318 /**
319  * Test configuration propagation over slaves.
320  */
321 static int
322 test_propagate(void)
323 {
324         unsigned i;
325         uint8_t n;
326         struct slave_conf *port;
327         uint8_t bond_rss_key[40];
328         struct rte_eth_rss_conf bond_rss_conf;
329
330         int retval = 0;
331         uint64_t rss_hf = 0;
332         uint64_t default_rss_hf = 0;
333
334         rte_eth_dev_info_get(test_params.bond_port_id, &test_params.bond_dev_info);
335
336         /*
337          *  Test hash function propagation
338          */
339         for (i = 0; i < sizeof(test_params.bond_dev_info.flow_type_rss_offloads)*8;
340                         i++) {
341
342                 rss_hf = test_params.bond_dev_info.flow_type_rss_offloads & (1<<i);
343                 if (rss_hf) {
344                         bond_rss_conf.rss_key = NULL;
345                         bond_rss_conf.rss_hf = rss_hf;
346
347                         retval = rte_eth_dev_rss_hash_update(test_params.bond_port_id,
348                                         &bond_rss_conf);
349                         TEST_ASSERT_SUCCESS(retval, "Cannot set slaves hash function");
350
351                         FOR_EACH_PORT(n, port) {
352                                 port = &test_params.slave_ports[n];
353
354                                 retval = rte_eth_dev_rss_hash_conf_get(port->port_id,
355                                                 &port->rss_conf);
356                                 TEST_ASSERT_SUCCESS(retval,
357                                                 "Cannot take slaves RSS configuration");
358
359                                 TEST_ASSERT(port->rss_conf.rss_hf == rss_hf,
360                                                 "Hash function not propagated for slave %d",
361                                                 port->port_id);
362                         }
363
364                         default_rss_hf = rss_hf;
365                 }
366
367         }
368
369         /*
370          *  Test key propagation
371          */
372         for (i = 1; i < 10; i++) {
373
374                 /* Set all keys to zero */
375                 FOR_EACH_PORT(n, port) {
376                         port = &test_params.slave_ports[n];
377                         memset(port->rss_conf.rss_key, 0, 40);
378                         retval = rte_eth_dev_rss_hash_update(port->port_id,
379                                         &port->rss_conf);
380                         TEST_ASSERT_SUCCESS(retval, "Cannot set slaves RSS keys");
381                 }
382
383                 memset(bond_rss_key, i, sizeof(bond_rss_key));
384                 bond_rss_conf.rss_hf = default_rss_hf,
385                 bond_rss_conf.rss_key = bond_rss_key;
386                 bond_rss_conf.rss_key_len = 40;
387
388                 retval = rte_eth_dev_rss_hash_update(test_params.bond_port_id,
389                                 &bond_rss_conf);
390                 TEST_ASSERT_SUCCESS(retval, "Cannot set bonded port RSS keys");
391
392                 FOR_EACH_PORT(n, port) {
393                         port = &test_params.slave_ports[n];
394
395                         retval = rte_eth_dev_rss_hash_conf_get(port->port_id,
396                                         &(port->rss_conf));
397
398                         TEST_ASSERT_SUCCESS(retval,
399                                         "Cannot take slaves RSS configuration");
400
401                         /* compare keys */
402                         retval = memcmp(port->rss_conf.rss_key, bond_rss_key,
403                                         sizeof(bond_rss_key));
404                         TEST_ASSERT(retval == 0, "Key value not propagated for slave %d",
405                                         port->port_id);
406                 }
407         }
408
409         /*
410          *  Test RETA propagation
411          */
412         for (i = 0; i < RXTX_QUEUE_COUNT; i++) {
413
414                 /* Set all keys to zero */
415                 FOR_EACH_PORT(n, port) {
416                         port = &test_params.slave_ports[n];
417                         retval = reta_set(port->port_id, (i + 1) % RXTX_QUEUE_COUNT,
418                                         port->dev_info.reta_size);
419                         TEST_ASSERT_SUCCESS(retval, "Cannot set slaves RETA");
420                 }
421
422                 TEST_ASSERT_SUCCESS(reta_set(test_params.bond_port_id,
423                                 i % RXTX_QUEUE_COUNT, test_params.bond_dev_info.reta_size),
424                                 "Cannot set bonded port RETA");
425
426                 bond_reta_fetch();
427
428                 FOR_EACH_PORT(n, port) {
429                         port = &test_params.slave_ports[n];
430
431                         slave_reta_fetch(port);
432                         TEST_ASSERT(reta_check_synced(port) == 1, "RETAs inconsistent");
433                 }
434         }
435
436         return TEST_SUCCESS;
437 }
438
439 /**
440  * Test propagation logic, when RX_RSS mq_mode is turned on for bonding port
441  */
442 static int
443 test_rss(void)
444 {
445         /**
446          * Configure bonding port in RSS mq mode
447          */
448         TEST_ASSERT_SUCCESS(configure_ethdev(test_params.bond_port_id,
449                         &rss_pmd_conf, 0), "Failed to configure bonding device\n");
450
451         rte_eth_dev_info_get(test_params.bond_port_id, &test_params.bond_dev_info);
452
453         TEST_ASSERT_SUCCESS(bond_slaves(), "Bonding slaves failed");
454
455         TEST_ASSERT_SUCCESS(rte_eth_dev_start(test_params.bond_port_id),
456                         "Failed to start bonding port (%d).", test_params.bond_port_id);
457
458         TEST_ASSERT_SUCCESS(test_propagate(), "Propagation test failed");
459
460         TEST_ASSERT(slave_remove_and_add() == 1, "New slave should be synced");
461
462         remove_slaves_and_stop_bonded_device();
463
464         return TEST_SUCCESS;
465 }
466
467 /**
468  * Test propagation logic, when RX_RSS mq_mode is turned off for bonding port
469  */
470 static int
471 test_rss_lazy(void)
472 {
473         TEST_ASSERT_SUCCESS(configure_ethdev(test_params.bond_port_id,
474                         &default_pmd_conf, 0), "Failed to configure bonding device\n");
475
476         rte_eth_dev_info_get(test_params.bond_port_id, &test_params.bond_dev_info);
477
478         TEST_ASSERT_SUCCESS(bond_slaves(), "Bonding slaves failed");
479
480         TEST_ASSERT_SUCCESS(rte_eth_dev_start(test_params.bond_port_id),
481                         "Failed to start bonding port (%d).", test_params.bond_port_id);
482
483         TEST_ASSERT_SUCCESS(test_propagate(), "Propagation test failed");
484
485         TEST_ASSERT(slave_remove_and_add() == 0, "New slave shouldn't be synced");
486
487         remove_slaves_and_stop_bonded_device();
488
489         return TEST_SUCCESS;
490 }
491
492 static int
493 test_setup(void)
494 {
495         unsigned n;
496         int retval;
497         int port_id;
498         char name[256];
499         struct slave_conf *port;
500         struct ether_addr mac_addr = { .addr_bytes = {0} };
501
502         if (test_params.mbuf_pool == NULL) {
503
504                 test_params.mbuf_pool = rte_pktmbuf_pool_create(
505                         "RSS_MBUF_POOL", NUM_MBUFS * SLAVE_COUNT,
506                         MBUF_CACHE_SIZE, 0, MBUF_SIZE, rte_socket_id());
507
508                 TEST_ASSERT(test_params.mbuf_pool != NULL,
509                                 "rte_pktmbuf_pool_create failed\n");
510         }
511
512         /* Create / initialize ring eth devs. */
513         FOR_EACH_PORT(n, port) {
514                 port = &test_params.slave_ports[n];
515
516                 port_id = rte_eth_dev_count_avail();
517                 snprintf(name, sizeof(name), SLAVE_DEV_NAME_FMT, port_id);
518
519                 retval = rte_vdev_init(name, "size=64,copy=0");
520                 TEST_ASSERT_SUCCESS(retval, "Failed to create null device '%s'\n",
521                                 name);
522
523                 port->port_id = port_id;
524
525                 port->rss_conf.rss_key = port->rss_key;
526                 port->rss_conf.rss_key_len = 40;
527
528                 retval = configure_ethdev(port->port_id, &default_pmd_conf, 0);
529                 TEST_ASSERT_SUCCESS(retval, "Failed to configure virtual ethdev %s\n",
530                                 name);
531
532                 /* assign a non-zero MAC */
533                 mac_addr.addr_bytes[5] = 0x10 + port->port_id;
534                 rte_eth_dev_default_mac_addr_set(port->port_id, &mac_addr);
535
536                 rte_eth_dev_info_get(port->port_id, &port->dev_info);
537         }
538
539         if (test_params.bond_port_id == INVALID_PORT_ID) {
540                 retval = rte_eth_bond_create(BONDED_DEV_NAME, 0, rte_socket_id());
541
542                 TEST_ASSERT(retval >= 0, "Failed to create bonded ethdev %s",
543                                 BONDED_DEV_NAME);
544
545                 test_params.bond_port_id = retval;
546
547                 TEST_ASSERT_SUCCESS(configure_ethdev(test_params.bond_port_id,
548                                 &default_pmd_conf, 0), "Failed to configure bonding device\n");
549
550                 rte_eth_dev_info_get(test_params.bond_port_id,
551                                 &test_params.bond_dev_info);
552         }
553
554         return TEST_SUCCESS;
555 }
556
557 static void
558 testsuite_teardown(void)
559 {
560         struct slave_conf *port;
561         uint8_t i;
562
563         /* Only stop ports.
564          * Any cleanup/reset state is done when particular test is
565          * started. */
566
567         rte_eth_dev_stop(test_params.bond_port_id);
568
569         FOR_EACH_PORT(i, port)
570                 rte_eth_dev_stop(port->port_id);
571 }
572
573 static int
574 check_environment(void)
575 {
576         return TEST_SUCCESS;
577 }
578
579 static int
580 test_rssconf_executor(int (*test_func)(void))
581 {
582         int test_result;
583
584         /* Check if environment is clean. Fail to launch a test if there was
585          * a critical error before that prevented to reset environment. */
586         TEST_ASSERT_SUCCESS(check_environment(),
587                 "Refusing to launch test in dirty environment.");
588
589         RTE_VERIFY(test_func != NULL);
590         test_result = (*test_func)();
591
592         /* If test succeed check if environment wast left in good condition. */
593         if (test_result == TEST_SUCCESS)
594                 test_result = check_environment();
595
596         /* Reset environment in case test failed to do that. */
597         if (test_result != TEST_SUCCESS) {
598                 TEST_ASSERT_SUCCESS(remove_slaves_and_stop_bonded_device(),
599                         "Failed to stop bonded device");
600         }
601
602         return test_result;
603 }
604
605 static int
606 test_setup_wrapper(void)
607 {
608         return test_rssconf_executor(&test_setup);
609 }
610
611 static int
612 test_rss_wrapper(void)
613 {
614         return test_rssconf_executor(&test_rss);
615 }
616
617 static int
618 test_rss_lazy_wrapper(void)
619 {
620         return test_rssconf_executor(&test_rss_lazy);
621 }
622
623 static struct unit_test_suite link_bonding_rssconf_test_suite  = {
624         .suite_name = "RSS Dynamic Configuration for Bonding Unit Test Suite",
625         .teardown = testsuite_teardown,
626         .unit_test_cases = {
627                 TEST_CASE_NAMED("test_setup", test_setup_wrapper),
628                 TEST_CASE_NAMED("test_rss", test_rss_wrapper),
629                 TEST_CASE_NAMED("test_rss_lazy", test_rss_lazy_wrapper),
630
631                 TEST_CASES_END()
632         }
633 };
634
635 static int
636 test_link_bonding_rssconf(void)
637 {
638         return unit_test_suite_runner(&link_bonding_rssconf_test_suite);
639 }
640
641 REGISTER_TEST_COMMAND(link_bonding_rssconf_autotest, test_link_bonding_rssconf);