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_launch.h>
56 #include <rte_atomic.h>
57 #include <rte_cycles.h>
58 #include <rte_prefetch.h>
59 #include <rte_lcore.h>
60 #include <rte_per_lcore.h>
61 #include <rte_branch_prediction.h>
62 #include <rte_interrupts.h>
64 #include <rte_random.h>
65 #include <rte_debug.h>
66 #include <rte_ether.h>
67 #include <rte_ethdev.h>
69 #include <rte_mempool.h>
74 #include <rte_string_fns.h>
75 #include <rte_pause.h>
77 #include <cmdline_parse.h>
78 #include <cmdline_parse_etheraddr.h>
80 #include <lthread_api.h>
82 #define APP_LOOKUP_EXACT_MATCH 0
83 #define APP_LOOKUP_LPM 1
84 #define DO_RFC_1812_CHECKS
86 /* Enable cpu-load stats 0-off, 1-on */
87 #define APP_CPU_LOAD 1
89 #ifndef APP_LOOKUP_METHOD
90 #define APP_LOOKUP_METHOD APP_LOOKUP_LPM
93 #ifndef __GLIBC__ /* sched_getcpu() is glibc specific */
94 #define sched_getcpu() rte_lcore_id()
98 check_ptype(int portid)
101 int ipv4 = 0, ipv6 = 0;
103 ret = rte_eth_dev_get_supported_ptypes(portid, RTE_PTYPE_L3_MASK, NULL,
108 uint32_t ptypes[ret];
110 ret = rte_eth_dev_get_supported_ptypes(portid, RTE_PTYPE_L3_MASK,
112 for (i = 0; i < ret; ++i) {
113 if (ptypes[i] & RTE_PTYPE_L3_IPV4)
115 if (ptypes[i] & RTE_PTYPE_L3_IPV6)
126 parse_ptype(struct rte_mbuf *m)
128 struct ether_hdr *eth_hdr;
129 uint32_t packet_type = RTE_PTYPE_UNKNOWN;
132 eth_hdr = rte_pktmbuf_mtod(m, struct ether_hdr *);
133 ether_type = eth_hdr->ether_type;
134 if (ether_type == rte_cpu_to_be_16(ETHER_TYPE_IPv4))
135 packet_type |= RTE_PTYPE_L3_IPV4_EXT_UNKNOWN;
136 else if (ether_type == rte_cpu_to_be_16(ETHER_TYPE_IPv6))
137 packet_type |= RTE_PTYPE_L3_IPV6_EXT_UNKNOWN;
139 m->packet_type = packet_type;
143 cb_parse_ptype(__rte_unused uint16_t port, __rte_unused uint16_t queue,
144 struct rte_mbuf *pkts[], uint16_t nb_pkts,
145 __rte_unused uint16_t max_pkts, __rte_unused void *user_param)
149 for (i = 0; i < nb_pkts; i++)
150 parse_ptype(pkts[i]);
156 * When set to zero, simple forwaring path is eanbled.
157 * When set to one, optimized forwarding path is enabled.
158 * Note that LPM optimisation path uses SSE4.1 instructions.
160 #define ENABLE_MULTI_BUFFER_OPTIMIZE 1
162 #if (APP_LOOKUP_METHOD == APP_LOOKUP_EXACT_MATCH)
163 #include <rte_hash.h>
164 #elif (APP_LOOKUP_METHOD == APP_LOOKUP_LPM)
166 #include <rte_lpm6.h>
168 #error "APP_LOOKUP_METHOD set to incorrect value"
171 #define RTE_LOGTYPE_L3FWD RTE_LOGTYPE_USER1
173 #define MAX_JUMBO_PKT_LEN 9600
175 #define IPV6_ADDR_LEN 16
177 #define MEMPOOL_CACHE_SIZE 256
180 * This expression is used to calculate the number of mbufs needed depending on
181 * user input, taking into account memory for rx and tx hardware rings, cache
182 * per lcore and mtable per port per lcore. RTE_MAX is used to ensure that
183 * NB_MBUF never goes below a minimum value of 8192
186 #define NB_MBUF RTE_MAX(\
187 (nb_ports*nb_rx_queue*nb_rxd + \
188 nb_ports*nb_lcores*MAX_PKT_BURST + \
189 nb_ports*n_tx_queue*nb_txd + \
190 nb_lcores*MEMPOOL_CACHE_SIZE), \
193 #define MAX_PKT_BURST 32
194 #define BURST_TX_DRAIN_US 100 /* TX drain every ~100us */
197 * Try to avoid TX buffering if we have at least MAX_TX_BURST packets to send.
199 #define MAX_TX_BURST (MAX_PKT_BURST / 2)
200 #define BURST_SIZE MAX_TX_BURST
204 /* Configure how many packets ahead to prefetch, when reading packets */
205 #define PREFETCH_OFFSET 3
207 /* Used to mark destination port as 'invalid'. */
208 #define BAD_PORT ((uint16_t)-1)
213 * Configurable number of RX/TX ring descriptors
215 #define RTE_TEST_RX_DESC_DEFAULT 128
216 #define RTE_TEST_TX_DESC_DEFAULT 128
217 static uint16_t nb_rxd = RTE_TEST_RX_DESC_DEFAULT;
218 static uint16_t nb_txd = RTE_TEST_TX_DESC_DEFAULT;
220 /* ethernet addresses of ports */
221 static uint64_t dest_eth_addr[RTE_MAX_ETHPORTS];
222 static struct ether_addr ports_eth_addr[RTE_MAX_ETHPORTS];
224 static xmm_t val_eth[RTE_MAX_ETHPORTS];
226 /* replace first 12B of the ethernet header. */
227 #define MASK_ETH 0x3f
229 /* mask of enabled ports */
230 static uint32_t enabled_port_mask;
231 static int promiscuous_on; /**< Set in promiscuous mode off by default. */
232 static int numa_on = 1; /**< NUMA is enabled by default. */
233 static int parse_ptype_on;
235 #if (APP_LOOKUP_METHOD == APP_LOOKUP_EXACT_MATCH)
236 static int ipv6; /**< ipv6 is false by default. */
239 #if (APP_CPU_LOAD == 1)
241 #define MAX_CPU RTE_MAX_LCORE
242 #define CPU_LOAD_TIMEOUT_US (5 * 1000 * 1000) /**< Timeout for collecting 5s */
244 #define CPU_PROCESS 0
246 #define MAX_CPU_COUNTER 2
251 uint64_t hits[MAX_CPU_COUNTER][MAX_CPU];
252 } __rte_cache_aligned;
254 static struct cpu_load cpu_load;
255 static int cpu_load_lcore_id = -1;
257 #define SET_CPU_BUSY(thread, counter) \
258 thread->conf.busy[counter] = 1
260 #define SET_CPU_IDLE(thread, counter) \
261 thread->conf.busy[counter] = 0
263 #define IS_CPU_BUSY(thread, counter) \
264 (thread->conf.busy[counter] > 0)
268 #define SET_CPU_BUSY(thread, counter)
269 #define SET_CPU_IDLE(thread, counter)
270 #define IS_CPU_BUSY(thread, counter) 0
276 struct rte_mbuf *m_table[MAX_PKT_BURST];
279 struct lcore_rx_queue {
282 } __rte_cache_aligned;
284 #define MAX_RX_QUEUE_PER_LCORE 16
285 #define MAX_TX_QUEUE_PER_PORT RTE_MAX_ETHPORTS
286 #define MAX_RX_QUEUE_PER_PORT 128
288 #define MAX_LCORE_PARAMS 1024
289 struct rx_thread_params {
294 } __rte_cache_aligned;
296 static struct rx_thread_params rx_thread_params_array[MAX_LCORE_PARAMS];
297 static struct rx_thread_params rx_thread_params_array_default[] = {
309 static struct rx_thread_params *rx_thread_params =
310 rx_thread_params_array_default;
311 static uint16_t nb_rx_thread_params = RTE_DIM(rx_thread_params_array_default);
313 struct tx_thread_params {
316 } __rte_cache_aligned;
318 static struct tx_thread_params tx_thread_params_array[MAX_LCORE_PARAMS];
319 static struct tx_thread_params tx_thread_params_array_default[] = {
331 static struct tx_thread_params *tx_thread_params =
332 tx_thread_params_array_default;
333 static uint16_t nb_tx_thread_params = RTE_DIM(tx_thread_params_array_default);
335 static struct rte_eth_conf port_conf = {
337 .mq_mode = ETH_MQ_RX_RSS,
338 .max_rx_pkt_len = ETHER_MAX_LEN,
340 .header_split = 0, /**< Header Split disabled */
341 .hw_ip_checksum = 1, /**< IP checksum offload enabled */
342 .hw_vlan_filter = 0, /**< VLAN filtering disabled */
343 .jumbo_frame = 0, /**< Jumbo Frame Support disabled */
344 .hw_strip_crc = 1, /**< CRC stripped by hardware */
349 .rss_hf = ETH_RSS_TCP,
353 .mq_mode = ETH_MQ_TX_NONE,
357 static struct rte_mempool *pktmbuf_pool[NB_SOCKETS];
359 #if (APP_LOOKUP_METHOD == APP_LOOKUP_EXACT_MATCH)
361 #include <rte_hash_crc.h>
362 #define DEFAULT_HASH_FUNC rte_hash_crc
370 } __attribute__((__packed__));
372 union ipv4_5tuple_host {
385 #define XMM_NUM_IN_IPV6_5TUPLE 3
388 uint8_t ip_dst[IPV6_ADDR_LEN];
389 uint8_t ip_src[IPV6_ADDR_LEN];
393 } __attribute__((__packed__));
395 union ipv6_5tuple_host {
400 uint8_t ip_src[IPV6_ADDR_LEN];
401 uint8_t ip_dst[IPV6_ADDR_LEN];
406 __m128i xmm[XMM_NUM_IN_IPV6_5TUPLE];
409 struct ipv4_l3fwd_route {
410 struct ipv4_5tuple key;
414 struct ipv6_l3fwd_route {
415 struct ipv6_5tuple key;
419 static struct ipv4_l3fwd_route ipv4_l3fwd_route_array[] = {
420 {{IPv4(101, 0, 0, 0), IPv4(100, 10, 0, 1), 101, 11, IPPROTO_TCP}, 0},
421 {{IPv4(201, 0, 0, 0), IPv4(200, 20, 0, 1), 102, 12, IPPROTO_TCP}, 1},
422 {{IPv4(111, 0, 0, 0), IPv4(100, 30, 0, 1), 101, 11, IPPROTO_TCP}, 2},
423 {{IPv4(211, 0, 0, 0), IPv4(200, 40, 0, 1), 102, 12, IPPROTO_TCP}, 3},
426 static struct ipv6_l3fwd_route ipv6_l3fwd_route_array[] = {
428 {0xfe, 0x80, 0, 0, 0, 0, 0, 0, 0x02, 0x1e, 0x67, 0xff, 0xfe, 0, 0, 0},
429 {0xfe, 0x80, 0, 0, 0, 0, 0, 0, 0x02, 0x1b, 0x21, 0xff, 0xfe, 0x91, 0x38,
431 101, 11, IPPROTO_TCP}, 0},
434 {0xfe, 0x90, 0, 0, 0, 0, 0, 0, 0x02, 0x1e, 0x67, 0xff, 0xfe, 0, 0, 0},
435 {0xfe, 0x90, 0, 0, 0, 0, 0, 0, 0x02, 0x1b, 0x21, 0xff, 0xfe, 0x91, 0x38,
437 102, 12, IPPROTO_TCP}, 1},
440 {0xfe, 0xa0, 0, 0, 0, 0, 0, 0, 0x02, 0x1e, 0x67, 0xff, 0xfe, 0, 0, 0},
441 {0xfe, 0xa0, 0, 0, 0, 0, 0, 0, 0x02, 0x1b, 0x21, 0xff, 0xfe, 0x91, 0x38,
443 101, 11, IPPROTO_TCP}, 2},
446 {0xfe, 0xb0, 0, 0, 0, 0, 0, 0, 0x02, 0x1e, 0x67, 0xff, 0xfe, 0, 0, 0},
447 {0xfe, 0xb0, 0, 0, 0, 0, 0, 0, 0x02, 0x1b, 0x21, 0xff, 0xfe, 0x91, 0x38,
449 102, 12, IPPROTO_TCP}, 3},
452 typedef struct rte_hash lookup_struct_t;
453 static lookup_struct_t *ipv4_l3fwd_lookup_struct[NB_SOCKETS];
454 static lookup_struct_t *ipv6_l3fwd_lookup_struct[NB_SOCKETS];
456 #ifdef RTE_ARCH_X86_64
457 /* default to 4 million hash entries (approx) */
458 #define L3FWD_HASH_ENTRIES (1024*1024*4)
460 /* 32-bit has less address-space for hugepage memory, limit to 1M entries */
461 #define L3FWD_HASH_ENTRIES (1024*1024*1)
463 #define HASH_ENTRY_NUMBER_DEFAULT 4
465 static uint32_t hash_entry_number = HASH_ENTRY_NUMBER_DEFAULT;
467 static inline uint32_t
468 ipv4_hash_crc(const void *data, __rte_unused uint32_t data_len,
471 const union ipv4_5tuple_host *k;
477 p = (const uint32_t *)&k->port_src;
479 init_val = rte_hash_crc_4byte(t, init_val);
480 init_val = rte_hash_crc_4byte(k->ip_src, init_val);
481 init_val = rte_hash_crc_4byte(k->ip_dst, init_val);
482 init_val = rte_hash_crc_4byte(*p, init_val);
486 static inline uint32_t
487 ipv6_hash_crc(const void *data, __rte_unused uint32_t data_len,
490 const union ipv6_5tuple_host *k;
493 const uint32_t *ip_src0, *ip_src1, *ip_src2, *ip_src3;
494 const uint32_t *ip_dst0, *ip_dst1, *ip_dst2, *ip_dst3;
498 p = (const uint32_t *)&k->port_src;
500 ip_src0 = (const uint32_t *) k->ip_src;
501 ip_src1 = (const uint32_t *)(k->ip_src + 4);
502 ip_src2 = (const uint32_t *)(k->ip_src + 8);
503 ip_src3 = (const uint32_t *)(k->ip_src + 12);
504 ip_dst0 = (const uint32_t *) k->ip_dst;
505 ip_dst1 = (const uint32_t *)(k->ip_dst + 4);
506 ip_dst2 = (const uint32_t *)(k->ip_dst + 8);
507 ip_dst3 = (const uint32_t *)(k->ip_dst + 12);
508 init_val = rte_hash_crc_4byte(t, init_val);
509 init_val = rte_hash_crc_4byte(*ip_src0, init_val);
510 init_val = rte_hash_crc_4byte(*ip_src1, init_val);
511 init_val = rte_hash_crc_4byte(*ip_src2, init_val);
512 init_val = rte_hash_crc_4byte(*ip_src3, init_val);
513 init_val = rte_hash_crc_4byte(*ip_dst0, init_val);
514 init_val = rte_hash_crc_4byte(*ip_dst1, init_val);
515 init_val = rte_hash_crc_4byte(*ip_dst2, init_val);
516 init_val = rte_hash_crc_4byte(*ip_dst3, init_val);
517 init_val = rte_hash_crc_4byte(*p, init_val);
521 #define IPV4_L3FWD_NUM_ROUTES RTE_DIM(ipv4_l3fwd_route_array)
522 #define IPV6_L3FWD_NUM_ROUTES RTE_DIM(ipv6_l3fwd_route_array)
524 static uint8_t ipv4_l3fwd_out_if[L3FWD_HASH_ENTRIES] __rte_cache_aligned;
525 static uint8_t ipv6_l3fwd_out_if[L3FWD_HASH_ENTRIES] __rte_cache_aligned;
529 #if (APP_LOOKUP_METHOD == APP_LOOKUP_LPM)
530 struct ipv4_l3fwd_route {
536 struct ipv6_l3fwd_route {
542 static struct ipv4_l3fwd_route ipv4_l3fwd_route_array[] = {
543 {IPv4(1, 1, 1, 0), 24, 0},
544 {IPv4(2, 1, 1, 0), 24, 1},
545 {IPv4(3, 1, 1, 0), 24, 2},
546 {IPv4(4, 1, 1, 0), 24, 3},
547 {IPv4(5, 1, 1, 0), 24, 4},
548 {IPv4(6, 1, 1, 0), 24, 5},
549 {IPv4(7, 1, 1, 0), 24, 6},
550 {IPv4(8, 1, 1, 0), 24, 7},
553 static struct ipv6_l3fwd_route ipv6_l3fwd_route_array[] = {
554 {{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, 48, 0},
555 {{2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, 48, 1},
556 {{3, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, 48, 2},
557 {{4, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, 48, 3},
558 {{5, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, 48, 4},
559 {{6, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, 48, 5},
560 {{7, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, 48, 6},
561 {{8, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, 48, 7},
564 #define IPV4_L3FWD_NUM_ROUTES RTE_DIM(ipv4_l3fwd_route_array)
565 #define IPV6_L3FWD_NUM_ROUTES RTE_DIM(ipv6_l3fwd_route_array)
567 #define IPV4_L3FWD_LPM_MAX_RULES 1024
568 #define IPV6_L3FWD_LPM_MAX_RULES 1024
569 #define IPV6_L3FWD_LPM_NUMBER_TBL8S (1 << 16)
571 typedef struct rte_lpm lookup_struct_t;
572 typedef struct rte_lpm6 lookup6_struct_t;
573 static lookup_struct_t *ipv4_l3fwd_lookup_struct[NB_SOCKETS];
574 static lookup6_struct_t *ipv6_l3fwd_lookup_struct[NB_SOCKETS];
578 lookup_struct_t *ipv4_lookup_struct;
579 #if (APP_LOOKUP_METHOD == APP_LOOKUP_LPM)
580 lookup6_struct_t *ipv6_lookup_struct;
582 lookup_struct_t *ipv6_lookup_struct;
585 } __rte_cache_aligned;
587 static struct lcore_conf lcore_conf[RTE_MAX_LCORE];
588 RTE_DEFINE_PER_LCORE(struct lcore_conf *, lcore_conf);
590 #define MAX_RX_QUEUE_PER_THREAD 16
591 #define MAX_TX_PORT_PER_THREAD RTE_MAX_ETHPORTS
592 #define MAX_TX_QUEUE_PER_PORT RTE_MAX_ETHPORTS
593 #define MAX_RX_QUEUE_PER_PORT 128
595 #define MAX_RX_THREAD 1024
596 #define MAX_TX_THREAD 1024
597 #define MAX_THREAD (MAX_RX_THREAD + MAX_TX_THREAD)
600 * Producers and consumers threads configuration
602 static int lthreads_on = 1; /**< Use lthreads for processing*/
604 rte_atomic16_t rx_counter; /**< Number of spawned rx threads */
605 rte_atomic16_t tx_counter; /**< Number of spawned tx threads */
608 uint16_t lcore_id; /**< Initial lcore for rx thread */
609 uint16_t cpu_id; /**< Cpu id for cpu load stats counter */
610 uint16_t thread_id; /**< Thread ID */
612 #if (APP_CPU_LOAD > 0)
613 int busy[MAX_CPU_COUNTER];
617 struct thread_rx_conf {
618 struct thread_conf conf;
621 struct lcore_rx_queue rx_queue_list[MAX_RX_QUEUE_PER_LCORE];
623 uint16_t n_ring; /**< Number of output rings */
624 struct rte_ring *ring[RTE_MAX_LCORE];
625 struct lthread_cond *ready[RTE_MAX_LCORE];
627 #if (APP_CPU_LOAD > 0)
628 int busy[MAX_CPU_COUNTER];
630 } __rte_cache_aligned;
632 uint16_t n_rx_thread;
633 struct thread_rx_conf rx_thread[MAX_RX_THREAD];
635 struct thread_tx_conf {
636 struct thread_conf conf;
638 uint16_t tx_queue_id[RTE_MAX_LCORE];
639 struct mbuf_table tx_mbufs[RTE_MAX_LCORE];
641 struct rte_ring *ring;
642 struct lthread_cond **ready;
644 } __rte_cache_aligned;
646 uint16_t n_tx_thread;
647 struct thread_tx_conf tx_thread[MAX_TX_THREAD];
649 /* Send burst of packets on an output interface */
651 send_burst(struct thread_tx_conf *qconf, uint16_t n, uint16_t port)
653 struct rte_mbuf **m_table;
657 queueid = qconf->tx_queue_id[port];
658 m_table = (struct rte_mbuf **)qconf->tx_mbufs[port].m_table;
660 ret = rte_eth_tx_burst(port, queueid, m_table, n);
661 if (unlikely(ret < n)) {
663 rte_pktmbuf_free(m_table[ret]);
670 /* Enqueue a single packet, and send burst if queue is filled */
672 send_single_packet(struct rte_mbuf *m, uint16_t port)
675 struct thread_tx_conf *qconf;
678 qconf = (struct thread_tx_conf *)lthread_get_data();
680 qconf = (struct thread_tx_conf *)RTE_PER_LCORE(lcore_conf)->data;
682 len = qconf->tx_mbufs[port].len;
683 qconf->tx_mbufs[port].m_table[len] = m;
686 /* enough pkts to be sent */
687 if (unlikely(len == MAX_PKT_BURST)) {
688 send_burst(qconf, MAX_PKT_BURST, port);
692 qconf->tx_mbufs[port].len = len;
696 #if ((APP_LOOKUP_METHOD == APP_LOOKUP_LPM) && \
697 (ENABLE_MULTI_BUFFER_OPTIMIZE == 1))
698 static __rte_always_inline void
699 send_packetsx4(uint16_t port,
700 struct rte_mbuf *m[], uint32_t num)
703 struct thread_tx_conf *qconf;
706 qconf = (struct thread_tx_conf *)lthread_get_data();
708 qconf = (struct thread_tx_conf *)RTE_PER_LCORE(lcore_conf)->data;
710 len = qconf->tx_mbufs[port].len;
713 * If TX buffer for that queue is empty, and we have enough packets,
714 * then send them straightway.
716 if (num >= MAX_TX_BURST && len == 0) {
717 n = rte_eth_tx_burst(port, qconf->tx_queue_id[port], m, num);
718 if (unlikely(n < num)) {
720 rte_pktmbuf_free(m[n]);
727 * Put packets into TX buffer for that queue.
731 n = (n > MAX_PKT_BURST) ? MAX_PKT_BURST - len : num;
734 switch (n % FWDSTEP) {
737 qconf->tx_mbufs[port].m_table[len + j] = m[j];
741 qconf->tx_mbufs[port].m_table[len + j] = m[j];
745 qconf->tx_mbufs[port].m_table[len + j] = m[j];
749 qconf->tx_mbufs[port].m_table[len + j] = m[j];
756 /* enough pkts to be sent */
757 if (unlikely(len == MAX_PKT_BURST)) {
759 send_burst(qconf, MAX_PKT_BURST, port);
761 /* copy rest of the packets into the TX buffer. */
764 switch (len % FWDSTEP) {
767 qconf->tx_mbufs[port].m_table[j] = m[n + j];
771 qconf->tx_mbufs[port].m_table[j] = m[n + j];
775 qconf->tx_mbufs[port].m_table[j] = m[n + j];
779 qconf->tx_mbufs[port].m_table[j] = m[n + j];
785 qconf->tx_mbufs[port].len = len;
787 #endif /* APP_LOOKUP_LPM */
789 #ifdef DO_RFC_1812_CHECKS
791 is_valid_ipv4_pkt(struct ipv4_hdr *pkt, uint32_t link_len)
793 /* From http://www.rfc-editor.org/rfc/rfc1812.txt section 5.2.2 */
795 * 1. The packet length reported by the Link Layer must be large
796 * enough to hold the minimum length legal IP datagram (20 bytes).
798 if (link_len < sizeof(struct ipv4_hdr))
801 /* 2. The IP checksum must be correct. */
802 /* this is checked in H/W */
805 * 3. The IP version number must be 4. If the version number is not 4
806 * then the packet may be another version of IP, such as IPng or
809 if (((pkt->version_ihl) >> 4) != 4)
812 * 4. The IP header length field must be large enough to hold the
813 * minimum length legal IP datagram (20 bytes = 5 words).
815 if ((pkt->version_ihl & 0xf) < 5)
819 * 5. The IP total length field must be large enough to hold the IP
820 * datagram header, whose length is specified in the IP header length
823 if (rte_cpu_to_be_16(pkt->total_length) < sizeof(struct ipv4_hdr))
830 #if (APP_LOOKUP_METHOD == APP_LOOKUP_EXACT_MATCH)
832 static __m128i mask0;
833 static __m128i mask1;
834 static __m128i mask2;
835 static inline uint16_t
836 get_ipv4_dst_port(void *ipv4_hdr, uint16_t portid,
837 lookup_struct_t *ipv4_l3fwd_lookup_struct)
840 union ipv4_5tuple_host key;
842 ipv4_hdr = (uint8_t *)ipv4_hdr + offsetof(struct ipv4_hdr, time_to_live);
843 __m128i data = _mm_loadu_si128((__m128i *)(ipv4_hdr));
844 /* Get 5 tuple: dst port, src port, dst IP address, src IP address and
846 key.xmm = _mm_and_si128(data, mask0);
847 /* Find destination port */
848 ret = rte_hash_lookup(ipv4_l3fwd_lookup_struct, (const void *)&key);
849 return ((ret < 0) ? portid : ipv4_l3fwd_out_if[ret]);
852 static inline uint16_t
853 get_ipv6_dst_port(void *ipv6_hdr, uint16_t portid,
854 lookup_struct_t *ipv6_l3fwd_lookup_struct)
857 union ipv6_5tuple_host key;
859 ipv6_hdr = (uint8_t *)ipv6_hdr + offsetof(struct ipv6_hdr, payload_len);
860 __m128i data0 = _mm_loadu_si128((__m128i *)(ipv6_hdr));
861 __m128i data1 = _mm_loadu_si128((__m128i *)(((uint8_t *)ipv6_hdr) +
863 __m128i data2 = _mm_loadu_si128((__m128i *)(((uint8_t *)ipv6_hdr) +
864 sizeof(__m128i) + sizeof(__m128i)));
865 /* Get part of 5 tuple: src IP address lower 96 bits and protocol */
866 key.xmm[0] = _mm_and_si128(data0, mask1);
867 /* Get part of 5 tuple: dst IP address lower 96 bits and src IP address
870 /* Get part of 5 tuple: dst port and src port and dst IP address higher
872 key.xmm[2] = _mm_and_si128(data2, mask2);
874 /* Find destination port */
875 ret = rte_hash_lookup(ipv6_l3fwd_lookup_struct, (const void *)&key);
876 return ((ret < 0) ? portid : ipv6_l3fwd_out_if[ret]);
880 #if (APP_LOOKUP_METHOD == APP_LOOKUP_LPM)
882 static inline uint16_t
883 get_ipv4_dst_port(void *ipv4_hdr, uint16_t portid,
884 lookup_struct_t *ipv4_l3fwd_lookup_struct)
888 return ((rte_lpm_lookup(ipv4_l3fwd_lookup_struct,
889 rte_be_to_cpu_32(((struct ipv4_hdr *)ipv4_hdr)->dst_addr),
890 &next_hop) == 0) ? next_hop : portid);
893 static inline uint16_t
894 get_ipv6_dst_port(void *ipv6_hdr, uint16_t portid,
895 lookup6_struct_t *ipv6_l3fwd_lookup_struct)
899 return ((rte_lpm6_lookup(ipv6_l3fwd_lookup_struct,
900 ((struct ipv6_hdr *)ipv6_hdr)->dst_addr, &next_hop) == 0) ?
905 static inline void l3fwd_simple_forward(struct rte_mbuf *m, uint16_t portid)
906 __attribute__((unused));
908 #if ((APP_LOOKUP_METHOD == APP_LOOKUP_EXACT_MATCH) && \
909 (ENABLE_MULTI_BUFFER_OPTIMIZE == 1))
911 #define MASK_ALL_PKTS 0xff
912 #define EXCLUDE_1ST_PKT 0xfe
913 #define EXCLUDE_2ND_PKT 0xfd
914 #define EXCLUDE_3RD_PKT 0xfb
915 #define EXCLUDE_4TH_PKT 0xf7
916 #define EXCLUDE_5TH_PKT 0xef
917 #define EXCLUDE_6TH_PKT 0xdf
918 #define EXCLUDE_7TH_PKT 0xbf
919 #define EXCLUDE_8TH_PKT 0x7f
922 simple_ipv4_fwd_8pkts(struct rte_mbuf *m[8], uint16_t portid)
924 struct ether_hdr *eth_hdr[8];
925 struct ipv4_hdr *ipv4_hdr[8];
926 uint16_t dst_port[8];
928 union ipv4_5tuple_host key[8];
931 eth_hdr[0] = rte_pktmbuf_mtod(m[0], struct ether_hdr *);
932 eth_hdr[1] = rte_pktmbuf_mtod(m[1], struct ether_hdr *);
933 eth_hdr[2] = rte_pktmbuf_mtod(m[2], struct ether_hdr *);
934 eth_hdr[3] = rte_pktmbuf_mtod(m[3], struct ether_hdr *);
935 eth_hdr[4] = rte_pktmbuf_mtod(m[4], struct ether_hdr *);
936 eth_hdr[5] = rte_pktmbuf_mtod(m[5], struct ether_hdr *);
937 eth_hdr[6] = rte_pktmbuf_mtod(m[6], struct ether_hdr *);
938 eth_hdr[7] = rte_pktmbuf_mtod(m[7], struct ether_hdr *);
940 /* Handle IPv4 headers.*/
941 ipv4_hdr[0] = rte_pktmbuf_mtod_offset(m[0], struct ipv4_hdr *,
942 sizeof(struct ether_hdr));
943 ipv4_hdr[1] = rte_pktmbuf_mtod_offset(m[1], struct ipv4_hdr *,
944 sizeof(struct ether_hdr));
945 ipv4_hdr[2] = rte_pktmbuf_mtod_offset(m[2], struct ipv4_hdr *,
946 sizeof(struct ether_hdr));
947 ipv4_hdr[3] = rte_pktmbuf_mtod_offset(m[3], struct ipv4_hdr *,
948 sizeof(struct ether_hdr));
949 ipv4_hdr[4] = rte_pktmbuf_mtod_offset(m[4], struct ipv4_hdr *,
950 sizeof(struct ether_hdr));
951 ipv4_hdr[5] = rte_pktmbuf_mtod_offset(m[5], struct ipv4_hdr *,
952 sizeof(struct ether_hdr));
953 ipv4_hdr[6] = rte_pktmbuf_mtod_offset(m[6], struct ipv4_hdr *,
954 sizeof(struct ether_hdr));
955 ipv4_hdr[7] = rte_pktmbuf_mtod_offset(m[7], struct ipv4_hdr *,
956 sizeof(struct ether_hdr));
958 #ifdef DO_RFC_1812_CHECKS
959 /* Check to make sure the packet is valid (RFC1812) */
960 uint8_t valid_mask = MASK_ALL_PKTS;
962 if (is_valid_ipv4_pkt(ipv4_hdr[0], m[0]->pkt_len) < 0) {
963 rte_pktmbuf_free(m[0]);
964 valid_mask &= EXCLUDE_1ST_PKT;
966 if (is_valid_ipv4_pkt(ipv4_hdr[1], m[1]->pkt_len) < 0) {
967 rte_pktmbuf_free(m[1]);
968 valid_mask &= EXCLUDE_2ND_PKT;
970 if (is_valid_ipv4_pkt(ipv4_hdr[2], m[2]->pkt_len) < 0) {
971 rte_pktmbuf_free(m[2]);
972 valid_mask &= EXCLUDE_3RD_PKT;
974 if (is_valid_ipv4_pkt(ipv4_hdr[3], m[3]->pkt_len) < 0) {
975 rte_pktmbuf_free(m[3]);
976 valid_mask &= EXCLUDE_4TH_PKT;
978 if (is_valid_ipv4_pkt(ipv4_hdr[4], m[4]->pkt_len) < 0) {
979 rte_pktmbuf_free(m[4]);
980 valid_mask &= EXCLUDE_5TH_PKT;
982 if (is_valid_ipv4_pkt(ipv4_hdr[5], m[5]->pkt_len) < 0) {
983 rte_pktmbuf_free(m[5]);
984 valid_mask &= EXCLUDE_6TH_PKT;
986 if (is_valid_ipv4_pkt(ipv4_hdr[6], m[6]->pkt_len) < 0) {
987 rte_pktmbuf_free(m[6]);
988 valid_mask &= EXCLUDE_7TH_PKT;
990 if (is_valid_ipv4_pkt(ipv4_hdr[7], m[7]->pkt_len) < 0) {
991 rte_pktmbuf_free(m[7]);
992 valid_mask &= EXCLUDE_8TH_PKT;
994 if (unlikely(valid_mask != MASK_ALL_PKTS)) {
1000 for (i = 0; i < 8; i++)
1001 if ((0x1 << i) & valid_mask)
1002 l3fwd_simple_forward(m[i], portid);
1004 #endif /* End of #ifdef DO_RFC_1812_CHECKS */
1006 data[0] = _mm_loadu_si128(rte_pktmbuf_mtod_offset(m[0], __m128i *,
1007 sizeof(struct ether_hdr) +
1008 offsetof(struct ipv4_hdr, time_to_live)));
1009 data[1] = _mm_loadu_si128(rte_pktmbuf_mtod_offset(m[1], __m128i *,
1010 sizeof(struct ether_hdr) +
1011 offsetof(struct ipv4_hdr, time_to_live)));
1012 data[2] = _mm_loadu_si128(rte_pktmbuf_mtod_offset(m[2], __m128i *,
1013 sizeof(struct ether_hdr) +
1014 offsetof(struct ipv4_hdr, time_to_live)));
1015 data[3] = _mm_loadu_si128(rte_pktmbuf_mtod_offset(m[3], __m128i *,
1016 sizeof(struct ether_hdr) +
1017 offsetof(struct ipv4_hdr, time_to_live)));
1018 data[4] = _mm_loadu_si128(rte_pktmbuf_mtod_offset(m[4], __m128i *,
1019 sizeof(struct ether_hdr) +
1020 offsetof(struct ipv4_hdr, time_to_live)));
1021 data[5] = _mm_loadu_si128(rte_pktmbuf_mtod_offset(m[5], __m128i *,
1022 sizeof(struct ether_hdr) +
1023 offsetof(struct ipv4_hdr, time_to_live)));
1024 data[6] = _mm_loadu_si128(rte_pktmbuf_mtod_offset(m[6], __m128i *,
1025 sizeof(struct ether_hdr) +
1026 offsetof(struct ipv4_hdr, time_to_live)));
1027 data[7] = _mm_loadu_si128(rte_pktmbuf_mtod_offset(m[7], __m128i *,
1028 sizeof(struct ether_hdr) +
1029 offsetof(struct ipv4_hdr, time_to_live)));
1031 key[0].xmm = _mm_and_si128(data[0], mask0);
1032 key[1].xmm = _mm_and_si128(data[1], mask0);
1033 key[2].xmm = _mm_and_si128(data[2], mask0);
1034 key[3].xmm = _mm_and_si128(data[3], mask0);
1035 key[4].xmm = _mm_and_si128(data[4], mask0);
1036 key[5].xmm = _mm_and_si128(data[5], mask0);
1037 key[6].xmm = _mm_and_si128(data[6], mask0);
1038 key[7].xmm = _mm_and_si128(data[7], mask0);
1040 const void *key_array[8] = {&key[0], &key[1], &key[2], &key[3],
1041 &key[4], &key[5], &key[6], &key[7]};
1043 rte_hash_lookup_bulk(RTE_PER_LCORE(lcore_conf)->ipv4_lookup_struct,
1044 &key_array[0], 8, ret);
1045 dst_port[0] = ((ret[0] < 0) ? portid : ipv4_l3fwd_out_if[ret[0]]);
1046 dst_port[1] = ((ret[1] < 0) ? portid : ipv4_l3fwd_out_if[ret[1]]);
1047 dst_port[2] = ((ret[2] < 0) ? portid : ipv4_l3fwd_out_if[ret[2]]);
1048 dst_port[3] = ((ret[3] < 0) ? portid : ipv4_l3fwd_out_if[ret[3]]);
1049 dst_port[4] = ((ret[4] < 0) ? portid : ipv4_l3fwd_out_if[ret[4]]);
1050 dst_port[5] = ((ret[5] < 0) ? portid : ipv4_l3fwd_out_if[ret[5]]);
1051 dst_port[6] = ((ret[6] < 0) ? portid : ipv4_l3fwd_out_if[ret[6]]);
1052 dst_port[7] = ((ret[7] < 0) ? portid : ipv4_l3fwd_out_if[ret[7]]);
1054 if (dst_port[0] >= RTE_MAX_ETHPORTS ||
1055 (enabled_port_mask & 1 << dst_port[0]) == 0)
1056 dst_port[0] = portid;
1057 if (dst_port[1] >= RTE_MAX_ETHPORTS ||
1058 (enabled_port_mask & 1 << dst_port[1]) == 0)
1059 dst_port[1] = portid;
1060 if (dst_port[2] >= RTE_MAX_ETHPORTS ||
1061 (enabled_port_mask & 1 << dst_port[2]) == 0)
1062 dst_port[2] = portid;
1063 if (dst_port[3] >= RTE_MAX_ETHPORTS ||
1064 (enabled_port_mask & 1 << dst_port[3]) == 0)
1065 dst_port[3] = portid;
1066 if (dst_port[4] >= RTE_MAX_ETHPORTS ||
1067 (enabled_port_mask & 1 << dst_port[4]) == 0)
1068 dst_port[4] = portid;
1069 if (dst_port[5] >= RTE_MAX_ETHPORTS ||
1070 (enabled_port_mask & 1 << dst_port[5]) == 0)
1071 dst_port[5] = portid;
1072 if (dst_port[6] >= RTE_MAX_ETHPORTS ||
1073 (enabled_port_mask & 1 << dst_port[6]) == 0)
1074 dst_port[6] = portid;
1075 if (dst_port[7] >= RTE_MAX_ETHPORTS ||
1076 (enabled_port_mask & 1 << dst_port[7]) == 0)
1077 dst_port[7] = portid;
1079 #ifdef DO_RFC_1812_CHECKS
1080 /* Update time to live and header checksum */
1081 --(ipv4_hdr[0]->time_to_live);
1082 --(ipv4_hdr[1]->time_to_live);
1083 --(ipv4_hdr[2]->time_to_live);
1084 --(ipv4_hdr[3]->time_to_live);
1085 ++(ipv4_hdr[0]->hdr_checksum);
1086 ++(ipv4_hdr[1]->hdr_checksum);
1087 ++(ipv4_hdr[2]->hdr_checksum);
1088 ++(ipv4_hdr[3]->hdr_checksum);
1089 --(ipv4_hdr[4]->time_to_live);
1090 --(ipv4_hdr[5]->time_to_live);
1091 --(ipv4_hdr[6]->time_to_live);
1092 --(ipv4_hdr[7]->time_to_live);
1093 ++(ipv4_hdr[4]->hdr_checksum);
1094 ++(ipv4_hdr[5]->hdr_checksum);
1095 ++(ipv4_hdr[6]->hdr_checksum);
1096 ++(ipv4_hdr[7]->hdr_checksum);
1100 *(uint64_t *)ð_hdr[0]->d_addr = dest_eth_addr[dst_port[0]];
1101 *(uint64_t *)ð_hdr[1]->d_addr = dest_eth_addr[dst_port[1]];
1102 *(uint64_t *)ð_hdr[2]->d_addr = dest_eth_addr[dst_port[2]];
1103 *(uint64_t *)ð_hdr[3]->d_addr = dest_eth_addr[dst_port[3]];
1104 *(uint64_t *)ð_hdr[4]->d_addr = dest_eth_addr[dst_port[4]];
1105 *(uint64_t *)ð_hdr[5]->d_addr = dest_eth_addr[dst_port[5]];
1106 *(uint64_t *)ð_hdr[6]->d_addr = dest_eth_addr[dst_port[6]];
1107 *(uint64_t *)ð_hdr[7]->d_addr = dest_eth_addr[dst_port[7]];
1110 ether_addr_copy(&ports_eth_addr[dst_port[0]], ð_hdr[0]->s_addr);
1111 ether_addr_copy(&ports_eth_addr[dst_port[1]], ð_hdr[1]->s_addr);
1112 ether_addr_copy(&ports_eth_addr[dst_port[2]], ð_hdr[2]->s_addr);
1113 ether_addr_copy(&ports_eth_addr[dst_port[3]], ð_hdr[3]->s_addr);
1114 ether_addr_copy(&ports_eth_addr[dst_port[4]], ð_hdr[4]->s_addr);
1115 ether_addr_copy(&ports_eth_addr[dst_port[5]], ð_hdr[5]->s_addr);
1116 ether_addr_copy(&ports_eth_addr[dst_port[6]], ð_hdr[6]->s_addr);
1117 ether_addr_copy(&ports_eth_addr[dst_port[7]], ð_hdr[7]->s_addr);
1119 send_single_packet(m[0], (uint8_t)dst_port[0]);
1120 send_single_packet(m[1], (uint8_t)dst_port[1]);
1121 send_single_packet(m[2], (uint8_t)dst_port[2]);
1122 send_single_packet(m[3], (uint8_t)dst_port[3]);
1123 send_single_packet(m[4], (uint8_t)dst_port[4]);
1124 send_single_packet(m[5], (uint8_t)dst_port[5]);
1125 send_single_packet(m[6], (uint8_t)dst_port[6]);
1126 send_single_packet(m[7], (uint8_t)dst_port[7]);
1130 static inline void get_ipv6_5tuple(struct rte_mbuf *m0, __m128i mask0,
1131 __m128i mask1, union ipv6_5tuple_host *key)
1133 __m128i tmpdata0 = _mm_loadu_si128(rte_pktmbuf_mtod_offset(m0,
1134 __m128i *, sizeof(struct ether_hdr) +
1135 offsetof(struct ipv6_hdr, payload_len)));
1136 __m128i tmpdata1 = _mm_loadu_si128(rte_pktmbuf_mtod_offset(m0,
1137 __m128i *, sizeof(struct ether_hdr) +
1138 offsetof(struct ipv6_hdr, payload_len) + sizeof(__m128i)));
1139 __m128i tmpdata2 = _mm_loadu_si128(rte_pktmbuf_mtod_offset(m0,
1140 __m128i *, sizeof(struct ether_hdr) +
1141 offsetof(struct ipv6_hdr, payload_len) + sizeof(__m128i) +
1143 key->xmm[0] = _mm_and_si128(tmpdata0, mask0);
1144 key->xmm[1] = tmpdata1;
1145 key->xmm[2] = _mm_and_si128(tmpdata2, mask1);
1149 simple_ipv6_fwd_8pkts(struct rte_mbuf *m[8], uint16_t portid)
1152 uint16_t dst_port[8];
1153 struct ether_hdr *eth_hdr[8];
1154 union ipv6_5tuple_host key[8];
1156 __attribute__((unused)) struct ipv6_hdr *ipv6_hdr[8];
1158 eth_hdr[0] = rte_pktmbuf_mtod(m[0], struct ether_hdr *);
1159 eth_hdr[1] = rte_pktmbuf_mtod(m[1], struct ether_hdr *);
1160 eth_hdr[2] = rte_pktmbuf_mtod(m[2], struct ether_hdr *);
1161 eth_hdr[3] = rte_pktmbuf_mtod(m[3], struct ether_hdr *);
1162 eth_hdr[4] = rte_pktmbuf_mtod(m[4], struct ether_hdr *);
1163 eth_hdr[5] = rte_pktmbuf_mtod(m[5], struct ether_hdr *);
1164 eth_hdr[6] = rte_pktmbuf_mtod(m[6], struct ether_hdr *);
1165 eth_hdr[7] = rte_pktmbuf_mtod(m[7], struct ether_hdr *);
1167 /* Handle IPv6 headers.*/
1168 ipv6_hdr[0] = rte_pktmbuf_mtod_offset(m[0], struct ipv6_hdr *,
1169 sizeof(struct ether_hdr));
1170 ipv6_hdr[1] = rte_pktmbuf_mtod_offset(m[1], struct ipv6_hdr *,
1171 sizeof(struct ether_hdr));
1172 ipv6_hdr[2] = rte_pktmbuf_mtod_offset(m[2], struct ipv6_hdr *,
1173 sizeof(struct ether_hdr));
1174 ipv6_hdr[3] = rte_pktmbuf_mtod_offset(m[3], struct ipv6_hdr *,
1175 sizeof(struct ether_hdr));
1176 ipv6_hdr[4] = rte_pktmbuf_mtod_offset(m[4], struct ipv6_hdr *,
1177 sizeof(struct ether_hdr));
1178 ipv6_hdr[5] = rte_pktmbuf_mtod_offset(m[5], struct ipv6_hdr *,
1179 sizeof(struct ether_hdr));
1180 ipv6_hdr[6] = rte_pktmbuf_mtod_offset(m[6], struct ipv6_hdr *,
1181 sizeof(struct ether_hdr));
1182 ipv6_hdr[7] = rte_pktmbuf_mtod_offset(m[7], struct ipv6_hdr *,
1183 sizeof(struct ether_hdr));
1185 get_ipv6_5tuple(m[0], mask1, mask2, &key[0]);
1186 get_ipv6_5tuple(m[1], mask1, mask2, &key[1]);
1187 get_ipv6_5tuple(m[2], mask1, mask2, &key[2]);
1188 get_ipv6_5tuple(m[3], mask1, mask2, &key[3]);
1189 get_ipv6_5tuple(m[4], mask1, mask2, &key[4]);
1190 get_ipv6_5tuple(m[5], mask1, mask2, &key[5]);
1191 get_ipv6_5tuple(m[6], mask1, mask2, &key[6]);
1192 get_ipv6_5tuple(m[7], mask1, mask2, &key[7]);
1194 const void *key_array[8] = {&key[0], &key[1], &key[2], &key[3],
1195 &key[4], &key[5], &key[6], &key[7]};
1197 rte_hash_lookup_bulk(RTE_PER_LCORE(lcore_conf)->ipv6_lookup_struct,
1198 &key_array[0], 4, ret);
1199 dst_port[0] = ((ret[0] < 0) ? portid : ipv6_l3fwd_out_if[ret[0]]);
1200 dst_port[1] = ((ret[1] < 0) ? portid : ipv6_l3fwd_out_if[ret[1]]);
1201 dst_port[2] = ((ret[2] < 0) ? portid : ipv6_l3fwd_out_if[ret[2]]);
1202 dst_port[3] = ((ret[3] < 0) ? portid : ipv6_l3fwd_out_if[ret[3]]);
1203 dst_port[4] = ((ret[4] < 0) ? portid : ipv6_l3fwd_out_if[ret[4]]);
1204 dst_port[5] = ((ret[5] < 0) ? portid : ipv6_l3fwd_out_if[ret[5]]);
1205 dst_port[6] = ((ret[6] < 0) ? portid : ipv6_l3fwd_out_if[ret[6]]);
1206 dst_port[7] = ((ret[7] < 0) ? portid : ipv6_l3fwd_out_if[ret[7]]);
1208 if (dst_port[0] >= RTE_MAX_ETHPORTS ||
1209 (enabled_port_mask & 1 << dst_port[0]) == 0)
1210 dst_port[0] = portid;
1211 if (dst_port[1] >= RTE_MAX_ETHPORTS ||
1212 (enabled_port_mask & 1 << dst_port[1]) == 0)
1213 dst_port[1] = portid;
1214 if (dst_port[2] >= RTE_MAX_ETHPORTS ||
1215 (enabled_port_mask & 1 << dst_port[2]) == 0)
1216 dst_port[2] = portid;
1217 if (dst_port[3] >= RTE_MAX_ETHPORTS ||
1218 (enabled_port_mask & 1 << dst_port[3]) == 0)
1219 dst_port[3] = portid;
1220 if (dst_port[4] >= RTE_MAX_ETHPORTS ||
1221 (enabled_port_mask & 1 << dst_port[4]) == 0)
1222 dst_port[4] = portid;
1223 if (dst_port[5] >= RTE_MAX_ETHPORTS ||
1224 (enabled_port_mask & 1 << dst_port[5]) == 0)
1225 dst_port[5] = portid;
1226 if (dst_port[6] >= RTE_MAX_ETHPORTS ||
1227 (enabled_port_mask & 1 << dst_port[6]) == 0)
1228 dst_port[6] = portid;
1229 if (dst_port[7] >= RTE_MAX_ETHPORTS ||
1230 (enabled_port_mask & 1 << dst_port[7]) == 0)
1231 dst_port[7] = portid;
1234 *(uint64_t *)ð_hdr[0]->d_addr = dest_eth_addr[dst_port[0]];
1235 *(uint64_t *)ð_hdr[1]->d_addr = dest_eth_addr[dst_port[1]];
1236 *(uint64_t *)ð_hdr[2]->d_addr = dest_eth_addr[dst_port[2]];
1237 *(uint64_t *)ð_hdr[3]->d_addr = dest_eth_addr[dst_port[3]];
1238 *(uint64_t *)ð_hdr[4]->d_addr = dest_eth_addr[dst_port[4]];
1239 *(uint64_t *)ð_hdr[5]->d_addr = dest_eth_addr[dst_port[5]];
1240 *(uint64_t *)ð_hdr[6]->d_addr = dest_eth_addr[dst_port[6]];
1241 *(uint64_t *)ð_hdr[7]->d_addr = dest_eth_addr[dst_port[7]];
1244 ether_addr_copy(&ports_eth_addr[dst_port[0]], ð_hdr[0]->s_addr);
1245 ether_addr_copy(&ports_eth_addr[dst_port[1]], ð_hdr[1]->s_addr);
1246 ether_addr_copy(&ports_eth_addr[dst_port[2]], ð_hdr[2]->s_addr);
1247 ether_addr_copy(&ports_eth_addr[dst_port[3]], ð_hdr[3]->s_addr);
1248 ether_addr_copy(&ports_eth_addr[dst_port[4]], ð_hdr[4]->s_addr);
1249 ether_addr_copy(&ports_eth_addr[dst_port[5]], ð_hdr[5]->s_addr);
1250 ether_addr_copy(&ports_eth_addr[dst_port[6]], ð_hdr[6]->s_addr);
1251 ether_addr_copy(&ports_eth_addr[dst_port[7]], ð_hdr[7]->s_addr);
1253 send_single_packet(m[0], dst_port[0]);
1254 send_single_packet(m[1], dst_port[1]);
1255 send_single_packet(m[2], dst_port[2]);
1256 send_single_packet(m[3], dst_port[3]);
1257 send_single_packet(m[4], dst_port[4]);
1258 send_single_packet(m[5], dst_port[5]);
1259 send_single_packet(m[6], dst_port[6]);
1260 send_single_packet(m[7], dst_port[7]);
1263 #endif /* APP_LOOKUP_METHOD */
1265 static __rte_always_inline void
1266 l3fwd_simple_forward(struct rte_mbuf *m, uint16_t portid)
1268 struct ether_hdr *eth_hdr;
1269 struct ipv4_hdr *ipv4_hdr;
1272 eth_hdr = rte_pktmbuf_mtod(m, struct ether_hdr *);
1274 if (RTE_ETH_IS_IPV4_HDR(m->packet_type)) {
1275 /* Handle IPv4 headers.*/
1276 ipv4_hdr = rte_pktmbuf_mtod_offset(m, struct ipv4_hdr *,
1277 sizeof(struct ether_hdr));
1279 #ifdef DO_RFC_1812_CHECKS
1280 /* Check to make sure the packet is valid (RFC1812) */
1281 if (is_valid_ipv4_pkt(ipv4_hdr, m->pkt_len) < 0) {
1282 rte_pktmbuf_free(m);
1287 dst_port = get_ipv4_dst_port(ipv4_hdr, portid,
1288 RTE_PER_LCORE(lcore_conf)->ipv4_lookup_struct);
1289 if (dst_port >= RTE_MAX_ETHPORTS ||
1290 (enabled_port_mask & 1 << dst_port) == 0)
1293 #ifdef DO_RFC_1812_CHECKS
1294 /* Update time to live and header checksum */
1295 --(ipv4_hdr->time_to_live);
1296 ++(ipv4_hdr->hdr_checksum);
1299 *(uint64_t *)ð_hdr->d_addr = dest_eth_addr[dst_port];
1302 ether_addr_copy(&ports_eth_addr[dst_port], ð_hdr->s_addr);
1304 send_single_packet(m, dst_port);
1305 } else if (RTE_ETH_IS_IPV6_HDR(m->packet_type)) {
1306 /* Handle IPv6 headers.*/
1307 struct ipv6_hdr *ipv6_hdr;
1309 ipv6_hdr = rte_pktmbuf_mtod_offset(m, struct ipv6_hdr *,
1310 sizeof(struct ether_hdr));
1312 dst_port = get_ipv6_dst_port(ipv6_hdr, portid,
1313 RTE_PER_LCORE(lcore_conf)->ipv6_lookup_struct);
1315 if (dst_port >= RTE_MAX_ETHPORTS ||
1316 (enabled_port_mask & 1 << dst_port) == 0)
1320 *(uint64_t *)ð_hdr->d_addr = dest_eth_addr[dst_port];
1323 ether_addr_copy(&ports_eth_addr[dst_port], ð_hdr->s_addr);
1325 send_single_packet(m, dst_port);
1327 /* Free the mbuf that contains non-IPV4/IPV6 packet */
1328 rte_pktmbuf_free(m);
1331 #if ((APP_LOOKUP_METHOD == APP_LOOKUP_LPM) && \
1332 (ENABLE_MULTI_BUFFER_OPTIMIZE == 1))
1333 #ifdef DO_RFC_1812_CHECKS
1335 #define IPV4_MIN_VER_IHL 0x45
1336 #define IPV4_MAX_VER_IHL 0x4f
1337 #define IPV4_MAX_VER_IHL_DIFF (IPV4_MAX_VER_IHL - IPV4_MIN_VER_IHL)
1339 /* Minimum value of IPV4 total length (20B) in network byte order. */
1340 #define IPV4_MIN_LEN_BE (sizeof(struct ipv4_hdr) << 8)
1343 * From http://www.rfc-editor.org/rfc/rfc1812.txt section 5.2.2:
1344 * - The IP version number must be 4.
1345 * - The IP header length field must be large enough to hold the
1346 * minimum length legal IP datagram (20 bytes = 5 words).
1347 * - The IP total length field must be large enough to hold the IP
1348 * datagram header, whose length is specified in the IP header length
1350 * If we encounter invalid IPV4 packet, then set destination port for it
1351 * to BAD_PORT value.
1353 static __rte_always_inline void
1354 rfc1812_process(struct ipv4_hdr *ipv4_hdr, uint16_t *dp, uint32_t ptype)
1358 if (RTE_ETH_IS_IPV4_HDR(ptype)) {
1359 ihl = ipv4_hdr->version_ihl - IPV4_MIN_VER_IHL;
1361 ipv4_hdr->time_to_live--;
1362 ipv4_hdr->hdr_checksum++;
1364 if (ihl > IPV4_MAX_VER_IHL_DIFF ||
1365 ((uint8_t)ipv4_hdr->total_length == 0 &&
1366 ipv4_hdr->total_length < IPV4_MIN_LEN_BE)) {
1373 #define rfc1812_process(mb, dp, ptype) do { } while (0)
1374 #endif /* DO_RFC_1812_CHECKS */
1375 #endif /* APP_LOOKUP_LPM && ENABLE_MULTI_BUFFER_OPTIMIZE */
1378 #if ((APP_LOOKUP_METHOD == APP_LOOKUP_LPM) && \
1379 (ENABLE_MULTI_BUFFER_OPTIMIZE == 1))
1381 static __rte_always_inline uint16_t
1382 get_dst_port(struct rte_mbuf *pkt, uint32_t dst_ipv4, uint16_t portid)
1385 struct ipv6_hdr *ipv6_hdr;
1386 struct ether_hdr *eth_hdr;
1388 if (RTE_ETH_IS_IPV4_HDR(pkt->packet_type)) {
1389 return (uint16_t) ((rte_lpm_lookup(
1390 RTE_PER_LCORE(lcore_conf)->ipv4_lookup_struct, dst_ipv4,
1391 &next_hop) == 0) ? next_hop : portid);
1393 } else if (RTE_ETH_IS_IPV6_HDR(pkt->packet_type)) {
1395 eth_hdr = rte_pktmbuf_mtod(pkt, struct ether_hdr *);
1396 ipv6_hdr = (struct ipv6_hdr *)(eth_hdr + 1);
1398 return (uint16_t) ((rte_lpm6_lookup(
1399 RTE_PER_LCORE(lcore_conf)->ipv6_lookup_struct,
1400 ipv6_hdr->dst_addr, &next_hop) == 0) ?
1409 process_packet(struct rte_mbuf *pkt, uint16_t *dst_port, uint16_t portid)
1411 struct ether_hdr *eth_hdr;
1412 struct ipv4_hdr *ipv4_hdr;
1417 eth_hdr = rte_pktmbuf_mtod(pkt, struct ether_hdr *);
1418 ipv4_hdr = (struct ipv4_hdr *)(eth_hdr + 1);
1420 dst_ipv4 = ipv4_hdr->dst_addr;
1421 dst_ipv4 = rte_be_to_cpu_32(dst_ipv4);
1422 dp = get_dst_port(pkt, dst_ipv4, portid);
1424 te = _mm_load_si128((__m128i *)eth_hdr);
1428 rfc1812_process(ipv4_hdr, dst_port, pkt->packet_type);
1430 te = _mm_blend_epi16(te, ve, MASK_ETH);
1431 _mm_store_si128((__m128i *)eth_hdr, te);
1435 * Read packet_type and destination IPV4 addresses from 4 mbufs.
1438 processx4_step1(struct rte_mbuf *pkt[FWDSTEP],
1440 uint32_t *ipv4_flag)
1442 struct ipv4_hdr *ipv4_hdr;
1443 struct ether_hdr *eth_hdr;
1444 uint32_t x0, x1, x2, x3;
1446 eth_hdr = rte_pktmbuf_mtod(pkt[0], struct ether_hdr *);
1447 ipv4_hdr = (struct ipv4_hdr *)(eth_hdr + 1);
1448 x0 = ipv4_hdr->dst_addr;
1449 ipv4_flag[0] = pkt[0]->packet_type & RTE_PTYPE_L3_IPV4;
1451 eth_hdr = rte_pktmbuf_mtod(pkt[1], struct ether_hdr *);
1452 ipv4_hdr = (struct ipv4_hdr *)(eth_hdr + 1);
1453 x1 = ipv4_hdr->dst_addr;
1454 ipv4_flag[0] &= pkt[1]->packet_type;
1456 eth_hdr = rte_pktmbuf_mtod(pkt[2], struct ether_hdr *);
1457 ipv4_hdr = (struct ipv4_hdr *)(eth_hdr + 1);
1458 x2 = ipv4_hdr->dst_addr;
1459 ipv4_flag[0] &= pkt[2]->packet_type;
1461 eth_hdr = rte_pktmbuf_mtod(pkt[3], struct ether_hdr *);
1462 ipv4_hdr = (struct ipv4_hdr *)(eth_hdr + 1);
1463 x3 = ipv4_hdr->dst_addr;
1464 ipv4_flag[0] &= pkt[3]->packet_type;
1466 dip[0] = _mm_set_epi32(x3, x2, x1, x0);
1470 * Lookup into LPM for destination port.
1471 * If lookup fails, use incoming port (portid) as destination port.
1474 processx4_step2(__m128i dip,
1477 struct rte_mbuf *pkt[FWDSTEP],
1478 uint16_t dprt[FWDSTEP])
1481 const __m128i bswap_mask = _mm_set_epi8(12, 13, 14, 15, 8, 9, 10, 11,
1482 4, 5, 6, 7, 0, 1, 2, 3);
1484 /* Byte swap 4 IPV4 addresses. */
1485 dip = _mm_shuffle_epi8(dip, bswap_mask);
1487 /* if all 4 packets are IPV4. */
1488 if (likely(ipv4_flag)) {
1489 rte_lpm_lookupx4(RTE_PER_LCORE(lcore_conf)->ipv4_lookup_struct, dip,
1492 /* get rid of unused upper 16 bit for each dport. */
1493 dst.x = _mm_packs_epi32(dst.x, dst.x);
1494 *(uint64_t *)dprt = dst.u64[0];
1497 dprt[0] = get_dst_port(pkt[0], dst.u32[0], portid);
1498 dprt[1] = get_dst_port(pkt[1], dst.u32[1], portid);
1499 dprt[2] = get_dst_port(pkt[2], dst.u32[2], portid);
1500 dprt[3] = get_dst_port(pkt[3], dst.u32[3], portid);
1505 * Update source and destination MAC addresses in the ethernet header.
1506 * Perform RFC1812 checks and updates for IPV4 packets.
1509 processx4_step3(struct rte_mbuf *pkt[FWDSTEP], uint16_t dst_port[FWDSTEP])
1511 __m128i te[FWDSTEP];
1512 __m128i ve[FWDSTEP];
1513 __m128i *p[FWDSTEP];
1515 p[0] = rte_pktmbuf_mtod(pkt[0], __m128i *);
1516 p[1] = rte_pktmbuf_mtod(pkt[1], __m128i *);
1517 p[2] = rte_pktmbuf_mtod(pkt[2], __m128i *);
1518 p[3] = rte_pktmbuf_mtod(pkt[3], __m128i *);
1520 ve[0] = val_eth[dst_port[0]];
1521 te[0] = _mm_load_si128(p[0]);
1523 ve[1] = val_eth[dst_port[1]];
1524 te[1] = _mm_load_si128(p[1]);
1526 ve[2] = val_eth[dst_port[2]];
1527 te[2] = _mm_load_si128(p[2]);
1529 ve[3] = val_eth[dst_port[3]];
1530 te[3] = _mm_load_si128(p[3]);
1532 /* Update first 12 bytes, keep rest bytes intact. */
1533 te[0] = _mm_blend_epi16(te[0], ve[0], MASK_ETH);
1534 te[1] = _mm_blend_epi16(te[1], ve[1], MASK_ETH);
1535 te[2] = _mm_blend_epi16(te[2], ve[2], MASK_ETH);
1536 te[3] = _mm_blend_epi16(te[3], ve[3], MASK_ETH);
1538 _mm_store_si128(p[0], te[0]);
1539 _mm_store_si128(p[1], te[1]);
1540 _mm_store_si128(p[2], te[2]);
1541 _mm_store_si128(p[3], te[3]);
1543 rfc1812_process((struct ipv4_hdr *)((struct ether_hdr *)p[0] + 1),
1544 &dst_port[0], pkt[0]->packet_type);
1545 rfc1812_process((struct ipv4_hdr *)((struct ether_hdr *)p[1] + 1),
1546 &dst_port[1], pkt[1]->packet_type);
1547 rfc1812_process((struct ipv4_hdr *)((struct ether_hdr *)p[2] + 1),
1548 &dst_port[2], pkt[2]->packet_type);
1549 rfc1812_process((struct ipv4_hdr *)((struct ether_hdr *)p[3] + 1),
1550 &dst_port[3], pkt[3]->packet_type);
1554 * We group consecutive packets with the same destionation port into one burst.
1555 * To avoid extra latency this is done together with some other packet
1556 * processing, but after we made a final decision about packet's destination.
1557 * To do this we maintain:
1558 * pnum - array of number of consecutive packets with the same dest port for
1559 * each packet in the input burst.
1560 * lp - pointer to the last updated element in the pnum.
1561 * dlp - dest port value lp corresponds to.
1564 #define GRPSZ (1 << FWDSTEP)
1565 #define GRPMSK (GRPSZ - 1)
1567 #define GROUP_PORT_STEP(dlp, dcp, lp, pn, idx) do { \
1568 if (likely((dlp) == (dcp)[(idx)])) { \
1571 (dlp) = (dcp)[idx]; \
1572 (lp) = (pn) + (idx); \
1578 * Group consecutive packets with the same destination port in bursts of 4.
1579 * Suppose we have array of destionation ports:
1580 * dst_port[] = {a, b, c, d,, e, ... }
1581 * dp1 should contain: <a, b, c, d>, dp2: <b, c, d, e>.
1582 * We doing 4 comparisons at once and the result is 4 bit mask.
1583 * This mask is used as an index into prebuild array of pnum values.
1585 static inline uint16_t *
1586 port_groupx4(uint16_t pn[FWDSTEP + 1], uint16_t *lp, __m128i dp1, __m128i dp2)
1588 static const struct {
1589 uint64_t pnum; /* prebuild 4 values for pnum[]. */
1590 int32_t idx; /* index for new last updated elemnet. */
1591 uint16_t lpv; /* add value to the last updated element. */
1594 /* 0: a != b, b != c, c != d, d != e */
1595 .pnum = UINT64_C(0x0001000100010001),
1600 /* 1: a == b, b != c, c != d, d != e */
1601 .pnum = UINT64_C(0x0001000100010002),
1606 /* 2: a != b, b == c, c != d, d != e */
1607 .pnum = UINT64_C(0x0001000100020001),
1612 /* 3: a == b, b == c, c != d, d != e */
1613 .pnum = UINT64_C(0x0001000100020003),
1618 /* 4: a != b, b != c, c == d, d != e */
1619 .pnum = UINT64_C(0x0001000200010001),
1624 /* 5: a == b, b != c, c == d, d != e */
1625 .pnum = UINT64_C(0x0001000200010002),
1630 /* 6: a != b, b == c, c == d, d != e */
1631 .pnum = UINT64_C(0x0001000200030001),
1636 /* 7: a == b, b == c, c == d, d != e */
1637 .pnum = UINT64_C(0x0001000200030004),
1642 /* 8: a != b, b != c, c != d, d == e */
1643 .pnum = UINT64_C(0x0002000100010001),
1648 /* 9: a == b, b != c, c != d, d == e */
1649 .pnum = UINT64_C(0x0002000100010002),
1654 /* 0xa: a != b, b == c, c != d, d == e */
1655 .pnum = UINT64_C(0x0002000100020001),
1660 /* 0xb: a == b, b == c, c != d, d == e */
1661 .pnum = UINT64_C(0x0002000100020003),
1666 /* 0xc: a != b, b != c, c == d, d == e */
1667 .pnum = UINT64_C(0x0002000300010001),
1672 /* 0xd: a == b, b != c, c == d, d == e */
1673 .pnum = UINT64_C(0x0002000300010002),
1678 /* 0xe: a != b, b == c, c == d, d == e */
1679 .pnum = UINT64_C(0x0002000300040001),
1684 /* 0xf: a == b, b == c, c == d, d == e */
1685 .pnum = UINT64_C(0x0002000300040005),
1692 uint16_t u16[FWDSTEP + 1];
1694 } *pnum = (void *)pn;
1698 dp1 = _mm_cmpeq_epi16(dp1, dp2);
1699 dp1 = _mm_unpacklo_epi16(dp1, dp1);
1700 v = _mm_movemask_ps((__m128)dp1);
1702 /* update last port counter. */
1703 lp[0] += gptbl[v].lpv;
1705 /* if dest port value has changed. */
1707 pnum->u64 = gptbl[v].pnum;
1708 pnum->u16[FWDSTEP] = 1;
1709 lp = pnum->u16 + gptbl[v].idx;
1715 #endif /* APP_LOOKUP_METHOD */
1718 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)
2145 int len[RTE_MAX_LCORE] = { 0 };
2146 int old_len, new_len;
2147 struct rte_mbuf *pkts_burst[MAX_PKT_BURST];
2148 struct thread_rx_conf *rx_conf;
2150 rx_conf = (struct thread_rx_conf *)dummy;
2151 lthread_set_data((void *)rx_conf);
2154 * Move this lthread to lcore
2156 lthread_set_affinity(rx_conf->conf.lcore_id);
2158 if (rx_conf->n_rx_queue == 0) {
2159 RTE_LOG(INFO, L3FWD, "lcore %u has nothing to do\n", rte_lcore_id());
2163 RTE_LOG(INFO, L3FWD, "Entering main Rx loop on lcore %u\n", rte_lcore_id());
2165 for (i = 0; i < rx_conf->n_rx_queue; i++) {
2167 portid = rx_conf->rx_queue_list[i].port_id;
2168 queueid = rx_conf->rx_queue_list[i].queue_id;
2169 RTE_LOG(INFO, L3FWD,
2170 " -- lcoreid=%u portid=%u rxqueueid=%hhu\n",
2171 rte_lcore_id(), portid, queueid);
2175 * Init all condition variables (one per rx thread)
2177 for (i = 0; i < rx_conf->n_rx_queue; i++)
2178 lthread_cond_init(NULL, &rx_conf->ready[i], NULL);
2182 rx_conf->conf.cpu_id = sched_getcpu();
2183 rte_atomic16_inc(&rx_counter);
2187 * Read packet from RX queues
2189 for (i = 0; i < rx_conf->n_rx_queue; ++i) {
2190 portid = rx_conf->rx_queue_list[i].port_id;
2191 queueid = rx_conf->rx_queue_list[i].queue_id;
2193 SET_CPU_BUSY(rx_conf, CPU_POLL);
2194 nb_rx = rte_eth_rx_burst(portid, queueid, pkts_burst,
2196 SET_CPU_IDLE(rx_conf, CPU_POLL);
2199 worker_id = (worker_id + 1) % rx_conf->n_ring;
2200 old_len = len[worker_id];
2202 SET_CPU_BUSY(rx_conf, CPU_PROCESS);
2203 ret = rte_ring_sp_enqueue_burst(
2204 rx_conf->ring[worker_id],
2205 (void **) pkts_burst,
2208 new_len = old_len + ret;
2210 if (new_len >= BURST_SIZE) {
2211 lthread_cond_signal(rx_conf->ready[worker_id]);
2215 len[worker_id] = new_len;
2217 if (unlikely(ret < nb_rx)) {
2220 for (k = ret; k < nb_rx; k++) {
2221 struct rte_mbuf *m = pkts_burst[k];
2223 rte_pktmbuf_free(m);
2226 SET_CPU_IDLE(rx_conf, CPU_PROCESS);
2235 * Start scheduler with initial lthread on lcore
2237 * This lthread loop spawns all rx and tx lthreads on master lcore
2241 lthread_spawner(__rte_unused void *arg) {
2242 struct lthread *lt[MAX_THREAD];
2246 printf("Entering lthread_spawner\n");
2249 * Create producers (rx threads) on default lcore
2251 for (i = 0; i < n_rx_thread; i++) {
2252 rx_thread[i].conf.thread_id = i;
2253 lthread_create(<[n_thread], -1, lthread_rx,
2254 (void *)&rx_thread[i]);
2259 * Wait for all producers. Until some producers can be started on the same
2260 * scheduler as this lthread, yielding is required to let them to run and
2261 * prevent deadlock here.
2263 while (rte_atomic16_read(&rx_counter) < n_rx_thread)
2264 lthread_sleep(100000);
2267 * Create consumers (tx threads) on default lcore_id
2269 for (i = 0; i < n_tx_thread; i++) {
2270 tx_thread[i].conf.thread_id = i;
2271 lthread_create(<[n_thread], -1, lthread_tx,
2272 (void *)&tx_thread[i]);
2277 * Wait for all threads finished
2279 for (i = 0; i < n_thread; i++)
2280 lthread_join(lt[i], NULL);
2285 * Start master scheduler with initial lthread spawning rx and tx lthreads
2286 * (main_lthread_master).
2289 lthread_master_spawner(__rte_unused void *arg) {
2291 int lcore_id = rte_lcore_id();
2293 RTE_PER_LCORE(lcore_conf) = &lcore_conf[lcore_id];
2294 lthread_create(<, -1, lthread_spawner, NULL);
2301 * Start scheduler on lcore.
2304 sched_spawner(__rte_unused void *arg) {
2306 int lcore_id = rte_lcore_id();
2309 if (lcore_id == cpu_load_lcore_id) {
2310 cpu_load_collector(arg);
2313 #endif /* APP_CPU_LOAD */
2315 RTE_PER_LCORE(lcore_conf) = &lcore_conf[lcore_id];
2316 lthread_create(<, -1, lthread_null, NULL);
2322 /* main processing loop */
2324 pthread_tx(void *dummy)
2326 struct rte_mbuf *pkts_burst[MAX_PKT_BURST];
2327 uint64_t prev_tsc, diff_tsc, cur_tsc;
2330 struct thread_tx_conf *tx_conf;
2332 const uint64_t drain_tsc = (rte_get_tsc_hz() + US_PER_S - 1) /
2333 US_PER_S * BURST_TX_DRAIN_US;
2337 tx_conf = (struct thread_tx_conf *)dummy;
2339 RTE_LOG(INFO, L3FWD, "Entering main Tx loop on lcore %u\n", rte_lcore_id());
2341 tx_conf->conf.cpu_id = sched_getcpu();
2342 rte_atomic16_inc(&tx_counter);
2345 cur_tsc = rte_rdtsc();
2348 * TX burst queue drain
2350 diff_tsc = cur_tsc - prev_tsc;
2351 if (unlikely(diff_tsc > drain_tsc)) {
2354 * This could be optimized (use queueid instead of
2355 * portid), but it is not called so often
2357 SET_CPU_BUSY(tx_conf, CPU_PROCESS);
2358 for (portid = 0; portid < RTE_MAX_ETHPORTS; portid++) {
2359 if (tx_conf->tx_mbufs[portid].len == 0)
2361 send_burst(tx_conf, tx_conf->tx_mbufs[portid].len, portid);
2362 tx_conf->tx_mbufs[portid].len = 0;
2364 SET_CPU_IDLE(tx_conf, CPU_PROCESS);
2370 * Read packet from ring
2372 SET_CPU_BUSY(tx_conf, CPU_POLL);
2373 nb_rx = rte_ring_sc_dequeue_burst(tx_conf->ring,
2374 (void **)pkts_burst, MAX_PKT_BURST, NULL);
2375 SET_CPU_IDLE(tx_conf, CPU_POLL);
2377 if (unlikely(nb_rx == 0)) {
2382 SET_CPU_BUSY(tx_conf, CPU_PROCESS);
2383 portid = pkts_burst[0]->port;
2384 process_burst(pkts_burst, nb_rx, portid);
2385 SET_CPU_IDLE(tx_conf, CPU_PROCESS);
2391 pthread_rx(void *dummy)
2400 struct rte_mbuf *pkts_burst[MAX_PKT_BURST];
2402 struct thread_rx_conf *rx_conf;
2404 lcore_id = rte_lcore_id();
2405 rx_conf = (struct thread_rx_conf *)dummy;
2407 if (rx_conf->n_rx_queue == 0) {
2408 RTE_LOG(INFO, L3FWD, "lcore %u has nothing to do\n", lcore_id);
2412 RTE_LOG(INFO, L3FWD, "entering main rx loop on lcore %u\n", lcore_id);
2414 for (i = 0; i < rx_conf->n_rx_queue; i++) {
2416 portid = rx_conf->rx_queue_list[i].port_id;
2417 queueid = rx_conf->rx_queue_list[i].queue_id;
2418 RTE_LOG(INFO, L3FWD,
2419 " -- lcoreid=%u portid=%u rxqueueid=%hhu\n",
2420 lcore_id, portid, queueid);
2424 rx_conf->conf.cpu_id = sched_getcpu();
2425 rte_atomic16_inc(&rx_counter);
2429 * Read packet from RX queues
2431 for (i = 0; i < rx_conf->n_rx_queue; ++i) {
2432 portid = rx_conf->rx_queue_list[i].port_id;
2433 queueid = rx_conf->rx_queue_list[i].queue_id;
2435 SET_CPU_BUSY(rx_conf, CPU_POLL);
2436 nb_rx = rte_eth_rx_burst(portid, queueid, pkts_burst,
2438 SET_CPU_IDLE(rx_conf, CPU_POLL);
2445 SET_CPU_BUSY(rx_conf, CPU_PROCESS);
2446 worker_id = (worker_id + 1) % rx_conf->n_ring;
2447 n = rte_ring_sp_enqueue_burst(rx_conf->ring[worker_id],
2448 (void **)pkts_burst, nb_rx, NULL);
2450 if (unlikely(n != nb_rx)) {
2453 for (k = n; k < nb_rx; k++) {
2454 struct rte_mbuf *m = pkts_burst[k];
2456 rte_pktmbuf_free(m);
2460 SET_CPU_IDLE(rx_conf, CPU_PROCESS);
2470 pthread_run(__rte_unused void *arg) {
2471 int lcore_id = rte_lcore_id();
2474 for (i = 0; i < n_rx_thread; i++)
2475 if (rx_thread[i].conf.lcore_id == lcore_id) {
2476 printf("Start rx thread on %d...\n", lcore_id);
2477 RTE_PER_LCORE(lcore_conf) = &lcore_conf[lcore_id];
2478 RTE_PER_LCORE(lcore_conf)->data = (void *)&rx_thread[i];
2479 pthread_rx((void *)&rx_thread[i]);
2483 for (i = 0; i < n_tx_thread; i++)
2484 if (tx_thread[i].conf.lcore_id == lcore_id) {
2485 printf("Start tx thread on %d...\n", lcore_id);
2486 RTE_PER_LCORE(lcore_conf) = &lcore_conf[lcore_id];
2487 RTE_PER_LCORE(lcore_conf)->data = (void *)&tx_thread[i];
2488 pthread_tx((void *)&tx_thread[i]);
2493 if (lcore_id == cpu_load_lcore_id)
2494 cpu_load_collector(arg);
2495 #endif /* APP_CPU_LOAD */
2501 check_lcore_params(void)
2503 uint8_t queue, lcore;
2507 for (i = 0; i < nb_rx_thread_params; ++i) {
2508 queue = rx_thread_params[i].queue_id;
2509 if (queue >= MAX_RX_QUEUE_PER_PORT) {
2510 printf("invalid queue number: %hhu\n", queue);
2513 lcore = rx_thread_params[i].lcore_id;
2514 if (!rte_lcore_is_enabled(lcore)) {
2515 printf("error: lcore %hhu is not enabled in lcore mask\n", lcore);
2518 socketid = rte_lcore_to_socket_id(lcore);
2519 if ((socketid != 0) && (numa_on == 0))
2520 printf("warning: lcore %hhu is on socket %d with numa off\n",
2527 check_port_config(const unsigned nb_ports)
2532 for (i = 0; i < nb_rx_thread_params; ++i) {
2533 portid = rx_thread_params[i].port_id;
2534 if ((enabled_port_mask & (1 << portid)) == 0) {
2535 printf("port %u is not enabled in port mask\n", portid);
2538 if (portid >= nb_ports) {
2539 printf("port %u is not present on the board\n", portid);
2547 get_port_n_rx_queues(const uint16_t port)
2552 for (i = 0; i < nb_rx_thread_params; ++i)
2553 if (rx_thread_params[i].port_id == port &&
2554 rx_thread_params[i].queue_id > queue)
2555 queue = rx_thread_params[i].queue_id;
2557 return (uint8_t)(++queue);
2564 struct thread_rx_conf *rx_conf;
2565 struct thread_tx_conf *tx_conf;
2566 unsigned rx_thread_id, tx_thread_id;
2568 struct rte_ring *ring = NULL;
2570 for (tx_thread_id = 0; tx_thread_id < n_tx_thread; tx_thread_id++) {
2572 tx_conf = &tx_thread[tx_thread_id];
2574 printf("Connecting tx-thread %d with rx-thread %d\n", tx_thread_id,
2575 tx_conf->conf.thread_id);
2577 rx_thread_id = tx_conf->conf.thread_id;
2578 if (rx_thread_id > n_tx_thread) {
2579 printf("connection from tx-thread %u to rx-thread %u fails "
2580 "(rx-thread not defined)\n", tx_thread_id, rx_thread_id);
2584 rx_conf = &rx_thread[rx_thread_id];
2585 socket_io = rte_lcore_to_socket_id(rx_conf->conf.lcore_id);
2587 snprintf(name, sizeof(name), "app_ring_s%u_rx%u_tx%u",
2588 socket_io, rx_thread_id, tx_thread_id);
2590 ring = rte_ring_create(name, 1024 * 4, socket_io,
2591 RING_F_SP_ENQ | RING_F_SC_DEQ);
2594 rte_panic("Cannot create ring to connect rx-thread %u "
2595 "with tx-thread %u\n", rx_thread_id, tx_thread_id);
2598 rx_conf->ring[rx_conf->n_ring] = ring;
2600 tx_conf->ring = ring;
2601 tx_conf->ready = &rx_conf->ready[rx_conf->n_ring];
2609 init_rx_queues(void)
2611 uint16_t i, nb_rx_queue;
2616 for (i = 0; i < nb_rx_thread_params; ++i) {
2617 thread = rx_thread_params[i].thread_id;
2618 nb_rx_queue = rx_thread[thread].n_rx_queue;
2620 if (nb_rx_queue >= MAX_RX_QUEUE_PER_LCORE) {
2621 printf("error: too many queues (%u) for thread: %u\n",
2622 (unsigned)nb_rx_queue + 1, (unsigned)thread);
2626 rx_thread[thread].conf.thread_id = thread;
2627 rx_thread[thread].conf.lcore_id = rx_thread_params[i].lcore_id;
2628 rx_thread[thread].rx_queue_list[nb_rx_queue].port_id =
2629 rx_thread_params[i].port_id;
2630 rx_thread[thread].rx_queue_list[nb_rx_queue].queue_id =
2631 rx_thread_params[i].queue_id;
2632 rx_thread[thread].n_rx_queue++;
2634 if (thread >= n_rx_thread)
2635 n_rx_thread = thread + 1;
2642 init_tx_threads(void)
2647 for (i = 0; i < nb_tx_thread_params; ++i) {
2648 tx_thread[n_tx_thread].conf.thread_id = tx_thread_params[i].thread_id;
2649 tx_thread[n_tx_thread].conf.lcore_id = tx_thread_params[i].lcore_id;
2657 print_usage(const char *prgname)
2659 printf("%s [EAL options] -- -p PORTMASK -P"
2660 " [--rx (port,queue,lcore,thread)[,(port,queue,lcore,thread]]"
2661 " [--tx (lcore,thread)[,(lcore,thread]]"
2662 " [--enable-jumbo [--max-pkt-len PKTLEN]]\n"
2663 " [--parse-ptype]\n\n"
2664 " -p PORTMASK: hexadecimal bitmask of ports to configure\n"
2665 " -P : enable promiscuous mode\n"
2666 " --rx (port,queue,lcore,thread): rx queues configuration\n"
2667 " --tx (lcore,thread): tx threads configuration\n"
2668 " --stat-lcore LCORE: use lcore for stat collector\n"
2669 " --eth-dest=X,MM:MM:MM:MM:MM:MM: optional, ethernet destination for port X\n"
2670 " --no-numa: optional, disable numa awareness\n"
2671 " --ipv6: optional, specify it if running ipv6 packets\n"
2672 " --enable-jumbo: enable jumbo frame"
2673 " which max packet len is PKTLEN in decimal (64-9600)\n"
2674 " --hash-entry-num: specify the hash entry number in hexadecimal to be setup\n"
2675 " --no-lthreads: turn off lthread model\n"
2676 " --parse-ptype: set to use software to analyze packet type\n\n",
2680 static int parse_max_pkt_len(const char *pktlen)
2685 /* parse decimal string */
2686 len = strtoul(pktlen, &end, 10);
2687 if ((pktlen[0] == '\0') || (end == NULL) || (*end != '\0'))
2697 parse_portmask(const char *portmask)
2702 /* parse hexadecimal string */
2703 pm = strtoul(portmask, &end, 16);
2704 if ((portmask[0] == '\0') || (end == NULL) || (*end != '\0'))
2713 #if (APP_LOOKUP_METHOD == APP_LOOKUP_EXACT_MATCH)
2715 parse_hash_entry_number(const char *hash_entry_num)
2718 unsigned long hash_en;
2720 /* parse hexadecimal string */
2721 hash_en = strtoul(hash_entry_num, &end, 16);
2722 if ((hash_entry_num[0] == '\0') || (end == NULL) || (*end != '\0'))
2733 parse_rx_config(const char *q_arg)
2736 const char *p, *p0 = q_arg;
2745 unsigned long int_fld[_NUM_FLD];
2746 char *str_fld[_NUM_FLD];
2750 nb_rx_thread_params = 0;
2752 while ((p = strchr(p0, '(')) != NULL) {
2754 p0 = strchr(p, ')');
2759 if (size >= sizeof(s))
2762 snprintf(s, sizeof(s), "%.*s", size, p);
2763 if (rte_strsplit(s, sizeof(s), str_fld, _NUM_FLD, ',') != _NUM_FLD)
2765 for (i = 0; i < _NUM_FLD; i++) {
2767 int_fld[i] = strtoul(str_fld[i], &end, 0);
2768 if (errno != 0 || end == str_fld[i] || int_fld[i] > 255)
2771 if (nb_rx_thread_params >= MAX_LCORE_PARAMS) {
2772 printf("exceeded max number of rx params: %hu\n",
2773 nb_rx_thread_params);
2776 rx_thread_params_array[nb_rx_thread_params].port_id =
2778 rx_thread_params_array[nb_rx_thread_params].queue_id =
2779 (uint8_t)int_fld[FLD_QUEUE];
2780 rx_thread_params_array[nb_rx_thread_params].lcore_id =
2781 (uint8_t)int_fld[FLD_LCORE];
2782 rx_thread_params_array[nb_rx_thread_params].thread_id =
2783 (uint8_t)int_fld[FLD_THREAD];
2784 ++nb_rx_thread_params;
2786 rx_thread_params = rx_thread_params_array;
2791 parse_tx_config(const char *q_arg)
2794 const char *p, *p0 = q_arg;
2801 unsigned long int_fld[_NUM_FLD];
2802 char *str_fld[_NUM_FLD];
2806 nb_tx_thread_params = 0;
2808 while ((p = strchr(p0, '(')) != NULL) {
2810 p0 = strchr(p, ')');
2815 if (size >= sizeof(s))
2818 snprintf(s, sizeof(s), "%.*s", size, p);
2819 if (rte_strsplit(s, sizeof(s), str_fld, _NUM_FLD, ',') != _NUM_FLD)
2821 for (i = 0; i < _NUM_FLD; i++) {
2823 int_fld[i] = strtoul(str_fld[i], &end, 0);
2824 if (errno != 0 || end == str_fld[i] || int_fld[i] > 255)
2827 if (nb_tx_thread_params >= MAX_LCORE_PARAMS) {
2828 printf("exceeded max number of tx params: %hu\n",
2829 nb_tx_thread_params);
2832 tx_thread_params_array[nb_tx_thread_params].lcore_id =
2833 (uint8_t)int_fld[FLD_LCORE];
2834 tx_thread_params_array[nb_tx_thread_params].thread_id =
2835 (uint8_t)int_fld[FLD_THREAD];
2836 ++nb_tx_thread_params;
2838 tx_thread_params = tx_thread_params_array;
2843 #if (APP_CPU_LOAD > 0)
2845 parse_stat_lcore(const char *stat_lcore)
2848 unsigned long lcore_id;
2850 lcore_id = strtoul(stat_lcore, &end, 10);
2851 if ((stat_lcore[0] == '\0') || (end == NULL) || (*end != '\0'))
2859 parse_eth_dest(const char *optarg)
2863 uint8_t c, *dest, peer_addr[6];
2866 portid = strtoul(optarg, &port_end, 10);
2867 if (errno != 0 || port_end == optarg || *port_end++ != ',')
2868 rte_exit(EXIT_FAILURE,
2869 "Invalid eth-dest: %s", optarg);
2870 if (portid >= RTE_MAX_ETHPORTS)
2871 rte_exit(EXIT_FAILURE,
2872 "eth-dest: port %d >= RTE_MAX_ETHPORTS(%d)\n",
2873 portid, RTE_MAX_ETHPORTS);
2875 if (cmdline_parse_etheraddr(NULL, port_end,
2876 &peer_addr, sizeof(peer_addr)) < 0)
2877 rte_exit(EXIT_FAILURE,
2878 "Invalid ethernet address: %s\n",
2880 dest = (uint8_t *)&dest_eth_addr[portid];
2881 for (c = 0; c < 6; c++)
2882 dest[c] = peer_addr[c];
2883 *(uint64_t *)(val_eth + portid) = dest_eth_addr[portid];
2886 #define CMD_LINE_OPT_RX_CONFIG "rx"
2887 #define CMD_LINE_OPT_TX_CONFIG "tx"
2888 #define CMD_LINE_OPT_STAT_LCORE "stat-lcore"
2889 #define CMD_LINE_OPT_ETH_DEST "eth-dest"
2890 #define CMD_LINE_OPT_NO_NUMA "no-numa"
2891 #define CMD_LINE_OPT_IPV6 "ipv6"
2892 #define CMD_LINE_OPT_ENABLE_JUMBO "enable-jumbo"
2893 #define CMD_LINE_OPT_HASH_ENTRY_NUM "hash-entry-num"
2894 #define CMD_LINE_OPT_NO_LTHREADS "no-lthreads"
2895 #define CMD_LINE_OPT_PARSE_PTYPE "parse-ptype"
2897 /* Parse the argument given in the command line of the application */
2899 parse_args(int argc, char **argv)
2904 char *prgname = argv[0];
2905 static struct option lgopts[] = {
2906 {CMD_LINE_OPT_RX_CONFIG, 1, 0, 0},
2907 {CMD_LINE_OPT_TX_CONFIG, 1, 0, 0},
2908 {CMD_LINE_OPT_STAT_LCORE, 1, 0, 0},
2909 {CMD_LINE_OPT_ETH_DEST, 1, 0, 0},
2910 {CMD_LINE_OPT_NO_NUMA, 0, 0, 0},
2911 {CMD_LINE_OPT_IPV6, 0, 0, 0},
2912 {CMD_LINE_OPT_ENABLE_JUMBO, 0, 0, 0},
2913 {CMD_LINE_OPT_HASH_ENTRY_NUM, 1, 0, 0},
2914 {CMD_LINE_OPT_NO_LTHREADS, 0, 0, 0},
2915 {CMD_LINE_OPT_PARSE_PTYPE, 0, 0, 0},
2921 while ((opt = getopt_long(argc, argvopt, "p:P",
2922 lgopts, &option_index)) != EOF) {
2927 enabled_port_mask = parse_portmask(optarg);
2928 if (enabled_port_mask == 0) {
2929 printf("invalid portmask\n");
2930 print_usage(prgname);
2935 printf("Promiscuous mode selected\n");
2941 if (!strncmp(lgopts[option_index].name, CMD_LINE_OPT_RX_CONFIG,
2942 sizeof(CMD_LINE_OPT_RX_CONFIG))) {
2943 ret = parse_rx_config(optarg);
2945 printf("invalid rx-config\n");
2946 print_usage(prgname);
2951 if (!strncmp(lgopts[option_index].name, CMD_LINE_OPT_TX_CONFIG,
2952 sizeof(CMD_LINE_OPT_TX_CONFIG))) {
2953 ret = parse_tx_config(optarg);
2955 printf("invalid tx-config\n");
2956 print_usage(prgname);
2961 #if (APP_CPU_LOAD > 0)
2962 if (!strncmp(lgopts[option_index].name, CMD_LINE_OPT_STAT_LCORE,
2963 sizeof(CMD_LINE_OPT_STAT_LCORE))) {
2964 cpu_load_lcore_id = parse_stat_lcore(optarg);
2968 if (!strncmp(lgopts[option_index].name, CMD_LINE_OPT_ETH_DEST,
2969 sizeof(CMD_LINE_OPT_ETH_DEST)))
2970 parse_eth_dest(optarg);
2972 if (!strncmp(lgopts[option_index].name, CMD_LINE_OPT_NO_NUMA,
2973 sizeof(CMD_LINE_OPT_NO_NUMA))) {
2974 printf("numa is disabled\n");
2978 #if (APP_LOOKUP_METHOD == APP_LOOKUP_EXACT_MATCH)
2979 if (!strncmp(lgopts[option_index].name, CMD_LINE_OPT_IPV6,
2980 sizeof(CMD_LINE_OPT_IPV6))) {
2981 printf("ipv6 is specified\n");
2986 if (!strncmp(lgopts[option_index].name, CMD_LINE_OPT_NO_LTHREADS,
2987 sizeof(CMD_LINE_OPT_NO_LTHREADS))) {
2988 printf("l-threads model is disabled\n");
2992 if (!strncmp(lgopts[option_index].name, CMD_LINE_OPT_PARSE_PTYPE,
2993 sizeof(CMD_LINE_OPT_PARSE_PTYPE))) {
2994 printf("software packet type parsing enabled\n");
2998 if (!strncmp(lgopts[option_index].name, CMD_LINE_OPT_ENABLE_JUMBO,
2999 sizeof(CMD_LINE_OPT_ENABLE_JUMBO))) {
3000 struct option lenopts = {"max-pkt-len", required_argument, 0,
3003 printf("jumbo frame is enabled - disabling simple TX path\n");
3004 port_conf.rxmode.jumbo_frame = 1;
3006 /* if no max-pkt-len set, use the default value ETHER_MAX_LEN */
3007 if (0 == getopt_long(argc, argvopt, "", &lenopts,
3010 ret = parse_max_pkt_len(optarg);
3011 if ((ret < 64) || (ret > MAX_JUMBO_PKT_LEN)) {
3012 printf("invalid packet length\n");
3013 print_usage(prgname);
3016 port_conf.rxmode.max_rx_pkt_len = ret;
3018 printf("set jumbo frame max packet length to %u\n",
3019 (unsigned int)port_conf.rxmode.max_rx_pkt_len);
3021 #if (APP_LOOKUP_METHOD == APP_LOOKUP_EXACT_MATCH)
3022 if (!strncmp(lgopts[option_index].name, CMD_LINE_OPT_HASH_ENTRY_NUM,
3023 sizeof(CMD_LINE_OPT_HASH_ENTRY_NUM))) {
3024 ret = parse_hash_entry_number(optarg);
3025 if ((ret > 0) && (ret <= L3FWD_HASH_ENTRIES)) {
3026 hash_entry_number = ret;
3028 printf("invalid hash entry number\n");
3029 print_usage(prgname);
3037 print_usage(prgname);
3043 argv[optind-1] = prgname;
3046 optind = 1; /* reset getopt lib */
3051 print_ethaddr(const char *name, const struct ether_addr *eth_addr)
3053 char buf[ETHER_ADDR_FMT_SIZE];
3055 ether_format_addr(buf, ETHER_ADDR_FMT_SIZE, eth_addr);
3056 printf("%s%s", name, buf);
3059 #if (APP_LOOKUP_METHOD == APP_LOOKUP_EXACT_MATCH)
3061 static void convert_ipv4_5tuple(struct ipv4_5tuple *key1,
3062 union ipv4_5tuple_host *key2)
3064 key2->ip_dst = rte_cpu_to_be_32(key1->ip_dst);
3065 key2->ip_src = rte_cpu_to_be_32(key1->ip_src);
3066 key2->port_dst = rte_cpu_to_be_16(key1->port_dst);
3067 key2->port_src = rte_cpu_to_be_16(key1->port_src);
3068 key2->proto = key1->proto;
3073 static void convert_ipv6_5tuple(struct ipv6_5tuple *key1,
3074 union ipv6_5tuple_host *key2)
3078 for (i = 0; i < 16; i++) {
3079 key2->ip_dst[i] = key1->ip_dst[i];
3080 key2->ip_src[i] = key1->ip_src[i];
3082 key2->port_dst = rte_cpu_to_be_16(key1->port_dst);
3083 key2->port_src = rte_cpu_to_be_16(key1->port_src);
3084 key2->proto = key1->proto;
3090 #define BYTE_VALUE_MAX 256
3091 #define ALL_32_BITS 0xffffffff
3092 #define BIT_8_TO_15 0x0000ff00
3094 populate_ipv4_few_flow_into_table(const struct rte_hash *h)
3098 uint32_t array_len = RTE_DIM(ipv4_l3fwd_route_array);
3100 mask0 = _mm_set_epi32(ALL_32_BITS, ALL_32_BITS, ALL_32_BITS, BIT_8_TO_15);
3101 for (i = 0; i < array_len; i++) {
3102 struct ipv4_l3fwd_route entry;
3103 union ipv4_5tuple_host newkey;
3105 entry = ipv4_l3fwd_route_array[i];
3106 convert_ipv4_5tuple(&entry.key, &newkey);
3107 ret = rte_hash_add_key(h, (void *)&newkey);
3109 rte_exit(EXIT_FAILURE, "Unable to add entry %" PRIu32
3110 " to the l3fwd hash.\n", i);
3112 ipv4_l3fwd_out_if[ret] = entry.if_out;
3114 printf("Hash: Adding 0x%" PRIx32 " keys\n", array_len);
3117 #define BIT_16_TO_23 0x00ff0000
3119 populate_ipv6_few_flow_into_table(const struct rte_hash *h)
3123 uint32_t array_len = RTE_DIM(ipv6_l3fwd_route_array);
3125 mask1 = _mm_set_epi32(ALL_32_BITS, ALL_32_BITS, ALL_32_BITS, BIT_16_TO_23);
3126 mask2 = _mm_set_epi32(0, 0, ALL_32_BITS, ALL_32_BITS);
3127 for (i = 0; i < array_len; i++) {
3128 struct ipv6_l3fwd_route entry;
3129 union ipv6_5tuple_host newkey;
3131 entry = ipv6_l3fwd_route_array[i];
3132 convert_ipv6_5tuple(&entry.key, &newkey);
3133 ret = rte_hash_add_key(h, (void *)&newkey);
3135 rte_exit(EXIT_FAILURE, "Unable to add entry %" PRIu32
3136 " to the l3fwd hash.\n", i);
3138 ipv6_l3fwd_out_if[ret] = entry.if_out;
3140 printf("Hash: Adding 0x%" PRIx32 "keys\n", array_len);
3143 #define NUMBER_PORT_USED 4
3145 populate_ipv4_many_flow_into_table(const struct rte_hash *h,
3146 unsigned int nr_flow)
3150 mask0 = _mm_set_epi32(ALL_32_BITS, ALL_32_BITS, ALL_32_BITS, BIT_8_TO_15);
3152 for (i = 0; i < nr_flow; i++) {
3153 struct ipv4_l3fwd_route entry;
3154 union ipv4_5tuple_host newkey;
3155 uint8_t a = (uint8_t)((i / NUMBER_PORT_USED) % BYTE_VALUE_MAX);
3156 uint8_t b = (uint8_t)(((i / NUMBER_PORT_USED) / BYTE_VALUE_MAX) %
3158 uint8_t c = (uint8_t)((i / NUMBER_PORT_USED) / (BYTE_VALUE_MAX *
3160 /* Create the ipv4 exact match flow */
3161 memset(&entry, 0, sizeof(entry));
3162 switch (i & (NUMBER_PORT_USED - 1)) {
3164 entry = ipv4_l3fwd_route_array[0];
3165 entry.key.ip_dst = IPv4(101, c, b, a);
3168 entry = ipv4_l3fwd_route_array[1];
3169 entry.key.ip_dst = IPv4(201, c, b, a);
3172 entry = ipv4_l3fwd_route_array[2];
3173 entry.key.ip_dst = IPv4(111, c, b, a);
3176 entry = ipv4_l3fwd_route_array[3];
3177 entry.key.ip_dst = IPv4(211, c, b, a);
3180 convert_ipv4_5tuple(&entry.key, &newkey);
3181 int32_t ret = rte_hash_add_key(h, (void *)&newkey);
3184 rte_exit(EXIT_FAILURE, "Unable to add entry %u\n", i);
3186 ipv4_l3fwd_out_if[ret] = (uint8_t)entry.if_out;
3189 printf("Hash: Adding 0x%x keys\n", nr_flow);
3193 populate_ipv6_many_flow_into_table(const struct rte_hash *h,
3194 unsigned int nr_flow)
3198 mask1 = _mm_set_epi32(ALL_32_BITS, ALL_32_BITS, ALL_32_BITS, BIT_16_TO_23);
3199 mask2 = _mm_set_epi32(0, 0, ALL_32_BITS, ALL_32_BITS);
3200 for (i = 0; i < nr_flow; i++) {
3201 struct ipv6_l3fwd_route entry;
3202 union ipv6_5tuple_host newkey;
3204 uint8_t a = (uint8_t) ((i / NUMBER_PORT_USED) % BYTE_VALUE_MAX);
3205 uint8_t b = (uint8_t) (((i / NUMBER_PORT_USED) / BYTE_VALUE_MAX) %
3207 uint8_t c = (uint8_t) ((i / NUMBER_PORT_USED) / (BYTE_VALUE_MAX *
3210 /* Create the ipv6 exact match flow */
3211 memset(&entry, 0, sizeof(entry));
3212 switch (i & (NUMBER_PORT_USED - 1)) {
3214 entry = ipv6_l3fwd_route_array[0];
3217 entry = ipv6_l3fwd_route_array[1];
3220 entry = ipv6_l3fwd_route_array[2];
3223 entry = ipv6_l3fwd_route_array[3];
3226 entry.key.ip_dst[13] = c;
3227 entry.key.ip_dst[14] = b;
3228 entry.key.ip_dst[15] = a;
3229 convert_ipv6_5tuple(&entry.key, &newkey);
3230 int32_t ret = rte_hash_add_key(h, (void *)&newkey);
3233 rte_exit(EXIT_FAILURE, "Unable to add entry %u\n", i);
3235 ipv6_l3fwd_out_if[ret] = (uint8_t) entry.if_out;
3238 printf("Hash: Adding 0x%x keys\n", nr_flow);
3242 setup_hash(int socketid)
3244 struct rte_hash_parameters ipv4_l3fwd_hash_params = {
3246 .entries = L3FWD_HASH_ENTRIES,
3247 .key_len = sizeof(union ipv4_5tuple_host),
3248 .hash_func = ipv4_hash_crc,
3249 .hash_func_init_val = 0,
3252 struct rte_hash_parameters ipv6_l3fwd_hash_params = {
3254 .entries = L3FWD_HASH_ENTRIES,
3255 .key_len = sizeof(union ipv6_5tuple_host),
3256 .hash_func = ipv6_hash_crc,
3257 .hash_func_init_val = 0,
3262 /* create ipv4 hash */
3263 snprintf(s, sizeof(s), "ipv4_l3fwd_hash_%d", socketid);
3264 ipv4_l3fwd_hash_params.name = s;
3265 ipv4_l3fwd_hash_params.socket_id = socketid;
3266 ipv4_l3fwd_lookup_struct[socketid] =
3267 rte_hash_create(&ipv4_l3fwd_hash_params);
3268 if (ipv4_l3fwd_lookup_struct[socketid] == NULL)
3269 rte_exit(EXIT_FAILURE, "Unable to create the l3fwd hash on "
3270 "socket %d\n", socketid);
3272 /* create ipv6 hash */
3273 snprintf(s, sizeof(s), "ipv6_l3fwd_hash_%d", socketid);
3274 ipv6_l3fwd_hash_params.name = s;
3275 ipv6_l3fwd_hash_params.socket_id = socketid;
3276 ipv6_l3fwd_lookup_struct[socketid] =
3277 rte_hash_create(&ipv6_l3fwd_hash_params);
3278 if (ipv6_l3fwd_lookup_struct[socketid] == NULL)
3279 rte_exit(EXIT_FAILURE, "Unable to create the l3fwd hash on "
3280 "socket %d\n", socketid);
3282 if (hash_entry_number != HASH_ENTRY_NUMBER_DEFAULT) {
3283 /* For testing hash matching with a large number of flows we
3284 * generate millions of IP 5-tuples with an incremented dst
3285 * address to initialize the hash table. */
3287 /* populate the ipv4 hash */
3288 populate_ipv4_many_flow_into_table(
3289 ipv4_l3fwd_lookup_struct[socketid], hash_entry_number);
3291 /* populate the ipv6 hash */
3292 populate_ipv6_many_flow_into_table(
3293 ipv6_l3fwd_lookup_struct[socketid], hash_entry_number);
3296 /* Use data in ipv4/ipv6 l3fwd lookup table directly to initialize
3299 /* populate the ipv4 hash */
3300 populate_ipv4_few_flow_into_table(
3301 ipv4_l3fwd_lookup_struct[socketid]);
3303 /* populate the ipv6 hash */
3304 populate_ipv6_few_flow_into_table(
3305 ipv6_l3fwd_lookup_struct[socketid]);
3311 #if (APP_LOOKUP_METHOD == APP_LOOKUP_LPM)
3313 setup_lpm(int socketid)
3315 struct rte_lpm6_config config;
3316 struct rte_lpm_config lpm_ipv4_config;
3321 /* create the LPM table */
3322 snprintf(s, sizeof(s), "IPV4_L3FWD_LPM_%d", socketid);
3323 lpm_ipv4_config.max_rules = IPV4_L3FWD_LPM_MAX_RULES;
3324 lpm_ipv4_config.number_tbl8s = 256;
3325 lpm_ipv4_config.flags = 0;
3326 ipv4_l3fwd_lookup_struct[socketid] =
3327 rte_lpm_create(s, socketid, &lpm_ipv4_config);
3328 if (ipv4_l3fwd_lookup_struct[socketid] == NULL)
3329 rte_exit(EXIT_FAILURE, "Unable to create the l3fwd LPM table"
3330 " on socket %d\n", socketid);
3332 /* populate the LPM table */
3333 for (i = 0; i < IPV4_L3FWD_NUM_ROUTES; i++) {
3335 /* skip unused ports */
3336 if ((1 << ipv4_l3fwd_route_array[i].if_out &
3337 enabled_port_mask) == 0)
3340 ret = rte_lpm_add(ipv4_l3fwd_lookup_struct[socketid],
3341 ipv4_l3fwd_route_array[i].ip,
3342 ipv4_l3fwd_route_array[i].depth,
3343 ipv4_l3fwd_route_array[i].if_out);
3346 rte_exit(EXIT_FAILURE, "Unable to add entry %u to the "
3347 "l3fwd LPM table on socket %d\n",
3351 printf("LPM: Adding route 0x%08x / %d (%d)\n",
3352 (unsigned)ipv4_l3fwd_route_array[i].ip,
3353 ipv4_l3fwd_route_array[i].depth,
3354 ipv4_l3fwd_route_array[i].if_out);
3357 /* create the LPM6 table */
3358 snprintf(s, sizeof(s), "IPV6_L3FWD_LPM_%d", socketid);
3360 config.max_rules = IPV6_L3FWD_LPM_MAX_RULES;
3361 config.number_tbl8s = IPV6_L3FWD_LPM_NUMBER_TBL8S;
3363 ipv6_l3fwd_lookup_struct[socketid] = rte_lpm6_create(s, socketid,
3365 if (ipv6_l3fwd_lookup_struct[socketid] == NULL)
3366 rte_exit(EXIT_FAILURE, "Unable to create the l3fwd LPM table"
3367 " on socket %d\n", socketid);
3369 /* populate the LPM table */
3370 for (i = 0; i < IPV6_L3FWD_NUM_ROUTES; i++) {
3372 /* skip unused ports */
3373 if ((1 << ipv6_l3fwd_route_array[i].if_out &
3374 enabled_port_mask) == 0)
3377 ret = rte_lpm6_add(ipv6_l3fwd_lookup_struct[socketid],
3378 ipv6_l3fwd_route_array[i].ip,
3379 ipv6_l3fwd_route_array[i].depth,
3380 ipv6_l3fwd_route_array[i].if_out);
3383 rte_exit(EXIT_FAILURE, "Unable to add entry %u to the "
3384 "l3fwd LPM table on socket %d\n",
3388 printf("LPM: Adding route %s / %d (%d)\n",
3390 ipv6_l3fwd_route_array[i].depth,
3391 ipv6_l3fwd_route_array[i].if_out);
3397 init_mem(unsigned nb_mbuf)
3399 struct lcore_conf *qconf;
3404 for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
3405 if (rte_lcore_is_enabled(lcore_id) == 0)
3409 socketid = rte_lcore_to_socket_id(lcore_id);
3413 if (socketid >= NB_SOCKETS) {
3414 rte_exit(EXIT_FAILURE, "Socket %d of lcore %u is out of range %d\n",
3415 socketid, lcore_id, NB_SOCKETS);
3417 if (pktmbuf_pool[socketid] == NULL) {
3418 snprintf(s, sizeof(s), "mbuf_pool_%d", socketid);
3419 pktmbuf_pool[socketid] =
3420 rte_pktmbuf_pool_create(s, nb_mbuf,
3421 MEMPOOL_CACHE_SIZE, 0,
3422 RTE_MBUF_DEFAULT_BUF_SIZE, socketid);
3423 if (pktmbuf_pool[socketid] == NULL)
3424 rte_exit(EXIT_FAILURE,
3425 "Cannot init mbuf pool on socket %d\n", socketid);
3427 printf("Allocated mbuf pool on socket %d\n", socketid);
3429 #if (APP_LOOKUP_METHOD == APP_LOOKUP_LPM)
3430 setup_lpm(socketid);
3432 setup_hash(socketid);
3435 qconf = &lcore_conf[lcore_id];
3436 qconf->ipv4_lookup_struct = ipv4_l3fwd_lookup_struct[socketid];
3437 qconf->ipv6_lookup_struct = ipv6_l3fwd_lookup_struct[socketid];
3442 /* Check the link status of all ports in up to 9s, and print them finally */
3444 check_all_ports_link_status(uint16_t port_num, uint32_t port_mask)
3446 #define CHECK_INTERVAL 100 /* 100ms */
3447 #define MAX_CHECK_TIME 90 /* 9s (90 * 100ms) in total */
3449 uint8_t count, all_ports_up, print_flag = 0;
3450 struct rte_eth_link link;
3452 printf("\nChecking link status");
3454 for (count = 0; count <= MAX_CHECK_TIME; count++) {
3456 for (portid = 0; portid < port_num; portid++) {
3457 if ((port_mask & (1 << portid)) == 0)
3459 memset(&link, 0, sizeof(link));
3460 rte_eth_link_get_nowait(portid, &link);
3461 /* print link status if flag set */
3462 if (print_flag == 1) {
3463 if (link.link_status)
3465 "Port%d Link Up. Speed %u Mbps - %s\n",
3466 portid, link.link_speed,
3467 (link.link_duplex == ETH_LINK_FULL_DUPLEX) ?
3468 ("full-duplex") : ("half-duplex\n"));
3470 printf("Port %d Link Down\n", portid);
3473 /* clear all_ports_up flag if any link down */
3474 if (link.link_status == ETH_LINK_DOWN) {
3479 /* after finally printing all link status, get out */
3480 if (print_flag == 1)
3483 if (all_ports_up == 0) {
3486 rte_delay_ms(CHECK_INTERVAL);
3489 /* set the print_flag if all ports up or timeout */
3490 if (all_ports_up == 1 || count == (MAX_CHECK_TIME - 1)) {
3498 main(int argc, char **argv)
3500 struct rte_eth_dev_info dev_info;
3501 struct rte_eth_txconf *txconf;
3505 uint16_t queueid, portid;
3507 uint32_t n_tx_queue, nb_lcores;
3508 uint8_t nb_rx_queue, queue, socketid;
3511 ret = rte_eal_init(argc, argv);
3513 rte_exit(EXIT_FAILURE, "Invalid EAL parameters\n");
3517 /* pre-init dst MACs for all ports to 02:00:00:00:00:xx */
3518 for (portid = 0; portid < RTE_MAX_ETHPORTS; portid++) {
3519 dest_eth_addr[portid] = ETHER_LOCAL_ADMIN_ADDR +
3520 ((uint64_t)portid << 40);
3521 *(uint64_t *)(val_eth + portid) = dest_eth_addr[portid];
3524 /* parse application arguments (after the EAL ones) */
3525 ret = parse_args(argc, argv);
3527 rte_exit(EXIT_FAILURE, "Invalid L3FWD parameters\n");
3529 if (check_lcore_params() < 0)
3530 rte_exit(EXIT_FAILURE, "check_lcore_params failed\n");
3532 printf("Initializing rx-queues...\n");
3533 ret = init_rx_queues();
3535 rte_exit(EXIT_FAILURE, "init_rx_queues failed\n");
3537 printf("Initializing tx-threads...\n");
3538 ret = init_tx_threads();
3540 rte_exit(EXIT_FAILURE, "init_tx_threads failed\n");
3542 printf("Initializing rings...\n");
3543 ret = init_rx_rings();
3545 rte_exit(EXIT_FAILURE, "init_rx_rings failed\n");
3547 nb_ports = rte_eth_dev_count();
3549 if (check_port_config(nb_ports) < 0)
3550 rte_exit(EXIT_FAILURE, "check_port_config failed\n");
3552 nb_lcores = rte_lcore_count();
3554 /* initialize all ports */
3555 for (portid = 0; portid < nb_ports; portid++) {
3556 /* skip ports that are not enabled */
3557 if ((enabled_port_mask & (1 << portid)) == 0) {
3558 printf("\nSkipping disabled port %d\n", portid);
3563 printf("Initializing port %d ... ", portid);
3566 nb_rx_queue = get_port_n_rx_queues(portid);
3567 n_tx_queue = nb_lcores;
3568 if (n_tx_queue > MAX_TX_QUEUE_PER_PORT)
3569 n_tx_queue = MAX_TX_QUEUE_PER_PORT;
3570 printf("Creating queues: nb_rxq=%d nb_txq=%u... ",
3571 nb_rx_queue, (unsigned)n_tx_queue);
3572 ret = rte_eth_dev_configure(portid, nb_rx_queue,
3573 (uint16_t)n_tx_queue, &port_conf);
3575 rte_exit(EXIT_FAILURE, "Cannot configure device: err=%d, port=%d\n",
3578 ret = rte_eth_dev_adjust_nb_rx_tx_desc(portid, &nb_rxd,
3581 rte_exit(EXIT_FAILURE,
3582 "rte_eth_dev_adjust_nb_rx_tx_desc: err=%d, port=%d\n",
3585 rte_eth_macaddr_get(portid, &ports_eth_addr[portid]);
3586 print_ethaddr(" Address:", &ports_eth_addr[portid]);
3588 print_ethaddr("Destination:",
3589 (const struct ether_addr *)&dest_eth_addr[portid]);
3593 * prepare src MACs for each port.
3595 ether_addr_copy(&ports_eth_addr[portid],
3596 (struct ether_addr *)(val_eth + portid) + 1);
3599 ret = init_mem(NB_MBUF);
3601 rte_exit(EXIT_FAILURE, "init_mem failed\n");
3603 /* init one TX queue per couple (lcore,port) */
3605 for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
3606 if (rte_lcore_is_enabled(lcore_id) == 0)
3610 socketid = (uint8_t)rte_lcore_to_socket_id(lcore_id);
3614 printf("txq=%u,%d,%d ", lcore_id, queueid, socketid);
3617 rte_eth_dev_info_get(portid, &dev_info);
3618 txconf = &dev_info.default_txconf;
3619 if (port_conf.rxmode.jumbo_frame)
3620 txconf->txq_flags = 0;
3621 ret = rte_eth_tx_queue_setup(portid, queueid, nb_txd,
3624 rte_exit(EXIT_FAILURE, "rte_eth_tx_queue_setup: err=%d, "
3625 "port=%d\n", ret, portid);
3627 tx_thread[lcore_id].tx_queue_id[portid] = queueid;
3633 for (i = 0; i < n_rx_thread; i++) {
3634 lcore_id = rx_thread[i].conf.lcore_id;
3636 if (rte_lcore_is_enabled(lcore_id) == 0) {
3637 rte_exit(EXIT_FAILURE,
3638 "Cannot start Rx thread on lcore %u: lcore disabled\n",
3643 printf("\nInitializing rx queues for Rx thread %d on lcore %u ... ",
3647 /* init RX queues */
3648 for (queue = 0; queue < rx_thread[i].n_rx_queue; ++queue) {
3649 portid = rx_thread[i].rx_queue_list[queue].port_id;
3650 queueid = rx_thread[i].rx_queue_list[queue].queue_id;
3653 socketid = (uint8_t)rte_lcore_to_socket_id(lcore_id);
3657 printf("rxq=%d,%d,%d ", portid, queueid, socketid);
3660 ret = rte_eth_rx_queue_setup(portid, queueid, nb_rxd,
3663 pktmbuf_pool[socketid]);
3665 rte_exit(EXIT_FAILURE, "rte_eth_rx_queue_setup: err=%d, "
3666 "port=%d\n", ret, portid);
3673 for (portid = 0; portid < nb_ports; portid++) {
3674 if ((enabled_port_mask & (1 << portid)) == 0)
3678 ret = rte_eth_dev_start(portid);
3680 rte_exit(EXIT_FAILURE, "rte_eth_dev_start: err=%d, port=%d\n",
3684 * If enabled, put device in promiscuous mode.
3685 * This allows IO forwarding mode to forward packets
3686 * to itself through 2 cross-connected ports of the
3690 rte_eth_promiscuous_enable(portid);
3693 for (i = 0; i < n_rx_thread; i++) {
3694 lcore_id = rx_thread[i].conf.lcore_id;
3695 if (rte_lcore_is_enabled(lcore_id) == 0)
3698 /* check if hw packet type is supported */
3699 for (queue = 0; queue < rx_thread[i].n_rx_queue; ++queue) {
3700 portid = rx_thread[i].rx_queue_list[queue].port_id;
3701 queueid = rx_thread[i].rx_queue_list[queue].queue_id;
3703 if (parse_ptype_on) {
3704 if (!rte_eth_add_rx_callback(portid, queueid,
3705 cb_parse_ptype, NULL))
3706 rte_exit(EXIT_FAILURE,
3707 "Failed to add rx callback: "
3708 "port=%d\n", portid);
3709 } else if (!check_ptype(portid))
3710 rte_exit(EXIT_FAILURE,
3711 "Port %d cannot parse packet type.\n\n"
3712 "Please add --parse-ptype to use sw "
3713 "packet type analyzer.\n\n",
3718 check_all_ports_link_status((uint8_t)nb_ports, enabled_port_mask);
3721 printf("Starting L-Threading Model\n");
3723 #if (APP_CPU_LOAD > 0)
3724 if (cpu_load_lcore_id > 0)
3725 /* Use one lcore for cpu load collector */
3729 lthread_num_schedulers_set(nb_lcores);
3730 rte_eal_mp_remote_launch(sched_spawner, NULL, SKIP_MASTER);
3731 lthread_master_spawner(NULL);
3734 printf("Starting P-Threading Model\n");
3735 /* launch per-lcore init on every lcore */
3736 rte_eal_mp_remote_launch(pthread_run, NULL, CALL_MASTER);
3737 RTE_LCORE_FOREACH_SLAVE(lcore_id) {
3738 if (rte_eal_wait_lcore(lcore_id) < 0)