1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2010-2016 Intel Corporation
13 #include <sys/types.h>
15 #include <sys/queue.h>
21 #include <rte_common.h>
23 #include <rte_byteorder.h>
25 #include <rte_memory.h>
26 #include <rte_memcpy.h>
28 #include <rte_launch.h>
29 #include <rte_cycles.h>
30 #include <rte_prefetch.h>
31 #include <rte_lcore.h>
32 #include <rte_per_lcore.h>
33 #include <rte_branch_prediction.h>
34 #include <rte_interrupts.h>
35 #include <rte_random.h>
36 #include <rte_debug.h>
37 #include <rte_ether.h>
38 #include <rte_ethdev.h>
40 #include <rte_mempool.h>
45 #include <rte_string_fns.h>
46 #include <rte_pause.h>
47 #include <rte_timer.h>
49 #include <cmdline_parse.h>
50 #include <cmdline_parse_etheraddr.h>
52 #include <lthread_api.h>
54 #define APP_LOOKUP_EXACT_MATCH 0
55 #define APP_LOOKUP_LPM 1
56 #define DO_RFC_1812_CHECKS
58 /* Enable cpu-load stats 0-off, 1-on */
59 #define APP_CPU_LOAD 1
61 #ifndef APP_LOOKUP_METHOD
62 #define APP_LOOKUP_METHOD APP_LOOKUP_LPM
65 #ifndef __GLIBC__ /* sched_getcpu() is glibc specific */
66 #define sched_getcpu() rte_lcore_id()
70 check_ptype(int portid)
73 int ipv4 = 0, ipv6 = 0;
75 ret = rte_eth_dev_get_supported_ptypes(portid, RTE_PTYPE_L3_MASK, NULL,
82 ret = rte_eth_dev_get_supported_ptypes(portid, RTE_PTYPE_L3_MASK,
84 for (i = 0; i < ret; ++i) {
85 if (ptypes[i] & RTE_PTYPE_L3_IPV4)
87 if (ptypes[i] & RTE_PTYPE_L3_IPV6)
98 parse_ptype(struct rte_mbuf *m)
100 struct rte_ether_hdr *eth_hdr;
101 uint32_t packet_type = RTE_PTYPE_UNKNOWN;
104 eth_hdr = rte_pktmbuf_mtod(m, struct rte_ether_hdr *);
105 ether_type = eth_hdr->ether_type;
106 if (ether_type == rte_cpu_to_be_16(RTE_ETHER_TYPE_IPV4))
107 packet_type |= RTE_PTYPE_L3_IPV4_EXT_UNKNOWN;
108 else if (ether_type == rte_cpu_to_be_16(RTE_ETHER_TYPE_IPV6))
109 packet_type |= RTE_PTYPE_L3_IPV6_EXT_UNKNOWN;
111 m->packet_type = packet_type;
115 cb_parse_ptype(__rte_unused uint16_t port, __rte_unused uint16_t queue,
116 struct rte_mbuf *pkts[], uint16_t nb_pkts,
117 __rte_unused uint16_t max_pkts, __rte_unused void *user_param)
121 for (i = 0; i < nb_pkts; i++)
122 parse_ptype(pkts[i]);
128 * When set to zero, simple forwaring path is eanbled.
129 * When set to one, optimized forwarding path is enabled.
130 * Note that LPM optimisation path uses SSE4.1 instructions.
132 #define ENABLE_MULTI_BUFFER_OPTIMIZE 1
134 #if (APP_LOOKUP_METHOD == APP_LOOKUP_EXACT_MATCH)
135 #include <rte_hash.h>
136 #elif (APP_LOOKUP_METHOD == APP_LOOKUP_LPM)
138 #include <rte_lpm6.h>
140 #error "APP_LOOKUP_METHOD set to incorrect value"
143 #define RTE_LOGTYPE_L3FWD RTE_LOGTYPE_USER1
145 #define MAX_JUMBO_PKT_LEN 9600
147 #define IPV6_ADDR_LEN 16
149 #define MEMPOOL_CACHE_SIZE 256
152 * This expression is used to calculate the number of mbufs needed depending on
153 * user input, taking into account memory for rx and tx hardware rings, cache
154 * per lcore and mtable per port per lcore. RTE_MAX is used to ensure that
155 * NB_MBUF never goes below a minimum value of 8192
158 #define NB_MBUF RTE_MAX(\
159 (nb_ports*nb_rx_queue*nb_rxd + \
160 nb_ports*nb_lcores*MAX_PKT_BURST + \
161 nb_ports*n_tx_queue*nb_txd + \
162 nb_lcores*MEMPOOL_CACHE_SIZE), \
165 #define MAX_PKT_BURST 32
166 #define BURST_TX_DRAIN_US 100 /* TX drain every ~100us */
169 * Try to avoid TX buffering if we have at least MAX_TX_BURST packets to send.
171 #define MAX_TX_BURST (MAX_PKT_BURST / 2)
172 #define BURST_SIZE MAX_TX_BURST
176 /* Configure how many packets ahead to prefetch, when reading packets */
177 #define PREFETCH_OFFSET 3
179 /* Used to mark destination port as 'invalid'. */
180 #define BAD_PORT ((uint16_t)-1)
185 * Configurable number of RX/TX ring descriptors
187 #define RTE_TEST_RX_DESC_DEFAULT 1024
188 #define RTE_TEST_TX_DESC_DEFAULT 1024
189 static uint16_t nb_rxd = RTE_TEST_RX_DESC_DEFAULT;
190 static uint16_t nb_txd = RTE_TEST_TX_DESC_DEFAULT;
192 /* ethernet addresses of ports */
193 static uint64_t dest_eth_addr[RTE_MAX_ETHPORTS];
194 static struct rte_ether_addr ports_eth_addr[RTE_MAX_ETHPORTS];
196 static xmm_t val_eth[RTE_MAX_ETHPORTS];
198 /* replace first 12B of the ethernet header. */
199 #define MASK_ETH 0x3f
201 /* mask of enabled ports */
202 static uint32_t enabled_port_mask;
203 static int promiscuous_on; /**< Set in promiscuous mode off by default. */
204 static int numa_on = 1; /**< NUMA is enabled by default. */
205 static int parse_ptype_on;
207 #if (APP_LOOKUP_METHOD == APP_LOOKUP_EXACT_MATCH)
208 static int ipv6; /**< ipv6 is false by default. */
211 #if (APP_CPU_LOAD == 1)
213 #define MAX_CPU RTE_MAX_LCORE
214 #define CPU_LOAD_TIMEOUT_US (5 * 1000 * 1000) /**< Timeout for collecting 5s */
216 #define CPU_PROCESS 0
218 #define MAX_CPU_COUNTER 2
223 uint64_t hits[MAX_CPU_COUNTER][MAX_CPU];
224 } __rte_cache_aligned;
226 static struct cpu_load cpu_load;
227 static int cpu_load_lcore_id = -1;
229 #define SET_CPU_BUSY(thread, counter) \
230 thread->conf.busy[counter] = 1
232 #define SET_CPU_IDLE(thread, counter) \
233 thread->conf.busy[counter] = 0
235 #define IS_CPU_BUSY(thread, counter) \
236 (thread->conf.busy[counter] > 0)
240 #define SET_CPU_BUSY(thread, counter)
241 #define SET_CPU_IDLE(thread, counter)
242 #define IS_CPU_BUSY(thread, counter) 0
248 struct rte_mbuf *m_table[MAX_PKT_BURST];
251 struct lcore_rx_queue {
254 } __rte_cache_aligned;
256 #define MAX_RX_QUEUE_PER_LCORE 16
257 #define MAX_TX_QUEUE_PER_PORT RTE_MAX_ETHPORTS
258 #define MAX_RX_QUEUE_PER_PORT 128
260 #define MAX_LCORE_PARAMS 1024
261 struct rx_thread_params {
266 } __rte_cache_aligned;
268 static struct rx_thread_params rx_thread_params_array[MAX_LCORE_PARAMS];
269 static struct rx_thread_params rx_thread_params_array_default[] = {
281 static struct rx_thread_params *rx_thread_params =
282 rx_thread_params_array_default;
283 static uint16_t nb_rx_thread_params = RTE_DIM(rx_thread_params_array_default);
285 struct tx_thread_params {
288 } __rte_cache_aligned;
290 static struct tx_thread_params tx_thread_params_array[MAX_LCORE_PARAMS];
291 static struct tx_thread_params tx_thread_params_array_default[] = {
303 static struct tx_thread_params *tx_thread_params =
304 tx_thread_params_array_default;
305 static uint16_t nb_tx_thread_params = RTE_DIM(tx_thread_params_array_default);
307 static struct rte_eth_conf port_conf = {
309 .mq_mode = RTE_ETH_MQ_RX_RSS,
311 .offloads = RTE_ETH_RX_OFFLOAD_CHECKSUM,
316 .rss_hf = RTE_ETH_RSS_TCP,
320 .mq_mode = RTE_ETH_MQ_TX_NONE,
324 static uint32_t max_pkt_len;
326 static struct rte_mempool *pktmbuf_pool[NB_SOCKETS];
328 #if (APP_LOOKUP_METHOD == APP_LOOKUP_EXACT_MATCH)
330 #include <rte_hash_crc.h>
331 #define DEFAULT_HASH_FUNC rte_hash_crc
341 union ipv4_5tuple_host {
354 #define XMM_NUM_IN_IPV6_5TUPLE 3
357 uint8_t ip_dst[IPV6_ADDR_LEN];
358 uint8_t ip_src[IPV6_ADDR_LEN];
364 union ipv6_5tuple_host {
369 uint8_t ip_src[IPV6_ADDR_LEN];
370 uint8_t ip_dst[IPV6_ADDR_LEN];
375 __m128i xmm[XMM_NUM_IN_IPV6_5TUPLE];
378 struct ipv4_l3fwd_route {
379 struct ipv4_5tuple key;
383 struct ipv6_l3fwd_route {
384 struct ipv6_5tuple key;
388 static struct ipv4_l3fwd_route ipv4_l3fwd_route_array[] = {
389 {{RTE_IPV4(101, 0, 0, 0), RTE_IPV4(100, 10, 0, 1), 101, 11, IPPROTO_TCP}, 0},
390 {{RTE_IPV4(201, 0, 0, 0), RTE_IPV4(200, 20, 0, 1), 102, 12, IPPROTO_TCP}, 1},
391 {{RTE_IPV4(111, 0, 0, 0), RTE_IPV4(100, 30, 0, 1), 101, 11, IPPROTO_TCP}, 2},
392 {{RTE_IPV4(211, 0, 0, 0), RTE_IPV4(200, 40, 0, 1), 102, 12, IPPROTO_TCP}, 3},
395 static struct ipv6_l3fwd_route ipv6_l3fwd_route_array[] = {
397 {0xfe, 0x80, 0, 0, 0, 0, 0, 0, 0x02, 0x1e, 0x67, 0xff, 0xfe, 0, 0, 0},
398 {0xfe, 0x80, 0, 0, 0, 0, 0, 0, 0x02, 0x1b, 0x21, 0xff, 0xfe, 0x91, 0x38,
400 101, 11, IPPROTO_TCP}, 0},
403 {0xfe, 0x90, 0, 0, 0, 0, 0, 0, 0x02, 0x1e, 0x67, 0xff, 0xfe, 0, 0, 0},
404 {0xfe, 0x90, 0, 0, 0, 0, 0, 0, 0x02, 0x1b, 0x21, 0xff, 0xfe, 0x91, 0x38,
406 102, 12, IPPROTO_TCP}, 1},
409 {0xfe, 0xa0, 0, 0, 0, 0, 0, 0, 0x02, 0x1e, 0x67, 0xff, 0xfe, 0, 0, 0},
410 {0xfe, 0xa0, 0, 0, 0, 0, 0, 0, 0x02, 0x1b, 0x21, 0xff, 0xfe, 0x91, 0x38,
412 101, 11, IPPROTO_TCP}, 2},
415 {0xfe, 0xb0, 0, 0, 0, 0, 0, 0, 0x02, 0x1e, 0x67, 0xff, 0xfe, 0, 0, 0},
416 {0xfe, 0xb0, 0, 0, 0, 0, 0, 0, 0x02, 0x1b, 0x21, 0xff, 0xfe, 0x91, 0x38,
418 102, 12, IPPROTO_TCP}, 3},
421 typedef struct rte_hash lookup_struct_t;
422 static lookup_struct_t *ipv4_l3fwd_lookup_struct[NB_SOCKETS];
423 static lookup_struct_t *ipv6_l3fwd_lookup_struct[NB_SOCKETS];
425 #ifdef RTE_ARCH_X86_64
426 /* default to 4 million hash entries (approx) */
427 #define L3FWD_HASH_ENTRIES (1024*1024*4)
429 /* 32-bit has less address-space for hugepage memory, limit to 1M entries */
430 #define L3FWD_HASH_ENTRIES (1024*1024*1)
432 #define HASH_ENTRY_NUMBER_DEFAULT 4
434 static uint32_t hash_entry_number = HASH_ENTRY_NUMBER_DEFAULT;
436 static inline uint32_t
437 ipv4_hash_crc(const void *data, __rte_unused uint32_t data_len,
440 const union ipv4_5tuple_host *k;
446 p = (const uint32_t *)&k->port_src;
448 init_val = rte_hash_crc_4byte(t, init_val);
449 init_val = rte_hash_crc_4byte(k->ip_src, init_val);
450 init_val = rte_hash_crc_4byte(k->ip_dst, init_val);
451 init_val = rte_hash_crc_4byte(*p, init_val);
455 static inline uint32_t
456 ipv6_hash_crc(const void *data, __rte_unused uint32_t data_len,
459 const union ipv6_5tuple_host *k;
462 const uint32_t *ip_src0, *ip_src1, *ip_src2, *ip_src3;
463 const uint32_t *ip_dst0, *ip_dst1, *ip_dst2, *ip_dst3;
467 p = (const uint32_t *)&k->port_src;
469 ip_src0 = (const uint32_t *) k->ip_src;
470 ip_src1 = (const uint32_t *)(k->ip_src + 4);
471 ip_src2 = (const uint32_t *)(k->ip_src + 8);
472 ip_src3 = (const uint32_t *)(k->ip_src + 12);
473 ip_dst0 = (const uint32_t *) k->ip_dst;
474 ip_dst1 = (const uint32_t *)(k->ip_dst + 4);
475 ip_dst2 = (const uint32_t *)(k->ip_dst + 8);
476 ip_dst3 = (const uint32_t *)(k->ip_dst + 12);
477 init_val = rte_hash_crc_4byte(t, init_val);
478 init_val = rte_hash_crc_4byte(*ip_src0, init_val);
479 init_val = rte_hash_crc_4byte(*ip_src1, init_val);
480 init_val = rte_hash_crc_4byte(*ip_src2, init_val);
481 init_val = rte_hash_crc_4byte(*ip_src3, init_val);
482 init_val = rte_hash_crc_4byte(*ip_dst0, init_val);
483 init_val = rte_hash_crc_4byte(*ip_dst1, init_val);
484 init_val = rte_hash_crc_4byte(*ip_dst2, init_val);
485 init_val = rte_hash_crc_4byte(*ip_dst3, init_val);
486 init_val = rte_hash_crc_4byte(*p, init_val);
490 #define IPV4_L3FWD_NUM_ROUTES RTE_DIM(ipv4_l3fwd_route_array)
491 #define IPV6_L3FWD_NUM_ROUTES RTE_DIM(ipv6_l3fwd_route_array)
493 static uint8_t ipv4_l3fwd_out_if[L3FWD_HASH_ENTRIES] __rte_cache_aligned;
494 static uint8_t ipv6_l3fwd_out_if[L3FWD_HASH_ENTRIES] __rte_cache_aligned;
498 #if (APP_LOOKUP_METHOD == APP_LOOKUP_LPM)
499 struct ipv4_l3fwd_route {
505 struct ipv6_l3fwd_route {
511 static struct ipv4_l3fwd_route ipv4_l3fwd_route_array[] = {
512 {RTE_IPV4(1, 1, 1, 0), 24, 0},
513 {RTE_IPV4(2, 1, 1, 0), 24, 1},
514 {RTE_IPV4(3, 1, 1, 0), 24, 2},
515 {RTE_IPV4(4, 1, 1, 0), 24, 3},
516 {RTE_IPV4(5, 1, 1, 0), 24, 4},
517 {RTE_IPV4(6, 1, 1, 0), 24, 5},
518 {RTE_IPV4(7, 1, 1, 0), 24, 6},
519 {RTE_IPV4(8, 1, 1, 0), 24, 7},
522 static struct ipv6_l3fwd_route ipv6_l3fwd_route_array[] = {
523 {{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, 48, 0},
524 {{2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, 48, 1},
525 {{3, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, 48, 2},
526 {{4, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, 48, 3},
527 {{5, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, 48, 4},
528 {{6, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, 48, 5},
529 {{7, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, 48, 6},
530 {{8, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, 48, 7},
533 #define IPV4_L3FWD_NUM_ROUTES RTE_DIM(ipv4_l3fwd_route_array)
534 #define IPV6_L3FWD_NUM_ROUTES RTE_DIM(ipv6_l3fwd_route_array)
536 #define IPV4_L3FWD_LPM_MAX_RULES 1024
537 #define IPV6_L3FWD_LPM_MAX_RULES 1024
538 #define IPV6_L3FWD_LPM_NUMBER_TBL8S (1 << 16)
540 typedef struct rte_lpm lookup_struct_t;
541 typedef struct rte_lpm6 lookup6_struct_t;
542 static lookup_struct_t *ipv4_l3fwd_lookup_struct[NB_SOCKETS];
543 static lookup6_struct_t *ipv6_l3fwd_lookup_struct[NB_SOCKETS];
547 lookup_struct_t *ipv4_lookup_struct;
548 #if (APP_LOOKUP_METHOD == APP_LOOKUP_LPM)
549 lookup6_struct_t *ipv6_lookup_struct;
551 lookup_struct_t *ipv6_lookup_struct;
554 } __rte_cache_aligned;
556 static struct lcore_conf lcore_conf[RTE_MAX_LCORE];
557 RTE_DEFINE_PER_LCORE(struct lcore_conf *, lcore_conf);
559 #define MAX_RX_QUEUE_PER_THREAD 16
560 #define MAX_TX_PORT_PER_THREAD RTE_MAX_ETHPORTS
561 #define MAX_TX_QUEUE_PER_PORT RTE_MAX_ETHPORTS
562 #define MAX_RX_QUEUE_PER_PORT 128
564 #define MAX_RX_THREAD 1024
565 #define MAX_TX_THREAD 1024
566 #define MAX_THREAD (MAX_RX_THREAD + MAX_TX_THREAD)
569 * Producers and consumers threads configuration
571 static int lthreads_on = 1; /**< Use lthreads for processing*/
573 uint16_t rx_counter; /**< Number of spawned rx threads */
574 uint16_t tx_counter; /**< Number of spawned tx threads */
577 uint16_t lcore_id; /**< Initial lcore for rx thread */
578 uint16_t cpu_id; /**< Cpu id for cpu load stats counter */
579 uint16_t thread_id; /**< Thread ID */
581 #if (APP_CPU_LOAD > 0)
582 int busy[MAX_CPU_COUNTER];
586 struct thread_rx_conf {
587 struct thread_conf conf;
590 struct lcore_rx_queue rx_queue_list[MAX_RX_QUEUE_PER_LCORE];
592 uint16_t n_ring; /**< Number of output rings */
593 struct rte_ring *ring[RTE_MAX_LCORE];
594 struct lthread_cond *ready[RTE_MAX_LCORE];
596 #if (APP_CPU_LOAD > 0)
597 int busy[MAX_CPU_COUNTER];
599 } __rte_cache_aligned;
601 uint16_t n_rx_thread;
602 struct thread_rx_conf rx_thread[MAX_RX_THREAD];
604 struct thread_tx_conf {
605 struct thread_conf conf;
607 uint16_t tx_queue_id[RTE_MAX_ETHPORTS];
608 struct mbuf_table tx_mbufs[RTE_MAX_ETHPORTS];
610 struct rte_ring *ring;
611 struct lthread_cond **ready;
613 } __rte_cache_aligned;
615 uint16_t n_tx_thread;
616 struct thread_tx_conf tx_thread[MAX_TX_THREAD];
618 /* Send burst of packets on an output interface */
620 send_burst(struct thread_tx_conf *qconf, uint16_t n, uint16_t port)
622 struct rte_mbuf **m_table;
626 queueid = qconf->tx_queue_id[port];
627 m_table = (struct rte_mbuf **)qconf->tx_mbufs[port].m_table;
629 ret = rte_eth_tx_burst(port, queueid, m_table, n);
630 if (unlikely(ret < n)) {
632 rte_pktmbuf_free(m_table[ret]);
639 /* Enqueue a single packet, and send burst if queue is filled */
641 send_single_packet(struct rte_mbuf *m, uint16_t port)
644 struct thread_tx_conf *qconf;
647 qconf = (struct thread_tx_conf *)lthread_get_data();
649 qconf = (struct thread_tx_conf *)RTE_PER_LCORE(lcore_conf)->data;
651 len = qconf->tx_mbufs[port].len;
652 qconf->tx_mbufs[port].m_table[len] = m;
655 /* enough pkts to be sent */
656 if (unlikely(len == MAX_PKT_BURST)) {
657 send_burst(qconf, MAX_PKT_BURST, port);
661 qconf->tx_mbufs[port].len = len;
665 #if ((APP_LOOKUP_METHOD == APP_LOOKUP_LPM) && \
666 (ENABLE_MULTI_BUFFER_OPTIMIZE == 1))
667 static __rte_always_inline void
668 send_packetsx4(uint16_t port,
669 struct rte_mbuf *m[], uint32_t num)
672 struct thread_tx_conf *qconf;
675 qconf = (struct thread_tx_conf *)lthread_get_data();
677 qconf = (struct thread_tx_conf *)RTE_PER_LCORE(lcore_conf)->data;
679 len = qconf->tx_mbufs[port].len;
682 * If TX buffer for that queue is empty, and we have enough packets,
683 * then send them straightway.
685 if (num >= MAX_TX_BURST && len == 0) {
686 n = rte_eth_tx_burst(port, qconf->tx_queue_id[port], m, num);
687 if (unlikely(n < num)) {
689 rte_pktmbuf_free(m[n]);
696 * Put packets into TX buffer for that queue.
700 n = (n > MAX_PKT_BURST) ? MAX_PKT_BURST - len : num;
703 switch (n % FWDSTEP) {
706 qconf->tx_mbufs[port].m_table[len + j] = m[j];
710 qconf->tx_mbufs[port].m_table[len + j] = m[j];
714 qconf->tx_mbufs[port].m_table[len + j] = m[j];
718 qconf->tx_mbufs[port].m_table[len + j] = m[j];
725 /* enough pkts to be sent */
726 if (unlikely(len == MAX_PKT_BURST)) {
728 send_burst(qconf, MAX_PKT_BURST, port);
730 /* copy rest of the packets into the TX buffer. */
733 switch (len % FWDSTEP) {
736 qconf->tx_mbufs[port].m_table[j] = m[n + j];
740 qconf->tx_mbufs[port].m_table[j] = m[n + j];
744 qconf->tx_mbufs[port].m_table[j] = m[n + j];
748 qconf->tx_mbufs[port].m_table[j] = m[n + j];
754 qconf->tx_mbufs[port].len = len;
756 #endif /* APP_LOOKUP_LPM */
758 #ifdef DO_RFC_1812_CHECKS
760 is_valid_ipv4_pkt(struct rte_ipv4_hdr *pkt, uint32_t link_len)
762 /* From http://www.rfc-editor.org/rfc/rfc1812.txt section 5.2.2 */
764 * 1. The packet length reported by the Link Layer must be large
765 * enough to hold the minimum length legal IP datagram (20 bytes).
767 if (link_len < sizeof(struct rte_ipv4_hdr))
770 /* 2. The IP checksum must be correct. */
771 /* this is checked in H/W */
774 * 3. The IP version number must be 4. If the version number is not 4
775 * then the packet may be another version of IP, such as IPng or
778 if (((pkt->version_ihl) >> 4) != 4)
781 * 4. The IP header length field must be large enough to hold the
782 * minimum length legal IP datagram (20 bytes = 5 words).
784 if ((pkt->version_ihl & 0xf) < 5)
788 * 5. The IP total length field must be large enough to hold the IP
789 * datagram header, whose length is specified in the IP header length
792 if (rte_cpu_to_be_16(pkt->total_length) < sizeof(struct rte_ipv4_hdr))
799 #if (APP_LOOKUP_METHOD == APP_LOOKUP_EXACT_MATCH)
801 static __m128i mask0;
802 static __m128i mask1;
803 static __m128i mask2;
804 static inline uint16_t
805 get_ipv4_dst_port(void *ipv4_hdr, uint16_t portid,
806 lookup_struct_t *ipv4_l3fwd_lookup_struct)
809 union ipv4_5tuple_host key;
811 ipv4_hdr = (uint8_t *)ipv4_hdr +
812 offsetof(struct rte_ipv4_hdr, time_to_live);
813 __m128i data = _mm_loadu_si128((__m128i *)(ipv4_hdr));
814 /* Get 5 tuple: dst port, src port, dst IP address, src IP address and
816 key.xmm = _mm_and_si128(data, mask0);
817 /* Find destination port */
818 ret = rte_hash_lookup(ipv4_l3fwd_lookup_struct, (const void *)&key);
819 return ((ret < 0) ? portid : ipv4_l3fwd_out_if[ret]);
822 static inline uint16_t
823 get_ipv6_dst_port(void *ipv6_hdr, uint16_t portid,
824 lookup_struct_t *ipv6_l3fwd_lookup_struct)
827 union ipv6_5tuple_host key;
829 ipv6_hdr = (uint8_t *)ipv6_hdr +
830 offsetof(struct rte_ipv6_hdr, payload_len);
831 __m128i data0 = _mm_loadu_si128((__m128i *)(ipv6_hdr));
832 __m128i data1 = _mm_loadu_si128((__m128i *)(((uint8_t *)ipv6_hdr) +
834 __m128i data2 = _mm_loadu_si128((__m128i *)(((uint8_t *)ipv6_hdr) +
835 sizeof(__m128i) + sizeof(__m128i)));
836 /* Get part of 5 tuple: src IP address lower 96 bits and protocol */
837 key.xmm[0] = _mm_and_si128(data0, mask1);
838 /* Get part of 5 tuple: dst IP address lower 96 bits and src IP address
841 /* Get part of 5 tuple: dst port and src port and dst IP address higher
843 key.xmm[2] = _mm_and_si128(data2, mask2);
845 /* Find destination port */
846 ret = rte_hash_lookup(ipv6_l3fwd_lookup_struct, (const void *)&key);
847 return ((ret < 0) ? portid : ipv6_l3fwd_out_if[ret]);
851 #if (APP_LOOKUP_METHOD == APP_LOOKUP_LPM)
853 static inline uint16_t
854 get_ipv4_dst_port(void *ipv4_hdr, uint16_t portid,
855 lookup_struct_t *ipv4_l3fwd_lookup_struct)
859 return ((rte_lpm_lookup(ipv4_l3fwd_lookup_struct,
860 rte_be_to_cpu_32(((struct rte_ipv4_hdr *)ipv4_hdr)->dst_addr),
861 &next_hop) == 0) ? next_hop : portid);
864 static inline uint16_t
865 get_ipv6_dst_port(void *ipv6_hdr, uint16_t portid,
866 lookup6_struct_t *ipv6_l3fwd_lookup_struct)
870 return ((rte_lpm6_lookup(ipv6_l3fwd_lookup_struct,
871 ((struct rte_ipv6_hdr *)ipv6_hdr)->dst_addr, &next_hop) == 0) ?
876 static inline void l3fwd_simple_forward(struct rte_mbuf *m, uint16_t portid)
879 #if ((APP_LOOKUP_METHOD == APP_LOOKUP_EXACT_MATCH) && \
880 (ENABLE_MULTI_BUFFER_OPTIMIZE == 1))
882 #define MASK_ALL_PKTS 0xff
883 #define EXCLUDE_1ST_PKT 0xfe
884 #define EXCLUDE_2ND_PKT 0xfd
885 #define EXCLUDE_3RD_PKT 0xfb
886 #define EXCLUDE_4TH_PKT 0xf7
887 #define EXCLUDE_5TH_PKT 0xef
888 #define EXCLUDE_6TH_PKT 0xdf
889 #define EXCLUDE_7TH_PKT 0xbf
890 #define EXCLUDE_8TH_PKT 0x7f
893 simple_ipv4_fwd_8pkts(struct rte_mbuf *m[8], uint16_t portid)
895 struct rte_ether_hdr *eth_hdr[8];
896 struct rte_ipv4_hdr *ipv4_hdr[8];
897 uint16_t dst_port[8];
899 union ipv4_5tuple_host key[8];
902 eth_hdr[0] = rte_pktmbuf_mtod(m[0], struct rte_ether_hdr *);
903 eth_hdr[1] = rte_pktmbuf_mtod(m[1], struct rte_ether_hdr *);
904 eth_hdr[2] = rte_pktmbuf_mtod(m[2], struct rte_ether_hdr *);
905 eth_hdr[3] = rte_pktmbuf_mtod(m[3], struct rte_ether_hdr *);
906 eth_hdr[4] = rte_pktmbuf_mtod(m[4], struct rte_ether_hdr *);
907 eth_hdr[5] = rte_pktmbuf_mtod(m[5], struct rte_ether_hdr *);
908 eth_hdr[6] = rte_pktmbuf_mtod(m[6], struct rte_ether_hdr *);
909 eth_hdr[7] = rte_pktmbuf_mtod(m[7], struct rte_ether_hdr *);
911 /* Handle IPv4 headers.*/
912 ipv4_hdr[0] = rte_pktmbuf_mtod_offset(m[0], struct rte_ipv4_hdr *,
913 sizeof(struct rte_ether_hdr));
914 ipv4_hdr[1] = rte_pktmbuf_mtod_offset(m[1], struct rte_ipv4_hdr *,
915 sizeof(struct rte_ether_hdr));
916 ipv4_hdr[2] = rte_pktmbuf_mtod_offset(m[2], struct rte_ipv4_hdr *,
917 sizeof(struct rte_ether_hdr));
918 ipv4_hdr[3] = rte_pktmbuf_mtod_offset(m[3], struct rte_ipv4_hdr *,
919 sizeof(struct rte_ether_hdr));
920 ipv4_hdr[4] = rte_pktmbuf_mtod_offset(m[4], struct rte_ipv4_hdr *,
921 sizeof(struct rte_ether_hdr));
922 ipv4_hdr[5] = rte_pktmbuf_mtod_offset(m[5], struct rte_ipv4_hdr *,
923 sizeof(struct rte_ether_hdr));
924 ipv4_hdr[6] = rte_pktmbuf_mtod_offset(m[6], struct rte_ipv4_hdr *,
925 sizeof(struct rte_ether_hdr));
926 ipv4_hdr[7] = rte_pktmbuf_mtod_offset(m[7], struct rte_ipv4_hdr *,
927 sizeof(struct rte_ether_hdr));
929 #ifdef DO_RFC_1812_CHECKS
930 /* Check to make sure the packet is valid (RFC1812) */
931 uint8_t valid_mask = MASK_ALL_PKTS;
933 if (is_valid_ipv4_pkt(ipv4_hdr[0], m[0]->pkt_len) < 0) {
934 rte_pktmbuf_free(m[0]);
935 valid_mask &= EXCLUDE_1ST_PKT;
937 if (is_valid_ipv4_pkt(ipv4_hdr[1], m[1]->pkt_len) < 0) {
938 rte_pktmbuf_free(m[1]);
939 valid_mask &= EXCLUDE_2ND_PKT;
941 if (is_valid_ipv4_pkt(ipv4_hdr[2], m[2]->pkt_len) < 0) {
942 rte_pktmbuf_free(m[2]);
943 valid_mask &= EXCLUDE_3RD_PKT;
945 if (is_valid_ipv4_pkt(ipv4_hdr[3], m[3]->pkt_len) < 0) {
946 rte_pktmbuf_free(m[3]);
947 valid_mask &= EXCLUDE_4TH_PKT;
949 if (is_valid_ipv4_pkt(ipv4_hdr[4], m[4]->pkt_len) < 0) {
950 rte_pktmbuf_free(m[4]);
951 valid_mask &= EXCLUDE_5TH_PKT;
953 if (is_valid_ipv4_pkt(ipv4_hdr[5], m[5]->pkt_len) < 0) {
954 rte_pktmbuf_free(m[5]);
955 valid_mask &= EXCLUDE_6TH_PKT;
957 if (is_valid_ipv4_pkt(ipv4_hdr[6], m[6]->pkt_len) < 0) {
958 rte_pktmbuf_free(m[6]);
959 valid_mask &= EXCLUDE_7TH_PKT;
961 if (is_valid_ipv4_pkt(ipv4_hdr[7], m[7]->pkt_len) < 0) {
962 rte_pktmbuf_free(m[7]);
963 valid_mask &= EXCLUDE_8TH_PKT;
965 if (unlikely(valid_mask != MASK_ALL_PKTS)) {
971 for (i = 0; i < 8; i++)
972 if ((0x1 << i) & valid_mask)
973 l3fwd_simple_forward(m[i], portid);
975 #endif /* End of #ifdef DO_RFC_1812_CHECKS */
977 data[0] = _mm_loadu_si128(rte_pktmbuf_mtod_offset(m[0], __m128i *,
978 sizeof(struct rte_ether_hdr) +
979 offsetof(struct rte_ipv4_hdr, time_to_live)));
980 data[1] = _mm_loadu_si128(rte_pktmbuf_mtod_offset(m[1], __m128i *,
981 sizeof(struct rte_ether_hdr) +
982 offsetof(struct rte_ipv4_hdr, time_to_live)));
983 data[2] = _mm_loadu_si128(rte_pktmbuf_mtod_offset(m[2], __m128i *,
984 sizeof(struct rte_ether_hdr) +
985 offsetof(struct rte_ipv4_hdr, time_to_live)));
986 data[3] = _mm_loadu_si128(rte_pktmbuf_mtod_offset(m[3], __m128i *,
987 sizeof(struct rte_ether_hdr) +
988 offsetof(struct rte_ipv4_hdr, time_to_live)));
989 data[4] = _mm_loadu_si128(rte_pktmbuf_mtod_offset(m[4], __m128i *,
990 sizeof(struct rte_ether_hdr) +
991 offsetof(struct rte_ipv4_hdr, time_to_live)));
992 data[5] = _mm_loadu_si128(rte_pktmbuf_mtod_offset(m[5], __m128i *,
993 sizeof(struct rte_ether_hdr) +
994 offsetof(struct rte_ipv4_hdr, time_to_live)));
995 data[6] = _mm_loadu_si128(rte_pktmbuf_mtod_offset(m[6], __m128i *,
996 sizeof(struct rte_ether_hdr) +
997 offsetof(struct rte_ipv4_hdr, time_to_live)));
998 data[7] = _mm_loadu_si128(rte_pktmbuf_mtod_offset(m[7], __m128i *,
999 sizeof(struct rte_ether_hdr) +
1000 offsetof(struct rte_ipv4_hdr, time_to_live)));
1002 key[0].xmm = _mm_and_si128(data[0], mask0);
1003 key[1].xmm = _mm_and_si128(data[1], mask0);
1004 key[2].xmm = _mm_and_si128(data[2], mask0);
1005 key[3].xmm = _mm_and_si128(data[3], mask0);
1006 key[4].xmm = _mm_and_si128(data[4], mask0);
1007 key[5].xmm = _mm_and_si128(data[5], mask0);
1008 key[6].xmm = _mm_and_si128(data[6], mask0);
1009 key[7].xmm = _mm_and_si128(data[7], mask0);
1011 const void *key_array[8] = {&key[0], &key[1], &key[2], &key[3],
1012 &key[4], &key[5], &key[6], &key[7]};
1014 rte_hash_lookup_bulk(RTE_PER_LCORE(lcore_conf)->ipv4_lookup_struct,
1015 &key_array[0], 8, ret);
1016 dst_port[0] = ((ret[0] < 0) ? portid : ipv4_l3fwd_out_if[ret[0]]);
1017 dst_port[1] = ((ret[1] < 0) ? portid : ipv4_l3fwd_out_if[ret[1]]);
1018 dst_port[2] = ((ret[2] < 0) ? portid : ipv4_l3fwd_out_if[ret[2]]);
1019 dst_port[3] = ((ret[3] < 0) ? portid : ipv4_l3fwd_out_if[ret[3]]);
1020 dst_port[4] = ((ret[4] < 0) ? portid : ipv4_l3fwd_out_if[ret[4]]);
1021 dst_port[5] = ((ret[5] < 0) ? portid : ipv4_l3fwd_out_if[ret[5]]);
1022 dst_port[6] = ((ret[6] < 0) ? portid : ipv4_l3fwd_out_if[ret[6]]);
1023 dst_port[7] = ((ret[7] < 0) ? portid : ipv4_l3fwd_out_if[ret[7]]);
1025 if (dst_port[0] >= RTE_MAX_ETHPORTS ||
1026 (enabled_port_mask & 1 << dst_port[0]) == 0)
1027 dst_port[0] = portid;
1028 if (dst_port[1] >= RTE_MAX_ETHPORTS ||
1029 (enabled_port_mask & 1 << dst_port[1]) == 0)
1030 dst_port[1] = portid;
1031 if (dst_port[2] >= RTE_MAX_ETHPORTS ||
1032 (enabled_port_mask & 1 << dst_port[2]) == 0)
1033 dst_port[2] = portid;
1034 if (dst_port[3] >= RTE_MAX_ETHPORTS ||
1035 (enabled_port_mask & 1 << dst_port[3]) == 0)
1036 dst_port[3] = portid;
1037 if (dst_port[4] >= RTE_MAX_ETHPORTS ||
1038 (enabled_port_mask & 1 << dst_port[4]) == 0)
1039 dst_port[4] = portid;
1040 if (dst_port[5] >= RTE_MAX_ETHPORTS ||
1041 (enabled_port_mask & 1 << dst_port[5]) == 0)
1042 dst_port[5] = portid;
1043 if (dst_port[6] >= RTE_MAX_ETHPORTS ||
1044 (enabled_port_mask & 1 << dst_port[6]) == 0)
1045 dst_port[6] = portid;
1046 if (dst_port[7] >= RTE_MAX_ETHPORTS ||
1047 (enabled_port_mask & 1 << dst_port[7]) == 0)
1048 dst_port[7] = portid;
1050 #ifdef DO_RFC_1812_CHECKS
1051 /* Update time to live and header checksum */
1052 --(ipv4_hdr[0]->time_to_live);
1053 --(ipv4_hdr[1]->time_to_live);
1054 --(ipv4_hdr[2]->time_to_live);
1055 --(ipv4_hdr[3]->time_to_live);
1056 ++(ipv4_hdr[0]->hdr_checksum);
1057 ++(ipv4_hdr[1]->hdr_checksum);
1058 ++(ipv4_hdr[2]->hdr_checksum);
1059 ++(ipv4_hdr[3]->hdr_checksum);
1060 --(ipv4_hdr[4]->time_to_live);
1061 --(ipv4_hdr[5]->time_to_live);
1062 --(ipv4_hdr[6]->time_to_live);
1063 --(ipv4_hdr[7]->time_to_live);
1064 ++(ipv4_hdr[4]->hdr_checksum);
1065 ++(ipv4_hdr[5]->hdr_checksum);
1066 ++(ipv4_hdr[6]->hdr_checksum);
1067 ++(ipv4_hdr[7]->hdr_checksum);
1071 *(uint64_t *)ð_hdr[0]->dst_addr = dest_eth_addr[dst_port[0]];
1072 *(uint64_t *)ð_hdr[1]->dst_addr = dest_eth_addr[dst_port[1]];
1073 *(uint64_t *)ð_hdr[2]->dst_addr = dest_eth_addr[dst_port[2]];
1074 *(uint64_t *)ð_hdr[3]->dst_addr = dest_eth_addr[dst_port[3]];
1075 *(uint64_t *)ð_hdr[4]->dst_addr = dest_eth_addr[dst_port[4]];
1076 *(uint64_t *)ð_hdr[5]->dst_addr = dest_eth_addr[dst_port[5]];
1077 *(uint64_t *)ð_hdr[6]->dst_addr = dest_eth_addr[dst_port[6]];
1078 *(uint64_t *)ð_hdr[7]->dst_addr = dest_eth_addr[dst_port[7]];
1081 rte_ether_addr_copy(&ports_eth_addr[dst_port[0]], ð_hdr[0]->src_addr);
1082 rte_ether_addr_copy(&ports_eth_addr[dst_port[1]], ð_hdr[1]->src_addr);
1083 rte_ether_addr_copy(&ports_eth_addr[dst_port[2]], ð_hdr[2]->src_addr);
1084 rte_ether_addr_copy(&ports_eth_addr[dst_port[3]], ð_hdr[3]->src_addr);
1085 rte_ether_addr_copy(&ports_eth_addr[dst_port[4]], ð_hdr[4]->src_addr);
1086 rte_ether_addr_copy(&ports_eth_addr[dst_port[5]], ð_hdr[5]->src_addr);
1087 rte_ether_addr_copy(&ports_eth_addr[dst_port[6]], ð_hdr[6]->src_addr);
1088 rte_ether_addr_copy(&ports_eth_addr[dst_port[7]], ð_hdr[7]->src_addr);
1090 send_single_packet(m[0], (uint8_t)dst_port[0]);
1091 send_single_packet(m[1], (uint8_t)dst_port[1]);
1092 send_single_packet(m[2], (uint8_t)dst_port[2]);
1093 send_single_packet(m[3], (uint8_t)dst_port[3]);
1094 send_single_packet(m[4], (uint8_t)dst_port[4]);
1095 send_single_packet(m[5], (uint8_t)dst_port[5]);
1096 send_single_packet(m[6], (uint8_t)dst_port[6]);
1097 send_single_packet(m[7], (uint8_t)dst_port[7]);
1101 static inline void get_ipv6_5tuple(struct rte_mbuf *m0, __m128i mask0,
1102 __m128i mask1, union ipv6_5tuple_host *key)
1104 __m128i tmpdata0 = _mm_loadu_si128(rte_pktmbuf_mtod_offset(m0,
1105 __m128i *, sizeof(struct rte_ether_hdr) +
1106 offsetof(struct rte_ipv6_hdr, payload_len)));
1107 __m128i tmpdata1 = _mm_loadu_si128(rte_pktmbuf_mtod_offset(m0,
1108 __m128i *, sizeof(struct rte_ether_hdr) +
1109 offsetof(struct rte_ipv6_hdr, payload_len) +
1111 __m128i tmpdata2 = _mm_loadu_si128(rte_pktmbuf_mtod_offset(m0,
1112 __m128i *, sizeof(struct rte_ether_hdr) +
1113 offsetof(struct rte_ipv6_hdr, payload_len) +
1114 sizeof(__m128i) + sizeof(__m128i)));
1115 key->xmm[0] = _mm_and_si128(tmpdata0, mask0);
1116 key->xmm[1] = tmpdata1;
1117 key->xmm[2] = _mm_and_si128(tmpdata2, mask1);
1121 simple_ipv6_fwd_8pkts(struct rte_mbuf *m[8], uint16_t portid)
1124 uint16_t dst_port[8];
1125 struct rte_ether_hdr *eth_hdr[8];
1126 union ipv6_5tuple_host key[8];
1128 __rte_unused struct rte_ipv6_hdr *ipv6_hdr[8];
1130 eth_hdr[0] = rte_pktmbuf_mtod(m[0], struct rte_ether_hdr *);
1131 eth_hdr[1] = rte_pktmbuf_mtod(m[1], struct rte_ether_hdr *);
1132 eth_hdr[2] = rte_pktmbuf_mtod(m[2], struct rte_ether_hdr *);
1133 eth_hdr[3] = rte_pktmbuf_mtod(m[3], struct rte_ether_hdr *);
1134 eth_hdr[4] = rte_pktmbuf_mtod(m[4], struct rte_ether_hdr *);
1135 eth_hdr[5] = rte_pktmbuf_mtod(m[5], struct rte_ether_hdr *);
1136 eth_hdr[6] = rte_pktmbuf_mtod(m[6], struct rte_ether_hdr *);
1137 eth_hdr[7] = rte_pktmbuf_mtod(m[7], struct rte_ether_hdr *);
1139 /* Handle IPv6 headers.*/
1140 ipv6_hdr[0] = rte_pktmbuf_mtod_offset(m[0], struct rte_ipv6_hdr *,
1141 sizeof(struct rte_ether_hdr));
1142 ipv6_hdr[1] = rte_pktmbuf_mtod_offset(m[1], struct rte_ipv6_hdr *,
1143 sizeof(struct rte_ether_hdr));
1144 ipv6_hdr[2] = rte_pktmbuf_mtod_offset(m[2], struct rte_ipv6_hdr *,
1145 sizeof(struct rte_ether_hdr));
1146 ipv6_hdr[3] = rte_pktmbuf_mtod_offset(m[3], struct rte_ipv6_hdr *,
1147 sizeof(struct rte_ether_hdr));
1148 ipv6_hdr[4] = rte_pktmbuf_mtod_offset(m[4], struct rte_ipv6_hdr *,
1149 sizeof(struct rte_ether_hdr));
1150 ipv6_hdr[5] = rte_pktmbuf_mtod_offset(m[5], struct rte_ipv6_hdr *,
1151 sizeof(struct rte_ether_hdr));
1152 ipv6_hdr[6] = rte_pktmbuf_mtod_offset(m[6], struct rte_ipv6_hdr *,
1153 sizeof(struct rte_ether_hdr));
1154 ipv6_hdr[7] = rte_pktmbuf_mtod_offset(m[7], struct rte_ipv6_hdr *,
1155 sizeof(struct rte_ether_hdr));
1157 get_ipv6_5tuple(m[0], mask1, mask2, &key[0]);
1158 get_ipv6_5tuple(m[1], mask1, mask2, &key[1]);
1159 get_ipv6_5tuple(m[2], mask1, mask2, &key[2]);
1160 get_ipv6_5tuple(m[3], mask1, mask2, &key[3]);
1161 get_ipv6_5tuple(m[4], mask1, mask2, &key[4]);
1162 get_ipv6_5tuple(m[5], mask1, mask2, &key[5]);
1163 get_ipv6_5tuple(m[6], mask1, mask2, &key[6]);
1164 get_ipv6_5tuple(m[7], mask1, mask2, &key[7]);
1166 const void *key_array[8] = {&key[0], &key[1], &key[2], &key[3],
1167 &key[4], &key[5], &key[6], &key[7]};
1169 rte_hash_lookup_bulk(RTE_PER_LCORE(lcore_conf)->ipv6_lookup_struct,
1170 &key_array[0], 4, ret);
1171 dst_port[0] = ((ret[0] < 0) ? portid : ipv6_l3fwd_out_if[ret[0]]);
1172 dst_port[1] = ((ret[1] < 0) ? portid : ipv6_l3fwd_out_if[ret[1]]);
1173 dst_port[2] = ((ret[2] < 0) ? portid : ipv6_l3fwd_out_if[ret[2]]);
1174 dst_port[3] = ((ret[3] < 0) ? portid : ipv6_l3fwd_out_if[ret[3]]);
1175 dst_port[4] = ((ret[4] < 0) ? portid : ipv6_l3fwd_out_if[ret[4]]);
1176 dst_port[5] = ((ret[5] < 0) ? portid : ipv6_l3fwd_out_if[ret[5]]);
1177 dst_port[6] = ((ret[6] < 0) ? portid : ipv6_l3fwd_out_if[ret[6]]);
1178 dst_port[7] = ((ret[7] < 0) ? portid : ipv6_l3fwd_out_if[ret[7]]);
1180 if (dst_port[0] >= RTE_MAX_ETHPORTS ||
1181 (enabled_port_mask & 1 << dst_port[0]) == 0)
1182 dst_port[0] = portid;
1183 if (dst_port[1] >= RTE_MAX_ETHPORTS ||
1184 (enabled_port_mask & 1 << dst_port[1]) == 0)
1185 dst_port[1] = portid;
1186 if (dst_port[2] >= RTE_MAX_ETHPORTS ||
1187 (enabled_port_mask & 1 << dst_port[2]) == 0)
1188 dst_port[2] = portid;
1189 if (dst_port[3] >= RTE_MAX_ETHPORTS ||
1190 (enabled_port_mask & 1 << dst_port[3]) == 0)
1191 dst_port[3] = portid;
1192 if (dst_port[4] >= RTE_MAX_ETHPORTS ||
1193 (enabled_port_mask & 1 << dst_port[4]) == 0)
1194 dst_port[4] = portid;
1195 if (dst_port[5] >= RTE_MAX_ETHPORTS ||
1196 (enabled_port_mask & 1 << dst_port[5]) == 0)
1197 dst_port[5] = portid;
1198 if (dst_port[6] >= RTE_MAX_ETHPORTS ||
1199 (enabled_port_mask & 1 << dst_port[6]) == 0)
1200 dst_port[6] = portid;
1201 if (dst_port[7] >= RTE_MAX_ETHPORTS ||
1202 (enabled_port_mask & 1 << dst_port[7]) == 0)
1203 dst_port[7] = portid;
1206 *(uint64_t *)ð_hdr[0]->dst_addr = dest_eth_addr[dst_port[0]];
1207 *(uint64_t *)ð_hdr[1]->dst_addr = dest_eth_addr[dst_port[1]];
1208 *(uint64_t *)ð_hdr[2]->dst_addr = dest_eth_addr[dst_port[2]];
1209 *(uint64_t *)ð_hdr[3]->dst_addr = dest_eth_addr[dst_port[3]];
1210 *(uint64_t *)ð_hdr[4]->dst_addr = dest_eth_addr[dst_port[4]];
1211 *(uint64_t *)ð_hdr[5]->dst_addr = dest_eth_addr[dst_port[5]];
1212 *(uint64_t *)ð_hdr[6]->dst_addr = dest_eth_addr[dst_port[6]];
1213 *(uint64_t *)ð_hdr[7]->dst_addr = dest_eth_addr[dst_port[7]];
1216 rte_ether_addr_copy(&ports_eth_addr[dst_port[0]], ð_hdr[0]->src_addr);
1217 rte_ether_addr_copy(&ports_eth_addr[dst_port[1]], ð_hdr[1]->src_addr);
1218 rte_ether_addr_copy(&ports_eth_addr[dst_port[2]], ð_hdr[2]->src_addr);
1219 rte_ether_addr_copy(&ports_eth_addr[dst_port[3]], ð_hdr[3]->src_addr);
1220 rte_ether_addr_copy(&ports_eth_addr[dst_port[4]], ð_hdr[4]->src_addr);
1221 rte_ether_addr_copy(&ports_eth_addr[dst_port[5]], ð_hdr[5]->src_addr);
1222 rte_ether_addr_copy(&ports_eth_addr[dst_port[6]], ð_hdr[6]->src_addr);
1223 rte_ether_addr_copy(&ports_eth_addr[dst_port[7]], ð_hdr[7]->src_addr);
1225 send_single_packet(m[0], dst_port[0]);
1226 send_single_packet(m[1], dst_port[1]);
1227 send_single_packet(m[2], dst_port[2]);
1228 send_single_packet(m[3], dst_port[3]);
1229 send_single_packet(m[4], dst_port[4]);
1230 send_single_packet(m[5], dst_port[5]);
1231 send_single_packet(m[6], dst_port[6]);
1232 send_single_packet(m[7], dst_port[7]);
1235 #endif /* APP_LOOKUP_METHOD */
1237 static __rte_always_inline void
1238 l3fwd_simple_forward(struct rte_mbuf *m, uint16_t portid)
1240 struct rte_ether_hdr *eth_hdr;
1241 struct rte_ipv4_hdr *ipv4_hdr;
1244 eth_hdr = rte_pktmbuf_mtod(m, struct rte_ether_hdr *);
1246 if (RTE_ETH_IS_IPV4_HDR(m->packet_type)) {
1247 /* Handle IPv4 headers.*/
1248 ipv4_hdr = rte_pktmbuf_mtod_offset(m, struct rte_ipv4_hdr *,
1249 sizeof(struct rte_ether_hdr));
1251 #ifdef DO_RFC_1812_CHECKS
1252 /* Check to make sure the packet is valid (RFC1812) */
1253 if (is_valid_ipv4_pkt(ipv4_hdr, m->pkt_len) < 0) {
1254 rte_pktmbuf_free(m);
1259 dst_port = get_ipv4_dst_port(ipv4_hdr, portid,
1260 RTE_PER_LCORE(lcore_conf)->ipv4_lookup_struct);
1261 if (dst_port >= RTE_MAX_ETHPORTS ||
1262 (enabled_port_mask & 1 << dst_port) == 0)
1265 #ifdef DO_RFC_1812_CHECKS
1266 /* Update time to live and header checksum */
1267 --(ipv4_hdr->time_to_live);
1268 ++(ipv4_hdr->hdr_checksum);
1271 *(uint64_t *)ð_hdr->dst_addr = dest_eth_addr[dst_port];
1274 rte_ether_addr_copy(&ports_eth_addr[dst_port],
1275 ð_hdr->src_addr);
1277 send_single_packet(m, dst_port);
1278 } else if (RTE_ETH_IS_IPV6_HDR(m->packet_type)) {
1279 /* Handle IPv6 headers.*/
1280 struct rte_ipv6_hdr *ipv6_hdr;
1282 ipv6_hdr = rte_pktmbuf_mtod_offset(m, struct rte_ipv6_hdr *,
1283 sizeof(struct rte_ether_hdr));
1285 dst_port = get_ipv6_dst_port(ipv6_hdr, portid,
1286 RTE_PER_LCORE(lcore_conf)->ipv6_lookup_struct);
1288 if (dst_port >= RTE_MAX_ETHPORTS ||
1289 (enabled_port_mask & 1 << dst_port) == 0)
1293 *(uint64_t *)ð_hdr->dst_addr = dest_eth_addr[dst_port];
1296 rte_ether_addr_copy(&ports_eth_addr[dst_port],
1297 ð_hdr->src_addr);
1299 send_single_packet(m, dst_port);
1301 /* Free the mbuf that contains non-IPV4/IPV6 packet */
1302 rte_pktmbuf_free(m);
1305 #if ((APP_LOOKUP_METHOD == APP_LOOKUP_LPM) && \
1306 (ENABLE_MULTI_BUFFER_OPTIMIZE == 1))
1307 #ifdef DO_RFC_1812_CHECKS
1309 #define IPV4_MIN_VER_IHL 0x45
1310 #define IPV4_MAX_VER_IHL 0x4f
1311 #define IPV4_MAX_VER_IHL_DIFF (IPV4_MAX_VER_IHL - IPV4_MIN_VER_IHL)
1313 /* Minimum value of IPV4 total length (20B) in network byte order. */
1314 #define IPV4_MIN_LEN_BE (sizeof(struct rte_ipv4_hdr) << 8)
1317 * From http://www.rfc-editor.org/rfc/rfc1812.txt section 5.2.2:
1318 * - The IP version number must be 4.
1319 * - The IP header length field must be large enough to hold the
1320 * minimum length legal IP datagram (20 bytes = 5 words).
1321 * - The IP total length field must be large enough to hold the IP
1322 * datagram header, whose length is specified in the IP header length
1324 * If we encounter invalid IPV4 packet, then set destination port for it
1325 * to BAD_PORT value.
1327 static __rte_always_inline void
1328 rfc1812_process(struct rte_ipv4_hdr *ipv4_hdr, uint16_t *dp, uint32_t ptype)
1332 if (RTE_ETH_IS_IPV4_HDR(ptype)) {
1333 ihl = ipv4_hdr->version_ihl - IPV4_MIN_VER_IHL;
1335 ipv4_hdr->time_to_live--;
1336 ipv4_hdr->hdr_checksum++;
1338 if (ihl > IPV4_MAX_VER_IHL_DIFF ||
1339 ((uint8_t)ipv4_hdr->total_length == 0 &&
1340 ipv4_hdr->total_length < IPV4_MIN_LEN_BE)) {
1347 #define rfc1812_process(mb, dp, ptype) do { } while (0)
1348 #endif /* DO_RFC_1812_CHECKS */
1349 #endif /* APP_LOOKUP_LPM && ENABLE_MULTI_BUFFER_OPTIMIZE */
1352 #if ((APP_LOOKUP_METHOD == APP_LOOKUP_LPM) && \
1353 (ENABLE_MULTI_BUFFER_OPTIMIZE == 1))
1355 static __rte_always_inline uint16_t
1356 get_dst_port(struct rte_mbuf *pkt, uint32_t dst_ipv4, uint16_t portid)
1359 struct rte_ipv6_hdr *ipv6_hdr;
1360 struct rte_ether_hdr *eth_hdr;
1362 if (RTE_ETH_IS_IPV4_HDR(pkt->packet_type)) {
1363 return (uint16_t) ((rte_lpm_lookup(
1364 RTE_PER_LCORE(lcore_conf)->ipv4_lookup_struct, dst_ipv4,
1365 &next_hop) == 0) ? next_hop : portid);
1367 } else if (RTE_ETH_IS_IPV6_HDR(pkt->packet_type)) {
1369 eth_hdr = rte_pktmbuf_mtod(pkt, struct rte_ether_hdr *);
1370 ipv6_hdr = (struct rte_ipv6_hdr *)(eth_hdr + 1);
1372 return (uint16_t) ((rte_lpm6_lookup(
1373 RTE_PER_LCORE(lcore_conf)->ipv6_lookup_struct,
1374 ipv6_hdr->dst_addr, &next_hop) == 0) ?
1383 process_packet(struct rte_mbuf *pkt, uint16_t *dst_port, uint16_t portid)
1385 struct rte_ether_hdr *eth_hdr;
1386 struct rte_ipv4_hdr *ipv4_hdr;
1391 eth_hdr = rte_pktmbuf_mtod(pkt, struct rte_ether_hdr *);
1392 ipv4_hdr = (struct rte_ipv4_hdr *)(eth_hdr + 1);
1394 dst_ipv4 = ipv4_hdr->dst_addr;
1395 dst_ipv4 = rte_be_to_cpu_32(dst_ipv4);
1396 dp = get_dst_port(pkt, dst_ipv4, portid);
1398 te = _mm_load_si128((__m128i *)eth_hdr);
1402 rfc1812_process(ipv4_hdr, dst_port, pkt->packet_type);
1404 te = _mm_blend_epi16(te, ve, MASK_ETH);
1405 _mm_store_si128((__m128i *)eth_hdr, te);
1409 * Read packet_type and destination IPV4 addresses from 4 mbufs.
1412 processx4_step1(struct rte_mbuf *pkt[FWDSTEP],
1414 uint32_t *ipv4_flag)
1416 struct rte_ipv4_hdr *ipv4_hdr;
1417 struct rte_ether_hdr *eth_hdr;
1418 uint32_t x0, x1, x2, x3;
1420 eth_hdr = rte_pktmbuf_mtod(pkt[0], struct rte_ether_hdr *);
1421 ipv4_hdr = (struct rte_ipv4_hdr *)(eth_hdr + 1);
1422 x0 = ipv4_hdr->dst_addr;
1423 ipv4_flag[0] = pkt[0]->packet_type & RTE_PTYPE_L3_IPV4;
1425 eth_hdr = rte_pktmbuf_mtod(pkt[1], struct rte_ether_hdr *);
1426 ipv4_hdr = (struct rte_ipv4_hdr *)(eth_hdr + 1);
1427 x1 = ipv4_hdr->dst_addr;
1428 ipv4_flag[0] &= pkt[1]->packet_type;
1430 eth_hdr = rte_pktmbuf_mtod(pkt[2], struct rte_ether_hdr *);
1431 ipv4_hdr = (struct rte_ipv4_hdr *)(eth_hdr + 1);
1432 x2 = ipv4_hdr->dst_addr;
1433 ipv4_flag[0] &= pkt[2]->packet_type;
1435 eth_hdr = rte_pktmbuf_mtod(pkt[3], struct rte_ether_hdr *);
1436 ipv4_hdr = (struct rte_ipv4_hdr *)(eth_hdr + 1);
1437 x3 = ipv4_hdr->dst_addr;
1438 ipv4_flag[0] &= pkt[3]->packet_type;
1440 dip[0] = _mm_set_epi32(x3, x2, x1, x0);
1444 * Lookup into LPM for destination port.
1445 * If lookup fails, use incoming port (portid) as destination port.
1448 processx4_step2(__m128i dip,
1451 struct rte_mbuf *pkt[FWDSTEP],
1452 uint16_t dprt[FWDSTEP])
1455 const __m128i bswap_mask = _mm_set_epi8(12, 13, 14, 15, 8, 9, 10, 11,
1456 4, 5, 6, 7, 0, 1, 2, 3);
1458 /* Byte swap 4 IPV4 addresses. */
1459 dip = _mm_shuffle_epi8(dip, bswap_mask);
1461 /* if all 4 packets are IPV4. */
1462 if (likely(ipv4_flag)) {
1463 rte_lpm_lookupx4(RTE_PER_LCORE(lcore_conf)->ipv4_lookup_struct, dip,
1466 /* get rid of unused upper 16 bit for each dport. */
1467 dst.x = _mm_packs_epi32(dst.x, dst.x);
1468 *(uint64_t *)dprt = dst.u64[0];
1471 dprt[0] = get_dst_port(pkt[0], dst.u32[0], portid);
1472 dprt[1] = get_dst_port(pkt[1], dst.u32[1], portid);
1473 dprt[2] = get_dst_port(pkt[2], dst.u32[2], portid);
1474 dprt[3] = get_dst_port(pkt[3], dst.u32[3], portid);
1479 * Update source and destination MAC addresses in the ethernet header.
1480 * Perform RFC1812 checks and updates for IPV4 packets.
1483 processx4_step3(struct rte_mbuf *pkt[FWDSTEP], uint16_t dst_port[FWDSTEP])
1485 __m128i te[FWDSTEP];
1486 __m128i ve[FWDSTEP];
1487 __m128i *p[FWDSTEP];
1489 p[0] = rte_pktmbuf_mtod(pkt[0], __m128i *);
1490 p[1] = rte_pktmbuf_mtod(pkt[1], __m128i *);
1491 p[2] = rte_pktmbuf_mtod(pkt[2], __m128i *);
1492 p[3] = rte_pktmbuf_mtod(pkt[3], __m128i *);
1494 ve[0] = val_eth[dst_port[0]];
1495 te[0] = _mm_load_si128(p[0]);
1497 ve[1] = val_eth[dst_port[1]];
1498 te[1] = _mm_load_si128(p[1]);
1500 ve[2] = val_eth[dst_port[2]];
1501 te[2] = _mm_load_si128(p[2]);
1503 ve[3] = val_eth[dst_port[3]];
1504 te[3] = _mm_load_si128(p[3]);
1506 /* Update first 12 bytes, keep rest bytes intact. */
1507 te[0] = _mm_blend_epi16(te[0], ve[0], MASK_ETH);
1508 te[1] = _mm_blend_epi16(te[1], ve[1], MASK_ETH);
1509 te[2] = _mm_blend_epi16(te[2], ve[2], MASK_ETH);
1510 te[3] = _mm_blend_epi16(te[3], ve[3], MASK_ETH);
1512 _mm_store_si128(p[0], te[0]);
1513 _mm_store_si128(p[1], te[1]);
1514 _mm_store_si128(p[2], te[2]);
1515 _mm_store_si128(p[3], te[3]);
1517 rfc1812_process((struct rte_ipv4_hdr *)
1518 ((struct rte_ether_hdr *)p[0] + 1),
1519 &dst_port[0], pkt[0]->packet_type);
1520 rfc1812_process((struct rte_ipv4_hdr *)
1521 ((struct rte_ether_hdr *)p[1] + 1),
1522 &dst_port[1], pkt[1]->packet_type);
1523 rfc1812_process((struct rte_ipv4_hdr *)
1524 ((struct rte_ether_hdr *)p[2] + 1),
1525 &dst_port[2], pkt[2]->packet_type);
1526 rfc1812_process((struct rte_ipv4_hdr *)
1527 ((struct rte_ether_hdr *)p[3] + 1),
1528 &dst_port[3], pkt[3]->packet_type);
1532 * We group consecutive packets with the same destionation port into one burst.
1533 * To avoid extra latency this is done together with some other packet
1534 * processing, but after we made a final decision about packet's destination.
1535 * To do this we maintain:
1536 * pnum - array of number of consecutive packets with the same dest port for
1537 * each packet in the input burst.
1538 * lp - pointer to the last updated element in the pnum.
1539 * dlp - dest port value lp corresponds to.
1542 #define GRPSZ (1 << FWDSTEP)
1543 #define GRPMSK (GRPSZ - 1)
1545 #define GROUP_PORT_STEP(dlp, dcp, lp, pn, idx) do { \
1546 if (likely((dlp) == (dcp)[(idx)])) { \
1549 (dlp) = (dcp)[idx]; \
1550 (lp) = (pn) + (idx); \
1556 * Group consecutive packets with the same destination port in bursts of 4.
1557 * Suppose we have array of destionation ports:
1558 * dst_port[] = {a, b, c, d,, e, ... }
1559 * dp1 should contain: <a, b, c, d>, dp2: <b, c, d, e>.
1560 * We doing 4 comparisons at once and the result is 4 bit mask.
1561 * This mask is used as an index into prebuild array of pnum values.
1563 static inline uint16_t *
1564 port_groupx4(uint16_t pn[FWDSTEP + 1], uint16_t *lp, __m128i dp1, __m128i dp2)
1566 static const struct {
1567 uint64_t pnum; /* prebuild 4 values for pnum[]. */
1568 int32_t idx; /* index for new last updated elemnet. */
1569 uint16_t lpv; /* add value to the last updated element. */
1572 /* 0: a != b, b != c, c != d, d != e */
1573 .pnum = UINT64_C(0x0001000100010001),
1578 /* 1: a == b, b != c, c != d, d != e */
1579 .pnum = UINT64_C(0x0001000100010002),
1584 /* 2: a != b, b == c, c != d, d != e */
1585 .pnum = UINT64_C(0x0001000100020001),
1590 /* 3: a == b, b == c, c != d, d != e */
1591 .pnum = UINT64_C(0x0001000100020003),
1596 /* 4: a != b, b != c, c == d, d != e */
1597 .pnum = UINT64_C(0x0001000200010001),
1602 /* 5: a == b, b != c, c == d, d != e */
1603 .pnum = UINT64_C(0x0001000200010002),
1608 /* 6: a != b, b == c, c == d, d != e */
1609 .pnum = UINT64_C(0x0001000200030001),
1614 /* 7: a == b, b == c, c == d, d != e */
1615 .pnum = UINT64_C(0x0001000200030004),
1620 /* 8: a != b, b != c, c != d, d == e */
1621 .pnum = UINT64_C(0x0002000100010001),
1626 /* 9: a == b, b != c, c != d, d == e */
1627 .pnum = UINT64_C(0x0002000100010002),
1632 /* 0xa: a != b, b == c, c != d, d == e */
1633 .pnum = UINT64_C(0x0002000100020001),
1638 /* 0xb: a == b, b == c, c != d, d == e */
1639 .pnum = UINT64_C(0x0002000100020003),
1644 /* 0xc: a != b, b != c, c == d, d == e */
1645 .pnum = UINT64_C(0x0002000300010001),
1650 /* 0xd: a == b, b != c, c == d, d == e */
1651 .pnum = UINT64_C(0x0002000300010002),
1656 /* 0xe: a != b, b == c, c == d, d == e */
1657 .pnum = UINT64_C(0x0002000300040001),
1662 /* 0xf: a == b, b == c, c == d, d == e */
1663 .pnum = UINT64_C(0x0002000300040005),
1670 uint16_t u16[FWDSTEP + 1];
1672 } *pnum = (void *)pn;
1676 dp1 = _mm_cmpeq_epi16(dp1, dp2);
1677 dp1 = _mm_unpacklo_epi16(dp1, dp1);
1678 v = _mm_movemask_ps((__m128)dp1);
1680 /* update last port counter. */
1681 lp[0] += gptbl[v].lpv;
1683 /* if dest port value has changed. */
1685 pnum->u64 = gptbl[v].pnum;
1686 pnum->u16[FWDSTEP] = 1;
1687 lp = pnum->u16 + gptbl[v].idx;
1693 #endif /* APP_LOOKUP_METHOD */
1696 process_burst(struct rte_mbuf *pkts_burst[MAX_PKT_BURST], int nb_rx,
1702 #if ((APP_LOOKUP_METHOD == APP_LOOKUP_LPM) && \
1703 (ENABLE_MULTI_BUFFER_OPTIMIZE == 1))
1707 uint16_t dst_port[MAX_PKT_BURST];
1708 __m128i dip[MAX_PKT_BURST / FWDSTEP];
1709 uint32_t ipv4_flag[MAX_PKT_BURST / FWDSTEP];
1710 uint16_t pnum[MAX_PKT_BURST + 1];
1714 #if (ENABLE_MULTI_BUFFER_OPTIMIZE == 1)
1715 #if (APP_LOOKUP_METHOD == APP_LOOKUP_EXACT_MATCH)
1718 * Send nb_rx - nb_rx%8 packets
1721 int32_t n = RTE_ALIGN_FLOOR(nb_rx, 8);
1723 for (j = 0; j < n; j += 8) {
1725 pkts_burst[j]->packet_type &
1726 pkts_burst[j+1]->packet_type &
1727 pkts_burst[j+2]->packet_type &
1728 pkts_burst[j+3]->packet_type &
1729 pkts_burst[j+4]->packet_type &
1730 pkts_burst[j+5]->packet_type &
1731 pkts_burst[j+6]->packet_type &
1732 pkts_burst[j+7]->packet_type;
1733 if (pkt_type & RTE_PTYPE_L3_IPV4) {
1734 simple_ipv4_fwd_8pkts(&pkts_burst[j], portid);
1735 } else if (pkt_type &
1736 RTE_PTYPE_L3_IPV6) {
1737 simple_ipv6_fwd_8pkts(&pkts_burst[j], portid);
1739 l3fwd_simple_forward(pkts_burst[j], portid);
1740 l3fwd_simple_forward(pkts_burst[j+1], portid);
1741 l3fwd_simple_forward(pkts_burst[j+2], portid);
1742 l3fwd_simple_forward(pkts_burst[j+3], portid);
1743 l3fwd_simple_forward(pkts_burst[j+4], portid);
1744 l3fwd_simple_forward(pkts_burst[j+5], portid);
1745 l3fwd_simple_forward(pkts_burst[j+6], portid);
1746 l3fwd_simple_forward(pkts_burst[j+7], portid);
1749 for (; j < nb_rx ; j++)
1750 l3fwd_simple_forward(pkts_burst[j], portid);
1752 #elif (APP_LOOKUP_METHOD == APP_LOOKUP_LPM)
1754 k = RTE_ALIGN_FLOOR(nb_rx, FWDSTEP);
1755 for (j = 0; j != k; j += FWDSTEP)
1756 processx4_step1(&pkts_burst[j], &dip[j / FWDSTEP],
1757 &ipv4_flag[j / FWDSTEP]);
1759 k = RTE_ALIGN_FLOOR(nb_rx, FWDSTEP);
1760 for (j = 0; j != k; j += FWDSTEP)
1761 processx4_step2(dip[j / FWDSTEP], ipv4_flag[j / FWDSTEP],
1762 portid, &pkts_burst[j], &dst_port[j]);
1765 * Finish packet processing and group consecutive
1766 * packets with the same destination port.
1768 k = RTE_ALIGN_FLOOR(nb_rx, FWDSTEP);
1775 processx4_step3(pkts_burst, dst_port);
1777 /* dp1: <d[0], d[1], d[2], d[3], ... > */
1778 dp1 = _mm_loadu_si128((__m128i *)dst_port);
1780 for (j = FWDSTEP; j != k; j += FWDSTEP) {
1781 processx4_step3(&pkts_burst[j], &dst_port[j]);
1785 * <d[j-3], d[j-2], d[j-1], d[j], ... >
1787 dp2 = _mm_loadu_si128(
1788 (__m128i *)&dst_port[j - FWDSTEP + 1]);
1789 lp = port_groupx4(&pnum[j - FWDSTEP], lp, dp1, dp2);
1793 * <d[j], d[j+1], d[j+2], d[j+3], ... >
1795 dp1 = _mm_srli_si128(dp2, (FWDSTEP - 1) *
1796 sizeof(dst_port[0]));
1800 * dp2: <d[j-3], d[j-2], d[j-1], d[j-1], ... >
1802 dp2 = _mm_shufflelo_epi16(dp1, 0xf9);
1803 lp = port_groupx4(&pnum[j - FWDSTEP], lp, dp1, dp2);
1806 * remove values added by the last repeated
1810 dlp = dst_port[j - 1];
1812 /* set dlp and lp to the never used values. */
1814 lp = pnum + MAX_PKT_BURST;
1817 /* Process up to last 3 packets one by one. */
1818 switch (nb_rx % FWDSTEP) {
1820 process_packet(pkts_burst[j], dst_port + j, portid);
1821 GROUP_PORT_STEP(dlp, dst_port, lp, pnum, j);
1825 process_packet(pkts_burst[j], dst_port + j, portid);
1826 GROUP_PORT_STEP(dlp, dst_port, lp, pnum, j);
1830 process_packet(pkts_burst[j], dst_port + j, portid);
1831 GROUP_PORT_STEP(dlp, dst_port, lp, pnum, j);
1836 * Send packets out, through destination port.
1837 * Consecuteve pacekts with the same destination port
1838 * are already grouped together.
1839 * If destination port for the packet equals BAD_PORT,
1840 * then free the packet without sending it out.
1842 for (j = 0; j < nb_rx; j += k) {
1850 if (likely(pn != BAD_PORT))
1851 send_packetsx4(pn, pkts_burst + j, k);
1853 for (m = j; m != j + k; m++)
1854 rte_pktmbuf_free(pkts_burst[m]);
1858 #endif /* APP_LOOKUP_METHOD */
1859 #else /* ENABLE_MULTI_BUFFER_OPTIMIZE == 0 */
1861 /* Prefetch first packets */
1862 for (j = 0; j < PREFETCH_OFFSET && j < nb_rx; j++)
1863 rte_prefetch0(rte_pktmbuf_mtod(pkts_burst[j], void *));
1865 /* Prefetch and forward already prefetched packets */
1866 for (j = 0; j < (nb_rx - PREFETCH_OFFSET); j++) {
1867 rte_prefetch0(rte_pktmbuf_mtod(pkts_burst[
1868 j + PREFETCH_OFFSET], void *));
1869 l3fwd_simple_forward(pkts_burst[j], portid);
1872 /* Forward remaining prefetched packets */
1873 for (; j < nb_rx; j++)
1874 l3fwd_simple_forward(pkts_burst[j], portid);
1876 #endif /* ENABLE_MULTI_BUFFER_OPTIMIZE */
1880 #if (APP_CPU_LOAD > 0)
1883 * CPU-load stats collector
1885 static int __rte_noreturn
1886 cpu_load_collector(__rte_unused void *arg) {
1888 uint64_t prev_tsc, diff_tsc, cur_tsc;
1889 uint64_t total[MAX_CPU] = { 0 };
1890 unsigned min_cpu = MAX_CPU;
1891 unsigned max_cpu = 0;
1896 unsigned int n_thread_per_cpu[MAX_CPU] = { 0 };
1897 struct thread_conf *thread_per_cpu[MAX_CPU][MAX_THREAD];
1899 struct thread_conf *thread_conf;
1901 const uint64_t interval_tsc = (rte_get_tsc_hz() + US_PER_S - 1) /
1902 US_PER_S * CPU_LOAD_TIMEOUT_US;
1906 * Wait for all threads
1909 printf("Waiting for %d rx threads and %d tx threads\n", n_rx_thread,
1912 rte_wait_until_equal_16(&rx_counter, n_rx_thread, __ATOMIC_RELAXED);
1913 rte_wait_until_equal_16(&tx_counter, n_tx_thread, __ATOMIC_RELAXED);
1915 for (i = 0; i < n_rx_thread; i++) {
1917 thread_conf = &rx_thread[i].conf;
1918 cpu_id = thread_conf->cpu_id;
1919 thread_per_cpu[cpu_id][n_thread_per_cpu[cpu_id]++] = thread_conf;
1921 if (cpu_id > max_cpu)
1923 if (cpu_id < min_cpu)
1926 for (i = 0; i < n_tx_thread; i++) {
1928 thread_conf = &tx_thread[i].conf;
1929 cpu_id = thread_conf->cpu_id;
1930 thread_per_cpu[cpu_id][n_thread_per_cpu[cpu_id]++] = thread_conf;
1932 if (thread_conf->cpu_id > max_cpu)
1933 max_cpu = thread_conf->cpu_id;
1934 if (thread_conf->cpu_id < min_cpu)
1935 min_cpu = thread_conf->cpu_id;
1941 for (i = min_cpu; i <= max_cpu; i++) {
1942 for (j = 0; j < MAX_CPU_COUNTER; j++) {
1943 for (k = 0; k < n_thread_per_cpu[i]; k++)
1944 if (thread_per_cpu[i][k]->busy[j]) {
1949 cpu_load.hits[j][i]++;
1961 cur_tsc = rte_rdtsc();
1963 diff_tsc = cur_tsc - prev_tsc;
1964 if (unlikely(diff_tsc > interval_tsc)) {
1968 printf("Cpu usage for %d rx threads and %d tx threads:\n\n",
1969 n_rx_thread, n_tx_thread);
1971 printf("cpu# proc%% poll%% overhead%%\n\n");
1973 for (i = min_cpu; i <= max_cpu; i++) {
1974 printf("CPU %d:", i);
1975 for (j = 0; j < MAX_CPU_COUNTER; j++) {
1976 printf("%7" PRIu64 "",
1977 cpu_load.hits[j][i] * 100 / cpu_load.counter);
1978 cpu_load.hits[j][i] = 0;
1980 printf("%7" PRIu64 "\n",
1981 100 - total[i] * 100 / cpu_load.counter);
1984 cpu_load.counter = 0;
1991 #endif /* APP_CPU_LOAD */
1994 * Null processing lthread loop
1996 * This loop is used to start empty scheduler on lcore.
1999 lthread_null(__rte_unused void *args)
2001 int lcore_id = rte_lcore_id();
2003 RTE_LOG(INFO, L3FWD, "Starting scheduler on lcore %d.\n", lcore_id);
2008 /* main processing loop */
2010 lthread_tx_per_ring(void *dummy)
2014 struct rte_ring *ring;
2015 struct thread_tx_conf *tx_conf;
2016 struct rte_mbuf *pkts_burst[MAX_PKT_BURST];
2017 struct lthread_cond *ready;
2019 tx_conf = (struct thread_tx_conf *)dummy;
2020 ring = tx_conf->ring;
2021 ready = *tx_conf->ready;
2023 lthread_set_data((void *)tx_conf);
2026 * Move this lthread to lcore
2028 lthread_set_affinity(tx_conf->conf.lcore_id);
2030 RTE_LOG(INFO, L3FWD, "entering main tx loop on lcore %u\n", rte_lcore_id());
2033 __atomic_fetch_add(&tx_counter, 1, __ATOMIC_RELAXED);
2037 * Read packet from ring
2039 SET_CPU_BUSY(tx_conf, CPU_POLL);
2040 nb_rx = rte_ring_sc_dequeue_burst(ring, (void **)pkts_burst,
2041 MAX_PKT_BURST, NULL);
2042 SET_CPU_IDLE(tx_conf, CPU_POLL);
2045 SET_CPU_BUSY(tx_conf, CPU_PROCESS);
2046 portid = pkts_burst[0]->port;
2047 process_burst(pkts_burst, nb_rx, portid);
2048 SET_CPU_IDLE(tx_conf, CPU_PROCESS);
2051 lthread_cond_wait(ready, 0);
2058 * Main tx-lthreads spawner lthread.
2060 * This lthread is used to spawn one new lthread per ring from producers.
2064 lthread_tx(void *args)
2070 struct thread_tx_conf *tx_conf;
2072 tx_conf = (struct thread_tx_conf *)args;
2073 lthread_set_data((void *)tx_conf);
2076 * Move this lthread to the selected lcore
2078 lthread_set_affinity(tx_conf->conf.lcore_id);
2081 * Spawn tx readers (one per input ring)
2083 lthread_create(<, tx_conf->conf.lcore_id, lthread_tx_per_ring,
2086 lcore_id = rte_lcore_id();
2088 RTE_LOG(INFO, L3FWD, "Entering Tx main loop on lcore %u\n", lcore_id);
2090 tx_conf->conf.cpu_id = sched_getcpu();
2093 lthread_sleep(BURST_TX_DRAIN_US * 1000);
2096 * TX burst queue drain
2098 for (portid = 0; portid < RTE_MAX_ETHPORTS; portid++) {
2099 if (tx_conf->tx_mbufs[portid].len == 0)
2101 SET_CPU_BUSY(tx_conf, CPU_PROCESS);
2102 send_burst(tx_conf, tx_conf->tx_mbufs[portid].len, portid);
2103 SET_CPU_IDLE(tx_conf, CPU_PROCESS);
2104 tx_conf->tx_mbufs[portid].len = 0;
2112 lthread_rx(void *dummy)
2120 int len[RTE_MAX_LCORE] = { 0 };
2121 int old_len, new_len;
2122 struct rte_mbuf *pkts_burst[MAX_PKT_BURST];
2123 struct thread_rx_conf *rx_conf;
2125 rx_conf = (struct thread_rx_conf *)dummy;
2126 lthread_set_data((void *)rx_conf);
2129 * Move this lthread to lcore
2131 lthread_set_affinity(rx_conf->conf.lcore_id);
2133 if (rx_conf->n_rx_queue == 0) {
2134 RTE_LOG(INFO, L3FWD, "lcore %u has nothing to do\n", rte_lcore_id());
2138 RTE_LOG(INFO, L3FWD, "Entering main Rx loop on lcore %u\n", rte_lcore_id());
2140 for (i = 0; i < rx_conf->n_rx_queue; i++) {
2142 portid = rx_conf->rx_queue_list[i].port_id;
2143 queueid = rx_conf->rx_queue_list[i].queue_id;
2144 RTE_LOG(INFO, L3FWD,
2145 " -- lcoreid=%u portid=%u rxqueueid=%hhu\n",
2146 rte_lcore_id(), portid, queueid);
2150 * Init all condition variables (one per rx thread)
2152 for (i = 0; i < rx_conf->n_rx_queue; i++)
2153 lthread_cond_init(NULL, &rx_conf->ready[i], NULL);
2157 rx_conf->conf.cpu_id = sched_getcpu();
2158 __atomic_fetch_add(&rx_counter, 1, __ATOMIC_RELAXED);
2162 * Read packet from RX queues
2164 for (i = 0; i < rx_conf->n_rx_queue; ++i) {
2165 portid = rx_conf->rx_queue_list[i].port_id;
2166 queueid = rx_conf->rx_queue_list[i].queue_id;
2168 SET_CPU_BUSY(rx_conf, CPU_POLL);
2169 nb_rx = rte_eth_rx_burst(portid, queueid, pkts_burst,
2171 SET_CPU_IDLE(rx_conf, CPU_POLL);
2174 worker_id = (worker_id + 1) % rx_conf->n_ring;
2175 old_len = len[worker_id];
2177 SET_CPU_BUSY(rx_conf, CPU_PROCESS);
2178 ret = rte_ring_sp_enqueue_burst(
2179 rx_conf->ring[worker_id],
2180 (void **) pkts_burst,
2183 new_len = old_len + ret;
2185 if (new_len >= BURST_SIZE) {
2186 lthread_cond_signal(rx_conf->ready[worker_id]);
2190 len[worker_id] = new_len;
2192 if (unlikely(ret < nb_rx)) {
2195 for (k = ret; k < nb_rx; k++) {
2196 struct rte_mbuf *m = pkts_burst[k];
2198 rte_pktmbuf_free(m);
2201 SET_CPU_IDLE(rx_conf, CPU_PROCESS);
2211 * Start scheduler with initial lthread on lcore
2213 * This lthread loop spawns all rx and tx lthreads on main lcore
2217 lthread_spawner(__rte_unused void *arg)
2219 struct lthread *lt[MAX_THREAD];
2223 printf("Entering lthread_spawner\n");
2226 * Create producers (rx threads) on default lcore
2228 for (i = 0; i < n_rx_thread; i++) {
2229 rx_thread[i].conf.thread_id = i;
2230 lthread_create(<[n_thread], -1, lthread_rx,
2231 (void *)&rx_thread[i]);
2236 * Wait for all producers. Until some producers can be started on the same
2237 * scheduler as this lthread, yielding is required to let them to run and
2238 * prevent deadlock here.
2240 while (__atomic_load_n(&rx_counter, __ATOMIC_RELAXED) < n_rx_thread)
2241 lthread_sleep(100000);
2244 * Create consumers (tx threads) on default lcore_id
2246 for (i = 0; i < n_tx_thread; i++) {
2247 tx_thread[i].conf.thread_id = i;
2248 lthread_create(<[n_thread], -1, lthread_tx,
2249 (void *)&tx_thread[i]);
2254 * Wait for all threads finished
2256 for (i = 0; i < n_thread; i++)
2257 lthread_join(lt[i], NULL);
2263 * Start main scheduler with initial lthread spawning rx and tx lthreads
2264 * (main_lthread_main).
2267 lthread_main_spawner(__rte_unused void *arg) {
2269 int lcore_id = rte_lcore_id();
2271 RTE_PER_LCORE(lcore_conf) = &lcore_conf[lcore_id];
2272 lthread_create(<, -1, lthread_spawner, NULL);
2279 * Start scheduler on lcore.
2282 sched_spawner(__rte_unused void *arg) {
2284 int lcore_id = rte_lcore_id();
2287 if (lcore_id == cpu_load_lcore_id) {
2288 cpu_load_collector(arg);
2291 #endif /* APP_CPU_LOAD */
2293 RTE_PER_LCORE(lcore_conf) = &lcore_conf[lcore_id];
2294 lthread_create(<, -1, lthread_null, NULL);
2300 /* main processing loop */
2301 static int __rte_noreturn
2302 pthread_tx(void *dummy)
2304 struct rte_mbuf *pkts_burst[MAX_PKT_BURST];
2305 uint64_t prev_tsc, diff_tsc, cur_tsc;
2308 struct thread_tx_conf *tx_conf;
2310 const uint64_t drain_tsc = (rte_get_tsc_hz() + US_PER_S - 1) /
2311 US_PER_S * BURST_TX_DRAIN_US;
2315 tx_conf = (struct thread_tx_conf *)dummy;
2317 RTE_LOG(INFO, L3FWD, "Entering main Tx loop on lcore %u\n", rte_lcore_id());
2319 tx_conf->conf.cpu_id = sched_getcpu();
2320 __atomic_fetch_add(&tx_counter, 1, __ATOMIC_RELAXED);
2323 cur_tsc = rte_rdtsc();
2326 * TX burst queue drain
2328 diff_tsc = cur_tsc - prev_tsc;
2329 if (unlikely(diff_tsc > drain_tsc)) {
2332 * This could be optimized (use queueid instead of
2333 * portid), but it is not called so often
2335 SET_CPU_BUSY(tx_conf, CPU_PROCESS);
2336 for (portid = 0; portid < RTE_MAX_ETHPORTS; portid++) {
2337 if (tx_conf->tx_mbufs[portid].len == 0)
2339 send_burst(tx_conf, tx_conf->tx_mbufs[portid].len, portid);
2340 tx_conf->tx_mbufs[portid].len = 0;
2342 SET_CPU_IDLE(tx_conf, CPU_PROCESS);
2348 * Read packet from ring
2350 SET_CPU_BUSY(tx_conf, CPU_POLL);
2351 nb_rx = rte_ring_sc_dequeue_burst(tx_conf->ring,
2352 (void **)pkts_burst, MAX_PKT_BURST, NULL);
2353 SET_CPU_IDLE(tx_conf, CPU_POLL);
2355 if (unlikely(nb_rx == 0)) {
2360 SET_CPU_BUSY(tx_conf, CPU_PROCESS);
2361 portid = pkts_burst[0]->port;
2362 process_burst(pkts_burst, nb_rx, portid);
2363 SET_CPU_IDLE(tx_conf, CPU_PROCESS);
2369 pthread_rx(void *dummy)
2378 struct rte_mbuf *pkts_burst[MAX_PKT_BURST];
2380 struct thread_rx_conf *rx_conf;
2382 lcore_id = rte_lcore_id();
2383 rx_conf = (struct thread_rx_conf *)dummy;
2385 if (rx_conf->n_rx_queue == 0) {
2386 RTE_LOG(INFO, L3FWD, "lcore %u has nothing to do\n", lcore_id);
2390 RTE_LOG(INFO, L3FWD, "entering main rx loop on lcore %u\n", lcore_id);
2392 for (i = 0; i < rx_conf->n_rx_queue; i++) {
2394 portid = rx_conf->rx_queue_list[i].port_id;
2395 queueid = rx_conf->rx_queue_list[i].queue_id;
2396 RTE_LOG(INFO, L3FWD,
2397 " -- lcoreid=%u portid=%u rxqueueid=%hhu\n",
2398 lcore_id, portid, queueid);
2402 rx_conf->conf.cpu_id = sched_getcpu();
2403 __atomic_fetch_add(&rx_counter, 1, __ATOMIC_RELAXED);
2407 * Read packet from RX queues
2409 for (i = 0; i < rx_conf->n_rx_queue; ++i) {
2410 portid = rx_conf->rx_queue_list[i].port_id;
2411 queueid = rx_conf->rx_queue_list[i].queue_id;
2413 SET_CPU_BUSY(rx_conf, CPU_POLL);
2414 nb_rx = rte_eth_rx_burst(portid, queueid, pkts_burst,
2416 SET_CPU_IDLE(rx_conf, CPU_POLL);
2423 SET_CPU_BUSY(rx_conf, CPU_PROCESS);
2424 worker_id = (worker_id + 1) % rx_conf->n_ring;
2425 n = rte_ring_sp_enqueue_burst(rx_conf->ring[worker_id],
2426 (void **)pkts_burst, nb_rx, NULL);
2428 if (unlikely(n != nb_rx)) {
2431 for (k = n; k < nb_rx; k++) {
2432 struct rte_mbuf *m = pkts_burst[k];
2434 rte_pktmbuf_free(m);
2438 SET_CPU_IDLE(rx_conf, CPU_PROCESS);
2448 pthread_run(__rte_unused void *arg) {
2449 int lcore_id = rte_lcore_id();
2452 for (i = 0; i < n_rx_thread; i++)
2453 if (rx_thread[i].conf.lcore_id == lcore_id) {
2454 printf("Start rx thread on %d...\n", lcore_id);
2455 RTE_PER_LCORE(lcore_conf) = &lcore_conf[lcore_id];
2456 RTE_PER_LCORE(lcore_conf)->data = (void *)&rx_thread[i];
2457 pthread_rx((void *)&rx_thread[i]);
2461 for (i = 0; i < n_tx_thread; i++)
2462 if (tx_thread[i].conf.lcore_id == lcore_id) {
2463 printf("Start tx thread on %d...\n", lcore_id);
2464 RTE_PER_LCORE(lcore_conf) = &lcore_conf[lcore_id];
2465 RTE_PER_LCORE(lcore_conf)->data = (void *)&tx_thread[i];
2466 pthread_tx((void *)&tx_thread[i]);
2471 if (lcore_id == cpu_load_lcore_id)
2472 cpu_load_collector(arg);
2473 #endif /* APP_CPU_LOAD */
2479 check_lcore_params(void)
2481 uint8_t queue, lcore;
2485 for (i = 0; i < nb_rx_thread_params; ++i) {
2486 queue = rx_thread_params[i].queue_id;
2487 if (queue >= MAX_RX_QUEUE_PER_PORT) {
2488 printf("invalid queue number: %hhu\n", queue);
2491 lcore = rx_thread_params[i].lcore_id;
2492 if (!rte_lcore_is_enabled(lcore)) {
2493 printf("error: lcore %hhu is not enabled in lcore mask\n", lcore);
2496 socketid = rte_lcore_to_socket_id(lcore);
2497 if ((socketid != 0) && (numa_on == 0))
2498 printf("warning: lcore %hhu is on socket %d with numa off\n",
2505 check_port_config(void)
2510 for (i = 0; i < nb_rx_thread_params; ++i) {
2511 portid = rx_thread_params[i].port_id;
2512 if ((enabled_port_mask & (1 << portid)) == 0) {
2513 printf("port %u is not enabled in port mask\n", portid);
2516 if (!rte_eth_dev_is_valid_port(portid)) {
2517 printf("port %u is not present on the board\n", portid);
2525 get_port_n_rx_queues(const uint16_t port)
2530 for (i = 0; i < nb_rx_thread_params; ++i)
2531 if (rx_thread_params[i].port_id == port &&
2532 rx_thread_params[i].queue_id > queue)
2533 queue = rx_thread_params[i].queue_id;
2535 return (uint8_t)(++queue);
2542 struct thread_rx_conf *rx_conf;
2543 struct thread_tx_conf *tx_conf;
2544 unsigned rx_thread_id, tx_thread_id;
2546 struct rte_ring *ring = NULL;
2548 for (tx_thread_id = 0; tx_thread_id < n_tx_thread; tx_thread_id++) {
2550 tx_conf = &tx_thread[tx_thread_id];
2552 printf("Connecting tx-thread %d with rx-thread %d\n", tx_thread_id,
2553 tx_conf->conf.thread_id);
2555 rx_thread_id = tx_conf->conf.thread_id;
2556 if (rx_thread_id > n_tx_thread) {
2557 printf("connection from tx-thread %u to rx-thread %u fails "
2558 "(rx-thread not defined)\n", tx_thread_id, rx_thread_id);
2562 rx_conf = &rx_thread[rx_thread_id];
2563 socket_io = rte_lcore_to_socket_id(rx_conf->conf.lcore_id);
2565 snprintf(name, sizeof(name), "app_ring_s%u_rx%u_tx%u",
2566 socket_io, rx_thread_id, tx_thread_id);
2568 ring = rte_ring_create(name, 1024 * 4, socket_io,
2569 RING_F_SP_ENQ | RING_F_SC_DEQ);
2572 rte_panic("Cannot create ring to connect rx-thread %u "
2573 "with tx-thread %u\n", rx_thread_id, tx_thread_id);
2576 rx_conf->ring[rx_conf->n_ring] = ring;
2578 tx_conf->ring = ring;
2579 tx_conf->ready = &rx_conf->ready[rx_conf->n_ring];
2587 init_rx_queues(void)
2589 uint16_t i, nb_rx_queue;
2594 for (i = 0; i < nb_rx_thread_params; ++i) {
2595 thread = rx_thread_params[i].thread_id;
2596 nb_rx_queue = rx_thread[thread].n_rx_queue;
2598 if (nb_rx_queue >= MAX_RX_QUEUE_PER_LCORE) {
2599 printf("error: too many queues (%u) for thread: %u\n",
2600 (unsigned)nb_rx_queue + 1, (unsigned)thread);
2604 rx_thread[thread].conf.thread_id = thread;
2605 rx_thread[thread].conf.lcore_id = rx_thread_params[i].lcore_id;
2606 rx_thread[thread].rx_queue_list[nb_rx_queue].port_id =
2607 rx_thread_params[i].port_id;
2608 rx_thread[thread].rx_queue_list[nb_rx_queue].queue_id =
2609 rx_thread_params[i].queue_id;
2610 rx_thread[thread].n_rx_queue++;
2612 if (thread >= n_rx_thread)
2613 n_rx_thread = thread + 1;
2620 init_tx_threads(void)
2625 for (i = 0; i < nb_tx_thread_params; ++i) {
2626 tx_thread[n_tx_thread].conf.thread_id = tx_thread_params[i].thread_id;
2627 tx_thread[n_tx_thread].conf.lcore_id = tx_thread_params[i].lcore_id;
2635 print_usage(const char *prgname)
2637 printf("%s [EAL options] -- -p PORTMASK -P"
2638 " [--rx (port,queue,lcore,thread)[,(port,queue,lcore,thread]]"
2639 " [--tx (lcore,thread)[,(lcore,thread]]"
2640 " [--max-pkt-len PKTLEN]"
2641 " [--parse-ptype]\n\n"
2642 " -p PORTMASK: hexadecimal bitmask of ports to configure\n"
2643 " -P : enable promiscuous mode\n"
2644 " --rx (port,queue,lcore,thread): rx queues configuration\n"
2645 " --tx (lcore,thread): tx threads configuration\n"
2646 " --stat-lcore LCORE: use lcore for stat collector\n"
2647 " --eth-dest=X,MM:MM:MM:MM:MM:MM: optional, ethernet destination for port X\n"
2648 " --no-numa: optional, disable numa awareness\n"
2649 " --ipv6: optional, specify it if running ipv6 packets\n"
2650 " --max-pkt-len PKTLEN: maximum packet length in decimal (64-9600)\n"
2651 " --hash-entry-num: specify the hash entry number in hexadecimal to be setup\n"
2652 " --no-lthreads: turn off lthread model\n"
2653 " --parse-ptype: set to use software to analyze packet type\n\n",
2657 static int parse_max_pkt_len(const char *pktlen)
2662 /* parse decimal string */
2663 len = strtoul(pktlen, &end, 10);
2664 if ((pktlen[0] == '\0') || (end == NULL) || (*end != '\0'))
2674 parse_portmask(const char *portmask)
2679 /* parse hexadecimal string */
2680 pm = strtoul(portmask, &end, 16);
2681 if ((portmask[0] == '\0') || (end == NULL) || (*end != '\0'))
2687 #if (APP_LOOKUP_METHOD == APP_LOOKUP_EXACT_MATCH)
2689 parse_hash_entry_number(const char *hash_entry_num)
2692 unsigned long hash_en;
2694 /* parse hexadecimal string */
2695 hash_en = strtoul(hash_entry_num, &end, 16);
2696 if ((hash_entry_num[0] == '\0') || (end == NULL) || (*end != '\0'))
2707 parse_rx_config(const char *q_arg)
2710 const char *p, *p0 = q_arg;
2719 unsigned long int_fld[_NUM_FLD];
2720 char *str_fld[_NUM_FLD];
2724 nb_rx_thread_params = 0;
2726 while ((p = strchr(p0, '(')) != NULL) {
2728 p0 = strchr(p, ')');
2733 if (size >= sizeof(s))
2736 snprintf(s, sizeof(s), "%.*s", size, p);
2737 if (rte_strsplit(s, sizeof(s), str_fld, _NUM_FLD, ',') != _NUM_FLD)
2739 for (i = 0; i < _NUM_FLD; i++) {
2741 int_fld[i] = strtoul(str_fld[i], &end, 0);
2742 if (errno != 0 || end == str_fld[i] || int_fld[i] > 255)
2745 if (nb_rx_thread_params >= MAX_LCORE_PARAMS) {
2746 printf("exceeded max number of rx params: %hu\n",
2747 nb_rx_thread_params);
2750 rx_thread_params_array[nb_rx_thread_params].port_id =
2752 rx_thread_params_array[nb_rx_thread_params].queue_id =
2753 (uint8_t)int_fld[FLD_QUEUE];
2754 rx_thread_params_array[nb_rx_thread_params].lcore_id =
2755 (uint8_t)int_fld[FLD_LCORE];
2756 rx_thread_params_array[nb_rx_thread_params].thread_id =
2757 (uint8_t)int_fld[FLD_THREAD];
2758 ++nb_rx_thread_params;
2760 rx_thread_params = rx_thread_params_array;
2765 parse_tx_config(const char *q_arg)
2768 const char *p, *p0 = q_arg;
2775 unsigned long int_fld[_NUM_FLD];
2776 char *str_fld[_NUM_FLD];
2780 nb_tx_thread_params = 0;
2782 while ((p = strchr(p0, '(')) != NULL) {
2784 p0 = strchr(p, ')');
2789 if (size >= sizeof(s))
2792 snprintf(s, sizeof(s), "%.*s", size, p);
2793 if (rte_strsplit(s, sizeof(s), str_fld, _NUM_FLD, ',') != _NUM_FLD)
2795 for (i = 0; i < _NUM_FLD; i++) {
2797 int_fld[i] = strtoul(str_fld[i], &end, 0);
2798 if (errno != 0 || end == str_fld[i] || int_fld[i] > 255)
2801 if (nb_tx_thread_params >= MAX_LCORE_PARAMS) {
2802 printf("exceeded max number of tx params: %hu\n",
2803 nb_tx_thread_params);
2806 tx_thread_params_array[nb_tx_thread_params].lcore_id =
2807 (uint8_t)int_fld[FLD_LCORE];
2808 tx_thread_params_array[nb_tx_thread_params].thread_id =
2809 (uint8_t)int_fld[FLD_THREAD];
2810 ++nb_tx_thread_params;
2812 tx_thread_params = tx_thread_params_array;
2817 #if (APP_CPU_LOAD > 0)
2819 parse_stat_lcore(const char *stat_lcore)
2822 unsigned long lcore_id;
2824 lcore_id = strtoul(stat_lcore, &end, 10);
2825 if ((stat_lcore[0] == '\0') || (end == NULL) || (*end != '\0'))
2833 parse_eth_dest(const char *optarg)
2837 uint8_t c, *dest, peer_addr[6];
2840 portid = strtoul(optarg, &port_end, 10);
2841 if (errno != 0 || port_end == optarg || *port_end++ != ',')
2842 rte_exit(EXIT_FAILURE,
2843 "Invalid eth-dest: %s", optarg);
2844 if (portid >= RTE_MAX_ETHPORTS)
2845 rte_exit(EXIT_FAILURE,
2846 "eth-dest: port %d >= RTE_MAX_ETHPORTS(%d)\n",
2847 portid, RTE_MAX_ETHPORTS);
2849 if (cmdline_parse_etheraddr(NULL, port_end,
2850 &peer_addr, sizeof(peer_addr)) < 0)
2851 rte_exit(EXIT_FAILURE,
2852 "Invalid ethernet address: %s\n",
2854 dest = (uint8_t *)&dest_eth_addr[portid];
2855 for (c = 0; c < 6; c++)
2856 dest[c] = peer_addr[c];
2857 *(uint64_t *)(val_eth + portid) = dest_eth_addr[portid];
2861 #define OPT_RX_CONFIG "rx"
2862 OPT_RX_CONFIG_NUM = 256,
2863 #define OPT_TX_CONFIG "tx"
2865 #define OPT_STAT_LCORE "stat-lcore"
2867 #define OPT_ETH_DEST "eth-dest"
2869 #define OPT_NO_NUMA "no-numa"
2871 #define OPT_IPV6 "ipv6"
2873 #define OPT_MAX_PKT_LEN "max-pkt-len"
2874 OPT_MAX_PKT_LEN_NUM,
2875 #define OPT_HASH_ENTRY_NUM "hash-entry-num"
2876 OPT_HASH_ENTRY_NUM_NUM,
2877 #define OPT_NO_LTHREADS "no-lthreads"
2878 OPT_NO_LTHREADS_NUM,
2879 #define OPT_PARSE_PTYPE "parse-ptype"
2880 OPT_PARSE_PTYPE_NUM,
2883 /* Parse the argument given in the command line of the application */
2885 parse_args(int argc, char **argv)
2890 char *prgname = argv[0];
2891 static struct option lgopts[] = {
2892 {OPT_RX_CONFIG, 1, NULL, OPT_RX_CONFIG_NUM },
2893 {OPT_TX_CONFIG, 1, NULL, OPT_TX_CONFIG_NUM },
2894 {OPT_STAT_LCORE, 1, NULL, OPT_STAT_LCORE_NUM },
2895 {OPT_ETH_DEST, 1, NULL, OPT_ETH_DEST_NUM },
2896 {OPT_NO_NUMA, 0, NULL, OPT_NO_NUMA_NUM },
2897 {OPT_IPV6, 0, NULL, OPT_IPV6_NUM },
2898 {OPT_MAX_PKT_LEN, 1, NULL, OPT_MAX_PKT_LEN_NUM },
2899 {OPT_HASH_ENTRY_NUM, 1, NULL, OPT_HASH_ENTRY_NUM_NUM },
2900 {OPT_NO_LTHREADS, 0, NULL, OPT_NO_LTHREADS_NUM },
2901 {OPT_PARSE_PTYPE, 0, NULL, OPT_PARSE_PTYPE_NUM },
2907 while ((opt = getopt_long(argc, argvopt, "p:P",
2908 lgopts, &option_index)) != EOF) {
2913 enabled_port_mask = parse_portmask(optarg);
2914 if (enabled_port_mask == 0) {
2915 printf("invalid portmask\n");
2916 print_usage(prgname);
2922 printf("Promiscuous mode selected\n");
2927 case OPT_RX_CONFIG_NUM:
2928 ret = parse_rx_config(optarg);
2930 printf("invalid rx-config\n");
2931 print_usage(prgname);
2936 case OPT_TX_CONFIG_NUM:
2937 ret = parse_tx_config(optarg);
2939 printf("invalid tx-config\n");
2940 print_usage(prgname);
2945 #if (APP_CPU_LOAD > 0)
2946 case OPT_STAT_LCORE_NUM:
2947 cpu_load_lcore_id = parse_stat_lcore(optarg);
2951 case OPT_ETH_DEST_NUM:
2952 parse_eth_dest(optarg);
2955 case OPT_NO_NUMA_NUM:
2956 printf("numa is disabled\n");
2960 #if (APP_LOOKUP_METHOD == APP_LOOKUP_EXACT_MATCH)
2962 printf("ipv6 is specified\n");
2967 case OPT_NO_LTHREADS_NUM:
2968 printf("l-threads model is disabled\n");
2972 case OPT_PARSE_PTYPE_NUM:
2973 printf("software packet type parsing enabled\n");
2977 case OPT_MAX_PKT_LEN_NUM:
2978 max_pkt_len = parse_max_pkt_len(optarg);
2981 #if (APP_LOOKUP_METHOD == APP_LOOKUP_EXACT_MATCH)
2982 case OPT_HASH_ENTRY_NUM_NUM:
2983 ret = parse_hash_entry_number(optarg);
2984 if ((ret > 0) && (ret <= L3FWD_HASH_ENTRIES)) {
2985 hash_entry_number = ret;
2987 printf("invalid hash entry number\n");
2988 print_usage(prgname);
2995 print_usage(prgname);
3001 argv[optind-1] = prgname;
3004 optind = 1; /* reset getopt lib */
3009 print_ethaddr(const char *name, const struct rte_ether_addr *eth_addr)
3011 char buf[RTE_ETHER_ADDR_FMT_SIZE];
3013 rte_ether_format_addr(buf, RTE_ETHER_ADDR_FMT_SIZE, eth_addr);
3014 printf("%s%s", name, buf);
3017 #if (APP_LOOKUP_METHOD == APP_LOOKUP_EXACT_MATCH)
3019 static void convert_ipv4_5tuple(struct ipv4_5tuple *key1,
3020 union ipv4_5tuple_host *key2)
3022 key2->ip_dst = rte_cpu_to_be_32(key1->ip_dst);
3023 key2->ip_src = rte_cpu_to_be_32(key1->ip_src);
3024 key2->port_dst = rte_cpu_to_be_16(key1->port_dst);
3025 key2->port_src = rte_cpu_to_be_16(key1->port_src);
3026 key2->proto = key1->proto;
3031 static void convert_ipv6_5tuple(struct ipv6_5tuple *key1,
3032 union ipv6_5tuple_host *key2)
3036 for (i = 0; i < 16; i++) {
3037 key2->ip_dst[i] = key1->ip_dst[i];
3038 key2->ip_src[i] = key1->ip_src[i];
3040 key2->port_dst = rte_cpu_to_be_16(key1->port_dst);
3041 key2->port_src = rte_cpu_to_be_16(key1->port_src);
3042 key2->proto = key1->proto;
3048 #define BYTE_VALUE_MAX 256
3049 #define ALL_32_BITS 0xffffffff
3050 #define BIT_8_TO_15 0x0000ff00
3052 populate_ipv4_few_flow_into_table(const struct rte_hash *h)
3056 uint32_t array_len = RTE_DIM(ipv4_l3fwd_route_array);
3058 mask0 = _mm_set_epi32(ALL_32_BITS, ALL_32_BITS, ALL_32_BITS, BIT_8_TO_15);
3059 for (i = 0; i < array_len; i++) {
3060 struct ipv4_l3fwd_route entry;
3061 union ipv4_5tuple_host newkey;
3063 entry = ipv4_l3fwd_route_array[i];
3064 convert_ipv4_5tuple(&entry.key, &newkey);
3065 ret = rte_hash_add_key(h, (void *)&newkey);
3067 rte_exit(EXIT_FAILURE, "Unable to add entry %" PRIu32
3068 " to the l3fwd hash.\n", i);
3070 ipv4_l3fwd_out_if[ret] = entry.if_out;
3072 printf("Hash: Adding 0x%" PRIx32 " keys\n", array_len);
3075 #define BIT_16_TO_23 0x00ff0000
3077 populate_ipv6_few_flow_into_table(const struct rte_hash *h)
3081 uint32_t array_len = RTE_DIM(ipv6_l3fwd_route_array);
3083 mask1 = _mm_set_epi32(ALL_32_BITS, ALL_32_BITS, ALL_32_BITS, BIT_16_TO_23);
3084 mask2 = _mm_set_epi32(0, 0, ALL_32_BITS, ALL_32_BITS);
3085 for (i = 0; i < array_len; i++) {
3086 struct ipv6_l3fwd_route entry;
3087 union ipv6_5tuple_host newkey;
3089 entry = ipv6_l3fwd_route_array[i];
3090 convert_ipv6_5tuple(&entry.key, &newkey);
3091 ret = rte_hash_add_key(h, (void *)&newkey);
3093 rte_exit(EXIT_FAILURE, "Unable to add entry %" PRIu32
3094 " to the l3fwd hash.\n", i);
3096 ipv6_l3fwd_out_if[ret] = entry.if_out;
3098 printf("Hash: Adding 0x%" PRIx32 "keys\n", array_len);
3101 #define NUMBER_PORT_USED 4
3103 populate_ipv4_many_flow_into_table(const struct rte_hash *h,
3104 unsigned int nr_flow)
3108 mask0 = _mm_set_epi32(ALL_32_BITS, ALL_32_BITS, ALL_32_BITS, BIT_8_TO_15);
3110 for (i = 0; i < nr_flow; i++) {
3111 struct ipv4_l3fwd_route entry;
3112 union ipv4_5tuple_host newkey;
3113 uint8_t a = (uint8_t)((i / NUMBER_PORT_USED) % BYTE_VALUE_MAX);
3114 uint8_t b = (uint8_t)(((i / NUMBER_PORT_USED) / BYTE_VALUE_MAX) %
3116 uint8_t c = (uint8_t)((i / NUMBER_PORT_USED) / (BYTE_VALUE_MAX *
3118 /* Create the ipv4 exact match flow */
3119 memset(&entry, 0, sizeof(entry));
3120 switch (i & (NUMBER_PORT_USED - 1)) {
3122 entry = ipv4_l3fwd_route_array[0];
3123 entry.key.ip_dst = RTE_IPV4(101, c, b, a);
3126 entry = ipv4_l3fwd_route_array[1];
3127 entry.key.ip_dst = RTE_IPV4(201, c, b, a);
3130 entry = ipv4_l3fwd_route_array[2];
3131 entry.key.ip_dst = RTE_IPV4(111, c, b, a);
3134 entry = ipv4_l3fwd_route_array[3];
3135 entry.key.ip_dst = RTE_IPV4(211, c, b, a);
3138 convert_ipv4_5tuple(&entry.key, &newkey);
3139 int32_t ret = rte_hash_add_key(h, (void *)&newkey);
3142 rte_exit(EXIT_FAILURE, "Unable to add entry %u\n", i);
3144 ipv4_l3fwd_out_if[ret] = (uint8_t)entry.if_out;
3147 printf("Hash: Adding 0x%x keys\n", nr_flow);
3151 populate_ipv6_many_flow_into_table(const struct rte_hash *h,
3152 unsigned int nr_flow)
3156 mask1 = _mm_set_epi32(ALL_32_BITS, ALL_32_BITS, ALL_32_BITS, BIT_16_TO_23);
3157 mask2 = _mm_set_epi32(0, 0, ALL_32_BITS, ALL_32_BITS);
3158 for (i = 0; i < nr_flow; i++) {
3159 struct ipv6_l3fwd_route entry;
3160 union ipv6_5tuple_host newkey;
3162 uint8_t a = (uint8_t) ((i / NUMBER_PORT_USED) % BYTE_VALUE_MAX);
3163 uint8_t b = (uint8_t) (((i / NUMBER_PORT_USED) / BYTE_VALUE_MAX) %
3165 uint8_t c = (uint8_t) ((i / NUMBER_PORT_USED) / (BYTE_VALUE_MAX *
3168 /* Create the ipv6 exact match flow */
3169 memset(&entry, 0, sizeof(entry));
3170 switch (i & (NUMBER_PORT_USED - 1)) {
3172 entry = ipv6_l3fwd_route_array[0];
3175 entry = ipv6_l3fwd_route_array[1];
3178 entry = ipv6_l3fwd_route_array[2];
3181 entry = ipv6_l3fwd_route_array[3];
3184 entry.key.ip_dst[13] = c;
3185 entry.key.ip_dst[14] = b;
3186 entry.key.ip_dst[15] = a;
3187 convert_ipv6_5tuple(&entry.key, &newkey);
3188 int32_t ret = rte_hash_add_key(h, (void *)&newkey);
3191 rte_exit(EXIT_FAILURE, "Unable to add entry %u\n", i);
3193 ipv6_l3fwd_out_if[ret] = (uint8_t) entry.if_out;
3196 printf("Hash: Adding 0x%x keys\n", nr_flow);
3200 setup_hash(int socketid)
3202 struct rte_hash_parameters ipv4_l3fwd_hash_params = {
3204 .entries = L3FWD_HASH_ENTRIES,
3205 .key_len = sizeof(union ipv4_5tuple_host),
3206 .hash_func = ipv4_hash_crc,
3207 .hash_func_init_val = 0,
3210 struct rte_hash_parameters ipv6_l3fwd_hash_params = {
3212 .entries = L3FWD_HASH_ENTRIES,
3213 .key_len = sizeof(union ipv6_5tuple_host),
3214 .hash_func = ipv6_hash_crc,
3215 .hash_func_init_val = 0,
3220 /* create ipv4 hash */
3221 snprintf(s, sizeof(s), "ipv4_l3fwd_hash_%d", socketid);
3222 ipv4_l3fwd_hash_params.name = s;
3223 ipv4_l3fwd_hash_params.socket_id = socketid;
3224 ipv4_l3fwd_lookup_struct[socketid] =
3225 rte_hash_create(&ipv4_l3fwd_hash_params);
3226 if (ipv4_l3fwd_lookup_struct[socketid] == NULL)
3227 rte_exit(EXIT_FAILURE, "Unable to create the l3fwd hash on "
3228 "socket %d\n", socketid);
3230 /* create ipv6 hash */
3231 snprintf(s, sizeof(s), "ipv6_l3fwd_hash_%d", socketid);
3232 ipv6_l3fwd_hash_params.name = s;
3233 ipv6_l3fwd_hash_params.socket_id = socketid;
3234 ipv6_l3fwd_lookup_struct[socketid] =
3235 rte_hash_create(&ipv6_l3fwd_hash_params);
3236 if (ipv6_l3fwd_lookup_struct[socketid] == NULL)
3237 rte_exit(EXIT_FAILURE, "Unable to create the l3fwd hash on "
3238 "socket %d\n", socketid);
3240 if (hash_entry_number != HASH_ENTRY_NUMBER_DEFAULT) {
3241 /* For testing hash matching with a large number of flows we
3242 * generate millions of IP 5-tuples with an incremented dst
3243 * address to initialize the hash table. */
3245 /* populate the ipv4 hash */
3246 populate_ipv4_many_flow_into_table(
3247 ipv4_l3fwd_lookup_struct[socketid], hash_entry_number);
3249 /* populate the ipv6 hash */
3250 populate_ipv6_many_flow_into_table(
3251 ipv6_l3fwd_lookup_struct[socketid], hash_entry_number);
3254 /* Use data in ipv4/ipv6 l3fwd lookup table directly to initialize
3257 /* populate the ipv4 hash */
3258 populate_ipv4_few_flow_into_table(
3259 ipv4_l3fwd_lookup_struct[socketid]);
3261 /* populate the ipv6 hash */
3262 populate_ipv6_few_flow_into_table(
3263 ipv6_l3fwd_lookup_struct[socketid]);
3269 #if (APP_LOOKUP_METHOD == APP_LOOKUP_LPM)
3271 setup_lpm(int socketid)
3273 struct rte_lpm6_config config;
3274 struct rte_lpm_config lpm_ipv4_config;
3279 /* create the LPM table */
3280 snprintf(s, sizeof(s), "IPV4_L3FWD_LPM_%d", socketid);
3281 lpm_ipv4_config.max_rules = IPV4_L3FWD_LPM_MAX_RULES;
3282 lpm_ipv4_config.number_tbl8s = 256;
3283 lpm_ipv4_config.flags = 0;
3284 ipv4_l3fwd_lookup_struct[socketid] =
3285 rte_lpm_create(s, socketid, &lpm_ipv4_config);
3286 if (ipv4_l3fwd_lookup_struct[socketid] == NULL)
3287 rte_exit(EXIT_FAILURE, "Unable to create the l3fwd LPM table"
3288 " on socket %d\n", socketid);
3290 /* populate the LPM table */
3291 for (i = 0; i < IPV4_L3FWD_NUM_ROUTES; i++) {
3293 /* skip unused ports */
3294 if ((1 << ipv4_l3fwd_route_array[i].if_out &
3295 enabled_port_mask) == 0)
3298 ret = rte_lpm_add(ipv4_l3fwd_lookup_struct[socketid],
3299 ipv4_l3fwd_route_array[i].ip,
3300 ipv4_l3fwd_route_array[i].depth,
3301 ipv4_l3fwd_route_array[i].if_out);
3304 rte_exit(EXIT_FAILURE, "Unable to add entry %u to the "
3305 "l3fwd LPM table on socket %d\n",
3309 printf("LPM: Adding route 0x%08x / %d (%d)\n",
3310 (unsigned)ipv4_l3fwd_route_array[i].ip,
3311 ipv4_l3fwd_route_array[i].depth,
3312 ipv4_l3fwd_route_array[i].if_out);
3315 /* create the LPM6 table */
3316 snprintf(s, sizeof(s), "IPV6_L3FWD_LPM_%d", socketid);
3318 config.max_rules = IPV6_L3FWD_LPM_MAX_RULES;
3319 config.number_tbl8s = IPV6_L3FWD_LPM_NUMBER_TBL8S;
3321 ipv6_l3fwd_lookup_struct[socketid] = rte_lpm6_create(s, socketid,
3323 if (ipv6_l3fwd_lookup_struct[socketid] == NULL)
3324 rte_exit(EXIT_FAILURE, "Unable to create the l3fwd LPM table"
3325 " on socket %d\n", socketid);
3327 /* populate the LPM table */
3328 for (i = 0; i < IPV6_L3FWD_NUM_ROUTES; i++) {
3330 /* skip unused ports */
3331 if ((1 << ipv6_l3fwd_route_array[i].if_out &
3332 enabled_port_mask) == 0)
3335 ret = rte_lpm6_add(ipv6_l3fwd_lookup_struct[socketid],
3336 ipv6_l3fwd_route_array[i].ip,
3337 ipv6_l3fwd_route_array[i].depth,
3338 ipv6_l3fwd_route_array[i].if_out);
3341 rte_exit(EXIT_FAILURE, "Unable to add entry %u to the "
3342 "l3fwd LPM table on socket %d\n",
3346 printf("LPM: Adding route %s / %d (%d)\n",
3348 ipv6_l3fwd_route_array[i].depth,
3349 ipv6_l3fwd_route_array[i].if_out);
3355 init_mem(unsigned nb_mbuf)
3357 struct lcore_conf *qconf;
3362 for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
3363 if (rte_lcore_is_enabled(lcore_id) == 0)
3367 socketid = rte_lcore_to_socket_id(lcore_id);
3371 if (socketid >= NB_SOCKETS) {
3372 rte_exit(EXIT_FAILURE, "Socket %d of lcore %u is out of range %d\n",
3373 socketid, lcore_id, NB_SOCKETS);
3375 if (pktmbuf_pool[socketid] == NULL) {
3376 snprintf(s, sizeof(s), "mbuf_pool_%d", socketid);
3377 pktmbuf_pool[socketid] =
3378 rte_pktmbuf_pool_create(s, nb_mbuf,
3379 MEMPOOL_CACHE_SIZE, 0,
3380 RTE_MBUF_DEFAULT_BUF_SIZE, socketid);
3381 if (pktmbuf_pool[socketid] == NULL)
3382 rte_exit(EXIT_FAILURE,
3383 "Cannot init mbuf pool on socket %d\n", socketid);
3385 printf("Allocated mbuf pool on socket %d\n", socketid);
3387 #if (APP_LOOKUP_METHOD == APP_LOOKUP_LPM)
3388 setup_lpm(socketid);
3390 setup_hash(socketid);
3393 qconf = &lcore_conf[lcore_id];
3394 qconf->ipv4_lookup_struct = ipv4_l3fwd_lookup_struct[socketid];
3395 qconf->ipv6_lookup_struct = ipv6_l3fwd_lookup_struct[socketid];
3400 /* Check the link status of all ports in up to 9s, and print them finally */
3402 check_all_ports_link_status(uint32_t port_mask)
3404 #define CHECK_INTERVAL 100 /* 100ms */
3405 #define MAX_CHECK_TIME 90 /* 9s (90 * 100ms) in total */
3407 uint8_t count, all_ports_up, print_flag = 0;
3408 struct rte_eth_link link;
3410 char link_status_text[RTE_ETH_LINK_MAX_STR_LEN];
3412 printf("\nChecking link status");
3414 for (count = 0; count <= MAX_CHECK_TIME; count++) {
3416 RTE_ETH_FOREACH_DEV(portid) {
3417 if ((port_mask & (1 << portid)) == 0)
3419 memset(&link, 0, sizeof(link));
3420 ret = rte_eth_link_get_nowait(portid, &link);
3423 if (print_flag == 1)
3424 printf("Port %u link get failed: %s\n",
3425 portid, rte_strerror(-ret));
3428 /* print link status if flag set */
3429 if (print_flag == 1) {
3430 rte_eth_link_to_str(link_status_text,
3431 sizeof(link_status_text), &link);
3432 printf("Port %d %s\n", portid,
3436 /* clear all_ports_up flag if any link down */
3437 if (link.link_status == RTE_ETH_LINK_DOWN) {
3442 /* after finally printing all link status, get out */
3443 if (print_flag == 1)
3446 if (all_ports_up == 0) {
3449 rte_delay_ms(CHECK_INTERVAL);
3452 /* set the print_flag if all ports up or timeout */
3453 if (all_ports_up == 1 || count == (MAX_CHECK_TIME - 1)) {
3461 eth_dev_get_overhead_len(uint32_t max_rx_pktlen, uint16_t max_mtu)
3463 uint32_t overhead_len;
3465 if (max_mtu != UINT16_MAX && max_rx_pktlen > max_mtu)
3466 overhead_len = max_rx_pktlen - max_mtu;
3468 overhead_len = RTE_ETHER_HDR_LEN + RTE_ETHER_CRC_LEN;
3470 return overhead_len;
3474 config_port_max_pkt_len(struct rte_eth_conf *conf,
3475 struct rte_eth_dev_info *dev_info)
3477 uint32_t overhead_len;
3479 if (max_pkt_len == 0)
3482 if (max_pkt_len < RTE_ETHER_MIN_LEN || max_pkt_len > MAX_JUMBO_PKT_LEN)
3485 overhead_len = eth_dev_get_overhead_len(dev_info->max_rx_pktlen,
3487 conf->rxmode.mtu = max_pkt_len - overhead_len;
3489 if (conf->rxmode.mtu > RTE_ETHER_MTU)
3490 conf->txmode.offloads |= RTE_ETH_TX_OFFLOAD_MULTI_SEGS;
3496 main(int argc, char **argv)
3498 struct rte_eth_dev_info dev_info;
3499 struct rte_eth_txconf *txconf;
3503 uint16_t queueid, portid;
3505 uint32_t n_tx_queue, nb_lcores;
3506 uint8_t nb_rx_queue, queue, socketid;
3509 ret = rte_eal_init(argc, argv);
3511 rte_exit(EXIT_FAILURE, "Invalid EAL parameters\n");
3515 ret = rte_timer_subsystem_init();
3517 rte_exit(EXIT_FAILURE, "Failed to initialize timer subystem\n");
3519 /* pre-init dst MACs for all ports to 02:00:00:00:00:xx */
3520 for (portid = 0; portid < RTE_MAX_ETHPORTS; portid++) {
3521 dest_eth_addr[portid] = RTE_ETHER_LOCAL_ADMIN_ADDR +
3522 ((uint64_t)portid << 40);
3523 *(uint64_t *)(val_eth + portid) = dest_eth_addr[portid];
3526 /* parse application arguments (after the EAL ones) */
3527 ret = parse_args(argc, argv);
3529 rte_exit(EXIT_FAILURE, "Invalid L3FWD parameters\n");
3531 if (check_lcore_params() < 0)
3532 rte_exit(EXIT_FAILURE, "check_lcore_params failed\n");
3534 printf("Initializing rx-queues...\n");
3535 ret = init_rx_queues();
3537 rte_exit(EXIT_FAILURE, "init_rx_queues failed\n");
3539 printf("Initializing tx-threads...\n");
3540 ret = init_tx_threads();
3542 rte_exit(EXIT_FAILURE, "init_tx_threads failed\n");
3544 printf("Initializing rings...\n");
3545 ret = init_rx_rings();
3547 rte_exit(EXIT_FAILURE, "init_rx_rings failed\n");
3549 nb_ports = rte_eth_dev_count_avail();
3551 if (check_port_config() < 0)
3552 rte_exit(EXIT_FAILURE, "check_port_config failed\n");
3554 nb_lcores = rte_lcore_count();
3556 /* initialize all ports */
3557 RTE_ETH_FOREACH_DEV(portid) {
3558 struct rte_eth_conf local_port_conf = port_conf;
3560 /* skip ports that are not enabled */
3561 if ((enabled_port_mask & (1 << portid)) == 0) {
3562 printf("\nSkipping disabled port %d\n", portid);
3567 printf("Initializing port %d ... ", portid);
3570 nb_rx_queue = get_port_n_rx_queues(portid);
3571 n_tx_queue = nb_lcores;
3572 if (n_tx_queue > MAX_TX_QUEUE_PER_PORT)
3573 n_tx_queue = MAX_TX_QUEUE_PER_PORT;
3574 printf("Creating queues: nb_rxq=%d nb_txq=%u... ",
3575 nb_rx_queue, (unsigned)n_tx_queue);
3577 ret = rte_eth_dev_info_get(portid, &dev_info);
3579 rte_exit(EXIT_FAILURE,
3580 "Error during getting device (port %u) info: %s\n",
3581 portid, strerror(-ret));
3583 ret = config_port_max_pkt_len(&local_port_conf, &dev_info);
3585 rte_exit(EXIT_FAILURE,
3586 "Invalid max packet length: %u (port %u)\n",
3587 max_pkt_len, portid);
3589 if (dev_info.tx_offload_capa & RTE_ETH_TX_OFFLOAD_MBUF_FAST_FREE)
3590 local_port_conf.txmode.offloads |=
3591 RTE_ETH_TX_OFFLOAD_MBUF_FAST_FREE;
3593 local_port_conf.rx_adv_conf.rss_conf.rss_hf &=
3594 dev_info.flow_type_rss_offloads;
3595 if (local_port_conf.rx_adv_conf.rss_conf.rss_hf !=
3596 port_conf.rx_adv_conf.rss_conf.rss_hf) {
3597 printf("Port %u modified RSS hash function based on hardware support,"
3598 "requested:%#"PRIx64" configured:%#"PRIx64"\n",
3600 port_conf.rx_adv_conf.rss_conf.rss_hf,
3601 local_port_conf.rx_adv_conf.rss_conf.rss_hf);
3604 ret = rte_eth_dev_configure(portid, nb_rx_queue,
3605 (uint16_t)n_tx_queue, &local_port_conf);
3607 rte_exit(EXIT_FAILURE, "Cannot configure device: err=%d, port=%d\n",
3610 ret = rte_eth_dev_adjust_nb_rx_tx_desc(portid, &nb_rxd,
3613 rte_exit(EXIT_FAILURE,
3614 "rte_eth_dev_adjust_nb_rx_tx_desc: err=%d, port=%d\n",
3617 ret = rte_eth_macaddr_get(portid, &ports_eth_addr[portid]);
3619 rte_exit(EXIT_FAILURE,
3620 "rte_eth_macaddr_get: err=%d, port=%d\n",
3623 print_ethaddr(" Address:", &ports_eth_addr[portid]);
3625 print_ethaddr("Destination:",
3626 (const struct rte_ether_addr *)&dest_eth_addr[portid]);
3630 * prepare src MACs for each port.
3632 rte_ether_addr_copy(&ports_eth_addr[portid],
3633 (struct rte_ether_addr *)(val_eth + portid) + 1);
3636 ret = init_mem(NB_MBUF);
3638 rte_exit(EXIT_FAILURE, "init_mem failed\n");
3640 /* init one TX queue per couple (lcore,port) */
3642 for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
3643 if (rte_lcore_is_enabled(lcore_id) == 0)
3647 socketid = (uint8_t)rte_lcore_to_socket_id(lcore_id);
3651 printf("txq=%u,%d,%d ", lcore_id, queueid, socketid);
3654 txconf = &dev_info.default_txconf;
3655 txconf->offloads = local_port_conf.txmode.offloads;
3656 ret = rte_eth_tx_queue_setup(portid, queueid, nb_txd,
3659 rte_exit(EXIT_FAILURE, "rte_eth_tx_queue_setup: err=%d, "
3660 "port=%d\n", ret, portid);
3662 tx_thread[lcore_id].tx_queue_id[portid] = queueid;
3668 for (i = 0; i < n_rx_thread; i++) {
3669 lcore_id = rx_thread[i].conf.lcore_id;
3671 if (rte_lcore_is_enabled(lcore_id) == 0) {
3672 rte_exit(EXIT_FAILURE,
3673 "Cannot start Rx thread on lcore %u: lcore disabled\n",
3678 printf("\nInitializing rx queues for Rx thread %d on lcore %u ... ",
3682 /* init RX queues */
3683 for (queue = 0; queue < rx_thread[i].n_rx_queue; ++queue) {
3684 struct rte_eth_rxconf rxq_conf;
3686 portid = rx_thread[i].rx_queue_list[queue].port_id;
3687 queueid = rx_thread[i].rx_queue_list[queue].queue_id;
3690 socketid = (uint8_t)rte_lcore_to_socket_id(lcore_id);
3694 printf("rxq=%d,%d,%d ", portid, queueid, socketid);
3697 ret = rte_eth_dev_info_get(portid, &dev_info);
3699 rte_exit(EXIT_FAILURE,
3700 "Error during getting device (port %u) info: %s\n",
3701 portid, strerror(-ret));
3703 rxq_conf = dev_info.default_rxconf;
3704 rxq_conf.offloads = port_conf.rxmode.offloads;
3705 ret = rte_eth_rx_queue_setup(portid, queueid, nb_rxd,
3708 pktmbuf_pool[socketid]);
3710 rte_exit(EXIT_FAILURE, "rte_eth_rx_queue_setup: err=%d, "
3711 "port=%d\n", ret, portid);
3718 RTE_ETH_FOREACH_DEV(portid) {
3719 if ((enabled_port_mask & (1 << portid)) == 0)
3723 ret = rte_eth_dev_start(portid);
3725 rte_exit(EXIT_FAILURE, "rte_eth_dev_start: err=%d, port=%d\n",
3729 * If enabled, put device in promiscuous mode.
3730 * This allows IO forwarding mode to forward packets
3731 * to itself through 2 cross-connected ports of the
3734 if (promiscuous_on) {
3735 ret = rte_eth_promiscuous_enable(portid);
3737 rte_exit(EXIT_FAILURE,
3738 "rte_eth_promiscuous_enable: err=%s, port=%u\n",
3739 rte_strerror(-ret), portid);
3743 for (i = 0; i < n_rx_thread; i++) {
3744 lcore_id = rx_thread[i].conf.lcore_id;
3745 if (rte_lcore_is_enabled(lcore_id) == 0)
3748 /* check if hw packet type is supported */
3749 for (queue = 0; queue < rx_thread[i].n_rx_queue; ++queue) {
3750 portid = rx_thread[i].rx_queue_list[queue].port_id;
3751 queueid = rx_thread[i].rx_queue_list[queue].queue_id;
3753 if (parse_ptype_on) {
3754 if (!rte_eth_add_rx_callback(portid, queueid,
3755 cb_parse_ptype, NULL))
3756 rte_exit(EXIT_FAILURE,
3757 "Failed to add rx callback: "
3758 "port=%d\n", portid);
3759 } else if (!check_ptype(portid))
3760 rte_exit(EXIT_FAILURE,
3761 "Port %d cannot parse packet type.\n\n"
3762 "Please add --parse-ptype to use sw "
3763 "packet type analyzer.\n\n",
3768 check_all_ports_link_status(enabled_port_mask);
3771 printf("Starting L-Threading Model\n");
3773 #if (APP_CPU_LOAD > 0)
3774 if (cpu_load_lcore_id > 0)
3775 /* Use one lcore for cpu load collector */
3779 lthread_num_schedulers_set(nb_lcores);
3780 rte_eal_mp_remote_launch(sched_spawner, NULL, SKIP_MAIN);
3781 lthread_main_spawner(NULL);
3784 printf("Starting P-Threading Model\n");
3785 /* launch per-lcore init on every lcore */
3786 rte_eal_mp_remote_launch(pthread_run, NULL, CALL_MAIN);
3787 RTE_LCORE_FOREACH_WORKER(lcore_id) {
3788 if (rte_eal_wait_lcore(lcore_id) < 0)
3793 /* clean up the EAL */