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