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