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