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