1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2010-2016 Intel Corporation
11 #include <sys/queue.h>
16 #include <rte_common.h>
18 #include <rte_byteorder.h>
20 #include <rte_memory.h>
21 #include <rte_memcpy.h>
23 #include <rte_launch.h>
24 #include <rte_atomic.h>
25 #include <rte_cycles.h>
26 #include <rte_prefetch.h>
27 #include <rte_lcore.h>
28 #include <rte_per_lcore.h>
29 #include <rte_branch_prediction.h>
30 #include <rte_interrupts.h>
31 #include <rte_random.h>
32 #include <rte_debug.h>
33 #include <rte_ether.h>
34 #include <rte_ethdev.h>
36 #include <rte_mempool.h>
41 #include <rte_string_fns.h>
42 #include <rte_pause.h>
44 #include <cmdline_parse.h>
45 #include <cmdline_parse_etheraddr.h>
47 #include <lthread_api.h>
49 #define APP_LOOKUP_EXACT_MATCH 0
50 #define APP_LOOKUP_LPM 1
51 #define DO_RFC_1812_CHECKS
53 /* Enable cpu-load stats 0-off, 1-on */
54 #define APP_CPU_LOAD 1
56 #ifndef APP_LOOKUP_METHOD
57 #define APP_LOOKUP_METHOD APP_LOOKUP_LPM
60 #ifndef __GLIBC__ /* sched_getcpu() is glibc specific */
61 #define sched_getcpu() rte_lcore_id()
65 check_ptype(int portid)
68 int ipv4 = 0, ipv6 = 0;
70 ret = rte_eth_dev_get_supported_ptypes(portid, RTE_PTYPE_L3_MASK, NULL,
77 ret = rte_eth_dev_get_supported_ptypes(portid, RTE_PTYPE_L3_MASK,
79 for (i = 0; i < ret; ++i) {
80 if (ptypes[i] & RTE_PTYPE_L3_IPV4)
82 if (ptypes[i] & RTE_PTYPE_L3_IPV6)
93 parse_ptype(struct rte_mbuf *m)
95 struct ether_hdr *eth_hdr;
96 uint32_t packet_type = RTE_PTYPE_UNKNOWN;
99 eth_hdr = rte_pktmbuf_mtod(m, struct ether_hdr *);
100 ether_type = eth_hdr->ether_type;
101 if (ether_type == rte_cpu_to_be_16(ETHER_TYPE_IPv4))
102 packet_type |= RTE_PTYPE_L3_IPV4_EXT_UNKNOWN;
103 else if (ether_type == rte_cpu_to_be_16(ETHER_TYPE_IPv6))
104 packet_type |= RTE_PTYPE_L3_IPV6_EXT_UNKNOWN;
106 m->packet_type = packet_type;
110 cb_parse_ptype(__rte_unused uint16_t port, __rte_unused uint16_t queue,
111 struct rte_mbuf *pkts[], uint16_t nb_pkts,
112 __rte_unused uint16_t max_pkts, __rte_unused void *user_param)
116 for (i = 0; i < nb_pkts; i++)
117 parse_ptype(pkts[i]);
123 * When set to zero, simple forwaring path is eanbled.
124 * When set to one, optimized forwarding path is enabled.
125 * Note that LPM optimisation path uses SSE4.1 instructions.
127 #define ENABLE_MULTI_BUFFER_OPTIMIZE 1
129 #if (APP_LOOKUP_METHOD == APP_LOOKUP_EXACT_MATCH)
130 #include <rte_hash.h>
131 #elif (APP_LOOKUP_METHOD == APP_LOOKUP_LPM)
133 #include <rte_lpm6.h>
135 #error "APP_LOOKUP_METHOD set to incorrect value"
138 #define RTE_LOGTYPE_L3FWD RTE_LOGTYPE_USER1
140 #define MAX_JUMBO_PKT_LEN 9600
142 #define IPV6_ADDR_LEN 16
144 #define MEMPOOL_CACHE_SIZE 256
147 * This expression is used to calculate the number of mbufs needed depending on
148 * user input, taking into account memory for rx and tx hardware rings, cache
149 * per lcore and mtable per port per lcore. RTE_MAX is used to ensure that
150 * NB_MBUF never goes below a minimum value of 8192
153 #define NB_MBUF RTE_MAX(\
154 (nb_ports*nb_rx_queue*nb_rxd + \
155 nb_ports*nb_lcores*MAX_PKT_BURST + \
156 nb_ports*n_tx_queue*nb_txd + \
157 nb_lcores*MEMPOOL_CACHE_SIZE), \
160 #define MAX_PKT_BURST 32
161 #define BURST_TX_DRAIN_US 100 /* TX drain every ~100us */
164 * Try to avoid TX buffering if we have at least MAX_TX_BURST packets to send.
166 #define MAX_TX_BURST (MAX_PKT_BURST / 2)
167 #define BURST_SIZE MAX_TX_BURST
171 /* Configure how many packets ahead to prefetch, when reading packets */
172 #define PREFETCH_OFFSET 3
174 /* Used to mark destination port as 'invalid'. */
175 #define BAD_PORT ((uint16_t)-1)
180 * Configurable number of RX/TX ring descriptors
182 #define RTE_TEST_RX_DESC_DEFAULT 1024
183 #define RTE_TEST_TX_DESC_DEFAULT 1024
184 static uint16_t nb_rxd = RTE_TEST_RX_DESC_DEFAULT;
185 static uint16_t nb_txd = RTE_TEST_TX_DESC_DEFAULT;
187 /* ethernet addresses of ports */
188 static uint64_t dest_eth_addr[RTE_MAX_ETHPORTS];
189 static struct ether_addr ports_eth_addr[RTE_MAX_ETHPORTS];
191 static xmm_t val_eth[RTE_MAX_ETHPORTS];
193 /* replace first 12B of the ethernet header. */
194 #define MASK_ETH 0x3f
196 /* mask of enabled ports */
197 static uint32_t enabled_port_mask;
198 static int promiscuous_on; /**< Set in promiscuous mode off by default. */
199 static int numa_on = 1; /**< NUMA is enabled by default. */
200 static int parse_ptype_on;
202 #if (APP_LOOKUP_METHOD == APP_LOOKUP_EXACT_MATCH)
203 static int ipv6; /**< ipv6 is false by default. */
206 #if (APP_CPU_LOAD == 1)
208 #define MAX_CPU RTE_MAX_LCORE
209 #define CPU_LOAD_TIMEOUT_US (5 * 1000 * 1000) /**< Timeout for collecting 5s */
211 #define CPU_PROCESS 0
213 #define MAX_CPU_COUNTER 2
218 uint64_t hits[MAX_CPU_COUNTER][MAX_CPU];
219 } __rte_cache_aligned;
221 static struct cpu_load cpu_load;
222 static int cpu_load_lcore_id = -1;
224 #define SET_CPU_BUSY(thread, counter) \
225 thread->conf.busy[counter] = 1
227 #define SET_CPU_IDLE(thread, counter) \
228 thread->conf.busy[counter] = 0
230 #define IS_CPU_BUSY(thread, counter) \
231 (thread->conf.busy[counter] > 0)
235 #define SET_CPU_BUSY(thread, counter)
236 #define SET_CPU_IDLE(thread, counter)
237 #define IS_CPU_BUSY(thread, counter) 0
243 struct rte_mbuf *m_table[MAX_PKT_BURST];
246 struct lcore_rx_queue {
249 } __rte_cache_aligned;
251 #define MAX_RX_QUEUE_PER_LCORE 16
252 #define MAX_TX_QUEUE_PER_PORT RTE_MAX_ETHPORTS
253 #define MAX_RX_QUEUE_PER_PORT 128
255 #define MAX_LCORE_PARAMS 1024
256 struct rx_thread_params {
261 } __rte_cache_aligned;
263 static struct rx_thread_params rx_thread_params_array[MAX_LCORE_PARAMS];
264 static struct rx_thread_params rx_thread_params_array_default[] = {
276 static struct rx_thread_params *rx_thread_params =
277 rx_thread_params_array_default;
278 static uint16_t nb_rx_thread_params = RTE_DIM(rx_thread_params_array_default);
280 struct tx_thread_params {
283 } __rte_cache_aligned;
285 static struct tx_thread_params tx_thread_params_array[MAX_LCORE_PARAMS];
286 static struct tx_thread_params tx_thread_params_array_default[] = {
298 static struct tx_thread_params *tx_thread_params =
299 tx_thread_params_array_default;
300 static uint16_t nb_tx_thread_params = RTE_DIM(tx_thread_params_array_default);
302 static struct rte_eth_conf port_conf = {
304 .mq_mode = ETH_MQ_RX_RSS,
305 .max_rx_pkt_len = ETHER_MAX_LEN,
307 .offloads = DEV_RX_OFFLOAD_CHECKSUM,
312 .rss_hf = ETH_RSS_TCP,
316 .mq_mode = ETH_MQ_TX_NONE,
320 static struct rte_mempool *pktmbuf_pool[NB_SOCKETS];
322 #if (APP_LOOKUP_METHOD == APP_LOOKUP_EXACT_MATCH)
324 #include <rte_hash_crc.h>
325 #define DEFAULT_HASH_FUNC rte_hash_crc
333 } __attribute__((__packed__));
335 union ipv4_5tuple_host {
348 #define XMM_NUM_IN_IPV6_5TUPLE 3
351 uint8_t ip_dst[IPV6_ADDR_LEN];
352 uint8_t ip_src[IPV6_ADDR_LEN];
356 } __attribute__((__packed__));
358 union ipv6_5tuple_host {
363 uint8_t ip_src[IPV6_ADDR_LEN];
364 uint8_t ip_dst[IPV6_ADDR_LEN];
369 __m128i xmm[XMM_NUM_IN_IPV6_5TUPLE];
372 struct ipv4_l3fwd_route {
373 struct ipv4_5tuple key;
377 struct ipv6_l3fwd_route {
378 struct ipv6_5tuple key;
382 static struct ipv4_l3fwd_route ipv4_l3fwd_route_array[] = {
383 {{IPv4(101, 0, 0, 0), IPv4(100, 10, 0, 1), 101, 11, IPPROTO_TCP}, 0},
384 {{IPv4(201, 0, 0, 0), IPv4(200, 20, 0, 1), 102, 12, IPPROTO_TCP}, 1},
385 {{IPv4(111, 0, 0, 0), IPv4(100, 30, 0, 1), 101, 11, IPPROTO_TCP}, 2},
386 {{IPv4(211, 0, 0, 0), IPv4(200, 40, 0, 1), 102, 12, IPPROTO_TCP}, 3},
389 static struct ipv6_l3fwd_route ipv6_l3fwd_route_array[] = {
391 {0xfe, 0x80, 0, 0, 0, 0, 0, 0, 0x02, 0x1e, 0x67, 0xff, 0xfe, 0, 0, 0},
392 {0xfe, 0x80, 0, 0, 0, 0, 0, 0, 0x02, 0x1b, 0x21, 0xff, 0xfe, 0x91, 0x38,
394 101, 11, IPPROTO_TCP}, 0},
397 {0xfe, 0x90, 0, 0, 0, 0, 0, 0, 0x02, 0x1e, 0x67, 0xff, 0xfe, 0, 0, 0},
398 {0xfe, 0x90, 0, 0, 0, 0, 0, 0, 0x02, 0x1b, 0x21, 0xff, 0xfe, 0x91, 0x38,
400 102, 12, IPPROTO_TCP}, 1},
403 {0xfe, 0xa0, 0, 0, 0, 0, 0, 0, 0x02, 0x1e, 0x67, 0xff, 0xfe, 0, 0, 0},
404 {0xfe, 0xa0, 0, 0, 0, 0, 0, 0, 0x02, 0x1b, 0x21, 0xff, 0xfe, 0x91, 0x38,
406 101, 11, IPPROTO_TCP}, 2},
409 {0xfe, 0xb0, 0, 0, 0, 0, 0, 0, 0x02, 0x1e, 0x67, 0xff, 0xfe, 0, 0, 0},
410 {0xfe, 0xb0, 0, 0, 0, 0, 0, 0, 0x02, 0x1b, 0x21, 0xff, 0xfe, 0x91, 0x38,
412 102, 12, IPPROTO_TCP}, 3},
415 typedef struct rte_hash lookup_struct_t;
416 static lookup_struct_t *ipv4_l3fwd_lookup_struct[NB_SOCKETS];
417 static lookup_struct_t *ipv6_l3fwd_lookup_struct[NB_SOCKETS];
419 #ifdef RTE_ARCH_X86_64
420 /* default to 4 million hash entries (approx) */
421 #define L3FWD_HASH_ENTRIES (1024*1024*4)
423 /* 32-bit has less address-space for hugepage memory, limit to 1M entries */
424 #define L3FWD_HASH_ENTRIES (1024*1024*1)
426 #define HASH_ENTRY_NUMBER_DEFAULT 4
428 static uint32_t hash_entry_number = HASH_ENTRY_NUMBER_DEFAULT;
430 static inline uint32_t
431 ipv4_hash_crc(const void *data, __rte_unused uint32_t data_len,
434 const union ipv4_5tuple_host *k;
440 p = (const uint32_t *)&k->port_src;
442 init_val = rte_hash_crc_4byte(t, init_val);
443 init_val = rte_hash_crc_4byte(k->ip_src, init_val);
444 init_val = rte_hash_crc_4byte(k->ip_dst, init_val);
445 init_val = rte_hash_crc_4byte(*p, init_val);
449 static inline uint32_t
450 ipv6_hash_crc(const void *data, __rte_unused uint32_t data_len,
453 const union ipv6_5tuple_host *k;
456 const uint32_t *ip_src0, *ip_src1, *ip_src2, *ip_src3;
457 const uint32_t *ip_dst0, *ip_dst1, *ip_dst2, *ip_dst3;
461 p = (const uint32_t *)&k->port_src;
463 ip_src0 = (const uint32_t *) k->ip_src;
464 ip_src1 = (const uint32_t *)(k->ip_src + 4);
465 ip_src2 = (const uint32_t *)(k->ip_src + 8);
466 ip_src3 = (const uint32_t *)(k->ip_src + 12);
467 ip_dst0 = (const uint32_t *) k->ip_dst;
468 ip_dst1 = (const uint32_t *)(k->ip_dst + 4);
469 ip_dst2 = (const uint32_t *)(k->ip_dst + 8);
470 ip_dst3 = (const uint32_t *)(k->ip_dst + 12);
471 init_val = rte_hash_crc_4byte(t, init_val);
472 init_val = rte_hash_crc_4byte(*ip_src0, init_val);
473 init_val = rte_hash_crc_4byte(*ip_src1, init_val);
474 init_val = rte_hash_crc_4byte(*ip_src2, init_val);
475 init_val = rte_hash_crc_4byte(*ip_src3, init_val);
476 init_val = rte_hash_crc_4byte(*ip_dst0, init_val);
477 init_val = rte_hash_crc_4byte(*ip_dst1, init_val);
478 init_val = rte_hash_crc_4byte(*ip_dst2, init_val);
479 init_val = rte_hash_crc_4byte(*ip_dst3, init_val);
480 init_val = rte_hash_crc_4byte(*p, init_val);
484 #define IPV4_L3FWD_NUM_ROUTES RTE_DIM(ipv4_l3fwd_route_array)
485 #define IPV6_L3FWD_NUM_ROUTES RTE_DIM(ipv6_l3fwd_route_array)
487 static uint8_t ipv4_l3fwd_out_if[L3FWD_HASH_ENTRIES] __rte_cache_aligned;
488 static uint8_t ipv6_l3fwd_out_if[L3FWD_HASH_ENTRIES] __rte_cache_aligned;
492 #if (APP_LOOKUP_METHOD == APP_LOOKUP_LPM)
493 struct ipv4_l3fwd_route {
499 struct ipv6_l3fwd_route {
505 static struct ipv4_l3fwd_route ipv4_l3fwd_route_array[] = {
506 {IPv4(1, 1, 1, 0), 24, 0},
507 {IPv4(2, 1, 1, 0), 24, 1},
508 {IPv4(3, 1, 1, 0), 24, 2},
509 {IPv4(4, 1, 1, 0), 24, 3},
510 {IPv4(5, 1, 1, 0), 24, 4},
511 {IPv4(6, 1, 1, 0), 24, 5},
512 {IPv4(7, 1, 1, 0), 24, 6},
513 {IPv4(8, 1, 1, 0), 24, 7},
516 static struct ipv6_l3fwd_route ipv6_l3fwd_route_array[] = {
517 {{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, 48, 0},
518 {{2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, 48, 1},
519 {{3, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, 48, 2},
520 {{4, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, 48, 3},
521 {{5, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, 48, 4},
522 {{6, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, 48, 5},
523 {{7, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, 48, 6},
524 {{8, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, 48, 7},
527 #define IPV4_L3FWD_NUM_ROUTES RTE_DIM(ipv4_l3fwd_route_array)
528 #define IPV6_L3FWD_NUM_ROUTES RTE_DIM(ipv6_l3fwd_route_array)
530 #define IPV4_L3FWD_LPM_MAX_RULES 1024
531 #define IPV6_L3FWD_LPM_MAX_RULES 1024
532 #define IPV6_L3FWD_LPM_NUMBER_TBL8S (1 << 16)
534 typedef struct rte_lpm lookup_struct_t;
535 typedef struct rte_lpm6 lookup6_struct_t;
536 static lookup_struct_t *ipv4_l3fwd_lookup_struct[NB_SOCKETS];
537 static lookup6_struct_t *ipv6_l3fwd_lookup_struct[NB_SOCKETS];
541 lookup_struct_t *ipv4_lookup_struct;
542 #if (APP_LOOKUP_METHOD == APP_LOOKUP_LPM)
543 lookup6_struct_t *ipv6_lookup_struct;
545 lookup_struct_t *ipv6_lookup_struct;
548 } __rte_cache_aligned;
550 static struct lcore_conf lcore_conf[RTE_MAX_LCORE];
551 RTE_DEFINE_PER_LCORE(struct lcore_conf *, lcore_conf);
553 #define MAX_RX_QUEUE_PER_THREAD 16
554 #define MAX_TX_PORT_PER_THREAD RTE_MAX_ETHPORTS
555 #define MAX_TX_QUEUE_PER_PORT RTE_MAX_ETHPORTS
556 #define MAX_RX_QUEUE_PER_PORT 128
558 #define MAX_RX_THREAD 1024
559 #define MAX_TX_THREAD 1024
560 #define MAX_THREAD (MAX_RX_THREAD + MAX_TX_THREAD)
563 * Producers and consumers threads configuration
565 static int lthreads_on = 1; /**< Use lthreads for processing*/
567 rte_atomic16_t rx_counter; /**< Number of spawned rx threads */
568 rte_atomic16_t tx_counter; /**< Number of spawned tx threads */
571 uint16_t lcore_id; /**< Initial lcore for rx thread */
572 uint16_t cpu_id; /**< Cpu id for cpu load stats counter */
573 uint16_t thread_id; /**< Thread ID */
575 #if (APP_CPU_LOAD > 0)
576 int busy[MAX_CPU_COUNTER];
580 struct thread_rx_conf {
581 struct thread_conf conf;
584 struct lcore_rx_queue rx_queue_list[MAX_RX_QUEUE_PER_LCORE];
586 uint16_t n_ring; /**< Number of output rings */
587 struct rte_ring *ring[RTE_MAX_LCORE];
588 struct lthread_cond *ready[RTE_MAX_LCORE];
590 #if (APP_CPU_LOAD > 0)
591 int busy[MAX_CPU_COUNTER];
593 } __rte_cache_aligned;
595 uint16_t n_rx_thread;
596 struct thread_rx_conf rx_thread[MAX_RX_THREAD];
598 struct thread_tx_conf {
599 struct thread_conf conf;
601 uint16_t tx_queue_id[RTE_MAX_LCORE];
602 struct mbuf_table tx_mbufs[RTE_MAX_LCORE];
604 struct rte_ring *ring;
605 struct lthread_cond **ready;
607 } __rte_cache_aligned;
609 uint16_t n_tx_thread;
610 struct thread_tx_conf tx_thread[MAX_TX_THREAD];
612 /* Send burst of packets on an output interface */
614 send_burst(struct thread_tx_conf *qconf, uint16_t n, uint16_t port)
616 struct rte_mbuf **m_table;
620 queueid = qconf->tx_queue_id[port];
621 m_table = (struct rte_mbuf **)qconf->tx_mbufs[port].m_table;
623 ret = rte_eth_tx_burst(port, queueid, m_table, n);
624 if (unlikely(ret < n)) {
626 rte_pktmbuf_free(m_table[ret]);
633 /* Enqueue a single packet, and send burst if queue is filled */
635 send_single_packet(struct rte_mbuf *m, uint16_t port)
638 struct thread_tx_conf *qconf;
641 qconf = (struct thread_tx_conf *)lthread_get_data();
643 qconf = (struct thread_tx_conf *)RTE_PER_LCORE(lcore_conf)->data;
645 len = qconf->tx_mbufs[port].len;
646 qconf->tx_mbufs[port].m_table[len] = m;
649 /* enough pkts to be sent */
650 if (unlikely(len == MAX_PKT_BURST)) {
651 send_burst(qconf, MAX_PKT_BURST, port);
655 qconf->tx_mbufs[port].len = len;
659 #if ((APP_LOOKUP_METHOD == APP_LOOKUP_LPM) && \
660 (ENABLE_MULTI_BUFFER_OPTIMIZE == 1))
661 static __rte_always_inline void
662 send_packetsx4(uint16_t port,
663 struct rte_mbuf *m[], uint32_t num)
666 struct thread_tx_conf *qconf;
669 qconf = (struct thread_tx_conf *)lthread_get_data();
671 qconf = (struct thread_tx_conf *)RTE_PER_LCORE(lcore_conf)->data;
673 len = qconf->tx_mbufs[port].len;
676 * If TX buffer for that queue is empty, and we have enough packets,
677 * then send them straightway.
679 if (num >= MAX_TX_BURST && len == 0) {
680 n = rte_eth_tx_burst(port, qconf->tx_queue_id[port], m, num);
681 if (unlikely(n < num)) {
683 rte_pktmbuf_free(m[n]);
690 * Put packets into TX buffer for that queue.
694 n = (n > MAX_PKT_BURST) ? MAX_PKT_BURST - len : num;
697 switch (n % FWDSTEP) {
700 qconf->tx_mbufs[port].m_table[len + j] = m[j];
704 qconf->tx_mbufs[port].m_table[len + j] = m[j];
708 qconf->tx_mbufs[port].m_table[len + j] = m[j];
712 qconf->tx_mbufs[port].m_table[len + j] = m[j];
719 /* enough pkts to be sent */
720 if (unlikely(len == MAX_PKT_BURST)) {
722 send_burst(qconf, MAX_PKT_BURST, port);
724 /* copy rest of the packets into the TX buffer. */
727 switch (len % FWDSTEP) {
730 qconf->tx_mbufs[port].m_table[j] = m[n + j];
734 qconf->tx_mbufs[port].m_table[j] = m[n + j];
738 qconf->tx_mbufs[port].m_table[j] = m[n + j];
742 qconf->tx_mbufs[port].m_table[j] = m[n + j];
748 qconf->tx_mbufs[port].len = len;
750 #endif /* APP_LOOKUP_LPM */
752 #ifdef DO_RFC_1812_CHECKS
754 is_valid_ipv4_pkt(struct ipv4_hdr *pkt, uint32_t link_len)
756 /* From http://www.rfc-editor.org/rfc/rfc1812.txt section 5.2.2 */
758 * 1. The packet length reported by the Link Layer must be large
759 * enough to hold the minimum length legal IP datagram (20 bytes).
761 if (link_len < sizeof(struct ipv4_hdr))
764 /* 2. The IP checksum must be correct. */
765 /* this is checked in H/W */
768 * 3. The IP version number must be 4. If the version number is not 4
769 * then the packet may be another version of IP, such as IPng or
772 if (((pkt->version_ihl) >> 4) != 4)
775 * 4. The IP header length field must be large enough to hold the
776 * minimum length legal IP datagram (20 bytes = 5 words).
778 if ((pkt->version_ihl & 0xf) < 5)
782 * 5. The IP total length field must be large enough to hold the IP
783 * datagram header, whose length is specified in the IP header length
786 if (rte_cpu_to_be_16(pkt->total_length) < sizeof(struct ipv4_hdr))
793 #if (APP_LOOKUP_METHOD == APP_LOOKUP_EXACT_MATCH)
795 static __m128i mask0;
796 static __m128i mask1;
797 static __m128i mask2;
798 static inline uint16_t
799 get_ipv4_dst_port(void *ipv4_hdr, uint16_t portid,
800 lookup_struct_t *ipv4_l3fwd_lookup_struct)
803 union ipv4_5tuple_host key;
805 ipv4_hdr = (uint8_t *)ipv4_hdr + offsetof(struct ipv4_hdr, time_to_live);
806 __m128i data = _mm_loadu_si128((__m128i *)(ipv4_hdr));
807 /* Get 5 tuple: dst port, src port, dst IP address, src IP address and
809 key.xmm = _mm_and_si128(data, mask0);
810 /* Find destination port */
811 ret = rte_hash_lookup(ipv4_l3fwd_lookup_struct, (const void *)&key);
812 return ((ret < 0) ? portid : ipv4_l3fwd_out_if[ret]);
815 static inline uint16_t
816 get_ipv6_dst_port(void *ipv6_hdr, uint16_t portid,
817 lookup_struct_t *ipv6_l3fwd_lookup_struct)
820 union ipv6_5tuple_host key;
822 ipv6_hdr = (uint8_t *)ipv6_hdr + offsetof(struct ipv6_hdr, payload_len);
823 __m128i data0 = _mm_loadu_si128((__m128i *)(ipv6_hdr));
824 __m128i data1 = _mm_loadu_si128((__m128i *)(((uint8_t *)ipv6_hdr) +
826 __m128i data2 = _mm_loadu_si128((__m128i *)(((uint8_t *)ipv6_hdr) +
827 sizeof(__m128i) + sizeof(__m128i)));
828 /* Get part of 5 tuple: src IP address lower 96 bits and protocol */
829 key.xmm[0] = _mm_and_si128(data0, mask1);
830 /* Get part of 5 tuple: dst IP address lower 96 bits and src IP address
833 /* Get part of 5 tuple: dst port and src port and dst IP address higher
835 key.xmm[2] = _mm_and_si128(data2, mask2);
837 /* Find destination port */
838 ret = rte_hash_lookup(ipv6_l3fwd_lookup_struct, (const void *)&key);
839 return ((ret < 0) ? portid : ipv6_l3fwd_out_if[ret]);
843 #if (APP_LOOKUP_METHOD == APP_LOOKUP_LPM)
845 static inline uint16_t
846 get_ipv4_dst_port(void *ipv4_hdr, uint16_t portid,
847 lookup_struct_t *ipv4_l3fwd_lookup_struct)
851 return ((rte_lpm_lookup(ipv4_l3fwd_lookup_struct,
852 rte_be_to_cpu_32(((struct ipv4_hdr *)ipv4_hdr)->dst_addr),
853 &next_hop) == 0) ? next_hop : portid);
856 static inline uint16_t
857 get_ipv6_dst_port(void *ipv6_hdr, uint16_t portid,
858 lookup6_struct_t *ipv6_l3fwd_lookup_struct)
862 return ((rte_lpm6_lookup(ipv6_l3fwd_lookup_struct,
863 ((struct ipv6_hdr *)ipv6_hdr)->dst_addr, &next_hop) == 0) ?
868 static inline void l3fwd_simple_forward(struct rte_mbuf *m, uint16_t portid)
869 __attribute__((unused));
871 #if ((APP_LOOKUP_METHOD == APP_LOOKUP_EXACT_MATCH) && \
872 (ENABLE_MULTI_BUFFER_OPTIMIZE == 1))
874 #define MASK_ALL_PKTS 0xff
875 #define EXCLUDE_1ST_PKT 0xfe
876 #define EXCLUDE_2ND_PKT 0xfd
877 #define EXCLUDE_3RD_PKT 0xfb
878 #define EXCLUDE_4TH_PKT 0xf7
879 #define EXCLUDE_5TH_PKT 0xef
880 #define EXCLUDE_6TH_PKT 0xdf
881 #define EXCLUDE_7TH_PKT 0xbf
882 #define EXCLUDE_8TH_PKT 0x7f
885 simple_ipv4_fwd_8pkts(struct rte_mbuf *m[8], uint16_t portid)
887 struct ether_hdr *eth_hdr[8];
888 struct ipv4_hdr *ipv4_hdr[8];
889 uint16_t dst_port[8];
891 union ipv4_5tuple_host key[8];
894 eth_hdr[0] = rte_pktmbuf_mtod(m[0], struct ether_hdr *);
895 eth_hdr[1] = rte_pktmbuf_mtod(m[1], struct ether_hdr *);
896 eth_hdr[2] = rte_pktmbuf_mtod(m[2], struct ether_hdr *);
897 eth_hdr[3] = rte_pktmbuf_mtod(m[3], struct ether_hdr *);
898 eth_hdr[4] = rte_pktmbuf_mtod(m[4], struct ether_hdr *);
899 eth_hdr[5] = rte_pktmbuf_mtod(m[5], struct ether_hdr *);
900 eth_hdr[6] = rte_pktmbuf_mtod(m[6], struct ether_hdr *);
901 eth_hdr[7] = rte_pktmbuf_mtod(m[7], struct ether_hdr *);
903 /* Handle IPv4 headers.*/
904 ipv4_hdr[0] = rte_pktmbuf_mtod_offset(m[0], struct ipv4_hdr *,
905 sizeof(struct ether_hdr));
906 ipv4_hdr[1] = rte_pktmbuf_mtod_offset(m[1], struct ipv4_hdr *,
907 sizeof(struct ether_hdr));
908 ipv4_hdr[2] = rte_pktmbuf_mtod_offset(m[2], struct ipv4_hdr *,
909 sizeof(struct ether_hdr));
910 ipv4_hdr[3] = rte_pktmbuf_mtod_offset(m[3], struct ipv4_hdr *,
911 sizeof(struct ether_hdr));
912 ipv4_hdr[4] = rte_pktmbuf_mtod_offset(m[4], struct ipv4_hdr *,
913 sizeof(struct ether_hdr));
914 ipv4_hdr[5] = rte_pktmbuf_mtod_offset(m[5], struct ipv4_hdr *,
915 sizeof(struct ether_hdr));
916 ipv4_hdr[6] = rte_pktmbuf_mtod_offset(m[6], struct ipv4_hdr *,
917 sizeof(struct ether_hdr));
918 ipv4_hdr[7] = rte_pktmbuf_mtod_offset(m[7], struct ipv4_hdr *,
919 sizeof(struct ether_hdr));
921 #ifdef DO_RFC_1812_CHECKS
922 /* Check to make sure the packet is valid (RFC1812) */
923 uint8_t valid_mask = MASK_ALL_PKTS;
925 if (is_valid_ipv4_pkt(ipv4_hdr[0], m[0]->pkt_len) < 0) {
926 rte_pktmbuf_free(m[0]);
927 valid_mask &= EXCLUDE_1ST_PKT;
929 if (is_valid_ipv4_pkt(ipv4_hdr[1], m[1]->pkt_len) < 0) {
930 rte_pktmbuf_free(m[1]);
931 valid_mask &= EXCLUDE_2ND_PKT;
933 if (is_valid_ipv4_pkt(ipv4_hdr[2], m[2]->pkt_len) < 0) {
934 rte_pktmbuf_free(m[2]);
935 valid_mask &= EXCLUDE_3RD_PKT;
937 if (is_valid_ipv4_pkt(ipv4_hdr[3], m[3]->pkt_len) < 0) {
938 rte_pktmbuf_free(m[3]);
939 valid_mask &= EXCLUDE_4TH_PKT;
941 if (is_valid_ipv4_pkt(ipv4_hdr[4], m[4]->pkt_len) < 0) {
942 rte_pktmbuf_free(m[4]);
943 valid_mask &= EXCLUDE_5TH_PKT;
945 if (is_valid_ipv4_pkt(ipv4_hdr[5], m[5]->pkt_len) < 0) {
946 rte_pktmbuf_free(m[5]);
947 valid_mask &= EXCLUDE_6TH_PKT;
949 if (is_valid_ipv4_pkt(ipv4_hdr[6], m[6]->pkt_len) < 0) {
950 rte_pktmbuf_free(m[6]);
951 valid_mask &= EXCLUDE_7TH_PKT;
953 if (is_valid_ipv4_pkt(ipv4_hdr[7], m[7]->pkt_len) < 0) {
954 rte_pktmbuf_free(m[7]);
955 valid_mask &= EXCLUDE_8TH_PKT;
957 if (unlikely(valid_mask != MASK_ALL_PKTS)) {
963 for (i = 0; i < 8; i++)
964 if ((0x1 << i) & valid_mask)
965 l3fwd_simple_forward(m[i], portid);
967 #endif /* End of #ifdef DO_RFC_1812_CHECKS */
969 data[0] = _mm_loadu_si128(rte_pktmbuf_mtod_offset(m[0], __m128i *,
970 sizeof(struct ether_hdr) +
971 offsetof(struct ipv4_hdr, time_to_live)));
972 data[1] = _mm_loadu_si128(rte_pktmbuf_mtod_offset(m[1], __m128i *,
973 sizeof(struct ether_hdr) +
974 offsetof(struct ipv4_hdr, time_to_live)));
975 data[2] = _mm_loadu_si128(rte_pktmbuf_mtod_offset(m[2], __m128i *,
976 sizeof(struct ether_hdr) +
977 offsetof(struct ipv4_hdr, time_to_live)));
978 data[3] = _mm_loadu_si128(rte_pktmbuf_mtod_offset(m[3], __m128i *,
979 sizeof(struct ether_hdr) +
980 offsetof(struct ipv4_hdr, time_to_live)));
981 data[4] = _mm_loadu_si128(rte_pktmbuf_mtod_offset(m[4], __m128i *,
982 sizeof(struct ether_hdr) +
983 offsetof(struct ipv4_hdr, time_to_live)));
984 data[5] = _mm_loadu_si128(rte_pktmbuf_mtod_offset(m[5], __m128i *,
985 sizeof(struct ether_hdr) +
986 offsetof(struct ipv4_hdr, time_to_live)));
987 data[6] = _mm_loadu_si128(rte_pktmbuf_mtod_offset(m[6], __m128i *,
988 sizeof(struct ether_hdr) +
989 offsetof(struct ipv4_hdr, time_to_live)));
990 data[7] = _mm_loadu_si128(rte_pktmbuf_mtod_offset(m[7], __m128i *,
991 sizeof(struct ether_hdr) +
992 offsetof(struct ipv4_hdr, time_to_live)));
994 key[0].xmm = _mm_and_si128(data[0], mask0);
995 key[1].xmm = _mm_and_si128(data[1], mask0);
996 key[2].xmm = _mm_and_si128(data[2], mask0);
997 key[3].xmm = _mm_and_si128(data[3], mask0);
998 key[4].xmm = _mm_and_si128(data[4], mask0);
999 key[5].xmm = _mm_and_si128(data[5], mask0);
1000 key[6].xmm = _mm_and_si128(data[6], mask0);
1001 key[7].xmm = _mm_and_si128(data[7], mask0);
1003 const void *key_array[8] = {&key[0], &key[1], &key[2], &key[3],
1004 &key[4], &key[5], &key[6], &key[7]};
1006 rte_hash_lookup_bulk(RTE_PER_LCORE(lcore_conf)->ipv4_lookup_struct,
1007 &key_array[0], 8, ret);
1008 dst_port[0] = ((ret[0] < 0) ? portid : ipv4_l3fwd_out_if[ret[0]]);
1009 dst_port[1] = ((ret[1] < 0) ? portid : ipv4_l3fwd_out_if[ret[1]]);
1010 dst_port[2] = ((ret[2] < 0) ? portid : ipv4_l3fwd_out_if[ret[2]]);
1011 dst_port[3] = ((ret[3] < 0) ? portid : ipv4_l3fwd_out_if[ret[3]]);
1012 dst_port[4] = ((ret[4] < 0) ? portid : ipv4_l3fwd_out_if[ret[4]]);
1013 dst_port[5] = ((ret[5] < 0) ? portid : ipv4_l3fwd_out_if[ret[5]]);
1014 dst_port[6] = ((ret[6] < 0) ? portid : ipv4_l3fwd_out_if[ret[6]]);
1015 dst_port[7] = ((ret[7] < 0) ? portid : ipv4_l3fwd_out_if[ret[7]]);
1017 if (dst_port[0] >= RTE_MAX_ETHPORTS ||
1018 (enabled_port_mask & 1 << dst_port[0]) == 0)
1019 dst_port[0] = portid;
1020 if (dst_port[1] >= RTE_MAX_ETHPORTS ||
1021 (enabled_port_mask & 1 << dst_port[1]) == 0)
1022 dst_port[1] = portid;
1023 if (dst_port[2] >= RTE_MAX_ETHPORTS ||
1024 (enabled_port_mask & 1 << dst_port[2]) == 0)
1025 dst_port[2] = portid;
1026 if (dst_port[3] >= RTE_MAX_ETHPORTS ||
1027 (enabled_port_mask & 1 << dst_port[3]) == 0)
1028 dst_port[3] = portid;
1029 if (dst_port[4] >= RTE_MAX_ETHPORTS ||
1030 (enabled_port_mask & 1 << dst_port[4]) == 0)
1031 dst_port[4] = portid;
1032 if (dst_port[5] >= RTE_MAX_ETHPORTS ||
1033 (enabled_port_mask & 1 << dst_port[5]) == 0)
1034 dst_port[5] = portid;
1035 if (dst_port[6] >= RTE_MAX_ETHPORTS ||
1036 (enabled_port_mask & 1 << dst_port[6]) == 0)
1037 dst_port[6] = portid;
1038 if (dst_port[7] >= RTE_MAX_ETHPORTS ||
1039 (enabled_port_mask & 1 << dst_port[7]) == 0)
1040 dst_port[7] = portid;
1042 #ifdef DO_RFC_1812_CHECKS
1043 /* Update time to live and header checksum */
1044 --(ipv4_hdr[0]->time_to_live);
1045 --(ipv4_hdr[1]->time_to_live);
1046 --(ipv4_hdr[2]->time_to_live);
1047 --(ipv4_hdr[3]->time_to_live);
1048 ++(ipv4_hdr[0]->hdr_checksum);
1049 ++(ipv4_hdr[1]->hdr_checksum);
1050 ++(ipv4_hdr[2]->hdr_checksum);
1051 ++(ipv4_hdr[3]->hdr_checksum);
1052 --(ipv4_hdr[4]->time_to_live);
1053 --(ipv4_hdr[5]->time_to_live);
1054 --(ipv4_hdr[6]->time_to_live);
1055 --(ipv4_hdr[7]->time_to_live);
1056 ++(ipv4_hdr[4]->hdr_checksum);
1057 ++(ipv4_hdr[5]->hdr_checksum);
1058 ++(ipv4_hdr[6]->hdr_checksum);
1059 ++(ipv4_hdr[7]->hdr_checksum);
1063 *(uint64_t *)ð_hdr[0]->d_addr = dest_eth_addr[dst_port[0]];
1064 *(uint64_t *)ð_hdr[1]->d_addr = dest_eth_addr[dst_port[1]];
1065 *(uint64_t *)ð_hdr[2]->d_addr = dest_eth_addr[dst_port[2]];
1066 *(uint64_t *)ð_hdr[3]->d_addr = dest_eth_addr[dst_port[3]];
1067 *(uint64_t *)ð_hdr[4]->d_addr = dest_eth_addr[dst_port[4]];
1068 *(uint64_t *)ð_hdr[5]->d_addr = dest_eth_addr[dst_port[5]];
1069 *(uint64_t *)ð_hdr[6]->d_addr = dest_eth_addr[dst_port[6]];
1070 *(uint64_t *)ð_hdr[7]->d_addr = dest_eth_addr[dst_port[7]];
1073 ether_addr_copy(&ports_eth_addr[dst_port[0]], ð_hdr[0]->s_addr);
1074 ether_addr_copy(&ports_eth_addr[dst_port[1]], ð_hdr[1]->s_addr);
1075 ether_addr_copy(&ports_eth_addr[dst_port[2]], ð_hdr[2]->s_addr);
1076 ether_addr_copy(&ports_eth_addr[dst_port[3]], ð_hdr[3]->s_addr);
1077 ether_addr_copy(&ports_eth_addr[dst_port[4]], ð_hdr[4]->s_addr);
1078 ether_addr_copy(&ports_eth_addr[dst_port[5]], ð_hdr[5]->s_addr);
1079 ether_addr_copy(&ports_eth_addr[dst_port[6]], ð_hdr[6]->s_addr);
1080 ether_addr_copy(&ports_eth_addr[dst_port[7]], ð_hdr[7]->s_addr);
1082 send_single_packet(m[0], (uint8_t)dst_port[0]);
1083 send_single_packet(m[1], (uint8_t)dst_port[1]);
1084 send_single_packet(m[2], (uint8_t)dst_port[2]);
1085 send_single_packet(m[3], (uint8_t)dst_port[3]);
1086 send_single_packet(m[4], (uint8_t)dst_port[4]);
1087 send_single_packet(m[5], (uint8_t)dst_port[5]);
1088 send_single_packet(m[6], (uint8_t)dst_port[6]);
1089 send_single_packet(m[7], (uint8_t)dst_port[7]);
1093 static inline void get_ipv6_5tuple(struct rte_mbuf *m0, __m128i mask0,
1094 __m128i mask1, union ipv6_5tuple_host *key)
1096 __m128i tmpdata0 = _mm_loadu_si128(rte_pktmbuf_mtod_offset(m0,
1097 __m128i *, sizeof(struct ether_hdr) +
1098 offsetof(struct ipv6_hdr, payload_len)));
1099 __m128i tmpdata1 = _mm_loadu_si128(rte_pktmbuf_mtod_offset(m0,
1100 __m128i *, sizeof(struct ether_hdr) +
1101 offsetof(struct ipv6_hdr, payload_len) + sizeof(__m128i)));
1102 __m128i tmpdata2 = _mm_loadu_si128(rte_pktmbuf_mtod_offset(m0,
1103 __m128i *, sizeof(struct ether_hdr) +
1104 offsetof(struct ipv6_hdr, payload_len) + sizeof(__m128i) +
1106 key->xmm[0] = _mm_and_si128(tmpdata0, mask0);
1107 key->xmm[1] = tmpdata1;
1108 key->xmm[2] = _mm_and_si128(tmpdata2, mask1);
1112 simple_ipv6_fwd_8pkts(struct rte_mbuf *m[8], uint16_t portid)
1115 uint16_t dst_port[8];
1116 struct ether_hdr *eth_hdr[8];
1117 union ipv6_5tuple_host key[8];
1119 __attribute__((unused)) struct ipv6_hdr *ipv6_hdr[8];
1121 eth_hdr[0] = rte_pktmbuf_mtod(m[0], struct ether_hdr *);
1122 eth_hdr[1] = rte_pktmbuf_mtod(m[1], struct ether_hdr *);
1123 eth_hdr[2] = rte_pktmbuf_mtod(m[2], struct ether_hdr *);
1124 eth_hdr[3] = rte_pktmbuf_mtod(m[3], struct ether_hdr *);
1125 eth_hdr[4] = rte_pktmbuf_mtod(m[4], struct ether_hdr *);
1126 eth_hdr[5] = rte_pktmbuf_mtod(m[5], struct ether_hdr *);
1127 eth_hdr[6] = rte_pktmbuf_mtod(m[6], struct ether_hdr *);
1128 eth_hdr[7] = rte_pktmbuf_mtod(m[7], struct ether_hdr *);
1130 /* Handle IPv6 headers.*/
1131 ipv6_hdr[0] = rte_pktmbuf_mtod_offset(m[0], struct ipv6_hdr *,
1132 sizeof(struct ether_hdr));
1133 ipv6_hdr[1] = rte_pktmbuf_mtod_offset(m[1], struct ipv6_hdr *,
1134 sizeof(struct ether_hdr));
1135 ipv6_hdr[2] = rte_pktmbuf_mtod_offset(m[2], struct ipv6_hdr *,
1136 sizeof(struct ether_hdr));
1137 ipv6_hdr[3] = rte_pktmbuf_mtod_offset(m[3], struct ipv6_hdr *,
1138 sizeof(struct ether_hdr));
1139 ipv6_hdr[4] = rte_pktmbuf_mtod_offset(m[4], struct ipv6_hdr *,
1140 sizeof(struct ether_hdr));
1141 ipv6_hdr[5] = rte_pktmbuf_mtod_offset(m[5], struct ipv6_hdr *,
1142 sizeof(struct ether_hdr));
1143 ipv6_hdr[6] = rte_pktmbuf_mtod_offset(m[6], struct ipv6_hdr *,
1144 sizeof(struct ether_hdr));
1145 ipv6_hdr[7] = rte_pktmbuf_mtod_offset(m[7], struct ipv6_hdr *,
1146 sizeof(struct ether_hdr));
1148 get_ipv6_5tuple(m[0], mask1, mask2, &key[0]);
1149 get_ipv6_5tuple(m[1], mask1, mask2, &key[1]);
1150 get_ipv6_5tuple(m[2], mask1, mask2, &key[2]);
1151 get_ipv6_5tuple(m[3], mask1, mask2, &key[3]);
1152 get_ipv6_5tuple(m[4], mask1, mask2, &key[4]);
1153 get_ipv6_5tuple(m[5], mask1, mask2, &key[5]);
1154 get_ipv6_5tuple(m[6], mask1, mask2, &key[6]);
1155 get_ipv6_5tuple(m[7], mask1, mask2, &key[7]);
1157 const void *key_array[8] = {&key[0], &key[1], &key[2], &key[3],
1158 &key[4], &key[5], &key[6], &key[7]};
1160 rte_hash_lookup_bulk(RTE_PER_LCORE(lcore_conf)->ipv6_lookup_struct,
1161 &key_array[0], 4, ret);
1162 dst_port[0] = ((ret[0] < 0) ? portid : ipv6_l3fwd_out_if[ret[0]]);
1163 dst_port[1] = ((ret[1] < 0) ? portid : ipv6_l3fwd_out_if[ret[1]]);
1164 dst_port[2] = ((ret[2] < 0) ? portid : ipv6_l3fwd_out_if[ret[2]]);
1165 dst_port[3] = ((ret[3] < 0) ? portid : ipv6_l3fwd_out_if[ret[3]]);
1166 dst_port[4] = ((ret[4] < 0) ? portid : ipv6_l3fwd_out_if[ret[4]]);
1167 dst_port[5] = ((ret[5] < 0) ? portid : ipv6_l3fwd_out_if[ret[5]]);
1168 dst_port[6] = ((ret[6] < 0) ? portid : ipv6_l3fwd_out_if[ret[6]]);
1169 dst_port[7] = ((ret[7] < 0) ? portid : ipv6_l3fwd_out_if[ret[7]]);
1171 if (dst_port[0] >= RTE_MAX_ETHPORTS ||
1172 (enabled_port_mask & 1 << dst_port[0]) == 0)
1173 dst_port[0] = portid;
1174 if (dst_port[1] >= RTE_MAX_ETHPORTS ||
1175 (enabled_port_mask & 1 << dst_port[1]) == 0)
1176 dst_port[1] = portid;
1177 if (dst_port[2] >= RTE_MAX_ETHPORTS ||
1178 (enabled_port_mask & 1 << dst_port[2]) == 0)
1179 dst_port[2] = portid;
1180 if (dst_port[3] >= RTE_MAX_ETHPORTS ||
1181 (enabled_port_mask & 1 << dst_port[3]) == 0)
1182 dst_port[3] = portid;
1183 if (dst_port[4] >= RTE_MAX_ETHPORTS ||
1184 (enabled_port_mask & 1 << dst_port[4]) == 0)
1185 dst_port[4] = portid;
1186 if (dst_port[5] >= RTE_MAX_ETHPORTS ||
1187 (enabled_port_mask & 1 << dst_port[5]) == 0)
1188 dst_port[5] = portid;
1189 if (dst_port[6] >= RTE_MAX_ETHPORTS ||
1190 (enabled_port_mask & 1 << dst_port[6]) == 0)
1191 dst_port[6] = portid;
1192 if (dst_port[7] >= RTE_MAX_ETHPORTS ||
1193 (enabled_port_mask & 1 << dst_port[7]) == 0)
1194 dst_port[7] = portid;
1197 *(uint64_t *)ð_hdr[0]->d_addr = dest_eth_addr[dst_port[0]];
1198 *(uint64_t *)ð_hdr[1]->d_addr = dest_eth_addr[dst_port[1]];
1199 *(uint64_t *)ð_hdr[2]->d_addr = dest_eth_addr[dst_port[2]];
1200 *(uint64_t *)ð_hdr[3]->d_addr = dest_eth_addr[dst_port[3]];
1201 *(uint64_t *)ð_hdr[4]->d_addr = dest_eth_addr[dst_port[4]];
1202 *(uint64_t *)ð_hdr[5]->d_addr = dest_eth_addr[dst_port[5]];
1203 *(uint64_t *)ð_hdr[6]->d_addr = dest_eth_addr[dst_port[6]];
1204 *(uint64_t *)ð_hdr[7]->d_addr = dest_eth_addr[dst_port[7]];
1207 ether_addr_copy(&ports_eth_addr[dst_port[0]], ð_hdr[0]->s_addr);
1208 ether_addr_copy(&ports_eth_addr[dst_port[1]], ð_hdr[1]->s_addr);
1209 ether_addr_copy(&ports_eth_addr[dst_port[2]], ð_hdr[2]->s_addr);
1210 ether_addr_copy(&ports_eth_addr[dst_port[3]], ð_hdr[3]->s_addr);
1211 ether_addr_copy(&ports_eth_addr[dst_port[4]], ð_hdr[4]->s_addr);
1212 ether_addr_copy(&ports_eth_addr[dst_port[5]], ð_hdr[5]->s_addr);
1213 ether_addr_copy(&ports_eth_addr[dst_port[6]], ð_hdr[6]->s_addr);
1214 ether_addr_copy(&ports_eth_addr[dst_port[7]], ð_hdr[7]->s_addr);
1216 send_single_packet(m[0], dst_port[0]);
1217 send_single_packet(m[1], dst_port[1]);
1218 send_single_packet(m[2], dst_port[2]);
1219 send_single_packet(m[3], dst_port[3]);
1220 send_single_packet(m[4], dst_port[4]);
1221 send_single_packet(m[5], dst_port[5]);
1222 send_single_packet(m[6], dst_port[6]);
1223 send_single_packet(m[7], dst_port[7]);
1226 #endif /* APP_LOOKUP_METHOD */
1228 static __rte_always_inline void
1229 l3fwd_simple_forward(struct rte_mbuf *m, uint16_t portid)
1231 struct ether_hdr *eth_hdr;
1232 struct ipv4_hdr *ipv4_hdr;
1235 eth_hdr = rte_pktmbuf_mtod(m, struct ether_hdr *);
1237 if (RTE_ETH_IS_IPV4_HDR(m->packet_type)) {
1238 /* Handle IPv4 headers.*/
1239 ipv4_hdr = rte_pktmbuf_mtod_offset(m, struct ipv4_hdr *,
1240 sizeof(struct ether_hdr));
1242 #ifdef DO_RFC_1812_CHECKS
1243 /* Check to make sure the packet is valid (RFC1812) */
1244 if (is_valid_ipv4_pkt(ipv4_hdr, m->pkt_len) < 0) {
1245 rte_pktmbuf_free(m);
1250 dst_port = get_ipv4_dst_port(ipv4_hdr, portid,
1251 RTE_PER_LCORE(lcore_conf)->ipv4_lookup_struct);
1252 if (dst_port >= RTE_MAX_ETHPORTS ||
1253 (enabled_port_mask & 1 << dst_port) == 0)
1256 #ifdef DO_RFC_1812_CHECKS
1257 /* Update time to live and header checksum */
1258 --(ipv4_hdr->time_to_live);
1259 ++(ipv4_hdr->hdr_checksum);
1262 *(uint64_t *)ð_hdr->d_addr = dest_eth_addr[dst_port];
1265 ether_addr_copy(&ports_eth_addr[dst_port], ð_hdr->s_addr);
1267 send_single_packet(m, dst_port);
1268 } else if (RTE_ETH_IS_IPV6_HDR(m->packet_type)) {
1269 /* Handle IPv6 headers.*/
1270 struct ipv6_hdr *ipv6_hdr;
1272 ipv6_hdr = rte_pktmbuf_mtod_offset(m, struct ipv6_hdr *,
1273 sizeof(struct ether_hdr));
1275 dst_port = get_ipv6_dst_port(ipv6_hdr, portid,
1276 RTE_PER_LCORE(lcore_conf)->ipv6_lookup_struct);
1278 if (dst_port >= RTE_MAX_ETHPORTS ||
1279 (enabled_port_mask & 1 << dst_port) == 0)
1283 *(uint64_t *)ð_hdr->d_addr = dest_eth_addr[dst_port];
1286 ether_addr_copy(&ports_eth_addr[dst_port], ð_hdr->s_addr);
1288 send_single_packet(m, dst_port);
1290 /* Free the mbuf that contains non-IPV4/IPV6 packet */
1291 rte_pktmbuf_free(m);
1294 #if ((APP_LOOKUP_METHOD == APP_LOOKUP_LPM) && \
1295 (ENABLE_MULTI_BUFFER_OPTIMIZE == 1))
1296 #ifdef DO_RFC_1812_CHECKS
1298 #define IPV4_MIN_VER_IHL 0x45
1299 #define IPV4_MAX_VER_IHL 0x4f
1300 #define IPV4_MAX_VER_IHL_DIFF (IPV4_MAX_VER_IHL - IPV4_MIN_VER_IHL)
1302 /* Minimum value of IPV4 total length (20B) in network byte order. */
1303 #define IPV4_MIN_LEN_BE (sizeof(struct ipv4_hdr) << 8)
1306 * From http://www.rfc-editor.org/rfc/rfc1812.txt section 5.2.2:
1307 * - The IP version number must be 4.
1308 * - The IP header length field must be large enough to hold the
1309 * minimum length legal IP datagram (20 bytes = 5 words).
1310 * - The IP total length field must be large enough to hold the IP
1311 * datagram header, whose length is specified in the IP header length
1313 * If we encounter invalid IPV4 packet, then set destination port for it
1314 * to BAD_PORT value.
1316 static __rte_always_inline void
1317 rfc1812_process(struct ipv4_hdr *ipv4_hdr, uint16_t *dp, uint32_t ptype)
1321 if (RTE_ETH_IS_IPV4_HDR(ptype)) {
1322 ihl = ipv4_hdr->version_ihl - IPV4_MIN_VER_IHL;
1324 ipv4_hdr->time_to_live--;
1325 ipv4_hdr->hdr_checksum++;
1327 if (ihl > IPV4_MAX_VER_IHL_DIFF ||
1328 ((uint8_t)ipv4_hdr->total_length == 0 &&
1329 ipv4_hdr->total_length < IPV4_MIN_LEN_BE)) {
1336 #define rfc1812_process(mb, dp, ptype) do { } while (0)
1337 #endif /* DO_RFC_1812_CHECKS */
1338 #endif /* APP_LOOKUP_LPM && ENABLE_MULTI_BUFFER_OPTIMIZE */
1341 #if ((APP_LOOKUP_METHOD == APP_LOOKUP_LPM) && \
1342 (ENABLE_MULTI_BUFFER_OPTIMIZE == 1))
1344 static __rte_always_inline uint16_t
1345 get_dst_port(struct rte_mbuf *pkt, uint32_t dst_ipv4, uint16_t portid)
1348 struct ipv6_hdr *ipv6_hdr;
1349 struct ether_hdr *eth_hdr;
1351 if (RTE_ETH_IS_IPV4_HDR(pkt->packet_type)) {
1352 return (uint16_t) ((rte_lpm_lookup(
1353 RTE_PER_LCORE(lcore_conf)->ipv4_lookup_struct, dst_ipv4,
1354 &next_hop) == 0) ? next_hop : portid);
1356 } else if (RTE_ETH_IS_IPV6_HDR(pkt->packet_type)) {
1358 eth_hdr = rte_pktmbuf_mtod(pkt, struct ether_hdr *);
1359 ipv6_hdr = (struct ipv6_hdr *)(eth_hdr + 1);
1361 return (uint16_t) ((rte_lpm6_lookup(
1362 RTE_PER_LCORE(lcore_conf)->ipv6_lookup_struct,
1363 ipv6_hdr->dst_addr, &next_hop) == 0) ?
1372 process_packet(struct rte_mbuf *pkt, uint16_t *dst_port, uint16_t portid)
1374 struct ether_hdr *eth_hdr;
1375 struct ipv4_hdr *ipv4_hdr;
1380 eth_hdr = rte_pktmbuf_mtod(pkt, struct ether_hdr *);
1381 ipv4_hdr = (struct ipv4_hdr *)(eth_hdr + 1);
1383 dst_ipv4 = ipv4_hdr->dst_addr;
1384 dst_ipv4 = rte_be_to_cpu_32(dst_ipv4);
1385 dp = get_dst_port(pkt, dst_ipv4, portid);
1387 te = _mm_load_si128((__m128i *)eth_hdr);
1391 rfc1812_process(ipv4_hdr, dst_port, pkt->packet_type);
1393 te = _mm_blend_epi16(te, ve, MASK_ETH);
1394 _mm_store_si128((__m128i *)eth_hdr, te);
1398 * Read packet_type and destination IPV4 addresses from 4 mbufs.
1401 processx4_step1(struct rte_mbuf *pkt[FWDSTEP],
1403 uint32_t *ipv4_flag)
1405 struct ipv4_hdr *ipv4_hdr;
1406 struct ether_hdr *eth_hdr;
1407 uint32_t x0, x1, x2, x3;
1409 eth_hdr = rte_pktmbuf_mtod(pkt[0], struct ether_hdr *);
1410 ipv4_hdr = (struct ipv4_hdr *)(eth_hdr + 1);
1411 x0 = ipv4_hdr->dst_addr;
1412 ipv4_flag[0] = pkt[0]->packet_type & RTE_PTYPE_L3_IPV4;
1414 eth_hdr = rte_pktmbuf_mtod(pkt[1], struct ether_hdr *);
1415 ipv4_hdr = (struct ipv4_hdr *)(eth_hdr + 1);
1416 x1 = ipv4_hdr->dst_addr;
1417 ipv4_flag[0] &= pkt[1]->packet_type;
1419 eth_hdr = rte_pktmbuf_mtod(pkt[2], struct ether_hdr *);
1420 ipv4_hdr = (struct ipv4_hdr *)(eth_hdr + 1);
1421 x2 = ipv4_hdr->dst_addr;
1422 ipv4_flag[0] &= pkt[2]->packet_type;
1424 eth_hdr = rte_pktmbuf_mtod(pkt[3], struct ether_hdr *);
1425 ipv4_hdr = (struct ipv4_hdr *)(eth_hdr + 1);
1426 x3 = ipv4_hdr->dst_addr;
1427 ipv4_flag[0] &= pkt[3]->packet_type;
1429 dip[0] = _mm_set_epi32(x3, x2, x1, x0);
1433 * Lookup into LPM for destination port.
1434 * If lookup fails, use incoming port (portid) as destination port.
1437 processx4_step2(__m128i dip,
1440 struct rte_mbuf *pkt[FWDSTEP],
1441 uint16_t dprt[FWDSTEP])
1444 const __m128i bswap_mask = _mm_set_epi8(12, 13, 14, 15, 8, 9, 10, 11,
1445 4, 5, 6, 7, 0, 1, 2, 3);
1447 /* Byte swap 4 IPV4 addresses. */
1448 dip = _mm_shuffle_epi8(dip, bswap_mask);
1450 /* if all 4 packets are IPV4. */
1451 if (likely(ipv4_flag)) {
1452 rte_lpm_lookupx4(RTE_PER_LCORE(lcore_conf)->ipv4_lookup_struct, dip,
1455 /* get rid of unused upper 16 bit for each dport. */
1456 dst.x = _mm_packs_epi32(dst.x, dst.x);
1457 *(uint64_t *)dprt = dst.u64[0];
1460 dprt[0] = get_dst_port(pkt[0], dst.u32[0], portid);
1461 dprt[1] = get_dst_port(pkt[1], dst.u32[1], portid);
1462 dprt[2] = get_dst_port(pkt[2], dst.u32[2], portid);
1463 dprt[3] = get_dst_port(pkt[3], dst.u32[3], portid);
1468 * Update source and destination MAC addresses in the ethernet header.
1469 * Perform RFC1812 checks and updates for IPV4 packets.
1472 processx4_step3(struct rte_mbuf *pkt[FWDSTEP], uint16_t dst_port[FWDSTEP])
1474 __m128i te[FWDSTEP];
1475 __m128i ve[FWDSTEP];
1476 __m128i *p[FWDSTEP];
1478 p[0] = rte_pktmbuf_mtod(pkt[0], __m128i *);
1479 p[1] = rte_pktmbuf_mtod(pkt[1], __m128i *);
1480 p[2] = rte_pktmbuf_mtod(pkt[2], __m128i *);
1481 p[3] = rte_pktmbuf_mtod(pkt[3], __m128i *);
1483 ve[0] = val_eth[dst_port[0]];
1484 te[0] = _mm_load_si128(p[0]);
1486 ve[1] = val_eth[dst_port[1]];
1487 te[1] = _mm_load_si128(p[1]);
1489 ve[2] = val_eth[dst_port[2]];
1490 te[2] = _mm_load_si128(p[2]);
1492 ve[3] = val_eth[dst_port[3]];
1493 te[3] = _mm_load_si128(p[3]);
1495 /* Update first 12 bytes, keep rest bytes intact. */
1496 te[0] = _mm_blend_epi16(te[0], ve[0], MASK_ETH);
1497 te[1] = _mm_blend_epi16(te[1], ve[1], MASK_ETH);
1498 te[2] = _mm_blend_epi16(te[2], ve[2], MASK_ETH);
1499 te[3] = _mm_blend_epi16(te[3], ve[3], MASK_ETH);
1501 _mm_store_si128(p[0], te[0]);
1502 _mm_store_si128(p[1], te[1]);
1503 _mm_store_si128(p[2], te[2]);
1504 _mm_store_si128(p[3], te[3]);
1506 rfc1812_process((struct ipv4_hdr *)((struct ether_hdr *)p[0] + 1),
1507 &dst_port[0], pkt[0]->packet_type);
1508 rfc1812_process((struct ipv4_hdr *)((struct ether_hdr *)p[1] + 1),
1509 &dst_port[1], pkt[1]->packet_type);
1510 rfc1812_process((struct ipv4_hdr *)((struct ether_hdr *)p[2] + 1),
1511 &dst_port[2], pkt[2]->packet_type);
1512 rfc1812_process((struct ipv4_hdr *)((struct ether_hdr *)p[3] + 1),
1513 &dst_port[3], pkt[3]->packet_type);
1517 * We group consecutive packets with the same destionation port into one burst.
1518 * To avoid extra latency this is done together with some other packet
1519 * processing, but after we made a final decision about packet's destination.
1520 * To do this we maintain:
1521 * pnum - array of number of consecutive packets with the same dest port for
1522 * each packet in the input burst.
1523 * lp - pointer to the last updated element in the pnum.
1524 * dlp - dest port value lp corresponds to.
1527 #define GRPSZ (1 << FWDSTEP)
1528 #define GRPMSK (GRPSZ - 1)
1530 #define GROUP_PORT_STEP(dlp, dcp, lp, pn, idx) do { \
1531 if (likely((dlp) == (dcp)[(idx)])) { \
1534 (dlp) = (dcp)[idx]; \
1535 (lp) = (pn) + (idx); \
1541 * Group consecutive packets with the same destination port in bursts of 4.
1542 * Suppose we have array of destionation ports:
1543 * dst_port[] = {a, b, c, d,, e, ... }
1544 * dp1 should contain: <a, b, c, d>, dp2: <b, c, d, e>.
1545 * We doing 4 comparisons at once and the result is 4 bit mask.
1546 * This mask is used as an index into prebuild array of pnum values.
1548 static inline uint16_t *
1549 port_groupx4(uint16_t pn[FWDSTEP + 1], uint16_t *lp, __m128i dp1, __m128i dp2)
1551 static const struct {
1552 uint64_t pnum; /* prebuild 4 values for pnum[]. */
1553 int32_t idx; /* index for new last updated elemnet. */
1554 uint16_t lpv; /* add value to the last updated element. */
1557 /* 0: a != b, b != c, c != d, d != e */
1558 .pnum = UINT64_C(0x0001000100010001),
1563 /* 1: a == b, b != c, c != d, d != e */
1564 .pnum = UINT64_C(0x0001000100010002),
1569 /* 2: a != b, b == c, c != d, d != e */
1570 .pnum = UINT64_C(0x0001000100020001),
1575 /* 3: a == b, b == c, c != d, d != e */
1576 .pnum = UINT64_C(0x0001000100020003),
1581 /* 4: a != b, b != c, c == d, d != e */
1582 .pnum = UINT64_C(0x0001000200010001),
1587 /* 5: a == b, b != c, c == d, d != e */
1588 .pnum = UINT64_C(0x0001000200010002),
1593 /* 6: a != b, b == c, c == d, d != e */
1594 .pnum = UINT64_C(0x0001000200030001),
1599 /* 7: a == b, b == c, c == d, d != e */
1600 .pnum = UINT64_C(0x0001000200030004),
1605 /* 8: a != b, b != c, c != d, d == e */
1606 .pnum = UINT64_C(0x0002000100010001),
1611 /* 9: a == b, b != c, c != d, d == e */
1612 .pnum = UINT64_C(0x0002000100010002),
1617 /* 0xa: a != b, b == c, c != d, d == e */
1618 .pnum = UINT64_C(0x0002000100020001),
1623 /* 0xb: a == b, b == c, c != d, d == e */
1624 .pnum = UINT64_C(0x0002000100020003),
1629 /* 0xc: a != b, b != c, c == d, d == e */
1630 .pnum = UINT64_C(0x0002000300010001),
1635 /* 0xd: a == b, b != c, c == d, d == e */
1636 .pnum = UINT64_C(0x0002000300010002),
1641 /* 0xe: a != b, b == c, c == d, d == e */
1642 .pnum = UINT64_C(0x0002000300040001),
1647 /* 0xf: a == b, b == c, c == d, d == e */
1648 .pnum = UINT64_C(0x0002000300040005),
1655 uint16_t u16[FWDSTEP + 1];
1657 } *pnum = (void *)pn;
1661 dp1 = _mm_cmpeq_epi16(dp1, dp2);
1662 dp1 = _mm_unpacklo_epi16(dp1, dp1);
1663 v = _mm_movemask_ps((__m128)dp1);
1665 /* update last port counter. */
1666 lp[0] += gptbl[v].lpv;
1668 /* if dest port value has changed. */
1670 pnum->u64 = gptbl[v].pnum;
1671 pnum->u16[FWDSTEP] = 1;
1672 lp = pnum->u16 + gptbl[v].idx;
1678 #endif /* APP_LOOKUP_METHOD */
1681 process_burst(struct rte_mbuf *pkts_burst[MAX_PKT_BURST], int nb_rx,
1687 #if ((APP_LOOKUP_METHOD == APP_LOOKUP_LPM) && \
1688 (ENABLE_MULTI_BUFFER_OPTIMIZE == 1))
1692 uint16_t dst_port[MAX_PKT_BURST];
1693 __m128i dip[MAX_PKT_BURST / FWDSTEP];
1694 uint32_t ipv4_flag[MAX_PKT_BURST / FWDSTEP];
1695 uint16_t pnum[MAX_PKT_BURST + 1];
1699 #if (ENABLE_MULTI_BUFFER_OPTIMIZE == 1)
1700 #if (APP_LOOKUP_METHOD == APP_LOOKUP_EXACT_MATCH)
1703 * Send nb_rx - nb_rx%8 packets
1706 int32_t n = RTE_ALIGN_FLOOR(nb_rx, 8);
1708 for (j = 0; j < n; j += 8) {
1710 pkts_burst[j]->packet_type &
1711 pkts_burst[j+1]->packet_type &
1712 pkts_burst[j+2]->packet_type &
1713 pkts_burst[j+3]->packet_type &
1714 pkts_burst[j+4]->packet_type &
1715 pkts_burst[j+5]->packet_type &
1716 pkts_burst[j+6]->packet_type &
1717 pkts_burst[j+7]->packet_type;
1718 if (pkt_type & RTE_PTYPE_L3_IPV4) {
1719 simple_ipv4_fwd_8pkts(&pkts_burst[j], portid);
1720 } else if (pkt_type &
1721 RTE_PTYPE_L3_IPV6) {
1722 simple_ipv6_fwd_8pkts(&pkts_burst[j], portid);
1724 l3fwd_simple_forward(pkts_burst[j], portid);
1725 l3fwd_simple_forward(pkts_burst[j+1], portid);
1726 l3fwd_simple_forward(pkts_burst[j+2], portid);
1727 l3fwd_simple_forward(pkts_burst[j+3], portid);
1728 l3fwd_simple_forward(pkts_burst[j+4], portid);
1729 l3fwd_simple_forward(pkts_burst[j+5], portid);
1730 l3fwd_simple_forward(pkts_burst[j+6], portid);
1731 l3fwd_simple_forward(pkts_burst[j+7], portid);
1734 for (; j < nb_rx ; j++)
1735 l3fwd_simple_forward(pkts_burst[j], portid);
1737 #elif (APP_LOOKUP_METHOD == APP_LOOKUP_LPM)
1739 k = RTE_ALIGN_FLOOR(nb_rx, FWDSTEP);
1740 for (j = 0; j != k; j += FWDSTEP)
1741 processx4_step1(&pkts_burst[j], &dip[j / FWDSTEP],
1742 &ipv4_flag[j / FWDSTEP]);
1744 k = RTE_ALIGN_FLOOR(nb_rx, FWDSTEP);
1745 for (j = 0; j != k; j += FWDSTEP)
1746 processx4_step2(dip[j / FWDSTEP], ipv4_flag[j / FWDSTEP],
1747 portid, &pkts_burst[j], &dst_port[j]);
1750 * Finish packet processing and group consecutive
1751 * packets with the same destination port.
1753 k = RTE_ALIGN_FLOOR(nb_rx, FWDSTEP);
1760 processx4_step3(pkts_burst, dst_port);
1762 /* dp1: <d[0], d[1], d[2], d[3], ... > */
1763 dp1 = _mm_loadu_si128((__m128i *)dst_port);
1765 for (j = FWDSTEP; j != k; j += FWDSTEP) {
1766 processx4_step3(&pkts_burst[j], &dst_port[j]);
1770 * <d[j-3], d[j-2], d[j-1], d[j], ... >
1772 dp2 = _mm_loadu_si128(
1773 (__m128i *)&dst_port[j - FWDSTEP + 1]);
1774 lp = port_groupx4(&pnum[j - FWDSTEP], lp, dp1, dp2);
1778 * <d[j], d[j+1], d[j+2], d[j+3], ... >
1780 dp1 = _mm_srli_si128(dp2, (FWDSTEP - 1) *
1781 sizeof(dst_port[0]));
1785 * dp2: <d[j-3], d[j-2], d[j-1], d[j-1], ... >
1787 dp2 = _mm_shufflelo_epi16(dp1, 0xf9);
1788 lp = port_groupx4(&pnum[j - FWDSTEP], lp, dp1, dp2);
1791 * remove values added by the last repeated
1795 dlp = dst_port[j - 1];
1797 /* set dlp and lp to the never used values. */
1799 lp = pnum + MAX_PKT_BURST;
1802 /* Process up to last 3 packets one by one. */
1803 switch (nb_rx % FWDSTEP) {
1805 process_packet(pkts_burst[j], dst_port + j, portid);
1806 GROUP_PORT_STEP(dlp, dst_port, lp, pnum, j);
1810 process_packet(pkts_burst[j], dst_port + j, portid);
1811 GROUP_PORT_STEP(dlp, dst_port, lp, pnum, j);
1815 process_packet(pkts_burst[j], dst_port + j, portid);
1816 GROUP_PORT_STEP(dlp, dst_port, lp, pnum, j);
1821 * Send packets out, through destination port.
1822 * Consecuteve pacekts with the same destination port
1823 * are already grouped together.
1824 * If destination port for the packet equals BAD_PORT,
1825 * then free the packet without sending it out.
1827 for (j = 0; j < nb_rx; j += k) {
1835 if (likely(pn != BAD_PORT))
1836 send_packetsx4(pn, pkts_burst + j, k);
1838 for (m = j; m != j + k; m++)
1839 rte_pktmbuf_free(pkts_burst[m]);
1843 #endif /* APP_LOOKUP_METHOD */
1844 #else /* ENABLE_MULTI_BUFFER_OPTIMIZE == 0 */
1846 /* Prefetch first packets */
1847 for (j = 0; j < PREFETCH_OFFSET && j < nb_rx; j++)
1848 rte_prefetch0(rte_pktmbuf_mtod(pkts_burst[j], void *));
1850 /* Prefetch and forward already prefetched packets */
1851 for (j = 0; j < (nb_rx - PREFETCH_OFFSET); j++) {
1852 rte_prefetch0(rte_pktmbuf_mtod(pkts_burst[
1853 j + PREFETCH_OFFSET], void *));
1854 l3fwd_simple_forward(pkts_burst[j], portid);
1857 /* Forward remaining prefetched packets */
1858 for (; j < nb_rx; j++)
1859 l3fwd_simple_forward(pkts_burst[j], portid);
1861 #endif /* ENABLE_MULTI_BUFFER_OPTIMIZE */
1865 #if (APP_CPU_LOAD > 0)
1868 * CPU-load stats collector
1871 cpu_load_collector(__rte_unused void *arg) {
1874 uint64_t prev_tsc, diff_tsc, cur_tsc;
1875 uint64_t total[MAX_CPU] = { 0 };
1876 unsigned min_cpu = MAX_CPU;
1877 unsigned max_cpu = 0;
1882 unsigned int n_thread_per_cpu[MAX_CPU] = { 0 };
1883 struct thread_conf *thread_per_cpu[MAX_CPU][MAX_THREAD];
1885 struct thread_conf *thread_conf;
1887 const uint64_t interval_tsc = (rte_get_tsc_hz() + US_PER_S - 1) /
1888 US_PER_S * CPU_LOAD_TIMEOUT_US;
1892 * Wait for all threads
1895 printf("Waiting for %d rx threads and %d tx threads\n", n_rx_thread,
1898 while (rte_atomic16_read(&rx_counter) < n_rx_thread)
1901 while (rte_atomic16_read(&tx_counter) < n_tx_thread)
1904 for (i = 0; i < n_rx_thread; i++) {
1906 thread_conf = &rx_thread[i].conf;
1907 cpu_id = thread_conf->cpu_id;
1908 thread_per_cpu[cpu_id][n_thread_per_cpu[cpu_id]++] = thread_conf;
1910 if (cpu_id > max_cpu)
1912 if (cpu_id < min_cpu)
1915 for (i = 0; i < n_tx_thread; i++) {
1917 thread_conf = &tx_thread[i].conf;
1918 cpu_id = thread_conf->cpu_id;
1919 thread_per_cpu[cpu_id][n_thread_per_cpu[cpu_id]++] = thread_conf;
1921 if (thread_conf->cpu_id > max_cpu)
1922 max_cpu = thread_conf->cpu_id;
1923 if (thread_conf->cpu_id < min_cpu)
1924 min_cpu = thread_conf->cpu_id;
1930 for (i = min_cpu; i <= max_cpu; i++) {
1931 for (j = 0; j < MAX_CPU_COUNTER; j++) {
1932 for (k = 0; k < n_thread_per_cpu[i]; k++)
1933 if (thread_per_cpu[i][k]->busy[j]) {
1938 cpu_load.hits[j][i]++;
1950 cur_tsc = rte_rdtsc();
1952 diff_tsc = cur_tsc - prev_tsc;
1953 if (unlikely(diff_tsc > interval_tsc)) {
1957 printf("Cpu usage for %d rx threads and %d tx threads:\n\n",
1958 n_rx_thread, n_tx_thread);
1960 printf("cpu# proc%% poll%% overhead%%\n\n");
1962 for (i = min_cpu; i <= max_cpu; i++) {
1964 printf("CPU %d:", i);
1965 for (j = 0; j < MAX_CPU_COUNTER; j++) {
1966 printf("%7" PRIu64 "",
1967 cpu_load.hits[j][i] * 100 / cpu_load.counter);
1968 hits += cpu_load.hits[j][i];
1969 cpu_load.hits[j][i] = 0;
1971 printf("%7" PRIu64 "\n",
1972 100 - total[i] * 100 / cpu_load.counter);
1975 cpu_load.counter = 0;
1982 #endif /* APP_CPU_LOAD */
1985 * Null processing lthread loop
1987 * This loop is used to start empty scheduler on lcore.
1990 lthread_null(__rte_unused void *args)
1992 int lcore_id = rte_lcore_id();
1994 RTE_LOG(INFO, L3FWD, "Starting scheduler on lcore %d.\n", lcore_id);
1999 /* main processing loop */
2001 lthread_tx_per_ring(void *dummy)
2005 struct rte_ring *ring;
2006 struct thread_tx_conf *tx_conf;
2007 struct rte_mbuf *pkts_burst[MAX_PKT_BURST];
2008 struct lthread_cond *ready;
2010 tx_conf = (struct thread_tx_conf *)dummy;
2011 ring = tx_conf->ring;
2012 ready = *tx_conf->ready;
2014 lthread_set_data((void *)tx_conf);
2017 * Move this lthread to lcore
2019 lthread_set_affinity(tx_conf->conf.lcore_id);
2021 RTE_LOG(INFO, L3FWD, "entering main tx loop on lcore %u\n", rte_lcore_id());
2024 rte_atomic16_inc(&tx_counter);
2028 * Read packet from ring
2030 SET_CPU_BUSY(tx_conf, CPU_POLL);
2031 nb_rx = rte_ring_sc_dequeue_burst(ring, (void **)pkts_burst,
2032 MAX_PKT_BURST, NULL);
2033 SET_CPU_IDLE(tx_conf, CPU_POLL);
2036 SET_CPU_BUSY(tx_conf, CPU_PROCESS);
2037 portid = pkts_burst[0]->port;
2038 process_burst(pkts_burst, nb_rx, portid);
2039 SET_CPU_IDLE(tx_conf, CPU_PROCESS);
2042 lthread_cond_wait(ready, 0);
2049 * Main tx-lthreads spawner lthread.
2051 * This lthread is used to spawn one new lthread per ring from producers.
2055 lthread_tx(void *args)
2061 struct thread_tx_conf *tx_conf;
2063 tx_conf = (struct thread_tx_conf *)args;
2064 lthread_set_data((void *)tx_conf);
2067 * Move this lthread to the selected lcore
2069 lthread_set_affinity(tx_conf->conf.lcore_id);
2072 * Spawn tx readers (one per input ring)
2074 lthread_create(<, tx_conf->conf.lcore_id, lthread_tx_per_ring,
2077 lcore_id = rte_lcore_id();
2079 RTE_LOG(INFO, L3FWD, "Entering Tx main loop on lcore %u\n", lcore_id);
2081 tx_conf->conf.cpu_id = sched_getcpu();
2084 lthread_sleep(BURST_TX_DRAIN_US * 1000);
2087 * TX burst queue drain
2089 for (portid = 0; portid < RTE_MAX_ETHPORTS; portid++) {
2090 if (tx_conf->tx_mbufs[portid].len == 0)
2092 SET_CPU_BUSY(tx_conf, CPU_PROCESS);
2093 send_burst(tx_conf, tx_conf->tx_mbufs[portid].len, portid);
2094 SET_CPU_IDLE(tx_conf, CPU_PROCESS);
2095 tx_conf->tx_mbufs[portid].len = 0;
2103 lthread_rx(void *dummy)
2111 int len[RTE_MAX_LCORE] = { 0 };
2112 int old_len, new_len;
2113 struct rte_mbuf *pkts_burst[MAX_PKT_BURST];
2114 struct thread_rx_conf *rx_conf;
2116 rx_conf = (struct thread_rx_conf *)dummy;
2117 lthread_set_data((void *)rx_conf);
2120 * Move this lthread to lcore
2122 lthread_set_affinity(rx_conf->conf.lcore_id);
2124 if (rx_conf->n_rx_queue == 0) {
2125 RTE_LOG(INFO, L3FWD, "lcore %u has nothing to do\n", rte_lcore_id());
2129 RTE_LOG(INFO, L3FWD, "Entering main Rx loop on lcore %u\n", rte_lcore_id());
2131 for (i = 0; i < rx_conf->n_rx_queue; i++) {
2133 portid = rx_conf->rx_queue_list[i].port_id;
2134 queueid = rx_conf->rx_queue_list[i].queue_id;
2135 RTE_LOG(INFO, L3FWD,
2136 " -- lcoreid=%u portid=%u rxqueueid=%hhu\n",
2137 rte_lcore_id(), portid, queueid);
2141 * Init all condition variables (one per rx thread)
2143 for (i = 0; i < rx_conf->n_rx_queue; i++)
2144 lthread_cond_init(NULL, &rx_conf->ready[i], NULL);
2148 rx_conf->conf.cpu_id = sched_getcpu();
2149 rte_atomic16_inc(&rx_counter);
2153 * Read packet from RX queues
2155 for (i = 0; i < rx_conf->n_rx_queue; ++i) {
2156 portid = rx_conf->rx_queue_list[i].port_id;
2157 queueid = rx_conf->rx_queue_list[i].queue_id;
2159 SET_CPU_BUSY(rx_conf, CPU_POLL);
2160 nb_rx = rte_eth_rx_burst(portid, queueid, pkts_burst,
2162 SET_CPU_IDLE(rx_conf, CPU_POLL);
2165 worker_id = (worker_id + 1) % rx_conf->n_ring;
2166 old_len = len[worker_id];
2168 SET_CPU_BUSY(rx_conf, CPU_PROCESS);
2169 ret = rte_ring_sp_enqueue_burst(
2170 rx_conf->ring[worker_id],
2171 (void **) pkts_burst,
2174 new_len = old_len + ret;
2176 if (new_len >= BURST_SIZE) {
2177 lthread_cond_signal(rx_conf->ready[worker_id]);
2181 len[worker_id] = new_len;
2183 if (unlikely(ret < nb_rx)) {
2186 for (k = ret; k < nb_rx; k++) {
2187 struct rte_mbuf *m = pkts_burst[k];
2189 rte_pktmbuf_free(m);
2192 SET_CPU_IDLE(rx_conf, CPU_PROCESS);
2202 * Start scheduler with initial lthread on lcore
2204 * This lthread loop spawns all rx and tx lthreads on master lcore
2208 lthread_spawner(__rte_unused void *arg)
2210 struct lthread *lt[MAX_THREAD];
2214 printf("Entering lthread_spawner\n");
2217 * Create producers (rx threads) on default lcore
2219 for (i = 0; i < n_rx_thread; i++) {
2220 rx_thread[i].conf.thread_id = i;
2221 lthread_create(<[n_thread], -1, lthread_rx,
2222 (void *)&rx_thread[i]);
2227 * Wait for all producers. Until some producers can be started on the same
2228 * scheduler as this lthread, yielding is required to let them to run and
2229 * prevent deadlock here.
2231 while (rte_atomic16_read(&rx_counter) < n_rx_thread)
2232 lthread_sleep(100000);
2235 * Create consumers (tx threads) on default lcore_id
2237 for (i = 0; i < n_tx_thread; i++) {
2238 tx_thread[i].conf.thread_id = i;
2239 lthread_create(<[n_thread], -1, lthread_tx,
2240 (void *)&tx_thread[i]);
2245 * Wait for all threads finished
2247 for (i = 0; i < n_thread; i++)
2248 lthread_join(lt[i], NULL);
2254 * Start master scheduler with initial lthread spawning rx and tx lthreads
2255 * (main_lthread_master).
2258 lthread_master_spawner(__rte_unused void *arg) {
2260 int lcore_id = rte_lcore_id();
2262 RTE_PER_LCORE(lcore_conf) = &lcore_conf[lcore_id];
2263 lthread_create(<, -1, lthread_spawner, NULL);
2270 * Start scheduler on lcore.
2273 sched_spawner(__rte_unused void *arg) {
2275 int lcore_id = rte_lcore_id();
2278 if (lcore_id == cpu_load_lcore_id) {
2279 cpu_load_collector(arg);
2282 #endif /* APP_CPU_LOAD */
2284 RTE_PER_LCORE(lcore_conf) = &lcore_conf[lcore_id];
2285 lthread_create(<, -1, lthread_null, NULL);
2291 /* main processing loop */
2293 pthread_tx(void *dummy)
2295 struct rte_mbuf *pkts_burst[MAX_PKT_BURST];
2296 uint64_t prev_tsc, diff_tsc, cur_tsc;
2299 struct thread_tx_conf *tx_conf;
2301 const uint64_t drain_tsc = (rte_get_tsc_hz() + US_PER_S - 1) /
2302 US_PER_S * BURST_TX_DRAIN_US;
2306 tx_conf = (struct thread_tx_conf *)dummy;
2308 RTE_LOG(INFO, L3FWD, "Entering main Tx loop on lcore %u\n", rte_lcore_id());
2310 tx_conf->conf.cpu_id = sched_getcpu();
2311 rte_atomic16_inc(&tx_counter);
2314 cur_tsc = rte_rdtsc();
2317 * TX burst queue drain
2319 diff_tsc = cur_tsc - prev_tsc;
2320 if (unlikely(diff_tsc > drain_tsc)) {
2323 * This could be optimized (use queueid instead of
2324 * portid), but it is not called so often
2326 SET_CPU_BUSY(tx_conf, CPU_PROCESS);
2327 for (portid = 0; portid < RTE_MAX_ETHPORTS; portid++) {
2328 if (tx_conf->tx_mbufs[portid].len == 0)
2330 send_burst(tx_conf, tx_conf->tx_mbufs[portid].len, portid);
2331 tx_conf->tx_mbufs[portid].len = 0;
2333 SET_CPU_IDLE(tx_conf, CPU_PROCESS);
2339 * Read packet from ring
2341 SET_CPU_BUSY(tx_conf, CPU_POLL);
2342 nb_rx = rte_ring_sc_dequeue_burst(tx_conf->ring,
2343 (void **)pkts_burst, MAX_PKT_BURST, NULL);
2344 SET_CPU_IDLE(tx_conf, CPU_POLL);
2346 if (unlikely(nb_rx == 0)) {
2351 SET_CPU_BUSY(tx_conf, CPU_PROCESS);
2352 portid = pkts_burst[0]->port;
2353 process_burst(pkts_burst, nb_rx, portid);
2354 SET_CPU_IDLE(tx_conf, CPU_PROCESS);
2360 pthread_rx(void *dummy)
2369 struct rte_mbuf *pkts_burst[MAX_PKT_BURST];
2371 struct thread_rx_conf *rx_conf;
2373 lcore_id = rte_lcore_id();
2374 rx_conf = (struct thread_rx_conf *)dummy;
2376 if (rx_conf->n_rx_queue == 0) {
2377 RTE_LOG(INFO, L3FWD, "lcore %u has nothing to do\n", lcore_id);
2381 RTE_LOG(INFO, L3FWD, "entering main rx loop on lcore %u\n", lcore_id);
2383 for (i = 0; i < rx_conf->n_rx_queue; i++) {
2385 portid = rx_conf->rx_queue_list[i].port_id;
2386 queueid = rx_conf->rx_queue_list[i].queue_id;
2387 RTE_LOG(INFO, L3FWD,
2388 " -- lcoreid=%u portid=%u rxqueueid=%hhu\n",
2389 lcore_id, portid, queueid);
2393 rx_conf->conf.cpu_id = sched_getcpu();
2394 rte_atomic16_inc(&rx_counter);
2398 * Read packet from RX queues
2400 for (i = 0; i < rx_conf->n_rx_queue; ++i) {
2401 portid = rx_conf->rx_queue_list[i].port_id;
2402 queueid = rx_conf->rx_queue_list[i].queue_id;
2404 SET_CPU_BUSY(rx_conf, CPU_POLL);
2405 nb_rx = rte_eth_rx_burst(portid, queueid, pkts_burst,
2407 SET_CPU_IDLE(rx_conf, CPU_POLL);
2414 SET_CPU_BUSY(rx_conf, CPU_PROCESS);
2415 worker_id = (worker_id + 1) % rx_conf->n_ring;
2416 n = rte_ring_sp_enqueue_burst(rx_conf->ring[worker_id],
2417 (void **)pkts_burst, nb_rx, NULL);
2419 if (unlikely(n != nb_rx)) {
2422 for (k = n; k < nb_rx; k++) {
2423 struct rte_mbuf *m = pkts_burst[k];
2425 rte_pktmbuf_free(m);
2429 SET_CPU_IDLE(rx_conf, CPU_PROCESS);
2439 pthread_run(__rte_unused void *arg) {
2440 int lcore_id = rte_lcore_id();
2443 for (i = 0; i < n_rx_thread; i++)
2444 if (rx_thread[i].conf.lcore_id == lcore_id) {
2445 printf("Start rx thread on %d...\n", lcore_id);
2446 RTE_PER_LCORE(lcore_conf) = &lcore_conf[lcore_id];
2447 RTE_PER_LCORE(lcore_conf)->data = (void *)&rx_thread[i];
2448 pthread_rx((void *)&rx_thread[i]);
2452 for (i = 0; i < n_tx_thread; i++)
2453 if (tx_thread[i].conf.lcore_id == lcore_id) {
2454 printf("Start tx thread on %d...\n", lcore_id);
2455 RTE_PER_LCORE(lcore_conf) = &lcore_conf[lcore_id];
2456 RTE_PER_LCORE(lcore_conf)->data = (void *)&tx_thread[i];
2457 pthread_tx((void *)&tx_thread[i]);
2462 if (lcore_id == cpu_load_lcore_id)
2463 cpu_load_collector(arg);
2464 #endif /* APP_CPU_LOAD */
2470 check_lcore_params(void)
2472 uint8_t queue, lcore;
2476 for (i = 0; i < nb_rx_thread_params; ++i) {
2477 queue = rx_thread_params[i].queue_id;
2478 if (queue >= MAX_RX_QUEUE_PER_PORT) {
2479 printf("invalid queue number: %hhu\n", queue);
2482 lcore = rx_thread_params[i].lcore_id;
2483 if (!rte_lcore_is_enabled(lcore)) {
2484 printf("error: lcore %hhu is not enabled in lcore mask\n", lcore);
2487 socketid = rte_lcore_to_socket_id(lcore);
2488 if ((socketid != 0) && (numa_on == 0))
2489 printf("warning: lcore %hhu is on socket %d with numa off\n",
2496 check_port_config(void)
2501 for (i = 0; i < nb_rx_thread_params; ++i) {
2502 portid = rx_thread_params[i].port_id;
2503 if ((enabled_port_mask & (1 << portid)) == 0) {
2504 printf("port %u is not enabled in port mask\n", portid);
2507 if (!rte_eth_dev_is_valid_port(portid)) {
2508 printf("port %u is not present on the board\n", portid);
2516 get_port_n_rx_queues(const uint16_t port)
2521 for (i = 0; i < nb_rx_thread_params; ++i)
2522 if (rx_thread_params[i].port_id == port &&
2523 rx_thread_params[i].queue_id > queue)
2524 queue = rx_thread_params[i].queue_id;
2526 return (uint8_t)(++queue);
2533 struct thread_rx_conf *rx_conf;
2534 struct thread_tx_conf *tx_conf;
2535 unsigned rx_thread_id, tx_thread_id;
2537 struct rte_ring *ring = NULL;
2539 for (tx_thread_id = 0; tx_thread_id < n_tx_thread; tx_thread_id++) {
2541 tx_conf = &tx_thread[tx_thread_id];
2543 printf("Connecting tx-thread %d with rx-thread %d\n", tx_thread_id,
2544 tx_conf->conf.thread_id);
2546 rx_thread_id = tx_conf->conf.thread_id;
2547 if (rx_thread_id > n_tx_thread) {
2548 printf("connection from tx-thread %u to rx-thread %u fails "
2549 "(rx-thread not defined)\n", tx_thread_id, rx_thread_id);
2553 rx_conf = &rx_thread[rx_thread_id];
2554 socket_io = rte_lcore_to_socket_id(rx_conf->conf.lcore_id);
2556 snprintf(name, sizeof(name), "app_ring_s%u_rx%u_tx%u",
2557 socket_io, rx_thread_id, tx_thread_id);
2559 ring = rte_ring_create(name, 1024 * 4, socket_io,
2560 RING_F_SP_ENQ | RING_F_SC_DEQ);
2563 rte_panic("Cannot create ring to connect rx-thread %u "
2564 "with tx-thread %u\n", rx_thread_id, tx_thread_id);
2567 rx_conf->ring[rx_conf->n_ring] = ring;
2569 tx_conf->ring = ring;
2570 tx_conf->ready = &rx_conf->ready[rx_conf->n_ring];
2578 init_rx_queues(void)
2580 uint16_t i, nb_rx_queue;
2585 for (i = 0; i < nb_rx_thread_params; ++i) {
2586 thread = rx_thread_params[i].thread_id;
2587 nb_rx_queue = rx_thread[thread].n_rx_queue;
2589 if (nb_rx_queue >= MAX_RX_QUEUE_PER_LCORE) {
2590 printf("error: too many queues (%u) for thread: %u\n",
2591 (unsigned)nb_rx_queue + 1, (unsigned)thread);
2595 rx_thread[thread].conf.thread_id = thread;
2596 rx_thread[thread].conf.lcore_id = rx_thread_params[i].lcore_id;
2597 rx_thread[thread].rx_queue_list[nb_rx_queue].port_id =
2598 rx_thread_params[i].port_id;
2599 rx_thread[thread].rx_queue_list[nb_rx_queue].queue_id =
2600 rx_thread_params[i].queue_id;
2601 rx_thread[thread].n_rx_queue++;
2603 if (thread >= n_rx_thread)
2604 n_rx_thread = thread + 1;
2611 init_tx_threads(void)
2616 for (i = 0; i < nb_tx_thread_params; ++i) {
2617 tx_thread[n_tx_thread].conf.thread_id = tx_thread_params[i].thread_id;
2618 tx_thread[n_tx_thread].conf.lcore_id = tx_thread_params[i].lcore_id;
2626 print_usage(const char *prgname)
2628 printf("%s [EAL options] -- -p PORTMASK -P"
2629 " [--rx (port,queue,lcore,thread)[,(port,queue,lcore,thread]]"
2630 " [--tx (lcore,thread)[,(lcore,thread]]"
2631 " [--enable-jumbo [--max-pkt-len PKTLEN]]\n"
2632 " [--parse-ptype]\n\n"
2633 " -p PORTMASK: hexadecimal bitmask of ports to configure\n"
2634 " -P : enable promiscuous mode\n"
2635 " --rx (port,queue,lcore,thread): rx queues configuration\n"
2636 " --tx (lcore,thread): tx threads configuration\n"
2637 " --stat-lcore LCORE: use lcore for stat collector\n"
2638 " --eth-dest=X,MM:MM:MM:MM:MM:MM: optional, ethernet destination for port X\n"
2639 " --no-numa: optional, disable numa awareness\n"
2640 " --ipv6: optional, specify it if running ipv6 packets\n"
2641 " --enable-jumbo: enable jumbo frame"
2642 " which max packet len is PKTLEN in decimal (64-9600)\n"
2643 " --hash-entry-num: specify the hash entry number in hexadecimal to be setup\n"
2644 " --no-lthreads: turn off lthread model\n"
2645 " --parse-ptype: set to use software to analyze packet type\n\n",
2649 static int parse_max_pkt_len(const char *pktlen)
2654 /* parse decimal string */
2655 len = strtoul(pktlen, &end, 10);
2656 if ((pktlen[0] == '\0') || (end == NULL) || (*end != '\0'))
2666 parse_portmask(const char *portmask)
2671 /* parse hexadecimal string */
2672 pm = strtoul(portmask, &end, 16);
2673 if ((portmask[0] == '\0') || (end == NULL) || (*end != '\0'))
2682 #if (APP_LOOKUP_METHOD == APP_LOOKUP_EXACT_MATCH)
2684 parse_hash_entry_number(const char *hash_entry_num)
2687 unsigned long hash_en;
2689 /* parse hexadecimal string */
2690 hash_en = strtoul(hash_entry_num, &end, 16);
2691 if ((hash_entry_num[0] == '\0') || (end == NULL) || (*end != '\0'))
2702 parse_rx_config(const char *q_arg)
2705 const char *p, *p0 = q_arg;
2714 unsigned long int_fld[_NUM_FLD];
2715 char *str_fld[_NUM_FLD];
2719 nb_rx_thread_params = 0;
2721 while ((p = strchr(p0, '(')) != NULL) {
2723 p0 = strchr(p, ')');
2728 if (size >= sizeof(s))
2731 snprintf(s, sizeof(s), "%.*s", size, p);
2732 if (rte_strsplit(s, sizeof(s), str_fld, _NUM_FLD, ',') != _NUM_FLD)
2734 for (i = 0; i < _NUM_FLD; i++) {
2736 int_fld[i] = strtoul(str_fld[i], &end, 0);
2737 if (errno != 0 || end == str_fld[i] || int_fld[i] > 255)
2740 if (nb_rx_thread_params >= MAX_LCORE_PARAMS) {
2741 printf("exceeded max number of rx params: %hu\n",
2742 nb_rx_thread_params);
2745 rx_thread_params_array[nb_rx_thread_params].port_id =
2747 rx_thread_params_array[nb_rx_thread_params].queue_id =
2748 (uint8_t)int_fld[FLD_QUEUE];
2749 rx_thread_params_array[nb_rx_thread_params].lcore_id =
2750 (uint8_t)int_fld[FLD_LCORE];
2751 rx_thread_params_array[nb_rx_thread_params].thread_id =
2752 (uint8_t)int_fld[FLD_THREAD];
2753 ++nb_rx_thread_params;
2755 rx_thread_params = rx_thread_params_array;
2760 parse_tx_config(const char *q_arg)
2763 const char *p, *p0 = q_arg;
2770 unsigned long int_fld[_NUM_FLD];
2771 char *str_fld[_NUM_FLD];
2775 nb_tx_thread_params = 0;
2777 while ((p = strchr(p0, '(')) != NULL) {
2779 p0 = strchr(p, ')');
2784 if (size >= sizeof(s))
2787 snprintf(s, sizeof(s), "%.*s", size, p);
2788 if (rte_strsplit(s, sizeof(s), str_fld, _NUM_FLD, ',') != _NUM_FLD)
2790 for (i = 0; i < _NUM_FLD; i++) {
2792 int_fld[i] = strtoul(str_fld[i], &end, 0);
2793 if (errno != 0 || end == str_fld[i] || int_fld[i] > 255)
2796 if (nb_tx_thread_params >= MAX_LCORE_PARAMS) {
2797 printf("exceeded max number of tx params: %hu\n",
2798 nb_tx_thread_params);
2801 tx_thread_params_array[nb_tx_thread_params].lcore_id =
2802 (uint8_t)int_fld[FLD_LCORE];
2803 tx_thread_params_array[nb_tx_thread_params].thread_id =
2804 (uint8_t)int_fld[FLD_THREAD];
2805 ++nb_tx_thread_params;
2807 tx_thread_params = tx_thread_params_array;
2812 #if (APP_CPU_LOAD > 0)
2814 parse_stat_lcore(const char *stat_lcore)
2817 unsigned long lcore_id;
2819 lcore_id = strtoul(stat_lcore, &end, 10);
2820 if ((stat_lcore[0] == '\0') || (end == NULL) || (*end != '\0'))
2828 parse_eth_dest(const char *optarg)
2832 uint8_t c, *dest, peer_addr[6];
2835 portid = strtoul(optarg, &port_end, 10);
2836 if (errno != 0 || port_end == optarg || *port_end++ != ',')
2837 rte_exit(EXIT_FAILURE,
2838 "Invalid eth-dest: %s", optarg);
2839 if (portid >= RTE_MAX_ETHPORTS)
2840 rte_exit(EXIT_FAILURE,
2841 "eth-dest: port %d >= RTE_MAX_ETHPORTS(%d)\n",
2842 portid, RTE_MAX_ETHPORTS);
2844 if (cmdline_parse_etheraddr(NULL, port_end,
2845 &peer_addr, sizeof(peer_addr)) < 0)
2846 rte_exit(EXIT_FAILURE,
2847 "Invalid ethernet address: %s\n",
2849 dest = (uint8_t *)&dest_eth_addr[portid];
2850 for (c = 0; c < 6; c++)
2851 dest[c] = peer_addr[c];
2852 *(uint64_t *)(val_eth + portid) = dest_eth_addr[portid];
2855 #define CMD_LINE_OPT_RX_CONFIG "rx"
2856 #define CMD_LINE_OPT_TX_CONFIG "tx"
2857 #define CMD_LINE_OPT_STAT_LCORE "stat-lcore"
2858 #define CMD_LINE_OPT_ETH_DEST "eth-dest"
2859 #define CMD_LINE_OPT_NO_NUMA "no-numa"
2860 #define CMD_LINE_OPT_IPV6 "ipv6"
2861 #define CMD_LINE_OPT_ENABLE_JUMBO "enable-jumbo"
2862 #define CMD_LINE_OPT_HASH_ENTRY_NUM "hash-entry-num"
2863 #define CMD_LINE_OPT_NO_LTHREADS "no-lthreads"
2864 #define CMD_LINE_OPT_PARSE_PTYPE "parse-ptype"
2866 /* Parse the argument given in the command line of the application */
2868 parse_args(int argc, char **argv)
2873 char *prgname = argv[0];
2874 static struct option lgopts[] = {
2875 {CMD_LINE_OPT_RX_CONFIG, 1, 0, 0},
2876 {CMD_LINE_OPT_TX_CONFIG, 1, 0, 0},
2877 {CMD_LINE_OPT_STAT_LCORE, 1, 0, 0},
2878 {CMD_LINE_OPT_ETH_DEST, 1, 0, 0},
2879 {CMD_LINE_OPT_NO_NUMA, 0, 0, 0},
2880 {CMD_LINE_OPT_IPV6, 0, 0, 0},
2881 {CMD_LINE_OPT_ENABLE_JUMBO, 0, 0, 0},
2882 {CMD_LINE_OPT_HASH_ENTRY_NUM, 1, 0, 0},
2883 {CMD_LINE_OPT_NO_LTHREADS, 0, 0, 0},
2884 {CMD_LINE_OPT_PARSE_PTYPE, 0, 0, 0},
2890 while ((opt = getopt_long(argc, argvopt, "p:P",
2891 lgopts, &option_index)) != EOF) {
2896 enabled_port_mask = parse_portmask(optarg);
2897 if (enabled_port_mask == 0) {
2898 printf("invalid portmask\n");
2899 print_usage(prgname);
2904 printf("Promiscuous mode selected\n");
2910 if (!strncmp(lgopts[option_index].name, CMD_LINE_OPT_RX_CONFIG,
2911 sizeof(CMD_LINE_OPT_RX_CONFIG))) {
2912 ret = parse_rx_config(optarg);
2914 printf("invalid rx-config\n");
2915 print_usage(prgname);
2920 if (!strncmp(lgopts[option_index].name, CMD_LINE_OPT_TX_CONFIG,
2921 sizeof(CMD_LINE_OPT_TX_CONFIG))) {
2922 ret = parse_tx_config(optarg);
2924 printf("invalid tx-config\n");
2925 print_usage(prgname);
2930 #if (APP_CPU_LOAD > 0)
2931 if (!strncmp(lgopts[option_index].name, CMD_LINE_OPT_STAT_LCORE,
2932 sizeof(CMD_LINE_OPT_STAT_LCORE))) {
2933 cpu_load_lcore_id = parse_stat_lcore(optarg);
2937 if (!strncmp(lgopts[option_index].name, CMD_LINE_OPT_ETH_DEST,
2938 sizeof(CMD_LINE_OPT_ETH_DEST)))
2939 parse_eth_dest(optarg);
2941 if (!strncmp(lgopts[option_index].name, CMD_LINE_OPT_NO_NUMA,
2942 sizeof(CMD_LINE_OPT_NO_NUMA))) {
2943 printf("numa is disabled\n");
2947 #if (APP_LOOKUP_METHOD == APP_LOOKUP_EXACT_MATCH)
2948 if (!strncmp(lgopts[option_index].name, CMD_LINE_OPT_IPV6,
2949 sizeof(CMD_LINE_OPT_IPV6))) {
2950 printf("ipv6 is specified\n");
2955 if (!strncmp(lgopts[option_index].name, CMD_LINE_OPT_NO_LTHREADS,
2956 sizeof(CMD_LINE_OPT_NO_LTHREADS))) {
2957 printf("l-threads model is disabled\n");
2961 if (!strncmp(lgopts[option_index].name, CMD_LINE_OPT_PARSE_PTYPE,
2962 sizeof(CMD_LINE_OPT_PARSE_PTYPE))) {
2963 printf("software packet type parsing enabled\n");
2967 if (!strncmp(lgopts[option_index].name, CMD_LINE_OPT_ENABLE_JUMBO,
2968 sizeof(CMD_LINE_OPT_ENABLE_JUMBO))) {
2969 struct option lenopts = {"max-pkt-len", required_argument, 0,
2972 printf("jumbo frame is enabled - disabling simple TX path\n");
2973 port_conf.rxmode.offloads |=
2974 DEV_RX_OFFLOAD_JUMBO_FRAME;
2975 port_conf.txmode.offloads |=
2976 DEV_TX_OFFLOAD_MULTI_SEGS;
2978 /* if no max-pkt-len set, use the default value ETHER_MAX_LEN */
2979 if (0 == getopt_long(argc, argvopt, "", &lenopts,
2982 ret = parse_max_pkt_len(optarg);
2983 if ((ret < 64) || (ret > MAX_JUMBO_PKT_LEN)) {
2984 printf("invalid packet length\n");
2985 print_usage(prgname);
2988 port_conf.rxmode.max_rx_pkt_len = ret;
2990 printf("set jumbo frame max packet length to %u\n",
2991 (unsigned int)port_conf.rxmode.max_rx_pkt_len);
2993 #if (APP_LOOKUP_METHOD == APP_LOOKUP_EXACT_MATCH)
2994 if (!strncmp(lgopts[option_index].name, CMD_LINE_OPT_HASH_ENTRY_NUM,
2995 sizeof(CMD_LINE_OPT_HASH_ENTRY_NUM))) {
2996 ret = parse_hash_entry_number(optarg);
2997 if ((ret > 0) && (ret <= L3FWD_HASH_ENTRIES)) {
2998 hash_entry_number = ret;
3000 printf("invalid hash entry number\n");
3001 print_usage(prgname);
3009 print_usage(prgname);
3015 argv[optind-1] = prgname;
3018 optind = 1; /* reset getopt lib */
3023 print_ethaddr(const char *name, const struct ether_addr *eth_addr)
3025 char buf[ETHER_ADDR_FMT_SIZE];
3027 ether_format_addr(buf, ETHER_ADDR_FMT_SIZE, eth_addr);
3028 printf("%s%s", name, buf);
3031 #if (APP_LOOKUP_METHOD == APP_LOOKUP_EXACT_MATCH)
3033 static void convert_ipv4_5tuple(struct ipv4_5tuple *key1,
3034 union ipv4_5tuple_host *key2)
3036 key2->ip_dst = rte_cpu_to_be_32(key1->ip_dst);
3037 key2->ip_src = rte_cpu_to_be_32(key1->ip_src);
3038 key2->port_dst = rte_cpu_to_be_16(key1->port_dst);
3039 key2->port_src = rte_cpu_to_be_16(key1->port_src);
3040 key2->proto = key1->proto;
3045 static void convert_ipv6_5tuple(struct ipv6_5tuple *key1,
3046 union ipv6_5tuple_host *key2)
3050 for (i = 0; i < 16; i++) {
3051 key2->ip_dst[i] = key1->ip_dst[i];
3052 key2->ip_src[i] = key1->ip_src[i];
3054 key2->port_dst = rte_cpu_to_be_16(key1->port_dst);
3055 key2->port_src = rte_cpu_to_be_16(key1->port_src);
3056 key2->proto = key1->proto;
3062 #define BYTE_VALUE_MAX 256
3063 #define ALL_32_BITS 0xffffffff
3064 #define BIT_8_TO_15 0x0000ff00
3066 populate_ipv4_few_flow_into_table(const struct rte_hash *h)
3070 uint32_t array_len = RTE_DIM(ipv4_l3fwd_route_array);
3072 mask0 = _mm_set_epi32(ALL_32_BITS, ALL_32_BITS, ALL_32_BITS, BIT_8_TO_15);
3073 for (i = 0; i < array_len; i++) {
3074 struct ipv4_l3fwd_route entry;
3075 union ipv4_5tuple_host newkey;
3077 entry = ipv4_l3fwd_route_array[i];
3078 convert_ipv4_5tuple(&entry.key, &newkey);
3079 ret = rte_hash_add_key(h, (void *)&newkey);
3081 rte_exit(EXIT_FAILURE, "Unable to add entry %" PRIu32
3082 " to the l3fwd hash.\n", i);
3084 ipv4_l3fwd_out_if[ret] = entry.if_out;
3086 printf("Hash: Adding 0x%" PRIx32 " keys\n", array_len);
3089 #define BIT_16_TO_23 0x00ff0000
3091 populate_ipv6_few_flow_into_table(const struct rte_hash *h)
3095 uint32_t array_len = RTE_DIM(ipv6_l3fwd_route_array);
3097 mask1 = _mm_set_epi32(ALL_32_BITS, ALL_32_BITS, ALL_32_BITS, BIT_16_TO_23);
3098 mask2 = _mm_set_epi32(0, 0, ALL_32_BITS, ALL_32_BITS);
3099 for (i = 0; i < array_len; i++) {
3100 struct ipv6_l3fwd_route entry;
3101 union ipv6_5tuple_host newkey;
3103 entry = ipv6_l3fwd_route_array[i];
3104 convert_ipv6_5tuple(&entry.key, &newkey);
3105 ret = rte_hash_add_key(h, (void *)&newkey);
3107 rte_exit(EXIT_FAILURE, "Unable to add entry %" PRIu32
3108 " to the l3fwd hash.\n", i);
3110 ipv6_l3fwd_out_if[ret] = entry.if_out;
3112 printf("Hash: Adding 0x%" PRIx32 "keys\n", array_len);
3115 #define NUMBER_PORT_USED 4
3117 populate_ipv4_many_flow_into_table(const struct rte_hash *h,
3118 unsigned int nr_flow)
3122 mask0 = _mm_set_epi32(ALL_32_BITS, ALL_32_BITS, ALL_32_BITS, BIT_8_TO_15);
3124 for (i = 0; i < nr_flow; i++) {
3125 struct ipv4_l3fwd_route entry;
3126 union ipv4_5tuple_host newkey;
3127 uint8_t a = (uint8_t)((i / NUMBER_PORT_USED) % BYTE_VALUE_MAX);
3128 uint8_t b = (uint8_t)(((i / NUMBER_PORT_USED) / BYTE_VALUE_MAX) %
3130 uint8_t c = (uint8_t)((i / NUMBER_PORT_USED) / (BYTE_VALUE_MAX *
3132 /* Create the ipv4 exact match flow */
3133 memset(&entry, 0, sizeof(entry));
3134 switch (i & (NUMBER_PORT_USED - 1)) {
3136 entry = ipv4_l3fwd_route_array[0];
3137 entry.key.ip_dst = IPv4(101, c, b, a);
3140 entry = ipv4_l3fwd_route_array[1];
3141 entry.key.ip_dst = IPv4(201, c, b, a);
3144 entry = ipv4_l3fwd_route_array[2];
3145 entry.key.ip_dst = IPv4(111, c, b, a);
3148 entry = ipv4_l3fwd_route_array[3];
3149 entry.key.ip_dst = IPv4(211, c, b, a);
3152 convert_ipv4_5tuple(&entry.key, &newkey);
3153 int32_t ret = rte_hash_add_key(h, (void *)&newkey);
3156 rte_exit(EXIT_FAILURE, "Unable to add entry %u\n", i);
3158 ipv4_l3fwd_out_if[ret] = (uint8_t)entry.if_out;
3161 printf("Hash: Adding 0x%x keys\n", nr_flow);
3165 populate_ipv6_many_flow_into_table(const struct rte_hash *h,
3166 unsigned int nr_flow)
3170 mask1 = _mm_set_epi32(ALL_32_BITS, ALL_32_BITS, ALL_32_BITS, BIT_16_TO_23);
3171 mask2 = _mm_set_epi32(0, 0, ALL_32_BITS, ALL_32_BITS);
3172 for (i = 0; i < nr_flow; i++) {
3173 struct ipv6_l3fwd_route entry;
3174 union ipv6_5tuple_host newkey;
3176 uint8_t a = (uint8_t) ((i / NUMBER_PORT_USED) % BYTE_VALUE_MAX);
3177 uint8_t b = (uint8_t) (((i / NUMBER_PORT_USED) / BYTE_VALUE_MAX) %
3179 uint8_t c = (uint8_t) ((i / NUMBER_PORT_USED) / (BYTE_VALUE_MAX *
3182 /* Create the ipv6 exact match flow */
3183 memset(&entry, 0, sizeof(entry));
3184 switch (i & (NUMBER_PORT_USED - 1)) {
3186 entry = ipv6_l3fwd_route_array[0];
3189 entry = ipv6_l3fwd_route_array[1];
3192 entry = ipv6_l3fwd_route_array[2];
3195 entry = ipv6_l3fwd_route_array[3];
3198 entry.key.ip_dst[13] = c;
3199 entry.key.ip_dst[14] = b;
3200 entry.key.ip_dst[15] = a;
3201 convert_ipv6_5tuple(&entry.key, &newkey);
3202 int32_t ret = rte_hash_add_key(h, (void *)&newkey);
3205 rte_exit(EXIT_FAILURE, "Unable to add entry %u\n", i);
3207 ipv6_l3fwd_out_if[ret] = (uint8_t) entry.if_out;
3210 printf("Hash: Adding 0x%x keys\n", nr_flow);
3214 setup_hash(int socketid)
3216 struct rte_hash_parameters ipv4_l3fwd_hash_params = {
3218 .entries = L3FWD_HASH_ENTRIES,
3219 .key_len = sizeof(union ipv4_5tuple_host),
3220 .hash_func = ipv4_hash_crc,
3221 .hash_func_init_val = 0,
3224 struct rte_hash_parameters ipv6_l3fwd_hash_params = {
3226 .entries = L3FWD_HASH_ENTRIES,
3227 .key_len = sizeof(union ipv6_5tuple_host),
3228 .hash_func = ipv6_hash_crc,
3229 .hash_func_init_val = 0,
3234 /* create ipv4 hash */
3235 snprintf(s, sizeof(s), "ipv4_l3fwd_hash_%d", socketid);
3236 ipv4_l3fwd_hash_params.name = s;
3237 ipv4_l3fwd_hash_params.socket_id = socketid;
3238 ipv4_l3fwd_lookup_struct[socketid] =
3239 rte_hash_create(&ipv4_l3fwd_hash_params);
3240 if (ipv4_l3fwd_lookup_struct[socketid] == NULL)
3241 rte_exit(EXIT_FAILURE, "Unable to create the l3fwd hash on "
3242 "socket %d\n", socketid);
3244 /* create ipv6 hash */
3245 snprintf(s, sizeof(s), "ipv6_l3fwd_hash_%d", socketid);
3246 ipv6_l3fwd_hash_params.name = s;
3247 ipv6_l3fwd_hash_params.socket_id = socketid;
3248 ipv6_l3fwd_lookup_struct[socketid] =
3249 rte_hash_create(&ipv6_l3fwd_hash_params);
3250 if (ipv6_l3fwd_lookup_struct[socketid] == NULL)
3251 rte_exit(EXIT_FAILURE, "Unable to create the l3fwd hash on "
3252 "socket %d\n", socketid);
3254 if (hash_entry_number != HASH_ENTRY_NUMBER_DEFAULT) {
3255 /* For testing hash matching with a large number of flows we
3256 * generate millions of IP 5-tuples with an incremented dst
3257 * address to initialize the hash table. */
3259 /* populate the ipv4 hash */
3260 populate_ipv4_many_flow_into_table(
3261 ipv4_l3fwd_lookup_struct[socketid], hash_entry_number);
3263 /* populate the ipv6 hash */
3264 populate_ipv6_many_flow_into_table(
3265 ipv6_l3fwd_lookup_struct[socketid], hash_entry_number);
3268 /* Use data in ipv4/ipv6 l3fwd lookup table directly to initialize
3271 /* populate the ipv4 hash */
3272 populate_ipv4_few_flow_into_table(
3273 ipv4_l3fwd_lookup_struct[socketid]);
3275 /* populate the ipv6 hash */
3276 populate_ipv6_few_flow_into_table(
3277 ipv6_l3fwd_lookup_struct[socketid]);
3283 #if (APP_LOOKUP_METHOD == APP_LOOKUP_LPM)
3285 setup_lpm(int socketid)
3287 struct rte_lpm6_config config;
3288 struct rte_lpm_config lpm_ipv4_config;
3293 /* create the LPM table */
3294 snprintf(s, sizeof(s), "IPV4_L3FWD_LPM_%d", socketid);
3295 lpm_ipv4_config.max_rules = IPV4_L3FWD_LPM_MAX_RULES;
3296 lpm_ipv4_config.number_tbl8s = 256;
3297 lpm_ipv4_config.flags = 0;
3298 ipv4_l3fwd_lookup_struct[socketid] =
3299 rte_lpm_create(s, socketid, &lpm_ipv4_config);
3300 if (ipv4_l3fwd_lookup_struct[socketid] == NULL)
3301 rte_exit(EXIT_FAILURE, "Unable to create the l3fwd LPM table"
3302 " on socket %d\n", socketid);
3304 /* populate the LPM table */
3305 for (i = 0; i < IPV4_L3FWD_NUM_ROUTES; i++) {
3307 /* skip unused ports */
3308 if ((1 << ipv4_l3fwd_route_array[i].if_out &
3309 enabled_port_mask) == 0)
3312 ret = rte_lpm_add(ipv4_l3fwd_lookup_struct[socketid],
3313 ipv4_l3fwd_route_array[i].ip,
3314 ipv4_l3fwd_route_array[i].depth,
3315 ipv4_l3fwd_route_array[i].if_out);
3318 rte_exit(EXIT_FAILURE, "Unable to add entry %u to the "
3319 "l3fwd LPM table on socket %d\n",
3323 printf("LPM: Adding route 0x%08x / %d (%d)\n",
3324 (unsigned)ipv4_l3fwd_route_array[i].ip,
3325 ipv4_l3fwd_route_array[i].depth,
3326 ipv4_l3fwd_route_array[i].if_out);
3329 /* create the LPM6 table */
3330 snprintf(s, sizeof(s), "IPV6_L3FWD_LPM_%d", socketid);
3332 config.max_rules = IPV6_L3FWD_LPM_MAX_RULES;
3333 config.number_tbl8s = IPV6_L3FWD_LPM_NUMBER_TBL8S;
3335 ipv6_l3fwd_lookup_struct[socketid] = rte_lpm6_create(s, socketid,
3337 if (ipv6_l3fwd_lookup_struct[socketid] == NULL)
3338 rte_exit(EXIT_FAILURE, "Unable to create the l3fwd LPM table"
3339 " on socket %d\n", socketid);
3341 /* populate the LPM table */
3342 for (i = 0; i < IPV6_L3FWD_NUM_ROUTES; i++) {
3344 /* skip unused ports */
3345 if ((1 << ipv6_l3fwd_route_array[i].if_out &
3346 enabled_port_mask) == 0)
3349 ret = rte_lpm6_add(ipv6_l3fwd_lookup_struct[socketid],
3350 ipv6_l3fwd_route_array[i].ip,
3351 ipv6_l3fwd_route_array[i].depth,
3352 ipv6_l3fwd_route_array[i].if_out);
3355 rte_exit(EXIT_FAILURE, "Unable to add entry %u to the "
3356 "l3fwd LPM table on socket %d\n",
3360 printf("LPM: Adding route %s / %d (%d)\n",
3362 ipv6_l3fwd_route_array[i].depth,
3363 ipv6_l3fwd_route_array[i].if_out);
3369 init_mem(unsigned nb_mbuf)
3371 struct lcore_conf *qconf;
3376 for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
3377 if (rte_lcore_is_enabled(lcore_id) == 0)
3381 socketid = rte_lcore_to_socket_id(lcore_id);
3385 if (socketid >= NB_SOCKETS) {
3386 rte_exit(EXIT_FAILURE, "Socket %d of lcore %u is out of range %d\n",
3387 socketid, lcore_id, NB_SOCKETS);
3389 if (pktmbuf_pool[socketid] == NULL) {
3390 snprintf(s, sizeof(s), "mbuf_pool_%d", socketid);
3391 pktmbuf_pool[socketid] =
3392 rte_pktmbuf_pool_create(s, nb_mbuf,
3393 MEMPOOL_CACHE_SIZE, 0,
3394 RTE_MBUF_DEFAULT_BUF_SIZE, socketid);
3395 if (pktmbuf_pool[socketid] == NULL)
3396 rte_exit(EXIT_FAILURE,
3397 "Cannot init mbuf pool on socket %d\n", socketid);
3399 printf("Allocated mbuf pool on socket %d\n", socketid);
3401 #if (APP_LOOKUP_METHOD == APP_LOOKUP_LPM)
3402 setup_lpm(socketid);
3404 setup_hash(socketid);
3407 qconf = &lcore_conf[lcore_id];
3408 qconf->ipv4_lookup_struct = ipv4_l3fwd_lookup_struct[socketid];
3409 qconf->ipv6_lookup_struct = ipv6_l3fwd_lookup_struct[socketid];
3414 /* Check the link status of all ports in up to 9s, and print them finally */
3416 check_all_ports_link_status(uint32_t port_mask)
3418 #define CHECK_INTERVAL 100 /* 100ms */
3419 #define MAX_CHECK_TIME 90 /* 9s (90 * 100ms) in total */
3421 uint8_t count, all_ports_up, print_flag = 0;
3422 struct rte_eth_link link;
3424 printf("\nChecking link status");
3426 for (count = 0; count <= MAX_CHECK_TIME; count++) {
3428 RTE_ETH_FOREACH_DEV(portid) {
3429 if ((port_mask & (1 << portid)) == 0)
3431 memset(&link, 0, sizeof(link));
3432 rte_eth_link_get_nowait(portid, &link);
3433 /* print link status if flag set */
3434 if (print_flag == 1) {
3435 if (link.link_status)
3437 "Port%d Link Up. Speed %u Mbps - %s\n",
3438 portid, link.link_speed,
3439 (link.link_duplex == ETH_LINK_FULL_DUPLEX) ?
3440 ("full-duplex") : ("half-duplex\n"));
3442 printf("Port %d Link Down\n", portid);
3445 /* clear all_ports_up flag if any link down */
3446 if (link.link_status == ETH_LINK_DOWN) {
3451 /* after finally printing all link status, get out */
3452 if (print_flag == 1)
3455 if (all_ports_up == 0) {
3458 rte_delay_ms(CHECK_INTERVAL);
3461 /* set the print_flag if all ports up or timeout */
3462 if (all_ports_up == 1 || count == (MAX_CHECK_TIME - 1)) {
3470 main(int argc, char **argv)
3472 struct rte_eth_dev_info dev_info;
3473 struct rte_eth_txconf *txconf;
3477 uint16_t queueid, portid;
3479 uint32_t n_tx_queue, nb_lcores;
3480 uint8_t nb_rx_queue, queue, socketid;
3483 ret = rte_eal_init(argc, argv);
3485 rte_exit(EXIT_FAILURE, "Invalid EAL parameters\n");
3489 /* pre-init dst MACs for all ports to 02:00:00:00:00:xx */
3490 for (portid = 0; portid < RTE_MAX_ETHPORTS; portid++) {
3491 dest_eth_addr[portid] = ETHER_LOCAL_ADMIN_ADDR +
3492 ((uint64_t)portid << 40);
3493 *(uint64_t *)(val_eth + portid) = dest_eth_addr[portid];
3496 /* parse application arguments (after the EAL ones) */
3497 ret = parse_args(argc, argv);
3499 rte_exit(EXIT_FAILURE, "Invalid L3FWD parameters\n");
3501 if (check_lcore_params() < 0)
3502 rte_exit(EXIT_FAILURE, "check_lcore_params failed\n");
3504 printf("Initializing rx-queues...\n");
3505 ret = init_rx_queues();
3507 rte_exit(EXIT_FAILURE, "init_rx_queues failed\n");
3509 printf("Initializing tx-threads...\n");
3510 ret = init_tx_threads();
3512 rte_exit(EXIT_FAILURE, "init_tx_threads failed\n");
3514 printf("Initializing rings...\n");
3515 ret = init_rx_rings();
3517 rte_exit(EXIT_FAILURE, "init_rx_rings failed\n");
3519 nb_ports = rte_eth_dev_count_avail();
3521 if (check_port_config() < 0)
3522 rte_exit(EXIT_FAILURE, "check_port_config failed\n");
3524 nb_lcores = rte_lcore_count();
3526 /* initialize all ports */
3527 RTE_ETH_FOREACH_DEV(portid) {
3528 struct rte_eth_conf local_port_conf = port_conf;
3530 /* skip ports that are not enabled */
3531 if ((enabled_port_mask & (1 << portid)) == 0) {
3532 printf("\nSkipping disabled port %d\n", portid);
3537 printf("Initializing port %d ... ", portid);
3540 nb_rx_queue = get_port_n_rx_queues(portid);
3541 n_tx_queue = nb_lcores;
3542 if (n_tx_queue > MAX_TX_QUEUE_PER_PORT)
3543 n_tx_queue = MAX_TX_QUEUE_PER_PORT;
3544 printf("Creating queues: nb_rxq=%d nb_txq=%u... ",
3545 nb_rx_queue, (unsigned)n_tx_queue);
3546 rte_eth_dev_info_get(portid, &dev_info);
3547 if (dev_info.tx_offload_capa & DEV_TX_OFFLOAD_MBUF_FAST_FREE)
3548 local_port_conf.txmode.offloads |=
3549 DEV_TX_OFFLOAD_MBUF_FAST_FREE;
3551 local_port_conf.rx_adv_conf.rss_conf.rss_hf &=
3552 dev_info.flow_type_rss_offloads;
3553 if (local_port_conf.rx_adv_conf.rss_conf.rss_hf !=
3554 port_conf.rx_adv_conf.rss_conf.rss_hf) {
3555 printf("Port %u modified RSS hash function based on hardware support,"
3556 "requested:%#"PRIx64" configured:%#"PRIx64"\n",
3558 port_conf.rx_adv_conf.rss_conf.rss_hf,
3559 local_port_conf.rx_adv_conf.rss_conf.rss_hf);
3562 ret = rte_eth_dev_configure(portid, nb_rx_queue,
3563 (uint16_t)n_tx_queue, &local_port_conf);
3565 rte_exit(EXIT_FAILURE, "Cannot configure device: err=%d, port=%d\n",
3568 ret = rte_eth_dev_adjust_nb_rx_tx_desc(portid, &nb_rxd,
3571 rte_exit(EXIT_FAILURE,
3572 "rte_eth_dev_adjust_nb_rx_tx_desc: err=%d, port=%d\n",
3575 rte_eth_macaddr_get(portid, &ports_eth_addr[portid]);
3576 print_ethaddr(" Address:", &ports_eth_addr[portid]);
3578 print_ethaddr("Destination:",
3579 (const struct ether_addr *)&dest_eth_addr[portid]);
3583 * prepare src MACs for each port.
3585 ether_addr_copy(&ports_eth_addr[portid],
3586 (struct ether_addr *)(val_eth + portid) + 1);
3589 ret = init_mem(NB_MBUF);
3591 rte_exit(EXIT_FAILURE, "init_mem failed\n");
3593 /* init one TX queue per couple (lcore,port) */
3595 for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
3596 if (rte_lcore_is_enabled(lcore_id) == 0)
3600 socketid = (uint8_t)rte_lcore_to_socket_id(lcore_id);
3604 printf("txq=%u,%d,%d ", lcore_id, queueid, socketid);
3607 txconf = &dev_info.default_txconf;
3608 txconf->offloads = local_port_conf.txmode.offloads;
3609 ret = rte_eth_tx_queue_setup(portid, queueid, nb_txd,
3612 rte_exit(EXIT_FAILURE, "rte_eth_tx_queue_setup: err=%d, "
3613 "port=%d\n", ret, portid);
3615 tx_thread[lcore_id].tx_queue_id[portid] = queueid;
3621 for (i = 0; i < n_rx_thread; i++) {
3622 lcore_id = rx_thread[i].conf.lcore_id;
3624 if (rte_lcore_is_enabled(lcore_id) == 0) {
3625 rte_exit(EXIT_FAILURE,
3626 "Cannot start Rx thread on lcore %u: lcore disabled\n",
3631 printf("\nInitializing rx queues for Rx thread %d on lcore %u ... ",
3635 /* init RX queues */
3636 for (queue = 0; queue < rx_thread[i].n_rx_queue; ++queue) {
3637 struct rte_eth_dev *dev;
3638 struct rte_eth_conf *conf;
3639 struct rte_eth_rxconf rxq_conf;
3641 portid = rx_thread[i].rx_queue_list[queue].port_id;
3642 queueid = rx_thread[i].rx_queue_list[queue].queue_id;
3643 dev = &rte_eth_devices[portid];
3644 conf = &dev->data->dev_conf;
3647 socketid = (uint8_t)rte_lcore_to_socket_id(lcore_id);
3651 printf("rxq=%d,%d,%d ", portid, queueid, socketid);
3654 rte_eth_dev_info_get(portid, &dev_info);
3655 rxq_conf = dev_info.default_rxconf;
3656 rxq_conf.offloads = conf->rxmode.offloads;
3657 ret = rte_eth_rx_queue_setup(portid, queueid, nb_rxd,
3660 pktmbuf_pool[socketid]);
3662 rte_exit(EXIT_FAILURE, "rte_eth_rx_queue_setup: err=%d, "
3663 "port=%d\n", ret, portid);
3670 RTE_ETH_FOREACH_DEV(portid) {
3671 if ((enabled_port_mask & (1 << portid)) == 0)
3675 ret = rte_eth_dev_start(portid);
3677 rte_exit(EXIT_FAILURE, "rte_eth_dev_start: err=%d, port=%d\n",
3681 * If enabled, put device in promiscuous mode.
3682 * This allows IO forwarding mode to forward packets
3683 * to itself through 2 cross-connected ports of the
3687 rte_eth_promiscuous_enable(portid);
3690 for (i = 0; i < n_rx_thread; i++) {
3691 lcore_id = rx_thread[i].conf.lcore_id;
3692 if (rte_lcore_is_enabled(lcore_id) == 0)
3695 /* check if hw packet type is supported */
3696 for (queue = 0; queue < rx_thread[i].n_rx_queue; ++queue) {
3697 portid = rx_thread[i].rx_queue_list[queue].port_id;
3698 queueid = rx_thread[i].rx_queue_list[queue].queue_id;
3700 if (parse_ptype_on) {
3701 if (!rte_eth_add_rx_callback(portid, queueid,
3702 cb_parse_ptype, NULL))
3703 rte_exit(EXIT_FAILURE,
3704 "Failed to add rx callback: "
3705 "port=%d\n", portid);
3706 } else if (!check_ptype(portid))
3707 rte_exit(EXIT_FAILURE,
3708 "Port %d cannot parse packet type.\n\n"
3709 "Please add --parse-ptype to use sw "
3710 "packet type analyzer.\n\n",
3715 check_all_ports_link_status(enabled_port_mask);
3718 printf("Starting L-Threading Model\n");
3720 #if (APP_CPU_LOAD > 0)
3721 if (cpu_load_lcore_id > 0)
3722 /* Use one lcore for cpu load collector */
3726 lthread_num_schedulers_set(nb_lcores);
3727 rte_eal_mp_remote_launch(sched_spawner, NULL, SKIP_MASTER);
3728 lthread_master_spawner(NULL);
3731 printf("Starting P-Threading Model\n");
3732 /* launch per-lcore init on every lcore */
3733 rte_eal_mp_remote_launch(pthread_run, NULL, CALL_MASTER);
3734 RTE_LCORE_FOREACH_SLAVE(lcore_id) {
3735 if (rte_eal_wait_lcore(lcore_id) < 0)