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>
49 #include <rte_telemetry.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,
205 enum appmode app_mode;
207 enum freq_scale_hint_t
215 struct lcore_rx_queue {
218 enum freq_scale_hint_t freq_up_hint;
219 uint32_t zero_rx_packet_count;
221 } __rte_cache_aligned;
223 #define MAX_RX_QUEUE_PER_LCORE 16
224 #define MAX_TX_QUEUE_PER_PORT RTE_MAX_ETHPORTS
225 #define MAX_RX_QUEUE_PER_PORT 128
227 #define MAX_RX_QUEUE_INTERRUPT_PER_PORT 16
230 struct lcore_params lcore_params_array[MAX_LCORE_PARAMS];
231 static struct lcore_params lcore_params_array_default[] = {
243 struct lcore_params *lcore_params = lcore_params_array_default;
244 uint16_t nb_lcore_params = RTE_DIM(lcore_params_array_default);
246 static struct rte_eth_conf port_conf = {
248 .mq_mode = ETH_MQ_RX_RSS,
249 .max_rx_pkt_len = RTE_ETHER_MAX_LEN,
251 .offloads = DEV_RX_OFFLOAD_CHECKSUM,
256 .rss_hf = ETH_RSS_UDP,
260 .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)
827 * we want to track when we are woken up by traffic so that we can go
828 * back to sleep again without log spamming.
831 struct rte_epoll_event event[num];
838 RTE_LOG(INFO, L3FWD_POWER,
839 "lcore %u sleeps until interrupt triggers\n",
843 n = rte_epoll_wait(RTE_EPOLL_PER_THREAD, event, num, 10);
844 for (i = 0; i < n; i++) {
845 data = event[i].epdata.data;
846 port_id = ((uintptr_t)data) >> CHAR_BIT;
847 queue_id = ((uintptr_t)data) &
848 RTE_LEN2MASK(CHAR_BIT, uint8_t);
849 RTE_LOG(INFO, L3FWD_POWER,
850 "lcore %u is waked up from rx interrupt on"
851 " port %d queue %d\n",
852 rte_lcore_id(), port_id, queue_id);
859 static void turn_on_off_intr(struct lcore_conf *qconf, bool on)
862 struct lcore_rx_queue *rx_queue;
866 for (i = 0; i < qconf->n_rx_queue; ++i) {
867 rx_queue = &(qconf->rx_queue_list[i]);
868 port_id = rx_queue->port_id;
869 queue_id = rx_queue->queue_id;
871 rte_spinlock_lock(&(locks[port_id]));
873 rte_eth_dev_rx_intr_enable(port_id, queue_id);
875 rte_eth_dev_rx_intr_disable(port_id, queue_id);
876 rte_spinlock_unlock(&(locks[port_id]));
880 static int event_register(struct lcore_conf *qconf)
882 struct lcore_rx_queue *rx_queue;
889 for (i = 0; i < qconf->n_rx_queue; ++i) {
890 rx_queue = &(qconf->rx_queue_list[i]);
891 portid = rx_queue->port_id;
892 queueid = rx_queue->queue_id;
893 data = portid << CHAR_BIT | queueid;
895 ret = rte_eth_dev_rx_intr_ctl_q(portid, queueid,
896 RTE_EPOLL_PER_THREAD,
898 (void *)((uintptr_t)data));
906 /* main processing loop */
907 static int main_intr_loop(__rte_unused void *dummy)
909 struct rte_mbuf *pkts_burst[MAX_PKT_BURST];
910 unsigned int lcore_id;
911 uint64_t prev_tsc, diff_tsc, cur_tsc;
915 struct lcore_conf *qconf;
916 struct lcore_rx_queue *rx_queue;
917 uint32_t lcore_rx_idle_count = 0;
918 uint32_t lcore_idle_hint = 0;
921 const uint64_t drain_tsc = (rte_get_tsc_hz() + US_PER_S - 1) /
922 US_PER_S * BURST_TX_DRAIN_US;
926 lcore_id = rte_lcore_id();
927 qconf = &lcore_conf[lcore_id];
929 if (qconf->n_rx_queue == 0) {
930 RTE_LOG(INFO, L3FWD_POWER, "lcore %u has nothing to do\n",
935 RTE_LOG(INFO, L3FWD_POWER, "entering main interrupt loop on lcore %u\n",
938 for (i = 0; i < qconf->n_rx_queue; i++) {
939 portid = qconf->rx_queue_list[i].port_id;
940 queueid = qconf->rx_queue_list[i].queue_id;
941 RTE_LOG(INFO, L3FWD_POWER,
942 " -- lcoreid=%u portid=%u rxqueueid=%hhu\n",
943 lcore_id, portid, queueid);
946 /* add into event wait list */
947 if (event_register(qconf) == 0)
950 RTE_LOG(INFO, L3FWD_POWER, "RX interrupt won't enable.\n");
953 stats[lcore_id].nb_iteration_looped++;
955 cur_tsc = rte_rdtsc();
958 * TX burst queue drain
960 diff_tsc = cur_tsc - prev_tsc;
961 if (unlikely(diff_tsc > drain_tsc)) {
962 for (i = 0; i < qconf->n_tx_port; ++i) {
963 portid = qconf->tx_port_id[i];
964 rte_eth_tx_buffer_flush(portid,
965 qconf->tx_queue_id[portid],
966 qconf->tx_buffer[portid]);
973 * Read packet from RX queues
975 lcore_rx_idle_count = 0;
976 for (i = 0; i < qconf->n_rx_queue; ++i) {
977 rx_queue = &(qconf->rx_queue_list[i]);
978 rx_queue->idle_hint = 0;
979 portid = rx_queue->port_id;
980 queueid = rx_queue->queue_id;
982 nb_rx = rte_eth_rx_burst(portid, queueid, pkts_burst,
985 stats[lcore_id].nb_rx_processed += nb_rx;
986 if (unlikely(nb_rx == 0)) {
988 * no packet received from rx queue, try to
989 * sleep for a while forcing CPU enter deeper
992 rx_queue->zero_rx_packet_count++;
994 if (rx_queue->zero_rx_packet_count <=
998 rx_queue->idle_hint = power_idle_heuristic(
999 rx_queue->zero_rx_packet_count);
1000 lcore_rx_idle_count++;
1002 rx_queue->zero_rx_packet_count = 0;
1005 /* Prefetch first packets */
1006 for (j = 0; j < PREFETCH_OFFSET && j < nb_rx; j++) {
1007 rte_prefetch0(rte_pktmbuf_mtod(
1008 pkts_burst[j], void *));
1011 /* Prefetch and forward already prefetched packets */
1012 for (j = 0; j < (nb_rx - PREFETCH_OFFSET); j++) {
1013 rte_prefetch0(rte_pktmbuf_mtod(
1014 pkts_burst[j + PREFETCH_OFFSET],
1016 l3fwd_simple_forward(
1017 pkts_burst[j], portid, qconf);
1020 /* Forward remaining prefetched packets */
1021 for (; j < nb_rx; j++) {
1022 l3fwd_simple_forward(
1023 pkts_burst[j], portid, qconf);
1027 if (unlikely(lcore_rx_idle_count == qconf->n_rx_queue)) {
1029 * All Rx queues empty in recent consecutive polls,
1030 * sleep in a conservative manner, meaning sleep as
1034 lcore_idle_hint = qconf->rx_queue_list[0].idle_hint;
1035 i < qconf->n_rx_queue; ++i) {
1036 rx_queue = &(qconf->rx_queue_list[i]);
1037 if (rx_queue->idle_hint < lcore_idle_hint)
1038 lcore_idle_hint = rx_queue->idle_hint;
1041 if (lcore_idle_hint < SUSPEND_THRESHOLD)
1043 * execute "pause" instruction to avoid context
1044 * switch which generally take hundred of
1045 * microseconds for short sleep.
1047 rte_delay_us(lcore_idle_hint);
1049 /* suspend until rx interrupt triggers */
1051 turn_on_off_intr(qconf, 1);
1052 sleep_until_rx_interrupt(
1054 turn_on_off_intr(qconf, 0);
1056 * start receiving packets immediately
1058 if (likely(!is_done()))
1062 stats[lcore_id].sleep_time += lcore_idle_hint;
1069 /* main processing loop */
1071 main_telemetry_loop(__rte_unused void *dummy)
1073 struct rte_mbuf *pkts_burst[MAX_PKT_BURST];
1074 unsigned int lcore_id;
1075 uint64_t prev_tsc, diff_tsc, cur_tsc, prev_tel_tsc;
1079 struct lcore_conf *qconf;
1080 struct lcore_rx_queue *rx_queue;
1081 uint64_t ep_nep[2] = {0}, fp_nfp[2] = {0};
1082 uint64_t poll_count;
1085 const uint64_t drain_tsc = (rte_get_tsc_hz() + US_PER_S - 1) /
1086 US_PER_S * BURST_TX_DRAIN_US;
1092 lcore_id = rte_lcore_id();
1093 qconf = &lcore_conf[lcore_id];
1095 if (qconf->n_rx_queue == 0) {
1096 RTE_LOG(INFO, L3FWD_POWER, "lcore %u has nothing to do\n",
1101 RTE_LOG(INFO, L3FWD_POWER, "entering main telemetry loop on lcore %u\n",
1104 for (i = 0; i < qconf->n_rx_queue; i++) {
1105 portid = qconf->rx_queue_list[i].port_id;
1106 queueid = qconf->rx_queue_list[i].queue_id;
1107 RTE_LOG(INFO, L3FWD_POWER, " -- lcoreid=%u portid=%u "
1108 "rxqueueid=%hhu\n", lcore_id, portid, queueid);
1111 while (!is_done()) {
1113 cur_tsc = rte_rdtsc();
1115 * TX burst queue drain
1117 diff_tsc = cur_tsc - prev_tsc;
1118 if (unlikely(diff_tsc > drain_tsc)) {
1119 for (i = 0; i < qconf->n_tx_port; ++i) {
1120 portid = qconf->tx_port_id[i];
1121 rte_eth_tx_buffer_flush(portid,
1122 qconf->tx_queue_id[portid],
1123 qconf->tx_buffer[portid]);
1129 * Read packet from RX queues
1131 for (i = 0; i < qconf->n_rx_queue; ++i) {
1132 rx_queue = &(qconf->rx_queue_list[i]);
1133 portid = rx_queue->port_id;
1134 queueid = rx_queue->queue_id;
1136 nb_rx = rte_eth_rx_burst(portid, queueid, pkts_burst,
1138 ep_nep[nb_rx == 0]++;
1139 fp_nfp[nb_rx == MAX_PKT_BURST]++;
1141 if (unlikely(nb_rx == 0))
1144 /* Prefetch first packets */
1145 for (j = 0; j < PREFETCH_OFFSET && j < nb_rx; j++) {
1146 rte_prefetch0(rte_pktmbuf_mtod(
1147 pkts_burst[j], void *));
1150 /* Prefetch and forward already prefetched packets */
1151 for (j = 0; j < (nb_rx - PREFETCH_OFFSET); j++) {
1152 rte_prefetch0(rte_pktmbuf_mtod(pkts_burst[
1153 j + PREFETCH_OFFSET], void *));
1154 l3fwd_simple_forward(pkts_burst[j], portid,
1158 /* Forward remaining prefetched packets */
1159 for (; j < nb_rx; j++) {
1160 l3fwd_simple_forward(pkts_burst[j], portid,
1164 if (unlikely(poll_count >= DEFAULT_COUNT)) {
1165 diff_tsc = cur_tsc - prev_tel_tsc;
1166 if (diff_tsc >= MAX_CYCLES) {
1168 } else if (diff_tsc > MIN_CYCLES &&
1169 diff_tsc < MAX_CYCLES) {
1170 br = (diff_tsc * 100) / MAX_CYCLES;
1175 prev_tel_tsc = cur_tsc;
1176 /* update stats for telemetry */
1177 rte_spinlock_lock(&stats[lcore_id].telemetry_lock);
1178 stats[lcore_id].ep_nep[0] = ep_nep[0];
1179 stats[lcore_id].ep_nep[1] = ep_nep[1];
1180 stats[lcore_id].fp_nfp[0] = fp_nfp[0];
1181 stats[lcore_id].fp_nfp[1] = fp_nfp[1];
1182 stats[lcore_id].br = br;
1183 rte_spinlock_unlock(&stats[lcore_id].telemetry_lock);
1189 /* main processing loop */
1191 main_empty_poll_loop(__rte_unused void *dummy)
1193 struct rte_mbuf *pkts_burst[MAX_PKT_BURST];
1194 unsigned int lcore_id;
1195 uint64_t prev_tsc, diff_tsc, cur_tsc;
1199 struct lcore_conf *qconf;
1200 struct lcore_rx_queue *rx_queue;
1202 const uint64_t drain_tsc =
1203 (rte_get_tsc_hz() + US_PER_S - 1) /
1204 US_PER_S * BURST_TX_DRAIN_US;
1208 lcore_id = rte_lcore_id();
1209 qconf = &lcore_conf[lcore_id];
1211 if (qconf->n_rx_queue == 0) {
1212 RTE_LOG(INFO, L3FWD_POWER, "lcore %u has nothing to do\n",
1217 for (i = 0; i < qconf->n_rx_queue; i++) {
1218 portid = qconf->rx_queue_list[i].port_id;
1219 queueid = qconf->rx_queue_list[i].queue_id;
1220 RTE_LOG(INFO, L3FWD_POWER, " -- lcoreid=%u portid=%u "
1221 "rxqueueid=%hhu\n", lcore_id, portid, queueid);
1224 while (!is_done()) {
1225 stats[lcore_id].nb_iteration_looped++;
1227 cur_tsc = rte_rdtsc();
1229 * TX burst queue drain
1231 diff_tsc = cur_tsc - prev_tsc;
1232 if (unlikely(diff_tsc > drain_tsc)) {
1233 for (i = 0; i < qconf->n_tx_port; ++i) {
1234 portid = qconf->tx_port_id[i];
1235 rte_eth_tx_buffer_flush(portid,
1236 qconf->tx_queue_id[portid],
1237 qconf->tx_buffer[portid]);
1243 * Read packet from RX queues
1245 for (i = 0; i < qconf->n_rx_queue; ++i) {
1246 rx_queue = &(qconf->rx_queue_list[i]);
1247 rx_queue->idle_hint = 0;
1248 portid = rx_queue->port_id;
1249 queueid = rx_queue->queue_id;
1251 nb_rx = rte_eth_rx_burst(portid, queueid, pkts_burst,
1254 stats[lcore_id].nb_rx_processed += nb_rx;
1258 rte_power_empty_poll_stat_update(lcore_id);
1262 rte_power_poll_stat_update(lcore_id, nb_rx);
1266 /* Prefetch first packets */
1267 for (j = 0; j < PREFETCH_OFFSET && j < nb_rx; j++) {
1268 rte_prefetch0(rte_pktmbuf_mtod(
1269 pkts_burst[j], void *));
1272 /* Prefetch and forward already prefetched packets */
1273 for (j = 0; j < (nb_rx - PREFETCH_OFFSET); j++) {
1274 rte_prefetch0(rte_pktmbuf_mtod(pkts_burst[
1275 j + PREFETCH_OFFSET],
1277 l3fwd_simple_forward(pkts_burst[j], portid,
1281 /* Forward remaining prefetched packets */
1282 for (; j < nb_rx; j++) {
1283 l3fwd_simple_forward(pkts_burst[j], portid,
1293 /* main processing loop */
1295 main_legacy_loop(__rte_unused void *dummy)
1297 struct rte_mbuf *pkts_burst[MAX_PKT_BURST];
1299 uint64_t prev_tsc, diff_tsc, cur_tsc, tim_res_tsc, hz;
1300 uint64_t prev_tsc_power = 0, cur_tsc_power, diff_tsc_power;
1304 struct lcore_conf *qconf;
1305 struct lcore_rx_queue *rx_queue;
1306 enum freq_scale_hint_t lcore_scaleup_hint;
1307 uint32_t lcore_rx_idle_count = 0;
1308 uint32_t lcore_idle_hint = 0;
1311 const uint64_t drain_tsc = (rte_get_tsc_hz() + US_PER_S - 1) / US_PER_S * BURST_TX_DRAIN_US;
1314 hz = rte_get_timer_hz();
1315 tim_res_tsc = hz/TIMER_NUMBER_PER_SECOND;
1317 lcore_id = rte_lcore_id();
1318 qconf = &lcore_conf[lcore_id];
1320 if (qconf->n_rx_queue == 0) {
1321 RTE_LOG(INFO, L3FWD_POWER, "lcore %u has nothing to do\n", lcore_id);
1325 RTE_LOG(INFO, L3FWD_POWER, "entering main loop on lcore %u\n", lcore_id);
1327 for (i = 0; i < qconf->n_rx_queue; i++) {
1328 portid = qconf->rx_queue_list[i].port_id;
1329 queueid = qconf->rx_queue_list[i].queue_id;
1330 RTE_LOG(INFO, L3FWD_POWER, " -- lcoreid=%u portid=%u "
1331 "rxqueueid=%hhu\n", lcore_id, portid, queueid);
1334 /* add into event wait list */
1335 if (event_register(qconf) == 0)
1338 RTE_LOG(INFO, L3FWD_POWER, "RX interrupt won't enable.\n");
1340 while (!is_done()) {
1341 stats[lcore_id].nb_iteration_looped++;
1343 cur_tsc = rte_rdtsc();
1344 cur_tsc_power = cur_tsc;
1347 * TX burst queue drain
1349 diff_tsc = cur_tsc - prev_tsc;
1350 if (unlikely(diff_tsc > drain_tsc)) {
1351 for (i = 0; i < qconf->n_tx_port; ++i) {
1352 portid = qconf->tx_port_id[i];
1353 rte_eth_tx_buffer_flush(portid,
1354 qconf->tx_queue_id[portid],
1355 qconf->tx_buffer[portid]);
1360 diff_tsc_power = cur_tsc_power - prev_tsc_power;
1361 if (diff_tsc_power > tim_res_tsc) {
1363 prev_tsc_power = cur_tsc_power;
1368 * Read packet from RX queues
1370 lcore_scaleup_hint = FREQ_CURRENT;
1371 lcore_rx_idle_count = 0;
1372 for (i = 0; i < qconf->n_rx_queue; ++i) {
1373 rx_queue = &(qconf->rx_queue_list[i]);
1374 rx_queue->idle_hint = 0;
1375 portid = rx_queue->port_id;
1376 queueid = rx_queue->queue_id;
1378 nb_rx = rte_eth_rx_burst(portid, queueid, pkts_burst,
1381 stats[lcore_id].nb_rx_processed += nb_rx;
1382 if (unlikely(nb_rx == 0)) {
1384 * no packet received from rx queue, try to
1385 * sleep for a while forcing CPU enter deeper
1388 rx_queue->zero_rx_packet_count++;
1390 if (rx_queue->zero_rx_packet_count <=
1391 MIN_ZERO_POLL_COUNT)
1394 rx_queue->idle_hint = power_idle_heuristic(\
1395 rx_queue->zero_rx_packet_count);
1396 lcore_rx_idle_count++;
1398 rx_queue->zero_rx_packet_count = 0;
1401 * do not scale up frequency immediately as
1402 * user to kernel space communication is costly
1403 * which might impact packet I/O for received
1406 rx_queue->freq_up_hint =
1407 power_freq_scaleup_heuristic(lcore_id,
1411 /* Prefetch first packets */
1412 for (j = 0; j < PREFETCH_OFFSET && j < nb_rx; j++) {
1413 rte_prefetch0(rte_pktmbuf_mtod(
1414 pkts_burst[j], void *));
1417 /* Prefetch and forward already prefetched packets */
1418 for (j = 0; j < (nb_rx - PREFETCH_OFFSET); j++) {
1419 rte_prefetch0(rte_pktmbuf_mtod(pkts_burst[
1420 j + PREFETCH_OFFSET], void *));
1421 l3fwd_simple_forward(pkts_burst[j], portid,
1425 /* Forward remaining prefetched packets */
1426 for (; j < nb_rx; j++) {
1427 l3fwd_simple_forward(pkts_burst[j], portid,
1432 if (likely(lcore_rx_idle_count != qconf->n_rx_queue)) {
1433 for (i = 1, lcore_scaleup_hint =
1434 qconf->rx_queue_list[0].freq_up_hint;
1435 i < qconf->n_rx_queue; ++i) {
1436 rx_queue = &(qconf->rx_queue_list[i]);
1437 if (rx_queue->freq_up_hint >
1439 lcore_scaleup_hint =
1440 rx_queue->freq_up_hint;
1443 if (lcore_scaleup_hint == FREQ_HIGHEST) {
1444 if (rte_power_freq_max)
1445 rte_power_freq_max(lcore_id);
1446 } else if (lcore_scaleup_hint == FREQ_HIGHER) {
1447 if (rte_power_freq_up)
1448 rte_power_freq_up(lcore_id);
1452 * All Rx queues empty in recent consecutive polls,
1453 * sleep in a conservative manner, meaning sleep as
1456 for (i = 1, lcore_idle_hint =
1457 qconf->rx_queue_list[0].idle_hint;
1458 i < qconf->n_rx_queue; ++i) {
1459 rx_queue = &(qconf->rx_queue_list[i]);
1460 if (rx_queue->idle_hint < lcore_idle_hint)
1461 lcore_idle_hint = rx_queue->idle_hint;
1464 if (lcore_idle_hint < SUSPEND_THRESHOLD)
1466 * execute "pause" instruction to avoid context
1467 * switch which generally take hundred of
1468 * microseconds for short sleep.
1470 rte_delay_us(lcore_idle_hint);
1472 /* suspend until rx interrupt triggers */
1474 turn_on_off_intr(qconf, 1);
1475 sleep_until_rx_interrupt(
1477 turn_on_off_intr(qconf, 0);
1479 * start receiving packets immediately
1481 if (likely(!is_done()))
1485 stats[lcore_id].sleep_time += lcore_idle_hint;
1493 check_lcore_params(void)
1495 uint8_t queue, lcore;
1499 for (i = 0; i < nb_lcore_params; ++i) {
1500 queue = lcore_params[i].queue_id;
1501 if (queue >= MAX_RX_QUEUE_PER_PORT) {
1502 printf("invalid queue number: %hhu\n", queue);
1505 lcore = lcore_params[i].lcore_id;
1506 if (!rte_lcore_is_enabled(lcore)) {
1507 printf("error: lcore %hhu is not enabled in lcore "
1511 if ((socketid = rte_lcore_to_socket_id(lcore) != 0) &&
1513 printf("warning: lcore %hhu is on socket %d with numa "
1514 "off\n", lcore, socketid);
1516 if (app_mode == APP_MODE_TELEMETRY && lcore == rte_lcore_id()) {
1517 printf("cannot enable master core %d in config for telemetry mode\n",
1526 check_port_config(void)
1531 for (i = 0; i < nb_lcore_params; ++i) {
1532 portid = lcore_params[i].port_id;
1533 if ((enabled_port_mask & (1 << portid)) == 0) {
1534 printf("port %u is not enabled in port mask\n",
1538 if (!rte_eth_dev_is_valid_port(portid)) {
1539 printf("port %u is not present on the board\n",
1548 get_port_n_rx_queues(const uint16_t port)
1553 for (i = 0; i < nb_lcore_params; ++i) {
1554 if (lcore_params[i].port_id == port &&
1555 lcore_params[i].queue_id > queue)
1556 queue = lcore_params[i].queue_id;
1558 return (uint8_t)(++queue);
1562 init_lcore_rx_queues(void)
1564 uint16_t i, nb_rx_queue;
1567 for (i = 0; i < nb_lcore_params; ++i) {
1568 lcore = lcore_params[i].lcore_id;
1569 nb_rx_queue = lcore_conf[lcore].n_rx_queue;
1570 if (nb_rx_queue >= MAX_RX_QUEUE_PER_LCORE) {
1571 printf("error: too many queues (%u) for lcore: %u\n",
1572 (unsigned)nb_rx_queue + 1, (unsigned)lcore);
1575 lcore_conf[lcore].rx_queue_list[nb_rx_queue].port_id =
1576 lcore_params[i].port_id;
1577 lcore_conf[lcore].rx_queue_list[nb_rx_queue].queue_id =
1578 lcore_params[i].queue_id;
1579 lcore_conf[lcore].n_rx_queue++;
1587 print_usage(const char *prgname)
1589 printf ("%s [EAL options] -- -p PORTMASK -P"
1590 " [--config (port,queue,lcore)[,(port,queue,lcore]]"
1591 " [--high-perf-cores CORELIST"
1592 " [--perf-config (port,queue,hi_perf,lcore_index)[,(port,queue,hi_perf,lcore_index]]"
1593 " [--enable-jumbo [--max-pkt-len PKTLEN]]\n"
1594 " -p PORTMASK: hexadecimal bitmask of ports to configure\n"
1595 " -P : enable promiscuous mode\n"
1596 " --config (port,queue,lcore): rx queues configuration\n"
1597 " --high-perf-cores CORELIST: list of high performance cores\n"
1598 " --perf-config: similar as config, cores specified as indices"
1599 " for bins containing high or regular performance cores\n"
1600 " --no-numa: optional, disable numa awareness\n"
1601 " --enable-jumbo: enable jumbo frame"
1602 " which max packet len is PKTLEN in decimal (64-9600)\n"
1603 " --parse-ptype: parse packet type by software\n"
1604 " --legacy: use legacy interrupt-based scaling\n"
1605 " --empty-poll: enable empty poll detection"
1606 " follow (training_flag, high_threshold, med_threshold)\n"
1607 " --telemetry: enable telemetry mode, to update"
1608 " empty polls, full polls, and core busyness to telemetry\n"
1609 " --interrupt-only: enable interrupt-only mode\n",
1613 static int parse_max_pkt_len(const char *pktlen)
1618 /* parse decimal string */
1619 len = strtoul(pktlen, &end, 10);
1620 if ((pktlen[0] == '\0') || (end == NULL) || (*end != '\0'))
1630 parse_portmask(const char *portmask)
1635 /* parse hexadecimal string */
1636 pm = strtoul(portmask, &end, 16);
1637 if ((portmask[0] == '\0') || (end == NULL) || (*end != '\0'))
1647 parse_config(const char *q_arg)
1650 const char *p, *p0 = q_arg;
1658 unsigned long int_fld[_NUM_FLD];
1659 char *str_fld[_NUM_FLD];
1663 nb_lcore_params = 0;
1665 while ((p = strchr(p0,'(')) != NULL) {
1667 if((p0 = strchr(p,')')) == NULL)
1671 if(size >= sizeof(s))
1674 snprintf(s, sizeof(s), "%.*s", size, p);
1675 if (rte_strsplit(s, sizeof(s), str_fld, _NUM_FLD, ',') !=
1678 for (i = 0; i < _NUM_FLD; i++){
1680 int_fld[i] = strtoul(str_fld[i], &end, 0);
1681 if (errno != 0 || end == str_fld[i] || int_fld[i] >
1685 if (nb_lcore_params >= MAX_LCORE_PARAMS) {
1686 printf("exceeded max number of lcore params: %hu\n",
1690 lcore_params_array[nb_lcore_params].port_id =
1691 (uint8_t)int_fld[FLD_PORT];
1692 lcore_params_array[nb_lcore_params].queue_id =
1693 (uint8_t)int_fld[FLD_QUEUE];
1694 lcore_params_array[nb_lcore_params].lcore_id =
1695 (uint8_t)int_fld[FLD_LCORE];
1698 lcore_params = lcore_params_array;
1703 parse_ep_config(const char *q_arg)
1706 const char *p = q_arg;
1716 ep_med_edpi = EMPTY_POLL_MED_THRESHOLD;
1717 ep_hgh_edpi = EMPTY_POLL_MED_THRESHOLD;
1719 strlcpy(s, p, sizeof(s));
1721 num_arg = rte_strsplit(s, sizeof(s), str_fld, 3, ',');
1723 empty_poll_train = false;
1730 training_flag = strtoul(str_fld[0], &end, 0);
1731 med_edpi = strtoul(str_fld[1], &end, 0);
1732 hgh_edpi = strtoul(str_fld[2], &end, 0);
1734 if (training_flag == 1)
1735 empty_poll_train = true;
1738 ep_med_edpi = med_edpi;
1741 ep_hgh_edpi = hgh_edpi;
1751 #define CMD_LINE_OPT_PARSE_PTYPE "parse-ptype"
1752 #define CMD_LINE_OPT_LEGACY "legacy"
1753 #define CMD_LINE_OPT_EMPTY_POLL "empty-poll"
1754 #define CMD_LINE_OPT_INTERRUPT_ONLY "interrupt-only"
1755 #define CMD_LINE_OPT_TELEMETRY "telemetry"
1757 /* Parse the argument given in the command line of the application */
1759 parse_args(int argc, char **argv)
1765 char *prgname = argv[0];
1766 static struct option lgopts[] = {
1767 {"config", 1, 0, 0},
1768 {"perf-config", 1, 0, 0},
1769 {"high-perf-cores", 1, 0, 0},
1770 {"no-numa", 0, 0, 0},
1771 {"enable-jumbo", 0, 0, 0},
1772 {CMD_LINE_OPT_EMPTY_POLL, 1, 0, 0},
1773 {CMD_LINE_OPT_PARSE_PTYPE, 0, 0, 0},
1774 {CMD_LINE_OPT_LEGACY, 0, 0, 0},
1775 {CMD_LINE_OPT_TELEMETRY, 0, 0, 0},
1776 {CMD_LINE_OPT_INTERRUPT_ONLY, 0, 0, 0},
1782 while ((opt = getopt_long(argc, argvopt, "p:l:m:h:P",
1783 lgopts, &option_index)) != EOF) {
1788 enabled_port_mask = parse_portmask(optarg);
1789 if (enabled_port_mask == 0) {
1790 printf("invalid portmask\n");
1791 print_usage(prgname);
1796 printf("Promiscuous mode selected\n");
1800 limit = parse_max_pkt_len(optarg);
1801 freq_tlb[LOW] = limit;
1804 limit = parse_max_pkt_len(optarg);
1805 freq_tlb[MED] = limit;
1808 limit = parse_max_pkt_len(optarg);
1809 freq_tlb[HGH] = limit;
1813 if (!strncmp(lgopts[option_index].name, "config", 6)) {
1814 ret = parse_config(optarg);
1816 printf("invalid config\n");
1817 print_usage(prgname);
1822 if (!strncmp(lgopts[option_index].name,
1823 "perf-config", 11)) {
1824 ret = parse_perf_config(optarg);
1826 printf("invalid perf-config\n");
1827 print_usage(prgname);
1832 if (!strncmp(lgopts[option_index].name,
1833 "high-perf-cores", 15)) {
1834 ret = parse_perf_core_list(optarg);
1836 printf("invalid high-perf-cores\n");
1837 print_usage(prgname);
1842 if (!strncmp(lgopts[option_index].name,
1844 printf("numa is disabled \n");
1848 if (!strncmp(lgopts[option_index].name,
1849 CMD_LINE_OPT_LEGACY,
1850 sizeof(CMD_LINE_OPT_LEGACY))) {
1851 if (app_mode != APP_MODE_DEFAULT) {
1852 printf(" legacy mode is mutually exclusive with other modes\n");
1855 app_mode = APP_MODE_LEGACY;
1856 printf("legacy mode is enabled\n");
1859 if (!strncmp(lgopts[option_index].name,
1860 CMD_LINE_OPT_EMPTY_POLL, 10)) {
1861 if (app_mode != APP_MODE_DEFAULT) {
1862 printf(" empty-poll mode is mutually exclusive with other modes\n");
1865 app_mode = APP_MODE_EMPTY_POLL;
1866 ret = parse_ep_config(optarg);
1869 printf("invalid empty poll config\n");
1870 print_usage(prgname);
1873 printf("empty-poll is enabled\n");
1876 if (!strncmp(lgopts[option_index].name,
1877 CMD_LINE_OPT_TELEMETRY,
1878 sizeof(CMD_LINE_OPT_TELEMETRY))) {
1879 if (app_mode != APP_MODE_DEFAULT) {
1880 printf(" telemetry mode is mutually exclusive with other modes\n");
1883 app_mode = APP_MODE_TELEMETRY;
1884 printf("telemetry mode is enabled\n");
1887 if (!strncmp(lgopts[option_index].name,
1888 CMD_LINE_OPT_INTERRUPT_ONLY,
1889 sizeof(CMD_LINE_OPT_INTERRUPT_ONLY))) {
1890 if (app_mode != APP_MODE_DEFAULT) {
1891 printf(" interrupt-only mode is mutually exclusive with other modes\n");
1894 app_mode = APP_MODE_INTERRUPT;
1895 printf("interrupt-only mode is enabled\n");
1898 if (!strncmp(lgopts[option_index].name,
1899 "enable-jumbo", 12)) {
1900 struct option lenopts =
1901 {"max-pkt-len", required_argument, \
1904 printf("jumbo frame is enabled \n");
1905 port_conf.rxmode.offloads |=
1906 DEV_RX_OFFLOAD_JUMBO_FRAME;
1907 port_conf.txmode.offloads |=
1908 DEV_TX_OFFLOAD_MULTI_SEGS;
1911 * if no max-pkt-len set, use the default value
1914 if (0 == getopt_long(argc, argvopt, "",
1915 &lenopts, &option_index)) {
1916 ret = parse_max_pkt_len(optarg);
1918 (ret > MAX_JUMBO_PKT_LEN)){
1919 printf("invalid packet "
1921 print_usage(prgname);
1924 port_conf.rxmode.max_rx_pkt_len = ret;
1926 printf("set jumbo frame "
1927 "max packet length to %u\n",
1928 (unsigned int)port_conf.rxmode.max_rx_pkt_len);
1931 if (!strncmp(lgopts[option_index].name,
1932 CMD_LINE_OPT_PARSE_PTYPE,
1933 sizeof(CMD_LINE_OPT_PARSE_PTYPE))) {
1934 printf("soft parse-ptype is enabled\n");
1941 print_usage(prgname);
1947 argv[optind-1] = prgname;
1950 optind = 1; /* reset getopt lib */
1955 print_ethaddr(const char *name, const struct rte_ether_addr *eth_addr)
1957 char buf[RTE_ETHER_ADDR_FMT_SIZE];
1958 rte_ether_format_addr(buf, RTE_ETHER_ADDR_FMT_SIZE, eth_addr);
1959 printf("%s%s", name, buf);
1962 #if (APP_LOOKUP_METHOD == APP_LOOKUP_EXACT_MATCH)
1964 setup_hash(int socketid)
1966 struct rte_hash_parameters ipv4_l3fwd_hash_params = {
1968 .entries = L3FWD_HASH_ENTRIES,
1969 .key_len = sizeof(struct ipv4_5tuple),
1970 .hash_func = DEFAULT_HASH_FUNC,
1971 .hash_func_init_val = 0,
1974 struct rte_hash_parameters ipv6_l3fwd_hash_params = {
1976 .entries = L3FWD_HASH_ENTRIES,
1977 .key_len = sizeof(struct ipv6_5tuple),
1978 .hash_func = DEFAULT_HASH_FUNC,
1979 .hash_func_init_val = 0,
1986 /* create ipv4 hash */
1987 snprintf(s, sizeof(s), "ipv4_l3fwd_hash_%d", socketid);
1988 ipv4_l3fwd_hash_params.name = s;
1989 ipv4_l3fwd_hash_params.socket_id = socketid;
1990 ipv4_l3fwd_lookup_struct[socketid] =
1991 rte_hash_create(&ipv4_l3fwd_hash_params);
1992 if (ipv4_l3fwd_lookup_struct[socketid] == NULL)
1993 rte_exit(EXIT_FAILURE, "Unable to create the l3fwd hash on "
1994 "socket %d\n", socketid);
1996 /* create ipv6 hash */
1997 snprintf(s, sizeof(s), "ipv6_l3fwd_hash_%d", socketid);
1998 ipv6_l3fwd_hash_params.name = s;
1999 ipv6_l3fwd_hash_params.socket_id = socketid;
2000 ipv6_l3fwd_lookup_struct[socketid] =
2001 rte_hash_create(&ipv6_l3fwd_hash_params);
2002 if (ipv6_l3fwd_lookup_struct[socketid] == NULL)
2003 rte_exit(EXIT_FAILURE, "Unable to create the l3fwd hash on "
2004 "socket %d\n", socketid);
2007 /* populate the ipv4 hash */
2008 for (i = 0; i < RTE_DIM(ipv4_l3fwd_route_array); i++) {
2009 ret = rte_hash_add_key (ipv4_l3fwd_lookup_struct[socketid],
2010 (void *) &ipv4_l3fwd_route_array[i].key);
2012 rte_exit(EXIT_FAILURE, "Unable to add entry %u to the"
2013 "l3fwd hash on socket %d\n", i, socketid);
2015 ipv4_l3fwd_out_if[ret] = ipv4_l3fwd_route_array[i].if_out;
2016 printf("Hash: Adding key\n");
2017 print_ipv4_key(ipv4_l3fwd_route_array[i].key);
2020 /* populate the ipv6 hash */
2021 for (i = 0; i < RTE_DIM(ipv6_l3fwd_route_array); i++) {
2022 ret = rte_hash_add_key (ipv6_l3fwd_lookup_struct[socketid],
2023 (void *) &ipv6_l3fwd_route_array[i].key);
2025 rte_exit(EXIT_FAILURE, "Unable to add entry %u to the"
2026 "l3fwd hash on socket %d\n", i, socketid);
2028 ipv6_l3fwd_out_if[ret] = ipv6_l3fwd_route_array[i].if_out;
2029 printf("Hash: Adding key\n");
2030 print_ipv6_key(ipv6_l3fwd_route_array[i].key);
2035 #if (APP_LOOKUP_METHOD == APP_LOOKUP_LPM)
2037 setup_lpm(int socketid)
2043 /* create the LPM table */
2044 struct rte_lpm_config lpm_ipv4_config;
2046 lpm_ipv4_config.max_rules = IPV4_L3FWD_LPM_MAX_RULES;
2047 lpm_ipv4_config.number_tbl8s = 256;
2048 lpm_ipv4_config.flags = 0;
2050 snprintf(s, sizeof(s), "IPV4_L3FWD_LPM_%d", socketid);
2051 ipv4_l3fwd_lookup_struct[socketid] =
2052 rte_lpm_create(s, socketid, &lpm_ipv4_config);
2053 if (ipv4_l3fwd_lookup_struct[socketid] == NULL)
2054 rte_exit(EXIT_FAILURE, "Unable to create the l3fwd LPM table"
2055 " on socket %d\n", socketid);
2057 /* populate the LPM table */
2058 for (i = 0; i < RTE_DIM(ipv4_l3fwd_route_array); i++) {
2059 ret = rte_lpm_add(ipv4_l3fwd_lookup_struct[socketid],
2060 ipv4_l3fwd_route_array[i].ip,
2061 ipv4_l3fwd_route_array[i].depth,
2062 ipv4_l3fwd_route_array[i].if_out);
2065 rte_exit(EXIT_FAILURE, "Unable to add entry %u to the "
2066 "l3fwd LPM table on socket %d\n",
2070 printf("LPM: Adding route 0x%08x / %d (%d)\n",
2071 (unsigned)ipv4_l3fwd_route_array[i].ip,
2072 ipv4_l3fwd_route_array[i].depth,
2073 ipv4_l3fwd_route_array[i].if_out);
2079 init_mem(unsigned nb_mbuf)
2081 struct lcore_conf *qconf;
2086 for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
2087 if (rte_lcore_is_enabled(lcore_id) == 0)
2091 socketid = rte_lcore_to_socket_id(lcore_id);
2095 if (socketid >= NB_SOCKETS) {
2096 rte_exit(EXIT_FAILURE, "Socket %d of lcore %u is "
2097 "out of range %d\n", socketid,
2098 lcore_id, NB_SOCKETS);
2100 if (pktmbuf_pool[socketid] == NULL) {
2101 snprintf(s, sizeof(s), "mbuf_pool_%d", socketid);
2102 pktmbuf_pool[socketid] =
2103 rte_pktmbuf_pool_create(s, nb_mbuf,
2104 MEMPOOL_CACHE_SIZE, 0,
2105 RTE_MBUF_DEFAULT_BUF_SIZE,
2107 if (pktmbuf_pool[socketid] == NULL)
2108 rte_exit(EXIT_FAILURE,
2109 "Cannot init mbuf pool on socket %d\n",
2112 printf("Allocated mbuf pool on socket %d\n",
2115 #if (APP_LOOKUP_METHOD == APP_LOOKUP_LPM)
2116 setup_lpm(socketid);
2118 setup_hash(socketid);
2121 qconf = &lcore_conf[lcore_id];
2122 qconf->ipv4_lookup_struct = ipv4_l3fwd_lookup_struct[socketid];
2123 #if (APP_LOOKUP_METHOD == APP_LOOKUP_EXACT_MATCH)
2124 qconf->ipv6_lookup_struct = ipv6_l3fwd_lookup_struct[socketid];
2130 /* Check the link status of all ports in up to 9s, and print them finally */
2132 check_all_ports_link_status(uint32_t port_mask)
2134 #define CHECK_INTERVAL 100 /* 100ms */
2135 #define MAX_CHECK_TIME 90 /* 9s (90 * 100ms) in total */
2136 uint8_t count, all_ports_up, print_flag = 0;
2138 struct rte_eth_link link;
2141 printf("\nChecking link status");
2143 for (count = 0; count <= MAX_CHECK_TIME; count++) {
2145 RTE_ETH_FOREACH_DEV(portid) {
2146 if ((port_mask & (1 << portid)) == 0)
2148 memset(&link, 0, sizeof(link));
2149 ret = rte_eth_link_get_nowait(portid, &link);
2152 if (print_flag == 1)
2153 printf("Port %u link get failed: %s\n",
2154 portid, rte_strerror(-ret));
2157 /* print link status if flag set */
2158 if (print_flag == 1) {
2159 if (link.link_status)
2160 printf("Port %d Link Up - speed %u "
2161 "Mbps - %s\n", (uint8_t)portid,
2162 (unsigned)link.link_speed,
2163 (link.link_duplex == ETH_LINK_FULL_DUPLEX) ?
2164 ("full-duplex") : ("half-duplex"));
2166 printf("Port %d Link Down\n",
2170 /* clear all_ports_up flag if any link down */
2171 if (link.link_status == ETH_LINK_DOWN) {
2176 /* after finally printing all link status, get out */
2177 if (print_flag == 1)
2180 if (all_ports_up == 0) {
2183 rte_delay_ms(CHECK_INTERVAL);
2186 /* set the print_flag if all ports up or timeout */
2187 if (all_ports_up == 1 || count == (MAX_CHECK_TIME - 1)) {
2194 static int check_ptype(uint16_t portid)
2197 int ptype_l3_ipv4 = 0;
2198 #if (APP_LOOKUP_METHOD == APP_LOOKUP_EXACT_MATCH)
2199 int ptype_l3_ipv6 = 0;
2201 uint32_t ptype_mask = RTE_PTYPE_L3_MASK;
2203 ret = rte_eth_dev_get_supported_ptypes(portid, ptype_mask, NULL, 0);
2207 uint32_t ptypes[ret];
2209 ret = rte_eth_dev_get_supported_ptypes(portid, ptype_mask, ptypes, ret);
2210 for (i = 0; i < ret; ++i) {
2211 if (ptypes[i] & RTE_PTYPE_L3_IPV4)
2213 #if (APP_LOOKUP_METHOD == APP_LOOKUP_EXACT_MATCH)
2214 if (ptypes[i] & RTE_PTYPE_L3_IPV6)
2219 if (ptype_l3_ipv4 == 0)
2220 printf("port %d cannot parse RTE_PTYPE_L3_IPV4\n", portid);
2222 #if (APP_LOOKUP_METHOD == APP_LOOKUP_EXACT_MATCH)
2223 if (ptype_l3_ipv6 == 0)
2224 printf("port %d cannot parse RTE_PTYPE_L3_IPV6\n", portid);
2227 #if (APP_LOOKUP_METHOD == APP_LOOKUP_LPM)
2229 #else /* APP_LOOKUP_EXACT_MATCH */
2230 if (ptype_l3_ipv4 && ptype_l3_ipv6)
2239 init_power_library(void)
2241 enum power_management_env env;
2242 unsigned int lcore_id;
2245 RTE_LCORE_FOREACH(lcore_id) {
2246 /* init power management library */
2247 ret = rte_power_init(lcore_id);
2250 "Library initialization failed on core %u\n",
2254 /* we're not supporting the VM channel mode */
2255 env = rte_power_get_env();
2256 if (env != PM_ENV_ACPI_CPUFREQ &&
2257 env != PM_ENV_PSTATE_CPUFREQ) {
2259 "Only ACPI and PSTATE mode are supported\n");
2267 deinit_power_library(void)
2269 unsigned int lcore_id;
2272 RTE_LCORE_FOREACH(lcore_id) {
2273 /* deinit power management library */
2274 ret = rte_power_exit(lcore_id);
2277 "Library deinitialization failed on core %u\n",
2286 get_current_stat_values(uint64_t *values)
2288 unsigned int lcore_id = rte_lcore_id();
2289 struct lcore_conf *qconf;
2290 uint64_t app_eps = 0, app_fps = 0, app_br = 0;
2293 RTE_LCORE_FOREACH_SLAVE(lcore_id) {
2294 qconf = &lcore_conf[lcore_id];
2295 if (qconf->n_rx_queue == 0)
2298 rte_spinlock_lock(&stats[lcore_id].telemetry_lock);
2299 app_eps += stats[lcore_id].ep_nep[1];
2300 app_fps += stats[lcore_id].fp_nfp[1];
2301 app_br += stats[lcore_id].br;
2302 rte_spinlock_unlock(&stats[lcore_id].telemetry_lock);
2306 values[0] = app_eps/count;
2307 values[1] = app_fps/count;
2308 values[2] = app_br/count;
2310 memset(values, 0, sizeof(uint64_t) * NUM_TELSTATS);
2315 update_telemetry(__rte_unused struct rte_timer *tim,
2316 __rte_unused void *arg)
2319 uint64_t values[NUM_TELSTATS] = {0};
2321 get_current_stat_values(values);
2322 ret = rte_metrics_update_values(RTE_METRICS_GLOBAL, telstats_index,
2323 values, RTE_DIM(values));
2325 RTE_LOG(WARNING, POWER, "failed to update metrcis\n");
2329 handle_app_stats(const char *cmd __rte_unused,
2330 const char *params __rte_unused,
2331 struct rte_tel_data *d)
2333 uint64_t values[NUM_TELSTATS] = {0};
2336 rte_tel_data_start_dict(d);
2337 get_current_stat_values(values);
2338 for (i = 0; i < NUM_TELSTATS; i++)
2339 rte_tel_data_add_dict_u64(d, telstats_strings[i].name,
2345 telemetry_setup_timer(void)
2347 int lcore_id = rte_lcore_id();
2348 uint64_t hz = rte_get_timer_hz();
2351 ticks = hz / TELEMETRY_INTERVALS_PER_SEC;
2352 rte_timer_reset_sync(&telemetry_timer,
2360 empty_poll_setup_timer(void)
2362 int lcore_id = rte_lcore_id();
2363 uint64_t hz = rte_get_timer_hz();
2365 struct ep_params *ep_ptr = ep_params;
2367 ep_ptr->interval_ticks = hz / INTERVALS_PER_SECOND;
2369 rte_timer_reset_sync(&ep_ptr->timer0,
2370 ep_ptr->interval_ticks,
2373 rte_empty_poll_detection,
2378 launch_timer(unsigned int lcore_id)
2380 int64_t prev_tsc = 0, cur_tsc, diff_tsc, cycles_10ms;
2382 RTE_SET_USED(lcore_id);
2385 if (rte_get_master_lcore() != lcore_id) {
2386 rte_panic("timer on lcore:%d which is not master core:%d\n",
2388 rte_get_master_lcore());
2391 RTE_LOG(INFO, POWER, "Bring up the Timer\n");
2393 if (app_mode == APP_MODE_EMPTY_POLL)
2394 empty_poll_setup_timer();
2396 telemetry_setup_timer();
2398 cycles_10ms = rte_get_timer_hz() / 100;
2400 while (!is_done()) {
2401 cur_tsc = rte_rdtsc();
2402 diff_tsc = cur_tsc - prev_tsc;
2403 if (diff_tsc > cycles_10ms) {
2406 cycles_10ms = rte_get_timer_hz() / 100;
2410 RTE_LOG(INFO, POWER, "Timer_subsystem is done\n");
2416 autodetect_mode(void)
2418 RTE_LOG(NOTICE, L3FWD_POWER, "Operating mode not specified, probing frequency scaling support...\n");
2421 * Empty poll and telemetry modes have to be specifically requested to
2422 * be enabled, but we can auto-detect between interrupt mode with or
2423 * without frequency scaling. Both ACPI and pstate can be used.
2425 if (rte_power_check_env_supported(PM_ENV_ACPI_CPUFREQ))
2426 return APP_MODE_LEGACY;
2427 if (rte_power_check_env_supported(PM_ENV_PSTATE_CPUFREQ))
2428 return APP_MODE_LEGACY;
2430 RTE_LOG(NOTICE, L3FWD_POWER, "Frequency scaling not supported, selecting interrupt-only mode\n");
2432 return APP_MODE_INTERRUPT;
2436 mode_to_str(enum appmode mode)
2439 case APP_MODE_LEGACY:
2441 case APP_MODE_EMPTY_POLL:
2442 return "empty poll";
2443 case APP_MODE_TELEMETRY:
2445 case APP_MODE_INTERRUPT:
2446 return "interrupt-only";
2453 main(int argc, char **argv)
2455 struct lcore_conf *qconf;
2456 struct rte_eth_dev_info dev_info;
2457 struct rte_eth_txconf *txconf;
2463 uint32_t n_tx_queue, nb_lcores;
2464 uint32_t dev_rxq_num, dev_txq_num;
2465 uint8_t nb_rx_queue, queue, socketid;
2467 const char *ptr_strings[NUM_TELSTATS];
2469 /* catch SIGINT and restore cpufreq governor to ondemand */
2470 signal(SIGINT, signal_exit_now);
2473 ret = rte_eal_init(argc, argv);
2475 rte_exit(EXIT_FAILURE, "Invalid EAL parameters\n");
2479 /* init RTE timer library to be used late */
2480 rte_timer_subsystem_init();
2482 /* parse application arguments (after the EAL ones) */
2483 ret = parse_args(argc, argv);
2485 rte_exit(EXIT_FAILURE, "Invalid L3FWD parameters\n");
2487 if (app_mode == APP_MODE_DEFAULT)
2488 app_mode = autodetect_mode();
2490 RTE_LOG(INFO, L3FWD_POWER, "Selected operation mode: %s\n",
2491 mode_to_str(app_mode));
2493 /* only legacy and empty poll mode rely on power library */
2494 if ((app_mode == APP_MODE_LEGACY || app_mode == APP_MODE_EMPTY_POLL) &&
2495 init_power_library())
2496 rte_exit(EXIT_FAILURE, "init_power_library failed\n");
2498 if (update_lcore_params() < 0)
2499 rte_exit(EXIT_FAILURE, "update_lcore_params failed\n");
2501 if (check_lcore_params() < 0)
2502 rte_exit(EXIT_FAILURE, "check_lcore_params failed\n");
2504 ret = init_lcore_rx_queues();
2506 rte_exit(EXIT_FAILURE, "init_lcore_rx_queues failed\n");
2508 nb_ports = rte_eth_dev_count_avail();
2510 if (check_port_config() < 0)
2511 rte_exit(EXIT_FAILURE, "check_port_config failed\n");
2513 nb_lcores = rte_lcore_count();
2515 /* initialize all ports */
2516 RTE_ETH_FOREACH_DEV(portid) {
2517 struct rte_eth_conf local_port_conf = port_conf;
2518 /* not all app modes need interrupts */
2519 bool need_intr = app_mode == APP_MODE_LEGACY ||
2520 app_mode == APP_MODE_INTERRUPT;
2522 /* skip ports that are not enabled */
2523 if ((enabled_port_mask & (1 << portid)) == 0) {
2524 printf("\nSkipping disabled port %d\n", portid);
2529 printf("Initializing port %d ... ", portid );
2532 ret = rte_eth_dev_info_get(portid, &dev_info);
2534 rte_exit(EXIT_FAILURE,
2535 "Error during getting device (port %u) info: %s\n",
2536 portid, strerror(-ret));
2538 dev_rxq_num = dev_info.max_rx_queues;
2539 dev_txq_num = dev_info.max_tx_queues;
2541 nb_rx_queue = get_port_n_rx_queues(portid);
2542 if (nb_rx_queue > dev_rxq_num)
2543 rte_exit(EXIT_FAILURE,
2544 "Cannot configure not existed rxq: "
2545 "port=%d\n", portid);
2547 n_tx_queue = nb_lcores;
2548 if (n_tx_queue > dev_txq_num)
2549 n_tx_queue = dev_txq_num;
2550 printf("Creating queues: nb_rxq=%d nb_txq=%u... ",
2551 nb_rx_queue, (unsigned)n_tx_queue );
2552 /* If number of Rx queue is 0, no need to enable Rx interrupt */
2553 if (nb_rx_queue == 0)
2557 local_port_conf.intr_conf.rxq = 1;
2559 ret = rte_eth_dev_info_get(portid, &dev_info);
2561 rte_exit(EXIT_FAILURE,
2562 "Error during getting device (port %u) info: %s\n",
2563 portid, strerror(-ret));
2565 if (dev_info.tx_offload_capa & DEV_TX_OFFLOAD_MBUF_FAST_FREE)
2566 local_port_conf.txmode.offloads |=
2567 DEV_TX_OFFLOAD_MBUF_FAST_FREE;
2569 local_port_conf.rx_adv_conf.rss_conf.rss_hf &=
2570 dev_info.flow_type_rss_offloads;
2571 if (local_port_conf.rx_adv_conf.rss_conf.rss_hf !=
2572 port_conf.rx_adv_conf.rss_conf.rss_hf) {
2573 printf("Port %u modified RSS hash function based on hardware support,"
2574 "requested:%#"PRIx64" configured:%#"PRIx64"\n",
2576 port_conf.rx_adv_conf.rss_conf.rss_hf,
2577 local_port_conf.rx_adv_conf.rss_conf.rss_hf);
2580 ret = rte_eth_dev_configure(portid, nb_rx_queue,
2581 (uint16_t)n_tx_queue, &local_port_conf);
2583 rte_exit(EXIT_FAILURE, "Cannot configure device: "
2584 "err=%d, port=%d\n", ret, portid);
2586 ret = rte_eth_dev_adjust_nb_rx_tx_desc(portid, &nb_rxd,
2589 rte_exit(EXIT_FAILURE,
2590 "Cannot adjust number of descriptors: err=%d, port=%d\n",
2593 ret = rte_eth_macaddr_get(portid, &ports_eth_addr[portid]);
2595 rte_exit(EXIT_FAILURE,
2596 "Cannot get MAC address: err=%d, port=%d\n",
2599 print_ethaddr(" Address:", &ports_eth_addr[portid]);
2603 ret = init_mem(NB_MBUF);
2605 rte_exit(EXIT_FAILURE, "init_mem failed\n");
2607 for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
2608 if (rte_lcore_is_enabled(lcore_id) == 0)
2611 /* Initialize TX buffers */
2612 qconf = &lcore_conf[lcore_id];
2613 qconf->tx_buffer[portid] = rte_zmalloc_socket("tx_buffer",
2614 RTE_ETH_TX_BUFFER_SIZE(MAX_PKT_BURST), 0,
2615 rte_eth_dev_socket_id(portid));
2616 if (qconf->tx_buffer[portid] == NULL)
2617 rte_exit(EXIT_FAILURE, "Can't allocate tx buffer for port %u\n",
2620 rte_eth_tx_buffer_init(qconf->tx_buffer[portid], MAX_PKT_BURST);
2623 /* init one TX queue per couple (lcore,port) */
2625 for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
2626 if (rte_lcore_is_enabled(lcore_id) == 0)
2629 if (queueid >= dev_txq_num)
2634 (uint8_t)rte_lcore_to_socket_id(lcore_id);
2638 printf("txq=%u,%d,%d ", lcore_id, queueid, socketid);
2641 txconf = &dev_info.default_txconf;
2642 txconf->offloads = local_port_conf.txmode.offloads;
2643 ret = rte_eth_tx_queue_setup(portid, queueid, nb_txd,
2646 rte_exit(EXIT_FAILURE,
2647 "rte_eth_tx_queue_setup: err=%d, "
2648 "port=%d\n", ret, portid);
2650 qconf = &lcore_conf[lcore_id];
2651 qconf->tx_queue_id[portid] = queueid;
2654 qconf->tx_port_id[qconf->n_tx_port] = portid;
2660 for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
2661 if (rte_lcore_is_enabled(lcore_id) == 0)
2664 if (app_mode == APP_MODE_LEGACY) {
2665 /* init timer structures for each enabled lcore */
2666 rte_timer_init(&power_timers[lcore_id]);
2667 hz = rte_get_timer_hz();
2668 rte_timer_reset(&power_timers[lcore_id],
2669 hz/TIMER_NUMBER_PER_SECOND,
2671 power_timer_cb, NULL);
2673 qconf = &lcore_conf[lcore_id];
2674 printf("\nInitializing rx queues on lcore %u ... ", lcore_id );
2676 /* init RX queues */
2677 for(queue = 0; queue < qconf->n_rx_queue; ++queue) {
2678 struct rte_eth_rxconf rxq_conf;
2680 portid = qconf->rx_queue_list[queue].port_id;
2681 queueid = qconf->rx_queue_list[queue].queue_id;
2685 (uint8_t)rte_lcore_to_socket_id(lcore_id);
2689 printf("rxq=%d,%d,%d ", portid, queueid, socketid);
2692 ret = rte_eth_dev_info_get(portid, &dev_info);
2694 rte_exit(EXIT_FAILURE,
2695 "Error during getting device (port %u) info: %s\n",
2696 portid, strerror(-ret));
2698 rxq_conf = dev_info.default_rxconf;
2699 rxq_conf.offloads = port_conf.rxmode.offloads;
2700 ret = rte_eth_rx_queue_setup(portid, queueid, nb_rxd,
2701 socketid, &rxq_conf,
2702 pktmbuf_pool[socketid]);
2704 rte_exit(EXIT_FAILURE,
2705 "rte_eth_rx_queue_setup: err=%d, "
2706 "port=%d\n", ret, portid);
2709 if (add_cb_parse_ptype(portid, queueid) < 0)
2710 rte_exit(EXIT_FAILURE,
2711 "Fail to add ptype cb\n");
2712 } else if (!check_ptype(portid))
2713 rte_exit(EXIT_FAILURE,
2714 "PMD can not provide needed ptypes\n");
2721 RTE_ETH_FOREACH_DEV(portid) {
2722 if ((enabled_port_mask & (1 << portid)) == 0) {
2726 ret = rte_eth_dev_start(portid);
2728 rte_exit(EXIT_FAILURE, "rte_eth_dev_start: err=%d, "
2729 "port=%d\n", ret, portid);
2731 * If enabled, put device in promiscuous mode.
2732 * This allows IO forwarding mode to forward packets
2733 * to itself through 2 cross-connected ports of the
2736 if (promiscuous_on) {
2737 ret = rte_eth_promiscuous_enable(portid);
2739 rte_exit(EXIT_FAILURE,
2740 "rte_eth_promiscuous_enable: err=%s, port=%u\n",
2741 rte_strerror(-ret), portid);
2743 /* initialize spinlock for each port */
2744 rte_spinlock_init(&(locks[portid]));
2747 check_all_ports_link_status(enabled_port_mask);
2749 if (app_mode == APP_MODE_EMPTY_POLL) {
2751 if (empty_poll_train) {
2752 policy.state = TRAINING;
2754 policy.state = MED_NORMAL;
2755 policy.med_base_edpi = ep_med_edpi;
2756 policy.hgh_base_edpi = ep_hgh_edpi;
2759 ret = rte_power_empty_poll_stat_init(&ep_params,
2763 rte_exit(EXIT_FAILURE, "empty poll init failed");
2767 /* launch per-lcore init on every lcore */
2768 if (app_mode == APP_MODE_LEGACY) {
2769 rte_eal_mp_remote_launch(main_legacy_loop, NULL, CALL_MASTER);
2770 } else if (app_mode == APP_MODE_EMPTY_POLL) {
2771 empty_poll_stop = false;
2772 rte_eal_mp_remote_launch(main_empty_poll_loop, NULL,
2774 } else if (app_mode == APP_MODE_TELEMETRY) {
2777 /* Init metrics library */
2778 rte_metrics_init(rte_socket_id());
2779 /** Register stats with metrics library */
2780 for (i = 0; i < NUM_TELSTATS; i++)
2781 ptr_strings[i] = telstats_strings[i].name;
2783 ret = rte_metrics_reg_names(ptr_strings, NUM_TELSTATS);
2785 telstats_index = ret;
2787 rte_exit(EXIT_FAILURE, "failed to register metrics names");
2789 RTE_LCORE_FOREACH_SLAVE(lcore_id) {
2790 rte_spinlock_init(&stats[lcore_id].telemetry_lock);
2792 rte_timer_init(&telemetry_timer);
2793 rte_telemetry_register_cmd("/l3fwd-power/stats",
2795 "Returns global power stats. Parameters: None");
2796 rte_eal_mp_remote_launch(main_telemetry_loop, NULL,
2798 } else if (app_mode == APP_MODE_INTERRUPT) {
2799 rte_eal_mp_remote_launch(main_intr_loop, NULL, CALL_MASTER);
2802 if (app_mode == APP_MODE_EMPTY_POLL || app_mode == APP_MODE_TELEMETRY)
2803 launch_timer(rte_lcore_id());
2805 RTE_LCORE_FOREACH_SLAVE(lcore_id) {
2806 if (rte_eal_wait_lcore(lcore_id) < 0)
2810 RTE_ETH_FOREACH_DEV(portid)
2812 if ((enabled_port_mask & (1 << portid)) == 0)
2815 rte_eth_dev_stop(portid);
2816 rte_eth_dev_close(portid);
2819 if (app_mode == APP_MODE_EMPTY_POLL)
2820 rte_power_empty_poll_stat_free();
2822 if ((app_mode == APP_MODE_LEGACY || app_mode == APP_MODE_EMPTY_POLL) &&
2823 deinit_power_library())
2824 rte_exit(EXIT_FAILURE, "deinit_power_library failed\n");
2826 if (rte_eal_cleanup() < 0)
2827 RTE_LOG(ERR, L3FWD_POWER, "EAL cleanup failed\n");