mbuf: fix performance with 128-byte cache line
[dpdk.git] / app / test / test_link_bonding_mode4.c
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
5  *   All rights reserved.
6  *
7  *   Redistribution and use in source and binary forms, with or without
8  *   modification, are permitted provided that the following conditions
9  *   are met:
10  *
11  *     * Redistributions of source code must retain the above copyright
12  *       notice, this list of conditions and the following disclaimer.
13  *     * Redistributions in binary form must reproduce the above copyright
14  *       notice, this list of conditions and the following disclaimer in
15  *       the documentation and/or other materials provided with the
16  *       distribution.
17  *     * Neither the name of Intel Corporation nor the names of its
18  *       contributors may be used to endorse or promote products derived
19  *       from this software without specific prior written permission.
20  *
21  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33
34 #include <string.h>
35 #include <stdarg.h>
36 #include <stdio.h>
37 #include <stdlib.h>
38 #include <stdint.h>
39 #include <inttypes.h>
40 #include <errno.h>
41 #include <rte_cycles.h>
42 #include <sys/queue.h>
43
44 #include <rte_byteorder.h>
45 #include <rte_common.h>
46 #include <rte_debug.h>
47 #include <rte_ethdev.h>
48 #include <rte_log.h>
49 #include <rte_lcore.h>
50 #include <rte_memory.h>
51
52 #include <rte_string_fns.h>
53
54 #include <rte_eth_ring.h>
55 #include <rte_errno.h>
56 #include <rte_eth_bond.h>
57 #include <rte_eth_bond_8023ad.h>
58
59 #include "packet_burst_generator.h"
60
61 #include "test.h"
62
63 #define SLAVE_COUNT (4)
64
65 #define RX_RING_SIZE 128
66 #define TX_RING_SIZE 512
67
68 #define MBUF_CACHE_SIZE         (250)
69 #define BURST_SIZE              (32)
70
71 #define TEST_RX_DESC_MAX        (2048)
72 #define TEST_TX_DESC_MAX        (2048)
73 #define MAX_PKT_BURST           (32)
74 #define DEF_PKT_BURST           (16)
75
76 #define BONDED_DEV_NAME         ("unit_test_mode4_bond_dev")
77
78 #define SLAVE_DEV_NAME_FMT      ("unit_test_mode4_slave_%d")
79 #define SLAVE_RX_QUEUE_FMT      ("unit_test_mode4_slave_%d_rx")
80 #define SLAVE_TX_QUEUE_FMT      ("unit_test_mode4_slave_%d_tx")
81
82 #define INVALID_SOCKET_ID       (-1)
83 #define INVALID_PORT_ID         (0xFF)
84 #define INVALID_BONDING_MODE    (-1)
85
86 static const struct ether_addr slave_mac_default = {
87         { 0x00, 0xFF, 0x00, 0xFF, 0x00, 0x00 }
88 };
89
90 static const struct ether_addr parnter_mac_default = {
91         { 0x22, 0xBB, 0xFF, 0xBB, 0x00, 0x00 }
92 };
93
94 static const struct ether_addr parnter_system = {
95         { 0x33, 0xFF, 0xBB, 0xFF, 0x00, 0x00 }
96 };
97
98 static const struct ether_addr slow_protocol_mac_addr = {
99         { 0x01, 0x80, 0xC2, 0x00, 0x00, 0x02 }
100 };
101
102 struct slave_conf {
103         struct rte_ring *rx_queue;
104         struct rte_ring *tx_queue;
105         uint8_t port_id;
106         uint8_t bonded : 1;
107
108         uint8_t lacp_parnter_state;
109 };
110
111 struct ether_vlan_hdr {
112         struct ether_hdr pkt_eth_hdr;
113         struct vlan_hdr vlan_hdr;
114 };
115
116 struct link_bonding_unittest_params {
117         uint8_t bonded_port_id;
118         struct slave_conf slave_ports[SLAVE_COUNT];
119
120         struct rte_mempool *mbuf_pool;
121 };
122
123 #define TEST_DEFAULT_SLAVE_COUNT     RTE_DIM(test_params.slave_ports)
124 #define TEST_RX_SLAVE_COUT           TEST_DEFAULT_SLAVE_COUNT
125 #define TEST_TX_SLAVE_COUNT          TEST_DEFAULT_SLAVE_COUNT
126 #define TEST_MARKER_SLAVE_COUT       TEST_DEFAULT_SLAVE_COUNT
127 #define TEST_EXPIRED_SLAVE_COUNT     TEST_DEFAULT_SLAVE_COUNT
128 #define TEST_PROMISC_SLAVE_COUNT     TEST_DEFAULT_SLAVE_COUNT
129
130 static struct link_bonding_unittest_params test_params  = {
131         .bonded_port_id = INVALID_PORT_ID,
132         .slave_ports = { [0 ... SLAVE_COUNT - 1] = { .port_id = INVALID_PORT_ID} },
133
134         .mbuf_pool = NULL,
135 };
136
137 static struct rte_eth_conf default_pmd_conf = {
138         .rxmode = {
139                 .mq_mode = ETH_MQ_RX_NONE,
140                 .max_rx_pkt_len = ETHER_MAX_LEN,
141                 .split_hdr_size = 0,
142                 .header_split   = 0, /**< Header Split disabled */
143                 .hw_ip_checksum = 0, /**< IP checksum offload enabled */
144                 .hw_vlan_filter = 0, /**< VLAN filtering disabled */
145                 .jumbo_frame    = 0, /**< Jumbo Frame Support disabled */
146                 .hw_strip_crc   = 0, /**< CRC stripped by hardware */
147         },
148         .txmode = {
149                 .mq_mode = ETH_MQ_TX_NONE,
150         },
151         .lpbk_mode = 0,
152 };
153
154 #define FOR_EACH(_i, _item, _array, _size) \
155         for (_i = 0, _item = &_array[0]; _i < _size && (_item = &_array[_i]); _i++)
156
157 /* Macro for iterating over every port that can be used as a slave
158  * in this test.
159  * _i variable used as an index in test_params->slave_ports
160  * _slave pointer to &test_params->slave_ports[_idx]
161  */
162 #define FOR_EACH_PORT(_i, _port) \
163         FOR_EACH(_i, _port, test_params.slave_ports, \
164                 RTE_DIM(test_params.slave_ports))
165
166 /* Macro for iterating over every port that can be used as a slave
167  * in this test and satisfy given condition.
168  *
169  * _i variable used as an index in test_params->slave_ports
170  * _slave pointer to &test_params->slave_ports[_idx]
171  * _condition condition that need to be checked
172  */
173 #define FOR_EACH_PORT_IF(_i, _port, _condition) FOR_EACH_PORT((_i), (_port)) \
174         if (!!(_condition))
175
176 /* Macro for iterating over every port that is currently a slave of a bonded
177  * device.
178  * _i variable used as an index in test_params->slave_ports
179  * _slave pointer to &test_params->slave_ports[_idx]
180  * */
181 #define FOR_EACH_SLAVE(_i, _slave) \
182         FOR_EACH_PORT_IF(_i, _slave, (_slave)->bonded != 0)
183
184 /*
185  * Returns packets from slaves TX queue.
186  * slave slave port
187  * buffer for packets
188  * size size of buffer
189  * return number of packets or negative error number
190  */
191 static int
192 slave_get_pkts(struct slave_conf *slave, struct rte_mbuf **buf, uint16_t size)
193 {
194         return rte_ring_dequeue_burst(slave->tx_queue, (void **)buf, size);
195 }
196
197 /*
198  * Injects given packets into slaves RX queue.
199  * slave slave port
200  * buffer for packets
201  * size number of packets to be injected
202  * return number of queued packets or negative error number
203  */
204 static int
205 slave_put_pkts(struct slave_conf *slave, struct rte_mbuf **buf, uint16_t size)
206 {
207         return rte_ring_enqueue_burst(slave->rx_queue, (void **)buf, size);
208 }
209
210 static uint16_t
211 bond_rx(struct rte_mbuf **buf, uint16_t size)
212 {
213         return rte_eth_rx_burst(test_params.bonded_port_id, 0, buf, size);
214 }
215
216 static uint16_t
217 bond_tx(struct rte_mbuf **buf, uint16_t size)
218 {
219         return rte_eth_tx_burst(test_params.bonded_port_id, 0, buf, size);
220 }
221
222 static void
223 free_pkts(struct rte_mbuf **pkts, uint16_t count)
224 {
225         uint16_t i;
226
227         for (i = 0; i < count; i++) {
228                 if (pkts[i] != NULL)
229                         rte_pktmbuf_free(pkts[i]);
230         }
231 }
232
233 static int
234 configure_ethdev(uint8_t port_id, uint8_t start)
235 {
236         TEST_ASSERT(rte_eth_dev_configure(port_id, 1, 1, &default_pmd_conf) == 0,
237                 "Failed to configure device %u", port_id);
238
239         TEST_ASSERT(rte_eth_rx_queue_setup(port_id, 0, RX_RING_SIZE,
240                 rte_eth_dev_socket_id(port_id), NULL, test_params.mbuf_pool) == 0,
241                 "Failed to setup rx queue.");
242
243         TEST_ASSERT(rte_eth_tx_queue_setup(port_id, 0, TX_RING_SIZE,
244                 rte_eth_dev_socket_id(port_id), NULL) == 0,
245                 "Failed to setup tx queue.");
246
247         if (start) {
248                 TEST_ASSERT(rte_eth_dev_start(port_id) == 0,
249                 "Failed to start device (%d).", port_id);
250         }
251         return 0;
252 }
253
254 static int
255 add_slave(struct slave_conf *slave, uint8_t start)
256 {
257         struct ether_addr addr, addr_check;
258
259         /* Some sanity check */
260         RTE_VERIFY(test_params.slave_ports <= slave &&
261                 slave - test_params.slave_ports < (int)RTE_DIM(test_params.slave_ports));
262         RTE_VERIFY(slave->bonded == 0);
263         RTE_VERIFY(slave->port_id != INVALID_PORT_ID);
264
265         ether_addr_copy(&slave_mac_default, &addr);
266         addr.addr_bytes[ETHER_ADDR_LEN - 1] = slave->port_id;
267
268         rte_eth_dev_mac_addr_remove(slave->port_id, &addr);
269
270         TEST_ASSERT_SUCCESS(rte_eth_dev_mac_addr_add(slave->port_id, &addr, 0),
271                 "Failed to set slave MAC address");
272
273         TEST_ASSERT_SUCCESS(rte_eth_bond_slave_add(test_params.bonded_port_id,
274                 slave->port_id),
275                         "Failed to add slave (idx=%u, id=%u) to bonding (id=%u)",
276                         (uint8_t)(slave - test_params.slave_ports), slave->port_id,
277                         test_params.bonded_port_id);
278
279         slave->bonded = 1;
280         if (start) {
281                 TEST_ASSERT_SUCCESS(rte_eth_dev_start(slave->port_id),
282                         "Failed to start slave %u", slave->port_id);
283         }
284
285         rte_eth_macaddr_get(slave->port_id, &addr_check);
286         TEST_ASSERT_EQUAL(is_same_ether_addr(&addr, &addr_check), 1,
287                         "Slave MAC address is not as expected");
288
289         RTE_VERIFY(slave->lacp_parnter_state == 0);
290         return 0;
291 }
292
293 static int
294 remove_slave(struct slave_conf *slave)
295 {
296         ptrdiff_t slave_idx = slave - test_params.slave_ports;
297
298         RTE_VERIFY(test_params.slave_ports <= slave &&
299                 slave_idx < (ptrdiff_t)RTE_DIM(test_params.slave_ports));
300
301         RTE_VERIFY(slave->bonded == 1);
302         RTE_VERIFY(slave->port_id != INVALID_PORT_ID);
303
304         TEST_ASSERT_EQUAL(rte_ring_count(slave->rx_queue), 0,
305                 "Slave %u tx queue not empty while removing from bonding.",
306                 slave->port_id);
307
308         TEST_ASSERT_EQUAL(rte_ring_count(slave->rx_queue), 0,
309                 "Slave %u tx queue not empty while removing from bonding.",
310                 slave->port_id);
311
312         TEST_ASSERT_EQUAL(rte_eth_bond_slave_remove(test_params.bonded_port_id,
313                         slave->port_id), 0,
314                         "Failed to remove slave (idx=%u, id=%u) from bonding (id=%u)",
315                         (uint8_t)slave_idx, slave->port_id,
316                         test_params.bonded_port_id);
317
318         slave->bonded = 0;
319         slave->lacp_parnter_state = 0;
320         return 0;
321 }
322
323 static int
324 initialize_bonded_device_with_slaves(uint8_t slave_count, uint8_t start)
325 {
326         uint8_t i;
327
328         RTE_VERIFY(test_params.bonded_port_id != INVALID_PORT_ID);
329
330         for (i = 0; i < slave_count; i++) {
331                 TEST_ASSERT_SUCCESS(add_slave(&test_params.slave_ports[i], 1),
332                         "Failed to add port %u to bonded device.\n",
333                         test_params.slave_ports[i].port_id);
334         }
335
336         /* Reset mode 4 configuration */
337         rte_eth_bond_8023ad_setup(test_params.bonded_port_id, NULL);
338         rte_eth_promiscuous_disable(test_params.bonded_port_id);
339
340         if (start)
341                 TEST_ASSERT_SUCCESS(rte_eth_dev_start(test_params.bonded_port_id),
342                         "Failed to start bonded device");
343
344         return TEST_SUCCESS;
345 }
346
347 static int
348 remove_slaves_and_stop_bonded_device(void)
349 {
350         struct slave_conf *slave;
351         int retval;
352         uint8_t slaves[RTE_MAX_ETHPORTS];
353         uint8_t i;
354
355         rte_eth_dev_stop(test_params.bonded_port_id);
356
357         FOR_EACH_SLAVE(i, slave)
358                 remove_slave(slave);
359
360         retval = rte_eth_bond_slaves_get(test_params.bonded_port_id, slaves,
361                 RTE_DIM(slaves));
362
363         TEST_ASSERT_EQUAL(retval, 0,
364                 "Expected bonded device %u have 0 slaves but returned %d.",
365                         test_params.bonded_port_id, retval);
366
367         FOR_EACH_PORT(i, slave) {
368                 rte_eth_dev_stop(slave->port_id);
369
370                 TEST_ASSERT(slave->bonded == 0,
371                         "Port id=%u is still marked as enslaved.", slave->port_id);
372         }
373
374         return TEST_SUCCESS;
375 }
376
377 static int
378 test_setup(void)
379 {
380         int retval, nb_mbuf_per_pool;
381         char name[RTE_ETH_NAME_MAX_LEN];
382         struct slave_conf *port;
383         const uint8_t socket_id = rte_socket_id();
384         uint8_t i;
385
386         if (test_params.mbuf_pool == NULL) {
387                 nb_mbuf_per_pool = TEST_RX_DESC_MAX + DEF_PKT_BURST +
388                                         TEST_TX_DESC_MAX + MAX_PKT_BURST;
389                 test_params.mbuf_pool = rte_pktmbuf_pool_create("TEST_MODE4",
390                         nb_mbuf_per_pool, MBUF_CACHE_SIZE, 0,
391                         RTE_MBUF_DEFAULT_BUF_SIZE, socket_id);
392
393                 TEST_ASSERT(test_params.mbuf_pool != NULL,
394                         "rte_mempool_create failed\n");
395         }
396
397         /* Create / initialize ring eth devs. */
398         FOR_EACH_PORT(i, port) {
399                 port = &test_params.slave_ports[i];
400
401                 if (port->rx_queue == NULL) {
402                         retval = snprintf(name, RTE_DIM(name), SLAVE_RX_QUEUE_FMT, i);
403                         TEST_ASSERT(retval <= (int)RTE_DIM(name) - 1, "Name too long");
404                         port->rx_queue = rte_ring_create(name, RX_RING_SIZE, socket_id, 0);
405                         TEST_ASSERT(port->rx_queue != NULL,
406                                 "Failed to allocate rx ring '%s': %s", name,
407                                 rte_strerror(rte_errno));
408                 }
409
410                 if (port->tx_queue == NULL) {
411                         retval = snprintf(name, RTE_DIM(name), SLAVE_TX_QUEUE_FMT, i);
412                         TEST_ASSERT(retval <= (int)RTE_DIM(name) - 1, "Name too long");
413                         port->tx_queue = rte_ring_create(name, TX_RING_SIZE, socket_id, 0);
414                         TEST_ASSERT_NOT_NULL(port->tx_queue,
415                                 "Failed to allocate tx ring '%s': %s", name,
416                                 rte_strerror(rte_errno));
417                 }
418
419                 if (port->port_id == INVALID_PORT_ID) {
420                         retval = snprintf(name, RTE_DIM(name), SLAVE_DEV_NAME_FMT, i);
421                         TEST_ASSERT(retval < (int)RTE_DIM(name) - 1, "Name too long");
422                         retval = rte_eth_from_rings(name, &port->rx_queue, 1,
423                                         &port->tx_queue, 1, socket_id);
424                         TEST_ASSERT(retval >= 0,
425                                 "Failed to create ring ethdev '%s'\n", name);
426
427                         port->port_id = rte_eth_dev_count() - 1;
428                 }
429
430                 retval = configure_ethdev(port->port_id, 1);
431                 TEST_ASSERT_SUCCESS(retval, "Failed to configure virtual ethdev %s\n",
432                         name);
433         }
434
435         if (test_params.bonded_port_id == INVALID_PORT_ID) {
436                 retval = rte_eth_bond_create(BONDED_DEV_NAME, BONDING_MODE_8023AD,
437                                 socket_id);
438
439                 TEST_ASSERT(retval >= 0, "Failed to create bonded ethdev %s",
440                                 BONDED_DEV_NAME);
441
442                 test_params.bonded_port_id = retval;
443                 TEST_ASSERT_SUCCESS(configure_ethdev(test_params.bonded_port_id, 0),
444                                 "Failed to configure bonded ethdev %s", BONDED_DEV_NAME);
445         } else if (rte_eth_bond_mode_get(test_params.bonded_port_id) !=
446                         BONDING_MODE_8023AD) {
447                 TEST_ASSERT(rte_eth_bond_mode_set(test_params.bonded_port_id,
448                         BONDING_MODE_8023AD) == 0,
449                         "Failed to set ethdev %d to mode %d",
450                         test_params.bonded_port_id, BONDING_MODE_8023AD);
451         }
452
453         return 0;
454 }
455
456 static void
457 testsuite_teardown(void)
458 {
459         struct slave_conf *port;
460         uint8_t i;
461
462         /* Only stop ports.
463          * Any cleanup/reset state is done when particular test is
464          * started. */
465
466         rte_eth_dev_stop(test_params.bonded_port_id);
467
468         FOR_EACH_PORT(i, port)
469                 rte_eth_dev_stop(port->port_id);
470 }
471
472 /*
473  * Check if given LACP packet. If it is, make make replay packet to force
474  * COLLECTING state.
475  * return 0 when pkt is LACP frame, 1 if it is not slow frame, 2 if it is slow
476  * frame but not LACP
477  */
478 static int
479 make_lacp_reply(struct slave_conf *slave, struct rte_mbuf *pkt)
480 {
481         struct ether_hdr *hdr;
482         struct slow_protocol_frame *slow_hdr;
483         struct lacpdu *lacp;
484
485         /* look for LACP */
486         hdr = rte_pktmbuf_mtod(pkt, struct ether_hdr *);
487         if (hdr->ether_type != rte_cpu_to_be_16(ETHER_TYPE_SLOW))
488                 return 1;
489
490         slow_hdr = rte_pktmbuf_mtod(pkt, struct slow_protocol_frame *);
491         /* ignore packets of other types */
492         if (slow_hdr->slow_protocol.subtype != SLOW_SUBTYPE_LACP)
493                 return 2;
494
495         slow_hdr = rte_pktmbuf_mtod(pkt, struct slow_protocol_frame *);
496
497         /* Change source address to partner address */
498         ether_addr_copy(&parnter_mac_default, &slow_hdr->eth_hdr.s_addr);
499         slow_hdr->eth_hdr.s_addr.addr_bytes[ETHER_ADDR_LEN - 1] = slave->port_id;
500
501         lacp = (struct lacpdu *) &slow_hdr->slow_protocol;
502         /* Save last received state */
503         slave->lacp_parnter_state = lacp->actor.state;
504         /* Change it into LACP replay by matching parameters. */
505         memcpy(&lacp->partner.port_params, &lacp->actor.port_params,
506                 sizeof(struct port_params));
507
508         lacp->partner.state = lacp->actor.state;
509
510         ether_addr_copy(&parnter_system, &lacp->actor.port_params.system);
511         lacp->actor.state = STATE_LACP_ACTIVE |
512                                                 STATE_SYNCHRONIZATION |
513                                                 STATE_AGGREGATION |
514                                                 STATE_COLLECTING |
515                                                 STATE_DISTRIBUTING;
516
517         return 0;
518 }
519
520 /*
521  * Reads packets from given slave, search for LACP packet and reply them.
522  *
523  * Receives burst of packets from slave. Looks for LACP packet. Drops
524  * all other packets. Prepares response LACP and sends it back.
525  *
526  * return number of LACP received and replied, -1 on error.
527  */
528 static int
529 bond_handshake_reply(struct slave_conf *slave)
530 {
531         int retval;
532         struct rte_mbuf *rx_buf[MAX_PKT_BURST];
533         struct rte_mbuf *lacp_tx_buf[MAX_PKT_BURST];
534         uint16_t lacp_tx_buf_cnt = 0, i;
535
536         retval = slave_get_pkts(slave, rx_buf, RTE_DIM(rx_buf));
537         TEST_ASSERT(retval >= 0, "Getting slave %u packets failed.",
538                         slave->port_id);
539
540         for (i = 0; i < (uint16_t)retval; i++) {
541                 if (make_lacp_reply(slave, rx_buf[i]) == 0) {
542                         /* reply with actor's LACP */
543                         lacp_tx_buf[lacp_tx_buf_cnt++] = rx_buf[i];
544                 } else
545                         rte_pktmbuf_free(rx_buf[i]);
546         }
547
548         if (lacp_tx_buf_cnt == 0)
549                 return 0;
550
551         retval = slave_put_pkts(slave, lacp_tx_buf, lacp_tx_buf_cnt);
552         if (retval <= lacp_tx_buf_cnt) {
553                 /* retval might be negative */
554                 for (i = RTE_MAX(0, retval); retval < lacp_tx_buf_cnt; retval++)
555                         rte_pktmbuf_free(lacp_tx_buf[i]);
556         }
557
558         TEST_ASSERT_EQUAL(retval, lacp_tx_buf_cnt,
559                 "Failed to equeue lacp packets into slave %u tx queue.",
560                 slave->port_id);
561
562         return lacp_tx_buf_cnt;
563 }
564
565 /*
566  * Function check if given slave tx queue contains packets that make mode 4
567  * handshake complete. It will drain slave queue.
568  * return 0 if handshake not completed, 1 if handshake was complete,
569  */
570 static int
571 bond_handshake_done(struct slave_conf *slave)
572 {
573         const uint8_t expected_state = STATE_LACP_ACTIVE | STATE_SYNCHRONIZATION |
574                         STATE_AGGREGATION | STATE_COLLECTING | STATE_DISTRIBUTING;
575
576         return slave->lacp_parnter_state == expected_state;
577 }
578
579 static unsigned
580 bond_get_update_timeout_ms(void)
581 {
582         struct rte_eth_bond_8023ad_conf conf;
583
584         rte_eth_bond_8023ad_conf_get(test_params.bonded_port_id, &conf);
585         return conf.update_timeout_ms;
586 }
587
588 /*
589  * Exchanges LACP packets with partner to achieve dynamic port configuration.
590  * return TEST_SUCCESS if initial handshake succeed, TEST_FAILED otherwise.
591  */
592 static int
593 bond_handshake(void)
594 {
595         struct slave_conf *slave;
596         struct rte_mbuf *buf[MAX_PKT_BURST];
597         uint16_t nb_pkts;
598         uint8_t all_slaves_done, i, j;
599         uint8_t status[RTE_DIM(test_params.slave_ports)] = { 0 };
600         const unsigned delay = bond_get_update_timeout_ms();
601
602         /* Exchange LACP frames */
603         all_slaves_done = 0;
604         for (i = 0; i < 30 && all_slaves_done == 0; ++i) {
605                 rte_delay_ms(delay);
606
607                 all_slaves_done = 1;
608                 FOR_EACH_SLAVE(j, slave) {
609                         /* If response already send, skip slave */
610                         if (status[j] != 0)
611                                 continue;
612
613                         if (bond_handshake_reply(slave) < 0) {
614                                 all_slaves_done = 0;
615                                 break;
616                         }
617
618                         status[j] = bond_handshake_done(slave);
619                         if (status[j] == 0)
620                                 all_slaves_done = 0;
621                 }
622
623                 nb_pkts = bond_tx(NULL, 0);
624                 TEST_ASSERT_EQUAL(nb_pkts, 0, "Packets transmitted unexpectedly");
625
626                 nb_pkts = bond_rx(buf, RTE_DIM(buf));
627                 free_pkts(buf, nb_pkts);
628                 TEST_ASSERT_EQUAL(nb_pkts, 0, "Packets received unexpectedly");
629         }
630         /* If response didn't send - report failure */
631         TEST_ASSERT_EQUAL(all_slaves_done, 1, "Bond handshake failed\n");
632
633         /* If flags doesn't match - report failure */
634         return all_slaves_done = 1 ? TEST_SUCCESS : TEST_FAILED;
635 }
636
637 #define TEST_LACP_SLAVE_COUT RTE_DIM(test_params.slave_ports)
638 static int
639 test_mode4_lacp(void)
640 {
641         int retval;
642
643         retval = initialize_bonded_device_with_slaves(TEST_LACP_SLAVE_COUT, 1);
644         TEST_ASSERT_SUCCESS(retval, "Failed to initialize bonded device");
645
646         /* Test LACP handshake function */
647         retval = bond_handshake();
648         TEST_ASSERT_SUCCESS(retval, "Initial handshake failed");
649
650         retval = remove_slaves_and_stop_bonded_device();
651         TEST_ASSERT_SUCCESS(retval, "Test cleanup failed.");
652
653         return TEST_SUCCESS;
654 }
655
656 static int
657 generate_packets(struct ether_addr *src_mac,
658         struct ether_addr *dst_mac, uint16_t count, struct rte_mbuf **buf)
659 {
660         uint16_t pktlen = PACKET_BURST_GEN_PKT_LEN;
661         uint8_t vlan_enable = 0;
662         uint16_t vlan_id = 0;
663         uint8_t ip4_type = 1; /* 0 - ipv6 */
664
665         uint16_t src_port = 10, dst_port = 20;
666
667         uint32_t ip_src[4] = { [0 ... 2] = 0xDEADBEEF, [3] = IPv4(192, 168, 0, 1) };
668         uint32_t ip_dst[4] = { [0 ... 2] = 0xFEEDFACE, [3] = IPv4(192, 168, 0, 2) };
669
670         struct ether_hdr pkt_eth_hdr;
671         struct udp_hdr pkt_udp_hdr;
672         union {
673                 struct ipv4_hdr v4;
674                 struct ipv6_hdr v6;
675         } pkt_ip_hdr;
676
677         int retval;
678
679         initialize_eth_header(&pkt_eth_hdr, src_mac, dst_mac, ip4_type,
680                         vlan_enable, vlan_id);
681
682         if (ip4_type)
683                 initialize_ipv4_header(&pkt_ip_hdr.v4, ip_src[3], ip_dst[3], pktlen);
684         else
685                 initialize_ipv6_header(&pkt_ip_hdr.v6, (uint8_t *)ip_src,
686                         (uint8_t *)&ip_dst, pktlen);
687
688         initialize_udp_header(&pkt_udp_hdr, src_port, dst_port, 16);
689
690         retval = generate_packet_burst(test_params.mbuf_pool, buf,
691                         &pkt_eth_hdr, vlan_enable, &pkt_ip_hdr, 1, &pkt_udp_hdr,
692                         count, pktlen, 1);
693
694         if (retval > 0 && retval != count)
695                 free_pkts(&buf[count - retval], retval);
696
697         TEST_ASSERT_EQUAL(retval, count, "Failed to generate %u packets",
698                 count);
699
700         return count;
701 }
702
703 static int
704 generate_and_put_packets(struct slave_conf *slave, struct ether_addr *src_mac,
705                 struct ether_addr *dst_mac, uint16_t count)
706 {
707         struct rte_mbuf *pkts[MAX_PKT_BURST];
708         int retval;
709
710         retval = generate_packets(src_mac, dst_mac, count, pkts);
711         if (retval != (int)count)
712                 return retval;
713
714         retval = slave_put_pkts(slave, pkts, count);
715         if (retval > 0 && retval != count)
716                 free_pkts(&pkts[retval], count - retval);
717
718         TEST_ASSERT_EQUAL(retval, count,
719                 "Failed to enqueue packets into slave %u RX queue", slave->port_id);
720
721         return TEST_SUCCESS;
722 }
723
724 static int
725 test_mode4_rx(void)
726 {
727         struct slave_conf *slave;
728         uint16_t i, j;
729
730         uint16_t expected_pkts_cnt;
731         struct rte_mbuf *pkts[MAX_PKT_BURST];
732         int retval;
733         unsigned delay;
734
735         struct ether_hdr *hdr;
736
737         struct ether_addr src_mac = { { 0x00, 0xFF, 0x00, 0xFF, 0x00, 0x00 } };
738         struct ether_addr dst_mac;
739         struct ether_addr bonded_mac;
740
741         retval = initialize_bonded_device_with_slaves(TEST_PROMISC_SLAVE_COUNT, 1);
742         TEST_ASSERT_SUCCESS(retval, "Failed to initialize bonded device");
743
744         retval = bond_handshake();
745         TEST_ASSERT_SUCCESS(retval, "Initial handshake failed");
746
747         rte_eth_macaddr_get(test_params.bonded_port_id, &bonded_mac);
748         ether_addr_copy(&bonded_mac, &dst_mac);
749
750         /* Assert that dst address is not bonding address */
751         dst_mac.addr_bytes[0]++;
752
753         /* First try with promiscuous mode enabled.
754          * Add 2 packets to each slave. First with bonding MAC address, second with
755          * different. Check if we received all of them. */
756         rte_eth_promiscuous_enable(test_params.bonded_port_id);
757
758         expected_pkts_cnt = 0;
759         FOR_EACH_SLAVE(i, slave) {
760                 retval = generate_and_put_packets(slave, &src_mac, &bonded_mac, 1);
761                 TEST_ASSERT_SUCCESS(retval, "Failed to enqueue packets to slave %u",
762                         slave->port_id);
763
764                 retval = generate_and_put_packets(slave, &src_mac, &dst_mac, 1);
765                 TEST_ASSERT_SUCCESS(retval, "Failed to enqueue packets to slave %u",
766                         slave->port_id);
767
768                 /* Expect 2 packets per slave */
769                 expected_pkts_cnt += 2;
770         }
771
772         retval = rte_eth_rx_burst(test_params.bonded_port_id, 0, pkts,
773                 RTE_DIM(pkts));
774
775         if (retval == expected_pkts_cnt) {
776                 int cnt[2] = { 0, 0 };
777
778                 for (i = 0; i < expected_pkts_cnt; i++) {
779                         hdr = rte_pktmbuf_mtod(pkts[i], struct ether_hdr *);
780                         cnt[is_same_ether_addr(&hdr->d_addr, &bonded_mac)]++;
781                 }
782
783                 free_pkts(pkts, expected_pkts_cnt);
784
785                 /* For division by 2 expected_pkts_cnt must be even */
786                 RTE_VERIFY((expected_pkts_cnt & 1) == 0);
787                 TEST_ASSERT(cnt[0] == expected_pkts_cnt / 2 &&
788                         cnt[1] == expected_pkts_cnt / 2,
789                         "Expected %u packets with the same MAC and %u with different but "
790                         "got %u with the same and %u with diffrent MAC",
791                         expected_pkts_cnt / 2, expected_pkts_cnt / 2, cnt[1], cnt[0]);
792         } else if (retval > 0)
793                 free_pkts(pkts, retval);
794
795         TEST_ASSERT_EQUAL(retval, expected_pkts_cnt,
796                 "Expected %u packets but received only %d", expected_pkts_cnt, retval);
797
798         /* Now, disable promiscuous mode. When promiscuous mode is disabled we
799          * expect to receive only packets that are directed to bonding port. */
800         rte_eth_promiscuous_disable(test_params.bonded_port_id);
801
802         expected_pkts_cnt = 0;
803         FOR_EACH_SLAVE(i, slave) {
804                 retval = generate_and_put_packets(slave, &src_mac, &bonded_mac, 1);
805                 TEST_ASSERT_SUCCESS(retval, "Failed to enqueue packets to slave %u",
806                         slave->port_id);
807
808                 retval = generate_and_put_packets(slave, &src_mac, &dst_mac, 1);
809                 TEST_ASSERT_SUCCESS(retval, "Failed to enqueue packets to slave %u",
810                         slave->port_id);
811
812                 /* Expect only one packet per slave */
813                 expected_pkts_cnt += 1;
814         }
815
816         retval = rte_eth_rx_burst(test_params.bonded_port_id, 0, pkts,
817                 RTE_DIM(pkts));
818
819         if (retval == expected_pkts_cnt) {
820                 int eq_cnt = 0;
821
822                 for (i = 0; i < expected_pkts_cnt; i++) {
823                         hdr = rte_pktmbuf_mtod(pkts[i], struct ether_hdr *);
824                         eq_cnt += is_same_ether_addr(&hdr->d_addr, &bonded_mac);
825                 }
826
827                 free_pkts(pkts, expected_pkts_cnt);
828                 TEST_ASSERT_EQUAL(eq_cnt, expected_pkts_cnt, "Packet address mismatch");
829         } else if (retval > 0)
830                 free_pkts(pkts, retval);
831
832         TEST_ASSERT_EQUAL(retval, expected_pkts_cnt,
833                 "Expected %u packets but received only %d", expected_pkts_cnt, retval);
834
835         /* Link down test: simulate link down for first slave. */
836         delay = bond_get_update_timeout_ms();
837
838         uint8_t slave_down_id = INVALID_PORT_ID;
839
840         /* Find first slave and make link down on it*/
841         FOR_EACH_SLAVE(i, slave) {
842                 rte_eth_dev_set_link_down(slave->port_id);
843                 slave_down_id = slave->port_id;
844                 break;
845         }
846
847         RTE_VERIFY(slave_down_id != INVALID_PORT_ID);
848
849         /* Give some time to rearrange bonding */
850         for (i = 0; i < 3; i++) {
851                 rte_delay_ms(delay);
852                 bond_handshake();
853         }
854
855         TEST_ASSERT_SUCCESS(bond_handshake(), "Handshake after link down failed");
856
857         /* Put packet to each slave */
858         FOR_EACH_SLAVE(i, slave) {
859                 void *pkt = NULL;
860
861                 dst_mac.addr_bytes[ETHER_ADDR_LEN - 1] = slave->port_id;
862                 retval = generate_and_put_packets(slave, &src_mac, &dst_mac, 1);
863                 TEST_ASSERT_SUCCESS(retval, "Failed to generate test packet burst.");
864
865                 src_mac.addr_bytes[ETHER_ADDR_LEN - 1] = slave->port_id;
866                 retval = generate_and_put_packets(slave, &src_mac, &bonded_mac, 1);
867                 TEST_ASSERT_SUCCESS(retval, "Failed to generate test packet burst.");
868
869                 retval = bond_rx(pkts, RTE_DIM(pkts));
870
871                 /* Clean anything */
872                 if (retval > 0)
873                         free_pkts(pkts, retval);
874
875                 while (rte_ring_dequeue(slave->rx_queue, (void **)&pkt) == 0)
876                         rte_pktmbuf_free(pkt);
877
878                 if (slave_down_id == slave->port_id)
879                         TEST_ASSERT_EQUAL(retval, 0, "Packets received unexpectedly.");
880                 else
881                         TEST_ASSERT_NOT_EQUAL(retval, 0,
882                                 "Expected to receive some packets on slave %u.",
883                                 slave->port_id);
884                 rte_eth_dev_start(slave->port_id);
885
886                 for (j = 0; j < 5; j++) {
887                         TEST_ASSERT(bond_handshake_reply(slave) >= 0,
888                                 "Handshake after link up");
889
890                         if (bond_handshake_done(slave) == 1)
891                                 break;
892                 }
893
894                 TEST_ASSERT(j < 5, "Failed to agregate slave after link up");
895         }
896
897         remove_slaves_and_stop_bonded_device();
898         return TEST_SUCCESS;
899 }
900
901 static int
902 test_mode4_tx_burst(void)
903 {
904         struct slave_conf *slave;
905         uint16_t i, j;
906
907         uint16_t exp_pkts_cnt, pkts_cnt = 0;
908         struct rte_mbuf *pkts[MAX_PKT_BURST];
909         int retval;
910         unsigned delay;
911
912         struct ether_addr dst_mac = { { 0x00, 0xFF, 0x00, 0xFF, 0x00, 0x00 } };
913         struct ether_addr bonded_mac;
914
915         retval = initialize_bonded_device_with_slaves(TEST_TX_SLAVE_COUNT, 1);
916         TEST_ASSERT_SUCCESS(retval, "Failed to initialize bonded device");
917
918         retval = bond_handshake();
919         TEST_ASSERT_SUCCESS(retval, "Initial handshake failed");
920
921         rte_eth_macaddr_get(test_params.bonded_port_id, &bonded_mac);
922
923         /* Prepare burst */
924         for (pkts_cnt = 0; pkts_cnt < RTE_DIM(pkts); pkts_cnt++) {
925                 dst_mac.addr_bytes[ETHER_ADDR_LEN - 1] = pkts_cnt;
926                 retval = generate_packets(&bonded_mac, &dst_mac, 1, &pkts[pkts_cnt]);
927
928                 if (retval != 1)
929                         free_pkts(pkts, pkts_cnt);
930
931                 TEST_ASSERT_EQUAL(retval, 1, "Failed to generate packet %u", pkts_cnt);
932         }
933         exp_pkts_cnt = pkts_cnt;
934
935         /* Transmit packets on bonded device */
936         retval = bond_tx(pkts, pkts_cnt);
937         if (retval > 0 && retval < pkts_cnt)
938                 free_pkts(&pkts[retval], pkts_cnt - retval);
939
940         TEST_ASSERT_EQUAL(retval, pkts_cnt, "TX on bonded device failed");
941
942         /* Check if packets were transmitted properly. Every slave should have
943          * at least one packet, and sum must match. Under normal operation
944          * there should be no LACP nor MARKER frames. */
945         pkts_cnt = 0;
946         FOR_EACH_SLAVE(i, slave) {
947                 uint16_t normal_cnt, slow_cnt;
948
949                 retval = slave_get_pkts(slave, pkts, RTE_DIM(pkts));
950                 normal_cnt = 0;
951                 slow_cnt = 0;
952
953                 for (j = 0; j < retval; j++) {
954                         if (make_lacp_reply(slave, pkts[j]) == 1)
955                                 normal_cnt++;
956                         else
957                                 slow_cnt++;
958                 }
959
960                 free_pkts(pkts, normal_cnt + slow_cnt);
961                 TEST_ASSERT_EQUAL(slow_cnt, 0,
962                         "slave %u unexpectedly transmitted %d SLOW packets", slave->port_id,
963                         slow_cnt);
964
965                 TEST_ASSERT_NOT_EQUAL(normal_cnt, 0,
966                         "slave %u did not transmitted any packets", slave->port_id);
967
968                 pkts_cnt += normal_cnt;
969         }
970
971         TEST_ASSERT_EQUAL(exp_pkts_cnt, pkts_cnt,
972                 "Expected %u packets but transmitted only %d", exp_pkts_cnt, pkts_cnt);
973
974         /* Link down test:
975          * simulate link down for first slave. */
976         delay = bond_get_update_timeout_ms();
977
978         uint8_t slave_down_id = INVALID_PORT_ID;
979
980         FOR_EACH_SLAVE(i, slave) {
981                 rte_eth_dev_set_link_down(slave->port_id);
982                 slave_down_id = slave->port_id;
983                 break;
984         }
985
986         RTE_VERIFY(slave_down_id != INVALID_PORT_ID);
987
988         /* Give some time to rearrange bonding. */
989         for (i = 0; i < 3; i++) {
990                 bond_handshake();
991                 rte_delay_ms(delay);
992         }
993
994         TEST_ASSERT_SUCCESS(bond_handshake(), "Handshake after link down failed");
995
996         /* Prepare burst. */
997         for (pkts_cnt = 0; pkts_cnt < RTE_DIM(pkts); pkts_cnt++) {
998                 dst_mac.addr_bytes[ETHER_ADDR_LEN - 1] = pkts_cnt;
999                 retval = generate_packets(&bonded_mac, &dst_mac, 1, &pkts[pkts_cnt]);
1000
1001                 if (retval != 1)
1002                         free_pkts(pkts, pkts_cnt);
1003
1004                 TEST_ASSERT_EQUAL(retval, 1, "Failed to generate test packet %u",
1005                         pkts_cnt);
1006         }
1007         exp_pkts_cnt = pkts_cnt;
1008
1009         /* Transmit packets on bonded device. */
1010         retval = bond_tx(pkts, pkts_cnt);
1011         if (retval > 0 && retval < pkts_cnt)
1012                 free_pkts(&pkts[retval], pkts_cnt - retval);
1013
1014         TEST_ASSERT_EQUAL(retval, pkts_cnt, "TX on bonded device failed");
1015
1016         /* Check if packets was transmitted properly. Every slave should have
1017          * at least one packet, and sum must match. Under normal operation
1018          * there should be no LACP nor MARKER frames. */
1019         pkts_cnt = 0;
1020         FOR_EACH_SLAVE(i, slave) {
1021                 uint16_t normal_cnt, slow_cnt;
1022
1023                 retval = slave_get_pkts(slave, pkts, RTE_DIM(pkts));
1024                 normal_cnt = 0;
1025                 slow_cnt = 0;
1026
1027                 for (j = 0; j < retval; j++) {
1028                         if (make_lacp_reply(slave, pkts[j]) == 1)
1029                                 normal_cnt++;
1030                         else
1031                                 slow_cnt++;
1032                 }
1033
1034                 free_pkts(pkts, normal_cnt + slow_cnt);
1035
1036                 if (slave_down_id == slave->port_id) {
1037                         TEST_ASSERT_EQUAL(normal_cnt + slow_cnt, 0,
1038                                 "slave %u enexpectedly transmitted %u packets",
1039                                 normal_cnt + slow_cnt, slave->port_id);
1040                 } else {
1041                         TEST_ASSERT_EQUAL(slow_cnt, 0,
1042                                 "slave %u unexpectedly transmitted %d SLOW packets",
1043                                 slave->port_id, slow_cnt);
1044
1045                         TEST_ASSERT_NOT_EQUAL(normal_cnt, 0,
1046                                 "slave %u did not transmitted any packets", slave->port_id);
1047                 }
1048
1049                 pkts_cnt += normal_cnt;
1050         }
1051
1052         TEST_ASSERT_EQUAL(exp_pkts_cnt, pkts_cnt,
1053                 "Expected %u packets but transmitted only %d", exp_pkts_cnt, pkts_cnt);
1054
1055         return remove_slaves_and_stop_bonded_device();
1056 }
1057
1058 static void
1059 init_marker(struct rte_mbuf *pkt, struct slave_conf *slave)
1060 {
1061         struct marker_header *marker_hdr = rte_pktmbuf_mtod(pkt,
1062                         struct marker_header *);
1063
1064         /* Copy multicast destination address */
1065         ether_addr_copy(&slow_protocol_mac_addr, &marker_hdr->eth_hdr.d_addr);
1066
1067         /* Init source address */
1068         ether_addr_copy(&parnter_mac_default, &marker_hdr->eth_hdr.s_addr);
1069         marker_hdr->eth_hdr.s_addr.addr_bytes[ETHER_ADDR_LEN-1] = slave->port_id;
1070
1071         marker_hdr->eth_hdr.ether_type = rte_cpu_to_be_16(ETHER_TYPE_SLOW);
1072
1073         marker_hdr->marker.subtype = SLOW_SUBTYPE_MARKER;
1074         marker_hdr->marker.version_number = 1;
1075         marker_hdr->marker.tlv_type_marker = MARKER_TLV_TYPE_INFO;
1076         marker_hdr->marker.info_length =
1077                         offsetof(struct marker, reserved_90) -
1078                         offsetof(struct marker, requester_port);
1079         RTE_VERIFY(marker_hdr->marker.info_length == 16);
1080         marker_hdr->marker.requester_port = slave->port_id + 1;
1081         marker_hdr->marker.tlv_type_terminator = TLV_TYPE_TERMINATOR_INFORMATION;
1082         marker_hdr->marker.terminator_length = 0;
1083 }
1084
1085 static int
1086 test_mode4_marker(void)
1087 {
1088         struct slave_conf *slave;
1089         struct rte_mbuf *pkts[MAX_PKT_BURST];
1090         struct rte_mbuf *marker_pkt;
1091         struct marker_header *marker_hdr;
1092
1093         unsigned delay;
1094         int retval;
1095         uint16_t nb_pkts;
1096         uint8_t i, j;
1097         const uint16_t ethtype_slow_be = rte_be_to_cpu_16(ETHER_TYPE_SLOW);
1098
1099         retval = initialize_bonded_device_with_slaves(TEST_MARKER_SLAVE_COUT, 1);
1100         TEST_ASSERT_SUCCESS(retval, "Failed to initialize bonded device");
1101
1102         /* Test LACP handshake function */
1103         retval = bond_handshake();
1104         TEST_ASSERT_SUCCESS(retval, "Initial handshake failed");
1105
1106         delay = bond_get_update_timeout_ms();
1107         FOR_EACH_SLAVE(i, slave) {
1108                 marker_pkt = rte_pktmbuf_alloc(test_params.mbuf_pool);
1109                 TEST_ASSERT_NOT_NULL(marker_pkt, "Failed to allocate marker packet");
1110                 init_marker(marker_pkt, slave);
1111
1112                 retval = slave_put_pkts(slave, &marker_pkt, 1);
1113                 if (retval != 1)
1114                         rte_pktmbuf_free(marker_pkt);
1115
1116                 TEST_ASSERT_EQUAL(retval, 1,
1117                         "Failed to send marker packet to slave %u", slave->port_id);
1118
1119                 for (j = 0; j < 20; ++j) {
1120                         rte_delay_ms(delay);
1121                         retval = rte_eth_rx_burst(test_params.bonded_port_id, 0, pkts,
1122                                 RTE_DIM(pkts));
1123
1124                         if (retval > 0)
1125                                 free_pkts(pkts, retval);
1126
1127                         TEST_ASSERT_EQUAL(retval, 0, "Received packets unexpectedly");
1128
1129                         retval = rte_eth_tx_burst(test_params.bonded_port_id, 0, NULL, 0);
1130                         TEST_ASSERT_EQUAL(retval, 0,
1131                                 "Requested TX of 0 packets but %d transmitted", retval);
1132
1133                         /* Check if LACP packet was send by state machines
1134                            First and only packet must be a maker response */
1135                         retval = slave_get_pkts(slave, pkts, MAX_PKT_BURST);
1136                         if (retval == 0)
1137                                 continue;
1138                         if (retval > 1)
1139                                 free_pkts(pkts, retval);
1140
1141                         TEST_ASSERT_EQUAL(retval, 1, "failed to get slave packets");
1142                         nb_pkts = retval;
1143
1144                         marker_hdr = rte_pktmbuf_mtod(pkts[0], struct marker_header *);
1145                         /* Check if it's slow packet*/
1146                         if (marker_hdr->eth_hdr.ether_type != ethtype_slow_be)
1147                                 retval = -1;
1148                         /* Check if it's marker packet */
1149                         else if (marker_hdr->marker.subtype != SLOW_SUBTYPE_MARKER)
1150                                 retval = -2;
1151                         else if (marker_hdr->marker.tlv_type_marker != MARKER_TLV_TYPE_RESP)
1152                                 retval = -3;
1153
1154                         free_pkts(pkts, nb_pkts);
1155
1156                         TEST_ASSERT_NOT_EQUAL(retval, -1, "Unexpected protocol type");
1157                         TEST_ASSERT_NOT_EQUAL(retval, -2, "Unexpected sub protocol type");
1158                         TEST_ASSERT_NOT_EQUAL(retval, -3, "Unexpected marker type");
1159                         break;
1160                 }
1161
1162                 TEST_ASSERT(j < 20, "Marker response not found");
1163         }
1164
1165         retval = remove_slaves_and_stop_bonded_device();
1166         TEST_ASSERT_SUCCESS(retval,     "Test cleanup failed.");
1167
1168         return TEST_SUCCESS;
1169 }
1170
1171 static int
1172 test_mode4_expired(void)
1173 {
1174         struct slave_conf *slave, *exp_slave = NULL;
1175         struct rte_mbuf *pkts[MAX_PKT_BURST];
1176         int retval;
1177         uint32_t old_delay;
1178
1179         uint8_t i;
1180         uint16_t j;
1181
1182         struct rte_eth_bond_8023ad_conf conf;
1183
1184         retval = initialize_bonded_device_with_slaves(TEST_EXPIRED_SLAVE_COUNT, 1);
1185         /* Set custom timeouts to make test last shorter. */
1186         rte_eth_bond_8023ad_conf_get(test_params.bonded_port_id, &conf);
1187         conf.fast_periodic_ms = 100;
1188         conf.slow_periodic_ms = 600;
1189         conf.short_timeout_ms = 300;
1190         conf.long_timeout_ms = 900;
1191         conf.aggregate_wait_timeout_ms = 200;
1192         conf.tx_period_ms = 100;
1193         old_delay = conf.update_timeout_ms;
1194         conf.update_timeout_ms = 10;
1195         rte_eth_bond_8023ad_setup(test_params.bonded_port_id, &conf);
1196
1197         /* Wait for new settings to be applied. */
1198         for (i = 0; i < old_delay/conf.update_timeout_ms * 2; i++) {
1199                 FOR_EACH_SLAVE(j, slave)
1200                         bond_handshake_reply(slave);
1201
1202                 rte_delay_ms(conf.update_timeout_ms);
1203         }
1204
1205         retval = bond_handshake();
1206         TEST_ASSERT_SUCCESS(retval, "Initial handshake failed");
1207
1208         /* Find first slave */
1209         FOR_EACH_SLAVE(i, slave) {
1210                 exp_slave = slave;
1211                 break;
1212         }
1213
1214         RTE_VERIFY(exp_slave != NULL);
1215
1216         /* When one of partners do not send or respond to LACP frame in
1217          * conf.long_timeout_ms time, internal state machines should detect this
1218          * and transit to expired state. */
1219         for (j = 0; j < conf.long_timeout_ms/conf.update_timeout_ms + 2; j++) {
1220                 rte_delay_ms(conf.update_timeout_ms);
1221
1222                 retval = bond_tx(NULL, 0);
1223                 TEST_ASSERT_EQUAL(retval, 0, "Unexpectedly received %d packets",
1224                         retval);
1225
1226                 FOR_EACH_SLAVE(i, slave) {
1227                         retval = bond_handshake_reply(slave);
1228                         TEST_ASSERT(retval >= 0, "Handshake failed");
1229
1230                         /* Remove replay for slave that supose to be expired. */
1231                         if (slave == exp_slave) {
1232                                 while (rte_ring_count(slave->rx_queue) > 0) {
1233                                         void *pkt = NULL;
1234
1235                                         rte_ring_dequeue(slave->rx_queue, &pkt);
1236                                         rte_pktmbuf_free(pkt);
1237                                 }
1238                         }
1239                 }
1240
1241                 retval = bond_rx(pkts, RTE_DIM(pkts));
1242                 if (retval > 0)
1243                         free_pkts(pkts, retval);
1244
1245                 TEST_ASSERT_EQUAL(retval, 0, "Unexpectedly received %d packets",
1246                         retval);
1247         }
1248
1249         /* After test only expected slave should be in EXPIRED state */
1250         FOR_EACH_SLAVE(i, slave) {
1251                 if (slave == exp_slave)
1252                         TEST_ASSERT(slave->lacp_parnter_state & STATE_EXPIRED,
1253                                 "Slave %u should be in expired.", slave->port_id);
1254                 else
1255                         TEST_ASSERT_EQUAL(bond_handshake_done(slave), 1,
1256                                 "Slave %u should be operational.", slave->port_id);
1257         }
1258
1259         retval = remove_slaves_and_stop_bonded_device();
1260         TEST_ASSERT_SUCCESS(retval, "Test cleanup failed.");
1261
1262         return TEST_SUCCESS;
1263 }
1264
1265 static int
1266 check_environment(void)
1267 {
1268         struct slave_conf *port;
1269         uint8_t i, env_state;
1270         uint8_t slaves[RTE_DIM(test_params.slave_ports)];
1271         int slaves_count;
1272
1273         env_state = 0;
1274         FOR_EACH_PORT(i, port) {
1275                 if (rte_ring_count(port->rx_queue) != 0)
1276                         env_state |= 0x01;
1277
1278                 if (rte_ring_count(port->tx_queue) != 0)
1279                         env_state |= 0x02;
1280
1281                 if (port->bonded != 0)
1282                         env_state |= 0x04;
1283
1284                 if (port->lacp_parnter_state != 0)
1285                         env_state |= 0x08;
1286
1287                 if (env_state != 0)
1288                         break;
1289         }
1290
1291         slaves_count = rte_eth_bond_slaves_get(test_params.bonded_port_id,
1292                         slaves, RTE_DIM(slaves));
1293
1294         if (slaves_count != 0)
1295                 env_state |= 0x10;
1296
1297         TEST_ASSERT_EQUAL(env_state, 0,
1298                 "Environment not clean (port %u):%s%s%s%s%s",
1299                 port->port_id,
1300                 env_state & 0x01 ? " slave rx queue not clean" : "",
1301                 env_state & 0x02 ? " slave tx queue not clean" : "",
1302                 env_state & 0x04 ? " port marked as enslaved" : "",
1303                 env_state & 0x80 ? " slave state is not reset" : "",
1304                 env_state & 0x10 ? " slave count not equal 0" : ".");
1305
1306
1307         return TEST_SUCCESS;
1308 }
1309
1310 static int
1311 test_mode4_executor(int (*test_func)(void))
1312 {
1313         struct slave_conf *port;
1314         int test_result;
1315         uint8_t i;
1316         void *pkt;
1317
1318         /* Check if environment is clean. Fail to launch a test if there was
1319          * a critical error before that prevented to reset environment. */
1320         TEST_ASSERT_SUCCESS(check_environment(),
1321                 "Refusing to launch test in dirty environment.");
1322
1323         RTE_VERIFY(test_func != NULL);
1324         test_result = (*test_func)();
1325
1326         /* If test succeed check if environment wast left in good condition. */
1327         if (test_result == TEST_SUCCESS)
1328                 test_result = check_environment();
1329
1330         /* Reset environment in case test failed to do that. */
1331         if (test_result != TEST_SUCCESS) {
1332                 TEST_ASSERT_SUCCESS(remove_slaves_and_stop_bonded_device(),
1333                         "Failed to stop bonded device");
1334
1335                 FOR_EACH_PORT(i, port) {
1336                         while (rte_ring_count(port->rx_queue) != 0) {
1337                                 if (rte_ring_dequeue(port->rx_queue, &pkt) == 0)
1338                                         rte_pktmbuf_free(pkt);
1339                         }
1340
1341                         while (rte_ring_count(port->tx_queue) != 0) {
1342                                 if (rte_ring_dequeue(port->tx_queue, &pkt) == 0)
1343                                         rte_pktmbuf_free(pkt);
1344                         }
1345                 }
1346         }
1347
1348         return test_result;
1349 }
1350
1351 static int
1352 test_mode4_lacp_wrapper(void)
1353 {
1354         return test_mode4_executor(&test_mode4_lacp);
1355 }
1356
1357 static int
1358 test_mode4_marker_wrapper(void)
1359 {
1360         return test_mode4_executor(&test_mode4_marker);
1361 }
1362
1363 static int
1364 test_mode4_rx_wrapper(void)
1365 {
1366         return test_mode4_executor(&test_mode4_rx);
1367 }
1368
1369 static int
1370 test_mode4_tx_burst_wrapper(void)
1371 {
1372         return test_mode4_executor(&test_mode4_tx_burst);
1373 }
1374
1375 static int
1376 test_mode4_expired_wrapper(void)
1377 {
1378         return test_mode4_executor(&test_mode4_expired);
1379 }
1380
1381 static struct unit_test_suite link_bonding_mode4_test_suite  = {
1382         .suite_name = "Link Bonding mode 4 Unit Test Suite",
1383         .setup = test_setup,
1384         .teardown = testsuite_teardown,
1385         .unit_test_cases = {
1386                 TEST_CASE_NAMED("test_mode4_lacp", test_mode4_lacp_wrapper),
1387                 TEST_CASE_NAMED("test_mode4_rx", test_mode4_rx_wrapper),
1388                 TEST_CASE_NAMED("test_mode4_tx_burst", test_mode4_tx_burst_wrapper),
1389                 TEST_CASE_NAMED("test_mode4_marker", test_mode4_marker_wrapper),
1390                 TEST_CASE_NAMED("test_mode4_expired", test_mode4_expired_wrapper),
1391
1392                 TEST_CASES_END() /**< NULL terminate unit test array */
1393         }
1394 };
1395
1396 static int
1397 test_link_bonding_mode4(void)
1398 {
1399         return unit_test_suite_runner(&link_bonding_mode4_test_suite);
1400 }
1401
1402 static struct test_command link_bonding_cmd = {
1403         .command = "link_bonding_mode4_autotest",
1404         .callback = test_link_bonding_mode4,
1405 };
1406
1407 REGISTER_TEST_COMMAND(link_bonding_cmd);