4 * Copyright(c) 2010-2015 Intel Corporation. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
17 * * Neither the name of Intel Corporation nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38 #include <sys/types.h>
40 #include <sys/queue.h>
45 #include <rte_common.h>
47 #include <rte_byteorder.h>
49 #include <rte_memory.h>
50 #include <rte_memcpy.h>
51 #include <rte_memzone.h>
53 #include <rte_per_lcore.h>
54 #include <rte_launch.h>
55 #include <rte_atomic.h>
56 #include <rte_cycles.h>
57 #include <rte_prefetch.h>
58 #include <rte_lcore.h>
59 #include <rte_per_lcore.h>
60 #include <rte_branch_prediction.h>
61 #include <rte_interrupts.h>
63 #include <rte_random.h>
64 #include <rte_debug.h>
65 #include <rte_ether.h>
66 #include <rte_ethdev.h>
68 #include <rte_mempool.h>
73 #include <rte_string_fns.h>
75 #include <cmdline_parse.h>
76 #include <cmdline_parse_etheraddr.h>
78 #define APP_LOOKUP_EXACT_MATCH 0
79 #define APP_LOOKUP_LPM 1
80 #define DO_RFC_1812_CHECKS
82 #ifndef APP_LOOKUP_METHOD
83 #define APP_LOOKUP_METHOD APP_LOOKUP_LPM
87 * When set to zero, simple forwaring path is eanbled.
88 * When set to one, optimized forwarding path is enabled.
89 * Note that LPM optimisation path uses SSE4.1 instructions.
91 #if ((APP_LOOKUP_METHOD == APP_LOOKUP_LPM) && !defined(__SSE4_1__))
92 #define ENABLE_MULTI_BUFFER_OPTIMIZE 0
94 #define ENABLE_MULTI_BUFFER_OPTIMIZE 1
97 #if (APP_LOOKUP_METHOD == APP_LOOKUP_EXACT_MATCH)
99 #elif (APP_LOOKUP_METHOD == APP_LOOKUP_LPM)
101 #include <rte_lpm6.h>
103 #error "APP_LOOKUP_METHOD set to incorrect value"
107 #define IPv6_BYTES_FMT "%02x%02x:%02x%02x:%02x%02x:%02x%02x:"\
108 "%02x%02x:%02x%02x:%02x%02x:%02x%02x"
109 #define IPv6_BYTES(addr) \
110 addr[0], addr[1], addr[2], addr[3], \
111 addr[4], addr[5], addr[6], addr[7], \
112 addr[8], addr[9], addr[10], addr[11],\
113 addr[12], addr[13],addr[14], addr[15]
117 #define RTE_LOGTYPE_L3FWD RTE_LOGTYPE_USER1
119 #define MAX_JUMBO_PKT_LEN 9600
121 #define IPV6_ADDR_LEN 16
123 #define MEMPOOL_CACHE_SIZE 256
126 * This expression is used to calculate the number of mbufs needed depending on user input, taking
127 * into account memory for rx and tx hardware rings, cache per lcore and mtable per port per lcore.
128 * RTE_MAX is used to ensure that NB_MBUF never goes below a minimum value of 8192
131 #define NB_MBUF RTE_MAX ( \
132 (nb_ports*nb_rx_queue*RTE_TEST_RX_DESC_DEFAULT + \
133 nb_ports*nb_lcores*MAX_PKT_BURST + \
134 nb_ports*n_tx_queue*RTE_TEST_TX_DESC_DEFAULT + \
135 nb_lcores*MEMPOOL_CACHE_SIZE), \
138 #define MAX_PKT_BURST 32
139 #define BURST_TX_DRAIN_US 100 /* TX drain every ~100us */
142 * Try to avoid TX buffering if we have at least MAX_TX_BURST packets to send.
144 #define MAX_TX_BURST (MAX_PKT_BURST / 2)
148 /* Configure how many packets ahead to prefetch, when reading packets */
149 #define PREFETCH_OFFSET 3
151 /* Used to mark destination port as 'invalid'. */
152 #define BAD_PORT ((uint16_t)-1)
157 * Configurable number of RX/TX ring descriptors
159 #define RTE_TEST_RX_DESC_DEFAULT 128
160 #define RTE_TEST_TX_DESC_DEFAULT 512
161 static uint16_t nb_rxd = RTE_TEST_RX_DESC_DEFAULT;
162 static uint16_t nb_txd = RTE_TEST_TX_DESC_DEFAULT;
164 /* ethernet addresses of ports */
165 static uint64_t dest_eth_addr[RTE_MAX_ETHPORTS];
166 static struct ether_addr ports_eth_addr[RTE_MAX_ETHPORTS];
168 static __m128i val_eth[RTE_MAX_ETHPORTS];
170 /* replace first 12B of the ethernet header. */
171 #define MASK_ETH 0x3f
173 /* mask of enabled ports */
174 static uint32_t enabled_port_mask = 0;
175 static int promiscuous_on = 0; /**< Ports set in promiscuous mode off by default. */
176 static int numa_on = 1; /**< NUMA is enabled by default. */
178 #if (APP_LOOKUP_METHOD == APP_LOOKUP_EXACT_MATCH)
179 static int ipv6 = 0; /**< ipv6 is false by default. */
184 struct rte_mbuf *m_table[MAX_PKT_BURST];
187 struct lcore_rx_queue {
190 } __rte_cache_aligned;
192 #define MAX_RX_QUEUE_PER_LCORE 16
193 #define MAX_TX_QUEUE_PER_PORT RTE_MAX_ETHPORTS
194 #define MAX_RX_QUEUE_PER_PORT 128
196 #define MAX_LCORE_PARAMS 1024
197 struct lcore_params {
201 } __rte_cache_aligned;
203 static struct lcore_params lcore_params_array[MAX_LCORE_PARAMS];
204 static struct lcore_params lcore_params_array_default[] = {
216 static struct lcore_params * lcore_params = lcore_params_array_default;
217 static uint16_t nb_lcore_params = sizeof(lcore_params_array_default) /
218 sizeof(lcore_params_array_default[0]);
220 static struct rte_eth_conf port_conf = {
222 .mq_mode = ETH_MQ_RX_RSS,
223 .max_rx_pkt_len = ETHER_MAX_LEN,
225 .header_split = 0, /**< Header Split disabled */
226 .hw_ip_checksum = 1, /**< IP checksum offload enabled */
227 .hw_vlan_filter = 0, /**< VLAN filtering disabled */
228 .jumbo_frame = 0, /**< Jumbo Frame Support disabled */
229 .hw_strip_crc = 0, /**< CRC stripped by hardware */
234 .rss_hf = ETH_RSS_IP,
238 .mq_mode = ETH_MQ_TX_NONE,
242 static struct rte_mempool * pktmbuf_pool[NB_SOCKETS];
244 #if (APP_LOOKUP_METHOD == APP_LOOKUP_EXACT_MATCH)
246 #ifdef RTE_MACHINE_CPUFLAG_SSE4_2
247 #include <rte_hash_crc.h>
248 #define DEFAULT_HASH_FUNC rte_hash_crc
250 #include <rte_jhash.h>
251 #define DEFAULT_HASH_FUNC rte_jhash
260 } __attribute__((__packed__));
262 union ipv4_5tuple_host {
275 #define XMM_NUM_IN_IPV6_5TUPLE 3
278 uint8_t ip_dst[IPV6_ADDR_LEN];
279 uint8_t ip_src[IPV6_ADDR_LEN];
283 } __attribute__((__packed__));
285 union ipv6_5tuple_host {
290 uint8_t ip_src[IPV6_ADDR_LEN];
291 uint8_t ip_dst[IPV6_ADDR_LEN];
296 __m128i xmm[XMM_NUM_IN_IPV6_5TUPLE];
299 struct ipv4_l3fwd_route {
300 struct ipv4_5tuple key;
304 struct ipv6_l3fwd_route {
305 struct ipv6_5tuple key;
309 static struct ipv4_l3fwd_route ipv4_l3fwd_route_array[] = {
310 {{IPv4(101,0,0,0), IPv4(100,10,0,1), 101, 11, IPPROTO_TCP}, 0},
311 {{IPv4(201,0,0,0), IPv4(200,20,0,1), 102, 12, IPPROTO_TCP}, 1},
312 {{IPv4(111,0,0,0), IPv4(100,30,0,1), 101, 11, IPPROTO_TCP}, 2},
313 {{IPv4(211,0,0,0), IPv4(200,40,0,1), 102, 12, IPPROTO_TCP}, 3},
316 static struct ipv6_l3fwd_route ipv6_l3fwd_route_array[] = {
318 {0xfe, 0x80, 0, 0, 0, 0, 0, 0, 0x02, 0x1e, 0x67, 0xff, 0xfe, 0, 0, 0},
319 {0xfe, 0x80, 0, 0, 0, 0, 0, 0, 0x02, 0x1b, 0x21, 0xff, 0xfe, 0x91, 0x38, 0x05},
320 101, 11, IPPROTO_TCP}, 0},
323 {0xfe, 0x90, 0, 0, 0, 0, 0, 0, 0x02, 0x1e, 0x67, 0xff, 0xfe, 0, 0, 0},
324 {0xfe, 0x90, 0, 0, 0, 0, 0, 0, 0x02, 0x1b, 0x21, 0xff, 0xfe, 0x91, 0x38, 0x05},
325 102, 12, IPPROTO_TCP}, 1},
328 {0xfe, 0xa0, 0, 0, 0, 0, 0, 0, 0x02, 0x1e, 0x67, 0xff, 0xfe, 0, 0, 0},
329 {0xfe, 0xa0, 0, 0, 0, 0, 0, 0, 0x02, 0x1b, 0x21, 0xff, 0xfe, 0x91, 0x38, 0x05},
330 101, 11, IPPROTO_TCP}, 2},
333 {0xfe, 0xb0, 0, 0, 0, 0, 0, 0, 0x02, 0x1e, 0x67, 0xff, 0xfe, 0, 0, 0},
334 {0xfe, 0xb0, 0, 0, 0, 0, 0, 0, 0x02, 0x1b, 0x21, 0xff, 0xfe, 0x91, 0x38, 0x05},
335 102, 12, IPPROTO_TCP}, 3},
338 typedef struct rte_hash lookup_struct_t;
339 static lookup_struct_t *ipv4_l3fwd_lookup_struct[NB_SOCKETS];
340 static lookup_struct_t *ipv6_l3fwd_lookup_struct[NB_SOCKETS];
342 #ifdef RTE_ARCH_X86_64
343 /* default to 4 million hash entries (approx) */
344 #define L3FWD_HASH_ENTRIES 1024*1024*4
346 /* 32-bit has less address-space for hugepage memory, limit to 1M entries */
347 #define L3FWD_HASH_ENTRIES 1024*1024*1
349 #define HASH_ENTRY_NUMBER_DEFAULT 4
351 static uint32_t hash_entry_number = HASH_ENTRY_NUMBER_DEFAULT;
353 static inline uint32_t
354 ipv4_hash_crc(const void *data, __rte_unused uint32_t data_len,
357 const union ipv4_5tuple_host *k;
363 p = (const uint32_t *)&k->port_src;
365 #ifdef RTE_MACHINE_CPUFLAG_SSE4_2
366 init_val = rte_hash_crc_4byte(t, init_val);
367 init_val = rte_hash_crc_4byte(k->ip_src, init_val);
368 init_val = rte_hash_crc_4byte(k->ip_dst, init_val);
369 init_val = rte_hash_crc_4byte(*p, init_val);
370 #else /* RTE_MACHINE_CPUFLAG_SSE4_2 */
371 init_val = rte_jhash_1word(t, init_val);
372 init_val = rte_jhash_1word(k->ip_src, init_val);
373 init_val = rte_jhash_1word(k->ip_dst, init_val);
374 init_val = rte_jhash_1word(*p, init_val);
375 #endif /* RTE_MACHINE_CPUFLAG_SSE4_2 */
379 static inline uint32_t
380 ipv6_hash_crc(const void *data, __rte_unused uint32_t data_len, uint32_t init_val)
382 const union ipv6_5tuple_host *k;
385 #ifdef RTE_MACHINE_CPUFLAG_SSE4_2
386 const uint32_t *ip_src0, *ip_src1, *ip_src2, *ip_src3;
387 const uint32_t *ip_dst0, *ip_dst1, *ip_dst2, *ip_dst3;
388 #endif /* RTE_MACHINE_CPUFLAG_SSE4_2 */
392 p = (const uint32_t *)&k->port_src;
394 #ifdef RTE_MACHINE_CPUFLAG_SSE4_2
395 ip_src0 = (const uint32_t *) k->ip_src;
396 ip_src1 = (const uint32_t *)(k->ip_src+4);
397 ip_src2 = (const uint32_t *)(k->ip_src+8);
398 ip_src3 = (const uint32_t *)(k->ip_src+12);
399 ip_dst0 = (const uint32_t *) k->ip_dst;
400 ip_dst1 = (const uint32_t *)(k->ip_dst+4);
401 ip_dst2 = (const uint32_t *)(k->ip_dst+8);
402 ip_dst3 = (const uint32_t *)(k->ip_dst+12);
403 init_val = rte_hash_crc_4byte(t, init_val);
404 init_val = rte_hash_crc_4byte(*ip_src0, init_val);
405 init_val = rte_hash_crc_4byte(*ip_src1, init_val);
406 init_val = rte_hash_crc_4byte(*ip_src2, init_val);
407 init_val = rte_hash_crc_4byte(*ip_src3, init_val);
408 init_val = rte_hash_crc_4byte(*ip_dst0, init_val);
409 init_val = rte_hash_crc_4byte(*ip_dst1, init_val);
410 init_val = rte_hash_crc_4byte(*ip_dst2, init_val);
411 init_val = rte_hash_crc_4byte(*ip_dst3, init_val);
412 init_val = rte_hash_crc_4byte(*p, init_val);
413 #else /* RTE_MACHINE_CPUFLAG_SSE4_2 */
414 init_val = rte_jhash_1word(t, init_val);
415 init_val = rte_jhash(k->ip_src, sizeof(uint8_t) * IPV6_ADDR_LEN, init_val);
416 init_val = rte_jhash(k->ip_dst, sizeof(uint8_t) * IPV6_ADDR_LEN, init_val);
417 init_val = rte_jhash_1word(*p, init_val);
418 #endif /* RTE_MACHINE_CPUFLAG_SSE4_2 */
422 #define IPV4_L3FWD_NUM_ROUTES \
423 (sizeof(ipv4_l3fwd_route_array) / sizeof(ipv4_l3fwd_route_array[0]))
425 #define IPV6_L3FWD_NUM_ROUTES \
426 (sizeof(ipv6_l3fwd_route_array) / sizeof(ipv6_l3fwd_route_array[0]))
428 static uint8_t ipv4_l3fwd_out_if[L3FWD_HASH_ENTRIES] __rte_cache_aligned;
429 static uint8_t ipv6_l3fwd_out_if[L3FWD_HASH_ENTRIES] __rte_cache_aligned;
433 #if (APP_LOOKUP_METHOD == APP_LOOKUP_LPM)
434 struct ipv4_l3fwd_route {
440 struct ipv6_l3fwd_route {
446 static struct ipv4_l3fwd_route ipv4_l3fwd_route_array[] = {
447 {IPv4(1,1,1,0), 24, 0},
448 {IPv4(2,1,1,0), 24, 1},
449 {IPv4(3,1,1,0), 24, 2},
450 {IPv4(4,1,1,0), 24, 3},
451 {IPv4(5,1,1,0), 24, 4},
452 {IPv4(6,1,1,0), 24, 5},
453 {IPv4(7,1,1,0), 24, 6},
454 {IPv4(8,1,1,0), 24, 7},
457 static struct ipv6_l3fwd_route ipv6_l3fwd_route_array[] = {
458 {{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}, 48, 0},
459 {{2,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}, 48, 1},
460 {{3,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}, 48, 2},
461 {{4,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}, 48, 3},
462 {{5,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}, 48, 4},
463 {{6,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}, 48, 5},
464 {{7,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}, 48, 6},
465 {{8,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}, 48, 7},
468 #define IPV4_L3FWD_NUM_ROUTES \
469 (sizeof(ipv4_l3fwd_route_array) / sizeof(ipv4_l3fwd_route_array[0]))
470 #define IPV6_L3FWD_NUM_ROUTES \
471 (sizeof(ipv6_l3fwd_route_array) / sizeof(ipv6_l3fwd_route_array[0]))
473 #define IPV4_L3FWD_LPM_MAX_RULES 1024
474 #define IPV6_L3FWD_LPM_MAX_RULES 1024
475 #define IPV6_L3FWD_LPM_NUMBER_TBL8S (1 << 16)
477 typedef struct rte_lpm lookup_struct_t;
478 typedef struct rte_lpm6 lookup6_struct_t;
479 static lookup_struct_t *ipv4_l3fwd_lookup_struct[NB_SOCKETS];
480 static lookup6_struct_t *ipv6_l3fwd_lookup_struct[NB_SOCKETS];
485 struct lcore_rx_queue rx_queue_list[MAX_RX_QUEUE_PER_LCORE];
486 uint16_t tx_queue_id[RTE_MAX_ETHPORTS];
487 struct mbuf_table tx_mbufs[RTE_MAX_ETHPORTS];
488 lookup_struct_t * ipv4_lookup_struct;
489 #if (APP_LOOKUP_METHOD == APP_LOOKUP_LPM)
490 lookup6_struct_t * ipv6_lookup_struct;
492 lookup_struct_t * ipv6_lookup_struct;
494 } __rte_cache_aligned;
496 static struct lcore_conf lcore_conf[RTE_MAX_LCORE];
498 /* Send burst of packets on an output interface */
500 send_burst(struct lcore_conf *qconf, uint16_t n, uint8_t port)
502 struct rte_mbuf **m_table;
506 queueid = qconf->tx_queue_id[port];
507 m_table = (struct rte_mbuf **)qconf->tx_mbufs[port].m_table;
509 ret = rte_eth_tx_burst(port, queueid, m_table, n);
510 if (unlikely(ret < n)) {
512 rte_pktmbuf_free(m_table[ret]);
519 /* Enqueue a single packet, and send burst if queue is filled */
521 send_single_packet(struct rte_mbuf *m, uint8_t port)
525 struct lcore_conf *qconf;
527 lcore_id = rte_lcore_id();
529 qconf = &lcore_conf[lcore_id];
530 len = qconf->tx_mbufs[port].len;
531 qconf->tx_mbufs[port].m_table[len] = m;
534 /* enough pkts to be sent */
535 if (unlikely(len == MAX_PKT_BURST)) {
536 send_burst(qconf, MAX_PKT_BURST, port);
540 qconf->tx_mbufs[port].len = len;
544 static inline __attribute__((always_inline)) void
545 send_packetsx4(struct lcore_conf *qconf, uint8_t port,
546 struct rte_mbuf *m[], uint32_t num)
550 len = qconf->tx_mbufs[port].len;
553 * If TX buffer for that queue is empty, and we have enough packets,
554 * then send them straightway.
556 if (num >= MAX_TX_BURST && len == 0) {
557 n = rte_eth_tx_burst(port, qconf->tx_queue_id[port], m, num);
558 if (unlikely(n < num)) {
560 rte_pktmbuf_free(m[n]);
567 * Put packets into TX buffer for that queue.
571 n = (n > MAX_PKT_BURST) ? MAX_PKT_BURST - len : num;
574 switch (n % FWDSTEP) {
577 qconf->tx_mbufs[port].m_table[len + j] = m[j];
580 qconf->tx_mbufs[port].m_table[len + j] = m[j];
583 qconf->tx_mbufs[port].m_table[len + j] = m[j];
586 qconf->tx_mbufs[port].m_table[len + j] = m[j];
593 /* enough pkts to be sent */
594 if (unlikely(len == MAX_PKT_BURST)) {
596 send_burst(qconf, MAX_PKT_BURST, port);
598 /* copy rest of the packets into the TX buffer. */
601 switch (len % FWDSTEP) {
604 qconf->tx_mbufs[port].m_table[j] = m[n + j];
607 qconf->tx_mbufs[port].m_table[j] = m[n + j];
610 qconf->tx_mbufs[port].m_table[j] = m[n + j];
613 qconf->tx_mbufs[port].m_table[j] = m[n + j];
619 qconf->tx_mbufs[port].len = len;
622 #ifdef DO_RFC_1812_CHECKS
624 is_valid_ipv4_pkt(struct ipv4_hdr *pkt, uint32_t link_len)
626 /* From http://www.rfc-editor.org/rfc/rfc1812.txt section 5.2.2 */
628 * 1. The packet length reported by the Link Layer must be large
629 * enough to hold the minimum length legal IP datagram (20 bytes).
631 if (link_len < sizeof(struct ipv4_hdr))
634 /* 2. The IP checksum must be correct. */
635 /* this is checked in H/W */
638 * 3. The IP version number must be 4. If the version number is not 4
639 * then the packet may be another version of IP, such as IPng or
642 if (((pkt->version_ihl) >> 4) != 4)
645 * 4. The IP header length field must be large enough to hold the
646 * minimum length legal IP datagram (20 bytes = 5 words).
648 if ((pkt->version_ihl & 0xf) < 5)
652 * 5. The IP total length field must be large enough to hold the IP
653 * datagram header, whose length is specified in the IP header length
656 if (rte_cpu_to_be_16(pkt->total_length) < sizeof(struct ipv4_hdr))
663 #if (APP_LOOKUP_METHOD == APP_LOOKUP_EXACT_MATCH)
665 static __m128i mask0;
666 static __m128i mask1;
667 static __m128i mask2;
668 static inline uint8_t
669 get_ipv4_dst_port(void *ipv4_hdr, uint8_t portid, lookup_struct_t * ipv4_l3fwd_lookup_struct)
672 union ipv4_5tuple_host key;
674 ipv4_hdr = (uint8_t *)ipv4_hdr + offsetof(struct ipv4_hdr, time_to_live);
675 __m128i data = _mm_loadu_si128((__m128i*)(ipv4_hdr));
676 /* Get 5 tuple: dst port, src port, dst IP address, src IP address and protocol */
677 key.xmm = _mm_and_si128(data, mask0);
678 /* Find destination port */
679 ret = rte_hash_lookup(ipv4_l3fwd_lookup_struct, (const void *)&key);
680 return (uint8_t)((ret < 0)? portid : ipv4_l3fwd_out_if[ret]);
683 static inline uint8_t
684 get_ipv6_dst_port(void *ipv6_hdr, uint8_t portid, lookup_struct_t * ipv6_l3fwd_lookup_struct)
687 union ipv6_5tuple_host key;
689 ipv6_hdr = (uint8_t *)ipv6_hdr + offsetof(struct ipv6_hdr, payload_len);
690 __m128i data0 = _mm_loadu_si128((__m128i*)(ipv6_hdr));
691 __m128i data1 = _mm_loadu_si128((__m128i*)(((uint8_t*)ipv6_hdr)+sizeof(__m128i)));
692 __m128i data2 = _mm_loadu_si128((__m128i*)(((uint8_t*)ipv6_hdr)+sizeof(__m128i)+sizeof(__m128i)));
693 /* Get part of 5 tuple: src IP address lower 96 bits and protocol */
694 key.xmm[0] = _mm_and_si128(data0, mask1);
695 /* Get part of 5 tuple: dst IP address lower 96 bits and src IP address higher 32 bits */
697 /* Get part of 5 tuple: dst port and src port and dst IP address higher 32 bits */
698 key.xmm[2] = _mm_and_si128(data2, mask2);
700 /* Find destination port */
701 ret = rte_hash_lookup(ipv6_l3fwd_lookup_struct, (const void *)&key);
702 return (uint8_t)((ret < 0)? portid : ipv6_l3fwd_out_if[ret]);
706 #if (APP_LOOKUP_METHOD == APP_LOOKUP_LPM)
708 static inline uint8_t
709 get_ipv4_dst_port(void *ipv4_hdr, uint8_t portid, lookup_struct_t * ipv4_l3fwd_lookup_struct)
713 return (uint8_t) ((rte_lpm_lookup(ipv4_l3fwd_lookup_struct,
714 rte_be_to_cpu_32(((struct ipv4_hdr *)ipv4_hdr)->dst_addr),
715 &next_hop) == 0) ? next_hop : portid);
718 static inline uint8_t
719 get_ipv6_dst_port(void *ipv6_hdr, uint8_t portid, lookup6_struct_t * ipv6_l3fwd_lookup_struct)
722 return (uint8_t) ((rte_lpm6_lookup(ipv6_l3fwd_lookup_struct,
723 ((struct ipv6_hdr*)ipv6_hdr)->dst_addr, &next_hop) == 0)?
728 static inline void l3fwd_simple_forward(struct rte_mbuf *m, uint8_t portid,
729 struct lcore_conf *qconf) __attribute__((unused));
731 #if ((APP_LOOKUP_METHOD == APP_LOOKUP_EXACT_MATCH) && \
732 (ENABLE_MULTI_BUFFER_OPTIMIZE == 1))
734 #define MASK_ALL_PKTS 0xff
735 #define EXCLUDE_1ST_PKT 0xfe
736 #define EXCLUDE_2ND_PKT 0xfd
737 #define EXCLUDE_3RD_PKT 0xfb
738 #define EXCLUDE_4TH_PKT 0xf7
739 #define EXCLUDE_5TH_PKT 0xef
740 #define EXCLUDE_6TH_PKT 0xdf
741 #define EXCLUDE_7TH_PKT 0xbf
742 #define EXCLUDE_8TH_PKT 0x7f
745 simple_ipv4_fwd_8pkts(struct rte_mbuf *m[8], uint8_t portid, struct lcore_conf *qconf)
747 struct ether_hdr *eth_hdr[8];
748 struct ipv4_hdr *ipv4_hdr[8];
751 union ipv4_5tuple_host key[8];
754 eth_hdr[0] = rte_pktmbuf_mtod(m[0], struct ether_hdr *);
755 eth_hdr[1] = rte_pktmbuf_mtod(m[1], struct ether_hdr *);
756 eth_hdr[2] = rte_pktmbuf_mtod(m[2], struct ether_hdr *);
757 eth_hdr[3] = rte_pktmbuf_mtod(m[3], struct ether_hdr *);
758 eth_hdr[4] = rte_pktmbuf_mtod(m[4], struct ether_hdr *);
759 eth_hdr[5] = rte_pktmbuf_mtod(m[5], struct ether_hdr *);
760 eth_hdr[6] = rte_pktmbuf_mtod(m[6], struct ether_hdr *);
761 eth_hdr[7] = rte_pktmbuf_mtod(m[7], struct ether_hdr *);
763 /* Handle IPv4 headers.*/
764 ipv4_hdr[0] = rte_pktmbuf_mtod_offset(m[0], struct ipv4_hdr *,
765 sizeof(struct ether_hdr));
766 ipv4_hdr[1] = rte_pktmbuf_mtod_offset(m[1], struct ipv4_hdr *,
767 sizeof(struct ether_hdr));
768 ipv4_hdr[2] = rte_pktmbuf_mtod_offset(m[2], struct ipv4_hdr *,
769 sizeof(struct ether_hdr));
770 ipv4_hdr[3] = rte_pktmbuf_mtod_offset(m[3], struct ipv4_hdr *,
771 sizeof(struct ether_hdr));
772 ipv4_hdr[4] = rte_pktmbuf_mtod_offset(m[4], struct ipv4_hdr *,
773 sizeof(struct ether_hdr));
774 ipv4_hdr[5] = rte_pktmbuf_mtod_offset(m[5], struct ipv4_hdr *,
775 sizeof(struct ether_hdr));
776 ipv4_hdr[6] = rte_pktmbuf_mtod_offset(m[6], struct ipv4_hdr *,
777 sizeof(struct ether_hdr));
778 ipv4_hdr[7] = rte_pktmbuf_mtod_offset(m[7], struct ipv4_hdr *,
779 sizeof(struct ether_hdr));
781 #ifdef DO_RFC_1812_CHECKS
782 /* Check to make sure the packet is valid (RFC1812) */
783 uint8_t valid_mask = MASK_ALL_PKTS;
784 if (is_valid_ipv4_pkt(ipv4_hdr[0], m[0]->pkt_len) < 0) {
785 rte_pktmbuf_free(m[0]);
786 valid_mask &= EXCLUDE_1ST_PKT;
788 if (is_valid_ipv4_pkt(ipv4_hdr[1], m[1]->pkt_len) < 0) {
789 rte_pktmbuf_free(m[1]);
790 valid_mask &= EXCLUDE_2ND_PKT;
792 if (is_valid_ipv4_pkt(ipv4_hdr[2], m[2]->pkt_len) < 0) {
793 rte_pktmbuf_free(m[2]);
794 valid_mask &= EXCLUDE_3RD_PKT;
796 if (is_valid_ipv4_pkt(ipv4_hdr[3], m[3]->pkt_len) < 0) {
797 rte_pktmbuf_free(m[3]);
798 valid_mask &= EXCLUDE_4TH_PKT;
800 if (is_valid_ipv4_pkt(ipv4_hdr[4], m[4]->pkt_len) < 0) {
801 rte_pktmbuf_free(m[4]);
802 valid_mask &= EXCLUDE_5TH_PKT;
804 if (is_valid_ipv4_pkt(ipv4_hdr[5], m[5]->pkt_len) < 0) {
805 rte_pktmbuf_free(m[5]);
806 valid_mask &= EXCLUDE_6TH_PKT;
808 if (is_valid_ipv4_pkt(ipv4_hdr[6], m[6]->pkt_len) < 0) {
809 rte_pktmbuf_free(m[6]);
810 valid_mask &= EXCLUDE_7TH_PKT;
812 if (is_valid_ipv4_pkt(ipv4_hdr[7], m[7]->pkt_len) < 0) {
813 rte_pktmbuf_free(m[7]);
814 valid_mask &= EXCLUDE_8TH_PKT;
816 if (unlikely(valid_mask != MASK_ALL_PKTS)) {
817 if (valid_mask == 0){
821 for (i = 0; i < 8; i++) {
822 if ((0x1 << i) & valid_mask) {
823 l3fwd_simple_forward(m[i], portid, qconf);
829 #endif // End of #ifdef DO_RFC_1812_CHECKS
831 data[0] = _mm_loadu_si128(rte_pktmbuf_mtod_offset(m[0], __m128i *,
832 sizeof(struct ether_hdr) +
833 offsetof(struct ipv4_hdr, time_to_live)));
834 data[1] = _mm_loadu_si128(rte_pktmbuf_mtod_offset(m[1], __m128i *,
835 sizeof(struct ether_hdr) +
836 offsetof(struct ipv4_hdr, time_to_live)));
837 data[2] = _mm_loadu_si128(rte_pktmbuf_mtod_offset(m[2], __m128i *,
838 sizeof(struct ether_hdr) +
839 offsetof(struct ipv4_hdr, time_to_live)));
840 data[3] = _mm_loadu_si128(rte_pktmbuf_mtod_offset(m[3], __m128i *,
841 sizeof(struct ether_hdr) +
842 offsetof(struct ipv4_hdr, time_to_live)));
843 data[4] = _mm_loadu_si128(rte_pktmbuf_mtod_offset(m[4], __m128i *,
844 sizeof(struct ether_hdr) +
845 offsetof(struct ipv4_hdr, time_to_live)));
846 data[5] = _mm_loadu_si128(rte_pktmbuf_mtod_offset(m[5], __m128i *,
847 sizeof(struct ether_hdr) +
848 offsetof(struct ipv4_hdr, time_to_live)));
849 data[6] = _mm_loadu_si128(rte_pktmbuf_mtod_offset(m[6], __m128i *,
850 sizeof(struct ether_hdr) +
851 offsetof(struct ipv4_hdr, time_to_live)));
852 data[7] = _mm_loadu_si128(rte_pktmbuf_mtod_offset(m[7], __m128i *,
853 sizeof(struct ether_hdr) +
854 offsetof(struct ipv4_hdr, time_to_live)));
856 key[0].xmm = _mm_and_si128(data[0], mask0);
857 key[1].xmm = _mm_and_si128(data[1], mask0);
858 key[2].xmm = _mm_and_si128(data[2], mask0);
859 key[3].xmm = _mm_and_si128(data[3], mask0);
860 key[4].xmm = _mm_and_si128(data[4], mask0);
861 key[5].xmm = _mm_and_si128(data[5], mask0);
862 key[6].xmm = _mm_and_si128(data[6], mask0);
863 key[7].xmm = _mm_and_si128(data[7], mask0);
865 const void *key_array[8] = {&key[0], &key[1], &key[2], &key[3],
866 &key[4], &key[5], &key[6], &key[7]};
868 rte_hash_lookup_multi(qconf->ipv4_lookup_struct, &key_array[0], 8, ret);
869 dst_port[0] = (uint8_t) ((ret[0] < 0) ? portid : ipv4_l3fwd_out_if[ret[0]]);
870 dst_port[1] = (uint8_t) ((ret[1] < 0) ? portid : ipv4_l3fwd_out_if[ret[1]]);
871 dst_port[2] = (uint8_t) ((ret[2] < 0) ? portid : ipv4_l3fwd_out_if[ret[2]]);
872 dst_port[3] = (uint8_t) ((ret[3] < 0) ? portid : ipv4_l3fwd_out_if[ret[3]]);
873 dst_port[4] = (uint8_t) ((ret[4] < 0) ? portid : ipv4_l3fwd_out_if[ret[4]]);
874 dst_port[5] = (uint8_t) ((ret[5] < 0) ? portid : ipv4_l3fwd_out_if[ret[5]]);
875 dst_port[6] = (uint8_t) ((ret[6] < 0) ? portid : ipv4_l3fwd_out_if[ret[6]]);
876 dst_port[7] = (uint8_t) ((ret[7] < 0) ? portid : ipv4_l3fwd_out_if[ret[7]]);
878 if (dst_port[0] >= RTE_MAX_ETHPORTS || (enabled_port_mask & 1 << dst_port[0]) == 0)
879 dst_port[0] = portid;
880 if (dst_port[1] >= RTE_MAX_ETHPORTS || (enabled_port_mask & 1 << dst_port[1]) == 0)
881 dst_port[1] = portid;
882 if (dst_port[2] >= RTE_MAX_ETHPORTS || (enabled_port_mask & 1 << dst_port[2]) == 0)
883 dst_port[2] = portid;
884 if (dst_port[3] >= RTE_MAX_ETHPORTS || (enabled_port_mask & 1 << dst_port[3]) == 0)
885 dst_port[3] = portid;
886 if (dst_port[4] >= RTE_MAX_ETHPORTS || (enabled_port_mask & 1 << dst_port[4]) == 0)
887 dst_port[4] = portid;
888 if (dst_port[5] >= RTE_MAX_ETHPORTS || (enabled_port_mask & 1 << dst_port[5]) == 0)
889 dst_port[5] = portid;
890 if (dst_port[6] >= RTE_MAX_ETHPORTS || (enabled_port_mask & 1 << dst_port[6]) == 0)
891 dst_port[6] = portid;
892 if (dst_port[7] >= RTE_MAX_ETHPORTS || (enabled_port_mask & 1 << dst_port[7]) == 0)
893 dst_port[7] = portid;
895 #ifdef DO_RFC_1812_CHECKS
896 /* Update time to live and header checksum */
897 --(ipv4_hdr[0]->time_to_live);
898 --(ipv4_hdr[1]->time_to_live);
899 --(ipv4_hdr[2]->time_to_live);
900 --(ipv4_hdr[3]->time_to_live);
901 ++(ipv4_hdr[0]->hdr_checksum);
902 ++(ipv4_hdr[1]->hdr_checksum);
903 ++(ipv4_hdr[2]->hdr_checksum);
904 ++(ipv4_hdr[3]->hdr_checksum);
905 --(ipv4_hdr[4]->time_to_live);
906 --(ipv4_hdr[5]->time_to_live);
907 --(ipv4_hdr[6]->time_to_live);
908 --(ipv4_hdr[7]->time_to_live);
909 ++(ipv4_hdr[4]->hdr_checksum);
910 ++(ipv4_hdr[5]->hdr_checksum);
911 ++(ipv4_hdr[6]->hdr_checksum);
912 ++(ipv4_hdr[7]->hdr_checksum);
916 *(uint64_t *)ð_hdr[0]->d_addr = dest_eth_addr[dst_port[0]];
917 *(uint64_t *)ð_hdr[1]->d_addr = dest_eth_addr[dst_port[1]];
918 *(uint64_t *)ð_hdr[2]->d_addr = dest_eth_addr[dst_port[2]];
919 *(uint64_t *)ð_hdr[3]->d_addr = dest_eth_addr[dst_port[3]];
920 *(uint64_t *)ð_hdr[4]->d_addr = dest_eth_addr[dst_port[4]];
921 *(uint64_t *)ð_hdr[5]->d_addr = dest_eth_addr[dst_port[5]];
922 *(uint64_t *)ð_hdr[6]->d_addr = dest_eth_addr[dst_port[6]];
923 *(uint64_t *)ð_hdr[7]->d_addr = dest_eth_addr[dst_port[7]];
926 ether_addr_copy(&ports_eth_addr[dst_port[0]], ð_hdr[0]->s_addr);
927 ether_addr_copy(&ports_eth_addr[dst_port[1]], ð_hdr[1]->s_addr);
928 ether_addr_copy(&ports_eth_addr[dst_port[2]], ð_hdr[2]->s_addr);
929 ether_addr_copy(&ports_eth_addr[dst_port[3]], ð_hdr[3]->s_addr);
930 ether_addr_copy(&ports_eth_addr[dst_port[4]], ð_hdr[4]->s_addr);
931 ether_addr_copy(&ports_eth_addr[dst_port[5]], ð_hdr[5]->s_addr);
932 ether_addr_copy(&ports_eth_addr[dst_port[6]], ð_hdr[6]->s_addr);
933 ether_addr_copy(&ports_eth_addr[dst_port[7]], ð_hdr[7]->s_addr);
935 send_single_packet(m[0], (uint8_t)dst_port[0]);
936 send_single_packet(m[1], (uint8_t)dst_port[1]);
937 send_single_packet(m[2], (uint8_t)dst_port[2]);
938 send_single_packet(m[3], (uint8_t)dst_port[3]);
939 send_single_packet(m[4], (uint8_t)dst_port[4]);
940 send_single_packet(m[5], (uint8_t)dst_port[5]);
941 send_single_packet(m[6], (uint8_t)dst_port[6]);
942 send_single_packet(m[7], (uint8_t)dst_port[7]);
946 static inline void get_ipv6_5tuple(struct rte_mbuf* m0, __m128i mask0, __m128i mask1,
947 union ipv6_5tuple_host * key)
949 __m128i tmpdata0 = _mm_loadu_si128(rte_pktmbuf_mtod_offset(m0, __m128i *, sizeof(struct ether_hdr) + offsetof(struct ipv6_hdr, payload_len)));
950 __m128i tmpdata1 = _mm_loadu_si128(rte_pktmbuf_mtod_offset(m0, __m128i *, sizeof(struct ether_hdr) + offsetof(struct ipv6_hdr, payload_len) + sizeof(__m128i)));
951 __m128i tmpdata2 = _mm_loadu_si128(rte_pktmbuf_mtod_offset(m0, __m128i *, sizeof(struct ether_hdr) + offsetof(struct ipv6_hdr, payload_len) + sizeof(__m128i) + sizeof(__m128i)));
952 key->xmm[0] = _mm_and_si128(tmpdata0, mask0);
953 key->xmm[1] = tmpdata1;
954 key->xmm[2] = _mm_and_si128(tmpdata2, mask1);
959 simple_ipv6_fwd_8pkts(struct rte_mbuf *m[8], uint8_t portid, struct lcore_conf *qconf)
961 struct ether_hdr *eth_hdr[8];
962 __attribute__((unused)) struct ipv6_hdr *ipv6_hdr[8];
965 union ipv6_5tuple_host key[8];
967 eth_hdr[0] = rte_pktmbuf_mtod(m[0], struct ether_hdr *);
968 eth_hdr[1] = rte_pktmbuf_mtod(m[1], struct ether_hdr *);
969 eth_hdr[2] = rte_pktmbuf_mtod(m[2], struct ether_hdr *);
970 eth_hdr[3] = rte_pktmbuf_mtod(m[3], struct ether_hdr *);
971 eth_hdr[4] = rte_pktmbuf_mtod(m[4], struct ether_hdr *);
972 eth_hdr[5] = rte_pktmbuf_mtod(m[5], struct ether_hdr *);
973 eth_hdr[6] = rte_pktmbuf_mtod(m[6], struct ether_hdr *);
974 eth_hdr[7] = rte_pktmbuf_mtod(m[7], struct ether_hdr *);
976 /* Handle IPv6 headers.*/
977 ipv6_hdr[0] = rte_pktmbuf_mtod_offset(m[0], struct ipv6_hdr *,
978 sizeof(struct ether_hdr));
979 ipv6_hdr[1] = rte_pktmbuf_mtod_offset(m[1], struct ipv6_hdr *,
980 sizeof(struct ether_hdr));
981 ipv6_hdr[2] = rte_pktmbuf_mtod_offset(m[2], struct ipv6_hdr *,
982 sizeof(struct ether_hdr));
983 ipv6_hdr[3] = rte_pktmbuf_mtod_offset(m[3], struct ipv6_hdr *,
984 sizeof(struct ether_hdr));
985 ipv6_hdr[4] = rte_pktmbuf_mtod_offset(m[4], struct ipv6_hdr *,
986 sizeof(struct ether_hdr));
987 ipv6_hdr[5] = rte_pktmbuf_mtod_offset(m[5], struct ipv6_hdr *,
988 sizeof(struct ether_hdr));
989 ipv6_hdr[6] = rte_pktmbuf_mtod_offset(m[6], struct ipv6_hdr *,
990 sizeof(struct ether_hdr));
991 ipv6_hdr[7] = rte_pktmbuf_mtod_offset(m[7], struct ipv6_hdr *,
992 sizeof(struct ether_hdr));
994 get_ipv6_5tuple(m[0], mask1, mask2, &key[0]);
995 get_ipv6_5tuple(m[1], mask1, mask2, &key[1]);
996 get_ipv6_5tuple(m[2], mask1, mask2, &key[2]);
997 get_ipv6_5tuple(m[3], mask1, mask2, &key[3]);
998 get_ipv6_5tuple(m[4], mask1, mask2, &key[4]);
999 get_ipv6_5tuple(m[5], mask1, mask2, &key[5]);
1000 get_ipv6_5tuple(m[6], mask1, mask2, &key[6]);
1001 get_ipv6_5tuple(m[7], mask1, mask2, &key[7]);
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_multi(qconf->ipv6_lookup_struct, &key_array[0], 4, ret);
1007 dst_port[0] = (uint8_t) ((ret[0] < 0) ? portid:ipv6_l3fwd_out_if[ret[0]]);
1008 dst_port[1] = (uint8_t) ((ret[1] < 0) ? portid:ipv6_l3fwd_out_if[ret[1]]);
1009 dst_port[2] = (uint8_t) ((ret[2] < 0) ? portid:ipv6_l3fwd_out_if[ret[2]]);
1010 dst_port[3] = (uint8_t) ((ret[3] < 0) ? portid:ipv6_l3fwd_out_if[ret[3]]);
1011 dst_port[4] = (uint8_t) ((ret[4] < 0) ? portid:ipv6_l3fwd_out_if[ret[4]]);
1012 dst_port[5] = (uint8_t) ((ret[5] < 0) ? portid:ipv6_l3fwd_out_if[ret[5]]);
1013 dst_port[6] = (uint8_t) ((ret[6] < 0) ? portid:ipv6_l3fwd_out_if[ret[6]]);
1014 dst_port[7] = (uint8_t) ((ret[7] < 0) ? portid:ipv6_l3fwd_out_if[ret[7]]);
1016 if (dst_port[0] >= RTE_MAX_ETHPORTS || (enabled_port_mask & 1 << dst_port[0]) == 0)
1017 dst_port[0] = portid;
1018 if (dst_port[1] >= RTE_MAX_ETHPORTS || (enabled_port_mask & 1 << dst_port[1]) == 0)
1019 dst_port[1] = portid;
1020 if (dst_port[2] >= RTE_MAX_ETHPORTS || (enabled_port_mask & 1 << dst_port[2]) == 0)
1021 dst_port[2] = portid;
1022 if (dst_port[3] >= RTE_MAX_ETHPORTS || (enabled_port_mask & 1 << dst_port[3]) == 0)
1023 dst_port[3] = portid;
1024 if (dst_port[4] >= RTE_MAX_ETHPORTS || (enabled_port_mask & 1 << dst_port[4]) == 0)
1025 dst_port[4] = portid;
1026 if (dst_port[5] >= RTE_MAX_ETHPORTS || (enabled_port_mask & 1 << dst_port[5]) == 0)
1027 dst_port[5] = portid;
1028 if (dst_port[6] >= RTE_MAX_ETHPORTS || (enabled_port_mask & 1 << dst_port[6]) == 0)
1029 dst_port[6] = portid;
1030 if (dst_port[7] >= RTE_MAX_ETHPORTS || (enabled_port_mask & 1 << dst_port[7]) == 0)
1031 dst_port[7] = portid;
1034 *(uint64_t *)ð_hdr[0]->d_addr = dest_eth_addr[dst_port[0]];
1035 *(uint64_t *)ð_hdr[1]->d_addr = dest_eth_addr[dst_port[1]];
1036 *(uint64_t *)ð_hdr[2]->d_addr = dest_eth_addr[dst_port[2]];
1037 *(uint64_t *)ð_hdr[3]->d_addr = dest_eth_addr[dst_port[3]];
1038 *(uint64_t *)ð_hdr[4]->d_addr = dest_eth_addr[dst_port[4]];
1039 *(uint64_t *)ð_hdr[5]->d_addr = dest_eth_addr[dst_port[5]];
1040 *(uint64_t *)ð_hdr[6]->d_addr = dest_eth_addr[dst_port[6]];
1041 *(uint64_t *)ð_hdr[7]->d_addr = dest_eth_addr[dst_port[7]];
1044 ether_addr_copy(&ports_eth_addr[dst_port[0]], ð_hdr[0]->s_addr);
1045 ether_addr_copy(&ports_eth_addr[dst_port[1]], ð_hdr[1]->s_addr);
1046 ether_addr_copy(&ports_eth_addr[dst_port[2]], ð_hdr[2]->s_addr);
1047 ether_addr_copy(&ports_eth_addr[dst_port[3]], ð_hdr[3]->s_addr);
1048 ether_addr_copy(&ports_eth_addr[dst_port[4]], ð_hdr[4]->s_addr);
1049 ether_addr_copy(&ports_eth_addr[dst_port[5]], ð_hdr[5]->s_addr);
1050 ether_addr_copy(&ports_eth_addr[dst_port[6]], ð_hdr[6]->s_addr);
1051 ether_addr_copy(&ports_eth_addr[dst_port[7]], ð_hdr[7]->s_addr);
1053 send_single_packet(m[0], (uint8_t)dst_port[0]);
1054 send_single_packet(m[1], (uint8_t)dst_port[1]);
1055 send_single_packet(m[2], (uint8_t)dst_port[2]);
1056 send_single_packet(m[3], (uint8_t)dst_port[3]);
1057 send_single_packet(m[4], (uint8_t)dst_port[4]);
1058 send_single_packet(m[5], (uint8_t)dst_port[5]);
1059 send_single_packet(m[6], (uint8_t)dst_port[6]);
1060 send_single_packet(m[7], (uint8_t)dst_port[7]);
1063 #endif /* APP_LOOKUP_METHOD */
1065 static inline __attribute__((always_inline)) void
1066 l3fwd_simple_forward(struct rte_mbuf *m, uint8_t portid, struct lcore_conf *qconf)
1068 struct ether_hdr *eth_hdr;
1069 struct ipv4_hdr *ipv4_hdr;
1072 eth_hdr = rte_pktmbuf_mtod(m, struct ether_hdr *);
1075 if (RTE_ETH_IS_IPV4_HDR(m->packet_type)) {
1077 if (m->ol_flags & PKT_RX_IPV4_HDR) {
1079 /* Handle IPv4 headers.*/
1080 ipv4_hdr = rte_pktmbuf_mtod_offset(m, struct ipv4_hdr *,
1081 sizeof(struct ether_hdr));
1083 #ifdef DO_RFC_1812_CHECKS
1084 /* Check to make sure the packet is valid (RFC1812) */
1085 if (is_valid_ipv4_pkt(ipv4_hdr, m->pkt_len) < 0) {
1086 rte_pktmbuf_free(m);
1091 dst_port = get_ipv4_dst_port(ipv4_hdr, portid,
1092 qconf->ipv4_lookup_struct);
1093 if (dst_port >= RTE_MAX_ETHPORTS ||
1094 (enabled_port_mask & 1 << dst_port) == 0)
1097 #ifdef DO_RFC_1812_CHECKS
1098 /* Update time to live and header checksum */
1099 --(ipv4_hdr->time_to_live);
1100 ++(ipv4_hdr->hdr_checksum);
1103 *(uint64_t *)ð_hdr->d_addr = dest_eth_addr[dst_port];
1106 ether_addr_copy(&ports_eth_addr[dst_port], ð_hdr->s_addr);
1108 send_single_packet(m, dst_port);
1110 } else if (RTE_ETH_IS_IPV6_HDR(m->packet_type)) {
1114 /* Handle IPv6 headers.*/
1115 struct ipv6_hdr *ipv6_hdr;
1117 ipv6_hdr = rte_pktmbuf_mtod_offset(m, struct ipv6_hdr *,
1118 sizeof(struct ether_hdr));
1120 dst_port = get_ipv6_dst_port(ipv6_hdr, portid, qconf->ipv6_lookup_struct);
1122 if (dst_port >= RTE_MAX_ETHPORTS || (enabled_port_mask & 1 << dst_port) == 0)
1126 *(uint64_t *)ð_hdr->d_addr = dest_eth_addr[dst_port];
1129 ether_addr_copy(&ports_eth_addr[dst_port], ð_hdr->s_addr);
1131 send_single_packet(m, dst_port);
1134 /* Free the mbuf that contains non-IPV4/IPV6 packet */
1135 rte_pktmbuf_free(m);
1141 #ifdef DO_RFC_1812_CHECKS
1143 #define IPV4_MIN_VER_IHL 0x45
1144 #define IPV4_MAX_VER_IHL 0x4f
1145 #define IPV4_MAX_VER_IHL_DIFF (IPV4_MAX_VER_IHL - IPV4_MIN_VER_IHL)
1147 /* Minimum value of IPV4 total length (20B) in network byte order. */
1148 #define IPV4_MIN_LEN_BE (sizeof(struct ipv4_hdr) << 8)
1151 * From http://www.rfc-editor.org/rfc/rfc1812.txt section 5.2.2:
1152 * - The IP version number must be 4.
1153 * - The IP header length field must be large enough to hold the
1154 * minimum length legal IP datagram (20 bytes = 5 words).
1155 * - The IP total length field must be large enough to hold the IP
1156 * datagram header, whose length is specified in the IP header length
1158 * If we encounter invalid IPV4 packet, then set destination port for it
1159 * to BAD_PORT value.
1161 static inline __attribute__((always_inline)) void
1163 rfc1812_process(struct ipv4_hdr *ipv4_hdr, uint16_t *dp, uint32_t ptype)
1165 rfc1812_process(struct ipv4_hdr *ipv4_hdr, uint16_t *dp, uint32_t flags)
1171 if (RTE_ETH_IS_IPV4_HDR(ptype)) {
1173 if ((flags & PKT_RX_IPV4_HDR) != 0) {
1175 ihl = ipv4_hdr->version_ihl - IPV4_MIN_VER_IHL;
1177 ipv4_hdr->time_to_live--;
1178 ipv4_hdr->hdr_checksum++;
1180 if (ihl > IPV4_MAX_VER_IHL_DIFF ||
1181 ((uint8_t)ipv4_hdr->total_length == 0 &&
1182 ipv4_hdr->total_length < IPV4_MIN_LEN_BE)) {
1189 #define rfc1812_process(mb, dp) do { } while (0)
1190 #endif /* DO_RFC_1812_CHECKS */
1193 #if ((APP_LOOKUP_METHOD == APP_LOOKUP_LPM) && \
1194 (ENABLE_MULTI_BUFFER_OPTIMIZE == 1))
1196 static inline __attribute__((always_inline)) uint16_t
1197 get_dst_port(const struct lcore_conf *qconf, struct rte_mbuf *pkt,
1198 uint32_t dst_ipv4, uint8_t portid)
1201 struct ipv6_hdr *ipv6_hdr;
1202 struct ether_hdr *eth_hdr;
1205 if (RTE_ETH_IS_IPV4_HDR(pkt->packet_type)) {
1207 if (pkt->ol_flags & PKT_RX_IPV4_HDR) {
1209 if (rte_lpm_lookup(qconf->ipv4_lookup_struct, dst_ipv4,
1213 } else if (RTE_ETH_IS_IPV6_HDR(pkt->packet_type)) {
1215 } else if (pkt->ol_flags & PKT_RX_IPV6_HDR) {
1217 eth_hdr = rte_pktmbuf_mtod(pkt, struct ether_hdr *);
1218 ipv6_hdr = (struct ipv6_hdr *)(eth_hdr + 1);
1219 if (rte_lpm6_lookup(qconf->ipv6_lookup_struct,
1220 ipv6_hdr->dst_addr, &next_hop) != 0)
1230 process_packet(struct lcore_conf *qconf, struct rte_mbuf *pkt,
1231 uint16_t *dst_port, uint8_t portid)
1233 struct ether_hdr *eth_hdr;
1234 struct ipv4_hdr *ipv4_hdr;
1239 eth_hdr = rte_pktmbuf_mtod(pkt, struct ether_hdr *);
1240 ipv4_hdr = (struct ipv4_hdr *)(eth_hdr + 1);
1242 dst_ipv4 = ipv4_hdr->dst_addr;
1243 dst_ipv4 = rte_be_to_cpu_32(dst_ipv4);
1244 dp = get_dst_port(qconf, pkt, dst_ipv4, portid);
1246 te = _mm_load_si128((__m128i *)eth_hdr);
1251 rfc1812_process(ipv4_hdr, dst_port, pkt->packet_type);
1253 rfc1812_process(ipv4_hdr, dst_port, pkt->ol_flags);
1256 te = _mm_blend_epi16(te, ve, MASK_ETH);
1257 _mm_store_si128((__m128i *)eth_hdr, te);
1262 * Read packet_type and destination IPV4 addresses from 4 mbufs.
1265 processx4_step1(struct rte_mbuf *pkt[FWDSTEP],
1267 uint32_t *ipv4_flag)
1269 struct ipv4_hdr *ipv4_hdr;
1270 struct ether_hdr *eth_hdr;
1271 uint32_t x0, x1, x2, x3;
1273 eth_hdr = rte_pktmbuf_mtod(pkt[0], struct ether_hdr *);
1274 ipv4_hdr = (struct ipv4_hdr *)(eth_hdr + 1);
1275 x0 = ipv4_hdr->dst_addr;
1276 ipv4_flag[0] = pkt[0]->packet_type & RTE_PTYPE_L3_IPV4;
1278 eth_hdr = rte_pktmbuf_mtod(pkt[1], struct ether_hdr *);
1279 ipv4_hdr = (struct ipv4_hdr *)(eth_hdr + 1);
1280 x1 = ipv4_hdr->dst_addr;
1281 ipv4_flag[0] &= pkt[1]->packet_type;
1283 eth_hdr = rte_pktmbuf_mtod(pkt[2], struct ether_hdr *);
1284 ipv4_hdr = (struct ipv4_hdr *)(eth_hdr + 1);
1285 x2 = ipv4_hdr->dst_addr;
1286 ipv4_flag[0] &= pkt[2]->packet_type;
1288 eth_hdr = rte_pktmbuf_mtod(pkt[3], struct ether_hdr *);
1289 ipv4_hdr = (struct ipv4_hdr *)(eth_hdr + 1);
1290 x3 = ipv4_hdr->dst_addr;
1291 ipv4_flag[0] &= pkt[3]->packet_type;
1293 dip[0] = _mm_set_epi32(x3, x2, x1, x0);
1295 #else /* RTE_NEXT_ABI */
1297 * Read ol_flags and destination IPV4 addresses from 4 mbufs.
1300 processx4_step1(struct rte_mbuf *pkt[FWDSTEP], __m128i *dip, uint32_t *flag)
1302 struct ipv4_hdr *ipv4_hdr;
1303 struct ether_hdr *eth_hdr;
1304 uint32_t x0, x1, x2, x3;
1306 eth_hdr = rte_pktmbuf_mtod(pkt[0], struct ether_hdr *);
1307 ipv4_hdr = (struct ipv4_hdr *)(eth_hdr + 1);
1308 x0 = ipv4_hdr->dst_addr;
1309 flag[0] = pkt[0]->ol_flags & PKT_RX_IPV4_HDR;
1311 eth_hdr = rte_pktmbuf_mtod(pkt[1], struct ether_hdr *);
1312 ipv4_hdr = (struct ipv4_hdr *)(eth_hdr + 1);
1313 x1 = ipv4_hdr->dst_addr;
1314 flag[0] &= pkt[1]->ol_flags;
1316 eth_hdr = rte_pktmbuf_mtod(pkt[2], struct ether_hdr *);
1317 ipv4_hdr = (struct ipv4_hdr *)(eth_hdr + 1);
1318 x2 = ipv4_hdr->dst_addr;
1319 flag[0] &= pkt[2]->ol_flags;
1321 eth_hdr = rte_pktmbuf_mtod(pkt[3], struct ether_hdr *);
1322 ipv4_hdr = (struct ipv4_hdr *)(eth_hdr + 1);
1323 x3 = ipv4_hdr->dst_addr;
1324 flag[0] &= pkt[3]->ol_flags;
1326 dip[0] = _mm_set_epi32(x3, x2, x1, x0);
1328 #endif /* RTE_NEXT_ABI */
1331 * Lookup into LPM for destination port.
1332 * If lookup fails, use incoming port (portid) as destination port.
1336 processx4_step2(const struct lcore_conf *qconf,
1340 struct rte_mbuf *pkt[FWDSTEP],
1341 uint16_t dprt[FWDSTEP])
1343 processx4_step2(const struct lcore_conf *qconf, __m128i dip, uint32_t flag,
1344 uint8_t portid, struct rte_mbuf *pkt[FWDSTEP], uint16_t dprt[FWDSTEP])
1345 #endif /* RTE_NEXT_ABI */
1348 const __m128i bswap_mask = _mm_set_epi8(12, 13, 14, 15, 8, 9, 10, 11,
1349 4, 5, 6, 7, 0, 1, 2, 3);
1351 /* Byte swap 4 IPV4 addresses. */
1352 dip = _mm_shuffle_epi8(dip, bswap_mask);
1354 /* if all 4 packets are IPV4. */
1356 if (likely(ipv4_flag)) {
1358 if (likely(flag != 0)) {
1360 rte_lpm_lookupx4(qconf->ipv4_lookup_struct, dip, dprt, portid);
1363 dprt[0] = get_dst_port(qconf, pkt[0], dst.u32[0], portid);
1364 dprt[1] = get_dst_port(qconf, pkt[1], dst.u32[1], portid);
1365 dprt[2] = get_dst_port(qconf, pkt[2], dst.u32[2], portid);
1366 dprt[3] = get_dst_port(qconf, pkt[3], dst.u32[3], portid);
1371 * Update source and destination MAC addresses in the ethernet header.
1372 * Perform RFC1812 checks and updates for IPV4 packets.
1375 processx4_step3(struct rte_mbuf *pkt[FWDSTEP], uint16_t dst_port[FWDSTEP])
1377 __m128i te[FWDSTEP];
1378 __m128i ve[FWDSTEP];
1379 __m128i *p[FWDSTEP];
1381 p[0] = rte_pktmbuf_mtod(pkt[0], __m128i *);
1382 p[1] = rte_pktmbuf_mtod(pkt[1], __m128i *);
1383 p[2] = rte_pktmbuf_mtod(pkt[2], __m128i *);
1384 p[3] = rte_pktmbuf_mtod(pkt[3], __m128i *);
1386 ve[0] = val_eth[dst_port[0]];
1387 te[0] = _mm_load_si128(p[0]);
1389 ve[1] = val_eth[dst_port[1]];
1390 te[1] = _mm_load_si128(p[1]);
1392 ve[2] = val_eth[dst_port[2]];
1393 te[2] = _mm_load_si128(p[2]);
1395 ve[3] = val_eth[dst_port[3]];
1396 te[3] = _mm_load_si128(p[3]);
1398 /* Update first 12 bytes, keep rest bytes intact. */
1399 te[0] = _mm_blend_epi16(te[0], ve[0], MASK_ETH);
1400 te[1] = _mm_blend_epi16(te[1], ve[1], MASK_ETH);
1401 te[2] = _mm_blend_epi16(te[2], ve[2], MASK_ETH);
1402 te[3] = _mm_blend_epi16(te[3], ve[3], MASK_ETH);
1404 _mm_store_si128(p[0], te[0]);
1405 _mm_store_si128(p[1], te[1]);
1406 _mm_store_si128(p[2], te[2]);
1407 _mm_store_si128(p[3], te[3]);
1410 rfc1812_process((struct ipv4_hdr *)((struct ether_hdr *)p[0] + 1),
1411 &dst_port[0], pkt[0]->packet_type);
1412 rfc1812_process((struct ipv4_hdr *)((struct ether_hdr *)p[1] + 1),
1413 &dst_port[1], pkt[1]->packet_type);
1414 rfc1812_process((struct ipv4_hdr *)((struct ether_hdr *)p[2] + 1),
1415 &dst_port[2], pkt[2]->packet_type);
1416 rfc1812_process((struct ipv4_hdr *)((struct ether_hdr *)p[3] + 1),
1417 &dst_port[3], pkt[3]->packet_type);
1418 #else /* RTE_NEXT_ABI */
1419 rfc1812_process((struct ipv4_hdr *)((struct ether_hdr *)p[0] + 1),
1420 &dst_port[0], pkt[0]->ol_flags);
1421 rfc1812_process((struct ipv4_hdr *)((struct ether_hdr *)p[1] + 1),
1422 &dst_port[1], pkt[1]->ol_flags);
1423 rfc1812_process((struct ipv4_hdr *)((struct ether_hdr *)p[2] + 1),
1424 &dst_port[2], pkt[2]->ol_flags);
1425 rfc1812_process((struct ipv4_hdr *)((struct ether_hdr *)p[3] + 1),
1426 &dst_port[3], pkt[3]->ol_flags);
1427 #endif /* RTE_NEXT_ABI */
1431 * We group consecutive packets with the same destionation port into one burst.
1432 * To avoid extra latency this is done together with some other packet
1433 * processing, but after we made a final decision about packet's destination.
1434 * To do this we maintain:
1435 * pnum - array of number of consecutive packets with the same dest port for
1436 * each packet in the input burst.
1437 * lp - pointer to the last updated element in the pnum.
1438 * dlp - dest port value lp corresponds to.
1441 #define GRPSZ (1 << FWDSTEP)
1442 #define GRPMSK (GRPSZ - 1)
1444 #define GROUP_PORT_STEP(dlp, dcp, lp, pn, idx) do { \
1445 if (likely((dlp) == (dcp)[(idx)])) { \
1448 (dlp) = (dcp)[idx]; \
1449 (lp) = (pn) + (idx); \
1455 * Group consecutive packets with the same destination port in bursts of 4.
1456 * Suppose we have array of destionation ports:
1457 * dst_port[] = {a, b, c, d,, e, ... }
1458 * dp1 should contain: <a, b, c, d>, dp2: <b, c, d, e>.
1459 * We doing 4 comparisions at once and the result is 4 bit mask.
1460 * This mask is used as an index into prebuild array of pnum values.
1462 static inline uint16_t *
1463 port_groupx4(uint16_t pn[FWDSTEP + 1], uint16_t *lp, __m128i dp1, __m128i dp2)
1465 static const struct {
1466 uint64_t pnum; /* prebuild 4 values for pnum[]. */
1467 int32_t idx; /* index for new last updated elemnet. */
1468 uint16_t lpv; /* add value to the last updated element. */
1471 /* 0: a != b, b != c, c != d, d != e */
1472 .pnum = UINT64_C(0x0001000100010001),
1477 /* 1: a == b, b != c, c != d, d != e */
1478 .pnum = UINT64_C(0x0001000100010002),
1483 /* 2: a != b, b == c, c != d, d != e */
1484 .pnum = UINT64_C(0x0001000100020001),
1489 /* 3: a == b, b == c, c != d, d != e */
1490 .pnum = UINT64_C(0x0001000100020003),
1495 /* 4: a != b, b != c, c == d, d != e */
1496 .pnum = UINT64_C(0x0001000200010001),
1501 /* 5: a == b, b != c, c == d, d != e */
1502 .pnum = UINT64_C(0x0001000200010002),
1507 /* 6: a != b, b == c, c == d, d != e */
1508 .pnum = UINT64_C(0x0001000200030001),
1513 /* 7: a == b, b == c, c == d, d != e */
1514 .pnum = UINT64_C(0x0001000200030004),
1519 /* 8: a != b, b != c, c != d, d == e */
1520 .pnum = UINT64_C(0x0002000100010001),
1525 /* 9: a == b, b != c, c != d, d == e */
1526 .pnum = UINT64_C(0x0002000100010002),
1531 /* 0xa: a != b, b == c, c != d, d == e */
1532 .pnum = UINT64_C(0x0002000100020001),
1537 /* 0xb: a == b, b == c, c != d, d == e */
1538 .pnum = UINT64_C(0x0002000100020003),
1543 /* 0xc: a != b, b != c, c == d, d == e */
1544 .pnum = UINT64_C(0x0002000300010001),
1549 /* 0xd: a == b, b != c, c == d, d == e */
1550 .pnum = UINT64_C(0x0002000300010002),
1555 /* 0xe: a != b, b == c, c == d, d == e */
1556 .pnum = UINT64_C(0x0002000300040001),
1561 /* 0xf: a == b, b == c, c == d, d == e */
1562 .pnum = UINT64_C(0x0002000300040005),
1569 uint16_t u16[FWDSTEP + 1];
1571 } *pnum = (void *)pn;
1575 dp1 = _mm_cmpeq_epi16(dp1, dp2);
1576 dp1 = _mm_unpacklo_epi16(dp1, dp1);
1577 v = _mm_movemask_ps((__m128)dp1);
1579 /* update last port counter. */
1580 lp[0] += gptbl[v].lpv;
1582 /* if dest port value has changed. */
1584 lp = pnum->u16 + gptbl[v].idx;
1586 pnum->u64 = gptbl[v].pnum;
1592 #endif /* APP_LOOKUP_METHOD */
1594 /* main processing loop */
1596 main_loop(__attribute__((unused)) void *dummy)
1598 struct rte_mbuf *pkts_burst[MAX_PKT_BURST];
1600 uint64_t prev_tsc, diff_tsc, cur_tsc;
1602 uint8_t portid, queueid;
1603 struct lcore_conf *qconf;
1604 const uint64_t drain_tsc = (rte_get_tsc_hz() + US_PER_S - 1) /
1605 US_PER_S * BURST_TX_DRAIN_US;
1607 #if ((APP_LOOKUP_METHOD == APP_LOOKUP_LPM) && \
1608 (ENABLE_MULTI_BUFFER_OPTIMIZE == 1))
1612 uint16_t dst_port[MAX_PKT_BURST];
1613 __m128i dip[MAX_PKT_BURST / FWDSTEP];
1615 uint32_t ipv4_flag[MAX_PKT_BURST / FWDSTEP];
1617 uint32_t flag[MAX_PKT_BURST / FWDSTEP];
1619 uint16_t pnum[MAX_PKT_BURST + 1];
1624 lcore_id = rte_lcore_id();
1625 qconf = &lcore_conf[lcore_id];
1627 if (qconf->n_rx_queue == 0) {
1628 RTE_LOG(INFO, L3FWD, "lcore %u has nothing to do\n", lcore_id);
1632 RTE_LOG(INFO, L3FWD, "entering main loop on lcore %u\n", lcore_id);
1634 for (i = 0; i < qconf->n_rx_queue; i++) {
1636 portid = qconf->rx_queue_list[i].port_id;
1637 queueid = qconf->rx_queue_list[i].queue_id;
1638 RTE_LOG(INFO, L3FWD, " -- lcoreid=%u portid=%hhu rxqueueid=%hhu\n", lcore_id,
1644 cur_tsc = rte_rdtsc();
1647 * TX burst queue drain
1649 diff_tsc = cur_tsc - prev_tsc;
1650 if (unlikely(diff_tsc > drain_tsc)) {
1653 * This could be optimized (use queueid instead of
1654 * portid), but it is not called so often
1656 for (portid = 0; portid < RTE_MAX_ETHPORTS; portid++) {
1657 if (qconf->tx_mbufs[portid].len == 0)
1660 qconf->tx_mbufs[portid].len,
1662 qconf->tx_mbufs[portid].len = 0;
1669 * Read packet from RX queues
1671 for (i = 0; i < qconf->n_rx_queue; ++i) {
1672 portid = qconf->rx_queue_list[i].port_id;
1673 queueid = qconf->rx_queue_list[i].queue_id;
1674 nb_rx = rte_eth_rx_burst(portid, queueid, pkts_burst,
1679 #if (ENABLE_MULTI_BUFFER_OPTIMIZE == 1)
1680 #if (APP_LOOKUP_METHOD == APP_LOOKUP_EXACT_MATCH)
1683 * Send nb_rx - nb_rx%8 packets
1686 int32_t n = RTE_ALIGN_FLOOR(nb_rx, 8);
1687 for (j = 0; j < n; j += 8) {
1690 pkts_burst[j]->packet_type &
1691 pkts_burst[j+1]->packet_type &
1692 pkts_burst[j+2]->packet_type &
1693 pkts_burst[j+3]->packet_type &
1694 pkts_burst[j+4]->packet_type &
1695 pkts_burst[j+5]->packet_type &
1696 pkts_burst[j+6]->packet_type &
1697 pkts_burst[j+7]->packet_type;
1698 if (pkt_type & RTE_PTYPE_L3_IPV4) {
1699 simple_ipv4_fwd_8pkts(
1700 &pkts_burst[j], portid, qconf);
1701 } else if (pkt_type &
1702 RTE_PTYPE_L3_IPV6) {
1703 #else /* RTE_NEXT_ABI */
1704 uint32_t ol_flag = pkts_burst[j]->ol_flags
1705 & pkts_burst[j+1]->ol_flags
1706 & pkts_burst[j+2]->ol_flags
1707 & pkts_burst[j+3]->ol_flags
1708 & pkts_burst[j+4]->ol_flags
1709 & pkts_burst[j+5]->ol_flags
1710 & pkts_burst[j+6]->ol_flags
1711 & pkts_burst[j+7]->ol_flags;
1712 if (ol_flag & PKT_RX_IPV4_HDR ) {
1713 simple_ipv8_fwd_4pkts(&pkts_burst[j],
1715 } else if (ol_flag & PKT_RX_IPV6_HDR) {
1716 #endif /* RTE_NEXT_ABI */
1717 simple_ipv6_fwd_4pkts(&pkts_burst[j],
1720 l3fwd_simple_forward(pkts_burst[j],
1722 l3fwd_simple_forward(pkts_burst[j+1],
1724 l3fwd_simple_forward(pkts_burst[j+2],
1726 l3fwd_simple_forward(pkts_burst[j+3],
1728 l3fwd_simple_forward(pkts_burst[j+4],
1730 l3fwd_simple_forward(pkts_burst[j+5],
1732 l3fwd_simple_forward(pkts_burst[j+6],
1734 l3fwd_simple_forward(pkts_burst[j+7],
1738 for (; j < nb_rx ; j++) {
1739 l3fwd_simple_forward(pkts_burst[j],
1743 #elif (APP_LOOKUP_METHOD == APP_LOOKUP_LPM)
1745 k = RTE_ALIGN_FLOOR(nb_rx, FWDSTEP);
1746 for (j = 0; j != k; j += FWDSTEP) {
1747 processx4_step1(&pkts_burst[j],
1750 &ipv4_flag[j / FWDSTEP]);
1752 &flag[j / FWDSTEP]);
1756 k = RTE_ALIGN_FLOOR(nb_rx, FWDSTEP);
1757 for (j = 0; j != k; j += FWDSTEP) {
1758 processx4_step2(qconf, dip[j / FWDSTEP],
1760 ipv4_flag[j / FWDSTEP], portid,
1762 flag[j / FWDSTEP], portid,
1764 &pkts_burst[j], &dst_port[j]);
1768 * Finish packet processing and group consecutive
1769 * packets with the same destination port.
1771 k = RTE_ALIGN_FLOOR(nb_rx, FWDSTEP);
1778 processx4_step3(pkts_burst, dst_port);
1780 /* dp1: <d[0], d[1], d[2], d[3], ... > */
1781 dp1 = _mm_loadu_si128((__m128i *)dst_port);
1783 for (j = FWDSTEP; j != k; j += FWDSTEP) {
1784 processx4_step3(&pkts_burst[j],
1789 * <d[j-3], d[j-2], d[j-1], d[j], ... >
1791 dp2 = _mm_loadu_si128((__m128i *)
1792 &dst_port[j - FWDSTEP + 1]);
1793 lp = port_groupx4(&pnum[j - FWDSTEP],
1798 * <d[j], d[j+1], d[j+2], d[j+3], ... >
1800 dp1 = _mm_srli_si128(dp2,
1802 sizeof(dst_port[0]));
1806 * dp2: <d[j-3], d[j-2], d[j-1], d[j-1], ... >
1808 dp2 = _mm_shufflelo_epi16(dp1, 0xf9);
1809 lp = port_groupx4(&pnum[j - FWDSTEP], lp,
1813 * remove values added by the last repeated
1817 dlp = dst_port[j - 1];
1819 /* set dlp and lp to the never used values. */
1821 lp = pnum + MAX_PKT_BURST;
1824 /* Process up to last 3 packets one by one. */
1825 switch (nb_rx % FWDSTEP) {
1827 process_packet(qconf, pkts_burst[j],
1828 dst_port + j, portid);
1829 GROUP_PORT_STEP(dlp, dst_port, lp, pnum, j);
1832 process_packet(qconf, pkts_burst[j],
1833 dst_port + j, portid);
1834 GROUP_PORT_STEP(dlp, dst_port, lp, pnum, j);
1837 process_packet(qconf, pkts_burst[j],
1838 dst_port + j, portid);
1839 GROUP_PORT_STEP(dlp, dst_port, lp, pnum, j);
1844 * Send packets out, through destination port.
1845 * Consecuteve pacekts with the same destination port
1846 * are already grouped together.
1847 * If destination port for the packet equals BAD_PORT,
1848 * then free the packet without sending it out.
1850 for (j = 0; j < nb_rx; j += k) {
1858 if (likely(pn != BAD_PORT)) {
1859 send_packetsx4(qconf, pn,
1862 for (m = j; m != j + k; m++)
1863 rte_pktmbuf_free(pkts_burst[m]);
1867 #endif /* APP_LOOKUP_METHOD */
1868 #else /* ENABLE_MULTI_BUFFER_OPTIMIZE == 0 */
1870 /* Prefetch first packets */
1871 for (j = 0; j < PREFETCH_OFFSET && j < nb_rx; j++) {
1872 rte_prefetch0(rte_pktmbuf_mtod(
1873 pkts_burst[j], void *));
1876 /* Prefetch and forward already prefetched packets */
1877 for (j = 0; j < (nb_rx - PREFETCH_OFFSET); j++) {
1878 rte_prefetch0(rte_pktmbuf_mtod(pkts_burst[
1879 j + PREFETCH_OFFSET], void *));
1880 l3fwd_simple_forward(pkts_burst[j], portid,
1884 /* Forward remaining prefetched packets */
1885 for (; j < nb_rx; j++) {
1886 l3fwd_simple_forward(pkts_burst[j], portid,
1889 #endif /* ENABLE_MULTI_BUFFER_OPTIMIZE */
1896 check_lcore_params(void)
1898 uint8_t queue, lcore;
1902 for (i = 0; i < nb_lcore_params; ++i) {
1903 queue = lcore_params[i].queue_id;
1904 if (queue >= MAX_RX_QUEUE_PER_PORT) {
1905 printf("invalid queue number: %hhu\n", queue);
1908 lcore = lcore_params[i].lcore_id;
1909 if (!rte_lcore_is_enabled(lcore)) {
1910 printf("error: lcore %hhu is not enabled in lcore mask\n", lcore);
1913 if ((socketid = rte_lcore_to_socket_id(lcore) != 0) &&
1915 printf("warning: lcore %hhu is on socket %d with numa off \n",
1923 check_port_config(const unsigned nb_ports)
1928 for (i = 0; i < nb_lcore_params; ++i) {
1929 portid = lcore_params[i].port_id;
1930 if ((enabled_port_mask & (1 << portid)) == 0) {
1931 printf("port %u is not enabled in port mask\n", portid);
1934 if (portid >= nb_ports) {
1935 printf("port %u is not present on the board\n", portid);
1943 get_port_n_rx_queues(const uint8_t port)
1948 for (i = 0; i < nb_lcore_params; ++i) {
1949 if (lcore_params[i].port_id == port && lcore_params[i].queue_id > queue)
1950 queue = lcore_params[i].queue_id;
1952 return (uint8_t)(++queue);
1956 init_lcore_rx_queues(void)
1958 uint16_t i, nb_rx_queue;
1961 for (i = 0; i < nb_lcore_params; ++i) {
1962 lcore = lcore_params[i].lcore_id;
1963 nb_rx_queue = lcore_conf[lcore].n_rx_queue;
1964 if (nb_rx_queue >= MAX_RX_QUEUE_PER_LCORE) {
1965 printf("error: too many queues (%u) for lcore: %u\n",
1966 (unsigned)nb_rx_queue + 1, (unsigned)lcore);
1969 lcore_conf[lcore].rx_queue_list[nb_rx_queue].port_id =
1970 lcore_params[i].port_id;
1971 lcore_conf[lcore].rx_queue_list[nb_rx_queue].queue_id =
1972 lcore_params[i].queue_id;
1973 lcore_conf[lcore].n_rx_queue++;
1981 print_usage(const char *prgname)
1983 printf ("%s [EAL options] -- -p PORTMASK -P"
1984 " [--config (port,queue,lcore)[,(port,queue,lcore]]"
1985 " [--enable-jumbo [--max-pkt-len PKTLEN]]\n"
1986 " -p PORTMASK: hexadecimal bitmask of ports to configure\n"
1987 " -P : enable promiscuous mode\n"
1988 " --config (port,queue,lcore): rx queues configuration\n"
1989 " --eth-dest=X,MM:MM:MM:MM:MM:MM: optional, ethernet destination for port X\n"
1990 " --no-numa: optional, disable numa awareness\n"
1991 " --ipv6: optional, specify it if running ipv6 packets\n"
1992 " --enable-jumbo: enable jumbo frame"
1993 " which max packet len is PKTLEN in decimal (64-9600)\n"
1994 " --hash-entry-num: specify the hash entry number in hexadecimal to be setup\n",
1998 static int parse_max_pkt_len(const char *pktlen)
2003 /* parse decimal string */
2004 len = strtoul(pktlen, &end, 10);
2005 if ((pktlen[0] == '\0') || (end == NULL) || (*end != '\0'))
2015 parse_portmask(const char *portmask)
2020 /* parse hexadecimal string */
2021 pm = strtoul(portmask, &end, 16);
2022 if ((portmask[0] == '\0') || (end == NULL) || (*end != '\0'))
2031 #if (APP_LOOKUP_METHOD == APP_LOOKUP_EXACT_MATCH)
2033 parse_hash_entry_number(const char *hash_entry_num)
2036 unsigned long hash_en;
2037 /* parse hexadecimal string */
2038 hash_en = strtoul(hash_entry_num, &end, 16);
2039 if ((hash_entry_num[0] == '\0') || (end == NULL) || (*end != '\0'))
2050 parse_config(const char *q_arg)
2053 const char *p, *p0 = q_arg;
2061 unsigned long int_fld[_NUM_FLD];
2062 char *str_fld[_NUM_FLD];
2066 nb_lcore_params = 0;
2068 while ((p = strchr(p0,'(')) != NULL) {
2070 if((p0 = strchr(p,')')) == NULL)
2074 if(size >= sizeof(s))
2077 snprintf(s, sizeof(s), "%.*s", size, p);
2078 if (rte_strsplit(s, sizeof(s), str_fld, _NUM_FLD, ',') != _NUM_FLD)
2080 for (i = 0; i < _NUM_FLD; i++){
2082 int_fld[i] = strtoul(str_fld[i], &end, 0);
2083 if (errno != 0 || end == str_fld[i] || int_fld[i] > 255)
2086 if (nb_lcore_params >= MAX_LCORE_PARAMS) {
2087 printf("exceeded max number of lcore params: %hu\n",
2091 lcore_params_array[nb_lcore_params].port_id = (uint8_t)int_fld[FLD_PORT];
2092 lcore_params_array[nb_lcore_params].queue_id = (uint8_t)int_fld[FLD_QUEUE];
2093 lcore_params_array[nb_lcore_params].lcore_id = (uint8_t)int_fld[FLD_LCORE];
2096 lcore_params = lcore_params_array;
2101 parse_eth_dest(const char *optarg)
2105 uint8_t c, *dest, peer_addr[6];
2108 portid = strtoul(optarg, &port_end, 10);
2109 if (errno != 0 || port_end == optarg || *port_end++ != ',')
2110 rte_exit(EXIT_FAILURE,
2111 "Invalid eth-dest: %s", optarg);
2112 if (portid >= RTE_MAX_ETHPORTS)
2113 rte_exit(EXIT_FAILURE,
2114 "eth-dest: port %d >= RTE_MAX_ETHPORTS(%d)\n",
2115 portid, RTE_MAX_ETHPORTS);
2117 if (cmdline_parse_etheraddr(NULL, port_end,
2118 &peer_addr, sizeof(peer_addr)) < 0)
2119 rte_exit(EXIT_FAILURE,
2120 "Invalid ethernet address: %s\n",
2122 dest = (uint8_t *)&dest_eth_addr[portid];
2123 for (c = 0; c < 6; c++)
2124 dest[c] = peer_addr[c];
2125 *(uint64_t *)(val_eth + portid) = dest_eth_addr[portid];
2128 #define CMD_LINE_OPT_CONFIG "config"
2129 #define CMD_LINE_OPT_ETH_DEST "eth-dest"
2130 #define CMD_LINE_OPT_NO_NUMA "no-numa"
2131 #define CMD_LINE_OPT_IPV6 "ipv6"
2132 #define CMD_LINE_OPT_ENABLE_JUMBO "enable-jumbo"
2133 #define CMD_LINE_OPT_HASH_ENTRY_NUM "hash-entry-num"
2135 /* Parse the argument given in the command line of the application */
2137 parse_args(int argc, char **argv)
2142 char *prgname = argv[0];
2143 static struct option lgopts[] = {
2144 {CMD_LINE_OPT_CONFIG, 1, 0, 0},
2145 {CMD_LINE_OPT_ETH_DEST, 1, 0, 0},
2146 {CMD_LINE_OPT_NO_NUMA, 0, 0, 0},
2147 {CMD_LINE_OPT_IPV6, 0, 0, 0},
2148 {CMD_LINE_OPT_ENABLE_JUMBO, 0, 0, 0},
2149 {CMD_LINE_OPT_HASH_ENTRY_NUM, 1, 0, 0},
2155 while ((opt = getopt_long(argc, argvopt, "p:P",
2156 lgopts, &option_index)) != EOF) {
2161 enabled_port_mask = parse_portmask(optarg);
2162 if (enabled_port_mask == 0) {
2163 printf("invalid portmask\n");
2164 print_usage(prgname);
2169 printf("Promiscuous mode selected\n");
2175 if (!strncmp(lgopts[option_index].name, CMD_LINE_OPT_CONFIG,
2176 sizeof (CMD_LINE_OPT_CONFIG))) {
2177 ret = parse_config(optarg);
2179 printf("invalid config\n");
2180 print_usage(prgname);
2185 if (!strncmp(lgopts[option_index].name, CMD_LINE_OPT_ETH_DEST,
2186 sizeof(CMD_LINE_OPT_CONFIG))) {
2187 parse_eth_dest(optarg);
2190 if (!strncmp(lgopts[option_index].name, CMD_LINE_OPT_NO_NUMA,
2191 sizeof(CMD_LINE_OPT_NO_NUMA))) {
2192 printf("numa is disabled \n");
2196 #if (APP_LOOKUP_METHOD == APP_LOOKUP_EXACT_MATCH)
2197 if (!strncmp(lgopts[option_index].name, CMD_LINE_OPT_IPV6,
2198 sizeof(CMD_LINE_OPT_IPV6))) {
2199 printf("ipv6 is specified \n");
2204 if (!strncmp(lgopts[option_index].name, CMD_LINE_OPT_ENABLE_JUMBO,
2205 sizeof (CMD_LINE_OPT_ENABLE_JUMBO))) {
2206 struct option lenopts = {"max-pkt-len", required_argument, 0, 0};
2208 printf("jumbo frame is enabled - disabling simple TX path\n");
2209 port_conf.rxmode.jumbo_frame = 1;
2211 /* if no max-pkt-len set, use the default value ETHER_MAX_LEN */
2212 if (0 == getopt_long(argc, argvopt, "", &lenopts, &option_index)) {
2213 ret = parse_max_pkt_len(optarg);
2214 if ((ret < 64) || (ret > MAX_JUMBO_PKT_LEN)){
2215 printf("invalid packet length\n");
2216 print_usage(prgname);
2219 port_conf.rxmode.max_rx_pkt_len = ret;
2221 printf("set jumbo frame max packet length to %u\n",
2222 (unsigned int)port_conf.rxmode.max_rx_pkt_len);
2224 #if (APP_LOOKUP_METHOD == APP_LOOKUP_EXACT_MATCH)
2225 if (!strncmp(lgopts[option_index].name, CMD_LINE_OPT_HASH_ENTRY_NUM,
2226 sizeof(CMD_LINE_OPT_HASH_ENTRY_NUM))) {
2227 ret = parse_hash_entry_number(optarg);
2228 if ((ret > 0) && (ret <= L3FWD_HASH_ENTRIES)) {
2229 hash_entry_number = ret;
2231 printf("invalid hash entry number\n");
2232 print_usage(prgname);
2240 print_usage(prgname);
2246 argv[optind-1] = prgname;
2249 optind = 0; /* reset getopt lib */
2254 print_ethaddr(const char *name, const struct ether_addr *eth_addr)
2256 char buf[ETHER_ADDR_FMT_SIZE];
2257 ether_format_addr(buf, ETHER_ADDR_FMT_SIZE, eth_addr);
2258 printf("%s%s", name, buf);
2261 #if (APP_LOOKUP_METHOD == APP_LOOKUP_EXACT_MATCH)
2263 static void convert_ipv4_5tuple(struct ipv4_5tuple* key1,
2264 union ipv4_5tuple_host* key2)
2266 key2->ip_dst = rte_cpu_to_be_32(key1->ip_dst);
2267 key2->ip_src = rte_cpu_to_be_32(key1->ip_src);
2268 key2->port_dst = rte_cpu_to_be_16(key1->port_dst);
2269 key2->port_src = rte_cpu_to_be_16(key1->port_src);
2270 key2->proto = key1->proto;
2276 static void convert_ipv6_5tuple(struct ipv6_5tuple* key1,
2277 union ipv6_5tuple_host* key2)
2280 for (i = 0; i < 16; i++)
2282 key2->ip_dst[i] = key1->ip_dst[i];
2283 key2->ip_src[i] = key1->ip_src[i];
2285 key2->port_dst = rte_cpu_to_be_16(key1->port_dst);
2286 key2->port_src = rte_cpu_to_be_16(key1->port_src);
2287 key2->proto = key1->proto;
2294 #define BYTE_VALUE_MAX 256
2295 #define ALL_32_BITS 0xffffffff
2296 #define BIT_8_TO_15 0x0000ff00
2298 populate_ipv4_few_flow_into_table(const struct rte_hash* h)
2302 uint32_t array_len = sizeof(ipv4_l3fwd_route_array)/sizeof(ipv4_l3fwd_route_array[0]);
2304 mask0 = _mm_set_epi32(ALL_32_BITS, ALL_32_BITS, ALL_32_BITS, BIT_8_TO_15);
2305 for (i = 0; i < array_len; i++) {
2306 struct ipv4_l3fwd_route entry;
2307 union ipv4_5tuple_host newkey;
2308 entry = ipv4_l3fwd_route_array[i];
2309 convert_ipv4_5tuple(&entry.key, &newkey);
2310 ret = rte_hash_add_key (h,(void *) &newkey);
2312 rte_exit(EXIT_FAILURE, "Unable to add entry %" PRIu32
2313 " to the l3fwd hash.\n", i);
2315 ipv4_l3fwd_out_if[ret] = entry.if_out;
2317 printf("Hash: Adding 0x%" PRIx32 " keys\n", array_len);
2320 #define BIT_16_TO_23 0x00ff0000
2322 populate_ipv6_few_flow_into_table(const struct rte_hash* h)
2326 uint32_t array_len = sizeof(ipv6_l3fwd_route_array)/sizeof(ipv6_l3fwd_route_array[0]);
2328 mask1 = _mm_set_epi32(ALL_32_BITS, ALL_32_BITS, ALL_32_BITS, BIT_16_TO_23);
2329 mask2 = _mm_set_epi32(0, 0, ALL_32_BITS, ALL_32_BITS);
2330 for (i = 0; i < array_len; i++) {
2331 struct ipv6_l3fwd_route entry;
2332 union ipv6_5tuple_host newkey;
2333 entry = ipv6_l3fwd_route_array[i];
2334 convert_ipv6_5tuple(&entry.key, &newkey);
2335 ret = rte_hash_add_key (h, (void *) &newkey);
2337 rte_exit(EXIT_FAILURE, "Unable to add entry %" PRIu32
2338 " to the l3fwd hash.\n", i);
2340 ipv6_l3fwd_out_if[ret] = entry.if_out;
2342 printf("Hash: Adding 0x%" PRIx32 "keys\n", array_len);
2345 #define NUMBER_PORT_USED 4
2347 populate_ipv4_many_flow_into_table(const struct rte_hash* h,
2348 unsigned int nr_flow)
2351 mask0 = _mm_set_epi32(ALL_32_BITS, ALL_32_BITS, ALL_32_BITS, BIT_8_TO_15);
2352 for (i = 0; i < nr_flow; i++) {
2353 struct ipv4_l3fwd_route entry;
2354 union ipv4_5tuple_host newkey;
2355 uint8_t a = (uint8_t) ((i/NUMBER_PORT_USED)%BYTE_VALUE_MAX);
2356 uint8_t b = (uint8_t) (((i/NUMBER_PORT_USED)/BYTE_VALUE_MAX)%BYTE_VALUE_MAX);
2357 uint8_t c = (uint8_t) ((i/NUMBER_PORT_USED)/(BYTE_VALUE_MAX*BYTE_VALUE_MAX));
2358 /* Create the ipv4 exact match flow */
2359 memset(&entry, 0, sizeof(entry));
2360 switch (i & (NUMBER_PORT_USED -1)) {
2362 entry = ipv4_l3fwd_route_array[0];
2363 entry.key.ip_dst = IPv4(101,c,b,a);
2366 entry = ipv4_l3fwd_route_array[1];
2367 entry.key.ip_dst = IPv4(201,c,b,a);
2370 entry = ipv4_l3fwd_route_array[2];
2371 entry.key.ip_dst = IPv4(111,c,b,a);
2374 entry = ipv4_l3fwd_route_array[3];
2375 entry.key.ip_dst = IPv4(211,c,b,a);
2378 convert_ipv4_5tuple(&entry.key, &newkey);
2379 int32_t ret = rte_hash_add_key(h,(void *) &newkey);
2381 rte_exit(EXIT_FAILURE, "Unable to add entry %u\n", i);
2383 ipv4_l3fwd_out_if[ret] = (uint8_t) entry.if_out;
2386 printf("Hash: Adding 0x%x keys\n", nr_flow);
2390 populate_ipv6_many_flow_into_table(const struct rte_hash* h,
2391 unsigned int nr_flow)
2394 mask1 = _mm_set_epi32(ALL_32_BITS, ALL_32_BITS, ALL_32_BITS, BIT_16_TO_23);
2395 mask2 = _mm_set_epi32(0, 0, ALL_32_BITS, ALL_32_BITS);
2396 for (i = 0; i < nr_flow; i++) {
2397 struct ipv6_l3fwd_route entry;
2398 union ipv6_5tuple_host newkey;
2399 uint8_t a = (uint8_t) ((i/NUMBER_PORT_USED)%BYTE_VALUE_MAX);
2400 uint8_t b = (uint8_t) (((i/NUMBER_PORT_USED)/BYTE_VALUE_MAX)%BYTE_VALUE_MAX);
2401 uint8_t c = (uint8_t) ((i/NUMBER_PORT_USED)/(BYTE_VALUE_MAX*BYTE_VALUE_MAX));
2402 /* Create the ipv6 exact match flow */
2403 memset(&entry, 0, sizeof(entry));
2404 switch (i & (NUMBER_PORT_USED - 1)) {
2405 case 0: entry = ipv6_l3fwd_route_array[0]; break;
2406 case 1: entry = ipv6_l3fwd_route_array[1]; break;
2407 case 2: entry = ipv6_l3fwd_route_array[2]; break;
2408 case 3: entry = ipv6_l3fwd_route_array[3]; break;
2410 entry.key.ip_dst[13] = c;
2411 entry.key.ip_dst[14] = b;
2412 entry.key.ip_dst[15] = a;
2413 convert_ipv6_5tuple(&entry.key, &newkey);
2414 int32_t ret = rte_hash_add_key(h,(void *) &newkey);
2416 rte_exit(EXIT_FAILURE, "Unable to add entry %u\n", i);
2418 ipv6_l3fwd_out_if[ret] = (uint8_t) entry.if_out;
2421 printf("Hash: Adding 0x%x keys\n", nr_flow);
2425 setup_hash(int socketid)
2427 struct rte_hash_parameters ipv4_l3fwd_hash_params = {
2429 .entries = L3FWD_HASH_ENTRIES,
2430 .key_len = sizeof(union ipv4_5tuple_host),
2431 .hash_func = ipv4_hash_crc,
2432 .hash_func_init_val = 0,
2435 struct rte_hash_parameters ipv6_l3fwd_hash_params = {
2437 .entries = L3FWD_HASH_ENTRIES,
2438 .key_len = sizeof(union ipv6_5tuple_host),
2439 .hash_func = ipv6_hash_crc,
2440 .hash_func_init_val = 0,
2445 /* create ipv4 hash */
2446 snprintf(s, sizeof(s), "ipv4_l3fwd_hash_%d", socketid);
2447 ipv4_l3fwd_hash_params.name = s;
2448 ipv4_l3fwd_hash_params.socket_id = socketid;
2449 ipv4_l3fwd_lookup_struct[socketid] = rte_hash_create(&ipv4_l3fwd_hash_params);
2450 if (ipv4_l3fwd_lookup_struct[socketid] == NULL)
2451 rte_exit(EXIT_FAILURE, "Unable to create the l3fwd hash on "
2452 "socket %d\n", socketid);
2454 /* create ipv6 hash */
2455 snprintf(s, sizeof(s), "ipv6_l3fwd_hash_%d", socketid);
2456 ipv6_l3fwd_hash_params.name = s;
2457 ipv6_l3fwd_hash_params.socket_id = socketid;
2458 ipv6_l3fwd_lookup_struct[socketid] = rte_hash_create(&ipv6_l3fwd_hash_params);
2459 if (ipv6_l3fwd_lookup_struct[socketid] == NULL)
2460 rte_exit(EXIT_FAILURE, "Unable to create the l3fwd hash on "
2461 "socket %d\n", socketid);
2463 if (hash_entry_number != HASH_ENTRY_NUMBER_DEFAULT) {
2464 /* For testing hash matching with a large number of flows we
2465 * generate millions of IP 5-tuples with an incremented dst
2466 * address to initialize the hash table. */
2468 /* populate the ipv4 hash */
2469 populate_ipv4_many_flow_into_table(
2470 ipv4_l3fwd_lookup_struct[socketid], hash_entry_number);
2472 /* populate the ipv6 hash */
2473 populate_ipv6_many_flow_into_table(
2474 ipv6_l3fwd_lookup_struct[socketid], hash_entry_number);
2477 /* Use data in ipv4/ipv6 l3fwd lookup table directly to initialize the hash table */
2479 /* populate the ipv4 hash */
2480 populate_ipv4_few_flow_into_table(ipv4_l3fwd_lookup_struct[socketid]);
2482 /* populate the ipv6 hash */
2483 populate_ipv6_few_flow_into_table(ipv6_l3fwd_lookup_struct[socketid]);
2489 #if (APP_LOOKUP_METHOD == APP_LOOKUP_LPM)
2491 setup_lpm(int socketid)
2493 struct rte_lpm6_config config;
2498 /* create the LPM table */
2499 snprintf(s, sizeof(s), "IPV4_L3FWD_LPM_%d", socketid);
2500 ipv4_l3fwd_lookup_struct[socketid] = rte_lpm_create(s, socketid,
2501 IPV4_L3FWD_LPM_MAX_RULES, 0);
2502 if (ipv4_l3fwd_lookup_struct[socketid] == NULL)
2503 rte_exit(EXIT_FAILURE, "Unable to create the l3fwd LPM table"
2504 " on socket %d\n", socketid);
2506 /* populate the LPM table */
2507 for (i = 0; i < IPV4_L3FWD_NUM_ROUTES; i++) {
2509 /* skip unused ports */
2510 if ((1 << ipv4_l3fwd_route_array[i].if_out &
2511 enabled_port_mask) == 0)
2514 ret = rte_lpm_add(ipv4_l3fwd_lookup_struct[socketid],
2515 ipv4_l3fwd_route_array[i].ip,
2516 ipv4_l3fwd_route_array[i].depth,
2517 ipv4_l3fwd_route_array[i].if_out);
2520 rte_exit(EXIT_FAILURE, "Unable to add entry %u to the "
2521 "l3fwd LPM table on socket %d\n",
2525 printf("LPM: Adding route 0x%08x / %d (%d)\n",
2526 (unsigned)ipv4_l3fwd_route_array[i].ip,
2527 ipv4_l3fwd_route_array[i].depth,
2528 ipv4_l3fwd_route_array[i].if_out);
2531 /* create the LPM6 table */
2532 snprintf(s, sizeof(s), "IPV6_L3FWD_LPM_%d", socketid);
2534 config.max_rules = IPV6_L3FWD_LPM_MAX_RULES;
2535 config.number_tbl8s = IPV6_L3FWD_LPM_NUMBER_TBL8S;
2537 ipv6_l3fwd_lookup_struct[socketid] = rte_lpm6_create(s, socketid,
2539 if (ipv6_l3fwd_lookup_struct[socketid] == NULL)
2540 rte_exit(EXIT_FAILURE, "Unable to create the l3fwd LPM table"
2541 " on socket %d\n", socketid);
2543 /* populate the LPM table */
2544 for (i = 0; i < IPV6_L3FWD_NUM_ROUTES; i++) {
2546 /* skip unused ports */
2547 if ((1 << ipv6_l3fwd_route_array[i].if_out &
2548 enabled_port_mask) == 0)
2551 ret = rte_lpm6_add(ipv6_l3fwd_lookup_struct[socketid],
2552 ipv6_l3fwd_route_array[i].ip,
2553 ipv6_l3fwd_route_array[i].depth,
2554 ipv6_l3fwd_route_array[i].if_out);
2557 rte_exit(EXIT_FAILURE, "Unable to add entry %u to the "
2558 "l3fwd LPM table on socket %d\n",
2562 printf("LPM: Adding route %s / %d (%d)\n",
2564 ipv6_l3fwd_route_array[i].depth,
2565 ipv6_l3fwd_route_array[i].if_out);
2571 init_mem(unsigned nb_mbuf)
2573 struct lcore_conf *qconf;
2578 for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
2579 if (rte_lcore_is_enabled(lcore_id) == 0)
2583 socketid = rte_lcore_to_socket_id(lcore_id);
2587 if (socketid >= NB_SOCKETS) {
2588 rte_exit(EXIT_FAILURE, "Socket %d of lcore %u is out of range %d\n",
2589 socketid, lcore_id, NB_SOCKETS);
2591 if (pktmbuf_pool[socketid] == NULL) {
2592 snprintf(s, sizeof(s), "mbuf_pool_%d", socketid);
2593 pktmbuf_pool[socketid] =
2594 rte_pktmbuf_pool_create(s, nb_mbuf,
2595 MEMPOOL_CACHE_SIZE, 0,
2596 RTE_MBUF_DEFAULT_BUF_SIZE, socketid);
2597 if (pktmbuf_pool[socketid] == NULL)
2598 rte_exit(EXIT_FAILURE,
2599 "Cannot init mbuf pool on socket %d\n", socketid);
2601 printf("Allocated mbuf pool on socket %d\n", socketid);
2603 #if (APP_LOOKUP_METHOD == APP_LOOKUP_LPM)
2604 setup_lpm(socketid);
2606 setup_hash(socketid);
2609 qconf = &lcore_conf[lcore_id];
2610 qconf->ipv4_lookup_struct = ipv4_l3fwd_lookup_struct[socketid];
2611 qconf->ipv6_lookup_struct = ipv6_l3fwd_lookup_struct[socketid];
2616 /* Check the link status of all ports in up to 9s, and print them finally */
2618 check_all_ports_link_status(uint8_t port_num, uint32_t port_mask)
2620 #define CHECK_INTERVAL 100 /* 100ms */
2621 #define MAX_CHECK_TIME 90 /* 9s (90 * 100ms) in total */
2622 uint8_t portid, count, all_ports_up, print_flag = 0;
2623 struct rte_eth_link link;
2625 printf("\nChecking link status");
2627 for (count = 0; count <= MAX_CHECK_TIME; count++) {
2629 for (portid = 0; portid < port_num; portid++) {
2630 if ((port_mask & (1 << portid)) == 0)
2632 memset(&link, 0, sizeof(link));
2633 rte_eth_link_get_nowait(portid, &link);
2634 /* print link status if flag set */
2635 if (print_flag == 1) {
2636 if (link.link_status)
2637 printf("Port %d Link Up - speed %u "
2638 "Mbps - %s\n", (uint8_t)portid,
2639 (unsigned)link.link_speed,
2640 (link.link_duplex == ETH_LINK_FULL_DUPLEX) ?
2641 ("full-duplex") : ("half-duplex\n"));
2643 printf("Port %d Link Down\n",
2647 /* clear all_ports_up flag if any link down */
2648 if (link.link_status == 0) {
2653 /* after finally printing all link status, get out */
2654 if (print_flag == 1)
2657 if (all_ports_up == 0) {
2660 rte_delay_ms(CHECK_INTERVAL);
2663 /* set the print_flag if all ports up or timeout */
2664 if (all_ports_up == 1 || count == (MAX_CHECK_TIME - 1)) {
2672 main(int argc, char **argv)
2674 struct lcore_conf *qconf;
2675 struct rte_eth_dev_info dev_info;
2676 struct rte_eth_txconf *txconf;
2681 uint32_t n_tx_queue, nb_lcores;
2682 uint8_t portid, nb_rx_queue, queue, socketid;
2685 ret = rte_eal_init(argc, argv);
2687 rte_exit(EXIT_FAILURE, "Invalid EAL parameters\n");
2691 /* pre-init dst MACs for all ports to 02:00:00:00:00:xx */
2692 for (portid = 0; portid < RTE_MAX_ETHPORTS; portid++) {
2693 dest_eth_addr[portid] = ETHER_LOCAL_ADMIN_ADDR + ((uint64_t)portid << 40);
2694 *(uint64_t *)(val_eth + portid) = dest_eth_addr[portid];
2697 /* parse application arguments (after the EAL ones) */
2698 ret = parse_args(argc, argv);
2700 rte_exit(EXIT_FAILURE, "Invalid L3FWD parameters\n");
2702 if (check_lcore_params() < 0)
2703 rte_exit(EXIT_FAILURE, "check_lcore_params failed\n");
2705 ret = init_lcore_rx_queues();
2707 rte_exit(EXIT_FAILURE, "init_lcore_rx_queues failed\n");
2709 nb_ports = rte_eth_dev_count();
2710 if (nb_ports > RTE_MAX_ETHPORTS)
2711 nb_ports = RTE_MAX_ETHPORTS;
2713 if (check_port_config(nb_ports) < 0)
2714 rte_exit(EXIT_FAILURE, "check_port_config failed\n");
2716 nb_lcores = rte_lcore_count();
2718 /* initialize all ports */
2719 for (portid = 0; portid < nb_ports; portid++) {
2720 /* skip ports that are not enabled */
2721 if ((enabled_port_mask & (1 << portid)) == 0) {
2722 printf("\nSkipping disabled port %d\n", portid);
2727 printf("Initializing port %d ... ", portid );
2730 nb_rx_queue = get_port_n_rx_queues(portid);
2731 n_tx_queue = nb_lcores;
2732 if (n_tx_queue > MAX_TX_QUEUE_PER_PORT)
2733 n_tx_queue = MAX_TX_QUEUE_PER_PORT;
2734 printf("Creating queues: nb_rxq=%d nb_txq=%u... ",
2735 nb_rx_queue, (unsigned)n_tx_queue );
2736 ret = rte_eth_dev_configure(portid, nb_rx_queue,
2737 (uint16_t)n_tx_queue, &port_conf);
2739 rte_exit(EXIT_FAILURE, "Cannot configure device: err=%d, port=%d\n",
2742 rte_eth_macaddr_get(portid, &ports_eth_addr[portid]);
2743 print_ethaddr(" Address:", &ports_eth_addr[portid]);
2745 print_ethaddr("Destination:",
2746 (const struct ether_addr *)&dest_eth_addr[portid]);
2750 * prepare src MACs for each port.
2752 ether_addr_copy(&ports_eth_addr[portid],
2753 (struct ether_addr *)(val_eth + portid) + 1);
2756 ret = init_mem(NB_MBUF);
2758 rte_exit(EXIT_FAILURE, "init_mem failed\n");
2760 /* init one TX queue per couple (lcore,port) */
2762 for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
2763 if (rte_lcore_is_enabled(lcore_id) == 0)
2767 socketid = (uint8_t)rte_lcore_to_socket_id(lcore_id);
2771 printf("txq=%u,%d,%d ", lcore_id, queueid, socketid);
2774 rte_eth_dev_info_get(portid, &dev_info);
2775 txconf = &dev_info.default_txconf;
2776 if (port_conf.rxmode.jumbo_frame)
2777 txconf->txq_flags = 0;
2778 ret = rte_eth_tx_queue_setup(portid, queueid, nb_txd,
2781 rte_exit(EXIT_FAILURE, "rte_eth_tx_queue_setup: err=%d, "
2782 "port=%d\n", ret, portid);
2784 qconf = &lcore_conf[lcore_id];
2785 qconf->tx_queue_id[portid] = queueid;
2791 for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
2792 if (rte_lcore_is_enabled(lcore_id) == 0)
2794 qconf = &lcore_conf[lcore_id];
2795 printf("\nInitializing rx queues on lcore %u ... ", lcore_id );
2797 /* init RX queues */
2798 for(queue = 0; queue < qconf->n_rx_queue; ++queue) {
2799 portid = qconf->rx_queue_list[queue].port_id;
2800 queueid = qconf->rx_queue_list[queue].queue_id;
2803 socketid = (uint8_t)rte_lcore_to_socket_id(lcore_id);
2807 printf("rxq=%d,%d,%d ", portid, queueid, socketid);
2810 ret = rte_eth_rx_queue_setup(portid, queueid, nb_rxd,
2813 pktmbuf_pool[socketid]);
2815 rte_exit(EXIT_FAILURE, "rte_eth_rx_queue_setup: err=%d,"
2816 "port=%d\n", ret, portid);
2823 for (portid = 0; portid < nb_ports; portid++) {
2824 if ((enabled_port_mask & (1 << portid)) == 0) {
2828 ret = rte_eth_dev_start(portid);
2830 rte_exit(EXIT_FAILURE, "rte_eth_dev_start: err=%d, port=%d\n",
2834 * If enabled, put device in promiscuous mode.
2835 * This allows IO forwarding mode to forward packets
2836 * to itself through 2 cross-connected ports of the
2840 rte_eth_promiscuous_enable(portid);
2843 check_all_ports_link_status((uint8_t)nb_ports, enabled_port_mask);
2845 /* launch per-lcore init on every lcore */
2846 rte_eal_mp_remote_launch(main_loop, NULL, CALL_MASTER);
2847 RTE_LCORE_FOREACH_SLAVE(lcore_id) {
2848 if (rte_eal_wait_lcore(lcore_id) < 0)