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