xref: /dpdk/examples/l2fwd/main.c (revision 7e06c0de1952d3109a5b0c4779d7e7d8059c9d78)
13998e2a0SBruce Richardson /* SPDX-License-Identifier: BSD-3-Clause
23998e2a0SBruce Richardson  * Copyright(c) 2010-2016 Intel Corporation
3af75078fSIntel  */
4af75078fSIntel 
5af75078fSIntel #include <stdio.h>
6af75078fSIntel #include <stdlib.h>
7af75078fSIntel #include <string.h>
8af75078fSIntel #include <stdint.h>
9af75078fSIntel #include <inttypes.h>
10af75078fSIntel #include <sys/types.h>
11af75078fSIntel #include <sys/queue.h>
12af75078fSIntel #include <setjmp.h>
13af75078fSIntel #include <stdarg.h>
14af75078fSIntel #include <ctype.h>
15af75078fSIntel #include <errno.h>
16af75078fSIntel #include <getopt.h>
1788908b61SZhihong Wang #include <signal.h>
1888908b61SZhihong Wang #include <stdbool.h>
19af75078fSIntel 
20af75078fSIntel #include <rte_common.h>
21af75078fSIntel #include <rte_log.h>
22e2366e74STomasz Kulasek #include <rte_malloc.h>
23af75078fSIntel #include <rte_memory.h>
24af75078fSIntel #include <rte_memcpy.h>
25af75078fSIntel #include <rte_eal.h>
26af75078fSIntel #include <rte_launch.h>
27af75078fSIntel #include <rte_cycles.h>
28af75078fSIntel #include <rte_prefetch.h>
29af75078fSIntel #include <rte_lcore.h>
30af75078fSIntel #include <rte_per_lcore.h>
31af75078fSIntel #include <rte_branch_prediction.h>
32af75078fSIntel #include <rte_interrupts.h>
33af75078fSIntel #include <rte_random.h>
34af75078fSIntel #include <rte_debug.h>
35af75078fSIntel #include <rte_ether.h>
36af75078fSIntel #include <rte_ethdev.h>
37af75078fSIntel #include <rte_mempool.h>
38af75078fSIntel #include <rte_mbuf.h>
39fa19eb20SVamsi Attunuru #include <rte_string_fns.h>
40af75078fSIntel 
4188908b61SZhihong Wang static volatile bool force_quit;
4288908b61SZhihong Wang 
43cf435a07SMaxime Coquelin /* MAC updating enabled by default */
44cf435a07SMaxime Coquelin static int mac_updating = 1;
45cf435a07SMaxime Coquelin 
46b54403fdSSarosh Arif /* Ports set in promiscuous mode off by default. */
47b54403fdSSarosh Arif static int promiscuous_on;
48b54403fdSSarosh Arif 
49af75078fSIntel #define RTE_LOGTYPE_L2FWD RTE_LOGTYPE_USER1
50af75078fSIntel 
51af75078fSIntel #define MAX_PKT_BURST 32
525c95261dSIntel #define BURST_TX_DRAIN_US 100 /* TX drain every ~100us */
53b3e4d4e3SJerin Jacob #define MEMPOOL_CACHE_SIZE 256
54af75078fSIntel 
55af75078fSIntel /*
56af75078fSIntel  * Configurable number of RX/TX ring descriptors
57af75078fSIntel  */
584ed89049SDavid Marchand #define RX_DESC_DEFAULT 1024
594ed89049SDavid Marchand #define TX_DESC_DEFAULT 1024
604ed89049SDavid Marchand static uint16_t nb_rxd = RX_DESC_DEFAULT;
614ed89049SDavid Marchand static uint16_t nb_txd = TX_DESC_DEFAULT;
62af75078fSIntel 
63af75078fSIntel /* ethernet addresses of ports */
646d13ea8eSOlivier Matz static struct rte_ether_addr l2fwd_ports_eth_addr[RTE_MAX_ETHPORTS];
65af75078fSIntel 
66af75078fSIntel /* mask of enabled ports */
67af75078fSIntel static uint32_t l2fwd_enabled_port_mask = 0;
68af75078fSIntel 
69af75078fSIntel /* list of enabled ports */
701c17baf4SIntel static uint32_t l2fwd_dst_ports[RTE_MAX_ETHPORTS];
71af75078fSIntel 
72*7e06c0deSTyler Retzlaff struct __rte_cache_aligned port_pair_params {
73fa19eb20SVamsi Attunuru #define NUM_PORTS	2
74fa19eb20SVamsi Attunuru 	uint16_t port[NUM_PORTS];
75*7e06c0deSTyler Retzlaff };
76fa19eb20SVamsi Attunuru 
77fa19eb20SVamsi Attunuru static struct port_pair_params port_pair_params_array[RTE_MAX_ETHPORTS / 2];
78fa19eb20SVamsi Attunuru static struct port_pair_params *port_pair_params;
79fa19eb20SVamsi Attunuru static uint16_t nb_port_pair_params;
80fa19eb20SVamsi Attunuru 
81af75078fSIntel static unsigned int l2fwd_rx_queue_per_lcore = 1;
82af75078fSIntel 
83af75078fSIntel #define MAX_RX_QUEUE_PER_LCORE 16
84af75078fSIntel #define MAX_TX_QUEUE_PER_PORT 16
859a212dc0SConor Fogarty /* List of queues to be polled for a given lcore. 8< */
86*7e06c0deSTyler Retzlaff struct __rte_cache_aligned lcore_queue_conf {
8716ac9cf0SIntel 	unsigned n_rx_port;
8816ac9cf0SIntel 	unsigned rx_port_list[MAX_RX_QUEUE_PER_LCORE];
89*7e06c0deSTyler Retzlaff };
90af75078fSIntel struct lcore_queue_conf lcore_queue_conf[RTE_MAX_LCORE];
919a212dc0SConor Fogarty /* >8 End of list of queues to be polled for a given lcore. */
92af75078fSIntel 
93e2366e74STomasz Kulasek static struct rte_eth_dev_tx_buffer *tx_buffer[RTE_MAX_ETHPORTS];
94e2366e74STomasz Kulasek 
9575d7f266SShahaf Shuler static struct rte_eth_conf port_conf = {
96af75078fSIntel 	.txmode = {
97295968d1SFerruh Yigit 		.mq_mode = RTE_ETH_MQ_TX_NONE,
98af75078fSIntel 	},
99af75078fSIntel };
100af75078fSIntel 
101af75078fSIntel struct rte_mempool * l2fwd_pktmbuf_pool = NULL;
102af75078fSIntel 
103af75078fSIntel /* Per-port statistics struct */
104*7e06c0deSTyler Retzlaff struct __rte_cache_aligned l2fwd_port_statistics {
105af75078fSIntel 	uint64_t tx;
106af75078fSIntel 	uint64_t rx;
107af75078fSIntel 	uint64_t dropped;
108*7e06c0deSTyler Retzlaff };
1091c17baf4SIntel struct l2fwd_port_statistics port_statistics[RTE_MAX_ETHPORTS];
110af75078fSIntel 
111af75078fSIntel #define MAX_TIMER_PERIOD 86400 /* 1 day max */
1122412742cSJerin Jacob /* A tsc-based timer responsible for triggering statistics printout */
1132412742cSJerin Jacob static uint64_t timer_period = 10; /* default period is 10 seconds */
114af75078fSIntel 
115af75078fSIntel /* Print out statistics on packets dropped */
116af75078fSIntel static void
print_stats(void)117af75078fSIntel print_stats(void)
118af75078fSIntel {
119af75078fSIntel 	uint64_t total_packets_dropped, total_packets_tx, total_packets_rx;
120af75078fSIntel 	unsigned portid;
121af75078fSIntel 
122af75078fSIntel 	total_packets_dropped = 0;
123af75078fSIntel 	total_packets_tx = 0;
124af75078fSIntel 	total_packets_rx = 0;
125af75078fSIntel 
126af75078fSIntel 	const char clr[] = { 27, '[', '2', 'J', '\0' };
127af75078fSIntel 	const char topLeft[] = { 27, '[', '1', ';', '1', 'H','\0' };
128af75078fSIntel 
129af75078fSIntel 		/* Clear screen and move to top left */
130af75078fSIntel 	printf("%s%s", clr, topLeft);
131af75078fSIntel 
132af75078fSIntel 	printf("\nPort statistics ====================================");
133af75078fSIntel 
1341c17baf4SIntel 	for (portid = 0; portid < RTE_MAX_ETHPORTS; portid++) {
135af75078fSIntel 		/* skip disabled ports */
136af75078fSIntel 		if ((l2fwd_enabled_port_mask & (1 << portid)) == 0)
137af75078fSIntel 			continue;
138af75078fSIntel 		printf("\nStatistics for port %u ------------------------------"
139af75078fSIntel 			   "\nPackets sent: %24"PRIu64
140af75078fSIntel 			   "\nPackets received: %20"PRIu64
141af75078fSIntel 			   "\nPackets dropped: %21"PRIu64,
142af75078fSIntel 			   portid,
143af75078fSIntel 			   port_statistics[portid].tx,
144af75078fSIntel 			   port_statistics[portid].rx,
145af75078fSIntel 			   port_statistics[portid].dropped);
146af75078fSIntel 
147af75078fSIntel 		total_packets_dropped += port_statistics[portid].dropped;
148af75078fSIntel 		total_packets_tx += port_statistics[portid].tx;
149af75078fSIntel 		total_packets_rx += port_statistics[portid].rx;
150af75078fSIntel 	}
151af75078fSIntel 	printf("\nAggregate statistics ==============================="
152af75078fSIntel 		   "\nTotal packets sent: %18"PRIu64
153af75078fSIntel 		   "\nTotal packets received: %14"PRIu64
154af75078fSIntel 		   "\nTotal packets dropped: %15"PRIu64,
155af75078fSIntel 		   total_packets_tx,
156af75078fSIntel 		   total_packets_rx,
157af75078fSIntel 		   total_packets_dropped);
158af75078fSIntel 	printf("\n====================================================\n");
1593ee6f706SGeorgiy Levashov 
1603ee6f706SGeorgiy Levashov 	fflush(stdout);
161af75078fSIntel }
162af75078fSIntel 
163af75078fSIntel static void
l2fwd_mac_updating(struct rte_mbuf * m,unsigned dest_portid)164cf435a07SMaxime Coquelin l2fwd_mac_updating(struct rte_mbuf *m, unsigned dest_portid)
165af75078fSIntel {
1666d13ea8eSOlivier Matz 	struct rte_ether_hdr *eth;
167af75078fSIntel 	void *tmp;
168cf435a07SMaxime Coquelin 
1696d13ea8eSOlivier Matz 	eth = rte_pktmbuf_mtod(m, struct rte_ether_hdr *);
170cf435a07SMaxime Coquelin 
171cf435a07SMaxime Coquelin 	/* 02:00:00:00:00:xx */
17204d43857SDmitry Kozlyuk 	tmp = &eth->dst_addr.addr_bytes[0];
173cf435a07SMaxime Coquelin 	*((uint64_t *)tmp) = 0x000000000002 + ((uint64_t)dest_portid << 40);
174cf435a07SMaxime Coquelin 
175cf435a07SMaxime Coquelin 	/* src addr */
17604d43857SDmitry Kozlyuk 	rte_ether_addr_copy(&l2fwd_ports_eth_addr[dest_portid], &eth->src_addr);
177cf435a07SMaxime Coquelin }
178cf435a07SMaxime Coquelin 
1799a212dc0SConor Fogarty /* Simple forward. 8< */
180cf435a07SMaxime Coquelin static void
l2fwd_simple_forward(struct rte_mbuf * m,unsigned portid)181cf435a07SMaxime Coquelin l2fwd_simple_forward(struct rte_mbuf *m, unsigned portid)
182cf435a07SMaxime Coquelin {
183af75078fSIntel 	unsigned dst_port;
184e2366e74STomasz Kulasek 	int sent;
185e2366e74STomasz Kulasek 	struct rte_eth_dev_tx_buffer *buffer;
186af75078fSIntel 
187af75078fSIntel 	dst_port = l2fwd_dst_ports[portid];
188af75078fSIntel 
189cf435a07SMaxime Coquelin 	if (mac_updating)
190cf435a07SMaxime Coquelin 		l2fwd_mac_updating(m, dst_port);
191af75078fSIntel 
192e2366e74STomasz Kulasek 	buffer = tx_buffer[dst_port];
193e2366e74STomasz Kulasek 	sent = rte_eth_tx_buffer(dst_port, 0, buffer, m);
194e2366e74STomasz Kulasek 	if (sent)
195e2366e74STomasz Kulasek 		port_statistics[dst_port].tx += sent;
196af75078fSIntel }
1979a212dc0SConor Fogarty /* >8 End of simple forward. */
198af75078fSIntel 
199af75078fSIntel /* main processing loop */
200af75078fSIntel static void
l2fwd_main_loop(void)201af75078fSIntel l2fwd_main_loop(void)
202af75078fSIntel {
203af75078fSIntel 	struct rte_mbuf *pkts_burst[MAX_PKT_BURST];
204af75078fSIntel 	struct rte_mbuf *m;
205e2366e74STomasz Kulasek 	int sent;
206af75078fSIntel 	unsigned lcore_id;
2075c95261dSIntel 	uint64_t prev_tsc, diff_tsc, cur_tsc, timer_tsc;
208af75078fSIntel 	unsigned i, j, portid, nb_rx;
209af75078fSIntel 	struct lcore_queue_conf *qconf;
210e2366e74STomasz Kulasek 	const uint64_t drain_tsc = (rte_get_tsc_hz() + US_PER_S - 1) / US_PER_S *
211e2366e74STomasz Kulasek 			BURST_TX_DRAIN_US;
212e2366e74STomasz Kulasek 	struct rte_eth_dev_tx_buffer *buffer;
213af75078fSIntel 
2145c95261dSIntel 	prev_tsc = 0;
215af75078fSIntel 	timer_tsc = 0;
216af75078fSIntel 
217af75078fSIntel 	lcore_id = rte_lcore_id();
218af75078fSIntel 	qconf = &lcore_queue_conf[lcore_id];
219af75078fSIntel 
22016ac9cf0SIntel 	if (qconf->n_rx_port == 0) {
221af75078fSIntel 		RTE_LOG(INFO, L2FWD, "lcore %u has nothing to do\n", lcore_id);
222cdfd5dbbSIntel 		return;
223af75078fSIntel 	}
224af75078fSIntel 
225af75078fSIntel 	RTE_LOG(INFO, L2FWD, "entering main loop on lcore %u\n", lcore_id);
226af75078fSIntel 
22716ac9cf0SIntel 	for (i = 0; i < qconf->n_rx_port; i++) {
228af75078fSIntel 
22916ac9cf0SIntel 		portid = qconf->rx_port_list[i];
230af75078fSIntel 		RTE_LOG(INFO, L2FWD, " -- lcoreid=%u portid=%u\n", lcore_id,
231af75078fSIntel 			portid);
232e2366e74STomasz Kulasek 
233af75078fSIntel 	}
234af75078fSIntel 
23588908b61SZhihong Wang 	while (!force_quit) {
236af75078fSIntel 
2379a212dc0SConor Fogarty 		/* Drains TX queue in its main loop. 8< */
238af75078fSIntel 		cur_tsc = rte_rdtsc();
239af75078fSIntel 
240af75078fSIntel 		/*
241af75078fSIntel 		 * TX burst queue drain
242af75078fSIntel 		 */
243af75078fSIntel 		diff_tsc = cur_tsc - prev_tsc;
2445c95261dSIntel 		if (unlikely(diff_tsc > drain_tsc)) {
245af75078fSIntel 
246e2366e74STomasz Kulasek 			for (i = 0; i < qconf->n_rx_port; i++) {
247e2366e74STomasz Kulasek 
248e2366e74STomasz Kulasek 				portid = l2fwd_dst_ports[qconf->rx_port_list[i]];
249e2366e74STomasz Kulasek 				buffer = tx_buffer[portid];
250e2366e74STomasz Kulasek 
251e2366e74STomasz Kulasek 				sent = rte_eth_tx_buffer_flush(portid, 0, buffer);
252e2366e74STomasz Kulasek 				if (sent)
253e2366e74STomasz Kulasek 					port_statistics[portid].tx += sent;
254e2366e74STomasz Kulasek 
255af75078fSIntel 			}
256af75078fSIntel 
257af75078fSIntel 			/* if timer is enabled */
258af75078fSIntel 			if (timer_period > 0) {
259af75078fSIntel 
260af75078fSIntel 				/* advance the timer */
261af75078fSIntel 				timer_tsc += diff_tsc;
262af75078fSIntel 
263af75078fSIntel 				/* if timer has reached its timeout */
2642412742cSJerin Jacob 				if (unlikely(timer_tsc >= timer_period)) {
265af75078fSIntel 
266cb056611SStephen Hemminger 					/* do this only on main core */
267cb056611SStephen Hemminger 					if (lcore_id == rte_get_main_lcore()) {
268af75078fSIntel 						print_stats();
269af75078fSIntel 						/* reset the timer */
270af75078fSIntel 						timer_tsc = 0;
271af75078fSIntel 					}
272af75078fSIntel 				}
273af75078fSIntel 			}
274af75078fSIntel 
275af75078fSIntel 			prev_tsc = cur_tsc;
276af75078fSIntel 		}
2779a212dc0SConor Fogarty 		/* >8 End of draining TX queue. */
278af75078fSIntel 
2799a212dc0SConor Fogarty 		/* Read packet from RX queues. 8< */
28016ac9cf0SIntel 		for (i = 0; i < qconf->n_rx_port; i++) {
281af75078fSIntel 
28216ac9cf0SIntel 			portid = qconf->rx_port_list[i];
283f8244c63SZhiyong Yang 			nb_rx = rte_eth_rx_burst(portid, 0,
284af75078fSIntel 						 pkts_burst, MAX_PKT_BURST);
285af75078fSIntel 
286a39f5e14SRahul Bhansali 			if (unlikely(nb_rx == 0))
287a39f5e14SRahul Bhansali 				continue;
288a39f5e14SRahul Bhansali 
289af75078fSIntel 			port_statistics[portid].rx += nb_rx;
290af75078fSIntel 
291af75078fSIntel 			for (j = 0; j < nb_rx; j++) {
292af75078fSIntel 				m = pkts_burst[j];
293af75078fSIntel 				rte_prefetch0(rte_pktmbuf_mtod(m, void *));
294af75078fSIntel 				l2fwd_simple_forward(m, portid);
295af75078fSIntel 			}
296af75078fSIntel 		}
2979a212dc0SConor Fogarty 		/* >8 End of read packet from RX queues. */
298af75078fSIntel 	}
299af75078fSIntel }
300af75078fSIntel 
301af75078fSIntel static int
l2fwd_launch_one_lcore(__rte_unused void * dummy)302f2fc83b4SThomas Monjalon l2fwd_launch_one_lcore(__rte_unused void *dummy)
303af75078fSIntel {
304af75078fSIntel 	l2fwd_main_loop();
305af75078fSIntel 	return 0;
306af75078fSIntel }
307af75078fSIntel 
308af75078fSIntel /* display usage */
309af75078fSIntel static void
l2fwd_usage(const char * prgname)310af75078fSIntel l2fwd_usage(const char *prgname)
311af75078fSIntel {
312b54403fdSSarosh Arif 	printf("%s [EAL options] -- -p PORTMASK [-P] [-q NQ]\n"
313af75078fSIntel 	       "  -p PORTMASK: hexadecimal bitmask of ports to configure\n"
314b54403fdSSarosh Arif 	       "  -P : Enable promiscuous mode\n"
315af75078fSIntel 	       "  -q NQ: number of queue (=ports) per lcore (default is 1)\n"
316cf435a07SMaxime Coquelin 	       "  -T PERIOD: statistics will be refreshed each PERIOD seconds (0 to disable, 10 default, 86400 maximum)\n"
317487c5825SChenglian Sun 	       "  --no-mac-updating: Disable MAC addresses updating (enabled by default)\n"
318cf435a07SMaxime Coquelin 	       "      When enabled:\n"
319cf435a07SMaxime Coquelin 	       "       - The source MAC address is replaced by the TX port MAC address\n"
320fa19eb20SVamsi Attunuru 	       "       - The destination MAC address is replaced by 02:00:00:00:00:TX_PORT_ID\n"
321fa19eb20SVamsi Attunuru 	       "  --portmap: Configure forwarding port pair mapping\n"
322fa19eb20SVamsi Attunuru 	       "	      Default: alternate port pairs\n\n",
323af75078fSIntel 	       prgname);
324af75078fSIntel }
325af75078fSIntel 
326af75078fSIntel static int
l2fwd_parse_portmask(const char * portmask)327af75078fSIntel l2fwd_parse_portmask(const char *portmask)
328af75078fSIntel {
329af75078fSIntel 	char *end = NULL;
330af75078fSIntel 	unsigned long pm;
331af75078fSIntel 
332af75078fSIntel 	/* parse hexadecimal string */
333af75078fSIntel 	pm = strtoul(portmask, &end, 16);
334af75078fSIntel 	if ((portmask[0] == '\0') || (end == NULL) || (*end != '\0'))
335ce6b8c31SSarosh Arif 		return 0;
336af75078fSIntel 
337af75078fSIntel 	return pm;
338af75078fSIntel }
339af75078fSIntel 
340fa19eb20SVamsi Attunuru static int
l2fwd_parse_port_pair_config(const char * q_arg)341fa19eb20SVamsi Attunuru l2fwd_parse_port_pair_config(const char *q_arg)
342fa19eb20SVamsi Attunuru {
343fa19eb20SVamsi Attunuru 	enum fieldnames {
344fa19eb20SVamsi Attunuru 		FLD_PORT1 = 0,
345fa19eb20SVamsi Attunuru 		FLD_PORT2,
346fa19eb20SVamsi Attunuru 		_NUM_FLD
347fa19eb20SVamsi Attunuru 	};
348fa19eb20SVamsi Attunuru 	unsigned long int_fld[_NUM_FLD];
349fa19eb20SVamsi Attunuru 	const char *p, *p0 = q_arg;
350fa19eb20SVamsi Attunuru 	char *str_fld[_NUM_FLD];
351fa19eb20SVamsi Attunuru 	unsigned int size;
352fa19eb20SVamsi Attunuru 	char s[256];
353fa19eb20SVamsi Attunuru 	char *end;
354fa19eb20SVamsi Attunuru 	int i;
355fa19eb20SVamsi Attunuru 
356fa19eb20SVamsi Attunuru 	nb_port_pair_params = 0;
357fa19eb20SVamsi Attunuru 
358fa19eb20SVamsi Attunuru 	while ((p = strchr(p0, '(')) != NULL) {
359fa19eb20SVamsi Attunuru 		++p;
360fa19eb20SVamsi Attunuru 		p0 = strchr(p, ')');
361fa19eb20SVamsi Attunuru 		if (p0 == NULL)
362fa19eb20SVamsi Attunuru 			return -1;
363fa19eb20SVamsi Attunuru 
364fa19eb20SVamsi Attunuru 		size = p0 - p;
365fa19eb20SVamsi Attunuru 		if (size >= sizeof(s))
366fa19eb20SVamsi Attunuru 			return -1;
367fa19eb20SVamsi Attunuru 
368fa19eb20SVamsi Attunuru 		memcpy(s, p, size);
369fa19eb20SVamsi Attunuru 		s[size] = '\0';
370fa19eb20SVamsi Attunuru 		if (rte_strsplit(s, sizeof(s), str_fld,
371fa19eb20SVamsi Attunuru 				 _NUM_FLD, ',') != _NUM_FLD)
372fa19eb20SVamsi Attunuru 			return -1;
373fa19eb20SVamsi Attunuru 		for (i = 0; i < _NUM_FLD; i++) {
374fa19eb20SVamsi Attunuru 			errno = 0;
375fa19eb20SVamsi Attunuru 			int_fld[i] = strtoul(str_fld[i], &end, 0);
376fa19eb20SVamsi Attunuru 			if (errno != 0 || end == str_fld[i] ||
377fa19eb20SVamsi Attunuru 			    int_fld[i] >= RTE_MAX_ETHPORTS)
378fa19eb20SVamsi Attunuru 				return -1;
379fa19eb20SVamsi Attunuru 		}
380fa19eb20SVamsi Attunuru 		if (nb_port_pair_params >= RTE_MAX_ETHPORTS/2) {
381fa19eb20SVamsi Attunuru 			printf("exceeded max number of port pair params: %hu\n",
382fa19eb20SVamsi Attunuru 				nb_port_pair_params);
383fa19eb20SVamsi Attunuru 			return -1;
384fa19eb20SVamsi Attunuru 		}
385fa19eb20SVamsi Attunuru 		port_pair_params_array[nb_port_pair_params].port[0] =
386fa19eb20SVamsi Attunuru 				(uint16_t)int_fld[FLD_PORT1];
387fa19eb20SVamsi Attunuru 		port_pair_params_array[nb_port_pair_params].port[1] =
388fa19eb20SVamsi Attunuru 				(uint16_t)int_fld[FLD_PORT2];
389fa19eb20SVamsi Attunuru 		++nb_port_pair_params;
390fa19eb20SVamsi Attunuru 	}
391fa19eb20SVamsi Attunuru 	port_pair_params = port_pair_params_array;
392fa19eb20SVamsi Attunuru 	return 0;
393fa19eb20SVamsi Attunuru }
394fa19eb20SVamsi Attunuru 
395af75078fSIntel static unsigned int
l2fwd_parse_nqueue(const char * q_arg)396af75078fSIntel l2fwd_parse_nqueue(const char *q_arg)
397af75078fSIntel {
398af75078fSIntel 	char *end = NULL;
399af75078fSIntel 	unsigned long n;
400af75078fSIntel 
401af75078fSIntel 	/* parse hexadecimal string */
402af75078fSIntel 	n = strtoul(q_arg, &end, 10);
403af75078fSIntel 	if ((q_arg[0] == '\0') || (end == NULL) || (*end != '\0'))
404af75078fSIntel 		return 0;
405af75078fSIntel 	if (n == 0)
406af75078fSIntel 		return 0;
407af75078fSIntel 	if (n >= MAX_RX_QUEUE_PER_LCORE)
408af75078fSIntel 		return 0;
409af75078fSIntel 
410af75078fSIntel 	return n;
411af75078fSIntel }
412af75078fSIntel 
413af75078fSIntel static int
l2fwd_parse_timer_period(const char * q_arg)414af75078fSIntel l2fwd_parse_timer_period(const char *q_arg)
415af75078fSIntel {
416af75078fSIntel 	char *end = NULL;
417af75078fSIntel 	int n;
418af75078fSIntel 
419af75078fSIntel 	/* parse number string */
420af75078fSIntel 	n = strtol(q_arg, &end, 10);
421af75078fSIntel 	if ((q_arg[0] == '\0') || (end == NULL) || (*end != '\0'))
422af75078fSIntel 		return -1;
423af75078fSIntel 	if (n >= MAX_TIMER_PERIOD)
424af75078fSIntel 		return -1;
425af75078fSIntel 
426af75078fSIntel 	return n;
427af75078fSIntel }
428af75078fSIntel 
4296876790dSOlivier Matz static const char short_options[] =
4306876790dSOlivier Matz 	"p:"  /* portmask */
431b54403fdSSarosh Arif 	"P"   /* promiscuous */
4326876790dSOlivier Matz 	"q:"  /* number of queues */
4336876790dSOlivier Matz 	"T:"  /* timer period */
4346876790dSOlivier Matz 	;
4356876790dSOlivier Matz 
4366876790dSOlivier Matz #define CMD_LINE_OPT_NO_MAC_UPDATING "no-mac-updating"
437fa19eb20SVamsi Attunuru #define CMD_LINE_OPT_PORTMAP_CONFIG "portmap"
4386876790dSOlivier Matz 
4396876790dSOlivier Matz enum {
4406876790dSOlivier Matz 	/* long options mapped to a short option */
4416876790dSOlivier Matz 
4426876790dSOlivier Matz 	/* first long only option value must be >= 256, so that we won't
4436876790dSOlivier Matz 	 * conflict with short options */
444487c5825SChenglian Sun 	CMD_LINE_OPT_NO_MAC_UPDATING_NUM = 256,
445fa19eb20SVamsi Attunuru 	CMD_LINE_OPT_PORTMAP_NUM,
4466876790dSOlivier Matz };
4476876790dSOlivier Matz 
4486876790dSOlivier Matz static const struct option lgopts[] = {
449bbbe38a6SChenglian Sun 	{ CMD_LINE_OPT_NO_MAC_UPDATING, no_argument, 0,
450bbbe38a6SChenglian Sun 		CMD_LINE_OPT_NO_MAC_UPDATING_NUM},
451fa19eb20SVamsi Attunuru 	{ CMD_LINE_OPT_PORTMAP_CONFIG, 1, 0, CMD_LINE_OPT_PORTMAP_NUM},
4526876790dSOlivier Matz 	{NULL, 0, 0, 0}
4536876790dSOlivier Matz };
4546876790dSOlivier Matz 
455af75078fSIntel /* Parse the argument given in the command line of the application */
456af75078fSIntel static int
l2fwd_parse_args(int argc,char ** argv)457af75078fSIntel l2fwd_parse_args(int argc, char **argv)
458af75078fSIntel {
4592412742cSJerin Jacob 	int opt, ret, timer_secs;
460af75078fSIntel 	char **argvopt;
461af75078fSIntel 	int option_index;
462af75078fSIntel 	char *prgname = argv[0];
463af75078fSIntel 
464af75078fSIntel 	argvopt = argv;
465fa19eb20SVamsi Attunuru 	port_pair_params = NULL;
466af75078fSIntel 
4676876790dSOlivier Matz 	while ((opt = getopt_long(argc, argvopt, short_options,
468af75078fSIntel 				  lgopts, &option_index)) != EOF) {
469af75078fSIntel 
470af75078fSIntel 		switch (opt) {
471af75078fSIntel 		/* portmask */
472af75078fSIntel 		case 'p':
473af75078fSIntel 			l2fwd_enabled_port_mask = l2fwd_parse_portmask(optarg);
474af75078fSIntel 			if (l2fwd_enabled_port_mask == 0) {
475af75078fSIntel 				printf("invalid portmask\n");
476af75078fSIntel 				l2fwd_usage(prgname);
477af75078fSIntel 				return -1;
478af75078fSIntel 			}
479af75078fSIntel 			break;
480b54403fdSSarosh Arif 		case 'P':
481b54403fdSSarosh Arif 			promiscuous_on = 1;
482b54403fdSSarosh Arif 			break;
483af75078fSIntel 
484af75078fSIntel 		/* nqueue */
485af75078fSIntel 		case 'q':
486af75078fSIntel 			l2fwd_rx_queue_per_lcore = l2fwd_parse_nqueue(optarg);
487af75078fSIntel 			if (l2fwd_rx_queue_per_lcore == 0) {
488af75078fSIntel 				printf("invalid queue number\n");
489af75078fSIntel 				l2fwd_usage(prgname);
490af75078fSIntel 				return -1;
491af75078fSIntel 			}
492af75078fSIntel 			break;
493af75078fSIntel 
494af75078fSIntel 		/* timer period */
495af75078fSIntel 		case 'T':
4962412742cSJerin Jacob 			timer_secs = l2fwd_parse_timer_period(optarg);
4972412742cSJerin Jacob 			if (timer_secs < 0) {
498af75078fSIntel 				printf("invalid timer period\n");
499af75078fSIntel 				l2fwd_usage(prgname);
500af75078fSIntel 				return -1;
501af75078fSIntel 			}
5022412742cSJerin Jacob 			timer_period = timer_secs;
503af75078fSIntel 			break;
504af75078fSIntel 
505af75078fSIntel 		/* long options */
506fa19eb20SVamsi Attunuru 		case CMD_LINE_OPT_PORTMAP_NUM:
507fa19eb20SVamsi Attunuru 			ret = l2fwd_parse_port_pair_config(optarg);
508fa19eb20SVamsi Attunuru 			if (ret) {
509fa19eb20SVamsi Attunuru 				fprintf(stderr, "Invalid config\n");
510fa19eb20SVamsi Attunuru 				l2fwd_usage(prgname);
511fa19eb20SVamsi Attunuru 				return -1;
512fa19eb20SVamsi Attunuru 			}
513cf435a07SMaxime Coquelin 			break;
514af75078fSIntel 
515bbbe38a6SChenglian Sun 		case CMD_LINE_OPT_NO_MAC_UPDATING_NUM:
516bbbe38a6SChenglian Sun 			mac_updating = 0;
517bbbe38a6SChenglian Sun 			break;
518bbbe38a6SChenglian Sun 
519af75078fSIntel 		default:
520af75078fSIntel 			l2fwd_usage(prgname);
521af75078fSIntel 			return -1;
522af75078fSIntel 		}
523af75078fSIntel 	}
524af75078fSIntel 
525af75078fSIntel 	if (optind >= 0)
526af75078fSIntel 		argv[optind-1] = prgname;
527af75078fSIntel 
528af75078fSIntel 	ret = optind-1;
5299d5ca532SKeith Wiles 	optind = 1; /* reset getopt lib */
530af75078fSIntel 	return ret;
531af75078fSIntel }
532af75078fSIntel 
533fa19eb20SVamsi Attunuru /*
534fa19eb20SVamsi Attunuru  * Check port pair config with enabled port mask,
535fa19eb20SVamsi Attunuru  * and for valid port pair combinations.
536fa19eb20SVamsi Attunuru  */
537fa19eb20SVamsi Attunuru static int
check_port_pair_config(void)538fa19eb20SVamsi Attunuru check_port_pair_config(void)
539fa19eb20SVamsi Attunuru {
540fa19eb20SVamsi Attunuru 	uint32_t port_pair_config_mask = 0;
541fa19eb20SVamsi Attunuru 	uint32_t port_pair_mask = 0;
542fa19eb20SVamsi Attunuru 	uint16_t index, i, portid;
543fa19eb20SVamsi Attunuru 
544fa19eb20SVamsi Attunuru 	for (index = 0; index < nb_port_pair_params; index++) {
545fa19eb20SVamsi Attunuru 		port_pair_mask = 0;
546fa19eb20SVamsi Attunuru 
547fa19eb20SVamsi Attunuru 		for (i = 0; i < NUM_PORTS; i++)  {
548fa19eb20SVamsi Attunuru 			portid = port_pair_params[index].port[i];
549fa19eb20SVamsi Attunuru 			if ((l2fwd_enabled_port_mask & (1 << portid)) == 0) {
550fa19eb20SVamsi Attunuru 				printf("port %u is not enabled in port mask\n",
551fa19eb20SVamsi Attunuru 				       portid);
552fa19eb20SVamsi Attunuru 				return -1;
553fa19eb20SVamsi Attunuru 			}
554fa19eb20SVamsi Attunuru 			if (!rte_eth_dev_is_valid_port(portid)) {
555fa19eb20SVamsi Attunuru 				printf("port %u is not present on the board\n",
556fa19eb20SVamsi Attunuru 				       portid);
557fa19eb20SVamsi Attunuru 				return -1;
558fa19eb20SVamsi Attunuru 			}
559fa19eb20SVamsi Attunuru 
560fa19eb20SVamsi Attunuru 			port_pair_mask |= 1 << portid;
561fa19eb20SVamsi Attunuru 		}
562fa19eb20SVamsi Attunuru 
563fa19eb20SVamsi Attunuru 		if (port_pair_config_mask & port_pair_mask) {
564fa19eb20SVamsi Attunuru 			printf("port %u is used in other port pairs\n", portid);
565fa19eb20SVamsi Attunuru 			return -1;
566fa19eb20SVamsi Attunuru 		}
567fa19eb20SVamsi Attunuru 		port_pair_config_mask |= port_pair_mask;
568fa19eb20SVamsi Attunuru 	}
569fa19eb20SVamsi Attunuru 
570fa19eb20SVamsi Attunuru 	l2fwd_enabled_port_mask &= port_pair_config_mask;
571fa19eb20SVamsi Attunuru 
572fa19eb20SVamsi Attunuru 	return 0;
573fa19eb20SVamsi Attunuru }
574fa19eb20SVamsi Attunuru 
575d3641ae8SIntel /* Check the link status of all ports in up to 9s, and print them finally */
576d3641ae8SIntel static void
check_all_ports_link_status(uint32_t port_mask)5778728ccf3SThomas Monjalon check_all_ports_link_status(uint32_t port_mask)
578d3641ae8SIntel {
579d3641ae8SIntel #define CHECK_INTERVAL 100 /* 100ms */
580d3641ae8SIntel #define MAX_CHECK_TIME 90 /* 9s (90 * 100ms) in total */
581f8244c63SZhiyong Yang 	uint16_t portid;
582f8244c63SZhiyong Yang 	uint8_t count, all_ports_up, print_flag = 0;
583d3641ae8SIntel 	struct rte_eth_link link;
58422e5c73bSIgor Romanov 	int ret;
585db4e8135SIvan Dyukov 	char link_status_text[RTE_ETH_LINK_MAX_STR_LEN];
586d3641ae8SIntel 
587d3641ae8SIntel 	printf("\nChecking link status");
588d3641ae8SIntel 	fflush(stdout);
589d3641ae8SIntel 	for (count = 0; count <= MAX_CHECK_TIME; count++) {
59088908b61SZhihong Wang 		if (force_quit)
59188908b61SZhihong Wang 			return;
592d3641ae8SIntel 		all_ports_up = 1;
5938728ccf3SThomas Monjalon 		RTE_ETH_FOREACH_DEV(portid) {
59488908b61SZhihong Wang 			if (force_quit)
59588908b61SZhihong Wang 				return;
596d3641ae8SIntel 			if ((port_mask & (1 << portid)) == 0)
597d3641ae8SIntel 				continue;
598d3641ae8SIntel 			memset(&link, 0, sizeof(link));
59922e5c73bSIgor Romanov 			ret = rte_eth_link_get_nowait(portid, &link);
60022e5c73bSIgor Romanov 			if (ret < 0) {
60122e5c73bSIgor Romanov 				all_ports_up = 0;
60222e5c73bSIgor Romanov 				if (print_flag == 1)
60322e5c73bSIgor Romanov 					printf("Port %u link get failed: %s\n",
60422e5c73bSIgor Romanov 						portid, rte_strerror(-ret));
60522e5c73bSIgor Romanov 				continue;
60622e5c73bSIgor Romanov 			}
607d3641ae8SIntel 			/* print link status if flag set */
608d3641ae8SIntel 			if (print_flag == 1) {
609db4e8135SIvan Dyukov 				rte_eth_link_to_str(link_status_text,
610db4e8135SIvan Dyukov 					sizeof(link_status_text), &link);
611db4e8135SIvan Dyukov 				printf("Port %d %s\n", portid,
612db4e8135SIvan Dyukov 				       link_status_text);
613d3641ae8SIntel 				continue;
614d3641ae8SIntel 			}
615d3641ae8SIntel 			/* clear all_ports_up flag if any link down */
616295968d1SFerruh Yigit 			if (link.link_status == RTE_ETH_LINK_DOWN) {
617d3641ae8SIntel 				all_ports_up = 0;
618d3641ae8SIntel 				break;
619d3641ae8SIntel 			}
620d3641ae8SIntel 		}
621d3641ae8SIntel 		/* after finally printing all link status, get out */
622d3641ae8SIntel 		if (print_flag == 1)
623d3641ae8SIntel 			break;
624d3641ae8SIntel 
625d3641ae8SIntel 		if (all_ports_up == 0) {
626d3641ae8SIntel 			printf(".");
627d3641ae8SIntel 			fflush(stdout);
628d3641ae8SIntel 			rte_delay_ms(CHECK_INTERVAL);
629d3641ae8SIntel 		}
630d3641ae8SIntel 
631d3641ae8SIntel 		/* set the print_flag if all ports up or timeout */
632d3641ae8SIntel 		if (all_ports_up == 1 || count == (MAX_CHECK_TIME - 1)) {
633d3641ae8SIntel 			print_flag = 1;
634d3641ae8SIntel 			printf("done\n");
635d3641ae8SIntel 		}
636d3641ae8SIntel 	}
637d3641ae8SIntel }
638d3641ae8SIntel 
63988908b61SZhihong Wang static void
signal_handler(int signum)64088908b61SZhihong Wang signal_handler(int signum)
64188908b61SZhihong Wang {
64288908b61SZhihong Wang 	if (signum == SIGINT || signum == SIGTERM) {
64388908b61SZhihong Wang 		printf("\n\nSignal %d received, preparing to exit...\n",
64488908b61SZhihong Wang 				signum);
64588908b61SZhihong Wang 		force_quit = true;
64688908b61SZhihong Wang 	}
64788908b61SZhihong Wang }
64888908b61SZhihong Wang 
649af75078fSIntel int
main(int argc,char ** argv)65098a16481SDavid Marchand main(int argc, char **argv)
651af75078fSIntel {
652af75078fSIntel 	struct lcore_queue_conf *qconf;
653af75078fSIntel 	int ret;
654f8244c63SZhiyong Yang 	uint16_t nb_ports;
6558728ccf3SThomas Monjalon 	uint16_t nb_ports_available = 0;
656f8244c63SZhiyong Yang 	uint16_t portid, last_port;
657af75078fSIntel 	unsigned lcore_id, rx_lcore_id;
658af75078fSIntel 	unsigned nb_ports_in_mask = 0;
6590b0ceb98SPavan Nikhilesh 	unsigned int nb_lcores = 0;
6600b0ceb98SPavan Nikhilesh 	unsigned int nb_mbufs;
661af75078fSIntel 
6629a212dc0SConor Fogarty 	/* Init EAL. 8< */
663af75078fSIntel 	ret = rte_eal_init(argc, argv);
664af75078fSIntel 	if (ret < 0)
665af75078fSIntel 		rte_exit(EXIT_FAILURE, "Invalid EAL arguments\n");
666af75078fSIntel 	argc -= ret;
667af75078fSIntel 	argv += ret;
668af75078fSIntel 
66988908b61SZhihong Wang 	force_quit = false;
67088908b61SZhihong Wang 	signal(SIGINT, signal_handler);
67188908b61SZhihong Wang 	signal(SIGTERM, signal_handler);
67288908b61SZhihong Wang 
673af75078fSIntel 	/* parse application arguments (after the EAL ones) */
674af75078fSIntel 	ret = l2fwd_parse_args(argc, argv);
675af75078fSIntel 	if (ret < 0)
676af75078fSIntel 		rte_exit(EXIT_FAILURE, "Invalid L2FWD arguments\n");
6779a212dc0SConor Fogarty 	/* >8 End of init EAL. */
678af75078fSIntel 
679cf435a07SMaxime Coquelin 	printf("MAC updating %s\n", mac_updating ? "enabled" : "disabled");
680cf435a07SMaxime Coquelin 
6812412742cSJerin Jacob 	/* convert to number of cycles */
6822412742cSJerin Jacob 	timer_period *= rte_get_timer_hz();
6832412742cSJerin Jacob 
684d9a42a69SThomas Monjalon 	nb_ports = rte_eth_dev_count_avail();
685af75078fSIntel 	if (nb_ports == 0)
686af75078fSIntel 		rte_exit(EXIT_FAILURE, "No Ethernet ports - bye\n");
687af75078fSIntel 
688fa19eb20SVamsi Attunuru 	if (port_pair_params != NULL) {
689fa19eb20SVamsi Attunuru 		if (check_port_pair_config() < 0)
690fa19eb20SVamsi Attunuru 			rte_exit(EXIT_FAILURE, "Invalid port pair config\n");
691fa19eb20SVamsi Attunuru 	}
692fa19eb20SVamsi Attunuru 
69367265219SVipin Varghese 	/* check port mask to possible port mask */
69467265219SVipin Varghese 	if (l2fwd_enabled_port_mask & ~((1 << nb_ports) - 1))
69567265219SVipin Varghese 		rte_exit(EXIT_FAILURE, "Invalid portmask; possible (0x%x)\n",
69667265219SVipin Varghese 			(1 << nb_ports) - 1);
69767265219SVipin Varghese 
6989a212dc0SConor Fogarty 	/* Initialization of the driver. 8< */
6999a212dc0SConor Fogarty 
700af75078fSIntel 	/* reset l2fwd_dst_ports */
7011c17baf4SIntel 	for (portid = 0; portid < RTE_MAX_ETHPORTS; portid++)
702af75078fSIntel 		l2fwd_dst_ports[portid] = 0;
703af75078fSIntel 	last_port = 0;
704af75078fSIntel 
705fa19eb20SVamsi Attunuru 	/* populate destination port details */
706fa19eb20SVamsi Attunuru 	if (port_pair_params != NULL) {
707fa19eb20SVamsi Attunuru 		uint16_t idx, p;
708fa19eb20SVamsi Attunuru 
709fa19eb20SVamsi Attunuru 		for (idx = 0; idx < (nb_port_pair_params << 1); idx++) {
710fa19eb20SVamsi Attunuru 			p = idx & 1;
711fa19eb20SVamsi Attunuru 			portid = port_pair_params[idx >> 1].port[p];
712fa19eb20SVamsi Attunuru 			l2fwd_dst_ports[portid] =
713fa19eb20SVamsi Attunuru 				port_pair_params[idx >> 1].port[p ^ 1];
714fa19eb20SVamsi Attunuru 		}
715fa19eb20SVamsi Attunuru 	} else {
7168728ccf3SThomas Monjalon 		RTE_ETH_FOREACH_DEV(portid) {
717af75078fSIntel 			/* skip ports that are not enabled */
718af75078fSIntel 			if ((l2fwd_enabled_port_mask & (1 << portid)) == 0)
719af75078fSIntel 				continue;
720af75078fSIntel 
721af75078fSIntel 			if (nb_ports_in_mask % 2) {
722af75078fSIntel 				l2fwd_dst_ports[portid] = last_port;
723af75078fSIntel 				l2fwd_dst_ports[last_port] = portid;
724fa19eb20SVamsi Attunuru 			} else {
725af75078fSIntel 				last_port = portid;
726fa19eb20SVamsi Attunuru 			}
727af75078fSIntel 
728af75078fSIntel 			nb_ports_in_mask++;
729af75078fSIntel 		}
730f56d0815SIntel 		if (nb_ports_in_mask % 2) {
73116ac9cf0SIntel 			printf("Notice: odd number of ports in portmask.\n");
73216ac9cf0SIntel 			l2fwd_dst_ports[last_port] = last_port;
733af75078fSIntel 		}
734fa19eb20SVamsi Attunuru 	}
7359a212dc0SConor Fogarty 	/* >8 End of initialization of the driver. */
736af75078fSIntel 
737af75078fSIntel 	rx_lcore_id = 0;
738af75078fSIntel 	qconf = NULL;
739af75078fSIntel 
740af75078fSIntel 	/* Initialize the port/queue configuration of each logical core */
7418728ccf3SThomas Monjalon 	RTE_ETH_FOREACH_DEV(portid) {
742af75078fSIntel 		/* skip ports that are not enabled */
743af75078fSIntel 		if ((l2fwd_enabled_port_mask & (1 << portid)) == 0)
744af75078fSIntel 			continue;
745af75078fSIntel 
746af75078fSIntel 		/* get the lcore_id for this port */
747af75078fSIntel 		while (rte_lcore_is_enabled(rx_lcore_id) == 0 ||
74816ac9cf0SIntel 		       lcore_queue_conf[rx_lcore_id].n_rx_port ==
749af75078fSIntel 		       l2fwd_rx_queue_per_lcore) {
750af75078fSIntel 			rx_lcore_id++;
751af75078fSIntel 			if (rx_lcore_id >= RTE_MAX_LCORE)
752af75078fSIntel 				rte_exit(EXIT_FAILURE, "Not enough cores\n");
753af75078fSIntel 		}
75416ac9cf0SIntel 
7550b0ceb98SPavan Nikhilesh 		if (qconf != &lcore_queue_conf[rx_lcore_id]) {
756af75078fSIntel 			/* Assigned a new logical core in the loop above. */
757af75078fSIntel 			qconf = &lcore_queue_conf[rx_lcore_id];
7580b0ceb98SPavan Nikhilesh 			nb_lcores++;
7590b0ceb98SPavan Nikhilesh 		}
76016ac9cf0SIntel 
76116ac9cf0SIntel 		qconf->rx_port_list[qconf->n_rx_port] = portid;
76216ac9cf0SIntel 		qconf->n_rx_port++;
763fa19eb20SVamsi Attunuru 		printf("Lcore %u: RX port %u TX port %u\n", rx_lcore_id,
764fa19eb20SVamsi Attunuru 		       portid, l2fwd_dst_ports[portid]);
765af75078fSIntel 	}
766af75078fSIntel 
7670b0ceb98SPavan Nikhilesh 	nb_mbufs = RTE_MAX(nb_ports * (nb_rxd + nb_txd + MAX_PKT_BURST +
7680b0ceb98SPavan Nikhilesh 		nb_lcores * MEMPOOL_CACHE_SIZE), 8192U);
7690b0ceb98SPavan Nikhilesh 
7709a212dc0SConor Fogarty 	/* Create the mbuf pool. 8< */
7710b0ceb98SPavan Nikhilesh 	l2fwd_pktmbuf_pool = rte_pktmbuf_pool_create("mbuf_pool", nb_mbufs,
7720b0ceb98SPavan Nikhilesh 		MEMPOOL_CACHE_SIZE, 0, RTE_MBUF_DEFAULT_BUF_SIZE,
7730b0ceb98SPavan Nikhilesh 		rte_socket_id());
7740b0ceb98SPavan Nikhilesh 	if (l2fwd_pktmbuf_pool == NULL)
7750b0ceb98SPavan Nikhilesh 		rte_exit(EXIT_FAILURE, "Cannot init mbuf pool\n");
7769a212dc0SConor Fogarty 	/* >8 End of create the mbuf pool. */
7770b0ceb98SPavan Nikhilesh 
778af75078fSIntel 	/* Initialise each port */
7798728ccf3SThomas Monjalon 	RTE_ETH_FOREACH_DEV(portid) {
78075d7f266SShahaf Shuler 		struct rte_eth_rxconf rxq_conf;
78175d7f266SShahaf Shuler 		struct rte_eth_txconf txq_conf;
78275d7f266SShahaf Shuler 		struct rte_eth_conf local_port_conf = port_conf;
78375d7f266SShahaf Shuler 		struct rte_eth_dev_info dev_info;
78475d7f266SShahaf Shuler 
785af75078fSIntel 		/* skip ports that are not enabled */
786af75078fSIntel 		if ((l2fwd_enabled_port_mask & (1 << portid)) == 0) {
787f8244c63SZhiyong Yang 			printf("Skipping disabled port %u\n", portid);
788af75078fSIntel 			continue;
789af75078fSIntel 		}
7908728ccf3SThomas Monjalon 		nb_ports_available++;
7918728ccf3SThomas Monjalon 
792af75078fSIntel 		/* init port */
793f8244c63SZhiyong Yang 		printf("Initializing port %u... ", portid);
794af75078fSIntel 		fflush(stdout);
795089e5ed7SIvan Ilchenko 
796089e5ed7SIvan Ilchenko 		ret = rte_eth_dev_info_get(portid, &dev_info);
797089e5ed7SIvan Ilchenko 		if (ret != 0)
798089e5ed7SIvan Ilchenko 			rte_exit(EXIT_FAILURE,
799089e5ed7SIvan Ilchenko 				"Error during getting device (port %u) info: %s\n",
800089e5ed7SIvan Ilchenko 				portid, strerror(-ret));
801089e5ed7SIvan Ilchenko 
802295968d1SFerruh Yigit 		if (dev_info.tx_offload_capa & RTE_ETH_TX_OFFLOAD_MBUF_FAST_FREE)
80375d7f266SShahaf Shuler 			local_port_conf.txmode.offloads |=
804295968d1SFerruh Yigit 				RTE_ETH_TX_OFFLOAD_MBUF_FAST_FREE;
8059a212dc0SConor Fogarty 		/* Configure the number of queues for a port. */
80675d7f266SShahaf Shuler 		ret = rte_eth_dev_configure(portid, 1, 1, &local_port_conf);
807af75078fSIntel 		if (ret < 0)
80816ac9cf0SIntel 			rte_exit(EXIT_FAILURE, "Cannot configure device: err=%d, port=%u\n",
809f8244c63SZhiyong Yang 				  ret, portid);
8109a212dc0SConor Fogarty 		/* >8 End of configuration of the number of queues for a port. */
811af75078fSIntel 
81260efb44fSRoman Zhukov 		ret = rte_eth_dev_adjust_nb_rx_tx_desc(portid, &nb_rxd,
81360efb44fSRoman Zhukov 						       &nb_txd);
81460efb44fSRoman Zhukov 		if (ret < 0)
81560efb44fSRoman Zhukov 			rte_exit(EXIT_FAILURE,
81660efb44fSRoman Zhukov 				 "Cannot adjust number of descriptors: err=%d, port=%u\n",
817f8244c63SZhiyong Yang 				 ret, portid);
81860efb44fSRoman Zhukov 
81970febdcfSIgor Romanov 		ret = rte_eth_macaddr_get(portid,
82070febdcfSIgor Romanov 					  &l2fwd_ports_eth_addr[portid]);
82170febdcfSIgor Romanov 		if (ret < 0)
82270febdcfSIgor Romanov 			rte_exit(EXIT_FAILURE,
82370febdcfSIgor Romanov 				 "Cannot get MAC address: err=%d, port=%u\n",
82470febdcfSIgor Romanov 				 ret, portid);
825af75078fSIntel 
826af75078fSIntel 		/* init one RX queue */
827af75078fSIntel 		fflush(stdout);
82875d7f266SShahaf Shuler 		rxq_conf = dev_info.default_rxconf;
82975d7f266SShahaf Shuler 		rxq_conf.offloads = local_port_conf.rxmode.offloads;
8309a212dc0SConor Fogarty 		/* RX queue setup. 8< */
831a974564bSIntel 		ret = rte_eth_rx_queue_setup(portid, 0, nb_rxd,
83281f7ecd9SPablo de Lara 					     rte_eth_dev_socket_id(portid),
83375d7f266SShahaf Shuler 					     &rxq_conf,
834af75078fSIntel 					     l2fwd_pktmbuf_pool);
835af75078fSIntel 		if (ret < 0)
836a974564bSIntel 			rte_exit(EXIT_FAILURE, "rte_eth_rx_queue_setup:err=%d, port=%u\n",
837f8244c63SZhiyong Yang 				  ret, portid);
8389a212dc0SConor Fogarty 		/* >8 End of RX queue setup. */
839af75078fSIntel 
8409a212dc0SConor Fogarty 		/* Init one TX queue on each port. 8< */
841af75078fSIntel 		fflush(stdout);
84275d7f266SShahaf Shuler 		txq_conf = dev_info.default_txconf;
84375d7f266SShahaf Shuler 		txq_conf.offloads = local_port_conf.txmode.offloads;
844a974564bSIntel 		ret = rte_eth_tx_queue_setup(portid, 0, nb_txd,
84581f7ecd9SPablo de Lara 				rte_eth_dev_socket_id(portid),
84675d7f266SShahaf Shuler 				&txq_conf);
847af75078fSIntel 		if (ret < 0)
84816ac9cf0SIntel 			rte_exit(EXIT_FAILURE, "rte_eth_tx_queue_setup:err=%d, port=%u\n",
849f8244c63SZhiyong Yang 				ret, portid);
8509a212dc0SConor Fogarty 		/* >8 End of init one TX queue on each port. */
851af75078fSIntel 
852e2366e74STomasz Kulasek 		/* Initialize TX buffers */
853e2366e74STomasz Kulasek 		tx_buffer[portid] = rte_zmalloc_socket("tx_buffer",
854e2366e74STomasz Kulasek 				RTE_ETH_TX_BUFFER_SIZE(MAX_PKT_BURST), 0,
855e2366e74STomasz Kulasek 				rte_eth_dev_socket_id(portid));
856e2366e74STomasz Kulasek 		if (tx_buffer[portid] == NULL)
857e2366e74STomasz Kulasek 			rte_exit(EXIT_FAILURE, "Cannot allocate buffer for tx on port %u\n",
858f8244c63SZhiyong Yang 					portid);
859e2366e74STomasz Kulasek 
860e2366e74STomasz Kulasek 		rte_eth_tx_buffer_init(tx_buffer[portid], MAX_PKT_BURST);
861e2366e74STomasz Kulasek 
862e2366e74STomasz Kulasek 		ret = rte_eth_tx_buffer_set_err_callback(tx_buffer[portid],
863e2366e74STomasz Kulasek 				rte_eth_tx_buffer_count_callback,
864e2366e74STomasz Kulasek 				&port_statistics[portid].dropped);
865e2366e74STomasz Kulasek 		if (ret < 0)
866f8244c63SZhiyong Yang 			rte_exit(EXIT_FAILURE,
867f8244c63SZhiyong Yang 			"Cannot set error callback for tx buffer on port %u\n",
868f8244c63SZhiyong Yang 				 portid);
869e2366e74STomasz Kulasek 
8709731df2eSPavan Nikhilesh 		ret = rte_eth_dev_set_ptypes(portid, RTE_PTYPE_UNKNOWN, NULL,
8719731df2eSPavan Nikhilesh 					     0);
8729731df2eSPavan Nikhilesh 		if (ret < 0)
8739731df2eSPavan Nikhilesh 			printf("Port %u, Failed to disable Ptype parsing\n",
8749731df2eSPavan Nikhilesh 					portid);
875af75078fSIntel 		/* Start device */
876a974564bSIntel 		ret = rte_eth_dev_start(portid);
877af75078fSIntel 		if (ret < 0)
87816ac9cf0SIntel 			rte_exit(EXIT_FAILURE, "rte_eth_dev_start:err=%d, port=%u\n",
879f8244c63SZhiyong Yang 				  ret, portid);
880af75078fSIntel 
881d3641ae8SIntel 		printf("done: \n");
882b54403fdSSarosh Arif 		if (promiscuous_on) {
883f430bbceSIvan Ilchenko 			ret = rte_eth_promiscuous_enable(portid);
884f430bbceSIvan Ilchenko 			if (ret != 0)
885f430bbceSIvan Ilchenko 				rte_exit(EXIT_FAILURE,
886f430bbceSIvan Ilchenko 					"rte_eth_promiscuous_enable:err=%s, port=%u\n",
887f430bbceSIvan Ilchenko 					rte_strerror(-ret), portid);
888b54403fdSSarosh Arif 		}
889af75078fSIntel 
890c2c4f87bSAman Deep Singh 		printf("Port %u, MAC address: " RTE_ETHER_ADDR_PRT_FMT "\n\n",
891f8244c63SZhiyong Yang 			portid,
892a7db3afcSAman Deep Singh 			RTE_ETHER_ADDR_BYTES(&l2fwd_ports_eth_addr[portid]));
893af75078fSIntel 
894af75078fSIntel 		/* initialize port stats */
895af75078fSIntel 		memset(&port_statistics, 0, sizeof(port_statistics));
896af75078fSIntel 	}
897af75078fSIntel 
898f56d0815SIntel 	if (!nb_ports_available) {
899f56d0815SIntel 		rte_exit(EXIT_FAILURE,
900f56d0815SIntel 			"All available ports are disabled. Please set portmask.\n");
901f56d0815SIntel 	}
902f56d0815SIntel 
9038728ccf3SThomas Monjalon 	check_all_ports_link_status(l2fwd_enabled_port_mask);
904d3641ae8SIntel 
90588908b61SZhihong Wang 	ret = 0;
906af75078fSIntel 	/* launch per-lcore init on every lcore */
907cb056611SStephen Hemminger 	rte_eal_mp_remote_launch(l2fwd_launch_one_lcore, NULL, CALL_MAIN);
908cb056611SStephen Hemminger 	RTE_LCORE_FOREACH_WORKER(lcore_id) {
90988908b61SZhihong Wang 		if (rte_eal_wait_lcore(lcore_id) < 0) {
91088908b61SZhihong Wang 			ret = -1;
91188908b61SZhihong Wang 			break;
91288908b61SZhihong Wang 		}
913af75078fSIntel 	}
914af75078fSIntel 
9158728ccf3SThomas Monjalon 	RTE_ETH_FOREACH_DEV(portid) {
91688908b61SZhihong Wang 		if ((l2fwd_enabled_port_mask & (1 << portid)) == 0)
91788908b61SZhihong Wang 			continue;
91888908b61SZhihong Wang 		printf("Closing port %d...", portid);
919b55efbabSIvan Ilchenko 		ret = rte_eth_dev_stop(portid);
920b55efbabSIvan Ilchenko 		if (ret != 0)
921b55efbabSIvan Ilchenko 			printf("rte_eth_dev_stop: err=%d, port=%d\n",
922b55efbabSIvan Ilchenko 			       ret, portid);
92388908b61SZhihong Wang 		rte_eth_dev_close(portid);
92488908b61SZhihong Wang 		printf(" Done\n");
92588908b61SZhihong Wang 	}
92610aa3757SChengchang Tang 
92710aa3757SChengchang Tang 	/* clean up the EAL */
92810aa3757SChengchang Tang 	rte_eal_cleanup();
92988908b61SZhihong Wang 	printf("Bye...\n");
93088908b61SZhihong Wang 
93188908b61SZhihong Wang 	return ret;
932af75078fSIntel }
933