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