xref: /dpdk/examples/l3fwd-power/main.c (revision 1cde1b9a9b4dbf31cb5e5ccdfc5da3cb079f43a2)
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2010-2018 Intel Corporation
3  */
4 
5 #include <stdio.h>
6 #include <stdlib.h>
7 #include <stdint.h>
8 #include <inttypes.h>
9 #include <sys/types.h>
10 #include <string.h>
11 #include <sys/queue.h>
12 #include <stdarg.h>
13 #include <errno.h>
14 #include <getopt.h>
15 #include <unistd.h>
16 #include <signal.h>
17 #include <math.h>
18 
19 #include <rte_common.h>
20 #include <rte_byteorder.h>
21 #include <rte_log.h>
22 #include <rte_malloc.h>
23 #include <rte_memory.h>
24 #include <rte_memcpy.h>
25 #include <rte_eal.h>
26 #include <rte_launch.h>
27 #include <rte_atomic.h>
28 #include <rte_cycles.h>
29 #include <rte_prefetch.h>
30 #include <rte_lcore.h>
31 #include <rte_per_lcore.h>
32 #include <rte_branch_prediction.h>
33 #include <rte_interrupts.h>
34 #include <rte_random.h>
35 #include <rte_debug.h>
36 #include <rte_ether.h>
37 #include <rte_ethdev.h>
38 #include <rte_mempool.h>
39 #include <rte_mbuf.h>
40 #include <rte_ip.h>
41 #include <rte_tcp.h>
42 #include <rte_udp.h>
43 #include <rte_string_fns.h>
44 #include <rte_timer.h>
45 #include <rte_power.h>
46 #include <rte_spinlock.h>
47 #include <rte_power_empty_poll.h>
48 #include <rte_metrics.h>
49 
50 #include "perf_core.h"
51 #include "main.h"
52 
53 #define RTE_LOGTYPE_L3FWD_POWER RTE_LOGTYPE_USER1
54 
55 #define MAX_PKT_BURST 32
56 
57 #define MIN_ZERO_POLL_COUNT 10
58 
59 /* 100 ms interval */
60 #define TIMER_NUMBER_PER_SECOND           10
61 /* (10ms) */
62 #define INTERVALS_PER_SECOND             100
63 /* 100000 us */
64 #define SCALING_PERIOD                    (1000000/TIMER_NUMBER_PER_SECOND)
65 #define SCALING_DOWN_TIME_RATIO_THRESHOLD 0.25
66 
67 #define APP_LOOKUP_EXACT_MATCH          0
68 #define APP_LOOKUP_LPM                  1
69 #define DO_RFC_1812_CHECKS
70 
71 #ifndef APP_LOOKUP_METHOD
72 #define APP_LOOKUP_METHOD             APP_LOOKUP_LPM
73 #endif
74 
75 #if (APP_LOOKUP_METHOD == APP_LOOKUP_EXACT_MATCH)
76 #include <rte_hash.h>
77 #elif (APP_LOOKUP_METHOD == APP_LOOKUP_LPM)
78 #include <rte_lpm.h>
79 #else
80 #error "APP_LOOKUP_METHOD set to incorrect value"
81 #endif
82 
83 #ifndef IPv6_BYTES
84 #define IPv6_BYTES_FMT "%02x%02x:%02x%02x:%02x%02x:%02x%02x:"\
85                        "%02x%02x:%02x%02x:%02x%02x:%02x%02x"
86 #define IPv6_BYTES(addr) \
87 	addr[0],  addr[1], addr[2],  addr[3], \
88 	addr[4],  addr[5], addr[6],  addr[7], \
89 	addr[8],  addr[9], addr[10], addr[11],\
90 	addr[12], addr[13],addr[14], addr[15]
91 #endif
92 
93 #define MAX_JUMBO_PKT_LEN  9600
94 
95 #define IPV6_ADDR_LEN 16
96 
97 #define MEMPOOL_CACHE_SIZE 256
98 
99 /*
100  * This expression is used to calculate the number of mbufs needed depending on
101  * user input, taking into account memory for rx and tx hardware rings, cache
102  * per lcore and mtable per port per lcore. RTE_MAX is used to ensure that
103  * NB_MBUF never goes below a minimum value of 8192.
104  */
105 
106 #define NB_MBUF RTE_MAX	( \
107 	(nb_ports*nb_rx_queue*nb_rxd + \
108 	nb_ports*nb_lcores*MAX_PKT_BURST + \
109 	nb_ports*n_tx_queue*nb_txd + \
110 	nb_lcores*MEMPOOL_CACHE_SIZE), \
111 	(unsigned)8192)
112 
113 #define BURST_TX_DRAIN_US 100 /* TX drain every ~100us */
114 
115 #define NB_SOCKETS 8
116 
117 /* Configure how many packets ahead to prefetch, when reading packets */
118 #define PREFETCH_OFFSET	3
119 
120 /*
121  * Configurable number of RX/TX ring descriptors
122  */
123 #define RTE_TEST_RX_DESC_DEFAULT 1024
124 #define RTE_TEST_TX_DESC_DEFAULT 1024
125 
126 /*
127  * These two thresholds were decided on by running the training algorithm on
128  * a 2.5GHz Xeon. These defaults can be overridden by supplying non-zero values
129  * for the med_threshold and high_threshold parameters on the command line.
130  */
131 #define EMPTY_POLL_MED_THRESHOLD 350000UL
132 #define EMPTY_POLL_HGH_THRESHOLD 580000UL
133 
134 
135 
136 static uint16_t nb_rxd = RTE_TEST_RX_DESC_DEFAULT;
137 static uint16_t nb_txd = RTE_TEST_TX_DESC_DEFAULT;
138 
139 /* ethernet addresses of ports */
140 static struct rte_ether_addr ports_eth_addr[RTE_MAX_ETHPORTS];
141 
142 /* ethernet addresses of ports */
143 static rte_spinlock_t locks[RTE_MAX_ETHPORTS];
144 
145 /* mask of enabled ports */
146 static uint32_t enabled_port_mask = 0;
147 /* Ports set in promiscuous mode off by default. */
148 static int promiscuous_on = 0;
149 /* NUMA is enabled by default. */
150 static int numa_on = 1;
151 static bool empty_poll_stop;
152 static bool empty_poll_train;
153 volatile bool quit_signal;
154 static struct  ep_params *ep_params;
155 static struct  ep_policy policy;
156 static long  ep_med_edpi, ep_hgh_edpi;
157 /* timer to update telemetry every 500ms */
158 static struct rte_timer telemetry_timer;
159 
160 /* stats index returned by metrics lib */
161 int telstats_index;
162 
163 struct telstats_name {
164 	char name[RTE_ETH_XSTATS_NAME_SIZE];
165 };
166 
167 /* telemetry stats to be reported */
168 const struct telstats_name telstats_strings[] = {
169 	{"empty_poll"},
170 	{"full_poll"},
171 	{"busy_percent"}
172 };
173 
174 /* core busyness in percentage */
175 enum busy_rate {
176 	ZERO = 0,
177 	PARTIAL = 50,
178 	FULL = 100
179 };
180 
181 /* reference poll count to measure core busyness */
182 #define DEFAULT_COUNT 10000
183 /*
184  * reference CYCLES to be used to
185  * measure core busyness based on poll count
186  */
187 #define MIN_CYCLES  1500000ULL
188 #define MAX_CYCLES 22000000ULL
189 
190 /* (500ms) */
191 #define TELEMETRY_INTERVALS_PER_SEC 2
192 
193 static int parse_ptype; /**< Parse packet type using rx callback, and */
194 			/**< disabled by default */
195 
196 enum appmode {
197 	APP_MODE_LEGACY = 0,
198 	APP_MODE_EMPTY_POLL,
199 	APP_MODE_TELEMETRY
200 };
201 
202 enum appmode app_mode;
203 
204 enum freq_scale_hint_t
205 {
206 	FREQ_LOWER    =      -1,
207 	FREQ_CURRENT  =       0,
208 	FREQ_HIGHER   =       1,
209 	FREQ_HIGHEST  =       2
210 };
211 
212 struct lcore_rx_queue {
213 	uint16_t port_id;
214 	uint8_t queue_id;
215 	enum freq_scale_hint_t freq_up_hint;
216 	uint32_t zero_rx_packet_count;
217 	uint32_t idle_hint;
218 } __rte_cache_aligned;
219 
220 #define MAX_RX_QUEUE_PER_LCORE 16
221 #define MAX_TX_QUEUE_PER_PORT RTE_MAX_ETHPORTS
222 #define MAX_RX_QUEUE_PER_PORT 128
223 
224 #define MAX_RX_QUEUE_INTERRUPT_PER_PORT 16
225 
226 
227 struct lcore_params lcore_params_array[MAX_LCORE_PARAMS];
228 static struct lcore_params lcore_params_array_default[] = {
229 	{0, 0, 2},
230 	{0, 1, 2},
231 	{0, 2, 2},
232 	{1, 0, 2},
233 	{1, 1, 2},
234 	{1, 2, 2},
235 	{2, 0, 2},
236 	{3, 0, 3},
237 	{3, 1, 3},
238 };
239 
240 struct lcore_params *lcore_params = lcore_params_array_default;
241 uint16_t nb_lcore_params = sizeof(lcore_params_array_default) /
242 				sizeof(lcore_params_array_default[0]);
243 
244 static struct rte_eth_conf port_conf = {
245 	.rxmode = {
246 		.mq_mode        = ETH_MQ_RX_RSS,
247 		.max_rx_pkt_len = RTE_ETHER_MAX_LEN,
248 		.split_hdr_size = 0,
249 		.offloads = DEV_RX_OFFLOAD_CHECKSUM,
250 	},
251 	.rx_adv_conf = {
252 		.rss_conf = {
253 			.rss_key = NULL,
254 			.rss_hf = ETH_RSS_UDP,
255 		},
256 	},
257 	.txmode = {
258 		.mq_mode = ETH_MQ_TX_NONE,
259 	},
260 	.intr_conf = {
261 		.rxq = 1,
262 	},
263 };
264 
265 static struct rte_mempool * pktmbuf_pool[NB_SOCKETS];
266 
267 
268 #if (APP_LOOKUP_METHOD == APP_LOOKUP_EXACT_MATCH)
269 
270 #ifdef RTE_ARCH_X86
271 #include <rte_hash_crc.h>
272 #define DEFAULT_HASH_FUNC       rte_hash_crc
273 #else
274 #include <rte_jhash.h>
275 #define DEFAULT_HASH_FUNC       rte_jhash
276 #endif
277 
278 struct ipv4_5tuple {
279 	uint32_t ip_dst;
280 	uint32_t ip_src;
281 	uint16_t port_dst;
282 	uint16_t port_src;
283 	uint8_t  proto;
284 } __attribute__((__packed__));
285 
286 struct ipv6_5tuple {
287 	uint8_t  ip_dst[IPV6_ADDR_LEN];
288 	uint8_t  ip_src[IPV6_ADDR_LEN];
289 	uint16_t port_dst;
290 	uint16_t port_src;
291 	uint8_t  proto;
292 } __attribute__((__packed__));
293 
294 struct ipv4_l3fwd_route {
295 	struct ipv4_5tuple key;
296 	uint8_t if_out;
297 };
298 
299 struct ipv6_l3fwd_route {
300 	struct ipv6_5tuple key;
301 	uint8_t if_out;
302 };
303 
304 static struct ipv4_l3fwd_route ipv4_l3fwd_route_array[] = {
305 	{{RTE_IPV4(100,10,0,1), RTE_IPV4(200,10,0,1), 101, 11, IPPROTO_TCP}, 0},
306 	{{RTE_IPV4(100,20,0,2), RTE_IPV4(200,20,0,2), 102, 12, IPPROTO_TCP}, 1},
307 	{{RTE_IPV4(100,30,0,3), RTE_IPV4(200,30,0,3), 103, 13, IPPROTO_TCP}, 2},
308 	{{RTE_IPV4(100,40,0,4), RTE_IPV4(200,40,0,4), 104, 14, IPPROTO_TCP}, 3},
309 };
310 
311 static struct ipv6_l3fwd_route ipv6_l3fwd_route_array[] = {
312 	{
313 		{
314 			{0xfe, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
315 			 0x02, 0x1b, 0x21, 0xff, 0xfe, 0x91, 0x38, 0x05},
316 			{0xfe, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
317 			 0x02, 0x1e, 0x67, 0xff, 0xfe, 0x0d, 0xb6, 0x0a},
318 			 1, 10, IPPROTO_UDP
319 		}, 4
320 	},
321 };
322 
323 typedef struct rte_hash lookup_struct_t;
324 static lookup_struct_t *ipv4_l3fwd_lookup_struct[NB_SOCKETS];
325 static lookup_struct_t *ipv6_l3fwd_lookup_struct[NB_SOCKETS];
326 
327 #define L3FWD_HASH_ENTRIES	1024
328 
329 #define IPV4_L3FWD_NUM_ROUTES \
330 	(sizeof(ipv4_l3fwd_route_array) / sizeof(ipv4_l3fwd_route_array[0]))
331 
332 #define IPV6_L3FWD_NUM_ROUTES \
333 	(sizeof(ipv6_l3fwd_route_array) / sizeof(ipv6_l3fwd_route_array[0]))
334 
335 static uint16_t ipv4_l3fwd_out_if[L3FWD_HASH_ENTRIES] __rte_cache_aligned;
336 static uint16_t ipv6_l3fwd_out_if[L3FWD_HASH_ENTRIES] __rte_cache_aligned;
337 #endif
338 
339 #if (APP_LOOKUP_METHOD == APP_LOOKUP_LPM)
340 struct ipv4_l3fwd_route {
341 	uint32_t ip;
342 	uint8_t  depth;
343 	uint8_t  if_out;
344 };
345 
346 static struct ipv4_l3fwd_route ipv4_l3fwd_route_array[] = {
347 	{RTE_IPV4(1,1,1,0), 24, 0},
348 	{RTE_IPV4(2,1,1,0), 24, 1},
349 	{RTE_IPV4(3,1,1,0), 24, 2},
350 	{RTE_IPV4(4,1,1,0), 24, 3},
351 	{RTE_IPV4(5,1,1,0), 24, 4},
352 	{RTE_IPV4(6,1,1,0), 24, 5},
353 	{RTE_IPV4(7,1,1,0), 24, 6},
354 	{RTE_IPV4(8,1,1,0), 24, 7},
355 };
356 
357 #define IPV4_L3FWD_NUM_ROUTES \
358 	(sizeof(ipv4_l3fwd_route_array) / sizeof(ipv4_l3fwd_route_array[0]))
359 
360 #define IPV4_L3FWD_LPM_MAX_RULES     1024
361 
362 typedef struct rte_lpm lookup_struct_t;
363 static lookup_struct_t *ipv4_l3fwd_lookup_struct[NB_SOCKETS];
364 #endif
365 
366 struct lcore_conf {
367 	uint16_t n_rx_queue;
368 	struct lcore_rx_queue rx_queue_list[MAX_RX_QUEUE_PER_LCORE];
369 	uint16_t n_tx_port;
370 	uint16_t tx_port_id[RTE_MAX_ETHPORTS];
371 	uint16_t tx_queue_id[RTE_MAX_ETHPORTS];
372 	struct rte_eth_dev_tx_buffer *tx_buffer[RTE_MAX_ETHPORTS];
373 	lookup_struct_t * ipv4_lookup_struct;
374 	lookup_struct_t * ipv6_lookup_struct;
375 } __rte_cache_aligned;
376 
377 struct lcore_stats {
378 	/* total sleep time in ms since last frequency scaling down */
379 	uint32_t sleep_time;
380 	/* number of long sleep recently */
381 	uint32_t nb_long_sleep;
382 	/* freq. scaling up trend */
383 	uint32_t trend;
384 	/* total packet processed recently */
385 	uint64_t nb_rx_processed;
386 	/* total iterations looped recently */
387 	uint64_t nb_iteration_looped;
388 	/*
389 	 * Represents empty and non empty polls
390 	 * of rte_eth_rx_burst();
391 	 * ep_nep[0] holds non empty polls
392 	 * i.e. 0 < nb_rx <= MAX_BURST
393 	 * ep_nep[1] holds empty polls.
394 	 * i.e. nb_rx == 0
395 	 */
396 	uint64_t ep_nep[2];
397 	/*
398 	 * Represents full and empty+partial
399 	 * polls of rte_eth_rx_burst();
400 	 * ep_nep[0] holds empty+partial polls.
401 	 * i.e. 0 <= nb_rx < MAX_BURST
402 	 * ep_nep[1] holds full polls
403 	 * i.e. nb_rx == MAX_BURST
404 	 */
405 	uint64_t fp_nfp[2];
406 	enum busy_rate br;
407 	rte_spinlock_t telemetry_lock;
408 } __rte_cache_aligned;
409 
410 static struct lcore_conf lcore_conf[RTE_MAX_LCORE] __rte_cache_aligned;
411 static struct lcore_stats stats[RTE_MAX_LCORE] __rte_cache_aligned;
412 static struct rte_timer power_timers[RTE_MAX_LCORE];
413 
414 static inline uint32_t power_idle_heuristic(uint32_t zero_rx_packet_count);
415 static inline enum freq_scale_hint_t power_freq_scaleup_heuristic( \
416 		unsigned int lcore_id, uint16_t port_id, uint16_t queue_id);
417 
418 
419 /*
420  * These defaults are using the max frequency index (1), a medium index (9)
421  * and a typical low frequency index (14). These can be adjusted to use
422  * different indexes using the relevant command line parameters.
423  */
424 static uint8_t  freq_tlb[] = {14, 9, 1};
425 
426 static int is_done(void)
427 {
428 	return quit_signal;
429 }
430 
431 /* exit signal handler */
432 static void
433 signal_exit_now(int sigtype)
434 {
435 	unsigned lcore_id;
436 	unsigned int portid;
437 	int ret;
438 
439 	if (sigtype == SIGINT) {
440 		if (app_mode == APP_MODE_EMPTY_POLL ||
441 				app_mode == APP_MODE_TELEMETRY)
442 			quit_signal = true;
443 
444 
445 		for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
446 			if (rte_lcore_is_enabled(lcore_id) == 0)
447 				continue;
448 
449 			/* init power management library */
450 			ret = rte_power_exit(lcore_id);
451 			if (ret)
452 				rte_exit(EXIT_FAILURE, "Power management "
453 					"library de-initialization failed on "
454 							"core%u\n", lcore_id);
455 		}
456 
457 		if (app_mode != APP_MODE_EMPTY_POLL) {
458 			RTE_ETH_FOREACH_DEV(portid) {
459 				if ((enabled_port_mask & (1 << portid)) == 0)
460 					continue;
461 
462 				rte_eth_dev_stop(portid);
463 				rte_eth_dev_close(portid);
464 			}
465 		}
466 	}
467 
468 	if (app_mode != APP_MODE_EMPTY_POLL)
469 		rte_exit(EXIT_SUCCESS, "User forced exit\n");
470 }
471 
472 /*  Freqency scale down timer callback */
473 static void
474 power_timer_cb(__attribute__((unused)) struct rte_timer *tim,
475 			  __attribute__((unused)) void *arg)
476 {
477 	uint64_t hz;
478 	float sleep_time_ratio;
479 	unsigned lcore_id = rte_lcore_id();
480 
481 	/* accumulate total execution time in us when callback is invoked */
482 	sleep_time_ratio = (float)(stats[lcore_id].sleep_time) /
483 					(float)SCALING_PERIOD;
484 	/**
485 	 * check whether need to scale down frequency a step if it sleep a lot.
486 	 */
487 	if (sleep_time_ratio >= SCALING_DOWN_TIME_RATIO_THRESHOLD) {
488 		if (rte_power_freq_down)
489 			rte_power_freq_down(lcore_id);
490 	}
491 	else if ( (unsigned)(stats[lcore_id].nb_rx_processed /
492 		stats[lcore_id].nb_iteration_looped) < MAX_PKT_BURST) {
493 		/**
494 		 * scale down a step if average packet per iteration less
495 		 * than expectation.
496 		 */
497 		if (rte_power_freq_down)
498 			rte_power_freq_down(lcore_id);
499 	}
500 
501 	/**
502 	 * initialize another timer according to current frequency to ensure
503 	 * timer interval is relatively fixed.
504 	 */
505 	hz = rte_get_timer_hz();
506 	rte_timer_reset(&power_timers[lcore_id], hz/TIMER_NUMBER_PER_SECOND,
507 				SINGLE, lcore_id, power_timer_cb, NULL);
508 
509 	stats[lcore_id].nb_rx_processed = 0;
510 	stats[lcore_id].nb_iteration_looped = 0;
511 
512 	stats[lcore_id].sleep_time = 0;
513 }
514 
515 /* Enqueue a single packet, and send burst if queue is filled */
516 static inline int
517 send_single_packet(struct rte_mbuf *m, uint16_t port)
518 {
519 	uint32_t lcore_id;
520 	struct lcore_conf *qconf;
521 
522 	lcore_id = rte_lcore_id();
523 	qconf = &lcore_conf[lcore_id];
524 
525 	rte_eth_tx_buffer(port, qconf->tx_queue_id[port],
526 			qconf->tx_buffer[port], m);
527 
528 	return 0;
529 }
530 
531 #ifdef DO_RFC_1812_CHECKS
532 static inline int
533 is_valid_ipv4_pkt(struct rte_ipv4_hdr *pkt, uint32_t link_len)
534 {
535 	/* From http://www.rfc-editor.org/rfc/rfc1812.txt section 5.2.2 */
536 	/*
537 	 * 1. The packet length reported by the Link Layer must be large
538 	 * enough to hold the minimum length legal IP datagram (20 bytes).
539 	 */
540 	if (link_len < sizeof(struct rte_ipv4_hdr))
541 		return -1;
542 
543 	/* 2. The IP checksum must be correct. */
544 	/* this is checked in H/W */
545 
546 	/*
547 	 * 3. The IP version number must be 4. If the version number is not 4
548 	 * then the packet may be another version of IP, such as IPng or
549 	 * ST-II.
550 	 */
551 	if (((pkt->version_ihl) >> 4) != 4)
552 		return -3;
553 	/*
554 	 * 4. The IP header length field must be large enough to hold the
555 	 * minimum length legal IP datagram (20 bytes = 5 words).
556 	 */
557 	if ((pkt->version_ihl & 0xf) < 5)
558 		return -4;
559 
560 	/*
561 	 * 5. The IP total length field must be large enough to hold the IP
562 	 * datagram header, whose length is specified in the IP header length
563 	 * field.
564 	 */
565 	if (rte_cpu_to_be_16(pkt->total_length) < sizeof(struct rte_ipv4_hdr))
566 		return -5;
567 
568 	return 0;
569 }
570 #endif
571 
572 #if (APP_LOOKUP_METHOD == APP_LOOKUP_EXACT_MATCH)
573 static void
574 print_ipv4_key(struct ipv4_5tuple key)
575 {
576 	printf("IP dst = %08x, IP src = %08x, port dst = %d, port src = %d, "
577 		"proto = %d\n", (unsigned)key.ip_dst, (unsigned)key.ip_src,
578 				key.port_dst, key.port_src, key.proto);
579 }
580 static void
581 print_ipv6_key(struct ipv6_5tuple key)
582 {
583 	printf( "IP dst = " IPv6_BYTES_FMT ", IP src = " IPv6_BYTES_FMT ", "
584 	        "port dst = %d, port src = %d, proto = %d\n",
585 	        IPv6_BYTES(key.ip_dst), IPv6_BYTES(key.ip_src),
586 	        key.port_dst, key.port_src, key.proto);
587 }
588 
589 static inline uint16_t
590 get_ipv4_dst_port(struct rte_ipv4_hdr *ipv4_hdr, uint16_t portid,
591 		lookup_struct_t * ipv4_l3fwd_lookup_struct)
592 {
593 	struct ipv4_5tuple key;
594 	struct rte_tcp_hdr *tcp;
595 	struct rte_udp_hdr *udp;
596 	int ret = 0;
597 
598 	key.ip_dst = rte_be_to_cpu_32(ipv4_hdr->dst_addr);
599 	key.ip_src = rte_be_to_cpu_32(ipv4_hdr->src_addr);
600 	key.proto = ipv4_hdr->next_proto_id;
601 
602 	switch (ipv4_hdr->next_proto_id) {
603 	case IPPROTO_TCP:
604 		tcp = (struct rte_tcp_hdr *)((unsigned char *)ipv4_hdr +
605 					sizeof(struct rte_ipv4_hdr));
606 		key.port_dst = rte_be_to_cpu_16(tcp->dst_port);
607 		key.port_src = rte_be_to_cpu_16(tcp->src_port);
608 		break;
609 
610 	case IPPROTO_UDP:
611 		udp = (struct rte_udp_hdr *)((unsigned char *)ipv4_hdr +
612 					sizeof(struct rte_ipv4_hdr));
613 		key.port_dst = rte_be_to_cpu_16(udp->dst_port);
614 		key.port_src = rte_be_to_cpu_16(udp->src_port);
615 		break;
616 
617 	default:
618 		key.port_dst = 0;
619 		key.port_src = 0;
620 		break;
621 	}
622 
623 	/* Find destination port */
624 	ret = rte_hash_lookup(ipv4_l3fwd_lookup_struct, (const void *)&key);
625 	return ((ret < 0) ? portid : ipv4_l3fwd_out_if[ret]);
626 }
627 
628 static inline uint16_t
629 get_ipv6_dst_port(struct rte_ipv6_hdr *ipv6_hdr, uint16_t portid,
630 			lookup_struct_t *ipv6_l3fwd_lookup_struct)
631 {
632 	struct ipv6_5tuple key;
633 	struct rte_tcp_hdr *tcp;
634 	struct rte_udp_hdr *udp;
635 	int ret = 0;
636 
637 	memcpy(key.ip_dst, ipv6_hdr->dst_addr, IPV6_ADDR_LEN);
638 	memcpy(key.ip_src, ipv6_hdr->src_addr, IPV6_ADDR_LEN);
639 
640 	key.proto = ipv6_hdr->proto;
641 
642 	switch (ipv6_hdr->proto) {
643 	case IPPROTO_TCP:
644 		tcp = (struct rte_tcp_hdr *)((unsigned char *) ipv6_hdr +
645 					sizeof(struct rte_ipv6_hdr));
646 		key.port_dst = rte_be_to_cpu_16(tcp->dst_port);
647 		key.port_src = rte_be_to_cpu_16(tcp->src_port);
648 		break;
649 
650 	case IPPROTO_UDP:
651 		udp = (struct rte_udp_hdr *)((unsigned char *) ipv6_hdr +
652 					sizeof(struct rte_ipv6_hdr));
653 		key.port_dst = rte_be_to_cpu_16(udp->dst_port);
654 		key.port_src = rte_be_to_cpu_16(udp->src_port);
655 		break;
656 
657 	default:
658 		key.port_dst = 0;
659 		key.port_src = 0;
660 		break;
661 	}
662 
663 	/* Find destination port */
664 	ret = rte_hash_lookup(ipv6_l3fwd_lookup_struct, (const void *)&key);
665 	return ((ret < 0) ? portid : ipv6_l3fwd_out_if[ret]);
666 }
667 #endif
668 
669 #if (APP_LOOKUP_METHOD == APP_LOOKUP_LPM)
670 static inline uint16_t
671 get_ipv4_dst_port(struct rte_ipv4_hdr *ipv4_hdr, uint16_t portid,
672 		lookup_struct_t *ipv4_l3fwd_lookup_struct)
673 {
674 	uint32_t next_hop;
675 
676 	return ((rte_lpm_lookup(ipv4_l3fwd_lookup_struct,
677 			rte_be_to_cpu_32(ipv4_hdr->dst_addr), &next_hop) == 0)?
678 			next_hop : portid);
679 }
680 #endif
681 
682 static inline void
683 parse_ptype_one(struct rte_mbuf *m)
684 {
685 	struct rte_ether_hdr *eth_hdr;
686 	uint32_t packet_type = RTE_PTYPE_UNKNOWN;
687 	uint16_t ether_type;
688 
689 	eth_hdr = rte_pktmbuf_mtod(m, struct rte_ether_hdr *);
690 	ether_type = eth_hdr->ether_type;
691 	if (ether_type == rte_cpu_to_be_16(RTE_ETHER_TYPE_IPV4))
692 		packet_type |= RTE_PTYPE_L3_IPV4_EXT_UNKNOWN;
693 	else if (ether_type == rte_cpu_to_be_16(RTE_ETHER_TYPE_IPV6))
694 		packet_type |= RTE_PTYPE_L3_IPV6_EXT_UNKNOWN;
695 
696 	m->packet_type = packet_type;
697 }
698 
699 static uint16_t
700 cb_parse_ptype(uint16_t port __rte_unused, uint16_t queue __rte_unused,
701 	       struct rte_mbuf *pkts[], uint16_t nb_pkts,
702 	       uint16_t max_pkts __rte_unused,
703 	       void *user_param __rte_unused)
704 {
705 	unsigned int i;
706 
707 	for (i = 0; i < nb_pkts; ++i)
708 		parse_ptype_one(pkts[i]);
709 
710 	return nb_pkts;
711 }
712 
713 static int
714 add_cb_parse_ptype(uint16_t portid, uint16_t queueid)
715 {
716 	printf("Port %d: softly parse packet type info\n", portid);
717 	if (rte_eth_add_rx_callback(portid, queueid, cb_parse_ptype, NULL))
718 		return 0;
719 
720 	printf("Failed to add rx callback: port=%d\n", portid);
721 	return -1;
722 }
723 
724 static inline void
725 l3fwd_simple_forward(struct rte_mbuf *m, uint16_t portid,
726 				struct lcore_conf *qconf)
727 {
728 	struct rte_ether_hdr *eth_hdr;
729 	struct rte_ipv4_hdr *ipv4_hdr;
730 	void *d_addr_bytes;
731 	uint16_t dst_port;
732 
733 	eth_hdr = rte_pktmbuf_mtod(m, struct rte_ether_hdr *);
734 
735 	if (RTE_ETH_IS_IPV4_HDR(m->packet_type)) {
736 		/* Handle IPv4 headers.*/
737 		ipv4_hdr =
738 			rte_pktmbuf_mtod_offset(m, struct rte_ipv4_hdr *,
739 						sizeof(struct rte_ether_hdr));
740 
741 #ifdef DO_RFC_1812_CHECKS
742 		/* Check to make sure the packet is valid (RFC1812) */
743 		if (is_valid_ipv4_pkt(ipv4_hdr, m->pkt_len) < 0) {
744 			rte_pktmbuf_free(m);
745 			return;
746 		}
747 #endif
748 
749 		dst_port = get_ipv4_dst_port(ipv4_hdr, portid,
750 					qconf->ipv4_lookup_struct);
751 		if (dst_port >= RTE_MAX_ETHPORTS ||
752 				(enabled_port_mask & 1 << dst_port) == 0)
753 			dst_port = portid;
754 
755 		/* 02:00:00:00:00:xx */
756 		d_addr_bytes = &eth_hdr->d_addr.addr_bytes[0];
757 		*((uint64_t *)d_addr_bytes) =
758 			0x000000000002 + ((uint64_t)dst_port << 40);
759 
760 #ifdef DO_RFC_1812_CHECKS
761 		/* Update time to live and header checksum */
762 		--(ipv4_hdr->time_to_live);
763 		++(ipv4_hdr->hdr_checksum);
764 #endif
765 
766 		/* src addr */
767 		rte_ether_addr_copy(&ports_eth_addr[dst_port],
768 				&eth_hdr->s_addr);
769 
770 		send_single_packet(m, dst_port);
771 	} else if (RTE_ETH_IS_IPV6_HDR(m->packet_type)) {
772 		/* Handle IPv6 headers.*/
773 #if (APP_LOOKUP_METHOD == APP_LOOKUP_EXACT_MATCH)
774 		struct rte_ipv6_hdr *ipv6_hdr;
775 
776 		ipv6_hdr =
777 			rte_pktmbuf_mtod_offset(m, struct rte_ipv6_hdr *,
778 						sizeof(struct rte_ether_hdr));
779 
780 		dst_port = get_ipv6_dst_port(ipv6_hdr, portid,
781 					qconf->ipv6_lookup_struct);
782 
783 		if (dst_port >= RTE_MAX_ETHPORTS ||
784 				(enabled_port_mask & 1 << dst_port) == 0)
785 			dst_port = portid;
786 
787 		/* 02:00:00:00:00:xx */
788 		d_addr_bytes = &eth_hdr->d_addr.addr_bytes[0];
789 		*((uint64_t *)d_addr_bytes) =
790 			0x000000000002 + ((uint64_t)dst_port << 40);
791 
792 		/* src addr */
793 		rte_ether_addr_copy(&ports_eth_addr[dst_port],
794 				&eth_hdr->s_addr);
795 
796 		send_single_packet(m, dst_port);
797 #else
798 		/* We don't currently handle IPv6 packets in LPM mode. */
799 		rte_pktmbuf_free(m);
800 #endif
801 	} else
802 		rte_pktmbuf_free(m);
803 
804 }
805 
806 #define MINIMUM_SLEEP_TIME         1
807 #define SUSPEND_THRESHOLD          300
808 
809 static inline uint32_t
810 power_idle_heuristic(uint32_t zero_rx_packet_count)
811 {
812 	/* If zero count is less than 100,  sleep 1us */
813 	if (zero_rx_packet_count < SUSPEND_THRESHOLD)
814 		return MINIMUM_SLEEP_TIME;
815 	/* If zero count is less than 1000, sleep 100 us which is the
816 		minimum latency switching from C3/C6 to C0
817 	*/
818 	else
819 		return SUSPEND_THRESHOLD;
820 }
821 
822 static inline enum freq_scale_hint_t
823 power_freq_scaleup_heuristic(unsigned lcore_id,
824 			     uint16_t port_id,
825 			     uint16_t queue_id)
826 {
827 	uint32_t rxq_count = rte_eth_rx_queue_count(port_id, queue_id);
828 /**
829  * HW Rx queue size is 128 by default, Rx burst read at maximum 32 entries
830  * per iteration
831  */
832 #define FREQ_GEAR1_RX_PACKET_THRESHOLD             MAX_PKT_BURST
833 #define FREQ_GEAR2_RX_PACKET_THRESHOLD             (MAX_PKT_BURST*2)
834 #define FREQ_GEAR3_RX_PACKET_THRESHOLD             (MAX_PKT_BURST*3)
835 #define FREQ_UP_TREND1_ACC   1
836 #define FREQ_UP_TREND2_ACC   100
837 #define FREQ_UP_THRESHOLD    10000
838 
839 	if (likely(rxq_count > FREQ_GEAR3_RX_PACKET_THRESHOLD)) {
840 		stats[lcore_id].trend = 0;
841 		return FREQ_HIGHEST;
842 	} else if (likely(rxq_count > FREQ_GEAR2_RX_PACKET_THRESHOLD))
843 		stats[lcore_id].trend += FREQ_UP_TREND2_ACC;
844 	else if (likely(rxq_count > FREQ_GEAR1_RX_PACKET_THRESHOLD))
845 		stats[lcore_id].trend += FREQ_UP_TREND1_ACC;
846 
847 	if (likely(stats[lcore_id].trend > FREQ_UP_THRESHOLD)) {
848 		stats[lcore_id].trend = 0;
849 		return FREQ_HIGHER;
850 	}
851 
852 	return FREQ_CURRENT;
853 }
854 
855 /**
856  * force polling thread sleep until one-shot rx interrupt triggers
857  * @param port_id
858  *  Port id.
859  * @param queue_id
860  *  Rx queue id.
861  * @return
862  *  0 on success
863  */
864 static int
865 sleep_until_rx_interrupt(int num)
866 {
867 	struct rte_epoll_event event[num];
868 	int n, i;
869 	uint16_t port_id;
870 	uint8_t queue_id;
871 	void *data;
872 
873 	RTE_LOG(INFO, L3FWD_POWER,
874 		"lcore %u sleeps until interrupt triggers\n",
875 		rte_lcore_id());
876 
877 	n = rte_epoll_wait(RTE_EPOLL_PER_THREAD, event, num, -1);
878 	for (i = 0; i < n; i++) {
879 		data = event[i].epdata.data;
880 		port_id = ((uintptr_t)data) >> CHAR_BIT;
881 		queue_id = ((uintptr_t)data) &
882 			RTE_LEN2MASK(CHAR_BIT, uint8_t);
883 		rte_eth_dev_rx_intr_disable(port_id, queue_id);
884 		RTE_LOG(INFO, L3FWD_POWER,
885 			"lcore %u is waked up from rx interrupt on"
886 			" port %d queue %d\n",
887 			rte_lcore_id(), port_id, queue_id);
888 	}
889 
890 	return 0;
891 }
892 
893 static void turn_on_intr(struct lcore_conf *qconf)
894 {
895 	int i;
896 	struct lcore_rx_queue *rx_queue;
897 	uint8_t queue_id;
898 	uint16_t port_id;
899 
900 	for (i = 0; i < qconf->n_rx_queue; ++i) {
901 		rx_queue = &(qconf->rx_queue_list[i]);
902 		port_id = rx_queue->port_id;
903 		queue_id = rx_queue->queue_id;
904 
905 		rte_spinlock_lock(&(locks[port_id]));
906 		rte_eth_dev_rx_intr_enable(port_id, queue_id);
907 		rte_spinlock_unlock(&(locks[port_id]));
908 	}
909 }
910 
911 static int event_register(struct lcore_conf *qconf)
912 {
913 	struct lcore_rx_queue *rx_queue;
914 	uint8_t queueid;
915 	uint16_t portid;
916 	uint32_t data;
917 	int ret;
918 	int i;
919 
920 	for (i = 0; i < qconf->n_rx_queue; ++i) {
921 		rx_queue = &(qconf->rx_queue_list[i]);
922 		portid = rx_queue->port_id;
923 		queueid = rx_queue->queue_id;
924 		data = portid << CHAR_BIT | queueid;
925 
926 		ret = rte_eth_dev_rx_intr_ctl_q(portid, queueid,
927 						RTE_EPOLL_PER_THREAD,
928 						RTE_INTR_EVENT_ADD,
929 						(void *)((uintptr_t)data));
930 		if (ret)
931 			return ret;
932 	}
933 
934 	return 0;
935 }
936 /* main processing loop */
937 static int
938 main_telemetry_loop(__attribute__((unused)) void *dummy)
939 {
940 	struct rte_mbuf *pkts_burst[MAX_PKT_BURST];
941 	unsigned int lcore_id;
942 	uint64_t prev_tsc, diff_tsc, cur_tsc, prev_tel_tsc;
943 	int i, j, nb_rx;
944 	uint8_t queueid;
945 	uint16_t portid;
946 	struct lcore_conf *qconf;
947 	struct lcore_rx_queue *rx_queue;
948 	uint64_t ep_nep[2] = {0}, fp_nfp[2] = {0};
949 	uint64_t poll_count;
950 	enum busy_rate br;
951 
952 	const uint64_t drain_tsc = (rte_get_tsc_hz() + US_PER_S - 1) /
953 					US_PER_S * BURST_TX_DRAIN_US;
954 
955 	poll_count = 0;
956 	prev_tsc = 0;
957 	prev_tel_tsc = 0;
958 
959 	lcore_id = rte_lcore_id();
960 	qconf = &lcore_conf[lcore_id];
961 
962 	if (qconf->n_rx_queue == 0) {
963 		RTE_LOG(INFO, L3FWD_POWER, "lcore %u has nothing to do\n",
964 			lcore_id);
965 		return 0;
966 	}
967 
968 	RTE_LOG(INFO, L3FWD_POWER, "entering main telemetry loop on lcore %u\n",
969 		lcore_id);
970 
971 	for (i = 0; i < qconf->n_rx_queue; i++) {
972 		portid = qconf->rx_queue_list[i].port_id;
973 		queueid = qconf->rx_queue_list[i].queue_id;
974 		RTE_LOG(INFO, L3FWD_POWER, " -- lcoreid=%u portid=%u "
975 			"rxqueueid=%hhu\n", lcore_id, portid, queueid);
976 	}
977 
978 	while (!is_done()) {
979 
980 		cur_tsc = rte_rdtsc();
981 		/*
982 		 * TX burst queue drain
983 		 */
984 		diff_tsc = cur_tsc - prev_tsc;
985 		if (unlikely(diff_tsc > drain_tsc)) {
986 			for (i = 0; i < qconf->n_tx_port; ++i) {
987 				portid = qconf->tx_port_id[i];
988 				rte_eth_tx_buffer_flush(portid,
989 						qconf->tx_queue_id[portid],
990 						qconf->tx_buffer[portid]);
991 			}
992 			prev_tsc = cur_tsc;
993 		}
994 
995 		/*
996 		 * Read packet from RX queues
997 		 */
998 		for (i = 0; i < qconf->n_rx_queue; ++i) {
999 			rx_queue = &(qconf->rx_queue_list[i]);
1000 			portid = rx_queue->port_id;
1001 			queueid = rx_queue->queue_id;
1002 
1003 			nb_rx = rte_eth_rx_burst(portid, queueid, pkts_burst,
1004 								MAX_PKT_BURST);
1005 			ep_nep[nb_rx == 0]++;
1006 			fp_nfp[nb_rx == MAX_PKT_BURST]++;
1007 			poll_count++;
1008 			if (unlikely(nb_rx == 0))
1009 				continue;
1010 
1011 			/* Prefetch first packets */
1012 			for (j = 0; j < PREFETCH_OFFSET && j < nb_rx; j++) {
1013 				rte_prefetch0(rte_pktmbuf_mtod(
1014 						pkts_burst[j], void *));
1015 			}
1016 
1017 			/* Prefetch and forward already prefetched packets */
1018 			for (j = 0; j < (nb_rx - PREFETCH_OFFSET); j++) {
1019 				rte_prefetch0(rte_pktmbuf_mtod(pkts_burst[
1020 						j + PREFETCH_OFFSET], void *));
1021 				l3fwd_simple_forward(pkts_burst[j], portid,
1022 								qconf);
1023 			}
1024 
1025 			/* Forward remaining prefetched packets */
1026 			for (; j < nb_rx; j++) {
1027 				l3fwd_simple_forward(pkts_burst[j], portid,
1028 								qconf);
1029 			}
1030 		}
1031 		if (unlikely(poll_count >= DEFAULT_COUNT)) {
1032 			diff_tsc = cur_tsc - prev_tel_tsc;
1033 			if (diff_tsc >= MAX_CYCLES) {
1034 				br = FULL;
1035 			} else if (diff_tsc > MIN_CYCLES &&
1036 					diff_tsc < MAX_CYCLES) {
1037 				br = (diff_tsc * 100) / MAX_CYCLES;
1038 			} else {
1039 				br = ZERO;
1040 			}
1041 			poll_count = 0;
1042 			prev_tel_tsc = cur_tsc;
1043 			/* update stats for telemetry */
1044 			rte_spinlock_lock(&stats[lcore_id].telemetry_lock);
1045 			stats[lcore_id].ep_nep[0] = ep_nep[0];
1046 			stats[lcore_id].ep_nep[1] = ep_nep[1];
1047 			stats[lcore_id].fp_nfp[0] = fp_nfp[0];
1048 			stats[lcore_id].fp_nfp[1] = fp_nfp[1];
1049 			stats[lcore_id].br = br;
1050 			rte_spinlock_unlock(&stats[lcore_id].telemetry_lock);
1051 		}
1052 	}
1053 
1054 	return 0;
1055 }
1056 /* main processing loop */
1057 static int
1058 main_empty_poll_loop(__attribute__((unused)) void *dummy)
1059 {
1060 	struct rte_mbuf *pkts_burst[MAX_PKT_BURST];
1061 	unsigned int lcore_id;
1062 	uint64_t prev_tsc, diff_tsc, cur_tsc;
1063 	int i, j, nb_rx;
1064 	uint8_t queueid;
1065 	uint16_t portid;
1066 	struct lcore_conf *qconf;
1067 	struct lcore_rx_queue *rx_queue;
1068 
1069 	const uint64_t drain_tsc =
1070 		(rte_get_tsc_hz() + US_PER_S - 1) /
1071 		US_PER_S * BURST_TX_DRAIN_US;
1072 
1073 	prev_tsc = 0;
1074 
1075 	lcore_id = rte_lcore_id();
1076 	qconf = &lcore_conf[lcore_id];
1077 
1078 	if (qconf->n_rx_queue == 0) {
1079 		RTE_LOG(INFO, L3FWD_POWER, "lcore %u has nothing to do\n",
1080 			lcore_id);
1081 		return 0;
1082 	}
1083 
1084 	for (i = 0; i < qconf->n_rx_queue; i++) {
1085 		portid = qconf->rx_queue_list[i].port_id;
1086 		queueid = qconf->rx_queue_list[i].queue_id;
1087 		RTE_LOG(INFO, L3FWD_POWER, " -- lcoreid=%u portid=%u "
1088 				"rxqueueid=%hhu\n", lcore_id, portid, queueid);
1089 	}
1090 
1091 	while (!is_done()) {
1092 		stats[lcore_id].nb_iteration_looped++;
1093 
1094 		cur_tsc = rte_rdtsc();
1095 		/*
1096 		 * TX burst queue drain
1097 		 */
1098 		diff_tsc = cur_tsc - prev_tsc;
1099 		if (unlikely(diff_tsc > drain_tsc)) {
1100 			for (i = 0; i < qconf->n_tx_port; ++i) {
1101 				portid = qconf->tx_port_id[i];
1102 				rte_eth_tx_buffer_flush(portid,
1103 						qconf->tx_queue_id[portid],
1104 						qconf->tx_buffer[portid]);
1105 			}
1106 			prev_tsc = cur_tsc;
1107 		}
1108 
1109 		/*
1110 		 * Read packet from RX queues
1111 		 */
1112 		for (i = 0; i < qconf->n_rx_queue; ++i) {
1113 			rx_queue = &(qconf->rx_queue_list[i]);
1114 			rx_queue->idle_hint = 0;
1115 			portid = rx_queue->port_id;
1116 			queueid = rx_queue->queue_id;
1117 
1118 			nb_rx = rte_eth_rx_burst(portid, queueid, pkts_burst,
1119 					MAX_PKT_BURST);
1120 
1121 			stats[lcore_id].nb_rx_processed += nb_rx;
1122 
1123 			if (nb_rx == 0) {
1124 
1125 				rte_power_empty_poll_stat_update(lcore_id);
1126 
1127 				continue;
1128 			} else {
1129 				rte_power_poll_stat_update(lcore_id, nb_rx);
1130 			}
1131 
1132 
1133 			/* Prefetch first packets */
1134 			for (j = 0; j < PREFETCH_OFFSET && j < nb_rx; j++) {
1135 				rte_prefetch0(rte_pktmbuf_mtod(
1136 							pkts_burst[j], void *));
1137 			}
1138 
1139 			/* Prefetch and forward already prefetched packets */
1140 			for (j = 0; j < (nb_rx - PREFETCH_OFFSET); j++) {
1141 				rte_prefetch0(rte_pktmbuf_mtod(pkts_burst[
1142 							j + PREFETCH_OFFSET],
1143 							void *));
1144 				l3fwd_simple_forward(pkts_burst[j], portid,
1145 						qconf);
1146 			}
1147 
1148 			/* Forward remaining prefetched packets */
1149 			for (; j < nb_rx; j++) {
1150 				l3fwd_simple_forward(pkts_burst[j], portid,
1151 						qconf);
1152 			}
1153 
1154 		}
1155 
1156 	}
1157 
1158 	return 0;
1159 }
1160 /* main processing loop */
1161 static int
1162 main_loop(__attribute__((unused)) void *dummy)
1163 {
1164 	struct rte_mbuf *pkts_burst[MAX_PKT_BURST];
1165 	unsigned lcore_id;
1166 	uint64_t prev_tsc, diff_tsc, cur_tsc, tim_res_tsc, hz;
1167 	uint64_t prev_tsc_power = 0, cur_tsc_power, diff_tsc_power;
1168 	int i, j, nb_rx;
1169 	uint8_t queueid;
1170 	uint16_t portid;
1171 	struct lcore_conf *qconf;
1172 	struct lcore_rx_queue *rx_queue;
1173 	enum freq_scale_hint_t lcore_scaleup_hint;
1174 	uint32_t lcore_rx_idle_count = 0;
1175 	uint32_t lcore_idle_hint = 0;
1176 	int intr_en = 0;
1177 
1178 	const uint64_t drain_tsc = (rte_get_tsc_hz() + US_PER_S - 1) / US_PER_S * BURST_TX_DRAIN_US;
1179 
1180 	prev_tsc = 0;
1181 	hz = rte_get_timer_hz();
1182 	tim_res_tsc = hz/TIMER_NUMBER_PER_SECOND;
1183 
1184 	lcore_id = rte_lcore_id();
1185 	qconf = &lcore_conf[lcore_id];
1186 
1187 	if (qconf->n_rx_queue == 0) {
1188 		RTE_LOG(INFO, L3FWD_POWER, "lcore %u has nothing to do\n", lcore_id);
1189 		return 0;
1190 	}
1191 
1192 	RTE_LOG(INFO, L3FWD_POWER, "entering main loop on lcore %u\n", lcore_id);
1193 
1194 	for (i = 0; i < qconf->n_rx_queue; i++) {
1195 		portid = qconf->rx_queue_list[i].port_id;
1196 		queueid = qconf->rx_queue_list[i].queue_id;
1197 		RTE_LOG(INFO, L3FWD_POWER, " -- lcoreid=%u portid=%u "
1198 			"rxqueueid=%hhu\n", lcore_id, portid, queueid);
1199 	}
1200 
1201 	/* add into event wait list */
1202 	if (event_register(qconf) == 0)
1203 		intr_en = 1;
1204 	else
1205 		RTE_LOG(INFO, L3FWD_POWER, "RX interrupt won't enable.\n");
1206 
1207 	while (1) {
1208 		stats[lcore_id].nb_iteration_looped++;
1209 
1210 		cur_tsc = rte_rdtsc();
1211 		cur_tsc_power = cur_tsc;
1212 
1213 		/*
1214 		 * TX burst queue drain
1215 		 */
1216 		diff_tsc = cur_tsc - prev_tsc;
1217 		if (unlikely(diff_tsc > drain_tsc)) {
1218 			for (i = 0; i < qconf->n_tx_port; ++i) {
1219 				portid = qconf->tx_port_id[i];
1220 				rte_eth_tx_buffer_flush(portid,
1221 						qconf->tx_queue_id[portid],
1222 						qconf->tx_buffer[portid]);
1223 			}
1224 			prev_tsc = cur_tsc;
1225 		}
1226 
1227 		diff_tsc_power = cur_tsc_power - prev_tsc_power;
1228 		if (diff_tsc_power > tim_res_tsc) {
1229 			rte_timer_manage();
1230 			prev_tsc_power = cur_tsc_power;
1231 		}
1232 
1233 start_rx:
1234 		/*
1235 		 * Read packet from RX queues
1236 		 */
1237 		lcore_scaleup_hint = FREQ_CURRENT;
1238 		lcore_rx_idle_count = 0;
1239 		for (i = 0; i < qconf->n_rx_queue; ++i) {
1240 			rx_queue = &(qconf->rx_queue_list[i]);
1241 			rx_queue->idle_hint = 0;
1242 			portid = rx_queue->port_id;
1243 			queueid = rx_queue->queue_id;
1244 
1245 			nb_rx = rte_eth_rx_burst(portid, queueid, pkts_burst,
1246 								MAX_PKT_BURST);
1247 
1248 			stats[lcore_id].nb_rx_processed += nb_rx;
1249 			if (unlikely(nb_rx == 0)) {
1250 				/**
1251 				 * no packet received from rx queue, try to
1252 				 * sleep for a while forcing CPU enter deeper
1253 				 * C states.
1254 				 */
1255 				rx_queue->zero_rx_packet_count++;
1256 
1257 				if (rx_queue->zero_rx_packet_count <=
1258 							MIN_ZERO_POLL_COUNT)
1259 					continue;
1260 
1261 				rx_queue->idle_hint = power_idle_heuristic(\
1262 					rx_queue->zero_rx_packet_count);
1263 				lcore_rx_idle_count++;
1264 			} else {
1265 				rx_queue->zero_rx_packet_count = 0;
1266 
1267 				/**
1268 				 * do not scale up frequency immediately as
1269 				 * user to kernel space communication is costly
1270 				 * which might impact packet I/O for received
1271 				 * packets.
1272 				 */
1273 				rx_queue->freq_up_hint =
1274 					power_freq_scaleup_heuristic(lcore_id,
1275 							portid, queueid);
1276 			}
1277 
1278 			/* Prefetch first packets */
1279 			for (j = 0; j < PREFETCH_OFFSET && j < nb_rx; j++) {
1280 				rte_prefetch0(rte_pktmbuf_mtod(
1281 						pkts_burst[j], void *));
1282 			}
1283 
1284 			/* Prefetch and forward already prefetched packets */
1285 			for (j = 0; j < (nb_rx - PREFETCH_OFFSET); j++) {
1286 				rte_prefetch0(rte_pktmbuf_mtod(pkts_burst[
1287 						j + PREFETCH_OFFSET], void *));
1288 				l3fwd_simple_forward(pkts_burst[j], portid,
1289 								qconf);
1290 			}
1291 
1292 			/* Forward remaining prefetched packets */
1293 			for (; j < nb_rx; j++) {
1294 				l3fwd_simple_forward(pkts_burst[j], portid,
1295 								qconf);
1296 			}
1297 		}
1298 
1299 		if (likely(lcore_rx_idle_count != qconf->n_rx_queue)) {
1300 			for (i = 1, lcore_scaleup_hint =
1301 				qconf->rx_queue_list[0].freq_up_hint;
1302 					i < qconf->n_rx_queue; ++i) {
1303 				rx_queue = &(qconf->rx_queue_list[i]);
1304 				if (rx_queue->freq_up_hint >
1305 						lcore_scaleup_hint)
1306 					lcore_scaleup_hint =
1307 						rx_queue->freq_up_hint;
1308 			}
1309 
1310 			if (lcore_scaleup_hint == FREQ_HIGHEST) {
1311 				if (rte_power_freq_max)
1312 					rte_power_freq_max(lcore_id);
1313 			} else if (lcore_scaleup_hint == FREQ_HIGHER) {
1314 				if (rte_power_freq_up)
1315 					rte_power_freq_up(lcore_id);
1316 			}
1317 		} else {
1318 			/**
1319 			 * All Rx queues empty in recent consecutive polls,
1320 			 * sleep in a conservative manner, meaning sleep as
1321 			 * less as possible.
1322 			 */
1323 			for (i = 1, lcore_idle_hint =
1324 				qconf->rx_queue_list[0].idle_hint;
1325 					i < qconf->n_rx_queue; ++i) {
1326 				rx_queue = &(qconf->rx_queue_list[i]);
1327 				if (rx_queue->idle_hint < lcore_idle_hint)
1328 					lcore_idle_hint = rx_queue->idle_hint;
1329 			}
1330 
1331 			if (lcore_idle_hint < SUSPEND_THRESHOLD)
1332 				/**
1333 				 * execute "pause" instruction to avoid context
1334 				 * switch which generally take hundred of
1335 				 * microseconds for short sleep.
1336 				 */
1337 				rte_delay_us(lcore_idle_hint);
1338 			else {
1339 				/* suspend until rx interrupt trigges */
1340 				if (intr_en) {
1341 					turn_on_intr(qconf);
1342 					sleep_until_rx_interrupt(
1343 						qconf->n_rx_queue);
1344 					/**
1345 					 * start receiving packets immediately
1346 					 */
1347 					goto start_rx;
1348 				}
1349 			}
1350 			stats[lcore_id].sleep_time += lcore_idle_hint;
1351 		}
1352 	}
1353 }
1354 
1355 static int
1356 check_lcore_params(void)
1357 {
1358 	uint8_t queue, lcore;
1359 	uint16_t i;
1360 	int socketid;
1361 
1362 	for (i = 0; i < nb_lcore_params; ++i) {
1363 		queue = lcore_params[i].queue_id;
1364 		if (queue >= MAX_RX_QUEUE_PER_PORT) {
1365 			printf("invalid queue number: %hhu\n", queue);
1366 			return -1;
1367 		}
1368 		lcore = lcore_params[i].lcore_id;
1369 		if (!rte_lcore_is_enabled(lcore)) {
1370 			printf("error: lcore %hhu is not enabled in lcore "
1371 							"mask\n", lcore);
1372 			return -1;
1373 		}
1374 		if ((socketid = rte_lcore_to_socket_id(lcore) != 0) &&
1375 							(numa_on == 0)) {
1376 			printf("warning: lcore %hhu is on socket %d with numa "
1377 						"off\n", lcore, socketid);
1378 		}
1379 		if (app_mode == APP_MODE_TELEMETRY && lcore == rte_lcore_id()) {
1380 			printf("cannot enable master core %d in config for telemetry mode\n",
1381 				rte_lcore_id());
1382 			return -1;
1383 		}
1384 	}
1385 	return 0;
1386 }
1387 
1388 static int
1389 check_port_config(void)
1390 {
1391 	unsigned portid;
1392 	uint16_t i;
1393 
1394 	for (i = 0; i < nb_lcore_params; ++i) {
1395 		portid = lcore_params[i].port_id;
1396 		if ((enabled_port_mask & (1 << portid)) == 0) {
1397 			printf("port %u is not enabled in port mask\n",
1398 								portid);
1399 			return -1;
1400 		}
1401 		if (!rte_eth_dev_is_valid_port(portid)) {
1402 			printf("port %u is not present on the board\n",
1403 								portid);
1404 			return -1;
1405 		}
1406 	}
1407 	return 0;
1408 }
1409 
1410 static uint8_t
1411 get_port_n_rx_queues(const uint16_t port)
1412 {
1413 	int queue = -1;
1414 	uint16_t i;
1415 
1416 	for (i = 0; i < nb_lcore_params; ++i) {
1417 		if (lcore_params[i].port_id == port &&
1418 				lcore_params[i].queue_id > queue)
1419 			queue = lcore_params[i].queue_id;
1420 	}
1421 	return (uint8_t)(++queue);
1422 }
1423 
1424 static int
1425 init_lcore_rx_queues(void)
1426 {
1427 	uint16_t i, nb_rx_queue;
1428 	uint8_t lcore;
1429 
1430 	for (i = 0; i < nb_lcore_params; ++i) {
1431 		lcore = lcore_params[i].lcore_id;
1432 		nb_rx_queue = lcore_conf[lcore].n_rx_queue;
1433 		if (nb_rx_queue >= MAX_RX_QUEUE_PER_LCORE) {
1434 			printf("error: too many queues (%u) for lcore: %u\n",
1435 				(unsigned)nb_rx_queue + 1, (unsigned)lcore);
1436 			return -1;
1437 		} else {
1438 			lcore_conf[lcore].rx_queue_list[nb_rx_queue].port_id =
1439 				lcore_params[i].port_id;
1440 			lcore_conf[lcore].rx_queue_list[nb_rx_queue].queue_id =
1441 				lcore_params[i].queue_id;
1442 			lcore_conf[lcore].n_rx_queue++;
1443 		}
1444 	}
1445 	return 0;
1446 }
1447 
1448 /* display usage */
1449 static void
1450 print_usage(const char *prgname)
1451 {
1452 	printf ("%s [EAL options] -- -p PORTMASK -P"
1453 		"  [--config (port,queue,lcore)[,(port,queue,lcore]]"
1454 		"  [--high-perf-cores CORELIST"
1455 		"  [--perf-config (port,queue,hi_perf,lcore_index)[,(port,queue,hi_perf,lcore_index]]"
1456 		"  [--enable-jumbo [--max-pkt-len PKTLEN]]\n"
1457 		"  -p PORTMASK: hexadecimal bitmask of ports to configure\n"
1458 		"  -P : enable promiscuous mode\n"
1459 		"  --config (port,queue,lcore): rx queues configuration\n"
1460 		"  --high-perf-cores CORELIST: list of high performance cores\n"
1461 		"  --perf-config: similar as config, cores specified as indices"
1462 		" for bins containing high or regular performance cores\n"
1463 		"  --no-numa: optional, disable numa awareness\n"
1464 		"  --enable-jumbo: enable jumbo frame"
1465 		" which max packet len is PKTLEN in decimal (64-9600)\n"
1466 		"  --parse-ptype: parse packet type by software\n"
1467 		"  --empty-poll: enable empty poll detection"
1468 		" follow (training_flag, high_threshold, med_threshold)\n"
1469 		" --telemetry: enable telemetry mode, to update"
1470 		" empty polls, full polls, and core busyness to telemetry\n",
1471 		prgname);
1472 }
1473 
1474 static int parse_max_pkt_len(const char *pktlen)
1475 {
1476 	char *end = NULL;
1477 	unsigned long len;
1478 
1479 	/* parse decimal string */
1480 	len = strtoul(pktlen, &end, 10);
1481 	if ((pktlen[0] == '\0') || (end == NULL) || (*end != '\0'))
1482 		return -1;
1483 
1484 	if (len == 0)
1485 		return -1;
1486 
1487 	return len;
1488 }
1489 
1490 static int
1491 parse_portmask(const char *portmask)
1492 {
1493 	char *end = NULL;
1494 	unsigned long pm;
1495 
1496 	/* parse hexadecimal string */
1497 	pm = strtoul(portmask, &end, 16);
1498 	if ((portmask[0] == '\0') || (end == NULL) || (*end != '\0'))
1499 		return -1;
1500 
1501 	if (pm == 0)
1502 		return -1;
1503 
1504 	return pm;
1505 }
1506 
1507 static int
1508 parse_config(const char *q_arg)
1509 {
1510 	char s[256];
1511 	const char *p, *p0 = q_arg;
1512 	char *end;
1513 	enum fieldnames {
1514 		FLD_PORT = 0,
1515 		FLD_QUEUE,
1516 		FLD_LCORE,
1517 		_NUM_FLD
1518 	};
1519 	unsigned long int_fld[_NUM_FLD];
1520 	char *str_fld[_NUM_FLD];
1521 	int i;
1522 	unsigned size;
1523 
1524 	nb_lcore_params = 0;
1525 
1526 	while ((p = strchr(p0,'(')) != NULL) {
1527 		++p;
1528 		if((p0 = strchr(p,')')) == NULL)
1529 			return -1;
1530 
1531 		size = p0 - p;
1532 		if(size >= sizeof(s))
1533 			return -1;
1534 
1535 		snprintf(s, sizeof(s), "%.*s", size, p);
1536 		if (rte_strsplit(s, sizeof(s), str_fld, _NUM_FLD, ',') !=
1537 								_NUM_FLD)
1538 			return -1;
1539 		for (i = 0; i < _NUM_FLD; i++){
1540 			errno = 0;
1541 			int_fld[i] = strtoul(str_fld[i], &end, 0);
1542 			if (errno != 0 || end == str_fld[i] || int_fld[i] >
1543 									255)
1544 				return -1;
1545 		}
1546 		if (nb_lcore_params >= MAX_LCORE_PARAMS) {
1547 			printf("exceeded max number of lcore params: %hu\n",
1548 				nb_lcore_params);
1549 			return -1;
1550 		}
1551 		lcore_params_array[nb_lcore_params].port_id =
1552 				(uint8_t)int_fld[FLD_PORT];
1553 		lcore_params_array[nb_lcore_params].queue_id =
1554 				(uint8_t)int_fld[FLD_QUEUE];
1555 		lcore_params_array[nb_lcore_params].lcore_id =
1556 				(uint8_t)int_fld[FLD_LCORE];
1557 		++nb_lcore_params;
1558 	}
1559 	lcore_params = lcore_params_array;
1560 
1561 	return 0;
1562 }
1563 static int
1564 parse_ep_config(const char *q_arg)
1565 {
1566 	char s[256];
1567 	const char *p = q_arg;
1568 	char *end;
1569 	int  num_arg;
1570 
1571 	char *str_fld[3];
1572 
1573 	int training_flag;
1574 	int med_edpi;
1575 	int hgh_edpi;
1576 
1577 	ep_med_edpi = EMPTY_POLL_MED_THRESHOLD;
1578 	ep_hgh_edpi = EMPTY_POLL_MED_THRESHOLD;
1579 
1580 	strlcpy(s, p, sizeof(s));
1581 
1582 	num_arg = rte_strsplit(s, sizeof(s), str_fld, 3, ',');
1583 
1584 	empty_poll_train = false;
1585 
1586 	if (num_arg == 0)
1587 		return 0;
1588 
1589 	if (num_arg == 3) {
1590 
1591 		training_flag = strtoul(str_fld[0], &end, 0);
1592 		med_edpi = strtoul(str_fld[1], &end, 0);
1593 		hgh_edpi = strtoul(str_fld[2], &end, 0);
1594 
1595 		if (training_flag == 1)
1596 			empty_poll_train = true;
1597 
1598 		if (med_edpi > 0)
1599 			ep_med_edpi = med_edpi;
1600 
1601 		if (med_edpi > 0)
1602 			ep_hgh_edpi = hgh_edpi;
1603 
1604 	} else {
1605 
1606 		return -1;
1607 	}
1608 
1609 	return 0;
1610 
1611 }
1612 #define CMD_LINE_OPT_PARSE_PTYPE "parse-ptype"
1613 #define CMD_LINE_OPT_TELEMETRY "telemetry"
1614 
1615 /* Parse the argument given in the command line of the application */
1616 static int
1617 parse_args(int argc, char **argv)
1618 {
1619 	int opt, ret;
1620 	char **argvopt;
1621 	int option_index;
1622 	uint32_t limit;
1623 	char *prgname = argv[0];
1624 	static struct option lgopts[] = {
1625 		{"config", 1, 0, 0},
1626 		{"perf-config", 1, 0, 0},
1627 		{"high-perf-cores", 1, 0, 0},
1628 		{"no-numa", 0, 0, 0},
1629 		{"enable-jumbo", 0, 0, 0},
1630 		{"empty-poll", 1, 0, 0},
1631 		{CMD_LINE_OPT_PARSE_PTYPE, 0, 0, 0},
1632 		{CMD_LINE_OPT_TELEMETRY, 0, 0, 0},
1633 		{NULL, 0, 0, 0}
1634 	};
1635 
1636 	argvopt = argv;
1637 
1638 	while ((opt = getopt_long(argc, argvopt, "p:l:m:h:P",
1639 				lgopts, &option_index)) != EOF) {
1640 
1641 		switch (opt) {
1642 		/* portmask */
1643 		case 'p':
1644 			enabled_port_mask = parse_portmask(optarg);
1645 			if (enabled_port_mask == 0) {
1646 				printf("invalid portmask\n");
1647 				print_usage(prgname);
1648 				return -1;
1649 			}
1650 			break;
1651 		case 'P':
1652 			printf("Promiscuous mode selected\n");
1653 			promiscuous_on = 1;
1654 			break;
1655 		case 'l':
1656 			limit = parse_max_pkt_len(optarg);
1657 			freq_tlb[LOW] = limit;
1658 			break;
1659 		case 'm':
1660 			limit = parse_max_pkt_len(optarg);
1661 			freq_tlb[MED] = limit;
1662 			break;
1663 		case 'h':
1664 			limit = parse_max_pkt_len(optarg);
1665 			freq_tlb[HGH] = limit;
1666 			break;
1667 		/* long options */
1668 		case 0:
1669 			if (!strncmp(lgopts[option_index].name, "config", 6)) {
1670 				ret = parse_config(optarg);
1671 				if (ret) {
1672 					printf("invalid config\n");
1673 					print_usage(prgname);
1674 					return -1;
1675 				}
1676 			}
1677 
1678 			if (!strncmp(lgopts[option_index].name,
1679 					"perf-config", 11)) {
1680 				ret = parse_perf_config(optarg);
1681 				if (ret) {
1682 					printf("invalid perf-config\n");
1683 					print_usage(prgname);
1684 					return -1;
1685 				}
1686 			}
1687 
1688 			if (!strncmp(lgopts[option_index].name,
1689 					"high-perf-cores", 15)) {
1690 				ret = parse_perf_core_list(optarg);
1691 				if (ret) {
1692 					printf("invalid high-perf-cores\n");
1693 					print_usage(prgname);
1694 					return -1;
1695 				}
1696 			}
1697 
1698 			if (!strncmp(lgopts[option_index].name,
1699 						"no-numa", 7)) {
1700 				printf("numa is disabled \n");
1701 				numa_on = 0;
1702 			}
1703 
1704 			if (!strncmp(lgopts[option_index].name,
1705 						"empty-poll", 10)) {
1706 				if (app_mode == APP_MODE_TELEMETRY) {
1707 					printf(" empty-poll cannot be enabled as telemetry mode is enabled\n");
1708 					return -1;
1709 				}
1710 				app_mode = APP_MODE_EMPTY_POLL;
1711 				ret = parse_ep_config(optarg);
1712 
1713 				if (ret) {
1714 					printf("invalid empty poll config\n");
1715 					print_usage(prgname);
1716 					return -1;
1717 				}
1718 				printf("empty-poll is enabled\n");
1719 			}
1720 
1721 			if (!strncmp(lgopts[option_index].name,
1722 					CMD_LINE_OPT_TELEMETRY,
1723 					sizeof(CMD_LINE_OPT_TELEMETRY))) {
1724 				if (app_mode == APP_MODE_EMPTY_POLL) {
1725 					printf("telemetry mode cannot be enabled as empty poll mode is enabled\n");
1726 					return -1;
1727 				}
1728 				app_mode = APP_MODE_TELEMETRY;
1729 				printf("telemetry mode is enabled\n");
1730 			}
1731 
1732 			if (!strncmp(lgopts[option_index].name,
1733 					"enable-jumbo", 12)) {
1734 				struct option lenopts =
1735 					{"max-pkt-len", required_argument, \
1736 									0, 0};
1737 
1738 				printf("jumbo frame is enabled \n");
1739 				port_conf.rxmode.offloads |=
1740 						DEV_RX_OFFLOAD_JUMBO_FRAME;
1741 				port_conf.txmode.offloads |=
1742 						DEV_TX_OFFLOAD_MULTI_SEGS;
1743 
1744 				/**
1745 				 * if no max-pkt-len set, use the default value
1746 				 * RTE_ETHER_MAX_LEN
1747 				 */
1748 				if (0 == getopt_long(argc, argvopt, "",
1749 						&lenopts, &option_index)) {
1750 					ret = parse_max_pkt_len(optarg);
1751 					if ((ret < 64) ||
1752 						(ret > MAX_JUMBO_PKT_LEN)){
1753 						printf("invalid packet "
1754 								"length\n");
1755 						print_usage(prgname);
1756 						return -1;
1757 					}
1758 					port_conf.rxmode.max_rx_pkt_len = ret;
1759 				}
1760 				printf("set jumbo frame "
1761 					"max packet length to %u\n",
1762 				(unsigned int)port_conf.rxmode.max_rx_pkt_len);
1763 			}
1764 
1765 			if (!strncmp(lgopts[option_index].name,
1766 				     CMD_LINE_OPT_PARSE_PTYPE,
1767 				     sizeof(CMD_LINE_OPT_PARSE_PTYPE))) {
1768 				printf("soft parse-ptype is enabled\n");
1769 				parse_ptype = 1;
1770 			}
1771 
1772 			break;
1773 
1774 		default:
1775 			print_usage(prgname);
1776 			return -1;
1777 		}
1778 	}
1779 
1780 	if (optind >= 0)
1781 		argv[optind-1] = prgname;
1782 
1783 	ret = optind-1;
1784 	optind = 1; /* reset getopt lib */
1785 	return ret;
1786 }
1787 
1788 static void
1789 print_ethaddr(const char *name, const struct rte_ether_addr *eth_addr)
1790 {
1791 	char buf[RTE_ETHER_ADDR_FMT_SIZE];
1792 	rte_ether_format_addr(buf, RTE_ETHER_ADDR_FMT_SIZE, eth_addr);
1793 	printf("%s%s", name, buf);
1794 }
1795 
1796 #if (APP_LOOKUP_METHOD == APP_LOOKUP_EXACT_MATCH)
1797 static void
1798 setup_hash(int socketid)
1799 {
1800 	struct rte_hash_parameters ipv4_l3fwd_hash_params = {
1801 		.name = NULL,
1802 		.entries = L3FWD_HASH_ENTRIES,
1803 		.key_len = sizeof(struct ipv4_5tuple),
1804 		.hash_func = DEFAULT_HASH_FUNC,
1805 		.hash_func_init_val = 0,
1806 	};
1807 
1808 	struct rte_hash_parameters ipv6_l3fwd_hash_params = {
1809 		.name = NULL,
1810 		.entries = L3FWD_HASH_ENTRIES,
1811 		.key_len = sizeof(struct ipv6_5tuple),
1812 		.hash_func = DEFAULT_HASH_FUNC,
1813 		.hash_func_init_val = 0,
1814 	};
1815 
1816 	unsigned i;
1817 	int ret;
1818 	char s[64];
1819 
1820 	/* create ipv4 hash */
1821 	snprintf(s, sizeof(s), "ipv4_l3fwd_hash_%d", socketid);
1822 	ipv4_l3fwd_hash_params.name = s;
1823 	ipv4_l3fwd_hash_params.socket_id = socketid;
1824 	ipv4_l3fwd_lookup_struct[socketid] =
1825 		rte_hash_create(&ipv4_l3fwd_hash_params);
1826 	if (ipv4_l3fwd_lookup_struct[socketid] == NULL)
1827 		rte_exit(EXIT_FAILURE, "Unable to create the l3fwd hash on "
1828 				"socket %d\n", socketid);
1829 
1830 	/* create ipv6 hash */
1831 	snprintf(s, sizeof(s), "ipv6_l3fwd_hash_%d", socketid);
1832 	ipv6_l3fwd_hash_params.name = s;
1833 	ipv6_l3fwd_hash_params.socket_id = socketid;
1834 	ipv6_l3fwd_lookup_struct[socketid] =
1835 		rte_hash_create(&ipv6_l3fwd_hash_params);
1836 	if (ipv6_l3fwd_lookup_struct[socketid] == NULL)
1837 		rte_exit(EXIT_FAILURE, "Unable to create the l3fwd hash on "
1838 				"socket %d\n", socketid);
1839 
1840 
1841 	/* populate the ipv4 hash */
1842 	for (i = 0; i < IPV4_L3FWD_NUM_ROUTES; i++) {
1843 		ret = rte_hash_add_key (ipv4_l3fwd_lookup_struct[socketid],
1844 				(void *) &ipv4_l3fwd_route_array[i].key);
1845 		if (ret < 0) {
1846 			rte_exit(EXIT_FAILURE, "Unable to add entry %u to the"
1847 				"l3fwd hash on socket %d\n", i, socketid);
1848 		}
1849 		ipv4_l3fwd_out_if[ret] = ipv4_l3fwd_route_array[i].if_out;
1850 		printf("Hash: Adding key\n");
1851 		print_ipv4_key(ipv4_l3fwd_route_array[i].key);
1852 	}
1853 
1854 	/* populate the ipv6 hash */
1855 	for (i = 0; i < IPV6_L3FWD_NUM_ROUTES; i++) {
1856 		ret = rte_hash_add_key (ipv6_l3fwd_lookup_struct[socketid],
1857 				(void *) &ipv6_l3fwd_route_array[i].key);
1858 		if (ret < 0) {
1859 			rte_exit(EXIT_FAILURE, "Unable to add entry %u to the"
1860 				"l3fwd hash on socket %d\n", i, socketid);
1861 		}
1862 		ipv6_l3fwd_out_if[ret] = ipv6_l3fwd_route_array[i].if_out;
1863 		printf("Hash: Adding key\n");
1864 		print_ipv6_key(ipv6_l3fwd_route_array[i].key);
1865 	}
1866 }
1867 #endif
1868 
1869 #if (APP_LOOKUP_METHOD == APP_LOOKUP_LPM)
1870 static void
1871 setup_lpm(int socketid)
1872 {
1873 	unsigned i;
1874 	int ret;
1875 	char s[64];
1876 
1877 	/* create the LPM table */
1878 	struct rte_lpm_config lpm_ipv4_config;
1879 
1880 	lpm_ipv4_config.max_rules = IPV4_L3FWD_LPM_MAX_RULES;
1881 	lpm_ipv4_config.number_tbl8s = 256;
1882 	lpm_ipv4_config.flags = 0;
1883 
1884 	snprintf(s, sizeof(s), "IPV4_L3FWD_LPM_%d", socketid);
1885 	ipv4_l3fwd_lookup_struct[socketid] =
1886 			rte_lpm_create(s, socketid, &lpm_ipv4_config);
1887 	if (ipv4_l3fwd_lookup_struct[socketid] == NULL)
1888 		rte_exit(EXIT_FAILURE, "Unable to create the l3fwd LPM table"
1889 				" on socket %d\n", socketid);
1890 
1891 	/* populate the LPM table */
1892 	for (i = 0; i < IPV4_L3FWD_NUM_ROUTES; i++) {
1893 		ret = rte_lpm_add(ipv4_l3fwd_lookup_struct[socketid],
1894 			ipv4_l3fwd_route_array[i].ip,
1895 			ipv4_l3fwd_route_array[i].depth,
1896 			ipv4_l3fwd_route_array[i].if_out);
1897 
1898 		if (ret < 0) {
1899 			rte_exit(EXIT_FAILURE, "Unable to add entry %u to the "
1900 				"l3fwd LPM table on socket %d\n",
1901 				i, socketid);
1902 		}
1903 
1904 		printf("LPM: Adding route 0x%08x / %d (%d)\n",
1905 			(unsigned)ipv4_l3fwd_route_array[i].ip,
1906 			ipv4_l3fwd_route_array[i].depth,
1907 			ipv4_l3fwd_route_array[i].if_out);
1908 	}
1909 }
1910 #endif
1911 
1912 static int
1913 init_mem(unsigned nb_mbuf)
1914 {
1915 	struct lcore_conf *qconf;
1916 	int socketid;
1917 	unsigned lcore_id;
1918 	char s[64];
1919 
1920 	for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
1921 		if (rte_lcore_is_enabled(lcore_id) == 0)
1922 			continue;
1923 
1924 		if (numa_on)
1925 			socketid = rte_lcore_to_socket_id(lcore_id);
1926 		else
1927 			socketid = 0;
1928 
1929 		if (socketid >= NB_SOCKETS) {
1930 			rte_exit(EXIT_FAILURE, "Socket %d of lcore %u is "
1931 					"out of range %d\n", socketid,
1932 						lcore_id, NB_SOCKETS);
1933 		}
1934 		if (pktmbuf_pool[socketid] == NULL) {
1935 			snprintf(s, sizeof(s), "mbuf_pool_%d", socketid);
1936 			pktmbuf_pool[socketid] =
1937 				rte_pktmbuf_pool_create(s, nb_mbuf,
1938 					MEMPOOL_CACHE_SIZE, 0,
1939 					RTE_MBUF_DEFAULT_BUF_SIZE,
1940 					socketid);
1941 			if (pktmbuf_pool[socketid] == NULL)
1942 				rte_exit(EXIT_FAILURE,
1943 					"Cannot init mbuf pool on socket %d\n",
1944 								socketid);
1945 			else
1946 				printf("Allocated mbuf pool on socket %d\n",
1947 								socketid);
1948 
1949 #if (APP_LOOKUP_METHOD == APP_LOOKUP_LPM)
1950 			setup_lpm(socketid);
1951 #else
1952 			setup_hash(socketid);
1953 #endif
1954 		}
1955 		qconf = &lcore_conf[lcore_id];
1956 		qconf->ipv4_lookup_struct = ipv4_l3fwd_lookup_struct[socketid];
1957 #if (APP_LOOKUP_METHOD == APP_LOOKUP_EXACT_MATCH)
1958 		qconf->ipv6_lookup_struct = ipv6_l3fwd_lookup_struct[socketid];
1959 #endif
1960 	}
1961 	return 0;
1962 }
1963 
1964 /* Check the link status of all ports in up to 9s, and print them finally */
1965 static void
1966 check_all_ports_link_status(uint32_t port_mask)
1967 {
1968 #define CHECK_INTERVAL 100 /* 100ms */
1969 #define MAX_CHECK_TIME 90 /* 9s (90 * 100ms) in total */
1970 	uint8_t count, all_ports_up, print_flag = 0;
1971 	uint16_t portid;
1972 	struct rte_eth_link link;
1973 	int ret;
1974 
1975 	printf("\nChecking link status");
1976 	fflush(stdout);
1977 	for (count = 0; count <= MAX_CHECK_TIME; count++) {
1978 		all_ports_up = 1;
1979 		RTE_ETH_FOREACH_DEV(portid) {
1980 			if ((port_mask & (1 << portid)) == 0)
1981 				continue;
1982 			memset(&link, 0, sizeof(link));
1983 			ret = rte_eth_link_get_nowait(portid, &link);
1984 			if (ret < 0) {
1985 				all_ports_up = 0;
1986 				if (print_flag == 1)
1987 					printf("Port %u link get failed: %s\n",
1988 						portid, rte_strerror(-ret));
1989 				continue;
1990 			}
1991 			/* print link status if flag set */
1992 			if (print_flag == 1) {
1993 				if (link.link_status)
1994 					printf("Port %d Link Up - speed %u "
1995 						"Mbps - %s\n", (uint8_t)portid,
1996 						(unsigned)link.link_speed,
1997 				(link.link_duplex == ETH_LINK_FULL_DUPLEX) ?
1998 					("full-duplex") : ("half-duplex\n"));
1999 				else
2000 					printf("Port %d Link Down\n",
2001 						(uint8_t)portid);
2002 				continue;
2003 			}
2004 			/* clear all_ports_up flag if any link down */
2005 			if (link.link_status == ETH_LINK_DOWN) {
2006 				all_ports_up = 0;
2007 				break;
2008 			}
2009 		}
2010 		/* after finally printing all link status, get out */
2011 		if (print_flag == 1)
2012 			break;
2013 
2014 		if (all_ports_up == 0) {
2015 			printf(".");
2016 			fflush(stdout);
2017 			rte_delay_ms(CHECK_INTERVAL);
2018 		}
2019 
2020 		/* set the print_flag if all ports up or timeout */
2021 		if (all_ports_up == 1 || count == (MAX_CHECK_TIME - 1)) {
2022 			print_flag = 1;
2023 			printf("done\n");
2024 		}
2025 	}
2026 }
2027 
2028 static int check_ptype(uint16_t portid)
2029 {
2030 	int i, ret;
2031 	int ptype_l3_ipv4 = 0;
2032 #if (APP_LOOKUP_METHOD == APP_LOOKUP_EXACT_MATCH)
2033 	int ptype_l3_ipv6 = 0;
2034 #endif
2035 	uint32_t ptype_mask = RTE_PTYPE_L3_MASK;
2036 
2037 	ret = rte_eth_dev_get_supported_ptypes(portid, ptype_mask, NULL, 0);
2038 	if (ret <= 0)
2039 		return 0;
2040 
2041 	uint32_t ptypes[ret];
2042 
2043 	ret = rte_eth_dev_get_supported_ptypes(portid, ptype_mask, ptypes, ret);
2044 	for (i = 0; i < ret; ++i) {
2045 		if (ptypes[i] & RTE_PTYPE_L3_IPV4)
2046 			ptype_l3_ipv4 = 1;
2047 #if (APP_LOOKUP_METHOD == APP_LOOKUP_EXACT_MATCH)
2048 		if (ptypes[i] & RTE_PTYPE_L3_IPV6)
2049 			ptype_l3_ipv6 = 1;
2050 #endif
2051 	}
2052 
2053 	if (ptype_l3_ipv4 == 0)
2054 		printf("port %d cannot parse RTE_PTYPE_L3_IPV4\n", portid);
2055 
2056 #if (APP_LOOKUP_METHOD == APP_LOOKUP_EXACT_MATCH)
2057 	if (ptype_l3_ipv6 == 0)
2058 		printf("port %d cannot parse RTE_PTYPE_L3_IPV6\n", portid);
2059 #endif
2060 
2061 #if (APP_LOOKUP_METHOD == APP_LOOKUP_LPM)
2062 	if (ptype_l3_ipv4)
2063 #else /* APP_LOOKUP_EXACT_MATCH */
2064 	if (ptype_l3_ipv4 && ptype_l3_ipv6)
2065 #endif
2066 		return 1;
2067 
2068 	return 0;
2069 
2070 }
2071 
2072 static int
2073 init_power_library(void)
2074 {
2075 	int ret = 0, lcore_id;
2076 	for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
2077 		if (rte_lcore_is_enabled(lcore_id)) {
2078 			/* init power management library */
2079 			ret = rte_power_init(lcore_id);
2080 			if (ret)
2081 				RTE_LOG(ERR, POWER,
2082 				"Library initialization failed on core %u\n",
2083 				lcore_id);
2084 		}
2085 	}
2086 	return ret;
2087 }
2088 static void
2089 update_telemetry(__attribute__((unused)) struct rte_timer *tim,
2090 		__attribute__((unused)) void *arg)
2091 {
2092 	unsigned int lcore_id = rte_lcore_id();
2093 	struct lcore_conf *qconf;
2094 	uint64_t app_eps = 0, app_fps = 0, app_br = 0;
2095 	uint64_t values[3] = {0};
2096 	int ret;
2097 	uint64_t count = 0;
2098 
2099 	RTE_LCORE_FOREACH_SLAVE(lcore_id) {
2100 		qconf = &lcore_conf[lcore_id];
2101 		if (qconf->n_rx_queue == 0)
2102 			continue;
2103 		count++;
2104 		rte_spinlock_lock(&stats[lcore_id].telemetry_lock);
2105 		app_eps += stats[lcore_id].ep_nep[1];
2106 		app_fps += stats[lcore_id].fp_nfp[1];
2107 		app_br += stats[lcore_id].br;
2108 		rte_spinlock_unlock(&stats[lcore_id].telemetry_lock);
2109 	}
2110 
2111 	if (count > 0) {
2112 		values[0] = app_eps/count;
2113 		values[1] = app_fps/count;
2114 		values[2] = app_br/count;
2115 	} else {
2116 		values[0] = 0;
2117 		values[1] = 0;
2118 		values[2] = 0;
2119 	}
2120 
2121 	ret = rte_metrics_update_values(RTE_METRICS_GLOBAL, telstats_index,
2122 					values, RTE_DIM(values));
2123 	if (ret < 0)
2124 		RTE_LOG(WARNING, POWER, "failed to update metrcis\n");
2125 }
2126 static void
2127 telemetry_setup_timer(void)
2128 {
2129 	int lcore_id = rte_lcore_id();
2130 	uint64_t hz = rte_get_timer_hz();
2131 	uint64_t ticks;
2132 
2133 	ticks = hz / TELEMETRY_INTERVALS_PER_SEC;
2134 	rte_timer_reset_sync(&telemetry_timer,
2135 			ticks,
2136 			PERIODICAL,
2137 			lcore_id,
2138 			update_telemetry,
2139 			NULL);
2140 }
2141 static void
2142 empty_poll_setup_timer(void)
2143 {
2144 	int lcore_id = rte_lcore_id();
2145 	uint64_t hz = rte_get_timer_hz();
2146 
2147 	struct  ep_params *ep_ptr = ep_params;
2148 
2149 	ep_ptr->interval_ticks = hz / INTERVALS_PER_SECOND;
2150 
2151 	rte_timer_reset_sync(&ep_ptr->timer0,
2152 			ep_ptr->interval_ticks,
2153 			PERIODICAL,
2154 			lcore_id,
2155 			rte_empty_poll_detection,
2156 			(void *)ep_ptr);
2157 
2158 }
2159 static int
2160 launch_timer(unsigned int lcore_id)
2161 {
2162 	int64_t prev_tsc = 0, cur_tsc, diff_tsc, cycles_10ms;
2163 
2164 	RTE_SET_USED(lcore_id);
2165 
2166 
2167 	if (rte_get_master_lcore() != lcore_id) {
2168 		rte_panic("timer on lcore:%d which is not master core:%d\n",
2169 				lcore_id,
2170 				rte_get_master_lcore());
2171 	}
2172 
2173 	RTE_LOG(INFO, POWER, "Bring up the Timer\n");
2174 
2175 	if (app_mode == APP_MODE_EMPTY_POLL)
2176 		empty_poll_setup_timer();
2177 	else
2178 		telemetry_setup_timer();
2179 
2180 	cycles_10ms = rte_get_timer_hz() / 100;
2181 
2182 	while (!is_done()) {
2183 		cur_tsc = rte_rdtsc();
2184 		diff_tsc = cur_tsc - prev_tsc;
2185 		if (diff_tsc > cycles_10ms) {
2186 			rte_timer_manage();
2187 			prev_tsc = cur_tsc;
2188 			cycles_10ms = rte_get_timer_hz() / 100;
2189 		}
2190 	}
2191 
2192 	RTE_LOG(INFO, POWER, "Timer_subsystem is done\n");
2193 
2194 	return 0;
2195 }
2196 
2197 
2198 int
2199 main(int argc, char **argv)
2200 {
2201 	struct lcore_conf *qconf;
2202 	struct rte_eth_dev_info dev_info;
2203 	struct rte_eth_txconf *txconf;
2204 	int ret;
2205 	uint16_t nb_ports;
2206 	uint16_t queueid;
2207 	unsigned lcore_id;
2208 	uint64_t hz;
2209 	uint32_t n_tx_queue, nb_lcores;
2210 	uint32_t dev_rxq_num, dev_txq_num;
2211 	uint8_t nb_rx_queue, queue, socketid;
2212 	uint16_t portid;
2213 	uint8_t num_telstats = RTE_DIM(telstats_strings);
2214 	const char *ptr_strings[num_telstats];
2215 
2216 	/* catch SIGINT and restore cpufreq governor to ondemand */
2217 	signal(SIGINT, signal_exit_now);
2218 
2219 	/* init EAL */
2220 	ret = rte_eal_init(argc, argv);
2221 	if (ret < 0)
2222 		rte_exit(EXIT_FAILURE, "Invalid EAL parameters\n");
2223 	argc -= ret;
2224 	argv += ret;
2225 
2226 	/* init RTE timer library to be used late */
2227 	rte_timer_subsystem_init();
2228 
2229 	/* parse application arguments (after the EAL ones) */
2230 	ret = parse_args(argc, argv);
2231 	if (ret < 0)
2232 		rte_exit(EXIT_FAILURE, "Invalid L3FWD parameters\n");
2233 
2234 	if (init_power_library())
2235 		RTE_LOG(ERR, L3FWD_POWER, "init_power_library failed\n");
2236 
2237 	if (update_lcore_params() < 0)
2238 		rte_exit(EXIT_FAILURE, "update_lcore_params failed\n");
2239 
2240 	if (check_lcore_params() < 0)
2241 		rte_exit(EXIT_FAILURE, "check_lcore_params failed\n");
2242 
2243 	ret = init_lcore_rx_queues();
2244 	if (ret < 0)
2245 		rte_exit(EXIT_FAILURE, "init_lcore_rx_queues failed\n");
2246 
2247 	nb_ports = rte_eth_dev_count_avail();
2248 
2249 	if (check_port_config() < 0)
2250 		rte_exit(EXIT_FAILURE, "check_port_config failed\n");
2251 
2252 	nb_lcores = rte_lcore_count();
2253 
2254 	/* initialize all ports */
2255 	RTE_ETH_FOREACH_DEV(portid) {
2256 		struct rte_eth_conf local_port_conf = port_conf;
2257 
2258 		/* skip ports that are not enabled */
2259 		if ((enabled_port_mask & (1 << portid)) == 0) {
2260 			printf("\nSkipping disabled port %d\n", portid);
2261 			continue;
2262 		}
2263 
2264 		/* init port */
2265 		printf("Initializing port %d ... ", portid );
2266 		fflush(stdout);
2267 
2268 		ret = rte_eth_dev_info_get(portid, &dev_info);
2269 		if (ret != 0)
2270 			rte_exit(EXIT_FAILURE,
2271 				"Error during getting device (port %u) info: %s\n",
2272 				portid, strerror(-ret));
2273 
2274 		dev_rxq_num = dev_info.max_rx_queues;
2275 		dev_txq_num = dev_info.max_tx_queues;
2276 
2277 		nb_rx_queue = get_port_n_rx_queues(portid);
2278 		if (nb_rx_queue > dev_rxq_num)
2279 			rte_exit(EXIT_FAILURE,
2280 				"Cannot configure not existed rxq: "
2281 				"port=%d\n", portid);
2282 
2283 		n_tx_queue = nb_lcores;
2284 		if (n_tx_queue > dev_txq_num)
2285 			n_tx_queue = dev_txq_num;
2286 		printf("Creating queues: nb_rxq=%d nb_txq=%u... ",
2287 			nb_rx_queue, (unsigned)n_tx_queue );
2288 		/* If number of Rx queue is 0, no need to enable Rx interrupt */
2289 		if (nb_rx_queue == 0)
2290 			local_port_conf.intr_conf.rxq = 0;
2291 
2292 		ret = rte_eth_dev_info_get(portid, &dev_info);
2293 		if (ret != 0)
2294 			rte_exit(EXIT_FAILURE,
2295 				"Error during getting device (port %u) info: %s\n",
2296 				portid, strerror(-ret));
2297 
2298 		if (dev_info.tx_offload_capa & DEV_TX_OFFLOAD_MBUF_FAST_FREE)
2299 			local_port_conf.txmode.offloads |=
2300 				DEV_TX_OFFLOAD_MBUF_FAST_FREE;
2301 
2302 		local_port_conf.rx_adv_conf.rss_conf.rss_hf &=
2303 			dev_info.flow_type_rss_offloads;
2304 		if (local_port_conf.rx_adv_conf.rss_conf.rss_hf !=
2305 				port_conf.rx_adv_conf.rss_conf.rss_hf) {
2306 			printf("Port %u modified RSS hash function based on hardware support,"
2307 				"requested:%#"PRIx64" configured:%#"PRIx64"\n",
2308 				portid,
2309 				port_conf.rx_adv_conf.rss_conf.rss_hf,
2310 				local_port_conf.rx_adv_conf.rss_conf.rss_hf);
2311 		}
2312 
2313 		ret = rte_eth_dev_configure(portid, nb_rx_queue,
2314 					(uint16_t)n_tx_queue, &local_port_conf);
2315 		if (ret < 0)
2316 			rte_exit(EXIT_FAILURE, "Cannot configure device: "
2317 					"err=%d, port=%d\n", ret, portid);
2318 
2319 		ret = rte_eth_dev_adjust_nb_rx_tx_desc(portid, &nb_rxd,
2320 						       &nb_txd);
2321 		if (ret < 0)
2322 			rte_exit(EXIT_FAILURE,
2323 				 "Cannot adjust number of descriptors: err=%d, port=%d\n",
2324 				 ret, portid);
2325 
2326 		ret = rte_eth_macaddr_get(portid, &ports_eth_addr[portid]);
2327 		if (ret < 0)
2328 			rte_exit(EXIT_FAILURE,
2329 				 "Cannot get MAC address: err=%d, port=%d\n",
2330 				 ret, portid);
2331 
2332 		print_ethaddr(" Address:", &ports_eth_addr[portid]);
2333 		printf(", ");
2334 
2335 		/* init memory */
2336 		ret = init_mem(NB_MBUF);
2337 		if (ret < 0)
2338 			rte_exit(EXIT_FAILURE, "init_mem failed\n");
2339 
2340 		for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
2341 			if (rte_lcore_is_enabled(lcore_id) == 0)
2342 				continue;
2343 
2344 			/* Initialize TX buffers */
2345 			qconf = &lcore_conf[lcore_id];
2346 			qconf->tx_buffer[portid] = rte_zmalloc_socket("tx_buffer",
2347 				RTE_ETH_TX_BUFFER_SIZE(MAX_PKT_BURST), 0,
2348 				rte_eth_dev_socket_id(portid));
2349 			if (qconf->tx_buffer[portid] == NULL)
2350 				rte_exit(EXIT_FAILURE, "Can't allocate tx buffer for port %u\n",
2351 						 portid);
2352 
2353 			rte_eth_tx_buffer_init(qconf->tx_buffer[portid], MAX_PKT_BURST);
2354 		}
2355 
2356 		/* init one TX queue per couple (lcore,port) */
2357 		queueid = 0;
2358 		for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
2359 			if (rte_lcore_is_enabled(lcore_id) == 0)
2360 				continue;
2361 
2362 			if (queueid >= dev_txq_num)
2363 				continue;
2364 
2365 			if (numa_on)
2366 				socketid = \
2367 				(uint8_t)rte_lcore_to_socket_id(lcore_id);
2368 			else
2369 				socketid = 0;
2370 
2371 			printf("txq=%u,%d,%d ", lcore_id, queueid, socketid);
2372 			fflush(stdout);
2373 
2374 			txconf = &dev_info.default_txconf;
2375 			txconf->offloads = local_port_conf.txmode.offloads;
2376 			ret = rte_eth_tx_queue_setup(portid, queueid, nb_txd,
2377 						     socketid, txconf);
2378 			if (ret < 0)
2379 				rte_exit(EXIT_FAILURE,
2380 					"rte_eth_tx_queue_setup: err=%d, "
2381 						"port=%d\n", ret, portid);
2382 
2383 			qconf = &lcore_conf[lcore_id];
2384 			qconf->tx_queue_id[portid] = queueid;
2385 			queueid++;
2386 
2387 			qconf->tx_port_id[qconf->n_tx_port] = portid;
2388 			qconf->n_tx_port++;
2389 		}
2390 		printf("\n");
2391 	}
2392 
2393 	for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
2394 		if (rte_lcore_is_enabled(lcore_id) == 0)
2395 			continue;
2396 
2397 		if (app_mode == APP_MODE_LEGACY) {
2398 			/* init timer structures for each enabled lcore */
2399 			rte_timer_init(&power_timers[lcore_id]);
2400 			hz = rte_get_timer_hz();
2401 			rte_timer_reset(&power_timers[lcore_id],
2402 					hz/TIMER_NUMBER_PER_SECOND,
2403 					SINGLE, lcore_id,
2404 					power_timer_cb, NULL);
2405 		}
2406 		qconf = &lcore_conf[lcore_id];
2407 		printf("\nInitializing rx queues on lcore %u ... ", lcore_id );
2408 		fflush(stdout);
2409 		/* init RX queues */
2410 		for(queue = 0; queue < qconf->n_rx_queue; ++queue) {
2411 			struct rte_eth_rxconf rxq_conf;
2412 
2413 			portid = qconf->rx_queue_list[queue].port_id;
2414 			queueid = qconf->rx_queue_list[queue].queue_id;
2415 
2416 			if (numa_on)
2417 				socketid = \
2418 				(uint8_t)rte_lcore_to_socket_id(lcore_id);
2419 			else
2420 				socketid = 0;
2421 
2422 			printf("rxq=%d,%d,%d ", portid, queueid, socketid);
2423 			fflush(stdout);
2424 
2425 			ret = rte_eth_dev_info_get(portid, &dev_info);
2426 			if (ret != 0)
2427 				rte_exit(EXIT_FAILURE,
2428 					"Error during getting device (port %u) info: %s\n",
2429 					portid, strerror(-ret));
2430 
2431 			rxq_conf = dev_info.default_rxconf;
2432 			rxq_conf.offloads = port_conf.rxmode.offloads;
2433 			ret = rte_eth_rx_queue_setup(portid, queueid, nb_rxd,
2434 				socketid, &rxq_conf,
2435 				pktmbuf_pool[socketid]);
2436 			if (ret < 0)
2437 				rte_exit(EXIT_FAILURE,
2438 					"rte_eth_rx_queue_setup: err=%d, "
2439 						"port=%d\n", ret, portid);
2440 
2441 			if (parse_ptype) {
2442 				if (add_cb_parse_ptype(portid, queueid) < 0)
2443 					rte_exit(EXIT_FAILURE,
2444 						 "Fail to add ptype cb\n");
2445 			} else if (!check_ptype(portid))
2446 				rte_exit(EXIT_FAILURE,
2447 					 "PMD can not provide needed ptypes\n");
2448 		}
2449 	}
2450 
2451 	printf("\n");
2452 
2453 	/* start ports */
2454 	RTE_ETH_FOREACH_DEV(portid) {
2455 		if ((enabled_port_mask & (1 << portid)) == 0) {
2456 			continue;
2457 		}
2458 		/* Start device */
2459 		ret = rte_eth_dev_start(portid);
2460 		if (ret < 0)
2461 			rte_exit(EXIT_FAILURE, "rte_eth_dev_start: err=%d, "
2462 						"port=%d\n", ret, portid);
2463 		/*
2464 		 * If enabled, put device in promiscuous mode.
2465 		 * This allows IO forwarding mode to forward packets
2466 		 * to itself through 2 cross-connected  ports of the
2467 		 * target machine.
2468 		 */
2469 		if (promiscuous_on) {
2470 			ret = rte_eth_promiscuous_enable(portid);
2471 			if (ret != 0)
2472 				rte_exit(EXIT_FAILURE,
2473 					"rte_eth_promiscuous_enable: err=%s, port=%u\n",
2474 					rte_strerror(-ret), portid);
2475 		}
2476 		/* initialize spinlock for each port */
2477 		rte_spinlock_init(&(locks[portid]));
2478 	}
2479 
2480 	check_all_ports_link_status(enabled_port_mask);
2481 
2482 	if (app_mode == APP_MODE_EMPTY_POLL) {
2483 
2484 		if (empty_poll_train) {
2485 			policy.state = TRAINING;
2486 		} else {
2487 			policy.state = MED_NORMAL;
2488 			policy.med_base_edpi = ep_med_edpi;
2489 			policy.hgh_base_edpi = ep_hgh_edpi;
2490 		}
2491 
2492 		ret = rte_power_empty_poll_stat_init(&ep_params,
2493 				freq_tlb,
2494 				&policy);
2495 		if (ret < 0)
2496 			rte_exit(EXIT_FAILURE, "empty poll init failed");
2497 	}
2498 
2499 
2500 	/* launch per-lcore init on every lcore */
2501 	if (app_mode == APP_MODE_LEGACY) {
2502 		rte_eal_mp_remote_launch(main_loop, NULL, CALL_MASTER);
2503 	} else if (app_mode == APP_MODE_EMPTY_POLL) {
2504 		empty_poll_stop = false;
2505 		rte_eal_mp_remote_launch(main_empty_poll_loop, NULL,
2506 				SKIP_MASTER);
2507 	} else {
2508 		unsigned int i;
2509 
2510 		/* Init metrics library */
2511 		rte_metrics_init(rte_socket_id());
2512 		/** Register stats with metrics library */
2513 		for (i = 0; i < num_telstats; i++)
2514 			ptr_strings[i] = telstats_strings[i].name;
2515 
2516 		ret = rte_metrics_reg_names(ptr_strings, num_telstats);
2517 		if (ret >= 0)
2518 			telstats_index = ret;
2519 		else
2520 			rte_exit(EXIT_FAILURE, "failed to register metrics names");
2521 
2522 		RTE_LCORE_FOREACH_SLAVE(lcore_id) {
2523 			rte_spinlock_init(&stats[lcore_id].telemetry_lock);
2524 		}
2525 		rte_timer_init(&telemetry_timer);
2526 		rte_eal_mp_remote_launch(main_telemetry_loop, NULL,
2527 						SKIP_MASTER);
2528 	}
2529 
2530 	if (app_mode == APP_MODE_EMPTY_POLL || app_mode == APP_MODE_TELEMETRY)
2531 		launch_timer(rte_lcore_id());
2532 
2533 	RTE_LCORE_FOREACH_SLAVE(lcore_id) {
2534 		if (rte_eal_wait_lcore(lcore_id) < 0)
2535 			return -1;
2536 	}
2537 
2538 	if (app_mode == APP_MODE_EMPTY_POLL)
2539 		rte_power_empty_poll_stat_free();
2540 
2541 	return 0;
2542 }
2543