tile: fix build
[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 static uint8_t lacpdu_rx_count[RTE_MAX_ETHPORTS] = {0, };
155
156 #define FOR_EACH(_i, _item, _array, _size) \
157         for (_i = 0, _item = &_array[0]; _i < _size && (_item = &_array[_i]); _i++)
158
159 /* Macro for iterating over every port that can be used as a slave
160  * in this test.
161  * _i variable used as an index in test_params->slave_ports
162  * _slave pointer to &test_params->slave_ports[_idx]
163  */
164 #define FOR_EACH_PORT(_i, _port) \
165         FOR_EACH(_i, _port, test_params.slave_ports, \
166                 RTE_DIM(test_params.slave_ports))
167
168 /* Macro for iterating over every port that can be used as a slave
169  * in this test and satisfy given condition.
170  *
171  * _i variable used as an index in test_params->slave_ports
172  * _slave pointer to &test_params->slave_ports[_idx]
173  * _condition condition that need to be checked
174  */
175 #define FOR_EACH_PORT_IF(_i, _port, _condition) FOR_EACH_PORT((_i), (_port)) \
176         if (!!(_condition))
177
178 /* Macro for iterating over every port that is currently a slave of a bonded
179  * device.
180  * _i variable used as an index in test_params->slave_ports
181  * _slave pointer to &test_params->slave_ports[_idx]
182  * */
183 #define FOR_EACH_SLAVE(_i, _slave) \
184         FOR_EACH_PORT_IF(_i, _slave, (_slave)->bonded != 0)
185
186 /*
187  * Returns packets from slaves TX queue.
188  * slave slave port
189  * buffer for packets
190  * size size of buffer
191  * return number of packets or negative error number
192  */
193 static int
194 slave_get_pkts(struct slave_conf *slave, struct rte_mbuf **buf, uint16_t size)
195 {
196         return rte_ring_dequeue_burst(slave->tx_queue, (void **)buf, size);
197 }
198
199 /*
200  * Injects given packets into slaves RX queue.
201  * slave slave port
202  * buffer for packets
203  * size number of packets to be injected
204  * return number of queued packets or negative error number
205  */
206 static int
207 slave_put_pkts(struct slave_conf *slave, struct rte_mbuf **buf, uint16_t size)
208 {
209         return rte_ring_enqueue_burst(slave->rx_queue, (void **)buf, size);
210 }
211
212 static uint16_t
213 bond_rx(struct rte_mbuf **buf, uint16_t size)
214 {
215         return rte_eth_rx_burst(test_params.bonded_port_id, 0, buf, size);
216 }
217
218 static uint16_t
219 bond_tx(struct rte_mbuf **buf, uint16_t size)
220 {
221         return rte_eth_tx_burst(test_params.bonded_port_id, 0, buf, size);
222 }
223
224 static void
225 free_pkts(struct rte_mbuf **pkts, uint16_t count)
226 {
227         uint16_t i;
228
229         for (i = 0; i < count; i++) {
230                 if (pkts[i] != NULL)
231                         rte_pktmbuf_free(pkts[i]);
232         }
233 }
234
235 static int
236 configure_ethdev(uint8_t port_id, uint8_t start)
237 {
238         TEST_ASSERT(rte_eth_dev_configure(port_id, 1, 1, &default_pmd_conf) == 0,
239                 "Failed to configure device %u", port_id);
240
241         TEST_ASSERT(rte_eth_rx_queue_setup(port_id, 0, RX_RING_SIZE,
242                 rte_eth_dev_socket_id(port_id), NULL, test_params.mbuf_pool) == 0,
243                 "Failed to setup rx queue.");
244
245         TEST_ASSERT(rte_eth_tx_queue_setup(port_id, 0, TX_RING_SIZE,
246                 rte_eth_dev_socket_id(port_id), NULL) == 0,
247                 "Failed to setup tx queue.");
248
249         if (start) {
250                 TEST_ASSERT(rte_eth_dev_start(port_id) == 0,
251                 "Failed to start device (%d).", port_id);
252         }
253         return 0;
254 }
255
256 static int
257 add_slave(struct slave_conf *slave, uint8_t start)
258 {
259         struct ether_addr addr, addr_check;
260
261         /* Some sanity check */
262         RTE_VERIFY(test_params.slave_ports <= slave &&
263                 slave - test_params.slave_ports < (int)RTE_DIM(test_params.slave_ports));
264         RTE_VERIFY(slave->bonded == 0);
265         RTE_VERIFY(slave->port_id != INVALID_PORT_ID);
266
267         ether_addr_copy(&slave_mac_default, &addr);
268         addr.addr_bytes[ETHER_ADDR_LEN - 1] = slave->port_id;
269
270         rte_eth_dev_mac_addr_remove(slave->port_id, &addr);
271
272         TEST_ASSERT_SUCCESS(rte_eth_dev_mac_addr_add(slave->port_id, &addr, 0),
273                 "Failed to set slave MAC address");
274
275         TEST_ASSERT_SUCCESS(rte_eth_bond_slave_add(test_params.bonded_port_id,
276                 slave->port_id),
277                         "Failed to add slave (idx=%u, id=%u) to bonding (id=%u)",
278                         (uint8_t)(slave - test_params.slave_ports), slave->port_id,
279                         test_params.bonded_port_id);
280
281         slave->bonded = 1;
282         if (start) {
283                 TEST_ASSERT_SUCCESS(rte_eth_dev_start(slave->port_id),
284                         "Failed to start slave %u", slave->port_id);
285         }
286
287         rte_eth_macaddr_get(slave->port_id, &addr_check);
288         TEST_ASSERT_EQUAL(is_same_ether_addr(&addr, &addr_check), 1,
289                         "Slave MAC address is not as expected");
290
291         RTE_VERIFY(slave->lacp_parnter_state == 0);
292         return 0;
293 }
294
295 static int
296 remove_slave(struct slave_conf *slave)
297 {
298         ptrdiff_t slave_idx = slave - test_params.slave_ports;
299
300         RTE_VERIFY(test_params.slave_ports <= slave &&
301                 slave_idx < (ptrdiff_t)RTE_DIM(test_params.slave_ports));
302
303         RTE_VERIFY(slave->bonded == 1);
304         RTE_VERIFY(slave->port_id != INVALID_PORT_ID);
305
306         TEST_ASSERT_EQUAL(rte_ring_count(slave->rx_queue), 0,
307                 "Slave %u tx queue not empty while removing from bonding.",
308                 slave->port_id);
309
310         TEST_ASSERT_EQUAL(rte_ring_count(slave->rx_queue), 0,
311                 "Slave %u tx queue not empty while removing from bonding.",
312                 slave->port_id);
313
314         TEST_ASSERT_EQUAL(rte_eth_bond_slave_remove(test_params.bonded_port_id,
315                         slave->port_id), 0,
316                         "Failed to remove slave (idx=%u, id=%u) from bonding (id=%u)",
317                         (uint8_t)slave_idx, slave->port_id,
318                         test_params.bonded_port_id);
319
320         slave->bonded = 0;
321         slave->lacp_parnter_state = 0;
322         return 0;
323 }
324
325 static void
326 lacp_recv_cb(uint8_t slave_id, struct rte_mbuf *lacp_pkt)
327 {
328         struct ether_hdr *hdr;
329         struct slow_protocol_frame *slow_hdr;
330
331         RTE_VERIFY(lacp_pkt != NULL);
332
333         hdr = rte_pktmbuf_mtod(lacp_pkt, struct ether_hdr *);
334         RTE_VERIFY(hdr->ether_type == rte_cpu_to_be_16(ETHER_TYPE_SLOW));
335
336         slow_hdr = rte_pktmbuf_mtod(lacp_pkt, struct slow_protocol_frame *);
337         RTE_VERIFY(slow_hdr->slow_protocol.subtype == SLOW_SUBTYPE_LACP);
338
339         lacpdu_rx_count[slave_id]++;
340         rte_pktmbuf_free(lacp_pkt);
341 }
342
343 static int
344 initialize_bonded_device_with_slaves(uint8_t slave_count, uint8_t external_sm)
345 {
346         uint8_t i;
347
348         RTE_VERIFY(test_params.bonded_port_id != INVALID_PORT_ID);
349
350         for (i = 0; i < slave_count; i++) {
351                 TEST_ASSERT_SUCCESS(add_slave(&test_params.slave_ports[i], 1),
352                         "Failed to add port %u to bonded device.\n",
353                         test_params.slave_ports[i].port_id);
354         }
355
356         /* Reset mode 4 configuration */
357         rte_eth_bond_8023ad_setup(test_params.bonded_port_id, NULL);
358         rte_eth_promiscuous_disable(test_params.bonded_port_id);
359
360         if (external_sm) {
361                 struct rte_eth_bond_8023ad_conf conf;
362
363                 rte_eth_bond_8023ad_conf_get(test_params.bonded_port_id, &conf);
364                 conf.slowrx_cb = lacp_recv_cb;
365                 rte_eth_bond_8023ad_setup(test_params.bonded_port_id, &conf);
366
367         }
368
369         TEST_ASSERT_SUCCESS(rte_eth_dev_start(test_params.bonded_port_id),
370                 "Failed to start bonded device");
371
372         return TEST_SUCCESS;
373 }
374
375 static int
376 remove_slaves_and_stop_bonded_device(void)
377 {
378         struct slave_conf *slave;
379         int retval;
380         uint8_t slaves[RTE_MAX_ETHPORTS];
381         uint8_t i;
382
383         rte_eth_dev_stop(test_params.bonded_port_id);
384
385         FOR_EACH_SLAVE(i, slave)
386                 remove_slave(slave);
387
388         retval = rte_eth_bond_slaves_get(test_params.bonded_port_id, slaves,
389                 RTE_DIM(slaves));
390
391         TEST_ASSERT_EQUAL(retval, 0,
392                 "Expected bonded device %u have 0 slaves but returned %d.",
393                         test_params.bonded_port_id, retval);
394
395         FOR_EACH_PORT(i, slave) {
396                 rte_eth_dev_stop(slave->port_id);
397
398                 TEST_ASSERT(slave->bonded == 0,
399                         "Port id=%u is still marked as enslaved.", slave->port_id);
400         }
401
402         return TEST_SUCCESS;
403 }
404
405 static int
406 test_setup(void)
407 {
408         int retval, nb_mbuf_per_pool;
409         char name[RTE_ETH_NAME_MAX_LEN];
410         struct slave_conf *port;
411         const uint8_t socket_id = rte_socket_id();
412         uint8_t i;
413
414         if (test_params.mbuf_pool == NULL) {
415                 nb_mbuf_per_pool = TEST_RX_DESC_MAX + DEF_PKT_BURST +
416                                         TEST_TX_DESC_MAX + MAX_PKT_BURST;
417                 test_params.mbuf_pool = rte_pktmbuf_pool_create("TEST_MODE4",
418                         nb_mbuf_per_pool, MBUF_CACHE_SIZE, 0,
419                         RTE_MBUF_DEFAULT_BUF_SIZE, socket_id);
420
421                 TEST_ASSERT(test_params.mbuf_pool != NULL,
422                         "rte_mempool_create failed\n");
423         }
424
425         /* Create / initialize ring eth devs. */
426         FOR_EACH_PORT(i, port) {
427                 port = &test_params.slave_ports[i];
428
429                 if (port->rx_queue == NULL) {
430                         retval = snprintf(name, RTE_DIM(name), SLAVE_RX_QUEUE_FMT, i);
431                         TEST_ASSERT(retval <= (int)RTE_DIM(name) - 1, "Name too long");
432                         port->rx_queue = rte_ring_create(name, RX_RING_SIZE, socket_id, 0);
433                         TEST_ASSERT(port->rx_queue != NULL,
434                                 "Failed to allocate rx ring '%s': %s", name,
435                                 rte_strerror(rte_errno));
436                 }
437
438                 if (port->tx_queue == NULL) {
439                         retval = snprintf(name, RTE_DIM(name), SLAVE_TX_QUEUE_FMT, i);
440                         TEST_ASSERT(retval <= (int)RTE_DIM(name) - 1, "Name too long");
441                         port->tx_queue = rte_ring_create(name, TX_RING_SIZE, socket_id, 0);
442                         TEST_ASSERT_NOT_NULL(port->tx_queue,
443                                 "Failed to allocate tx ring '%s': %s", name,
444                                 rte_strerror(rte_errno));
445                 }
446
447                 if (port->port_id == INVALID_PORT_ID) {
448                         retval = snprintf(name, RTE_DIM(name), SLAVE_DEV_NAME_FMT, i);
449                         TEST_ASSERT(retval < (int)RTE_DIM(name) - 1, "Name too long");
450                         retval = rte_eth_from_rings(name, &port->rx_queue, 1,
451                                         &port->tx_queue, 1, socket_id);
452                         TEST_ASSERT(retval >= 0,
453                                 "Failed to create ring ethdev '%s'\n", name);
454
455                         port->port_id = rte_eth_dev_count() - 1;
456                 }
457
458                 retval = configure_ethdev(port->port_id, 1);
459                 TEST_ASSERT_SUCCESS(retval, "Failed to configure virtual ethdev %s\n",
460                         name);
461         }
462
463         if (test_params.bonded_port_id == INVALID_PORT_ID) {
464                 retval = rte_eth_bond_create(BONDED_DEV_NAME, BONDING_MODE_8023AD,
465                                 socket_id);
466
467                 TEST_ASSERT(retval >= 0, "Failed to create bonded ethdev %s",
468                                 BONDED_DEV_NAME);
469
470                 test_params.bonded_port_id = retval;
471                 TEST_ASSERT_SUCCESS(configure_ethdev(test_params.bonded_port_id, 0),
472                                 "Failed to configure bonded ethdev %s", BONDED_DEV_NAME);
473         } else if (rte_eth_bond_mode_get(test_params.bonded_port_id) !=
474                         BONDING_MODE_8023AD) {
475                 TEST_ASSERT(rte_eth_bond_mode_set(test_params.bonded_port_id,
476                         BONDING_MODE_8023AD) == 0,
477                         "Failed to set ethdev %d to mode %d",
478                         test_params.bonded_port_id, BONDING_MODE_8023AD);
479         }
480
481         return 0;
482 }
483
484 static void
485 testsuite_teardown(void)
486 {
487         struct slave_conf *port;
488         uint8_t i;
489
490         /* Only stop ports.
491          * Any cleanup/reset state is done when particular test is
492          * started. */
493
494         rte_eth_dev_stop(test_params.bonded_port_id);
495
496         FOR_EACH_PORT(i, port)
497                 rte_eth_dev_stop(port->port_id);
498 }
499
500 /*
501  * Check if given LACP packet. If it is, make make replay packet to force
502  * COLLECTING state.
503  * return 0 when pkt is LACP frame, 1 if it is not slow frame, 2 if it is slow
504  * frame but not LACP
505  */
506 static int
507 make_lacp_reply(struct slave_conf *slave, struct rte_mbuf *pkt)
508 {
509         struct ether_hdr *hdr;
510         struct slow_protocol_frame *slow_hdr;
511         struct lacpdu *lacp;
512
513         /* look for LACP */
514         hdr = rte_pktmbuf_mtod(pkt, struct ether_hdr *);
515         if (hdr->ether_type != rte_cpu_to_be_16(ETHER_TYPE_SLOW))
516                 return 1;
517
518         slow_hdr = rte_pktmbuf_mtod(pkt, struct slow_protocol_frame *);
519         /* ignore packets of other types */
520         if (slow_hdr->slow_protocol.subtype != SLOW_SUBTYPE_LACP)
521                 return 2;
522
523         slow_hdr = rte_pktmbuf_mtod(pkt, struct slow_protocol_frame *);
524
525         /* Change source address to partner address */
526         ether_addr_copy(&parnter_mac_default, &slow_hdr->eth_hdr.s_addr);
527         slow_hdr->eth_hdr.s_addr.addr_bytes[ETHER_ADDR_LEN - 1] = slave->port_id;
528
529         lacp = (struct lacpdu *) &slow_hdr->slow_protocol;
530         /* Save last received state */
531         slave->lacp_parnter_state = lacp->actor.state;
532         /* Change it into LACP replay by matching parameters. */
533         memcpy(&lacp->partner.port_params, &lacp->actor.port_params,
534                 sizeof(struct port_params));
535
536         lacp->partner.state = lacp->actor.state;
537
538         ether_addr_copy(&parnter_system, &lacp->actor.port_params.system);
539         lacp->actor.state = STATE_LACP_ACTIVE |
540                                                 STATE_SYNCHRONIZATION |
541                                                 STATE_AGGREGATION |
542                                                 STATE_COLLECTING |
543                                                 STATE_DISTRIBUTING;
544
545         return 0;
546 }
547
548 /*
549  * Reads packets from given slave, search for LACP packet and reply them.
550  *
551  * Receives burst of packets from slave. Looks for LACP packet. Drops
552  * all other packets. Prepares response LACP and sends it back.
553  *
554  * return number of LACP received and replied, -1 on error.
555  */
556 static int
557 bond_handshake_reply(struct slave_conf *slave)
558 {
559         int retval;
560         struct rte_mbuf *rx_buf[MAX_PKT_BURST];
561         struct rte_mbuf *lacp_tx_buf[MAX_PKT_BURST];
562         uint16_t lacp_tx_buf_cnt = 0, i;
563
564         retval = slave_get_pkts(slave, rx_buf, RTE_DIM(rx_buf));
565         TEST_ASSERT(retval >= 0, "Getting slave %u packets failed.",
566                         slave->port_id);
567
568         for (i = 0; i < (uint16_t)retval; i++) {
569                 if (make_lacp_reply(slave, rx_buf[i]) == 0) {
570                         /* reply with actor's LACP */
571                         lacp_tx_buf[lacp_tx_buf_cnt++] = rx_buf[i];
572                 } else
573                         rte_pktmbuf_free(rx_buf[i]);
574         }
575
576         if (lacp_tx_buf_cnt == 0)
577                 return 0;
578
579         retval = slave_put_pkts(slave, lacp_tx_buf, lacp_tx_buf_cnt);
580         if (retval <= lacp_tx_buf_cnt) {
581                 /* retval might be negative */
582                 for (i = RTE_MAX(0, retval); retval < lacp_tx_buf_cnt; retval++)
583                         rte_pktmbuf_free(lacp_tx_buf[i]);
584         }
585
586         TEST_ASSERT_EQUAL(retval, lacp_tx_buf_cnt,
587                 "Failed to equeue lacp packets into slave %u tx queue.",
588                 slave->port_id);
589
590         return lacp_tx_buf_cnt;
591 }
592
593 /*
594  * Function check if given slave tx queue contains packets that make mode 4
595  * handshake complete. It will drain slave queue.
596  * return 0 if handshake not completed, 1 if handshake was complete,
597  */
598 static int
599 bond_handshake_done(struct slave_conf *slave)
600 {
601         const uint8_t expected_state = STATE_LACP_ACTIVE | STATE_SYNCHRONIZATION |
602                         STATE_AGGREGATION | STATE_COLLECTING | STATE_DISTRIBUTING;
603
604         return slave->lacp_parnter_state == expected_state;
605 }
606
607 static unsigned
608 bond_get_update_timeout_ms(void)
609 {
610         struct rte_eth_bond_8023ad_conf conf;
611
612         rte_eth_bond_8023ad_conf_get(test_params.bonded_port_id, &conf);
613         return conf.update_timeout_ms;
614 }
615
616 /*
617  * Exchanges LACP packets with partner to achieve dynamic port configuration.
618  * return TEST_SUCCESS if initial handshake succeed, TEST_FAILED otherwise.
619  */
620 static int
621 bond_handshake(void)
622 {
623         struct slave_conf *slave;
624         struct rte_mbuf *buf[MAX_PKT_BURST];
625         uint16_t nb_pkts;
626         uint8_t all_slaves_done, i, j;
627         uint8_t status[RTE_DIM(test_params.slave_ports)] = { 0 };
628         const unsigned delay = bond_get_update_timeout_ms();
629
630         /* Exchange LACP frames */
631         all_slaves_done = 0;
632         for (i = 0; i < 30 && all_slaves_done == 0; ++i) {
633                 rte_delay_ms(delay);
634
635                 all_slaves_done = 1;
636                 FOR_EACH_SLAVE(j, slave) {
637                         /* If response already send, skip slave */
638                         if (status[j] != 0)
639                                 continue;
640
641                         if (bond_handshake_reply(slave) < 0) {
642                                 all_slaves_done = 0;
643                                 break;
644                         }
645
646                         status[j] = bond_handshake_done(slave);
647                         if (status[j] == 0)
648                                 all_slaves_done = 0;
649                 }
650
651                 nb_pkts = bond_tx(NULL, 0);
652                 TEST_ASSERT_EQUAL(nb_pkts, 0, "Packets transmitted unexpectedly");
653
654                 nb_pkts = bond_rx(buf, RTE_DIM(buf));
655                 free_pkts(buf, nb_pkts);
656                 TEST_ASSERT_EQUAL(nb_pkts, 0, "Packets received unexpectedly");
657         }
658         /* If response didn't send - report failure */
659         TEST_ASSERT_EQUAL(all_slaves_done, 1, "Bond handshake failed\n");
660
661         /* If flags doesn't match - report failure */
662         return all_slaves_done = 1 ? TEST_SUCCESS : TEST_FAILED;
663 }
664
665 #define TEST_LACP_SLAVE_COUT RTE_DIM(test_params.slave_ports)
666 static int
667 test_mode4_lacp(void)
668 {
669         int retval;
670
671         retval = initialize_bonded_device_with_slaves(TEST_LACP_SLAVE_COUT, 0);
672         TEST_ASSERT_SUCCESS(retval, "Failed to initialize bonded device");
673
674         /* Test LACP handshake function */
675         retval = bond_handshake();
676         TEST_ASSERT_SUCCESS(retval, "Initial handshake failed");
677
678         retval = remove_slaves_and_stop_bonded_device();
679         TEST_ASSERT_SUCCESS(retval, "Test cleanup failed.");
680
681         return TEST_SUCCESS;
682 }
683
684 static int
685 generate_packets(struct ether_addr *src_mac,
686         struct ether_addr *dst_mac, uint16_t count, struct rte_mbuf **buf)
687 {
688         uint16_t pktlen = PACKET_BURST_GEN_PKT_LEN;
689         uint8_t vlan_enable = 0;
690         uint16_t vlan_id = 0;
691         uint8_t ip4_type = 1; /* 0 - ipv6 */
692
693         uint16_t src_port = 10, dst_port = 20;
694
695         uint32_t ip_src[4] = { [0 ... 2] = 0xDEADBEEF, [3] = IPv4(192, 168, 0, 1) };
696         uint32_t ip_dst[4] = { [0 ... 2] = 0xFEEDFACE, [3] = IPv4(192, 168, 0, 2) };
697
698         struct ether_hdr pkt_eth_hdr;
699         struct udp_hdr pkt_udp_hdr;
700         union {
701                 struct ipv4_hdr v4;
702                 struct ipv6_hdr v6;
703         } pkt_ip_hdr;
704
705         int retval;
706
707         initialize_eth_header(&pkt_eth_hdr, src_mac, dst_mac, ip4_type,
708                         vlan_enable, vlan_id);
709
710         if (ip4_type)
711                 initialize_ipv4_header(&pkt_ip_hdr.v4, ip_src[3], ip_dst[3], pktlen);
712         else
713                 initialize_ipv6_header(&pkt_ip_hdr.v6, (uint8_t *)ip_src,
714                         (uint8_t *)&ip_dst, pktlen);
715
716         initialize_udp_header(&pkt_udp_hdr, src_port, dst_port, 16);
717
718         retval = generate_packet_burst(test_params.mbuf_pool, buf,
719                         &pkt_eth_hdr, vlan_enable, &pkt_ip_hdr, 1, &pkt_udp_hdr,
720                         count, pktlen, 1);
721
722         if (retval > 0 && retval != count)
723                 free_pkts(&buf[count - retval], retval);
724
725         TEST_ASSERT_EQUAL(retval, count, "Failed to generate %u packets",
726                 count);
727
728         return count;
729 }
730
731 static int
732 generate_and_put_packets(struct slave_conf *slave, struct ether_addr *src_mac,
733                 struct ether_addr *dst_mac, uint16_t count)
734 {
735         struct rte_mbuf *pkts[MAX_PKT_BURST];
736         int retval;
737
738         retval = generate_packets(src_mac, dst_mac, count, pkts);
739         if (retval != (int)count)
740                 return retval;
741
742         retval = slave_put_pkts(slave, pkts, count);
743         if (retval > 0 && retval != count)
744                 free_pkts(&pkts[retval], count - retval);
745
746         TEST_ASSERT_EQUAL(retval, count,
747                 "Failed to enqueue packets into slave %u RX queue", slave->port_id);
748
749         return TEST_SUCCESS;
750 }
751
752 static int
753 test_mode4_rx(void)
754 {
755         struct slave_conf *slave;
756         uint16_t i, j;
757
758         uint16_t expected_pkts_cnt;
759         struct rte_mbuf *pkts[MAX_PKT_BURST];
760         int retval;
761         unsigned delay;
762
763         struct ether_hdr *hdr;
764
765         struct ether_addr src_mac = { { 0x00, 0xFF, 0x00, 0xFF, 0x00, 0x00 } };
766         struct ether_addr dst_mac;
767         struct ether_addr bonded_mac;
768
769         retval = initialize_bonded_device_with_slaves(TEST_PROMISC_SLAVE_COUNT,
770                                                       0);
771         TEST_ASSERT_SUCCESS(retval, "Failed to initialize bonded device");
772
773         retval = bond_handshake();
774         TEST_ASSERT_SUCCESS(retval, "Initial handshake failed");
775
776         rte_eth_macaddr_get(test_params.bonded_port_id, &bonded_mac);
777         ether_addr_copy(&bonded_mac, &dst_mac);
778
779         /* Assert that dst address is not bonding address.  Do not set the
780          * least significant bit of the zero byte as this would create a
781          * multicast address.
782          */
783         dst_mac.addr_bytes[0] += 2;
784
785         /* First try with promiscuous mode enabled.
786          * Add 2 packets to each slave. First with bonding MAC address, second with
787          * different. Check if we received all of them. */
788         rte_eth_promiscuous_enable(test_params.bonded_port_id);
789
790         expected_pkts_cnt = 0;
791         FOR_EACH_SLAVE(i, slave) {
792                 retval = generate_and_put_packets(slave, &src_mac, &bonded_mac, 1);
793                 TEST_ASSERT_SUCCESS(retval, "Failed to enqueue packets to slave %u",
794                         slave->port_id);
795
796                 retval = generate_and_put_packets(slave, &src_mac, &dst_mac, 1);
797                 TEST_ASSERT_SUCCESS(retval, "Failed to enqueue packets to slave %u",
798                         slave->port_id);
799
800                 /* Expect 2 packets per slave */
801                 expected_pkts_cnt += 2;
802         }
803
804         retval = rte_eth_rx_burst(test_params.bonded_port_id, 0, pkts,
805                 RTE_DIM(pkts));
806
807         if (retval == expected_pkts_cnt) {
808                 int cnt[2] = { 0, 0 };
809
810                 for (i = 0; i < expected_pkts_cnt; i++) {
811                         hdr = rte_pktmbuf_mtod(pkts[i], struct ether_hdr *);
812                         cnt[is_same_ether_addr(&hdr->d_addr, &bonded_mac)]++;
813                 }
814
815                 free_pkts(pkts, expected_pkts_cnt);
816
817                 /* For division by 2 expected_pkts_cnt must be even */
818                 RTE_VERIFY((expected_pkts_cnt & 1) == 0);
819                 TEST_ASSERT(cnt[0] == expected_pkts_cnt / 2 &&
820                         cnt[1] == expected_pkts_cnt / 2,
821                         "Expected %u packets with the same MAC and %u with different but "
822                         "got %u with the same and %u with diffrent MAC",
823                         expected_pkts_cnt / 2, expected_pkts_cnt / 2, cnt[1], cnt[0]);
824         } else if (retval > 0)
825                 free_pkts(pkts, retval);
826
827         TEST_ASSERT_EQUAL(retval, expected_pkts_cnt,
828                 "Expected %u packets but received only %d", expected_pkts_cnt, retval);
829
830         /* Now, disable promiscuous mode. When promiscuous mode is disabled we
831          * expect to receive only packets that are directed to bonding port. */
832         rte_eth_promiscuous_disable(test_params.bonded_port_id);
833
834         expected_pkts_cnt = 0;
835         FOR_EACH_SLAVE(i, slave) {
836                 retval = generate_and_put_packets(slave, &src_mac, &bonded_mac, 1);
837                 TEST_ASSERT_SUCCESS(retval, "Failed to enqueue packets to slave %u",
838                         slave->port_id);
839
840                 retval = generate_and_put_packets(slave, &src_mac, &dst_mac, 1);
841                 TEST_ASSERT_SUCCESS(retval, "Failed to enqueue packets to slave %u",
842                         slave->port_id);
843
844                 /* Expect only one packet per slave */
845                 expected_pkts_cnt += 1;
846         }
847
848         retval = rte_eth_rx_burst(test_params.bonded_port_id, 0, pkts,
849                 RTE_DIM(pkts));
850
851         if (retval == expected_pkts_cnt) {
852                 int eq_cnt = 0;
853
854                 for (i = 0; i < expected_pkts_cnt; i++) {
855                         hdr = rte_pktmbuf_mtod(pkts[i], struct ether_hdr *);
856                         eq_cnt += is_same_ether_addr(&hdr->d_addr, &bonded_mac);
857                 }
858
859                 free_pkts(pkts, expected_pkts_cnt);
860                 TEST_ASSERT_EQUAL(eq_cnt, expected_pkts_cnt, "Packet address mismatch");
861         } else if (retval > 0)
862                 free_pkts(pkts, retval);
863
864         TEST_ASSERT_EQUAL(retval, expected_pkts_cnt,
865                 "Expected %u packets but received only %d", expected_pkts_cnt, retval);
866
867         /* Link down test: simulate link down for first slave. */
868         delay = bond_get_update_timeout_ms();
869
870         uint8_t slave_down_id = INVALID_PORT_ID;
871
872         /* Find first slave and make link down on it*/
873         FOR_EACH_SLAVE(i, slave) {
874                 rte_eth_dev_set_link_down(slave->port_id);
875                 slave_down_id = slave->port_id;
876                 break;
877         }
878
879         RTE_VERIFY(slave_down_id != INVALID_PORT_ID);
880
881         /* Give some time to rearrange bonding */
882         for (i = 0; i < 3; i++) {
883                 rte_delay_ms(delay);
884                 bond_handshake();
885         }
886
887         TEST_ASSERT_SUCCESS(bond_handshake(), "Handshake after link down failed");
888
889         /* Put packet to each slave */
890         FOR_EACH_SLAVE(i, slave) {
891                 void *pkt = NULL;
892
893                 dst_mac.addr_bytes[ETHER_ADDR_LEN - 1] = slave->port_id;
894                 retval = generate_and_put_packets(slave, &src_mac, &dst_mac, 1);
895                 TEST_ASSERT_SUCCESS(retval, "Failed to generate test packet burst.");
896
897                 src_mac.addr_bytes[ETHER_ADDR_LEN - 1] = slave->port_id;
898                 retval = generate_and_put_packets(slave, &src_mac, &bonded_mac, 1);
899                 TEST_ASSERT_SUCCESS(retval, "Failed to generate test packet burst.");
900
901                 retval = bond_rx(pkts, RTE_DIM(pkts));
902
903                 /* Clean anything */
904                 if (retval > 0)
905                         free_pkts(pkts, retval);
906
907                 while (rte_ring_dequeue(slave->rx_queue, (void **)&pkt) == 0)
908                         rte_pktmbuf_free(pkt);
909
910                 if (slave_down_id == slave->port_id)
911                         TEST_ASSERT_EQUAL(retval, 0, "Packets received unexpectedly.");
912                 else
913                         TEST_ASSERT_NOT_EQUAL(retval, 0,
914                                 "Expected to receive some packets on slave %u.",
915                                 slave->port_id);
916                 rte_eth_dev_start(slave->port_id);
917
918                 for (j = 0; j < 5; j++) {
919                         TEST_ASSERT(bond_handshake_reply(slave) >= 0,
920                                 "Handshake after link up");
921
922                         if (bond_handshake_done(slave) == 1)
923                                 break;
924                 }
925
926                 TEST_ASSERT(j < 5, "Failed to agregate slave after link up");
927         }
928
929         remove_slaves_and_stop_bonded_device();
930         return TEST_SUCCESS;
931 }
932
933 static int
934 test_mode4_tx_burst(void)
935 {
936         struct slave_conf *slave;
937         uint16_t i, j;
938
939         uint16_t exp_pkts_cnt, pkts_cnt = 0;
940         struct rte_mbuf *pkts[MAX_PKT_BURST];
941         int retval;
942         unsigned delay;
943
944         struct ether_addr dst_mac = { { 0x00, 0xFF, 0x00, 0xFF, 0x00, 0x00 } };
945         struct ether_addr bonded_mac;
946
947         retval = initialize_bonded_device_with_slaves(TEST_TX_SLAVE_COUNT, 0);
948         TEST_ASSERT_SUCCESS(retval, "Failed to initialize bonded device");
949
950         retval = bond_handshake();
951         TEST_ASSERT_SUCCESS(retval, "Initial handshake failed");
952
953         rte_eth_macaddr_get(test_params.bonded_port_id, &bonded_mac);
954
955         /* Prepare burst */
956         for (pkts_cnt = 0; pkts_cnt < RTE_DIM(pkts); pkts_cnt++) {
957                 dst_mac.addr_bytes[ETHER_ADDR_LEN - 1] = pkts_cnt;
958                 retval = generate_packets(&bonded_mac, &dst_mac, 1, &pkts[pkts_cnt]);
959
960                 if (retval != 1)
961                         free_pkts(pkts, pkts_cnt);
962
963                 TEST_ASSERT_EQUAL(retval, 1, "Failed to generate packet %u", pkts_cnt);
964         }
965         exp_pkts_cnt = pkts_cnt;
966
967         /* Transmit packets on bonded device */
968         retval = bond_tx(pkts, pkts_cnt);
969         if (retval > 0 && retval < pkts_cnt)
970                 free_pkts(&pkts[retval], pkts_cnt - retval);
971
972         TEST_ASSERT_EQUAL(retval, pkts_cnt, "TX on bonded device failed");
973
974         /* Check if packets were transmitted properly. Every slave should have
975          * at least one packet, and sum must match. Under normal operation
976          * there should be no LACP nor MARKER frames. */
977         pkts_cnt = 0;
978         FOR_EACH_SLAVE(i, slave) {
979                 uint16_t normal_cnt, slow_cnt;
980
981                 retval = slave_get_pkts(slave, pkts, RTE_DIM(pkts));
982                 normal_cnt = 0;
983                 slow_cnt = 0;
984
985                 for (j = 0; j < retval; j++) {
986                         if (make_lacp_reply(slave, pkts[j]) == 1)
987                                 normal_cnt++;
988                         else
989                                 slow_cnt++;
990                 }
991
992                 free_pkts(pkts, normal_cnt + slow_cnt);
993                 TEST_ASSERT_EQUAL(slow_cnt, 0,
994                         "slave %u unexpectedly transmitted %d SLOW packets", slave->port_id,
995                         slow_cnt);
996
997                 TEST_ASSERT_NOT_EQUAL(normal_cnt, 0,
998                         "slave %u did not transmitted any packets", slave->port_id);
999
1000                 pkts_cnt += normal_cnt;
1001         }
1002
1003         TEST_ASSERT_EQUAL(exp_pkts_cnt, pkts_cnt,
1004                 "Expected %u packets but transmitted only %d", exp_pkts_cnt, pkts_cnt);
1005
1006         /* Link down test:
1007          * simulate link down for first slave. */
1008         delay = bond_get_update_timeout_ms();
1009
1010         uint8_t slave_down_id = INVALID_PORT_ID;
1011
1012         FOR_EACH_SLAVE(i, slave) {
1013                 rte_eth_dev_set_link_down(slave->port_id);
1014                 slave_down_id = slave->port_id;
1015                 break;
1016         }
1017
1018         RTE_VERIFY(slave_down_id != INVALID_PORT_ID);
1019
1020         /* Give some time to rearrange bonding. */
1021         for (i = 0; i < 3; i++) {
1022                 bond_handshake();
1023                 rte_delay_ms(delay);
1024         }
1025
1026         TEST_ASSERT_SUCCESS(bond_handshake(), "Handshake after link down failed");
1027
1028         /* Prepare burst. */
1029         for (pkts_cnt = 0; pkts_cnt < RTE_DIM(pkts); pkts_cnt++) {
1030                 dst_mac.addr_bytes[ETHER_ADDR_LEN - 1] = pkts_cnt;
1031                 retval = generate_packets(&bonded_mac, &dst_mac, 1, &pkts[pkts_cnt]);
1032
1033                 if (retval != 1)
1034                         free_pkts(pkts, pkts_cnt);
1035
1036                 TEST_ASSERT_EQUAL(retval, 1, "Failed to generate test packet %u",
1037                         pkts_cnt);
1038         }
1039         exp_pkts_cnt = pkts_cnt;
1040
1041         /* Transmit packets on bonded device. */
1042         retval = bond_tx(pkts, pkts_cnt);
1043         if (retval > 0 && retval < pkts_cnt)
1044                 free_pkts(&pkts[retval], pkts_cnt - retval);
1045
1046         TEST_ASSERT_EQUAL(retval, pkts_cnt, "TX on bonded device failed");
1047
1048         /* Check if packets was transmitted properly. Every slave should have
1049          * at least one packet, and sum must match. Under normal operation
1050          * there should be no LACP nor MARKER frames. */
1051         pkts_cnt = 0;
1052         FOR_EACH_SLAVE(i, slave) {
1053                 uint16_t normal_cnt, slow_cnt;
1054
1055                 retval = slave_get_pkts(slave, pkts, RTE_DIM(pkts));
1056                 normal_cnt = 0;
1057                 slow_cnt = 0;
1058
1059                 for (j = 0; j < retval; j++) {
1060                         if (make_lacp_reply(slave, pkts[j]) == 1)
1061                                 normal_cnt++;
1062                         else
1063                                 slow_cnt++;
1064                 }
1065
1066                 free_pkts(pkts, normal_cnt + slow_cnt);
1067
1068                 if (slave_down_id == slave->port_id) {
1069                         TEST_ASSERT_EQUAL(normal_cnt + slow_cnt, 0,
1070                                 "slave %u enexpectedly transmitted %u packets",
1071                                 normal_cnt + slow_cnt, slave->port_id);
1072                 } else {
1073                         TEST_ASSERT_EQUAL(slow_cnt, 0,
1074                                 "slave %u unexpectedly transmitted %d SLOW packets",
1075                                 slave->port_id, slow_cnt);
1076
1077                         TEST_ASSERT_NOT_EQUAL(normal_cnt, 0,
1078                                 "slave %u did not transmitted any packets", slave->port_id);
1079                 }
1080
1081                 pkts_cnt += normal_cnt;
1082         }
1083
1084         TEST_ASSERT_EQUAL(exp_pkts_cnt, pkts_cnt,
1085                 "Expected %u packets but transmitted only %d", exp_pkts_cnt, pkts_cnt);
1086
1087         return remove_slaves_and_stop_bonded_device();
1088 }
1089
1090 static void
1091 init_marker(struct rte_mbuf *pkt, struct slave_conf *slave)
1092 {
1093         struct marker_header *marker_hdr = rte_pktmbuf_mtod(pkt,
1094                         struct marker_header *);
1095
1096         /* Copy multicast destination address */
1097         ether_addr_copy(&slow_protocol_mac_addr, &marker_hdr->eth_hdr.d_addr);
1098
1099         /* Init source address */
1100         ether_addr_copy(&parnter_mac_default, &marker_hdr->eth_hdr.s_addr);
1101         marker_hdr->eth_hdr.s_addr.addr_bytes[ETHER_ADDR_LEN-1] = slave->port_id;
1102
1103         marker_hdr->eth_hdr.ether_type = rte_cpu_to_be_16(ETHER_TYPE_SLOW);
1104
1105         marker_hdr->marker.subtype = SLOW_SUBTYPE_MARKER;
1106         marker_hdr->marker.version_number = 1;
1107         marker_hdr->marker.tlv_type_marker = MARKER_TLV_TYPE_INFO;
1108         marker_hdr->marker.info_length =
1109                         offsetof(struct marker, reserved_90) -
1110                         offsetof(struct marker, requester_port);
1111         RTE_VERIFY(marker_hdr->marker.info_length == 16);
1112         marker_hdr->marker.requester_port = slave->port_id + 1;
1113         marker_hdr->marker.tlv_type_terminator = TLV_TYPE_TERMINATOR_INFORMATION;
1114         marker_hdr->marker.terminator_length = 0;
1115 }
1116
1117 static int
1118 test_mode4_marker(void)
1119 {
1120         struct slave_conf *slave;
1121         struct rte_mbuf *pkts[MAX_PKT_BURST];
1122         struct rte_mbuf *marker_pkt;
1123         struct marker_header *marker_hdr;
1124
1125         unsigned delay;
1126         int retval;
1127         uint16_t nb_pkts;
1128         uint8_t i, j;
1129         const uint16_t ethtype_slow_be = rte_be_to_cpu_16(ETHER_TYPE_SLOW);
1130
1131         retval = initialize_bonded_device_with_slaves(TEST_MARKER_SLAVE_COUT,
1132                                                       0);
1133         TEST_ASSERT_SUCCESS(retval, "Failed to initialize bonded device");
1134
1135         /* Test LACP handshake function */
1136         retval = bond_handshake();
1137         TEST_ASSERT_SUCCESS(retval, "Initial handshake failed");
1138
1139         delay = bond_get_update_timeout_ms();
1140         FOR_EACH_SLAVE(i, slave) {
1141                 marker_pkt = rte_pktmbuf_alloc(test_params.mbuf_pool);
1142                 TEST_ASSERT_NOT_NULL(marker_pkt, "Failed to allocate marker packet");
1143                 init_marker(marker_pkt, slave);
1144
1145                 retval = slave_put_pkts(slave, &marker_pkt, 1);
1146                 if (retval != 1)
1147                         rte_pktmbuf_free(marker_pkt);
1148
1149                 TEST_ASSERT_EQUAL(retval, 1,
1150                         "Failed to send marker packet to slave %u", slave->port_id);
1151
1152                 for (j = 0; j < 20; ++j) {
1153                         rte_delay_ms(delay);
1154                         retval = rte_eth_rx_burst(test_params.bonded_port_id, 0, pkts,
1155                                 RTE_DIM(pkts));
1156
1157                         if (retval > 0)
1158                                 free_pkts(pkts, retval);
1159
1160                         TEST_ASSERT_EQUAL(retval, 0, "Received packets unexpectedly");
1161
1162                         retval = rte_eth_tx_burst(test_params.bonded_port_id, 0, NULL, 0);
1163                         TEST_ASSERT_EQUAL(retval, 0,
1164                                 "Requested TX of 0 packets but %d transmitted", retval);
1165
1166                         /* Check if LACP packet was send by state machines
1167                            First and only packet must be a maker response */
1168                         retval = slave_get_pkts(slave, pkts, MAX_PKT_BURST);
1169                         if (retval == 0)
1170                                 continue;
1171                         if (retval > 1)
1172                                 free_pkts(pkts, retval);
1173
1174                         TEST_ASSERT_EQUAL(retval, 1, "failed to get slave packets");
1175                         nb_pkts = retval;
1176
1177                         marker_hdr = rte_pktmbuf_mtod(pkts[0], struct marker_header *);
1178                         /* Check if it's slow packet*/
1179                         if (marker_hdr->eth_hdr.ether_type != ethtype_slow_be)
1180                                 retval = -1;
1181                         /* Check if it's marker packet */
1182                         else if (marker_hdr->marker.subtype != SLOW_SUBTYPE_MARKER)
1183                                 retval = -2;
1184                         else if (marker_hdr->marker.tlv_type_marker != MARKER_TLV_TYPE_RESP)
1185                                 retval = -3;
1186
1187                         free_pkts(pkts, nb_pkts);
1188
1189                         TEST_ASSERT_NOT_EQUAL(retval, -1, "Unexpected protocol type");
1190                         TEST_ASSERT_NOT_EQUAL(retval, -2, "Unexpected sub protocol type");
1191                         TEST_ASSERT_NOT_EQUAL(retval, -3, "Unexpected marker type");
1192                         break;
1193                 }
1194
1195                 TEST_ASSERT(j < 20, "Marker response not found");
1196         }
1197
1198         retval = remove_slaves_and_stop_bonded_device();
1199         TEST_ASSERT_SUCCESS(retval,     "Test cleanup failed.");
1200
1201         return TEST_SUCCESS;
1202 }
1203
1204 static int
1205 test_mode4_expired(void)
1206 {
1207         struct slave_conf *slave, *exp_slave = NULL;
1208         struct rte_mbuf *pkts[MAX_PKT_BURST];
1209         int retval;
1210         uint32_t old_delay;
1211
1212         uint8_t i;
1213         uint16_t j;
1214
1215         struct rte_eth_bond_8023ad_conf conf;
1216
1217         retval = initialize_bonded_device_with_slaves(TEST_EXPIRED_SLAVE_COUNT,
1218                                                       0);
1219         /* Set custom timeouts to make test last shorter. */
1220         rte_eth_bond_8023ad_conf_get(test_params.bonded_port_id, &conf);
1221         conf.fast_periodic_ms = 100;
1222         conf.slow_periodic_ms = 600;
1223         conf.short_timeout_ms = 300;
1224         conf.long_timeout_ms = 900;
1225         conf.aggregate_wait_timeout_ms = 200;
1226         conf.tx_period_ms = 100;
1227         old_delay = conf.update_timeout_ms;
1228         conf.update_timeout_ms = 10;
1229         rte_eth_bond_8023ad_setup(test_params.bonded_port_id, &conf);
1230
1231         /* Wait for new settings to be applied. */
1232         for (i = 0; i < old_delay/conf.update_timeout_ms * 2; i++) {
1233                 FOR_EACH_SLAVE(j, slave)
1234                         bond_handshake_reply(slave);
1235
1236                 rte_delay_ms(conf.update_timeout_ms);
1237         }
1238
1239         retval = bond_handshake();
1240         TEST_ASSERT_SUCCESS(retval, "Initial handshake failed");
1241
1242         /* Find first slave */
1243         FOR_EACH_SLAVE(i, slave) {
1244                 exp_slave = slave;
1245                 break;
1246         }
1247
1248         RTE_VERIFY(exp_slave != NULL);
1249
1250         /* When one of partners do not send or respond to LACP frame in
1251          * conf.long_timeout_ms time, internal state machines should detect this
1252          * and transit to expired state. */
1253         for (j = 0; j < conf.long_timeout_ms/conf.update_timeout_ms + 2; j++) {
1254                 rte_delay_ms(conf.update_timeout_ms);
1255
1256                 retval = bond_tx(NULL, 0);
1257                 TEST_ASSERT_EQUAL(retval, 0, "Unexpectedly received %d packets",
1258                         retval);
1259
1260                 FOR_EACH_SLAVE(i, slave) {
1261                         retval = bond_handshake_reply(slave);
1262                         TEST_ASSERT(retval >= 0, "Handshake failed");
1263
1264                         /* Remove replay for slave that supose to be expired. */
1265                         if (slave == exp_slave) {
1266                                 while (rte_ring_count(slave->rx_queue) > 0) {
1267                                         void *pkt = NULL;
1268
1269                                         rte_ring_dequeue(slave->rx_queue, &pkt);
1270                                         rte_pktmbuf_free(pkt);
1271                                 }
1272                         }
1273                 }
1274
1275                 retval = bond_rx(pkts, RTE_DIM(pkts));
1276                 if (retval > 0)
1277                         free_pkts(pkts, retval);
1278
1279                 TEST_ASSERT_EQUAL(retval, 0, "Unexpectedly received %d packets",
1280                         retval);
1281         }
1282
1283         /* After test only expected slave should be in EXPIRED state */
1284         FOR_EACH_SLAVE(i, slave) {
1285                 if (slave == exp_slave)
1286                         TEST_ASSERT(slave->lacp_parnter_state & STATE_EXPIRED,
1287                                 "Slave %u should be in expired.", slave->port_id);
1288                 else
1289                         TEST_ASSERT_EQUAL(bond_handshake_done(slave), 1,
1290                                 "Slave %u should be operational.", slave->port_id);
1291         }
1292
1293         retval = remove_slaves_and_stop_bonded_device();
1294         TEST_ASSERT_SUCCESS(retval, "Test cleanup failed.");
1295
1296         return TEST_SUCCESS;
1297 }
1298
1299 static int
1300 test_mode4_ext_ctrl(void)
1301 {
1302         /*
1303          * configure bonded interface without the external sm enabled
1304          *   . try to transmit lacpdu (should fail)
1305          *   . try to set collecting and distributing flags (should fail)
1306          * reconfigure w/external sm
1307          *   . transmit one lacpdu on each slave using new api
1308          *   . make sure each slave receives one lacpdu using the callback api
1309          *   . transmit one data pdu on each slave (should fail)
1310          *   . enable distribution and collection, send one data pdu each again
1311          */
1312
1313         int retval;
1314         struct slave_conf *slave = NULL;
1315         uint8_t i;
1316
1317         struct rte_mbuf *lacp_tx_buf[SLAVE_COUNT];
1318         struct ether_addr src_mac, dst_mac;
1319         struct lacpdu_header lacpdu = {
1320                 .lacpdu = {
1321                         .subtype = SLOW_SUBTYPE_LACP,
1322                 },
1323         };
1324
1325         ether_addr_copy(&parnter_system, &src_mac);
1326         ether_addr_copy(&slow_protocol_mac_addr, &dst_mac);
1327
1328         initialize_eth_header(&lacpdu.eth_hdr, &src_mac, &dst_mac,
1329                               ETHER_TYPE_SLOW, 0, 0);
1330
1331         for (i = 0; i < SLAVE_COUNT; i++) {
1332                 lacp_tx_buf[i] = rte_pktmbuf_alloc(test_params.mbuf_pool);
1333                 rte_memcpy(rte_pktmbuf_mtod(lacp_tx_buf[i], char *),
1334                            &lacpdu, sizeof(lacpdu));
1335                 rte_pktmbuf_pkt_len(lacp_tx_buf[i]) = sizeof(lacpdu);
1336         }
1337
1338         retval = initialize_bonded_device_with_slaves(TEST_TX_SLAVE_COUNT, 0);
1339         TEST_ASSERT_SUCCESS(retval, "Failed to initialize bonded device");
1340
1341         FOR_EACH_SLAVE(i, slave) {
1342                 TEST_ASSERT_FAIL(rte_eth_bond_8023ad_ext_slowtx(
1343                                                 test_params.bonded_port_id,
1344                                                 slave->port_id, lacp_tx_buf[i]),
1345                                  "Slave should not allow manual LACP xmit");
1346                 TEST_ASSERT_FAIL(rte_eth_bond_8023ad_ext_collect(
1347                                                 test_params.bonded_port_id,
1348                                                 slave->port_id, 1),
1349                                  "Slave should not allow external state controls");
1350         }
1351
1352         free_pkts(lacp_tx_buf, RTE_DIM(lacp_tx_buf));
1353
1354         retval = remove_slaves_and_stop_bonded_device();
1355         TEST_ASSERT_SUCCESS(retval, "Bonded device cleanup failed.");
1356
1357         return TEST_SUCCESS;
1358 }
1359
1360
1361 static int
1362 test_mode4_ext_lacp(void)
1363 {
1364         int retval;
1365         struct slave_conf *slave = NULL;
1366         uint8_t all_slaves_done = 0, i;
1367         uint16_t nb_pkts;
1368         const unsigned int delay = bond_get_update_timeout_ms();
1369
1370         struct rte_mbuf *lacp_tx_buf[SLAVE_COUNT];
1371         struct rte_mbuf *buf[SLAVE_COUNT];
1372         struct ether_addr src_mac, dst_mac;
1373         struct lacpdu_header lacpdu = {
1374                 .lacpdu = {
1375                         .subtype = SLOW_SUBTYPE_LACP,
1376                 },
1377         };
1378
1379         ether_addr_copy(&parnter_system, &src_mac);
1380         ether_addr_copy(&slow_protocol_mac_addr, &dst_mac);
1381
1382         initialize_eth_header(&lacpdu.eth_hdr, &src_mac, &dst_mac,
1383                               ETHER_TYPE_SLOW, 0, 0);
1384
1385         for (i = 0; i < SLAVE_COUNT; i++) {
1386                 lacp_tx_buf[i] = rte_pktmbuf_alloc(test_params.mbuf_pool);
1387                 rte_memcpy(rte_pktmbuf_mtod(lacp_tx_buf[i], char *),
1388                            &lacpdu, sizeof(lacpdu));
1389                 rte_pktmbuf_pkt_len(lacp_tx_buf[i]) = sizeof(lacpdu);
1390         }
1391
1392         retval = initialize_bonded_device_with_slaves(TEST_TX_SLAVE_COUNT, 1);
1393         TEST_ASSERT_SUCCESS(retval, "Failed to initialize bonded device");
1394
1395         memset(lacpdu_rx_count, 0, sizeof(lacpdu_rx_count));
1396
1397         /* Wait for new settings to be applied. */
1398         for (i = 0; i < 30; ++i)
1399                 rte_delay_ms(delay);
1400
1401         FOR_EACH_SLAVE(i, slave) {
1402                 retval = rte_eth_bond_8023ad_ext_slowtx(
1403                                                 test_params.bonded_port_id,
1404                                                 slave->port_id, lacp_tx_buf[i]);
1405                 TEST_ASSERT_SUCCESS(retval,
1406                                     "Slave should allow manual LACP xmit");
1407         }
1408
1409         nb_pkts = bond_tx(NULL, 0);
1410         TEST_ASSERT_EQUAL(nb_pkts, 0, "Packets transmitted unexpectedly");
1411
1412         FOR_EACH_SLAVE(i, slave) {
1413                 nb_pkts = slave_get_pkts(slave, buf, RTE_DIM(buf));
1414                 TEST_ASSERT_EQUAL(nb_pkts, 1, "found %u packets on slave %d\n",
1415                                   nb_pkts, i);
1416                 slave_put_pkts(slave, buf, nb_pkts);
1417         }
1418
1419         nb_pkts = bond_rx(buf, RTE_DIM(buf));
1420         free_pkts(buf, nb_pkts);
1421         TEST_ASSERT_EQUAL(nb_pkts, 0, "Packets received unexpectedly");
1422
1423         /* wait for the periodic callback to run */
1424         for (i = 0; i < 30 && all_slaves_done == 0; ++i) {
1425                 uint8_t s, total = 0;
1426
1427                 rte_delay_ms(delay);
1428                 FOR_EACH_SLAVE(s, slave) {
1429                         total += lacpdu_rx_count[slave->port_id];
1430                 }
1431
1432                 if (total >= SLAVE_COUNT)
1433                         all_slaves_done = 1;
1434         }
1435
1436         FOR_EACH_SLAVE(i, slave) {
1437                 TEST_ASSERT_EQUAL(lacpdu_rx_count[slave->port_id], 1,
1438                                   "Slave port %u should have received 1 lacpdu (count=%u)",
1439                                   slave->port_id,
1440                                   lacpdu_rx_count[slave->port_id]);
1441         }
1442
1443         retval = remove_slaves_and_stop_bonded_device();
1444         TEST_ASSERT_SUCCESS(retval, "Test cleanup failed.");
1445
1446         return TEST_SUCCESS;
1447 }
1448
1449 static int
1450 check_environment(void)
1451 {
1452         struct slave_conf *port;
1453         uint8_t i, env_state;
1454         uint8_t slaves[RTE_DIM(test_params.slave_ports)];
1455         int slaves_count;
1456
1457         env_state = 0;
1458         FOR_EACH_PORT(i, port) {
1459                 if (rte_ring_count(port->rx_queue) != 0)
1460                         env_state |= 0x01;
1461
1462                 if (rte_ring_count(port->tx_queue) != 0)
1463                         env_state |= 0x02;
1464
1465                 if (port->bonded != 0)
1466                         env_state |= 0x04;
1467
1468                 if (port->lacp_parnter_state != 0)
1469                         env_state |= 0x08;
1470
1471                 if (env_state != 0)
1472                         break;
1473         }
1474
1475         slaves_count = rte_eth_bond_slaves_get(test_params.bonded_port_id,
1476                         slaves, RTE_DIM(slaves));
1477
1478         if (slaves_count != 0)
1479                 env_state |= 0x10;
1480
1481         TEST_ASSERT_EQUAL(env_state, 0,
1482                 "Environment not clean (port %u):%s%s%s%s%s",
1483                 port->port_id,
1484                 env_state & 0x01 ? " slave rx queue not clean" : "",
1485                 env_state & 0x02 ? " slave tx queue not clean" : "",
1486                 env_state & 0x04 ? " port marked as enslaved" : "",
1487                 env_state & 0x80 ? " slave state is not reset" : "",
1488                 env_state & 0x10 ? " slave count not equal 0" : ".");
1489
1490
1491         return TEST_SUCCESS;
1492 }
1493
1494 static int
1495 test_mode4_executor(int (*test_func)(void))
1496 {
1497         struct slave_conf *port;
1498         int test_result;
1499         uint8_t i;
1500         void *pkt;
1501
1502         /* Check if environment is clean. Fail to launch a test if there was
1503          * a critical error before that prevented to reset environment. */
1504         TEST_ASSERT_SUCCESS(check_environment(),
1505                 "Refusing to launch test in dirty environment.");
1506
1507         RTE_VERIFY(test_func != NULL);
1508         test_result = (*test_func)();
1509
1510         /* If test succeed check if environment wast left in good condition. */
1511         if (test_result == TEST_SUCCESS)
1512                 test_result = check_environment();
1513
1514         /* Reset environment in case test failed to do that. */
1515         if (test_result != TEST_SUCCESS) {
1516                 TEST_ASSERT_SUCCESS(remove_slaves_and_stop_bonded_device(),
1517                         "Failed to stop bonded device");
1518
1519                 FOR_EACH_PORT(i, port) {
1520                         while (rte_ring_count(port->rx_queue) != 0) {
1521                                 if (rte_ring_dequeue(port->rx_queue, &pkt) == 0)
1522                                         rte_pktmbuf_free(pkt);
1523                         }
1524
1525                         while (rte_ring_count(port->tx_queue) != 0) {
1526                                 if (rte_ring_dequeue(port->tx_queue, &pkt) == 0)
1527                                         rte_pktmbuf_free(pkt);
1528                         }
1529                 }
1530         }
1531
1532         return test_result;
1533 }
1534
1535 static int
1536 test_mode4_lacp_wrapper(void)
1537 {
1538         return test_mode4_executor(&test_mode4_lacp);
1539 }
1540
1541 static int
1542 test_mode4_marker_wrapper(void)
1543 {
1544         return test_mode4_executor(&test_mode4_marker);
1545 }
1546
1547 static int
1548 test_mode4_rx_wrapper(void)
1549 {
1550         return test_mode4_executor(&test_mode4_rx);
1551 }
1552
1553 static int
1554 test_mode4_tx_burst_wrapper(void)
1555 {
1556         return test_mode4_executor(&test_mode4_tx_burst);
1557 }
1558
1559 static int
1560 test_mode4_expired_wrapper(void)
1561 {
1562         return test_mode4_executor(&test_mode4_expired);
1563 }
1564
1565 static int
1566 test_mode4_ext_ctrl_wrapper(void)
1567 {
1568         return test_mode4_executor(&test_mode4_ext_ctrl);
1569 }
1570
1571 static int
1572 test_mode4_ext_lacp_wrapper(void)
1573 {
1574         return test_mode4_executor(&test_mode4_ext_lacp);
1575 }
1576
1577 static struct unit_test_suite link_bonding_mode4_test_suite  = {
1578         .suite_name = "Link Bonding mode 4 Unit Test Suite",
1579         .setup = test_setup,
1580         .teardown = testsuite_teardown,
1581         .unit_test_cases = {
1582                 TEST_CASE_NAMED("test_mode4_lacp", test_mode4_lacp_wrapper),
1583                 TEST_CASE_NAMED("test_mode4_rx", test_mode4_rx_wrapper),
1584                 TEST_CASE_NAMED("test_mode4_tx_burst", test_mode4_tx_burst_wrapper),
1585                 TEST_CASE_NAMED("test_mode4_marker", test_mode4_marker_wrapper),
1586                 TEST_CASE_NAMED("test_mode4_expired", test_mode4_expired_wrapper),
1587                 TEST_CASE_NAMED("test_mode4_ext_ctrl",
1588                                 test_mode4_ext_ctrl_wrapper),
1589                 TEST_CASE_NAMED("test_mode4_ext_lacp",
1590                                 test_mode4_ext_lacp_wrapper),
1591
1592                 TEST_CASES_END() /**< NULL terminate unit test array */
1593         }
1594 };
1595
1596 static int
1597 test_link_bonding_mode4(void)
1598 {
1599         return unit_test_suite_runner(&link_bonding_mode4_test_suite);
1600 }
1601
1602 REGISTER_TEST_COMMAND(link_bonding_mode4_autotest, test_link_bonding_mode4);