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 __rte_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];
766 qconf->tx_mbufs[port].m_table[len + j] = m[j];
770 qconf->tx_mbufs[port].m_table[len + j] = m[j];
774 qconf->tx_mbufs[port].m_table[len + j] = m[j];
781 /* enough pkts to be sent */
782 if (unlikely(len == MAX_PKT_BURST)) {
784 send_burst(qconf, MAX_PKT_BURST, port);
786 /* copy rest of the packets into the TX buffer. */
789 switch (len % FWDSTEP) {
792 qconf->tx_mbufs[port].m_table[j] = m[n + j];
796 qconf->tx_mbufs[port].m_table[j] = m[n + j];
800 qconf->tx_mbufs[port].m_table[j] = m[n + j];
804 qconf->tx_mbufs[port].m_table[j] = m[n + j];
810 qconf->tx_mbufs[port].len = len;
812 #endif /* APP_LOOKUP_LPM */
814 #ifdef DO_RFC_1812_CHECKS
816 is_valid_ipv4_pkt(struct ipv4_hdr *pkt, uint32_t link_len)
818 /* From http://www.rfc-editor.org/rfc/rfc1812.txt section 5.2.2 */
820 * 1. The packet length reported by the Link Layer must be large
821 * enough to hold the minimum length legal IP datagram (20 bytes).
823 if (link_len < sizeof(struct ipv4_hdr))
826 /* 2. The IP checksum must be correct. */
827 /* this is checked in H/W */
830 * 3. The IP version number must be 4. If the version number is not 4
831 * then the packet may be another version of IP, such as IPng or
834 if (((pkt->version_ihl) >> 4) != 4)
837 * 4. The IP header length field must be large enough to hold the
838 * minimum length legal IP datagram (20 bytes = 5 words).
840 if ((pkt->version_ihl & 0xf) < 5)
844 * 5. The IP total length field must be large enough to hold the IP
845 * datagram header, whose length is specified in the IP header length
848 if (rte_cpu_to_be_16(pkt->total_length) < sizeof(struct ipv4_hdr))
855 #if (APP_LOOKUP_METHOD == APP_LOOKUP_EXACT_MATCH)
857 static __m128i mask0;
858 static __m128i mask1;
859 static __m128i mask2;
860 static inline uint8_t
861 get_ipv4_dst_port(void *ipv4_hdr, uint8_t portid,
862 lookup_struct_t *ipv4_l3fwd_lookup_struct)
865 union ipv4_5tuple_host key;
867 ipv4_hdr = (uint8_t *)ipv4_hdr + offsetof(struct ipv4_hdr, time_to_live);
868 __m128i data = _mm_loadu_si128((__m128i *)(ipv4_hdr));
869 /* Get 5 tuple: dst port, src port, dst IP address, src IP address and
871 key.xmm = _mm_and_si128(data, mask0);
872 /* Find destination port */
873 ret = rte_hash_lookup(ipv4_l3fwd_lookup_struct, (const void *)&key);
874 return (uint8_t)((ret < 0) ? portid : ipv4_l3fwd_out_if[ret]);
877 static inline uint8_t
878 get_ipv6_dst_port(void *ipv6_hdr, uint8_t portid,
879 lookup_struct_t *ipv6_l3fwd_lookup_struct)
882 union ipv6_5tuple_host key;
884 ipv6_hdr = (uint8_t *)ipv6_hdr + offsetof(struct ipv6_hdr, payload_len);
885 __m128i data0 = _mm_loadu_si128((__m128i *)(ipv6_hdr));
886 __m128i data1 = _mm_loadu_si128((__m128i *)(((uint8_t *)ipv6_hdr) +
888 __m128i data2 = _mm_loadu_si128((__m128i *)(((uint8_t *)ipv6_hdr) +
889 sizeof(__m128i) + sizeof(__m128i)));
890 /* Get part of 5 tuple: src IP address lower 96 bits and protocol */
891 key.xmm[0] = _mm_and_si128(data0, mask1);
892 /* Get part of 5 tuple: dst IP address lower 96 bits and src IP address
895 /* Get part of 5 tuple: dst port and src port and dst IP address higher
897 key.xmm[2] = _mm_and_si128(data2, mask2);
899 /* Find destination port */
900 ret = rte_hash_lookup(ipv6_l3fwd_lookup_struct, (const void *)&key);
901 return (uint8_t)((ret < 0) ? portid : ipv6_l3fwd_out_if[ret]);
905 #if (APP_LOOKUP_METHOD == APP_LOOKUP_LPM)
907 static inline uint8_t
908 get_ipv4_dst_port(void *ipv4_hdr, uint8_t portid,
909 lookup_struct_t *ipv4_l3fwd_lookup_struct)
913 return (uint8_t)((rte_lpm_lookup(ipv4_l3fwd_lookup_struct,
914 rte_be_to_cpu_32(((struct ipv4_hdr *)ipv4_hdr)->dst_addr),
915 &next_hop) == 0) ? next_hop : portid);
918 static inline uint8_t
919 get_ipv6_dst_port(void *ipv6_hdr, uint8_t portid,
920 lookup6_struct_t *ipv6_l3fwd_lookup_struct)
924 return (uint8_t) ((rte_lpm6_lookup(ipv6_l3fwd_lookup_struct,
925 ((struct ipv6_hdr *)ipv6_hdr)->dst_addr, &next_hop) == 0) ?
930 static inline void l3fwd_simple_forward(struct rte_mbuf *m, uint8_t portid)
931 __attribute__((unused));
933 #if ((APP_LOOKUP_METHOD == APP_LOOKUP_EXACT_MATCH) && \
934 (ENABLE_MULTI_BUFFER_OPTIMIZE == 1))
936 #define MASK_ALL_PKTS 0xff
937 #define EXCLUDE_1ST_PKT 0xfe
938 #define EXCLUDE_2ND_PKT 0xfd
939 #define EXCLUDE_3RD_PKT 0xfb
940 #define EXCLUDE_4TH_PKT 0xf7
941 #define EXCLUDE_5TH_PKT 0xef
942 #define EXCLUDE_6TH_PKT 0xdf
943 #define EXCLUDE_7TH_PKT 0xbf
944 #define EXCLUDE_8TH_PKT 0x7f
947 simple_ipv4_fwd_8pkts(struct rte_mbuf *m[8], uint8_t portid)
949 struct ether_hdr *eth_hdr[8];
950 struct ipv4_hdr *ipv4_hdr[8];
953 union ipv4_5tuple_host key[8];
956 eth_hdr[0] = rte_pktmbuf_mtod(m[0], struct ether_hdr *);
957 eth_hdr[1] = rte_pktmbuf_mtod(m[1], struct ether_hdr *);
958 eth_hdr[2] = rte_pktmbuf_mtod(m[2], struct ether_hdr *);
959 eth_hdr[3] = rte_pktmbuf_mtod(m[3], struct ether_hdr *);
960 eth_hdr[4] = rte_pktmbuf_mtod(m[4], struct ether_hdr *);
961 eth_hdr[5] = rte_pktmbuf_mtod(m[5], struct ether_hdr *);
962 eth_hdr[6] = rte_pktmbuf_mtod(m[6], struct ether_hdr *);
963 eth_hdr[7] = rte_pktmbuf_mtod(m[7], struct ether_hdr *);
965 /* Handle IPv4 headers.*/
966 ipv4_hdr[0] = rte_pktmbuf_mtod_offset(m[0], struct ipv4_hdr *,
967 sizeof(struct ether_hdr));
968 ipv4_hdr[1] = rte_pktmbuf_mtod_offset(m[1], struct ipv4_hdr *,
969 sizeof(struct ether_hdr));
970 ipv4_hdr[2] = rte_pktmbuf_mtod_offset(m[2], struct ipv4_hdr *,
971 sizeof(struct ether_hdr));
972 ipv4_hdr[3] = rte_pktmbuf_mtod_offset(m[3], struct ipv4_hdr *,
973 sizeof(struct ether_hdr));
974 ipv4_hdr[4] = rte_pktmbuf_mtod_offset(m[4], struct ipv4_hdr *,
975 sizeof(struct ether_hdr));
976 ipv4_hdr[5] = rte_pktmbuf_mtod_offset(m[5], struct ipv4_hdr *,
977 sizeof(struct ether_hdr));
978 ipv4_hdr[6] = rte_pktmbuf_mtod_offset(m[6], struct ipv4_hdr *,
979 sizeof(struct ether_hdr));
980 ipv4_hdr[7] = rte_pktmbuf_mtod_offset(m[7], struct ipv4_hdr *,
981 sizeof(struct ether_hdr));
983 #ifdef DO_RFC_1812_CHECKS
984 /* Check to make sure the packet is valid (RFC1812) */
985 uint8_t valid_mask = MASK_ALL_PKTS;
987 if (is_valid_ipv4_pkt(ipv4_hdr[0], m[0]->pkt_len) < 0) {
988 rte_pktmbuf_free(m[0]);
989 valid_mask &= EXCLUDE_1ST_PKT;
991 if (is_valid_ipv4_pkt(ipv4_hdr[1], m[1]->pkt_len) < 0) {
992 rte_pktmbuf_free(m[1]);
993 valid_mask &= EXCLUDE_2ND_PKT;
995 if (is_valid_ipv4_pkt(ipv4_hdr[2], m[2]->pkt_len) < 0) {
996 rte_pktmbuf_free(m[2]);
997 valid_mask &= EXCLUDE_3RD_PKT;
999 if (is_valid_ipv4_pkt(ipv4_hdr[3], m[3]->pkt_len) < 0) {
1000 rte_pktmbuf_free(m[3]);
1001 valid_mask &= EXCLUDE_4TH_PKT;
1003 if (is_valid_ipv4_pkt(ipv4_hdr[4], m[4]->pkt_len) < 0) {
1004 rte_pktmbuf_free(m[4]);
1005 valid_mask &= EXCLUDE_5TH_PKT;
1007 if (is_valid_ipv4_pkt(ipv4_hdr[5], m[5]->pkt_len) < 0) {
1008 rte_pktmbuf_free(m[5]);
1009 valid_mask &= EXCLUDE_6TH_PKT;
1011 if (is_valid_ipv4_pkt(ipv4_hdr[6], m[6]->pkt_len) < 0) {
1012 rte_pktmbuf_free(m[6]);
1013 valid_mask &= EXCLUDE_7TH_PKT;
1015 if (is_valid_ipv4_pkt(ipv4_hdr[7], m[7]->pkt_len) < 0) {
1016 rte_pktmbuf_free(m[7]);
1017 valid_mask &= EXCLUDE_8TH_PKT;
1019 if (unlikely(valid_mask != MASK_ALL_PKTS)) {
1020 if (valid_mask == 0)
1025 for (i = 0; i < 8; i++)
1026 if ((0x1 << i) & valid_mask)
1027 l3fwd_simple_forward(m[i], portid);
1029 #endif /* End of #ifdef DO_RFC_1812_CHECKS */
1031 data[0] = _mm_loadu_si128(rte_pktmbuf_mtod_offset(m[0], __m128i *,
1032 sizeof(struct ether_hdr) +
1033 offsetof(struct ipv4_hdr, time_to_live)));
1034 data[1] = _mm_loadu_si128(rte_pktmbuf_mtod_offset(m[1], __m128i *,
1035 sizeof(struct ether_hdr) +
1036 offsetof(struct ipv4_hdr, time_to_live)));
1037 data[2] = _mm_loadu_si128(rte_pktmbuf_mtod_offset(m[2], __m128i *,
1038 sizeof(struct ether_hdr) +
1039 offsetof(struct ipv4_hdr, time_to_live)));
1040 data[3] = _mm_loadu_si128(rte_pktmbuf_mtod_offset(m[3], __m128i *,
1041 sizeof(struct ether_hdr) +
1042 offsetof(struct ipv4_hdr, time_to_live)));
1043 data[4] = _mm_loadu_si128(rte_pktmbuf_mtod_offset(m[4], __m128i *,
1044 sizeof(struct ether_hdr) +
1045 offsetof(struct ipv4_hdr, time_to_live)));
1046 data[5] = _mm_loadu_si128(rte_pktmbuf_mtod_offset(m[5], __m128i *,
1047 sizeof(struct ether_hdr) +
1048 offsetof(struct ipv4_hdr, time_to_live)));
1049 data[6] = _mm_loadu_si128(rte_pktmbuf_mtod_offset(m[6], __m128i *,
1050 sizeof(struct ether_hdr) +
1051 offsetof(struct ipv4_hdr, time_to_live)));
1052 data[7] = _mm_loadu_si128(rte_pktmbuf_mtod_offset(m[7], __m128i *,
1053 sizeof(struct ether_hdr) +
1054 offsetof(struct ipv4_hdr, time_to_live)));
1056 key[0].xmm = _mm_and_si128(data[0], mask0);
1057 key[1].xmm = _mm_and_si128(data[1], mask0);
1058 key[2].xmm = _mm_and_si128(data[2], mask0);
1059 key[3].xmm = _mm_and_si128(data[3], mask0);
1060 key[4].xmm = _mm_and_si128(data[4], mask0);
1061 key[5].xmm = _mm_and_si128(data[5], mask0);
1062 key[6].xmm = _mm_and_si128(data[6], mask0);
1063 key[7].xmm = _mm_and_si128(data[7], mask0);
1065 const void *key_array[8] = {&key[0], &key[1], &key[2], &key[3],
1066 &key[4], &key[5], &key[6], &key[7]};
1068 rte_hash_lookup_bulk(RTE_PER_LCORE(lcore_conf)->ipv4_lookup_struct,
1069 &key_array[0], 8, ret);
1070 dst_port[0] = (uint8_t) ((ret[0] < 0) ? portid : ipv4_l3fwd_out_if[ret[0]]);
1071 dst_port[1] = (uint8_t) ((ret[1] < 0) ? portid : ipv4_l3fwd_out_if[ret[1]]);
1072 dst_port[2] = (uint8_t) ((ret[2] < 0) ? portid : ipv4_l3fwd_out_if[ret[2]]);
1073 dst_port[3] = (uint8_t) ((ret[3] < 0) ? portid : ipv4_l3fwd_out_if[ret[3]]);
1074 dst_port[4] = (uint8_t) ((ret[4] < 0) ? portid : ipv4_l3fwd_out_if[ret[4]]);
1075 dst_port[5] = (uint8_t) ((ret[5] < 0) ? portid : ipv4_l3fwd_out_if[ret[5]]);
1076 dst_port[6] = (uint8_t) ((ret[6] < 0) ? portid : ipv4_l3fwd_out_if[ret[6]]);
1077 dst_port[7] = (uint8_t) ((ret[7] < 0) ? portid : ipv4_l3fwd_out_if[ret[7]]);
1079 if (dst_port[0] >= RTE_MAX_ETHPORTS ||
1080 (enabled_port_mask & 1 << dst_port[0]) == 0)
1081 dst_port[0] = portid;
1082 if (dst_port[1] >= RTE_MAX_ETHPORTS ||
1083 (enabled_port_mask & 1 << dst_port[1]) == 0)
1084 dst_port[1] = portid;
1085 if (dst_port[2] >= RTE_MAX_ETHPORTS ||
1086 (enabled_port_mask & 1 << dst_port[2]) == 0)
1087 dst_port[2] = portid;
1088 if (dst_port[3] >= RTE_MAX_ETHPORTS ||
1089 (enabled_port_mask & 1 << dst_port[3]) == 0)
1090 dst_port[3] = portid;
1091 if (dst_port[4] >= RTE_MAX_ETHPORTS ||
1092 (enabled_port_mask & 1 << dst_port[4]) == 0)
1093 dst_port[4] = portid;
1094 if (dst_port[5] >= RTE_MAX_ETHPORTS ||
1095 (enabled_port_mask & 1 << dst_port[5]) == 0)
1096 dst_port[5] = portid;
1097 if (dst_port[6] >= RTE_MAX_ETHPORTS ||
1098 (enabled_port_mask & 1 << dst_port[6]) == 0)
1099 dst_port[6] = portid;
1100 if (dst_port[7] >= RTE_MAX_ETHPORTS ||
1101 (enabled_port_mask & 1 << dst_port[7]) == 0)
1102 dst_port[7] = portid;
1104 #ifdef DO_RFC_1812_CHECKS
1105 /* Update time to live and header checksum */
1106 --(ipv4_hdr[0]->time_to_live);
1107 --(ipv4_hdr[1]->time_to_live);
1108 --(ipv4_hdr[2]->time_to_live);
1109 --(ipv4_hdr[3]->time_to_live);
1110 ++(ipv4_hdr[0]->hdr_checksum);
1111 ++(ipv4_hdr[1]->hdr_checksum);
1112 ++(ipv4_hdr[2]->hdr_checksum);
1113 ++(ipv4_hdr[3]->hdr_checksum);
1114 --(ipv4_hdr[4]->time_to_live);
1115 --(ipv4_hdr[5]->time_to_live);
1116 --(ipv4_hdr[6]->time_to_live);
1117 --(ipv4_hdr[7]->time_to_live);
1118 ++(ipv4_hdr[4]->hdr_checksum);
1119 ++(ipv4_hdr[5]->hdr_checksum);
1120 ++(ipv4_hdr[6]->hdr_checksum);
1121 ++(ipv4_hdr[7]->hdr_checksum);
1125 *(uint64_t *)ð_hdr[0]->d_addr = dest_eth_addr[dst_port[0]];
1126 *(uint64_t *)ð_hdr[1]->d_addr = dest_eth_addr[dst_port[1]];
1127 *(uint64_t *)ð_hdr[2]->d_addr = dest_eth_addr[dst_port[2]];
1128 *(uint64_t *)ð_hdr[3]->d_addr = dest_eth_addr[dst_port[3]];
1129 *(uint64_t *)ð_hdr[4]->d_addr = dest_eth_addr[dst_port[4]];
1130 *(uint64_t *)ð_hdr[5]->d_addr = dest_eth_addr[dst_port[5]];
1131 *(uint64_t *)ð_hdr[6]->d_addr = dest_eth_addr[dst_port[6]];
1132 *(uint64_t *)ð_hdr[7]->d_addr = dest_eth_addr[dst_port[7]];
1135 ether_addr_copy(&ports_eth_addr[dst_port[0]], ð_hdr[0]->s_addr);
1136 ether_addr_copy(&ports_eth_addr[dst_port[1]], ð_hdr[1]->s_addr);
1137 ether_addr_copy(&ports_eth_addr[dst_port[2]], ð_hdr[2]->s_addr);
1138 ether_addr_copy(&ports_eth_addr[dst_port[3]], ð_hdr[3]->s_addr);
1139 ether_addr_copy(&ports_eth_addr[dst_port[4]], ð_hdr[4]->s_addr);
1140 ether_addr_copy(&ports_eth_addr[dst_port[5]], ð_hdr[5]->s_addr);
1141 ether_addr_copy(&ports_eth_addr[dst_port[6]], ð_hdr[6]->s_addr);
1142 ether_addr_copy(&ports_eth_addr[dst_port[7]], ð_hdr[7]->s_addr);
1144 send_single_packet(m[0], (uint8_t)dst_port[0]);
1145 send_single_packet(m[1], (uint8_t)dst_port[1]);
1146 send_single_packet(m[2], (uint8_t)dst_port[2]);
1147 send_single_packet(m[3], (uint8_t)dst_port[3]);
1148 send_single_packet(m[4], (uint8_t)dst_port[4]);
1149 send_single_packet(m[5], (uint8_t)dst_port[5]);
1150 send_single_packet(m[6], (uint8_t)dst_port[6]);
1151 send_single_packet(m[7], (uint8_t)dst_port[7]);
1155 static inline void get_ipv6_5tuple(struct rte_mbuf *m0, __m128i mask0,
1156 __m128i mask1, union ipv6_5tuple_host *key)
1158 __m128i tmpdata0 = _mm_loadu_si128(rte_pktmbuf_mtod_offset(m0,
1159 __m128i *, sizeof(struct ether_hdr) +
1160 offsetof(struct ipv6_hdr, payload_len)));
1161 __m128i tmpdata1 = _mm_loadu_si128(rte_pktmbuf_mtod_offset(m0,
1162 __m128i *, sizeof(struct ether_hdr) +
1163 offsetof(struct ipv6_hdr, payload_len) + sizeof(__m128i)));
1164 __m128i tmpdata2 = _mm_loadu_si128(rte_pktmbuf_mtod_offset(m0,
1165 __m128i *, sizeof(struct ether_hdr) +
1166 offsetof(struct ipv6_hdr, payload_len) + sizeof(__m128i) +
1168 key->xmm[0] = _mm_and_si128(tmpdata0, mask0);
1169 key->xmm[1] = tmpdata1;
1170 key->xmm[2] = _mm_and_si128(tmpdata2, mask1);
1174 simple_ipv6_fwd_8pkts(struct rte_mbuf *m[8], uint8_t portid)
1177 uint8_t dst_port[8];
1178 struct ether_hdr *eth_hdr[8];
1179 union ipv6_5tuple_host key[8];
1181 __attribute__((unused)) struct ipv6_hdr *ipv6_hdr[8];
1183 eth_hdr[0] = rte_pktmbuf_mtod(m[0], struct ether_hdr *);
1184 eth_hdr[1] = rte_pktmbuf_mtod(m[1], struct ether_hdr *);
1185 eth_hdr[2] = rte_pktmbuf_mtod(m[2], struct ether_hdr *);
1186 eth_hdr[3] = rte_pktmbuf_mtod(m[3], struct ether_hdr *);
1187 eth_hdr[4] = rte_pktmbuf_mtod(m[4], struct ether_hdr *);
1188 eth_hdr[5] = rte_pktmbuf_mtod(m[5], struct ether_hdr *);
1189 eth_hdr[6] = rte_pktmbuf_mtod(m[6], struct ether_hdr *);
1190 eth_hdr[7] = rte_pktmbuf_mtod(m[7], struct ether_hdr *);
1192 /* Handle IPv6 headers.*/
1193 ipv6_hdr[0] = rte_pktmbuf_mtod_offset(m[0], struct ipv6_hdr *,
1194 sizeof(struct ether_hdr));
1195 ipv6_hdr[1] = rte_pktmbuf_mtod_offset(m[1], struct ipv6_hdr *,
1196 sizeof(struct ether_hdr));
1197 ipv6_hdr[2] = rte_pktmbuf_mtod_offset(m[2], struct ipv6_hdr *,
1198 sizeof(struct ether_hdr));
1199 ipv6_hdr[3] = rte_pktmbuf_mtod_offset(m[3], struct ipv6_hdr *,
1200 sizeof(struct ether_hdr));
1201 ipv6_hdr[4] = rte_pktmbuf_mtod_offset(m[4], struct ipv6_hdr *,
1202 sizeof(struct ether_hdr));
1203 ipv6_hdr[5] = rte_pktmbuf_mtod_offset(m[5], struct ipv6_hdr *,
1204 sizeof(struct ether_hdr));
1205 ipv6_hdr[6] = rte_pktmbuf_mtod_offset(m[6], struct ipv6_hdr *,
1206 sizeof(struct ether_hdr));
1207 ipv6_hdr[7] = rte_pktmbuf_mtod_offset(m[7], struct ipv6_hdr *,
1208 sizeof(struct ether_hdr));
1210 get_ipv6_5tuple(m[0], mask1, mask2, &key[0]);
1211 get_ipv6_5tuple(m[1], mask1, mask2, &key[1]);
1212 get_ipv6_5tuple(m[2], mask1, mask2, &key[2]);
1213 get_ipv6_5tuple(m[3], mask1, mask2, &key[3]);
1214 get_ipv6_5tuple(m[4], mask1, mask2, &key[4]);
1215 get_ipv6_5tuple(m[5], mask1, mask2, &key[5]);
1216 get_ipv6_5tuple(m[6], mask1, mask2, &key[6]);
1217 get_ipv6_5tuple(m[7], mask1, mask2, &key[7]);
1219 const void *key_array[8] = {&key[0], &key[1], &key[2], &key[3],
1220 &key[4], &key[5], &key[6], &key[7]};
1222 rte_hash_lookup_bulk(RTE_PER_LCORE(lcore_conf)->ipv6_lookup_struct,
1223 &key_array[0], 4, ret);
1224 dst_port[0] = (uint8_t) ((ret[0] < 0) ? portid : ipv6_l3fwd_out_if[ret[0]]);
1225 dst_port[1] = (uint8_t) ((ret[1] < 0) ? portid : ipv6_l3fwd_out_if[ret[1]]);
1226 dst_port[2] = (uint8_t) ((ret[2] < 0) ? portid : ipv6_l3fwd_out_if[ret[2]]);
1227 dst_port[3] = (uint8_t) ((ret[3] < 0) ? portid : ipv6_l3fwd_out_if[ret[3]]);
1228 dst_port[4] = (uint8_t) ((ret[4] < 0) ? portid : ipv6_l3fwd_out_if[ret[4]]);
1229 dst_port[5] = (uint8_t) ((ret[5] < 0) ? portid : ipv6_l3fwd_out_if[ret[5]]);
1230 dst_port[6] = (uint8_t) ((ret[6] < 0) ? portid : ipv6_l3fwd_out_if[ret[6]]);
1231 dst_port[7] = (uint8_t) ((ret[7] < 0) ? portid : ipv6_l3fwd_out_if[ret[7]]);
1233 if (dst_port[0] >= RTE_MAX_ETHPORTS ||
1234 (enabled_port_mask & 1 << dst_port[0]) == 0)
1235 dst_port[0] = portid;
1236 if (dst_port[1] >= RTE_MAX_ETHPORTS ||
1237 (enabled_port_mask & 1 << dst_port[1]) == 0)
1238 dst_port[1] = portid;
1239 if (dst_port[2] >= RTE_MAX_ETHPORTS ||
1240 (enabled_port_mask & 1 << dst_port[2]) == 0)
1241 dst_port[2] = portid;
1242 if (dst_port[3] >= RTE_MAX_ETHPORTS ||
1243 (enabled_port_mask & 1 << dst_port[3]) == 0)
1244 dst_port[3] = portid;
1245 if (dst_port[4] >= RTE_MAX_ETHPORTS ||
1246 (enabled_port_mask & 1 << dst_port[4]) == 0)
1247 dst_port[4] = portid;
1248 if (dst_port[5] >= RTE_MAX_ETHPORTS ||
1249 (enabled_port_mask & 1 << dst_port[5]) == 0)
1250 dst_port[5] = portid;
1251 if (dst_port[6] >= RTE_MAX_ETHPORTS ||
1252 (enabled_port_mask & 1 << dst_port[6]) == 0)
1253 dst_port[6] = portid;
1254 if (dst_port[7] >= RTE_MAX_ETHPORTS ||
1255 (enabled_port_mask & 1 << dst_port[7]) == 0)
1256 dst_port[7] = portid;
1259 *(uint64_t *)ð_hdr[0]->d_addr = dest_eth_addr[dst_port[0]];
1260 *(uint64_t *)ð_hdr[1]->d_addr = dest_eth_addr[dst_port[1]];
1261 *(uint64_t *)ð_hdr[2]->d_addr = dest_eth_addr[dst_port[2]];
1262 *(uint64_t *)ð_hdr[3]->d_addr = dest_eth_addr[dst_port[3]];
1263 *(uint64_t *)ð_hdr[4]->d_addr = dest_eth_addr[dst_port[4]];
1264 *(uint64_t *)ð_hdr[5]->d_addr = dest_eth_addr[dst_port[5]];
1265 *(uint64_t *)ð_hdr[6]->d_addr = dest_eth_addr[dst_port[6]];
1266 *(uint64_t *)ð_hdr[7]->d_addr = dest_eth_addr[dst_port[7]];
1269 ether_addr_copy(&ports_eth_addr[dst_port[0]], ð_hdr[0]->s_addr);
1270 ether_addr_copy(&ports_eth_addr[dst_port[1]], ð_hdr[1]->s_addr);
1271 ether_addr_copy(&ports_eth_addr[dst_port[2]], ð_hdr[2]->s_addr);
1272 ether_addr_copy(&ports_eth_addr[dst_port[3]], ð_hdr[3]->s_addr);
1273 ether_addr_copy(&ports_eth_addr[dst_port[4]], ð_hdr[4]->s_addr);
1274 ether_addr_copy(&ports_eth_addr[dst_port[5]], ð_hdr[5]->s_addr);
1275 ether_addr_copy(&ports_eth_addr[dst_port[6]], ð_hdr[6]->s_addr);
1276 ether_addr_copy(&ports_eth_addr[dst_port[7]], ð_hdr[7]->s_addr);
1278 send_single_packet(m[0], (uint8_t)dst_port[0]);
1279 send_single_packet(m[1], (uint8_t)dst_port[1]);
1280 send_single_packet(m[2], (uint8_t)dst_port[2]);
1281 send_single_packet(m[3], (uint8_t)dst_port[3]);
1282 send_single_packet(m[4], (uint8_t)dst_port[4]);
1283 send_single_packet(m[5], (uint8_t)dst_port[5]);
1284 send_single_packet(m[6], (uint8_t)dst_port[6]);
1285 send_single_packet(m[7], (uint8_t)dst_port[7]);
1288 #endif /* APP_LOOKUP_METHOD */
1290 static __rte_always_inline void
1291 l3fwd_simple_forward(struct rte_mbuf *m, uint8_t portid)
1293 struct ether_hdr *eth_hdr;
1294 struct ipv4_hdr *ipv4_hdr;
1297 eth_hdr = rte_pktmbuf_mtod(m, struct ether_hdr *);
1299 if (RTE_ETH_IS_IPV4_HDR(m->packet_type)) {
1300 /* Handle IPv4 headers.*/
1301 ipv4_hdr = rte_pktmbuf_mtod_offset(m, struct ipv4_hdr *,
1302 sizeof(struct ether_hdr));
1304 #ifdef DO_RFC_1812_CHECKS
1305 /* Check to make sure the packet is valid (RFC1812) */
1306 if (is_valid_ipv4_pkt(ipv4_hdr, m->pkt_len) < 0) {
1307 rte_pktmbuf_free(m);
1312 dst_port = get_ipv4_dst_port(ipv4_hdr, portid,
1313 RTE_PER_LCORE(lcore_conf)->ipv4_lookup_struct);
1314 if (dst_port >= RTE_MAX_ETHPORTS ||
1315 (enabled_port_mask & 1 << dst_port) == 0)
1318 #ifdef DO_RFC_1812_CHECKS
1319 /* Update time to live and header checksum */
1320 --(ipv4_hdr->time_to_live);
1321 ++(ipv4_hdr->hdr_checksum);
1324 *(uint64_t *)ð_hdr->d_addr = dest_eth_addr[dst_port];
1327 ether_addr_copy(&ports_eth_addr[dst_port], ð_hdr->s_addr);
1329 send_single_packet(m, dst_port);
1330 } else if (RTE_ETH_IS_IPV6_HDR(m->packet_type)) {
1331 /* Handle IPv6 headers.*/
1332 struct ipv6_hdr *ipv6_hdr;
1334 ipv6_hdr = rte_pktmbuf_mtod_offset(m, struct ipv6_hdr *,
1335 sizeof(struct ether_hdr));
1337 dst_port = get_ipv6_dst_port(ipv6_hdr, portid,
1338 RTE_PER_LCORE(lcore_conf)->ipv6_lookup_struct);
1340 if (dst_port >= RTE_MAX_ETHPORTS ||
1341 (enabled_port_mask & 1 << dst_port) == 0)
1345 *(uint64_t *)ð_hdr->d_addr = dest_eth_addr[dst_port];
1348 ether_addr_copy(&ports_eth_addr[dst_port], ð_hdr->s_addr);
1350 send_single_packet(m, dst_port);
1352 /* Free the mbuf that contains non-IPV4/IPV6 packet */
1353 rte_pktmbuf_free(m);
1356 #if ((APP_LOOKUP_METHOD == APP_LOOKUP_LPM) && \
1357 (ENABLE_MULTI_BUFFER_OPTIMIZE == 1))
1358 #ifdef DO_RFC_1812_CHECKS
1360 #define IPV4_MIN_VER_IHL 0x45
1361 #define IPV4_MAX_VER_IHL 0x4f
1362 #define IPV4_MAX_VER_IHL_DIFF (IPV4_MAX_VER_IHL - IPV4_MIN_VER_IHL)
1364 /* Minimum value of IPV4 total length (20B) in network byte order. */
1365 #define IPV4_MIN_LEN_BE (sizeof(struct ipv4_hdr) << 8)
1368 * From http://www.rfc-editor.org/rfc/rfc1812.txt section 5.2.2:
1369 * - The IP version number must be 4.
1370 * - The IP header length field must be large enough to hold the
1371 * minimum length legal IP datagram (20 bytes = 5 words).
1372 * - The IP total length field must be large enough to hold the IP
1373 * datagram header, whose length is specified in the IP header length
1375 * If we encounter invalid IPV4 packet, then set destination port for it
1376 * to BAD_PORT value.
1378 static __rte_always_inline void
1379 rfc1812_process(struct ipv4_hdr *ipv4_hdr, uint16_t *dp, uint32_t ptype)
1383 if (RTE_ETH_IS_IPV4_HDR(ptype)) {
1384 ihl = ipv4_hdr->version_ihl - IPV4_MIN_VER_IHL;
1386 ipv4_hdr->time_to_live--;
1387 ipv4_hdr->hdr_checksum++;
1389 if (ihl > IPV4_MAX_VER_IHL_DIFF ||
1390 ((uint8_t)ipv4_hdr->total_length == 0 &&
1391 ipv4_hdr->total_length < IPV4_MIN_LEN_BE)) {
1398 #define rfc1812_process(mb, dp, ptype) do { } while (0)
1399 #endif /* DO_RFC_1812_CHECKS */
1400 #endif /* APP_LOOKUP_LPM && ENABLE_MULTI_BUFFER_OPTIMIZE */
1403 #if ((APP_LOOKUP_METHOD == APP_LOOKUP_LPM) && \
1404 (ENABLE_MULTI_BUFFER_OPTIMIZE == 1))
1406 static __rte_always_inline uint16_t
1407 get_dst_port(struct rte_mbuf *pkt, uint32_t dst_ipv4, uint8_t portid)
1410 struct ipv6_hdr *ipv6_hdr;
1411 struct ether_hdr *eth_hdr;
1413 if (RTE_ETH_IS_IPV4_HDR(pkt->packet_type)) {
1414 return (uint16_t) ((rte_lpm_lookup(
1415 RTE_PER_LCORE(lcore_conf)->ipv4_lookup_struct, dst_ipv4,
1416 &next_hop) == 0) ? next_hop : portid);
1418 } else if (RTE_ETH_IS_IPV6_HDR(pkt->packet_type)) {
1420 eth_hdr = rte_pktmbuf_mtod(pkt, struct ether_hdr *);
1421 ipv6_hdr = (struct ipv6_hdr *)(eth_hdr + 1);
1423 return (uint16_t) ((rte_lpm6_lookup(
1424 RTE_PER_LCORE(lcore_conf)->ipv6_lookup_struct,
1425 ipv6_hdr->dst_addr, &next_hop) == 0) ?
1434 process_packet(struct rte_mbuf *pkt, uint16_t *dst_port, uint8_t portid)
1436 struct ether_hdr *eth_hdr;
1437 struct ipv4_hdr *ipv4_hdr;
1442 eth_hdr = rte_pktmbuf_mtod(pkt, struct ether_hdr *);
1443 ipv4_hdr = (struct ipv4_hdr *)(eth_hdr + 1);
1445 dst_ipv4 = ipv4_hdr->dst_addr;
1446 dst_ipv4 = rte_be_to_cpu_32(dst_ipv4);
1447 dp = get_dst_port(pkt, dst_ipv4, portid);
1449 te = _mm_load_si128((__m128i *)eth_hdr);
1453 rfc1812_process(ipv4_hdr, dst_port, pkt->packet_type);
1455 te = _mm_blend_epi16(te, ve, MASK_ETH);
1456 _mm_store_si128((__m128i *)eth_hdr, te);
1460 * Read packet_type and destination IPV4 addresses from 4 mbufs.
1463 processx4_step1(struct rte_mbuf *pkt[FWDSTEP],
1465 uint32_t *ipv4_flag)
1467 struct ipv4_hdr *ipv4_hdr;
1468 struct ether_hdr *eth_hdr;
1469 uint32_t x0, x1, x2, x3;
1471 eth_hdr = rte_pktmbuf_mtod(pkt[0], struct ether_hdr *);
1472 ipv4_hdr = (struct ipv4_hdr *)(eth_hdr + 1);
1473 x0 = ipv4_hdr->dst_addr;
1474 ipv4_flag[0] = pkt[0]->packet_type & RTE_PTYPE_L3_IPV4;
1476 eth_hdr = rte_pktmbuf_mtod(pkt[1], struct ether_hdr *);
1477 ipv4_hdr = (struct ipv4_hdr *)(eth_hdr + 1);
1478 x1 = ipv4_hdr->dst_addr;
1479 ipv4_flag[0] &= pkt[1]->packet_type;
1481 eth_hdr = rte_pktmbuf_mtod(pkt[2], struct ether_hdr *);
1482 ipv4_hdr = (struct ipv4_hdr *)(eth_hdr + 1);
1483 x2 = ipv4_hdr->dst_addr;
1484 ipv4_flag[0] &= pkt[2]->packet_type;
1486 eth_hdr = rte_pktmbuf_mtod(pkt[3], struct ether_hdr *);
1487 ipv4_hdr = (struct ipv4_hdr *)(eth_hdr + 1);
1488 x3 = ipv4_hdr->dst_addr;
1489 ipv4_flag[0] &= pkt[3]->packet_type;
1491 dip[0] = _mm_set_epi32(x3, x2, x1, x0);
1495 * Lookup into LPM for destination port.
1496 * If lookup fails, use incoming port (portid) as destination port.
1499 processx4_step2(__m128i dip,
1502 struct rte_mbuf *pkt[FWDSTEP],
1503 uint16_t dprt[FWDSTEP])
1506 const __m128i bswap_mask = _mm_set_epi8(12, 13, 14, 15, 8, 9, 10, 11,
1507 4, 5, 6, 7, 0, 1, 2, 3);
1509 /* Byte swap 4 IPV4 addresses. */
1510 dip = _mm_shuffle_epi8(dip, bswap_mask);
1512 /* if all 4 packets are IPV4. */
1513 if (likely(ipv4_flag)) {
1514 rte_lpm_lookupx4(RTE_PER_LCORE(lcore_conf)->ipv4_lookup_struct, dip,
1517 /* get rid of unused upper 16 bit for each dport. */
1518 dst.x = _mm_packs_epi32(dst.x, dst.x);
1519 *(uint64_t *)dprt = dst.u64[0];
1522 dprt[0] = get_dst_port(pkt[0], dst.u32[0], portid);
1523 dprt[1] = get_dst_port(pkt[1], dst.u32[1], portid);
1524 dprt[2] = get_dst_port(pkt[2], dst.u32[2], portid);
1525 dprt[3] = get_dst_port(pkt[3], dst.u32[3], portid);
1530 * Update source and destination MAC addresses in the ethernet header.
1531 * Perform RFC1812 checks and updates for IPV4 packets.
1534 processx4_step3(struct rte_mbuf *pkt[FWDSTEP], uint16_t dst_port[FWDSTEP])
1536 __m128i te[FWDSTEP];
1537 __m128i ve[FWDSTEP];
1538 __m128i *p[FWDSTEP];
1540 p[0] = rte_pktmbuf_mtod(pkt[0], __m128i *);
1541 p[1] = rte_pktmbuf_mtod(pkt[1], __m128i *);
1542 p[2] = rte_pktmbuf_mtod(pkt[2], __m128i *);
1543 p[3] = rte_pktmbuf_mtod(pkt[3], __m128i *);
1545 ve[0] = val_eth[dst_port[0]];
1546 te[0] = _mm_load_si128(p[0]);
1548 ve[1] = val_eth[dst_port[1]];
1549 te[1] = _mm_load_si128(p[1]);
1551 ve[2] = val_eth[dst_port[2]];
1552 te[2] = _mm_load_si128(p[2]);
1554 ve[3] = val_eth[dst_port[3]];
1555 te[3] = _mm_load_si128(p[3]);
1557 /* Update first 12 bytes, keep rest bytes intact. */
1558 te[0] = _mm_blend_epi16(te[0], ve[0], MASK_ETH);
1559 te[1] = _mm_blend_epi16(te[1], ve[1], MASK_ETH);
1560 te[2] = _mm_blend_epi16(te[2], ve[2], MASK_ETH);
1561 te[3] = _mm_blend_epi16(te[3], ve[3], MASK_ETH);
1563 _mm_store_si128(p[0], te[0]);
1564 _mm_store_si128(p[1], te[1]);
1565 _mm_store_si128(p[2], te[2]);
1566 _mm_store_si128(p[3], te[3]);
1568 rfc1812_process((struct ipv4_hdr *)((struct ether_hdr *)p[0] + 1),
1569 &dst_port[0], pkt[0]->packet_type);
1570 rfc1812_process((struct ipv4_hdr *)((struct ether_hdr *)p[1] + 1),
1571 &dst_port[1], pkt[1]->packet_type);
1572 rfc1812_process((struct ipv4_hdr *)((struct ether_hdr *)p[2] + 1),
1573 &dst_port[2], pkt[2]->packet_type);
1574 rfc1812_process((struct ipv4_hdr *)((struct ether_hdr *)p[3] + 1),
1575 &dst_port[3], pkt[3]->packet_type);
1579 * We group consecutive packets with the same destionation port into one burst.
1580 * To avoid extra latency this is done together with some other packet
1581 * processing, but after we made a final decision about packet's destination.
1582 * To do this we maintain:
1583 * pnum - array of number of consecutive packets with the same dest port for
1584 * each packet in the input burst.
1585 * lp - pointer to the last updated element in the pnum.
1586 * dlp - dest port value lp corresponds to.
1589 #define GRPSZ (1 << FWDSTEP)
1590 #define GRPMSK (GRPSZ - 1)
1592 #define GROUP_PORT_STEP(dlp, dcp, lp, pn, idx) do { \
1593 if (likely((dlp) == (dcp)[(idx)])) { \
1596 (dlp) = (dcp)[idx]; \
1597 (lp) = (pn) + (idx); \
1603 * Group consecutive packets with the same destination port in bursts of 4.
1604 * Suppose we have array of destionation ports:
1605 * dst_port[] = {a, b, c, d,, e, ... }
1606 * dp1 should contain: <a, b, c, d>, dp2: <b, c, d, e>.
1607 * We doing 4 comparisons at once and the result is 4 bit mask.
1608 * This mask is used as an index into prebuild array of pnum values.
1610 static inline uint16_t *
1611 port_groupx4(uint16_t pn[FWDSTEP + 1], uint16_t *lp, __m128i dp1, __m128i dp2)
1613 static const struct {
1614 uint64_t pnum; /* prebuild 4 values for pnum[]. */
1615 int32_t idx; /* index for new last updated elemnet. */
1616 uint16_t lpv; /* add value to the last updated element. */
1619 /* 0: a != b, b != c, c != d, d != e */
1620 .pnum = UINT64_C(0x0001000100010001),
1625 /* 1: a == b, b != c, c != d, d != e */
1626 .pnum = UINT64_C(0x0001000100010002),
1631 /* 2: a != b, b == c, c != d, d != e */
1632 .pnum = UINT64_C(0x0001000100020001),
1637 /* 3: a == b, b == c, c != d, d != e */
1638 .pnum = UINT64_C(0x0001000100020003),
1643 /* 4: a != b, b != c, c == d, d != e */
1644 .pnum = UINT64_C(0x0001000200010001),
1649 /* 5: a == b, b != c, c == d, d != e */
1650 .pnum = UINT64_C(0x0001000200010002),
1655 /* 6: a != b, b == c, c == d, d != e */
1656 .pnum = UINT64_C(0x0001000200030001),
1661 /* 7: a == b, b == c, c == d, d != e */
1662 .pnum = UINT64_C(0x0001000200030004),
1667 /* 8: a != b, b != c, c != d, d == e */
1668 .pnum = UINT64_C(0x0002000100010001),
1673 /* 9: a == b, b != c, c != d, d == e */
1674 .pnum = UINT64_C(0x0002000100010002),
1679 /* 0xa: a != b, b == c, c != d, d == e */
1680 .pnum = UINT64_C(0x0002000100020001),
1685 /* 0xb: a == b, b == c, c != d, d == e */
1686 .pnum = UINT64_C(0x0002000100020003),
1691 /* 0xc: a != b, b != c, c == d, d == e */
1692 .pnum = UINT64_C(0x0002000300010001),
1697 /* 0xd: a == b, b != c, c == d, d == e */
1698 .pnum = UINT64_C(0x0002000300010002),
1703 /* 0xe: a != b, b == c, c == d, d == e */
1704 .pnum = UINT64_C(0x0002000300040001),
1709 /* 0xf: a == b, b == c, c == d, d == e */
1710 .pnum = UINT64_C(0x0002000300040005),
1717 uint16_t u16[FWDSTEP + 1];
1719 } *pnum = (void *)pn;
1723 dp1 = _mm_cmpeq_epi16(dp1, dp2);
1724 dp1 = _mm_unpacklo_epi16(dp1, dp1);
1725 v = _mm_movemask_ps((__m128)dp1);
1727 /* update last port counter. */
1728 lp[0] += gptbl[v].lpv;
1730 /* if dest port value has changed. */
1732 pnum->u64 = gptbl[v].pnum;
1733 pnum->u16[FWDSTEP] = 1;
1734 lp = pnum->u16 + gptbl[v].idx;
1740 #endif /* APP_LOOKUP_METHOD */
1743 process_burst(struct rte_mbuf *pkts_burst[MAX_PKT_BURST], int nb_rx,
1748 #if ((APP_LOOKUP_METHOD == APP_LOOKUP_LPM) && \
1749 (ENABLE_MULTI_BUFFER_OPTIMIZE == 1))
1753 uint16_t dst_port[MAX_PKT_BURST];
1754 __m128i dip[MAX_PKT_BURST / FWDSTEP];
1755 uint32_t ipv4_flag[MAX_PKT_BURST / FWDSTEP];
1756 uint16_t pnum[MAX_PKT_BURST + 1];
1760 #if (ENABLE_MULTI_BUFFER_OPTIMIZE == 1)
1761 #if (APP_LOOKUP_METHOD == APP_LOOKUP_EXACT_MATCH)
1764 * Send nb_rx - nb_rx%8 packets
1767 int32_t n = RTE_ALIGN_FLOOR(nb_rx, 8);
1769 for (j = 0; j < n; j += 8) {
1771 pkts_burst[j]->packet_type &
1772 pkts_burst[j+1]->packet_type &
1773 pkts_burst[j+2]->packet_type &
1774 pkts_burst[j+3]->packet_type &
1775 pkts_burst[j+4]->packet_type &
1776 pkts_burst[j+5]->packet_type &
1777 pkts_burst[j+6]->packet_type &
1778 pkts_burst[j+7]->packet_type;
1779 if (pkt_type & RTE_PTYPE_L3_IPV4) {
1780 simple_ipv4_fwd_8pkts(&pkts_burst[j], portid);
1781 } else if (pkt_type &
1782 RTE_PTYPE_L3_IPV6) {
1783 simple_ipv6_fwd_8pkts(&pkts_burst[j], portid);
1785 l3fwd_simple_forward(pkts_burst[j], portid);
1786 l3fwd_simple_forward(pkts_burst[j+1], portid);
1787 l3fwd_simple_forward(pkts_burst[j+2], portid);
1788 l3fwd_simple_forward(pkts_burst[j+3], portid);
1789 l3fwd_simple_forward(pkts_burst[j+4], portid);
1790 l3fwd_simple_forward(pkts_burst[j+5], portid);
1791 l3fwd_simple_forward(pkts_burst[j+6], portid);
1792 l3fwd_simple_forward(pkts_burst[j+7], portid);
1795 for (; j < nb_rx ; j++)
1796 l3fwd_simple_forward(pkts_burst[j], portid);
1798 #elif (APP_LOOKUP_METHOD == APP_LOOKUP_LPM)
1800 k = RTE_ALIGN_FLOOR(nb_rx, FWDSTEP);
1801 for (j = 0; j != k; j += FWDSTEP)
1802 processx4_step1(&pkts_burst[j], &dip[j / FWDSTEP],
1803 &ipv4_flag[j / FWDSTEP]);
1805 k = RTE_ALIGN_FLOOR(nb_rx, FWDSTEP);
1806 for (j = 0; j != k; j += FWDSTEP)
1807 processx4_step2(dip[j / FWDSTEP], ipv4_flag[j / FWDSTEP],
1808 portid, &pkts_burst[j], &dst_port[j]);
1811 * Finish packet processing and group consecutive
1812 * packets with the same destination port.
1814 k = RTE_ALIGN_FLOOR(nb_rx, FWDSTEP);
1821 processx4_step3(pkts_burst, dst_port);
1823 /* dp1: <d[0], d[1], d[2], d[3], ... > */
1824 dp1 = _mm_loadu_si128((__m128i *)dst_port);
1826 for (j = FWDSTEP; j != k; j += FWDSTEP) {
1827 processx4_step3(&pkts_burst[j], &dst_port[j]);
1831 * <d[j-3], d[j-2], d[j-1], d[j], ... >
1833 dp2 = _mm_loadu_si128(
1834 (__m128i *)&dst_port[j - FWDSTEP + 1]);
1835 lp = port_groupx4(&pnum[j - FWDSTEP], lp, dp1, dp2);
1839 * <d[j], d[j+1], d[j+2], d[j+3], ... >
1841 dp1 = _mm_srli_si128(dp2, (FWDSTEP - 1) *
1842 sizeof(dst_port[0]));
1846 * dp2: <d[j-3], d[j-2], d[j-1], d[j-1], ... >
1848 dp2 = _mm_shufflelo_epi16(dp1, 0xf9);
1849 lp = port_groupx4(&pnum[j - FWDSTEP], lp, dp1, dp2);
1852 * remove values added by the last repeated
1856 dlp = dst_port[j - 1];
1858 /* set dlp and lp to the never used values. */
1860 lp = pnum + MAX_PKT_BURST;
1863 /* Process up to last 3 packets one by one. */
1864 switch (nb_rx % FWDSTEP) {
1866 process_packet(pkts_burst[j], dst_port + j, portid);
1867 GROUP_PORT_STEP(dlp, dst_port, lp, pnum, j);
1871 process_packet(pkts_burst[j], dst_port + j, portid);
1872 GROUP_PORT_STEP(dlp, dst_port, lp, pnum, j);
1876 process_packet(pkts_burst[j], dst_port + j, portid);
1877 GROUP_PORT_STEP(dlp, dst_port, lp, pnum, j);
1882 * Send packets out, through destination port.
1883 * Consecuteve pacekts with the same destination port
1884 * are already grouped together.
1885 * If destination port for the packet equals BAD_PORT,
1886 * then free the packet without sending it out.
1888 for (j = 0; j < nb_rx; j += k) {
1896 if (likely(pn != BAD_PORT))
1897 send_packetsx4(pn, pkts_burst + j, k);
1899 for (m = j; m != j + k; m++)
1900 rte_pktmbuf_free(pkts_burst[m]);
1904 #endif /* APP_LOOKUP_METHOD */
1905 #else /* ENABLE_MULTI_BUFFER_OPTIMIZE == 0 */
1907 /* Prefetch first packets */
1908 for (j = 0; j < PREFETCH_OFFSET && j < nb_rx; j++)
1909 rte_prefetch0(rte_pktmbuf_mtod(pkts_burst[j], void *));
1911 /* Prefetch and forward already prefetched packets */
1912 for (j = 0; j < (nb_rx - PREFETCH_OFFSET); j++) {
1913 rte_prefetch0(rte_pktmbuf_mtod(pkts_burst[
1914 j + PREFETCH_OFFSET], void *));
1915 l3fwd_simple_forward(pkts_burst[j], portid);
1918 /* Forward remaining prefetched packets */
1919 for (; j < nb_rx; j++)
1920 l3fwd_simple_forward(pkts_burst[j], portid);
1922 #endif /* ENABLE_MULTI_BUFFER_OPTIMIZE */
1926 #if (APP_CPU_LOAD > 0)
1929 * CPU-load stats collector
1932 cpu_load_collector(__rte_unused void *arg) {
1935 uint64_t prev_tsc, diff_tsc, cur_tsc;
1936 uint64_t total[MAX_CPU] = { 0 };
1937 unsigned min_cpu = MAX_CPU;
1938 unsigned max_cpu = 0;
1943 unsigned int n_thread_per_cpu[MAX_CPU] = { 0 };
1944 struct thread_conf *thread_per_cpu[MAX_CPU][MAX_THREAD];
1946 struct thread_conf *thread_conf;
1948 const uint64_t interval_tsc = (rte_get_tsc_hz() + US_PER_S - 1) /
1949 US_PER_S * CPU_LOAD_TIMEOUT_US;
1953 * Wait for all threads
1956 printf("Waiting for %d rx threads and %d tx threads\n", n_rx_thread,
1959 while (rte_atomic16_read(&rx_counter) < n_rx_thread)
1962 while (rte_atomic16_read(&tx_counter) < n_tx_thread)
1965 for (i = 0; i < n_rx_thread; i++) {
1967 thread_conf = &rx_thread[i].conf;
1968 cpu_id = thread_conf->cpu_id;
1969 thread_per_cpu[cpu_id][n_thread_per_cpu[cpu_id]++] = thread_conf;
1971 if (cpu_id > max_cpu)
1973 if (cpu_id < min_cpu)
1976 for (i = 0; i < n_tx_thread; i++) {
1978 thread_conf = &tx_thread[i].conf;
1979 cpu_id = thread_conf->cpu_id;
1980 thread_per_cpu[cpu_id][n_thread_per_cpu[cpu_id]++] = thread_conf;
1982 if (thread_conf->cpu_id > max_cpu)
1983 max_cpu = thread_conf->cpu_id;
1984 if (thread_conf->cpu_id < min_cpu)
1985 min_cpu = thread_conf->cpu_id;
1991 for (i = min_cpu; i <= max_cpu; i++) {
1992 for (j = 0; j < MAX_CPU_COUNTER; j++) {
1993 for (k = 0; k < n_thread_per_cpu[i]; k++)
1994 if (thread_per_cpu[i][k]->busy[j]) {
1999 cpu_load.hits[j][i]++;
2011 cur_tsc = rte_rdtsc();
2013 diff_tsc = cur_tsc - prev_tsc;
2014 if (unlikely(diff_tsc > interval_tsc)) {
2018 printf("Cpu usage for %d rx threads and %d tx threads:\n\n",
2019 n_rx_thread, n_tx_thread);
2021 printf("cpu# proc%% poll%% overhead%%\n\n");
2023 for (i = min_cpu; i <= max_cpu; i++) {
2025 printf("CPU %d:", i);
2026 for (j = 0; j < MAX_CPU_COUNTER; j++) {
2027 printf("%7" PRIu64 "",
2028 cpu_load.hits[j][i] * 100 / cpu_load.counter);
2029 hits += cpu_load.hits[j][i];
2030 cpu_load.hits[j][i] = 0;
2032 printf("%7" PRIu64 "\n",
2033 100 - total[i] * 100 / cpu_load.counter);
2036 cpu_load.counter = 0;
2043 #endif /* APP_CPU_LOAD */
2046 * Null processing lthread loop
2048 * This loop is used to start empty scheduler on lcore.
2051 lthread_null(__rte_unused void *args)
2053 int lcore_id = rte_lcore_id();
2055 RTE_LOG(INFO, L3FWD, "Starting scheduler on lcore %d.\n", lcore_id);
2059 /* main processing loop */
2061 lthread_tx_per_ring(void *dummy)
2065 struct rte_ring *ring;
2066 struct thread_tx_conf *tx_conf;
2067 struct rte_mbuf *pkts_burst[MAX_PKT_BURST];
2068 struct lthread_cond *ready;
2070 tx_conf = (struct thread_tx_conf *)dummy;
2071 ring = tx_conf->ring;
2072 ready = *tx_conf->ready;
2074 lthread_set_data((void *)tx_conf);
2077 * Move this lthread to lcore
2079 lthread_set_affinity(tx_conf->conf.lcore_id);
2081 RTE_LOG(INFO, L3FWD, "entering main tx loop on lcore %u\n", rte_lcore_id());
2084 rte_atomic16_inc(&tx_counter);
2088 * Read packet from ring
2090 SET_CPU_BUSY(tx_conf, CPU_POLL);
2091 nb_rx = rte_ring_sc_dequeue_burst(ring, (void **)pkts_burst,
2092 MAX_PKT_BURST, NULL);
2093 SET_CPU_IDLE(tx_conf, CPU_POLL);
2096 SET_CPU_BUSY(tx_conf, CPU_PROCESS);
2097 portid = pkts_burst[0]->port;
2098 process_burst(pkts_burst, nb_rx, portid);
2099 SET_CPU_IDLE(tx_conf, CPU_PROCESS);
2102 lthread_cond_wait(ready, 0);
2108 * Main tx-lthreads spawner lthread.
2110 * This lthread is used to spawn one new lthread per ring from producers.
2114 lthread_tx(void *args)
2120 struct thread_tx_conf *tx_conf;
2122 tx_conf = (struct thread_tx_conf *)args;
2123 lthread_set_data((void *)tx_conf);
2126 * Move this lthread to the selected lcore
2128 lthread_set_affinity(tx_conf->conf.lcore_id);
2131 * Spawn tx readers (one per input ring)
2133 lthread_create(<, tx_conf->conf.lcore_id, lthread_tx_per_ring,
2136 lcore_id = rte_lcore_id();
2138 RTE_LOG(INFO, L3FWD, "Entering Tx main loop on lcore %u\n", lcore_id);
2140 tx_conf->conf.cpu_id = sched_getcpu();
2143 lthread_sleep(BURST_TX_DRAIN_US * 1000);
2146 * TX burst queue drain
2148 for (portid = 0; portid < RTE_MAX_ETHPORTS; portid++) {
2149 if (tx_conf->tx_mbufs[portid].len == 0)
2151 SET_CPU_BUSY(tx_conf, CPU_PROCESS);
2152 send_burst(tx_conf, tx_conf->tx_mbufs[portid].len, portid);
2153 SET_CPU_IDLE(tx_conf, CPU_PROCESS);
2154 tx_conf->tx_mbufs[portid].len = 0;
2161 lthread_rx(void *dummy)
2166 uint8_t portid, queueid;
2168 int len[RTE_MAX_LCORE] = { 0 };
2169 int old_len, new_len;
2170 struct rte_mbuf *pkts_burst[MAX_PKT_BURST];
2171 struct thread_rx_conf *rx_conf;
2173 rx_conf = (struct thread_rx_conf *)dummy;
2174 lthread_set_data((void *)rx_conf);
2177 * Move this lthread to lcore
2179 lthread_set_affinity(rx_conf->conf.lcore_id);
2181 if (rx_conf->n_rx_queue == 0) {
2182 RTE_LOG(INFO, L3FWD, "lcore %u has nothing to do\n", rte_lcore_id());
2186 RTE_LOG(INFO, L3FWD, "Entering main Rx loop on lcore %u\n", rte_lcore_id());
2188 for (i = 0; i < rx_conf->n_rx_queue; i++) {
2190 portid = rx_conf->rx_queue_list[i].port_id;
2191 queueid = rx_conf->rx_queue_list[i].queue_id;
2192 RTE_LOG(INFO, L3FWD, " -- lcoreid=%u portid=%hhu rxqueueid=%hhu\n",
2193 rte_lcore_id(), portid, queueid);
2197 * Init all condition variables (one per rx thread)
2199 for (i = 0; i < rx_conf->n_rx_queue; i++)
2200 lthread_cond_init(NULL, &rx_conf->ready[i], NULL);
2204 rx_conf->conf.cpu_id = sched_getcpu();
2205 rte_atomic16_inc(&rx_counter);
2209 * Read packet from RX queues
2211 for (i = 0; i < rx_conf->n_rx_queue; ++i) {
2212 portid = rx_conf->rx_queue_list[i].port_id;
2213 queueid = rx_conf->rx_queue_list[i].queue_id;
2215 SET_CPU_BUSY(rx_conf, CPU_POLL);
2216 nb_rx = rte_eth_rx_burst(portid, queueid, pkts_burst,
2218 SET_CPU_IDLE(rx_conf, CPU_POLL);
2221 worker_id = (worker_id + 1) % rx_conf->n_ring;
2222 old_len = len[worker_id];
2224 SET_CPU_BUSY(rx_conf, CPU_PROCESS);
2225 ret = rte_ring_sp_enqueue_burst(
2226 rx_conf->ring[worker_id],
2227 (void **) pkts_burst,
2230 new_len = old_len + ret;
2232 if (new_len >= BURST_SIZE) {
2233 lthread_cond_signal(rx_conf->ready[worker_id]);
2237 len[worker_id] = new_len;
2239 if (unlikely(ret < nb_rx)) {
2242 for (k = ret; k < nb_rx; k++) {
2243 struct rte_mbuf *m = pkts_burst[k];
2245 rte_pktmbuf_free(m);
2248 SET_CPU_IDLE(rx_conf, CPU_PROCESS);
2257 * Start scheduler with initial lthread on lcore
2259 * This lthread loop spawns all rx and tx lthreads on master lcore
2263 lthread_spawner(__rte_unused void *arg) {
2264 struct lthread *lt[MAX_THREAD];
2268 printf("Entering lthread_spawner\n");
2271 * Create producers (rx threads) on default lcore
2273 for (i = 0; i < n_rx_thread; i++) {
2274 rx_thread[i].conf.thread_id = i;
2275 lthread_create(<[n_thread], -1, lthread_rx,
2276 (void *)&rx_thread[i]);
2281 * Wait for all producers. Until some producers can be started on the same
2282 * scheduler as this lthread, yielding is required to let them to run and
2283 * prevent deadlock here.
2285 while (rte_atomic16_read(&rx_counter) < n_rx_thread)
2286 lthread_sleep(100000);
2289 * Create consumers (tx threads) on default lcore_id
2291 for (i = 0; i < n_tx_thread; i++) {
2292 tx_thread[i].conf.thread_id = i;
2293 lthread_create(<[n_thread], -1, lthread_tx,
2294 (void *)&tx_thread[i]);
2299 * Wait for all threads finished
2301 for (i = 0; i < n_thread; i++)
2302 lthread_join(lt[i], NULL);
2307 * Start master scheduler with initial lthread spawning rx and tx lthreads
2308 * (main_lthread_master).
2311 lthread_master_spawner(__rte_unused void *arg) {
2313 int lcore_id = rte_lcore_id();
2315 RTE_PER_LCORE(lcore_conf) = &lcore_conf[lcore_id];
2316 lthread_create(<, -1, lthread_spawner, NULL);
2323 * Start scheduler on lcore.
2326 sched_spawner(__rte_unused void *arg) {
2328 int lcore_id = rte_lcore_id();
2331 if (lcore_id == cpu_load_lcore_id) {
2332 cpu_load_collector(arg);
2335 #endif /* APP_CPU_LOAD */
2337 RTE_PER_LCORE(lcore_conf) = &lcore_conf[lcore_id];
2338 lthread_create(<, -1, lthread_null, NULL);
2344 /* main processing loop */
2346 pthread_tx(void *dummy)
2348 struct rte_mbuf *pkts_burst[MAX_PKT_BURST];
2349 uint64_t prev_tsc, diff_tsc, cur_tsc;
2352 struct thread_tx_conf *tx_conf;
2354 const uint64_t drain_tsc = (rte_get_tsc_hz() + US_PER_S - 1) /
2355 US_PER_S * BURST_TX_DRAIN_US;
2359 tx_conf = (struct thread_tx_conf *)dummy;
2361 RTE_LOG(INFO, L3FWD, "Entering main Tx loop on lcore %u\n", rte_lcore_id());
2363 tx_conf->conf.cpu_id = sched_getcpu();
2364 rte_atomic16_inc(&tx_counter);
2367 cur_tsc = rte_rdtsc();
2370 * TX burst queue drain
2372 diff_tsc = cur_tsc - prev_tsc;
2373 if (unlikely(diff_tsc > drain_tsc)) {
2376 * This could be optimized (use queueid instead of
2377 * portid), but it is not called so often
2379 SET_CPU_BUSY(tx_conf, CPU_PROCESS);
2380 for (portid = 0; portid < RTE_MAX_ETHPORTS; portid++) {
2381 if (tx_conf->tx_mbufs[portid].len == 0)
2383 send_burst(tx_conf, tx_conf->tx_mbufs[portid].len, portid);
2384 tx_conf->tx_mbufs[portid].len = 0;
2386 SET_CPU_IDLE(tx_conf, CPU_PROCESS);
2392 * Read packet from ring
2394 SET_CPU_BUSY(tx_conf, CPU_POLL);
2395 nb_rx = rte_ring_sc_dequeue_burst(tx_conf->ring,
2396 (void **)pkts_burst, MAX_PKT_BURST, NULL);
2397 SET_CPU_IDLE(tx_conf, CPU_POLL);
2399 if (unlikely(nb_rx == 0)) {
2404 SET_CPU_BUSY(tx_conf, CPU_PROCESS);
2405 portid = pkts_burst[0]->port;
2406 process_burst(pkts_burst, nb_rx, portid);
2407 SET_CPU_IDLE(tx_conf, CPU_PROCESS);
2413 pthread_rx(void *dummy)
2420 uint8_t portid, queueid;
2421 struct rte_mbuf *pkts_burst[MAX_PKT_BURST];
2423 struct thread_rx_conf *rx_conf;
2425 lcore_id = rte_lcore_id();
2426 rx_conf = (struct thread_rx_conf *)dummy;
2428 if (rx_conf->n_rx_queue == 0) {
2429 RTE_LOG(INFO, L3FWD, "lcore %u has nothing to do\n", lcore_id);
2433 RTE_LOG(INFO, L3FWD, "entering main rx loop on lcore %u\n", lcore_id);
2435 for (i = 0; i < rx_conf->n_rx_queue; i++) {
2437 portid = rx_conf->rx_queue_list[i].port_id;
2438 queueid = rx_conf->rx_queue_list[i].queue_id;
2439 RTE_LOG(INFO, L3FWD, " -- lcoreid=%u portid=%hhu rxqueueid=%hhu\n",
2440 lcore_id, portid, queueid);
2444 rx_conf->conf.cpu_id = sched_getcpu();
2445 rte_atomic16_inc(&rx_counter);
2449 * Read packet from RX queues
2451 for (i = 0; i < rx_conf->n_rx_queue; ++i) {
2452 portid = rx_conf->rx_queue_list[i].port_id;
2453 queueid = rx_conf->rx_queue_list[i].queue_id;
2455 SET_CPU_BUSY(rx_conf, CPU_POLL);
2456 nb_rx = rte_eth_rx_burst(portid, queueid, pkts_burst,
2458 SET_CPU_IDLE(rx_conf, CPU_POLL);
2465 SET_CPU_BUSY(rx_conf, CPU_PROCESS);
2466 worker_id = (worker_id + 1) % rx_conf->n_ring;
2467 n = rte_ring_sp_enqueue_burst(rx_conf->ring[worker_id],
2468 (void **)pkts_burst, nb_rx, NULL);
2470 if (unlikely(n != nb_rx)) {
2473 for (k = n; k < nb_rx; k++) {
2474 struct rte_mbuf *m = pkts_burst[k];
2476 rte_pktmbuf_free(m);
2480 SET_CPU_IDLE(rx_conf, CPU_PROCESS);
2490 pthread_run(__rte_unused void *arg) {
2491 int lcore_id = rte_lcore_id();
2494 for (i = 0; i < n_rx_thread; i++)
2495 if (rx_thread[i].conf.lcore_id == lcore_id) {
2496 printf("Start rx thread on %d...\n", lcore_id);
2497 RTE_PER_LCORE(lcore_conf) = &lcore_conf[lcore_id];
2498 RTE_PER_LCORE(lcore_conf)->data = (void *)&rx_thread[i];
2499 pthread_rx((void *)&rx_thread[i]);
2503 for (i = 0; i < n_tx_thread; i++)
2504 if (tx_thread[i].conf.lcore_id == lcore_id) {
2505 printf("Start tx thread on %d...\n", lcore_id);
2506 RTE_PER_LCORE(lcore_conf) = &lcore_conf[lcore_id];
2507 RTE_PER_LCORE(lcore_conf)->data = (void *)&tx_thread[i];
2508 pthread_tx((void *)&tx_thread[i]);
2513 if (lcore_id == cpu_load_lcore_id)
2514 cpu_load_collector(arg);
2515 #endif /* APP_CPU_LOAD */
2521 check_lcore_params(void)
2523 uint8_t queue, lcore;
2527 for (i = 0; i < nb_rx_thread_params; ++i) {
2528 queue = rx_thread_params[i].queue_id;
2529 if (queue >= MAX_RX_QUEUE_PER_PORT) {
2530 printf("invalid queue number: %hhu\n", queue);
2533 lcore = rx_thread_params[i].lcore_id;
2534 if (!rte_lcore_is_enabled(lcore)) {
2535 printf("error: lcore %hhu is not enabled in lcore mask\n", lcore);
2538 socketid = rte_lcore_to_socket_id(lcore);
2539 if ((socketid != 0) && (numa_on == 0))
2540 printf("warning: lcore %hhu is on socket %d with numa off\n",
2547 check_port_config(const unsigned nb_ports)
2552 for (i = 0; i < nb_rx_thread_params; ++i) {
2553 portid = rx_thread_params[i].port_id;
2554 if ((enabled_port_mask & (1 << portid)) == 0) {
2555 printf("port %u is not enabled in port mask\n", portid);
2558 if (portid >= nb_ports) {
2559 printf("port %u is not present on the board\n", portid);
2567 get_port_n_rx_queues(const uint8_t port)
2572 for (i = 0; i < nb_rx_thread_params; ++i)
2573 if (rx_thread_params[i].port_id == port &&
2574 rx_thread_params[i].queue_id > queue)
2575 queue = rx_thread_params[i].queue_id;
2577 return (uint8_t)(++queue);
2584 struct thread_rx_conf *rx_conf;
2585 struct thread_tx_conf *tx_conf;
2586 unsigned rx_thread_id, tx_thread_id;
2588 struct rte_ring *ring = NULL;
2590 for (tx_thread_id = 0; tx_thread_id < n_tx_thread; tx_thread_id++) {
2592 tx_conf = &tx_thread[tx_thread_id];
2594 printf("Connecting tx-thread %d with rx-thread %d\n", tx_thread_id,
2595 tx_conf->conf.thread_id);
2597 rx_thread_id = tx_conf->conf.thread_id;
2598 if (rx_thread_id > n_tx_thread) {
2599 printf("connection from tx-thread %u to rx-thread %u fails "
2600 "(rx-thread not defined)\n", tx_thread_id, rx_thread_id);
2604 rx_conf = &rx_thread[rx_thread_id];
2605 socket_io = rte_lcore_to_socket_id(rx_conf->conf.lcore_id);
2607 snprintf(name, sizeof(name), "app_ring_s%u_rx%u_tx%u",
2608 socket_io, rx_thread_id, tx_thread_id);
2610 ring = rte_ring_create(name, 1024 * 4, socket_io,
2611 RING_F_SP_ENQ | RING_F_SC_DEQ);
2614 rte_panic("Cannot create ring to connect rx-thread %u "
2615 "with tx-thread %u\n", rx_thread_id, tx_thread_id);
2618 rx_conf->ring[rx_conf->n_ring] = ring;
2620 tx_conf->ring = ring;
2621 tx_conf->ready = &rx_conf->ready[rx_conf->n_ring];
2629 init_rx_queues(void)
2631 uint16_t i, nb_rx_queue;
2636 for (i = 0; i < nb_rx_thread_params; ++i) {
2637 thread = rx_thread_params[i].thread_id;
2638 nb_rx_queue = rx_thread[thread].n_rx_queue;
2640 if (nb_rx_queue >= MAX_RX_QUEUE_PER_LCORE) {
2641 printf("error: too many queues (%u) for thread: %u\n",
2642 (unsigned)nb_rx_queue + 1, (unsigned)thread);
2646 rx_thread[thread].conf.thread_id = thread;
2647 rx_thread[thread].conf.lcore_id = rx_thread_params[i].lcore_id;
2648 rx_thread[thread].rx_queue_list[nb_rx_queue].port_id =
2649 rx_thread_params[i].port_id;
2650 rx_thread[thread].rx_queue_list[nb_rx_queue].queue_id =
2651 rx_thread_params[i].queue_id;
2652 rx_thread[thread].n_rx_queue++;
2654 if (thread >= n_rx_thread)
2655 n_rx_thread = thread + 1;
2662 init_tx_threads(void)
2667 for (i = 0; i < nb_tx_thread_params; ++i) {
2668 tx_thread[n_tx_thread].conf.thread_id = tx_thread_params[i].thread_id;
2669 tx_thread[n_tx_thread].conf.lcore_id = tx_thread_params[i].lcore_id;
2677 print_usage(const char *prgname)
2679 printf("%s [EAL options] -- -p PORTMASK -P"
2680 " [--rx (port,queue,lcore,thread)[,(port,queue,lcore,thread]]"
2681 " [--tx (lcore,thread)[,(lcore,thread]]"
2682 " [--enable-jumbo [--max-pkt-len PKTLEN]]\n"
2683 " [--parse-ptype]\n\n"
2684 " -p PORTMASK: hexadecimal bitmask of ports to configure\n"
2685 " -P : enable promiscuous mode\n"
2686 " --rx (port,queue,lcore,thread): rx queues configuration\n"
2687 " --tx (lcore,thread): tx threads configuration\n"
2688 " --stat-lcore LCORE: use lcore for stat collector\n"
2689 " --eth-dest=X,MM:MM:MM:MM:MM:MM: optional, ethernet destination for port X\n"
2690 " --no-numa: optional, disable numa awareness\n"
2691 " --ipv6: optional, specify it if running ipv6 packets\n"
2692 " --enable-jumbo: enable jumbo frame"
2693 " which max packet len is PKTLEN in decimal (64-9600)\n"
2694 " --hash-entry-num: specify the hash entry number in hexadecimal to be setup\n"
2695 " --no-lthreads: turn off lthread model\n"
2696 " --parse-ptype: set to use software to analyze packet type\n\n",
2700 static int parse_max_pkt_len(const char *pktlen)
2705 /* parse decimal string */
2706 len = strtoul(pktlen, &end, 10);
2707 if ((pktlen[0] == '\0') || (end == NULL) || (*end != '\0'))
2717 parse_portmask(const char *portmask)
2722 /* parse hexadecimal string */
2723 pm = strtoul(portmask, &end, 16);
2724 if ((portmask[0] == '\0') || (end == NULL) || (*end != '\0'))
2733 #if (APP_LOOKUP_METHOD == APP_LOOKUP_EXACT_MATCH)
2735 parse_hash_entry_number(const char *hash_entry_num)
2738 unsigned long hash_en;
2740 /* parse hexadecimal string */
2741 hash_en = strtoul(hash_entry_num, &end, 16);
2742 if ((hash_entry_num[0] == '\0') || (end == NULL) || (*end != '\0'))
2753 parse_rx_config(const char *q_arg)
2756 const char *p, *p0 = q_arg;
2765 unsigned long int_fld[_NUM_FLD];
2766 char *str_fld[_NUM_FLD];
2770 nb_rx_thread_params = 0;
2772 while ((p = strchr(p0, '(')) != NULL) {
2774 p0 = strchr(p, ')');
2779 if (size >= sizeof(s))
2782 snprintf(s, sizeof(s), "%.*s", size, p);
2783 if (rte_strsplit(s, sizeof(s), str_fld, _NUM_FLD, ',') != _NUM_FLD)
2785 for (i = 0; i < _NUM_FLD; i++) {
2787 int_fld[i] = strtoul(str_fld[i], &end, 0);
2788 if (errno != 0 || end == str_fld[i] || int_fld[i] > 255)
2791 if (nb_rx_thread_params >= MAX_LCORE_PARAMS) {
2792 printf("exceeded max number of rx params: %hu\n",
2793 nb_rx_thread_params);
2796 rx_thread_params_array[nb_rx_thread_params].port_id =
2797 (uint8_t)int_fld[FLD_PORT];
2798 rx_thread_params_array[nb_rx_thread_params].queue_id =
2799 (uint8_t)int_fld[FLD_QUEUE];
2800 rx_thread_params_array[nb_rx_thread_params].lcore_id =
2801 (uint8_t)int_fld[FLD_LCORE];
2802 rx_thread_params_array[nb_rx_thread_params].thread_id =
2803 (uint8_t)int_fld[FLD_THREAD];
2804 ++nb_rx_thread_params;
2806 rx_thread_params = rx_thread_params_array;
2811 parse_tx_config(const char *q_arg)
2814 const char *p, *p0 = q_arg;
2821 unsigned long int_fld[_NUM_FLD];
2822 char *str_fld[_NUM_FLD];
2826 nb_tx_thread_params = 0;
2828 while ((p = strchr(p0, '(')) != NULL) {
2830 p0 = strchr(p, ')');
2835 if (size >= sizeof(s))
2838 snprintf(s, sizeof(s), "%.*s", size, p);
2839 if (rte_strsplit(s, sizeof(s), str_fld, _NUM_FLD, ',') != _NUM_FLD)
2841 for (i = 0; i < _NUM_FLD; i++) {
2843 int_fld[i] = strtoul(str_fld[i], &end, 0);
2844 if (errno != 0 || end == str_fld[i] || int_fld[i] > 255)
2847 if (nb_tx_thread_params >= MAX_LCORE_PARAMS) {
2848 printf("exceeded max number of tx params: %hu\n",
2849 nb_tx_thread_params);
2852 tx_thread_params_array[nb_tx_thread_params].lcore_id =
2853 (uint8_t)int_fld[FLD_LCORE];
2854 tx_thread_params_array[nb_tx_thread_params].thread_id =
2855 (uint8_t)int_fld[FLD_THREAD];
2856 ++nb_tx_thread_params;
2858 tx_thread_params = tx_thread_params_array;
2863 #if (APP_CPU_LOAD > 0)
2865 parse_stat_lcore(const char *stat_lcore)
2868 unsigned long lcore_id;
2870 lcore_id = strtoul(stat_lcore, &end, 10);
2871 if ((stat_lcore[0] == '\0') || (end == NULL) || (*end != '\0'))
2879 parse_eth_dest(const char *optarg)
2883 uint8_t c, *dest, peer_addr[6];
2886 portid = strtoul(optarg, &port_end, 10);
2887 if (errno != 0 || port_end == optarg || *port_end++ != ',')
2888 rte_exit(EXIT_FAILURE,
2889 "Invalid eth-dest: %s", optarg);
2890 if (portid >= RTE_MAX_ETHPORTS)
2891 rte_exit(EXIT_FAILURE,
2892 "eth-dest: port %d >= RTE_MAX_ETHPORTS(%d)\n",
2893 portid, RTE_MAX_ETHPORTS);
2895 if (cmdline_parse_etheraddr(NULL, port_end,
2896 &peer_addr, sizeof(peer_addr)) < 0)
2897 rte_exit(EXIT_FAILURE,
2898 "Invalid ethernet address: %s\n",
2900 dest = (uint8_t *)&dest_eth_addr[portid];
2901 for (c = 0; c < 6; c++)
2902 dest[c] = peer_addr[c];
2903 *(uint64_t *)(val_eth + portid) = dest_eth_addr[portid];
2906 #define CMD_LINE_OPT_RX_CONFIG "rx"
2907 #define CMD_LINE_OPT_TX_CONFIG "tx"
2908 #define CMD_LINE_OPT_STAT_LCORE "stat-lcore"
2909 #define CMD_LINE_OPT_ETH_DEST "eth-dest"
2910 #define CMD_LINE_OPT_NO_NUMA "no-numa"
2911 #define CMD_LINE_OPT_IPV6 "ipv6"
2912 #define CMD_LINE_OPT_ENABLE_JUMBO "enable-jumbo"
2913 #define CMD_LINE_OPT_HASH_ENTRY_NUM "hash-entry-num"
2914 #define CMD_LINE_OPT_NO_LTHREADS "no-lthreads"
2915 #define CMD_LINE_OPT_PARSE_PTYPE "parse-ptype"
2917 /* Parse the argument given in the command line of the application */
2919 parse_args(int argc, char **argv)
2924 char *prgname = argv[0];
2925 static struct option lgopts[] = {
2926 {CMD_LINE_OPT_RX_CONFIG, 1, 0, 0},
2927 {CMD_LINE_OPT_TX_CONFIG, 1, 0, 0},
2928 {CMD_LINE_OPT_STAT_LCORE, 1, 0, 0},
2929 {CMD_LINE_OPT_ETH_DEST, 1, 0, 0},
2930 {CMD_LINE_OPT_NO_NUMA, 0, 0, 0},
2931 {CMD_LINE_OPT_IPV6, 0, 0, 0},
2932 {CMD_LINE_OPT_ENABLE_JUMBO, 0, 0, 0},
2933 {CMD_LINE_OPT_HASH_ENTRY_NUM, 1, 0, 0},
2934 {CMD_LINE_OPT_NO_LTHREADS, 0, 0, 0},
2935 {CMD_LINE_OPT_PARSE_PTYPE, 0, 0, 0},
2941 while ((opt = getopt_long(argc, argvopt, "p:P",
2942 lgopts, &option_index)) != EOF) {
2947 enabled_port_mask = parse_portmask(optarg);
2948 if (enabled_port_mask == 0) {
2949 printf("invalid portmask\n");
2950 print_usage(prgname);
2955 printf("Promiscuous mode selected\n");
2961 if (!strncmp(lgopts[option_index].name, CMD_LINE_OPT_RX_CONFIG,
2962 sizeof(CMD_LINE_OPT_RX_CONFIG))) {
2963 ret = parse_rx_config(optarg);
2965 printf("invalid rx-config\n");
2966 print_usage(prgname);
2971 if (!strncmp(lgopts[option_index].name, CMD_LINE_OPT_TX_CONFIG,
2972 sizeof(CMD_LINE_OPT_TX_CONFIG))) {
2973 ret = parse_tx_config(optarg);
2975 printf("invalid tx-config\n");
2976 print_usage(prgname);
2981 #if (APP_CPU_LOAD > 0)
2982 if (!strncmp(lgopts[option_index].name, CMD_LINE_OPT_STAT_LCORE,
2983 sizeof(CMD_LINE_OPT_STAT_LCORE))) {
2984 cpu_load_lcore_id = parse_stat_lcore(optarg);
2988 if (!strncmp(lgopts[option_index].name, CMD_LINE_OPT_ETH_DEST,
2989 sizeof(CMD_LINE_OPT_ETH_DEST)))
2990 parse_eth_dest(optarg);
2992 if (!strncmp(lgopts[option_index].name, CMD_LINE_OPT_NO_NUMA,
2993 sizeof(CMD_LINE_OPT_NO_NUMA))) {
2994 printf("numa is disabled\n");
2998 #if (APP_LOOKUP_METHOD == APP_LOOKUP_EXACT_MATCH)
2999 if (!strncmp(lgopts[option_index].name, CMD_LINE_OPT_IPV6,
3000 sizeof(CMD_LINE_OPT_IPV6))) {
3001 printf("ipv6 is specified\n");
3006 if (!strncmp(lgopts[option_index].name, CMD_LINE_OPT_NO_LTHREADS,
3007 sizeof(CMD_LINE_OPT_NO_LTHREADS))) {
3008 printf("l-threads model is disabled\n");
3012 if (!strncmp(lgopts[option_index].name, CMD_LINE_OPT_PARSE_PTYPE,
3013 sizeof(CMD_LINE_OPT_PARSE_PTYPE))) {
3014 printf("software packet type parsing enabled\n");
3018 if (!strncmp(lgopts[option_index].name, CMD_LINE_OPT_ENABLE_JUMBO,
3019 sizeof(CMD_LINE_OPT_ENABLE_JUMBO))) {
3020 struct option lenopts = {"max-pkt-len", required_argument, 0,
3023 printf("jumbo frame is enabled - disabling simple TX path\n");
3024 port_conf.rxmode.jumbo_frame = 1;
3026 /* if no max-pkt-len set, use the default value ETHER_MAX_LEN */
3027 if (0 == getopt_long(argc, argvopt, "", &lenopts,
3030 ret = parse_max_pkt_len(optarg);
3031 if ((ret < 64) || (ret > MAX_JUMBO_PKT_LEN)) {
3032 printf("invalid packet length\n");
3033 print_usage(prgname);
3036 port_conf.rxmode.max_rx_pkt_len = ret;
3038 printf("set jumbo frame max packet length to %u\n",
3039 (unsigned int)port_conf.rxmode.max_rx_pkt_len);
3041 #if (APP_LOOKUP_METHOD == APP_LOOKUP_EXACT_MATCH)
3042 if (!strncmp(lgopts[option_index].name, CMD_LINE_OPT_HASH_ENTRY_NUM,
3043 sizeof(CMD_LINE_OPT_HASH_ENTRY_NUM))) {
3044 ret = parse_hash_entry_number(optarg);
3045 if ((ret > 0) && (ret <= L3FWD_HASH_ENTRIES)) {
3046 hash_entry_number = ret;
3048 printf("invalid hash entry number\n");
3049 print_usage(prgname);
3057 print_usage(prgname);
3063 argv[optind-1] = prgname;
3066 optind = 1; /* reset getopt lib */
3071 print_ethaddr(const char *name, const struct ether_addr *eth_addr)
3073 char buf[ETHER_ADDR_FMT_SIZE];
3075 ether_format_addr(buf, ETHER_ADDR_FMT_SIZE, eth_addr);
3076 printf("%s%s", name, buf);
3079 #if (APP_LOOKUP_METHOD == APP_LOOKUP_EXACT_MATCH)
3081 static void convert_ipv4_5tuple(struct ipv4_5tuple *key1,
3082 union ipv4_5tuple_host *key2)
3084 key2->ip_dst = rte_cpu_to_be_32(key1->ip_dst);
3085 key2->ip_src = rte_cpu_to_be_32(key1->ip_src);
3086 key2->port_dst = rte_cpu_to_be_16(key1->port_dst);
3087 key2->port_src = rte_cpu_to_be_16(key1->port_src);
3088 key2->proto = key1->proto;
3093 static void convert_ipv6_5tuple(struct ipv6_5tuple *key1,
3094 union ipv6_5tuple_host *key2)
3098 for (i = 0; i < 16; i++) {
3099 key2->ip_dst[i] = key1->ip_dst[i];
3100 key2->ip_src[i] = key1->ip_src[i];
3102 key2->port_dst = rte_cpu_to_be_16(key1->port_dst);
3103 key2->port_src = rte_cpu_to_be_16(key1->port_src);
3104 key2->proto = key1->proto;
3110 #define BYTE_VALUE_MAX 256
3111 #define ALL_32_BITS 0xffffffff
3112 #define BIT_8_TO_15 0x0000ff00
3114 populate_ipv4_few_flow_into_table(const struct rte_hash *h)
3118 uint32_t array_len = RTE_DIM(ipv4_l3fwd_route_array);
3120 mask0 = _mm_set_epi32(ALL_32_BITS, ALL_32_BITS, ALL_32_BITS, BIT_8_TO_15);
3121 for (i = 0; i < array_len; i++) {
3122 struct ipv4_l3fwd_route entry;
3123 union ipv4_5tuple_host newkey;
3125 entry = ipv4_l3fwd_route_array[i];
3126 convert_ipv4_5tuple(&entry.key, &newkey);
3127 ret = rte_hash_add_key(h, (void *)&newkey);
3129 rte_exit(EXIT_FAILURE, "Unable to add entry %" PRIu32
3130 " to the l3fwd hash.\n", i);
3132 ipv4_l3fwd_out_if[ret] = entry.if_out;
3134 printf("Hash: Adding 0x%" PRIx32 " keys\n", array_len);
3137 #define BIT_16_TO_23 0x00ff0000
3139 populate_ipv6_few_flow_into_table(const struct rte_hash *h)
3143 uint32_t array_len = RTE_DIM(ipv6_l3fwd_route_array);
3145 mask1 = _mm_set_epi32(ALL_32_BITS, ALL_32_BITS, ALL_32_BITS, BIT_16_TO_23);
3146 mask2 = _mm_set_epi32(0, 0, ALL_32_BITS, ALL_32_BITS);
3147 for (i = 0; i < array_len; i++) {
3148 struct ipv6_l3fwd_route entry;
3149 union ipv6_5tuple_host newkey;
3151 entry = ipv6_l3fwd_route_array[i];
3152 convert_ipv6_5tuple(&entry.key, &newkey);
3153 ret = rte_hash_add_key(h, (void *)&newkey);
3155 rte_exit(EXIT_FAILURE, "Unable to add entry %" PRIu32
3156 " to the l3fwd hash.\n", i);
3158 ipv6_l3fwd_out_if[ret] = entry.if_out;
3160 printf("Hash: Adding 0x%" PRIx32 "keys\n", array_len);
3163 #define NUMBER_PORT_USED 4
3165 populate_ipv4_many_flow_into_table(const struct rte_hash *h,
3166 unsigned int nr_flow)
3170 mask0 = _mm_set_epi32(ALL_32_BITS, ALL_32_BITS, ALL_32_BITS, BIT_8_TO_15);
3172 for (i = 0; i < nr_flow; i++) {
3173 struct ipv4_l3fwd_route entry;
3174 union ipv4_5tuple_host newkey;
3175 uint8_t a = (uint8_t)((i / NUMBER_PORT_USED) % BYTE_VALUE_MAX);
3176 uint8_t b = (uint8_t)(((i / NUMBER_PORT_USED) / BYTE_VALUE_MAX) %
3178 uint8_t c = (uint8_t)((i / NUMBER_PORT_USED) / (BYTE_VALUE_MAX *
3180 /* Create the ipv4 exact match flow */
3181 memset(&entry, 0, sizeof(entry));
3182 switch (i & (NUMBER_PORT_USED - 1)) {
3184 entry = ipv4_l3fwd_route_array[0];
3185 entry.key.ip_dst = IPv4(101, c, b, a);
3188 entry = ipv4_l3fwd_route_array[1];
3189 entry.key.ip_dst = IPv4(201, c, b, a);
3192 entry = ipv4_l3fwd_route_array[2];
3193 entry.key.ip_dst = IPv4(111, c, b, a);
3196 entry = ipv4_l3fwd_route_array[3];
3197 entry.key.ip_dst = IPv4(211, c, b, a);
3200 convert_ipv4_5tuple(&entry.key, &newkey);
3201 int32_t ret = rte_hash_add_key(h, (void *)&newkey);
3204 rte_exit(EXIT_FAILURE, "Unable to add entry %u\n", i);
3206 ipv4_l3fwd_out_if[ret] = (uint8_t)entry.if_out;
3209 printf("Hash: Adding 0x%x keys\n", nr_flow);
3213 populate_ipv6_many_flow_into_table(const struct rte_hash *h,
3214 unsigned int nr_flow)
3218 mask1 = _mm_set_epi32(ALL_32_BITS, ALL_32_BITS, ALL_32_BITS, BIT_16_TO_23);
3219 mask2 = _mm_set_epi32(0, 0, ALL_32_BITS, ALL_32_BITS);
3220 for (i = 0; i < nr_flow; i++) {
3221 struct ipv6_l3fwd_route entry;
3222 union ipv6_5tuple_host newkey;
3224 uint8_t a = (uint8_t) ((i / NUMBER_PORT_USED) % BYTE_VALUE_MAX);
3225 uint8_t b = (uint8_t) (((i / NUMBER_PORT_USED) / BYTE_VALUE_MAX) %
3227 uint8_t c = (uint8_t) ((i / NUMBER_PORT_USED) / (BYTE_VALUE_MAX *
3230 /* Create the ipv6 exact match flow */
3231 memset(&entry, 0, sizeof(entry));
3232 switch (i & (NUMBER_PORT_USED - 1)) {
3234 entry = ipv6_l3fwd_route_array[0];
3237 entry = ipv6_l3fwd_route_array[1];
3240 entry = ipv6_l3fwd_route_array[2];
3243 entry = ipv6_l3fwd_route_array[3];
3246 entry.key.ip_dst[13] = c;
3247 entry.key.ip_dst[14] = b;
3248 entry.key.ip_dst[15] = a;
3249 convert_ipv6_5tuple(&entry.key, &newkey);
3250 int32_t ret = rte_hash_add_key(h, (void *)&newkey);
3253 rte_exit(EXIT_FAILURE, "Unable to add entry %u\n", i);
3255 ipv6_l3fwd_out_if[ret] = (uint8_t) entry.if_out;
3258 printf("Hash: Adding 0x%x keys\n", nr_flow);
3262 setup_hash(int socketid)
3264 struct rte_hash_parameters ipv4_l3fwd_hash_params = {
3266 .entries = L3FWD_HASH_ENTRIES,
3267 .key_len = sizeof(union ipv4_5tuple_host),
3268 .hash_func = ipv4_hash_crc,
3269 .hash_func_init_val = 0,
3272 struct rte_hash_parameters ipv6_l3fwd_hash_params = {
3274 .entries = L3FWD_HASH_ENTRIES,
3275 .key_len = sizeof(union ipv6_5tuple_host),
3276 .hash_func = ipv6_hash_crc,
3277 .hash_func_init_val = 0,
3282 /* create ipv4 hash */
3283 snprintf(s, sizeof(s), "ipv4_l3fwd_hash_%d", socketid);
3284 ipv4_l3fwd_hash_params.name = s;
3285 ipv4_l3fwd_hash_params.socket_id = socketid;
3286 ipv4_l3fwd_lookup_struct[socketid] =
3287 rte_hash_create(&ipv4_l3fwd_hash_params);
3288 if (ipv4_l3fwd_lookup_struct[socketid] == NULL)
3289 rte_exit(EXIT_FAILURE, "Unable to create the l3fwd hash on "
3290 "socket %d\n", socketid);
3292 /* create ipv6 hash */
3293 snprintf(s, sizeof(s), "ipv6_l3fwd_hash_%d", socketid);
3294 ipv6_l3fwd_hash_params.name = s;
3295 ipv6_l3fwd_hash_params.socket_id = socketid;
3296 ipv6_l3fwd_lookup_struct[socketid] =
3297 rte_hash_create(&ipv6_l3fwd_hash_params);
3298 if (ipv6_l3fwd_lookup_struct[socketid] == NULL)
3299 rte_exit(EXIT_FAILURE, "Unable to create the l3fwd hash on "
3300 "socket %d\n", socketid);
3302 if (hash_entry_number != HASH_ENTRY_NUMBER_DEFAULT) {
3303 /* For testing hash matching with a large number of flows we
3304 * generate millions of IP 5-tuples with an incremented dst
3305 * address to initialize the hash table. */
3307 /* populate the ipv4 hash */
3308 populate_ipv4_many_flow_into_table(
3309 ipv4_l3fwd_lookup_struct[socketid], hash_entry_number);
3311 /* populate the ipv6 hash */
3312 populate_ipv6_many_flow_into_table(
3313 ipv6_l3fwd_lookup_struct[socketid], hash_entry_number);
3316 /* Use data in ipv4/ipv6 l3fwd lookup table directly to initialize
3319 /* populate the ipv4 hash */
3320 populate_ipv4_few_flow_into_table(
3321 ipv4_l3fwd_lookup_struct[socketid]);
3323 /* populate the ipv6 hash */
3324 populate_ipv6_few_flow_into_table(
3325 ipv6_l3fwd_lookup_struct[socketid]);
3331 #if (APP_LOOKUP_METHOD == APP_LOOKUP_LPM)
3333 setup_lpm(int socketid)
3335 struct rte_lpm6_config config;
3336 struct rte_lpm_config lpm_ipv4_config;
3341 /* create the LPM table */
3342 snprintf(s, sizeof(s), "IPV4_L3FWD_LPM_%d", socketid);
3343 lpm_ipv4_config.max_rules = IPV4_L3FWD_LPM_MAX_RULES;
3344 lpm_ipv4_config.number_tbl8s = 256;
3345 lpm_ipv4_config.flags = 0;
3346 ipv4_l3fwd_lookup_struct[socketid] =
3347 rte_lpm_create(s, socketid, &lpm_ipv4_config);
3348 if (ipv4_l3fwd_lookup_struct[socketid] == NULL)
3349 rte_exit(EXIT_FAILURE, "Unable to create the l3fwd LPM table"
3350 " on socket %d\n", socketid);
3352 /* populate the LPM table */
3353 for (i = 0; i < IPV4_L3FWD_NUM_ROUTES; i++) {
3355 /* skip unused ports */
3356 if ((1 << ipv4_l3fwd_route_array[i].if_out &
3357 enabled_port_mask) == 0)
3360 ret = rte_lpm_add(ipv4_l3fwd_lookup_struct[socketid],
3361 ipv4_l3fwd_route_array[i].ip,
3362 ipv4_l3fwd_route_array[i].depth,
3363 ipv4_l3fwd_route_array[i].if_out);
3366 rte_exit(EXIT_FAILURE, "Unable to add entry %u to the "
3367 "l3fwd LPM table on socket %d\n",
3371 printf("LPM: Adding route 0x%08x / %d (%d)\n",
3372 (unsigned)ipv4_l3fwd_route_array[i].ip,
3373 ipv4_l3fwd_route_array[i].depth,
3374 ipv4_l3fwd_route_array[i].if_out);
3377 /* create the LPM6 table */
3378 snprintf(s, sizeof(s), "IPV6_L3FWD_LPM_%d", socketid);
3380 config.max_rules = IPV6_L3FWD_LPM_MAX_RULES;
3381 config.number_tbl8s = IPV6_L3FWD_LPM_NUMBER_TBL8S;
3383 ipv6_l3fwd_lookup_struct[socketid] = rte_lpm6_create(s, socketid,
3385 if (ipv6_l3fwd_lookup_struct[socketid] == NULL)
3386 rte_exit(EXIT_FAILURE, "Unable to create the l3fwd LPM table"
3387 " on socket %d\n", socketid);
3389 /* populate the LPM table */
3390 for (i = 0; i < IPV6_L3FWD_NUM_ROUTES; i++) {
3392 /* skip unused ports */
3393 if ((1 << ipv6_l3fwd_route_array[i].if_out &
3394 enabled_port_mask) == 0)
3397 ret = rte_lpm6_add(ipv6_l3fwd_lookup_struct[socketid],
3398 ipv6_l3fwd_route_array[i].ip,
3399 ipv6_l3fwd_route_array[i].depth,
3400 ipv6_l3fwd_route_array[i].if_out);
3403 rte_exit(EXIT_FAILURE, "Unable to add entry %u to the "
3404 "l3fwd LPM table on socket %d\n",
3408 printf("LPM: Adding route %s / %d (%d)\n",
3410 ipv6_l3fwd_route_array[i].depth,
3411 ipv6_l3fwd_route_array[i].if_out);
3417 init_mem(unsigned nb_mbuf)
3419 struct lcore_conf *qconf;
3424 for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
3425 if (rte_lcore_is_enabled(lcore_id) == 0)
3429 socketid = rte_lcore_to_socket_id(lcore_id);
3433 if (socketid >= NB_SOCKETS) {
3434 rte_exit(EXIT_FAILURE, "Socket %d of lcore %u is out of range %d\n",
3435 socketid, lcore_id, NB_SOCKETS);
3437 if (pktmbuf_pool[socketid] == NULL) {
3438 snprintf(s, sizeof(s), "mbuf_pool_%d", socketid);
3439 pktmbuf_pool[socketid] =
3440 rte_pktmbuf_pool_create(s, nb_mbuf,
3441 MEMPOOL_CACHE_SIZE, 0,
3442 RTE_MBUF_DEFAULT_BUF_SIZE, socketid);
3443 if (pktmbuf_pool[socketid] == NULL)
3444 rte_exit(EXIT_FAILURE,
3445 "Cannot init mbuf pool on socket %d\n", socketid);
3447 printf("Allocated mbuf pool on socket %d\n", socketid);
3449 #if (APP_LOOKUP_METHOD == APP_LOOKUP_LPM)
3450 setup_lpm(socketid);
3452 setup_hash(socketid);
3455 qconf = &lcore_conf[lcore_id];
3456 qconf->ipv4_lookup_struct = ipv4_l3fwd_lookup_struct[socketid];
3457 qconf->ipv6_lookup_struct = ipv6_l3fwd_lookup_struct[socketid];
3462 /* Check the link status of all ports in up to 9s, and print them finally */
3464 check_all_ports_link_status(uint8_t port_num, uint32_t port_mask)
3466 #define CHECK_INTERVAL 100 /* 100ms */
3467 #define MAX_CHECK_TIME 90 /* 9s (90 * 100ms) in total */
3468 uint8_t portid, count, all_ports_up, print_flag = 0;
3469 struct rte_eth_link link;
3471 printf("\nChecking link status");
3473 for (count = 0; count <= MAX_CHECK_TIME; count++) {
3475 for (portid = 0; portid < port_num; portid++) {
3476 if ((port_mask & (1 << portid)) == 0)
3478 memset(&link, 0, sizeof(link));
3479 rte_eth_link_get_nowait(portid, &link);
3480 /* print link status if flag set */
3481 if (print_flag == 1) {
3482 if (link.link_status)
3483 printf("Port %d Link Up - speed %u "
3484 "Mbps - %s\n", (uint8_t)portid,
3485 (unsigned)link.link_speed,
3486 (link.link_duplex == ETH_LINK_FULL_DUPLEX) ?
3487 ("full-duplex") : ("half-duplex\n"));
3489 printf("Port %d Link Down\n",
3493 /* clear all_ports_up flag if any link down */
3494 if (link.link_status == ETH_LINK_DOWN) {
3499 /* after finally printing all link status, get out */
3500 if (print_flag == 1)
3503 if (all_ports_up == 0) {
3506 rte_delay_ms(CHECK_INTERVAL);
3509 /* set the print_flag if all ports up or timeout */
3510 if (all_ports_up == 1 || count == (MAX_CHECK_TIME - 1)) {
3518 main(int argc, char **argv)
3520 struct rte_eth_dev_info dev_info;
3521 struct rte_eth_txconf *txconf;
3527 uint32_t n_tx_queue, nb_lcores;
3528 uint8_t portid, nb_rx_queue, queue, socketid;
3531 ret = rte_eal_init(argc, argv);
3533 rte_exit(EXIT_FAILURE, "Invalid EAL parameters\n");
3537 /* pre-init dst MACs for all ports to 02:00:00:00:00:xx */
3538 for (portid = 0; portid < RTE_MAX_ETHPORTS; portid++) {
3539 dest_eth_addr[portid] = ETHER_LOCAL_ADMIN_ADDR +
3540 ((uint64_t)portid << 40);
3541 *(uint64_t *)(val_eth + portid) = dest_eth_addr[portid];
3544 /* parse application arguments (after the EAL ones) */
3545 ret = parse_args(argc, argv);
3547 rte_exit(EXIT_FAILURE, "Invalid L3FWD parameters\n");
3549 if (check_lcore_params() < 0)
3550 rte_exit(EXIT_FAILURE, "check_lcore_params failed\n");
3552 printf("Initializing rx-queues...\n");
3553 ret = init_rx_queues();
3555 rte_exit(EXIT_FAILURE, "init_rx_queues failed\n");
3557 printf("Initializing tx-threads...\n");
3558 ret = init_tx_threads();
3560 rte_exit(EXIT_FAILURE, "init_tx_threads failed\n");
3562 printf("Initializing rings...\n");
3563 ret = init_rx_rings();
3565 rte_exit(EXIT_FAILURE, "init_rx_rings failed\n");
3567 nb_ports = rte_eth_dev_count();
3569 if (check_port_config(nb_ports) < 0)
3570 rte_exit(EXIT_FAILURE, "check_port_config failed\n");
3572 nb_lcores = rte_lcore_count();
3574 /* initialize all ports */
3575 for (portid = 0; portid < nb_ports; portid++) {
3576 /* skip ports that are not enabled */
3577 if ((enabled_port_mask & (1 << portid)) == 0) {
3578 printf("\nSkipping disabled port %d\n", portid);
3583 printf("Initializing port %d ... ", portid);
3586 nb_rx_queue = get_port_n_rx_queues(portid);
3587 n_tx_queue = nb_lcores;
3588 if (n_tx_queue > MAX_TX_QUEUE_PER_PORT)
3589 n_tx_queue = MAX_TX_QUEUE_PER_PORT;
3590 printf("Creating queues: nb_rxq=%d nb_txq=%u... ",
3591 nb_rx_queue, (unsigned)n_tx_queue);
3592 ret = rte_eth_dev_configure(portid, nb_rx_queue,
3593 (uint16_t)n_tx_queue, &port_conf);
3595 rte_exit(EXIT_FAILURE, "Cannot configure device: err=%d, port=%d\n",
3598 rte_eth_macaddr_get(portid, &ports_eth_addr[portid]);
3599 print_ethaddr(" Address:", &ports_eth_addr[portid]);
3601 print_ethaddr("Destination:",
3602 (const struct ether_addr *)&dest_eth_addr[portid]);
3606 * prepare src MACs for each port.
3608 ether_addr_copy(&ports_eth_addr[portid],
3609 (struct ether_addr *)(val_eth + portid) + 1);
3612 ret = init_mem(NB_MBUF);
3614 rte_exit(EXIT_FAILURE, "init_mem failed\n");
3616 /* init one TX queue per couple (lcore,port) */
3618 for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
3619 if (rte_lcore_is_enabled(lcore_id) == 0)
3623 socketid = (uint8_t)rte_lcore_to_socket_id(lcore_id);
3627 printf("txq=%u,%d,%d ", lcore_id, queueid, socketid);
3630 rte_eth_dev_info_get(portid, &dev_info);
3631 txconf = &dev_info.default_txconf;
3632 if (port_conf.rxmode.jumbo_frame)
3633 txconf->txq_flags = 0;
3634 ret = rte_eth_tx_queue_setup(portid, queueid, nb_txd,
3637 rte_exit(EXIT_FAILURE, "rte_eth_tx_queue_setup: err=%d, "
3638 "port=%d\n", ret, portid);
3640 tx_thread[lcore_id].tx_queue_id[portid] = queueid;
3646 for (i = 0; i < n_rx_thread; i++) {
3647 lcore_id = rx_thread[i].conf.lcore_id;
3649 if (rte_lcore_is_enabled(lcore_id) == 0) {
3650 rte_exit(EXIT_FAILURE,
3651 "Cannot start Rx thread on lcore %u: lcore disabled\n",
3656 printf("\nInitializing rx queues for Rx thread %d on lcore %u ... ",
3660 /* init RX queues */
3661 for (queue = 0; queue < rx_thread[i].n_rx_queue; ++queue) {
3662 portid = rx_thread[i].rx_queue_list[queue].port_id;
3663 queueid = rx_thread[i].rx_queue_list[queue].queue_id;
3666 socketid = (uint8_t)rte_lcore_to_socket_id(lcore_id);
3670 printf("rxq=%d,%d,%d ", portid, queueid, socketid);
3673 ret = rte_eth_rx_queue_setup(portid, queueid, nb_rxd,
3676 pktmbuf_pool[socketid]);
3678 rte_exit(EXIT_FAILURE, "rte_eth_rx_queue_setup: err=%d, "
3679 "port=%d\n", ret, portid);
3686 for (portid = 0; portid < nb_ports; portid++) {
3687 if ((enabled_port_mask & (1 << portid)) == 0)
3691 ret = rte_eth_dev_start(portid);
3693 rte_exit(EXIT_FAILURE, "rte_eth_dev_start: err=%d, port=%d\n",
3697 * If enabled, put device in promiscuous mode.
3698 * This allows IO forwarding mode to forward packets
3699 * to itself through 2 cross-connected ports of the
3703 rte_eth_promiscuous_enable(portid);
3706 for (i = 0; i < n_rx_thread; i++) {
3707 lcore_id = rx_thread[i].conf.lcore_id;
3708 if (rte_lcore_is_enabled(lcore_id) == 0)
3711 /* check if hw packet type is supported */
3712 for (queue = 0; queue < rx_thread[i].n_rx_queue; ++queue) {
3713 portid = rx_thread[i].rx_queue_list[queue].port_id;
3714 queueid = rx_thread[i].rx_queue_list[queue].queue_id;
3716 if (parse_ptype_on) {
3717 if (!rte_eth_add_rx_callback(portid, queueid,
3718 cb_parse_ptype, NULL))
3719 rte_exit(EXIT_FAILURE,
3720 "Failed to add rx callback: "
3721 "port=%d\n", portid);
3722 } else if (!check_ptype(portid))
3723 rte_exit(EXIT_FAILURE,
3724 "Port %d cannot parse packet type.\n\n"
3725 "Please add --parse-ptype to use sw "
3726 "packet type analyzer.\n\n",
3731 check_all_ports_link_status((uint8_t)nb_ports, enabled_port_mask);
3734 printf("Starting L-Threading Model\n");
3736 #if (APP_CPU_LOAD > 0)
3737 if (cpu_load_lcore_id > 0)
3738 /* Use one lcore for cpu load collector */
3742 lthread_num_schedulers_set(nb_lcores);
3743 rte_eal_mp_remote_launch(sched_spawner, NULL, SKIP_MASTER);
3744 lthread_master_spawner(NULL);
3747 printf("Starting P-Threading Model\n");
3748 /* launch per-lcore init on every lcore */
3749 rte_eal_mp_remote_launch(pthread_run, NULL, CALL_MASTER);
3750 RTE_LCORE_FOREACH_SLAVE(lcore_id) {
3751 if (rte_eal_wait_lcore(lcore_id) < 0)