xref: /dpdk/examples/l2fwd-keepalive/main.c (revision 13830b98b2bcf6a8a1aa8edde5132907c053861c)
13998e2a0SBruce Richardson /* SPDX-License-Identifier: BSD-3-Clause
23998e2a0SBruce Richardson  * Copyright(c) 2010-2016 Intel Corporation
3e64833f2SRemy Horton  */
4e64833f2SRemy Horton 
5e64833f2SRemy Horton #include <stdio.h>
6e64833f2SRemy Horton #include <stdlib.h>
7e64833f2SRemy Horton #include <string.h>
8e64833f2SRemy Horton #include <stdint.h>
9e64833f2SRemy Horton #include <inttypes.h>
10e64833f2SRemy Horton #include <sys/types.h>
11e64833f2SRemy Horton #include <sys/queue.h>
12e64833f2SRemy Horton #include <netinet/in.h>
13e64833f2SRemy Horton #include <setjmp.h>
14e64833f2SRemy Horton #include <stdarg.h>
15e64833f2SRemy Horton #include <ctype.h>
16e64833f2SRemy Horton #include <errno.h>
17e64833f2SRemy Horton #include <getopt.h>
1891e89e47SRemy Horton #include <signal.h>
19e64833f2SRemy Horton 
20e64833f2SRemy Horton #include <rte_common.h>
21e64833f2SRemy Horton #include <rte_log.h>
22e2366e74STomasz Kulasek #include <rte_malloc.h>
23e64833f2SRemy Horton #include <rte_memory.h>
24e64833f2SRemy Horton #include <rte_memcpy.h>
25e64833f2SRemy Horton #include <rte_eal.h>
26e64833f2SRemy Horton #include <rte_launch.h>
27e64833f2SRemy Horton #include <rte_cycles.h>
28e64833f2SRemy Horton #include <rte_prefetch.h>
29e64833f2SRemy Horton #include <rte_lcore.h>
30e64833f2SRemy Horton #include <rte_per_lcore.h>
31e64833f2SRemy Horton #include <rte_branch_prediction.h>
32e64833f2SRemy Horton #include <rte_interrupts.h>
33e64833f2SRemy Horton #include <rte_random.h>
34e64833f2SRemy Horton #include <rte_debug.h>
35e64833f2SRemy Horton #include <rte_ether.h>
36e64833f2SRemy Horton #include <rte_ethdev.h>
37e64833f2SRemy Horton #include <rte_mempool.h>
38e64833f2SRemy Horton #include <rte_mbuf.h>
39e64833f2SRemy Horton #include <rte_timer.h>
40e64833f2SRemy Horton #include <rte_keepalive.h>
41e64833f2SRemy Horton 
427b2a704cSRemy Horton #include "shm.h"
437b2a704cSRemy Horton 
440c2b79e8SLouise Kilheeney #define NB_MBUF_PER_PORT 3000
45e64833f2SRemy Horton 
46e64833f2SRemy Horton #define MAX_PKT_BURST 32
47e64833f2SRemy Horton #define BURST_TX_DRAIN_US 100 /* TX drain every ~100us */
48e64833f2SRemy Horton 
49e64833f2SRemy Horton /*
50e64833f2SRemy Horton  * Configurable number of RX/TX ring descriptors
51e64833f2SRemy Horton  */
524ed89049SDavid Marchand #define RX_DESC_DEFAULT 1024
534ed89049SDavid Marchand #define TX_DESC_DEFAULT 1024
544ed89049SDavid Marchand static uint16_t nb_rxd = RX_DESC_DEFAULT;
554ed89049SDavid Marchand static uint16_t nb_txd = TX_DESC_DEFAULT;
56e64833f2SRemy Horton 
57e64833f2SRemy Horton /* ethernet addresses of ports */
586d13ea8eSOlivier Matz static struct rte_ether_addr l2fwd_ports_eth_addr[RTE_MAX_ETHPORTS];
59e64833f2SRemy Horton 
60e64833f2SRemy Horton /* mask of enabled ports */
61e64833f2SRemy Horton static uint32_t l2fwd_enabled_port_mask;
62e64833f2SRemy Horton 
63e64833f2SRemy Horton /* list of enabled ports */
64e64833f2SRemy Horton static uint32_t l2fwd_dst_ports[RTE_MAX_ETHPORTS];
65e64833f2SRemy Horton 
66e64833f2SRemy Horton static unsigned int l2fwd_rx_queue_per_lcore = 1;
67e64833f2SRemy Horton 
68e64833f2SRemy Horton #define MAX_RX_QUEUE_PER_LCORE 16
69e64833f2SRemy Horton #define MAX_TX_QUEUE_PER_PORT 16
70*7e06c0deSTyler Retzlaff struct __rte_cache_aligned lcore_queue_conf {
71e64833f2SRemy Horton 	unsigned n_rx_port;
72e64833f2SRemy Horton 	unsigned rx_port_list[MAX_RX_QUEUE_PER_LCORE];
73*7e06c0deSTyler Retzlaff };
74e64833f2SRemy Horton struct lcore_queue_conf lcore_queue_conf[RTE_MAX_LCORE];
75e64833f2SRemy Horton 
76e2366e74STomasz Kulasek struct rte_eth_dev_tx_buffer *tx_buffer[RTE_MAX_ETHPORTS];
77e2366e74STomasz Kulasek 
78563e239bSShahaf Shuler static struct rte_eth_conf port_conf = {
79e64833f2SRemy Horton 	.txmode = {
80295968d1SFerruh Yigit 		.mq_mode = RTE_ETH_MQ_TX_NONE,
81e64833f2SRemy Horton 	},
82e64833f2SRemy Horton };
83e64833f2SRemy Horton 
84e64833f2SRemy Horton struct rte_mempool *l2fwd_pktmbuf_pool = NULL;
85e64833f2SRemy Horton 
86e64833f2SRemy Horton /* Per-port statistics struct */
87*7e06c0deSTyler Retzlaff struct __rte_cache_aligned l2fwd_port_statistics {
88e64833f2SRemy Horton 	uint64_t tx;
89e64833f2SRemy Horton 	uint64_t rx;
90e64833f2SRemy Horton 	uint64_t dropped;
91*7e06c0deSTyler Retzlaff };
92e64833f2SRemy Horton struct l2fwd_port_statistics port_statistics[RTE_MAX_ETHPORTS];
93e64833f2SRemy Horton 
94e64833f2SRemy Horton /* A tsc-based timer responsible for triggering statistics printout */
95e64833f2SRemy Horton #define TIMER_MILLISECOND 1
96e64833f2SRemy Horton #define MAX_TIMER_PERIOD 86400 /* 1 day max */
97e64833f2SRemy Horton static int64_t timer_period = 10 * TIMER_MILLISECOND * 1000; /* 10 seconds */
98e64833f2SRemy Horton static int64_t check_period = 5; /* default check cycle is 5ms */
99e64833f2SRemy Horton 
100e64833f2SRemy Horton /* Keepalive structure */
101e64833f2SRemy Horton struct rte_keepalive *rte_global_keepalive_info;
102e64833f2SRemy Horton 
10391e89e47SRemy Horton /* Termination signalling */
10491e89e47SRemy Horton static int terminate_signal_received;
10591e89e47SRemy Horton 
10691e89e47SRemy Horton /* Termination signal handler */
handle_sigterm(__rte_unused int value)10791e89e47SRemy Horton static void handle_sigterm(__rte_unused int value)
10891e89e47SRemy Horton {
10991e89e47SRemy Horton 	terminate_signal_received = 1;
11091e89e47SRemy Horton }
11191e89e47SRemy Horton 
112e64833f2SRemy Horton /* Print out statistics on packets dropped */
113e64833f2SRemy Horton static void
print_stats(__rte_unused struct rte_timer * ptr_timer,__rte_unused void * ptr_data)114f2fc83b4SThomas Monjalon print_stats(__rte_unused struct rte_timer *ptr_timer,
115f2fc83b4SThomas Monjalon 	__rte_unused void *ptr_data)
116e64833f2SRemy Horton {
117e64833f2SRemy Horton 	uint64_t total_packets_dropped, total_packets_tx, total_packets_rx;
118f8244c63SZhiyong Yang 	uint16_t portid;
119e64833f2SRemy Horton 
120e64833f2SRemy Horton 	total_packets_dropped = 0;
121e64833f2SRemy Horton 	total_packets_tx = 0;
122e64833f2SRemy Horton 	total_packets_rx = 0;
123e64833f2SRemy Horton 
124e64833f2SRemy Horton 	const char clr[] = { 27, '[', '2', 'J', '\0' };
125e64833f2SRemy Horton 	const char topLeft[] = { 27, '[', '1', ';', '1', 'H', '\0' };
126e64833f2SRemy Horton 
127e64833f2SRemy Horton 		/* Clear screen and move to top left */
128e64833f2SRemy Horton 	printf("%s%s", clr, topLeft);
129e64833f2SRemy Horton 
130e64833f2SRemy Horton 	printf("\nPort statistics ====================================");
131e64833f2SRemy Horton 
132e64833f2SRemy Horton 	for (portid = 0; portid < RTE_MAX_ETHPORTS; portid++) {
133e64833f2SRemy Horton 		/* skip disabled ports */
134e64833f2SRemy Horton 		if ((l2fwd_enabled_port_mask & (1 << portid)) == 0)
135e64833f2SRemy Horton 			continue;
136e64833f2SRemy Horton 		printf("\nStatistics for port %u ------------------------------"
137e64833f2SRemy Horton 			   "\nPackets sent: %24"PRIu64
138e64833f2SRemy Horton 			   "\nPackets received: %20"PRIu64
139e64833f2SRemy Horton 			   "\nPackets dropped: %21"PRIu64,
140e64833f2SRemy Horton 			   portid,
141e64833f2SRemy Horton 			   port_statistics[portid].tx,
142e64833f2SRemy Horton 			   port_statistics[portid].rx,
143e64833f2SRemy Horton 			   port_statistics[portid].dropped);
144e64833f2SRemy Horton 
145e64833f2SRemy Horton 		total_packets_dropped += port_statistics[portid].dropped;
146e64833f2SRemy Horton 		total_packets_tx += port_statistics[portid].tx;
147e64833f2SRemy Horton 		total_packets_rx += port_statistics[portid].rx;
148e64833f2SRemy Horton 	}
149e64833f2SRemy Horton 	printf("\nAggregate statistics ==============================="
150e64833f2SRemy Horton 		   "\nTotal packets sent: %18"PRIu64
151e64833f2SRemy Horton 		   "\nTotal packets received: %14"PRIu64
152e64833f2SRemy Horton 		   "\nTotal packets dropped: %15"PRIu64,
153e64833f2SRemy Horton 		   total_packets_tx,
154e64833f2SRemy Horton 		   total_packets_rx,
155e64833f2SRemy Horton 		   total_packets_dropped);
156e64833f2SRemy Horton 	printf("\n====================================================\n");
1573ee6f706SGeorgiy Levashov 
1583ee6f706SGeorgiy Levashov 	fflush(stdout);
159e64833f2SRemy Horton }
160e64833f2SRemy Horton 
161e64833f2SRemy Horton static void
l2fwd_simple_forward(struct rte_mbuf * m,unsigned portid)162e64833f2SRemy Horton l2fwd_simple_forward(struct rte_mbuf *m, unsigned portid)
163e64833f2SRemy Horton {
1646d13ea8eSOlivier Matz 	struct rte_ether_hdr *eth;
165e64833f2SRemy Horton 	void *tmp;
166e2366e74STomasz Kulasek 	int sent;
167e64833f2SRemy Horton 	unsigned dst_port;
168e2366e74STomasz Kulasek 	struct rte_eth_dev_tx_buffer *buffer;
169e64833f2SRemy Horton 
170e64833f2SRemy Horton 	dst_port = l2fwd_dst_ports[portid];
1716d13ea8eSOlivier Matz 	eth = rte_pktmbuf_mtod(m, struct rte_ether_hdr *);
172e64833f2SRemy Horton 
173e64833f2SRemy Horton 	/* 02:00:00:00:00:xx */
17404d43857SDmitry Kozlyuk 	tmp = &eth->dst_addr.addr_bytes[0];
175e64833f2SRemy Horton 	*((uint64_t *)tmp) = 0x000000000002 + ((uint64_t)dst_port << 40);
176e64833f2SRemy Horton 
177e64833f2SRemy Horton 	/* src addr */
17804d43857SDmitry Kozlyuk 	rte_ether_addr_copy(&l2fwd_ports_eth_addr[dst_port], &eth->src_addr);
179e64833f2SRemy Horton 
180e2366e74STomasz Kulasek 	buffer = tx_buffer[dst_port];
181e2366e74STomasz Kulasek 	sent = rte_eth_tx_buffer(dst_port, 0, buffer, m);
182e2366e74STomasz Kulasek 	if (sent)
183e2366e74STomasz Kulasek 		port_statistics[dst_port].tx += sent;
184e64833f2SRemy Horton }
185e64833f2SRemy Horton 
186e64833f2SRemy Horton /* main processing loop */
187e64833f2SRemy Horton static void
l2fwd_main_loop(void)188e64833f2SRemy Horton l2fwd_main_loop(void)
189e64833f2SRemy Horton {
190e64833f2SRemy Horton 	struct rte_mbuf *pkts_burst[MAX_PKT_BURST];
191e64833f2SRemy Horton 	struct rte_mbuf *m;
192e2366e74STomasz Kulasek 	int sent;
193e64833f2SRemy Horton 	unsigned lcore_id;
194e64833f2SRemy Horton 	uint64_t prev_tsc, diff_tsc, cur_tsc;
195e64833f2SRemy Horton 	unsigned i, j, portid, nb_rx;
196e64833f2SRemy Horton 	struct lcore_queue_conf *qconf;
197e64833f2SRemy Horton 	const uint64_t drain_tsc = (rte_get_tsc_hz() + US_PER_S - 1)
198e64833f2SRemy Horton 		/ US_PER_S * BURST_TX_DRAIN_US;
199e2366e74STomasz Kulasek 	struct rte_eth_dev_tx_buffer *buffer;
200e64833f2SRemy Horton 
201e64833f2SRemy Horton 	prev_tsc = 0;
202e64833f2SRemy Horton 
203e64833f2SRemy Horton 	lcore_id = rte_lcore_id();
204e64833f2SRemy Horton 	qconf = &lcore_queue_conf[lcore_id];
205e64833f2SRemy Horton 
206e64833f2SRemy Horton 	if (qconf->n_rx_port == 0) {
207e64833f2SRemy Horton 		RTE_LOG(INFO, L2FWD, "lcore %u has nothing to do\n", lcore_id);
208e64833f2SRemy Horton 		return;
209e64833f2SRemy Horton 	}
210e64833f2SRemy Horton 
211e64833f2SRemy Horton 	RTE_LOG(INFO, L2FWD, "entering main loop on lcore %u\n", lcore_id);
212e64833f2SRemy Horton 
213e64833f2SRemy Horton 	for (i = 0; i < qconf->n_rx_port; i++) {
214e64833f2SRemy Horton 
215e64833f2SRemy Horton 		portid = qconf->rx_port_list[i];
216e64833f2SRemy Horton 		RTE_LOG(INFO, L2FWD, " -- lcoreid=%u portid=%u\n", lcore_id,
217e64833f2SRemy Horton 			portid);
218e64833f2SRemy Horton 	}
219e64833f2SRemy Horton 
220e64833f2SRemy Horton 	uint64_t tsc_initial = rte_rdtsc();
221f4495ba2SStephen Hemminger 	uint64_t tsc_lifetime = rte_rand_max(8 * rte_get_tsc_hz());
222e64833f2SRemy Horton 
22391e89e47SRemy Horton 	while (!terminate_signal_received) {
2249a212dc0SConor Fogarty 		/* Keepalive heartbeat. 8< */
225e64833f2SRemy Horton 		rte_keepalive_mark_alive(rte_global_keepalive_info);
226e64833f2SRemy Horton 
227e64833f2SRemy Horton 		cur_tsc = rte_rdtsc();
228e64833f2SRemy Horton 
229e64833f2SRemy Horton 		/*
230e64833f2SRemy Horton 		 * Die randomly within 7 secs for demo purposes if
231e64833f2SRemy Horton 		 * keepalive enabled
232e64833f2SRemy Horton 		 */
233e64833f2SRemy Horton 		if (check_period > 0 && cur_tsc - tsc_initial > tsc_lifetime)
234e64833f2SRemy Horton 			break;
2359a212dc0SConor Fogarty 		/* >8 End of keepalive heartbeat. */
236e64833f2SRemy Horton 
237e64833f2SRemy Horton 		/*
238e64833f2SRemy Horton 		 * TX burst queue drain
239e64833f2SRemy Horton 		 */
240e64833f2SRemy Horton 		diff_tsc = cur_tsc - prev_tsc;
241e64833f2SRemy Horton 		if (unlikely(diff_tsc > drain_tsc)) {
242e64833f2SRemy Horton 
243e2366e74STomasz Kulasek 			for (i = 0; i < qconf->n_rx_port; i++) {
244e2366e74STomasz Kulasek 
245e2366e74STomasz Kulasek 				portid = l2fwd_dst_ports[qconf->rx_port_list[i]];
246e2366e74STomasz Kulasek 				buffer = tx_buffer[portid];
247e2366e74STomasz Kulasek 
248e2366e74STomasz Kulasek 				sent = rte_eth_tx_buffer_flush(portid, 0, buffer);
249e2366e74STomasz Kulasek 				if (sent)
250e2366e74STomasz Kulasek 					port_statistics[portid].tx += sent;
251e2366e74STomasz Kulasek 
252e64833f2SRemy Horton 			}
253e64833f2SRemy Horton 
254e64833f2SRemy Horton 			prev_tsc = cur_tsc;
255e64833f2SRemy Horton 		}
256e64833f2SRemy Horton 
257e64833f2SRemy Horton 		/*
258e64833f2SRemy Horton 		 * Read packet from RX queues
259e64833f2SRemy Horton 		 */
260e64833f2SRemy Horton 		for (i = 0; i < qconf->n_rx_port; i++) {
261e64833f2SRemy Horton 
262e64833f2SRemy Horton 			portid = qconf->rx_port_list[i];
263f8244c63SZhiyong Yang 			nb_rx = rte_eth_rx_burst(portid, 0,
264e64833f2SRemy Horton 						 pkts_burst, MAX_PKT_BURST);
265e64833f2SRemy Horton 
266e64833f2SRemy Horton 			port_statistics[portid].rx += nb_rx;
267e64833f2SRemy Horton 
268e64833f2SRemy Horton 			for (j = 0; j < nb_rx; j++) {
269e64833f2SRemy Horton 				m = pkts_burst[j];
270e64833f2SRemy Horton 				rte_prefetch0(rte_pktmbuf_mtod(m, void *));
271e64833f2SRemy Horton 				l2fwd_simple_forward(m, portid);
272e64833f2SRemy Horton 			}
273e64833f2SRemy Horton 		}
274e64833f2SRemy Horton 	}
275e64833f2SRemy Horton }
276e64833f2SRemy Horton 
277e64833f2SRemy Horton static int
l2fwd_launch_one_lcore(__rte_unused void * dummy)278f2fc83b4SThomas Monjalon l2fwd_launch_one_lcore(__rte_unused void *dummy)
279e64833f2SRemy Horton {
280e64833f2SRemy Horton 	l2fwd_main_loop();
281e64833f2SRemy Horton 	return 0;
282e64833f2SRemy Horton }
283e64833f2SRemy Horton 
284e64833f2SRemy Horton /* display usage */
285e64833f2SRemy Horton static void
l2fwd_usage(const char * prgname)286e64833f2SRemy Horton l2fwd_usage(const char *prgname)
287e64833f2SRemy Horton {
288e64833f2SRemy Horton 	printf("%s [EAL options] -- -p PORTMASK [-q NQ]\n"
289e64833f2SRemy Horton 	       "  -p PORTMASK: hexadecimal bitmask of ports to configure\n"
290e64833f2SRemy Horton 	       "  -q NQ: number of queue (=ports) per lcore (default is 1)\n"
291e64833f2SRemy Horton 	       "  -K PERIOD: Keepalive check period (5 default; 86400 max)\n"
292e64833f2SRemy Horton 		   "  -T PERIOD: statistics will be refreshed each PERIOD seconds (0 to disable, 10 default, 86400 maximum)\n",
293e64833f2SRemy Horton 	       prgname);
294e64833f2SRemy Horton }
295e64833f2SRemy Horton 
296e64833f2SRemy Horton static int
l2fwd_parse_portmask(const char * portmask)297e64833f2SRemy Horton l2fwd_parse_portmask(const char *portmask)
298e64833f2SRemy Horton {
299e64833f2SRemy Horton 	char *end = NULL;
300e64833f2SRemy Horton 	unsigned long pm;
301e64833f2SRemy Horton 
302e64833f2SRemy Horton 	/* parse hexadecimal string */
303e64833f2SRemy Horton 	pm = strtoul(portmask, &end, 16);
304e64833f2SRemy Horton 	if ((portmask[0] == '\0') || (end == NULL) || (*end != '\0'))
305ce6b8c31SSarosh Arif 		return 0;
306e64833f2SRemy Horton 
307e64833f2SRemy Horton 	return pm;
308e64833f2SRemy Horton }
309e64833f2SRemy Horton 
310e64833f2SRemy Horton static unsigned int
l2fwd_parse_nqueue(const char * q_arg)311e64833f2SRemy Horton l2fwd_parse_nqueue(const char *q_arg)
312e64833f2SRemy Horton {
313e64833f2SRemy Horton 	char *end = NULL;
314e64833f2SRemy Horton 	unsigned long n;
315e64833f2SRemy Horton 
316e64833f2SRemy Horton 	/* parse hexadecimal string */
317e64833f2SRemy Horton 	n = strtoul(q_arg, &end, 10);
318e64833f2SRemy Horton 	if ((q_arg[0] == '\0') || (end == NULL) || (*end != '\0'))
319e64833f2SRemy Horton 		return 0;
320e64833f2SRemy Horton 	if (n == 0)
321e64833f2SRemy Horton 		return 0;
322e64833f2SRemy Horton 	if (n >= MAX_RX_QUEUE_PER_LCORE)
323e64833f2SRemy Horton 		return 0;
324e64833f2SRemy Horton 
325e64833f2SRemy Horton 	return n;
326e64833f2SRemy Horton }
327e64833f2SRemy Horton 
328e64833f2SRemy Horton static int
l2fwd_parse_timer_period(const char * q_arg)329e64833f2SRemy Horton l2fwd_parse_timer_period(const char *q_arg)
330e64833f2SRemy Horton {
331e64833f2SRemy Horton 	char *end = NULL;
332e64833f2SRemy Horton 	int n;
333e64833f2SRemy Horton 
334e64833f2SRemy Horton 	/* parse number string */
335e64833f2SRemy Horton 	n = strtol(q_arg, &end, 10);
336e64833f2SRemy Horton 	if ((q_arg[0] == '\0') || (end == NULL) || (*end != '\0'))
337e64833f2SRemy Horton 		return -1;
338e64833f2SRemy Horton 	if (n >= MAX_TIMER_PERIOD)
339e64833f2SRemy Horton 		return -1;
340e64833f2SRemy Horton 
341e64833f2SRemy Horton 	return n;
342e64833f2SRemy Horton }
343e64833f2SRemy Horton 
344e64833f2SRemy Horton static int
l2fwd_parse_check_period(const char * q_arg)345e64833f2SRemy Horton l2fwd_parse_check_period(const char *q_arg)
346e64833f2SRemy Horton {
347e64833f2SRemy Horton 	char *end = NULL;
348e64833f2SRemy Horton 	int n;
349e64833f2SRemy Horton 
350e64833f2SRemy Horton 	/* parse number string */
351e64833f2SRemy Horton 	n = strtol(q_arg, &end, 10);
352e64833f2SRemy Horton 	if ((q_arg[0] == '\0') || (end == NULL) || (*end != '\0'))
353e64833f2SRemy Horton 		return -1;
354e64833f2SRemy Horton 	if (n >= MAX_TIMER_PERIOD)
355e64833f2SRemy Horton 		return -1;
356e64833f2SRemy Horton 
357e64833f2SRemy Horton 	return n;
358e64833f2SRemy Horton }
359e64833f2SRemy Horton 
360e64833f2SRemy Horton /* Parse the argument given in the command line of the application */
361e64833f2SRemy Horton static int
l2fwd_parse_args(int argc,char ** argv)362e64833f2SRemy Horton l2fwd_parse_args(int argc, char **argv)
363e64833f2SRemy Horton {
364e64833f2SRemy Horton 	int opt, ret;
365e64833f2SRemy Horton 	char **argvopt;
366e64833f2SRemy Horton 	int option_index;
367e64833f2SRemy Horton 	char *prgname = argv[0];
368e64833f2SRemy Horton 	static struct option lgopts[] = {
369e64833f2SRemy Horton 		{NULL, 0, 0, 0}
370e64833f2SRemy Horton 	};
371e64833f2SRemy Horton 
372e64833f2SRemy Horton 	argvopt = argv;
373e64833f2SRemy Horton 
374e64833f2SRemy Horton 	while ((opt = getopt_long(argc, argvopt, "p:q:T:K:",
375e64833f2SRemy Horton 				  lgopts, &option_index)) != EOF) {
376e64833f2SRemy Horton 
377e64833f2SRemy Horton 		switch (opt) {
378e64833f2SRemy Horton 		/* portmask */
379e64833f2SRemy Horton 		case 'p':
380e64833f2SRemy Horton 			l2fwd_enabled_port_mask = l2fwd_parse_portmask(optarg);
381e64833f2SRemy Horton 			if (l2fwd_enabled_port_mask == 0) {
382e64833f2SRemy Horton 				printf("invalid portmask\n");
383e64833f2SRemy Horton 				l2fwd_usage(prgname);
384e64833f2SRemy Horton 				return -1;
385e64833f2SRemy Horton 			}
386e64833f2SRemy Horton 			break;
387e64833f2SRemy Horton 
388e64833f2SRemy Horton 		/* nqueue */
389e64833f2SRemy Horton 		case 'q':
390e64833f2SRemy Horton 			l2fwd_rx_queue_per_lcore = l2fwd_parse_nqueue(optarg);
391e64833f2SRemy Horton 			if (l2fwd_rx_queue_per_lcore == 0) {
392e64833f2SRemy Horton 				printf("invalid queue number\n");
393e64833f2SRemy Horton 				l2fwd_usage(prgname);
394e64833f2SRemy Horton 				return -1;
395e64833f2SRemy Horton 			}
396e64833f2SRemy Horton 			break;
397e64833f2SRemy Horton 
398e64833f2SRemy Horton 		/* timer period */
399e64833f2SRemy Horton 		case 'T':
400e64833f2SRemy Horton 			timer_period = l2fwd_parse_timer_period(optarg)
40183c27f59SRemy Horton 				* (int64_t)(1000 * TIMER_MILLISECOND);
402e64833f2SRemy Horton 			if (timer_period < 0) {
403e64833f2SRemy Horton 				printf("invalid timer period\n");
404e64833f2SRemy Horton 				l2fwd_usage(prgname);
405e64833f2SRemy Horton 				return -1;
406e64833f2SRemy Horton 			}
407e64833f2SRemy Horton 			break;
408e64833f2SRemy Horton 
409e64833f2SRemy Horton 		/* Check period */
410e64833f2SRemy Horton 		case 'K':
411e64833f2SRemy Horton 			check_period = l2fwd_parse_check_period(optarg);
412e64833f2SRemy Horton 			if (check_period < 0) {
413e64833f2SRemy Horton 				printf("invalid check period\n");
414e64833f2SRemy Horton 				l2fwd_usage(prgname);
415e64833f2SRemy Horton 				return -1;
416e64833f2SRemy Horton 			}
417e64833f2SRemy Horton 			break;
418e64833f2SRemy Horton 
419e64833f2SRemy Horton 		/* long options */
420e64833f2SRemy Horton 		case 0:
421e64833f2SRemy Horton 			l2fwd_usage(prgname);
422e64833f2SRemy Horton 			return -1;
423e64833f2SRemy Horton 
424e64833f2SRemy Horton 		default:
425e64833f2SRemy Horton 			l2fwd_usage(prgname);
426e64833f2SRemy Horton 			return -1;
427e64833f2SRemy Horton 		}
428e64833f2SRemy Horton 	}
429e64833f2SRemy Horton 
430e64833f2SRemy Horton 	if (optind >= 0)
431e64833f2SRemy Horton 		argv[optind-1] = prgname;
432e64833f2SRemy Horton 
433e64833f2SRemy Horton 	ret = optind-1;
4349d5ca532SKeith Wiles 	optind = 1; /* reset getopt lib */
435e64833f2SRemy Horton 	return ret;
436e64833f2SRemy Horton }
437e64833f2SRemy Horton 
438e64833f2SRemy Horton /* Check the link status of all ports in up to 9s, and print them finally */
439e64833f2SRemy Horton static void
check_all_ports_link_status(uint32_t port_mask)4408728ccf3SThomas Monjalon check_all_ports_link_status(uint32_t port_mask)
441e64833f2SRemy Horton {
442e64833f2SRemy Horton #define CHECK_INTERVAL 100 /* 100ms */
443e64833f2SRemy Horton #define MAX_CHECK_TIME 90 /* 9s (90 * 100ms) in total */
444f8244c63SZhiyong Yang 	uint16_t portid;
445f8244c63SZhiyong Yang 	uint8_t count, all_ports_up, print_flag = 0;
446e64833f2SRemy Horton 	struct rte_eth_link link;
44722e5c73bSIgor Romanov 	int ret;
448db4e8135SIvan Dyukov 	char link_status_text[RTE_ETH_LINK_MAX_STR_LEN];
449e64833f2SRemy Horton 
450e64833f2SRemy Horton 	printf("\nChecking link status");
451e64833f2SRemy Horton 	fflush(stdout);
452e64833f2SRemy Horton 	for (count = 0; count <= MAX_CHECK_TIME; count++) {
453e64833f2SRemy Horton 		all_ports_up = 1;
4548728ccf3SThomas Monjalon 		RTE_ETH_FOREACH_DEV(portid) {
455e64833f2SRemy Horton 			if ((port_mask & (1 << portid)) == 0)
456e64833f2SRemy Horton 				continue;
457e64833f2SRemy Horton 			memset(&link, 0, sizeof(link));
45822e5c73bSIgor Romanov 			ret = rte_eth_link_get_nowait(portid, &link);
45922e5c73bSIgor Romanov 			if (ret < 0) {
46022e5c73bSIgor Romanov 				all_ports_up = 0;
46122e5c73bSIgor Romanov 				if (print_flag == 1)
46222e5c73bSIgor Romanov 					printf("Port %u link get failed: %s\n",
46322e5c73bSIgor Romanov 						portid, rte_strerror(-ret));
46422e5c73bSIgor Romanov 				continue;
46522e5c73bSIgor Romanov 			}
466e64833f2SRemy Horton 			/* print link status if flag set */
467e64833f2SRemy Horton 			if (print_flag == 1) {
468db4e8135SIvan Dyukov 				rte_eth_link_to_str(link_status_text,
469db4e8135SIvan Dyukov 					sizeof(link_status_text), &link);
470db4e8135SIvan Dyukov 				printf("Port %d %s\n", portid,
471db4e8135SIvan Dyukov 				       link_status_text);
472e64833f2SRemy Horton 				continue;
473e64833f2SRemy Horton 			}
474e64833f2SRemy Horton 			/* clear all_ports_up flag if any link down */
475295968d1SFerruh Yigit 			if (link.link_status == RTE_ETH_LINK_DOWN) {
476e64833f2SRemy Horton 				all_ports_up = 0;
477e64833f2SRemy Horton 				break;
478e64833f2SRemy Horton 			}
479e64833f2SRemy Horton 		}
480e64833f2SRemy Horton 		/* after finally printing all link status, get out */
481e64833f2SRemy Horton 		if (print_flag == 1)
482e64833f2SRemy Horton 			break;
483e64833f2SRemy Horton 
484e64833f2SRemy Horton 		if (all_ports_up == 0) {
485e64833f2SRemy Horton 			printf(".");
486e64833f2SRemy Horton 			fflush(stdout);
487e64833f2SRemy Horton 			rte_delay_ms(CHECK_INTERVAL);
488e64833f2SRemy Horton 		}
489e64833f2SRemy Horton 
490e64833f2SRemy Horton 		/* set the print_flag if all ports up or timeout */
491e64833f2SRemy Horton 		if (all_ports_up == 1 || count == (MAX_CHECK_TIME - 1)) {
492e64833f2SRemy Horton 			print_flag = 1;
493e64833f2SRemy Horton 			printf("done\n");
494e64833f2SRemy Horton 		}
495e64833f2SRemy Horton 	}
496e64833f2SRemy Horton }
497e64833f2SRemy Horton 
498e64833f2SRemy Horton static void
dead_core(__rte_unused void * ptr_data,const int id_core)4997b2a704cSRemy Horton dead_core(__rte_unused void *ptr_data, const int id_core)
500e64833f2SRemy Horton {
50191e89e47SRemy Horton 	if (terminate_signal_received)
50291e89e47SRemy Horton 		return;
503e64833f2SRemy Horton 	printf("Dead core %i - restarting..\n", id_core);
504f6c6c686SHonnappa Nagarahalli 	if (rte_eal_get_lcore_state(id_core) == WAIT) {
505e64833f2SRemy Horton 		rte_eal_remote_launch(l2fwd_launch_one_lcore, NULL, id_core);
506e64833f2SRemy Horton 	} else {
507e64833f2SRemy Horton 		printf("..false positive!\n");
508e64833f2SRemy Horton 	}
509e64833f2SRemy Horton }
510e64833f2SRemy Horton 
5117b2a704cSRemy Horton static void
relay_core_state(void * ptr_data,const int id_core,const enum rte_keepalive_state core_state,uint64_t last_alive)5127b2a704cSRemy Horton relay_core_state(void *ptr_data, const int id_core,
5137b2a704cSRemy Horton 	const enum rte_keepalive_state core_state, uint64_t last_alive)
5147b2a704cSRemy Horton {
5157b2a704cSRemy Horton 	rte_keepalive_relayed_state((struct rte_keepalive_shm *)ptr_data,
5167b2a704cSRemy Horton 		id_core, core_state, last_alive);
5177b2a704cSRemy Horton }
5187b2a704cSRemy Horton 
519e64833f2SRemy Horton int
main(int argc,char ** argv)520e64833f2SRemy Horton main(int argc, char **argv)
521e64833f2SRemy Horton {
522e64833f2SRemy Horton 	struct lcore_queue_conf *qconf;
523e64833f2SRemy Horton 	int ret;
524f8244c63SZhiyong Yang 	uint16_t nb_ports;
5258728ccf3SThomas Monjalon 	uint16_t nb_ports_available = 0;
526f8244c63SZhiyong Yang 	uint16_t portid, last_port;
527e64833f2SRemy Horton 	unsigned lcore_id, rx_lcore_id;
528e64833f2SRemy Horton 	unsigned nb_ports_in_mask = 0;
5290c2b79e8SLouise Kilheeney 	unsigned int total_nb_mbufs;
53091e89e47SRemy Horton 	struct sigaction signal_handler;
53193543923SRemy Horton 	struct rte_keepalive_shm *ka_shm;
53291e89e47SRemy Horton 
53391e89e47SRemy Horton 	memset(&signal_handler, 0, sizeof(signal_handler));
53491e89e47SRemy Horton 	terminate_signal_received = 0;
53591e89e47SRemy Horton 	signal_handler.sa_handler = &handle_sigterm;
53691e89e47SRemy Horton 	if (sigaction(SIGINT, &signal_handler, NULL) == -1 ||
53791e89e47SRemy Horton 			sigaction(SIGTERM, &signal_handler, NULL) == -1)
53891e89e47SRemy Horton 		rte_exit(EXIT_FAILURE, "SIGNAL\n");
53991e89e47SRemy Horton 
540e64833f2SRemy Horton 
541e64833f2SRemy Horton 	/* init EAL */
542e64833f2SRemy Horton 	ret = rte_eal_init(argc, argv);
543e64833f2SRemy Horton 	if (ret < 0)
544e64833f2SRemy Horton 		rte_exit(EXIT_FAILURE, "Invalid EAL arguments\n");
545e64833f2SRemy Horton 	argc -= ret;
546e64833f2SRemy Horton 	argv += ret;
547e64833f2SRemy Horton 
548e64833f2SRemy Horton 	l2fwd_enabled_port_mask = 0;
549e64833f2SRemy Horton 
550e64833f2SRemy Horton 	/* parse application arguments (after the EAL ones) */
551e64833f2SRemy Horton 	ret = l2fwd_parse_args(argc, argv);
552e64833f2SRemy Horton 	if (ret < 0)
553e64833f2SRemy Horton 		rte_exit(EXIT_FAILURE, "Invalid L2FWD arguments\n");
554e64833f2SRemy Horton 
555d9a42a69SThomas Monjalon 	nb_ports = rte_eth_dev_count_avail();
556e64833f2SRemy Horton 	if (nb_ports == 0)
557e64833f2SRemy Horton 		rte_exit(EXIT_FAILURE, "No Ethernet ports - bye\n");
558e64833f2SRemy Horton 
5590c2b79e8SLouise Kilheeney 	/* create the mbuf pool */
5600c2b79e8SLouise Kilheeney 	total_nb_mbufs = NB_MBUF_PER_PORT * nb_ports;
5610c2b79e8SLouise Kilheeney 
5620c2b79e8SLouise Kilheeney 	l2fwd_pktmbuf_pool = rte_pktmbuf_pool_create("mbuf_pool",
5630c2b79e8SLouise Kilheeney 		total_nb_mbufs,	32, 0, RTE_MBUF_DEFAULT_BUF_SIZE,
5640c2b79e8SLouise Kilheeney 		rte_socket_id());
5650c2b79e8SLouise Kilheeney 	if (l2fwd_pktmbuf_pool == NULL)
5660c2b79e8SLouise Kilheeney 		rte_exit(EXIT_FAILURE, "Cannot init mbuf pool\n");
5670c2b79e8SLouise Kilheeney 
568e64833f2SRemy Horton 	/* reset l2fwd_dst_ports */
569e64833f2SRemy Horton 	for (portid = 0; portid < RTE_MAX_ETHPORTS; portid++)
570e64833f2SRemy Horton 		l2fwd_dst_ports[portid] = 0;
571e64833f2SRemy Horton 	last_port = 0;
572e64833f2SRemy Horton 
573e64833f2SRemy Horton 	/*
574e64833f2SRemy Horton 	 * Each logical core is assigned a dedicated TX queue on each port.
575e64833f2SRemy Horton 	 */
5768728ccf3SThomas Monjalon 	RTE_ETH_FOREACH_DEV(portid) {
577e64833f2SRemy Horton 		/* skip ports that are not enabled */
578e64833f2SRemy Horton 		if ((l2fwd_enabled_port_mask & (1 << portid)) == 0)
579e64833f2SRemy Horton 			continue;
580e64833f2SRemy Horton 
581e64833f2SRemy Horton 		if (nb_ports_in_mask % 2) {
582e64833f2SRemy Horton 			l2fwd_dst_ports[portid] = last_port;
583e64833f2SRemy Horton 			l2fwd_dst_ports[last_port] = portid;
584e64833f2SRemy Horton 		} else
585e64833f2SRemy Horton 			last_port = portid;
586e64833f2SRemy Horton 
587e64833f2SRemy Horton 		nb_ports_in_mask++;
588e64833f2SRemy Horton 	}
589e64833f2SRemy Horton 	if (nb_ports_in_mask % 2) {
590e64833f2SRemy Horton 		printf("Notice: odd number of ports in portmask.\n");
591e64833f2SRemy Horton 		l2fwd_dst_ports[last_port] = last_port;
592e64833f2SRemy Horton 	}
593e64833f2SRemy Horton 
594e64833f2SRemy Horton 	rx_lcore_id = 1;
595e64833f2SRemy Horton 	qconf = NULL;
596e64833f2SRemy Horton 
597e64833f2SRemy Horton 	/* Initialize the port/queue configuration of each logical core */
5988728ccf3SThomas Monjalon 	RTE_ETH_FOREACH_DEV(portid) {
599e64833f2SRemy Horton 		/* skip ports that are not enabled */
600e64833f2SRemy Horton 		if ((l2fwd_enabled_port_mask & (1 << portid)) == 0)
601e64833f2SRemy Horton 			continue;
602e64833f2SRemy Horton 
603e64833f2SRemy Horton 		/* get the lcore_id for this port */
604e64833f2SRemy Horton 		while (rte_lcore_is_enabled(rx_lcore_id) == 0 ||
605e64833f2SRemy Horton 		       lcore_queue_conf[rx_lcore_id].n_rx_port ==
606e64833f2SRemy Horton 		       l2fwd_rx_queue_per_lcore) {
607e64833f2SRemy Horton 			rx_lcore_id++;
608e64833f2SRemy Horton 			if (rx_lcore_id >= RTE_MAX_LCORE)
609e64833f2SRemy Horton 				rte_exit(EXIT_FAILURE, "Not enough cores\n");
610e64833f2SRemy Horton 		}
611e64833f2SRemy Horton 
612e64833f2SRemy Horton 		if (qconf != &lcore_queue_conf[rx_lcore_id])
613e64833f2SRemy Horton 			/* Assigned a new logical core in the loop above. */
614e64833f2SRemy Horton 			qconf = &lcore_queue_conf[rx_lcore_id];
615e64833f2SRemy Horton 
616e64833f2SRemy Horton 		qconf->rx_port_list[qconf->n_rx_port] = portid;
617e64833f2SRemy Horton 		qconf->n_rx_port++;
618e64833f2SRemy Horton 		printf("Lcore %u: RX port %u\n",
619f8244c63SZhiyong Yang 			rx_lcore_id, portid);
620e64833f2SRemy Horton 	}
621e64833f2SRemy Horton 
622e64833f2SRemy Horton 	/* Initialise each port */
6238728ccf3SThomas Monjalon 	RTE_ETH_FOREACH_DEV(portid) {
624563e239bSShahaf Shuler 		struct rte_eth_dev_info dev_info;
625563e239bSShahaf Shuler 		struct rte_eth_rxconf rxq_conf;
626563e239bSShahaf Shuler 		struct rte_eth_txconf txq_conf;
627563e239bSShahaf Shuler 		struct rte_eth_conf local_port_conf = port_conf;
628563e239bSShahaf Shuler 
629e64833f2SRemy Horton 		/* skip ports that are not enabled */
630e64833f2SRemy Horton 		if ((l2fwd_enabled_port_mask & (1 << portid)) == 0) {
631f8244c63SZhiyong Yang 			printf("Skipping disabled port %u\n", portid);
632e64833f2SRemy Horton 			continue;
633e64833f2SRemy Horton 		}
6348728ccf3SThomas Monjalon 		nb_ports_available++;
6358728ccf3SThomas Monjalon 
636e64833f2SRemy Horton 		/* init port */
637f8244c63SZhiyong Yang 		printf("Initializing port %u... ", portid);
638e64833f2SRemy Horton 		fflush(stdout);
639089e5ed7SIvan Ilchenko 
640089e5ed7SIvan Ilchenko 		ret = rte_eth_dev_info_get(portid, &dev_info);
641089e5ed7SIvan Ilchenko 		if (ret != 0)
642089e5ed7SIvan Ilchenko 			rte_exit(EXIT_FAILURE,
643089e5ed7SIvan Ilchenko 				"Error during getting device (port %u) info: %s\n",
644089e5ed7SIvan Ilchenko 				portid, strerror(-ret));
645089e5ed7SIvan Ilchenko 
646295968d1SFerruh Yigit 		if (dev_info.tx_offload_capa & RTE_ETH_TX_OFFLOAD_MBUF_FAST_FREE)
647563e239bSShahaf Shuler 			local_port_conf.txmode.offloads |=
648295968d1SFerruh Yigit 				RTE_ETH_TX_OFFLOAD_MBUF_FAST_FREE;
649563e239bSShahaf Shuler 		ret = rte_eth_dev_configure(portid, 1, 1, &local_port_conf);
650e64833f2SRemy Horton 		if (ret < 0)
651e64833f2SRemy Horton 			rte_exit(EXIT_FAILURE,
652e64833f2SRemy Horton 				"Cannot configure device: err=%d, port=%u\n",
653f8244c63SZhiyong Yang 				ret, portid);
654e64833f2SRemy Horton 
65560efb44fSRoman Zhukov 		ret = rte_eth_dev_adjust_nb_rx_tx_desc(portid, &nb_rxd,
65660efb44fSRoman Zhukov 						       &nb_txd);
65760efb44fSRoman Zhukov 		if (ret < 0)
65860efb44fSRoman Zhukov 			rte_exit(EXIT_FAILURE,
65960efb44fSRoman Zhukov 				"Cannot adjust number of descriptors: err=%d, port=%u\n",
660f8244c63SZhiyong Yang 				ret, portid);
66160efb44fSRoman Zhukov 
66270febdcfSIgor Romanov 		ret = rte_eth_macaddr_get(portid,
66370febdcfSIgor Romanov 					  &l2fwd_ports_eth_addr[portid]);
66470febdcfSIgor Romanov 		if (ret < 0)
66570febdcfSIgor Romanov 			rte_exit(EXIT_FAILURE,
66670febdcfSIgor Romanov 				"Cannot mac address: err=%d, port=%u\n",
66770febdcfSIgor Romanov 				ret, portid);
668e64833f2SRemy Horton 
669e64833f2SRemy Horton 		/* init one RX queue */
670e64833f2SRemy Horton 		fflush(stdout);
671563e239bSShahaf Shuler 		rxq_conf = dev_info.default_rxconf;
672563e239bSShahaf Shuler 		rxq_conf.offloads = local_port_conf.rxmode.offloads;
673e64833f2SRemy Horton 		ret = rte_eth_rx_queue_setup(portid, 0, nb_rxd,
674e64833f2SRemy Horton 					     rte_eth_dev_socket_id(portid),
675563e239bSShahaf Shuler 					     &rxq_conf,
676e64833f2SRemy Horton 					     l2fwd_pktmbuf_pool);
677e64833f2SRemy Horton 		if (ret < 0)
678e64833f2SRemy Horton 			rte_exit(EXIT_FAILURE,
679e64833f2SRemy Horton 				"rte_eth_rx_queue_setup:err=%d, port=%u\n",
680f8244c63SZhiyong Yang 				ret, portid);
681e64833f2SRemy Horton 
682e64833f2SRemy Horton 		/* init one TX queue on each port */
683e64833f2SRemy Horton 		fflush(stdout);
684563e239bSShahaf Shuler 		txq_conf = dev_info.default_txconf;
685563e239bSShahaf Shuler 		txq_conf.offloads = local_port_conf.txmode.offloads;
686e64833f2SRemy Horton 		ret = rte_eth_tx_queue_setup(portid, 0, nb_txd,
687e64833f2SRemy Horton 				rte_eth_dev_socket_id(portid),
688563e239bSShahaf Shuler 				&txq_conf);
689e64833f2SRemy Horton 		if (ret < 0)
690e64833f2SRemy Horton 			rte_exit(EXIT_FAILURE,
691e64833f2SRemy Horton 				"rte_eth_tx_queue_setup:err=%d, port=%u\n",
692f8244c63SZhiyong Yang 				ret, portid);
693e64833f2SRemy Horton 
694e2366e74STomasz Kulasek 		/* Initialize TX buffers */
695e2366e74STomasz Kulasek 		tx_buffer[portid] = rte_zmalloc_socket("tx_buffer",
696e2366e74STomasz Kulasek 				RTE_ETH_TX_BUFFER_SIZE(MAX_PKT_BURST), 0,
697e2366e74STomasz Kulasek 				rte_eth_dev_socket_id(portid));
698e2366e74STomasz Kulasek 		if (tx_buffer[portid] == NULL)
699e2366e74STomasz Kulasek 			rte_exit(EXIT_FAILURE, "Cannot allocate buffer for tx on port %u\n",
700f8244c63SZhiyong Yang 						portid);
701e2366e74STomasz Kulasek 
702e2366e74STomasz Kulasek 		rte_eth_tx_buffer_init(tx_buffer[portid], MAX_PKT_BURST);
703e2366e74STomasz Kulasek 
704e2366e74STomasz Kulasek 		ret = rte_eth_tx_buffer_set_err_callback(tx_buffer[portid],
705e2366e74STomasz Kulasek 				rte_eth_tx_buffer_count_callback,
706e2366e74STomasz Kulasek 				&port_statistics[portid].dropped);
707e2366e74STomasz Kulasek 		if (ret < 0)
708f8244c63SZhiyong Yang 			rte_exit(EXIT_FAILURE,
709f8244c63SZhiyong Yang 			"Cannot set error callback for tx buffer on port %u\n",
710f8244c63SZhiyong Yang 				 portid);
711e2366e74STomasz Kulasek 
712e64833f2SRemy Horton 		/* Start device */
713e64833f2SRemy Horton 		ret = rte_eth_dev_start(portid);
714e64833f2SRemy Horton 		if (ret < 0)
715e64833f2SRemy Horton 			rte_exit(EXIT_FAILURE,
716e64833f2SRemy Horton 				"rte_eth_dev_start:err=%d, port=%u\n",
717f8244c63SZhiyong Yang 				  ret, portid);
718e64833f2SRemy Horton 
719f430bbceSIvan Ilchenko 		ret = rte_eth_promiscuous_enable(portid);
720f430bbceSIvan Ilchenko 		if (ret != 0)
721f430bbceSIvan Ilchenko 			rte_exit(EXIT_FAILURE,
722f430bbceSIvan Ilchenko 				 "rte_eth_promiscuous_enable:err=%s, port=%u\n",
723f430bbceSIvan Ilchenko 				 rte_strerror(-ret), portid);
724e64833f2SRemy Horton 
725e64833f2SRemy Horton 		printf("Port %u, MAC address: "
726c2c4f87bSAman Deep Singh 			RTE_ETHER_ADDR_PRT_FMT "\n\n",
727f8244c63SZhiyong Yang 			portid,
728a7db3afcSAman Deep Singh 			RTE_ETHER_ADDR_BYTES(&l2fwd_ports_eth_addr[portid]));
729e64833f2SRemy Horton 
730e64833f2SRemy Horton 		/* initialize port stats */
731e64833f2SRemy Horton 		memset(&port_statistics, 0, sizeof(port_statistics));
732e64833f2SRemy Horton 	}
733e64833f2SRemy Horton 
734e64833f2SRemy Horton 	if (!nb_ports_available) {
735e64833f2SRemy Horton 		rte_exit(EXIT_FAILURE,
736e64833f2SRemy Horton 			"All available ports are disabled. Please set portmask.\n");
737e64833f2SRemy Horton 	}
738e64833f2SRemy Horton 
7398728ccf3SThomas Monjalon 	check_all_ports_link_status(l2fwd_enabled_port_mask);
740e64833f2SRemy Horton 
741e64833f2SRemy Horton 	struct rte_timer hb_timer, stats_timer;
742e64833f2SRemy Horton 
743e64833f2SRemy Horton 	rte_timer_subsystem_init();
744e64833f2SRemy Horton 	rte_timer_init(&stats_timer);
745e64833f2SRemy Horton 
74693543923SRemy Horton 	ka_shm = NULL;
747e64833f2SRemy Horton 	if (check_period > 0) {
7487b2a704cSRemy Horton 		ka_shm = rte_keepalive_shm_create();
7497b2a704cSRemy Horton 		if (ka_shm == NULL)
7507b2a704cSRemy Horton 			rte_exit(EXIT_FAILURE,
7517b2a704cSRemy Horton 				"rte_keepalive_shm_create() failed");
7529a212dc0SConor Fogarty 		/* Initialize keepalive functionality. 8< */
753e64833f2SRemy Horton 		rte_global_keepalive_info =
7547b2a704cSRemy Horton 			rte_keepalive_create(&dead_core, ka_shm);
755e64833f2SRemy Horton 		if (rte_global_keepalive_info == NULL)
756e64833f2SRemy Horton 			rte_exit(EXIT_FAILURE, "init_keep_alive() failed");
7579a212dc0SConor Fogarty 		/* >8 End of initializing keepalive functionality. */
7587b2a704cSRemy Horton 		rte_keepalive_register_relay_callback(rte_global_keepalive_info,
7597b2a704cSRemy Horton 			relay_core_state, ka_shm);
760e64833f2SRemy Horton 		rte_timer_init(&hb_timer);
761e64833f2SRemy Horton 		if (rte_timer_reset(&hb_timer,
762e64833f2SRemy Horton 				(check_period * rte_get_timer_hz()) / 1000,
763e64833f2SRemy Horton 				PERIODICAL,
764e64833f2SRemy Horton 				rte_lcore_id(),
765e64833f2SRemy Horton 				(void(*)(struct rte_timer*, void*))
766e64833f2SRemy Horton 				&rte_keepalive_dispatch_pings,
767e64833f2SRemy Horton 				rte_global_keepalive_info
768e64833f2SRemy Horton 				) != 0 )
769e64833f2SRemy Horton 			rte_exit(EXIT_FAILURE, "Keepalive setup failure.\n");
770e64833f2SRemy Horton 	}
771e64833f2SRemy Horton 	if (timer_period > 0) {
7729a212dc0SConor Fogarty 		/* Issues the pings keepalive_dispatch_pings(). 8< */
773e64833f2SRemy Horton 		if (rte_timer_reset(&stats_timer,
774e64833f2SRemy Horton 				(timer_period * rte_get_timer_hz()) / 1000,
775e64833f2SRemy Horton 				PERIODICAL,
776e64833f2SRemy Horton 				rte_lcore_id(),
777e64833f2SRemy Horton 				&print_stats, NULL
778e64833f2SRemy Horton 				) != 0 )
779e64833f2SRemy Horton 			rte_exit(EXIT_FAILURE, "Stats setup failure.\n");
7809a212dc0SConor Fogarty 		/* >8 End of issuing the pings keepalive_dispatch_pings(). */
781e64833f2SRemy Horton 	}
782cb056611SStephen Hemminger 	/* launch per-lcore init on every worker lcore */
783cb056611SStephen Hemminger 	RTE_LCORE_FOREACH_WORKER(lcore_id) {
784e64833f2SRemy Horton 		struct lcore_queue_conf *qconf = &lcore_queue_conf[lcore_id];
785e64833f2SRemy Horton 
786e64833f2SRemy Horton 		if (qconf->n_rx_port == 0)
787e64833f2SRemy Horton 			RTE_LOG(INFO, L2FWD,
788e64833f2SRemy Horton 				"lcore %u has nothing to do\n",
789e64833f2SRemy Horton 				lcore_id
790e64833f2SRemy Horton 				);
791e64833f2SRemy Horton 		else {
792e64833f2SRemy Horton 			rte_eal_remote_launch(
793e64833f2SRemy Horton 				l2fwd_launch_one_lcore,
794e64833f2SRemy Horton 				NULL,
795e64833f2SRemy Horton 				lcore_id
796e64833f2SRemy Horton 				);
797e64833f2SRemy Horton 			rte_keepalive_register_core(rte_global_keepalive_info,
798e64833f2SRemy Horton 				lcore_id);
799e64833f2SRemy Horton 		}
800e64833f2SRemy Horton 	}
80191e89e47SRemy Horton 	while (!terminate_signal_received) {
802e64833f2SRemy Horton 		rte_timer_manage();
803e64833f2SRemy Horton 		rte_delay_ms(5);
804e64833f2SRemy Horton 		}
805e64833f2SRemy Horton 
806cb056611SStephen Hemminger 	RTE_LCORE_FOREACH_WORKER(lcore_id) {
807e64833f2SRemy Horton 		if (rte_eal_wait_lcore(lcore_id) < 0)
808e64833f2SRemy Horton 			return -1;
809e64833f2SRemy Horton 	}
810e64833f2SRemy Horton 
81193543923SRemy Horton 	if (ka_shm != NULL)
81293543923SRemy Horton 		rte_keepalive_shm_cleanup(ka_shm);
81310aa3757SChengchang Tang 
81410aa3757SChengchang Tang 	/* clean up the EAL */
81510aa3757SChengchang Tang 	rte_eal_cleanup();
81610aa3757SChengchang Tang 
817e64833f2SRemy Horton 	return 0;
818e64833f2SRemy Horton }
819