remove useless memzone includes
[dpdk.git] / examples / bond / main.c
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2010-2015 Intel Corporation. All rights reserved.
5  *   All rights reserved.
6  *
7  *   Redistribution and use in source and binary forms, with or without
8  *   modification, are permitted provided that the following conditions
9  *   are met:
10  *
11  *     * Redistributions of source code must retain the above copyright
12  *       notice, this list of conditions and the following disclaimer.
13  *     * Redistributions in binary form must reproduce the above copyright
14  *       notice, this list of conditions and the following disclaimer in
15  *       the documentation and/or other materials provided with the
16  *       distribution.
17  *     * Neither the name of Intel Corporation nor the names of its
18  *       contributors may be used to endorse or promote products derived
19  *       from this software without specific prior written permission.
20  *
21  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33
34 #include <stdint.h>
35 #include <sys/queue.h>
36 #include <sys/socket.h>
37 #include <stdlib.h>
38 #include <string.h>
39 #include <stdio.h>
40 #include <assert.h>
41 #include <errno.h>
42 #include <signal.h>
43 #include <stdarg.h>
44 #include <inttypes.h>
45 #include <getopt.h>
46 #include <termios.h>
47 #include <unistd.h>
48 #include <pthread.h>
49
50 #include <rte_common.h>
51 #include <rte_log.h>
52 #include <rte_memory.h>
53 #include <rte_memcpy.h>
54 #include <rte_eal.h>
55 #include <rte_launch.h>
56 #include <rte_atomic.h>
57 #include <rte_cycles.h>
58 #include <rte_prefetch.h>
59 #include <rte_lcore.h>
60 #include <rte_per_lcore.h>
61 #include <rte_branch_prediction.h>
62 #include <rte_interrupts.h>
63 #include <rte_pci.h>
64 #include <rte_random.h>
65 #include <rte_debug.h>
66 #include <rte_ether.h>
67 #include <rte_ethdev.h>
68 #include <rte_mempool.h>
69 #include <rte_mbuf.h>
70 #include <rte_ip.h>
71 #include <rte_tcp.h>
72 #include <rte_arp.h>
73 #include <rte_spinlock.h>
74
75 #include <cmdline_rdline.h>
76 #include <cmdline_parse.h>
77 #include <cmdline_parse_num.h>
78 #include <cmdline_parse_string.h>
79 #include <cmdline_parse_ipaddr.h>
80 #include <cmdline_parse_etheraddr.h>
81 #include <cmdline_socket.h>
82 #include <cmdline.h>
83
84 #include "main.h"
85
86 #include <rte_devargs.h>
87
88
89 #include "rte_byteorder.h"
90 #include "rte_cpuflags.h"
91 #include "rte_eth_bond.h"
92
93 #define RTE_LOGTYPE_DCB RTE_LOGTYPE_USER1
94
95 #define NB_MBUF   (1024*8)
96
97 #define MAX_PKT_BURST 32
98 #define BURST_TX_DRAIN_US 100      /* TX drain every ~100us */
99 #define BURST_RX_INTERVAL_NS (10) /* RX poll interval ~100ns */
100
101 /*
102  * RX and TX Prefetch, Host, and Write-back threshold values should be
103  * carefully set for optimal performance. Consult the network
104  * controller's datasheet and supporting DPDK documentation for guidance
105  * on how these parameters should be set.
106  */
107 #define RX_PTHRESH 8 /**< Default values of RX prefetch threshold reg. */
108 #define RX_HTHRESH 8 /**< Default values of RX host threshold reg. */
109 #define RX_WTHRESH 4 /**< Default values of RX write-back threshold reg. */
110 #define RX_FTHRESH (MAX_PKT_BURST * 2)/**< Default values of RX free threshold reg. */
111
112 /*
113  * These default values are optimized for use with the Intel(R) 82599 10 GbE
114  * Controller and the DPDK ixgbe PMD. Consider using other values for other
115  * network controllers and/or network drivers.
116  */
117 #define TX_PTHRESH 36 /**< Default values of TX prefetch threshold reg. */
118 #define TX_HTHRESH 0  /**< Default values of TX host threshold reg. */
119 #define TX_WTHRESH 0  /**< Default values of TX write-back threshold reg. */
120
121 /*
122  * Configurable number of RX/TX ring descriptors
123  */
124 #define RTE_RX_DESC_DEFAULT 128
125 #define RTE_TX_DESC_DEFAULT 512
126
127 #define BOND_IP_1       7
128 #define BOND_IP_2       0
129 #define BOND_IP_3       0
130 #define BOND_IP_4       10
131
132 /* not defined under linux */
133 #ifndef NIPQUAD
134 #define NIPQUAD_FMT "%u.%u.%u.%u"
135 #endif
136
137 #define MAX_PORTS       4
138 #define PRINT_MAC(addr)         printf("%02"PRIx8":%02"PRIx8":%02"PRIx8 \
139                 ":%02"PRIx8":%02"PRIx8":%02"PRIx8,      \
140                 addr.addr_bytes[0], addr.addr_bytes[1], addr.addr_bytes[2], \
141                 addr.addr_bytes[3], addr.addr_bytes[4], addr.addr_bytes[5])
142
143 uint16_t slaves[RTE_MAX_ETHPORTS];
144 uint16_t slaves_count;
145
146 static uint16_t BOND_PORT = 0xffff;
147
148 static struct rte_mempool *mbuf_pool;
149
150 static struct rte_eth_conf port_conf = {
151         .rxmode = {
152                 .mq_mode = ETH_MQ_RX_NONE,
153                 .max_rx_pkt_len = ETHER_MAX_LEN,
154                 .split_hdr_size = 0,
155                 .header_split   = 0, /**< Header Split disabled */
156                 .hw_ip_checksum = 0, /**< IP checksum offload enabled */
157                 .hw_vlan_filter = 0, /**< VLAN filtering disabled */
158                 .jumbo_frame    = 0, /**< Jumbo Frame Support disabled */
159                 .hw_strip_crc   = 1, /**< CRC stripped by hardware */
160         },
161         .rx_adv_conf = {
162                 .rss_conf = {
163                         .rss_key = NULL,
164                         .rss_hf = ETH_RSS_IP,
165                 },
166         },
167         .txmode = {
168                 .mq_mode = ETH_MQ_TX_NONE,
169         },
170 };
171
172 static void
173 slave_port_init(uint16_t portid, struct rte_mempool *mbuf_pool)
174 {
175         int retval;
176         uint16_t nb_rxd = RTE_RX_DESC_DEFAULT;
177         uint16_t nb_txd = RTE_TX_DESC_DEFAULT;
178
179         if (portid >= rte_eth_dev_count())
180                 rte_exit(EXIT_FAILURE, "Invalid port\n");
181
182         retval = rte_eth_dev_configure(portid, 1, 1, &port_conf);
183         if (retval != 0)
184                 rte_exit(EXIT_FAILURE, "port %u: configuration failed (res=%d)\n",
185                                 portid, retval);
186
187         retval = rte_eth_dev_adjust_nb_rx_tx_desc(portid, &nb_rxd, &nb_txd);
188         if (retval != 0)
189                 rte_exit(EXIT_FAILURE, "port %u: rte_eth_dev_adjust_nb_rx_tx_desc "
190                                 "failed (res=%d)\n", portid, retval);
191
192         /* RX setup */
193         retval = rte_eth_rx_queue_setup(portid, 0, nb_rxd,
194                                         rte_eth_dev_socket_id(portid), NULL,
195                                         mbuf_pool);
196         if (retval < 0)
197                 rte_exit(retval, " port %u: RX queue 0 setup failed (res=%d)",
198                                 portid, retval);
199
200         /* TX setup */
201         retval = rte_eth_tx_queue_setup(portid, 0, nb_txd,
202                                 rte_eth_dev_socket_id(portid), NULL);
203
204         if (retval < 0)
205                 rte_exit(retval, "port %u: TX queue 0 setup failed (res=%d)",
206                                 portid, retval);
207
208         retval  = rte_eth_dev_start(portid);
209         if (retval < 0)
210                 rte_exit(retval,
211                                 "Start port %d failed (res=%d)",
212                                 portid, retval);
213
214         struct ether_addr addr;
215
216         rte_eth_macaddr_get(portid, &addr);
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
230         retval = rte_eth_bond_create("bond0", 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_configure(BOND_PORT, 1, 1, &port_conf);
239         if (retval != 0)
240                 rte_exit(EXIT_FAILURE, "port %u: configuration failed (res=%d)\n",
241                                 BOND_PORT, retval);
242
243         retval = rte_eth_dev_adjust_nb_rx_tx_desc(BOND_PORT, &nb_rxd, &nb_txd);
244         if (retval != 0)
245                 rte_exit(EXIT_FAILURE, "port %u: rte_eth_dev_adjust_nb_rx_tx_desc "
246                                 "failed (res=%d)\n", BOND_PORT, retval);
247
248         /* RX setup */
249         retval = rte_eth_rx_queue_setup(BOND_PORT, 0, nb_rxd,
250                                         rte_eth_dev_socket_id(BOND_PORT), NULL,
251                                         mbuf_pool);
252         if (retval < 0)
253                 rte_exit(retval, " port %u: RX queue 0 setup failed (res=%d)",
254                                 BOND_PORT, retval);
255
256         /* TX setup */
257         retval = rte_eth_tx_queue_setup(BOND_PORT, 0, nb_txd,
258                                 rte_eth_dev_socket_id(BOND_PORT), NULL);
259
260         if (retval < 0)
261                 rte_exit(retval, "port %u: TX queue 0 setup failed (res=%d)",
262                                 BOND_PORT, retval);
263
264         for (i = 0; i < slaves_count; i++) {
265                 if (rte_eth_bond_slave_add(BOND_PORT, slaves[i]) == -1)
266                         rte_exit(-1, "Oooops! adding slave (%u) to bond (%u) failed!\n",
267                                         slaves[i], BOND_PORT);
268
269         }
270
271         retval  = rte_eth_dev_start(BOND_PORT);
272         if (retval < 0)
273                 rte_exit(retval, "Start port %d failed (res=%d)", BOND_PORT, retval);
274
275         rte_eth_promiscuous_enable(BOND_PORT);
276
277         struct ether_addr addr;
278
279         rte_eth_macaddr_get(BOND_PORT, &addr);
280         printf("Port %u MAC: ", (unsigned)BOND_PORT);
281                 PRINT_MAC(addr);
282                 printf("\n");
283 }
284
285 static inline size_t
286 get_vlan_offset(struct ether_hdr *eth_hdr, uint16_t *proto)
287 {
288         size_t vlan_offset = 0;
289
290         if (rte_cpu_to_be_16(ETHER_TYPE_VLAN) == *proto) {
291                 struct vlan_hdr *vlan_hdr = (struct vlan_hdr *)(eth_hdr + 1);
292
293                 vlan_offset = sizeof(struct vlan_hdr);
294                 *proto = vlan_hdr->eth_proto;
295
296                 if (rte_cpu_to_be_16(ETHER_TYPE_VLAN) == *proto) {
297                         vlan_hdr = vlan_hdr + 1;
298
299                         *proto = vlan_hdr->eth_proto;
300                         vlan_offset += sizeof(struct vlan_hdr);
301                 }
302         }
303         return vlan_offset;
304 }
305
306 struct global_flag_stru_t {
307         int LcoreMainIsRunning;
308         int LcoreMainCore;
309         uint32_t port_packets[4];
310         rte_spinlock_t lock;
311 };
312 struct global_flag_stru_t global_flag_stru;
313 struct global_flag_stru_t *global_flag_stru_p = &global_flag_stru;
314
315 /*
316  * Main thread that does the work, reading from INPUT_PORT
317  * and writing to OUTPUT_PORT
318  */
319 static int lcore_main(__attribute__((unused)) void *arg1)
320 {
321         struct rte_mbuf *pkts[MAX_PKT_BURST] __rte_cache_aligned;
322         struct ether_addr d_addr;
323
324         struct ether_hdr *eth_hdr;
325         struct arp_hdr *arp_hdr;
326         struct ipv4_hdr *ipv4_hdr;
327         uint16_t ether_type, offset;
328
329         uint16_t rx_cnt;
330         uint32_t bond_ip;
331         int i = 0;
332         uint8_t is_free;
333
334         bond_ip = BOND_IP_1 | (BOND_IP_2 << 8) |
335                                 (BOND_IP_3 << 16) | (BOND_IP_4 << 24);
336
337         rte_spinlock_trylock(&global_flag_stru_p->lock);
338
339         while (global_flag_stru_p->LcoreMainIsRunning) {
340                 rte_spinlock_unlock(&global_flag_stru_p->lock);
341                 rx_cnt = rte_eth_rx_burst(BOND_PORT, 0, pkts, MAX_PKT_BURST);
342                 is_free = 0;
343
344                 /* If didn't receive any packets, wait and go to next iteration */
345                 if (rx_cnt == 0) {
346                         rte_delay_us(50);
347                         continue;
348                 }
349
350                 /* Search incoming data for ARP packets and prepare response */
351                 for (i = 0; i < rx_cnt; i++) {
352                         if (rte_spinlock_trylock(&global_flag_stru_p->lock) == 1) {
353                                 global_flag_stru_p->port_packets[0]++;
354                                 rte_spinlock_unlock(&global_flag_stru_p->lock);
355                         }
356                         eth_hdr = rte_pktmbuf_mtod(pkts[i], struct ether_hdr *);
357                         ether_type = eth_hdr->ether_type;
358                         if (ether_type == rte_cpu_to_be_16(ETHER_TYPE_VLAN))
359                                 printf("VLAN taged frame, offset:");
360                         offset = get_vlan_offset(eth_hdr, &ether_type);
361                         if (offset > 0)
362                                 printf("%d\n", offset);
363                         if (ether_type == rte_cpu_to_be_16(ETHER_TYPE_ARP)) {
364                                 if (rte_spinlock_trylock(&global_flag_stru_p->lock) == 1)     {
365                                         global_flag_stru_p->port_packets[1]++;
366                                         rte_spinlock_unlock(&global_flag_stru_p->lock);
367                                 }
368                                 arp_hdr = (struct arp_hdr *)((char *)(eth_hdr + 1) + offset);
369                                 if (arp_hdr->arp_data.arp_tip == bond_ip) {
370                                         if (arp_hdr->arp_op == rte_cpu_to_be_16(ARP_OP_REQUEST)) {
371                                                 arp_hdr->arp_op = rte_cpu_to_be_16(ARP_OP_REPLY);
372                                                 /* Switch src and dst data and set bonding MAC */
373                                                 ether_addr_copy(&eth_hdr->s_addr, &eth_hdr->d_addr);
374                                                 rte_eth_macaddr_get(BOND_PORT, &eth_hdr->s_addr);
375                                                 ether_addr_copy(&arp_hdr->arp_data.arp_sha, &arp_hdr->arp_data.arp_tha);
376                                                 arp_hdr->arp_data.arp_tip = arp_hdr->arp_data.arp_sip;
377                                                 rte_eth_macaddr_get(BOND_PORT, &d_addr);
378                                                 ether_addr_copy(&d_addr, &arp_hdr->arp_data.arp_sha);
379                                                 arp_hdr->arp_data.arp_sip = bond_ip;
380                                                 rte_eth_tx_burst(BOND_PORT, 0, &pkts[i], 1);
381                                                 is_free = 1;
382                                         } else {
383                                                 rte_eth_tx_burst(BOND_PORT, 0, NULL, 0);
384                                         }
385                                 }
386                         } else if (ether_type == rte_cpu_to_be_16(ETHER_TYPE_IPv4)) {
387                                 if (rte_spinlock_trylock(&global_flag_stru_p->lock) == 1)     {
388                                         global_flag_stru_p->port_packets[2]++;
389                                         rte_spinlock_unlock(&global_flag_stru_p->lock);
390                                  }
391                                 ipv4_hdr = (struct ipv4_hdr *)((char *)(eth_hdr + 1) + offset);
392                                 if (ipv4_hdr->dst_addr == bond_ip) {
393                                         ether_addr_copy(&eth_hdr->s_addr, &eth_hdr->d_addr);
394                                         rte_eth_macaddr_get(BOND_PORT, &eth_hdr->s_addr);
395                                         ipv4_hdr->dst_addr = ipv4_hdr->src_addr;
396                                         ipv4_hdr->src_addr = bond_ip;
397                                         rte_eth_tx_burst(BOND_PORT, 0, &pkts[i], 1);
398                                 }
399
400                         }
401
402                         /* Free processed packets */
403                         if (is_free == 0)
404                                 rte_pktmbuf_free(pkts[i]);
405                 }
406                 rte_spinlock_trylock(&global_flag_stru_p->lock);
407         }
408         rte_spinlock_unlock(&global_flag_stru_p->lock);
409         printf("BYE lcore_main\n");
410         return 0;
411 }
412
413 struct cmd_obj_send_result {
414         cmdline_fixed_string_t action;
415         cmdline_ipaddr_t ip;
416 };
417 static inline void get_string(struct cmd_obj_send_result *res, char *buf, uint8_t size)
418 {
419         snprintf(buf, size, NIPQUAD_FMT,
420                 ((unsigned)((unsigned char *)&(res->ip.addr.ipv4))[0]),
421                 ((unsigned)((unsigned char *)&(res->ip.addr.ipv4))[1]),
422                 ((unsigned)((unsigned char *)&(res->ip.addr.ipv4))[2]),
423                 ((unsigned)((unsigned char *)&(res->ip.addr.ipv4))[3])
424                 );
425 }
426 static void cmd_obj_send_parsed(void *parsed_result,
427                 __attribute__((unused)) struct cmdline *cl,
428                                __attribute__((unused)) void *data)
429 {
430
431         struct cmd_obj_send_result *res = parsed_result;
432         char ip_str[INET6_ADDRSTRLEN];
433
434         struct rte_mbuf *created_pkt;
435         struct ether_hdr *eth_hdr;
436         struct arp_hdr *arp_hdr;
437
438         uint32_t bond_ip;
439         size_t pkt_size;
440
441         if (res->ip.family == AF_INET)
442                 get_string(res, ip_str, INET_ADDRSTRLEN);
443         else
444                 cmdline_printf(cl, "Wrong IP format. Only IPv4 is supported\n");
445
446         bond_ip = BOND_IP_1 | (BOND_IP_2 << 8) |
447                                 (BOND_IP_3 << 16) | (BOND_IP_4 << 24);
448
449         created_pkt = rte_pktmbuf_alloc(mbuf_pool);
450         pkt_size = sizeof(struct ether_hdr) + sizeof(struct arp_hdr);
451         created_pkt->data_len = pkt_size;
452         created_pkt->pkt_len = pkt_size;
453
454         eth_hdr = rte_pktmbuf_mtod(created_pkt, struct ether_hdr *);
455         rte_eth_macaddr_get(BOND_PORT, &eth_hdr->s_addr);
456         memset(&eth_hdr->d_addr, 0xFF, ETHER_ADDR_LEN);
457         eth_hdr->ether_type = rte_cpu_to_be_16(ETHER_TYPE_ARP);
458
459         arp_hdr = (struct arp_hdr *)((char *)eth_hdr + sizeof(struct ether_hdr));
460         arp_hdr->arp_hrd = rte_cpu_to_be_16(ARP_HRD_ETHER);
461         arp_hdr->arp_pro = rte_cpu_to_be_16(ETHER_TYPE_IPv4);
462         arp_hdr->arp_hln = ETHER_ADDR_LEN;
463         arp_hdr->arp_pln = sizeof(uint32_t);
464         arp_hdr->arp_op = rte_cpu_to_be_16(ARP_OP_REQUEST);
465
466         rte_eth_macaddr_get(BOND_PORT, &arp_hdr->arp_data.arp_sha);
467         arp_hdr->arp_data.arp_sip = bond_ip;
468         memset(&arp_hdr->arp_data.arp_tha, 0, ETHER_ADDR_LEN);
469         arp_hdr->arp_data.arp_tip =
470                           ((unsigned char *)&res->ip.addr.ipv4)[0]        |
471                          (((unsigned char *)&res->ip.addr.ipv4)[1] << 8)  |
472                          (((unsigned char *)&res->ip.addr.ipv4)[2] << 16) |
473                          (((unsigned char *)&res->ip.addr.ipv4)[3] << 24);
474         rte_eth_tx_burst(BOND_PORT, 0, &created_pkt, 1);
475
476         rte_delay_ms(100);
477         cmdline_printf(cl, "\n");
478 }
479
480 cmdline_parse_token_string_t cmd_obj_action_send =
481         TOKEN_STRING_INITIALIZER(struct cmd_obj_send_result, action, "send");
482 cmdline_parse_token_ipaddr_t cmd_obj_ip =
483         TOKEN_IPV4_INITIALIZER(struct cmd_obj_send_result, ip);
484
485 cmdline_parse_inst_t cmd_obj_send = {
486         .f = cmd_obj_send_parsed,  /* function to call */
487         .data = NULL,      /* 2nd arg of func */
488         .help_str = "send client_ip",
489         .tokens = {        /* token list, NULL terminated */
490                 (void *)&cmd_obj_action_send,
491                 (void *)&cmd_obj_ip,
492                 NULL,
493         },
494 };
495
496 struct cmd_start_result {
497         cmdline_fixed_string_t start;
498 };
499
500 static void cmd_start_parsed(__attribute__((unused)) void *parsed_result,
501                                struct cmdline *cl,
502                                __attribute__((unused)) void *data)
503 {
504         int slave_core_id = rte_lcore_id();
505
506         rte_spinlock_trylock(&global_flag_stru_p->lock);
507         if (global_flag_stru_p->LcoreMainIsRunning == 0)        {
508                 if (lcore_config[global_flag_stru_p->LcoreMainCore].state != WAIT)      {
509                         rte_spinlock_unlock(&global_flag_stru_p->lock);
510                         return;
511                 }
512                 rte_spinlock_unlock(&global_flag_stru_p->lock);
513         } else {
514                 cmdline_printf(cl, "lcore_main already running on core:%d\n",
515                                 global_flag_stru_p->LcoreMainCore);
516                 rte_spinlock_unlock(&global_flag_stru_p->lock);
517                 return;
518         }
519
520         /* start lcore main on core != master_core - ARP response thread */
521         slave_core_id = rte_get_next_lcore(rte_lcore_id(), 1, 0);
522         if ((slave_core_id >= RTE_MAX_LCORE) || (slave_core_id == 0))
523                 return;
524
525         rte_spinlock_trylock(&global_flag_stru_p->lock);
526         global_flag_stru_p->LcoreMainIsRunning = 1;
527         rte_spinlock_unlock(&global_flag_stru_p->lock);
528         cmdline_printf(cl,
529                         "Starting lcore_main on core %d:%d "
530                         "Our IP:%d.%d.%d.%d\n",
531                         slave_core_id,
532                         rte_eal_remote_launch(lcore_main, NULL, slave_core_id),
533                         BOND_IP_1,
534                         BOND_IP_2,
535                         BOND_IP_3,
536                         BOND_IP_4
537                 );
538 }
539
540 cmdline_parse_token_string_t cmd_start_start =
541         TOKEN_STRING_INITIALIZER(struct cmd_start_result, start, "start");
542
543 cmdline_parse_inst_t cmd_start = {
544         .f = cmd_start_parsed,  /* function to call */
545         .data = NULL,      /* 2nd arg of func */
546         .help_str = "starts listening if not started at startup",
547         .tokens = {        /* token list, NULL terminated */
548                 (void *)&cmd_start_start,
549                 NULL,
550         },
551 };
552
553 struct cmd_help_result {
554         cmdline_fixed_string_t help;
555 };
556
557 static void cmd_help_parsed(__attribute__((unused)) void *parsed_result,
558                             struct cmdline *cl,
559                             __attribute__((unused)) void *data)
560 {
561         cmdline_printf(cl,
562                         "ALB - link bonding mode 6 example\n"
563                         "send IP        - sends one ARPrequest through bonding for IP.\n"
564                         "start          - starts listening ARPs.\n"
565                         "stop           - stops lcore_main.\n"
566                         "show           - shows some bond info: ex. active slaves etc.\n"
567                         "help           - prints help.\n"
568                         "quit           - terminate all threads and quit.\n"
569                        );
570 }
571
572 cmdline_parse_token_string_t cmd_help_help =
573         TOKEN_STRING_INITIALIZER(struct cmd_help_result, help, "help");
574
575 cmdline_parse_inst_t cmd_help = {
576         .f = cmd_help_parsed,  /* function to call */
577         .data = NULL,      /* 2nd arg of func */
578         .help_str = "show help",
579         .tokens = {        /* token list, NULL terminated */
580                 (void *)&cmd_help_help,
581                 NULL,
582         },
583 };
584
585 struct cmd_stop_result {
586         cmdline_fixed_string_t stop;
587 };
588
589 static void cmd_stop_parsed(__attribute__((unused)) void *parsed_result,
590                             struct cmdline *cl,
591                             __attribute__((unused)) void *data)
592 {
593         rte_spinlock_trylock(&global_flag_stru_p->lock);
594         if (global_flag_stru_p->LcoreMainIsRunning == 0)        {
595                 cmdline_printf(cl,
596                                         "lcore_main not running on core:%d\n",
597                                         global_flag_stru_p->LcoreMainCore);
598                 rte_spinlock_unlock(&global_flag_stru_p->lock);
599                 return;
600         }
601         global_flag_stru_p->LcoreMainIsRunning = 0;
602         if (rte_eal_wait_lcore(global_flag_stru_p->LcoreMainCore) < 0)
603                 cmdline_printf(cl,
604                                 "error: lcore_main can not stop on core:%d\n",
605                                 global_flag_stru_p->LcoreMainCore);
606         else
607                 cmdline_printf(cl,
608                                 "lcore_main stopped on core:%d\n",
609                                 global_flag_stru_p->LcoreMainCore);
610         rte_spinlock_unlock(&global_flag_stru_p->lock);
611 }
612
613 cmdline_parse_token_string_t cmd_stop_stop =
614         TOKEN_STRING_INITIALIZER(struct cmd_stop_result, stop, "stop");
615
616 cmdline_parse_inst_t cmd_stop = {
617         .f = cmd_stop_parsed,  /* function to call */
618         .data = NULL,      /* 2nd arg of func */
619         .help_str = "this command do not handle any arguments",
620         .tokens = {        /* token list, NULL terminated */
621                 (void *)&cmd_stop_stop,
622                 NULL,
623         },
624 };
625
626 struct cmd_quit_result {
627         cmdline_fixed_string_t quit;
628 };
629
630 static void cmd_quit_parsed(__attribute__((unused)) void *parsed_result,
631                             struct cmdline *cl,
632                             __attribute__((unused)) void *data)
633 {
634         rte_spinlock_trylock(&global_flag_stru_p->lock);
635         if (global_flag_stru_p->LcoreMainIsRunning == 0)        {
636                 cmdline_printf(cl,
637                                         "lcore_main not running on core:%d\n",
638                                         global_flag_stru_p->LcoreMainCore);
639                 rte_spinlock_unlock(&global_flag_stru_p->lock);
640                 cmdline_quit(cl);
641                 return;
642         }
643         global_flag_stru_p->LcoreMainIsRunning = 0;
644         if (rte_eal_wait_lcore(global_flag_stru_p->LcoreMainCore) < 0)
645                 cmdline_printf(cl,
646                                 "error: lcore_main can not stop on core:%d\n",
647                                 global_flag_stru_p->LcoreMainCore);
648         else
649                 cmdline_printf(cl,
650                                 "lcore_main stopped on core:%d\n",
651                                 global_flag_stru_p->LcoreMainCore);
652         rte_spinlock_unlock(&global_flag_stru_p->lock);
653         cmdline_quit(cl);
654 }
655
656 cmdline_parse_token_string_t cmd_quit_quit =
657         TOKEN_STRING_INITIALIZER(struct cmd_quit_result, quit, "quit");
658
659 cmdline_parse_inst_t cmd_quit = {
660         .f = cmd_quit_parsed,  /* function to call */
661         .data = NULL,      /* 2nd arg of func */
662         .help_str = "this command do not handle any arguments",
663         .tokens = {        /* token list, NULL terminated */
664                 (void *)&cmd_quit_quit,
665                 NULL,
666         },
667 };
668
669 struct cmd_show_result {
670         cmdline_fixed_string_t show;
671 };
672
673 static void cmd_show_parsed(__attribute__((unused)) void *parsed_result,
674                             struct cmdline *cl,
675                             __attribute__((unused)) void *data)
676 {
677         uint16_t slaves[16] = {0};
678         uint8_t len = 16;
679         struct ether_addr addr;
680         uint16_t i = 0;
681
682         while (i < slaves_count)        {
683                 rte_eth_macaddr_get(i, &addr);
684                 PRINT_MAC(addr);
685                 printf("\n");
686                 i++;
687         }
688
689         rte_spinlock_trylock(&global_flag_stru_p->lock);
690         cmdline_printf(cl,
691                         "Active_slaves:%d "
692                         "packets received:Tot:%d Arp:%d IPv4:%d\n",
693                         rte_eth_bond_active_slaves_get(BOND_PORT, slaves, len),
694                         global_flag_stru_p->port_packets[0],
695                         global_flag_stru_p->port_packets[1],
696                         global_flag_stru_p->port_packets[2]);
697         rte_spinlock_unlock(&global_flag_stru_p->lock);
698 }
699
700 cmdline_parse_token_string_t cmd_show_show =
701         TOKEN_STRING_INITIALIZER(struct cmd_show_result, show, "show");
702
703 cmdline_parse_inst_t cmd_show = {
704         .f = cmd_show_parsed,  /* function to call */
705         .data = NULL,      /* 2nd arg of func */
706         .help_str = "this command do not handle any arguments",
707         .tokens = {        /* token list, NULL terminated */
708                 (void *)&cmd_show_show,
709                 NULL,
710         },
711 };
712
713 /****** CONTEXT (list of instruction) */
714
715 cmdline_parse_ctx_t main_ctx[] = {
716         (cmdline_parse_inst_t *)&cmd_start,
717         (cmdline_parse_inst_t *)&cmd_obj_send,
718         (cmdline_parse_inst_t *)&cmd_stop,
719         (cmdline_parse_inst_t *)&cmd_show,
720         (cmdline_parse_inst_t *)&cmd_quit,
721         (cmdline_parse_inst_t *)&cmd_help,
722         NULL,
723 };
724
725 /* prompt function, called from main on MASTER lcore */
726 static void prompt(__attribute__((unused)) void *arg1)
727 {
728         struct cmdline *cl;
729
730         cl = cmdline_stdin_new(main_ctx, "bond6>");
731         if (cl != NULL) {
732                 cmdline_interact(cl);
733                 cmdline_stdin_exit(cl);
734         }
735 }
736
737 /* Main function, does initialisation and calls the per-lcore functions */
738 int
739 main(int argc, char *argv[])
740 {
741         int ret;
742         uint8_t nb_ports, i;
743
744         /* init EAL */
745         ret = rte_eal_init(argc, argv);
746         rte_eal_devargs_dump(stdout);
747         if (ret < 0)
748                 rte_exit(EXIT_FAILURE, "Error with EAL initialization\n");
749         argc -= ret;
750         argv += ret;
751
752         nb_ports = rte_eth_dev_count();
753         if (nb_ports == 0)
754                 rte_exit(EXIT_FAILURE, "Give at least one port\n");
755         else if (nb_ports > MAX_PORTS)
756                 rte_exit(EXIT_FAILURE, "You can have max 4 ports\n");
757
758         mbuf_pool = rte_pktmbuf_pool_create("MBUF_POOL", NB_MBUF, 32,
759                 0, RTE_MBUF_DEFAULT_BUF_SIZE, rte_socket_id());
760         if (mbuf_pool == NULL)
761                 rte_exit(EXIT_FAILURE, "Cannot create mbuf pool\n");
762
763         /* initialize all ports */
764         slaves_count = nb_ports;
765         for (i = 0; i < nb_ports; i++) {
766                 slave_port_init(i, mbuf_pool);
767                 slaves[i] = i;
768         }
769
770         bond_port_init(mbuf_pool);
771
772         rte_spinlock_init(&global_flag_stru_p->lock);
773         int slave_core_id = rte_lcore_id();
774
775         /* check state of lcores */
776         RTE_LCORE_FOREACH_SLAVE(slave_core_id) {
777         if (lcore_config[slave_core_id].state != WAIT)
778                 return -EBUSY;
779         }
780         /* start lcore main on core != master_core - ARP response thread */
781         slave_core_id = rte_get_next_lcore(rte_lcore_id(), 1, 0);
782         if ((slave_core_id >= RTE_MAX_LCORE) || (slave_core_id == 0))
783                 return -EPERM;
784
785         global_flag_stru_p->LcoreMainIsRunning = 1;
786         global_flag_stru_p->LcoreMainCore = slave_core_id;
787         printf("Starting lcore_main on core %d:%d Our IP:%d.%d.%d.%d\n",
788                         slave_core_id,
789                         rte_eal_remote_launch((lcore_function_t *)lcore_main,
790                                         NULL,
791                                         slave_core_id),
792                         BOND_IP_1,
793                         BOND_IP_2,
794                         BOND_IP_3,
795                         BOND_IP_4
796                 );
797
798         /* Start prompt for user interact */
799         prompt(NULL);
800
801         rte_delay_ms(100);
802         return 0;
803 }