4 * Copyright(c) 2010-2015 Intel Corporation. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
17 * * Neither the name of Intel Corporation nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
40 #include <sys/types.h>
42 #include <sys/queue.h>
47 #include <rte_common.h>
49 #include <rte_byteorder.h>
51 #include <rte_memory.h>
52 #include <rte_memcpy.h>
53 #include <rte_memzone.h>
55 #include <rte_per_lcore.h>
56 #include <rte_launch.h>
57 #include <rte_atomic.h>
58 #include <rte_cycles.h>
59 #include <rte_prefetch.h>
60 #include <rte_lcore.h>
61 #include <rte_per_lcore.h>
62 #include <rte_branch_prediction.h>
63 #include <rte_interrupts.h>
65 #include <rte_random.h>
66 #include <rte_debug.h>
67 #include <rte_ether.h>
68 #include <rte_ethdev.h>
70 #include <rte_mempool.h>
75 #include <rte_string_fns.h>
77 #include <cmdline_parse.h>
78 #include <cmdline_parse_etheraddr.h>
80 #include <lthread_api.h>
82 #define APP_LOOKUP_EXACT_MATCH 0
83 #define APP_LOOKUP_LPM 1
84 #define DO_RFC_1812_CHECKS
86 /* Enable cpu-load stats 0-off, 1-on */
87 #define APP_CPU_LOAD 1
89 #ifndef APP_LOOKUP_METHOD
90 #define APP_LOOKUP_METHOD APP_LOOKUP_LPM
94 * When set to zero, simple forwaring path is eanbled.
95 * When set to one, optimized forwarding path is enabled.
96 * Note that LPM optimisation path uses SSE4.1 instructions.
98 #if ((APP_LOOKUP_METHOD == APP_LOOKUP_LPM) && !defined(__SSE4_1__))
99 #define ENABLE_MULTI_BUFFER_OPTIMIZE 0
101 #define ENABLE_MULTI_BUFFER_OPTIMIZE 1
104 #if (APP_LOOKUP_METHOD == APP_LOOKUP_EXACT_MATCH)
105 #include <rte_hash.h>
106 #elif (APP_LOOKUP_METHOD == APP_LOOKUP_LPM)
108 #include <rte_lpm6.h>
110 #error "APP_LOOKUP_METHOD set to incorrect value"
113 #define RTE_LOGTYPE_L3FWD RTE_LOGTYPE_USER1
115 #define MAX_JUMBO_PKT_LEN 9600
117 #define IPV6_ADDR_LEN 16
119 #define MEMPOOL_CACHE_SIZE 256
122 * This expression is used to calculate the number of mbufs needed depending on
123 * user input, taking into account memory for rx and tx hardware rings, cache
124 * per lcore and mtable per port per lcore. RTE_MAX is used to ensure that
125 * NB_MBUF never goes below a minimum value of 8192
128 #define NB_MBUF RTE_MAX(\
129 (nb_ports*nb_rx_queue*RTE_TEST_RX_DESC_DEFAULT + \
130 nb_ports*nb_lcores*MAX_PKT_BURST + \
131 nb_ports*n_tx_queue*RTE_TEST_TX_DESC_DEFAULT + \
132 nb_lcores*MEMPOOL_CACHE_SIZE), \
135 #define MAX_PKT_BURST 32
136 #define BURST_TX_DRAIN_US 100 /* TX drain every ~100us */
139 * Try to avoid TX buffering if we have at least MAX_TX_BURST packets to send.
141 #define MAX_TX_BURST (MAX_PKT_BURST / 2)
142 #define BURST_SIZE MAX_TX_BURST
146 /* Configure how many packets ahead to prefetch, when reading packets */
147 #define PREFETCH_OFFSET 3
149 /* Used to mark destination port as 'invalid'. */
150 #define BAD_PORT ((uint16_t)-1)
155 * Configurable number of RX/TX ring descriptors
157 #define RTE_TEST_RX_DESC_DEFAULT 128
158 #define RTE_TEST_TX_DESC_DEFAULT 128
159 static uint16_t nb_rxd = RTE_TEST_RX_DESC_DEFAULT;
160 static uint16_t nb_txd = RTE_TEST_TX_DESC_DEFAULT;
162 /* ethernet addresses of ports */
163 static uint64_t dest_eth_addr[RTE_MAX_ETHPORTS];
164 static struct ether_addr ports_eth_addr[RTE_MAX_ETHPORTS];
166 static __m128i val_eth[RTE_MAX_ETHPORTS];
168 /* replace first 12B of the ethernet header. */
169 #define MASK_ETH 0x3f
171 /* mask of enabled ports */
172 static uint32_t enabled_port_mask;
173 static int promiscuous_on; /**< $et in promiscuous mode off by default. */
174 static int numa_on = 1; /**< NUMA is enabled by default. */
176 #if (APP_LOOKUP_METHOD == APP_LOOKUP_EXACT_MATCH)
177 static int ipv6; /**< ipv6 is false by default. */
180 #if (APP_CPU_LOAD == 1)
182 #define MAX_CPU RTE_MAX_LCORE
183 #define CPU_LOAD_TIMEOUT_US (5 * 1000 * 1000) /**< Timeout for collecting 5s */
185 #define CPU_PROCESS 0
187 #define MAX_CPU_COUNTER 2
192 uint64_t hits[MAX_CPU_COUNTER][MAX_CPU];
193 } __rte_cache_aligned;
195 static struct cpu_load cpu_load;
196 static int cpu_load_lcore_id = -1;
198 #define SET_CPU_BUSY(thread, counter) \
199 thread->conf.busy[counter] = 1
201 #define SET_CPU_IDLE(thread, counter) \
202 thread->conf.busy[counter] = 0
204 #define IS_CPU_BUSY(thread, counter) \
205 (thread->conf.busy[counter] > 0)
209 #define SET_CPU_BUSY(thread, counter)
210 #define SET_CPU_IDLE(thread, counter)
211 #define IS_CPU_BUSY(thread, counter) 0
217 struct rte_mbuf *m_table[MAX_PKT_BURST];
220 struct lcore_rx_queue {
223 } __rte_cache_aligned;
225 #define MAX_RX_QUEUE_PER_LCORE 16
226 #define MAX_TX_QUEUE_PER_PORT RTE_MAX_ETHPORTS
227 #define MAX_RX_QUEUE_PER_PORT 128
229 #define MAX_LCORE_PARAMS 1024
230 struct rx_thread_params {
235 } __rte_cache_aligned;
237 static struct rx_thread_params rx_thread_params_array[MAX_LCORE_PARAMS];
238 static struct rx_thread_params rx_thread_params_array_default[] = {
250 static struct rx_thread_params *rx_thread_params =
251 rx_thread_params_array_default;
252 static uint16_t nb_rx_thread_params = RTE_DIM(rx_thread_params_array_default);
254 struct tx_thread_params {
257 } __rte_cache_aligned;
259 static struct tx_thread_params tx_thread_params_array[MAX_LCORE_PARAMS];
260 static struct tx_thread_params tx_thread_params_array_default[] = {
272 static struct tx_thread_params *tx_thread_params =
273 tx_thread_params_array_default;
274 static uint16_t nb_tx_thread_params = RTE_DIM(tx_thread_params_array_default);
276 static struct rte_eth_conf port_conf = {
278 .mq_mode = ETH_MQ_RX_RSS,
279 .max_rx_pkt_len = ETHER_MAX_LEN,
281 .header_split = 0, /**< Header Split disabled */
282 .hw_ip_checksum = 1, /**< IP checksum offload enabled */
283 .hw_vlan_filter = 0, /**< VLAN filtering disabled */
284 .jumbo_frame = 0, /**< Jumbo Frame Support disabled */
285 .hw_strip_crc = 0, /**< CRC stripped by hardware */
290 .rss_hf = ETH_RSS_TCP,
294 .mq_mode = ETH_MQ_TX_NONE,
298 static struct rte_mempool *pktmbuf_pool[NB_SOCKETS];
300 #if (APP_LOOKUP_METHOD == APP_LOOKUP_EXACT_MATCH)
302 #ifdef RTE_MACHINE_CPUFLAG_SSE4_2
303 #include <rte_hash_crc.h>
304 #define DEFAULT_HASH_FUNC rte_hash_crc
306 #include <rte_jhash.h>
307 #define DEFAULT_HASH_FUNC rte_jhash
316 } __attribute__((__packed__));
318 union ipv4_5tuple_host {
331 #define XMM_NUM_IN_IPV6_5TUPLE 3
334 uint8_t ip_dst[IPV6_ADDR_LEN];
335 uint8_t ip_src[IPV6_ADDR_LEN];
339 } __attribute__((__packed__));
341 union ipv6_5tuple_host {
346 uint8_t ip_src[IPV6_ADDR_LEN];
347 uint8_t ip_dst[IPV6_ADDR_LEN];
352 __m128i xmm[XMM_NUM_IN_IPV6_5TUPLE];
355 struct ipv4_l3fwd_route {
356 struct ipv4_5tuple key;
360 struct ipv6_l3fwd_route {
361 struct ipv6_5tuple key;
365 static struct ipv4_l3fwd_route ipv4_l3fwd_route_array[] = {
366 {{IPv4(101, 0, 0, 0), IPv4(100, 10, 0, 1), 101, 11, IPPROTO_TCP}, 0},
367 {{IPv4(201, 0, 0, 0), IPv4(200, 20, 0, 1), 102, 12, IPPROTO_TCP}, 1},
368 {{IPv4(111, 0, 0, 0), IPv4(100, 30, 0, 1), 101, 11, IPPROTO_TCP}, 2},
369 {{IPv4(211, 0, 0, 0), IPv4(200, 40, 0, 1), 102, 12, IPPROTO_TCP}, 3},
372 static struct ipv6_l3fwd_route ipv6_l3fwd_route_array[] = {
374 {0xfe, 0x80, 0, 0, 0, 0, 0, 0, 0x02, 0x1e, 0x67, 0xff, 0xfe, 0, 0, 0},
375 {0xfe, 0x80, 0, 0, 0, 0, 0, 0, 0x02, 0x1b, 0x21, 0xff, 0xfe, 0x91, 0x38,
377 101, 11, IPPROTO_TCP}, 0},
380 {0xfe, 0x90, 0, 0, 0, 0, 0, 0, 0x02, 0x1e, 0x67, 0xff, 0xfe, 0, 0, 0},
381 {0xfe, 0x90, 0, 0, 0, 0, 0, 0, 0x02, 0x1b, 0x21, 0xff, 0xfe, 0x91, 0x38,
383 102, 12, IPPROTO_TCP}, 1},
386 {0xfe, 0xa0, 0, 0, 0, 0, 0, 0, 0x02, 0x1e, 0x67, 0xff, 0xfe, 0, 0, 0},
387 {0xfe, 0xa0, 0, 0, 0, 0, 0, 0, 0x02, 0x1b, 0x21, 0xff, 0xfe, 0x91, 0x38,
389 101, 11, IPPROTO_TCP}, 2},
392 {0xfe, 0xb0, 0, 0, 0, 0, 0, 0, 0x02, 0x1e, 0x67, 0xff, 0xfe, 0, 0, 0},
393 {0xfe, 0xb0, 0, 0, 0, 0, 0, 0, 0x02, 0x1b, 0x21, 0xff, 0xfe, 0x91, 0x38,
395 102, 12, IPPROTO_TCP}, 3},
398 typedef struct rte_hash lookup_struct_t;
399 static lookup_struct_t *ipv4_l3fwd_lookup_struct[NB_SOCKETS];
400 static lookup_struct_t *ipv6_l3fwd_lookup_struct[NB_SOCKETS];
402 #ifdef RTE_ARCH_X86_64
403 /* default to 4 million hash entries (approx) */
404 #define L3FWD_HASH_ENTRIES (1024*1024*4)
406 /* 32-bit has less address-space for hugepage memory, limit to 1M entries */
407 #define L3FWD_HASH_ENTRIES (1024*1024*1)
409 #define HASH_ENTRY_NUMBER_DEFAULT 4
411 static uint32_t hash_entry_number = HASH_ENTRY_NUMBER_DEFAULT;
413 static inline uint32_t
414 ipv4_hash_crc(const void *data, __rte_unused uint32_t data_len,
417 const union ipv4_5tuple_host *k;
423 p = (const uint32_t *)&k->port_src;
425 #ifdef RTE_MACHINE_CPUFLAG_SSE4_2
426 init_val = rte_hash_crc_4byte(t, init_val);
427 init_val = rte_hash_crc_4byte(k->ip_src, init_val);
428 init_val = rte_hash_crc_4byte(k->ip_dst, init_val);
429 init_val = rte_hash_crc_4byte(*p, init_val);
430 #else /* RTE_MACHINE_CPUFLAG_SSE4_2 */
431 init_val = rte_jhash_1word(t, init_val);
432 init_val = rte_jhash_1word(k->ip_src, init_val);
433 init_val = rte_jhash_1word(k->ip_dst, init_val);
434 init_val = rte_jhash_1word(*p, init_val);
435 #endif /* RTE_MACHINE_CPUFLAG_SSE4_2 */
439 static inline uint32_t
440 ipv6_hash_crc(const void *data, __rte_unused uint32_t data_len,
443 const union ipv6_5tuple_host *k;
446 #ifdef RTE_MACHINE_CPUFLAG_SSE4_2
447 const uint32_t *ip_src0, *ip_src1, *ip_src2, *ip_src3;
448 const uint32_t *ip_dst0, *ip_dst1, *ip_dst2, *ip_dst3;
449 #endif /* RTE_MACHINE_CPUFLAG_SSE4_2 */
453 p = (const uint32_t *)&k->port_src;
455 #ifdef RTE_MACHINE_CPUFLAG_SSE4_2
456 ip_src0 = (const uint32_t *) k->ip_src;
457 ip_src1 = (const uint32_t *)(k->ip_src + 4);
458 ip_src2 = (const uint32_t *)(k->ip_src + 8);
459 ip_src3 = (const uint32_t *)(k->ip_src + 12);
460 ip_dst0 = (const uint32_t *) k->ip_dst;
461 ip_dst1 = (const uint32_t *)(k->ip_dst + 4);
462 ip_dst2 = (const uint32_t *)(k->ip_dst + 8);
463 ip_dst3 = (const uint32_t *)(k->ip_dst + 12);
464 init_val = rte_hash_crc_4byte(t, init_val);
465 init_val = rte_hash_crc_4byte(*ip_src0, init_val);
466 init_val = rte_hash_crc_4byte(*ip_src1, init_val);
467 init_val = rte_hash_crc_4byte(*ip_src2, init_val);
468 init_val = rte_hash_crc_4byte(*ip_src3, init_val);
469 init_val = rte_hash_crc_4byte(*ip_dst0, init_val);
470 init_val = rte_hash_crc_4byte(*ip_dst1, init_val);
471 init_val = rte_hash_crc_4byte(*ip_dst2, init_val);
472 init_val = rte_hash_crc_4byte(*ip_dst3, init_val);
473 init_val = rte_hash_crc_4byte(*p, init_val);
474 #else /* RTE_MACHINE_CPUFLAG_SSE4_2 */
475 init_val = rte_jhash_1word(t, init_val);
476 init_val = rte_jhash(k->ip_src, sizeof(uint8_t) * IPV6_ADDR_LEN, init_val);
477 init_val = rte_jhash(k->ip_dst, sizeof(uint8_t) * IPV6_ADDR_LEN, init_val);
478 init_val = rte_jhash_1word(*p, init_val);
479 #endif /* RTE_MACHINE_CPUFLAG_SSE4_2 */
483 #define IPV4_L3FWD_NUM_ROUTES RTE_DIM(ipv4_l3fwd_route_array)
484 #define IPV6_L3FWD_NUM_ROUTES RTE_DIM(ipv6_l3fwd_route_array)
486 static uint8_t ipv4_l3fwd_out_if[L3FWD_HASH_ENTRIES] __rte_cache_aligned;
487 static uint8_t ipv6_l3fwd_out_if[L3FWD_HASH_ENTRIES] __rte_cache_aligned;
491 #if (APP_LOOKUP_METHOD == APP_LOOKUP_LPM)
492 struct ipv4_l3fwd_route {
498 struct ipv6_l3fwd_route {
504 static struct ipv4_l3fwd_route ipv4_l3fwd_route_array[] = {
505 {IPv4(1, 1, 1, 0), 24, 0},
506 {IPv4(2, 1, 1, 0), 24, 1},
507 {IPv4(3, 1, 1, 0), 24, 2},
508 {IPv4(4, 1, 1, 0), 24, 3},
509 {IPv4(5, 1, 1, 0), 24, 4},
510 {IPv4(6, 1, 1, 0), 24, 5},
511 {IPv4(7, 1, 1, 0), 24, 6},
512 {IPv4(8, 1, 1, 0), 24, 7},
515 static struct ipv6_l3fwd_route ipv6_l3fwd_route_array[] = {
516 {{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, 48, 0},
517 {{2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, 48, 1},
518 {{3, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, 48, 2},
519 {{4, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, 48, 3},
520 {{5, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, 48, 4},
521 {{6, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, 48, 5},
522 {{7, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, 48, 6},
523 {{8, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, 48, 7},
526 #define IPV4_L3FWD_NUM_ROUTES RTE_DIM(ipv4_l3fwd_route_array)
527 #define IPV6_L3FWD_NUM_ROUTES RTE_DIM(ipv6_l3fwd_route_array)
529 #define IPV4_L3FWD_LPM_MAX_RULES 1024
530 #define IPV6_L3FWD_LPM_MAX_RULES 1024
531 #define IPV6_L3FWD_LPM_NUMBER_TBL8S (1 << 16)
533 typedef struct rte_lpm lookup_struct_t;
534 typedef struct rte_lpm6 lookup6_struct_t;
535 static lookup_struct_t *ipv4_l3fwd_lookup_struct[NB_SOCKETS];
536 static lookup6_struct_t *ipv6_l3fwd_lookup_struct[NB_SOCKETS];
540 lookup_struct_t *ipv4_lookup_struct;
541 #if (APP_LOOKUP_METHOD == APP_LOOKUP_LPM)
542 lookup6_struct_t *ipv6_lookup_struct;
544 lookup_struct_t *ipv6_lookup_struct;
547 } __rte_cache_aligned;
549 static struct lcore_conf lcore_conf[RTE_MAX_LCORE];
550 RTE_DEFINE_PER_LCORE(struct lcore_conf *, lcore_conf);
552 #define MAX_RX_QUEUE_PER_THREAD 16
553 #define MAX_TX_PORT_PER_THREAD RTE_MAX_ETHPORTS
554 #define MAX_TX_QUEUE_PER_PORT RTE_MAX_ETHPORTS
555 #define MAX_RX_QUEUE_PER_PORT 128
557 #define MAX_RX_THREAD 1024
558 #define MAX_TX_THREAD 1024
559 #define MAX_THREAD (MAX_RX_THREAD + MAX_TX_THREAD)
562 * Producers and consumers threads configuration
564 static int lthreads_on = 1; /**< Use lthreads for processing*/
566 rte_atomic16_t rx_counter; /**< Number of spawned rx threads */
567 rte_atomic16_t tx_counter; /**< Number of spawned tx threads */
570 uint16_t lcore_id; /**< Initial lcore for rx thread */
571 uint16_t cpu_id; /**< Cpu id for cpu load stats counter */
572 uint16_t thread_id; /**< Thread ID */
574 #if (APP_CPU_LOAD > 0)
575 int busy[MAX_CPU_COUNTER];
579 struct thread_rx_conf {
580 struct thread_conf conf;
583 struct lcore_rx_queue rx_queue_list[MAX_RX_QUEUE_PER_LCORE];
585 uint16_t n_ring; /**< Number of output rings */
586 struct rte_ring *ring[RTE_MAX_LCORE];
587 struct lthread_cond *ready[RTE_MAX_LCORE];
589 #if (APP_CPU_LOAD > 0)
590 int busy[MAX_CPU_COUNTER];
592 } __rte_cache_aligned;
594 uint16_t n_rx_thread;
595 struct thread_rx_conf rx_thread[MAX_RX_THREAD];
597 struct thread_tx_conf {
598 struct thread_conf conf;
600 uint16_t tx_queue_id[RTE_MAX_LCORE];
601 struct mbuf_table tx_mbufs[RTE_MAX_LCORE];
603 struct rte_ring *ring;
604 struct lthread_cond **ready;
606 } __rte_cache_aligned;
608 uint16_t n_tx_thread;
609 struct thread_tx_conf tx_thread[MAX_TX_THREAD];
611 /* Send burst of packets on an output interface */
613 send_burst(struct thread_tx_conf *qconf, uint16_t n, uint8_t port)
615 struct rte_mbuf **m_table;
619 queueid = qconf->tx_queue_id[port];
620 m_table = (struct rte_mbuf **)qconf->tx_mbufs[port].m_table;
622 ret = rte_eth_tx_burst(port, queueid, m_table, n);
623 if (unlikely(ret < n)) {
625 rte_pktmbuf_free(m_table[ret]);
632 /* Enqueue a single packet, and send burst if queue is filled */
634 send_single_packet(struct rte_mbuf *m, uint8_t port)
637 struct thread_tx_conf *qconf;
640 qconf = (struct thread_tx_conf *)lthread_get_data();
642 qconf = (struct thread_tx_conf *)RTE_PER_LCORE(lcore_conf)->data;
644 len = qconf->tx_mbufs[port].len;
645 qconf->tx_mbufs[port].m_table[len] = m;
648 /* enough pkts to be sent */
649 if (unlikely(len == MAX_PKT_BURST)) {
650 send_burst(qconf, MAX_PKT_BURST, port);
654 qconf->tx_mbufs[port].len = len;
658 #if ((APP_LOOKUP_METHOD == APP_LOOKUP_LPM) && \
659 (ENABLE_MULTI_BUFFER_OPTIMIZE == 1))
660 static inline __attribute__((always_inline)) void
661 send_packetsx4(uint8_t port,
662 struct rte_mbuf *m[], uint32_t num)
665 struct thread_tx_conf *qconf;
668 qconf = (struct thread_tx_conf *)lthread_get_data();
670 qconf = (struct thread_tx_conf *)RTE_PER_LCORE(lcore_conf)->data;
672 len = qconf->tx_mbufs[port].len;
675 * If TX buffer for that queue is empty, and we have enough packets,
676 * then send them straightway.
678 if (num >= MAX_TX_BURST && len == 0) {
679 n = rte_eth_tx_burst(port, qconf->tx_queue_id[port], m, num);
680 if (unlikely(n < num)) {
682 rte_pktmbuf_free(m[n]);
689 * Put packets into TX buffer for that queue.
693 n = (n > MAX_PKT_BURST) ? MAX_PKT_BURST - len : num;
696 switch (n % FWDSTEP) {
699 qconf->tx_mbufs[port].m_table[len + j] = m[j];
702 qconf->tx_mbufs[port].m_table[len + j] = m[j];
705 qconf->tx_mbufs[port].m_table[len + j] = m[j];
708 qconf->tx_mbufs[port].m_table[len + j] = m[j];
715 /* enough pkts to be sent */
716 if (unlikely(len == MAX_PKT_BURST)) {
718 send_burst(qconf, MAX_PKT_BURST, port);
720 /* copy rest of the packets into the TX buffer. */
723 switch (len % FWDSTEP) {
726 qconf->tx_mbufs[port].m_table[j] = m[n + j];
729 qconf->tx_mbufs[port].m_table[j] = m[n + j];
732 qconf->tx_mbufs[port].m_table[j] = m[n + j];
735 qconf->tx_mbufs[port].m_table[j] = m[n + j];
741 qconf->tx_mbufs[port].len = len;
743 #endif /* APP_LOOKUP_LPM */
745 #ifdef DO_RFC_1812_CHECKS
747 is_valid_ipv4_pkt(struct ipv4_hdr *pkt, uint32_t link_len)
749 /* From http://www.rfc-editor.org/rfc/rfc1812.txt section 5.2.2 */
751 * 1. The packet length reported by the Link Layer must be large
752 * enough to hold the minimum length legal IP datagram (20 bytes).
754 if (link_len < sizeof(struct ipv4_hdr))
757 /* 2. The IP checksum must be correct. */
758 /* this is checked in H/W */
761 * 3. The IP version number must be 4. If the version number is not 4
762 * then the packet may be another version of IP, such as IPng or
765 if (((pkt->version_ihl) >> 4) != 4)
768 * 4. The IP header length field must be large enough to hold the
769 * minimum length legal IP datagram (20 bytes = 5 words).
771 if ((pkt->version_ihl & 0xf) < 5)
775 * 5. The IP total length field must be large enough to hold the IP
776 * datagram header, whose length is specified in the IP header length
779 if (rte_cpu_to_be_16(pkt->total_length) < sizeof(struct ipv4_hdr))
786 #if (APP_LOOKUP_METHOD == APP_LOOKUP_EXACT_MATCH)
788 static __m128i mask0;
789 static __m128i mask1;
790 static __m128i mask2;
791 static inline uint8_t
792 get_ipv4_dst_port(void *ipv4_hdr, uint8_t portid,
793 lookup_struct_t *ipv4_l3fwd_lookup_struct)
796 union ipv4_5tuple_host key;
798 ipv4_hdr = (uint8_t *)ipv4_hdr + offsetof(struct ipv4_hdr, time_to_live);
799 __m128i data = _mm_loadu_si128((__m128i *)(ipv4_hdr));
800 /* Get 5 tuple: dst port, src port, dst IP address, src IP address and
802 key.xmm = _mm_and_si128(data, mask0);
803 /* Find destination port */
804 ret = rte_hash_lookup(ipv4_l3fwd_lookup_struct, (const void *)&key);
805 return (uint8_t)((ret < 0) ? portid : ipv4_l3fwd_out_if[ret]);
808 static inline uint8_t
809 get_ipv6_dst_port(void *ipv6_hdr, uint8_t portid,
810 lookup_struct_t *ipv6_l3fwd_lookup_struct)
813 union ipv6_5tuple_host key;
815 ipv6_hdr = (uint8_t *)ipv6_hdr + offsetof(struct ipv6_hdr, payload_len);
816 __m128i data0 = _mm_loadu_si128((__m128i *)(ipv6_hdr));
817 __m128i data1 = _mm_loadu_si128((__m128i *)(((uint8_t *)ipv6_hdr) +
819 __m128i data2 = _mm_loadu_si128((__m128i *)(((uint8_t *)ipv6_hdr) +
820 sizeof(__m128i) + sizeof(__m128i)));
821 /* Get part of 5 tuple: src IP address lower 96 bits and protocol */
822 key.xmm[0] = _mm_and_si128(data0, mask1);
823 /* Get part of 5 tuple: dst IP address lower 96 bits and src IP address
826 /* Get part of 5 tuple: dst port and src port and dst IP address higher
828 key.xmm[2] = _mm_and_si128(data2, mask2);
830 /* Find destination port */
831 ret = rte_hash_lookup(ipv6_l3fwd_lookup_struct, (const void *)&key);
832 return (uint8_t)((ret < 0) ? portid : ipv6_l3fwd_out_if[ret]);
836 #if (APP_LOOKUP_METHOD == APP_LOOKUP_LPM)
838 static inline uint8_t
839 get_ipv4_dst_port(void *ipv4_hdr, uint8_t portid,
840 lookup_struct_t *ipv4_l3fwd_lookup_struct)
844 return (uint8_t)((rte_lpm_lookup(ipv4_l3fwd_lookup_struct,
845 rte_be_to_cpu_32(((struct ipv4_hdr *)ipv4_hdr)->dst_addr),
846 &next_hop) == 0) ? next_hop : portid);
849 static inline uint8_t
850 get_ipv6_dst_port(void *ipv6_hdr, uint8_t portid,
851 lookup6_struct_t *ipv6_l3fwd_lookup_struct)
855 return (uint8_t) ((rte_lpm6_lookup(ipv6_l3fwd_lookup_struct,
856 ((struct ipv6_hdr *)ipv6_hdr)->dst_addr, &next_hop) == 0) ?
861 static inline void l3fwd_simple_forward(struct rte_mbuf *m, uint8_t portid)
862 __attribute__((unused));
864 #if ((APP_LOOKUP_METHOD == APP_LOOKUP_EXACT_MATCH) && \
865 (ENABLE_MULTI_BUFFER_OPTIMIZE == 1))
867 #define MASK_ALL_PKTS 0xff
868 #define EXCLUDE_1ST_PKT 0xfe
869 #define EXCLUDE_2ND_PKT 0xfd
870 #define EXCLUDE_3RD_PKT 0xfb
871 #define EXCLUDE_4TH_PKT 0xf7
872 #define EXCLUDE_5TH_PKT 0xef
873 #define EXCLUDE_6TH_PKT 0xdf
874 #define EXCLUDE_7TH_PKT 0xbf
875 #define EXCLUDE_8TH_PKT 0x7f
878 simple_ipv4_fwd_8pkts(struct rte_mbuf *m[8], uint8_t portid)
880 struct ether_hdr *eth_hdr[8];
881 struct ipv4_hdr *ipv4_hdr[8];
884 union ipv4_5tuple_host key[8];
887 eth_hdr[0] = rte_pktmbuf_mtod(m[0], struct ether_hdr *);
888 eth_hdr[1] = rte_pktmbuf_mtod(m[1], struct ether_hdr *);
889 eth_hdr[2] = rte_pktmbuf_mtod(m[2], struct ether_hdr *);
890 eth_hdr[3] = rte_pktmbuf_mtod(m[3], struct ether_hdr *);
891 eth_hdr[4] = rte_pktmbuf_mtod(m[4], struct ether_hdr *);
892 eth_hdr[5] = rte_pktmbuf_mtod(m[5], struct ether_hdr *);
893 eth_hdr[6] = rte_pktmbuf_mtod(m[6], struct ether_hdr *);
894 eth_hdr[7] = rte_pktmbuf_mtod(m[7], struct ether_hdr *);
896 /* Handle IPv4 headers.*/
897 ipv4_hdr[0] = rte_pktmbuf_mtod_offset(m[0], struct ipv4_hdr *,
898 sizeof(struct ether_hdr));
899 ipv4_hdr[1] = rte_pktmbuf_mtod_offset(m[1], struct ipv4_hdr *,
900 sizeof(struct ether_hdr));
901 ipv4_hdr[2] = rte_pktmbuf_mtod_offset(m[2], struct ipv4_hdr *,
902 sizeof(struct ether_hdr));
903 ipv4_hdr[3] = rte_pktmbuf_mtod_offset(m[3], struct ipv4_hdr *,
904 sizeof(struct ether_hdr));
905 ipv4_hdr[4] = rte_pktmbuf_mtod_offset(m[4], struct ipv4_hdr *,
906 sizeof(struct ether_hdr));
907 ipv4_hdr[5] = rte_pktmbuf_mtod_offset(m[5], struct ipv4_hdr *,
908 sizeof(struct ether_hdr));
909 ipv4_hdr[6] = rte_pktmbuf_mtod_offset(m[6], struct ipv4_hdr *,
910 sizeof(struct ether_hdr));
911 ipv4_hdr[7] = rte_pktmbuf_mtod_offset(m[7], struct ipv4_hdr *,
912 sizeof(struct ether_hdr));
914 #ifdef DO_RFC_1812_CHECKS
915 /* Check to make sure the packet is valid (RFC1812) */
916 uint8_t valid_mask = MASK_ALL_PKTS;
918 if (is_valid_ipv4_pkt(ipv4_hdr[0], m[0]->pkt_len) < 0) {
919 rte_pktmbuf_free(m[0]);
920 valid_mask &= EXCLUDE_1ST_PKT;
922 if (is_valid_ipv4_pkt(ipv4_hdr[1], m[1]->pkt_len) < 0) {
923 rte_pktmbuf_free(m[1]);
924 valid_mask &= EXCLUDE_2ND_PKT;
926 if (is_valid_ipv4_pkt(ipv4_hdr[2], m[2]->pkt_len) < 0) {
927 rte_pktmbuf_free(m[2]);
928 valid_mask &= EXCLUDE_3RD_PKT;
930 if (is_valid_ipv4_pkt(ipv4_hdr[3], m[3]->pkt_len) < 0) {
931 rte_pktmbuf_free(m[3]);
932 valid_mask &= EXCLUDE_4TH_PKT;
934 if (is_valid_ipv4_pkt(ipv4_hdr[4], m[4]->pkt_len) < 0) {
935 rte_pktmbuf_free(m[4]);
936 valid_mask &= EXCLUDE_5TH_PKT;
938 if (is_valid_ipv4_pkt(ipv4_hdr[5], m[5]->pkt_len) < 0) {
939 rte_pktmbuf_free(m[5]);
940 valid_mask &= EXCLUDE_6TH_PKT;
942 if (is_valid_ipv4_pkt(ipv4_hdr[6], m[6]->pkt_len) < 0) {
943 rte_pktmbuf_free(m[6]);
944 valid_mask &= EXCLUDE_7TH_PKT;
946 if (is_valid_ipv4_pkt(ipv4_hdr[7], m[7]->pkt_len) < 0) {
947 rte_pktmbuf_free(m[7]);
948 valid_mask &= EXCLUDE_8TH_PKT;
950 if (unlikely(valid_mask != MASK_ALL_PKTS)) {
956 for (i = 0; i < 8; i++)
957 if ((0x1 << i) & valid_mask)
958 l3fwd_simple_forward(m[i], portid);
960 #endif /* End of #ifdef DO_RFC_1812_CHECKS */
962 data[0] = _mm_loadu_si128(rte_pktmbuf_mtod_offset(m[0], __m128i *,
963 sizeof(struct ether_hdr) +
964 offsetof(struct ipv4_hdr, time_to_live)));
965 data[1] = _mm_loadu_si128(rte_pktmbuf_mtod_offset(m[1], __m128i *,
966 sizeof(struct ether_hdr) +
967 offsetof(struct ipv4_hdr, time_to_live)));
968 data[2] = _mm_loadu_si128(rte_pktmbuf_mtod_offset(m[2], __m128i *,
969 sizeof(struct ether_hdr) +
970 offsetof(struct ipv4_hdr, time_to_live)));
971 data[3] = _mm_loadu_si128(rte_pktmbuf_mtod_offset(m[3], __m128i *,
972 sizeof(struct ether_hdr) +
973 offsetof(struct ipv4_hdr, time_to_live)));
974 data[4] = _mm_loadu_si128(rte_pktmbuf_mtod_offset(m[4], __m128i *,
975 sizeof(struct ether_hdr) +
976 offsetof(struct ipv4_hdr, time_to_live)));
977 data[5] = _mm_loadu_si128(rte_pktmbuf_mtod_offset(m[5], __m128i *,
978 sizeof(struct ether_hdr) +
979 offsetof(struct ipv4_hdr, time_to_live)));
980 data[6] = _mm_loadu_si128(rte_pktmbuf_mtod_offset(m[6], __m128i *,
981 sizeof(struct ether_hdr) +
982 offsetof(struct ipv4_hdr, time_to_live)));
983 data[7] = _mm_loadu_si128(rte_pktmbuf_mtod_offset(m[7], __m128i *,
984 sizeof(struct ether_hdr) +
985 offsetof(struct ipv4_hdr, time_to_live)));
987 key[0].xmm = _mm_and_si128(data[0], mask0);
988 key[1].xmm = _mm_and_si128(data[1], mask0);
989 key[2].xmm = _mm_and_si128(data[2], mask0);
990 key[3].xmm = _mm_and_si128(data[3], mask0);
991 key[4].xmm = _mm_and_si128(data[4], mask0);
992 key[5].xmm = _mm_and_si128(data[5], mask0);
993 key[6].xmm = _mm_and_si128(data[6], mask0);
994 key[7].xmm = _mm_and_si128(data[7], mask0);
996 const void *key_array[8] = {&key[0], &key[1], &key[2], &key[3],
997 &key[4], &key[5], &key[6], &key[7]};
999 rte_hash_lookup_bulk(RTE_PER_LCORE(lcore_conf)->ipv4_lookup_struct,
1000 &key_array[0], 8, ret);
1001 dst_port[0] = (uint8_t) ((ret[0] < 0) ? portid : ipv4_l3fwd_out_if[ret[0]]);
1002 dst_port[1] = (uint8_t) ((ret[1] < 0) ? portid : ipv4_l3fwd_out_if[ret[1]]);
1003 dst_port[2] = (uint8_t) ((ret[2] < 0) ? portid : ipv4_l3fwd_out_if[ret[2]]);
1004 dst_port[3] = (uint8_t) ((ret[3] < 0) ? portid : ipv4_l3fwd_out_if[ret[3]]);
1005 dst_port[4] = (uint8_t) ((ret[4] < 0) ? portid : ipv4_l3fwd_out_if[ret[4]]);
1006 dst_port[5] = (uint8_t) ((ret[5] < 0) ? portid : ipv4_l3fwd_out_if[ret[5]]);
1007 dst_port[6] = (uint8_t) ((ret[6] < 0) ? portid : ipv4_l3fwd_out_if[ret[6]]);
1008 dst_port[7] = (uint8_t) ((ret[7] < 0) ? portid : ipv4_l3fwd_out_if[ret[7]]);
1010 if (dst_port[0] >= RTE_MAX_ETHPORTS ||
1011 (enabled_port_mask & 1 << dst_port[0]) == 0)
1012 dst_port[0] = portid;
1013 if (dst_port[1] >= RTE_MAX_ETHPORTS ||
1014 (enabled_port_mask & 1 << dst_port[1]) == 0)
1015 dst_port[1] = portid;
1016 if (dst_port[2] >= RTE_MAX_ETHPORTS ||
1017 (enabled_port_mask & 1 << dst_port[2]) == 0)
1018 dst_port[2] = portid;
1019 if (dst_port[3] >= RTE_MAX_ETHPORTS ||
1020 (enabled_port_mask & 1 << dst_port[3]) == 0)
1021 dst_port[3] = portid;
1022 if (dst_port[4] >= RTE_MAX_ETHPORTS ||
1023 (enabled_port_mask & 1 << dst_port[4]) == 0)
1024 dst_port[4] = portid;
1025 if (dst_port[5] >= RTE_MAX_ETHPORTS ||
1026 (enabled_port_mask & 1 << dst_port[5]) == 0)
1027 dst_port[5] = portid;
1028 if (dst_port[6] >= RTE_MAX_ETHPORTS ||
1029 (enabled_port_mask & 1 << dst_port[6]) == 0)
1030 dst_port[6] = portid;
1031 if (dst_port[7] >= RTE_MAX_ETHPORTS ||
1032 (enabled_port_mask & 1 << dst_port[7]) == 0)
1033 dst_port[7] = portid;
1035 #ifdef DO_RFC_1812_CHECKS
1036 /* Update time to live and header checksum */
1037 --(ipv4_hdr[0]->time_to_live);
1038 --(ipv4_hdr[1]->time_to_live);
1039 --(ipv4_hdr[2]->time_to_live);
1040 --(ipv4_hdr[3]->time_to_live);
1041 ++(ipv4_hdr[0]->hdr_checksum);
1042 ++(ipv4_hdr[1]->hdr_checksum);
1043 ++(ipv4_hdr[2]->hdr_checksum);
1044 ++(ipv4_hdr[3]->hdr_checksum);
1045 --(ipv4_hdr[4]->time_to_live);
1046 --(ipv4_hdr[5]->time_to_live);
1047 --(ipv4_hdr[6]->time_to_live);
1048 --(ipv4_hdr[7]->time_to_live);
1049 ++(ipv4_hdr[4]->hdr_checksum);
1050 ++(ipv4_hdr[5]->hdr_checksum);
1051 ++(ipv4_hdr[6]->hdr_checksum);
1052 ++(ipv4_hdr[7]->hdr_checksum);
1056 *(uint64_t *)ð_hdr[0]->d_addr = dest_eth_addr[dst_port[0]];
1057 *(uint64_t *)ð_hdr[1]->d_addr = dest_eth_addr[dst_port[1]];
1058 *(uint64_t *)ð_hdr[2]->d_addr = dest_eth_addr[dst_port[2]];
1059 *(uint64_t *)ð_hdr[3]->d_addr = dest_eth_addr[dst_port[3]];
1060 *(uint64_t *)ð_hdr[4]->d_addr = dest_eth_addr[dst_port[4]];
1061 *(uint64_t *)ð_hdr[5]->d_addr = dest_eth_addr[dst_port[5]];
1062 *(uint64_t *)ð_hdr[6]->d_addr = dest_eth_addr[dst_port[6]];
1063 *(uint64_t *)ð_hdr[7]->d_addr = dest_eth_addr[dst_port[7]];
1066 ether_addr_copy(&ports_eth_addr[dst_port[0]], ð_hdr[0]->s_addr);
1067 ether_addr_copy(&ports_eth_addr[dst_port[1]], ð_hdr[1]->s_addr);
1068 ether_addr_copy(&ports_eth_addr[dst_port[2]], ð_hdr[2]->s_addr);
1069 ether_addr_copy(&ports_eth_addr[dst_port[3]], ð_hdr[3]->s_addr);
1070 ether_addr_copy(&ports_eth_addr[dst_port[4]], ð_hdr[4]->s_addr);
1071 ether_addr_copy(&ports_eth_addr[dst_port[5]], ð_hdr[5]->s_addr);
1072 ether_addr_copy(&ports_eth_addr[dst_port[6]], ð_hdr[6]->s_addr);
1073 ether_addr_copy(&ports_eth_addr[dst_port[7]], ð_hdr[7]->s_addr);
1075 send_single_packet(m[0], (uint8_t)dst_port[0]);
1076 send_single_packet(m[1], (uint8_t)dst_port[1]);
1077 send_single_packet(m[2], (uint8_t)dst_port[2]);
1078 send_single_packet(m[3], (uint8_t)dst_port[3]);
1079 send_single_packet(m[4], (uint8_t)dst_port[4]);
1080 send_single_packet(m[5], (uint8_t)dst_port[5]);
1081 send_single_packet(m[6], (uint8_t)dst_port[6]);
1082 send_single_packet(m[7], (uint8_t)dst_port[7]);
1086 static inline void get_ipv6_5tuple(struct rte_mbuf *m0, __m128i mask0,
1087 __m128i mask1, union ipv6_5tuple_host *key)
1089 __m128i tmpdata0 = _mm_loadu_si128(rte_pktmbuf_mtod_offset(m0,
1090 __m128i *, sizeof(struct ether_hdr) +
1091 offsetof(struct ipv6_hdr, payload_len)));
1092 __m128i tmpdata1 = _mm_loadu_si128(rte_pktmbuf_mtod_offset(m0,
1093 __m128i *, sizeof(struct ether_hdr) +
1094 offsetof(struct ipv6_hdr, payload_len) + sizeof(__m128i)));
1095 __m128i tmpdata2 = _mm_loadu_si128(rte_pktmbuf_mtod_offset(m0,
1096 __m128i *, sizeof(struct ether_hdr) +
1097 offsetof(struct ipv6_hdr, payload_len) + sizeof(__m128i) +
1099 key->xmm[0] = _mm_and_si128(tmpdata0, mask0);
1100 key->xmm[1] = tmpdata1;
1101 key->xmm[2] = _mm_and_si128(tmpdata2, mask1);
1105 simple_ipv6_fwd_8pkts(struct rte_mbuf *m[8], uint8_t portid)
1108 uint8_t dst_port[8];
1109 struct ether_hdr *eth_hdr[8];
1110 union ipv6_5tuple_host key[8];
1112 __attribute__((unused)) struct ipv6_hdr *ipv6_hdr[8];
1114 eth_hdr[0] = rte_pktmbuf_mtod(m[0], struct ether_hdr *);
1115 eth_hdr[1] = rte_pktmbuf_mtod(m[1], struct ether_hdr *);
1116 eth_hdr[2] = rte_pktmbuf_mtod(m[2], struct ether_hdr *);
1117 eth_hdr[3] = rte_pktmbuf_mtod(m[3], struct ether_hdr *);
1118 eth_hdr[4] = rte_pktmbuf_mtod(m[4], struct ether_hdr *);
1119 eth_hdr[5] = rte_pktmbuf_mtod(m[5], struct ether_hdr *);
1120 eth_hdr[6] = rte_pktmbuf_mtod(m[6], struct ether_hdr *);
1121 eth_hdr[7] = rte_pktmbuf_mtod(m[7], struct ether_hdr *);
1123 /* Handle IPv6 headers.*/
1124 ipv6_hdr[0] = rte_pktmbuf_mtod_offset(m[0], struct ipv6_hdr *,
1125 sizeof(struct ether_hdr));
1126 ipv6_hdr[1] = rte_pktmbuf_mtod_offset(m[1], struct ipv6_hdr *,
1127 sizeof(struct ether_hdr));
1128 ipv6_hdr[2] = rte_pktmbuf_mtod_offset(m[2], struct ipv6_hdr *,
1129 sizeof(struct ether_hdr));
1130 ipv6_hdr[3] = rte_pktmbuf_mtod_offset(m[3], struct ipv6_hdr *,
1131 sizeof(struct ether_hdr));
1132 ipv6_hdr[4] = rte_pktmbuf_mtod_offset(m[4], struct ipv6_hdr *,
1133 sizeof(struct ether_hdr));
1134 ipv6_hdr[5] = rte_pktmbuf_mtod_offset(m[5], struct ipv6_hdr *,
1135 sizeof(struct ether_hdr));
1136 ipv6_hdr[6] = rte_pktmbuf_mtod_offset(m[6], struct ipv6_hdr *,
1137 sizeof(struct ether_hdr));
1138 ipv6_hdr[7] = rte_pktmbuf_mtod_offset(m[7], struct ipv6_hdr *,
1139 sizeof(struct ether_hdr));
1141 get_ipv6_5tuple(m[0], mask1, mask2, &key[0]);
1142 get_ipv6_5tuple(m[1], mask1, mask2, &key[1]);
1143 get_ipv6_5tuple(m[2], mask1, mask2, &key[2]);
1144 get_ipv6_5tuple(m[3], mask1, mask2, &key[3]);
1145 get_ipv6_5tuple(m[4], mask1, mask2, &key[4]);
1146 get_ipv6_5tuple(m[5], mask1, mask2, &key[5]);
1147 get_ipv6_5tuple(m[6], mask1, mask2, &key[6]);
1148 get_ipv6_5tuple(m[7], mask1, mask2, &key[7]);
1150 const void *key_array[8] = {&key[0], &key[1], &key[2], &key[3],
1151 &key[4], &key[5], &key[6], &key[7]};
1153 rte_hash_lookup_bulk(RTE_PER_LCORE(lcore_conf)->ipv6_lookup_struct,
1154 &key_array[0], 4, ret);
1155 dst_port[0] = (uint8_t) ((ret[0] < 0) ? portid : ipv6_l3fwd_out_if[ret[0]]);
1156 dst_port[1] = (uint8_t) ((ret[1] < 0) ? portid : ipv6_l3fwd_out_if[ret[1]]);
1157 dst_port[2] = (uint8_t) ((ret[2] < 0) ? portid : ipv6_l3fwd_out_if[ret[2]]);
1158 dst_port[3] = (uint8_t) ((ret[3] < 0) ? portid : ipv6_l3fwd_out_if[ret[3]]);
1159 dst_port[4] = (uint8_t) ((ret[4] < 0) ? portid : ipv6_l3fwd_out_if[ret[4]]);
1160 dst_port[5] = (uint8_t) ((ret[5] < 0) ? portid : ipv6_l3fwd_out_if[ret[5]]);
1161 dst_port[6] = (uint8_t) ((ret[6] < 0) ? portid : ipv6_l3fwd_out_if[ret[6]]);
1162 dst_port[7] = (uint8_t) ((ret[7] < 0) ? portid : ipv6_l3fwd_out_if[ret[7]]);
1164 if (dst_port[0] >= RTE_MAX_ETHPORTS ||
1165 (enabled_port_mask & 1 << dst_port[0]) == 0)
1166 dst_port[0] = portid;
1167 if (dst_port[1] >= RTE_MAX_ETHPORTS ||
1168 (enabled_port_mask & 1 << dst_port[1]) == 0)
1169 dst_port[1] = portid;
1170 if (dst_port[2] >= RTE_MAX_ETHPORTS ||
1171 (enabled_port_mask & 1 << dst_port[2]) == 0)
1172 dst_port[2] = portid;
1173 if (dst_port[3] >= RTE_MAX_ETHPORTS ||
1174 (enabled_port_mask & 1 << dst_port[3]) == 0)
1175 dst_port[3] = portid;
1176 if (dst_port[4] >= RTE_MAX_ETHPORTS ||
1177 (enabled_port_mask & 1 << dst_port[4]) == 0)
1178 dst_port[4] = portid;
1179 if (dst_port[5] >= RTE_MAX_ETHPORTS ||
1180 (enabled_port_mask & 1 << dst_port[5]) == 0)
1181 dst_port[5] = portid;
1182 if (dst_port[6] >= RTE_MAX_ETHPORTS ||
1183 (enabled_port_mask & 1 << dst_port[6]) == 0)
1184 dst_port[6] = portid;
1185 if (dst_port[7] >= RTE_MAX_ETHPORTS ||
1186 (enabled_port_mask & 1 << dst_port[7]) == 0)
1187 dst_port[7] = portid;
1190 *(uint64_t *)ð_hdr[0]->d_addr = dest_eth_addr[dst_port[0]];
1191 *(uint64_t *)ð_hdr[1]->d_addr = dest_eth_addr[dst_port[1]];
1192 *(uint64_t *)ð_hdr[2]->d_addr = dest_eth_addr[dst_port[2]];
1193 *(uint64_t *)ð_hdr[3]->d_addr = dest_eth_addr[dst_port[3]];
1194 *(uint64_t *)ð_hdr[4]->d_addr = dest_eth_addr[dst_port[4]];
1195 *(uint64_t *)ð_hdr[5]->d_addr = dest_eth_addr[dst_port[5]];
1196 *(uint64_t *)ð_hdr[6]->d_addr = dest_eth_addr[dst_port[6]];
1197 *(uint64_t *)ð_hdr[7]->d_addr = dest_eth_addr[dst_port[7]];
1200 ether_addr_copy(&ports_eth_addr[dst_port[0]], ð_hdr[0]->s_addr);
1201 ether_addr_copy(&ports_eth_addr[dst_port[1]], ð_hdr[1]->s_addr);
1202 ether_addr_copy(&ports_eth_addr[dst_port[2]], ð_hdr[2]->s_addr);
1203 ether_addr_copy(&ports_eth_addr[dst_port[3]], ð_hdr[3]->s_addr);
1204 ether_addr_copy(&ports_eth_addr[dst_port[4]], ð_hdr[4]->s_addr);
1205 ether_addr_copy(&ports_eth_addr[dst_port[5]], ð_hdr[5]->s_addr);
1206 ether_addr_copy(&ports_eth_addr[dst_port[6]], ð_hdr[6]->s_addr);
1207 ether_addr_copy(&ports_eth_addr[dst_port[7]], ð_hdr[7]->s_addr);
1209 send_single_packet(m[0], (uint8_t)dst_port[0]);
1210 send_single_packet(m[1], (uint8_t)dst_port[1]);
1211 send_single_packet(m[2], (uint8_t)dst_port[2]);
1212 send_single_packet(m[3], (uint8_t)dst_port[3]);
1213 send_single_packet(m[4], (uint8_t)dst_port[4]);
1214 send_single_packet(m[5], (uint8_t)dst_port[5]);
1215 send_single_packet(m[6], (uint8_t)dst_port[6]);
1216 send_single_packet(m[7], (uint8_t)dst_port[7]);
1219 #endif /* APP_LOOKUP_METHOD */
1221 static inline __attribute__((always_inline)) void
1222 l3fwd_simple_forward(struct rte_mbuf *m, uint8_t portid)
1224 struct ether_hdr *eth_hdr;
1225 struct ipv4_hdr *ipv4_hdr;
1228 eth_hdr = rte_pktmbuf_mtod(m, struct ether_hdr *);
1230 if (RTE_ETH_IS_IPV4_HDR(m->packet_type)) {
1231 /* Handle IPv4 headers.*/
1232 ipv4_hdr = rte_pktmbuf_mtod_offset(m, struct ipv4_hdr *,
1233 sizeof(struct ether_hdr));
1235 #ifdef DO_RFC_1812_CHECKS
1236 /* Check to make sure the packet is valid (RFC1812) */
1237 if (is_valid_ipv4_pkt(ipv4_hdr, m->pkt_len) < 0) {
1238 rte_pktmbuf_free(m);
1243 dst_port = get_ipv4_dst_port(ipv4_hdr, portid,
1244 RTE_PER_LCORE(lcore_conf)->ipv4_lookup_struct);
1245 if (dst_port >= RTE_MAX_ETHPORTS ||
1246 (enabled_port_mask & 1 << dst_port) == 0)
1249 #ifdef DO_RFC_1812_CHECKS
1250 /* Update time to live and header checksum */
1251 --(ipv4_hdr->time_to_live);
1252 ++(ipv4_hdr->hdr_checksum);
1255 *(uint64_t *)ð_hdr->d_addr = dest_eth_addr[dst_port];
1258 ether_addr_copy(&ports_eth_addr[dst_port], ð_hdr->s_addr);
1260 send_single_packet(m, dst_port);
1261 } else if (RTE_ETH_IS_IPV6_HDR(m->packet_type)) {
1262 /* Handle IPv6 headers.*/
1263 struct ipv6_hdr *ipv6_hdr;
1265 ipv6_hdr = rte_pktmbuf_mtod_offset(m, struct ipv6_hdr *,
1266 sizeof(struct ether_hdr));
1268 dst_port = get_ipv6_dst_port(ipv6_hdr, portid,
1269 RTE_PER_LCORE(lcore_conf)->ipv6_lookup_struct);
1271 if (dst_port >= RTE_MAX_ETHPORTS ||
1272 (enabled_port_mask & 1 << dst_port) == 0)
1276 *(uint64_t *)ð_hdr->d_addr = dest_eth_addr[dst_port];
1279 ether_addr_copy(&ports_eth_addr[dst_port], ð_hdr->s_addr);
1281 send_single_packet(m, dst_port);
1283 /* Free the mbuf that contains non-IPV4/IPV6 packet */
1284 rte_pktmbuf_free(m);
1287 #if ((APP_LOOKUP_METHOD == APP_LOOKUP_LPM) && \
1288 (ENABLE_MULTI_BUFFER_OPTIMIZE == 1))
1289 #ifdef DO_RFC_1812_CHECKS
1291 #define IPV4_MIN_VER_IHL 0x45
1292 #define IPV4_MAX_VER_IHL 0x4f
1293 #define IPV4_MAX_VER_IHL_DIFF (IPV4_MAX_VER_IHL - IPV4_MIN_VER_IHL)
1295 /* Minimum value of IPV4 total length (20B) in network byte order. */
1296 #define IPV4_MIN_LEN_BE (sizeof(struct ipv4_hdr) << 8)
1299 * From http://www.rfc-editor.org/rfc/rfc1812.txt section 5.2.2:
1300 * - The IP version number must be 4.
1301 * - The IP header length field must be large enough to hold the
1302 * minimum length legal IP datagram (20 bytes = 5 words).
1303 * - The IP total length field must be large enough to hold the IP
1304 * datagram header, whose length is specified in the IP header length
1306 * If we encounter invalid IPV4 packet, then set destination port for it
1307 * to BAD_PORT value.
1309 static inline __attribute__((always_inline)) void
1310 rfc1812_process(struct ipv4_hdr *ipv4_hdr, uint16_t *dp, uint32_t ptype)
1314 if (RTE_ETH_IS_IPV4_HDR(ptype)) {
1315 ihl = ipv4_hdr->version_ihl - IPV4_MIN_VER_IHL;
1317 ipv4_hdr->time_to_live--;
1318 ipv4_hdr->hdr_checksum++;
1320 if (ihl > IPV4_MAX_VER_IHL_DIFF ||
1321 ((uint8_t)ipv4_hdr->total_length == 0 &&
1322 ipv4_hdr->total_length < IPV4_MIN_LEN_BE)) {
1329 #define rfc1812_process(mb, dp, ptype) do { } while (0)
1330 #endif /* DO_RFC_1812_CHECKS */
1331 #endif /* APP_LOOKUP_LPM && ENABLE_MULTI_BUFFER_OPTIMIZE */
1334 #if ((APP_LOOKUP_METHOD == APP_LOOKUP_LPM) && \
1335 (ENABLE_MULTI_BUFFER_OPTIMIZE == 1))
1337 static inline __attribute__((always_inline)) uint16_t
1338 get_dst_port(struct rte_mbuf *pkt, uint32_t dst_ipv4, uint8_t portid)
1340 uint32_t next_hop_ipv4;
1341 uint8_t next_hop_ipv6;
1342 struct ipv6_hdr *ipv6_hdr;
1343 struct ether_hdr *eth_hdr;
1345 if (RTE_ETH_IS_IPV4_HDR(pkt->packet_type)) {
1346 return (uint16_t) ((rte_lpm_lookup(
1347 RTE_PER_LCORE(lcore_conf)->ipv4_lookup_struct, dst_ipv4,
1348 &next_hop_ipv4) == 0) ? next_hop_ipv4 : portid);
1350 } else if (RTE_ETH_IS_IPV6_HDR(pkt->packet_type)) {
1352 eth_hdr = rte_pktmbuf_mtod(pkt, struct ether_hdr *);
1353 ipv6_hdr = (struct ipv6_hdr *)(eth_hdr + 1);
1355 return (uint16_t) ((rte_lpm6_lookup(
1356 RTE_PER_LCORE(lcore_conf)->ipv6_lookup_struct,
1357 ipv6_hdr->dst_addr, &next_hop_ipv6) == 0) ? next_hop_ipv6 :
1366 process_packet(struct rte_mbuf *pkt, uint16_t *dst_port, uint8_t portid)
1368 struct ether_hdr *eth_hdr;
1369 struct ipv4_hdr *ipv4_hdr;
1374 eth_hdr = rte_pktmbuf_mtod(pkt, struct ether_hdr *);
1375 ipv4_hdr = (struct ipv4_hdr *)(eth_hdr + 1);
1377 dst_ipv4 = ipv4_hdr->dst_addr;
1378 dst_ipv4 = rte_be_to_cpu_32(dst_ipv4);
1379 dp = get_dst_port(pkt, dst_ipv4, portid);
1381 te = _mm_load_si128((__m128i *)eth_hdr);
1385 rfc1812_process(ipv4_hdr, dst_port, pkt->packet_type);
1387 te = _mm_blend_epi16(te, ve, MASK_ETH);
1388 _mm_store_si128((__m128i *)eth_hdr, te);
1392 * Read packet_type and destination IPV4 addresses from 4 mbufs.
1395 processx4_step1(struct rte_mbuf *pkt[FWDSTEP],
1397 uint32_t *ipv4_flag)
1399 struct ipv4_hdr *ipv4_hdr;
1400 struct ether_hdr *eth_hdr;
1401 uint32_t x0, x1, x2, x3;
1403 eth_hdr = rte_pktmbuf_mtod(pkt[0], struct ether_hdr *);
1404 ipv4_hdr = (struct ipv4_hdr *)(eth_hdr + 1);
1405 x0 = ipv4_hdr->dst_addr;
1406 ipv4_flag[0] = pkt[0]->packet_type & RTE_PTYPE_L3_IPV4;
1408 eth_hdr = rte_pktmbuf_mtod(pkt[1], struct ether_hdr *);
1409 ipv4_hdr = (struct ipv4_hdr *)(eth_hdr + 1);
1410 x1 = ipv4_hdr->dst_addr;
1411 ipv4_flag[0] &= pkt[1]->packet_type;
1413 eth_hdr = rte_pktmbuf_mtod(pkt[2], struct ether_hdr *);
1414 ipv4_hdr = (struct ipv4_hdr *)(eth_hdr + 1);
1415 x2 = ipv4_hdr->dst_addr;
1416 ipv4_flag[0] &= pkt[2]->packet_type;
1418 eth_hdr = rte_pktmbuf_mtod(pkt[3], struct ether_hdr *);
1419 ipv4_hdr = (struct ipv4_hdr *)(eth_hdr + 1);
1420 x3 = ipv4_hdr->dst_addr;
1421 ipv4_flag[0] &= pkt[3]->packet_type;
1423 dip[0] = _mm_set_epi32(x3, x2, x1, x0);
1427 * Lookup into LPM for destination port.
1428 * If lookup fails, use incoming port (portid) as destination port.
1431 processx4_step2(__m128i dip,
1434 struct rte_mbuf *pkt[FWDSTEP],
1435 uint16_t dprt[FWDSTEP])
1438 const __m128i bswap_mask = _mm_set_epi8(12, 13, 14, 15, 8, 9, 10, 11,
1439 4, 5, 6, 7, 0, 1, 2, 3);
1441 /* Byte swap 4 IPV4 addresses. */
1442 dip = _mm_shuffle_epi8(dip, bswap_mask);
1444 /* if all 4 packets are IPV4. */
1445 if (likely(ipv4_flag)) {
1446 rte_lpm_lookupx4(RTE_PER_LCORE(lcore_conf)->ipv4_lookup_struct, dip,
1449 /* get rid of unused upper 16 bit for each dport. */
1450 dst.x = _mm_packs_epi32(dst.x, dst.x);
1451 *(uint64_t *)dprt = dst.u64[0];
1454 dprt[0] = get_dst_port(pkt[0], dst.u32[0], portid);
1455 dprt[1] = get_dst_port(pkt[1], dst.u32[1], portid);
1456 dprt[2] = get_dst_port(pkt[2], dst.u32[2], portid);
1457 dprt[3] = get_dst_port(pkt[3], dst.u32[3], portid);
1462 * Update source and destination MAC addresses in the ethernet header.
1463 * Perform RFC1812 checks and updates for IPV4 packets.
1466 processx4_step3(struct rte_mbuf *pkt[FWDSTEP], uint16_t dst_port[FWDSTEP])
1468 __m128i te[FWDSTEP];
1469 __m128i ve[FWDSTEP];
1470 __m128i *p[FWDSTEP];
1472 p[0] = rte_pktmbuf_mtod(pkt[0], __m128i *);
1473 p[1] = rte_pktmbuf_mtod(pkt[1], __m128i *);
1474 p[2] = rte_pktmbuf_mtod(pkt[2], __m128i *);
1475 p[3] = rte_pktmbuf_mtod(pkt[3], __m128i *);
1477 ve[0] = val_eth[dst_port[0]];
1478 te[0] = _mm_load_si128(p[0]);
1480 ve[1] = val_eth[dst_port[1]];
1481 te[1] = _mm_load_si128(p[1]);
1483 ve[2] = val_eth[dst_port[2]];
1484 te[2] = _mm_load_si128(p[2]);
1486 ve[3] = val_eth[dst_port[3]];
1487 te[3] = _mm_load_si128(p[3]);
1489 /* Update first 12 bytes, keep rest bytes intact. */
1490 te[0] = _mm_blend_epi16(te[0], ve[0], MASK_ETH);
1491 te[1] = _mm_blend_epi16(te[1], ve[1], MASK_ETH);
1492 te[2] = _mm_blend_epi16(te[2], ve[2], MASK_ETH);
1493 te[3] = _mm_blend_epi16(te[3], ve[3], MASK_ETH);
1495 _mm_store_si128(p[0], te[0]);
1496 _mm_store_si128(p[1], te[1]);
1497 _mm_store_si128(p[2], te[2]);
1498 _mm_store_si128(p[3], te[3]);
1500 rfc1812_process((struct ipv4_hdr *)((struct ether_hdr *)p[0] + 1),
1501 &dst_port[0], pkt[0]->packet_type);
1502 rfc1812_process((struct ipv4_hdr *)((struct ether_hdr *)p[1] + 1),
1503 &dst_port[1], pkt[1]->packet_type);
1504 rfc1812_process((struct ipv4_hdr *)((struct ether_hdr *)p[2] + 1),
1505 &dst_port[2], pkt[2]->packet_type);
1506 rfc1812_process((struct ipv4_hdr *)((struct ether_hdr *)p[3] + 1),
1507 &dst_port[3], pkt[3]->packet_type);
1511 * We group consecutive packets with the same destionation port into one burst.
1512 * To avoid extra latency this is done together with some other packet
1513 * processing, but after we made a final decision about packet's destination.
1514 * To do this we maintain:
1515 * pnum - array of number of consecutive packets with the same dest port for
1516 * each packet in the input burst.
1517 * lp - pointer to the last updated element in the pnum.
1518 * dlp - dest port value lp corresponds to.
1521 #define GRPSZ (1 << FWDSTEP)
1522 #define GRPMSK (GRPSZ - 1)
1524 #define GROUP_PORT_STEP(dlp, dcp, lp, pn, idx) do { \
1525 if (likely((dlp) == (dcp)[(idx)])) { \
1528 (dlp) = (dcp)[idx]; \
1529 (lp) = (pn) + (idx); \
1535 * Group consecutive packets with the same destination port in bursts of 4.
1536 * Suppose we have array of destionation ports:
1537 * dst_port[] = {a, b, c, d,, e, ... }
1538 * dp1 should contain: <a, b, c, d>, dp2: <b, c, d, e>.
1539 * We doing 4 comparisions at once and the result is 4 bit mask.
1540 * This mask is used as an index into prebuild array of pnum values.
1542 static inline uint16_t *
1543 port_groupx4(uint16_t pn[FWDSTEP + 1], uint16_t *lp, __m128i dp1, __m128i dp2)
1545 static const struct {
1546 uint64_t pnum; /* prebuild 4 values for pnum[]. */
1547 int32_t idx; /* index for new last updated elemnet. */
1548 uint16_t lpv; /* add value to the last updated element. */
1551 /* 0: a != b, b != c, c != d, d != e */
1552 .pnum = UINT64_C(0x0001000100010001),
1557 /* 1: a == b, b != c, c != d, d != e */
1558 .pnum = UINT64_C(0x0001000100010002),
1563 /* 2: a != b, b == c, c != d, d != e */
1564 .pnum = UINT64_C(0x0001000100020001),
1569 /* 3: a == b, b == c, c != d, d != e */
1570 .pnum = UINT64_C(0x0001000100020003),
1575 /* 4: a != b, b != c, c == d, d != e */
1576 .pnum = UINT64_C(0x0001000200010001),
1581 /* 5: a == b, b != c, c == d, d != e */
1582 .pnum = UINT64_C(0x0001000200010002),
1587 /* 6: a != b, b == c, c == d, d != e */
1588 .pnum = UINT64_C(0x0001000200030001),
1593 /* 7: a == b, b == c, c == d, d != e */
1594 .pnum = UINT64_C(0x0001000200030004),
1599 /* 8: a != b, b != c, c != d, d == e */
1600 .pnum = UINT64_C(0x0002000100010001),
1605 /* 9: a == b, b != c, c != d, d == e */
1606 .pnum = UINT64_C(0x0002000100010002),
1611 /* 0xa: a != b, b == c, c != d, d == e */
1612 .pnum = UINT64_C(0x0002000100020001),
1617 /* 0xb: a == b, b == c, c != d, d == e */
1618 .pnum = UINT64_C(0x0002000100020003),
1623 /* 0xc: a != b, b != c, c == d, d == e */
1624 .pnum = UINT64_C(0x0002000300010001),
1629 /* 0xd: a == b, b != c, c == d, d == e */
1630 .pnum = UINT64_C(0x0002000300010002),
1635 /* 0xe: a != b, b == c, c == d, d == e */
1636 .pnum = UINT64_C(0x0002000300040001),
1641 /* 0xf: a == b, b == c, c == d, d == e */
1642 .pnum = UINT64_C(0x0002000300040005),
1649 uint16_t u16[FWDSTEP + 1];
1651 } *pnum = (void *)pn;
1655 dp1 = _mm_cmpeq_epi16(dp1, dp2);
1656 dp1 = _mm_unpacklo_epi16(dp1, dp1);
1657 v = _mm_movemask_ps((__m128)dp1);
1659 /* update last port counter. */
1660 lp[0] += gptbl[v].lpv;
1662 /* if dest port value has changed. */
1664 pnum->u64 = gptbl[v].pnum;
1665 pnum->u16[FWDSTEP] = 1;
1666 lp = pnum->u16 + gptbl[v].idx;
1672 #endif /* APP_LOOKUP_METHOD */
1675 process_burst(struct rte_mbuf *pkts_burst[MAX_PKT_BURST], int nb_rx,
1680 #if ((APP_LOOKUP_METHOD == APP_LOOKUP_LPM) && \
1681 (ENABLE_MULTI_BUFFER_OPTIMIZE == 1))
1685 uint16_t dst_port[MAX_PKT_BURST];
1686 __m128i dip[MAX_PKT_BURST / FWDSTEP];
1687 uint32_t ipv4_flag[MAX_PKT_BURST / FWDSTEP];
1688 uint16_t pnum[MAX_PKT_BURST + 1];
1692 #if (ENABLE_MULTI_BUFFER_OPTIMIZE == 1)
1693 #if (APP_LOOKUP_METHOD == APP_LOOKUP_EXACT_MATCH)
1696 * Send nb_rx - nb_rx%8 packets
1699 int32_t n = RTE_ALIGN_FLOOR(nb_rx, 8);
1701 for (j = 0; j < n; j += 8) {
1703 pkts_burst[j]->packet_type &
1704 pkts_burst[j+1]->packet_type &
1705 pkts_burst[j+2]->packet_type &
1706 pkts_burst[j+3]->packet_type &
1707 pkts_burst[j+4]->packet_type &
1708 pkts_burst[j+5]->packet_type &
1709 pkts_burst[j+6]->packet_type &
1710 pkts_burst[j+7]->packet_type;
1711 if (pkt_type & RTE_PTYPE_L3_IPV4) {
1712 simple_ipv4_fwd_8pkts(&pkts_burst[j], portid);
1713 } else if (pkt_type &
1714 RTE_PTYPE_L3_IPV6) {
1715 simple_ipv6_fwd_8pkts(&pkts_burst[j], portid);
1717 l3fwd_simple_forward(pkts_burst[j], portid);
1718 l3fwd_simple_forward(pkts_burst[j+1], portid);
1719 l3fwd_simple_forward(pkts_burst[j+2], portid);
1720 l3fwd_simple_forward(pkts_burst[j+3], portid);
1721 l3fwd_simple_forward(pkts_burst[j+4], portid);
1722 l3fwd_simple_forward(pkts_burst[j+5], portid);
1723 l3fwd_simple_forward(pkts_burst[j+6], portid);
1724 l3fwd_simple_forward(pkts_burst[j+7], portid);
1727 for (; j < nb_rx ; j++)
1728 l3fwd_simple_forward(pkts_burst[j], portid);
1730 #elif (APP_LOOKUP_METHOD == APP_LOOKUP_LPM)
1732 k = RTE_ALIGN_FLOOR(nb_rx, FWDSTEP);
1733 for (j = 0; j != k; j += FWDSTEP)
1734 processx4_step1(&pkts_burst[j], &dip[j / FWDSTEP],
1735 &ipv4_flag[j / FWDSTEP]);
1737 k = RTE_ALIGN_FLOOR(nb_rx, FWDSTEP);
1738 for (j = 0; j != k; j += FWDSTEP)
1739 processx4_step2(dip[j / FWDSTEP], ipv4_flag[j / FWDSTEP],
1740 portid, &pkts_burst[j], &dst_port[j]);
1743 * Finish packet processing and group consecutive
1744 * packets with the same destination port.
1746 k = RTE_ALIGN_FLOOR(nb_rx, FWDSTEP);
1753 processx4_step3(pkts_burst, dst_port);
1755 /* dp1: <d[0], d[1], d[2], d[3], ... > */
1756 dp1 = _mm_loadu_si128((__m128i *)dst_port);
1758 for (j = FWDSTEP; j != k; j += FWDSTEP) {
1759 processx4_step3(&pkts_burst[j], &dst_port[j]);
1763 * <d[j-3], d[j-2], d[j-1], d[j], ... >
1765 dp2 = _mm_loadu_si128(
1766 (__m128i *)&dst_port[j - FWDSTEP + 1]);
1767 lp = port_groupx4(&pnum[j - FWDSTEP], lp, dp1, dp2);
1771 * <d[j], d[j+1], d[j+2], d[j+3], ... >
1773 dp1 = _mm_srli_si128(dp2, (FWDSTEP - 1) *
1774 sizeof(dst_port[0]));
1778 * dp2: <d[j-3], d[j-2], d[j-1], d[j-1], ... >
1780 dp2 = _mm_shufflelo_epi16(dp1, 0xf9);
1781 lp = port_groupx4(&pnum[j - FWDSTEP], lp, dp1, dp2);
1784 * remove values added by the last repeated
1788 dlp = dst_port[j - 1];
1790 /* set dlp and lp to the never used values. */
1792 lp = pnum + MAX_PKT_BURST;
1795 /* Process up to last 3 packets one by one. */
1796 switch (nb_rx % FWDSTEP) {
1798 process_packet(pkts_burst[j], dst_port + j, portid);
1799 GROUP_PORT_STEP(dlp, dst_port, lp, pnum, j);
1802 process_packet(pkts_burst[j], dst_port + j, portid);
1803 GROUP_PORT_STEP(dlp, dst_port, lp, pnum, j);
1806 process_packet(pkts_burst[j], dst_port + j, portid);
1807 GROUP_PORT_STEP(dlp, dst_port, lp, pnum, j);
1812 * Send packets out, through destination port.
1813 * Consecuteve pacekts with the same destination port
1814 * are already grouped together.
1815 * If destination port for the packet equals BAD_PORT,
1816 * then free the packet without sending it out.
1818 for (j = 0; j < nb_rx; j += k) {
1826 if (likely(pn != BAD_PORT))
1827 send_packetsx4(pn, pkts_burst + j, k);
1829 for (m = j; m != j + k; m++)
1830 rte_pktmbuf_free(pkts_burst[m]);
1834 #endif /* APP_LOOKUP_METHOD */
1835 #else /* ENABLE_MULTI_BUFFER_OPTIMIZE == 0 */
1837 /* Prefetch first packets */
1838 for (j = 0; j < PREFETCH_OFFSET && j < nb_rx; j++)
1839 rte_prefetch0(rte_pktmbuf_mtod(pkts_burst[j], void *));
1841 /* Prefetch and forward already prefetched packets */
1842 for (j = 0; j < (nb_rx - PREFETCH_OFFSET); j++) {
1843 rte_prefetch0(rte_pktmbuf_mtod(pkts_burst[
1844 j + PREFETCH_OFFSET], void *));
1845 l3fwd_simple_forward(pkts_burst[j], portid);
1848 /* Forward remaining prefetched packets */
1849 for (; j < nb_rx; j++)
1850 l3fwd_simple_forward(pkts_burst[j], portid);
1852 #endif /* ENABLE_MULTI_BUFFER_OPTIMIZE */
1856 #if (APP_CPU_LOAD > 0)
1859 * CPU-load stats collector
1862 cpu_load_collector(__rte_unused void *arg) {
1865 uint64_t prev_tsc, diff_tsc, cur_tsc;
1866 uint64_t total[MAX_CPU] = { 0 };
1867 unsigned min_cpu = MAX_CPU;
1868 unsigned max_cpu = 0;
1873 unsigned int n_thread_per_cpu[MAX_CPU] = { 0 };
1874 struct thread_conf *thread_per_cpu[MAX_CPU][MAX_THREAD];
1876 struct thread_conf *thread_conf;
1878 const uint64_t interval_tsc = (rte_get_tsc_hz() + US_PER_S - 1) /
1879 US_PER_S * CPU_LOAD_TIMEOUT_US;
1883 * Wait for all threads
1886 printf("Waiting for %d rx threads and %d tx threads\n", n_rx_thread,
1889 while (rte_atomic16_read(&rx_counter) < n_rx_thread)
1892 while (rte_atomic16_read(&tx_counter) < n_tx_thread)
1895 for (i = 0; i < n_rx_thread; i++) {
1897 thread_conf = &rx_thread[i].conf;
1898 cpu_id = thread_conf->cpu_id;
1899 thread_per_cpu[cpu_id][n_thread_per_cpu[cpu_id]++] = thread_conf;
1901 if (cpu_id > max_cpu)
1903 if (cpu_id < min_cpu)
1906 for (i = 0; i < n_tx_thread; i++) {
1908 thread_conf = &tx_thread[i].conf;
1909 cpu_id = thread_conf->cpu_id;
1910 thread_per_cpu[cpu_id][n_thread_per_cpu[cpu_id]++] = thread_conf;
1912 if (thread_conf->cpu_id > max_cpu)
1913 max_cpu = thread_conf->cpu_id;
1914 if (thread_conf->cpu_id < min_cpu)
1915 min_cpu = thread_conf->cpu_id;
1921 for (i = min_cpu; i <= max_cpu; i++) {
1922 for (j = 0; j < MAX_CPU_COUNTER; j++) {
1923 for (k = 0; k < n_thread_per_cpu[i]; k++)
1924 if (thread_per_cpu[i][k]->busy[j]) {
1929 cpu_load.hits[j][i]++;
1941 cur_tsc = rte_rdtsc();
1943 diff_tsc = cur_tsc - prev_tsc;
1944 if (unlikely(diff_tsc > interval_tsc)) {
1948 printf("Cpu usage for %d rx threads and %d tx threads:\n\n",
1949 n_rx_thread, n_tx_thread);
1951 printf("cpu# proc%% poll%% overhead%%\n\n");
1953 for (i = min_cpu; i <= max_cpu; i++) {
1955 printf("CPU %d:", i);
1956 for (j = 0; j < MAX_CPU_COUNTER; j++) {
1957 printf("%7" PRIu64 "",
1958 cpu_load.hits[j][i] * 100 / cpu_load.counter);
1959 hits += cpu_load.hits[j][i];
1960 cpu_load.hits[j][i] = 0;
1962 printf("%7" PRIu64 "\n",
1963 100 - total[i] * 100 / cpu_load.counter);
1966 cpu_load.counter = 0;
1973 #endif /* APP_CPU_LOAD */
1976 * Null processing lthread loop
1978 * This loop is used to start empty scheduler on lcore.
1981 lthread_null(__rte_unused void *args)
1983 int lcore_id = rte_lcore_id();
1985 RTE_LOG(INFO, L3FWD, "Starting scheduler on lcore %d.\n", lcore_id);
1989 /* main processing loop */
1991 lthread_tx_per_ring(void *dummy)
1995 struct rte_ring *ring;
1996 struct thread_tx_conf *tx_conf;
1997 struct rte_mbuf *pkts_burst[MAX_PKT_BURST];
1998 struct lthread_cond *ready;
2000 tx_conf = (struct thread_tx_conf *)dummy;
2001 ring = tx_conf->ring;
2002 ready = *tx_conf->ready;
2004 lthread_set_data((void *)tx_conf);
2007 * Move this lthread to lcore
2009 lthread_set_affinity(tx_conf->conf.lcore_id);
2011 RTE_LOG(INFO, L3FWD, "entering main tx loop on lcore %u\n", rte_lcore_id());
2014 rte_atomic16_inc(&tx_counter);
2018 * Read packet from ring
2020 SET_CPU_BUSY(tx_conf, CPU_POLL);
2021 nb_rx = rte_ring_sc_dequeue_burst(ring, (void **)pkts_burst,
2023 SET_CPU_IDLE(tx_conf, CPU_POLL);
2026 SET_CPU_BUSY(tx_conf, CPU_PROCESS);
2027 portid = pkts_burst[0]->port;
2028 process_burst(pkts_burst, nb_rx, portid);
2029 SET_CPU_IDLE(tx_conf, CPU_PROCESS);
2032 lthread_cond_wait(ready, 0);
2038 * Main tx-lthreads spawner lthread.
2040 * This lthread is used to spawn one new lthread per ring from producers.
2044 lthread_tx(void *args)
2050 struct thread_tx_conf *tx_conf;
2052 tx_conf = (struct thread_tx_conf *)args;
2053 lthread_set_data((void *)tx_conf);
2056 * Move this lthread to the selected lcore
2058 lthread_set_affinity(tx_conf->conf.lcore_id);
2061 * Spawn tx readers (one per input ring)
2063 lthread_create(<, tx_conf->conf.lcore_id, lthread_tx_per_ring,
2066 lcore_id = rte_lcore_id();
2068 RTE_LOG(INFO, L3FWD, "Entering Tx main loop on lcore %u\n", lcore_id);
2070 tx_conf->conf.cpu_id = sched_getcpu();
2073 lthread_sleep(BURST_TX_DRAIN_US * 1000);
2076 * TX burst queue drain
2078 for (portid = 0; portid < RTE_MAX_ETHPORTS; portid++) {
2079 if (tx_conf->tx_mbufs[portid].len == 0)
2081 SET_CPU_BUSY(tx_conf, CPU_PROCESS);
2082 send_burst(tx_conf, tx_conf->tx_mbufs[portid].len, portid);
2083 SET_CPU_IDLE(tx_conf, CPU_PROCESS);
2084 tx_conf->tx_mbufs[portid].len = 0;
2091 lthread_rx(void *dummy)
2096 uint8_t portid, queueid;
2098 int len[RTE_MAX_LCORE] = { 0 };
2099 int old_len, new_len;
2100 struct rte_mbuf *pkts_burst[MAX_PKT_BURST];
2101 struct thread_rx_conf *rx_conf;
2103 rx_conf = (struct thread_rx_conf *)dummy;
2104 lthread_set_data((void *)rx_conf);
2107 * Move this lthread to lcore
2109 lthread_set_affinity(rx_conf->conf.lcore_id);
2111 if (rx_conf->n_rx_queue == 0) {
2112 RTE_LOG(INFO, L3FWD, "lcore %u has nothing to do\n", rte_lcore_id());
2116 RTE_LOG(INFO, L3FWD, "Entering main Rx loop on lcore %u\n", rte_lcore_id());
2118 for (i = 0; i < rx_conf->n_rx_queue; i++) {
2120 portid = rx_conf->rx_queue_list[i].port_id;
2121 queueid = rx_conf->rx_queue_list[i].queue_id;
2122 RTE_LOG(INFO, L3FWD, " -- lcoreid=%u portid=%hhu rxqueueid=%hhu\n",
2123 rte_lcore_id(), portid, queueid);
2127 * Init all condition variables (one per rx thread)
2129 for (i = 0; i < rx_conf->n_rx_queue; i++)
2130 lthread_cond_init(NULL, &rx_conf->ready[i], NULL);
2134 rx_conf->conf.cpu_id = sched_getcpu();
2135 rte_atomic16_inc(&rx_counter);
2139 * Read packet from RX queues
2141 for (i = 0; i < rx_conf->n_rx_queue; ++i) {
2142 portid = rx_conf->rx_queue_list[i].port_id;
2143 queueid = rx_conf->rx_queue_list[i].queue_id;
2145 SET_CPU_BUSY(rx_conf, CPU_POLL);
2146 nb_rx = rte_eth_rx_burst(portid, queueid, pkts_burst,
2148 SET_CPU_IDLE(rx_conf, CPU_POLL);
2151 worker_id = (worker_id + 1) % rx_conf->n_ring;
2152 old_len = len[worker_id];
2154 SET_CPU_BUSY(rx_conf, CPU_PROCESS);
2155 ret = rte_ring_sp_enqueue_burst(
2156 rx_conf->ring[worker_id],
2157 (void **) pkts_burst,
2160 new_len = old_len + ret;
2162 if (new_len >= BURST_SIZE) {
2163 lthread_cond_signal(rx_conf->ready[worker_id]);
2167 len[worker_id] = new_len;
2169 if (unlikely(ret < nb_rx)) {
2172 for (k = ret; k < nb_rx; k++) {
2173 struct rte_mbuf *m = pkts_burst[k];
2175 rte_pktmbuf_free(m);
2178 SET_CPU_IDLE(rx_conf, CPU_PROCESS);
2187 * Start scheduler with initial lthread on lcore
2189 * This lthread loop spawns all rx and tx lthreads on master lcore
2193 lthread_spawner(__rte_unused void *arg) {
2194 struct lthread *lt[MAX_THREAD];
2198 printf("Entering lthread_spawner\n");
2201 * Create producers (rx threads) on default lcore
2203 for (i = 0; i < n_rx_thread; i++) {
2204 rx_thread[i].conf.thread_id = i;
2205 lthread_create(<[n_thread], -1, lthread_rx,
2206 (void *)&rx_thread[i]);
2211 * Wait for all producers. Until some producers can be started on the same
2212 * scheduler as this lthread, yielding is required to let them to run and
2213 * prevent deadlock here.
2215 while (rte_atomic16_read(&rx_counter) < n_rx_thread)
2216 lthread_sleep(100000);
2219 * Create consumers (tx threads) on default lcore_id
2221 for (i = 0; i < n_tx_thread; i++) {
2222 tx_thread[i].conf.thread_id = i;
2223 lthread_create(<[n_thread], -1, lthread_tx,
2224 (void *)&tx_thread[i]);
2229 * Wait for all threads finished
2231 for (i = 0; i < n_thread; i++)
2232 lthread_join(lt[i], NULL);
2237 * Start master scheduler with initial lthread spawning rx and tx lthreads
2238 * (main_lthread_master).
2241 lthread_master_spawner(__rte_unused void *arg) {
2243 int lcore_id = rte_lcore_id();
2245 RTE_PER_LCORE(lcore_conf) = &lcore_conf[lcore_id];
2246 lthread_create(<, -1, lthread_spawner, NULL);
2253 * Start scheduler on lcore.
2256 sched_spawner(__rte_unused void *arg) {
2258 int lcore_id = rte_lcore_id();
2261 if (lcore_id == cpu_load_lcore_id) {
2262 cpu_load_collector(arg);
2265 #endif /* APP_CPU_LOAD */
2267 RTE_PER_LCORE(lcore_conf) = &lcore_conf[lcore_id];
2268 lthread_create(<, -1, lthread_null, NULL);
2274 /* main processing loop */
2276 pthread_tx(void *dummy)
2278 struct rte_mbuf *pkts_burst[MAX_PKT_BURST];
2279 uint64_t prev_tsc, diff_tsc, cur_tsc;
2282 struct thread_tx_conf *tx_conf;
2284 const uint64_t drain_tsc = (rte_get_tsc_hz() + US_PER_S - 1) /
2285 US_PER_S * BURST_TX_DRAIN_US;
2289 tx_conf = (struct thread_tx_conf *)dummy;
2291 RTE_LOG(INFO, L3FWD, "Entering main Tx loop on lcore %u\n", rte_lcore_id());
2293 tx_conf->conf.cpu_id = sched_getcpu();
2294 rte_atomic16_inc(&tx_counter);
2297 cur_tsc = rte_rdtsc();
2300 * TX burst queue drain
2302 diff_tsc = cur_tsc - prev_tsc;
2303 if (unlikely(diff_tsc > drain_tsc)) {
2306 * This could be optimized (use queueid instead of
2307 * portid), but it is not called so often
2309 SET_CPU_BUSY(tx_conf, CPU_PROCESS);
2310 for (portid = 0; portid < RTE_MAX_ETHPORTS; portid++) {
2311 if (tx_conf->tx_mbufs[portid].len == 0)
2313 send_burst(tx_conf, tx_conf->tx_mbufs[portid].len, portid);
2314 tx_conf->tx_mbufs[portid].len = 0;
2316 SET_CPU_IDLE(tx_conf, CPU_PROCESS);
2322 * Read packet from ring
2324 SET_CPU_BUSY(tx_conf, CPU_POLL);
2325 nb_rx = rte_ring_sc_dequeue_burst(tx_conf->ring,
2326 (void **)pkts_burst, MAX_PKT_BURST);
2327 SET_CPU_IDLE(tx_conf, CPU_POLL);
2329 if (unlikely(nb_rx == 0)) {
2334 SET_CPU_BUSY(tx_conf, CPU_PROCESS);
2335 portid = pkts_burst[0]->port;
2336 process_burst(pkts_burst, nb_rx, portid);
2337 SET_CPU_IDLE(tx_conf, CPU_PROCESS);
2343 pthread_rx(void *dummy)
2350 uint8_t portid, queueid;
2351 struct rte_mbuf *pkts_burst[MAX_PKT_BURST];
2353 struct thread_rx_conf *rx_conf;
2355 lcore_id = rte_lcore_id();
2356 rx_conf = (struct thread_rx_conf *)dummy;
2358 if (rx_conf->n_rx_queue == 0) {
2359 RTE_LOG(INFO, L3FWD, "lcore %u has nothing to do\n", lcore_id);
2363 RTE_LOG(INFO, L3FWD, "entering main rx loop on lcore %u\n", lcore_id);
2365 for (i = 0; i < rx_conf->n_rx_queue; i++) {
2367 portid = rx_conf->rx_queue_list[i].port_id;
2368 queueid = rx_conf->rx_queue_list[i].queue_id;
2369 RTE_LOG(INFO, L3FWD, " -- lcoreid=%u portid=%hhu rxqueueid=%hhu\n",
2370 lcore_id, portid, queueid);
2374 rx_conf->conf.cpu_id = sched_getcpu();
2375 rte_atomic16_inc(&rx_counter);
2379 * Read packet from RX queues
2381 for (i = 0; i < rx_conf->n_rx_queue; ++i) {
2382 portid = rx_conf->rx_queue_list[i].port_id;
2383 queueid = rx_conf->rx_queue_list[i].queue_id;
2385 SET_CPU_BUSY(rx_conf, CPU_POLL);
2386 nb_rx = rte_eth_rx_burst(portid, queueid, pkts_burst,
2388 SET_CPU_IDLE(rx_conf, CPU_POLL);
2395 SET_CPU_BUSY(rx_conf, CPU_PROCESS);
2396 worker_id = (worker_id + 1) % rx_conf->n_ring;
2397 n = rte_ring_sp_enqueue_burst(rx_conf->ring[worker_id],
2398 (void **)pkts_burst, nb_rx);
2400 if (unlikely(n != nb_rx)) {
2403 for (k = n; k < nb_rx; k++) {
2404 struct rte_mbuf *m = pkts_burst[k];
2406 rte_pktmbuf_free(m);
2410 SET_CPU_IDLE(rx_conf, CPU_PROCESS);
2420 pthread_run(__rte_unused void *arg) {
2421 int lcore_id = rte_lcore_id();
2424 for (i = 0; i < n_rx_thread; i++)
2425 if (rx_thread[i].conf.lcore_id == lcore_id) {
2426 printf("Start rx thread on %d...\n", lcore_id);
2427 RTE_PER_LCORE(lcore_conf) = &lcore_conf[lcore_id];
2428 RTE_PER_LCORE(lcore_conf)->data = (void *)&rx_thread[i];
2429 pthread_rx((void *)&rx_thread[i]);
2433 for (i = 0; i < n_tx_thread; i++)
2434 if (tx_thread[i].conf.lcore_id == lcore_id) {
2435 printf("Start tx thread on %d...\n", lcore_id);
2436 RTE_PER_LCORE(lcore_conf) = &lcore_conf[lcore_id];
2437 RTE_PER_LCORE(lcore_conf)->data = (void *)&tx_thread[i];
2438 pthread_tx((void *)&tx_thread[i]);
2443 if (lcore_id == cpu_load_lcore_id)
2444 cpu_load_collector(arg);
2445 #endif /* APP_CPU_LOAD */
2451 check_lcore_params(void)
2453 uint8_t queue, lcore;
2457 for (i = 0; i < nb_rx_thread_params; ++i) {
2458 queue = rx_thread_params[i].queue_id;
2459 if (queue >= MAX_RX_QUEUE_PER_PORT) {
2460 printf("invalid queue number: %hhu\n", queue);
2463 lcore = rx_thread_params[i].lcore_id;
2464 if (!rte_lcore_is_enabled(lcore)) {
2465 printf("error: lcore %hhu is not enabled in lcore mask\n", lcore);
2468 socketid = rte_lcore_to_socket_id(lcore);
2469 if ((socketid != 0) && (numa_on == 0))
2470 printf("warning: lcore %hhu is on socket %d with numa off\n",
2477 check_port_config(const unsigned nb_ports)
2482 for (i = 0; i < nb_rx_thread_params; ++i) {
2483 portid = rx_thread_params[i].port_id;
2484 if ((enabled_port_mask & (1 << portid)) == 0) {
2485 printf("port %u is not enabled in port mask\n", portid);
2488 if (portid >= nb_ports) {
2489 printf("port %u is not present on the board\n", portid);
2497 get_port_n_rx_queues(const uint8_t port)
2502 for (i = 0; i < nb_rx_thread_params; ++i)
2503 if (rx_thread_params[i].port_id == port &&
2504 rx_thread_params[i].queue_id > queue)
2505 queue = rx_thread_params[i].queue_id;
2507 return (uint8_t)(++queue);
2514 struct thread_rx_conf *rx_conf;
2515 struct thread_tx_conf *tx_conf;
2516 unsigned rx_thread_id, tx_thread_id;
2518 struct rte_ring *ring = NULL;
2520 for (tx_thread_id = 0; tx_thread_id < n_tx_thread; tx_thread_id++) {
2522 tx_conf = &tx_thread[tx_thread_id];
2524 printf("Connecting tx-thread %d with rx-thread %d\n", tx_thread_id,
2525 tx_conf->conf.thread_id);
2527 rx_thread_id = tx_conf->conf.thread_id;
2528 if (rx_thread_id > n_tx_thread) {
2529 printf("connection from tx-thread %u to rx-thread %u fails "
2530 "(rx-thread not defined)\n", tx_thread_id, rx_thread_id);
2534 rx_conf = &rx_thread[rx_thread_id];
2535 socket_io = rte_lcore_to_socket_id(rx_conf->conf.lcore_id);
2537 snprintf(name, sizeof(name), "app_ring_s%u_rx%u_tx%u",
2538 socket_io, rx_thread_id, tx_thread_id);
2540 ring = rte_ring_create(name, 1024 * 4, socket_io,
2541 RING_F_SP_ENQ | RING_F_SC_DEQ);
2544 rte_panic("Cannot create ring to connect rx-thread %u "
2545 "with tx-thread %u\n", rx_thread_id, tx_thread_id);
2548 rx_conf->ring[rx_conf->n_ring] = ring;
2550 tx_conf->ring = ring;
2551 tx_conf->ready = &rx_conf->ready[rx_conf->n_ring];
2559 init_rx_queues(void)
2561 uint16_t i, nb_rx_queue;
2566 for (i = 0; i < nb_rx_thread_params; ++i) {
2567 thread = rx_thread_params[i].thread_id;
2568 nb_rx_queue = rx_thread[thread].n_rx_queue;
2570 if (nb_rx_queue >= MAX_RX_QUEUE_PER_LCORE) {
2571 printf("error: too many queues (%u) for thread: %u\n",
2572 (unsigned)nb_rx_queue + 1, (unsigned)thread);
2576 rx_thread[thread].conf.thread_id = thread;
2577 rx_thread[thread].conf.lcore_id = rx_thread_params[i].lcore_id;
2578 rx_thread[thread].rx_queue_list[nb_rx_queue].port_id =
2579 rx_thread_params[i].port_id;
2580 rx_thread[thread].rx_queue_list[nb_rx_queue].queue_id =
2581 rx_thread_params[i].queue_id;
2582 rx_thread[thread].n_rx_queue++;
2584 if (thread >= n_rx_thread)
2585 n_rx_thread = thread + 1;
2592 init_tx_threads(void)
2597 for (i = 0; i < nb_tx_thread_params; ++i) {
2598 tx_thread[n_tx_thread].conf.thread_id = tx_thread_params[i].thread_id;
2599 tx_thread[n_tx_thread].conf.lcore_id = tx_thread_params[i].lcore_id;
2607 print_usage(const char *prgname)
2609 printf("%s [EAL options] -- -p PORTMASK -P"
2610 " [--rx (port,queue,lcore,thread)[,(port,queue,lcore,thread]]"
2611 " [--tx (lcore,thread)[,(lcore,thread]]"
2612 " [--enable-jumbo [--max-pkt-len PKTLEN]]\n"
2613 " -p PORTMASK: hexadecimal bitmask of ports to configure\n"
2614 " -P : enable promiscuous mode\n"
2615 " --rx (port,queue,lcore,thread): rx queues configuration\n"
2616 " --tx (lcore,thread): tx threads configuration\n"
2617 " --stat-lcore LCORE: use lcore for stat collector\n"
2618 " --eth-dest=X,MM:MM:MM:MM:MM:MM: optional, ethernet destination for port X\n"
2619 " --no-numa: optional, disable numa awareness\n"
2620 " --ipv6: optional, specify it if running ipv6 packets\n"
2621 " --enable-jumbo: enable jumbo frame"
2622 " which max packet len is PKTLEN in decimal (64-9600)\n"
2623 " --hash-entry-num: specify the hash entry number in hexadecimal to be setup\n"
2624 " --no-lthreads: turn off lthread model\n",
2628 static int parse_max_pkt_len(const char *pktlen)
2633 /* parse decimal string */
2634 len = strtoul(pktlen, &end, 10);
2635 if ((pktlen[0] == '\0') || (end == NULL) || (*end != '\0'))
2645 parse_portmask(const char *portmask)
2650 /* parse hexadecimal string */
2651 pm = strtoul(portmask, &end, 16);
2652 if ((portmask[0] == '\0') || (end == NULL) || (*end != '\0'))
2661 #if (APP_LOOKUP_METHOD == APP_LOOKUP_EXACT_MATCH)
2663 parse_hash_entry_number(const char *hash_entry_num)
2666 unsigned long hash_en;
2668 /* parse hexadecimal string */
2669 hash_en = strtoul(hash_entry_num, &end, 16);
2670 if ((hash_entry_num[0] == '\0') || (end == NULL) || (*end != '\0'))
2681 parse_rx_config(const char *q_arg)
2684 const char *p, *p0 = q_arg;
2693 unsigned long int_fld[_NUM_FLD];
2694 char *str_fld[_NUM_FLD];
2698 nb_rx_thread_params = 0;
2700 while ((p = strchr(p0, '(')) != NULL) {
2702 p0 = strchr(p, ')');
2707 if (size >= sizeof(s))
2710 snprintf(s, sizeof(s), "%.*s", size, p);
2711 if (rte_strsplit(s, sizeof(s), str_fld, _NUM_FLD, ',') != _NUM_FLD)
2713 for (i = 0; i < _NUM_FLD; i++) {
2715 int_fld[i] = strtoul(str_fld[i], &end, 0);
2716 if (errno != 0 || end == str_fld[i] || int_fld[i] > 255)
2719 if (nb_rx_thread_params >= MAX_LCORE_PARAMS) {
2720 printf("exceeded max number of rx params: %hu\n",
2721 nb_rx_thread_params);
2724 rx_thread_params_array[nb_rx_thread_params].port_id =
2725 (uint8_t)int_fld[FLD_PORT];
2726 rx_thread_params_array[nb_rx_thread_params].queue_id =
2727 (uint8_t)int_fld[FLD_QUEUE];
2728 rx_thread_params_array[nb_rx_thread_params].lcore_id =
2729 (uint8_t)int_fld[FLD_LCORE];
2730 rx_thread_params_array[nb_rx_thread_params].thread_id =
2731 (uint8_t)int_fld[FLD_THREAD];
2732 ++nb_rx_thread_params;
2734 rx_thread_params = rx_thread_params_array;
2739 parse_tx_config(const char *q_arg)
2742 const char *p, *p0 = q_arg;
2749 unsigned long int_fld[_NUM_FLD];
2750 char *str_fld[_NUM_FLD];
2754 nb_tx_thread_params = 0;
2756 while ((p = strchr(p0, '(')) != NULL) {
2758 p0 = strchr(p, ')');
2763 if (size >= sizeof(s))
2766 snprintf(s, sizeof(s), "%.*s", size, p);
2767 if (rte_strsplit(s, sizeof(s), str_fld, _NUM_FLD, ',') != _NUM_FLD)
2769 for (i = 0; i < _NUM_FLD; i++) {
2771 int_fld[i] = strtoul(str_fld[i], &end, 0);
2772 if (errno != 0 || end == str_fld[i] || int_fld[i] > 255)
2775 if (nb_tx_thread_params >= MAX_LCORE_PARAMS) {
2776 printf("exceeded max number of tx params: %hu\n",
2777 nb_tx_thread_params);
2780 tx_thread_params_array[nb_tx_thread_params].lcore_id =
2781 (uint8_t)int_fld[FLD_LCORE];
2782 tx_thread_params_array[nb_tx_thread_params].thread_id =
2783 (uint8_t)int_fld[FLD_THREAD];
2784 ++nb_tx_thread_params;
2786 tx_thread_params = tx_thread_params_array;
2791 #if (APP_CPU_LOAD > 0)
2793 parse_stat_lcore(const char *stat_lcore)
2796 unsigned long lcore_id;
2798 lcore_id = strtoul(stat_lcore, &end, 10);
2799 if ((stat_lcore[0] == '\0') || (end == NULL) || (*end != '\0'))
2807 parse_eth_dest(const char *optarg)
2811 uint8_t c, *dest, peer_addr[6];
2814 portid = strtoul(optarg, &port_end, 10);
2815 if (errno != 0 || port_end == optarg || *port_end++ != ',')
2816 rte_exit(EXIT_FAILURE,
2817 "Invalid eth-dest: %s", optarg);
2818 if (portid >= RTE_MAX_ETHPORTS)
2819 rte_exit(EXIT_FAILURE,
2820 "eth-dest: port %d >= RTE_MAX_ETHPORTS(%d)\n",
2821 portid, RTE_MAX_ETHPORTS);
2823 if (cmdline_parse_etheraddr(NULL, port_end,
2824 &peer_addr, sizeof(peer_addr)) < 0)
2825 rte_exit(EXIT_FAILURE,
2826 "Invalid ethernet address: %s\n",
2828 dest = (uint8_t *)&dest_eth_addr[portid];
2829 for (c = 0; c < 6; c++)
2830 dest[c] = peer_addr[c];
2831 *(uint64_t *)(val_eth + portid) = dest_eth_addr[portid];
2834 #define CMD_LINE_OPT_RX_CONFIG "rx"
2835 #define CMD_LINE_OPT_TX_CONFIG "tx"
2836 #define CMD_LINE_OPT_STAT_LCORE "stat-lcore"
2837 #define CMD_LINE_OPT_ETH_DEST "eth-dest"
2838 #define CMD_LINE_OPT_NO_NUMA "no-numa"
2839 #define CMD_LINE_OPT_IPV6 "ipv6"
2840 #define CMD_LINE_OPT_ENABLE_JUMBO "enable-jumbo"
2841 #define CMD_LINE_OPT_HASH_ENTRY_NUM "hash-entry-num"
2842 #define CMD_LINE_OPT_NO_LTHREADS "no-lthreads"
2844 /* Parse the argument given in the command line of the application */
2846 parse_args(int argc, char **argv)
2851 char *prgname = argv[0];
2852 static struct option lgopts[] = {
2853 {CMD_LINE_OPT_RX_CONFIG, 1, 0, 0},
2854 {CMD_LINE_OPT_TX_CONFIG, 1, 0, 0},
2855 {CMD_LINE_OPT_STAT_LCORE, 1, 0, 0},
2856 {CMD_LINE_OPT_ETH_DEST, 1, 0, 0},
2857 {CMD_LINE_OPT_NO_NUMA, 0, 0, 0},
2858 {CMD_LINE_OPT_IPV6, 0, 0, 0},
2859 {CMD_LINE_OPT_ENABLE_JUMBO, 0, 0, 0},
2860 {CMD_LINE_OPT_HASH_ENTRY_NUM, 1, 0, 0},
2861 {CMD_LINE_OPT_NO_LTHREADS, 0, 0, 0},
2867 while ((opt = getopt_long(argc, argvopt, "p:P",
2868 lgopts, &option_index)) != EOF) {
2873 enabled_port_mask = parse_portmask(optarg);
2874 if (enabled_port_mask == 0) {
2875 printf("invalid portmask\n");
2876 print_usage(prgname);
2881 printf("Promiscuous mode selected\n");
2887 if (!strncmp(lgopts[option_index].name, CMD_LINE_OPT_RX_CONFIG,
2888 sizeof(CMD_LINE_OPT_RX_CONFIG))) {
2889 ret = parse_rx_config(optarg);
2891 printf("invalid rx-config\n");
2892 print_usage(prgname);
2897 if (!strncmp(lgopts[option_index].name, CMD_LINE_OPT_TX_CONFIG,
2898 sizeof(CMD_LINE_OPT_TX_CONFIG))) {
2899 ret = parse_tx_config(optarg);
2901 printf("invalid tx-config\n");
2902 print_usage(prgname);
2907 #if (APP_CPU_LOAD > 0)
2908 if (!strncmp(lgopts[option_index].name, CMD_LINE_OPT_STAT_LCORE,
2909 sizeof(CMD_LINE_OPT_STAT_LCORE))) {
2910 cpu_load_lcore_id = parse_stat_lcore(optarg);
2914 if (!strncmp(lgopts[option_index].name, CMD_LINE_OPT_ETH_DEST,
2915 sizeof(CMD_LINE_OPT_ETH_DEST)))
2916 parse_eth_dest(optarg);
2918 if (!strncmp(lgopts[option_index].name, CMD_LINE_OPT_NO_NUMA,
2919 sizeof(CMD_LINE_OPT_NO_NUMA))) {
2920 printf("numa is disabled\n");
2924 #if (APP_LOOKUP_METHOD == APP_LOOKUP_EXACT_MATCH)
2925 if (!strncmp(lgopts[option_index].name, CMD_LINE_OPT_IPV6,
2926 sizeof(CMD_LINE_OPT_IPV6))) {
2927 printf("ipv6 is specified\n");
2932 if (!strncmp(lgopts[option_index].name, CMD_LINE_OPT_NO_LTHREADS,
2933 sizeof(CMD_LINE_OPT_NO_LTHREADS))) {
2934 printf("l-threads model is disabled\n");
2938 if (!strncmp(lgopts[option_index].name, CMD_LINE_OPT_ENABLE_JUMBO,
2939 sizeof(CMD_LINE_OPT_ENABLE_JUMBO))) {
2940 struct option lenopts = {"max-pkt-len", required_argument, 0,
2943 printf("jumbo frame is enabled - disabling simple TX path\n");
2944 port_conf.rxmode.jumbo_frame = 1;
2946 /* if no max-pkt-len set, use the default value ETHER_MAX_LEN */
2947 if (0 == getopt_long(argc, argvopt, "", &lenopts,
2950 ret = parse_max_pkt_len(optarg);
2951 if ((ret < 64) || (ret > MAX_JUMBO_PKT_LEN)) {
2952 printf("invalid packet length\n");
2953 print_usage(prgname);
2956 port_conf.rxmode.max_rx_pkt_len = ret;
2958 printf("set jumbo frame max packet length to %u\n",
2959 (unsigned int)port_conf.rxmode.max_rx_pkt_len);
2961 #if (APP_LOOKUP_METHOD == APP_LOOKUP_EXACT_MATCH)
2962 if (!strncmp(lgopts[option_index].name, CMD_LINE_OPT_HASH_ENTRY_NUM,
2963 sizeof(CMD_LINE_OPT_HASH_ENTRY_NUM))) {
2964 ret = parse_hash_entry_number(optarg);
2965 if ((ret > 0) && (ret <= L3FWD_HASH_ENTRIES)) {
2966 hash_entry_number = ret;
2968 printf("invalid hash entry number\n");
2969 print_usage(prgname);
2977 print_usage(prgname);
2983 argv[optind-1] = prgname;
2986 optind = 0; /* reset getopt lib */
2991 print_ethaddr(const char *name, const struct ether_addr *eth_addr)
2993 char buf[ETHER_ADDR_FMT_SIZE];
2995 ether_format_addr(buf, ETHER_ADDR_FMT_SIZE, eth_addr);
2996 printf("%s%s", name, buf);
2999 #if (APP_LOOKUP_METHOD == APP_LOOKUP_EXACT_MATCH)
3001 static void convert_ipv4_5tuple(struct ipv4_5tuple *key1,
3002 union ipv4_5tuple_host *key2)
3004 key2->ip_dst = rte_cpu_to_be_32(key1->ip_dst);
3005 key2->ip_src = rte_cpu_to_be_32(key1->ip_src);
3006 key2->port_dst = rte_cpu_to_be_16(key1->port_dst);
3007 key2->port_src = rte_cpu_to_be_16(key1->port_src);
3008 key2->proto = key1->proto;
3013 static void convert_ipv6_5tuple(struct ipv6_5tuple *key1,
3014 union ipv6_5tuple_host *key2)
3018 for (i = 0; i < 16; i++) {
3019 key2->ip_dst[i] = key1->ip_dst[i];
3020 key2->ip_src[i] = key1->ip_src[i];
3022 key2->port_dst = rte_cpu_to_be_16(key1->port_dst);
3023 key2->port_src = rte_cpu_to_be_16(key1->port_src);
3024 key2->proto = key1->proto;
3030 #define BYTE_VALUE_MAX 256
3031 #define ALL_32_BITS 0xffffffff
3032 #define BIT_8_TO_15 0x0000ff00
3034 populate_ipv4_few_flow_into_table(const struct rte_hash *h)
3038 uint32_t array_len = RTE_DIM(ipv4_l3fwd_route_array);
3040 mask0 = _mm_set_epi32(ALL_32_BITS, ALL_32_BITS, ALL_32_BITS, BIT_8_TO_15);
3041 for (i = 0; i < array_len; i++) {
3042 struct ipv4_l3fwd_route entry;
3043 union ipv4_5tuple_host newkey;
3045 entry = ipv4_l3fwd_route_array[i];
3046 convert_ipv4_5tuple(&entry.key, &newkey);
3047 ret = rte_hash_add_key(h, (void *)&newkey);
3049 rte_exit(EXIT_FAILURE, "Unable to add entry %" PRIu32
3050 " to the l3fwd hash.\n", i);
3052 ipv4_l3fwd_out_if[ret] = entry.if_out;
3054 printf("Hash: Adding 0x%" PRIx32 " keys\n", array_len);
3057 #define BIT_16_TO_23 0x00ff0000
3059 populate_ipv6_few_flow_into_table(const struct rte_hash *h)
3063 uint32_t array_len = RTE_DIM(ipv6_l3fwd_route_array);
3065 mask1 = _mm_set_epi32(ALL_32_BITS, ALL_32_BITS, ALL_32_BITS, BIT_16_TO_23);
3066 mask2 = _mm_set_epi32(0, 0, ALL_32_BITS, ALL_32_BITS);
3067 for (i = 0; i < array_len; i++) {
3068 struct ipv6_l3fwd_route entry;
3069 union ipv6_5tuple_host newkey;
3071 entry = ipv6_l3fwd_route_array[i];
3072 convert_ipv6_5tuple(&entry.key, &newkey);
3073 ret = rte_hash_add_key(h, (void *)&newkey);
3075 rte_exit(EXIT_FAILURE, "Unable to add entry %" PRIu32
3076 " to the l3fwd hash.\n", i);
3078 ipv6_l3fwd_out_if[ret] = entry.if_out;
3080 printf("Hash: Adding 0x%" PRIx32 "keys\n", array_len);
3083 #define NUMBER_PORT_USED 4
3085 populate_ipv4_many_flow_into_table(const struct rte_hash *h,
3086 unsigned int nr_flow)
3090 mask0 = _mm_set_epi32(ALL_32_BITS, ALL_32_BITS, ALL_32_BITS, BIT_8_TO_15);
3092 for (i = 0; i < nr_flow; i++) {
3093 struct ipv4_l3fwd_route entry;
3094 union ipv4_5tuple_host newkey;
3095 uint8_t a = (uint8_t)((i / NUMBER_PORT_USED) % BYTE_VALUE_MAX);
3096 uint8_t b = (uint8_t)(((i / NUMBER_PORT_USED) / BYTE_VALUE_MAX) %
3098 uint8_t c = (uint8_t)((i / NUMBER_PORT_USED) / (BYTE_VALUE_MAX *
3100 /* Create the ipv4 exact match flow */
3101 memset(&entry, 0, sizeof(entry));
3102 switch (i & (NUMBER_PORT_USED - 1)) {
3104 entry = ipv4_l3fwd_route_array[0];
3105 entry.key.ip_dst = IPv4(101, c, b, a);
3108 entry = ipv4_l3fwd_route_array[1];
3109 entry.key.ip_dst = IPv4(201, c, b, a);
3112 entry = ipv4_l3fwd_route_array[2];
3113 entry.key.ip_dst = IPv4(111, c, b, a);
3116 entry = ipv4_l3fwd_route_array[3];
3117 entry.key.ip_dst = IPv4(211, c, b, a);
3120 convert_ipv4_5tuple(&entry.key, &newkey);
3121 int32_t ret = rte_hash_add_key(h, (void *)&newkey);
3124 rte_exit(EXIT_FAILURE, "Unable to add entry %u\n", i);
3126 ipv4_l3fwd_out_if[ret] = (uint8_t)entry.if_out;
3129 printf("Hash: Adding 0x%x keys\n", nr_flow);
3133 populate_ipv6_many_flow_into_table(const struct rte_hash *h,
3134 unsigned int nr_flow)
3138 mask1 = _mm_set_epi32(ALL_32_BITS, ALL_32_BITS, ALL_32_BITS, BIT_16_TO_23);
3139 mask2 = _mm_set_epi32(0, 0, ALL_32_BITS, ALL_32_BITS);
3140 for (i = 0; i < nr_flow; i++) {
3141 struct ipv6_l3fwd_route entry;
3142 union ipv6_5tuple_host newkey;
3144 uint8_t a = (uint8_t) ((i / NUMBER_PORT_USED) % BYTE_VALUE_MAX);
3145 uint8_t b = (uint8_t) (((i / NUMBER_PORT_USED) / BYTE_VALUE_MAX) %
3147 uint8_t c = (uint8_t) ((i / NUMBER_PORT_USED) / (BYTE_VALUE_MAX *
3150 /* Create the ipv6 exact match flow */
3151 memset(&entry, 0, sizeof(entry));
3152 switch (i & (NUMBER_PORT_USED - 1)) {
3154 entry = ipv6_l3fwd_route_array[0];
3157 entry = ipv6_l3fwd_route_array[1];
3160 entry = ipv6_l3fwd_route_array[2];
3163 entry = ipv6_l3fwd_route_array[3];
3166 entry.key.ip_dst[13] = c;
3167 entry.key.ip_dst[14] = b;
3168 entry.key.ip_dst[15] = a;
3169 convert_ipv6_5tuple(&entry.key, &newkey);
3170 int32_t ret = rte_hash_add_key(h, (void *)&newkey);
3173 rte_exit(EXIT_FAILURE, "Unable to add entry %u\n", i);
3175 ipv6_l3fwd_out_if[ret] = (uint8_t) entry.if_out;
3178 printf("Hash: Adding 0x%x keys\n", nr_flow);
3182 setup_hash(int socketid)
3184 struct rte_hash_parameters ipv4_l3fwd_hash_params = {
3186 .entries = L3FWD_HASH_ENTRIES,
3187 .key_len = sizeof(union ipv4_5tuple_host),
3188 .hash_func = ipv4_hash_crc,
3189 .hash_func_init_val = 0,
3192 struct rte_hash_parameters ipv6_l3fwd_hash_params = {
3194 .entries = L3FWD_HASH_ENTRIES,
3195 .key_len = sizeof(union ipv6_5tuple_host),
3196 .hash_func = ipv6_hash_crc,
3197 .hash_func_init_val = 0,
3202 /* create ipv4 hash */
3203 snprintf(s, sizeof(s), "ipv4_l3fwd_hash_%d", socketid);
3204 ipv4_l3fwd_hash_params.name = s;
3205 ipv4_l3fwd_hash_params.socket_id = socketid;
3206 ipv4_l3fwd_lookup_struct[socketid] =
3207 rte_hash_create(&ipv4_l3fwd_hash_params);
3208 if (ipv4_l3fwd_lookup_struct[socketid] == NULL)
3209 rte_exit(EXIT_FAILURE, "Unable to create the l3fwd hash on "
3210 "socket %d\n", socketid);
3212 /* create ipv6 hash */
3213 snprintf(s, sizeof(s), "ipv6_l3fwd_hash_%d", socketid);
3214 ipv6_l3fwd_hash_params.name = s;
3215 ipv6_l3fwd_hash_params.socket_id = socketid;
3216 ipv6_l3fwd_lookup_struct[socketid] =
3217 rte_hash_create(&ipv6_l3fwd_hash_params);
3218 if (ipv6_l3fwd_lookup_struct[socketid] == NULL)
3219 rte_exit(EXIT_FAILURE, "Unable to create the l3fwd hash on "
3220 "socket %d\n", socketid);
3222 if (hash_entry_number != HASH_ENTRY_NUMBER_DEFAULT) {
3223 /* For testing hash matching with a large number of flows we
3224 * generate millions of IP 5-tuples with an incremented dst
3225 * address to initialize the hash table. */
3227 /* populate the ipv4 hash */
3228 populate_ipv4_many_flow_into_table(
3229 ipv4_l3fwd_lookup_struct[socketid], hash_entry_number);
3231 /* populate the ipv6 hash */
3232 populate_ipv6_many_flow_into_table(
3233 ipv6_l3fwd_lookup_struct[socketid], hash_entry_number);
3236 /* Use data in ipv4/ipv6 l3fwd lookup table directly to initialize
3239 /* populate the ipv4 hash */
3240 populate_ipv4_few_flow_into_table(
3241 ipv4_l3fwd_lookup_struct[socketid]);
3243 /* populate the ipv6 hash */
3244 populate_ipv6_few_flow_into_table(
3245 ipv6_l3fwd_lookup_struct[socketid]);
3251 #if (APP_LOOKUP_METHOD == APP_LOOKUP_LPM)
3253 setup_lpm(int socketid)
3255 struct rte_lpm6_config config;
3256 struct rte_lpm_config lpm_ipv4_config;
3261 /* create the LPM table */
3262 snprintf(s, sizeof(s), "IPV4_L3FWD_LPM_%d", socketid);
3263 lpm_ipv4_config.max_rules = IPV4_L3FWD_LPM_MAX_RULES;
3264 lpm_ipv4_config.number_tbl8s = 256;
3265 lpm_ipv4_config.flags = 0;
3266 ipv4_l3fwd_lookup_struct[socketid] =
3267 rte_lpm_create(s, socketid, &lpm_ipv4_config);
3268 if (ipv4_l3fwd_lookup_struct[socketid] == NULL)
3269 rte_exit(EXIT_FAILURE, "Unable to create the l3fwd LPM table"
3270 " on socket %d\n", socketid);
3272 /* populate the LPM table */
3273 for (i = 0; i < IPV4_L3FWD_NUM_ROUTES; i++) {
3275 /* skip unused ports */
3276 if ((1 << ipv4_l3fwd_route_array[i].if_out &
3277 enabled_port_mask) == 0)
3280 ret = rte_lpm_add(ipv4_l3fwd_lookup_struct[socketid],
3281 ipv4_l3fwd_route_array[i].ip,
3282 ipv4_l3fwd_route_array[i].depth,
3283 ipv4_l3fwd_route_array[i].if_out);
3286 rte_exit(EXIT_FAILURE, "Unable to add entry %u to the "
3287 "l3fwd LPM table on socket %d\n",
3291 printf("LPM: Adding route 0x%08x / %d (%d)\n",
3292 (unsigned)ipv4_l3fwd_route_array[i].ip,
3293 ipv4_l3fwd_route_array[i].depth,
3294 ipv4_l3fwd_route_array[i].if_out);
3297 /* create the LPM6 table */
3298 snprintf(s, sizeof(s), "IPV6_L3FWD_LPM_%d", socketid);
3300 config.max_rules = IPV6_L3FWD_LPM_MAX_RULES;
3301 config.number_tbl8s = IPV6_L3FWD_LPM_NUMBER_TBL8S;
3303 ipv6_l3fwd_lookup_struct[socketid] = rte_lpm6_create(s, socketid,
3305 if (ipv6_l3fwd_lookup_struct[socketid] == NULL)
3306 rte_exit(EXIT_FAILURE, "Unable to create the l3fwd LPM table"
3307 " on socket %d\n", socketid);
3309 /* populate the LPM table */
3310 for (i = 0; i < IPV6_L3FWD_NUM_ROUTES; i++) {
3312 /* skip unused ports */
3313 if ((1 << ipv6_l3fwd_route_array[i].if_out &
3314 enabled_port_mask) == 0)
3317 ret = rte_lpm6_add(ipv6_l3fwd_lookup_struct[socketid],
3318 ipv6_l3fwd_route_array[i].ip,
3319 ipv6_l3fwd_route_array[i].depth,
3320 ipv6_l3fwd_route_array[i].if_out);
3323 rte_exit(EXIT_FAILURE, "Unable to add entry %u to the "
3324 "l3fwd LPM table on socket %d\n",
3328 printf("LPM: Adding route %s / %d (%d)\n",
3330 ipv6_l3fwd_route_array[i].depth,
3331 ipv6_l3fwd_route_array[i].if_out);
3337 init_mem(unsigned nb_mbuf)
3339 struct lcore_conf *qconf;
3344 for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
3345 if (rte_lcore_is_enabled(lcore_id) == 0)
3349 socketid = rte_lcore_to_socket_id(lcore_id);
3353 if (socketid >= NB_SOCKETS) {
3354 rte_exit(EXIT_FAILURE, "Socket %d of lcore %u is out of range %d\n",
3355 socketid, lcore_id, NB_SOCKETS);
3357 if (pktmbuf_pool[socketid] == NULL) {
3358 snprintf(s, sizeof(s), "mbuf_pool_%d", socketid);
3359 pktmbuf_pool[socketid] =
3360 rte_pktmbuf_pool_create(s, nb_mbuf,
3361 MEMPOOL_CACHE_SIZE, 0,
3362 RTE_MBUF_DEFAULT_BUF_SIZE, socketid);
3363 if (pktmbuf_pool[socketid] == NULL)
3364 rte_exit(EXIT_FAILURE,
3365 "Cannot init mbuf pool on socket %d\n", socketid);
3367 printf("Allocated mbuf pool on socket %d\n", socketid);
3369 #if (APP_LOOKUP_METHOD == APP_LOOKUP_LPM)
3370 setup_lpm(socketid);
3372 setup_hash(socketid);
3375 qconf = &lcore_conf[lcore_id];
3376 qconf->ipv4_lookup_struct = ipv4_l3fwd_lookup_struct[socketid];
3377 qconf->ipv6_lookup_struct = ipv6_l3fwd_lookup_struct[socketid];
3382 /* Check the link status of all ports in up to 9s, and print them finally */
3384 check_all_ports_link_status(uint8_t port_num, uint32_t port_mask)
3386 #define CHECK_INTERVAL 100 /* 100ms */
3387 #define MAX_CHECK_TIME 90 /* 9s (90 * 100ms) in total */
3388 uint8_t portid, count, all_ports_up, print_flag = 0;
3389 struct rte_eth_link link;
3391 printf("\nChecking link status");
3393 for (count = 0; count <= MAX_CHECK_TIME; count++) {
3395 for (portid = 0; portid < port_num; portid++) {
3396 if ((port_mask & (1 << portid)) == 0)
3398 memset(&link, 0, sizeof(link));
3399 rte_eth_link_get_nowait(portid, &link);
3400 /* print link status if flag set */
3401 if (print_flag == 1) {
3402 if (link.link_status)
3403 printf("Port %d Link Up - speed %u "
3404 "Mbps - %s\n", (uint8_t)portid,
3405 (unsigned)link.link_speed,
3406 (link.link_duplex == ETH_LINK_FULL_DUPLEX) ?
3407 ("full-duplex") : ("half-duplex\n"));
3409 printf("Port %d Link Down\n",
3413 /* clear all_ports_up flag if any link down */
3414 if (link.link_status == ETH_LINK_DOWN) {
3419 /* after finally printing all link status, get out */
3420 if (print_flag == 1)
3423 if (all_ports_up == 0) {
3426 rte_delay_ms(CHECK_INTERVAL);
3429 /* set the print_flag if all ports up or timeout */
3430 if (all_ports_up == 1 || count == (MAX_CHECK_TIME - 1)) {
3438 main(int argc, char **argv)
3440 struct rte_eth_dev_info dev_info;
3441 struct rte_eth_txconf *txconf;
3447 uint32_t n_tx_queue, nb_lcores;
3448 uint8_t portid, nb_rx_queue, queue, socketid;
3451 ret = rte_eal_init(argc, argv);
3453 rte_exit(EXIT_FAILURE, "Invalid EAL parameters\n");
3457 /* pre-init dst MACs for all ports to 02:00:00:00:00:xx */
3458 for (portid = 0; portid < RTE_MAX_ETHPORTS; portid++) {
3459 dest_eth_addr[portid] = ETHER_LOCAL_ADMIN_ADDR +
3460 ((uint64_t)portid << 40);
3461 *(uint64_t *)(val_eth + portid) = dest_eth_addr[portid];
3464 /* parse application arguments (after the EAL ones) */
3465 ret = parse_args(argc, argv);
3467 rte_exit(EXIT_FAILURE, "Invalid L3FWD parameters\n");
3469 if (check_lcore_params() < 0)
3470 rte_exit(EXIT_FAILURE, "check_lcore_params failed\n");
3472 printf("Initializing rx-queues...\n");
3473 ret = init_rx_queues();
3475 rte_exit(EXIT_FAILURE, "init_rx_queues failed\n");
3477 printf("Initializing tx-threads...\n");
3478 ret = init_tx_threads();
3480 rte_exit(EXIT_FAILURE, "init_tx_threads failed\n");
3482 printf("Initializing rings...\n");
3483 ret = init_rx_rings();
3485 rte_exit(EXIT_FAILURE, "init_rx_rings failed\n");
3487 nb_ports = rte_eth_dev_count();
3489 if (check_port_config(nb_ports) < 0)
3490 rte_exit(EXIT_FAILURE, "check_port_config failed\n");
3492 nb_lcores = rte_lcore_count();
3494 /* initialize all ports */
3495 for (portid = 0; portid < nb_ports; portid++) {
3496 /* skip ports that are not enabled */
3497 if ((enabled_port_mask & (1 << portid)) == 0) {
3498 printf("\nSkipping disabled port %d\n", portid);
3503 printf("Initializing port %d ... ", portid);
3506 nb_rx_queue = get_port_n_rx_queues(portid);
3507 n_tx_queue = nb_lcores;
3508 if (n_tx_queue > MAX_TX_QUEUE_PER_PORT)
3509 n_tx_queue = MAX_TX_QUEUE_PER_PORT;
3510 printf("Creating queues: nb_rxq=%d nb_txq=%u... ",
3511 nb_rx_queue, (unsigned)n_tx_queue);
3512 ret = rte_eth_dev_configure(portid, nb_rx_queue,
3513 (uint16_t)n_tx_queue, &port_conf);
3515 rte_exit(EXIT_FAILURE, "Cannot configure device: err=%d, port=%d\n",
3518 rte_eth_macaddr_get(portid, &ports_eth_addr[portid]);
3519 print_ethaddr(" Address:", &ports_eth_addr[portid]);
3521 print_ethaddr("Destination:",
3522 (const struct ether_addr *)&dest_eth_addr[portid]);
3526 * prepare src MACs for each port.
3528 ether_addr_copy(&ports_eth_addr[portid],
3529 (struct ether_addr *)(val_eth + portid) + 1);
3532 ret = init_mem(NB_MBUF);
3534 rte_exit(EXIT_FAILURE, "init_mem failed\n");
3536 /* init one TX queue per couple (lcore,port) */
3538 for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
3539 if (rte_lcore_is_enabled(lcore_id) == 0)
3543 socketid = (uint8_t)rte_lcore_to_socket_id(lcore_id);
3547 printf("txq=%u,%d,%d ", lcore_id, queueid, socketid);
3550 rte_eth_dev_info_get(portid, &dev_info);
3551 txconf = &dev_info.default_txconf;
3552 if (port_conf.rxmode.jumbo_frame)
3553 txconf->txq_flags = 0;
3554 ret = rte_eth_tx_queue_setup(portid, queueid, nb_txd,
3557 rte_exit(EXIT_FAILURE, "rte_eth_tx_queue_setup: err=%d, "
3558 "port=%d\n", ret, portid);
3560 tx_thread[lcore_id].tx_queue_id[portid] = queueid;
3566 for (i = 0; i < n_rx_thread; i++) {
3567 lcore_id = rx_thread[i].conf.lcore_id;
3569 if (rte_lcore_is_enabled(lcore_id) == 0) {
3570 rte_exit(EXIT_FAILURE,
3571 "Cannot start Rx thread on lcore %u: lcore disabled\n",
3576 printf("\nInitializing rx queues for Rx thread %d on lcore %u ... ",
3580 /* init RX queues */
3581 for (queue = 0; queue < rx_thread[i].n_rx_queue; ++queue) {
3582 portid = rx_thread[i].rx_queue_list[queue].port_id;
3583 queueid = rx_thread[i].rx_queue_list[queue].queue_id;
3586 socketid = (uint8_t)rte_lcore_to_socket_id(lcore_id);
3590 printf("rxq=%d,%d,%d ", portid, queueid, socketid);
3593 ret = rte_eth_rx_queue_setup(portid, queueid, nb_rxd,
3596 pktmbuf_pool[socketid]);
3598 rte_exit(EXIT_FAILURE, "rte_eth_rx_queue_setup: err=%d, "
3599 "port=%d\n", ret, portid);
3606 for (portid = 0; portid < nb_ports; portid++) {
3607 if ((enabled_port_mask & (1 << portid)) == 0)
3611 ret = rte_eth_dev_start(portid);
3613 rte_exit(EXIT_FAILURE, "rte_eth_dev_start: err=%d, port=%d\n",
3617 * If enabled, put device in promiscuous mode.
3618 * This allows IO forwarding mode to forward packets
3619 * to itself through 2 cross-connected ports of the
3623 rte_eth_promiscuous_enable(portid);
3626 check_all_ports_link_status((uint8_t)nb_ports, enabled_port_mask);
3629 printf("Starting L-Threading Model\n");
3631 #if (APP_CPU_LOAD > 0)
3632 if (cpu_load_lcore_id > 0)
3633 /* Use one lcore for cpu load collector */
3637 lthread_num_schedulers_set(nb_lcores);
3638 rte_eal_mp_remote_launch(sched_spawner, NULL, SKIP_MASTER);
3639 lthread_master_spawner(NULL);
3642 printf("Starting P-Threading Model\n");
3643 /* launch per-lcore init on every lcore */
3644 rte_eal_mp_remote_launch(pthread_run, NULL, CALL_MASTER);
3645 RTE_LCORE_FOREACH_SLAVE(lcore_id) {
3646 if (rte_eal_wait_lcore(lcore_id) < 0)