xref: /f-stack/dpdk/examples/bond/main.c (revision 2d9fd380)
1d30ea906Sjfb8856606 /* SPDX-License-Identifier: BSD-3-Clause
2d30ea906Sjfb8856606  * Copyright(c) 2010-2015 Intel Corporation
3a9643ea8Slogwang  */
4a9643ea8Slogwang 
5a9643ea8Slogwang #include <stdint.h>
6a9643ea8Slogwang #include <sys/queue.h>
7a9643ea8Slogwang #include <sys/socket.h>
8a9643ea8Slogwang #include <stdlib.h>
9a9643ea8Slogwang #include <string.h>
10a9643ea8Slogwang #include <stdio.h>
11a9643ea8Slogwang #include <assert.h>
12a9643ea8Slogwang #include <errno.h>
13a9643ea8Slogwang #include <signal.h>
14a9643ea8Slogwang #include <stdarg.h>
15a9643ea8Slogwang #include <inttypes.h>
16a9643ea8Slogwang #include <getopt.h>
17a9643ea8Slogwang #include <termios.h>
18a9643ea8Slogwang #include <unistd.h>
19a9643ea8Slogwang #include <pthread.h>
20a9643ea8Slogwang 
21a9643ea8Slogwang #include <rte_common.h>
22a9643ea8Slogwang #include <rte_log.h>
23a9643ea8Slogwang #include <rte_memory.h>
24a9643ea8Slogwang #include <rte_memcpy.h>
25a9643ea8Slogwang #include <rte_eal.h>
26a9643ea8Slogwang #include <rte_launch.h>
27a9643ea8Slogwang #include <rte_atomic.h>
28a9643ea8Slogwang #include <rte_cycles.h>
29a9643ea8Slogwang #include <rte_prefetch.h>
30a9643ea8Slogwang #include <rte_lcore.h>
31a9643ea8Slogwang #include <rte_per_lcore.h>
32a9643ea8Slogwang #include <rte_branch_prediction.h>
33a9643ea8Slogwang #include <rte_interrupts.h>
34a9643ea8Slogwang #include <rte_random.h>
35a9643ea8Slogwang #include <rte_debug.h>
36a9643ea8Slogwang #include <rte_ether.h>
37a9643ea8Slogwang #include <rte_ethdev.h>
38a9643ea8Slogwang #include <rte_mempool.h>
39a9643ea8Slogwang #include <rte_mbuf.h>
40a9643ea8Slogwang #include <rte_ip.h>
41a9643ea8Slogwang #include <rte_tcp.h>
42a9643ea8Slogwang #include <rte_arp.h>
43a9643ea8Slogwang #include <rte_spinlock.h>
44*2d9fd380Sjfb8856606 #include <rte_devargs.h>
45*2d9fd380Sjfb8856606 #include <rte_byteorder.h>
46*2d9fd380Sjfb8856606 #include <rte_cpuflags.h>
47*2d9fd380Sjfb8856606 #include <rte_eth_bond.h>
48a9643ea8Slogwang 
49a9643ea8Slogwang #include <cmdline_rdline.h>
50a9643ea8Slogwang #include <cmdline_parse.h>
51a9643ea8Slogwang #include <cmdline_parse_num.h>
52a9643ea8Slogwang #include <cmdline_parse_string.h>
53a9643ea8Slogwang #include <cmdline_parse_ipaddr.h>
54a9643ea8Slogwang #include <cmdline_parse_etheraddr.h>
55a9643ea8Slogwang #include <cmdline_socket.h>
56a9643ea8Slogwang #include <cmdline.h>
57a9643ea8Slogwang 
58a9643ea8Slogwang #include "main.h"
59a9643ea8Slogwang 
60a9643ea8Slogwang #define RTE_LOGTYPE_DCB RTE_LOGTYPE_USER1
61a9643ea8Slogwang 
62a9643ea8Slogwang #define NB_MBUF   (1024*8)
63a9643ea8Slogwang 
64a9643ea8Slogwang #define MAX_PKT_BURST 32
65a9643ea8Slogwang #define BURST_TX_DRAIN_US 100      /* TX drain every ~100us */
66a9643ea8Slogwang #define BURST_RX_INTERVAL_NS (10) /* RX poll interval ~100ns */
67a9643ea8Slogwang 
68a9643ea8Slogwang /*
69a9643ea8Slogwang  * RX and TX Prefetch, Host, and Write-back threshold values should be
70a9643ea8Slogwang  * carefully set for optimal performance. Consult the network
71a9643ea8Slogwang  * controller's datasheet and supporting DPDK documentation for guidance
72a9643ea8Slogwang  * on how these parameters should be set.
73a9643ea8Slogwang  */
74a9643ea8Slogwang #define RX_PTHRESH 8 /**< Default values of RX prefetch threshold reg. */
75a9643ea8Slogwang #define RX_HTHRESH 8 /**< Default values of RX host threshold reg. */
76a9643ea8Slogwang #define RX_WTHRESH 4 /**< Default values of RX write-back threshold reg. */
77a9643ea8Slogwang #define RX_FTHRESH (MAX_PKT_BURST * 2)/**< Default values of RX free threshold reg. */
78a9643ea8Slogwang 
79a9643ea8Slogwang /*
80a9643ea8Slogwang  * These default values are optimized for use with the Intel(R) 82599 10 GbE
81a9643ea8Slogwang  * Controller and the DPDK ixgbe PMD. Consider using other values for other
82a9643ea8Slogwang  * network controllers and/or network drivers.
83a9643ea8Slogwang  */
84a9643ea8Slogwang #define TX_PTHRESH 36 /**< Default values of TX prefetch threshold reg. */
85a9643ea8Slogwang #define TX_HTHRESH 0  /**< Default values of TX host threshold reg. */
86a9643ea8Slogwang #define TX_WTHRESH 0  /**< Default values of TX write-back threshold reg. */
87a9643ea8Slogwang 
88a9643ea8Slogwang /*
89a9643ea8Slogwang  * Configurable number of RX/TX ring descriptors
90a9643ea8Slogwang  */
91d30ea906Sjfb8856606 #define RTE_RX_DESC_DEFAULT 1024
92d30ea906Sjfb8856606 #define RTE_TX_DESC_DEFAULT 1024
93a9643ea8Slogwang 
94a9643ea8Slogwang #define BOND_IP_1	7
95a9643ea8Slogwang #define BOND_IP_2	0
96a9643ea8Slogwang #define BOND_IP_3	0
97a9643ea8Slogwang #define BOND_IP_4	10
98a9643ea8Slogwang 
99a9643ea8Slogwang /* not defined under linux */
100a9643ea8Slogwang #ifndef NIPQUAD
101a9643ea8Slogwang #define NIPQUAD_FMT "%u.%u.%u.%u"
102a9643ea8Slogwang #endif
103a9643ea8Slogwang 
104a9643ea8Slogwang #define MAX_PORTS	4
105a9643ea8Slogwang #define PRINT_MAC(addr)		printf("%02"PRIx8":%02"PRIx8":%02"PRIx8 \
106a9643ea8Slogwang 		":%02"PRIx8":%02"PRIx8":%02"PRIx8,	\
107a9643ea8Slogwang 		addr.addr_bytes[0], addr.addr_bytes[1], addr.addr_bytes[2], \
108a9643ea8Slogwang 		addr.addr_bytes[3], addr.addr_bytes[4], addr.addr_bytes[5])
109a9643ea8Slogwang 
1102bfe3f2eSlogwang uint16_t slaves[RTE_MAX_ETHPORTS];
1112bfe3f2eSlogwang uint16_t slaves_count;
112a9643ea8Slogwang 
1132bfe3f2eSlogwang static uint16_t BOND_PORT = 0xffff;
114a9643ea8Slogwang 
115a9643ea8Slogwang static struct rte_mempool *mbuf_pool;
116a9643ea8Slogwang 
117a9643ea8Slogwang static struct rte_eth_conf port_conf = {
118a9643ea8Slogwang 	.rxmode = {
119a9643ea8Slogwang 		.mq_mode = ETH_MQ_RX_NONE,
1204418919fSjohnjiang 		.max_rx_pkt_len = RTE_ETHER_MAX_LEN,
121a9643ea8Slogwang 		.split_hdr_size = 0,
122a9643ea8Slogwang 	},
123a9643ea8Slogwang 	.rx_adv_conf = {
124a9643ea8Slogwang 		.rss_conf = {
125a9643ea8Slogwang 			.rss_key = NULL,
126a9643ea8Slogwang 			.rss_hf = ETH_RSS_IP,
127a9643ea8Slogwang 		},
128a9643ea8Slogwang 	},
129a9643ea8Slogwang 	.txmode = {
130a9643ea8Slogwang 		.mq_mode = ETH_MQ_TX_NONE,
131a9643ea8Slogwang 	},
132a9643ea8Slogwang };
133a9643ea8Slogwang 
134a9643ea8Slogwang static void
slave_port_init(uint16_t portid,struct rte_mempool * mbuf_pool)1352bfe3f2eSlogwang slave_port_init(uint16_t portid, struct rte_mempool *mbuf_pool)
136a9643ea8Slogwang {
137a9643ea8Slogwang 	int retval;
1382bfe3f2eSlogwang 	uint16_t nb_rxd = RTE_RX_DESC_DEFAULT;
1392bfe3f2eSlogwang 	uint16_t nb_txd = RTE_TX_DESC_DEFAULT;
140d30ea906Sjfb8856606 	struct rte_eth_dev_info dev_info;
141d30ea906Sjfb8856606 	struct rte_eth_rxconf rxq_conf;
142d30ea906Sjfb8856606 	struct rte_eth_txconf txq_conf;
143d30ea906Sjfb8856606 	struct rte_eth_conf local_port_conf = port_conf;
144a9643ea8Slogwang 
145d30ea906Sjfb8856606 	if (!rte_eth_dev_is_valid_port(portid))
146a9643ea8Slogwang 		rte_exit(EXIT_FAILURE, "Invalid port\n");
147a9643ea8Slogwang 
1484418919fSjohnjiang 	retval = rte_eth_dev_info_get(portid, &dev_info);
1494418919fSjohnjiang 	if (retval != 0)
1504418919fSjohnjiang 		rte_exit(EXIT_FAILURE,
1514418919fSjohnjiang 			"Error during getting device (port %u) info: %s\n",
1524418919fSjohnjiang 			portid, strerror(-retval));
1534418919fSjohnjiang 
154d30ea906Sjfb8856606 	if (dev_info.tx_offload_capa & DEV_TX_OFFLOAD_MBUF_FAST_FREE)
155d30ea906Sjfb8856606 		local_port_conf.txmode.offloads |=
156d30ea906Sjfb8856606 			DEV_TX_OFFLOAD_MBUF_FAST_FREE;
157d30ea906Sjfb8856606 
158d30ea906Sjfb8856606 	local_port_conf.rx_adv_conf.rss_conf.rss_hf &=
159d30ea906Sjfb8856606 		dev_info.flow_type_rss_offloads;
160d30ea906Sjfb8856606 	if (local_port_conf.rx_adv_conf.rss_conf.rss_hf !=
161d30ea906Sjfb8856606 			port_conf.rx_adv_conf.rss_conf.rss_hf) {
162d30ea906Sjfb8856606 		printf("Port %u modified RSS hash function based on hardware support,"
163d30ea906Sjfb8856606 			"requested:%#"PRIx64" configured:%#"PRIx64"\n",
164d30ea906Sjfb8856606 			portid,
165d30ea906Sjfb8856606 			port_conf.rx_adv_conf.rss_conf.rss_hf,
166d30ea906Sjfb8856606 			local_port_conf.rx_adv_conf.rss_conf.rss_hf);
167d30ea906Sjfb8856606 	}
168d30ea906Sjfb8856606 
169d30ea906Sjfb8856606 	retval = rte_eth_dev_configure(portid, 1, 1, &local_port_conf);
170a9643ea8Slogwang 	if (retval != 0)
171a9643ea8Slogwang 		rte_exit(EXIT_FAILURE, "port %u: configuration failed (res=%d)\n",
172a9643ea8Slogwang 				portid, retval);
173a9643ea8Slogwang 
1742bfe3f2eSlogwang 	retval = rte_eth_dev_adjust_nb_rx_tx_desc(portid, &nb_rxd, &nb_txd);
1752bfe3f2eSlogwang 	if (retval != 0)
1762bfe3f2eSlogwang 		rte_exit(EXIT_FAILURE, "port %u: rte_eth_dev_adjust_nb_rx_tx_desc "
1772bfe3f2eSlogwang 				"failed (res=%d)\n", portid, retval);
1782bfe3f2eSlogwang 
179a9643ea8Slogwang 	/* RX setup */
180d30ea906Sjfb8856606 	rxq_conf = dev_info.default_rxconf;
181d30ea906Sjfb8856606 	rxq_conf.offloads = local_port_conf.rxmode.offloads;
1822bfe3f2eSlogwang 	retval = rte_eth_rx_queue_setup(portid, 0, nb_rxd,
183d30ea906Sjfb8856606 					rte_eth_dev_socket_id(portid),
184d30ea906Sjfb8856606 					&rxq_conf,
185a9643ea8Slogwang 					mbuf_pool);
186a9643ea8Slogwang 	if (retval < 0)
187a9643ea8Slogwang 		rte_exit(retval, " port %u: RX queue 0 setup failed (res=%d)",
188a9643ea8Slogwang 				portid, retval);
189a9643ea8Slogwang 
190a9643ea8Slogwang 	/* TX setup */
191d30ea906Sjfb8856606 	txq_conf = dev_info.default_txconf;
192d30ea906Sjfb8856606 	txq_conf.offloads = local_port_conf.txmode.offloads;
1932bfe3f2eSlogwang 	retval = rte_eth_tx_queue_setup(portid, 0, nb_txd,
194d30ea906Sjfb8856606 				rte_eth_dev_socket_id(portid), &txq_conf);
195a9643ea8Slogwang 
196a9643ea8Slogwang 	if (retval < 0)
197a9643ea8Slogwang 		rte_exit(retval, "port %u: TX queue 0 setup failed (res=%d)",
198a9643ea8Slogwang 				portid, retval);
199a9643ea8Slogwang 
200a9643ea8Slogwang 	retval  = rte_eth_dev_start(portid);
201a9643ea8Slogwang 	if (retval < 0)
202a9643ea8Slogwang 		rte_exit(retval,
203a9643ea8Slogwang 				"Start port %d failed (res=%d)",
204a9643ea8Slogwang 				portid, retval);
205a9643ea8Slogwang 
2064418919fSjohnjiang 	struct rte_ether_addr addr;
207a9643ea8Slogwang 
2084418919fSjohnjiang 	retval = rte_eth_macaddr_get(portid, &addr);
2094418919fSjohnjiang 	if (retval != 0)
2104418919fSjohnjiang 		rte_exit(retval,
2114418919fSjohnjiang 				"Mac address get port %d failed (res=%d)",
2124418919fSjohnjiang 				portid, retval);
2134418919fSjohnjiang 
2142bfe3f2eSlogwang 	printf("Port %u MAC: ", portid);
215a9643ea8Slogwang 	PRINT_MAC(addr);
216a9643ea8Slogwang 	printf("\n");
217a9643ea8Slogwang }
218a9643ea8Slogwang 
219a9643ea8Slogwang static void
bond_port_init(struct rte_mempool * mbuf_pool)220a9643ea8Slogwang bond_port_init(struct rte_mempool *mbuf_pool)
221a9643ea8Slogwang {
222a9643ea8Slogwang 	int retval;
223a9643ea8Slogwang 	uint8_t i;
2242bfe3f2eSlogwang 	uint16_t nb_rxd = RTE_RX_DESC_DEFAULT;
2252bfe3f2eSlogwang 	uint16_t nb_txd = RTE_TX_DESC_DEFAULT;
226d30ea906Sjfb8856606 	struct rte_eth_dev_info dev_info;
227d30ea906Sjfb8856606 	struct rte_eth_rxconf rxq_conf;
228d30ea906Sjfb8856606 	struct rte_eth_txconf txq_conf;
229d30ea906Sjfb8856606 	struct rte_eth_conf local_port_conf = port_conf;
2301646932aSjfb8856606 	uint16_t wait_counter = 20;
231a9643ea8Slogwang 
2322bfe3f2eSlogwang 	retval = rte_eth_bond_create("net_bonding0", BONDING_MODE_ALB,
233a9643ea8Slogwang 			0 /*SOCKET_ID_ANY*/);
234a9643ea8Slogwang 	if (retval < 0)
235a9643ea8Slogwang 		rte_exit(EXIT_FAILURE,
236a9643ea8Slogwang 				"Faled to create bond port\n");
237a9643ea8Slogwang 
2382bfe3f2eSlogwang 	BOND_PORT = retval;
239a9643ea8Slogwang 
2404418919fSjohnjiang 	retval = rte_eth_dev_info_get(BOND_PORT, &dev_info);
2414418919fSjohnjiang 	if (retval != 0)
2424418919fSjohnjiang 		rte_exit(EXIT_FAILURE,
2434418919fSjohnjiang 			"Error during getting device (port %u) info: %s\n",
2444418919fSjohnjiang 			BOND_PORT, strerror(-retval));
2454418919fSjohnjiang 
246d30ea906Sjfb8856606 	if (dev_info.tx_offload_capa & DEV_TX_OFFLOAD_MBUF_FAST_FREE)
247d30ea906Sjfb8856606 		local_port_conf.txmode.offloads |=
248d30ea906Sjfb8856606 			DEV_TX_OFFLOAD_MBUF_FAST_FREE;
249d30ea906Sjfb8856606 	retval = rte_eth_dev_configure(BOND_PORT, 1, 1, &local_port_conf);
250a9643ea8Slogwang 	if (retval != 0)
251a9643ea8Slogwang 		rte_exit(EXIT_FAILURE, "port %u: configuration failed (res=%d)\n",
252a9643ea8Slogwang 				BOND_PORT, retval);
253a9643ea8Slogwang 
2542bfe3f2eSlogwang 	retval = rte_eth_dev_adjust_nb_rx_tx_desc(BOND_PORT, &nb_rxd, &nb_txd);
2552bfe3f2eSlogwang 	if (retval != 0)
2562bfe3f2eSlogwang 		rte_exit(EXIT_FAILURE, "port %u: rte_eth_dev_adjust_nb_rx_tx_desc "
2572bfe3f2eSlogwang 				"failed (res=%d)\n", BOND_PORT, retval);
2582bfe3f2eSlogwang 
2591646932aSjfb8856606 	for (i = 0; i < slaves_count; i++) {
2601646932aSjfb8856606 		if (rte_eth_bond_slave_add(BOND_PORT, slaves[i]) == -1)
2611646932aSjfb8856606 			rte_exit(-1, "Oooops! adding slave (%u) to bond (%u) failed!\n",
2621646932aSjfb8856606 					slaves[i], BOND_PORT);
2631646932aSjfb8856606 
2641646932aSjfb8856606 	}
2651646932aSjfb8856606 
266a9643ea8Slogwang 	/* RX setup */
267d30ea906Sjfb8856606 	rxq_conf = dev_info.default_rxconf;
268d30ea906Sjfb8856606 	rxq_conf.offloads = local_port_conf.rxmode.offloads;
2692bfe3f2eSlogwang 	retval = rte_eth_rx_queue_setup(BOND_PORT, 0, nb_rxd,
270d30ea906Sjfb8856606 					rte_eth_dev_socket_id(BOND_PORT),
271d30ea906Sjfb8856606 					&rxq_conf, mbuf_pool);
272a9643ea8Slogwang 	if (retval < 0)
273a9643ea8Slogwang 		rte_exit(retval, " port %u: RX queue 0 setup failed (res=%d)",
274a9643ea8Slogwang 				BOND_PORT, retval);
275a9643ea8Slogwang 
276a9643ea8Slogwang 	/* TX setup */
277d30ea906Sjfb8856606 	txq_conf = dev_info.default_txconf;
278d30ea906Sjfb8856606 	txq_conf.offloads = local_port_conf.txmode.offloads;
2792bfe3f2eSlogwang 	retval = rte_eth_tx_queue_setup(BOND_PORT, 0, nb_txd,
280d30ea906Sjfb8856606 				rte_eth_dev_socket_id(BOND_PORT), &txq_conf);
281a9643ea8Slogwang 
282a9643ea8Slogwang 	if (retval < 0)
283a9643ea8Slogwang 		rte_exit(retval, "port %u: TX queue 0 setup failed (res=%d)",
284a9643ea8Slogwang 				BOND_PORT, retval);
285a9643ea8Slogwang 
286a9643ea8Slogwang 	retval  = rte_eth_dev_start(BOND_PORT);
287a9643ea8Slogwang 	if (retval < 0)
288a9643ea8Slogwang 		rte_exit(retval, "Start port %d failed (res=%d)", BOND_PORT, retval);
289a9643ea8Slogwang 
2901646932aSjfb8856606 	printf("Waiting for slaves to become active...");
2911646932aSjfb8856606 	while (wait_counter) {
2921646932aSjfb8856606 		uint16_t act_slaves[16] = {0};
2931646932aSjfb8856606 		if (rte_eth_bond_active_slaves_get(BOND_PORT, act_slaves, 16) ==
2941646932aSjfb8856606 				slaves_count) {
2951646932aSjfb8856606 			printf("\n");
2961646932aSjfb8856606 			break;
2971646932aSjfb8856606 		}
2981646932aSjfb8856606 		sleep(1);
2991646932aSjfb8856606 		printf("...");
3001646932aSjfb8856606 		if (--wait_counter == 0)
3011646932aSjfb8856606 			rte_exit(-1, "\nFailed to activate slaves\n");
3021646932aSjfb8856606 	}
3031646932aSjfb8856606 
3044418919fSjohnjiang 	retval = rte_eth_promiscuous_enable(BOND_PORT);
3054418919fSjohnjiang 	if (retval != 0) {
3064418919fSjohnjiang 		rte_exit(EXIT_FAILURE,
3074418919fSjohnjiang 				"port %u: promiscuous mode enable failed: %s\n",
3084418919fSjohnjiang 				BOND_PORT, rte_strerror(-retval));
3094418919fSjohnjiang 		return;
3104418919fSjohnjiang 	}
311a9643ea8Slogwang 
3124418919fSjohnjiang 	struct rte_ether_addr addr;
313a9643ea8Slogwang 
3144418919fSjohnjiang 	retval = rte_eth_macaddr_get(BOND_PORT, &addr);
3154418919fSjohnjiang 	if (retval != 0)
3164418919fSjohnjiang 		rte_exit(retval, "port %u: Mac address get failed (res=%d)",
3174418919fSjohnjiang 				BOND_PORT, retval);
3184418919fSjohnjiang 
319a9643ea8Slogwang 	printf("Port %u MAC: ", (unsigned)BOND_PORT);
320a9643ea8Slogwang 		PRINT_MAC(addr);
321a9643ea8Slogwang 		printf("\n");
322a9643ea8Slogwang }
323a9643ea8Slogwang 
324a9643ea8Slogwang static inline size_t
get_vlan_offset(struct rte_ether_hdr * eth_hdr,uint16_t * proto)3254418919fSjohnjiang get_vlan_offset(struct rte_ether_hdr *eth_hdr, uint16_t *proto)
326a9643ea8Slogwang {
327a9643ea8Slogwang 	size_t vlan_offset = 0;
328a9643ea8Slogwang 
3294418919fSjohnjiang 	if (rte_cpu_to_be_16(RTE_ETHER_TYPE_VLAN) == *proto) {
3304418919fSjohnjiang 		struct rte_vlan_hdr *vlan_hdr =
3314418919fSjohnjiang 			(struct rte_vlan_hdr *)(eth_hdr + 1);
332a9643ea8Slogwang 
3334418919fSjohnjiang 		vlan_offset = sizeof(struct rte_vlan_hdr);
334a9643ea8Slogwang 		*proto = vlan_hdr->eth_proto;
335a9643ea8Slogwang 
3364418919fSjohnjiang 		if (rte_cpu_to_be_16(RTE_ETHER_TYPE_VLAN) == *proto) {
337a9643ea8Slogwang 			vlan_hdr = vlan_hdr + 1;
338a9643ea8Slogwang 
339a9643ea8Slogwang 			*proto = vlan_hdr->eth_proto;
3404418919fSjohnjiang 			vlan_offset += sizeof(struct rte_vlan_hdr);
341a9643ea8Slogwang 		}
342a9643ea8Slogwang 	}
343a9643ea8Slogwang 	return vlan_offset;
344a9643ea8Slogwang }
345a9643ea8Slogwang 
346a9643ea8Slogwang struct global_flag_stru_t {
347a9643ea8Slogwang 	int LcoreMainIsRunning;
348a9643ea8Slogwang 	int LcoreMainCore;
349a9643ea8Slogwang 	uint32_t port_packets[4];
350a9643ea8Slogwang 	rte_spinlock_t lock;
351a9643ea8Slogwang };
352a9643ea8Slogwang struct global_flag_stru_t global_flag_stru;
353a9643ea8Slogwang struct global_flag_stru_t *global_flag_stru_p = &global_flag_stru;
354a9643ea8Slogwang 
355a9643ea8Slogwang /*
356a9643ea8Slogwang  * Main thread that does the work, reading from INPUT_PORT
357a9643ea8Slogwang  * and writing to OUTPUT_PORT
358a9643ea8Slogwang  */
lcore_main(__rte_unused void * arg1)359*2d9fd380Sjfb8856606 static int lcore_main(__rte_unused void *arg1)
360a9643ea8Slogwang {
361a9643ea8Slogwang 	struct rte_mbuf *pkts[MAX_PKT_BURST] __rte_cache_aligned;
3624418919fSjohnjiang 	struct rte_ether_addr d_addr;
363a9643ea8Slogwang 
3644418919fSjohnjiang 	struct rte_ether_addr bond_mac_addr;
3654418919fSjohnjiang 	struct rte_ether_hdr *eth_hdr;
3664418919fSjohnjiang 	struct rte_arp_hdr *arp_hdr;
3674418919fSjohnjiang 	struct rte_ipv4_hdr *ipv4_hdr;
368a9643ea8Slogwang 	uint16_t ether_type, offset;
369a9643ea8Slogwang 
370a9643ea8Slogwang 	uint16_t rx_cnt;
371a9643ea8Slogwang 	uint32_t bond_ip;
372a9643ea8Slogwang 	int i = 0;
373a9643ea8Slogwang 	uint8_t is_free;
3744418919fSjohnjiang 	int ret;
375a9643ea8Slogwang 
376a9643ea8Slogwang 	bond_ip = BOND_IP_1 | (BOND_IP_2 << 8) |
377a9643ea8Slogwang 				(BOND_IP_3 << 16) | (BOND_IP_4 << 24);
378a9643ea8Slogwang 
379a9643ea8Slogwang 	rte_spinlock_trylock(&global_flag_stru_p->lock);
380a9643ea8Slogwang 
381a9643ea8Slogwang 	while (global_flag_stru_p->LcoreMainIsRunning) {
382a9643ea8Slogwang 		rte_spinlock_unlock(&global_flag_stru_p->lock);
383a9643ea8Slogwang 		rx_cnt = rte_eth_rx_burst(BOND_PORT, 0, pkts, MAX_PKT_BURST);
384a9643ea8Slogwang 		is_free = 0;
385a9643ea8Slogwang 
386a9643ea8Slogwang 		/* If didn't receive any packets, wait and go to next iteration */
387a9643ea8Slogwang 		if (rx_cnt == 0) {
388a9643ea8Slogwang 			rte_delay_us(50);
389a9643ea8Slogwang 			continue;
390a9643ea8Slogwang 		}
391a9643ea8Slogwang 
3924418919fSjohnjiang 		ret = rte_eth_macaddr_get(BOND_PORT, &bond_mac_addr);
3934418919fSjohnjiang 		if (ret != 0) {
3944418919fSjohnjiang 			printf("Bond (port %u) MAC address get failed: %s.\n"
3954418919fSjohnjiang 			       "%u packets dropped", BOND_PORT, strerror(-ret),
3964418919fSjohnjiang 			       rx_cnt);
3974418919fSjohnjiang 			rte_pktmbuf_free(pkts[i]);
3984418919fSjohnjiang 			continue;
3994418919fSjohnjiang 		}
4004418919fSjohnjiang 
401a9643ea8Slogwang 		/* Search incoming data for ARP packets and prepare response */
402a9643ea8Slogwang 		for (i = 0; i < rx_cnt; i++) {
403a9643ea8Slogwang 			if (rte_spinlock_trylock(&global_flag_stru_p->lock) == 1) {
404a9643ea8Slogwang 				global_flag_stru_p->port_packets[0]++;
405a9643ea8Slogwang 				rte_spinlock_unlock(&global_flag_stru_p->lock);
406a9643ea8Slogwang 			}
4074418919fSjohnjiang 			eth_hdr = rte_pktmbuf_mtod(pkts[i],
4084418919fSjohnjiang 						struct rte_ether_hdr *);
409a9643ea8Slogwang 			ether_type = eth_hdr->ether_type;
4104418919fSjohnjiang 			if (ether_type == rte_cpu_to_be_16(RTE_ETHER_TYPE_VLAN))
411a9643ea8Slogwang 				printf("VLAN taged frame, offset:");
412a9643ea8Slogwang 			offset = get_vlan_offset(eth_hdr, &ether_type);
413a9643ea8Slogwang 			if (offset > 0)
414a9643ea8Slogwang 				printf("%d\n", offset);
4154418919fSjohnjiang 			if (ether_type == rte_cpu_to_be_16(RTE_ETHER_TYPE_ARP)) {
416a9643ea8Slogwang 				if (rte_spinlock_trylock(&global_flag_stru_p->lock) == 1)     {
417a9643ea8Slogwang 					global_flag_stru_p->port_packets[1]++;
418a9643ea8Slogwang 					rte_spinlock_unlock(&global_flag_stru_p->lock);
419a9643ea8Slogwang 				}
4204418919fSjohnjiang 				arp_hdr = (struct rte_arp_hdr *)(
4214418919fSjohnjiang 					(char *)(eth_hdr + 1) + offset);
422a9643ea8Slogwang 				if (arp_hdr->arp_data.arp_tip == bond_ip) {
4234418919fSjohnjiang 					if (arp_hdr->arp_opcode == rte_cpu_to_be_16(RTE_ARP_OP_REQUEST)) {
4244418919fSjohnjiang 						arp_hdr->arp_opcode = rte_cpu_to_be_16(RTE_ARP_OP_REPLY);
425a9643ea8Slogwang 						/* Switch src and dst data and set bonding MAC */
4264418919fSjohnjiang 						rte_ether_addr_copy(&eth_hdr->s_addr, &eth_hdr->d_addr);
4274418919fSjohnjiang 						rte_ether_addr_copy(&bond_mac_addr, &eth_hdr->s_addr);
4284418919fSjohnjiang 						rte_ether_addr_copy(&arp_hdr->arp_data.arp_sha,
4294418919fSjohnjiang 								&arp_hdr->arp_data.arp_tha);
430a9643ea8Slogwang 						arp_hdr->arp_data.arp_tip = arp_hdr->arp_data.arp_sip;
4314418919fSjohnjiang 						rte_ether_addr_copy(&bond_mac_addr, &d_addr);
4324418919fSjohnjiang 						rte_ether_addr_copy(&d_addr, &arp_hdr->arp_data.arp_sha);
433a9643ea8Slogwang 						arp_hdr->arp_data.arp_sip = bond_ip;
434a9643ea8Slogwang 						rte_eth_tx_burst(BOND_PORT, 0, &pkts[i], 1);
435a9643ea8Slogwang 						is_free = 1;
436a9643ea8Slogwang 					} else {
437a9643ea8Slogwang 						rte_eth_tx_burst(BOND_PORT, 0, NULL, 0);
438a9643ea8Slogwang 					}
439a9643ea8Slogwang 				}
4404418919fSjohnjiang 			} else if (ether_type == rte_cpu_to_be_16(RTE_ETHER_TYPE_IPV4)) {
441a9643ea8Slogwang 				if (rte_spinlock_trylock(&global_flag_stru_p->lock) == 1)     {
442a9643ea8Slogwang 					global_flag_stru_p->port_packets[2]++;
443a9643ea8Slogwang 					rte_spinlock_unlock(&global_flag_stru_p->lock);
444a9643ea8Slogwang 				 }
4454418919fSjohnjiang 				ipv4_hdr = (struct rte_ipv4_hdr *)((char *)(eth_hdr + 1) + offset);
446a9643ea8Slogwang 				if (ipv4_hdr->dst_addr == bond_ip) {
4474418919fSjohnjiang 					rte_ether_addr_copy(&eth_hdr->s_addr, &eth_hdr->d_addr);
4484418919fSjohnjiang 					rte_ether_addr_copy(&bond_mac_addr, &eth_hdr->s_addr);
449a9643ea8Slogwang 					ipv4_hdr->dst_addr = ipv4_hdr->src_addr;
450a9643ea8Slogwang 					ipv4_hdr->src_addr = bond_ip;
451a9643ea8Slogwang 					rte_eth_tx_burst(BOND_PORT, 0, &pkts[i], 1);
452a9643ea8Slogwang 				}
453a9643ea8Slogwang 
454a9643ea8Slogwang 			}
455a9643ea8Slogwang 
456a9643ea8Slogwang 			/* Free processed packets */
457a9643ea8Slogwang 			if (is_free == 0)
458a9643ea8Slogwang 				rte_pktmbuf_free(pkts[i]);
459a9643ea8Slogwang 		}
460a9643ea8Slogwang 		rte_spinlock_trylock(&global_flag_stru_p->lock);
461a9643ea8Slogwang 	}
462a9643ea8Slogwang 	rte_spinlock_unlock(&global_flag_stru_p->lock);
463a9643ea8Slogwang 	printf("BYE lcore_main\n");
464a9643ea8Slogwang 	return 0;
465a9643ea8Slogwang }
466a9643ea8Slogwang 
467a9643ea8Slogwang struct cmd_obj_send_result {
468a9643ea8Slogwang 	cmdline_fixed_string_t action;
469a9643ea8Slogwang 	cmdline_ipaddr_t ip;
470a9643ea8Slogwang };
get_string(struct cmd_obj_send_result * res,char * buf,uint8_t size)471a9643ea8Slogwang static inline void get_string(struct cmd_obj_send_result *res, char *buf, uint8_t size)
472a9643ea8Slogwang {
473a9643ea8Slogwang 	snprintf(buf, size, NIPQUAD_FMT,
474a9643ea8Slogwang 		((unsigned)((unsigned char *)&(res->ip.addr.ipv4))[0]),
475a9643ea8Slogwang 		((unsigned)((unsigned char *)&(res->ip.addr.ipv4))[1]),
476a9643ea8Slogwang 		((unsigned)((unsigned char *)&(res->ip.addr.ipv4))[2]),
477a9643ea8Slogwang 		((unsigned)((unsigned char *)&(res->ip.addr.ipv4))[3])
478a9643ea8Slogwang 		);
479a9643ea8Slogwang }
cmd_obj_send_parsed(void * parsed_result,__rte_unused struct cmdline * cl,__rte_unused void * data)480a9643ea8Slogwang static void cmd_obj_send_parsed(void *parsed_result,
481*2d9fd380Sjfb8856606 		__rte_unused struct cmdline *cl,
482*2d9fd380Sjfb8856606 			       __rte_unused void *data)
483a9643ea8Slogwang {
484a9643ea8Slogwang 
485a9643ea8Slogwang 	struct cmd_obj_send_result *res = parsed_result;
486a9643ea8Slogwang 	char ip_str[INET6_ADDRSTRLEN];
487a9643ea8Slogwang 
4884418919fSjohnjiang 	struct rte_ether_addr bond_mac_addr;
489a9643ea8Slogwang 	struct rte_mbuf *created_pkt;
4904418919fSjohnjiang 	struct rte_ether_hdr *eth_hdr;
4914418919fSjohnjiang 	struct rte_arp_hdr *arp_hdr;
492a9643ea8Slogwang 
493a9643ea8Slogwang 	uint32_t bond_ip;
494a9643ea8Slogwang 	size_t pkt_size;
4954418919fSjohnjiang 	int ret;
496a9643ea8Slogwang 
497a9643ea8Slogwang 	if (res->ip.family == AF_INET)
498a9643ea8Slogwang 		get_string(res, ip_str, INET_ADDRSTRLEN);
499a9643ea8Slogwang 	else
500a9643ea8Slogwang 		cmdline_printf(cl, "Wrong IP format. Only IPv4 is supported\n");
501a9643ea8Slogwang 
502a9643ea8Slogwang 	bond_ip = BOND_IP_1 | (BOND_IP_2 << 8) |
503a9643ea8Slogwang 				(BOND_IP_3 << 16) | (BOND_IP_4 << 24);
504a9643ea8Slogwang 
5054418919fSjohnjiang 	ret = rte_eth_macaddr_get(BOND_PORT, &bond_mac_addr);
5064418919fSjohnjiang 	if (ret != 0) {
5074418919fSjohnjiang 		cmdline_printf(cl,
5084418919fSjohnjiang 			       "Failed to get bond (port %u) MAC address: %s\n",
5094418919fSjohnjiang 			       BOND_PORT, strerror(-ret));
5104418919fSjohnjiang 	}
5114418919fSjohnjiang 
512a9643ea8Slogwang 	created_pkt = rte_pktmbuf_alloc(mbuf_pool);
5132bfe3f2eSlogwang 	if (created_pkt == NULL) {
5142bfe3f2eSlogwang 		cmdline_printf(cl, "Failed to allocate mbuf\n");
5152bfe3f2eSlogwang 		return;
5162bfe3f2eSlogwang 	}
5172bfe3f2eSlogwang 
5184418919fSjohnjiang 	pkt_size = sizeof(struct rte_ether_hdr) + sizeof(struct rte_arp_hdr);
519a9643ea8Slogwang 	created_pkt->data_len = pkt_size;
520a9643ea8Slogwang 	created_pkt->pkt_len = pkt_size;
521a9643ea8Slogwang 
5224418919fSjohnjiang 	eth_hdr = rte_pktmbuf_mtod(created_pkt, struct rte_ether_hdr *);
5234418919fSjohnjiang 	rte_ether_addr_copy(&bond_mac_addr, &eth_hdr->s_addr);
5244418919fSjohnjiang 	memset(&eth_hdr->d_addr, 0xFF, RTE_ETHER_ADDR_LEN);
5254418919fSjohnjiang 	eth_hdr->ether_type = rte_cpu_to_be_16(RTE_ETHER_TYPE_ARP);
526a9643ea8Slogwang 
5274418919fSjohnjiang 	arp_hdr = (struct rte_arp_hdr *)(
5284418919fSjohnjiang 		(char *)eth_hdr + sizeof(struct rte_ether_hdr));
5294418919fSjohnjiang 	arp_hdr->arp_hardware = rte_cpu_to_be_16(RTE_ARP_HRD_ETHER);
5304418919fSjohnjiang 	arp_hdr->arp_protocol = rte_cpu_to_be_16(RTE_ETHER_TYPE_IPV4);
5314418919fSjohnjiang 	arp_hdr->arp_hlen = RTE_ETHER_ADDR_LEN;
5324418919fSjohnjiang 	arp_hdr->arp_plen = sizeof(uint32_t);
5334418919fSjohnjiang 	arp_hdr->arp_opcode = rte_cpu_to_be_16(RTE_ARP_OP_REQUEST);
534a9643ea8Slogwang 
5354418919fSjohnjiang 	rte_ether_addr_copy(&bond_mac_addr, &arp_hdr->arp_data.arp_sha);
536a9643ea8Slogwang 	arp_hdr->arp_data.arp_sip = bond_ip;
5374418919fSjohnjiang 	memset(&arp_hdr->arp_data.arp_tha, 0, RTE_ETHER_ADDR_LEN);
538a9643ea8Slogwang 	arp_hdr->arp_data.arp_tip =
539a9643ea8Slogwang 			  ((unsigned char *)&res->ip.addr.ipv4)[0]        |
540a9643ea8Slogwang 			 (((unsigned char *)&res->ip.addr.ipv4)[1] << 8)  |
541a9643ea8Slogwang 			 (((unsigned char *)&res->ip.addr.ipv4)[2] << 16) |
542a9643ea8Slogwang 			 (((unsigned char *)&res->ip.addr.ipv4)[3] << 24);
543a9643ea8Slogwang 	rte_eth_tx_burst(BOND_PORT, 0, &created_pkt, 1);
544a9643ea8Slogwang 
545a9643ea8Slogwang 	rte_delay_ms(100);
546a9643ea8Slogwang 	cmdline_printf(cl, "\n");
547a9643ea8Slogwang }
548a9643ea8Slogwang 
549a9643ea8Slogwang cmdline_parse_token_string_t cmd_obj_action_send =
550a9643ea8Slogwang 	TOKEN_STRING_INITIALIZER(struct cmd_obj_send_result, action, "send");
551a9643ea8Slogwang cmdline_parse_token_ipaddr_t cmd_obj_ip =
552a9643ea8Slogwang 	TOKEN_IPV4_INITIALIZER(struct cmd_obj_send_result, ip);
553a9643ea8Slogwang 
554a9643ea8Slogwang cmdline_parse_inst_t cmd_obj_send = {
555a9643ea8Slogwang 	.f = cmd_obj_send_parsed,  /* function to call */
556a9643ea8Slogwang 	.data = NULL,      /* 2nd arg of func */
557a9643ea8Slogwang 	.help_str = "send client_ip",
558a9643ea8Slogwang 	.tokens = {        /* token list, NULL terminated */
559a9643ea8Slogwang 		(void *)&cmd_obj_action_send,
560a9643ea8Slogwang 		(void *)&cmd_obj_ip,
561a9643ea8Slogwang 		NULL,
562a9643ea8Slogwang 	},
563a9643ea8Slogwang };
564a9643ea8Slogwang 
565a9643ea8Slogwang struct cmd_start_result {
566a9643ea8Slogwang 	cmdline_fixed_string_t start;
567a9643ea8Slogwang };
568a9643ea8Slogwang 
cmd_start_parsed(__rte_unused void * parsed_result,struct cmdline * cl,__rte_unused void * data)569*2d9fd380Sjfb8856606 static void cmd_start_parsed(__rte_unused void *parsed_result,
570a9643ea8Slogwang 			       struct cmdline *cl,
571*2d9fd380Sjfb8856606 			       __rte_unused void *data)
572a9643ea8Slogwang {
573*2d9fd380Sjfb8856606 	int worker_core_id = rte_lcore_id();
574a9643ea8Slogwang 
575a9643ea8Slogwang 	rte_spinlock_trylock(&global_flag_stru_p->lock);
576a9643ea8Slogwang 	if (global_flag_stru_p->LcoreMainIsRunning == 0) {
5774418919fSjohnjiang 		if (rte_eal_get_lcore_state(global_flag_stru_p->LcoreMainCore)
5784418919fSjohnjiang 		    != WAIT) {
579a9643ea8Slogwang 			rte_spinlock_unlock(&global_flag_stru_p->lock);
580a9643ea8Slogwang 			return;
581a9643ea8Slogwang 		}
582a9643ea8Slogwang 		rte_spinlock_unlock(&global_flag_stru_p->lock);
583a9643ea8Slogwang 	} else {
584a9643ea8Slogwang 		cmdline_printf(cl, "lcore_main already running on core:%d\n",
585a9643ea8Slogwang 				global_flag_stru_p->LcoreMainCore);
586a9643ea8Slogwang 		rte_spinlock_unlock(&global_flag_stru_p->lock);
587a9643ea8Slogwang 		return;
588a9643ea8Slogwang 	}
589a9643ea8Slogwang 
590*2d9fd380Sjfb8856606 	/* start lcore main on core != main_core - ARP response thread */
591*2d9fd380Sjfb8856606 	worker_core_id = rte_get_next_lcore(rte_lcore_id(), 1, 0);
592*2d9fd380Sjfb8856606 	if ((worker_core_id >= RTE_MAX_LCORE) || (worker_core_id == 0))
593a9643ea8Slogwang 		return;
594a9643ea8Slogwang 
595a9643ea8Slogwang 	rte_spinlock_trylock(&global_flag_stru_p->lock);
596a9643ea8Slogwang 	global_flag_stru_p->LcoreMainIsRunning = 1;
597a9643ea8Slogwang 	rte_spinlock_unlock(&global_flag_stru_p->lock);
598a9643ea8Slogwang 	cmdline_printf(cl,
599a9643ea8Slogwang 			"Starting lcore_main on core %d:%d "
600a9643ea8Slogwang 			"Our IP:%d.%d.%d.%d\n",
601*2d9fd380Sjfb8856606 			worker_core_id,
602*2d9fd380Sjfb8856606 			rte_eal_remote_launch(lcore_main, NULL, worker_core_id),
603a9643ea8Slogwang 			BOND_IP_1,
604a9643ea8Slogwang 			BOND_IP_2,
605a9643ea8Slogwang 			BOND_IP_3,
606a9643ea8Slogwang 			BOND_IP_4
607a9643ea8Slogwang 		);
608a9643ea8Slogwang }
609a9643ea8Slogwang 
610a9643ea8Slogwang cmdline_parse_token_string_t cmd_start_start =
611a9643ea8Slogwang 	TOKEN_STRING_INITIALIZER(struct cmd_start_result, start, "start");
612a9643ea8Slogwang 
613a9643ea8Slogwang cmdline_parse_inst_t cmd_start = {
614a9643ea8Slogwang 	.f = cmd_start_parsed,  /* function to call */
615a9643ea8Slogwang 	.data = NULL,      /* 2nd arg of func */
616a9643ea8Slogwang 	.help_str = "starts listening if not started at startup",
617a9643ea8Slogwang 	.tokens = {        /* token list, NULL terminated */
618a9643ea8Slogwang 		(void *)&cmd_start_start,
619a9643ea8Slogwang 		NULL,
620a9643ea8Slogwang 	},
621a9643ea8Slogwang };
622a9643ea8Slogwang 
623a9643ea8Slogwang struct cmd_help_result {
624a9643ea8Slogwang 	cmdline_fixed_string_t help;
625a9643ea8Slogwang };
626a9643ea8Slogwang 
cmd_help_parsed(__rte_unused void * parsed_result,struct cmdline * cl,__rte_unused void * data)627*2d9fd380Sjfb8856606 static void cmd_help_parsed(__rte_unused void *parsed_result,
628a9643ea8Slogwang 			    struct cmdline *cl,
629*2d9fd380Sjfb8856606 			    __rte_unused void *data)
630a9643ea8Slogwang {
631a9643ea8Slogwang 	cmdline_printf(cl,
632a9643ea8Slogwang 			"ALB - link bonding mode 6 example\n"
6332bfe3f2eSlogwang 			"send IP	- sends one ARPrequest through bonding for IP.\n"
634a9643ea8Slogwang 			"start		- starts listening ARPs.\n"
635a9643ea8Slogwang 			"stop		- stops lcore_main.\n"
636a9643ea8Slogwang 			"show		- shows some bond info: ex. active slaves etc.\n"
637a9643ea8Slogwang 			"help		- prints help.\n"
638a9643ea8Slogwang 			"quit		- terminate all threads and quit.\n"
639a9643ea8Slogwang 		       );
640a9643ea8Slogwang }
641a9643ea8Slogwang 
642a9643ea8Slogwang cmdline_parse_token_string_t cmd_help_help =
643a9643ea8Slogwang 	TOKEN_STRING_INITIALIZER(struct cmd_help_result, help, "help");
644a9643ea8Slogwang 
645a9643ea8Slogwang cmdline_parse_inst_t cmd_help = {
646a9643ea8Slogwang 	.f = cmd_help_parsed,  /* function to call */
647a9643ea8Slogwang 	.data = NULL,      /* 2nd arg of func */
648a9643ea8Slogwang 	.help_str = "show help",
649a9643ea8Slogwang 	.tokens = {        /* token list, NULL terminated */
650a9643ea8Slogwang 		(void *)&cmd_help_help,
651a9643ea8Slogwang 		NULL,
652a9643ea8Slogwang 	},
653a9643ea8Slogwang };
654a9643ea8Slogwang 
655a9643ea8Slogwang struct cmd_stop_result {
656a9643ea8Slogwang 	cmdline_fixed_string_t stop;
657a9643ea8Slogwang };
658a9643ea8Slogwang 
cmd_stop_parsed(__rte_unused void * parsed_result,struct cmdline * cl,__rte_unused void * data)659*2d9fd380Sjfb8856606 static void cmd_stop_parsed(__rte_unused void *parsed_result,
660a9643ea8Slogwang 			    struct cmdline *cl,
661*2d9fd380Sjfb8856606 			    __rte_unused void *data)
662a9643ea8Slogwang {
663a9643ea8Slogwang 	rte_spinlock_trylock(&global_flag_stru_p->lock);
664a9643ea8Slogwang 	if (global_flag_stru_p->LcoreMainIsRunning == 0)	{
665a9643ea8Slogwang 		cmdline_printf(cl,
666a9643ea8Slogwang 					"lcore_main not running on core:%d\n",
667a9643ea8Slogwang 					global_flag_stru_p->LcoreMainCore);
668a9643ea8Slogwang 		rte_spinlock_unlock(&global_flag_stru_p->lock);
669a9643ea8Slogwang 		return;
670a9643ea8Slogwang 	}
671a9643ea8Slogwang 	global_flag_stru_p->LcoreMainIsRunning = 0;
672a9643ea8Slogwang 	if (rte_eal_wait_lcore(global_flag_stru_p->LcoreMainCore) < 0)
673a9643ea8Slogwang 		cmdline_printf(cl,
674a9643ea8Slogwang 				"error: lcore_main can not stop on core:%d\n",
675a9643ea8Slogwang 				global_flag_stru_p->LcoreMainCore);
676a9643ea8Slogwang 	else
677a9643ea8Slogwang 		cmdline_printf(cl,
678a9643ea8Slogwang 				"lcore_main stopped on core:%d\n",
679a9643ea8Slogwang 				global_flag_stru_p->LcoreMainCore);
680a9643ea8Slogwang 	rte_spinlock_unlock(&global_flag_stru_p->lock);
681a9643ea8Slogwang }
682a9643ea8Slogwang 
683a9643ea8Slogwang cmdline_parse_token_string_t cmd_stop_stop =
684a9643ea8Slogwang 	TOKEN_STRING_INITIALIZER(struct cmd_stop_result, stop, "stop");
685a9643ea8Slogwang 
686a9643ea8Slogwang cmdline_parse_inst_t cmd_stop = {
687a9643ea8Slogwang 	.f = cmd_stop_parsed,  /* function to call */
688a9643ea8Slogwang 	.data = NULL,      /* 2nd arg of func */
689a9643ea8Slogwang 	.help_str = "this command do not handle any arguments",
690a9643ea8Slogwang 	.tokens = {        /* token list, NULL terminated */
691a9643ea8Slogwang 		(void *)&cmd_stop_stop,
692a9643ea8Slogwang 		NULL,
693a9643ea8Slogwang 	},
694a9643ea8Slogwang };
695a9643ea8Slogwang 
696a9643ea8Slogwang struct cmd_quit_result {
697a9643ea8Slogwang 	cmdline_fixed_string_t quit;
698a9643ea8Slogwang };
699a9643ea8Slogwang 
cmd_quit_parsed(__rte_unused void * parsed_result,struct cmdline * cl,__rte_unused void * data)700*2d9fd380Sjfb8856606 static void cmd_quit_parsed(__rte_unused void *parsed_result,
701a9643ea8Slogwang 			    struct cmdline *cl,
702*2d9fd380Sjfb8856606 			    __rte_unused void *data)
703a9643ea8Slogwang {
704a9643ea8Slogwang 	rte_spinlock_trylock(&global_flag_stru_p->lock);
705a9643ea8Slogwang 	if (global_flag_stru_p->LcoreMainIsRunning == 0)	{
706a9643ea8Slogwang 		cmdline_printf(cl,
707a9643ea8Slogwang 					"lcore_main not running on core:%d\n",
708a9643ea8Slogwang 					global_flag_stru_p->LcoreMainCore);
709a9643ea8Slogwang 		rte_spinlock_unlock(&global_flag_stru_p->lock);
710a9643ea8Slogwang 		cmdline_quit(cl);
711a9643ea8Slogwang 		return;
712a9643ea8Slogwang 	}
713a9643ea8Slogwang 	global_flag_stru_p->LcoreMainIsRunning = 0;
714a9643ea8Slogwang 	if (rte_eal_wait_lcore(global_flag_stru_p->LcoreMainCore) < 0)
715a9643ea8Slogwang 		cmdline_printf(cl,
716a9643ea8Slogwang 				"error: lcore_main can not stop on core:%d\n",
717a9643ea8Slogwang 				global_flag_stru_p->LcoreMainCore);
718a9643ea8Slogwang 	else
719a9643ea8Slogwang 		cmdline_printf(cl,
720a9643ea8Slogwang 				"lcore_main stopped on core:%d\n",
721a9643ea8Slogwang 				global_flag_stru_p->LcoreMainCore);
722a9643ea8Slogwang 	rte_spinlock_unlock(&global_flag_stru_p->lock);
723a9643ea8Slogwang 	cmdline_quit(cl);
724a9643ea8Slogwang }
725a9643ea8Slogwang 
726a9643ea8Slogwang cmdline_parse_token_string_t cmd_quit_quit =
727a9643ea8Slogwang 	TOKEN_STRING_INITIALIZER(struct cmd_quit_result, quit, "quit");
728a9643ea8Slogwang 
729a9643ea8Slogwang cmdline_parse_inst_t cmd_quit = {
730a9643ea8Slogwang 	.f = cmd_quit_parsed,  /* function to call */
731a9643ea8Slogwang 	.data = NULL,      /* 2nd arg of func */
732a9643ea8Slogwang 	.help_str = "this command do not handle any arguments",
733a9643ea8Slogwang 	.tokens = {        /* token list, NULL terminated */
734a9643ea8Slogwang 		(void *)&cmd_quit_quit,
735a9643ea8Slogwang 		NULL,
736a9643ea8Slogwang 	},
737a9643ea8Slogwang };
738a9643ea8Slogwang 
739a9643ea8Slogwang struct cmd_show_result {
740a9643ea8Slogwang 	cmdline_fixed_string_t show;
741a9643ea8Slogwang };
742a9643ea8Slogwang 
cmd_show_parsed(__rte_unused void * parsed_result,struct cmdline * cl,__rte_unused void * data)743*2d9fd380Sjfb8856606 static void cmd_show_parsed(__rte_unused void *parsed_result,
744a9643ea8Slogwang 			    struct cmdline *cl,
745*2d9fd380Sjfb8856606 			    __rte_unused void *data)
746a9643ea8Slogwang {
7472bfe3f2eSlogwang 	uint16_t slaves[16] = {0};
748a9643ea8Slogwang 	uint8_t len = 16;
7494418919fSjohnjiang 	struct rte_ether_addr addr;
7504418919fSjohnjiang 	uint16_t i;
7514418919fSjohnjiang 	int ret;
752a9643ea8Slogwang 
7534418919fSjohnjiang 	for (i = 0; i < slaves_count; i++) {
7544418919fSjohnjiang 		ret = rte_eth_macaddr_get(i, &addr);
7554418919fSjohnjiang 		if (ret != 0) {
7564418919fSjohnjiang 			cmdline_printf(cl,
7574418919fSjohnjiang 				"Failed to get port %u MAC address: %s\n",
7584418919fSjohnjiang 				i, strerror(-ret));
7594418919fSjohnjiang 			continue;
7604418919fSjohnjiang 		}
7614418919fSjohnjiang 
762a9643ea8Slogwang 		PRINT_MAC(addr);
763a9643ea8Slogwang 		printf("\n");
764a9643ea8Slogwang 	}
765a9643ea8Slogwang 
766a9643ea8Slogwang 	rte_spinlock_trylock(&global_flag_stru_p->lock);
767a9643ea8Slogwang 	cmdline_printf(cl,
768a9643ea8Slogwang 			"Active_slaves:%d "
769a9643ea8Slogwang 			"packets received:Tot:%d Arp:%d IPv4:%d\n",
770a9643ea8Slogwang 			rte_eth_bond_active_slaves_get(BOND_PORT, slaves, len),
771a9643ea8Slogwang 			global_flag_stru_p->port_packets[0],
772a9643ea8Slogwang 			global_flag_stru_p->port_packets[1],
773a9643ea8Slogwang 			global_flag_stru_p->port_packets[2]);
774a9643ea8Slogwang 	rte_spinlock_unlock(&global_flag_stru_p->lock);
775a9643ea8Slogwang }
776a9643ea8Slogwang 
777a9643ea8Slogwang cmdline_parse_token_string_t cmd_show_show =
778a9643ea8Slogwang 	TOKEN_STRING_INITIALIZER(struct cmd_show_result, show, "show");
779a9643ea8Slogwang 
780a9643ea8Slogwang cmdline_parse_inst_t cmd_show = {
781a9643ea8Slogwang 	.f = cmd_show_parsed,  /* function to call */
782a9643ea8Slogwang 	.data = NULL,      /* 2nd arg of func */
783a9643ea8Slogwang 	.help_str = "this command do not handle any arguments",
784a9643ea8Slogwang 	.tokens = {        /* token list, NULL terminated */
785a9643ea8Slogwang 		(void *)&cmd_show_show,
786a9643ea8Slogwang 		NULL,
787a9643ea8Slogwang 	},
788a9643ea8Slogwang };
789a9643ea8Slogwang 
790a9643ea8Slogwang /****** CONTEXT (list of instruction) */
791a9643ea8Slogwang 
792a9643ea8Slogwang cmdline_parse_ctx_t main_ctx[] = {
793a9643ea8Slogwang 	(cmdline_parse_inst_t *)&cmd_start,
794a9643ea8Slogwang 	(cmdline_parse_inst_t *)&cmd_obj_send,
795a9643ea8Slogwang 	(cmdline_parse_inst_t *)&cmd_stop,
796a9643ea8Slogwang 	(cmdline_parse_inst_t *)&cmd_show,
797a9643ea8Slogwang 	(cmdline_parse_inst_t *)&cmd_quit,
798a9643ea8Slogwang 	(cmdline_parse_inst_t *)&cmd_help,
799a9643ea8Slogwang 	NULL,
800a9643ea8Slogwang };
801a9643ea8Slogwang 
802*2d9fd380Sjfb8856606 /* prompt function, called from main on MAIN lcore */
prompt(__rte_unused void * arg1)803*2d9fd380Sjfb8856606 static void prompt(__rte_unused void *arg1)
804a9643ea8Slogwang {
805a9643ea8Slogwang 	struct cmdline *cl;
806a9643ea8Slogwang 
807a9643ea8Slogwang 	cl = cmdline_stdin_new(main_ctx, "bond6>");
808a9643ea8Slogwang 	if (cl != NULL) {
809a9643ea8Slogwang 		cmdline_interact(cl);
810a9643ea8Slogwang 		cmdline_stdin_exit(cl);
811a9643ea8Slogwang 	}
812a9643ea8Slogwang }
813a9643ea8Slogwang 
814a9643ea8Slogwang /* Main function, does initialisation and calls the per-lcore functions */
815a9643ea8Slogwang int
main(int argc,char * argv[])816a9643ea8Slogwang main(int argc, char *argv[])
817a9643ea8Slogwang {
818*2d9fd380Sjfb8856606 	int ret, worker_core_id;
819d30ea906Sjfb8856606 	uint16_t nb_ports, i;
820a9643ea8Slogwang 
821a9643ea8Slogwang 	/* init EAL */
822a9643ea8Slogwang 	ret = rte_eal_init(argc, argv);
823d30ea906Sjfb8856606 	rte_devargs_dump(stdout);
824a9643ea8Slogwang 	if (ret < 0)
825a9643ea8Slogwang 		rte_exit(EXIT_FAILURE, "Error with EAL initialization\n");
826a9643ea8Slogwang 	argc -= ret;
827a9643ea8Slogwang 	argv += ret;
828a9643ea8Slogwang 
829d30ea906Sjfb8856606 	nb_ports = rte_eth_dev_count_avail();
830a9643ea8Slogwang 	if (nb_ports == 0)
831a9643ea8Slogwang 		rte_exit(EXIT_FAILURE, "Give at least one port\n");
832a9643ea8Slogwang 	else if (nb_ports > MAX_PORTS)
833a9643ea8Slogwang 		rte_exit(EXIT_FAILURE, "You can have max 4 ports\n");
834a9643ea8Slogwang 
835a9643ea8Slogwang 	mbuf_pool = rte_pktmbuf_pool_create("MBUF_POOL", NB_MBUF, 32,
836a9643ea8Slogwang 		0, RTE_MBUF_DEFAULT_BUF_SIZE, rte_socket_id());
837a9643ea8Slogwang 	if (mbuf_pool == NULL)
838a9643ea8Slogwang 		rte_exit(EXIT_FAILURE, "Cannot create mbuf pool\n");
839a9643ea8Slogwang 
840a9643ea8Slogwang 	/* initialize all ports */
841a9643ea8Slogwang 	slaves_count = nb_ports;
842d30ea906Sjfb8856606 	RTE_ETH_FOREACH_DEV(i) {
843a9643ea8Slogwang 		slave_port_init(i, mbuf_pool);
844a9643ea8Slogwang 		slaves[i] = i;
845a9643ea8Slogwang 	}
846a9643ea8Slogwang 
847a9643ea8Slogwang 	bond_port_init(mbuf_pool);
848a9643ea8Slogwang 
849a9643ea8Slogwang 	rte_spinlock_init(&global_flag_stru_p->lock);
850a9643ea8Slogwang 
851a9643ea8Slogwang 	/* check state of lcores */
852*2d9fd380Sjfb8856606 	RTE_LCORE_FOREACH_WORKER(worker_core_id) {
853*2d9fd380Sjfb8856606 		if (rte_eal_get_lcore_state(worker_core_id) != WAIT)
854a9643ea8Slogwang 			return -EBUSY;
855a9643ea8Slogwang 	}
8564418919fSjohnjiang 
857*2d9fd380Sjfb8856606 	/* start lcore main on core != main_core - ARP response thread */
858*2d9fd380Sjfb8856606 	worker_core_id = rte_get_next_lcore(rte_lcore_id(), 1, 0);
859*2d9fd380Sjfb8856606 	if ((worker_core_id >= RTE_MAX_LCORE) || (worker_core_id == 0))
860a9643ea8Slogwang 		return -EPERM;
861a9643ea8Slogwang 
862a9643ea8Slogwang 	global_flag_stru_p->LcoreMainIsRunning = 1;
863*2d9fd380Sjfb8856606 	global_flag_stru_p->LcoreMainCore = worker_core_id;
864a9643ea8Slogwang 	printf("Starting lcore_main on core %d:%d Our IP:%d.%d.%d.%d\n",
865*2d9fd380Sjfb8856606 			worker_core_id,
866a9643ea8Slogwang 			rte_eal_remote_launch((lcore_function_t *)lcore_main,
867a9643ea8Slogwang 					NULL,
868*2d9fd380Sjfb8856606 					worker_core_id),
869a9643ea8Slogwang 			BOND_IP_1,
870a9643ea8Slogwang 			BOND_IP_2,
871a9643ea8Slogwang 			BOND_IP_3,
872a9643ea8Slogwang 			BOND_IP_4
873a9643ea8Slogwang 		);
874a9643ea8Slogwang 
875a9643ea8Slogwang 	/* Start prompt for user interact */
876a9643ea8Slogwang 	prompt(NULL);
877a9643ea8Slogwang 
878a9643ea8Slogwang 	rte_delay_ms(100);
879a9643ea8Slogwang 	return 0;
880a9643ea8Slogwang }
881