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