4 * Copyright(c) 2010-2014 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>
46 #include <rte_common_vect.h>
47 #include <rte_byteorder.h>
49 #include <rte_memory.h>
50 #include <rte_memcpy.h>
51 #include <rte_memzone.h>
52 #include <rte_tailq.h>
54 #include <rte_per_lcore.h>
55 #include <rte_launch.h>
56 #include <rte_atomic.h>
57 #include <rte_cycles.h>
58 #include <rte_prefetch.h>
59 #include <rte_lcore.h>
60 #include <rte_per_lcore.h>
61 #include <rte_branch_prediction.h>
62 #include <rte_interrupts.h>
64 #include <rte_random.h>
65 #include <rte_debug.h>
66 #include <rte_ether.h>
67 #include <rte_ethdev.h>
69 #include <rte_mempool.h>
74 #include <rte_string_fns.h>
76 #define APP_LOOKUP_EXACT_MATCH 0
77 #define APP_LOOKUP_LPM 1
78 #define DO_RFC_1812_CHECKS
80 #ifndef APP_LOOKUP_METHOD
81 #define APP_LOOKUP_METHOD APP_LOOKUP_LPM
85 * When set to zero, simple forwaring path is eanbled.
86 * When set to one, optimized forwarding path is enabled.
87 * Note that LPM optimisation path uses SSE4.1 instructions.
89 #if ((APP_LOOKUP_METHOD == APP_LOOKUP_LPM) && !defined(__SSE4_1__))
90 #define ENABLE_MULTI_BUFFER_OPTIMIZE 0
92 #define ENABLE_MULTI_BUFFER_OPTIMIZE 1
95 #if (APP_LOOKUP_METHOD == APP_LOOKUP_EXACT_MATCH)
97 #elif (APP_LOOKUP_METHOD == APP_LOOKUP_LPM)
101 #error "APP_LOOKUP_METHOD set to incorrect value"
105 #define IPv6_BYTES_FMT "%02x%02x:%02x%02x:%02x%02x:%02x%02x:"\
106 "%02x%02x:%02x%02x:%02x%02x:%02x%02x"
107 #define IPv6_BYTES(addr) \
108 addr[0], addr[1], addr[2], addr[3], \
109 addr[4], addr[5], addr[6], addr[7], \
110 addr[8], addr[9], addr[10], addr[11],\
111 addr[12], addr[13],addr[14], addr[15]
115 #define RTE_LOGTYPE_L3FWD RTE_LOGTYPE_USER1
117 #define MAX_JUMBO_PKT_LEN 9600
119 #define IPV6_ADDR_LEN 16
121 #define MEMPOOL_CACHE_SIZE 256
123 #define MBUF_SIZE (2048 + sizeof(struct rte_mbuf) + RTE_PKTMBUF_HEADROOM)
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 struct ether_addr ports_eth_addr[RTE_MAX_ETHPORTS];
167 static __m128i val_eth[RTE_MAX_ETHPORTS];
169 /* replace first 12B of the ethernet header. */
170 #define MASK_ETH 0x3f
172 /* mask of enabled ports */
173 static uint32_t enabled_port_mask = 0;
174 static int promiscuous_on = 0; /**< Ports set in promiscuous mode off by default. */
175 static int numa_on = 1; /**< NUMA is enabled by default. */
177 #if (APP_LOOKUP_METHOD == APP_LOOKUP_EXACT_MATCH)
178 static int ipv6 = 0; /**< ipv6 is false by default. */
183 struct rte_mbuf *m_table[MAX_PKT_BURST];
186 struct lcore_rx_queue {
189 } __rte_cache_aligned;
191 #define MAX_RX_QUEUE_PER_LCORE 16
192 #define MAX_TX_QUEUE_PER_PORT RTE_MAX_ETHPORTS
193 #define MAX_RX_QUEUE_PER_PORT 128
195 #define MAX_LCORE_PARAMS 1024
196 struct lcore_params {
200 } __rte_cache_aligned;
202 static struct lcore_params lcore_params_array[MAX_LCORE_PARAMS];
203 static struct lcore_params lcore_params_array_default[] = {
215 static struct lcore_params * lcore_params = lcore_params_array_default;
216 static uint16_t nb_lcore_params = sizeof(lcore_params_array_default) /
217 sizeof(lcore_params_array_default[0]);
219 static struct rte_eth_conf port_conf = {
221 .mq_mode = ETH_MQ_RX_RSS,
222 .max_rx_pkt_len = ETHER_MAX_LEN,
224 .header_split = 0, /**< Header Split disabled */
225 .hw_ip_checksum = 1, /**< IP checksum offload enabled */
226 .hw_vlan_filter = 0, /**< VLAN filtering disabled */
227 .jumbo_frame = 0, /**< Jumbo Frame Support disabled */
228 .hw_strip_crc = 0, /**< CRC stripped by hardware */
233 .rss_hf = ETH_RSS_IP,
237 .mq_mode = ETH_MQ_TX_NONE,
241 static struct rte_mempool * pktmbuf_pool[NB_SOCKETS];
243 #if (APP_LOOKUP_METHOD == APP_LOOKUP_EXACT_MATCH)
245 #ifdef RTE_MACHINE_CPUFLAG_SSE4_2
246 #include <rte_hash_crc.h>
247 #define DEFAULT_HASH_FUNC rte_hash_crc
249 #include <rte_jhash.h>
250 #define DEFAULT_HASH_FUNC rte_jhash
259 } __attribute__((__packed__));
261 union ipv4_5tuple_host {
274 #define XMM_NUM_IN_IPV6_5TUPLE 3
277 uint8_t ip_dst[IPV6_ADDR_LEN];
278 uint8_t ip_src[IPV6_ADDR_LEN];
282 } __attribute__((__packed__));
284 union ipv6_5tuple_host {
289 uint8_t ip_src[IPV6_ADDR_LEN];
290 uint8_t ip_dst[IPV6_ADDR_LEN];
295 __m128i xmm[XMM_NUM_IN_IPV6_5TUPLE];
298 struct ipv4_l3fwd_route {
299 struct ipv4_5tuple key;
303 struct ipv6_l3fwd_route {
304 struct ipv6_5tuple key;
308 static struct ipv4_l3fwd_route ipv4_l3fwd_route_array[] = {
309 {{IPv4(101,0,0,0), IPv4(100,10,0,1), 101, 11, IPPROTO_TCP}, 0},
310 {{IPv4(201,0,0,0), IPv4(200,20,0,1), 102, 12, IPPROTO_TCP}, 1},
311 {{IPv4(111,0,0,0), IPv4(100,30,0,1), 101, 11, IPPROTO_TCP}, 2},
312 {{IPv4(211,0,0,0), IPv4(200,40,0,1), 102, 12, IPPROTO_TCP}, 3},
315 static struct ipv6_l3fwd_route ipv6_l3fwd_route_array[] = {
317 {0xfe, 0x80, 0, 0, 0, 0, 0, 0, 0x02, 0x1e, 0x67, 0xff, 0xfe, 0, 0, 0},
318 {0xfe, 0x80, 0, 0, 0, 0, 0, 0, 0x02, 0x1b, 0x21, 0xff, 0xfe, 0x91, 0x38, 0x05},
319 101, 11, IPPROTO_TCP}, 0},
322 {0xfe, 0x90, 0, 0, 0, 0, 0, 0, 0x02, 0x1e, 0x67, 0xff, 0xfe, 0, 0, 0},
323 {0xfe, 0x90, 0, 0, 0, 0, 0, 0, 0x02, 0x1b, 0x21, 0xff, 0xfe, 0x91, 0x38, 0x05},
324 102, 12, IPPROTO_TCP}, 1},
327 {0xfe, 0xa0, 0, 0, 0, 0, 0, 0, 0x02, 0x1e, 0x67, 0xff, 0xfe, 0, 0, 0},
328 {0xfe, 0xa0, 0, 0, 0, 0, 0, 0, 0x02, 0x1b, 0x21, 0xff, 0xfe, 0x91, 0x38, 0x05},
329 101, 11, IPPROTO_TCP}, 2},
332 {0xfe, 0xb0, 0, 0, 0, 0, 0, 0, 0x02, 0x1e, 0x67, 0xff, 0xfe, 0, 0, 0},
333 {0xfe, 0xb0, 0, 0, 0, 0, 0, 0, 0x02, 0x1b, 0x21, 0xff, 0xfe, 0x91, 0x38, 0x05},
334 102, 12, IPPROTO_TCP}, 3},
337 typedef struct rte_hash lookup_struct_t;
338 static lookup_struct_t *ipv4_l3fwd_lookup_struct[NB_SOCKETS];
339 static lookup_struct_t *ipv6_l3fwd_lookup_struct[NB_SOCKETS];
341 #ifdef RTE_ARCH_X86_64
342 /* default to 4 million hash entries (approx) */
343 #define L3FWD_HASH_ENTRIES 1024*1024*4
345 /* 32-bit has less address-space for hugepage memory, limit to 1M entries */
346 #define L3FWD_HASH_ENTRIES 1024*1024*1
348 #define HASH_ENTRY_NUMBER_DEFAULT 4
350 static uint32_t hash_entry_number = HASH_ENTRY_NUMBER_DEFAULT;
352 static inline uint32_t
353 ipv4_hash_crc(const void *data, __rte_unused uint32_t data_len,
356 const union ipv4_5tuple_host *k;
362 p = (const uint32_t *)&k->port_src;
364 #ifdef RTE_MACHINE_CPUFLAG_SSE4_2
365 init_val = rte_hash_crc_4byte(t, init_val);
366 init_val = rte_hash_crc_4byte(k->ip_src, init_val);
367 init_val = rte_hash_crc_4byte(k->ip_dst, init_val);
368 init_val = rte_hash_crc_4byte(*p, init_val);
369 #else /* RTE_MACHINE_CPUFLAG_SSE4_2 */
370 init_val = rte_jhash_1word(t, init_val);
371 init_val = rte_jhash_1word(k->ip_src, init_val);
372 init_val = rte_jhash_1word(k->ip_dst, init_val);
373 init_val = rte_jhash_1word(*p, init_val);
374 #endif /* RTE_MACHINE_CPUFLAG_SSE4_2 */
378 static inline uint32_t
379 ipv6_hash_crc(const void *data, __rte_unused uint32_t data_len, uint32_t init_val)
381 const union ipv6_5tuple_host *k;
384 #ifdef RTE_MACHINE_CPUFLAG_SSE4_2
385 const uint32_t *ip_src0, *ip_src1, *ip_src2, *ip_src3;
386 const uint32_t *ip_dst0, *ip_dst1, *ip_dst2, *ip_dst3;
387 #endif /* RTE_MACHINE_CPUFLAG_SSE4_2 */
391 p = (const uint32_t *)&k->port_src;
393 #ifdef RTE_MACHINE_CPUFLAG_SSE4_2
394 ip_src0 = (const uint32_t *) k->ip_src;
395 ip_src1 = (const uint32_t *)(k->ip_src+4);
396 ip_src2 = (const uint32_t *)(k->ip_src+8);
397 ip_src3 = (const uint32_t *)(k->ip_src+12);
398 ip_dst0 = (const uint32_t *) k->ip_dst;
399 ip_dst1 = (const uint32_t *)(k->ip_dst+4);
400 ip_dst2 = (const uint32_t *)(k->ip_dst+8);
401 ip_dst3 = (const uint32_t *)(k->ip_dst+12);
402 init_val = rte_hash_crc_4byte(t, init_val);
403 init_val = rte_hash_crc_4byte(*ip_src0, init_val);
404 init_val = rte_hash_crc_4byte(*ip_src1, init_val);
405 init_val = rte_hash_crc_4byte(*ip_src2, init_val);
406 init_val = rte_hash_crc_4byte(*ip_src3, init_val);
407 init_val = rte_hash_crc_4byte(*ip_dst0, init_val);
408 init_val = rte_hash_crc_4byte(*ip_dst1, init_val);
409 init_val = rte_hash_crc_4byte(*ip_dst2, init_val);
410 init_val = rte_hash_crc_4byte(*ip_dst3, init_val);
411 init_val = rte_hash_crc_4byte(*p, init_val);
412 #else /* RTE_MACHINE_CPUFLAG_SSE4_2 */
413 init_val = rte_jhash_1word(t, init_val);
414 init_val = rte_jhash(k->ip_src, sizeof(uint8_t) * IPV6_ADDR_LEN, init_val);
415 init_val = rte_jhash(k->ip_dst, sizeof(uint8_t) * IPV6_ADDR_LEN, init_val);
416 init_val = rte_jhash_1word(*p, init_val);
417 #endif /* RTE_MACHINE_CPUFLAG_SSE4_2 */
421 #define IPV4_L3FWD_NUM_ROUTES \
422 (sizeof(ipv4_l3fwd_route_array) / sizeof(ipv4_l3fwd_route_array[0]))
424 #define IPV6_L3FWD_NUM_ROUTES \
425 (sizeof(ipv6_l3fwd_route_array) / sizeof(ipv6_l3fwd_route_array[0]))
427 static uint8_t ipv4_l3fwd_out_if[L3FWD_HASH_ENTRIES] __rte_cache_aligned;
428 static uint8_t ipv6_l3fwd_out_if[L3FWD_HASH_ENTRIES] __rte_cache_aligned;
432 #if (APP_LOOKUP_METHOD == APP_LOOKUP_LPM)
433 struct ipv4_l3fwd_route {
439 struct ipv6_l3fwd_route {
445 static struct ipv4_l3fwd_route ipv4_l3fwd_route_array[] = {
446 {IPv4(1,1,1,0), 24, 0},
447 {IPv4(2,1,1,0), 24, 1},
448 {IPv4(3,1,1,0), 24, 2},
449 {IPv4(4,1,1,0), 24, 3},
450 {IPv4(5,1,1,0), 24, 4},
451 {IPv4(6,1,1,0), 24, 5},
452 {IPv4(7,1,1,0), 24, 6},
453 {IPv4(8,1,1,0), 24, 7},
456 static struct ipv6_l3fwd_route ipv6_l3fwd_route_array[] = {
457 {{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}, 48, 0},
458 {{2,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}, 48, 1},
459 {{3,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}, 48, 2},
460 {{4,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}, 48, 3},
461 {{5,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}, 48, 4},
462 {{6,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}, 48, 5},
463 {{7,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}, 48, 6},
464 {{8,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}, 48, 7},
467 #define IPV4_L3FWD_NUM_ROUTES \
468 (sizeof(ipv4_l3fwd_route_array) / sizeof(ipv4_l3fwd_route_array[0]))
469 #define IPV6_L3FWD_NUM_ROUTES \
470 (sizeof(ipv6_l3fwd_route_array) / sizeof(ipv6_l3fwd_route_array[0]))
472 #define IPV4_L3FWD_LPM_MAX_RULES 1024
473 #define IPV6_L3FWD_LPM_MAX_RULES 1024
474 #define IPV6_L3FWD_LPM_NUMBER_TBL8S (1 << 16)
476 typedef struct rte_lpm lookup_struct_t;
477 typedef struct rte_lpm6 lookup6_struct_t;
478 static lookup_struct_t *ipv4_l3fwd_lookup_struct[NB_SOCKETS];
479 static lookup6_struct_t *ipv6_l3fwd_lookup_struct[NB_SOCKETS];
484 struct lcore_rx_queue rx_queue_list[MAX_RX_QUEUE_PER_LCORE];
485 uint16_t tx_queue_id[RTE_MAX_ETHPORTS];
486 struct mbuf_table tx_mbufs[RTE_MAX_ETHPORTS];
487 lookup_struct_t * ipv4_lookup_struct;
488 #if (APP_LOOKUP_METHOD == APP_LOOKUP_LPM)
489 lookup6_struct_t * ipv6_lookup_struct;
491 lookup_struct_t * ipv6_lookup_struct;
493 } __rte_cache_aligned;
495 static struct lcore_conf lcore_conf[RTE_MAX_LCORE];
497 /* Send burst of packets on an output interface */
499 send_burst(struct lcore_conf *qconf, uint16_t n, uint8_t port)
501 struct rte_mbuf **m_table;
505 queueid = qconf->tx_queue_id[port];
506 m_table = (struct rte_mbuf **)qconf->tx_mbufs[port].m_table;
508 ret = rte_eth_tx_burst(port, queueid, m_table, n);
509 if (unlikely(ret < n)) {
511 rte_pktmbuf_free(m_table[ret]);
518 /* Enqueue a single packet, and send burst if queue is filled */
520 send_single_packet(struct rte_mbuf *m, uint8_t port)
524 struct lcore_conf *qconf;
526 lcore_id = rte_lcore_id();
528 qconf = &lcore_conf[lcore_id];
529 len = qconf->tx_mbufs[port].len;
530 qconf->tx_mbufs[port].m_table[len] = m;
533 /* enough pkts to be sent */
534 if (unlikely(len == MAX_PKT_BURST)) {
535 send_burst(qconf, MAX_PKT_BURST, port);
539 qconf->tx_mbufs[port].len = len;
543 static inline __attribute__((always_inline)) void
544 send_packetsx4(struct lcore_conf *qconf, uint8_t port,
545 struct rte_mbuf *m[], uint32_t num)
549 len = qconf->tx_mbufs[port].len;
552 * If TX buffer for that queue is empty, and we have enough packets,
553 * then send them straightway.
555 if (num >= MAX_TX_BURST && len == 0) {
556 n = rte_eth_tx_burst(port, qconf->tx_queue_id[port], m, num);
557 if (unlikely(n < num)) {
559 rte_pktmbuf_free(m[n]);
566 * Put packets into TX buffer for that queue.
570 n = (n > MAX_PKT_BURST) ? MAX_PKT_BURST - len : num;
573 switch (n % FWDSTEP) {
576 qconf->tx_mbufs[port].m_table[len + j] = m[j];
579 qconf->tx_mbufs[port].m_table[len + j] = m[j];
582 qconf->tx_mbufs[port].m_table[len + j] = m[j];
585 qconf->tx_mbufs[port].m_table[len + j] = m[j];
592 /* enough pkts to be sent */
593 if (unlikely(len == MAX_PKT_BURST)) {
595 send_burst(qconf, MAX_PKT_BURST, port);
597 /* copy rest of the packets into the TX buffer. */
600 switch (len % FWDSTEP) {
603 qconf->tx_mbufs[port].m_table[j] = m[n + j];
606 qconf->tx_mbufs[port].m_table[j] = m[n + j];
609 qconf->tx_mbufs[port].m_table[j] = m[n + j];
612 qconf->tx_mbufs[port].m_table[j] = m[n + j];
618 qconf->tx_mbufs[port].len = len;
621 #ifdef DO_RFC_1812_CHECKS
623 is_valid_ipv4_pkt(struct ipv4_hdr *pkt, uint32_t link_len)
625 /* From http://www.rfc-editor.org/rfc/rfc1812.txt section 5.2.2 */
627 * 1. The packet length reported by the Link Layer must be large
628 * enough to hold the minimum length legal IP datagram (20 bytes).
630 if (link_len < sizeof(struct ipv4_hdr))
633 /* 2. The IP checksum must be correct. */
634 /* this is checked in H/W */
637 * 3. The IP version number must be 4. If the version number is not 4
638 * then the packet may be another version of IP, such as IPng or
641 if (((pkt->version_ihl) >> 4) != 4)
644 * 4. The IP header length field must be large enough to hold the
645 * minimum length legal IP datagram (20 bytes = 5 words).
647 if ((pkt->version_ihl & 0xf) < 5)
651 * 5. The IP total length field must be large enough to hold the IP
652 * datagram header, whose length is specified in the IP header length
655 if (rte_cpu_to_be_16(pkt->total_length) < sizeof(struct ipv4_hdr))
662 #if (APP_LOOKUP_METHOD == APP_LOOKUP_EXACT_MATCH)
664 static __m128i mask0;
665 static __m128i mask1;
666 static __m128i mask2;
667 static inline uint8_t
668 get_ipv4_dst_port(void *ipv4_hdr, uint8_t portid, lookup_struct_t * ipv4_l3fwd_lookup_struct)
671 union ipv4_5tuple_host key;
673 ipv4_hdr = (uint8_t *)ipv4_hdr + offsetof(struct ipv4_hdr, time_to_live);
674 __m128i data = _mm_loadu_si128((__m128i*)(ipv4_hdr));
675 /* Get 5 tuple: dst port, src port, dst IP address, src IP address and protocol */
676 key.xmm = _mm_and_si128(data, mask0);
677 /* Find destination port */
678 ret = rte_hash_lookup(ipv4_l3fwd_lookup_struct, (const void *)&key);
679 return (uint8_t)((ret < 0)? portid : ipv4_l3fwd_out_if[ret]);
682 static inline uint8_t
683 get_ipv6_dst_port(void *ipv6_hdr, uint8_t portid, lookup_struct_t * ipv6_l3fwd_lookup_struct)
686 union ipv6_5tuple_host key;
688 ipv6_hdr = (uint8_t *)ipv6_hdr + offsetof(struct ipv6_hdr, payload_len);
689 __m128i data0 = _mm_loadu_si128((__m128i*)(ipv6_hdr));
690 __m128i data1 = _mm_loadu_si128((__m128i*)(((uint8_t*)ipv6_hdr)+sizeof(__m128i)));
691 __m128i data2 = _mm_loadu_si128((__m128i*)(((uint8_t*)ipv6_hdr)+sizeof(__m128i)+sizeof(__m128i)));
692 /* Get part of 5 tuple: src IP address lower 96 bits and protocol */
693 key.xmm[0] = _mm_and_si128(data0, mask1);
694 /* Get part of 5 tuple: dst IP address lower 96 bits and src IP address higher 32 bits */
696 /* Get part of 5 tuple: dst port and src port and dst IP address higher 32 bits */
697 key.xmm[2] = _mm_and_si128(data2, mask2);
699 /* Find destination port */
700 ret = rte_hash_lookup(ipv6_l3fwd_lookup_struct, (const void *)&key);
701 return (uint8_t)((ret < 0)? portid : ipv6_l3fwd_out_if[ret]);
705 #if (APP_LOOKUP_METHOD == APP_LOOKUP_LPM)
707 static inline uint8_t
708 get_ipv4_dst_port(void *ipv4_hdr, uint8_t portid, lookup_struct_t * ipv4_l3fwd_lookup_struct)
712 return (uint8_t) ((rte_lpm_lookup(ipv4_l3fwd_lookup_struct,
713 rte_be_to_cpu_32(((struct ipv4_hdr *)ipv4_hdr)->dst_addr),
714 &next_hop) == 0) ? next_hop : portid);
717 static inline uint8_t
718 get_ipv6_dst_port(void *ipv6_hdr, uint8_t portid, lookup6_struct_t * ipv6_l3fwd_lookup_struct)
721 return (uint8_t) ((rte_lpm6_lookup(ipv6_l3fwd_lookup_struct,
722 ((struct ipv6_hdr*)ipv6_hdr)->dst_addr, &next_hop) == 0)?
727 static inline void l3fwd_simple_forward(struct rte_mbuf *m, uint8_t portid,
728 struct lcore_conf *qconf) __attribute__((unused));
730 #if ((APP_LOOKUP_METHOD == APP_LOOKUP_EXACT_MATCH) && \
731 (ENABLE_MULTI_BUFFER_OPTIMIZE == 1))
733 #define MASK_ALL_PKTS 0xf
734 #define EXECLUDE_1ST_PKT 0xe
735 #define EXECLUDE_2ND_PKT 0xd
736 #define EXECLUDE_3RD_PKT 0xb
737 #define EXECLUDE_4TH_PKT 0x7
740 simple_ipv4_fwd_4pkts(struct rte_mbuf* m[4], uint8_t portid, struct lcore_conf *qconf)
742 struct ether_hdr *eth_hdr[4];
743 struct ipv4_hdr *ipv4_hdr[4];
744 void *d_addr_bytes[4];
747 union ipv4_5tuple_host key[4];
750 eth_hdr[0] = rte_pktmbuf_mtod(m[0], struct ether_hdr *);
751 eth_hdr[1] = rte_pktmbuf_mtod(m[1], struct ether_hdr *);
752 eth_hdr[2] = rte_pktmbuf_mtod(m[2], struct ether_hdr *);
753 eth_hdr[3] = rte_pktmbuf_mtod(m[3], struct ether_hdr *);
755 /* Handle IPv4 headers.*/
756 ipv4_hdr[0] = (struct ipv4_hdr *)(rte_pktmbuf_mtod(m[0], unsigned char *) +
757 sizeof(struct ether_hdr));
758 ipv4_hdr[1] = (struct ipv4_hdr *)(rte_pktmbuf_mtod(m[1], unsigned char *) +
759 sizeof(struct ether_hdr));
760 ipv4_hdr[2] = (struct ipv4_hdr *)(rte_pktmbuf_mtod(m[2], unsigned char *) +
761 sizeof(struct ether_hdr));
762 ipv4_hdr[3] = (struct ipv4_hdr *)(rte_pktmbuf_mtod(m[3], unsigned char *) +
763 sizeof(struct ether_hdr));
765 #ifdef DO_RFC_1812_CHECKS
766 /* Check to make sure the packet is valid (RFC1812) */
767 uint8_t valid_mask = MASK_ALL_PKTS;
768 if (is_valid_ipv4_pkt(ipv4_hdr[0], m[0]->pkt_len) < 0) {
769 rte_pktmbuf_free(m[0]);
770 valid_mask &= EXECLUDE_1ST_PKT;
772 if (is_valid_ipv4_pkt(ipv4_hdr[1], m[1]->pkt_len) < 0) {
773 rte_pktmbuf_free(m[1]);
774 valid_mask &= EXECLUDE_2ND_PKT;
776 if (is_valid_ipv4_pkt(ipv4_hdr[2], m[2]->pkt_len) < 0) {
777 rte_pktmbuf_free(m[2]);
778 valid_mask &= EXECLUDE_3RD_PKT;
780 if (is_valid_ipv4_pkt(ipv4_hdr[3], m[3]->pkt_len) < 0) {
781 rte_pktmbuf_free(m[3]);
782 valid_mask &= EXECLUDE_4TH_PKT;
784 if (unlikely(valid_mask != MASK_ALL_PKTS)) {
785 if (valid_mask == 0){
789 for (i = 0; i < 4; i++) {
790 if ((0x1 << i) & valid_mask) {
791 l3fwd_simple_forward(m[i], portid, qconf);
797 #endif // End of #ifdef DO_RFC_1812_CHECKS
799 data[0] = _mm_loadu_si128((__m128i*)(rte_pktmbuf_mtod(m[0], unsigned char *) +
800 sizeof(struct ether_hdr) + offsetof(struct ipv4_hdr, time_to_live)));
801 data[1] = _mm_loadu_si128((__m128i*)(rte_pktmbuf_mtod(m[1], unsigned char *) +
802 sizeof(struct ether_hdr) + offsetof(struct ipv4_hdr, time_to_live)));
803 data[2] = _mm_loadu_si128((__m128i*)(rte_pktmbuf_mtod(m[2], unsigned char *) +
804 sizeof(struct ether_hdr) + offsetof(struct ipv4_hdr, time_to_live)));
805 data[3] = _mm_loadu_si128((__m128i*)(rte_pktmbuf_mtod(m[3], unsigned char *) +
806 sizeof(struct ether_hdr) + offsetof(struct ipv4_hdr, time_to_live)));
808 key[0].xmm = _mm_and_si128(data[0], mask0);
809 key[1].xmm = _mm_and_si128(data[1], mask0);
810 key[2].xmm = _mm_and_si128(data[2], mask0);
811 key[3].xmm = _mm_and_si128(data[3], mask0);
813 const void *key_array[4] = {&key[0], &key[1], &key[2],&key[3]};
814 rte_hash_lookup_multi(qconf->ipv4_lookup_struct, &key_array[0], 4, ret);
815 dst_port[0] = (uint8_t) ((ret[0] < 0) ? portid : ipv4_l3fwd_out_if[ret[0]]);
816 dst_port[1] = (uint8_t) ((ret[1] < 0) ? portid : ipv4_l3fwd_out_if[ret[1]]);
817 dst_port[2] = (uint8_t) ((ret[2] < 0) ? portid : ipv4_l3fwd_out_if[ret[2]]);
818 dst_port[3] = (uint8_t) ((ret[3] < 0) ? portid : ipv4_l3fwd_out_if[ret[3]]);
820 if (dst_port[0] >= RTE_MAX_ETHPORTS || (enabled_port_mask & 1 << dst_port[0]) == 0)
821 dst_port[0] = portid;
822 if (dst_port[1] >= RTE_MAX_ETHPORTS || (enabled_port_mask & 1 << dst_port[1]) == 0)
823 dst_port[1] = portid;
824 if (dst_port[2] >= RTE_MAX_ETHPORTS || (enabled_port_mask & 1 << dst_port[2]) == 0)
825 dst_port[2] = portid;
826 if (dst_port[3] >= RTE_MAX_ETHPORTS || (enabled_port_mask & 1 << dst_port[3]) == 0)
827 dst_port[3] = portid;
829 /* 02:00:00:00:00:xx */
830 d_addr_bytes[0] = ð_hdr[0]->d_addr.addr_bytes[0];
831 d_addr_bytes[1] = ð_hdr[1]->d_addr.addr_bytes[0];
832 d_addr_bytes[2] = ð_hdr[2]->d_addr.addr_bytes[0];
833 d_addr_bytes[3] = ð_hdr[3]->d_addr.addr_bytes[0];
834 *((uint64_t *)d_addr_bytes[0]) = 0x000000000002 + ((uint64_t)dst_port[0] << 40);
835 *((uint64_t *)d_addr_bytes[1]) = 0x000000000002 + ((uint64_t)dst_port[1] << 40);
836 *((uint64_t *)d_addr_bytes[2]) = 0x000000000002 + ((uint64_t)dst_port[2] << 40);
837 *((uint64_t *)d_addr_bytes[3]) = 0x000000000002 + ((uint64_t)dst_port[3] << 40);
839 #ifdef DO_RFC_1812_CHECKS
840 /* Update time to live and header checksum */
841 --(ipv4_hdr[0]->time_to_live);
842 --(ipv4_hdr[1]->time_to_live);
843 --(ipv4_hdr[2]->time_to_live);
844 --(ipv4_hdr[3]->time_to_live);
845 ++(ipv4_hdr[0]->hdr_checksum);
846 ++(ipv4_hdr[1]->hdr_checksum);
847 ++(ipv4_hdr[2]->hdr_checksum);
848 ++(ipv4_hdr[3]->hdr_checksum);
852 ether_addr_copy(&ports_eth_addr[dst_port[0]], ð_hdr[0]->s_addr);
853 ether_addr_copy(&ports_eth_addr[dst_port[1]], ð_hdr[1]->s_addr);
854 ether_addr_copy(&ports_eth_addr[dst_port[2]], ð_hdr[2]->s_addr);
855 ether_addr_copy(&ports_eth_addr[dst_port[3]], ð_hdr[3]->s_addr);
857 send_single_packet(m[0], (uint8_t)dst_port[0]);
858 send_single_packet(m[1], (uint8_t)dst_port[1]);
859 send_single_packet(m[2], (uint8_t)dst_port[2]);
860 send_single_packet(m[3], (uint8_t)dst_port[3]);
864 static inline void get_ipv6_5tuple(struct rte_mbuf* m0, __m128i mask0, __m128i mask1,
865 union ipv6_5tuple_host * key)
867 __m128i tmpdata0 = _mm_loadu_si128((__m128i*)(rte_pktmbuf_mtod(m0, unsigned char *)
868 + sizeof(struct ether_hdr) + offsetof(struct ipv6_hdr, payload_len)));
869 __m128i tmpdata1 = _mm_loadu_si128((__m128i*)(rte_pktmbuf_mtod(m0, unsigned char *)
870 + sizeof(struct ether_hdr) + offsetof(struct ipv6_hdr, payload_len)
872 __m128i tmpdata2 = _mm_loadu_si128((__m128i*)(rte_pktmbuf_mtod(m0, unsigned char *)
873 + sizeof(struct ether_hdr) + offsetof(struct ipv6_hdr, payload_len)
874 + sizeof(__m128i) + sizeof(__m128i)));
875 key->xmm[0] = _mm_and_si128(tmpdata0, mask0);
876 key->xmm[1] = tmpdata1;
877 key->xmm[2] = _mm_and_si128(tmpdata2, mask1);
882 simple_ipv6_fwd_4pkts(struct rte_mbuf* m[4], uint8_t portid, struct lcore_conf *qconf)
884 struct ether_hdr *eth_hdr[4];
885 __attribute__((unused)) struct ipv6_hdr *ipv6_hdr[4];
886 void *d_addr_bytes[4];
889 union ipv6_5tuple_host key[4];
891 eth_hdr[0] = rte_pktmbuf_mtod(m[0], struct ether_hdr *);
892 eth_hdr[1] = rte_pktmbuf_mtod(m[1], struct ether_hdr *);
893 eth_hdr[2] = rte_pktmbuf_mtod(m[2], struct ether_hdr *);
894 eth_hdr[3] = rte_pktmbuf_mtod(m[3], struct ether_hdr *);
896 /* Handle IPv6 headers.*/
897 ipv6_hdr[0] = (struct ipv6_hdr *)(rte_pktmbuf_mtod(m[0], unsigned char *) +
898 sizeof(struct ether_hdr));
899 ipv6_hdr[1] = (struct ipv6_hdr *)(rte_pktmbuf_mtod(m[1], unsigned char *) +
900 sizeof(struct ether_hdr));
901 ipv6_hdr[2] = (struct ipv6_hdr *)(rte_pktmbuf_mtod(m[2], unsigned char *) +
902 sizeof(struct ether_hdr));
903 ipv6_hdr[3] = (struct ipv6_hdr *)(rte_pktmbuf_mtod(m[3], unsigned char *) +
904 sizeof(struct ether_hdr));
906 get_ipv6_5tuple(m[0], mask1, mask2, &key[0]);
907 get_ipv6_5tuple(m[1], mask1, mask2, &key[1]);
908 get_ipv6_5tuple(m[2], mask1, mask2, &key[2]);
909 get_ipv6_5tuple(m[3], mask1, mask2, &key[3]);
911 const void *key_array[4] = {&key[0], &key[1], &key[2],&key[3]};
912 rte_hash_lookup_multi(qconf->ipv6_lookup_struct, &key_array[0], 4, ret);
913 dst_port[0] = (uint8_t) ((ret[0] < 0)? portid:ipv6_l3fwd_out_if[ret[0]]);
914 dst_port[1] = (uint8_t) ((ret[1] < 0)? portid:ipv6_l3fwd_out_if[ret[1]]);
915 dst_port[2] = (uint8_t) ((ret[2] < 0)? portid:ipv6_l3fwd_out_if[ret[2]]);
916 dst_port[3] = (uint8_t) ((ret[3] < 0)? portid:ipv6_l3fwd_out_if[ret[3]]);
918 if (dst_port[0] >= RTE_MAX_ETHPORTS || (enabled_port_mask & 1 << dst_port[0]) == 0)
919 dst_port[0] = portid;
920 if (dst_port[1] >= RTE_MAX_ETHPORTS || (enabled_port_mask & 1 << dst_port[1]) == 0)
921 dst_port[1] = portid;
922 if (dst_port[2] >= RTE_MAX_ETHPORTS || (enabled_port_mask & 1 << dst_port[2]) == 0)
923 dst_port[2] = portid;
924 if (dst_port[3] >= RTE_MAX_ETHPORTS || (enabled_port_mask & 1 << dst_port[3]) == 0)
925 dst_port[3] = portid;
927 /* 02:00:00:00:00:xx */
928 d_addr_bytes[0] = ð_hdr[0]->d_addr.addr_bytes[0];
929 d_addr_bytes[1] = ð_hdr[1]->d_addr.addr_bytes[0];
930 d_addr_bytes[2] = ð_hdr[2]->d_addr.addr_bytes[0];
931 d_addr_bytes[3] = ð_hdr[3]->d_addr.addr_bytes[0];
932 *((uint64_t *)d_addr_bytes[0]) = 0x000000000002 + ((uint64_t)dst_port[0] << 40);
933 *((uint64_t *)d_addr_bytes[1]) = 0x000000000002 + ((uint64_t)dst_port[1] << 40);
934 *((uint64_t *)d_addr_bytes[2]) = 0x000000000002 + ((uint64_t)dst_port[2] << 40);
935 *((uint64_t *)d_addr_bytes[3]) = 0x000000000002 + ((uint64_t)dst_port[3] << 40);
938 ether_addr_copy(&ports_eth_addr[dst_port[0]], ð_hdr[0]->s_addr);
939 ether_addr_copy(&ports_eth_addr[dst_port[1]], ð_hdr[1]->s_addr);
940 ether_addr_copy(&ports_eth_addr[dst_port[2]], ð_hdr[2]->s_addr);
941 ether_addr_copy(&ports_eth_addr[dst_port[3]], ð_hdr[3]->s_addr);
943 send_single_packet(m[0], (uint8_t)dst_port[0]);
944 send_single_packet(m[1], (uint8_t)dst_port[1]);
945 send_single_packet(m[2], (uint8_t)dst_port[2]);
946 send_single_packet(m[3], (uint8_t)dst_port[3]);
949 #endif /* APP_LOOKUP_METHOD */
951 static inline __attribute__((always_inline)) void
952 l3fwd_simple_forward(struct rte_mbuf *m, uint8_t portid, struct lcore_conf *qconf)
954 struct ether_hdr *eth_hdr;
955 struct ipv4_hdr *ipv4_hdr;
959 eth_hdr = rte_pktmbuf_mtod(m, struct ether_hdr *);
961 if (m->ol_flags & PKT_RX_IPV4_HDR) {
962 /* Handle IPv4 headers.*/
963 ipv4_hdr = (struct ipv4_hdr *)(rte_pktmbuf_mtod(m, unsigned char *) +
964 sizeof(struct ether_hdr));
966 #ifdef DO_RFC_1812_CHECKS
967 /* Check to make sure the packet is valid (RFC1812) */
968 if (is_valid_ipv4_pkt(ipv4_hdr, m->pkt_len) < 0) {
974 dst_port = get_ipv4_dst_port(ipv4_hdr, portid,
975 qconf->ipv4_lookup_struct);
976 if (dst_port >= RTE_MAX_ETHPORTS ||
977 (enabled_port_mask & 1 << dst_port) == 0)
980 /* 02:00:00:00:00:xx */
981 d_addr_bytes = ð_hdr->d_addr.addr_bytes[0];
982 *((uint64_t *)d_addr_bytes) = ETHER_LOCAL_ADMIN_ADDR +
983 ((uint64_t)dst_port << 40);
985 #ifdef DO_RFC_1812_CHECKS
986 /* Update time to live and header checksum */
987 --(ipv4_hdr->time_to_live);
988 ++(ipv4_hdr->hdr_checksum);
992 ether_addr_copy(&ports_eth_addr[dst_port], ð_hdr->s_addr);
994 send_single_packet(m, dst_port);
997 /* Handle IPv6 headers.*/
998 struct ipv6_hdr *ipv6_hdr;
1000 ipv6_hdr = (struct ipv6_hdr *)(rte_pktmbuf_mtod(m, unsigned char *) +
1001 sizeof(struct ether_hdr));
1003 dst_port = get_ipv6_dst_port(ipv6_hdr, portid, qconf->ipv6_lookup_struct);
1005 if (dst_port >= RTE_MAX_ETHPORTS || (enabled_port_mask & 1 << dst_port) == 0)
1008 /* 02:00:00:00:00:xx */
1009 d_addr_bytes = ð_hdr->d_addr.addr_bytes[0];
1010 *((uint64_t *)d_addr_bytes) = ETHER_LOCAL_ADMIN_ADDR +
1011 ((uint64_t)dst_port << 40);
1014 ether_addr_copy(&ports_eth_addr[dst_port], ð_hdr->s_addr);
1016 send_single_packet(m, dst_port);
1021 #ifdef DO_RFC_1812_CHECKS
1023 #define IPV4_MIN_VER_IHL 0x45
1024 #define IPV4_MAX_VER_IHL 0x4f
1025 #define IPV4_MAX_VER_IHL_DIFF (IPV4_MAX_VER_IHL - IPV4_MIN_VER_IHL)
1027 /* Minimum value of IPV4 total length (20B) in network byte order. */
1028 #define IPV4_MIN_LEN_BE (sizeof(struct ipv4_hdr) << 8)
1031 * From http://www.rfc-editor.org/rfc/rfc1812.txt section 5.2.2:
1032 * - The IP version number must be 4.
1033 * - The IP header length field must be large enough to hold the
1034 * minimum length legal IP datagram (20 bytes = 5 words).
1035 * - The IP total length field must be large enough to hold the IP
1036 * datagram header, whose length is specified in the IP header length
1038 * If we encounter invalid IPV4 packet, then set destination port for it
1039 * to BAD_PORT value.
1041 static inline __attribute__((always_inline)) void
1042 rfc1812_process(struct ipv4_hdr *ipv4_hdr, uint16_t *dp, uint32_t flags)
1046 if ((flags & PKT_RX_IPV4_HDR) != 0) {
1048 ihl = ipv4_hdr->version_ihl - IPV4_MIN_VER_IHL;
1050 ipv4_hdr->time_to_live--;
1051 ipv4_hdr->hdr_checksum++;
1053 if (ihl > IPV4_MAX_VER_IHL_DIFF ||
1054 ((uint8_t)ipv4_hdr->total_length == 0 &&
1055 ipv4_hdr->total_length < IPV4_MIN_LEN_BE)) {
1062 #define rfc1812_process(mb, dp) do { } while (0)
1063 #endif /* DO_RFC_1812_CHECKS */
1066 #if ((APP_LOOKUP_METHOD == APP_LOOKUP_LPM) && \
1067 (ENABLE_MULTI_BUFFER_OPTIMIZE == 1))
1069 static inline __attribute__((always_inline)) uint16_t
1070 get_dst_port(const struct lcore_conf *qconf, struct rte_mbuf *pkt,
1071 uint32_t dst_ipv4, uint8_t portid)
1074 struct ipv6_hdr *ipv6_hdr;
1075 struct ether_hdr *eth_hdr;
1077 if (pkt->ol_flags & PKT_RX_IPV4_HDR) {
1078 if (rte_lpm_lookup(qconf->ipv4_lookup_struct, dst_ipv4,
1081 } else if (pkt->ol_flags & PKT_RX_IPV6_HDR) {
1082 eth_hdr = rte_pktmbuf_mtod(pkt, struct ether_hdr *);
1083 ipv6_hdr = (struct ipv6_hdr *)(eth_hdr + 1);
1084 if (rte_lpm6_lookup(qconf->ipv6_lookup_struct,
1085 ipv6_hdr->dst_addr, &next_hop) != 0)
1095 process_packet(struct lcore_conf *qconf, struct rte_mbuf *pkt,
1096 uint16_t *dst_port, uint8_t portid)
1098 struct ether_hdr *eth_hdr;
1099 struct ipv4_hdr *ipv4_hdr;
1104 eth_hdr = rte_pktmbuf_mtod(pkt, struct ether_hdr *);
1105 ipv4_hdr = (struct ipv4_hdr *)(eth_hdr + 1);
1107 dst_ipv4 = ipv4_hdr->dst_addr;
1108 dst_ipv4 = rte_be_to_cpu_32(dst_ipv4);
1109 dp = get_dst_port(qconf, pkt, dst_ipv4, portid);
1111 te = _mm_load_si128((__m128i *)eth_hdr);
1115 rfc1812_process(ipv4_hdr, dst_port, pkt->ol_flags);
1117 te = _mm_blend_epi16(te, ve, MASK_ETH);
1118 _mm_store_si128((__m128i *)eth_hdr, te);
1122 * Read ol_flags and destination IPV4 addresses from 4 mbufs.
1125 processx4_step1(struct rte_mbuf *pkt[FWDSTEP], __m128i *dip, uint32_t *flag)
1127 struct ipv4_hdr *ipv4_hdr;
1128 struct ether_hdr *eth_hdr;
1129 uint32_t x0, x1, x2, x3;
1131 eth_hdr = rte_pktmbuf_mtod(pkt[0], struct ether_hdr *);
1132 ipv4_hdr = (struct ipv4_hdr *)(eth_hdr + 1);
1133 x0 = ipv4_hdr->dst_addr;
1134 flag[0] = pkt[0]->ol_flags & PKT_RX_IPV4_HDR;
1136 eth_hdr = rte_pktmbuf_mtod(pkt[1], struct ether_hdr *);
1137 ipv4_hdr = (struct ipv4_hdr *)(eth_hdr + 1);
1138 x1 = ipv4_hdr->dst_addr;
1139 flag[0] &= pkt[1]->ol_flags;
1141 eth_hdr = rte_pktmbuf_mtod(pkt[2], struct ether_hdr *);
1142 ipv4_hdr = (struct ipv4_hdr *)(eth_hdr + 1);
1143 x2 = ipv4_hdr->dst_addr;
1144 flag[0] &= pkt[2]->ol_flags;
1146 eth_hdr = rte_pktmbuf_mtod(pkt[3], struct ether_hdr *);
1147 ipv4_hdr = (struct ipv4_hdr *)(eth_hdr + 1);
1148 x3 = ipv4_hdr->dst_addr;
1149 flag[0] &= pkt[3]->ol_flags;
1151 dip[0] = _mm_set_epi32(x3, x2, x1, x0);
1155 * Lookup into LPM for destination port.
1156 * If lookup fails, use incoming port (portid) as destination port.
1159 processx4_step2(const struct lcore_conf *qconf, __m128i dip, uint32_t flag,
1160 uint8_t portid, struct rte_mbuf *pkt[FWDSTEP], uint16_t dprt[FWDSTEP])
1163 const __m128i bswap_mask = _mm_set_epi8(12, 13, 14, 15, 8, 9, 10, 11,
1164 4, 5, 6, 7, 0, 1, 2, 3);
1166 /* Byte swap 4 IPV4 addresses. */
1167 dip = _mm_shuffle_epi8(dip, bswap_mask);
1169 /* if all 4 packets are IPV4. */
1170 if (likely(flag != 0)) {
1171 rte_lpm_lookupx4(qconf->ipv4_lookup_struct, dip, dprt, portid);
1174 dprt[0] = get_dst_port(qconf, pkt[0], dst.u32[0], portid);
1175 dprt[1] = get_dst_port(qconf, pkt[1], dst.u32[1], portid);
1176 dprt[2] = get_dst_port(qconf, pkt[2], dst.u32[2], portid);
1177 dprt[3] = get_dst_port(qconf, pkt[3], dst.u32[3], portid);
1182 * Update source and destination MAC addresses in the ethernet header.
1183 * Perform RFC1812 checks and updates for IPV4 packets.
1186 processx4_step3(struct rte_mbuf *pkt[FWDSTEP], uint16_t dst_port[FWDSTEP])
1188 __m128i te[FWDSTEP];
1189 __m128i ve[FWDSTEP];
1190 __m128i *p[FWDSTEP];
1192 p[0] = (rte_pktmbuf_mtod(pkt[0], __m128i *));
1193 p[1] = (rte_pktmbuf_mtod(pkt[1], __m128i *));
1194 p[2] = (rte_pktmbuf_mtod(pkt[2], __m128i *));
1195 p[3] = (rte_pktmbuf_mtod(pkt[3], __m128i *));
1197 ve[0] = val_eth[dst_port[0]];
1198 te[0] = _mm_load_si128(p[0]);
1200 ve[1] = val_eth[dst_port[1]];
1201 te[1] = _mm_load_si128(p[1]);
1203 ve[2] = val_eth[dst_port[2]];
1204 te[2] = _mm_load_si128(p[2]);
1206 ve[3] = val_eth[dst_port[3]];
1207 te[3] = _mm_load_si128(p[3]);
1209 /* Update first 12 bytes, keep rest bytes intact. */
1210 te[0] = _mm_blend_epi16(te[0], ve[0], MASK_ETH);
1211 te[1] = _mm_blend_epi16(te[1], ve[1], MASK_ETH);
1212 te[2] = _mm_blend_epi16(te[2], ve[2], MASK_ETH);
1213 te[3] = _mm_blend_epi16(te[3], ve[3], MASK_ETH);
1215 _mm_store_si128(p[0], te[0]);
1216 _mm_store_si128(p[1], te[1]);
1217 _mm_store_si128(p[2], te[2]);
1218 _mm_store_si128(p[3], te[3]);
1220 rfc1812_process((struct ipv4_hdr *)((struct ether_hdr *)p[0] + 1),
1221 &dst_port[0], pkt[0]->ol_flags);
1222 rfc1812_process((struct ipv4_hdr *)((struct ether_hdr *)p[1] + 1),
1223 &dst_port[1], pkt[1]->ol_flags);
1224 rfc1812_process((struct ipv4_hdr *)((struct ether_hdr *)p[2] + 1),
1225 &dst_port[2], pkt[2]->ol_flags);
1226 rfc1812_process((struct ipv4_hdr *)((struct ether_hdr *)p[3] + 1),
1227 &dst_port[3], pkt[3]->ol_flags);
1231 * We group consecutive packets with the same destionation port into one burst.
1232 * To avoid extra latency this is done together with some other packet
1233 * processing, but after we made a final decision about packet's destination.
1234 * To do this we maintain:
1235 * pnum - array of number of consecutive packets with the same dest port for
1236 * each packet in the input burst.
1237 * lp - pointer to the last updated element in the pnum.
1238 * dlp - dest port value lp corresponds to.
1241 #define GRPSZ (1 << FWDSTEP)
1242 #define GRPMSK (GRPSZ - 1)
1244 #define GROUP_PORT_STEP(dlp, dcp, lp, pn, idx) do { \
1245 if (likely((dlp) == (dcp)[(idx)])) { \
1248 (dlp) = (dcp)[idx]; \
1249 (lp) = (pn) + (idx); \
1255 * Group consecutive packets with the same destination port in bursts of 4.
1256 * Suppose we have array of destionation ports:
1257 * dst_port[] = {a, b, c, d,, e, ... }
1258 * dp1 should contain: <a, b, c, d>, dp2: <b, c, d, e>.
1259 * We doing 4 comparisions at once and the result is 4 bit mask.
1260 * This mask is used as an index into prebuild array of pnum values.
1262 static inline uint16_t *
1263 port_groupx4(uint16_t pn[FWDSTEP + 1], uint16_t *lp, __m128i dp1, __m128i dp2)
1265 static const struct {
1266 uint64_t pnum; /* prebuild 4 values for pnum[]. */
1267 int32_t idx; /* index for new last updated elemnet. */
1268 uint16_t lpv; /* add value to the last updated element. */
1271 /* 0: a != b, b != c, c != d, d != e */
1272 .pnum = UINT64_C(0x0001000100010001),
1277 /* 1: a == b, b != c, c != d, d != e */
1278 .pnum = UINT64_C(0x0001000100010002),
1283 /* 2: a != b, b == c, c != d, d != e */
1284 .pnum = UINT64_C(0x0001000100020001),
1289 /* 3: a == b, b == c, c != d, d != e */
1290 .pnum = UINT64_C(0x0001000100020003),
1295 /* 4: a != b, b != c, c == d, d != e */
1296 .pnum = UINT64_C(0x0001000200010001),
1301 /* 5: a == b, b != c, c == d, d != e */
1302 .pnum = UINT64_C(0x0001000200010002),
1307 /* 6: a != b, b == c, c == d, d != e */
1308 .pnum = UINT64_C(0x0001000200030001),
1313 /* 7: a == b, b == c, c == d, d != e */
1314 .pnum = UINT64_C(0x0001000200030004),
1319 /* 8: a != b, b != c, c != d, d == e */
1320 .pnum = UINT64_C(0x0002000100010001),
1325 /* 9: a == b, b != c, c != d, d == e */
1326 .pnum = UINT64_C(0x0002000100010002),
1331 /* 0xa: a != b, b == c, c != d, d == e */
1332 .pnum = UINT64_C(0x0002000100020001),
1337 /* 0xb: a == b, b == c, c != d, d == e */
1338 .pnum = UINT64_C(0x0002000100020003),
1343 /* 0xc: a != b, b != c, c == d, d == e */
1344 .pnum = UINT64_C(0x0002000300010001),
1349 /* 0xd: a == b, b != c, c == d, d == e */
1350 .pnum = UINT64_C(0x0002000300010002),
1355 /* 0xe: a != b, b == c, c == d, d == e */
1356 .pnum = UINT64_C(0x0002000300040001),
1361 /* 0xf: a == b, b == c, c == d, d == e */
1362 .pnum = UINT64_C(0x0002000300040005),
1369 uint16_t u16[FWDSTEP + 1];
1371 } *pnum = (void *)pn;
1375 dp1 = _mm_cmpeq_epi16(dp1, dp2);
1376 dp1 = _mm_unpacklo_epi16(dp1, dp1);
1377 v = _mm_movemask_ps((__m128)dp1);
1379 /* update last port counter. */
1380 lp[0] += gptbl[v].lpv;
1382 /* if dest port value has changed. */
1384 lp = pnum->u16 + gptbl[v].idx;
1386 pnum->u64 = gptbl[v].pnum;
1392 #endif /* APP_LOOKUP_METHOD */
1394 /* main processing loop */
1396 main_loop(__attribute__((unused)) void *dummy)
1398 struct rte_mbuf *pkts_burst[MAX_PKT_BURST];
1400 uint64_t prev_tsc, diff_tsc, cur_tsc;
1402 uint8_t portid, queueid;
1403 struct lcore_conf *qconf;
1404 const uint64_t drain_tsc = (rte_get_tsc_hz() + US_PER_S - 1) /
1405 US_PER_S * BURST_TX_DRAIN_US;
1407 #if ((APP_LOOKUP_METHOD == APP_LOOKUP_LPM) && \
1408 (ENABLE_MULTI_BUFFER_OPTIMIZE == 1))
1412 uint16_t dst_port[MAX_PKT_BURST];
1413 __m128i dip[MAX_PKT_BURST / FWDSTEP];
1414 uint32_t flag[MAX_PKT_BURST / FWDSTEP];
1415 uint16_t pnum[MAX_PKT_BURST + 1];
1420 lcore_id = rte_lcore_id();
1421 qconf = &lcore_conf[lcore_id];
1423 if (qconf->n_rx_queue == 0) {
1424 RTE_LOG(INFO, L3FWD, "lcore %u has nothing to do\n", lcore_id);
1428 RTE_LOG(INFO, L3FWD, "entering main loop on lcore %u\n", lcore_id);
1430 for (i = 0; i < qconf->n_rx_queue; i++) {
1432 portid = qconf->rx_queue_list[i].port_id;
1433 queueid = qconf->rx_queue_list[i].queue_id;
1434 RTE_LOG(INFO, L3FWD, " -- lcoreid=%u portid=%hhu rxqueueid=%hhu\n", lcore_id,
1440 cur_tsc = rte_rdtsc();
1443 * TX burst queue drain
1445 diff_tsc = cur_tsc - prev_tsc;
1446 if (unlikely(diff_tsc > drain_tsc)) {
1449 * This could be optimized (use queueid instead of
1450 * portid), but it is not called so often
1452 for (portid = 0; portid < RTE_MAX_ETHPORTS; portid++) {
1453 if (qconf->tx_mbufs[portid].len == 0)
1456 qconf->tx_mbufs[portid].len,
1458 qconf->tx_mbufs[portid].len = 0;
1465 * Read packet from RX queues
1467 for (i = 0; i < qconf->n_rx_queue; ++i) {
1468 portid = qconf->rx_queue_list[i].port_id;
1469 queueid = qconf->rx_queue_list[i].queue_id;
1470 nb_rx = rte_eth_rx_burst(portid, queueid, pkts_burst,
1475 #if (ENABLE_MULTI_BUFFER_OPTIMIZE == 1)
1476 #if (APP_LOOKUP_METHOD == APP_LOOKUP_EXACT_MATCH)
1479 * Send nb_rx - nb_rx%4 packets
1482 int32_t n = RTE_ALIGN_FLOOR(nb_rx, 4);
1483 for (j = 0; j < n ; j+=4) {
1484 uint32_t ol_flag = pkts_burst[j]->ol_flags
1485 & pkts_burst[j+1]->ol_flags
1486 & pkts_burst[j+2]->ol_flags
1487 & pkts_burst[j+3]->ol_flags;
1488 if (ol_flag & PKT_RX_IPV4_HDR ) {
1489 simple_ipv4_fwd_4pkts(&pkts_burst[j],
1491 } else if (ol_flag & PKT_RX_IPV6_HDR) {
1492 simple_ipv6_fwd_4pkts(&pkts_burst[j],
1495 l3fwd_simple_forward(pkts_burst[j],
1497 l3fwd_simple_forward(pkts_burst[j+1],
1499 l3fwd_simple_forward(pkts_burst[j+2],
1501 l3fwd_simple_forward(pkts_burst[j+3],
1505 for (; j < nb_rx ; j++) {
1506 l3fwd_simple_forward(pkts_burst[j],
1510 #elif (APP_LOOKUP_METHOD == APP_LOOKUP_LPM)
1512 k = RTE_ALIGN_FLOOR(nb_rx, FWDSTEP);
1513 for (j = 0; j != k; j += FWDSTEP) {
1514 processx4_step1(&pkts_burst[j],
1516 &flag[j / FWDSTEP]);
1519 k = RTE_ALIGN_FLOOR(nb_rx, FWDSTEP);
1520 for (j = 0; j != k; j += FWDSTEP) {
1521 processx4_step2(qconf, dip[j / FWDSTEP],
1522 flag[j / FWDSTEP], portid,
1523 &pkts_burst[j], &dst_port[j]);
1527 * Finish packet processing and group consecutive
1528 * packets with the same destination port.
1530 k = RTE_ALIGN_FLOOR(nb_rx, FWDSTEP);
1537 processx4_step3(pkts_burst, dst_port);
1539 /* dp1: <d[0], d[1], d[2], d[3], ... > */
1540 dp1 = _mm_loadu_si128((__m128i *)dst_port);
1542 for (j = FWDSTEP; j != k; j += FWDSTEP) {
1543 processx4_step3(&pkts_burst[j],
1548 * <d[j-3], d[j-2], d[j-1], d[j], ... >
1550 dp2 = _mm_loadu_si128((__m128i *)
1551 &dst_port[j - FWDSTEP + 1]);
1552 lp = port_groupx4(&pnum[j - FWDSTEP],
1557 * <d[j], d[j+1], d[j+2], d[j+3], ... >
1559 dp1 = _mm_srli_si128(dp2,
1561 sizeof(dst_port[0]));
1565 * dp2: <d[j-3], d[j-2], d[j-1], d[j-1], ... >
1567 dp2 = _mm_shufflelo_epi16(dp1, 0xf9);
1568 lp = port_groupx4(&pnum[j - FWDSTEP], lp,
1572 * remove values added by the last repeated
1576 dlp = dst_port[j - 1];
1578 /* set dlp and lp to the never used values. */
1580 lp = pnum + MAX_PKT_BURST;
1583 /* Process up to last 3 packets one by one. */
1584 switch (nb_rx % FWDSTEP) {
1586 process_packet(qconf, pkts_burst[j],
1587 dst_port + j, portid);
1588 GROUP_PORT_STEP(dlp, dst_port, lp, pnum, j);
1591 process_packet(qconf, pkts_burst[j],
1592 dst_port + j, portid);
1593 GROUP_PORT_STEP(dlp, dst_port, lp, pnum, j);
1596 process_packet(qconf, pkts_burst[j],
1597 dst_port + j, portid);
1598 GROUP_PORT_STEP(dlp, dst_port, lp, pnum, j);
1603 * Send packets out, through destination port.
1604 * Consecuteve pacekts with the same destination port
1605 * are already grouped together.
1606 * If destination port for the packet equals BAD_PORT,
1607 * then free the packet without sending it out.
1609 for (j = 0; j < nb_rx; j += k) {
1617 if (likely(pn != BAD_PORT)) {
1618 send_packetsx4(qconf, pn,
1621 for (m = j; m != j + k; m++)
1622 rte_pktmbuf_free(pkts_burst[m]);
1626 #endif /* APP_LOOKUP_METHOD */
1627 #else /* ENABLE_MULTI_BUFFER_OPTIMIZE == 0 */
1629 /* Prefetch first packets */
1630 for (j = 0; j < PREFETCH_OFFSET && j < nb_rx; j++) {
1631 rte_prefetch0(rte_pktmbuf_mtod(
1632 pkts_burst[j], void *));
1635 /* Prefetch and forward already prefetched packets */
1636 for (j = 0; j < (nb_rx - PREFETCH_OFFSET); j++) {
1637 rte_prefetch0(rte_pktmbuf_mtod(pkts_burst[
1638 j + PREFETCH_OFFSET], void *));
1639 l3fwd_simple_forward(pkts_burst[j], portid,
1643 /* Forward remaining prefetched packets */
1644 for (; j < nb_rx; j++) {
1645 l3fwd_simple_forward(pkts_burst[j], portid,
1648 #endif /* ENABLE_MULTI_BUFFER_OPTIMIZE */
1655 check_lcore_params(void)
1657 uint8_t queue, lcore;
1661 for (i = 0; i < nb_lcore_params; ++i) {
1662 queue = lcore_params[i].queue_id;
1663 if (queue >= MAX_RX_QUEUE_PER_PORT) {
1664 printf("invalid queue number: %hhu\n", queue);
1667 lcore = lcore_params[i].lcore_id;
1668 if (!rte_lcore_is_enabled(lcore)) {
1669 printf("error: lcore %hhu is not enabled in lcore mask\n", lcore);
1672 if ((socketid = rte_lcore_to_socket_id(lcore) != 0) &&
1674 printf("warning: lcore %hhu is on socket %d with numa off \n",
1682 check_port_config(const unsigned nb_ports)
1687 for (i = 0; i < nb_lcore_params; ++i) {
1688 portid = lcore_params[i].port_id;
1689 if ((enabled_port_mask & (1 << portid)) == 0) {
1690 printf("port %u is not enabled in port mask\n", portid);
1693 if (portid >= nb_ports) {
1694 printf("port %u is not present on the board\n", portid);
1702 get_port_n_rx_queues(const uint8_t port)
1707 for (i = 0; i < nb_lcore_params; ++i) {
1708 if (lcore_params[i].port_id == port && lcore_params[i].queue_id > queue)
1709 queue = lcore_params[i].queue_id;
1711 return (uint8_t)(++queue);
1715 init_lcore_rx_queues(void)
1717 uint16_t i, nb_rx_queue;
1720 for (i = 0; i < nb_lcore_params; ++i) {
1721 lcore = lcore_params[i].lcore_id;
1722 nb_rx_queue = lcore_conf[lcore].n_rx_queue;
1723 if (nb_rx_queue >= MAX_RX_QUEUE_PER_LCORE) {
1724 printf("error: too many queues (%u) for lcore: %u\n",
1725 (unsigned)nb_rx_queue + 1, (unsigned)lcore);
1728 lcore_conf[lcore].rx_queue_list[nb_rx_queue].port_id =
1729 lcore_params[i].port_id;
1730 lcore_conf[lcore].rx_queue_list[nb_rx_queue].queue_id =
1731 lcore_params[i].queue_id;
1732 lcore_conf[lcore].n_rx_queue++;
1740 print_usage(const char *prgname)
1742 printf ("%s [EAL options] -- -p PORTMASK -P"
1743 " [--config (port,queue,lcore)[,(port,queue,lcore]]"
1744 " [--enable-jumbo [--max-pkt-len PKTLEN]]\n"
1745 " -p PORTMASK: hexadecimal bitmask of ports to configure\n"
1746 " -P : enable promiscuous mode\n"
1747 " --config (port,queue,lcore): rx queues configuration\n"
1748 " --no-numa: optional, disable numa awareness\n"
1749 " --ipv6: optional, specify it if running ipv6 packets\n"
1750 " --enable-jumbo: enable jumbo frame"
1751 " which max packet len is PKTLEN in decimal (64-9600)\n"
1752 " --hash-entry-num: specify the hash entry number in hexadecimal to be setup\n",
1756 static int parse_max_pkt_len(const char *pktlen)
1761 /* parse decimal string */
1762 len = strtoul(pktlen, &end, 10);
1763 if ((pktlen[0] == '\0') || (end == NULL) || (*end != '\0'))
1773 parse_portmask(const char *portmask)
1778 /* parse hexadecimal string */
1779 pm = strtoul(portmask, &end, 16);
1780 if ((portmask[0] == '\0') || (end == NULL) || (*end != '\0'))
1789 #if (APP_LOOKUP_METHOD == APP_LOOKUP_EXACT_MATCH)
1791 parse_hash_entry_number(const char *hash_entry_num)
1794 unsigned long hash_en;
1795 /* parse hexadecimal string */
1796 hash_en = strtoul(hash_entry_num, &end, 16);
1797 if ((hash_entry_num[0] == '\0') || (end == NULL) || (*end != '\0'))
1808 parse_config(const char *q_arg)
1811 const char *p, *p0 = q_arg;
1819 unsigned long int_fld[_NUM_FLD];
1820 char *str_fld[_NUM_FLD];
1824 nb_lcore_params = 0;
1826 while ((p = strchr(p0,'(')) != NULL) {
1828 if((p0 = strchr(p,')')) == NULL)
1832 if(size >= sizeof(s))
1835 snprintf(s, sizeof(s), "%.*s", size, p);
1836 if (rte_strsplit(s, sizeof(s), str_fld, _NUM_FLD, ',') != _NUM_FLD)
1838 for (i = 0; i < _NUM_FLD; i++){
1840 int_fld[i] = strtoul(str_fld[i], &end, 0);
1841 if (errno != 0 || end == str_fld[i] || int_fld[i] > 255)
1844 if (nb_lcore_params >= MAX_LCORE_PARAMS) {
1845 printf("exceeded max number of lcore params: %hu\n",
1849 lcore_params_array[nb_lcore_params].port_id = (uint8_t)int_fld[FLD_PORT];
1850 lcore_params_array[nb_lcore_params].queue_id = (uint8_t)int_fld[FLD_QUEUE];
1851 lcore_params_array[nb_lcore_params].lcore_id = (uint8_t)int_fld[FLD_LCORE];
1854 lcore_params = lcore_params_array;
1858 #define CMD_LINE_OPT_CONFIG "config"
1859 #define CMD_LINE_OPT_NO_NUMA "no-numa"
1860 #define CMD_LINE_OPT_IPV6 "ipv6"
1861 #define CMD_LINE_OPT_ENABLE_JUMBO "enable-jumbo"
1862 #define CMD_LINE_OPT_HASH_ENTRY_NUM "hash-entry-num"
1864 /* Parse the argument given in the command line of the application */
1866 parse_args(int argc, char **argv)
1871 char *prgname = argv[0];
1872 static struct option lgopts[] = {
1873 {CMD_LINE_OPT_CONFIG, 1, 0, 0},
1874 {CMD_LINE_OPT_NO_NUMA, 0, 0, 0},
1875 {CMD_LINE_OPT_IPV6, 0, 0, 0},
1876 {CMD_LINE_OPT_ENABLE_JUMBO, 0, 0, 0},
1877 {CMD_LINE_OPT_HASH_ENTRY_NUM, 1, 0, 0},
1883 while ((opt = getopt_long(argc, argvopt, "p:P",
1884 lgopts, &option_index)) != EOF) {
1889 enabled_port_mask = parse_portmask(optarg);
1890 if (enabled_port_mask == 0) {
1891 printf("invalid portmask\n");
1892 print_usage(prgname);
1897 printf("Promiscuous mode selected\n");
1903 if (!strncmp(lgopts[option_index].name, CMD_LINE_OPT_CONFIG,
1904 sizeof (CMD_LINE_OPT_CONFIG))) {
1905 ret = parse_config(optarg);
1907 printf("invalid config\n");
1908 print_usage(prgname);
1913 if (!strncmp(lgopts[option_index].name, CMD_LINE_OPT_NO_NUMA,
1914 sizeof(CMD_LINE_OPT_NO_NUMA))) {
1915 printf("numa is disabled \n");
1919 #if (APP_LOOKUP_METHOD == APP_LOOKUP_EXACT_MATCH)
1920 if (!strncmp(lgopts[option_index].name, CMD_LINE_OPT_IPV6,
1921 sizeof(CMD_LINE_OPT_IPV6))) {
1922 printf("ipv6 is specified \n");
1927 if (!strncmp(lgopts[option_index].name, CMD_LINE_OPT_ENABLE_JUMBO,
1928 sizeof (CMD_LINE_OPT_ENABLE_JUMBO))) {
1929 struct option lenopts = {"max-pkt-len", required_argument, 0, 0};
1931 printf("jumbo frame is enabled - disabling simple TX path\n");
1932 port_conf.rxmode.jumbo_frame = 1;
1934 /* if no max-pkt-len set, use the default value ETHER_MAX_LEN */
1935 if (0 == getopt_long(argc, argvopt, "", &lenopts, &option_index)) {
1936 ret = parse_max_pkt_len(optarg);
1937 if ((ret < 64) || (ret > MAX_JUMBO_PKT_LEN)){
1938 printf("invalid packet length\n");
1939 print_usage(prgname);
1942 port_conf.rxmode.max_rx_pkt_len = ret;
1944 printf("set jumbo frame max packet length to %u\n",
1945 (unsigned int)port_conf.rxmode.max_rx_pkt_len);
1947 #if (APP_LOOKUP_METHOD == APP_LOOKUP_EXACT_MATCH)
1948 if (!strncmp(lgopts[option_index].name, CMD_LINE_OPT_HASH_ENTRY_NUM,
1949 sizeof(CMD_LINE_OPT_HASH_ENTRY_NUM))) {
1950 ret = parse_hash_entry_number(optarg);
1951 if ((ret > 0) && (ret <= L3FWD_HASH_ENTRIES)) {
1952 hash_entry_number = ret;
1954 printf("invalid hash entry number\n");
1955 print_usage(prgname);
1963 print_usage(prgname);
1969 argv[optind-1] = prgname;
1972 optind = 0; /* reset getopt lib */
1977 print_ethaddr(const char *name, const struct ether_addr *eth_addr)
1979 char buf[ETHER_ADDR_FMT_SIZE];
1980 ether_format_addr(buf, ETHER_ADDR_FMT_SIZE, eth_addr);
1981 printf("%s%s", name, buf);
1984 #if (APP_LOOKUP_METHOD == APP_LOOKUP_EXACT_MATCH)
1986 static void convert_ipv4_5tuple(struct ipv4_5tuple* key1,
1987 union ipv4_5tuple_host* key2)
1989 key2->ip_dst = rte_cpu_to_be_32(key1->ip_dst);
1990 key2->ip_src = rte_cpu_to_be_32(key1->ip_src);
1991 key2->port_dst = rte_cpu_to_be_16(key1->port_dst);
1992 key2->port_src = rte_cpu_to_be_16(key1->port_src);
1993 key2->proto = key1->proto;
1999 static void convert_ipv6_5tuple(struct ipv6_5tuple* key1,
2000 union ipv6_5tuple_host* key2)
2003 for (i = 0; i < 16; i++)
2005 key2->ip_dst[i] = key1->ip_dst[i];
2006 key2->ip_src[i] = key1->ip_src[i];
2008 key2->port_dst = rte_cpu_to_be_16(key1->port_dst);
2009 key2->port_src = rte_cpu_to_be_16(key1->port_src);
2010 key2->proto = key1->proto;
2017 #define BYTE_VALUE_MAX 256
2018 #define ALL_32_BITS 0xffffffff
2019 #define BIT_8_TO_15 0x0000ff00
2021 populate_ipv4_few_flow_into_table(const struct rte_hash* h)
2025 uint32_t array_len = sizeof(ipv4_l3fwd_route_array)/sizeof(ipv4_l3fwd_route_array[0]);
2027 mask0 = _mm_set_epi32(ALL_32_BITS, ALL_32_BITS, ALL_32_BITS, BIT_8_TO_15);
2028 for (i = 0; i < array_len; i++) {
2029 struct ipv4_l3fwd_route entry;
2030 union ipv4_5tuple_host newkey;
2031 entry = ipv4_l3fwd_route_array[i];
2032 convert_ipv4_5tuple(&entry.key, &newkey);
2033 ret = rte_hash_add_key (h,(void *) &newkey);
2035 rte_exit(EXIT_FAILURE, "Unable to add entry %" PRIu32
2036 " to the l3fwd hash.\n", i);
2038 ipv4_l3fwd_out_if[ret] = entry.if_out;
2040 printf("Hash: Adding 0x%" PRIx32 " keys\n", array_len);
2043 #define BIT_16_TO_23 0x00ff0000
2045 populate_ipv6_few_flow_into_table(const struct rte_hash* h)
2049 uint32_t array_len = sizeof(ipv6_l3fwd_route_array)/sizeof(ipv6_l3fwd_route_array[0]);
2051 mask1 = _mm_set_epi32(ALL_32_BITS, ALL_32_BITS, ALL_32_BITS, BIT_16_TO_23);
2052 mask2 = _mm_set_epi32(0, 0, ALL_32_BITS, ALL_32_BITS);
2053 for (i = 0; i < array_len; i++) {
2054 struct ipv6_l3fwd_route entry;
2055 union ipv6_5tuple_host newkey;
2056 entry = ipv6_l3fwd_route_array[i];
2057 convert_ipv6_5tuple(&entry.key, &newkey);
2058 ret = rte_hash_add_key (h, (void *) &newkey);
2060 rte_exit(EXIT_FAILURE, "Unable to add entry %" PRIu32
2061 " to the l3fwd hash.\n", i);
2063 ipv6_l3fwd_out_if[ret] = entry.if_out;
2065 printf("Hash: Adding 0x%" PRIx32 "keys\n", array_len);
2068 #define NUMBER_PORT_USED 4
2070 populate_ipv4_many_flow_into_table(const struct rte_hash* h,
2071 unsigned int nr_flow)
2074 mask0 = _mm_set_epi32(ALL_32_BITS, ALL_32_BITS, ALL_32_BITS, BIT_8_TO_15);
2075 for (i = 0; i < nr_flow; i++) {
2076 struct ipv4_l3fwd_route entry;
2077 union ipv4_5tuple_host newkey;
2078 uint8_t a = (uint8_t) ((i/NUMBER_PORT_USED)%BYTE_VALUE_MAX);
2079 uint8_t b = (uint8_t) (((i/NUMBER_PORT_USED)/BYTE_VALUE_MAX)%BYTE_VALUE_MAX);
2080 uint8_t c = (uint8_t) ((i/NUMBER_PORT_USED)/(BYTE_VALUE_MAX*BYTE_VALUE_MAX));
2081 /* Create the ipv4 exact match flow */
2082 memset(&entry, 0, sizeof(entry));
2083 switch (i & (NUMBER_PORT_USED -1)) {
2085 entry = ipv4_l3fwd_route_array[0];
2086 entry.key.ip_dst = IPv4(101,c,b,a);
2089 entry = ipv4_l3fwd_route_array[1];
2090 entry.key.ip_dst = IPv4(201,c,b,a);
2093 entry = ipv4_l3fwd_route_array[2];
2094 entry.key.ip_dst = IPv4(111,c,b,a);
2097 entry = ipv4_l3fwd_route_array[3];
2098 entry.key.ip_dst = IPv4(211,c,b,a);
2101 convert_ipv4_5tuple(&entry.key, &newkey);
2102 int32_t ret = rte_hash_add_key(h,(void *) &newkey);
2104 rte_exit(EXIT_FAILURE, "Unable to add entry %u\n", i);
2106 ipv4_l3fwd_out_if[ret] = (uint8_t) entry.if_out;
2109 printf("Hash: Adding 0x%x keys\n", nr_flow);
2113 populate_ipv6_many_flow_into_table(const struct rte_hash* h,
2114 unsigned int nr_flow)
2117 mask1 = _mm_set_epi32(ALL_32_BITS, ALL_32_BITS, ALL_32_BITS, BIT_16_TO_23);
2118 mask2 = _mm_set_epi32(0, 0, ALL_32_BITS, ALL_32_BITS);
2119 for (i = 0; i < nr_flow; i++) {
2120 struct ipv6_l3fwd_route entry;
2121 union ipv6_5tuple_host newkey;
2122 uint8_t a = (uint8_t) ((i/NUMBER_PORT_USED)%BYTE_VALUE_MAX);
2123 uint8_t b = (uint8_t) (((i/NUMBER_PORT_USED)/BYTE_VALUE_MAX)%BYTE_VALUE_MAX);
2124 uint8_t c = (uint8_t) ((i/NUMBER_PORT_USED)/(BYTE_VALUE_MAX*BYTE_VALUE_MAX));
2125 /* Create the ipv6 exact match flow */
2126 memset(&entry, 0, sizeof(entry));
2127 switch (i & (NUMBER_PORT_USED - 1)) {
2128 case 0: entry = ipv6_l3fwd_route_array[0]; break;
2129 case 1: entry = ipv6_l3fwd_route_array[1]; break;
2130 case 2: entry = ipv6_l3fwd_route_array[2]; break;
2131 case 3: entry = ipv6_l3fwd_route_array[3]; break;
2133 entry.key.ip_dst[13] = c;
2134 entry.key.ip_dst[14] = b;
2135 entry.key.ip_dst[15] = a;
2136 convert_ipv6_5tuple(&entry.key, &newkey);
2137 int32_t ret = rte_hash_add_key(h,(void *) &newkey);
2139 rte_exit(EXIT_FAILURE, "Unable to add entry %u\n", i);
2141 ipv6_l3fwd_out_if[ret] = (uint8_t) entry.if_out;
2144 printf("Hash: Adding 0x%x keys\n", nr_flow);
2148 setup_hash(int socketid)
2150 struct rte_hash_parameters ipv4_l3fwd_hash_params = {
2152 .entries = L3FWD_HASH_ENTRIES,
2153 .bucket_entries = 4,
2154 .key_len = sizeof(union ipv4_5tuple_host),
2155 .hash_func = ipv4_hash_crc,
2156 .hash_func_init_val = 0,
2159 struct rte_hash_parameters ipv6_l3fwd_hash_params = {
2161 .entries = L3FWD_HASH_ENTRIES,
2162 .bucket_entries = 4,
2163 .key_len = sizeof(union ipv6_5tuple_host),
2164 .hash_func = ipv6_hash_crc,
2165 .hash_func_init_val = 0,
2170 /* create ipv4 hash */
2171 snprintf(s, sizeof(s), "ipv4_l3fwd_hash_%d", socketid);
2172 ipv4_l3fwd_hash_params.name = s;
2173 ipv4_l3fwd_hash_params.socket_id = socketid;
2174 ipv4_l3fwd_lookup_struct[socketid] = rte_hash_create(&ipv4_l3fwd_hash_params);
2175 if (ipv4_l3fwd_lookup_struct[socketid] == NULL)
2176 rte_exit(EXIT_FAILURE, "Unable to create the l3fwd hash on "
2177 "socket %d\n", socketid);
2179 /* create ipv6 hash */
2180 snprintf(s, sizeof(s), "ipv6_l3fwd_hash_%d", socketid);
2181 ipv6_l3fwd_hash_params.name = s;
2182 ipv6_l3fwd_hash_params.socket_id = socketid;
2183 ipv6_l3fwd_lookup_struct[socketid] = rte_hash_create(&ipv6_l3fwd_hash_params);
2184 if (ipv6_l3fwd_lookup_struct[socketid] == NULL)
2185 rte_exit(EXIT_FAILURE, "Unable to create the l3fwd hash on "
2186 "socket %d\n", socketid);
2188 if (hash_entry_number != HASH_ENTRY_NUMBER_DEFAULT) {
2189 /* For testing hash matching with a large number of flows we
2190 * generate millions of IP 5-tuples with an incremented dst
2191 * address to initialize the hash table. */
2193 /* populate the ipv4 hash */
2194 populate_ipv4_many_flow_into_table(
2195 ipv4_l3fwd_lookup_struct[socketid], hash_entry_number);
2197 /* populate the ipv6 hash */
2198 populate_ipv6_many_flow_into_table(
2199 ipv6_l3fwd_lookup_struct[socketid], hash_entry_number);
2202 /* Use data in ipv4/ipv6 l3fwd lookup table directly to initialize the hash table */
2204 /* populate the ipv4 hash */
2205 populate_ipv4_few_flow_into_table(ipv4_l3fwd_lookup_struct[socketid]);
2207 /* populate the ipv6 hash */
2208 populate_ipv6_few_flow_into_table(ipv6_l3fwd_lookup_struct[socketid]);
2214 #if (APP_LOOKUP_METHOD == APP_LOOKUP_LPM)
2216 setup_lpm(int socketid)
2218 struct rte_lpm6_config config;
2223 /* create the LPM table */
2224 snprintf(s, sizeof(s), "IPV4_L3FWD_LPM_%d", socketid);
2225 ipv4_l3fwd_lookup_struct[socketid] = rte_lpm_create(s, socketid,
2226 IPV4_L3FWD_LPM_MAX_RULES, 0);
2227 if (ipv4_l3fwd_lookup_struct[socketid] == NULL)
2228 rte_exit(EXIT_FAILURE, "Unable to create the l3fwd LPM table"
2229 " on socket %d\n", socketid);
2231 /* populate the LPM table */
2232 for (i = 0; i < IPV4_L3FWD_NUM_ROUTES; i++) {
2234 /* skip unused ports */
2235 if ((1 << ipv4_l3fwd_route_array[i].if_out &
2236 enabled_port_mask) == 0)
2239 ret = rte_lpm_add(ipv4_l3fwd_lookup_struct[socketid],
2240 ipv4_l3fwd_route_array[i].ip,
2241 ipv4_l3fwd_route_array[i].depth,
2242 ipv4_l3fwd_route_array[i].if_out);
2245 rte_exit(EXIT_FAILURE, "Unable to add entry %u to the "
2246 "l3fwd LPM table on socket %d\n",
2250 printf("LPM: Adding route 0x%08x / %d (%d)\n",
2251 (unsigned)ipv4_l3fwd_route_array[i].ip,
2252 ipv4_l3fwd_route_array[i].depth,
2253 ipv4_l3fwd_route_array[i].if_out);
2256 /* create the LPM6 table */
2257 snprintf(s, sizeof(s), "IPV6_L3FWD_LPM_%d", socketid);
2259 config.max_rules = IPV6_L3FWD_LPM_MAX_RULES;
2260 config.number_tbl8s = IPV6_L3FWD_LPM_NUMBER_TBL8S;
2262 ipv6_l3fwd_lookup_struct[socketid] = rte_lpm6_create(s, socketid,
2264 if (ipv6_l3fwd_lookup_struct[socketid] == NULL)
2265 rte_exit(EXIT_FAILURE, "Unable to create the l3fwd LPM table"
2266 " on socket %d\n", socketid);
2268 /* populate the LPM table */
2269 for (i = 0; i < IPV6_L3FWD_NUM_ROUTES; i++) {
2271 /* skip unused ports */
2272 if ((1 << ipv6_l3fwd_route_array[i].if_out &
2273 enabled_port_mask) == 0)
2276 ret = rte_lpm6_add(ipv6_l3fwd_lookup_struct[socketid],
2277 ipv6_l3fwd_route_array[i].ip,
2278 ipv6_l3fwd_route_array[i].depth,
2279 ipv6_l3fwd_route_array[i].if_out);
2282 rte_exit(EXIT_FAILURE, "Unable to add entry %u to the "
2283 "l3fwd LPM table on socket %d\n",
2287 printf("LPM: Adding route %s / %d (%d)\n",
2289 ipv6_l3fwd_route_array[i].depth,
2290 ipv6_l3fwd_route_array[i].if_out);
2296 init_mem(unsigned nb_mbuf)
2298 struct lcore_conf *qconf;
2303 for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
2304 if (rte_lcore_is_enabled(lcore_id) == 0)
2308 socketid = rte_lcore_to_socket_id(lcore_id);
2312 if (socketid >= NB_SOCKETS) {
2313 rte_exit(EXIT_FAILURE, "Socket %d of lcore %u is out of range %d\n",
2314 socketid, lcore_id, NB_SOCKETS);
2316 if (pktmbuf_pool[socketid] == NULL) {
2317 snprintf(s, sizeof(s), "mbuf_pool_%d", socketid);
2318 pktmbuf_pool[socketid] =
2319 rte_mempool_create(s, nb_mbuf, MBUF_SIZE, MEMPOOL_CACHE_SIZE,
2320 sizeof(struct rte_pktmbuf_pool_private),
2321 rte_pktmbuf_pool_init, NULL,
2322 rte_pktmbuf_init, NULL,
2324 if (pktmbuf_pool[socketid] == NULL)
2325 rte_exit(EXIT_FAILURE,
2326 "Cannot init mbuf pool on socket %d\n", socketid);
2328 printf("Allocated mbuf pool on socket %d\n", socketid);
2330 #if (APP_LOOKUP_METHOD == APP_LOOKUP_LPM)
2331 setup_lpm(socketid);
2333 setup_hash(socketid);
2336 qconf = &lcore_conf[lcore_id];
2337 qconf->ipv4_lookup_struct = ipv4_l3fwd_lookup_struct[socketid];
2338 qconf->ipv6_lookup_struct = ipv6_l3fwd_lookup_struct[socketid];
2343 /* Check the link status of all ports in up to 9s, and print them finally */
2345 check_all_ports_link_status(uint8_t port_num, uint32_t port_mask)
2347 #define CHECK_INTERVAL 100 /* 100ms */
2348 #define MAX_CHECK_TIME 90 /* 9s (90 * 100ms) in total */
2349 uint8_t portid, count, all_ports_up, print_flag = 0;
2350 struct rte_eth_link link;
2352 printf("\nChecking link status");
2354 for (count = 0; count <= MAX_CHECK_TIME; count++) {
2356 for (portid = 0; portid < port_num; portid++) {
2357 if ((port_mask & (1 << portid)) == 0)
2359 memset(&link, 0, sizeof(link));
2360 rte_eth_link_get_nowait(portid, &link);
2361 /* print link status if flag set */
2362 if (print_flag == 1) {
2363 if (link.link_status)
2364 printf("Port %d Link Up - speed %u "
2365 "Mbps - %s\n", (uint8_t)portid,
2366 (unsigned)link.link_speed,
2367 (link.link_duplex == ETH_LINK_FULL_DUPLEX) ?
2368 ("full-duplex") : ("half-duplex\n"));
2370 printf("Port %d Link Down\n",
2374 /* clear all_ports_up flag if any link down */
2375 if (link.link_status == 0) {
2380 /* after finally printing all link status, get out */
2381 if (print_flag == 1)
2384 if (all_ports_up == 0) {
2387 rte_delay_ms(CHECK_INTERVAL);
2390 /* set the print_flag if all ports up or timeout */
2391 if (all_ports_up == 1 || count == (MAX_CHECK_TIME - 1)) {
2399 main(int argc, char **argv)
2401 struct lcore_conf *qconf;
2402 struct rte_eth_dev_info dev_info;
2403 struct rte_eth_txconf *txconf;
2408 uint32_t n_tx_queue, nb_lcores;
2409 uint8_t portid, nb_rx_queue, queue, socketid;
2412 ret = rte_eal_init(argc, argv);
2414 rte_exit(EXIT_FAILURE, "Invalid EAL parameters\n");
2418 /* parse application arguments (after the EAL ones) */
2419 ret = parse_args(argc, argv);
2421 rte_exit(EXIT_FAILURE, "Invalid L3FWD parameters\n");
2423 if (check_lcore_params() < 0)
2424 rte_exit(EXIT_FAILURE, "check_lcore_params failed\n");
2426 ret = init_lcore_rx_queues();
2428 rte_exit(EXIT_FAILURE, "init_lcore_rx_queues failed\n");
2430 nb_ports = rte_eth_dev_count();
2431 if (nb_ports > RTE_MAX_ETHPORTS)
2432 nb_ports = RTE_MAX_ETHPORTS;
2434 if (check_port_config(nb_ports) < 0)
2435 rte_exit(EXIT_FAILURE, "check_port_config failed\n");
2437 nb_lcores = rte_lcore_count();
2439 /* initialize all ports */
2440 for (portid = 0; portid < nb_ports; portid++) {
2441 /* skip ports that are not enabled */
2442 if ((enabled_port_mask & (1 << portid)) == 0) {
2443 printf("\nSkipping disabled port %d\n", portid);
2448 printf("Initializing port %d ... ", portid );
2451 nb_rx_queue = get_port_n_rx_queues(portid);
2452 n_tx_queue = nb_lcores;
2453 if (n_tx_queue > MAX_TX_QUEUE_PER_PORT)
2454 n_tx_queue = MAX_TX_QUEUE_PER_PORT;
2455 printf("Creating queues: nb_rxq=%d nb_txq=%u... ",
2456 nb_rx_queue, (unsigned)n_tx_queue );
2457 ret = rte_eth_dev_configure(portid, nb_rx_queue,
2458 (uint16_t)n_tx_queue, &port_conf);
2460 rte_exit(EXIT_FAILURE, "Cannot configure device: err=%d, port=%d\n",
2463 rte_eth_macaddr_get(portid, &ports_eth_addr[portid]);
2464 print_ethaddr(" Address:", &ports_eth_addr[portid]);
2468 * prepare dst and src MACs for each port.
2470 *(uint64_t *)(val_eth + portid) =
2471 ETHER_LOCAL_ADMIN_ADDR + ((uint64_t)portid << 40);
2472 ether_addr_copy(&ports_eth_addr[portid],
2473 (struct ether_addr *)(val_eth + portid) + 1);
2476 ret = init_mem(NB_MBUF);
2478 rte_exit(EXIT_FAILURE, "init_mem failed\n");
2480 /* init one TX queue per couple (lcore,port) */
2482 for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
2483 if (rte_lcore_is_enabled(lcore_id) == 0)
2487 socketid = (uint8_t)rte_lcore_to_socket_id(lcore_id);
2491 printf("txq=%u,%d,%d ", lcore_id, queueid, socketid);
2494 rte_eth_dev_info_get(portid, &dev_info);
2495 txconf = &dev_info.default_txconf;
2496 if (port_conf.rxmode.jumbo_frame)
2497 txconf->txq_flags = 0;
2498 ret = rte_eth_tx_queue_setup(portid, queueid, nb_txd,
2501 rte_exit(EXIT_FAILURE, "rte_eth_tx_queue_setup: err=%d, "
2502 "port=%d\n", ret, portid);
2504 qconf = &lcore_conf[lcore_id];
2505 qconf->tx_queue_id[portid] = queueid;
2511 for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
2512 if (rte_lcore_is_enabled(lcore_id) == 0)
2514 qconf = &lcore_conf[lcore_id];
2515 printf("\nInitializing rx queues on lcore %u ... ", lcore_id );
2517 /* init RX queues */
2518 for(queue = 0; queue < qconf->n_rx_queue; ++queue) {
2519 portid = qconf->rx_queue_list[queue].port_id;
2520 queueid = qconf->rx_queue_list[queue].queue_id;
2523 socketid = (uint8_t)rte_lcore_to_socket_id(lcore_id);
2527 printf("rxq=%d,%d,%d ", portid, queueid, socketid);
2530 ret = rte_eth_rx_queue_setup(portid, queueid, nb_rxd,
2533 pktmbuf_pool[socketid]);
2535 rte_exit(EXIT_FAILURE, "rte_eth_rx_queue_setup: err=%d,"
2536 "port=%d\n", ret, portid);
2543 for (portid = 0; portid < nb_ports; portid++) {
2544 if ((enabled_port_mask & (1 << portid)) == 0) {
2548 ret = rte_eth_dev_start(portid);
2550 rte_exit(EXIT_FAILURE, "rte_eth_dev_start: err=%d, port=%d\n",
2554 * If enabled, put device in promiscuous mode.
2555 * This allows IO forwarding mode to forward packets
2556 * to itself through 2 cross-connected ports of the
2560 rte_eth_promiscuous_enable(portid);
2563 check_all_ports_link_status((uint8_t)nb_ports, enabled_port_mask);
2565 /* launch per-lcore init on every lcore */
2566 rte_eal_mp_remote_launch(main_loop, NULL, CALL_MASTER);
2567 RTE_LCORE_FOREACH_SLAVE(lcore_id) {
2568 if (rte_eal_wait_lcore(lcore_id) < 0)