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