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