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,
265 static struct rte_mempool * pktmbuf_pool[NB_SOCKETS];
268 #if (APP_LOOKUP_METHOD == APP_LOOKUP_EXACT_MATCH)
271 #include <rte_hash_crc.h>
272 #define DEFAULT_HASH_FUNC rte_hash_crc
274 #include <rte_jhash.h>
275 #define DEFAULT_HASH_FUNC rte_jhash
287 uint8_t ip_dst[IPV6_ADDR_LEN];
288 uint8_t ip_src[IPV6_ADDR_LEN];
294 struct ipv4_l3fwd_route {
295 struct ipv4_5tuple key;
299 struct ipv6_l3fwd_route {
300 struct ipv6_5tuple key;
304 static struct ipv4_l3fwd_route ipv4_l3fwd_route_array[] = {
305 {{RTE_IPV4(100,10,0,1), RTE_IPV4(200,10,0,1), 101, 11, IPPROTO_TCP}, 0},
306 {{RTE_IPV4(100,20,0,2), RTE_IPV4(200,20,0,2), 102, 12, IPPROTO_TCP}, 1},
307 {{RTE_IPV4(100,30,0,3), RTE_IPV4(200,30,0,3), 103, 13, IPPROTO_TCP}, 2},
308 {{RTE_IPV4(100,40,0,4), RTE_IPV4(200,40,0,4), 104, 14, IPPROTO_TCP}, 3},
311 static struct ipv6_l3fwd_route ipv6_l3fwd_route_array[] = {
314 {0xfe, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
315 0x02, 0x1b, 0x21, 0xff, 0xfe, 0x91, 0x38, 0x05},
316 {0xfe, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
317 0x02, 0x1e, 0x67, 0xff, 0xfe, 0x0d, 0xb6, 0x0a},
323 typedef struct rte_hash lookup_struct_t;
324 static lookup_struct_t *ipv4_l3fwd_lookup_struct[NB_SOCKETS];
325 static lookup_struct_t *ipv6_l3fwd_lookup_struct[NB_SOCKETS];
327 #define L3FWD_HASH_ENTRIES 1024
329 static uint16_t ipv4_l3fwd_out_if[L3FWD_HASH_ENTRIES] __rte_cache_aligned;
330 static uint16_t ipv6_l3fwd_out_if[L3FWD_HASH_ENTRIES] __rte_cache_aligned;
333 #if (APP_LOOKUP_METHOD == APP_LOOKUP_LPM)
334 struct ipv4_l3fwd_route {
340 static struct ipv4_l3fwd_route ipv4_l3fwd_route_array[] = {
341 {RTE_IPV4(1,1,1,0), 24, 0},
342 {RTE_IPV4(2,1,1,0), 24, 1},
343 {RTE_IPV4(3,1,1,0), 24, 2},
344 {RTE_IPV4(4,1,1,0), 24, 3},
345 {RTE_IPV4(5,1,1,0), 24, 4},
346 {RTE_IPV4(6,1,1,0), 24, 5},
347 {RTE_IPV4(7,1,1,0), 24, 6},
348 {RTE_IPV4(8,1,1,0), 24, 7},
351 #define IPV4_L3FWD_LPM_MAX_RULES 1024
353 typedef struct rte_lpm lookup_struct_t;
354 static lookup_struct_t *ipv4_l3fwd_lookup_struct[NB_SOCKETS];
359 struct lcore_rx_queue rx_queue_list[MAX_RX_QUEUE_PER_LCORE];
361 uint16_t tx_port_id[RTE_MAX_ETHPORTS];
362 uint16_t tx_queue_id[RTE_MAX_ETHPORTS];
363 struct rte_eth_dev_tx_buffer *tx_buffer[RTE_MAX_ETHPORTS];
364 lookup_struct_t * ipv4_lookup_struct;
365 lookup_struct_t * ipv6_lookup_struct;
366 } __rte_cache_aligned;
369 /* total sleep time in ms since last frequency scaling down */
371 /* number of long sleep recently */
372 uint32_t nb_long_sleep;
373 /* freq. scaling up trend */
375 /* total packet processed recently */
376 uint64_t nb_rx_processed;
377 /* total iterations looped recently */
378 uint64_t nb_iteration_looped;
380 * Represents empty and non empty polls
381 * of rte_eth_rx_burst();
382 * ep_nep[0] holds non empty polls
383 * i.e. 0 < nb_rx <= MAX_BURST
384 * ep_nep[1] holds empty polls.
389 * Represents full and empty+partial
390 * polls of rte_eth_rx_burst();
391 * ep_nep[0] holds empty+partial polls.
392 * i.e. 0 <= nb_rx < MAX_BURST
393 * ep_nep[1] holds full polls
394 * i.e. nb_rx == MAX_BURST
398 rte_spinlock_t telemetry_lock;
399 } __rte_cache_aligned;
401 static struct lcore_conf lcore_conf[RTE_MAX_LCORE] __rte_cache_aligned;
402 static struct lcore_stats stats[RTE_MAX_LCORE] __rte_cache_aligned;
403 static struct rte_timer power_timers[RTE_MAX_LCORE];
405 static inline uint32_t power_idle_heuristic(uint32_t zero_rx_packet_count);
406 static inline enum freq_scale_hint_t power_freq_scaleup_heuristic( \
407 unsigned int lcore_id, uint16_t port_id, uint16_t queue_id);
411 * These defaults are using the max frequency index (1), a medium index (9)
412 * and a typical low frequency index (14). These can be adjusted to use
413 * different indexes using the relevant command line parameters.
415 static uint8_t freq_tlb[] = {14, 9, 1};
417 static int is_done(void)
422 /* exit signal handler */
424 signal_exit_now(int sigtype)
427 if (sigtype == SIGINT)
432 /* Freqency scale down timer callback */
434 power_timer_cb(__rte_unused struct rte_timer *tim,
435 __rte_unused void *arg)
438 float sleep_time_ratio;
439 unsigned lcore_id = rte_lcore_id();
441 /* accumulate total execution time in us when callback is invoked */
442 sleep_time_ratio = (float)(stats[lcore_id].sleep_time) /
443 (float)SCALING_PERIOD;
445 * check whether need to scale down frequency a step if it sleep a lot.
447 if (sleep_time_ratio >= SCALING_DOWN_TIME_RATIO_THRESHOLD) {
448 if (rte_power_freq_down)
449 rte_power_freq_down(lcore_id);
451 else if ( (unsigned)(stats[lcore_id].nb_rx_processed /
452 stats[lcore_id].nb_iteration_looped) < MAX_PKT_BURST) {
454 * scale down a step if average packet per iteration less
457 if (rte_power_freq_down)
458 rte_power_freq_down(lcore_id);
462 * initialize another timer according to current frequency to ensure
463 * timer interval is relatively fixed.
465 hz = rte_get_timer_hz();
466 rte_timer_reset(&power_timers[lcore_id], hz/TIMER_NUMBER_PER_SECOND,
467 SINGLE, lcore_id, power_timer_cb, NULL);
469 stats[lcore_id].nb_rx_processed = 0;
470 stats[lcore_id].nb_iteration_looped = 0;
472 stats[lcore_id].sleep_time = 0;
475 /* Enqueue a single packet, and send burst if queue is filled */
477 send_single_packet(struct rte_mbuf *m, uint16_t port)
480 struct lcore_conf *qconf;
482 lcore_id = rte_lcore_id();
483 qconf = &lcore_conf[lcore_id];
485 rte_eth_tx_buffer(port, qconf->tx_queue_id[port],
486 qconf->tx_buffer[port], m);
491 #ifdef DO_RFC_1812_CHECKS
493 is_valid_ipv4_pkt(struct rte_ipv4_hdr *pkt, uint32_t link_len)
495 /* From http://www.rfc-editor.org/rfc/rfc1812.txt section 5.2.2 */
497 * 1. The packet length reported by the Link Layer must be large
498 * enough to hold the minimum length legal IP datagram (20 bytes).
500 if (link_len < sizeof(struct rte_ipv4_hdr))
503 /* 2. The IP checksum must be correct. */
504 /* this is checked in H/W */
507 * 3. The IP version number must be 4. If the version number is not 4
508 * then the packet may be another version of IP, such as IPng or
511 if (((pkt->version_ihl) >> 4) != 4)
514 * 4. The IP header length field must be large enough to hold the
515 * minimum length legal IP datagram (20 bytes = 5 words).
517 if ((pkt->version_ihl & 0xf) < 5)
521 * 5. The IP total length field must be large enough to hold the IP
522 * datagram header, whose length is specified in the IP header length
525 if (rte_cpu_to_be_16(pkt->total_length) < sizeof(struct rte_ipv4_hdr))
532 #if (APP_LOOKUP_METHOD == APP_LOOKUP_EXACT_MATCH)
534 print_ipv4_key(struct ipv4_5tuple key)
536 printf("IP dst = %08x, IP src = %08x, port dst = %d, port src = %d, "
537 "proto = %d\n", (unsigned)key.ip_dst, (unsigned)key.ip_src,
538 key.port_dst, key.port_src, key.proto);
541 print_ipv6_key(struct ipv6_5tuple key)
543 printf( "IP dst = " IPv6_BYTES_FMT ", IP src = " IPv6_BYTES_FMT ", "
544 "port dst = %d, port src = %d, proto = %d\n",
545 IPv6_BYTES(key.ip_dst), IPv6_BYTES(key.ip_src),
546 key.port_dst, key.port_src, key.proto);
549 static inline uint16_t
550 get_ipv4_dst_port(struct rte_ipv4_hdr *ipv4_hdr, uint16_t portid,
551 lookup_struct_t * ipv4_l3fwd_lookup_struct)
553 struct ipv4_5tuple key;
554 struct rte_tcp_hdr *tcp;
555 struct rte_udp_hdr *udp;
558 key.ip_dst = rte_be_to_cpu_32(ipv4_hdr->dst_addr);
559 key.ip_src = rte_be_to_cpu_32(ipv4_hdr->src_addr);
560 key.proto = ipv4_hdr->next_proto_id;
562 switch (ipv4_hdr->next_proto_id) {
564 tcp = (struct rte_tcp_hdr *)((unsigned char *)ipv4_hdr +
565 sizeof(struct rte_ipv4_hdr));
566 key.port_dst = rte_be_to_cpu_16(tcp->dst_port);
567 key.port_src = rte_be_to_cpu_16(tcp->src_port);
571 udp = (struct rte_udp_hdr *)((unsigned char *)ipv4_hdr +
572 sizeof(struct rte_ipv4_hdr));
573 key.port_dst = rte_be_to_cpu_16(udp->dst_port);
574 key.port_src = rte_be_to_cpu_16(udp->src_port);
583 /* Find destination port */
584 ret = rte_hash_lookup(ipv4_l3fwd_lookup_struct, (const void *)&key);
585 return ((ret < 0) ? portid : ipv4_l3fwd_out_if[ret]);
588 static inline uint16_t
589 get_ipv6_dst_port(struct rte_ipv6_hdr *ipv6_hdr, uint16_t portid,
590 lookup_struct_t *ipv6_l3fwd_lookup_struct)
592 struct ipv6_5tuple key;
593 struct rte_tcp_hdr *tcp;
594 struct rte_udp_hdr *udp;
597 memcpy(key.ip_dst, ipv6_hdr->dst_addr, IPV6_ADDR_LEN);
598 memcpy(key.ip_src, ipv6_hdr->src_addr, IPV6_ADDR_LEN);
600 key.proto = ipv6_hdr->proto;
602 switch (ipv6_hdr->proto) {
604 tcp = (struct rte_tcp_hdr *)((unsigned char *) ipv6_hdr +
605 sizeof(struct rte_ipv6_hdr));
606 key.port_dst = rte_be_to_cpu_16(tcp->dst_port);
607 key.port_src = rte_be_to_cpu_16(tcp->src_port);
611 udp = (struct rte_udp_hdr *)((unsigned char *) ipv6_hdr +
612 sizeof(struct rte_ipv6_hdr));
613 key.port_dst = rte_be_to_cpu_16(udp->dst_port);
614 key.port_src = rte_be_to_cpu_16(udp->src_port);
623 /* Find destination port */
624 ret = rte_hash_lookup(ipv6_l3fwd_lookup_struct, (const void *)&key);
625 return ((ret < 0) ? portid : ipv6_l3fwd_out_if[ret]);
629 #if (APP_LOOKUP_METHOD == APP_LOOKUP_LPM)
630 static inline uint16_t
631 get_ipv4_dst_port(struct rte_ipv4_hdr *ipv4_hdr, uint16_t portid,
632 lookup_struct_t *ipv4_l3fwd_lookup_struct)
636 return ((rte_lpm_lookup(ipv4_l3fwd_lookup_struct,
637 rte_be_to_cpu_32(ipv4_hdr->dst_addr), &next_hop) == 0)?
643 parse_ptype_one(struct rte_mbuf *m)
645 struct rte_ether_hdr *eth_hdr;
646 uint32_t packet_type = RTE_PTYPE_UNKNOWN;
649 eth_hdr = rte_pktmbuf_mtod(m, struct rte_ether_hdr *);
650 ether_type = eth_hdr->ether_type;
651 if (ether_type == rte_cpu_to_be_16(RTE_ETHER_TYPE_IPV4))
652 packet_type |= RTE_PTYPE_L3_IPV4_EXT_UNKNOWN;
653 else if (ether_type == rte_cpu_to_be_16(RTE_ETHER_TYPE_IPV6))
654 packet_type |= RTE_PTYPE_L3_IPV6_EXT_UNKNOWN;
656 m->packet_type = packet_type;
660 cb_parse_ptype(uint16_t port __rte_unused, uint16_t queue __rte_unused,
661 struct rte_mbuf *pkts[], uint16_t nb_pkts,
662 uint16_t max_pkts __rte_unused,
663 void *user_param __rte_unused)
667 for (i = 0; i < nb_pkts; ++i)
668 parse_ptype_one(pkts[i]);
674 add_cb_parse_ptype(uint16_t portid, uint16_t queueid)
676 printf("Port %d: softly parse packet type info\n", portid);
677 if (rte_eth_add_rx_callback(portid, queueid, cb_parse_ptype, NULL))
680 printf("Failed to add rx callback: port=%d\n", portid);
685 l3fwd_simple_forward(struct rte_mbuf *m, uint16_t portid,
686 struct lcore_conf *qconf)
688 struct rte_ether_hdr *eth_hdr;
689 struct rte_ipv4_hdr *ipv4_hdr;
693 eth_hdr = rte_pktmbuf_mtod(m, struct rte_ether_hdr *);
695 if (RTE_ETH_IS_IPV4_HDR(m->packet_type)) {
696 /* Handle IPv4 headers.*/
698 rte_pktmbuf_mtod_offset(m, struct rte_ipv4_hdr *,
699 sizeof(struct rte_ether_hdr));
701 #ifdef DO_RFC_1812_CHECKS
702 /* Check to make sure the packet is valid (RFC1812) */
703 if (is_valid_ipv4_pkt(ipv4_hdr, m->pkt_len) < 0) {
709 dst_port = get_ipv4_dst_port(ipv4_hdr, portid,
710 qconf->ipv4_lookup_struct);
711 if (dst_port >= RTE_MAX_ETHPORTS ||
712 (enabled_port_mask & 1 << dst_port) == 0)
715 /* 02:00:00:00:00:xx */
716 d_addr_bytes = ð_hdr->d_addr.addr_bytes[0];
717 *((uint64_t *)d_addr_bytes) =
718 0x000000000002 + ((uint64_t)dst_port << 40);
720 #ifdef DO_RFC_1812_CHECKS
721 /* Update time to live and header checksum */
722 --(ipv4_hdr->time_to_live);
723 ++(ipv4_hdr->hdr_checksum);
727 rte_ether_addr_copy(&ports_eth_addr[dst_port],
730 send_single_packet(m, dst_port);
731 } else if (RTE_ETH_IS_IPV6_HDR(m->packet_type)) {
732 /* Handle IPv6 headers.*/
733 #if (APP_LOOKUP_METHOD == APP_LOOKUP_EXACT_MATCH)
734 struct rte_ipv6_hdr *ipv6_hdr;
737 rte_pktmbuf_mtod_offset(m, struct rte_ipv6_hdr *,
738 sizeof(struct rte_ether_hdr));
740 dst_port = get_ipv6_dst_port(ipv6_hdr, portid,
741 qconf->ipv6_lookup_struct);
743 if (dst_port >= RTE_MAX_ETHPORTS ||
744 (enabled_port_mask & 1 << dst_port) == 0)
747 /* 02:00:00:00:00:xx */
748 d_addr_bytes = ð_hdr->d_addr.addr_bytes[0];
749 *((uint64_t *)d_addr_bytes) =
750 0x000000000002 + ((uint64_t)dst_port << 40);
753 rte_ether_addr_copy(&ports_eth_addr[dst_port],
756 send_single_packet(m, dst_port);
758 /* We don't currently handle IPv6 packets in LPM mode. */
766 #define MINIMUM_SLEEP_TIME 1
767 #define SUSPEND_THRESHOLD 300
769 static inline uint32_t
770 power_idle_heuristic(uint32_t zero_rx_packet_count)
772 /* If zero count is less than 100, sleep 1us */
773 if (zero_rx_packet_count < SUSPEND_THRESHOLD)
774 return MINIMUM_SLEEP_TIME;
775 /* If zero count is less than 1000, sleep 100 us which is the
776 minimum latency switching from C3/C6 to C0
779 return SUSPEND_THRESHOLD;
782 static inline enum freq_scale_hint_t
783 power_freq_scaleup_heuristic(unsigned lcore_id,
787 uint32_t rxq_count = rte_eth_rx_queue_count(port_id, queue_id);
789 * HW Rx queue size is 128 by default, Rx burst read at maximum 32 entries
792 #define FREQ_GEAR1_RX_PACKET_THRESHOLD MAX_PKT_BURST
793 #define FREQ_GEAR2_RX_PACKET_THRESHOLD (MAX_PKT_BURST*2)
794 #define FREQ_GEAR3_RX_PACKET_THRESHOLD (MAX_PKT_BURST*3)
795 #define FREQ_UP_TREND1_ACC 1
796 #define FREQ_UP_TREND2_ACC 100
797 #define FREQ_UP_THRESHOLD 10000
799 if (likely(rxq_count > FREQ_GEAR3_RX_PACKET_THRESHOLD)) {
800 stats[lcore_id].trend = 0;
802 } else if (likely(rxq_count > FREQ_GEAR2_RX_PACKET_THRESHOLD))
803 stats[lcore_id].trend += FREQ_UP_TREND2_ACC;
804 else if (likely(rxq_count > FREQ_GEAR1_RX_PACKET_THRESHOLD))
805 stats[lcore_id].trend += FREQ_UP_TREND1_ACC;
807 if (likely(stats[lcore_id].trend > FREQ_UP_THRESHOLD)) {
808 stats[lcore_id].trend = 0;
816 * force polling thread sleep until one-shot rx interrupt triggers
825 sleep_until_rx_interrupt(int num)
827 struct rte_epoll_event event[num];
833 RTE_LOG(INFO, L3FWD_POWER,
834 "lcore %u sleeps until interrupt triggers\n",
837 n = rte_epoll_wait(RTE_EPOLL_PER_THREAD, event, num, -1);
838 for (i = 0; i < n; i++) {
839 data = event[i].epdata.data;
840 port_id = ((uintptr_t)data) >> CHAR_BIT;
841 queue_id = ((uintptr_t)data) &
842 RTE_LEN2MASK(CHAR_BIT, uint8_t);
843 RTE_LOG(INFO, L3FWD_POWER,
844 "lcore %u is waked up from rx interrupt on"
845 " port %d queue %d\n",
846 rte_lcore_id(), port_id, queue_id);
852 static void turn_on_off_intr(struct lcore_conf *qconf, bool on)
855 struct lcore_rx_queue *rx_queue;
859 for (i = 0; i < qconf->n_rx_queue; ++i) {
860 rx_queue = &(qconf->rx_queue_list[i]);
861 port_id = rx_queue->port_id;
862 queue_id = rx_queue->queue_id;
864 rte_spinlock_lock(&(locks[port_id]));
866 rte_eth_dev_rx_intr_enable(port_id, queue_id);
868 rte_eth_dev_rx_intr_disable(port_id, queue_id);
869 rte_spinlock_unlock(&(locks[port_id]));
873 static int event_register(struct lcore_conf *qconf)
875 struct lcore_rx_queue *rx_queue;
882 for (i = 0; i < qconf->n_rx_queue; ++i) {
883 rx_queue = &(qconf->rx_queue_list[i]);
884 portid = rx_queue->port_id;
885 queueid = rx_queue->queue_id;
886 data = portid << CHAR_BIT | queueid;
888 ret = rte_eth_dev_rx_intr_ctl_q(portid, queueid,
889 RTE_EPOLL_PER_THREAD,
891 (void *)((uintptr_t)data));
898 /* main processing loop */
900 main_telemetry_loop(__rte_unused void *dummy)
902 struct rte_mbuf *pkts_burst[MAX_PKT_BURST];
903 unsigned int lcore_id;
904 uint64_t prev_tsc, diff_tsc, cur_tsc, prev_tel_tsc;
908 struct lcore_conf *qconf;
909 struct lcore_rx_queue *rx_queue;
910 uint64_t ep_nep[2] = {0}, fp_nfp[2] = {0};
914 const uint64_t drain_tsc = (rte_get_tsc_hz() + US_PER_S - 1) /
915 US_PER_S * BURST_TX_DRAIN_US;
921 lcore_id = rte_lcore_id();
922 qconf = &lcore_conf[lcore_id];
924 if (qconf->n_rx_queue == 0) {
925 RTE_LOG(INFO, L3FWD_POWER, "lcore %u has nothing to do\n",
930 RTE_LOG(INFO, L3FWD_POWER, "entering main telemetry loop on lcore %u\n",
933 for (i = 0; i < qconf->n_rx_queue; i++) {
934 portid = qconf->rx_queue_list[i].port_id;
935 queueid = qconf->rx_queue_list[i].queue_id;
936 RTE_LOG(INFO, L3FWD_POWER, " -- lcoreid=%u portid=%u "
937 "rxqueueid=%hhu\n", lcore_id, portid, queueid);
942 cur_tsc = rte_rdtsc();
944 * TX burst queue drain
946 diff_tsc = cur_tsc - prev_tsc;
947 if (unlikely(diff_tsc > drain_tsc)) {
948 for (i = 0; i < qconf->n_tx_port; ++i) {
949 portid = qconf->tx_port_id[i];
950 rte_eth_tx_buffer_flush(portid,
951 qconf->tx_queue_id[portid],
952 qconf->tx_buffer[portid]);
958 * Read packet from RX queues
960 for (i = 0; i < qconf->n_rx_queue; ++i) {
961 rx_queue = &(qconf->rx_queue_list[i]);
962 portid = rx_queue->port_id;
963 queueid = rx_queue->queue_id;
965 nb_rx = rte_eth_rx_burst(portid, queueid, pkts_burst,
967 ep_nep[nb_rx == 0]++;
968 fp_nfp[nb_rx == MAX_PKT_BURST]++;
970 if (unlikely(nb_rx == 0))
973 /* Prefetch first packets */
974 for (j = 0; j < PREFETCH_OFFSET && j < nb_rx; j++) {
975 rte_prefetch0(rte_pktmbuf_mtod(
976 pkts_burst[j], void *));
979 /* Prefetch and forward already prefetched packets */
980 for (j = 0; j < (nb_rx - PREFETCH_OFFSET); j++) {
981 rte_prefetch0(rte_pktmbuf_mtod(pkts_burst[
982 j + PREFETCH_OFFSET], void *));
983 l3fwd_simple_forward(pkts_burst[j], portid,
987 /* Forward remaining prefetched packets */
988 for (; j < nb_rx; j++) {
989 l3fwd_simple_forward(pkts_burst[j], portid,
993 if (unlikely(poll_count >= DEFAULT_COUNT)) {
994 diff_tsc = cur_tsc - prev_tel_tsc;
995 if (diff_tsc >= MAX_CYCLES) {
997 } else if (diff_tsc > MIN_CYCLES &&
998 diff_tsc < MAX_CYCLES) {
999 br = (diff_tsc * 100) / MAX_CYCLES;
1004 prev_tel_tsc = cur_tsc;
1005 /* update stats for telemetry */
1006 rte_spinlock_lock(&stats[lcore_id].telemetry_lock);
1007 stats[lcore_id].ep_nep[0] = ep_nep[0];
1008 stats[lcore_id].ep_nep[1] = ep_nep[1];
1009 stats[lcore_id].fp_nfp[0] = fp_nfp[0];
1010 stats[lcore_id].fp_nfp[1] = fp_nfp[1];
1011 stats[lcore_id].br = br;
1012 rte_spinlock_unlock(&stats[lcore_id].telemetry_lock);
1018 /* main processing loop */
1020 main_empty_poll_loop(__rte_unused void *dummy)
1022 struct rte_mbuf *pkts_burst[MAX_PKT_BURST];
1023 unsigned int lcore_id;
1024 uint64_t prev_tsc, diff_tsc, cur_tsc;
1028 struct lcore_conf *qconf;
1029 struct lcore_rx_queue *rx_queue;
1031 const uint64_t drain_tsc =
1032 (rte_get_tsc_hz() + US_PER_S - 1) /
1033 US_PER_S * BURST_TX_DRAIN_US;
1037 lcore_id = rte_lcore_id();
1038 qconf = &lcore_conf[lcore_id];
1040 if (qconf->n_rx_queue == 0) {
1041 RTE_LOG(INFO, L3FWD_POWER, "lcore %u has nothing to do\n",
1046 for (i = 0; i < qconf->n_rx_queue; i++) {
1047 portid = qconf->rx_queue_list[i].port_id;
1048 queueid = qconf->rx_queue_list[i].queue_id;
1049 RTE_LOG(INFO, L3FWD_POWER, " -- lcoreid=%u portid=%u "
1050 "rxqueueid=%hhu\n", lcore_id, portid, queueid);
1053 while (!is_done()) {
1054 stats[lcore_id].nb_iteration_looped++;
1056 cur_tsc = rte_rdtsc();
1058 * TX burst queue drain
1060 diff_tsc = cur_tsc - prev_tsc;
1061 if (unlikely(diff_tsc > drain_tsc)) {
1062 for (i = 0; i < qconf->n_tx_port; ++i) {
1063 portid = qconf->tx_port_id[i];
1064 rte_eth_tx_buffer_flush(portid,
1065 qconf->tx_queue_id[portid],
1066 qconf->tx_buffer[portid]);
1072 * Read packet from RX queues
1074 for (i = 0; i < qconf->n_rx_queue; ++i) {
1075 rx_queue = &(qconf->rx_queue_list[i]);
1076 rx_queue->idle_hint = 0;
1077 portid = rx_queue->port_id;
1078 queueid = rx_queue->queue_id;
1080 nb_rx = rte_eth_rx_burst(portid, queueid, pkts_burst,
1083 stats[lcore_id].nb_rx_processed += nb_rx;
1087 rte_power_empty_poll_stat_update(lcore_id);
1091 rte_power_poll_stat_update(lcore_id, nb_rx);
1095 /* Prefetch first packets */
1096 for (j = 0; j < PREFETCH_OFFSET && j < nb_rx; j++) {
1097 rte_prefetch0(rte_pktmbuf_mtod(
1098 pkts_burst[j], void *));
1101 /* Prefetch and forward already prefetched packets */
1102 for (j = 0; j < (nb_rx - PREFETCH_OFFSET); j++) {
1103 rte_prefetch0(rte_pktmbuf_mtod(pkts_burst[
1104 j + PREFETCH_OFFSET],
1106 l3fwd_simple_forward(pkts_burst[j], portid,
1110 /* Forward remaining prefetched packets */
1111 for (; j < nb_rx; j++) {
1112 l3fwd_simple_forward(pkts_burst[j], portid,
1122 /* main processing loop */
1124 main_loop(__rte_unused void *dummy)
1126 struct rte_mbuf *pkts_burst[MAX_PKT_BURST];
1128 uint64_t prev_tsc, diff_tsc, cur_tsc, tim_res_tsc, hz;
1129 uint64_t prev_tsc_power = 0, cur_tsc_power, diff_tsc_power;
1133 struct lcore_conf *qconf;
1134 struct lcore_rx_queue *rx_queue;
1135 enum freq_scale_hint_t lcore_scaleup_hint;
1136 uint32_t lcore_rx_idle_count = 0;
1137 uint32_t lcore_idle_hint = 0;
1140 const uint64_t drain_tsc = (rte_get_tsc_hz() + US_PER_S - 1) / US_PER_S * BURST_TX_DRAIN_US;
1143 hz = rte_get_timer_hz();
1144 tim_res_tsc = hz/TIMER_NUMBER_PER_SECOND;
1146 lcore_id = rte_lcore_id();
1147 qconf = &lcore_conf[lcore_id];
1149 if (qconf->n_rx_queue == 0) {
1150 RTE_LOG(INFO, L3FWD_POWER, "lcore %u has nothing to do\n", lcore_id);
1154 RTE_LOG(INFO, L3FWD_POWER, "entering main loop on lcore %u\n", lcore_id);
1156 for (i = 0; i < qconf->n_rx_queue; i++) {
1157 portid = qconf->rx_queue_list[i].port_id;
1158 queueid = qconf->rx_queue_list[i].queue_id;
1159 RTE_LOG(INFO, L3FWD_POWER, " -- lcoreid=%u portid=%u "
1160 "rxqueueid=%hhu\n", lcore_id, portid, queueid);
1163 /* add into event wait list */
1164 if (event_register(qconf) == 0)
1167 RTE_LOG(INFO, L3FWD_POWER, "RX interrupt won't enable.\n");
1169 while (!is_done()) {
1170 stats[lcore_id].nb_iteration_looped++;
1172 cur_tsc = rte_rdtsc();
1173 cur_tsc_power = cur_tsc;
1176 * TX burst queue drain
1178 diff_tsc = cur_tsc - prev_tsc;
1179 if (unlikely(diff_tsc > drain_tsc)) {
1180 for (i = 0; i < qconf->n_tx_port; ++i) {
1181 portid = qconf->tx_port_id[i];
1182 rte_eth_tx_buffer_flush(portid,
1183 qconf->tx_queue_id[portid],
1184 qconf->tx_buffer[portid]);
1189 diff_tsc_power = cur_tsc_power - prev_tsc_power;
1190 if (diff_tsc_power > tim_res_tsc) {
1192 prev_tsc_power = cur_tsc_power;
1197 * Read packet from RX queues
1199 lcore_scaleup_hint = FREQ_CURRENT;
1200 lcore_rx_idle_count = 0;
1201 for (i = 0; i < qconf->n_rx_queue; ++i) {
1202 rx_queue = &(qconf->rx_queue_list[i]);
1203 rx_queue->idle_hint = 0;
1204 portid = rx_queue->port_id;
1205 queueid = rx_queue->queue_id;
1207 nb_rx = rte_eth_rx_burst(portid, queueid, pkts_burst,
1210 stats[lcore_id].nb_rx_processed += nb_rx;
1211 if (unlikely(nb_rx == 0)) {
1213 * no packet received from rx queue, try to
1214 * sleep for a while forcing CPU enter deeper
1217 rx_queue->zero_rx_packet_count++;
1219 if (rx_queue->zero_rx_packet_count <=
1220 MIN_ZERO_POLL_COUNT)
1223 rx_queue->idle_hint = power_idle_heuristic(\
1224 rx_queue->zero_rx_packet_count);
1225 lcore_rx_idle_count++;
1227 rx_queue->zero_rx_packet_count = 0;
1230 * do not scale up frequency immediately as
1231 * user to kernel space communication is costly
1232 * which might impact packet I/O for received
1235 rx_queue->freq_up_hint =
1236 power_freq_scaleup_heuristic(lcore_id,
1240 /* Prefetch first packets */
1241 for (j = 0; j < PREFETCH_OFFSET && j < nb_rx; j++) {
1242 rte_prefetch0(rte_pktmbuf_mtod(
1243 pkts_burst[j], void *));
1246 /* Prefetch and forward already prefetched packets */
1247 for (j = 0; j < (nb_rx - PREFETCH_OFFSET); j++) {
1248 rte_prefetch0(rte_pktmbuf_mtod(pkts_burst[
1249 j + PREFETCH_OFFSET], void *));
1250 l3fwd_simple_forward(pkts_burst[j], portid,
1254 /* Forward remaining prefetched packets */
1255 for (; j < nb_rx; j++) {
1256 l3fwd_simple_forward(pkts_burst[j], portid,
1261 if (likely(lcore_rx_idle_count != qconf->n_rx_queue)) {
1262 for (i = 1, lcore_scaleup_hint =
1263 qconf->rx_queue_list[0].freq_up_hint;
1264 i < qconf->n_rx_queue; ++i) {
1265 rx_queue = &(qconf->rx_queue_list[i]);
1266 if (rx_queue->freq_up_hint >
1268 lcore_scaleup_hint =
1269 rx_queue->freq_up_hint;
1272 if (lcore_scaleup_hint == FREQ_HIGHEST) {
1273 if (rte_power_freq_max)
1274 rte_power_freq_max(lcore_id);
1275 } else if (lcore_scaleup_hint == FREQ_HIGHER) {
1276 if (rte_power_freq_up)
1277 rte_power_freq_up(lcore_id);
1281 * All Rx queues empty in recent consecutive polls,
1282 * sleep in a conservative manner, meaning sleep as
1285 for (i = 1, lcore_idle_hint =
1286 qconf->rx_queue_list[0].idle_hint;
1287 i < qconf->n_rx_queue; ++i) {
1288 rx_queue = &(qconf->rx_queue_list[i]);
1289 if (rx_queue->idle_hint < lcore_idle_hint)
1290 lcore_idle_hint = rx_queue->idle_hint;
1293 if (lcore_idle_hint < SUSPEND_THRESHOLD)
1295 * execute "pause" instruction to avoid context
1296 * switch which generally take hundred of
1297 * microseconds for short sleep.
1299 rte_delay_us(lcore_idle_hint);
1301 /* suspend until rx interrupt triggers */
1303 turn_on_off_intr(qconf, 1);
1304 sleep_until_rx_interrupt(
1306 turn_on_off_intr(qconf, 0);
1308 * start receiving packets immediately
1313 stats[lcore_id].sleep_time += lcore_idle_hint;
1321 check_lcore_params(void)
1323 uint8_t queue, lcore;
1327 for (i = 0; i < nb_lcore_params; ++i) {
1328 queue = lcore_params[i].queue_id;
1329 if (queue >= MAX_RX_QUEUE_PER_PORT) {
1330 printf("invalid queue number: %hhu\n", queue);
1333 lcore = lcore_params[i].lcore_id;
1334 if (!rte_lcore_is_enabled(lcore)) {
1335 printf("error: lcore %hhu is not enabled in lcore "
1339 if ((socketid = rte_lcore_to_socket_id(lcore) != 0) &&
1341 printf("warning: lcore %hhu is on socket %d with numa "
1342 "off\n", lcore, socketid);
1344 if (app_mode == APP_MODE_TELEMETRY && lcore == rte_lcore_id()) {
1345 printf("cannot enable master core %d in config for telemetry mode\n",
1354 check_port_config(void)
1359 for (i = 0; i < nb_lcore_params; ++i) {
1360 portid = lcore_params[i].port_id;
1361 if ((enabled_port_mask & (1 << portid)) == 0) {
1362 printf("port %u is not enabled in port mask\n",
1366 if (!rte_eth_dev_is_valid_port(portid)) {
1367 printf("port %u is not present on the board\n",
1376 get_port_n_rx_queues(const uint16_t port)
1381 for (i = 0; i < nb_lcore_params; ++i) {
1382 if (lcore_params[i].port_id == port &&
1383 lcore_params[i].queue_id > queue)
1384 queue = lcore_params[i].queue_id;
1386 return (uint8_t)(++queue);
1390 init_lcore_rx_queues(void)
1392 uint16_t i, nb_rx_queue;
1395 for (i = 0; i < nb_lcore_params; ++i) {
1396 lcore = lcore_params[i].lcore_id;
1397 nb_rx_queue = lcore_conf[lcore].n_rx_queue;
1398 if (nb_rx_queue >= MAX_RX_QUEUE_PER_LCORE) {
1399 printf("error: too many queues (%u) for lcore: %u\n",
1400 (unsigned)nb_rx_queue + 1, (unsigned)lcore);
1403 lcore_conf[lcore].rx_queue_list[nb_rx_queue].port_id =
1404 lcore_params[i].port_id;
1405 lcore_conf[lcore].rx_queue_list[nb_rx_queue].queue_id =
1406 lcore_params[i].queue_id;
1407 lcore_conf[lcore].n_rx_queue++;
1415 print_usage(const char *prgname)
1417 printf ("%s [EAL options] -- -p PORTMASK -P"
1418 " [--config (port,queue,lcore)[,(port,queue,lcore]]"
1419 " [--high-perf-cores CORELIST"
1420 " [--perf-config (port,queue,hi_perf,lcore_index)[,(port,queue,hi_perf,lcore_index]]"
1421 " [--enable-jumbo [--max-pkt-len PKTLEN]]\n"
1422 " -p PORTMASK: hexadecimal bitmask of ports to configure\n"
1423 " -P : enable promiscuous mode\n"
1424 " --config (port,queue,lcore): rx queues configuration\n"
1425 " --high-perf-cores CORELIST: list of high performance cores\n"
1426 " --perf-config: similar as config, cores specified as indices"
1427 " for bins containing high or regular performance cores\n"
1428 " --no-numa: optional, disable numa awareness\n"
1429 " --enable-jumbo: enable jumbo frame"
1430 " which max packet len is PKTLEN in decimal (64-9600)\n"
1431 " --parse-ptype: parse packet type by software\n"
1432 " --empty-poll: enable empty poll detection"
1433 " follow (training_flag, high_threshold, med_threshold)\n"
1434 " --telemetry: enable telemetry mode, to update"
1435 " empty polls, full polls, and core busyness to telemetry\n",
1439 static int parse_max_pkt_len(const char *pktlen)
1444 /* parse decimal string */
1445 len = strtoul(pktlen, &end, 10);
1446 if ((pktlen[0] == '\0') || (end == NULL) || (*end != '\0'))
1456 parse_portmask(const char *portmask)
1461 /* parse hexadecimal string */
1462 pm = strtoul(portmask, &end, 16);
1463 if ((portmask[0] == '\0') || (end == NULL) || (*end != '\0'))
1473 parse_config(const char *q_arg)
1476 const char *p, *p0 = q_arg;
1484 unsigned long int_fld[_NUM_FLD];
1485 char *str_fld[_NUM_FLD];
1489 nb_lcore_params = 0;
1491 while ((p = strchr(p0,'(')) != NULL) {
1493 if((p0 = strchr(p,')')) == NULL)
1497 if(size >= sizeof(s))
1500 snprintf(s, sizeof(s), "%.*s", size, p);
1501 if (rte_strsplit(s, sizeof(s), str_fld, _NUM_FLD, ',') !=
1504 for (i = 0; i < _NUM_FLD; i++){
1506 int_fld[i] = strtoul(str_fld[i], &end, 0);
1507 if (errno != 0 || end == str_fld[i] || int_fld[i] >
1511 if (nb_lcore_params >= MAX_LCORE_PARAMS) {
1512 printf("exceeded max number of lcore params: %hu\n",
1516 lcore_params_array[nb_lcore_params].port_id =
1517 (uint8_t)int_fld[FLD_PORT];
1518 lcore_params_array[nb_lcore_params].queue_id =
1519 (uint8_t)int_fld[FLD_QUEUE];
1520 lcore_params_array[nb_lcore_params].lcore_id =
1521 (uint8_t)int_fld[FLD_LCORE];
1524 lcore_params = lcore_params_array;
1529 parse_ep_config(const char *q_arg)
1532 const char *p = q_arg;
1542 ep_med_edpi = EMPTY_POLL_MED_THRESHOLD;
1543 ep_hgh_edpi = EMPTY_POLL_MED_THRESHOLD;
1545 strlcpy(s, p, sizeof(s));
1547 num_arg = rte_strsplit(s, sizeof(s), str_fld, 3, ',');
1549 empty_poll_train = false;
1556 training_flag = strtoul(str_fld[0], &end, 0);
1557 med_edpi = strtoul(str_fld[1], &end, 0);
1558 hgh_edpi = strtoul(str_fld[2], &end, 0);
1560 if (training_flag == 1)
1561 empty_poll_train = true;
1564 ep_med_edpi = med_edpi;
1567 ep_hgh_edpi = hgh_edpi;
1577 #define CMD_LINE_OPT_PARSE_PTYPE "parse-ptype"
1578 #define CMD_LINE_OPT_TELEMETRY "telemetry"
1580 /* Parse the argument given in the command line of the application */
1582 parse_args(int argc, char **argv)
1588 char *prgname = argv[0];
1589 static struct option lgopts[] = {
1590 {"config", 1, 0, 0},
1591 {"perf-config", 1, 0, 0},
1592 {"high-perf-cores", 1, 0, 0},
1593 {"no-numa", 0, 0, 0},
1594 {"enable-jumbo", 0, 0, 0},
1595 {"empty-poll", 1, 0, 0},
1596 {CMD_LINE_OPT_PARSE_PTYPE, 0, 0, 0},
1597 {CMD_LINE_OPT_TELEMETRY, 0, 0, 0},
1603 while ((opt = getopt_long(argc, argvopt, "p:l:m:h:P",
1604 lgopts, &option_index)) != EOF) {
1609 enabled_port_mask = parse_portmask(optarg);
1610 if (enabled_port_mask == 0) {
1611 printf("invalid portmask\n");
1612 print_usage(prgname);
1617 printf("Promiscuous mode selected\n");
1621 limit = parse_max_pkt_len(optarg);
1622 freq_tlb[LOW] = limit;
1625 limit = parse_max_pkt_len(optarg);
1626 freq_tlb[MED] = limit;
1629 limit = parse_max_pkt_len(optarg);
1630 freq_tlb[HGH] = limit;
1634 if (!strncmp(lgopts[option_index].name, "config", 6)) {
1635 ret = parse_config(optarg);
1637 printf("invalid config\n");
1638 print_usage(prgname);
1643 if (!strncmp(lgopts[option_index].name,
1644 "perf-config", 11)) {
1645 ret = parse_perf_config(optarg);
1647 printf("invalid perf-config\n");
1648 print_usage(prgname);
1653 if (!strncmp(lgopts[option_index].name,
1654 "high-perf-cores", 15)) {
1655 ret = parse_perf_core_list(optarg);
1657 printf("invalid high-perf-cores\n");
1658 print_usage(prgname);
1663 if (!strncmp(lgopts[option_index].name,
1665 printf("numa is disabled \n");
1669 if (!strncmp(lgopts[option_index].name,
1670 "empty-poll", 10)) {
1671 if (app_mode == APP_MODE_TELEMETRY) {
1672 printf(" empty-poll cannot be enabled as telemetry mode is enabled\n");
1675 app_mode = APP_MODE_EMPTY_POLL;
1676 ret = parse_ep_config(optarg);
1679 printf("invalid empty poll config\n");
1680 print_usage(prgname);
1683 printf("empty-poll is enabled\n");
1686 if (!strncmp(lgopts[option_index].name,
1687 CMD_LINE_OPT_TELEMETRY,
1688 sizeof(CMD_LINE_OPT_TELEMETRY))) {
1689 if (app_mode == APP_MODE_EMPTY_POLL) {
1690 printf("telemetry mode cannot be enabled as empty poll mode is enabled\n");
1693 app_mode = APP_MODE_TELEMETRY;
1694 printf("telemetry mode is enabled\n");
1697 if (!strncmp(lgopts[option_index].name,
1698 "enable-jumbo", 12)) {
1699 struct option lenopts =
1700 {"max-pkt-len", required_argument, \
1703 printf("jumbo frame is enabled \n");
1704 port_conf.rxmode.offloads |=
1705 DEV_RX_OFFLOAD_JUMBO_FRAME;
1706 port_conf.txmode.offloads |=
1707 DEV_TX_OFFLOAD_MULTI_SEGS;
1710 * if no max-pkt-len set, use the default value
1713 if (0 == getopt_long(argc, argvopt, "",
1714 &lenopts, &option_index)) {
1715 ret = parse_max_pkt_len(optarg);
1717 (ret > MAX_JUMBO_PKT_LEN)){
1718 printf("invalid packet "
1720 print_usage(prgname);
1723 port_conf.rxmode.max_rx_pkt_len = ret;
1725 printf("set jumbo frame "
1726 "max packet length to %u\n",
1727 (unsigned int)port_conf.rxmode.max_rx_pkt_len);
1730 if (!strncmp(lgopts[option_index].name,
1731 CMD_LINE_OPT_PARSE_PTYPE,
1732 sizeof(CMD_LINE_OPT_PARSE_PTYPE))) {
1733 printf("soft parse-ptype is enabled\n");
1740 print_usage(prgname);
1746 argv[optind-1] = prgname;
1749 optind = 1; /* reset getopt lib */
1754 print_ethaddr(const char *name, const struct rte_ether_addr *eth_addr)
1756 char buf[RTE_ETHER_ADDR_FMT_SIZE];
1757 rte_ether_format_addr(buf, RTE_ETHER_ADDR_FMT_SIZE, eth_addr);
1758 printf("%s%s", name, buf);
1761 #if (APP_LOOKUP_METHOD == APP_LOOKUP_EXACT_MATCH)
1763 setup_hash(int socketid)
1765 struct rte_hash_parameters ipv4_l3fwd_hash_params = {
1767 .entries = L3FWD_HASH_ENTRIES,
1768 .key_len = sizeof(struct ipv4_5tuple),
1769 .hash_func = DEFAULT_HASH_FUNC,
1770 .hash_func_init_val = 0,
1773 struct rte_hash_parameters ipv6_l3fwd_hash_params = {
1775 .entries = L3FWD_HASH_ENTRIES,
1776 .key_len = sizeof(struct ipv6_5tuple),
1777 .hash_func = DEFAULT_HASH_FUNC,
1778 .hash_func_init_val = 0,
1785 /* create ipv4 hash */
1786 snprintf(s, sizeof(s), "ipv4_l3fwd_hash_%d", socketid);
1787 ipv4_l3fwd_hash_params.name = s;
1788 ipv4_l3fwd_hash_params.socket_id = socketid;
1789 ipv4_l3fwd_lookup_struct[socketid] =
1790 rte_hash_create(&ipv4_l3fwd_hash_params);
1791 if (ipv4_l3fwd_lookup_struct[socketid] == NULL)
1792 rte_exit(EXIT_FAILURE, "Unable to create the l3fwd hash on "
1793 "socket %d\n", socketid);
1795 /* create ipv6 hash */
1796 snprintf(s, sizeof(s), "ipv6_l3fwd_hash_%d", socketid);
1797 ipv6_l3fwd_hash_params.name = s;
1798 ipv6_l3fwd_hash_params.socket_id = socketid;
1799 ipv6_l3fwd_lookup_struct[socketid] =
1800 rte_hash_create(&ipv6_l3fwd_hash_params);
1801 if (ipv6_l3fwd_lookup_struct[socketid] == NULL)
1802 rte_exit(EXIT_FAILURE, "Unable to create the l3fwd hash on "
1803 "socket %d\n", socketid);
1806 /* populate the ipv4 hash */
1807 for (i = 0; i < RTE_DIM(ipv4_l3fwd_route_array); i++) {
1808 ret = rte_hash_add_key (ipv4_l3fwd_lookup_struct[socketid],
1809 (void *) &ipv4_l3fwd_route_array[i].key);
1811 rte_exit(EXIT_FAILURE, "Unable to add entry %u to the"
1812 "l3fwd hash on socket %d\n", i, socketid);
1814 ipv4_l3fwd_out_if[ret] = ipv4_l3fwd_route_array[i].if_out;
1815 printf("Hash: Adding key\n");
1816 print_ipv4_key(ipv4_l3fwd_route_array[i].key);
1819 /* populate the ipv6 hash */
1820 for (i = 0; i < RTE_DIM(ipv6_l3fwd_route_array); i++) {
1821 ret = rte_hash_add_key (ipv6_l3fwd_lookup_struct[socketid],
1822 (void *) &ipv6_l3fwd_route_array[i].key);
1824 rte_exit(EXIT_FAILURE, "Unable to add entry %u to the"
1825 "l3fwd hash on socket %d\n", i, socketid);
1827 ipv6_l3fwd_out_if[ret] = ipv6_l3fwd_route_array[i].if_out;
1828 printf("Hash: Adding key\n");
1829 print_ipv6_key(ipv6_l3fwd_route_array[i].key);
1834 #if (APP_LOOKUP_METHOD == APP_LOOKUP_LPM)
1836 setup_lpm(int socketid)
1842 /* create the LPM table */
1843 struct rte_lpm_config lpm_ipv4_config;
1845 lpm_ipv4_config.max_rules = IPV4_L3FWD_LPM_MAX_RULES;
1846 lpm_ipv4_config.number_tbl8s = 256;
1847 lpm_ipv4_config.flags = 0;
1849 snprintf(s, sizeof(s), "IPV4_L3FWD_LPM_%d", socketid);
1850 ipv4_l3fwd_lookup_struct[socketid] =
1851 rte_lpm_create(s, socketid, &lpm_ipv4_config);
1852 if (ipv4_l3fwd_lookup_struct[socketid] == NULL)
1853 rte_exit(EXIT_FAILURE, "Unable to create the l3fwd LPM table"
1854 " on socket %d\n", socketid);
1856 /* populate the LPM table */
1857 for (i = 0; i < RTE_DIM(ipv4_l3fwd_route_array); i++) {
1858 ret = rte_lpm_add(ipv4_l3fwd_lookup_struct[socketid],
1859 ipv4_l3fwd_route_array[i].ip,
1860 ipv4_l3fwd_route_array[i].depth,
1861 ipv4_l3fwd_route_array[i].if_out);
1864 rte_exit(EXIT_FAILURE, "Unable to add entry %u to the "
1865 "l3fwd LPM table on socket %d\n",
1869 printf("LPM: Adding route 0x%08x / %d (%d)\n",
1870 (unsigned)ipv4_l3fwd_route_array[i].ip,
1871 ipv4_l3fwd_route_array[i].depth,
1872 ipv4_l3fwd_route_array[i].if_out);
1878 init_mem(unsigned nb_mbuf)
1880 struct lcore_conf *qconf;
1885 for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
1886 if (rte_lcore_is_enabled(lcore_id) == 0)
1890 socketid = rte_lcore_to_socket_id(lcore_id);
1894 if (socketid >= NB_SOCKETS) {
1895 rte_exit(EXIT_FAILURE, "Socket %d of lcore %u is "
1896 "out of range %d\n", socketid,
1897 lcore_id, NB_SOCKETS);
1899 if (pktmbuf_pool[socketid] == NULL) {
1900 snprintf(s, sizeof(s), "mbuf_pool_%d", socketid);
1901 pktmbuf_pool[socketid] =
1902 rte_pktmbuf_pool_create(s, nb_mbuf,
1903 MEMPOOL_CACHE_SIZE, 0,
1904 RTE_MBUF_DEFAULT_BUF_SIZE,
1906 if (pktmbuf_pool[socketid] == NULL)
1907 rte_exit(EXIT_FAILURE,
1908 "Cannot init mbuf pool on socket %d\n",
1911 printf("Allocated mbuf pool on socket %d\n",
1914 #if (APP_LOOKUP_METHOD == APP_LOOKUP_LPM)
1915 setup_lpm(socketid);
1917 setup_hash(socketid);
1920 qconf = &lcore_conf[lcore_id];
1921 qconf->ipv4_lookup_struct = ipv4_l3fwd_lookup_struct[socketid];
1922 #if (APP_LOOKUP_METHOD == APP_LOOKUP_EXACT_MATCH)
1923 qconf->ipv6_lookup_struct = ipv6_l3fwd_lookup_struct[socketid];
1929 /* Check the link status of all ports in up to 9s, and print them finally */
1931 check_all_ports_link_status(uint32_t port_mask)
1933 #define CHECK_INTERVAL 100 /* 100ms */
1934 #define MAX_CHECK_TIME 90 /* 9s (90 * 100ms) in total */
1935 uint8_t count, all_ports_up, print_flag = 0;
1937 struct rte_eth_link link;
1940 printf("\nChecking link status");
1942 for (count = 0; count <= MAX_CHECK_TIME; count++) {
1944 RTE_ETH_FOREACH_DEV(portid) {
1945 if ((port_mask & (1 << portid)) == 0)
1947 memset(&link, 0, sizeof(link));
1948 ret = rte_eth_link_get_nowait(portid, &link);
1951 if (print_flag == 1)
1952 printf("Port %u link get failed: %s\n",
1953 portid, rte_strerror(-ret));
1956 /* print link status if flag set */
1957 if (print_flag == 1) {
1958 if (link.link_status)
1959 printf("Port %d Link Up - speed %u "
1960 "Mbps - %s\n", (uint8_t)portid,
1961 (unsigned)link.link_speed,
1962 (link.link_duplex == ETH_LINK_FULL_DUPLEX) ?
1963 ("full-duplex") : ("half-duplex\n"));
1965 printf("Port %d Link Down\n",
1969 /* clear all_ports_up flag if any link down */
1970 if (link.link_status == ETH_LINK_DOWN) {
1975 /* after finally printing all link status, get out */
1976 if (print_flag == 1)
1979 if (all_ports_up == 0) {
1982 rte_delay_ms(CHECK_INTERVAL);
1985 /* set the print_flag if all ports up or timeout */
1986 if (all_ports_up == 1 || count == (MAX_CHECK_TIME - 1)) {
1993 static int check_ptype(uint16_t portid)
1996 int ptype_l3_ipv4 = 0;
1997 #if (APP_LOOKUP_METHOD == APP_LOOKUP_EXACT_MATCH)
1998 int ptype_l3_ipv6 = 0;
2000 uint32_t ptype_mask = RTE_PTYPE_L3_MASK;
2002 ret = rte_eth_dev_get_supported_ptypes(portid, ptype_mask, NULL, 0);
2006 uint32_t ptypes[ret];
2008 ret = rte_eth_dev_get_supported_ptypes(portid, ptype_mask, ptypes, ret);
2009 for (i = 0; i < ret; ++i) {
2010 if (ptypes[i] & RTE_PTYPE_L3_IPV4)
2012 #if (APP_LOOKUP_METHOD == APP_LOOKUP_EXACT_MATCH)
2013 if (ptypes[i] & RTE_PTYPE_L3_IPV6)
2018 if (ptype_l3_ipv4 == 0)
2019 printf("port %d cannot parse RTE_PTYPE_L3_IPV4\n", portid);
2021 #if (APP_LOOKUP_METHOD == APP_LOOKUP_EXACT_MATCH)
2022 if (ptype_l3_ipv6 == 0)
2023 printf("port %d cannot parse RTE_PTYPE_L3_IPV6\n", portid);
2026 #if (APP_LOOKUP_METHOD == APP_LOOKUP_LPM)
2028 #else /* APP_LOOKUP_EXACT_MATCH */
2029 if (ptype_l3_ipv4 && ptype_l3_ipv6)
2038 init_power_library(void)
2040 unsigned int lcore_id;
2043 RTE_LCORE_FOREACH(lcore_id) {
2044 /* init power management library */
2045 ret = rte_power_init(lcore_id);
2048 "Library initialization failed on core %u\n",
2057 deinit_power_library(void)
2059 unsigned int lcore_id;
2062 RTE_LCORE_FOREACH(lcore_id) {
2063 /* deinit power management library */
2064 ret = rte_power_exit(lcore_id);
2067 "Library deinitialization failed on core %u\n",
2076 get_current_stat_values(uint64_t *values)
2078 unsigned int lcore_id = rte_lcore_id();
2079 struct lcore_conf *qconf;
2080 uint64_t app_eps = 0, app_fps = 0, app_br = 0;
2083 RTE_LCORE_FOREACH_SLAVE(lcore_id) {
2084 qconf = &lcore_conf[lcore_id];
2085 if (qconf->n_rx_queue == 0)
2088 rte_spinlock_lock(&stats[lcore_id].telemetry_lock);
2089 app_eps += stats[lcore_id].ep_nep[1];
2090 app_fps += stats[lcore_id].fp_nfp[1];
2091 app_br += stats[lcore_id].br;
2092 rte_spinlock_unlock(&stats[lcore_id].telemetry_lock);
2096 values[0] = app_eps/count;
2097 values[1] = app_fps/count;
2098 values[2] = app_br/count;
2100 memset(values, 0, sizeof(uint64_t) * NUM_TELSTATS);
2105 update_telemetry(__rte_unused struct rte_timer *tim,
2106 __rte_unused void *arg)
2109 uint64_t values[NUM_TELSTATS] = {0};
2111 get_current_stat_values(values);
2112 ret = rte_metrics_update_values(RTE_METRICS_GLOBAL, telstats_index,
2113 values, RTE_DIM(values));
2115 RTE_LOG(WARNING, POWER, "failed to update metrcis\n");
2119 handle_app_stats(const char *cmd __rte_unused,
2120 const char *params __rte_unused,
2121 struct rte_tel_data *d)
2123 uint64_t values[NUM_TELSTATS] = {0};
2126 rte_tel_data_start_dict(d);
2127 get_current_stat_values(values);
2128 for (i = 0; i < NUM_TELSTATS; i++)
2129 rte_tel_data_add_dict_u64(d, telstats_strings[i].name,
2135 telemetry_setup_timer(void)
2137 int lcore_id = rte_lcore_id();
2138 uint64_t hz = rte_get_timer_hz();
2141 ticks = hz / TELEMETRY_INTERVALS_PER_SEC;
2142 rte_timer_reset_sync(&telemetry_timer,
2150 empty_poll_setup_timer(void)
2152 int lcore_id = rte_lcore_id();
2153 uint64_t hz = rte_get_timer_hz();
2155 struct ep_params *ep_ptr = ep_params;
2157 ep_ptr->interval_ticks = hz / INTERVALS_PER_SECOND;
2159 rte_timer_reset_sync(&ep_ptr->timer0,
2160 ep_ptr->interval_ticks,
2163 rte_empty_poll_detection,
2168 launch_timer(unsigned int lcore_id)
2170 int64_t prev_tsc = 0, cur_tsc, diff_tsc, cycles_10ms;
2172 RTE_SET_USED(lcore_id);
2175 if (rte_get_master_lcore() != lcore_id) {
2176 rte_panic("timer on lcore:%d which is not master core:%d\n",
2178 rte_get_master_lcore());
2181 RTE_LOG(INFO, POWER, "Bring up the Timer\n");
2183 if (app_mode == APP_MODE_EMPTY_POLL)
2184 empty_poll_setup_timer();
2186 telemetry_setup_timer();
2188 cycles_10ms = rte_get_timer_hz() / 100;
2190 while (!is_done()) {
2191 cur_tsc = rte_rdtsc();
2192 diff_tsc = cur_tsc - prev_tsc;
2193 if (diff_tsc > cycles_10ms) {
2196 cycles_10ms = rte_get_timer_hz() / 100;
2200 RTE_LOG(INFO, POWER, "Timer_subsystem is done\n");
2207 main(int argc, char **argv)
2209 struct lcore_conf *qconf;
2210 struct rte_eth_dev_info dev_info;
2211 struct rte_eth_txconf *txconf;
2217 uint32_t n_tx_queue, nb_lcores;
2218 uint32_t dev_rxq_num, dev_txq_num;
2219 uint8_t nb_rx_queue, queue, socketid;
2221 const char *ptr_strings[NUM_TELSTATS];
2223 /* catch SIGINT and restore cpufreq governor to ondemand */
2224 signal(SIGINT, signal_exit_now);
2227 ret = rte_eal_init(argc, argv);
2229 rte_exit(EXIT_FAILURE, "Invalid EAL parameters\n");
2233 /* init RTE timer library to be used late */
2234 rte_timer_subsystem_init();
2236 /* parse application arguments (after the EAL ones) */
2237 ret = parse_args(argc, argv);
2239 rte_exit(EXIT_FAILURE, "Invalid L3FWD parameters\n");
2241 if (app_mode != APP_MODE_TELEMETRY && init_power_library())
2242 rte_exit(EXIT_FAILURE, "init_power_library failed\n");
2244 if (update_lcore_params() < 0)
2245 rte_exit(EXIT_FAILURE, "update_lcore_params failed\n");
2247 if (check_lcore_params() < 0)
2248 rte_exit(EXIT_FAILURE, "check_lcore_params failed\n");
2250 ret = init_lcore_rx_queues();
2252 rte_exit(EXIT_FAILURE, "init_lcore_rx_queues failed\n");
2254 nb_ports = rte_eth_dev_count_avail();
2256 if (check_port_config() < 0)
2257 rte_exit(EXIT_FAILURE, "check_port_config failed\n");
2259 nb_lcores = rte_lcore_count();
2261 /* initialize all ports */
2262 RTE_ETH_FOREACH_DEV(portid) {
2263 struct rte_eth_conf local_port_conf = port_conf;
2265 /* skip ports that are not enabled */
2266 if ((enabled_port_mask & (1 << portid)) == 0) {
2267 printf("\nSkipping disabled port %d\n", portid);
2272 printf("Initializing port %d ... ", portid );
2275 ret = rte_eth_dev_info_get(portid, &dev_info);
2277 rte_exit(EXIT_FAILURE,
2278 "Error during getting device (port %u) info: %s\n",
2279 portid, strerror(-ret));
2281 dev_rxq_num = dev_info.max_rx_queues;
2282 dev_txq_num = dev_info.max_tx_queues;
2284 nb_rx_queue = get_port_n_rx_queues(portid);
2285 if (nb_rx_queue > dev_rxq_num)
2286 rte_exit(EXIT_FAILURE,
2287 "Cannot configure not existed rxq: "
2288 "port=%d\n", portid);
2290 n_tx_queue = nb_lcores;
2291 if (n_tx_queue > dev_txq_num)
2292 n_tx_queue = dev_txq_num;
2293 printf("Creating queues: nb_rxq=%d nb_txq=%u... ",
2294 nb_rx_queue, (unsigned)n_tx_queue );
2295 /* If number of Rx queue is 0, no need to enable Rx interrupt */
2296 if (nb_rx_queue == 0)
2297 local_port_conf.intr_conf.rxq = 0;
2299 ret = rte_eth_dev_info_get(portid, &dev_info);
2301 rte_exit(EXIT_FAILURE,
2302 "Error during getting device (port %u) info: %s\n",
2303 portid, strerror(-ret));
2305 if (dev_info.tx_offload_capa & DEV_TX_OFFLOAD_MBUF_FAST_FREE)
2306 local_port_conf.txmode.offloads |=
2307 DEV_TX_OFFLOAD_MBUF_FAST_FREE;
2309 local_port_conf.rx_adv_conf.rss_conf.rss_hf &=
2310 dev_info.flow_type_rss_offloads;
2311 if (local_port_conf.rx_adv_conf.rss_conf.rss_hf !=
2312 port_conf.rx_adv_conf.rss_conf.rss_hf) {
2313 printf("Port %u modified RSS hash function based on hardware support,"
2314 "requested:%#"PRIx64" configured:%#"PRIx64"\n",
2316 port_conf.rx_adv_conf.rss_conf.rss_hf,
2317 local_port_conf.rx_adv_conf.rss_conf.rss_hf);
2320 ret = rte_eth_dev_configure(portid, nb_rx_queue,
2321 (uint16_t)n_tx_queue, &local_port_conf);
2323 rte_exit(EXIT_FAILURE, "Cannot configure device: "
2324 "err=%d, port=%d\n", ret, portid);
2326 ret = rte_eth_dev_adjust_nb_rx_tx_desc(portid, &nb_rxd,
2329 rte_exit(EXIT_FAILURE,
2330 "Cannot adjust number of descriptors: err=%d, port=%d\n",
2333 ret = rte_eth_macaddr_get(portid, &ports_eth_addr[portid]);
2335 rte_exit(EXIT_FAILURE,
2336 "Cannot get MAC address: err=%d, port=%d\n",
2339 print_ethaddr(" Address:", &ports_eth_addr[portid]);
2343 ret = init_mem(NB_MBUF);
2345 rte_exit(EXIT_FAILURE, "init_mem failed\n");
2347 for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
2348 if (rte_lcore_is_enabled(lcore_id) == 0)
2351 /* Initialize TX buffers */
2352 qconf = &lcore_conf[lcore_id];
2353 qconf->tx_buffer[portid] = rte_zmalloc_socket("tx_buffer",
2354 RTE_ETH_TX_BUFFER_SIZE(MAX_PKT_BURST), 0,
2355 rte_eth_dev_socket_id(portid));
2356 if (qconf->tx_buffer[portid] == NULL)
2357 rte_exit(EXIT_FAILURE, "Can't allocate tx buffer for port %u\n",
2360 rte_eth_tx_buffer_init(qconf->tx_buffer[portid], MAX_PKT_BURST);
2363 /* init one TX queue per couple (lcore,port) */
2365 for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
2366 if (rte_lcore_is_enabled(lcore_id) == 0)
2369 if (queueid >= dev_txq_num)
2374 (uint8_t)rte_lcore_to_socket_id(lcore_id);
2378 printf("txq=%u,%d,%d ", lcore_id, queueid, socketid);
2381 txconf = &dev_info.default_txconf;
2382 txconf->offloads = local_port_conf.txmode.offloads;
2383 ret = rte_eth_tx_queue_setup(portid, queueid, nb_txd,
2386 rte_exit(EXIT_FAILURE,
2387 "rte_eth_tx_queue_setup: err=%d, "
2388 "port=%d\n", ret, portid);
2390 qconf = &lcore_conf[lcore_id];
2391 qconf->tx_queue_id[portid] = queueid;
2394 qconf->tx_port_id[qconf->n_tx_port] = portid;
2400 for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
2401 if (rte_lcore_is_enabled(lcore_id) == 0)
2404 if (app_mode == APP_MODE_LEGACY) {
2405 /* init timer structures for each enabled lcore */
2406 rte_timer_init(&power_timers[lcore_id]);
2407 hz = rte_get_timer_hz();
2408 rte_timer_reset(&power_timers[lcore_id],
2409 hz/TIMER_NUMBER_PER_SECOND,
2411 power_timer_cb, NULL);
2413 qconf = &lcore_conf[lcore_id];
2414 printf("\nInitializing rx queues on lcore %u ... ", lcore_id );
2416 /* init RX queues */
2417 for(queue = 0; queue < qconf->n_rx_queue; ++queue) {
2418 struct rte_eth_rxconf rxq_conf;
2420 portid = qconf->rx_queue_list[queue].port_id;
2421 queueid = qconf->rx_queue_list[queue].queue_id;
2425 (uint8_t)rte_lcore_to_socket_id(lcore_id);
2429 printf("rxq=%d,%d,%d ", portid, queueid, socketid);
2432 ret = rte_eth_dev_info_get(portid, &dev_info);
2434 rte_exit(EXIT_FAILURE,
2435 "Error during getting device (port %u) info: %s\n",
2436 portid, strerror(-ret));
2438 rxq_conf = dev_info.default_rxconf;
2439 rxq_conf.offloads = port_conf.rxmode.offloads;
2440 ret = rte_eth_rx_queue_setup(portid, queueid, nb_rxd,
2441 socketid, &rxq_conf,
2442 pktmbuf_pool[socketid]);
2444 rte_exit(EXIT_FAILURE,
2445 "rte_eth_rx_queue_setup: err=%d, "
2446 "port=%d\n", ret, portid);
2449 if (add_cb_parse_ptype(portid, queueid) < 0)
2450 rte_exit(EXIT_FAILURE,
2451 "Fail to add ptype cb\n");
2452 } else if (!check_ptype(portid))
2453 rte_exit(EXIT_FAILURE,
2454 "PMD can not provide needed ptypes\n");
2461 RTE_ETH_FOREACH_DEV(portid) {
2462 if ((enabled_port_mask & (1 << portid)) == 0) {
2466 ret = rte_eth_dev_start(portid);
2468 rte_exit(EXIT_FAILURE, "rte_eth_dev_start: err=%d, "
2469 "port=%d\n", ret, portid);
2471 * If enabled, put device in promiscuous mode.
2472 * This allows IO forwarding mode to forward packets
2473 * to itself through 2 cross-connected ports of the
2476 if (promiscuous_on) {
2477 ret = rte_eth_promiscuous_enable(portid);
2479 rte_exit(EXIT_FAILURE,
2480 "rte_eth_promiscuous_enable: err=%s, port=%u\n",
2481 rte_strerror(-ret), portid);
2483 /* initialize spinlock for each port */
2484 rte_spinlock_init(&(locks[portid]));
2487 check_all_ports_link_status(enabled_port_mask);
2489 if (app_mode == APP_MODE_EMPTY_POLL) {
2491 if (empty_poll_train) {
2492 policy.state = TRAINING;
2494 policy.state = MED_NORMAL;
2495 policy.med_base_edpi = ep_med_edpi;
2496 policy.hgh_base_edpi = ep_hgh_edpi;
2499 ret = rte_power_empty_poll_stat_init(&ep_params,
2503 rte_exit(EXIT_FAILURE, "empty poll init failed");
2507 /* launch per-lcore init on every lcore */
2508 if (app_mode == APP_MODE_LEGACY) {
2509 rte_eal_mp_remote_launch(main_loop, NULL, CALL_MASTER);
2510 } else if (app_mode == APP_MODE_EMPTY_POLL) {
2511 empty_poll_stop = false;
2512 rte_eal_mp_remote_launch(main_empty_poll_loop, NULL,
2517 /* Init metrics library */
2518 rte_metrics_init(rte_socket_id());
2519 /** Register stats with metrics library */
2520 for (i = 0; i < NUM_TELSTATS; i++)
2521 ptr_strings[i] = telstats_strings[i].name;
2523 ret = rte_metrics_reg_names(ptr_strings, NUM_TELSTATS);
2525 telstats_index = ret;
2527 rte_exit(EXIT_FAILURE, "failed to register metrics names");
2529 RTE_LCORE_FOREACH_SLAVE(lcore_id) {
2530 rte_spinlock_init(&stats[lcore_id].telemetry_lock);
2532 rte_timer_init(&telemetry_timer);
2533 rte_telemetry_register_cmd("/l3fwd-power/stats",
2535 "Returns global power stats. Parameters: None");
2536 rte_eal_mp_remote_launch(main_telemetry_loop, NULL,
2540 if (app_mode == APP_MODE_EMPTY_POLL || app_mode == APP_MODE_TELEMETRY)
2541 launch_timer(rte_lcore_id());
2543 RTE_LCORE_FOREACH_SLAVE(lcore_id) {
2544 if (rte_eal_wait_lcore(lcore_id) < 0)
2548 RTE_ETH_FOREACH_DEV(portid)
2550 if ((enabled_port_mask & (1 << portid)) == 0)
2553 rte_eth_dev_stop(portid);
2554 rte_eth_dev_close(portid);
2557 if (app_mode == APP_MODE_EMPTY_POLL)
2558 rte_power_empty_poll_stat_free();
2560 if (app_mode != APP_MODE_TELEMETRY && deinit_power_library())
2561 rte_exit(EXIT_FAILURE, "deinit_power_library failed\n");
2563 if (rte_eal_cleanup() < 0)
2564 RTE_LOG(ERR, L3FWD_POWER, "EAL cleanup failed\n");