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