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