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