4 * Copyright(c) 2010-2015 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 * When set to zero, simple forwaring path is eanbled.
95 * When set to one, optimized forwarding path is enabled.
96 * Note that LPM optimisation path uses SSE4.1 instructions.
98 #if ((APP_LOOKUP_METHOD == APP_LOOKUP_LPM) && !defined(__SSE4_1__))
99 #define ENABLE_MULTI_BUFFER_OPTIMIZE 0
101 #define ENABLE_MULTI_BUFFER_OPTIMIZE 1
104 #if (APP_LOOKUP_METHOD == APP_LOOKUP_EXACT_MATCH)
105 #include <rte_hash.h>
106 #elif (APP_LOOKUP_METHOD == APP_LOOKUP_LPM)
108 #include <rte_lpm6.h>
110 #error "APP_LOOKUP_METHOD set to incorrect value"
113 #define RTE_LOGTYPE_L3FWD RTE_LOGTYPE_USER1
115 #define MAX_JUMBO_PKT_LEN 9600
117 #define IPV6_ADDR_LEN 16
119 #define MEMPOOL_CACHE_SIZE 256
122 * This expression is used to calculate the number of mbufs needed depending on
123 * user input, taking into account memory for rx and tx hardware rings, cache
124 * per lcore and mtable per port per lcore. RTE_MAX is used to ensure that
125 * NB_MBUF never goes below a minimum value of 8192
128 #define NB_MBUF RTE_MAX(\
129 (nb_ports*nb_rx_queue*RTE_TEST_RX_DESC_DEFAULT + \
130 nb_ports*nb_lcores*MAX_PKT_BURST + \
131 nb_ports*n_tx_queue*RTE_TEST_TX_DESC_DEFAULT + \
132 nb_lcores*MEMPOOL_CACHE_SIZE), \
135 #define MAX_PKT_BURST 32
136 #define BURST_TX_DRAIN_US 100 /* TX drain every ~100us */
139 * Try to avoid TX buffering if we have at least MAX_TX_BURST packets to send.
141 #define MAX_TX_BURST (MAX_PKT_BURST / 2)
142 #define BURST_SIZE MAX_TX_BURST
146 /* Configure how many packets ahead to prefetch, when reading packets */
147 #define PREFETCH_OFFSET 3
149 /* Used to mark destination port as 'invalid'. */
150 #define BAD_PORT ((uint16_t)-1)
155 * Configurable number of RX/TX ring descriptors
157 #define RTE_TEST_RX_DESC_DEFAULT 128
158 #define RTE_TEST_TX_DESC_DEFAULT 128
159 static uint16_t nb_rxd = RTE_TEST_RX_DESC_DEFAULT;
160 static uint16_t nb_txd = RTE_TEST_TX_DESC_DEFAULT;
162 /* ethernet addresses of ports */
163 static uint64_t dest_eth_addr[RTE_MAX_ETHPORTS];
164 static struct ether_addr ports_eth_addr[RTE_MAX_ETHPORTS];
166 static __m128i val_eth[RTE_MAX_ETHPORTS];
168 /* replace first 12B of the ethernet header. */
169 #define MASK_ETH 0x3f
171 /* mask of enabled ports */
172 static uint32_t enabled_port_mask;
173 static int promiscuous_on; /**< $et in promiscuous mode off by default. */
174 static int numa_on = 1; /**< NUMA is enabled by default. */
176 #if (APP_LOOKUP_METHOD == APP_LOOKUP_EXACT_MATCH)
177 static int ipv6; /**< ipv6 is false by default. */
180 #if (APP_CPU_LOAD == 1)
182 #define MAX_CPU RTE_MAX_LCORE
183 #define CPU_LOAD_TIMEOUT_US (5 * 1000 * 1000) /**< Timeout for collecting 5s */
185 #define CPU_PROCESS 0
187 #define MAX_CPU_COUNTER 2
192 uint64_t hits[MAX_CPU_COUNTER][MAX_CPU];
193 } __rte_cache_aligned;
195 static struct cpu_load cpu_load;
196 static int cpu_load_lcore_id = -1;
198 #define SET_CPU_BUSY(thread, counter) \
199 thread->conf.busy[counter] = 1
201 #define SET_CPU_IDLE(thread, counter) \
202 thread->conf.busy[counter] = 0
204 #define IS_CPU_BUSY(thread, counter) \
205 (thread->conf.busy[counter] > 0)
209 #define SET_CPU_BUSY(thread, counter)
210 #define SET_CPU_IDLE(thread, counter)
211 #define IS_CPU_BUSY(thread, counter) 0
217 struct rte_mbuf *m_table[MAX_PKT_BURST];
220 struct lcore_rx_queue {
223 } __rte_cache_aligned;
225 #define MAX_RX_QUEUE_PER_LCORE 16
226 #define MAX_TX_QUEUE_PER_PORT RTE_MAX_ETHPORTS
227 #define MAX_RX_QUEUE_PER_PORT 128
229 #define MAX_LCORE_PARAMS 1024
230 struct rx_thread_params {
235 } __rte_cache_aligned;
237 static struct rx_thread_params rx_thread_params_array[MAX_LCORE_PARAMS];
238 static struct rx_thread_params rx_thread_params_array_default[] = {
250 static struct rx_thread_params *rx_thread_params =
251 rx_thread_params_array_default;
252 static uint16_t nb_rx_thread_params = RTE_DIM(rx_thread_params_array_default);
254 struct tx_thread_params {
257 } __rte_cache_aligned;
259 static struct tx_thread_params tx_thread_params_array[MAX_LCORE_PARAMS];
260 static struct tx_thread_params tx_thread_params_array_default[] = {
272 static struct tx_thread_params *tx_thread_params =
273 tx_thread_params_array_default;
274 static uint16_t nb_tx_thread_params = RTE_DIM(tx_thread_params_array_default);
276 static struct rte_eth_conf port_conf = {
278 .mq_mode = ETH_MQ_RX_RSS,
279 .max_rx_pkt_len = ETHER_MAX_LEN,
281 .header_split = 0, /**< Header Split disabled */
282 .hw_ip_checksum = 1, /**< IP checksum offload enabled */
283 .hw_vlan_filter = 0, /**< VLAN filtering disabled */
284 .jumbo_frame = 0, /**< Jumbo Frame Support disabled */
285 .hw_strip_crc = 0, /**< CRC stripped by hardware */
290 .rss_hf = ETH_RSS_TCP,
294 .mq_mode = ETH_MQ_TX_NONE,
298 static struct rte_mempool *pktmbuf_pool[NB_SOCKETS];
300 #if (APP_LOOKUP_METHOD == APP_LOOKUP_EXACT_MATCH)
302 #ifdef RTE_MACHINE_CPUFLAG_SSE4_2
303 #include <rte_hash_crc.h>
304 #define DEFAULT_HASH_FUNC rte_hash_crc
306 #include <rte_jhash.h>
307 #define DEFAULT_HASH_FUNC rte_jhash
316 } __attribute__((__packed__));
318 union ipv4_5tuple_host {
331 #define XMM_NUM_IN_IPV6_5TUPLE 3
334 uint8_t ip_dst[IPV6_ADDR_LEN];
335 uint8_t ip_src[IPV6_ADDR_LEN];
339 } __attribute__((__packed__));
341 union ipv6_5tuple_host {
346 uint8_t ip_src[IPV6_ADDR_LEN];
347 uint8_t ip_dst[IPV6_ADDR_LEN];
352 __m128i xmm[XMM_NUM_IN_IPV6_5TUPLE];
355 struct ipv4_l3fwd_route {
356 struct ipv4_5tuple key;
360 struct ipv6_l3fwd_route {
361 struct ipv6_5tuple key;
365 static struct ipv4_l3fwd_route ipv4_l3fwd_route_array[] = {
366 {{IPv4(101, 0, 0, 0), IPv4(100, 10, 0, 1), 101, 11, IPPROTO_TCP}, 0},
367 {{IPv4(201, 0, 0, 0), IPv4(200, 20, 0, 1), 102, 12, IPPROTO_TCP}, 1},
368 {{IPv4(111, 0, 0, 0), IPv4(100, 30, 0, 1), 101, 11, IPPROTO_TCP}, 2},
369 {{IPv4(211, 0, 0, 0), IPv4(200, 40, 0, 1), 102, 12, IPPROTO_TCP}, 3},
372 static struct ipv6_l3fwd_route ipv6_l3fwd_route_array[] = {
374 {0xfe, 0x80, 0, 0, 0, 0, 0, 0, 0x02, 0x1e, 0x67, 0xff, 0xfe, 0, 0, 0},
375 {0xfe, 0x80, 0, 0, 0, 0, 0, 0, 0x02, 0x1b, 0x21, 0xff, 0xfe, 0x91, 0x38,
377 101, 11, IPPROTO_TCP}, 0},
380 {0xfe, 0x90, 0, 0, 0, 0, 0, 0, 0x02, 0x1e, 0x67, 0xff, 0xfe, 0, 0, 0},
381 {0xfe, 0x90, 0, 0, 0, 0, 0, 0, 0x02, 0x1b, 0x21, 0xff, 0xfe, 0x91, 0x38,
383 102, 12, IPPROTO_TCP}, 1},
386 {0xfe, 0xa0, 0, 0, 0, 0, 0, 0, 0x02, 0x1e, 0x67, 0xff, 0xfe, 0, 0, 0},
387 {0xfe, 0xa0, 0, 0, 0, 0, 0, 0, 0x02, 0x1b, 0x21, 0xff, 0xfe, 0x91, 0x38,
389 101, 11, IPPROTO_TCP}, 2},
392 {0xfe, 0xb0, 0, 0, 0, 0, 0, 0, 0x02, 0x1e, 0x67, 0xff, 0xfe, 0, 0, 0},
393 {0xfe, 0xb0, 0, 0, 0, 0, 0, 0, 0x02, 0x1b, 0x21, 0xff, 0xfe, 0x91, 0x38,
395 102, 12, IPPROTO_TCP}, 3},
398 typedef struct rte_hash lookup_struct_t;
399 static lookup_struct_t *ipv4_l3fwd_lookup_struct[NB_SOCKETS];
400 static lookup_struct_t *ipv6_l3fwd_lookup_struct[NB_SOCKETS];
402 #ifdef RTE_ARCH_X86_64
403 /* default to 4 million hash entries (approx) */
404 #define L3FWD_HASH_ENTRIES (1024*1024*4)
406 /* 32-bit has less address-space for hugepage memory, limit to 1M entries */
407 #define L3FWD_HASH_ENTRIES (1024*1024*1)
409 #define HASH_ENTRY_NUMBER_DEFAULT 4
411 static uint32_t hash_entry_number = HASH_ENTRY_NUMBER_DEFAULT;
413 static inline uint32_t
414 ipv4_hash_crc(const void *data, __rte_unused uint32_t data_len,
417 const union ipv4_5tuple_host *k;
423 p = (const uint32_t *)&k->port_src;
425 #ifdef RTE_MACHINE_CPUFLAG_SSE4_2
426 init_val = rte_hash_crc_4byte(t, init_val);
427 init_val = rte_hash_crc_4byte(k->ip_src, init_val);
428 init_val = rte_hash_crc_4byte(k->ip_dst, init_val);
429 init_val = rte_hash_crc_4byte(*p, init_val);
430 #else /* RTE_MACHINE_CPUFLAG_SSE4_2 */
431 init_val = rte_jhash_1word(t, init_val);
432 init_val = rte_jhash_1word(k->ip_src, init_val);
433 init_val = rte_jhash_1word(k->ip_dst, init_val);
434 init_val = rte_jhash_1word(*p, init_val);
435 #endif /* RTE_MACHINE_CPUFLAG_SSE4_2 */
439 static inline uint32_t
440 ipv6_hash_crc(const void *data, __rte_unused uint32_t data_len,
443 const union ipv6_5tuple_host *k;
446 #ifdef RTE_MACHINE_CPUFLAG_SSE4_2
447 const uint32_t *ip_src0, *ip_src1, *ip_src2, *ip_src3;
448 const uint32_t *ip_dst0, *ip_dst1, *ip_dst2, *ip_dst3;
449 #endif /* RTE_MACHINE_CPUFLAG_SSE4_2 */
453 p = (const uint32_t *)&k->port_src;
455 #ifdef RTE_MACHINE_CPUFLAG_SSE4_2
456 ip_src0 = (const uint32_t *) k->ip_src;
457 ip_src1 = (const uint32_t *)(k->ip_src + 4);
458 ip_src2 = (const uint32_t *)(k->ip_src + 8);
459 ip_src3 = (const uint32_t *)(k->ip_src + 12);
460 ip_dst0 = (const uint32_t *) k->ip_dst;
461 ip_dst1 = (const uint32_t *)(k->ip_dst + 4);
462 ip_dst2 = (const uint32_t *)(k->ip_dst + 8);
463 ip_dst3 = (const uint32_t *)(k->ip_dst + 12);
464 init_val = rte_hash_crc_4byte(t, init_val);
465 init_val = rte_hash_crc_4byte(*ip_src0, init_val);
466 init_val = rte_hash_crc_4byte(*ip_src1, init_val);
467 init_val = rte_hash_crc_4byte(*ip_src2, init_val);
468 init_val = rte_hash_crc_4byte(*ip_src3, init_val);
469 init_val = rte_hash_crc_4byte(*ip_dst0, init_val);
470 init_val = rte_hash_crc_4byte(*ip_dst1, init_val);
471 init_val = rte_hash_crc_4byte(*ip_dst2, init_val);
472 init_val = rte_hash_crc_4byte(*ip_dst3, init_val);
473 init_val = rte_hash_crc_4byte(*p, init_val);
474 #else /* RTE_MACHINE_CPUFLAG_SSE4_2 */
475 init_val = rte_jhash_1word(t, init_val);
476 init_val = rte_jhash(k->ip_src, sizeof(uint8_t) * IPV6_ADDR_LEN, init_val);
477 init_val = rte_jhash(k->ip_dst, sizeof(uint8_t) * IPV6_ADDR_LEN, init_val);
478 init_val = rte_jhash_1word(*p, init_val);
479 #endif /* RTE_MACHINE_CPUFLAG_SSE4_2 */
483 #define IPV4_L3FWD_NUM_ROUTES RTE_DIM(ipv4_l3fwd_route_array)
484 #define IPV6_L3FWD_NUM_ROUTES RTE_DIM(ipv6_l3fwd_route_array)
486 static uint8_t ipv4_l3fwd_out_if[L3FWD_HASH_ENTRIES] __rte_cache_aligned;
487 static uint8_t ipv6_l3fwd_out_if[L3FWD_HASH_ENTRIES] __rte_cache_aligned;
491 #if (APP_LOOKUP_METHOD == APP_LOOKUP_LPM)
492 struct ipv4_l3fwd_route {
498 struct ipv6_l3fwd_route {
504 static struct ipv4_l3fwd_route ipv4_l3fwd_route_array[] = {
505 {IPv4(1, 1, 1, 0), 24, 0},
506 {IPv4(2, 1, 1, 0), 24, 1},
507 {IPv4(3, 1, 1, 0), 24, 2},
508 {IPv4(4, 1, 1, 0), 24, 3},
509 {IPv4(5, 1, 1, 0), 24, 4},
510 {IPv4(6, 1, 1, 0), 24, 5},
511 {IPv4(7, 1, 1, 0), 24, 6},
512 {IPv4(8, 1, 1, 0), 24, 7},
515 static struct ipv6_l3fwd_route ipv6_l3fwd_route_array[] = {
516 {{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, 48, 0},
517 {{2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, 48, 1},
518 {{3, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, 48, 2},
519 {{4, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, 48, 3},
520 {{5, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, 48, 4},
521 {{6, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, 48, 5},
522 {{7, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, 48, 6},
523 {{8, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, 48, 7},
526 #define IPV4_L3FWD_NUM_ROUTES RTE_DIM(ipv4_l3fwd_route_array)
527 #define IPV6_L3FWD_NUM_ROUTES RTE_DIM(ipv6_l3fwd_route_array)
529 #define IPV4_L3FWD_LPM_MAX_RULES 1024
530 #define IPV6_L3FWD_LPM_MAX_RULES 1024
531 #define IPV6_L3FWD_LPM_NUMBER_TBL8S (1 << 16)
533 typedef struct rte_lpm lookup_struct_t;
534 typedef struct rte_lpm6 lookup6_struct_t;
535 static lookup_struct_t *ipv4_l3fwd_lookup_struct[NB_SOCKETS];
536 static lookup6_struct_t *ipv6_l3fwd_lookup_struct[NB_SOCKETS];
540 lookup_struct_t *ipv4_lookup_struct;
541 #if (APP_LOOKUP_METHOD == APP_LOOKUP_LPM)
542 lookup6_struct_t *ipv6_lookup_struct;
544 lookup_struct_t *ipv6_lookup_struct;
547 } __rte_cache_aligned;
549 static struct lcore_conf lcore_conf[RTE_MAX_LCORE];
550 RTE_DEFINE_PER_LCORE(struct lcore_conf *, lcore_conf);
552 #define MAX_RX_QUEUE_PER_THREAD 16
553 #define MAX_TX_PORT_PER_THREAD RTE_MAX_ETHPORTS
554 #define MAX_TX_QUEUE_PER_PORT RTE_MAX_ETHPORTS
555 #define MAX_RX_QUEUE_PER_PORT 128
557 #define MAX_RX_THREAD 1024
558 #define MAX_TX_THREAD 1024
559 #define MAX_THREAD (MAX_RX_THREAD + MAX_TX_THREAD)
562 * Producers and consumers threads configuration
564 static int lthreads_on = 1; /**< Use lthreads for processing*/
566 rte_atomic16_t rx_counter; /**< Number of spawned rx threads */
567 rte_atomic16_t tx_counter; /**< Number of spawned tx threads */
570 uint16_t lcore_id; /**< Initial lcore for rx thread */
571 uint16_t cpu_id; /**< Cpu id for cpu load stats counter */
572 uint16_t thread_id; /**< Thread ID */
574 #if (APP_CPU_LOAD > 0)
575 int busy[MAX_CPU_COUNTER];
579 struct thread_rx_conf {
580 struct thread_conf conf;
583 struct lcore_rx_queue rx_queue_list[MAX_RX_QUEUE_PER_LCORE];
585 uint16_t n_ring; /**< Number of output rings */
586 struct rte_ring *ring[RTE_MAX_LCORE];
587 struct lthread_cond *ready[RTE_MAX_LCORE];
589 #if (APP_CPU_LOAD > 0)
590 int busy[MAX_CPU_COUNTER];
592 } __rte_cache_aligned;
594 uint16_t n_rx_thread;
595 struct thread_rx_conf rx_thread[MAX_RX_THREAD];
597 struct thread_tx_conf {
598 struct thread_conf conf;
600 uint16_t tx_queue_id[RTE_MAX_LCORE];
601 struct mbuf_table tx_mbufs[RTE_MAX_LCORE];
603 struct rte_ring *ring;
604 struct lthread_cond **ready;
606 } __rte_cache_aligned;
608 uint16_t n_tx_thread;
609 struct thread_tx_conf tx_thread[MAX_TX_THREAD];
611 /* Send burst of packets on an output interface */
613 send_burst(struct thread_tx_conf *qconf, uint16_t n, uint8_t port)
615 struct rte_mbuf **m_table;
619 queueid = qconf->tx_queue_id[port];
620 m_table = (struct rte_mbuf **)qconf->tx_mbufs[port].m_table;
622 ret = rte_eth_tx_burst(port, queueid, m_table, n);
623 if (unlikely(ret < n)) {
625 rte_pktmbuf_free(m_table[ret]);
632 /* Enqueue a single packet, and send burst if queue is filled */
634 send_single_packet(struct rte_mbuf *m, uint8_t port)
637 struct thread_tx_conf *qconf;
640 qconf = (struct thread_tx_conf *)lthread_get_data();
642 qconf = (struct thread_tx_conf *)RTE_PER_LCORE(lcore_conf)->data;
644 len = qconf->tx_mbufs[port].len;
645 qconf->tx_mbufs[port].m_table[len] = m;
648 /* enough pkts to be sent */
649 if (unlikely(len == MAX_PKT_BURST)) {
650 send_burst(qconf, MAX_PKT_BURST, port);
654 qconf->tx_mbufs[port].len = len;
658 #if (APP_LOOKUP_METHOD == APP_LOOKUP_LPM)
659 static inline __attribute__((always_inline)) void
660 send_packetsx4(uint8_t port,
661 struct rte_mbuf *m[], uint32_t num)
664 struct thread_tx_conf *qconf;
667 qconf = (struct thread_tx_conf *)lthread_get_data();
669 qconf = (struct thread_tx_conf *)RTE_PER_LCORE(lcore_conf)->data;
671 len = qconf->tx_mbufs[port].len;
674 * If TX buffer for that queue is empty, and we have enough packets,
675 * then send them straightway.
677 if (num >= MAX_TX_BURST && len == 0) {
678 n = rte_eth_tx_burst(port, qconf->tx_queue_id[port], m, num);
679 if (unlikely(n < num)) {
681 rte_pktmbuf_free(m[n]);
688 * Put packets into TX buffer for that queue.
692 n = (n > MAX_PKT_BURST) ? MAX_PKT_BURST - len : num;
695 switch (n % FWDSTEP) {
698 qconf->tx_mbufs[port].m_table[len + j] = m[j];
701 qconf->tx_mbufs[port].m_table[len + j] = m[j];
704 qconf->tx_mbufs[port].m_table[len + j] = m[j];
707 qconf->tx_mbufs[port].m_table[len + j] = m[j];
714 /* enough pkts to be sent */
715 if (unlikely(len == MAX_PKT_BURST)) {
717 send_burst(qconf, MAX_PKT_BURST, port);
719 /* copy rest of the packets into the TX buffer. */
722 switch (len % FWDSTEP) {
725 qconf->tx_mbufs[port].m_table[j] = m[n + j];
728 qconf->tx_mbufs[port].m_table[j] = m[n + j];
731 qconf->tx_mbufs[port].m_table[j] = m[n + j];
734 qconf->tx_mbufs[port].m_table[j] = m[n + j];
740 qconf->tx_mbufs[port].len = len;
742 #endif /* APP_LOOKUP_LPM */
744 #ifdef DO_RFC_1812_CHECKS
746 is_valid_ipv4_pkt(struct ipv4_hdr *pkt, uint32_t link_len)
748 /* From http://www.rfc-editor.org/rfc/rfc1812.txt section 5.2.2 */
750 * 1. The packet length reported by the Link Layer must be large
751 * enough to hold the minimum length legal IP datagram (20 bytes).
753 if (link_len < sizeof(struct ipv4_hdr))
756 /* 2. The IP checksum must be correct. */
757 /* this is checked in H/W */
760 * 3. The IP version number must be 4. If the version number is not 4
761 * then the packet may be another version of IP, such as IPng or
764 if (((pkt->version_ihl) >> 4) != 4)
767 * 4. The IP header length field must be large enough to hold the
768 * minimum length legal IP datagram (20 bytes = 5 words).
770 if ((pkt->version_ihl & 0xf) < 5)
774 * 5. The IP total length field must be large enough to hold the IP
775 * datagram header, whose length is specified in the IP header length
778 if (rte_cpu_to_be_16(pkt->total_length) < sizeof(struct ipv4_hdr))
785 #if (APP_LOOKUP_METHOD == APP_LOOKUP_EXACT_MATCH)
787 static __m128i mask0;
788 static __m128i mask1;
789 static __m128i mask2;
790 static inline uint8_t
791 get_ipv4_dst_port(void *ipv4_hdr, uint8_t portid,
792 lookup_struct_t *ipv4_l3fwd_lookup_struct)
795 union ipv4_5tuple_host key;
797 ipv4_hdr = (uint8_t *)ipv4_hdr + offsetof(struct ipv4_hdr, time_to_live);
798 __m128i data = _mm_loadu_si128((__m128i *)(ipv4_hdr));
799 /* Get 5 tuple: dst port, src port, dst IP address, src IP address and
801 key.xmm = _mm_and_si128(data, mask0);
802 /* Find destination port */
803 ret = rte_hash_lookup(ipv4_l3fwd_lookup_struct, (const void *)&key);
804 return (uint8_t)((ret < 0) ? portid : ipv4_l3fwd_out_if[ret]);
807 static inline uint8_t
808 get_ipv6_dst_port(void *ipv6_hdr, uint8_t portid,
809 lookup_struct_t *ipv6_l3fwd_lookup_struct)
812 union ipv6_5tuple_host key;
814 ipv6_hdr = (uint8_t *)ipv6_hdr + offsetof(struct ipv6_hdr, payload_len);
815 __m128i data0 = _mm_loadu_si128((__m128i *)(ipv6_hdr));
816 __m128i data1 = _mm_loadu_si128((__m128i *)(((uint8_t *)ipv6_hdr) +
818 __m128i data2 = _mm_loadu_si128((__m128i *)(((uint8_t *)ipv6_hdr) +
819 sizeof(__m128i) + sizeof(__m128i)));
820 /* Get part of 5 tuple: src IP address lower 96 bits and protocol */
821 key.xmm[0] = _mm_and_si128(data0, mask1);
822 /* Get part of 5 tuple: dst IP address lower 96 bits and src IP address
825 /* Get part of 5 tuple: dst port and src port and dst IP address higher
827 key.xmm[2] = _mm_and_si128(data2, mask2);
829 /* Find destination port */
830 ret = rte_hash_lookup(ipv6_l3fwd_lookup_struct, (const void *)&key);
831 return (uint8_t)((ret < 0) ? portid : ipv6_l3fwd_out_if[ret]);
835 #if (APP_LOOKUP_METHOD == APP_LOOKUP_LPM)
837 static inline uint8_t
838 get_ipv4_dst_port(void *ipv4_hdr, uint8_t portid,
839 lookup_struct_t *ipv4_l3fwd_lookup_struct)
843 return (uint8_t)((rte_lpm_lookup(ipv4_l3fwd_lookup_struct,
844 rte_be_to_cpu_32(((struct ipv4_hdr *)ipv4_hdr)->dst_addr),
845 &next_hop) == 0) ? next_hop : portid);
848 static inline uint8_t
849 get_ipv6_dst_port(void *ipv6_hdr, uint8_t portid,
850 lookup6_struct_t *ipv6_l3fwd_lookup_struct)
854 return (uint8_t) ((rte_lpm6_lookup(ipv6_l3fwd_lookup_struct,
855 ((struct ipv6_hdr *)ipv6_hdr)->dst_addr, &next_hop) == 0) ?
860 static inline void l3fwd_simple_forward(struct rte_mbuf *m, uint8_t portid)
861 __attribute__((unused));
863 #if ((APP_LOOKUP_METHOD == APP_LOOKUP_EXACT_MATCH) && \
864 (ENABLE_MULTI_BUFFER_OPTIMIZE == 1))
866 #define MASK_ALL_PKTS 0xff
867 #define EXCLUDE_1ST_PKT 0xfe
868 #define EXCLUDE_2ND_PKT 0xfd
869 #define EXCLUDE_3RD_PKT 0xfb
870 #define EXCLUDE_4TH_PKT 0xf7
871 #define EXCLUDE_5TH_PKT 0xef
872 #define EXCLUDE_6TH_PKT 0xdf
873 #define EXCLUDE_7TH_PKT 0xbf
874 #define EXCLUDE_8TH_PKT 0x7f
877 simple_ipv4_fwd_8pkts(struct rte_mbuf *m[8], uint8_t portid)
879 struct ether_hdr *eth_hdr[8];
880 struct ipv4_hdr *ipv4_hdr[8];
883 union ipv4_5tuple_host key[8];
886 eth_hdr[0] = rte_pktmbuf_mtod(m[0], struct ether_hdr *);
887 eth_hdr[1] = rte_pktmbuf_mtod(m[1], struct ether_hdr *);
888 eth_hdr[2] = rte_pktmbuf_mtod(m[2], struct ether_hdr *);
889 eth_hdr[3] = rte_pktmbuf_mtod(m[3], struct ether_hdr *);
890 eth_hdr[4] = rte_pktmbuf_mtod(m[4], struct ether_hdr *);
891 eth_hdr[5] = rte_pktmbuf_mtod(m[5], struct ether_hdr *);
892 eth_hdr[6] = rte_pktmbuf_mtod(m[6], struct ether_hdr *);
893 eth_hdr[7] = rte_pktmbuf_mtod(m[7], struct ether_hdr *);
895 /* Handle IPv4 headers.*/
896 ipv4_hdr[0] = rte_pktmbuf_mtod_offset(m[0], struct ipv4_hdr *,
897 sizeof(struct ether_hdr));
898 ipv4_hdr[1] = rte_pktmbuf_mtod_offset(m[1], struct ipv4_hdr *,
899 sizeof(struct ether_hdr));
900 ipv4_hdr[2] = rte_pktmbuf_mtod_offset(m[2], struct ipv4_hdr *,
901 sizeof(struct ether_hdr));
902 ipv4_hdr[3] = rte_pktmbuf_mtod_offset(m[3], struct ipv4_hdr *,
903 sizeof(struct ether_hdr));
904 ipv4_hdr[4] = rte_pktmbuf_mtod_offset(m[4], struct ipv4_hdr *,
905 sizeof(struct ether_hdr));
906 ipv4_hdr[5] = rte_pktmbuf_mtod_offset(m[5], struct ipv4_hdr *,
907 sizeof(struct ether_hdr));
908 ipv4_hdr[6] = rte_pktmbuf_mtod_offset(m[6], struct ipv4_hdr *,
909 sizeof(struct ether_hdr));
910 ipv4_hdr[7] = rte_pktmbuf_mtod_offset(m[7], struct ipv4_hdr *,
911 sizeof(struct ether_hdr));
913 #ifdef DO_RFC_1812_CHECKS
914 /* Check to make sure the packet is valid (RFC1812) */
915 uint8_t valid_mask = MASK_ALL_PKTS;
917 if (is_valid_ipv4_pkt(ipv4_hdr[0], m[0]->pkt_len) < 0) {
918 rte_pktmbuf_free(m[0]);
919 valid_mask &= EXCLUDE_1ST_PKT;
921 if (is_valid_ipv4_pkt(ipv4_hdr[1], m[1]->pkt_len) < 0) {
922 rte_pktmbuf_free(m[1]);
923 valid_mask &= EXCLUDE_2ND_PKT;
925 if (is_valid_ipv4_pkt(ipv4_hdr[2], m[2]->pkt_len) < 0) {
926 rte_pktmbuf_free(m[2]);
927 valid_mask &= EXCLUDE_3RD_PKT;
929 if (is_valid_ipv4_pkt(ipv4_hdr[3], m[3]->pkt_len) < 0) {
930 rte_pktmbuf_free(m[3]);
931 valid_mask &= EXCLUDE_4TH_PKT;
933 if (is_valid_ipv4_pkt(ipv4_hdr[4], m[4]->pkt_len) < 0) {
934 rte_pktmbuf_free(m[4]);
935 valid_mask &= EXCLUDE_5TH_PKT;
937 if (is_valid_ipv4_pkt(ipv4_hdr[5], m[5]->pkt_len) < 0) {
938 rte_pktmbuf_free(m[5]);
939 valid_mask &= EXCLUDE_6TH_PKT;
941 if (is_valid_ipv4_pkt(ipv4_hdr[6], m[6]->pkt_len) < 0) {
942 rte_pktmbuf_free(m[6]);
943 valid_mask &= EXCLUDE_7TH_PKT;
945 if (is_valid_ipv4_pkt(ipv4_hdr[7], m[7]->pkt_len) < 0) {
946 rte_pktmbuf_free(m[7]);
947 valid_mask &= EXCLUDE_8TH_PKT;
949 if (unlikely(valid_mask != MASK_ALL_PKTS)) {
955 for (i = 0; i < 8; i++)
956 if ((0x1 << i) & valid_mask)
957 l3fwd_simple_forward(m[i], portid);
959 #endif /* End of #ifdef DO_RFC_1812_CHECKS */
961 data[0] = _mm_loadu_si128(rte_pktmbuf_mtod_offset(m[0], __m128i *,
962 sizeof(struct ether_hdr) +
963 offsetof(struct ipv4_hdr, time_to_live)));
964 data[1] = _mm_loadu_si128(rte_pktmbuf_mtod_offset(m[1], __m128i *,
965 sizeof(struct ether_hdr) +
966 offsetof(struct ipv4_hdr, time_to_live)));
967 data[2] = _mm_loadu_si128(rte_pktmbuf_mtod_offset(m[2], __m128i *,
968 sizeof(struct ether_hdr) +
969 offsetof(struct ipv4_hdr, time_to_live)));
970 data[3] = _mm_loadu_si128(rte_pktmbuf_mtod_offset(m[3], __m128i *,
971 sizeof(struct ether_hdr) +
972 offsetof(struct ipv4_hdr, time_to_live)));
973 data[4] = _mm_loadu_si128(rte_pktmbuf_mtod_offset(m[4], __m128i *,
974 sizeof(struct ether_hdr) +
975 offsetof(struct ipv4_hdr, time_to_live)));
976 data[5] = _mm_loadu_si128(rte_pktmbuf_mtod_offset(m[5], __m128i *,
977 sizeof(struct ether_hdr) +
978 offsetof(struct ipv4_hdr, time_to_live)));
979 data[6] = _mm_loadu_si128(rte_pktmbuf_mtod_offset(m[6], __m128i *,
980 sizeof(struct ether_hdr) +
981 offsetof(struct ipv4_hdr, time_to_live)));
982 data[7] = _mm_loadu_si128(rte_pktmbuf_mtod_offset(m[7], __m128i *,
983 sizeof(struct ether_hdr) +
984 offsetof(struct ipv4_hdr, time_to_live)));
986 key[0].xmm = _mm_and_si128(data[0], mask0);
987 key[1].xmm = _mm_and_si128(data[1], mask0);
988 key[2].xmm = _mm_and_si128(data[2], mask0);
989 key[3].xmm = _mm_and_si128(data[3], mask0);
990 key[4].xmm = _mm_and_si128(data[4], mask0);
991 key[5].xmm = _mm_and_si128(data[5], mask0);
992 key[6].xmm = _mm_and_si128(data[6], mask0);
993 key[7].xmm = _mm_and_si128(data[7], mask0);
995 const void *key_array[8] = {&key[0], &key[1], &key[2], &key[3],
996 &key[4], &key[5], &key[6], &key[7]};
998 rte_hash_lookup_multi(RTE_PER_LCORE(lcore_conf)->ipv4_lookup_struct,
999 &key_array[0], 8, ret);
1000 dst_port[0] = (uint8_t) ((ret[0] < 0) ? portid : ipv4_l3fwd_out_if[ret[0]]);
1001 dst_port[1] = (uint8_t) ((ret[1] < 0) ? portid : ipv4_l3fwd_out_if[ret[1]]);
1002 dst_port[2] = (uint8_t) ((ret[2] < 0) ? portid : ipv4_l3fwd_out_if[ret[2]]);
1003 dst_port[3] = (uint8_t) ((ret[3] < 0) ? portid : ipv4_l3fwd_out_if[ret[3]]);
1004 dst_port[4] = (uint8_t) ((ret[4] < 0) ? portid : ipv4_l3fwd_out_if[ret[4]]);
1005 dst_port[5] = (uint8_t) ((ret[5] < 0) ? portid : ipv4_l3fwd_out_if[ret[5]]);
1006 dst_port[6] = (uint8_t) ((ret[6] < 0) ? portid : ipv4_l3fwd_out_if[ret[6]]);
1007 dst_port[7] = (uint8_t) ((ret[7] < 0) ? portid : ipv4_l3fwd_out_if[ret[7]]);
1009 if (dst_port[0] >= RTE_MAX_ETHPORTS ||
1010 (enabled_port_mask & 1 << dst_port[0]) == 0)
1011 dst_port[0] = portid;
1012 if (dst_port[1] >= RTE_MAX_ETHPORTS ||
1013 (enabled_port_mask & 1 << dst_port[1]) == 0)
1014 dst_port[1] = portid;
1015 if (dst_port[2] >= RTE_MAX_ETHPORTS ||
1016 (enabled_port_mask & 1 << dst_port[2]) == 0)
1017 dst_port[2] = portid;
1018 if (dst_port[3] >= RTE_MAX_ETHPORTS ||
1019 (enabled_port_mask & 1 << dst_port[3]) == 0)
1020 dst_port[3] = portid;
1021 if (dst_port[4] >= RTE_MAX_ETHPORTS ||
1022 (enabled_port_mask & 1 << dst_port[4]) == 0)
1023 dst_port[4] = portid;
1024 if (dst_port[5] >= RTE_MAX_ETHPORTS ||
1025 (enabled_port_mask & 1 << dst_port[5]) == 0)
1026 dst_port[5] = portid;
1027 if (dst_port[6] >= RTE_MAX_ETHPORTS ||
1028 (enabled_port_mask & 1 << dst_port[6]) == 0)
1029 dst_port[6] = portid;
1030 if (dst_port[7] >= RTE_MAX_ETHPORTS ||
1031 (enabled_port_mask & 1 << dst_port[7]) == 0)
1032 dst_port[7] = portid;
1034 #ifdef DO_RFC_1812_CHECKS
1035 /* Update time to live and header checksum */
1036 --(ipv4_hdr[0]->time_to_live);
1037 --(ipv4_hdr[1]->time_to_live);
1038 --(ipv4_hdr[2]->time_to_live);
1039 --(ipv4_hdr[3]->time_to_live);
1040 ++(ipv4_hdr[0]->hdr_checksum);
1041 ++(ipv4_hdr[1]->hdr_checksum);
1042 ++(ipv4_hdr[2]->hdr_checksum);
1043 ++(ipv4_hdr[3]->hdr_checksum);
1044 --(ipv4_hdr[4]->time_to_live);
1045 --(ipv4_hdr[5]->time_to_live);
1046 --(ipv4_hdr[6]->time_to_live);
1047 --(ipv4_hdr[7]->time_to_live);
1048 ++(ipv4_hdr[4]->hdr_checksum);
1049 ++(ipv4_hdr[5]->hdr_checksum);
1050 ++(ipv4_hdr[6]->hdr_checksum);
1051 ++(ipv4_hdr[7]->hdr_checksum);
1055 *(uint64_t *)ð_hdr[0]->d_addr = dest_eth_addr[dst_port[0]];
1056 *(uint64_t *)ð_hdr[1]->d_addr = dest_eth_addr[dst_port[1]];
1057 *(uint64_t *)ð_hdr[2]->d_addr = dest_eth_addr[dst_port[2]];
1058 *(uint64_t *)ð_hdr[3]->d_addr = dest_eth_addr[dst_port[3]];
1059 *(uint64_t *)ð_hdr[4]->d_addr = dest_eth_addr[dst_port[4]];
1060 *(uint64_t *)ð_hdr[5]->d_addr = dest_eth_addr[dst_port[5]];
1061 *(uint64_t *)ð_hdr[6]->d_addr = dest_eth_addr[dst_port[6]];
1062 *(uint64_t *)ð_hdr[7]->d_addr = dest_eth_addr[dst_port[7]];
1065 ether_addr_copy(&ports_eth_addr[dst_port[0]], ð_hdr[0]->s_addr);
1066 ether_addr_copy(&ports_eth_addr[dst_port[1]], ð_hdr[1]->s_addr);
1067 ether_addr_copy(&ports_eth_addr[dst_port[2]], ð_hdr[2]->s_addr);
1068 ether_addr_copy(&ports_eth_addr[dst_port[3]], ð_hdr[3]->s_addr);
1069 ether_addr_copy(&ports_eth_addr[dst_port[4]], ð_hdr[4]->s_addr);
1070 ether_addr_copy(&ports_eth_addr[dst_port[5]], ð_hdr[5]->s_addr);
1071 ether_addr_copy(&ports_eth_addr[dst_port[6]], ð_hdr[6]->s_addr);
1072 ether_addr_copy(&ports_eth_addr[dst_port[7]], ð_hdr[7]->s_addr);
1074 send_single_packet(m[0], (uint8_t)dst_port[0]);
1075 send_single_packet(m[1], (uint8_t)dst_port[1]);
1076 send_single_packet(m[2], (uint8_t)dst_port[2]);
1077 send_single_packet(m[3], (uint8_t)dst_port[3]);
1078 send_single_packet(m[4], (uint8_t)dst_port[4]);
1079 send_single_packet(m[5], (uint8_t)dst_port[5]);
1080 send_single_packet(m[6], (uint8_t)dst_port[6]);
1081 send_single_packet(m[7], (uint8_t)dst_port[7]);
1085 static inline void get_ipv6_5tuple(struct rte_mbuf *m0, __m128i mask0,
1086 __m128i mask1, union ipv6_5tuple_host *key)
1088 __m128i tmpdata0 = _mm_loadu_si128(rte_pktmbuf_mtod_offset(m0,
1089 __m128i *, sizeof(struct ether_hdr) +
1090 offsetof(struct ipv6_hdr, payload_len)));
1091 __m128i tmpdata1 = _mm_loadu_si128(rte_pktmbuf_mtod_offset(m0,
1092 __m128i *, sizeof(struct ether_hdr) +
1093 offsetof(struct ipv6_hdr, payload_len) + sizeof(__m128i)));
1094 __m128i tmpdata2 = _mm_loadu_si128(rte_pktmbuf_mtod_offset(m0,
1095 __m128i *, sizeof(struct ether_hdr) +
1096 offsetof(struct ipv6_hdr, payload_len) + sizeof(__m128i) +
1098 key->xmm[0] = _mm_and_si128(tmpdata0, mask0);
1099 key->xmm[1] = tmpdata1;
1100 key->xmm[2] = _mm_and_si128(tmpdata2, mask1);
1104 simple_ipv6_fwd_8pkts(struct rte_mbuf *m[8], uint8_t portid)
1107 uint8_t dst_port[8];
1108 struct ether_hdr *eth_hdr[8];
1109 union ipv6_5tuple_host key[8];
1111 __attribute__((unused)) struct ipv6_hdr *ipv6_hdr[8];
1113 eth_hdr[0] = rte_pktmbuf_mtod(m[0], struct ether_hdr *);
1114 eth_hdr[1] = rte_pktmbuf_mtod(m[1], struct ether_hdr *);
1115 eth_hdr[2] = rte_pktmbuf_mtod(m[2], struct ether_hdr *);
1116 eth_hdr[3] = rte_pktmbuf_mtod(m[3], struct ether_hdr *);
1117 eth_hdr[4] = rte_pktmbuf_mtod(m[4], struct ether_hdr *);
1118 eth_hdr[5] = rte_pktmbuf_mtod(m[5], struct ether_hdr *);
1119 eth_hdr[6] = rte_pktmbuf_mtod(m[6], struct ether_hdr *);
1120 eth_hdr[7] = rte_pktmbuf_mtod(m[7], struct ether_hdr *);
1122 /* Handle IPv6 headers.*/
1123 ipv6_hdr[0] = rte_pktmbuf_mtod_offset(m[0], struct ipv6_hdr *,
1124 sizeof(struct ether_hdr));
1125 ipv6_hdr[1] = rte_pktmbuf_mtod_offset(m[1], struct ipv6_hdr *,
1126 sizeof(struct ether_hdr));
1127 ipv6_hdr[2] = rte_pktmbuf_mtod_offset(m[2], struct ipv6_hdr *,
1128 sizeof(struct ether_hdr));
1129 ipv6_hdr[3] = rte_pktmbuf_mtod_offset(m[3], struct ipv6_hdr *,
1130 sizeof(struct ether_hdr));
1131 ipv6_hdr[4] = rte_pktmbuf_mtod_offset(m[4], struct ipv6_hdr *,
1132 sizeof(struct ether_hdr));
1133 ipv6_hdr[5] = rte_pktmbuf_mtod_offset(m[5], struct ipv6_hdr *,
1134 sizeof(struct ether_hdr));
1135 ipv6_hdr[6] = rte_pktmbuf_mtod_offset(m[6], struct ipv6_hdr *,
1136 sizeof(struct ether_hdr));
1137 ipv6_hdr[7] = rte_pktmbuf_mtod_offset(m[7], struct ipv6_hdr *,
1138 sizeof(struct ether_hdr));
1140 get_ipv6_5tuple(m[0], mask1, mask2, &key[0]);
1141 get_ipv6_5tuple(m[1], mask1, mask2, &key[1]);
1142 get_ipv6_5tuple(m[2], mask1, mask2, &key[2]);
1143 get_ipv6_5tuple(m[3], mask1, mask2, &key[3]);
1144 get_ipv6_5tuple(m[4], mask1, mask2, &key[4]);
1145 get_ipv6_5tuple(m[5], mask1, mask2, &key[5]);
1146 get_ipv6_5tuple(m[6], mask1, mask2, &key[6]);
1147 get_ipv6_5tuple(m[7], mask1, mask2, &key[7]);
1149 const void *key_array[8] = {&key[0], &key[1], &key[2], &key[3],
1150 &key[4], &key[5], &key[6], &key[7]};
1152 rte_hash_lookup_multi(RTE_PER_LCORE(lcore_conf)->ipv6_lookup_struct,
1153 &key_array[0], 4, ret);
1154 dst_port[0] = (uint8_t) ((ret[0] < 0) ? portid : ipv6_l3fwd_out_if[ret[0]]);
1155 dst_port[1] = (uint8_t) ((ret[1] < 0) ? portid : ipv6_l3fwd_out_if[ret[1]]);
1156 dst_port[2] = (uint8_t) ((ret[2] < 0) ? portid : ipv6_l3fwd_out_if[ret[2]]);
1157 dst_port[3] = (uint8_t) ((ret[3] < 0) ? portid : ipv6_l3fwd_out_if[ret[3]]);
1158 dst_port[4] = (uint8_t) ((ret[4] < 0) ? portid : ipv6_l3fwd_out_if[ret[4]]);
1159 dst_port[5] = (uint8_t) ((ret[5] < 0) ? portid : ipv6_l3fwd_out_if[ret[5]]);
1160 dst_port[6] = (uint8_t) ((ret[6] < 0) ? portid : ipv6_l3fwd_out_if[ret[6]]);
1161 dst_port[7] = (uint8_t) ((ret[7] < 0) ? portid : ipv6_l3fwd_out_if[ret[7]]);
1163 if (dst_port[0] >= RTE_MAX_ETHPORTS ||
1164 (enabled_port_mask & 1 << dst_port[0]) == 0)
1165 dst_port[0] = portid;
1166 if (dst_port[1] >= RTE_MAX_ETHPORTS ||
1167 (enabled_port_mask & 1 << dst_port[1]) == 0)
1168 dst_port[1] = portid;
1169 if (dst_port[2] >= RTE_MAX_ETHPORTS ||
1170 (enabled_port_mask & 1 << dst_port[2]) == 0)
1171 dst_port[2] = portid;
1172 if (dst_port[3] >= RTE_MAX_ETHPORTS ||
1173 (enabled_port_mask & 1 << dst_port[3]) == 0)
1174 dst_port[3] = portid;
1175 if (dst_port[4] >= RTE_MAX_ETHPORTS ||
1176 (enabled_port_mask & 1 << dst_port[4]) == 0)
1177 dst_port[4] = portid;
1178 if (dst_port[5] >= RTE_MAX_ETHPORTS ||
1179 (enabled_port_mask & 1 << dst_port[5]) == 0)
1180 dst_port[5] = portid;
1181 if (dst_port[6] >= RTE_MAX_ETHPORTS ||
1182 (enabled_port_mask & 1 << dst_port[6]) == 0)
1183 dst_port[6] = portid;
1184 if (dst_port[7] >= RTE_MAX_ETHPORTS ||
1185 (enabled_port_mask & 1 << dst_port[7]) == 0)
1186 dst_port[7] = portid;
1189 *(uint64_t *)ð_hdr[0]->d_addr = dest_eth_addr[dst_port[0]];
1190 *(uint64_t *)ð_hdr[1]->d_addr = dest_eth_addr[dst_port[1]];
1191 *(uint64_t *)ð_hdr[2]->d_addr = dest_eth_addr[dst_port[2]];
1192 *(uint64_t *)ð_hdr[3]->d_addr = dest_eth_addr[dst_port[3]];
1193 *(uint64_t *)ð_hdr[4]->d_addr = dest_eth_addr[dst_port[4]];
1194 *(uint64_t *)ð_hdr[5]->d_addr = dest_eth_addr[dst_port[5]];
1195 *(uint64_t *)ð_hdr[6]->d_addr = dest_eth_addr[dst_port[6]];
1196 *(uint64_t *)ð_hdr[7]->d_addr = dest_eth_addr[dst_port[7]];
1199 ether_addr_copy(&ports_eth_addr[dst_port[0]], ð_hdr[0]->s_addr);
1200 ether_addr_copy(&ports_eth_addr[dst_port[1]], ð_hdr[1]->s_addr);
1201 ether_addr_copy(&ports_eth_addr[dst_port[2]], ð_hdr[2]->s_addr);
1202 ether_addr_copy(&ports_eth_addr[dst_port[3]], ð_hdr[3]->s_addr);
1203 ether_addr_copy(&ports_eth_addr[dst_port[4]], ð_hdr[4]->s_addr);
1204 ether_addr_copy(&ports_eth_addr[dst_port[5]], ð_hdr[5]->s_addr);
1205 ether_addr_copy(&ports_eth_addr[dst_port[6]], ð_hdr[6]->s_addr);
1206 ether_addr_copy(&ports_eth_addr[dst_port[7]], ð_hdr[7]->s_addr);
1208 send_single_packet(m[0], (uint8_t)dst_port[0]);
1209 send_single_packet(m[1], (uint8_t)dst_port[1]);
1210 send_single_packet(m[2], (uint8_t)dst_port[2]);
1211 send_single_packet(m[3], (uint8_t)dst_port[3]);
1212 send_single_packet(m[4], (uint8_t)dst_port[4]);
1213 send_single_packet(m[5], (uint8_t)dst_port[5]);
1214 send_single_packet(m[6], (uint8_t)dst_port[6]);
1215 send_single_packet(m[7], (uint8_t)dst_port[7]);
1218 #endif /* APP_LOOKUP_METHOD */
1220 static inline __attribute__((always_inline)) void
1221 l3fwd_simple_forward(struct rte_mbuf *m, uint8_t portid)
1223 struct ether_hdr *eth_hdr;
1224 struct ipv4_hdr *ipv4_hdr;
1227 eth_hdr = rte_pktmbuf_mtod(m, struct ether_hdr *);
1229 if (RTE_ETH_IS_IPV4_HDR(m->packet_type)) {
1230 /* Handle IPv4 headers.*/
1231 ipv4_hdr = rte_pktmbuf_mtod_offset(m, struct ipv4_hdr *,
1232 sizeof(struct ether_hdr));
1234 #ifdef DO_RFC_1812_CHECKS
1235 /* Check to make sure the packet is valid (RFC1812) */
1236 if (is_valid_ipv4_pkt(ipv4_hdr, m->pkt_len) < 0) {
1237 rte_pktmbuf_free(m);
1242 dst_port = get_ipv4_dst_port(ipv4_hdr, portid,
1243 RTE_PER_LCORE(lcore_conf)->ipv4_lookup_struct);
1244 if (dst_port >= RTE_MAX_ETHPORTS ||
1245 (enabled_port_mask & 1 << dst_port) == 0)
1248 #ifdef DO_RFC_1812_CHECKS
1249 /* Update time to live and header checksum */
1250 --(ipv4_hdr->time_to_live);
1251 ++(ipv4_hdr->hdr_checksum);
1254 *(uint64_t *)ð_hdr->d_addr = dest_eth_addr[dst_port];
1257 ether_addr_copy(&ports_eth_addr[dst_port], ð_hdr->s_addr);
1259 send_single_packet(m, dst_port);
1260 } else if (RTE_ETH_IS_IPV6_HDR(m->packet_type)) {
1261 /* Handle IPv6 headers.*/
1262 struct ipv6_hdr *ipv6_hdr;
1264 ipv6_hdr = rte_pktmbuf_mtod_offset(m, struct ipv6_hdr *,
1265 sizeof(struct ether_hdr));
1267 dst_port = get_ipv6_dst_port(ipv6_hdr, portid,
1268 RTE_PER_LCORE(lcore_conf)->ipv6_lookup_struct);
1270 if (dst_port >= RTE_MAX_ETHPORTS ||
1271 (enabled_port_mask & 1 << dst_port) == 0)
1275 *(uint64_t *)ð_hdr->d_addr = dest_eth_addr[dst_port];
1278 ether_addr_copy(&ports_eth_addr[dst_port], ð_hdr->s_addr);
1280 send_single_packet(m, dst_port);
1282 /* Free the mbuf that contains non-IPV4/IPV6 packet */
1283 rte_pktmbuf_free(m);
1286 #if ((APP_LOOKUP_METHOD == APP_LOOKUP_LPM) && \
1287 (ENABLE_MULTI_BUFFER_OPTIMIZE == 1))
1288 #ifdef DO_RFC_1812_CHECKS
1290 #define IPV4_MIN_VER_IHL 0x45
1291 #define IPV4_MAX_VER_IHL 0x4f
1292 #define IPV4_MAX_VER_IHL_DIFF (IPV4_MAX_VER_IHL - IPV4_MIN_VER_IHL)
1294 /* Minimum value of IPV4 total length (20B) in network byte order. */
1295 #define IPV4_MIN_LEN_BE (sizeof(struct ipv4_hdr) << 8)
1298 * From http://www.rfc-editor.org/rfc/rfc1812.txt section 5.2.2:
1299 * - The IP version number must be 4.
1300 * - The IP header length field must be large enough to hold the
1301 * minimum length legal IP datagram (20 bytes = 5 words).
1302 * - The IP total length field must be large enough to hold the IP
1303 * datagram header, whose length is specified in the IP header length
1305 * If we encounter invalid IPV4 packet, then set destination port for it
1306 * to BAD_PORT value.
1308 static inline __attribute__((always_inline)) void
1309 rfc1812_process(struct ipv4_hdr *ipv4_hdr, uint32_t *dp, uint32_t ptype)
1313 if (RTE_ETH_IS_IPV4_HDR(ptype)) {
1314 ihl = ipv4_hdr->version_ihl - IPV4_MIN_VER_IHL;
1316 ipv4_hdr->time_to_live--;
1317 ipv4_hdr->hdr_checksum++;
1319 if (ihl > IPV4_MAX_VER_IHL_DIFF ||
1320 ((uint8_t)ipv4_hdr->total_length == 0 &&
1321 ipv4_hdr->total_length < IPV4_MIN_LEN_BE)) {
1328 #define rfc1812_process(mb, dp) do { } while (0)
1329 #endif /* DO_RFC_1812_CHECKS */
1330 #endif /* APP_LOOKUP_LPM && ENABLE_MULTI_BUFFER_OPTIMIZE */
1333 #if ((APP_LOOKUP_METHOD == APP_LOOKUP_LPM) && \
1334 (ENABLE_MULTI_BUFFER_OPTIMIZE == 1))
1336 static inline __attribute__((always_inline)) uint16_t
1337 get_dst_port(struct rte_mbuf *pkt, uint32_t dst_ipv4, uint8_t portid)
1339 uint32_t next_hop_ipv4;
1340 uint8_t next_hop_ipv6;
1341 struct ipv6_hdr *ipv6_hdr;
1342 struct ether_hdr *eth_hdr;
1344 if (RTE_ETH_IS_IPV4_HDR(pkt->packet_type)) {
1345 if (rte_lpm_lookup(RTE_PER_LCORE(lcore_conf)->ipv4_lookup_struct,
1346 dst_ipv4, &next_hop_ipv4) != 0) {
1347 next_hop_ipv4 = portid;
1348 return next_hop_ipv4;
1350 } else if (RTE_ETH_IS_IPV6_HDR(pkt->packet_type)) {
1351 eth_hdr = rte_pktmbuf_mtod(pkt, struct ether_hdr *);
1352 ipv6_hdr = (struct ipv6_hdr *)(eth_hdr + 1);
1353 if (rte_lpm6_lookup(RTE_PER_LCORE(lcore_conf)->ipv6_lookup_struct,
1354 ipv6_hdr->dst_addr, &next_hop_ipv6) != 0) {
1355 next_hop_ipv6 = portid;
1356 return next_hop_ipv6;
1359 next_hop_ipv4 = portid;
1360 return next_hop_ipv4;
1366 process_packet(struct rte_mbuf *pkt, uint32_t *dst_port, uint8_t portid)
1368 struct ether_hdr *eth_hdr;
1369 struct ipv4_hdr *ipv4_hdr;
1374 eth_hdr = rte_pktmbuf_mtod(pkt, struct ether_hdr *);
1375 ipv4_hdr = (struct ipv4_hdr *)(eth_hdr + 1);
1377 dst_ipv4 = ipv4_hdr->dst_addr;
1378 dst_ipv4 = rte_be_to_cpu_32(dst_ipv4);
1379 dp = get_dst_port(pkt, dst_ipv4, portid);
1381 te = _mm_load_si128((__m128i *)eth_hdr);
1385 rfc1812_process(ipv4_hdr, dst_port, pkt->packet_type);
1387 te = _mm_blend_epi16(te, ve, MASK_ETH);
1388 _mm_store_si128((__m128i *)eth_hdr, te);
1392 * Read packet_type and destination IPV4 addresses from 4 mbufs.
1395 processx4_step1(struct rte_mbuf *pkt[FWDSTEP],
1397 uint32_t *ipv4_flag)
1399 struct ipv4_hdr *ipv4_hdr;
1400 struct ether_hdr *eth_hdr;
1401 uint32_t x0, x1, x2, x3;
1403 eth_hdr = rte_pktmbuf_mtod(pkt[0], struct ether_hdr *);
1404 ipv4_hdr = (struct ipv4_hdr *)(eth_hdr + 1);
1405 x0 = ipv4_hdr->dst_addr;
1406 ipv4_flag[0] = pkt[0]->packet_type & RTE_PTYPE_L3_IPV4;
1408 eth_hdr = rte_pktmbuf_mtod(pkt[1], struct ether_hdr *);
1409 ipv4_hdr = (struct ipv4_hdr *)(eth_hdr + 1);
1410 x1 = ipv4_hdr->dst_addr;
1411 ipv4_flag[0] &= pkt[1]->packet_type;
1413 eth_hdr = rte_pktmbuf_mtod(pkt[2], struct ether_hdr *);
1414 ipv4_hdr = (struct ipv4_hdr *)(eth_hdr + 1);
1415 x2 = ipv4_hdr->dst_addr;
1416 ipv4_flag[0] &= pkt[2]->packet_type;
1418 eth_hdr = rte_pktmbuf_mtod(pkt[3], struct ether_hdr *);
1419 ipv4_hdr = (struct ipv4_hdr *)(eth_hdr + 1);
1420 x3 = ipv4_hdr->dst_addr;
1421 ipv4_flag[0] &= pkt[3]->packet_type;
1423 dip[0] = _mm_set_epi32(x3, x2, x1, x0);
1427 * Lookup into LPM for destination port.
1428 * If lookup fails, use incoming port (portid) as destination port.
1431 processx4_step2(__m128i dip,
1434 struct rte_mbuf *pkt[FWDSTEP],
1435 uint32_t dprt[FWDSTEP])
1438 const __m128i bswap_mask = _mm_set_epi8(12, 13, 14, 15, 8, 9, 10, 11,
1439 4, 5, 6, 7, 0, 1, 2, 3);
1441 /* Byte swap 4 IPV4 addresses. */
1442 dip = _mm_shuffle_epi8(dip, bswap_mask);
1444 /* if all 4 packets are IPV4. */
1445 if (likely(ipv4_flag)) {
1446 rte_lpm_lookupx4(RTE_PER_LCORE(lcore_conf)->ipv4_lookup_struct, dip,
1450 dprt[0] = get_dst_port(pkt[0], dst.u32[0], portid);
1451 dprt[1] = get_dst_port(pkt[1], dst.u32[1], portid);
1452 dprt[2] = get_dst_port(pkt[2], dst.u32[2], portid);
1453 dprt[3] = get_dst_port(pkt[3], dst.u32[3], portid);
1458 * Update source and destination MAC addresses in the ethernet header.
1459 * Perform RFC1812 checks and updates for IPV4 packets.
1462 processx4_step3(struct rte_mbuf *pkt[FWDSTEP], uint32_t dst_port[FWDSTEP])
1464 __m128i te[FWDSTEP];
1465 __m128i ve[FWDSTEP];
1466 __m128i *p[FWDSTEP];
1468 p[0] = rte_pktmbuf_mtod(pkt[0], __m128i *);
1469 p[1] = rte_pktmbuf_mtod(pkt[1], __m128i *);
1470 p[2] = rte_pktmbuf_mtod(pkt[2], __m128i *);
1471 p[3] = rte_pktmbuf_mtod(pkt[3], __m128i *);
1473 ve[0] = val_eth[dst_port[0]];
1474 te[0] = _mm_load_si128(p[0]);
1476 ve[1] = val_eth[dst_port[1]];
1477 te[1] = _mm_load_si128(p[1]);
1479 ve[2] = val_eth[dst_port[2]];
1480 te[2] = _mm_load_si128(p[2]);
1482 ve[3] = val_eth[dst_port[3]];
1483 te[3] = _mm_load_si128(p[3]);
1485 /* Update first 12 bytes, keep rest bytes intact. */
1486 te[0] = _mm_blend_epi16(te[0], ve[0], MASK_ETH);
1487 te[1] = _mm_blend_epi16(te[1], ve[1], MASK_ETH);
1488 te[2] = _mm_blend_epi16(te[2], ve[2], MASK_ETH);
1489 te[3] = _mm_blend_epi16(te[3], ve[3], MASK_ETH);
1491 _mm_store_si128(p[0], te[0]);
1492 _mm_store_si128(p[1], te[1]);
1493 _mm_store_si128(p[2], te[2]);
1494 _mm_store_si128(p[3], te[3]);
1496 rfc1812_process((struct ipv4_hdr *)((struct ether_hdr *)p[0] + 1),
1497 &dst_port[0], pkt[0]->packet_type);
1498 rfc1812_process((struct ipv4_hdr *)((struct ether_hdr *)p[1] + 1),
1499 &dst_port[1], pkt[1]->packet_type);
1500 rfc1812_process((struct ipv4_hdr *)((struct ether_hdr *)p[2] + 1),
1501 &dst_port[2], pkt[2]->packet_type);
1502 rfc1812_process((struct ipv4_hdr *)((struct ether_hdr *)p[3] + 1),
1503 &dst_port[3], pkt[3]->packet_type);
1507 * We group consecutive packets with the same destionation port into one burst.
1508 * To avoid extra latency this is done together with some other packet
1509 * processing, but after we made a final decision about packet's destination.
1510 * To do this we maintain:
1511 * pnum - array of number of consecutive packets with the same dest port for
1512 * each packet in the input burst.
1513 * lp - pointer to the last updated element in the pnum.
1514 * dlp - dest port value lp corresponds to.
1517 #define GRPSZ (1 << FWDSTEP)
1518 #define GRPMSK (GRPSZ - 1)
1520 #define GROUP_PORT_STEP(dlp, dcp, lp, pn, idx) do { \
1521 if (likely((dlp) == (dcp)[(idx)])) { \
1524 (dlp) = (dcp)[idx]; \
1525 (lp) = (pn) + (idx); \
1531 * Group consecutive packets with the same destination port in bursts of 4.
1532 * Suppose we have array of destionation ports:
1533 * dst_port[] = {a, b, c, d,, e, ... }
1534 * dp1 should contain: <a, b, c, d>, dp2: <b, c, d, e>.
1535 * We doing 4 comparisions at once and the result is 4 bit mask.
1536 * This mask is used as an index into prebuild array of pnum values.
1538 static inline uint16_t *
1539 port_groupx4(uint16_t pn[FWDSTEP + 1], uint16_t *lp, __m128i dp1, __m128i dp2)
1541 static const struct {
1542 uint64_t pnum; /* prebuild 4 values for pnum[]. */
1543 int32_t idx; /* index for new last updated elemnet. */
1544 uint16_t lpv; /* add value to the last updated element. */
1547 /* 0: a != b, b != c, c != d, d != e */
1548 .pnum = UINT64_C(0x0001000100010001),
1553 /* 1: a == b, b != c, c != d, d != e */
1554 .pnum = UINT64_C(0x0001000100010002),
1559 /* 2: a != b, b == c, c != d, d != e */
1560 .pnum = UINT64_C(0x0001000100020001),
1565 /* 3: a == b, b == c, c != d, d != e */
1566 .pnum = UINT64_C(0x0001000100020003),
1571 /* 4: a != b, b != c, c == d, d != e */
1572 .pnum = UINT64_C(0x0001000200010001),
1577 /* 5: a == b, b != c, c == d, d != e */
1578 .pnum = UINT64_C(0x0001000200010002),
1583 /* 6: a != b, b == c, c == d, d != e */
1584 .pnum = UINT64_C(0x0001000200030001),
1589 /* 7: a == b, b == c, c == d, d != e */
1590 .pnum = UINT64_C(0x0001000200030004),
1595 /* 8: a != b, b != c, c != d, d == e */
1596 .pnum = UINT64_C(0x0002000100010001),
1601 /* 9: a == b, b != c, c != d, d == e */
1602 .pnum = UINT64_C(0x0002000100010002),
1607 /* 0xa: a != b, b == c, c != d, d == e */
1608 .pnum = UINT64_C(0x0002000100020001),
1613 /* 0xb: a == b, b == c, c != d, d == e */
1614 .pnum = UINT64_C(0x0002000100020003),
1619 /* 0xc: a != b, b != c, c == d, d == e */
1620 .pnum = UINT64_C(0x0002000300010001),
1625 /* 0xd: a == b, b != c, c == d, d == e */
1626 .pnum = UINT64_C(0x0002000300010002),
1631 /* 0xe: a != b, b == c, c == d, d == e */
1632 .pnum = UINT64_C(0x0002000300040001),
1637 /* 0xf: a == b, b == c, c == d, d == e */
1638 .pnum = UINT64_C(0x0002000300040005),
1645 uint16_t u16[FWDSTEP + 1];
1647 } *pnum = (void *)pn;
1651 dp1 = _mm_cmpeq_epi16(dp1, dp2);
1652 dp1 = _mm_unpacklo_epi16(dp1, dp1);
1653 v = _mm_movemask_ps((__m128)dp1);
1655 /* update last port counter. */
1656 lp[0] += gptbl[v].lpv;
1658 /* if dest port value has changed. */
1660 lp = pnum->u16 + gptbl[v].idx;
1662 pnum->u64 = gptbl[v].pnum;
1668 #endif /* APP_LOOKUP_METHOD */
1671 process_burst(struct rte_mbuf *pkts_burst[MAX_PKT_BURST], int nb_rx,
1676 #if ((APP_LOOKUP_METHOD == APP_LOOKUP_LPM) && \
1677 (ENABLE_MULTI_BUFFER_OPTIMIZE == 1))
1681 uint32_t dst_port[MAX_PKT_BURST];
1682 __m128i dip[MAX_PKT_BURST / FWDSTEP];
1683 uint32_t ipv4_flag[MAX_PKT_BURST / FWDSTEP];
1684 uint16_t pnum[MAX_PKT_BURST + 1];
1688 #if (ENABLE_MULTI_BUFFER_OPTIMIZE == 1)
1689 #if (APP_LOOKUP_METHOD == APP_LOOKUP_EXACT_MATCH)
1692 * Send nb_rx - nb_rx%8 packets
1695 int32_t n = RTE_ALIGN_FLOOR(nb_rx, 8);
1697 for (j = 0; j < n; j += 8) {
1699 pkts_burst[j]->packet_type &
1700 pkts_burst[j+1]->packet_type &
1701 pkts_burst[j+2]->packet_type &
1702 pkts_burst[j+3]->packet_type &
1703 pkts_burst[j+4]->packet_type &
1704 pkts_burst[j+5]->packet_type &
1705 pkts_burst[j+6]->packet_type &
1706 pkts_burst[j+7]->packet_type;
1707 if (pkt_type & RTE_PTYPE_L3_IPV4) {
1708 simple_ipv4_fwd_8pkts(&pkts_burst[j], portid);
1709 } else if (pkt_type &
1710 RTE_PTYPE_L3_IPV6) {
1711 simple_ipv6_fwd_8pkts(&pkts_burst[j], portid);
1713 l3fwd_simple_forward(pkts_burst[j], portid);
1714 l3fwd_simple_forward(pkts_burst[j+1], portid);
1715 l3fwd_simple_forward(pkts_burst[j+2], portid);
1716 l3fwd_simple_forward(pkts_burst[j+3], portid);
1717 l3fwd_simple_forward(pkts_burst[j+4], portid);
1718 l3fwd_simple_forward(pkts_burst[j+5], portid);
1719 l3fwd_simple_forward(pkts_burst[j+6], portid);
1720 l3fwd_simple_forward(pkts_burst[j+7], portid);
1723 for (; j < nb_rx ; j++)
1724 l3fwd_simple_forward(pkts_burst[j], portid);
1726 #elif (APP_LOOKUP_METHOD == APP_LOOKUP_LPM)
1728 k = RTE_ALIGN_FLOOR(nb_rx, FWDSTEP);
1729 for (j = 0; j != k; j += FWDSTEP)
1730 processx4_step1(&pkts_burst[j], &dip[j / FWDSTEP],
1731 &ipv4_flag[j / FWDSTEP]);
1733 k = RTE_ALIGN_FLOOR(nb_rx, FWDSTEP);
1734 for (j = 0; j != k; j += FWDSTEP)
1735 processx4_step2(dip[j / FWDSTEP], ipv4_flag[j / FWDSTEP],
1736 portid, &pkts_burst[j], &dst_port[j]);
1739 * Finish packet processing and group consecutive
1740 * packets with the same destination port.
1742 k = RTE_ALIGN_FLOOR(nb_rx, FWDSTEP);
1749 processx4_step3(pkts_burst, dst_port);
1751 /* dp1: <d[0], d[1], d[2], d[3], ... > */
1752 dp1 = _mm_loadu_si128((__m128i *)dst_port);
1754 for (j = FWDSTEP; j != k; j += FWDSTEP) {
1755 processx4_step3(&pkts_burst[j], &dst_port[j]);
1759 * <d[j-3], d[j-2], d[j-1], d[j], ... >
1761 dp2 = _mm_loadu_si128(
1762 (__m128i *)&dst_port[j - FWDSTEP + 1]);
1763 lp = port_groupx4(&pnum[j - FWDSTEP], lp, dp1, dp2);
1767 * <d[j], d[j+1], d[j+2], d[j+3], ... >
1769 dp1 = _mm_srli_si128(dp2, (FWDSTEP - 1) *
1770 sizeof(dst_port[0]));
1774 * dp2: <d[j-3], d[j-2], d[j-1], d[j-1], ... >
1776 dp2 = _mm_shufflelo_epi16(dp1, 0xf9);
1777 lp = port_groupx4(&pnum[j - FWDSTEP], lp, dp1, dp2);
1780 * remove values added by the last repeated
1784 dlp = dst_port[j - 1];
1786 /* set dlp and lp to the never used values. */
1788 lp = pnum + MAX_PKT_BURST;
1791 /* Process up to last 3 packets one by one. */
1792 switch (nb_rx % FWDSTEP) {
1794 process_packet(pkts_burst[j], dst_port + j, portid);
1795 GROUP_PORT_STEP(dlp, dst_port, lp, pnum, j);
1798 process_packet(pkts_burst[j], dst_port + j, portid);
1799 GROUP_PORT_STEP(dlp, dst_port, lp, pnum, j);
1802 process_packet(pkts_burst[j], dst_port + j, portid);
1803 GROUP_PORT_STEP(dlp, dst_port, lp, pnum, j);
1808 * Send packets out, through destination port.
1809 * Consecuteve pacekts with the same destination port
1810 * are already grouped together.
1811 * If destination port for the packet equals BAD_PORT,
1812 * then free the packet without sending it out.
1814 for (j = 0; j < nb_rx; j += k) {
1822 if (likely(pn != BAD_PORT))
1823 send_packetsx4(pn, pkts_burst + j, k);
1825 for (m = j; m != j + k; m++)
1826 rte_pktmbuf_free(pkts_burst[m]);
1830 #endif /* APP_LOOKUP_METHOD */
1831 #else /* ENABLE_MULTI_BUFFER_OPTIMIZE == 0 */
1833 /* Prefetch first packets */
1834 for (j = 0; j < PREFETCH_OFFSET && j < nb_rx; j++)
1835 rte_prefetch0(rte_pktmbuf_mtod(pkts_burst[j], void *));
1837 /* Prefetch and forward already prefetched packets */
1838 for (j = 0; j < (nb_rx - PREFETCH_OFFSET); j++) {
1839 rte_prefetch0(rte_pktmbuf_mtod(pkts_burst[
1840 j + PREFETCH_OFFSET], void *));
1841 l3fwd_simple_forward(pkts_burst[j], portid, qconf);
1844 /* Forward remaining prefetched packets */
1845 for (; j < nb_rx; j++)
1846 l3fwd_simple_forward(pkts_burst[j], portid, qconf);
1848 #endif /* ENABLE_MULTI_BUFFER_OPTIMIZE */
1852 #if (APP_CPU_LOAD > 0)
1855 * CPU-load stats collector
1858 cpu_load_collector(__rte_unused void *arg) {
1861 uint64_t prev_tsc, diff_tsc, cur_tsc;
1862 uint64_t total[MAX_CPU] = { 0 };
1863 unsigned min_cpu = MAX_CPU;
1864 unsigned max_cpu = 0;
1869 unsigned int n_thread_per_cpu[MAX_CPU] = { 0 };
1870 struct thread_conf *thread_per_cpu[MAX_CPU][MAX_THREAD];
1872 struct thread_conf *thread_conf;
1874 const uint64_t interval_tsc = (rte_get_tsc_hz() + US_PER_S - 1) /
1875 US_PER_S * CPU_LOAD_TIMEOUT_US;
1879 * Wait for all threads
1882 printf("Waiting for %d rx threads and %d tx threads\n", n_rx_thread,
1885 while (rte_atomic16_read(&rx_counter) < n_rx_thread)
1888 while (rte_atomic16_read(&tx_counter) < n_tx_thread)
1891 for (i = 0; i < n_rx_thread; i++) {
1893 thread_conf = &rx_thread[i].conf;
1894 cpu_id = thread_conf->cpu_id;
1895 thread_per_cpu[cpu_id][n_thread_per_cpu[cpu_id]++] = thread_conf;
1897 if (cpu_id > max_cpu)
1899 if (cpu_id < min_cpu)
1902 for (i = 0; i < n_tx_thread; i++) {
1904 thread_conf = &tx_thread[i].conf;
1905 cpu_id = thread_conf->cpu_id;
1906 thread_per_cpu[cpu_id][n_thread_per_cpu[cpu_id]++] = thread_conf;
1908 if (thread_conf->cpu_id > max_cpu)
1909 max_cpu = thread_conf->cpu_id;
1910 if (thread_conf->cpu_id < min_cpu)
1911 min_cpu = thread_conf->cpu_id;
1917 for (i = min_cpu; i <= max_cpu; i++) {
1918 for (j = 0; j < MAX_CPU_COUNTER; j++) {
1919 for (k = 0; k < n_thread_per_cpu[i]; k++)
1920 if (thread_per_cpu[i][k]->busy[j]) {
1925 cpu_load.hits[j][i]++;
1937 cur_tsc = rte_rdtsc();
1939 diff_tsc = cur_tsc - prev_tsc;
1940 if (unlikely(diff_tsc > interval_tsc)) {
1944 printf("Cpu usage for %d rx threads and %d tx threads:\n\n",
1945 n_rx_thread, n_tx_thread);
1947 printf("cpu# proc%% poll%% overhead%%\n\n");
1949 for (i = min_cpu; i <= max_cpu; i++) {
1951 printf("CPU %d:", i);
1952 for (j = 0; j < MAX_CPU_COUNTER; j++) {
1953 printf("%7" PRIu64 "",
1954 cpu_load.hits[j][i] * 100 / cpu_load.counter);
1955 hits += cpu_load.hits[j][i];
1956 cpu_load.hits[j][i] = 0;
1958 printf("%7" PRIu64 "\n",
1959 100 - total[i] * 100 / cpu_load.counter);
1962 cpu_load.counter = 0;
1969 #endif /* APP_CPU_LOAD */
1972 * Null processing lthread loop
1974 * This loop is used to start empty scheduler on lcore.
1977 lthread_null(__rte_unused void *args)
1979 int lcore_id = rte_lcore_id();
1981 RTE_LOG(INFO, L3FWD, "Starting scheduler on lcore %d.\n", lcore_id);
1985 /* main processing loop */
1987 lthread_tx_per_ring(void *dummy)
1991 struct rte_ring *ring;
1992 struct thread_tx_conf *tx_conf;
1993 struct rte_mbuf *pkts_burst[MAX_PKT_BURST];
1994 struct lthread_cond *ready;
1996 tx_conf = (struct thread_tx_conf *)dummy;
1997 ring = tx_conf->ring;
1998 ready = *tx_conf->ready;
2000 lthread_set_data((void *)tx_conf);
2003 * Move this lthread to lcore
2005 lthread_set_affinity(tx_conf->conf.lcore_id);
2007 RTE_LOG(INFO, L3FWD, "entering main tx loop on lcore %u\n", rte_lcore_id());
2010 rte_atomic16_inc(&tx_counter);
2014 * Read packet from ring
2016 SET_CPU_BUSY(tx_conf, CPU_POLL);
2017 nb_rx = rte_ring_sc_dequeue_burst(ring, (void **)pkts_burst,
2019 SET_CPU_IDLE(tx_conf, CPU_POLL);
2022 SET_CPU_BUSY(tx_conf, CPU_PROCESS);
2023 portid = pkts_burst[0]->port;
2024 process_burst(pkts_burst, nb_rx, portid);
2025 SET_CPU_IDLE(tx_conf, CPU_PROCESS);
2028 lthread_cond_wait(ready, 0);
2034 * Main tx-lthreads spawner lthread.
2036 * This lthread is used to spawn one new lthread per ring from producers.
2040 lthread_tx(void *args)
2046 struct thread_tx_conf *tx_conf;
2048 tx_conf = (struct thread_tx_conf *)args;
2049 lthread_set_data((void *)tx_conf);
2052 * Move this lthread to the selected lcore
2054 lthread_set_affinity(tx_conf->conf.lcore_id);
2057 * Spawn tx readers (one per input ring)
2059 lthread_create(<, tx_conf->conf.lcore_id, lthread_tx_per_ring,
2062 lcore_id = rte_lcore_id();
2064 RTE_LOG(INFO, L3FWD, "Entering Tx main loop on lcore %u\n", lcore_id);
2066 tx_conf->conf.cpu_id = sched_getcpu();
2069 lthread_sleep(BURST_TX_DRAIN_US * 1000);
2072 * TX burst queue drain
2074 for (portid = 0; portid < RTE_MAX_ETHPORTS; portid++) {
2075 if (tx_conf->tx_mbufs[portid].len == 0)
2077 SET_CPU_BUSY(tx_conf, CPU_PROCESS);
2078 send_burst(tx_conf, tx_conf->tx_mbufs[portid].len, portid);
2079 SET_CPU_IDLE(tx_conf, CPU_PROCESS);
2080 tx_conf->tx_mbufs[portid].len = 0;
2087 lthread_rx(void *dummy)
2092 uint8_t portid, queueid;
2094 int len[RTE_MAX_LCORE] = { 0 };
2095 int old_len, new_len;
2096 struct rte_mbuf *pkts_burst[MAX_PKT_BURST];
2097 struct thread_rx_conf *rx_conf;
2099 rx_conf = (struct thread_rx_conf *)dummy;
2100 lthread_set_data((void *)rx_conf);
2103 * Move this lthread to lcore
2105 lthread_set_affinity(rx_conf->conf.lcore_id);
2107 if (rx_conf->n_rx_queue == 0) {
2108 RTE_LOG(INFO, L3FWD, "lcore %u has nothing to do\n", rte_lcore_id());
2112 RTE_LOG(INFO, L3FWD, "Entering main Rx loop on lcore %u\n", rte_lcore_id());
2114 for (i = 0; i < rx_conf->n_rx_queue; i++) {
2116 portid = rx_conf->rx_queue_list[i].port_id;
2117 queueid = rx_conf->rx_queue_list[i].queue_id;
2118 RTE_LOG(INFO, L3FWD, " -- lcoreid=%u portid=%hhu rxqueueid=%hhu\n",
2119 rte_lcore_id(), portid, queueid);
2123 * Init all condition variables (one per rx thread)
2125 for (i = 0; i < rx_conf->n_rx_queue; i++)
2126 lthread_cond_init(NULL, &rx_conf->ready[i], NULL);
2130 rx_conf->conf.cpu_id = sched_getcpu();
2131 rte_atomic16_inc(&rx_counter);
2135 * Read packet from RX queues
2137 for (i = 0; i < rx_conf->n_rx_queue; ++i) {
2138 portid = rx_conf->rx_queue_list[i].port_id;
2139 queueid = rx_conf->rx_queue_list[i].queue_id;
2141 SET_CPU_BUSY(rx_conf, CPU_POLL);
2142 nb_rx = rte_eth_rx_burst(portid, queueid, pkts_burst,
2144 SET_CPU_IDLE(rx_conf, CPU_POLL);
2147 worker_id = (worker_id + 1) % rx_conf->n_ring;
2148 old_len = len[worker_id];
2150 SET_CPU_BUSY(rx_conf, CPU_PROCESS);
2151 ret = rte_ring_sp_enqueue_burst(
2152 rx_conf->ring[worker_id],
2153 (void **) pkts_burst,
2156 new_len = old_len + ret;
2158 if (new_len >= BURST_SIZE) {
2159 lthread_cond_signal(rx_conf->ready[worker_id]);
2163 len[worker_id] = new_len;
2165 if (unlikely(ret < nb_rx)) {
2168 for (k = ret; k < nb_rx; k++) {
2169 struct rte_mbuf *m = pkts_burst[k];
2171 rte_pktmbuf_free(m);
2174 SET_CPU_IDLE(rx_conf, CPU_PROCESS);
2183 * Start scheduler with initial lthread on lcore
2185 * This lthread loop spawns all rx and tx lthreads on master lcore
2189 lthread_spawner(__rte_unused void *arg) {
2190 struct lthread *lt[MAX_THREAD];
2194 printf("Entering lthread_spawner\n");
2197 * Create producers (rx threads) on default lcore
2199 for (i = 0; i < n_rx_thread; i++) {
2200 rx_thread[i].conf.thread_id = i;
2201 lthread_create(<[n_thread], -1, lthread_rx,
2202 (void *)&rx_thread[i]);
2207 * Wait for all producers. Until some producers can be started on the same
2208 * scheduler as this lthread, yielding is required to let them to run and
2209 * prevent deadlock here.
2211 while (rte_atomic16_read(&rx_counter) < n_rx_thread)
2212 lthread_sleep(100000);
2215 * Create consumers (tx threads) on default lcore_id
2217 for (i = 0; i < n_tx_thread; i++) {
2218 tx_thread[i].conf.thread_id = i;
2219 lthread_create(<[n_thread], -1, lthread_tx,
2220 (void *)&tx_thread[i]);
2225 * Wait for all threads finished
2227 for (i = 0; i < n_thread; i++)
2228 lthread_join(lt[i], NULL);
2233 * Start master scheduler with initial lthread spawning rx and tx lthreads
2234 * (main_lthread_master).
2237 lthread_master_spawner(__rte_unused void *arg) {
2239 int lcore_id = rte_lcore_id();
2241 RTE_PER_LCORE(lcore_conf) = &lcore_conf[lcore_id];
2242 lthread_create(<, -1, lthread_spawner, NULL);
2249 * Start scheduler on lcore.
2252 sched_spawner(__rte_unused void *arg) {
2254 int lcore_id = rte_lcore_id();
2257 if (lcore_id == cpu_load_lcore_id) {
2258 cpu_load_collector(arg);
2261 #endif /* APP_CPU_LOAD */
2263 RTE_PER_LCORE(lcore_conf) = &lcore_conf[lcore_id];
2264 lthread_create(<, -1, lthread_null, NULL);
2270 /* main processing loop */
2272 pthread_tx(void *dummy)
2274 struct rte_mbuf *pkts_burst[MAX_PKT_BURST];
2275 uint64_t prev_tsc, diff_tsc, cur_tsc;
2278 struct thread_tx_conf *tx_conf;
2280 const uint64_t drain_tsc = (rte_get_tsc_hz() + US_PER_S - 1) /
2281 US_PER_S * BURST_TX_DRAIN_US;
2285 tx_conf = (struct thread_tx_conf *)dummy;
2287 RTE_LOG(INFO, L3FWD, "Entering main Tx loop on lcore %u\n", rte_lcore_id());
2289 tx_conf->conf.cpu_id = sched_getcpu();
2290 rte_atomic16_inc(&tx_counter);
2293 cur_tsc = rte_rdtsc();
2296 * TX burst queue drain
2298 diff_tsc = cur_tsc - prev_tsc;
2299 if (unlikely(diff_tsc > drain_tsc)) {
2302 * This could be optimized (use queueid instead of
2303 * portid), but it is not called so often
2305 SET_CPU_BUSY(tx_conf, CPU_PROCESS);
2306 for (portid = 0; portid < RTE_MAX_ETHPORTS; portid++) {
2307 if (tx_conf->tx_mbufs[portid].len == 0)
2309 send_burst(tx_conf, tx_conf->tx_mbufs[portid].len, portid);
2310 tx_conf->tx_mbufs[portid].len = 0;
2312 SET_CPU_IDLE(tx_conf, CPU_PROCESS);
2318 * Read packet from ring
2320 SET_CPU_BUSY(tx_conf, CPU_POLL);
2321 nb_rx = rte_ring_sc_dequeue_burst(tx_conf->ring,
2322 (void **)pkts_burst, MAX_PKT_BURST);
2323 SET_CPU_IDLE(tx_conf, CPU_POLL);
2325 if (unlikely(nb_rx == 0)) {
2330 SET_CPU_BUSY(tx_conf, CPU_PROCESS);
2331 portid = pkts_burst[0]->port;
2332 process_burst(pkts_burst, nb_rx, portid);
2333 SET_CPU_IDLE(tx_conf, CPU_PROCESS);
2339 pthread_rx(void *dummy)
2346 uint8_t portid, queueid;
2347 struct rte_mbuf *pkts_burst[MAX_PKT_BURST];
2349 struct thread_rx_conf *rx_conf;
2351 lcore_id = rte_lcore_id();
2352 rx_conf = (struct thread_rx_conf *)dummy;
2354 if (rx_conf->n_rx_queue == 0) {
2355 RTE_LOG(INFO, L3FWD, "lcore %u has nothing to do\n", lcore_id);
2359 RTE_LOG(INFO, L3FWD, "entering main rx loop on lcore %u\n", lcore_id);
2361 for (i = 0; i < rx_conf->n_rx_queue; i++) {
2363 portid = rx_conf->rx_queue_list[i].port_id;
2364 queueid = rx_conf->rx_queue_list[i].queue_id;
2365 RTE_LOG(INFO, L3FWD, " -- lcoreid=%u portid=%hhu rxqueueid=%hhu\n",
2366 lcore_id, portid, queueid);
2370 rx_conf->conf.cpu_id = sched_getcpu();
2371 rte_atomic16_inc(&rx_counter);
2375 * Read packet from RX queues
2377 for (i = 0; i < rx_conf->n_rx_queue; ++i) {
2378 portid = rx_conf->rx_queue_list[i].port_id;
2379 queueid = rx_conf->rx_queue_list[i].queue_id;
2381 SET_CPU_BUSY(rx_conf, CPU_POLL);
2382 nb_rx = rte_eth_rx_burst(portid, queueid, pkts_burst,
2384 SET_CPU_IDLE(rx_conf, CPU_POLL);
2391 SET_CPU_BUSY(rx_conf, CPU_PROCESS);
2392 worker_id = (worker_id + 1) % rx_conf->n_ring;
2393 n = rte_ring_sp_enqueue_burst(rx_conf->ring[worker_id],
2394 (void **)pkts_burst, nb_rx);
2396 if (unlikely(n != nb_rx)) {
2399 for (k = n; k < nb_rx; k++) {
2400 struct rte_mbuf *m = pkts_burst[k];
2402 rte_pktmbuf_free(m);
2406 SET_CPU_IDLE(rx_conf, CPU_PROCESS);
2416 pthread_run(__rte_unused void *arg) {
2417 int lcore_id = rte_lcore_id();
2420 for (i = 0; i < n_rx_thread; i++)
2421 if (rx_thread[i].conf.lcore_id == lcore_id) {
2422 printf("Start rx thread on %d...\n", lcore_id);
2423 RTE_PER_LCORE(lcore_conf) = &lcore_conf[lcore_id];
2424 RTE_PER_LCORE(lcore_conf)->data = (void *)&rx_thread[i];
2425 pthread_rx((void *)&rx_thread[i]);
2429 for (i = 0; i < n_tx_thread; i++)
2430 if (tx_thread[i].conf.lcore_id == lcore_id) {
2431 printf("Start tx thread on %d...\n", lcore_id);
2432 RTE_PER_LCORE(lcore_conf) = &lcore_conf[lcore_id];
2433 RTE_PER_LCORE(lcore_conf)->data = (void *)&tx_thread[i];
2434 pthread_tx((void *)&tx_thread[i]);
2439 if (lcore_id == cpu_load_lcore_id)
2440 cpu_load_collector(arg);
2441 #endif /* APP_CPU_LOAD */
2447 check_lcore_params(void)
2449 uint8_t queue, lcore;
2453 for (i = 0; i < nb_rx_thread_params; ++i) {
2454 queue = rx_thread_params[i].queue_id;
2455 if (queue >= MAX_RX_QUEUE_PER_PORT) {
2456 printf("invalid queue number: %hhu\n", queue);
2459 lcore = rx_thread_params[i].lcore_id;
2460 if (!rte_lcore_is_enabled(lcore)) {
2461 printf("error: lcore %hhu is not enabled in lcore mask\n", lcore);
2464 socketid = rte_lcore_to_socket_id(lcore);
2465 if ((socketid != 0) && (numa_on == 0))
2466 printf("warning: lcore %hhu is on socket %d with numa off\n",
2473 check_port_config(const unsigned nb_ports)
2478 for (i = 0; i < nb_rx_thread_params; ++i) {
2479 portid = rx_thread_params[i].port_id;
2480 if ((enabled_port_mask & (1 << portid)) == 0) {
2481 printf("port %u is not enabled in port mask\n", portid);
2484 if (portid >= nb_ports) {
2485 printf("port %u is not present on the board\n", portid);
2493 get_port_n_rx_queues(const uint8_t port)
2498 for (i = 0; i < nb_rx_thread_params; ++i)
2499 if (rx_thread_params[i].port_id == port &&
2500 rx_thread_params[i].queue_id > queue)
2501 queue = rx_thread_params[i].queue_id;
2503 return (uint8_t)(++queue);
2510 struct thread_rx_conf *rx_conf;
2511 struct thread_tx_conf *tx_conf;
2512 unsigned rx_thread_id, tx_thread_id;
2514 struct rte_ring *ring = NULL;
2516 for (tx_thread_id = 0; tx_thread_id < n_tx_thread; tx_thread_id++) {
2518 tx_conf = &tx_thread[tx_thread_id];
2520 printf("Connecting tx-thread %d with rx-thread %d\n", tx_thread_id,
2521 tx_conf->conf.thread_id);
2523 rx_thread_id = tx_conf->conf.thread_id;
2524 if (rx_thread_id > n_tx_thread) {
2525 printf("connection from tx-thread %u to rx-thread %u fails "
2526 "(rx-thread not defined)\n", tx_thread_id, rx_thread_id);
2530 rx_conf = &rx_thread[rx_thread_id];
2531 socket_io = rte_lcore_to_socket_id(rx_conf->conf.lcore_id);
2533 snprintf(name, sizeof(name), "app_ring_s%u_rx%u_tx%u",
2534 socket_io, rx_thread_id, tx_thread_id);
2536 ring = rte_ring_create(name, 1024 * 4, socket_io,
2537 RING_F_SP_ENQ | RING_F_SC_DEQ);
2540 rte_panic("Cannot create ring to connect rx-thread %u "
2541 "with tx-thread %u\n", rx_thread_id, tx_thread_id);
2544 rx_conf->ring[rx_conf->n_ring] = ring;
2546 tx_conf->ring = ring;
2547 tx_conf->ready = &rx_conf->ready[rx_conf->n_ring];
2555 init_rx_queues(void)
2557 uint16_t i, nb_rx_queue;
2562 for (i = 0; i < nb_rx_thread_params; ++i) {
2563 thread = rx_thread_params[i].thread_id;
2564 nb_rx_queue = rx_thread[thread].n_rx_queue;
2566 if (nb_rx_queue >= MAX_RX_QUEUE_PER_LCORE) {
2567 printf("error: too many queues (%u) for thread: %u\n",
2568 (unsigned)nb_rx_queue + 1, (unsigned)thread);
2572 rx_thread[thread].conf.thread_id = thread;
2573 rx_thread[thread].conf.lcore_id = rx_thread_params[i].lcore_id;
2574 rx_thread[thread].rx_queue_list[nb_rx_queue].port_id =
2575 rx_thread_params[i].port_id;
2576 rx_thread[thread].rx_queue_list[nb_rx_queue].queue_id =
2577 rx_thread_params[i].queue_id;
2578 rx_thread[thread].n_rx_queue++;
2580 if (thread >= n_rx_thread)
2581 n_rx_thread = thread + 1;
2588 init_tx_threads(void)
2593 for (i = 0; i < nb_tx_thread_params; ++i) {
2594 tx_thread[n_tx_thread].conf.thread_id = tx_thread_params[i].thread_id;
2595 tx_thread[n_tx_thread].conf.lcore_id = tx_thread_params[i].lcore_id;
2603 print_usage(const char *prgname)
2605 printf("%s [EAL options] -- -p PORTMASK -P"
2606 " [--rx (port,queue,lcore,thread)[,(port,queue,lcore,thread]]"
2607 " [--tx (lcore,thread)[,(lcore,thread]]"
2608 " [--enable-jumbo [--max-pkt-len PKTLEN]]\n"
2609 " -p PORTMASK: hexadecimal bitmask of ports to configure\n"
2610 " -P : enable promiscuous mode\n"
2611 " --rx (port,queue,lcore,thread): rx queues configuration\n"
2612 " --tx (lcore,thread): tx threads configuration\n"
2613 " --stat-lcore LCORE: use lcore for stat collector\n"
2614 " --eth-dest=X,MM:MM:MM:MM:MM:MM: optional, ethernet destination for port X\n"
2615 " --no-numa: optional, disable numa awareness\n"
2616 " --ipv6: optional, specify it if running ipv6 packets\n"
2617 " --enable-jumbo: enable jumbo frame"
2618 " which max packet len is PKTLEN in decimal (64-9600)\n"
2619 " --hash-entry-num: specify the hash entry number in hexadecimal to be setup\n"
2620 " --no-lthreads: turn off lthread model\n",
2624 static int parse_max_pkt_len(const char *pktlen)
2629 /* parse decimal string */
2630 len = strtoul(pktlen, &end, 10);
2631 if ((pktlen[0] == '\0') || (end == NULL) || (*end != '\0'))
2641 parse_portmask(const char *portmask)
2646 /* parse hexadecimal string */
2647 pm = strtoul(portmask, &end, 16);
2648 if ((portmask[0] == '\0') || (end == NULL) || (*end != '\0'))
2657 #if (APP_LOOKUP_METHOD == APP_LOOKUP_EXACT_MATCH)
2659 parse_hash_entry_number(const char *hash_entry_num)
2662 unsigned long hash_en;
2664 /* parse hexadecimal string */
2665 hash_en = strtoul(hash_entry_num, &end, 16);
2666 if ((hash_entry_num[0] == '\0') || (end == NULL) || (*end != '\0'))
2677 parse_rx_config(const char *q_arg)
2680 const char *p, *p0 = q_arg;
2689 unsigned long int_fld[_NUM_FLD];
2690 char *str_fld[_NUM_FLD];
2694 nb_rx_thread_params = 0;
2696 while ((p = strchr(p0, '(')) != NULL) {
2698 p0 = strchr(p, ')');
2703 if (size >= sizeof(s))
2706 snprintf(s, sizeof(s), "%.*s", size, p);
2707 if (rte_strsplit(s, sizeof(s), str_fld, _NUM_FLD, ',') != _NUM_FLD)
2709 for (i = 0; i < _NUM_FLD; i++) {
2711 int_fld[i] = strtoul(str_fld[i], &end, 0);
2712 if (errno != 0 || end == str_fld[i] || int_fld[i] > 255)
2715 if (nb_rx_thread_params >= MAX_LCORE_PARAMS) {
2716 printf("exceeded max number of rx params: %hu\n",
2717 nb_rx_thread_params);
2720 rx_thread_params_array[nb_rx_thread_params].port_id =
2721 (uint8_t)int_fld[FLD_PORT];
2722 rx_thread_params_array[nb_rx_thread_params].queue_id =
2723 (uint8_t)int_fld[FLD_QUEUE];
2724 rx_thread_params_array[nb_rx_thread_params].lcore_id =
2725 (uint8_t)int_fld[FLD_LCORE];
2726 rx_thread_params_array[nb_rx_thread_params].thread_id =
2727 (uint8_t)int_fld[FLD_THREAD];
2728 ++nb_rx_thread_params;
2730 rx_thread_params = rx_thread_params_array;
2735 parse_tx_config(const char *q_arg)
2738 const char *p, *p0 = q_arg;
2745 unsigned long int_fld[_NUM_FLD];
2746 char *str_fld[_NUM_FLD];
2750 nb_tx_thread_params = 0;
2752 while ((p = strchr(p0, '(')) != NULL) {
2754 p0 = strchr(p, ')');
2759 if (size >= sizeof(s))
2762 snprintf(s, sizeof(s), "%.*s", size, p);
2763 if (rte_strsplit(s, sizeof(s), str_fld, _NUM_FLD, ',') != _NUM_FLD)
2765 for (i = 0; i < _NUM_FLD; i++) {
2767 int_fld[i] = strtoul(str_fld[i], &end, 0);
2768 if (errno != 0 || end == str_fld[i] || int_fld[i] > 255)
2771 if (nb_tx_thread_params >= MAX_LCORE_PARAMS) {
2772 printf("exceeded max number of tx params: %hu\n",
2773 nb_tx_thread_params);
2776 tx_thread_params_array[nb_tx_thread_params].lcore_id =
2777 (uint8_t)int_fld[FLD_LCORE];
2778 tx_thread_params_array[nb_tx_thread_params].thread_id =
2779 (uint8_t)int_fld[FLD_THREAD];
2780 ++nb_tx_thread_params;
2782 tx_thread_params = tx_thread_params_array;
2787 #if (APP_CPU_LOAD > 0)
2789 parse_stat_lcore(const char *stat_lcore)
2792 unsigned long lcore_id;
2794 lcore_id = strtoul(stat_lcore, &end, 10);
2795 if ((stat_lcore[0] == '\0') || (end == NULL) || (*end != '\0'))
2803 parse_eth_dest(const char *optarg)
2807 uint8_t c, *dest, peer_addr[6];
2810 portid = strtoul(optarg, &port_end, 10);
2811 if (errno != 0 || port_end == optarg || *port_end++ != ',')
2812 rte_exit(EXIT_FAILURE,
2813 "Invalid eth-dest: %s", optarg);
2814 if (portid >= RTE_MAX_ETHPORTS)
2815 rte_exit(EXIT_FAILURE,
2816 "eth-dest: port %d >= RTE_MAX_ETHPORTS(%d)\n",
2817 portid, RTE_MAX_ETHPORTS);
2819 if (cmdline_parse_etheraddr(NULL, port_end,
2820 &peer_addr, sizeof(peer_addr)) < 0)
2821 rte_exit(EXIT_FAILURE,
2822 "Invalid ethernet address: %s\n",
2824 dest = (uint8_t *)&dest_eth_addr[portid];
2825 for (c = 0; c < 6; c++)
2826 dest[c] = peer_addr[c];
2827 *(uint64_t *)(val_eth + portid) = dest_eth_addr[portid];
2830 #define CMD_LINE_OPT_RX_CONFIG "rx"
2831 #define CMD_LINE_OPT_TX_CONFIG "tx"
2832 #define CMD_LINE_OPT_STAT_LCORE "stat-lcore"
2833 #define CMD_LINE_OPT_ETH_DEST "eth-dest"
2834 #define CMD_LINE_OPT_NO_NUMA "no-numa"
2835 #define CMD_LINE_OPT_IPV6 "ipv6"
2836 #define CMD_LINE_OPT_ENABLE_JUMBO "enable-jumbo"
2837 #define CMD_LINE_OPT_HASH_ENTRY_NUM "hash-entry-num"
2838 #define CMD_LINE_OPT_NO_LTHREADS "no-lthreads"
2840 /* Parse the argument given in the command line of the application */
2842 parse_args(int argc, char **argv)
2847 char *prgname = argv[0];
2848 static struct option lgopts[] = {
2849 {CMD_LINE_OPT_RX_CONFIG, 1, 0, 0},
2850 {CMD_LINE_OPT_TX_CONFIG, 1, 0, 0},
2851 {CMD_LINE_OPT_STAT_LCORE, 1, 0, 0},
2852 {CMD_LINE_OPT_ETH_DEST, 1, 0, 0},
2853 {CMD_LINE_OPT_NO_NUMA, 0, 0, 0},
2854 {CMD_LINE_OPT_IPV6, 0, 0, 0},
2855 {CMD_LINE_OPT_ENABLE_JUMBO, 0, 0, 0},
2856 {CMD_LINE_OPT_HASH_ENTRY_NUM, 1, 0, 0},
2857 {CMD_LINE_OPT_NO_LTHREADS, 0, 0, 0},
2863 while ((opt = getopt_long(argc, argvopt, "p:P",
2864 lgopts, &option_index)) != EOF) {
2869 enabled_port_mask = parse_portmask(optarg);
2870 if (enabled_port_mask == 0) {
2871 printf("invalid portmask\n");
2872 print_usage(prgname);
2877 printf("Promiscuous mode selected\n");
2883 if (!strncmp(lgopts[option_index].name, CMD_LINE_OPT_RX_CONFIG,
2884 sizeof(CMD_LINE_OPT_RX_CONFIG))) {
2885 ret = parse_rx_config(optarg);
2887 printf("invalid rx-config\n");
2888 print_usage(prgname);
2893 if (!strncmp(lgopts[option_index].name, CMD_LINE_OPT_TX_CONFIG,
2894 sizeof(CMD_LINE_OPT_TX_CONFIG))) {
2895 ret = parse_tx_config(optarg);
2897 printf("invalid tx-config\n");
2898 print_usage(prgname);
2903 #if (APP_CPU_LOAD > 0)
2904 if (!strncmp(lgopts[option_index].name, CMD_LINE_OPT_STAT_LCORE,
2905 sizeof(CMD_LINE_OPT_STAT_LCORE))) {
2906 cpu_load_lcore_id = parse_stat_lcore(optarg);
2910 if (!strncmp(lgopts[option_index].name, CMD_LINE_OPT_ETH_DEST,
2911 sizeof(CMD_LINE_OPT_ETH_DEST)))
2912 parse_eth_dest(optarg);
2914 if (!strncmp(lgopts[option_index].name, CMD_LINE_OPT_NO_NUMA,
2915 sizeof(CMD_LINE_OPT_NO_NUMA))) {
2916 printf("numa is disabled\n");
2920 #if (APP_LOOKUP_METHOD == APP_LOOKUP_EXACT_MATCH)
2921 if (!strncmp(lgopts[option_index].name, CMD_LINE_OPT_IPV6,
2922 sizeof(CMD_LINE_OPT_IPV6))) {
2923 printf("ipv6 is specified\n");
2928 if (!strncmp(lgopts[option_index].name, CMD_LINE_OPT_NO_LTHREADS,
2929 sizeof(CMD_LINE_OPT_NO_LTHREADS))) {
2930 printf("l-threads model is disabled\n");
2934 if (!strncmp(lgopts[option_index].name, CMD_LINE_OPT_ENABLE_JUMBO,
2935 sizeof(CMD_LINE_OPT_ENABLE_JUMBO))) {
2936 struct option lenopts = {"max-pkt-len", required_argument, 0,
2939 printf("jumbo frame is enabled - disabling simple TX path\n");
2940 port_conf.rxmode.jumbo_frame = 1;
2942 /* if no max-pkt-len set, use the default value ETHER_MAX_LEN */
2943 if (0 == getopt_long(argc, argvopt, "", &lenopts,
2946 ret = parse_max_pkt_len(optarg);
2947 if ((ret < 64) || (ret > MAX_JUMBO_PKT_LEN)) {
2948 printf("invalid packet length\n");
2949 print_usage(prgname);
2952 port_conf.rxmode.max_rx_pkt_len = ret;
2954 printf("set jumbo frame max packet length to %u\n",
2955 (unsigned int)port_conf.rxmode.max_rx_pkt_len);
2957 #if (APP_LOOKUP_METHOD == APP_LOOKUP_EXACT_MATCH)
2958 if (!strncmp(lgopts[option_index].name, CMD_LINE_OPT_HASH_ENTRY_NUM,
2959 sizeof(CMD_LINE_OPT_HASH_ENTRY_NUM))) {
2960 ret = parse_hash_entry_number(optarg);
2961 if ((ret > 0) && (ret <= L3FWD_HASH_ENTRIES)) {
2962 hash_entry_number = ret;
2964 printf("invalid hash entry number\n");
2965 print_usage(prgname);
2973 print_usage(prgname);
2979 argv[optind-1] = prgname;
2982 optind = 0; /* reset getopt lib */
2987 print_ethaddr(const char *name, const struct ether_addr *eth_addr)
2989 char buf[ETHER_ADDR_FMT_SIZE];
2991 ether_format_addr(buf, ETHER_ADDR_FMT_SIZE, eth_addr);
2992 printf("%s%s", name, buf);
2995 #if (APP_LOOKUP_METHOD == APP_LOOKUP_EXACT_MATCH)
2997 static void convert_ipv4_5tuple(struct ipv4_5tuple *key1,
2998 union ipv4_5tuple_host *key2)
3000 key2->ip_dst = rte_cpu_to_be_32(key1->ip_dst);
3001 key2->ip_src = rte_cpu_to_be_32(key1->ip_src);
3002 key2->port_dst = rte_cpu_to_be_16(key1->port_dst);
3003 key2->port_src = rte_cpu_to_be_16(key1->port_src);
3004 key2->proto = key1->proto;
3009 static void convert_ipv6_5tuple(struct ipv6_5tuple *key1,
3010 union ipv6_5tuple_host *key2)
3014 for (i = 0; i < 16; i++) {
3015 key2->ip_dst[i] = key1->ip_dst[i];
3016 key2->ip_src[i] = key1->ip_src[i];
3018 key2->port_dst = rte_cpu_to_be_16(key1->port_dst);
3019 key2->port_src = rte_cpu_to_be_16(key1->port_src);
3020 key2->proto = key1->proto;
3026 #define BYTE_VALUE_MAX 256
3027 #define ALL_32_BITS 0xffffffff
3028 #define BIT_8_TO_15 0x0000ff00
3030 populate_ipv4_few_flow_into_table(const struct rte_hash *h)
3034 uint32_t array_len = RTE_DIM(ipv4_l3fwd_route_array);
3036 mask0 = _mm_set_epi32(ALL_32_BITS, ALL_32_BITS, ALL_32_BITS, BIT_8_TO_15);
3037 for (i = 0; i < array_len; i++) {
3038 struct ipv4_l3fwd_route entry;
3039 union ipv4_5tuple_host newkey;
3041 entry = ipv4_l3fwd_route_array[i];
3042 convert_ipv4_5tuple(&entry.key, &newkey);
3043 ret = rte_hash_add_key(h, (void *)&newkey);
3045 rte_exit(EXIT_FAILURE, "Unable to add entry %" PRIu32
3046 " to the l3fwd hash.\n", i);
3048 ipv4_l3fwd_out_if[ret] = entry.if_out;
3050 printf("Hash: Adding 0x%" PRIx32 " keys\n", array_len);
3053 #define BIT_16_TO_23 0x00ff0000
3055 populate_ipv6_few_flow_into_table(const struct rte_hash *h)
3059 uint32_t array_len = RTE_DIM(ipv6_l3fwd_route_array);
3061 mask1 = _mm_set_epi32(ALL_32_BITS, ALL_32_BITS, ALL_32_BITS, BIT_16_TO_23);
3062 mask2 = _mm_set_epi32(0, 0, ALL_32_BITS, ALL_32_BITS);
3063 for (i = 0; i < array_len; i++) {
3064 struct ipv6_l3fwd_route entry;
3065 union ipv6_5tuple_host newkey;
3067 entry = ipv6_l3fwd_route_array[i];
3068 convert_ipv6_5tuple(&entry.key, &newkey);
3069 ret = rte_hash_add_key(h, (void *)&newkey);
3071 rte_exit(EXIT_FAILURE, "Unable to add entry %" PRIu32
3072 " to the l3fwd hash.\n", i);
3074 ipv6_l3fwd_out_if[ret] = entry.if_out;
3076 printf("Hash: Adding 0x%" PRIx32 "keys\n", array_len);
3079 #define NUMBER_PORT_USED 4
3081 populate_ipv4_many_flow_into_table(const struct rte_hash *h,
3082 unsigned int nr_flow)
3086 mask0 = _mm_set_epi32(ALL_32_BITS, ALL_32_BITS, ALL_32_BITS, BIT_8_TO_15);
3088 for (i = 0; i < nr_flow; i++) {
3089 struct ipv4_l3fwd_route entry;
3090 union ipv4_5tuple_host newkey;
3091 uint8_t a = (uint8_t)((i / NUMBER_PORT_USED) % BYTE_VALUE_MAX);
3092 uint8_t b = (uint8_t)(((i / NUMBER_PORT_USED) / BYTE_VALUE_MAX) %
3094 uint8_t c = (uint8_t)((i / NUMBER_PORT_USED) / (BYTE_VALUE_MAX *
3096 /* Create the ipv4 exact match flow */
3097 memset(&entry, 0, sizeof(entry));
3098 switch (i & (NUMBER_PORT_USED - 1)) {
3100 entry = ipv4_l3fwd_route_array[0];
3101 entry.key.ip_dst = IPv4(101, c, b, a);
3104 entry = ipv4_l3fwd_route_array[1];
3105 entry.key.ip_dst = IPv4(201, c, b, a);
3108 entry = ipv4_l3fwd_route_array[2];
3109 entry.key.ip_dst = IPv4(111, c, b, a);
3112 entry = ipv4_l3fwd_route_array[3];
3113 entry.key.ip_dst = IPv4(211, c, b, a);
3116 convert_ipv4_5tuple(&entry.key, &newkey);
3117 int32_t ret = rte_hash_add_key(h, (void *)&newkey);
3120 rte_exit(EXIT_FAILURE, "Unable to add entry %u\n", i);
3122 ipv4_l3fwd_out_if[ret] = (uint8_t)entry.if_out;
3125 printf("Hash: Adding 0x%x keys\n", nr_flow);
3129 populate_ipv6_many_flow_into_table(const struct rte_hash *h,
3130 unsigned int nr_flow)
3134 mask1 = _mm_set_epi32(ALL_32_BITS, ALL_32_BITS, ALL_32_BITS, BIT_16_TO_23);
3135 mask2 = _mm_set_epi32(0, 0, ALL_32_BITS, ALL_32_BITS);
3136 for (i = 0; i < nr_flow; i++) {
3137 struct ipv6_l3fwd_route entry;
3138 union ipv6_5tuple_host newkey;
3140 uint8_t a = (uint8_t) ((i / NUMBER_PORT_USED) % BYTE_VALUE_MAX);
3141 uint8_t b = (uint8_t) (((i / NUMBER_PORT_USED) / BYTE_VALUE_MAX) %
3143 uint8_t c = (uint8_t) ((i / NUMBER_PORT_USED) / (BYTE_VALUE_MAX *
3146 /* Create the ipv6 exact match flow */
3147 memset(&entry, 0, sizeof(entry));
3148 switch (i & (NUMBER_PORT_USED - 1)) {
3150 entry = ipv6_l3fwd_route_array[0];
3153 entry = ipv6_l3fwd_route_array[1];
3156 entry = ipv6_l3fwd_route_array[2];
3159 entry = ipv6_l3fwd_route_array[3];
3162 entry.key.ip_dst[13] = c;
3163 entry.key.ip_dst[14] = b;
3164 entry.key.ip_dst[15] = a;
3165 convert_ipv6_5tuple(&entry.key, &newkey);
3166 int32_t ret = rte_hash_add_key(h, (void *)&newkey);
3169 rte_exit(EXIT_FAILURE, "Unable to add entry %u\n", i);
3171 ipv6_l3fwd_out_if[ret] = (uint8_t) entry.if_out;
3174 printf("Hash: Adding 0x%x keys\n", nr_flow);
3178 setup_hash(int socketid)
3180 struct rte_hash_parameters ipv4_l3fwd_hash_params = {
3182 .entries = L3FWD_HASH_ENTRIES,
3183 .key_len = sizeof(union ipv4_5tuple_host),
3184 .hash_func = ipv4_hash_crc,
3185 .hash_func_init_val = 0,
3188 struct rte_hash_parameters ipv6_l3fwd_hash_params = {
3190 .entries = L3FWD_HASH_ENTRIES,
3191 .key_len = sizeof(union ipv6_5tuple_host),
3192 .hash_func = ipv6_hash_crc,
3193 .hash_func_init_val = 0,
3198 /* create ipv4 hash */
3199 snprintf(s, sizeof(s), "ipv4_l3fwd_hash_%d", socketid);
3200 ipv4_l3fwd_hash_params.name = s;
3201 ipv4_l3fwd_hash_params.socket_id = socketid;
3202 ipv4_l3fwd_lookup_struct[socketid] =
3203 rte_hash_create(&ipv4_l3fwd_hash_params);
3204 if (ipv4_l3fwd_lookup_struct[socketid] == NULL)
3205 rte_exit(EXIT_FAILURE, "Unable to create the l3fwd hash on "
3206 "socket %d\n", socketid);
3208 /* create ipv6 hash */
3209 snprintf(s, sizeof(s), "ipv6_l3fwd_hash_%d", socketid);
3210 ipv6_l3fwd_hash_params.name = s;
3211 ipv6_l3fwd_hash_params.socket_id = socketid;
3212 ipv6_l3fwd_lookup_struct[socketid] =
3213 rte_hash_create(&ipv6_l3fwd_hash_params);
3214 if (ipv6_l3fwd_lookup_struct[socketid] == NULL)
3215 rte_exit(EXIT_FAILURE, "Unable to create the l3fwd hash on "
3216 "socket %d\n", socketid);
3218 if (hash_entry_number != HASH_ENTRY_NUMBER_DEFAULT) {
3219 /* For testing hash matching with a large number of flows we
3220 * generate millions of IP 5-tuples with an incremented dst
3221 * address to initialize the hash table. */
3223 /* populate the ipv4 hash */
3224 populate_ipv4_many_flow_into_table(
3225 ipv4_l3fwd_lookup_struct[socketid], hash_entry_number);
3227 /* populate the ipv6 hash */
3228 populate_ipv6_many_flow_into_table(
3229 ipv6_l3fwd_lookup_struct[socketid], hash_entry_number);
3232 /* Use data in ipv4/ipv6 l3fwd lookup table directly to initialize
3235 /* populate the ipv4 hash */
3236 populate_ipv4_few_flow_into_table(
3237 ipv4_l3fwd_lookup_struct[socketid]);
3239 /* populate the ipv6 hash */
3240 populate_ipv6_few_flow_into_table(
3241 ipv6_l3fwd_lookup_struct[socketid]);
3247 #if (APP_LOOKUP_METHOD == APP_LOOKUP_LPM)
3249 setup_lpm(int socketid)
3251 struct rte_lpm6_config config;
3252 struct rte_lpm_config lpm_ipv4_config;
3257 /* create the LPM table */
3258 snprintf(s, sizeof(s), "IPV4_L3FWD_LPM_%d", socketid);
3259 lpm_ipv4_config.max_rules = IPV4_L3FWD_LPM_MAX_RULES;
3260 lpm_ipv4_config.number_tbl8s = 256;
3261 lpm_ipv4_config.flags = 0;
3262 ipv4_l3fwd_lookup_struct[socketid] =
3263 rte_lpm_create(s, socketid, &lpm_ipv4_config);
3264 if (ipv4_l3fwd_lookup_struct[socketid] == NULL)
3265 rte_exit(EXIT_FAILURE, "Unable to create the l3fwd LPM table"
3266 " on socket %d\n", socketid);
3268 /* populate the LPM table */
3269 for (i = 0; i < IPV4_L3FWD_NUM_ROUTES; i++) {
3271 /* skip unused ports */
3272 if ((1 << ipv4_l3fwd_route_array[i].if_out &
3273 enabled_port_mask) == 0)
3276 ret = rte_lpm_add(ipv4_l3fwd_lookup_struct[socketid],
3277 ipv4_l3fwd_route_array[i].ip,
3278 ipv4_l3fwd_route_array[i].depth,
3279 ipv4_l3fwd_route_array[i].if_out);
3282 rte_exit(EXIT_FAILURE, "Unable to add entry %u to the "
3283 "l3fwd LPM table on socket %d\n",
3287 printf("LPM: Adding route 0x%08x / %d (%d)\n",
3288 (unsigned)ipv4_l3fwd_route_array[i].ip,
3289 ipv4_l3fwd_route_array[i].depth,
3290 ipv4_l3fwd_route_array[i].if_out);
3293 /* create the LPM6 table */
3294 snprintf(s, sizeof(s), "IPV6_L3FWD_LPM_%d", socketid);
3296 config.max_rules = IPV6_L3FWD_LPM_MAX_RULES;
3297 config.number_tbl8s = IPV6_L3FWD_LPM_NUMBER_TBL8S;
3299 ipv6_l3fwd_lookup_struct[socketid] = rte_lpm6_create(s, socketid,
3301 if (ipv6_l3fwd_lookup_struct[socketid] == NULL)
3302 rte_exit(EXIT_FAILURE, "Unable to create the l3fwd LPM table"
3303 " on socket %d\n", socketid);
3305 /* populate the LPM table */
3306 for (i = 0; i < IPV6_L3FWD_NUM_ROUTES; i++) {
3308 /* skip unused ports */
3309 if ((1 << ipv6_l3fwd_route_array[i].if_out &
3310 enabled_port_mask) == 0)
3313 ret = rte_lpm6_add(ipv6_l3fwd_lookup_struct[socketid],
3314 ipv6_l3fwd_route_array[i].ip,
3315 ipv6_l3fwd_route_array[i].depth,
3316 ipv6_l3fwd_route_array[i].if_out);
3319 rte_exit(EXIT_FAILURE, "Unable to add entry %u to the "
3320 "l3fwd LPM table on socket %d\n",
3324 printf("LPM: Adding route %s / %d (%d)\n",
3326 ipv6_l3fwd_route_array[i].depth,
3327 ipv6_l3fwd_route_array[i].if_out);
3333 init_mem(unsigned nb_mbuf)
3335 struct lcore_conf *qconf;
3340 for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
3341 if (rte_lcore_is_enabled(lcore_id) == 0)
3345 socketid = rte_lcore_to_socket_id(lcore_id);
3349 if (socketid >= NB_SOCKETS) {
3350 rte_exit(EXIT_FAILURE, "Socket %d of lcore %u is out of range %d\n",
3351 socketid, lcore_id, NB_SOCKETS);
3353 if (pktmbuf_pool[socketid] == NULL) {
3354 snprintf(s, sizeof(s), "mbuf_pool_%d", socketid);
3355 pktmbuf_pool[socketid] =
3356 rte_pktmbuf_pool_create(s, nb_mbuf,
3357 MEMPOOL_CACHE_SIZE, 0,
3358 RTE_MBUF_DEFAULT_BUF_SIZE, socketid);
3359 if (pktmbuf_pool[socketid] == NULL)
3360 rte_exit(EXIT_FAILURE,
3361 "Cannot init mbuf pool on socket %d\n", socketid);
3363 printf("Allocated mbuf pool on socket %d\n", socketid);
3365 #if (APP_LOOKUP_METHOD == APP_LOOKUP_LPM)
3366 setup_lpm(socketid);
3368 setup_hash(socketid);
3371 qconf = &lcore_conf[lcore_id];
3372 qconf->ipv4_lookup_struct = ipv4_l3fwd_lookup_struct[socketid];
3373 qconf->ipv6_lookup_struct = ipv6_l3fwd_lookup_struct[socketid];
3378 /* Check the link status of all ports in up to 9s, and print them finally */
3380 check_all_ports_link_status(uint8_t port_num, uint32_t port_mask)
3382 #define CHECK_INTERVAL 100 /* 100ms */
3383 #define MAX_CHECK_TIME 90 /* 9s (90 * 100ms) in total */
3384 uint8_t portid, count, all_ports_up, print_flag = 0;
3385 struct rte_eth_link link;
3387 printf("\nChecking link status");
3389 for (count = 0; count <= MAX_CHECK_TIME; count++) {
3391 for (portid = 0; portid < port_num; portid++) {
3392 if ((port_mask & (1 << portid)) == 0)
3394 memset(&link, 0, sizeof(link));
3395 rte_eth_link_get_nowait(portid, &link);
3396 /* print link status if flag set */
3397 if (print_flag == 1) {
3398 if (link.link_status)
3399 printf("Port %d Link Up - speed %u "
3400 "Mbps - %s\n", (uint8_t)portid,
3401 (unsigned)link.link_speed,
3402 (link.link_duplex == ETH_LINK_FULL_DUPLEX) ?
3403 ("full-duplex") : ("half-duplex\n"));
3405 printf("Port %d Link Down\n",
3409 /* clear all_ports_up flag if any link down */
3410 if (link.link_status == 0) {
3415 /* after finally printing all link status, get out */
3416 if (print_flag == 1)
3419 if (all_ports_up == 0) {
3422 rte_delay_ms(CHECK_INTERVAL);
3425 /* set the print_flag if all ports up or timeout */
3426 if (all_ports_up == 1 || count == (MAX_CHECK_TIME - 1)) {
3434 main(int argc, char **argv)
3436 struct rte_eth_dev_info dev_info;
3437 struct rte_eth_txconf *txconf;
3443 uint32_t n_tx_queue, nb_lcores;
3444 uint8_t portid, nb_rx_queue, queue, socketid;
3447 ret = rte_eal_init(argc, argv);
3449 rte_exit(EXIT_FAILURE, "Invalid EAL parameters\n");
3453 /* pre-init dst MACs for all ports to 02:00:00:00:00:xx */
3454 for (portid = 0; portid < RTE_MAX_ETHPORTS; portid++) {
3455 dest_eth_addr[portid] = ETHER_LOCAL_ADMIN_ADDR +
3456 ((uint64_t)portid << 40);
3457 *(uint64_t *)(val_eth + portid) = dest_eth_addr[portid];
3460 /* parse application arguments (after the EAL ones) */
3461 ret = parse_args(argc, argv);
3463 rte_exit(EXIT_FAILURE, "Invalid L3FWD parameters\n");
3465 if (check_lcore_params() < 0)
3466 rte_exit(EXIT_FAILURE, "check_lcore_params failed\n");
3468 printf("Initializing rx-queues...\n");
3469 ret = init_rx_queues();
3471 rte_exit(EXIT_FAILURE, "init_rx_queues failed\n");
3473 printf("Initializing tx-threads...\n");
3474 ret = init_tx_threads();
3476 rte_exit(EXIT_FAILURE, "init_tx_threads failed\n");
3478 printf("Initializing rings...\n");
3479 ret = init_rx_rings();
3481 rte_exit(EXIT_FAILURE, "init_rx_rings failed\n");
3483 nb_ports = rte_eth_dev_count();
3484 if (nb_ports > RTE_MAX_ETHPORTS)
3485 nb_ports = RTE_MAX_ETHPORTS;
3487 if (check_port_config(nb_ports) < 0)
3488 rte_exit(EXIT_FAILURE, "check_port_config failed\n");
3490 nb_lcores = rte_lcore_count();
3492 /* initialize all ports */
3493 for (portid = 0; portid < nb_ports; portid++) {
3494 /* skip ports that are not enabled */
3495 if ((enabled_port_mask & (1 << portid)) == 0) {
3496 printf("\nSkipping disabled port %d\n", portid);
3501 printf("Initializing port %d ... ", portid);
3504 nb_rx_queue = get_port_n_rx_queues(portid);
3505 n_tx_queue = nb_lcores;
3506 if (n_tx_queue > MAX_TX_QUEUE_PER_PORT)
3507 n_tx_queue = MAX_TX_QUEUE_PER_PORT;
3508 printf("Creating queues: nb_rxq=%d nb_txq=%u... ",
3509 nb_rx_queue, (unsigned)n_tx_queue);
3510 ret = rte_eth_dev_configure(portid, nb_rx_queue,
3511 (uint16_t)n_tx_queue, &port_conf);
3513 rte_exit(EXIT_FAILURE, "Cannot configure device: err=%d, port=%d\n",
3516 rte_eth_macaddr_get(portid, &ports_eth_addr[portid]);
3517 print_ethaddr(" Address:", &ports_eth_addr[portid]);
3519 print_ethaddr("Destination:",
3520 (const struct ether_addr *)&dest_eth_addr[portid]);
3524 * prepare src MACs for each port.
3526 ether_addr_copy(&ports_eth_addr[portid],
3527 (struct ether_addr *)(val_eth + portid) + 1);
3530 ret = init_mem(NB_MBUF);
3532 rte_exit(EXIT_FAILURE, "init_mem failed\n");
3534 /* init one TX queue per couple (lcore,port) */
3536 for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
3537 if (rte_lcore_is_enabled(lcore_id) == 0)
3541 socketid = (uint8_t)rte_lcore_to_socket_id(lcore_id);
3545 printf("txq=%u,%d,%d ", lcore_id, queueid, socketid);
3548 rte_eth_dev_info_get(portid, &dev_info);
3549 txconf = &dev_info.default_txconf;
3550 if (port_conf.rxmode.jumbo_frame)
3551 txconf->txq_flags = 0;
3552 ret = rte_eth_tx_queue_setup(portid, queueid, nb_txd,
3555 rte_exit(EXIT_FAILURE, "rte_eth_tx_queue_setup: err=%d, "
3556 "port=%d\n", ret, portid);
3558 tx_thread[lcore_id].tx_queue_id[portid] = queueid;
3564 for (i = 0; i < n_rx_thread; i++) {
3565 lcore_id = rx_thread[i].conf.lcore_id;
3567 if (rte_lcore_is_enabled(lcore_id) == 0) {
3568 rte_exit(EXIT_FAILURE,
3569 "Cannot start Rx thread on lcore %u: lcore disabled\n",
3574 printf("\nInitializing rx queues for Rx thread %d on lcore %u ... ",
3578 /* init RX queues */
3579 for (queue = 0; queue < rx_thread[i].n_rx_queue; ++queue) {
3580 portid = rx_thread[i].rx_queue_list[queue].port_id;
3581 queueid = rx_thread[i].rx_queue_list[queue].queue_id;
3584 socketid = (uint8_t)rte_lcore_to_socket_id(lcore_id);
3588 printf("rxq=%d,%d,%d ", portid, queueid, socketid);
3591 ret = rte_eth_rx_queue_setup(portid, queueid, nb_rxd,
3594 pktmbuf_pool[socketid]);
3596 rte_exit(EXIT_FAILURE, "rte_eth_rx_queue_setup: err=%d, "
3597 "port=%d\n", ret, portid);
3604 for (portid = 0; portid < nb_ports; portid++) {
3605 if ((enabled_port_mask & (1 << portid)) == 0)
3609 ret = rte_eth_dev_start(portid);
3611 rte_exit(EXIT_FAILURE, "rte_eth_dev_start: err=%d, port=%d\n",
3615 * If enabled, put device in promiscuous mode.
3616 * This allows IO forwarding mode to forward packets
3617 * to itself through 2 cross-connected ports of the
3621 rte_eth_promiscuous_enable(portid);
3624 check_all_ports_link_status((uint8_t)nb_ports, enabled_port_mask);
3627 printf("Starting L-Threading Model\n");
3629 #if (APP_CPU_LOAD > 0)
3630 if (cpu_load_lcore_id > 0)
3631 /* Use one lcore for cpu load collector */
3635 lthread_num_schedulers_set(nb_lcores);
3636 rte_eal_mp_remote_launch(sched_spawner, NULL, SKIP_MASTER);
3637 lthread_master_spawner(NULL);
3640 printf("Starting P-Threading Model\n");
3641 /* launch per-lcore init on every lcore */
3642 rte_eal_mp_remote_launch(pthread_run, NULL, CALL_MASTER);
3643 RTE_LCORE_FOREACH_SLAVE(lcore_id) {
3644 if (rte_eal_wait_lcore(lcore_id) < 0)