98415d66d335282bcbba0fb6a241c857c8e107c8
[dpdk.git] / examples / bond / main.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2010-2015 Intel Corporation
3  */
4
5 #include <stdint.h>
6 #include <sys/queue.h>
7 #include <sys/socket.h>
8 #include <stdlib.h>
9 #include <string.h>
10 #include <stdio.h>
11 #include <assert.h>
12 #include <errno.h>
13 #include <signal.h>
14 #include <stdarg.h>
15 #include <inttypes.h>
16 #include <getopt.h>
17 #include <termios.h>
18 #include <unistd.h>
19 #include <pthread.h>
20
21 #include <rte_common.h>
22 #include <rte_log.h>
23 #include <rte_memory.h>
24 #include <rte_memcpy.h>
25 #include <rte_eal.h>
26 #include <rte_launch.h>
27 #include <rte_atomic.h>
28 #include <rte_cycles.h>
29 #include <rte_prefetch.h>
30 #include <rte_lcore.h>
31 #include <rte_per_lcore.h>
32 #include <rte_branch_prediction.h>
33 #include <rte_interrupts.h>
34 #include <rte_random.h>
35 #include <rte_debug.h>
36 #include <rte_ether.h>
37 #include <rte_ethdev.h>
38 #include <rte_mempool.h>
39 #include <rte_mbuf.h>
40 #include <rte_ip.h>
41 #include <rte_tcp.h>
42 #include <rte_arp.h>
43 #include <rte_spinlock.h>
44
45 #include <cmdline_rdline.h>
46 #include <cmdline_parse.h>
47 #include <cmdline_parse_num.h>
48 #include <cmdline_parse_string.h>
49 #include <cmdline_parse_ipaddr.h>
50 #include <cmdline_parse_etheraddr.h>
51 #include <cmdline_socket.h>
52 #include <cmdline.h>
53
54 #include "main.h"
55
56 #include <rte_devargs.h>
57
58
59 #include "rte_byteorder.h"
60 #include "rte_cpuflags.h"
61 #include "rte_eth_bond.h"
62
63 #define RTE_LOGTYPE_DCB RTE_LOGTYPE_USER1
64
65 #define NB_MBUF   (1024*8)
66
67 #define MAX_PKT_BURST 32
68 #define BURST_TX_DRAIN_US 100      /* TX drain every ~100us */
69 #define BURST_RX_INTERVAL_NS (10) /* RX poll interval ~100ns */
70
71 /*
72  * RX and TX Prefetch, Host, and Write-back threshold values should be
73  * carefully set for optimal performance. Consult the network
74  * controller's datasheet and supporting DPDK documentation for guidance
75  * on how these parameters should be set.
76  */
77 #define RX_PTHRESH 8 /**< Default values of RX prefetch threshold reg. */
78 #define RX_HTHRESH 8 /**< Default values of RX host threshold reg. */
79 #define RX_WTHRESH 4 /**< Default values of RX write-back threshold reg. */
80 #define RX_FTHRESH (MAX_PKT_BURST * 2)/**< Default values of RX free threshold reg. */
81
82 /*
83  * These default values are optimized for use with the Intel(R) 82599 10 GbE
84  * Controller and the DPDK ixgbe PMD. Consider using other values for other
85  * network controllers and/or network drivers.
86  */
87 #define TX_PTHRESH 36 /**< Default values of TX prefetch threshold reg. */
88 #define TX_HTHRESH 0  /**< Default values of TX host threshold reg. */
89 #define TX_WTHRESH 0  /**< Default values of TX write-back threshold reg. */
90
91 /*
92  * Configurable number of RX/TX ring descriptors
93  */
94 #define RTE_RX_DESC_DEFAULT 1024
95 #define RTE_TX_DESC_DEFAULT 1024
96
97 #define BOND_IP_1       7
98 #define BOND_IP_2       0
99 #define BOND_IP_3       0
100 #define BOND_IP_4       10
101
102 /* not defined under linux */
103 #ifndef NIPQUAD
104 #define NIPQUAD_FMT "%u.%u.%u.%u"
105 #endif
106
107 #define MAX_PORTS       4
108 #define PRINT_MAC(addr)         printf("%02"PRIx8":%02"PRIx8":%02"PRIx8 \
109                 ":%02"PRIx8":%02"PRIx8":%02"PRIx8,      \
110                 addr.addr_bytes[0], addr.addr_bytes[1], addr.addr_bytes[2], \
111                 addr.addr_bytes[3], addr.addr_bytes[4], addr.addr_bytes[5])
112
113 uint16_t slaves[RTE_MAX_ETHPORTS];
114 uint16_t slaves_count;
115
116 static uint16_t BOND_PORT = 0xffff;
117
118 static struct rte_mempool *mbuf_pool;
119
120 static struct rte_eth_conf port_conf = {
121         .rxmode = {
122                 .mq_mode = ETH_MQ_RX_NONE,
123                 .max_rx_pkt_len = ETHER_MAX_LEN,
124                 .split_hdr_size = 0,
125                 .offloads = DEV_RX_OFFLOAD_CRC_STRIP,
126         },
127         .rx_adv_conf = {
128                 .rss_conf = {
129                         .rss_key = NULL,
130                         .rss_hf = ETH_RSS_IP,
131                 },
132         },
133         .txmode = {
134                 .mq_mode = ETH_MQ_TX_NONE,
135         },
136 };
137
138 static void
139 slave_port_init(uint16_t portid, struct rte_mempool *mbuf_pool)
140 {
141         int retval;
142         uint16_t nb_rxd = RTE_RX_DESC_DEFAULT;
143         uint16_t nb_txd = RTE_TX_DESC_DEFAULT;
144         struct rte_eth_dev_info dev_info;
145         struct rte_eth_rxconf rxq_conf;
146         struct rte_eth_txconf txq_conf;
147         struct rte_eth_conf local_port_conf = port_conf;
148
149         if (!rte_eth_dev_is_valid_port(portid))
150                 rte_exit(EXIT_FAILURE, "Invalid port\n");
151
152         rte_eth_dev_info_get(portid, &dev_info);
153         if (dev_info.tx_offload_capa & DEV_TX_OFFLOAD_MBUF_FAST_FREE)
154                 local_port_conf.txmode.offloads |=
155                         DEV_TX_OFFLOAD_MBUF_FAST_FREE;
156         retval = rte_eth_dev_configure(portid, 1, 1, &local_port_conf);
157         if (retval != 0)
158                 rte_exit(EXIT_FAILURE, "port %u: configuration failed (res=%d)\n",
159                                 portid, retval);
160
161         retval = rte_eth_dev_adjust_nb_rx_tx_desc(portid, &nb_rxd, &nb_txd);
162         if (retval != 0)
163                 rte_exit(EXIT_FAILURE, "port %u: rte_eth_dev_adjust_nb_rx_tx_desc "
164                                 "failed (res=%d)\n", portid, retval);
165
166         /* RX setup */
167         rxq_conf = dev_info.default_rxconf;
168         rxq_conf.offloads = local_port_conf.rxmode.offloads;
169         retval = rte_eth_rx_queue_setup(portid, 0, nb_rxd,
170                                         rte_eth_dev_socket_id(portid),
171                                         &rxq_conf,
172                                         mbuf_pool);
173         if (retval < 0)
174                 rte_exit(retval, " port %u: RX queue 0 setup failed (res=%d)",
175                                 portid, retval);
176
177         /* TX setup */
178         txq_conf = dev_info.default_txconf;
179         txq_conf.offloads = local_port_conf.txmode.offloads;
180         retval = rte_eth_tx_queue_setup(portid, 0, nb_txd,
181                                 rte_eth_dev_socket_id(portid), &txq_conf);
182
183         if (retval < 0)
184                 rte_exit(retval, "port %u: TX queue 0 setup failed (res=%d)",
185                                 portid, retval);
186
187         retval  = rte_eth_dev_start(portid);
188         if (retval < 0)
189                 rte_exit(retval,
190                                 "Start port %d failed (res=%d)",
191                                 portid, retval);
192
193         struct ether_addr addr;
194
195         rte_eth_macaddr_get(portid, &addr);
196         printf("Port %u MAC: ", portid);
197         PRINT_MAC(addr);
198         printf("\n");
199 }
200
201 static void
202 bond_port_init(struct rte_mempool *mbuf_pool)
203 {
204         int retval;
205         uint8_t i;
206         uint16_t nb_rxd = RTE_RX_DESC_DEFAULT;
207         uint16_t nb_txd = RTE_TX_DESC_DEFAULT;
208         struct rte_eth_dev_info dev_info;
209         struct rte_eth_rxconf rxq_conf;
210         struct rte_eth_txconf txq_conf;
211         struct rte_eth_conf local_port_conf = port_conf;
212
213         retval = rte_eth_bond_create("net_bonding0", BONDING_MODE_ALB,
214                         0 /*SOCKET_ID_ANY*/);
215         if (retval < 0)
216                 rte_exit(EXIT_FAILURE,
217                                 "Faled to create bond port\n");
218
219         BOND_PORT = retval;
220
221         rte_eth_dev_info_get(BOND_PORT, &dev_info);
222         if (dev_info.tx_offload_capa & DEV_TX_OFFLOAD_MBUF_FAST_FREE)
223                 local_port_conf.txmode.offloads |=
224                         DEV_TX_OFFLOAD_MBUF_FAST_FREE;
225         retval = rte_eth_dev_configure(BOND_PORT, 1, 1, &local_port_conf);
226         if (retval != 0)
227                 rte_exit(EXIT_FAILURE, "port %u: configuration failed (res=%d)\n",
228                                 BOND_PORT, retval);
229
230         retval = rte_eth_dev_adjust_nb_rx_tx_desc(BOND_PORT, &nb_rxd, &nb_txd);
231         if (retval != 0)
232                 rte_exit(EXIT_FAILURE, "port %u: rte_eth_dev_adjust_nb_rx_tx_desc "
233                                 "failed (res=%d)\n", BOND_PORT, retval);
234
235         /* RX setup */
236         rxq_conf = dev_info.default_rxconf;
237         rxq_conf.offloads = local_port_conf.rxmode.offloads;
238         retval = rte_eth_rx_queue_setup(BOND_PORT, 0, nb_rxd,
239                                         rte_eth_dev_socket_id(BOND_PORT),
240                                         &rxq_conf, mbuf_pool);
241         if (retval < 0)
242                 rte_exit(retval, " port %u: RX queue 0 setup failed (res=%d)",
243                                 BOND_PORT, retval);
244
245         /* TX setup */
246         txq_conf = dev_info.default_txconf;
247         txq_conf.offloads = local_port_conf.txmode.offloads;
248         retval = rte_eth_tx_queue_setup(BOND_PORT, 0, nb_txd,
249                                 rte_eth_dev_socket_id(BOND_PORT), &txq_conf);
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         if (created_pkt == NULL) {
442                 cmdline_printf(cl, "Failed to allocate mbuf\n");
443                 return;
444         }
445
446         pkt_size = sizeof(struct ether_hdr) + sizeof(struct arp_hdr);
447         created_pkt->data_len = pkt_size;
448         created_pkt->pkt_len = pkt_size;
449
450         eth_hdr = rte_pktmbuf_mtod(created_pkt, struct ether_hdr *);
451         rte_eth_macaddr_get(BOND_PORT, &eth_hdr->s_addr);
452         memset(&eth_hdr->d_addr, 0xFF, ETHER_ADDR_LEN);
453         eth_hdr->ether_type = rte_cpu_to_be_16(ETHER_TYPE_ARP);
454
455         arp_hdr = (struct arp_hdr *)((char *)eth_hdr + sizeof(struct ether_hdr));
456         arp_hdr->arp_hrd = rte_cpu_to_be_16(ARP_HRD_ETHER);
457         arp_hdr->arp_pro = rte_cpu_to_be_16(ETHER_TYPE_IPv4);
458         arp_hdr->arp_hln = ETHER_ADDR_LEN;
459         arp_hdr->arp_pln = sizeof(uint32_t);
460         arp_hdr->arp_op = rte_cpu_to_be_16(ARP_OP_REQUEST);
461
462         rte_eth_macaddr_get(BOND_PORT, &arp_hdr->arp_data.arp_sha);
463         arp_hdr->arp_data.arp_sip = bond_ip;
464         memset(&arp_hdr->arp_data.arp_tha, 0, ETHER_ADDR_LEN);
465         arp_hdr->arp_data.arp_tip =
466                           ((unsigned char *)&res->ip.addr.ipv4)[0]        |
467                          (((unsigned char *)&res->ip.addr.ipv4)[1] << 8)  |
468                          (((unsigned char *)&res->ip.addr.ipv4)[2] << 16) |
469                          (((unsigned char *)&res->ip.addr.ipv4)[3] << 24);
470         rte_eth_tx_burst(BOND_PORT, 0, &created_pkt, 1);
471
472         rte_delay_ms(100);
473         cmdline_printf(cl, "\n");
474 }
475
476 cmdline_parse_token_string_t cmd_obj_action_send =
477         TOKEN_STRING_INITIALIZER(struct cmd_obj_send_result, action, "send");
478 cmdline_parse_token_ipaddr_t cmd_obj_ip =
479         TOKEN_IPV4_INITIALIZER(struct cmd_obj_send_result, ip);
480
481 cmdline_parse_inst_t cmd_obj_send = {
482         .f = cmd_obj_send_parsed,  /* function to call */
483         .data = NULL,      /* 2nd arg of func */
484         .help_str = "send client_ip",
485         .tokens = {        /* token list, NULL terminated */
486                 (void *)&cmd_obj_action_send,
487                 (void *)&cmd_obj_ip,
488                 NULL,
489         },
490 };
491
492 struct cmd_start_result {
493         cmdline_fixed_string_t start;
494 };
495
496 static void cmd_start_parsed(__attribute__((unused)) void *parsed_result,
497                                struct cmdline *cl,
498                                __attribute__((unused)) void *data)
499 {
500         int slave_core_id = rte_lcore_id();
501
502         rte_spinlock_trylock(&global_flag_stru_p->lock);
503         if (global_flag_stru_p->LcoreMainIsRunning == 0)        {
504                 if (lcore_config[global_flag_stru_p->LcoreMainCore].state != WAIT)      {
505                         rte_spinlock_unlock(&global_flag_stru_p->lock);
506                         return;
507                 }
508                 rte_spinlock_unlock(&global_flag_stru_p->lock);
509         } else {
510                 cmdline_printf(cl, "lcore_main already running on core:%d\n",
511                                 global_flag_stru_p->LcoreMainCore);
512                 rte_spinlock_unlock(&global_flag_stru_p->lock);
513                 return;
514         }
515
516         /* start lcore main on core != master_core - ARP response thread */
517         slave_core_id = rte_get_next_lcore(rte_lcore_id(), 1, 0);
518         if ((slave_core_id >= RTE_MAX_LCORE) || (slave_core_id == 0))
519                 return;
520
521         rte_spinlock_trylock(&global_flag_stru_p->lock);
522         global_flag_stru_p->LcoreMainIsRunning = 1;
523         rte_spinlock_unlock(&global_flag_stru_p->lock);
524         cmdline_printf(cl,
525                         "Starting lcore_main on core %d:%d "
526                         "Our IP:%d.%d.%d.%d\n",
527                         slave_core_id,
528                         rte_eal_remote_launch(lcore_main, NULL, slave_core_id),
529                         BOND_IP_1,
530                         BOND_IP_2,
531                         BOND_IP_3,
532                         BOND_IP_4
533                 );
534 }
535
536 cmdline_parse_token_string_t cmd_start_start =
537         TOKEN_STRING_INITIALIZER(struct cmd_start_result, start, "start");
538
539 cmdline_parse_inst_t cmd_start = {
540         .f = cmd_start_parsed,  /* function to call */
541         .data = NULL,      /* 2nd arg of func */
542         .help_str = "starts listening if not started at startup",
543         .tokens = {        /* token list, NULL terminated */
544                 (void *)&cmd_start_start,
545                 NULL,
546         },
547 };
548
549 struct cmd_help_result {
550         cmdline_fixed_string_t help;
551 };
552
553 static void cmd_help_parsed(__attribute__((unused)) void *parsed_result,
554                             struct cmdline *cl,
555                             __attribute__((unused)) void *data)
556 {
557         cmdline_printf(cl,
558                         "ALB - link bonding mode 6 example\n"
559                         "send IP        - sends one ARPrequest through bonding for IP.\n"
560                         "start          - starts listening ARPs.\n"
561                         "stop           - stops lcore_main.\n"
562                         "show           - shows some bond info: ex. active slaves etc.\n"
563                         "help           - prints help.\n"
564                         "quit           - terminate all threads and quit.\n"
565                        );
566 }
567
568 cmdline_parse_token_string_t cmd_help_help =
569         TOKEN_STRING_INITIALIZER(struct cmd_help_result, help, "help");
570
571 cmdline_parse_inst_t cmd_help = {
572         .f = cmd_help_parsed,  /* function to call */
573         .data = NULL,      /* 2nd arg of func */
574         .help_str = "show help",
575         .tokens = {        /* token list, NULL terminated */
576                 (void *)&cmd_help_help,
577                 NULL,
578         },
579 };
580
581 struct cmd_stop_result {
582         cmdline_fixed_string_t stop;
583 };
584
585 static void cmd_stop_parsed(__attribute__((unused)) void *parsed_result,
586                             struct cmdline *cl,
587                             __attribute__((unused)) void *data)
588 {
589         rte_spinlock_trylock(&global_flag_stru_p->lock);
590         if (global_flag_stru_p->LcoreMainIsRunning == 0)        {
591                 cmdline_printf(cl,
592                                         "lcore_main not running on core:%d\n",
593                                         global_flag_stru_p->LcoreMainCore);
594                 rte_spinlock_unlock(&global_flag_stru_p->lock);
595                 return;
596         }
597         global_flag_stru_p->LcoreMainIsRunning = 0;
598         if (rte_eal_wait_lcore(global_flag_stru_p->LcoreMainCore) < 0)
599                 cmdline_printf(cl,
600                                 "error: lcore_main can not stop on core:%d\n",
601                                 global_flag_stru_p->LcoreMainCore);
602         else
603                 cmdline_printf(cl,
604                                 "lcore_main stopped on core:%d\n",
605                                 global_flag_stru_p->LcoreMainCore);
606         rte_spinlock_unlock(&global_flag_stru_p->lock);
607 }
608
609 cmdline_parse_token_string_t cmd_stop_stop =
610         TOKEN_STRING_INITIALIZER(struct cmd_stop_result, stop, "stop");
611
612 cmdline_parse_inst_t cmd_stop = {
613         .f = cmd_stop_parsed,  /* function to call */
614         .data = NULL,      /* 2nd arg of func */
615         .help_str = "this command do not handle any arguments",
616         .tokens = {        /* token list, NULL terminated */
617                 (void *)&cmd_stop_stop,
618                 NULL,
619         },
620 };
621
622 struct cmd_quit_result {
623         cmdline_fixed_string_t quit;
624 };
625
626 static void cmd_quit_parsed(__attribute__((unused)) void *parsed_result,
627                             struct cmdline *cl,
628                             __attribute__((unused)) void *data)
629 {
630         rte_spinlock_trylock(&global_flag_stru_p->lock);
631         if (global_flag_stru_p->LcoreMainIsRunning == 0)        {
632                 cmdline_printf(cl,
633                                         "lcore_main not running on core:%d\n",
634                                         global_flag_stru_p->LcoreMainCore);
635                 rte_spinlock_unlock(&global_flag_stru_p->lock);
636                 cmdline_quit(cl);
637                 return;
638         }
639         global_flag_stru_p->LcoreMainIsRunning = 0;
640         if (rte_eal_wait_lcore(global_flag_stru_p->LcoreMainCore) < 0)
641                 cmdline_printf(cl,
642                                 "error: lcore_main can not stop on core:%d\n",
643                                 global_flag_stru_p->LcoreMainCore);
644         else
645                 cmdline_printf(cl,
646                                 "lcore_main stopped on core:%d\n",
647                                 global_flag_stru_p->LcoreMainCore);
648         rte_spinlock_unlock(&global_flag_stru_p->lock);
649         cmdline_quit(cl);
650 }
651
652 cmdline_parse_token_string_t cmd_quit_quit =
653         TOKEN_STRING_INITIALIZER(struct cmd_quit_result, quit, "quit");
654
655 cmdline_parse_inst_t cmd_quit = {
656         .f = cmd_quit_parsed,  /* function to call */
657         .data = NULL,      /* 2nd arg of func */
658         .help_str = "this command do not handle any arguments",
659         .tokens = {        /* token list, NULL terminated */
660                 (void *)&cmd_quit_quit,
661                 NULL,
662         },
663 };
664
665 struct cmd_show_result {
666         cmdline_fixed_string_t show;
667 };
668
669 static void cmd_show_parsed(__attribute__((unused)) void *parsed_result,
670                             struct cmdline *cl,
671                             __attribute__((unused)) void *data)
672 {
673         uint16_t slaves[16] = {0};
674         uint8_t len = 16;
675         struct ether_addr addr;
676         uint16_t i = 0;
677
678         while (i < slaves_count)        {
679                 rte_eth_macaddr_get(i, &addr);
680                 PRINT_MAC(addr);
681                 printf("\n");
682                 i++;
683         }
684
685         rte_spinlock_trylock(&global_flag_stru_p->lock);
686         cmdline_printf(cl,
687                         "Active_slaves:%d "
688                         "packets received:Tot:%d Arp:%d IPv4:%d\n",
689                         rte_eth_bond_active_slaves_get(BOND_PORT, slaves, len),
690                         global_flag_stru_p->port_packets[0],
691                         global_flag_stru_p->port_packets[1],
692                         global_flag_stru_p->port_packets[2]);
693         rte_spinlock_unlock(&global_flag_stru_p->lock);
694 }
695
696 cmdline_parse_token_string_t cmd_show_show =
697         TOKEN_STRING_INITIALIZER(struct cmd_show_result, show, "show");
698
699 cmdline_parse_inst_t cmd_show = {
700         .f = cmd_show_parsed,  /* function to call */
701         .data = NULL,      /* 2nd arg of func */
702         .help_str = "this command do not handle any arguments",
703         .tokens = {        /* token list, NULL terminated */
704                 (void *)&cmd_show_show,
705                 NULL,
706         },
707 };
708
709 /****** CONTEXT (list of instruction) */
710
711 cmdline_parse_ctx_t main_ctx[] = {
712         (cmdline_parse_inst_t *)&cmd_start,
713         (cmdline_parse_inst_t *)&cmd_obj_send,
714         (cmdline_parse_inst_t *)&cmd_stop,
715         (cmdline_parse_inst_t *)&cmd_show,
716         (cmdline_parse_inst_t *)&cmd_quit,
717         (cmdline_parse_inst_t *)&cmd_help,
718         NULL,
719 };
720
721 /* prompt function, called from main on MASTER lcore */
722 static void prompt(__attribute__((unused)) void *arg1)
723 {
724         struct cmdline *cl;
725
726         cl = cmdline_stdin_new(main_ctx, "bond6>");
727         if (cl != NULL) {
728                 cmdline_interact(cl);
729                 cmdline_stdin_exit(cl);
730         }
731 }
732
733 /* Main function, does initialisation and calls the per-lcore functions */
734 int
735 main(int argc, char *argv[])
736 {
737         int ret;
738         uint16_t nb_ports, i;
739
740         /* init EAL */
741         ret = rte_eal_init(argc, argv);
742         rte_devargs_dump(stdout);
743         if (ret < 0)
744                 rte_exit(EXIT_FAILURE, "Error with EAL initialization\n");
745         argc -= ret;
746         argv += ret;
747
748         nb_ports = rte_eth_dev_count_avail();
749         if (nb_ports == 0)
750                 rte_exit(EXIT_FAILURE, "Give at least one port\n");
751         else if (nb_ports > MAX_PORTS)
752                 rte_exit(EXIT_FAILURE, "You can have max 4 ports\n");
753
754         mbuf_pool = rte_pktmbuf_pool_create("MBUF_POOL", NB_MBUF, 32,
755                 0, RTE_MBUF_DEFAULT_BUF_SIZE, rte_socket_id());
756         if (mbuf_pool == NULL)
757                 rte_exit(EXIT_FAILURE, "Cannot create mbuf pool\n");
758
759         /* initialize all ports */
760         slaves_count = nb_ports;
761         RTE_ETH_FOREACH_DEV(i) {
762                 slave_port_init(i, mbuf_pool);
763                 slaves[i] = i;
764         }
765
766         bond_port_init(mbuf_pool);
767
768         rte_spinlock_init(&global_flag_stru_p->lock);
769         int slave_core_id = rte_lcore_id();
770
771         /* check state of lcores */
772         RTE_LCORE_FOREACH_SLAVE(slave_core_id) {
773         if (lcore_config[slave_core_id].state != WAIT)
774                 return -EBUSY;
775         }
776         /* start lcore main on core != master_core - ARP response thread */
777         slave_core_id = rte_get_next_lcore(rte_lcore_id(), 1, 0);
778         if ((slave_core_id >= RTE_MAX_LCORE) || (slave_core_id == 0))
779                 return -EPERM;
780
781         global_flag_stru_p->LcoreMainIsRunning = 1;
782         global_flag_stru_p->LcoreMainCore = slave_core_id;
783         printf("Starting lcore_main on core %d:%d Our IP:%d.%d.%d.%d\n",
784                         slave_core_id,
785                         rte_eal_remote_launch((lcore_function_t *)lcore_main,
786                                         NULL,
787                                         slave_core_id),
788                         BOND_IP_1,
789                         BOND_IP_2,
790                         BOND_IP_3,
791                         BOND_IP_4
792                 );
793
794         /* Start prompt for user interact */
795         prompt(NULL);
796
797         rte_delay_ms(100);
798         return 0;
799 }