replace unused attributes
[dpdk.git] / examples / bond / main.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2010-2015 Intel Corporation
3  */
4
5 #include <stdint.h>
6 #include <sys/queue.h>
7 #include <sys/socket.h>
8 #include <stdlib.h>
9 #include <string.h>
10 #include <stdio.h>
11 #include <assert.h>
12 #include <errno.h>
13 #include <signal.h>
14 #include <stdarg.h>
15 #include <inttypes.h>
16 #include <getopt.h>
17 #include <termios.h>
18 #include <unistd.h>
19 #include <pthread.h>
20
21 #include <rte_common.h>
22 #include <rte_log.h>
23 #include <rte_memory.h>
24 #include <rte_memcpy.h>
25 #include <rte_eal.h>
26 #include <rte_launch.h>
27 #include <rte_atomic.h>
28 #include <rte_cycles.h>
29 #include <rte_prefetch.h>
30 #include <rte_lcore.h>
31 #include <rte_per_lcore.h>
32 #include <rte_branch_prediction.h>
33 #include <rte_interrupts.h>
34 #include <rte_random.h>
35 #include <rte_debug.h>
36 #include <rte_ether.h>
37 #include <rte_ethdev.h>
38 #include <rte_mempool.h>
39 #include <rte_mbuf.h>
40 #include <rte_ip.h>
41 #include <rte_tcp.h>
42 #include <rte_arp.h>
43 #include <rte_spinlock.h>
44
45 #include <cmdline_rdline.h>
46 #include <cmdline_parse.h>
47 #include <cmdline_parse_num.h>
48 #include <cmdline_parse_string.h>
49 #include <cmdline_parse_ipaddr.h>
50 #include <cmdline_parse_etheraddr.h>
51 #include <cmdline_socket.h>
52 #include <cmdline.h>
53
54 #include "main.h"
55
56 #include <rte_devargs.h>
57
58
59 #include "rte_byteorder.h"
60 #include "rte_cpuflags.h"
61 #include "rte_eth_bond.h"
62
63 #define RTE_LOGTYPE_DCB RTE_LOGTYPE_USER1
64
65 #define NB_MBUF   (1024*8)
66
67 #define MAX_PKT_BURST 32
68 #define BURST_TX_DRAIN_US 100      /* TX drain every ~100us */
69 #define BURST_RX_INTERVAL_NS (10) /* RX poll interval ~100ns */
70
71 /*
72  * RX and TX Prefetch, Host, and Write-back threshold values should be
73  * carefully set for optimal performance. Consult the network
74  * controller's datasheet and supporting DPDK documentation for guidance
75  * on how these parameters should be set.
76  */
77 #define RX_PTHRESH 8 /**< Default values of RX prefetch threshold reg. */
78 #define RX_HTHRESH 8 /**< Default values of RX host threshold reg. */
79 #define RX_WTHRESH 4 /**< Default values of RX write-back threshold reg. */
80 #define RX_FTHRESH (MAX_PKT_BURST * 2)/**< Default values of RX free threshold reg. */
81
82 /*
83  * These default values are optimized for use with the Intel(R) 82599 10 GbE
84  * Controller and the DPDK ixgbe PMD. Consider using other values for other
85  * network controllers and/or network drivers.
86  */
87 #define TX_PTHRESH 36 /**< Default values of TX prefetch threshold reg. */
88 #define TX_HTHRESH 0  /**< Default values of TX host threshold reg. */
89 #define TX_WTHRESH 0  /**< Default values of TX write-back threshold reg. */
90
91 /*
92  * Configurable number of RX/TX ring descriptors
93  */
94 #define RTE_RX_DESC_DEFAULT 1024
95 #define RTE_TX_DESC_DEFAULT 1024
96
97 #define BOND_IP_1       7
98 #define BOND_IP_2       0
99 #define BOND_IP_3       0
100 #define BOND_IP_4       10
101
102 /* not defined under linux */
103 #ifndef NIPQUAD
104 #define NIPQUAD_FMT "%u.%u.%u.%u"
105 #endif
106
107 #define MAX_PORTS       4
108 #define PRINT_MAC(addr)         printf("%02"PRIx8":%02"PRIx8":%02"PRIx8 \
109                 ":%02"PRIx8":%02"PRIx8":%02"PRIx8,      \
110                 addr.addr_bytes[0], addr.addr_bytes[1], addr.addr_bytes[2], \
111                 addr.addr_bytes[3], addr.addr_bytes[4], addr.addr_bytes[5])
112
113 uint16_t slaves[RTE_MAX_ETHPORTS];
114 uint16_t slaves_count;
115
116 static uint16_t BOND_PORT = 0xffff;
117
118 static struct rte_mempool *mbuf_pool;
119
120 static struct rte_eth_conf port_conf = {
121         .rxmode = {
122                 .mq_mode = ETH_MQ_RX_NONE,
123                 .max_rx_pkt_len = RTE_ETHER_MAX_LEN,
124                 .split_hdr_size = 0,
125         },
126         .rx_adv_conf = {
127                 .rss_conf = {
128                         .rss_key = NULL,
129                         .rss_hf = ETH_RSS_IP,
130                 },
131         },
132         .txmode = {
133                 .mq_mode = ETH_MQ_TX_NONE,
134         },
135 };
136
137 static void
138 slave_port_init(uint16_t portid, struct rte_mempool *mbuf_pool)
139 {
140         int retval;
141         uint16_t nb_rxd = RTE_RX_DESC_DEFAULT;
142         uint16_t nb_txd = RTE_TX_DESC_DEFAULT;
143         struct rte_eth_dev_info dev_info;
144         struct rte_eth_rxconf rxq_conf;
145         struct rte_eth_txconf txq_conf;
146         struct rte_eth_conf local_port_conf = port_conf;
147
148         if (!rte_eth_dev_is_valid_port(portid))
149                 rte_exit(EXIT_FAILURE, "Invalid port\n");
150
151         retval = rte_eth_dev_info_get(portid, &dev_info);
152         if (retval != 0)
153                 rte_exit(EXIT_FAILURE,
154                         "Error during getting device (port %u) info: %s\n",
155                         portid, strerror(-retval));
156
157         if (dev_info.tx_offload_capa & DEV_TX_OFFLOAD_MBUF_FAST_FREE)
158                 local_port_conf.txmode.offloads |=
159                         DEV_TX_OFFLOAD_MBUF_FAST_FREE;
160
161         local_port_conf.rx_adv_conf.rss_conf.rss_hf &=
162                 dev_info.flow_type_rss_offloads;
163         if (local_port_conf.rx_adv_conf.rss_conf.rss_hf !=
164                         port_conf.rx_adv_conf.rss_conf.rss_hf) {
165                 printf("Port %u modified RSS hash function based on hardware support,"
166                         "requested:%#"PRIx64" configured:%#"PRIx64"\n",
167                         portid,
168                         port_conf.rx_adv_conf.rss_conf.rss_hf,
169                         local_port_conf.rx_adv_conf.rss_conf.rss_hf);
170         }
171
172         retval = rte_eth_dev_configure(portid, 1, 1, &local_port_conf);
173         if (retval != 0)
174                 rte_exit(EXIT_FAILURE, "port %u: configuration failed (res=%d)\n",
175                                 portid, retval);
176
177         retval = rte_eth_dev_adjust_nb_rx_tx_desc(portid, &nb_rxd, &nb_txd);
178         if (retval != 0)
179                 rte_exit(EXIT_FAILURE, "port %u: rte_eth_dev_adjust_nb_rx_tx_desc "
180                                 "failed (res=%d)\n", portid, retval);
181
182         /* RX setup */
183         rxq_conf = dev_info.default_rxconf;
184         rxq_conf.offloads = local_port_conf.rxmode.offloads;
185         retval = rte_eth_rx_queue_setup(portid, 0, nb_rxd,
186                                         rte_eth_dev_socket_id(portid),
187                                         &rxq_conf,
188                                         mbuf_pool);
189         if (retval < 0)
190                 rte_exit(retval, " port %u: RX queue 0 setup failed (res=%d)",
191                                 portid, retval);
192
193         /* TX setup */
194         txq_conf = dev_info.default_txconf;
195         txq_conf.offloads = local_port_conf.txmode.offloads;
196         retval = rte_eth_tx_queue_setup(portid, 0, nb_txd,
197                                 rte_eth_dev_socket_id(portid), &txq_conf);
198
199         if (retval < 0)
200                 rte_exit(retval, "port %u: TX queue 0 setup failed (res=%d)",
201                                 portid, retval);
202
203         retval  = rte_eth_dev_start(portid);
204         if (retval < 0)
205                 rte_exit(retval,
206                                 "Start port %d failed (res=%d)",
207                                 portid, retval);
208
209         struct rte_ether_addr addr;
210
211         retval = rte_eth_macaddr_get(portid, &addr);
212         if (retval != 0)
213                 rte_exit(retval,
214                                 "Mac address get port %d failed (res=%d)",
215                                 portid, retval);
216
217         printf("Port %u MAC: ", portid);
218         PRINT_MAC(addr);
219         printf("\n");
220 }
221
222 static void
223 bond_port_init(struct rte_mempool *mbuf_pool)
224 {
225         int retval;
226         uint8_t i;
227         uint16_t nb_rxd = RTE_RX_DESC_DEFAULT;
228         uint16_t nb_txd = RTE_TX_DESC_DEFAULT;
229         struct rte_eth_dev_info dev_info;
230         struct rte_eth_rxconf rxq_conf;
231         struct rte_eth_txconf txq_conf;
232         struct rte_eth_conf local_port_conf = port_conf;
233         uint16_t wait_counter = 20;
234
235         retval = rte_eth_bond_create("net_bonding0", BONDING_MODE_ALB,
236                         0 /*SOCKET_ID_ANY*/);
237         if (retval < 0)
238                 rte_exit(EXIT_FAILURE,
239                                 "Faled to create bond port\n");
240
241         BOND_PORT = retval;
242
243         retval = rte_eth_dev_info_get(BOND_PORT, &dev_info);
244         if (retval != 0)
245                 rte_exit(EXIT_FAILURE,
246                         "Error during getting device (port %u) info: %s\n",
247                         BOND_PORT, strerror(-retval));
248
249         if (dev_info.tx_offload_capa & DEV_TX_OFFLOAD_MBUF_FAST_FREE)
250                 local_port_conf.txmode.offloads |=
251                         DEV_TX_OFFLOAD_MBUF_FAST_FREE;
252         retval = rte_eth_dev_configure(BOND_PORT, 1, 1, &local_port_conf);
253         if (retval != 0)
254                 rte_exit(EXIT_FAILURE, "port %u: configuration failed (res=%d)\n",
255                                 BOND_PORT, retval);
256
257         retval = rte_eth_dev_adjust_nb_rx_tx_desc(BOND_PORT, &nb_rxd, &nb_txd);
258         if (retval != 0)
259                 rte_exit(EXIT_FAILURE, "port %u: rte_eth_dev_adjust_nb_rx_tx_desc "
260                                 "failed (res=%d)\n", BOND_PORT, retval);
261
262         for (i = 0; i < slaves_count; i++) {
263                 if (rte_eth_bond_slave_add(BOND_PORT, slaves[i]) == -1)
264                         rte_exit(-1, "Oooops! adding slave (%u) to bond (%u) failed!\n",
265                                         slaves[i], BOND_PORT);
266
267         }
268
269         /* RX setup */
270         rxq_conf = dev_info.default_rxconf;
271         rxq_conf.offloads = local_port_conf.rxmode.offloads;
272         retval = rte_eth_rx_queue_setup(BOND_PORT, 0, nb_rxd,
273                                         rte_eth_dev_socket_id(BOND_PORT),
274                                         &rxq_conf, mbuf_pool);
275         if (retval < 0)
276                 rte_exit(retval, " port %u: RX queue 0 setup failed (res=%d)",
277                                 BOND_PORT, retval);
278
279         /* TX setup */
280         txq_conf = dev_info.default_txconf;
281         txq_conf.offloads = local_port_conf.txmode.offloads;
282         retval = rte_eth_tx_queue_setup(BOND_PORT, 0, nb_txd,
283                                 rte_eth_dev_socket_id(BOND_PORT), &txq_conf);
284
285         if (retval < 0)
286                 rte_exit(retval, "port %u: TX queue 0 setup failed (res=%d)",
287                                 BOND_PORT, retval);
288
289         retval  = rte_eth_dev_start(BOND_PORT);
290         if (retval < 0)
291                 rte_exit(retval, "Start port %d failed (res=%d)", BOND_PORT, retval);
292
293         printf("Waiting for slaves to become active...");
294         while (wait_counter) {
295                 uint16_t act_slaves[16] = {0};
296                 if (rte_eth_bond_active_slaves_get(BOND_PORT, act_slaves, 16) ==
297                                 slaves_count) {
298                         printf("\n");
299                         break;
300                 }
301                 sleep(1);
302                 printf("...");
303                 if (--wait_counter == 0)
304                         rte_exit(-1, "\nFailed to activate slaves\n");
305         }
306
307         retval = rte_eth_promiscuous_enable(BOND_PORT);
308         if (retval != 0) {
309                 rte_exit(EXIT_FAILURE,
310                                 "port %u: promiscuous mode enable failed: %s\n",
311                                 BOND_PORT, rte_strerror(-retval));
312                 return;
313         }
314
315         struct rte_ether_addr addr;
316
317         retval = rte_eth_macaddr_get(BOND_PORT, &addr);
318         if (retval != 0)
319                 rte_exit(retval, "port %u: Mac address get failed (res=%d)",
320                                 BOND_PORT, retval);
321
322         printf("Port %u MAC: ", (unsigned)BOND_PORT);
323                 PRINT_MAC(addr);
324                 printf("\n");
325 }
326
327 static inline size_t
328 get_vlan_offset(struct rte_ether_hdr *eth_hdr, uint16_t *proto)
329 {
330         size_t vlan_offset = 0;
331
332         if (rte_cpu_to_be_16(RTE_ETHER_TYPE_VLAN) == *proto) {
333                 struct rte_vlan_hdr *vlan_hdr =
334                         (struct rte_vlan_hdr *)(eth_hdr + 1);
335
336                 vlan_offset = sizeof(struct rte_vlan_hdr);
337                 *proto = vlan_hdr->eth_proto;
338
339                 if (rte_cpu_to_be_16(RTE_ETHER_TYPE_VLAN) == *proto) {
340                         vlan_hdr = vlan_hdr + 1;
341
342                         *proto = vlan_hdr->eth_proto;
343                         vlan_offset += sizeof(struct rte_vlan_hdr);
344                 }
345         }
346         return vlan_offset;
347 }
348
349 struct global_flag_stru_t {
350         int LcoreMainIsRunning;
351         int LcoreMainCore;
352         uint32_t port_packets[4];
353         rte_spinlock_t lock;
354 };
355 struct global_flag_stru_t global_flag_stru;
356 struct global_flag_stru_t *global_flag_stru_p = &global_flag_stru;
357
358 /*
359  * Main thread that does the work, reading from INPUT_PORT
360  * and writing to OUTPUT_PORT
361  */
362 static int lcore_main(__rte_unused void *arg1)
363 {
364         struct rte_mbuf *pkts[MAX_PKT_BURST] __rte_cache_aligned;
365         struct rte_ether_addr d_addr;
366
367         struct rte_ether_addr bond_mac_addr;
368         struct rte_ether_hdr *eth_hdr;
369         struct rte_arp_hdr *arp_hdr;
370         struct rte_ipv4_hdr *ipv4_hdr;
371         uint16_t ether_type, offset;
372
373         uint16_t rx_cnt;
374         uint32_t bond_ip;
375         int i = 0;
376         uint8_t is_free;
377         int ret;
378
379         bond_ip = BOND_IP_1 | (BOND_IP_2 << 8) |
380                                 (BOND_IP_3 << 16) | (BOND_IP_4 << 24);
381
382         rte_spinlock_trylock(&global_flag_stru_p->lock);
383
384         while (global_flag_stru_p->LcoreMainIsRunning) {
385                 rte_spinlock_unlock(&global_flag_stru_p->lock);
386                 rx_cnt = rte_eth_rx_burst(BOND_PORT, 0, pkts, MAX_PKT_BURST);
387                 is_free = 0;
388
389                 /* If didn't receive any packets, wait and go to next iteration */
390                 if (rx_cnt == 0) {
391                         rte_delay_us(50);
392                         continue;
393                 }
394
395                 ret = rte_eth_macaddr_get(BOND_PORT, &bond_mac_addr);
396                 if (ret != 0) {
397                         printf("Bond (port %u) MAC address get failed: %s.\n"
398                                "%u packets dropped", BOND_PORT, strerror(-ret),
399                                rx_cnt);
400                         rte_pktmbuf_free(pkts[i]);
401                         continue;
402                 }
403
404                 /* Search incoming data for ARP packets and prepare response */
405                 for (i = 0; i < rx_cnt; i++) {
406                         if (rte_spinlock_trylock(&global_flag_stru_p->lock) == 1) {
407                                 global_flag_stru_p->port_packets[0]++;
408                                 rte_spinlock_unlock(&global_flag_stru_p->lock);
409                         }
410                         eth_hdr = rte_pktmbuf_mtod(pkts[i],
411                                                 struct rte_ether_hdr *);
412                         ether_type = eth_hdr->ether_type;
413                         if (ether_type == rte_cpu_to_be_16(RTE_ETHER_TYPE_VLAN))
414                                 printf("VLAN taged frame, offset:");
415                         offset = get_vlan_offset(eth_hdr, &ether_type);
416                         if (offset > 0)
417                                 printf("%d\n", offset);
418                         if (ether_type == rte_cpu_to_be_16(RTE_ETHER_TYPE_ARP)) {
419                                 if (rte_spinlock_trylock(&global_flag_stru_p->lock) == 1)     {
420                                         global_flag_stru_p->port_packets[1]++;
421                                         rte_spinlock_unlock(&global_flag_stru_p->lock);
422                                 }
423                                 arp_hdr = (struct rte_arp_hdr *)(
424                                         (char *)(eth_hdr + 1) + offset);
425                                 if (arp_hdr->arp_data.arp_tip == bond_ip) {
426                                         if (arp_hdr->arp_opcode == rte_cpu_to_be_16(RTE_ARP_OP_REQUEST)) {
427                                                 arp_hdr->arp_opcode = rte_cpu_to_be_16(RTE_ARP_OP_REPLY);
428                                                 /* Switch src and dst data and set bonding MAC */
429                                                 rte_ether_addr_copy(&eth_hdr->s_addr, &eth_hdr->d_addr);
430                                                 rte_ether_addr_copy(&bond_mac_addr, &eth_hdr->s_addr);
431                                                 rte_ether_addr_copy(&arp_hdr->arp_data.arp_sha,
432                                                                 &arp_hdr->arp_data.arp_tha);
433                                                 arp_hdr->arp_data.arp_tip = arp_hdr->arp_data.arp_sip;
434                                                 rte_ether_addr_copy(&bond_mac_addr, &d_addr);
435                                                 rte_ether_addr_copy(&d_addr, &arp_hdr->arp_data.arp_sha);
436                                                 arp_hdr->arp_data.arp_sip = bond_ip;
437                                                 rte_eth_tx_burst(BOND_PORT, 0, &pkts[i], 1);
438                                                 is_free = 1;
439                                         } else {
440                                                 rte_eth_tx_burst(BOND_PORT, 0, NULL, 0);
441                                         }
442                                 }
443                         } else if (ether_type == rte_cpu_to_be_16(RTE_ETHER_TYPE_IPV4)) {
444                                 if (rte_spinlock_trylock(&global_flag_stru_p->lock) == 1)     {
445                                         global_flag_stru_p->port_packets[2]++;
446                                         rte_spinlock_unlock(&global_flag_stru_p->lock);
447                                  }
448                                 ipv4_hdr = (struct rte_ipv4_hdr *)((char *)(eth_hdr + 1) + offset);
449                                 if (ipv4_hdr->dst_addr == bond_ip) {
450                                         rte_ether_addr_copy(&eth_hdr->s_addr, &eth_hdr->d_addr);
451                                         rte_ether_addr_copy(&bond_mac_addr, &eth_hdr->s_addr);
452                                         ipv4_hdr->dst_addr = ipv4_hdr->src_addr;
453                                         ipv4_hdr->src_addr = bond_ip;
454                                         rte_eth_tx_burst(BOND_PORT, 0, &pkts[i], 1);
455                                 }
456
457                         }
458
459                         /* Free processed packets */
460                         if (is_free == 0)
461                                 rte_pktmbuf_free(pkts[i]);
462                 }
463                 rte_spinlock_trylock(&global_flag_stru_p->lock);
464         }
465         rte_spinlock_unlock(&global_flag_stru_p->lock);
466         printf("BYE lcore_main\n");
467         return 0;
468 }
469
470 struct cmd_obj_send_result {
471         cmdline_fixed_string_t action;
472         cmdline_ipaddr_t ip;
473 };
474 static inline void get_string(struct cmd_obj_send_result *res, char *buf, uint8_t size)
475 {
476         snprintf(buf, size, NIPQUAD_FMT,
477                 ((unsigned)((unsigned char *)&(res->ip.addr.ipv4))[0]),
478                 ((unsigned)((unsigned char *)&(res->ip.addr.ipv4))[1]),
479                 ((unsigned)((unsigned char *)&(res->ip.addr.ipv4))[2]),
480                 ((unsigned)((unsigned char *)&(res->ip.addr.ipv4))[3])
481                 );
482 }
483 static void cmd_obj_send_parsed(void *parsed_result,
484                 __rte_unused struct cmdline *cl,
485                                __rte_unused void *data)
486 {
487
488         struct cmd_obj_send_result *res = parsed_result;
489         char ip_str[INET6_ADDRSTRLEN];
490
491         struct rte_ether_addr bond_mac_addr;
492         struct rte_mbuf *created_pkt;
493         struct rte_ether_hdr *eth_hdr;
494         struct rte_arp_hdr *arp_hdr;
495
496         uint32_t bond_ip;
497         size_t pkt_size;
498         int ret;
499
500         if (res->ip.family == AF_INET)
501                 get_string(res, ip_str, INET_ADDRSTRLEN);
502         else
503                 cmdline_printf(cl, "Wrong IP format. Only IPv4 is supported\n");
504
505         bond_ip = BOND_IP_1 | (BOND_IP_2 << 8) |
506                                 (BOND_IP_3 << 16) | (BOND_IP_4 << 24);
507
508         ret = rte_eth_macaddr_get(BOND_PORT, &bond_mac_addr);
509         if (ret != 0) {
510                 cmdline_printf(cl,
511                                "Failed to get bond (port %u) MAC address: %s\n",
512                                BOND_PORT, strerror(-ret));
513         }
514
515         created_pkt = rte_pktmbuf_alloc(mbuf_pool);
516         if (created_pkt == NULL) {
517                 cmdline_printf(cl, "Failed to allocate mbuf\n");
518                 return;
519         }
520
521         pkt_size = sizeof(struct rte_ether_hdr) + sizeof(struct rte_arp_hdr);
522         created_pkt->data_len = pkt_size;
523         created_pkt->pkt_len = pkt_size;
524
525         eth_hdr = rte_pktmbuf_mtod(created_pkt, struct rte_ether_hdr *);
526         rte_ether_addr_copy(&bond_mac_addr, &eth_hdr->s_addr);
527         memset(&eth_hdr->d_addr, 0xFF, RTE_ETHER_ADDR_LEN);
528         eth_hdr->ether_type = rte_cpu_to_be_16(RTE_ETHER_TYPE_ARP);
529
530         arp_hdr = (struct rte_arp_hdr *)(
531                 (char *)eth_hdr + sizeof(struct rte_ether_hdr));
532         arp_hdr->arp_hardware = rte_cpu_to_be_16(RTE_ARP_HRD_ETHER);
533         arp_hdr->arp_protocol = rte_cpu_to_be_16(RTE_ETHER_TYPE_IPV4);
534         arp_hdr->arp_hlen = RTE_ETHER_ADDR_LEN;
535         arp_hdr->arp_plen = sizeof(uint32_t);
536         arp_hdr->arp_opcode = rte_cpu_to_be_16(RTE_ARP_OP_REQUEST);
537
538         rte_ether_addr_copy(&bond_mac_addr, &arp_hdr->arp_data.arp_sha);
539         arp_hdr->arp_data.arp_sip = bond_ip;
540         memset(&arp_hdr->arp_data.arp_tha, 0, RTE_ETHER_ADDR_LEN);
541         arp_hdr->arp_data.arp_tip =
542                           ((unsigned char *)&res->ip.addr.ipv4)[0]        |
543                          (((unsigned char *)&res->ip.addr.ipv4)[1] << 8)  |
544                          (((unsigned char *)&res->ip.addr.ipv4)[2] << 16) |
545                          (((unsigned char *)&res->ip.addr.ipv4)[3] << 24);
546         rte_eth_tx_burst(BOND_PORT, 0, &created_pkt, 1);
547
548         rte_delay_ms(100);
549         cmdline_printf(cl, "\n");
550 }
551
552 cmdline_parse_token_string_t cmd_obj_action_send =
553         TOKEN_STRING_INITIALIZER(struct cmd_obj_send_result, action, "send");
554 cmdline_parse_token_ipaddr_t cmd_obj_ip =
555         TOKEN_IPV4_INITIALIZER(struct cmd_obj_send_result, ip);
556
557 cmdline_parse_inst_t cmd_obj_send = {
558         .f = cmd_obj_send_parsed,  /* function to call */
559         .data = NULL,      /* 2nd arg of func */
560         .help_str = "send client_ip",
561         .tokens = {        /* token list, NULL terminated */
562                 (void *)&cmd_obj_action_send,
563                 (void *)&cmd_obj_ip,
564                 NULL,
565         },
566 };
567
568 struct cmd_start_result {
569         cmdline_fixed_string_t start;
570 };
571
572 static void cmd_start_parsed(__rte_unused void *parsed_result,
573                                struct cmdline *cl,
574                                __rte_unused void *data)
575 {
576         int slave_core_id = rte_lcore_id();
577
578         rte_spinlock_trylock(&global_flag_stru_p->lock);
579         if (global_flag_stru_p->LcoreMainIsRunning == 0) {
580                 if (rte_eal_get_lcore_state(global_flag_stru_p->LcoreMainCore)
581                     != WAIT) {
582                         rte_spinlock_unlock(&global_flag_stru_p->lock);
583                         return;
584                 }
585                 rte_spinlock_unlock(&global_flag_stru_p->lock);
586         } else {
587                 cmdline_printf(cl, "lcore_main already running on core:%d\n",
588                                 global_flag_stru_p->LcoreMainCore);
589                 rte_spinlock_unlock(&global_flag_stru_p->lock);
590                 return;
591         }
592
593         /* start lcore main on core != master_core - ARP response thread */
594         slave_core_id = rte_get_next_lcore(rte_lcore_id(), 1, 0);
595         if ((slave_core_id >= RTE_MAX_LCORE) || (slave_core_id == 0))
596                 return;
597
598         rte_spinlock_trylock(&global_flag_stru_p->lock);
599         global_flag_stru_p->LcoreMainIsRunning = 1;
600         rte_spinlock_unlock(&global_flag_stru_p->lock);
601         cmdline_printf(cl,
602                         "Starting lcore_main on core %d:%d "
603                         "Our IP:%d.%d.%d.%d\n",
604                         slave_core_id,
605                         rte_eal_remote_launch(lcore_main, NULL, slave_core_id),
606                         BOND_IP_1,
607                         BOND_IP_2,
608                         BOND_IP_3,
609                         BOND_IP_4
610                 );
611 }
612
613 cmdline_parse_token_string_t cmd_start_start =
614         TOKEN_STRING_INITIALIZER(struct cmd_start_result, start, "start");
615
616 cmdline_parse_inst_t cmd_start = {
617         .f = cmd_start_parsed,  /* function to call */
618         .data = NULL,      /* 2nd arg of func */
619         .help_str = "starts listening if not started at startup",
620         .tokens = {        /* token list, NULL terminated */
621                 (void *)&cmd_start_start,
622                 NULL,
623         },
624 };
625
626 struct cmd_help_result {
627         cmdline_fixed_string_t help;
628 };
629
630 static void cmd_help_parsed(__rte_unused void *parsed_result,
631                             struct cmdline *cl,
632                             __rte_unused void *data)
633 {
634         cmdline_printf(cl,
635                         "ALB - link bonding mode 6 example\n"
636                         "send IP        - sends one ARPrequest through bonding for IP.\n"
637                         "start          - starts listening ARPs.\n"
638                         "stop           - stops lcore_main.\n"
639                         "show           - shows some bond info: ex. active slaves etc.\n"
640                         "help           - prints help.\n"
641                         "quit           - terminate all threads and quit.\n"
642                        );
643 }
644
645 cmdline_parse_token_string_t cmd_help_help =
646         TOKEN_STRING_INITIALIZER(struct cmd_help_result, help, "help");
647
648 cmdline_parse_inst_t cmd_help = {
649         .f = cmd_help_parsed,  /* function to call */
650         .data = NULL,      /* 2nd arg of func */
651         .help_str = "show help",
652         .tokens = {        /* token list, NULL terminated */
653                 (void *)&cmd_help_help,
654                 NULL,
655         },
656 };
657
658 struct cmd_stop_result {
659         cmdline_fixed_string_t stop;
660 };
661
662 static void cmd_stop_parsed(__rte_unused void *parsed_result,
663                             struct cmdline *cl,
664                             __rte_unused void *data)
665 {
666         rte_spinlock_trylock(&global_flag_stru_p->lock);
667         if (global_flag_stru_p->LcoreMainIsRunning == 0)        {
668                 cmdline_printf(cl,
669                                         "lcore_main not running on core:%d\n",
670                                         global_flag_stru_p->LcoreMainCore);
671                 rte_spinlock_unlock(&global_flag_stru_p->lock);
672                 return;
673         }
674         global_flag_stru_p->LcoreMainIsRunning = 0;
675         if (rte_eal_wait_lcore(global_flag_stru_p->LcoreMainCore) < 0)
676                 cmdline_printf(cl,
677                                 "error: lcore_main can not stop on core:%d\n",
678                                 global_flag_stru_p->LcoreMainCore);
679         else
680                 cmdline_printf(cl,
681                                 "lcore_main stopped on core:%d\n",
682                                 global_flag_stru_p->LcoreMainCore);
683         rte_spinlock_unlock(&global_flag_stru_p->lock);
684 }
685
686 cmdline_parse_token_string_t cmd_stop_stop =
687         TOKEN_STRING_INITIALIZER(struct cmd_stop_result, stop, "stop");
688
689 cmdline_parse_inst_t cmd_stop = {
690         .f = cmd_stop_parsed,  /* function to call */
691         .data = NULL,      /* 2nd arg of func */
692         .help_str = "this command do not handle any arguments",
693         .tokens = {        /* token list, NULL terminated */
694                 (void *)&cmd_stop_stop,
695                 NULL,
696         },
697 };
698
699 struct cmd_quit_result {
700         cmdline_fixed_string_t quit;
701 };
702
703 static void cmd_quit_parsed(__rte_unused void *parsed_result,
704                             struct cmdline *cl,
705                             __rte_unused void *data)
706 {
707         rte_spinlock_trylock(&global_flag_stru_p->lock);
708         if (global_flag_stru_p->LcoreMainIsRunning == 0)        {
709                 cmdline_printf(cl,
710                                         "lcore_main not running on core:%d\n",
711                                         global_flag_stru_p->LcoreMainCore);
712                 rte_spinlock_unlock(&global_flag_stru_p->lock);
713                 cmdline_quit(cl);
714                 return;
715         }
716         global_flag_stru_p->LcoreMainIsRunning = 0;
717         if (rte_eal_wait_lcore(global_flag_stru_p->LcoreMainCore) < 0)
718                 cmdline_printf(cl,
719                                 "error: lcore_main can not stop on core:%d\n",
720                                 global_flag_stru_p->LcoreMainCore);
721         else
722                 cmdline_printf(cl,
723                                 "lcore_main stopped on core:%d\n",
724                                 global_flag_stru_p->LcoreMainCore);
725         rte_spinlock_unlock(&global_flag_stru_p->lock);
726         cmdline_quit(cl);
727 }
728
729 cmdline_parse_token_string_t cmd_quit_quit =
730         TOKEN_STRING_INITIALIZER(struct cmd_quit_result, quit, "quit");
731
732 cmdline_parse_inst_t cmd_quit = {
733         .f = cmd_quit_parsed,  /* function to call */
734         .data = NULL,      /* 2nd arg of func */
735         .help_str = "this command do not handle any arguments",
736         .tokens = {        /* token list, NULL terminated */
737                 (void *)&cmd_quit_quit,
738                 NULL,
739         },
740 };
741
742 struct cmd_show_result {
743         cmdline_fixed_string_t show;
744 };
745
746 static void cmd_show_parsed(__rte_unused void *parsed_result,
747                             struct cmdline *cl,
748                             __rte_unused void *data)
749 {
750         uint16_t slaves[16] = {0};
751         uint8_t len = 16;
752         struct rte_ether_addr addr;
753         uint16_t i;
754         int ret;
755
756         for (i = 0; i < slaves_count; i++) {
757                 ret = rte_eth_macaddr_get(i, &addr);
758                 if (ret != 0) {
759                         cmdline_printf(cl,
760                                 "Failed to get port %u MAC address: %s\n",
761                                 i, strerror(-ret));
762                         continue;
763                 }
764
765                 PRINT_MAC(addr);
766                 printf("\n");
767         }
768
769         rte_spinlock_trylock(&global_flag_stru_p->lock);
770         cmdline_printf(cl,
771                         "Active_slaves:%d "
772                         "packets received:Tot:%d Arp:%d IPv4:%d\n",
773                         rte_eth_bond_active_slaves_get(BOND_PORT, slaves, len),
774                         global_flag_stru_p->port_packets[0],
775                         global_flag_stru_p->port_packets[1],
776                         global_flag_stru_p->port_packets[2]);
777         rte_spinlock_unlock(&global_flag_stru_p->lock);
778 }
779
780 cmdline_parse_token_string_t cmd_show_show =
781         TOKEN_STRING_INITIALIZER(struct cmd_show_result, show, "show");
782
783 cmdline_parse_inst_t cmd_show = {
784         .f = cmd_show_parsed,  /* function to call */
785         .data = NULL,      /* 2nd arg of func */
786         .help_str = "this command do not handle any arguments",
787         .tokens = {        /* token list, NULL terminated */
788                 (void *)&cmd_show_show,
789                 NULL,
790         },
791 };
792
793 /****** CONTEXT (list of instruction) */
794
795 cmdline_parse_ctx_t main_ctx[] = {
796         (cmdline_parse_inst_t *)&cmd_start,
797         (cmdline_parse_inst_t *)&cmd_obj_send,
798         (cmdline_parse_inst_t *)&cmd_stop,
799         (cmdline_parse_inst_t *)&cmd_show,
800         (cmdline_parse_inst_t *)&cmd_quit,
801         (cmdline_parse_inst_t *)&cmd_help,
802         NULL,
803 };
804
805 /* prompt function, called from main on MASTER lcore */
806 static void prompt(__rte_unused void *arg1)
807 {
808         struct cmdline *cl;
809
810         cl = cmdline_stdin_new(main_ctx, "bond6>");
811         if (cl != NULL) {
812                 cmdline_interact(cl);
813                 cmdline_stdin_exit(cl);
814         }
815 }
816
817 /* Main function, does initialisation and calls the per-lcore functions */
818 int
819 main(int argc, char *argv[])
820 {
821         int ret, slave_core_id;
822         uint16_t nb_ports, i;
823
824         /* init EAL */
825         ret = rte_eal_init(argc, argv);
826         rte_devargs_dump(stdout);
827         if (ret < 0)
828                 rte_exit(EXIT_FAILURE, "Error with EAL initialization\n");
829         argc -= ret;
830         argv += ret;
831
832         nb_ports = rte_eth_dev_count_avail();
833         if (nb_ports == 0)
834                 rte_exit(EXIT_FAILURE, "Give at least one port\n");
835         else if (nb_ports > MAX_PORTS)
836                 rte_exit(EXIT_FAILURE, "You can have max 4 ports\n");
837
838         mbuf_pool = rte_pktmbuf_pool_create("MBUF_POOL", NB_MBUF, 32,
839                 0, RTE_MBUF_DEFAULT_BUF_SIZE, rte_socket_id());
840         if (mbuf_pool == NULL)
841                 rte_exit(EXIT_FAILURE, "Cannot create mbuf pool\n");
842
843         /* initialize all ports */
844         slaves_count = nb_ports;
845         RTE_ETH_FOREACH_DEV(i) {
846                 slave_port_init(i, mbuf_pool);
847                 slaves[i] = i;
848         }
849
850         bond_port_init(mbuf_pool);
851
852         rte_spinlock_init(&global_flag_stru_p->lock);
853
854         /* check state of lcores */
855         RTE_LCORE_FOREACH_SLAVE(slave_core_id) {
856                 if (rte_eal_get_lcore_state(slave_core_id) != WAIT)
857                         return -EBUSY;
858         }
859
860         /* start lcore main on core != master_core - ARP response thread */
861         slave_core_id = rte_get_next_lcore(rte_lcore_id(), 1, 0);
862         if ((slave_core_id >= RTE_MAX_LCORE) || (slave_core_id == 0))
863                 return -EPERM;
864
865         global_flag_stru_p->LcoreMainIsRunning = 1;
866         global_flag_stru_p->LcoreMainCore = slave_core_id;
867         printf("Starting lcore_main on core %d:%d Our IP:%d.%d.%d.%d\n",
868                         slave_core_id,
869                         rte_eal_remote_launch((lcore_function_t *)lcore_main,
870                                         NULL,
871                                         slave_core_id),
872                         BOND_IP_1,
873                         BOND_IP_2,
874                         BOND_IP_3,
875                         BOND_IP_4
876                 );
877
878         /* Start prompt for user interact */
879         prompt(NULL);
880
881         rte_delay_ms(100);
882         return 0;
883 }