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