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