4 * Copyright(c) 2010-2016 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>
76 #include <rte_pause.h>
78 #include <cmdline_parse.h>
79 #include <cmdline_parse_etheraddr.h>
81 #include <lthread_api.h>
83 #define APP_LOOKUP_EXACT_MATCH 0
84 #define APP_LOOKUP_LPM 1
85 #define DO_RFC_1812_CHECKS
87 /* Enable cpu-load stats 0-off, 1-on */
88 #define APP_CPU_LOAD 1
90 #ifndef APP_LOOKUP_METHOD
91 #define APP_LOOKUP_METHOD APP_LOOKUP_LPM
94 #ifndef __GLIBC__ /* sched_getcpu() is glibc specific */
95 #define sched_getcpu() rte_lcore_id()
99 check_ptype(int portid)
102 int ipv4 = 0, ipv6 = 0;
104 ret = rte_eth_dev_get_supported_ptypes(portid, RTE_PTYPE_L3_MASK, NULL,
109 uint32_t ptypes[ret];
111 ret = rte_eth_dev_get_supported_ptypes(portid, RTE_PTYPE_L3_MASK,
113 for (i = 0; i < ret; ++i) {
114 if (ptypes[i] & RTE_PTYPE_L3_IPV4)
116 if (ptypes[i] & RTE_PTYPE_L3_IPV6)
127 parse_ptype(struct rte_mbuf *m)
129 struct ether_hdr *eth_hdr;
130 uint32_t packet_type = RTE_PTYPE_UNKNOWN;
133 eth_hdr = rte_pktmbuf_mtod(m, struct ether_hdr *);
134 ether_type = eth_hdr->ether_type;
135 if (ether_type == rte_cpu_to_be_16(ETHER_TYPE_IPv4))
136 packet_type |= RTE_PTYPE_L3_IPV4_EXT_UNKNOWN;
137 else if (ether_type == rte_cpu_to_be_16(ETHER_TYPE_IPv6))
138 packet_type |= RTE_PTYPE_L3_IPV6_EXT_UNKNOWN;
140 m->packet_type = packet_type;
144 cb_parse_ptype(__rte_unused uint8_t port, __rte_unused uint16_t queue,
145 struct rte_mbuf *pkts[], uint16_t nb_pkts,
146 __rte_unused uint16_t max_pkts, __rte_unused void *user_param)
150 for (i = 0; i < nb_pkts; i++)
151 parse_ptype(pkts[i]);
157 * When set to zero, simple forwaring path is eanbled.
158 * When set to one, optimized forwarding path is enabled.
159 * Note that LPM optimisation path uses SSE4.1 instructions.
161 #define ENABLE_MULTI_BUFFER_OPTIMIZE 1
163 #if (APP_LOOKUP_METHOD == APP_LOOKUP_EXACT_MATCH)
164 #include <rte_hash.h>
165 #elif (APP_LOOKUP_METHOD == APP_LOOKUP_LPM)
167 #include <rte_lpm6.h>
169 #error "APP_LOOKUP_METHOD set to incorrect value"
172 #define RTE_LOGTYPE_L3FWD RTE_LOGTYPE_USER1
174 #define MAX_JUMBO_PKT_LEN 9600
176 #define IPV6_ADDR_LEN 16
178 #define MEMPOOL_CACHE_SIZE 256
181 * This expression is used to calculate the number of mbufs needed depending on
182 * user input, taking into account memory for rx and tx hardware rings, cache
183 * per lcore and mtable per port per lcore. RTE_MAX is used to ensure that
184 * NB_MBUF never goes below a minimum value of 8192
187 #define NB_MBUF RTE_MAX(\
188 (nb_ports*nb_rx_queue*RTE_TEST_RX_DESC_DEFAULT + \
189 nb_ports*nb_lcores*MAX_PKT_BURST + \
190 nb_ports*n_tx_queue*RTE_TEST_TX_DESC_DEFAULT + \
191 nb_lcores*MEMPOOL_CACHE_SIZE), \
194 #define MAX_PKT_BURST 32
195 #define BURST_TX_DRAIN_US 100 /* TX drain every ~100us */
198 * Try to avoid TX buffering if we have at least MAX_TX_BURST packets to send.
200 #define MAX_TX_BURST (MAX_PKT_BURST / 2)
201 #define BURST_SIZE MAX_TX_BURST
205 /* Configure how many packets ahead to prefetch, when reading packets */
206 #define PREFETCH_OFFSET 3
208 /* Used to mark destination port as 'invalid'. */
209 #define BAD_PORT ((uint16_t)-1)
214 * Configurable number of RX/TX ring descriptors
216 #define RTE_TEST_RX_DESC_DEFAULT 128
217 #define RTE_TEST_TX_DESC_DEFAULT 128
218 static uint16_t nb_rxd = RTE_TEST_RX_DESC_DEFAULT;
219 static uint16_t nb_txd = RTE_TEST_TX_DESC_DEFAULT;
221 /* ethernet addresses of ports */
222 static uint64_t dest_eth_addr[RTE_MAX_ETHPORTS];
223 static struct ether_addr ports_eth_addr[RTE_MAX_ETHPORTS];
225 static __m128i val_eth[RTE_MAX_ETHPORTS];
227 /* replace first 12B of the ethernet header. */
228 #define MASK_ETH 0x3f
230 /* mask of enabled ports */
231 static uint32_t enabled_port_mask;
232 static int promiscuous_on; /**< Set in promiscuous mode off by default. */
233 static int numa_on = 1; /**< NUMA is enabled by default. */
234 static int parse_ptype_on;
236 #if (APP_LOOKUP_METHOD == APP_LOOKUP_EXACT_MATCH)
237 static int ipv6; /**< ipv6 is false by default. */
240 #if (APP_CPU_LOAD == 1)
242 #define MAX_CPU RTE_MAX_LCORE
243 #define CPU_LOAD_TIMEOUT_US (5 * 1000 * 1000) /**< Timeout for collecting 5s */
245 #define CPU_PROCESS 0
247 #define MAX_CPU_COUNTER 2
252 uint64_t hits[MAX_CPU_COUNTER][MAX_CPU];
253 } __rte_cache_aligned;
255 static struct cpu_load cpu_load;
256 static int cpu_load_lcore_id = -1;
258 #define SET_CPU_BUSY(thread, counter) \
259 thread->conf.busy[counter] = 1
261 #define SET_CPU_IDLE(thread, counter) \
262 thread->conf.busy[counter] = 0
264 #define IS_CPU_BUSY(thread, counter) \
265 (thread->conf.busy[counter] > 0)
269 #define SET_CPU_BUSY(thread, counter)
270 #define SET_CPU_IDLE(thread, counter)
271 #define IS_CPU_BUSY(thread, counter) 0
277 struct rte_mbuf *m_table[MAX_PKT_BURST];
280 struct lcore_rx_queue {
283 } __rte_cache_aligned;
285 #define MAX_RX_QUEUE_PER_LCORE 16
286 #define MAX_TX_QUEUE_PER_PORT RTE_MAX_ETHPORTS
287 #define MAX_RX_QUEUE_PER_PORT 128
289 #define MAX_LCORE_PARAMS 1024
290 struct rx_thread_params {
295 } __rte_cache_aligned;
297 static struct rx_thread_params rx_thread_params_array[MAX_LCORE_PARAMS];
298 static struct rx_thread_params rx_thread_params_array_default[] = {
310 static struct rx_thread_params *rx_thread_params =
311 rx_thread_params_array_default;
312 static uint16_t nb_rx_thread_params = RTE_DIM(rx_thread_params_array_default);
314 struct tx_thread_params {
317 } __rte_cache_aligned;
319 static struct tx_thread_params tx_thread_params_array[MAX_LCORE_PARAMS];
320 static struct tx_thread_params tx_thread_params_array_default[] = {
332 static struct tx_thread_params *tx_thread_params =
333 tx_thread_params_array_default;
334 static uint16_t nb_tx_thread_params = RTE_DIM(tx_thread_params_array_default);
336 static struct rte_eth_conf port_conf = {
338 .mq_mode = ETH_MQ_RX_RSS,
339 .max_rx_pkt_len = ETHER_MAX_LEN,
341 .header_split = 0, /**< Header Split disabled */
342 .hw_ip_checksum = 1, /**< IP checksum offload enabled */
343 .hw_vlan_filter = 0, /**< VLAN filtering disabled */
344 .jumbo_frame = 0, /**< Jumbo Frame Support disabled */
345 .hw_strip_crc = 1, /**< CRC stripped by hardware */
350 .rss_hf = ETH_RSS_TCP,
354 .mq_mode = ETH_MQ_TX_NONE,
358 static struct rte_mempool *pktmbuf_pool[NB_SOCKETS];
360 #if (APP_LOOKUP_METHOD == APP_LOOKUP_EXACT_MATCH)
362 #include <rte_hash_crc.h>
363 #define DEFAULT_HASH_FUNC rte_hash_crc
371 } __attribute__((__packed__));
373 union ipv4_5tuple_host {
386 #define XMM_NUM_IN_IPV6_5TUPLE 3
389 uint8_t ip_dst[IPV6_ADDR_LEN];
390 uint8_t ip_src[IPV6_ADDR_LEN];
394 } __attribute__((__packed__));
396 union ipv6_5tuple_host {
401 uint8_t ip_src[IPV6_ADDR_LEN];
402 uint8_t ip_dst[IPV6_ADDR_LEN];
407 __m128i xmm[XMM_NUM_IN_IPV6_5TUPLE];
410 struct ipv4_l3fwd_route {
411 struct ipv4_5tuple key;
415 struct ipv6_l3fwd_route {
416 struct ipv6_5tuple key;
420 static struct ipv4_l3fwd_route ipv4_l3fwd_route_array[] = {
421 {{IPv4(101, 0, 0, 0), IPv4(100, 10, 0, 1), 101, 11, IPPROTO_TCP}, 0},
422 {{IPv4(201, 0, 0, 0), IPv4(200, 20, 0, 1), 102, 12, IPPROTO_TCP}, 1},
423 {{IPv4(111, 0, 0, 0), IPv4(100, 30, 0, 1), 101, 11, IPPROTO_TCP}, 2},
424 {{IPv4(211, 0, 0, 0), IPv4(200, 40, 0, 1), 102, 12, IPPROTO_TCP}, 3},
427 static struct ipv6_l3fwd_route ipv6_l3fwd_route_array[] = {
429 {0xfe, 0x80, 0, 0, 0, 0, 0, 0, 0x02, 0x1e, 0x67, 0xff, 0xfe, 0, 0, 0},
430 {0xfe, 0x80, 0, 0, 0, 0, 0, 0, 0x02, 0x1b, 0x21, 0xff, 0xfe, 0x91, 0x38,
432 101, 11, IPPROTO_TCP}, 0},
435 {0xfe, 0x90, 0, 0, 0, 0, 0, 0, 0x02, 0x1e, 0x67, 0xff, 0xfe, 0, 0, 0},
436 {0xfe, 0x90, 0, 0, 0, 0, 0, 0, 0x02, 0x1b, 0x21, 0xff, 0xfe, 0x91, 0x38,
438 102, 12, IPPROTO_TCP}, 1},
441 {0xfe, 0xa0, 0, 0, 0, 0, 0, 0, 0x02, 0x1e, 0x67, 0xff, 0xfe, 0, 0, 0},
442 {0xfe, 0xa0, 0, 0, 0, 0, 0, 0, 0x02, 0x1b, 0x21, 0xff, 0xfe, 0x91, 0x38,
444 101, 11, IPPROTO_TCP}, 2},
447 {0xfe, 0xb0, 0, 0, 0, 0, 0, 0, 0x02, 0x1e, 0x67, 0xff, 0xfe, 0, 0, 0},
448 {0xfe, 0xb0, 0, 0, 0, 0, 0, 0, 0x02, 0x1b, 0x21, 0xff, 0xfe, 0x91, 0x38,
450 102, 12, IPPROTO_TCP}, 3},
453 typedef struct rte_hash lookup_struct_t;
454 static lookup_struct_t *ipv4_l3fwd_lookup_struct[NB_SOCKETS];
455 static lookup_struct_t *ipv6_l3fwd_lookup_struct[NB_SOCKETS];
457 #ifdef RTE_ARCH_X86_64
458 /* default to 4 million hash entries (approx) */
459 #define L3FWD_HASH_ENTRIES (1024*1024*4)
461 /* 32-bit has less address-space for hugepage memory, limit to 1M entries */
462 #define L3FWD_HASH_ENTRIES (1024*1024*1)
464 #define HASH_ENTRY_NUMBER_DEFAULT 4
466 static uint32_t hash_entry_number = HASH_ENTRY_NUMBER_DEFAULT;
468 static inline uint32_t
469 ipv4_hash_crc(const void *data, __rte_unused uint32_t data_len,
472 const union ipv4_5tuple_host *k;
478 p = (const uint32_t *)&k->port_src;
480 init_val = rte_hash_crc_4byte(t, init_val);
481 init_val = rte_hash_crc_4byte(k->ip_src, init_val);
482 init_val = rte_hash_crc_4byte(k->ip_dst, init_val);
483 init_val = rte_hash_crc_4byte(*p, init_val);
487 static inline uint32_t
488 ipv6_hash_crc(const void *data, __rte_unused uint32_t data_len,
491 const union ipv6_5tuple_host *k;
494 const uint32_t *ip_src0, *ip_src1, *ip_src2, *ip_src3;
495 const uint32_t *ip_dst0, *ip_dst1, *ip_dst2, *ip_dst3;
499 p = (const uint32_t *)&k->port_src;
501 ip_src0 = (const uint32_t *) k->ip_src;
502 ip_src1 = (const uint32_t *)(k->ip_src + 4);
503 ip_src2 = (const uint32_t *)(k->ip_src + 8);
504 ip_src3 = (const uint32_t *)(k->ip_src + 12);
505 ip_dst0 = (const uint32_t *) k->ip_dst;
506 ip_dst1 = (const uint32_t *)(k->ip_dst + 4);
507 ip_dst2 = (const uint32_t *)(k->ip_dst + 8);
508 ip_dst3 = (const uint32_t *)(k->ip_dst + 12);
509 init_val = rte_hash_crc_4byte(t, init_val);
510 init_val = rte_hash_crc_4byte(*ip_src0, init_val);
511 init_val = rte_hash_crc_4byte(*ip_src1, init_val);
512 init_val = rte_hash_crc_4byte(*ip_src2, init_val);
513 init_val = rte_hash_crc_4byte(*ip_src3, init_val);
514 init_val = rte_hash_crc_4byte(*ip_dst0, init_val);
515 init_val = rte_hash_crc_4byte(*ip_dst1, init_val);
516 init_val = rte_hash_crc_4byte(*ip_dst2, init_val);
517 init_val = rte_hash_crc_4byte(*ip_dst3, init_val);
518 init_val = rte_hash_crc_4byte(*p, init_val);
522 #define IPV4_L3FWD_NUM_ROUTES RTE_DIM(ipv4_l3fwd_route_array)
523 #define IPV6_L3FWD_NUM_ROUTES RTE_DIM(ipv6_l3fwd_route_array)
525 static uint8_t ipv4_l3fwd_out_if[L3FWD_HASH_ENTRIES] __rte_cache_aligned;
526 static uint8_t ipv6_l3fwd_out_if[L3FWD_HASH_ENTRIES] __rte_cache_aligned;
530 #if (APP_LOOKUP_METHOD == APP_LOOKUP_LPM)
531 struct ipv4_l3fwd_route {
537 struct ipv6_l3fwd_route {
543 static struct ipv4_l3fwd_route ipv4_l3fwd_route_array[] = {
544 {IPv4(1, 1, 1, 0), 24, 0},
545 {IPv4(2, 1, 1, 0), 24, 1},
546 {IPv4(3, 1, 1, 0), 24, 2},
547 {IPv4(4, 1, 1, 0), 24, 3},
548 {IPv4(5, 1, 1, 0), 24, 4},
549 {IPv4(6, 1, 1, 0), 24, 5},
550 {IPv4(7, 1, 1, 0), 24, 6},
551 {IPv4(8, 1, 1, 0), 24, 7},
554 static struct ipv6_l3fwd_route ipv6_l3fwd_route_array[] = {
555 {{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, 48, 0},
556 {{2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, 48, 1},
557 {{3, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, 48, 2},
558 {{4, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, 48, 3},
559 {{5, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, 48, 4},
560 {{6, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, 48, 5},
561 {{7, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, 48, 6},
562 {{8, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, 48, 7},
565 #define IPV4_L3FWD_NUM_ROUTES RTE_DIM(ipv4_l3fwd_route_array)
566 #define IPV6_L3FWD_NUM_ROUTES RTE_DIM(ipv6_l3fwd_route_array)
568 #define IPV4_L3FWD_LPM_MAX_RULES 1024
569 #define IPV6_L3FWD_LPM_MAX_RULES 1024
570 #define IPV6_L3FWD_LPM_NUMBER_TBL8S (1 << 16)
572 typedef struct rte_lpm lookup_struct_t;
573 typedef struct rte_lpm6 lookup6_struct_t;
574 static lookup_struct_t *ipv4_l3fwd_lookup_struct[NB_SOCKETS];
575 static lookup6_struct_t *ipv6_l3fwd_lookup_struct[NB_SOCKETS];
579 lookup_struct_t *ipv4_lookup_struct;
580 #if (APP_LOOKUP_METHOD == APP_LOOKUP_LPM)
581 lookup6_struct_t *ipv6_lookup_struct;
583 lookup_struct_t *ipv6_lookup_struct;
586 } __rte_cache_aligned;
588 static struct lcore_conf lcore_conf[RTE_MAX_LCORE];
589 RTE_DEFINE_PER_LCORE(struct lcore_conf *, lcore_conf);
591 #define MAX_RX_QUEUE_PER_THREAD 16
592 #define MAX_TX_PORT_PER_THREAD RTE_MAX_ETHPORTS
593 #define MAX_TX_QUEUE_PER_PORT RTE_MAX_ETHPORTS
594 #define MAX_RX_QUEUE_PER_PORT 128
596 #define MAX_RX_THREAD 1024
597 #define MAX_TX_THREAD 1024
598 #define MAX_THREAD (MAX_RX_THREAD + MAX_TX_THREAD)
601 * Producers and consumers threads configuration
603 static int lthreads_on = 1; /**< Use lthreads for processing*/
605 rte_atomic16_t rx_counter; /**< Number of spawned rx threads */
606 rte_atomic16_t tx_counter; /**< Number of spawned tx threads */
609 uint16_t lcore_id; /**< Initial lcore for rx thread */
610 uint16_t cpu_id; /**< Cpu id for cpu load stats counter */
611 uint16_t thread_id; /**< Thread ID */
613 #if (APP_CPU_LOAD > 0)
614 int busy[MAX_CPU_COUNTER];
618 struct thread_rx_conf {
619 struct thread_conf conf;
622 struct lcore_rx_queue rx_queue_list[MAX_RX_QUEUE_PER_LCORE];
624 uint16_t n_ring; /**< Number of output rings */
625 struct rte_ring *ring[RTE_MAX_LCORE];
626 struct lthread_cond *ready[RTE_MAX_LCORE];
628 #if (APP_CPU_LOAD > 0)
629 int busy[MAX_CPU_COUNTER];
631 } __rte_cache_aligned;
633 uint16_t n_rx_thread;
634 struct thread_rx_conf rx_thread[MAX_RX_THREAD];
636 struct thread_tx_conf {
637 struct thread_conf conf;
639 uint16_t tx_queue_id[RTE_MAX_LCORE];
640 struct mbuf_table tx_mbufs[RTE_MAX_LCORE];
642 struct rte_ring *ring;
643 struct lthread_cond **ready;
645 } __rte_cache_aligned;
647 uint16_t n_tx_thread;
648 struct thread_tx_conf tx_thread[MAX_TX_THREAD];
650 /* Send burst of packets on an output interface */
652 send_burst(struct thread_tx_conf *qconf, uint16_t n, uint8_t port)
654 struct rte_mbuf **m_table;
658 queueid = qconf->tx_queue_id[port];
659 m_table = (struct rte_mbuf **)qconf->tx_mbufs[port].m_table;
661 ret = rte_eth_tx_burst(port, queueid, m_table, n);
662 if (unlikely(ret < n)) {
664 rte_pktmbuf_free(m_table[ret]);
671 /* Enqueue a single packet, and send burst if queue is filled */
673 send_single_packet(struct rte_mbuf *m, uint8_t port)
676 struct thread_tx_conf *qconf;
679 qconf = (struct thread_tx_conf *)lthread_get_data();
681 qconf = (struct thread_tx_conf *)RTE_PER_LCORE(lcore_conf)->data;
683 len = qconf->tx_mbufs[port].len;
684 qconf->tx_mbufs[port].m_table[len] = m;
687 /* enough pkts to be sent */
688 if (unlikely(len == MAX_PKT_BURST)) {
689 send_burst(qconf, MAX_PKT_BURST, port);
693 qconf->tx_mbufs[port].len = len;
697 #if ((APP_LOOKUP_METHOD == APP_LOOKUP_LPM) && \
698 (ENABLE_MULTI_BUFFER_OPTIMIZE == 1))
699 static __rte_always_inline void
700 send_packetsx4(uint8_t port,
701 struct rte_mbuf *m[], uint32_t num)
704 struct thread_tx_conf *qconf;
707 qconf = (struct thread_tx_conf *)lthread_get_data();
709 qconf = (struct thread_tx_conf *)RTE_PER_LCORE(lcore_conf)->data;
711 len = qconf->tx_mbufs[port].len;
714 * If TX buffer for that queue is empty, and we have enough packets,
715 * then send them straightway.
717 if (num >= MAX_TX_BURST && len == 0) {
718 n = rte_eth_tx_burst(port, qconf->tx_queue_id[port], m, num);
719 if (unlikely(n < num)) {
721 rte_pktmbuf_free(m[n]);
728 * Put packets into TX buffer for that queue.
732 n = (n > MAX_PKT_BURST) ? MAX_PKT_BURST - len : num;
735 switch (n % FWDSTEP) {
738 qconf->tx_mbufs[port].m_table[len + j] = m[j];
742 qconf->tx_mbufs[port].m_table[len + j] = m[j];
746 qconf->tx_mbufs[port].m_table[len + j] = m[j];
750 qconf->tx_mbufs[port].m_table[len + j] = m[j];
757 /* enough pkts to be sent */
758 if (unlikely(len == MAX_PKT_BURST)) {
760 send_burst(qconf, MAX_PKT_BURST, port);
762 /* copy rest of the packets into the TX buffer. */
765 switch (len % FWDSTEP) {
768 qconf->tx_mbufs[port].m_table[j] = m[n + j];
772 qconf->tx_mbufs[port].m_table[j] = m[n + j];
776 qconf->tx_mbufs[port].m_table[j] = m[n + j];
780 qconf->tx_mbufs[port].m_table[j] = m[n + j];
786 qconf->tx_mbufs[port].len = len;
788 #endif /* APP_LOOKUP_LPM */
790 #ifdef DO_RFC_1812_CHECKS
792 is_valid_ipv4_pkt(struct ipv4_hdr *pkt, uint32_t link_len)
794 /* From http://www.rfc-editor.org/rfc/rfc1812.txt section 5.2.2 */
796 * 1. The packet length reported by the Link Layer must be large
797 * enough to hold the minimum length legal IP datagram (20 bytes).
799 if (link_len < sizeof(struct ipv4_hdr))
802 /* 2. The IP checksum must be correct. */
803 /* this is checked in H/W */
806 * 3. The IP version number must be 4. If the version number is not 4
807 * then the packet may be another version of IP, such as IPng or
810 if (((pkt->version_ihl) >> 4) != 4)
813 * 4. The IP header length field must be large enough to hold the
814 * minimum length legal IP datagram (20 bytes = 5 words).
816 if ((pkt->version_ihl & 0xf) < 5)
820 * 5. The IP total length field must be large enough to hold the IP
821 * datagram header, whose length is specified in the IP header length
824 if (rte_cpu_to_be_16(pkt->total_length) < sizeof(struct ipv4_hdr))
831 #if (APP_LOOKUP_METHOD == APP_LOOKUP_EXACT_MATCH)
833 static __m128i mask0;
834 static __m128i mask1;
835 static __m128i mask2;
836 static inline uint8_t
837 get_ipv4_dst_port(void *ipv4_hdr, uint8_t portid,
838 lookup_struct_t *ipv4_l3fwd_lookup_struct)
841 union ipv4_5tuple_host key;
843 ipv4_hdr = (uint8_t *)ipv4_hdr + offsetof(struct ipv4_hdr, time_to_live);
844 __m128i data = _mm_loadu_si128((__m128i *)(ipv4_hdr));
845 /* Get 5 tuple: dst port, src port, dst IP address, src IP address and
847 key.xmm = _mm_and_si128(data, mask0);
848 /* Find destination port */
849 ret = rte_hash_lookup(ipv4_l3fwd_lookup_struct, (const void *)&key);
850 return (uint8_t)((ret < 0) ? portid : ipv4_l3fwd_out_if[ret]);
853 static inline uint8_t
854 get_ipv6_dst_port(void *ipv6_hdr, uint8_t portid,
855 lookup_struct_t *ipv6_l3fwd_lookup_struct)
858 union ipv6_5tuple_host key;
860 ipv6_hdr = (uint8_t *)ipv6_hdr + offsetof(struct ipv6_hdr, payload_len);
861 __m128i data0 = _mm_loadu_si128((__m128i *)(ipv6_hdr));
862 __m128i data1 = _mm_loadu_si128((__m128i *)(((uint8_t *)ipv6_hdr) +
864 __m128i data2 = _mm_loadu_si128((__m128i *)(((uint8_t *)ipv6_hdr) +
865 sizeof(__m128i) + sizeof(__m128i)));
866 /* Get part of 5 tuple: src IP address lower 96 bits and protocol */
867 key.xmm[0] = _mm_and_si128(data0, mask1);
868 /* Get part of 5 tuple: dst IP address lower 96 bits and src IP address
871 /* Get part of 5 tuple: dst port and src port and dst IP address higher
873 key.xmm[2] = _mm_and_si128(data2, mask2);
875 /* Find destination port */
876 ret = rte_hash_lookup(ipv6_l3fwd_lookup_struct, (const void *)&key);
877 return (uint8_t)((ret < 0) ? portid : ipv6_l3fwd_out_if[ret]);
881 #if (APP_LOOKUP_METHOD == APP_LOOKUP_LPM)
883 static inline uint8_t
884 get_ipv4_dst_port(void *ipv4_hdr, uint8_t portid,
885 lookup_struct_t *ipv4_l3fwd_lookup_struct)
889 return (uint8_t)((rte_lpm_lookup(ipv4_l3fwd_lookup_struct,
890 rte_be_to_cpu_32(((struct ipv4_hdr *)ipv4_hdr)->dst_addr),
891 &next_hop) == 0) ? next_hop : portid);
894 static inline uint8_t
895 get_ipv6_dst_port(void *ipv6_hdr, uint8_t portid,
896 lookup6_struct_t *ipv6_l3fwd_lookup_struct)
900 return (uint8_t) ((rte_lpm6_lookup(ipv6_l3fwd_lookup_struct,
901 ((struct ipv6_hdr *)ipv6_hdr)->dst_addr, &next_hop) == 0) ?
906 static inline void l3fwd_simple_forward(struct rte_mbuf *m, uint8_t portid)
907 __attribute__((unused));
909 #if ((APP_LOOKUP_METHOD == APP_LOOKUP_EXACT_MATCH) && \
910 (ENABLE_MULTI_BUFFER_OPTIMIZE == 1))
912 #define MASK_ALL_PKTS 0xff
913 #define EXCLUDE_1ST_PKT 0xfe
914 #define EXCLUDE_2ND_PKT 0xfd
915 #define EXCLUDE_3RD_PKT 0xfb
916 #define EXCLUDE_4TH_PKT 0xf7
917 #define EXCLUDE_5TH_PKT 0xef
918 #define EXCLUDE_6TH_PKT 0xdf
919 #define EXCLUDE_7TH_PKT 0xbf
920 #define EXCLUDE_8TH_PKT 0x7f
923 simple_ipv4_fwd_8pkts(struct rte_mbuf *m[8], uint8_t portid)
925 struct ether_hdr *eth_hdr[8];
926 struct ipv4_hdr *ipv4_hdr[8];
929 union ipv4_5tuple_host key[8];
932 eth_hdr[0] = rte_pktmbuf_mtod(m[0], struct ether_hdr *);
933 eth_hdr[1] = rte_pktmbuf_mtod(m[1], struct ether_hdr *);
934 eth_hdr[2] = rte_pktmbuf_mtod(m[2], struct ether_hdr *);
935 eth_hdr[3] = rte_pktmbuf_mtod(m[3], struct ether_hdr *);
936 eth_hdr[4] = rte_pktmbuf_mtod(m[4], struct ether_hdr *);
937 eth_hdr[5] = rte_pktmbuf_mtod(m[5], struct ether_hdr *);
938 eth_hdr[6] = rte_pktmbuf_mtod(m[6], struct ether_hdr *);
939 eth_hdr[7] = rte_pktmbuf_mtod(m[7], struct ether_hdr *);
941 /* Handle IPv4 headers.*/
942 ipv4_hdr[0] = rte_pktmbuf_mtod_offset(m[0], struct ipv4_hdr *,
943 sizeof(struct ether_hdr));
944 ipv4_hdr[1] = rte_pktmbuf_mtod_offset(m[1], struct ipv4_hdr *,
945 sizeof(struct ether_hdr));
946 ipv4_hdr[2] = rte_pktmbuf_mtod_offset(m[2], struct ipv4_hdr *,
947 sizeof(struct ether_hdr));
948 ipv4_hdr[3] = rte_pktmbuf_mtod_offset(m[3], struct ipv4_hdr *,
949 sizeof(struct ether_hdr));
950 ipv4_hdr[4] = rte_pktmbuf_mtod_offset(m[4], struct ipv4_hdr *,
951 sizeof(struct ether_hdr));
952 ipv4_hdr[5] = rte_pktmbuf_mtod_offset(m[5], struct ipv4_hdr *,
953 sizeof(struct ether_hdr));
954 ipv4_hdr[6] = rte_pktmbuf_mtod_offset(m[6], struct ipv4_hdr *,
955 sizeof(struct ether_hdr));
956 ipv4_hdr[7] = rte_pktmbuf_mtod_offset(m[7], struct ipv4_hdr *,
957 sizeof(struct ether_hdr));
959 #ifdef DO_RFC_1812_CHECKS
960 /* Check to make sure the packet is valid (RFC1812) */
961 uint8_t valid_mask = MASK_ALL_PKTS;
963 if (is_valid_ipv4_pkt(ipv4_hdr[0], m[0]->pkt_len) < 0) {
964 rte_pktmbuf_free(m[0]);
965 valid_mask &= EXCLUDE_1ST_PKT;
967 if (is_valid_ipv4_pkt(ipv4_hdr[1], m[1]->pkt_len) < 0) {
968 rte_pktmbuf_free(m[1]);
969 valid_mask &= EXCLUDE_2ND_PKT;
971 if (is_valid_ipv4_pkt(ipv4_hdr[2], m[2]->pkt_len) < 0) {
972 rte_pktmbuf_free(m[2]);
973 valid_mask &= EXCLUDE_3RD_PKT;
975 if (is_valid_ipv4_pkt(ipv4_hdr[3], m[3]->pkt_len) < 0) {
976 rte_pktmbuf_free(m[3]);
977 valid_mask &= EXCLUDE_4TH_PKT;
979 if (is_valid_ipv4_pkt(ipv4_hdr[4], m[4]->pkt_len) < 0) {
980 rte_pktmbuf_free(m[4]);
981 valid_mask &= EXCLUDE_5TH_PKT;
983 if (is_valid_ipv4_pkt(ipv4_hdr[5], m[5]->pkt_len) < 0) {
984 rte_pktmbuf_free(m[5]);
985 valid_mask &= EXCLUDE_6TH_PKT;
987 if (is_valid_ipv4_pkt(ipv4_hdr[6], m[6]->pkt_len) < 0) {
988 rte_pktmbuf_free(m[6]);
989 valid_mask &= EXCLUDE_7TH_PKT;
991 if (is_valid_ipv4_pkt(ipv4_hdr[7], m[7]->pkt_len) < 0) {
992 rte_pktmbuf_free(m[7]);
993 valid_mask &= EXCLUDE_8TH_PKT;
995 if (unlikely(valid_mask != MASK_ALL_PKTS)) {
1001 for (i = 0; i < 8; i++)
1002 if ((0x1 << i) & valid_mask)
1003 l3fwd_simple_forward(m[i], portid);
1005 #endif /* End of #ifdef DO_RFC_1812_CHECKS */
1007 data[0] = _mm_loadu_si128(rte_pktmbuf_mtod_offset(m[0], __m128i *,
1008 sizeof(struct ether_hdr) +
1009 offsetof(struct ipv4_hdr, time_to_live)));
1010 data[1] = _mm_loadu_si128(rte_pktmbuf_mtod_offset(m[1], __m128i *,
1011 sizeof(struct ether_hdr) +
1012 offsetof(struct ipv4_hdr, time_to_live)));
1013 data[2] = _mm_loadu_si128(rte_pktmbuf_mtod_offset(m[2], __m128i *,
1014 sizeof(struct ether_hdr) +
1015 offsetof(struct ipv4_hdr, time_to_live)));
1016 data[3] = _mm_loadu_si128(rte_pktmbuf_mtod_offset(m[3], __m128i *,
1017 sizeof(struct ether_hdr) +
1018 offsetof(struct ipv4_hdr, time_to_live)));
1019 data[4] = _mm_loadu_si128(rte_pktmbuf_mtod_offset(m[4], __m128i *,
1020 sizeof(struct ether_hdr) +
1021 offsetof(struct ipv4_hdr, time_to_live)));
1022 data[5] = _mm_loadu_si128(rte_pktmbuf_mtod_offset(m[5], __m128i *,
1023 sizeof(struct ether_hdr) +
1024 offsetof(struct ipv4_hdr, time_to_live)));
1025 data[6] = _mm_loadu_si128(rte_pktmbuf_mtod_offset(m[6], __m128i *,
1026 sizeof(struct ether_hdr) +
1027 offsetof(struct ipv4_hdr, time_to_live)));
1028 data[7] = _mm_loadu_si128(rte_pktmbuf_mtod_offset(m[7], __m128i *,
1029 sizeof(struct ether_hdr) +
1030 offsetof(struct ipv4_hdr, time_to_live)));
1032 key[0].xmm = _mm_and_si128(data[0], mask0);
1033 key[1].xmm = _mm_and_si128(data[1], mask0);
1034 key[2].xmm = _mm_and_si128(data[2], mask0);
1035 key[3].xmm = _mm_and_si128(data[3], mask0);
1036 key[4].xmm = _mm_and_si128(data[4], mask0);
1037 key[5].xmm = _mm_and_si128(data[5], mask0);
1038 key[6].xmm = _mm_and_si128(data[6], mask0);
1039 key[7].xmm = _mm_and_si128(data[7], mask0);
1041 const void *key_array[8] = {&key[0], &key[1], &key[2], &key[3],
1042 &key[4], &key[5], &key[6], &key[7]};
1044 rte_hash_lookup_bulk(RTE_PER_LCORE(lcore_conf)->ipv4_lookup_struct,
1045 &key_array[0], 8, ret);
1046 dst_port[0] = (uint8_t) ((ret[0] < 0) ? portid : ipv4_l3fwd_out_if[ret[0]]);
1047 dst_port[1] = (uint8_t) ((ret[1] < 0) ? portid : ipv4_l3fwd_out_if[ret[1]]);
1048 dst_port[2] = (uint8_t) ((ret[2] < 0) ? portid : ipv4_l3fwd_out_if[ret[2]]);
1049 dst_port[3] = (uint8_t) ((ret[3] < 0) ? portid : ipv4_l3fwd_out_if[ret[3]]);
1050 dst_port[4] = (uint8_t) ((ret[4] < 0) ? portid : ipv4_l3fwd_out_if[ret[4]]);
1051 dst_port[5] = (uint8_t) ((ret[5] < 0) ? portid : ipv4_l3fwd_out_if[ret[5]]);
1052 dst_port[6] = (uint8_t) ((ret[6] < 0) ? portid : ipv4_l3fwd_out_if[ret[6]]);
1053 dst_port[7] = (uint8_t) ((ret[7] < 0) ? portid : ipv4_l3fwd_out_if[ret[7]]);
1055 if (dst_port[0] >= RTE_MAX_ETHPORTS ||
1056 (enabled_port_mask & 1 << dst_port[0]) == 0)
1057 dst_port[0] = portid;
1058 if (dst_port[1] >= RTE_MAX_ETHPORTS ||
1059 (enabled_port_mask & 1 << dst_port[1]) == 0)
1060 dst_port[1] = portid;
1061 if (dst_port[2] >= RTE_MAX_ETHPORTS ||
1062 (enabled_port_mask & 1 << dst_port[2]) == 0)
1063 dst_port[2] = portid;
1064 if (dst_port[3] >= RTE_MAX_ETHPORTS ||
1065 (enabled_port_mask & 1 << dst_port[3]) == 0)
1066 dst_port[3] = portid;
1067 if (dst_port[4] >= RTE_MAX_ETHPORTS ||
1068 (enabled_port_mask & 1 << dst_port[4]) == 0)
1069 dst_port[4] = portid;
1070 if (dst_port[5] >= RTE_MAX_ETHPORTS ||
1071 (enabled_port_mask & 1 << dst_port[5]) == 0)
1072 dst_port[5] = portid;
1073 if (dst_port[6] >= RTE_MAX_ETHPORTS ||
1074 (enabled_port_mask & 1 << dst_port[6]) == 0)
1075 dst_port[6] = portid;
1076 if (dst_port[7] >= RTE_MAX_ETHPORTS ||
1077 (enabled_port_mask & 1 << dst_port[7]) == 0)
1078 dst_port[7] = portid;
1080 #ifdef DO_RFC_1812_CHECKS
1081 /* Update time to live and header checksum */
1082 --(ipv4_hdr[0]->time_to_live);
1083 --(ipv4_hdr[1]->time_to_live);
1084 --(ipv4_hdr[2]->time_to_live);
1085 --(ipv4_hdr[3]->time_to_live);
1086 ++(ipv4_hdr[0]->hdr_checksum);
1087 ++(ipv4_hdr[1]->hdr_checksum);
1088 ++(ipv4_hdr[2]->hdr_checksum);
1089 ++(ipv4_hdr[3]->hdr_checksum);
1090 --(ipv4_hdr[4]->time_to_live);
1091 --(ipv4_hdr[5]->time_to_live);
1092 --(ipv4_hdr[6]->time_to_live);
1093 --(ipv4_hdr[7]->time_to_live);
1094 ++(ipv4_hdr[4]->hdr_checksum);
1095 ++(ipv4_hdr[5]->hdr_checksum);
1096 ++(ipv4_hdr[6]->hdr_checksum);
1097 ++(ipv4_hdr[7]->hdr_checksum);
1101 *(uint64_t *)ð_hdr[0]->d_addr = dest_eth_addr[dst_port[0]];
1102 *(uint64_t *)ð_hdr[1]->d_addr = dest_eth_addr[dst_port[1]];
1103 *(uint64_t *)ð_hdr[2]->d_addr = dest_eth_addr[dst_port[2]];
1104 *(uint64_t *)ð_hdr[3]->d_addr = dest_eth_addr[dst_port[3]];
1105 *(uint64_t *)ð_hdr[4]->d_addr = dest_eth_addr[dst_port[4]];
1106 *(uint64_t *)ð_hdr[5]->d_addr = dest_eth_addr[dst_port[5]];
1107 *(uint64_t *)ð_hdr[6]->d_addr = dest_eth_addr[dst_port[6]];
1108 *(uint64_t *)ð_hdr[7]->d_addr = dest_eth_addr[dst_port[7]];
1111 ether_addr_copy(&ports_eth_addr[dst_port[0]], ð_hdr[0]->s_addr);
1112 ether_addr_copy(&ports_eth_addr[dst_port[1]], ð_hdr[1]->s_addr);
1113 ether_addr_copy(&ports_eth_addr[dst_port[2]], ð_hdr[2]->s_addr);
1114 ether_addr_copy(&ports_eth_addr[dst_port[3]], ð_hdr[3]->s_addr);
1115 ether_addr_copy(&ports_eth_addr[dst_port[4]], ð_hdr[4]->s_addr);
1116 ether_addr_copy(&ports_eth_addr[dst_port[5]], ð_hdr[5]->s_addr);
1117 ether_addr_copy(&ports_eth_addr[dst_port[6]], ð_hdr[6]->s_addr);
1118 ether_addr_copy(&ports_eth_addr[dst_port[7]], ð_hdr[7]->s_addr);
1120 send_single_packet(m[0], (uint8_t)dst_port[0]);
1121 send_single_packet(m[1], (uint8_t)dst_port[1]);
1122 send_single_packet(m[2], (uint8_t)dst_port[2]);
1123 send_single_packet(m[3], (uint8_t)dst_port[3]);
1124 send_single_packet(m[4], (uint8_t)dst_port[4]);
1125 send_single_packet(m[5], (uint8_t)dst_port[5]);
1126 send_single_packet(m[6], (uint8_t)dst_port[6]);
1127 send_single_packet(m[7], (uint8_t)dst_port[7]);
1131 static inline void get_ipv6_5tuple(struct rte_mbuf *m0, __m128i mask0,
1132 __m128i mask1, union ipv6_5tuple_host *key)
1134 __m128i tmpdata0 = _mm_loadu_si128(rte_pktmbuf_mtod_offset(m0,
1135 __m128i *, sizeof(struct ether_hdr) +
1136 offsetof(struct ipv6_hdr, payload_len)));
1137 __m128i tmpdata1 = _mm_loadu_si128(rte_pktmbuf_mtod_offset(m0,
1138 __m128i *, sizeof(struct ether_hdr) +
1139 offsetof(struct ipv6_hdr, payload_len) + sizeof(__m128i)));
1140 __m128i tmpdata2 = _mm_loadu_si128(rte_pktmbuf_mtod_offset(m0,
1141 __m128i *, sizeof(struct ether_hdr) +
1142 offsetof(struct ipv6_hdr, payload_len) + sizeof(__m128i) +
1144 key->xmm[0] = _mm_and_si128(tmpdata0, mask0);
1145 key->xmm[1] = tmpdata1;
1146 key->xmm[2] = _mm_and_si128(tmpdata2, mask1);
1150 simple_ipv6_fwd_8pkts(struct rte_mbuf *m[8], uint8_t portid)
1153 uint8_t dst_port[8];
1154 struct ether_hdr *eth_hdr[8];
1155 union ipv6_5tuple_host key[8];
1157 __attribute__((unused)) struct ipv6_hdr *ipv6_hdr[8];
1159 eth_hdr[0] = rte_pktmbuf_mtod(m[0], struct ether_hdr *);
1160 eth_hdr[1] = rte_pktmbuf_mtod(m[1], struct ether_hdr *);
1161 eth_hdr[2] = rte_pktmbuf_mtod(m[2], struct ether_hdr *);
1162 eth_hdr[3] = rte_pktmbuf_mtod(m[3], struct ether_hdr *);
1163 eth_hdr[4] = rte_pktmbuf_mtod(m[4], struct ether_hdr *);
1164 eth_hdr[5] = rte_pktmbuf_mtod(m[5], struct ether_hdr *);
1165 eth_hdr[6] = rte_pktmbuf_mtod(m[6], struct ether_hdr *);
1166 eth_hdr[7] = rte_pktmbuf_mtod(m[7], struct ether_hdr *);
1168 /* Handle IPv6 headers.*/
1169 ipv6_hdr[0] = rte_pktmbuf_mtod_offset(m[0], struct ipv6_hdr *,
1170 sizeof(struct ether_hdr));
1171 ipv6_hdr[1] = rte_pktmbuf_mtod_offset(m[1], struct ipv6_hdr *,
1172 sizeof(struct ether_hdr));
1173 ipv6_hdr[2] = rte_pktmbuf_mtod_offset(m[2], struct ipv6_hdr *,
1174 sizeof(struct ether_hdr));
1175 ipv6_hdr[3] = rte_pktmbuf_mtod_offset(m[3], struct ipv6_hdr *,
1176 sizeof(struct ether_hdr));
1177 ipv6_hdr[4] = rte_pktmbuf_mtod_offset(m[4], struct ipv6_hdr *,
1178 sizeof(struct ether_hdr));
1179 ipv6_hdr[5] = rte_pktmbuf_mtod_offset(m[5], struct ipv6_hdr *,
1180 sizeof(struct ether_hdr));
1181 ipv6_hdr[6] = rte_pktmbuf_mtod_offset(m[6], struct ipv6_hdr *,
1182 sizeof(struct ether_hdr));
1183 ipv6_hdr[7] = rte_pktmbuf_mtod_offset(m[7], struct ipv6_hdr *,
1184 sizeof(struct ether_hdr));
1186 get_ipv6_5tuple(m[0], mask1, mask2, &key[0]);
1187 get_ipv6_5tuple(m[1], mask1, mask2, &key[1]);
1188 get_ipv6_5tuple(m[2], mask1, mask2, &key[2]);
1189 get_ipv6_5tuple(m[3], mask1, mask2, &key[3]);
1190 get_ipv6_5tuple(m[4], mask1, mask2, &key[4]);
1191 get_ipv6_5tuple(m[5], mask1, mask2, &key[5]);
1192 get_ipv6_5tuple(m[6], mask1, mask2, &key[6]);
1193 get_ipv6_5tuple(m[7], mask1, mask2, &key[7]);
1195 const void *key_array[8] = {&key[0], &key[1], &key[2], &key[3],
1196 &key[4], &key[5], &key[6], &key[7]};
1198 rte_hash_lookup_bulk(RTE_PER_LCORE(lcore_conf)->ipv6_lookup_struct,
1199 &key_array[0], 4, ret);
1200 dst_port[0] = (uint8_t) ((ret[0] < 0) ? portid : ipv6_l3fwd_out_if[ret[0]]);
1201 dst_port[1] = (uint8_t) ((ret[1] < 0) ? portid : ipv6_l3fwd_out_if[ret[1]]);
1202 dst_port[2] = (uint8_t) ((ret[2] < 0) ? portid : ipv6_l3fwd_out_if[ret[2]]);
1203 dst_port[3] = (uint8_t) ((ret[3] < 0) ? portid : ipv6_l3fwd_out_if[ret[3]]);
1204 dst_port[4] = (uint8_t) ((ret[4] < 0) ? portid : ipv6_l3fwd_out_if[ret[4]]);
1205 dst_port[5] = (uint8_t) ((ret[5] < 0) ? portid : ipv6_l3fwd_out_if[ret[5]]);
1206 dst_port[6] = (uint8_t) ((ret[6] < 0) ? portid : ipv6_l3fwd_out_if[ret[6]]);
1207 dst_port[7] = (uint8_t) ((ret[7] < 0) ? portid : ipv6_l3fwd_out_if[ret[7]]);
1209 if (dst_port[0] >= RTE_MAX_ETHPORTS ||
1210 (enabled_port_mask & 1 << dst_port[0]) == 0)
1211 dst_port[0] = portid;
1212 if (dst_port[1] >= RTE_MAX_ETHPORTS ||
1213 (enabled_port_mask & 1 << dst_port[1]) == 0)
1214 dst_port[1] = portid;
1215 if (dst_port[2] >= RTE_MAX_ETHPORTS ||
1216 (enabled_port_mask & 1 << dst_port[2]) == 0)
1217 dst_port[2] = portid;
1218 if (dst_port[3] >= RTE_MAX_ETHPORTS ||
1219 (enabled_port_mask & 1 << dst_port[3]) == 0)
1220 dst_port[3] = portid;
1221 if (dst_port[4] >= RTE_MAX_ETHPORTS ||
1222 (enabled_port_mask & 1 << dst_port[4]) == 0)
1223 dst_port[4] = portid;
1224 if (dst_port[5] >= RTE_MAX_ETHPORTS ||
1225 (enabled_port_mask & 1 << dst_port[5]) == 0)
1226 dst_port[5] = portid;
1227 if (dst_port[6] >= RTE_MAX_ETHPORTS ||
1228 (enabled_port_mask & 1 << dst_port[6]) == 0)
1229 dst_port[6] = portid;
1230 if (dst_port[7] >= RTE_MAX_ETHPORTS ||
1231 (enabled_port_mask & 1 << dst_port[7]) == 0)
1232 dst_port[7] = portid;
1235 *(uint64_t *)ð_hdr[0]->d_addr = dest_eth_addr[dst_port[0]];
1236 *(uint64_t *)ð_hdr[1]->d_addr = dest_eth_addr[dst_port[1]];
1237 *(uint64_t *)ð_hdr[2]->d_addr = dest_eth_addr[dst_port[2]];
1238 *(uint64_t *)ð_hdr[3]->d_addr = dest_eth_addr[dst_port[3]];
1239 *(uint64_t *)ð_hdr[4]->d_addr = dest_eth_addr[dst_port[4]];
1240 *(uint64_t *)ð_hdr[5]->d_addr = dest_eth_addr[dst_port[5]];
1241 *(uint64_t *)ð_hdr[6]->d_addr = dest_eth_addr[dst_port[6]];
1242 *(uint64_t *)ð_hdr[7]->d_addr = dest_eth_addr[dst_port[7]];
1245 ether_addr_copy(&ports_eth_addr[dst_port[0]], ð_hdr[0]->s_addr);
1246 ether_addr_copy(&ports_eth_addr[dst_port[1]], ð_hdr[1]->s_addr);
1247 ether_addr_copy(&ports_eth_addr[dst_port[2]], ð_hdr[2]->s_addr);
1248 ether_addr_copy(&ports_eth_addr[dst_port[3]], ð_hdr[3]->s_addr);
1249 ether_addr_copy(&ports_eth_addr[dst_port[4]], ð_hdr[4]->s_addr);
1250 ether_addr_copy(&ports_eth_addr[dst_port[5]], ð_hdr[5]->s_addr);
1251 ether_addr_copy(&ports_eth_addr[dst_port[6]], ð_hdr[6]->s_addr);
1252 ether_addr_copy(&ports_eth_addr[dst_port[7]], ð_hdr[7]->s_addr);
1254 send_single_packet(m[0], (uint8_t)dst_port[0]);
1255 send_single_packet(m[1], (uint8_t)dst_port[1]);
1256 send_single_packet(m[2], (uint8_t)dst_port[2]);
1257 send_single_packet(m[3], (uint8_t)dst_port[3]);
1258 send_single_packet(m[4], (uint8_t)dst_port[4]);
1259 send_single_packet(m[5], (uint8_t)dst_port[5]);
1260 send_single_packet(m[6], (uint8_t)dst_port[6]);
1261 send_single_packet(m[7], (uint8_t)dst_port[7]);
1264 #endif /* APP_LOOKUP_METHOD */
1266 static __rte_always_inline void
1267 l3fwd_simple_forward(struct rte_mbuf *m, uint8_t portid)
1269 struct ether_hdr *eth_hdr;
1270 struct ipv4_hdr *ipv4_hdr;
1273 eth_hdr = rte_pktmbuf_mtod(m, struct ether_hdr *);
1275 if (RTE_ETH_IS_IPV4_HDR(m->packet_type)) {
1276 /* Handle IPv4 headers.*/
1277 ipv4_hdr = rte_pktmbuf_mtod_offset(m, struct ipv4_hdr *,
1278 sizeof(struct ether_hdr));
1280 #ifdef DO_RFC_1812_CHECKS
1281 /* Check to make sure the packet is valid (RFC1812) */
1282 if (is_valid_ipv4_pkt(ipv4_hdr, m->pkt_len) < 0) {
1283 rte_pktmbuf_free(m);
1288 dst_port = get_ipv4_dst_port(ipv4_hdr, portid,
1289 RTE_PER_LCORE(lcore_conf)->ipv4_lookup_struct);
1290 if (dst_port >= RTE_MAX_ETHPORTS ||
1291 (enabled_port_mask & 1 << dst_port) == 0)
1294 #ifdef DO_RFC_1812_CHECKS
1295 /* Update time to live and header checksum */
1296 --(ipv4_hdr->time_to_live);
1297 ++(ipv4_hdr->hdr_checksum);
1300 *(uint64_t *)ð_hdr->d_addr = dest_eth_addr[dst_port];
1303 ether_addr_copy(&ports_eth_addr[dst_port], ð_hdr->s_addr);
1305 send_single_packet(m, dst_port);
1306 } else if (RTE_ETH_IS_IPV6_HDR(m->packet_type)) {
1307 /* Handle IPv6 headers.*/
1308 struct ipv6_hdr *ipv6_hdr;
1310 ipv6_hdr = rte_pktmbuf_mtod_offset(m, struct ipv6_hdr *,
1311 sizeof(struct ether_hdr));
1313 dst_port = get_ipv6_dst_port(ipv6_hdr, portid,
1314 RTE_PER_LCORE(lcore_conf)->ipv6_lookup_struct);
1316 if (dst_port >= RTE_MAX_ETHPORTS ||
1317 (enabled_port_mask & 1 << dst_port) == 0)
1321 *(uint64_t *)ð_hdr->d_addr = dest_eth_addr[dst_port];
1324 ether_addr_copy(&ports_eth_addr[dst_port], ð_hdr->s_addr);
1326 send_single_packet(m, dst_port);
1328 /* Free the mbuf that contains non-IPV4/IPV6 packet */
1329 rte_pktmbuf_free(m);
1332 #if ((APP_LOOKUP_METHOD == APP_LOOKUP_LPM) && \
1333 (ENABLE_MULTI_BUFFER_OPTIMIZE == 1))
1334 #ifdef DO_RFC_1812_CHECKS
1336 #define IPV4_MIN_VER_IHL 0x45
1337 #define IPV4_MAX_VER_IHL 0x4f
1338 #define IPV4_MAX_VER_IHL_DIFF (IPV4_MAX_VER_IHL - IPV4_MIN_VER_IHL)
1340 /* Minimum value of IPV4 total length (20B) in network byte order. */
1341 #define IPV4_MIN_LEN_BE (sizeof(struct ipv4_hdr) << 8)
1344 * From http://www.rfc-editor.org/rfc/rfc1812.txt section 5.2.2:
1345 * - The IP version number must be 4.
1346 * - The IP header length field must be large enough to hold the
1347 * minimum length legal IP datagram (20 bytes = 5 words).
1348 * - The IP total length field must be large enough to hold the IP
1349 * datagram header, whose length is specified in the IP header length
1351 * If we encounter invalid IPV4 packet, then set destination port for it
1352 * to BAD_PORT value.
1354 static __rte_always_inline void
1355 rfc1812_process(struct ipv4_hdr *ipv4_hdr, uint16_t *dp, uint32_t ptype)
1359 if (RTE_ETH_IS_IPV4_HDR(ptype)) {
1360 ihl = ipv4_hdr->version_ihl - IPV4_MIN_VER_IHL;
1362 ipv4_hdr->time_to_live--;
1363 ipv4_hdr->hdr_checksum++;
1365 if (ihl > IPV4_MAX_VER_IHL_DIFF ||
1366 ((uint8_t)ipv4_hdr->total_length == 0 &&
1367 ipv4_hdr->total_length < IPV4_MIN_LEN_BE)) {
1374 #define rfc1812_process(mb, dp, ptype) do { } while (0)
1375 #endif /* DO_RFC_1812_CHECKS */
1376 #endif /* APP_LOOKUP_LPM && ENABLE_MULTI_BUFFER_OPTIMIZE */
1379 #if ((APP_LOOKUP_METHOD == APP_LOOKUP_LPM) && \
1380 (ENABLE_MULTI_BUFFER_OPTIMIZE == 1))
1382 static __rte_always_inline uint16_t
1383 get_dst_port(struct rte_mbuf *pkt, uint32_t dst_ipv4, uint8_t portid)
1386 struct ipv6_hdr *ipv6_hdr;
1387 struct ether_hdr *eth_hdr;
1389 if (RTE_ETH_IS_IPV4_HDR(pkt->packet_type)) {
1390 return (uint16_t) ((rte_lpm_lookup(
1391 RTE_PER_LCORE(lcore_conf)->ipv4_lookup_struct, dst_ipv4,
1392 &next_hop) == 0) ? next_hop : portid);
1394 } else if (RTE_ETH_IS_IPV6_HDR(pkt->packet_type)) {
1396 eth_hdr = rte_pktmbuf_mtod(pkt, struct ether_hdr *);
1397 ipv6_hdr = (struct ipv6_hdr *)(eth_hdr + 1);
1399 return (uint16_t) ((rte_lpm6_lookup(
1400 RTE_PER_LCORE(lcore_conf)->ipv6_lookup_struct,
1401 ipv6_hdr->dst_addr, &next_hop) == 0) ?
1410 process_packet(struct rte_mbuf *pkt, uint16_t *dst_port, uint8_t portid)
1412 struct ether_hdr *eth_hdr;
1413 struct ipv4_hdr *ipv4_hdr;
1418 eth_hdr = rte_pktmbuf_mtod(pkt, struct ether_hdr *);
1419 ipv4_hdr = (struct ipv4_hdr *)(eth_hdr + 1);
1421 dst_ipv4 = ipv4_hdr->dst_addr;
1422 dst_ipv4 = rte_be_to_cpu_32(dst_ipv4);
1423 dp = get_dst_port(pkt, dst_ipv4, portid);
1425 te = _mm_load_si128((__m128i *)eth_hdr);
1429 rfc1812_process(ipv4_hdr, dst_port, pkt->packet_type);
1431 te = _mm_blend_epi16(te, ve, MASK_ETH);
1432 _mm_store_si128((__m128i *)eth_hdr, te);
1436 * Read packet_type and destination IPV4 addresses from 4 mbufs.
1439 processx4_step1(struct rte_mbuf *pkt[FWDSTEP],
1441 uint32_t *ipv4_flag)
1443 struct ipv4_hdr *ipv4_hdr;
1444 struct ether_hdr *eth_hdr;
1445 uint32_t x0, x1, x2, x3;
1447 eth_hdr = rte_pktmbuf_mtod(pkt[0], struct ether_hdr *);
1448 ipv4_hdr = (struct ipv4_hdr *)(eth_hdr + 1);
1449 x0 = ipv4_hdr->dst_addr;
1450 ipv4_flag[0] = pkt[0]->packet_type & RTE_PTYPE_L3_IPV4;
1452 eth_hdr = rte_pktmbuf_mtod(pkt[1], struct ether_hdr *);
1453 ipv4_hdr = (struct ipv4_hdr *)(eth_hdr + 1);
1454 x1 = ipv4_hdr->dst_addr;
1455 ipv4_flag[0] &= pkt[1]->packet_type;
1457 eth_hdr = rte_pktmbuf_mtod(pkt[2], struct ether_hdr *);
1458 ipv4_hdr = (struct ipv4_hdr *)(eth_hdr + 1);
1459 x2 = ipv4_hdr->dst_addr;
1460 ipv4_flag[0] &= pkt[2]->packet_type;
1462 eth_hdr = rte_pktmbuf_mtod(pkt[3], struct ether_hdr *);
1463 ipv4_hdr = (struct ipv4_hdr *)(eth_hdr + 1);
1464 x3 = ipv4_hdr->dst_addr;
1465 ipv4_flag[0] &= pkt[3]->packet_type;
1467 dip[0] = _mm_set_epi32(x3, x2, x1, x0);
1471 * Lookup into LPM for destination port.
1472 * If lookup fails, use incoming port (portid) as destination port.
1475 processx4_step2(__m128i dip,
1478 struct rte_mbuf *pkt[FWDSTEP],
1479 uint16_t dprt[FWDSTEP])
1482 const __m128i bswap_mask = _mm_set_epi8(12, 13, 14, 15, 8, 9, 10, 11,
1483 4, 5, 6, 7, 0, 1, 2, 3);
1485 /* Byte swap 4 IPV4 addresses. */
1486 dip = _mm_shuffle_epi8(dip, bswap_mask);
1488 /* if all 4 packets are IPV4. */
1489 if (likely(ipv4_flag)) {
1490 rte_lpm_lookupx4(RTE_PER_LCORE(lcore_conf)->ipv4_lookup_struct, dip,
1493 /* get rid of unused upper 16 bit for each dport. */
1494 dst.x = _mm_packs_epi32(dst.x, dst.x);
1495 *(uint64_t *)dprt = dst.u64[0];
1498 dprt[0] = get_dst_port(pkt[0], dst.u32[0], portid);
1499 dprt[1] = get_dst_port(pkt[1], dst.u32[1], portid);
1500 dprt[2] = get_dst_port(pkt[2], dst.u32[2], portid);
1501 dprt[3] = get_dst_port(pkt[3], dst.u32[3], portid);
1506 * Update source and destination MAC addresses in the ethernet header.
1507 * Perform RFC1812 checks and updates for IPV4 packets.
1510 processx4_step3(struct rte_mbuf *pkt[FWDSTEP], uint16_t dst_port[FWDSTEP])
1512 __m128i te[FWDSTEP];
1513 __m128i ve[FWDSTEP];
1514 __m128i *p[FWDSTEP];
1516 p[0] = rte_pktmbuf_mtod(pkt[0], __m128i *);
1517 p[1] = rte_pktmbuf_mtod(pkt[1], __m128i *);
1518 p[2] = rte_pktmbuf_mtod(pkt[2], __m128i *);
1519 p[3] = rte_pktmbuf_mtod(pkt[3], __m128i *);
1521 ve[0] = val_eth[dst_port[0]];
1522 te[0] = _mm_load_si128(p[0]);
1524 ve[1] = val_eth[dst_port[1]];
1525 te[1] = _mm_load_si128(p[1]);
1527 ve[2] = val_eth[dst_port[2]];
1528 te[2] = _mm_load_si128(p[2]);
1530 ve[3] = val_eth[dst_port[3]];
1531 te[3] = _mm_load_si128(p[3]);
1533 /* Update first 12 bytes, keep rest bytes intact. */
1534 te[0] = _mm_blend_epi16(te[0], ve[0], MASK_ETH);
1535 te[1] = _mm_blend_epi16(te[1], ve[1], MASK_ETH);
1536 te[2] = _mm_blend_epi16(te[2], ve[2], MASK_ETH);
1537 te[3] = _mm_blend_epi16(te[3], ve[3], MASK_ETH);
1539 _mm_store_si128(p[0], te[0]);
1540 _mm_store_si128(p[1], te[1]);
1541 _mm_store_si128(p[2], te[2]);
1542 _mm_store_si128(p[3], te[3]);
1544 rfc1812_process((struct ipv4_hdr *)((struct ether_hdr *)p[0] + 1),
1545 &dst_port[0], pkt[0]->packet_type);
1546 rfc1812_process((struct ipv4_hdr *)((struct ether_hdr *)p[1] + 1),
1547 &dst_port[1], pkt[1]->packet_type);
1548 rfc1812_process((struct ipv4_hdr *)((struct ether_hdr *)p[2] + 1),
1549 &dst_port[2], pkt[2]->packet_type);
1550 rfc1812_process((struct ipv4_hdr *)((struct ether_hdr *)p[3] + 1),
1551 &dst_port[3], pkt[3]->packet_type);
1555 * We group consecutive packets with the same destionation port into one burst.
1556 * To avoid extra latency this is done together with some other packet
1557 * processing, but after we made a final decision about packet's destination.
1558 * To do this we maintain:
1559 * pnum - array of number of consecutive packets with the same dest port for
1560 * each packet in the input burst.
1561 * lp - pointer to the last updated element in the pnum.
1562 * dlp - dest port value lp corresponds to.
1565 #define GRPSZ (1 << FWDSTEP)
1566 #define GRPMSK (GRPSZ - 1)
1568 #define GROUP_PORT_STEP(dlp, dcp, lp, pn, idx) do { \
1569 if (likely((dlp) == (dcp)[(idx)])) { \
1572 (dlp) = (dcp)[idx]; \
1573 (lp) = (pn) + (idx); \
1579 * Group consecutive packets with the same destination port in bursts of 4.
1580 * Suppose we have array of destionation ports:
1581 * dst_port[] = {a, b, c, d,, e, ... }
1582 * dp1 should contain: <a, b, c, d>, dp2: <b, c, d, e>.
1583 * We doing 4 comparisons at once and the result is 4 bit mask.
1584 * This mask is used as an index into prebuild array of pnum values.
1586 static inline uint16_t *
1587 port_groupx4(uint16_t pn[FWDSTEP + 1], uint16_t *lp, __m128i dp1, __m128i dp2)
1589 static const struct {
1590 uint64_t pnum; /* prebuild 4 values for pnum[]. */
1591 int32_t idx; /* index for new last updated elemnet. */
1592 uint16_t lpv; /* add value to the last updated element. */
1595 /* 0: a != b, b != c, c != d, d != e */
1596 .pnum = UINT64_C(0x0001000100010001),
1601 /* 1: a == b, b != c, c != d, d != e */
1602 .pnum = UINT64_C(0x0001000100010002),
1607 /* 2: a != b, b == c, c != d, d != e */
1608 .pnum = UINT64_C(0x0001000100020001),
1613 /* 3: a == b, b == c, c != d, d != e */
1614 .pnum = UINT64_C(0x0001000100020003),
1619 /* 4: a != b, b != c, c == d, d != e */
1620 .pnum = UINT64_C(0x0001000200010001),
1625 /* 5: a == b, b != c, c == d, d != e */
1626 .pnum = UINT64_C(0x0001000200010002),
1631 /* 6: a != b, b == c, c == d, d != e */
1632 .pnum = UINT64_C(0x0001000200030001),
1637 /* 7: a == b, b == c, c == d, d != e */
1638 .pnum = UINT64_C(0x0001000200030004),
1643 /* 8: a != b, b != c, c != d, d == e */
1644 .pnum = UINT64_C(0x0002000100010001),
1649 /* 9: a == b, b != c, c != d, d == e */
1650 .pnum = UINT64_C(0x0002000100010002),
1655 /* 0xa: a != b, b == c, c != d, d == e */
1656 .pnum = UINT64_C(0x0002000100020001),
1661 /* 0xb: a == b, b == c, c != d, d == e */
1662 .pnum = UINT64_C(0x0002000100020003),
1667 /* 0xc: a != b, b != c, c == d, d == e */
1668 .pnum = UINT64_C(0x0002000300010001),
1673 /* 0xd: a == b, b != c, c == d, d == e */
1674 .pnum = UINT64_C(0x0002000300010002),
1679 /* 0xe: a != b, b == c, c == d, d == e */
1680 .pnum = UINT64_C(0x0002000300040001),
1685 /* 0xf: a == b, b == c, c == d, d == e */
1686 .pnum = UINT64_C(0x0002000300040005),
1693 uint16_t u16[FWDSTEP + 1];
1695 } *pnum = (void *)pn;
1699 dp1 = _mm_cmpeq_epi16(dp1, dp2);
1700 dp1 = _mm_unpacklo_epi16(dp1, dp1);
1701 v = _mm_movemask_ps((__m128)dp1);
1703 /* update last port counter. */
1704 lp[0] += gptbl[v].lpv;
1706 /* if dest port value has changed. */
1708 pnum->u64 = gptbl[v].pnum;
1709 pnum->u16[FWDSTEP] = 1;
1710 lp = pnum->u16 + gptbl[v].idx;
1716 #endif /* APP_LOOKUP_METHOD */
1719 process_burst(struct rte_mbuf *pkts_burst[MAX_PKT_BURST], int nb_rx,
1724 #if ((APP_LOOKUP_METHOD == APP_LOOKUP_LPM) && \
1725 (ENABLE_MULTI_BUFFER_OPTIMIZE == 1))
1729 uint16_t dst_port[MAX_PKT_BURST];
1730 __m128i dip[MAX_PKT_BURST / FWDSTEP];
1731 uint32_t ipv4_flag[MAX_PKT_BURST / FWDSTEP];
1732 uint16_t pnum[MAX_PKT_BURST + 1];
1736 #if (ENABLE_MULTI_BUFFER_OPTIMIZE == 1)
1737 #if (APP_LOOKUP_METHOD == APP_LOOKUP_EXACT_MATCH)
1740 * Send nb_rx - nb_rx%8 packets
1743 int32_t n = RTE_ALIGN_FLOOR(nb_rx, 8);
1745 for (j = 0; j < n; j += 8) {
1747 pkts_burst[j]->packet_type &
1748 pkts_burst[j+1]->packet_type &
1749 pkts_burst[j+2]->packet_type &
1750 pkts_burst[j+3]->packet_type &
1751 pkts_burst[j+4]->packet_type &
1752 pkts_burst[j+5]->packet_type &
1753 pkts_burst[j+6]->packet_type &
1754 pkts_burst[j+7]->packet_type;
1755 if (pkt_type & RTE_PTYPE_L3_IPV4) {
1756 simple_ipv4_fwd_8pkts(&pkts_burst[j], portid);
1757 } else if (pkt_type &
1758 RTE_PTYPE_L3_IPV6) {
1759 simple_ipv6_fwd_8pkts(&pkts_burst[j], portid);
1761 l3fwd_simple_forward(pkts_burst[j], portid);
1762 l3fwd_simple_forward(pkts_burst[j+1], portid);
1763 l3fwd_simple_forward(pkts_burst[j+2], portid);
1764 l3fwd_simple_forward(pkts_burst[j+3], portid);
1765 l3fwd_simple_forward(pkts_burst[j+4], portid);
1766 l3fwd_simple_forward(pkts_burst[j+5], portid);
1767 l3fwd_simple_forward(pkts_burst[j+6], portid);
1768 l3fwd_simple_forward(pkts_burst[j+7], portid);
1771 for (; j < nb_rx ; j++)
1772 l3fwd_simple_forward(pkts_burst[j], portid);
1774 #elif (APP_LOOKUP_METHOD == APP_LOOKUP_LPM)
1776 k = RTE_ALIGN_FLOOR(nb_rx, FWDSTEP);
1777 for (j = 0; j != k; j += FWDSTEP)
1778 processx4_step1(&pkts_burst[j], &dip[j / FWDSTEP],
1779 &ipv4_flag[j / FWDSTEP]);
1781 k = RTE_ALIGN_FLOOR(nb_rx, FWDSTEP);
1782 for (j = 0; j != k; j += FWDSTEP)
1783 processx4_step2(dip[j / FWDSTEP], ipv4_flag[j / FWDSTEP],
1784 portid, &pkts_burst[j], &dst_port[j]);
1787 * Finish packet processing and group consecutive
1788 * packets with the same destination port.
1790 k = RTE_ALIGN_FLOOR(nb_rx, FWDSTEP);
1797 processx4_step3(pkts_burst, dst_port);
1799 /* dp1: <d[0], d[1], d[2], d[3], ... > */
1800 dp1 = _mm_loadu_si128((__m128i *)dst_port);
1802 for (j = FWDSTEP; j != k; j += FWDSTEP) {
1803 processx4_step3(&pkts_burst[j], &dst_port[j]);
1807 * <d[j-3], d[j-2], d[j-1], d[j], ... >
1809 dp2 = _mm_loadu_si128(
1810 (__m128i *)&dst_port[j - FWDSTEP + 1]);
1811 lp = port_groupx4(&pnum[j - FWDSTEP], lp, dp1, dp2);
1815 * <d[j], d[j+1], d[j+2], d[j+3], ... >
1817 dp1 = _mm_srli_si128(dp2, (FWDSTEP - 1) *
1818 sizeof(dst_port[0]));
1822 * dp2: <d[j-3], d[j-2], d[j-1], d[j-1], ... >
1824 dp2 = _mm_shufflelo_epi16(dp1, 0xf9);
1825 lp = port_groupx4(&pnum[j - FWDSTEP], lp, dp1, dp2);
1828 * remove values added by the last repeated
1832 dlp = dst_port[j - 1];
1834 /* set dlp and lp to the never used values. */
1836 lp = pnum + MAX_PKT_BURST;
1839 /* Process up to last 3 packets one by one. */
1840 switch (nb_rx % FWDSTEP) {
1842 process_packet(pkts_burst[j], dst_port + j, portid);
1843 GROUP_PORT_STEP(dlp, dst_port, lp, pnum, j);
1847 process_packet(pkts_burst[j], dst_port + j, portid);
1848 GROUP_PORT_STEP(dlp, dst_port, lp, pnum, j);
1852 process_packet(pkts_burst[j], dst_port + j, portid);
1853 GROUP_PORT_STEP(dlp, dst_port, lp, pnum, j);
1858 * Send packets out, through destination port.
1859 * Consecuteve pacekts with the same destination port
1860 * are already grouped together.
1861 * If destination port for the packet equals BAD_PORT,
1862 * then free the packet without sending it out.
1864 for (j = 0; j < nb_rx; j += k) {
1872 if (likely(pn != BAD_PORT))
1873 send_packetsx4(pn, pkts_burst + j, k);
1875 for (m = j; m != j + k; m++)
1876 rte_pktmbuf_free(pkts_burst[m]);
1880 #endif /* APP_LOOKUP_METHOD */
1881 #else /* ENABLE_MULTI_BUFFER_OPTIMIZE == 0 */
1883 /* Prefetch first packets */
1884 for (j = 0; j < PREFETCH_OFFSET && j < nb_rx; j++)
1885 rte_prefetch0(rte_pktmbuf_mtod(pkts_burst[j], void *));
1887 /* Prefetch and forward already prefetched packets */
1888 for (j = 0; j < (nb_rx - PREFETCH_OFFSET); j++) {
1889 rte_prefetch0(rte_pktmbuf_mtod(pkts_burst[
1890 j + PREFETCH_OFFSET], void *));
1891 l3fwd_simple_forward(pkts_burst[j], portid);
1894 /* Forward remaining prefetched packets */
1895 for (; j < nb_rx; j++)
1896 l3fwd_simple_forward(pkts_burst[j], portid);
1898 #endif /* ENABLE_MULTI_BUFFER_OPTIMIZE */
1902 #if (APP_CPU_LOAD > 0)
1905 * CPU-load stats collector
1908 cpu_load_collector(__rte_unused void *arg) {
1911 uint64_t prev_tsc, diff_tsc, cur_tsc;
1912 uint64_t total[MAX_CPU] = { 0 };
1913 unsigned min_cpu = MAX_CPU;
1914 unsigned max_cpu = 0;
1919 unsigned int n_thread_per_cpu[MAX_CPU] = { 0 };
1920 struct thread_conf *thread_per_cpu[MAX_CPU][MAX_THREAD];
1922 struct thread_conf *thread_conf;
1924 const uint64_t interval_tsc = (rte_get_tsc_hz() + US_PER_S - 1) /
1925 US_PER_S * CPU_LOAD_TIMEOUT_US;
1929 * Wait for all threads
1932 printf("Waiting for %d rx threads and %d tx threads\n", n_rx_thread,
1935 while (rte_atomic16_read(&rx_counter) < n_rx_thread)
1938 while (rte_atomic16_read(&tx_counter) < n_tx_thread)
1941 for (i = 0; i < n_rx_thread; i++) {
1943 thread_conf = &rx_thread[i].conf;
1944 cpu_id = thread_conf->cpu_id;
1945 thread_per_cpu[cpu_id][n_thread_per_cpu[cpu_id]++] = thread_conf;
1947 if (cpu_id > max_cpu)
1949 if (cpu_id < min_cpu)
1952 for (i = 0; i < n_tx_thread; i++) {
1954 thread_conf = &tx_thread[i].conf;
1955 cpu_id = thread_conf->cpu_id;
1956 thread_per_cpu[cpu_id][n_thread_per_cpu[cpu_id]++] = thread_conf;
1958 if (thread_conf->cpu_id > max_cpu)
1959 max_cpu = thread_conf->cpu_id;
1960 if (thread_conf->cpu_id < min_cpu)
1961 min_cpu = thread_conf->cpu_id;
1967 for (i = min_cpu; i <= max_cpu; i++) {
1968 for (j = 0; j < MAX_CPU_COUNTER; j++) {
1969 for (k = 0; k < n_thread_per_cpu[i]; k++)
1970 if (thread_per_cpu[i][k]->busy[j]) {
1975 cpu_load.hits[j][i]++;
1987 cur_tsc = rte_rdtsc();
1989 diff_tsc = cur_tsc - prev_tsc;
1990 if (unlikely(diff_tsc > interval_tsc)) {
1994 printf("Cpu usage for %d rx threads and %d tx threads:\n\n",
1995 n_rx_thread, n_tx_thread);
1997 printf("cpu# proc%% poll%% overhead%%\n\n");
1999 for (i = min_cpu; i <= max_cpu; i++) {
2001 printf("CPU %d:", i);
2002 for (j = 0; j < MAX_CPU_COUNTER; j++) {
2003 printf("%7" PRIu64 "",
2004 cpu_load.hits[j][i] * 100 / cpu_load.counter);
2005 hits += cpu_load.hits[j][i];
2006 cpu_load.hits[j][i] = 0;
2008 printf("%7" PRIu64 "\n",
2009 100 - total[i] * 100 / cpu_load.counter);
2012 cpu_load.counter = 0;
2019 #endif /* APP_CPU_LOAD */
2022 * Null processing lthread loop
2024 * This loop is used to start empty scheduler on lcore.
2027 lthread_null(__rte_unused void *args)
2029 int lcore_id = rte_lcore_id();
2031 RTE_LOG(INFO, L3FWD, "Starting scheduler on lcore %d.\n", lcore_id);
2035 /* main processing loop */
2037 lthread_tx_per_ring(void *dummy)
2041 struct rte_ring *ring;
2042 struct thread_tx_conf *tx_conf;
2043 struct rte_mbuf *pkts_burst[MAX_PKT_BURST];
2044 struct lthread_cond *ready;
2046 tx_conf = (struct thread_tx_conf *)dummy;
2047 ring = tx_conf->ring;
2048 ready = *tx_conf->ready;
2050 lthread_set_data((void *)tx_conf);
2053 * Move this lthread to lcore
2055 lthread_set_affinity(tx_conf->conf.lcore_id);
2057 RTE_LOG(INFO, L3FWD, "entering main tx loop on lcore %u\n", rte_lcore_id());
2060 rte_atomic16_inc(&tx_counter);
2064 * Read packet from ring
2066 SET_CPU_BUSY(tx_conf, CPU_POLL);
2067 nb_rx = rte_ring_sc_dequeue_burst(ring, (void **)pkts_burst,
2068 MAX_PKT_BURST, NULL);
2069 SET_CPU_IDLE(tx_conf, CPU_POLL);
2072 SET_CPU_BUSY(tx_conf, CPU_PROCESS);
2073 portid = pkts_burst[0]->port;
2074 process_burst(pkts_burst, nb_rx, portid);
2075 SET_CPU_IDLE(tx_conf, CPU_PROCESS);
2078 lthread_cond_wait(ready, 0);
2084 * Main tx-lthreads spawner lthread.
2086 * This lthread is used to spawn one new lthread per ring from producers.
2090 lthread_tx(void *args)
2096 struct thread_tx_conf *tx_conf;
2098 tx_conf = (struct thread_tx_conf *)args;
2099 lthread_set_data((void *)tx_conf);
2102 * Move this lthread to the selected lcore
2104 lthread_set_affinity(tx_conf->conf.lcore_id);
2107 * Spawn tx readers (one per input ring)
2109 lthread_create(<, tx_conf->conf.lcore_id, lthread_tx_per_ring,
2112 lcore_id = rte_lcore_id();
2114 RTE_LOG(INFO, L3FWD, "Entering Tx main loop on lcore %u\n", lcore_id);
2116 tx_conf->conf.cpu_id = sched_getcpu();
2119 lthread_sleep(BURST_TX_DRAIN_US * 1000);
2122 * TX burst queue drain
2124 for (portid = 0; portid < RTE_MAX_ETHPORTS; portid++) {
2125 if (tx_conf->tx_mbufs[portid].len == 0)
2127 SET_CPU_BUSY(tx_conf, CPU_PROCESS);
2128 send_burst(tx_conf, tx_conf->tx_mbufs[portid].len, portid);
2129 SET_CPU_IDLE(tx_conf, CPU_PROCESS);
2130 tx_conf->tx_mbufs[portid].len = 0;
2137 lthread_rx(void *dummy)
2142 uint8_t portid, queueid;
2144 int len[RTE_MAX_LCORE] = { 0 };
2145 int old_len, new_len;
2146 struct rte_mbuf *pkts_burst[MAX_PKT_BURST];
2147 struct thread_rx_conf *rx_conf;
2149 rx_conf = (struct thread_rx_conf *)dummy;
2150 lthread_set_data((void *)rx_conf);
2153 * Move this lthread to lcore
2155 lthread_set_affinity(rx_conf->conf.lcore_id);
2157 if (rx_conf->n_rx_queue == 0) {
2158 RTE_LOG(INFO, L3FWD, "lcore %u has nothing to do\n", rte_lcore_id());
2162 RTE_LOG(INFO, L3FWD, "Entering main Rx loop on lcore %u\n", rte_lcore_id());
2164 for (i = 0; i < rx_conf->n_rx_queue; i++) {
2166 portid = rx_conf->rx_queue_list[i].port_id;
2167 queueid = rx_conf->rx_queue_list[i].queue_id;
2168 RTE_LOG(INFO, L3FWD, " -- lcoreid=%u portid=%hhu rxqueueid=%hhu\n",
2169 rte_lcore_id(), portid, queueid);
2173 * Init all condition variables (one per rx thread)
2175 for (i = 0; i < rx_conf->n_rx_queue; i++)
2176 lthread_cond_init(NULL, &rx_conf->ready[i], NULL);
2180 rx_conf->conf.cpu_id = sched_getcpu();
2181 rte_atomic16_inc(&rx_counter);
2185 * Read packet from RX queues
2187 for (i = 0; i < rx_conf->n_rx_queue; ++i) {
2188 portid = rx_conf->rx_queue_list[i].port_id;
2189 queueid = rx_conf->rx_queue_list[i].queue_id;
2191 SET_CPU_BUSY(rx_conf, CPU_POLL);
2192 nb_rx = rte_eth_rx_burst(portid, queueid, pkts_burst,
2194 SET_CPU_IDLE(rx_conf, CPU_POLL);
2197 worker_id = (worker_id + 1) % rx_conf->n_ring;
2198 old_len = len[worker_id];
2200 SET_CPU_BUSY(rx_conf, CPU_PROCESS);
2201 ret = rte_ring_sp_enqueue_burst(
2202 rx_conf->ring[worker_id],
2203 (void **) pkts_burst,
2206 new_len = old_len + ret;
2208 if (new_len >= BURST_SIZE) {
2209 lthread_cond_signal(rx_conf->ready[worker_id]);
2213 len[worker_id] = new_len;
2215 if (unlikely(ret < nb_rx)) {
2218 for (k = ret; k < nb_rx; k++) {
2219 struct rte_mbuf *m = pkts_burst[k];
2221 rte_pktmbuf_free(m);
2224 SET_CPU_IDLE(rx_conf, CPU_PROCESS);
2233 * Start scheduler with initial lthread on lcore
2235 * This lthread loop spawns all rx and tx lthreads on master lcore
2239 lthread_spawner(__rte_unused void *arg) {
2240 struct lthread *lt[MAX_THREAD];
2244 printf("Entering lthread_spawner\n");
2247 * Create producers (rx threads) on default lcore
2249 for (i = 0; i < n_rx_thread; i++) {
2250 rx_thread[i].conf.thread_id = i;
2251 lthread_create(<[n_thread], -1, lthread_rx,
2252 (void *)&rx_thread[i]);
2257 * Wait for all producers. Until some producers can be started on the same
2258 * scheduler as this lthread, yielding is required to let them to run and
2259 * prevent deadlock here.
2261 while (rte_atomic16_read(&rx_counter) < n_rx_thread)
2262 lthread_sleep(100000);
2265 * Create consumers (tx threads) on default lcore_id
2267 for (i = 0; i < n_tx_thread; i++) {
2268 tx_thread[i].conf.thread_id = i;
2269 lthread_create(<[n_thread], -1, lthread_tx,
2270 (void *)&tx_thread[i]);
2275 * Wait for all threads finished
2277 for (i = 0; i < n_thread; i++)
2278 lthread_join(lt[i], NULL);
2283 * Start master scheduler with initial lthread spawning rx and tx lthreads
2284 * (main_lthread_master).
2287 lthread_master_spawner(__rte_unused void *arg) {
2289 int lcore_id = rte_lcore_id();
2291 RTE_PER_LCORE(lcore_conf) = &lcore_conf[lcore_id];
2292 lthread_create(<, -1, lthread_spawner, NULL);
2299 * Start scheduler on lcore.
2302 sched_spawner(__rte_unused void *arg) {
2304 int lcore_id = rte_lcore_id();
2307 if (lcore_id == cpu_load_lcore_id) {
2308 cpu_load_collector(arg);
2311 #endif /* APP_CPU_LOAD */
2313 RTE_PER_LCORE(lcore_conf) = &lcore_conf[lcore_id];
2314 lthread_create(<, -1, lthread_null, NULL);
2320 /* main processing loop */
2322 pthread_tx(void *dummy)
2324 struct rte_mbuf *pkts_burst[MAX_PKT_BURST];
2325 uint64_t prev_tsc, diff_tsc, cur_tsc;
2328 struct thread_tx_conf *tx_conf;
2330 const uint64_t drain_tsc = (rte_get_tsc_hz() + US_PER_S - 1) /
2331 US_PER_S * BURST_TX_DRAIN_US;
2335 tx_conf = (struct thread_tx_conf *)dummy;
2337 RTE_LOG(INFO, L3FWD, "Entering main Tx loop on lcore %u\n", rte_lcore_id());
2339 tx_conf->conf.cpu_id = sched_getcpu();
2340 rte_atomic16_inc(&tx_counter);
2343 cur_tsc = rte_rdtsc();
2346 * TX burst queue drain
2348 diff_tsc = cur_tsc - prev_tsc;
2349 if (unlikely(diff_tsc > drain_tsc)) {
2352 * This could be optimized (use queueid instead of
2353 * portid), but it is not called so often
2355 SET_CPU_BUSY(tx_conf, CPU_PROCESS);
2356 for (portid = 0; portid < RTE_MAX_ETHPORTS; portid++) {
2357 if (tx_conf->tx_mbufs[portid].len == 0)
2359 send_burst(tx_conf, tx_conf->tx_mbufs[portid].len, portid);
2360 tx_conf->tx_mbufs[portid].len = 0;
2362 SET_CPU_IDLE(tx_conf, CPU_PROCESS);
2368 * Read packet from ring
2370 SET_CPU_BUSY(tx_conf, CPU_POLL);
2371 nb_rx = rte_ring_sc_dequeue_burst(tx_conf->ring,
2372 (void **)pkts_burst, MAX_PKT_BURST, NULL);
2373 SET_CPU_IDLE(tx_conf, CPU_POLL);
2375 if (unlikely(nb_rx == 0)) {
2380 SET_CPU_BUSY(tx_conf, CPU_PROCESS);
2381 portid = pkts_burst[0]->port;
2382 process_burst(pkts_burst, nb_rx, portid);
2383 SET_CPU_IDLE(tx_conf, CPU_PROCESS);
2389 pthread_rx(void *dummy)
2396 uint8_t portid, queueid;
2397 struct rte_mbuf *pkts_burst[MAX_PKT_BURST];
2399 struct thread_rx_conf *rx_conf;
2401 lcore_id = rte_lcore_id();
2402 rx_conf = (struct thread_rx_conf *)dummy;
2404 if (rx_conf->n_rx_queue == 0) {
2405 RTE_LOG(INFO, L3FWD, "lcore %u has nothing to do\n", lcore_id);
2409 RTE_LOG(INFO, L3FWD, "entering main rx loop on lcore %u\n", lcore_id);
2411 for (i = 0; i < rx_conf->n_rx_queue; i++) {
2413 portid = rx_conf->rx_queue_list[i].port_id;
2414 queueid = rx_conf->rx_queue_list[i].queue_id;
2415 RTE_LOG(INFO, L3FWD, " -- lcoreid=%u portid=%hhu rxqueueid=%hhu\n",
2416 lcore_id, portid, queueid);
2420 rx_conf->conf.cpu_id = sched_getcpu();
2421 rte_atomic16_inc(&rx_counter);
2425 * Read packet from RX queues
2427 for (i = 0; i < rx_conf->n_rx_queue; ++i) {
2428 portid = rx_conf->rx_queue_list[i].port_id;
2429 queueid = rx_conf->rx_queue_list[i].queue_id;
2431 SET_CPU_BUSY(rx_conf, CPU_POLL);
2432 nb_rx = rte_eth_rx_burst(portid, queueid, pkts_burst,
2434 SET_CPU_IDLE(rx_conf, CPU_POLL);
2441 SET_CPU_BUSY(rx_conf, CPU_PROCESS);
2442 worker_id = (worker_id + 1) % rx_conf->n_ring;
2443 n = rte_ring_sp_enqueue_burst(rx_conf->ring[worker_id],
2444 (void **)pkts_burst, nb_rx, NULL);
2446 if (unlikely(n != nb_rx)) {
2449 for (k = n; k < nb_rx; k++) {
2450 struct rte_mbuf *m = pkts_burst[k];
2452 rte_pktmbuf_free(m);
2456 SET_CPU_IDLE(rx_conf, CPU_PROCESS);
2466 pthread_run(__rte_unused void *arg) {
2467 int lcore_id = rte_lcore_id();
2470 for (i = 0; i < n_rx_thread; i++)
2471 if (rx_thread[i].conf.lcore_id == lcore_id) {
2472 printf("Start rx thread on %d...\n", lcore_id);
2473 RTE_PER_LCORE(lcore_conf) = &lcore_conf[lcore_id];
2474 RTE_PER_LCORE(lcore_conf)->data = (void *)&rx_thread[i];
2475 pthread_rx((void *)&rx_thread[i]);
2479 for (i = 0; i < n_tx_thread; i++)
2480 if (tx_thread[i].conf.lcore_id == lcore_id) {
2481 printf("Start tx thread on %d...\n", lcore_id);
2482 RTE_PER_LCORE(lcore_conf) = &lcore_conf[lcore_id];
2483 RTE_PER_LCORE(lcore_conf)->data = (void *)&tx_thread[i];
2484 pthread_tx((void *)&tx_thread[i]);
2489 if (lcore_id == cpu_load_lcore_id)
2490 cpu_load_collector(arg);
2491 #endif /* APP_CPU_LOAD */
2497 check_lcore_params(void)
2499 uint8_t queue, lcore;
2503 for (i = 0; i < nb_rx_thread_params; ++i) {
2504 queue = rx_thread_params[i].queue_id;
2505 if (queue >= MAX_RX_QUEUE_PER_PORT) {
2506 printf("invalid queue number: %hhu\n", queue);
2509 lcore = rx_thread_params[i].lcore_id;
2510 if (!rte_lcore_is_enabled(lcore)) {
2511 printf("error: lcore %hhu is not enabled in lcore mask\n", lcore);
2514 socketid = rte_lcore_to_socket_id(lcore);
2515 if ((socketid != 0) && (numa_on == 0))
2516 printf("warning: lcore %hhu is on socket %d with numa off\n",
2523 check_port_config(const unsigned nb_ports)
2528 for (i = 0; i < nb_rx_thread_params; ++i) {
2529 portid = rx_thread_params[i].port_id;
2530 if ((enabled_port_mask & (1 << portid)) == 0) {
2531 printf("port %u is not enabled in port mask\n", portid);
2534 if (portid >= nb_ports) {
2535 printf("port %u is not present on the board\n", portid);
2543 get_port_n_rx_queues(const uint8_t port)
2548 for (i = 0; i < nb_rx_thread_params; ++i)
2549 if (rx_thread_params[i].port_id == port &&
2550 rx_thread_params[i].queue_id > queue)
2551 queue = rx_thread_params[i].queue_id;
2553 return (uint8_t)(++queue);
2560 struct thread_rx_conf *rx_conf;
2561 struct thread_tx_conf *tx_conf;
2562 unsigned rx_thread_id, tx_thread_id;
2564 struct rte_ring *ring = NULL;
2566 for (tx_thread_id = 0; tx_thread_id < n_tx_thread; tx_thread_id++) {
2568 tx_conf = &tx_thread[tx_thread_id];
2570 printf("Connecting tx-thread %d with rx-thread %d\n", tx_thread_id,
2571 tx_conf->conf.thread_id);
2573 rx_thread_id = tx_conf->conf.thread_id;
2574 if (rx_thread_id > n_tx_thread) {
2575 printf("connection from tx-thread %u to rx-thread %u fails "
2576 "(rx-thread not defined)\n", tx_thread_id, rx_thread_id);
2580 rx_conf = &rx_thread[rx_thread_id];
2581 socket_io = rte_lcore_to_socket_id(rx_conf->conf.lcore_id);
2583 snprintf(name, sizeof(name), "app_ring_s%u_rx%u_tx%u",
2584 socket_io, rx_thread_id, tx_thread_id);
2586 ring = rte_ring_create(name, 1024 * 4, socket_io,
2587 RING_F_SP_ENQ | RING_F_SC_DEQ);
2590 rte_panic("Cannot create ring to connect rx-thread %u "
2591 "with tx-thread %u\n", rx_thread_id, tx_thread_id);
2594 rx_conf->ring[rx_conf->n_ring] = ring;
2596 tx_conf->ring = ring;
2597 tx_conf->ready = &rx_conf->ready[rx_conf->n_ring];
2605 init_rx_queues(void)
2607 uint16_t i, nb_rx_queue;
2612 for (i = 0; i < nb_rx_thread_params; ++i) {
2613 thread = rx_thread_params[i].thread_id;
2614 nb_rx_queue = rx_thread[thread].n_rx_queue;
2616 if (nb_rx_queue >= MAX_RX_QUEUE_PER_LCORE) {
2617 printf("error: too many queues (%u) for thread: %u\n",
2618 (unsigned)nb_rx_queue + 1, (unsigned)thread);
2622 rx_thread[thread].conf.thread_id = thread;
2623 rx_thread[thread].conf.lcore_id = rx_thread_params[i].lcore_id;
2624 rx_thread[thread].rx_queue_list[nb_rx_queue].port_id =
2625 rx_thread_params[i].port_id;
2626 rx_thread[thread].rx_queue_list[nb_rx_queue].queue_id =
2627 rx_thread_params[i].queue_id;
2628 rx_thread[thread].n_rx_queue++;
2630 if (thread >= n_rx_thread)
2631 n_rx_thread = thread + 1;
2638 init_tx_threads(void)
2643 for (i = 0; i < nb_tx_thread_params; ++i) {
2644 tx_thread[n_tx_thread].conf.thread_id = tx_thread_params[i].thread_id;
2645 tx_thread[n_tx_thread].conf.lcore_id = tx_thread_params[i].lcore_id;
2653 print_usage(const char *prgname)
2655 printf("%s [EAL options] -- -p PORTMASK -P"
2656 " [--rx (port,queue,lcore,thread)[,(port,queue,lcore,thread]]"
2657 " [--tx (lcore,thread)[,(lcore,thread]]"
2658 " [--enable-jumbo [--max-pkt-len PKTLEN]]\n"
2659 " [--parse-ptype]\n\n"
2660 " -p PORTMASK: hexadecimal bitmask of ports to configure\n"
2661 " -P : enable promiscuous mode\n"
2662 " --rx (port,queue,lcore,thread): rx queues configuration\n"
2663 " --tx (lcore,thread): tx threads configuration\n"
2664 " --stat-lcore LCORE: use lcore for stat collector\n"
2665 " --eth-dest=X,MM:MM:MM:MM:MM:MM: optional, ethernet destination for port X\n"
2666 " --no-numa: optional, disable numa awareness\n"
2667 " --ipv6: optional, specify it if running ipv6 packets\n"
2668 " --enable-jumbo: enable jumbo frame"
2669 " which max packet len is PKTLEN in decimal (64-9600)\n"
2670 " --hash-entry-num: specify the hash entry number in hexadecimal to be setup\n"
2671 " --no-lthreads: turn off lthread model\n"
2672 " --parse-ptype: set to use software to analyze packet type\n\n",
2676 static int parse_max_pkt_len(const char *pktlen)
2681 /* parse decimal string */
2682 len = strtoul(pktlen, &end, 10);
2683 if ((pktlen[0] == '\0') || (end == NULL) || (*end != '\0'))
2693 parse_portmask(const char *portmask)
2698 /* parse hexadecimal string */
2699 pm = strtoul(portmask, &end, 16);
2700 if ((portmask[0] == '\0') || (end == NULL) || (*end != '\0'))
2709 #if (APP_LOOKUP_METHOD == APP_LOOKUP_EXACT_MATCH)
2711 parse_hash_entry_number(const char *hash_entry_num)
2714 unsigned long hash_en;
2716 /* parse hexadecimal string */
2717 hash_en = strtoul(hash_entry_num, &end, 16);
2718 if ((hash_entry_num[0] == '\0') || (end == NULL) || (*end != '\0'))
2729 parse_rx_config(const char *q_arg)
2732 const char *p, *p0 = q_arg;
2741 unsigned long int_fld[_NUM_FLD];
2742 char *str_fld[_NUM_FLD];
2746 nb_rx_thread_params = 0;
2748 while ((p = strchr(p0, '(')) != NULL) {
2750 p0 = strchr(p, ')');
2755 if (size >= sizeof(s))
2758 snprintf(s, sizeof(s), "%.*s", size, p);
2759 if (rte_strsplit(s, sizeof(s), str_fld, _NUM_FLD, ',') != _NUM_FLD)
2761 for (i = 0; i < _NUM_FLD; i++) {
2763 int_fld[i] = strtoul(str_fld[i], &end, 0);
2764 if (errno != 0 || end == str_fld[i] || int_fld[i] > 255)
2767 if (nb_rx_thread_params >= MAX_LCORE_PARAMS) {
2768 printf("exceeded max number of rx params: %hu\n",
2769 nb_rx_thread_params);
2772 rx_thread_params_array[nb_rx_thread_params].port_id =
2773 (uint8_t)int_fld[FLD_PORT];
2774 rx_thread_params_array[nb_rx_thread_params].queue_id =
2775 (uint8_t)int_fld[FLD_QUEUE];
2776 rx_thread_params_array[nb_rx_thread_params].lcore_id =
2777 (uint8_t)int_fld[FLD_LCORE];
2778 rx_thread_params_array[nb_rx_thread_params].thread_id =
2779 (uint8_t)int_fld[FLD_THREAD];
2780 ++nb_rx_thread_params;
2782 rx_thread_params = rx_thread_params_array;
2787 parse_tx_config(const char *q_arg)
2790 const char *p, *p0 = q_arg;
2797 unsigned long int_fld[_NUM_FLD];
2798 char *str_fld[_NUM_FLD];
2802 nb_tx_thread_params = 0;
2804 while ((p = strchr(p0, '(')) != NULL) {
2806 p0 = strchr(p, ')');
2811 if (size >= sizeof(s))
2814 snprintf(s, sizeof(s), "%.*s", size, p);
2815 if (rte_strsplit(s, sizeof(s), str_fld, _NUM_FLD, ',') != _NUM_FLD)
2817 for (i = 0; i < _NUM_FLD; i++) {
2819 int_fld[i] = strtoul(str_fld[i], &end, 0);
2820 if (errno != 0 || end == str_fld[i] || int_fld[i] > 255)
2823 if (nb_tx_thread_params >= MAX_LCORE_PARAMS) {
2824 printf("exceeded max number of tx params: %hu\n",
2825 nb_tx_thread_params);
2828 tx_thread_params_array[nb_tx_thread_params].lcore_id =
2829 (uint8_t)int_fld[FLD_LCORE];
2830 tx_thread_params_array[nb_tx_thread_params].thread_id =
2831 (uint8_t)int_fld[FLD_THREAD];
2832 ++nb_tx_thread_params;
2834 tx_thread_params = tx_thread_params_array;
2839 #if (APP_CPU_LOAD > 0)
2841 parse_stat_lcore(const char *stat_lcore)
2844 unsigned long lcore_id;
2846 lcore_id = strtoul(stat_lcore, &end, 10);
2847 if ((stat_lcore[0] == '\0') || (end == NULL) || (*end != '\0'))
2855 parse_eth_dest(const char *optarg)
2859 uint8_t c, *dest, peer_addr[6];
2862 portid = strtoul(optarg, &port_end, 10);
2863 if (errno != 0 || port_end == optarg || *port_end++ != ',')
2864 rte_exit(EXIT_FAILURE,
2865 "Invalid eth-dest: %s", optarg);
2866 if (portid >= RTE_MAX_ETHPORTS)
2867 rte_exit(EXIT_FAILURE,
2868 "eth-dest: port %d >= RTE_MAX_ETHPORTS(%d)\n",
2869 portid, RTE_MAX_ETHPORTS);
2871 if (cmdline_parse_etheraddr(NULL, port_end,
2872 &peer_addr, sizeof(peer_addr)) < 0)
2873 rte_exit(EXIT_FAILURE,
2874 "Invalid ethernet address: %s\n",
2876 dest = (uint8_t *)&dest_eth_addr[portid];
2877 for (c = 0; c < 6; c++)
2878 dest[c] = peer_addr[c];
2879 *(uint64_t *)(val_eth + portid) = dest_eth_addr[portid];
2882 #define CMD_LINE_OPT_RX_CONFIG "rx"
2883 #define CMD_LINE_OPT_TX_CONFIG "tx"
2884 #define CMD_LINE_OPT_STAT_LCORE "stat-lcore"
2885 #define CMD_LINE_OPT_ETH_DEST "eth-dest"
2886 #define CMD_LINE_OPT_NO_NUMA "no-numa"
2887 #define CMD_LINE_OPT_IPV6 "ipv6"
2888 #define CMD_LINE_OPT_ENABLE_JUMBO "enable-jumbo"
2889 #define CMD_LINE_OPT_HASH_ENTRY_NUM "hash-entry-num"
2890 #define CMD_LINE_OPT_NO_LTHREADS "no-lthreads"
2891 #define CMD_LINE_OPT_PARSE_PTYPE "parse-ptype"
2893 /* Parse the argument given in the command line of the application */
2895 parse_args(int argc, char **argv)
2900 char *prgname = argv[0];
2901 static struct option lgopts[] = {
2902 {CMD_LINE_OPT_RX_CONFIG, 1, 0, 0},
2903 {CMD_LINE_OPT_TX_CONFIG, 1, 0, 0},
2904 {CMD_LINE_OPT_STAT_LCORE, 1, 0, 0},
2905 {CMD_LINE_OPT_ETH_DEST, 1, 0, 0},
2906 {CMD_LINE_OPT_NO_NUMA, 0, 0, 0},
2907 {CMD_LINE_OPT_IPV6, 0, 0, 0},
2908 {CMD_LINE_OPT_ENABLE_JUMBO, 0, 0, 0},
2909 {CMD_LINE_OPT_HASH_ENTRY_NUM, 1, 0, 0},
2910 {CMD_LINE_OPT_NO_LTHREADS, 0, 0, 0},
2911 {CMD_LINE_OPT_PARSE_PTYPE, 0, 0, 0},
2917 while ((opt = getopt_long(argc, argvopt, "p:P",
2918 lgopts, &option_index)) != EOF) {
2923 enabled_port_mask = parse_portmask(optarg);
2924 if (enabled_port_mask == 0) {
2925 printf("invalid portmask\n");
2926 print_usage(prgname);
2931 printf("Promiscuous mode selected\n");
2937 if (!strncmp(lgopts[option_index].name, CMD_LINE_OPT_RX_CONFIG,
2938 sizeof(CMD_LINE_OPT_RX_CONFIG))) {
2939 ret = parse_rx_config(optarg);
2941 printf("invalid rx-config\n");
2942 print_usage(prgname);
2947 if (!strncmp(lgopts[option_index].name, CMD_LINE_OPT_TX_CONFIG,
2948 sizeof(CMD_LINE_OPT_TX_CONFIG))) {
2949 ret = parse_tx_config(optarg);
2951 printf("invalid tx-config\n");
2952 print_usage(prgname);
2957 #if (APP_CPU_LOAD > 0)
2958 if (!strncmp(lgopts[option_index].name, CMD_LINE_OPT_STAT_LCORE,
2959 sizeof(CMD_LINE_OPT_STAT_LCORE))) {
2960 cpu_load_lcore_id = parse_stat_lcore(optarg);
2964 if (!strncmp(lgopts[option_index].name, CMD_LINE_OPT_ETH_DEST,
2965 sizeof(CMD_LINE_OPT_ETH_DEST)))
2966 parse_eth_dest(optarg);
2968 if (!strncmp(lgopts[option_index].name, CMD_LINE_OPT_NO_NUMA,
2969 sizeof(CMD_LINE_OPT_NO_NUMA))) {
2970 printf("numa is disabled\n");
2974 #if (APP_LOOKUP_METHOD == APP_LOOKUP_EXACT_MATCH)
2975 if (!strncmp(lgopts[option_index].name, CMD_LINE_OPT_IPV6,
2976 sizeof(CMD_LINE_OPT_IPV6))) {
2977 printf("ipv6 is specified\n");
2982 if (!strncmp(lgopts[option_index].name, CMD_LINE_OPT_NO_LTHREADS,
2983 sizeof(CMD_LINE_OPT_NO_LTHREADS))) {
2984 printf("l-threads model is disabled\n");
2988 if (!strncmp(lgopts[option_index].name, CMD_LINE_OPT_PARSE_PTYPE,
2989 sizeof(CMD_LINE_OPT_PARSE_PTYPE))) {
2990 printf("software packet type parsing enabled\n");
2994 if (!strncmp(lgopts[option_index].name, CMD_LINE_OPT_ENABLE_JUMBO,
2995 sizeof(CMD_LINE_OPT_ENABLE_JUMBO))) {
2996 struct option lenopts = {"max-pkt-len", required_argument, 0,
2999 printf("jumbo frame is enabled - disabling simple TX path\n");
3000 port_conf.rxmode.jumbo_frame = 1;
3002 /* if no max-pkt-len set, use the default value ETHER_MAX_LEN */
3003 if (0 == getopt_long(argc, argvopt, "", &lenopts,
3006 ret = parse_max_pkt_len(optarg);
3007 if ((ret < 64) || (ret > MAX_JUMBO_PKT_LEN)) {
3008 printf("invalid packet length\n");
3009 print_usage(prgname);
3012 port_conf.rxmode.max_rx_pkt_len = ret;
3014 printf("set jumbo frame max packet length to %u\n",
3015 (unsigned int)port_conf.rxmode.max_rx_pkt_len);
3017 #if (APP_LOOKUP_METHOD == APP_LOOKUP_EXACT_MATCH)
3018 if (!strncmp(lgopts[option_index].name, CMD_LINE_OPT_HASH_ENTRY_NUM,
3019 sizeof(CMD_LINE_OPT_HASH_ENTRY_NUM))) {
3020 ret = parse_hash_entry_number(optarg);
3021 if ((ret > 0) && (ret <= L3FWD_HASH_ENTRIES)) {
3022 hash_entry_number = ret;
3024 printf("invalid hash entry number\n");
3025 print_usage(prgname);
3033 print_usage(prgname);
3039 argv[optind-1] = prgname;
3042 optind = 1; /* reset getopt lib */
3047 print_ethaddr(const char *name, const struct ether_addr *eth_addr)
3049 char buf[ETHER_ADDR_FMT_SIZE];
3051 ether_format_addr(buf, ETHER_ADDR_FMT_SIZE, eth_addr);
3052 printf("%s%s", name, buf);
3055 #if (APP_LOOKUP_METHOD == APP_LOOKUP_EXACT_MATCH)
3057 static void convert_ipv4_5tuple(struct ipv4_5tuple *key1,
3058 union ipv4_5tuple_host *key2)
3060 key2->ip_dst = rte_cpu_to_be_32(key1->ip_dst);
3061 key2->ip_src = rte_cpu_to_be_32(key1->ip_src);
3062 key2->port_dst = rte_cpu_to_be_16(key1->port_dst);
3063 key2->port_src = rte_cpu_to_be_16(key1->port_src);
3064 key2->proto = key1->proto;
3069 static void convert_ipv6_5tuple(struct ipv6_5tuple *key1,
3070 union ipv6_5tuple_host *key2)
3074 for (i = 0; i < 16; i++) {
3075 key2->ip_dst[i] = key1->ip_dst[i];
3076 key2->ip_src[i] = key1->ip_src[i];
3078 key2->port_dst = rte_cpu_to_be_16(key1->port_dst);
3079 key2->port_src = rte_cpu_to_be_16(key1->port_src);
3080 key2->proto = key1->proto;
3086 #define BYTE_VALUE_MAX 256
3087 #define ALL_32_BITS 0xffffffff
3088 #define BIT_8_TO_15 0x0000ff00
3090 populate_ipv4_few_flow_into_table(const struct rte_hash *h)
3094 uint32_t array_len = RTE_DIM(ipv4_l3fwd_route_array);
3096 mask0 = _mm_set_epi32(ALL_32_BITS, ALL_32_BITS, ALL_32_BITS, BIT_8_TO_15);
3097 for (i = 0; i < array_len; i++) {
3098 struct ipv4_l3fwd_route entry;
3099 union ipv4_5tuple_host newkey;
3101 entry = ipv4_l3fwd_route_array[i];
3102 convert_ipv4_5tuple(&entry.key, &newkey);
3103 ret = rte_hash_add_key(h, (void *)&newkey);
3105 rte_exit(EXIT_FAILURE, "Unable to add entry %" PRIu32
3106 " to the l3fwd hash.\n", i);
3108 ipv4_l3fwd_out_if[ret] = entry.if_out;
3110 printf("Hash: Adding 0x%" PRIx32 " keys\n", array_len);
3113 #define BIT_16_TO_23 0x00ff0000
3115 populate_ipv6_few_flow_into_table(const struct rte_hash *h)
3119 uint32_t array_len = RTE_DIM(ipv6_l3fwd_route_array);
3121 mask1 = _mm_set_epi32(ALL_32_BITS, ALL_32_BITS, ALL_32_BITS, BIT_16_TO_23);
3122 mask2 = _mm_set_epi32(0, 0, ALL_32_BITS, ALL_32_BITS);
3123 for (i = 0; i < array_len; i++) {
3124 struct ipv6_l3fwd_route entry;
3125 union ipv6_5tuple_host newkey;
3127 entry = ipv6_l3fwd_route_array[i];
3128 convert_ipv6_5tuple(&entry.key, &newkey);
3129 ret = rte_hash_add_key(h, (void *)&newkey);
3131 rte_exit(EXIT_FAILURE, "Unable to add entry %" PRIu32
3132 " to the l3fwd hash.\n", i);
3134 ipv6_l3fwd_out_if[ret] = entry.if_out;
3136 printf("Hash: Adding 0x%" PRIx32 "keys\n", array_len);
3139 #define NUMBER_PORT_USED 4
3141 populate_ipv4_many_flow_into_table(const struct rte_hash *h,
3142 unsigned int nr_flow)
3146 mask0 = _mm_set_epi32(ALL_32_BITS, ALL_32_BITS, ALL_32_BITS, BIT_8_TO_15);
3148 for (i = 0; i < nr_flow; i++) {
3149 struct ipv4_l3fwd_route entry;
3150 union ipv4_5tuple_host newkey;
3151 uint8_t a = (uint8_t)((i / NUMBER_PORT_USED) % BYTE_VALUE_MAX);
3152 uint8_t b = (uint8_t)(((i / NUMBER_PORT_USED) / BYTE_VALUE_MAX) %
3154 uint8_t c = (uint8_t)((i / NUMBER_PORT_USED) / (BYTE_VALUE_MAX *
3156 /* Create the ipv4 exact match flow */
3157 memset(&entry, 0, sizeof(entry));
3158 switch (i & (NUMBER_PORT_USED - 1)) {
3160 entry = ipv4_l3fwd_route_array[0];
3161 entry.key.ip_dst = IPv4(101, c, b, a);
3164 entry = ipv4_l3fwd_route_array[1];
3165 entry.key.ip_dst = IPv4(201, c, b, a);
3168 entry = ipv4_l3fwd_route_array[2];
3169 entry.key.ip_dst = IPv4(111, c, b, a);
3172 entry = ipv4_l3fwd_route_array[3];
3173 entry.key.ip_dst = IPv4(211, c, b, a);
3176 convert_ipv4_5tuple(&entry.key, &newkey);
3177 int32_t ret = rte_hash_add_key(h, (void *)&newkey);
3180 rte_exit(EXIT_FAILURE, "Unable to add entry %u\n", i);
3182 ipv4_l3fwd_out_if[ret] = (uint8_t)entry.if_out;
3185 printf("Hash: Adding 0x%x keys\n", nr_flow);
3189 populate_ipv6_many_flow_into_table(const struct rte_hash *h,
3190 unsigned int nr_flow)
3194 mask1 = _mm_set_epi32(ALL_32_BITS, ALL_32_BITS, ALL_32_BITS, BIT_16_TO_23);
3195 mask2 = _mm_set_epi32(0, 0, ALL_32_BITS, ALL_32_BITS);
3196 for (i = 0; i < nr_flow; i++) {
3197 struct ipv6_l3fwd_route entry;
3198 union ipv6_5tuple_host newkey;
3200 uint8_t a = (uint8_t) ((i / NUMBER_PORT_USED) % BYTE_VALUE_MAX);
3201 uint8_t b = (uint8_t) (((i / NUMBER_PORT_USED) / BYTE_VALUE_MAX) %
3203 uint8_t c = (uint8_t) ((i / NUMBER_PORT_USED) / (BYTE_VALUE_MAX *
3206 /* Create the ipv6 exact match flow */
3207 memset(&entry, 0, sizeof(entry));
3208 switch (i & (NUMBER_PORT_USED - 1)) {
3210 entry = ipv6_l3fwd_route_array[0];
3213 entry = ipv6_l3fwd_route_array[1];
3216 entry = ipv6_l3fwd_route_array[2];
3219 entry = ipv6_l3fwd_route_array[3];
3222 entry.key.ip_dst[13] = c;
3223 entry.key.ip_dst[14] = b;
3224 entry.key.ip_dst[15] = a;
3225 convert_ipv6_5tuple(&entry.key, &newkey);
3226 int32_t ret = rte_hash_add_key(h, (void *)&newkey);
3229 rte_exit(EXIT_FAILURE, "Unable to add entry %u\n", i);
3231 ipv6_l3fwd_out_if[ret] = (uint8_t) entry.if_out;
3234 printf("Hash: Adding 0x%x keys\n", nr_flow);
3238 setup_hash(int socketid)
3240 struct rte_hash_parameters ipv4_l3fwd_hash_params = {
3242 .entries = L3FWD_HASH_ENTRIES,
3243 .key_len = sizeof(union ipv4_5tuple_host),
3244 .hash_func = ipv4_hash_crc,
3245 .hash_func_init_val = 0,
3248 struct rte_hash_parameters ipv6_l3fwd_hash_params = {
3250 .entries = L3FWD_HASH_ENTRIES,
3251 .key_len = sizeof(union ipv6_5tuple_host),
3252 .hash_func = ipv6_hash_crc,
3253 .hash_func_init_val = 0,
3258 /* create ipv4 hash */
3259 snprintf(s, sizeof(s), "ipv4_l3fwd_hash_%d", socketid);
3260 ipv4_l3fwd_hash_params.name = s;
3261 ipv4_l3fwd_hash_params.socket_id = socketid;
3262 ipv4_l3fwd_lookup_struct[socketid] =
3263 rte_hash_create(&ipv4_l3fwd_hash_params);
3264 if (ipv4_l3fwd_lookup_struct[socketid] == NULL)
3265 rte_exit(EXIT_FAILURE, "Unable to create the l3fwd hash on "
3266 "socket %d\n", socketid);
3268 /* create ipv6 hash */
3269 snprintf(s, sizeof(s), "ipv6_l3fwd_hash_%d", socketid);
3270 ipv6_l3fwd_hash_params.name = s;
3271 ipv6_l3fwd_hash_params.socket_id = socketid;
3272 ipv6_l3fwd_lookup_struct[socketid] =
3273 rte_hash_create(&ipv6_l3fwd_hash_params);
3274 if (ipv6_l3fwd_lookup_struct[socketid] == NULL)
3275 rte_exit(EXIT_FAILURE, "Unable to create the l3fwd hash on "
3276 "socket %d\n", socketid);
3278 if (hash_entry_number != HASH_ENTRY_NUMBER_DEFAULT) {
3279 /* For testing hash matching with a large number of flows we
3280 * generate millions of IP 5-tuples with an incremented dst
3281 * address to initialize the hash table. */
3283 /* populate the ipv4 hash */
3284 populate_ipv4_many_flow_into_table(
3285 ipv4_l3fwd_lookup_struct[socketid], hash_entry_number);
3287 /* populate the ipv6 hash */
3288 populate_ipv6_many_flow_into_table(
3289 ipv6_l3fwd_lookup_struct[socketid], hash_entry_number);
3292 /* Use data in ipv4/ipv6 l3fwd lookup table directly to initialize
3295 /* populate the ipv4 hash */
3296 populate_ipv4_few_flow_into_table(
3297 ipv4_l3fwd_lookup_struct[socketid]);
3299 /* populate the ipv6 hash */
3300 populate_ipv6_few_flow_into_table(
3301 ipv6_l3fwd_lookup_struct[socketid]);
3307 #if (APP_LOOKUP_METHOD == APP_LOOKUP_LPM)
3309 setup_lpm(int socketid)
3311 struct rte_lpm6_config config;
3312 struct rte_lpm_config lpm_ipv4_config;
3317 /* create the LPM table */
3318 snprintf(s, sizeof(s), "IPV4_L3FWD_LPM_%d", socketid);
3319 lpm_ipv4_config.max_rules = IPV4_L3FWD_LPM_MAX_RULES;
3320 lpm_ipv4_config.number_tbl8s = 256;
3321 lpm_ipv4_config.flags = 0;
3322 ipv4_l3fwd_lookup_struct[socketid] =
3323 rte_lpm_create(s, socketid, &lpm_ipv4_config);
3324 if (ipv4_l3fwd_lookup_struct[socketid] == NULL)
3325 rte_exit(EXIT_FAILURE, "Unable to create the l3fwd LPM table"
3326 " on socket %d\n", socketid);
3328 /* populate the LPM table */
3329 for (i = 0; i < IPV4_L3FWD_NUM_ROUTES; i++) {
3331 /* skip unused ports */
3332 if ((1 << ipv4_l3fwd_route_array[i].if_out &
3333 enabled_port_mask) == 0)
3336 ret = rte_lpm_add(ipv4_l3fwd_lookup_struct[socketid],
3337 ipv4_l3fwd_route_array[i].ip,
3338 ipv4_l3fwd_route_array[i].depth,
3339 ipv4_l3fwd_route_array[i].if_out);
3342 rte_exit(EXIT_FAILURE, "Unable to add entry %u to the "
3343 "l3fwd LPM table on socket %d\n",
3347 printf("LPM: Adding route 0x%08x / %d (%d)\n",
3348 (unsigned)ipv4_l3fwd_route_array[i].ip,
3349 ipv4_l3fwd_route_array[i].depth,
3350 ipv4_l3fwd_route_array[i].if_out);
3353 /* create the LPM6 table */
3354 snprintf(s, sizeof(s), "IPV6_L3FWD_LPM_%d", socketid);
3356 config.max_rules = IPV6_L3FWD_LPM_MAX_RULES;
3357 config.number_tbl8s = IPV6_L3FWD_LPM_NUMBER_TBL8S;
3359 ipv6_l3fwd_lookup_struct[socketid] = rte_lpm6_create(s, socketid,
3361 if (ipv6_l3fwd_lookup_struct[socketid] == NULL)
3362 rte_exit(EXIT_FAILURE, "Unable to create the l3fwd LPM table"
3363 " on socket %d\n", socketid);
3365 /* populate the LPM table */
3366 for (i = 0; i < IPV6_L3FWD_NUM_ROUTES; i++) {
3368 /* skip unused ports */
3369 if ((1 << ipv6_l3fwd_route_array[i].if_out &
3370 enabled_port_mask) == 0)
3373 ret = rte_lpm6_add(ipv6_l3fwd_lookup_struct[socketid],
3374 ipv6_l3fwd_route_array[i].ip,
3375 ipv6_l3fwd_route_array[i].depth,
3376 ipv6_l3fwd_route_array[i].if_out);
3379 rte_exit(EXIT_FAILURE, "Unable to add entry %u to the "
3380 "l3fwd LPM table on socket %d\n",
3384 printf("LPM: Adding route %s / %d (%d)\n",
3386 ipv6_l3fwd_route_array[i].depth,
3387 ipv6_l3fwd_route_array[i].if_out);
3393 init_mem(unsigned nb_mbuf)
3395 struct lcore_conf *qconf;
3400 for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
3401 if (rte_lcore_is_enabled(lcore_id) == 0)
3405 socketid = rte_lcore_to_socket_id(lcore_id);
3409 if (socketid >= NB_SOCKETS) {
3410 rte_exit(EXIT_FAILURE, "Socket %d of lcore %u is out of range %d\n",
3411 socketid, lcore_id, NB_SOCKETS);
3413 if (pktmbuf_pool[socketid] == NULL) {
3414 snprintf(s, sizeof(s), "mbuf_pool_%d", socketid);
3415 pktmbuf_pool[socketid] =
3416 rte_pktmbuf_pool_create(s, nb_mbuf,
3417 MEMPOOL_CACHE_SIZE, 0,
3418 RTE_MBUF_DEFAULT_BUF_SIZE, socketid);
3419 if (pktmbuf_pool[socketid] == NULL)
3420 rte_exit(EXIT_FAILURE,
3421 "Cannot init mbuf pool on socket %d\n", socketid);
3423 printf("Allocated mbuf pool on socket %d\n", socketid);
3425 #if (APP_LOOKUP_METHOD == APP_LOOKUP_LPM)
3426 setup_lpm(socketid);
3428 setup_hash(socketid);
3431 qconf = &lcore_conf[lcore_id];
3432 qconf->ipv4_lookup_struct = ipv4_l3fwd_lookup_struct[socketid];
3433 qconf->ipv6_lookup_struct = ipv6_l3fwd_lookup_struct[socketid];
3438 /* Check the link status of all ports in up to 9s, and print them finally */
3440 check_all_ports_link_status(uint8_t port_num, uint32_t port_mask)
3442 #define CHECK_INTERVAL 100 /* 100ms */
3443 #define MAX_CHECK_TIME 90 /* 9s (90 * 100ms) in total */
3444 uint8_t portid, count, all_ports_up, print_flag = 0;
3445 struct rte_eth_link link;
3447 printf("\nChecking link status");
3449 for (count = 0; count <= MAX_CHECK_TIME; count++) {
3451 for (portid = 0; portid < port_num; portid++) {
3452 if ((port_mask & (1 << portid)) == 0)
3454 memset(&link, 0, sizeof(link));
3455 rte_eth_link_get_nowait(portid, &link);
3456 /* print link status if flag set */
3457 if (print_flag == 1) {
3458 if (link.link_status)
3459 printf("Port %d Link Up - speed %u "
3460 "Mbps - %s\n", (uint8_t)portid,
3461 (unsigned)link.link_speed,
3462 (link.link_duplex == ETH_LINK_FULL_DUPLEX) ?
3463 ("full-duplex") : ("half-duplex\n"));
3465 printf("Port %d Link Down\n",
3469 /* clear all_ports_up flag if any link down */
3470 if (link.link_status == ETH_LINK_DOWN) {
3475 /* after finally printing all link status, get out */
3476 if (print_flag == 1)
3479 if (all_ports_up == 0) {
3482 rte_delay_ms(CHECK_INTERVAL);
3485 /* set the print_flag if all ports up or timeout */
3486 if (all_ports_up == 1 || count == (MAX_CHECK_TIME - 1)) {
3494 main(int argc, char **argv)
3496 struct rte_eth_dev_info dev_info;
3497 struct rte_eth_txconf *txconf;
3503 uint32_t n_tx_queue, nb_lcores;
3504 uint8_t portid, nb_rx_queue, queue, socketid;
3507 ret = rte_eal_init(argc, argv);
3509 rte_exit(EXIT_FAILURE, "Invalid EAL parameters\n");
3513 /* pre-init dst MACs for all ports to 02:00:00:00:00:xx */
3514 for (portid = 0; portid < RTE_MAX_ETHPORTS; portid++) {
3515 dest_eth_addr[portid] = ETHER_LOCAL_ADMIN_ADDR +
3516 ((uint64_t)portid << 40);
3517 *(uint64_t *)(val_eth + portid) = dest_eth_addr[portid];
3520 /* parse application arguments (after the EAL ones) */
3521 ret = parse_args(argc, argv);
3523 rte_exit(EXIT_FAILURE, "Invalid L3FWD parameters\n");
3525 if (check_lcore_params() < 0)
3526 rte_exit(EXIT_FAILURE, "check_lcore_params failed\n");
3528 printf("Initializing rx-queues...\n");
3529 ret = init_rx_queues();
3531 rte_exit(EXIT_FAILURE, "init_rx_queues failed\n");
3533 printf("Initializing tx-threads...\n");
3534 ret = init_tx_threads();
3536 rte_exit(EXIT_FAILURE, "init_tx_threads failed\n");
3538 printf("Initializing rings...\n");
3539 ret = init_rx_rings();
3541 rte_exit(EXIT_FAILURE, "init_rx_rings failed\n");
3543 nb_ports = rte_eth_dev_count();
3545 if (check_port_config(nb_ports) < 0)
3546 rte_exit(EXIT_FAILURE, "check_port_config failed\n");
3548 nb_lcores = rte_lcore_count();
3550 /* initialize all ports */
3551 for (portid = 0; portid < nb_ports; portid++) {
3552 /* skip ports that are not enabled */
3553 if ((enabled_port_mask & (1 << portid)) == 0) {
3554 printf("\nSkipping disabled port %d\n", portid);
3559 printf("Initializing port %d ... ", portid);
3562 nb_rx_queue = get_port_n_rx_queues(portid);
3563 n_tx_queue = nb_lcores;
3564 if (n_tx_queue > MAX_TX_QUEUE_PER_PORT)
3565 n_tx_queue = MAX_TX_QUEUE_PER_PORT;
3566 printf("Creating queues: nb_rxq=%d nb_txq=%u... ",
3567 nb_rx_queue, (unsigned)n_tx_queue);
3568 ret = rte_eth_dev_configure(portid, nb_rx_queue,
3569 (uint16_t)n_tx_queue, &port_conf);
3571 rte_exit(EXIT_FAILURE, "Cannot configure device: err=%d, port=%d\n",
3574 rte_eth_macaddr_get(portid, &ports_eth_addr[portid]);
3575 print_ethaddr(" Address:", &ports_eth_addr[portid]);
3577 print_ethaddr("Destination:",
3578 (const struct ether_addr *)&dest_eth_addr[portid]);
3582 * prepare src MACs for each port.
3584 ether_addr_copy(&ports_eth_addr[portid],
3585 (struct ether_addr *)(val_eth + portid) + 1);
3588 ret = init_mem(NB_MBUF);
3590 rte_exit(EXIT_FAILURE, "init_mem failed\n");
3592 /* init one TX queue per couple (lcore,port) */
3594 for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
3595 if (rte_lcore_is_enabled(lcore_id) == 0)
3599 socketid = (uint8_t)rte_lcore_to_socket_id(lcore_id);
3603 printf("txq=%u,%d,%d ", lcore_id, queueid, socketid);
3606 rte_eth_dev_info_get(portid, &dev_info);
3607 txconf = &dev_info.default_txconf;
3608 if (port_conf.rxmode.jumbo_frame)
3609 txconf->txq_flags = 0;
3610 ret = rte_eth_tx_queue_setup(portid, queueid, nb_txd,
3613 rte_exit(EXIT_FAILURE, "rte_eth_tx_queue_setup: err=%d, "
3614 "port=%d\n", ret, portid);
3616 tx_thread[lcore_id].tx_queue_id[portid] = queueid;
3622 for (i = 0; i < n_rx_thread; i++) {
3623 lcore_id = rx_thread[i].conf.lcore_id;
3625 if (rte_lcore_is_enabled(lcore_id) == 0) {
3626 rte_exit(EXIT_FAILURE,
3627 "Cannot start Rx thread on lcore %u: lcore disabled\n",
3632 printf("\nInitializing rx queues for Rx thread %d on lcore %u ... ",
3636 /* init RX queues */
3637 for (queue = 0; queue < rx_thread[i].n_rx_queue; ++queue) {
3638 portid = rx_thread[i].rx_queue_list[queue].port_id;
3639 queueid = rx_thread[i].rx_queue_list[queue].queue_id;
3642 socketid = (uint8_t)rte_lcore_to_socket_id(lcore_id);
3646 printf("rxq=%d,%d,%d ", portid, queueid, socketid);
3649 ret = rte_eth_rx_queue_setup(portid, queueid, nb_rxd,
3652 pktmbuf_pool[socketid]);
3654 rte_exit(EXIT_FAILURE, "rte_eth_rx_queue_setup: err=%d, "
3655 "port=%d\n", ret, portid);
3662 for (portid = 0; portid < nb_ports; portid++) {
3663 if ((enabled_port_mask & (1 << portid)) == 0)
3667 ret = rte_eth_dev_start(portid);
3669 rte_exit(EXIT_FAILURE, "rte_eth_dev_start: err=%d, port=%d\n",
3673 * If enabled, put device in promiscuous mode.
3674 * This allows IO forwarding mode to forward packets
3675 * to itself through 2 cross-connected ports of the
3679 rte_eth_promiscuous_enable(portid);
3682 for (i = 0; i < n_rx_thread; i++) {
3683 lcore_id = rx_thread[i].conf.lcore_id;
3684 if (rte_lcore_is_enabled(lcore_id) == 0)
3687 /* check if hw packet type is supported */
3688 for (queue = 0; queue < rx_thread[i].n_rx_queue; ++queue) {
3689 portid = rx_thread[i].rx_queue_list[queue].port_id;
3690 queueid = rx_thread[i].rx_queue_list[queue].queue_id;
3692 if (parse_ptype_on) {
3693 if (!rte_eth_add_rx_callback(portid, queueid,
3694 cb_parse_ptype, NULL))
3695 rte_exit(EXIT_FAILURE,
3696 "Failed to add rx callback: "
3697 "port=%d\n", portid);
3698 } else if (!check_ptype(portid))
3699 rte_exit(EXIT_FAILURE,
3700 "Port %d cannot parse packet type.\n\n"
3701 "Please add --parse-ptype to use sw "
3702 "packet type analyzer.\n\n",
3707 check_all_ports_link_status((uint8_t)nb_ports, enabled_port_mask);
3710 printf("Starting L-Threading Model\n");
3712 #if (APP_CPU_LOAD > 0)
3713 if (cpu_load_lcore_id > 0)
3714 /* Use one lcore for cpu load collector */
3718 lthread_num_schedulers_set(nb_lcores);
3719 rte_eal_mp_remote_launch(sched_spawner, NULL, SKIP_MASTER);
3720 lthread_master_spawner(NULL);
3723 printf("Starting P-Threading Model\n");
3724 /* launch per-lcore init on every lcore */
3725 rte_eal_mp_remote_launch(pthread_run, NULL, CALL_MASTER);
3726 RTE_LCORE_FOREACH_SLAVE(lcore_id) {
3727 if (rte_eal_wait_lcore(lcore_id) < 0)