4 * Copyright(c) 2010-2016 Intel Corporation. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
17 * * Neither the name of Intel Corporation nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
40 #include <sys/types.h>
42 #include <sys/queue.h>
47 #include <rte_common.h>
49 #include <rte_byteorder.h>
51 #include <rte_memory.h>
52 #include <rte_memcpy.h>
53 #include <rte_memzone.h>
55 #include <rte_per_lcore.h>
56 #include <rte_launch.h>
57 #include <rte_atomic.h>
58 #include <rte_cycles.h>
59 #include <rte_prefetch.h>
60 #include <rte_lcore.h>
61 #include <rte_per_lcore.h>
62 #include <rte_branch_prediction.h>
63 #include <rte_interrupts.h>
65 #include <rte_random.h>
66 #include <rte_debug.h>
67 #include <rte_ether.h>
68 #include <rte_ethdev.h>
70 #include <rte_mempool.h>
75 #include <rte_string_fns.h>
77 #include <cmdline_parse.h>
78 #include <cmdline_parse_etheraddr.h>
80 #include <lthread_api.h>
82 #define APP_LOOKUP_EXACT_MATCH 0
83 #define APP_LOOKUP_LPM 1
84 #define DO_RFC_1812_CHECKS
86 /* Enable cpu-load stats 0-off, 1-on */
87 #define APP_CPU_LOAD 1
89 #ifndef APP_LOOKUP_METHOD
90 #define APP_LOOKUP_METHOD APP_LOOKUP_LPM
94 check_ptype(int portid)
97 int ipv4 = 0, ipv6 = 0;
99 ret = rte_eth_dev_get_supported_ptypes(portid, RTE_PTYPE_L3_MASK, NULL,
104 uint32_t ptypes[ret];
106 ret = rte_eth_dev_get_supported_ptypes(portid, RTE_PTYPE_L3_MASK,
108 for (i = 0; i < ret; ++i) {
109 if (ptypes[i] & RTE_PTYPE_L3_IPV4)
111 if (ptypes[i] & RTE_PTYPE_L3_IPV6)
122 parse_ptype(struct rte_mbuf *m)
124 struct ether_hdr *eth_hdr;
125 uint32_t packet_type = RTE_PTYPE_UNKNOWN;
128 eth_hdr = rte_pktmbuf_mtod(m, struct ether_hdr *);
129 ether_type = eth_hdr->ether_type;
130 if (ether_type == rte_cpu_to_be_16(ETHER_TYPE_IPv4))
131 packet_type |= RTE_PTYPE_L3_IPV4_EXT_UNKNOWN;
132 else if (ether_type == rte_cpu_to_be_16(ETHER_TYPE_IPv6))
133 packet_type |= RTE_PTYPE_L3_IPV6_EXT_UNKNOWN;
135 m->packet_type = packet_type;
139 cb_parse_ptype(__rte_unused uint8_t port, __rte_unused uint16_t queue,
140 struct rte_mbuf *pkts[], uint16_t nb_pkts,
141 __rte_unused uint16_t max_pkts, __rte_unused void *user_param)
145 for (i = 0; i < nb_pkts; i++)
146 parse_ptype(pkts[i]);
152 * When set to zero, simple forwaring path is eanbled.
153 * When set to one, optimized forwarding path is enabled.
154 * Note that LPM optimisation path uses SSE4.1 instructions.
156 #if ((APP_LOOKUP_METHOD == APP_LOOKUP_LPM) && !defined(__SSE4_1__))
157 #define ENABLE_MULTI_BUFFER_OPTIMIZE 0
159 #define ENABLE_MULTI_BUFFER_OPTIMIZE 1
162 #if (APP_LOOKUP_METHOD == APP_LOOKUP_EXACT_MATCH)
163 #include <rte_hash.h>
164 #elif (APP_LOOKUP_METHOD == APP_LOOKUP_LPM)
166 #include <rte_lpm6.h>
168 #error "APP_LOOKUP_METHOD set to incorrect value"
171 #define RTE_LOGTYPE_L3FWD RTE_LOGTYPE_USER1
173 #define MAX_JUMBO_PKT_LEN 9600
175 #define IPV6_ADDR_LEN 16
177 #define MEMPOOL_CACHE_SIZE 256
180 * This expression is used to calculate the number of mbufs needed depending on
181 * user input, taking into account memory for rx and tx hardware rings, cache
182 * per lcore and mtable per port per lcore. RTE_MAX is used to ensure that
183 * NB_MBUF never goes below a minimum value of 8192
186 #define NB_MBUF RTE_MAX(\
187 (nb_ports*nb_rx_queue*RTE_TEST_RX_DESC_DEFAULT + \
188 nb_ports*nb_lcores*MAX_PKT_BURST + \
189 nb_ports*n_tx_queue*RTE_TEST_TX_DESC_DEFAULT + \
190 nb_lcores*MEMPOOL_CACHE_SIZE), \
193 #define MAX_PKT_BURST 32
194 #define BURST_TX_DRAIN_US 100 /* TX drain every ~100us */
197 * Try to avoid TX buffering if we have at least MAX_TX_BURST packets to send.
199 #define MAX_TX_BURST (MAX_PKT_BURST / 2)
200 #define BURST_SIZE MAX_TX_BURST
204 /* Configure how many packets ahead to prefetch, when reading packets */
205 #define PREFETCH_OFFSET 3
207 /* Used to mark destination port as 'invalid'. */
208 #define BAD_PORT ((uint16_t)-1)
213 * Configurable number of RX/TX ring descriptors
215 #define RTE_TEST_RX_DESC_DEFAULT 128
216 #define RTE_TEST_TX_DESC_DEFAULT 128
217 static uint16_t nb_rxd = RTE_TEST_RX_DESC_DEFAULT;
218 static uint16_t nb_txd = RTE_TEST_TX_DESC_DEFAULT;
220 /* ethernet addresses of ports */
221 static uint64_t dest_eth_addr[RTE_MAX_ETHPORTS];
222 static struct ether_addr ports_eth_addr[RTE_MAX_ETHPORTS];
224 static __m128i val_eth[RTE_MAX_ETHPORTS];
226 /* replace first 12B of the ethernet header. */
227 #define MASK_ETH 0x3f
229 /* mask of enabled ports */
230 static uint32_t enabled_port_mask;
231 static int promiscuous_on; /**< Set in promiscuous mode off by default. */
232 static int numa_on = 1; /**< NUMA is enabled by default. */
233 static int parse_ptype_on;
235 #if (APP_LOOKUP_METHOD == APP_LOOKUP_EXACT_MATCH)
236 static int ipv6; /**< ipv6 is false by default. */
239 #if (APP_CPU_LOAD == 1)
241 #define MAX_CPU RTE_MAX_LCORE
242 #define CPU_LOAD_TIMEOUT_US (5 * 1000 * 1000) /**< Timeout for collecting 5s */
244 #define CPU_PROCESS 0
246 #define MAX_CPU_COUNTER 2
251 uint64_t hits[MAX_CPU_COUNTER][MAX_CPU];
252 } __rte_cache_aligned;
254 static struct cpu_load cpu_load;
255 static int cpu_load_lcore_id = -1;
257 #define SET_CPU_BUSY(thread, counter) \
258 thread->conf.busy[counter] = 1
260 #define SET_CPU_IDLE(thread, counter) \
261 thread->conf.busy[counter] = 0
263 #define IS_CPU_BUSY(thread, counter) \
264 (thread->conf.busy[counter] > 0)
268 #define SET_CPU_BUSY(thread, counter)
269 #define SET_CPU_IDLE(thread, counter)
270 #define IS_CPU_BUSY(thread, counter) 0
276 struct rte_mbuf *m_table[MAX_PKT_BURST];
279 struct lcore_rx_queue {
282 } __rte_cache_aligned;
284 #define MAX_RX_QUEUE_PER_LCORE 16
285 #define MAX_TX_QUEUE_PER_PORT RTE_MAX_ETHPORTS
286 #define MAX_RX_QUEUE_PER_PORT 128
288 #define MAX_LCORE_PARAMS 1024
289 struct rx_thread_params {
294 } __rte_cache_aligned;
296 static struct rx_thread_params rx_thread_params_array[MAX_LCORE_PARAMS];
297 static struct rx_thread_params rx_thread_params_array_default[] = {
309 static struct rx_thread_params *rx_thread_params =
310 rx_thread_params_array_default;
311 static uint16_t nb_rx_thread_params = RTE_DIM(rx_thread_params_array_default);
313 struct tx_thread_params {
316 } __rte_cache_aligned;
318 static struct tx_thread_params tx_thread_params_array[MAX_LCORE_PARAMS];
319 static struct tx_thread_params tx_thread_params_array_default[] = {
331 static struct tx_thread_params *tx_thread_params =
332 tx_thread_params_array_default;
333 static uint16_t nb_tx_thread_params = RTE_DIM(tx_thread_params_array_default);
335 static struct rte_eth_conf port_conf = {
337 .mq_mode = ETH_MQ_RX_RSS,
338 .max_rx_pkt_len = ETHER_MAX_LEN,
340 .header_split = 0, /**< Header Split disabled */
341 .hw_ip_checksum = 1, /**< IP checksum offload enabled */
342 .hw_vlan_filter = 0, /**< VLAN filtering disabled */
343 .jumbo_frame = 0, /**< Jumbo Frame Support disabled */
344 .hw_strip_crc = 1, /**< CRC stripped by hardware */
349 .rss_hf = ETH_RSS_TCP,
353 .mq_mode = ETH_MQ_TX_NONE,
357 static struct rte_mempool *pktmbuf_pool[NB_SOCKETS];
359 #if (APP_LOOKUP_METHOD == APP_LOOKUP_EXACT_MATCH)
361 #ifdef RTE_MACHINE_CPUFLAG_SSE4_2
362 #include <rte_hash_crc.h>
363 #define DEFAULT_HASH_FUNC rte_hash_crc
365 #include <rte_jhash.h>
366 #define DEFAULT_HASH_FUNC rte_jhash
375 } __attribute__((__packed__));
377 union ipv4_5tuple_host {
390 #define XMM_NUM_IN_IPV6_5TUPLE 3
393 uint8_t ip_dst[IPV6_ADDR_LEN];
394 uint8_t ip_src[IPV6_ADDR_LEN];
398 } __attribute__((__packed__));
400 union ipv6_5tuple_host {
405 uint8_t ip_src[IPV6_ADDR_LEN];
406 uint8_t ip_dst[IPV6_ADDR_LEN];
411 __m128i xmm[XMM_NUM_IN_IPV6_5TUPLE];
414 struct ipv4_l3fwd_route {
415 struct ipv4_5tuple key;
419 struct ipv6_l3fwd_route {
420 struct ipv6_5tuple key;
424 static struct ipv4_l3fwd_route ipv4_l3fwd_route_array[] = {
425 {{IPv4(101, 0, 0, 0), IPv4(100, 10, 0, 1), 101, 11, IPPROTO_TCP}, 0},
426 {{IPv4(201, 0, 0, 0), IPv4(200, 20, 0, 1), 102, 12, IPPROTO_TCP}, 1},
427 {{IPv4(111, 0, 0, 0), IPv4(100, 30, 0, 1), 101, 11, IPPROTO_TCP}, 2},
428 {{IPv4(211, 0, 0, 0), IPv4(200, 40, 0, 1), 102, 12, IPPROTO_TCP}, 3},
431 static struct ipv6_l3fwd_route ipv6_l3fwd_route_array[] = {
433 {0xfe, 0x80, 0, 0, 0, 0, 0, 0, 0x02, 0x1e, 0x67, 0xff, 0xfe, 0, 0, 0},
434 {0xfe, 0x80, 0, 0, 0, 0, 0, 0, 0x02, 0x1b, 0x21, 0xff, 0xfe, 0x91, 0x38,
436 101, 11, IPPROTO_TCP}, 0},
439 {0xfe, 0x90, 0, 0, 0, 0, 0, 0, 0x02, 0x1e, 0x67, 0xff, 0xfe, 0, 0, 0},
440 {0xfe, 0x90, 0, 0, 0, 0, 0, 0, 0x02, 0x1b, 0x21, 0xff, 0xfe, 0x91, 0x38,
442 102, 12, IPPROTO_TCP}, 1},
445 {0xfe, 0xa0, 0, 0, 0, 0, 0, 0, 0x02, 0x1e, 0x67, 0xff, 0xfe, 0, 0, 0},
446 {0xfe, 0xa0, 0, 0, 0, 0, 0, 0, 0x02, 0x1b, 0x21, 0xff, 0xfe, 0x91, 0x38,
448 101, 11, IPPROTO_TCP}, 2},
451 {0xfe, 0xb0, 0, 0, 0, 0, 0, 0, 0x02, 0x1e, 0x67, 0xff, 0xfe, 0, 0, 0},
452 {0xfe, 0xb0, 0, 0, 0, 0, 0, 0, 0x02, 0x1b, 0x21, 0xff, 0xfe, 0x91, 0x38,
454 102, 12, IPPROTO_TCP}, 3},
457 typedef struct rte_hash lookup_struct_t;
458 static lookup_struct_t *ipv4_l3fwd_lookup_struct[NB_SOCKETS];
459 static lookup_struct_t *ipv6_l3fwd_lookup_struct[NB_SOCKETS];
461 #ifdef RTE_ARCH_X86_64
462 /* default to 4 million hash entries (approx) */
463 #define L3FWD_HASH_ENTRIES (1024*1024*4)
465 /* 32-bit has less address-space for hugepage memory, limit to 1M entries */
466 #define L3FWD_HASH_ENTRIES (1024*1024*1)
468 #define HASH_ENTRY_NUMBER_DEFAULT 4
470 static uint32_t hash_entry_number = HASH_ENTRY_NUMBER_DEFAULT;
472 static inline uint32_t
473 ipv4_hash_crc(const void *data, __rte_unused uint32_t data_len,
476 const union ipv4_5tuple_host *k;
482 p = (const uint32_t *)&k->port_src;
484 #ifdef RTE_MACHINE_CPUFLAG_SSE4_2
485 init_val = rte_hash_crc_4byte(t, init_val);
486 init_val = rte_hash_crc_4byte(k->ip_src, init_val);
487 init_val = rte_hash_crc_4byte(k->ip_dst, init_val);
488 init_val = rte_hash_crc_4byte(*p, init_val);
489 #else /* RTE_MACHINE_CPUFLAG_SSE4_2 */
490 init_val = rte_jhash_1word(t, init_val);
491 init_val = rte_jhash_1word(k->ip_src, init_val);
492 init_val = rte_jhash_1word(k->ip_dst, init_val);
493 init_val = rte_jhash_1word(*p, init_val);
494 #endif /* RTE_MACHINE_CPUFLAG_SSE4_2 */
498 static inline uint32_t
499 ipv6_hash_crc(const void *data, __rte_unused uint32_t data_len,
502 const union ipv6_5tuple_host *k;
505 #ifdef RTE_MACHINE_CPUFLAG_SSE4_2
506 const uint32_t *ip_src0, *ip_src1, *ip_src2, *ip_src3;
507 const uint32_t *ip_dst0, *ip_dst1, *ip_dst2, *ip_dst3;
508 #endif /* RTE_MACHINE_CPUFLAG_SSE4_2 */
512 p = (const uint32_t *)&k->port_src;
514 #ifdef RTE_MACHINE_CPUFLAG_SSE4_2
515 ip_src0 = (const uint32_t *) k->ip_src;
516 ip_src1 = (const uint32_t *)(k->ip_src + 4);
517 ip_src2 = (const uint32_t *)(k->ip_src + 8);
518 ip_src3 = (const uint32_t *)(k->ip_src + 12);
519 ip_dst0 = (const uint32_t *) k->ip_dst;
520 ip_dst1 = (const uint32_t *)(k->ip_dst + 4);
521 ip_dst2 = (const uint32_t *)(k->ip_dst + 8);
522 ip_dst3 = (const uint32_t *)(k->ip_dst + 12);
523 init_val = rte_hash_crc_4byte(t, init_val);
524 init_val = rte_hash_crc_4byte(*ip_src0, init_val);
525 init_val = rte_hash_crc_4byte(*ip_src1, init_val);
526 init_val = rte_hash_crc_4byte(*ip_src2, init_val);
527 init_val = rte_hash_crc_4byte(*ip_src3, init_val);
528 init_val = rte_hash_crc_4byte(*ip_dst0, init_val);
529 init_val = rte_hash_crc_4byte(*ip_dst1, init_val);
530 init_val = rte_hash_crc_4byte(*ip_dst2, init_val);
531 init_val = rte_hash_crc_4byte(*ip_dst3, init_val);
532 init_val = rte_hash_crc_4byte(*p, init_val);
533 #else /* RTE_MACHINE_CPUFLAG_SSE4_2 */
534 init_val = rte_jhash_1word(t, init_val);
535 init_val = rte_jhash(k->ip_src, sizeof(uint8_t) * IPV6_ADDR_LEN, init_val);
536 init_val = rte_jhash(k->ip_dst, sizeof(uint8_t) * IPV6_ADDR_LEN, init_val);
537 init_val = rte_jhash_1word(*p, init_val);
538 #endif /* RTE_MACHINE_CPUFLAG_SSE4_2 */
542 #define IPV4_L3FWD_NUM_ROUTES RTE_DIM(ipv4_l3fwd_route_array)
543 #define IPV6_L3FWD_NUM_ROUTES RTE_DIM(ipv6_l3fwd_route_array)
545 static uint8_t ipv4_l3fwd_out_if[L3FWD_HASH_ENTRIES] __rte_cache_aligned;
546 static uint8_t ipv6_l3fwd_out_if[L3FWD_HASH_ENTRIES] __rte_cache_aligned;
550 #if (APP_LOOKUP_METHOD == APP_LOOKUP_LPM)
551 struct ipv4_l3fwd_route {
557 struct ipv6_l3fwd_route {
563 static struct ipv4_l3fwd_route ipv4_l3fwd_route_array[] = {
564 {IPv4(1, 1, 1, 0), 24, 0},
565 {IPv4(2, 1, 1, 0), 24, 1},
566 {IPv4(3, 1, 1, 0), 24, 2},
567 {IPv4(4, 1, 1, 0), 24, 3},
568 {IPv4(5, 1, 1, 0), 24, 4},
569 {IPv4(6, 1, 1, 0), 24, 5},
570 {IPv4(7, 1, 1, 0), 24, 6},
571 {IPv4(8, 1, 1, 0), 24, 7},
574 static struct ipv6_l3fwd_route ipv6_l3fwd_route_array[] = {
575 {{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, 48, 0},
576 {{2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, 48, 1},
577 {{3, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, 48, 2},
578 {{4, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, 48, 3},
579 {{5, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, 48, 4},
580 {{6, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, 48, 5},
581 {{7, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, 48, 6},
582 {{8, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, 48, 7},
585 #define IPV4_L3FWD_NUM_ROUTES RTE_DIM(ipv4_l3fwd_route_array)
586 #define IPV6_L3FWD_NUM_ROUTES RTE_DIM(ipv6_l3fwd_route_array)
588 #define IPV4_L3FWD_LPM_MAX_RULES 1024
589 #define IPV6_L3FWD_LPM_MAX_RULES 1024
590 #define IPV6_L3FWD_LPM_NUMBER_TBL8S (1 << 16)
592 typedef struct rte_lpm lookup_struct_t;
593 typedef struct rte_lpm6 lookup6_struct_t;
594 static lookup_struct_t *ipv4_l3fwd_lookup_struct[NB_SOCKETS];
595 static lookup6_struct_t *ipv6_l3fwd_lookup_struct[NB_SOCKETS];
599 lookup_struct_t *ipv4_lookup_struct;
600 #if (APP_LOOKUP_METHOD == APP_LOOKUP_LPM)
601 lookup6_struct_t *ipv6_lookup_struct;
603 lookup_struct_t *ipv6_lookup_struct;
606 } __rte_cache_aligned;
608 static struct lcore_conf lcore_conf[RTE_MAX_LCORE];
609 RTE_DEFINE_PER_LCORE(struct lcore_conf *, lcore_conf);
611 #define MAX_RX_QUEUE_PER_THREAD 16
612 #define MAX_TX_PORT_PER_THREAD RTE_MAX_ETHPORTS
613 #define MAX_TX_QUEUE_PER_PORT RTE_MAX_ETHPORTS
614 #define MAX_RX_QUEUE_PER_PORT 128
616 #define MAX_RX_THREAD 1024
617 #define MAX_TX_THREAD 1024
618 #define MAX_THREAD (MAX_RX_THREAD + MAX_TX_THREAD)
621 * Producers and consumers threads configuration
623 static int lthreads_on = 1; /**< Use lthreads for processing*/
625 rte_atomic16_t rx_counter; /**< Number of spawned rx threads */
626 rte_atomic16_t tx_counter; /**< Number of spawned tx threads */
629 uint16_t lcore_id; /**< Initial lcore for rx thread */
630 uint16_t cpu_id; /**< Cpu id for cpu load stats counter */
631 uint16_t thread_id; /**< Thread ID */
633 #if (APP_CPU_LOAD > 0)
634 int busy[MAX_CPU_COUNTER];
638 struct thread_rx_conf {
639 struct thread_conf conf;
642 struct lcore_rx_queue rx_queue_list[MAX_RX_QUEUE_PER_LCORE];
644 uint16_t n_ring; /**< Number of output rings */
645 struct rte_ring *ring[RTE_MAX_LCORE];
646 struct lthread_cond *ready[RTE_MAX_LCORE];
648 #if (APP_CPU_LOAD > 0)
649 int busy[MAX_CPU_COUNTER];
651 } __rte_cache_aligned;
653 uint16_t n_rx_thread;
654 struct thread_rx_conf rx_thread[MAX_RX_THREAD];
656 struct thread_tx_conf {
657 struct thread_conf conf;
659 uint16_t tx_queue_id[RTE_MAX_LCORE];
660 struct mbuf_table tx_mbufs[RTE_MAX_LCORE];
662 struct rte_ring *ring;
663 struct lthread_cond **ready;
665 } __rte_cache_aligned;
667 uint16_t n_tx_thread;
668 struct thread_tx_conf tx_thread[MAX_TX_THREAD];
670 /* Send burst of packets on an output interface */
672 send_burst(struct thread_tx_conf *qconf, uint16_t n, uint8_t port)
674 struct rte_mbuf **m_table;
678 queueid = qconf->tx_queue_id[port];
679 m_table = (struct rte_mbuf **)qconf->tx_mbufs[port].m_table;
681 ret = rte_eth_tx_burst(port, queueid, m_table, n);
682 if (unlikely(ret < n)) {
684 rte_pktmbuf_free(m_table[ret]);
691 /* Enqueue a single packet, and send burst if queue is filled */
693 send_single_packet(struct rte_mbuf *m, uint8_t port)
696 struct thread_tx_conf *qconf;
699 qconf = (struct thread_tx_conf *)lthread_get_data();
701 qconf = (struct thread_tx_conf *)RTE_PER_LCORE(lcore_conf)->data;
703 len = qconf->tx_mbufs[port].len;
704 qconf->tx_mbufs[port].m_table[len] = m;
707 /* enough pkts to be sent */
708 if (unlikely(len == MAX_PKT_BURST)) {
709 send_burst(qconf, MAX_PKT_BURST, port);
713 qconf->tx_mbufs[port].len = len;
717 #if ((APP_LOOKUP_METHOD == APP_LOOKUP_LPM) && \
718 (ENABLE_MULTI_BUFFER_OPTIMIZE == 1))
719 static inline __attribute__((always_inline)) void
720 send_packetsx4(uint8_t port,
721 struct rte_mbuf *m[], uint32_t num)
724 struct thread_tx_conf *qconf;
727 qconf = (struct thread_tx_conf *)lthread_get_data();
729 qconf = (struct thread_tx_conf *)RTE_PER_LCORE(lcore_conf)->data;
731 len = qconf->tx_mbufs[port].len;
734 * If TX buffer for that queue is empty, and we have enough packets,
735 * then send them straightway.
737 if (num >= MAX_TX_BURST && len == 0) {
738 n = rte_eth_tx_burst(port, qconf->tx_queue_id[port], m, num);
739 if (unlikely(n < num)) {
741 rte_pktmbuf_free(m[n]);
748 * Put packets into TX buffer for that queue.
752 n = (n > MAX_PKT_BURST) ? MAX_PKT_BURST - len : num;
755 switch (n % FWDSTEP) {
758 qconf->tx_mbufs[port].m_table[len + j] = m[j];
761 qconf->tx_mbufs[port].m_table[len + j] = m[j];
764 qconf->tx_mbufs[port].m_table[len + j] = m[j];
767 qconf->tx_mbufs[port].m_table[len + j] = m[j];
774 /* enough pkts to be sent */
775 if (unlikely(len == MAX_PKT_BURST)) {
777 send_burst(qconf, MAX_PKT_BURST, port);
779 /* copy rest of the packets into the TX buffer. */
782 switch (len % FWDSTEP) {
785 qconf->tx_mbufs[port].m_table[j] = m[n + j];
788 qconf->tx_mbufs[port].m_table[j] = m[n + j];
791 qconf->tx_mbufs[port].m_table[j] = m[n + j];
794 qconf->tx_mbufs[port].m_table[j] = m[n + j];
800 qconf->tx_mbufs[port].len = len;
802 #endif /* APP_LOOKUP_LPM */
804 #ifdef DO_RFC_1812_CHECKS
806 is_valid_ipv4_pkt(struct ipv4_hdr *pkt, uint32_t link_len)
808 /* From http://www.rfc-editor.org/rfc/rfc1812.txt section 5.2.2 */
810 * 1. The packet length reported by the Link Layer must be large
811 * enough to hold the minimum length legal IP datagram (20 bytes).
813 if (link_len < sizeof(struct ipv4_hdr))
816 /* 2. The IP checksum must be correct. */
817 /* this is checked in H/W */
820 * 3. The IP version number must be 4. If the version number is not 4
821 * then the packet may be another version of IP, such as IPng or
824 if (((pkt->version_ihl) >> 4) != 4)
827 * 4. The IP header length field must be large enough to hold the
828 * minimum length legal IP datagram (20 bytes = 5 words).
830 if ((pkt->version_ihl & 0xf) < 5)
834 * 5. The IP total length field must be large enough to hold the IP
835 * datagram header, whose length is specified in the IP header length
838 if (rte_cpu_to_be_16(pkt->total_length) < sizeof(struct ipv4_hdr))
845 #if (APP_LOOKUP_METHOD == APP_LOOKUP_EXACT_MATCH)
847 static __m128i mask0;
848 static __m128i mask1;
849 static __m128i mask2;
850 static inline uint8_t
851 get_ipv4_dst_port(void *ipv4_hdr, uint8_t portid,
852 lookup_struct_t *ipv4_l3fwd_lookup_struct)
855 union ipv4_5tuple_host key;
857 ipv4_hdr = (uint8_t *)ipv4_hdr + offsetof(struct ipv4_hdr, time_to_live);
858 __m128i data = _mm_loadu_si128((__m128i *)(ipv4_hdr));
859 /* Get 5 tuple: dst port, src port, dst IP address, src IP address and
861 key.xmm = _mm_and_si128(data, mask0);
862 /* Find destination port */
863 ret = rte_hash_lookup(ipv4_l3fwd_lookup_struct, (const void *)&key);
864 return (uint8_t)((ret < 0) ? portid : ipv4_l3fwd_out_if[ret]);
867 static inline uint8_t
868 get_ipv6_dst_port(void *ipv6_hdr, uint8_t portid,
869 lookup_struct_t *ipv6_l3fwd_lookup_struct)
872 union ipv6_5tuple_host key;
874 ipv6_hdr = (uint8_t *)ipv6_hdr + offsetof(struct ipv6_hdr, payload_len);
875 __m128i data0 = _mm_loadu_si128((__m128i *)(ipv6_hdr));
876 __m128i data1 = _mm_loadu_si128((__m128i *)(((uint8_t *)ipv6_hdr) +
878 __m128i data2 = _mm_loadu_si128((__m128i *)(((uint8_t *)ipv6_hdr) +
879 sizeof(__m128i) + sizeof(__m128i)));
880 /* Get part of 5 tuple: src IP address lower 96 bits and protocol */
881 key.xmm[0] = _mm_and_si128(data0, mask1);
882 /* Get part of 5 tuple: dst IP address lower 96 bits and src IP address
885 /* Get part of 5 tuple: dst port and src port and dst IP address higher
887 key.xmm[2] = _mm_and_si128(data2, mask2);
889 /* Find destination port */
890 ret = rte_hash_lookup(ipv6_l3fwd_lookup_struct, (const void *)&key);
891 return (uint8_t)((ret < 0) ? portid : ipv6_l3fwd_out_if[ret]);
895 #if (APP_LOOKUP_METHOD == APP_LOOKUP_LPM)
897 static inline uint8_t
898 get_ipv4_dst_port(void *ipv4_hdr, uint8_t portid,
899 lookup_struct_t *ipv4_l3fwd_lookup_struct)
903 return (uint8_t)((rte_lpm_lookup(ipv4_l3fwd_lookup_struct,
904 rte_be_to_cpu_32(((struct ipv4_hdr *)ipv4_hdr)->dst_addr),
905 &next_hop) == 0) ? next_hop : portid);
908 static inline uint8_t
909 get_ipv6_dst_port(void *ipv6_hdr, uint8_t portid,
910 lookup6_struct_t *ipv6_l3fwd_lookup_struct)
914 return (uint8_t) ((rte_lpm6_lookup(ipv6_l3fwd_lookup_struct,
915 ((struct ipv6_hdr *)ipv6_hdr)->dst_addr, &next_hop) == 0) ?
920 static inline void l3fwd_simple_forward(struct rte_mbuf *m, uint8_t portid)
921 __attribute__((unused));
923 #if ((APP_LOOKUP_METHOD == APP_LOOKUP_EXACT_MATCH) && \
924 (ENABLE_MULTI_BUFFER_OPTIMIZE == 1))
926 #define MASK_ALL_PKTS 0xff
927 #define EXCLUDE_1ST_PKT 0xfe
928 #define EXCLUDE_2ND_PKT 0xfd
929 #define EXCLUDE_3RD_PKT 0xfb
930 #define EXCLUDE_4TH_PKT 0xf7
931 #define EXCLUDE_5TH_PKT 0xef
932 #define EXCLUDE_6TH_PKT 0xdf
933 #define EXCLUDE_7TH_PKT 0xbf
934 #define EXCLUDE_8TH_PKT 0x7f
937 simple_ipv4_fwd_8pkts(struct rte_mbuf *m[8], uint8_t portid)
939 struct ether_hdr *eth_hdr[8];
940 struct ipv4_hdr *ipv4_hdr[8];
943 union ipv4_5tuple_host key[8];
946 eth_hdr[0] = rte_pktmbuf_mtod(m[0], struct ether_hdr *);
947 eth_hdr[1] = rte_pktmbuf_mtod(m[1], struct ether_hdr *);
948 eth_hdr[2] = rte_pktmbuf_mtod(m[2], struct ether_hdr *);
949 eth_hdr[3] = rte_pktmbuf_mtod(m[3], struct ether_hdr *);
950 eth_hdr[4] = rte_pktmbuf_mtod(m[4], struct ether_hdr *);
951 eth_hdr[5] = rte_pktmbuf_mtod(m[5], struct ether_hdr *);
952 eth_hdr[6] = rte_pktmbuf_mtod(m[6], struct ether_hdr *);
953 eth_hdr[7] = rte_pktmbuf_mtod(m[7], struct ether_hdr *);
955 /* Handle IPv4 headers.*/
956 ipv4_hdr[0] = rte_pktmbuf_mtod_offset(m[0], struct ipv4_hdr *,
957 sizeof(struct ether_hdr));
958 ipv4_hdr[1] = rte_pktmbuf_mtod_offset(m[1], struct ipv4_hdr *,
959 sizeof(struct ether_hdr));
960 ipv4_hdr[2] = rte_pktmbuf_mtod_offset(m[2], struct ipv4_hdr *,
961 sizeof(struct ether_hdr));
962 ipv4_hdr[3] = rte_pktmbuf_mtod_offset(m[3], struct ipv4_hdr *,
963 sizeof(struct ether_hdr));
964 ipv4_hdr[4] = rte_pktmbuf_mtod_offset(m[4], struct ipv4_hdr *,
965 sizeof(struct ether_hdr));
966 ipv4_hdr[5] = rte_pktmbuf_mtod_offset(m[5], struct ipv4_hdr *,
967 sizeof(struct ether_hdr));
968 ipv4_hdr[6] = rte_pktmbuf_mtod_offset(m[6], struct ipv4_hdr *,
969 sizeof(struct ether_hdr));
970 ipv4_hdr[7] = rte_pktmbuf_mtod_offset(m[7], struct ipv4_hdr *,
971 sizeof(struct ether_hdr));
973 #ifdef DO_RFC_1812_CHECKS
974 /* Check to make sure the packet is valid (RFC1812) */
975 uint8_t valid_mask = MASK_ALL_PKTS;
977 if (is_valid_ipv4_pkt(ipv4_hdr[0], m[0]->pkt_len) < 0) {
978 rte_pktmbuf_free(m[0]);
979 valid_mask &= EXCLUDE_1ST_PKT;
981 if (is_valid_ipv4_pkt(ipv4_hdr[1], m[1]->pkt_len) < 0) {
982 rte_pktmbuf_free(m[1]);
983 valid_mask &= EXCLUDE_2ND_PKT;
985 if (is_valid_ipv4_pkt(ipv4_hdr[2], m[2]->pkt_len) < 0) {
986 rte_pktmbuf_free(m[2]);
987 valid_mask &= EXCLUDE_3RD_PKT;
989 if (is_valid_ipv4_pkt(ipv4_hdr[3], m[3]->pkt_len) < 0) {
990 rte_pktmbuf_free(m[3]);
991 valid_mask &= EXCLUDE_4TH_PKT;
993 if (is_valid_ipv4_pkt(ipv4_hdr[4], m[4]->pkt_len) < 0) {
994 rte_pktmbuf_free(m[4]);
995 valid_mask &= EXCLUDE_5TH_PKT;
997 if (is_valid_ipv4_pkt(ipv4_hdr[5], m[5]->pkt_len) < 0) {
998 rte_pktmbuf_free(m[5]);
999 valid_mask &= EXCLUDE_6TH_PKT;
1001 if (is_valid_ipv4_pkt(ipv4_hdr[6], m[6]->pkt_len) < 0) {
1002 rte_pktmbuf_free(m[6]);
1003 valid_mask &= EXCLUDE_7TH_PKT;
1005 if (is_valid_ipv4_pkt(ipv4_hdr[7], m[7]->pkt_len) < 0) {
1006 rte_pktmbuf_free(m[7]);
1007 valid_mask &= EXCLUDE_8TH_PKT;
1009 if (unlikely(valid_mask != MASK_ALL_PKTS)) {
1010 if (valid_mask == 0)
1015 for (i = 0; i < 8; i++)
1016 if ((0x1 << i) & valid_mask)
1017 l3fwd_simple_forward(m[i], portid);
1019 #endif /* End of #ifdef DO_RFC_1812_CHECKS */
1021 data[0] = _mm_loadu_si128(rte_pktmbuf_mtod_offset(m[0], __m128i *,
1022 sizeof(struct ether_hdr) +
1023 offsetof(struct ipv4_hdr, time_to_live)));
1024 data[1] = _mm_loadu_si128(rte_pktmbuf_mtod_offset(m[1], __m128i *,
1025 sizeof(struct ether_hdr) +
1026 offsetof(struct ipv4_hdr, time_to_live)));
1027 data[2] = _mm_loadu_si128(rte_pktmbuf_mtod_offset(m[2], __m128i *,
1028 sizeof(struct ether_hdr) +
1029 offsetof(struct ipv4_hdr, time_to_live)));
1030 data[3] = _mm_loadu_si128(rte_pktmbuf_mtod_offset(m[3], __m128i *,
1031 sizeof(struct ether_hdr) +
1032 offsetof(struct ipv4_hdr, time_to_live)));
1033 data[4] = _mm_loadu_si128(rte_pktmbuf_mtod_offset(m[4], __m128i *,
1034 sizeof(struct ether_hdr) +
1035 offsetof(struct ipv4_hdr, time_to_live)));
1036 data[5] = _mm_loadu_si128(rte_pktmbuf_mtod_offset(m[5], __m128i *,
1037 sizeof(struct ether_hdr) +
1038 offsetof(struct ipv4_hdr, time_to_live)));
1039 data[6] = _mm_loadu_si128(rte_pktmbuf_mtod_offset(m[6], __m128i *,
1040 sizeof(struct ether_hdr) +
1041 offsetof(struct ipv4_hdr, time_to_live)));
1042 data[7] = _mm_loadu_si128(rte_pktmbuf_mtod_offset(m[7], __m128i *,
1043 sizeof(struct ether_hdr) +
1044 offsetof(struct ipv4_hdr, time_to_live)));
1046 key[0].xmm = _mm_and_si128(data[0], mask0);
1047 key[1].xmm = _mm_and_si128(data[1], mask0);
1048 key[2].xmm = _mm_and_si128(data[2], mask0);
1049 key[3].xmm = _mm_and_si128(data[3], mask0);
1050 key[4].xmm = _mm_and_si128(data[4], mask0);
1051 key[5].xmm = _mm_and_si128(data[5], mask0);
1052 key[6].xmm = _mm_and_si128(data[6], mask0);
1053 key[7].xmm = _mm_and_si128(data[7], mask0);
1055 const void *key_array[8] = {&key[0], &key[1], &key[2], &key[3],
1056 &key[4], &key[5], &key[6], &key[7]};
1058 rte_hash_lookup_bulk(RTE_PER_LCORE(lcore_conf)->ipv4_lookup_struct,
1059 &key_array[0], 8, ret);
1060 dst_port[0] = (uint8_t) ((ret[0] < 0) ? portid : ipv4_l3fwd_out_if[ret[0]]);
1061 dst_port[1] = (uint8_t) ((ret[1] < 0) ? portid : ipv4_l3fwd_out_if[ret[1]]);
1062 dst_port[2] = (uint8_t) ((ret[2] < 0) ? portid : ipv4_l3fwd_out_if[ret[2]]);
1063 dst_port[3] = (uint8_t) ((ret[3] < 0) ? portid : ipv4_l3fwd_out_if[ret[3]]);
1064 dst_port[4] = (uint8_t) ((ret[4] < 0) ? portid : ipv4_l3fwd_out_if[ret[4]]);
1065 dst_port[5] = (uint8_t) ((ret[5] < 0) ? portid : ipv4_l3fwd_out_if[ret[5]]);
1066 dst_port[6] = (uint8_t) ((ret[6] < 0) ? portid : ipv4_l3fwd_out_if[ret[6]]);
1067 dst_port[7] = (uint8_t) ((ret[7] < 0) ? portid : ipv4_l3fwd_out_if[ret[7]]);
1069 if (dst_port[0] >= RTE_MAX_ETHPORTS ||
1070 (enabled_port_mask & 1 << dst_port[0]) == 0)
1071 dst_port[0] = portid;
1072 if (dst_port[1] >= RTE_MAX_ETHPORTS ||
1073 (enabled_port_mask & 1 << dst_port[1]) == 0)
1074 dst_port[1] = portid;
1075 if (dst_port[2] >= RTE_MAX_ETHPORTS ||
1076 (enabled_port_mask & 1 << dst_port[2]) == 0)
1077 dst_port[2] = portid;
1078 if (dst_port[3] >= RTE_MAX_ETHPORTS ||
1079 (enabled_port_mask & 1 << dst_port[3]) == 0)
1080 dst_port[3] = portid;
1081 if (dst_port[4] >= RTE_MAX_ETHPORTS ||
1082 (enabled_port_mask & 1 << dst_port[4]) == 0)
1083 dst_port[4] = portid;
1084 if (dst_port[5] >= RTE_MAX_ETHPORTS ||
1085 (enabled_port_mask & 1 << dst_port[5]) == 0)
1086 dst_port[5] = portid;
1087 if (dst_port[6] >= RTE_MAX_ETHPORTS ||
1088 (enabled_port_mask & 1 << dst_port[6]) == 0)
1089 dst_port[6] = portid;
1090 if (dst_port[7] >= RTE_MAX_ETHPORTS ||
1091 (enabled_port_mask & 1 << dst_port[7]) == 0)
1092 dst_port[7] = portid;
1094 #ifdef DO_RFC_1812_CHECKS
1095 /* Update time to live and header checksum */
1096 --(ipv4_hdr[0]->time_to_live);
1097 --(ipv4_hdr[1]->time_to_live);
1098 --(ipv4_hdr[2]->time_to_live);
1099 --(ipv4_hdr[3]->time_to_live);
1100 ++(ipv4_hdr[0]->hdr_checksum);
1101 ++(ipv4_hdr[1]->hdr_checksum);
1102 ++(ipv4_hdr[2]->hdr_checksum);
1103 ++(ipv4_hdr[3]->hdr_checksum);
1104 --(ipv4_hdr[4]->time_to_live);
1105 --(ipv4_hdr[5]->time_to_live);
1106 --(ipv4_hdr[6]->time_to_live);
1107 --(ipv4_hdr[7]->time_to_live);
1108 ++(ipv4_hdr[4]->hdr_checksum);
1109 ++(ipv4_hdr[5]->hdr_checksum);
1110 ++(ipv4_hdr[6]->hdr_checksum);
1111 ++(ipv4_hdr[7]->hdr_checksum);
1115 *(uint64_t *)ð_hdr[0]->d_addr = dest_eth_addr[dst_port[0]];
1116 *(uint64_t *)ð_hdr[1]->d_addr = dest_eth_addr[dst_port[1]];
1117 *(uint64_t *)ð_hdr[2]->d_addr = dest_eth_addr[dst_port[2]];
1118 *(uint64_t *)ð_hdr[3]->d_addr = dest_eth_addr[dst_port[3]];
1119 *(uint64_t *)ð_hdr[4]->d_addr = dest_eth_addr[dst_port[4]];
1120 *(uint64_t *)ð_hdr[5]->d_addr = dest_eth_addr[dst_port[5]];
1121 *(uint64_t *)ð_hdr[6]->d_addr = dest_eth_addr[dst_port[6]];
1122 *(uint64_t *)ð_hdr[7]->d_addr = dest_eth_addr[dst_port[7]];
1125 ether_addr_copy(&ports_eth_addr[dst_port[0]], ð_hdr[0]->s_addr);
1126 ether_addr_copy(&ports_eth_addr[dst_port[1]], ð_hdr[1]->s_addr);
1127 ether_addr_copy(&ports_eth_addr[dst_port[2]], ð_hdr[2]->s_addr);
1128 ether_addr_copy(&ports_eth_addr[dst_port[3]], ð_hdr[3]->s_addr);
1129 ether_addr_copy(&ports_eth_addr[dst_port[4]], ð_hdr[4]->s_addr);
1130 ether_addr_copy(&ports_eth_addr[dst_port[5]], ð_hdr[5]->s_addr);
1131 ether_addr_copy(&ports_eth_addr[dst_port[6]], ð_hdr[6]->s_addr);
1132 ether_addr_copy(&ports_eth_addr[dst_port[7]], ð_hdr[7]->s_addr);
1134 send_single_packet(m[0], (uint8_t)dst_port[0]);
1135 send_single_packet(m[1], (uint8_t)dst_port[1]);
1136 send_single_packet(m[2], (uint8_t)dst_port[2]);
1137 send_single_packet(m[3], (uint8_t)dst_port[3]);
1138 send_single_packet(m[4], (uint8_t)dst_port[4]);
1139 send_single_packet(m[5], (uint8_t)dst_port[5]);
1140 send_single_packet(m[6], (uint8_t)dst_port[6]);
1141 send_single_packet(m[7], (uint8_t)dst_port[7]);
1145 static inline void get_ipv6_5tuple(struct rte_mbuf *m0, __m128i mask0,
1146 __m128i mask1, union ipv6_5tuple_host *key)
1148 __m128i tmpdata0 = _mm_loadu_si128(rte_pktmbuf_mtod_offset(m0,
1149 __m128i *, sizeof(struct ether_hdr) +
1150 offsetof(struct ipv6_hdr, payload_len)));
1151 __m128i tmpdata1 = _mm_loadu_si128(rte_pktmbuf_mtod_offset(m0,
1152 __m128i *, sizeof(struct ether_hdr) +
1153 offsetof(struct ipv6_hdr, payload_len) + sizeof(__m128i)));
1154 __m128i tmpdata2 = _mm_loadu_si128(rte_pktmbuf_mtod_offset(m0,
1155 __m128i *, sizeof(struct ether_hdr) +
1156 offsetof(struct ipv6_hdr, payload_len) + sizeof(__m128i) +
1158 key->xmm[0] = _mm_and_si128(tmpdata0, mask0);
1159 key->xmm[1] = tmpdata1;
1160 key->xmm[2] = _mm_and_si128(tmpdata2, mask1);
1164 simple_ipv6_fwd_8pkts(struct rte_mbuf *m[8], uint8_t portid)
1167 uint8_t dst_port[8];
1168 struct ether_hdr *eth_hdr[8];
1169 union ipv6_5tuple_host key[8];
1171 __attribute__((unused)) struct ipv6_hdr *ipv6_hdr[8];
1173 eth_hdr[0] = rte_pktmbuf_mtod(m[0], struct ether_hdr *);
1174 eth_hdr[1] = rte_pktmbuf_mtod(m[1], struct ether_hdr *);
1175 eth_hdr[2] = rte_pktmbuf_mtod(m[2], struct ether_hdr *);
1176 eth_hdr[3] = rte_pktmbuf_mtod(m[3], struct ether_hdr *);
1177 eth_hdr[4] = rte_pktmbuf_mtod(m[4], struct ether_hdr *);
1178 eth_hdr[5] = rte_pktmbuf_mtod(m[5], struct ether_hdr *);
1179 eth_hdr[6] = rte_pktmbuf_mtod(m[6], struct ether_hdr *);
1180 eth_hdr[7] = rte_pktmbuf_mtod(m[7], struct ether_hdr *);
1182 /* Handle IPv6 headers.*/
1183 ipv6_hdr[0] = rte_pktmbuf_mtod_offset(m[0], struct ipv6_hdr *,
1184 sizeof(struct ether_hdr));
1185 ipv6_hdr[1] = rte_pktmbuf_mtod_offset(m[1], struct ipv6_hdr *,
1186 sizeof(struct ether_hdr));
1187 ipv6_hdr[2] = rte_pktmbuf_mtod_offset(m[2], struct ipv6_hdr *,
1188 sizeof(struct ether_hdr));
1189 ipv6_hdr[3] = rte_pktmbuf_mtod_offset(m[3], struct ipv6_hdr *,
1190 sizeof(struct ether_hdr));
1191 ipv6_hdr[4] = rte_pktmbuf_mtod_offset(m[4], struct ipv6_hdr *,
1192 sizeof(struct ether_hdr));
1193 ipv6_hdr[5] = rte_pktmbuf_mtod_offset(m[5], struct ipv6_hdr *,
1194 sizeof(struct ether_hdr));
1195 ipv6_hdr[6] = rte_pktmbuf_mtod_offset(m[6], struct ipv6_hdr *,
1196 sizeof(struct ether_hdr));
1197 ipv6_hdr[7] = rte_pktmbuf_mtod_offset(m[7], struct ipv6_hdr *,
1198 sizeof(struct ether_hdr));
1200 get_ipv6_5tuple(m[0], mask1, mask2, &key[0]);
1201 get_ipv6_5tuple(m[1], mask1, mask2, &key[1]);
1202 get_ipv6_5tuple(m[2], mask1, mask2, &key[2]);
1203 get_ipv6_5tuple(m[3], mask1, mask2, &key[3]);
1204 get_ipv6_5tuple(m[4], mask1, mask2, &key[4]);
1205 get_ipv6_5tuple(m[5], mask1, mask2, &key[5]);
1206 get_ipv6_5tuple(m[6], mask1, mask2, &key[6]);
1207 get_ipv6_5tuple(m[7], mask1, mask2, &key[7]);
1209 const void *key_array[8] = {&key[0], &key[1], &key[2], &key[3],
1210 &key[4], &key[5], &key[6], &key[7]};
1212 rte_hash_lookup_bulk(RTE_PER_LCORE(lcore_conf)->ipv6_lookup_struct,
1213 &key_array[0], 4, ret);
1214 dst_port[0] = (uint8_t) ((ret[0] < 0) ? portid : ipv6_l3fwd_out_if[ret[0]]);
1215 dst_port[1] = (uint8_t) ((ret[1] < 0) ? portid : ipv6_l3fwd_out_if[ret[1]]);
1216 dst_port[2] = (uint8_t) ((ret[2] < 0) ? portid : ipv6_l3fwd_out_if[ret[2]]);
1217 dst_port[3] = (uint8_t) ((ret[3] < 0) ? portid : ipv6_l3fwd_out_if[ret[3]]);
1218 dst_port[4] = (uint8_t) ((ret[4] < 0) ? portid : ipv6_l3fwd_out_if[ret[4]]);
1219 dst_port[5] = (uint8_t) ((ret[5] < 0) ? portid : ipv6_l3fwd_out_if[ret[5]]);
1220 dst_port[6] = (uint8_t) ((ret[6] < 0) ? portid : ipv6_l3fwd_out_if[ret[6]]);
1221 dst_port[7] = (uint8_t) ((ret[7] < 0) ? portid : ipv6_l3fwd_out_if[ret[7]]);
1223 if (dst_port[0] >= RTE_MAX_ETHPORTS ||
1224 (enabled_port_mask & 1 << dst_port[0]) == 0)
1225 dst_port[0] = portid;
1226 if (dst_port[1] >= RTE_MAX_ETHPORTS ||
1227 (enabled_port_mask & 1 << dst_port[1]) == 0)
1228 dst_port[1] = portid;
1229 if (dst_port[2] >= RTE_MAX_ETHPORTS ||
1230 (enabled_port_mask & 1 << dst_port[2]) == 0)
1231 dst_port[2] = portid;
1232 if (dst_port[3] >= RTE_MAX_ETHPORTS ||
1233 (enabled_port_mask & 1 << dst_port[3]) == 0)
1234 dst_port[3] = portid;
1235 if (dst_port[4] >= RTE_MAX_ETHPORTS ||
1236 (enabled_port_mask & 1 << dst_port[4]) == 0)
1237 dst_port[4] = portid;
1238 if (dst_port[5] >= RTE_MAX_ETHPORTS ||
1239 (enabled_port_mask & 1 << dst_port[5]) == 0)
1240 dst_port[5] = portid;
1241 if (dst_port[6] >= RTE_MAX_ETHPORTS ||
1242 (enabled_port_mask & 1 << dst_port[6]) == 0)
1243 dst_port[6] = portid;
1244 if (dst_port[7] >= RTE_MAX_ETHPORTS ||
1245 (enabled_port_mask & 1 << dst_port[7]) == 0)
1246 dst_port[7] = portid;
1249 *(uint64_t *)ð_hdr[0]->d_addr = dest_eth_addr[dst_port[0]];
1250 *(uint64_t *)ð_hdr[1]->d_addr = dest_eth_addr[dst_port[1]];
1251 *(uint64_t *)ð_hdr[2]->d_addr = dest_eth_addr[dst_port[2]];
1252 *(uint64_t *)ð_hdr[3]->d_addr = dest_eth_addr[dst_port[3]];
1253 *(uint64_t *)ð_hdr[4]->d_addr = dest_eth_addr[dst_port[4]];
1254 *(uint64_t *)ð_hdr[5]->d_addr = dest_eth_addr[dst_port[5]];
1255 *(uint64_t *)ð_hdr[6]->d_addr = dest_eth_addr[dst_port[6]];
1256 *(uint64_t *)ð_hdr[7]->d_addr = dest_eth_addr[dst_port[7]];
1259 ether_addr_copy(&ports_eth_addr[dst_port[0]], ð_hdr[0]->s_addr);
1260 ether_addr_copy(&ports_eth_addr[dst_port[1]], ð_hdr[1]->s_addr);
1261 ether_addr_copy(&ports_eth_addr[dst_port[2]], ð_hdr[2]->s_addr);
1262 ether_addr_copy(&ports_eth_addr[dst_port[3]], ð_hdr[3]->s_addr);
1263 ether_addr_copy(&ports_eth_addr[dst_port[4]], ð_hdr[4]->s_addr);
1264 ether_addr_copy(&ports_eth_addr[dst_port[5]], ð_hdr[5]->s_addr);
1265 ether_addr_copy(&ports_eth_addr[dst_port[6]], ð_hdr[6]->s_addr);
1266 ether_addr_copy(&ports_eth_addr[dst_port[7]], ð_hdr[7]->s_addr);
1268 send_single_packet(m[0], (uint8_t)dst_port[0]);
1269 send_single_packet(m[1], (uint8_t)dst_port[1]);
1270 send_single_packet(m[2], (uint8_t)dst_port[2]);
1271 send_single_packet(m[3], (uint8_t)dst_port[3]);
1272 send_single_packet(m[4], (uint8_t)dst_port[4]);
1273 send_single_packet(m[5], (uint8_t)dst_port[5]);
1274 send_single_packet(m[6], (uint8_t)dst_port[6]);
1275 send_single_packet(m[7], (uint8_t)dst_port[7]);
1278 #endif /* APP_LOOKUP_METHOD */
1280 static inline __attribute__((always_inline)) void
1281 l3fwd_simple_forward(struct rte_mbuf *m, uint8_t portid)
1283 struct ether_hdr *eth_hdr;
1284 struct ipv4_hdr *ipv4_hdr;
1287 eth_hdr = rte_pktmbuf_mtod(m, struct ether_hdr *);
1289 if (RTE_ETH_IS_IPV4_HDR(m->packet_type)) {
1290 /* Handle IPv4 headers.*/
1291 ipv4_hdr = rte_pktmbuf_mtod_offset(m, struct ipv4_hdr *,
1292 sizeof(struct ether_hdr));
1294 #ifdef DO_RFC_1812_CHECKS
1295 /* Check to make sure the packet is valid (RFC1812) */
1296 if (is_valid_ipv4_pkt(ipv4_hdr, m->pkt_len) < 0) {
1297 rte_pktmbuf_free(m);
1302 dst_port = get_ipv4_dst_port(ipv4_hdr, portid,
1303 RTE_PER_LCORE(lcore_conf)->ipv4_lookup_struct);
1304 if (dst_port >= RTE_MAX_ETHPORTS ||
1305 (enabled_port_mask & 1 << dst_port) == 0)
1308 #ifdef DO_RFC_1812_CHECKS
1309 /* Update time to live and header checksum */
1310 --(ipv4_hdr->time_to_live);
1311 ++(ipv4_hdr->hdr_checksum);
1314 *(uint64_t *)ð_hdr->d_addr = dest_eth_addr[dst_port];
1317 ether_addr_copy(&ports_eth_addr[dst_port], ð_hdr->s_addr);
1319 send_single_packet(m, dst_port);
1320 } else if (RTE_ETH_IS_IPV6_HDR(m->packet_type)) {
1321 /* Handle IPv6 headers.*/
1322 struct ipv6_hdr *ipv6_hdr;
1324 ipv6_hdr = rte_pktmbuf_mtod_offset(m, struct ipv6_hdr *,
1325 sizeof(struct ether_hdr));
1327 dst_port = get_ipv6_dst_port(ipv6_hdr, portid,
1328 RTE_PER_LCORE(lcore_conf)->ipv6_lookup_struct);
1330 if (dst_port >= RTE_MAX_ETHPORTS ||
1331 (enabled_port_mask & 1 << dst_port) == 0)
1335 *(uint64_t *)ð_hdr->d_addr = dest_eth_addr[dst_port];
1338 ether_addr_copy(&ports_eth_addr[dst_port], ð_hdr->s_addr);
1340 send_single_packet(m, dst_port);
1342 /* Free the mbuf that contains non-IPV4/IPV6 packet */
1343 rte_pktmbuf_free(m);
1346 #if ((APP_LOOKUP_METHOD == APP_LOOKUP_LPM) && \
1347 (ENABLE_MULTI_BUFFER_OPTIMIZE == 1))
1348 #ifdef DO_RFC_1812_CHECKS
1350 #define IPV4_MIN_VER_IHL 0x45
1351 #define IPV4_MAX_VER_IHL 0x4f
1352 #define IPV4_MAX_VER_IHL_DIFF (IPV4_MAX_VER_IHL - IPV4_MIN_VER_IHL)
1354 /* Minimum value of IPV4 total length (20B) in network byte order. */
1355 #define IPV4_MIN_LEN_BE (sizeof(struct ipv4_hdr) << 8)
1358 * From http://www.rfc-editor.org/rfc/rfc1812.txt section 5.2.2:
1359 * - The IP version number must be 4.
1360 * - The IP header length field must be large enough to hold the
1361 * minimum length legal IP datagram (20 bytes = 5 words).
1362 * - The IP total length field must be large enough to hold the IP
1363 * datagram header, whose length is specified in the IP header length
1365 * If we encounter invalid IPV4 packet, then set destination port for it
1366 * to BAD_PORT value.
1368 static inline __attribute__((always_inline)) void
1369 rfc1812_process(struct ipv4_hdr *ipv4_hdr, uint16_t *dp, uint32_t ptype)
1373 if (RTE_ETH_IS_IPV4_HDR(ptype)) {
1374 ihl = ipv4_hdr->version_ihl - IPV4_MIN_VER_IHL;
1376 ipv4_hdr->time_to_live--;
1377 ipv4_hdr->hdr_checksum++;
1379 if (ihl > IPV4_MAX_VER_IHL_DIFF ||
1380 ((uint8_t)ipv4_hdr->total_length == 0 &&
1381 ipv4_hdr->total_length < IPV4_MIN_LEN_BE)) {
1388 #define rfc1812_process(mb, dp, ptype) do { } while (0)
1389 #endif /* DO_RFC_1812_CHECKS */
1390 #endif /* APP_LOOKUP_LPM && ENABLE_MULTI_BUFFER_OPTIMIZE */
1393 #if ((APP_LOOKUP_METHOD == APP_LOOKUP_LPM) && \
1394 (ENABLE_MULTI_BUFFER_OPTIMIZE == 1))
1396 static inline __attribute__((always_inline)) uint16_t
1397 get_dst_port(struct rte_mbuf *pkt, uint32_t dst_ipv4, uint8_t portid)
1400 struct ipv6_hdr *ipv6_hdr;
1401 struct ether_hdr *eth_hdr;
1403 if (RTE_ETH_IS_IPV4_HDR(pkt->packet_type)) {
1404 return (uint16_t) ((rte_lpm_lookup(
1405 RTE_PER_LCORE(lcore_conf)->ipv4_lookup_struct, dst_ipv4,
1406 &next_hop) == 0) ? next_hop : portid);
1408 } else if (RTE_ETH_IS_IPV6_HDR(pkt->packet_type)) {
1410 eth_hdr = rte_pktmbuf_mtod(pkt, struct ether_hdr *);
1411 ipv6_hdr = (struct ipv6_hdr *)(eth_hdr + 1);
1413 return (uint16_t) ((rte_lpm6_lookup(
1414 RTE_PER_LCORE(lcore_conf)->ipv6_lookup_struct,
1415 ipv6_hdr->dst_addr, &next_hop) == 0) ?
1424 process_packet(struct rte_mbuf *pkt, uint16_t *dst_port, uint8_t portid)
1426 struct ether_hdr *eth_hdr;
1427 struct ipv4_hdr *ipv4_hdr;
1432 eth_hdr = rte_pktmbuf_mtod(pkt, struct ether_hdr *);
1433 ipv4_hdr = (struct ipv4_hdr *)(eth_hdr + 1);
1435 dst_ipv4 = ipv4_hdr->dst_addr;
1436 dst_ipv4 = rte_be_to_cpu_32(dst_ipv4);
1437 dp = get_dst_port(pkt, dst_ipv4, portid);
1439 te = _mm_load_si128((__m128i *)eth_hdr);
1443 rfc1812_process(ipv4_hdr, dst_port, pkt->packet_type);
1445 te = _mm_blend_epi16(te, ve, MASK_ETH);
1446 _mm_store_si128((__m128i *)eth_hdr, te);
1450 * Read packet_type and destination IPV4 addresses from 4 mbufs.
1453 processx4_step1(struct rte_mbuf *pkt[FWDSTEP],
1455 uint32_t *ipv4_flag)
1457 struct ipv4_hdr *ipv4_hdr;
1458 struct ether_hdr *eth_hdr;
1459 uint32_t x0, x1, x2, x3;
1461 eth_hdr = rte_pktmbuf_mtod(pkt[0], struct ether_hdr *);
1462 ipv4_hdr = (struct ipv4_hdr *)(eth_hdr + 1);
1463 x0 = ipv4_hdr->dst_addr;
1464 ipv4_flag[0] = pkt[0]->packet_type & RTE_PTYPE_L3_IPV4;
1466 eth_hdr = rte_pktmbuf_mtod(pkt[1], struct ether_hdr *);
1467 ipv4_hdr = (struct ipv4_hdr *)(eth_hdr + 1);
1468 x1 = ipv4_hdr->dst_addr;
1469 ipv4_flag[0] &= pkt[1]->packet_type;
1471 eth_hdr = rte_pktmbuf_mtod(pkt[2], struct ether_hdr *);
1472 ipv4_hdr = (struct ipv4_hdr *)(eth_hdr + 1);
1473 x2 = ipv4_hdr->dst_addr;
1474 ipv4_flag[0] &= pkt[2]->packet_type;
1476 eth_hdr = rte_pktmbuf_mtod(pkt[3], struct ether_hdr *);
1477 ipv4_hdr = (struct ipv4_hdr *)(eth_hdr + 1);
1478 x3 = ipv4_hdr->dst_addr;
1479 ipv4_flag[0] &= pkt[3]->packet_type;
1481 dip[0] = _mm_set_epi32(x3, x2, x1, x0);
1485 * Lookup into LPM for destination port.
1486 * If lookup fails, use incoming port (portid) as destination port.
1489 processx4_step2(__m128i dip,
1492 struct rte_mbuf *pkt[FWDSTEP],
1493 uint16_t dprt[FWDSTEP])
1496 const __m128i bswap_mask = _mm_set_epi8(12, 13, 14, 15, 8, 9, 10, 11,
1497 4, 5, 6, 7, 0, 1, 2, 3);
1499 /* Byte swap 4 IPV4 addresses. */
1500 dip = _mm_shuffle_epi8(dip, bswap_mask);
1502 /* if all 4 packets are IPV4. */
1503 if (likely(ipv4_flag)) {
1504 rte_lpm_lookupx4(RTE_PER_LCORE(lcore_conf)->ipv4_lookup_struct, dip,
1507 /* get rid of unused upper 16 bit for each dport. */
1508 dst.x = _mm_packs_epi32(dst.x, dst.x);
1509 *(uint64_t *)dprt = dst.u64[0];
1512 dprt[0] = get_dst_port(pkt[0], dst.u32[0], portid);
1513 dprt[1] = get_dst_port(pkt[1], dst.u32[1], portid);
1514 dprt[2] = get_dst_port(pkt[2], dst.u32[2], portid);
1515 dprt[3] = get_dst_port(pkt[3], dst.u32[3], portid);
1520 * Update source and destination MAC addresses in the ethernet header.
1521 * Perform RFC1812 checks and updates for IPV4 packets.
1524 processx4_step3(struct rte_mbuf *pkt[FWDSTEP], uint16_t dst_port[FWDSTEP])
1526 __m128i te[FWDSTEP];
1527 __m128i ve[FWDSTEP];
1528 __m128i *p[FWDSTEP];
1530 p[0] = rte_pktmbuf_mtod(pkt[0], __m128i *);
1531 p[1] = rte_pktmbuf_mtod(pkt[1], __m128i *);
1532 p[2] = rte_pktmbuf_mtod(pkt[2], __m128i *);
1533 p[3] = rte_pktmbuf_mtod(pkt[3], __m128i *);
1535 ve[0] = val_eth[dst_port[0]];
1536 te[0] = _mm_load_si128(p[0]);
1538 ve[1] = val_eth[dst_port[1]];
1539 te[1] = _mm_load_si128(p[1]);
1541 ve[2] = val_eth[dst_port[2]];
1542 te[2] = _mm_load_si128(p[2]);
1544 ve[3] = val_eth[dst_port[3]];
1545 te[3] = _mm_load_si128(p[3]);
1547 /* Update first 12 bytes, keep rest bytes intact. */
1548 te[0] = _mm_blend_epi16(te[0], ve[0], MASK_ETH);
1549 te[1] = _mm_blend_epi16(te[1], ve[1], MASK_ETH);
1550 te[2] = _mm_blend_epi16(te[2], ve[2], MASK_ETH);
1551 te[3] = _mm_blend_epi16(te[3], ve[3], MASK_ETH);
1553 _mm_store_si128(p[0], te[0]);
1554 _mm_store_si128(p[1], te[1]);
1555 _mm_store_si128(p[2], te[2]);
1556 _mm_store_si128(p[3], te[3]);
1558 rfc1812_process((struct ipv4_hdr *)((struct ether_hdr *)p[0] + 1),
1559 &dst_port[0], pkt[0]->packet_type);
1560 rfc1812_process((struct ipv4_hdr *)((struct ether_hdr *)p[1] + 1),
1561 &dst_port[1], pkt[1]->packet_type);
1562 rfc1812_process((struct ipv4_hdr *)((struct ether_hdr *)p[2] + 1),
1563 &dst_port[2], pkt[2]->packet_type);
1564 rfc1812_process((struct ipv4_hdr *)((struct ether_hdr *)p[3] + 1),
1565 &dst_port[3], pkt[3]->packet_type);
1569 * We group consecutive packets with the same destionation port into one burst.
1570 * To avoid extra latency this is done together with some other packet
1571 * processing, but after we made a final decision about packet's destination.
1572 * To do this we maintain:
1573 * pnum - array of number of consecutive packets with the same dest port for
1574 * each packet in the input burst.
1575 * lp - pointer to the last updated element in the pnum.
1576 * dlp - dest port value lp corresponds to.
1579 #define GRPSZ (1 << FWDSTEP)
1580 #define GRPMSK (GRPSZ - 1)
1582 #define GROUP_PORT_STEP(dlp, dcp, lp, pn, idx) do { \
1583 if (likely((dlp) == (dcp)[(idx)])) { \
1586 (dlp) = (dcp)[idx]; \
1587 (lp) = (pn) + (idx); \
1593 * Group consecutive packets with the same destination port in bursts of 4.
1594 * Suppose we have array of destionation ports:
1595 * dst_port[] = {a, b, c, d,, e, ... }
1596 * dp1 should contain: <a, b, c, d>, dp2: <b, c, d, e>.
1597 * We doing 4 comparisions at once and the result is 4 bit mask.
1598 * This mask is used as an index into prebuild array of pnum values.
1600 static inline uint16_t *
1601 port_groupx4(uint16_t pn[FWDSTEP + 1], uint16_t *lp, __m128i dp1, __m128i dp2)
1603 static const struct {
1604 uint64_t pnum; /* prebuild 4 values for pnum[]. */
1605 int32_t idx; /* index for new last updated elemnet. */
1606 uint16_t lpv; /* add value to the last updated element. */
1609 /* 0: a != b, b != c, c != d, d != e */
1610 .pnum = UINT64_C(0x0001000100010001),
1615 /* 1: a == b, b != c, c != d, d != e */
1616 .pnum = UINT64_C(0x0001000100010002),
1621 /* 2: a != b, b == c, c != d, d != e */
1622 .pnum = UINT64_C(0x0001000100020001),
1627 /* 3: a == b, b == c, c != d, d != e */
1628 .pnum = UINT64_C(0x0001000100020003),
1633 /* 4: a != b, b != c, c == d, d != e */
1634 .pnum = UINT64_C(0x0001000200010001),
1639 /* 5: a == b, b != c, c == d, d != e */
1640 .pnum = UINT64_C(0x0001000200010002),
1645 /* 6: a != b, b == c, c == d, d != e */
1646 .pnum = UINT64_C(0x0001000200030001),
1651 /* 7: a == b, b == c, c == d, d != e */
1652 .pnum = UINT64_C(0x0001000200030004),
1657 /* 8: a != b, b != c, c != d, d == e */
1658 .pnum = UINT64_C(0x0002000100010001),
1663 /* 9: a == b, b != c, c != d, d == e */
1664 .pnum = UINT64_C(0x0002000100010002),
1669 /* 0xa: a != b, b == c, c != d, d == e */
1670 .pnum = UINT64_C(0x0002000100020001),
1675 /* 0xb: a == b, b == c, c != d, d == e */
1676 .pnum = UINT64_C(0x0002000100020003),
1681 /* 0xc: a != b, b != c, c == d, d == e */
1682 .pnum = UINT64_C(0x0002000300010001),
1687 /* 0xd: a == b, b != c, c == d, d == e */
1688 .pnum = UINT64_C(0x0002000300010002),
1693 /* 0xe: a != b, b == c, c == d, d == e */
1694 .pnum = UINT64_C(0x0002000300040001),
1699 /* 0xf: a == b, b == c, c == d, d == e */
1700 .pnum = UINT64_C(0x0002000300040005),
1707 uint16_t u16[FWDSTEP + 1];
1709 } *pnum = (void *)pn;
1713 dp1 = _mm_cmpeq_epi16(dp1, dp2);
1714 dp1 = _mm_unpacklo_epi16(dp1, dp1);
1715 v = _mm_movemask_ps((__m128)dp1);
1717 /* update last port counter. */
1718 lp[0] += gptbl[v].lpv;
1720 /* if dest port value has changed. */
1722 pnum->u64 = gptbl[v].pnum;
1723 pnum->u16[FWDSTEP] = 1;
1724 lp = pnum->u16 + gptbl[v].idx;
1730 #endif /* APP_LOOKUP_METHOD */
1733 process_burst(struct rte_mbuf *pkts_burst[MAX_PKT_BURST], int nb_rx,
1738 #if ((APP_LOOKUP_METHOD == APP_LOOKUP_LPM) && \
1739 (ENABLE_MULTI_BUFFER_OPTIMIZE == 1))
1743 uint16_t dst_port[MAX_PKT_BURST];
1744 __m128i dip[MAX_PKT_BURST / FWDSTEP];
1745 uint32_t ipv4_flag[MAX_PKT_BURST / FWDSTEP];
1746 uint16_t pnum[MAX_PKT_BURST + 1];
1750 #if (ENABLE_MULTI_BUFFER_OPTIMIZE == 1)
1751 #if (APP_LOOKUP_METHOD == APP_LOOKUP_EXACT_MATCH)
1754 * Send nb_rx - nb_rx%8 packets
1757 int32_t n = RTE_ALIGN_FLOOR(nb_rx, 8);
1759 for (j = 0; j < n; j += 8) {
1761 pkts_burst[j]->packet_type &
1762 pkts_burst[j+1]->packet_type &
1763 pkts_burst[j+2]->packet_type &
1764 pkts_burst[j+3]->packet_type &
1765 pkts_burst[j+4]->packet_type &
1766 pkts_burst[j+5]->packet_type &
1767 pkts_burst[j+6]->packet_type &
1768 pkts_burst[j+7]->packet_type;
1769 if (pkt_type & RTE_PTYPE_L3_IPV4) {
1770 simple_ipv4_fwd_8pkts(&pkts_burst[j], portid);
1771 } else if (pkt_type &
1772 RTE_PTYPE_L3_IPV6) {
1773 simple_ipv6_fwd_8pkts(&pkts_burst[j], portid);
1775 l3fwd_simple_forward(pkts_burst[j], portid);
1776 l3fwd_simple_forward(pkts_burst[j+1], portid);
1777 l3fwd_simple_forward(pkts_burst[j+2], portid);
1778 l3fwd_simple_forward(pkts_burst[j+3], portid);
1779 l3fwd_simple_forward(pkts_burst[j+4], portid);
1780 l3fwd_simple_forward(pkts_burst[j+5], portid);
1781 l3fwd_simple_forward(pkts_burst[j+6], portid);
1782 l3fwd_simple_forward(pkts_burst[j+7], portid);
1785 for (; j < nb_rx ; j++)
1786 l3fwd_simple_forward(pkts_burst[j], portid);
1788 #elif (APP_LOOKUP_METHOD == APP_LOOKUP_LPM)
1790 k = RTE_ALIGN_FLOOR(nb_rx, FWDSTEP);
1791 for (j = 0; j != k; j += FWDSTEP)
1792 processx4_step1(&pkts_burst[j], &dip[j / FWDSTEP],
1793 &ipv4_flag[j / FWDSTEP]);
1795 k = RTE_ALIGN_FLOOR(nb_rx, FWDSTEP);
1796 for (j = 0; j != k; j += FWDSTEP)
1797 processx4_step2(dip[j / FWDSTEP], ipv4_flag[j / FWDSTEP],
1798 portid, &pkts_burst[j], &dst_port[j]);
1801 * Finish packet processing and group consecutive
1802 * packets with the same destination port.
1804 k = RTE_ALIGN_FLOOR(nb_rx, FWDSTEP);
1811 processx4_step3(pkts_burst, dst_port);
1813 /* dp1: <d[0], d[1], d[2], d[3], ... > */
1814 dp1 = _mm_loadu_si128((__m128i *)dst_port);
1816 for (j = FWDSTEP; j != k; j += FWDSTEP) {
1817 processx4_step3(&pkts_burst[j], &dst_port[j]);
1821 * <d[j-3], d[j-2], d[j-1], d[j], ... >
1823 dp2 = _mm_loadu_si128(
1824 (__m128i *)&dst_port[j - FWDSTEP + 1]);
1825 lp = port_groupx4(&pnum[j - FWDSTEP], lp, dp1, dp2);
1829 * <d[j], d[j+1], d[j+2], d[j+3], ... >
1831 dp1 = _mm_srli_si128(dp2, (FWDSTEP - 1) *
1832 sizeof(dst_port[0]));
1836 * dp2: <d[j-3], d[j-2], d[j-1], d[j-1], ... >
1838 dp2 = _mm_shufflelo_epi16(dp1, 0xf9);
1839 lp = port_groupx4(&pnum[j - FWDSTEP], lp, dp1, dp2);
1842 * remove values added by the last repeated
1846 dlp = dst_port[j - 1];
1848 /* set dlp and lp to the never used values. */
1850 lp = pnum + MAX_PKT_BURST;
1853 /* Process up to last 3 packets one by one. */
1854 switch (nb_rx % FWDSTEP) {
1856 process_packet(pkts_burst[j], dst_port + j, portid);
1857 GROUP_PORT_STEP(dlp, dst_port, lp, pnum, j);
1860 process_packet(pkts_burst[j], dst_port + j, portid);
1861 GROUP_PORT_STEP(dlp, dst_port, lp, pnum, j);
1864 process_packet(pkts_burst[j], dst_port + j, portid);
1865 GROUP_PORT_STEP(dlp, dst_port, lp, pnum, j);
1870 * Send packets out, through destination port.
1871 * Consecuteve pacekts with the same destination port
1872 * are already grouped together.
1873 * If destination port for the packet equals BAD_PORT,
1874 * then free the packet without sending it out.
1876 for (j = 0; j < nb_rx; j += k) {
1884 if (likely(pn != BAD_PORT))
1885 send_packetsx4(pn, pkts_burst + j, k);
1887 for (m = j; m != j + k; m++)
1888 rte_pktmbuf_free(pkts_burst[m]);
1892 #endif /* APP_LOOKUP_METHOD */
1893 #else /* ENABLE_MULTI_BUFFER_OPTIMIZE == 0 */
1895 /* Prefetch first packets */
1896 for (j = 0; j < PREFETCH_OFFSET && j < nb_rx; j++)
1897 rte_prefetch0(rte_pktmbuf_mtod(pkts_burst[j], void *));
1899 /* Prefetch and forward already prefetched packets */
1900 for (j = 0; j < (nb_rx - PREFETCH_OFFSET); j++) {
1901 rte_prefetch0(rte_pktmbuf_mtod(pkts_burst[
1902 j + PREFETCH_OFFSET], void *));
1903 l3fwd_simple_forward(pkts_burst[j], portid);
1906 /* Forward remaining prefetched packets */
1907 for (; j < nb_rx; j++)
1908 l3fwd_simple_forward(pkts_burst[j], portid);
1910 #endif /* ENABLE_MULTI_BUFFER_OPTIMIZE */
1914 #if (APP_CPU_LOAD > 0)
1917 * CPU-load stats collector
1920 cpu_load_collector(__rte_unused void *arg) {
1923 uint64_t prev_tsc, diff_tsc, cur_tsc;
1924 uint64_t total[MAX_CPU] = { 0 };
1925 unsigned min_cpu = MAX_CPU;
1926 unsigned max_cpu = 0;
1931 unsigned int n_thread_per_cpu[MAX_CPU] = { 0 };
1932 struct thread_conf *thread_per_cpu[MAX_CPU][MAX_THREAD];
1934 struct thread_conf *thread_conf;
1936 const uint64_t interval_tsc = (rte_get_tsc_hz() + US_PER_S - 1) /
1937 US_PER_S * CPU_LOAD_TIMEOUT_US;
1941 * Wait for all threads
1944 printf("Waiting for %d rx threads and %d tx threads\n", n_rx_thread,
1947 while (rte_atomic16_read(&rx_counter) < n_rx_thread)
1950 while (rte_atomic16_read(&tx_counter) < n_tx_thread)
1953 for (i = 0; i < n_rx_thread; i++) {
1955 thread_conf = &rx_thread[i].conf;
1956 cpu_id = thread_conf->cpu_id;
1957 thread_per_cpu[cpu_id][n_thread_per_cpu[cpu_id]++] = thread_conf;
1959 if (cpu_id > max_cpu)
1961 if (cpu_id < min_cpu)
1964 for (i = 0; i < n_tx_thread; i++) {
1966 thread_conf = &tx_thread[i].conf;
1967 cpu_id = thread_conf->cpu_id;
1968 thread_per_cpu[cpu_id][n_thread_per_cpu[cpu_id]++] = thread_conf;
1970 if (thread_conf->cpu_id > max_cpu)
1971 max_cpu = thread_conf->cpu_id;
1972 if (thread_conf->cpu_id < min_cpu)
1973 min_cpu = thread_conf->cpu_id;
1979 for (i = min_cpu; i <= max_cpu; i++) {
1980 for (j = 0; j < MAX_CPU_COUNTER; j++) {
1981 for (k = 0; k < n_thread_per_cpu[i]; k++)
1982 if (thread_per_cpu[i][k]->busy[j]) {
1987 cpu_load.hits[j][i]++;
1999 cur_tsc = rte_rdtsc();
2001 diff_tsc = cur_tsc - prev_tsc;
2002 if (unlikely(diff_tsc > interval_tsc)) {
2006 printf("Cpu usage for %d rx threads and %d tx threads:\n\n",
2007 n_rx_thread, n_tx_thread);
2009 printf("cpu# proc%% poll%% overhead%%\n\n");
2011 for (i = min_cpu; i <= max_cpu; i++) {
2013 printf("CPU %d:", i);
2014 for (j = 0; j < MAX_CPU_COUNTER; j++) {
2015 printf("%7" PRIu64 "",
2016 cpu_load.hits[j][i] * 100 / cpu_load.counter);
2017 hits += cpu_load.hits[j][i];
2018 cpu_load.hits[j][i] = 0;
2020 printf("%7" PRIu64 "\n",
2021 100 - total[i] * 100 / cpu_load.counter);
2024 cpu_load.counter = 0;
2031 #endif /* APP_CPU_LOAD */
2034 * Null processing lthread loop
2036 * This loop is used to start empty scheduler on lcore.
2039 lthread_null(__rte_unused void *args)
2041 int lcore_id = rte_lcore_id();
2043 RTE_LOG(INFO, L3FWD, "Starting scheduler on lcore %d.\n", lcore_id);
2047 /* main processing loop */
2049 lthread_tx_per_ring(void *dummy)
2053 struct rte_ring *ring;
2054 struct thread_tx_conf *tx_conf;
2055 struct rte_mbuf *pkts_burst[MAX_PKT_BURST];
2056 struct lthread_cond *ready;
2058 tx_conf = (struct thread_tx_conf *)dummy;
2059 ring = tx_conf->ring;
2060 ready = *tx_conf->ready;
2062 lthread_set_data((void *)tx_conf);
2065 * Move this lthread to lcore
2067 lthread_set_affinity(tx_conf->conf.lcore_id);
2069 RTE_LOG(INFO, L3FWD, "entering main tx loop on lcore %u\n", rte_lcore_id());
2072 rte_atomic16_inc(&tx_counter);
2076 * Read packet from ring
2078 SET_CPU_BUSY(tx_conf, CPU_POLL);
2079 nb_rx = rte_ring_sc_dequeue_burst(ring, (void **)pkts_burst,
2080 MAX_PKT_BURST, NULL);
2081 SET_CPU_IDLE(tx_conf, CPU_POLL);
2084 SET_CPU_BUSY(tx_conf, CPU_PROCESS);
2085 portid = pkts_burst[0]->port;
2086 process_burst(pkts_burst, nb_rx, portid);
2087 SET_CPU_IDLE(tx_conf, CPU_PROCESS);
2090 lthread_cond_wait(ready, 0);
2096 * Main tx-lthreads spawner lthread.
2098 * This lthread is used to spawn one new lthread per ring from producers.
2102 lthread_tx(void *args)
2108 struct thread_tx_conf *tx_conf;
2110 tx_conf = (struct thread_tx_conf *)args;
2111 lthread_set_data((void *)tx_conf);
2114 * Move this lthread to the selected lcore
2116 lthread_set_affinity(tx_conf->conf.lcore_id);
2119 * Spawn tx readers (one per input ring)
2121 lthread_create(<, tx_conf->conf.lcore_id, lthread_tx_per_ring,
2124 lcore_id = rte_lcore_id();
2126 RTE_LOG(INFO, L3FWD, "Entering Tx main loop on lcore %u\n", lcore_id);
2128 tx_conf->conf.cpu_id = sched_getcpu();
2131 lthread_sleep(BURST_TX_DRAIN_US * 1000);
2134 * TX burst queue drain
2136 for (portid = 0; portid < RTE_MAX_ETHPORTS; portid++) {
2137 if (tx_conf->tx_mbufs[portid].len == 0)
2139 SET_CPU_BUSY(tx_conf, CPU_PROCESS);
2140 send_burst(tx_conf, tx_conf->tx_mbufs[portid].len, portid);
2141 SET_CPU_IDLE(tx_conf, CPU_PROCESS);
2142 tx_conf->tx_mbufs[portid].len = 0;
2149 lthread_rx(void *dummy)
2154 uint8_t portid, queueid;
2156 int len[RTE_MAX_LCORE] = { 0 };
2157 int old_len, new_len;
2158 struct rte_mbuf *pkts_burst[MAX_PKT_BURST];
2159 struct thread_rx_conf *rx_conf;
2161 rx_conf = (struct thread_rx_conf *)dummy;
2162 lthread_set_data((void *)rx_conf);
2165 * Move this lthread to lcore
2167 lthread_set_affinity(rx_conf->conf.lcore_id);
2169 if (rx_conf->n_rx_queue == 0) {
2170 RTE_LOG(INFO, L3FWD, "lcore %u has nothing to do\n", rte_lcore_id());
2174 RTE_LOG(INFO, L3FWD, "Entering main Rx loop on lcore %u\n", rte_lcore_id());
2176 for (i = 0; i < rx_conf->n_rx_queue; i++) {
2178 portid = rx_conf->rx_queue_list[i].port_id;
2179 queueid = rx_conf->rx_queue_list[i].queue_id;
2180 RTE_LOG(INFO, L3FWD, " -- lcoreid=%u portid=%hhu rxqueueid=%hhu\n",
2181 rte_lcore_id(), portid, queueid);
2185 * Init all condition variables (one per rx thread)
2187 for (i = 0; i < rx_conf->n_rx_queue; i++)
2188 lthread_cond_init(NULL, &rx_conf->ready[i], NULL);
2192 rx_conf->conf.cpu_id = sched_getcpu();
2193 rte_atomic16_inc(&rx_counter);
2197 * Read packet from RX queues
2199 for (i = 0; i < rx_conf->n_rx_queue; ++i) {
2200 portid = rx_conf->rx_queue_list[i].port_id;
2201 queueid = rx_conf->rx_queue_list[i].queue_id;
2203 SET_CPU_BUSY(rx_conf, CPU_POLL);
2204 nb_rx = rte_eth_rx_burst(portid, queueid, pkts_burst,
2206 SET_CPU_IDLE(rx_conf, CPU_POLL);
2209 worker_id = (worker_id + 1) % rx_conf->n_ring;
2210 old_len = len[worker_id];
2212 SET_CPU_BUSY(rx_conf, CPU_PROCESS);
2213 ret = rte_ring_sp_enqueue_burst(
2214 rx_conf->ring[worker_id],
2215 (void **) pkts_burst,
2218 new_len = old_len + ret;
2220 if (new_len >= BURST_SIZE) {
2221 lthread_cond_signal(rx_conf->ready[worker_id]);
2225 len[worker_id] = new_len;
2227 if (unlikely(ret < nb_rx)) {
2230 for (k = ret; k < nb_rx; k++) {
2231 struct rte_mbuf *m = pkts_burst[k];
2233 rte_pktmbuf_free(m);
2236 SET_CPU_IDLE(rx_conf, CPU_PROCESS);
2245 * Start scheduler with initial lthread on lcore
2247 * This lthread loop spawns all rx and tx lthreads on master lcore
2251 lthread_spawner(__rte_unused void *arg) {
2252 struct lthread *lt[MAX_THREAD];
2256 printf("Entering lthread_spawner\n");
2259 * Create producers (rx threads) on default lcore
2261 for (i = 0; i < n_rx_thread; i++) {
2262 rx_thread[i].conf.thread_id = i;
2263 lthread_create(<[n_thread], -1, lthread_rx,
2264 (void *)&rx_thread[i]);
2269 * Wait for all producers. Until some producers can be started on the same
2270 * scheduler as this lthread, yielding is required to let them to run and
2271 * prevent deadlock here.
2273 while (rte_atomic16_read(&rx_counter) < n_rx_thread)
2274 lthread_sleep(100000);
2277 * Create consumers (tx threads) on default lcore_id
2279 for (i = 0; i < n_tx_thread; i++) {
2280 tx_thread[i].conf.thread_id = i;
2281 lthread_create(<[n_thread], -1, lthread_tx,
2282 (void *)&tx_thread[i]);
2287 * Wait for all threads finished
2289 for (i = 0; i < n_thread; i++)
2290 lthread_join(lt[i], NULL);
2295 * Start master scheduler with initial lthread spawning rx and tx lthreads
2296 * (main_lthread_master).
2299 lthread_master_spawner(__rte_unused void *arg) {
2301 int lcore_id = rte_lcore_id();
2303 RTE_PER_LCORE(lcore_conf) = &lcore_conf[lcore_id];
2304 lthread_create(<, -1, lthread_spawner, NULL);
2311 * Start scheduler on lcore.
2314 sched_spawner(__rte_unused void *arg) {
2316 int lcore_id = rte_lcore_id();
2319 if (lcore_id == cpu_load_lcore_id) {
2320 cpu_load_collector(arg);
2323 #endif /* APP_CPU_LOAD */
2325 RTE_PER_LCORE(lcore_conf) = &lcore_conf[lcore_id];
2326 lthread_create(<, -1, lthread_null, NULL);
2332 /* main processing loop */
2334 pthread_tx(void *dummy)
2336 struct rte_mbuf *pkts_burst[MAX_PKT_BURST];
2337 uint64_t prev_tsc, diff_tsc, cur_tsc;
2340 struct thread_tx_conf *tx_conf;
2342 const uint64_t drain_tsc = (rte_get_tsc_hz() + US_PER_S - 1) /
2343 US_PER_S * BURST_TX_DRAIN_US;
2347 tx_conf = (struct thread_tx_conf *)dummy;
2349 RTE_LOG(INFO, L3FWD, "Entering main Tx loop on lcore %u\n", rte_lcore_id());
2351 tx_conf->conf.cpu_id = sched_getcpu();
2352 rte_atomic16_inc(&tx_counter);
2355 cur_tsc = rte_rdtsc();
2358 * TX burst queue drain
2360 diff_tsc = cur_tsc - prev_tsc;
2361 if (unlikely(diff_tsc > drain_tsc)) {
2364 * This could be optimized (use queueid instead of
2365 * portid), but it is not called so often
2367 SET_CPU_BUSY(tx_conf, CPU_PROCESS);
2368 for (portid = 0; portid < RTE_MAX_ETHPORTS; portid++) {
2369 if (tx_conf->tx_mbufs[portid].len == 0)
2371 send_burst(tx_conf, tx_conf->tx_mbufs[portid].len, portid);
2372 tx_conf->tx_mbufs[portid].len = 0;
2374 SET_CPU_IDLE(tx_conf, CPU_PROCESS);
2380 * Read packet from ring
2382 SET_CPU_BUSY(tx_conf, CPU_POLL);
2383 nb_rx = rte_ring_sc_dequeue_burst(tx_conf->ring,
2384 (void **)pkts_burst, MAX_PKT_BURST, NULL);
2385 SET_CPU_IDLE(tx_conf, CPU_POLL);
2387 if (unlikely(nb_rx == 0)) {
2392 SET_CPU_BUSY(tx_conf, CPU_PROCESS);
2393 portid = pkts_burst[0]->port;
2394 process_burst(pkts_burst, nb_rx, portid);
2395 SET_CPU_IDLE(tx_conf, CPU_PROCESS);
2401 pthread_rx(void *dummy)
2408 uint8_t portid, queueid;
2409 struct rte_mbuf *pkts_burst[MAX_PKT_BURST];
2411 struct thread_rx_conf *rx_conf;
2413 lcore_id = rte_lcore_id();
2414 rx_conf = (struct thread_rx_conf *)dummy;
2416 if (rx_conf->n_rx_queue == 0) {
2417 RTE_LOG(INFO, L3FWD, "lcore %u has nothing to do\n", lcore_id);
2421 RTE_LOG(INFO, L3FWD, "entering main rx loop on lcore %u\n", lcore_id);
2423 for (i = 0; i < rx_conf->n_rx_queue; i++) {
2425 portid = rx_conf->rx_queue_list[i].port_id;
2426 queueid = rx_conf->rx_queue_list[i].queue_id;
2427 RTE_LOG(INFO, L3FWD, " -- lcoreid=%u portid=%hhu rxqueueid=%hhu\n",
2428 lcore_id, portid, queueid);
2432 rx_conf->conf.cpu_id = sched_getcpu();
2433 rte_atomic16_inc(&rx_counter);
2437 * Read packet from RX queues
2439 for (i = 0; i < rx_conf->n_rx_queue; ++i) {
2440 portid = rx_conf->rx_queue_list[i].port_id;
2441 queueid = rx_conf->rx_queue_list[i].queue_id;
2443 SET_CPU_BUSY(rx_conf, CPU_POLL);
2444 nb_rx = rte_eth_rx_burst(portid, queueid, pkts_burst,
2446 SET_CPU_IDLE(rx_conf, CPU_POLL);
2453 SET_CPU_BUSY(rx_conf, CPU_PROCESS);
2454 worker_id = (worker_id + 1) % rx_conf->n_ring;
2455 n = rte_ring_sp_enqueue_burst(rx_conf->ring[worker_id],
2456 (void **)pkts_burst, nb_rx, NULL);
2458 if (unlikely(n != nb_rx)) {
2461 for (k = n; k < nb_rx; k++) {
2462 struct rte_mbuf *m = pkts_burst[k];
2464 rte_pktmbuf_free(m);
2468 SET_CPU_IDLE(rx_conf, CPU_PROCESS);
2478 pthread_run(__rte_unused void *arg) {
2479 int lcore_id = rte_lcore_id();
2482 for (i = 0; i < n_rx_thread; i++)
2483 if (rx_thread[i].conf.lcore_id == lcore_id) {
2484 printf("Start rx thread on %d...\n", lcore_id);
2485 RTE_PER_LCORE(lcore_conf) = &lcore_conf[lcore_id];
2486 RTE_PER_LCORE(lcore_conf)->data = (void *)&rx_thread[i];
2487 pthread_rx((void *)&rx_thread[i]);
2491 for (i = 0; i < n_tx_thread; i++)
2492 if (tx_thread[i].conf.lcore_id == lcore_id) {
2493 printf("Start tx thread on %d...\n", lcore_id);
2494 RTE_PER_LCORE(lcore_conf) = &lcore_conf[lcore_id];
2495 RTE_PER_LCORE(lcore_conf)->data = (void *)&tx_thread[i];
2496 pthread_tx((void *)&tx_thread[i]);
2501 if (lcore_id == cpu_load_lcore_id)
2502 cpu_load_collector(arg);
2503 #endif /* APP_CPU_LOAD */
2509 check_lcore_params(void)
2511 uint8_t queue, lcore;
2515 for (i = 0; i < nb_rx_thread_params; ++i) {
2516 queue = rx_thread_params[i].queue_id;
2517 if (queue >= MAX_RX_QUEUE_PER_PORT) {
2518 printf("invalid queue number: %hhu\n", queue);
2521 lcore = rx_thread_params[i].lcore_id;
2522 if (!rte_lcore_is_enabled(lcore)) {
2523 printf("error: lcore %hhu is not enabled in lcore mask\n", lcore);
2526 socketid = rte_lcore_to_socket_id(lcore);
2527 if ((socketid != 0) && (numa_on == 0))
2528 printf("warning: lcore %hhu is on socket %d with numa off\n",
2535 check_port_config(const unsigned nb_ports)
2540 for (i = 0; i < nb_rx_thread_params; ++i) {
2541 portid = rx_thread_params[i].port_id;
2542 if ((enabled_port_mask & (1 << portid)) == 0) {
2543 printf("port %u is not enabled in port mask\n", portid);
2546 if (portid >= nb_ports) {
2547 printf("port %u is not present on the board\n", portid);
2555 get_port_n_rx_queues(const uint8_t port)
2560 for (i = 0; i < nb_rx_thread_params; ++i)
2561 if (rx_thread_params[i].port_id == port &&
2562 rx_thread_params[i].queue_id > queue)
2563 queue = rx_thread_params[i].queue_id;
2565 return (uint8_t)(++queue);
2572 struct thread_rx_conf *rx_conf;
2573 struct thread_tx_conf *tx_conf;
2574 unsigned rx_thread_id, tx_thread_id;
2576 struct rte_ring *ring = NULL;
2578 for (tx_thread_id = 0; tx_thread_id < n_tx_thread; tx_thread_id++) {
2580 tx_conf = &tx_thread[tx_thread_id];
2582 printf("Connecting tx-thread %d with rx-thread %d\n", tx_thread_id,
2583 tx_conf->conf.thread_id);
2585 rx_thread_id = tx_conf->conf.thread_id;
2586 if (rx_thread_id > n_tx_thread) {
2587 printf("connection from tx-thread %u to rx-thread %u fails "
2588 "(rx-thread not defined)\n", tx_thread_id, rx_thread_id);
2592 rx_conf = &rx_thread[rx_thread_id];
2593 socket_io = rte_lcore_to_socket_id(rx_conf->conf.lcore_id);
2595 snprintf(name, sizeof(name), "app_ring_s%u_rx%u_tx%u",
2596 socket_io, rx_thread_id, tx_thread_id);
2598 ring = rte_ring_create(name, 1024 * 4, socket_io,
2599 RING_F_SP_ENQ | RING_F_SC_DEQ);
2602 rte_panic("Cannot create ring to connect rx-thread %u "
2603 "with tx-thread %u\n", rx_thread_id, tx_thread_id);
2606 rx_conf->ring[rx_conf->n_ring] = ring;
2608 tx_conf->ring = ring;
2609 tx_conf->ready = &rx_conf->ready[rx_conf->n_ring];
2617 init_rx_queues(void)
2619 uint16_t i, nb_rx_queue;
2624 for (i = 0; i < nb_rx_thread_params; ++i) {
2625 thread = rx_thread_params[i].thread_id;
2626 nb_rx_queue = rx_thread[thread].n_rx_queue;
2628 if (nb_rx_queue >= MAX_RX_QUEUE_PER_LCORE) {
2629 printf("error: too many queues (%u) for thread: %u\n",
2630 (unsigned)nb_rx_queue + 1, (unsigned)thread);
2634 rx_thread[thread].conf.thread_id = thread;
2635 rx_thread[thread].conf.lcore_id = rx_thread_params[i].lcore_id;
2636 rx_thread[thread].rx_queue_list[nb_rx_queue].port_id =
2637 rx_thread_params[i].port_id;
2638 rx_thread[thread].rx_queue_list[nb_rx_queue].queue_id =
2639 rx_thread_params[i].queue_id;
2640 rx_thread[thread].n_rx_queue++;
2642 if (thread >= n_rx_thread)
2643 n_rx_thread = thread + 1;
2650 init_tx_threads(void)
2655 for (i = 0; i < nb_tx_thread_params; ++i) {
2656 tx_thread[n_tx_thread].conf.thread_id = tx_thread_params[i].thread_id;
2657 tx_thread[n_tx_thread].conf.lcore_id = tx_thread_params[i].lcore_id;
2665 print_usage(const char *prgname)
2667 printf("%s [EAL options] -- -p PORTMASK -P"
2668 " [--rx (port,queue,lcore,thread)[,(port,queue,lcore,thread]]"
2669 " [--tx (lcore,thread)[,(lcore,thread]]"
2670 " [--enable-jumbo [--max-pkt-len PKTLEN]]\n"
2671 " [--parse-ptype]\n\n"
2672 " -p PORTMASK: hexadecimal bitmask of ports to configure\n"
2673 " -P : enable promiscuous mode\n"
2674 " --rx (port,queue,lcore,thread): rx queues configuration\n"
2675 " --tx (lcore,thread): tx threads configuration\n"
2676 " --stat-lcore LCORE: use lcore for stat collector\n"
2677 " --eth-dest=X,MM:MM:MM:MM:MM:MM: optional, ethernet destination for port X\n"
2678 " --no-numa: optional, disable numa awareness\n"
2679 " --ipv6: optional, specify it if running ipv6 packets\n"
2680 " --enable-jumbo: enable jumbo frame"
2681 " which max packet len is PKTLEN in decimal (64-9600)\n"
2682 " --hash-entry-num: specify the hash entry number in hexadecimal to be setup\n"
2683 " --no-lthreads: turn off lthread model\n"
2684 " --parse-ptype: set to use software to analyze packet type\n\n",
2688 static int parse_max_pkt_len(const char *pktlen)
2693 /* parse decimal string */
2694 len = strtoul(pktlen, &end, 10);
2695 if ((pktlen[0] == '\0') || (end == NULL) || (*end != '\0'))
2705 parse_portmask(const char *portmask)
2710 /* parse hexadecimal string */
2711 pm = strtoul(portmask, &end, 16);
2712 if ((portmask[0] == '\0') || (end == NULL) || (*end != '\0'))
2721 #if (APP_LOOKUP_METHOD == APP_LOOKUP_EXACT_MATCH)
2723 parse_hash_entry_number(const char *hash_entry_num)
2726 unsigned long hash_en;
2728 /* parse hexadecimal string */
2729 hash_en = strtoul(hash_entry_num, &end, 16);
2730 if ((hash_entry_num[0] == '\0') || (end == NULL) || (*end != '\0'))
2741 parse_rx_config(const char *q_arg)
2744 const char *p, *p0 = q_arg;
2753 unsigned long int_fld[_NUM_FLD];
2754 char *str_fld[_NUM_FLD];
2758 nb_rx_thread_params = 0;
2760 while ((p = strchr(p0, '(')) != NULL) {
2762 p0 = strchr(p, ')');
2767 if (size >= sizeof(s))
2770 snprintf(s, sizeof(s), "%.*s", size, p);
2771 if (rte_strsplit(s, sizeof(s), str_fld, _NUM_FLD, ',') != _NUM_FLD)
2773 for (i = 0; i < _NUM_FLD; i++) {
2775 int_fld[i] = strtoul(str_fld[i], &end, 0);
2776 if (errno != 0 || end == str_fld[i] || int_fld[i] > 255)
2779 if (nb_rx_thread_params >= MAX_LCORE_PARAMS) {
2780 printf("exceeded max number of rx params: %hu\n",
2781 nb_rx_thread_params);
2784 rx_thread_params_array[nb_rx_thread_params].port_id =
2785 (uint8_t)int_fld[FLD_PORT];
2786 rx_thread_params_array[nb_rx_thread_params].queue_id =
2787 (uint8_t)int_fld[FLD_QUEUE];
2788 rx_thread_params_array[nb_rx_thread_params].lcore_id =
2789 (uint8_t)int_fld[FLD_LCORE];
2790 rx_thread_params_array[nb_rx_thread_params].thread_id =
2791 (uint8_t)int_fld[FLD_THREAD];
2792 ++nb_rx_thread_params;
2794 rx_thread_params = rx_thread_params_array;
2799 parse_tx_config(const char *q_arg)
2802 const char *p, *p0 = q_arg;
2809 unsigned long int_fld[_NUM_FLD];
2810 char *str_fld[_NUM_FLD];
2814 nb_tx_thread_params = 0;
2816 while ((p = strchr(p0, '(')) != NULL) {
2818 p0 = strchr(p, ')');
2823 if (size >= sizeof(s))
2826 snprintf(s, sizeof(s), "%.*s", size, p);
2827 if (rte_strsplit(s, sizeof(s), str_fld, _NUM_FLD, ',') != _NUM_FLD)
2829 for (i = 0; i < _NUM_FLD; i++) {
2831 int_fld[i] = strtoul(str_fld[i], &end, 0);
2832 if (errno != 0 || end == str_fld[i] || int_fld[i] > 255)
2835 if (nb_tx_thread_params >= MAX_LCORE_PARAMS) {
2836 printf("exceeded max number of tx params: %hu\n",
2837 nb_tx_thread_params);
2840 tx_thread_params_array[nb_tx_thread_params].lcore_id =
2841 (uint8_t)int_fld[FLD_LCORE];
2842 tx_thread_params_array[nb_tx_thread_params].thread_id =
2843 (uint8_t)int_fld[FLD_THREAD];
2844 ++nb_tx_thread_params;
2846 tx_thread_params = tx_thread_params_array;
2851 #if (APP_CPU_LOAD > 0)
2853 parse_stat_lcore(const char *stat_lcore)
2856 unsigned long lcore_id;
2858 lcore_id = strtoul(stat_lcore, &end, 10);
2859 if ((stat_lcore[0] == '\0') || (end == NULL) || (*end != '\0'))
2867 parse_eth_dest(const char *optarg)
2871 uint8_t c, *dest, peer_addr[6];
2874 portid = strtoul(optarg, &port_end, 10);
2875 if (errno != 0 || port_end == optarg || *port_end++ != ',')
2876 rte_exit(EXIT_FAILURE,
2877 "Invalid eth-dest: %s", optarg);
2878 if (portid >= RTE_MAX_ETHPORTS)
2879 rte_exit(EXIT_FAILURE,
2880 "eth-dest: port %d >= RTE_MAX_ETHPORTS(%d)\n",
2881 portid, RTE_MAX_ETHPORTS);
2883 if (cmdline_parse_etheraddr(NULL, port_end,
2884 &peer_addr, sizeof(peer_addr)) < 0)
2885 rte_exit(EXIT_FAILURE,
2886 "Invalid ethernet address: %s\n",
2888 dest = (uint8_t *)&dest_eth_addr[portid];
2889 for (c = 0; c < 6; c++)
2890 dest[c] = peer_addr[c];
2891 *(uint64_t *)(val_eth + portid) = dest_eth_addr[portid];
2894 #define CMD_LINE_OPT_RX_CONFIG "rx"
2895 #define CMD_LINE_OPT_TX_CONFIG "tx"
2896 #define CMD_LINE_OPT_STAT_LCORE "stat-lcore"
2897 #define CMD_LINE_OPT_ETH_DEST "eth-dest"
2898 #define CMD_LINE_OPT_NO_NUMA "no-numa"
2899 #define CMD_LINE_OPT_IPV6 "ipv6"
2900 #define CMD_LINE_OPT_ENABLE_JUMBO "enable-jumbo"
2901 #define CMD_LINE_OPT_HASH_ENTRY_NUM "hash-entry-num"
2902 #define CMD_LINE_OPT_NO_LTHREADS "no-lthreads"
2903 #define CMD_LINE_OPT_PARSE_PTYPE "parse-ptype"
2905 /* Parse the argument given in the command line of the application */
2907 parse_args(int argc, char **argv)
2912 char *prgname = argv[0];
2913 static struct option lgopts[] = {
2914 {CMD_LINE_OPT_RX_CONFIG, 1, 0, 0},
2915 {CMD_LINE_OPT_TX_CONFIG, 1, 0, 0},
2916 {CMD_LINE_OPT_STAT_LCORE, 1, 0, 0},
2917 {CMD_LINE_OPT_ETH_DEST, 1, 0, 0},
2918 {CMD_LINE_OPT_NO_NUMA, 0, 0, 0},
2919 {CMD_LINE_OPT_IPV6, 0, 0, 0},
2920 {CMD_LINE_OPT_ENABLE_JUMBO, 0, 0, 0},
2921 {CMD_LINE_OPT_HASH_ENTRY_NUM, 1, 0, 0},
2922 {CMD_LINE_OPT_NO_LTHREADS, 0, 0, 0},
2923 {CMD_LINE_OPT_PARSE_PTYPE, 0, 0, 0},
2929 while ((opt = getopt_long(argc, argvopt, "p:P",
2930 lgopts, &option_index)) != EOF) {
2935 enabled_port_mask = parse_portmask(optarg);
2936 if (enabled_port_mask == 0) {
2937 printf("invalid portmask\n");
2938 print_usage(prgname);
2943 printf("Promiscuous mode selected\n");
2949 if (!strncmp(lgopts[option_index].name, CMD_LINE_OPT_RX_CONFIG,
2950 sizeof(CMD_LINE_OPT_RX_CONFIG))) {
2951 ret = parse_rx_config(optarg);
2953 printf("invalid rx-config\n");
2954 print_usage(prgname);
2959 if (!strncmp(lgopts[option_index].name, CMD_LINE_OPT_TX_CONFIG,
2960 sizeof(CMD_LINE_OPT_TX_CONFIG))) {
2961 ret = parse_tx_config(optarg);
2963 printf("invalid tx-config\n");
2964 print_usage(prgname);
2969 #if (APP_CPU_LOAD > 0)
2970 if (!strncmp(lgopts[option_index].name, CMD_LINE_OPT_STAT_LCORE,
2971 sizeof(CMD_LINE_OPT_STAT_LCORE))) {
2972 cpu_load_lcore_id = parse_stat_lcore(optarg);
2976 if (!strncmp(lgopts[option_index].name, CMD_LINE_OPT_ETH_DEST,
2977 sizeof(CMD_LINE_OPT_ETH_DEST)))
2978 parse_eth_dest(optarg);
2980 if (!strncmp(lgopts[option_index].name, CMD_LINE_OPT_NO_NUMA,
2981 sizeof(CMD_LINE_OPT_NO_NUMA))) {
2982 printf("numa is disabled\n");
2986 #if (APP_LOOKUP_METHOD == APP_LOOKUP_EXACT_MATCH)
2987 if (!strncmp(lgopts[option_index].name, CMD_LINE_OPT_IPV6,
2988 sizeof(CMD_LINE_OPT_IPV6))) {
2989 printf("ipv6 is specified\n");
2994 if (!strncmp(lgopts[option_index].name, CMD_LINE_OPT_NO_LTHREADS,
2995 sizeof(CMD_LINE_OPT_NO_LTHREADS))) {
2996 printf("l-threads model is disabled\n");
3000 if (!strncmp(lgopts[option_index].name, CMD_LINE_OPT_PARSE_PTYPE,
3001 sizeof(CMD_LINE_OPT_PARSE_PTYPE))) {
3002 printf("software packet type parsing enabled\n");
3006 if (!strncmp(lgopts[option_index].name, CMD_LINE_OPT_ENABLE_JUMBO,
3007 sizeof(CMD_LINE_OPT_ENABLE_JUMBO))) {
3008 struct option lenopts = {"max-pkt-len", required_argument, 0,
3011 printf("jumbo frame is enabled - disabling simple TX path\n");
3012 port_conf.rxmode.jumbo_frame = 1;
3014 /* if no max-pkt-len set, use the default value ETHER_MAX_LEN */
3015 if (0 == getopt_long(argc, argvopt, "", &lenopts,
3018 ret = parse_max_pkt_len(optarg);
3019 if ((ret < 64) || (ret > MAX_JUMBO_PKT_LEN)) {
3020 printf("invalid packet length\n");
3021 print_usage(prgname);
3024 port_conf.rxmode.max_rx_pkt_len = ret;
3026 printf("set jumbo frame max packet length to %u\n",
3027 (unsigned int)port_conf.rxmode.max_rx_pkt_len);
3029 #if (APP_LOOKUP_METHOD == APP_LOOKUP_EXACT_MATCH)
3030 if (!strncmp(lgopts[option_index].name, CMD_LINE_OPT_HASH_ENTRY_NUM,
3031 sizeof(CMD_LINE_OPT_HASH_ENTRY_NUM))) {
3032 ret = parse_hash_entry_number(optarg);
3033 if ((ret > 0) && (ret <= L3FWD_HASH_ENTRIES)) {
3034 hash_entry_number = ret;
3036 printf("invalid hash entry number\n");
3037 print_usage(prgname);
3045 print_usage(prgname);
3051 argv[optind-1] = prgname;
3054 optind = 1; /* reset getopt lib */
3059 print_ethaddr(const char *name, const struct ether_addr *eth_addr)
3061 char buf[ETHER_ADDR_FMT_SIZE];
3063 ether_format_addr(buf, ETHER_ADDR_FMT_SIZE, eth_addr);
3064 printf("%s%s", name, buf);
3067 #if (APP_LOOKUP_METHOD == APP_LOOKUP_EXACT_MATCH)
3069 static void convert_ipv4_5tuple(struct ipv4_5tuple *key1,
3070 union ipv4_5tuple_host *key2)
3072 key2->ip_dst = rte_cpu_to_be_32(key1->ip_dst);
3073 key2->ip_src = rte_cpu_to_be_32(key1->ip_src);
3074 key2->port_dst = rte_cpu_to_be_16(key1->port_dst);
3075 key2->port_src = rte_cpu_to_be_16(key1->port_src);
3076 key2->proto = key1->proto;
3081 static void convert_ipv6_5tuple(struct ipv6_5tuple *key1,
3082 union ipv6_5tuple_host *key2)
3086 for (i = 0; i < 16; i++) {
3087 key2->ip_dst[i] = key1->ip_dst[i];
3088 key2->ip_src[i] = key1->ip_src[i];
3090 key2->port_dst = rte_cpu_to_be_16(key1->port_dst);
3091 key2->port_src = rte_cpu_to_be_16(key1->port_src);
3092 key2->proto = key1->proto;
3098 #define BYTE_VALUE_MAX 256
3099 #define ALL_32_BITS 0xffffffff
3100 #define BIT_8_TO_15 0x0000ff00
3102 populate_ipv4_few_flow_into_table(const struct rte_hash *h)
3106 uint32_t array_len = RTE_DIM(ipv4_l3fwd_route_array);
3108 mask0 = _mm_set_epi32(ALL_32_BITS, ALL_32_BITS, ALL_32_BITS, BIT_8_TO_15);
3109 for (i = 0; i < array_len; i++) {
3110 struct ipv4_l3fwd_route entry;
3111 union ipv4_5tuple_host newkey;
3113 entry = ipv4_l3fwd_route_array[i];
3114 convert_ipv4_5tuple(&entry.key, &newkey);
3115 ret = rte_hash_add_key(h, (void *)&newkey);
3117 rte_exit(EXIT_FAILURE, "Unable to add entry %" PRIu32
3118 " to the l3fwd hash.\n", i);
3120 ipv4_l3fwd_out_if[ret] = entry.if_out;
3122 printf("Hash: Adding 0x%" PRIx32 " keys\n", array_len);
3125 #define BIT_16_TO_23 0x00ff0000
3127 populate_ipv6_few_flow_into_table(const struct rte_hash *h)
3131 uint32_t array_len = RTE_DIM(ipv6_l3fwd_route_array);
3133 mask1 = _mm_set_epi32(ALL_32_BITS, ALL_32_BITS, ALL_32_BITS, BIT_16_TO_23);
3134 mask2 = _mm_set_epi32(0, 0, ALL_32_BITS, ALL_32_BITS);
3135 for (i = 0; i < array_len; i++) {
3136 struct ipv6_l3fwd_route entry;
3137 union ipv6_5tuple_host newkey;
3139 entry = ipv6_l3fwd_route_array[i];
3140 convert_ipv6_5tuple(&entry.key, &newkey);
3141 ret = rte_hash_add_key(h, (void *)&newkey);
3143 rte_exit(EXIT_FAILURE, "Unable to add entry %" PRIu32
3144 " to the l3fwd hash.\n", i);
3146 ipv6_l3fwd_out_if[ret] = entry.if_out;
3148 printf("Hash: Adding 0x%" PRIx32 "keys\n", array_len);
3151 #define NUMBER_PORT_USED 4
3153 populate_ipv4_many_flow_into_table(const struct rte_hash *h,
3154 unsigned int nr_flow)
3158 mask0 = _mm_set_epi32(ALL_32_BITS, ALL_32_BITS, ALL_32_BITS, BIT_8_TO_15);
3160 for (i = 0; i < nr_flow; i++) {
3161 struct ipv4_l3fwd_route entry;
3162 union ipv4_5tuple_host newkey;
3163 uint8_t a = (uint8_t)((i / NUMBER_PORT_USED) % BYTE_VALUE_MAX);
3164 uint8_t b = (uint8_t)(((i / NUMBER_PORT_USED) / BYTE_VALUE_MAX) %
3166 uint8_t c = (uint8_t)((i / NUMBER_PORT_USED) / (BYTE_VALUE_MAX *
3168 /* Create the ipv4 exact match flow */
3169 memset(&entry, 0, sizeof(entry));
3170 switch (i & (NUMBER_PORT_USED - 1)) {
3172 entry = ipv4_l3fwd_route_array[0];
3173 entry.key.ip_dst = IPv4(101, c, b, a);
3176 entry = ipv4_l3fwd_route_array[1];
3177 entry.key.ip_dst = IPv4(201, c, b, a);
3180 entry = ipv4_l3fwd_route_array[2];
3181 entry.key.ip_dst = IPv4(111, c, b, a);
3184 entry = ipv4_l3fwd_route_array[3];
3185 entry.key.ip_dst = IPv4(211, c, b, a);
3188 convert_ipv4_5tuple(&entry.key, &newkey);
3189 int32_t ret = rte_hash_add_key(h, (void *)&newkey);
3192 rte_exit(EXIT_FAILURE, "Unable to add entry %u\n", i);
3194 ipv4_l3fwd_out_if[ret] = (uint8_t)entry.if_out;
3197 printf("Hash: Adding 0x%x keys\n", nr_flow);
3201 populate_ipv6_many_flow_into_table(const struct rte_hash *h,
3202 unsigned int nr_flow)
3206 mask1 = _mm_set_epi32(ALL_32_BITS, ALL_32_BITS, ALL_32_BITS, BIT_16_TO_23);
3207 mask2 = _mm_set_epi32(0, 0, ALL_32_BITS, ALL_32_BITS);
3208 for (i = 0; i < nr_flow; i++) {
3209 struct ipv6_l3fwd_route entry;
3210 union ipv6_5tuple_host newkey;
3212 uint8_t a = (uint8_t) ((i / NUMBER_PORT_USED) % BYTE_VALUE_MAX);
3213 uint8_t b = (uint8_t) (((i / NUMBER_PORT_USED) / BYTE_VALUE_MAX) %
3215 uint8_t c = (uint8_t) ((i / NUMBER_PORT_USED) / (BYTE_VALUE_MAX *
3218 /* Create the ipv6 exact match flow */
3219 memset(&entry, 0, sizeof(entry));
3220 switch (i & (NUMBER_PORT_USED - 1)) {
3222 entry = ipv6_l3fwd_route_array[0];
3225 entry = ipv6_l3fwd_route_array[1];
3228 entry = ipv6_l3fwd_route_array[2];
3231 entry = ipv6_l3fwd_route_array[3];
3234 entry.key.ip_dst[13] = c;
3235 entry.key.ip_dst[14] = b;
3236 entry.key.ip_dst[15] = a;
3237 convert_ipv6_5tuple(&entry.key, &newkey);
3238 int32_t ret = rte_hash_add_key(h, (void *)&newkey);
3241 rte_exit(EXIT_FAILURE, "Unable to add entry %u\n", i);
3243 ipv6_l3fwd_out_if[ret] = (uint8_t) entry.if_out;
3246 printf("Hash: Adding 0x%x keys\n", nr_flow);
3250 setup_hash(int socketid)
3252 struct rte_hash_parameters ipv4_l3fwd_hash_params = {
3254 .entries = L3FWD_HASH_ENTRIES,
3255 .key_len = sizeof(union ipv4_5tuple_host),
3256 .hash_func = ipv4_hash_crc,
3257 .hash_func_init_val = 0,
3260 struct rte_hash_parameters ipv6_l3fwd_hash_params = {
3262 .entries = L3FWD_HASH_ENTRIES,
3263 .key_len = sizeof(union ipv6_5tuple_host),
3264 .hash_func = ipv6_hash_crc,
3265 .hash_func_init_val = 0,
3270 /* create ipv4 hash */
3271 snprintf(s, sizeof(s), "ipv4_l3fwd_hash_%d", socketid);
3272 ipv4_l3fwd_hash_params.name = s;
3273 ipv4_l3fwd_hash_params.socket_id = socketid;
3274 ipv4_l3fwd_lookup_struct[socketid] =
3275 rte_hash_create(&ipv4_l3fwd_hash_params);
3276 if (ipv4_l3fwd_lookup_struct[socketid] == NULL)
3277 rte_exit(EXIT_FAILURE, "Unable to create the l3fwd hash on "
3278 "socket %d\n", socketid);
3280 /* create ipv6 hash */
3281 snprintf(s, sizeof(s), "ipv6_l3fwd_hash_%d", socketid);
3282 ipv6_l3fwd_hash_params.name = s;
3283 ipv6_l3fwd_hash_params.socket_id = socketid;
3284 ipv6_l3fwd_lookup_struct[socketid] =
3285 rte_hash_create(&ipv6_l3fwd_hash_params);
3286 if (ipv6_l3fwd_lookup_struct[socketid] == NULL)
3287 rte_exit(EXIT_FAILURE, "Unable to create the l3fwd hash on "
3288 "socket %d\n", socketid);
3290 if (hash_entry_number != HASH_ENTRY_NUMBER_DEFAULT) {
3291 /* For testing hash matching with a large number of flows we
3292 * generate millions of IP 5-tuples with an incremented dst
3293 * address to initialize the hash table. */
3295 /* populate the ipv4 hash */
3296 populate_ipv4_many_flow_into_table(
3297 ipv4_l3fwd_lookup_struct[socketid], hash_entry_number);
3299 /* populate the ipv6 hash */
3300 populate_ipv6_many_flow_into_table(
3301 ipv6_l3fwd_lookup_struct[socketid], hash_entry_number);
3304 /* Use data in ipv4/ipv6 l3fwd lookup table directly to initialize
3307 /* populate the ipv4 hash */
3308 populate_ipv4_few_flow_into_table(
3309 ipv4_l3fwd_lookup_struct[socketid]);
3311 /* populate the ipv6 hash */
3312 populate_ipv6_few_flow_into_table(
3313 ipv6_l3fwd_lookup_struct[socketid]);
3319 #if (APP_LOOKUP_METHOD == APP_LOOKUP_LPM)
3321 setup_lpm(int socketid)
3323 struct rte_lpm6_config config;
3324 struct rte_lpm_config lpm_ipv4_config;
3329 /* create the LPM table */
3330 snprintf(s, sizeof(s), "IPV4_L3FWD_LPM_%d", socketid);
3331 lpm_ipv4_config.max_rules = IPV4_L3FWD_LPM_MAX_RULES;
3332 lpm_ipv4_config.number_tbl8s = 256;
3333 lpm_ipv4_config.flags = 0;
3334 ipv4_l3fwd_lookup_struct[socketid] =
3335 rte_lpm_create(s, socketid, &lpm_ipv4_config);
3336 if (ipv4_l3fwd_lookup_struct[socketid] == NULL)
3337 rte_exit(EXIT_FAILURE, "Unable to create the l3fwd LPM table"
3338 " on socket %d\n", socketid);
3340 /* populate the LPM table */
3341 for (i = 0; i < IPV4_L3FWD_NUM_ROUTES; i++) {
3343 /* skip unused ports */
3344 if ((1 << ipv4_l3fwd_route_array[i].if_out &
3345 enabled_port_mask) == 0)
3348 ret = rte_lpm_add(ipv4_l3fwd_lookup_struct[socketid],
3349 ipv4_l3fwd_route_array[i].ip,
3350 ipv4_l3fwd_route_array[i].depth,
3351 ipv4_l3fwd_route_array[i].if_out);
3354 rte_exit(EXIT_FAILURE, "Unable to add entry %u to the "
3355 "l3fwd LPM table on socket %d\n",
3359 printf("LPM: Adding route 0x%08x / %d (%d)\n",
3360 (unsigned)ipv4_l3fwd_route_array[i].ip,
3361 ipv4_l3fwd_route_array[i].depth,
3362 ipv4_l3fwd_route_array[i].if_out);
3365 /* create the LPM6 table */
3366 snprintf(s, sizeof(s), "IPV6_L3FWD_LPM_%d", socketid);
3368 config.max_rules = IPV6_L3FWD_LPM_MAX_RULES;
3369 config.number_tbl8s = IPV6_L3FWD_LPM_NUMBER_TBL8S;
3371 ipv6_l3fwd_lookup_struct[socketid] = rte_lpm6_create(s, socketid,
3373 if (ipv6_l3fwd_lookup_struct[socketid] == NULL)
3374 rte_exit(EXIT_FAILURE, "Unable to create the l3fwd LPM table"
3375 " on socket %d\n", socketid);
3377 /* populate the LPM table */
3378 for (i = 0; i < IPV6_L3FWD_NUM_ROUTES; i++) {
3380 /* skip unused ports */
3381 if ((1 << ipv6_l3fwd_route_array[i].if_out &
3382 enabled_port_mask) == 0)
3385 ret = rte_lpm6_add(ipv6_l3fwd_lookup_struct[socketid],
3386 ipv6_l3fwd_route_array[i].ip,
3387 ipv6_l3fwd_route_array[i].depth,
3388 ipv6_l3fwd_route_array[i].if_out);
3391 rte_exit(EXIT_FAILURE, "Unable to add entry %u to the "
3392 "l3fwd LPM table on socket %d\n",
3396 printf("LPM: Adding route %s / %d (%d)\n",
3398 ipv6_l3fwd_route_array[i].depth,
3399 ipv6_l3fwd_route_array[i].if_out);
3405 init_mem(unsigned nb_mbuf)
3407 struct lcore_conf *qconf;
3412 for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
3413 if (rte_lcore_is_enabled(lcore_id) == 0)
3417 socketid = rte_lcore_to_socket_id(lcore_id);
3421 if (socketid >= NB_SOCKETS) {
3422 rte_exit(EXIT_FAILURE, "Socket %d of lcore %u is out of range %d\n",
3423 socketid, lcore_id, NB_SOCKETS);
3425 if (pktmbuf_pool[socketid] == NULL) {
3426 snprintf(s, sizeof(s), "mbuf_pool_%d", socketid);
3427 pktmbuf_pool[socketid] =
3428 rte_pktmbuf_pool_create(s, nb_mbuf,
3429 MEMPOOL_CACHE_SIZE, 0,
3430 RTE_MBUF_DEFAULT_BUF_SIZE, socketid);
3431 if (pktmbuf_pool[socketid] == NULL)
3432 rte_exit(EXIT_FAILURE,
3433 "Cannot init mbuf pool on socket %d\n", socketid);
3435 printf("Allocated mbuf pool on socket %d\n", socketid);
3437 #if (APP_LOOKUP_METHOD == APP_LOOKUP_LPM)
3438 setup_lpm(socketid);
3440 setup_hash(socketid);
3443 qconf = &lcore_conf[lcore_id];
3444 qconf->ipv4_lookup_struct = ipv4_l3fwd_lookup_struct[socketid];
3445 qconf->ipv6_lookup_struct = ipv6_l3fwd_lookup_struct[socketid];
3450 /* Check the link status of all ports in up to 9s, and print them finally */
3452 check_all_ports_link_status(uint8_t port_num, uint32_t port_mask)
3454 #define CHECK_INTERVAL 100 /* 100ms */
3455 #define MAX_CHECK_TIME 90 /* 9s (90 * 100ms) in total */
3456 uint8_t portid, count, all_ports_up, print_flag = 0;
3457 struct rte_eth_link link;
3459 printf("\nChecking link status");
3461 for (count = 0; count <= MAX_CHECK_TIME; count++) {
3463 for (portid = 0; portid < port_num; portid++) {
3464 if ((port_mask & (1 << portid)) == 0)
3466 memset(&link, 0, sizeof(link));
3467 rte_eth_link_get_nowait(portid, &link);
3468 /* print link status if flag set */
3469 if (print_flag == 1) {
3470 if (link.link_status)
3471 printf("Port %d Link Up - speed %u "
3472 "Mbps - %s\n", (uint8_t)portid,
3473 (unsigned)link.link_speed,
3474 (link.link_duplex == ETH_LINK_FULL_DUPLEX) ?
3475 ("full-duplex") : ("half-duplex\n"));
3477 printf("Port %d Link Down\n",
3481 /* clear all_ports_up flag if any link down */
3482 if (link.link_status == ETH_LINK_DOWN) {
3487 /* after finally printing all link status, get out */
3488 if (print_flag == 1)
3491 if (all_ports_up == 0) {
3494 rte_delay_ms(CHECK_INTERVAL);
3497 /* set the print_flag if all ports up or timeout */
3498 if (all_ports_up == 1 || count == (MAX_CHECK_TIME - 1)) {
3506 main(int argc, char **argv)
3508 struct rte_eth_dev_info dev_info;
3509 struct rte_eth_txconf *txconf;
3515 uint32_t n_tx_queue, nb_lcores;
3516 uint8_t portid, nb_rx_queue, queue, socketid;
3519 ret = rte_eal_init(argc, argv);
3521 rte_exit(EXIT_FAILURE, "Invalid EAL parameters\n");
3525 /* pre-init dst MACs for all ports to 02:00:00:00:00:xx */
3526 for (portid = 0; portid < RTE_MAX_ETHPORTS; portid++) {
3527 dest_eth_addr[portid] = ETHER_LOCAL_ADMIN_ADDR +
3528 ((uint64_t)portid << 40);
3529 *(uint64_t *)(val_eth + portid) = dest_eth_addr[portid];
3532 /* parse application arguments (after the EAL ones) */
3533 ret = parse_args(argc, argv);
3535 rte_exit(EXIT_FAILURE, "Invalid L3FWD parameters\n");
3537 if (check_lcore_params() < 0)
3538 rte_exit(EXIT_FAILURE, "check_lcore_params failed\n");
3540 printf("Initializing rx-queues...\n");
3541 ret = init_rx_queues();
3543 rte_exit(EXIT_FAILURE, "init_rx_queues failed\n");
3545 printf("Initializing tx-threads...\n");
3546 ret = init_tx_threads();
3548 rte_exit(EXIT_FAILURE, "init_tx_threads failed\n");
3550 printf("Initializing rings...\n");
3551 ret = init_rx_rings();
3553 rte_exit(EXIT_FAILURE, "init_rx_rings failed\n");
3555 nb_ports = rte_eth_dev_count();
3557 if (check_port_config(nb_ports) < 0)
3558 rte_exit(EXIT_FAILURE, "check_port_config failed\n");
3560 nb_lcores = rte_lcore_count();
3562 /* initialize all ports */
3563 for (portid = 0; portid < nb_ports; portid++) {
3564 /* skip ports that are not enabled */
3565 if ((enabled_port_mask & (1 << portid)) == 0) {
3566 printf("\nSkipping disabled port %d\n", portid);
3571 printf("Initializing port %d ... ", portid);
3574 nb_rx_queue = get_port_n_rx_queues(portid);
3575 n_tx_queue = nb_lcores;
3576 if (n_tx_queue > MAX_TX_QUEUE_PER_PORT)
3577 n_tx_queue = MAX_TX_QUEUE_PER_PORT;
3578 printf("Creating queues: nb_rxq=%d nb_txq=%u... ",
3579 nb_rx_queue, (unsigned)n_tx_queue);
3580 ret = rte_eth_dev_configure(portid, nb_rx_queue,
3581 (uint16_t)n_tx_queue, &port_conf);
3583 rte_exit(EXIT_FAILURE, "Cannot configure device: err=%d, port=%d\n",
3586 rte_eth_macaddr_get(portid, &ports_eth_addr[portid]);
3587 print_ethaddr(" Address:", &ports_eth_addr[portid]);
3589 print_ethaddr("Destination:",
3590 (const struct ether_addr *)&dest_eth_addr[portid]);
3594 * prepare src MACs for each port.
3596 ether_addr_copy(&ports_eth_addr[portid],
3597 (struct ether_addr *)(val_eth + portid) + 1);
3600 ret = init_mem(NB_MBUF);
3602 rte_exit(EXIT_FAILURE, "init_mem failed\n");
3604 /* init one TX queue per couple (lcore,port) */
3606 for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
3607 if (rte_lcore_is_enabled(lcore_id) == 0)
3611 socketid = (uint8_t)rte_lcore_to_socket_id(lcore_id);
3615 printf("txq=%u,%d,%d ", lcore_id, queueid, socketid);
3618 rte_eth_dev_info_get(portid, &dev_info);
3619 txconf = &dev_info.default_txconf;
3620 if (port_conf.rxmode.jumbo_frame)
3621 txconf->txq_flags = 0;
3622 ret = rte_eth_tx_queue_setup(portid, queueid, nb_txd,
3625 rte_exit(EXIT_FAILURE, "rte_eth_tx_queue_setup: err=%d, "
3626 "port=%d\n", ret, portid);
3628 tx_thread[lcore_id].tx_queue_id[portid] = queueid;
3634 for (i = 0; i < n_rx_thread; i++) {
3635 lcore_id = rx_thread[i].conf.lcore_id;
3637 if (rte_lcore_is_enabled(lcore_id) == 0) {
3638 rte_exit(EXIT_FAILURE,
3639 "Cannot start Rx thread on lcore %u: lcore disabled\n",
3644 printf("\nInitializing rx queues for Rx thread %d on lcore %u ... ",
3648 /* init RX queues */
3649 for (queue = 0; queue < rx_thread[i].n_rx_queue; ++queue) {
3650 portid = rx_thread[i].rx_queue_list[queue].port_id;
3651 queueid = rx_thread[i].rx_queue_list[queue].queue_id;
3654 socketid = (uint8_t)rte_lcore_to_socket_id(lcore_id);
3658 printf("rxq=%d,%d,%d ", portid, queueid, socketid);
3661 ret = rte_eth_rx_queue_setup(portid, queueid, nb_rxd,
3664 pktmbuf_pool[socketid]);
3666 rte_exit(EXIT_FAILURE, "rte_eth_rx_queue_setup: err=%d, "
3667 "port=%d\n", ret, portid);
3674 for (portid = 0; portid < nb_ports; portid++) {
3675 if ((enabled_port_mask & (1 << portid)) == 0)
3679 ret = rte_eth_dev_start(portid);
3681 rte_exit(EXIT_FAILURE, "rte_eth_dev_start: err=%d, port=%d\n",
3685 * If enabled, put device in promiscuous mode.
3686 * This allows IO forwarding mode to forward packets
3687 * to itself through 2 cross-connected ports of the
3691 rte_eth_promiscuous_enable(portid);
3694 for (i = 0; i < n_rx_thread; i++) {
3695 lcore_id = rx_thread[i].conf.lcore_id;
3696 if (rte_lcore_is_enabled(lcore_id) == 0)
3699 /* check if hw packet type is supported */
3700 for (queue = 0; queue < rx_thread[i].n_rx_queue; ++queue) {
3701 portid = rx_thread[i].rx_queue_list[queue].port_id;
3702 queueid = rx_thread[i].rx_queue_list[queue].queue_id;
3704 if (parse_ptype_on) {
3705 if (!rte_eth_add_rx_callback(portid, queueid,
3706 cb_parse_ptype, NULL))
3707 rte_exit(EXIT_FAILURE,
3708 "Failed to add rx callback: "
3709 "port=%d\n", portid);
3710 } else if (!check_ptype(portid))
3711 rte_exit(EXIT_FAILURE,
3712 "Port %d cannot parse packet type.\n\n"
3713 "Please add --parse-ptype to use sw "
3714 "packet type analyzer.\n\n",
3719 check_all_ports_link_status((uint8_t)nb_ports, enabled_port_mask);
3722 printf("Starting L-Threading Model\n");
3724 #if (APP_CPU_LOAD > 0)
3725 if (cpu_load_lcore_id > 0)
3726 /* Use one lcore for cpu load collector */
3730 lthread_num_schedulers_set(nb_lcores);
3731 rte_eal_mp_remote_launch(sched_spawner, NULL, SKIP_MASTER);
3732 lthread_master_spawner(NULL);
3735 printf("Starting P-Threading Model\n");
3736 /* launch per-lcore init on every lcore */
3737 rte_eal_mp_remote_launch(pthread_run, NULL, CALL_MASTER);
3738 RTE_LCORE_FOREACH_SLAVE(lcore_id) {
3739 if (rte_eal_wait_lcore(lcore_id) < 0)