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