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>
76 #include <rte_pause.h>
78 #include <cmdline_parse.h>
79 #include <cmdline_parse_etheraddr.h>
81 #include <lthread_api.h>
83 #define APP_LOOKUP_EXACT_MATCH 0
84 #define APP_LOOKUP_LPM 1
85 #define DO_RFC_1812_CHECKS
87 /* Enable cpu-load stats 0-off, 1-on */
88 #define APP_CPU_LOAD 1
90 #ifndef APP_LOOKUP_METHOD
91 #define APP_LOOKUP_METHOD APP_LOOKUP_LPM
94 #ifndef __GLIBC__ /* sched_getcpu() is glibc specific */
95 #define sched_getcpu() rte_lcore_id()
99 check_ptype(int portid)
102 int ipv4 = 0, ipv6 = 0;
104 ret = rte_eth_dev_get_supported_ptypes(portid, RTE_PTYPE_L3_MASK, NULL,
109 uint32_t ptypes[ret];
111 ret = rte_eth_dev_get_supported_ptypes(portid, RTE_PTYPE_L3_MASK,
113 for (i = 0; i < ret; ++i) {
114 if (ptypes[i] & RTE_PTYPE_L3_IPV4)
116 if (ptypes[i] & RTE_PTYPE_L3_IPV6)
127 parse_ptype(struct rte_mbuf *m)
129 struct ether_hdr *eth_hdr;
130 uint32_t packet_type = RTE_PTYPE_UNKNOWN;
133 eth_hdr = rte_pktmbuf_mtod(m, struct ether_hdr *);
134 ether_type = eth_hdr->ether_type;
135 if (ether_type == rte_cpu_to_be_16(ETHER_TYPE_IPv4))
136 packet_type |= RTE_PTYPE_L3_IPV4_EXT_UNKNOWN;
137 else if (ether_type == rte_cpu_to_be_16(ETHER_TYPE_IPv6))
138 packet_type |= RTE_PTYPE_L3_IPV6_EXT_UNKNOWN;
140 m->packet_type = packet_type;
144 cb_parse_ptype(__rte_unused uint8_t port, __rte_unused uint16_t queue,
145 struct rte_mbuf *pkts[], uint16_t nb_pkts,
146 __rte_unused uint16_t max_pkts, __rte_unused void *user_param)
150 for (i = 0; i < nb_pkts; i++)
151 parse_ptype(pkts[i]);
157 * When set to zero, simple forwaring path is eanbled.
158 * When set to one, optimized forwarding path is enabled.
159 * Note that LPM optimisation path uses SSE4.1 instructions.
161 #if ((APP_LOOKUP_METHOD == APP_LOOKUP_LPM) && !defined(__SSE4_1__))
162 #define ENABLE_MULTI_BUFFER_OPTIMIZE 0
164 #define ENABLE_MULTI_BUFFER_OPTIMIZE 1
167 #if (APP_LOOKUP_METHOD == APP_LOOKUP_EXACT_MATCH)
168 #include <rte_hash.h>
169 #elif (APP_LOOKUP_METHOD == APP_LOOKUP_LPM)
171 #include <rte_lpm6.h>
173 #error "APP_LOOKUP_METHOD set to incorrect value"
176 #define RTE_LOGTYPE_L3FWD RTE_LOGTYPE_USER1
178 #define MAX_JUMBO_PKT_LEN 9600
180 #define IPV6_ADDR_LEN 16
182 #define MEMPOOL_CACHE_SIZE 256
185 * This expression is used to calculate the number of mbufs needed depending on
186 * user input, taking into account memory for rx and tx hardware rings, cache
187 * per lcore and mtable per port per lcore. RTE_MAX is used to ensure that
188 * NB_MBUF never goes below a minimum value of 8192
191 #define NB_MBUF RTE_MAX(\
192 (nb_ports*nb_rx_queue*RTE_TEST_RX_DESC_DEFAULT + \
193 nb_ports*nb_lcores*MAX_PKT_BURST + \
194 nb_ports*n_tx_queue*RTE_TEST_TX_DESC_DEFAULT + \
195 nb_lcores*MEMPOOL_CACHE_SIZE), \
198 #define MAX_PKT_BURST 32
199 #define BURST_TX_DRAIN_US 100 /* TX drain every ~100us */
202 * Try to avoid TX buffering if we have at least MAX_TX_BURST packets to send.
204 #define MAX_TX_BURST (MAX_PKT_BURST / 2)
205 #define BURST_SIZE MAX_TX_BURST
209 /* Configure how many packets ahead to prefetch, when reading packets */
210 #define PREFETCH_OFFSET 3
212 /* Used to mark destination port as 'invalid'. */
213 #define BAD_PORT ((uint16_t)-1)
218 * Configurable number of RX/TX ring descriptors
220 #define RTE_TEST_RX_DESC_DEFAULT 128
221 #define RTE_TEST_TX_DESC_DEFAULT 128
222 static uint16_t nb_rxd = RTE_TEST_RX_DESC_DEFAULT;
223 static uint16_t nb_txd = RTE_TEST_TX_DESC_DEFAULT;
225 /* ethernet addresses of ports */
226 static uint64_t dest_eth_addr[RTE_MAX_ETHPORTS];
227 static struct ether_addr ports_eth_addr[RTE_MAX_ETHPORTS];
229 static __m128i val_eth[RTE_MAX_ETHPORTS];
231 /* replace first 12B of the ethernet header. */
232 #define MASK_ETH 0x3f
234 /* mask of enabled ports */
235 static uint32_t enabled_port_mask;
236 static int promiscuous_on; /**< Set in promiscuous mode off by default. */
237 static int numa_on = 1; /**< NUMA is enabled by default. */
238 static int parse_ptype_on;
240 #if (APP_LOOKUP_METHOD == APP_LOOKUP_EXACT_MATCH)
241 static int ipv6; /**< ipv6 is false by default. */
244 #if (APP_CPU_LOAD == 1)
246 #define MAX_CPU RTE_MAX_LCORE
247 #define CPU_LOAD_TIMEOUT_US (5 * 1000 * 1000) /**< Timeout for collecting 5s */
249 #define CPU_PROCESS 0
251 #define MAX_CPU_COUNTER 2
256 uint64_t hits[MAX_CPU_COUNTER][MAX_CPU];
257 } __rte_cache_aligned;
259 static struct cpu_load cpu_load;
260 static int cpu_load_lcore_id = -1;
262 #define SET_CPU_BUSY(thread, counter) \
263 thread->conf.busy[counter] = 1
265 #define SET_CPU_IDLE(thread, counter) \
266 thread->conf.busy[counter] = 0
268 #define IS_CPU_BUSY(thread, counter) \
269 (thread->conf.busy[counter] > 0)
273 #define SET_CPU_BUSY(thread, counter)
274 #define SET_CPU_IDLE(thread, counter)
275 #define IS_CPU_BUSY(thread, counter) 0
281 struct rte_mbuf *m_table[MAX_PKT_BURST];
284 struct lcore_rx_queue {
287 } __rte_cache_aligned;
289 #define MAX_RX_QUEUE_PER_LCORE 16
290 #define MAX_TX_QUEUE_PER_PORT RTE_MAX_ETHPORTS
291 #define MAX_RX_QUEUE_PER_PORT 128
293 #define MAX_LCORE_PARAMS 1024
294 struct rx_thread_params {
299 } __rte_cache_aligned;
301 static struct rx_thread_params rx_thread_params_array[MAX_LCORE_PARAMS];
302 static struct rx_thread_params rx_thread_params_array_default[] = {
314 static struct rx_thread_params *rx_thread_params =
315 rx_thread_params_array_default;
316 static uint16_t nb_rx_thread_params = RTE_DIM(rx_thread_params_array_default);
318 struct tx_thread_params {
321 } __rte_cache_aligned;
323 static struct tx_thread_params tx_thread_params_array[MAX_LCORE_PARAMS];
324 static struct tx_thread_params tx_thread_params_array_default[] = {
336 static struct tx_thread_params *tx_thread_params =
337 tx_thread_params_array_default;
338 static uint16_t nb_tx_thread_params = RTE_DIM(tx_thread_params_array_default);
340 static struct rte_eth_conf port_conf = {
342 .mq_mode = ETH_MQ_RX_RSS,
343 .max_rx_pkt_len = ETHER_MAX_LEN,
345 .header_split = 0, /**< Header Split disabled */
346 .hw_ip_checksum = 1, /**< IP checksum offload enabled */
347 .hw_vlan_filter = 0, /**< VLAN filtering disabled */
348 .jumbo_frame = 0, /**< Jumbo Frame Support disabled */
349 .hw_strip_crc = 1, /**< CRC stripped by hardware */
354 .rss_hf = ETH_RSS_TCP,
358 .mq_mode = ETH_MQ_TX_NONE,
362 static struct rte_mempool *pktmbuf_pool[NB_SOCKETS];
364 #if (APP_LOOKUP_METHOD == APP_LOOKUP_EXACT_MATCH)
366 #ifdef RTE_MACHINE_CPUFLAG_SSE4_2
367 #include <rte_hash_crc.h>
368 #define DEFAULT_HASH_FUNC rte_hash_crc
370 #include <rte_jhash.h>
371 #define DEFAULT_HASH_FUNC rte_jhash
380 } __attribute__((__packed__));
382 union ipv4_5tuple_host {
395 #define XMM_NUM_IN_IPV6_5TUPLE 3
398 uint8_t ip_dst[IPV6_ADDR_LEN];
399 uint8_t ip_src[IPV6_ADDR_LEN];
403 } __attribute__((__packed__));
405 union ipv6_5tuple_host {
410 uint8_t ip_src[IPV6_ADDR_LEN];
411 uint8_t ip_dst[IPV6_ADDR_LEN];
416 __m128i xmm[XMM_NUM_IN_IPV6_5TUPLE];
419 struct ipv4_l3fwd_route {
420 struct ipv4_5tuple key;
424 struct ipv6_l3fwd_route {
425 struct ipv6_5tuple key;
429 static struct ipv4_l3fwd_route ipv4_l3fwd_route_array[] = {
430 {{IPv4(101, 0, 0, 0), IPv4(100, 10, 0, 1), 101, 11, IPPROTO_TCP}, 0},
431 {{IPv4(201, 0, 0, 0), IPv4(200, 20, 0, 1), 102, 12, IPPROTO_TCP}, 1},
432 {{IPv4(111, 0, 0, 0), IPv4(100, 30, 0, 1), 101, 11, IPPROTO_TCP}, 2},
433 {{IPv4(211, 0, 0, 0), IPv4(200, 40, 0, 1), 102, 12, IPPROTO_TCP}, 3},
436 static struct ipv6_l3fwd_route ipv6_l3fwd_route_array[] = {
438 {0xfe, 0x80, 0, 0, 0, 0, 0, 0, 0x02, 0x1e, 0x67, 0xff, 0xfe, 0, 0, 0},
439 {0xfe, 0x80, 0, 0, 0, 0, 0, 0, 0x02, 0x1b, 0x21, 0xff, 0xfe, 0x91, 0x38,
441 101, 11, IPPROTO_TCP}, 0},
444 {0xfe, 0x90, 0, 0, 0, 0, 0, 0, 0x02, 0x1e, 0x67, 0xff, 0xfe, 0, 0, 0},
445 {0xfe, 0x90, 0, 0, 0, 0, 0, 0, 0x02, 0x1b, 0x21, 0xff, 0xfe, 0x91, 0x38,
447 102, 12, IPPROTO_TCP}, 1},
450 {0xfe, 0xa0, 0, 0, 0, 0, 0, 0, 0x02, 0x1e, 0x67, 0xff, 0xfe, 0, 0, 0},
451 {0xfe, 0xa0, 0, 0, 0, 0, 0, 0, 0x02, 0x1b, 0x21, 0xff, 0xfe, 0x91, 0x38,
453 101, 11, IPPROTO_TCP}, 2},
456 {0xfe, 0xb0, 0, 0, 0, 0, 0, 0, 0x02, 0x1e, 0x67, 0xff, 0xfe, 0, 0, 0},
457 {0xfe, 0xb0, 0, 0, 0, 0, 0, 0, 0x02, 0x1b, 0x21, 0xff, 0xfe, 0x91, 0x38,
459 102, 12, IPPROTO_TCP}, 3},
462 typedef struct rte_hash lookup_struct_t;
463 static lookup_struct_t *ipv4_l3fwd_lookup_struct[NB_SOCKETS];
464 static lookup_struct_t *ipv6_l3fwd_lookup_struct[NB_SOCKETS];
466 #ifdef RTE_ARCH_X86_64
467 /* default to 4 million hash entries (approx) */
468 #define L3FWD_HASH_ENTRIES (1024*1024*4)
470 /* 32-bit has less address-space for hugepage memory, limit to 1M entries */
471 #define L3FWD_HASH_ENTRIES (1024*1024*1)
473 #define HASH_ENTRY_NUMBER_DEFAULT 4
475 static uint32_t hash_entry_number = HASH_ENTRY_NUMBER_DEFAULT;
477 static inline uint32_t
478 ipv4_hash_crc(const void *data, __rte_unused uint32_t data_len,
481 const union ipv4_5tuple_host *k;
487 p = (const uint32_t *)&k->port_src;
489 #ifdef RTE_MACHINE_CPUFLAG_SSE4_2
490 init_val = rte_hash_crc_4byte(t, init_val);
491 init_val = rte_hash_crc_4byte(k->ip_src, init_val);
492 init_val = rte_hash_crc_4byte(k->ip_dst, init_val);
493 init_val = rte_hash_crc_4byte(*p, init_val);
494 #else /* RTE_MACHINE_CPUFLAG_SSE4_2 */
495 init_val = rte_jhash_1word(t, init_val);
496 init_val = rte_jhash_1word(k->ip_src, init_val);
497 init_val = rte_jhash_1word(k->ip_dst, init_val);
498 init_val = rte_jhash_1word(*p, init_val);
499 #endif /* RTE_MACHINE_CPUFLAG_SSE4_2 */
503 static inline uint32_t
504 ipv6_hash_crc(const void *data, __rte_unused uint32_t data_len,
507 const union ipv6_5tuple_host *k;
510 #ifdef RTE_MACHINE_CPUFLAG_SSE4_2
511 const uint32_t *ip_src0, *ip_src1, *ip_src2, *ip_src3;
512 const uint32_t *ip_dst0, *ip_dst1, *ip_dst2, *ip_dst3;
513 #endif /* RTE_MACHINE_CPUFLAG_SSE4_2 */
517 p = (const uint32_t *)&k->port_src;
519 #ifdef RTE_MACHINE_CPUFLAG_SSE4_2
520 ip_src0 = (const uint32_t *) k->ip_src;
521 ip_src1 = (const uint32_t *)(k->ip_src + 4);
522 ip_src2 = (const uint32_t *)(k->ip_src + 8);
523 ip_src3 = (const uint32_t *)(k->ip_src + 12);
524 ip_dst0 = (const uint32_t *) k->ip_dst;
525 ip_dst1 = (const uint32_t *)(k->ip_dst + 4);
526 ip_dst2 = (const uint32_t *)(k->ip_dst + 8);
527 ip_dst3 = (const uint32_t *)(k->ip_dst + 12);
528 init_val = rte_hash_crc_4byte(t, init_val);
529 init_val = rte_hash_crc_4byte(*ip_src0, init_val);
530 init_val = rte_hash_crc_4byte(*ip_src1, init_val);
531 init_val = rte_hash_crc_4byte(*ip_src2, init_val);
532 init_val = rte_hash_crc_4byte(*ip_src3, init_val);
533 init_val = rte_hash_crc_4byte(*ip_dst0, init_val);
534 init_val = rte_hash_crc_4byte(*ip_dst1, init_val);
535 init_val = rte_hash_crc_4byte(*ip_dst2, init_val);
536 init_val = rte_hash_crc_4byte(*ip_dst3, init_val);
537 init_val = rte_hash_crc_4byte(*p, init_val);
538 #else /* RTE_MACHINE_CPUFLAG_SSE4_2 */
539 init_val = rte_jhash_1word(t, init_val);
540 init_val = rte_jhash(k->ip_src, sizeof(uint8_t) * IPV6_ADDR_LEN, init_val);
541 init_val = rte_jhash(k->ip_dst, sizeof(uint8_t) * IPV6_ADDR_LEN, init_val);
542 init_val = rte_jhash_1word(*p, init_val);
543 #endif /* RTE_MACHINE_CPUFLAG_SSE4_2 */
547 #define IPV4_L3FWD_NUM_ROUTES RTE_DIM(ipv4_l3fwd_route_array)
548 #define IPV6_L3FWD_NUM_ROUTES RTE_DIM(ipv6_l3fwd_route_array)
550 static uint8_t ipv4_l3fwd_out_if[L3FWD_HASH_ENTRIES] __rte_cache_aligned;
551 static uint8_t ipv6_l3fwd_out_if[L3FWD_HASH_ENTRIES] __rte_cache_aligned;
555 #if (APP_LOOKUP_METHOD == APP_LOOKUP_LPM)
556 struct ipv4_l3fwd_route {
562 struct ipv6_l3fwd_route {
568 static struct ipv4_l3fwd_route ipv4_l3fwd_route_array[] = {
569 {IPv4(1, 1, 1, 0), 24, 0},
570 {IPv4(2, 1, 1, 0), 24, 1},
571 {IPv4(3, 1, 1, 0), 24, 2},
572 {IPv4(4, 1, 1, 0), 24, 3},
573 {IPv4(5, 1, 1, 0), 24, 4},
574 {IPv4(6, 1, 1, 0), 24, 5},
575 {IPv4(7, 1, 1, 0), 24, 6},
576 {IPv4(8, 1, 1, 0), 24, 7},
579 static struct ipv6_l3fwd_route ipv6_l3fwd_route_array[] = {
580 {{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, 48, 0},
581 {{2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, 48, 1},
582 {{3, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, 48, 2},
583 {{4, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, 48, 3},
584 {{5, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, 48, 4},
585 {{6, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, 48, 5},
586 {{7, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, 48, 6},
587 {{8, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, 48, 7},
590 #define IPV4_L3FWD_NUM_ROUTES RTE_DIM(ipv4_l3fwd_route_array)
591 #define IPV6_L3FWD_NUM_ROUTES RTE_DIM(ipv6_l3fwd_route_array)
593 #define IPV4_L3FWD_LPM_MAX_RULES 1024
594 #define IPV6_L3FWD_LPM_MAX_RULES 1024
595 #define IPV6_L3FWD_LPM_NUMBER_TBL8S (1 << 16)
597 typedef struct rte_lpm lookup_struct_t;
598 typedef struct rte_lpm6 lookup6_struct_t;
599 static lookup_struct_t *ipv4_l3fwd_lookup_struct[NB_SOCKETS];
600 static lookup6_struct_t *ipv6_l3fwd_lookup_struct[NB_SOCKETS];
604 lookup_struct_t *ipv4_lookup_struct;
605 #if (APP_LOOKUP_METHOD == APP_LOOKUP_LPM)
606 lookup6_struct_t *ipv6_lookup_struct;
608 lookup_struct_t *ipv6_lookup_struct;
611 } __rte_cache_aligned;
613 static struct lcore_conf lcore_conf[RTE_MAX_LCORE];
614 RTE_DEFINE_PER_LCORE(struct lcore_conf *, lcore_conf);
616 #define MAX_RX_QUEUE_PER_THREAD 16
617 #define MAX_TX_PORT_PER_THREAD RTE_MAX_ETHPORTS
618 #define MAX_TX_QUEUE_PER_PORT RTE_MAX_ETHPORTS
619 #define MAX_RX_QUEUE_PER_PORT 128
621 #define MAX_RX_THREAD 1024
622 #define MAX_TX_THREAD 1024
623 #define MAX_THREAD (MAX_RX_THREAD + MAX_TX_THREAD)
626 * Producers and consumers threads configuration
628 static int lthreads_on = 1; /**< Use lthreads for processing*/
630 rte_atomic16_t rx_counter; /**< Number of spawned rx threads */
631 rte_atomic16_t tx_counter; /**< Number of spawned tx threads */
634 uint16_t lcore_id; /**< Initial lcore for rx thread */
635 uint16_t cpu_id; /**< Cpu id for cpu load stats counter */
636 uint16_t thread_id; /**< Thread ID */
638 #if (APP_CPU_LOAD > 0)
639 int busy[MAX_CPU_COUNTER];
643 struct thread_rx_conf {
644 struct thread_conf conf;
647 struct lcore_rx_queue rx_queue_list[MAX_RX_QUEUE_PER_LCORE];
649 uint16_t n_ring; /**< Number of output rings */
650 struct rte_ring *ring[RTE_MAX_LCORE];
651 struct lthread_cond *ready[RTE_MAX_LCORE];
653 #if (APP_CPU_LOAD > 0)
654 int busy[MAX_CPU_COUNTER];
656 } __rte_cache_aligned;
658 uint16_t n_rx_thread;
659 struct thread_rx_conf rx_thread[MAX_RX_THREAD];
661 struct thread_tx_conf {
662 struct thread_conf conf;
664 uint16_t tx_queue_id[RTE_MAX_LCORE];
665 struct mbuf_table tx_mbufs[RTE_MAX_LCORE];
667 struct rte_ring *ring;
668 struct lthread_cond **ready;
670 } __rte_cache_aligned;
672 uint16_t n_tx_thread;
673 struct thread_tx_conf tx_thread[MAX_TX_THREAD];
675 /* Send burst of packets on an output interface */
677 send_burst(struct thread_tx_conf *qconf, uint16_t n, uint8_t port)
679 struct rte_mbuf **m_table;
683 queueid = qconf->tx_queue_id[port];
684 m_table = (struct rte_mbuf **)qconf->tx_mbufs[port].m_table;
686 ret = rte_eth_tx_burst(port, queueid, m_table, n);
687 if (unlikely(ret < n)) {
689 rte_pktmbuf_free(m_table[ret]);
696 /* Enqueue a single packet, and send burst if queue is filled */
698 send_single_packet(struct rte_mbuf *m, uint8_t port)
701 struct thread_tx_conf *qconf;
704 qconf = (struct thread_tx_conf *)lthread_get_data();
706 qconf = (struct thread_tx_conf *)RTE_PER_LCORE(lcore_conf)->data;
708 len = qconf->tx_mbufs[port].len;
709 qconf->tx_mbufs[port].m_table[len] = m;
712 /* enough pkts to be sent */
713 if (unlikely(len == MAX_PKT_BURST)) {
714 send_burst(qconf, MAX_PKT_BURST, port);
718 qconf->tx_mbufs[port].len = len;
722 #if ((APP_LOOKUP_METHOD == APP_LOOKUP_LPM) && \
723 (ENABLE_MULTI_BUFFER_OPTIMIZE == 1))
724 static __rte_always_inline void
725 send_packetsx4(uint8_t port,
726 struct rte_mbuf *m[], uint32_t num)
729 struct thread_tx_conf *qconf;
732 qconf = (struct thread_tx_conf *)lthread_get_data();
734 qconf = (struct thread_tx_conf *)RTE_PER_LCORE(lcore_conf)->data;
736 len = qconf->tx_mbufs[port].len;
739 * If TX buffer for that queue is empty, and we have enough packets,
740 * then send them straightway.
742 if (num >= MAX_TX_BURST && len == 0) {
743 n = rte_eth_tx_burst(port, qconf->tx_queue_id[port], m, num);
744 if (unlikely(n < num)) {
746 rte_pktmbuf_free(m[n]);
753 * Put packets into TX buffer for that queue.
757 n = (n > MAX_PKT_BURST) ? MAX_PKT_BURST - len : num;
760 switch (n % FWDSTEP) {
763 qconf->tx_mbufs[port].m_table[len + j] = m[j];
767 qconf->tx_mbufs[port].m_table[len + j] = m[j];
771 qconf->tx_mbufs[port].m_table[len + j] = m[j];
775 qconf->tx_mbufs[port].m_table[len + j] = m[j];
782 /* enough pkts to be sent */
783 if (unlikely(len == MAX_PKT_BURST)) {
785 send_burst(qconf, MAX_PKT_BURST, port);
787 /* copy rest of the packets into the TX buffer. */
790 switch (len % FWDSTEP) {
793 qconf->tx_mbufs[port].m_table[j] = m[n + j];
797 qconf->tx_mbufs[port].m_table[j] = m[n + j];
801 qconf->tx_mbufs[port].m_table[j] = m[n + j];
805 qconf->tx_mbufs[port].m_table[j] = m[n + j];
811 qconf->tx_mbufs[port].len = len;
813 #endif /* APP_LOOKUP_LPM */
815 #ifdef DO_RFC_1812_CHECKS
817 is_valid_ipv4_pkt(struct ipv4_hdr *pkt, uint32_t link_len)
819 /* From http://www.rfc-editor.org/rfc/rfc1812.txt section 5.2.2 */
821 * 1. The packet length reported by the Link Layer must be large
822 * enough to hold the minimum length legal IP datagram (20 bytes).
824 if (link_len < sizeof(struct ipv4_hdr))
827 /* 2. The IP checksum must be correct. */
828 /* this is checked in H/W */
831 * 3. The IP version number must be 4. If the version number is not 4
832 * then the packet may be another version of IP, such as IPng or
835 if (((pkt->version_ihl) >> 4) != 4)
838 * 4. The IP header length field must be large enough to hold the
839 * minimum length legal IP datagram (20 bytes = 5 words).
841 if ((pkt->version_ihl & 0xf) < 5)
845 * 5. The IP total length field must be large enough to hold the IP
846 * datagram header, whose length is specified in the IP header length
849 if (rte_cpu_to_be_16(pkt->total_length) < sizeof(struct ipv4_hdr))
856 #if (APP_LOOKUP_METHOD == APP_LOOKUP_EXACT_MATCH)
858 static __m128i mask0;
859 static __m128i mask1;
860 static __m128i mask2;
861 static inline uint8_t
862 get_ipv4_dst_port(void *ipv4_hdr, uint8_t portid,
863 lookup_struct_t *ipv4_l3fwd_lookup_struct)
866 union ipv4_5tuple_host key;
868 ipv4_hdr = (uint8_t *)ipv4_hdr + offsetof(struct ipv4_hdr, time_to_live);
869 __m128i data = _mm_loadu_si128((__m128i *)(ipv4_hdr));
870 /* Get 5 tuple: dst port, src port, dst IP address, src IP address and
872 key.xmm = _mm_and_si128(data, mask0);
873 /* Find destination port */
874 ret = rte_hash_lookup(ipv4_l3fwd_lookup_struct, (const void *)&key);
875 return (uint8_t)((ret < 0) ? portid : ipv4_l3fwd_out_if[ret]);
878 static inline uint8_t
879 get_ipv6_dst_port(void *ipv6_hdr, uint8_t portid,
880 lookup_struct_t *ipv6_l3fwd_lookup_struct)
883 union ipv6_5tuple_host key;
885 ipv6_hdr = (uint8_t *)ipv6_hdr + offsetof(struct ipv6_hdr, payload_len);
886 __m128i data0 = _mm_loadu_si128((__m128i *)(ipv6_hdr));
887 __m128i data1 = _mm_loadu_si128((__m128i *)(((uint8_t *)ipv6_hdr) +
889 __m128i data2 = _mm_loadu_si128((__m128i *)(((uint8_t *)ipv6_hdr) +
890 sizeof(__m128i) + sizeof(__m128i)));
891 /* Get part of 5 tuple: src IP address lower 96 bits and protocol */
892 key.xmm[0] = _mm_and_si128(data0, mask1);
893 /* Get part of 5 tuple: dst IP address lower 96 bits and src IP address
896 /* Get part of 5 tuple: dst port and src port and dst IP address higher
898 key.xmm[2] = _mm_and_si128(data2, mask2);
900 /* Find destination port */
901 ret = rte_hash_lookup(ipv6_l3fwd_lookup_struct, (const void *)&key);
902 return (uint8_t)((ret < 0) ? portid : ipv6_l3fwd_out_if[ret]);
906 #if (APP_LOOKUP_METHOD == APP_LOOKUP_LPM)
908 static inline uint8_t
909 get_ipv4_dst_port(void *ipv4_hdr, uint8_t portid,
910 lookup_struct_t *ipv4_l3fwd_lookup_struct)
914 return (uint8_t)((rte_lpm_lookup(ipv4_l3fwd_lookup_struct,
915 rte_be_to_cpu_32(((struct ipv4_hdr *)ipv4_hdr)->dst_addr),
916 &next_hop) == 0) ? next_hop : portid);
919 static inline uint8_t
920 get_ipv6_dst_port(void *ipv6_hdr, uint8_t portid,
921 lookup6_struct_t *ipv6_l3fwd_lookup_struct)
925 return (uint8_t) ((rte_lpm6_lookup(ipv6_l3fwd_lookup_struct,
926 ((struct ipv6_hdr *)ipv6_hdr)->dst_addr, &next_hop) == 0) ?
931 static inline void l3fwd_simple_forward(struct rte_mbuf *m, uint8_t portid)
932 __attribute__((unused));
934 #if ((APP_LOOKUP_METHOD == APP_LOOKUP_EXACT_MATCH) && \
935 (ENABLE_MULTI_BUFFER_OPTIMIZE == 1))
937 #define MASK_ALL_PKTS 0xff
938 #define EXCLUDE_1ST_PKT 0xfe
939 #define EXCLUDE_2ND_PKT 0xfd
940 #define EXCLUDE_3RD_PKT 0xfb
941 #define EXCLUDE_4TH_PKT 0xf7
942 #define EXCLUDE_5TH_PKT 0xef
943 #define EXCLUDE_6TH_PKT 0xdf
944 #define EXCLUDE_7TH_PKT 0xbf
945 #define EXCLUDE_8TH_PKT 0x7f
948 simple_ipv4_fwd_8pkts(struct rte_mbuf *m[8], uint8_t portid)
950 struct ether_hdr *eth_hdr[8];
951 struct ipv4_hdr *ipv4_hdr[8];
954 union ipv4_5tuple_host key[8];
957 eth_hdr[0] = rte_pktmbuf_mtod(m[0], struct ether_hdr *);
958 eth_hdr[1] = rte_pktmbuf_mtod(m[1], struct ether_hdr *);
959 eth_hdr[2] = rte_pktmbuf_mtod(m[2], struct ether_hdr *);
960 eth_hdr[3] = rte_pktmbuf_mtod(m[3], struct ether_hdr *);
961 eth_hdr[4] = rte_pktmbuf_mtod(m[4], struct ether_hdr *);
962 eth_hdr[5] = rte_pktmbuf_mtod(m[5], struct ether_hdr *);
963 eth_hdr[6] = rte_pktmbuf_mtod(m[6], struct ether_hdr *);
964 eth_hdr[7] = rte_pktmbuf_mtod(m[7], struct ether_hdr *);
966 /* Handle IPv4 headers.*/
967 ipv4_hdr[0] = rte_pktmbuf_mtod_offset(m[0], struct ipv4_hdr *,
968 sizeof(struct ether_hdr));
969 ipv4_hdr[1] = rte_pktmbuf_mtod_offset(m[1], struct ipv4_hdr *,
970 sizeof(struct ether_hdr));
971 ipv4_hdr[2] = rte_pktmbuf_mtod_offset(m[2], struct ipv4_hdr *,
972 sizeof(struct ether_hdr));
973 ipv4_hdr[3] = rte_pktmbuf_mtod_offset(m[3], struct ipv4_hdr *,
974 sizeof(struct ether_hdr));
975 ipv4_hdr[4] = rte_pktmbuf_mtod_offset(m[4], struct ipv4_hdr *,
976 sizeof(struct ether_hdr));
977 ipv4_hdr[5] = rte_pktmbuf_mtod_offset(m[5], struct ipv4_hdr *,
978 sizeof(struct ether_hdr));
979 ipv4_hdr[6] = rte_pktmbuf_mtod_offset(m[6], struct ipv4_hdr *,
980 sizeof(struct ether_hdr));
981 ipv4_hdr[7] = rte_pktmbuf_mtod_offset(m[7], struct ipv4_hdr *,
982 sizeof(struct ether_hdr));
984 #ifdef DO_RFC_1812_CHECKS
985 /* Check to make sure the packet is valid (RFC1812) */
986 uint8_t valid_mask = MASK_ALL_PKTS;
988 if (is_valid_ipv4_pkt(ipv4_hdr[0], m[0]->pkt_len) < 0) {
989 rte_pktmbuf_free(m[0]);
990 valid_mask &= EXCLUDE_1ST_PKT;
992 if (is_valid_ipv4_pkt(ipv4_hdr[1], m[1]->pkt_len) < 0) {
993 rte_pktmbuf_free(m[1]);
994 valid_mask &= EXCLUDE_2ND_PKT;
996 if (is_valid_ipv4_pkt(ipv4_hdr[2], m[2]->pkt_len) < 0) {
997 rte_pktmbuf_free(m[2]);
998 valid_mask &= EXCLUDE_3RD_PKT;
1000 if (is_valid_ipv4_pkt(ipv4_hdr[3], m[3]->pkt_len) < 0) {
1001 rte_pktmbuf_free(m[3]);
1002 valid_mask &= EXCLUDE_4TH_PKT;
1004 if (is_valid_ipv4_pkt(ipv4_hdr[4], m[4]->pkt_len) < 0) {
1005 rte_pktmbuf_free(m[4]);
1006 valid_mask &= EXCLUDE_5TH_PKT;
1008 if (is_valid_ipv4_pkt(ipv4_hdr[5], m[5]->pkt_len) < 0) {
1009 rte_pktmbuf_free(m[5]);
1010 valid_mask &= EXCLUDE_6TH_PKT;
1012 if (is_valid_ipv4_pkt(ipv4_hdr[6], m[6]->pkt_len) < 0) {
1013 rte_pktmbuf_free(m[6]);
1014 valid_mask &= EXCLUDE_7TH_PKT;
1016 if (is_valid_ipv4_pkt(ipv4_hdr[7], m[7]->pkt_len) < 0) {
1017 rte_pktmbuf_free(m[7]);
1018 valid_mask &= EXCLUDE_8TH_PKT;
1020 if (unlikely(valid_mask != MASK_ALL_PKTS)) {
1021 if (valid_mask == 0)
1026 for (i = 0; i < 8; i++)
1027 if ((0x1 << i) & valid_mask)
1028 l3fwd_simple_forward(m[i], portid);
1030 #endif /* End of #ifdef DO_RFC_1812_CHECKS */
1032 data[0] = _mm_loadu_si128(rte_pktmbuf_mtod_offset(m[0], __m128i *,
1033 sizeof(struct ether_hdr) +
1034 offsetof(struct ipv4_hdr, time_to_live)));
1035 data[1] = _mm_loadu_si128(rte_pktmbuf_mtod_offset(m[1], __m128i *,
1036 sizeof(struct ether_hdr) +
1037 offsetof(struct ipv4_hdr, time_to_live)));
1038 data[2] = _mm_loadu_si128(rte_pktmbuf_mtod_offset(m[2], __m128i *,
1039 sizeof(struct ether_hdr) +
1040 offsetof(struct ipv4_hdr, time_to_live)));
1041 data[3] = _mm_loadu_si128(rte_pktmbuf_mtod_offset(m[3], __m128i *,
1042 sizeof(struct ether_hdr) +
1043 offsetof(struct ipv4_hdr, time_to_live)));
1044 data[4] = _mm_loadu_si128(rte_pktmbuf_mtod_offset(m[4], __m128i *,
1045 sizeof(struct ether_hdr) +
1046 offsetof(struct ipv4_hdr, time_to_live)));
1047 data[5] = _mm_loadu_si128(rte_pktmbuf_mtod_offset(m[5], __m128i *,
1048 sizeof(struct ether_hdr) +
1049 offsetof(struct ipv4_hdr, time_to_live)));
1050 data[6] = _mm_loadu_si128(rte_pktmbuf_mtod_offset(m[6], __m128i *,
1051 sizeof(struct ether_hdr) +
1052 offsetof(struct ipv4_hdr, time_to_live)));
1053 data[7] = _mm_loadu_si128(rte_pktmbuf_mtod_offset(m[7], __m128i *,
1054 sizeof(struct ether_hdr) +
1055 offsetof(struct ipv4_hdr, time_to_live)));
1057 key[0].xmm = _mm_and_si128(data[0], mask0);
1058 key[1].xmm = _mm_and_si128(data[1], mask0);
1059 key[2].xmm = _mm_and_si128(data[2], mask0);
1060 key[3].xmm = _mm_and_si128(data[3], mask0);
1061 key[4].xmm = _mm_and_si128(data[4], mask0);
1062 key[5].xmm = _mm_and_si128(data[5], mask0);
1063 key[6].xmm = _mm_and_si128(data[6], mask0);
1064 key[7].xmm = _mm_and_si128(data[7], mask0);
1066 const void *key_array[8] = {&key[0], &key[1], &key[2], &key[3],
1067 &key[4], &key[5], &key[6], &key[7]};
1069 rte_hash_lookup_bulk(RTE_PER_LCORE(lcore_conf)->ipv4_lookup_struct,
1070 &key_array[0], 8, ret);
1071 dst_port[0] = (uint8_t) ((ret[0] < 0) ? portid : ipv4_l3fwd_out_if[ret[0]]);
1072 dst_port[1] = (uint8_t) ((ret[1] < 0) ? portid : ipv4_l3fwd_out_if[ret[1]]);
1073 dst_port[2] = (uint8_t) ((ret[2] < 0) ? portid : ipv4_l3fwd_out_if[ret[2]]);
1074 dst_port[3] = (uint8_t) ((ret[3] < 0) ? portid : ipv4_l3fwd_out_if[ret[3]]);
1075 dst_port[4] = (uint8_t) ((ret[4] < 0) ? portid : ipv4_l3fwd_out_if[ret[4]]);
1076 dst_port[5] = (uint8_t) ((ret[5] < 0) ? portid : ipv4_l3fwd_out_if[ret[5]]);
1077 dst_port[6] = (uint8_t) ((ret[6] < 0) ? portid : ipv4_l3fwd_out_if[ret[6]]);
1078 dst_port[7] = (uint8_t) ((ret[7] < 0) ? portid : ipv4_l3fwd_out_if[ret[7]]);
1080 if (dst_port[0] >= RTE_MAX_ETHPORTS ||
1081 (enabled_port_mask & 1 << dst_port[0]) == 0)
1082 dst_port[0] = portid;
1083 if (dst_port[1] >= RTE_MAX_ETHPORTS ||
1084 (enabled_port_mask & 1 << dst_port[1]) == 0)
1085 dst_port[1] = portid;
1086 if (dst_port[2] >= RTE_MAX_ETHPORTS ||
1087 (enabled_port_mask & 1 << dst_port[2]) == 0)
1088 dst_port[2] = portid;
1089 if (dst_port[3] >= RTE_MAX_ETHPORTS ||
1090 (enabled_port_mask & 1 << dst_port[3]) == 0)
1091 dst_port[3] = portid;
1092 if (dst_port[4] >= RTE_MAX_ETHPORTS ||
1093 (enabled_port_mask & 1 << dst_port[4]) == 0)
1094 dst_port[4] = portid;
1095 if (dst_port[5] >= RTE_MAX_ETHPORTS ||
1096 (enabled_port_mask & 1 << dst_port[5]) == 0)
1097 dst_port[5] = portid;
1098 if (dst_port[6] >= RTE_MAX_ETHPORTS ||
1099 (enabled_port_mask & 1 << dst_port[6]) == 0)
1100 dst_port[6] = portid;
1101 if (dst_port[7] >= RTE_MAX_ETHPORTS ||
1102 (enabled_port_mask & 1 << dst_port[7]) == 0)
1103 dst_port[7] = portid;
1105 #ifdef DO_RFC_1812_CHECKS
1106 /* Update time to live and header checksum */
1107 --(ipv4_hdr[0]->time_to_live);
1108 --(ipv4_hdr[1]->time_to_live);
1109 --(ipv4_hdr[2]->time_to_live);
1110 --(ipv4_hdr[3]->time_to_live);
1111 ++(ipv4_hdr[0]->hdr_checksum);
1112 ++(ipv4_hdr[1]->hdr_checksum);
1113 ++(ipv4_hdr[2]->hdr_checksum);
1114 ++(ipv4_hdr[3]->hdr_checksum);
1115 --(ipv4_hdr[4]->time_to_live);
1116 --(ipv4_hdr[5]->time_to_live);
1117 --(ipv4_hdr[6]->time_to_live);
1118 --(ipv4_hdr[7]->time_to_live);
1119 ++(ipv4_hdr[4]->hdr_checksum);
1120 ++(ipv4_hdr[5]->hdr_checksum);
1121 ++(ipv4_hdr[6]->hdr_checksum);
1122 ++(ipv4_hdr[7]->hdr_checksum);
1126 *(uint64_t *)ð_hdr[0]->d_addr = dest_eth_addr[dst_port[0]];
1127 *(uint64_t *)ð_hdr[1]->d_addr = dest_eth_addr[dst_port[1]];
1128 *(uint64_t *)ð_hdr[2]->d_addr = dest_eth_addr[dst_port[2]];
1129 *(uint64_t *)ð_hdr[3]->d_addr = dest_eth_addr[dst_port[3]];
1130 *(uint64_t *)ð_hdr[4]->d_addr = dest_eth_addr[dst_port[4]];
1131 *(uint64_t *)ð_hdr[5]->d_addr = dest_eth_addr[dst_port[5]];
1132 *(uint64_t *)ð_hdr[6]->d_addr = dest_eth_addr[dst_port[6]];
1133 *(uint64_t *)ð_hdr[7]->d_addr = dest_eth_addr[dst_port[7]];
1136 ether_addr_copy(&ports_eth_addr[dst_port[0]], ð_hdr[0]->s_addr);
1137 ether_addr_copy(&ports_eth_addr[dst_port[1]], ð_hdr[1]->s_addr);
1138 ether_addr_copy(&ports_eth_addr[dst_port[2]], ð_hdr[2]->s_addr);
1139 ether_addr_copy(&ports_eth_addr[dst_port[3]], ð_hdr[3]->s_addr);
1140 ether_addr_copy(&ports_eth_addr[dst_port[4]], ð_hdr[4]->s_addr);
1141 ether_addr_copy(&ports_eth_addr[dst_port[5]], ð_hdr[5]->s_addr);
1142 ether_addr_copy(&ports_eth_addr[dst_port[6]], ð_hdr[6]->s_addr);
1143 ether_addr_copy(&ports_eth_addr[dst_port[7]], ð_hdr[7]->s_addr);
1145 send_single_packet(m[0], (uint8_t)dst_port[0]);
1146 send_single_packet(m[1], (uint8_t)dst_port[1]);
1147 send_single_packet(m[2], (uint8_t)dst_port[2]);
1148 send_single_packet(m[3], (uint8_t)dst_port[3]);
1149 send_single_packet(m[4], (uint8_t)dst_port[4]);
1150 send_single_packet(m[5], (uint8_t)dst_port[5]);
1151 send_single_packet(m[6], (uint8_t)dst_port[6]);
1152 send_single_packet(m[7], (uint8_t)dst_port[7]);
1156 static inline void get_ipv6_5tuple(struct rte_mbuf *m0, __m128i mask0,
1157 __m128i mask1, union ipv6_5tuple_host *key)
1159 __m128i tmpdata0 = _mm_loadu_si128(rte_pktmbuf_mtod_offset(m0,
1160 __m128i *, sizeof(struct ether_hdr) +
1161 offsetof(struct ipv6_hdr, payload_len)));
1162 __m128i tmpdata1 = _mm_loadu_si128(rte_pktmbuf_mtod_offset(m0,
1163 __m128i *, sizeof(struct ether_hdr) +
1164 offsetof(struct ipv6_hdr, payload_len) + sizeof(__m128i)));
1165 __m128i tmpdata2 = _mm_loadu_si128(rte_pktmbuf_mtod_offset(m0,
1166 __m128i *, sizeof(struct ether_hdr) +
1167 offsetof(struct ipv6_hdr, payload_len) + sizeof(__m128i) +
1169 key->xmm[0] = _mm_and_si128(tmpdata0, mask0);
1170 key->xmm[1] = tmpdata1;
1171 key->xmm[2] = _mm_and_si128(tmpdata2, mask1);
1175 simple_ipv6_fwd_8pkts(struct rte_mbuf *m[8], uint8_t portid)
1178 uint8_t dst_port[8];
1179 struct ether_hdr *eth_hdr[8];
1180 union ipv6_5tuple_host key[8];
1182 __attribute__((unused)) struct ipv6_hdr *ipv6_hdr[8];
1184 eth_hdr[0] = rte_pktmbuf_mtod(m[0], struct ether_hdr *);
1185 eth_hdr[1] = rte_pktmbuf_mtod(m[1], struct ether_hdr *);
1186 eth_hdr[2] = rte_pktmbuf_mtod(m[2], struct ether_hdr *);
1187 eth_hdr[3] = rte_pktmbuf_mtod(m[3], struct ether_hdr *);
1188 eth_hdr[4] = rte_pktmbuf_mtod(m[4], struct ether_hdr *);
1189 eth_hdr[5] = rte_pktmbuf_mtod(m[5], struct ether_hdr *);
1190 eth_hdr[6] = rte_pktmbuf_mtod(m[6], struct ether_hdr *);
1191 eth_hdr[7] = rte_pktmbuf_mtod(m[7], struct ether_hdr *);
1193 /* Handle IPv6 headers.*/
1194 ipv6_hdr[0] = rte_pktmbuf_mtod_offset(m[0], struct ipv6_hdr *,
1195 sizeof(struct ether_hdr));
1196 ipv6_hdr[1] = rte_pktmbuf_mtod_offset(m[1], struct ipv6_hdr *,
1197 sizeof(struct ether_hdr));
1198 ipv6_hdr[2] = rte_pktmbuf_mtod_offset(m[2], struct ipv6_hdr *,
1199 sizeof(struct ether_hdr));
1200 ipv6_hdr[3] = rte_pktmbuf_mtod_offset(m[3], struct ipv6_hdr *,
1201 sizeof(struct ether_hdr));
1202 ipv6_hdr[4] = rte_pktmbuf_mtod_offset(m[4], struct ipv6_hdr *,
1203 sizeof(struct ether_hdr));
1204 ipv6_hdr[5] = rte_pktmbuf_mtod_offset(m[5], struct ipv6_hdr *,
1205 sizeof(struct ether_hdr));
1206 ipv6_hdr[6] = rte_pktmbuf_mtod_offset(m[6], struct ipv6_hdr *,
1207 sizeof(struct ether_hdr));
1208 ipv6_hdr[7] = rte_pktmbuf_mtod_offset(m[7], struct ipv6_hdr *,
1209 sizeof(struct ether_hdr));
1211 get_ipv6_5tuple(m[0], mask1, mask2, &key[0]);
1212 get_ipv6_5tuple(m[1], mask1, mask2, &key[1]);
1213 get_ipv6_5tuple(m[2], mask1, mask2, &key[2]);
1214 get_ipv6_5tuple(m[3], mask1, mask2, &key[3]);
1215 get_ipv6_5tuple(m[4], mask1, mask2, &key[4]);
1216 get_ipv6_5tuple(m[5], mask1, mask2, &key[5]);
1217 get_ipv6_5tuple(m[6], mask1, mask2, &key[6]);
1218 get_ipv6_5tuple(m[7], mask1, mask2, &key[7]);
1220 const void *key_array[8] = {&key[0], &key[1], &key[2], &key[3],
1221 &key[4], &key[5], &key[6], &key[7]};
1223 rte_hash_lookup_bulk(RTE_PER_LCORE(lcore_conf)->ipv6_lookup_struct,
1224 &key_array[0], 4, ret);
1225 dst_port[0] = (uint8_t) ((ret[0] < 0) ? portid : ipv6_l3fwd_out_if[ret[0]]);
1226 dst_port[1] = (uint8_t) ((ret[1] < 0) ? portid : ipv6_l3fwd_out_if[ret[1]]);
1227 dst_port[2] = (uint8_t) ((ret[2] < 0) ? portid : ipv6_l3fwd_out_if[ret[2]]);
1228 dst_port[3] = (uint8_t) ((ret[3] < 0) ? portid : ipv6_l3fwd_out_if[ret[3]]);
1229 dst_port[4] = (uint8_t) ((ret[4] < 0) ? portid : ipv6_l3fwd_out_if[ret[4]]);
1230 dst_port[5] = (uint8_t) ((ret[5] < 0) ? portid : ipv6_l3fwd_out_if[ret[5]]);
1231 dst_port[6] = (uint8_t) ((ret[6] < 0) ? portid : ipv6_l3fwd_out_if[ret[6]]);
1232 dst_port[7] = (uint8_t) ((ret[7] < 0) ? portid : ipv6_l3fwd_out_if[ret[7]]);
1234 if (dst_port[0] >= RTE_MAX_ETHPORTS ||
1235 (enabled_port_mask & 1 << dst_port[0]) == 0)
1236 dst_port[0] = portid;
1237 if (dst_port[1] >= RTE_MAX_ETHPORTS ||
1238 (enabled_port_mask & 1 << dst_port[1]) == 0)
1239 dst_port[1] = portid;
1240 if (dst_port[2] >= RTE_MAX_ETHPORTS ||
1241 (enabled_port_mask & 1 << dst_port[2]) == 0)
1242 dst_port[2] = portid;
1243 if (dst_port[3] >= RTE_MAX_ETHPORTS ||
1244 (enabled_port_mask & 1 << dst_port[3]) == 0)
1245 dst_port[3] = portid;
1246 if (dst_port[4] >= RTE_MAX_ETHPORTS ||
1247 (enabled_port_mask & 1 << dst_port[4]) == 0)
1248 dst_port[4] = portid;
1249 if (dst_port[5] >= RTE_MAX_ETHPORTS ||
1250 (enabled_port_mask & 1 << dst_port[5]) == 0)
1251 dst_port[5] = portid;
1252 if (dst_port[6] >= RTE_MAX_ETHPORTS ||
1253 (enabled_port_mask & 1 << dst_port[6]) == 0)
1254 dst_port[6] = portid;
1255 if (dst_port[7] >= RTE_MAX_ETHPORTS ||
1256 (enabled_port_mask & 1 << dst_port[7]) == 0)
1257 dst_port[7] = portid;
1260 *(uint64_t *)ð_hdr[0]->d_addr = dest_eth_addr[dst_port[0]];
1261 *(uint64_t *)ð_hdr[1]->d_addr = dest_eth_addr[dst_port[1]];
1262 *(uint64_t *)ð_hdr[2]->d_addr = dest_eth_addr[dst_port[2]];
1263 *(uint64_t *)ð_hdr[3]->d_addr = dest_eth_addr[dst_port[3]];
1264 *(uint64_t *)ð_hdr[4]->d_addr = dest_eth_addr[dst_port[4]];
1265 *(uint64_t *)ð_hdr[5]->d_addr = dest_eth_addr[dst_port[5]];
1266 *(uint64_t *)ð_hdr[6]->d_addr = dest_eth_addr[dst_port[6]];
1267 *(uint64_t *)ð_hdr[7]->d_addr = dest_eth_addr[dst_port[7]];
1270 ether_addr_copy(&ports_eth_addr[dst_port[0]], ð_hdr[0]->s_addr);
1271 ether_addr_copy(&ports_eth_addr[dst_port[1]], ð_hdr[1]->s_addr);
1272 ether_addr_copy(&ports_eth_addr[dst_port[2]], ð_hdr[2]->s_addr);
1273 ether_addr_copy(&ports_eth_addr[dst_port[3]], ð_hdr[3]->s_addr);
1274 ether_addr_copy(&ports_eth_addr[dst_port[4]], ð_hdr[4]->s_addr);
1275 ether_addr_copy(&ports_eth_addr[dst_port[5]], ð_hdr[5]->s_addr);
1276 ether_addr_copy(&ports_eth_addr[dst_port[6]], ð_hdr[6]->s_addr);
1277 ether_addr_copy(&ports_eth_addr[dst_port[7]], ð_hdr[7]->s_addr);
1279 send_single_packet(m[0], (uint8_t)dst_port[0]);
1280 send_single_packet(m[1], (uint8_t)dst_port[1]);
1281 send_single_packet(m[2], (uint8_t)dst_port[2]);
1282 send_single_packet(m[3], (uint8_t)dst_port[3]);
1283 send_single_packet(m[4], (uint8_t)dst_port[4]);
1284 send_single_packet(m[5], (uint8_t)dst_port[5]);
1285 send_single_packet(m[6], (uint8_t)dst_port[6]);
1286 send_single_packet(m[7], (uint8_t)dst_port[7]);
1289 #endif /* APP_LOOKUP_METHOD */
1291 static __rte_always_inline void
1292 l3fwd_simple_forward(struct rte_mbuf *m, uint8_t portid)
1294 struct ether_hdr *eth_hdr;
1295 struct ipv4_hdr *ipv4_hdr;
1298 eth_hdr = rte_pktmbuf_mtod(m, struct ether_hdr *);
1300 if (RTE_ETH_IS_IPV4_HDR(m->packet_type)) {
1301 /* Handle IPv4 headers.*/
1302 ipv4_hdr = rte_pktmbuf_mtod_offset(m, struct ipv4_hdr *,
1303 sizeof(struct ether_hdr));
1305 #ifdef DO_RFC_1812_CHECKS
1306 /* Check to make sure the packet is valid (RFC1812) */
1307 if (is_valid_ipv4_pkt(ipv4_hdr, m->pkt_len) < 0) {
1308 rte_pktmbuf_free(m);
1313 dst_port = get_ipv4_dst_port(ipv4_hdr, portid,
1314 RTE_PER_LCORE(lcore_conf)->ipv4_lookup_struct);
1315 if (dst_port >= RTE_MAX_ETHPORTS ||
1316 (enabled_port_mask & 1 << dst_port) == 0)
1319 #ifdef DO_RFC_1812_CHECKS
1320 /* Update time to live and header checksum */
1321 --(ipv4_hdr->time_to_live);
1322 ++(ipv4_hdr->hdr_checksum);
1325 *(uint64_t *)ð_hdr->d_addr = dest_eth_addr[dst_port];
1328 ether_addr_copy(&ports_eth_addr[dst_port], ð_hdr->s_addr);
1330 send_single_packet(m, dst_port);
1331 } else if (RTE_ETH_IS_IPV6_HDR(m->packet_type)) {
1332 /* Handle IPv6 headers.*/
1333 struct ipv6_hdr *ipv6_hdr;
1335 ipv6_hdr = rte_pktmbuf_mtod_offset(m, struct ipv6_hdr *,
1336 sizeof(struct ether_hdr));
1338 dst_port = get_ipv6_dst_port(ipv6_hdr, portid,
1339 RTE_PER_LCORE(lcore_conf)->ipv6_lookup_struct);
1341 if (dst_port >= RTE_MAX_ETHPORTS ||
1342 (enabled_port_mask & 1 << dst_port) == 0)
1346 *(uint64_t *)ð_hdr->d_addr = dest_eth_addr[dst_port];
1349 ether_addr_copy(&ports_eth_addr[dst_port], ð_hdr->s_addr);
1351 send_single_packet(m, dst_port);
1353 /* Free the mbuf that contains non-IPV4/IPV6 packet */
1354 rte_pktmbuf_free(m);
1357 #if ((APP_LOOKUP_METHOD == APP_LOOKUP_LPM) && \
1358 (ENABLE_MULTI_BUFFER_OPTIMIZE == 1))
1359 #ifdef DO_RFC_1812_CHECKS
1361 #define IPV4_MIN_VER_IHL 0x45
1362 #define IPV4_MAX_VER_IHL 0x4f
1363 #define IPV4_MAX_VER_IHL_DIFF (IPV4_MAX_VER_IHL - IPV4_MIN_VER_IHL)
1365 /* Minimum value of IPV4 total length (20B) in network byte order. */
1366 #define IPV4_MIN_LEN_BE (sizeof(struct ipv4_hdr) << 8)
1369 * From http://www.rfc-editor.org/rfc/rfc1812.txt section 5.2.2:
1370 * - The IP version number must be 4.
1371 * - The IP header length field must be large enough to hold the
1372 * minimum length legal IP datagram (20 bytes = 5 words).
1373 * - The IP total length field must be large enough to hold the IP
1374 * datagram header, whose length is specified in the IP header length
1376 * If we encounter invalid IPV4 packet, then set destination port for it
1377 * to BAD_PORT value.
1379 static __rte_always_inline void
1380 rfc1812_process(struct ipv4_hdr *ipv4_hdr, uint16_t *dp, uint32_t ptype)
1384 if (RTE_ETH_IS_IPV4_HDR(ptype)) {
1385 ihl = ipv4_hdr->version_ihl - IPV4_MIN_VER_IHL;
1387 ipv4_hdr->time_to_live--;
1388 ipv4_hdr->hdr_checksum++;
1390 if (ihl > IPV4_MAX_VER_IHL_DIFF ||
1391 ((uint8_t)ipv4_hdr->total_length == 0 &&
1392 ipv4_hdr->total_length < IPV4_MIN_LEN_BE)) {
1399 #define rfc1812_process(mb, dp, ptype) do { } while (0)
1400 #endif /* DO_RFC_1812_CHECKS */
1401 #endif /* APP_LOOKUP_LPM && ENABLE_MULTI_BUFFER_OPTIMIZE */
1404 #if ((APP_LOOKUP_METHOD == APP_LOOKUP_LPM) && \
1405 (ENABLE_MULTI_BUFFER_OPTIMIZE == 1))
1407 static __rte_always_inline uint16_t
1408 get_dst_port(struct rte_mbuf *pkt, uint32_t dst_ipv4, uint8_t portid)
1411 struct ipv6_hdr *ipv6_hdr;
1412 struct ether_hdr *eth_hdr;
1414 if (RTE_ETH_IS_IPV4_HDR(pkt->packet_type)) {
1415 return (uint16_t) ((rte_lpm_lookup(
1416 RTE_PER_LCORE(lcore_conf)->ipv4_lookup_struct, dst_ipv4,
1417 &next_hop) == 0) ? next_hop : portid);
1419 } else if (RTE_ETH_IS_IPV6_HDR(pkt->packet_type)) {
1421 eth_hdr = rte_pktmbuf_mtod(pkt, struct ether_hdr *);
1422 ipv6_hdr = (struct ipv6_hdr *)(eth_hdr + 1);
1424 return (uint16_t) ((rte_lpm6_lookup(
1425 RTE_PER_LCORE(lcore_conf)->ipv6_lookup_struct,
1426 ipv6_hdr->dst_addr, &next_hop) == 0) ?
1435 process_packet(struct rte_mbuf *pkt, uint16_t *dst_port, uint8_t portid)
1437 struct ether_hdr *eth_hdr;
1438 struct ipv4_hdr *ipv4_hdr;
1443 eth_hdr = rte_pktmbuf_mtod(pkt, struct ether_hdr *);
1444 ipv4_hdr = (struct ipv4_hdr *)(eth_hdr + 1);
1446 dst_ipv4 = ipv4_hdr->dst_addr;
1447 dst_ipv4 = rte_be_to_cpu_32(dst_ipv4);
1448 dp = get_dst_port(pkt, dst_ipv4, portid);
1450 te = _mm_load_si128((__m128i *)eth_hdr);
1454 rfc1812_process(ipv4_hdr, dst_port, pkt->packet_type);
1456 te = _mm_blend_epi16(te, ve, MASK_ETH);
1457 _mm_store_si128((__m128i *)eth_hdr, te);
1461 * Read packet_type and destination IPV4 addresses from 4 mbufs.
1464 processx4_step1(struct rte_mbuf *pkt[FWDSTEP],
1466 uint32_t *ipv4_flag)
1468 struct ipv4_hdr *ipv4_hdr;
1469 struct ether_hdr *eth_hdr;
1470 uint32_t x0, x1, x2, x3;
1472 eth_hdr = rte_pktmbuf_mtod(pkt[0], struct ether_hdr *);
1473 ipv4_hdr = (struct ipv4_hdr *)(eth_hdr + 1);
1474 x0 = ipv4_hdr->dst_addr;
1475 ipv4_flag[0] = pkt[0]->packet_type & RTE_PTYPE_L3_IPV4;
1477 eth_hdr = rte_pktmbuf_mtod(pkt[1], struct ether_hdr *);
1478 ipv4_hdr = (struct ipv4_hdr *)(eth_hdr + 1);
1479 x1 = ipv4_hdr->dst_addr;
1480 ipv4_flag[0] &= pkt[1]->packet_type;
1482 eth_hdr = rte_pktmbuf_mtod(pkt[2], struct ether_hdr *);
1483 ipv4_hdr = (struct ipv4_hdr *)(eth_hdr + 1);
1484 x2 = ipv4_hdr->dst_addr;
1485 ipv4_flag[0] &= pkt[2]->packet_type;
1487 eth_hdr = rte_pktmbuf_mtod(pkt[3], struct ether_hdr *);
1488 ipv4_hdr = (struct ipv4_hdr *)(eth_hdr + 1);
1489 x3 = ipv4_hdr->dst_addr;
1490 ipv4_flag[0] &= pkt[3]->packet_type;
1492 dip[0] = _mm_set_epi32(x3, x2, x1, x0);
1496 * Lookup into LPM for destination port.
1497 * If lookup fails, use incoming port (portid) as destination port.
1500 processx4_step2(__m128i dip,
1503 struct rte_mbuf *pkt[FWDSTEP],
1504 uint16_t dprt[FWDSTEP])
1507 const __m128i bswap_mask = _mm_set_epi8(12, 13, 14, 15, 8, 9, 10, 11,
1508 4, 5, 6, 7, 0, 1, 2, 3);
1510 /* Byte swap 4 IPV4 addresses. */
1511 dip = _mm_shuffle_epi8(dip, bswap_mask);
1513 /* if all 4 packets are IPV4. */
1514 if (likely(ipv4_flag)) {
1515 rte_lpm_lookupx4(RTE_PER_LCORE(lcore_conf)->ipv4_lookup_struct, dip,
1518 /* get rid of unused upper 16 bit for each dport. */
1519 dst.x = _mm_packs_epi32(dst.x, dst.x);
1520 *(uint64_t *)dprt = dst.u64[0];
1523 dprt[0] = get_dst_port(pkt[0], dst.u32[0], portid);
1524 dprt[1] = get_dst_port(pkt[1], dst.u32[1], portid);
1525 dprt[2] = get_dst_port(pkt[2], dst.u32[2], portid);
1526 dprt[3] = get_dst_port(pkt[3], dst.u32[3], portid);
1531 * Update source and destination MAC addresses in the ethernet header.
1532 * Perform RFC1812 checks and updates for IPV4 packets.
1535 processx4_step3(struct rte_mbuf *pkt[FWDSTEP], uint16_t dst_port[FWDSTEP])
1537 __m128i te[FWDSTEP];
1538 __m128i ve[FWDSTEP];
1539 __m128i *p[FWDSTEP];
1541 p[0] = rte_pktmbuf_mtod(pkt[0], __m128i *);
1542 p[1] = rte_pktmbuf_mtod(pkt[1], __m128i *);
1543 p[2] = rte_pktmbuf_mtod(pkt[2], __m128i *);
1544 p[3] = rte_pktmbuf_mtod(pkt[3], __m128i *);
1546 ve[0] = val_eth[dst_port[0]];
1547 te[0] = _mm_load_si128(p[0]);
1549 ve[1] = val_eth[dst_port[1]];
1550 te[1] = _mm_load_si128(p[1]);
1552 ve[2] = val_eth[dst_port[2]];
1553 te[2] = _mm_load_si128(p[2]);
1555 ve[3] = val_eth[dst_port[3]];
1556 te[3] = _mm_load_si128(p[3]);
1558 /* Update first 12 bytes, keep rest bytes intact. */
1559 te[0] = _mm_blend_epi16(te[0], ve[0], MASK_ETH);
1560 te[1] = _mm_blend_epi16(te[1], ve[1], MASK_ETH);
1561 te[2] = _mm_blend_epi16(te[2], ve[2], MASK_ETH);
1562 te[3] = _mm_blend_epi16(te[3], ve[3], MASK_ETH);
1564 _mm_store_si128(p[0], te[0]);
1565 _mm_store_si128(p[1], te[1]);
1566 _mm_store_si128(p[2], te[2]);
1567 _mm_store_si128(p[3], te[3]);
1569 rfc1812_process((struct ipv4_hdr *)((struct ether_hdr *)p[0] + 1),
1570 &dst_port[0], pkt[0]->packet_type);
1571 rfc1812_process((struct ipv4_hdr *)((struct ether_hdr *)p[1] + 1),
1572 &dst_port[1], pkt[1]->packet_type);
1573 rfc1812_process((struct ipv4_hdr *)((struct ether_hdr *)p[2] + 1),
1574 &dst_port[2], pkt[2]->packet_type);
1575 rfc1812_process((struct ipv4_hdr *)((struct ether_hdr *)p[3] + 1),
1576 &dst_port[3], pkt[3]->packet_type);
1580 * We group consecutive packets with the same destionation port into one burst.
1581 * To avoid extra latency this is done together with some other packet
1582 * processing, but after we made a final decision about packet's destination.
1583 * To do this we maintain:
1584 * pnum - array of number of consecutive packets with the same dest port for
1585 * each packet in the input burst.
1586 * lp - pointer to the last updated element in the pnum.
1587 * dlp - dest port value lp corresponds to.
1590 #define GRPSZ (1 << FWDSTEP)
1591 #define GRPMSK (GRPSZ - 1)
1593 #define GROUP_PORT_STEP(dlp, dcp, lp, pn, idx) do { \
1594 if (likely((dlp) == (dcp)[(idx)])) { \
1597 (dlp) = (dcp)[idx]; \
1598 (lp) = (pn) + (idx); \
1604 * Group consecutive packets with the same destination port in bursts of 4.
1605 * Suppose we have array of destionation ports:
1606 * dst_port[] = {a, b, c, d,, e, ... }
1607 * dp1 should contain: <a, b, c, d>, dp2: <b, c, d, e>.
1608 * We doing 4 comparisons at once and the result is 4 bit mask.
1609 * This mask is used as an index into prebuild array of pnum values.
1611 static inline uint16_t *
1612 port_groupx4(uint16_t pn[FWDSTEP + 1], uint16_t *lp, __m128i dp1, __m128i dp2)
1614 static const struct {
1615 uint64_t pnum; /* prebuild 4 values for pnum[]. */
1616 int32_t idx; /* index for new last updated elemnet. */
1617 uint16_t lpv; /* add value to the last updated element. */
1620 /* 0: a != b, b != c, c != d, d != e */
1621 .pnum = UINT64_C(0x0001000100010001),
1626 /* 1: a == b, b != c, c != d, d != e */
1627 .pnum = UINT64_C(0x0001000100010002),
1632 /* 2: a != b, b == c, c != d, d != e */
1633 .pnum = UINT64_C(0x0001000100020001),
1638 /* 3: a == b, b == c, c != d, d != e */
1639 .pnum = UINT64_C(0x0001000100020003),
1644 /* 4: a != b, b != c, c == d, d != e */
1645 .pnum = UINT64_C(0x0001000200010001),
1650 /* 5: a == b, b != c, c == d, d != e */
1651 .pnum = UINT64_C(0x0001000200010002),
1656 /* 6: a != b, b == c, c == d, d != e */
1657 .pnum = UINT64_C(0x0001000200030001),
1662 /* 7: a == b, b == c, c == d, d != e */
1663 .pnum = UINT64_C(0x0001000200030004),
1668 /* 8: a != b, b != c, c != d, d == e */
1669 .pnum = UINT64_C(0x0002000100010001),
1674 /* 9: a == b, b != c, c != d, d == e */
1675 .pnum = UINT64_C(0x0002000100010002),
1680 /* 0xa: a != b, b == c, c != d, d == e */
1681 .pnum = UINT64_C(0x0002000100020001),
1686 /* 0xb: a == b, b == c, c != d, d == e */
1687 .pnum = UINT64_C(0x0002000100020003),
1692 /* 0xc: a != b, b != c, c == d, d == e */
1693 .pnum = UINT64_C(0x0002000300010001),
1698 /* 0xd: a == b, b != c, c == d, d == e */
1699 .pnum = UINT64_C(0x0002000300010002),
1704 /* 0xe: a != b, b == c, c == d, d == e */
1705 .pnum = UINT64_C(0x0002000300040001),
1710 /* 0xf: a == b, b == c, c == d, d == e */
1711 .pnum = UINT64_C(0x0002000300040005),
1718 uint16_t u16[FWDSTEP + 1];
1720 } *pnum = (void *)pn;
1724 dp1 = _mm_cmpeq_epi16(dp1, dp2);
1725 dp1 = _mm_unpacklo_epi16(dp1, dp1);
1726 v = _mm_movemask_ps((__m128)dp1);
1728 /* update last port counter. */
1729 lp[0] += gptbl[v].lpv;
1731 /* if dest port value has changed. */
1733 pnum->u64 = gptbl[v].pnum;
1734 pnum->u16[FWDSTEP] = 1;
1735 lp = pnum->u16 + gptbl[v].idx;
1741 #endif /* APP_LOOKUP_METHOD */
1744 process_burst(struct rte_mbuf *pkts_burst[MAX_PKT_BURST], int nb_rx,
1749 #if ((APP_LOOKUP_METHOD == APP_LOOKUP_LPM) && \
1750 (ENABLE_MULTI_BUFFER_OPTIMIZE == 1))
1754 uint16_t dst_port[MAX_PKT_BURST];
1755 __m128i dip[MAX_PKT_BURST / FWDSTEP];
1756 uint32_t ipv4_flag[MAX_PKT_BURST / FWDSTEP];
1757 uint16_t pnum[MAX_PKT_BURST + 1];
1761 #if (ENABLE_MULTI_BUFFER_OPTIMIZE == 1)
1762 #if (APP_LOOKUP_METHOD == APP_LOOKUP_EXACT_MATCH)
1765 * Send nb_rx - nb_rx%8 packets
1768 int32_t n = RTE_ALIGN_FLOOR(nb_rx, 8);
1770 for (j = 0; j < n; j += 8) {
1772 pkts_burst[j]->packet_type &
1773 pkts_burst[j+1]->packet_type &
1774 pkts_burst[j+2]->packet_type &
1775 pkts_burst[j+3]->packet_type &
1776 pkts_burst[j+4]->packet_type &
1777 pkts_burst[j+5]->packet_type &
1778 pkts_burst[j+6]->packet_type &
1779 pkts_burst[j+7]->packet_type;
1780 if (pkt_type & RTE_PTYPE_L3_IPV4) {
1781 simple_ipv4_fwd_8pkts(&pkts_burst[j], portid);
1782 } else if (pkt_type &
1783 RTE_PTYPE_L3_IPV6) {
1784 simple_ipv6_fwd_8pkts(&pkts_burst[j], portid);
1786 l3fwd_simple_forward(pkts_burst[j], portid);
1787 l3fwd_simple_forward(pkts_burst[j+1], portid);
1788 l3fwd_simple_forward(pkts_burst[j+2], portid);
1789 l3fwd_simple_forward(pkts_burst[j+3], portid);
1790 l3fwd_simple_forward(pkts_burst[j+4], portid);
1791 l3fwd_simple_forward(pkts_burst[j+5], portid);
1792 l3fwd_simple_forward(pkts_burst[j+6], portid);
1793 l3fwd_simple_forward(pkts_burst[j+7], portid);
1796 for (; j < nb_rx ; j++)
1797 l3fwd_simple_forward(pkts_burst[j], portid);
1799 #elif (APP_LOOKUP_METHOD == APP_LOOKUP_LPM)
1801 k = RTE_ALIGN_FLOOR(nb_rx, FWDSTEP);
1802 for (j = 0; j != k; j += FWDSTEP)
1803 processx4_step1(&pkts_burst[j], &dip[j / FWDSTEP],
1804 &ipv4_flag[j / FWDSTEP]);
1806 k = RTE_ALIGN_FLOOR(nb_rx, FWDSTEP);
1807 for (j = 0; j != k; j += FWDSTEP)
1808 processx4_step2(dip[j / FWDSTEP], ipv4_flag[j / FWDSTEP],
1809 portid, &pkts_burst[j], &dst_port[j]);
1812 * Finish packet processing and group consecutive
1813 * packets with the same destination port.
1815 k = RTE_ALIGN_FLOOR(nb_rx, FWDSTEP);
1822 processx4_step3(pkts_burst, dst_port);
1824 /* dp1: <d[0], d[1], d[2], d[3], ... > */
1825 dp1 = _mm_loadu_si128((__m128i *)dst_port);
1827 for (j = FWDSTEP; j != k; j += FWDSTEP) {
1828 processx4_step3(&pkts_burst[j], &dst_port[j]);
1832 * <d[j-3], d[j-2], d[j-1], d[j], ... >
1834 dp2 = _mm_loadu_si128(
1835 (__m128i *)&dst_port[j - FWDSTEP + 1]);
1836 lp = port_groupx4(&pnum[j - FWDSTEP], lp, dp1, dp2);
1840 * <d[j], d[j+1], d[j+2], d[j+3], ... >
1842 dp1 = _mm_srli_si128(dp2, (FWDSTEP - 1) *
1843 sizeof(dst_port[0]));
1847 * dp2: <d[j-3], d[j-2], d[j-1], d[j-1], ... >
1849 dp2 = _mm_shufflelo_epi16(dp1, 0xf9);
1850 lp = port_groupx4(&pnum[j - FWDSTEP], lp, dp1, dp2);
1853 * remove values added by the last repeated
1857 dlp = dst_port[j - 1];
1859 /* set dlp and lp to the never used values. */
1861 lp = pnum + MAX_PKT_BURST;
1864 /* Process up to last 3 packets one by one. */
1865 switch (nb_rx % FWDSTEP) {
1867 process_packet(pkts_burst[j], dst_port + j, portid);
1868 GROUP_PORT_STEP(dlp, dst_port, lp, pnum, j);
1872 process_packet(pkts_burst[j], dst_port + j, portid);
1873 GROUP_PORT_STEP(dlp, dst_port, lp, pnum, j);
1877 process_packet(pkts_burst[j], dst_port + j, portid);
1878 GROUP_PORT_STEP(dlp, dst_port, lp, pnum, j);
1883 * Send packets out, through destination port.
1884 * Consecuteve pacekts with the same destination port
1885 * are already grouped together.
1886 * If destination port for the packet equals BAD_PORT,
1887 * then free the packet without sending it out.
1889 for (j = 0; j < nb_rx; j += k) {
1897 if (likely(pn != BAD_PORT))
1898 send_packetsx4(pn, pkts_burst + j, k);
1900 for (m = j; m != j + k; m++)
1901 rte_pktmbuf_free(pkts_burst[m]);
1905 #endif /* APP_LOOKUP_METHOD */
1906 #else /* ENABLE_MULTI_BUFFER_OPTIMIZE == 0 */
1908 /* Prefetch first packets */
1909 for (j = 0; j < PREFETCH_OFFSET && j < nb_rx; j++)
1910 rte_prefetch0(rte_pktmbuf_mtod(pkts_burst[j], void *));
1912 /* Prefetch and forward already prefetched packets */
1913 for (j = 0; j < (nb_rx - PREFETCH_OFFSET); j++) {
1914 rte_prefetch0(rte_pktmbuf_mtod(pkts_burst[
1915 j + PREFETCH_OFFSET], void *));
1916 l3fwd_simple_forward(pkts_burst[j], portid);
1919 /* Forward remaining prefetched packets */
1920 for (; j < nb_rx; j++)
1921 l3fwd_simple_forward(pkts_burst[j], portid);
1923 #endif /* ENABLE_MULTI_BUFFER_OPTIMIZE */
1927 #if (APP_CPU_LOAD > 0)
1930 * CPU-load stats collector
1933 cpu_load_collector(__rte_unused void *arg) {
1936 uint64_t prev_tsc, diff_tsc, cur_tsc;
1937 uint64_t total[MAX_CPU] = { 0 };
1938 unsigned min_cpu = MAX_CPU;
1939 unsigned max_cpu = 0;
1944 unsigned int n_thread_per_cpu[MAX_CPU] = { 0 };
1945 struct thread_conf *thread_per_cpu[MAX_CPU][MAX_THREAD];
1947 struct thread_conf *thread_conf;
1949 const uint64_t interval_tsc = (rte_get_tsc_hz() + US_PER_S - 1) /
1950 US_PER_S * CPU_LOAD_TIMEOUT_US;
1954 * Wait for all threads
1957 printf("Waiting for %d rx threads and %d tx threads\n", n_rx_thread,
1960 while (rte_atomic16_read(&rx_counter) < n_rx_thread)
1963 while (rte_atomic16_read(&tx_counter) < n_tx_thread)
1966 for (i = 0; i < n_rx_thread; i++) {
1968 thread_conf = &rx_thread[i].conf;
1969 cpu_id = thread_conf->cpu_id;
1970 thread_per_cpu[cpu_id][n_thread_per_cpu[cpu_id]++] = thread_conf;
1972 if (cpu_id > max_cpu)
1974 if (cpu_id < min_cpu)
1977 for (i = 0; i < n_tx_thread; i++) {
1979 thread_conf = &tx_thread[i].conf;
1980 cpu_id = thread_conf->cpu_id;
1981 thread_per_cpu[cpu_id][n_thread_per_cpu[cpu_id]++] = thread_conf;
1983 if (thread_conf->cpu_id > max_cpu)
1984 max_cpu = thread_conf->cpu_id;
1985 if (thread_conf->cpu_id < min_cpu)
1986 min_cpu = thread_conf->cpu_id;
1992 for (i = min_cpu; i <= max_cpu; i++) {
1993 for (j = 0; j < MAX_CPU_COUNTER; j++) {
1994 for (k = 0; k < n_thread_per_cpu[i]; k++)
1995 if (thread_per_cpu[i][k]->busy[j]) {
2000 cpu_load.hits[j][i]++;
2012 cur_tsc = rte_rdtsc();
2014 diff_tsc = cur_tsc - prev_tsc;
2015 if (unlikely(diff_tsc > interval_tsc)) {
2019 printf("Cpu usage for %d rx threads and %d tx threads:\n\n",
2020 n_rx_thread, n_tx_thread);
2022 printf("cpu# proc%% poll%% overhead%%\n\n");
2024 for (i = min_cpu; i <= max_cpu; i++) {
2026 printf("CPU %d:", i);
2027 for (j = 0; j < MAX_CPU_COUNTER; j++) {
2028 printf("%7" PRIu64 "",
2029 cpu_load.hits[j][i] * 100 / cpu_load.counter);
2030 hits += cpu_load.hits[j][i];
2031 cpu_load.hits[j][i] = 0;
2033 printf("%7" PRIu64 "\n",
2034 100 - total[i] * 100 / cpu_load.counter);
2037 cpu_load.counter = 0;
2044 #endif /* APP_CPU_LOAD */
2047 * Null processing lthread loop
2049 * This loop is used to start empty scheduler on lcore.
2052 lthread_null(__rte_unused void *args)
2054 int lcore_id = rte_lcore_id();
2056 RTE_LOG(INFO, L3FWD, "Starting scheduler on lcore %d.\n", lcore_id);
2060 /* main processing loop */
2062 lthread_tx_per_ring(void *dummy)
2066 struct rte_ring *ring;
2067 struct thread_tx_conf *tx_conf;
2068 struct rte_mbuf *pkts_burst[MAX_PKT_BURST];
2069 struct lthread_cond *ready;
2071 tx_conf = (struct thread_tx_conf *)dummy;
2072 ring = tx_conf->ring;
2073 ready = *tx_conf->ready;
2075 lthread_set_data((void *)tx_conf);
2078 * Move this lthread to lcore
2080 lthread_set_affinity(tx_conf->conf.lcore_id);
2082 RTE_LOG(INFO, L3FWD, "entering main tx loop on lcore %u\n", rte_lcore_id());
2085 rte_atomic16_inc(&tx_counter);
2089 * Read packet from ring
2091 SET_CPU_BUSY(tx_conf, CPU_POLL);
2092 nb_rx = rte_ring_sc_dequeue_burst(ring, (void **)pkts_burst,
2093 MAX_PKT_BURST, NULL);
2094 SET_CPU_IDLE(tx_conf, CPU_POLL);
2097 SET_CPU_BUSY(tx_conf, CPU_PROCESS);
2098 portid = pkts_burst[0]->port;
2099 process_burst(pkts_burst, nb_rx, portid);
2100 SET_CPU_IDLE(tx_conf, CPU_PROCESS);
2103 lthread_cond_wait(ready, 0);
2109 * Main tx-lthreads spawner lthread.
2111 * This lthread is used to spawn one new lthread per ring from producers.
2115 lthread_tx(void *args)
2121 struct thread_tx_conf *tx_conf;
2123 tx_conf = (struct thread_tx_conf *)args;
2124 lthread_set_data((void *)tx_conf);
2127 * Move this lthread to the selected lcore
2129 lthread_set_affinity(tx_conf->conf.lcore_id);
2132 * Spawn tx readers (one per input ring)
2134 lthread_create(<, tx_conf->conf.lcore_id, lthread_tx_per_ring,
2137 lcore_id = rte_lcore_id();
2139 RTE_LOG(INFO, L3FWD, "Entering Tx main loop on lcore %u\n", lcore_id);
2141 tx_conf->conf.cpu_id = sched_getcpu();
2144 lthread_sleep(BURST_TX_DRAIN_US * 1000);
2147 * TX burst queue drain
2149 for (portid = 0; portid < RTE_MAX_ETHPORTS; portid++) {
2150 if (tx_conf->tx_mbufs[portid].len == 0)
2152 SET_CPU_BUSY(tx_conf, CPU_PROCESS);
2153 send_burst(tx_conf, tx_conf->tx_mbufs[portid].len, portid);
2154 SET_CPU_IDLE(tx_conf, CPU_PROCESS);
2155 tx_conf->tx_mbufs[portid].len = 0;
2162 lthread_rx(void *dummy)
2167 uint8_t portid, queueid;
2169 int len[RTE_MAX_LCORE] = { 0 };
2170 int old_len, new_len;
2171 struct rte_mbuf *pkts_burst[MAX_PKT_BURST];
2172 struct thread_rx_conf *rx_conf;
2174 rx_conf = (struct thread_rx_conf *)dummy;
2175 lthread_set_data((void *)rx_conf);
2178 * Move this lthread to lcore
2180 lthread_set_affinity(rx_conf->conf.lcore_id);
2182 if (rx_conf->n_rx_queue == 0) {
2183 RTE_LOG(INFO, L3FWD, "lcore %u has nothing to do\n", rte_lcore_id());
2187 RTE_LOG(INFO, L3FWD, "Entering main Rx loop on lcore %u\n", rte_lcore_id());
2189 for (i = 0; i < rx_conf->n_rx_queue; i++) {
2191 portid = rx_conf->rx_queue_list[i].port_id;
2192 queueid = rx_conf->rx_queue_list[i].queue_id;
2193 RTE_LOG(INFO, L3FWD, " -- lcoreid=%u portid=%hhu rxqueueid=%hhu\n",
2194 rte_lcore_id(), portid, queueid);
2198 * Init all condition variables (one per rx thread)
2200 for (i = 0; i < rx_conf->n_rx_queue; i++)
2201 lthread_cond_init(NULL, &rx_conf->ready[i], NULL);
2205 rx_conf->conf.cpu_id = sched_getcpu();
2206 rte_atomic16_inc(&rx_counter);
2210 * Read packet from RX queues
2212 for (i = 0; i < rx_conf->n_rx_queue; ++i) {
2213 portid = rx_conf->rx_queue_list[i].port_id;
2214 queueid = rx_conf->rx_queue_list[i].queue_id;
2216 SET_CPU_BUSY(rx_conf, CPU_POLL);
2217 nb_rx = rte_eth_rx_burst(portid, queueid, pkts_burst,
2219 SET_CPU_IDLE(rx_conf, CPU_POLL);
2222 worker_id = (worker_id + 1) % rx_conf->n_ring;
2223 old_len = len[worker_id];
2225 SET_CPU_BUSY(rx_conf, CPU_PROCESS);
2226 ret = rte_ring_sp_enqueue_burst(
2227 rx_conf->ring[worker_id],
2228 (void **) pkts_burst,
2231 new_len = old_len + ret;
2233 if (new_len >= BURST_SIZE) {
2234 lthread_cond_signal(rx_conf->ready[worker_id]);
2238 len[worker_id] = new_len;
2240 if (unlikely(ret < nb_rx)) {
2243 for (k = ret; k < nb_rx; k++) {
2244 struct rte_mbuf *m = pkts_burst[k];
2246 rte_pktmbuf_free(m);
2249 SET_CPU_IDLE(rx_conf, CPU_PROCESS);
2258 * Start scheduler with initial lthread on lcore
2260 * This lthread loop spawns all rx and tx lthreads on master lcore
2264 lthread_spawner(__rte_unused void *arg) {
2265 struct lthread *lt[MAX_THREAD];
2269 printf("Entering lthread_spawner\n");
2272 * Create producers (rx threads) on default lcore
2274 for (i = 0; i < n_rx_thread; i++) {
2275 rx_thread[i].conf.thread_id = i;
2276 lthread_create(<[n_thread], -1, lthread_rx,
2277 (void *)&rx_thread[i]);
2282 * Wait for all producers. Until some producers can be started on the same
2283 * scheduler as this lthread, yielding is required to let them to run and
2284 * prevent deadlock here.
2286 while (rte_atomic16_read(&rx_counter) < n_rx_thread)
2287 lthread_sleep(100000);
2290 * Create consumers (tx threads) on default lcore_id
2292 for (i = 0; i < n_tx_thread; i++) {
2293 tx_thread[i].conf.thread_id = i;
2294 lthread_create(<[n_thread], -1, lthread_tx,
2295 (void *)&tx_thread[i]);
2300 * Wait for all threads finished
2302 for (i = 0; i < n_thread; i++)
2303 lthread_join(lt[i], NULL);
2308 * Start master scheduler with initial lthread spawning rx and tx lthreads
2309 * (main_lthread_master).
2312 lthread_master_spawner(__rte_unused void *arg) {
2314 int lcore_id = rte_lcore_id();
2316 RTE_PER_LCORE(lcore_conf) = &lcore_conf[lcore_id];
2317 lthread_create(<, -1, lthread_spawner, NULL);
2324 * Start scheduler on lcore.
2327 sched_spawner(__rte_unused void *arg) {
2329 int lcore_id = rte_lcore_id();
2332 if (lcore_id == cpu_load_lcore_id) {
2333 cpu_load_collector(arg);
2336 #endif /* APP_CPU_LOAD */
2338 RTE_PER_LCORE(lcore_conf) = &lcore_conf[lcore_id];
2339 lthread_create(<, -1, lthread_null, NULL);
2345 /* main processing loop */
2347 pthread_tx(void *dummy)
2349 struct rte_mbuf *pkts_burst[MAX_PKT_BURST];
2350 uint64_t prev_tsc, diff_tsc, cur_tsc;
2353 struct thread_tx_conf *tx_conf;
2355 const uint64_t drain_tsc = (rte_get_tsc_hz() + US_PER_S - 1) /
2356 US_PER_S * BURST_TX_DRAIN_US;
2360 tx_conf = (struct thread_tx_conf *)dummy;
2362 RTE_LOG(INFO, L3FWD, "Entering main Tx loop on lcore %u\n", rte_lcore_id());
2364 tx_conf->conf.cpu_id = sched_getcpu();
2365 rte_atomic16_inc(&tx_counter);
2368 cur_tsc = rte_rdtsc();
2371 * TX burst queue drain
2373 diff_tsc = cur_tsc - prev_tsc;
2374 if (unlikely(diff_tsc > drain_tsc)) {
2377 * This could be optimized (use queueid instead of
2378 * portid), but it is not called so often
2380 SET_CPU_BUSY(tx_conf, CPU_PROCESS);
2381 for (portid = 0; portid < RTE_MAX_ETHPORTS; portid++) {
2382 if (tx_conf->tx_mbufs[portid].len == 0)
2384 send_burst(tx_conf, tx_conf->tx_mbufs[portid].len, portid);
2385 tx_conf->tx_mbufs[portid].len = 0;
2387 SET_CPU_IDLE(tx_conf, CPU_PROCESS);
2393 * Read packet from ring
2395 SET_CPU_BUSY(tx_conf, CPU_POLL);
2396 nb_rx = rte_ring_sc_dequeue_burst(tx_conf->ring,
2397 (void **)pkts_burst, MAX_PKT_BURST, NULL);
2398 SET_CPU_IDLE(tx_conf, CPU_POLL);
2400 if (unlikely(nb_rx == 0)) {
2405 SET_CPU_BUSY(tx_conf, CPU_PROCESS);
2406 portid = pkts_burst[0]->port;
2407 process_burst(pkts_burst, nb_rx, portid);
2408 SET_CPU_IDLE(tx_conf, CPU_PROCESS);
2414 pthread_rx(void *dummy)
2421 uint8_t portid, queueid;
2422 struct rte_mbuf *pkts_burst[MAX_PKT_BURST];
2424 struct thread_rx_conf *rx_conf;
2426 lcore_id = rte_lcore_id();
2427 rx_conf = (struct thread_rx_conf *)dummy;
2429 if (rx_conf->n_rx_queue == 0) {
2430 RTE_LOG(INFO, L3FWD, "lcore %u has nothing to do\n", lcore_id);
2434 RTE_LOG(INFO, L3FWD, "entering main rx loop on lcore %u\n", lcore_id);
2436 for (i = 0; i < rx_conf->n_rx_queue; i++) {
2438 portid = rx_conf->rx_queue_list[i].port_id;
2439 queueid = rx_conf->rx_queue_list[i].queue_id;
2440 RTE_LOG(INFO, L3FWD, " -- lcoreid=%u portid=%hhu rxqueueid=%hhu\n",
2441 lcore_id, portid, queueid);
2445 rx_conf->conf.cpu_id = sched_getcpu();
2446 rte_atomic16_inc(&rx_counter);
2450 * Read packet from RX queues
2452 for (i = 0; i < rx_conf->n_rx_queue; ++i) {
2453 portid = rx_conf->rx_queue_list[i].port_id;
2454 queueid = rx_conf->rx_queue_list[i].queue_id;
2456 SET_CPU_BUSY(rx_conf, CPU_POLL);
2457 nb_rx = rte_eth_rx_burst(portid, queueid, pkts_burst,
2459 SET_CPU_IDLE(rx_conf, CPU_POLL);
2466 SET_CPU_BUSY(rx_conf, CPU_PROCESS);
2467 worker_id = (worker_id + 1) % rx_conf->n_ring;
2468 n = rte_ring_sp_enqueue_burst(rx_conf->ring[worker_id],
2469 (void **)pkts_burst, nb_rx, NULL);
2471 if (unlikely(n != nb_rx)) {
2474 for (k = n; k < nb_rx; k++) {
2475 struct rte_mbuf *m = pkts_burst[k];
2477 rte_pktmbuf_free(m);
2481 SET_CPU_IDLE(rx_conf, CPU_PROCESS);
2491 pthread_run(__rte_unused void *arg) {
2492 int lcore_id = rte_lcore_id();
2495 for (i = 0; i < n_rx_thread; i++)
2496 if (rx_thread[i].conf.lcore_id == lcore_id) {
2497 printf("Start rx thread on %d...\n", lcore_id);
2498 RTE_PER_LCORE(lcore_conf) = &lcore_conf[lcore_id];
2499 RTE_PER_LCORE(lcore_conf)->data = (void *)&rx_thread[i];
2500 pthread_rx((void *)&rx_thread[i]);
2504 for (i = 0; i < n_tx_thread; i++)
2505 if (tx_thread[i].conf.lcore_id == lcore_id) {
2506 printf("Start tx thread on %d...\n", lcore_id);
2507 RTE_PER_LCORE(lcore_conf) = &lcore_conf[lcore_id];
2508 RTE_PER_LCORE(lcore_conf)->data = (void *)&tx_thread[i];
2509 pthread_tx((void *)&tx_thread[i]);
2514 if (lcore_id == cpu_load_lcore_id)
2515 cpu_load_collector(arg);
2516 #endif /* APP_CPU_LOAD */
2522 check_lcore_params(void)
2524 uint8_t queue, lcore;
2528 for (i = 0; i < nb_rx_thread_params; ++i) {
2529 queue = rx_thread_params[i].queue_id;
2530 if (queue >= MAX_RX_QUEUE_PER_PORT) {
2531 printf("invalid queue number: %hhu\n", queue);
2534 lcore = rx_thread_params[i].lcore_id;
2535 if (!rte_lcore_is_enabled(lcore)) {
2536 printf("error: lcore %hhu is not enabled in lcore mask\n", lcore);
2539 socketid = rte_lcore_to_socket_id(lcore);
2540 if ((socketid != 0) && (numa_on == 0))
2541 printf("warning: lcore %hhu is on socket %d with numa off\n",
2548 check_port_config(const unsigned nb_ports)
2553 for (i = 0; i < nb_rx_thread_params; ++i) {
2554 portid = rx_thread_params[i].port_id;
2555 if ((enabled_port_mask & (1 << portid)) == 0) {
2556 printf("port %u is not enabled in port mask\n", portid);
2559 if (portid >= nb_ports) {
2560 printf("port %u is not present on the board\n", portid);
2568 get_port_n_rx_queues(const uint8_t port)
2573 for (i = 0; i < nb_rx_thread_params; ++i)
2574 if (rx_thread_params[i].port_id == port &&
2575 rx_thread_params[i].queue_id > queue)
2576 queue = rx_thread_params[i].queue_id;
2578 return (uint8_t)(++queue);
2585 struct thread_rx_conf *rx_conf;
2586 struct thread_tx_conf *tx_conf;
2587 unsigned rx_thread_id, tx_thread_id;
2589 struct rte_ring *ring = NULL;
2591 for (tx_thread_id = 0; tx_thread_id < n_tx_thread; tx_thread_id++) {
2593 tx_conf = &tx_thread[tx_thread_id];
2595 printf("Connecting tx-thread %d with rx-thread %d\n", tx_thread_id,
2596 tx_conf->conf.thread_id);
2598 rx_thread_id = tx_conf->conf.thread_id;
2599 if (rx_thread_id > n_tx_thread) {
2600 printf("connection from tx-thread %u to rx-thread %u fails "
2601 "(rx-thread not defined)\n", tx_thread_id, rx_thread_id);
2605 rx_conf = &rx_thread[rx_thread_id];
2606 socket_io = rte_lcore_to_socket_id(rx_conf->conf.lcore_id);
2608 snprintf(name, sizeof(name), "app_ring_s%u_rx%u_tx%u",
2609 socket_io, rx_thread_id, tx_thread_id);
2611 ring = rte_ring_create(name, 1024 * 4, socket_io,
2612 RING_F_SP_ENQ | RING_F_SC_DEQ);
2615 rte_panic("Cannot create ring to connect rx-thread %u "
2616 "with tx-thread %u\n", rx_thread_id, tx_thread_id);
2619 rx_conf->ring[rx_conf->n_ring] = ring;
2621 tx_conf->ring = ring;
2622 tx_conf->ready = &rx_conf->ready[rx_conf->n_ring];
2630 init_rx_queues(void)
2632 uint16_t i, nb_rx_queue;
2637 for (i = 0; i < nb_rx_thread_params; ++i) {
2638 thread = rx_thread_params[i].thread_id;
2639 nb_rx_queue = rx_thread[thread].n_rx_queue;
2641 if (nb_rx_queue >= MAX_RX_QUEUE_PER_LCORE) {
2642 printf("error: too many queues (%u) for thread: %u\n",
2643 (unsigned)nb_rx_queue + 1, (unsigned)thread);
2647 rx_thread[thread].conf.thread_id = thread;
2648 rx_thread[thread].conf.lcore_id = rx_thread_params[i].lcore_id;
2649 rx_thread[thread].rx_queue_list[nb_rx_queue].port_id =
2650 rx_thread_params[i].port_id;
2651 rx_thread[thread].rx_queue_list[nb_rx_queue].queue_id =
2652 rx_thread_params[i].queue_id;
2653 rx_thread[thread].n_rx_queue++;
2655 if (thread >= n_rx_thread)
2656 n_rx_thread = thread + 1;
2663 init_tx_threads(void)
2668 for (i = 0; i < nb_tx_thread_params; ++i) {
2669 tx_thread[n_tx_thread].conf.thread_id = tx_thread_params[i].thread_id;
2670 tx_thread[n_tx_thread].conf.lcore_id = tx_thread_params[i].lcore_id;
2678 print_usage(const char *prgname)
2680 printf("%s [EAL options] -- -p PORTMASK -P"
2681 " [--rx (port,queue,lcore,thread)[,(port,queue,lcore,thread]]"
2682 " [--tx (lcore,thread)[,(lcore,thread]]"
2683 " [--enable-jumbo [--max-pkt-len PKTLEN]]\n"
2684 " [--parse-ptype]\n\n"
2685 " -p PORTMASK: hexadecimal bitmask of ports to configure\n"
2686 " -P : enable promiscuous mode\n"
2687 " --rx (port,queue,lcore,thread): rx queues configuration\n"
2688 " --tx (lcore,thread): tx threads configuration\n"
2689 " --stat-lcore LCORE: use lcore for stat collector\n"
2690 " --eth-dest=X,MM:MM:MM:MM:MM:MM: optional, ethernet destination for port X\n"
2691 " --no-numa: optional, disable numa awareness\n"
2692 " --ipv6: optional, specify it if running ipv6 packets\n"
2693 " --enable-jumbo: enable jumbo frame"
2694 " which max packet len is PKTLEN in decimal (64-9600)\n"
2695 " --hash-entry-num: specify the hash entry number in hexadecimal to be setup\n"
2696 " --no-lthreads: turn off lthread model\n"
2697 " --parse-ptype: set to use software to analyze packet type\n\n",
2701 static int parse_max_pkt_len(const char *pktlen)
2706 /* parse decimal string */
2707 len = strtoul(pktlen, &end, 10);
2708 if ((pktlen[0] == '\0') || (end == NULL) || (*end != '\0'))
2718 parse_portmask(const char *portmask)
2723 /* parse hexadecimal string */
2724 pm = strtoul(portmask, &end, 16);
2725 if ((portmask[0] == '\0') || (end == NULL) || (*end != '\0'))
2734 #if (APP_LOOKUP_METHOD == APP_LOOKUP_EXACT_MATCH)
2736 parse_hash_entry_number(const char *hash_entry_num)
2739 unsigned long hash_en;
2741 /* parse hexadecimal string */
2742 hash_en = strtoul(hash_entry_num, &end, 16);
2743 if ((hash_entry_num[0] == '\0') || (end == NULL) || (*end != '\0'))
2754 parse_rx_config(const char *q_arg)
2757 const char *p, *p0 = q_arg;
2766 unsigned long int_fld[_NUM_FLD];
2767 char *str_fld[_NUM_FLD];
2771 nb_rx_thread_params = 0;
2773 while ((p = strchr(p0, '(')) != NULL) {
2775 p0 = strchr(p, ')');
2780 if (size >= sizeof(s))
2783 snprintf(s, sizeof(s), "%.*s", size, p);
2784 if (rte_strsplit(s, sizeof(s), str_fld, _NUM_FLD, ',') != _NUM_FLD)
2786 for (i = 0; i < _NUM_FLD; i++) {
2788 int_fld[i] = strtoul(str_fld[i], &end, 0);
2789 if (errno != 0 || end == str_fld[i] || int_fld[i] > 255)
2792 if (nb_rx_thread_params >= MAX_LCORE_PARAMS) {
2793 printf("exceeded max number of rx params: %hu\n",
2794 nb_rx_thread_params);
2797 rx_thread_params_array[nb_rx_thread_params].port_id =
2798 (uint8_t)int_fld[FLD_PORT];
2799 rx_thread_params_array[nb_rx_thread_params].queue_id =
2800 (uint8_t)int_fld[FLD_QUEUE];
2801 rx_thread_params_array[nb_rx_thread_params].lcore_id =
2802 (uint8_t)int_fld[FLD_LCORE];
2803 rx_thread_params_array[nb_rx_thread_params].thread_id =
2804 (uint8_t)int_fld[FLD_THREAD];
2805 ++nb_rx_thread_params;
2807 rx_thread_params = rx_thread_params_array;
2812 parse_tx_config(const char *q_arg)
2815 const char *p, *p0 = q_arg;
2822 unsigned long int_fld[_NUM_FLD];
2823 char *str_fld[_NUM_FLD];
2827 nb_tx_thread_params = 0;
2829 while ((p = strchr(p0, '(')) != NULL) {
2831 p0 = strchr(p, ')');
2836 if (size >= sizeof(s))
2839 snprintf(s, sizeof(s), "%.*s", size, p);
2840 if (rte_strsplit(s, sizeof(s), str_fld, _NUM_FLD, ',') != _NUM_FLD)
2842 for (i = 0; i < _NUM_FLD; i++) {
2844 int_fld[i] = strtoul(str_fld[i], &end, 0);
2845 if (errno != 0 || end == str_fld[i] || int_fld[i] > 255)
2848 if (nb_tx_thread_params >= MAX_LCORE_PARAMS) {
2849 printf("exceeded max number of tx params: %hu\n",
2850 nb_tx_thread_params);
2853 tx_thread_params_array[nb_tx_thread_params].lcore_id =
2854 (uint8_t)int_fld[FLD_LCORE];
2855 tx_thread_params_array[nb_tx_thread_params].thread_id =
2856 (uint8_t)int_fld[FLD_THREAD];
2857 ++nb_tx_thread_params;
2859 tx_thread_params = tx_thread_params_array;
2864 #if (APP_CPU_LOAD > 0)
2866 parse_stat_lcore(const char *stat_lcore)
2869 unsigned long lcore_id;
2871 lcore_id = strtoul(stat_lcore, &end, 10);
2872 if ((stat_lcore[0] == '\0') || (end == NULL) || (*end != '\0'))
2880 parse_eth_dest(const char *optarg)
2884 uint8_t c, *dest, peer_addr[6];
2887 portid = strtoul(optarg, &port_end, 10);
2888 if (errno != 0 || port_end == optarg || *port_end++ != ',')
2889 rte_exit(EXIT_FAILURE,
2890 "Invalid eth-dest: %s", optarg);
2891 if (portid >= RTE_MAX_ETHPORTS)
2892 rte_exit(EXIT_FAILURE,
2893 "eth-dest: port %d >= RTE_MAX_ETHPORTS(%d)\n",
2894 portid, RTE_MAX_ETHPORTS);
2896 if (cmdline_parse_etheraddr(NULL, port_end,
2897 &peer_addr, sizeof(peer_addr)) < 0)
2898 rte_exit(EXIT_FAILURE,
2899 "Invalid ethernet address: %s\n",
2901 dest = (uint8_t *)&dest_eth_addr[portid];
2902 for (c = 0; c < 6; c++)
2903 dest[c] = peer_addr[c];
2904 *(uint64_t *)(val_eth + portid) = dest_eth_addr[portid];
2907 #define CMD_LINE_OPT_RX_CONFIG "rx"
2908 #define CMD_LINE_OPT_TX_CONFIG "tx"
2909 #define CMD_LINE_OPT_STAT_LCORE "stat-lcore"
2910 #define CMD_LINE_OPT_ETH_DEST "eth-dest"
2911 #define CMD_LINE_OPT_NO_NUMA "no-numa"
2912 #define CMD_LINE_OPT_IPV6 "ipv6"
2913 #define CMD_LINE_OPT_ENABLE_JUMBO "enable-jumbo"
2914 #define CMD_LINE_OPT_HASH_ENTRY_NUM "hash-entry-num"
2915 #define CMD_LINE_OPT_NO_LTHREADS "no-lthreads"
2916 #define CMD_LINE_OPT_PARSE_PTYPE "parse-ptype"
2918 /* Parse the argument given in the command line of the application */
2920 parse_args(int argc, char **argv)
2925 char *prgname = argv[0];
2926 static struct option lgopts[] = {
2927 {CMD_LINE_OPT_RX_CONFIG, 1, 0, 0},
2928 {CMD_LINE_OPT_TX_CONFIG, 1, 0, 0},
2929 {CMD_LINE_OPT_STAT_LCORE, 1, 0, 0},
2930 {CMD_LINE_OPT_ETH_DEST, 1, 0, 0},
2931 {CMD_LINE_OPT_NO_NUMA, 0, 0, 0},
2932 {CMD_LINE_OPT_IPV6, 0, 0, 0},
2933 {CMD_LINE_OPT_ENABLE_JUMBO, 0, 0, 0},
2934 {CMD_LINE_OPT_HASH_ENTRY_NUM, 1, 0, 0},
2935 {CMD_LINE_OPT_NO_LTHREADS, 0, 0, 0},
2936 {CMD_LINE_OPT_PARSE_PTYPE, 0, 0, 0},
2942 while ((opt = getopt_long(argc, argvopt, "p:P",
2943 lgopts, &option_index)) != EOF) {
2948 enabled_port_mask = parse_portmask(optarg);
2949 if (enabled_port_mask == 0) {
2950 printf("invalid portmask\n");
2951 print_usage(prgname);
2956 printf("Promiscuous mode selected\n");
2962 if (!strncmp(lgopts[option_index].name, CMD_LINE_OPT_RX_CONFIG,
2963 sizeof(CMD_LINE_OPT_RX_CONFIG))) {
2964 ret = parse_rx_config(optarg);
2966 printf("invalid rx-config\n");
2967 print_usage(prgname);
2972 if (!strncmp(lgopts[option_index].name, CMD_LINE_OPT_TX_CONFIG,
2973 sizeof(CMD_LINE_OPT_TX_CONFIG))) {
2974 ret = parse_tx_config(optarg);
2976 printf("invalid tx-config\n");
2977 print_usage(prgname);
2982 #if (APP_CPU_LOAD > 0)
2983 if (!strncmp(lgopts[option_index].name, CMD_LINE_OPT_STAT_LCORE,
2984 sizeof(CMD_LINE_OPT_STAT_LCORE))) {
2985 cpu_load_lcore_id = parse_stat_lcore(optarg);
2989 if (!strncmp(lgopts[option_index].name, CMD_LINE_OPT_ETH_DEST,
2990 sizeof(CMD_LINE_OPT_ETH_DEST)))
2991 parse_eth_dest(optarg);
2993 if (!strncmp(lgopts[option_index].name, CMD_LINE_OPT_NO_NUMA,
2994 sizeof(CMD_LINE_OPT_NO_NUMA))) {
2995 printf("numa is disabled\n");
2999 #if (APP_LOOKUP_METHOD == APP_LOOKUP_EXACT_MATCH)
3000 if (!strncmp(lgopts[option_index].name, CMD_LINE_OPT_IPV6,
3001 sizeof(CMD_LINE_OPT_IPV6))) {
3002 printf("ipv6 is specified\n");
3007 if (!strncmp(lgopts[option_index].name, CMD_LINE_OPT_NO_LTHREADS,
3008 sizeof(CMD_LINE_OPT_NO_LTHREADS))) {
3009 printf("l-threads model is disabled\n");
3013 if (!strncmp(lgopts[option_index].name, CMD_LINE_OPT_PARSE_PTYPE,
3014 sizeof(CMD_LINE_OPT_PARSE_PTYPE))) {
3015 printf("software packet type parsing enabled\n");
3019 if (!strncmp(lgopts[option_index].name, CMD_LINE_OPT_ENABLE_JUMBO,
3020 sizeof(CMD_LINE_OPT_ENABLE_JUMBO))) {
3021 struct option lenopts = {"max-pkt-len", required_argument, 0,
3024 printf("jumbo frame is enabled - disabling simple TX path\n");
3025 port_conf.rxmode.jumbo_frame = 1;
3027 /* if no max-pkt-len set, use the default value ETHER_MAX_LEN */
3028 if (0 == getopt_long(argc, argvopt, "", &lenopts,
3031 ret = parse_max_pkt_len(optarg);
3032 if ((ret < 64) || (ret > MAX_JUMBO_PKT_LEN)) {
3033 printf("invalid packet length\n");
3034 print_usage(prgname);
3037 port_conf.rxmode.max_rx_pkt_len = ret;
3039 printf("set jumbo frame max packet length to %u\n",
3040 (unsigned int)port_conf.rxmode.max_rx_pkt_len);
3042 #if (APP_LOOKUP_METHOD == APP_LOOKUP_EXACT_MATCH)
3043 if (!strncmp(lgopts[option_index].name, CMD_LINE_OPT_HASH_ENTRY_NUM,
3044 sizeof(CMD_LINE_OPT_HASH_ENTRY_NUM))) {
3045 ret = parse_hash_entry_number(optarg);
3046 if ((ret > 0) && (ret <= L3FWD_HASH_ENTRIES)) {
3047 hash_entry_number = ret;
3049 printf("invalid hash entry number\n");
3050 print_usage(prgname);
3058 print_usage(prgname);
3064 argv[optind-1] = prgname;
3067 optind = 1; /* reset getopt lib */
3072 print_ethaddr(const char *name, const struct ether_addr *eth_addr)
3074 char buf[ETHER_ADDR_FMT_SIZE];
3076 ether_format_addr(buf, ETHER_ADDR_FMT_SIZE, eth_addr);
3077 printf("%s%s", name, buf);
3080 #if (APP_LOOKUP_METHOD == APP_LOOKUP_EXACT_MATCH)
3082 static void convert_ipv4_5tuple(struct ipv4_5tuple *key1,
3083 union ipv4_5tuple_host *key2)
3085 key2->ip_dst = rte_cpu_to_be_32(key1->ip_dst);
3086 key2->ip_src = rte_cpu_to_be_32(key1->ip_src);
3087 key2->port_dst = rte_cpu_to_be_16(key1->port_dst);
3088 key2->port_src = rte_cpu_to_be_16(key1->port_src);
3089 key2->proto = key1->proto;
3094 static void convert_ipv6_5tuple(struct ipv6_5tuple *key1,
3095 union ipv6_5tuple_host *key2)
3099 for (i = 0; i < 16; i++) {
3100 key2->ip_dst[i] = key1->ip_dst[i];
3101 key2->ip_src[i] = key1->ip_src[i];
3103 key2->port_dst = rte_cpu_to_be_16(key1->port_dst);
3104 key2->port_src = rte_cpu_to_be_16(key1->port_src);
3105 key2->proto = key1->proto;
3111 #define BYTE_VALUE_MAX 256
3112 #define ALL_32_BITS 0xffffffff
3113 #define BIT_8_TO_15 0x0000ff00
3115 populate_ipv4_few_flow_into_table(const struct rte_hash *h)
3119 uint32_t array_len = RTE_DIM(ipv4_l3fwd_route_array);
3121 mask0 = _mm_set_epi32(ALL_32_BITS, ALL_32_BITS, ALL_32_BITS, BIT_8_TO_15);
3122 for (i = 0; i < array_len; i++) {
3123 struct ipv4_l3fwd_route entry;
3124 union ipv4_5tuple_host newkey;
3126 entry = ipv4_l3fwd_route_array[i];
3127 convert_ipv4_5tuple(&entry.key, &newkey);
3128 ret = rte_hash_add_key(h, (void *)&newkey);
3130 rte_exit(EXIT_FAILURE, "Unable to add entry %" PRIu32
3131 " to the l3fwd hash.\n", i);
3133 ipv4_l3fwd_out_if[ret] = entry.if_out;
3135 printf("Hash: Adding 0x%" PRIx32 " keys\n", array_len);
3138 #define BIT_16_TO_23 0x00ff0000
3140 populate_ipv6_few_flow_into_table(const struct rte_hash *h)
3144 uint32_t array_len = RTE_DIM(ipv6_l3fwd_route_array);
3146 mask1 = _mm_set_epi32(ALL_32_BITS, ALL_32_BITS, ALL_32_BITS, BIT_16_TO_23);
3147 mask2 = _mm_set_epi32(0, 0, ALL_32_BITS, ALL_32_BITS);
3148 for (i = 0; i < array_len; i++) {
3149 struct ipv6_l3fwd_route entry;
3150 union ipv6_5tuple_host newkey;
3152 entry = ipv6_l3fwd_route_array[i];
3153 convert_ipv6_5tuple(&entry.key, &newkey);
3154 ret = rte_hash_add_key(h, (void *)&newkey);
3156 rte_exit(EXIT_FAILURE, "Unable to add entry %" PRIu32
3157 " to the l3fwd hash.\n", i);
3159 ipv6_l3fwd_out_if[ret] = entry.if_out;
3161 printf("Hash: Adding 0x%" PRIx32 "keys\n", array_len);
3164 #define NUMBER_PORT_USED 4
3166 populate_ipv4_many_flow_into_table(const struct rte_hash *h,
3167 unsigned int nr_flow)
3171 mask0 = _mm_set_epi32(ALL_32_BITS, ALL_32_BITS, ALL_32_BITS, BIT_8_TO_15);
3173 for (i = 0; i < nr_flow; i++) {
3174 struct ipv4_l3fwd_route entry;
3175 union ipv4_5tuple_host newkey;
3176 uint8_t a = (uint8_t)((i / NUMBER_PORT_USED) % BYTE_VALUE_MAX);
3177 uint8_t b = (uint8_t)(((i / NUMBER_PORT_USED) / BYTE_VALUE_MAX) %
3179 uint8_t c = (uint8_t)((i / NUMBER_PORT_USED) / (BYTE_VALUE_MAX *
3181 /* Create the ipv4 exact match flow */
3182 memset(&entry, 0, sizeof(entry));
3183 switch (i & (NUMBER_PORT_USED - 1)) {
3185 entry = ipv4_l3fwd_route_array[0];
3186 entry.key.ip_dst = IPv4(101, c, b, a);
3189 entry = ipv4_l3fwd_route_array[1];
3190 entry.key.ip_dst = IPv4(201, c, b, a);
3193 entry = ipv4_l3fwd_route_array[2];
3194 entry.key.ip_dst = IPv4(111, c, b, a);
3197 entry = ipv4_l3fwd_route_array[3];
3198 entry.key.ip_dst = IPv4(211, c, b, a);
3201 convert_ipv4_5tuple(&entry.key, &newkey);
3202 int32_t ret = rte_hash_add_key(h, (void *)&newkey);
3205 rte_exit(EXIT_FAILURE, "Unable to add entry %u\n", i);
3207 ipv4_l3fwd_out_if[ret] = (uint8_t)entry.if_out;
3210 printf("Hash: Adding 0x%x keys\n", nr_flow);
3214 populate_ipv6_many_flow_into_table(const struct rte_hash *h,
3215 unsigned int nr_flow)
3219 mask1 = _mm_set_epi32(ALL_32_BITS, ALL_32_BITS, ALL_32_BITS, BIT_16_TO_23);
3220 mask2 = _mm_set_epi32(0, 0, ALL_32_BITS, ALL_32_BITS);
3221 for (i = 0; i < nr_flow; i++) {
3222 struct ipv6_l3fwd_route entry;
3223 union ipv6_5tuple_host newkey;
3225 uint8_t a = (uint8_t) ((i / NUMBER_PORT_USED) % BYTE_VALUE_MAX);
3226 uint8_t b = (uint8_t) (((i / NUMBER_PORT_USED) / BYTE_VALUE_MAX) %
3228 uint8_t c = (uint8_t) ((i / NUMBER_PORT_USED) / (BYTE_VALUE_MAX *
3231 /* Create the ipv6 exact match flow */
3232 memset(&entry, 0, sizeof(entry));
3233 switch (i & (NUMBER_PORT_USED - 1)) {
3235 entry = ipv6_l3fwd_route_array[0];
3238 entry = ipv6_l3fwd_route_array[1];
3241 entry = ipv6_l3fwd_route_array[2];
3244 entry = ipv6_l3fwd_route_array[3];
3247 entry.key.ip_dst[13] = c;
3248 entry.key.ip_dst[14] = b;
3249 entry.key.ip_dst[15] = a;
3250 convert_ipv6_5tuple(&entry.key, &newkey);
3251 int32_t ret = rte_hash_add_key(h, (void *)&newkey);
3254 rte_exit(EXIT_FAILURE, "Unable to add entry %u\n", i);
3256 ipv6_l3fwd_out_if[ret] = (uint8_t) entry.if_out;
3259 printf("Hash: Adding 0x%x keys\n", nr_flow);
3263 setup_hash(int socketid)
3265 struct rte_hash_parameters ipv4_l3fwd_hash_params = {
3267 .entries = L3FWD_HASH_ENTRIES,
3268 .key_len = sizeof(union ipv4_5tuple_host),
3269 .hash_func = ipv4_hash_crc,
3270 .hash_func_init_val = 0,
3273 struct rte_hash_parameters ipv6_l3fwd_hash_params = {
3275 .entries = L3FWD_HASH_ENTRIES,
3276 .key_len = sizeof(union ipv6_5tuple_host),
3277 .hash_func = ipv6_hash_crc,
3278 .hash_func_init_val = 0,
3283 /* create ipv4 hash */
3284 snprintf(s, sizeof(s), "ipv4_l3fwd_hash_%d", socketid);
3285 ipv4_l3fwd_hash_params.name = s;
3286 ipv4_l3fwd_hash_params.socket_id = socketid;
3287 ipv4_l3fwd_lookup_struct[socketid] =
3288 rte_hash_create(&ipv4_l3fwd_hash_params);
3289 if (ipv4_l3fwd_lookup_struct[socketid] == NULL)
3290 rte_exit(EXIT_FAILURE, "Unable to create the l3fwd hash on "
3291 "socket %d\n", socketid);
3293 /* create ipv6 hash */
3294 snprintf(s, sizeof(s), "ipv6_l3fwd_hash_%d", socketid);
3295 ipv6_l3fwd_hash_params.name = s;
3296 ipv6_l3fwd_hash_params.socket_id = socketid;
3297 ipv6_l3fwd_lookup_struct[socketid] =
3298 rte_hash_create(&ipv6_l3fwd_hash_params);
3299 if (ipv6_l3fwd_lookup_struct[socketid] == NULL)
3300 rte_exit(EXIT_FAILURE, "Unable to create the l3fwd hash on "
3301 "socket %d\n", socketid);
3303 if (hash_entry_number != HASH_ENTRY_NUMBER_DEFAULT) {
3304 /* For testing hash matching with a large number of flows we
3305 * generate millions of IP 5-tuples with an incremented dst
3306 * address to initialize the hash table. */
3308 /* populate the ipv4 hash */
3309 populate_ipv4_many_flow_into_table(
3310 ipv4_l3fwd_lookup_struct[socketid], hash_entry_number);
3312 /* populate the ipv6 hash */
3313 populate_ipv6_many_flow_into_table(
3314 ipv6_l3fwd_lookup_struct[socketid], hash_entry_number);
3317 /* Use data in ipv4/ipv6 l3fwd lookup table directly to initialize
3320 /* populate the ipv4 hash */
3321 populate_ipv4_few_flow_into_table(
3322 ipv4_l3fwd_lookup_struct[socketid]);
3324 /* populate the ipv6 hash */
3325 populate_ipv6_few_flow_into_table(
3326 ipv6_l3fwd_lookup_struct[socketid]);
3332 #if (APP_LOOKUP_METHOD == APP_LOOKUP_LPM)
3334 setup_lpm(int socketid)
3336 struct rte_lpm6_config config;
3337 struct rte_lpm_config lpm_ipv4_config;
3342 /* create the LPM table */
3343 snprintf(s, sizeof(s), "IPV4_L3FWD_LPM_%d", socketid);
3344 lpm_ipv4_config.max_rules = IPV4_L3FWD_LPM_MAX_RULES;
3345 lpm_ipv4_config.number_tbl8s = 256;
3346 lpm_ipv4_config.flags = 0;
3347 ipv4_l3fwd_lookup_struct[socketid] =
3348 rte_lpm_create(s, socketid, &lpm_ipv4_config);
3349 if (ipv4_l3fwd_lookup_struct[socketid] == NULL)
3350 rte_exit(EXIT_FAILURE, "Unable to create the l3fwd LPM table"
3351 " on socket %d\n", socketid);
3353 /* populate the LPM table */
3354 for (i = 0; i < IPV4_L3FWD_NUM_ROUTES; i++) {
3356 /* skip unused ports */
3357 if ((1 << ipv4_l3fwd_route_array[i].if_out &
3358 enabled_port_mask) == 0)
3361 ret = rte_lpm_add(ipv4_l3fwd_lookup_struct[socketid],
3362 ipv4_l3fwd_route_array[i].ip,
3363 ipv4_l3fwd_route_array[i].depth,
3364 ipv4_l3fwd_route_array[i].if_out);
3367 rte_exit(EXIT_FAILURE, "Unable to add entry %u to the "
3368 "l3fwd LPM table on socket %d\n",
3372 printf("LPM: Adding route 0x%08x / %d (%d)\n",
3373 (unsigned)ipv4_l3fwd_route_array[i].ip,
3374 ipv4_l3fwd_route_array[i].depth,
3375 ipv4_l3fwd_route_array[i].if_out);
3378 /* create the LPM6 table */
3379 snprintf(s, sizeof(s), "IPV6_L3FWD_LPM_%d", socketid);
3381 config.max_rules = IPV6_L3FWD_LPM_MAX_RULES;
3382 config.number_tbl8s = IPV6_L3FWD_LPM_NUMBER_TBL8S;
3384 ipv6_l3fwd_lookup_struct[socketid] = rte_lpm6_create(s, socketid,
3386 if (ipv6_l3fwd_lookup_struct[socketid] == NULL)
3387 rte_exit(EXIT_FAILURE, "Unable to create the l3fwd LPM table"
3388 " on socket %d\n", socketid);
3390 /* populate the LPM table */
3391 for (i = 0; i < IPV6_L3FWD_NUM_ROUTES; i++) {
3393 /* skip unused ports */
3394 if ((1 << ipv6_l3fwd_route_array[i].if_out &
3395 enabled_port_mask) == 0)
3398 ret = rte_lpm6_add(ipv6_l3fwd_lookup_struct[socketid],
3399 ipv6_l3fwd_route_array[i].ip,
3400 ipv6_l3fwd_route_array[i].depth,
3401 ipv6_l3fwd_route_array[i].if_out);
3404 rte_exit(EXIT_FAILURE, "Unable to add entry %u to the "
3405 "l3fwd LPM table on socket %d\n",
3409 printf("LPM: Adding route %s / %d (%d)\n",
3411 ipv6_l3fwd_route_array[i].depth,
3412 ipv6_l3fwd_route_array[i].if_out);
3418 init_mem(unsigned nb_mbuf)
3420 struct lcore_conf *qconf;
3425 for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
3426 if (rte_lcore_is_enabled(lcore_id) == 0)
3430 socketid = rte_lcore_to_socket_id(lcore_id);
3434 if (socketid >= NB_SOCKETS) {
3435 rte_exit(EXIT_FAILURE, "Socket %d of lcore %u is out of range %d\n",
3436 socketid, lcore_id, NB_SOCKETS);
3438 if (pktmbuf_pool[socketid] == NULL) {
3439 snprintf(s, sizeof(s), "mbuf_pool_%d", socketid);
3440 pktmbuf_pool[socketid] =
3441 rte_pktmbuf_pool_create(s, nb_mbuf,
3442 MEMPOOL_CACHE_SIZE, 0,
3443 RTE_MBUF_DEFAULT_BUF_SIZE, socketid);
3444 if (pktmbuf_pool[socketid] == NULL)
3445 rte_exit(EXIT_FAILURE,
3446 "Cannot init mbuf pool on socket %d\n", socketid);
3448 printf("Allocated mbuf pool on socket %d\n", socketid);
3450 #if (APP_LOOKUP_METHOD == APP_LOOKUP_LPM)
3451 setup_lpm(socketid);
3453 setup_hash(socketid);
3456 qconf = &lcore_conf[lcore_id];
3457 qconf->ipv4_lookup_struct = ipv4_l3fwd_lookup_struct[socketid];
3458 qconf->ipv6_lookup_struct = ipv6_l3fwd_lookup_struct[socketid];
3463 /* Check the link status of all ports in up to 9s, and print them finally */
3465 check_all_ports_link_status(uint8_t port_num, uint32_t port_mask)
3467 #define CHECK_INTERVAL 100 /* 100ms */
3468 #define MAX_CHECK_TIME 90 /* 9s (90 * 100ms) in total */
3469 uint8_t portid, count, all_ports_up, print_flag = 0;
3470 struct rte_eth_link link;
3472 printf("\nChecking link status");
3474 for (count = 0; count <= MAX_CHECK_TIME; count++) {
3476 for (portid = 0; portid < port_num; portid++) {
3477 if ((port_mask & (1 << portid)) == 0)
3479 memset(&link, 0, sizeof(link));
3480 rte_eth_link_get_nowait(portid, &link);
3481 /* print link status if flag set */
3482 if (print_flag == 1) {
3483 if (link.link_status)
3484 printf("Port %d Link Up - speed %u "
3485 "Mbps - %s\n", (uint8_t)portid,
3486 (unsigned)link.link_speed,
3487 (link.link_duplex == ETH_LINK_FULL_DUPLEX) ?
3488 ("full-duplex") : ("half-duplex\n"));
3490 printf("Port %d Link Down\n",
3494 /* clear all_ports_up flag if any link down */
3495 if (link.link_status == ETH_LINK_DOWN) {
3500 /* after finally printing all link status, get out */
3501 if (print_flag == 1)
3504 if (all_ports_up == 0) {
3507 rte_delay_ms(CHECK_INTERVAL);
3510 /* set the print_flag if all ports up or timeout */
3511 if (all_ports_up == 1 || count == (MAX_CHECK_TIME - 1)) {
3519 main(int argc, char **argv)
3521 struct rte_eth_dev_info dev_info;
3522 struct rte_eth_txconf *txconf;
3528 uint32_t n_tx_queue, nb_lcores;
3529 uint8_t portid, nb_rx_queue, queue, socketid;
3532 ret = rte_eal_init(argc, argv);
3534 rte_exit(EXIT_FAILURE, "Invalid EAL parameters\n");
3538 /* pre-init dst MACs for all ports to 02:00:00:00:00:xx */
3539 for (portid = 0; portid < RTE_MAX_ETHPORTS; portid++) {
3540 dest_eth_addr[portid] = ETHER_LOCAL_ADMIN_ADDR +
3541 ((uint64_t)portid << 40);
3542 *(uint64_t *)(val_eth + portid) = dest_eth_addr[portid];
3545 /* parse application arguments (after the EAL ones) */
3546 ret = parse_args(argc, argv);
3548 rte_exit(EXIT_FAILURE, "Invalid L3FWD parameters\n");
3550 if (check_lcore_params() < 0)
3551 rte_exit(EXIT_FAILURE, "check_lcore_params failed\n");
3553 printf("Initializing rx-queues...\n");
3554 ret = init_rx_queues();
3556 rte_exit(EXIT_FAILURE, "init_rx_queues failed\n");
3558 printf("Initializing tx-threads...\n");
3559 ret = init_tx_threads();
3561 rte_exit(EXIT_FAILURE, "init_tx_threads failed\n");
3563 printf("Initializing rings...\n");
3564 ret = init_rx_rings();
3566 rte_exit(EXIT_FAILURE, "init_rx_rings failed\n");
3568 nb_ports = rte_eth_dev_count();
3570 if (check_port_config(nb_ports) < 0)
3571 rte_exit(EXIT_FAILURE, "check_port_config failed\n");
3573 nb_lcores = rte_lcore_count();
3575 /* initialize all ports */
3576 for (portid = 0; portid < nb_ports; portid++) {
3577 /* skip ports that are not enabled */
3578 if ((enabled_port_mask & (1 << portid)) == 0) {
3579 printf("\nSkipping disabled port %d\n", portid);
3584 printf("Initializing port %d ... ", portid);
3587 nb_rx_queue = get_port_n_rx_queues(portid);
3588 n_tx_queue = nb_lcores;
3589 if (n_tx_queue > MAX_TX_QUEUE_PER_PORT)
3590 n_tx_queue = MAX_TX_QUEUE_PER_PORT;
3591 printf("Creating queues: nb_rxq=%d nb_txq=%u... ",
3592 nb_rx_queue, (unsigned)n_tx_queue);
3593 ret = rte_eth_dev_configure(portid, nb_rx_queue,
3594 (uint16_t)n_tx_queue, &port_conf);
3596 rte_exit(EXIT_FAILURE, "Cannot configure device: err=%d, port=%d\n",
3599 rte_eth_macaddr_get(portid, &ports_eth_addr[portid]);
3600 print_ethaddr(" Address:", &ports_eth_addr[portid]);
3602 print_ethaddr("Destination:",
3603 (const struct ether_addr *)&dest_eth_addr[portid]);
3607 * prepare src MACs for each port.
3609 ether_addr_copy(&ports_eth_addr[portid],
3610 (struct ether_addr *)(val_eth + portid) + 1);
3613 ret = init_mem(NB_MBUF);
3615 rte_exit(EXIT_FAILURE, "init_mem failed\n");
3617 /* init one TX queue per couple (lcore,port) */
3619 for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
3620 if (rte_lcore_is_enabled(lcore_id) == 0)
3624 socketid = (uint8_t)rte_lcore_to_socket_id(lcore_id);
3628 printf("txq=%u,%d,%d ", lcore_id, queueid, socketid);
3631 rte_eth_dev_info_get(portid, &dev_info);
3632 txconf = &dev_info.default_txconf;
3633 if (port_conf.rxmode.jumbo_frame)
3634 txconf->txq_flags = 0;
3635 ret = rte_eth_tx_queue_setup(portid, queueid, nb_txd,
3638 rte_exit(EXIT_FAILURE, "rte_eth_tx_queue_setup: err=%d, "
3639 "port=%d\n", ret, portid);
3641 tx_thread[lcore_id].tx_queue_id[portid] = queueid;
3647 for (i = 0; i < n_rx_thread; i++) {
3648 lcore_id = rx_thread[i].conf.lcore_id;
3650 if (rte_lcore_is_enabled(lcore_id) == 0) {
3651 rte_exit(EXIT_FAILURE,
3652 "Cannot start Rx thread on lcore %u: lcore disabled\n",
3657 printf("\nInitializing rx queues for Rx thread %d on lcore %u ... ",
3661 /* init RX queues */
3662 for (queue = 0; queue < rx_thread[i].n_rx_queue; ++queue) {
3663 portid = rx_thread[i].rx_queue_list[queue].port_id;
3664 queueid = rx_thread[i].rx_queue_list[queue].queue_id;
3667 socketid = (uint8_t)rte_lcore_to_socket_id(lcore_id);
3671 printf("rxq=%d,%d,%d ", portid, queueid, socketid);
3674 ret = rte_eth_rx_queue_setup(portid, queueid, nb_rxd,
3677 pktmbuf_pool[socketid]);
3679 rte_exit(EXIT_FAILURE, "rte_eth_rx_queue_setup: err=%d, "
3680 "port=%d\n", ret, portid);
3687 for (portid = 0; portid < nb_ports; portid++) {
3688 if ((enabled_port_mask & (1 << portid)) == 0)
3692 ret = rte_eth_dev_start(portid);
3694 rte_exit(EXIT_FAILURE, "rte_eth_dev_start: err=%d, port=%d\n",
3698 * If enabled, put device in promiscuous mode.
3699 * This allows IO forwarding mode to forward packets
3700 * to itself through 2 cross-connected ports of the
3704 rte_eth_promiscuous_enable(portid);
3707 for (i = 0; i < n_rx_thread; i++) {
3708 lcore_id = rx_thread[i].conf.lcore_id;
3709 if (rte_lcore_is_enabled(lcore_id) == 0)
3712 /* check if hw packet type is supported */
3713 for (queue = 0; queue < rx_thread[i].n_rx_queue; ++queue) {
3714 portid = rx_thread[i].rx_queue_list[queue].port_id;
3715 queueid = rx_thread[i].rx_queue_list[queue].queue_id;
3717 if (parse_ptype_on) {
3718 if (!rte_eth_add_rx_callback(portid, queueid,
3719 cb_parse_ptype, NULL))
3720 rte_exit(EXIT_FAILURE,
3721 "Failed to add rx callback: "
3722 "port=%d\n", portid);
3723 } else if (!check_ptype(portid))
3724 rte_exit(EXIT_FAILURE,
3725 "Port %d cannot parse packet type.\n\n"
3726 "Please add --parse-ptype to use sw "
3727 "packet type analyzer.\n\n",
3732 check_all_ports_link_status((uint8_t)nb_ports, enabled_port_mask);
3735 printf("Starting L-Threading Model\n");
3737 #if (APP_CPU_LOAD > 0)
3738 if (cpu_load_lcore_id > 0)
3739 /* Use one lcore for cpu load collector */
3743 lthread_num_schedulers_set(nb_lcores);
3744 rte_eal_mp_remote_launch(sched_spawner, NULL, SKIP_MASTER);
3745 lthread_master_spawner(NULL);
3748 printf("Starting P-Threading Model\n");
3749 /* launch per-lcore init on every lcore */
3750 rte_eal_mp_remote_launch(pthread_run, NULL, CALL_MASTER);
3751 RTE_LCORE_FOREACH_SLAVE(lcore_id) {
3752 if (rte_eal_wait_lcore(lcore_id) < 0)