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_cycles.h>
28 #include <rte_prefetch.h>
29 #include <rte_lcore.h>
30 #include <rte_per_lcore.h>
31 #include <rte_branch_prediction.h>
32 #include <rte_interrupts.h>
33 #include <rte_random.h>
34 #include <rte_debug.h>
35 #include <rte_ether.h>
36 #include <rte_ethdev.h>
37 #include <rte_mempool.h>
42 #include <rte_string_fns.h>
43 #include <rte_timer.h>
44 #include <rte_power.h>
45 #include <rte_spinlock.h>
46 #include <rte_power_empty_poll.h>
47 #include <rte_metrics.h>
48 #include <rte_telemetry.h>
49 #include <rte_power_pmd_mgmt.h>
51 #include "perf_core.h"
54 #define RTE_LOGTYPE_L3FWD_POWER RTE_LOGTYPE_USER1
56 #define MAX_PKT_BURST 32
58 #define MIN_ZERO_POLL_COUNT 10
61 #define TIMER_NUMBER_PER_SECOND 10
63 #define INTERVALS_PER_SECOND 100
65 #define SCALING_PERIOD (1000000/TIMER_NUMBER_PER_SECOND)
66 #define SCALING_DOWN_TIME_RATIO_THRESHOLD 0.25
68 #define APP_LOOKUP_EXACT_MATCH 0
69 #define APP_LOOKUP_LPM 1
70 #define DO_RFC_1812_CHECKS
72 #ifndef APP_LOOKUP_METHOD
73 #define APP_LOOKUP_METHOD APP_LOOKUP_LPM
76 #if (APP_LOOKUP_METHOD == APP_LOOKUP_EXACT_MATCH)
78 #elif (APP_LOOKUP_METHOD == APP_LOOKUP_LPM)
81 #error "APP_LOOKUP_METHOD set to incorrect value"
85 #define IPv6_BYTES_FMT "%02x%02x:%02x%02x:%02x%02x:%02x%02x:"\
86 "%02x%02x:%02x%02x:%02x%02x:%02x%02x"
87 #define IPv6_BYTES(addr) \
88 addr[0], addr[1], addr[2], addr[3], \
89 addr[4], addr[5], addr[6], addr[7], \
90 addr[8], addr[9], addr[10], addr[11],\
91 addr[12], addr[13],addr[14], addr[15]
94 #define MAX_JUMBO_PKT_LEN 9600
96 #define IPV6_ADDR_LEN 16
98 #define MEMPOOL_CACHE_SIZE 256
101 * This expression is used to calculate the number of mbufs needed depending on
102 * user input, taking into account memory for rx and tx hardware rings, cache
103 * per lcore and mtable per port per lcore. RTE_MAX is used to ensure that
104 * NB_MBUF never goes below a minimum value of 8192.
107 #define NB_MBUF RTE_MAX ( \
108 (nb_ports*nb_rx_queue*nb_rxd + \
109 nb_ports*nb_lcores*MAX_PKT_BURST + \
110 nb_ports*n_tx_queue*nb_txd + \
111 nb_lcores*MEMPOOL_CACHE_SIZE), \
114 #define BURST_TX_DRAIN_US 100 /* TX drain every ~100us */
118 /* Configure how many packets ahead to prefetch, when reading packets */
119 #define PREFETCH_OFFSET 3
122 * Configurable number of RX/TX ring descriptors
124 #define RTE_TEST_RX_DESC_DEFAULT 1024
125 #define RTE_TEST_TX_DESC_DEFAULT 1024
128 * These two thresholds were decided on by running the training algorithm on
129 * a 2.5GHz Xeon. These defaults can be overridden by supplying non-zero values
130 * for the med_threshold and high_threshold parameters on the command line.
132 #define EMPTY_POLL_MED_THRESHOLD 350000UL
133 #define EMPTY_POLL_HGH_THRESHOLD 580000UL
135 #define NUM_TELSTATS RTE_DIM(telstats_strings)
137 static uint16_t nb_rxd = RTE_TEST_RX_DESC_DEFAULT;
138 static uint16_t nb_txd = RTE_TEST_TX_DESC_DEFAULT;
140 /* ethernet addresses of ports */
141 static struct rte_ether_addr ports_eth_addr[RTE_MAX_ETHPORTS];
143 /* ethernet addresses of ports */
144 static rte_spinlock_t locks[RTE_MAX_ETHPORTS];
146 /* mask of enabled ports */
147 static uint32_t enabled_port_mask = 0;
148 /* Ports set in promiscuous mode off by default. */
149 static int promiscuous_on = 0;
150 /* NUMA is enabled by default. */
151 static int numa_on = 1;
152 static bool empty_poll_stop;
153 static bool empty_poll_train;
154 volatile bool quit_signal;
155 static struct ep_params *ep_params;
156 static struct ep_policy policy;
157 static long ep_med_edpi, ep_hgh_edpi;
158 /* timer to update telemetry every 500ms */
159 static struct rte_timer telemetry_timer;
161 /* stats index returned by metrics lib */
164 struct telstats_name {
165 char name[RTE_ETH_XSTATS_NAME_SIZE];
168 /* telemetry stats to be reported */
169 const struct telstats_name telstats_strings[] = {
175 /* core busyness in percentage */
182 /* reference poll count to measure core busyness */
183 #define DEFAULT_COUNT 10000
185 * reference CYCLES to be used to
186 * measure core busyness based on poll count
188 #define MIN_CYCLES 1500000ULL
189 #define MAX_CYCLES 22000000ULL
192 #define TELEMETRY_INTERVALS_PER_SEC 2
194 static int parse_ptype; /**< Parse packet type using rx callback, and */
195 /**< disabled by default */
198 APP_MODE_DEFAULT = 0,
206 enum appmode app_mode;
208 static enum rte_power_pmd_mgmt_type pmgmt_type;
209 bool baseline_enabled;
211 enum freq_scale_hint_t
219 struct lcore_rx_queue {
222 enum freq_scale_hint_t freq_up_hint;
223 uint32_t zero_rx_packet_count;
225 } __rte_cache_aligned;
227 #define MAX_RX_QUEUE_PER_LCORE 16
228 #define MAX_TX_QUEUE_PER_PORT RTE_MAX_ETHPORTS
229 #define MAX_RX_QUEUE_PER_PORT 128
231 #define MAX_RX_QUEUE_INTERRUPT_PER_PORT 16
234 struct lcore_params lcore_params_array[MAX_LCORE_PARAMS];
235 static struct lcore_params lcore_params_array_default[] = {
247 struct lcore_params *lcore_params = lcore_params_array_default;
248 uint16_t nb_lcore_params = RTE_DIM(lcore_params_array_default);
250 static struct rte_eth_conf port_conf = {
252 .mq_mode = ETH_MQ_RX_RSS,
253 .max_rx_pkt_len = RTE_ETHER_MAX_LEN,
255 .offloads = DEV_RX_OFFLOAD_CHECKSUM,
260 .rss_hf = ETH_RSS_UDP,
264 .mq_mode = ETH_MQ_TX_NONE,
268 static struct rte_mempool * pktmbuf_pool[NB_SOCKETS];
271 #if (APP_LOOKUP_METHOD == APP_LOOKUP_EXACT_MATCH)
274 #include <rte_hash_crc.h>
275 #define DEFAULT_HASH_FUNC rte_hash_crc
277 #include <rte_jhash.h>
278 #define DEFAULT_HASH_FUNC rte_jhash
290 uint8_t ip_dst[IPV6_ADDR_LEN];
291 uint8_t ip_src[IPV6_ADDR_LEN];
297 struct ipv4_l3fwd_route {
298 struct ipv4_5tuple key;
302 struct ipv6_l3fwd_route {
303 struct ipv6_5tuple key;
307 static struct ipv4_l3fwd_route ipv4_l3fwd_route_array[] = {
308 {{RTE_IPV4(100,10,0,1), RTE_IPV4(200,10,0,1), 101, 11, IPPROTO_TCP}, 0},
309 {{RTE_IPV4(100,20,0,2), RTE_IPV4(200,20,0,2), 102, 12, IPPROTO_TCP}, 1},
310 {{RTE_IPV4(100,30,0,3), RTE_IPV4(200,30,0,3), 103, 13, IPPROTO_TCP}, 2},
311 {{RTE_IPV4(100,40,0,4), RTE_IPV4(200,40,0,4), 104, 14, IPPROTO_TCP}, 3},
314 static struct ipv6_l3fwd_route ipv6_l3fwd_route_array[] = {
317 {0xfe, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
318 0x02, 0x1b, 0x21, 0xff, 0xfe, 0x91, 0x38, 0x05},
319 {0xfe, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
320 0x02, 0x1e, 0x67, 0xff, 0xfe, 0x0d, 0xb6, 0x0a},
326 typedef struct rte_hash lookup_struct_t;
327 static lookup_struct_t *ipv4_l3fwd_lookup_struct[NB_SOCKETS];
328 static lookup_struct_t *ipv6_l3fwd_lookup_struct[NB_SOCKETS];
330 #define L3FWD_HASH_ENTRIES 1024
332 static uint16_t ipv4_l3fwd_out_if[L3FWD_HASH_ENTRIES] __rte_cache_aligned;
333 static uint16_t ipv6_l3fwd_out_if[L3FWD_HASH_ENTRIES] __rte_cache_aligned;
336 #if (APP_LOOKUP_METHOD == APP_LOOKUP_LPM)
337 struct ipv4_l3fwd_route {
343 static struct ipv4_l3fwd_route ipv4_l3fwd_route_array[] = {
344 {RTE_IPV4(1,1,1,0), 24, 0},
345 {RTE_IPV4(2,1,1,0), 24, 1},
346 {RTE_IPV4(3,1,1,0), 24, 2},
347 {RTE_IPV4(4,1,1,0), 24, 3},
348 {RTE_IPV4(5,1,1,0), 24, 4},
349 {RTE_IPV4(6,1,1,0), 24, 5},
350 {RTE_IPV4(7,1,1,0), 24, 6},
351 {RTE_IPV4(8,1,1,0), 24, 7},
354 #define IPV4_L3FWD_LPM_MAX_RULES 1024
356 typedef struct rte_lpm lookup_struct_t;
357 static lookup_struct_t *ipv4_l3fwd_lookup_struct[NB_SOCKETS];
362 struct lcore_rx_queue rx_queue_list[MAX_RX_QUEUE_PER_LCORE];
364 uint16_t tx_port_id[RTE_MAX_ETHPORTS];
365 uint16_t tx_queue_id[RTE_MAX_ETHPORTS];
366 struct rte_eth_dev_tx_buffer *tx_buffer[RTE_MAX_ETHPORTS];
367 lookup_struct_t * ipv4_lookup_struct;
368 lookup_struct_t * ipv6_lookup_struct;
369 } __rte_cache_aligned;
372 /* total sleep time in ms since last frequency scaling down */
374 /* number of long sleep recently */
375 uint32_t nb_long_sleep;
376 /* freq. scaling up trend */
378 /* total packet processed recently */
379 uint64_t nb_rx_processed;
380 /* total iterations looped recently */
381 uint64_t nb_iteration_looped;
383 * Represents empty and non empty polls
384 * of rte_eth_rx_burst();
385 * ep_nep[0] holds non empty polls
386 * i.e. 0 < nb_rx <= MAX_BURST
387 * ep_nep[1] holds empty polls.
392 * Represents full and empty+partial
393 * polls of rte_eth_rx_burst();
394 * ep_nep[0] holds empty+partial polls.
395 * i.e. 0 <= nb_rx < MAX_BURST
396 * ep_nep[1] holds full polls
397 * i.e. nb_rx == MAX_BURST
401 rte_spinlock_t telemetry_lock;
402 } __rte_cache_aligned;
404 static struct lcore_conf lcore_conf[RTE_MAX_LCORE] __rte_cache_aligned;
405 static struct lcore_stats stats[RTE_MAX_LCORE] __rte_cache_aligned;
406 static struct rte_timer power_timers[RTE_MAX_LCORE];
408 static inline uint32_t power_idle_heuristic(uint32_t zero_rx_packet_count);
409 static inline enum freq_scale_hint_t power_freq_scaleup_heuristic( \
410 unsigned int lcore_id, uint16_t port_id, uint16_t queue_id);
414 * These defaults are using the max frequency index (1), a medium index (9)
415 * and a typical low frequency index (14). These can be adjusted to use
416 * different indexes using the relevant command line parameters.
418 static uint8_t freq_tlb[] = {14, 9, 1};
420 static int is_done(void)
425 /* exit signal handler */
427 signal_exit_now(int sigtype)
430 if (sigtype == SIGINT)
435 /* Freqency scale down timer callback */
437 power_timer_cb(__rte_unused struct rte_timer *tim,
438 __rte_unused void *arg)
441 float sleep_time_ratio;
442 unsigned lcore_id = rte_lcore_id();
444 /* accumulate total execution time in us when callback is invoked */
445 sleep_time_ratio = (float)(stats[lcore_id].sleep_time) /
446 (float)SCALING_PERIOD;
448 * check whether need to scale down frequency a step if it sleep a lot.
450 if (sleep_time_ratio >= SCALING_DOWN_TIME_RATIO_THRESHOLD) {
451 if (rte_power_freq_down)
452 rte_power_freq_down(lcore_id);
454 else if ( (unsigned)(stats[lcore_id].nb_rx_processed /
455 stats[lcore_id].nb_iteration_looped) < MAX_PKT_BURST) {
457 * scale down a step if average packet per iteration less
460 if (rte_power_freq_down)
461 rte_power_freq_down(lcore_id);
465 * initialize another timer according to current frequency to ensure
466 * timer interval is relatively fixed.
468 hz = rte_get_timer_hz();
469 rte_timer_reset(&power_timers[lcore_id], hz/TIMER_NUMBER_PER_SECOND,
470 SINGLE, lcore_id, power_timer_cb, NULL);
472 stats[lcore_id].nb_rx_processed = 0;
473 stats[lcore_id].nb_iteration_looped = 0;
475 stats[lcore_id].sleep_time = 0;
478 /* Enqueue a single packet, and send burst if queue is filled */
480 send_single_packet(struct rte_mbuf *m, uint16_t port)
483 struct lcore_conf *qconf;
485 lcore_id = rte_lcore_id();
486 qconf = &lcore_conf[lcore_id];
488 rte_eth_tx_buffer(port, qconf->tx_queue_id[port],
489 qconf->tx_buffer[port], m);
494 #ifdef DO_RFC_1812_CHECKS
496 is_valid_ipv4_pkt(struct rte_ipv4_hdr *pkt, uint32_t link_len)
498 /* From http://www.rfc-editor.org/rfc/rfc1812.txt section 5.2.2 */
500 * 1. The packet length reported by the Link Layer must be large
501 * enough to hold the minimum length legal IP datagram (20 bytes).
503 if (link_len < sizeof(struct rte_ipv4_hdr))
506 /* 2. The IP checksum must be correct. */
507 /* this is checked in H/W */
510 * 3. The IP version number must be 4. If the version number is not 4
511 * then the packet may be another version of IP, such as IPng or
514 if (((pkt->version_ihl) >> 4) != 4)
517 * 4. The IP header length field must be large enough to hold the
518 * minimum length legal IP datagram (20 bytes = 5 words).
520 if ((pkt->version_ihl & 0xf) < 5)
524 * 5. The IP total length field must be large enough to hold the IP
525 * datagram header, whose length is specified in the IP header length
528 if (rte_cpu_to_be_16(pkt->total_length) < sizeof(struct rte_ipv4_hdr))
535 #if (APP_LOOKUP_METHOD == APP_LOOKUP_EXACT_MATCH)
537 print_ipv4_key(struct ipv4_5tuple key)
539 printf("IP dst = %08x, IP src = %08x, port dst = %d, port src = %d, "
540 "proto = %d\n", (unsigned)key.ip_dst, (unsigned)key.ip_src,
541 key.port_dst, key.port_src, key.proto);
544 print_ipv6_key(struct ipv6_5tuple key)
546 printf( "IP dst = " IPv6_BYTES_FMT ", IP src = " IPv6_BYTES_FMT ", "
547 "port dst = %d, port src = %d, proto = %d\n",
548 IPv6_BYTES(key.ip_dst), IPv6_BYTES(key.ip_src),
549 key.port_dst, key.port_src, key.proto);
552 static inline uint16_t
553 get_ipv4_dst_port(struct rte_ipv4_hdr *ipv4_hdr, uint16_t portid,
554 lookup_struct_t * ipv4_l3fwd_lookup_struct)
556 struct ipv4_5tuple key;
557 struct rte_tcp_hdr *tcp;
558 struct rte_udp_hdr *udp;
561 key.ip_dst = rte_be_to_cpu_32(ipv4_hdr->dst_addr);
562 key.ip_src = rte_be_to_cpu_32(ipv4_hdr->src_addr);
563 key.proto = ipv4_hdr->next_proto_id;
565 switch (ipv4_hdr->next_proto_id) {
567 tcp = (struct rte_tcp_hdr *)((unsigned char *)ipv4_hdr +
568 sizeof(struct rte_ipv4_hdr));
569 key.port_dst = rte_be_to_cpu_16(tcp->dst_port);
570 key.port_src = rte_be_to_cpu_16(tcp->src_port);
574 udp = (struct rte_udp_hdr *)((unsigned char *)ipv4_hdr +
575 sizeof(struct rte_ipv4_hdr));
576 key.port_dst = rte_be_to_cpu_16(udp->dst_port);
577 key.port_src = rte_be_to_cpu_16(udp->src_port);
586 /* Find destination port */
587 ret = rte_hash_lookup(ipv4_l3fwd_lookup_struct, (const void *)&key);
588 return ((ret < 0) ? portid : ipv4_l3fwd_out_if[ret]);
591 static inline uint16_t
592 get_ipv6_dst_port(struct rte_ipv6_hdr *ipv6_hdr, uint16_t portid,
593 lookup_struct_t *ipv6_l3fwd_lookup_struct)
595 struct ipv6_5tuple key;
596 struct rte_tcp_hdr *tcp;
597 struct rte_udp_hdr *udp;
600 memcpy(key.ip_dst, ipv6_hdr->dst_addr, IPV6_ADDR_LEN);
601 memcpy(key.ip_src, ipv6_hdr->src_addr, IPV6_ADDR_LEN);
603 key.proto = ipv6_hdr->proto;
605 switch (ipv6_hdr->proto) {
607 tcp = (struct rte_tcp_hdr *)((unsigned char *) ipv6_hdr +
608 sizeof(struct rte_ipv6_hdr));
609 key.port_dst = rte_be_to_cpu_16(tcp->dst_port);
610 key.port_src = rte_be_to_cpu_16(tcp->src_port);
614 udp = (struct rte_udp_hdr *)((unsigned char *) ipv6_hdr +
615 sizeof(struct rte_ipv6_hdr));
616 key.port_dst = rte_be_to_cpu_16(udp->dst_port);
617 key.port_src = rte_be_to_cpu_16(udp->src_port);
626 /* Find destination port */
627 ret = rte_hash_lookup(ipv6_l3fwd_lookup_struct, (const void *)&key);
628 return ((ret < 0) ? portid : ipv6_l3fwd_out_if[ret]);
632 #if (APP_LOOKUP_METHOD == APP_LOOKUP_LPM)
633 static inline uint16_t
634 get_ipv4_dst_port(struct rte_ipv4_hdr *ipv4_hdr, uint16_t portid,
635 lookup_struct_t *ipv4_l3fwd_lookup_struct)
639 return ((rte_lpm_lookup(ipv4_l3fwd_lookup_struct,
640 rte_be_to_cpu_32(ipv4_hdr->dst_addr), &next_hop) == 0)?
646 parse_ptype_one(struct rte_mbuf *m)
648 struct rte_ether_hdr *eth_hdr;
649 uint32_t packet_type = RTE_PTYPE_UNKNOWN;
652 eth_hdr = rte_pktmbuf_mtod(m, struct rte_ether_hdr *);
653 ether_type = eth_hdr->ether_type;
654 if (ether_type == rte_cpu_to_be_16(RTE_ETHER_TYPE_IPV4))
655 packet_type |= RTE_PTYPE_L3_IPV4_EXT_UNKNOWN;
656 else if (ether_type == rte_cpu_to_be_16(RTE_ETHER_TYPE_IPV6))
657 packet_type |= RTE_PTYPE_L3_IPV6_EXT_UNKNOWN;
659 m->packet_type = packet_type;
663 cb_parse_ptype(uint16_t port __rte_unused, uint16_t queue __rte_unused,
664 struct rte_mbuf *pkts[], uint16_t nb_pkts,
665 uint16_t max_pkts __rte_unused,
666 void *user_param __rte_unused)
670 for (i = 0; i < nb_pkts; ++i)
671 parse_ptype_one(pkts[i]);
677 add_cb_parse_ptype(uint16_t portid, uint16_t queueid)
679 printf("Port %d: softly parse packet type info\n", portid);
680 if (rte_eth_add_rx_callback(portid, queueid, cb_parse_ptype, NULL))
683 printf("Failed to add rx callback: port=%d\n", portid);
688 l3fwd_simple_forward(struct rte_mbuf *m, uint16_t portid,
689 struct lcore_conf *qconf)
691 struct rte_ether_hdr *eth_hdr;
692 struct rte_ipv4_hdr *ipv4_hdr;
696 eth_hdr = rte_pktmbuf_mtod(m, struct rte_ether_hdr *);
698 if (RTE_ETH_IS_IPV4_HDR(m->packet_type)) {
699 /* Handle IPv4 headers.*/
701 rte_pktmbuf_mtod_offset(m, struct rte_ipv4_hdr *,
702 sizeof(struct rte_ether_hdr));
704 #ifdef DO_RFC_1812_CHECKS
705 /* Check to make sure the packet is valid (RFC1812) */
706 if (is_valid_ipv4_pkt(ipv4_hdr, m->pkt_len) < 0) {
712 dst_port = get_ipv4_dst_port(ipv4_hdr, portid,
713 qconf->ipv4_lookup_struct);
714 if (dst_port >= RTE_MAX_ETHPORTS ||
715 (enabled_port_mask & 1 << dst_port) == 0)
718 /* 02:00:00:00:00:xx */
719 d_addr_bytes = ð_hdr->dst_addr.addr_bytes[0];
720 *((uint64_t *)d_addr_bytes) =
721 0x000000000002 + ((uint64_t)dst_port << 40);
723 #ifdef DO_RFC_1812_CHECKS
724 /* Update time to live and header checksum */
725 --(ipv4_hdr->time_to_live);
726 ++(ipv4_hdr->hdr_checksum);
730 rte_ether_addr_copy(&ports_eth_addr[dst_port],
733 send_single_packet(m, dst_port);
734 } else if (RTE_ETH_IS_IPV6_HDR(m->packet_type)) {
735 /* Handle IPv6 headers.*/
736 #if (APP_LOOKUP_METHOD == APP_LOOKUP_EXACT_MATCH)
737 struct rte_ipv6_hdr *ipv6_hdr;
740 rte_pktmbuf_mtod_offset(m, struct rte_ipv6_hdr *,
741 sizeof(struct rte_ether_hdr));
743 dst_port = get_ipv6_dst_port(ipv6_hdr, portid,
744 qconf->ipv6_lookup_struct);
746 if (dst_port >= RTE_MAX_ETHPORTS ||
747 (enabled_port_mask & 1 << dst_port) == 0)
750 /* 02:00:00:00:00:xx */
751 d_addr_bytes = ð_hdr->dst_addr.addr_bytes[0];
752 *((uint64_t *)d_addr_bytes) =
753 0x000000000002 + ((uint64_t)dst_port << 40);
756 rte_ether_addr_copy(&ports_eth_addr[dst_port],
759 send_single_packet(m, dst_port);
761 /* We don't currently handle IPv6 packets in LPM mode. */
769 #define MINIMUM_SLEEP_TIME 1
770 #define SUSPEND_THRESHOLD 300
772 static inline uint32_t
773 power_idle_heuristic(uint32_t zero_rx_packet_count)
775 /* If zero count is less than 100, sleep 1us */
776 if (zero_rx_packet_count < SUSPEND_THRESHOLD)
777 return MINIMUM_SLEEP_TIME;
778 /* If zero count is less than 1000, sleep 100 us which is the
779 minimum latency switching from C3/C6 to C0
782 return SUSPEND_THRESHOLD;
785 static inline enum freq_scale_hint_t
786 power_freq_scaleup_heuristic(unsigned lcore_id,
790 uint32_t rxq_count = rte_eth_rx_queue_count(port_id, queue_id);
792 * HW Rx queue size is 128 by default, Rx burst read at maximum 32 entries
795 #define FREQ_GEAR1_RX_PACKET_THRESHOLD MAX_PKT_BURST
796 #define FREQ_GEAR2_RX_PACKET_THRESHOLD (MAX_PKT_BURST*2)
797 #define FREQ_GEAR3_RX_PACKET_THRESHOLD (MAX_PKT_BURST*3)
798 #define FREQ_UP_TREND1_ACC 1
799 #define FREQ_UP_TREND2_ACC 100
800 #define FREQ_UP_THRESHOLD 10000
802 if (likely(rxq_count > FREQ_GEAR3_RX_PACKET_THRESHOLD)) {
803 stats[lcore_id].trend = 0;
805 } else if (likely(rxq_count > FREQ_GEAR2_RX_PACKET_THRESHOLD))
806 stats[lcore_id].trend += FREQ_UP_TREND2_ACC;
807 else if (likely(rxq_count > FREQ_GEAR1_RX_PACKET_THRESHOLD))
808 stats[lcore_id].trend += FREQ_UP_TREND1_ACC;
810 if (likely(stats[lcore_id].trend > FREQ_UP_THRESHOLD)) {
811 stats[lcore_id].trend = 0;
819 * force polling thread sleep until one-shot rx interrupt triggers
828 sleep_until_rx_interrupt(int num, int lcore)
831 * we want to track when we are woken up by traffic so that we can go
832 * back to sleep again without log spamming. Avoid cache line sharing
833 * to prevent threads stepping on each others' toes.
837 } __rte_cache_aligned status[RTE_MAX_LCORE];
838 struct rte_epoll_event event[num];
844 if (status[lcore].wakeup) {
845 RTE_LOG(INFO, L3FWD_POWER,
846 "lcore %u sleeps until interrupt triggers\n",
850 n = rte_epoll_wait(RTE_EPOLL_PER_THREAD, event, num, 10);
851 for (i = 0; i < n; i++) {
852 data = event[i].epdata.data;
853 port_id = ((uintptr_t)data) >> CHAR_BIT;
854 queue_id = ((uintptr_t)data) &
855 RTE_LEN2MASK(CHAR_BIT, uint8_t);
856 RTE_LOG(INFO, L3FWD_POWER,
857 "lcore %u is waked up from rx interrupt on"
858 " port %d queue %d\n",
859 rte_lcore_id(), port_id, queue_id);
861 status[lcore].wakeup = n != 0;
866 static void turn_on_off_intr(struct lcore_conf *qconf, bool on)
869 struct lcore_rx_queue *rx_queue;
873 for (i = 0; i < qconf->n_rx_queue; ++i) {
874 rx_queue = &(qconf->rx_queue_list[i]);
875 port_id = rx_queue->port_id;
876 queue_id = rx_queue->queue_id;
878 rte_spinlock_lock(&(locks[port_id]));
880 rte_eth_dev_rx_intr_enable(port_id, queue_id);
882 rte_eth_dev_rx_intr_disable(port_id, queue_id);
883 rte_spinlock_unlock(&(locks[port_id]));
887 static int event_register(struct lcore_conf *qconf)
889 struct lcore_rx_queue *rx_queue;
896 for (i = 0; i < qconf->n_rx_queue; ++i) {
897 rx_queue = &(qconf->rx_queue_list[i]);
898 portid = rx_queue->port_id;
899 queueid = rx_queue->queue_id;
900 data = portid << CHAR_BIT | queueid;
902 ret = rte_eth_dev_rx_intr_ctl_q(portid, queueid,
903 RTE_EPOLL_PER_THREAD,
905 (void *)((uintptr_t)data));
913 /* Main processing loop. 8< */
914 static int main_intr_loop(__rte_unused void *dummy)
916 struct rte_mbuf *pkts_burst[MAX_PKT_BURST];
917 unsigned int lcore_id;
918 uint64_t prev_tsc, diff_tsc, cur_tsc;
922 struct lcore_conf *qconf;
923 struct lcore_rx_queue *rx_queue;
924 uint32_t lcore_rx_idle_count = 0;
925 uint32_t lcore_idle_hint = 0;
928 const uint64_t drain_tsc = (rte_get_tsc_hz() + US_PER_S - 1) /
929 US_PER_S * BURST_TX_DRAIN_US;
933 lcore_id = rte_lcore_id();
934 qconf = &lcore_conf[lcore_id];
936 if (qconf->n_rx_queue == 0) {
937 RTE_LOG(INFO, L3FWD_POWER, "lcore %u has nothing to do\n",
942 RTE_LOG(INFO, L3FWD_POWER, "entering main interrupt loop on lcore %u\n",
945 for (i = 0; i < qconf->n_rx_queue; i++) {
946 portid = qconf->rx_queue_list[i].port_id;
947 queueid = qconf->rx_queue_list[i].queue_id;
948 RTE_LOG(INFO, L3FWD_POWER,
949 " -- lcoreid=%u portid=%u rxqueueid=%hhu\n",
950 lcore_id, portid, queueid);
953 /* add into event wait list */
954 if (event_register(qconf) == 0)
957 RTE_LOG(INFO, L3FWD_POWER, "RX interrupt won't enable.\n");
960 stats[lcore_id].nb_iteration_looped++;
962 cur_tsc = rte_rdtsc();
965 * TX burst queue drain
967 diff_tsc = cur_tsc - prev_tsc;
968 if (unlikely(diff_tsc > drain_tsc)) {
969 for (i = 0; i < qconf->n_tx_port; ++i) {
970 portid = qconf->tx_port_id[i];
971 rte_eth_tx_buffer_flush(portid,
972 qconf->tx_queue_id[portid],
973 qconf->tx_buffer[portid]);
980 * Read packet from RX queues
982 lcore_rx_idle_count = 0;
983 for (i = 0; i < qconf->n_rx_queue; ++i) {
984 rx_queue = &(qconf->rx_queue_list[i]);
985 rx_queue->idle_hint = 0;
986 portid = rx_queue->port_id;
987 queueid = rx_queue->queue_id;
989 nb_rx = rte_eth_rx_burst(portid, queueid, pkts_burst,
992 stats[lcore_id].nb_rx_processed += nb_rx;
993 if (unlikely(nb_rx == 0)) {
995 * no packet received from rx queue, try to
996 * sleep for a while forcing CPU enter deeper
999 rx_queue->zero_rx_packet_count++;
1001 if (rx_queue->zero_rx_packet_count <=
1002 MIN_ZERO_POLL_COUNT)
1005 rx_queue->idle_hint = power_idle_heuristic(
1006 rx_queue->zero_rx_packet_count);
1007 lcore_rx_idle_count++;
1009 rx_queue->zero_rx_packet_count = 0;
1012 /* Prefetch first packets */
1013 for (j = 0; j < PREFETCH_OFFSET && j < nb_rx; j++) {
1014 rte_prefetch0(rte_pktmbuf_mtod(
1015 pkts_burst[j], void *));
1018 /* Prefetch and forward already prefetched packets */
1019 for (j = 0; j < (nb_rx - PREFETCH_OFFSET); j++) {
1020 rte_prefetch0(rte_pktmbuf_mtod(
1021 pkts_burst[j + PREFETCH_OFFSET],
1023 l3fwd_simple_forward(
1024 pkts_burst[j], portid, qconf);
1027 /* Forward remaining prefetched packets */
1028 for (; j < nb_rx; j++) {
1029 l3fwd_simple_forward(
1030 pkts_burst[j], portid, qconf);
1034 if (unlikely(lcore_rx_idle_count == qconf->n_rx_queue)) {
1036 * All Rx queues empty in recent consecutive polls,
1037 * sleep in a conservative manner, meaning sleep as
1041 lcore_idle_hint = qconf->rx_queue_list[0].idle_hint;
1042 i < qconf->n_rx_queue; ++i) {
1043 rx_queue = &(qconf->rx_queue_list[i]);
1044 if (rx_queue->idle_hint < lcore_idle_hint)
1045 lcore_idle_hint = rx_queue->idle_hint;
1048 if (lcore_idle_hint < SUSPEND_THRESHOLD)
1050 * execute "pause" instruction to avoid context
1051 * switch which generally take hundred of
1052 * microseconds for short sleep.
1054 rte_delay_us(lcore_idle_hint);
1056 /* suspend until rx interrupt triggers */
1058 turn_on_off_intr(qconf, 1);
1059 sleep_until_rx_interrupt(
1062 turn_on_off_intr(qconf, 0);
1064 * start receiving packets immediately
1066 if (likely(!is_done()))
1070 stats[lcore_id].sleep_time += lcore_idle_hint;
1076 /* >8 End of main processing loop. */
1078 /* main processing loop */
1080 main_telemetry_loop(__rte_unused void *dummy)
1082 struct rte_mbuf *pkts_burst[MAX_PKT_BURST];
1083 unsigned int lcore_id;
1084 uint64_t prev_tsc, diff_tsc, cur_tsc, prev_tel_tsc;
1088 struct lcore_conf *qconf;
1089 struct lcore_rx_queue *rx_queue;
1090 uint64_t ep_nep[2] = {0}, fp_nfp[2] = {0};
1091 uint64_t poll_count;
1094 const uint64_t drain_tsc = (rte_get_tsc_hz() + US_PER_S - 1) /
1095 US_PER_S * BURST_TX_DRAIN_US;
1101 lcore_id = rte_lcore_id();
1102 qconf = &lcore_conf[lcore_id];
1104 if (qconf->n_rx_queue == 0) {
1105 RTE_LOG(INFO, L3FWD_POWER, "lcore %u has nothing to do\n",
1110 RTE_LOG(INFO, L3FWD_POWER, "entering main telemetry loop on lcore %u\n",
1113 for (i = 0; i < qconf->n_rx_queue; i++) {
1114 portid = qconf->rx_queue_list[i].port_id;
1115 queueid = qconf->rx_queue_list[i].queue_id;
1116 RTE_LOG(INFO, L3FWD_POWER, " -- lcoreid=%u portid=%u "
1117 "rxqueueid=%hhu\n", lcore_id, portid, queueid);
1120 while (!is_done()) {
1122 cur_tsc = rte_rdtsc();
1124 * TX burst queue drain
1126 diff_tsc = cur_tsc - prev_tsc;
1127 if (unlikely(diff_tsc > drain_tsc)) {
1128 for (i = 0; i < qconf->n_tx_port; ++i) {
1129 portid = qconf->tx_port_id[i];
1130 rte_eth_tx_buffer_flush(portid,
1131 qconf->tx_queue_id[portid],
1132 qconf->tx_buffer[portid]);
1138 * Read packet from RX queues
1140 for (i = 0; i < qconf->n_rx_queue; ++i) {
1141 rx_queue = &(qconf->rx_queue_list[i]);
1142 portid = rx_queue->port_id;
1143 queueid = rx_queue->queue_id;
1145 nb_rx = rte_eth_rx_burst(portid, queueid, pkts_burst,
1147 ep_nep[nb_rx == 0]++;
1148 fp_nfp[nb_rx == MAX_PKT_BURST]++;
1150 if (unlikely(nb_rx == 0))
1153 /* Prefetch first packets */
1154 for (j = 0; j < PREFETCH_OFFSET && j < nb_rx; j++) {
1155 rte_prefetch0(rte_pktmbuf_mtod(
1156 pkts_burst[j], void *));
1159 /* Prefetch and forward already prefetched packets */
1160 for (j = 0; j < (nb_rx - PREFETCH_OFFSET); j++) {
1161 rte_prefetch0(rte_pktmbuf_mtod(pkts_burst[
1162 j + PREFETCH_OFFSET], void *));
1163 l3fwd_simple_forward(pkts_burst[j], portid,
1167 /* Forward remaining prefetched packets */
1168 for (; j < nb_rx; j++) {
1169 l3fwd_simple_forward(pkts_burst[j], portid,
1173 if (unlikely(poll_count >= DEFAULT_COUNT)) {
1174 diff_tsc = cur_tsc - prev_tel_tsc;
1175 if (diff_tsc >= MAX_CYCLES) {
1177 } else if (diff_tsc > MIN_CYCLES &&
1178 diff_tsc < MAX_CYCLES) {
1179 br = (diff_tsc * 100) / MAX_CYCLES;
1184 prev_tel_tsc = cur_tsc;
1185 /* update stats for telemetry */
1186 rte_spinlock_lock(&stats[lcore_id].telemetry_lock);
1187 stats[lcore_id].ep_nep[0] = ep_nep[0];
1188 stats[lcore_id].ep_nep[1] = ep_nep[1];
1189 stats[lcore_id].fp_nfp[0] = fp_nfp[0];
1190 stats[lcore_id].fp_nfp[1] = fp_nfp[1];
1191 stats[lcore_id].br = br;
1192 rte_spinlock_unlock(&stats[lcore_id].telemetry_lock);
1198 /* main processing loop */
1200 main_empty_poll_loop(__rte_unused void *dummy)
1202 struct rte_mbuf *pkts_burst[MAX_PKT_BURST];
1203 unsigned int lcore_id;
1204 uint64_t prev_tsc, diff_tsc, cur_tsc;
1208 struct lcore_conf *qconf;
1209 struct lcore_rx_queue *rx_queue;
1211 const uint64_t drain_tsc =
1212 (rte_get_tsc_hz() + US_PER_S - 1) /
1213 US_PER_S * BURST_TX_DRAIN_US;
1217 lcore_id = rte_lcore_id();
1218 qconf = &lcore_conf[lcore_id];
1220 if (qconf->n_rx_queue == 0) {
1221 RTE_LOG(INFO, L3FWD_POWER, "lcore %u has nothing to do\n",
1226 for (i = 0; i < qconf->n_rx_queue; i++) {
1227 portid = qconf->rx_queue_list[i].port_id;
1228 queueid = qconf->rx_queue_list[i].queue_id;
1229 RTE_LOG(INFO, L3FWD_POWER, " -- lcoreid=%u portid=%u "
1230 "rxqueueid=%hhu\n", lcore_id, portid, queueid);
1233 while (!is_done()) {
1234 stats[lcore_id].nb_iteration_looped++;
1236 cur_tsc = rte_rdtsc();
1238 * TX burst queue drain
1240 diff_tsc = cur_tsc - prev_tsc;
1241 if (unlikely(diff_tsc > drain_tsc)) {
1242 for (i = 0; i < qconf->n_tx_port; ++i) {
1243 portid = qconf->tx_port_id[i];
1244 rte_eth_tx_buffer_flush(portid,
1245 qconf->tx_queue_id[portid],
1246 qconf->tx_buffer[portid]);
1252 * Read packet from RX queues
1254 for (i = 0; i < qconf->n_rx_queue; ++i) {
1255 rx_queue = &(qconf->rx_queue_list[i]);
1256 rx_queue->idle_hint = 0;
1257 portid = rx_queue->port_id;
1258 queueid = rx_queue->queue_id;
1260 nb_rx = rte_eth_rx_burst(portid, queueid, pkts_burst,
1263 stats[lcore_id].nb_rx_processed += nb_rx;
1267 rte_power_empty_poll_stat_update(lcore_id);
1271 rte_power_poll_stat_update(lcore_id, nb_rx);
1275 /* Prefetch first packets */
1276 for (j = 0; j < PREFETCH_OFFSET && j < nb_rx; j++) {
1277 rte_prefetch0(rte_pktmbuf_mtod(
1278 pkts_burst[j], void *));
1281 /* Prefetch and forward already prefetched packets */
1282 for (j = 0; j < (nb_rx - PREFETCH_OFFSET); j++) {
1283 rte_prefetch0(rte_pktmbuf_mtod(pkts_burst[
1284 j + PREFETCH_OFFSET],
1286 l3fwd_simple_forward(pkts_burst[j], portid,
1290 /* Forward remaining prefetched packets */
1291 for (; j < nb_rx; j++) {
1292 l3fwd_simple_forward(pkts_burst[j], portid,
1302 /* main processing loop */
1304 main_legacy_loop(__rte_unused void *dummy)
1306 struct rte_mbuf *pkts_burst[MAX_PKT_BURST];
1308 uint64_t prev_tsc, diff_tsc, cur_tsc, tim_res_tsc, hz;
1309 uint64_t prev_tsc_power = 0, cur_tsc_power, diff_tsc_power;
1313 struct lcore_conf *qconf;
1314 struct lcore_rx_queue *rx_queue;
1315 enum freq_scale_hint_t lcore_scaleup_hint;
1316 uint32_t lcore_rx_idle_count = 0;
1317 uint32_t lcore_idle_hint = 0;
1320 const uint64_t drain_tsc = (rte_get_tsc_hz() + US_PER_S - 1) / US_PER_S * BURST_TX_DRAIN_US;
1323 hz = rte_get_timer_hz();
1324 tim_res_tsc = hz/TIMER_NUMBER_PER_SECOND;
1326 lcore_id = rte_lcore_id();
1327 qconf = &lcore_conf[lcore_id];
1329 if (qconf->n_rx_queue == 0) {
1330 RTE_LOG(INFO, L3FWD_POWER, "lcore %u has nothing to do\n", lcore_id);
1334 RTE_LOG(INFO, L3FWD_POWER, "entering main loop on lcore %u\n", lcore_id);
1336 for (i = 0; i < qconf->n_rx_queue; i++) {
1337 portid = qconf->rx_queue_list[i].port_id;
1338 queueid = qconf->rx_queue_list[i].queue_id;
1339 RTE_LOG(INFO, L3FWD_POWER, " -- lcoreid=%u portid=%u "
1340 "rxqueueid=%hhu\n", lcore_id, portid, queueid);
1343 /* add into event wait list */
1344 if (event_register(qconf) == 0)
1347 RTE_LOG(INFO, L3FWD_POWER, "RX interrupt won't enable.\n");
1349 while (!is_done()) {
1350 stats[lcore_id].nb_iteration_looped++;
1352 cur_tsc = rte_rdtsc();
1353 cur_tsc_power = cur_tsc;
1356 * TX burst queue drain
1358 diff_tsc = cur_tsc - prev_tsc;
1359 if (unlikely(diff_tsc > drain_tsc)) {
1360 for (i = 0; i < qconf->n_tx_port; ++i) {
1361 portid = qconf->tx_port_id[i];
1362 rte_eth_tx_buffer_flush(portid,
1363 qconf->tx_queue_id[portid],
1364 qconf->tx_buffer[portid]);
1369 diff_tsc_power = cur_tsc_power - prev_tsc_power;
1370 if (diff_tsc_power > tim_res_tsc) {
1372 prev_tsc_power = cur_tsc_power;
1377 * Read packet from RX queues
1379 lcore_scaleup_hint = FREQ_CURRENT;
1380 lcore_rx_idle_count = 0;
1381 for (i = 0; i < qconf->n_rx_queue; ++i) {
1382 rx_queue = &(qconf->rx_queue_list[i]);
1383 rx_queue->idle_hint = 0;
1384 portid = rx_queue->port_id;
1385 queueid = rx_queue->queue_id;
1387 nb_rx = rte_eth_rx_burst(portid, queueid, pkts_burst,
1390 stats[lcore_id].nb_rx_processed += nb_rx;
1391 if (unlikely(nb_rx == 0)) {
1393 * no packet received from rx queue, try to
1394 * sleep for a while forcing CPU enter deeper
1397 rx_queue->zero_rx_packet_count++;
1399 if (rx_queue->zero_rx_packet_count <=
1400 MIN_ZERO_POLL_COUNT)
1403 rx_queue->idle_hint = power_idle_heuristic(\
1404 rx_queue->zero_rx_packet_count);
1405 lcore_rx_idle_count++;
1407 rx_queue->zero_rx_packet_count = 0;
1410 * do not scale up frequency immediately as
1411 * user to kernel space communication is costly
1412 * which might impact packet I/O for received
1415 rx_queue->freq_up_hint =
1416 power_freq_scaleup_heuristic(lcore_id,
1420 /* Prefetch first packets */
1421 for (j = 0; j < PREFETCH_OFFSET && j < nb_rx; j++) {
1422 rte_prefetch0(rte_pktmbuf_mtod(
1423 pkts_burst[j], void *));
1426 /* Prefetch and forward already prefetched packets */
1427 for (j = 0; j < (nb_rx - PREFETCH_OFFSET); j++) {
1428 rte_prefetch0(rte_pktmbuf_mtod(pkts_burst[
1429 j + PREFETCH_OFFSET], void *));
1430 l3fwd_simple_forward(pkts_burst[j], portid,
1434 /* Forward remaining prefetched packets */
1435 for (; j < nb_rx; j++) {
1436 l3fwd_simple_forward(pkts_burst[j], portid,
1441 if (likely(lcore_rx_idle_count != qconf->n_rx_queue)) {
1442 for (i = 1, lcore_scaleup_hint =
1443 qconf->rx_queue_list[0].freq_up_hint;
1444 i < qconf->n_rx_queue; ++i) {
1445 rx_queue = &(qconf->rx_queue_list[i]);
1446 if (rx_queue->freq_up_hint >
1448 lcore_scaleup_hint =
1449 rx_queue->freq_up_hint;
1452 if (lcore_scaleup_hint == FREQ_HIGHEST) {
1453 if (rte_power_freq_max)
1454 rte_power_freq_max(lcore_id);
1455 } else if (lcore_scaleup_hint == FREQ_HIGHER) {
1456 if (rte_power_freq_up)
1457 rte_power_freq_up(lcore_id);
1461 * All Rx queues empty in recent consecutive polls,
1462 * sleep in a conservative manner, meaning sleep as
1465 for (i = 1, lcore_idle_hint =
1466 qconf->rx_queue_list[0].idle_hint;
1467 i < qconf->n_rx_queue; ++i) {
1468 rx_queue = &(qconf->rx_queue_list[i]);
1469 if (rx_queue->idle_hint < lcore_idle_hint)
1470 lcore_idle_hint = rx_queue->idle_hint;
1473 if (lcore_idle_hint < SUSPEND_THRESHOLD)
1475 * execute "pause" instruction to avoid context
1476 * switch which generally take hundred of
1477 * microseconds for short sleep.
1479 rte_delay_us(lcore_idle_hint);
1481 /* suspend until rx interrupt triggers */
1483 turn_on_off_intr(qconf, 1);
1484 sleep_until_rx_interrupt(
1487 turn_on_off_intr(qconf, 0);
1489 * start receiving packets immediately
1491 if (likely(!is_done()))
1495 stats[lcore_id].sleep_time += lcore_idle_hint;
1503 check_lcore_params(void)
1505 uint8_t queue, lcore;
1509 for (i = 0; i < nb_lcore_params; ++i) {
1510 queue = lcore_params[i].queue_id;
1511 if (queue >= MAX_RX_QUEUE_PER_PORT) {
1512 printf("invalid queue number: %hhu\n", queue);
1515 lcore = lcore_params[i].lcore_id;
1516 if (!rte_lcore_is_enabled(lcore)) {
1517 printf("error: lcore %hhu is not enabled in lcore "
1521 if ((socketid = rte_lcore_to_socket_id(lcore) != 0) &&
1523 printf("warning: lcore %hhu is on socket %d with numa "
1524 "off\n", lcore, socketid);
1526 if (app_mode == APP_MODE_TELEMETRY && lcore == rte_lcore_id()) {
1527 printf("cannot enable main core %d in config for telemetry mode\n",
1536 check_port_config(void)
1541 for (i = 0; i < nb_lcore_params; ++i) {
1542 portid = lcore_params[i].port_id;
1543 if ((enabled_port_mask & (1 << portid)) == 0) {
1544 printf("port %u is not enabled in port mask\n",
1548 if (!rte_eth_dev_is_valid_port(portid)) {
1549 printf("port %u is not present on the board\n",
1558 get_port_n_rx_queues(const uint16_t port)
1563 for (i = 0; i < nb_lcore_params; ++i) {
1564 if (lcore_params[i].port_id == port &&
1565 lcore_params[i].queue_id > queue)
1566 queue = lcore_params[i].queue_id;
1568 return (uint8_t)(++queue);
1572 init_lcore_rx_queues(void)
1574 uint16_t i, nb_rx_queue;
1577 for (i = 0; i < nb_lcore_params; ++i) {
1578 lcore = lcore_params[i].lcore_id;
1579 nb_rx_queue = lcore_conf[lcore].n_rx_queue;
1580 if (nb_rx_queue >= MAX_RX_QUEUE_PER_LCORE) {
1581 printf("error: too many queues (%u) for lcore: %u\n",
1582 (unsigned)nb_rx_queue + 1, (unsigned)lcore);
1585 lcore_conf[lcore].rx_queue_list[nb_rx_queue].port_id =
1586 lcore_params[i].port_id;
1587 lcore_conf[lcore].rx_queue_list[nb_rx_queue].queue_id =
1588 lcore_params[i].queue_id;
1589 lcore_conf[lcore].n_rx_queue++;
1597 print_usage(const char *prgname)
1599 printf ("%s [EAL options] -- -p PORTMASK -P"
1600 " [--config (port,queue,lcore)[,(port,queue,lcore]]"
1601 " [--high-perf-cores CORELIST"
1602 " [--perf-config (port,queue,hi_perf,lcore_index)[,(port,queue,hi_perf,lcore_index]]"
1603 " [--enable-jumbo [--max-pkt-len PKTLEN]]\n"
1604 " -p PORTMASK: hexadecimal bitmask of ports to configure\n"
1605 " -P : enable promiscuous mode\n"
1606 " --config (port,queue,lcore): rx queues configuration\n"
1607 " --high-perf-cores CORELIST: list of high performance cores\n"
1608 " --perf-config: similar as config, cores specified as indices"
1609 " for bins containing high or regular performance cores\n"
1610 " --no-numa: optional, disable numa awareness\n"
1611 " --enable-jumbo: enable jumbo frame"
1612 " which max packet len is PKTLEN in decimal (64-9600)\n"
1613 " --parse-ptype: parse packet type by software\n"
1614 " --legacy: use legacy interrupt-based scaling\n"
1615 " --empty-poll: enable empty poll detection"
1616 " follow (training_flag, high_threshold, med_threshold)\n"
1617 " --telemetry: enable telemetry mode, to update"
1618 " empty polls, full polls, and core busyness to telemetry\n"
1619 " --interrupt-only: enable interrupt-only mode\n"
1620 " --pmd-mgmt MODE: enable PMD power management mode. "
1621 "Currently supported modes: baseline, monitor, pause, scale\n",
1625 static int parse_max_pkt_len(const char *pktlen)
1630 /* parse decimal string */
1631 len = strtoul(pktlen, &end, 10);
1632 if ((pktlen[0] == '\0') || (end == NULL) || (*end != '\0'))
1642 parse_portmask(const char *portmask)
1647 /* parse hexadecimal string */
1648 pm = strtoul(portmask, &end, 16);
1649 if ((portmask[0] == '\0') || (end == NULL) || (*end != '\0'))
1656 parse_config(const char *q_arg)
1659 const char *p, *p0 = q_arg;
1667 unsigned long int_fld[_NUM_FLD];
1668 char *str_fld[_NUM_FLD];
1672 nb_lcore_params = 0;
1674 while ((p = strchr(p0,'(')) != NULL) {
1676 if((p0 = strchr(p,')')) == NULL)
1680 if(size >= sizeof(s))
1683 snprintf(s, sizeof(s), "%.*s", size, p);
1684 if (rte_strsplit(s, sizeof(s), str_fld, _NUM_FLD, ',') !=
1687 for (i = 0; i < _NUM_FLD; i++){
1689 int_fld[i] = strtoul(str_fld[i], &end, 0);
1690 if (errno != 0 || end == str_fld[i] || int_fld[i] >
1694 if (nb_lcore_params >= MAX_LCORE_PARAMS) {
1695 printf("exceeded max number of lcore params: %hu\n",
1699 lcore_params_array[nb_lcore_params].port_id =
1700 (uint8_t)int_fld[FLD_PORT];
1701 lcore_params_array[nb_lcore_params].queue_id =
1702 (uint8_t)int_fld[FLD_QUEUE];
1703 lcore_params_array[nb_lcore_params].lcore_id =
1704 (uint8_t)int_fld[FLD_LCORE];
1707 lcore_params = lcore_params_array;
1713 parse_pmd_mgmt_config(const char *name)
1715 #define PMD_MGMT_MONITOR "monitor"
1716 #define PMD_MGMT_PAUSE "pause"
1717 #define PMD_MGMT_SCALE "scale"
1718 #define PMD_MGMT_BASELINE "baseline"
1720 if (strncmp(PMD_MGMT_MONITOR, name, sizeof(PMD_MGMT_MONITOR)) == 0) {
1721 pmgmt_type = RTE_POWER_MGMT_TYPE_MONITOR;
1725 if (strncmp(PMD_MGMT_PAUSE, name, sizeof(PMD_MGMT_PAUSE)) == 0) {
1726 pmgmt_type = RTE_POWER_MGMT_TYPE_PAUSE;
1730 if (strncmp(PMD_MGMT_SCALE, name, sizeof(PMD_MGMT_SCALE)) == 0) {
1731 pmgmt_type = RTE_POWER_MGMT_TYPE_SCALE;
1734 if (strncmp(PMD_MGMT_BASELINE, name, sizeof(PMD_MGMT_BASELINE)) == 0) {
1735 baseline_enabled = true;
1738 /* unknown PMD power management mode */
1743 parse_ep_config(const char *q_arg)
1746 const char *p = q_arg;
1756 ep_med_edpi = EMPTY_POLL_MED_THRESHOLD;
1757 ep_hgh_edpi = EMPTY_POLL_HGH_THRESHOLD;
1759 strlcpy(s, p, sizeof(s));
1761 num_arg = rte_strsplit(s, sizeof(s), str_fld, 3, ',');
1763 empty_poll_train = false;
1770 training_flag = strtoul(str_fld[0], &end, 0);
1771 med_edpi = strtoul(str_fld[1], &end, 0);
1772 hgh_edpi = strtoul(str_fld[2], &end, 0);
1774 if (training_flag == 1)
1775 empty_poll_train = true;
1778 ep_med_edpi = med_edpi;
1781 ep_hgh_edpi = hgh_edpi;
1791 #define CMD_LINE_OPT_PARSE_PTYPE "parse-ptype"
1792 #define CMD_LINE_OPT_LEGACY "legacy"
1793 #define CMD_LINE_OPT_EMPTY_POLL "empty-poll"
1794 #define CMD_LINE_OPT_INTERRUPT_ONLY "interrupt-only"
1795 #define CMD_LINE_OPT_TELEMETRY "telemetry"
1796 #define CMD_LINE_OPT_PMD_MGMT "pmd-mgmt"
1798 /* Parse the argument given in the command line of the application */
1800 parse_args(int argc, char **argv)
1806 char *prgname = argv[0];
1807 static struct option lgopts[] = {
1808 {"config", 1, 0, 0},
1809 {"perf-config", 1, 0, 0},
1810 {"high-perf-cores", 1, 0, 0},
1811 {"no-numa", 0, 0, 0},
1812 {"enable-jumbo", 0, 0, 0},
1813 {CMD_LINE_OPT_EMPTY_POLL, 1, 0, 0},
1814 {CMD_LINE_OPT_PARSE_PTYPE, 0, 0, 0},
1815 {CMD_LINE_OPT_LEGACY, 0, 0, 0},
1816 {CMD_LINE_OPT_TELEMETRY, 0, 0, 0},
1817 {CMD_LINE_OPT_INTERRUPT_ONLY, 0, 0, 0},
1818 {CMD_LINE_OPT_PMD_MGMT, 1, 0, 0},
1824 while ((opt = getopt_long(argc, argvopt, "p:l:m:h:P",
1825 lgopts, &option_index)) != EOF) {
1830 enabled_port_mask = parse_portmask(optarg);
1831 if (enabled_port_mask == 0) {
1832 printf("invalid portmask\n");
1833 print_usage(prgname);
1838 printf("Promiscuous mode selected\n");
1842 limit = parse_max_pkt_len(optarg);
1843 freq_tlb[LOW] = limit;
1846 limit = parse_max_pkt_len(optarg);
1847 freq_tlb[MED] = limit;
1850 limit = parse_max_pkt_len(optarg);
1851 freq_tlb[HGH] = limit;
1855 if (!strncmp(lgopts[option_index].name, "config", 6)) {
1856 ret = parse_config(optarg);
1858 printf("invalid config\n");
1859 print_usage(prgname);
1864 if (!strncmp(lgopts[option_index].name,
1865 "perf-config", 11)) {
1866 ret = parse_perf_config(optarg);
1868 printf("invalid perf-config\n");
1869 print_usage(prgname);
1874 if (!strncmp(lgopts[option_index].name,
1875 "high-perf-cores", 15)) {
1876 ret = parse_perf_core_list(optarg);
1878 printf("invalid high-perf-cores\n");
1879 print_usage(prgname);
1884 if (!strncmp(lgopts[option_index].name,
1886 printf("numa is disabled \n");
1890 if (!strncmp(lgopts[option_index].name,
1891 CMD_LINE_OPT_LEGACY,
1892 sizeof(CMD_LINE_OPT_LEGACY))) {
1893 if (app_mode != APP_MODE_DEFAULT) {
1894 printf(" legacy mode is mutually exclusive with other modes\n");
1897 app_mode = APP_MODE_LEGACY;
1898 printf("legacy mode is enabled\n");
1901 if (!strncmp(lgopts[option_index].name,
1902 CMD_LINE_OPT_EMPTY_POLL, 10)) {
1903 if (app_mode != APP_MODE_DEFAULT) {
1904 printf(" empty-poll mode is mutually exclusive with other modes\n");
1907 app_mode = APP_MODE_EMPTY_POLL;
1908 ret = parse_ep_config(optarg);
1911 printf("invalid empty poll config\n");
1912 print_usage(prgname);
1915 printf("empty-poll is enabled\n");
1918 if (!strncmp(lgopts[option_index].name,
1919 CMD_LINE_OPT_TELEMETRY,
1920 sizeof(CMD_LINE_OPT_TELEMETRY))) {
1921 if (app_mode != APP_MODE_DEFAULT) {
1922 printf(" telemetry mode is mutually exclusive with other modes\n");
1925 app_mode = APP_MODE_TELEMETRY;
1926 printf("telemetry mode is enabled\n");
1929 if (!strncmp(lgopts[option_index].name,
1930 CMD_LINE_OPT_PMD_MGMT,
1931 sizeof(CMD_LINE_OPT_PMD_MGMT))) {
1932 if (app_mode != APP_MODE_DEFAULT) {
1933 printf(" power mgmt mode is mutually exclusive with other modes\n");
1936 if (parse_pmd_mgmt_config(optarg) < 0) {
1937 printf(" Invalid PMD power management mode: %s\n",
1941 app_mode = APP_MODE_PMD_MGMT;
1942 printf("PMD power mgmt mode is enabled\n");
1944 if (!strncmp(lgopts[option_index].name,
1945 CMD_LINE_OPT_INTERRUPT_ONLY,
1946 sizeof(CMD_LINE_OPT_INTERRUPT_ONLY))) {
1947 if (app_mode != APP_MODE_DEFAULT) {
1948 printf(" interrupt-only mode is mutually exclusive with other modes\n");
1951 app_mode = APP_MODE_INTERRUPT;
1952 printf("interrupt-only mode is enabled\n");
1955 if (!strncmp(lgopts[option_index].name,
1956 "enable-jumbo", 12)) {
1957 struct option lenopts =
1958 {"max-pkt-len", required_argument, \
1961 printf("jumbo frame is enabled \n");
1962 port_conf.rxmode.offloads |=
1963 DEV_RX_OFFLOAD_JUMBO_FRAME;
1964 port_conf.txmode.offloads |=
1965 DEV_TX_OFFLOAD_MULTI_SEGS;
1968 * if no max-pkt-len set, use the default value
1971 if (0 == getopt_long(argc, argvopt, "",
1972 &lenopts, &option_index)) {
1973 ret = parse_max_pkt_len(optarg);
1975 (ret > MAX_JUMBO_PKT_LEN)){
1976 printf("invalid packet "
1978 print_usage(prgname);
1981 port_conf.rxmode.max_rx_pkt_len = ret;
1983 printf("set jumbo frame "
1984 "max packet length to %u\n",
1985 (unsigned int)port_conf.rxmode.max_rx_pkt_len);
1988 if (!strncmp(lgopts[option_index].name,
1989 CMD_LINE_OPT_PARSE_PTYPE,
1990 sizeof(CMD_LINE_OPT_PARSE_PTYPE))) {
1991 printf("soft parse-ptype is enabled\n");
1998 print_usage(prgname);
2004 argv[optind-1] = prgname;
2007 optind = 1; /* reset getopt lib */
2012 print_ethaddr(const char *name, const struct rte_ether_addr *eth_addr)
2014 char buf[RTE_ETHER_ADDR_FMT_SIZE];
2015 rte_ether_format_addr(buf, RTE_ETHER_ADDR_FMT_SIZE, eth_addr);
2016 printf("%s%s", name, buf);
2019 #if (APP_LOOKUP_METHOD == APP_LOOKUP_EXACT_MATCH)
2021 setup_hash(int socketid)
2023 struct rte_hash_parameters ipv4_l3fwd_hash_params = {
2025 .entries = L3FWD_HASH_ENTRIES,
2026 .key_len = sizeof(struct ipv4_5tuple),
2027 .hash_func = DEFAULT_HASH_FUNC,
2028 .hash_func_init_val = 0,
2031 struct rte_hash_parameters ipv6_l3fwd_hash_params = {
2033 .entries = L3FWD_HASH_ENTRIES,
2034 .key_len = sizeof(struct ipv6_5tuple),
2035 .hash_func = DEFAULT_HASH_FUNC,
2036 .hash_func_init_val = 0,
2043 /* create ipv4 hash */
2044 snprintf(s, sizeof(s), "ipv4_l3fwd_hash_%d", socketid);
2045 ipv4_l3fwd_hash_params.name = s;
2046 ipv4_l3fwd_hash_params.socket_id = socketid;
2047 ipv4_l3fwd_lookup_struct[socketid] =
2048 rte_hash_create(&ipv4_l3fwd_hash_params);
2049 if (ipv4_l3fwd_lookup_struct[socketid] == NULL)
2050 rte_exit(EXIT_FAILURE, "Unable to create the l3fwd hash on "
2051 "socket %d\n", socketid);
2053 /* create ipv6 hash */
2054 snprintf(s, sizeof(s), "ipv6_l3fwd_hash_%d", socketid);
2055 ipv6_l3fwd_hash_params.name = s;
2056 ipv6_l3fwd_hash_params.socket_id = socketid;
2057 ipv6_l3fwd_lookup_struct[socketid] =
2058 rte_hash_create(&ipv6_l3fwd_hash_params);
2059 if (ipv6_l3fwd_lookup_struct[socketid] == NULL)
2060 rte_exit(EXIT_FAILURE, "Unable to create the l3fwd hash on "
2061 "socket %d\n", socketid);
2064 /* populate the ipv4 hash */
2065 for (i = 0; i < RTE_DIM(ipv4_l3fwd_route_array); i++) {
2066 ret = rte_hash_add_key (ipv4_l3fwd_lookup_struct[socketid],
2067 (void *) &ipv4_l3fwd_route_array[i].key);
2069 rte_exit(EXIT_FAILURE, "Unable to add entry %u to the"
2070 "l3fwd hash on socket %d\n", i, socketid);
2072 ipv4_l3fwd_out_if[ret] = ipv4_l3fwd_route_array[i].if_out;
2073 printf("Hash: Adding key\n");
2074 print_ipv4_key(ipv4_l3fwd_route_array[i].key);
2077 /* populate the ipv6 hash */
2078 for (i = 0; i < RTE_DIM(ipv6_l3fwd_route_array); i++) {
2079 ret = rte_hash_add_key (ipv6_l3fwd_lookup_struct[socketid],
2080 (void *) &ipv6_l3fwd_route_array[i].key);
2082 rte_exit(EXIT_FAILURE, "Unable to add entry %u to the"
2083 "l3fwd hash on socket %d\n", i, socketid);
2085 ipv6_l3fwd_out_if[ret] = ipv6_l3fwd_route_array[i].if_out;
2086 printf("Hash: Adding key\n");
2087 print_ipv6_key(ipv6_l3fwd_route_array[i].key);
2092 #if (APP_LOOKUP_METHOD == APP_LOOKUP_LPM)
2094 setup_lpm(int socketid)
2100 /* create the LPM table */
2101 struct rte_lpm_config lpm_ipv4_config;
2103 lpm_ipv4_config.max_rules = IPV4_L3FWD_LPM_MAX_RULES;
2104 lpm_ipv4_config.number_tbl8s = 256;
2105 lpm_ipv4_config.flags = 0;
2107 snprintf(s, sizeof(s), "IPV4_L3FWD_LPM_%d", socketid);
2108 ipv4_l3fwd_lookup_struct[socketid] =
2109 rte_lpm_create(s, socketid, &lpm_ipv4_config);
2110 if (ipv4_l3fwd_lookup_struct[socketid] == NULL)
2111 rte_exit(EXIT_FAILURE, "Unable to create the l3fwd LPM table"
2112 " on socket %d\n", socketid);
2114 /* populate the LPM table */
2115 for (i = 0; i < RTE_DIM(ipv4_l3fwd_route_array); i++) {
2116 ret = rte_lpm_add(ipv4_l3fwd_lookup_struct[socketid],
2117 ipv4_l3fwd_route_array[i].ip,
2118 ipv4_l3fwd_route_array[i].depth,
2119 ipv4_l3fwd_route_array[i].if_out);
2122 rte_exit(EXIT_FAILURE, "Unable to add entry %u to the "
2123 "l3fwd LPM table on socket %d\n",
2127 printf("LPM: Adding route 0x%08x / %d (%d)\n",
2128 (unsigned)ipv4_l3fwd_route_array[i].ip,
2129 ipv4_l3fwd_route_array[i].depth,
2130 ipv4_l3fwd_route_array[i].if_out);
2136 init_mem(unsigned nb_mbuf)
2138 struct lcore_conf *qconf;
2143 for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
2144 if (rte_lcore_is_enabled(lcore_id) == 0)
2148 socketid = rte_lcore_to_socket_id(lcore_id);
2152 if (socketid >= NB_SOCKETS) {
2153 rte_exit(EXIT_FAILURE, "Socket %d of lcore %u is "
2154 "out of range %d\n", socketid,
2155 lcore_id, NB_SOCKETS);
2157 if (pktmbuf_pool[socketid] == NULL) {
2158 snprintf(s, sizeof(s), "mbuf_pool_%d", socketid);
2159 pktmbuf_pool[socketid] =
2160 rte_pktmbuf_pool_create(s, nb_mbuf,
2161 MEMPOOL_CACHE_SIZE, 0,
2162 RTE_MBUF_DEFAULT_BUF_SIZE,
2164 if (pktmbuf_pool[socketid] == NULL)
2165 rte_exit(EXIT_FAILURE,
2166 "Cannot init mbuf pool on socket %d\n",
2169 printf("Allocated mbuf pool on socket %d\n",
2172 #if (APP_LOOKUP_METHOD == APP_LOOKUP_LPM)
2173 setup_lpm(socketid);
2175 setup_hash(socketid);
2178 qconf = &lcore_conf[lcore_id];
2179 qconf->ipv4_lookup_struct = ipv4_l3fwd_lookup_struct[socketid];
2180 #if (APP_LOOKUP_METHOD == APP_LOOKUP_EXACT_MATCH)
2181 qconf->ipv6_lookup_struct = ipv6_l3fwd_lookup_struct[socketid];
2187 /* Check the link status of all ports in up to 9s, and print them finally */
2189 check_all_ports_link_status(uint32_t port_mask)
2191 #define CHECK_INTERVAL 100 /* 100ms */
2192 #define MAX_CHECK_TIME 90 /* 9s (90 * 100ms) in total */
2193 uint8_t count, all_ports_up, print_flag = 0;
2195 struct rte_eth_link link;
2197 char link_status_text[RTE_ETH_LINK_MAX_STR_LEN];
2199 printf("\nChecking link status");
2201 for (count = 0; count <= MAX_CHECK_TIME; count++) {
2203 RTE_ETH_FOREACH_DEV(portid) {
2204 if ((port_mask & (1 << portid)) == 0)
2206 memset(&link, 0, sizeof(link));
2207 ret = rte_eth_link_get_nowait(portid, &link);
2210 if (print_flag == 1)
2211 printf("Port %u link get failed: %s\n",
2212 portid, rte_strerror(-ret));
2215 /* print link status if flag set */
2216 if (print_flag == 1) {
2217 rte_eth_link_to_str(link_status_text,
2218 sizeof(link_status_text), &link);
2219 printf("Port %d %s\n", portid,
2223 /* clear all_ports_up flag if any link down */
2224 if (link.link_status == ETH_LINK_DOWN) {
2229 /* after finally printing all link status, get out */
2230 if (print_flag == 1)
2233 if (all_ports_up == 0) {
2236 rte_delay_ms(CHECK_INTERVAL);
2239 /* set the print_flag if all ports up or timeout */
2240 if (all_ports_up == 1 || count == (MAX_CHECK_TIME - 1)) {
2247 static int check_ptype(uint16_t portid)
2250 int ptype_l3_ipv4 = 0;
2251 #if (APP_LOOKUP_METHOD == APP_LOOKUP_EXACT_MATCH)
2252 int ptype_l3_ipv6 = 0;
2254 uint32_t ptype_mask = RTE_PTYPE_L3_MASK;
2256 ret = rte_eth_dev_get_supported_ptypes(portid, ptype_mask, NULL, 0);
2260 uint32_t ptypes[ret];
2262 ret = rte_eth_dev_get_supported_ptypes(portid, ptype_mask, ptypes, ret);
2263 for (i = 0; i < ret; ++i) {
2264 if (ptypes[i] & RTE_PTYPE_L3_IPV4)
2266 #if (APP_LOOKUP_METHOD == APP_LOOKUP_EXACT_MATCH)
2267 if (ptypes[i] & RTE_PTYPE_L3_IPV6)
2272 if (ptype_l3_ipv4 == 0)
2273 printf("port %d cannot parse RTE_PTYPE_L3_IPV4\n", portid);
2275 #if (APP_LOOKUP_METHOD == APP_LOOKUP_EXACT_MATCH)
2276 if (ptype_l3_ipv6 == 0)
2277 printf("port %d cannot parse RTE_PTYPE_L3_IPV6\n", portid);
2280 #if (APP_LOOKUP_METHOD == APP_LOOKUP_LPM)
2282 #else /* APP_LOOKUP_EXACT_MATCH */
2283 if (ptype_l3_ipv4 && ptype_l3_ipv6)
2292 init_power_library(void)
2294 enum power_management_env env;
2295 unsigned int lcore_id;
2298 RTE_LCORE_FOREACH(lcore_id) {
2299 /* init power management library */
2300 ret = rte_power_init(lcore_id);
2303 "Library initialization failed on core %u\n",
2307 /* we're not supporting the VM channel mode */
2308 env = rte_power_get_env();
2309 if (env != PM_ENV_ACPI_CPUFREQ &&
2310 env != PM_ENV_PSTATE_CPUFREQ) {
2312 "Only ACPI and PSTATE mode are supported\n");
2320 deinit_power_library(void)
2322 unsigned int lcore_id;
2325 RTE_LCORE_FOREACH(lcore_id) {
2326 /* deinit power management library */
2327 ret = rte_power_exit(lcore_id);
2330 "Library deinitialization failed on core %u\n",
2339 get_current_stat_values(uint64_t *values)
2341 unsigned int lcore_id = rte_lcore_id();
2342 struct lcore_conf *qconf;
2343 uint64_t app_eps = 0, app_fps = 0, app_br = 0;
2346 RTE_LCORE_FOREACH_WORKER(lcore_id) {
2347 qconf = &lcore_conf[lcore_id];
2348 if (qconf->n_rx_queue == 0)
2351 rte_spinlock_lock(&stats[lcore_id].telemetry_lock);
2352 app_eps += stats[lcore_id].ep_nep[1];
2353 app_fps += stats[lcore_id].fp_nfp[1];
2354 app_br += stats[lcore_id].br;
2355 rte_spinlock_unlock(&stats[lcore_id].telemetry_lock);
2359 values[0] = app_eps/count;
2360 values[1] = app_fps/count;
2361 values[2] = app_br/count;
2363 memset(values, 0, sizeof(uint64_t) * NUM_TELSTATS);
2368 update_telemetry(__rte_unused struct rte_timer *tim,
2369 __rte_unused void *arg)
2372 uint64_t values[NUM_TELSTATS] = {0};
2374 get_current_stat_values(values);
2375 ret = rte_metrics_update_values(RTE_METRICS_GLOBAL, telstats_index,
2376 values, RTE_DIM(values));
2378 RTE_LOG(WARNING, POWER, "failed to update metrcis\n");
2382 handle_app_stats(const char *cmd __rte_unused,
2383 const char *params __rte_unused,
2384 struct rte_tel_data *d)
2386 uint64_t values[NUM_TELSTATS] = {0};
2389 rte_tel_data_start_dict(d);
2390 get_current_stat_values(values);
2391 for (i = 0; i < NUM_TELSTATS; i++)
2392 rte_tel_data_add_dict_u64(d, telstats_strings[i].name,
2398 telemetry_setup_timer(void)
2400 int lcore_id = rte_lcore_id();
2401 uint64_t hz = rte_get_timer_hz();
2404 ticks = hz / TELEMETRY_INTERVALS_PER_SEC;
2405 rte_timer_reset_sync(&telemetry_timer,
2413 empty_poll_setup_timer(void)
2415 int lcore_id = rte_lcore_id();
2416 uint64_t hz = rte_get_timer_hz();
2418 struct ep_params *ep_ptr = ep_params;
2420 ep_ptr->interval_ticks = hz / INTERVALS_PER_SECOND;
2422 rte_timer_reset_sync(&ep_ptr->timer0,
2423 ep_ptr->interval_ticks,
2426 rte_empty_poll_detection,
2431 launch_timer(unsigned int lcore_id)
2433 int64_t prev_tsc = 0, cur_tsc, diff_tsc, cycles_10ms;
2435 RTE_SET_USED(lcore_id);
2438 if (rte_get_main_lcore() != lcore_id) {
2439 rte_panic("timer on lcore:%d which is not main core:%d\n",
2441 rte_get_main_lcore());
2444 RTE_LOG(INFO, POWER, "Bring up the Timer\n");
2446 if (app_mode == APP_MODE_EMPTY_POLL)
2447 empty_poll_setup_timer();
2449 telemetry_setup_timer();
2451 cycles_10ms = rte_get_timer_hz() / 100;
2453 while (!is_done()) {
2454 cur_tsc = rte_rdtsc();
2455 diff_tsc = cur_tsc - prev_tsc;
2456 if (diff_tsc > cycles_10ms) {
2459 cycles_10ms = rte_get_timer_hz() / 100;
2463 RTE_LOG(INFO, POWER, "Timer_subsystem is done\n");
2469 autodetect_mode(void)
2471 RTE_LOG(NOTICE, L3FWD_POWER, "Operating mode not specified, probing frequency scaling support...\n");
2474 * Empty poll and telemetry modes have to be specifically requested to
2475 * be enabled, but we can auto-detect between interrupt mode with or
2476 * without frequency scaling. Both ACPI and pstate can be used.
2478 if (rte_power_check_env_supported(PM_ENV_ACPI_CPUFREQ))
2479 return APP_MODE_LEGACY;
2480 if (rte_power_check_env_supported(PM_ENV_PSTATE_CPUFREQ))
2481 return APP_MODE_LEGACY;
2483 RTE_LOG(NOTICE, L3FWD_POWER, "Frequency scaling not supported, selecting interrupt-only mode\n");
2485 return APP_MODE_INTERRUPT;
2489 mode_to_str(enum appmode mode)
2492 case APP_MODE_LEGACY:
2494 case APP_MODE_EMPTY_POLL:
2495 return "empty poll";
2496 case APP_MODE_TELEMETRY:
2498 case APP_MODE_INTERRUPT:
2499 return "interrupt-only";
2500 case APP_MODE_PMD_MGMT:
2507 /* Power library initialized in the main routine. 8< */
2509 main(int argc, char **argv)
2511 struct lcore_conf *qconf;
2512 struct rte_eth_dev_info dev_info;
2513 struct rte_eth_txconf *txconf;
2519 uint32_t n_tx_queue, nb_lcores;
2520 uint32_t dev_rxq_num, dev_txq_num;
2521 uint8_t nb_rx_queue, queue, socketid;
2523 const char *ptr_strings[NUM_TELSTATS];
2525 /* catch SIGINT and restore cpufreq governor to ondemand */
2526 signal(SIGINT, signal_exit_now);
2529 ret = rte_eal_init(argc, argv);
2531 rte_exit(EXIT_FAILURE, "Invalid EAL parameters\n");
2535 /* init RTE timer library to be used late */
2536 rte_timer_subsystem_init();
2538 /* if we're running pmd-mgmt mode, don't default to baseline mode */
2539 baseline_enabled = false;
2541 /* parse application arguments (after the EAL ones) */
2542 ret = parse_args(argc, argv);
2544 rte_exit(EXIT_FAILURE, "Invalid L3FWD parameters\n");
2546 if (app_mode == APP_MODE_DEFAULT)
2547 app_mode = autodetect_mode();
2549 RTE_LOG(INFO, L3FWD_POWER, "Selected operation mode: %s\n",
2550 mode_to_str(app_mode));
2552 /* only legacy and empty poll mode rely on power library */
2553 if ((app_mode == APP_MODE_LEGACY || app_mode == APP_MODE_EMPTY_POLL) &&
2554 init_power_library())
2555 rte_exit(EXIT_FAILURE, "init_power_library failed\n");
2557 if (update_lcore_params() < 0)
2558 rte_exit(EXIT_FAILURE, "update_lcore_params failed\n");
2560 if (check_lcore_params() < 0)
2561 rte_exit(EXIT_FAILURE, "check_lcore_params failed\n");
2563 ret = init_lcore_rx_queues();
2565 rte_exit(EXIT_FAILURE, "init_lcore_rx_queues failed\n");
2567 nb_ports = rte_eth_dev_count_avail();
2569 if (check_port_config() < 0)
2570 rte_exit(EXIT_FAILURE, "check_port_config failed\n");
2572 nb_lcores = rte_lcore_count();
2574 /* initialize all ports */
2575 RTE_ETH_FOREACH_DEV(portid) {
2576 struct rte_eth_conf local_port_conf = port_conf;
2577 /* not all app modes need interrupts */
2578 bool need_intr = app_mode == APP_MODE_LEGACY ||
2579 app_mode == APP_MODE_INTERRUPT;
2581 /* skip ports that are not enabled */
2582 if ((enabled_port_mask & (1 << portid)) == 0) {
2583 printf("\nSkipping disabled port %d\n", portid);
2588 printf("Initializing port %d ... ", portid );
2591 ret = rte_eth_dev_info_get(portid, &dev_info);
2593 rte_exit(EXIT_FAILURE,
2594 "Error during getting device (port %u) info: %s\n",
2595 portid, strerror(-ret));
2597 dev_rxq_num = dev_info.max_rx_queues;
2598 dev_txq_num = dev_info.max_tx_queues;
2600 nb_rx_queue = get_port_n_rx_queues(portid);
2601 if (nb_rx_queue > dev_rxq_num)
2602 rte_exit(EXIT_FAILURE,
2603 "Cannot configure not existed rxq: "
2604 "port=%d\n", portid);
2606 n_tx_queue = nb_lcores;
2607 if (n_tx_queue > dev_txq_num)
2608 n_tx_queue = dev_txq_num;
2609 printf("Creating queues: nb_rxq=%d nb_txq=%u... ",
2610 nb_rx_queue, (unsigned)n_tx_queue );
2611 /* If number of Rx queue is 0, no need to enable Rx interrupt */
2612 if (nb_rx_queue == 0)
2616 local_port_conf.intr_conf.rxq = 1;
2618 ret = rte_eth_dev_info_get(portid, &dev_info);
2620 rte_exit(EXIT_FAILURE,
2621 "Error during getting device (port %u) info: %s\n",
2622 portid, strerror(-ret));
2624 if (dev_info.tx_offload_capa & DEV_TX_OFFLOAD_MBUF_FAST_FREE)
2625 local_port_conf.txmode.offloads |=
2626 DEV_TX_OFFLOAD_MBUF_FAST_FREE;
2628 local_port_conf.rx_adv_conf.rss_conf.rss_hf &=
2629 dev_info.flow_type_rss_offloads;
2630 if (local_port_conf.rx_adv_conf.rss_conf.rss_hf !=
2631 port_conf.rx_adv_conf.rss_conf.rss_hf) {
2632 printf("Port %u modified RSS hash function based on hardware support,"
2633 "requested:%#"PRIx64" configured:%#"PRIx64"\n",
2635 port_conf.rx_adv_conf.rss_conf.rss_hf,
2636 local_port_conf.rx_adv_conf.rss_conf.rss_hf);
2639 ret = rte_eth_dev_configure(portid, nb_rx_queue,
2640 (uint16_t)n_tx_queue, &local_port_conf);
2642 rte_exit(EXIT_FAILURE, "Cannot configure device: "
2643 "err=%d, port=%d\n", ret, portid);
2645 ret = rte_eth_dev_adjust_nb_rx_tx_desc(portid, &nb_rxd,
2648 rte_exit(EXIT_FAILURE,
2649 "Cannot adjust number of descriptors: err=%d, port=%d\n",
2652 ret = rte_eth_macaddr_get(portid, &ports_eth_addr[portid]);
2654 rte_exit(EXIT_FAILURE,
2655 "Cannot get MAC address: err=%d, port=%d\n",
2658 print_ethaddr(" Address:", &ports_eth_addr[portid]);
2662 ret = init_mem(NB_MBUF);
2664 rte_exit(EXIT_FAILURE, "init_mem failed\n");
2666 for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
2667 if (rte_lcore_is_enabled(lcore_id) == 0)
2670 /* Initialize TX buffers */
2671 qconf = &lcore_conf[lcore_id];
2672 qconf->tx_buffer[portid] = rte_zmalloc_socket("tx_buffer",
2673 RTE_ETH_TX_BUFFER_SIZE(MAX_PKT_BURST), 0,
2674 rte_eth_dev_socket_id(portid));
2675 if (qconf->tx_buffer[portid] == NULL)
2676 rte_exit(EXIT_FAILURE, "Can't allocate tx buffer for port %u\n",
2679 rte_eth_tx_buffer_init(qconf->tx_buffer[portid], MAX_PKT_BURST);
2682 /* init one TX queue per couple (lcore,port) */
2684 for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
2685 if (rte_lcore_is_enabled(lcore_id) == 0)
2688 if (queueid >= dev_txq_num)
2693 (uint8_t)rte_lcore_to_socket_id(lcore_id);
2697 printf("txq=%u,%d,%d ", lcore_id, queueid, socketid);
2700 txconf = &dev_info.default_txconf;
2701 txconf->offloads = local_port_conf.txmode.offloads;
2702 ret = rte_eth_tx_queue_setup(portid, queueid, nb_txd,
2705 rte_exit(EXIT_FAILURE,
2706 "rte_eth_tx_queue_setup: err=%d, "
2707 "port=%d\n", ret, portid);
2709 qconf = &lcore_conf[lcore_id];
2710 qconf->tx_queue_id[portid] = queueid;
2713 qconf->tx_port_id[qconf->n_tx_port] = portid;
2719 for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
2720 if (rte_lcore_is_enabled(lcore_id) == 0)
2723 if (app_mode == APP_MODE_LEGACY) {
2724 /* init timer structures for each enabled lcore */
2725 rte_timer_init(&power_timers[lcore_id]);
2726 hz = rte_get_timer_hz();
2727 rte_timer_reset(&power_timers[lcore_id],
2728 hz/TIMER_NUMBER_PER_SECOND,
2730 power_timer_cb, NULL);
2732 qconf = &lcore_conf[lcore_id];
2733 printf("\nInitializing rx queues on lcore %u ... ", lcore_id );
2736 /* init RX queues */
2737 for(queue = 0; queue < qconf->n_rx_queue; ++queue) {
2738 struct rte_eth_rxconf rxq_conf;
2740 portid = qconf->rx_queue_list[queue].port_id;
2741 queueid = qconf->rx_queue_list[queue].queue_id;
2745 (uint8_t)rte_lcore_to_socket_id(lcore_id);
2749 printf("rxq=%d,%d,%d ", portid, queueid, socketid);
2752 ret = rte_eth_dev_info_get(portid, &dev_info);
2754 rte_exit(EXIT_FAILURE,
2755 "Error during getting device (port %u) info: %s\n",
2756 portid, strerror(-ret));
2758 rxq_conf = dev_info.default_rxconf;
2759 rxq_conf.offloads = port_conf.rxmode.offloads;
2760 ret = rte_eth_rx_queue_setup(portid, queueid, nb_rxd,
2761 socketid, &rxq_conf,
2762 pktmbuf_pool[socketid]);
2764 rte_exit(EXIT_FAILURE,
2765 "rte_eth_rx_queue_setup: err=%d, "
2766 "port=%d\n", ret, portid);
2769 if (add_cb_parse_ptype(portid, queueid) < 0)
2770 rte_exit(EXIT_FAILURE,
2771 "Fail to add ptype cb\n");
2774 if (app_mode == APP_MODE_PMD_MGMT && !baseline_enabled) {
2775 ret = rte_power_ethdev_pmgmt_queue_enable(
2776 lcore_id, portid, queueid,
2779 rte_exit(EXIT_FAILURE,
2780 "rte_power_ethdev_pmgmt_queue_enable: err=%d, port=%d\n",
2785 /* >8 End of power library initialization. */
2790 RTE_ETH_FOREACH_DEV(portid) {
2791 if ((enabled_port_mask & (1 << portid)) == 0) {
2795 ret = rte_eth_dev_start(portid);
2797 rte_exit(EXIT_FAILURE, "rte_eth_dev_start: err=%d, "
2798 "port=%d\n", ret, portid);
2800 * If enabled, put device in promiscuous mode.
2801 * This allows IO forwarding mode to forward packets
2802 * to itself through 2 cross-connected ports of the
2805 if (promiscuous_on) {
2806 ret = rte_eth_promiscuous_enable(portid);
2808 rte_exit(EXIT_FAILURE,
2809 "rte_eth_promiscuous_enable: err=%s, port=%u\n",
2810 rte_strerror(-ret), portid);
2812 /* initialize spinlock for each port */
2813 rte_spinlock_init(&(locks[portid]));
2816 if (!check_ptype(portid))
2817 rte_exit(EXIT_FAILURE,
2818 "PMD can not provide needed ptypes\n");
2821 check_all_ports_link_status(enabled_port_mask);
2823 if (app_mode == APP_MODE_EMPTY_POLL) {
2825 if (empty_poll_train) {
2826 policy.state = TRAINING;
2828 policy.state = MED_NORMAL;
2829 policy.med_base_edpi = ep_med_edpi;
2830 policy.hgh_base_edpi = ep_hgh_edpi;
2833 ret = rte_power_empty_poll_stat_init(&ep_params,
2837 rte_exit(EXIT_FAILURE, "empty poll init failed");
2841 /* launch per-lcore init on every lcore */
2842 if (app_mode == APP_MODE_LEGACY) {
2843 rte_eal_mp_remote_launch(main_legacy_loop, NULL, CALL_MAIN);
2844 } else if (app_mode == APP_MODE_EMPTY_POLL) {
2845 empty_poll_stop = false;
2846 rte_eal_mp_remote_launch(main_empty_poll_loop, NULL,
2848 } else if (app_mode == APP_MODE_TELEMETRY) {
2851 /* Init metrics library */
2852 rte_metrics_init(rte_socket_id());
2853 /** Register stats with metrics library */
2854 for (i = 0; i < NUM_TELSTATS; i++)
2855 ptr_strings[i] = telstats_strings[i].name;
2857 ret = rte_metrics_reg_names(ptr_strings, NUM_TELSTATS);
2859 telstats_index = ret;
2861 rte_exit(EXIT_FAILURE, "failed to register metrics names");
2863 RTE_LCORE_FOREACH_WORKER(lcore_id) {
2864 rte_spinlock_init(&stats[lcore_id].telemetry_lock);
2866 rte_timer_init(&telemetry_timer);
2867 rte_telemetry_register_cmd("/l3fwd-power/stats",
2869 "Returns global power stats. Parameters: None");
2870 rte_eal_mp_remote_launch(main_telemetry_loop, NULL,
2872 } else if (app_mode == APP_MODE_INTERRUPT) {
2873 rte_eal_mp_remote_launch(main_intr_loop, NULL, CALL_MAIN);
2874 } else if (app_mode == APP_MODE_PMD_MGMT) {
2875 /* reuse telemetry loop for PMD power management mode */
2876 rte_eal_mp_remote_launch(main_telemetry_loop, NULL, CALL_MAIN);
2879 if (app_mode == APP_MODE_EMPTY_POLL || app_mode == APP_MODE_TELEMETRY)
2880 launch_timer(rte_lcore_id());
2882 RTE_LCORE_FOREACH_WORKER(lcore_id) {
2883 if (rte_eal_wait_lcore(lcore_id) < 0)
2887 if (app_mode == APP_MODE_PMD_MGMT) {
2888 for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
2889 if (rte_lcore_is_enabled(lcore_id) == 0)
2891 qconf = &lcore_conf[lcore_id];
2892 for (queue = 0; queue < qconf->n_rx_queue; ++queue) {
2893 portid = qconf->rx_queue_list[queue].port_id;
2894 queueid = qconf->rx_queue_list[queue].queue_id;
2896 rte_power_ethdev_pmgmt_queue_disable(lcore_id,
2902 RTE_ETH_FOREACH_DEV(portid)
2904 if ((enabled_port_mask & (1 << portid)) == 0)
2907 ret = rte_eth_dev_stop(portid);
2909 RTE_LOG(ERR, L3FWD_POWER, "rte_eth_dev_stop: err=%d, port=%u\n",
2912 rte_eth_dev_close(portid);
2915 if (app_mode == APP_MODE_EMPTY_POLL)
2916 rte_power_empty_poll_stat_free();
2918 if ((app_mode == APP_MODE_LEGACY || app_mode == APP_MODE_EMPTY_POLL) &&
2919 deinit_power_library())
2920 rte_exit(EXIT_FAILURE, "deinit_power_library failed\n");
2922 if (rte_eal_cleanup() < 0)
2923 RTE_LOG(ERR, L3FWD_POWER, "EAL cleanup failed\n");