1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2010-2018 Intel Corporation
11 #include <sys/queue.h>
19 #include <rte_common.h>
20 #include <rte_byteorder.h>
22 #include <rte_malloc.h>
23 #include <rte_memory.h>
24 #include <rte_memcpy.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>
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>
50 #include "perf_core.h"
53 #define RTE_LOGTYPE_L3FWD_POWER RTE_LOGTYPE_USER1
55 #define MAX_PKT_BURST 32
57 #define MIN_ZERO_POLL_COUNT 10
60 #define TIMER_NUMBER_PER_SECOND 10
62 #define INTERVALS_PER_SECOND 100
64 #define SCALING_PERIOD (1000000/TIMER_NUMBER_PER_SECOND)
65 #define SCALING_DOWN_TIME_RATIO_THRESHOLD 0.25
67 #define APP_LOOKUP_EXACT_MATCH 0
68 #define APP_LOOKUP_LPM 1
69 #define DO_RFC_1812_CHECKS
71 #ifndef APP_LOOKUP_METHOD
72 #define APP_LOOKUP_METHOD APP_LOOKUP_LPM
75 #if (APP_LOOKUP_METHOD == APP_LOOKUP_EXACT_MATCH)
77 #elif (APP_LOOKUP_METHOD == APP_LOOKUP_LPM)
80 #error "APP_LOOKUP_METHOD set to incorrect value"
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]
93 #define MAX_JUMBO_PKT_LEN 9600
95 #define IPV6_ADDR_LEN 16
97 #define MEMPOOL_CACHE_SIZE 256
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.
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), \
113 #define BURST_TX_DRAIN_US 100 /* TX drain every ~100us */
117 /* Configure how many packets ahead to prefetch, when reading packets */
118 #define PREFETCH_OFFSET 3
121 * Configurable number of RX/TX ring descriptors
123 #define RTE_TEST_RX_DESC_DEFAULT 1024
124 #define RTE_TEST_TX_DESC_DEFAULT 1024
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.
131 #define EMPTY_POLL_MED_THRESHOLD 350000UL
132 #define EMPTY_POLL_HGH_THRESHOLD 580000UL
136 static uint16_t nb_rxd = RTE_TEST_RX_DESC_DEFAULT;
137 static uint16_t nb_txd = RTE_TEST_TX_DESC_DEFAULT;
139 /* ethernet addresses of ports */
140 static struct rte_ether_addr ports_eth_addr[RTE_MAX_ETHPORTS];
142 /* ethernet addresses of ports */
143 static rte_spinlock_t locks[RTE_MAX_ETHPORTS];
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;
160 /* stats index returned by metrics lib */
163 struct telstats_name {
164 char name[RTE_ETH_XSTATS_NAME_SIZE];
167 /* telemetry stats to be reported */
168 const struct telstats_name telstats_strings[] = {
174 /* core busyness in percentage */
181 /* reference poll count to measure core busyness */
182 #define DEFAULT_COUNT 10000
184 * reference CYCLES to be used to
185 * measure core busyness based on poll count
187 #define MIN_CYCLES 1500000ULL
188 #define MAX_CYCLES 22000000ULL
191 #define TELEMETRY_INTERVALS_PER_SEC 2
193 static int parse_ptype; /**< Parse packet type using rx callback, and */
194 /**< disabled by default */
202 enum appmode app_mode;
204 enum freq_scale_hint_t
212 struct lcore_rx_queue {
215 enum freq_scale_hint_t freq_up_hint;
216 uint32_t zero_rx_packet_count;
218 } __rte_cache_aligned;
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
224 #define MAX_RX_QUEUE_INTERRUPT_PER_PORT 16
227 struct lcore_params lcore_params_array[MAX_LCORE_PARAMS];
228 static struct lcore_params lcore_params_array_default[] = {
240 struct lcore_params *lcore_params = lcore_params_array_default;
241 uint16_t nb_lcore_params = RTE_DIM(lcore_params_array_default);
243 static struct rte_eth_conf port_conf = {
245 .mq_mode = ETH_MQ_RX_RSS,
246 .max_rx_pkt_len = RTE_ETHER_MAX_LEN,
248 .offloads = DEV_RX_OFFLOAD_CHECKSUM,
253 .rss_hf = ETH_RSS_UDP,
257 .mq_mode = ETH_MQ_TX_NONE,
264 static struct rte_mempool * pktmbuf_pool[NB_SOCKETS];
267 #if (APP_LOOKUP_METHOD == APP_LOOKUP_EXACT_MATCH)
270 #include <rte_hash_crc.h>
271 #define DEFAULT_HASH_FUNC rte_hash_crc
273 #include <rte_jhash.h>
274 #define DEFAULT_HASH_FUNC rte_jhash
286 uint8_t ip_dst[IPV6_ADDR_LEN];
287 uint8_t ip_src[IPV6_ADDR_LEN];
293 struct ipv4_l3fwd_route {
294 struct ipv4_5tuple key;
298 struct ipv6_l3fwd_route {
299 struct ipv6_5tuple key;
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},
310 static struct ipv6_l3fwd_route ipv6_l3fwd_route_array[] = {
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},
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];
326 #define L3FWD_HASH_ENTRIES 1024
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;
332 #if (APP_LOOKUP_METHOD == APP_LOOKUP_LPM)
333 struct ipv4_l3fwd_route {
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},
350 #define IPV4_L3FWD_LPM_MAX_RULES 1024
352 typedef struct rte_lpm lookup_struct_t;
353 static lookup_struct_t *ipv4_l3fwd_lookup_struct[NB_SOCKETS];
358 struct lcore_rx_queue rx_queue_list[MAX_RX_QUEUE_PER_LCORE];
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;
368 /* total sleep time in ms since last frequency scaling down */
370 /* number of long sleep recently */
371 uint32_t nb_long_sleep;
372 /* freq. scaling up trend */
374 /* total packet processed recently */
375 uint64_t nb_rx_processed;
376 /* total iterations looped recently */
377 uint64_t nb_iteration_looped;
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.
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
397 rte_spinlock_t telemetry_lock;
398 } __rte_cache_aligned;
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];
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);
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.
414 static uint8_t freq_tlb[] = {14, 9, 1};
416 static int is_done(void)
421 /* exit signal handler */
423 signal_exit_now(int sigtype)
426 if (sigtype == SIGINT)
431 /* Freqency scale down timer callback */
433 power_timer_cb(__rte_unused struct rte_timer *tim,
434 __rte_unused void *arg)
437 float sleep_time_ratio;
438 unsigned lcore_id = rte_lcore_id();
440 /* accumulate total execution time in us when callback is invoked */
441 sleep_time_ratio = (float)(stats[lcore_id].sleep_time) /
442 (float)SCALING_PERIOD;
444 * check whether need to scale down frequency a step if it sleep a lot.
446 if (sleep_time_ratio >= SCALING_DOWN_TIME_RATIO_THRESHOLD) {
447 if (rte_power_freq_down)
448 rte_power_freq_down(lcore_id);
450 else if ( (unsigned)(stats[lcore_id].nb_rx_processed /
451 stats[lcore_id].nb_iteration_looped) < MAX_PKT_BURST) {
453 * scale down a step if average packet per iteration less
456 if (rte_power_freq_down)
457 rte_power_freq_down(lcore_id);
461 * initialize another timer according to current frequency to ensure
462 * timer interval is relatively fixed.
464 hz = rte_get_timer_hz();
465 rte_timer_reset(&power_timers[lcore_id], hz/TIMER_NUMBER_PER_SECOND,
466 SINGLE, lcore_id, power_timer_cb, NULL);
468 stats[lcore_id].nb_rx_processed = 0;
469 stats[lcore_id].nb_iteration_looped = 0;
471 stats[lcore_id].sleep_time = 0;
474 /* Enqueue a single packet, and send burst if queue is filled */
476 send_single_packet(struct rte_mbuf *m, uint16_t port)
479 struct lcore_conf *qconf;
481 lcore_id = rte_lcore_id();
482 qconf = &lcore_conf[lcore_id];
484 rte_eth_tx_buffer(port, qconf->tx_queue_id[port],
485 qconf->tx_buffer[port], m);
490 #ifdef DO_RFC_1812_CHECKS
492 is_valid_ipv4_pkt(struct rte_ipv4_hdr *pkt, uint32_t link_len)
494 /* From http://www.rfc-editor.org/rfc/rfc1812.txt section 5.2.2 */
496 * 1. The packet length reported by the Link Layer must be large
497 * enough to hold the minimum length legal IP datagram (20 bytes).
499 if (link_len < sizeof(struct rte_ipv4_hdr))
502 /* 2. The IP checksum must be correct. */
503 /* this is checked in H/W */
506 * 3. The IP version number must be 4. If the version number is not 4
507 * then the packet may be another version of IP, such as IPng or
510 if (((pkt->version_ihl) >> 4) != 4)
513 * 4. The IP header length field must be large enough to hold the
514 * minimum length legal IP datagram (20 bytes = 5 words).
516 if ((pkt->version_ihl & 0xf) < 5)
520 * 5. The IP total length field must be large enough to hold the IP
521 * datagram header, whose length is specified in the IP header length
524 if (rte_cpu_to_be_16(pkt->total_length) < sizeof(struct rte_ipv4_hdr))
531 #if (APP_LOOKUP_METHOD == APP_LOOKUP_EXACT_MATCH)
533 print_ipv4_key(struct ipv4_5tuple key)
535 printf("IP dst = %08x, IP src = %08x, port dst = %d, port src = %d, "
536 "proto = %d\n", (unsigned)key.ip_dst, (unsigned)key.ip_src,
537 key.port_dst, key.port_src, key.proto);
540 print_ipv6_key(struct ipv6_5tuple key)
542 printf( "IP dst = " IPv6_BYTES_FMT ", IP src = " IPv6_BYTES_FMT ", "
543 "port dst = %d, port src = %d, proto = %d\n",
544 IPv6_BYTES(key.ip_dst), IPv6_BYTES(key.ip_src),
545 key.port_dst, key.port_src, key.proto);
548 static inline uint16_t
549 get_ipv4_dst_port(struct rte_ipv4_hdr *ipv4_hdr, uint16_t portid,
550 lookup_struct_t * ipv4_l3fwd_lookup_struct)
552 struct ipv4_5tuple key;
553 struct rte_tcp_hdr *tcp;
554 struct rte_udp_hdr *udp;
557 key.ip_dst = rte_be_to_cpu_32(ipv4_hdr->dst_addr);
558 key.ip_src = rte_be_to_cpu_32(ipv4_hdr->src_addr);
559 key.proto = ipv4_hdr->next_proto_id;
561 switch (ipv4_hdr->next_proto_id) {
563 tcp = (struct rte_tcp_hdr *)((unsigned char *)ipv4_hdr +
564 sizeof(struct rte_ipv4_hdr));
565 key.port_dst = rte_be_to_cpu_16(tcp->dst_port);
566 key.port_src = rte_be_to_cpu_16(tcp->src_port);
570 udp = (struct rte_udp_hdr *)((unsigned char *)ipv4_hdr +
571 sizeof(struct rte_ipv4_hdr));
572 key.port_dst = rte_be_to_cpu_16(udp->dst_port);
573 key.port_src = rte_be_to_cpu_16(udp->src_port);
582 /* Find destination port */
583 ret = rte_hash_lookup(ipv4_l3fwd_lookup_struct, (const void *)&key);
584 return ((ret < 0) ? portid : ipv4_l3fwd_out_if[ret]);
587 static inline uint16_t
588 get_ipv6_dst_port(struct rte_ipv6_hdr *ipv6_hdr, uint16_t portid,
589 lookup_struct_t *ipv6_l3fwd_lookup_struct)
591 struct ipv6_5tuple key;
592 struct rte_tcp_hdr *tcp;
593 struct rte_udp_hdr *udp;
596 memcpy(key.ip_dst, ipv6_hdr->dst_addr, IPV6_ADDR_LEN);
597 memcpy(key.ip_src, ipv6_hdr->src_addr, IPV6_ADDR_LEN);
599 key.proto = ipv6_hdr->proto;
601 switch (ipv6_hdr->proto) {
603 tcp = (struct rte_tcp_hdr *)((unsigned char *) ipv6_hdr +
604 sizeof(struct rte_ipv6_hdr));
605 key.port_dst = rte_be_to_cpu_16(tcp->dst_port);
606 key.port_src = rte_be_to_cpu_16(tcp->src_port);
610 udp = (struct rte_udp_hdr *)((unsigned char *) ipv6_hdr +
611 sizeof(struct rte_ipv6_hdr));
612 key.port_dst = rte_be_to_cpu_16(udp->dst_port);
613 key.port_src = rte_be_to_cpu_16(udp->src_port);
622 /* Find destination port */
623 ret = rte_hash_lookup(ipv6_l3fwd_lookup_struct, (const void *)&key);
624 return ((ret < 0) ? portid : ipv6_l3fwd_out_if[ret]);
628 #if (APP_LOOKUP_METHOD == APP_LOOKUP_LPM)
629 static inline uint16_t
630 get_ipv4_dst_port(struct rte_ipv4_hdr *ipv4_hdr, uint16_t portid,
631 lookup_struct_t *ipv4_l3fwd_lookup_struct)
635 return ((rte_lpm_lookup(ipv4_l3fwd_lookup_struct,
636 rte_be_to_cpu_32(ipv4_hdr->dst_addr), &next_hop) == 0)?
642 parse_ptype_one(struct rte_mbuf *m)
644 struct rte_ether_hdr *eth_hdr;
645 uint32_t packet_type = RTE_PTYPE_UNKNOWN;
648 eth_hdr = rte_pktmbuf_mtod(m, struct rte_ether_hdr *);
649 ether_type = eth_hdr->ether_type;
650 if (ether_type == rte_cpu_to_be_16(RTE_ETHER_TYPE_IPV4))
651 packet_type |= RTE_PTYPE_L3_IPV4_EXT_UNKNOWN;
652 else if (ether_type == rte_cpu_to_be_16(RTE_ETHER_TYPE_IPV6))
653 packet_type |= RTE_PTYPE_L3_IPV6_EXT_UNKNOWN;
655 m->packet_type = packet_type;
659 cb_parse_ptype(uint16_t port __rte_unused, uint16_t queue __rte_unused,
660 struct rte_mbuf *pkts[], uint16_t nb_pkts,
661 uint16_t max_pkts __rte_unused,
662 void *user_param __rte_unused)
666 for (i = 0; i < nb_pkts; ++i)
667 parse_ptype_one(pkts[i]);
673 add_cb_parse_ptype(uint16_t portid, uint16_t queueid)
675 printf("Port %d: softly parse packet type info\n", portid);
676 if (rte_eth_add_rx_callback(portid, queueid, cb_parse_ptype, NULL))
679 printf("Failed to add rx callback: port=%d\n", portid);
684 l3fwd_simple_forward(struct rte_mbuf *m, uint16_t portid,
685 struct lcore_conf *qconf)
687 struct rte_ether_hdr *eth_hdr;
688 struct rte_ipv4_hdr *ipv4_hdr;
692 eth_hdr = rte_pktmbuf_mtod(m, struct rte_ether_hdr *);
694 if (RTE_ETH_IS_IPV4_HDR(m->packet_type)) {
695 /* Handle IPv4 headers.*/
697 rte_pktmbuf_mtod_offset(m, struct rte_ipv4_hdr *,
698 sizeof(struct rte_ether_hdr));
700 #ifdef DO_RFC_1812_CHECKS
701 /* Check to make sure the packet is valid (RFC1812) */
702 if (is_valid_ipv4_pkt(ipv4_hdr, m->pkt_len) < 0) {
708 dst_port = get_ipv4_dst_port(ipv4_hdr, portid,
709 qconf->ipv4_lookup_struct);
710 if (dst_port >= RTE_MAX_ETHPORTS ||
711 (enabled_port_mask & 1 << dst_port) == 0)
714 /* 02:00:00:00:00:xx */
715 d_addr_bytes = ð_hdr->d_addr.addr_bytes[0];
716 *((uint64_t *)d_addr_bytes) =
717 0x000000000002 + ((uint64_t)dst_port << 40);
719 #ifdef DO_RFC_1812_CHECKS
720 /* Update time to live and header checksum */
721 --(ipv4_hdr->time_to_live);
722 ++(ipv4_hdr->hdr_checksum);
726 rte_ether_addr_copy(&ports_eth_addr[dst_port],
729 send_single_packet(m, dst_port);
730 } else if (RTE_ETH_IS_IPV6_HDR(m->packet_type)) {
731 /* Handle IPv6 headers.*/
732 #if (APP_LOOKUP_METHOD == APP_LOOKUP_EXACT_MATCH)
733 struct rte_ipv6_hdr *ipv6_hdr;
736 rte_pktmbuf_mtod_offset(m, struct rte_ipv6_hdr *,
737 sizeof(struct rte_ether_hdr));
739 dst_port = get_ipv6_dst_port(ipv6_hdr, portid,
740 qconf->ipv6_lookup_struct);
742 if (dst_port >= RTE_MAX_ETHPORTS ||
743 (enabled_port_mask & 1 << dst_port) == 0)
746 /* 02:00:00:00:00:xx */
747 d_addr_bytes = ð_hdr->d_addr.addr_bytes[0];
748 *((uint64_t *)d_addr_bytes) =
749 0x000000000002 + ((uint64_t)dst_port << 40);
752 rte_ether_addr_copy(&ports_eth_addr[dst_port],
755 send_single_packet(m, dst_port);
757 /* We don't currently handle IPv6 packets in LPM mode. */
765 #define MINIMUM_SLEEP_TIME 1
766 #define SUSPEND_THRESHOLD 300
768 static inline uint32_t
769 power_idle_heuristic(uint32_t zero_rx_packet_count)
771 /* If zero count is less than 100, sleep 1us */
772 if (zero_rx_packet_count < SUSPEND_THRESHOLD)
773 return MINIMUM_SLEEP_TIME;
774 /* If zero count is less than 1000, sleep 100 us which is the
775 minimum latency switching from C3/C6 to C0
778 return SUSPEND_THRESHOLD;
781 static inline enum freq_scale_hint_t
782 power_freq_scaleup_heuristic(unsigned lcore_id,
786 uint32_t rxq_count = rte_eth_rx_queue_count(port_id, queue_id);
788 * HW Rx queue size is 128 by default, Rx burst read at maximum 32 entries
791 #define FREQ_GEAR1_RX_PACKET_THRESHOLD MAX_PKT_BURST
792 #define FREQ_GEAR2_RX_PACKET_THRESHOLD (MAX_PKT_BURST*2)
793 #define FREQ_GEAR3_RX_PACKET_THRESHOLD (MAX_PKT_BURST*3)
794 #define FREQ_UP_TREND1_ACC 1
795 #define FREQ_UP_TREND2_ACC 100
796 #define FREQ_UP_THRESHOLD 10000
798 if (likely(rxq_count > FREQ_GEAR3_RX_PACKET_THRESHOLD)) {
799 stats[lcore_id].trend = 0;
801 } else if (likely(rxq_count > FREQ_GEAR2_RX_PACKET_THRESHOLD))
802 stats[lcore_id].trend += FREQ_UP_TREND2_ACC;
803 else if (likely(rxq_count > FREQ_GEAR1_RX_PACKET_THRESHOLD))
804 stats[lcore_id].trend += FREQ_UP_TREND1_ACC;
806 if (likely(stats[lcore_id].trend > FREQ_UP_THRESHOLD)) {
807 stats[lcore_id].trend = 0;
815 * force polling thread sleep until one-shot rx interrupt triggers
824 sleep_until_rx_interrupt(int num)
826 struct rte_epoll_event event[num];
832 RTE_LOG(INFO, L3FWD_POWER,
833 "lcore %u sleeps until interrupt triggers\n",
836 n = rte_epoll_wait(RTE_EPOLL_PER_THREAD, event, num, -1);
837 for (i = 0; i < n; i++) {
838 data = event[i].epdata.data;
839 port_id = ((uintptr_t)data) >> CHAR_BIT;
840 queue_id = ((uintptr_t)data) &
841 RTE_LEN2MASK(CHAR_BIT, uint8_t);
842 RTE_LOG(INFO, L3FWD_POWER,
843 "lcore %u is waked up from rx interrupt on"
844 " port %d queue %d\n",
845 rte_lcore_id(), port_id, queue_id);
851 static void turn_on_off_intr(struct lcore_conf *qconf, bool on)
854 struct lcore_rx_queue *rx_queue;
858 for (i = 0; i < qconf->n_rx_queue; ++i) {
859 rx_queue = &(qconf->rx_queue_list[i]);
860 port_id = rx_queue->port_id;
861 queue_id = rx_queue->queue_id;
863 rte_spinlock_lock(&(locks[port_id]));
865 rte_eth_dev_rx_intr_enable(port_id, queue_id);
867 rte_eth_dev_rx_intr_disable(port_id, queue_id);
868 rte_spinlock_unlock(&(locks[port_id]));
872 static int event_register(struct lcore_conf *qconf)
874 struct lcore_rx_queue *rx_queue;
881 for (i = 0; i < qconf->n_rx_queue; ++i) {
882 rx_queue = &(qconf->rx_queue_list[i]);
883 portid = rx_queue->port_id;
884 queueid = rx_queue->queue_id;
885 data = portid << CHAR_BIT | queueid;
887 ret = rte_eth_dev_rx_intr_ctl_q(portid, queueid,
888 RTE_EPOLL_PER_THREAD,
890 (void *)((uintptr_t)data));
897 /* main processing loop */
899 main_telemetry_loop(__rte_unused void *dummy)
901 struct rte_mbuf *pkts_burst[MAX_PKT_BURST];
902 unsigned int lcore_id;
903 uint64_t prev_tsc, diff_tsc, cur_tsc, prev_tel_tsc;
907 struct lcore_conf *qconf;
908 struct lcore_rx_queue *rx_queue;
909 uint64_t ep_nep[2] = {0}, fp_nfp[2] = {0};
913 const uint64_t drain_tsc = (rte_get_tsc_hz() + US_PER_S - 1) /
914 US_PER_S * BURST_TX_DRAIN_US;
920 lcore_id = rte_lcore_id();
921 qconf = &lcore_conf[lcore_id];
923 if (qconf->n_rx_queue == 0) {
924 RTE_LOG(INFO, L3FWD_POWER, "lcore %u has nothing to do\n",
929 RTE_LOG(INFO, L3FWD_POWER, "entering main telemetry loop on lcore %u\n",
932 for (i = 0; i < qconf->n_rx_queue; i++) {
933 portid = qconf->rx_queue_list[i].port_id;
934 queueid = qconf->rx_queue_list[i].queue_id;
935 RTE_LOG(INFO, L3FWD_POWER, " -- lcoreid=%u portid=%u "
936 "rxqueueid=%hhu\n", lcore_id, portid, queueid);
941 cur_tsc = rte_rdtsc();
943 * TX burst queue drain
945 diff_tsc = cur_tsc - prev_tsc;
946 if (unlikely(diff_tsc > drain_tsc)) {
947 for (i = 0; i < qconf->n_tx_port; ++i) {
948 portid = qconf->tx_port_id[i];
949 rte_eth_tx_buffer_flush(portid,
950 qconf->tx_queue_id[portid],
951 qconf->tx_buffer[portid]);
957 * Read packet from RX queues
959 for (i = 0; i < qconf->n_rx_queue; ++i) {
960 rx_queue = &(qconf->rx_queue_list[i]);
961 portid = rx_queue->port_id;
962 queueid = rx_queue->queue_id;
964 nb_rx = rte_eth_rx_burst(portid, queueid, pkts_burst,
966 ep_nep[nb_rx == 0]++;
967 fp_nfp[nb_rx == MAX_PKT_BURST]++;
969 if (unlikely(nb_rx == 0))
972 /* Prefetch first packets */
973 for (j = 0; j < PREFETCH_OFFSET && j < nb_rx; j++) {
974 rte_prefetch0(rte_pktmbuf_mtod(
975 pkts_burst[j], void *));
978 /* Prefetch and forward already prefetched packets */
979 for (j = 0; j < (nb_rx - PREFETCH_OFFSET); j++) {
980 rte_prefetch0(rte_pktmbuf_mtod(pkts_burst[
981 j + PREFETCH_OFFSET], void *));
982 l3fwd_simple_forward(pkts_burst[j], portid,
986 /* Forward remaining prefetched packets */
987 for (; j < nb_rx; j++) {
988 l3fwd_simple_forward(pkts_burst[j], portid,
992 if (unlikely(poll_count >= DEFAULT_COUNT)) {
993 diff_tsc = cur_tsc - prev_tel_tsc;
994 if (diff_tsc >= MAX_CYCLES) {
996 } else if (diff_tsc > MIN_CYCLES &&
997 diff_tsc < MAX_CYCLES) {
998 br = (diff_tsc * 100) / MAX_CYCLES;
1003 prev_tel_tsc = cur_tsc;
1004 /* update stats for telemetry */
1005 rte_spinlock_lock(&stats[lcore_id].telemetry_lock);
1006 stats[lcore_id].ep_nep[0] = ep_nep[0];
1007 stats[lcore_id].ep_nep[1] = ep_nep[1];
1008 stats[lcore_id].fp_nfp[0] = fp_nfp[0];
1009 stats[lcore_id].fp_nfp[1] = fp_nfp[1];
1010 stats[lcore_id].br = br;
1011 rte_spinlock_unlock(&stats[lcore_id].telemetry_lock);
1017 /* main processing loop */
1019 main_empty_poll_loop(__rte_unused void *dummy)
1021 struct rte_mbuf *pkts_burst[MAX_PKT_BURST];
1022 unsigned int lcore_id;
1023 uint64_t prev_tsc, diff_tsc, cur_tsc;
1027 struct lcore_conf *qconf;
1028 struct lcore_rx_queue *rx_queue;
1030 const uint64_t drain_tsc =
1031 (rte_get_tsc_hz() + US_PER_S - 1) /
1032 US_PER_S * BURST_TX_DRAIN_US;
1036 lcore_id = rte_lcore_id();
1037 qconf = &lcore_conf[lcore_id];
1039 if (qconf->n_rx_queue == 0) {
1040 RTE_LOG(INFO, L3FWD_POWER, "lcore %u has nothing to do\n",
1045 for (i = 0; i < qconf->n_rx_queue; i++) {
1046 portid = qconf->rx_queue_list[i].port_id;
1047 queueid = qconf->rx_queue_list[i].queue_id;
1048 RTE_LOG(INFO, L3FWD_POWER, " -- lcoreid=%u portid=%u "
1049 "rxqueueid=%hhu\n", lcore_id, portid, queueid);
1052 while (!is_done()) {
1053 stats[lcore_id].nb_iteration_looped++;
1055 cur_tsc = rte_rdtsc();
1057 * TX burst queue drain
1059 diff_tsc = cur_tsc - prev_tsc;
1060 if (unlikely(diff_tsc > drain_tsc)) {
1061 for (i = 0; i < qconf->n_tx_port; ++i) {
1062 portid = qconf->tx_port_id[i];
1063 rte_eth_tx_buffer_flush(portid,
1064 qconf->tx_queue_id[portid],
1065 qconf->tx_buffer[portid]);
1071 * Read packet from RX queues
1073 for (i = 0; i < qconf->n_rx_queue; ++i) {
1074 rx_queue = &(qconf->rx_queue_list[i]);
1075 rx_queue->idle_hint = 0;
1076 portid = rx_queue->port_id;
1077 queueid = rx_queue->queue_id;
1079 nb_rx = rte_eth_rx_burst(portid, queueid, pkts_burst,
1082 stats[lcore_id].nb_rx_processed += nb_rx;
1086 rte_power_empty_poll_stat_update(lcore_id);
1090 rte_power_poll_stat_update(lcore_id, nb_rx);
1094 /* Prefetch first packets */
1095 for (j = 0; j < PREFETCH_OFFSET && j < nb_rx; j++) {
1096 rte_prefetch0(rte_pktmbuf_mtod(
1097 pkts_burst[j], void *));
1100 /* Prefetch and forward already prefetched packets */
1101 for (j = 0; j < (nb_rx - PREFETCH_OFFSET); j++) {
1102 rte_prefetch0(rte_pktmbuf_mtod(pkts_burst[
1103 j + PREFETCH_OFFSET],
1105 l3fwd_simple_forward(pkts_burst[j], portid,
1109 /* Forward remaining prefetched packets */
1110 for (; j < nb_rx; j++) {
1111 l3fwd_simple_forward(pkts_burst[j], portid,
1121 /* main processing loop */
1123 main_loop(__rte_unused void *dummy)
1125 struct rte_mbuf *pkts_burst[MAX_PKT_BURST];
1127 uint64_t prev_tsc, diff_tsc, cur_tsc, tim_res_tsc, hz;
1128 uint64_t prev_tsc_power = 0, cur_tsc_power, diff_tsc_power;
1132 struct lcore_conf *qconf;
1133 struct lcore_rx_queue *rx_queue;
1134 enum freq_scale_hint_t lcore_scaleup_hint;
1135 uint32_t lcore_rx_idle_count = 0;
1136 uint32_t lcore_idle_hint = 0;
1139 const uint64_t drain_tsc = (rte_get_tsc_hz() + US_PER_S - 1) / US_PER_S * BURST_TX_DRAIN_US;
1142 hz = rte_get_timer_hz();
1143 tim_res_tsc = hz/TIMER_NUMBER_PER_SECOND;
1145 lcore_id = rte_lcore_id();
1146 qconf = &lcore_conf[lcore_id];
1148 if (qconf->n_rx_queue == 0) {
1149 RTE_LOG(INFO, L3FWD_POWER, "lcore %u has nothing to do\n", lcore_id);
1153 RTE_LOG(INFO, L3FWD_POWER, "entering main loop on lcore %u\n", lcore_id);
1155 for (i = 0; i < qconf->n_rx_queue; i++) {
1156 portid = qconf->rx_queue_list[i].port_id;
1157 queueid = qconf->rx_queue_list[i].queue_id;
1158 RTE_LOG(INFO, L3FWD_POWER, " -- lcoreid=%u portid=%u "
1159 "rxqueueid=%hhu\n", lcore_id, portid, queueid);
1162 /* add into event wait list */
1163 if (event_register(qconf) == 0)
1166 RTE_LOG(INFO, L3FWD_POWER, "RX interrupt won't enable.\n");
1168 while (!is_done()) {
1169 stats[lcore_id].nb_iteration_looped++;
1171 cur_tsc = rte_rdtsc();
1172 cur_tsc_power = cur_tsc;
1175 * TX burst queue drain
1177 diff_tsc = cur_tsc - prev_tsc;
1178 if (unlikely(diff_tsc > drain_tsc)) {
1179 for (i = 0; i < qconf->n_tx_port; ++i) {
1180 portid = qconf->tx_port_id[i];
1181 rte_eth_tx_buffer_flush(portid,
1182 qconf->tx_queue_id[portid],
1183 qconf->tx_buffer[portid]);
1188 diff_tsc_power = cur_tsc_power - prev_tsc_power;
1189 if (diff_tsc_power > tim_res_tsc) {
1191 prev_tsc_power = cur_tsc_power;
1196 * Read packet from RX queues
1198 lcore_scaleup_hint = FREQ_CURRENT;
1199 lcore_rx_idle_count = 0;
1200 for (i = 0; i < qconf->n_rx_queue; ++i) {
1201 rx_queue = &(qconf->rx_queue_list[i]);
1202 rx_queue->idle_hint = 0;
1203 portid = rx_queue->port_id;
1204 queueid = rx_queue->queue_id;
1206 nb_rx = rte_eth_rx_burst(portid, queueid, pkts_burst,
1209 stats[lcore_id].nb_rx_processed += nb_rx;
1210 if (unlikely(nb_rx == 0)) {
1212 * no packet received from rx queue, try to
1213 * sleep for a while forcing CPU enter deeper
1216 rx_queue->zero_rx_packet_count++;
1218 if (rx_queue->zero_rx_packet_count <=
1219 MIN_ZERO_POLL_COUNT)
1222 rx_queue->idle_hint = power_idle_heuristic(\
1223 rx_queue->zero_rx_packet_count);
1224 lcore_rx_idle_count++;
1226 rx_queue->zero_rx_packet_count = 0;
1229 * do not scale up frequency immediately as
1230 * user to kernel space communication is costly
1231 * which might impact packet I/O for received
1234 rx_queue->freq_up_hint =
1235 power_freq_scaleup_heuristic(lcore_id,
1239 /* Prefetch first packets */
1240 for (j = 0; j < PREFETCH_OFFSET && j < nb_rx; j++) {
1241 rte_prefetch0(rte_pktmbuf_mtod(
1242 pkts_burst[j], void *));
1245 /* Prefetch and forward already prefetched packets */
1246 for (j = 0; j < (nb_rx - PREFETCH_OFFSET); j++) {
1247 rte_prefetch0(rte_pktmbuf_mtod(pkts_burst[
1248 j + PREFETCH_OFFSET], void *));
1249 l3fwd_simple_forward(pkts_burst[j], portid,
1253 /* Forward remaining prefetched packets */
1254 for (; j < nb_rx; j++) {
1255 l3fwd_simple_forward(pkts_burst[j], portid,
1260 if (likely(lcore_rx_idle_count != qconf->n_rx_queue)) {
1261 for (i = 1, lcore_scaleup_hint =
1262 qconf->rx_queue_list[0].freq_up_hint;
1263 i < qconf->n_rx_queue; ++i) {
1264 rx_queue = &(qconf->rx_queue_list[i]);
1265 if (rx_queue->freq_up_hint >
1267 lcore_scaleup_hint =
1268 rx_queue->freq_up_hint;
1271 if (lcore_scaleup_hint == FREQ_HIGHEST) {
1272 if (rte_power_freq_max)
1273 rte_power_freq_max(lcore_id);
1274 } else if (lcore_scaleup_hint == FREQ_HIGHER) {
1275 if (rte_power_freq_up)
1276 rte_power_freq_up(lcore_id);
1280 * All Rx queues empty in recent consecutive polls,
1281 * sleep in a conservative manner, meaning sleep as
1284 for (i = 1, lcore_idle_hint =
1285 qconf->rx_queue_list[0].idle_hint;
1286 i < qconf->n_rx_queue; ++i) {
1287 rx_queue = &(qconf->rx_queue_list[i]);
1288 if (rx_queue->idle_hint < lcore_idle_hint)
1289 lcore_idle_hint = rx_queue->idle_hint;
1292 if (lcore_idle_hint < SUSPEND_THRESHOLD)
1294 * execute "pause" instruction to avoid context
1295 * switch which generally take hundred of
1296 * microseconds for short sleep.
1298 rte_delay_us(lcore_idle_hint);
1300 /* suspend until rx interrupt triggers */
1302 turn_on_off_intr(qconf, 1);
1303 sleep_until_rx_interrupt(
1305 turn_on_off_intr(qconf, 0);
1307 * start receiving packets immediately
1312 stats[lcore_id].sleep_time += lcore_idle_hint;
1320 check_lcore_params(void)
1322 uint8_t queue, lcore;
1326 for (i = 0; i < nb_lcore_params; ++i) {
1327 queue = lcore_params[i].queue_id;
1328 if (queue >= MAX_RX_QUEUE_PER_PORT) {
1329 printf("invalid queue number: %hhu\n", queue);
1332 lcore = lcore_params[i].lcore_id;
1333 if (!rte_lcore_is_enabled(lcore)) {
1334 printf("error: lcore %hhu is not enabled in lcore "
1338 if ((socketid = rte_lcore_to_socket_id(lcore) != 0) &&
1340 printf("warning: lcore %hhu is on socket %d with numa "
1341 "off\n", lcore, socketid);
1343 if (app_mode == APP_MODE_TELEMETRY && lcore == rte_lcore_id()) {
1344 printf("cannot enable master core %d in config for telemetry mode\n",
1353 check_port_config(void)
1358 for (i = 0; i < nb_lcore_params; ++i) {
1359 portid = lcore_params[i].port_id;
1360 if ((enabled_port_mask & (1 << portid)) == 0) {
1361 printf("port %u is not enabled in port mask\n",
1365 if (!rte_eth_dev_is_valid_port(portid)) {
1366 printf("port %u is not present on the board\n",
1375 get_port_n_rx_queues(const uint16_t port)
1380 for (i = 0; i < nb_lcore_params; ++i) {
1381 if (lcore_params[i].port_id == port &&
1382 lcore_params[i].queue_id > queue)
1383 queue = lcore_params[i].queue_id;
1385 return (uint8_t)(++queue);
1389 init_lcore_rx_queues(void)
1391 uint16_t i, nb_rx_queue;
1394 for (i = 0; i < nb_lcore_params; ++i) {
1395 lcore = lcore_params[i].lcore_id;
1396 nb_rx_queue = lcore_conf[lcore].n_rx_queue;
1397 if (nb_rx_queue >= MAX_RX_QUEUE_PER_LCORE) {
1398 printf("error: too many queues (%u) for lcore: %u\n",
1399 (unsigned)nb_rx_queue + 1, (unsigned)lcore);
1402 lcore_conf[lcore].rx_queue_list[nb_rx_queue].port_id =
1403 lcore_params[i].port_id;
1404 lcore_conf[lcore].rx_queue_list[nb_rx_queue].queue_id =
1405 lcore_params[i].queue_id;
1406 lcore_conf[lcore].n_rx_queue++;
1414 print_usage(const char *prgname)
1416 printf ("%s [EAL options] -- -p PORTMASK -P"
1417 " [--config (port,queue,lcore)[,(port,queue,lcore]]"
1418 " [--high-perf-cores CORELIST"
1419 " [--perf-config (port,queue,hi_perf,lcore_index)[,(port,queue,hi_perf,lcore_index]]"
1420 " [--enable-jumbo [--max-pkt-len PKTLEN]]\n"
1421 " -p PORTMASK: hexadecimal bitmask of ports to configure\n"
1422 " -P : enable promiscuous mode\n"
1423 " --config (port,queue,lcore): rx queues configuration\n"
1424 " --high-perf-cores CORELIST: list of high performance cores\n"
1425 " --perf-config: similar as config, cores specified as indices"
1426 " for bins containing high or regular performance cores\n"
1427 " --no-numa: optional, disable numa awareness\n"
1428 " --enable-jumbo: enable jumbo frame"
1429 " which max packet len is PKTLEN in decimal (64-9600)\n"
1430 " --parse-ptype: parse packet type by software\n"
1431 " --empty-poll: enable empty poll detection"
1432 " follow (training_flag, high_threshold, med_threshold)\n"
1433 " --telemetry: enable telemetry mode, to update"
1434 " empty polls, full polls, and core busyness to telemetry\n",
1438 static int parse_max_pkt_len(const char *pktlen)
1443 /* parse decimal string */
1444 len = strtoul(pktlen, &end, 10);
1445 if ((pktlen[0] == '\0') || (end == NULL) || (*end != '\0'))
1455 parse_portmask(const char *portmask)
1460 /* parse hexadecimal string */
1461 pm = strtoul(portmask, &end, 16);
1462 if ((portmask[0] == '\0') || (end == NULL) || (*end != '\0'))
1472 parse_config(const char *q_arg)
1475 const char *p, *p0 = q_arg;
1483 unsigned long int_fld[_NUM_FLD];
1484 char *str_fld[_NUM_FLD];
1488 nb_lcore_params = 0;
1490 while ((p = strchr(p0,'(')) != NULL) {
1492 if((p0 = strchr(p,')')) == NULL)
1496 if(size >= sizeof(s))
1499 snprintf(s, sizeof(s), "%.*s", size, p);
1500 if (rte_strsplit(s, sizeof(s), str_fld, _NUM_FLD, ',') !=
1503 for (i = 0; i < _NUM_FLD; i++){
1505 int_fld[i] = strtoul(str_fld[i], &end, 0);
1506 if (errno != 0 || end == str_fld[i] || int_fld[i] >
1510 if (nb_lcore_params >= MAX_LCORE_PARAMS) {
1511 printf("exceeded max number of lcore params: %hu\n",
1515 lcore_params_array[nb_lcore_params].port_id =
1516 (uint8_t)int_fld[FLD_PORT];
1517 lcore_params_array[nb_lcore_params].queue_id =
1518 (uint8_t)int_fld[FLD_QUEUE];
1519 lcore_params_array[nb_lcore_params].lcore_id =
1520 (uint8_t)int_fld[FLD_LCORE];
1523 lcore_params = lcore_params_array;
1528 parse_ep_config(const char *q_arg)
1531 const char *p = q_arg;
1541 ep_med_edpi = EMPTY_POLL_MED_THRESHOLD;
1542 ep_hgh_edpi = EMPTY_POLL_MED_THRESHOLD;
1544 strlcpy(s, p, sizeof(s));
1546 num_arg = rte_strsplit(s, sizeof(s), str_fld, 3, ',');
1548 empty_poll_train = false;
1555 training_flag = strtoul(str_fld[0], &end, 0);
1556 med_edpi = strtoul(str_fld[1], &end, 0);
1557 hgh_edpi = strtoul(str_fld[2], &end, 0);
1559 if (training_flag == 1)
1560 empty_poll_train = true;
1563 ep_med_edpi = med_edpi;
1566 ep_hgh_edpi = hgh_edpi;
1576 #define CMD_LINE_OPT_PARSE_PTYPE "parse-ptype"
1577 #define CMD_LINE_OPT_TELEMETRY "telemetry"
1579 /* Parse the argument given in the command line of the application */
1581 parse_args(int argc, char **argv)
1587 char *prgname = argv[0];
1588 static struct option lgopts[] = {
1589 {"config", 1, 0, 0},
1590 {"perf-config", 1, 0, 0},
1591 {"high-perf-cores", 1, 0, 0},
1592 {"no-numa", 0, 0, 0},
1593 {"enable-jumbo", 0, 0, 0},
1594 {"empty-poll", 1, 0, 0},
1595 {CMD_LINE_OPT_PARSE_PTYPE, 0, 0, 0},
1596 {CMD_LINE_OPT_TELEMETRY, 0, 0, 0},
1602 while ((opt = getopt_long(argc, argvopt, "p:l:m:h:P",
1603 lgopts, &option_index)) != EOF) {
1608 enabled_port_mask = parse_portmask(optarg);
1609 if (enabled_port_mask == 0) {
1610 printf("invalid portmask\n");
1611 print_usage(prgname);
1616 printf("Promiscuous mode selected\n");
1620 limit = parse_max_pkt_len(optarg);
1621 freq_tlb[LOW] = limit;
1624 limit = parse_max_pkt_len(optarg);
1625 freq_tlb[MED] = limit;
1628 limit = parse_max_pkt_len(optarg);
1629 freq_tlb[HGH] = limit;
1633 if (!strncmp(lgopts[option_index].name, "config", 6)) {
1634 ret = parse_config(optarg);
1636 printf("invalid config\n");
1637 print_usage(prgname);
1642 if (!strncmp(lgopts[option_index].name,
1643 "perf-config", 11)) {
1644 ret = parse_perf_config(optarg);
1646 printf("invalid perf-config\n");
1647 print_usage(prgname);
1652 if (!strncmp(lgopts[option_index].name,
1653 "high-perf-cores", 15)) {
1654 ret = parse_perf_core_list(optarg);
1656 printf("invalid high-perf-cores\n");
1657 print_usage(prgname);
1662 if (!strncmp(lgopts[option_index].name,
1664 printf("numa is disabled \n");
1668 if (!strncmp(lgopts[option_index].name,
1669 "empty-poll", 10)) {
1670 if (app_mode == APP_MODE_TELEMETRY) {
1671 printf(" empty-poll cannot be enabled as telemetry mode is enabled\n");
1674 app_mode = APP_MODE_EMPTY_POLL;
1675 ret = parse_ep_config(optarg);
1678 printf("invalid empty poll config\n");
1679 print_usage(prgname);
1682 printf("empty-poll is enabled\n");
1685 if (!strncmp(lgopts[option_index].name,
1686 CMD_LINE_OPT_TELEMETRY,
1687 sizeof(CMD_LINE_OPT_TELEMETRY))) {
1688 if (app_mode == APP_MODE_EMPTY_POLL) {
1689 printf("telemetry mode cannot be enabled as empty poll mode is enabled\n");
1692 app_mode = APP_MODE_TELEMETRY;
1693 printf("telemetry mode is enabled\n");
1696 if (!strncmp(lgopts[option_index].name,
1697 "enable-jumbo", 12)) {
1698 struct option lenopts =
1699 {"max-pkt-len", required_argument, \
1702 printf("jumbo frame is enabled \n");
1703 port_conf.rxmode.offloads |=
1704 DEV_RX_OFFLOAD_JUMBO_FRAME;
1705 port_conf.txmode.offloads |=
1706 DEV_TX_OFFLOAD_MULTI_SEGS;
1709 * if no max-pkt-len set, use the default value
1712 if (0 == getopt_long(argc, argvopt, "",
1713 &lenopts, &option_index)) {
1714 ret = parse_max_pkt_len(optarg);
1716 (ret > MAX_JUMBO_PKT_LEN)){
1717 printf("invalid packet "
1719 print_usage(prgname);
1722 port_conf.rxmode.max_rx_pkt_len = ret;
1724 printf("set jumbo frame "
1725 "max packet length to %u\n",
1726 (unsigned int)port_conf.rxmode.max_rx_pkt_len);
1729 if (!strncmp(lgopts[option_index].name,
1730 CMD_LINE_OPT_PARSE_PTYPE,
1731 sizeof(CMD_LINE_OPT_PARSE_PTYPE))) {
1732 printf("soft parse-ptype is enabled\n");
1739 print_usage(prgname);
1745 argv[optind-1] = prgname;
1748 optind = 1; /* reset getopt lib */
1753 print_ethaddr(const char *name, const struct rte_ether_addr *eth_addr)
1755 char buf[RTE_ETHER_ADDR_FMT_SIZE];
1756 rte_ether_format_addr(buf, RTE_ETHER_ADDR_FMT_SIZE, eth_addr);
1757 printf("%s%s", name, buf);
1760 #if (APP_LOOKUP_METHOD == APP_LOOKUP_EXACT_MATCH)
1762 setup_hash(int socketid)
1764 struct rte_hash_parameters ipv4_l3fwd_hash_params = {
1766 .entries = L3FWD_HASH_ENTRIES,
1767 .key_len = sizeof(struct ipv4_5tuple),
1768 .hash_func = DEFAULT_HASH_FUNC,
1769 .hash_func_init_val = 0,
1772 struct rte_hash_parameters ipv6_l3fwd_hash_params = {
1774 .entries = L3FWD_HASH_ENTRIES,
1775 .key_len = sizeof(struct ipv6_5tuple),
1776 .hash_func = DEFAULT_HASH_FUNC,
1777 .hash_func_init_val = 0,
1784 /* create ipv4 hash */
1785 snprintf(s, sizeof(s), "ipv4_l3fwd_hash_%d", socketid);
1786 ipv4_l3fwd_hash_params.name = s;
1787 ipv4_l3fwd_hash_params.socket_id = socketid;
1788 ipv4_l3fwd_lookup_struct[socketid] =
1789 rte_hash_create(&ipv4_l3fwd_hash_params);
1790 if (ipv4_l3fwd_lookup_struct[socketid] == NULL)
1791 rte_exit(EXIT_FAILURE, "Unable to create the l3fwd hash on "
1792 "socket %d\n", socketid);
1794 /* create ipv6 hash */
1795 snprintf(s, sizeof(s), "ipv6_l3fwd_hash_%d", socketid);
1796 ipv6_l3fwd_hash_params.name = s;
1797 ipv6_l3fwd_hash_params.socket_id = socketid;
1798 ipv6_l3fwd_lookup_struct[socketid] =
1799 rte_hash_create(&ipv6_l3fwd_hash_params);
1800 if (ipv6_l3fwd_lookup_struct[socketid] == NULL)
1801 rte_exit(EXIT_FAILURE, "Unable to create the l3fwd hash on "
1802 "socket %d\n", socketid);
1805 /* populate the ipv4 hash */
1806 for (i = 0; i < RTE_DIM(ipv4_l3fwd_route_array); i++) {
1807 ret = rte_hash_add_key (ipv4_l3fwd_lookup_struct[socketid],
1808 (void *) &ipv4_l3fwd_route_array[i].key);
1810 rte_exit(EXIT_FAILURE, "Unable to add entry %u to the"
1811 "l3fwd hash on socket %d\n", i, socketid);
1813 ipv4_l3fwd_out_if[ret] = ipv4_l3fwd_route_array[i].if_out;
1814 printf("Hash: Adding key\n");
1815 print_ipv4_key(ipv4_l3fwd_route_array[i].key);
1818 /* populate the ipv6 hash */
1819 for (i = 0; i < RTE_DIM(ipv6_l3fwd_route_array); i++) {
1820 ret = rte_hash_add_key (ipv6_l3fwd_lookup_struct[socketid],
1821 (void *) &ipv6_l3fwd_route_array[i].key);
1823 rte_exit(EXIT_FAILURE, "Unable to add entry %u to the"
1824 "l3fwd hash on socket %d\n", i, socketid);
1826 ipv6_l3fwd_out_if[ret] = ipv6_l3fwd_route_array[i].if_out;
1827 printf("Hash: Adding key\n");
1828 print_ipv6_key(ipv6_l3fwd_route_array[i].key);
1833 #if (APP_LOOKUP_METHOD == APP_LOOKUP_LPM)
1835 setup_lpm(int socketid)
1841 /* create the LPM table */
1842 struct rte_lpm_config lpm_ipv4_config;
1844 lpm_ipv4_config.max_rules = IPV4_L3FWD_LPM_MAX_RULES;
1845 lpm_ipv4_config.number_tbl8s = 256;
1846 lpm_ipv4_config.flags = 0;
1848 snprintf(s, sizeof(s), "IPV4_L3FWD_LPM_%d", socketid);
1849 ipv4_l3fwd_lookup_struct[socketid] =
1850 rte_lpm_create(s, socketid, &lpm_ipv4_config);
1851 if (ipv4_l3fwd_lookup_struct[socketid] == NULL)
1852 rte_exit(EXIT_FAILURE, "Unable to create the l3fwd LPM table"
1853 " on socket %d\n", socketid);
1855 /* populate the LPM table */
1856 for (i = 0; i < RTE_DIM(ipv4_l3fwd_route_array); i++) {
1857 ret = rte_lpm_add(ipv4_l3fwd_lookup_struct[socketid],
1858 ipv4_l3fwd_route_array[i].ip,
1859 ipv4_l3fwd_route_array[i].depth,
1860 ipv4_l3fwd_route_array[i].if_out);
1863 rte_exit(EXIT_FAILURE, "Unable to add entry %u to the "
1864 "l3fwd LPM table on socket %d\n",
1868 printf("LPM: Adding route 0x%08x / %d (%d)\n",
1869 (unsigned)ipv4_l3fwd_route_array[i].ip,
1870 ipv4_l3fwd_route_array[i].depth,
1871 ipv4_l3fwd_route_array[i].if_out);
1877 init_mem(unsigned nb_mbuf)
1879 struct lcore_conf *qconf;
1884 for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
1885 if (rte_lcore_is_enabled(lcore_id) == 0)
1889 socketid = rte_lcore_to_socket_id(lcore_id);
1893 if (socketid >= NB_SOCKETS) {
1894 rte_exit(EXIT_FAILURE, "Socket %d of lcore %u is "
1895 "out of range %d\n", socketid,
1896 lcore_id, NB_SOCKETS);
1898 if (pktmbuf_pool[socketid] == NULL) {
1899 snprintf(s, sizeof(s), "mbuf_pool_%d", socketid);
1900 pktmbuf_pool[socketid] =
1901 rte_pktmbuf_pool_create(s, nb_mbuf,
1902 MEMPOOL_CACHE_SIZE, 0,
1903 RTE_MBUF_DEFAULT_BUF_SIZE,
1905 if (pktmbuf_pool[socketid] == NULL)
1906 rte_exit(EXIT_FAILURE,
1907 "Cannot init mbuf pool on socket %d\n",
1910 printf("Allocated mbuf pool on socket %d\n",
1913 #if (APP_LOOKUP_METHOD == APP_LOOKUP_LPM)
1914 setup_lpm(socketid);
1916 setup_hash(socketid);
1919 qconf = &lcore_conf[lcore_id];
1920 qconf->ipv4_lookup_struct = ipv4_l3fwd_lookup_struct[socketid];
1921 #if (APP_LOOKUP_METHOD == APP_LOOKUP_EXACT_MATCH)
1922 qconf->ipv6_lookup_struct = ipv6_l3fwd_lookup_struct[socketid];
1928 /* Check the link status of all ports in up to 9s, and print them finally */
1930 check_all_ports_link_status(uint32_t port_mask)
1932 #define CHECK_INTERVAL 100 /* 100ms */
1933 #define MAX_CHECK_TIME 90 /* 9s (90 * 100ms) in total */
1934 uint8_t count, all_ports_up, print_flag = 0;
1936 struct rte_eth_link link;
1939 printf("\nChecking link status");
1941 for (count = 0; count <= MAX_CHECK_TIME; count++) {
1943 RTE_ETH_FOREACH_DEV(portid) {
1944 if ((port_mask & (1 << portid)) == 0)
1946 memset(&link, 0, sizeof(link));
1947 ret = rte_eth_link_get_nowait(portid, &link);
1950 if (print_flag == 1)
1951 printf("Port %u link get failed: %s\n",
1952 portid, rte_strerror(-ret));
1955 /* print link status if flag set */
1956 if (print_flag == 1) {
1957 if (link.link_status)
1958 printf("Port %d Link Up - speed %u "
1959 "Mbps - %s\n", (uint8_t)portid,
1960 (unsigned)link.link_speed,
1961 (link.link_duplex == ETH_LINK_FULL_DUPLEX) ?
1962 ("full-duplex") : ("half-duplex\n"));
1964 printf("Port %d Link Down\n",
1968 /* clear all_ports_up flag if any link down */
1969 if (link.link_status == ETH_LINK_DOWN) {
1974 /* after finally printing all link status, get out */
1975 if (print_flag == 1)
1978 if (all_ports_up == 0) {
1981 rte_delay_ms(CHECK_INTERVAL);
1984 /* set the print_flag if all ports up or timeout */
1985 if (all_ports_up == 1 || count == (MAX_CHECK_TIME - 1)) {
1992 static int check_ptype(uint16_t portid)
1995 int ptype_l3_ipv4 = 0;
1996 #if (APP_LOOKUP_METHOD == APP_LOOKUP_EXACT_MATCH)
1997 int ptype_l3_ipv6 = 0;
1999 uint32_t ptype_mask = RTE_PTYPE_L3_MASK;
2001 ret = rte_eth_dev_get_supported_ptypes(portid, ptype_mask, NULL, 0);
2005 uint32_t ptypes[ret];
2007 ret = rte_eth_dev_get_supported_ptypes(portid, ptype_mask, ptypes, ret);
2008 for (i = 0; i < ret; ++i) {
2009 if (ptypes[i] & RTE_PTYPE_L3_IPV4)
2011 #if (APP_LOOKUP_METHOD == APP_LOOKUP_EXACT_MATCH)
2012 if (ptypes[i] & RTE_PTYPE_L3_IPV6)
2017 if (ptype_l3_ipv4 == 0)
2018 printf("port %d cannot parse RTE_PTYPE_L3_IPV4\n", portid);
2020 #if (APP_LOOKUP_METHOD == APP_LOOKUP_EXACT_MATCH)
2021 if (ptype_l3_ipv6 == 0)
2022 printf("port %d cannot parse RTE_PTYPE_L3_IPV6\n", portid);
2025 #if (APP_LOOKUP_METHOD == APP_LOOKUP_LPM)
2027 #else /* APP_LOOKUP_EXACT_MATCH */
2028 if (ptype_l3_ipv4 && ptype_l3_ipv6)
2037 init_power_library(void)
2039 unsigned int lcore_id;
2042 RTE_LCORE_FOREACH(lcore_id) {
2043 /* init power management library */
2044 ret = rte_power_init(lcore_id);
2047 "Library initialization failed on core %u\n",
2056 deinit_power_library(void)
2058 unsigned int lcore_id;
2061 RTE_LCORE_FOREACH(lcore_id) {
2062 /* deinit power management library */
2063 ret = rte_power_exit(lcore_id);
2066 "Library deinitialization failed on core %u\n",
2075 update_telemetry(__rte_unused struct rte_timer *tim,
2076 __rte_unused void *arg)
2078 unsigned int lcore_id = rte_lcore_id();
2079 struct lcore_conf *qconf;
2080 uint64_t app_eps = 0, app_fps = 0, app_br = 0;
2081 uint64_t values[3] = {0};
2085 RTE_LCORE_FOREACH_SLAVE(lcore_id) {
2086 qconf = &lcore_conf[lcore_id];
2087 if (qconf->n_rx_queue == 0)
2090 rte_spinlock_lock(&stats[lcore_id].telemetry_lock);
2091 app_eps += stats[lcore_id].ep_nep[1];
2092 app_fps += stats[lcore_id].fp_nfp[1];
2093 app_br += stats[lcore_id].br;
2094 rte_spinlock_unlock(&stats[lcore_id].telemetry_lock);
2098 values[0] = app_eps/count;
2099 values[1] = app_fps/count;
2100 values[2] = app_br/count;
2107 ret = rte_metrics_update_values(RTE_METRICS_GLOBAL, telstats_index,
2108 values, RTE_DIM(values));
2110 RTE_LOG(WARNING, POWER, "failed to update metrcis\n");
2113 telemetry_setup_timer(void)
2115 int lcore_id = rte_lcore_id();
2116 uint64_t hz = rte_get_timer_hz();
2119 ticks = hz / TELEMETRY_INTERVALS_PER_SEC;
2120 rte_timer_reset_sync(&telemetry_timer,
2128 empty_poll_setup_timer(void)
2130 int lcore_id = rte_lcore_id();
2131 uint64_t hz = rte_get_timer_hz();
2133 struct ep_params *ep_ptr = ep_params;
2135 ep_ptr->interval_ticks = hz / INTERVALS_PER_SECOND;
2137 rte_timer_reset_sync(&ep_ptr->timer0,
2138 ep_ptr->interval_ticks,
2141 rte_empty_poll_detection,
2146 launch_timer(unsigned int lcore_id)
2148 int64_t prev_tsc = 0, cur_tsc, diff_tsc, cycles_10ms;
2150 RTE_SET_USED(lcore_id);
2153 if (rte_get_master_lcore() != lcore_id) {
2154 rte_panic("timer on lcore:%d which is not master core:%d\n",
2156 rte_get_master_lcore());
2159 RTE_LOG(INFO, POWER, "Bring up the Timer\n");
2161 if (app_mode == APP_MODE_EMPTY_POLL)
2162 empty_poll_setup_timer();
2164 telemetry_setup_timer();
2166 cycles_10ms = rte_get_timer_hz() / 100;
2168 while (!is_done()) {
2169 cur_tsc = rte_rdtsc();
2170 diff_tsc = cur_tsc - prev_tsc;
2171 if (diff_tsc > cycles_10ms) {
2174 cycles_10ms = rte_get_timer_hz() / 100;
2178 RTE_LOG(INFO, POWER, "Timer_subsystem is done\n");
2185 main(int argc, char **argv)
2187 struct lcore_conf *qconf;
2188 struct rte_eth_dev_info dev_info;
2189 struct rte_eth_txconf *txconf;
2195 uint32_t n_tx_queue, nb_lcores;
2196 uint32_t dev_rxq_num, dev_txq_num;
2197 uint8_t nb_rx_queue, queue, socketid;
2199 uint8_t num_telstats = RTE_DIM(telstats_strings);
2200 const char *ptr_strings[num_telstats];
2202 /* catch SIGINT and restore cpufreq governor to ondemand */
2203 signal(SIGINT, signal_exit_now);
2206 ret = rte_eal_init(argc, argv);
2208 rte_exit(EXIT_FAILURE, "Invalid EAL parameters\n");
2212 /* init RTE timer library to be used late */
2213 rte_timer_subsystem_init();
2215 /* parse application arguments (after the EAL ones) */
2216 ret = parse_args(argc, argv);
2218 rte_exit(EXIT_FAILURE, "Invalid L3FWD parameters\n");
2220 if (app_mode != APP_MODE_TELEMETRY && init_power_library())
2221 rte_exit(EXIT_FAILURE, "init_power_library failed\n");
2223 if (update_lcore_params() < 0)
2224 rte_exit(EXIT_FAILURE, "update_lcore_params failed\n");
2226 if (check_lcore_params() < 0)
2227 rte_exit(EXIT_FAILURE, "check_lcore_params failed\n");
2229 ret = init_lcore_rx_queues();
2231 rte_exit(EXIT_FAILURE, "init_lcore_rx_queues failed\n");
2233 nb_ports = rte_eth_dev_count_avail();
2235 if (check_port_config() < 0)
2236 rte_exit(EXIT_FAILURE, "check_port_config failed\n");
2238 nb_lcores = rte_lcore_count();
2240 /* initialize all ports */
2241 RTE_ETH_FOREACH_DEV(portid) {
2242 struct rte_eth_conf local_port_conf = port_conf;
2244 /* skip ports that are not enabled */
2245 if ((enabled_port_mask & (1 << portid)) == 0) {
2246 printf("\nSkipping disabled port %d\n", portid);
2251 printf("Initializing port %d ... ", portid );
2254 ret = rte_eth_dev_info_get(portid, &dev_info);
2256 rte_exit(EXIT_FAILURE,
2257 "Error during getting device (port %u) info: %s\n",
2258 portid, strerror(-ret));
2260 dev_rxq_num = dev_info.max_rx_queues;
2261 dev_txq_num = dev_info.max_tx_queues;
2263 nb_rx_queue = get_port_n_rx_queues(portid);
2264 if (nb_rx_queue > dev_rxq_num)
2265 rte_exit(EXIT_FAILURE,
2266 "Cannot configure not existed rxq: "
2267 "port=%d\n", portid);
2269 n_tx_queue = nb_lcores;
2270 if (n_tx_queue > dev_txq_num)
2271 n_tx_queue = dev_txq_num;
2272 printf("Creating queues: nb_rxq=%d nb_txq=%u... ",
2273 nb_rx_queue, (unsigned)n_tx_queue );
2274 /* If number of Rx queue is 0, no need to enable Rx interrupt */
2275 if (nb_rx_queue == 0)
2276 local_port_conf.intr_conf.rxq = 0;
2278 ret = rte_eth_dev_info_get(portid, &dev_info);
2280 rte_exit(EXIT_FAILURE,
2281 "Error during getting device (port %u) info: %s\n",
2282 portid, strerror(-ret));
2284 if (dev_info.tx_offload_capa & DEV_TX_OFFLOAD_MBUF_FAST_FREE)
2285 local_port_conf.txmode.offloads |=
2286 DEV_TX_OFFLOAD_MBUF_FAST_FREE;
2288 local_port_conf.rx_adv_conf.rss_conf.rss_hf &=
2289 dev_info.flow_type_rss_offloads;
2290 if (local_port_conf.rx_adv_conf.rss_conf.rss_hf !=
2291 port_conf.rx_adv_conf.rss_conf.rss_hf) {
2292 printf("Port %u modified RSS hash function based on hardware support,"
2293 "requested:%#"PRIx64" configured:%#"PRIx64"\n",
2295 port_conf.rx_adv_conf.rss_conf.rss_hf,
2296 local_port_conf.rx_adv_conf.rss_conf.rss_hf);
2299 ret = rte_eth_dev_configure(portid, nb_rx_queue,
2300 (uint16_t)n_tx_queue, &local_port_conf);
2302 rte_exit(EXIT_FAILURE, "Cannot configure device: "
2303 "err=%d, port=%d\n", ret, portid);
2305 ret = rte_eth_dev_adjust_nb_rx_tx_desc(portid, &nb_rxd,
2308 rte_exit(EXIT_FAILURE,
2309 "Cannot adjust number of descriptors: err=%d, port=%d\n",
2312 ret = rte_eth_macaddr_get(portid, &ports_eth_addr[portid]);
2314 rte_exit(EXIT_FAILURE,
2315 "Cannot get MAC address: err=%d, port=%d\n",
2318 print_ethaddr(" Address:", &ports_eth_addr[portid]);
2322 ret = init_mem(NB_MBUF);
2324 rte_exit(EXIT_FAILURE, "init_mem failed\n");
2326 for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
2327 if (rte_lcore_is_enabled(lcore_id) == 0)
2330 /* Initialize TX buffers */
2331 qconf = &lcore_conf[lcore_id];
2332 qconf->tx_buffer[portid] = rte_zmalloc_socket("tx_buffer",
2333 RTE_ETH_TX_BUFFER_SIZE(MAX_PKT_BURST), 0,
2334 rte_eth_dev_socket_id(portid));
2335 if (qconf->tx_buffer[portid] == NULL)
2336 rte_exit(EXIT_FAILURE, "Can't allocate tx buffer for port %u\n",
2339 rte_eth_tx_buffer_init(qconf->tx_buffer[portid], MAX_PKT_BURST);
2342 /* init one TX queue per couple (lcore,port) */
2344 for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
2345 if (rte_lcore_is_enabled(lcore_id) == 0)
2348 if (queueid >= dev_txq_num)
2353 (uint8_t)rte_lcore_to_socket_id(lcore_id);
2357 printf("txq=%u,%d,%d ", lcore_id, queueid, socketid);
2360 txconf = &dev_info.default_txconf;
2361 txconf->offloads = local_port_conf.txmode.offloads;
2362 ret = rte_eth_tx_queue_setup(portid, queueid, nb_txd,
2365 rte_exit(EXIT_FAILURE,
2366 "rte_eth_tx_queue_setup: err=%d, "
2367 "port=%d\n", ret, portid);
2369 qconf = &lcore_conf[lcore_id];
2370 qconf->tx_queue_id[portid] = queueid;
2373 qconf->tx_port_id[qconf->n_tx_port] = portid;
2379 for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
2380 if (rte_lcore_is_enabled(lcore_id) == 0)
2383 if (app_mode == APP_MODE_LEGACY) {
2384 /* init timer structures for each enabled lcore */
2385 rte_timer_init(&power_timers[lcore_id]);
2386 hz = rte_get_timer_hz();
2387 rte_timer_reset(&power_timers[lcore_id],
2388 hz/TIMER_NUMBER_PER_SECOND,
2390 power_timer_cb, NULL);
2392 qconf = &lcore_conf[lcore_id];
2393 printf("\nInitializing rx queues on lcore %u ... ", lcore_id );
2395 /* init RX queues */
2396 for(queue = 0; queue < qconf->n_rx_queue; ++queue) {
2397 struct rte_eth_rxconf rxq_conf;
2399 portid = qconf->rx_queue_list[queue].port_id;
2400 queueid = qconf->rx_queue_list[queue].queue_id;
2404 (uint8_t)rte_lcore_to_socket_id(lcore_id);
2408 printf("rxq=%d,%d,%d ", portid, queueid, socketid);
2411 ret = rte_eth_dev_info_get(portid, &dev_info);
2413 rte_exit(EXIT_FAILURE,
2414 "Error during getting device (port %u) info: %s\n",
2415 portid, strerror(-ret));
2417 rxq_conf = dev_info.default_rxconf;
2418 rxq_conf.offloads = port_conf.rxmode.offloads;
2419 ret = rte_eth_rx_queue_setup(portid, queueid, nb_rxd,
2420 socketid, &rxq_conf,
2421 pktmbuf_pool[socketid]);
2423 rte_exit(EXIT_FAILURE,
2424 "rte_eth_rx_queue_setup: err=%d, "
2425 "port=%d\n", ret, portid);
2428 if (add_cb_parse_ptype(portid, queueid) < 0)
2429 rte_exit(EXIT_FAILURE,
2430 "Fail to add ptype cb\n");
2431 } else if (!check_ptype(portid))
2432 rte_exit(EXIT_FAILURE,
2433 "PMD can not provide needed ptypes\n");
2440 RTE_ETH_FOREACH_DEV(portid) {
2441 if ((enabled_port_mask & (1 << portid)) == 0) {
2445 ret = rte_eth_dev_start(portid);
2447 rte_exit(EXIT_FAILURE, "rte_eth_dev_start: err=%d, "
2448 "port=%d\n", ret, portid);
2450 * If enabled, put device in promiscuous mode.
2451 * This allows IO forwarding mode to forward packets
2452 * to itself through 2 cross-connected ports of the
2455 if (promiscuous_on) {
2456 ret = rte_eth_promiscuous_enable(portid);
2458 rte_exit(EXIT_FAILURE,
2459 "rte_eth_promiscuous_enable: err=%s, port=%u\n",
2460 rte_strerror(-ret), portid);
2462 /* initialize spinlock for each port */
2463 rte_spinlock_init(&(locks[portid]));
2466 check_all_ports_link_status(enabled_port_mask);
2468 if (app_mode == APP_MODE_EMPTY_POLL) {
2470 if (empty_poll_train) {
2471 policy.state = TRAINING;
2473 policy.state = MED_NORMAL;
2474 policy.med_base_edpi = ep_med_edpi;
2475 policy.hgh_base_edpi = ep_hgh_edpi;
2478 ret = rte_power_empty_poll_stat_init(&ep_params,
2482 rte_exit(EXIT_FAILURE, "empty poll init failed");
2486 /* launch per-lcore init on every lcore */
2487 if (app_mode == APP_MODE_LEGACY) {
2488 rte_eal_mp_remote_launch(main_loop, NULL, CALL_MASTER);
2489 } else if (app_mode == APP_MODE_EMPTY_POLL) {
2490 empty_poll_stop = false;
2491 rte_eal_mp_remote_launch(main_empty_poll_loop, NULL,
2496 /* Init metrics library */
2497 rte_metrics_init(rte_socket_id());
2498 /** Register stats with metrics library */
2499 for (i = 0; i < num_telstats; i++)
2500 ptr_strings[i] = telstats_strings[i].name;
2502 ret = rte_metrics_reg_names(ptr_strings, num_telstats);
2504 telstats_index = ret;
2506 rte_exit(EXIT_FAILURE, "failed to register metrics names");
2508 RTE_LCORE_FOREACH_SLAVE(lcore_id) {
2509 rte_spinlock_init(&stats[lcore_id].telemetry_lock);
2511 rte_timer_init(&telemetry_timer);
2512 rte_eal_mp_remote_launch(main_telemetry_loop, NULL,
2516 if (app_mode == APP_MODE_EMPTY_POLL || app_mode == APP_MODE_TELEMETRY)
2517 launch_timer(rte_lcore_id());
2519 RTE_LCORE_FOREACH_SLAVE(lcore_id) {
2520 if (rte_eal_wait_lcore(lcore_id) < 0)
2524 RTE_ETH_FOREACH_DEV(portid)
2526 if ((enabled_port_mask & (1 << portid)) == 0)
2529 rte_eth_dev_stop(portid);
2530 rte_eth_dev_close(portid);
2533 if (app_mode == APP_MODE_EMPTY_POLL)
2534 rte_power_empty_poll_stat_free();
2536 if (app_mode != APP_MODE_TELEMETRY && deinit_power_library())
2537 rte_exit(EXIT_FAILURE, "deinit_power_library failed\n");
2539 if (rte_eal_cleanup() < 0)
2540 RTE_LOG(ERR, L3FWD_POWER, "EAL cleanup failed\n");