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 */
203 enum appmode app_mode;
205 enum freq_scale_hint_t
213 struct lcore_rx_queue {
216 enum freq_scale_hint_t freq_up_hint;
217 uint32_t zero_rx_packet_count;
219 } __rte_cache_aligned;
221 #define MAX_RX_QUEUE_PER_LCORE 16
222 #define MAX_TX_QUEUE_PER_PORT RTE_MAX_ETHPORTS
223 #define MAX_RX_QUEUE_PER_PORT 128
225 #define MAX_RX_QUEUE_INTERRUPT_PER_PORT 16
228 struct lcore_params lcore_params_array[MAX_LCORE_PARAMS];
229 static struct lcore_params lcore_params_array_default[] = {
241 struct lcore_params *lcore_params = lcore_params_array_default;
242 uint16_t nb_lcore_params = RTE_DIM(lcore_params_array_default);
244 static struct rte_eth_conf port_conf = {
246 .mq_mode = ETH_MQ_RX_RSS,
247 .max_rx_pkt_len = RTE_ETHER_MAX_LEN,
249 .offloads = DEV_RX_OFFLOAD_CHECKSUM,
254 .rss_hf = ETH_RSS_UDP,
258 .mq_mode = ETH_MQ_TX_NONE,
262 static struct rte_mempool * pktmbuf_pool[NB_SOCKETS];
265 #if (APP_LOOKUP_METHOD == APP_LOOKUP_EXACT_MATCH)
268 #include <rte_hash_crc.h>
269 #define DEFAULT_HASH_FUNC rte_hash_crc
271 #include <rte_jhash.h>
272 #define DEFAULT_HASH_FUNC rte_jhash
284 uint8_t ip_dst[IPV6_ADDR_LEN];
285 uint8_t ip_src[IPV6_ADDR_LEN];
291 struct ipv4_l3fwd_route {
292 struct ipv4_5tuple key;
296 struct ipv6_l3fwd_route {
297 struct ipv6_5tuple key;
301 static struct ipv4_l3fwd_route ipv4_l3fwd_route_array[] = {
302 {{RTE_IPV4(100,10,0,1), RTE_IPV4(200,10,0,1), 101, 11, IPPROTO_TCP}, 0},
303 {{RTE_IPV4(100,20,0,2), RTE_IPV4(200,20,0,2), 102, 12, IPPROTO_TCP}, 1},
304 {{RTE_IPV4(100,30,0,3), RTE_IPV4(200,30,0,3), 103, 13, IPPROTO_TCP}, 2},
305 {{RTE_IPV4(100,40,0,4), RTE_IPV4(200,40,0,4), 104, 14, IPPROTO_TCP}, 3},
308 static struct ipv6_l3fwd_route ipv6_l3fwd_route_array[] = {
311 {0xfe, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
312 0x02, 0x1b, 0x21, 0xff, 0xfe, 0x91, 0x38, 0x05},
313 {0xfe, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
314 0x02, 0x1e, 0x67, 0xff, 0xfe, 0x0d, 0xb6, 0x0a},
320 typedef struct rte_hash lookup_struct_t;
321 static lookup_struct_t *ipv4_l3fwd_lookup_struct[NB_SOCKETS];
322 static lookup_struct_t *ipv6_l3fwd_lookup_struct[NB_SOCKETS];
324 #define L3FWD_HASH_ENTRIES 1024
326 static uint16_t ipv4_l3fwd_out_if[L3FWD_HASH_ENTRIES] __rte_cache_aligned;
327 static uint16_t ipv6_l3fwd_out_if[L3FWD_HASH_ENTRIES] __rte_cache_aligned;
330 #if (APP_LOOKUP_METHOD == APP_LOOKUP_LPM)
331 struct ipv4_l3fwd_route {
337 static struct ipv4_l3fwd_route ipv4_l3fwd_route_array[] = {
338 {RTE_IPV4(1,1,1,0), 24, 0},
339 {RTE_IPV4(2,1,1,0), 24, 1},
340 {RTE_IPV4(3,1,1,0), 24, 2},
341 {RTE_IPV4(4,1,1,0), 24, 3},
342 {RTE_IPV4(5,1,1,0), 24, 4},
343 {RTE_IPV4(6,1,1,0), 24, 5},
344 {RTE_IPV4(7,1,1,0), 24, 6},
345 {RTE_IPV4(8,1,1,0), 24, 7},
348 #define IPV4_L3FWD_LPM_MAX_RULES 1024
350 typedef struct rte_lpm lookup_struct_t;
351 static lookup_struct_t *ipv4_l3fwd_lookup_struct[NB_SOCKETS];
356 struct lcore_rx_queue rx_queue_list[MAX_RX_QUEUE_PER_LCORE];
358 uint16_t tx_port_id[RTE_MAX_ETHPORTS];
359 uint16_t tx_queue_id[RTE_MAX_ETHPORTS];
360 struct rte_eth_dev_tx_buffer *tx_buffer[RTE_MAX_ETHPORTS];
361 lookup_struct_t * ipv4_lookup_struct;
362 lookup_struct_t * ipv6_lookup_struct;
363 } __rte_cache_aligned;
366 /* total sleep time in ms since last frequency scaling down */
368 /* number of long sleep recently */
369 uint32_t nb_long_sleep;
370 /* freq. scaling up trend */
372 /* total packet processed recently */
373 uint64_t nb_rx_processed;
374 /* total iterations looped recently */
375 uint64_t nb_iteration_looped;
377 * Represents empty and non empty polls
378 * of rte_eth_rx_burst();
379 * ep_nep[0] holds non empty polls
380 * i.e. 0 < nb_rx <= MAX_BURST
381 * ep_nep[1] holds empty polls.
386 * Represents full and empty+partial
387 * polls of rte_eth_rx_burst();
388 * ep_nep[0] holds empty+partial polls.
389 * i.e. 0 <= nb_rx < MAX_BURST
390 * ep_nep[1] holds full polls
391 * i.e. nb_rx == MAX_BURST
395 rte_spinlock_t telemetry_lock;
396 } __rte_cache_aligned;
398 static struct lcore_conf lcore_conf[RTE_MAX_LCORE] __rte_cache_aligned;
399 static struct lcore_stats stats[RTE_MAX_LCORE] __rte_cache_aligned;
400 static struct rte_timer power_timers[RTE_MAX_LCORE];
402 static inline uint32_t power_idle_heuristic(uint32_t zero_rx_packet_count);
403 static inline enum freq_scale_hint_t power_freq_scaleup_heuristic( \
404 unsigned int lcore_id, uint16_t port_id, uint16_t queue_id);
408 * These defaults are using the max frequency index (1), a medium index (9)
409 * and a typical low frequency index (14). These can be adjusted to use
410 * different indexes using the relevant command line parameters.
412 static uint8_t freq_tlb[] = {14, 9, 1};
414 static int is_done(void)
419 /* exit signal handler */
421 signal_exit_now(int sigtype)
424 if (sigtype == SIGINT)
429 /* Freqency scale down timer callback */
431 power_timer_cb(__rte_unused struct rte_timer *tim,
432 __rte_unused void *arg)
435 float sleep_time_ratio;
436 unsigned lcore_id = rte_lcore_id();
438 /* accumulate total execution time in us when callback is invoked */
439 sleep_time_ratio = (float)(stats[lcore_id].sleep_time) /
440 (float)SCALING_PERIOD;
442 * check whether need to scale down frequency a step if it sleep a lot.
444 if (sleep_time_ratio >= SCALING_DOWN_TIME_RATIO_THRESHOLD) {
445 if (rte_power_freq_down)
446 rte_power_freq_down(lcore_id);
448 else if ( (unsigned)(stats[lcore_id].nb_rx_processed /
449 stats[lcore_id].nb_iteration_looped) < MAX_PKT_BURST) {
451 * scale down a step if average packet per iteration less
454 if (rte_power_freq_down)
455 rte_power_freq_down(lcore_id);
459 * initialize another timer according to current frequency to ensure
460 * timer interval is relatively fixed.
462 hz = rte_get_timer_hz();
463 rte_timer_reset(&power_timers[lcore_id], hz/TIMER_NUMBER_PER_SECOND,
464 SINGLE, lcore_id, power_timer_cb, NULL);
466 stats[lcore_id].nb_rx_processed = 0;
467 stats[lcore_id].nb_iteration_looped = 0;
469 stats[lcore_id].sleep_time = 0;
472 /* Enqueue a single packet, and send burst if queue is filled */
474 send_single_packet(struct rte_mbuf *m, uint16_t port)
477 struct lcore_conf *qconf;
479 lcore_id = rte_lcore_id();
480 qconf = &lcore_conf[lcore_id];
482 rte_eth_tx_buffer(port, qconf->tx_queue_id[port],
483 qconf->tx_buffer[port], m);
488 #ifdef DO_RFC_1812_CHECKS
490 is_valid_ipv4_pkt(struct rte_ipv4_hdr *pkt, uint32_t link_len)
492 /* From http://www.rfc-editor.org/rfc/rfc1812.txt section 5.2.2 */
494 * 1. The packet length reported by the Link Layer must be large
495 * enough to hold the minimum length legal IP datagram (20 bytes).
497 if (link_len < sizeof(struct rte_ipv4_hdr))
500 /* 2. The IP checksum must be correct. */
501 /* this is checked in H/W */
504 * 3. The IP version number must be 4. If the version number is not 4
505 * then the packet may be another version of IP, such as IPng or
508 if (((pkt->version_ihl) >> 4) != 4)
511 * 4. The IP header length field must be large enough to hold the
512 * minimum length legal IP datagram (20 bytes = 5 words).
514 if ((pkt->version_ihl & 0xf) < 5)
518 * 5. The IP total length field must be large enough to hold the IP
519 * datagram header, whose length is specified in the IP header length
522 if (rte_cpu_to_be_16(pkt->total_length) < sizeof(struct rte_ipv4_hdr))
529 #if (APP_LOOKUP_METHOD == APP_LOOKUP_EXACT_MATCH)
531 print_ipv4_key(struct ipv4_5tuple key)
533 printf("IP dst = %08x, IP src = %08x, port dst = %d, port src = %d, "
534 "proto = %d\n", (unsigned)key.ip_dst, (unsigned)key.ip_src,
535 key.port_dst, key.port_src, key.proto);
538 print_ipv6_key(struct ipv6_5tuple key)
540 printf( "IP dst = " IPv6_BYTES_FMT ", IP src = " IPv6_BYTES_FMT ", "
541 "port dst = %d, port src = %d, proto = %d\n",
542 IPv6_BYTES(key.ip_dst), IPv6_BYTES(key.ip_src),
543 key.port_dst, key.port_src, key.proto);
546 static inline uint16_t
547 get_ipv4_dst_port(struct rte_ipv4_hdr *ipv4_hdr, uint16_t portid,
548 lookup_struct_t * ipv4_l3fwd_lookup_struct)
550 struct ipv4_5tuple key;
551 struct rte_tcp_hdr *tcp;
552 struct rte_udp_hdr *udp;
555 key.ip_dst = rte_be_to_cpu_32(ipv4_hdr->dst_addr);
556 key.ip_src = rte_be_to_cpu_32(ipv4_hdr->src_addr);
557 key.proto = ipv4_hdr->next_proto_id;
559 switch (ipv4_hdr->next_proto_id) {
561 tcp = (struct rte_tcp_hdr *)((unsigned char *)ipv4_hdr +
562 sizeof(struct rte_ipv4_hdr));
563 key.port_dst = rte_be_to_cpu_16(tcp->dst_port);
564 key.port_src = rte_be_to_cpu_16(tcp->src_port);
568 udp = (struct rte_udp_hdr *)((unsigned char *)ipv4_hdr +
569 sizeof(struct rte_ipv4_hdr));
570 key.port_dst = rte_be_to_cpu_16(udp->dst_port);
571 key.port_src = rte_be_to_cpu_16(udp->src_port);
580 /* Find destination port */
581 ret = rte_hash_lookup(ipv4_l3fwd_lookup_struct, (const void *)&key);
582 return ((ret < 0) ? portid : ipv4_l3fwd_out_if[ret]);
585 static inline uint16_t
586 get_ipv6_dst_port(struct rte_ipv6_hdr *ipv6_hdr, uint16_t portid,
587 lookup_struct_t *ipv6_l3fwd_lookup_struct)
589 struct ipv6_5tuple key;
590 struct rte_tcp_hdr *tcp;
591 struct rte_udp_hdr *udp;
594 memcpy(key.ip_dst, ipv6_hdr->dst_addr, IPV6_ADDR_LEN);
595 memcpy(key.ip_src, ipv6_hdr->src_addr, IPV6_ADDR_LEN);
597 key.proto = ipv6_hdr->proto;
599 switch (ipv6_hdr->proto) {
601 tcp = (struct rte_tcp_hdr *)((unsigned char *) ipv6_hdr +
602 sizeof(struct rte_ipv6_hdr));
603 key.port_dst = rte_be_to_cpu_16(tcp->dst_port);
604 key.port_src = rte_be_to_cpu_16(tcp->src_port);
608 udp = (struct rte_udp_hdr *)((unsigned char *) ipv6_hdr +
609 sizeof(struct rte_ipv6_hdr));
610 key.port_dst = rte_be_to_cpu_16(udp->dst_port);
611 key.port_src = rte_be_to_cpu_16(udp->src_port);
620 /* Find destination port */
621 ret = rte_hash_lookup(ipv6_l3fwd_lookup_struct, (const void *)&key);
622 return ((ret < 0) ? portid : ipv6_l3fwd_out_if[ret]);
626 #if (APP_LOOKUP_METHOD == APP_LOOKUP_LPM)
627 static inline uint16_t
628 get_ipv4_dst_port(struct rte_ipv4_hdr *ipv4_hdr, uint16_t portid,
629 lookup_struct_t *ipv4_l3fwd_lookup_struct)
633 return ((rte_lpm_lookup(ipv4_l3fwd_lookup_struct,
634 rte_be_to_cpu_32(ipv4_hdr->dst_addr), &next_hop) == 0)?
640 parse_ptype_one(struct rte_mbuf *m)
642 struct rte_ether_hdr *eth_hdr;
643 uint32_t packet_type = RTE_PTYPE_UNKNOWN;
646 eth_hdr = rte_pktmbuf_mtod(m, struct rte_ether_hdr *);
647 ether_type = eth_hdr->ether_type;
648 if (ether_type == rte_cpu_to_be_16(RTE_ETHER_TYPE_IPV4))
649 packet_type |= RTE_PTYPE_L3_IPV4_EXT_UNKNOWN;
650 else if (ether_type == rte_cpu_to_be_16(RTE_ETHER_TYPE_IPV6))
651 packet_type |= RTE_PTYPE_L3_IPV6_EXT_UNKNOWN;
653 m->packet_type = packet_type;
657 cb_parse_ptype(uint16_t port __rte_unused, uint16_t queue __rte_unused,
658 struct rte_mbuf *pkts[], uint16_t nb_pkts,
659 uint16_t max_pkts __rte_unused,
660 void *user_param __rte_unused)
664 for (i = 0; i < nb_pkts; ++i)
665 parse_ptype_one(pkts[i]);
671 add_cb_parse_ptype(uint16_t portid, uint16_t queueid)
673 printf("Port %d: softly parse packet type info\n", portid);
674 if (rte_eth_add_rx_callback(portid, queueid, cb_parse_ptype, NULL))
677 printf("Failed to add rx callback: port=%d\n", portid);
682 l3fwd_simple_forward(struct rte_mbuf *m, uint16_t portid,
683 struct lcore_conf *qconf)
685 struct rte_ether_hdr *eth_hdr;
686 struct rte_ipv4_hdr *ipv4_hdr;
690 eth_hdr = rte_pktmbuf_mtod(m, struct rte_ether_hdr *);
692 if (RTE_ETH_IS_IPV4_HDR(m->packet_type)) {
693 /* Handle IPv4 headers.*/
695 rte_pktmbuf_mtod_offset(m, struct rte_ipv4_hdr *,
696 sizeof(struct rte_ether_hdr));
698 #ifdef DO_RFC_1812_CHECKS
699 /* Check to make sure the packet is valid (RFC1812) */
700 if (is_valid_ipv4_pkt(ipv4_hdr, m->pkt_len) < 0) {
706 dst_port = get_ipv4_dst_port(ipv4_hdr, portid,
707 qconf->ipv4_lookup_struct);
708 if (dst_port >= RTE_MAX_ETHPORTS ||
709 (enabled_port_mask & 1 << dst_port) == 0)
712 /* 02:00:00:00:00:xx */
713 d_addr_bytes = ð_hdr->d_addr.addr_bytes[0];
714 *((uint64_t *)d_addr_bytes) =
715 0x000000000002 + ((uint64_t)dst_port << 40);
717 #ifdef DO_RFC_1812_CHECKS
718 /* Update time to live and header checksum */
719 --(ipv4_hdr->time_to_live);
720 ++(ipv4_hdr->hdr_checksum);
724 rte_ether_addr_copy(&ports_eth_addr[dst_port],
727 send_single_packet(m, dst_port);
728 } else if (RTE_ETH_IS_IPV6_HDR(m->packet_type)) {
729 /* Handle IPv6 headers.*/
730 #if (APP_LOOKUP_METHOD == APP_LOOKUP_EXACT_MATCH)
731 struct rte_ipv6_hdr *ipv6_hdr;
734 rte_pktmbuf_mtod_offset(m, struct rte_ipv6_hdr *,
735 sizeof(struct rte_ether_hdr));
737 dst_port = get_ipv6_dst_port(ipv6_hdr, portid,
738 qconf->ipv6_lookup_struct);
740 if (dst_port >= RTE_MAX_ETHPORTS ||
741 (enabled_port_mask & 1 << dst_port) == 0)
744 /* 02:00:00:00:00:xx */
745 d_addr_bytes = ð_hdr->d_addr.addr_bytes[0];
746 *((uint64_t *)d_addr_bytes) =
747 0x000000000002 + ((uint64_t)dst_port << 40);
750 rte_ether_addr_copy(&ports_eth_addr[dst_port],
753 send_single_packet(m, dst_port);
755 /* We don't currently handle IPv6 packets in LPM mode. */
763 #define MINIMUM_SLEEP_TIME 1
764 #define SUSPEND_THRESHOLD 300
766 static inline uint32_t
767 power_idle_heuristic(uint32_t zero_rx_packet_count)
769 /* If zero count is less than 100, sleep 1us */
770 if (zero_rx_packet_count < SUSPEND_THRESHOLD)
771 return MINIMUM_SLEEP_TIME;
772 /* If zero count is less than 1000, sleep 100 us which is the
773 minimum latency switching from C3/C6 to C0
776 return SUSPEND_THRESHOLD;
779 static inline enum freq_scale_hint_t
780 power_freq_scaleup_heuristic(unsigned lcore_id,
784 uint32_t rxq_count = rte_eth_rx_queue_count(port_id, queue_id);
786 * HW Rx queue size is 128 by default, Rx burst read at maximum 32 entries
789 #define FREQ_GEAR1_RX_PACKET_THRESHOLD MAX_PKT_BURST
790 #define FREQ_GEAR2_RX_PACKET_THRESHOLD (MAX_PKT_BURST*2)
791 #define FREQ_GEAR3_RX_PACKET_THRESHOLD (MAX_PKT_BURST*3)
792 #define FREQ_UP_TREND1_ACC 1
793 #define FREQ_UP_TREND2_ACC 100
794 #define FREQ_UP_THRESHOLD 10000
796 if (likely(rxq_count > FREQ_GEAR3_RX_PACKET_THRESHOLD)) {
797 stats[lcore_id].trend = 0;
799 } else if (likely(rxq_count > FREQ_GEAR2_RX_PACKET_THRESHOLD))
800 stats[lcore_id].trend += FREQ_UP_TREND2_ACC;
801 else if (likely(rxq_count > FREQ_GEAR1_RX_PACKET_THRESHOLD))
802 stats[lcore_id].trend += FREQ_UP_TREND1_ACC;
804 if (likely(stats[lcore_id].trend > FREQ_UP_THRESHOLD)) {
805 stats[lcore_id].trend = 0;
813 * force polling thread sleep until one-shot rx interrupt triggers
822 sleep_until_rx_interrupt(int num)
825 * we want to track when we are woken up by traffic so that we can go
826 * back to sleep again without log spamming.
829 struct rte_epoll_event event[num];
836 RTE_LOG(INFO, L3FWD_POWER,
837 "lcore %u sleeps until interrupt triggers\n",
841 n = rte_epoll_wait(RTE_EPOLL_PER_THREAD, event, num, 10);
842 for (i = 0; i < n; i++) {
843 data = event[i].epdata.data;
844 port_id = ((uintptr_t)data) >> CHAR_BIT;
845 queue_id = ((uintptr_t)data) &
846 RTE_LEN2MASK(CHAR_BIT, uint8_t);
847 RTE_LOG(INFO, L3FWD_POWER,
848 "lcore %u is waked up from rx interrupt on"
849 " port %d queue %d\n",
850 rte_lcore_id(), port_id, queue_id);
857 static void turn_on_off_intr(struct lcore_conf *qconf, bool on)
860 struct lcore_rx_queue *rx_queue;
864 for (i = 0; i < qconf->n_rx_queue; ++i) {
865 rx_queue = &(qconf->rx_queue_list[i]);
866 port_id = rx_queue->port_id;
867 queue_id = rx_queue->queue_id;
869 rte_spinlock_lock(&(locks[port_id]));
871 rte_eth_dev_rx_intr_enable(port_id, queue_id);
873 rte_eth_dev_rx_intr_disable(port_id, queue_id);
874 rte_spinlock_unlock(&(locks[port_id]));
878 static int event_register(struct lcore_conf *qconf)
880 struct lcore_rx_queue *rx_queue;
887 for (i = 0; i < qconf->n_rx_queue; ++i) {
888 rx_queue = &(qconf->rx_queue_list[i]);
889 portid = rx_queue->port_id;
890 queueid = rx_queue->queue_id;
891 data = portid << CHAR_BIT | queueid;
893 ret = rte_eth_dev_rx_intr_ctl_q(portid, queueid,
894 RTE_EPOLL_PER_THREAD,
896 (void *)((uintptr_t)data));
903 /* main processing loop */
905 main_telemetry_loop(__rte_unused void *dummy)
907 struct rte_mbuf *pkts_burst[MAX_PKT_BURST];
908 unsigned int lcore_id;
909 uint64_t prev_tsc, diff_tsc, cur_tsc, prev_tel_tsc;
913 struct lcore_conf *qconf;
914 struct lcore_rx_queue *rx_queue;
915 uint64_t ep_nep[2] = {0}, fp_nfp[2] = {0};
919 const uint64_t drain_tsc = (rte_get_tsc_hz() + US_PER_S - 1) /
920 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 telemetry 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, " -- lcoreid=%u portid=%u "
942 "rxqueueid=%hhu\n", lcore_id, portid, queueid);
947 cur_tsc = rte_rdtsc();
949 * TX burst queue drain
951 diff_tsc = cur_tsc - prev_tsc;
952 if (unlikely(diff_tsc > drain_tsc)) {
953 for (i = 0; i < qconf->n_tx_port; ++i) {
954 portid = qconf->tx_port_id[i];
955 rte_eth_tx_buffer_flush(portid,
956 qconf->tx_queue_id[portid],
957 qconf->tx_buffer[portid]);
963 * Read packet from RX queues
965 for (i = 0; i < qconf->n_rx_queue; ++i) {
966 rx_queue = &(qconf->rx_queue_list[i]);
967 portid = rx_queue->port_id;
968 queueid = rx_queue->queue_id;
970 nb_rx = rte_eth_rx_burst(portid, queueid, pkts_burst,
972 ep_nep[nb_rx == 0]++;
973 fp_nfp[nb_rx == MAX_PKT_BURST]++;
975 if (unlikely(nb_rx == 0))
978 /* Prefetch first packets */
979 for (j = 0; j < PREFETCH_OFFSET && j < nb_rx; j++) {
980 rte_prefetch0(rte_pktmbuf_mtod(
981 pkts_burst[j], void *));
984 /* Prefetch and forward already prefetched packets */
985 for (j = 0; j < (nb_rx - PREFETCH_OFFSET); j++) {
986 rte_prefetch0(rte_pktmbuf_mtod(pkts_burst[
987 j + PREFETCH_OFFSET], void *));
988 l3fwd_simple_forward(pkts_burst[j], portid,
992 /* Forward remaining prefetched packets */
993 for (; j < nb_rx; j++) {
994 l3fwd_simple_forward(pkts_burst[j], portid,
998 if (unlikely(poll_count >= DEFAULT_COUNT)) {
999 diff_tsc = cur_tsc - prev_tel_tsc;
1000 if (diff_tsc >= MAX_CYCLES) {
1002 } else if (diff_tsc > MIN_CYCLES &&
1003 diff_tsc < MAX_CYCLES) {
1004 br = (diff_tsc * 100) / MAX_CYCLES;
1009 prev_tel_tsc = cur_tsc;
1010 /* update stats for telemetry */
1011 rte_spinlock_lock(&stats[lcore_id].telemetry_lock);
1012 stats[lcore_id].ep_nep[0] = ep_nep[0];
1013 stats[lcore_id].ep_nep[1] = ep_nep[1];
1014 stats[lcore_id].fp_nfp[0] = fp_nfp[0];
1015 stats[lcore_id].fp_nfp[1] = fp_nfp[1];
1016 stats[lcore_id].br = br;
1017 rte_spinlock_unlock(&stats[lcore_id].telemetry_lock);
1023 /* main processing loop */
1025 main_empty_poll_loop(__rte_unused void *dummy)
1027 struct rte_mbuf *pkts_burst[MAX_PKT_BURST];
1028 unsigned int lcore_id;
1029 uint64_t prev_tsc, diff_tsc, cur_tsc;
1033 struct lcore_conf *qconf;
1034 struct lcore_rx_queue *rx_queue;
1036 const uint64_t drain_tsc =
1037 (rte_get_tsc_hz() + US_PER_S - 1) /
1038 US_PER_S * BURST_TX_DRAIN_US;
1042 lcore_id = rte_lcore_id();
1043 qconf = &lcore_conf[lcore_id];
1045 if (qconf->n_rx_queue == 0) {
1046 RTE_LOG(INFO, L3FWD_POWER, "lcore %u has nothing to do\n",
1051 for (i = 0; i < qconf->n_rx_queue; i++) {
1052 portid = qconf->rx_queue_list[i].port_id;
1053 queueid = qconf->rx_queue_list[i].queue_id;
1054 RTE_LOG(INFO, L3FWD_POWER, " -- lcoreid=%u portid=%u "
1055 "rxqueueid=%hhu\n", lcore_id, portid, queueid);
1058 while (!is_done()) {
1059 stats[lcore_id].nb_iteration_looped++;
1061 cur_tsc = rte_rdtsc();
1063 * TX burst queue drain
1065 diff_tsc = cur_tsc - prev_tsc;
1066 if (unlikely(diff_tsc > drain_tsc)) {
1067 for (i = 0; i < qconf->n_tx_port; ++i) {
1068 portid = qconf->tx_port_id[i];
1069 rte_eth_tx_buffer_flush(portid,
1070 qconf->tx_queue_id[portid],
1071 qconf->tx_buffer[portid]);
1077 * Read packet from RX queues
1079 for (i = 0; i < qconf->n_rx_queue; ++i) {
1080 rx_queue = &(qconf->rx_queue_list[i]);
1081 rx_queue->idle_hint = 0;
1082 portid = rx_queue->port_id;
1083 queueid = rx_queue->queue_id;
1085 nb_rx = rte_eth_rx_burst(portid, queueid, pkts_burst,
1088 stats[lcore_id].nb_rx_processed += nb_rx;
1092 rte_power_empty_poll_stat_update(lcore_id);
1096 rte_power_poll_stat_update(lcore_id, nb_rx);
1100 /* Prefetch first packets */
1101 for (j = 0; j < PREFETCH_OFFSET && j < nb_rx; j++) {
1102 rte_prefetch0(rte_pktmbuf_mtod(
1103 pkts_burst[j], void *));
1106 /* Prefetch and forward already prefetched packets */
1107 for (j = 0; j < (nb_rx - PREFETCH_OFFSET); j++) {
1108 rte_prefetch0(rte_pktmbuf_mtod(pkts_burst[
1109 j + PREFETCH_OFFSET],
1111 l3fwd_simple_forward(pkts_burst[j], portid,
1115 /* Forward remaining prefetched packets */
1116 for (; j < nb_rx; j++) {
1117 l3fwd_simple_forward(pkts_burst[j], portid,
1127 /* main processing loop */
1129 main_loop(__rte_unused void *dummy)
1131 struct rte_mbuf *pkts_burst[MAX_PKT_BURST];
1133 uint64_t prev_tsc, diff_tsc, cur_tsc, tim_res_tsc, hz;
1134 uint64_t prev_tsc_power = 0, cur_tsc_power, diff_tsc_power;
1138 struct lcore_conf *qconf;
1139 struct lcore_rx_queue *rx_queue;
1140 enum freq_scale_hint_t lcore_scaleup_hint;
1141 uint32_t lcore_rx_idle_count = 0;
1142 uint32_t lcore_idle_hint = 0;
1145 const uint64_t drain_tsc = (rte_get_tsc_hz() + US_PER_S - 1) / US_PER_S * BURST_TX_DRAIN_US;
1148 hz = rte_get_timer_hz();
1149 tim_res_tsc = hz/TIMER_NUMBER_PER_SECOND;
1151 lcore_id = rte_lcore_id();
1152 qconf = &lcore_conf[lcore_id];
1154 if (qconf->n_rx_queue == 0) {
1155 RTE_LOG(INFO, L3FWD_POWER, "lcore %u has nothing to do\n", lcore_id);
1159 RTE_LOG(INFO, L3FWD_POWER, "entering main loop on lcore %u\n", lcore_id);
1161 for (i = 0; i < qconf->n_rx_queue; i++) {
1162 portid = qconf->rx_queue_list[i].port_id;
1163 queueid = qconf->rx_queue_list[i].queue_id;
1164 RTE_LOG(INFO, L3FWD_POWER, " -- lcoreid=%u portid=%u "
1165 "rxqueueid=%hhu\n", lcore_id, portid, queueid);
1168 /* add into event wait list */
1169 if (event_register(qconf) == 0)
1172 RTE_LOG(INFO, L3FWD_POWER, "RX interrupt won't enable.\n");
1174 while (!is_done()) {
1175 stats[lcore_id].nb_iteration_looped++;
1177 cur_tsc = rte_rdtsc();
1178 cur_tsc_power = cur_tsc;
1181 * TX burst queue drain
1183 diff_tsc = cur_tsc - prev_tsc;
1184 if (unlikely(diff_tsc > drain_tsc)) {
1185 for (i = 0; i < qconf->n_tx_port; ++i) {
1186 portid = qconf->tx_port_id[i];
1187 rte_eth_tx_buffer_flush(portid,
1188 qconf->tx_queue_id[portid],
1189 qconf->tx_buffer[portid]);
1194 diff_tsc_power = cur_tsc_power - prev_tsc_power;
1195 if (diff_tsc_power > tim_res_tsc) {
1197 prev_tsc_power = cur_tsc_power;
1202 * Read packet from RX queues
1204 lcore_scaleup_hint = FREQ_CURRENT;
1205 lcore_rx_idle_count = 0;
1206 for (i = 0; i < qconf->n_rx_queue; ++i) {
1207 rx_queue = &(qconf->rx_queue_list[i]);
1208 rx_queue->idle_hint = 0;
1209 portid = rx_queue->port_id;
1210 queueid = rx_queue->queue_id;
1212 nb_rx = rte_eth_rx_burst(portid, queueid, pkts_burst,
1215 stats[lcore_id].nb_rx_processed += nb_rx;
1216 if (unlikely(nb_rx == 0)) {
1218 * no packet received from rx queue, try to
1219 * sleep for a while forcing CPU enter deeper
1222 rx_queue->zero_rx_packet_count++;
1224 if (rx_queue->zero_rx_packet_count <=
1225 MIN_ZERO_POLL_COUNT)
1228 rx_queue->idle_hint = power_idle_heuristic(\
1229 rx_queue->zero_rx_packet_count);
1230 lcore_rx_idle_count++;
1232 rx_queue->zero_rx_packet_count = 0;
1235 * do not scale up frequency immediately as
1236 * user to kernel space communication is costly
1237 * which might impact packet I/O for received
1240 rx_queue->freq_up_hint =
1241 power_freq_scaleup_heuristic(lcore_id,
1245 /* Prefetch first packets */
1246 for (j = 0; j < PREFETCH_OFFSET && j < nb_rx; j++) {
1247 rte_prefetch0(rte_pktmbuf_mtod(
1248 pkts_burst[j], void *));
1251 /* Prefetch and forward already prefetched packets */
1252 for (j = 0; j < (nb_rx - PREFETCH_OFFSET); j++) {
1253 rte_prefetch0(rte_pktmbuf_mtod(pkts_burst[
1254 j + PREFETCH_OFFSET], void *));
1255 l3fwd_simple_forward(pkts_burst[j], portid,
1259 /* Forward remaining prefetched packets */
1260 for (; j < nb_rx; j++) {
1261 l3fwd_simple_forward(pkts_burst[j], portid,
1266 if (likely(lcore_rx_idle_count != qconf->n_rx_queue)) {
1267 for (i = 1, lcore_scaleup_hint =
1268 qconf->rx_queue_list[0].freq_up_hint;
1269 i < qconf->n_rx_queue; ++i) {
1270 rx_queue = &(qconf->rx_queue_list[i]);
1271 if (rx_queue->freq_up_hint >
1273 lcore_scaleup_hint =
1274 rx_queue->freq_up_hint;
1277 if (lcore_scaleup_hint == FREQ_HIGHEST) {
1278 if (rte_power_freq_max)
1279 rte_power_freq_max(lcore_id);
1280 } else if (lcore_scaleup_hint == FREQ_HIGHER) {
1281 if (rte_power_freq_up)
1282 rte_power_freq_up(lcore_id);
1286 * All Rx queues empty in recent consecutive polls,
1287 * sleep in a conservative manner, meaning sleep as
1290 for (i = 1, lcore_idle_hint =
1291 qconf->rx_queue_list[0].idle_hint;
1292 i < qconf->n_rx_queue; ++i) {
1293 rx_queue = &(qconf->rx_queue_list[i]);
1294 if (rx_queue->idle_hint < lcore_idle_hint)
1295 lcore_idle_hint = rx_queue->idle_hint;
1298 if (lcore_idle_hint < SUSPEND_THRESHOLD)
1300 * execute "pause" instruction to avoid context
1301 * switch which generally take hundred of
1302 * microseconds for short sleep.
1304 rte_delay_us(lcore_idle_hint);
1306 /* suspend until rx interrupt triggers */
1308 turn_on_off_intr(qconf, 1);
1309 sleep_until_rx_interrupt(
1311 turn_on_off_intr(qconf, 0);
1313 * start receiving packets immediately
1315 if (likely(!is_done()))
1319 stats[lcore_id].sleep_time += lcore_idle_hint;
1327 check_lcore_params(void)
1329 uint8_t queue, lcore;
1333 for (i = 0; i < nb_lcore_params; ++i) {
1334 queue = lcore_params[i].queue_id;
1335 if (queue >= MAX_RX_QUEUE_PER_PORT) {
1336 printf("invalid queue number: %hhu\n", queue);
1339 lcore = lcore_params[i].lcore_id;
1340 if (!rte_lcore_is_enabled(lcore)) {
1341 printf("error: lcore %hhu is not enabled in lcore "
1345 if ((socketid = rte_lcore_to_socket_id(lcore) != 0) &&
1347 printf("warning: lcore %hhu is on socket %d with numa "
1348 "off\n", lcore, socketid);
1350 if (app_mode == APP_MODE_TELEMETRY && lcore == rte_lcore_id()) {
1351 printf("cannot enable master core %d in config for telemetry mode\n",
1360 check_port_config(void)
1365 for (i = 0; i < nb_lcore_params; ++i) {
1366 portid = lcore_params[i].port_id;
1367 if ((enabled_port_mask & (1 << portid)) == 0) {
1368 printf("port %u is not enabled in port mask\n",
1372 if (!rte_eth_dev_is_valid_port(portid)) {
1373 printf("port %u is not present on the board\n",
1382 get_port_n_rx_queues(const uint16_t port)
1387 for (i = 0; i < nb_lcore_params; ++i) {
1388 if (lcore_params[i].port_id == port &&
1389 lcore_params[i].queue_id > queue)
1390 queue = lcore_params[i].queue_id;
1392 return (uint8_t)(++queue);
1396 init_lcore_rx_queues(void)
1398 uint16_t i, nb_rx_queue;
1401 for (i = 0; i < nb_lcore_params; ++i) {
1402 lcore = lcore_params[i].lcore_id;
1403 nb_rx_queue = lcore_conf[lcore].n_rx_queue;
1404 if (nb_rx_queue >= MAX_RX_QUEUE_PER_LCORE) {
1405 printf("error: too many queues (%u) for lcore: %u\n",
1406 (unsigned)nb_rx_queue + 1, (unsigned)lcore);
1409 lcore_conf[lcore].rx_queue_list[nb_rx_queue].port_id =
1410 lcore_params[i].port_id;
1411 lcore_conf[lcore].rx_queue_list[nb_rx_queue].queue_id =
1412 lcore_params[i].queue_id;
1413 lcore_conf[lcore].n_rx_queue++;
1421 print_usage(const char *prgname)
1423 printf ("%s [EAL options] -- -p PORTMASK -P"
1424 " [--config (port,queue,lcore)[,(port,queue,lcore]]"
1425 " [--high-perf-cores CORELIST"
1426 " [--perf-config (port,queue,hi_perf,lcore_index)[,(port,queue,hi_perf,lcore_index]]"
1427 " [--enable-jumbo [--max-pkt-len PKTLEN]]\n"
1428 " -p PORTMASK: hexadecimal bitmask of ports to configure\n"
1429 " -P : enable promiscuous mode\n"
1430 " --config (port,queue,lcore): rx queues configuration\n"
1431 " --high-perf-cores CORELIST: list of high performance cores\n"
1432 " --perf-config: similar as config, cores specified as indices"
1433 " for bins containing high or regular performance cores\n"
1434 " --no-numa: optional, disable numa awareness\n"
1435 " --enable-jumbo: enable jumbo frame"
1436 " which max packet len is PKTLEN in decimal (64-9600)\n"
1437 " --parse-ptype: parse packet type by software\n"
1438 " --empty-poll: enable empty poll detection"
1439 " follow (training_flag, high_threshold, med_threshold)\n"
1440 " --telemetry: enable telemetry mode, to update"
1441 " empty polls, full polls, and core busyness to telemetry\n",
1445 static int parse_max_pkt_len(const char *pktlen)
1450 /* parse decimal string */
1451 len = strtoul(pktlen, &end, 10);
1452 if ((pktlen[0] == '\0') || (end == NULL) || (*end != '\0'))
1462 parse_portmask(const char *portmask)
1467 /* parse hexadecimal string */
1468 pm = strtoul(portmask, &end, 16);
1469 if ((portmask[0] == '\0') || (end == NULL) || (*end != '\0'))
1479 parse_config(const char *q_arg)
1482 const char *p, *p0 = q_arg;
1490 unsigned long int_fld[_NUM_FLD];
1491 char *str_fld[_NUM_FLD];
1495 nb_lcore_params = 0;
1497 while ((p = strchr(p0,'(')) != NULL) {
1499 if((p0 = strchr(p,')')) == NULL)
1503 if(size >= sizeof(s))
1506 snprintf(s, sizeof(s), "%.*s", size, p);
1507 if (rte_strsplit(s, sizeof(s), str_fld, _NUM_FLD, ',') !=
1510 for (i = 0; i < _NUM_FLD; i++){
1512 int_fld[i] = strtoul(str_fld[i], &end, 0);
1513 if (errno != 0 || end == str_fld[i] || int_fld[i] >
1517 if (nb_lcore_params >= MAX_LCORE_PARAMS) {
1518 printf("exceeded max number of lcore params: %hu\n",
1522 lcore_params_array[nb_lcore_params].port_id =
1523 (uint8_t)int_fld[FLD_PORT];
1524 lcore_params_array[nb_lcore_params].queue_id =
1525 (uint8_t)int_fld[FLD_QUEUE];
1526 lcore_params_array[nb_lcore_params].lcore_id =
1527 (uint8_t)int_fld[FLD_LCORE];
1530 lcore_params = lcore_params_array;
1535 parse_ep_config(const char *q_arg)
1538 const char *p = q_arg;
1548 ep_med_edpi = EMPTY_POLL_MED_THRESHOLD;
1549 ep_hgh_edpi = EMPTY_POLL_MED_THRESHOLD;
1551 strlcpy(s, p, sizeof(s));
1553 num_arg = rte_strsplit(s, sizeof(s), str_fld, 3, ',');
1555 empty_poll_train = false;
1562 training_flag = strtoul(str_fld[0], &end, 0);
1563 med_edpi = strtoul(str_fld[1], &end, 0);
1564 hgh_edpi = strtoul(str_fld[2], &end, 0);
1566 if (training_flag == 1)
1567 empty_poll_train = true;
1570 ep_med_edpi = med_edpi;
1573 ep_hgh_edpi = hgh_edpi;
1583 #define CMD_LINE_OPT_PARSE_PTYPE "parse-ptype"
1584 #define CMD_LINE_OPT_TELEMETRY "telemetry"
1586 /* Parse the argument given in the command line of the application */
1588 parse_args(int argc, char **argv)
1594 char *prgname = argv[0];
1595 static struct option lgopts[] = {
1596 {"config", 1, 0, 0},
1597 {"perf-config", 1, 0, 0},
1598 {"high-perf-cores", 1, 0, 0},
1599 {"no-numa", 0, 0, 0},
1600 {"enable-jumbo", 0, 0, 0},
1601 {"empty-poll", 1, 0, 0},
1602 {CMD_LINE_OPT_PARSE_PTYPE, 0, 0, 0},
1603 {CMD_LINE_OPT_TELEMETRY, 0, 0, 0},
1609 while ((opt = getopt_long(argc, argvopt, "p:l:m:h:P",
1610 lgopts, &option_index)) != EOF) {
1615 enabled_port_mask = parse_portmask(optarg);
1616 if (enabled_port_mask == 0) {
1617 printf("invalid portmask\n");
1618 print_usage(prgname);
1623 printf("Promiscuous mode selected\n");
1627 limit = parse_max_pkt_len(optarg);
1628 freq_tlb[LOW] = limit;
1631 limit = parse_max_pkt_len(optarg);
1632 freq_tlb[MED] = limit;
1635 limit = parse_max_pkt_len(optarg);
1636 freq_tlb[HGH] = limit;
1640 if (!strncmp(lgopts[option_index].name, "config", 6)) {
1641 ret = parse_config(optarg);
1643 printf("invalid config\n");
1644 print_usage(prgname);
1649 if (!strncmp(lgopts[option_index].name,
1650 "perf-config", 11)) {
1651 ret = parse_perf_config(optarg);
1653 printf("invalid perf-config\n");
1654 print_usage(prgname);
1659 if (!strncmp(lgopts[option_index].name,
1660 "high-perf-cores", 15)) {
1661 ret = parse_perf_core_list(optarg);
1663 printf("invalid high-perf-cores\n");
1664 print_usage(prgname);
1669 if (!strncmp(lgopts[option_index].name,
1671 printf("numa is disabled \n");
1675 if (!strncmp(lgopts[option_index].name,
1676 "empty-poll", 10)) {
1677 if (app_mode == APP_MODE_TELEMETRY) {
1678 printf(" empty-poll cannot be enabled as telemetry mode is enabled\n");
1681 app_mode = APP_MODE_EMPTY_POLL;
1682 ret = parse_ep_config(optarg);
1685 printf("invalid empty poll config\n");
1686 print_usage(prgname);
1689 printf("empty-poll is enabled\n");
1692 if (!strncmp(lgopts[option_index].name,
1693 CMD_LINE_OPT_TELEMETRY,
1694 sizeof(CMD_LINE_OPT_TELEMETRY))) {
1695 if (app_mode == APP_MODE_EMPTY_POLL) {
1696 printf("telemetry mode cannot be enabled as empty poll mode is enabled\n");
1699 app_mode = APP_MODE_TELEMETRY;
1700 printf("telemetry mode is enabled\n");
1703 if (!strncmp(lgopts[option_index].name,
1704 "enable-jumbo", 12)) {
1705 struct option lenopts =
1706 {"max-pkt-len", required_argument, \
1709 printf("jumbo frame is enabled \n");
1710 port_conf.rxmode.offloads |=
1711 DEV_RX_OFFLOAD_JUMBO_FRAME;
1712 port_conf.txmode.offloads |=
1713 DEV_TX_OFFLOAD_MULTI_SEGS;
1716 * if no max-pkt-len set, use the default value
1719 if (0 == getopt_long(argc, argvopt, "",
1720 &lenopts, &option_index)) {
1721 ret = parse_max_pkt_len(optarg);
1723 (ret > MAX_JUMBO_PKT_LEN)){
1724 printf("invalid packet "
1726 print_usage(prgname);
1729 port_conf.rxmode.max_rx_pkt_len = ret;
1731 printf("set jumbo frame "
1732 "max packet length to %u\n",
1733 (unsigned int)port_conf.rxmode.max_rx_pkt_len);
1736 if (!strncmp(lgopts[option_index].name,
1737 CMD_LINE_OPT_PARSE_PTYPE,
1738 sizeof(CMD_LINE_OPT_PARSE_PTYPE))) {
1739 printf("soft parse-ptype is enabled\n");
1746 print_usage(prgname);
1752 argv[optind-1] = prgname;
1755 optind = 1; /* reset getopt lib */
1760 print_ethaddr(const char *name, const struct rte_ether_addr *eth_addr)
1762 char buf[RTE_ETHER_ADDR_FMT_SIZE];
1763 rte_ether_format_addr(buf, RTE_ETHER_ADDR_FMT_SIZE, eth_addr);
1764 printf("%s%s", name, buf);
1767 #if (APP_LOOKUP_METHOD == APP_LOOKUP_EXACT_MATCH)
1769 setup_hash(int socketid)
1771 struct rte_hash_parameters ipv4_l3fwd_hash_params = {
1773 .entries = L3FWD_HASH_ENTRIES,
1774 .key_len = sizeof(struct ipv4_5tuple),
1775 .hash_func = DEFAULT_HASH_FUNC,
1776 .hash_func_init_val = 0,
1779 struct rte_hash_parameters ipv6_l3fwd_hash_params = {
1781 .entries = L3FWD_HASH_ENTRIES,
1782 .key_len = sizeof(struct ipv6_5tuple),
1783 .hash_func = DEFAULT_HASH_FUNC,
1784 .hash_func_init_val = 0,
1791 /* create ipv4 hash */
1792 snprintf(s, sizeof(s), "ipv4_l3fwd_hash_%d", socketid);
1793 ipv4_l3fwd_hash_params.name = s;
1794 ipv4_l3fwd_hash_params.socket_id = socketid;
1795 ipv4_l3fwd_lookup_struct[socketid] =
1796 rte_hash_create(&ipv4_l3fwd_hash_params);
1797 if (ipv4_l3fwd_lookup_struct[socketid] == NULL)
1798 rte_exit(EXIT_FAILURE, "Unable to create the l3fwd hash on "
1799 "socket %d\n", socketid);
1801 /* create ipv6 hash */
1802 snprintf(s, sizeof(s), "ipv6_l3fwd_hash_%d", socketid);
1803 ipv6_l3fwd_hash_params.name = s;
1804 ipv6_l3fwd_hash_params.socket_id = socketid;
1805 ipv6_l3fwd_lookup_struct[socketid] =
1806 rte_hash_create(&ipv6_l3fwd_hash_params);
1807 if (ipv6_l3fwd_lookup_struct[socketid] == NULL)
1808 rte_exit(EXIT_FAILURE, "Unable to create the l3fwd hash on "
1809 "socket %d\n", socketid);
1812 /* populate the ipv4 hash */
1813 for (i = 0; i < RTE_DIM(ipv4_l3fwd_route_array); i++) {
1814 ret = rte_hash_add_key (ipv4_l3fwd_lookup_struct[socketid],
1815 (void *) &ipv4_l3fwd_route_array[i].key);
1817 rte_exit(EXIT_FAILURE, "Unable to add entry %u to the"
1818 "l3fwd hash on socket %d\n", i, socketid);
1820 ipv4_l3fwd_out_if[ret] = ipv4_l3fwd_route_array[i].if_out;
1821 printf("Hash: Adding key\n");
1822 print_ipv4_key(ipv4_l3fwd_route_array[i].key);
1825 /* populate the ipv6 hash */
1826 for (i = 0; i < RTE_DIM(ipv6_l3fwd_route_array); i++) {
1827 ret = rte_hash_add_key (ipv6_l3fwd_lookup_struct[socketid],
1828 (void *) &ipv6_l3fwd_route_array[i].key);
1830 rte_exit(EXIT_FAILURE, "Unable to add entry %u to the"
1831 "l3fwd hash on socket %d\n", i, socketid);
1833 ipv6_l3fwd_out_if[ret] = ipv6_l3fwd_route_array[i].if_out;
1834 printf("Hash: Adding key\n");
1835 print_ipv6_key(ipv6_l3fwd_route_array[i].key);
1840 #if (APP_LOOKUP_METHOD == APP_LOOKUP_LPM)
1842 setup_lpm(int socketid)
1848 /* create the LPM table */
1849 struct rte_lpm_config lpm_ipv4_config;
1851 lpm_ipv4_config.max_rules = IPV4_L3FWD_LPM_MAX_RULES;
1852 lpm_ipv4_config.number_tbl8s = 256;
1853 lpm_ipv4_config.flags = 0;
1855 snprintf(s, sizeof(s), "IPV4_L3FWD_LPM_%d", socketid);
1856 ipv4_l3fwd_lookup_struct[socketid] =
1857 rte_lpm_create(s, socketid, &lpm_ipv4_config);
1858 if (ipv4_l3fwd_lookup_struct[socketid] == NULL)
1859 rte_exit(EXIT_FAILURE, "Unable to create the l3fwd LPM table"
1860 " on socket %d\n", socketid);
1862 /* populate the LPM table */
1863 for (i = 0; i < RTE_DIM(ipv4_l3fwd_route_array); i++) {
1864 ret = rte_lpm_add(ipv4_l3fwd_lookup_struct[socketid],
1865 ipv4_l3fwd_route_array[i].ip,
1866 ipv4_l3fwd_route_array[i].depth,
1867 ipv4_l3fwd_route_array[i].if_out);
1870 rte_exit(EXIT_FAILURE, "Unable to add entry %u to the "
1871 "l3fwd LPM table on socket %d\n",
1875 printf("LPM: Adding route 0x%08x / %d (%d)\n",
1876 (unsigned)ipv4_l3fwd_route_array[i].ip,
1877 ipv4_l3fwd_route_array[i].depth,
1878 ipv4_l3fwd_route_array[i].if_out);
1884 init_mem(unsigned nb_mbuf)
1886 struct lcore_conf *qconf;
1891 for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
1892 if (rte_lcore_is_enabled(lcore_id) == 0)
1896 socketid = rte_lcore_to_socket_id(lcore_id);
1900 if (socketid >= NB_SOCKETS) {
1901 rte_exit(EXIT_FAILURE, "Socket %d of lcore %u is "
1902 "out of range %d\n", socketid,
1903 lcore_id, NB_SOCKETS);
1905 if (pktmbuf_pool[socketid] == NULL) {
1906 snprintf(s, sizeof(s), "mbuf_pool_%d", socketid);
1907 pktmbuf_pool[socketid] =
1908 rte_pktmbuf_pool_create(s, nb_mbuf,
1909 MEMPOOL_CACHE_SIZE, 0,
1910 RTE_MBUF_DEFAULT_BUF_SIZE,
1912 if (pktmbuf_pool[socketid] == NULL)
1913 rte_exit(EXIT_FAILURE,
1914 "Cannot init mbuf pool on socket %d\n",
1917 printf("Allocated mbuf pool on socket %d\n",
1920 #if (APP_LOOKUP_METHOD == APP_LOOKUP_LPM)
1921 setup_lpm(socketid);
1923 setup_hash(socketid);
1926 qconf = &lcore_conf[lcore_id];
1927 qconf->ipv4_lookup_struct = ipv4_l3fwd_lookup_struct[socketid];
1928 #if (APP_LOOKUP_METHOD == APP_LOOKUP_EXACT_MATCH)
1929 qconf->ipv6_lookup_struct = ipv6_l3fwd_lookup_struct[socketid];
1935 /* Check the link status of all ports in up to 9s, and print them finally */
1937 check_all_ports_link_status(uint32_t port_mask)
1939 #define CHECK_INTERVAL 100 /* 100ms */
1940 #define MAX_CHECK_TIME 90 /* 9s (90 * 100ms) in total */
1941 uint8_t count, all_ports_up, print_flag = 0;
1943 struct rte_eth_link link;
1946 printf("\nChecking link status");
1948 for (count = 0; count <= MAX_CHECK_TIME; count++) {
1950 RTE_ETH_FOREACH_DEV(portid) {
1951 if ((port_mask & (1 << portid)) == 0)
1953 memset(&link, 0, sizeof(link));
1954 ret = rte_eth_link_get_nowait(portid, &link);
1957 if (print_flag == 1)
1958 printf("Port %u link get failed: %s\n",
1959 portid, rte_strerror(-ret));
1962 /* print link status if flag set */
1963 if (print_flag == 1) {
1964 if (link.link_status)
1965 printf("Port %d Link Up - speed %u "
1966 "Mbps - %s\n", (uint8_t)portid,
1967 (unsigned)link.link_speed,
1968 (link.link_duplex == ETH_LINK_FULL_DUPLEX) ?
1969 ("full-duplex") : ("half-duplex"));
1971 printf("Port %d Link Down\n",
1975 /* clear all_ports_up flag if any link down */
1976 if (link.link_status == ETH_LINK_DOWN) {
1981 /* after finally printing all link status, get out */
1982 if (print_flag == 1)
1985 if (all_ports_up == 0) {
1988 rte_delay_ms(CHECK_INTERVAL);
1991 /* set the print_flag if all ports up or timeout */
1992 if (all_ports_up == 1 || count == (MAX_CHECK_TIME - 1)) {
1999 static int check_ptype(uint16_t portid)
2002 int ptype_l3_ipv4 = 0;
2003 #if (APP_LOOKUP_METHOD == APP_LOOKUP_EXACT_MATCH)
2004 int ptype_l3_ipv6 = 0;
2006 uint32_t ptype_mask = RTE_PTYPE_L3_MASK;
2008 ret = rte_eth_dev_get_supported_ptypes(portid, ptype_mask, NULL, 0);
2012 uint32_t ptypes[ret];
2014 ret = rte_eth_dev_get_supported_ptypes(portid, ptype_mask, ptypes, ret);
2015 for (i = 0; i < ret; ++i) {
2016 if (ptypes[i] & RTE_PTYPE_L3_IPV4)
2018 #if (APP_LOOKUP_METHOD == APP_LOOKUP_EXACT_MATCH)
2019 if (ptypes[i] & RTE_PTYPE_L3_IPV6)
2024 if (ptype_l3_ipv4 == 0)
2025 printf("port %d cannot parse RTE_PTYPE_L3_IPV4\n", portid);
2027 #if (APP_LOOKUP_METHOD == APP_LOOKUP_EXACT_MATCH)
2028 if (ptype_l3_ipv6 == 0)
2029 printf("port %d cannot parse RTE_PTYPE_L3_IPV6\n", portid);
2032 #if (APP_LOOKUP_METHOD == APP_LOOKUP_LPM)
2034 #else /* APP_LOOKUP_EXACT_MATCH */
2035 if (ptype_l3_ipv4 && ptype_l3_ipv6)
2044 init_power_library(void)
2046 enum power_management_env env;
2047 unsigned int lcore_id;
2050 RTE_LCORE_FOREACH(lcore_id) {
2051 /* init power management library */
2052 ret = rte_power_init(lcore_id);
2055 "Library initialization failed on core %u\n",
2059 /* we're not supporting the VM channel mode */
2060 env = rte_power_get_env();
2061 if (env != PM_ENV_ACPI_CPUFREQ &&
2062 env != PM_ENV_PSTATE_CPUFREQ) {
2064 "Only ACPI and PSTATE mode are supported\n");
2072 deinit_power_library(void)
2074 unsigned int lcore_id;
2077 RTE_LCORE_FOREACH(lcore_id) {
2078 /* deinit power management library */
2079 ret = rte_power_exit(lcore_id);
2082 "Library deinitialization failed on core %u\n",
2091 get_current_stat_values(uint64_t *values)
2093 unsigned int lcore_id = rte_lcore_id();
2094 struct lcore_conf *qconf;
2095 uint64_t app_eps = 0, app_fps = 0, app_br = 0;
2098 RTE_LCORE_FOREACH_SLAVE(lcore_id) {
2099 qconf = &lcore_conf[lcore_id];
2100 if (qconf->n_rx_queue == 0)
2103 rte_spinlock_lock(&stats[lcore_id].telemetry_lock);
2104 app_eps += stats[lcore_id].ep_nep[1];
2105 app_fps += stats[lcore_id].fp_nfp[1];
2106 app_br += stats[lcore_id].br;
2107 rte_spinlock_unlock(&stats[lcore_id].telemetry_lock);
2111 values[0] = app_eps/count;
2112 values[1] = app_fps/count;
2113 values[2] = app_br/count;
2115 memset(values, 0, sizeof(uint64_t) * NUM_TELSTATS);
2120 update_telemetry(__rte_unused struct rte_timer *tim,
2121 __rte_unused void *arg)
2124 uint64_t values[NUM_TELSTATS] = {0};
2126 get_current_stat_values(values);
2127 ret = rte_metrics_update_values(RTE_METRICS_GLOBAL, telstats_index,
2128 values, RTE_DIM(values));
2130 RTE_LOG(WARNING, POWER, "failed to update metrcis\n");
2134 handle_app_stats(const char *cmd __rte_unused,
2135 const char *params __rte_unused,
2136 struct rte_tel_data *d)
2138 uint64_t values[NUM_TELSTATS] = {0};
2141 rte_tel_data_start_dict(d);
2142 get_current_stat_values(values);
2143 for (i = 0; i < NUM_TELSTATS; i++)
2144 rte_tel_data_add_dict_u64(d, telstats_strings[i].name,
2150 telemetry_setup_timer(void)
2152 int lcore_id = rte_lcore_id();
2153 uint64_t hz = rte_get_timer_hz();
2156 ticks = hz / TELEMETRY_INTERVALS_PER_SEC;
2157 rte_timer_reset_sync(&telemetry_timer,
2165 empty_poll_setup_timer(void)
2167 int lcore_id = rte_lcore_id();
2168 uint64_t hz = rte_get_timer_hz();
2170 struct ep_params *ep_ptr = ep_params;
2172 ep_ptr->interval_ticks = hz / INTERVALS_PER_SECOND;
2174 rte_timer_reset_sync(&ep_ptr->timer0,
2175 ep_ptr->interval_ticks,
2178 rte_empty_poll_detection,
2183 launch_timer(unsigned int lcore_id)
2185 int64_t prev_tsc = 0, cur_tsc, diff_tsc, cycles_10ms;
2187 RTE_SET_USED(lcore_id);
2190 if (rte_get_master_lcore() != lcore_id) {
2191 rte_panic("timer on lcore:%d which is not master core:%d\n",
2193 rte_get_master_lcore());
2196 RTE_LOG(INFO, POWER, "Bring up the Timer\n");
2198 if (app_mode == APP_MODE_EMPTY_POLL)
2199 empty_poll_setup_timer();
2201 telemetry_setup_timer();
2203 cycles_10ms = rte_get_timer_hz() / 100;
2205 while (!is_done()) {
2206 cur_tsc = rte_rdtsc();
2207 diff_tsc = cur_tsc - prev_tsc;
2208 if (diff_tsc > cycles_10ms) {
2211 cycles_10ms = rte_get_timer_hz() / 100;
2215 RTE_LOG(INFO, POWER, "Timer_subsystem is done\n");
2222 main(int argc, char **argv)
2224 struct lcore_conf *qconf;
2225 struct rte_eth_dev_info dev_info;
2226 struct rte_eth_txconf *txconf;
2232 uint32_t n_tx_queue, nb_lcores;
2233 uint32_t dev_rxq_num, dev_txq_num;
2234 uint8_t nb_rx_queue, queue, socketid;
2236 const char *ptr_strings[NUM_TELSTATS];
2238 /* catch SIGINT and restore cpufreq governor to ondemand */
2239 signal(SIGINT, signal_exit_now);
2242 ret = rte_eal_init(argc, argv);
2244 rte_exit(EXIT_FAILURE, "Invalid EAL parameters\n");
2248 /* init RTE timer library to be used late */
2249 rte_timer_subsystem_init();
2251 /* parse application arguments (after the EAL ones) */
2252 ret = parse_args(argc, argv);
2254 rte_exit(EXIT_FAILURE, "Invalid L3FWD parameters\n");
2256 if (app_mode != APP_MODE_TELEMETRY && init_power_library())
2257 rte_exit(EXIT_FAILURE, "init_power_library failed\n");
2259 if (update_lcore_params() < 0)
2260 rte_exit(EXIT_FAILURE, "update_lcore_params failed\n");
2262 if (check_lcore_params() < 0)
2263 rte_exit(EXIT_FAILURE, "check_lcore_params failed\n");
2265 ret = init_lcore_rx_queues();
2267 rte_exit(EXIT_FAILURE, "init_lcore_rx_queues failed\n");
2269 nb_ports = rte_eth_dev_count_avail();
2271 if (check_port_config() < 0)
2272 rte_exit(EXIT_FAILURE, "check_port_config failed\n");
2274 nb_lcores = rte_lcore_count();
2276 /* initialize all ports */
2277 RTE_ETH_FOREACH_DEV(portid) {
2278 struct rte_eth_conf local_port_conf = port_conf;
2279 /* not all app modes need interrupts */
2280 bool need_intr = app_mode == APP_MODE_LEGACY;
2282 /* skip ports that are not enabled */
2283 if ((enabled_port_mask & (1 << portid)) == 0) {
2284 printf("\nSkipping disabled port %d\n", portid);
2289 printf("Initializing port %d ... ", portid );
2292 ret = rte_eth_dev_info_get(portid, &dev_info);
2294 rte_exit(EXIT_FAILURE,
2295 "Error during getting device (port %u) info: %s\n",
2296 portid, strerror(-ret));
2298 dev_rxq_num = dev_info.max_rx_queues;
2299 dev_txq_num = dev_info.max_tx_queues;
2301 nb_rx_queue = get_port_n_rx_queues(portid);
2302 if (nb_rx_queue > dev_rxq_num)
2303 rte_exit(EXIT_FAILURE,
2304 "Cannot configure not existed rxq: "
2305 "port=%d\n", portid);
2307 n_tx_queue = nb_lcores;
2308 if (n_tx_queue > dev_txq_num)
2309 n_tx_queue = dev_txq_num;
2310 printf("Creating queues: nb_rxq=%d nb_txq=%u... ",
2311 nb_rx_queue, (unsigned)n_tx_queue );
2312 /* If number of Rx queue is 0, no need to enable Rx interrupt */
2313 if (nb_rx_queue == 0)
2317 local_port_conf.intr_conf.rxq = 1;
2319 ret = rte_eth_dev_info_get(portid, &dev_info);
2321 rte_exit(EXIT_FAILURE,
2322 "Error during getting device (port %u) info: %s\n",
2323 portid, strerror(-ret));
2325 if (dev_info.tx_offload_capa & DEV_TX_OFFLOAD_MBUF_FAST_FREE)
2326 local_port_conf.txmode.offloads |=
2327 DEV_TX_OFFLOAD_MBUF_FAST_FREE;
2329 local_port_conf.rx_adv_conf.rss_conf.rss_hf &=
2330 dev_info.flow_type_rss_offloads;
2331 if (local_port_conf.rx_adv_conf.rss_conf.rss_hf !=
2332 port_conf.rx_adv_conf.rss_conf.rss_hf) {
2333 printf("Port %u modified RSS hash function based on hardware support,"
2334 "requested:%#"PRIx64" configured:%#"PRIx64"\n",
2336 port_conf.rx_adv_conf.rss_conf.rss_hf,
2337 local_port_conf.rx_adv_conf.rss_conf.rss_hf);
2340 ret = rte_eth_dev_configure(portid, nb_rx_queue,
2341 (uint16_t)n_tx_queue, &local_port_conf);
2343 rte_exit(EXIT_FAILURE, "Cannot configure device: "
2344 "err=%d, port=%d\n", ret, portid);
2346 ret = rte_eth_dev_adjust_nb_rx_tx_desc(portid, &nb_rxd,
2349 rte_exit(EXIT_FAILURE,
2350 "Cannot adjust number of descriptors: err=%d, port=%d\n",
2353 ret = rte_eth_macaddr_get(portid, &ports_eth_addr[portid]);
2355 rte_exit(EXIT_FAILURE,
2356 "Cannot get MAC address: err=%d, port=%d\n",
2359 print_ethaddr(" Address:", &ports_eth_addr[portid]);
2363 ret = init_mem(NB_MBUF);
2365 rte_exit(EXIT_FAILURE, "init_mem failed\n");
2367 for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
2368 if (rte_lcore_is_enabled(lcore_id) == 0)
2371 /* Initialize TX buffers */
2372 qconf = &lcore_conf[lcore_id];
2373 qconf->tx_buffer[portid] = rte_zmalloc_socket("tx_buffer",
2374 RTE_ETH_TX_BUFFER_SIZE(MAX_PKT_BURST), 0,
2375 rte_eth_dev_socket_id(portid));
2376 if (qconf->tx_buffer[portid] == NULL)
2377 rte_exit(EXIT_FAILURE, "Can't allocate tx buffer for port %u\n",
2380 rte_eth_tx_buffer_init(qconf->tx_buffer[portid], MAX_PKT_BURST);
2383 /* init one TX queue per couple (lcore,port) */
2385 for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
2386 if (rte_lcore_is_enabled(lcore_id) == 0)
2389 if (queueid >= dev_txq_num)
2394 (uint8_t)rte_lcore_to_socket_id(lcore_id);
2398 printf("txq=%u,%d,%d ", lcore_id, queueid, socketid);
2401 txconf = &dev_info.default_txconf;
2402 txconf->offloads = local_port_conf.txmode.offloads;
2403 ret = rte_eth_tx_queue_setup(portid, queueid, nb_txd,
2406 rte_exit(EXIT_FAILURE,
2407 "rte_eth_tx_queue_setup: err=%d, "
2408 "port=%d\n", ret, portid);
2410 qconf = &lcore_conf[lcore_id];
2411 qconf->tx_queue_id[portid] = queueid;
2414 qconf->tx_port_id[qconf->n_tx_port] = portid;
2420 for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
2421 if (rte_lcore_is_enabled(lcore_id) == 0)
2424 if (app_mode == APP_MODE_LEGACY) {
2425 /* init timer structures for each enabled lcore */
2426 rte_timer_init(&power_timers[lcore_id]);
2427 hz = rte_get_timer_hz();
2428 rte_timer_reset(&power_timers[lcore_id],
2429 hz/TIMER_NUMBER_PER_SECOND,
2431 power_timer_cb, NULL);
2433 qconf = &lcore_conf[lcore_id];
2434 printf("\nInitializing rx queues on lcore %u ... ", lcore_id );
2436 /* init RX queues */
2437 for(queue = 0; queue < qconf->n_rx_queue; ++queue) {
2438 struct rte_eth_rxconf rxq_conf;
2440 portid = qconf->rx_queue_list[queue].port_id;
2441 queueid = qconf->rx_queue_list[queue].queue_id;
2445 (uint8_t)rte_lcore_to_socket_id(lcore_id);
2449 printf("rxq=%d,%d,%d ", portid, queueid, socketid);
2452 ret = rte_eth_dev_info_get(portid, &dev_info);
2454 rte_exit(EXIT_FAILURE,
2455 "Error during getting device (port %u) info: %s\n",
2456 portid, strerror(-ret));
2458 rxq_conf = dev_info.default_rxconf;
2459 rxq_conf.offloads = port_conf.rxmode.offloads;
2460 ret = rte_eth_rx_queue_setup(portid, queueid, nb_rxd,
2461 socketid, &rxq_conf,
2462 pktmbuf_pool[socketid]);
2464 rte_exit(EXIT_FAILURE,
2465 "rte_eth_rx_queue_setup: err=%d, "
2466 "port=%d\n", ret, portid);
2469 if (add_cb_parse_ptype(portid, queueid) < 0)
2470 rte_exit(EXIT_FAILURE,
2471 "Fail to add ptype cb\n");
2472 } else if (!check_ptype(portid))
2473 rte_exit(EXIT_FAILURE,
2474 "PMD can not provide needed ptypes\n");
2481 RTE_ETH_FOREACH_DEV(portid) {
2482 if ((enabled_port_mask & (1 << portid)) == 0) {
2486 ret = rte_eth_dev_start(portid);
2488 rte_exit(EXIT_FAILURE, "rte_eth_dev_start: err=%d, "
2489 "port=%d\n", ret, portid);
2491 * If enabled, put device in promiscuous mode.
2492 * This allows IO forwarding mode to forward packets
2493 * to itself through 2 cross-connected ports of the
2496 if (promiscuous_on) {
2497 ret = rte_eth_promiscuous_enable(portid);
2499 rte_exit(EXIT_FAILURE,
2500 "rte_eth_promiscuous_enable: err=%s, port=%u\n",
2501 rte_strerror(-ret), portid);
2503 /* initialize spinlock for each port */
2504 rte_spinlock_init(&(locks[portid]));
2507 check_all_ports_link_status(enabled_port_mask);
2509 if (app_mode == APP_MODE_EMPTY_POLL) {
2511 if (empty_poll_train) {
2512 policy.state = TRAINING;
2514 policy.state = MED_NORMAL;
2515 policy.med_base_edpi = ep_med_edpi;
2516 policy.hgh_base_edpi = ep_hgh_edpi;
2519 ret = rte_power_empty_poll_stat_init(&ep_params,
2523 rte_exit(EXIT_FAILURE, "empty poll init failed");
2527 /* launch per-lcore init on every lcore */
2528 if (app_mode == APP_MODE_LEGACY) {
2529 rte_eal_mp_remote_launch(main_loop, NULL, CALL_MASTER);
2530 } else if (app_mode == APP_MODE_EMPTY_POLL) {
2531 empty_poll_stop = false;
2532 rte_eal_mp_remote_launch(main_empty_poll_loop, NULL,
2537 /* Init metrics library */
2538 rte_metrics_init(rte_socket_id());
2539 /** Register stats with metrics library */
2540 for (i = 0; i < NUM_TELSTATS; i++)
2541 ptr_strings[i] = telstats_strings[i].name;
2543 ret = rte_metrics_reg_names(ptr_strings, NUM_TELSTATS);
2545 telstats_index = ret;
2547 rte_exit(EXIT_FAILURE, "failed to register metrics names");
2549 RTE_LCORE_FOREACH_SLAVE(lcore_id) {
2550 rte_spinlock_init(&stats[lcore_id].telemetry_lock);
2552 rte_timer_init(&telemetry_timer);
2553 rte_telemetry_register_cmd("/l3fwd-power/stats",
2555 "Returns global power stats. Parameters: None");
2556 rte_eal_mp_remote_launch(main_telemetry_loop, NULL,
2560 if (app_mode == APP_MODE_EMPTY_POLL || app_mode == APP_MODE_TELEMETRY)
2561 launch_timer(rte_lcore_id());
2563 RTE_LCORE_FOREACH_SLAVE(lcore_id) {
2564 if (rte_eal_wait_lcore(lcore_id) < 0)
2568 RTE_ETH_FOREACH_DEV(portid)
2570 if ((enabled_port_mask & (1 << portid)) == 0)
2573 rte_eth_dev_stop(portid);
2574 rte_eth_dev_close(portid);
2577 if (app_mode == APP_MODE_EMPTY_POLL)
2578 rte_power_empty_poll_stat_free();
2580 if (app_mode != APP_MODE_TELEMETRY && deinit_power_library())
2581 rte_exit(EXIT_FAILURE, "deinit_power_library failed\n");
2583 if (rte_eal_cleanup() < 0)
2584 RTE_LOG(ERR, L3FWD_POWER, "EAL cleanup failed\n");