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>
47 #include <rte_common.h>
49 #include <rte_byteorder.h>
51 #include <rte_memory.h>
52 #include <rte_memcpy.h>
53 #include <rte_memzone.h>
55 #include <rte_per_lcore.h>
56 #include <rte_launch.h>
57 #include <rte_atomic.h>
58 #include <rte_cycles.h>
59 #include <rte_prefetch.h>
60 #include <rte_lcore.h>
61 #include <rte_per_lcore.h>
62 #include <rte_branch_prediction.h>
63 #include <rte_interrupts.h>
65 #include <rte_random.h>
66 #include <rte_debug.h>
67 #include <rte_ether.h>
68 #include <rte_ethdev.h>
70 #include <rte_mempool.h>
75 #include <rte_string_fns.h>
77 #include <cmdline_parse.h>
78 #include <cmdline_parse_etheraddr.h>
80 static volatile bool force_quit;
82 #define APP_LOOKUP_EXACT_MATCH 0
83 #define APP_LOOKUP_LPM 1
84 #define DO_RFC_1812_CHECKS
86 #ifndef APP_LOOKUP_METHOD
87 #define APP_LOOKUP_METHOD APP_LOOKUP_LPM
91 * When set to zero, simple forwaring path is eanbled.
92 * When set to one, optimized forwarding path is enabled.
93 * Note that LPM optimisation path uses SSE4.1 instructions.
95 #if ((APP_LOOKUP_METHOD == APP_LOOKUP_LPM) && !defined(__SSE4_1__))
96 #define ENABLE_MULTI_BUFFER_OPTIMIZE 0
98 #define ENABLE_MULTI_BUFFER_OPTIMIZE 1
101 #if (APP_LOOKUP_METHOD == APP_LOOKUP_EXACT_MATCH)
102 #include <rte_hash.h>
103 #elif (APP_LOOKUP_METHOD == APP_LOOKUP_LPM)
105 #include <rte_lpm6.h>
107 #error "APP_LOOKUP_METHOD set to incorrect value"
111 #define IPv6_BYTES_FMT "%02x%02x:%02x%02x:%02x%02x:%02x%02x:"\
112 "%02x%02x:%02x%02x:%02x%02x:%02x%02x"
113 #define IPv6_BYTES(addr) \
114 addr[0], addr[1], addr[2], addr[3], \
115 addr[4], addr[5], addr[6], addr[7], \
116 addr[8], addr[9], addr[10], addr[11],\
117 addr[12], addr[13],addr[14], addr[15]
121 #define RTE_LOGTYPE_L3FWD RTE_LOGTYPE_USER1
123 #define MAX_JUMBO_PKT_LEN 9600
125 #define IPV6_ADDR_LEN 16
127 #define MEMPOOL_CACHE_SIZE 256
130 * This expression is used to calculate the number of mbufs needed depending on user input, taking
131 * into account memory for rx and tx hardware rings, cache per lcore and mtable per port per lcore.
132 * RTE_MAX is used to ensure that NB_MBUF never goes below a minimum value of 8192
135 #define NB_MBUF RTE_MAX ( \
136 (nb_ports*nb_rx_queue*RTE_TEST_RX_DESC_DEFAULT + \
137 nb_ports*nb_lcores*MAX_PKT_BURST + \
138 nb_ports*n_tx_queue*RTE_TEST_TX_DESC_DEFAULT + \
139 nb_lcores*MEMPOOL_CACHE_SIZE), \
142 #define MAX_PKT_BURST 32
143 #define BURST_TX_DRAIN_US 100 /* TX drain every ~100us */
146 * Try to avoid TX buffering if we have at least MAX_TX_BURST packets to send.
148 #define MAX_TX_BURST (MAX_PKT_BURST / 2)
152 /* Configure how many packets ahead to prefetch, when reading packets */
153 #define PREFETCH_OFFSET 3
155 /* Used to mark destination port as 'invalid'. */
156 #define BAD_PORT ((uint16_t)-1)
161 * Configurable number of RX/TX ring descriptors
163 #define RTE_TEST_RX_DESC_DEFAULT 128
164 #define RTE_TEST_TX_DESC_DEFAULT 512
165 static uint16_t nb_rxd = RTE_TEST_RX_DESC_DEFAULT;
166 static uint16_t nb_txd = RTE_TEST_TX_DESC_DEFAULT;
168 /* ethernet addresses of ports */
169 static uint64_t dest_eth_addr[RTE_MAX_ETHPORTS];
170 static struct ether_addr ports_eth_addr[RTE_MAX_ETHPORTS];
172 static __m128i val_eth[RTE_MAX_ETHPORTS];
174 /* replace first 12B of the ethernet header. */
175 #define MASK_ETH 0x3f
177 /* mask of enabled ports */
178 static uint32_t enabled_port_mask = 0;
179 static int promiscuous_on = 0; /**< Ports set in promiscuous mode off by default. */
180 static int numa_on = 1; /**< NUMA is enabled by default. */
182 #if (APP_LOOKUP_METHOD == APP_LOOKUP_EXACT_MATCH)
183 static int ipv6 = 0; /**< ipv6 is false by default. */
188 struct rte_mbuf *m_table[MAX_PKT_BURST];
191 struct lcore_rx_queue {
194 } __rte_cache_aligned;
196 #define MAX_RX_QUEUE_PER_LCORE 16
197 #define MAX_TX_QUEUE_PER_PORT RTE_MAX_ETHPORTS
198 #define MAX_RX_QUEUE_PER_PORT 128
200 #define MAX_LCORE_PARAMS 1024
201 struct lcore_params {
205 } __rte_cache_aligned;
207 static struct lcore_params lcore_params_array[MAX_LCORE_PARAMS];
208 static struct lcore_params lcore_params_array_default[] = {
220 static struct lcore_params * lcore_params = lcore_params_array_default;
221 static uint16_t nb_lcore_params = sizeof(lcore_params_array_default) /
222 sizeof(lcore_params_array_default[0]);
224 static struct rte_eth_conf port_conf = {
226 .mq_mode = ETH_MQ_RX_RSS,
227 .max_rx_pkt_len = ETHER_MAX_LEN,
229 .header_split = 0, /**< Header Split disabled */
230 .hw_ip_checksum = 1, /**< IP checksum offload enabled */
231 .hw_vlan_filter = 0, /**< VLAN filtering disabled */
232 .jumbo_frame = 0, /**< Jumbo Frame Support disabled */
233 .hw_strip_crc = 0, /**< CRC stripped by hardware */
238 .rss_hf = ETH_RSS_IP,
242 .mq_mode = ETH_MQ_TX_NONE,
246 static struct rte_mempool * pktmbuf_pool[NB_SOCKETS];
248 #if (APP_LOOKUP_METHOD == APP_LOOKUP_EXACT_MATCH)
250 #ifdef RTE_MACHINE_CPUFLAG_SSE4_2
251 #include <rte_hash_crc.h>
252 #define DEFAULT_HASH_FUNC rte_hash_crc
254 #include <rte_jhash.h>
255 #define DEFAULT_HASH_FUNC rte_jhash
264 } __attribute__((__packed__));
266 union ipv4_5tuple_host {
279 #define XMM_NUM_IN_IPV6_5TUPLE 3
282 uint8_t ip_dst[IPV6_ADDR_LEN];
283 uint8_t ip_src[IPV6_ADDR_LEN];
287 } __attribute__((__packed__));
289 union ipv6_5tuple_host {
294 uint8_t ip_src[IPV6_ADDR_LEN];
295 uint8_t ip_dst[IPV6_ADDR_LEN];
300 __m128i xmm[XMM_NUM_IN_IPV6_5TUPLE];
303 struct ipv4_l3fwd_route {
304 struct ipv4_5tuple key;
308 struct ipv6_l3fwd_route {
309 struct ipv6_5tuple key;
313 static struct ipv4_l3fwd_route ipv4_l3fwd_route_array[] = {
314 {{IPv4(101,0,0,0), IPv4(100,10,0,1), 101, 11, IPPROTO_TCP}, 0},
315 {{IPv4(201,0,0,0), IPv4(200,20,0,1), 102, 12, IPPROTO_TCP}, 1},
316 {{IPv4(111,0,0,0), IPv4(100,30,0,1), 101, 11, IPPROTO_TCP}, 2},
317 {{IPv4(211,0,0,0), IPv4(200,40,0,1), 102, 12, IPPROTO_TCP}, 3},
320 static struct ipv6_l3fwd_route ipv6_l3fwd_route_array[] = {
322 {0xfe, 0x80, 0, 0, 0, 0, 0, 0, 0x02, 0x1e, 0x67, 0xff, 0xfe, 0, 0, 0},
323 {0xfe, 0x80, 0, 0, 0, 0, 0, 0, 0x02, 0x1b, 0x21, 0xff, 0xfe, 0x91, 0x38, 0x05},
324 101, 11, IPPROTO_TCP}, 0},
327 {0xfe, 0x90, 0, 0, 0, 0, 0, 0, 0x02, 0x1e, 0x67, 0xff, 0xfe, 0, 0, 0},
328 {0xfe, 0x90, 0, 0, 0, 0, 0, 0, 0x02, 0x1b, 0x21, 0xff, 0xfe, 0x91, 0x38, 0x05},
329 102, 12, IPPROTO_TCP}, 1},
332 {0xfe, 0xa0, 0, 0, 0, 0, 0, 0, 0x02, 0x1e, 0x67, 0xff, 0xfe, 0, 0, 0},
333 {0xfe, 0xa0, 0, 0, 0, 0, 0, 0, 0x02, 0x1b, 0x21, 0xff, 0xfe, 0x91, 0x38, 0x05},
334 101, 11, IPPROTO_TCP}, 2},
337 {0xfe, 0xb0, 0, 0, 0, 0, 0, 0, 0x02, 0x1e, 0x67, 0xff, 0xfe, 0, 0, 0},
338 {0xfe, 0xb0, 0, 0, 0, 0, 0, 0, 0x02, 0x1b, 0x21, 0xff, 0xfe, 0x91, 0x38, 0x05},
339 102, 12, IPPROTO_TCP}, 3},
342 typedef struct rte_hash lookup_struct_t;
343 static lookup_struct_t *ipv4_l3fwd_lookup_struct[NB_SOCKETS];
344 static lookup_struct_t *ipv6_l3fwd_lookup_struct[NB_SOCKETS];
346 #ifdef RTE_ARCH_X86_64
347 /* default to 4 million hash entries (approx) */
348 #define L3FWD_HASH_ENTRIES 1024*1024*4
350 /* 32-bit has less address-space for hugepage memory, limit to 1M entries */
351 #define L3FWD_HASH_ENTRIES 1024*1024*1
353 #define HASH_ENTRY_NUMBER_DEFAULT 4
355 static uint32_t hash_entry_number = HASH_ENTRY_NUMBER_DEFAULT;
357 static inline uint32_t
358 ipv4_hash_crc(const void *data, __rte_unused uint32_t data_len,
361 const union ipv4_5tuple_host *k;
367 p = (const uint32_t *)&k->port_src;
369 #ifdef RTE_MACHINE_CPUFLAG_SSE4_2
370 init_val = rte_hash_crc_4byte(t, init_val);
371 init_val = rte_hash_crc_4byte(k->ip_src, init_val);
372 init_val = rte_hash_crc_4byte(k->ip_dst, init_val);
373 init_val = rte_hash_crc_4byte(*p, init_val);
374 #else /* RTE_MACHINE_CPUFLAG_SSE4_2 */
375 init_val = rte_jhash_1word(t, init_val);
376 init_val = rte_jhash_1word(k->ip_src, init_val);
377 init_val = rte_jhash_1word(k->ip_dst, init_val);
378 init_val = rte_jhash_1word(*p, init_val);
379 #endif /* RTE_MACHINE_CPUFLAG_SSE4_2 */
383 static inline uint32_t
384 ipv6_hash_crc(const void *data, __rte_unused uint32_t data_len, uint32_t init_val)
386 const union ipv6_5tuple_host *k;
389 #ifdef RTE_MACHINE_CPUFLAG_SSE4_2
390 const uint32_t *ip_src0, *ip_src1, *ip_src2, *ip_src3;
391 const uint32_t *ip_dst0, *ip_dst1, *ip_dst2, *ip_dst3;
392 #endif /* RTE_MACHINE_CPUFLAG_SSE4_2 */
396 p = (const uint32_t *)&k->port_src;
398 #ifdef RTE_MACHINE_CPUFLAG_SSE4_2
399 ip_src0 = (const uint32_t *) k->ip_src;
400 ip_src1 = (const uint32_t *)(k->ip_src+4);
401 ip_src2 = (const uint32_t *)(k->ip_src+8);
402 ip_src3 = (const uint32_t *)(k->ip_src+12);
403 ip_dst0 = (const uint32_t *) k->ip_dst;
404 ip_dst1 = (const uint32_t *)(k->ip_dst+4);
405 ip_dst2 = (const uint32_t *)(k->ip_dst+8);
406 ip_dst3 = (const uint32_t *)(k->ip_dst+12);
407 init_val = rte_hash_crc_4byte(t, init_val);
408 init_val = rte_hash_crc_4byte(*ip_src0, init_val);
409 init_val = rte_hash_crc_4byte(*ip_src1, init_val);
410 init_val = rte_hash_crc_4byte(*ip_src2, init_val);
411 init_val = rte_hash_crc_4byte(*ip_src3, init_val);
412 init_val = rte_hash_crc_4byte(*ip_dst0, init_val);
413 init_val = rte_hash_crc_4byte(*ip_dst1, init_val);
414 init_val = rte_hash_crc_4byte(*ip_dst2, init_val);
415 init_val = rte_hash_crc_4byte(*ip_dst3, init_val);
416 init_val = rte_hash_crc_4byte(*p, init_val);
417 #else /* RTE_MACHINE_CPUFLAG_SSE4_2 */
418 init_val = rte_jhash_1word(t, init_val);
419 init_val = rte_jhash(k->ip_src, sizeof(uint8_t) * IPV6_ADDR_LEN, init_val);
420 init_val = rte_jhash(k->ip_dst, sizeof(uint8_t) * IPV6_ADDR_LEN, init_val);
421 init_val = rte_jhash_1word(*p, init_val);
422 #endif /* RTE_MACHINE_CPUFLAG_SSE4_2 */
426 #define IPV4_L3FWD_NUM_ROUTES \
427 (sizeof(ipv4_l3fwd_route_array) / sizeof(ipv4_l3fwd_route_array[0]))
429 #define IPV6_L3FWD_NUM_ROUTES \
430 (sizeof(ipv6_l3fwd_route_array) / sizeof(ipv6_l3fwd_route_array[0]))
432 static uint8_t ipv4_l3fwd_out_if[L3FWD_HASH_ENTRIES] __rte_cache_aligned;
433 static uint8_t ipv6_l3fwd_out_if[L3FWD_HASH_ENTRIES] __rte_cache_aligned;
437 #if (APP_LOOKUP_METHOD == APP_LOOKUP_LPM)
438 struct ipv4_l3fwd_route {
444 struct ipv6_l3fwd_route {
450 static struct ipv4_l3fwd_route ipv4_l3fwd_route_array[] = {
451 {IPv4(1,1,1,0), 24, 0},
452 {IPv4(2,1,1,0), 24, 1},
453 {IPv4(3,1,1,0), 24, 2},
454 {IPv4(4,1,1,0), 24, 3},
455 {IPv4(5,1,1,0), 24, 4},
456 {IPv4(6,1,1,0), 24, 5},
457 {IPv4(7,1,1,0), 24, 6},
458 {IPv4(8,1,1,0), 24, 7},
461 static struct ipv6_l3fwd_route ipv6_l3fwd_route_array[] = {
462 {{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}, 48, 0},
463 {{2,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}, 48, 1},
464 {{3,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}, 48, 2},
465 {{4,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}, 48, 3},
466 {{5,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}, 48, 4},
467 {{6,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}, 48, 5},
468 {{7,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}, 48, 6},
469 {{8,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}, 48, 7},
472 #define IPV4_L3FWD_NUM_ROUTES \
473 (sizeof(ipv4_l3fwd_route_array) / sizeof(ipv4_l3fwd_route_array[0]))
474 #define IPV6_L3FWD_NUM_ROUTES \
475 (sizeof(ipv6_l3fwd_route_array) / sizeof(ipv6_l3fwd_route_array[0]))
477 #define IPV4_L3FWD_LPM_MAX_RULES 1024
478 #define IPV6_L3FWD_LPM_MAX_RULES 1024
479 #define IPV6_L3FWD_LPM_NUMBER_TBL8S (1 << 16)
481 typedef struct rte_lpm lookup_struct_t;
482 typedef struct rte_lpm6 lookup6_struct_t;
483 static lookup_struct_t *ipv4_l3fwd_lookup_struct[NB_SOCKETS];
484 static lookup6_struct_t *ipv6_l3fwd_lookup_struct[NB_SOCKETS];
489 struct lcore_rx_queue rx_queue_list[MAX_RX_QUEUE_PER_LCORE];
490 uint16_t tx_queue_id[RTE_MAX_ETHPORTS];
491 struct mbuf_table tx_mbufs[RTE_MAX_ETHPORTS];
492 lookup_struct_t * ipv4_lookup_struct;
493 #if (APP_LOOKUP_METHOD == APP_LOOKUP_LPM)
494 lookup6_struct_t * ipv6_lookup_struct;
496 lookup_struct_t * ipv6_lookup_struct;
498 } __rte_cache_aligned;
500 static struct lcore_conf lcore_conf[RTE_MAX_LCORE];
502 /* Send burst of packets on an output interface */
504 send_burst(struct lcore_conf *qconf, uint16_t n, uint8_t port)
506 struct rte_mbuf **m_table;
510 queueid = qconf->tx_queue_id[port];
511 m_table = (struct rte_mbuf **)qconf->tx_mbufs[port].m_table;
513 ret = rte_eth_tx_burst(port, queueid, m_table, n);
514 if (unlikely(ret < n)) {
516 rte_pktmbuf_free(m_table[ret]);
523 /* Enqueue a single packet, and send burst if queue is filled */
525 send_single_packet(struct rte_mbuf *m, uint8_t port)
529 struct lcore_conf *qconf;
531 lcore_id = rte_lcore_id();
533 qconf = &lcore_conf[lcore_id];
534 len = qconf->tx_mbufs[port].len;
535 qconf->tx_mbufs[port].m_table[len] = m;
538 /* enough pkts to be sent */
539 if (unlikely(len == MAX_PKT_BURST)) {
540 send_burst(qconf, MAX_PKT_BURST, port);
544 qconf->tx_mbufs[port].len = len;
548 #if ((APP_LOOKUP_METHOD == APP_LOOKUP_LPM) && \
549 (ENABLE_MULTI_BUFFER_OPTIMIZE == 1))
550 static inline __attribute__((always_inline)) void
551 send_packetsx4(struct lcore_conf *qconf, uint8_t port,
552 struct rte_mbuf *m[], uint32_t num)
556 len = qconf->tx_mbufs[port].len;
559 * If TX buffer for that queue is empty, and we have enough packets,
560 * then send them straightway.
562 if (num >= MAX_TX_BURST && len == 0) {
563 n = rte_eth_tx_burst(port, qconf->tx_queue_id[port], m, num);
564 if (unlikely(n < num)) {
566 rte_pktmbuf_free(m[n]);
573 * Put packets into TX buffer for that queue.
577 n = (n > MAX_PKT_BURST) ? MAX_PKT_BURST - len : num;
580 switch (n % FWDSTEP) {
583 qconf->tx_mbufs[port].m_table[len + j] = m[j];
586 qconf->tx_mbufs[port].m_table[len + j] = m[j];
589 qconf->tx_mbufs[port].m_table[len + j] = m[j];
592 qconf->tx_mbufs[port].m_table[len + j] = m[j];
599 /* enough pkts to be sent */
600 if (unlikely(len == MAX_PKT_BURST)) {
602 send_burst(qconf, MAX_PKT_BURST, port);
604 /* copy rest of the packets into the TX buffer. */
607 switch (len % FWDSTEP) {
610 qconf->tx_mbufs[port].m_table[j] = m[n + j];
613 qconf->tx_mbufs[port].m_table[j] = m[n + j];
616 qconf->tx_mbufs[port].m_table[j] = m[n + j];
619 qconf->tx_mbufs[port].m_table[j] = m[n + j];
625 qconf->tx_mbufs[port].len = len;
627 #endif /* APP_LOOKUP_LPM */
629 #ifdef DO_RFC_1812_CHECKS
631 is_valid_ipv4_pkt(struct ipv4_hdr *pkt, uint32_t link_len)
633 /* From http://www.rfc-editor.org/rfc/rfc1812.txt section 5.2.2 */
635 * 1. The packet length reported by the Link Layer must be large
636 * enough to hold the minimum length legal IP datagram (20 bytes).
638 if (link_len < sizeof(struct ipv4_hdr))
641 /* 2. The IP checksum must be correct. */
642 /* this is checked in H/W */
645 * 3. The IP version number must be 4. If the version number is not 4
646 * then the packet may be another version of IP, such as IPng or
649 if (((pkt->version_ihl) >> 4) != 4)
652 * 4. The IP header length field must be large enough to hold the
653 * minimum length legal IP datagram (20 bytes = 5 words).
655 if ((pkt->version_ihl & 0xf) < 5)
659 * 5. The IP total length field must be large enough to hold the IP
660 * datagram header, whose length is specified in the IP header length
663 if (rte_cpu_to_be_16(pkt->total_length) < sizeof(struct ipv4_hdr))
670 #if (APP_LOOKUP_METHOD == APP_LOOKUP_EXACT_MATCH)
672 static __m128i mask0;
673 static __m128i mask1;
674 static __m128i mask2;
675 static inline uint8_t
676 get_ipv4_dst_port(void *ipv4_hdr, uint8_t portid, lookup_struct_t * ipv4_l3fwd_lookup_struct)
679 union ipv4_5tuple_host key;
681 ipv4_hdr = (uint8_t *)ipv4_hdr + offsetof(struct ipv4_hdr, time_to_live);
682 __m128i data = _mm_loadu_si128((__m128i*)(ipv4_hdr));
683 /* Get 5 tuple: dst port, src port, dst IP address, src IP address and protocol */
684 key.xmm = _mm_and_si128(data, mask0);
685 /* Find destination port */
686 ret = rte_hash_lookup(ipv4_l3fwd_lookup_struct, (const void *)&key);
687 return (uint8_t)((ret < 0)? portid : ipv4_l3fwd_out_if[ret]);
690 static inline uint8_t
691 get_ipv6_dst_port(void *ipv6_hdr, uint8_t portid, lookup_struct_t * ipv6_l3fwd_lookup_struct)
694 union ipv6_5tuple_host key;
696 ipv6_hdr = (uint8_t *)ipv6_hdr + offsetof(struct ipv6_hdr, payload_len);
697 __m128i data0 = _mm_loadu_si128((__m128i*)(ipv6_hdr));
698 __m128i data1 = _mm_loadu_si128((__m128i*)(((uint8_t*)ipv6_hdr)+sizeof(__m128i)));
699 __m128i data2 = _mm_loadu_si128((__m128i*)(((uint8_t*)ipv6_hdr)+sizeof(__m128i)+sizeof(__m128i)));
700 /* Get part of 5 tuple: src IP address lower 96 bits and protocol */
701 key.xmm[0] = _mm_and_si128(data0, mask1);
702 /* Get part of 5 tuple: dst IP address lower 96 bits and src IP address higher 32 bits */
704 /* Get part of 5 tuple: dst port and src port and dst IP address higher 32 bits */
705 key.xmm[2] = _mm_and_si128(data2, mask2);
707 /* Find destination port */
708 ret = rte_hash_lookup(ipv6_l3fwd_lookup_struct, (const void *)&key);
709 return (uint8_t)((ret < 0)? portid : ipv6_l3fwd_out_if[ret]);
713 #if (APP_LOOKUP_METHOD == APP_LOOKUP_LPM)
715 static inline uint8_t
716 get_ipv4_dst_port(void *ipv4_hdr, uint8_t portid, lookup_struct_t * ipv4_l3fwd_lookup_struct)
720 return (uint8_t) ((rte_lpm_lookup(ipv4_l3fwd_lookup_struct,
721 rte_be_to_cpu_32(((struct ipv4_hdr *)ipv4_hdr)->dst_addr),
722 &next_hop) == 0) ? next_hop : portid);
725 static inline uint8_t
726 get_ipv6_dst_port(void *ipv6_hdr, uint8_t portid, lookup6_struct_t * ipv6_l3fwd_lookup_struct)
729 return (uint8_t) ((rte_lpm6_lookup(ipv6_l3fwd_lookup_struct,
730 ((struct ipv6_hdr*)ipv6_hdr)->dst_addr, &next_hop) == 0)?
735 static inline void l3fwd_simple_forward(struct rte_mbuf *m, uint8_t portid,
736 struct lcore_conf *qconf) __attribute__((unused));
738 #if ((APP_LOOKUP_METHOD == APP_LOOKUP_EXACT_MATCH) && \
739 (ENABLE_MULTI_BUFFER_OPTIMIZE == 1))
741 #define MASK_ALL_PKTS 0xff
742 #define EXCLUDE_1ST_PKT 0xfe
743 #define EXCLUDE_2ND_PKT 0xfd
744 #define EXCLUDE_3RD_PKT 0xfb
745 #define EXCLUDE_4TH_PKT 0xf7
746 #define EXCLUDE_5TH_PKT 0xef
747 #define EXCLUDE_6TH_PKT 0xdf
748 #define EXCLUDE_7TH_PKT 0xbf
749 #define EXCLUDE_8TH_PKT 0x7f
752 simple_ipv4_fwd_8pkts(struct rte_mbuf *m[8], uint8_t portid, struct lcore_conf *qconf)
754 struct ether_hdr *eth_hdr[8];
755 struct ipv4_hdr *ipv4_hdr[8];
758 union ipv4_5tuple_host key[8];
761 eth_hdr[0] = rte_pktmbuf_mtod(m[0], struct ether_hdr *);
762 eth_hdr[1] = rte_pktmbuf_mtod(m[1], struct ether_hdr *);
763 eth_hdr[2] = rte_pktmbuf_mtod(m[2], struct ether_hdr *);
764 eth_hdr[3] = rte_pktmbuf_mtod(m[3], struct ether_hdr *);
765 eth_hdr[4] = rte_pktmbuf_mtod(m[4], struct ether_hdr *);
766 eth_hdr[5] = rte_pktmbuf_mtod(m[5], struct ether_hdr *);
767 eth_hdr[6] = rte_pktmbuf_mtod(m[6], struct ether_hdr *);
768 eth_hdr[7] = rte_pktmbuf_mtod(m[7], struct ether_hdr *);
770 /* Handle IPv4 headers.*/
771 ipv4_hdr[0] = rte_pktmbuf_mtod_offset(m[0], struct ipv4_hdr *,
772 sizeof(struct ether_hdr));
773 ipv4_hdr[1] = rte_pktmbuf_mtod_offset(m[1], struct ipv4_hdr *,
774 sizeof(struct ether_hdr));
775 ipv4_hdr[2] = rte_pktmbuf_mtod_offset(m[2], struct ipv4_hdr *,
776 sizeof(struct ether_hdr));
777 ipv4_hdr[3] = rte_pktmbuf_mtod_offset(m[3], struct ipv4_hdr *,
778 sizeof(struct ether_hdr));
779 ipv4_hdr[4] = rte_pktmbuf_mtod_offset(m[4], struct ipv4_hdr *,
780 sizeof(struct ether_hdr));
781 ipv4_hdr[5] = rte_pktmbuf_mtod_offset(m[5], struct ipv4_hdr *,
782 sizeof(struct ether_hdr));
783 ipv4_hdr[6] = rte_pktmbuf_mtod_offset(m[6], struct ipv4_hdr *,
784 sizeof(struct ether_hdr));
785 ipv4_hdr[7] = rte_pktmbuf_mtod_offset(m[7], struct ipv4_hdr *,
786 sizeof(struct ether_hdr));
788 #ifdef DO_RFC_1812_CHECKS
789 /* Check to make sure the packet is valid (RFC1812) */
790 uint8_t valid_mask = MASK_ALL_PKTS;
791 if (is_valid_ipv4_pkt(ipv4_hdr[0], m[0]->pkt_len) < 0) {
792 rte_pktmbuf_free(m[0]);
793 valid_mask &= EXCLUDE_1ST_PKT;
795 if (is_valid_ipv4_pkt(ipv4_hdr[1], m[1]->pkt_len) < 0) {
796 rte_pktmbuf_free(m[1]);
797 valid_mask &= EXCLUDE_2ND_PKT;
799 if (is_valid_ipv4_pkt(ipv4_hdr[2], m[2]->pkt_len) < 0) {
800 rte_pktmbuf_free(m[2]);
801 valid_mask &= EXCLUDE_3RD_PKT;
803 if (is_valid_ipv4_pkt(ipv4_hdr[3], m[3]->pkt_len) < 0) {
804 rte_pktmbuf_free(m[3]);
805 valid_mask &= EXCLUDE_4TH_PKT;
807 if (is_valid_ipv4_pkt(ipv4_hdr[4], m[4]->pkt_len) < 0) {
808 rte_pktmbuf_free(m[4]);
809 valid_mask &= EXCLUDE_5TH_PKT;
811 if (is_valid_ipv4_pkt(ipv4_hdr[5], m[5]->pkt_len) < 0) {
812 rte_pktmbuf_free(m[5]);
813 valid_mask &= EXCLUDE_6TH_PKT;
815 if (is_valid_ipv4_pkt(ipv4_hdr[6], m[6]->pkt_len) < 0) {
816 rte_pktmbuf_free(m[6]);
817 valid_mask &= EXCLUDE_7TH_PKT;
819 if (is_valid_ipv4_pkt(ipv4_hdr[7], m[7]->pkt_len) < 0) {
820 rte_pktmbuf_free(m[7]);
821 valid_mask &= EXCLUDE_8TH_PKT;
823 if (unlikely(valid_mask != MASK_ALL_PKTS)) {
824 if (valid_mask == 0){
828 for (i = 0; i < 8; i++) {
829 if ((0x1 << i) & valid_mask) {
830 l3fwd_simple_forward(m[i], portid, qconf);
836 #endif // End of #ifdef DO_RFC_1812_CHECKS
838 data[0] = _mm_loadu_si128(rte_pktmbuf_mtod_offset(m[0], __m128i *,
839 sizeof(struct ether_hdr) +
840 offsetof(struct ipv4_hdr, time_to_live)));
841 data[1] = _mm_loadu_si128(rte_pktmbuf_mtod_offset(m[1], __m128i *,
842 sizeof(struct ether_hdr) +
843 offsetof(struct ipv4_hdr, time_to_live)));
844 data[2] = _mm_loadu_si128(rte_pktmbuf_mtod_offset(m[2], __m128i *,
845 sizeof(struct ether_hdr) +
846 offsetof(struct ipv4_hdr, time_to_live)));
847 data[3] = _mm_loadu_si128(rte_pktmbuf_mtod_offset(m[3], __m128i *,
848 sizeof(struct ether_hdr) +
849 offsetof(struct ipv4_hdr, time_to_live)));
850 data[4] = _mm_loadu_si128(rte_pktmbuf_mtod_offset(m[4], __m128i *,
851 sizeof(struct ether_hdr) +
852 offsetof(struct ipv4_hdr, time_to_live)));
853 data[5] = _mm_loadu_si128(rte_pktmbuf_mtod_offset(m[5], __m128i *,
854 sizeof(struct ether_hdr) +
855 offsetof(struct ipv4_hdr, time_to_live)));
856 data[6] = _mm_loadu_si128(rte_pktmbuf_mtod_offset(m[6], __m128i *,
857 sizeof(struct ether_hdr) +
858 offsetof(struct ipv4_hdr, time_to_live)));
859 data[7] = _mm_loadu_si128(rte_pktmbuf_mtod_offset(m[7], __m128i *,
860 sizeof(struct ether_hdr) +
861 offsetof(struct ipv4_hdr, time_to_live)));
863 key[0].xmm = _mm_and_si128(data[0], mask0);
864 key[1].xmm = _mm_and_si128(data[1], mask0);
865 key[2].xmm = _mm_and_si128(data[2], mask0);
866 key[3].xmm = _mm_and_si128(data[3], mask0);
867 key[4].xmm = _mm_and_si128(data[4], mask0);
868 key[5].xmm = _mm_and_si128(data[5], mask0);
869 key[6].xmm = _mm_and_si128(data[6], mask0);
870 key[7].xmm = _mm_and_si128(data[7], mask0);
872 const void *key_array[8] = {&key[0], &key[1], &key[2], &key[3],
873 &key[4], &key[5], &key[6], &key[7]};
875 rte_hash_lookup_multi(qconf->ipv4_lookup_struct, &key_array[0], 8, ret);
876 dst_port[0] = (uint8_t) ((ret[0] < 0) ? portid : ipv4_l3fwd_out_if[ret[0]]);
877 dst_port[1] = (uint8_t) ((ret[1] < 0) ? portid : ipv4_l3fwd_out_if[ret[1]]);
878 dst_port[2] = (uint8_t) ((ret[2] < 0) ? portid : ipv4_l3fwd_out_if[ret[2]]);
879 dst_port[3] = (uint8_t) ((ret[3] < 0) ? portid : ipv4_l3fwd_out_if[ret[3]]);
880 dst_port[4] = (uint8_t) ((ret[4] < 0) ? portid : ipv4_l3fwd_out_if[ret[4]]);
881 dst_port[5] = (uint8_t) ((ret[5] < 0) ? portid : ipv4_l3fwd_out_if[ret[5]]);
882 dst_port[6] = (uint8_t) ((ret[6] < 0) ? portid : ipv4_l3fwd_out_if[ret[6]]);
883 dst_port[7] = (uint8_t) ((ret[7] < 0) ? portid : ipv4_l3fwd_out_if[ret[7]]);
885 if (dst_port[0] >= RTE_MAX_ETHPORTS || (enabled_port_mask & 1 << dst_port[0]) == 0)
886 dst_port[0] = portid;
887 if (dst_port[1] >= RTE_MAX_ETHPORTS || (enabled_port_mask & 1 << dst_port[1]) == 0)
888 dst_port[1] = portid;
889 if (dst_port[2] >= RTE_MAX_ETHPORTS || (enabled_port_mask & 1 << dst_port[2]) == 0)
890 dst_port[2] = portid;
891 if (dst_port[3] >= RTE_MAX_ETHPORTS || (enabled_port_mask & 1 << dst_port[3]) == 0)
892 dst_port[3] = portid;
893 if (dst_port[4] >= RTE_MAX_ETHPORTS || (enabled_port_mask & 1 << dst_port[4]) == 0)
894 dst_port[4] = portid;
895 if (dst_port[5] >= RTE_MAX_ETHPORTS || (enabled_port_mask & 1 << dst_port[5]) == 0)
896 dst_port[5] = portid;
897 if (dst_port[6] >= RTE_MAX_ETHPORTS || (enabled_port_mask & 1 << dst_port[6]) == 0)
898 dst_port[6] = portid;
899 if (dst_port[7] >= RTE_MAX_ETHPORTS || (enabled_port_mask & 1 << dst_port[7]) == 0)
900 dst_port[7] = portid;
902 #ifdef DO_RFC_1812_CHECKS
903 /* Update time to live and header checksum */
904 --(ipv4_hdr[0]->time_to_live);
905 --(ipv4_hdr[1]->time_to_live);
906 --(ipv4_hdr[2]->time_to_live);
907 --(ipv4_hdr[3]->time_to_live);
908 ++(ipv4_hdr[0]->hdr_checksum);
909 ++(ipv4_hdr[1]->hdr_checksum);
910 ++(ipv4_hdr[2]->hdr_checksum);
911 ++(ipv4_hdr[3]->hdr_checksum);
912 --(ipv4_hdr[4]->time_to_live);
913 --(ipv4_hdr[5]->time_to_live);
914 --(ipv4_hdr[6]->time_to_live);
915 --(ipv4_hdr[7]->time_to_live);
916 ++(ipv4_hdr[4]->hdr_checksum);
917 ++(ipv4_hdr[5]->hdr_checksum);
918 ++(ipv4_hdr[6]->hdr_checksum);
919 ++(ipv4_hdr[7]->hdr_checksum);
923 *(uint64_t *)ð_hdr[0]->d_addr = dest_eth_addr[dst_port[0]];
924 *(uint64_t *)ð_hdr[1]->d_addr = dest_eth_addr[dst_port[1]];
925 *(uint64_t *)ð_hdr[2]->d_addr = dest_eth_addr[dst_port[2]];
926 *(uint64_t *)ð_hdr[3]->d_addr = dest_eth_addr[dst_port[3]];
927 *(uint64_t *)ð_hdr[4]->d_addr = dest_eth_addr[dst_port[4]];
928 *(uint64_t *)ð_hdr[5]->d_addr = dest_eth_addr[dst_port[5]];
929 *(uint64_t *)ð_hdr[6]->d_addr = dest_eth_addr[dst_port[6]];
930 *(uint64_t *)ð_hdr[7]->d_addr = dest_eth_addr[dst_port[7]];
933 ether_addr_copy(&ports_eth_addr[dst_port[0]], ð_hdr[0]->s_addr);
934 ether_addr_copy(&ports_eth_addr[dst_port[1]], ð_hdr[1]->s_addr);
935 ether_addr_copy(&ports_eth_addr[dst_port[2]], ð_hdr[2]->s_addr);
936 ether_addr_copy(&ports_eth_addr[dst_port[3]], ð_hdr[3]->s_addr);
937 ether_addr_copy(&ports_eth_addr[dst_port[4]], ð_hdr[4]->s_addr);
938 ether_addr_copy(&ports_eth_addr[dst_port[5]], ð_hdr[5]->s_addr);
939 ether_addr_copy(&ports_eth_addr[dst_port[6]], ð_hdr[6]->s_addr);
940 ether_addr_copy(&ports_eth_addr[dst_port[7]], ð_hdr[7]->s_addr);
942 send_single_packet(m[0], (uint8_t)dst_port[0]);
943 send_single_packet(m[1], (uint8_t)dst_port[1]);
944 send_single_packet(m[2], (uint8_t)dst_port[2]);
945 send_single_packet(m[3], (uint8_t)dst_port[3]);
946 send_single_packet(m[4], (uint8_t)dst_port[4]);
947 send_single_packet(m[5], (uint8_t)dst_port[5]);
948 send_single_packet(m[6], (uint8_t)dst_port[6]);
949 send_single_packet(m[7], (uint8_t)dst_port[7]);
953 static inline void get_ipv6_5tuple(struct rte_mbuf* m0, __m128i mask0, __m128i mask1,
954 union ipv6_5tuple_host * key)
956 __m128i tmpdata0 = _mm_loadu_si128(rte_pktmbuf_mtod_offset(m0, __m128i *, sizeof(struct ether_hdr) + offsetof(struct ipv6_hdr, payload_len)));
957 __m128i tmpdata1 = _mm_loadu_si128(rte_pktmbuf_mtod_offset(m0, __m128i *, sizeof(struct ether_hdr) + offsetof(struct ipv6_hdr, payload_len) + sizeof(__m128i)));
958 __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)));
959 key->xmm[0] = _mm_and_si128(tmpdata0, mask0);
960 key->xmm[1] = tmpdata1;
961 key->xmm[2] = _mm_and_si128(tmpdata2, mask1);
966 simple_ipv6_fwd_8pkts(struct rte_mbuf *m[8], uint8_t portid, struct lcore_conf *qconf)
968 struct ether_hdr *eth_hdr[8];
969 __attribute__((unused)) struct ipv6_hdr *ipv6_hdr[8];
972 union ipv6_5tuple_host key[8];
974 eth_hdr[0] = rte_pktmbuf_mtod(m[0], struct ether_hdr *);
975 eth_hdr[1] = rte_pktmbuf_mtod(m[1], struct ether_hdr *);
976 eth_hdr[2] = rte_pktmbuf_mtod(m[2], struct ether_hdr *);
977 eth_hdr[3] = rte_pktmbuf_mtod(m[3], struct ether_hdr *);
978 eth_hdr[4] = rte_pktmbuf_mtod(m[4], struct ether_hdr *);
979 eth_hdr[5] = rte_pktmbuf_mtod(m[5], struct ether_hdr *);
980 eth_hdr[6] = rte_pktmbuf_mtod(m[6], struct ether_hdr *);
981 eth_hdr[7] = rte_pktmbuf_mtod(m[7], struct ether_hdr *);
983 /* Handle IPv6 headers.*/
984 ipv6_hdr[0] = rte_pktmbuf_mtod_offset(m[0], struct ipv6_hdr *,
985 sizeof(struct ether_hdr));
986 ipv6_hdr[1] = rte_pktmbuf_mtod_offset(m[1], struct ipv6_hdr *,
987 sizeof(struct ether_hdr));
988 ipv6_hdr[2] = rte_pktmbuf_mtod_offset(m[2], struct ipv6_hdr *,
989 sizeof(struct ether_hdr));
990 ipv6_hdr[3] = rte_pktmbuf_mtod_offset(m[3], struct ipv6_hdr *,
991 sizeof(struct ether_hdr));
992 ipv6_hdr[4] = rte_pktmbuf_mtod_offset(m[4], struct ipv6_hdr *,
993 sizeof(struct ether_hdr));
994 ipv6_hdr[5] = rte_pktmbuf_mtod_offset(m[5], struct ipv6_hdr *,
995 sizeof(struct ether_hdr));
996 ipv6_hdr[6] = rte_pktmbuf_mtod_offset(m[6], struct ipv6_hdr *,
997 sizeof(struct ether_hdr));
998 ipv6_hdr[7] = rte_pktmbuf_mtod_offset(m[7], struct ipv6_hdr *,
999 sizeof(struct ether_hdr));
1001 get_ipv6_5tuple(m[0], mask1, mask2, &key[0]);
1002 get_ipv6_5tuple(m[1], mask1, mask2, &key[1]);
1003 get_ipv6_5tuple(m[2], mask1, mask2, &key[2]);
1004 get_ipv6_5tuple(m[3], mask1, mask2, &key[3]);
1005 get_ipv6_5tuple(m[4], mask1, mask2, &key[4]);
1006 get_ipv6_5tuple(m[5], mask1, mask2, &key[5]);
1007 get_ipv6_5tuple(m[6], mask1, mask2, &key[6]);
1008 get_ipv6_5tuple(m[7], mask1, mask2, &key[7]);
1010 const void *key_array[8] = {&key[0], &key[1], &key[2], &key[3],
1011 &key[4], &key[5], &key[6], &key[7]};
1013 rte_hash_lookup_multi(qconf->ipv6_lookup_struct, &key_array[0], 8, ret);
1014 dst_port[0] = (uint8_t) ((ret[0] < 0) ? portid:ipv6_l3fwd_out_if[ret[0]]);
1015 dst_port[1] = (uint8_t) ((ret[1] < 0) ? portid:ipv6_l3fwd_out_if[ret[1]]);
1016 dst_port[2] = (uint8_t) ((ret[2] < 0) ? portid:ipv6_l3fwd_out_if[ret[2]]);
1017 dst_port[3] = (uint8_t) ((ret[3] < 0) ? portid:ipv6_l3fwd_out_if[ret[3]]);
1018 dst_port[4] = (uint8_t) ((ret[4] < 0) ? portid:ipv6_l3fwd_out_if[ret[4]]);
1019 dst_port[5] = (uint8_t) ((ret[5] < 0) ? portid:ipv6_l3fwd_out_if[ret[5]]);
1020 dst_port[6] = (uint8_t) ((ret[6] < 0) ? portid:ipv6_l3fwd_out_if[ret[6]]);
1021 dst_port[7] = (uint8_t) ((ret[7] < 0) ? portid:ipv6_l3fwd_out_if[ret[7]]);
1023 if (dst_port[0] >= RTE_MAX_ETHPORTS || (enabled_port_mask & 1 << dst_port[0]) == 0)
1024 dst_port[0] = portid;
1025 if (dst_port[1] >= RTE_MAX_ETHPORTS || (enabled_port_mask & 1 << dst_port[1]) == 0)
1026 dst_port[1] = portid;
1027 if (dst_port[2] >= RTE_MAX_ETHPORTS || (enabled_port_mask & 1 << dst_port[2]) == 0)
1028 dst_port[2] = portid;
1029 if (dst_port[3] >= RTE_MAX_ETHPORTS || (enabled_port_mask & 1 << dst_port[3]) == 0)
1030 dst_port[3] = portid;
1031 if (dst_port[4] >= RTE_MAX_ETHPORTS || (enabled_port_mask & 1 << dst_port[4]) == 0)
1032 dst_port[4] = portid;
1033 if (dst_port[5] >= RTE_MAX_ETHPORTS || (enabled_port_mask & 1 << dst_port[5]) == 0)
1034 dst_port[5] = portid;
1035 if (dst_port[6] >= RTE_MAX_ETHPORTS || (enabled_port_mask & 1 << dst_port[6]) == 0)
1036 dst_port[6] = portid;
1037 if (dst_port[7] >= RTE_MAX_ETHPORTS || (enabled_port_mask & 1 << dst_port[7]) == 0)
1038 dst_port[7] = portid;
1041 *(uint64_t *)ð_hdr[0]->d_addr = dest_eth_addr[dst_port[0]];
1042 *(uint64_t *)ð_hdr[1]->d_addr = dest_eth_addr[dst_port[1]];
1043 *(uint64_t *)ð_hdr[2]->d_addr = dest_eth_addr[dst_port[2]];
1044 *(uint64_t *)ð_hdr[3]->d_addr = dest_eth_addr[dst_port[3]];
1045 *(uint64_t *)ð_hdr[4]->d_addr = dest_eth_addr[dst_port[4]];
1046 *(uint64_t *)ð_hdr[5]->d_addr = dest_eth_addr[dst_port[5]];
1047 *(uint64_t *)ð_hdr[6]->d_addr = dest_eth_addr[dst_port[6]];
1048 *(uint64_t *)ð_hdr[7]->d_addr = dest_eth_addr[dst_port[7]];
1051 ether_addr_copy(&ports_eth_addr[dst_port[0]], ð_hdr[0]->s_addr);
1052 ether_addr_copy(&ports_eth_addr[dst_port[1]], ð_hdr[1]->s_addr);
1053 ether_addr_copy(&ports_eth_addr[dst_port[2]], ð_hdr[2]->s_addr);
1054 ether_addr_copy(&ports_eth_addr[dst_port[3]], ð_hdr[3]->s_addr);
1055 ether_addr_copy(&ports_eth_addr[dst_port[4]], ð_hdr[4]->s_addr);
1056 ether_addr_copy(&ports_eth_addr[dst_port[5]], ð_hdr[5]->s_addr);
1057 ether_addr_copy(&ports_eth_addr[dst_port[6]], ð_hdr[6]->s_addr);
1058 ether_addr_copy(&ports_eth_addr[dst_port[7]], ð_hdr[7]->s_addr);
1060 send_single_packet(m[0], (uint8_t)dst_port[0]);
1061 send_single_packet(m[1], (uint8_t)dst_port[1]);
1062 send_single_packet(m[2], (uint8_t)dst_port[2]);
1063 send_single_packet(m[3], (uint8_t)dst_port[3]);
1064 send_single_packet(m[4], (uint8_t)dst_port[4]);
1065 send_single_packet(m[5], (uint8_t)dst_port[5]);
1066 send_single_packet(m[6], (uint8_t)dst_port[6]);
1067 send_single_packet(m[7], (uint8_t)dst_port[7]);
1070 #endif /* APP_LOOKUP_METHOD */
1072 static inline __attribute__((always_inline)) void
1073 l3fwd_simple_forward(struct rte_mbuf *m, uint8_t portid, struct lcore_conf *qconf)
1075 struct ether_hdr *eth_hdr;
1076 struct ipv4_hdr *ipv4_hdr;
1079 eth_hdr = rte_pktmbuf_mtod(m, struct ether_hdr *);
1081 if (RTE_ETH_IS_IPV4_HDR(m->packet_type)) {
1082 /* Handle IPv4 headers.*/
1083 ipv4_hdr = rte_pktmbuf_mtod_offset(m, struct ipv4_hdr *,
1084 sizeof(struct ether_hdr));
1086 #ifdef DO_RFC_1812_CHECKS
1087 /* Check to make sure the packet is valid (RFC1812) */
1088 if (is_valid_ipv4_pkt(ipv4_hdr, m->pkt_len) < 0) {
1089 rte_pktmbuf_free(m);
1094 dst_port = get_ipv4_dst_port(ipv4_hdr, portid,
1095 qconf->ipv4_lookup_struct);
1096 if (dst_port >= RTE_MAX_ETHPORTS ||
1097 (enabled_port_mask & 1 << dst_port) == 0)
1100 #ifdef DO_RFC_1812_CHECKS
1101 /* Update time to live and header checksum */
1102 --(ipv4_hdr->time_to_live);
1103 ++(ipv4_hdr->hdr_checksum);
1106 *(uint64_t *)ð_hdr->d_addr = dest_eth_addr[dst_port];
1109 ether_addr_copy(&ports_eth_addr[dst_port], ð_hdr->s_addr);
1111 send_single_packet(m, dst_port);
1112 } else if (RTE_ETH_IS_IPV6_HDR(m->packet_type)) {
1113 /* Handle IPv6 headers.*/
1114 struct ipv6_hdr *ipv6_hdr;
1116 ipv6_hdr = rte_pktmbuf_mtod_offset(m, struct ipv6_hdr *,
1117 sizeof(struct ether_hdr));
1119 dst_port = get_ipv6_dst_port(ipv6_hdr, portid, qconf->ipv6_lookup_struct);
1121 if (dst_port >= RTE_MAX_ETHPORTS || (enabled_port_mask & 1 << dst_port) == 0)
1125 *(uint64_t *)ð_hdr->d_addr = dest_eth_addr[dst_port];
1128 ether_addr_copy(&ports_eth_addr[dst_port], ð_hdr->s_addr);
1130 send_single_packet(m, dst_port);
1132 /* Free the mbuf that contains non-IPV4/IPV6 packet */
1133 rte_pktmbuf_free(m);
1136 #if ((APP_LOOKUP_METHOD == APP_LOOKUP_LPM) && \
1137 (ENABLE_MULTI_BUFFER_OPTIMIZE == 1))
1138 #ifdef DO_RFC_1812_CHECKS
1140 #define IPV4_MIN_VER_IHL 0x45
1141 #define IPV4_MAX_VER_IHL 0x4f
1142 #define IPV4_MAX_VER_IHL_DIFF (IPV4_MAX_VER_IHL - IPV4_MIN_VER_IHL)
1144 /* Minimum value of IPV4 total length (20B) in network byte order. */
1145 #define IPV4_MIN_LEN_BE (sizeof(struct ipv4_hdr) << 8)
1148 * From http://www.rfc-editor.org/rfc/rfc1812.txt section 5.2.2:
1149 * - The IP version number must be 4.
1150 * - The IP header length field must be large enough to hold the
1151 * minimum length legal IP datagram (20 bytes = 5 words).
1152 * - The IP total length field must be large enough to hold the IP
1153 * datagram header, whose length is specified in the IP header length
1155 * If we encounter invalid IPV4 packet, then set destination port for it
1156 * to BAD_PORT value.
1158 static inline __attribute__((always_inline)) void
1159 rfc1812_process(struct ipv4_hdr *ipv4_hdr, uint16_t *dp, uint32_t ptype)
1163 if (RTE_ETH_IS_IPV4_HDR(ptype)) {
1164 ihl = ipv4_hdr->version_ihl - IPV4_MIN_VER_IHL;
1166 ipv4_hdr->time_to_live--;
1167 ipv4_hdr->hdr_checksum++;
1169 if (ihl > IPV4_MAX_VER_IHL_DIFF ||
1170 ((uint8_t)ipv4_hdr->total_length == 0 &&
1171 ipv4_hdr->total_length < IPV4_MIN_LEN_BE)) {
1178 #define rfc1812_process(mb, dp) do { } while (0)
1179 #endif /* DO_RFC_1812_CHECKS */
1180 #endif /* APP_LOOKUP_LPM && ENABLE_MULTI_BUFFER_OPTIMIZE */
1183 #if ((APP_LOOKUP_METHOD == APP_LOOKUP_LPM) && \
1184 (ENABLE_MULTI_BUFFER_OPTIMIZE == 1))
1186 static inline __attribute__((always_inline)) uint16_t
1187 get_dst_port(const struct lcore_conf *qconf, struct rte_mbuf *pkt,
1188 uint32_t dst_ipv4, uint8_t portid)
1191 struct ipv6_hdr *ipv6_hdr;
1192 struct ether_hdr *eth_hdr;
1194 if (RTE_ETH_IS_IPV4_HDR(pkt->packet_type)) {
1195 if (rte_lpm_lookup(qconf->ipv4_lookup_struct, dst_ipv4,
1198 } else if (RTE_ETH_IS_IPV6_HDR(pkt->packet_type)) {
1199 eth_hdr = rte_pktmbuf_mtod(pkt, struct ether_hdr *);
1200 ipv6_hdr = (struct ipv6_hdr *)(eth_hdr + 1);
1201 if (rte_lpm6_lookup(qconf->ipv6_lookup_struct,
1202 ipv6_hdr->dst_addr, &next_hop) != 0)
1212 process_packet(struct lcore_conf *qconf, struct rte_mbuf *pkt,
1213 uint16_t *dst_port, uint8_t portid)
1215 struct ether_hdr *eth_hdr;
1216 struct ipv4_hdr *ipv4_hdr;
1221 eth_hdr = rte_pktmbuf_mtod(pkt, struct ether_hdr *);
1222 ipv4_hdr = (struct ipv4_hdr *)(eth_hdr + 1);
1224 dst_ipv4 = ipv4_hdr->dst_addr;
1225 dst_ipv4 = rte_be_to_cpu_32(dst_ipv4);
1226 dp = get_dst_port(qconf, pkt, dst_ipv4, portid);
1228 te = _mm_loadu_si128((__m128i *)eth_hdr);
1232 rfc1812_process(ipv4_hdr, dst_port, pkt->packet_type);
1234 te = _mm_blend_epi16(te, ve, MASK_ETH);
1235 _mm_storeu_si128((__m128i *)eth_hdr, te);
1239 * Read packet_type and destination IPV4 addresses from 4 mbufs.
1242 processx4_step1(struct rte_mbuf *pkt[FWDSTEP],
1244 uint32_t *ipv4_flag)
1246 struct ipv4_hdr *ipv4_hdr;
1247 struct ether_hdr *eth_hdr;
1248 uint32_t x0, x1, x2, x3;
1250 eth_hdr = rte_pktmbuf_mtod(pkt[0], struct ether_hdr *);
1251 ipv4_hdr = (struct ipv4_hdr *)(eth_hdr + 1);
1252 x0 = ipv4_hdr->dst_addr;
1253 ipv4_flag[0] = pkt[0]->packet_type & RTE_PTYPE_L3_IPV4;
1255 eth_hdr = rte_pktmbuf_mtod(pkt[1], struct ether_hdr *);
1256 ipv4_hdr = (struct ipv4_hdr *)(eth_hdr + 1);
1257 x1 = ipv4_hdr->dst_addr;
1258 ipv4_flag[0] &= pkt[1]->packet_type;
1260 eth_hdr = rte_pktmbuf_mtod(pkt[2], struct ether_hdr *);
1261 ipv4_hdr = (struct ipv4_hdr *)(eth_hdr + 1);
1262 x2 = ipv4_hdr->dst_addr;
1263 ipv4_flag[0] &= pkt[2]->packet_type;
1265 eth_hdr = rte_pktmbuf_mtod(pkt[3], struct ether_hdr *);
1266 ipv4_hdr = (struct ipv4_hdr *)(eth_hdr + 1);
1267 x3 = ipv4_hdr->dst_addr;
1268 ipv4_flag[0] &= pkt[3]->packet_type;
1270 dip[0] = _mm_set_epi32(x3, x2, x1, x0);
1274 * Lookup into LPM for destination port.
1275 * If lookup fails, use incoming port (portid) as destination port.
1278 processx4_step2(const struct lcore_conf *qconf,
1282 struct rte_mbuf *pkt[FWDSTEP],
1283 uint16_t dprt[FWDSTEP])
1286 const __m128i bswap_mask = _mm_set_epi8(12, 13, 14, 15, 8, 9, 10, 11,
1287 4, 5, 6, 7, 0, 1, 2, 3);
1289 /* Byte swap 4 IPV4 addresses. */
1290 dip = _mm_shuffle_epi8(dip, bswap_mask);
1292 /* if all 4 packets are IPV4. */
1293 if (likely(ipv4_flag)) {
1294 rte_lpm_lookupx4(qconf->ipv4_lookup_struct, dip, dprt, portid);
1297 dprt[0] = get_dst_port(qconf, pkt[0], dst.u32[0], portid);
1298 dprt[1] = get_dst_port(qconf, pkt[1], dst.u32[1], portid);
1299 dprt[2] = get_dst_port(qconf, pkt[2], dst.u32[2], portid);
1300 dprt[3] = get_dst_port(qconf, pkt[3], dst.u32[3], portid);
1305 * Update source and destination MAC addresses in the ethernet header.
1306 * Perform RFC1812 checks and updates for IPV4 packets.
1309 processx4_step3(struct rte_mbuf *pkt[FWDSTEP], uint16_t dst_port[FWDSTEP])
1311 __m128i te[FWDSTEP];
1312 __m128i ve[FWDSTEP];
1313 __m128i *p[FWDSTEP];
1315 p[0] = rte_pktmbuf_mtod(pkt[0], __m128i *);
1316 p[1] = rte_pktmbuf_mtod(pkt[1], __m128i *);
1317 p[2] = rte_pktmbuf_mtod(pkt[2], __m128i *);
1318 p[3] = rte_pktmbuf_mtod(pkt[3], __m128i *);
1320 ve[0] = val_eth[dst_port[0]];
1321 te[0] = _mm_loadu_si128(p[0]);
1323 ve[1] = val_eth[dst_port[1]];
1324 te[1] = _mm_loadu_si128(p[1]);
1326 ve[2] = val_eth[dst_port[2]];
1327 te[2] = _mm_loadu_si128(p[2]);
1329 ve[3] = val_eth[dst_port[3]];
1330 te[3] = _mm_loadu_si128(p[3]);
1332 /* Update first 12 bytes, keep rest bytes intact. */
1333 te[0] = _mm_blend_epi16(te[0], ve[0], MASK_ETH);
1334 te[1] = _mm_blend_epi16(te[1], ve[1], MASK_ETH);
1335 te[2] = _mm_blend_epi16(te[2], ve[2], MASK_ETH);
1336 te[3] = _mm_blend_epi16(te[3], ve[3], MASK_ETH);
1338 _mm_storeu_si128(p[0], te[0]);
1339 _mm_storeu_si128(p[1], te[1]);
1340 _mm_storeu_si128(p[2], te[2]);
1341 _mm_storeu_si128(p[3], te[3]);
1343 rfc1812_process((struct ipv4_hdr *)((struct ether_hdr *)p[0] + 1),
1344 &dst_port[0], pkt[0]->packet_type);
1345 rfc1812_process((struct ipv4_hdr *)((struct ether_hdr *)p[1] + 1),
1346 &dst_port[1], pkt[1]->packet_type);
1347 rfc1812_process((struct ipv4_hdr *)((struct ether_hdr *)p[2] + 1),
1348 &dst_port[2], pkt[2]->packet_type);
1349 rfc1812_process((struct ipv4_hdr *)((struct ether_hdr *)p[3] + 1),
1350 &dst_port[3], pkt[3]->packet_type);
1354 * We group consecutive packets with the same destionation port into one burst.
1355 * To avoid extra latency this is done together with some other packet
1356 * processing, but after we made a final decision about packet's destination.
1357 * To do this we maintain:
1358 * pnum - array of number of consecutive packets with the same dest port for
1359 * each packet in the input burst.
1360 * lp - pointer to the last updated element in the pnum.
1361 * dlp - dest port value lp corresponds to.
1364 #define GRPSZ (1 << FWDSTEP)
1365 #define GRPMSK (GRPSZ - 1)
1367 #define GROUP_PORT_STEP(dlp, dcp, lp, pn, idx) do { \
1368 if (likely((dlp) == (dcp)[(idx)])) { \
1371 (dlp) = (dcp)[idx]; \
1372 (lp) = (pn) + (idx); \
1378 * Group consecutive packets with the same destination port in bursts of 4.
1379 * Suppose we have array of destionation ports:
1380 * dst_port[] = {a, b, c, d,, e, ... }
1381 * dp1 should contain: <a, b, c, d>, dp2: <b, c, d, e>.
1382 * We doing 4 comparisions at once and the result is 4 bit mask.
1383 * This mask is used as an index into prebuild array of pnum values.
1385 static inline uint16_t *
1386 port_groupx4(uint16_t pn[FWDSTEP + 1], uint16_t *lp, __m128i dp1, __m128i dp2)
1388 static const struct {
1389 uint64_t pnum; /* prebuild 4 values for pnum[]. */
1390 int32_t idx; /* index for new last updated elemnet. */
1391 uint16_t lpv; /* add value to the last updated element. */
1394 /* 0: a != b, b != c, c != d, d != e */
1395 .pnum = UINT64_C(0x0001000100010001),
1400 /* 1: a == b, b != c, c != d, d != e */
1401 .pnum = UINT64_C(0x0001000100010002),
1406 /* 2: a != b, b == c, c != d, d != e */
1407 .pnum = UINT64_C(0x0001000100020001),
1412 /* 3: a == b, b == c, c != d, d != e */
1413 .pnum = UINT64_C(0x0001000100020003),
1418 /* 4: a != b, b != c, c == d, d != e */
1419 .pnum = UINT64_C(0x0001000200010001),
1424 /* 5: a == b, b != c, c == d, d != e */
1425 .pnum = UINT64_C(0x0001000200010002),
1430 /* 6: a != b, b == c, c == d, d != e */
1431 .pnum = UINT64_C(0x0001000200030001),
1436 /* 7: a == b, b == c, c == d, d != e */
1437 .pnum = UINT64_C(0x0001000200030004),
1442 /* 8: a != b, b != c, c != d, d == e */
1443 .pnum = UINT64_C(0x0002000100010001),
1448 /* 9: a == b, b != c, c != d, d == e */
1449 .pnum = UINT64_C(0x0002000100010002),
1454 /* 0xa: a != b, b == c, c != d, d == e */
1455 .pnum = UINT64_C(0x0002000100020001),
1460 /* 0xb: a == b, b == c, c != d, d == e */
1461 .pnum = UINT64_C(0x0002000100020003),
1466 /* 0xc: a != b, b != c, c == d, d == e */
1467 .pnum = UINT64_C(0x0002000300010001),
1472 /* 0xd: a == b, b != c, c == d, d == e */
1473 .pnum = UINT64_C(0x0002000300010002),
1478 /* 0xe: a != b, b == c, c == d, d == e */
1479 .pnum = UINT64_C(0x0002000300040001),
1484 /* 0xf: a == b, b == c, c == d, d == e */
1485 .pnum = UINT64_C(0x0002000300040005),
1492 uint16_t u16[FWDSTEP + 1];
1494 } *pnum = (void *)pn;
1498 dp1 = _mm_cmpeq_epi16(dp1, dp2);
1499 dp1 = _mm_unpacklo_epi16(dp1, dp1);
1500 v = _mm_movemask_ps((__m128)dp1);
1502 /* update last port counter. */
1503 lp[0] += gptbl[v].lpv;
1505 /* if dest port value has changed. */
1507 lp = pnum->u16 + gptbl[v].idx;
1509 pnum->u64 = gptbl[v].pnum;
1515 #endif /* APP_LOOKUP_METHOD */
1517 /* main processing loop */
1519 main_loop(__attribute__((unused)) void *dummy)
1521 struct rte_mbuf *pkts_burst[MAX_PKT_BURST];
1523 uint64_t prev_tsc, diff_tsc, cur_tsc;
1525 uint8_t portid, queueid;
1526 struct lcore_conf *qconf;
1527 const uint64_t drain_tsc = (rte_get_tsc_hz() + US_PER_S - 1) /
1528 US_PER_S * BURST_TX_DRAIN_US;
1530 #if ((APP_LOOKUP_METHOD == APP_LOOKUP_LPM) && \
1531 (ENABLE_MULTI_BUFFER_OPTIMIZE == 1))
1535 uint16_t dst_port[MAX_PKT_BURST];
1536 __m128i dip[MAX_PKT_BURST / FWDSTEP];
1537 uint32_t ipv4_flag[MAX_PKT_BURST / FWDSTEP];
1538 uint16_t pnum[MAX_PKT_BURST + 1];
1543 lcore_id = rte_lcore_id();
1544 qconf = &lcore_conf[lcore_id];
1546 if (qconf->n_rx_queue == 0) {
1547 RTE_LOG(INFO, L3FWD, "lcore %u has nothing to do\n", lcore_id);
1551 RTE_LOG(INFO, L3FWD, "entering main loop on lcore %u\n", lcore_id);
1553 for (i = 0; i < qconf->n_rx_queue; i++) {
1555 portid = qconf->rx_queue_list[i].port_id;
1556 queueid = qconf->rx_queue_list[i].queue_id;
1557 RTE_LOG(INFO, L3FWD, " -- lcoreid=%u portid=%hhu rxqueueid=%hhu\n", lcore_id,
1561 while (!force_quit) {
1563 cur_tsc = rte_rdtsc();
1566 * TX burst queue drain
1568 diff_tsc = cur_tsc - prev_tsc;
1569 if (unlikely(diff_tsc > drain_tsc)) {
1572 * This could be optimized (use queueid instead of
1573 * portid), but it is not called so often
1575 for (portid = 0; portid < RTE_MAX_ETHPORTS; portid++) {
1576 if (qconf->tx_mbufs[portid].len == 0)
1579 qconf->tx_mbufs[portid].len,
1581 qconf->tx_mbufs[portid].len = 0;
1588 * Read packet from RX queues
1590 for (i = 0; i < qconf->n_rx_queue; ++i) {
1591 portid = qconf->rx_queue_list[i].port_id;
1592 queueid = qconf->rx_queue_list[i].queue_id;
1593 nb_rx = rte_eth_rx_burst(portid, queueid, pkts_burst,
1598 #if (ENABLE_MULTI_BUFFER_OPTIMIZE == 1)
1599 #if (APP_LOOKUP_METHOD == APP_LOOKUP_EXACT_MATCH)
1602 * Send nb_rx - nb_rx%8 packets
1605 int32_t n = RTE_ALIGN_FLOOR(nb_rx, 8);
1606 for (j = 0; j < n; j += 8) {
1608 pkts_burst[j]->packet_type &
1609 pkts_burst[j+1]->packet_type &
1610 pkts_burst[j+2]->packet_type &
1611 pkts_burst[j+3]->packet_type &
1612 pkts_burst[j+4]->packet_type &
1613 pkts_burst[j+5]->packet_type &
1614 pkts_burst[j+6]->packet_type &
1615 pkts_burst[j+7]->packet_type;
1616 if (pkt_type & RTE_PTYPE_L3_IPV4) {
1617 simple_ipv4_fwd_8pkts(
1618 &pkts_burst[j], portid, qconf);
1619 } else if (pkt_type &
1620 RTE_PTYPE_L3_IPV6) {
1621 simple_ipv6_fwd_8pkts(&pkts_burst[j],
1624 l3fwd_simple_forward(pkts_burst[j],
1626 l3fwd_simple_forward(pkts_burst[j+1],
1628 l3fwd_simple_forward(pkts_burst[j+2],
1630 l3fwd_simple_forward(pkts_burst[j+3],
1632 l3fwd_simple_forward(pkts_burst[j+4],
1634 l3fwd_simple_forward(pkts_burst[j+5],
1636 l3fwd_simple_forward(pkts_burst[j+6],
1638 l3fwd_simple_forward(pkts_burst[j+7],
1642 for (; j < nb_rx ; j++) {
1643 l3fwd_simple_forward(pkts_burst[j],
1647 #elif (APP_LOOKUP_METHOD == APP_LOOKUP_LPM)
1649 k = RTE_ALIGN_FLOOR(nb_rx, FWDSTEP);
1650 for (j = 0; j != k; j += FWDSTEP) {
1651 processx4_step1(&pkts_burst[j],
1653 &ipv4_flag[j / FWDSTEP]);
1656 k = RTE_ALIGN_FLOOR(nb_rx, FWDSTEP);
1657 for (j = 0; j != k; j += FWDSTEP) {
1658 processx4_step2(qconf, dip[j / FWDSTEP],
1659 ipv4_flag[j / FWDSTEP], portid,
1660 &pkts_burst[j], &dst_port[j]);
1664 * Finish packet processing and group consecutive
1665 * packets with the same destination port.
1667 k = RTE_ALIGN_FLOOR(nb_rx, FWDSTEP);
1674 processx4_step3(pkts_burst, dst_port);
1676 /* dp1: <d[0], d[1], d[2], d[3], ... > */
1677 dp1 = _mm_loadu_si128((__m128i *)dst_port);
1679 for (j = FWDSTEP; j != k; j += FWDSTEP) {
1680 processx4_step3(&pkts_burst[j],
1685 * <d[j-3], d[j-2], d[j-1], d[j], ... >
1687 dp2 = _mm_loadu_si128((__m128i *)
1688 &dst_port[j - FWDSTEP + 1]);
1689 lp = port_groupx4(&pnum[j - FWDSTEP],
1694 * <d[j], d[j+1], d[j+2], d[j+3], ... >
1696 dp1 = _mm_srli_si128(dp2,
1698 sizeof(dst_port[0]));
1702 * dp2: <d[j-3], d[j-2], d[j-1], d[j-1], ... >
1704 dp2 = _mm_shufflelo_epi16(dp1, 0xf9);
1705 lp = port_groupx4(&pnum[j - FWDSTEP], lp,
1709 * remove values added by the last repeated
1713 dlp = dst_port[j - 1];
1715 /* set dlp and lp to the never used values. */
1717 lp = pnum + MAX_PKT_BURST;
1720 /* Process up to last 3 packets one by one. */
1721 switch (nb_rx % FWDSTEP) {
1723 process_packet(qconf, pkts_burst[j],
1724 dst_port + j, portid);
1725 GROUP_PORT_STEP(dlp, dst_port, lp, pnum, j);
1728 process_packet(qconf, pkts_burst[j],
1729 dst_port + j, portid);
1730 GROUP_PORT_STEP(dlp, dst_port, lp, pnum, j);
1733 process_packet(qconf, pkts_burst[j],
1734 dst_port + j, portid);
1735 GROUP_PORT_STEP(dlp, dst_port, lp, pnum, j);
1740 * Send packets out, through destination port.
1741 * Consecuteve pacekts with the same destination port
1742 * are already grouped together.
1743 * If destination port for the packet equals BAD_PORT,
1744 * then free the packet without sending it out.
1746 for (j = 0; j < nb_rx; j += k) {
1754 if (likely(pn != BAD_PORT)) {
1755 send_packetsx4(qconf, pn,
1758 for (m = j; m != j + k; m++)
1759 rte_pktmbuf_free(pkts_burst[m]);
1763 #endif /* APP_LOOKUP_METHOD */
1764 #else /* ENABLE_MULTI_BUFFER_OPTIMIZE == 0 */
1766 /* Prefetch first packets */
1767 for (j = 0; j < PREFETCH_OFFSET && j < nb_rx; j++) {
1768 rte_prefetch0(rte_pktmbuf_mtod(
1769 pkts_burst[j], void *));
1772 /* Prefetch and forward already prefetched packets */
1773 for (j = 0; j < (nb_rx - PREFETCH_OFFSET); j++) {
1774 rte_prefetch0(rte_pktmbuf_mtod(pkts_burst[
1775 j + PREFETCH_OFFSET], void *));
1776 l3fwd_simple_forward(pkts_burst[j], portid,
1780 /* Forward remaining prefetched packets */
1781 for (; j < nb_rx; j++) {
1782 l3fwd_simple_forward(pkts_burst[j], portid,
1785 #endif /* ENABLE_MULTI_BUFFER_OPTIMIZE */
1794 check_lcore_params(void)
1796 uint8_t queue, lcore;
1800 for (i = 0; i < nb_lcore_params; ++i) {
1801 queue = lcore_params[i].queue_id;
1802 if (queue >= MAX_RX_QUEUE_PER_PORT) {
1803 printf("invalid queue number: %hhu\n", queue);
1806 lcore = lcore_params[i].lcore_id;
1807 if (!rte_lcore_is_enabled(lcore)) {
1808 printf("error: lcore %hhu is not enabled in lcore mask\n", lcore);
1811 if ((socketid = rte_lcore_to_socket_id(lcore) != 0) &&
1813 printf("warning: lcore %hhu is on socket %d with numa off \n",
1821 check_port_config(const unsigned nb_ports)
1826 for (i = 0; i < nb_lcore_params; ++i) {
1827 portid = lcore_params[i].port_id;
1828 if ((enabled_port_mask & (1 << portid)) == 0) {
1829 printf("port %u is not enabled in port mask\n", portid);
1832 if (portid >= nb_ports) {
1833 printf("port %u is not present on the board\n", portid);
1841 get_port_n_rx_queues(const uint8_t port)
1846 for (i = 0; i < nb_lcore_params; ++i) {
1847 if (lcore_params[i].port_id == port && lcore_params[i].queue_id > queue)
1848 queue = lcore_params[i].queue_id;
1850 return (uint8_t)(++queue);
1854 init_lcore_rx_queues(void)
1856 uint16_t i, nb_rx_queue;
1859 for (i = 0; i < nb_lcore_params; ++i) {
1860 lcore = lcore_params[i].lcore_id;
1861 nb_rx_queue = lcore_conf[lcore].n_rx_queue;
1862 if (nb_rx_queue >= MAX_RX_QUEUE_PER_LCORE) {
1863 printf("error: too many queues (%u) for lcore: %u\n",
1864 (unsigned)nb_rx_queue + 1, (unsigned)lcore);
1867 lcore_conf[lcore].rx_queue_list[nb_rx_queue].port_id =
1868 lcore_params[i].port_id;
1869 lcore_conf[lcore].rx_queue_list[nb_rx_queue].queue_id =
1870 lcore_params[i].queue_id;
1871 lcore_conf[lcore].n_rx_queue++;
1879 print_usage(const char *prgname)
1881 printf ("%s [EAL options] -- -p PORTMASK -P"
1882 " [--config (port,queue,lcore)[,(port,queue,lcore]]"
1883 " [--enable-jumbo [--max-pkt-len PKTLEN]]\n"
1884 " -p PORTMASK: hexadecimal bitmask of ports to configure\n"
1885 " -P : enable promiscuous mode\n"
1886 " --config (port,queue,lcore): rx queues configuration\n"
1887 " --eth-dest=X,MM:MM:MM:MM:MM:MM: optional, ethernet destination for port X\n"
1888 " --no-numa: optional, disable numa awareness\n"
1889 " --ipv6: optional, specify it if running ipv6 packets\n"
1890 " --enable-jumbo: enable jumbo frame"
1891 " which max packet len is PKTLEN in decimal (64-9600)\n"
1892 " --hash-entry-num: specify the hash entry number in hexadecimal to be setup\n",
1896 static int parse_max_pkt_len(const char *pktlen)
1901 /* parse decimal string */
1902 len = strtoul(pktlen, &end, 10);
1903 if ((pktlen[0] == '\0') || (end == NULL) || (*end != '\0'))
1913 parse_portmask(const char *portmask)
1918 /* parse hexadecimal string */
1919 pm = strtoul(portmask, &end, 16);
1920 if ((portmask[0] == '\0') || (end == NULL) || (*end != '\0'))
1929 #if (APP_LOOKUP_METHOD == APP_LOOKUP_EXACT_MATCH)
1931 parse_hash_entry_number(const char *hash_entry_num)
1934 unsigned long hash_en;
1935 /* parse hexadecimal string */
1936 hash_en = strtoul(hash_entry_num, &end, 16);
1937 if ((hash_entry_num[0] == '\0') || (end == NULL) || (*end != '\0'))
1948 parse_config(const char *q_arg)
1951 const char *p, *p0 = q_arg;
1959 unsigned long int_fld[_NUM_FLD];
1960 char *str_fld[_NUM_FLD];
1964 nb_lcore_params = 0;
1966 while ((p = strchr(p0,'(')) != NULL) {
1968 if((p0 = strchr(p,')')) == NULL)
1972 if(size >= sizeof(s))
1975 snprintf(s, sizeof(s), "%.*s", size, p);
1976 if (rte_strsplit(s, sizeof(s), str_fld, _NUM_FLD, ',') != _NUM_FLD)
1978 for (i = 0; i < _NUM_FLD; i++){
1980 int_fld[i] = strtoul(str_fld[i], &end, 0);
1981 if (errno != 0 || end == str_fld[i] || int_fld[i] > 255)
1984 if (nb_lcore_params >= MAX_LCORE_PARAMS) {
1985 printf("exceeded max number of lcore params: %hu\n",
1989 lcore_params_array[nb_lcore_params].port_id = (uint8_t)int_fld[FLD_PORT];
1990 lcore_params_array[nb_lcore_params].queue_id = (uint8_t)int_fld[FLD_QUEUE];
1991 lcore_params_array[nb_lcore_params].lcore_id = (uint8_t)int_fld[FLD_LCORE];
1994 lcore_params = lcore_params_array;
1999 parse_eth_dest(const char *optarg)
2003 uint8_t c, *dest, peer_addr[6];
2006 portid = strtoul(optarg, &port_end, 10);
2007 if (errno != 0 || port_end == optarg || *port_end++ != ',')
2008 rte_exit(EXIT_FAILURE,
2009 "Invalid eth-dest: %s", optarg);
2010 if (portid >= RTE_MAX_ETHPORTS)
2011 rte_exit(EXIT_FAILURE,
2012 "eth-dest: port %d >= RTE_MAX_ETHPORTS(%d)\n",
2013 portid, RTE_MAX_ETHPORTS);
2015 if (cmdline_parse_etheraddr(NULL, port_end,
2016 &peer_addr, sizeof(peer_addr)) < 0)
2017 rte_exit(EXIT_FAILURE,
2018 "Invalid ethernet address: %s\n",
2020 dest = (uint8_t *)&dest_eth_addr[portid];
2021 for (c = 0; c < 6; c++)
2022 dest[c] = peer_addr[c];
2023 *(uint64_t *)(val_eth + portid) = dest_eth_addr[portid];
2026 #define CMD_LINE_OPT_CONFIG "config"
2027 #define CMD_LINE_OPT_ETH_DEST "eth-dest"
2028 #define CMD_LINE_OPT_NO_NUMA "no-numa"
2029 #define CMD_LINE_OPT_IPV6 "ipv6"
2030 #define CMD_LINE_OPT_ENABLE_JUMBO "enable-jumbo"
2031 #define CMD_LINE_OPT_HASH_ENTRY_NUM "hash-entry-num"
2033 /* Parse the argument given in the command line of the application */
2035 parse_args(int argc, char **argv)
2040 char *prgname = argv[0];
2041 static struct option lgopts[] = {
2042 {CMD_LINE_OPT_CONFIG, 1, 0, 0},
2043 {CMD_LINE_OPT_ETH_DEST, 1, 0, 0},
2044 {CMD_LINE_OPT_NO_NUMA, 0, 0, 0},
2045 {CMD_LINE_OPT_IPV6, 0, 0, 0},
2046 {CMD_LINE_OPT_ENABLE_JUMBO, 0, 0, 0},
2047 {CMD_LINE_OPT_HASH_ENTRY_NUM, 1, 0, 0},
2053 while ((opt = getopt_long(argc, argvopt, "p:P",
2054 lgopts, &option_index)) != EOF) {
2059 enabled_port_mask = parse_portmask(optarg);
2060 if (enabled_port_mask == 0) {
2061 printf("invalid portmask\n");
2062 print_usage(prgname);
2067 printf("Promiscuous mode selected\n");
2073 if (!strncmp(lgopts[option_index].name, CMD_LINE_OPT_CONFIG,
2074 sizeof (CMD_LINE_OPT_CONFIG))) {
2075 ret = parse_config(optarg);
2077 printf("invalid config\n");
2078 print_usage(prgname);
2083 if (!strncmp(lgopts[option_index].name, CMD_LINE_OPT_ETH_DEST,
2084 sizeof(CMD_LINE_OPT_ETH_DEST))) {
2085 parse_eth_dest(optarg);
2088 if (!strncmp(lgopts[option_index].name, CMD_LINE_OPT_NO_NUMA,
2089 sizeof(CMD_LINE_OPT_NO_NUMA))) {
2090 printf("numa is disabled \n");
2094 #if (APP_LOOKUP_METHOD == APP_LOOKUP_EXACT_MATCH)
2095 if (!strncmp(lgopts[option_index].name, CMD_LINE_OPT_IPV6,
2096 sizeof(CMD_LINE_OPT_IPV6))) {
2097 printf("ipv6 is specified \n");
2102 if (!strncmp(lgopts[option_index].name, CMD_LINE_OPT_ENABLE_JUMBO,
2103 sizeof (CMD_LINE_OPT_ENABLE_JUMBO))) {
2104 struct option lenopts = {"max-pkt-len", required_argument, 0, 0};
2106 printf("jumbo frame is enabled - disabling simple TX path\n");
2107 port_conf.rxmode.jumbo_frame = 1;
2109 /* if no max-pkt-len set, use the default value ETHER_MAX_LEN */
2110 if (0 == getopt_long(argc, argvopt, "", &lenopts, &option_index)) {
2111 ret = parse_max_pkt_len(optarg);
2112 if ((ret < 64) || (ret > MAX_JUMBO_PKT_LEN)){
2113 printf("invalid packet length\n");
2114 print_usage(prgname);
2117 port_conf.rxmode.max_rx_pkt_len = ret;
2119 printf("set jumbo frame max packet length to %u\n",
2120 (unsigned int)port_conf.rxmode.max_rx_pkt_len);
2122 #if (APP_LOOKUP_METHOD == APP_LOOKUP_EXACT_MATCH)
2123 if (!strncmp(lgopts[option_index].name, CMD_LINE_OPT_HASH_ENTRY_NUM,
2124 sizeof(CMD_LINE_OPT_HASH_ENTRY_NUM))) {
2125 ret = parse_hash_entry_number(optarg);
2126 if ((ret > 0) && (ret <= L3FWD_HASH_ENTRIES)) {
2127 hash_entry_number = ret;
2129 printf("invalid hash entry number\n");
2130 print_usage(prgname);
2138 print_usage(prgname);
2144 argv[optind-1] = prgname;
2147 optind = 0; /* reset getopt lib */
2152 print_ethaddr(const char *name, const struct ether_addr *eth_addr)
2154 char buf[ETHER_ADDR_FMT_SIZE];
2155 ether_format_addr(buf, ETHER_ADDR_FMT_SIZE, eth_addr);
2156 printf("%s%s", name, buf);
2159 #if (APP_LOOKUP_METHOD == APP_LOOKUP_EXACT_MATCH)
2161 static void convert_ipv4_5tuple(struct ipv4_5tuple* key1,
2162 union ipv4_5tuple_host* key2)
2164 key2->ip_dst = rte_cpu_to_be_32(key1->ip_dst);
2165 key2->ip_src = rte_cpu_to_be_32(key1->ip_src);
2166 key2->port_dst = rte_cpu_to_be_16(key1->port_dst);
2167 key2->port_src = rte_cpu_to_be_16(key1->port_src);
2168 key2->proto = key1->proto;
2174 static void convert_ipv6_5tuple(struct ipv6_5tuple* key1,
2175 union ipv6_5tuple_host* key2)
2178 for (i = 0; i < 16; i++)
2180 key2->ip_dst[i] = key1->ip_dst[i];
2181 key2->ip_src[i] = key1->ip_src[i];
2183 key2->port_dst = rte_cpu_to_be_16(key1->port_dst);
2184 key2->port_src = rte_cpu_to_be_16(key1->port_src);
2185 key2->proto = key1->proto;
2192 #define BYTE_VALUE_MAX 256
2193 #define ALL_32_BITS 0xffffffff
2194 #define BIT_8_TO_15 0x0000ff00
2196 populate_ipv4_few_flow_into_table(const struct rte_hash* h)
2200 uint32_t array_len = sizeof(ipv4_l3fwd_route_array)/sizeof(ipv4_l3fwd_route_array[0]);
2202 mask0 = _mm_set_epi32(ALL_32_BITS, ALL_32_BITS, ALL_32_BITS, BIT_8_TO_15);
2203 for (i = 0; i < array_len; i++) {
2204 struct ipv4_l3fwd_route entry;
2205 union ipv4_5tuple_host newkey;
2206 entry = ipv4_l3fwd_route_array[i];
2207 convert_ipv4_5tuple(&entry.key, &newkey);
2208 ret = rte_hash_add_key (h,(void *) &newkey);
2210 rte_exit(EXIT_FAILURE, "Unable to add entry %" PRIu32
2211 " to the l3fwd hash.\n", i);
2213 ipv4_l3fwd_out_if[ret] = entry.if_out;
2215 printf("Hash: Adding 0x%" PRIx32 " keys\n", array_len);
2218 #define BIT_16_TO_23 0x00ff0000
2220 populate_ipv6_few_flow_into_table(const struct rte_hash* h)
2224 uint32_t array_len = sizeof(ipv6_l3fwd_route_array)/sizeof(ipv6_l3fwd_route_array[0]);
2226 mask1 = _mm_set_epi32(ALL_32_BITS, ALL_32_BITS, ALL_32_BITS, BIT_16_TO_23);
2227 mask2 = _mm_set_epi32(0, 0, ALL_32_BITS, ALL_32_BITS);
2228 for (i = 0; i < array_len; i++) {
2229 struct ipv6_l3fwd_route entry;
2230 union ipv6_5tuple_host newkey;
2231 entry = ipv6_l3fwd_route_array[i];
2232 convert_ipv6_5tuple(&entry.key, &newkey);
2233 ret = rte_hash_add_key (h, (void *) &newkey);
2235 rte_exit(EXIT_FAILURE, "Unable to add entry %" PRIu32
2236 " to the l3fwd hash.\n", i);
2238 ipv6_l3fwd_out_if[ret] = entry.if_out;
2240 printf("Hash: Adding 0x%" PRIx32 "keys\n", array_len);
2243 #define NUMBER_PORT_USED 4
2245 populate_ipv4_many_flow_into_table(const struct rte_hash* h,
2246 unsigned int nr_flow)
2249 mask0 = _mm_set_epi32(ALL_32_BITS, ALL_32_BITS, ALL_32_BITS, BIT_8_TO_15);
2250 for (i = 0; i < nr_flow; i++) {
2251 struct ipv4_l3fwd_route entry;
2252 union ipv4_5tuple_host newkey;
2253 uint8_t a = (uint8_t) ((i/NUMBER_PORT_USED)%BYTE_VALUE_MAX);
2254 uint8_t b = (uint8_t) (((i/NUMBER_PORT_USED)/BYTE_VALUE_MAX)%BYTE_VALUE_MAX);
2255 uint8_t c = (uint8_t) ((i/NUMBER_PORT_USED)/(BYTE_VALUE_MAX*BYTE_VALUE_MAX));
2256 /* Create the ipv4 exact match flow */
2257 memset(&entry, 0, sizeof(entry));
2258 switch (i & (NUMBER_PORT_USED -1)) {
2260 entry = ipv4_l3fwd_route_array[0];
2261 entry.key.ip_dst = IPv4(101,c,b,a);
2264 entry = ipv4_l3fwd_route_array[1];
2265 entry.key.ip_dst = IPv4(201,c,b,a);
2268 entry = ipv4_l3fwd_route_array[2];
2269 entry.key.ip_dst = IPv4(111,c,b,a);
2272 entry = ipv4_l3fwd_route_array[3];
2273 entry.key.ip_dst = IPv4(211,c,b,a);
2276 convert_ipv4_5tuple(&entry.key, &newkey);
2277 int32_t ret = rte_hash_add_key(h,(void *) &newkey);
2279 rte_exit(EXIT_FAILURE, "Unable to add entry %u\n", i);
2281 ipv4_l3fwd_out_if[ret] = (uint8_t) entry.if_out;
2284 printf("Hash: Adding 0x%x keys\n", nr_flow);
2288 populate_ipv6_many_flow_into_table(const struct rte_hash* h,
2289 unsigned int nr_flow)
2292 mask1 = _mm_set_epi32(ALL_32_BITS, ALL_32_BITS, ALL_32_BITS, BIT_16_TO_23);
2293 mask2 = _mm_set_epi32(0, 0, ALL_32_BITS, ALL_32_BITS);
2294 for (i = 0; i < nr_flow; i++) {
2295 struct ipv6_l3fwd_route entry;
2296 union ipv6_5tuple_host newkey;
2297 uint8_t a = (uint8_t) ((i/NUMBER_PORT_USED)%BYTE_VALUE_MAX);
2298 uint8_t b = (uint8_t) (((i/NUMBER_PORT_USED)/BYTE_VALUE_MAX)%BYTE_VALUE_MAX);
2299 uint8_t c = (uint8_t) ((i/NUMBER_PORT_USED)/(BYTE_VALUE_MAX*BYTE_VALUE_MAX));
2300 /* Create the ipv6 exact match flow */
2301 memset(&entry, 0, sizeof(entry));
2302 switch (i & (NUMBER_PORT_USED - 1)) {
2303 case 0: entry = ipv6_l3fwd_route_array[0]; break;
2304 case 1: entry = ipv6_l3fwd_route_array[1]; break;
2305 case 2: entry = ipv6_l3fwd_route_array[2]; break;
2306 case 3: entry = ipv6_l3fwd_route_array[3]; break;
2308 entry.key.ip_dst[13] = c;
2309 entry.key.ip_dst[14] = b;
2310 entry.key.ip_dst[15] = a;
2311 convert_ipv6_5tuple(&entry.key, &newkey);
2312 int32_t ret = rte_hash_add_key(h,(void *) &newkey);
2314 rte_exit(EXIT_FAILURE, "Unable to add entry %u\n", i);
2316 ipv6_l3fwd_out_if[ret] = (uint8_t) entry.if_out;
2319 printf("Hash: Adding 0x%x keys\n", nr_flow);
2323 setup_hash(int socketid)
2325 struct rte_hash_parameters ipv4_l3fwd_hash_params = {
2327 .entries = L3FWD_HASH_ENTRIES,
2328 .key_len = sizeof(union ipv4_5tuple_host),
2329 .hash_func = ipv4_hash_crc,
2330 .hash_func_init_val = 0,
2333 struct rte_hash_parameters ipv6_l3fwd_hash_params = {
2335 .entries = L3FWD_HASH_ENTRIES,
2336 .key_len = sizeof(union ipv6_5tuple_host),
2337 .hash_func = ipv6_hash_crc,
2338 .hash_func_init_val = 0,
2343 /* create ipv4 hash */
2344 snprintf(s, sizeof(s), "ipv4_l3fwd_hash_%d", socketid);
2345 ipv4_l3fwd_hash_params.name = s;
2346 ipv4_l3fwd_hash_params.socket_id = socketid;
2347 ipv4_l3fwd_lookup_struct[socketid] = rte_hash_create(&ipv4_l3fwd_hash_params);
2348 if (ipv4_l3fwd_lookup_struct[socketid] == NULL)
2349 rte_exit(EXIT_FAILURE, "Unable to create the l3fwd hash on "
2350 "socket %d\n", socketid);
2352 /* create ipv6 hash */
2353 snprintf(s, sizeof(s), "ipv6_l3fwd_hash_%d", socketid);
2354 ipv6_l3fwd_hash_params.name = s;
2355 ipv6_l3fwd_hash_params.socket_id = socketid;
2356 ipv6_l3fwd_lookup_struct[socketid] = rte_hash_create(&ipv6_l3fwd_hash_params);
2357 if (ipv6_l3fwd_lookup_struct[socketid] == NULL)
2358 rte_exit(EXIT_FAILURE, "Unable to create the l3fwd hash on "
2359 "socket %d\n", socketid);
2361 if (hash_entry_number != HASH_ENTRY_NUMBER_DEFAULT) {
2362 /* For testing hash matching with a large number of flows we
2363 * generate millions of IP 5-tuples with an incremented dst
2364 * address to initialize the hash table. */
2366 /* populate the ipv4 hash */
2367 populate_ipv4_many_flow_into_table(
2368 ipv4_l3fwd_lookup_struct[socketid], hash_entry_number);
2370 /* populate the ipv6 hash */
2371 populate_ipv6_many_flow_into_table(
2372 ipv6_l3fwd_lookup_struct[socketid], hash_entry_number);
2375 /* Use data in ipv4/ipv6 l3fwd lookup table directly to initialize the hash table */
2377 /* populate the ipv4 hash */
2378 populate_ipv4_few_flow_into_table(ipv4_l3fwd_lookup_struct[socketid]);
2380 /* populate the ipv6 hash */
2381 populate_ipv6_few_flow_into_table(ipv6_l3fwd_lookup_struct[socketid]);
2387 #if (APP_LOOKUP_METHOD == APP_LOOKUP_LPM)
2389 setup_lpm(int socketid)
2391 struct rte_lpm6_config config;
2396 /* create the LPM table */
2397 snprintf(s, sizeof(s), "IPV4_L3FWD_LPM_%d", socketid);
2398 ipv4_l3fwd_lookup_struct[socketid] = rte_lpm_create(s, socketid,
2399 IPV4_L3FWD_LPM_MAX_RULES, 0);
2400 if (ipv4_l3fwd_lookup_struct[socketid] == NULL)
2401 rte_exit(EXIT_FAILURE, "Unable to create the l3fwd LPM table"
2402 " on socket %d\n", socketid);
2404 /* populate the LPM table */
2405 for (i = 0; i < IPV4_L3FWD_NUM_ROUTES; i++) {
2407 /* skip unused ports */
2408 if ((1 << ipv4_l3fwd_route_array[i].if_out &
2409 enabled_port_mask) == 0)
2412 ret = rte_lpm_add(ipv4_l3fwd_lookup_struct[socketid],
2413 ipv4_l3fwd_route_array[i].ip,
2414 ipv4_l3fwd_route_array[i].depth,
2415 ipv4_l3fwd_route_array[i].if_out);
2418 rte_exit(EXIT_FAILURE, "Unable to add entry %u to the "
2419 "l3fwd LPM table on socket %d\n",
2423 printf("LPM: Adding route 0x%08x / %d (%d)\n",
2424 (unsigned)ipv4_l3fwd_route_array[i].ip,
2425 ipv4_l3fwd_route_array[i].depth,
2426 ipv4_l3fwd_route_array[i].if_out);
2429 /* create the LPM6 table */
2430 snprintf(s, sizeof(s), "IPV6_L3FWD_LPM_%d", socketid);
2432 config.max_rules = IPV6_L3FWD_LPM_MAX_RULES;
2433 config.number_tbl8s = IPV6_L3FWD_LPM_NUMBER_TBL8S;
2435 ipv6_l3fwd_lookup_struct[socketid] = rte_lpm6_create(s, socketid,
2437 if (ipv6_l3fwd_lookup_struct[socketid] == NULL)
2438 rte_exit(EXIT_FAILURE, "Unable to create the l3fwd LPM table"
2439 " on socket %d\n", socketid);
2441 /* populate the LPM table */
2442 for (i = 0; i < IPV6_L3FWD_NUM_ROUTES; i++) {
2444 /* skip unused ports */
2445 if ((1 << ipv6_l3fwd_route_array[i].if_out &
2446 enabled_port_mask) == 0)
2449 ret = rte_lpm6_add(ipv6_l3fwd_lookup_struct[socketid],
2450 ipv6_l3fwd_route_array[i].ip,
2451 ipv6_l3fwd_route_array[i].depth,
2452 ipv6_l3fwd_route_array[i].if_out);
2455 rte_exit(EXIT_FAILURE, "Unable to add entry %u to the "
2456 "l3fwd LPM table on socket %d\n",
2460 printf("LPM: Adding route %s / %d (%d)\n",
2462 ipv6_l3fwd_route_array[i].depth,
2463 ipv6_l3fwd_route_array[i].if_out);
2469 init_mem(unsigned nb_mbuf)
2471 struct lcore_conf *qconf;
2476 for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
2477 if (rte_lcore_is_enabled(lcore_id) == 0)
2481 socketid = rte_lcore_to_socket_id(lcore_id);
2485 if (socketid >= NB_SOCKETS) {
2486 rte_exit(EXIT_FAILURE, "Socket %d of lcore %u is out of range %d\n",
2487 socketid, lcore_id, NB_SOCKETS);
2489 if (pktmbuf_pool[socketid] == NULL) {
2490 snprintf(s, sizeof(s), "mbuf_pool_%d", socketid);
2491 pktmbuf_pool[socketid] =
2492 rte_pktmbuf_pool_create(s, nb_mbuf,
2493 MEMPOOL_CACHE_SIZE, 0,
2494 RTE_MBUF_DEFAULT_BUF_SIZE, socketid);
2495 if (pktmbuf_pool[socketid] == NULL)
2496 rte_exit(EXIT_FAILURE,
2497 "Cannot init mbuf pool on socket %d\n", socketid);
2499 printf("Allocated mbuf pool on socket %d\n", socketid);
2501 #if (APP_LOOKUP_METHOD == APP_LOOKUP_LPM)
2502 setup_lpm(socketid);
2504 setup_hash(socketid);
2507 qconf = &lcore_conf[lcore_id];
2508 qconf->ipv4_lookup_struct = ipv4_l3fwd_lookup_struct[socketid];
2509 qconf->ipv6_lookup_struct = ipv6_l3fwd_lookup_struct[socketid];
2514 /* Check the link status of all ports in up to 9s, and print them finally */
2516 check_all_ports_link_status(uint8_t port_num, uint32_t port_mask)
2518 #define CHECK_INTERVAL 100 /* 100ms */
2519 #define MAX_CHECK_TIME 90 /* 9s (90 * 100ms) in total */
2520 uint8_t portid, count, all_ports_up, print_flag = 0;
2521 struct rte_eth_link link;
2523 printf("\nChecking link status");
2525 for (count = 0; count <= MAX_CHECK_TIME; count++) {
2529 for (portid = 0; portid < port_num; portid++) {
2532 if ((port_mask & (1 << portid)) == 0)
2534 memset(&link, 0, sizeof(link));
2535 rte_eth_link_get_nowait(portid, &link);
2536 /* print link status if flag set */
2537 if (print_flag == 1) {
2538 if (link.link_status)
2539 printf("Port %d Link Up - speed %u "
2540 "Mbps - %s\n", (uint8_t)portid,
2541 (unsigned)link.link_speed,
2542 (link.link_duplex == ETH_LINK_FULL_DUPLEX) ?
2543 ("full-duplex") : ("half-duplex\n"));
2545 printf("Port %d Link Down\n",
2549 /* clear all_ports_up flag if any link down */
2550 if (link.link_status == 0) {
2555 /* after finally printing all link status, get out */
2556 if (print_flag == 1)
2559 if (all_ports_up == 0) {
2562 rte_delay_ms(CHECK_INTERVAL);
2565 /* set the print_flag if all ports up or timeout */
2566 if (all_ports_up == 1 || count == (MAX_CHECK_TIME - 1)) {
2574 signal_handler(int signum)
2576 if (signum == SIGINT || signum == SIGTERM) {
2577 printf("\n\nSignal %d received, preparing to exit...\n",
2584 main(int argc, char **argv)
2586 struct lcore_conf *qconf;
2587 struct rte_eth_dev_info dev_info;
2588 struct rte_eth_txconf *txconf;
2593 uint32_t n_tx_queue, nb_lcores;
2594 uint8_t portid, nb_rx_queue, queue, socketid;
2597 ret = rte_eal_init(argc, argv);
2599 rte_exit(EXIT_FAILURE, "Invalid EAL parameters\n");
2604 signal(SIGINT, signal_handler);
2605 signal(SIGTERM, signal_handler);
2607 /* pre-init dst MACs for all ports to 02:00:00:00:00:xx */
2608 for (portid = 0; portid < RTE_MAX_ETHPORTS; portid++) {
2609 dest_eth_addr[portid] = ETHER_LOCAL_ADMIN_ADDR + ((uint64_t)portid << 40);
2610 *(uint64_t *)(val_eth + portid) = dest_eth_addr[portid];
2613 /* parse application arguments (after the EAL ones) */
2614 ret = parse_args(argc, argv);
2616 rte_exit(EXIT_FAILURE, "Invalid L3FWD parameters\n");
2618 if (check_lcore_params() < 0)
2619 rte_exit(EXIT_FAILURE, "check_lcore_params failed\n");
2621 ret = init_lcore_rx_queues();
2623 rte_exit(EXIT_FAILURE, "init_lcore_rx_queues failed\n");
2625 nb_ports = rte_eth_dev_count();
2626 if (nb_ports > RTE_MAX_ETHPORTS)
2627 nb_ports = RTE_MAX_ETHPORTS;
2629 if (check_port_config(nb_ports) < 0)
2630 rte_exit(EXIT_FAILURE, "check_port_config failed\n");
2632 nb_lcores = rte_lcore_count();
2634 /* initialize all ports */
2635 for (portid = 0; portid < nb_ports; portid++) {
2636 /* skip ports that are not enabled */
2637 if ((enabled_port_mask & (1 << portid)) == 0) {
2638 printf("\nSkipping disabled port %d\n", portid);
2643 printf("Initializing port %d ... ", portid );
2646 nb_rx_queue = get_port_n_rx_queues(portid);
2647 n_tx_queue = nb_lcores;
2648 if (n_tx_queue > MAX_TX_QUEUE_PER_PORT)
2649 n_tx_queue = MAX_TX_QUEUE_PER_PORT;
2650 printf("Creating queues: nb_rxq=%d nb_txq=%u... ",
2651 nb_rx_queue, (unsigned)n_tx_queue );
2652 ret = rte_eth_dev_configure(portid, nb_rx_queue,
2653 (uint16_t)n_tx_queue, &port_conf);
2655 rte_exit(EXIT_FAILURE, "Cannot configure device: err=%d, port=%d\n",
2658 rte_eth_macaddr_get(portid, &ports_eth_addr[portid]);
2659 print_ethaddr(" Address:", &ports_eth_addr[portid]);
2661 print_ethaddr("Destination:",
2662 (const struct ether_addr *)&dest_eth_addr[portid]);
2666 * prepare src MACs for each port.
2668 ether_addr_copy(&ports_eth_addr[portid],
2669 (struct ether_addr *)(val_eth + portid) + 1);
2672 ret = init_mem(NB_MBUF);
2674 rte_exit(EXIT_FAILURE, "init_mem failed\n");
2676 /* init one TX queue per couple (lcore,port) */
2678 for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
2679 if (rte_lcore_is_enabled(lcore_id) == 0)
2683 socketid = (uint8_t)rte_lcore_to_socket_id(lcore_id);
2687 printf("txq=%u,%d,%d ", lcore_id, queueid, socketid);
2690 rte_eth_dev_info_get(portid, &dev_info);
2691 txconf = &dev_info.default_txconf;
2692 if (port_conf.rxmode.jumbo_frame)
2693 txconf->txq_flags = 0;
2694 ret = rte_eth_tx_queue_setup(portid, queueid, nb_txd,
2697 rte_exit(EXIT_FAILURE, "rte_eth_tx_queue_setup: err=%d, "
2698 "port=%d\n", ret, portid);
2700 qconf = &lcore_conf[lcore_id];
2701 qconf->tx_queue_id[portid] = queueid;
2707 for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
2708 if (rte_lcore_is_enabled(lcore_id) == 0)
2710 qconf = &lcore_conf[lcore_id];
2711 printf("\nInitializing rx queues on lcore %u ... ", lcore_id );
2713 /* init RX queues */
2714 for(queue = 0; queue < qconf->n_rx_queue; ++queue) {
2715 portid = qconf->rx_queue_list[queue].port_id;
2716 queueid = qconf->rx_queue_list[queue].queue_id;
2719 socketid = (uint8_t)rte_lcore_to_socket_id(lcore_id);
2723 printf("rxq=%d,%d,%d ", portid, queueid, socketid);
2726 ret = rte_eth_rx_queue_setup(portid, queueid, nb_rxd,
2729 pktmbuf_pool[socketid]);
2731 rte_exit(EXIT_FAILURE, "rte_eth_rx_queue_setup: err=%d,"
2732 "port=%d\n", ret, portid);
2739 for (portid = 0; portid < nb_ports; portid++) {
2740 if ((enabled_port_mask & (1 << portid)) == 0) {
2744 ret = rte_eth_dev_start(portid);
2746 rte_exit(EXIT_FAILURE, "rte_eth_dev_start: err=%d, port=%d\n",
2750 * If enabled, put device in promiscuous mode.
2751 * This allows IO forwarding mode to forward packets
2752 * to itself through 2 cross-connected ports of the
2756 rte_eth_promiscuous_enable(portid);
2759 check_all_ports_link_status((uint8_t)nb_ports, enabled_port_mask);
2762 /* launch per-lcore init on every lcore */
2763 rte_eal_mp_remote_launch(main_loop, NULL, CALL_MASTER);
2764 RTE_LCORE_FOREACH_SLAVE(lcore_id) {
2765 if (rte_eal_wait_lcore(lcore_id) < 0) {
2772 for (portid = 0; portid < nb_ports; portid++) {
2773 if ((enabled_port_mask & (1 << portid)) == 0)
2775 printf("Closing port %d...", portid);
2776 rte_eth_dev_stop(portid);
2777 rte_eth_dev_close(portid);