b8efcafca3cdc1eb3ee03f2ccde10eef76c60b05
[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 = ETHER_MAX_LEN,
124                 .split_hdr_size = 0,
125                 .ignore_offload_bitfield = 1,
126                 .offloads = DEV_RX_OFFLOAD_CRC_STRIP,
127         },
128         .rx_adv_conf = {
129                 .rss_conf = {
130                         .rss_key = NULL,
131                         .rss_hf = ETH_RSS_IP,
132                 },
133         },
134         .txmode = {
135                 .mq_mode = ETH_MQ_TX_NONE,
136         },
137 };
138
139 static void
140 slave_port_init(uint16_t portid, struct rte_mempool *mbuf_pool)
141 {
142         int retval;
143         uint16_t nb_rxd = RTE_RX_DESC_DEFAULT;
144         uint16_t nb_txd = RTE_TX_DESC_DEFAULT;
145         struct rte_eth_dev_info dev_info;
146         struct rte_eth_rxconf rxq_conf;
147         struct rte_eth_txconf txq_conf;
148         struct rte_eth_conf local_port_conf = port_conf;
149
150         if (portid >= rte_eth_dev_count())
151                 rte_exit(EXIT_FAILURE, "Invalid port\n");
152
153         rte_eth_dev_info_get(portid, &dev_info);
154         if (dev_info.tx_offload_capa & DEV_TX_OFFLOAD_MBUF_FAST_FREE)
155                 local_port_conf.txmode.offloads |=
156                         DEV_TX_OFFLOAD_MBUF_FAST_FREE;
157         retval = rte_eth_dev_configure(portid, 1, 1, &local_port_conf);
158         if (retval != 0)
159                 rte_exit(EXIT_FAILURE, "port %u: configuration failed (res=%d)\n",
160                                 portid, retval);
161
162         retval = rte_eth_dev_adjust_nb_rx_tx_desc(portid, &nb_rxd, &nb_txd);
163         if (retval != 0)
164                 rte_exit(EXIT_FAILURE, "port %u: rte_eth_dev_adjust_nb_rx_tx_desc "
165                                 "failed (res=%d)\n", portid, retval);
166
167         /* RX setup */
168         rxq_conf = dev_info.default_rxconf;
169         rxq_conf.offloads = local_port_conf.rxmode.offloads;
170         retval = rte_eth_rx_queue_setup(portid, 0, nb_rxd,
171                                         rte_eth_dev_socket_id(portid),
172                                         &rxq_conf,
173                                         mbuf_pool);
174         if (retval < 0)
175                 rte_exit(retval, " port %u: RX queue 0 setup failed (res=%d)",
176                                 portid, retval);
177
178         /* TX setup */
179         txq_conf = dev_info.default_txconf;
180         txq_conf.txq_flags = ETH_TXQ_FLAGS_IGNORE;
181         txq_conf.offloads = local_port_conf.txmode.offloads;
182         retval = rte_eth_tx_queue_setup(portid, 0, nb_txd,
183                                 rte_eth_dev_socket_id(portid), &txq_conf);
184
185         if (retval < 0)
186                 rte_exit(retval, "port %u: TX queue 0 setup failed (res=%d)",
187                                 portid, retval);
188
189         retval  = rte_eth_dev_start(portid);
190         if (retval < 0)
191                 rte_exit(retval,
192                                 "Start port %d failed (res=%d)",
193                                 portid, retval);
194
195         struct ether_addr addr;
196
197         rte_eth_macaddr_get(portid, &addr);
198         printf("Port %u MAC: ", portid);
199         PRINT_MAC(addr);
200         printf("\n");
201 }
202
203 static void
204 bond_port_init(struct rte_mempool *mbuf_pool)
205 {
206         int retval;
207         uint8_t i;
208         uint16_t nb_rxd = RTE_RX_DESC_DEFAULT;
209         uint16_t nb_txd = RTE_TX_DESC_DEFAULT;
210         struct rte_eth_dev_info dev_info;
211         struct rte_eth_rxconf rxq_conf;
212         struct rte_eth_txconf txq_conf;
213         struct rte_eth_conf local_port_conf = port_conf;
214
215         retval = rte_eth_bond_create("net_bonding0", BONDING_MODE_ALB,
216                         0 /*SOCKET_ID_ANY*/);
217         if (retval < 0)
218                 rte_exit(EXIT_FAILURE,
219                                 "Faled to create bond port\n");
220
221         BOND_PORT = retval;
222
223         rte_eth_dev_info_get(BOND_PORT, &dev_info);
224         if (dev_info.tx_offload_capa & DEV_TX_OFFLOAD_MBUF_FAST_FREE)
225                 local_port_conf.txmode.offloads |=
226                         DEV_TX_OFFLOAD_MBUF_FAST_FREE;
227         retval = rte_eth_dev_configure(BOND_PORT, 1, 1, &local_port_conf);
228         if (retval != 0)
229                 rte_exit(EXIT_FAILURE, "port %u: configuration failed (res=%d)\n",
230                                 BOND_PORT, retval);
231
232         retval = rte_eth_dev_adjust_nb_rx_tx_desc(BOND_PORT, &nb_rxd, &nb_txd);
233         if (retval != 0)
234                 rte_exit(EXIT_FAILURE, "port %u: rte_eth_dev_adjust_nb_rx_tx_desc "
235                                 "failed (res=%d)\n", BOND_PORT, retval);
236
237         /* RX setup */
238         rxq_conf = dev_info.default_rxconf;
239         rxq_conf.offloads = local_port_conf.rxmode.offloads;
240         retval = rte_eth_rx_queue_setup(BOND_PORT, 0, nb_rxd,
241                                         rte_eth_dev_socket_id(BOND_PORT),
242                                         &rxq_conf, mbuf_pool);
243         if (retval < 0)
244                 rte_exit(retval, " port %u: RX queue 0 setup failed (res=%d)",
245                                 BOND_PORT, retval);
246
247         /* TX setup */
248         txq_conf = dev_info.default_txconf;
249         txq_conf.txq_flags = ETH_TXQ_FLAGS_IGNORE;
250         txq_conf.offloads = local_port_conf.txmode.offloads;
251         retval = rte_eth_tx_queue_setup(BOND_PORT, 0, nb_txd,
252                                 rte_eth_dev_socket_id(BOND_PORT), &txq_conf);
253
254         if (retval < 0)
255                 rte_exit(retval, "port %u: TX queue 0 setup failed (res=%d)",
256                                 BOND_PORT, retval);
257
258         for (i = 0; i < slaves_count; i++) {
259                 if (rte_eth_bond_slave_add(BOND_PORT, slaves[i]) == -1)
260                         rte_exit(-1, "Oooops! adding slave (%u) to bond (%u) failed!\n",
261                                         slaves[i], BOND_PORT);
262
263         }
264
265         retval  = rte_eth_dev_start(BOND_PORT);
266         if (retval < 0)
267                 rte_exit(retval, "Start port %d failed (res=%d)", BOND_PORT, retval);
268
269         rte_eth_promiscuous_enable(BOND_PORT);
270
271         struct ether_addr addr;
272
273         rte_eth_macaddr_get(BOND_PORT, &addr);
274         printf("Port %u MAC: ", (unsigned)BOND_PORT);
275                 PRINT_MAC(addr);
276                 printf("\n");
277 }
278
279 static inline size_t
280 get_vlan_offset(struct ether_hdr *eth_hdr, uint16_t *proto)
281 {
282         size_t vlan_offset = 0;
283
284         if (rte_cpu_to_be_16(ETHER_TYPE_VLAN) == *proto) {
285                 struct vlan_hdr *vlan_hdr = (struct vlan_hdr *)(eth_hdr + 1);
286
287                 vlan_offset = sizeof(struct vlan_hdr);
288                 *proto = vlan_hdr->eth_proto;
289
290                 if (rte_cpu_to_be_16(ETHER_TYPE_VLAN) == *proto) {
291                         vlan_hdr = vlan_hdr + 1;
292
293                         *proto = vlan_hdr->eth_proto;
294                         vlan_offset += sizeof(struct vlan_hdr);
295                 }
296         }
297         return vlan_offset;
298 }
299
300 struct global_flag_stru_t {
301         int LcoreMainIsRunning;
302         int LcoreMainCore;
303         uint32_t port_packets[4];
304         rte_spinlock_t lock;
305 };
306 struct global_flag_stru_t global_flag_stru;
307 struct global_flag_stru_t *global_flag_stru_p = &global_flag_stru;
308
309 /*
310  * Main thread that does the work, reading from INPUT_PORT
311  * and writing to OUTPUT_PORT
312  */
313 static int lcore_main(__attribute__((unused)) void *arg1)
314 {
315         struct rte_mbuf *pkts[MAX_PKT_BURST] __rte_cache_aligned;
316         struct ether_addr d_addr;
317
318         struct ether_hdr *eth_hdr;
319         struct arp_hdr *arp_hdr;
320         struct ipv4_hdr *ipv4_hdr;
321         uint16_t ether_type, offset;
322
323         uint16_t rx_cnt;
324         uint32_t bond_ip;
325         int i = 0;
326         uint8_t is_free;
327
328         bond_ip = BOND_IP_1 | (BOND_IP_2 << 8) |
329                                 (BOND_IP_3 << 16) | (BOND_IP_4 << 24);
330
331         rte_spinlock_trylock(&global_flag_stru_p->lock);
332
333         while (global_flag_stru_p->LcoreMainIsRunning) {
334                 rte_spinlock_unlock(&global_flag_stru_p->lock);
335                 rx_cnt = rte_eth_rx_burst(BOND_PORT, 0, pkts, MAX_PKT_BURST);
336                 is_free = 0;
337
338                 /* If didn't receive any packets, wait and go to next iteration */
339                 if (rx_cnt == 0) {
340                         rte_delay_us(50);
341                         continue;
342                 }
343
344                 /* Search incoming data for ARP packets and prepare response */
345                 for (i = 0; i < rx_cnt; i++) {
346                         if (rte_spinlock_trylock(&global_flag_stru_p->lock) == 1) {
347                                 global_flag_stru_p->port_packets[0]++;
348                                 rte_spinlock_unlock(&global_flag_stru_p->lock);
349                         }
350                         eth_hdr = rte_pktmbuf_mtod(pkts[i], struct ether_hdr *);
351                         ether_type = eth_hdr->ether_type;
352                         if (ether_type == rte_cpu_to_be_16(ETHER_TYPE_VLAN))
353                                 printf("VLAN taged frame, offset:");
354                         offset = get_vlan_offset(eth_hdr, &ether_type);
355                         if (offset > 0)
356                                 printf("%d\n", offset);
357                         if (ether_type == rte_cpu_to_be_16(ETHER_TYPE_ARP)) {
358                                 if (rte_spinlock_trylock(&global_flag_stru_p->lock) == 1)     {
359                                         global_flag_stru_p->port_packets[1]++;
360                                         rte_spinlock_unlock(&global_flag_stru_p->lock);
361                                 }
362                                 arp_hdr = (struct arp_hdr *)((char *)(eth_hdr + 1) + offset);
363                                 if (arp_hdr->arp_data.arp_tip == bond_ip) {
364                                         if (arp_hdr->arp_op == rte_cpu_to_be_16(ARP_OP_REQUEST)) {
365                                                 arp_hdr->arp_op = rte_cpu_to_be_16(ARP_OP_REPLY);
366                                                 /* Switch src and dst data and set bonding MAC */
367                                                 ether_addr_copy(&eth_hdr->s_addr, &eth_hdr->d_addr);
368                                                 rte_eth_macaddr_get(BOND_PORT, &eth_hdr->s_addr);
369                                                 ether_addr_copy(&arp_hdr->arp_data.arp_sha, &arp_hdr->arp_data.arp_tha);
370                                                 arp_hdr->arp_data.arp_tip = arp_hdr->arp_data.arp_sip;
371                                                 rte_eth_macaddr_get(BOND_PORT, &d_addr);
372                                                 ether_addr_copy(&d_addr, &arp_hdr->arp_data.arp_sha);
373                                                 arp_hdr->arp_data.arp_sip = bond_ip;
374                                                 rte_eth_tx_burst(BOND_PORT, 0, &pkts[i], 1);
375                                                 is_free = 1;
376                                         } else {
377                                                 rte_eth_tx_burst(BOND_PORT, 0, NULL, 0);
378                                         }
379                                 }
380                         } else if (ether_type == rte_cpu_to_be_16(ETHER_TYPE_IPv4)) {
381                                 if (rte_spinlock_trylock(&global_flag_stru_p->lock) == 1)     {
382                                         global_flag_stru_p->port_packets[2]++;
383                                         rte_spinlock_unlock(&global_flag_stru_p->lock);
384                                  }
385                                 ipv4_hdr = (struct ipv4_hdr *)((char *)(eth_hdr + 1) + offset);
386                                 if (ipv4_hdr->dst_addr == bond_ip) {
387                                         ether_addr_copy(&eth_hdr->s_addr, &eth_hdr->d_addr);
388                                         rte_eth_macaddr_get(BOND_PORT, &eth_hdr->s_addr);
389                                         ipv4_hdr->dst_addr = ipv4_hdr->src_addr;
390                                         ipv4_hdr->src_addr = bond_ip;
391                                         rte_eth_tx_burst(BOND_PORT, 0, &pkts[i], 1);
392                                 }
393
394                         }
395
396                         /* Free processed packets */
397                         if (is_free == 0)
398                                 rte_pktmbuf_free(pkts[i]);
399                 }
400                 rte_spinlock_trylock(&global_flag_stru_p->lock);
401         }
402         rte_spinlock_unlock(&global_flag_stru_p->lock);
403         printf("BYE lcore_main\n");
404         return 0;
405 }
406
407 struct cmd_obj_send_result {
408         cmdline_fixed_string_t action;
409         cmdline_ipaddr_t ip;
410 };
411 static inline void get_string(struct cmd_obj_send_result *res, char *buf, uint8_t size)
412 {
413         snprintf(buf, size, NIPQUAD_FMT,
414                 ((unsigned)((unsigned char *)&(res->ip.addr.ipv4))[0]),
415                 ((unsigned)((unsigned char *)&(res->ip.addr.ipv4))[1]),
416                 ((unsigned)((unsigned char *)&(res->ip.addr.ipv4))[2]),
417                 ((unsigned)((unsigned char *)&(res->ip.addr.ipv4))[3])
418                 );
419 }
420 static void cmd_obj_send_parsed(void *parsed_result,
421                 __attribute__((unused)) struct cmdline *cl,
422                                __attribute__((unused)) void *data)
423 {
424
425         struct cmd_obj_send_result *res = parsed_result;
426         char ip_str[INET6_ADDRSTRLEN];
427
428         struct rte_mbuf *created_pkt;
429         struct ether_hdr *eth_hdr;
430         struct arp_hdr *arp_hdr;
431
432         uint32_t bond_ip;
433         size_t pkt_size;
434
435         if (res->ip.family == AF_INET)
436                 get_string(res, ip_str, INET_ADDRSTRLEN);
437         else
438                 cmdline_printf(cl, "Wrong IP format. Only IPv4 is supported\n");
439
440         bond_ip = BOND_IP_1 | (BOND_IP_2 << 8) |
441                                 (BOND_IP_3 << 16) | (BOND_IP_4 << 24);
442
443         created_pkt = rte_pktmbuf_alloc(mbuf_pool);
444         pkt_size = sizeof(struct ether_hdr) + sizeof(struct arp_hdr);
445         created_pkt->data_len = pkt_size;
446         created_pkt->pkt_len = pkt_size;
447
448         eth_hdr = rte_pktmbuf_mtod(created_pkt, struct ether_hdr *);
449         rte_eth_macaddr_get(BOND_PORT, &eth_hdr->s_addr);
450         memset(&eth_hdr->d_addr, 0xFF, ETHER_ADDR_LEN);
451         eth_hdr->ether_type = rte_cpu_to_be_16(ETHER_TYPE_ARP);
452
453         arp_hdr = (struct arp_hdr *)((char *)eth_hdr + sizeof(struct ether_hdr));
454         arp_hdr->arp_hrd = rte_cpu_to_be_16(ARP_HRD_ETHER);
455         arp_hdr->arp_pro = rte_cpu_to_be_16(ETHER_TYPE_IPv4);
456         arp_hdr->arp_hln = ETHER_ADDR_LEN;
457         arp_hdr->arp_pln = sizeof(uint32_t);
458         arp_hdr->arp_op = rte_cpu_to_be_16(ARP_OP_REQUEST);
459
460         rte_eth_macaddr_get(BOND_PORT, &arp_hdr->arp_data.arp_sha);
461         arp_hdr->arp_data.arp_sip = bond_ip;
462         memset(&arp_hdr->arp_data.arp_tha, 0, ETHER_ADDR_LEN);
463         arp_hdr->arp_data.arp_tip =
464                           ((unsigned char *)&res->ip.addr.ipv4)[0]        |
465                          (((unsigned char *)&res->ip.addr.ipv4)[1] << 8)  |
466                          (((unsigned char *)&res->ip.addr.ipv4)[2] << 16) |
467                          (((unsigned char *)&res->ip.addr.ipv4)[3] << 24);
468         rte_eth_tx_burst(BOND_PORT, 0, &created_pkt, 1);
469
470         rte_delay_ms(100);
471         cmdline_printf(cl, "\n");
472 }
473
474 cmdline_parse_token_string_t cmd_obj_action_send =
475         TOKEN_STRING_INITIALIZER(struct cmd_obj_send_result, action, "send");
476 cmdline_parse_token_ipaddr_t cmd_obj_ip =
477         TOKEN_IPV4_INITIALIZER(struct cmd_obj_send_result, ip);
478
479 cmdline_parse_inst_t cmd_obj_send = {
480         .f = cmd_obj_send_parsed,  /* function to call */
481         .data = NULL,      /* 2nd arg of func */
482         .help_str = "send client_ip",
483         .tokens = {        /* token list, NULL terminated */
484                 (void *)&cmd_obj_action_send,
485                 (void *)&cmd_obj_ip,
486                 NULL,
487         },
488 };
489
490 struct cmd_start_result {
491         cmdline_fixed_string_t start;
492 };
493
494 static void cmd_start_parsed(__attribute__((unused)) void *parsed_result,
495                                struct cmdline *cl,
496                                __attribute__((unused)) void *data)
497 {
498         int slave_core_id = rte_lcore_id();
499
500         rte_spinlock_trylock(&global_flag_stru_p->lock);
501         if (global_flag_stru_p->LcoreMainIsRunning == 0)        {
502                 if (lcore_config[global_flag_stru_p->LcoreMainCore].state != WAIT)      {
503                         rte_spinlock_unlock(&global_flag_stru_p->lock);
504                         return;
505                 }
506                 rte_spinlock_unlock(&global_flag_stru_p->lock);
507         } else {
508                 cmdline_printf(cl, "lcore_main already running on core:%d\n",
509                                 global_flag_stru_p->LcoreMainCore);
510                 rte_spinlock_unlock(&global_flag_stru_p->lock);
511                 return;
512         }
513
514         /* start lcore main on core != master_core - ARP response thread */
515         slave_core_id = rte_get_next_lcore(rte_lcore_id(), 1, 0);
516         if ((slave_core_id >= RTE_MAX_LCORE) || (slave_core_id == 0))
517                 return;
518
519         rte_spinlock_trylock(&global_flag_stru_p->lock);
520         global_flag_stru_p->LcoreMainIsRunning = 1;
521         rte_spinlock_unlock(&global_flag_stru_p->lock);
522         cmdline_printf(cl,
523                         "Starting lcore_main on core %d:%d "
524                         "Our IP:%d.%d.%d.%d\n",
525                         slave_core_id,
526                         rte_eal_remote_launch(lcore_main, NULL, slave_core_id),
527                         BOND_IP_1,
528                         BOND_IP_2,
529                         BOND_IP_3,
530                         BOND_IP_4
531                 );
532 }
533
534 cmdline_parse_token_string_t cmd_start_start =
535         TOKEN_STRING_INITIALIZER(struct cmd_start_result, start, "start");
536
537 cmdline_parse_inst_t cmd_start = {
538         .f = cmd_start_parsed,  /* function to call */
539         .data = NULL,      /* 2nd arg of func */
540         .help_str = "starts listening if not started at startup",
541         .tokens = {        /* token list, NULL terminated */
542                 (void *)&cmd_start_start,
543                 NULL,
544         },
545 };
546
547 struct cmd_help_result {
548         cmdline_fixed_string_t help;
549 };
550
551 static void cmd_help_parsed(__attribute__((unused)) void *parsed_result,
552                             struct cmdline *cl,
553                             __attribute__((unused)) void *data)
554 {
555         cmdline_printf(cl,
556                         "ALB - link bonding mode 6 example\n"
557                         "send IP        - sends one ARPrequest through bonding for IP.\n"
558                         "start          - starts listening ARPs.\n"
559                         "stop           - stops lcore_main.\n"
560                         "show           - shows some bond info: ex. active slaves etc.\n"
561                         "help           - prints help.\n"
562                         "quit           - terminate all threads and quit.\n"
563                        );
564 }
565
566 cmdline_parse_token_string_t cmd_help_help =
567         TOKEN_STRING_INITIALIZER(struct cmd_help_result, help, "help");
568
569 cmdline_parse_inst_t cmd_help = {
570         .f = cmd_help_parsed,  /* function to call */
571         .data = NULL,      /* 2nd arg of func */
572         .help_str = "show help",
573         .tokens = {        /* token list, NULL terminated */
574                 (void *)&cmd_help_help,
575                 NULL,
576         },
577 };
578
579 struct cmd_stop_result {
580         cmdline_fixed_string_t stop;
581 };
582
583 static void cmd_stop_parsed(__attribute__((unused)) void *parsed_result,
584                             struct cmdline *cl,
585                             __attribute__((unused)) void *data)
586 {
587         rte_spinlock_trylock(&global_flag_stru_p->lock);
588         if (global_flag_stru_p->LcoreMainIsRunning == 0)        {
589                 cmdline_printf(cl,
590                                         "lcore_main not running on core:%d\n",
591                                         global_flag_stru_p->LcoreMainCore);
592                 rte_spinlock_unlock(&global_flag_stru_p->lock);
593                 return;
594         }
595         global_flag_stru_p->LcoreMainIsRunning = 0;
596         if (rte_eal_wait_lcore(global_flag_stru_p->LcoreMainCore) < 0)
597                 cmdline_printf(cl,
598                                 "error: lcore_main can not stop on core:%d\n",
599                                 global_flag_stru_p->LcoreMainCore);
600         else
601                 cmdline_printf(cl,
602                                 "lcore_main stopped on core:%d\n",
603                                 global_flag_stru_p->LcoreMainCore);
604         rte_spinlock_unlock(&global_flag_stru_p->lock);
605 }
606
607 cmdline_parse_token_string_t cmd_stop_stop =
608         TOKEN_STRING_INITIALIZER(struct cmd_stop_result, stop, "stop");
609
610 cmdline_parse_inst_t cmd_stop = {
611         .f = cmd_stop_parsed,  /* function to call */
612         .data = NULL,      /* 2nd arg of func */
613         .help_str = "this command do not handle any arguments",
614         .tokens = {        /* token list, NULL terminated */
615                 (void *)&cmd_stop_stop,
616                 NULL,
617         },
618 };
619
620 struct cmd_quit_result {
621         cmdline_fixed_string_t quit;
622 };
623
624 static void cmd_quit_parsed(__attribute__((unused)) void *parsed_result,
625                             struct cmdline *cl,
626                             __attribute__((unused)) void *data)
627 {
628         rte_spinlock_trylock(&global_flag_stru_p->lock);
629         if (global_flag_stru_p->LcoreMainIsRunning == 0)        {
630                 cmdline_printf(cl,
631                                         "lcore_main not running on core:%d\n",
632                                         global_flag_stru_p->LcoreMainCore);
633                 rte_spinlock_unlock(&global_flag_stru_p->lock);
634                 cmdline_quit(cl);
635                 return;
636         }
637         global_flag_stru_p->LcoreMainIsRunning = 0;
638         if (rte_eal_wait_lcore(global_flag_stru_p->LcoreMainCore) < 0)
639                 cmdline_printf(cl,
640                                 "error: lcore_main can not stop on core:%d\n",
641                                 global_flag_stru_p->LcoreMainCore);
642         else
643                 cmdline_printf(cl,
644                                 "lcore_main stopped on core:%d\n",
645                                 global_flag_stru_p->LcoreMainCore);
646         rte_spinlock_unlock(&global_flag_stru_p->lock);
647         cmdline_quit(cl);
648 }
649
650 cmdline_parse_token_string_t cmd_quit_quit =
651         TOKEN_STRING_INITIALIZER(struct cmd_quit_result, quit, "quit");
652
653 cmdline_parse_inst_t cmd_quit = {
654         .f = cmd_quit_parsed,  /* function to call */
655         .data = NULL,      /* 2nd arg of func */
656         .help_str = "this command do not handle any arguments",
657         .tokens = {        /* token list, NULL terminated */
658                 (void *)&cmd_quit_quit,
659                 NULL,
660         },
661 };
662
663 struct cmd_show_result {
664         cmdline_fixed_string_t show;
665 };
666
667 static void cmd_show_parsed(__attribute__((unused)) void *parsed_result,
668                             struct cmdline *cl,
669                             __attribute__((unused)) void *data)
670 {
671         uint16_t slaves[16] = {0};
672         uint8_t len = 16;
673         struct ether_addr addr;
674         uint16_t i = 0;
675
676         while (i < slaves_count)        {
677                 rte_eth_macaddr_get(i, &addr);
678                 PRINT_MAC(addr);
679                 printf("\n");
680                 i++;
681         }
682
683         rte_spinlock_trylock(&global_flag_stru_p->lock);
684         cmdline_printf(cl,
685                         "Active_slaves:%d "
686                         "packets received:Tot:%d Arp:%d IPv4:%d\n",
687                         rte_eth_bond_active_slaves_get(BOND_PORT, slaves, len),
688                         global_flag_stru_p->port_packets[0],
689                         global_flag_stru_p->port_packets[1],
690                         global_flag_stru_p->port_packets[2]);
691         rte_spinlock_unlock(&global_flag_stru_p->lock);
692 }
693
694 cmdline_parse_token_string_t cmd_show_show =
695         TOKEN_STRING_INITIALIZER(struct cmd_show_result, show, "show");
696
697 cmdline_parse_inst_t cmd_show = {
698         .f = cmd_show_parsed,  /* function to call */
699         .data = NULL,      /* 2nd arg of func */
700         .help_str = "this command do not handle any arguments",
701         .tokens = {        /* token list, NULL terminated */
702                 (void *)&cmd_show_show,
703                 NULL,
704         },
705 };
706
707 /****** CONTEXT (list of instruction) */
708
709 cmdline_parse_ctx_t main_ctx[] = {
710         (cmdline_parse_inst_t *)&cmd_start,
711         (cmdline_parse_inst_t *)&cmd_obj_send,
712         (cmdline_parse_inst_t *)&cmd_stop,
713         (cmdline_parse_inst_t *)&cmd_show,
714         (cmdline_parse_inst_t *)&cmd_quit,
715         (cmdline_parse_inst_t *)&cmd_help,
716         NULL,
717 };
718
719 /* prompt function, called from main on MASTER lcore */
720 static void prompt(__attribute__((unused)) void *arg1)
721 {
722         struct cmdline *cl;
723
724         cl = cmdline_stdin_new(main_ctx, "bond6>");
725         if (cl != NULL) {
726                 cmdline_interact(cl);
727                 cmdline_stdin_exit(cl);
728         }
729 }
730
731 /* Main function, does initialisation and calls the per-lcore functions */
732 int
733 main(int argc, char *argv[])
734 {
735         int ret;
736         uint8_t nb_ports, i;
737
738         /* init EAL */
739         ret = rte_eal_init(argc, argv);
740         rte_eal_devargs_dump(stdout);
741         if (ret < 0)
742                 rte_exit(EXIT_FAILURE, "Error with EAL initialization\n");
743         argc -= ret;
744         argv += ret;
745
746         nb_ports = rte_eth_dev_count();
747         if (nb_ports == 0)
748                 rte_exit(EXIT_FAILURE, "Give at least one port\n");
749         else if (nb_ports > MAX_PORTS)
750                 rte_exit(EXIT_FAILURE, "You can have max 4 ports\n");
751
752         mbuf_pool = rte_pktmbuf_pool_create("MBUF_POOL", NB_MBUF, 32,
753                 0, RTE_MBUF_DEFAULT_BUF_SIZE, rte_socket_id());
754         if (mbuf_pool == NULL)
755                 rte_exit(EXIT_FAILURE, "Cannot create mbuf pool\n");
756
757         /* initialize all ports */
758         slaves_count = nb_ports;
759         for (i = 0; i < nb_ports; i++) {
760                 slave_port_init(i, mbuf_pool);
761                 slaves[i] = i;
762         }
763
764         bond_port_init(mbuf_pool);
765
766         rte_spinlock_init(&global_flag_stru_p->lock);
767         int slave_core_id = rte_lcore_id();
768
769         /* check state of lcores */
770         RTE_LCORE_FOREACH_SLAVE(slave_core_id) {
771         if (lcore_config[slave_core_id].state != WAIT)
772                 return -EBUSY;
773         }
774         /* start lcore main on core != master_core - ARP response thread */
775         slave_core_id = rte_get_next_lcore(rte_lcore_id(), 1, 0);
776         if ((slave_core_id >= RTE_MAX_LCORE) || (slave_core_id == 0))
777                 return -EPERM;
778
779         global_flag_stru_p->LcoreMainIsRunning = 1;
780         global_flag_stru_p->LcoreMainCore = slave_core_id;
781         printf("Starting lcore_main on core %d:%d Our IP:%d.%d.%d.%d\n",
782                         slave_core_id,
783                         rte_eal_remote_launch((lcore_function_t *)lcore_main,
784                                         NULL,
785                                         slave_core_id),
786                         BOND_IP_1,
787                         BOND_IP_2,
788                         BOND_IP_3,
789                         BOND_IP_4
790                 );
791
792         /* Start prompt for user interact */
793         prompt(NULL);
794
795         rte_delay_ms(100);
796         return 0;
797 }