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
93 #ifndef __GLIBC__ /* sched_getcpu() is glibc specific */
94 #define sched_getcpu() rte_lcore_id()
98 * When set to zero, simple forwaring path is eanbled.
99 * When set to one, optimized forwarding path is enabled.
100 * Note that LPM optimisation path uses SSE4.1 instructions.
102 #if ((APP_LOOKUP_METHOD == APP_LOOKUP_LPM) && !defined(__SSE4_1__))
103 #define ENABLE_MULTI_BUFFER_OPTIMIZE 0
105 #define ENABLE_MULTI_BUFFER_OPTIMIZE 1
108 #if (APP_LOOKUP_METHOD == APP_LOOKUP_EXACT_MATCH)
109 #include <rte_hash.h>
110 #elif (APP_LOOKUP_METHOD == APP_LOOKUP_LPM)
112 #include <rte_lpm6.h>
114 #error "APP_LOOKUP_METHOD set to incorrect value"
117 #define RTE_LOGTYPE_L3FWD RTE_LOGTYPE_USER1
119 #define MAX_JUMBO_PKT_LEN 9600
121 #define IPV6_ADDR_LEN 16
123 #define MEMPOOL_CACHE_SIZE 256
126 * This expression is used to calculate the number of mbufs needed depending on
127 * user input, taking into account memory for rx and tx hardware rings, cache
128 * per lcore and mtable per port per lcore. RTE_MAX is used to ensure that
129 * NB_MBUF never goes below a minimum value of 8192
132 #define NB_MBUF RTE_MAX(\
133 (nb_ports*nb_rx_queue*RTE_TEST_RX_DESC_DEFAULT + \
134 nb_ports*nb_lcores*MAX_PKT_BURST + \
135 nb_ports*n_tx_queue*RTE_TEST_TX_DESC_DEFAULT + \
136 nb_lcores*MEMPOOL_CACHE_SIZE), \
139 #define MAX_PKT_BURST 32
140 #define BURST_TX_DRAIN_US 100 /* TX drain every ~100us */
143 * Try to avoid TX buffering if we have at least MAX_TX_BURST packets to send.
145 #define MAX_TX_BURST (MAX_PKT_BURST / 2)
146 #define BURST_SIZE MAX_TX_BURST
150 /* Configure how many packets ahead to prefetch, when reading packets */
151 #define PREFETCH_OFFSET 3
153 /* Used to mark destination port as 'invalid'. */
154 #define BAD_PORT ((uint16_t)-1)
159 * Configurable number of RX/TX ring descriptors
161 #define RTE_TEST_RX_DESC_DEFAULT 128
162 #define RTE_TEST_TX_DESC_DEFAULT 128
163 static uint16_t nb_rxd = RTE_TEST_RX_DESC_DEFAULT;
164 static uint16_t nb_txd = RTE_TEST_TX_DESC_DEFAULT;
166 /* ethernet addresses of ports */
167 static uint64_t dest_eth_addr[RTE_MAX_ETHPORTS];
168 static struct ether_addr ports_eth_addr[RTE_MAX_ETHPORTS];
170 static __m128i val_eth[RTE_MAX_ETHPORTS];
172 /* replace first 12B of the ethernet header. */
173 #define MASK_ETH 0x3f
175 /* mask of enabled ports */
176 static uint32_t enabled_port_mask;
177 static int promiscuous_on; /**< $et in promiscuous mode off by default. */
178 static int numa_on = 1; /**< NUMA is enabled by default. */
180 #if (APP_LOOKUP_METHOD == APP_LOOKUP_EXACT_MATCH)
181 static int ipv6; /**< ipv6 is false by default. */
184 #if (APP_CPU_LOAD == 1)
186 #define MAX_CPU RTE_MAX_LCORE
187 #define CPU_LOAD_TIMEOUT_US (5 * 1000 * 1000) /**< Timeout for collecting 5s */
189 #define CPU_PROCESS 0
191 #define MAX_CPU_COUNTER 2
196 uint64_t hits[MAX_CPU_COUNTER][MAX_CPU];
197 } __rte_cache_aligned;
199 static struct cpu_load cpu_load;
200 static int cpu_load_lcore_id = -1;
202 #define SET_CPU_BUSY(thread, counter) \
203 thread->conf.busy[counter] = 1
205 #define SET_CPU_IDLE(thread, counter) \
206 thread->conf.busy[counter] = 0
208 #define IS_CPU_BUSY(thread, counter) \
209 (thread->conf.busy[counter] > 0)
213 #define SET_CPU_BUSY(thread, counter)
214 #define SET_CPU_IDLE(thread, counter)
215 #define IS_CPU_BUSY(thread, counter) 0
221 struct rte_mbuf *m_table[MAX_PKT_BURST];
224 struct lcore_rx_queue {
227 } __rte_cache_aligned;
229 #define MAX_RX_QUEUE_PER_LCORE 16
230 #define MAX_TX_QUEUE_PER_PORT RTE_MAX_ETHPORTS
231 #define MAX_RX_QUEUE_PER_PORT 128
233 #define MAX_LCORE_PARAMS 1024
234 struct rx_thread_params {
239 } __rte_cache_aligned;
241 static struct rx_thread_params rx_thread_params_array[MAX_LCORE_PARAMS];
242 static struct rx_thread_params rx_thread_params_array_default[] = {
254 static struct rx_thread_params *rx_thread_params =
255 rx_thread_params_array_default;
256 static uint16_t nb_rx_thread_params = RTE_DIM(rx_thread_params_array_default);
258 struct tx_thread_params {
261 } __rte_cache_aligned;
263 static struct tx_thread_params tx_thread_params_array[MAX_LCORE_PARAMS];
264 static struct tx_thread_params tx_thread_params_array_default[] = {
276 static struct tx_thread_params *tx_thread_params =
277 tx_thread_params_array_default;
278 static uint16_t nb_tx_thread_params = RTE_DIM(tx_thread_params_array_default);
280 static struct rte_eth_conf port_conf = {
282 .mq_mode = ETH_MQ_RX_RSS,
283 .max_rx_pkt_len = ETHER_MAX_LEN,
285 .header_split = 0, /**< Header Split disabled */
286 .hw_ip_checksum = 1, /**< IP checksum offload enabled */
287 .hw_vlan_filter = 0, /**< VLAN filtering disabled */
288 .jumbo_frame = 0, /**< Jumbo Frame Support disabled */
289 .hw_strip_crc = 1, /**< CRC stripped by hardware */
294 .rss_hf = ETH_RSS_TCP,
298 .mq_mode = ETH_MQ_TX_NONE,
302 static struct rte_mempool *pktmbuf_pool[NB_SOCKETS];
304 #if (APP_LOOKUP_METHOD == APP_LOOKUP_EXACT_MATCH)
306 #ifdef RTE_MACHINE_CPUFLAG_SSE4_2
307 #include <rte_hash_crc.h>
308 #define DEFAULT_HASH_FUNC rte_hash_crc
310 #include <rte_jhash.h>
311 #define DEFAULT_HASH_FUNC rte_jhash
320 } __attribute__((__packed__));
322 union ipv4_5tuple_host {
335 #define XMM_NUM_IN_IPV6_5TUPLE 3
338 uint8_t ip_dst[IPV6_ADDR_LEN];
339 uint8_t ip_src[IPV6_ADDR_LEN];
343 } __attribute__((__packed__));
345 union ipv6_5tuple_host {
350 uint8_t ip_src[IPV6_ADDR_LEN];
351 uint8_t ip_dst[IPV6_ADDR_LEN];
356 __m128i xmm[XMM_NUM_IN_IPV6_5TUPLE];
359 struct ipv4_l3fwd_route {
360 struct ipv4_5tuple key;
364 struct ipv6_l3fwd_route {
365 struct ipv6_5tuple key;
369 static struct ipv4_l3fwd_route ipv4_l3fwd_route_array[] = {
370 {{IPv4(101, 0, 0, 0), IPv4(100, 10, 0, 1), 101, 11, IPPROTO_TCP}, 0},
371 {{IPv4(201, 0, 0, 0), IPv4(200, 20, 0, 1), 102, 12, IPPROTO_TCP}, 1},
372 {{IPv4(111, 0, 0, 0), IPv4(100, 30, 0, 1), 101, 11, IPPROTO_TCP}, 2},
373 {{IPv4(211, 0, 0, 0), IPv4(200, 40, 0, 1), 102, 12, IPPROTO_TCP}, 3},
376 static struct ipv6_l3fwd_route ipv6_l3fwd_route_array[] = {
378 {0xfe, 0x80, 0, 0, 0, 0, 0, 0, 0x02, 0x1e, 0x67, 0xff, 0xfe, 0, 0, 0},
379 {0xfe, 0x80, 0, 0, 0, 0, 0, 0, 0x02, 0x1b, 0x21, 0xff, 0xfe, 0x91, 0x38,
381 101, 11, IPPROTO_TCP}, 0},
384 {0xfe, 0x90, 0, 0, 0, 0, 0, 0, 0x02, 0x1e, 0x67, 0xff, 0xfe, 0, 0, 0},
385 {0xfe, 0x90, 0, 0, 0, 0, 0, 0, 0x02, 0x1b, 0x21, 0xff, 0xfe, 0x91, 0x38,
387 102, 12, IPPROTO_TCP}, 1},
390 {0xfe, 0xa0, 0, 0, 0, 0, 0, 0, 0x02, 0x1e, 0x67, 0xff, 0xfe, 0, 0, 0},
391 {0xfe, 0xa0, 0, 0, 0, 0, 0, 0, 0x02, 0x1b, 0x21, 0xff, 0xfe, 0x91, 0x38,
393 101, 11, IPPROTO_TCP}, 2},
396 {0xfe, 0xb0, 0, 0, 0, 0, 0, 0, 0x02, 0x1e, 0x67, 0xff, 0xfe, 0, 0, 0},
397 {0xfe, 0xb0, 0, 0, 0, 0, 0, 0, 0x02, 0x1b, 0x21, 0xff, 0xfe, 0x91, 0x38,
399 102, 12, IPPROTO_TCP}, 3},
402 typedef struct rte_hash lookup_struct_t;
403 static lookup_struct_t *ipv4_l3fwd_lookup_struct[NB_SOCKETS];
404 static lookup_struct_t *ipv6_l3fwd_lookup_struct[NB_SOCKETS];
406 #ifdef RTE_ARCH_X86_64
407 /* default to 4 million hash entries (approx) */
408 #define L3FWD_HASH_ENTRIES (1024*1024*4)
410 /* 32-bit has less address-space for hugepage memory, limit to 1M entries */
411 #define L3FWD_HASH_ENTRIES (1024*1024*1)
413 #define HASH_ENTRY_NUMBER_DEFAULT 4
415 static uint32_t hash_entry_number = HASH_ENTRY_NUMBER_DEFAULT;
417 static inline uint32_t
418 ipv4_hash_crc(const void *data, __rte_unused uint32_t data_len,
421 const union ipv4_5tuple_host *k;
427 p = (const uint32_t *)&k->port_src;
429 #ifdef RTE_MACHINE_CPUFLAG_SSE4_2
430 init_val = rte_hash_crc_4byte(t, init_val);
431 init_val = rte_hash_crc_4byte(k->ip_src, init_val);
432 init_val = rte_hash_crc_4byte(k->ip_dst, init_val);
433 init_val = rte_hash_crc_4byte(*p, init_val);
434 #else /* RTE_MACHINE_CPUFLAG_SSE4_2 */
435 init_val = rte_jhash_1word(t, init_val);
436 init_val = rte_jhash_1word(k->ip_src, init_val);
437 init_val = rte_jhash_1word(k->ip_dst, init_val);
438 init_val = rte_jhash_1word(*p, init_val);
439 #endif /* RTE_MACHINE_CPUFLAG_SSE4_2 */
443 static inline uint32_t
444 ipv6_hash_crc(const void *data, __rte_unused uint32_t data_len,
447 const union ipv6_5tuple_host *k;
450 #ifdef RTE_MACHINE_CPUFLAG_SSE4_2
451 const uint32_t *ip_src0, *ip_src1, *ip_src2, *ip_src3;
452 const uint32_t *ip_dst0, *ip_dst1, *ip_dst2, *ip_dst3;
453 #endif /* RTE_MACHINE_CPUFLAG_SSE4_2 */
457 p = (const uint32_t *)&k->port_src;
459 #ifdef RTE_MACHINE_CPUFLAG_SSE4_2
460 ip_src0 = (const uint32_t *) k->ip_src;
461 ip_src1 = (const uint32_t *)(k->ip_src + 4);
462 ip_src2 = (const uint32_t *)(k->ip_src + 8);
463 ip_src3 = (const uint32_t *)(k->ip_src + 12);
464 ip_dst0 = (const uint32_t *) k->ip_dst;
465 ip_dst1 = (const uint32_t *)(k->ip_dst + 4);
466 ip_dst2 = (const uint32_t *)(k->ip_dst + 8);
467 ip_dst3 = (const uint32_t *)(k->ip_dst + 12);
468 init_val = rte_hash_crc_4byte(t, init_val);
469 init_val = rte_hash_crc_4byte(*ip_src0, init_val);
470 init_val = rte_hash_crc_4byte(*ip_src1, init_val);
471 init_val = rte_hash_crc_4byte(*ip_src2, init_val);
472 init_val = rte_hash_crc_4byte(*ip_src3, init_val);
473 init_val = rte_hash_crc_4byte(*ip_dst0, init_val);
474 init_val = rte_hash_crc_4byte(*ip_dst1, init_val);
475 init_val = rte_hash_crc_4byte(*ip_dst2, init_val);
476 init_val = rte_hash_crc_4byte(*ip_dst3, init_val);
477 init_val = rte_hash_crc_4byte(*p, init_val);
478 #else /* RTE_MACHINE_CPUFLAG_SSE4_2 */
479 init_val = rte_jhash_1word(t, init_val);
480 init_val = rte_jhash(k->ip_src, sizeof(uint8_t) * IPV6_ADDR_LEN, init_val);
481 init_val = rte_jhash(k->ip_dst, sizeof(uint8_t) * IPV6_ADDR_LEN, init_val);
482 init_val = rte_jhash_1word(*p, init_val);
483 #endif /* RTE_MACHINE_CPUFLAG_SSE4_2 */
487 #define IPV4_L3FWD_NUM_ROUTES RTE_DIM(ipv4_l3fwd_route_array)
488 #define IPV6_L3FWD_NUM_ROUTES RTE_DIM(ipv6_l3fwd_route_array)
490 static uint8_t ipv4_l3fwd_out_if[L3FWD_HASH_ENTRIES] __rte_cache_aligned;
491 static uint8_t ipv6_l3fwd_out_if[L3FWD_HASH_ENTRIES] __rte_cache_aligned;
495 #if (APP_LOOKUP_METHOD == APP_LOOKUP_LPM)
496 struct ipv4_l3fwd_route {
502 struct ipv6_l3fwd_route {
508 static struct ipv4_l3fwd_route ipv4_l3fwd_route_array[] = {
509 {IPv4(1, 1, 1, 0), 24, 0},
510 {IPv4(2, 1, 1, 0), 24, 1},
511 {IPv4(3, 1, 1, 0), 24, 2},
512 {IPv4(4, 1, 1, 0), 24, 3},
513 {IPv4(5, 1, 1, 0), 24, 4},
514 {IPv4(6, 1, 1, 0), 24, 5},
515 {IPv4(7, 1, 1, 0), 24, 6},
516 {IPv4(8, 1, 1, 0), 24, 7},
519 static struct ipv6_l3fwd_route ipv6_l3fwd_route_array[] = {
520 {{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, 48, 0},
521 {{2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, 48, 1},
522 {{3, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, 48, 2},
523 {{4, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, 48, 3},
524 {{5, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, 48, 4},
525 {{6, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, 48, 5},
526 {{7, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, 48, 6},
527 {{8, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, 48, 7},
530 #define IPV4_L3FWD_NUM_ROUTES RTE_DIM(ipv4_l3fwd_route_array)
531 #define IPV6_L3FWD_NUM_ROUTES RTE_DIM(ipv6_l3fwd_route_array)
533 #define IPV4_L3FWD_LPM_MAX_RULES 1024
534 #define IPV6_L3FWD_LPM_MAX_RULES 1024
535 #define IPV6_L3FWD_LPM_NUMBER_TBL8S (1 << 16)
537 typedef struct rte_lpm lookup_struct_t;
538 typedef struct rte_lpm6 lookup6_struct_t;
539 static lookup_struct_t *ipv4_l3fwd_lookup_struct[NB_SOCKETS];
540 static lookup6_struct_t *ipv6_l3fwd_lookup_struct[NB_SOCKETS];
544 lookup_struct_t *ipv4_lookup_struct;
545 #if (APP_LOOKUP_METHOD == APP_LOOKUP_LPM)
546 lookup6_struct_t *ipv6_lookup_struct;
548 lookup_struct_t *ipv6_lookup_struct;
551 } __rte_cache_aligned;
553 static struct lcore_conf lcore_conf[RTE_MAX_LCORE];
554 RTE_DEFINE_PER_LCORE(struct lcore_conf *, lcore_conf);
556 #define MAX_RX_QUEUE_PER_THREAD 16
557 #define MAX_TX_PORT_PER_THREAD RTE_MAX_ETHPORTS
558 #define MAX_TX_QUEUE_PER_PORT RTE_MAX_ETHPORTS
559 #define MAX_RX_QUEUE_PER_PORT 128
561 #define MAX_RX_THREAD 1024
562 #define MAX_TX_THREAD 1024
563 #define MAX_THREAD (MAX_RX_THREAD + MAX_TX_THREAD)
566 * Producers and consumers threads configuration
568 static int lthreads_on = 1; /**< Use lthreads for processing*/
570 rte_atomic16_t rx_counter; /**< Number of spawned rx threads */
571 rte_atomic16_t tx_counter; /**< Number of spawned tx threads */
574 uint16_t lcore_id; /**< Initial lcore for rx thread */
575 uint16_t cpu_id; /**< Cpu id for cpu load stats counter */
576 uint16_t thread_id; /**< Thread ID */
578 #if (APP_CPU_LOAD > 0)
579 int busy[MAX_CPU_COUNTER];
583 struct thread_rx_conf {
584 struct thread_conf conf;
587 struct lcore_rx_queue rx_queue_list[MAX_RX_QUEUE_PER_LCORE];
589 uint16_t n_ring; /**< Number of output rings */
590 struct rte_ring *ring[RTE_MAX_LCORE];
591 struct lthread_cond *ready[RTE_MAX_LCORE];
593 #if (APP_CPU_LOAD > 0)
594 int busy[MAX_CPU_COUNTER];
596 } __rte_cache_aligned;
598 uint16_t n_rx_thread;
599 struct thread_rx_conf rx_thread[MAX_RX_THREAD];
601 struct thread_tx_conf {
602 struct thread_conf conf;
604 uint16_t tx_queue_id[RTE_MAX_LCORE];
605 struct mbuf_table tx_mbufs[RTE_MAX_LCORE];
607 struct rte_ring *ring;
608 struct lthread_cond **ready;
610 } __rte_cache_aligned;
612 uint16_t n_tx_thread;
613 struct thread_tx_conf tx_thread[MAX_TX_THREAD];
615 /* Send burst of packets on an output interface */
617 send_burst(struct thread_tx_conf *qconf, uint16_t n, uint8_t port)
619 struct rte_mbuf **m_table;
623 queueid = qconf->tx_queue_id[port];
624 m_table = (struct rte_mbuf **)qconf->tx_mbufs[port].m_table;
626 ret = rte_eth_tx_burst(port, queueid, m_table, n);
627 if (unlikely(ret < n)) {
629 rte_pktmbuf_free(m_table[ret]);
636 /* Enqueue a single packet, and send burst if queue is filled */
638 send_single_packet(struct rte_mbuf *m, uint8_t port)
641 struct thread_tx_conf *qconf;
644 qconf = (struct thread_tx_conf *)lthread_get_data();
646 qconf = (struct thread_tx_conf *)RTE_PER_LCORE(lcore_conf)->data;
648 len = qconf->tx_mbufs[port].len;
649 qconf->tx_mbufs[port].m_table[len] = m;
652 /* enough pkts to be sent */
653 if (unlikely(len == MAX_PKT_BURST)) {
654 send_burst(qconf, MAX_PKT_BURST, port);
658 qconf->tx_mbufs[port].len = len;
662 #if ((APP_LOOKUP_METHOD == APP_LOOKUP_LPM) && \
663 (ENABLE_MULTI_BUFFER_OPTIMIZE == 1))
664 static inline __attribute__((always_inline)) void
665 send_packetsx4(uint8_t port,
666 struct rte_mbuf *m[], uint32_t num)
669 struct thread_tx_conf *qconf;
672 qconf = (struct thread_tx_conf *)lthread_get_data();
674 qconf = (struct thread_tx_conf *)RTE_PER_LCORE(lcore_conf)->data;
676 len = qconf->tx_mbufs[port].len;
679 * If TX buffer for that queue is empty, and we have enough packets,
680 * then send them straightway.
682 if (num >= MAX_TX_BURST && len == 0) {
683 n = rte_eth_tx_burst(port, qconf->tx_queue_id[port], m, num);
684 if (unlikely(n < num)) {
686 rte_pktmbuf_free(m[n]);
693 * Put packets into TX buffer for that queue.
697 n = (n > MAX_PKT_BURST) ? MAX_PKT_BURST - len : num;
700 switch (n % FWDSTEP) {
703 qconf->tx_mbufs[port].m_table[len + j] = m[j];
706 qconf->tx_mbufs[port].m_table[len + j] = m[j];
709 qconf->tx_mbufs[port].m_table[len + j] = m[j];
712 qconf->tx_mbufs[port].m_table[len + j] = m[j];
719 /* enough pkts to be sent */
720 if (unlikely(len == MAX_PKT_BURST)) {
722 send_burst(qconf, MAX_PKT_BURST, port);
724 /* copy rest of the packets into the TX buffer. */
727 switch (len % FWDSTEP) {
730 qconf->tx_mbufs[port].m_table[j] = m[n + j];
733 qconf->tx_mbufs[port].m_table[j] = m[n + j];
736 qconf->tx_mbufs[port].m_table[j] = m[n + j];
739 qconf->tx_mbufs[port].m_table[j] = m[n + j];
745 qconf->tx_mbufs[port].len = len;
747 #endif /* APP_LOOKUP_LPM */
749 #ifdef DO_RFC_1812_CHECKS
751 is_valid_ipv4_pkt(struct ipv4_hdr *pkt, uint32_t link_len)
753 /* From http://www.rfc-editor.org/rfc/rfc1812.txt section 5.2.2 */
755 * 1. The packet length reported by the Link Layer must be large
756 * enough to hold the minimum length legal IP datagram (20 bytes).
758 if (link_len < sizeof(struct ipv4_hdr))
761 /* 2. The IP checksum must be correct. */
762 /* this is checked in H/W */
765 * 3. The IP version number must be 4. If the version number is not 4
766 * then the packet may be another version of IP, such as IPng or
769 if (((pkt->version_ihl) >> 4) != 4)
772 * 4. The IP header length field must be large enough to hold the
773 * minimum length legal IP datagram (20 bytes = 5 words).
775 if ((pkt->version_ihl & 0xf) < 5)
779 * 5. The IP total length field must be large enough to hold the IP
780 * datagram header, whose length is specified in the IP header length
783 if (rte_cpu_to_be_16(pkt->total_length) < sizeof(struct ipv4_hdr))
790 #if (APP_LOOKUP_METHOD == APP_LOOKUP_EXACT_MATCH)
792 static __m128i mask0;
793 static __m128i mask1;
794 static __m128i mask2;
795 static inline uint8_t
796 get_ipv4_dst_port(void *ipv4_hdr, uint8_t portid,
797 lookup_struct_t *ipv4_l3fwd_lookup_struct)
800 union ipv4_5tuple_host key;
802 ipv4_hdr = (uint8_t *)ipv4_hdr + offsetof(struct ipv4_hdr, time_to_live);
803 __m128i data = _mm_loadu_si128((__m128i *)(ipv4_hdr));
804 /* Get 5 tuple: dst port, src port, dst IP address, src IP address and
806 key.xmm = _mm_and_si128(data, mask0);
807 /* Find destination port */
808 ret = rte_hash_lookup(ipv4_l3fwd_lookup_struct, (const void *)&key);
809 return (uint8_t)((ret < 0) ? portid : ipv4_l3fwd_out_if[ret]);
812 static inline uint8_t
813 get_ipv6_dst_port(void *ipv6_hdr, uint8_t portid,
814 lookup_struct_t *ipv6_l3fwd_lookup_struct)
817 union ipv6_5tuple_host key;
819 ipv6_hdr = (uint8_t *)ipv6_hdr + offsetof(struct ipv6_hdr, payload_len);
820 __m128i data0 = _mm_loadu_si128((__m128i *)(ipv6_hdr));
821 __m128i data1 = _mm_loadu_si128((__m128i *)(((uint8_t *)ipv6_hdr) +
823 __m128i data2 = _mm_loadu_si128((__m128i *)(((uint8_t *)ipv6_hdr) +
824 sizeof(__m128i) + sizeof(__m128i)));
825 /* Get part of 5 tuple: src IP address lower 96 bits and protocol */
826 key.xmm[0] = _mm_and_si128(data0, mask1);
827 /* Get part of 5 tuple: dst IP address lower 96 bits and src IP address
830 /* Get part of 5 tuple: dst port and src port and dst IP address higher
832 key.xmm[2] = _mm_and_si128(data2, mask2);
834 /* Find destination port */
835 ret = rte_hash_lookup(ipv6_l3fwd_lookup_struct, (const void *)&key);
836 return (uint8_t)((ret < 0) ? portid : ipv6_l3fwd_out_if[ret]);
840 #if (APP_LOOKUP_METHOD == APP_LOOKUP_LPM)
842 static inline uint8_t
843 get_ipv4_dst_port(void *ipv4_hdr, uint8_t portid,
844 lookup_struct_t *ipv4_l3fwd_lookup_struct)
848 return (uint8_t)((rte_lpm_lookup(ipv4_l3fwd_lookup_struct,
849 rte_be_to_cpu_32(((struct ipv4_hdr *)ipv4_hdr)->dst_addr),
850 &next_hop) == 0) ? next_hop : portid);
853 static inline uint8_t
854 get_ipv6_dst_port(void *ipv6_hdr, uint8_t portid,
855 lookup6_struct_t *ipv6_l3fwd_lookup_struct)
859 return (uint8_t) ((rte_lpm6_lookup(ipv6_l3fwd_lookup_struct,
860 ((struct ipv6_hdr *)ipv6_hdr)->dst_addr, &next_hop) == 0) ?
865 static inline void l3fwd_simple_forward(struct rte_mbuf *m, uint8_t portid)
866 __attribute__((unused));
868 #if ((APP_LOOKUP_METHOD == APP_LOOKUP_EXACT_MATCH) && \
869 (ENABLE_MULTI_BUFFER_OPTIMIZE == 1))
871 #define MASK_ALL_PKTS 0xff
872 #define EXCLUDE_1ST_PKT 0xfe
873 #define EXCLUDE_2ND_PKT 0xfd
874 #define EXCLUDE_3RD_PKT 0xfb
875 #define EXCLUDE_4TH_PKT 0xf7
876 #define EXCLUDE_5TH_PKT 0xef
877 #define EXCLUDE_6TH_PKT 0xdf
878 #define EXCLUDE_7TH_PKT 0xbf
879 #define EXCLUDE_8TH_PKT 0x7f
882 simple_ipv4_fwd_8pkts(struct rte_mbuf *m[8], uint8_t portid)
884 struct ether_hdr *eth_hdr[8];
885 struct ipv4_hdr *ipv4_hdr[8];
888 union ipv4_5tuple_host key[8];
891 eth_hdr[0] = rte_pktmbuf_mtod(m[0], struct ether_hdr *);
892 eth_hdr[1] = rte_pktmbuf_mtod(m[1], struct ether_hdr *);
893 eth_hdr[2] = rte_pktmbuf_mtod(m[2], struct ether_hdr *);
894 eth_hdr[3] = rte_pktmbuf_mtod(m[3], struct ether_hdr *);
895 eth_hdr[4] = rte_pktmbuf_mtod(m[4], struct ether_hdr *);
896 eth_hdr[5] = rte_pktmbuf_mtod(m[5], struct ether_hdr *);
897 eth_hdr[6] = rte_pktmbuf_mtod(m[6], struct ether_hdr *);
898 eth_hdr[7] = rte_pktmbuf_mtod(m[7], struct ether_hdr *);
900 /* Handle IPv4 headers.*/
901 ipv4_hdr[0] = rte_pktmbuf_mtod_offset(m[0], struct ipv4_hdr *,
902 sizeof(struct ether_hdr));
903 ipv4_hdr[1] = rte_pktmbuf_mtod_offset(m[1], struct ipv4_hdr *,
904 sizeof(struct ether_hdr));
905 ipv4_hdr[2] = rte_pktmbuf_mtod_offset(m[2], struct ipv4_hdr *,
906 sizeof(struct ether_hdr));
907 ipv4_hdr[3] = rte_pktmbuf_mtod_offset(m[3], struct ipv4_hdr *,
908 sizeof(struct ether_hdr));
909 ipv4_hdr[4] = rte_pktmbuf_mtod_offset(m[4], struct ipv4_hdr *,
910 sizeof(struct ether_hdr));
911 ipv4_hdr[5] = rte_pktmbuf_mtod_offset(m[5], struct ipv4_hdr *,
912 sizeof(struct ether_hdr));
913 ipv4_hdr[6] = rte_pktmbuf_mtod_offset(m[6], struct ipv4_hdr *,
914 sizeof(struct ether_hdr));
915 ipv4_hdr[7] = rte_pktmbuf_mtod_offset(m[7], struct ipv4_hdr *,
916 sizeof(struct ether_hdr));
918 #ifdef DO_RFC_1812_CHECKS
919 /* Check to make sure the packet is valid (RFC1812) */
920 uint8_t valid_mask = MASK_ALL_PKTS;
922 if (is_valid_ipv4_pkt(ipv4_hdr[0], m[0]->pkt_len) < 0) {
923 rte_pktmbuf_free(m[0]);
924 valid_mask &= EXCLUDE_1ST_PKT;
926 if (is_valid_ipv4_pkt(ipv4_hdr[1], m[1]->pkt_len) < 0) {
927 rte_pktmbuf_free(m[1]);
928 valid_mask &= EXCLUDE_2ND_PKT;
930 if (is_valid_ipv4_pkt(ipv4_hdr[2], m[2]->pkt_len) < 0) {
931 rte_pktmbuf_free(m[2]);
932 valid_mask &= EXCLUDE_3RD_PKT;
934 if (is_valid_ipv4_pkt(ipv4_hdr[3], m[3]->pkt_len) < 0) {
935 rte_pktmbuf_free(m[3]);
936 valid_mask &= EXCLUDE_4TH_PKT;
938 if (is_valid_ipv4_pkt(ipv4_hdr[4], m[4]->pkt_len) < 0) {
939 rte_pktmbuf_free(m[4]);
940 valid_mask &= EXCLUDE_5TH_PKT;
942 if (is_valid_ipv4_pkt(ipv4_hdr[5], m[5]->pkt_len) < 0) {
943 rte_pktmbuf_free(m[5]);
944 valid_mask &= EXCLUDE_6TH_PKT;
946 if (is_valid_ipv4_pkt(ipv4_hdr[6], m[6]->pkt_len) < 0) {
947 rte_pktmbuf_free(m[6]);
948 valid_mask &= EXCLUDE_7TH_PKT;
950 if (is_valid_ipv4_pkt(ipv4_hdr[7], m[7]->pkt_len) < 0) {
951 rte_pktmbuf_free(m[7]);
952 valid_mask &= EXCLUDE_8TH_PKT;
954 if (unlikely(valid_mask != MASK_ALL_PKTS)) {
960 for (i = 0; i < 8; i++)
961 if ((0x1 << i) & valid_mask)
962 l3fwd_simple_forward(m[i], portid);
964 #endif /* End of #ifdef DO_RFC_1812_CHECKS */
966 data[0] = _mm_loadu_si128(rte_pktmbuf_mtod_offset(m[0], __m128i *,
967 sizeof(struct ether_hdr) +
968 offsetof(struct ipv4_hdr, time_to_live)));
969 data[1] = _mm_loadu_si128(rte_pktmbuf_mtod_offset(m[1], __m128i *,
970 sizeof(struct ether_hdr) +
971 offsetof(struct ipv4_hdr, time_to_live)));
972 data[2] = _mm_loadu_si128(rte_pktmbuf_mtod_offset(m[2], __m128i *,
973 sizeof(struct ether_hdr) +
974 offsetof(struct ipv4_hdr, time_to_live)));
975 data[3] = _mm_loadu_si128(rte_pktmbuf_mtod_offset(m[3], __m128i *,
976 sizeof(struct ether_hdr) +
977 offsetof(struct ipv4_hdr, time_to_live)));
978 data[4] = _mm_loadu_si128(rte_pktmbuf_mtod_offset(m[4], __m128i *,
979 sizeof(struct ether_hdr) +
980 offsetof(struct ipv4_hdr, time_to_live)));
981 data[5] = _mm_loadu_si128(rte_pktmbuf_mtod_offset(m[5], __m128i *,
982 sizeof(struct ether_hdr) +
983 offsetof(struct ipv4_hdr, time_to_live)));
984 data[6] = _mm_loadu_si128(rte_pktmbuf_mtod_offset(m[6], __m128i *,
985 sizeof(struct ether_hdr) +
986 offsetof(struct ipv4_hdr, time_to_live)));
987 data[7] = _mm_loadu_si128(rte_pktmbuf_mtod_offset(m[7], __m128i *,
988 sizeof(struct ether_hdr) +
989 offsetof(struct ipv4_hdr, time_to_live)));
991 key[0].xmm = _mm_and_si128(data[0], mask0);
992 key[1].xmm = _mm_and_si128(data[1], mask0);
993 key[2].xmm = _mm_and_si128(data[2], mask0);
994 key[3].xmm = _mm_and_si128(data[3], mask0);
995 key[4].xmm = _mm_and_si128(data[4], mask0);
996 key[5].xmm = _mm_and_si128(data[5], mask0);
997 key[6].xmm = _mm_and_si128(data[6], mask0);
998 key[7].xmm = _mm_and_si128(data[7], mask0);
1000 const void *key_array[8] = {&key[0], &key[1], &key[2], &key[3],
1001 &key[4], &key[5], &key[6], &key[7]};
1003 rte_hash_lookup_bulk(RTE_PER_LCORE(lcore_conf)->ipv4_lookup_struct,
1004 &key_array[0], 8, ret);
1005 dst_port[0] = (uint8_t) ((ret[0] < 0) ? portid : ipv4_l3fwd_out_if[ret[0]]);
1006 dst_port[1] = (uint8_t) ((ret[1] < 0) ? portid : ipv4_l3fwd_out_if[ret[1]]);
1007 dst_port[2] = (uint8_t) ((ret[2] < 0) ? portid : ipv4_l3fwd_out_if[ret[2]]);
1008 dst_port[3] = (uint8_t) ((ret[3] < 0) ? portid : ipv4_l3fwd_out_if[ret[3]]);
1009 dst_port[4] = (uint8_t) ((ret[4] < 0) ? portid : ipv4_l3fwd_out_if[ret[4]]);
1010 dst_port[5] = (uint8_t) ((ret[5] < 0) ? portid : ipv4_l3fwd_out_if[ret[5]]);
1011 dst_port[6] = (uint8_t) ((ret[6] < 0) ? portid : ipv4_l3fwd_out_if[ret[6]]);
1012 dst_port[7] = (uint8_t) ((ret[7] < 0) ? portid : ipv4_l3fwd_out_if[ret[7]]);
1014 if (dst_port[0] >= RTE_MAX_ETHPORTS ||
1015 (enabled_port_mask & 1 << dst_port[0]) == 0)
1016 dst_port[0] = portid;
1017 if (dst_port[1] >= RTE_MAX_ETHPORTS ||
1018 (enabled_port_mask & 1 << dst_port[1]) == 0)
1019 dst_port[1] = portid;
1020 if (dst_port[2] >= RTE_MAX_ETHPORTS ||
1021 (enabled_port_mask & 1 << dst_port[2]) == 0)
1022 dst_port[2] = portid;
1023 if (dst_port[3] >= RTE_MAX_ETHPORTS ||
1024 (enabled_port_mask & 1 << dst_port[3]) == 0)
1025 dst_port[3] = portid;
1026 if (dst_port[4] >= RTE_MAX_ETHPORTS ||
1027 (enabled_port_mask & 1 << dst_port[4]) == 0)
1028 dst_port[4] = portid;
1029 if (dst_port[5] >= RTE_MAX_ETHPORTS ||
1030 (enabled_port_mask & 1 << dst_port[5]) == 0)
1031 dst_port[5] = portid;
1032 if (dst_port[6] >= RTE_MAX_ETHPORTS ||
1033 (enabled_port_mask & 1 << dst_port[6]) == 0)
1034 dst_port[6] = portid;
1035 if (dst_port[7] >= RTE_MAX_ETHPORTS ||
1036 (enabled_port_mask & 1 << dst_port[7]) == 0)
1037 dst_port[7] = portid;
1039 #ifdef DO_RFC_1812_CHECKS
1040 /* Update time to live and header checksum */
1041 --(ipv4_hdr[0]->time_to_live);
1042 --(ipv4_hdr[1]->time_to_live);
1043 --(ipv4_hdr[2]->time_to_live);
1044 --(ipv4_hdr[3]->time_to_live);
1045 ++(ipv4_hdr[0]->hdr_checksum);
1046 ++(ipv4_hdr[1]->hdr_checksum);
1047 ++(ipv4_hdr[2]->hdr_checksum);
1048 ++(ipv4_hdr[3]->hdr_checksum);
1049 --(ipv4_hdr[4]->time_to_live);
1050 --(ipv4_hdr[5]->time_to_live);
1051 --(ipv4_hdr[6]->time_to_live);
1052 --(ipv4_hdr[7]->time_to_live);
1053 ++(ipv4_hdr[4]->hdr_checksum);
1054 ++(ipv4_hdr[5]->hdr_checksum);
1055 ++(ipv4_hdr[6]->hdr_checksum);
1056 ++(ipv4_hdr[7]->hdr_checksum);
1060 *(uint64_t *)ð_hdr[0]->d_addr = dest_eth_addr[dst_port[0]];
1061 *(uint64_t *)ð_hdr[1]->d_addr = dest_eth_addr[dst_port[1]];
1062 *(uint64_t *)ð_hdr[2]->d_addr = dest_eth_addr[dst_port[2]];
1063 *(uint64_t *)ð_hdr[3]->d_addr = dest_eth_addr[dst_port[3]];
1064 *(uint64_t *)ð_hdr[4]->d_addr = dest_eth_addr[dst_port[4]];
1065 *(uint64_t *)ð_hdr[5]->d_addr = dest_eth_addr[dst_port[5]];
1066 *(uint64_t *)ð_hdr[6]->d_addr = dest_eth_addr[dst_port[6]];
1067 *(uint64_t *)ð_hdr[7]->d_addr = dest_eth_addr[dst_port[7]];
1070 ether_addr_copy(&ports_eth_addr[dst_port[0]], ð_hdr[0]->s_addr);
1071 ether_addr_copy(&ports_eth_addr[dst_port[1]], ð_hdr[1]->s_addr);
1072 ether_addr_copy(&ports_eth_addr[dst_port[2]], ð_hdr[2]->s_addr);
1073 ether_addr_copy(&ports_eth_addr[dst_port[3]], ð_hdr[3]->s_addr);
1074 ether_addr_copy(&ports_eth_addr[dst_port[4]], ð_hdr[4]->s_addr);
1075 ether_addr_copy(&ports_eth_addr[dst_port[5]], ð_hdr[5]->s_addr);
1076 ether_addr_copy(&ports_eth_addr[dst_port[6]], ð_hdr[6]->s_addr);
1077 ether_addr_copy(&ports_eth_addr[dst_port[7]], ð_hdr[7]->s_addr);
1079 send_single_packet(m[0], (uint8_t)dst_port[0]);
1080 send_single_packet(m[1], (uint8_t)dst_port[1]);
1081 send_single_packet(m[2], (uint8_t)dst_port[2]);
1082 send_single_packet(m[3], (uint8_t)dst_port[3]);
1083 send_single_packet(m[4], (uint8_t)dst_port[4]);
1084 send_single_packet(m[5], (uint8_t)dst_port[5]);
1085 send_single_packet(m[6], (uint8_t)dst_port[6]);
1086 send_single_packet(m[7], (uint8_t)dst_port[7]);
1090 static inline void get_ipv6_5tuple(struct rte_mbuf *m0, __m128i mask0,
1091 __m128i mask1, union ipv6_5tuple_host *key)
1093 __m128i tmpdata0 = _mm_loadu_si128(rte_pktmbuf_mtod_offset(m0,
1094 __m128i *, sizeof(struct ether_hdr) +
1095 offsetof(struct ipv6_hdr, payload_len)));
1096 __m128i tmpdata1 = _mm_loadu_si128(rte_pktmbuf_mtod_offset(m0,
1097 __m128i *, sizeof(struct ether_hdr) +
1098 offsetof(struct ipv6_hdr, payload_len) + sizeof(__m128i)));
1099 __m128i tmpdata2 = _mm_loadu_si128(rte_pktmbuf_mtod_offset(m0,
1100 __m128i *, sizeof(struct ether_hdr) +
1101 offsetof(struct ipv6_hdr, payload_len) + sizeof(__m128i) +
1103 key->xmm[0] = _mm_and_si128(tmpdata0, mask0);
1104 key->xmm[1] = tmpdata1;
1105 key->xmm[2] = _mm_and_si128(tmpdata2, mask1);
1109 simple_ipv6_fwd_8pkts(struct rte_mbuf *m[8], uint8_t portid)
1112 uint8_t dst_port[8];
1113 struct ether_hdr *eth_hdr[8];
1114 union ipv6_5tuple_host key[8];
1116 __attribute__((unused)) struct ipv6_hdr *ipv6_hdr[8];
1118 eth_hdr[0] = rte_pktmbuf_mtod(m[0], struct ether_hdr *);
1119 eth_hdr[1] = rte_pktmbuf_mtod(m[1], struct ether_hdr *);
1120 eth_hdr[2] = rte_pktmbuf_mtod(m[2], struct ether_hdr *);
1121 eth_hdr[3] = rte_pktmbuf_mtod(m[3], struct ether_hdr *);
1122 eth_hdr[4] = rte_pktmbuf_mtod(m[4], struct ether_hdr *);
1123 eth_hdr[5] = rte_pktmbuf_mtod(m[5], struct ether_hdr *);
1124 eth_hdr[6] = rte_pktmbuf_mtod(m[6], struct ether_hdr *);
1125 eth_hdr[7] = rte_pktmbuf_mtod(m[7], struct ether_hdr *);
1127 /* Handle IPv6 headers.*/
1128 ipv6_hdr[0] = rte_pktmbuf_mtod_offset(m[0], struct ipv6_hdr *,
1129 sizeof(struct ether_hdr));
1130 ipv6_hdr[1] = rte_pktmbuf_mtod_offset(m[1], struct ipv6_hdr *,
1131 sizeof(struct ether_hdr));
1132 ipv6_hdr[2] = rte_pktmbuf_mtod_offset(m[2], struct ipv6_hdr *,
1133 sizeof(struct ether_hdr));
1134 ipv6_hdr[3] = rte_pktmbuf_mtod_offset(m[3], struct ipv6_hdr *,
1135 sizeof(struct ether_hdr));
1136 ipv6_hdr[4] = rte_pktmbuf_mtod_offset(m[4], struct ipv6_hdr *,
1137 sizeof(struct ether_hdr));
1138 ipv6_hdr[5] = rte_pktmbuf_mtod_offset(m[5], struct ipv6_hdr *,
1139 sizeof(struct ether_hdr));
1140 ipv6_hdr[6] = rte_pktmbuf_mtod_offset(m[6], struct ipv6_hdr *,
1141 sizeof(struct ether_hdr));
1142 ipv6_hdr[7] = rte_pktmbuf_mtod_offset(m[7], struct ipv6_hdr *,
1143 sizeof(struct ether_hdr));
1145 get_ipv6_5tuple(m[0], mask1, mask2, &key[0]);
1146 get_ipv6_5tuple(m[1], mask1, mask2, &key[1]);
1147 get_ipv6_5tuple(m[2], mask1, mask2, &key[2]);
1148 get_ipv6_5tuple(m[3], mask1, mask2, &key[3]);
1149 get_ipv6_5tuple(m[4], mask1, mask2, &key[4]);
1150 get_ipv6_5tuple(m[5], mask1, mask2, &key[5]);
1151 get_ipv6_5tuple(m[6], mask1, mask2, &key[6]);
1152 get_ipv6_5tuple(m[7], mask1, mask2, &key[7]);
1154 const void *key_array[8] = {&key[0], &key[1], &key[2], &key[3],
1155 &key[4], &key[5], &key[6], &key[7]};
1157 rte_hash_lookup_bulk(RTE_PER_LCORE(lcore_conf)->ipv6_lookup_struct,
1158 &key_array[0], 4, ret);
1159 dst_port[0] = (uint8_t) ((ret[0] < 0) ? portid : ipv6_l3fwd_out_if[ret[0]]);
1160 dst_port[1] = (uint8_t) ((ret[1] < 0) ? portid : ipv6_l3fwd_out_if[ret[1]]);
1161 dst_port[2] = (uint8_t) ((ret[2] < 0) ? portid : ipv6_l3fwd_out_if[ret[2]]);
1162 dst_port[3] = (uint8_t) ((ret[3] < 0) ? portid : ipv6_l3fwd_out_if[ret[3]]);
1163 dst_port[4] = (uint8_t) ((ret[4] < 0) ? portid : ipv6_l3fwd_out_if[ret[4]]);
1164 dst_port[5] = (uint8_t) ((ret[5] < 0) ? portid : ipv6_l3fwd_out_if[ret[5]]);
1165 dst_port[6] = (uint8_t) ((ret[6] < 0) ? portid : ipv6_l3fwd_out_if[ret[6]]);
1166 dst_port[7] = (uint8_t) ((ret[7] < 0) ? portid : ipv6_l3fwd_out_if[ret[7]]);
1168 if (dst_port[0] >= RTE_MAX_ETHPORTS ||
1169 (enabled_port_mask & 1 << dst_port[0]) == 0)
1170 dst_port[0] = portid;
1171 if (dst_port[1] >= RTE_MAX_ETHPORTS ||
1172 (enabled_port_mask & 1 << dst_port[1]) == 0)
1173 dst_port[1] = portid;
1174 if (dst_port[2] >= RTE_MAX_ETHPORTS ||
1175 (enabled_port_mask & 1 << dst_port[2]) == 0)
1176 dst_port[2] = portid;
1177 if (dst_port[3] >= RTE_MAX_ETHPORTS ||
1178 (enabled_port_mask & 1 << dst_port[3]) == 0)
1179 dst_port[3] = portid;
1180 if (dst_port[4] >= RTE_MAX_ETHPORTS ||
1181 (enabled_port_mask & 1 << dst_port[4]) == 0)
1182 dst_port[4] = portid;
1183 if (dst_port[5] >= RTE_MAX_ETHPORTS ||
1184 (enabled_port_mask & 1 << dst_port[5]) == 0)
1185 dst_port[5] = portid;
1186 if (dst_port[6] >= RTE_MAX_ETHPORTS ||
1187 (enabled_port_mask & 1 << dst_port[6]) == 0)
1188 dst_port[6] = portid;
1189 if (dst_port[7] >= RTE_MAX_ETHPORTS ||
1190 (enabled_port_mask & 1 << dst_port[7]) == 0)
1191 dst_port[7] = portid;
1194 *(uint64_t *)ð_hdr[0]->d_addr = dest_eth_addr[dst_port[0]];
1195 *(uint64_t *)ð_hdr[1]->d_addr = dest_eth_addr[dst_port[1]];
1196 *(uint64_t *)ð_hdr[2]->d_addr = dest_eth_addr[dst_port[2]];
1197 *(uint64_t *)ð_hdr[3]->d_addr = dest_eth_addr[dst_port[3]];
1198 *(uint64_t *)ð_hdr[4]->d_addr = dest_eth_addr[dst_port[4]];
1199 *(uint64_t *)ð_hdr[5]->d_addr = dest_eth_addr[dst_port[5]];
1200 *(uint64_t *)ð_hdr[6]->d_addr = dest_eth_addr[dst_port[6]];
1201 *(uint64_t *)ð_hdr[7]->d_addr = dest_eth_addr[dst_port[7]];
1204 ether_addr_copy(&ports_eth_addr[dst_port[0]], ð_hdr[0]->s_addr);
1205 ether_addr_copy(&ports_eth_addr[dst_port[1]], ð_hdr[1]->s_addr);
1206 ether_addr_copy(&ports_eth_addr[dst_port[2]], ð_hdr[2]->s_addr);
1207 ether_addr_copy(&ports_eth_addr[dst_port[3]], ð_hdr[3]->s_addr);
1208 ether_addr_copy(&ports_eth_addr[dst_port[4]], ð_hdr[4]->s_addr);
1209 ether_addr_copy(&ports_eth_addr[dst_port[5]], ð_hdr[5]->s_addr);
1210 ether_addr_copy(&ports_eth_addr[dst_port[6]], ð_hdr[6]->s_addr);
1211 ether_addr_copy(&ports_eth_addr[dst_port[7]], ð_hdr[7]->s_addr);
1213 send_single_packet(m[0], (uint8_t)dst_port[0]);
1214 send_single_packet(m[1], (uint8_t)dst_port[1]);
1215 send_single_packet(m[2], (uint8_t)dst_port[2]);
1216 send_single_packet(m[3], (uint8_t)dst_port[3]);
1217 send_single_packet(m[4], (uint8_t)dst_port[4]);
1218 send_single_packet(m[5], (uint8_t)dst_port[5]);
1219 send_single_packet(m[6], (uint8_t)dst_port[6]);
1220 send_single_packet(m[7], (uint8_t)dst_port[7]);
1223 #endif /* APP_LOOKUP_METHOD */
1225 static inline __attribute__((always_inline)) void
1226 l3fwd_simple_forward(struct rte_mbuf *m, uint8_t portid)
1228 struct ether_hdr *eth_hdr;
1229 struct ipv4_hdr *ipv4_hdr;
1232 eth_hdr = rte_pktmbuf_mtod(m, struct ether_hdr *);
1234 if (RTE_ETH_IS_IPV4_HDR(m->packet_type)) {
1235 /* Handle IPv4 headers.*/
1236 ipv4_hdr = rte_pktmbuf_mtod_offset(m, struct ipv4_hdr *,
1237 sizeof(struct ether_hdr));
1239 #ifdef DO_RFC_1812_CHECKS
1240 /* Check to make sure the packet is valid (RFC1812) */
1241 if (is_valid_ipv4_pkt(ipv4_hdr, m->pkt_len) < 0) {
1242 rte_pktmbuf_free(m);
1247 dst_port = get_ipv4_dst_port(ipv4_hdr, portid,
1248 RTE_PER_LCORE(lcore_conf)->ipv4_lookup_struct);
1249 if (dst_port >= RTE_MAX_ETHPORTS ||
1250 (enabled_port_mask & 1 << dst_port) == 0)
1253 #ifdef DO_RFC_1812_CHECKS
1254 /* Update time to live and header checksum */
1255 --(ipv4_hdr->time_to_live);
1256 ++(ipv4_hdr->hdr_checksum);
1259 *(uint64_t *)ð_hdr->d_addr = dest_eth_addr[dst_port];
1262 ether_addr_copy(&ports_eth_addr[dst_port], ð_hdr->s_addr);
1264 send_single_packet(m, dst_port);
1265 } else if (RTE_ETH_IS_IPV6_HDR(m->packet_type)) {
1266 /* Handle IPv6 headers.*/
1267 struct ipv6_hdr *ipv6_hdr;
1269 ipv6_hdr = rte_pktmbuf_mtod_offset(m, struct ipv6_hdr *,
1270 sizeof(struct ether_hdr));
1272 dst_port = get_ipv6_dst_port(ipv6_hdr, portid,
1273 RTE_PER_LCORE(lcore_conf)->ipv6_lookup_struct);
1275 if (dst_port >= RTE_MAX_ETHPORTS ||
1276 (enabled_port_mask & 1 << dst_port) == 0)
1280 *(uint64_t *)ð_hdr->d_addr = dest_eth_addr[dst_port];
1283 ether_addr_copy(&ports_eth_addr[dst_port], ð_hdr->s_addr);
1285 send_single_packet(m, dst_port);
1287 /* Free the mbuf that contains non-IPV4/IPV6 packet */
1288 rte_pktmbuf_free(m);
1291 #if ((APP_LOOKUP_METHOD == APP_LOOKUP_LPM) && \
1292 (ENABLE_MULTI_BUFFER_OPTIMIZE == 1))
1293 #ifdef DO_RFC_1812_CHECKS
1295 #define IPV4_MIN_VER_IHL 0x45
1296 #define IPV4_MAX_VER_IHL 0x4f
1297 #define IPV4_MAX_VER_IHL_DIFF (IPV4_MAX_VER_IHL - IPV4_MIN_VER_IHL)
1299 /* Minimum value of IPV4 total length (20B) in network byte order. */
1300 #define IPV4_MIN_LEN_BE (sizeof(struct ipv4_hdr) << 8)
1303 * From http://www.rfc-editor.org/rfc/rfc1812.txt section 5.2.2:
1304 * - The IP version number must be 4.
1305 * - The IP header length field must be large enough to hold the
1306 * minimum length legal IP datagram (20 bytes = 5 words).
1307 * - The IP total length field must be large enough to hold the IP
1308 * datagram header, whose length is specified in the IP header length
1310 * If we encounter invalid IPV4 packet, then set destination port for it
1311 * to BAD_PORT value.
1313 static inline __attribute__((always_inline)) void
1314 rfc1812_process(struct ipv4_hdr *ipv4_hdr, uint16_t *dp, uint32_t ptype)
1318 if (RTE_ETH_IS_IPV4_HDR(ptype)) {
1319 ihl = ipv4_hdr->version_ihl - IPV4_MIN_VER_IHL;
1321 ipv4_hdr->time_to_live--;
1322 ipv4_hdr->hdr_checksum++;
1324 if (ihl > IPV4_MAX_VER_IHL_DIFF ||
1325 ((uint8_t)ipv4_hdr->total_length == 0 &&
1326 ipv4_hdr->total_length < IPV4_MIN_LEN_BE)) {
1333 #define rfc1812_process(mb, dp, ptype) do { } while (0)
1334 #endif /* DO_RFC_1812_CHECKS */
1335 #endif /* APP_LOOKUP_LPM && ENABLE_MULTI_BUFFER_OPTIMIZE */
1338 #if ((APP_LOOKUP_METHOD == APP_LOOKUP_LPM) && \
1339 (ENABLE_MULTI_BUFFER_OPTIMIZE == 1))
1341 static inline __attribute__((always_inline)) uint16_t
1342 get_dst_port(struct rte_mbuf *pkt, uint32_t dst_ipv4, uint8_t portid)
1344 uint32_t next_hop_ipv4;
1345 uint8_t next_hop_ipv6;
1346 struct ipv6_hdr *ipv6_hdr;
1347 struct ether_hdr *eth_hdr;
1349 if (RTE_ETH_IS_IPV4_HDR(pkt->packet_type)) {
1350 return (uint16_t) ((rte_lpm_lookup(
1351 RTE_PER_LCORE(lcore_conf)->ipv4_lookup_struct, dst_ipv4,
1352 &next_hop_ipv4) == 0) ? next_hop_ipv4 : portid);
1354 } else if (RTE_ETH_IS_IPV6_HDR(pkt->packet_type)) {
1356 eth_hdr = rte_pktmbuf_mtod(pkt, struct ether_hdr *);
1357 ipv6_hdr = (struct ipv6_hdr *)(eth_hdr + 1);
1359 return (uint16_t) ((rte_lpm6_lookup(
1360 RTE_PER_LCORE(lcore_conf)->ipv6_lookup_struct,
1361 ipv6_hdr->dst_addr, &next_hop_ipv6) == 0) ? next_hop_ipv6 :
1370 process_packet(struct rte_mbuf *pkt, uint16_t *dst_port, uint8_t portid)
1372 struct ether_hdr *eth_hdr;
1373 struct ipv4_hdr *ipv4_hdr;
1378 eth_hdr = rte_pktmbuf_mtod(pkt, struct ether_hdr *);
1379 ipv4_hdr = (struct ipv4_hdr *)(eth_hdr + 1);
1381 dst_ipv4 = ipv4_hdr->dst_addr;
1382 dst_ipv4 = rte_be_to_cpu_32(dst_ipv4);
1383 dp = get_dst_port(pkt, dst_ipv4, portid);
1385 te = _mm_load_si128((__m128i *)eth_hdr);
1389 rfc1812_process(ipv4_hdr, dst_port, pkt->packet_type);
1391 te = _mm_blend_epi16(te, ve, MASK_ETH);
1392 _mm_store_si128((__m128i *)eth_hdr, te);
1396 * Read packet_type and destination IPV4 addresses from 4 mbufs.
1399 processx4_step1(struct rte_mbuf *pkt[FWDSTEP],
1401 uint32_t *ipv4_flag)
1403 struct ipv4_hdr *ipv4_hdr;
1404 struct ether_hdr *eth_hdr;
1405 uint32_t x0, x1, x2, x3;
1407 eth_hdr = rte_pktmbuf_mtod(pkt[0], struct ether_hdr *);
1408 ipv4_hdr = (struct ipv4_hdr *)(eth_hdr + 1);
1409 x0 = ipv4_hdr->dst_addr;
1410 ipv4_flag[0] = pkt[0]->packet_type & RTE_PTYPE_L3_IPV4;
1412 eth_hdr = rte_pktmbuf_mtod(pkt[1], struct ether_hdr *);
1413 ipv4_hdr = (struct ipv4_hdr *)(eth_hdr + 1);
1414 x1 = ipv4_hdr->dst_addr;
1415 ipv4_flag[0] &= pkt[1]->packet_type;
1417 eth_hdr = rte_pktmbuf_mtod(pkt[2], struct ether_hdr *);
1418 ipv4_hdr = (struct ipv4_hdr *)(eth_hdr + 1);
1419 x2 = ipv4_hdr->dst_addr;
1420 ipv4_flag[0] &= pkt[2]->packet_type;
1422 eth_hdr = rte_pktmbuf_mtod(pkt[3], struct ether_hdr *);
1423 ipv4_hdr = (struct ipv4_hdr *)(eth_hdr + 1);
1424 x3 = ipv4_hdr->dst_addr;
1425 ipv4_flag[0] &= pkt[3]->packet_type;
1427 dip[0] = _mm_set_epi32(x3, x2, x1, x0);
1431 * Lookup into LPM for destination port.
1432 * If lookup fails, use incoming port (portid) as destination port.
1435 processx4_step2(__m128i dip,
1438 struct rte_mbuf *pkt[FWDSTEP],
1439 uint16_t dprt[FWDSTEP])
1442 const __m128i bswap_mask = _mm_set_epi8(12, 13, 14, 15, 8, 9, 10, 11,
1443 4, 5, 6, 7, 0, 1, 2, 3);
1445 /* Byte swap 4 IPV4 addresses. */
1446 dip = _mm_shuffle_epi8(dip, bswap_mask);
1448 /* if all 4 packets are IPV4. */
1449 if (likely(ipv4_flag)) {
1450 rte_lpm_lookupx4(RTE_PER_LCORE(lcore_conf)->ipv4_lookup_struct, dip,
1453 /* get rid of unused upper 16 bit for each dport. */
1454 dst.x = _mm_packs_epi32(dst.x, dst.x);
1455 *(uint64_t *)dprt = dst.u64[0];
1458 dprt[0] = get_dst_port(pkt[0], dst.u32[0], portid);
1459 dprt[1] = get_dst_port(pkt[1], dst.u32[1], portid);
1460 dprt[2] = get_dst_port(pkt[2], dst.u32[2], portid);
1461 dprt[3] = get_dst_port(pkt[3], dst.u32[3], portid);
1466 * Update source and destination MAC addresses in the ethernet header.
1467 * Perform RFC1812 checks and updates for IPV4 packets.
1470 processx4_step3(struct rte_mbuf *pkt[FWDSTEP], uint16_t dst_port[FWDSTEP])
1472 __m128i te[FWDSTEP];
1473 __m128i ve[FWDSTEP];
1474 __m128i *p[FWDSTEP];
1476 p[0] = rte_pktmbuf_mtod(pkt[0], __m128i *);
1477 p[1] = rte_pktmbuf_mtod(pkt[1], __m128i *);
1478 p[2] = rte_pktmbuf_mtod(pkt[2], __m128i *);
1479 p[3] = rte_pktmbuf_mtod(pkt[3], __m128i *);
1481 ve[0] = val_eth[dst_port[0]];
1482 te[0] = _mm_load_si128(p[0]);
1484 ve[1] = val_eth[dst_port[1]];
1485 te[1] = _mm_load_si128(p[1]);
1487 ve[2] = val_eth[dst_port[2]];
1488 te[2] = _mm_load_si128(p[2]);
1490 ve[3] = val_eth[dst_port[3]];
1491 te[3] = _mm_load_si128(p[3]);
1493 /* Update first 12 bytes, keep rest bytes intact. */
1494 te[0] = _mm_blend_epi16(te[0], ve[0], MASK_ETH);
1495 te[1] = _mm_blend_epi16(te[1], ve[1], MASK_ETH);
1496 te[2] = _mm_blend_epi16(te[2], ve[2], MASK_ETH);
1497 te[3] = _mm_blend_epi16(te[3], ve[3], MASK_ETH);
1499 _mm_store_si128(p[0], te[0]);
1500 _mm_store_si128(p[1], te[1]);
1501 _mm_store_si128(p[2], te[2]);
1502 _mm_store_si128(p[3], te[3]);
1504 rfc1812_process((struct ipv4_hdr *)((struct ether_hdr *)p[0] + 1),
1505 &dst_port[0], pkt[0]->packet_type);
1506 rfc1812_process((struct ipv4_hdr *)((struct ether_hdr *)p[1] + 1),
1507 &dst_port[1], pkt[1]->packet_type);
1508 rfc1812_process((struct ipv4_hdr *)((struct ether_hdr *)p[2] + 1),
1509 &dst_port[2], pkt[2]->packet_type);
1510 rfc1812_process((struct ipv4_hdr *)((struct ether_hdr *)p[3] + 1),
1511 &dst_port[3], pkt[3]->packet_type);
1515 * We group consecutive packets with the same destionation port into one burst.
1516 * To avoid extra latency this is done together with some other packet
1517 * processing, but after we made a final decision about packet's destination.
1518 * To do this we maintain:
1519 * pnum - array of number of consecutive packets with the same dest port for
1520 * each packet in the input burst.
1521 * lp - pointer to the last updated element in the pnum.
1522 * dlp - dest port value lp corresponds to.
1525 #define GRPSZ (1 << FWDSTEP)
1526 #define GRPMSK (GRPSZ - 1)
1528 #define GROUP_PORT_STEP(dlp, dcp, lp, pn, idx) do { \
1529 if (likely((dlp) == (dcp)[(idx)])) { \
1532 (dlp) = (dcp)[idx]; \
1533 (lp) = (pn) + (idx); \
1539 * Group consecutive packets with the same destination port in bursts of 4.
1540 * Suppose we have array of destionation ports:
1541 * dst_port[] = {a, b, c, d,, e, ... }
1542 * dp1 should contain: <a, b, c, d>, dp2: <b, c, d, e>.
1543 * We doing 4 comparisions at once and the result is 4 bit mask.
1544 * This mask is used as an index into prebuild array of pnum values.
1546 static inline uint16_t *
1547 port_groupx4(uint16_t pn[FWDSTEP + 1], uint16_t *lp, __m128i dp1, __m128i dp2)
1549 static const struct {
1550 uint64_t pnum; /* prebuild 4 values for pnum[]. */
1551 int32_t idx; /* index for new last updated elemnet. */
1552 uint16_t lpv; /* add value to the last updated element. */
1555 /* 0: a != b, b != c, c != d, d != e */
1556 .pnum = UINT64_C(0x0001000100010001),
1561 /* 1: a == b, b != c, c != d, d != e */
1562 .pnum = UINT64_C(0x0001000100010002),
1567 /* 2: a != b, b == c, c != d, d != e */
1568 .pnum = UINT64_C(0x0001000100020001),
1573 /* 3: a == b, b == c, c != d, d != e */
1574 .pnum = UINT64_C(0x0001000100020003),
1579 /* 4: a != b, b != c, c == d, d != e */
1580 .pnum = UINT64_C(0x0001000200010001),
1585 /* 5: a == b, b != c, c == d, d != e */
1586 .pnum = UINT64_C(0x0001000200010002),
1591 /* 6: a != b, b == c, c == d, d != e */
1592 .pnum = UINT64_C(0x0001000200030001),
1597 /* 7: a == b, b == c, c == d, d != e */
1598 .pnum = UINT64_C(0x0001000200030004),
1603 /* 8: a != b, b != c, c != d, d == e */
1604 .pnum = UINT64_C(0x0002000100010001),
1609 /* 9: a == b, b != c, c != d, d == e */
1610 .pnum = UINT64_C(0x0002000100010002),
1615 /* 0xa: a != b, b == c, c != d, d == e */
1616 .pnum = UINT64_C(0x0002000100020001),
1621 /* 0xb: a == b, b == c, c != d, d == e */
1622 .pnum = UINT64_C(0x0002000100020003),
1627 /* 0xc: a != b, b != c, c == d, d == e */
1628 .pnum = UINT64_C(0x0002000300010001),
1633 /* 0xd: a == b, b != c, c == d, d == e */
1634 .pnum = UINT64_C(0x0002000300010002),
1639 /* 0xe: a != b, b == c, c == d, d == e */
1640 .pnum = UINT64_C(0x0002000300040001),
1645 /* 0xf: a == b, b == c, c == d, d == e */
1646 .pnum = UINT64_C(0x0002000300040005),
1653 uint16_t u16[FWDSTEP + 1];
1655 } *pnum = (void *)pn;
1659 dp1 = _mm_cmpeq_epi16(dp1, dp2);
1660 dp1 = _mm_unpacklo_epi16(dp1, dp1);
1661 v = _mm_movemask_ps((__m128)dp1);
1663 /* update last port counter. */
1664 lp[0] += gptbl[v].lpv;
1666 /* if dest port value has changed. */
1668 pnum->u64 = gptbl[v].pnum;
1669 pnum->u16[FWDSTEP] = 1;
1670 lp = pnum->u16 + gptbl[v].idx;
1676 #endif /* APP_LOOKUP_METHOD */
1679 process_burst(struct rte_mbuf *pkts_burst[MAX_PKT_BURST], int nb_rx,
1684 #if ((APP_LOOKUP_METHOD == APP_LOOKUP_LPM) && \
1685 (ENABLE_MULTI_BUFFER_OPTIMIZE == 1))
1689 uint16_t dst_port[MAX_PKT_BURST];
1690 __m128i dip[MAX_PKT_BURST / FWDSTEP];
1691 uint32_t ipv4_flag[MAX_PKT_BURST / FWDSTEP];
1692 uint16_t pnum[MAX_PKT_BURST + 1];
1696 #if (ENABLE_MULTI_BUFFER_OPTIMIZE == 1)
1697 #if (APP_LOOKUP_METHOD == APP_LOOKUP_EXACT_MATCH)
1700 * Send nb_rx - nb_rx%8 packets
1703 int32_t n = RTE_ALIGN_FLOOR(nb_rx, 8);
1705 for (j = 0; j < n; j += 8) {
1707 pkts_burst[j]->packet_type &
1708 pkts_burst[j+1]->packet_type &
1709 pkts_burst[j+2]->packet_type &
1710 pkts_burst[j+3]->packet_type &
1711 pkts_burst[j+4]->packet_type &
1712 pkts_burst[j+5]->packet_type &
1713 pkts_burst[j+6]->packet_type &
1714 pkts_burst[j+7]->packet_type;
1715 if (pkt_type & RTE_PTYPE_L3_IPV4) {
1716 simple_ipv4_fwd_8pkts(&pkts_burst[j], portid);
1717 } else if (pkt_type &
1718 RTE_PTYPE_L3_IPV6) {
1719 simple_ipv6_fwd_8pkts(&pkts_burst[j], portid);
1721 l3fwd_simple_forward(pkts_burst[j], portid);
1722 l3fwd_simple_forward(pkts_burst[j+1], portid);
1723 l3fwd_simple_forward(pkts_burst[j+2], portid);
1724 l3fwd_simple_forward(pkts_burst[j+3], portid);
1725 l3fwd_simple_forward(pkts_burst[j+4], portid);
1726 l3fwd_simple_forward(pkts_burst[j+5], portid);
1727 l3fwd_simple_forward(pkts_burst[j+6], portid);
1728 l3fwd_simple_forward(pkts_burst[j+7], portid);
1731 for (; j < nb_rx ; j++)
1732 l3fwd_simple_forward(pkts_burst[j], portid);
1734 #elif (APP_LOOKUP_METHOD == APP_LOOKUP_LPM)
1736 k = RTE_ALIGN_FLOOR(nb_rx, FWDSTEP);
1737 for (j = 0; j != k; j += FWDSTEP)
1738 processx4_step1(&pkts_burst[j], &dip[j / FWDSTEP],
1739 &ipv4_flag[j / FWDSTEP]);
1741 k = RTE_ALIGN_FLOOR(nb_rx, FWDSTEP);
1742 for (j = 0; j != k; j += FWDSTEP)
1743 processx4_step2(dip[j / FWDSTEP], ipv4_flag[j / FWDSTEP],
1744 portid, &pkts_burst[j], &dst_port[j]);
1747 * Finish packet processing and group consecutive
1748 * packets with the same destination port.
1750 k = RTE_ALIGN_FLOOR(nb_rx, FWDSTEP);
1757 processx4_step3(pkts_burst, dst_port);
1759 /* dp1: <d[0], d[1], d[2], d[3], ... > */
1760 dp1 = _mm_loadu_si128((__m128i *)dst_port);
1762 for (j = FWDSTEP; j != k; j += FWDSTEP) {
1763 processx4_step3(&pkts_burst[j], &dst_port[j]);
1767 * <d[j-3], d[j-2], d[j-1], d[j], ... >
1769 dp2 = _mm_loadu_si128(
1770 (__m128i *)&dst_port[j - FWDSTEP + 1]);
1771 lp = port_groupx4(&pnum[j - FWDSTEP], lp, dp1, dp2);
1775 * <d[j], d[j+1], d[j+2], d[j+3], ... >
1777 dp1 = _mm_srli_si128(dp2, (FWDSTEP - 1) *
1778 sizeof(dst_port[0]));
1782 * dp2: <d[j-3], d[j-2], d[j-1], d[j-1], ... >
1784 dp2 = _mm_shufflelo_epi16(dp1, 0xf9);
1785 lp = port_groupx4(&pnum[j - FWDSTEP], lp, dp1, dp2);
1788 * remove values added by the last repeated
1792 dlp = dst_port[j - 1];
1794 /* set dlp and lp to the never used values. */
1796 lp = pnum + MAX_PKT_BURST;
1799 /* Process up to last 3 packets one by one. */
1800 switch (nb_rx % FWDSTEP) {
1802 process_packet(pkts_burst[j], dst_port + j, portid);
1803 GROUP_PORT_STEP(dlp, dst_port, lp, pnum, j);
1806 process_packet(pkts_burst[j], dst_port + j, portid);
1807 GROUP_PORT_STEP(dlp, dst_port, lp, pnum, j);
1810 process_packet(pkts_burst[j], dst_port + j, portid);
1811 GROUP_PORT_STEP(dlp, dst_port, lp, pnum, j);
1816 * Send packets out, through destination port.
1817 * Consecuteve pacekts with the same destination port
1818 * are already grouped together.
1819 * If destination port for the packet equals BAD_PORT,
1820 * then free the packet without sending it out.
1822 for (j = 0; j < nb_rx; j += k) {
1830 if (likely(pn != BAD_PORT))
1831 send_packetsx4(pn, pkts_burst + j, k);
1833 for (m = j; m != j + k; m++)
1834 rte_pktmbuf_free(pkts_burst[m]);
1838 #endif /* APP_LOOKUP_METHOD */
1839 #else /* ENABLE_MULTI_BUFFER_OPTIMIZE == 0 */
1841 /* Prefetch first packets */
1842 for (j = 0; j < PREFETCH_OFFSET && j < nb_rx; j++)
1843 rte_prefetch0(rte_pktmbuf_mtod(pkts_burst[j], void *));
1845 /* Prefetch and forward already prefetched packets */
1846 for (j = 0; j < (nb_rx - PREFETCH_OFFSET); j++) {
1847 rte_prefetch0(rte_pktmbuf_mtod(pkts_burst[
1848 j + PREFETCH_OFFSET], void *));
1849 l3fwd_simple_forward(pkts_burst[j], portid);
1852 /* Forward remaining prefetched packets */
1853 for (; j < nb_rx; j++)
1854 l3fwd_simple_forward(pkts_burst[j], portid);
1856 #endif /* ENABLE_MULTI_BUFFER_OPTIMIZE */
1860 #if (APP_CPU_LOAD > 0)
1863 * CPU-load stats collector
1866 cpu_load_collector(__rte_unused void *arg) {
1869 uint64_t prev_tsc, diff_tsc, cur_tsc;
1870 uint64_t total[MAX_CPU] = { 0 };
1871 unsigned min_cpu = MAX_CPU;
1872 unsigned max_cpu = 0;
1877 unsigned int n_thread_per_cpu[MAX_CPU] = { 0 };
1878 struct thread_conf *thread_per_cpu[MAX_CPU][MAX_THREAD];
1880 struct thread_conf *thread_conf;
1882 const uint64_t interval_tsc = (rte_get_tsc_hz() + US_PER_S - 1) /
1883 US_PER_S * CPU_LOAD_TIMEOUT_US;
1887 * Wait for all threads
1890 printf("Waiting for %d rx threads and %d tx threads\n", n_rx_thread,
1893 while (rte_atomic16_read(&rx_counter) < n_rx_thread)
1896 while (rte_atomic16_read(&tx_counter) < n_tx_thread)
1899 for (i = 0; i < n_rx_thread; i++) {
1901 thread_conf = &rx_thread[i].conf;
1902 cpu_id = thread_conf->cpu_id;
1903 thread_per_cpu[cpu_id][n_thread_per_cpu[cpu_id]++] = thread_conf;
1905 if (cpu_id > max_cpu)
1907 if (cpu_id < min_cpu)
1910 for (i = 0; i < n_tx_thread; i++) {
1912 thread_conf = &tx_thread[i].conf;
1913 cpu_id = thread_conf->cpu_id;
1914 thread_per_cpu[cpu_id][n_thread_per_cpu[cpu_id]++] = thread_conf;
1916 if (thread_conf->cpu_id > max_cpu)
1917 max_cpu = thread_conf->cpu_id;
1918 if (thread_conf->cpu_id < min_cpu)
1919 min_cpu = thread_conf->cpu_id;
1925 for (i = min_cpu; i <= max_cpu; i++) {
1926 for (j = 0; j < MAX_CPU_COUNTER; j++) {
1927 for (k = 0; k < n_thread_per_cpu[i]; k++)
1928 if (thread_per_cpu[i][k]->busy[j]) {
1933 cpu_load.hits[j][i]++;
1945 cur_tsc = rte_rdtsc();
1947 diff_tsc = cur_tsc - prev_tsc;
1948 if (unlikely(diff_tsc > interval_tsc)) {
1952 printf("Cpu usage for %d rx threads and %d tx threads:\n\n",
1953 n_rx_thread, n_tx_thread);
1955 printf("cpu# proc%% poll%% overhead%%\n\n");
1957 for (i = min_cpu; i <= max_cpu; i++) {
1959 printf("CPU %d:", i);
1960 for (j = 0; j < MAX_CPU_COUNTER; j++) {
1961 printf("%7" PRIu64 "",
1962 cpu_load.hits[j][i] * 100 / cpu_load.counter);
1963 hits += cpu_load.hits[j][i];
1964 cpu_load.hits[j][i] = 0;
1966 printf("%7" PRIu64 "\n",
1967 100 - total[i] * 100 / cpu_load.counter);
1970 cpu_load.counter = 0;
1977 #endif /* APP_CPU_LOAD */
1980 * Null processing lthread loop
1982 * This loop is used to start empty scheduler on lcore.
1985 lthread_null(__rte_unused void *args)
1987 int lcore_id = rte_lcore_id();
1989 RTE_LOG(INFO, L3FWD, "Starting scheduler on lcore %d.\n", lcore_id);
1994 /* main processing loop */
1996 lthread_tx_per_ring(void *dummy)
2000 struct rte_ring *ring;
2001 struct thread_tx_conf *tx_conf;
2002 struct rte_mbuf *pkts_burst[MAX_PKT_BURST];
2003 struct lthread_cond *ready;
2005 tx_conf = (struct thread_tx_conf *)dummy;
2006 ring = tx_conf->ring;
2007 ready = *tx_conf->ready;
2009 lthread_set_data((void *)tx_conf);
2012 * Move this lthread to lcore
2014 lthread_set_affinity(tx_conf->conf.lcore_id);
2016 RTE_LOG(INFO, L3FWD, "entering main tx loop on lcore %u\n", rte_lcore_id());
2019 rte_atomic16_inc(&tx_counter);
2023 * Read packet from ring
2025 SET_CPU_BUSY(tx_conf, CPU_POLL);
2026 nb_rx = rte_ring_sc_dequeue_burst(ring, (void **)pkts_burst,
2028 SET_CPU_IDLE(tx_conf, CPU_POLL);
2031 SET_CPU_BUSY(tx_conf, CPU_PROCESS);
2032 portid = pkts_burst[0]->port;
2033 process_burst(pkts_burst, nb_rx, portid);
2034 SET_CPU_IDLE(tx_conf, CPU_PROCESS);
2037 lthread_cond_wait(ready, 0);
2044 * Main tx-lthreads spawner lthread.
2046 * This lthread is used to spawn one new lthread per ring from producers.
2050 lthread_tx(void *args)
2056 struct thread_tx_conf *tx_conf;
2058 tx_conf = (struct thread_tx_conf *)args;
2059 lthread_set_data((void *)tx_conf);
2062 * Move this lthread to the selected lcore
2064 lthread_set_affinity(tx_conf->conf.lcore_id);
2067 * Spawn tx readers (one per input ring)
2069 lthread_create(<, tx_conf->conf.lcore_id, lthread_tx_per_ring,
2072 lcore_id = rte_lcore_id();
2074 RTE_LOG(INFO, L3FWD, "Entering Tx main loop on lcore %u\n", lcore_id);
2076 tx_conf->conf.cpu_id = sched_getcpu();
2079 lthread_sleep(BURST_TX_DRAIN_US * 1000);
2082 * TX burst queue drain
2084 for (portid = 0; portid < RTE_MAX_ETHPORTS; portid++) {
2085 if (tx_conf->tx_mbufs[portid].len == 0)
2087 SET_CPU_BUSY(tx_conf, CPU_PROCESS);
2088 send_burst(tx_conf, tx_conf->tx_mbufs[portid].len, portid);
2089 SET_CPU_IDLE(tx_conf, CPU_PROCESS);
2090 tx_conf->tx_mbufs[portid].len = 0;
2098 lthread_rx(void *dummy)
2103 uint8_t portid, queueid;
2105 int len[RTE_MAX_LCORE] = { 0 };
2106 int old_len, new_len;
2107 struct rte_mbuf *pkts_burst[MAX_PKT_BURST];
2108 struct thread_rx_conf *rx_conf;
2110 rx_conf = (struct thread_rx_conf *)dummy;
2111 lthread_set_data((void *)rx_conf);
2114 * Move this lthread to lcore
2116 lthread_set_affinity(rx_conf->conf.lcore_id);
2118 if (rx_conf->n_rx_queue == 0) {
2119 RTE_LOG(INFO, L3FWD, "lcore %u has nothing to do\n", rte_lcore_id());
2123 RTE_LOG(INFO, L3FWD, "Entering main Rx loop on lcore %u\n", rte_lcore_id());
2125 for (i = 0; i < rx_conf->n_rx_queue; i++) {
2127 portid = rx_conf->rx_queue_list[i].port_id;
2128 queueid = rx_conf->rx_queue_list[i].queue_id;
2129 RTE_LOG(INFO, L3FWD, " -- lcoreid=%u portid=%hhu rxqueueid=%hhu\n",
2130 rte_lcore_id(), portid, queueid);
2134 * Init all condition variables (one per rx thread)
2136 for (i = 0; i < rx_conf->n_rx_queue; i++)
2137 lthread_cond_init(NULL, &rx_conf->ready[i], NULL);
2141 rx_conf->conf.cpu_id = sched_getcpu();
2142 rte_atomic16_inc(&rx_counter);
2146 * Read packet from RX queues
2148 for (i = 0; i < rx_conf->n_rx_queue; ++i) {
2149 portid = rx_conf->rx_queue_list[i].port_id;
2150 queueid = rx_conf->rx_queue_list[i].queue_id;
2152 SET_CPU_BUSY(rx_conf, CPU_POLL);
2153 nb_rx = rte_eth_rx_burst(portid, queueid, pkts_burst,
2155 SET_CPU_IDLE(rx_conf, CPU_POLL);
2158 worker_id = (worker_id + 1) % rx_conf->n_ring;
2159 old_len = len[worker_id];
2161 SET_CPU_BUSY(rx_conf, CPU_PROCESS);
2162 ret = rte_ring_sp_enqueue_burst(
2163 rx_conf->ring[worker_id],
2164 (void **) pkts_burst,
2167 new_len = old_len + ret;
2169 if (new_len >= BURST_SIZE) {
2170 lthread_cond_signal(rx_conf->ready[worker_id]);
2174 len[worker_id] = new_len;
2176 if (unlikely(ret < nb_rx)) {
2179 for (k = ret; k < nb_rx; k++) {
2180 struct rte_mbuf *m = pkts_burst[k];
2182 rte_pktmbuf_free(m);
2185 SET_CPU_IDLE(rx_conf, CPU_PROCESS);
2195 * Start scheduler with initial lthread on lcore
2197 * This lthread loop spawns all rx and tx lthreads on master lcore
2201 lthread_spawner(__rte_unused void *arg)
2203 struct lthread *lt[MAX_THREAD];
2207 printf("Entering lthread_spawner\n");
2210 * Create producers (rx threads) on default lcore
2212 for (i = 0; i < n_rx_thread; i++) {
2213 rx_thread[i].conf.thread_id = i;
2214 lthread_create(<[n_thread], -1, lthread_rx,
2215 (void *)&rx_thread[i]);
2220 * Wait for all producers. Until some producers can be started on the same
2221 * scheduler as this lthread, yielding is required to let them to run and
2222 * prevent deadlock here.
2224 while (rte_atomic16_read(&rx_counter) < n_rx_thread)
2225 lthread_sleep(100000);
2228 * Create consumers (tx threads) on default lcore_id
2230 for (i = 0; i < n_tx_thread; i++) {
2231 tx_thread[i].conf.thread_id = i;
2232 lthread_create(<[n_thread], -1, lthread_tx,
2233 (void *)&tx_thread[i]);
2238 * Wait for all threads finished
2240 for (i = 0; i < n_thread; i++)
2241 lthread_join(lt[i], NULL);
2247 * Start master scheduler with initial lthread spawning rx and tx lthreads
2248 * (main_lthread_master).
2251 lthread_master_spawner(__rte_unused void *arg) {
2253 int lcore_id = rte_lcore_id();
2255 RTE_PER_LCORE(lcore_conf) = &lcore_conf[lcore_id];
2256 lthread_create(<, -1, lthread_spawner, NULL);
2263 * Start scheduler on lcore.
2266 sched_spawner(__rte_unused void *arg) {
2268 int lcore_id = rte_lcore_id();
2271 if (lcore_id == cpu_load_lcore_id) {
2272 cpu_load_collector(arg);
2275 #endif /* APP_CPU_LOAD */
2277 RTE_PER_LCORE(lcore_conf) = &lcore_conf[lcore_id];
2278 lthread_create(<, -1, lthread_null, NULL);
2284 /* main processing loop */
2286 pthread_tx(void *dummy)
2288 struct rte_mbuf *pkts_burst[MAX_PKT_BURST];
2289 uint64_t prev_tsc, diff_tsc, cur_tsc;
2292 struct thread_tx_conf *tx_conf;
2294 const uint64_t drain_tsc = (rte_get_tsc_hz() + US_PER_S - 1) /
2295 US_PER_S * BURST_TX_DRAIN_US;
2299 tx_conf = (struct thread_tx_conf *)dummy;
2301 RTE_LOG(INFO, L3FWD, "Entering main Tx loop on lcore %u\n", rte_lcore_id());
2303 tx_conf->conf.cpu_id = sched_getcpu();
2304 rte_atomic16_inc(&tx_counter);
2307 cur_tsc = rte_rdtsc();
2310 * TX burst queue drain
2312 diff_tsc = cur_tsc - prev_tsc;
2313 if (unlikely(diff_tsc > drain_tsc)) {
2316 * This could be optimized (use queueid instead of
2317 * portid), but it is not called so often
2319 SET_CPU_BUSY(tx_conf, CPU_PROCESS);
2320 for (portid = 0; portid < RTE_MAX_ETHPORTS; portid++) {
2321 if (tx_conf->tx_mbufs[portid].len == 0)
2323 send_burst(tx_conf, tx_conf->tx_mbufs[portid].len, portid);
2324 tx_conf->tx_mbufs[portid].len = 0;
2326 SET_CPU_IDLE(tx_conf, CPU_PROCESS);
2332 * Read packet from ring
2334 SET_CPU_BUSY(tx_conf, CPU_POLL);
2335 nb_rx = rte_ring_sc_dequeue_burst(tx_conf->ring,
2336 (void **)pkts_burst, MAX_PKT_BURST);
2337 SET_CPU_IDLE(tx_conf, CPU_POLL);
2339 if (unlikely(nb_rx == 0)) {
2344 SET_CPU_BUSY(tx_conf, CPU_PROCESS);
2345 portid = pkts_burst[0]->port;
2346 process_burst(pkts_burst, nb_rx, portid);
2347 SET_CPU_IDLE(tx_conf, CPU_PROCESS);
2353 pthread_rx(void *dummy)
2360 uint8_t portid, queueid;
2361 struct rte_mbuf *pkts_burst[MAX_PKT_BURST];
2363 struct thread_rx_conf *rx_conf;
2365 lcore_id = rte_lcore_id();
2366 rx_conf = (struct thread_rx_conf *)dummy;
2368 if (rx_conf->n_rx_queue == 0) {
2369 RTE_LOG(INFO, L3FWD, "lcore %u has nothing to do\n", lcore_id);
2373 RTE_LOG(INFO, L3FWD, "entering main rx loop on lcore %u\n", lcore_id);
2375 for (i = 0; i < rx_conf->n_rx_queue; i++) {
2377 portid = rx_conf->rx_queue_list[i].port_id;
2378 queueid = rx_conf->rx_queue_list[i].queue_id;
2379 RTE_LOG(INFO, L3FWD, " -- lcoreid=%u portid=%hhu rxqueueid=%hhu\n",
2380 lcore_id, portid, queueid);
2384 rx_conf->conf.cpu_id = sched_getcpu();
2385 rte_atomic16_inc(&rx_counter);
2389 * Read packet from RX queues
2391 for (i = 0; i < rx_conf->n_rx_queue; ++i) {
2392 portid = rx_conf->rx_queue_list[i].port_id;
2393 queueid = rx_conf->rx_queue_list[i].queue_id;
2395 SET_CPU_BUSY(rx_conf, CPU_POLL);
2396 nb_rx = rte_eth_rx_burst(portid, queueid, pkts_burst,
2398 SET_CPU_IDLE(rx_conf, CPU_POLL);
2405 SET_CPU_BUSY(rx_conf, CPU_PROCESS);
2406 worker_id = (worker_id + 1) % rx_conf->n_ring;
2407 n = rte_ring_sp_enqueue_burst(rx_conf->ring[worker_id],
2408 (void **)pkts_burst, nb_rx);
2410 if (unlikely(n != nb_rx)) {
2413 for (k = n; k < nb_rx; k++) {
2414 struct rte_mbuf *m = pkts_burst[k];
2416 rte_pktmbuf_free(m);
2420 SET_CPU_IDLE(rx_conf, CPU_PROCESS);
2430 pthread_run(__rte_unused void *arg) {
2431 int lcore_id = rte_lcore_id();
2434 for (i = 0; i < n_rx_thread; i++)
2435 if (rx_thread[i].conf.lcore_id == lcore_id) {
2436 printf("Start rx thread on %d...\n", lcore_id);
2437 RTE_PER_LCORE(lcore_conf) = &lcore_conf[lcore_id];
2438 RTE_PER_LCORE(lcore_conf)->data = (void *)&rx_thread[i];
2439 pthread_rx((void *)&rx_thread[i]);
2443 for (i = 0; i < n_tx_thread; i++)
2444 if (tx_thread[i].conf.lcore_id == lcore_id) {
2445 printf("Start tx thread on %d...\n", lcore_id);
2446 RTE_PER_LCORE(lcore_conf) = &lcore_conf[lcore_id];
2447 RTE_PER_LCORE(lcore_conf)->data = (void *)&tx_thread[i];
2448 pthread_tx((void *)&tx_thread[i]);
2453 if (lcore_id == cpu_load_lcore_id)
2454 cpu_load_collector(arg);
2455 #endif /* APP_CPU_LOAD */
2461 check_lcore_params(void)
2463 uint8_t queue, lcore;
2467 for (i = 0; i < nb_rx_thread_params; ++i) {
2468 queue = rx_thread_params[i].queue_id;
2469 if (queue >= MAX_RX_QUEUE_PER_PORT) {
2470 printf("invalid queue number: %hhu\n", queue);
2473 lcore = rx_thread_params[i].lcore_id;
2474 if (!rte_lcore_is_enabled(lcore)) {
2475 printf("error: lcore %hhu is not enabled in lcore mask\n", lcore);
2478 socketid = rte_lcore_to_socket_id(lcore);
2479 if ((socketid != 0) && (numa_on == 0))
2480 printf("warning: lcore %hhu is on socket %d with numa off\n",
2487 check_port_config(const unsigned nb_ports)
2492 for (i = 0; i < nb_rx_thread_params; ++i) {
2493 portid = rx_thread_params[i].port_id;
2494 if ((enabled_port_mask & (1 << portid)) == 0) {
2495 printf("port %u is not enabled in port mask\n", portid);
2498 if (portid >= nb_ports) {
2499 printf("port %u is not present on the board\n", portid);
2507 get_port_n_rx_queues(const uint8_t port)
2512 for (i = 0; i < nb_rx_thread_params; ++i)
2513 if (rx_thread_params[i].port_id == port &&
2514 rx_thread_params[i].queue_id > queue)
2515 queue = rx_thread_params[i].queue_id;
2517 return (uint8_t)(++queue);
2524 struct thread_rx_conf *rx_conf;
2525 struct thread_tx_conf *tx_conf;
2526 unsigned rx_thread_id, tx_thread_id;
2528 struct rte_ring *ring = NULL;
2530 for (tx_thread_id = 0; tx_thread_id < n_tx_thread; tx_thread_id++) {
2532 tx_conf = &tx_thread[tx_thread_id];
2534 printf("Connecting tx-thread %d with rx-thread %d\n", tx_thread_id,
2535 tx_conf->conf.thread_id);
2537 rx_thread_id = tx_conf->conf.thread_id;
2538 if (rx_thread_id > n_tx_thread) {
2539 printf("connection from tx-thread %u to rx-thread %u fails "
2540 "(rx-thread not defined)\n", tx_thread_id, rx_thread_id);
2544 rx_conf = &rx_thread[rx_thread_id];
2545 socket_io = rte_lcore_to_socket_id(rx_conf->conf.lcore_id);
2547 snprintf(name, sizeof(name), "app_ring_s%u_rx%u_tx%u",
2548 socket_io, rx_thread_id, tx_thread_id);
2550 ring = rte_ring_create(name, 1024 * 4, socket_io,
2551 RING_F_SP_ENQ | RING_F_SC_DEQ);
2554 rte_panic("Cannot create ring to connect rx-thread %u "
2555 "with tx-thread %u\n", rx_thread_id, tx_thread_id);
2558 rx_conf->ring[rx_conf->n_ring] = ring;
2560 tx_conf->ring = ring;
2561 tx_conf->ready = &rx_conf->ready[rx_conf->n_ring];
2569 init_rx_queues(void)
2571 uint16_t i, nb_rx_queue;
2576 for (i = 0; i < nb_rx_thread_params; ++i) {
2577 thread = rx_thread_params[i].thread_id;
2578 nb_rx_queue = rx_thread[thread].n_rx_queue;
2580 if (nb_rx_queue >= MAX_RX_QUEUE_PER_LCORE) {
2581 printf("error: too many queues (%u) for thread: %u\n",
2582 (unsigned)nb_rx_queue + 1, (unsigned)thread);
2586 rx_thread[thread].conf.thread_id = thread;
2587 rx_thread[thread].conf.lcore_id = rx_thread_params[i].lcore_id;
2588 rx_thread[thread].rx_queue_list[nb_rx_queue].port_id =
2589 rx_thread_params[i].port_id;
2590 rx_thread[thread].rx_queue_list[nb_rx_queue].queue_id =
2591 rx_thread_params[i].queue_id;
2592 rx_thread[thread].n_rx_queue++;
2594 if (thread >= n_rx_thread)
2595 n_rx_thread = thread + 1;
2602 init_tx_threads(void)
2607 for (i = 0; i < nb_tx_thread_params; ++i) {
2608 tx_thread[n_tx_thread].conf.thread_id = tx_thread_params[i].thread_id;
2609 tx_thread[n_tx_thread].conf.lcore_id = tx_thread_params[i].lcore_id;
2617 print_usage(const char *prgname)
2619 printf("%s [EAL options] -- -p PORTMASK -P"
2620 " [--rx (port,queue,lcore,thread)[,(port,queue,lcore,thread]]"
2621 " [--tx (lcore,thread)[,(lcore,thread]]"
2622 " [--enable-jumbo [--max-pkt-len PKTLEN]]\n"
2623 " -p PORTMASK: hexadecimal bitmask of ports to configure\n"
2624 " -P : enable promiscuous mode\n"
2625 " --rx (port,queue,lcore,thread): rx queues configuration\n"
2626 " --tx (lcore,thread): tx threads configuration\n"
2627 " --stat-lcore LCORE: use lcore for stat collector\n"
2628 " --eth-dest=X,MM:MM:MM:MM:MM:MM: optional, ethernet destination for port X\n"
2629 " --no-numa: optional, disable numa awareness\n"
2630 " --ipv6: optional, specify it if running ipv6 packets\n"
2631 " --enable-jumbo: enable jumbo frame"
2632 " which max packet len is PKTLEN in decimal (64-9600)\n"
2633 " --hash-entry-num: specify the hash entry number in hexadecimal to be setup\n"
2634 " --no-lthreads: turn off lthread model\n",
2638 static int parse_max_pkt_len(const char *pktlen)
2643 /* parse decimal string */
2644 len = strtoul(pktlen, &end, 10);
2645 if ((pktlen[0] == '\0') || (end == NULL) || (*end != '\0'))
2655 parse_portmask(const char *portmask)
2660 /* parse hexadecimal string */
2661 pm = strtoul(portmask, &end, 16);
2662 if ((portmask[0] == '\0') || (end == NULL) || (*end != '\0'))
2671 #if (APP_LOOKUP_METHOD == APP_LOOKUP_EXACT_MATCH)
2673 parse_hash_entry_number(const char *hash_entry_num)
2676 unsigned long hash_en;
2678 /* parse hexadecimal string */
2679 hash_en = strtoul(hash_entry_num, &end, 16);
2680 if ((hash_entry_num[0] == '\0') || (end == NULL) || (*end != '\0'))
2691 parse_rx_config(const char *q_arg)
2694 const char *p, *p0 = q_arg;
2703 unsigned long int_fld[_NUM_FLD];
2704 char *str_fld[_NUM_FLD];
2708 nb_rx_thread_params = 0;
2710 while ((p = strchr(p0, '(')) != NULL) {
2712 p0 = strchr(p, ')');
2717 if (size >= sizeof(s))
2720 snprintf(s, sizeof(s), "%.*s", size, p);
2721 if (rte_strsplit(s, sizeof(s), str_fld, _NUM_FLD, ',') != _NUM_FLD)
2723 for (i = 0; i < _NUM_FLD; i++) {
2725 int_fld[i] = strtoul(str_fld[i], &end, 0);
2726 if (errno != 0 || end == str_fld[i] || int_fld[i] > 255)
2729 if (nb_rx_thread_params >= MAX_LCORE_PARAMS) {
2730 printf("exceeded max number of rx params: %hu\n",
2731 nb_rx_thread_params);
2734 rx_thread_params_array[nb_rx_thread_params].port_id =
2735 (uint8_t)int_fld[FLD_PORT];
2736 rx_thread_params_array[nb_rx_thread_params].queue_id =
2737 (uint8_t)int_fld[FLD_QUEUE];
2738 rx_thread_params_array[nb_rx_thread_params].lcore_id =
2739 (uint8_t)int_fld[FLD_LCORE];
2740 rx_thread_params_array[nb_rx_thread_params].thread_id =
2741 (uint8_t)int_fld[FLD_THREAD];
2742 ++nb_rx_thread_params;
2744 rx_thread_params = rx_thread_params_array;
2749 parse_tx_config(const char *q_arg)
2752 const char *p, *p0 = q_arg;
2759 unsigned long int_fld[_NUM_FLD];
2760 char *str_fld[_NUM_FLD];
2764 nb_tx_thread_params = 0;
2766 while ((p = strchr(p0, '(')) != NULL) {
2768 p0 = strchr(p, ')');
2773 if (size >= sizeof(s))
2776 snprintf(s, sizeof(s), "%.*s", size, p);
2777 if (rte_strsplit(s, sizeof(s), str_fld, _NUM_FLD, ',') != _NUM_FLD)
2779 for (i = 0; i < _NUM_FLD; i++) {
2781 int_fld[i] = strtoul(str_fld[i], &end, 0);
2782 if (errno != 0 || end == str_fld[i] || int_fld[i] > 255)
2785 if (nb_tx_thread_params >= MAX_LCORE_PARAMS) {
2786 printf("exceeded max number of tx params: %hu\n",
2787 nb_tx_thread_params);
2790 tx_thread_params_array[nb_tx_thread_params].lcore_id =
2791 (uint8_t)int_fld[FLD_LCORE];
2792 tx_thread_params_array[nb_tx_thread_params].thread_id =
2793 (uint8_t)int_fld[FLD_THREAD];
2794 ++nb_tx_thread_params;
2796 tx_thread_params = tx_thread_params_array;
2801 #if (APP_CPU_LOAD > 0)
2803 parse_stat_lcore(const char *stat_lcore)
2806 unsigned long lcore_id;
2808 lcore_id = strtoul(stat_lcore, &end, 10);
2809 if ((stat_lcore[0] == '\0') || (end == NULL) || (*end != '\0'))
2817 parse_eth_dest(const char *optarg)
2821 uint8_t c, *dest, peer_addr[6];
2824 portid = strtoul(optarg, &port_end, 10);
2825 if (errno != 0 || port_end == optarg || *port_end++ != ',')
2826 rte_exit(EXIT_FAILURE,
2827 "Invalid eth-dest: %s", optarg);
2828 if (portid >= RTE_MAX_ETHPORTS)
2829 rte_exit(EXIT_FAILURE,
2830 "eth-dest: port %d >= RTE_MAX_ETHPORTS(%d)\n",
2831 portid, RTE_MAX_ETHPORTS);
2833 if (cmdline_parse_etheraddr(NULL, port_end,
2834 &peer_addr, sizeof(peer_addr)) < 0)
2835 rte_exit(EXIT_FAILURE,
2836 "Invalid ethernet address: %s\n",
2838 dest = (uint8_t *)&dest_eth_addr[portid];
2839 for (c = 0; c < 6; c++)
2840 dest[c] = peer_addr[c];
2841 *(uint64_t *)(val_eth + portid) = dest_eth_addr[portid];
2844 #define CMD_LINE_OPT_RX_CONFIG "rx"
2845 #define CMD_LINE_OPT_TX_CONFIG "tx"
2846 #define CMD_LINE_OPT_STAT_LCORE "stat-lcore"
2847 #define CMD_LINE_OPT_ETH_DEST "eth-dest"
2848 #define CMD_LINE_OPT_NO_NUMA "no-numa"
2849 #define CMD_LINE_OPT_IPV6 "ipv6"
2850 #define CMD_LINE_OPT_ENABLE_JUMBO "enable-jumbo"
2851 #define CMD_LINE_OPT_HASH_ENTRY_NUM "hash-entry-num"
2852 #define CMD_LINE_OPT_NO_LTHREADS "no-lthreads"
2854 /* Parse the argument given in the command line of the application */
2856 parse_args(int argc, char **argv)
2861 char *prgname = argv[0];
2862 static struct option lgopts[] = {
2863 {CMD_LINE_OPT_RX_CONFIG, 1, 0, 0},
2864 {CMD_LINE_OPT_TX_CONFIG, 1, 0, 0},
2865 {CMD_LINE_OPT_STAT_LCORE, 1, 0, 0},
2866 {CMD_LINE_OPT_ETH_DEST, 1, 0, 0},
2867 {CMD_LINE_OPT_NO_NUMA, 0, 0, 0},
2868 {CMD_LINE_OPT_IPV6, 0, 0, 0},
2869 {CMD_LINE_OPT_ENABLE_JUMBO, 0, 0, 0},
2870 {CMD_LINE_OPT_HASH_ENTRY_NUM, 1, 0, 0},
2871 {CMD_LINE_OPT_NO_LTHREADS, 0, 0, 0},
2877 while ((opt = getopt_long(argc, argvopt, "p:P",
2878 lgopts, &option_index)) != EOF) {
2883 enabled_port_mask = parse_portmask(optarg);
2884 if (enabled_port_mask == 0) {
2885 printf("invalid portmask\n");
2886 print_usage(prgname);
2891 printf("Promiscuous mode selected\n");
2897 if (!strncmp(lgopts[option_index].name, CMD_LINE_OPT_RX_CONFIG,
2898 sizeof(CMD_LINE_OPT_RX_CONFIG))) {
2899 ret = parse_rx_config(optarg);
2901 printf("invalid rx-config\n");
2902 print_usage(prgname);
2907 if (!strncmp(lgopts[option_index].name, CMD_LINE_OPT_TX_CONFIG,
2908 sizeof(CMD_LINE_OPT_TX_CONFIG))) {
2909 ret = parse_tx_config(optarg);
2911 printf("invalid tx-config\n");
2912 print_usage(prgname);
2917 #if (APP_CPU_LOAD > 0)
2918 if (!strncmp(lgopts[option_index].name, CMD_LINE_OPT_STAT_LCORE,
2919 sizeof(CMD_LINE_OPT_STAT_LCORE))) {
2920 cpu_load_lcore_id = parse_stat_lcore(optarg);
2924 if (!strncmp(lgopts[option_index].name, CMD_LINE_OPT_ETH_DEST,
2925 sizeof(CMD_LINE_OPT_ETH_DEST)))
2926 parse_eth_dest(optarg);
2928 if (!strncmp(lgopts[option_index].name, CMD_LINE_OPT_NO_NUMA,
2929 sizeof(CMD_LINE_OPT_NO_NUMA))) {
2930 printf("numa is disabled\n");
2934 #if (APP_LOOKUP_METHOD == APP_LOOKUP_EXACT_MATCH)
2935 if (!strncmp(lgopts[option_index].name, CMD_LINE_OPT_IPV6,
2936 sizeof(CMD_LINE_OPT_IPV6))) {
2937 printf("ipv6 is specified\n");
2942 if (!strncmp(lgopts[option_index].name, CMD_LINE_OPT_NO_LTHREADS,
2943 sizeof(CMD_LINE_OPT_NO_LTHREADS))) {
2944 printf("l-threads model is disabled\n");
2948 if (!strncmp(lgopts[option_index].name, CMD_LINE_OPT_ENABLE_JUMBO,
2949 sizeof(CMD_LINE_OPT_ENABLE_JUMBO))) {
2950 struct option lenopts = {"max-pkt-len", required_argument, 0,
2953 printf("jumbo frame is enabled - disabling simple TX path\n");
2954 port_conf.rxmode.jumbo_frame = 1;
2956 /* if no max-pkt-len set, use the default value ETHER_MAX_LEN */
2957 if (0 == getopt_long(argc, argvopt, "", &lenopts,
2960 ret = parse_max_pkt_len(optarg);
2961 if ((ret < 64) || (ret > MAX_JUMBO_PKT_LEN)) {
2962 printf("invalid packet length\n");
2963 print_usage(prgname);
2966 port_conf.rxmode.max_rx_pkt_len = ret;
2968 printf("set jumbo frame max packet length to %u\n",
2969 (unsigned int)port_conf.rxmode.max_rx_pkt_len);
2971 #if (APP_LOOKUP_METHOD == APP_LOOKUP_EXACT_MATCH)
2972 if (!strncmp(lgopts[option_index].name, CMD_LINE_OPT_HASH_ENTRY_NUM,
2973 sizeof(CMD_LINE_OPT_HASH_ENTRY_NUM))) {
2974 ret = parse_hash_entry_number(optarg);
2975 if ((ret > 0) && (ret <= L3FWD_HASH_ENTRIES)) {
2976 hash_entry_number = ret;
2978 printf("invalid hash entry number\n");
2979 print_usage(prgname);
2987 print_usage(prgname);
2993 argv[optind-1] = prgname;
2996 optind = 0; /* reset getopt lib */
3001 print_ethaddr(const char *name, const struct ether_addr *eth_addr)
3003 char buf[ETHER_ADDR_FMT_SIZE];
3005 ether_format_addr(buf, ETHER_ADDR_FMT_SIZE, eth_addr);
3006 printf("%s%s", name, buf);
3009 #if (APP_LOOKUP_METHOD == APP_LOOKUP_EXACT_MATCH)
3011 static void convert_ipv4_5tuple(struct ipv4_5tuple *key1,
3012 union ipv4_5tuple_host *key2)
3014 key2->ip_dst = rte_cpu_to_be_32(key1->ip_dst);
3015 key2->ip_src = rte_cpu_to_be_32(key1->ip_src);
3016 key2->port_dst = rte_cpu_to_be_16(key1->port_dst);
3017 key2->port_src = rte_cpu_to_be_16(key1->port_src);
3018 key2->proto = key1->proto;
3023 static void convert_ipv6_5tuple(struct ipv6_5tuple *key1,
3024 union ipv6_5tuple_host *key2)
3028 for (i = 0; i < 16; i++) {
3029 key2->ip_dst[i] = key1->ip_dst[i];
3030 key2->ip_src[i] = key1->ip_src[i];
3032 key2->port_dst = rte_cpu_to_be_16(key1->port_dst);
3033 key2->port_src = rte_cpu_to_be_16(key1->port_src);
3034 key2->proto = key1->proto;
3040 #define BYTE_VALUE_MAX 256
3041 #define ALL_32_BITS 0xffffffff
3042 #define BIT_8_TO_15 0x0000ff00
3044 populate_ipv4_few_flow_into_table(const struct rte_hash *h)
3048 uint32_t array_len = RTE_DIM(ipv4_l3fwd_route_array);
3050 mask0 = _mm_set_epi32(ALL_32_BITS, ALL_32_BITS, ALL_32_BITS, BIT_8_TO_15);
3051 for (i = 0; i < array_len; i++) {
3052 struct ipv4_l3fwd_route entry;
3053 union ipv4_5tuple_host newkey;
3055 entry = ipv4_l3fwd_route_array[i];
3056 convert_ipv4_5tuple(&entry.key, &newkey);
3057 ret = rte_hash_add_key(h, (void *)&newkey);
3059 rte_exit(EXIT_FAILURE, "Unable to add entry %" PRIu32
3060 " to the l3fwd hash.\n", i);
3062 ipv4_l3fwd_out_if[ret] = entry.if_out;
3064 printf("Hash: Adding 0x%" PRIx32 " keys\n", array_len);
3067 #define BIT_16_TO_23 0x00ff0000
3069 populate_ipv6_few_flow_into_table(const struct rte_hash *h)
3073 uint32_t array_len = RTE_DIM(ipv6_l3fwd_route_array);
3075 mask1 = _mm_set_epi32(ALL_32_BITS, ALL_32_BITS, ALL_32_BITS, BIT_16_TO_23);
3076 mask2 = _mm_set_epi32(0, 0, ALL_32_BITS, ALL_32_BITS);
3077 for (i = 0; i < array_len; i++) {
3078 struct ipv6_l3fwd_route entry;
3079 union ipv6_5tuple_host newkey;
3081 entry = ipv6_l3fwd_route_array[i];
3082 convert_ipv6_5tuple(&entry.key, &newkey);
3083 ret = rte_hash_add_key(h, (void *)&newkey);
3085 rte_exit(EXIT_FAILURE, "Unable to add entry %" PRIu32
3086 " to the l3fwd hash.\n", i);
3088 ipv6_l3fwd_out_if[ret] = entry.if_out;
3090 printf("Hash: Adding 0x%" PRIx32 "keys\n", array_len);
3093 #define NUMBER_PORT_USED 4
3095 populate_ipv4_many_flow_into_table(const struct rte_hash *h,
3096 unsigned int nr_flow)
3100 mask0 = _mm_set_epi32(ALL_32_BITS, ALL_32_BITS, ALL_32_BITS, BIT_8_TO_15);
3102 for (i = 0; i < nr_flow; i++) {
3103 struct ipv4_l3fwd_route entry;
3104 union ipv4_5tuple_host newkey;
3105 uint8_t a = (uint8_t)((i / NUMBER_PORT_USED) % BYTE_VALUE_MAX);
3106 uint8_t b = (uint8_t)(((i / NUMBER_PORT_USED) / BYTE_VALUE_MAX) %
3108 uint8_t c = (uint8_t)((i / NUMBER_PORT_USED) / (BYTE_VALUE_MAX *
3110 /* Create the ipv4 exact match flow */
3111 memset(&entry, 0, sizeof(entry));
3112 switch (i & (NUMBER_PORT_USED - 1)) {
3114 entry = ipv4_l3fwd_route_array[0];
3115 entry.key.ip_dst = IPv4(101, c, b, a);
3118 entry = ipv4_l3fwd_route_array[1];
3119 entry.key.ip_dst = IPv4(201, c, b, a);
3122 entry = ipv4_l3fwd_route_array[2];
3123 entry.key.ip_dst = IPv4(111, c, b, a);
3126 entry = ipv4_l3fwd_route_array[3];
3127 entry.key.ip_dst = IPv4(211, c, b, a);
3130 convert_ipv4_5tuple(&entry.key, &newkey);
3131 int32_t ret = rte_hash_add_key(h, (void *)&newkey);
3134 rte_exit(EXIT_FAILURE, "Unable to add entry %u\n", i);
3136 ipv4_l3fwd_out_if[ret] = (uint8_t)entry.if_out;
3139 printf("Hash: Adding 0x%x keys\n", nr_flow);
3143 populate_ipv6_many_flow_into_table(const struct rte_hash *h,
3144 unsigned int nr_flow)
3148 mask1 = _mm_set_epi32(ALL_32_BITS, ALL_32_BITS, ALL_32_BITS, BIT_16_TO_23);
3149 mask2 = _mm_set_epi32(0, 0, ALL_32_BITS, ALL_32_BITS);
3150 for (i = 0; i < nr_flow; i++) {
3151 struct ipv6_l3fwd_route entry;
3152 union ipv6_5tuple_host newkey;
3154 uint8_t a = (uint8_t) ((i / NUMBER_PORT_USED) % BYTE_VALUE_MAX);
3155 uint8_t b = (uint8_t) (((i / NUMBER_PORT_USED) / BYTE_VALUE_MAX) %
3157 uint8_t c = (uint8_t) ((i / NUMBER_PORT_USED) / (BYTE_VALUE_MAX *
3160 /* Create the ipv6 exact match flow */
3161 memset(&entry, 0, sizeof(entry));
3162 switch (i & (NUMBER_PORT_USED - 1)) {
3164 entry = ipv6_l3fwd_route_array[0];
3167 entry = ipv6_l3fwd_route_array[1];
3170 entry = ipv6_l3fwd_route_array[2];
3173 entry = ipv6_l3fwd_route_array[3];
3176 entry.key.ip_dst[13] = c;
3177 entry.key.ip_dst[14] = b;
3178 entry.key.ip_dst[15] = a;
3179 convert_ipv6_5tuple(&entry.key, &newkey);
3180 int32_t ret = rte_hash_add_key(h, (void *)&newkey);
3183 rte_exit(EXIT_FAILURE, "Unable to add entry %u\n", i);
3185 ipv6_l3fwd_out_if[ret] = (uint8_t) entry.if_out;
3188 printf("Hash: Adding 0x%x keys\n", nr_flow);
3192 setup_hash(int socketid)
3194 struct rte_hash_parameters ipv4_l3fwd_hash_params = {
3196 .entries = L3FWD_HASH_ENTRIES,
3197 .key_len = sizeof(union ipv4_5tuple_host),
3198 .hash_func = ipv4_hash_crc,
3199 .hash_func_init_val = 0,
3202 struct rte_hash_parameters ipv6_l3fwd_hash_params = {
3204 .entries = L3FWD_HASH_ENTRIES,
3205 .key_len = sizeof(union ipv6_5tuple_host),
3206 .hash_func = ipv6_hash_crc,
3207 .hash_func_init_val = 0,
3212 /* create ipv4 hash */
3213 snprintf(s, sizeof(s), "ipv4_l3fwd_hash_%d", socketid);
3214 ipv4_l3fwd_hash_params.name = s;
3215 ipv4_l3fwd_hash_params.socket_id = socketid;
3216 ipv4_l3fwd_lookup_struct[socketid] =
3217 rte_hash_create(&ipv4_l3fwd_hash_params);
3218 if (ipv4_l3fwd_lookup_struct[socketid] == NULL)
3219 rte_exit(EXIT_FAILURE, "Unable to create the l3fwd hash on "
3220 "socket %d\n", socketid);
3222 /* create ipv6 hash */
3223 snprintf(s, sizeof(s), "ipv6_l3fwd_hash_%d", socketid);
3224 ipv6_l3fwd_hash_params.name = s;
3225 ipv6_l3fwd_hash_params.socket_id = socketid;
3226 ipv6_l3fwd_lookup_struct[socketid] =
3227 rte_hash_create(&ipv6_l3fwd_hash_params);
3228 if (ipv6_l3fwd_lookup_struct[socketid] == NULL)
3229 rte_exit(EXIT_FAILURE, "Unable to create the l3fwd hash on "
3230 "socket %d\n", socketid);
3232 if (hash_entry_number != HASH_ENTRY_NUMBER_DEFAULT) {
3233 /* For testing hash matching with a large number of flows we
3234 * generate millions of IP 5-tuples with an incremented dst
3235 * address to initialize the hash table. */
3237 /* populate the ipv4 hash */
3238 populate_ipv4_many_flow_into_table(
3239 ipv4_l3fwd_lookup_struct[socketid], hash_entry_number);
3241 /* populate the ipv6 hash */
3242 populate_ipv6_many_flow_into_table(
3243 ipv6_l3fwd_lookup_struct[socketid], hash_entry_number);
3246 /* Use data in ipv4/ipv6 l3fwd lookup table directly to initialize
3249 /* populate the ipv4 hash */
3250 populate_ipv4_few_flow_into_table(
3251 ipv4_l3fwd_lookup_struct[socketid]);
3253 /* populate the ipv6 hash */
3254 populate_ipv6_few_flow_into_table(
3255 ipv6_l3fwd_lookup_struct[socketid]);
3261 #if (APP_LOOKUP_METHOD == APP_LOOKUP_LPM)
3263 setup_lpm(int socketid)
3265 struct rte_lpm6_config config;
3266 struct rte_lpm_config lpm_ipv4_config;
3271 /* create the LPM table */
3272 snprintf(s, sizeof(s), "IPV4_L3FWD_LPM_%d", socketid);
3273 lpm_ipv4_config.max_rules = IPV4_L3FWD_LPM_MAX_RULES;
3274 lpm_ipv4_config.number_tbl8s = 256;
3275 lpm_ipv4_config.flags = 0;
3276 ipv4_l3fwd_lookup_struct[socketid] =
3277 rte_lpm_create(s, socketid, &lpm_ipv4_config);
3278 if (ipv4_l3fwd_lookup_struct[socketid] == NULL)
3279 rte_exit(EXIT_FAILURE, "Unable to create the l3fwd LPM table"
3280 " on socket %d\n", socketid);
3282 /* populate the LPM table */
3283 for (i = 0; i < IPV4_L3FWD_NUM_ROUTES; i++) {
3285 /* skip unused ports */
3286 if ((1 << ipv4_l3fwd_route_array[i].if_out &
3287 enabled_port_mask) == 0)
3290 ret = rte_lpm_add(ipv4_l3fwd_lookup_struct[socketid],
3291 ipv4_l3fwd_route_array[i].ip,
3292 ipv4_l3fwd_route_array[i].depth,
3293 ipv4_l3fwd_route_array[i].if_out);
3296 rte_exit(EXIT_FAILURE, "Unable to add entry %u to the "
3297 "l3fwd LPM table on socket %d\n",
3301 printf("LPM: Adding route 0x%08x / %d (%d)\n",
3302 (unsigned)ipv4_l3fwd_route_array[i].ip,
3303 ipv4_l3fwd_route_array[i].depth,
3304 ipv4_l3fwd_route_array[i].if_out);
3307 /* create the LPM6 table */
3308 snprintf(s, sizeof(s), "IPV6_L3FWD_LPM_%d", socketid);
3310 config.max_rules = IPV6_L3FWD_LPM_MAX_RULES;
3311 config.number_tbl8s = IPV6_L3FWD_LPM_NUMBER_TBL8S;
3313 ipv6_l3fwd_lookup_struct[socketid] = rte_lpm6_create(s, socketid,
3315 if (ipv6_l3fwd_lookup_struct[socketid] == NULL)
3316 rte_exit(EXIT_FAILURE, "Unable to create the l3fwd LPM table"
3317 " on socket %d\n", socketid);
3319 /* populate the LPM table */
3320 for (i = 0; i < IPV6_L3FWD_NUM_ROUTES; i++) {
3322 /* skip unused ports */
3323 if ((1 << ipv6_l3fwd_route_array[i].if_out &
3324 enabled_port_mask) == 0)
3327 ret = rte_lpm6_add(ipv6_l3fwd_lookup_struct[socketid],
3328 ipv6_l3fwd_route_array[i].ip,
3329 ipv6_l3fwd_route_array[i].depth,
3330 ipv6_l3fwd_route_array[i].if_out);
3333 rte_exit(EXIT_FAILURE, "Unable to add entry %u to the "
3334 "l3fwd LPM table on socket %d\n",
3338 printf("LPM: Adding route %s / %d (%d)\n",
3340 ipv6_l3fwd_route_array[i].depth,
3341 ipv6_l3fwd_route_array[i].if_out);
3347 init_mem(unsigned nb_mbuf)
3349 struct lcore_conf *qconf;
3354 for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
3355 if (rte_lcore_is_enabled(lcore_id) == 0)
3359 socketid = rte_lcore_to_socket_id(lcore_id);
3363 if (socketid >= NB_SOCKETS) {
3364 rte_exit(EXIT_FAILURE, "Socket %d of lcore %u is out of range %d\n",
3365 socketid, lcore_id, NB_SOCKETS);
3367 if (pktmbuf_pool[socketid] == NULL) {
3368 snprintf(s, sizeof(s), "mbuf_pool_%d", socketid);
3369 pktmbuf_pool[socketid] =
3370 rte_pktmbuf_pool_create(s, nb_mbuf,
3371 MEMPOOL_CACHE_SIZE, 0,
3372 RTE_MBUF_DEFAULT_BUF_SIZE, socketid);
3373 if (pktmbuf_pool[socketid] == NULL)
3374 rte_exit(EXIT_FAILURE,
3375 "Cannot init mbuf pool on socket %d\n", socketid);
3377 printf("Allocated mbuf pool on socket %d\n", socketid);
3379 #if (APP_LOOKUP_METHOD == APP_LOOKUP_LPM)
3380 setup_lpm(socketid);
3382 setup_hash(socketid);
3385 qconf = &lcore_conf[lcore_id];
3386 qconf->ipv4_lookup_struct = ipv4_l3fwd_lookup_struct[socketid];
3387 qconf->ipv6_lookup_struct = ipv6_l3fwd_lookup_struct[socketid];
3392 /* Check the link status of all ports in up to 9s, and print them finally */
3394 check_all_ports_link_status(uint8_t port_num, uint32_t port_mask)
3396 #define CHECK_INTERVAL 100 /* 100ms */
3397 #define MAX_CHECK_TIME 90 /* 9s (90 * 100ms) in total */
3398 uint8_t portid, count, all_ports_up, print_flag = 0;
3399 struct rte_eth_link link;
3401 printf("\nChecking link status");
3403 for (count = 0; count <= MAX_CHECK_TIME; count++) {
3405 for (portid = 0; portid < port_num; portid++) {
3406 if ((port_mask & (1 << portid)) == 0)
3408 memset(&link, 0, sizeof(link));
3409 rte_eth_link_get_nowait(portid, &link);
3410 /* print link status if flag set */
3411 if (print_flag == 1) {
3412 if (link.link_status)
3413 printf("Port %d Link Up - speed %u "
3414 "Mbps - %s\n", (uint8_t)portid,
3415 (unsigned)link.link_speed,
3416 (link.link_duplex == ETH_LINK_FULL_DUPLEX) ?
3417 ("full-duplex") : ("half-duplex\n"));
3419 printf("Port %d Link Down\n",
3423 /* clear all_ports_up flag if any link down */
3424 if (link.link_status == ETH_LINK_DOWN) {
3429 /* after finally printing all link status, get out */
3430 if (print_flag == 1)
3433 if (all_ports_up == 0) {
3436 rte_delay_ms(CHECK_INTERVAL);
3439 /* set the print_flag if all ports up or timeout */
3440 if (all_ports_up == 1 || count == (MAX_CHECK_TIME - 1)) {
3448 main(int argc, char **argv)
3450 struct rte_eth_dev_info dev_info;
3451 struct rte_eth_txconf *txconf;
3457 uint32_t n_tx_queue, nb_lcores;
3458 uint8_t portid, nb_rx_queue, queue, socketid;
3461 ret = rte_eal_init(argc, argv);
3463 rte_exit(EXIT_FAILURE, "Invalid EAL parameters\n");
3467 /* pre-init dst MACs for all ports to 02:00:00:00:00:xx */
3468 for (portid = 0; portid < RTE_MAX_ETHPORTS; portid++) {
3469 dest_eth_addr[portid] = ETHER_LOCAL_ADMIN_ADDR +
3470 ((uint64_t)portid << 40);
3471 *(uint64_t *)(val_eth + portid) = dest_eth_addr[portid];
3474 /* parse application arguments (after the EAL ones) */
3475 ret = parse_args(argc, argv);
3477 rte_exit(EXIT_FAILURE, "Invalid L3FWD parameters\n");
3479 if (check_lcore_params() < 0)
3480 rte_exit(EXIT_FAILURE, "check_lcore_params failed\n");
3482 printf("Initializing rx-queues...\n");
3483 ret = init_rx_queues();
3485 rte_exit(EXIT_FAILURE, "init_rx_queues failed\n");
3487 printf("Initializing tx-threads...\n");
3488 ret = init_tx_threads();
3490 rte_exit(EXIT_FAILURE, "init_tx_threads failed\n");
3492 printf("Initializing rings...\n");
3493 ret = init_rx_rings();
3495 rte_exit(EXIT_FAILURE, "init_rx_rings failed\n");
3497 nb_ports = rte_eth_dev_count();
3499 if (check_port_config(nb_ports) < 0)
3500 rte_exit(EXIT_FAILURE, "check_port_config failed\n");
3502 nb_lcores = rte_lcore_count();
3504 /* initialize all ports */
3505 for (portid = 0; portid < nb_ports; portid++) {
3506 /* skip ports that are not enabled */
3507 if ((enabled_port_mask & (1 << portid)) == 0) {
3508 printf("\nSkipping disabled port %d\n", portid);
3513 printf("Initializing port %d ... ", portid);
3516 nb_rx_queue = get_port_n_rx_queues(portid);
3517 n_tx_queue = nb_lcores;
3518 if (n_tx_queue > MAX_TX_QUEUE_PER_PORT)
3519 n_tx_queue = MAX_TX_QUEUE_PER_PORT;
3520 printf("Creating queues: nb_rxq=%d nb_txq=%u... ",
3521 nb_rx_queue, (unsigned)n_tx_queue);
3522 ret = rte_eth_dev_configure(portid, nb_rx_queue,
3523 (uint16_t)n_tx_queue, &port_conf);
3525 rte_exit(EXIT_FAILURE, "Cannot configure device: err=%d, port=%d\n",
3528 rte_eth_macaddr_get(portid, &ports_eth_addr[portid]);
3529 print_ethaddr(" Address:", &ports_eth_addr[portid]);
3531 print_ethaddr("Destination:",
3532 (const struct ether_addr *)&dest_eth_addr[portid]);
3536 * prepare src MACs for each port.
3538 ether_addr_copy(&ports_eth_addr[portid],
3539 (struct ether_addr *)(val_eth + portid) + 1);
3542 ret = init_mem(NB_MBUF);
3544 rte_exit(EXIT_FAILURE, "init_mem failed\n");
3546 /* init one TX queue per couple (lcore,port) */
3548 for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
3549 if (rte_lcore_is_enabled(lcore_id) == 0)
3553 socketid = (uint8_t)rte_lcore_to_socket_id(lcore_id);
3557 printf("txq=%u,%d,%d ", lcore_id, queueid, socketid);
3560 rte_eth_dev_info_get(portid, &dev_info);
3561 txconf = &dev_info.default_txconf;
3562 if (port_conf.rxmode.jumbo_frame)
3563 txconf->txq_flags = 0;
3564 ret = rte_eth_tx_queue_setup(portid, queueid, nb_txd,
3567 rte_exit(EXIT_FAILURE, "rte_eth_tx_queue_setup: err=%d, "
3568 "port=%d\n", ret, portid);
3570 tx_thread[lcore_id].tx_queue_id[portid] = queueid;
3576 for (i = 0; i < n_rx_thread; i++) {
3577 lcore_id = rx_thread[i].conf.lcore_id;
3579 if (rte_lcore_is_enabled(lcore_id) == 0) {
3580 rte_exit(EXIT_FAILURE,
3581 "Cannot start Rx thread on lcore %u: lcore disabled\n",
3586 printf("\nInitializing rx queues for Rx thread %d on lcore %u ... ",
3590 /* init RX queues */
3591 for (queue = 0; queue < rx_thread[i].n_rx_queue; ++queue) {
3592 portid = rx_thread[i].rx_queue_list[queue].port_id;
3593 queueid = rx_thread[i].rx_queue_list[queue].queue_id;
3596 socketid = (uint8_t)rte_lcore_to_socket_id(lcore_id);
3600 printf("rxq=%d,%d,%d ", portid, queueid, socketid);
3603 ret = rte_eth_rx_queue_setup(portid, queueid, nb_rxd,
3606 pktmbuf_pool[socketid]);
3608 rte_exit(EXIT_FAILURE, "rte_eth_rx_queue_setup: err=%d, "
3609 "port=%d\n", ret, portid);
3616 for (portid = 0; portid < nb_ports; portid++) {
3617 if ((enabled_port_mask & (1 << portid)) == 0)
3621 ret = rte_eth_dev_start(portid);
3623 rte_exit(EXIT_FAILURE, "rte_eth_dev_start: err=%d, port=%d\n",
3627 * If enabled, put device in promiscuous mode.
3628 * This allows IO forwarding mode to forward packets
3629 * to itself through 2 cross-connected ports of the
3633 rte_eth_promiscuous_enable(portid);
3636 check_all_ports_link_status((uint8_t)nb_ports, enabled_port_mask);
3639 printf("Starting L-Threading Model\n");
3641 #if (APP_CPU_LOAD > 0)
3642 if (cpu_load_lcore_id > 0)
3643 /* Use one lcore for cpu load collector */
3647 lthread_num_schedulers_set(nb_lcores);
3648 rte_eal_mp_remote_launch(sched_spawner, NULL, SKIP_MASTER);
3649 lthread_master_spawner(NULL);
3652 printf("Starting P-Threading Model\n");
3653 /* launch per-lcore init on every lcore */
3654 rte_eal_mp_remote_launch(pthread_run, NULL, CALL_MASTER);
3655 RTE_LCORE_FOREACH_SLAVE(lcore_id) {
3656 if (rte_eal_wait_lcore(lcore_id) < 0)