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