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