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 (ENABLE_MULTI_BUFFER_OPTIMIZE == 1))
660 static inline __attribute__((always_inline)) void
661 send_packetsx4(uint8_t port,
662 struct rte_mbuf *m[], uint32_t num)
665 struct thread_tx_conf *qconf;
668 qconf = (struct thread_tx_conf *)lthread_get_data();
670 qconf = (struct thread_tx_conf *)RTE_PER_LCORE(lcore_conf)->data;
672 len = qconf->tx_mbufs[port].len;
675 * If TX buffer for that queue is empty, and we have enough packets,
676 * then send them straightway.
678 if (num >= MAX_TX_BURST && len == 0) {
679 n = rte_eth_tx_burst(port, qconf->tx_queue_id[port], m, num);
680 if (unlikely(n < num)) {
682 rte_pktmbuf_free(m[n]);
689 * Put packets into TX buffer for that queue.
693 n = (n > MAX_PKT_BURST) ? MAX_PKT_BURST - len : num;
696 switch (n % FWDSTEP) {
699 qconf->tx_mbufs[port].m_table[len + j] = m[j];
702 qconf->tx_mbufs[port].m_table[len + j] = m[j];
705 qconf->tx_mbufs[port].m_table[len + j] = m[j];
708 qconf->tx_mbufs[port].m_table[len + j] = m[j];
715 /* enough pkts to be sent */
716 if (unlikely(len == MAX_PKT_BURST)) {
718 send_burst(qconf, MAX_PKT_BURST, port);
720 /* copy rest of the packets into the TX buffer. */
723 switch (len % FWDSTEP) {
726 qconf->tx_mbufs[port].m_table[j] = m[n + j];
729 qconf->tx_mbufs[port].m_table[j] = m[n + j];
732 qconf->tx_mbufs[port].m_table[j] = m[n + j];
735 qconf->tx_mbufs[port].m_table[j] = m[n + j];
741 qconf->tx_mbufs[port].len = len;
743 #endif /* APP_LOOKUP_LPM */
745 #ifdef DO_RFC_1812_CHECKS
747 is_valid_ipv4_pkt(struct ipv4_hdr *pkt, uint32_t link_len)
749 /* From http://www.rfc-editor.org/rfc/rfc1812.txt section 5.2.2 */
751 * 1. The packet length reported by the Link Layer must be large
752 * enough to hold the minimum length legal IP datagram (20 bytes).
754 if (link_len < sizeof(struct ipv4_hdr))
757 /* 2. The IP checksum must be correct. */
758 /* this is checked in H/W */
761 * 3. The IP version number must be 4. If the version number is not 4
762 * then the packet may be another version of IP, such as IPng or
765 if (((pkt->version_ihl) >> 4) != 4)
768 * 4. The IP header length field must be large enough to hold the
769 * minimum length legal IP datagram (20 bytes = 5 words).
771 if ((pkt->version_ihl & 0xf) < 5)
775 * 5. The IP total length field must be large enough to hold the IP
776 * datagram header, whose length is specified in the IP header length
779 if (rte_cpu_to_be_16(pkt->total_length) < sizeof(struct ipv4_hdr))
786 #if (APP_LOOKUP_METHOD == APP_LOOKUP_EXACT_MATCH)
788 static __m128i mask0;
789 static __m128i mask1;
790 static __m128i mask2;
791 static inline uint8_t
792 get_ipv4_dst_port(void *ipv4_hdr, uint8_t portid,
793 lookup_struct_t *ipv4_l3fwd_lookup_struct)
796 union ipv4_5tuple_host key;
798 ipv4_hdr = (uint8_t *)ipv4_hdr + offsetof(struct ipv4_hdr, time_to_live);
799 __m128i data = _mm_loadu_si128((__m128i *)(ipv4_hdr));
800 /* Get 5 tuple: dst port, src port, dst IP address, src IP address and
802 key.xmm = _mm_and_si128(data, mask0);
803 /* Find destination port */
804 ret = rte_hash_lookup(ipv4_l3fwd_lookup_struct, (const void *)&key);
805 return (uint8_t)((ret < 0) ? portid : ipv4_l3fwd_out_if[ret]);
808 static inline uint8_t
809 get_ipv6_dst_port(void *ipv6_hdr, uint8_t portid,
810 lookup_struct_t *ipv6_l3fwd_lookup_struct)
813 union ipv6_5tuple_host key;
815 ipv6_hdr = (uint8_t *)ipv6_hdr + offsetof(struct ipv6_hdr, payload_len);
816 __m128i data0 = _mm_loadu_si128((__m128i *)(ipv6_hdr));
817 __m128i data1 = _mm_loadu_si128((__m128i *)(((uint8_t *)ipv6_hdr) +
819 __m128i data2 = _mm_loadu_si128((__m128i *)(((uint8_t *)ipv6_hdr) +
820 sizeof(__m128i) + sizeof(__m128i)));
821 /* Get part of 5 tuple: src IP address lower 96 bits and protocol */
822 key.xmm[0] = _mm_and_si128(data0, mask1);
823 /* Get part of 5 tuple: dst IP address lower 96 bits and src IP address
826 /* Get part of 5 tuple: dst port and src port and dst IP address higher
828 key.xmm[2] = _mm_and_si128(data2, mask2);
830 /* Find destination port */
831 ret = rte_hash_lookup(ipv6_l3fwd_lookup_struct, (const void *)&key);
832 return (uint8_t)((ret < 0) ? portid : ipv6_l3fwd_out_if[ret]);
836 #if (APP_LOOKUP_METHOD == APP_LOOKUP_LPM)
838 static inline uint8_t
839 get_ipv4_dst_port(void *ipv4_hdr, uint8_t portid,
840 lookup_struct_t *ipv4_l3fwd_lookup_struct)
844 return (uint8_t)((rte_lpm_lookup(ipv4_l3fwd_lookup_struct,
845 rte_be_to_cpu_32(((struct ipv4_hdr *)ipv4_hdr)->dst_addr),
846 &next_hop) == 0) ? next_hop : portid);
849 static inline uint8_t
850 get_ipv6_dst_port(void *ipv6_hdr, uint8_t portid,
851 lookup6_struct_t *ipv6_l3fwd_lookup_struct)
855 return (uint8_t) ((rte_lpm6_lookup(ipv6_l3fwd_lookup_struct,
856 ((struct ipv6_hdr *)ipv6_hdr)->dst_addr, &next_hop) == 0) ?
861 static inline void l3fwd_simple_forward(struct rte_mbuf *m, uint8_t portid)
862 __attribute__((unused));
864 #if ((APP_LOOKUP_METHOD == APP_LOOKUP_EXACT_MATCH) && \
865 (ENABLE_MULTI_BUFFER_OPTIMIZE == 1))
867 #define MASK_ALL_PKTS 0xff
868 #define EXCLUDE_1ST_PKT 0xfe
869 #define EXCLUDE_2ND_PKT 0xfd
870 #define EXCLUDE_3RD_PKT 0xfb
871 #define EXCLUDE_4TH_PKT 0xf7
872 #define EXCLUDE_5TH_PKT 0xef
873 #define EXCLUDE_6TH_PKT 0xdf
874 #define EXCLUDE_7TH_PKT 0xbf
875 #define EXCLUDE_8TH_PKT 0x7f
878 simple_ipv4_fwd_8pkts(struct rte_mbuf *m[8], uint8_t portid)
880 struct ether_hdr *eth_hdr[8];
881 struct ipv4_hdr *ipv4_hdr[8];
884 union ipv4_5tuple_host key[8];
887 eth_hdr[0] = rte_pktmbuf_mtod(m[0], struct ether_hdr *);
888 eth_hdr[1] = rte_pktmbuf_mtod(m[1], struct ether_hdr *);
889 eth_hdr[2] = rte_pktmbuf_mtod(m[2], struct ether_hdr *);
890 eth_hdr[3] = rte_pktmbuf_mtod(m[3], struct ether_hdr *);
891 eth_hdr[4] = rte_pktmbuf_mtod(m[4], struct ether_hdr *);
892 eth_hdr[5] = rte_pktmbuf_mtod(m[5], struct ether_hdr *);
893 eth_hdr[6] = rte_pktmbuf_mtod(m[6], struct ether_hdr *);
894 eth_hdr[7] = rte_pktmbuf_mtod(m[7], struct ether_hdr *);
896 /* Handle IPv4 headers.*/
897 ipv4_hdr[0] = rte_pktmbuf_mtod_offset(m[0], struct ipv4_hdr *,
898 sizeof(struct ether_hdr));
899 ipv4_hdr[1] = rte_pktmbuf_mtod_offset(m[1], struct ipv4_hdr *,
900 sizeof(struct ether_hdr));
901 ipv4_hdr[2] = rte_pktmbuf_mtod_offset(m[2], struct ipv4_hdr *,
902 sizeof(struct ether_hdr));
903 ipv4_hdr[3] = rte_pktmbuf_mtod_offset(m[3], struct ipv4_hdr *,
904 sizeof(struct ether_hdr));
905 ipv4_hdr[4] = rte_pktmbuf_mtod_offset(m[4], struct ipv4_hdr *,
906 sizeof(struct ether_hdr));
907 ipv4_hdr[5] = rte_pktmbuf_mtod_offset(m[5], struct ipv4_hdr *,
908 sizeof(struct ether_hdr));
909 ipv4_hdr[6] = rte_pktmbuf_mtod_offset(m[6], struct ipv4_hdr *,
910 sizeof(struct ether_hdr));
911 ipv4_hdr[7] = rte_pktmbuf_mtod_offset(m[7], struct ipv4_hdr *,
912 sizeof(struct ether_hdr));
914 #ifdef DO_RFC_1812_CHECKS
915 /* Check to make sure the packet is valid (RFC1812) */
916 uint8_t valid_mask = MASK_ALL_PKTS;
918 if (is_valid_ipv4_pkt(ipv4_hdr[0], m[0]->pkt_len) < 0) {
919 rte_pktmbuf_free(m[0]);
920 valid_mask &= EXCLUDE_1ST_PKT;
922 if (is_valid_ipv4_pkt(ipv4_hdr[1], m[1]->pkt_len) < 0) {
923 rte_pktmbuf_free(m[1]);
924 valid_mask &= EXCLUDE_2ND_PKT;
926 if (is_valid_ipv4_pkt(ipv4_hdr[2], m[2]->pkt_len) < 0) {
927 rte_pktmbuf_free(m[2]);
928 valid_mask &= EXCLUDE_3RD_PKT;
930 if (is_valid_ipv4_pkt(ipv4_hdr[3], m[3]->pkt_len) < 0) {
931 rte_pktmbuf_free(m[3]);
932 valid_mask &= EXCLUDE_4TH_PKT;
934 if (is_valid_ipv4_pkt(ipv4_hdr[4], m[4]->pkt_len) < 0) {
935 rte_pktmbuf_free(m[4]);
936 valid_mask &= EXCLUDE_5TH_PKT;
938 if (is_valid_ipv4_pkt(ipv4_hdr[5], m[5]->pkt_len) < 0) {
939 rte_pktmbuf_free(m[5]);
940 valid_mask &= EXCLUDE_6TH_PKT;
942 if (is_valid_ipv4_pkt(ipv4_hdr[6], m[6]->pkt_len) < 0) {
943 rte_pktmbuf_free(m[6]);
944 valid_mask &= EXCLUDE_7TH_PKT;
946 if (is_valid_ipv4_pkt(ipv4_hdr[7], m[7]->pkt_len) < 0) {
947 rte_pktmbuf_free(m[7]);
948 valid_mask &= EXCLUDE_8TH_PKT;
950 if (unlikely(valid_mask != MASK_ALL_PKTS)) {
956 for (i = 0; i < 8; i++)
957 if ((0x1 << i) & valid_mask)
958 l3fwd_simple_forward(m[i], portid);
960 #endif /* End of #ifdef DO_RFC_1812_CHECKS */
962 data[0] = _mm_loadu_si128(rte_pktmbuf_mtod_offset(m[0], __m128i *,
963 sizeof(struct ether_hdr) +
964 offsetof(struct ipv4_hdr, time_to_live)));
965 data[1] = _mm_loadu_si128(rte_pktmbuf_mtod_offset(m[1], __m128i *,
966 sizeof(struct ether_hdr) +
967 offsetof(struct ipv4_hdr, time_to_live)));
968 data[2] = _mm_loadu_si128(rte_pktmbuf_mtod_offset(m[2], __m128i *,
969 sizeof(struct ether_hdr) +
970 offsetof(struct ipv4_hdr, time_to_live)));
971 data[3] = _mm_loadu_si128(rte_pktmbuf_mtod_offset(m[3], __m128i *,
972 sizeof(struct ether_hdr) +
973 offsetof(struct ipv4_hdr, time_to_live)));
974 data[4] = _mm_loadu_si128(rte_pktmbuf_mtod_offset(m[4], __m128i *,
975 sizeof(struct ether_hdr) +
976 offsetof(struct ipv4_hdr, time_to_live)));
977 data[5] = _mm_loadu_si128(rte_pktmbuf_mtod_offset(m[5], __m128i *,
978 sizeof(struct ether_hdr) +
979 offsetof(struct ipv4_hdr, time_to_live)));
980 data[6] = _mm_loadu_si128(rte_pktmbuf_mtod_offset(m[6], __m128i *,
981 sizeof(struct ether_hdr) +
982 offsetof(struct ipv4_hdr, time_to_live)));
983 data[7] = _mm_loadu_si128(rte_pktmbuf_mtod_offset(m[7], __m128i *,
984 sizeof(struct ether_hdr) +
985 offsetof(struct ipv4_hdr, time_to_live)));
987 key[0].xmm = _mm_and_si128(data[0], mask0);
988 key[1].xmm = _mm_and_si128(data[1], mask0);
989 key[2].xmm = _mm_and_si128(data[2], mask0);
990 key[3].xmm = _mm_and_si128(data[3], mask0);
991 key[4].xmm = _mm_and_si128(data[4], mask0);
992 key[5].xmm = _mm_and_si128(data[5], mask0);
993 key[6].xmm = _mm_and_si128(data[6], mask0);
994 key[7].xmm = _mm_and_si128(data[7], mask0);
996 const void *key_array[8] = {&key[0], &key[1], &key[2], &key[3],
997 &key[4], &key[5], &key[6], &key[7]};
999 rte_hash_lookup_bulk(RTE_PER_LCORE(lcore_conf)->ipv4_lookup_struct,
1000 &key_array[0], 8, ret);
1001 dst_port[0] = (uint8_t) ((ret[0] < 0) ? portid : ipv4_l3fwd_out_if[ret[0]]);
1002 dst_port[1] = (uint8_t) ((ret[1] < 0) ? portid : ipv4_l3fwd_out_if[ret[1]]);
1003 dst_port[2] = (uint8_t) ((ret[2] < 0) ? portid : ipv4_l3fwd_out_if[ret[2]]);
1004 dst_port[3] = (uint8_t) ((ret[3] < 0) ? portid : ipv4_l3fwd_out_if[ret[3]]);
1005 dst_port[4] = (uint8_t) ((ret[4] < 0) ? portid : ipv4_l3fwd_out_if[ret[4]]);
1006 dst_port[5] = (uint8_t) ((ret[5] < 0) ? portid : ipv4_l3fwd_out_if[ret[5]]);
1007 dst_port[6] = (uint8_t) ((ret[6] < 0) ? portid : ipv4_l3fwd_out_if[ret[6]]);
1008 dst_port[7] = (uint8_t) ((ret[7] < 0) ? portid : ipv4_l3fwd_out_if[ret[7]]);
1010 if (dst_port[0] >= RTE_MAX_ETHPORTS ||
1011 (enabled_port_mask & 1 << dst_port[0]) == 0)
1012 dst_port[0] = portid;
1013 if (dst_port[1] >= RTE_MAX_ETHPORTS ||
1014 (enabled_port_mask & 1 << dst_port[1]) == 0)
1015 dst_port[1] = portid;
1016 if (dst_port[2] >= RTE_MAX_ETHPORTS ||
1017 (enabled_port_mask & 1 << dst_port[2]) == 0)
1018 dst_port[2] = portid;
1019 if (dst_port[3] >= RTE_MAX_ETHPORTS ||
1020 (enabled_port_mask & 1 << dst_port[3]) == 0)
1021 dst_port[3] = portid;
1022 if (dst_port[4] >= RTE_MAX_ETHPORTS ||
1023 (enabled_port_mask & 1 << dst_port[4]) == 0)
1024 dst_port[4] = portid;
1025 if (dst_port[5] >= RTE_MAX_ETHPORTS ||
1026 (enabled_port_mask & 1 << dst_port[5]) == 0)
1027 dst_port[5] = portid;
1028 if (dst_port[6] >= RTE_MAX_ETHPORTS ||
1029 (enabled_port_mask & 1 << dst_port[6]) == 0)
1030 dst_port[6] = portid;
1031 if (dst_port[7] >= RTE_MAX_ETHPORTS ||
1032 (enabled_port_mask & 1 << dst_port[7]) == 0)
1033 dst_port[7] = portid;
1035 #ifdef DO_RFC_1812_CHECKS
1036 /* Update time to live and header checksum */
1037 --(ipv4_hdr[0]->time_to_live);
1038 --(ipv4_hdr[1]->time_to_live);
1039 --(ipv4_hdr[2]->time_to_live);
1040 --(ipv4_hdr[3]->time_to_live);
1041 ++(ipv4_hdr[0]->hdr_checksum);
1042 ++(ipv4_hdr[1]->hdr_checksum);
1043 ++(ipv4_hdr[2]->hdr_checksum);
1044 ++(ipv4_hdr[3]->hdr_checksum);
1045 --(ipv4_hdr[4]->time_to_live);
1046 --(ipv4_hdr[5]->time_to_live);
1047 --(ipv4_hdr[6]->time_to_live);
1048 --(ipv4_hdr[7]->time_to_live);
1049 ++(ipv4_hdr[4]->hdr_checksum);
1050 ++(ipv4_hdr[5]->hdr_checksum);
1051 ++(ipv4_hdr[6]->hdr_checksum);
1052 ++(ipv4_hdr[7]->hdr_checksum);
1056 *(uint64_t *)ð_hdr[0]->d_addr = dest_eth_addr[dst_port[0]];
1057 *(uint64_t *)ð_hdr[1]->d_addr = dest_eth_addr[dst_port[1]];
1058 *(uint64_t *)ð_hdr[2]->d_addr = dest_eth_addr[dst_port[2]];
1059 *(uint64_t *)ð_hdr[3]->d_addr = dest_eth_addr[dst_port[3]];
1060 *(uint64_t *)ð_hdr[4]->d_addr = dest_eth_addr[dst_port[4]];
1061 *(uint64_t *)ð_hdr[5]->d_addr = dest_eth_addr[dst_port[5]];
1062 *(uint64_t *)ð_hdr[6]->d_addr = dest_eth_addr[dst_port[6]];
1063 *(uint64_t *)ð_hdr[7]->d_addr = dest_eth_addr[dst_port[7]];
1066 ether_addr_copy(&ports_eth_addr[dst_port[0]], ð_hdr[0]->s_addr);
1067 ether_addr_copy(&ports_eth_addr[dst_port[1]], ð_hdr[1]->s_addr);
1068 ether_addr_copy(&ports_eth_addr[dst_port[2]], ð_hdr[2]->s_addr);
1069 ether_addr_copy(&ports_eth_addr[dst_port[3]], ð_hdr[3]->s_addr);
1070 ether_addr_copy(&ports_eth_addr[dst_port[4]], ð_hdr[4]->s_addr);
1071 ether_addr_copy(&ports_eth_addr[dst_port[5]], ð_hdr[5]->s_addr);
1072 ether_addr_copy(&ports_eth_addr[dst_port[6]], ð_hdr[6]->s_addr);
1073 ether_addr_copy(&ports_eth_addr[dst_port[7]], ð_hdr[7]->s_addr);
1075 send_single_packet(m[0], (uint8_t)dst_port[0]);
1076 send_single_packet(m[1], (uint8_t)dst_port[1]);
1077 send_single_packet(m[2], (uint8_t)dst_port[2]);
1078 send_single_packet(m[3], (uint8_t)dst_port[3]);
1079 send_single_packet(m[4], (uint8_t)dst_port[4]);
1080 send_single_packet(m[5], (uint8_t)dst_port[5]);
1081 send_single_packet(m[6], (uint8_t)dst_port[6]);
1082 send_single_packet(m[7], (uint8_t)dst_port[7]);
1086 static inline void get_ipv6_5tuple(struct rte_mbuf *m0, __m128i mask0,
1087 __m128i mask1, union ipv6_5tuple_host *key)
1089 __m128i tmpdata0 = _mm_loadu_si128(rte_pktmbuf_mtod_offset(m0,
1090 __m128i *, sizeof(struct ether_hdr) +
1091 offsetof(struct ipv6_hdr, payload_len)));
1092 __m128i tmpdata1 = _mm_loadu_si128(rte_pktmbuf_mtod_offset(m0,
1093 __m128i *, sizeof(struct ether_hdr) +
1094 offsetof(struct ipv6_hdr, payload_len) + sizeof(__m128i)));
1095 __m128i tmpdata2 = _mm_loadu_si128(rte_pktmbuf_mtod_offset(m0,
1096 __m128i *, sizeof(struct ether_hdr) +
1097 offsetof(struct ipv6_hdr, payload_len) + sizeof(__m128i) +
1099 key->xmm[0] = _mm_and_si128(tmpdata0, mask0);
1100 key->xmm[1] = tmpdata1;
1101 key->xmm[2] = _mm_and_si128(tmpdata2, mask1);
1105 simple_ipv6_fwd_8pkts(struct rte_mbuf *m[8], uint8_t portid)
1108 uint8_t dst_port[8];
1109 struct ether_hdr *eth_hdr[8];
1110 union ipv6_5tuple_host key[8];
1112 __attribute__((unused)) struct ipv6_hdr *ipv6_hdr[8];
1114 eth_hdr[0] = rte_pktmbuf_mtod(m[0], struct ether_hdr *);
1115 eth_hdr[1] = rte_pktmbuf_mtod(m[1], struct ether_hdr *);
1116 eth_hdr[2] = rte_pktmbuf_mtod(m[2], struct ether_hdr *);
1117 eth_hdr[3] = rte_pktmbuf_mtod(m[3], struct ether_hdr *);
1118 eth_hdr[4] = rte_pktmbuf_mtod(m[4], struct ether_hdr *);
1119 eth_hdr[5] = rte_pktmbuf_mtod(m[5], struct ether_hdr *);
1120 eth_hdr[6] = rte_pktmbuf_mtod(m[6], struct ether_hdr *);
1121 eth_hdr[7] = rte_pktmbuf_mtod(m[7], struct ether_hdr *);
1123 /* Handle IPv6 headers.*/
1124 ipv6_hdr[0] = rte_pktmbuf_mtod_offset(m[0], struct ipv6_hdr *,
1125 sizeof(struct ether_hdr));
1126 ipv6_hdr[1] = rte_pktmbuf_mtod_offset(m[1], struct ipv6_hdr *,
1127 sizeof(struct ether_hdr));
1128 ipv6_hdr[2] = rte_pktmbuf_mtod_offset(m[2], struct ipv6_hdr *,
1129 sizeof(struct ether_hdr));
1130 ipv6_hdr[3] = rte_pktmbuf_mtod_offset(m[3], struct ipv6_hdr *,
1131 sizeof(struct ether_hdr));
1132 ipv6_hdr[4] = rte_pktmbuf_mtod_offset(m[4], struct ipv6_hdr *,
1133 sizeof(struct ether_hdr));
1134 ipv6_hdr[5] = rte_pktmbuf_mtod_offset(m[5], struct ipv6_hdr *,
1135 sizeof(struct ether_hdr));
1136 ipv6_hdr[6] = rte_pktmbuf_mtod_offset(m[6], struct ipv6_hdr *,
1137 sizeof(struct ether_hdr));
1138 ipv6_hdr[7] = rte_pktmbuf_mtod_offset(m[7], struct ipv6_hdr *,
1139 sizeof(struct ether_hdr));
1141 get_ipv6_5tuple(m[0], mask1, mask2, &key[0]);
1142 get_ipv6_5tuple(m[1], mask1, mask2, &key[1]);
1143 get_ipv6_5tuple(m[2], mask1, mask2, &key[2]);
1144 get_ipv6_5tuple(m[3], mask1, mask2, &key[3]);
1145 get_ipv6_5tuple(m[4], mask1, mask2, &key[4]);
1146 get_ipv6_5tuple(m[5], mask1, mask2, &key[5]);
1147 get_ipv6_5tuple(m[6], mask1, mask2, &key[6]);
1148 get_ipv6_5tuple(m[7], mask1, mask2, &key[7]);
1150 const void *key_array[8] = {&key[0], &key[1], &key[2], &key[3],
1151 &key[4], &key[5], &key[6], &key[7]};
1153 rte_hash_lookup_bulk(RTE_PER_LCORE(lcore_conf)->ipv6_lookup_struct,
1154 &key_array[0], 4, ret);
1155 dst_port[0] = (uint8_t) ((ret[0] < 0) ? portid : ipv6_l3fwd_out_if[ret[0]]);
1156 dst_port[1] = (uint8_t) ((ret[1] < 0) ? portid : ipv6_l3fwd_out_if[ret[1]]);
1157 dst_port[2] = (uint8_t) ((ret[2] < 0) ? portid : ipv6_l3fwd_out_if[ret[2]]);
1158 dst_port[3] = (uint8_t) ((ret[3] < 0) ? portid : ipv6_l3fwd_out_if[ret[3]]);
1159 dst_port[4] = (uint8_t) ((ret[4] < 0) ? portid : ipv6_l3fwd_out_if[ret[4]]);
1160 dst_port[5] = (uint8_t) ((ret[5] < 0) ? portid : ipv6_l3fwd_out_if[ret[5]]);
1161 dst_port[6] = (uint8_t) ((ret[6] < 0) ? portid : ipv6_l3fwd_out_if[ret[6]]);
1162 dst_port[7] = (uint8_t) ((ret[7] < 0) ? portid : ipv6_l3fwd_out_if[ret[7]]);
1164 if (dst_port[0] >= RTE_MAX_ETHPORTS ||
1165 (enabled_port_mask & 1 << dst_port[0]) == 0)
1166 dst_port[0] = portid;
1167 if (dst_port[1] >= RTE_MAX_ETHPORTS ||
1168 (enabled_port_mask & 1 << dst_port[1]) == 0)
1169 dst_port[1] = portid;
1170 if (dst_port[2] >= RTE_MAX_ETHPORTS ||
1171 (enabled_port_mask & 1 << dst_port[2]) == 0)
1172 dst_port[2] = portid;
1173 if (dst_port[3] >= RTE_MAX_ETHPORTS ||
1174 (enabled_port_mask & 1 << dst_port[3]) == 0)
1175 dst_port[3] = portid;
1176 if (dst_port[4] >= RTE_MAX_ETHPORTS ||
1177 (enabled_port_mask & 1 << dst_port[4]) == 0)
1178 dst_port[4] = portid;
1179 if (dst_port[5] >= RTE_MAX_ETHPORTS ||
1180 (enabled_port_mask & 1 << dst_port[5]) == 0)
1181 dst_port[5] = portid;
1182 if (dst_port[6] >= RTE_MAX_ETHPORTS ||
1183 (enabled_port_mask & 1 << dst_port[6]) == 0)
1184 dst_port[6] = portid;
1185 if (dst_port[7] >= RTE_MAX_ETHPORTS ||
1186 (enabled_port_mask & 1 << dst_port[7]) == 0)
1187 dst_port[7] = portid;
1190 *(uint64_t *)ð_hdr[0]->d_addr = dest_eth_addr[dst_port[0]];
1191 *(uint64_t *)ð_hdr[1]->d_addr = dest_eth_addr[dst_port[1]];
1192 *(uint64_t *)ð_hdr[2]->d_addr = dest_eth_addr[dst_port[2]];
1193 *(uint64_t *)ð_hdr[3]->d_addr = dest_eth_addr[dst_port[3]];
1194 *(uint64_t *)ð_hdr[4]->d_addr = dest_eth_addr[dst_port[4]];
1195 *(uint64_t *)ð_hdr[5]->d_addr = dest_eth_addr[dst_port[5]];
1196 *(uint64_t *)ð_hdr[6]->d_addr = dest_eth_addr[dst_port[6]];
1197 *(uint64_t *)ð_hdr[7]->d_addr = dest_eth_addr[dst_port[7]];
1200 ether_addr_copy(&ports_eth_addr[dst_port[0]], ð_hdr[0]->s_addr);
1201 ether_addr_copy(&ports_eth_addr[dst_port[1]], ð_hdr[1]->s_addr);
1202 ether_addr_copy(&ports_eth_addr[dst_port[2]], ð_hdr[2]->s_addr);
1203 ether_addr_copy(&ports_eth_addr[dst_port[3]], ð_hdr[3]->s_addr);
1204 ether_addr_copy(&ports_eth_addr[dst_port[4]], ð_hdr[4]->s_addr);
1205 ether_addr_copy(&ports_eth_addr[dst_port[5]], ð_hdr[5]->s_addr);
1206 ether_addr_copy(&ports_eth_addr[dst_port[6]], ð_hdr[6]->s_addr);
1207 ether_addr_copy(&ports_eth_addr[dst_port[7]], ð_hdr[7]->s_addr);
1209 send_single_packet(m[0], (uint8_t)dst_port[0]);
1210 send_single_packet(m[1], (uint8_t)dst_port[1]);
1211 send_single_packet(m[2], (uint8_t)dst_port[2]);
1212 send_single_packet(m[3], (uint8_t)dst_port[3]);
1213 send_single_packet(m[4], (uint8_t)dst_port[4]);
1214 send_single_packet(m[5], (uint8_t)dst_port[5]);
1215 send_single_packet(m[6], (uint8_t)dst_port[6]);
1216 send_single_packet(m[7], (uint8_t)dst_port[7]);
1219 #endif /* APP_LOOKUP_METHOD */
1221 static inline __attribute__((always_inline)) void
1222 l3fwd_simple_forward(struct rte_mbuf *m, uint8_t portid)
1224 struct ether_hdr *eth_hdr;
1225 struct ipv4_hdr *ipv4_hdr;
1228 eth_hdr = rte_pktmbuf_mtod(m, struct ether_hdr *);
1230 if (RTE_ETH_IS_IPV4_HDR(m->packet_type)) {
1231 /* Handle IPv4 headers.*/
1232 ipv4_hdr = rte_pktmbuf_mtod_offset(m, struct ipv4_hdr *,
1233 sizeof(struct ether_hdr));
1235 #ifdef DO_RFC_1812_CHECKS
1236 /* Check to make sure the packet is valid (RFC1812) */
1237 if (is_valid_ipv4_pkt(ipv4_hdr, m->pkt_len) < 0) {
1238 rte_pktmbuf_free(m);
1243 dst_port = get_ipv4_dst_port(ipv4_hdr, portid,
1244 RTE_PER_LCORE(lcore_conf)->ipv4_lookup_struct);
1245 if (dst_port >= RTE_MAX_ETHPORTS ||
1246 (enabled_port_mask & 1 << dst_port) == 0)
1249 #ifdef DO_RFC_1812_CHECKS
1250 /* Update time to live and header checksum */
1251 --(ipv4_hdr->time_to_live);
1252 ++(ipv4_hdr->hdr_checksum);
1255 *(uint64_t *)ð_hdr->d_addr = dest_eth_addr[dst_port];
1258 ether_addr_copy(&ports_eth_addr[dst_port], ð_hdr->s_addr);
1260 send_single_packet(m, dst_port);
1261 } else if (RTE_ETH_IS_IPV6_HDR(m->packet_type)) {
1262 /* Handle IPv6 headers.*/
1263 struct ipv6_hdr *ipv6_hdr;
1265 ipv6_hdr = rte_pktmbuf_mtod_offset(m, struct ipv6_hdr *,
1266 sizeof(struct ether_hdr));
1268 dst_port = get_ipv6_dst_port(ipv6_hdr, portid,
1269 RTE_PER_LCORE(lcore_conf)->ipv6_lookup_struct);
1271 if (dst_port >= RTE_MAX_ETHPORTS ||
1272 (enabled_port_mask & 1 << dst_port) == 0)
1276 *(uint64_t *)ð_hdr->d_addr = dest_eth_addr[dst_port];
1279 ether_addr_copy(&ports_eth_addr[dst_port], ð_hdr->s_addr);
1281 send_single_packet(m, dst_port);
1283 /* Free the mbuf that contains non-IPV4/IPV6 packet */
1284 rte_pktmbuf_free(m);
1287 #if ((APP_LOOKUP_METHOD == APP_LOOKUP_LPM) && \
1288 (ENABLE_MULTI_BUFFER_OPTIMIZE == 1))
1289 #ifdef DO_RFC_1812_CHECKS
1291 #define IPV4_MIN_VER_IHL 0x45
1292 #define IPV4_MAX_VER_IHL 0x4f
1293 #define IPV4_MAX_VER_IHL_DIFF (IPV4_MAX_VER_IHL - IPV4_MIN_VER_IHL)
1295 /* Minimum value of IPV4 total length (20B) in network byte order. */
1296 #define IPV4_MIN_LEN_BE (sizeof(struct ipv4_hdr) << 8)
1299 * From http://www.rfc-editor.org/rfc/rfc1812.txt section 5.2.2:
1300 * - The IP version number must be 4.
1301 * - The IP header length field must be large enough to hold the
1302 * minimum length legal IP datagram (20 bytes = 5 words).
1303 * - The IP total length field must be large enough to hold the IP
1304 * datagram header, whose length is specified in the IP header length
1306 * If we encounter invalid IPV4 packet, then set destination port for it
1307 * to BAD_PORT value.
1309 static inline __attribute__((always_inline)) void
1310 rfc1812_process(struct ipv4_hdr *ipv4_hdr, uint32_t *dp, uint32_t ptype)
1314 if (RTE_ETH_IS_IPV4_HDR(ptype)) {
1315 ihl = ipv4_hdr->version_ihl - IPV4_MIN_VER_IHL;
1317 ipv4_hdr->time_to_live--;
1318 ipv4_hdr->hdr_checksum++;
1320 if (ihl > IPV4_MAX_VER_IHL_DIFF ||
1321 ((uint8_t)ipv4_hdr->total_length == 0 &&
1322 ipv4_hdr->total_length < IPV4_MIN_LEN_BE)) {
1329 #define rfc1812_process(mb, dp) do { } while (0)
1330 #endif /* DO_RFC_1812_CHECKS */
1331 #endif /* APP_LOOKUP_LPM && ENABLE_MULTI_BUFFER_OPTIMIZE */
1334 #if ((APP_LOOKUP_METHOD == APP_LOOKUP_LPM) && \
1335 (ENABLE_MULTI_BUFFER_OPTIMIZE == 1))
1337 static inline __attribute__((always_inline)) uint16_t
1338 get_dst_port(struct rte_mbuf *pkt, uint32_t dst_ipv4, uint8_t portid)
1340 uint32_t next_hop_ipv4;
1341 uint8_t next_hop_ipv6;
1342 struct ipv6_hdr *ipv6_hdr;
1343 struct ether_hdr *eth_hdr;
1345 if (RTE_ETH_IS_IPV4_HDR(pkt->packet_type)) {
1346 if (rte_lpm_lookup(RTE_PER_LCORE(lcore_conf)->ipv4_lookup_struct,
1347 dst_ipv4, &next_hop_ipv4) != 0) {
1348 next_hop_ipv4 = portid;
1349 return next_hop_ipv4;
1351 } else if (RTE_ETH_IS_IPV6_HDR(pkt->packet_type)) {
1352 eth_hdr = rte_pktmbuf_mtod(pkt, struct ether_hdr *);
1353 ipv6_hdr = (struct ipv6_hdr *)(eth_hdr + 1);
1354 if (rte_lpm6_lookup(RTE_PER_LCORE(lcore_conf)->ipv6_lookup_struct,
1355 ipv6_hdr->dst_addr, &next_hop_ipv6) != 0) {
1356 next_hop_ipv6 = portid;
1357 return next_hop_ipv6;
1360 next_hop_ipv4 = portid;
1361 return next_hop_ipv4;
1367 process_packet(struct rte_mbuf *pkt, uint32_t *dst_port, uint8_t portid)
1369 struct ether_hdr *eth_hdr;
1370 struct ipv4_hdr *ipv4_hdr;
1375 eth_hdr = rte_pktmbuf_mtod(pkt, struct ether_hdr *);
1376 ipv4_hdr = (struct ipv4_hdr *)(eth_hdr + 1);
1378 dst_ipv4 = ipv4_hdr->dst_addr;
1379 dst_ipv4 = rte_be_to_cpu_32(dst_ipv4);
1380 dp = get_dst_port(pkt, dst_ipv4, portid);
1382 te = _mm_load_si128((__m128i *)eth_hdr);
1386 rfc1812_process(ipv4_hdr, dst_port, pkt->packet_type);
1388 te = _mm_blend_epi16(te, ve, MASK_ETH);
1389 _mm_store_si128((__m128i *)eth_hdr, te);
1393 * Read packet_type and destination IPV4 addresses from 4 mbufs.
1396 processx4_step1(struct rte_mbuf *pkt[FWDSTEP],
1398 uint32_t *ipv4_flag)
1400 struct ipv4_hdr *ipv4_hdr;
1401 struct ether_hdr *eth_hdr;
1402 uint32_t x0, x1, x2, x3;
1404 eth_hdr = rte_pktmbuf_mtod(pkt[0], struct ether_hdr *);
1405 ipv4_hdr = (struct ipv4_hdr *)(eth_hdr + 1);
1406 x0 = ipv4_hdr->dst_addr;
1407 ipv4_flag[0] = pkt[0]->packet_type & RTE_PTYPE_L3_IPV4;
1409 eth_hdr = rte_pktmbuf_mtod(pkt[1], struct ether_hdr *);
1410 ipv4_hdr = (struct ipv4_hdr *)(eth_hdr + 1);
1411 x1 = ipv4_hdr->dst_addr;
1412 ipv4_flag[0] &= pkt[1]->packet_type;
1414 eth_hdr = rte_pktmbuf_mtod(pkt[2], struct ether_hdr *);
1415 ipv4_hdr = (struct ipv4_hdr *)(eth_hdr + 1);
1416 x2 = ipv4_hdr->dst_addr;
1417 ipv4_flag[0] &= pkt[2]->packet_type;
1419 eth_hdr = rte_pktmbuf_mtod(pkt[3], struct ether_hdr *);
1420 ipv4_hdr = (struct ipv4_hdr *)(eth_hdr + 1);
1421 x3 = ipv4_hdr->dst_addr;
1422 ipv4_flag[0] &= pkt[3]->packet_type;
1424 dip[0] = _mm_set_epi32(x3, x2, x1, x0);
1428 * Lookup into LPM for destination port.
1429 * If lookup fails, use incoming port (portid) as destination port.
1432 processx4_step2(__m128i dip,
1435 struct rte_mbuf *pkt[FWDSTEP],
1436 uint32_t dprt[FWDSTEP])
1439 const __m128i bswap_mask = _mm_set_epi8(12, 13, 14, 15, 8, 9, 10, 11,
1440 4, 5, 6, 7, 0, 1, 2, 3);
1442 /* Byte swap 4 IPV4 addresses. */
1443 dip = _mm_shuffle_epi8(dip, bswap_mask);
1445 /* if all 4 packets are IPV4. */
1446 if (likely(ipv4_flag)) {
1447 rte_lpm_lookupx4(RTE_PER_LCORE(lcore_conf)->ipv4_lookup_struct, dip,
1451 dprt[0] = get_dst_port(pkt[0], dst.u32[0], portid);
1452 dprt[1] = get_dst_port(pkt[1], dst.u32[1], portid);
1453 dprt[2] = get_dst_port(pkt[2], dst.u32[2], portid);
1454 dprt[3] = get_dst_port(pkt[3], dst.u32[3], portid);
1459 * Update source and destination MAC addresses in the ethernet header.
1460 * Perform RFC1812 checks and updates for IPV4 packets.
1463 processx4_step3(struct rte_mbuf *pkt[FWDSTEP], uint32_t dst_port[FWDSTEP])
1465 __m128i te[FWDSTEP];
1466 __m128i ve[FWDSTEP];
1467 __m128i *p[FWDSTEP];
1469 p[0] = rte_pktmbuf_mtod(pkt[0], __m128i *);
1470 p[1] = rte_pktmbuf_mtod(pkt[1], __m128i *);
1471 p[2] = rte_pktmbuf_mtod(pkt[2], __m128i *);
1472 p[3] = rte_pktmbuf_mtod(pkt[3], __m128i *);
1474 ve[0] = val_eth[dst_port[0]];
1475 te[0] = _mm_load_si128(p[0]);
1477 ve[1] = val_eth[dst_port[1]];
1478 te[1] = _mm_load_si128(p[1]);
1480 ve[2] = val_eth[dst_port[2]];
1481 te[2] = _mm_load_si128(p[2]);
1483 ve[3] = val_eth[dst_port[3]];
1484 te[3] = _mm_load_si128(p[3]);
1486 /* Update first 12 bytes, keep rest bytes intact. */
1487 te[0] = _mm_blend_epi16(te[0], ve[0], MASK_ETH);
1488 te[1] = _mm_blend_epi16(te[1], ve[1], MASK_ETH);
1489 te[2] = _mm_blend_epi16(te[2], ve[2], MASK_ETH);
1490 te[3] = _mm_blend_epi16(te[3], ve[3], MASK_ETH);
1492 _mm_store_si128(p[0], te[0]);
1493 _mm_store_si128(p[1], te[1]);
1494 _mm_store_si128(p[2], te[2]);
1495 _mm_store_si128(p[3], te[3]);
1497 rfc1812_process((struct ipv4_hdr *)((struct ether_hdr *)p[0] + 1),
1498 &dst_port[0], pkt[0]->packet_type);
1499 rfc1812_process((struct ipv4_hdr *)((struct ether_hdr *)p[1] + 1),
1500 &dst_port[1], pkt[1]->packet_type);
1501 rfc1812_process((struct ipv4_hdr *)((struct ether_hdr *)p[2] + 1),
1502 &dst_port[2], pkt[2]->packet_type);
1503 rfc1812_process((struct ipv4_hdr *)((struct ether_hdr *)p[3] + 1),
1504 &dst_port[3], pkt[3]->packet_type);
1508 * We group consecutive packets with the same destionation port into one burst.
1509 * To avoid extra latency this is done together with some other packet
1510 * processing, but after we made a final decision about packet's destination.
1511 * To do this we maintain:
1512 * pnum - array of number of consecutive packets with the same dest port for
1513 * each packet in the input burst.
1514 * lp - pointer to the last updated element in the pnum.
1515 * dlp - dest port value lp corresponds to.
1518 #define GRPSZ (1 << FWDSTEP)
1519 #define GRPMSK (GRPSZ - 1)
1521 #define GROUP_PORT_STEP(dlp, dcp, lp, pn, idx) do { \
1522 if (likely((dlp) == (dcp)[(idx)])) { \
1525 (dlp) = (dcp)[idx]; \
1526 (lp) = (pn) + (idx); \
1532 * Group consecutive packets with the same destination port in bursts of 4.
1533 * Suppose we have array of destionation ports:
1534 * dst_port[] = {a, b, c, d,, e, ... }
1535 * dp1 should contain: <a, b, c, d>, dp2: <b, c, d, e>.
1536 * We doing 4 comparisions at once and the result is 4 bit mask.
1537 * This mask is used as an index into prebuild array of pnum values.
1539 static inline uint16_t *
1540 port_groupx4(uint16_t pn[FWDSTEP + 1], uint16_t *lp, __m128i dp1, __m128i dp2)
1542 static const struct {
1543 uint64_t pnum; /* prebuild 4 values for pnum[]. */
1544 int32_t idx; /* index for new last updated elemnet. */
1545 uint16_t lpv; /* add value to the last updated element. */
1548 /* 0: a != b, b != c, c != d, d != e */
1549 .pnum = UINT64_C(0x0001000100010001),
1554 /* 1: a == b, b != c, c != d, d != e */
1555 .pnum = UINT64_C(0x0001000100010002),
1560 /* 2: a != b, b == c, c != d, d != e */
1561 .pnum = UINT64_C(0x0001000100020001),
1566 /* 3: a == b, b == c, c != d, d != e */
1567 .pnum = UINT64_C(0x0001000100020003),
1572 /* 4: a != b, b != c, c == d, d != e */
1573 .pnum = UINT64_C(0x0001000200010001),
1578 /* 5: a == b, b != c, c == d, d != e */
1579 .pnum = UINT64_C(0x0001000200010002),
1584 /* 6: a != b, b == c, c == d, d != e */
1585 .pnum = UINT64_C(0x0001000200030001),
1590 /* 7: a == b, b == c, c == d, d != e */
1591 .pnum = UINT64_C(0x0001000200030004),
1596 /* 8: a != b, b != c, c != d, d == e */
1597 .pnum = UINT64_C(0x0002000100010001),
1602 /* 9: a == b, b != c, c != d, d == e */
1603 .pnum = UINT64_C(0x0002000100010002),
1608 /* 0xa: a != b, b == c, c != d, d == e */
1609 .pnum = UINT64_C(0x0002000100020001),
1614 /* 0xb: a == b, b == c, c != d, d == e */
1615 .pnum = UINT64_C(0x0002000100020003),
1620 /* 0xc: a != b, b != c, c == d, d == e */
1621 .pnum = UINT64_C(0x0002000300010001),
1626 /* 0xd: a == b, b != c, c == d, d == e */
1627 .pnum = UINT64_C(0x0002000300010002),
1632 /* 0xe: a != b, b == c, c == d, d == e */
1633 .pnum = UINT64_C(0x0002000300040001),
1638 /* 0xf: a == b, b == c, c == d, d == e */
1639 .pnum = UINT64_C(0x0002000300040005),
1646 uint16_t u16[FWDSTEP + 1];
1648 } *pnum = (void *)pn;
1652 dp1 = _mm_cmpeq_epi16(dp1, dp2);
1653 dp1 = _mm_unpacklo_epi16(dp1, dp1);
1654 v = _mm_movemask_ps((__m128)dp1);
1656 /* update last port counter. */
1657 lp[0] += gptbl[v].lpv;
1659 /* if dest port value has changed. */
1661 pnum->u64 = gptbl[v].pnum;
1662 pnum->u16[FWDSTEP] = 1;
1663 lp = pnum->u16 + gptbl[v].idx;
1669 #endif /* APP_LOOKUP_METHOD */
1672 process_burst(struct rte_mbuf *pkts_burst[MAX_PKT_BURST], int nb_rx,
1677 #if ((APP_LOOKUP_METHOD == APP_LOOKUP_LPM) && \
1678 (ENABLE_MULTI_BUFFER_OPTIMIZE == 1))
1682 uint32_t dst_port[MAX_PKT_BURST];
1683 __m128i dip[MAX_PKT_BURST / FWDSTEP];
1684 uint32_t ipv4_flag[MAX_PKT_BURST / FWDSTEP];
1685 uint16_t pnum[MAX_PKT_BURST + 1];
1689 #if (ENABLE_MULTI_BUFFER_OPTIMIZE == 1)
1690 #if (APP_LOOKUP_METHOD == APP_LOOKUP_EXACT_MATCH)
1693 * Send nb_rx - nb_rx%8 packets
1696 int32_t n = RTE_ALIGN_FLOOR(nb_rx, 8);
1698 for (j = 0; j < n; j += 8) {
1700 pkts_burst[j]->packet_type &
1701 pkts_burst[j+1]->packet_type &
1702 pkts_burst[j+2]->packet_type &
1703 pkts_burst[j+3]->packet_type &
1704 pkts_burst[j+4]->packet_type &
1705 pkts_burst[j+5]->packet_type &
1706 pkts_burst[j+6]->packet_type &
1707 pkts_burst[j+7]->packet_type;
1708 if (pkt_type & RTE_PTYPE_L3_IPV4) {
1709 simple_ipv4_fwd_8pkts(&pkts_burst[j], portid);
1710 } else if (pkt_type &
1711 RTE_PTYPE_L3_IPV6) {
1712 simple_ipv6_fwd_8pkts(&pkts_burst[j], portid);
1714 l3fwd_simple_forward(pkts_burst[j], portid);
1715 l3fwd_simple_forward(pkts_burst[j+1], portid);
1716 l3fwd_simple_forward(pkts_burst[j+2], portid);
1717 l3fwd_simple_forward(pkts_burst[j+3], portid);
1718 l3fwd_simple_forward(pkts_burst[j+4], portid);
1719 l3fwd_simple_forward(pkts_burst[j+5], portid);
1720 l3fwd_simple_forward(pkts_burst[j+6], portid);
1721 l3fwd_simple_forward(pkts_burst[j+7], portid);
1724 for (; j < nb_rx ; j++)
1725 l3fwd_simple_forward(pkts_burst[j], portid);
1727 #elif (APP_LOOKUP_METHOD == APP_LOOKUP_LPM)
1729 k = RTE_ALIGN_FLOOR(nb_rx, FWDSTEP);
1730 for (j = 0; j != k; j += FWDSTEP)
1731 processx4_step1(&pkts_burst[j], &dip[j / FWDSTEP],
1732 &ipv4_flag[j / FWDSTEP]);
1734 k = RTE_ALIGN_FLOOR(nb_rx, FWDSTEP);
1735 for (j = 0; j != k; j += FWDSTEP)
1736 processx4_step2(dip[j / FWDSTEP], ipv4_flag[j / FWDSTEP],
1737 portid, &pkts_burst[j], &dst_port[j]);
1740 * Finish packet processing and group consecutive
1741 * packets with the same destination port.
1743 k = RTE_ALIGN_FLOOR(nb_rx, FWDSTEP);
1750 processx4_step3(pkts_burst, dst_port);
1752 /* dp1: <d[0], d[1], d[2], d[3], ... > */
1753 dp1 = _mm_loadu_si128((__m128i *)dst_port);
1755 for (j = FWDSTEP; j != k; j += FWDSTEP) {
1756 processx4_step3(&pkts_burst[j], &dst_port[j]);
1760 * <d[j-3], d[j-2], d[j-1], d[j], ... >
1762 dp2 = _mm_loadu_si128(
1763 (__m128i *)&dst_port[j - FWDSTEP + 1]);
1764 lp = port_groupx4(&pnum[j - FWDSTEP], lp, dp1, dp2);
1768 * <d[j], d[j+1], d[j+2], d[j+3], ... >
1770 dp1 = _mm_srli_si128(dp2, (FWDSTEP - 1) *
1771 sizeof(dst_port[0]));
1775 * dp2: <d[j-3], d[j-2], d[j-1], d[j-1], ... >
1777 dp2 = _mm_shufflelo_epi16(dp1, 0xf9);
1778 lp = port_groupx4(&pnum[j - FWDSTEP], lp, dp1, dp2);
1781 * remove values added by the last repeated
1785 dlp = dst_port[j - 1];
1787 /* set dlp and lp to the never used values. */
1789 lp = pnum + MAX_PKT_BURST;
1792 /* Process up to last 3 packets one by one. */
1793 switch (nb_rx % FWDSTEP) {
1795 process_packet(pkts_burst[j], dst_port + j, portid);
1796 GROUP_PORT_STEP(dlp, dst_port, lp, pnum, j);
1799 process_packet(pkts_burst[j], dst_port + j, portid);
1800 GROUP_PORT_STEP(dlp, dst_port, lp, pnum, j);
1803 process_packet(pkts_burst[j], dst_port + j, portid);
1804 GROUP_PORT_STEP(dlp, dst_port, lp, pnum, j);
1809 * Send packets out, through destination port.
1810 * Consecuteve pacekts with the same destination port
1811 * are already grouped together.
1812 * If destination port for the packet equals BAD_PORT,
1813 * then free the packet without sending it out.
1815 for (j = 0; j < nb_rx; j += k) {
1823 if (likely(pn != BAD_PORT))
1824 send_packetsx4(pn, pkts_burst + j, k);
1826 for (m = j; m != j + k; m++)
1827 rte_pktmbuf_free(pkts_burst[m]);
1831 #endif /* APP_LOOKUP_METHOD */
1832 #else /* ENABLE_MULTI_BUFFER_OPTIMIZE == 0 */
1834 /* Prefetch first packets */
1835 for (j = 0; j < PREFETCH_OFFSET && j < nb_rx; j++)
1836 rte_prefetch0(rte_pktmbuf_mtod(pkts_burst[j], void *));
1838 /* Prefetch and forward already prefetched packets */
1839 for (j = 0; j < (nb_rx - PREFETCH_OFFSET); j++) {
1840 rte_prefetch0(rte_pktmbuf_mtod(pkts_burst[
1841 j + PREFETCH_OFFSET], void *));
1842 l3fwd_simple_forward(pkts_burst[j], portid);
1845 /* Forward remaining prefetched packets */
1846 for (; j < nb_rx; j++)
1847 l3fwd_simple_forward(pkts_burst[j], portid);
1849 #endif /* ENABLE_MULTI_BUFFER_OPTIMIZE */
1853 #if (APP_CPU_LOAD > 0)
1856 * CPU-load stats collector
1859 cpu_load_collector(__rte_unused void *arg) {
1862 uint64_t prev_tsc, diff_tsc, cur_tsc;
1863 uint64_t total[MAX_CPU] = { 0 };
1864 unsigned min_cpu = MAX_CPU;
1865 unsigned max_cpu = 0;
1870 unsigned int n_thread_per_cpu[MAX_CPU] = { 0 };
1871 struct thread_conf *thread_per_cpu[MAX_CPU][MAX_THREAD];
1873 struct thread_conf *thread_conf;
1875 const uint64_t interval_tsc = (rte_get_tsc_hz() + US_PER_S - 1) /
1876 US_PER_S * CPU_LOAD_TIMEOUT_US;
1880 * Wait for all threads
1883 printf("Waiting for %d rx threads and %d tx threads\n", n_rx_thread,
1886 while (rte_atomic16_read(&rx_counter) < n_rx_thread)
1889 while (rte_atomic16_read(&tx_counter) < n_tx_thread)
1892 for (i = 0; i < n_rx_thread; i++) {
1894 thread_conf = &rx_thread[i].conf;
1895 cpu_id = thread_conf->cpu_id;
1896 thread_per_cpu[cpu_id][n_thread_per_cpu[cpu_id]++] = thread_conf;
1898 if (cpu_id > max_cpu)
1900 if (cpu_id < min_cpu)
1903 for (i = 0; i < n_tx_thread; i++) {
1905 thread_conf = &tx_thread[i].conf;
1906 cpu_id = thread_conf->cpu_id;
1907 thread_per_cpu[cpu_id][n_thread_per_cpu[cpu_id]++] = thread_conf;
1909 if (thread_conf->cpu_id > max_cpu)
1910 max_cpu = thread_conf->cpu_id;
1911 if (thread_conf->cpu_id < min_cpu)
1912 min_cpu = thread_conf->cpu_id;
1918 for (i = min_cpu; i <= max_cpu; i++) {
1919 for (j = 0; j < MAX_CPU_COUNTER; j++) {
1920 for (k = 0; k < n_thread_per_cpu[i]; k++)
1921 if (thread_per_cpu[i][k]->busy[j]) {
1926 cpu_load.hits[j][i]++;
1938 cur_tsc = rte_rdtsc();
1940 diff_tsc = cur_tsc - prev_tsc;
1941 if (unlikely(diff_tsc > interval_tsc)) {
1945 printf("Cpu usage for %d rx threads and %d tx threads:\n\n",
1946 n_rx_thread, n_tx_thread);
1948 printf("cpu# proc%% poll%% overhead%%\n\n");
1950 for (i = min_cpu; i <= max_cpu; i++) {
1952 printf("CPU %d:", i);
1953 for (j = 0; j < MAX_CPU_COUNTER; j++) {
1954 printf("%7" PRIu64 "",
1955 cpu_load.hits[j][i] * 100 / cpu_load.counter);
1956 hits += cpu_load.hits[j][i];
1957 cpu_load.hits[j][i] = 0;
1959 printf("%7" PRIu64 "\n",
1960 100 - total[i] * 100 / cpu_load.counter);
1963 cpu_load.counter = 0;
1970 #endif /* APP_CPU_LOAD */
1973 * Null processing lthread loop
1975 * This loop is used to start empty scheduler on lcore.
1978 lthread_null(__rte_unused void *args)
1980 int lcore_id = rte_lcore_id();
1982 RTE_LOG(INFO, L3FWD, "Starting scheduler on lcore %d.\n", lcore_id);
1986 /* main processing loop */
1988 lthread_tx_per_ring(void *dummy)
1992 struct rte_ring *ring;
1993 struct thread_tx_conf *tx_conf;
1994 struct rte_mbuf *pkts_burst[MAX_PKT_BURST];
1995 struct lthread_cond *ready;
1997 tx_conf = (struct thread_tx_conf *)dummy;
1998 ring = tx_conf->ring;
1999 ready = *tx_conf->ready;
2001 lthread_set_data((void *)tx_conf);
2004 * Move this lthread to lcore
2006 lthread_set_affinity(tx_conf->conf.lcore_id);
2008 RTE_LOG(INFO, L3FWD, "entering main tx loop on lcore %u\n", rte_lcore_id());
2011 rte_atomic16_inc(&tx_counter);
2015 * Read packet from ring
2017 SET_CPU_BUSY(tx_conf, CPU_POLL);
2018 nb_rx = rte_ring_sc_dequeue_burst(ring, (void **)pkts_burst,
2020 SET_CPU_IDLE(tx_conf, CPU_POLL);
2023 SET_CPU_BUSY(tx_conf, CPU_PROCESS);
2024 portid = pkts_burst[0]->port;
2025 process_burst(pkts_burst, nb_rx, portid);
2026 SET_CPU_IDLE(tx_conf, CPU_PROCESS);
2029 lthread_cond_wait(ready, 0);
2035 * Main tx-lthreads spawner lthread.
2037 * This lthread is used to spawn one new lthread per ring from producers.
2041 lthread_tx(void *args)
2047 struct thread_tx_conf *tx_conf;
2049 tx_conf = (struct thread_tx_conf *)args;
2050 lthread_set_data((void *)tx_conf);
2053 * Move this lthread to the selected lcore
2055 lthread_set_affinity(tx_conf->conf.lcore_id);
2058 * Spawn tx readers (one per input ring)
2060 lthread_create(<, tx_conf->conf.lcore_id, lthread_tx_per_ring,
2063 lcore_id = rte_lcore_id();
2065 RTE_LOG(INFO, L3FWD, "Entering Tx main loop on lcore %u\n", lcore_id);
2067 tx_conf->conf.cpu_id = sched_getcpu();
2070 lthread_sleep(BURST_TX_DRAIN_US * 1000);
2073 * TX burst queue drain
2075 for (portid = 0; portid < RTE_MAX_ETHPORTS; portid++) {
2076 if (tx_conf->tx_mbufs[portid].len == 0)
2078 SET_CPU_BUSY(tx_conf, CPU_PROCESS);
2079 send_burst(tx_conf, tx_conf->tx_mbufs[portid].len, portid);
2080 SET_CPU_IDLE(tx_conf, CPU_PROCESS);
2081 tx_conf->tx_mbufs[portid].len = 0;
2088 lthread_rx(void *dummy)
2093 uint8_t portid, queueid;
2095 int len[RTE_MAX_LCORE] = { 0 };
2096 int old_len, new_len;
2097 struct rte_mbuf *pkts_burst[MAX_PKT_BURST];
2098 struct thread_rx_conf *rx_conf;
2100 rx_conf = (struct thread_rx_conf *)dummy;
2101 lthread_set_data((void *)rx_conf);
2104 * Move this lthread to lcore
2106 lthread_set_affinity(rx_conf->conf.lcore_id);
2108 if (rx_conf->n_rx_queue == 0) {
2109 RTE_LOG(INFO, L3FWD, "lcore %u has nothing to do\n", rte_lcore_id());
2113 RTE_LOG(INFO, L3FWD, "Entering main Rx loop on lcore %u\n", rte_lcore_id());
2115 for (i = 0; i < rx_conf->n_rx_queue; i++) {
2117 portid = rx_conf->rx_queue_list[i].port_id;
2118 queueid = rx_conf->rx_queue_list[i].queue_id;
2119 RTE_LOG(INFO, L3FWD, " -- lcoreid=%u portid=%hhu rxqueueid=%hhu\n",
2120 rte_lcore_id(), portid, queueid);
2124 * Init all condition variables (one per rx thread)
2126 for (i = 0; i < rx_conf->n_rx_queue; i++)
2127 lthread_cond_init(NULL, &rx_conf->ready[i], NULL);
2131 rx_conf->conf.cpu_id = sched_getcpu();
2132 rte_atomic16_inc(&rx_counter);
2136 * Read packet from RX queues
2138 for (i = 0; i < rx_conf->n_rx_queue; ++i) {
2139 portid = rx_conf->rx_queue_list[i].port_id;
2140 queueid = rx_conf->rx_queue_list[i].queue_id;
2142 SET_CPU_BUSY(rx_conf, CPU_POLL);
2143 nb_rx = rte_eth_rx_burst(portid, queueid, pkts_burst,
2145 SET_CPU_IDLE(rx_conf, CPU_POLL);
2148 worker_id = (worker_id + 1) % rx_conf->n_ring;
2149 old_len = len[worker_id];
2151 SET_CPU_BUSY(rx_conf, CPU_PROCESS);
2152 ret = rte_ring_sp_enqueue_burst(
2153 rx_conf->ring[worker_id],
2154 (void **) pkts_burst,
2157 new_len = old_len + ret;
2159 if (new_len >= BURST_SIZE) {
2160 lthread_cond_signal(rx_conf->ready[worker_id]);
2164 len[worker_id] = new_len;
2166 if (unlikely(ret < nb_rx)) {
2169 for (k = ret; k < nb_rx; k++) {
2170 struct rte_mbuf *m = pkts_burst[k];
2172 rte_pktmbuf_free(m);
2175 SET_CPU_IDLE(rx_conf, CPU_PROCESS);
2184 * Start scheduler with initial lthread on lcore
2186 * This lthread loop spawns all rx and tx lthreads on master lcore
2190 lthread_spawner(__rte_unused void *arg) {
2191 struct lthread *lt[MAX_THREAD];
2195 printf("Entering lthread_spawner\n");
2198 * Create producers (rx threads) on default lcore
2200 for (i = 0; i < n_rx_thread; i++) {
2201 rx_thread[i].conf.thread_id = i;
2202 lthread_create(<[n_thread], -1, lthread_rx,
2203 (void *)&rx_thread[i]);
2208 * Wait for all producers. Until some producers can be started on the same
2209 * scheduler as this lthread, yielding is required to let them to run and
2210 * prevent deadlock here.
2212 while (rte_atomic16_read(&rx_counter) < n_rx_thread)
2213 lthread_sleep(100000);
2216 * Create consumers (tx threads) on default lcore_id
2218 for (i = 0; i < n_tx_thread; i++) {
2219 tx_thread[i].conf.thread_id = i;
2220 lthread_create(<[n_thread], -1, lthread_tx,
2221 (void *)&tx_thread[i]);
2226 * Wait for all threads finished
2228 for (i = 0; i < n_thread; i++)
2229 lthread_join(lt[i], NULL);
2234 * Start master scheduler with initial lthread spawning rx and tx lthreads
2235 * (main_lthread_master).
2238 lthread_master_spawner(__rte_unused void *arg) {
2240 int lcore_id = rte_lcore_id();
2242 RTE_PER_LCORE(lcore_conf) = &lcore_conf[lcore_id];
2243 lthread_create(<, -1, lthread_spawner, NULL);
2250 * Start scheduler on lcore.
2253 sched_spawner(__rte_unused void *arg) {
2255 int lcore_id = rte_lcore_id();
2258 if (lcore_id == cpu_load_lcore_id) {
2259 cpu_load_collector(arg);
2262 #endif /* APP_CPU_LOAD */
2264 RTE_PER_LCORE(lcore_conf) = &lcore_conf[lcore_id];
2265 lthread_create(<, -1, lthread_null, NULL);
2271 /* main processing loop */
2273 pthread_tx(void *dummy)
2275 struct rte_mbuf *pkts_burst[MAX_PKT_BURST];
2276 uint64_t prev_tsc, diff_tsc, cur_tsc;
2279 struct thread_tx_conf *tx_conf;
2281 const uint64_t drain_tsc = (rte_get_tsc_hz() + US_PER_S - 1) /
2282 US_PER_S * BURST_TX_DRAIN_US;
2286 tx_conf = (struct thread_tx_conf *)dummy;
2288 RTE_LOG(INFO, L3FWD, "Entering main Tx loop on lcore %u\n", rte_lcore_id());
2290 tx_conf->conf.cpu_id = sched_getcpu();
2291 rte_atomic16_inc(&tx_counter);
2294 cur_tsc = rte_rdtsc();
2297 * TX burst queue drain
2299 diff_tsc = cur_tsc - prev_tsc;
2300 if (unlikely(diff_tsc > drain_tsc)) {
2303 * This could be optimized (use queueid instead of
2304 * portid), but it is not called so often
2306 SET_CPU_BUSY(tx_conf, CPU_PROCESS);
2307 for (portid = 0; portid < RTE_MAX_ETHPORTS; portid++) {
2308 if (tx_conf->tx_mbufs[portid].len == 0)
2310 send_burst(tx_conf, tx_conf->tx_mbufs[portid].len, portid);
2311 tx_conf->tx_mbufs[portid].len = 0;
2313 SET_CPU_IDLE(tx_conf, CPU_PROCESS);
2319 * Read packet from ring
2321 SET_CPU_BUSY(tx_conf, CPU_POLL);
2322 nb_rx = rte_ring_sc_dequeue_burst(tx_conf->ring,
2323 (void **)pkts_burst, MAX_PKT_BURST);
2324 SET_CPU_IDLE(tx_conf, CPU_POLL);
2326 if (unlikely(nb_rx == 0)) {
2331 SET_CPU_BUSY(tx_conf, CPU_PROCESS);
2332 portid = pkts_burst[0]->port;
2333 process_burst(pkts_burst, nb_rx, portid);
2334 SET_CPU_IDLE(tx_conf, CPU_PROCESS);
2340 pthread_rx(void *dummy)
2347 uint8_t portid, queueid;
2348 struct rte_mbuf *pkts_burst[MAX_PKT_BURST];
2350 struct thread_rx_conf *rx_conf;
2352 lcore_id = rte_lcore_id();
2353 rx_conf = (struct thread_rx_conf *)dummy;
2355 if (rx_conf->n_rx_queue == 0) {
2356 RTE_LOG(INFO, L3FWD, "lcore %u has nothing to do\n", lcore_id);
2360 RTE_LOG(INFO, L3FWD, "entering main rx loop on lcore %u\n", lcore_id);
2362 for (i = 0; i < rx_conf->n_rx_queue; i++) {
2364 portid = rx_conf->rx_queue_list[i].port_id;
2365 queueid = rx_conf->rx_queue_list[i].queue_id;
2366 RTE_LOG(INFO, L3FWD, " -- lcoreid=%u portid=%hhu rxqueueid=%hhu\n",
2367 lcore_id, portid, queueid);
2371 rx_conf->conf.cpu_id = sched_getcpu();
2372 rte_atomic16_inc(&rx_counter);
2376 * Read packet from RX queues
2378 for (i = 0; i < rx_conf->n_rx_queue; ++i) {
2379 portid = rx_conf->rx_queue_list[i].port_id;
2380 queueid = rx_conf->rx_queue_list[i].queue_id;
2382 SET_CPU_BUSY(rx_conf, CPU_POLL);
2383 nb_rx = rte_eth_rx_burst(portid, queueid, pkts_burst,
2385 SET_CPU_IDLE(rx_conf, CPU_POLL);
2392 SET_CPU_BUSY(rx_conf, CPU_PROCESS);
2393 worker_id = (worker_id + 1) % rx_conf->n_ring;
2394 n = rte_ring_sp_enqueue_burst(rx_conf->ring[worker_id],
2395 (void **)pkts_burst, nb_rx);
2397 if (unlikely(n != nb_rx)) {
2400 for (k = n; k < nb_rx; k++) {
2401 struct rte_mbuf *m = pkts_burst[k];
2403 rte_pktmbuf_free(m);
2407 SET_CPU_IDLE(rx_conf, CPU_PROCESS);
2417 pthread_run(__rte_unused void *arg) {
2418 int lcore_id = rte_lcore_id();
2421 for (i = 0; i < n_rx_thread; i++)
2422 if (rx_thread[i].conf.lcore_id == lcore_id) {
2423 printf("Start rx thread on %d...\n", lcore_id);
2424 RTE_PER_LCORE(lcore_conf) = &lcore_conf[lcore_id];
2425 RTE_PER_LCORE(lcore_conf)->data = (void *)&rx_thread[i];
2426 pthread_rx((void *)&rx_thread[i]);
2430 for (i = 0; i < n_tx_thread; i++)
2431 if (tx_thread[i].conf.lcore_id == lcore_id) {
2432 printf("Start tx thread on %d...\n", lcore_id);
2433 RTE_PER_LCORE(lcore_conf) = &lcore_conf[lcore_id];
2434 RTE_PER_LCORE(lcore_conf)->data = (void *)&tx_thread[i];
2435 pthread_tx((void *)&tx_thread[i]);
2440 if (lcore_id == cpu_load_lcore_id)
2441 cpu_load_collector(arg);
2442 #endif /* APP_CPU_LOAD */
2448 check_lcore_params(void)
2450 uint8_t queue, lcore;
2454 for (i = 0; i < nb_rx_thread_params; ++i) {
2455 queue = rx_thread_params[i].queue_id;
2456 if (queue >= MAX_RX_QUEUE_PER_PORT) {
2457 printf("invalid queue number: %hhu\n", queue);
2460 lcore = rx_thread_params[i].lcore_id;
2461 if (!rte_lcore_is_enabled(lcore)) {
2462 printf("error: lcore %hhu is not enabled in lcore mask\n", lcore);
2465 socketid = rte_lcore_to_socket_id(lcore);
2466 if ((socketid != 0) && (numa_on == 0))
2467 printf("warning: lcore %hhu is on socket %d with numa off\n",
2474 check_port_config(const unsigned nb_ports)
2479 for (i = 0; i < nb_rx_thread_params; ++i) {
2480 portid = rx_thread_params[i].port_id;
2481 if ((enabled_port_mask & (1 << portid)) == 0) {
2482 printf("port %u is not enabled in port mask\n", portid);
2485 if (portid >= nb_ports) {
2486 printf("port %u is not present on the board\n", portid);
2494 get_port_n_rx_queues(const uint8_t port)
2499 for (i = 0; i < nb_rx_thread_params; ++i)
2500 if (rx_thread_params[i].port_id == port &&
2501 rx_thread_params[i].queue_id > queue)
2502 queue = rx_thread_params[i].queue_id;
2504 return (uint8_t)(++queue);
2511 struct thread_rx_conf *rx_conf;
2512 struct thread_tx_conf *tx_conf;
2513 unsigned rx_thread_id, tx_thread_id;
2515 struct rte_ring *ring = NULL;
2517 for (tx_thread_id = 0; tx_thread_id < n_tx_thread; tx_thread_id++) {
2519 tx_conf = &tx_thread[tx_thread_id];
2521 printf("Connecting tx-thread %d with rx-thread %d\n", tx_thread_id,
2522 tx_conf->conf.thread_id);
2524 rx_thread_id = tx_conf->conf.thread_id;
2525 if (rx_thread_id > n_tx_thread) {
2526 printf("connection from tx-thread %u to rx-thread %u fails "
2527 "(rx-thread not defined)\n", tx_thread_id, rx_thread_id);
2531 rx_conf = &rx_thread[rx_thread_id];
2532 socket_io = rte_lcore_to_socket_id(rx_conf->conf.lcore_id);
2534 snprintf(name, sizeof(name), "app_ring_s%u_rx%u_tx%u",
2535 socket_io, rx_thread_id, tx_thread_id);
2537 ring = rte_ring_create(name, 1024 * 4, socket_io,
2538 RING_F_SP_ENQ | RING_F_SC_DEQ);
2541 rte_panic("Cannot create ring to connect rx-thread %u "
2542 "with tx-thread %u\n", rx_thread_id, tx_thread_id);
2545 rx_conf->ring[rx_conf->n_ring] = ring;
2547 tx_conf->ring = ring;
2548 tx_conf->ready = &rx_conf->ready[rx_conf->n_ring];
2556 init_rx_queues(void)
2558 uint16_t i, nb_rx_queue;
2563 for (i = 0; i < nb_rx_thread_params; ++i) {
2564 thread = rx_thread_params[i].thread_id;
2565 nb_rx_queue = rx_thread[thread].n_rx_queue;
2567 if (nb_rx_queue >= MAX_RX_QUEUE_PER_LCORE) {
2568 printf("error: too many queues (%u) for thread: %u\n",
2569 (unsigned)nb_rx_queue + 1, (unsigned)thread);
2573 rx_thread[thread].conf.thread_id = thread;
2574 rx_thread[thread].conf.lcore_id = rx_thread_params[i].lcore_id;
2575 rx_thread[thread].rx_queue_list[nb_rx_queue].port_id =
2576 rx_thread_params[i].port_id;
2577 rx_thread[thread].rx_queue_list[nb_rx_queue].queue_id =
2578 rx_thread_params[i].queue_id;
2579 rx_thread[thread].n_rx_queue++;
2581 if (thread >= n_rx_thread)
2582 n_rx_thread = thread + 1;
2589 init_tx_threads(void)
2594 for (i = 0; i < nb_tx_thread_params; ++i) {
2595 tx_thread[n_tx_thread].conf.thread_id = tx_thread_params[i].thread_id;
2596 tx_thread[n_tx_thread].conf.lcore_id = tx_thread_params[i].lcore_id;
2604 print_usage(const char *prgname)
2606 printf("%s [EAL options] -- -p PORTMASK -P"
2607 " [--rx (port,queue,lcore,thread)[,(port,queue,lcore,thread]]"
2608 " [--tx (lcore,thread)[,(lcore,thread]]"
2609 " [--enable-jumbo [--max-pkt-len PKTLEN]]\n"
2610 " -p PORTMASK: hexadecimal bitmask of ports to configure\n"
2611 " -P : enable promiscuous mode\n"
2612 " --rx (port,queue,lcore,thread): rx queues configuration\n"
2613 " --tx (lcore,thread): tx threads configuration\n"
2614 " --stat-lcore LCORE: use lcore for stat collector\n"
2615 " --eth-dest=X,MM:MM:MM:MM:MM:MM: optional, ethernet destination for port X\n"
2616 " --no-numa: optional, disable numa awareness\n"
2617 " --ipv6: optional, specify it if running ipv6 packets\n"
2618 " --enable-jumbo: enable jumbo frame"
2619 " which max packet len is PKTLEN in decimal (64-9600)\n"
2620 " --hash-entry-num: specify the hash entry number in hexadecimal to be setup\n"
2621 " --no-lthreads: turn off lthread model\n",
2625 static int parse_max_pkt_len(const char *pktlen)
2630 /* parse decimal string */
2631 len = strtoul(pktlen, &end, 10);
2632 if ((pktlen[0] == '\0') || (end == NULL) || (*end != '\0'))
2642 parse_portmask(const char *portmask)
2647 /* parse hexadecimal string */
2648 pm = strtoul(portmask, &end, 16);
2649 if ((portmask[0] == '\0') || (end == NULL) || (*end != '\0'))
2658 #if (APP_LOOKUP_METHOD == APP_LOOKUP_EXACT_MATCH)
2660 parse_hash_entry_number(const char *hash_entry_num)
2663 unsigned long hash_en;
2665 /* parse hexadecimal string */
2666 hash_en = strtoul(hash_entry_num, &end, 16);
2667 if ((hash_entry_num[0] == '\0') || (end == NULL) || (*end != '\0'))
2678 parse_rx_config(const char *q_arg)
2681 const char *p, *p0 = q_arg;
2690 unsigned long int_fld[_NUM_FLD];
2691 char *str_fld[_NUM_FLD];
2695 nb_rx_thread_params = 0;
2697 while ((p = strchr(p0, '(')) != NULL) {
2699 p0 = strchr(p, ')');
2704 if (size >= sizeof(s))
2707 snprintf(s, sizeof(s), "%.*s", size, p);
2708 if (rte_strsplit(s, sizeof(s), str_fld, _NUM_FLD, ',') != _NUM_FLD)
2710 for (i = 0; i < _NUM_FLD; i++) {
2712 int_fld[i] = strtoul(str_fld[i], &end, 0);
2713 if (errno != 0 || end == str_fld[i] || int_fld[i] > 255)
2716 if (nb_rx_thread_params >= MAX_LCORE_PARAMS) {
2717 printf("exceeded max number of rx params: %hu\n",
2718 nb_rx_thread_params);
2721 rx_thread_params_array[nb_rx_thread_params].port_id =
2722 (uint8_t)int_fld[FLD_PORT];
2723 rx_thread_params_array[nb_rx_thread_params].queue_id =
2724 (uint8_t)int_fld[FLD_QUEUE];
2725 rx_thread_params_array[nb_rx_thread_params].lcore_id =
2726 (uint8_t)int_fld[FLD_LCORE];
2727 rx_thread_params_array[nb_rx_thread_params].thread_id =
2728 (uint8_t)int_fld[FLD_THREAD];
2729 ++nb_rx_thread_params;
2731 rx_thread_params = rx_thread_params_array;
2736 parse_tx_config(const char *q_arg)
2739 const char *p, *p0 = q_arg;
2746 unsigned long int_fld[_NUM_FLD];
2747 char *str_fld[_NUM_FLD];
2751 nb_tx_thread_params = 0;
2753 while ((p = strchr(p0, '(')) != NULL) {
2755 p0 = strchr(p, ')');
2760 if (size >= sizeof(s))
2763 snprintf(s, sizeof(s), "%.*s", size, p);
2764 if (rte_strsplit(s, sizeof(s), str_fld, _NUM_FLD, ',') != _NUM_FLD)
2766 for (i = 0; i < _NUM_FLD; i++) {
2768 int_fld[i] = strtoul(str_fld[i], &end, 0);
2769 if (errno != 0 || end == str_fld[i] || int_fld[i] > 255)
2772 if (nb_tx_thread_params >= MAX_LCORE_PARAMS) {
2773 printf("exceeded max number of tx params: %hu\n",
2774 nb_tx_thread_params);
2777 tx_thread_params_array[nb_tx_thread_params].lcore_id =
2778 (uint8_t)int_fld[FLD_LCORE];
2779 tx_thread_params_array[nb_tx_thread_params].thread_id =
2780 (uint8_t)int_fld[FLD_THREAD];
2781 ++nb_tx_thread_params;
2783 tx_thread_params = tx_thread_params_array;
2788 #if (APP_CPU_LOAD > 0)
2790 parse_stat_lcore(const char *stat_lcore)
2793 unsigned long lcore_id;
2795 lcore_id = strtoul(stat_lcore, &end, 10);
2796 if ((stat_lcore[0] == '\0') || (end == NULL) || (*end != '\0'))
2804 parse_eth_dest(const char *optarg)
2808 uint8_t c, *dest, peer_addr[6];
2811 portid = strtoul(optarg, &port_end, 10);
2812 if (errno != 0 || port_end == optarg || *port_end++ != ',')
2813 rte_exit(EXIT_FAILURE,
2814 "Invalid eth-dest: %s", optarg);
2815 if (portid >= RTE_MAX_ETHPORTS)
2816 rte_exit(EXIT_FAILURE,
2817 "eth-dest: port %d >= RTE_MAX_ETHPORTS(%d)\n",
2818 portid, RTE_MAX_ETHPORTS);
2820 if (cmdline_parse_etheraddr(NULL, port_end,
2821 &peer_addr, sizeof(peer_addr)) < 0)
2822 rte_exit(EXIT_FAILURE,
2823 "Invalid ethernet address: %s\n",
2825 dest = (uint8_t *)&dest_eth_addr[portid];
2826 for (c = 0; c < 6; c++)
2827 dest[c] = peer_addr[c];
2828 *(uint64_t *)(val_eth + portid) = dest_eth_addr[portid];
2831 #define CMD_LINE_OPT_RX_CONFIG "rx"
2832 #define CMD_LINE_OPT_TX_CONFIG "tx"
2833 #define CMD_LINE_OPT_STAT_LCORE "stat-lcore"
2834 #define CMD_LINE_OPT_ETH_DEST "eth-dest"
2835 #define CMD_LINE_OPT_NO_NUMA "no-numa"
2836 #define CMD_LINE_OPT_IPV6 "ipv6"
2837 #define CMD_LINE_OPT_ENABLE_JUMBO "enable-jumbo"
2838 #define CMD_LINE_OPT_HASH_ENTRY_NUM "hash-entry-num"
2839 #define CMD_LINE_OPT_NO_LTHREADS "no-lthreads"
2841 /* Parse the argument given in the command line of the application */
2843 parse_args(int argc, char **argv)
2848 char *prgname = argv[0];
2849 static struct option lgopts[] = {
2850 {CMD_LINE_OPT_RX_CONFIG, 1, 0, 0},
2851 {CMD_LINE_OPT_TX_CONFIG, 1, 0, 0},
2852 {CMD_LINE_OPT_STAT_LCORE, 1, 0, 0},
2853 {CMD_LINE_OPT_ETH_DEST, 1, 0, 0},
2854 {CMD_LINE_OPT_NO_NUMA, 0, 0, 0},
2855 {CMD_LINE_OPT_IPV6, 0, 0, 0},
2856 {CMD_LINE_OPT_ENABLE_JUMBO, 0, 0, 0},
2857 {CMD_LINE_OPT_HASH_ENTRY_NUM, 1, 0, 0},
2858 {CMD_LINE_OPT_NO_LTHREADS, 0, 0, 0},
2864 while ((opt = getopt_long(argc, argvopt, "p:P",
2865 lgopts, &option_index)) != EOF) {
2870 enabled_port_mask = parse_portmask(optarg);
2871 if (enabled_port_mask == 0) {
2872 printf("invalid portmask\n");
2873 print_usage(prgname);
2878 printf("Promiscuous mode selected\n");
2884 if (!strncmp(lgopts[option_index].name, CMD_LINE_OPT_RX_CONFIG,
2885 sizeof(CMD_LINE_OPT_RX_CONFIG))) {
2886 ret = parse_rx_config(optarg);
2888 printf("invalid rx-config\n");
2889 print_usage(prgname);
2894 if (!strncmp(lgopts[option_index].name, CMD_LINE_OPT_TX_CONFIG,
2895 sizeof(CMD_LINE_OPT_TX_CONFIG))) {
2896 ret = parse_tx_config(optarg);
2898 printf("invalid tx-config\n");
2899 print_usage(prgname);
2904 #if (APP_CPU_LOAD > 0)
2905 if (!strncmp(lgopts[option_index].name, CMD_LINE_OPT_STAT_LCORE,
2906 sizeof(CMD_LINE_OPT_STAT_LCORE))) {
2907 cpu_load_lcore_id = parse_stat_lcore(optarg);
2911 if (!strncmp(lgopts[option_index].name, CMD_LINE_OPT_ETH_DEST,
2912 sizeof(CMD_LINE_OPT_ETH_DEST)))
2913 parse_eth_dest(optarg);
2915 if (!strncmp(lgopts[option_index].name, CMD_LINE_OPT_NO_NUMA,
2916 sizeof(CMD_LINE_OPT_NO_NUMA))) {
2917 printf("numa is disabled\n");
2921 #if (APP_LOOKUP_METHOD == APP_LOOKUP_EXACT_MATCH)
2922 if (!strncmp(lgopts[option_index].name, CMD_LINE_OPT_IPV6,
2923 sizeof(CMD_LINE_OPT_IPV6))) {
2924 printf("ipv6 is specified\n");
2929 if (!strncmp(lgopts[option_index].name, CMD_LINE_OPT_NO_LTHREADS,
2930 sizeof(CMD_LINE_OPT_NO_LTHREADS))) {
2931 printf("l-threads model is disabled\n");
2935 if (!strncmp(lgopts[option_index].name, CMD_LINE_OPT_ENABLE_JUMBO,
2936 sizeof(CMD_LINE_OPT_ENABLE_JUMBO))) {
2937 struct option lenopts = {"max-pkt-len", required_argument, 0,
2940 printf("jumbo frame is enabled - disabling simple TX path\n");
2941 port_conf.rxmode.jumbo_frame = 1;
2943 /* if no max-pkt-len set, use the default value ETHER_MAX_LEN */
2944 if (0 == getopt_long(argc, argvopt, "", &lenopts,
2947 ret = parse_max_pkt_len(optarg);
2948 if ((ret < 64) || (ret > MAX_JUMBO_PKT_LEN)) {
2949 printf("invalid packet length\n");
2950 print_usage(prgname);
2953 port_conf.rxmode.max_rx_pkt_len = ret;
2955 printf("set jumbo frame max packet length to %u\n",
2956 (unsigned int)port_conf.rxmode.max_rx_pkt_len);
2958 #if (APP_LOOKUP_METHOD == APP_LOOKUP_EXACT_MATCH)
2959 if (!strncmp(lgopts[option_index].name, CMD_LINE_OPT_HASH_ENTRY_NUM,
2960 sizeof(CMD_LINE_OPT_HASH_ENTRY_NUM))) {
2961 ret = parse_hash_entry_number(optarg);
2962 if ((ret > 0) && (ret <= L3FWD_HASH_ENTRIES)) {
2963 hash_entry_number = ret;
2965 printf("invalid hash entry number\n");
2966 print_usage(prgname);
2974 print_usage(prgname);
2980 argv[optind-1] = prgname;
2983 optind = 0; /* reset getopt lib */
2988 print_ethaddr(const char *name, const struct ether_addr *eth_addr)
2990 char buf[ETHER_ADDR_FMT_SIZE];
2992 ether_format_addr(buf, ETHER_ADDR_FMT_SIZE, eth_addr);
2993 printf("%s%s", name, buf);
2996 #if (APP_LOOKUP_METHOD == APP_LOOKUP_EXACT_MATCH)
2998 static void convert_ipv4_5tuple(struct ipv4_5tuple *key1,
2999 union ipv4_5tuple_host *key2)
3001 key2->ip_dst = rte_cpu_to_be_32(key1->ip_dst);
3002 key2->ip_src = rte_cpu_to_be_32(key1->ip_src);
3003 key2->port_dst = rte_cpu_to_be_16(key1->port_dst);
3004 key2->port_src = rte_cpu_to_be_16(key1->port_src);
3005 key2->proto = key1->proto;
3010 static void convert_ipv6_5tuple(struct ipv6_5tuple *key1,
3011 union ipv6_5tuple_host *key2)
3015 for (i = 0; i < 16; i++) {
3016 key2->ip_dst[i] = key1->ip_dst[i];
3017 key2->ip_src[i] = key1->ip_src[i];
3019 key2->port_dst = rte_cpu_to_be_16(key1->port_dst);
3020 key2->port_src = rte_cpu_to_be_16(key1->port_src);
3021 key2->proto = key1->proto;
3027 #define BYTE_VALUE_MAX 256
3028 #define ALL_32_BITS 0xffffffff
3029 #define BIT_8_TO_15 0x0000ff00
3031 populate_ipv4_few_flow_into_table(const struct rte_hash *h)
3035 uint32_t array_len = RTE_DIM(ipv4_l3fwd_route_array);
3037 mask0 = _mm_set_epi32(ALL_32_BITS, ALL_32_BITS, ALL_32_BITS, BIT_8_TO_15);
3038 for (i = 0; i < array_len; i++) {
3039 struct ipv4_l3fwd_route entry;
3040 union ipv4_5tuple_host newkey;
3042 entry = ipv4_l3fwd_route_array[i];
3043 convert_ipv4_5tuple(&entry.key, &newkey);
3044 ret = rte_hash_add_key(h, (void *)&newkey);
3046 rte_exit(EXIT_FAILURE, "Unable to add entry %" PRIu32
3047 " to the l3fwd hash.\n", i);
3049 ipv4_l3fwd_out_if[ret] = entry.if_out;
3051 printf("Hash: Adding 0x%" PRIx32 " keys\n", array_len);
3054 #define BIT_16_TO_23 0x00ff0000
3056 populate_ipv6_few_flow_into_table(const struct rte_hash *h)
3060 uint32_t array_len = RTE_DIM(ipv6_l3fwd_route_array);
3062 mask1 = _mm_set_epi32(ALL_32_BITS, ALL_32_BITS, ALL_32_BITS, BIT_16_TO_23);
3063 mask2 = _mm_set_epi32(0, 0, ALL_32_BITS, ALL_32_BITS);
3064 for (i = 0; i < array_len; i++) {
3065 struct ipv6_l3fwd_route entry;
3066 union ipv6_5tuple_host newkey;
3068 entry = ipv6_l3fwd_route_array[i];
3069 convert_ipv6_5tuple(&entry.key, &newkey);
3070 ret = rte_hash_add_key(h, (void *)&newkey);
3072 rte_exit(EXIT_FAILURE, "Unable to add entry %" PRIu32
3073 " to the l3fwd hash.\n", i);
3075 ipv6_l3fwd_out_if[ret] = entry.if_out;
3077 printf("Hash: Adding 0x%" PRIx32 "keys\n", array_len);
3080 #define NUMBER_PORT_USED 4
3082 populate_ipv4_many_flow_into_table(const struct rte_hash *h,
3083 unsigned int nr_flow)
3087 mask0 = _mm_set_epi32(ALL_32_BITS, ALL_32_BITS, ALL_32_BITS, BIT_8_TO_15);
3089 for (i = 0; i < nr_flow; i++) {
3090 struct ipv4_l3fwd_route entry;
3091 union ipv4_5tuple_host newkey;
3092 uint8_t a = (uint8_t)((i / NUMBER_PORT_USED) % BYTE_VALUE_MAX);
3093 uint8_t b = (uint8_t)(((i / NUMBER_PORT_USED) / BYTE_VALUE_MAX) %
3095 uint8_t c = (uint8_t)((i / NUMBER_PORT_USED) / (BYTE_VALUE_MAX *
3097 /* Create the ipv4 exact match flow */
3098 memset(&entry, 0, sizeof(entry));
3099 switch (i & (NUMBER_PORT_USED - 1)) {
3101 entry = ipv4_l3fwd_route_array[0];
3102 entry.key.ip_dst = IPv4(101, c, b, a);
3105 entry = ipv4_l3fwd_route_array[1];
3106 entry.key.ip_dst = IPv4(201, c, b, a);
3109 entry = ipv4_l3fwd_route_array[2];
3110 entry.key.ip_dst = IPv4(111, c, b, a);
3113 entry = ipv4_l3fwd_route_array[3];
3114 entry.key.ip_dst = IPv4(211, c, b, a);
3117 convert_ipv4_5tuple(&entry.key, &newkey);
3118 int32_t ret = rte_hash_add_key(h, (void *)&newkey);
3121 rte_exit(EXIT_FAILURE, "Unable to add entry %u\n", i);
3123 ipv4_l3fwd_out_if[ret] = (uint8_t)entry.if_out;
3126 printf("Hash: Adding 0x%x keys\n", nr_flow);
3130 populate_ipv6_many_flow_into_table(const struct rte_hash *h,
3131 unsigned int nr_flow)
3135 mask1 = _mm_set_epi32(ALL_32_BITS, ALL_32_BITS, ALL_32_BITS, BIT_16_TO_23);
3136 mask2 = _mm_set_epi32(0, 0, ALL_32_BITS, ALL_32_BITS);
3137 for (i = 0; i < nr_flow; i++) {
3138 struct ipv6_l3fwd_route entry;
3139 union ipv6_5tuple_host newkey;
3141 uint8_t a = (uint8_t) ((i / NUMBER_PORT_USED) % BYTE_VALUE_MAX);
3142 uint8_t b = (uint8_t) (((i / NUMBER_PORT_USED) / BYTE_VALUE_MAX) %
3144 uint8_t c = (uint8_t) ((i / NUMBER_PORT_USED) / (BYTE_VALUE_MAX *
3147 /* Create the ipv6 exact match flow */
3148 memset(&entry, 0, sizeof(entry));
3149 switch (i & (NUMBER_PORT_USED - 1)) {
3151 entry = ipv6_l3fwd_route_array[0];
3154 entry = ipv6_l3fwd_route_array[1];
3157 entry = ipv6_l3fwd_route_array[2];
3160 entry = ipv6_l3fwd_route_array[3];
3163 entry.key.ip_dst[13] = c;
3164 entry.key.ip_dst[14] = b;
3165 entry.key.ip_dst[15] = a;
3166 convert_ipv6_5tuple(&entry.key, &newkey);
3167 int32_t ret = rte_hash_add_key(h, (void *)&newkey);
3170 rte_exit(EXIT_FAILURE, "Unable to add entry %u\n", i);
3172 ipv6_l3fwd_out_if[ret] = (uint8_t) entry.if_out;
3175 printf("Hash: Adding 0x%x keys\n", nr_flow);
3179 setup_hash(int socketid)
3181 struct rte_hash_parameters ipv4_l3fwd_hash_params = {
3183 .entries = L3FWD_HASH_ENTRIES,
3184 .key_len = sizeof(union ipv4_5tuple_host),
3185 .hash_func = ipv4_hash_crc,
3186 .hash_func_init_val = 0,
3189 struct rte_hash_parameters ipv6_l3fwd_hash_params = {
3191 .entries = L3FWD_HASH_ENTRIES,
3192 .key_len = sizeof(union ipv6_5tuple_host),
3193 .hash_func = ipv6_hash_crc,
3194 .hash_func_init_val = 0,
3199 /* create ipv4 hash */
3200 snprintf(s, sizeof(s), "ipv4_l3fwd_hash_%d", socketid);
3201 ipv4_l3fwd_hash_params.name = s;
3202 ipv4_l3fwd_hash_params.socket_id = socketid;
3203 ipv4_l3fwd_lookup_struct[socketid] =
3204 rte_hash_create(&ipv4_l3fwd_hash_params);
3205 if (ipv4_l3fwd_lookup_struct[socketid] == NULL)
3206 rte_exit(EXIT_FAILURE, "Unable to create the l3fwd hash on "
3207 "socket %d\n", socketid);
3209 /* create ipv6 hash */
3210 snprintf(s, sizeof(s), "ipv6_l3fwd_hash_%d", socketid);
3211 ipv6_l3fwd_hash_params.name = s;
3212 ipv6_l3fwd_hash_params.socket_id = socketid;
3213 ipv6_l3fwd_lookup_struct[socketid] =
3214 rte_hash_create(&ipv6_l3fwd_hash_params);
3215 if (ipv6_l3fwd_lookup_struct[socketid] == NULL)
3216 rte_exit(EXIT_FAILURE, "Unable to create the l3fwd hash on "
3217 "socket %d\n", socketid);
3219 if (hash_entry_number != HASH_ENTRY_NUMBER_DEFAULT) {
3220 /* For testing hash matching with a large number of flows we
3221 * generate millions of IP 5-tuples with an incremented dst
3222 * address to initialize the hash table. */
3224 /* populate the ipv4 hash */
3225 populate_ipv4_many_flow_into_table(
3226 ipv4_l3fwd_lookup_struct[socketid], hash_entry_number);
3228 /* populate the ipv6 hash */
3229 populate_ipv6_many_flow_into_table(
3230 ipv6_l3fwd_lookup_struct[socketid], hash_entry_number);
3233 /* Use data in ipv4/ipv6 l3fwd lookup table directly to initialize
3236 /* populate the ipv4 hash */
3237 populate_ipv4_few_flow_into_table(
3238 ipv4_l3fwd_lookup_struct[socketid]);
3240 /* populate the ipv6 hash */
3241 populate_ipv6_few_flow_into_table(
3242 ipv6_l3fwd_lookup_struct[socketid]);
3248 #if (APP_LOOKUP_METHOD == APP_LOOKUP_LPM)
3250 setup_lpm(int socketid)
3252 struct rte_lpm6_config config;
3253 struct rte_lpm_config lpm_ipv4_config;
3258 /* create the LPM table */
3259 snprintf(s, sizeof(s), "IPV4_L3FWD_LPM_%d", socketid);
3260 lpm_ipv4_config.max_rules = IPV4_L3FWD_LPM_MAX_RULES;
3261 lpm_ipv4_config.number_tbl8s = 256;
3262 lpm_ipv4_config.flags = 0;
3263 ipv4_l3fwd_lookup_struct[socketid] =
3264 rte_lpm_create(s, socketid, &lpm_ipv4_config);
3265 if (ipv4_l3fwd_lookup_struct[socketid] == NULL)
3266 rte_exit(EXIT_FAILURE, "Unable to create the l3fwd LPM table"
3267 " on socket %d\n", socketid);
3269 /* populate the LPM table */
3270 for (i = 0; i < IPV4_L3FWD_NUM_ROUTES; i++) {
3272 /* skip unused ports */
3273 if ((1 << ipv4_l3fwd_route_array[i].if_out &
3274 enabled_port_mask) == 0)
3277 ret = rte_lpm_add(ipv4_l3fwd_lookup_struct[socketid],
3278 ipv4_l3fwd_route_array[i].ip,
3279 ipv4_l3fwd_route_array[i].depth,
3280 ipv4_l3fwd_route_array[i].if_out);
3283 rte_exit(EXIT_FAILURE, "Unable to add entry %u to the "
3284 "l3fwd LPM table on socket %d\n",
3288 printf("LPM: Adding route 0x%08x / %d (%d)\n",
3289 (unsigned)ipv4_l3fwd_route_array[i].ip,
3290 ipv4_l3fwd_route_array[i].depth,
3291 ipv4_l3fwd_route_array[i].if_out);
3294 /* create the LPM6 table */
3295 snprintf(s, sizeof(s), "IPV6_L3FWD_LPM_%d", socketid);
3297 config.max_rules = IPV6_L3FWD_LPM_MAX_RULES;
3298 config.number_tbl8s = IPV6_L3FWD_LPM_NUMBER_TBL8S;
3300 ipv6_l3fwd_lookup_struct[socketid] = rte_lpm6_create(s, socketid,
3302 if (ipv6_l3fwd_lookup_struct[socketid] == NULL)
3303 rte_exit(EXIT_FAILURE, "Unable to create the l3fwd LPM table"
3304 " on socket %d\n", socketid);
3306 /* populate the LPM table */
3307 for (i = 0; i < IPV6_L3FWD_NUM_ROUTES; i++) {
3309 /* skip unused ports */
3310 if ((1 << ipv6_l3fwd_route_array[i].if_out &
3311 enabled_port_mask) == 0)
3314 ret = rte_lpm6_add(ipv6_l3fwd_lookup_struct[socketid],
3315 ipv6_l3fwd_route_array[i].ip,
3316 ipv6_l3fwd_route_array[i].depth,
3317 ipv6_l3fwd_route_array[i].if_out);
3320 rte_exit(EXIT_FAILURE, "Unable to add entry %u to the "
3321 "l3fwd LPM table on socket %d\n",
3325 printf("LPM: Adding route %s / %d (%d)\n",
3327 ipv6_l3fwd_route_array[i].depth,
3328 ipv6_l3fwd_route_array[i].if_out);
3334 init_mem(unsigned nb_mbuf)
3336 struct lcore_conf *qconf;
3341 for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
3342 if (rte_lcore_is_enabled(lcore_id) == 0)
3346 socketid = rte_lcore_to_socket_id(lcore_id);
3350 if (socketid >= NB_SOCKETS) {
3351 rte_exit(EXIT_FAILURE, "Socket %d of lcore %u is out of range %d\n",
3352 socketid, lcore_id, NB_SOCKETS);
3354 if (pktmbuf_pool[socketid] == NULL) {
3355 snprintf(s, sizeof(s), "mbuf_pool_%d", socketid);
3356 pktmbuf_pool[socketid] =
3357 rte_pktmbuf_pool_create(s, nb_mbuf,
3358 MEMPOOL_CACHE_SIZE, 0,
3359 RTE_MBUF_DEFAULT_BUF_SIZE, socketid);
3360 if (pktmbuf_pool[socketid] == NULL)
3361 rte_exit(EXIT_FAILURE,
3362 "Cannot init mbuf pool on socket %d\n", socketid);
3364 printf("Allocated mbuf pool on socket %d\n", socketid);
3366 #if (APP_LOOKUP_METHOD == APP_LOOKUP_LPM)
3367 setup_lpm(socketid);
3369 setup_hash(socketid);
3372 qconf = &lcore_conf[lcore_id];
3373 qconf->ipv4_lookup_struct = ipv4_l3fwd_lookup_struct[socketid];
3374 qconf->ipv6_lookup_struct = ipv6_l3fwd_lookup_struct[socketid];
3379 /* Check the link status of all ports in up to 9s, and print them finally */
3381 check_all_ports_link_status(uint8_t port_num, uint32_t port_mask)
3383 #define CHECK_INTERVAL 100 /* 100ms */
3384 #define MAX_CHECK_TIME 90 /* 9s (90 * 100ms) in total */
3385 uint8_t portid, count, all_ports_up, print_flag = 0;
3386 struct rte_eth_link link;
3388 printf("\nChecking link status");
3390 for (count = 0; count <= MAX_CHECK_TIME; count++) {
3392 for (portid = 0; portid < port_num; portid++) {
3393 if ((port_mask & (1 << portid)) == 0)
3395 memset(&link, 0, sizeof(link));
3396 rte_eth_link_get_nowait(portid, &link);
3397 /* print link status if flag set */
3398 if (print_flag == 1) {
3399 if (link.link_status)
3400 printf("Port %d Link Up - speed %u "
3401 "Mbps - %s\n", (uint8_t)portid,
3402 (unsigned)link.link_speed,
3403 (link.link_duplex == ETH_LINK_FULL_DUPLEX) ?
3404 ("full-duplex") : ("half-duplex\n"));
3406 printf("Port %d Link Down\n",
3410 /* clear all_ports_up flag if any link down */
3411 if (link.link_status == ETH_LINK_DOWN) {
3416 /* after finally printing all link status, get out */
3417 if (print_flag == 1)
3420 if (all_ports_up == 0) {
3423 rte_delay_ms(CHECK_INTERVAL);
3426 /* set the print_flag if all ports up or timeout */
3427 if (all_ports_up == 1 || count == (MAX_CHECK_TIME - 1)) {
3435 main(int argc, char **argv)
3437 struct rte_eth_dev_info dev_info;
3438 struct rte_eth_txconf *txconf;
3444 uint32_t n_tx_queue, nb_lcores;
3445 uint8_t portid, nb_rx_queue, queue, socketid;
3448 ret = rte_eal_init(argc, argv);
3450 rte_exit(EXIT_FAILURE, "Invalid EAL parameters\n");
3454 /* pre-init dst MACs for all ports to 02:00:00:00:00:xx */
3455 for (portid = 0; portid < RTE_MAX_ETHPORTS; portid++) {
3456 dest_eth_addr[portid] = ETHER_LOCAL_ADMIN_ADDR +
3457 ((uint64_t)portid << 40);
3458 *(uint64_t *)(val_eth + portid) = dest_eth_addr[portid];
3461 /* parse application arguments (after the EAL ones) */
3462 ret = parse_args(argc, argv);
3464 rte_exit(EXIT_FAILURE, "Invalid L3FWD parameters\n");
3466 if (check_lcore_params() < 0)
3467 rte_exit(EXIT_FAILURE, "check_lcore_params failed\n");
3469 printf("Initializing rx-queues...\n");
3470 ret = init_rx_queues();
3472 rte_exit(EXIT_FAILURE, "init_rx_queues failed\n");
3474 printf("Initializing tx-threads...\n");
3475 ret = init_tx_threads();
3477 rte_exit(EXIT_FAILURE, "init_tx_threads failed\n");
3479 printf("Initializing rings...\n");
3480 ret = init_rx_rings();
3482 rte_exit(EXIT_FAILURE, "init_rx_rings failed\n");
3484 nb_ports = rte_eth_dev_count();
3486 if (check_port_config(nb_ports) < 0)
3487 rte_exit(EXIT_FAILURE, "check_port_config failed\n");
3489 nb_lcores = rte_lcore_count();
3491 /* initialize all ports */
3492 for (portid = 0; portid < nb_ports; portid++) {
3493 /* skip ports that are not enabled */
3494 if ((enabled_port_mask & (1 << portid)) == 0) {
3495 printf("\nSkipping disabled port %d\n", portid);
3500 printf("Initializing port %d ... ", portid);
3503 nb_rx_queue = get_port_n_rx_queues(portid);
3504 n_tx_queue = nb_lcores;
3505 if (n_tx_queue > MAX_TX_QUEUE_PER_PORT)
3506 n_tx_queue = MAX_TX_QUEUE_PER_PORT;
3507 printf("Creating queues: nb_rxq=%d nb_txq=%u... ",
3508 nb_rx_queue, (unsigned)n_tx_queue);
3509 ret = rte_eth_dev_configure(portid, nb_rx_queue,
3510 (uint16_t)n_tx_queue, &port_conf);
3512 rte_exit(EXIT_FAILURE, "Cannot configure device: err=%d, port=%d\n",
3515 rte_eth_macaddr_get(portid, &ports_eth_addr[portid]);
3516 print_ethaddr(" Address:", &ports_eth_addr[portid]);
3518 print_ethaddr("Destination:",
3519 (const struct ether_addr *)&dest_eth_addr[portid]);
3523 * prepare src MACs for each port.
3525 ether_addr_copy(&ports_eth_addr[portid],
3526 (struct ether_addr *)(val_eth + portid) + 1);
3529 ret = init_mem(NB_MBUF);
3531 rte_exit(EXIT_FAILURE, "init_mem failed\n");
3533 /* init one TX queue per couple (lcore,port) */
3535 for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
3536 if (rte_lcore_is_enabled(lcore_id) == 0)
3540 socketid = (uint8_t)rte_lcore_to_socket_id(lcore_id);
3544 printf("txq=%u,%d,%d ", lcore_id, queueid, socketid);
3547 rte_eth_dev_info_get(portid, &dev_info);
3548 txconf = &dev_info.default_txconf;
3549 if (port_conf.rxmode.jumbo_frame)
3550 txconf->txq_flags = 0;
3551 ret = rte_eth_tx_queue_setup(portid, queueid, nb_txd,
3554 rte_exit(EXIT_FAILURE, "rte_eth_tx_queue_setup: err=%d, "
3555 "port=%d\n", ret, portid);
3557 tx_thread[lcore_id].tx_queue_id[portid] = queueid;
3563 for (i = 0; i < n_rx_thread; i++) {
3564 lcore_id = rx_thread[i].conf.lcore_id;
3566 if (rte_lcore_is_enabled(lcore_id) == 0) {
3567 rte_exit(EXIT_FAILURE,
3568 "Cannot start Rx thread on lcore %u: lcore disabled\n",
3573 printf("\nInitializing rx queues for Rx thread %d on lcore %u ... ",
3577 /* init RX queues */
3578 for (queue = 0; queue < rx_thread[i].n_rx_queue; ++queue) {
3579 portid = rx_thread[i].rx_queue_list[queue].port_id;
3580 queueid = rx_thread[i].rx_queue_list[queue].queue_id;
3583 socketid = (uint8_t)rte_lcore_to_socket_id(lcore_id);
3587 printf("rxq=%d,%d,%d ", portid, queueid, socketid);
3590 ret = rte_eth_rx_queue_setup(portid, queueid, nb_rxd,
3593 pktmbuf_pool[socketid]);
3595 rte_exit(EXIT_FAILURE, "rte_eth_rx_queue_setup: err=%d, "
3596 "port=%d\n", ret, portid);
3603 for (portid = 0; portid < nb_ports; portid++) {
3604 if ((enabled_port_mask & (1 << portid)) == 0)
3608 ret = rte_eth_dev_start(portid);
3610 rte_exit(EXIT_FAILURE, "rte_eth_dev_start: err=%d, port=%d\n",
3614 * If enabled, put device in promiscuous mode.
3615 * This allows IO forwarding mode to forward packets
3616 * to itself through 2 cross-connected ports of the
3620 rte_eth_promiscuous_enable(portid);
3623 check_all_ports_link_status((uint8_t)nb_ports, enabled_port_mask);
3626 printf("Starting L-Threading Model\n");
3628 #if (APP_CPU_LOAD > 0)
3629 if (cpu_load_lcore_id > 0)
3630 /* Use one lcore for cpu load collector */
3634 lthread_num_schedulers_set(nb_lcores);
3635 rte_eal_mp_remote_launch(sched_spawner, NULL, SKIP_MASTER);
3636 lthread_master_spawner(NULL);
3639 printf("Starting P-Threading Model\n");
3640 /* launch per-lcore init on every lcore */
3641 rte_eal_mp_remote_launch(pthread_run, NULL, CALL_MASTER);
3642 RTE_LCORE_FOREACH_SLAVE(lcore_id) {
3643 if (rte_eal_wait_lcore(lcore_id) < 0)