4 * Copyright(c) 2010-2016 Intel Corporation. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
17 * * Neither the name of Intel Corporation nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
40 #include <sys/types.h>
42 #include <sys/queue.h>
47 #include <rte_common.h>
49 #include <rte_byteorder.h>
51 #include <rte_memory.h>
52 #include <rte_memcpy.h>
53 #include <rte_memzone.h>
55 #include <rte_per_lcore.h>
56 #include <rte_launch.h>
57 #include <rte_atomic.h>
58 #include <rte_cycles.h>
59 #include <rte_prefetch.h>
60 #include <rte_lcore.h>
61 #include <rte_per_lcore.h>
62 #include <rte_branch_prediction.h>
63 #include <rte_interrupts.h>
65 #include <rte_random.h>
66 #include <rte_debug.h>
67 #include <rte_ether.h>
68 #include <rte_ethdev.h>
70 #include <rte_mempool.h>
75 #include <rte_string_fns.h>
77 #include <cmdline_parse.h>
78 #include <cmdline_parse_etheraddr.h>
80 #include <lthread_api.h>
82 #define APP_LOOKUP_EXACT_MATCH 0
83 #define APP_LOOKUP_LPM 1
84 #define DO_RFC_1812_CHECKS
86 /* Enable cpu-load stats 0-off, 1-on */
87 #define APP_CPU_LOAD 1
89 #ifndef APP_LOOKUP_METHOD
90 #define APP_LOOKUP_METHOD APP_LOOKUP_LPM
93 #ifndef __GLIBC__ /* sched_getcpu() is glibc specific */
94 #define sched_getcpu() rte_lcore_id()
98 check_ptype(int portid)
101 int ipv4 = 0, ipv6 = 0;
103 ret = rte_eth_dev_get_supported_ptypes(portid, RTE_PTYPE_L3_MASK, NULL,
108 uint32_t ptypes[ret];
110 ret = rte_eth_dev_get_supported_ptypes(portid, RTE_PTYPE_L3_MASK,
112 for (i = 0; i < ret; ++i) {
113 if (ptypes[i] & RTE_PTYPE_L3_IPV4)
115 if (ptypes[i] & RTE_PTYPE_L3_IPV6)
126 parse_ptype(struct rte_mbuf *m)
128 struct ether_hdr *eth_hdr;
129 uint32_t packet_type = RTE_PTYPE_UNKNOWN;
132 eth_hdr = rte_pktmbuf_mtod(m, struct ether_hdr *);
133 ether_type = eth_hdr->ether_type;
134 if (ether_type == rte_cpu_to_be_16(ETHER_TYPE_IPv4))
135 packet_type |= RTE_PTYPE_L3_IPV4_EXT_UNKNOWN;
136 else if (ether_type == rte_cpu_to_be_16(ETHER_TYPE_IPv6))
137 packet_type |= RTE_PTYPE_L3_IPV6_EXT_UNKNOWN;
139 m->packet_type = packet_type;
143 cb_parse_ptype(__rte_unused uint8_t port, __rte_unused uint16_t queue,
144 struct rte_mbuf *pkts[], uint16_t nb_pkts,
145 __rte_unused uint16_t max_pkts, __rte_unused void *user_param)
149 for (i = 0; i < nb_pkts; i++)
150 parse_ptype(pkts[i]);
156 * When set to zero, simple forwaring path is eanbled.
157 * When set to one, optimized forwarding path is enabled.
158 * Note that LPM optimisation path uses SSE4.1 instructions.
160 #if ((APP_LOOKUP_METHOD == APP_LOOKUP_LPM) && !defined(__SSE4_1__))
161 #define ENABLE_MULTI_BUFFER_OPTIMIZE 0
163 #define ENABLE_MULTI_BUFFER_OPTIMIZE 1
166 #if (APP_LOOKUP_METHOD == APP_LOOKUP_EXACT_MATCH)
167 #include <rte_hash.h>
168 #elif (APP_LOOKUP_METHOD == APP_LOOKUP_LPM)
170 #include <rte_lpm6.h>
172 #error "APP_LOOKUP_METHOD set to incorrect value"
175 #define RTE_LOGTYPE_L3FWD RTE_LOGTYPE_USER1
177 #define MAX_JUMBO_PKT_LEN 9600
179 #define IPV6_ADDR_LEN 16
181 #define MEMPOOL_CACHE_SIZE 256
184 * This expression is used to calculate the number of mbufs needed depending on
185 * user input, taking into account memory for rx and tx hardware rings, cache
186 * per lcore and mtable per port per lcore. RTE_MAX is used to ensure that
187 * NB_MBUF never goes below a minimum value of 8192
190 #define NB_MBUF RTE_MAX(\
191 (nb_ports*nb_rx_queue*RTE_TEST_RX_DESC_DEFAULT + \
192 nb_ports*nb_lcores*MAX_PKT_BURST + \
193 nb_ports*n_tx_queue*RTE_TEST_TX_DESC_DEFAULT + \
194 nb_lcores*MEMPOOL_CACHE_SIZE), \
197 #define MAX_PKT_BURST 32
198 #define BURST_TX_DRAIN_US 100 /* TX drain every ~100us */
201 * Try to avoid TX buffering if we have at least MAX_TX_BURST packets to send.
203 #define MAX_TX_BURST (MAX_PKT_BURST / 2)
204 #define BURST_SIZE MAX_TX_BURST
208 /* Configure how many packets ahead to prefetch, when reading packets */
209 #define PREFETCH_OFFSET 3
211 /* Used to mark destination port as 'invalid'. */
212 #define BAD_PORT ((uint16_t)-1)
217 * Configurable number of RX/TX ring descriptors
219 #define RTE_TEST_RX_DESC_DEFAULT 128
220 #define RTE_TEST_TX_DESC_DEFAULT 128
221 static uint16_t nb_rxd = RTE_TEST_RX_DESC_DEFAULT;
222 static uint16_t nb_txd = RTE_TEST_TX_DESC_DEFAULT;
224 /* ethernet addresses of ports */
225 static uint64_t dest_eth_addr[RTE_MAX_ETHPORTS];
226 static struct ether_addr ports_eth_addr[RTE_MAX_ETHPORTS];
228 static __m128i val_eth[RTE_MAX_ETHPORTS];
230 /* replace first 12B of the ethernet header. */
231 #define MASK_ETH 0x3f
233 /* mask of enabled ports */
234 static uint32_t enabled_port_mask;
235 static int promiscuous_on; /**< Set in promiscuous mode off by default. */
236 static int numa_on = 1; /**< NUMA is enabled by default. */
237 static int parse_ptype_on;
239 #if (APP_LOOKUP_METHOD == APP_LOOKUP_EXACT_MATCH)
240 static int ipv6; /**< ipv6 is false by default. */
243 #if (APP_CPU_LOAD == 1)
245 #define MAX_CPU RTE_MAX_LCORE
246 #define CPU_LOAD_TIMEOUT_US (5 * 1000 * 1000) /**< Timeout for collecting 5s */
248 #define CPU_PROCESS 0
250 #define MAX_CPU_COUNTER 2
255 uint64_t hits[MAX_CPU_COUNTER][MAX_CPU];
256 } __rte_cache_aligned;
258 static struct cpu_load cpu_load;
259 static int cpu_load_lcore_id = -1;
261 #define SET_CPU_BUSY(thread, counter) \
262 thread->conf.busy[counter] = 1
264 #define SET_CPU_IDLE(thread, counter) \
265 thread->conf.busy[counter] = 0
267 #define IS_CPU_BUSY(thread, counter) \
268 (thread->conf.busy[counter] > 0)
272 #define SET_CPU_BUSY(thread, counter)
273 #define SET_CPU_IDLE(thread, counter)
274 #define IS_CPU_BUSY(thread, counter) 0
280 struct rte_mbuf *m_table[MAX_PKT_BURST];
283 struct lcore_rx_queue {
286 } __rte_cache_aligned;
288 #define MAX_RX_QUEUE_PER_LCORE 16
289 #define MAX_TX_QUEUE_PER_PORT RTE_MAX_ETHPORTS
290 #define MAX_RX_QUEUE_PER_PORT 128
292 #define MAX_LCORE_PARAMS 1024
293 struct rx_thread_params {
298 } __rte_cache_aligned;
300 static struct rx_thread_params rx_thread_params_array[MAX_LCORE_PARAMS];
301 static struct rx_thread_params rx_thread_params_array_default[] = {
313 static struct rx_thread_params *rx_thread_params =
314 rx_thread_params_array_default;
315 static uint16_t nb_rx_thread_params = RTE_DIM(rx_thread_params_array_default);
317 struct tx_thread_params {
320 } __rte_cache_aligned;
322 static struct tx_thread_params tx_thread_params_array[MAX_LCORE_PARAMS];
323 static struct tx_thread_params tx_thread_params_array_default[] = {
335 static struct tx_thread_params *tx_thread_params =
336 tx_thread_params_array_default;
337 static uint16_t nb_tx_thread_params = RTE_DIM(tx_thread_params_array_default);
339 static struct rte_eth_conf port_conf = {
341 .mq_mode = ETH_MQ_RX_RSS,
342 .max_rx_pkt_len = ETHER_MAX_LEN,
344 .header_split = 0, /**< Header Split disabled */
345 .hw_ip_checksum = 1, /**< IP checksum offload enabled */
346 .hw_vlan_filter = 0, /**< VLAN filtering disabled */
347 .jumbo_frame = 0, /**< Jumbo Frame Support disabled */
348 .hw_strip_crc = 1, /**< CRC stripped by hardware */
353 .rss_hf = ETH_RSS_TCP,
357 .mq_mode = ETH_MQ_TX_NONE,
361 static struct rte_mempool *pktmbuf_pool[NB_SOCKETS];
363 #if (APP_LOOKUP_METHOD == APP_LOOKUP_EXACT_MATCH)
365 #ifdef RTE_MACHINE_CPUFLAG_SSE4_2
366 #include <rte_hash_crc.h>
367 #define DEFAULT_HASH_FUNC rte_hash_crc
369 #include <rte_jhash.h>
370 #define DEFAULT_HASH_FUNC rte_jhash
379 } __attribute__((__packed__));
381 union ipv4_5tuple_host {
394 #define XMM_NUM_IN_IPV6_5TUPLE 3
397 uint8_t ip_dst[IPV6_ADDR_LEN];
398 uint8_t ip_src[IPV6_ADDR_LEN];
402 } __attribute__((__packed__));
404 union ipv6_5tuple_host {
409 uint8_t ip_src[IPV6_ADDR_LEN];
410 uint8_t ip_dst[IPV6_ADDR_LEN];
415 __m128i xmm[XMM_NUM_IN_IPV6_5TUPLE];
418 struct ipv4_l3fwd_route {
419 struct ipv4_5tuple key;
423 struct ipv6_l3fwd_route {
424 struct ipv6_5tuple key;
428 static struct ipv4_l3fwd_route ipv4_l3fwd_route_array[] = {
429 {{IPv4(101, 0, 0, 0), IPv4(100, 10, 0, 1), 101, 11, IPPROTO_TCP}, 0},
430 {{IPv4(201, 0, 0, 0), IPv4(200, 20, 0, 1), 102, 12, IPPROTO_TCP}, 1},
431 {{IPv4(111, 0, 0, 0), IPv4(100, 30, 0, 1), 101, 11, IPPROTO_TCP}, 2},
432 {{IPv4(211, 0, 0, 0), IPv4(200, 40, 0, 1), 102, 12, IPPROTO_TCP}, 3},
435 static struct ipv6_l3fwd_route ipv6_l3fwd_route_array[] = {
437 {0xfe, 0x80, 0, 0, 0, 0, 0, 0, 0x02, 0x1e, 0x67, 0xff, 0xfe, 0, 0, 0},
438 {0xfe, 0x80, 0, 0, 0, 0, 0, 0, 0x02, 0x1b, 0x21, 0xff, 0xfe, 0x91, 0x38,
440 101, 11, IPPROTO_TCP}, 0},
443 {0xfe, 0x90, 0, 0, 0, 0, 0, 0, 0x02, 0x1e, 0x67, 0xff, 0xfe, 0, 0, 0},
444 {0xfe, 0x90, 0, 0, 0, 0, 0, 0, 0x02, 0x1b, 0x21, 0xff, 0xfe, 0x91, 0x38,
446 102, 12, IPPROTO_TCP}, 1},
449 {0xfe, 0xa0, 0, 0, 0, 0, 0, 0, 0x02, 0x1e, 0x67, 0xff, 0xfe, 0, 0, 0},
450 {0xfe, 0xa0, 0, 0, 0, 0, 0, 0, 0x02, 0x1b, 0x21, 0xff, 0xfe, 0x91, 0x38,
452 101, 11, IPPROTO_TCP}, 2},
455 {0xfe, 0xb0, 0, 0, 0, 0, 0, 0, 0x02, 0x1e, 0x67, 0xff, 0xfe, 0, 0, 0},
456 {0xfe, 0xb0, 0, 0, 0, 0, 0, 0, 0x02, 0x1b, 0x21, 0xff, 0xfe, 0x91, 0x38,
458 102, 12, IPPROTO_TCP}, 3},
461 typedef struct rte_hash lookup_struct_t;
462 static lookup_struct_t *ipv4_l3fwd_lookup_struct[NB_SOCKETS];
463 static lookup_struct_t *ipv6_l3fwd_lookup_struct[NB_SOCKETS];
465 #ifdef RTE_ARCH_X86_64
466 /* default to 4 million hash entries (approx) */
467 #define L3FWD_HASH_ENTRIES (1024*1024*4)
469 /* 32-bit has less address-space for hugepage memory, limit to 1M entries */
470 #define L3FWD_HASH_ENTRIES (1024*1024*1)
472 #define HASH_ENTRY_NUMBER_DEFAULT 4
474 static uint32_t hash_entry_number = HASH_ENTRY_NUMBER_DEFAULT;
476 static inline uint32_t
477 ipv4_hash_crc(const void *data, __rte_unused uint32_t data_len,
480 const union ipv4_5tuple_host *k;
486 p = (const uint32_t *)&k->port_src;
488 #ifdef RTE_MACHINE_CPUFLAG_SSE4_2
489 init_val = rte_hash_crc_4byte(t, init_val);
490 init_val = rte_hash_crc_4byte(k->ip_src, init_val);
491 init_val = rte_hash_crc_4byte(k->ip_dst, init_val);
492 init_val = rte_hash_crc_4byte(*p, init_val);
493 #else /* RTE_MACHINE_CPUFLAG_SSE4_2 */
494 init_val = rte_jhash_1word(t, init_val);
495 init_val = rte_jhash_1word(k->ip_src, init_val);
496 init_val = rte_jhash_1word(k->ip_dst, init_val);
497 init_val = rte_jhash_1word(*p, init_val);
498 #endif /* RTE_MACHINE_CPUFLAG_SSE4_2 */
502 static inline uint32_t
503 ipv6_hash_crc(const void *data, __rte_unused uint32_t data_len,
506 const union ipv6_5tuple_host *k;
509 #ifdef RTE_MACHINE_CPUFLAG_SSE4_2
510 const uint32_t *ip_src0, *ip_src1, *ip_src2, *ip_src3;
511 const uint32_t *ip_dst0, *ip_dst1, *ip_dst2, *ip_dst3;
512 #endif /* RTE_MACHINE_CPUFLAG_SSE4_2 */
516 p = (const uint32_t *)&k->port_src;
518 #ifdef RTE_MACHINE_CPUFLAG_SSE4_2
519 ip_src0 = (const uint32_t *) k->ip_src;
520 ip_src1 = (const uint32_t *)(k->ip_src + 4);
521 ip_src2 = (const uint32_t *)(k->ip_src + 8);
522 ip_src3 = (const uint32_t *)(k->ip_src + 12);
523 ip_dst0 = (const uint32_t *) k->ip_dst;
524 ip_dst1 = (const uint32_t *)(k->ip_dst + 4);
525 ip_dst2 = (const uint32_t *)(k->ip_dst + 8);
526 ip_dst3 = (const uint32_t *)(k->ip_dst + 12);
527 init_val = rte_hash_crc_4byte(t, init_val);
528 init_val = rte_hash_crc_4byte(*ip_src0, init_val);
529 init_val = rte_hash_crc_4byte(*ip_src1, init_val);
530 init_val = rte_hash_crc_4byte(*ip_src2, init_val);
531 init_val = rte_hash_crc_4byte(*ip_src3, init_val);
532 init_val = rte_hash_crc_4byte(*ip_dst0, init_val);
533 init_val = rte_hash_crc_4byte(*ip_dst1, init_val);
534 init_val = rte_hash_crc_4byte(*ip_dst2, init_val);
535 init_val = rte_hash_crc_4byte(*ip_dst3, init_val);
536 init_val = rte_hash_crc_4byte(*p, init_val);
537 #else /* RTE_MACHINE_CPUFLAG_SSE4_2 */
538 init_val = rte_jhash_1word(t, init_val);
539 init_val = rte_jhash(k->ip_src, sizeof(uint8_t) * IPV6_ADDR_LEN, init_val);
540 init_val = rte_jhash(k->ip_dst, sizeof(uint8_t) * IPV6_ADDR_LEN, init_val);
541 init_val = rte_jhash_1word(*p, init_val);
542 #endif /* RTE_MACHINE_CPUFLAG_SSE4_2 */
546 #define IPV4_L3FWD_NUM_ROUTES RTE_DIM(ipv4_l3fwd_route_array)
547 #define IPV6_L3FWD_NUM_ROUTES RTE_DIM(ipv6_l3fwd_route_array)
549 static uint8_t ipv4_l3fwd_out_if[L3FWD_HASH_ENTRIES] __rte_cache_aligned;
550 static uint8_t ipv6_l3fwd_out_if[L3FWD_HASH_ENTRIES] __rte_cache_aligned;
554 #if (APP_LOOKUP_METHOD == APP_LOOKUP_LPM)
555 struct ipv4_l3fwd_route {
561 struct ipv6_l3fwd_route {
567 static struct ipv4_l3fwd_route ipv4_l3fwd_route_array[] = {
568 {IPv4(1, 1, 1, 0), 24, 0},
569 {IPv4(2, 1, 1, 0), 24, 1},
570 {IPv4(3, 1, 1, 0), 24, 2},
571 {IPv4(4, 1, 1, 0), 24, 3},
572 {IPv4(5, 1, 1, 0), 24, 4},
573 {IPv4(6, 1, 1, 0), 24, 5},
574 {IPv4(7, 1, 1, 0), 24, 6},
575 {IPv4(8, 1, 1, 0), 24, 7},
578 static struct ipv6_l3fwd_route ipv6_l3fwd_route_array[] = {
579 {{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, 48, 0},
580 {{2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, 48, 1},
581 {{3, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, 48, 2},
582 {{4, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, 48, 3},
583 {{5, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, 48, 4},
584 {{6, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, 48, 5},
585 {{7, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, 48, 6},
586 {{8, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, 48, 7},
589 #define IPV4_L3FWD_NUM_ROUTES RTE_DIM(ipv4_l3fwd_route_array)
590 #define IPV6_L3FWD_NUM_ROUTES RTE_DIM(ipv6_l3fwd_route_array)
592 #define IPV4_L3FWD_LPM_MAX_RULES 1024
593 #define IPV6_L3FWD_LPM_MAX_RULES 1024
594 #define IPV6_L3FWD_LPM_NUMBER_TBL8S (1 << 16)
596 typedef struct rte_lpm lookup_struct_t;
597 typedef struct rte_lpm6 lookup6_struct_t;
598 static lookup_struct_t *ipv4_l3fwd_lookup_struct[NB_SOCKETS];
599 static lookup6_struct_t *ipv6_l3fwd_lookup_struct[NB_SOCKETS];
603 lookup_struct_t *ipv4_lookup_struct;
604 #if (APP_LOOKUP_METHOD == APP_LOOKUP_LPM)
605 lookup6_struct_t *ipv6_lookup_struct;
607 lookup_struct_t *ipv6_lookup_struct;
610 } __rte_cache_aligned;
612 static struct lcore_conf lcore_conf[RTE_MAX_LCORE];
613 RTE_DEFINE_PER_LCORE(struct lcore_conf *, lcore_conf);
615 #define MAX_RX_QUEUE_PER_THREAD 16
616 #define MAX_TX_PORT_PER_THREAD RTE_MAX_ETHPORTS
617 #define MAX_TX_QUEUE_PER_PORT RTE_MAX_ETHPORTS
618 #define MAX_RX_QUEUE_PER_PORT 128
620 #define MAX_RX_THREAD 1024
621 #define MAX_TX_THREAD 1024
622 #define MAX_THREAD (MAX_RX_THREAD + MAX_TX_THREAD)
625 * Producers and consumers threads configuration
627 static int lthreads_on = 1; /**< Use lthreads for processing*/
629 rte_atomic16_t rx_counter; /**< Number of spawned rx threads */
630 rte_atomic16_t tx_counter; /**< Number of spawned tx threads */
633 uint16_t lcore_id; /**< Initial lcore for rx thread */
634 uint16_t cpu_id; /**< Cpu id for cpu load stats counter */
635 uint16_t thread_id; /**< Thread ID */
637 #if (APP_CPU_LOAD > 0)
638 int busy[MAX_CPU_COUNTER];
642 struct thread_rx_conf {
643 struct thread_conf conf;
646 struct lcore_rx_queue rx_queue_list[MAX_RX_QUEUE_PER_LCORE];
648 uint16_t n_ring; /**< Number of output rings */
649 struct rte_ring *ring[RTE_MAX_LCORE];
650 struct lthread_cond *ready[RTE_MAX_LCORE];
652 #if (APP_CPU_LOAD > 0)
653 int busy[MAX_CPU_COUNTER];
655 } __rte_cache_aligned;
657 uint16_t n_rx_thread;
658 struct thread_rx_conf rx_thread[MAX_RX_THREAD];
660 struct thread_tx_conf {
661 struct thread_conf conf;
663 uint16_t tx_queue_id[RTE_MAX_LCORE];
664 struct mbuf_table tx_mbufs[RTE_MAX_LCORE];
666 struct rte_ring *ring;
667 struct lthread_cond **ready;
669 } __rte_cache_aligned;
671 uint16_t n_tx_thread;
672 struct thread_tx_conf tx_thread[MAX_TX_THREAD];
674 /* Send burst of packets on an output interface */
676 send_burst(struct thread_tx_conf *qconf, uint16_t n, uint8_t port)
678 struct rte_mbuf **m_table;
682 queueid = qconf->tx_queue_id[port];
683 m_table = (struct rte_mbuf **)qconf->tx_mbufs[port].m_table;
685 ret = rte_eth_tx_burst(port, queueid, m_table, n);
686 if (unlikely(ret < n)) {
688 rte_pktmbuf_free(m_table[ret]);
695 /* Enqueue a single packet, and send burst if queue is filled */
697 send_single_packet(struct rte_mbuf *m, uint8_t port)
700 struct thread_tx_conf *qconf;
703 qconf = (struct thread_tx_conf *)lthread_get_data();
705 qconf = (struct thread_tx_conf *)RTE_PER_LCORE(lcore_conf)->data;
707 len = qconf->tx_mbufs[port].len;
708 qconf->tx_mbufs[port].m_table[len] = m;
711 /* enough pkts to be sent */
712 if (unlikely(len == MAX_PKT_BURST)) {
713 send_burst(qconf, MAX_PKT_BURST, port);
717 qconf->tx_mbufs[port].len = len;
721 #if ((APP_LOOKUP_METHOD == APP_LOOKUP_LPM) && \
722 (ENABLE_MULTI_BUFFER_OPTIMIZE == 1))
723 static inline __attribute__((always_inline)) void
724 send_packetsx4(uint8_t port,
725 struct rte_mbuf *m[], uint32_t num)
728 struct thread_tx_conf *qconf;
731 qconf = (struct thread_tx_conf *)lthread_get_data();
733 qconf = (struct thread_tx_conf *)RTE_PER_LCORE(lcore_conf)->data;
735 len = qconf->tx_mbufs[port].len;
738 * If TX buffer for that queue is empty, and we have enough packets,
739 * then send them straightway.
741 if (num >= MAX_TX_BURST && len == 0) {
742 n = rte_eth_tx_burst(port, qconf->tx_queue_id[port], m, num);
743 if (unlikely(n < num)) {
745 rte_pktmbuf_free(m[n]);
752 * Put packets into TX buffer for that queue.
756 n = (n > MAX_PKT_BURST) ? MAX_PKT_BURST - len : num;
759 switch (n % FWDSTEP) {
762 qconf->tx_mbufs[port].m_table[len + j] = m[j];
765 qconf->tx_mbufs[port].m_table[len + j] = m[j];
768 qconf->tx_mbufs[port].m_table[len + j] = m[j];
771 qconf->tx_mbufs[port].m_table[len + j] = m[j];
778 /* enough pkts to be sent */
779 if (unlikely(len == MAX_PKT_BURST)) {
781 send_burst(qconf, MAX_PKT_BURST, port);
783 /* copy rest of the packets into the TX buffer. */
786 switch (len % FWDSTEP) {
789 qconf->tx_mbufs[port].m_table[j] = m[n + j];
792 qconf->tx_mbufs[port].m_table[j] = m[n + j];
795 qconf->tx_mbufs[port].m_table[j] = m[n + j];
798 qconf->tx_mbufs[port].m_table[j] = m[n + j];
804 qconf->tx_mbufs[port].len = len;
806 #endif /* APP_LOOKUP_LPM */
808 #ifdef DO_RFC_1812_CHECKS
810 is_valid_ipv4_pkt(struct ipv4_hdr *pkt, uint32_t link_len)
812 /* From http://www.rfc-editor.org/rfc/rfc1812.txt section 5.2.2 */
814 * 1. The packet length reported by the Link Layer must be large
815 * enough to hold the minimum length legal IP datagram (20 bytes).
817 if (link_len < sizeof(struct ipv4_hdr))
820 /* 2. The IP checksum must be correct. */
821 /* this is checked in H/W */
824 * 3. The IP version number must be 4. If the version number is not 4
825 * then the packet may be another version of IP, such as IPng or
828 if (((pkt->version_ihl) >> 4) != 4)
831 * 4. The IP header length field must be large enough to hold the
832 * minimum length legal IP datagram (20 bytes = 5 words).
834 if ((pkt->version_ihl & 0xf) < 5)
838 * 5. The IP total length field must be large enough to hold the IP
839 * datagram header, whose length is specified in the IP header length
842 if (rte_cpu_to_be_16(pkt->total_length) < sizeof(struct ipv4_hdr))
849 #if (APP_LOOKUP_METHOD == APP_LOOKUP_EXACT_MATCH)
851 static __m128i mask0;
852 static __m128i mask1;
853 static __m128i mask2;
854 static inline uint8_t
855 get_ipv4_dst_port(void *ipv4_hdr, uint8_t portid,
856 lookup_struct_t *ipv4_l3fwd_lookup_struct)
859 union ipv4_5tuple_host key;
861 ipv4_hdr = (uint8_t *)ipv4_hdr + offsetof(struct ipv4_hdr, time_to_live);
862 __m128i data = _mm_loadu_si128((__m128i *)(ipv4_hdr));
863 /* Get 5 tuple: dst port, src port, dst IP address, src IP address and
865 key.xmm = _mm_and_si128(data, mask0);
866 /* Find destination port */
867 ret = rte_hash_lookup(ipv4_l3fwd_lookup_struct, (const void *)&key);
868 return (uint8_t)((ret < 0) ? portid : ipv4_l3fwd_out_if[ret]);
871 static inline uint8_t
872 get_ipv6_dst_port(void *ipv6_hdr, uint8_t portid,
873 lookup_struct_t *ipv6_l3fwd_lookup_struct)
876 union ipv6_5tuple_host key;
878 ipv6_hdr = (uint8_t *)ipv6_hdr + offsetof(struct ipv6_hdr, payload_len);
879 __m128i data0 = _mm_loadu_si128((__m128i *)(ipv6_hdr));
880 __m128i data1 = _mm_loadu_si128((__m128i *)(((uint8_t *)ipv6_hdr) +
882 __m128i data2 = _mm_loadu_si128((__m128i *)(((uint8_t *)ipv6_hdr) +
883 sizeof(__m128i) + sizeof(__m128i)));
884 /* Get part of 5 tuple: src IP address lower 96 bits and protocol */
885 key.xmm[0] = _mm_and_si128(data0, mask1);
886 /* Get part of 5 tuple: dst IP address lower 96 bits and src IP address
889 /* Get part of 5 tuple: dst port and src port and dst IP address higher
891 key.xmm[2] = _mm_and_si128(data2, mask2);
893 /* Find destination port */
894 ret = rte_hash_lookup(ipv6_l3fwd_lookup_struct, (const void *)&key);
895 return (uint8_t)((ret < 0) ? portid : ipv6_l3fwd_out_if[ret]);
899 #if (APP_LOOKUP_METHOD == APP_LOOKUP_LPM)
901 static inline uint8_t
902 get_ipv4_dst_port(void *ipv4_hdr, uint8_t portid,
903 lookup_struct_t *ipv4_l3fwd_lookup_struct)
907 return (uint8_t)((rte_lpm_lookup(ipv4_l3fwd_lookup_struct,
908 rte_be_to_cpu_32(((struct ipv4_hdr *)ipv4_hdr)->dst_addr),
909 &next_hop) == 0) ? next_hop : portid);
912 static inline uint8_t
913 get_ipv6_dst_port(void *ipv6_hdr, uint8_t portid,
914 lookup6_struct_t *ipv6_l3fwd_lookup_struct)
918 return (uint8_t) ((rte_lpm6_lookup(ipv6_l3fwd_lookup_struct,
919 ((struct ipv6_hdr *)ipv6_hdr)->dst_addr, &next_hop) == 0) ?
924 static inline void l3fwd_simple_forward(struct rte_mbuf *m, uint8_t portid)
925 __attribute__((unused));
927 #if ((APP_LOOKUP_METHOD == APP_LOOKUP_EXACT_MATCH) && \
928 (ENABLE_MULTI_BUFFER_OPTIMIZE == 1))
930 #define MASK_ALL_PKTS 0xff
931 #define EXCLUDE_1ST_PKT 0xfe
932 #define EXCLUDE_2ND_PKT 0xfd
933 #define EXCLUDE_3RD_PKT 0xfb
934 #define EXCLUDE_4TH_PKT 0xf7
935 #define EXCLUDE_5TH_PKT 0xef
936 #define EXCLUDE_6TH_PKT 0xdf
937 #define EXCLUDE_7TH_PKT 0xbf
938 #define EXCLUDE_8TH_PKT 0x7f
941 simple_ipv4_fwd_8pkts(struct rte_mbuf *m[8], uint8_t portid)
943 struct ether_hdr *eth_hdr[8];
944 struct ipv4_hdr *ipv4_hdr[8];
947 union ipv4_5tuple_host key[8];
950 eth_hdr[0] = rte_pktmbuf_mtod(m[0], struct ether_hdr *);
951 eth_hdr[1] = rte_pktmbuf_mtod(m[1], struct ether_hdr *);
952 eth_hdr[2] = rte_pktmbuf_mtod(m[2], struct ether_hdr *);
953 eth_hdr[3] = rte_pktmbuf_mtod(m[3], struct ether_hdr *);
954 eth_hdr[4] = rte_pktmbuf_mtod(m[4], struct ether_hdr *);
955 eth_hdr[5] = rte_pktmbuf_mtod(m[5], struct ether_hdr *);
956 eth_hdr[6] = rte_pktmbuf_mtod(m[6], struct ether_hdr *);
957 eth_hdr[7] = rte_pktmbuf_mtod(m[7], struct ether_hdr *);
959 /* Handle IPv4 headers.*/
960 ipv4_hdr[0] = rte_pktmbuf_mtod_offset(m[0], struct ipv4_hdr *,
961 sizeof(struct ether_hdr));
962 ipv4_hdr[1] = rte_pktmbuf_mtod_offset(m[1], struct ipv4_hdr *,
963 sizeof(struct ether_hdr));
964 ipv4_hdr[2] = rte_pktmbuf_mtod_offset(m[2], struct ipv4_hdr *,
965 sizeof(struct ether_hdr));
966 ipv4_hdr[3] = rte_pktmbuf_mtod_offset(m[3], struct ipv4_hdr *,
967 sizeof(struct ether_hdr));
968 ipv4_hdr[4] = rte_pktmbuf_mtod_offset(m[4], struct ipv4_hdr *,
969 sizeof(struct ether_hdr));
970 ipv4_hdr[5] = rte_pktmbuf_mtod_offset(m[5], struct ipv4_hdr *,
971 sizeof(struct ether_hdr));
972 ipv4_hdr[6] = rte_pktmbuf_mtod_offset(m[6], struct ipv4_hdr *,
973 sizeof(struct ether_hdr));
974 ipv4_hdr[7] = rte_pktmbuf_mtod_offset(m[7], struct ipv4_hdr *,
975 sizeof(struct ether_hdr));
977 #ifdef DO_RFC_1812_CHECKS
978 /* Check to make sure the packet is valid (RFC1812) */
979 uint8_t valid_mask = MASK_ALL_PKTS;
981 if (is_valid_ipv4_pkt(ipv4_hdr[0], m[0]->pkt_len) < 0) {
982 rte_pktmbuf_free(m[0]);
983 valid_mask &= EXCLUDE_1ST_PKT;
985 if (is_valid_ipv4_pkt(ipv4_hdr[1], m[1]->pkt_len) < 0) {
986 rte_pktmbuf_free(m[1]);
987 valid_mask &= EXCLUDE_2ND_PKT;
989 if (is_valid_ipv4_pkt(ipv4_hdr[2], m[2]->pkt_len) < 0) {
990 rte_pktmbuf_free(m[2]);
991 valid_mask &= EXCLUDE_3RD_PKT;
993 if (is_valid_ipv4_pkt(ipv4_hdr[3], m[3]->pkt_len) < 0) {
994 rte_pktmbuf_free(m[3]);
995 valid_mask &= EXCLUDE_4TH_PKT;
997 if (is_valid_ipv4_pkt(ipv4_hdr[4], m[4]->pkt_len) < 0) {
998 rte_pktmbuf_free(m[4]);
999 valid_mask &= EXCLUDE_5TH_PKT;
1001 if (is_valid_ipv4_pkt(ipv4_hdr[5], m[5]->pkt_len) < 0) {
1002 rte_pktmbuf_free(m[5]);
1003 valid_mask &= EXCLUDE_6TH_PKT;
1005 if (is_valid_ipv4_pkt(ipv4_hdr[6], m[6]->pkt_len) < 0) {
1006 rte_pktmbuf_free(m[6]);
1007 valid_mask &= EXCLUDE_7TH_PKT;
1009 if (is_valid_ipv4_pkt(ipv4_hdr[7], m[7]->pkt_len) < 0) {
1010 rte_pktmbuf_free(m[7]);
1011 valid_mask &= EXCLUDE_8TH_PKT;
1013 if (unlikely(valid_mask != MASK_ALL_PKTS)) {
1014 if (valid_mask == 0)
1019 for (i = 0; i < 8; i++)
1020 if ((0x1 << i) & valid_mask)
1021 l3fwd_simple_forward(m[i], portid);
1023 #endif /* End of #ifdef DO_RFC_1812_CHECKS */
1025 data[0] = _mm_loadu_si128(rte_pktmbuf_mtod_offset(m[0], __m128i *,
1026 sizeof(struct ether_hdr) +
1027 offsetof(struct ipv4_hdr, time_to_live)));
1028 data[1] = _mm_loadu_si128(rte_pktmbuf_mtod_offset(m[1], __m128i *,
1029 sizeof(struct ether_hdr) +
1030 offsetof(struct ipv4_hdr, time_to_live)));
1031 data[2] = _mm_loadu_si128(rte_pktmbuf_mtod_offset(m[2], __m128i *,
1032 sizeof(struct ether_hdr) +
1033 offsetof(struct ipv4_hdr, time_to_live)));
1034 data[3] = _mm_loadu_si128(rte_pktmbuf_mtod_offset(m[3], __m128i *,
1035 sizeof(struct ether_hdr) +
1036 offsetof(struct ipv4_hdr, time_to_live)));
1037 data[4] = _mm_loadu_si128(rte_pktmbuf_mtod_offset(m[4], __m128i *,
1038 sizeof(struct ether_hdr) +
1039 offsetof(struct ipv4_hdr, time_to_live)));
1040 data[5] = _mm_loadu_si128(rte_pktmbuf_mtod_offset(m[5], __m128i *,
1041 sizeof(struct ether_hdr) +
1042 offsetof(struct ipv4_hdr, time_to_live)));
1043 data[6] = _mm_loadu_si128(rte_pktmbuf_mtod_offset(m[6], __m128i *,
1044 sizeof(struct ether_hdr) +
1045 offsetof(struct ipv4_hdr, time_to_live)));
1046 data[7] = _mm_loadu_si128(rte_pktmbuf_mtod_offset(m[7], __m128i *,
1047 sizeof(struct ether_hdr) +
1048 offsetof(struct ipv4_hdr, time_to_live)));
1050 key[0].xmm = _mm_and_si128(data[0], mask0);
1051 key[1].xmm = _mm_and_si128(data[1], mask0);
1052 key[2].xmm = _mm_and_si128(data[2], mask0);
1053 key[3].xmm = _mm_and_si128(data[3], mask0);
1054 key[4].xmm = _mm_and_si128(data[4], mask0);
1055 key[5].xmm = _mm_and_si128(data[5], mask0);
1056 key[6].xmm = _mm_and_si128(data[6], mask0);
1057 key[7].xmm = _mm_and_si128(data[7], mask0);
1059 const void *key_array[8] = {&key[0], &key[1], &key[2], &key[3],
1060 &key[4], &key[5], &key[6], &key[7]};
1062 rte_hash_lookup_bulk(RTE_PER_LCORE(lcore_conf)->ipv4_lookup_struct,
1063 &key_array[0], 8, ret);
1064 dst_port[0] = (uint8_t) ((ret[0] < 0) ? portid : ipv4_l3fwd_out_if[ret[0]]);
1065 dst_port[1] = (uint8_t) ((ret[1] < 0) ? portid : ipv4_l3fwd_out_if[ret[1]]);
1066 dst_port[2] = (uint8_t) ((ret[2] < 0) ? portid : ipv4_l3fwd_out_if[ret[2]]);
1067 dst_port[3] = (uint8_t) ((ret[3] < 0) ? portid : ipv4_l3fwd_out_if[ret[3]]);
1068 dst_port[4] = (uint8_t) ((ret[4] < 0) ? portid : ipv4_l3fwd_out_if[ret[4]]);
1069 dst_port[5] = (uint8_t) ((ret[5] < 0) ? portid : ipv4_l3fwd_out_if[ret[5]]);
1070 dst_port[6] = (uint8_t) ((ret[6] < 0) ? portid : ipv4_l3fwd_out_if[ret[6]]);
1071 dst_port[7] = (uint8_t) ((ret[7] < 0) ? portid : ipv4_l3fwd_out_if[ret[7]]);
1073 if (dst_port[0] >= RTE_MAX_ETHPORTS ||
1074 (enabled_port_mask & 1 << dst_port[0]) == 0)
1075 dst_port[0] = portid;
1076 if (dst_port[1] >= RTE_MAX_ETHPORTS ||
1077 (enabled_port_mask & 1 << dst_port[1]) == 0)
1078 dst_port[1] = portid;
1079 if (dst_port[2] >= RTE_MAX_ETHPORTS ||
1080 (enabled_port_mask & 1 << dst_port[2]) == 0)
1081 dst_port[2] = portid;
1082 if (dst_port[3] >= RTE_MAX_ETHPORTS ||
1083 (enabled_port_mask & 1 << dst_port[3]) == 0)
1084 dst_port[3] = portid;
1085 if (dst_port[4] >= RTE_MAX_ETHPORTS ||
1086 (enabled_port_mask & 1 << dst_port[4]) == 0)
1087 dst_port[4] = portid;
1088 if (dst_port[5] >= RTE_MAX_ETHPORTS ||
1089 (enabled_port_mask & 1 << dst_port[5]) == 0)
1090 dst_port[5] = portid;
1091 if (dst_port[6] >= RTE_MAX_ETHPORTS ||
1092 (enabled_port_mask & 1 << dst_port[6]) == 0)
1093 dst_port[6] = portid;
1094 if (dst_port[7] >= RTE_MAX_ETHPORTS ||
1095 (enabled_port_mask & 1 << dst_port[7]) == 0)
1096 dst_port[7] = portid;
1098 #ifdef DO_RFC_1812_CHECKS
1099 /* Update time to live and header checksum */
1100 --(ipv4_hdr[0]->time_to_live);
1101 --(ipv4_hdr[1]->time_to_live);
1102 --(ipv4_hdr[2]->time_to_live);
1103 --(ipv4_hdr[3]->time_to_live);
1104 ++(ipv4_hdr[0]->hdr_checksum);
1105 ++(ipv4_hdr[1]->hdr_checksum);
1106 ++(ipv4_hdr[2]->hdr_checksum);
1107 ++(ipv4_hdr[3]->hdr_checksum);
1108 --(ipv4_hdr[4]->time_to_live);
1109 --(ipv4_hdr[5]->time_to_live);
1110 --(ipv4_hdr[6]->time_to_live);
1111 --(ipv4_hdr[7]->time_to_live);
1112 ++(ipv4_hdr[4]->hdr_checksum);
1113 ++(ipv4_hdr[5]->hdr_checksum);
1114 ++(ipv4_hdr[6]->hdr_checksum);
1115 ++(ipv4_hdr[7]->hdr_checksum);
1119 *(uint64_t *)ð_hdr[0]->d_addr = dest_eth_addr[dst_port[0]];
1120 *(uint64_t *)ð_hdr[1]->d_addr = dest_eth_addr[dst_port[1]];
1121 *(uint64_t *)ð_hdr[2]->d_addr = dest_eth_addr[dst_port[2]];
1122 *(uint64_t *)ð_hdr[3]->d_addr = dest_eth_addr[dst_port[3]];
1123 *(uint64_t *)ð_hdr[4]->d_addr = dest_eth_addr[dst_port[4]];
1124 *(uint64_t *)ð_hdr[5]->d_addr = dest_eth_addr[dst_port[5]];
1125 *(uint64_t *)ð_hdr[6]->d_addr = dest_eth_addr[dst_port[6]];
1126 *(uint64_t *)ð_hdr[7]->d_addr = dest_eth_addr[dst_port[7]];
1129 ether_addr_copy(&ports_eth_addr[dst_port[0]], ð_hdr[0]->s_addr);
1130 ether_addr_copy(&ports_eth_addr[dst_port[1]], ð_hdr[1]->s_addr);
1131 ether_addr_copy(&ports_eth_addr[dst_port[2]], ð_hdr[2]->s_addr);
1132 ether_addr_copy(&ports_eth_addr[dst_port[3]], ð_hdr[3]->s_addr);
1133 ether_addr_copy(&ports_eth_addr[dst_port[4]], ð_hdr[4]->s_addr);
1134 ether_addr_copy(&ports_eth_addr[dst_port[5]], ð_hdr[5]->s_addr);
1135 ether_addr_copy(&ports_eth_addr[dst_port[6]], ð_hdr[6]->s_addr);
1136 ether_addr_copy(&ports_eth_addr[dst_port[7]], ð_hdr[7]->s_addr);
1138 send_single_packet(m[0], (uint8_t)dst_port[0]);
1139 send_single_packet(m[1], (uint8_t)dst_port[1]);
1140 send_single_packet(m[2], (uint8_t)dst_port[2]);
1141 send_single_packet(m[3], (uint8_t)dst_port[3]);
1142 send_single_packet(m[4], (uint8_t)dst_port[4]);
1143 send_single_packet(m[5], (uint8_t)dst_port[5]);
1144 send_single_packet(m[6], (uint8_t)dst_port[6]);
1145 send_single_packet(m[7], (uint8_t)dst_port[7]);
1149 static inline void get_ipv6_5tuple(struct rte_mbuf *m0, __m128i mask0,
1150 __m128i mask1, union ipv6_5tuple_host *key)
1152 __m128i tmpdata0 = _mm_loadu_si128(rte_pktmbuf_mtod_offset(m0,
1153 __m128i *, sizeof(struct ether_hdr) +
1154 offsetof(struct ipv6_hdr, payload_len)));
1155 __m128i tmpdata1 = _mm_loadu_si128(rte_pktmbuf_mtod_offset(m0,
1156 __m128i *, sizeof(struct ether_hdr) +
1157 offsetof(struct ipv6_hdr, payload_len) + sizeof(__m128i)));
1158 __m128i tmpdata2 = _mm_loadu_si128(rte_pktmbuf_mtod_offset(m0,
1159 __m128i *, sizeof(struct ether_hdr) +
1160 offsetof(struct ipv6_hdr, payload_len) + sizeof(__m128i) +
1162 key->xmm[0] = _mm_and_si128(tmpdata0, mask0);
1163 key->xmm[1] = tmpdata1;
1164 key->xmm[2] = _mm_and_si128(tmpdata2, mask1);
1168 simple_ipv6_fwd_8pkts(struct rte_mbuf *m[8], uint8_t portid)
1171 uint8_t dst_port[8];
1172 struct ether_hdr *eth_hdr[8];
1173 union ipv6_5tuple_host key[8];
1175 __attribute__((unused)) struct ipv6_hdr *ipv6_hdr[8];
1177 eth_hdr[0] = rte_pktmbuf_mtod(m[0], struct ether_hdr *);
1178 eth_hdr[1] = rte_pktmbuf_mtod(m[1], struct ether_hdr *);
1179 eth_hdr[2] = rte_pktmbuf_mtod(m[2], struct ether_hdr *);
1180 eth_hdr[3] = rte_pktmbuf_mtod(m[3], struct ether_hdr *);
1181 eth_hdr[4] = rte_pktmbuf_mtod(m[4], struct ether_hdr *);
1182 eth_hdr[5] = rte_pktmbuf_mtod(m[5], struct ether_hdr *);
1183 eth_hdr[6] = rte_pktmbuf_mtod(m[6], struct ether_hdr *);
1184 eth_hdr[7] = rte_pktmbuf_mtod(m[7], struct ether_hdr *);
1186 /* Handle IPv6 headers.*/
1187 ipv6_hdr[0] = rte_pktmbuf_mtod_offset(m[0], struct ipv6_hdr *,
1188 sizeof(struct ether_hdr));
1189 ipv6_hdr[1] = rte_pktmbuf_mtod_offset(m[1], struct ipv6_hdr *,
1190 sizeof(struct ether_hdr));
1191 ipv6_hdr[2] = rte_pktmbuf_mtod_offset(m[2], struct ipv6_hdr *,
1192 sizeof(struct ether_hdr));
1193 ipv6_hdr[3] = rte_pktmbuf_mtod_offset(m[3], struct ipv6_hdr *,
1194 sizeof(struct ether_hdr));
1195 ipv6_hdr[4] = rte_pktmbuf_mtod_offset(m[4], struct ipv6_hdr *,
1196 sizeof(struct ether_hdr));
1197 ipv6_hdr[5] = rte_pktmbuf_mtod_offset(m[5], struct ipv6_hdr *,
1198 sizeof(struct ether_hdr));
1199 ipv6_hdr[6] = rte_pktmbuf_mtod_offset(m[6], struct ipv6_hdr *,
1200 sizeof(struct ether_hdr));
1201 ipv6_hdr[7] = rte_pktmbuf_mtod_offset(m[7], struct ipv6_hdr *,
1202 sizeof(struct ether_hdr));
1204 get_ipv6_5tuple(m[0], mask1, mask2, &key[0]);
1205 get_ipv6_5tuple(m[1], mask1, mask2, &key[1]);
1206 get_ipv6_5tuple(m[2], mask1, mask2, &key[2]);
1207 get_ipv6_5tuple(m[3], mask1, mask2, &key[3]);
1208 get_ipv6_5tuple(m[4], mask1, mask2, &key[4]);
1209 get_ipv6_5tuple(m[5], mask1, mask2, &key[5]);
1210 get_ipv6_5tuple(m[6], mask1, mask2, &key[6]);
1211 get_ipv6_5tuple(m[7], mask1, mask2, &key[7]);
1213 const void *key_array[8] = {&key[0], &key[1], &key[2], &key[3],
1214 &key[4], &key[5], &key[6], &key[7]};
1216 rte_hash_lookup_bulk(RTE_PER_LCORE(lcore_conf)->ipv6_lookup_struct,
1217 &key_array[0], 4, ret);
1218 dst_port[0] = (uint8_t) ((ret[0] < 0) ? portid : ipv6_l3fwd_out_if[ret[0]]);
1219 dst_port[1] = (uint8_t) ((ret[1] < 0) ? portid : ipv6_l3fwd_out_if[ret[1]]);
1220 dst_port[2] = (uint8_t) ((ret[2] < 0) ? portid : ipv6_l3fwd_out_if[ret[2]]);
1221 dst_port[3] = (uint8_t) ((ret[3] < 0) ? portid : ipv6_l3fwd_out_if[ret[3]]);
1222 dst_port[4] = (uint8_t) ((ret[4] < 0) ? portid : ipv6_l3fwd_out_if[ret[4]]);
1223 dst_port[5] = (uint8_t) ((ret[5] < 0) ? portid : ipv6_l3fwd_out_if[ret[5]]);
1224 dst_port[6] = (uint8_t) ((ret[6] < 0) ? portid : ipv6_l3fwd_out_if[ret[6]]);
1225 dst_port[7] = (uint8_t) ((ret[7] < 0) ? portid : ipv6_l3fwd_out_if[ret[7]]);
1227 if (dst_port[0] >= RTE_MAX_ETHPORTS ||
1228 (enabled_port_mask & 1 << dst_port[0]) == 0)
1229 dst_port[0] = portid;
1230 if (dst_port[1] >= RTE_MAX_ETHPORTS ||
1231 (enabled_port_mask & 1 << dst_port[1]) == 0)
1232 dst_port[1] = portid;
1233 if (dst_port[2] >= RTE_MAX_ETHPORTS ||
1234 (enabled_port_mask & 1 << dst_port[2]) == 0)
1235 dst_port[2] = portid;
1236 if (dst_port[3] >= RTE_MAX_ETHPORTS ||
1237 (enabled_port_mask & 1 << dst_port[3]) == 0)
1238 dst_port[3] = portid;
1239 if (dst_port[4] >= RTE_MAX_ETHPORTS ||
1240 (enabled_port_mask & 1 << dst_port[4]) == 0)
1241 dst_port[4] = portid;
1242 if (dst_port[5] >= RTE_MAX_ETHPORTS ||
1243 (enabled_port_mask & 1 << dst_port[5]) == 0)
1244 dst_port[5] = portid;
1245 if (dst_port[6] >= RTE_MAX_ETHPORTS ||
1246 (enabled_port_mask & 1 << dst_port[6]) == 0)
1247 dst_port[6] = portid;
1248 if (dst_port[7] >= RTE_MAX_ETHPORTS ||
1249 (enabled_port_mask & 1 << dst_port[7]) == 0)
1250 dst_port[7] = portid;
1253 *(uint64_t *)ð_hdr[0]->d_addr = dest_eth_addr[dst_port[0]];
1254 *(uint64_t *)ð_hdr[1]->d_addr = dest_eth_addr[dst_port[1]];
1255 *(uint64_t *)ð_hdr[2]->d_addr = dest_eth_addr[dst_port[2]];
1256 *(uint64_t *)ð_hdr[3]->d_addr = dest_eth_addr[dst_port[3]];
1257 *(uint64_t *)ð_hdr[4]->d_addr = dest_eth_addr[dst_port[4]];
1258 *(uint64_t *)ð_hdr[5]->d_addr = dest_eth_addr[dst_port[5]];
1259 *(uint64_t *)ð_hdr[6]->d_addr = dest_eth_addr[dst_port[6]];
1260 *(uint64_t *)ð_hdr[7]->d_addr = dest_eth_addr[dst_port[7]];
1263 ether_addr_copy(&ports_eth_addr[dst_port[0]], ð_hdr[0]->s_addr);
1264 ether_addr_copy(&ports_eth_addr[dst_port[1]], ð_hdr[1]->s_addr);
1265 ether_addr_copy(&ports_eth_addr[dst_port[2]], ð_hdr[2]->s_addr);
1266 ether_addr_copy(&ports_eth_addr[dst_port[3]], ð_hdr[3]->s_addr);
1267 ether_addr_copy(&ports_eth_addr[dst_port[4]], ð_hdr[4]->s_addr);
1268 ether_addr_copy(&ports_eth_addr[dst_port[5]], ð_hdr[5]->s_addr);
1269 ether_addr_copy(&ports_eth_addr[dst_port[6]], ð_hdr[6]->s_addr);
1270 ether_addr_copy(&ports_eth_addr[dst_port[7]], ð_hdr[7]->s_addr);
1272 send_single_packet(m[0], (uint8_t)dst_port[0]);
1273 send_single_packet(m[1], (uint8_t)dst_port[1]);
1274 send_single_packet(m[2], (uint8_t)dst_port[2]);
1275 send_single_packet(m[3], (uint8_t)dst_port[3]);
1276 send_single_packet(m[4], (uint8_t)dst_port[4]);
1277 send_single_packet(m[5], (uint8_t)dst_port[5]);
1278 send_single_packet(m[6], (uint8_t)dst_port[6]);
1279 send_single_packet(m[7], (uint8_t)dst_port[7]);
1282 #endif /* APP_LOOKUP_METHOD */
1284 static inline __attribute__((always_inline)) void
1285 l3fwd_simple_forward(struct rte_mbuf *m, uint8_t portid)
1287 struct ether_hdr *eth_hdr;
1288 struct ipv4_hdr *ipv4_hdr;
1291 eth_hdr = rte_pktmbuf_mtod(m, struct ether_hdr *);
1293 if (RTE_ETH_IS_IPV4_HDR(m->packet_type)) {
1294 /* Handle IPv4 headers.*/
1295 ipv4_hdr = rte_pktmbuf_mtod_offset(m, struct ipv4_hdr *,
1296 sizeof(struct ether_hdr));
1298 #ifdef DO_RFC_1812_CHECKS
1299 /* Check to make sure the packet is valid (RFC1812) */
1300 if (is_valid_ipv4_pkt(ipv4_hdr, m->pkt_len) < 0) {
1301 rte_pktmbuf_free(m);
1306 dst_port = get_ipv4_dst_port(ipv4_hdr, portid,
1307 RTE_PER_LCORE(lcore_conf)->ipv4_lookup_struct);
1308 if (dst_port >= RTE_MAX_ETHPORTS ||
1309 (enabled_port_mask & 1 << dst_port) == 0)
1312 #ifdef DO_RFC_1812_CHECKS
1313 /* Update time to live and header checksum */
1314 --(ipv4_hdr->time_to_live);
1315 ++(ipv4_hdr->hdr_checksum);
1318 *(uint64_t *)ð_hdr->d_addr = dest_eth_addr[dst_port];
1321 ether_addr_copy(&ports_eth_addr[dst_port], ð_hdr->s_addr);
1323 send_single_packet(m, dst_port);
1324 } else if (RTE_ETH_IS_IPV6_HDR(m->packet_type)) {
1325 /* Handle IPv6 headers.*/
1326 struct ipv6_hdr *ipv6_hdr;
1328 ipv6_hdr = rte_pktmbuf_mtod_offset(m, struct ipv6_hdr *,
1329 sizeof(struct ether_hdr));
1331 dst_port = get_ipv6_dst_port(ipv6_hdr, portid,
1332 RTE_PER_LCORE(lcore_conf)->ipv6_lookup_struct);
1334 if (dst_port >= RTE_MAX_ETHPORTS ||
1335 (enabled_port_mask & 1 << dst_port) == 0)
1339 *(uint64_t *)ð_hdr->d_addr = dest_eth_addr[dst_port];
1342 ether_addr_copy(&ports_eth_addr[dst_port], ð_hdr->s_addr);
1344 send_single_packet(m, dst_port);
1346 /* Free the mbuf that contains non-IPV4/IPV6 packet */
1347 rte_pktmbuf_free(m);
1350 #if ((APP_LOOKUP_METHOD == APP_LOOKUP_LPM) && \
1351 (ENABLE_MULTI_BUFFER_OPTIMIZE == 1))
1352 #ifdef DO_RFC_1812_CHECKS
1354 #define IPV4_MIN_VER_IHL 0x45
1355 #define IPV4_MAX_VER_IHL 0x4f
1356 #define IPV4_MAX_VER_IHL_DIFF (IPV4_MAX_VER_IHL - IPV4_MIN_VER_IHL)
1358 /* Minimum value of IPV4 total length (20B) in network byte order. */
1359 #define IPV4_MIN_LEN_BE (sizeof(struct ipv4_hdr) << 8)
1362 * From http://www.rfc-editor.org/rfc/rfc1812.txt section 5.2.2:
1363 * - The IP version number must be 4.
1364 * - The IP header length field must be large enough to hold the
1365 * minimum length legal IP datagram (20 bytes = 5 words).
1366 * - The IP total length field must be large enough to hold the IP
1367 * datagram header, whose length is specified in the IP header length
1369 * If we encounter invalid IPV4 packet, then set destination port for it
1370 * to BAD_PORT value.
1372 static inline __attribute__((always_inline)) void
1373 rfc1812_process(struct ipv4_hdr *ipv4_hdr, uint16_t *dp, uint32_t ptype)
1377 if (RTE_ETH_IS_IPV4_HDR(ptype)) {
1378 ihl = ipv4_hdr->version_ihl - IPV4_MIN_VER_IHL;
1380 ipv4_hdr->time_to_live--;
1381 ipv4_hdr->hdr_checksum++;
1383 if (ihl > IPV4_MAX_VER_IHL_DIFF ||
1384 ((uint8_t)ipv4_hdr->total_length == 0 &&
1385 ipv4_hdr->total_length < IPV4_MIN_LEN_BE)) {
1392 #define rfc1812_process(mb, dp, ptype) do { } while (0)
1393 #endif /* DO_RFC_1812_CHECKS */
1394 #endif /* APP_LOOKUP_LPM && ENABLE_MULTI_BUFFER_OPTIMIZE */
1397 #if ((APP_LOOKUP_METHOD == APP_LOOKUP_LPM) && \
1398 (ENABLE_MULTI_BUFFER_OPTIMIZE == 1))
1400 static inline __attribute__((always_inline)) uint16_t
1401 get_dst_port(struct rte_mbuf *pkt, uint32_t dst_ipv4, uint8_t portid)
1404 struct ipv6_hdr *ipv6_hdr;
1405 struct ether_hdr *eth_hdr;
1407 if (RTE_ETH_IS_IPV4_HDR(pkt->packet_type)) {
1408 return (uint16_t) ((rte_lpm_lookup(
1409 RTE_PER_LCORE(lcore_conf)->ipv4_lookup_struct, dst_ipv4,
1410 &next_hop) == 0) ? next_hop : portid);
1412 } else if (RTE_ETH_IS_IPV6_HDR(pkt->packet_type)) {
1414 eth_hdr = rte_pktmbuf_mtod(pkt, struct ether_hdr *);
1415 ipv6_hdr = (struct ipv6_hdr *)(eth_hdr + 1);
1417 return (uint16_t) ((rte_lpm6_lookup(
1418 RTE_PER_LCORE(lcore_conf)->ipv6_lookup_struct,
1419 ipv6_hdr->dst_addr, &next_hop) == 0) ?
1428 process_packet(struct rte_mbuf *pkt, uint16_t *dst_port, uint8_t portid)
1430 struct ether_hdr *eth_hdr;
1431 struct ipv4_hdr *ipv4_hdr;
1436 eth_hdr = rte_pktmbuf_mtod(pkt, struct ether_hdr *);
1437 ipv4_hdr = (struct ipv4_hdr *)(eth_hdr + 1);
1439 dst_ipv4 = ipv4_hdr->dst_addr;
1440 dst_ipv4 = rte_be_to_cpu_32(dst_ipv4);
1441 dp = get_dst_port(pkt, dst_ipv4, portid);
1443 te = _mm_load_si128((__m128i *)eth_hdr);
1447 rfc1812_process(ipv4_hdr, dst_port, pkt->packet_type);
1449 te = _mm_blend_epi16(te, ve, MASK_ETH);
1450 _mm_store_si128((__m128i *)eth_hdr, te);
1454 * Read packet_type and destination IPV4 addresses from 4 mbufs.
1457 processx4_step1(struct rte_mbuf *pkt[FWDSTEP],
1459 uint32_t *ipv4_flag)
1461 struct ipv4_hdr *ipv4_hdr;
1462 struct ether_hdr *eth_hdr;
1463 uint32_t x0, x1, x2, x3;
1465 eth_hdr = rte_pktmbuf_mtod(pkt[0], struct ether_hdr *);
1466 ipv4_hdr = (struct ipv4_hdr *)(eth_hdr + 1);
1467 x0 = ipv4_hdr->dst_addr;
1468 ipv4_flag[0] = pkt[0]->packet_type & RTE_PTYPE_L3_IPV4;
1470 eth_hdr = rte_pktmbuf_mtod(pkt[1], struct ether_hdr *);
1471 ipv4_hdr = (struct ipv4_hdr *)(eth_hdr + 1);
1472 x1 = ipv4_hdr->dst_addr;
1473 ipv4_flag[0] &= pkt[1]->packet_type;
1475 eth_hdr = rte_pktmbuf_mtod(pkt[2], struct ether_hdr *);
1476 ipv4_hdr = (struct ipv4_hdr *)(eth_hdr + 1);
1477 x2 = ipv4_hdr->dst_addr;
1478 ipv4_flag[0] &= pkt[2]->packet_type;
1480 eth_hdr = rte_pktmbuf_mtod(pkt[3], struct ether_hdr *);
1481 ipv4_hdr = (struct ipv4_hdr *)(eth_hdr + 1);
1482 x3 = ipv4_hdr->dst_addr;
1483 ipv4_flag[0] &= pkt[3]->packet_type;
1485 dip[0] = _mm_set_epi32(x3, x2, x1, x0);
1489 * Lookup into LPM for destination port.
1490 * If lookup fails, use incoming port (portid) as destination port.
1493 processx4_step2(__m128i dip,
1496 struct rte_mbuf *pkt[FWDSTEP],
1497 uint16_t dprt[FWDSTEP])
1500 const __m128i bswap_mask = _mm_set_epi8(12, 13, 14, 15, 8, 9, 10, 11,
1501 4, 5, 6, 7, 0, 1, 2, 3);
1503 /* Byte swap 4 IPV4 addresses. */
1504 dip = _mm_shuffle_epi8(dip, bswap_mask);
1506 /* if all 4 packets are IPV4. */
1507 if (likely(ipv4_flag)) {
1508 rte_lpm_lookupx4(RTE_PER_LCORE(lcore_conf)->ipv4_lookup_struct, dip,
1511 /* get rid of unused upper 16 bit for each dport. */
1512 dst.x = _mm_packs_epi32(dst.x, dst.x);
1513 *(uint64_t *)dprt = dst.u64[0];
1516 dprt[0] = get_dst_port(pkt[0], dst.u32[0], portid);
1517 dprt[1] = get_dst_port(pkt[1], dst.u32[1], portid);
1518 dprt[2] = get_dst_port(pkt[2], dst.u32[2], portid);
1519 dprt[3] = get_dst_port(pkt[3], dst.u32[3], portid);
1524 * Update source and destination MAC addresses in the ethernet header.
1525 * Perform RFC1812 checks and updates for IPV4 packets.
1528 processx4_step3(struct rte_mbuf *pkt[FWDSTEP], uint16_t dst_port[FWDSTEP])
1530 __m128i te[FWDSTEP];
1531 __m128i ve[FWDSTEP];
1532 __m128i *p[FWDSTEP];
1534 p[0] = rte_pktmbuf_mtod(pkt[0], __m128i *);
1535 p[1] = rte_pktmbuf_mtod(pkt[1], __m128i *);
1536 p[2] = rte_pktmbuf_mtod(pkt[2], __m128i *);
1537 p[3] = rte_pktmbuf_mtod(pkt[3], __m128i *);
1539 ve[0] = val_eth[dst_port[0]];
1540 te[0] = _mm_load_si128(p[0]);
1542 ve[1] = val_eth[dst_port[1]];
1543 te[1] = _mm_load_si128(p[1]);
1545 ve[2] = val_eth[dst_port[2]];
1546 te[2] = _mm_load_si128(p[2]);
1548 ve[3] = val_eth[dst_port[3]];
1549 te[3] = _mm_load_si128(p[3]);
1551 /* Update first 12 bytes, keep rest bytes intact. */
1552 te[0] = _mm_blend_epi16(te[0], ve[0], MASK_ETH);
1553 te[1] = _mm_blend_epi16(te[1], ve[1], MASK_ETH);
1554 te[2] = _mm_blend_epi16(te[2], ve[2], MASK_ETH);
1555 te[3] = _mm_blend_epi16(te[3], ve[3], MASK_ETH);
1557 _mm_store_si128(p[0], te[0]);
1558 _mm_store_si128(p[1], te[1]);
1559 _mm_store_si128(p[2], te[2]);
1560 _mm_store_si128(p[3], te[3]);
1562 rfc1812_process((struct ipv4_hdr *)((struct ether_hdr *)p[0] + 1),
1563 &dst_port[0], pkt[0]->packet_type);
1564 rfc1812_process((struct ipv4_hdr *)((struct ether_hdr *)p[1] + 1),
1565 &dst_port[1], pkt[1]->packet_type);
1566 rfc1812_process((struct ipv4_hdr *)((struct ether_hdr *)p[2] + 1),
1567 &dst_port[2], pkt[2]->packet_type);
1568 rfc1812_process((struct ipv4_hdr *)((struct ether_hdr *)p[3] + 1),
1569 &dst_port[3], pkt[3]->packet_type);
1573 * We group consecutive packets with the same destionation port into one burst.
1574 * To avoid extra latency this is done together with some other packet
1575 * processing, but after we made a final decision about packet's destination.
1576 * To do this we maintain:
1577 * pnum - array of number of consecutive packets with the same dest port for
1578 * each packet in the input burst.
1579 * lp - pointer to the last updated element in the pnum.
1580 * dlp - dest port value lp corresponds to.
1583 #define GRPSZ (1 << FWDSTEP)
1584 #define GRPMSK (GRPSZ - 1)
1586 #define GROUP_PORT_STEP(dlp, dcp, lp, pn, idx) do { \
1587 if (likely((dlp) == (dcp)[(idx)])) { \
1590 (dlp) = (dcp)[idx]; \
1591 (lp) = (pn) + (idx); \
1597 * Group consecutive packets with the same destination port in bursts of 4.
1598 * Suppose we have array of destionation ports:
1599 * dst_port[] = {a, b, c, d,, e, ... }
1600 * dp1 should contain: <a, b, c, d>, dp2: <b, c, d, e>.
1601 * We doing 4 comparisions at once and the result is 4 bit mask.
1602 * This mask is used as an index into prebuild array of pnum values.
1604 static inline uint16_t *
1605 port_groupx4(uint16_t pn[FWDSTEP + 1], uint16_t *lp, __m128i dp1, __m128i dp2)
1607 static const struct {
1608 uint64_t pnum; /* prebuild 4 values for pnum[]. */
1609 int32_t idx; /* index for new last updated elemnet. */
1610 uint16_t lpv; /* add value to the last updated element. */
1613 /* 0: a != b, b != c, c != d, d != e */
1614 .pnum = UINT64_C(0x0001000100010001),
1619 /* 1: a == b, b != c, c != d, d != e */
1620 .pnum = UINT64_C(0x0001000100010002),
1625 /* 2: a != b, b == c, c != d, d != e */
1626 .pnum = UINT64_C(0x0001000100020001),
1631 /* 3: a == b, b == c, c != d, d != e */
1632 .pnum = UINT64_C(0x0001000100020003),
1637 /* 4: a != b, b != c, c == d, d != e */
1638 .pnum = UINT64_C(0x0001000200010001),
1643 /* 5: a == b, b != c, c == d, d != e */
1644 .pnum = UINT64_C(0x0001000200010002),
1649 /* 6: a != b, b == c, c == d, d != e */
1650 .pnum = UINT64_C(0x0001000200030001),
1655 /* 7: a == b, b == c, c == d, d != e */
1656 .pnum = UINT64_C(0x0001000200030004),
1661 /* 8: a != b, b != c, c != d, d == e */
1662 .pnum = UINT64_C(0x0002000100010001),
1667 /* 9: a == b, b != c, c != d, d == e */
1668 .pnum = UINT64_C(0x0002000100010002),
1673 /* 0xa: a != b, b == c, c != d, d == e */
1674 .pnum = UINT64_C(0x0002000100020001),
1679 /* 0xb: a == b, b == c, c != d, d == e */
1680 .pnum = UINT64_C(0x0002000100020003),
1685 /* 0xc: a != b, b != c, c == d, d == e */
1686 .pnum = UINT64_C(0x0002000300010001),
1691 /* 0xd: a == b, b != c, c == d, d == e */
1692 .pnum = UINT64_C(0x0002000300010002),
1697 /* 0xe: a != b, b == c, c == d, d == e */
1698 .pnum = UINT64_C(0x0002000300040001),
1703 /* 0xf: a == b, b == c, c == d, d == e */
1704 .pnum = UINT64_C(0x0002000300040005),
1711 uint16_t u16[FWDSTEP + 1];
1713 } *pnum = (void *)pn;
1717 dp1 = _mm_cmpeq_epi16(dp1, dp2);
1718 dp1 = _mm_unpacklo_epi16(dp1, dp1);
1719 v = _mm_movemask_ps((__m128)dp1);
1721 /* update last port counter. */
1722 lp[0] += gptbl[v].lpv;
1724 /* if dest port value has changed. */
1726 pnum->u64 = gptbl[v].pnum;
1727 pnum->u16[FWDSTEP] = 1;
1728 lp = pnum->u16 + gptbl[v].idx;
1734 #endif /* APP_LOOKUP_METHOD */
1737 process_burst(struct rte_mbuf *pkts_burst[MAX_PKT_BURST], int nb_rx,
1742 #if ((APP_LOOKUP_METHOD == APP_LOOKUP_LPM) && \
1743 (ENABLE_MULTI_BUFFER_OPTIMIZE == 1))
1747 uint16_t dst_port[MAX_PKT_BURST];
1748 __m128i dip[MAX_PKT_BURST / FWDSTEP];
1749 uint32_t ipv4_flag[MAX_PKT_BURST / FWDSTEP];
1750 uint16_t pnum[MAX_PKT_BURST + 1];
1754 #if (ENABLE_MULTI_BUFFER_OPTIMIZE == 1)
1755 #if (APP_LOOKUP_METHOD == APP_LOOKUP_EXACT_MATCH)
1758 * Send nb_rx - nb_rx%8 packets
1761 int32_t n = RTE_ALIGN_FLOOR(nb_rx, 8);
1763 for (j = 0; j < n; j += 8) {
1765 pkts_burst[j]->packet_type &
1766 pkts_burst[j+1]->packet_type &
1767 pkts_burst[j+2]->packet_type &
1768 pkts_burst[j+3]->packet_type &
1769 pkts_burst[j+4]->packet_type &
1770 pkts_burst[j+5]->packet_type &
1771 pkts_burst[j+6]->packet_type &
1772 pkts_burst[j+7]->packet_type;
1773 if (pkt_type & RTE_PTYPE_L3_IPV4) {
1774 simple_ipv4_fwd_8pkts(&pkts_burst[j], portid);
1775 } else if (pkt_type &
1776 RTE_PTYPE_L3_IPV6) {
1777 simple_ipv6_fwd_8pkts(&pkts_burst[j], portid);
1779 l3fwd_simple_forward(pkts_burst[j], portid);
1780 l3fwd_simple_forward(pkts_burst[j+1], portid);
1781 l3fwd_simple_forward(pkts_burst[j+2], portid);
1782 l3fwd_simple_forward(pkts_burst[j+3], portid);
1783 l3fwd_simple_forward(pkts_burst[j+4], portid);
1784 l3fwd_simple_forward(pkts_burst[j+5], portid);
1785 l3fwd_simple_forward(pkts_burst[j+6], portid);
1786 l3fwd_simple_forward(pkts_burst[j+7], portid);
1789 for (; j < nb_rx ; j++)
1790 l3fwd_simple_forward(pkts_burst[j], portid);
1792 #elif (APP_LOOKUP_METHOD == APP_LOOKUP_LPM)
1794 k = RTE_ALIGN_FLOOR(nb_rx, FWDSTEP);
1795 for (j = 0; j != k; j += FWDSTEP)
1796 processx4_step1(&pkts_burst[j], &dip[j / FWDSTEP],
1797 &ipv4_flag[j / FWDSTEP]);
1799 k = RTE_ALIGN_FLOOR(nb_rx, FWDSTEP);
1800 for (j = 0; j != k; j += FWDSTEP)
1801 processx4_step2(dip[j / FWDSTEP], ipv4_flag[j / FWDSTEP],
1802 portid, &pkts_burst[j], &dst_port[j]);
1805 * Finish packet processing and group consecutive
1806 * packets with the same destination port.
1808 k = RTE_ALIGN_FLOOR(nb_rx, FWDSTEP);
1815 processx4_step3(pkts_burst, dst_port);
1817 /* dp1: <d[0], d[1], d[2], d[3], ... > */
1818 dp1 = _mm_loadu_si128((__m128i *)dst_port);
1820 for (j = FWDSTEP; j != k; j += FWDSTEP) {
1821 processx4_step3(&pkts_burst[j], &dst_port[j]);
1825 * <d[j-3], d[j-2], d[j-1], d[j], ... >
1827 dp2 = _mm_loadu_si128(
1828 (__m128i *)&dst_port[j - FWDSTEP + 1]);
1829 lp = port_groupx4(&pnum[j - FWDSTEP], lp, dp1, dp2);
1833 * <d[j], d[j+1], d[j+2], d[j+3], ... >
1835 dp1 = _mm_srli_si128(dp2, (FWDSTEP - 1) *
1836 sizeof(dst_port[0]));
1840 * dp2: <d[j-3], d[j-2], d[j-1], d[j-1], ... >
1842 dp2 = _mm_shufflelo_epi16(dp1, 0xf9);
1843 lp = port_groupx4(&pnum[j - FWDSTEP], lp, dp1, dp2);
1846 * remove values added by the last repeated
1850 dlp = dst_port[j - 1];
1852 /* set dlp and lp to the never used values. */
1854 lp = pnum + MAX_PKT_BURST;
1857 /* Process up to last 3 packets one by one. */
1858 switch (nb_rx % FWDSTEP) {
1860 process_packet(pkts_burst[j], dst_port + j, portid);
1861 GROUP_PORT_STEP(dlp, dst_port, lp, pnum, j);
1864 process_packet(pkts_burst[j], dst_port + j, portid);
1865 GROUP_PORT_STEP(dlp, dst_port, lp, pnum, j);
1868 process_packet(pkts_burst[j], dst_port + j, portid);
1869 GROUP_PORT_STEP(dlp, dst_port, lp, pnum, j);
1874 * Send packets out, through destination port.
1875 * Consecuteve pacekts with the same destination port
1876 * are already grouped together.
1877 * If destination port for the packet equals BAD_PORT,
1878 * then free the packet without sending it out.
1880 for (j = 0; j < nb_rx; j += k) {
1888 if (likely(pn != BAD_PORT))
1889 send_packetsx4(pn, pkts_burst + j, k);
1891 for (m = j; m != j + k; m++)
1892 rte_pktmbuf_free(pkts_burst[m]);
1896 #endif /* APP_LOOKUP_METHOD */
1897 #else /* ENABLE_MULTI_BUFFER_OPTIMIZE == 0 */
1899 /* Prefetch first packets */
1900 for (j = 0; j < PREFETCH_OFFSET && j < nb_rx; j++)
1901 rte_prefetch0(rte_pktmbuf_mtod(pkts_burst[j], void *));
1903 /* Prefetch and forward already prefetched packets */
1904 for (j = 0; j < (nb_rx - PREFETCH_OFFSET); j++) {
1905 rte_prefetch0(rte_pktmbuf_mtod(pkts_burst[
1906 j + PREFETCH_OFFSET], void *));
1907 l3fwd_simple_forward(pkts_burst[j], portid);
1910 /* Forward remaining prefetched packets */
1911 for (; j < nb_rx; j++)
1912 l3fwd_simple_forward(pkts_burst[j], portid);
1914 #endif /* ENABLE_MULTI_BUFFER_OPTIMIZE */
1918 #if (APP_CPU_LOAD > 0)
1921 * CPU-load stats collector
1924 cpu_load_collector(__rte_unused void *arg) {
1927 uint64_t prev_tsc, diff_tsc, cur_tsc;
1928 uint64_t total[MAX_CPU] = { 0 };
1929 unsigned min_cpu = MAX_CPU;
1930 unsigned max_cpu = 0;
1935 unsigned int n_thread_per_cpu[MAX_CPU] = { 0 };
1936 struct thread_conf *thread_per_cpu[MAX_CPU][MAX_THREAD];
1938 struct thread_conf *thread_conf;
1940 const uint64_t interval_tsc = (rte_get_tsc_hz() + US_PER_S - 1) /
1941 US_PER_S * CPU_LOAD_TIMEOUT_US;
1945 * Wait for all threads
1948 printf("Waiting for %d rx threads and %d tx threads\n", n_rx_thread,
1951 while (rte_atomic16_read(&rx_counter) < n_rx_thread)
1954 while (rte_atomic16_read(&tx_counter) < n_tx_thread)
1957 for (i = 0; i < n_rx_thread; i++) {
1959 thread_conf = &rx_thread[i].conf;
1960 cpu_id = thread_conf->cpu_id;
1961 thread_per_cpu[cpu_id][n_thread_per_cpu[cpu_id]++] = thread_conf;
1963 if (cpu_id > max_cpu)
1965 if (cpu_id < min_cpu)
1968 for (i = 0; i < n_tx_thread; i++) {
1970 thread_conf = &tx_thread[i].conf;
1971 cpu_id = thread_conf->cpu_id;
1972 thread_per_cpu[cpu_id][n_thread_per_cpu[cpu_id]++] = thread_conf;
1974 if (thread_conf->cpu_id > max_cpu)
1975 max_cpu = thread_conf->cpu_id;
1976 if (thread_conf->cpu_id < min_cpu)
1977 min_cpu = thread_conf->cpu_id;
1983 for (i = min_cpu; i <= max_cpu; i++) {
1984 for (j = 0; j < MAX_CPU_COUNTER; j++) {
1985 for (k = 0; k < n_thread_per_cpu[i]; k++)
1986 if (thread_per_cpu[i][k]->busy[j]) {
1991 cpu_load.hits[j][i]++;
2003 cur_tsc = rte_rdtsc();
2005 diff_tsc = cur_tsc - prev_tsc;
2006 if (unlikely(diff_tsc > interval_tsc)) {
2010 printf("Cpu usage for %d rx threads and %d tx threads:\n\n",
2011 n_rx_thread, n_tx_thread);
2013 printf("cpu# proc%% poll%% overhead%%\n\n");
2015 for (i = min_cpu; i <= max_cpu; i++) {
2017 printf("CPU %d:", i);
2018 for (j = 0; j < MAX_CPU_COUNTER; j++) {
2019 printf("%7" PRIu64 "",
2020 cpu_load.hits[j][i] * 100 / cpu_load.counter);
2021 hits += cpu_load.hits[j][i];
2022 cpu_load.hits[j][i] = 0;
2024 printf("%7" PRIu64 "\n",
2025 100 - total[i] * 100 / cpu_load.counter);
2028 cpu_load.counter = 0;
2035 #endif /* APP_CPU_LOAD */
2038 * Null processing lthread loop
2040 * This loop is used to start empty scheduler on lcore.
2043 lthread_null(__rte_unused void *args)
2045 int lcore_id = rte_lcore_id();
2047 RTE_LOG(INFO, L3FWD, "Starting scheduler on lcore %d.\n", lcore_id);
2051 /* main processing loop */
2053 lthread_tx_per_ring(void *dummy)
2057 struct rte_ring *ring;
2058 struct thread_tx_conf *tx_conf;
2059 struct rte_mbuf *pkts_burst[MAX_PKT_BURST];
2060 struct lthread_cond *ready;
2062 tx_conf = (struct thread_tx_conf *)dummy;
2063 ring = tx_conf->ring;
2064 ready = *tx_conf->ready;
2066 lthread_set_data((void *)tx_conf);
2069 * Move this lthread to lcore
2071 lthread_set_affinity(tx_conf->conf.lcore_id);
2073 RTE_LOG(INFO, L3FWD, "entering main tx loop on lcore %u\n", rte_lcore_id());
2076 rte_atomic16_inc(&tx_counter);
2080 * Read packet from ring
2082 SET_CPU_BUSY(tx_conf, CPU_POLL);
2083 nb_rx = rte_ring_sc_dequeue_burst(ring, (void **)pkts_burst,
2084 MAX_PKT_BURST, NULL);
2085 SET_CPU_IDLE(tx_conf, CPU_POLL);
2088 SET_CPU_BUSY(tx_conf, CPU_PROCESS);
2089 portid = pkts_burst[0]->port;
2090 process_burst(pkts_burst, nb_rx, portid);
2091 SET_CPU_IDLE(tx_conf, CPU_PROCESS);
2094 lthread_cond_wait(ready, 0);
2100 * Main tx-lthreads spawner lthread.
2102 * This lthread is used to spawn one new lthread per ring from producers.
2106 lthread_tx(void *args)
2112 struct thread_tx_conf *tx_conf;
2114 tx_conf = (struct thread_tx_conf *)args;
2115 lthread_set_data((void *)tx_conf);
2118 * Move this lthread to the selected lcore
2120 lthread_set_affinity(tx_conf->conf.lcore_id);
2123 * Spawn tx readers (one per input ring)
2125 lthread_create(<, tx_conf->conf.lcore_id, lthread_tx_per_ring,
2128 lcore_id = rte_lcore_id();
2130 RTE_LOG(INFO, L3FWD, "Entering Tx main loop on lcore %u\n", lcore_id);
2132 tx_conf->conf.cpu_id = sched_getcpu();
2135 lthread_sleep(BURST_TX_DRAIN_US * 1000);
2138 * TX burst queue drain
2140 for (portid = 0; portid < RTE_MAX_ETHPORTS; portid++) {
2141 if (tx_conf->tx_mbufs[portid].len == 0)
2143 SET_CPU_BUSY(tx_conf, CPU_PROCESS);
2144 send_burst(tx_conf, tx_conf->tx_mbufs[portid].len, portid);
2145 SET_CPU_IDLE(tx_conf, CPU_PROCESS);
2146 tx_conf->tx_mbufs[portid].len = 0;
2153 lthread_rx(void *dummy)
2158 uint8_t portid, queueid;
2160 int len[RTE_MAX_LCORE] = { 0 };
2161 int old_len, new_len;
2162 struct rte_mbuf *pkts_burst[MAX_PKT_BURST];
2163 struct thread_rx_conf *rx_conf;
2165 rx_conf = (struct thread_rx_conf *)dummy;
2166 lthread_set_data((void *)rx_conf);
2169 * Move this lthread to lcore
2171 lthread_set_affinity(rx_conf->conf.lcore_id);
2173 if (rx_conf->n_rx_queue == 0) {
2174 RTE_LOG(INFO, L3FWD, "lcore %u has nothing to do\n", rte_lcore_id());
2178 RTE_LOG(INFO, L3FWD, "Entering main Rx loop on lcore %u\n", rte_lcore_id());
2180 for (i = 0; i < rx_conf->n_rx_queue; i++) {
2182 portid = rx_conf->rx_queue_list[i].port_id;
2183 queueid = rx_conf->rx_queue_list[i].queue_id;
2184 RTE_LOG(INFO, L3FWD, " -- lcoreid=%u portid=%hhu rxqueueid=%hhu\n",
2185 rte_lcore_id(), portid, queueid);
2189 * Init all condition variables (one per rx thread)
2191 for (i = 0; i < rx_conf->n_rx_queue; i++)
2192 lthread_cond_init(NULL, &rx_conf->ready[i], NULL);
2196 rx_conf->conf.cpu_id = sched_getcpu();
2197 rte_atomic16_inc(&rx_counter);
2201 * Read packet from RX queues
2203 for (i = 0; i < rx_conf->n_rx_queue; ++i) {
2204 portid = rx_conf->rx_queue_list[i].port_id;
2205 queueid = rx_conf->rx_queue_list[i].queue_id;
2207 SET_CPU_BUSY(rx_conf, CPU_POLL);
2208 nb_rx = rte_eth_rx_burst(portid, queueid, pkts_burst,
2210 SET_CPU_IDLE(rx_conf, CPU_POLL);
2213 worker_id = (worker_id + 1) % rx_conf->n_ring;
2214 old_len = len[worker_id];
2216 SET_CPU_BUSY(rx_conf, CPU_PROCESS);
2217 ret = rte_ring_sp_enqueue_burst(
2218 rx_conf->ring[worker_id],
2219 (void **) pkts_burst,
2222 new_len = old_len + ret;
2224 if (new_len >= BURST_SIZE) {
2225 lthread_cond_signal(rx_conf->ready[worker_id]);
2229 len[worker_id] = new_len;
2231 if (unlikely(ret < nb_rx)) {
2234 for (k = ret; k < nb_rx; k++) {
2235 struct rte_mbuf *m = pkts_burst[k];
2237 rte_pktmbuf_free(m);
2240 SET_CPU_IDLE(rx_conf, CPU_PROCESS);
2249 * Start scheduler with initial lthread on lcore
2251 * This lthread loop spawns all rx and tx lthreads on master lcore
2255 lthread_spawner(__rte_unused void *arg) {
2256 struct lthread *lt[MAX_THREAD];
2260 printf("Entering lthread_spawner\n");
2263 * Create producers (rx threads) on default lcore
2265 for (i = 0; i < n_rx_thread; i++) {
2266 rx_thread[i].conf.thread_id = i;
2267 lthread_create(<[n_thread], -1, lthread_rx,
2268 (void *)&rx_thread[i]);
2273 * Wait for all producers. Until some producers can be started on the same
2274 * scheduler as this lthread, yielding is required to let them to run and
2275 * prevent deadlock here.
2277 while (rte_atomic16_read(&rx_counter) < n_rx_thread)
2278 lthread_sleep(100000);
2281 * Create consumers (tx threads) on default lcore_id
2283 for (i = 0; i < n_tx_thread; i++) {
2284 tx_thread[i].conf.thread_id = i;
2285 lthread_create(<[n_thread], -1, lthread_tx,
2286 (void *)&tx_thread[i]);
2291 * Wait for all threads finished
2293 for (i = 0; i < n_thread; i++)
2294 lthread_join(lt[i], NULL);
2299 * Start master scheduler with initial lthread spawning rx and tx lthreads
2300 * (main_lthread_master).
2303 lthread_master_spawner(__rte_unused void *arg) {
2305 int lcore_id = rte_lcore_id();
2307 RTE_PER_LCORE(lcore_conf) = &lcore_conf[lcore_id];
2308 lthread_create(<, -1, lthread_spawner, NULL);
2315 * Start scheduler on lcore.
2318 sched_spawner(__rte_unused void *arg) {
2320 int lcore_id = rte_lcore_id();
2323 if (lcore_id == cpu_load_lcore_id) {
2324 cpu_load_collector(arg);
2327 #endif /* APP_CPU_LOAD */
2329 RTE_PER_LCORE(lcore_conf) = &lcore_conf[lcore_id];
2330 lthread_create(<, -1, lthread_null, NULL);
2336 /* main processing loop */
2338 pthread_tx(void *dummy)
2340 struct rte_mbuf *pkts_burst[MAX_PKT_BURST];
2341 uint64_t prev_tsc, diff_tsc, cur_tsc;
2344 struct thread_tx_conf *tx_conf;
2346 const uint64_t drain_tsc = (rte_get_tsc_hz() + US_PER_S - 1) /
2347 US_PER_S * BURST_TX_DRAIN_US;
2351 tx_conf = (struct thread_tx_conf *)dummy;
2353 RTE_LOG(INFO, L3FWD, "Entering main Tx loop on lcore %u\n", rte_lcore_id());
2355 tx_conf->conf.cpu_id = sched_getcpu();
2356 rte_atomic16_inc(&tx_counter);
2359 cur_tsc = rte_rdtsc();
2362 * TX burst queue drain
2364 diff_tsc = cur_tsc - prev_tsc;
2365 if (unlikely(diff_tsc > drain_tsc)) {
2368 * This could be optimized (use queueid instead of
2369 * portid), but it is not called so often
2371 SET_CPU_BUSY(tx_conf, CPU_PROCESS);
2372 for (portid = 0; portid < RTE_MAX_ETHPORTS; portid++) {
2373 if (tx_conf->tx_mbufs[portid].len == 0)
2375 send_burst(tx_conf, tx_conf->tx_mbufs[portid].len, portid);
2376 tx_conf->tx_mbufs[portid].len = 0;
2378 SET_CPU_IDLE(tx_conf, CPU_PROCESS);
2384 * Read packet from ring
2386 SET_CPU_BUSY(tx_conf, CPU_POLL);
2387 nb_rx = rte_ring_sc_dequeue_burst(tx_conf->ring,
2388 (void **)pkts_burst, MAX_PKT_BURST, NULL);
2389 SET_CPU_IDLE(tx_conf, CPU_POLL);
2391 if (unlikely(nb_rx == 0)) {
2396 SET_CPU_BUSY(tx_conf, CPU_PROCESS);
2397 portid = pkts_burst[0]->port;
2398 process_burst(pkts_burst, nb_rx, portid);
2399 SET_CPU_IDLE(tx_conf, CPU_PROCESS);
2405 pthread_rx(void *dummy)
2412 uint8_t portid, queueid;
2413 struct rte_mbuf *pkts_burst[MAX_PKT_BURST];
2415 struct thread_rx_conf *rx_conf;
2417 lcore_id = rte_lcore_id();
2418 rx_conf = (struct thread_rx_conf *)dummy;
2420 if (rx_conf->n_rx_queue == 0) {
2421 RTE_LOG(INFO, L3FWD, "lcore %u has nothing to do\n", lcore_id);
2425 RTE_LOG(INFO, L3FWD, "entering main rx loop on lcore %u\n", lcore_id);
2427 for (i = 0; i < rx_conf->n_rx_queue; i++) {
2429 portid = rx_conf->rx_queue_list[i].port_id;
2430 queueid = rx_conf->rx_queue_list[i].queue_id;
2431 RTE_LOG(INFO, L3FWD, " -- lcoreid=%u portid=%hhu rxqueueid=%hhu\n",
2432 lcore_id, portid, queueid);
2436 rx_conf->conf.cpu_id = sched_getcpu();
2437 rte_atomic16_inc(&rx_counter);
2441 * Read packet from RX queues
2443 for (i = 0; i < rx_conf->n_rx_queue; ++i) {
2444 portid = rx_conf->rx_queue_list[i].port_id;
2445 queueid = rx_conf->rx_queue_list[i].queue_id;
2447 SET_CPU_BUSY(rx_conf, CPU_POLL);
2448 nb_rx = rte_eth_rx_burst(portid, queueid, pkts_burst,
2450 SET_CPU_IDLE(rx_conf, CPU_POLL);
2457 SET_CPU_BUSY(rx_conf, CPU_PROCESS);
2458 worker_id = (worker_id + 1) % rx_conf->n_ring;
2459 n = rte_ring_sp_enqueue_burst(rx_conf->ring[worker_id],
2460 (void **)pkts_burst, nb_rx, NULL);
2462 if (unlikely(n != nb_rx)) {
2465 for (k = n; k < nb_rx; k++) {
2466 struct rte_mbuf *m = pkts_burst[k];
2468 rte_pktmbuf_free(m);
2472 SET_CPU_IDLE(rx_conf, CPU_PROCESS);
2482 pthread_run(__rte_unused void *arg) {
2483 int lcore_id = rte_lcore_id();
2486 for (i = 0; i < n_rx_thread; i++)
2487 if (rx_thread[i].conf.lcore_id == lcore_id) {
2488 printf("Start rx thread on %d...\n", lcore_id);
2489 RTE_PER_LCORE(lcore_conf) = &lcore_conf[lcore_id];
2490 RTE_PER_LCORE(lcore_conf)->data = (void *)&rx_thread[i];
2491 pthread_rx((void *)&rx_thread[i]);
2495 for (i = 0; i < n_tx_thread; i++)
2496 if (tx_thread[i].conf.lcore_id == lcore_id) {
2497 printf("Start tx thread on %d...\n", lcore_id);
2498 RTE_PER_LCORE(lcore_conf) = &lcore_conf[lcore_id];
2499 RTE_PER_LCORE(lcore_conf)->data = (void *)&tx_thread[i];
2500 pthread_tx((void *)&tx_thread[i]);
2505 if (lcore_id == cpu_load_lcore_id)
2506 cpu_load_collector(arg);
2507 #endif /* APP_CPU_LOAD */
2513 check_lcore_params(void)
2515 uint8_t queue, lcore;
2519 for (i = 0; i < nb_rx_thread_params; ++i) {
2520 queue = rx_thread_params[i].queue_id;
2521 if (queue >= MAX_RX_QUEUE_PER_PORT) {
2522 printf("invalid queue number: %hhu\n", queue);
2525 lcore = rx_thread_params[i].lcore_id;
2526 if (!rte_lcore_is_enabled(lcore)) {
2527 printf("error: lcore %hhu is not enabled in lcore mask\n", lcore);
2530 socketid = rte_lcore_to_socket_id(lcore);
2531 if ((socketid != 0) && (numa_on == 0))
2532 printf("warning: lcore %hhu is on socket %d with numa off\n",
2539 check_port_config(const unsigned nb_ports)
2544 for (i = 0; i < nb_rx_thread_params; ++i) {
2545 portid = rx_thread_params[i].port_id;
2546 if ((enabled_port_mask & (1 << portid)) == 0) {
2547 printf("port %u is not enabled in port mask\n", portid);
2550 if (portid >= nb_ports) {
2551 printf("port %u is not present on the board\n", portid);
2559 get_port_n_rx_queues(const uint8_t port)
2564 for (i = 0; i < nb_rx_thread_params; ++i)
2565 if (rx_thread_params[i].port_id == port &&
2566 rx_thread_params[i].queue_id > queue)
2567 queue = rx_thread_params[i].queue_id;
2569 return (uint8_t)(++queue);
2576 struct thread_rx_conf *rx_conf;
2577 struct thread_tx_conf *tx_conf;
2578 unsigned rx_thread_id, tx_thread_id;
2580 struct rte_ring *ring = NULL;
2582 for (tx_thread_id = 0; tx_thread_id < n_tx_thread; tx_thread_id++) {
2584 tx_conf = &tx_thread[tx_thread_id];
2586 printf("Connecting tx-thread %d with rx-thread %d\n", tx_thread_id,
2587 tx_conf->conf.thread_id);
2589 rx_thread_id = tx_conf->conf.thread_id;
2590 if (rx_thread_id > n_tx_thread) {
2591 printf("connection from tx-thread %u to rx-thread %u fails "
2592 "(rx-thread not defined)\n", tx_thread_id, rx_thread_id);
2596 rx_conf = &rx_thread[rx_thread_id];
2597 socket_io = rte_lcore_to_socket_id(rx_conf->conf.lcore_id);
2599 snprintf(name, sizeof(name), "app_ring_s%u_rx%u_tx%u",
2600 socket_io, rx_thread_id, tx_thread_id);
2602 ring = rte_ring_create(name, 1024 * 4, socket_io,
2603 RING_F_SP_ENQ | RING_F_SC_DEQ);
2606 rte_panic("Cannot create ring to connect rx-thread %u "
2607 "with tx-thread %u\n", rx_thread_id, tx_thread_id);
2610 rx_conf->ring[rx_conf->n_ring] = ring;
2612 tx_conf->ring = ring;
2613 tx_conf->ready = &rx_conf->ready[rx_conf->n_ring];
2621 init_rx_queues(void)
2623 uint16_t i, nb_rx_queue;
2628 for (i = 0; i < nb_rx_thread_params; ++i) {
2629 thread = rx_thread_params[i].thread_id;
2630 nb_rx_queue = rx_thread[thread].n_rx_queue;
2632 if (nb_rx_queue >= MAX_RX_QUEUE_PER_LCORE) {
2633 printf("error: too many queues (%u) for thread: %u\n",
2634 (unsigned)nb_rx_queue + 1, (unsigned)thread);
2638 rx_thread[thread].conf.thread_id = thread;
2639 rx_thread[thread].conf.lcore_id = rx_thread_params[i].lcore_id;
2640 rx_thread[thread].rx_queue_list[nb_rx_queue].port_id =
2641 rx_thread_params[i].port_id;
2642 rx_thread[thread].rx_queue_list[nb_rx_queue].queue_id =
2643 rx_thread_params[i].queue_id;
2644 rx_thread[thread].n_rx_queue++;
2646 if (thread >= n_rx_thread)
2647 n_rx_thread = thread + 1;
2654 init_tx_threads(void)
2659 for (i = 0; i < nb_tx_thread_params; ++i) {
2660 tx_thread[n_tx_thread].conf.thread_id = tx_thread_params[i].thread_id;
2661 tx_thread[n_tx_thread].conf.lcore_id = tx_thread_params[i].lcore_id;
2669 print_usage(const char *prgname)
2671 printf("%s [EAL options] -- -p PORTMASK -P"
2672 " [--rx (port,queue,lcore,thread)[,(port,queue,lcore,thread]]"
2673 " [--tx (lcore,thread)[,(lcore,thread]]"
2674 " [--enable-jumbo [--max-pkt-len PKTLEN]]\n"
2675 " [--parse-ptype]\n\n"
2676 " -p PORTMASK: hexadecimal bitmask of ports to configure\n"
2677 " -P : enable promiscuous mode\n"
2678 " --rx (port,queue,lcore,thread): rx queues configuration\n"
2679 " --tx (lcore,thread): tx threads configuration\n"
2680 " --stat-lcore LCORE: use lcore for stat collector\n"
2681 " --eth-dest=X,MM:MM:MM:MM:MM:MM: optional, ethernet destination for port X\n"
2682 " --no-numa: optional, disable numa awareness\n"
2683 " --ipv6: optional, specify it if running ipv6 packets\n"
2684 " --enable-jumbo: enable jumbo frame"
2685 " which max packet len is PKTLEN in decimal (64-9600)\n"
2686 " --hash-entry-num: specify the hash entry number in hexadecimal to be setup\n"
2687 " --no-lthreads: turn off lthread model\n"
2688 " --parse-ptype: set to use software to analyze packet type\n\n",
2692 static int parse_max_pkt_len(const char *pktlen)
2697 /* parse decimal string */
2698 len = strtoul(pktlen, &end, 10);
2699 if ((pktlen[0] == '\0') || (end == NULL) || (*end != '\0'))
2709 parse_portmask(const char *portmask)
2714 /* parse hexadecimal string */
2715 pm = strtoul(portmask, &end, 16);
2716 if ((portmask[0] == '\0') || (end == NULL) || (*end != '\0'))
2725 #if (APP_LOOKUP_METHOD == APP_LOOKUP_EXACT_MATCH)
2727 parse_hash_entry_number(const char *hash_entry_num)
2730 unsigned long hash_en;
2732 /* parse hexadecimal string */
2733 hash_en = strtoul(hash_entry_num, &end, 16);
2734 if ((hash_entry_num[0] == '\0') || (end == NULL) || (*end != '\0'))
2745 parse_rx_config(const char *q_arg)
2748 const char *p, *p0 = q_arg;
2757 unsigned long int_fld[_NUM_FLD];
2758 char *str_fld[_NUM_FLD];
2762 nb_rx_thread_params = 0;
2764 while ((p = strchr(p0, '(')) != NULL) {
2766 p0 = strchr(p, ')');
2771 if (size >= sizeof(s))
2774 snprintf(s, sizeof(s), "%.*s", size, p);
2775 if (rte_strsplit(s, sizeof(s), str_fld, _NUM_FLD, ',') != _NUM_FLD)
2777 for (i = 0; i < _NUM_FLD; i++) {
2779 int_fld[i] = strtoul(str_fld[i], &end, 0);
2780 if (errno != 0 || end == str_fld[i] || int_fld[i] > 255)
2783 if (nb_rx_thread_params >= MAX_LCORE_PARAMS) {
2784 printf("exceeded max number of rx params: %hu\n",
2785 nb_rx_thread_params);
2788 rx_thread_params_array[nb_rx_thread_params].port_id =
2789 (uint8_t)int_fld[FLD_PORT];
2790 rx_thread_params_array[nb_rx_thread_params].queue_id =
2791 (uint8_t)int_fld[FLD_QUEUE];
2792 rx_thread_params_array[nb_rx_thread_params].lcore_id =
2793 (uint8_t)int_fld[FLD_LCORE];
2794 rx_thread_params_array[nb_rx_thread_params].thread_id =
2795 (uint8_t)int_fld[FLD_THREAD];
2796 ++nb_rx_thread_params;
2798 rx_thread_params = rx_thread_params_array;
2803 parse_tx_config(const char *q_arg)
2806 const char *p, *p0 = q_arg;
2813 unsigned long int_fld[_NUM_FLD];
2814 char *str_fld[_NUM_FLD];
2818 nb_tx_thread_params = 0;
2820 while ((p = strchr(p0, '(')) != NULL) {
2822 p0 = strchr(p, ')');
2827 if (size >= sizeof(s))
2830 snprintf(s, sizeof(s), "%.*s", size, p);
2831 if (rte_strsplit(s, sizeof(s), str_fld, _NUM_FLD, ',') != _NUM_FLD)
2833 for (i = 0; i < _NUM_FLD; i++) {
2835 int_fld[i] = strtoul(str_fld[i], &end, 0);
2836 if (errno != 0 || end == str_fld[i] || int_fld[i] > 255)
2839 if (nb_tx_thread_params >= MAX_LCORE_PARAMS) {
2840 printf("exceeded max number of tx params: %hu\n",
2841 nb_tx_thread_params);
2844 tx_thread_params_array[nb_tx_thread_params].lcore_id =
2845 (uint8_t)int_fld[FLD_LCORE];
2846 tx_thread_params_array[nb_tx_thread_params].thread_id =
2847 (uint8_t)int_fld[FLD_THREAD];
2848 ++nb_tx_thread_params;
2850 tx_thread_params = tx_thread_params_array;
2855 #if (APP_CPU_LOAD > 0)
2857 parse_stat_lcore(const char *stat_lcore)
2860 unsigned long lcore_id;
2862 lcore_id = strtoul(stat_lcore, &end, 10);
2863 if ((stat_lcore[0] == '\0') || (end == NULL) || (*end != '\0'))
2871 parse_eth_dest(const char *optarg)
2875 uint8_t c, *dest, peer_addr[6];
2878 portid = strtoul(optarg, &port_end, 10);
2879 if (errno != 0 || port_end == optarg || *port_end++ != ',')
2880 rte_exit(EXIT_FAILURE,
2881 "Invalid eth-dest: %s", optarg);
2882 if (portid >= RTE_MAX_ETHPORTS)
2883 rte_exit(EXIT_FAILURE,
2884 "eth-dest: port %d >= RTE_MAX_ETHPORTS(%d)\n",
2885 portid, RTE_MAX_ETHPORTS);
2887 if (cmdline_parse_etheraddr(NULL, port_end,
2888 &peer_addr, sizeof(peer_addr)) < 0)
2889 rte_exit(EXIT_FAILURE,
2890 "Invalid ethernet address: %s\n",
2892 dest = (uint8_t *)&dest_eth_addr[portid];
2893 for (c = 0; c < 6; c++)
2894 dest[c] = peer_addr[c];
2895 *(uint64_t *)(val_eth + portid) = dest_eth_addr[portid];
2898 #define CMD_LINE_OPT_RX_CONFIG "rx"
2899 #define CMD_LINE_OPT_TX_CONFIG "tx"
2900 #define CMD_LINE_OPT_STAT_LCORE "stat-lcore"
2901 #define CMD_LINE_OPT_ETH_DEST "eth-dest"
2902 #define CMD_LINE_OPT_NO_NUMA "no-numa"
2903 #define CMD_LINE_OPT_IPV6 "ipv6"
2904 #define CMD_LINE_OPT_ENABLE_JUMBO "enable-jumbo"
2905 #define CMD_LINE_OPT_HASH_ENTRY_NUM "hash-entry-num"
2906 #define CMD_LINE_OPT_NO_LTHREADS "no-lthreads"
2907 #define CMD_LINE_OPT_PARSE_PTYPE "parse-ptype"
2909 /* Parse the argument given in the command line of the application */
2911 parse_args(int argc, char **argv)
2916 char *prgname = argv[0];
2917 static struct option lgopts[] = {
2918 {CMD_LINE_OPT_RX_CONFIG, 1, 0, 0},
2919 {CMD_LINE_OPT_TX_CONFIG, 1, 0, 0},
2920 {CMD_LINE_OPT_STAT_LCORE, 1, 0, 0},
2921 {CMD_LINE_OPT_ETH_DEST, 1, 0, 0},
2922 {CMD_LINE_OPT_NO_NUMA, 0, 0, 0},
2923 {CMD_LINE_OPT_IPV6, 0, 0, 0},
2924 {CMD_LINE_OPT_ENABLE_JUMBO, 0, 0, 0},
2925 {CMD_LINE_OPT_HASH_ENTRY_NUM, 1, 0, 0},
2926 {CMD_LINE_OPT_NO_LTHREADS, 0, 0, 0},
2927 {CMD_LINE_OPT_PARSE_PTYPE, 0, 0, 0},
2933 while ((opt = getopt_long(argc, argvopt, "p:P",
2934 lgopts, &option_index)) != EOF) {
2939 enabled_port_mask = parse_portmask(optarg);
2940 if (enabled_port_mask == 0) {
2941 printf("invalid portmask\n");
2942 print_usage(prgname);
2947 printf("Promiscuous mode selected\n");
2953 if (!strncmp(lgopts[option_index].name, CMD_LINE_OPT_RX_CONFIG,
2954 sizeof(CMD_LINE_OPT_RX_CONFIG))) {
2955 ret = parse_rx_config(optarg);
2957 printf("invalid rx-config\n");
2958 print_usage(prgname);
2963 if (!strncmp(lgopts[option_index].name, CMD_LINE_OPT_TX_CONFIG,
2964 sizeof(CMD_LINE_OPT_TX_CONFIG))) {
2965 ret = parse_tx_config(optarg);
2967 printf("invalid tx-config\n");
2968 print_usage(prgname);
2973 #if (APP_CPU_LOAD > 0)
2974 if (!strncmp(lgopts[option_index].name, CMD_LINE_OPT_STAT_LCORE,
2975 sizeof(CMD_LINE_OPT_STAT_LCORE))) {
2976 cpu_load_lcore_id = parse_stat_lcore(optarg);
2980 if (!strncmp(lgopts[option_index].name, CMD_LINE_OPT_ETH_DEST,
2981 sizeof(CMD_LINE_OPT_ETH_DEST)))
2982 parse_eth_dest(optarg);
2984 if (!strncmp(lgopts[option_index].name, CMD_LINE_OPT_NO_NUMA,
2985 sizeof(CMD_LINE_OPT_NO_NUMA))) {
2986 printf("numa is disabled\n");
2990 #if (APP_LOOKUP_METHOD == APP_LOOKUP_EXACT_MATCH)
2991 if (!strncmp(lgopts[option_index].name, CMD_LINE_OPT_IPV6,
2992 sizeof(CMD_LINE_OPT_IPV6))) {
2993 printf("ipv6 is specified\n");
2998 if (!strncmp(lgopts[option_index].name, CMD_LINE_OPT_NO_LTHREADS,
2999 sizeof(CMD_LINE_OPT_NO_LTHREADS))) {
3000 printf("l-threads model is disabled\n");
3004 if (!strncmp(lgopts[option_index].name, CMD_LINE_OPT_PARSE_PTYPE,
3005 sizeof(CMD_LINE_OPT_PARSE_PTYPE))) {
3006 printf("software packet type parsing enabled\n");
3010 if (!strncmp(lgopts[option_index].name, CMD_LINE_OPT_ENABLE_JUMBO,
3011 sizeof(CMD_LINE_OPT_ENABLE_JUMBO))) {
3012 struct option lenopts = {"max-pkt-len", required_argument, 0,
3015 printf("jumbo frame is enabled - disabling simple TX path\n");
3016 port_conf.rxmode.jumbo_frame = 1;
3018 /* if no max-pkt-len set, use the default value ETHER_MAX_LEN */
3019 if (0 == getopt_long(argc, argvopt, "", &lenopts,
3022 ret = parse_max_pkt_len(optarg);
3023 if ((ret < 64) || (ret > MAX_JUMBO_PKT_LEN)) {
3024 printf("invalid packet length\n");
3025 print_usage(prgname);
3028 port_conf.rxmode.max_rx_pkt_len = ret;
3030 printf("set jumbo frame max packet length to %u\n",
3031 (unsigned int)port_conf.rxmode.max_rx_pkt_len);
3033 #if (APP_LOOKUP_METHOD == APP_LOOKUP_EXACT_MATCH)
3034 if (!strncmp(lgopts[option_index].name, CMD_LINE_OPT_HASH_ENTRY_NUM,
3035 sizeof(CMD_LINE_OPT_HASH_ENTRY_NUM))) {
3036 ret = parse_hash_entry_number(optarg);
3037 if ((ret > 0) && (ret <= L3FWD_HASH_ENTRIES)) {
3038 hash_entry_number = ret;
3040 printf("invalid hash entry number\n");
3041 print_usage(prgname);
3049 print_usage(prgname);
3055 argv[optind-1] = prgname;
3058 optind = 1; /* reset getopt lib */
3063 print_ethaddr(const char *name, const struct ether_addr *eth_addr)
3065 char buf[ETHER_ADDR_FMT_SIZE];
3067 ether_format_addr(buf, ETHER_ADDR_FMT_SIZE, eth_addr);
3068 printf("%s%s", name, buf);
3071 #if (APP_LOOKUP_METHOD == APP_LOOKUP_EXACT_MATCH)
3073 static void convert_ipv4_5tuple(struct ipv4_5tuple *key1,
3074 union ipv4_5tuple_host *key2)
3076 key2->ip_dst = rte_cpu_to_be_32(key1->ip_dst);
3077 key2->ip_src = rte_cpu_to_be_32(key1->ip_src);
3078 key2->port_dst = rte_cpu_to_be_16(key1->port_dst);
3079 key2->port_src = rte_cpu_to_be_16(key1->port_src);
3080 key2->proto = key1->proto;
3085 static void convert_ipv6_5tuple(struct ipv6_5tuple *key1,
3086 union ipv6_5tuple_host *key2)
3090 for (i = 0; i < 16; i++) {
3091 key2->ip_dst[i] = key1->ip_dst[i];
3092 key2->ip_src[i] = key1->ip_src[i];
3094 key2->port_dst = rte_cpu_to_be_16(key1->port_dst);
3095 key2->port_src = rte_cpu_to_be_16(key1->port_src);
3096 key2->proto = key1->proto;
3102 #define BYTE_VALUE_MAX 256
3103 #define ALL_32_BITS 0xffffffff
3104 #define BIT_8_TO_15 0x0000ff00
3106 populate_ipv4_few_flow_into_table(const struct rte_hash *h)
3110 uint32_t array_len = RTE_DIM(ipv4_l3fwd_route_array);
3112 mask0 = _mm_set_epi32(ALL_32_BITS, ALL_32_BITS, ALL_32_BITS, BIT_8_TO_15);
3113 for (i = 0; i < array_len; i++) {
3114 struct ipv4_l3fwd_route entry;
3115 union ipv4_5tuple_host newkey;
3117 entry = ipv4_l3fwd_route_array[i];
3118 convert_ipv4_5tuple(&entry.key, &newkey);
3119 ret = rte_hash_add_key(h, (void *)&newkey);
3121 rte_exit(EXIT_FAILURE, "Unable to add entry %" PRIu32
3122 " to the l3fwd hash.\n", i);
3124 ipv4_l3fwd_out_if[ret] = entry.if_out;
3126 printf("Hash: Adding 0x%" PRIx32 " keys\n", array_len);
3129 #define BIT_16_TO_23 0x00ff0000
3131 populate_ipv6_few_flow_into_table(const struct rte_hash *h)
3135 uint32_t array_len = RTE_DIM(ipv6_l3fwd_route_array);
3137 mask1 = _mm_set_epi32(ALL_32_BITS, ALL_32_BITS, ALL_32_BITS, BIT_16_TO_23);
3138 mask2 = _mm_set_epi32(0, 0, ALL_32_BITS, ALL_32_BITS);
3139 for (i = 0; i < array_len; i++) {
3140 struct ipv6_l3fwd_route entry;
3141 union ipv6_5tuple_host newkey;
3143 entry = ipv6_l3fwd_route_array[i];
3144 convert_ipv6_5tuple(&entry.key, &newkey);
3145 ret = rte_hash_add_key(h, (void *)&newkey);
3147 rte_exit(EXIT_FAILURE, "Unable to add entry %" PRIu32
3148 " to the l3fwd hash.\n", i);
3150 ipv6_l3fwd_out_if[ret] = entry.if_out;
3152 printf("Hash: Adding 0x%" PRIx32 "keys\n", array_len);
3155 #define NUMBER_PORT_USED 4
3157 populate_ipv4_many_flow_into_table(const struct rte_hash *h,
3158 unsigned int nr_flow)
3162 mask0 = _mm_set_epi32(ALL_32_BITS, ALL_32_BITS, ALL_32_BITS, BIT_8_TO_15);
3164 for (i = 0; i < nr_flow; i++) {
3165 struct ipv4_l3fwd_route entry;
3166 union ipv4_5tuple_host newkey;
3167 uint8_t a = (uint8_t)((i / NUMBER_PORT_USED) % BYTE_VALUE_MAX);
3168 uint8_t b = (uint8_t)(((i / NUMBER_PORT_USED) / BYTE_VALUE_MAX) %
3170 uint8_t c = (uint8_t)((i / NUMBER_PORT_USED) / (BYTE_VALUE_MAX *
3172 /* Create the ipv4 exact match flow */
3173 memset(&entry, 0, sizeof(entry));
3174 switch (i & (NUMBER_PORT_USED - 1)) {
3176 entry = ipv4_l3fwd_route_array[0];
3177 entry.key.ip_dst = IPv4(101, c, b, a);
3180 entry = ipv4_l3fwd_route_array[1];
3181 entry.key.ip_dst = IPv4(201, c, b, a);
3184 entry = ipv4_l3fwd_route_array[2];
3185 entry.key.ip_dst = IPv4(111, c, b, a);
3188 entry = ipv4_l3fwd_route_array[3];
3189 entry.key.ip_dst = IPv4(211, c, b, a);
3192 convert_ipv4_5tuple(&entry.key, &newkey);
3193 int32_t ret = rte_hash_add_key(h, (void *)&newkey);
3196 rte_exit(EXIT_FAILURE, "Unable to add entry %u\n", i);
3198 ipv4_l3fwd_out_if[ret] = (uint8_t)entry.if_out;
3201 printf("Hash: Adding 0x%x keys\n", nr_flow);
3205 populate_ipv6_many_flow_into_table(const struct rte_hash *h,
3206 unsigned int nr_flow)
3210 mask1 = _mm_set_epi32(ALL_32_BITS, ALL_32_BITS, ALL_32_BITS, BIT_16_TO_23);
3211 mask2 = _mm_set_epi32(0, 0, ALL_32_BITS, ALL_32_BITS);
3212 for (i = 0; i < nr_flow; i++) {
3213 struct ipv6_l3fwd_route entry;
3214 union ipv6_5tuple_host newkey;
3216 uint8_t a = (uint8_t) ((i / NUMBER_PORT_USED) % BYTE_VALUE_MAX);
3217 uint8_t b = (uint8_t) (((i / NUMBER_PORT_USED) / BYTE_VALUE_MAX) %
3219 uint8_t c = (uint8_t) ((i / NUMBER_PORT_USED) / (BYTE_VALUE_MAX *
3222 /* Create the ipv6 exact match flow */
3223 memset(&entry, 0, sizeof(entry));
3224 switch (i & (NUMBER_PORT_USED - 1)) {
3226 entry = ipv6_l3fwd_route_array[0];
3229 entry = ipv6_l3fwd_route_array[1];
3232 entry = ipv6_l3fwd_route_array[2];
3235 entry = ipv6_l3fwd_route_array[3];
3238 entry.key.ip_dst[13] = c;
3239 entry.key.ip_dst[14] = b;
3240 entry.key.ip_dst[15] = a;
3241 convert_ipv6_5tuple(&entry.key, &newkey);
3242 int32_t ret = rte_hash_add_key(h, (void *)&newkey);
3245 rte_exit(EXIT_FAILURE, "Unable to add entry %u\n", i);
3247 ipv6_l3fwd_out_if[ret] = (uint8_t) entry.if_out;
3250 printf("Hash: Adding 0x%x keys\n", nr_flow);
3254 setup_hash(int socketid)
3256 struct rte_hash_parameters ipv4_l3fwd_hash_params = {
3258 .entries = L3FWD_HASH_ENTRIES,
3259 .key_len = sizeof(union ipv4_5tuple_host),
3260 .hash_func = ipv4_hash_crc,
3261 .hash_func_init_val = 0,
3264 struct rte_hash_parameters ipv6_l3fwd_hash_params = {
3266 .entries = L3FWD_HASH_ENTRIES,
3267 .key_len = sizeof(union ipv6_5tuple_host),
3268 .hash_func = ipv6_hash_crc,
3269 .hash_func_init_val = 0,
3274 /* create ipv4 hash */
3275 snprintf(s, sizeof(s), "ipv4_l3fwd_hash_%d", socketid);
3276 ipv4_l3fwd_hash_params.name = s;
3277 ipv4_l3fwd_hash_params.socket_id = socketid;
3278 ipv4_l3fwd_lookup_struct[socketid] =
3279 rte_hash_create(&ipv4_l3fwd_hash_params);
3280 if (ipv4_l3fwd_lookup_struct[socketid] == NULL)
3281 rte_exit(EXIT_FAILURE, "Unable to create the l3fwd hash on "
3282 "socket %d\n", socketid);
3284 /* create ipv6 hash */
3285 snprintf(s, sizeof(s), "ipv6_l3fwd_hash_%d", socketid);
3286 ipv6_l3fwd_hash_params.name = s;
3287 ipv6_l3fwd_hash_params.socket_id = socketid;
3288 ipv6_l3fwd_lookup_struct[socketid] =
3289 rte_hash_create(&ipv6_l3fwd_hash_params);
3290 if (ipv6_l3fwd_lookup_struct[socketid] == NULL)
3291 rte_exit(EXIT_FAILURE, "Unable to create the l3fwd hash on "
3292 "socket %d\n", socketid);
3294 if (hash_entry_number != HASH_ENTRY_NUMBER_DEFAULT) {
3295 /* For testing hash matching with a large number of flows we
3296 * generate millions of IP 5-tuples with an incremented dst
3297 * address to initialize the hash table. */
3299 /* populate the ipv4 hash */
3300 populate_ipv4_many_flow_into_table(
3301 ipv4_l3fwd_lookup_struct[socketid], hash_entry_number);
3303 /* populate the ipv6 hash */
3304 populate_ipv6_many_flow_into_table(
3305 ipv6_l3fwd_lookup_struct[socketid], hash_entry_number);
3308 /* Use data in ipv4/ipv6 l3fwd lookup table directly to initialize
3311 /* populate the ipv4 hash */
3312 populate_ipv4_few_flow_into_table(
3313 ipv4_l3fwd_lookup_struct[socketid]);
3315 /* populate the ipv6 hash */
3316 populate_ipv6_few_flow_into_table(
3317 ipv6_l3fwd_lookup_struct[socketid]);
3323 #if (APP_LOOKUP_METHOD == APP_LOOKUP_LPM)
3325 setup_lpm(int socketid)
3327 struct rte_lpm6_config config;
3328 struct rte_lpm_config lpm_ipv4_config;
3333 /* create the LPM table */
3334 snprintf(s, sizeof(s), "IPV4_L3FWD_LPM_%d", socketid);
3335 lpm_ipv4_config.max_rules = IPV4_L3FWD_LPM_MAX_RULES;
3336 lpm_ipv4_config.number_tbl8s = 256;
3337 lpm_ipv4_config.flags = 0;
3338 ipv4_l3fwd_lookup_struct[socketid] =
3339 rte_lpm_create(s, socketid, &lpm_ipv4_config);
3340 if (ipv4_l3fwd_lookup_struct[socketid] == NULL)
3341 rte_exit(EXIT_FAILURE, "Unable to create the l3fwd LPM table"
3342 " on socket %d\n", socketid);
3344 /* populate the LPM table */
3345 for (i = 0; i < IPV4_L3FWD_NUM_ROUTES; i++) {
3347 /* skip unused ports */
3348 if ((1 << ipv4_l3fwd_route_array[i].if_out &
3349 enabled_port_mask) == 0)
3352 ret = rte_lpm_add(ipv4_l3fwd_lookup_struct[socketid],
3353 ipv4_l3fwd_route_array[i].ip,
3354 ipv4_l3fwd_route_array[i].depth,
3355 ipv4_l3fwd_route_array[i].if_out);
3358 rte_exit(EXIT_FAILURE, "Unable to add entry %u to the "
3359 "l3fwd LPM table on socket %d\n",
3363 printf("LPM: Adding route 0x%08x / %d (%d)\n",
3364 (unsigned)ipv4_l3fwd_route_array[i].ip,
3365 ipv4_l3fwd_route_array[i].depth,
3366 ipv4_l3fwd_route_array[i].if_out);
3369 /* create the LPM6 table */
3370 snprintf(s, sizeof(s), "IPV6_L3FWD_LPM_%d", socketid);
3372 config.max_rules = IPV6_L3FWD_LPM_MAX_RULES;
3373 config.number_tbl8s = IPV6_L3FWD_LPM_NUMBER_TBL8S;
3375 ipv6_l3fwd_lookup_struct[socketid] = rte_lpm6_create(s, socketid,
3377 if (ipv6_l3fwd_lookup_struct[socketid] == NULL)
3378 rte_exit(EXIT_FAILURE, "Unable to create the l3fwd LPM table"
3379 " on socket %d\n", socketid);
3381 /* populate the LPM table */
3382 for (i = 0; i < IPV6_L3FWD_NUM_ROUTES; i++) {
3384 /* skip unused ports */
3385 if ((1 << ipv6_l3fwd_route_array[i].if_out &
3386 enabled_port_mask) == 0)
3389 ret = rte_lpm6_add(ipv6_l3fwd_lookup_struct[socketid],
3390 ipv6_l3fwd_route_array[i].ip,
3391 ipv6_l3fwd_route_array[i].depth,
3392 ipv6_l3fwd_route_array[i].if_out);
3395 rte_exit(EXIT_FAILURE, "Unable to add entry %u to the "
3396 "l3fwd LPM table on socket %d\n",
3400 printf("LPM: Adding route %s / %d (%d)\n",
3402 ipv6_l3fwd_route_array[i].depth,
3403 ipv6_l3fwd_route_array[i].if_out);
3409 init_mem(unsigned nb_mbuf)
3411 struct lcore_conf *qconf;
3416 for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
3417 if (rte_lcore_is_enabled(lcore_id) == 0)
3421 socketid = rte_lcore_to_socket_id(lcore_id);
3425 if (socketid >= NB_SOCKETS) {
3426 rte_exit(EXIT_FAILURE, "Socket %d of lcore %u is out of range %d\n",
3427 socketid, lcore_id, NB_SOCKETS);
3429 if (pktmbuf_pool[socketid] == NULL) {
3430 snprintf(s, sizeof(s), "mbuf_pool_%d", socketid);
3431 pktmbuf_pool[socketid] =
3432 rte_pktmbuf_pool_create(s, nb_mbuf,
3433 MEMPOOL_CACHE_SIZE, 0,
3434 RTE_MBUF_DEFAULT_BUF_SIZE, socketid);
3435 if (pktmbuf_pool[socketid] == NULL)
3436 rte_exit(EXIT_FAILURE,
3437 "Cannot init mbuf pool on socket %d\n", socketid);
3439 printf("Allocated mbuf pool on socket %d\n", socketid);
3441 #if (APP_LOOKUP_METHOD == APP_LOOKUP_LPM)
3442 setup_lpm(socketid);
3444 setup_hash(socketid);
3447 qconf = &lcore_conf[lcore_id];
3448 qconf->ipv4_lookup_struct = ipv4_l3fwd_lookup_struct[socketid];
3449 qconf->ipv6_lookup_struct = ipv6_l3fwd_lookup_struct[socketid];
3454 /* Check the link status of all ports in up to 9s, and print them finally */
3456 check_all_ports_link_status(uint8_t port_num, uint32_t port_mask)
3458 #define CHECK_INTERVAL 100 /* 100ms */
3459 #define MAX_CHECK_TIME 90 /* 9s (90 * 100ms) in total */
3460 uint8_t portid, count, all_ports_up, print_flag = 0;
3461 struct rte_eth_link link;
3463 printf("\nChecking link status");
3465 for (count = 0; count <= MAX_CHECK_TIME; count++) {
3467 for (portid = 0; portid < port_num; portid++) {
3468 if ((port_mask & (1 << portid)) == 0)
3470 memset(&link, 0, sizeof(link));
3471 rte_eth_link_get_nowait(portid, &link);
3472 /* print link status if flag set */
3473 if (print_flag == 1) {
3474 if (link.link_status)
3475 printf("Port %d Link Up - speed %u "
3476 "Mbps - %s\n", (uint8_t)portid,
3477 (unsigned)link.link_speed,
3478 (link.link_duplex == ETH_LINK_FULL_DUPLEX) ?
3479 ("full-duplex") : ("half-duplex\n"));
3481 printf("Port %d Link Down\n",
3485 /* clear all_ports_up flag if any link down */
3486 if (link.link_status == ETH_LINK_DOWN) {
3491 /* after finally printing all link status, get out */
3492 if (print_flag == 1)
3495 if (all_ports_up == 0) {
3498 rte_delay_ms(CHECK_INTERVAL);
3501 /* set the print_flag if all ports up or timeout */
3502 if (all_ports_up == 1 || count == (MAX_CHECK_TIME - 1)) {
3510 main(int argc, char **argv)
3512 struct rte_eth_dev_info dev_info;
3513 struct rte_eth_txconf *txconf;
3519 uint32_t n_tx_queue, nb_lcores;
3520 uint8_t portid, nb_rx_queue, queue, socketid;
3523 ret = rte_eal_init(argc, argv);
3525 rte_exit(EXIT_FAILURE, "Invalid EAL parameters\n");
3529 /* pre-init dst MACs for all ports to 02:00:00:00:00:xx */
3530 for (portid = 0; portid < RTE_MAX_ETHPORTS; portid++) {
3531 dest_eth_addr[portid] = ETHER_LOCAL_ADMIN_ADDR +
3532 ((uint64_t)portid << 40);
3533 *(uint64_t *)(val_eth + portid) = dest_eth_addr[portid];
3536 /* parse application arguments (after the EAL ones) */
3537 ret = parse_args(argc, argv);
3539 rte_exit(EXIT_FAILURE, "Invalid L3FWD parameters\n");
3541 if (check_lcore_params() < 0)
3542 rte_exit(EXIT_FAILURE, "check_lcore_params failed\n");
3544 printf("Initializing rx-queues...\n");
3545 ret = init_rx_queues();
3547 rte_exit(EXIT_FAILURE, "init_rx_queues failed\n");
3549 printf("Initializing tx-threads...\n");
3550 ret = init_tx_threads();
3552 rte_exit(EXIT_FAILURE, "init_tx_threads failed\n");
3554 printf("Initializing rings...\n");
3555 ret = init_rx_rings();
3557 rte_exit(EXIT_FAILURE, "init_rx_rings failed\n");
3559 nb_ports = rte_eth_dev_count();
3561 if (check_port_config(nb_ports) < 0)
3562 rte_exit(EXIT_FAILURE, "check_port_config failed\n");
3564 nb_lcores = rte_lcore_count();
3566 /* initialize all ports */
3567 for (portid = 0; portid < nb_ports; portid++) {
3568 /* skip ports that are not enabled */
3569 if ((enabled_port_mask & (1 << portid)) == 0) {
3570 printf("\nSkipping disabled port %d\n", portid);
3575 printf("Initializing port %d ... ", portid);
3578 nb_rx_queue = get_port_n_rx_queues(portid);
3579 n_tx_queue = nb_lcores;
3580 if (n_tx_queue > MAX_TX_QUEUE_PER_PORT)
3581 n_tx_queue = MAX_TX_QUEUE_PER_PORT;
3582 printf("Creating queues: nb_rxq=%d nb_txq=%u... ",
3583 nb_rx_queue, (unsigned)n_tx_queue);
3584 ret = rte_eth_dev_configure(portid, nb_rx_queue,
3585 (uint16_t)n_tx_queue, &port_conf);
3587 rte_exit(EXIT_FAILURE, "Cannot configure device: err=%d, port=%d\n",
3590 rte_eth_macaddr_get(portid, &ports_eth_addr[portid]);
3591 print_ethaddr(" Address:", &ports_eth_addr[portid]);
3593 print_ethaddr("Destination:",
3594 (const struct ether_addr *)&dest_eth_addr[portid]);
3598 * prepare src MACs for each port.
3600 ether_addr_copy(&ports_eth_addr[portid],
3601 (struct ether_addr *)(val_eth + portid) + 1);
3604 ret = init_mem(NB_MBUF);
3606 rte_exit(EXIT_FAILURE, "init_mem failed\n");
3608 /* init one TX queue per couple (lcore,port) */
3610 for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
3611 if (rte_lcore_is_enabled(lcore_id) == 0)
3615 socketid = (uint8_t)rte_lcore_to_socket_id(lcore_id);
3619 printf("txq=%u,%d,%d ", lcore_id, queueid, socketid);
3622 rte_eth_dev_info_get(portid, &dev_info);
3623 txconf = &dev_info.default_txconf;
3624 if (port_conf.rxmode.jumbo_frame)
3625 txconf->txq_flags = 0;
3626 ret = rte_eth_tx_queue_setup(portid, queueid, nb_txd,
3629 rte_exit(EXIT_FAILURE, "rte_eth_tx_queue_setup: err=%d, "
3630 "port=%d\n", ret, portid);
3632 tx_thread[lcore_id].tx_queue_id[portid] = queueid;
3638 for (i = 0; i < n_rx_thread; i++) {
3639 lcore_id = rx_thread[i].conf.lcore_id;
3641 if (rte_lcore_is_enabled(lcore_id) == 0) {
3642 rte_exit(EXIT_FAILURE,
3643 "Cannot start Rx thread on lcore %u: lcore disabled\n",
3648 printf("\nInitializing rx queues for Rx thread %d on lcore %u ... ",
3652 /* init RX queues */
3653 for (queue = 0; queue < rx_thread[i].n_rx_queue; ++queue) {
3654 portid = rx_thread[i].rx_queue_list[queue].port_id;
3655 queueid = rx_thread[i].rx_queue_list[queue].queue_id;
3658 socketid = (uint8_t)rte_lcore_to_socket_id(lcore_id);
3662 printf("rxq=%d,%d,%d ", portid, queueid, socketid);
3665 ret = rte_eth_rx_queue_setup(portid, queueid, nb_rxd,
3668 pktmbuf_pool[socketid]);
3670 rte_exit(EXIT_FAILURE, "rte_eth_rx_queue_setup: err=%d, "
3671 "port=%d\n", ret, portid);
3678 for (portid = 0; portid < nb_ports; portid++) {
3679 if ((enabled_port_mask & (1 << portid)) == 0)
3683 ret = rte_eth_dev_start(portid);
3685 rte_exit(EXIT_FAILURE, "rte_eth_dev_start: err=%d, port=%d\n",
3689 * If enabled, put device in promiscuous mode.
3690 * This allows IO forwarding mode to forward packets
3691 * to itself through 2 cross-connected ports of the
3695 rte_eth_promiscuous_enable(portid);
3698 for (i = 0; i < n_rx_thread; i++) {
3699 lcore_id = rx_thread[i].conf.lcore_id;
3700 if (rte_lcore_is_enabled(lcore_id) == 0)
3703 /* check if hw packet type is supported */
3704 for (queue = 0; queue < rx_thread[i].n_rx_queue; ++queue) {
3705 portid = rx_thread[i].rx_queue_list[queue].port_id;
3706 queueid = rx_thread[i].rx_queue_list[queue].queue_id;
3708 if (parse_ptype_on) {
3709 if (!rte_eth_add_rx_callback(portid, queueid,
3710 cb_parse_ptype, NULL))
3711 rte_exit(EXIT_FAILURE,
3712 "Failed to add rx callback: "
3713 "port=%d\n", portid);
3714 } else if (!check_ptype(portid))
3715 rte_exit(EXIT_FAILURE,
3716 "Port %d cannot parse packet type.\n\n"
3717 "Please add --parse-ptype to use sw "
3718 "packet type analyzer.\n\n",
3723 check_all_ports_link_status((uint8_t)nb_ports, enabled_port_mask);
3726 printf("Starting L-Threading Model\n");
3728 #if (APP_CPU_LOAD > 0)
3729 if (cpu_load_lcore_id > 0)
3730 /* Use one lcore for cpu load collector */
3734 lthread_num_schedulers_set(nb_lcores);
3735 rte_eal_mp_remote_launch(sched_spawner, NULL, SKIP_MASTER);
3736 lthread_master_spawner(NULL);
3739 printf("Starting P-Threading Model\n");
3740 /* launch per-lcore init on every lcore */
3741 rte_eal_mp_remote_launch(pthread_run, NULL, CALL_MASTER);
3742 RTE_LCORE_FOREACH_SLAVE(lcore_id) {
3743 if (rte_eal_wait_lcore(lcore_id) < 0)