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