net: add rte prefix to IP defines
[dpdk.git] / app / 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 rte_ether_addr slave_mac_default = {
58         { 0x00, 0xFF, 0x00, 0xFF, 0x00, 0x00 }
59 };
60
61 static const struct rte_ether_addr parnter_mac_default = {
62         { 0x22, 0xBB, 0xFF, 0xBB, 0x00, 0x00 }
63 };
64
65 static const struct rte_ether_addr parnter_system = {
66         { 0x33, 0xFF, 0xBB, 0xFF, 0x00, 0x00 }
67 };
68
69 static const struct rte_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 rte_ether_hdr pkt_eth_hdr;
84         struct rte_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 = RTE_ETHER_MAX_LEN,
112                 .split_hdr_size = 0,
113         },
114         .txmode = {
115                 .mq_mode = ETH_MQ_TX_NONE,
116         },
117         .lpbk_mode = 0,
118 };
119
120 static uint8_t lacpdu_rx_count[RTE_MAX_ETHPORTS] = {0, };
121
122 #define FOR_EACH(_i, _item, _array, _size) \
123         for (_i = 0, _item = &_array[0]; _i < _size && (_item = &_array[_i]); _i++)
124
125 /* Macro for iterating over every port that can be used as a slave
126  * in this test.
127  * _i variable used as an index in test_params->slave_ports
128  * _slave pointer to &test_params->slave_ports[_idx]
129  */
130 #define FOR_EACH_PORT(_i, _port) \
131         FOR_EACH(_i, _port, test_params.slave_ports, \
132                 RTE_DIM(test_params.slave_ports))
133
134 /* Macro for iterating over every port that can be used as a slave
135  * in this test and satisfy given condition.
136  *
137  * _i variable used as an index in test_params->slave_ports
138  * _slave pointer to &test_params->slave_ports[_idx]
139  * _condition condition that need to be checked
140  */
141 #define FOR_EACH_PORT_IF(_i, _port, _condition) FOR_EACH_PORT((_i), (_port)) \
142         if (!!(_condition))
143
144 /* Macro for iterating over every port that is currently a slave of a bonded
145  * device.
146  * _i variable used as an index in test_params->slave_ports
147  * _slave pointer to &test_params->slave_ports[_idx]
148  * */
149 #define FOR_EACH_SLAVE(_i, _slave) \
150         FOR_EACH_PORT_IF(_i, _slave, (_slave)->bonded != 0)
151
152 /*
153  * Returns packets from slaves TX queue.
154  * slave slave port
155  * buffer for packets
156  * size size of buffer
157  * return number of packets or negative error number
158  */
159 static int
160 slave_get_pkts(struct slave_conf *slave, struct rte_mbuf **buf, uint16_t size)
161 {
162         return rte_ring_dequeue_burst(slave->tx_queue, (void **)buf,
163                         size, NULL);
164 }
165
166 /*
167  * Injects given packets into slaves RX queue.
168  * slave slave port
169  * buffer for packets
170  * size number of packets to be injected
171  * return number of queued packets or negative error number
172  */
173 static int
174 slave_put_pkts(struct slave_conf *slave, struct rte_mbuf **buf, uint16_t size)
175 {
176         return rte_ring_enqueue_burst(slave->rx_queue, (void **)buf,
177                         size, NULL);
178 }
179
180 static uint16_t
181 bond_rx(struct rte_mbuf **buf, uint16_t size)
182 {
183         return rte_eth_rx_burst(test_params.bonded_port_id, 0, buf, size);
184 }
185
186 static uint16_t
187 bond_tx(struct rte_mbuf **buf, uint16_t size)
188 {
189         return rte_eth_tx_burst(test_params.bonded_port_id, 0, buf, size);
190 }
191
192 static void
193 free_pkts(struct rte_mbuf **pkts, uint16_t count)
194 {
195         uint16_t i;
196
197         for (i = 0; i < count; i++) {
198                 if (pkts[i] != NULL)
199                         rte_pktmbuf_free(pkts[i]);
200         }
201 }
202
203 static int
204 configure_ethdev(uint16_t port_id, uint8_t start)
205 {
206         TEST_ASSERT(rte_eth_dev_configure(port_id, 1, 1, &default_pmd_conf) == 0,
207                 "Failed to configure device %u", port_id);
208
209         TEST_ASSERT(rte_eth_rx_queue_setup(port_id, 0, RX_RING_SIZE,
210                 rte_eth_dev_socket_id(port_id), NULL, test_params.mbuf_pool) == 0,
211                 "Failed to setup rx queue.");
212
213         TEST_ASSERT(rte_eth_tx_queue_setup(port_id, 0, TX_RING_SIZE,
214                 rte_eth_dev_socket_id(port_id), NULL) == 0,
215                 "Failed to setup tx queue.");
216
217         if (start) {
218                 TEST_ASSERT(rte_eth_dev_start(port_id) == 0,
219                 "Failed to start device (%d).", port_id);
220         }
221         return 0;
222 }
223
224 static int
225 add_slave(struct slave_conf *slave, uint8_t start)
226 {
227         struct rte_ether_addr addr, addr_check;
228
229         /* Some sanity check */
230         RTE_VERIFY(test_params.slave_ports <= slave &&
231                 slave - test_params.slave_ports < (int)RTE_DIM(test_params.slave_ports));
232         RTE_VERIFY(slave->bonded == 0);
233         RTE_VERIFY(slave->port_id != INVALID_PORT_ID);
234
235         rte_ether_addr_copy(&slave_mac_default, &addr);
236         addr.addr_bytes[RTE_ETHER_ADDR_LEN - 1] = slave->port_id;
237
238         rte_eth_dev_mac_addr_remove(slave->port_id, &addr);
239
240         TEST_ASSERT_SUCCESS(rte_eth_dev_mac_addr_add(slave->port_id, &addr, 0),
241                 "Failed to set slave MAC address");
242
243         TEST_ASSERT_SUCCESS(rte_eth_bond_slave_add(test_params.bonded_port_id,
244                 slave->port_id),
245                         "Failed to add slave (idx=%u, id=%u) to bonding (id=%u)",
246                         (uint8_t)(slave - test_params.slave_ports), slave->port_id,
247                         test_params.bonded_port_id);
248
249         slave->bonded = 1;
250         if (start) {
251                 TEST_ASSERT_SUCCESS(rte_eth_dev_start(slave->port_id),
252                         "Failed to start slave %u", slave->port_id);
253         }
254
255         rte_eth_macaddr_get(slave->port_id, &addr_check);
256         TEST_ASSERT_EQUAL(rte_is_same_ether_addr(&addr, &addr_check), 1,
257                         "Slave MAC address is not as expected");
258
259         RTE_VERIFY(slave->lacp_parnter_state == 0);
260         return 0;
261 }
262
263 static int
264 remove_slave(struct slave_conf *slave)
265 {
266         ptrdiff_t slave_idx = slave - test_params.slave_ports;
267
268         RTE_VERIFY(test_params.slave_ports <= slave &&
269                 slave_idx < (ptrdiff_t)RTE_DIM(test_params.slave_ports));
270
271         RTE_VERIFY(slave->bonded == 1);
272         RTE_VERIFY(slave->port_id != INVALID_PORT_ID);
273
274         TEST_ASSERT_EQUAL(rte_ring_count(slave->rx_queue), 0,
275                 "Slave %u tx queue not empty while removing from bonding.",
276                 slave->port_id);
277
278         TEST_ASSERT_EQUAL(rte_ring_count(slave->rx_queue), 0,
279                 "Slave %u tx queue not empty while removing from bonding.",
280                 slave->port_id);
281
282         TEST_ASSERT_EQUAL(rte_eth_bond_slave_remove(test_params.bonded_port_id,
283                         slave->port_id), 0,
284                         "Failed to remove slave (idx=%u, id=%u) from bonding (id=%u)",
285                         (uint8_t)slave_idx, slave->port_id,
286                         test_params.bonded_port_id);
287
288         slave->bonded = 0;
289         slave->lacp_parnter_state = 0;
290         return 0;
291 }
292
293 static void
294 lacp_recv_cb(uint16_t slave_id, struct rte_mbuf *lacp_pkt)
295 {
296         struct rte_ether_hdr *hdr;
297         struct slow_protocol_frame *slow_hdr;
298
299         RTE_VERIFY(lacp_pkt != NULL);
300
301         hdr = rte_pktmbuf_mtod(lacp_pkt, struct rte_ether_hdr *);
302         RTE_VERIFY(hdr->ether_type == rte_cpu_to_be_16(RTE_ETHER_TYPE_SLOW));
303
304         slow_hdr = rte_pktmbuf_mtod(lacp_pkt, struct slow_protocol_frame *);
305         RTE_VERIFY(slow_hdr->slow_protocol.subtype == SLOW_SUBTYPE_LACP);
306
307         lacpdu_rx_count[slave_id]++;
308         rte_pktmbuf_free(lacp_pkt);
309 }
310
311 static int
312 initialize_bonded_device_with_slaves(uint16_t slave_count, uint8_t external_sm)
313 {
314         uint8_t i;
315
316         RTE_VERIFY(test_params.bonded_port_id != INVALID_PORT_ID);
317
318         for (i = 0; i < slave_count; i++) {
319                 TEST_ASSERT_SUCCESS(add_slave(&test_params.slave_ports[i], 1),
320                         "Failed to add port %u to bonded device.\n",
321                         test_params.slave_ports[i].port_id);
322         }
323
324         /* Reset mode 4 configuration */
325         rte_eth_bond_8023ad_setup(test_params.bonded_port_id, NULL);
326         rte_eth_promiscuous_disable(test_params.bonded_port_id);
327
328         if (external_sm) {
329                 struct rte_eth_bond_8023ad_conf conf;
330
331                 rte_eth_bond_8023ad_conf_get(test_params.bonded_port_id, &conf);
332                 conf.slowrx_cb = lacp_recv_cb;
333                 rte_eth_bond_8023ad_setup(test_params.bonded_port_id, &conf);
334
335         }
336
337         TEST_ASSERT_SUCCESS(rte_eth_dev_start(test_params.bonded_port_id),
338                 "Failed to start bonded device");
339
340         return TEST_SUCCESS;
341 }
342
343 static int
344 remove_slaves_and_stop_bonded_device(void)
345 {
346         struct slave_conf *slave;
347         int retval;
348         uint16_t slaves[RTE_MAX_ETHPORTS];
349         uint16_t i;
350
351         rte_eth_dev_stop(test_params.bonded_port_id);
352
353         FOR_EACH_SLAVE(i, slave)
354                 remove_slave(slave);
355
356         retval = rte_eth_bond_slaves_get(test_params.bonded_port_id, slaves,
357                 RTE_DIM(slaves));
358
359         TEST_ASSERT_EQUAL(retval, 0,
360                 "Expected bonded device %u have 0 slaves but returned %d.",
361                         test_params.bonded_port_id, retval);
362
363         FOR_EACH_PORT(i, slave) {
364                 rte_eth_dev_stop(slave->port_id);
365
366                 TEST_ASSERT(slave->bonded == 0,
367                         "Port id=%u is still marked as enslaved.", slave->port_id);
368         }
369
370         return TEST_SUCCESS;
371 }
372
373 static int
374 test_setup(void)
375 {
376         int retval, nb_mbuf_per_pool;
377         char name[RTE_ETH_NAME_MAX_LEN];
378         struct slave_conf *port;
379         const uint8_t socket_id = rte_socket_id();
380         uint16_t i;
381
382         if (test_params.mbuf_pool == NULL) {
383                 nb_mbuf_per_pool = TEST_RX_DESC_MAX + DEF_PKT_BURST +
384                                         TEST_TX_DESC_MAX + MAX_PKT_BURST;
385                 test_params.mbuf_pool = rte_pktmbuf_pool_create("TEST_MODE4",
386                         nb_mbuf_per_pool, MBUF_CACHE_SIZE, 0,
387                         RTE_MBUF_DEFAULT_BUF_SIZE, socket_id);
388
389                 TEST_ASSERT(test_params.mbuf_pool != NULL,
390                         "rte_mempool_create failed\n");
391         }
392
393         /* Create / initialize ring eth devs. */
394         FOR_EACH_PORT(i, port) {
395                 port = &test_params.slave_ports[i];
396
397                 if (port->rx_queue == NULL) {
398                         retval = snprintf(name, RTE_DIM(name), SLAVE_RX_QUEUE_FMT, i);
399                         TEST_ASSERT(retval <= (int)RTE_DIM(name) - 1, "Name too long");
400                         port->rx_queue = rte_ring_create(name, RX_RING_SIZE, socket_id, 0);
401                         TEST_ASSERT(port->rx_queue != NULL,
402                                 "Failed to allocate rx ring '%s': %s", name,
403                                 rte_strerror(rte_errno));
404                 }
405
406                 if (port->tx_queue == NULL) {
407                         retval = snprintf(name, RTE_DIM(name), SLAVE_TX_QUEUE_FMT, i);
408                         TEST_ASSERT(retval <= (int)RTE_DIM(name) - 1, "Name too long");
409                         port->tx_queue = rte_ring_create(name, TX_RING_SIZE, socket_id, 0);
410                         TEST_ASSERT_NOT_NULL(port->tx_queue,
411                                 "Failed to allocate tx ring '%s': %s", name,
412                                 rte_strerror(rte_errno));
413                 }
414
415                 if (port->port_id == INVALID_PORT_ID) {
416                         retval = snprintf(name, RTE_DIM(name), SLAVE_DEV_NAME_FMT, i);
417                         TEST_ASSERT(retval < (int)RTE_DIM(name) - 1, "Name too long");
418                         retval = rte_eth_from_rings(name, &port->rx_queue, 1,
419                                         &port->tx_queue, 1, socket_id);
420                         TEST_ASSERT(retval >= 0,
421                                 "Failed to create ring ethdev '%s'\n", name);
422
423                         port->port_id = rte_eth_dev_count_avail() - 1;
424                 }
425
426                 retval = configure_ethdev(port->port_id, 1);
427                 TEST_ASSERT_SUCCESS(retval, "Failed to configure virtual ethdev %s\n",
428                         name);
429         }
430
431         if (test_params.bonded_port_id == INVALID_PORT_ID) {
432                 retval = rte_eth_bond_create(BONDED_DEV_NAME, BONDING_MODE_8023AD,
433                                 socket_id);
434
435                 TEST_ASSERT(retval >= 0, "Failed to create bonded ethdev %s",
436                                 BONDED_DEV_NAME);
437
438                 test_params.bonded_port_id = retval;
439                 TEST_ASSERT_SUCCESS(configure_ethdev(test_params.bonded_port_id, 0),
440                                 "Failed to configure bonded ethdev %s", BONDED_DEV_NAME);
441         } else if (rte_eth_bond_mode_get(test_params.bonded_port_id) !=
442                         BONDING_MODE_8023AD) {
443                 TEST_ASSERT(rte_eth_bond_mode_set(test_params.bonded_port_id,
444                         BONDING_MODE_8023AD) == 0,
445                         "Failed to set ethdev %d to mode %d",
446                         test_params.bonded_port_id, BONDING_MODE_8023AD);
447         }
448
449         return 0;
450 }
451
452 static void
453 testsuite_teardown(void)
454 {
455         struct slave_conf *port;
456         uint8_t i;
457
458         /* Only stop ports.
459          * Any cleanup/reset state is done when particular test is
460          * started. */
461
462         rte_eth_dev_stop(test_params.bonded_port_id);
463
464         FOR_EACH_PORT(i, port)
465                 rte_eth_dev_stop(port->port_id);
466 }
467
468 /*
469  * Check if given LACP packet. If it is, make make replay packet to force
470  * COLLECTING state.
471  * return 0 when pkt is LACP frame, 1 if it is not slow frame, 2 if it is slow
472  * frame but not LACP
473  */
474 static int
475 make_lacp_reply(struct slave_conf *slave, struct rte_mbuf *pkt)
476 {
477         struct rte_ether_hdr *hdr;
478         struct slow_protocol_frame *slow_hdr;
479         struct lacpdu *lacp;
480
481         /* look for LACP */
482         hdr = rte_pktmbuf_mtod(pkt, struct rte_ether_hdr *);
483         if (hdr->ether_type != rte_cpu_to_be_16(RTE_ETHER_TYPE_SLOW))
484                 return 1;
485
486         slow_hdr = rte_pktmbuf_mtod(pkt, struct slow_protocol_frame *);
487         /* ignore packets of other types */
488         if (slow_hdr->slow_protocol.subtype != SLOW_SUBTYPE_LACP)
489                 return 2;
490
491         slow_hdr = rte_pktmbuf_mtod(pkt, struct slow_protocol_frame *);
492
493         /* Change source address to partner address */
494         rte_ether_addr_copy(&parnter_mac_default, &slow_hdr->eth_hdr.s_addr);
495         slow_hdr->eth_hdr.s_addr.addr_bytes[RTE_ETHER_ADDR_LEN - 1] =
496                 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         rte_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 rte_ether_addr *src_mac,
723         struct rte_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] = RTE_IPv4(192, 168, 0, 1) };
733         uint32_t ip_dst[4] = { [0 ... 2] = 0xFEEDFACE, [3] = RTE_IPv4(192, 168, 0, 2) };
734
735         struct rte_ether_hdr pkt_eth_hdr;
736         struct udp_hdr pkt_udp_hdr;
737         union {
738                 struct rte_ipv4_hdr v4;
739                 struct rte_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,
770                         struct rte_ether_addr *src_mac,
771                         struct rte_ether_addr *dst_mac, uint16_t count)
772 {
773         struct rte_mbuf *pkts[MAX_PKT_BURST];
774         int retval;
775
776         retval = generate_packets(src_mac, dst_mac, count, pkts);
777         if (retval != (int)count)
778                 return retval;
779
780         retval = slave_put_pkts(slave, pkts, count);
781         if (retval > 0 && retval != count)
782                 free_pkts(&pkts[retval], count - retval);
783
784         TEST_ASSERT_EQUAL(retval, count,
785                 "Failed to enqueue packets into slave %u RX queue", slave->port_id);
786
787         return TEST_SUCCESS;
788 }
789
790 static int
791 test_mode4_rx(void)
792 {
793         struct slave_conf *slave;
794         uint16_t i, j;
795
796         uint16_t expected_pkts_cnt;
797         struct rte_mbuf *pkts[MAX_PKT_BURST];
798         int retval;
799         unsigned delay;
800
801         struct rte_ether_hdr *hdr;
802
803         struct rte_ether_addr src_mac = {
804                 { 0x00, 0xFF, 0x00, 0xFF, 0x00, 0x00 } };
805         struct rte_ether_addr dst_mac;
806         struct rte_ether_addr bonded_mac;
807
808         retval = initialize_bonded_device_with_slaves(TEST_PROMISC_SLAVE_COUNT,
809                                                       0);
810         TEST_ASSERT_SUCCESS(retval, "Failed to initialize bonded device");
811
812         retval = bond_handshake();
813         TEST_ASSERT_SUCCESS(retval, "Initial handshake failed");
814
815         rte_eth_macaddr_get(test_params.bonded_port_id, &bonded_mac);
816         rte_ether_addr_copy(&bonded_mac, &dst_mac);
817
818         /* Assert that dst address is not bonding address.  Do not set the
819          * least significant bit of the zero byte as this would create a
820          * multicast address.
821          */
822         dst_mac.addr_bytes[0] += 2;
823
824         /* First try with promiscuous mode enabled.
825          * Add 2 packets to each slave. First with bonding MAC address, second with
826          * different. Check if we received all of them. */
827         rte_eth_promiscuous_enable(test_params.bonded_port_id);
828
829         expected_pkts_cnt = 0;
830         FOR_EACH_SLAVE(i, slave) {
831                 retval = generate_and_put_packets(slave, &src_mac, &bonded_mac, 1);
832                 TEST_ASSERT_SUCCESS(retval, "Failed to enqueue packets to slave %u",
833                         slave->port_id);
834
835                 retval = generate_and_put_packets(slave, &src_mac, &dst_mac, 1);
836                 TEST_ASSERT_SUCCESS(retval, "Failed to enqueue packets to slave %u",
837                         slave->port_id);
838
839                 /* Expect 2 packets per slave */
840                 expected_pkts_cnt += 2;
841         }
842
843         retval = rte_eth_rx_burst(test_params.bonded_port_id, 0, pkts,
844                 RTE_DIM(pkts));
845
846         if (retval == expected_pkts_cnt) {
847                 int cnt[2] = { 0, 0 };
848
849                 for (i = 0; i < expected_pkts_cnt; i++) {
850                         hdr = rte_pktmbuf_mtod(pkts[i], struct rte_ether_hdr *);
851                         cnt[rte_is_same_ether_addr(&hdr->d_addr,
852                                                         &bonded_mac)]++;
853                 }
854
855                 free_pkts(pkts, expected_pkts_cnt);
856
857                 /* For division by 2 expected_pkts_cnt must be even */
858                 RTE_VERIFY((expected_pkts_cnt & 1) == 0);
859                 TEST_ASSERT(cnt[0] == expected_pkts_cnt / 2 &&
860                         cnt[1] == expected_pkts_cnt / 2,
861                         "Expected %u packets with the same MAC and %u with different but "
862                         "got %u with the same and %u with different MAC",
863                         expected_pkts_cnt / 2, expected_pkts_cnt / 2, cnt[1], cnt[0]);
864         } else if (retval > 0)
865                 free_pkts(pkts, retval);
866
867         TEST_ASSERT_EQUAL(retval, expected_pkts_cnt,
868                 "Expected %u packets but received only %d", expected_pkts_cnt, retval);
869
870         /* Now, disable promiscuous mode. When promiscuous mode is disabled we
871          * expect to receive only packets that are directed to bonding port. */
872         rte_eth_promiscuous_disable(test_params.bonded_port_id);
873
874         expected_pkts_cnt = 0;
875         FOR_EACH_SLAVE(i, slave) {
876                 retval = generate_and_put_packets(slave, &src_mac, &bonded_mac, 1);
877                 TEST_ASSERT_SUCCESS(retval, "Failed to enqueue packets to slave %u",
878                         slave->port_id);
879
880                 retval = generate_and_put_packets(slave, &src_mac, &dst_mac, 1);
881                 TEST_ASSERT_SUCCESS(retval, "Failed to enqueue packets to slave %u",
882                         slave->port_id);
883
884                 /* Expect only one packet per slave */
885                 expected_pkts_cnt += 1;
886         }
887
888         retval = rte_eth_rx_burst(test_params.bonded_port_id, 0, pkts,
889                 RTE_DIM(pkts));
890
891         if (retval == expected_pkts_cnt) {
892                 int eq_cnt = 0;
893
894                 for (i = 0; i < expected_pkts_cnt; i++) {
895                         hdr = rte_pktmbuf_mtod(pkts[i], struct rte_ether_hdr *);
896                         eq_cnt += rte_is_same_ether_addr(&hdr->d_addr,
897                                                         &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[RTE_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[RTE_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 rte_ether_addr dst_mac = {
986                 { 0x00, 0xFF, 0x00, 0xFF, 0x00, 0x00 } };
987         struct rte_ether_addr bonded_mac;
988
989         retval = initialize_bonded_device_with_slaves(TEST_TX_SLAVE_COUNT, 0);
990         TEST_ASSERT_SUCCESS(retval, "Failed to initialize bonded device");
991
992         retval = bond_handshake();
993         TEST_ASSERT_SUCCESS(retval, "Initial handshake failed");
994
995         rte_eth_macaddr_get(test_params.bonded_port_id, &bonded_mac);
996
997         /* Prepare burst */
998         for (pkts_cnt = 0; pkts_cnt < RTE_DIM(pkts); pkts_cnt++) {
999                 dst_mac.addr_bytes[RTE_ETHER_ADDR_LEN - 1] = pkts_cnt;
1000                 retval = generate_packets(&bonded_mac, &dst_mac, 1, &pkts[pkts_cnt]);
1001
1002                 if (retval != 1)
1003                         free_pkts(pkts, pkts_cnt);
1004
1005                 TEST_ASSERT_EQUAL(retval, 1, "Failed to generate packet %u", pkts_cnt);
1006         }
1007         exp_pkts_cnt = pkts_cnt;
1008
1009         /* Transmit packets on bonded device */
1010         retval = bond_tx(pkts, pkts_cnt);
1011         if (retval > 0 && retval < pkts_cnt)
1012                 free_pkts(&pkts[retval], pkts_cnt - retval);
1013
1014         TEST_ASSERT_EQUAL(retval, pkts_cnt, "TX on bonded device failed");
1015
1016         /* Check if packets were transmitted properly. Every slave should have
1017          * at least one packet, and sum must match. Under normal operation
1018          * there should be no LACP nor MARKER frames. */
1019         pkts_cnt = 0;
1020         FOR_EACH_SLAVE(i, slave) {
1021                 uint16_t normal_cnt, slow_cnt;
1022
1023                 retval = slave_get_pkts(slave, pkts, RTE_DIM(pkts));
1024                 normal_cnt = 0;
1025                 slow_cnt = 0;
1026
1027                 for (j = 0; j < retval; j++) {
1028                         if (make_lacp_reply(slave, pkts[j]) == 1)
1029                                 normal_cnt++;
1030                         else
1031                                 slow_cnt++;
1032                 }
1033
1034                 free_pkts(pkts, normal_cnt + slow_cnt);
1035                 TEST_ASSERT_EQUAL(slow_cnt, 0,
1036                         "slave %u unexpectedly transmitted %d SLOW packets", slave->port_id,
1037                         slow_cnt);
1038
1039                 TEST_ASSERT_NOT_EQUAL(normal_cnt, 0,
1040                         "slave %u did not transmitted any packets", slave->port_id);
1041
1042                 pkts_cnt += normal_cnt;
1043         }
1044
1045         TEST_ASSERT_EQUAL(exp_pkts_cnt, pkts_cnt,
1046                 "Expected %u packets but transmitted only %d", exp_pkts_cnt, pkts_cnt);
1047
1048         /* Link down test:
1049          * simulate link down for first slave. */
1050         delay = bond_get_update_timeout_ms();
1051
1052         uint8_t slave_down_id = INVALID_PORT_ID;
1053
1054         FOR_EACH_SLAVE(i, slave) {
1055                 rte_eth_dev_set_link_down(slave->port_id);
1056                 slave_down_id = slave->port_id;
1057                 break;
1058         }
1059
1060         RTE_VERIFY(slave_down_id != INVALID_PORT_ID);
1061
1062         /* Give some time to rearrange bonding. */
1063         for (i = 0; i < 3; i++) {
1064                 bond_handshake();
1065                 rte_delay_ms(delay);
1066         }
1067
1068         TEST_ASSERT_SUCCESS(bond_handshake(), "Handshake after link down failed");
1069
1070         /* Prepare burst. */
1071         for (pkts_cnt = 0; pkts_cnt < RTE_DIM(pkts); pkts_cnt++) {
1072                 dst_mac.addr_bytes[RTE_ETHER_ADDR_LEN - 1] = pkts_cnt;
1073                 retval = generate_packets(&bonded_mac, &dst_mac, 1, &pkts[pkts_cnt]);
1074
1075                 if (retval != 1)
1076                         free_pkts(pkts, pkts_cnt);
1077
1078                 TEST_ASSERT_EQUAL(retval, 1, "Failed to generate test packet %u",
1079                         pkts_cnt);
1080         }
1081         exp_pkts_cnt = pkts_cnt;
1082
1083         /* Transmit packets on bonded device. */
1084         retval = bond_tx(pkts, pkts_cnt);
1085         if (retval > 0 && retval < pkts_cnt)
1086                 free_pkts(&pkts[retval], pkts_cnt - retval);
1087
1088         TEST_ASSERT_EQUAL(retval, pkts_cnt, "TX on bonded device failed");
1089
1090         /* Check if packets was transmitted properly. Every slave should have
1091          * at least one packet, and sum must match. Under normal operation
1092          * there should be no LACP nor MARKER frames. */
1093         pkts_cnt = 0;
1094         FOR_EACH_SLAVE(i, slave) {
1095                 uint16_t normal_cnt, slow_cnt;
1096
1097                 retval = slave_get_pkts(slave, pkts, RTE_DIM(pkts));
1098                 normal_cnt = 0;
1099                 slow_cnt = 0;
1100
1101                 for (j = 0; j < retval; j++) {
1102                         if (make_lacp_reply(slave, pkts[j]) == 1)
1103                                 normal_cnt++;
1104                         else
1105                                 slow_cnt++;
1106                 }
1107
1108                 free_pkts(pkts, normal_cnt + slow_cnt);
1109
1110                 if (slave_down_id == slave->port_id) {
1111                         TEST_ASSERT_EQUAL(normal_cnt + slow_cnt, 0,
1112                                 "slave %u enexpectedly transmitted %u packets",
1113                                 normal_cnt + slow_cnt, slave->port_id);
1114                 } else {
1115                         TEST_ASSERT_EQUAL(slow_cnt, 0,
1116                                 "slave %u unexpectedly transmitted %d SLOW packets",
1117                                 slave->port_id, slow_cnt);
1118
1119                         TEST_ASSERT_NOT_EQUAL(normal_cnt, 0,
1120                                 "slave %u did not transmitted any packets", slave->port_id);
1121                 }
1122
1123                 pkts_cnt += normal_cnt;
1124         }
1125
1126         TEST_ASSERT_EQUAL(exp_pkts_cnt, pkts_cnt,
1127                 "Expected %u packets but transmitted only %d", exp_pkts_cnt, pkts_cnt);
1128
1129         return remove_slaves_and_stop_bonded_device();
1130 }
1131
1132 static void
1133 init_marker(struct rte_mbuf *pkt, struct slave_conf *slave)
1134 {
1135         struct marker_header *marker_hdr = rte_pktmbuf_mtod(pkt,
1136                         struct marker_header *);
1137
1138         /* Copy multicast destination address */
1139         rte_ether_addr_copy(&slow_protocol_mac_addr,
1140                         &marker_hdr->eth_hdr.d_addr);
1141
1142         /* Init source address */
1143         rte_ether_addr_copy(&parnter_mac_default, &marker_hdr->eth_hdr.s_addr);
1144         marker_hdr->eth_hdr.s_addr.addr_bytes[RTE_ETHER_ADDR_LEN - 1] =
1145                 slave->port_id;
1146
1147         marker_hdr->eth_hdr.ether_type = rte_cpu_to_be_16(RTE_ETHER_TYPE_SLOW);
1148
1149         marker_hdr->marker.subtype = SLOW_SUBTYPE_MARKER;
1150         marker_hdr->marker.version_number = 1;
1151         marker_hdr->marker.tlv_type_marker = MARKER_TLV_TYPE_INFO;
1152         marker_hdr->marker.info_length =
1153                         offsetof(struct marker, reserved_90) -
1154                         offsetof(struct marker, requester_port);
1155         RTE_VERIFY(marker_hdr->marker.info_length == 16);
1156         marker_hdr->marker.requester_port = slave->port_id + 1;
1157         marker_hdr->marker.tlv_type_terminator = TLV_TYPE_TERMINATOR_INFORMATION;
1158         marker_hdr->marker.terminator_length = 0;
1159 }
1160
1161 static int
1162 test_mode4_marker(void)
1163 {
1164         struct slave_conf *slave;
1165         struct rte_mbuf *pkts[MAX_PKT_BURST];
1166         struct rte_mbuf *marker_pkt;
1167         struct marker_header *marker_hdr;
1168
1169         unsigned delay;
1170         int retval;
1171         uint16_t nb_pkts;
1172         uint8_t i, j;
1173         const uint16_t ethtype_slow_be = rte_be_to_cpu_16(RTE_ETHER_TYPE_SLOW);
1174
1175         retval = initialize_bonded_device_with_slaves(TEST_MARKER_SLAVE_COUT,
1176                                                       0);
1177         TEST_ASSERT_SUCCESS(retval, "Failed to initialize bonded device");
1178
1179         /* Test LACP handshake function */
1180         retval = bond_handshake();
1181         TEST_ASSERT_SUCCESS(retval, "Initial handshake failed");
1182
1183         delay = bond_get_update_timeout_ms();
1184         FOR_EACH_SLAVE(i, slave) {
1185                 marker_pkt = rte_pktmbuf_alloc(test_params.mbuf_pool);
1186                 TEST_ASSERT_NOT_NULL(marker_pkt, "Failed to allocate marker packet");
1187                 init_marker(marker_pkt, slave);
1188
1189                 retval = slave_put_pkts(slave, &marker_pkt, 1);
1190                 if (retval != 1)
1191                         rte_pktmbuf_free(marker_pkt);
1192
1193                 TEST_ASSERT_EQUAL(retval, 1,
1194                         "Failed to send marker packet to slave %u", slave->port_id);
1195
1196                 for (j = 0; j < 20; ++j) {
1197                         rte_delay_ms(delay);
1198                         retval = rte_eth_rx_burst(test_params.bonded_port_id, 0, pkts,
1199                                 RTE_DIM(pkts));
1200
1201                         if (retval > 0)
1202                                 free_pkts(pkts, retval);
1203
1204                         TEST_ASSERT_EQUAL(retval, 0, "Received packets unexpectedly");
1205
1206                         retval = rte_eth_tx_burst(test_params.bonded_port_id, 0, NULL, 0);
1207                         TEST_ASSERT_EQUAL(retval, 0,
1208                                 "Requested TX of 0 packets but %d transmitted", retval);
1209
1210                         /* Check if LACP packet was send by state machines
1211                            First and only packet must be a maker response */
1212                         retval = slave_get_pkts(slave, pkts, MAX_PKT_BURST);
1213                         if (retval == 0)
1214                                 continue;
1215                         if (retval > 1)
1216                                 free_pkts(pkts, retval);
1217
1218                         TEST_ASSERT_EQUAL(retval, 1, "failed to get slave packets");
1219                         nb_pkts = retval;
1220
1221                         marker_hdr = rte_pktmbuf_mtod(pkts[0], struct marker_header *);
1222                         /* Check if it's slow packet*/
1223                         if (marker_hdr->eth_hdr.ether_type != ethtype_slow_be)
1224                                 retval = -1;
1225                         /* Check if it's marker packet */
1226                         else if (marker_hdr->marker.subtype != SLOW_SUBTYPE_MARKER)
1227                                 retval = -2;
1228                         else if (marker_hdr->marker.tlv_type_marker != MARKER_TLV_TYPE_RESP)
1229                                 retval = -3;
1230
1231                         free_pkts(pkts, nb_pkts);
1232
1233                         TEST_ASSERT_NOT_EQUAL(retval, -1, "Unexpected protocol type");
1234                         TEST_ASSERT_NOT_EQUAL(retval, -2, "Unexpected sub protocol type");
1235                         TEST_ASSERT_NOT_EQUAL(retval, -3, "Unexpected marker type");
1236                         break;
1237                 }
1238
1239                 TEST_ASSERT(j < 20, "Marker response not found");
1240         }
1241
1242         retval = remove_slaves_and_stop_bonded_device();
1243         TEST_ASSERT_SUCCESS(retval,     "Test cleanup failed.");
1244
1245         return TEST_SUCCESS;
1246 }
1247
1248 static int
1249 test_mode4_expired(void)
1250 {
1251         struct slave_conf *slave, *exp_slave = NULL;
1252         struct rte_mbuf *pkts[MAX_PKT_BURST];
1253         int retval;
1254         uint32_t old_delay;
1255
1256         uint8_t i;
1257         uint16_t j;
1258
1259         struct rte_eth_bond_8023ad_conf conf;
1260
1261         retval = initialize_bonded_device_with_slaves(TEST_EXPIRED_SLAVE_COUNT,
1262                                                       0);
1263         /* Set custom timeouts to make test last shorter. */
1264         rte_eth_bond_8023ad_conf_get(test_params.bonded_port_id, &conf);
1265         conf.fast_periodic_ms = 100;
1266         conf.slow_periodic_ms = 600;
1267         conf.short_timeout_ms = 300;
1268         conf.long_timeout_ms = 900;
1269         conf.aggregate_wait_timeout_ms = 200;
1270         conf.tx_period_ms = 100;
1271         old_delay = conf.update_timeout_ms;
1272         conf.update_timeout_ms = 10;
1273         rte_eth_bond_8023ad_setup(test_params.bonded_port_id, &conf);
1274
1275         /* Wait for new settings to be applied. */
1276         for (i = 0; i < old_delay/conf.update_timeout_ms * 2; i++) {
1277                 FOR_EACH_SLAVE(j, slave)
1278                         bond_handshake_reply(slave);
1279
1280                 rte_delay_ms(conf.update_timeout_ms);
1281         }
1282
1283         retval = bond_handshake();
1284         TEST_ASSERT_SUCCESS(retval, "Initial handshake failed");
1285
1286         /* Find first slave */
1287         FOR_EACH_SLAVE(i, slave) {
1288                 exp_slave = slave;
1289                 break;
1290         }
1291
1292         RTE_VERIFY(exp_slave != NULL);
1293
1294         /* When one of partners do not send or respond to LACP frame in
1295          * conf.long_timeout_ms time, internal state machines should detect this
1296          * and transit to expired state. */
1297         for (j = 0; j < conf.long_timeout_ms/conf.update_timeout_ms + 2; j++) {
1298                 rte_delay_ms(conf.update_timeout_ms);
1299
1300                 retval = bond_tx(NULL, 0);
1301                 TEST_ASSERT_EQUAL(retval, 0, "Unexpectedly received %d packets",
1302                         retval);
1303
1304                 FOR_EACH_SLAVE(i, slave) {
1305                         retval = bond_handshake_reply(slave);
1306                         TEST_ASSERT(retval >= 0, "Handshake failed");
1307
1308                         /* Remove replay for slave that suppose to be expired. */
1309                         if (slave == exp_slave) {
1310                                 while (rte_ring_count(slave->rx_queue) > 0) {
1311                                         void *pkt = NULL;
1312
1313                                         rte_ring_dequeue(slave->rx_queue, &pkt);
1314                                         rte_pktmbuf_free(pkt);
1315                                 }
1316                         }
1317                 }
1318
1319                 retval = bond_rx(pkts, RTE_DIM(pkts));
1320                 if (retval > 0)
1321                         free_pkts(pkts, retval);
1322
1323                 TEST_ASSERT_EQUAL(retval, 0, "Unexpectedly received %d packets",
1324                         retval);
1325         }
1326
1327         /* After test only expected slave should be in EXPIRED state */
1328         FOR_EACH_SLAVE(i, slave) {
1329                 if (slave == exp_slave)
1330                         TEST_ASSERT(slave->lacp_parnter_state & STATE_EXPIRED,
1331                                 "Slave %u should be in expired.", slave->port_id);
1332                 else
1333                         TEST_ASSERT_EQUAL(bond_handshake_done(slave), 1,
1334                                 "Slave %u should be operational.", slave->port_id);
1335         }
1336
1337         retval = remove_slaves_and_stop_bonded_device();
1338         TEST_ASSERT_SUCCESS(retval, "Test cleanup failed.");
1339
1340         return TEST_SUCCESS;
1341 }
1342
1343 static int
1344 test_mode4_ext_ctrl(void)
1345 {
1346         /*
1347          * configure bonded interface without the external sm enabled
1348          *   . try to transmit lacpdu (should fail)
1349          *   . try to set collecting and distributing flags (should fail)
1350          * reconfigure w/external sm
1351          *   . transmit one lacpdu on each slave using new api
1352          *   . make sure each slave receives one lacpdu using the callback api
1353          *   . transmit one data pdu on each slave (should fail)
1354          *   . enable distribution and collection, send one data pdu each again
1355          */
1356
1357         int retval;
1358         struct slave_conf *slave = NULL;
1359         uint8_t i;
1360
1361         struct rte_mbuf *lacp_tx_buf[SLAVE_COUNT];
1362         struct rte_ether_addr src_mac, dst_mac;
1363         struct lacpdu_header lacpdu = {
1364                 .lacpdu = {
1365                         .subtype = SLOW_SUBTYPE_LACP,
1366                 },
1367         };
1368
1369         rte_ether_addr_copy(&parnter_system, &src_mac);
1370         rte_ether_addr_copy(&slow_protocol_mac_addr, &dst_mac);
1371
1372         initialize_eth_header(&lacpdu.eth_hdr, &src_mac, &dst_mac,
1373                               RTE_ETHER_TYPE_SLOW, 0, 0);
1374
1375         for (i = 0; i < SLAVE_COUNT; i++) {
1376                 lacp_tx_buf[i] = rte_pktmbuf_alloc(test_params.mbuf_pool);
1377                 rte_memcpy(rte_pktmbuf_mtod(lacp_tx_buf[i], char *),
1378                            &lacpdu, sizeof(lacpdu));
1379                 rte_pktmbuf_pkt_len(lacp_tx_buf[i]) = sizeof(lacpdu);
1380         }
1381
1382         retval = initialize_bonded_device_with_slaves(TEST_TX_SLAVE_COUNT, 0);
1383         TEST_ASSERT_SUCCESS(retval, "Failed to initialize bonded device");
1384
1385         FOR_EACH_SLAVE(i, slave) {
1386                 TEST_ASSERT_FAIL(rte_eth_bond_8023ad_ext_slowtx(
1387                                                 test_params.bonded_port_id,
1388                                                 slave->port_id, lacp_tx_buf[i]),
1389                                  "Slave should not allow manual LACP xmit");
1390                 TEST_ASSERT_FAIL(rte_eth_bond_8023ad_ext_collect(
1391                                                 test_params.bonded_port_id,
1392                                                 slave->port_id, 1),
1393                                  "Slave should not allow external state controls");
1394         }
1395
1396         free_pkts(lacp_tx_buf, RTE_DIM(lacp_tx_buf));
1397
1398         retval = remove_slaves_and_stop_bonded_device();
1399         TEST_ASSERT_SUCCESS(retval, "Bonded device cleanup failed.");
1400
1401         return TEST_SUCCESS;
1402 }
1403
1404
1405 static int
1406 test_mode4_ext_lacp(void)
1407 {
1408         int retval;
1409         struct slave_conf *slave = NULL;
1410         uint8_t all_slaves_done = 0, i;
1411         uint16_t nb_pkts;
1412         const unsigned int delay = bond_get_update_timeout_ms();
1413
1414         struct rte_mbuf *lacp_tx_buf[SLAVE_COUNT];
1415         struct rte_mbuf *buf[SLAVE_COUNT];
1416         struct rte_ether_addr src_mac, dst_mac;
1417         struct lacpdu_header lacpdu = {
1418                 .lacpdu = {
1419                         .subtype = SLOW_SUBTYPE_LACP,
1420                 },
1421         };
1422
1423         rte_ether_addr_copy(&parnter_system, &src_mac);
1424         rte_ether_addr_copy(&slow_protocol_mac_addr, &dst_mac);
1425
1426         initialize_eth_header(&lacpdu.eth_hdr, &src_mac, &dst_mac,
1427                               RTE_ETHER_TYPE_SLOW, 0, 0);
1428
1429         for (i = 0; i < SLAVE_COUNT; i++) {
1430                 lacp_tx_buf[i] = rte_pktmbuf_alloc(test_params.mbuf_pool);
1431                 rte_memcpy(rte_pktmbuf_mtod(lacp_tx_buf[i], char *),
1432                            &lacpdu, sizeof(lacpdu));
1433                 rte_pktmbuf_pkt_len(lacp_tx_buf[i]) = sizeof(lacpdu);
1434         }
1435
1436         retval = initialize_bonded_device_with_slaves(TEST_TX_SLAVE_COUNT, 1);
1437         TEST_ASSERT_SUCCESS(retval, "Failed to initialize bonded device");
1438
1439         memset(lacpdu_rx_count, 0, sizeof(lacpdu_rx_count));
1440
1441         /* Wait for new settings to be applied. */
1442         for (i = 0; i < 30; ++i)
1443                 rte_delay_ms(delay);
1444
1445         FOR_EACH_SLAVE(i, slave) {
1446                 retval = rte_eth_bond_8023ad_ext_slowtx(
1447                                                 test_params.bonded_port_id,
1448                                                 slave->port_id, lacp_tx_buf[i]);
1449                 TEST_ASSERT_SUCCESS(retval,
1450                                     "Slave should allow manual LACP xmit");
1451         }
1452
1453         nb_pkts = bond_tx(NULL, 0);
1454         TEST_ASSERT_EQUAL(nb_pkts, 0, "Packets transmitted unexpectedly");
1455
1456         FOR_EACH_SLAVE(i, slave) {
1457                 nb_pkts = slave_get_pkts(slave, buf, RTE_DIM(buf));
1458                 TEST_ASSERT_EQUAL(nb_pkts, 1, "found %u packets on slave %d\n",
1459                                   nb_pkts, i);
1460                 slave_put_pkts(slave, buf, nb_pkts);
1461         }
1462
1463         nb_pkts = bond_rx(buf, RTE_DIM(buf));
1464         free_pkts(buf, nb_pkts);
1465         TEST_ASSERT_EQUAL(nb_pkts, 0, "Packets received unexpectedly");
1466
1467         /* wait for the periodic callback to run */
1468         for (i = 0; i < 30 && all_slaves_done == 0; ++i) {
1469                 uint8_t s, total = 0;
1470
1471                 rte_delay_ms(delay);
1472                 FOR_EACH_SLAVE(s, slave) {
1473                         total += lacpdu_rx_count[slave->port_id];
1474                 }
1475
1476                 if (total >= SLAVE_COUNT)
1477                         all_slaves_done = 1;
1478         }
1479
1480         FOR_EACH_SLAVE(i, slave) {
1481                 TEST_ASSERT_EQUAL(lacpdu_rx_count[slave->port_id], 1,
1482                                   "Slave port %u should have received 1 lacpdu (count=%u)",
1483                                   slave->port_id,
1484                                   lacpdu_rx_count[slave->port_id]);
1485         }
1486
1487         retval = remove_slaves_and_stop_bonded_device();
1488         TEST_ASSERT_SUCCESS(retval, "Test cleanup failed.");
1489
1490         return TEST_SUCCESS;
1491 }
1492
1493 static int
1494 check_environment(void)
1495 {
1496         struct slave_conf *port;
1497         uint8_t i, env_state;
1498         uint16_t slaves[RTE_DIM(test_params.slave_ports)];
1499         int slaves_count;
1500
1501         env_state = 0;
1502         FOR_EACH_PORT(i, port) {
1503                 if (rte_ring_count(port->rx_queue) != 0)
1504                         env_state |= 0x01;
1505
1506                 if (rte_ring_count(port->tx_queue) != 0)
1507                         env_state |= 0x02;
1508
1509                 if (port->bonded != 0)
1510                         env_state |= 0x04;
1511
1512                 if (port->lacp_parnter_state != 0)
1513                         env_state |= 0x08;
1514
1515                 if (env_state != 0)
1516                         break;
1517         }
1518
1519         slaves_count = rte_eth_bond_slaves_get(test_params.bonded_port_id,
1520                         slaves, RTE_DIM(slaves));
1521
1522         if (slaves_count != 0)
1523                 env_state |= 0x10;
1524
1525         TEST_ASSERT_EQUAL(env_state, 0,
1526                 "Environment not clean (port %u):%s%s%s%s%s",
1527                 port->port_id,
1528                 env_state & 0x01 ? " slave rx queue not clean" : "",
1529                 env_state & 0x02 ? " slave tx queue not clean" : "",
1530                 env_state & 0x04 ? " port marked as enslaved" : "",
1531                 env_state & 0x80 ? " slave state is not reset" : "",
1532                 env_state & 0x10 ? " slave count not equal 0" : ".");
1533
1534
1535         return TEST_SUCCESS;
1536 }
1537
1538 static int
1539 test_mode4_executor(int (*test_func)(void))
1540 {
1541         struct slave_conf *port;
1542         int test_result;
1543         uint8_t i;
1544         void *pkt;
1545
1546         /* Check if environment is clean. Fail to launch a test if there was
1547          * a critical error before that prevented to reset environment. */
1548         TEST_ASSERT_SUCCESS(check_environment(),
1549                 "Refusing to launch test in dirty environment.");
1550
1551         RTE_VERIFY(test_func != NULL);
1552         test_result = (*test_func)();
1553
1554         /* If test succeed check if environment wast left in good condition. */
1555         if (test_result == TEST_SUCCESS)
1556                 test_result = check_environment();
1557
1558         /* Reset environment in case test failed to do that. */
1559         if (test_result != TEST_SUCCESS) {
1560                 TEST_ASSERT_SUCCESS(remove_slaves_and_stop_bonded_device(),
1561                         "Failed to stop bonded device");
1562
1563                 FOR_EACH_PORT(i, port) {
1564                         while (rte_ring_count(port->rx_queue) != 0) {
1565                                 if (rte_ring_dequeue(port->rx_queue, &pkt) == 0)
1566                                         rte_pktmbuf_free(pkt);
1567                         }
1568
1569                         while (rte_ring_count(port->tx_queue) != 0) {
1570                                 if (rte_ring_dequeue(port->tx_queue, &pkt) == 0)
1571                                         rte_pktmbuf_free(pkt);
1572                         }
1573                 }
1574         }
1575
1576         return test_result;
1577 }
1578
1579 static int
1580 test_mode4_agg_mode_selection_wrapper(void){
1581         return test_mode4_executor(&test_mode4_agg_mode_selection);
1582 }
1583
1584 static int
1585 test_mode4_lacp_wrapper(void)
1586 {
1587         return test_mode4_executor(&test_mode4_lacp);
1588 }
1589
1590 static int
1591 test_mode4_marker_wrapper(void)
1592 {
1593         return test_mode4_executor(&test_mode4_marker);
1594 }
1595
1596 static int
1597 test_mode4_rx_wrapper(void)
1598 {
1599         return test_mode4_executor(&test_mode4_rx);
1600 }
1601
1602 static int
1603 test_mode4_tx_burst_wrapper(void)
1604 {
1605         return test_mode4_executor(&test_mode4_tx_burst);
1606 }
1607
1608 static int
1609 test_mode4_expired_wrapper(void)
1610 {
1611         return test_mode4_executor(&test_mode4_expired);
1612 }
1613
1614 static int
1615 test_mode4_ext_ctrl_wrapper(void)
1616 {
1617         return test_mode4_executor(&test_mode4_ext_ctrl);
1618 }
1619
1620 static int
1621 test_mode4_ext_lacp_wrapper(void)
1622 {
1623         return test_mode4_executor(&test_mode4_ext_lacp);
1624 }
1625
1626 static struct unit_test_suite link_bonding_mode4_test_suite  = {
1627         .suite_name = "Link Bonding mode 4 Unit Test Suite",
1628         .setup = test_setup,
1629         .teardown = testsuite_teardown,
1630         .unit_test_cases = {
1631                 TEST_CASE_NAMED("test_mode4_agg_mode_selection",
1632                                 test_mode4_agg_mode_selection_wrapper),
1633                 TEST_CASE_NAMED("test_mode4_lacp", test_mode4_lacp_wrapper),
1634                 TEST_CASE_NAMED("test_mode4_rx", test_mode4_rx_wrapper),
1635                 TEST_CASE_NAMED("test_mode4_tx_burst", test_mode4_tx_burst_wrapper),
1636                 TEST_CASE_NAMED("test_mode4_marker", test_mode4_marker_wrapper),
1637                 TEST_CASE_NAMED("test_mode4_expired", test_mode4_expired_wrapper),
1638                 TEST_CASE_NAMED("test_mode4_ext_ctrl",
1639                                 test_mode4_ext_ctrl_wrapper),
1640                 TEST_CASE_NAMED("test_mode4_ext_lacp",
1641                                 test_mode4_ext_lacp_wrapper),
1642
1643                 TEST_CASES_END() /**< NULL terminate unit test array */
1644         }
1645 };
1646
1647 static int
1648 test_link_bonding_mode4(void)
1649 {
1650         return unit_test_suite_runner(&link_bonding_mode4_test_suite);
1651 }
1652
1653 REGISTER_TEST_COMMAND(link_bonding_mode4_autotest, test_link_bonding_mode4);