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