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, uint16_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)
1340 struct ipv6_hdr *ipv6_hdr;
1341 struct ether_hdr *eth_hdr;
1343 if (RTE_ETH_IS_IPV4_HDR(pkt->packet_type)) {
1344 if (rte_lpm_lookup(RTE_PER_LCORE(lcore_conf)->ipv4_lookup_struct,
1345 dst_ipv4, &next_hop) != 0)
1347 } else if (RTE_ETH_IS_IPV6_HDR(pkt->packet_type)) {
1348 eth_hdr = rte_pktmbuf_mtod(pkt, struct ether_hdr *);
1349 ipv6_hdr = (struct ipv6_hdr *)(eth_hdr + 1);
1350 if (rte_lpm6_lookup(RTE_PER_LCORE(lcore_conf)->ipv6_lookup_struct,
1351 ipv6_hdr->dst_addr, &next_hop) != 0)
1361 process_packet(struct rte_mbuf *pkt, uint16_t *dst_port, uint8_t portid)
1363 struct ether_hdr *eth_hdr;
1364 struct ipv4_hdr *ipv4_hdr;
1369 eth_hdr = rte_pktmbuf_mtod(pkt, struct ether_hdr *);
1370 ipv4_hdr = (struct ipv4_hdr *)(eth_hdr + 1);
1372 dst_ipv4 = ipv4_hdr->dst_addr;
1373 dst_ipv4 = rte_be_to_cpu_32(dst_ipv4);
1374 dp = get_dst_port(pkt, dst_ipv4, portid);
1376 te = _mm_load_si128((__m128i *)eth_hdr);
1380 rfc1812_process(ipv4_hdr, dst_port, pkt->packet_type);
1382 te = _mm_blend_epi16(te, ve, MASK_ETH);
1383 _mm_store_si128((__m128i *)eth_hdr, te);
1387 * Read packet_type and destination IPV4 addresses from 4 mbufs.
1390 processx4_step1(struct rte_mbuf *pkt[FWDSTEP],
1392 uint32_t *ipv4_flag)
1394 struct ipv4_hdr *ipv4_hdr;
1395 struct ether_hdr *eth_hdr;
1396 uint32_t x0, x1, x2, x3;
1398 eth_hdr = rte_pktmbuf_mtod(pkt[0], struct ether_hdr *);
1399 ipv4_hdr = (struct ipv4_hdr *)(eth_hdr + 1);
1400 x0 = ipv4_hdr->dst_addr;
1401 ipv4_flag[0] = pkt[0]->packet_type & RTE_PTYPE_L3_IPV4;
1403 eth_hdr = rte_pktmbuf_mtod(pkt[1], struct ether_hdr *);
1404 ipv4_hdr = (struct ipv4_hdr *)(eth_hdr + 1);
1405 x1 = ipv4_hdr->dst_addr;
1406 ipv4_flag[0] &= pkt[1]->packet_type;
1408 eth_hdr = rte_pktmbuf_mtod(pkt[2], struct ether_hdr *);
1409 ipv4_hdr = (struct ipv4_hdr *)(eth_hdr + 1);
1410 x2 = ipv4_hdr->dst_addr;
1411 ipv4_flag[0] &= pkt[2]->packet_type;
1413 eth_hdr = rte_pktmbuf_mtod(pkt[3], struct ether_hdr *);
1414 ipv4_hdr = (struct ipv4_hdr *)(eth_hdr + 1);
1415 x3 = ipv4_hdr->dst_addr;
1416 ipv4_flag[0] &= pkt[3]->packet_type;
1418 dip[0] = _mm_set_epi32(x3, x2, x1, x0);
1422 * Lookup into LPM for destination port.
1423 * If lookup fails, use incoming port (portid) as destination port.
1426 processx4_step2(__m128i dip,
1429 struct rte_mbuf *pkt[FWDSTEP],
1430 uint16_t dprt[FWDSTEP])
1433 const __m128i bswap_mask = _mm_set_epi8(12, 13, 14, 15, 8, 9, 10, 11,
1434 4, 5, 6, 7, 0, 1, 2, 3);
1436 /* Byte swap 4 IPV4 addresses. */
1437 dip = _mm_shuffle_epi8(dip, bswap_mask);
1439 /* if all 4 packets are IPV4. */
1440 if (likely(ipv4_flag)) {
1441 rte_lpm_lookupx4(RTE_PER_LCORE(lcore_conf)->ipv4_lookup_struct, dip,
1445 dprt[0] = get_dst_port(pkt[0], dst.u32[0], portid);
1446 dprt[1] = get_dst_port(pkt[1], dst.u32[1], portid);
1447 dprt[2] = get_dst_port(pkt[2], dst.u32[2], portid);
1448 dprt[3] = get_dst_port(pkt[3], dst.u32[3], portid);
1453 * Update source and destination MAC addresses in the ethernet header.
1454 * Perform RFC1812 checks and updates for IPV4 packets.
1457 processx4_step3(struct rte_mbuf *pkt[FWDSTEP], uint16_t dst_port[FWDSTEP])
1459 __m128i te[FWDSTEP];
1460 __m128i ve[FWDSTEP];
1461 __m128i *p[FWDSTEP];
1463 p[0] = rte_pktmbuf_mtod(pkt[0], __m128i *);
1464 p[1] = rte_pktmbuf_mtod(pkt[1], __m128i *);
1465 p[2] = rte_pktmbuf_mtod(pkt[2], __m128i *);
1466 p[3] = rte_pktmbuf_mtod(pkt[3], __m128i *);
1468 ve[0] = val_eth[dst_port[0]];
1469 te[0] = _mm_load_si128(p[0]);
1471 ve[1] = val_eth[dst_port[1]];
1472 te[1] = _mm_load_si128(p[1]);
1474 ve[2] = val_eth[dst_port[2]];
1475 te[2] = _mm_load_si128(p[2]);
1477 ve[3] = val_eth[dst_port[3]];
1478 te[3] = _mm_load_si128(p[3]);
1480 /* Update first 12 bytes, keep rest bytes intact. */
1481 te[0] = _mm_blend_epi16(te[0], ve[0], MASK_ETH);
1482 te[1] = _mm_blend_epi16(te[1], ve[1], MASK_ETH);
1483 te[2] = _mm_blend_epi16(te[2], ve[2], MASK_ETH);
1484 te[3] = _mm_blend_epi16(te[3], ve[3], MASK_ETH);
1486 _mm_store_si128(p[0], te[0]);
1487 _mm_store_si128(p[1], te[1]);
1488 _mm_store_si128(p[2], te[2]);
1489 _mm_store_si128(p[3], te[3]);
1491 rfc1812_process((struct ipv4_hdr *)((struct ether_hdr *)p[0] + 1),
1492 &dst_port[0], pkt[0]->packet_type);
1493 rfc1812_process((struct ipv4_hdr *)((struct ether_hdr *)p[1] + 1),
1494 &dst_port[1], pkt[1]->packet_type);
1495 rfc1812_process((struct ipv4_hdr *)((struct ether_hdr *)p[2] + 1),
1496 &dst_port[2], pkt[2]->packet_type);
1497 rfc1812_process((struct ipv4_hdr *)((struct ether_hdr *)p[3] + 1),
1498 &dst_port[3], pkt[3]->packet_type);
1502 * We group consecutive packets with the same destionation port into one burst.
1503 * To avoid extra latency this is done together with some other packet
1504 * processing, but after we made a final decision about packet's destination.
1505 * To do this we maintain:
1506 * pnum - array of number of consecutive packets with the same dest port for
1507 * each packet in the input burst.
1508 * lp - pointer to the last updated element in the pnum.
1509 * dlp - dest port value lp corresponds to.
1512 #define GRPSZ (1 << FWDSTEP)
1513 #define GRPMSK (GRPSZ - 1)
1515 #define GROUP_PORT_STEP(dlp, dcp, lp, pn, idx) do { \
1516 if (likely((dlp) == (dcp)[(idx)])) { \
1519 (dlp) = (dcp)[idx]; \
1520 (lp) = (pn) + (idx); \
1526 * Group consecutive packets with the same destination port in bursts of 4.
1527 * Suppose we have array of destionation ports:
1528 * dst_port[] = {a, b, c, d,, e, ... }
1529 * dp1 should contain: <a, b, c, d>, dp2: <b, c, d, e>.
1530 * We doing 4 comparisions at once and the result is 4 bit mask.
1531 * This mask is used as an index into prebuild array of pnum values.
1533 static inline uint16_t *
1534 port_groupx4(uint16_t pn[FWDSTEP + 1], uint16_t *lp, __m128i dp1, __m128i dp2)
1536 static const struct {
1537 uint64_t pnum; /* prebuild 4 values for pnum[]. */
1538 int32_t idx; /* index for new last updated elemnet. */
1539 uint16_t lpv; /* add value to the last updated element. */
1542 /* 0: a != b, b != c, c != d, d != e */
1543 .pnum = UINT64_C(0x0001000100010001),
1548 /* 1: a == b, b != c, c != d, d != e */
1549 .pnum = UINT64_C(0x0001000100010002),
1554 /* 2: a != b, b == c, c != d, d != e */
1555 .pnum = UINT64_C(0x0001000100020001),
1560 /* 3: a == b, b == c, c != d, d != e */
1561 .pnum = UINT64_C(0x0001000100020003),
1566 /* 4: a != b, b != c, c == d, d != e */
1567 .pnum = UINT64_C(0x0001000200010001),
1572 /* 5: a == b, b != c, c == d, d != e */
1573 .pnum = UINT64_C(0x0001000200010002),
1578 /* 6: a != b, b == c, c == d, d != e */
1579 .pnum = UINT64_C(0x0001000200030001),
1584 /* 7: a == b, b == c, c == d, d != e */
1585 .pnum = UINT64_C(0x0001000200030004),
1590 /* 8: a != b, b != c, c != d, d == e */
1591 .pnum = UINT64_C(0x0002000100010001),
1596 /* 9: a == b, b != c, c != d, d == e */
1597 .pnum = UINT64_C(0x0002000100010002),
1602 /* 0xa: a != b, b == c, c != d, d == e */
1603 .pnum = UINT64_C(0x0002000100020001),
1608 /* 0xb: a == b, b == c, c != d, d == e */
1609 .pnum = UINT64_C(0x0002000100020003),
1614 /* 0xc: a != b, b != c, c == d, d == e */
1615 .pnum = UINT64_C(0x0002000300010001),
1620 /* 0xd: a == b, b != c, c == d, d == e */
1621 .pnum = UINT64_C(0x0002000300010002),
1626 /* 0xe: a != b, b == c, c == d, d == e */
1627 .pnum = UINT64_C(0x0002000300040001),
1632 /* 0xf: a == b, b == c, c == d, d == e */
1633 .pnum = UINT64_C(0x0002000300040005),
1640 uint16_t u16[FWDSTEP + 1];
1642 } *pnum = (void *)pn;
1646 dp1 = _mm_cmpeq_epi16(dp1, dp2);
1647 dp1 = _mm_unpacklo_epi16(dp1, dp1);
1648 v = _mm_movemask_ps((__m128)dp1);
1650 /* update last port counter. */
1651 lp[0] += gptbl[v].lpv;
1653 /* if dest port value has changed. */
1655 lp = pnum->u16 + gptbl[v].idx;
1657 pnum->u64 = gptbl[v].pnum;
1663 #endif /* APP_LOOKUP_METHOD */
1666 process_burst(struct rte_mbuf *pkts_burst[MAX_PKT_BURST], int nb_rx,
1671 #if ((APP_LOOKUP_METHOD == APP_LOOKUP_LPM) && \
1672 (ENABLE_MULTI_BUFFER_OPTIMIZE == 1))
1676 uint16_t dst_port[MAX_PKT_BURST];
1677 __m128i dip[MAX_PKT_BURST / FWDSTEP];
1678 uint32_t ipv4_flag[MAX_PKT_BURST / FWDSTEP];
1679 uint16_t pnum[MAX_PKT_BURST + 1];
1683 #if (ENABLE_MULTI_BUFFER_OPTIMIZE == 1)
1684 #if (APP_LOOKUP_METHOD == APP_LOOKUP_EXACT_MATCH)
1687 * Send nb_rx - nb_rx%8 packets
1690 int32_t n = RTE_ALIGN_FLOOR(nb_rx, 8);
1692 for (j = 0; j < n; j += 8) {
1694 pkts_burst[j]->packet_type &
1695 pkts_burst[j+1]->packet_type &
1696 pkts_burst[j+2]->packet_type &
1697 pkts_burst[j+3]->packet_type &
1698 pkts_burst[j+4]->packet_type &
1699 pkts_burst[j+5]->packet_type &
1700 pkts_burst[j+6]->packet_type &
1701 pkts_burst[j+7]->packet_type;
1702 if (pkt_type & RTE_PTYPE_L3_IPV4) {
1703 simple_ipv4_fwd_8pkts(&pkts_burst[j], portid);
1704 } else if (pkt_type &
1705 RTE_PTYPE_L3_IPV6) {
1706 simple_ipv6_fwd_8pkts(&pkts_burst[j], portid);
1708 l3fwd_simple_forward(pkts_burst[j], portid);
1709 l3fwd_simple_forward(pkts_burst[j+1], portid);
1710 l3fwd_simple_forward(pkts_burst[j+2], portid);
1711 l3fwd_simple_forward(pkts_burst[j+3], portid);
1712 l3fwd_simple_forward(pkts_burst[j+4], portid);
1713 l3fwd_simple_forward(pkts_burst[j+5], portid);
1714 l3fwd_simple_forward(pkts_burst[j+6], portid);
1715 l3fwd_simple_forward(pkts_burst[j+7], portid);
1718 for (; j < nb_rx ; j++)
1719 l3fwd_simple_forward(pkts_burst[j], portid);
1721 #elif (APP_LOOKUP_METHOD == APP_LOOKUP_LPM)
1723 k = RTE_ALIGN_FLOOR(nb_rx, FWDSTEP);
1724 for (j = 0; j != k; j += FWDSTEP)
1725 processx4_step1(&pkts_burst[j], &dip[j / FWDSTEP],
1726 &ipv4_flag[j / FWDSTEP]);
1728 k = RTE_ALIGN_FLOOR(nb_rx, FWDSTEP);
1729 for (j = 0; j != k; j += FWDSTEP)
1730 processx4_step2(dip[j / FWDSTEP], ipv4_flag[j / FWDSTEP],
1731 portid, &pkts_burst[j], &dst_port[j]);
1734 * Finish packet processing and group consecutive
1735 * packets with the same destination port.
1737 k = RTE_ALIGN_FLOOR(nb_rx, FWDSTEP);
1744 processx4_step3(pkts_burst, dst_port);
1746 /* dp1: <d[0], d[1], d[2], d[3], ... > */
1747 dp1 = _mm_loadu_si128((__m128i *)dst_port);
1749 for (j = FWDSTEP; j != k; j += FWDSTEP) {
1750 processx4_step3(&pkts_burst[j], &dst_port[j]);
1754 * <d[j-3], d[j-2], d[j-1], d[j], ... >
1756 dp2 = _mm_loadu_si128(
1757 (__m128i *)&dst_port[j - FWDSTEP + 1]);
1758 lp = port_groupx4(&pnum[j - FWDSTEP], lp, dp1, dp2);
1762 * <d[j], d[j+1], d[j+2], d[j+3], ... >
1764 dp1 = _mm_srli_si128(dp2, (FWDSTEP - 1) *
1765 sizeof(dst_port[0]));
1769 * dp2: <d[j-3], d[j-2], d[j-1], d[j-1], ... >
1771 dp2 = _mm_shufflelo_epi16(dp1, 0xf9);
1772 lp = port_groupx4(&pnum[j - FWDSTEP], lp, dp1, dp2);
1775 * remove values added by the last repeated
1779 dlp = dst_port[j - 1];
1781 /* set dlp and lp to the never used values. */
1783 lp = pnum + MAX_PKT_BURST;
1786 /* Process up to last 3 packets one by one. */
1787 switch (nb_rx % FWDSTEP) {
1789 process_packet(pkts_burst[j], dst_port + j, portid);
1790 GROUP_PORT_STEP(dlp, dst_port, lp, pnum, j);
1793 process_packet(pkts_burst[j], dst_port + j, portid);
1794 GROUP_PORT_STEP(dlp, dst_port, lp, pnum, j);
1797 process_packet(pkts_burst[j], dst_port + j, portid);
1798 GROUP_PORT_STEP(dlp, dst_port, lp, pnum, j);
1803 * Send packets out, through destination port.
1804 * Consecuteve pacekts with the same destination port
1805 * are already grouped together.
1806 * If destination port for the packet equals BAD_PORT,
1807 * then free the packet without sending it out.
1809 for (j = 0; j < nb_rx; j += k) {
1817 if (likely(pn != BAD_PORT))
1818 send_packetsx4(pn, pkts_burst + j, k);
1820 for (m = j; m != j + k; m++)
1821 rte_pktmbuf_free(pkts_burst[m]);
1825 #endif /* APP_LOOKUP_METHOD */
1826 #else /* ENABLE_MULTI_BUFFER_OPTIMIZE == 0 */
1828 /* Prefetch first packets */
1829 for (j = 0; j < PREFETCH_OFFSET && j < nb_rx; j++)
1830 rte_prefetch0(rte_pktmbuf_mtod(pkts_burst[j], void *));
1832 /* Prefetch and forward already prefetched packets */
1833 for (j = 0; j < (nb_rx - PREFETCH_OFFSET); j++) {
1834 rte_prefetch0(rte_pktmbuf_mtod(pkts_burst[
1835 j + PREFETCH_OFFSET], void *));
1836 l3fwd_simple_forward(pkts_burst[j], portid, qconf);
1839 /* Forward remaining prefetched packets */
1840 for (; j < nb_rx; j++)
1841 l3fwd_simple_forward(pkts_burst[j], portid, qconf);
1843 #endif /* ENABLE_MULTI_BUFFER_OPTIMIZE */
1847 #if (APP_CPU_LOAD > 0)
1850 * CPU-load stats collector
1853 cpu_load_collector(__rte_unused void *arg) {
1856 uint64_t prev_tsc, diff_tsc, cur_tsc;
1857 uint64_t total[MAX_CPU] = { 0 };
1858 unsigned min_cpu = MAX_CPU;
1859 unsigned max_cpu = 0;
1864 unsigned int n_thread_per_cpu[MAX_CPU] = { 0 };
1865 struct thread_conf *thread_per_cpu[MAX_CPU][MAX_THREAD];
1867 struct thread_conf *thread_conf;
1869 const uint64_t interval_tsc = (rte_get_tsc_hz() + US_PER_S - 1) /
1870 US_PER_S * CPU_LOAD_TIMEOUT_US;
1874 * Wait for all threads
1877 printf("Waiting for %d rx threads and %d tx threads\n", n_rx_thread,
1880 while (rte_atomic16_read(&rx_counter) < n_rx_thread)
1883 while (rte_atomic16_read(&tx_counter) < n_tx_thread)
1886 for (i = 0; i < n_rx_thread; i++) {
1888 thread_conf = &rx_thread[i].conf;
1889 cpu_id = thread_conf->cpu_id;
1890 thread_per_cpu[cpu_id][n_thread_per_cpu[cpu_id]++] = thread_conf;
1892 if (cpu_id > max_cpu)
1894 if (cpu_id < min_cpu)
1897 for (i = 0; i < n_tx_thread; i++) {
1899 thread_conf = &tx_thread[i].conf;
1900 cpu_id = thread_conf->cpu_id;
1901 thread_per_cpu[cpu_id][n_thread_per_cpu[cpu_id]++] = thread_conf;
1903 if (thread_conf->cpu_id > max_cpu)
1904 max_cpu = thread_conf->cpu_id;
1905 if (thread_conf->cpu_id < min_cpu)
1906 min_cpu = thread_conf->cpu_id;
1912 for (i = min_cpu; i <= max_cpu; i++) {
1913 for (j = 0; j < MAX_CPU_COUNTER; j++) {
1914 for (k = 0; k < n_thread_per_cpu[i]; k++)
1915 if (thread_per_cpu[i][k]->busy[j]) {
1920 cpu_load.hits[j][i]++;
1932 cur_tsc = rte_rdtsc();
1934 diff_tsc = cur_tsc - prev_tsc;
1935 if (unlikely(diff_tsc > interval_tsc)) {
1939 printf("Cpu usage for %d rx threads and %d tx threads:\n\n",
1940 n_rx_thread, n_tx_thread);
1942 printf("cpu# proc%% poll%% overhead%%\n\n");
1944 for (i = min_cpu; i <= max_cpu; i++) {
1946 printf("CPU %d:", i);
1947 for (j = 0; j < MAX_CPU_COUNTER; j++) {
1948 printf("%7" PRIu64 "",
1949 cpu_load.hits[j][i] * 100 / cpu_load.counter);
1950 hits += cpu_load.hits[j][i];
1951 cpu_load.hits[j][i] = 0;
1953 printf("%7" PRIu64 "\n",
1954 100 - total[i] * 100 / cpu_load.counter);
1957 cpu_load.counter = 0;
1964 #endif /* APP_CPU_LOAD */
1967 * Null processing lthread loop
1969 * This loop is used to start empty scheduler on lcore.
1972 lthread_null(__rte_unused void *args)
1974 int lcore_id = rte_lcore_id();
1976 RTE_LOG(INFO, L3FWD, "Starting scheduler on lcore %d.\n", lcore_id);
1980 /* main processing loop */
1982 lthread_tx_per_ring(void *dummy)
1986 struct rte_ring *ring;
1987 struct thread_tx_conf *tx_conf;
1988 struct rte_mbuf *pkts_burst[MAX_PKT_BURST];
1989 struct lthread_cond *ready;
1991 tx_conf = (struct thread_tx_conf *)dummy;
1992 ring = tx_conf->ring;
1993 ready = *tx_conf->ready;
1995 lthread_set_data((void *)tx_conf);
1998 * Move this lthread to lcore
2000 lthread_set_affinity(tx_conf->conf.lcore_id);
2002 RTE_LOG(INFO, L3FWD, "entering main tx loop on lcore %u\n", rte_lcore_id());
2005 rte_atomic16_inc(&tx_counter);
2009 * Read packet from ring
2011 SET_CPU_BUSY(tx_conf, CPU_POLL);
2012 nb_rx = rte_ring_sc_dequeue_burst(ring, (void **)pkts_burst,
2014 SET_CPU_IDLE(tx_conf, CPU_POLL);
2017 SET_CPU_BUSY(tx_conf, CPU_PROCESS);
2018 portid = pkts_burst[0]->port;
2019 process_burst(pkts_burst, nb_rx, portid);
2020 SET_CPU_IDLE(tx_conf, CPU_PROCESS);
2023 lthread_cond_wait(ready, 0);
2029 * Main tx-lthreads spawner lthread.
2031 * This lthread is used to spawn one new lthread per ring from producers.
2035 lthread_tx(void *args)
2041 struct thread_tx_conf *tx_conf;
2043 tx_conf = (struct thread_tx_conf *)args;
2044 lthread_set_data((void *)tx_conf);
2047 * Move this lthread to the selected lcore
2049 lthread_set_affinity(tx_conf->conf.lcore_id);
2052 * Spawn tx readers (one per input ring)
2054 lthread_create(<, tx_conf->conf.lcore_id, lthread_tx_per_ring,
2057 lcore_id = rte_lcore_id();
2059 RTE_LOG(INFO, L3FWD, "Entering Tx main loop on lcore %u\n", lcore_id);
2061 tx_conf->conf.cpu_id = sched_getcpu();
2064 lthread_sleep(BURST_TX_DRAIN_US * 1000);
2067 * TX burst queue drain
2069 for (portid = 0; portid < RTE_MAX_ETHPORTS; portid++) {
2070 if (tx_conf->tx_mbufs[portid].len == 0)
2072 SET_CPU_BUSY(tx_conf, CPU_PROCESS);
2073 send_burst(tx_conf, tx_conf->tx_mbufs[portid].len, portid);
2074 SET_CPU_IDLE(tx_conf, CPU_PROCESS);
2075 tx_conf->tx_mbufs[portid].len = 0;
2082 lthread_rx(void *dummy)
2087 uint8_t portid, queueid;
2089 int len[RTE_MAX_LCORE] = { 0 };
2090 int old_len, new_len;
2091 struct rte_mbuf *pkts_burst[MAX_PKT_BURST];
2092 struct thread_rx_conf *rx_conf;
2094 rx_conf = (struct thread_rx_conf *)dummy;
2095 lthread_set_data((void *)rx_conf);
2098 * Move this lthread to lcore
2100 lthread_set_affinity(rx_conf->conf.lcore_id);
2102 if (rx_conf->n_rx_queue == 0) {
2103 RTE_LOG(INFO, L3FWD, "lcore %u has nothing to do\n", rte_lcore_id());
2107 RTE_LOG(INFO, L3FWD, "Entering main Rx loop on lcore %u\n", rte_lcore_id());
2109 for (i = 0; i < rx_conf->n_rx_queue; i++) {
2111 portid = rx_conf->rx_queue_list[i].port_id;
2112 queueid = rx_conf->rx_queue_list[i].queue_id;
2113 RTE_LOG(INFO, L3FWD, " -- lcoreid=%u portid=%hhu rxqueueid=%hhu\n",
2114 rte_lcore_id(), portid, queueid);
2118 * Init all condition variables (one per rx thread)
2120 for (i = 0; i < rx_conf->n_rx_queue; i++)
2121 lthread_cond_init(NULL, &rx_conf->ready[i], NULL);
2125 rx_conf->conf.cpu_id = sched_getcpu();
2126 rte_atomic16_inc(&rx_counter);
2130 * Read packet from RX queues
2132 for (i = 0; i < rx_conf->n_rx_queue; ++i) {
2133 portid = rx_conf->rx_queue_list[i].port_id;
2134 queueid = rx_conf->rx_queue_list[i].queue_id;
2136 SET_CPU_BUSY(rx_conf, CPU_POLL);
2137 nb_rx = rte_eth_rx_burst(portid, queueid, pkts_burst,
2139 SET_CPU_IDLE(rx_conf, CPU_POLL);
2142 worker_id = (worker_id + 1) % rx_conf->n_ring;
2143 old_len = len[worker_id];
2145 SET_CPU_BUSY(rx_conf, CPU_PROCESS);
2146 ret = rte_ring_sp_enqueue_burst(
2147 rx_conf->ring[worker_id],
2148 (void **) pkts_burst,
2151 new_len = old_len + ret;
2153 if (new_len >= BURST_SIZE) {
2154 lthread_cond_signal(rx_conf->ready[worker_id]);
2158 len[worker_id] = new_len;
2160 if (unlikely(ret < nb_rx)) {
2163 for (k = ret; k < nb_rx; k++) {
2164 struct rte_mbuf *m = pkts_burst[k];
2166 rte_pktmbuf_free(m);
2169 SET_CPU_IDLE(rx_conf, CPU_PROCESS);
2178 * Start scheduler with initial lthread on lcore
2180 * This lthread loop spawns all rx and tx lthreads on master lcore
2184 lthread_spawner(__rte_unused void *arg) {
2185 struct lthread *lt[MAX_THREAD];
2189 printf("Entering lthread_spawner\n");
2192 * Create producers (rx threads) on default lcore
2194 for (i = 0; i < n_rx_thread; i++) {
2195 rx_thread[i].conf.thread_id = i;
2196 lthread_create(<[n_thread], -1, lthread_rx,
2197 (void *)&rx_thread[i]);
2202 * Wait for all producers. Until some producers can be started on the same
2203 * scheduler as this lthread, yielding is required to let them to run and
2204 * prevent deadlock here.
2206 while (rte_atomic16_read(&rx_counter) < n_rx_thread)
2207 lthread_sleep(100000);
2210 * Create consumers (tx threads) on default lcore_id
2212 for (i = 0; i < n_tx_thread; i++) {
2213 tx_thread[i].conf.thread_id = i;
2214 lthread_create(<[n_thread], -1, lthread_tx,
2215 (void *)&tx_thread[i]);
2220 * Wait for all threads finished
2222 for (i = 0; i < n_thread; i++)
2223 lthread_join(lt[i], NULL);
2228 * Start master scheduler with initial lthread spawning rx and tx lthreads
2229 * (main_lthread_master).
2232 lthread_master_spawner(__rte_unused void *arg) {
2234 int lcore_id = rte_lcore_id();
2236 RTE_PER_LCORE(lcore_conf) = &lcore_conf[lcore_id];
2237 lthread_create(<, -1, lthread_spawner, NULL);
2244 * Start scheduler on lcore.
2247 sched_spawner(__rte_unused void *arg) {
2249 int lcore_id = rte_lcore_id();
2252 if (lcore_id == cpu_load_lcore_id) {
2253 cpu_load_collector(arg);
2256 #endif /* APP_CPU_LOAD */
2258 RTE_PER_LCORE(lcore_conf) = &lcore_conf[lcore_id];
2259 lthread_create(<, -1, lthread_null, NULL);
2265 /* main processing loop */
2267 pthread_tx(void *dummy)
2269 struct rte_mbuf *pkts_burst[MAX_PKT_BURST];
2270 uint64_t prev_tsc, diff_tsc, cur_tsc;
2273 struct thread_tx_conf *tx_conf;
2275 const uint64_t drain_tsc = (rte_get_tsc_hz() + US_PER_S - 1) /
2276 US_PER_S * BURST_TX_DRAIN_US;
2280 tx_conf = (struct thread_tx_conf *)dummy;
2282 RTE_LOG(INFO, L3FWD, "Entering main Tx loop on lcore %u\n", rte_lcore_id());
2284 tx_conf->conf.cpu_id = sched_getcpu();
2285 rte_atomic16_inc(&tx_counter);
2288 cur_tsc = rte_rdtsc();
2291 * TX burst queue drain
2293 diff_tsc = cur_tsc - prev_tsc;
2294 if (unlikely(diff_tsc > drain_tsc)) {
2297 * This could be optimized (use queueid instead of
2298 * portid), but it is not called so often
2300 SET_CPU_BUSY(tx_conf, CPU_PROCESS);
2301 for (portid = 0; portid < RTE_MAX_ETHPORTS; portid++) {
2302 if (tx_conf->tx_mbufs[portid].len == 0)
2304 send_burst(tx_conf, tx_conf->tx_mbufs[portid].len, portid);
2305 tx_conf->tx_mbufs[portid].len = 0;
2307 SET_CPU_IDLE(tx_conf, CPU_PROCESS);
2313 * Read packet from ring
2315 SET_CPU_BUSY(tx_conf, CPU_POLL);
2316 nb_rx = rte_ring_sc_dequeue_burst(tx_conf->ring,
2317 (void **)pkts_burst, MAX_PKT_BURST);
2318 SET_CPU_IDLE(tx_conf, CPU_POLL);
2320 if (unlikely(nb_rx == 0)) {
2325 SET_CPU_BUSY(tx_conf, CPU_PROCESS);
2326 portid = pkts_burst[0]->port;
2327 process_burst(pkts_burst, nb_rx, portid);
2328 SET_CPU_IDLE(tx_conf, CPU_PROCESS);
2334 pthread_rx(void *dummy)
2341 uint8_t portid, queueid;
2342 struct rte_mbuf *pkts_burst[MAX_PKT_BURST];
2344 struct thread_rx_conf *rx_conf;
2346 lcore_id = rte_lcore_id();
2347 rx_conf = (struct thread_rx_conf *)dummy;
2349 if (rx_conf->n_rx_queue == 0) {
2350 RTE_LOG(INFO, L3FWD, "lcore %u has nothing to do\n", lcore_id);
2354 RTE_LOG(INFO, L3FWD, "entering main rx loop on lcore %u\n", lcore_id);
2356 for (i = 0; i < rx_conf->n_rx_queue; i++) {
2358 portid = rx_conf->rx_queue_list[i].port_id;
2359 queueid = rx_conf->rx_queue_list[i].queue_id;
2360 RTE_LOG(INFO, L3FWD, " -- lcoreid=%u portid=%hhu rxqueueid=%hhu\n",
2361 lcore_id, portid, queueid);
2365 rx_conf->conf.cpu_id = sched_getcpu();
2366 rte_atomic16_inc(&rx_counter);
2370 * Read packet from RX queues
2372 for (i = 0; i < rx_conf->n_rx_queue; ++i) {
2373 portid = rx_conf->rx_queue_list[i].port_id;
2374 queueid = rx_conf->rx_queue_list[i].queue_id;
2376 SET_CPU_BUSY(rx_conf, CPU_POLL);
2377 nb_rx = rte_eth_rx_burst(portid, queueid, pkts_burst,
2379 SET_CPU_IDLE(rx_conf, CPU_POLL);
2386 SET_CPU_BUSY(rx_conf, CPU_PROCESS);
2387 worker_id = (worker_id + 1) % rx_conf->n_ring;
2388 n = rte_ring_sp_enqueue_burst(rx_conf->ring[worker_id],
2389 (void **)pkts_burst, nb_rx);
2391 if (unlikely(n != nb_rx)) {
2394 for (k = n; k < nb_rx; k++) {
2395 struct rte_mbuf *m = pkts_burst[k];
2397 rte_pktmbuf_free(m);
2401 SET_CPU_IDLE(rx_conf, CPU_PROCESS);
2411 pthread_run(__rte_unused void *arg) {
2412 int lcore_id = rte_lcore_id();
2415 for (i = 0; i < n_rx_thread; i++)
2416 if (rx_thread[i].conf.lcore_id == lcore_id) {
2417 printf("Start rx thread on %d...\n", lcore_id);
2418 RTE_PER_LCORE(lcore_conf) = &lcore_conf[lcore_id];
2419 RTE_PER_LCORE(lcore_conf)->data = (void *)&rx_thread[i];
2420 pthread_rx((void *)&rx_thread[i]);
2424 for (i = 0; i < n_tx_thread; i++)
2425 if (tx_thread[i].conf.lcore_id == lcore_id) {
2426 printf("Start tx thread on %d...\n", lcore_id);
2427 RTE_PER_LCORE(lcore_conf) = &lcore_conf[lcore_id];
2428 RTE_PER_LCORE(lcore_conf)->data = (void *)&tx_thread[i];
2429 pthread_tx((void *)&tx_thread[i]);
2434 if (lcore_id == cpu_load_lcore_id)
2435 cpu_load_collector(arg);
2436 #endif /* APP_CPU_LOAD */
2442 check_lcore_params(void)
2444 uint8_t queue, lcore;
2448 for (i = 0; i < nb_rx_thread_params; ++i) {
2449 queue = rx_thread_params[i].queue_id;
2450 if (queue >= MAX_RX_QUEUE_PER_PORT) {
2451 printf("invalid queue number: %hhu\n", queue);
2454 lcore = rx_thread_params[i].lcore_id;
2455 if (!rte_lcore_is_enabled(lcore)) {
2456 printf("error: lcore %hhu is not enabled in lcore mask\n", lcore);
2459 socketid = rte_lcore_to_socket_id(lcore);
2460 if ((socketid != 0) && (numa_on == 0))
2461 printf("warning: lcore %hhu is on socket %d with numa off\n",
2468 check_port_config(const unsigned nb_ports)
2473 for (i = 0; i < nb_rx_thread_params; ++i) {
2474 portid = rx_thread_params[i].port_id;
2475 if ((enabled_port_mask & (1 << portid)) == 0) {
2476 printf("port %u is not enabled in port mask\n", portid);
2479 if (portid >= nb_ports) {
2480 printf("port %u is not present on the board\n", portid);
2488 get_port_n_rx_queues(const uint8_t port)
2493 for (i = 0; i < nb_rx_thread_params; ++i)
2494 if (rx_thread_params[i].port_id == port &&
2495 rx_thread_params[i].queue_id > queue)
2496 queue = rx_thread_params[i].queue_id;
2498 return (uint8_t)(++queue);
2505 struct thread_rx_conf *rx_conf;
2506 struct thread_tx_conf *tx_conf;
2507 unsigned rx_thread_id, tx_thread_id;
2509 struct rte_ring *ring = NULL;
2511 for (tx_thread_id = 0; tx_thread_id < n_tx_thread; tx_thread_id++) {
2513 tx_conf = &tx_thread[tx_thread_id];
2515 printf("Connecting tx-thread %d with rx-thread %d\n", tx_thread_id,
2516 tx_conf->conf.thread_id);
2518 rx_thread_id = tx_conf->conf.thread_id;
2519 if (rx_thread_id > n_tx_thread) {
2520 printf("connection from tx-thread %u to rx-thread %u fails "
2521 "(rx-thread not defined)\n", tx_thread_id, rx_thread_id);
2525 rx_conf = &rx_thread[rx_thread_id];
2526 socket_io = rte_lcore_to_socket_id(rx_conf->conf.lcore_id);
2528 snprintf(name, sizeof(name), "app_ring_s%u_rx%u_tx%u",
2529 socket_io, rx_thread_id, tx_thread_id);
2531 ring = rte_ring_create(name, 1024 * 4, socket_io,
2532 RING_F_SP_ENQ | RING_F_SC_DEQ);
2535 rte_panic("Cannot create ring to connect rx-thread %u "
2536 "with tx-thread %u\n", rx_thread_id, tx_thread_id);
2539 rx_conf->ring[rx_conf->n_ring] = ring;
2541 tx_conf->ring = ring;
2542 tx_conf->ready = &rx_conf->ready[rx_conf->n_ring];
2550 init_rx_queues(void)
2552 uint16_t i, nb_rx_queue;
2557 for (i = 0; i < nb_rx_thread_params; ++i) {
2558 thread = rx_thread_params[i].thread_id;
2559 nb_rx_queue = rx_thread[thread].n_rx_queue;
2561 if (nb_rx_queue >= MAX_RX_QUEUE_PER_LCORE) {
2562 printf("error: too many queues (%u) for thread: %u\n",
2563 (unsigned)nb_rx_queue + 1, (unsigned)thread);
2567 rx_thread[thread].conf.thread_id = thread;
2568 rx_thread[thread].conf.lcore_id = rx_thread_params[i].lcore_id;
2569 rx_thread[thread].rx_queue_list[nb_rx_queue].port_id =
2570 rx_thread_params[i].port_id;
2571 rx_thread[thread].rx_queue_list[nb_rx_queue].queue_id =
2572 rx_thread_params[i].queue_id;
2573 rx_thread[thread].n_rx_queue++;
2575 if (thread >= n_rx_thread)
2576 n_rx_thread = thread + 1;
2583 init_tx_threads(void)
2588 for (i = 0; i < nb_tx_thread_params; ++i) {
2589 tx_thread[n_tx_thread].conf.thread_id = tx_thread_params[i].thread_id;
2590 tx_thread[n_tx_thread].conf.lcore_id = tx_thread_params[i].lcore_id;
2598 print_usage(const char *prgname)
2600 printf("%s [EAL options] -- -p PORTMASK -P"
2601 " [--rx (port,queue,lcore,thread)[,(port,queue,lcore,thread]]"
2602 " [--tx (lcore,thread)[,(lcore,thread]]"
2603 " [--enable-jumbo [--max-pkt-len PKTLEN]]\n"
2604 " -p PORTMASK: hexadecimal bitmask of ports to configure\n"
2605 " -P : enable promiscuous mode\n"
2606 " --rx (port,queue,lcore,thread): rx queues configuration\n"
2607 " --tx (lcore,thread): tx threads configuration\n"
2608 " --stat-lcore LCORE: use lcore for stat collector\n"
2609 " --eth-dest=X,MM:MM:MM:MM:MM:MM: optional, ethernet destination for port X\n"
2610 " --no-numa: optional, disable numa awareness\n"
2611 " --ipv6: optional, specify it if running ipv6 packets\n"
2612 " --enable-jumbo: enable jumbo frame"
2613 " which max packet len is PKTLEN in decimal (64-9600)\n"
2614 " --hash-entry-num: specify the hash entry number in hexadecimal to be setup\n"
2615 " --no-lthreads: turn off lthread model\n",
2619 static int parse_max_pkt_len(const char *pktlen)
2624 /* parse decimal string */
2625 len = strtoul(pktlen, &end, 10);
2626 if ((pktlen[0] == '\0') || (end == NULL) || (*end != '\0'))
2636 parse_portmask(const char *portmask)
2641 /* parse hexadecimal string */
2642 pm = strtoul(portmask, &end, 16);
2643 if ((portmask[0] == '\0') || (end == NULL) || (*end != '\0'))
2652 #if (APP_LOOKUP_METHOD == APP_LOOKUP_EXACT_MATCH)
2654 parse_hash_entry_number(const char *hash_entry_num)
2657 unsigned long hash_en;
2659 /* parse hexadecimal string */
2660 hash_en = strtoul(hash_entry_num, &end, 16);
2661 if ((hash_entry_num[0] == '\0') || (end == NULL) || (*end != '\0'))
2672 parse_rx_config(const char *q_arg)
2675 const char *p, *p0 = q_arg;
2684 unsigned long int_fld[_NUM_FLD];
2685 char *str_fld[_NUM_FLD];
2689 nb_rx_thread_params = 0;
2691 while ((p = strchr(p0, '(')) != NULL) {
2693 p0 = strchr(p, ')');
2698 if (size >= sizeof(s))
2701 snprintf(s, sizeof(s), "%.*s", size, p);
2702 if (rte_strsplit(s, sizeof(s), str_fld, _NUM_FLD, ',') != _NUM_FLD)
2704 for (i = 0; i < _NUM_FLD; i++) {
2706 int_fld[i] = strtoul(str_fld[i], &end, 0);
2707 if (errno != 0 || end == str_fld[i] || int_fld[i] > 255)
2710 if (nb_rx_thread_params >= MAX_LCORE_PARAMS) {
2711 printf("exceeded max number of rx params: %hu\n",
2712 nb_rx_thread_params);
2715 rx_thread_params_array[nb_rx_thread_params].port_id =
2716 (uint8_t)int_fld[FLD_PORT];
2717 rx_thread_params_array[nb_rx_thread_params].queue_id =
2718 (uint8_t)int_fld[FLD_QUEUE];
2719 rx_thread_params_array[nb_rx_thread_params].lcore_id =
2720 (uint8_t)int_fld[FLD_LCORE];
2721 rx_thread_params_array[nb_rx_thread_params].thread_id =
2722 (uint8_t)int_fld[FLD_THREAD];
2723 ++nb_rx_thread_params;
2725 rx_thread_params = rx_thread_params_array;
2730 parse_tx_config(const char *q_arg)
2733 const char *p, *p0 = q_arg;
2740 unsigned long int_fld[_NUM_FLD];
2741 char *str_fld[_NUM_FLD];
2745 nb_tx_thread_params = 0;
2747 while ((p = strchr(p0, '(')) != NULL) {
2749 p0 = strchr(p, ')');
2754 if (size >= sizeof(s))
2757 snprintf(s, sizeof(s), "%.*s", size, p);
2758 if (rte_strsplit(s, sizeof(s), str_fld, _NUM_FLD, ',') != _NUM_FLD)
2760 for (i = 0; i < _NUM_FLD; i++) {
2762 int_fld[i] = strtoul(str_fld[i], &end, 0);
2763 if (errno != 0 || end == str_fld[i] || int_fld[i] > 255)
2766 if (nb_tx_thread_params >= MAX_LCORE_PARAMS) {
2767 printf("exceeded max number of tx params: %hu\n",
2768 nb_tx_thread_params);
2771 tx_thread_params_array[nb_tx_thread_params].lcore_id =
2772 (uint8_t)int_fld[FLD_LCORE];
2773 tx_thread_params_array[nb_tx_thread_params].thread_id =
2774 (uint8_t)int_fld[FLD_THREAD];
2775 ++nb_tx_thread_params;
2777 tx_thread_params = tx_thread_params_array;
2782 #if (APP_CPU_LOAD > 0)
2784 parse_stat_lcore(const char *stat_lcore)
2787 unsigned long lcore_id;
2789 lcore_id = strtoul(stat_lcore, &end, 10);
2790 if ((stat_lcore[0] == '\0') || (end == NULL) || (*end != '\0'))
2798 parse_eth_dest(const char *optarg)
2802 uint8_t c, *dest, peer_addr[6];
2805 portid = strtoul(optarg, &port_end, 10);
2806 if (errno != 0 || port_end == optarg || *port_end++ != ',')
2807 rte_exit(EXIT_FAILURE,
2808 "Invalid eth-dest: %s", optarg);
2809 if (portid >= RTE_MAX_ETHPORTS)
2810 rte_exit(EXIT_FAILURE,
2811 "eth-dest: port %d >= RTE_MAX_ETHPORTS(%d)\n",
2812 portid, RTE_MAX_ETHPORTS);
2814 if (cmdline_parse_etheraddr(NULL, port_end,
2815 &peer_addr, sizeof(peer_addr)) < 0)
2816 rte_exit(EXIT_FAILURE,
2817 "Invalid ethernet address: %s\n",
2819 dest = (uint8_t *)&dest_eth_addr[portid];
2820 for (c = 0; c < 6; c++)
2821 dest[c] = peer_addr[c];
2822 *(uint64_t *)(val_eth + portid) = dest_eth_addr[portid];
2825 #define CMD_LINE_OPT_RX_CONFIG "rx"
2826 #define CMD_LINE_OPT_TX_CONFIG "tx"
2827 #define CMD_LINE_OPT_STAT_LCORE "stat-lcore"
2828 #define CMD_LINE_OPT_ETH_DEST "eth-dest"
2829 #define CMD_LINE_OPT_NO_NUMA "no-numa"
2830 #define CMD_LINE_OPT_IPV6 "ipv6"
2831 #define CMD_LINE_OPT_ENABLE_JUMBO "enable-jumbo"
2832 #define CMD_LINE_OPT_HASH_ENTRY_NUM "hash-entry-num"
2833 #define CMD_LINE_OPT_NO_LTHREADS "no-lthreads"
2835 /* Parse the argument given in the command line of the application */
2837 parse_args(int argc, char **argv)
2842 char *prgname = argv[0];
2843 static struct option lgopts[] = {
2844 {CMD_LINE_OPT_RX_CONFIG, 1, 0, 0},
2845 {CMD_LINE_OPT_TX_CONFIG, 1, 0, 0},
2846 {CMD_LINE_OPT_STAT_LCORE, 1, 0, 0},
2847 {CMD_LINE_OPT_ETH_DEST, 1, 0, 0},
2848 {CMD_LINE_OPT_NO_NUMA, 0, 0, 0},
2849 {CMD_LINE_OPT_IPV6, 0, 0, 0},
2850 {CMD_LINE_OPT_ENABLE_JUMBO, 0, 0, 0},
2851 {CMD_LINE_OPT_HASH_ENTRY_NUM, 1, 0, 0},
2852 {CMD_LINE_OPT_NO_LTHREADS, 0, 0, 0},
2858 while ((opt = getopt_long(argc, argvopt, "p:P",
2859 lgopts, &option_index)) != EOF) {
2864 enabled_port_mask = parse_portmask(optarg);
2865 if (enabled_port_mask == 0) {
2866 printf("invalid portmask\n");
2867 print_usage(prgname);
2872 printf("Promiscuous mode selected\n");
2878 if (!strncmp(lgopts[option_index].name, CMD_LINE_OPT_RX_CONFIG,
2879 sizeof(CMD_LINE_OPT_RX_CONFIG))) {
2880 ret = parse_rx_config(optarg);
2882 printf("invalid rx-config\n");
2883 print_usage(prgname);
2888 if (!strncmp(lgopts[option_index].name, CMD_LINE_OPT_TX_CONFIG,
2889 sizeof(CMD_LINE_OPT_TX_CONFIG))) {
2890 ret = parse_tx_config(optarg);
2892 printf("invalid tx-config\n");
2893 print_usage(prgname);
2898 #if (APP_CPU_LOAD > 0)
2899 if (!strncmp(lgopts[option_index].name, CMD_LINE_OPT_STAT_LCORE,
2900 sizeof(CMD_LINE_OPT_STAT_LCORE))) {
2901 cpu_load_lcore_id = parse_stat_lcore(optarg);
2905 if (!strncmp(lgopts[option_index].name, CMD_LINE_OPT_ETH_DEST,
2906 sizeof(CMD_LINE_OPT_ETH_DEST)))
2907 parse_eth_dest(optarg);
2909 if (!strncmp(lgopts[option_index].name, CMD_LINE_OPT_NO_NUMA,
2910 sizeof(CMD_LINE_OPT_NO_NUMA))) {
2911 printf("numa is disabled\n");
2915 #if (APP_LOOKUP_METHOD == APP_LOOKUP_EXACT_MATCH)
2916 if (!strncmp(lgopts[option_index].name, CMD_LINE_OPT_IPV6,
2917 sizeof(CMD_LINE_OPT_IPV6))) {
2918 printf("ipv6 is specified\n");
2923 if (!strncmp(lgopts[option_index].name, CMD_LINE_OPT_NO_LTHREADS,
2924 sizeof(CMD_LINE_OPT_NO_LTHREADS))) {
2925 printf("l-threads model is disabled\n");
2929 if (!strncmp(lgopts[option_index].name, CMD_LINE_OPT_ENABLE_JUMBO,
2930 sizeof(CMD_LINE_OPT_ENABLE_JUMBO))) {
2931 struct option lenopts = {"max-pkt-len", required_argument, 0,
2934 printf("jumbo frame is enabled - disabling simple TX path\n");
2935 port_conf.rxmode.jumbo_frame = 1;
2937 /* if no max-pkt-len set, use the default value ETHER_MAX_LEN */
2938 if (0 == getopt_long(argc, argvopt, "", &lenopts,
2941 ret = parse_max_pkt_len(optarg);
2942 if ((ret < 64) || (ret > MAX_JUMBO_PKT_LEN)) {
2943 printf("invalid packet length\n");
2944 print_usage(prgname);
2947 port_conf.rxmode.max_rx_pkt_len = ret;
2949 printf("set jumbo frame max packet length to %u\n",
2950 (unsigned int)port_conf.rxmode.max_rx_pkt_len);
2952 #if (APP_LOOKUP_METHOD == APP_LOOKUP_EXACT_MATCH)
2953 if (!strncmp(lgopts[option_index].name, CMD_LINE_OPT_HASH_ENTRY_NUM,
2954 sizeof(CMD_LINE_OPT_HASH_ENTRY_NUM))) {
2955 ret = parse_hash_entry_number(optarg);
2956 if ((ret > 0) && (ret <= L3FWD_HASH_ENTRIES)) {
2957 hash_entry_number = ret;
2959 printf("invalid hash entry number\n");
2960 print_usage(prgname);
2968 print_usage(prgname);
2974 argv[optind-1] = prgname;
2977 optind = 0; /* reset getopt lib */
2982 print_ethaddr(const char *name, const struct ether_addr *eth_addr)
2984 char buf[ETHER_ADDR_FMT_SIZE];
2986 ether_format_addr(buf, ETHER_ADDR_FMT_SIZE, eth_addr);
2987 printf("%s%s", name, buf);
2990 #if (APP_LOOKUP_METHOD == APP_LOOKUP_EXACT_MATCH)
2992 static void convert_ipv4_5tuple(struct ipv4_5tuple *key1,
2993 union ipv4_5tuple_host *key2)
2995 key2->ip_dst = rte_cpu_to_be_32(key1->ip_dst);
2996 key2->ip_src = rte_cpu_to_be_32(key1->ip_src);
2997 key2->port_dst = rte_cpu_to_be_16(key1->port_dst);
2998 key2->port_src = rte_cpu_to_be_16(key1->port_src);
2999 key2->proto = key1->proto;
3004 static void convert_ipv6_5tuple(struct ipv6_5tuple *key1,
3005 union ipv6_5tuple_host *key2)
3009 for (i = 0; i < 16; i++) {
3010 key2->ip_dst[i] = key1->ip_dst[i];
3011 key2->ip_src[i] = key1->ip_src[i];
3013 key2->port_dst = rte_cpu_to_be_16(key1->port_dst);
3014 key2->port_src = rte_cpu_to_be_16(key1->port_src);
3015 key2->proto = key1->proto;
3021 #define BYTE_VALUE_MAX 256
3022 #define ALL_32_BITS 0xffffffff
3023 #define BIT_8_TO_15 0x0000ff00
3025 populate_ipv4_few_flow_into_table(const struct rte_hash *h)
3029 uint32_t array_len = RTE_DIM(ipv4_l3fwd_route_array);
3031 mask0 = _mm_set_epi32(ALL_32_BITS, ALL_32_BITS, ALL_32_BITS, BIT_8_TO_15);
3032 for (i = 0; i < array_len; i++) {
3033 struct ipv4_l3fwd_route entry;
3034 union ipv4_5tuple_host newkey;
3036 entry = ipv4_l3fwd_route_array[i];
3037 convert_ipv4_5tuple(&entry.key, &newkey);
3038 ret = rte_hash_add_key(h, (void *)&newkey);
3040 rte_exit(EXIT_FAILURE, "Unable to add entry %" PRIu32
3041 " to the l3fwd hash.\n", i);
3043 ipv4_l3fwd_out_if[ret] = entry.if_out;
3045 printf("Hash: Adding 0x%" PRIx32 " keys\n", array_len);
3048 #define BIT_16_TO_23 0x00ff0000
3050 populate_ipv6_few_flow_into_table(const struct rte_hash *h)
3054 uint32_t array_len = RTE_DIM(ipv6_l3fwd_route_array);
3056 mask1 = _mm_set_epi32(ALL_32_BITS, ALL_32_BITS, ALL_32_BITS, BIT_16_TO_23);
3057 mask2 = _mm_set_epi32(0, 0, ALL_32_BITS, ALL_32_BITS);
3058 for (i = 0; i < array_len; i++) {
3059 struct ipv6_l3fwd_route entry;
3060 union ipv6_5tuple_host newkey;
3062 entry = ipv6_l3fwd_route_array[i];
3063 convert_ipv6_5tuple(&entry.key, &newkey);
3064 ret = rte_hash_add_key(h, (void *)&newkey);
3066 rte_exit(EXIT_FAILURE, "Unable to add entry %" PRIu32
3067 " to the l3fwd hash.\n", i);
3069 ipv6_l3fwd_out_if[ret] = entry.if_out;
3071 printf("Hash: Adding 0x%" PRIx32 "keys\n", array_len);
3074 #define NUMBER_PORT_USED 4
3076 populate_ipv4_many_flow_into_table(const struct rte_hash *h,
3077 unsigned int nr_flow)
3081 mask0 = _mm_set_epi32(ALL_32_BITS, ALL_32_BITS, ALL_32_BITS, BIT_8_TO_15);
3083 for (i = 0; i < nr_flow; i++) {
3084 struct ipv4_l3fwd_route entry;
3085 union ipv4_5tuple_host newkey;
3086 uint8_t a = (uint8_t)((i / NUMBER_PORT_USED) % BYTE_VALUE_MAX);
3087 uint8_t b = (uint8_t)(((i / NUMBER_PORT_USED) / BYTE_VALUE_MAX) %
3089 uint8_t c = (uint8_t)((i / NUMBER_PORT_USED) / (BYTE_VALUE_MAX *
3091 /* Create the ipv4 exact match flow */
3092 memset(&entry, 0, sizeof(entry));
3093 switch (i & (NUMBER_PORT_USED - 1)) {
3095 entry = ipv4_l3fwd_route_array[0];
3096 entry.key.ip_dst = IPv4(101, c, b, a);
3099 entry = ipv4_l3fwd_route_array[1];
3100 entry.key.ip_dst = IPv4(201, c, b, a);
3103 entry = ipv4_l3fwd_route_array[2];
3104 entry.key.ip_dst = IPv4(111, c, b, a);
3107 entry = ipv4_l3fwd_route_array[3];
3108 entry.key.ip_dst = IPv4(211, c, b, a);
3111 convert_ipv4_5tuple(&entry.key, &newkey);
3112 int32_t ret = rte_hash_add_key(h, (void *)&newkey);
3115 rte_exit(EXIT_FAILURE, "Unable to add entry %u\n", i);
3117 ipv4_l3fwd_out_if[ret] = (uint8_t)entry.if_out;
3120 printf("Hash: Adding 0x%x keys\n", nr_flow);
3124 populate_ipv6_many_flow_into_table(const struct rte_hash *h,
3125 unsigned int nr_flow)
3129 mask1 = _mm_set_epi32(ALL_32_BITS, ALL_32_BITS, ALL_32_BITS, BIT_16_TO_23);
3130 mask2 = _mm_set_epi32(0, 0, ALL_32_BITS, ALL_32_BITS);
3131 for (i = 0; i < nr_flow; i++) {
3132 struct ipv6_l3fwd_route entry;
3133 union ipv6_5tuple_host newkey;
3135 uint8_t a = (uint8_t) ((i / NUMBER_PORT_USED) % BYTE_VALUE_MAX);
3136 uint8_t b = (uint8_t) (((i / NUMBER_PORT_USED) / BYTE_VALUE_MAX) %
3138 uint8_t c = (uint8_t) ((i / NUMBER_PORT_USED) / (BYTE_VALUE_MAX *
3141 /* Create the ipv6 exact match flow */
3142 memset(&entry, 0, sizeof(entry));
3143 switch (i & (NUMBER_PORT_USED - 1)) {
3145 entry = ipv6_l3fwd_route_array[0];
3148 entry = ipv6_l3fwd_route_array[1];
3151 entry = ipv6_l3fwd_route_array[2];
3154 entry = ipv6_l3fwd_route_array[3];
3157 entry.key.ip_dst[13] = c;
3158 entry.key.ip_dst[14] = b;
3159 entry.key.ip_dst[15] = a;
3160 convert_ipv6_5tuple(&entry.key, &newkey);
3161 int32_t ret = rte_hash_add_key(h, (void *)&newkey);
3164 rte_exit(EXIT_FAILURE, "Unable to add entry %u\n", i);
3166 ipv6_l3fwd_out_if[ret] = (uint8_t) entry.if_out;
3169 printf("Hash: Adding 0x%x keys\n", nr_flow);
3173 setup_hash(int socketid)
3175 struct rte_hash_parameters ipv4_l3fwd_hash_params = {
3177 .entries = L3FWD_HASH_ENTRIES,
3178 .key_len = sizeof(union ipv4_5tuple_host),
3179 .hash_func = ipv4_hash_crc,
3180 .hash_func_init_val = 0,
3183 struct rte_hash_parameters ipv6_l3fwd_hash_params = {
3185 .entries = L3FWD_HASH_ENTRIES,
3186 .key_len = sizeof(union ipv6_5tuple_host),
3187 .hash_func = ipv6_hash_crc,
3188 .hash_func_init_val = 0,
3193 /* create ipv4 hash */
3194 snprintf(s, sizeof(s), "ipv4_l3fwd_hash_%d", socketid);
3195 ipv4_l3fwd_hash_params.name = s;
3196 ipv4_l3fwd_hash_params.socket_id = socketid;
3197 ipv4_l3fwd_lookup_struct[socketid] =
3198 rte_hash_create(&ipv4_l3fwd_hash_params);
3199 if (ipv4_l3fwd_lookup_struct[socketid] == NULL)
3200 rte_exit(EXIT_FAILURE, "Unable to create the l3fwd hash on "
3201 "socket %d\n", socketid);
3203 /* create ipv6 hash */
3204 snprintf(s, sizeof(s), "ipv6_l3fwd_hash_%d", socketid);
3205 ipv6_l3fwd_hash_params.name = s;
3206 ipv6_l3fwd_hash_params.socket_id = socketid;
3207 ipv6_l3fwd_lookup_struct[socketid] =
3208 rte_hash_create(&ipv6_l3fwd_hash_params);
3209 if (ipv6_l3fwd_lookup_struct[socketid] == NULL)
3210 rte_exit(EXIT_FAILURE, "Unable to create the l3fwd hash on "
3211 "socket %d\n", socketid);
3213 if (hash_entry_number != HASH_ENTRY_NUMBER_DEFAULT) {
3214 /* For testing hash matching with a large number of flows we
3215 * generate millions of IP 5-tuples with an incremented dst
3216 * address to initialize the hash table. */
3218 /* populate the ipv4 hash */
3219 populate_ipv4_many_flow_into_table(
3220 ipv4_l3fwd_lookup_struct[socketid], hash_entry_number);
3222 /* populate the ipv6 hash */
3223 populate_ipv6_many_flow_into_table(
3224 ipv6_l3fwd_lookup_struct[socketid], hash_entry_number);
3227 /* Use data in ipv4/ipv6 l3fwd lookup table directly to initialize
3230 /* populate the ipv4 hash */
3231 populate_ipv4_few_flow_into_table(
3232 ipv4_l3fwd_lookup_struct[socketid]);
3234 /* populate the ipv6 hash */
3235 populate_ipv6_few_flow_into_table(
3236 ipv6_l3fwd_lookup_struct[socketid]);
3242 #if (APP_LOOKUP_METHOD == APP_LOOKUP_LPM)
3244 setup_lpm(int socketid)
3246 struct rte_lpm6_config config;
3251 /* create the LPM table */
3252 snprintf(s, sizeof(s), "IPV4_L3FWD_LPM_%d", socketid);
3253 ipv4_l3fwd_lookup_struct[socketid] = rte_lpm_create(s, socketid,
3254 IPV4_L3FWD_LPM_MAX_RULES, 0);
3255 if (ipv4_l3fwd_lookup_struct[socketid] == NULL)
3256 rte_exit(EXIT_FAILURE, "Unable to create the l3fwd LPM table"
3257 " on socket %d\n", socketid);
3259 /* populate the LPM table */
3260 for (i = 0; i < IPV4_L3FWD_NUM_ROUTES; i++) {
3262 /* skip unused ports */
3263 if ((1 << ipv4_l3fwd_route_array[i].if_out &
3264 enabled_port_mask) == 0)
3267 ret = rte_lpm_add(ipv4_l3fwd_lookup_struct[socketid],
3268 ipv4_l3fwd_route_array[i].ip,
3269 ipv4_l3fwd_route_array[i].depth,
3270 ipv4_l3fwd_route_array[i].if_out);
3273 rte_exit(EXIT_FAILURE, "Unable to add entry %u to the "
3274 "l3fwd LPM table on socket %d\n",
3278 printf("LPM: Adding route 0x%08x / %d (%d)\n",
3279 (unsigned)ipv4_l3fwd_route_array[i].ip,
3280 ipv4_l3fwd_route_array[i].depth,
3281 ipv4_l3fwd_route_array[i].if_out);
3284 /* create the LPM6 table */
3285 snprintf(s, sizeof(s), "IPV6_L3FWD_LPM_%d", socketid);
3287 config.max_rules = IPV6_L3FWD_LPM_MAX_RULES;
3288 config.number_tbl8s = IPV6_L3FWD_LPM_NUMBER_TBL8S;
3290 ipv6_l3fwd_lookup_struct[socketid] = rte_lpm6_create(s, socketid,
3292 if (ipv6_l3fwd_lookup_struct[socketid] == NULL)
3293 rte_exit(EXIT_FAILURE, "Unable to create the l3fwd LPM table"
3294 " on socket %d\n", socketid);
3296 /* populate the LPM table */
3297 for (i = 0; i < IPV6_L3FWD_NUM_ROUTES; i++) {
3299 /* skip unused ports */
3300 if ((1 << ipv6_l3fwd_route_array[i].if_out &
3301 enabled_port_mask) == 0)
3304 ret = rte_lpm6_add(ipv6_l3fwd_lookup_struct[socketid],
3305 ipv6_l3fwd_route_array[i].ip,
3306 ipv6_l3fwd_route_array[i].depth,
3307 ipv6_l3fwd_route_array[i].if_out);
3310 rte_exit(EXIT_FAILURE, "Unable to add entry %u to the "
3311 "l3fwd LPM table on socket %d\n",
3315 printf("LPM: Adding route %s / %d (%d)\n",
3317 ipv6_l3fwd_route_array[i].depth,
3318 ipv6_l3fwd_route_array[i].if_out);
3324 init_mem(unsigned nb_mbuf)
3326 struct lcore_conf *qconf;
3331 for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
3332 if (rte_lcore_is_enabled(lcore_id) == 0)
3336 socketid = rte_lcore_to_socket_id(lcore_id);
3340 if (socketid >= NB_SOCKETS) {
3341 rte_exit(EXIT_FAILURE, "Socket %d of lcore %u is out of range %d\n",
3342 socketid, lcore_id, NB_SOCKETS);
3344 if (pktmbuf_pool[socketid] == NULL) {
3345 snprintf(s, sizeof(s), "mbuf_pool_%d", socketid);
3346 pktmbuf_pool[socketid] =
3347 rte_pktmbuf_pool_create(s, nb_mbuf,
3348 MEMPOOL_CACHE_SIZE, 0,
3349 RTE_MBUF_DEFAULT_BUF_SIZE, socketid);
3350 if (pktmbuf_pool[socketid] == NULL)
3351 rte_exit(EXIT_FAILURE,
3352 "Cannot init mbuf pool on socket %d\n", socketid);
3354 printf("Allocated mbuf pool on socket %d\n", socketid);
3356 #if (APP_LOOKUP_METHOD == APP_LOOKUP_LPM)
3357 setup_lpm(socketid);
3359 setup_hash(socketid);
3362 qconf = &lcore_conf[lcore_id];
3363 qconf->ipv4_lookup_struct = ipv4_l3fwd_lookup_struct[socketid];
3364 qconf->ipv6_lookup_struct = ipv6_l3fwd_lookup_struct[socketid];
3369 /* Check the link status of all ports in up to 9s, and print them finally */
3371 check_all_ports_link_status(uint8_t port_num, uint32_t port_mask)
3373 #define CHECK_INTERVAL 100 /* 100ms */
3374 #define MAX_CHECK_TIME 90 /* 9s (90 * 100ms) in total */
3375 uint8_t portid, count, all_ports_up, print_flag = 0;
3376 struct rte_eth_link link;
3378 printf("\nChecking link status");
3380 for (count = 0; count <= MAX_CHECK_TIME; count++) {
3382 for (portid = 0; portid < port_num; portid++) {
3383 if ((port_mask & (1 << portid)) == 0)
3385 memset(&link, 0, sizeof(link));
3386 rte_eth_link_get_nowait(portid, &link);
3387 /* print link status if flag set */
3388 if (print_flag == 1) {
3389 if (link.link_status)
3390 printf("Port %d Link Up - speed %u "
3391 "Mbps - %s\n", (uint8_t)portid,
3392 (unsigned)link.link_speed,
3393 (link.link_duplex == ETH_LINK_FULL_DUPLEX) ?
3394 ("full-duplex") : ("half-duplex\n"));
3396 printf("Port %d Link Down\n",
3400 /* clear all_ports_up flag if any link down */
3401 if (link.link_status == 0) {
3406 /* after finally printing all link status, get out */
3407 if (print_flag == 1)
3410 if (all_ports_up == 0) {
3413 rte_delay_ms(CHECK_INTERVAL);
3416 /* set the print_flag if all ports up or timeout */
3417 if (all_ports_up == 1 || count == (MAX_CHECK_TIME - 1)) {
3425 main(int argc, char **argv)
3427 struct rte_eth_dev_info dev_info;
3428 struct rte_eth_txconf *txconf;
3434 uint32_t n_tx_queue, nb_lcores;
3435 uint8_t portid, nb_rx_queue, queue, socketid;
3438 ret = rte_eal_init(argc, argv);
3440 rte_exit(EXIT_FAILURE, "Invalid EAL parameters\n");
3444 /* pre-init dst MACs for all ports to 02:00:00:00:00:xx */
3445 for (portid = 0; portid < RTE_MAX_ETHPORTS; portid++) {
3446 dest_eth_addr[portid] = ETHER_LOCAL_ADMIN_ADDR +
3447 ((uint64_t)portid << 40);
3448 *(uint64_t *)(val_eth + portid) = dest_eth_addr[portid];
3451 /* parse application arguments (after the EAL ones) */
3452 ret = parse_args(argc, argv);
3454 rte_exit(EXIT_FAILURE, "Invalid L3FWD parameters\n");
3456 if (check_lcore_params() < 0)
3457 rte_exit(EXIT_FAILURE, "check_lcore_params failed\n");
3459 printf("Initializing rx-queues...\n");
3460 ret = init_rx_queues();
3462 rte_exit(EXIT_FAILURE, "init_rx_queues failed\n");
3464 printf("Initializing tx-threads...\n");
3465 ret = init_tx_threads();
3467 rte_exit(EXIT_FAILURE, "init_tx_threads failed\n");
3469 printf("Initializing rings...\n");
3470 ret = init_rx_rings();
3472 rte_exit(EXIT_FAILURE, "init_rx_rings failed\n");
3474 nb_ports = rte_eth_dev_count();
3475 if (nb_ports > RTE_MAX_ETHPORTS)
3476 nb_ports = RTE_MAX_ETHPORTS;
3478 if (check_port_config(nb_ports) < 0)
3479 rte_exit(EXIT_FAILURE, "check_port_config failed\n");
3481 nb_lcores = rte_lcore_count();
3483 /* initialize all ports */
3484 for (portid = 0; portid < nb_ports; portid++) {
3485 /* skip ports that are not enabled */
3486 if ((enabled_port_mask & (1 << portid)) == 0) {
3487 printf("\nSkipping disabled port %d\n", portid);
3492 printf("Initializing port %d ... ", portid);
3495 nb_rx_queue = get_port_n_rx_queues(portid);
3496 n_tx_queue = nb_lcores;
3497 if (n_tx_queue > MAX_TX_QUEUE_PER_PORT)
3498 n_tx_queue = MAX_TX_QUEUE_PER_PORT;
3499 printf("Creating queues: nb_rxq=%d nb_txq=%u... ",
3500 nb_rx_queue, (unsigned)n_tx_queue);
3501 ret = rte_eth_dev_configure(portid, nb_rx_queue,
3502 (uint16_t)n_tx_queue, &port_conf);
3504 rte_exit(EXIT_FAILURE, "Cannot configure device: err=%d, port=%d\n",
3507 rte_eth_macaddr_get(portid, &ports_eth_addr[portid]);
3508 print_ethaddr(" Address:", &ports_eth_addr[portid]);
3510 print_ethaddr("Destination:",
3511 (const struct ether_addr *)&dest_eth_addr[portid]);
3515 * prepare src MACs for each port.
3517 ether_addr_copy(&ports_eth_addr[portid],
3518 (struct ether_addr *)(val_eth + portid) + 1);
3521 ret = init_mem(NB_MBUF);
3523 rte_exit(EXIT_FAILURE, "init_mem failed\n");
3525 /* init one TX queue per couple (lcore,port) */
3527 for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
3528 if (rte_lcore_is_enabled(lcore_id) == 0)
3532 socketid = (uint8_t)rte_lcore_to_socket_id(lcore_id);
3536 printf("txq=%u,%d,%d ", lcore_id, queueid, socketid);
3539 rte_eth_dev_info_get(portid, &dev_info);
3540 txconf = &dev_info.default_txconf;
3541 if (port_conf.rxmode.jumbo_frame)
3542 txconf->txq_flags = 0;
3543 ret = rte_eth_tx_queue_setup(portid, queueid, nb_txd,
3546 rte_exit(EXIT_FAILURE, "rte_eth_tx_queue_setup: err=%d, "
3547 "port=%d\n", ret, portid);
3549 tx_thread[lcore_id].tx_queue_id[portid] = queueid;
3555 for (i = 0; i < n_rx_thread; i++) {
3556 lcore_id = rx_thread[i].conf.lcore_id;
3558 if (rte_lcore_is_enabled(lcore_id) == 0) {
3559 rte_exit(EXIT_FAILURE,
3560 "Cannot start Rx thread on lcore %u: lcore disabled\n",
3565 printf("\nInitializing rx queues for Rx thread %d on lcore %u ... ",
3569 /* init RX queues */
3570 for (queue = 0; queue < rx_thread[i].n_rx_queue; ++queue) {
3571 portid = rx_thread[i].rx_queue_list[queue].port_id;
3572 queueid = rx_thread[i].rx_queue_list[queue].queue_id;
3575 socketid = (uint8_t)rte_lcore_to_socket_id(lcore_id);
3579 printf("rxq=%d,%d,%d ", portid, queueid, socketid);
3582 ret = rte_eth_rx_queue_setup(portid, queueid, nb_rxd,
3585 pktmbuf_pool[socketid]);
3587 rte_exit(EXIT_FAILURE, "rte_eth_rx_queue_setup: err=%d, "
3588 "port=%d\n", ret, portid);
3595 for (portid = 0; portid < nb_ports; portid++) {
3596 if ((enabled_port_mask & (1 << portid)) == 0)
3600 ret = rte_eth_dev_start(portid);
3602 rte_exit(EXIT_FAILURE, "rte_eth_dev_start: err=%d, port=%d\n",
3606 * If enabled, put device in promiscuous mode.
3607 * This allows IO forwarding mode to forward packets
3608 * to itself through 2 cross-connected ports of the
3612 rte_eth_promiscuous_enable(portid);
3615 check_all_ports_link_status((uint8_t)nb_ports, enabled_port_mask);
3618 printf("Starting L-Threading Model\n");
3620 #if (APP_CPU_LOAD > 0)
3621 if (cpu_load_lcore_id > 0)
3622 /* Use one lcore for cpu load collector */
3626 lthread_num_schedulers_set(nb_lcores);
3627 rte_eal_mp_remote_launch(sched_spawner, NULL, SKIP_MASTER);
3628 lthread_master_spawner(NULL);
3631 printf("Starting P-Threading Model\n");
3632 /* launch per-lcore init on every lcore */
3633 rte_eal_mp_remote_launch(pthread_run, NULL, CALL_MASTER);
3634 RTE_LCORE_FOREACH_SLAVE(lcore_id) {
3635 if (rte_eal_wait_lcore(lcore_id) < 0)