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