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 <tmmintrin.h>
46 #include <rte_common.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>
78 #define APP_LOOKUP_EXACT_MATCH 0
79 #define APP_LOOKUP_LPM 1
80 #define DO_RFC_1812_CHECKS
82 #ifndef APP_LOOKUP_METHOD
83 #define APP_LOOKUP_METHOD APP_LOOKUP_LPM
86 #define ENABLE_MULTI_BUFFER_OPTIMIZE 1
88 #if (APP_LOOKUP_METHOD == APP_LOOKUP_EXACT_MATCH)
90 #elif (APP_LOOKUP_METHOD == APP_LOOKUP_LPM)
94 #error "APP_LOOKUP_METHOD set to incorrect value"
98 #define IPv6_BYTES_FMT "%02x%02x:%02x%02x:%02x%02x:%02x%02x:"\
99 "%02x%02x:%02x%02x:%02x%02x:%02x%02x"
100 #define IPv6_BYTES(addr) \
101 addr[0], addr[1], addr[2], addr[3], \
102 addr[4], addr[5], addr[6], addr[7], \
103 addr[8], addr[9], addr[10], addr[11],\
104 addr[12], addr[13],addr[14], addr[15]
108 #define RTE_LOGTYPE_L3FWD RTE_LOGTYPE_USER1
110 #define MAX_JUMBO_PKT_LEN 9600
112 #define IPV6_ADDR_LEN 16
114 #define MEMPOOL_CACHE_SIZE 256
116 #define MBUF_SIZE (2048 + sizeof(struct rte_mbuf) + RTE_PKTMBUF_HEADROOM)
119 * This expression is used to calculate the number of mbufs needed depending on user input, taking
120 * into account memory for rx and tx hardware rings, cache per lcore and mtable per port per lcore.
121 * RTE_MAX is used to ensure that NB_MBUF never goes below a minimum value of 8192
124 #define NB_MBUF RTE_MAX ( \
125 (nb_ports*nb_rx_queue*RTE_TEST_RX_DESC_DEFAULT + \
126 nb_ports*nb_lcores*MAX_PKT_BURST + \
127 nb_ports*n_tx_queue*RTE_TEST_TX_DESC_DEFAULT + \
128 nb_lcores*MEMPOOL_CACHE_SIZE), \
132 * RX and TX Prefetch, Host, and Write-back threshold values should be
133 * carefully set for optimal performance. Consult the network
134 * controller's datasheet and supporting DPDK documentation for guidance
135 * on how these parameters should be set.
137 #define RX_PTHRESH 8 /**< Default values of RX prefetch threshold reg. */
138 #define RX_HTHRESH 8 /**< Default values of RX host threshold reg. */
139 #define RX_WTHRESH 4 /**< Default values of RX write-back threshold reg. */
142 * These default values are optimized for use with the Intel(R) 82599 10 GbE
143 * Controller and the DPDK ixgbe PMD. Consider using other values for other
144 * network controllers and/or network drivers.
146 #define TX_PTHRESH 36 /**< Default values of TX prefetch threshold reg. */
147 #define TX_HTHRESH 0 /**< Default values of TX host threshold reg. */
148 #define TX_WTHRESH 0 /**< Default values of TX write-back threshold reg. */
150 #define MAX_PKT_BURST 32
151 #define BURST_TX_DRAIN_US 100 /* TX drain every ~100us */
155 /* Configure how many packets ahead to prefetch, when reading packets */
156 #define PREFETCH_OFFSET 3
159 * Configurable number of RX/TX ring descriptors
161 #define RTE_TEST_RX_DESC_DEFAULT 128
162 #define RTE_TEST_TX_DESC_DEFAULT 512
163 static uint16_t nb_rxd = RTE_TEST_RX_DESC_DEFAULT;
164 static uint16_t nb_txd = RTE_TEST_TX_DESC_DEFAULT;
166 /* ethernet addresses of ports */
167 static struct ether_addr ports_eth_addr[RTE_MAX_ETHPORTS];
169 /* mask of enabled ports */
170 static uint32_t enabled_port_mask = 0;
171 static int promiscuous_on = 0; /**< Ports set in promiscuous mode off by default. */
172 static int numa_on = 1; /**< NUMA is enabled by default. */
174 #if (APP_LOOKUP_METHOD == APP_LOOKUP_EXACT_MATCH)
175 static int ipv6 = 0; /**< ipv6 is false by default. */
180 struct rte_mbuf *m_table[MAX_PKT_BURST];
183 struct lcore_rx_queue {
186 } __rte_cache_aligned;
188 #define MAX_RX_QUEUE_PER_LCORE 16
189 #define MAX_TX_QUEUE_PER_PORT RTE_MAX_ETHPORTS
190 #define MAX_RX_QUEUE_PER_PORT 128
192 #define MAX_LCORE_PARAMS 1024
193 struct lcore_params {
197 } __rte_cache_aligned;
199 static struct lcore_params lcore_params_array[MAX_LCORE_PARAMS];
200 static struct lcore_params lcore_params_array_default[] = {
212 static struct lcore_params * lcore_params = lcore_params_array_default;
213 static uint16_t nb_lcore_params = sizeof(lcore_params_array_default) /
214 sizeof(lcore_params_array_default[0]);
216 static struct rte_eth_conf port_conf = {
218 .max_rx_pkt_len = ETHER_MAX_LEN,
220 .header_split = 0, /**< Header Split disabled */
221 .hw_ip_checksum = 1, /**< IP checksum offload enabled */
222 .hw_vlan_filter = 0, /**< VLAN filtering disabled */
223 .jumbo_frame = 0, /**< Jumbo Frame Support disabled */
224 .hw_strip_crc = 0, /**< CRC stripped by hardware */
229 .rss_hf = ETH_RSS_IPV4 | ETH_RSS_IPV6,
233 .mq_mode = ETH_MQ_TX_NONE,
237 static const struct rte_eth_rxconf rx_conf = {
239 .pthresh = RX_PTHRESH,
240 .hthresh = RX_HTHRESH,
241 .wthresh = RX_WTHRESH,
243 .rx_free_thresh = 32,
246 static struct rte_eth_txconf tx_conf = {
248 .pthresh = TX_PTHRESH,
249 .hthresh = TX_HTHRESH,
250 .wthresh = TX_WTHRESH,
252 .tx_free_thresh = 0, /* Use PMD default values */
253 .tx_rs_thresh = 0, /* Use PMD default values */
254 .txq_flags = (ETH_TXQ_FLAGS_NOMULTSEGS |
255 ETH_TXQ_FLAGS_NOVLANOFFL |
256 ETH_TXQ_FLAGS_NOXSUMSCTP |
257 ETH_TXQ_FLAGS_NOXSUMUDP |
258 ETH_TXQ_FLAGS_NOXSUMTCP)
262 static struct rte_mempool * pktmbuf_pool[NB_SOCKETS];
264 #if (APP_LOOKUP_METHOD == APP_LOOKUP_EXACT_MATCH)
266 #ifdef RTE_MACHINE_CPUFLAG_SSE4_2
267 #include <rte_hash_crc.h>
268 #define DEFAULT_HASH_FUNC rte_hash_crc
270 #include <rte_jhash.h>
271 #define DEFAULT_HASH_FUNC rte_jhash
280 } __attribute__((__packed__));
282 union ipv4_5tuple_host {
295 #define XMM_NUM_IN_IPV6_5TUPLE 3
298 uint8_t ip_dst[IPV6_ADDR_LEN];
299 uint8_t ip_src[IPV6_ADDR_LEN];
303 } __attribute__((__packed__));
305 union ipv6_5tuple_host {
310 uint8_t ip_src[IPV6_ADDR_LEN];
311 uint8_t ip_dst[IPV6_ADDR_LEN];
316 __m128i xmm[XMM_NUM_IN_IPV6_5TUPLE];
319 struct ipv4_l3fwd_route {
320 struct ipv4_5tuple key;
324 struct ipv6_l3fwd_route {
325 struct ipv6_5tuple key;
329 static struct ipv4_l3fwd_route ipv4_l3fwd_route_array[] = {
330 {{IPv4(101,0,0,0), IPv4(100,10,0,1), 101, 11, IPPROTO_TCP}, 0},
331 {{IPv4(201,0,0,0), IPv4(200,20,0,1), 102, 12, IPPROTO_TCP}, 1},
332 {{IPv4(111,0,0,0), IPv4(100,30,0,1), 101, 11, IPPROTO_TCP}, 2},
333 {{IPv4(211,0,0,0), IPv4(200,40,0,1), 102, 12, IPPROTO_TCP}, 3},
336 static struct ipv6_l3fwd_route ipv6_l3fwd_route_array[] = {
338 {0xfe, 0x80, 0, 0, 0, 0, 0, 0, 0x02, 0x1e, 0x67, 0xff, 0xfe, 0, 0, 0},
339 {0xfe, 0x80, 0, 0, 0, 0, 0, 0, 0x02, 0x1b, 0x21, 0xff, 0xfe, 0x91, 0x38, 0x05},
340 101, 11, IPPROTO_TCP}, 0},
343 {0xfe, 0x90, 0, 0, 0, 0, 0, 0, 0x02, 0x1e, 0x67, 0xff, 0xfe, 0, 0, 0},
344 {0xfe, 0x90, 0, 0, 0, 0, 0, 0, 0x02, 0x1b, 0x21, 0xff, 0xfe, 0x91, 0x38, 0x05},
345 102, 12, IPPROTO_TCP}, 1},
348 {0xfe, 0xa0, 0, 0, 0, 0, 0, 0, 0x02, 0x1e, 0x67, 0xff, 0xfe, 0, 0, 0},
349 {0xfe, 0xa0, 0, 0, 0, 0, 0, 0, 0x02, 0x1b, 0x21, 0xff, 0xfe, 0x91, 0x38, 0x05},
350 101, 11, IPPROTO_TCP}, 2},
353 {0xfe, 0xb0, 0, 0, 0, 0, 0, 0, 0x02, 0x1e, 0x67, 0xff, 0xfe, 0, 0, 0},
354 {0xfe, 0xb0, 0, 0, 0, 0, 0, 0, 0x02, 0x1b, 0x21, 0xff, 0xfe, 0x91, 0x38, 0x05},
355 102, 12, IPPROTO_TCP}, 3},
358 typedef struct rte_hash lookup_struct_t;
359 static lookup_struct_t *ipv4_l3fwd_lookup_struct[NB_SOCKETS];
360 static lookup_struct_t *ipv6_l3fwd_lookup_struct[NB_SOCKETS];
362 #ifdef RTE_ARCH_X86_64
363 /* default to 4 million hash entries (approx) */
364 #define L3FWD_HASH_ENTRIES 1024*1024*4
366 /* 32-bit has less address-space for hugepage memory, limit to 1M entries */
367 #define L3FWD_HASH_ENTRIES 1024*1024*1
369 #define HASH_ENTRY_NUMBER_DEFAULT 4
371 static uint32_t hash_entry_number = HASH_ENTRY_NUMBER_DEFAULT;
373 static inline uint32_t
374 ipv4_hash_crc(const void *data, __rte_unused uint32_t data_len,
377 const union ipv4_5tuple_host *k;
383 p = (const uint32_t *)&k->port_src;
385 #ifdef RTE_MACHINE_CPUFLAG_SSE4_2
386 init_val = rte_hash_crc_4byte(t, init_val);
387 init_val = rte_hash_crc_4byte(k->ip_src, init_val);
388 init_val = rte_hash_crc_4byte(k->ip_dst, init_val);
389 init_val = rte_hash_crc_4byte(*p, init_val);
390 #else /* RTE_MACHINE_CPUFLAG_SSE4_2 */
391 init_val = rte_jhash_1word(t, init_val);
392 init_val = rte_jhash_1word(k->ip_src, init_val);
393 init_val = rte_jhash_1word(k->ip_dst, init_val);
394 init_val = rte_jhash_1word(*p, init_val);
395 #endif /* RTE_MACHINE_CPUFLAG_SSE4_2 */
399 static inline uint32_t
400 ipv6_hash_crc(const void *data, __rte_unused uint32_t data_len, uint32_t init_val)
402 const union ipv6_5tuple_host *k;
405 #ifdef RTE_MACHINE_CPUFLAG_SSE4_2
406 const uint32_t *ip_src0, *ip_src1, *ip_src2, *ip_src3;
407 const uint32_t *ip_dst0, *ip_dst1, *ip_dst2, *ip_dst3;
408 #endif /* RTE_MACHINE_CPUFLAG_SSE4_2 */
412 p = (const uint32_t *)&k->port_src;
414 #ifdef RTE_MACHINE_CPUFLAG_SSE4_2
415 ip_src0 = (const uint32_t *) k->ip_src;
416 ip_src1 = (const uint32_t *)(k->ip_src+4);
417 ip_src2 = (const uint32_t *)(k->ip_src+8);
418 ip_src3 = (const uint32_t *)(k->ip_src+12);
419 ip_dst0 = (const uint32_t *) k->ip_dst;
420 ip_dst1 = (const uint32_t *)(k->ip_dst+4);
421 ip_dst2 = (const uint32_t *)(k->ip_dst+8);
422 ip_dst3 = (const uint32_t *)(k->ip_dst+12);
423 init_val = rte_hash_crc_4byte(t, init_val);
424 init_val = rte_hash_crc_4byte(*ip_src0, init_val);
425 init_val = rte_hash_crc_4byte(*ip_src1, init_val);
426 init_val = rte_hash_crc_4byte(*ip_src2, init_val);
427 init_val = rte_hash_crc_4byte(*ip_src3, init_val);
428 init_val = rte_hash_crc_4byte(*ip_dst0, init_val);
429 init_val = rte_hash_crc_4byte(*ip_dst1, init_val);
430 init_val = rte_hash_crc_4byte(*ip_dst2, init_val);
431 init_val = rte_hash_crc_4byte(*ip_dst3, init_val);
432 init_val = rte_hash_crc_4byte(*p, init_val);
433 #else /* RTE_MACHINE_CPUFLAG_SSE4_2 */
434 init_val = rte_jhash_1word(t, init_val);
435 init_val = rte_jhash(k->ip_src, sizeof(uint8_t) * IPV6_ADDR_LEN, init_val);
436 init_val = rte_jhash(k->ip_dst, sizeof(uint8_t) * IPV6_ADDR_LEN, init_val);
437 init_val = rte_jhash_1word(*p, init_val);
438 #endif /* RTE_MACHINE_CPUFLAG_SSE4_2 */
442 #define IPV4_L3FWD_NUM_ROUTES \
443 (sizeof(ipv4_l3fwd_route_array) / sizeof(ipv4_l3fwd_route_array[0]))
445 #define IPV6_L3FWD_NUM_ROUTES \
446 (sizeof(ipv6_l3fwd_route_array) / sizeof(ipv6_l3fwd_route_array[0]))
448 static uint8_t ipv4_l3fwd_out_if[L3FWD_HASH_ENTRIES] __rte_cache_aligned;
449 static uint8_t ipv6_l3fwd_out_if[L3FWD_HASH_ENTRIES] __rte_cache_aligned;
453 #if (APP_LOOKUP_METHOD == APP_LOOKUP_LPM)
454 struct ipv4_l3fwd_route {
460 struct ipv6_l3fwd_route {
466 static struct ipv4_l3fwd_route ipv4_l3fwd_route_array[] = {
467 {IPv4(1,1,1,0), 24, 0},
468 {IPv4(2,1,1,0), 24, 1},
469 {IPv4(3,1,1,0), 24, 2},
470 {IPv4(4,1,1,0), 24, 3},
471 {IPv4(5,1,1,0), 24, 4},
472 {IPv4(6,1,1,0), 24, 5},
473 {IPv4(7,1,1,0), 24, 6},
474 {IPv4(8,1,1,0), 24, 7},
477 static struct ipv6_l3fwd_route ipv6_l3fwd_route_array[] = {
478 {{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}, 48, 0},
479 {{2,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}, 48, 1},
480 {{3,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}, 48, 2},
481 {{4,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}, 48, 3},
482 {{5,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}, 48, 4},
483 {{6,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}, 48, 5},
484 {{7,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}, 48, 6},
485 {{8,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}, 48, 7},
488 #define IPV4_L3FWD_NUM_ROUTES \
489 (sizeof(ipv4_l3fwd_route_array) / sizeof(ipv4_l3fwd_route_array[0]))
490 #define IPV6_L3FWD_NUM_ROUTES \
491 (sizeof(ipv6_l3fwd_route_array) / sizeof(ipv6_l3fwd_route_array[0]))
493 #define IPV4_L3FWD_LPM_MAX_RULES 1024
494 #define IPV6_L3FWD_LPM_MAX_RULES 1024
495 #define IPV6_L3FWD_LPM_NUMBER_TBL8S (1 << 16)
497 typedef struct rte_lpm lookup_struct_t;
498 typedef struct rte_lpm6 lookup6_struct_t;
499 static lookup_struct_t *ipv4_l3fwd_lookup_struct[NB_SOCKETS];
500 static lookup6_struct_t *ipv6_l3fwd_lookup_struct[NB_SOCKETS];
505 struct lcore_rx_queue rx_queue_list[MAX_RX_QUEUE_PER_LCORE];
506 uint16_t tx_queue_id[RTE_MAX_ETHPORTS];
507 struct mbuf_table tx_mbufs[RTE_MAX_ETHPORTS];
508 lookup_struct_t * ipv4_lookup_struct;
509 #if (APP_LOOKUP_METHOD == APP_LOOKUP_LPM)
510 lookup6_struct_t * ipv6_lookup_struct;
512 lookup_struct_t * ipv6_lookup_struct;
514 } __rte_cache_aligned;
516 static struct lcore_conf lcore_conf[RTE_MAX_LCORE];
518 /* Send burst of packets on an output interface */
520 send_burst(struct lcore_conf *qconf, uint16_t n, uint8_t port)
522 struct rte_mbuf **m_table;
526 queueid = qconf->tx_queue_id[port];
527 m_table = (struct rte_mbuf **)qconf->tx_mbufs[port].m_table;
529 ret = rte_eth_tx_burst(port, queueid, m_table, n);
530 if (unlikely(ret < n)) {
532 rte_pktmbuf_free(m_table[ret]);
539 /* Enqueue a single packet, and send burst if queue is filled */
541 send_single_packet(struct rte_mbuf *m, uint8_t port)
545 struct lcore_conf *qconf;
547 lcore_id = rte_lcore_id();
549 qconf = &lcore_conf[lcore_id];
550 len = qconf->tx_mbufs[port].len;
551 qconf->tx_mbufs[port].m_table[len] = m;
554 /* enough pkts to be sent */
555 if (unlikely(len == MAX_PKT_BURST)) {
556 send_burst(qconf, MAX_PKT_BURST, port);
560 qconf->tx_mbufs[port].len = len;
564 #ifdef DO_RFC_1812_CHECKS
566 is_valid_ipv4_pkt(struct ipv4_hdr *pkt, uint32_t link_len)
568 /* From http://www.rfc-editor.org/rfc/rfc1812.txt section 5.2.2 */
570 * 1. The packet length reported by the Link Layer must be large
571 * enough to hold the minimum length legal IP datagram (20 bytes).
573 if (link_len < sizeof(struct ipv4_hdr))
576 /* 2. The IP checksum must be correct. */
577 /* this is checked in H/W */
580 * 3. The IP version number must be 4. If the version number is not 4
581 * then the packet may be another version of IP, such as IPng or
584 if (((pkt->version_ihl) >> 4) != 4)
587 * 4. The IP header length field must be large enough to hold the
588 * minimum length legal IP datagram (20 bytes = 5 words).
590 if ((pkt->version_ihl & 0xf) < 5)
594 * 5. The IP total length field must be large enough to hold the IP
595 * datagram header, whose length is specified in the IP header length
598 if (rte_cpu_to_be_16(pkt->total_length) < sizeof(struct ipv4_hdr))
605 #if (APP_LOOKUP_METHOD == APP_LOOKUP_EXACT_MATCH)
607 static __m128i mask0;
608 static __m128i mask1;
609 static __m128i mask2;
610 static inline uint8_t
611 get_ipv4_dst_port(void *ipv4_hdr, uint8_t portid, lookup_struct_t * ipv4_l3fwd_lookup_struct)
614 union ipv4_5tuple_host key;
616 ipv4_hdr = (uint8_t *)ipv4_hdr + offsetof(struct ipv4_hdr, time_to_live);
617 __m128i data = _mm_loadu_si128((__m128i*)(ipv4_hdr));
618 /* Get 5 tuple: dst port, src port, dst IP address, src IP address and protocol */
619 key.xmm = _mm_and_si128(data, mask0);
620 /* Find destination port */
621 ret = rte_hash_lookup(ipv4_l3fwd_lookup_struct, (const void *)&key);
622 return (uint8_t)((ret < 0)? portid : ipv4_l3fwd_out_if[ret]);
625 static inline uint8_t
626 get_ipv6_dst_port(void *ipv6_hdr, uint8_t portid, lookup_struct_t * ipv6_l3fwd_lookup_struct)
629 union ipv6_5tuple_host key;
631 ipv6_hdr = (uint8_t *)ipv6_hdr + offsetof(struct ipv6_hdr, payload_len);
632 __m128i data0 = _mm_loadu_si128((__m128i*)(ipv6_hdr));
633 __m128i data1 = _mm_loadu_si128((__m128i*)(((uint8_t*)ipv6_hdr)+sizeof(__m128i)));
634 __m128i data2 = _mm_loadu_si128((__m128i*)(((uint8_t*)ipv6_hdr)+sizeof(__m128i)+sizeof(__m128i)));
635 /* Get part of 5 tuple: src IP address lower 96 bits and protocol */
636 key.xmm[0] = _mm_and_si128(data0, mask1);
637 /* Get part of 5 tuple: dst IP address lower 96 bits and src IP address higher 32 bits */
639 /* Get part of 5 tuple: dst port and src port and dst IP address higher 32 bits */
640 key.xmm[2] = _mm_and_si128(data2, mask2);
642 /* Find destination port */
643 ret = rte_hash_lookup(ipv6_l3fwd_lookup_struct, (const void *)&key);
644 return (uint8_t)((ret < 0)? portid : ipv6_l3fwd_out_if[ret]);
648 #if (APP_LOOKUP_METHOD == APP_LOOKUP_LPM)
649 static inline uint8_t
650 get_ipv4_dst_port(void *ipv4_hdr, uint8_t portid, lookup_struct_t * ipv4_l3fwd_lookup_struct)
654 return (uint8_t) ((rte_lpm_lookup(ipv4_l3fwd_lookup_struct,
655 rte_be_to_cpu_32(((struct ipv4_hdr*)ipv4_hdr)->dst_addr), &next_hop) == 0)?
659 static inline uint8_t
660 get_ipv6_dst_port(void *ipv6_hdr, uint8_t portid, lookup6_struct_t * ipv6_l3fwd_lookup_struct)
663 return (uint8_t) ((rte_lpm6_lookup(ipv6_l3fwd_lookup_struct,
664 ((struct ipv6_hdr*)ipv6_hdr)->dst_addr, &next_hop) == 0)?
669 #if (APP_LOOKUP_METHOD == APP_LOOKUP_EXACT_MATCH) & (ENABLE_MULTI_BUFFER_OPTIMIZE == 1)
670 static inline void l3fwd_simple_forward(struct rte_mbuf *m, uint8_t portid, struct lcore_conf *qconf);
672 #define MASK_ALL_PKTS 0xf
673 #define EXECLUDE_1ST_PKT 0xe
674 #define EXECLUDE_2ND_PKT 0xd
675 #define EXECLUDE_3RD_PKT 0xb
676 #define EXECLUDE_4TH_PKT 0x7
679 simple_ipv4_fwd_4pkts(struct rte_mbuf* m[4], uint8_t portid, struct lcore_conf *qconf)
681 struct ether_hdr *eth_hdr[4];
682 struct ipv4_hdr *ipv4_hdr[4];
683 void *d_addr_bytes[4];
686 union ipv4_5tuple_host key[4];
689 eth_hdr[0] = rte_pktmbuf_mtod(m[0], struct ether_hdr *);
690 eth_hdr[1] = rte_pktmbuf_mtod(m[1], struct ether_hdr *);
691 eth_hdr[2] = rte_pktmbuf_mtod(m[2], struct ether_hdr *);
692 eth_hdr[3] = rte_pktmbuf_mtod(m[3], struct ether_hdr *);
694 /* Handle IPv4 headers.*/
695 ipv4_hdr[0] = (struct ipv4_hdr *)(rte_pktmbuf_mtod(m[0], unsigned char *) +
696 sizeof(struct ether_hdr));
697 ipv4_hdr[1] = (struct ipv4_hdr *)(rte_pktmbuf_mtod(m[1], unsigned char *) +
698 sizeof(struct ether_hdr));
699 ipv4_hdr[2] = (struct ipv4_hdr *)(rte_pktmbuf_mtod(m[2], unsigned char *) +
700 sizeof(struct ether_hdr));
701 ipv4_hdr[3] = (struct ipv4_hdr *)(rte_pktmbuf_mtod(m[3], unsigned char *) +
702 sizeof(struct ether_hdr));
704 #ifdef DO_RFC_1812_CHECKS
705 /* Check to make sure the packet is valid (RFC1812) */
706 uint8_t valid_mask = MASK_ALL_PKTS;
707 if (is_valid_ipv4_pkt(ipv4_hdr[0], m[0]->pkt.pkt_len) < 0) {
708 rte_pktmbuf_free(m[0]);
709 valid_mask &= EXECLUDE_1ST_PKT;
711 if (is_valid_ipv4_pkt(ipv4_hdr[1], m[1]->pkt.pkt_len) < 0) {
712 rte_pktmbuf_free(m[1]);
713 valid_mask &= EXECLUDE_2ND_PKT;
715 if (is_valid_ipv4_pkt(ipv4_hdr[2], m[2]->pkt.pkt_len) < 0) {
716 rte_pktmbuf_free(m[2]);
717 valid_mask &= EXECLUDE_3RD_PKT;
719 if (is_valid_ipv4_pkt(ipv4_hdr[3], m[3]->pkt.pkt_len) < 0) {
720 rte_pktmbuf_free(m[3]);
721 valid_mask &= EXECLUDE_4TH_PKT;
723 if (unlikely(valid_mask != MASK_ALL_PKTS)) {
724 if (valid_mask == 0){
728 for (i = 0; i < 4; i++) {
729 if ((0x1 << i) & valid_mask) {
730 l3fwd_simple_forward(m[i], portid, qconf);
736 #endif // End of #ifdef DO_RFC_1812_CHECKS
738 data[0] = _mm_loadu_si128((__m128i*)(rte_pktmbuf_mtod(m[0], unsigned char *) +
739 sizeof(struct ether_hdr) + offsetof(struct ipv4_hdr, time_to_live)));
740 data[1] = _mm_loadu_si128((__m128i*)(rte_pktmbuf_mtod(m[1], unsigned char *) +
741 sizeof(struct ether_hdr) + offsetof(struct ipv4_hdr, time_to_live)));
742 data[2] = _mm_loadu_si128((__m128i*)(rte_pktmbuf_mtod(m[2], unsigned char *) +
743 sizeof(struct ether_hdr) + offsetof(struct ipv4_hdr, time_to_live)));
744 data[3] = _mm_loadu_si128((__m128i*)(rte_pktmbuf_mtod(m[3], unsigned char *) +
745 sizeof(struct ether_hdr) + offsetof(struct ipv4_hdr, time_to_live)));
747 key[0].xmm = _mm_and_si128(data[0], mask0);
748 key[1].xmm = _mm_and_si128(data[1], mask0);
749 key[2].xmm = _mm_and_si128(data[2], mask0);
750 key[3].xmm = _mm_and_si128(data[3], mask0);
752 const void *key_array[4] = {&key[0], &key[1], &key[2],&key[3]};
753 rte_hash_lookup_multi(qconf->ipv4_lookup_struct, &key_array[0], 4, ret);
754 dst_port[0] = (uint8_t) ((ret[0] < 0) ? portid : ipv4_l3fwd_out_if[ret[0]]);
755 dst_port[1] = (uint8_t) ((ret[1] < 0) ? portid : ipv4_l3fwd_out_if[ret[1]]);
756 dst_port[2] = (uint8_t) ((ret[2] < 0) ? portid : ipv4_l3fwd_out_if[ret[2]]);
757 dst_port[3] = (uint8_t) ((ret[3] < 0) ? portid : ipv4_l3fwd_out_if[ret[3]]);
759 if (dst_port[0] >= RTE_MAX_ETHPORTS || (enabled_port_mask & 1 << dst_port[0]) == 0)
760 dst_port[0] = portid;
761 if (dst_port[1] >= RTE_MAX_ETHPORTS || (enabled_port_mask & 1 << dst_port[1]) == 0)
762 dst_port[1] = portid;
763 if (dst_port[2] >= RTE_MAX_ETHPORTS || (enabled_port_mask & 1 << dst_port[2]) == 0)
764 dst_port[2] = portid;
765 if (dst_port[3] >= RTE_MAX_ETHPORTS || (enabled_port_mask & 1 << dst_port[3]) == 0)
766 dst_port[3] = portid;
768 /* 02:00:00:00:00:xx */
769 d_addr_bytes[0] = ð_hdr[0]->d_addr.addr_bytes[0];
770 d_addr_bytes[1] = ð_hdr[1]->d_addr.addr_bytes[0];
771 d_addr_bytes[2] = ð_hdr[2]->d_addr.addr_bytes[0];
772 d_addr_bytes[3] = ð_hdr[3]->d_addr.addr_bytes[0];
773 *((uint64_t *)d_addr_bytes[0]) = 0x000000000002 + ((uint64_t)dst_port[0] << 40);
774 *((uint64_t *)d_addr_bytes[1]) = 0x000000000002 + ((uint64_t)dst_port[1] << 40);
775 *((uint64_t *)d_addr_bytes[2]) = 0x000000000002 + ((uint64_t)dst_port[2] << 40);
776 *((uint64_t *)d_addr_bytes[3]) = 0x000000000002 + ((uint64_t)dst_port[3] << 40);
778 #ifdef DO_RFC_1812_CHECKS
779 /* Update time to live and header checksum */
780 --(ipv4_hdr[0]->time_to_live);
781 --(ipv4_hdr[1]->time_to_live);
782 --(ipv4_hdr[2]->time_to_live);
783 --(ipv4_hdr[3]->time_to_live);
784 ++(ipv4_hdr[0]->hdr_checksum);
785 ++(ipv4_hdr[1]->hdr_checksum);
786 ++(ipv4_hdr[2]->hdr_checksum);
787 ++(ipv4_hdr[3]->hdr_checksum);
791 ether_addr_copy(&ports_eth_addr[dst_port[0]], ð_hdr[0]->s_addr);
792 ether_addr_copy(&ports_eth_addr[dst_port[1]], ð_hdr[1]->s_addr);
793 ether_addr_copy(&ports_eth_addr[dst_port[2]], ð_hdr[2]->s_addr);
794 ether_addr_copy(&ports_eth_addr[dst_port[3]], ð_hdr[3]->s_addr);
796 send_single_packet(m[0], (uint8_t)dst_port[0]);
797 send_single_packet(m[1], (uint8_t)dst_port[1]);
798 send_single_packet(m[2], (uint8_t)dst_port[2]);
799 send_single_packet(m[3], (uint8_t)dst_port[3]);
803 static inline void get_ipv6_5tuple(struct rte_mbuf* m0, __m128i mask0, __m128i mask1,
804 union ipv6_5tuple_host * key)
806 __m128i tmpdata0 = _mm_loadu_si128((__m128i*)(rte_pktmbuf_mtod(m0, unsigned char *)
807 + sizeof(struct ether_hdr) + offsetof(struct ipv6_hdr, payload_len)));
808 __m128i tmpdata1 = _mm_loadu_si128((__m128i*)(rte_pktmbuf_mtod(m0, unsigned char *)
809 + sizeof(struct ether_hdr) + offsetof(struct ipv6_hdr, payload_len)
811 __m128i tmpdata2 = _mm_loadu_si128((__m128i*)(rte_pktmbuf_mtod(m0, unsigned char *)
812 + sizeof(struct ether_hdr) + offsetof(struct ipv6_hdr, payload_len)
813 + sizeof(__m128i) + sizeof(__m128i)));
814 key->xmm[0] = _mm_and_si128(tmpdata0, mask0);
815 key->xmm[1] = tmpdata1;
816 key->xmm[2] = _mm_and_si128(tmpdata2, mask1);
821 simple_ipv6_fwd_4pkts(struct rte_mbuf* m[4], uint8_t portid, struct lcore_conf *qconf)
823 struct ether_hdr *eth_hdr[4];
824 __attribute__((unused)) struct ipv6_hdr *ipv6_hdr[4];
825 void *d_addr_bytes[4];
828 union ipv6_5tuple_host key[4];
830 eth_hdr[0] = rte_pktmbuf_mtod(m[0], struct ether_hdr *);
831 eth_hdr[1] = rte_pktmbuf_mtod(m[1], struct ether_hdr *);
832 eth_hdr[2] = rte_pktmbuf_mtod(m[2], struct ether_hdr *);
833 eth_hdr[3] = rte_pktmbuf_mtod(m[3], struct ether_hdr *);
835 /* Handle IPv6 headers.*/
836 ipv6_hdr[0] = (struct ipv6_hdr *)(rte_pktmbuf_mtod(m[0], unsigned char *) +
837 sizeof(struct ether_hdr));
838 ipv6_hdr[1] = (struct ipv6_hdr *)(rte_pktmbuf_mtod(m[1], unsigned char *) +
839 sizeof(struct ether_hdr));
840 ipv6_hdr[2] = (struct ipv6_hdr *)(rte_pktmbuf_mtod(m[2], unsigned char *) +
841 sizeof(struct ether_hdr));
842 ipv6_hdr[3] = (struct ipv6_hdr *)(rte_pktmbuf_mtod(m[3], unsigned char *) +
843 sizeof(struct ether_hdr));
845 get_ipv6_5tuple(m[0], mask1, mask2, &key[0]);
846 get_ipv6_5tuple(m[1], mask1, mask2, &key[1]);
847 get_ipv6_5tuple(m[2], mask1, mask2, &key[2]);
848 get_ipv6_5tuple(m[3], mask1, mask2, &key[3]);
850 const void *key_array[4] = {&key[0], &key[1], &key[2],&key[3]};
851 rte_hash_lookup_multi(qconf->ipv6_lookup_struct, &key_array[0], 4, ret);
852 dst_port[0] = (uint8_t) ((ret[0] < 0)? portid:ipv6_l3fwd_out_if[ret[0]]);
853 dst_port[1] = (uint8_t) ((ret[1] < 0)? portid:ipv6_l3fwd_out_if[ret[1]]);
854 dst_port[2] = (uint8_t) ((ret[2] < 0)? portid:ipv6_l3fwd_out_if[ret[2]]);
855 dst_port[3] = (uint8_t) ((ret[3] < 0)? portid:ipv6_l3fwd_out_if[ret[3]]);
857 if (dst_port[0] >= RTE_MAX_ETHPORTS || (enabled_port_mask & 1 << dst_port[0]) == 0)
858 dst_port[0] = portid;
859 if (dst_port[1] >= RTE_MAX_ETHPORTS || (enabled_port_mask & 1 << dst_port[1]) == 0)
860 dst_port[1] = portid;
861 if (dst_port[2] >= RTE_MAX_ETHPORTS || (enabled_port_mask & 1 << dst_port[2]) == 0)
862 dst_port[2] = portid;
863 if (dst_port[3] >= RTE_MAX_ETHPORTS || (enabled_port_mask & 1 << dst_port[3]) == 0)
864 dst_port[3] = portid;
866 /* 02:00:00:00:00:xx */
867 d_addr_bytes[0] = ð_hdr[0]->d_addr.addr_bytes[0];
868 d_addr_bytes[1] = ð_hdr[1]->d_addr.addr_bytes[0];
869 d_addr_bytes[2] = ð_hdr[2]->d_addr.addr_bytes[0];
870 d_addr_bytes[3] = ð_hdr[3]->d_addr.addr_bytes[0];
871 *((uint64_t *)d_addr_bytes[0]) = 0x000000000002 + ((uint64_t)dst_port[0] << 40);
872 *((uint64_t *)d_addr_bytes[1]) = 0x000000000002 + ((uint64_t)dst_port[1] << 40);
873 *((uint64_t *)d_addr_bytes[2]) = 0x000000000002 + ((uint64_t)dst_port[2] << 40);
874 *((uint64_t *)d_addr_bytes[3]) = 0x000000000002 + ((uint64_t)dst_port[3] << 40);
877 ether_addr_copy(&ports_eth_addr[dst_port[0]], ð_hdr[0]->s_addr);
878 ether_addr_copy(&ports_eth_addr[dst_port[1]], ð_hdr[1]->s_addr);
879 ether_addr_copy(&ports_eth_addr[dst_port[2]], ð_hdr[2]->s_addr);
880 ether_addr_copy(&ports_eth_addr[dst_port[3]], ð_hdr[3]->s_addr);
882 send_single_packet(m[0], (uint8_t)dst_port[0]);
883 send_single_packet(m[1], (uint8_t)dst_port[1]);
884 send_single_packet(m[2], (uint8_t)dst_port[2]);
885 send_single_packet(m[3], (uint8_t)dst_port[3]);
888 #endif // End of #if(APP_LOOKUP_METHOD == APP_LOOKUP_EXACT_MATCH)&(ENABLE_MULTI_BUFFER_OPTIMIZE == 1)
890 static inline __attribute__((always_inline)) void
891 l3fwd_simple_forward(struct rte_mbuf *m, uint8_t portid, struct lcore_conf *qconf)
893 struct ether_hdr *eth_hdr;
894 struct ipv4_hdr *ipv4_hdr;
898 eth_hdr = rte_pktmbuf_mtod(m, struct ether_hdr *);
900 if (m->ol_flags & PKT_RX_IPV4_HDR) {
901 /* Handle IPv4 headers.*/
902 ipv4_hdr = (struct ipv4_hdr *)(rte_pktmbuf_mtod(m, unsigned char *) +
903 sizeof(struct ether_hdr));
905 #ifdef DO_RFC_1812_CHECKS
906 /* Check to make sure the packet is valid (RFC1812) */
907 if (is_valid_ipv4_pkt(ipv4_hdr, m->pkt.pkt_len) < 0) {
913 dst_port = get_ipv4_dst_port(ipv4_hdr, portid, qconf->ipv4_lookup_struct);
914 if (dst_port >= RTE_MAX_ETHPORTS || (enabled_port_mask & 1 << dst_port) == 0)
917 /* 02:00:00:00:00:xx */
918 d_addr_bytes = ð_hdr->d_addr.addr_bytes[0];
919 *((uint64_t *)d_addr_bytes) = 0x000000000002 + ((uint64_t)dst_port << 40);
921 #ifdef DO_RFC_1812_CHECKS
922 /* Update time to live and header checksum */
923 --(ipv4_hdr->time_to_live);
924 ++(ipv4_hdr->hdr_checksum);
928 ether_addr_copy(&ports_eth_addr[dst_port], ð_hdr->s_addr);
930 send_single_packet(m, dst_port);
933 /* Handle IPv6 headers.*/
934 struct ipv6_hdr *ipv6_hdr;
936 ipv6_hdr = (struct ipv6_hdr *)(rte_pktmbuf_mtod(m, unsigned char *) +
937 sizeof(struct ether_hdr));
939 dst_port = get_ipv6_dst_port(ipv6_hdr, portid, qconf->ipv6_lookup_struct);
941 if (dst_port >= RTE_MAX_ETHPORTS || (enabled_port_mask & 1 << dst_port) == 0)
944 /* 02:00:00:00:00:xx */
945 d_addr_bytes = ð_hdr->d_addr.addr_bytes[0];
946 *((uint64_t *)d_addr_bytes) = 0x000000000002 + ((uint64_t)dst_port << 40);
949 ether_addr_copy(&ports_eth_addr[dst_port], ð_hdr->s_addr);
951 send_single_packet(m, dst_port);
956 /* main processing loop */
958 main_loop(__attribute__((unused)) void *dummy)
960 struct rte_mbuf *pkts_burst[MAX_PKT_BURST];
962 uint64_t prev_tsc, diff_tsc, cur_tsc;
964 uint8_t portid, queueid;
965 struct lcore_conf *qconf;
966 const uint64_t drain_tsc = (rte_get_tsc_hz() + US_PER_S - 1) / US_PER_S * BURST_TX_DRAIN_US;
970 lcore_id = rte_lcore_id();
971 qconf = &lcore_conf[lcore_id];
973 if (qconf->n_rx_queue == 0) {
974 RTE_LOG(INFO, L3FWD, "lcore %u has nothing to do\n", lcore_id);
978 RTE_LOG(INFO, L3FWD, "entering main loop on lcore %u\n", lcore_id);
980 for (i = 0; i < qconf->n_rx_queue; i++) {
982 portid = qconf->rx_queue_list[i].port_id;
983 queueid = qconf->rx_queue_list[i].queue_id;
984 RTE_LOG(INFO, L3FWD, " -- lcoreid=%u portid=%hhu rxqueueid=%hhu\n", lcore_id,
990 cur_tsc = rte_rdtsc();
993 * TX burst queue drain
995 diff_tsc = cur_tsc - prev_tsc;
996 if (unlikely(diff_tsc > drain_tsc)) {
999 * This could be optimized (use queueid instead of
1000 * portid), but it is not called so often
1002 for (portid = 0; portid < RTE_MAX_ETHPORTS; portid++) {
1003 if (qconf->tx_mbufs[portid].len == 0)
1005 send_burst(&lcore_conf[lcore_id],
1006 qconf->tx_mbufs[portid].len,
1008 qconf->tx_mbufs[portid].len = 0;
1015 * Read packet from RX queues
1017 for (i = 0; i < qconf->n_rx_queue; ++i) {
1018 portid = qconf->rx_queue_list[i].port_id;
1019 queueid = qconf->rx_queue_list[i].queue_id;
1020 nb_rx = rte_eth_rx_burst(portid, queueid, pkts_burst, MAX_PKT_BURST);
1021 #if (APP_LOOKUP_METHOD == APP_LOOKUP_EXACT_MATCH) & (ENABLE_MULTI_BUFFER_OPTIMIZE == 1)
1023 /* Send nb_rx - nb_rx%4 packets in groups of 4.*/
1024 int32_t n = RTE_ALIGN_FLOOR(nb_rx, 4);
1025 for (j = 0; j < n ; j+=4) {
1026 uint32_t ol_flag = pkts_burst[j]->ol_flags
1027 & pkts_burst[j+1]->ol_flags
1028 & pkts_burst[j+2]->ol_flags
1029 & pkts_burst[j+3]->ol_flags;
1030 if (ol_flag & PKT_RX_IPV4_HDR ) {
1031 simple_ipv4_fwd_4pkts(&pkts_burst[j],
1033 } else if (ol_flag & PKT_RX_IPV6_HDR) {
1034 simple_ipv6_fwd_4pkts(&pkts_burst[j],
1037 l3fwd_simple_forward(pkts_burst[j],
1039 l3fwd_simple_forward(pkts_burst[j+1],
1041 l3fwd_simple_forward(pkts_burst[j+2],
1043 l3fwd_simple_forward(pkts_burst[j+3],
1047 for (; j < nb_rx ; j++) {
1048 l3fwd_simple_forward(pkts_burst[j],
1053 /* Prefetch first packets */
1054 for (j = 0; j < PREFETCH_OFFSET && j < nb_rx; j++) {
1055 rte_prefetch0(rte_pktmbuf_mtod(
1056 pkts_burst[j], void *));
1059 /* Prefetch and forward already prefetched packets */
1060 for (j = 0; j < (nb_rx - PREFETCH_OFFSET); j++) {
1061 rte_prefetch0(rte_pktmbuf_mtod(pkts_burst[
1062 j + PREFETCH_OFFSET], void *));
1063 l3fwd_simple_forward(pkts_burst[j], portid, qconf);
1066 /* Forward remaining prefetched packets */
1067 for (; j < nb_rx; j++) {
1068 l3fwd_simple_forward(pkts_burst[j], portid, qconf);
1070 #endif // End of #if((ENABLE_MULTI_BUFFER_OPTIMIZE == 1)&(APP_LOOKUP_METHOD == APP_LOOKUP_EXACT_MATCH))
1076 check_lcore_params(void)
1078 uint8_t queue, lcore;
1082 for (i = 0; i < nb_lcore_params; ++i) {
1083 queue = lcore_params[i].queue_id;
1084 if (queue >= MAX_RX_QUEUE_PER_PORT) {
1085 printf("invalid queue number: %hhu\n", queue);
1088 lcore = lcore_params[i].lcore_id;
1089 if (!rte_lcore_is_enabled(lcore)) {
1090 printf("error: lcore %hhu is not enabled in lcore mask\n", lcore);
1093 if ((socketid = rte_lcore_to_socket_id(lcore) != 0) &&
1095 printf("warning: lcore %hhu is on socket %d with numa off \n",
1103 check_port_config(const unsigned nb_ports)
1108 for (i = 0; i < nb_lcore_params; ++i) {
1109 portid = lcore_params[i].port_id;
1110 if ((enabled_port_mask & (1 << portid)) == 0) {
1111 printf("port %u is not enabled in port mask\n", portid);
1114 if (portid >= nb_ports) {
1115 printf("port %u is not present on the board\n", portid);
1123 get_port_n_rx_queues(const uint8_t port)
1128 for (i = 0; i < nb_lcore_params; ++i) {
1129 if (lcore_params[i].port_id == port && lcore_params[i].queue_id > queue)
1130 queue = lcore_params[i].queue_id;
1132 return (uint8_t)(++queue);
1136 init_lcore_rx_queues(void)
1138 uint16_t i, nb_rx_queue;
1141 for (i = 0; i < nb_lcore_params; ++i) {
1142 lcore = lcore_params[i].lcore_id;
1143 nb_rx_queue = lcore_conf[lcore].n_rx_queue;
1144 if (nb_rx_queue >= MAX_RX_QUEUE_PER_LCORE) {
1145 printf("error: too many queues (%u) for lcore: %u\n",
1146 (unsigned)nb_rx_queue + 1, (unsigned)lcore);
1149 lcore_conf[lcore].rx_queue_list[nb_rx_queue].port_id =
1150 lcore_params[i].port_id;
1151 lcore_conf[lcore].rx_queue_list[nb_rx_queue].queue_id =
1152 lcore_params[i].queue_id;
1153 lcore_conf[lcore].n_rx_queue++;
1161 print_usage(const char *prgname)
1163 printf ("%s [EAL options] -- -p PORTMASK -P"
1164 " [--config (port,queue,lcore)[,(port,queue,lcore]]"
1165 " [--enable-jumbo [--max-pkt-len PKTLEN]]\n"
1166 " -p PORTMASK: hexadecimal bitmask of ports to configure\n"
1167 " -P : enable promiscuous mode\n"
1168 " --config (port,queue,lcore): rx queues configuration\n"
1169 " --no-numa: optional, disable numa awareness\n"
1170 " --ipv6: optional, specify it if running ipv6 packets\n"
1171 " --enable-jumbo: enable jumbo frame"
1172 " which max packet len is PKTLEN in decimal (64-9600)\n"
1173 " --hash-entry-num: specify the hash entry number in hexadecimal to be setup\n",
1177 static int parse_max_pkt_len(const char *pktlen)
1182 /* parse decimal string */
1183 len = strtoul(pktlen, &end, 10);
1184 if ((pktlen[0] == '\0') || (end == NULL) || (*end != '\0'))
1194 parse_portmask(const char *portmask)
1199 /* parse hexadecimal string */
1200 pm = strtoul(portmask, &end, 16);
1201 if ((portmask[0] == '\0') || (end == NULL) || (*end != '\0'))
1210 #if (APP_LOOKUP_METHOD == APP_LOOKUP_EXACT_MATCH)
1212 parse_hash_entry_number(const char *hash_entry_num)
1215 unsigned long hash_en;
1216 /* parse hexadecimal string */
1217 hash_en = strtoul(hash_entry_num, &end, 16);
1218 if ((hash_entry_num[0] == '\0') || (end == NULL) || (*end != '\0'))
1229 parse_config(const char *q_arg)
1232 const char *p, *p0 = q_arg;
1240 unsigned long int_fld[_NUM_FLD];
1241 char *str_fld[_NUM_FLD];
1245 nb_lcore_params = 0;
1247 while ((p = strchr(p0,'(')) != NULL) {
1249 if((p0 = strchr(p,')')) == NULL)
1253 if(size >= sizeof(s))
1256 rte_snprintf(s, sizeof(s), "%.*s", size, p);
1257 if (rte_strsplit(s, sizeof(s), str_fld, _NUM_FLD, ',') != _NUM_FLD)
1259 for (i = 0; i < _NUM_FLD; i++){
1261 int_fld[i] = strtoul(str_fld[i], &end, 0);
1262 if (errno != 0 || end == str_fld[i] || int_fld[i] > 255)
1265 if (nb_lcore_params >= MAX_LCORE_PARAMS) {
1266 printf("exceeded max number of lcore params: %hu\n",
1270 lcore_params_array[nb_lcore_params].port_id = (uint8_t)int_fld[FLD_PORT];
1271 lcore_params_array[nb_lcore_params].queue_id = (uint8_t)int_fld[FLD_QUEUE];
1272 lcore_params_array[nb_lcore_params].lcore_id = (uint8_t)int_fld[FLD_LCORE];
1275 lcore_params = lcore_params_array;
1279 #define CMD_LINE_OPT_CONFIG "config"
1280 #define CMD_LINE_OPT_NO_NUMA "no-numa"
1281 #define CMD_LINE_OPT_IPV6 "ipv6"
1282 #define CMD_LINE_OPT_ENABLE_JUMBO "enable-jumbo"
1283 #define CMD_LINE_OPT_HASH_ENTRY_NUM "hash-entry-num"
1285 /* Parse the argument given in the command line of the application */
1287 parse_args(int argc, char **argv)
1292 char *prgname = argv[0];
1293 static struct option lgopts[] = {
1294 {CMD_LINE_OPT_CONFIG, 1, 0, 0},
1295 {CMD_LINE_OPT_NO_NUMA, 0, 0, 0},
1296 {CMD_LINE_OPT_IPV6, 0, 0, 0},
1297 {CMD_LINE_OPT_ENABLE_JUMBO, 0, 0, 0},
1298 {CMD_LINE_OPT_HASH_ENTRY_NUM, 1, 0, 0},
1304 while ((opt = getopt_long(argc, argvopt, "p:P",
1305 lgopts, &option_index)) != EOF) {
1310 enabled_port_mask = parse_portmask(optarg);
1311 if (enabled_port_mask == 0) {
1312 printf("invalid portmask\n");
1313 print_usage(prgname);
1318 printf("Promiscuous mode selected\n");
1324 if (!strncmp(lgopts[option_index].name, CMD_LINE_OPT_CONFIG,
1325 sizeof (CMD_LINE_OPT_CONFIG))) {
1326 ret = parse_config(optarg);
1328 printf("invalid config\n");
1329 print_usage(prgname);
1334 if (!strncmp(lgopts[option_index].name, CMD_LINE_OPT_NO_NUMA,
1335 sizeof(CMD_LINE_OPT_NO_NUMA))) {
1336 printf("numa is disabled \n");
1340 #if (APP_LOOKUP_METHOD == APP_LOOKUP_EXACT_MATCH)
1341 if (!strncmp(lgopts[option_index].name, CMD_LINE_OPT_IPV6,
1342 sizeof(CMD_LINE_OPT_IPV6))) {
1343 printf("ipv6 is specified \n");
1348 if (!strncmp(lgopts[option_index].name, CMD_LINE_OPT_ENABLE_JUMBO,
1349 sizeof (CMD_LINE_OPT_ENABLE_JUMBO))) {
1350 struct option lenopts = {"max-pkt-len", required_argument, 0, 0};
1352 printf("jumbo frame is enabled - disabling simple TX path\n");
1353 port_conf.rxmode.jumbo_frame = 1;
1354 tx_conf.txq_flags = 0;
1356 /* if no max-pkt-len set, use the default value ETHER_MAX_LEN */
1357 if (0 == getopt_long(argc, argvopt, "", &lenopts, &option_index)) {
1358 ret = parse_max_pkt_len(optarg);
1359 if ((ret < 64) || (ret > MAX_JUMBO_PKT_LEN)){
1360 printf("invalid packet length\n");
1361 print_usage(prgname);
1364 port_conf.rxmode.max_rx_pkt_len = ret;
1366 printf("set jumbo frame max packet length to %u\n",
1367 (unsigned int)port_conf.rxmode.max_rx_pkt_len);
1369 #if (APP_LOOKUP_METHOD == APP_LOOKUP_EXACT_MATCH)
1370 if (!strncmp(lgopts[option_index].name, CMD_LINE_OPT_HASH_ENTRY_NUM,
1371 sizeof(CMD_LINE_OPT_HASH_ENTRY_NUM))) {
1372 ret = parse_hash_entry_number(optarg);
1373 if ((ret > 0) && (ret <= L3FWD_HASH_ENTRIES)) {
1374 hash_entry_number = ret;
1376 printf("invalid hash entry number\n");
1377 print_usage(prgname);
1385 print_usage(prgname);
1391 argv[optind-1] = prgname;
1394 optind = 0; /* reset getopt lib */
1399 print_ethaddr(const char *name, const struct ether_addr *eth_addr)
1401 printf ("%s%02X:%02X:%02X:%02X:%02X:%02X", name,
1402 eth_addr->addr_bytes[0],
1403 eth_addr->addr_bytes[1],
1404 eth_addr->addr_bytes[2],
1405 eth_addr->addr_bytes[3],
1406 eth_addr->addr_bytes[4],
1407 eth_addr->addr_bytes[5]);
1410 #if (APP_LOOKUP_METHOD == APP_LOOKUP_EXACT_MATCH)
1412 static void convert_ipv4_5tuple(struct ipv4_5tuple* key1,
1413 union ipv4_5tuple_host* key2)
1415 key2->ip_dst = rte_cpu_to_be_32(key1->ip_dst);
1416 key2->ip_src = rte_cpu_to_be_32(key1->ip_src);
1417 key2->port_dst = rte_cpu_to_be_16(key1->port_dst);
1418 key2->port_src = rte_cpu_to_be_16(key1->port_src);
1419 key2->proto = key1->proto;
1425 static void convert_ipv6_5tuple(struct ipv6_5tuple* key1,
1426 union ipv6_5tuple_host* key2)
1429 for (i = 0; i < 16; i++)
1431 key2->ip_dst[i] = key1->ip_dst[i];
1432 key2->ip_src[i] = key1->ip_src[i];
1434 key2->port_dst = rte_cpu_to_be_16(key1->port_dst);
1435 key2->port_src = rte_cpu_to_be_16(key1->port_src);
1436 key2->proto = key1->proto;
1443 #define BYTE_VALUE_MAX 256
1444 #define ALL_32_BITS 0xffffffff
1445 #define BIT_8_TO_15 0x0000ff00
1447 populate_ipv4_few_flow_into_table(const struct rte_hash* h)
1451 uint32_t array_len = sizeof(ipv4_l3fwd_route_array)/sizeof(ipv4_l3fwd_route_array[0]);
1453 mask0 = _mm_set_epi32(ALL_32_BITS, ALL_32_BITS, ALL_32_BITS, BIT_8_TO_15);
1454 for (i = 0; i < array_len; i++) {
1455 struct ipv4_l3fwd_route entry;
1456 union ipv4_5tuple_host newkey;
1457 entry = ipv4_l3fwd_route_array[i];
1458 convert_ipv4_5tuple(&entry.key, &newkey);
1459 ret = rte_hash_add_key (h,(void *) &newkey);
1461 rte_exit(EXIT_FAILURE, "Unable to add entry %u to the"
1462 "l3fwd hash.\n", i);
1464 ipv4_l3fwd_out_if[ret] = entry.if_out;
1466 printf("Hash: Adding 0x%x keys\n", array_len);
1469 #define BIT_16_TO_23 0x00ff0000
1471 populate_ipv6_few_flow_into_table(const struct rte_hash* h)
1475 uint32_t array_len = sizeof(ipv6_l3fwd_route_array)/sizeof(ipv6_l3fwd_route_array[0]);
1477 mask1 = _mm_set_epi32(ALL_32_BITS, ALL_32_BITS, ALL_32_BITS, BIT_16_TO_23);
1478 mask2 = _mm_set_epi32(0, 0, ALL_32_BITS, ALL_32_BITS);
1479 for (i = 0; i < array_len; i++) {
1480 struct ipv6_l3fwd_route entry;
1481 union ipv6_5tuple_host newkey;
1482 entry = ipv6_l3fwd_route_array[i];
1483 convert_ipv6_5tuple(&entry.key, &newkey);
1484 ret = rte_hash_add_key (h, (void *) &newkey);
1486 rte_exit(EXIT_FAILURE, "Unable to add entry %u to the"
1487 "l3fwd hash.\n", i);
1489 ipv6_l3fwd_out_if[ret] = entry.if_out;
1491 printf("Hash: Adding 0x%xkeys\n", array_len);
1494 #define NUMBER_PORT_USED 4
1496 populate_ipv4_many_flow_into_table(const struct rte_hash* h,
1497 unsigned int nr_flow)
1500 mask0 = _mm_set_epi32(ALL_32_BITS, ALL_32_BITS, ALL_32_BITS, BIT_8_TO_15);
1501 for (i = 0; i < nr_flow; i++) {
1502 struct ipv4_l3fwd_route entry;
1503 union ipv4_5tuple_host newkey;
1504 uint8_t a = (uint8_t) ((i/NUMBER_PORT_USED)%BYTE_VALUE_MAX);
1505 uint8_t b = (uint8_t) (((i/NUMBER_PORT_USED)/BYTE_VALUE_MAX)%BYTE_VALUE_MAX);
1506 uint8_t c = (uint8_t) ((i/NUMBER_PORT_USED)/(BYTE_VALUE_MAX*BYTE_VALUE_MAX));
1507 /* Create the ipv4 exact match flow */
1508 switch (i & (NUMBER_PORT_USED -1)) {
1510 entry = ipv4_l3fwd_route_array[0];
1511 entry.key.ip_dst = IPv4(101,c,b,a);
1514 entry = ipv4_l3fwd_route_array[1];
1515 entry.key.ip_dst = IPv4(201,c,b,a);
1518 entry = ipv4_l3fwd_route_array[2];
1519 entry.key.ip_dst = IPv4(111,c,b,a);
1522 entry = ipv4_l3fwd_route_array[3];
1523 entry.key.ip_dst = IPv4(211,c,b,a);
1526 convert_ipv4_5tuple(&entry.key, &newkey);
1527 int32_t ret = rte_hash_add_key(h,(void *) &newkey);
1529 rte_exit(EXIT_FAILURE, "Unable to add entry %u\n", i);
1531 ipv4_l3fwd_out_if[ret] = (uint8_t) entry.if_out;
1534 printf("Hash: Adding 0x%x keys\n", nr_flow);
1538 populate_ipv6_many_flow_into_table(const struct rte_hash* h,
1539 unsigned int nr_flow)
1542 mask1 = _mm_set_epi32(ALL_32_BITS, ALL_32_BITS, ALL_32_BITS, BIT_16_TO_23);
1543 mask2 = _mm_set_epi32(0, 0, ALL_32_BITS, ALL_32_BITS);
1544 for (i = 0; i < nr_flow; i++) {
1545 struct ipv6_l3fwd_route entry;
1546 union ipv6_5tuple_host newkey;
1547 uint8_t a = (uint8_t) ((i/NUMBER_PORT_USED)%BYTE_VALUE_MAX);
1548 uint8_t b = (uint8_t) (((i/NUMBER_PORT_USED)/BYTE_VALUE_MAX)%BYTE_VALUE_MAX);
1549 uint8_t c = (uint8_t) ((i/NUMBER_PORT_USED)/(BYTE_VALUE_MAX*BYTE_VALUE_MAX));
1550 /* Create the ipv6 exact match flow */
1551 switch (i & (NUMBER_PORT_USED - 1)) {
1552 case 0: entry = ipv6_l3fwd_route_array[0]; break;
1553 case 1: entry = ipv6_l3fwd_route_array[1]; break;
1554 case 2: entry = ipv6_l3fwd_route_array[2]; break;
1555 case 3: entry = ipv6_l3fwd_route_array[3]; break;
1557 entry.key.ip_dst[13] = c;
1558 entry.key.ip_dst[14] = b;
1559 entry.key.ip_dst[15] = a;
1560 convert_ipv6_5tuple(&entry.key, &newkey);
1561 int32_t ret = rte_hash_add_key(h,(void *) &newkey);
1563 rte_exit(EXIT_FAILURE, "Unable to add entry %u\n", i);
1565 ipv6_l3fwd_out_if[ret] = (uint8_t) entry.if_out;
1568 printf("Hash: Adding 0x%x keys\n", nr_flow);
1572 setup_hash(int socketid)
1574 struct rte_hash_parameters ipv4_l3fwd_hash_params = {
1576 .entries = L3FWD_HASH_ENTRIES,
1577 .bucket_entries = 4,
1578 .key_len = sizeof(union ipv4_5tuple_host),
1579 .hash_func = ipv4_hash_crc,
1580 .hash_func_init_val = 0,
1583 struct rte_hash_parameters ipv6_l3fwd_hash_params = {
1585 .entries = L3FWD_HASH_ENTRIES,
1586 .bucket_entries = 4,
1587 .key_len = sizeof(union ipv6_5tuple_host),
1588 .hash_func = ipv6_hash_crc,
1589 .hash_func_init_val = 0,
1594 /* create ipv4 hash */
1595 rte_snprintf(s, sizeof(s), "ipv4_l3fwd_hash_%d", socketid);
1596 ipv4_l3fwd_hash_params.name = s;
1597 ipv4_l3fwd_hash_params.socket_id = socketid;
1598 ipv4_l3fwd_lookup_struct[socketid] = rte_hash_create(&ipv4_l3fwd_hash_params);
1599 if (ipv4_l3fwd_lookup_struct[socketid] == NULL)
1600 rte_exit(EXIT_FAILURE, "Unable to create the l3fwd hash on "
1601 "socket %d\n", socketid);
1603 /* create ipv6 hash */
1604 rte_snprintf(s, sizeof(s), "ipv6_l3fwd_hash_%d", socketid);
1605 ipv6_l3fwd_hash_params.name = s;
1606 ipv6_l3fwd_hash_params.socket_id = socketid;
1607 ipv6_l3fwd_lookup_struct[socketid] = rte_hash_create(&ipv6_l3fwd_hash_params);
1608 if (ipv6_l3fwd_lookup_struct[socketid] == NULL)
1609 rte_exit(EXIT_FAILURE, "Unable to create the l3fwd hash on "
1610 "socket %d\n", socketid);
1612 if (hash_entry_number != HASH_ENTRY_NUMBER_DEFAULT) {
1613 /* For testing hash matching with a large number of flows we
1614 * generate millions of IP 5-tuples with an incremented dst
1615 * address to initialize the hash table. */
1617 /* populate the ipv4 hash */
1618 populate_ipv4_many_flow_into_table(
1619 ipv4_l3fwd_lookup_struct[socketid], hash_entry_number);
1621 /* populate the ipv6 hash */
1622 populate_ipv6_many_flow_into_table(
1623 ipv6_l3fwd_lookup_struct[socketid], hash_entry_number);
1626 /* Use data in ipv4/ipv6 l3fwd lookup table directly to initialize the hash table */
1628 /* populate the ipv4 hash */
1629 populate_ipv4_few_flow_into_table(ipv4_l3fwd_lookup_struct[socketid]);
1631 /* populate the ipv6 hash */
1632 populate_ipv6_few_flow_into_table(ipv6_l3fwd_lookup_struct[socketid]);
1638 #if (APP_LOOKUP_METHOD == APP_LOOKUP_LPM)
1640 setup_lpm(int socketid)
1642 struct rte_lpm6_config config;
1647 /* create the LPM table */
1648 rte_snprintf(s, sizeof(s), "IPV4_L3FWD_LPM_%d", socketid);
1649 ipv4_l3fwd_lookup_struct[socketid] = rte_lpm_create(s, socketid,
1650 IPV4_L3FWD_LPM_MAX_RULES, 0);
1651 if (ipv4_l3fwd_lookup_struct[socketid] == NULL)
1652 rte_exit(EXIT_FAILURE, "Unable to create the l3fwd LPM table"
1653 " on socket %d\n", socketid);
1655 /* populate the LPM table */
1656 for (i = 0; i < IPV4_L3FWD_NUM_ROUTES; i++) {
1657 ret = rte_lpm_add(ipv4_l3fwd_lookup_struct[socketid],
1658 ipv4_l3fwd_route_array[i].ip,
1659 ipv4_l3fwd_route_array[i].depth,
1660 ipv4_l3fwd_route_array[i].if_out);
1663 rte_exit(EXIT_FAILURE, "Unable to add entry %u to the "
1664 "l3fwd LPM table on socket %d\n",
1668 printf("LPM: Adding route 0x%08x / %d (%d)\n",
1669 (unsigned)ipv4_l3fwd_route_array[i].ip,
1670 ipv4_l3fwd_route_array[i].depth,
1671 ipv4_l3fwd_route_array[i].if_out);
1674 /* create the LPM6 table */
1675 rte_snprintf(s, sizeof(s), "IPV6_L3FWD_LPM_%d", socketid);
1677 config.max_rules = IPV6_L3FWD_LPM_MAX_RULES;
1678 config.number_tbl8s = IPV6_L3FWD_LPM_NUMBER_TBL8S;
1680 ipv6_l3fwd_lookup_struct[socketid] = rte_lpm6_create(s, socketid,
1682 if (ipv6_l3fwd_lookup_struct[socketid] == NULL)
1683 rte_exit(EXIT_FAILURE, "Unable to create the l3fwd LPM table"
1684 " on socket %d\n", socketid);
1686 /* populate the LPM table */
1687 for (i = 0; i < IPV6_L3FWD_NUM_ROUTES; i++) {
1688 ret = rte_lpm6_add(ipv6_l3fwd_lookup_struct[socketid],
1689 ipv6_l3fwd_route_array[i].ip,
1690 ipv6_l3fwd_route_array[i].depth,
1691 ipv6_l3fwd_route_array[i].if_out);
1694 rte_exit(EXIT_FAILURE, "Unable to add entry %u to the "
1695 "l3fwd LPM table on socket %d\n",
1699 printf("LPM: Adding route %s / %d (%d)\n",
1701 ipv6_l3fwd_route_array[i].depth,
1702 ipv6_l3fwd_route_array[i].if_out);
1708 init_mem(unsigned nb_mbuf)
1710 struct lcore_conf *qconf;
1715 for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
1716 if (rte_lcore_is_enabled(lcore_id) == 0)
1720 socketid = rte_lcore_to_socket_id(lcore_id);
1724 if (socketid >= NB_SOCKETS) {
1725 rte_exit(EXIT_FAILURE, "Socket %d of lcore %u is out of range %d\n",
1726 socketid, lcore_id, NB_SOCKETS);
1728 if (pktmbuf_pool[socketid] == NULL) {
1729 rte_snprintf(s, sizeof(s), "mbuf_pool_%d", socketid);
1730 pktmbuf_pool[socketid] =
1731 rte_mempool_create(s, nb_mbuf, MBUF_SIZE, MEMPOOL_CACHE_SIZE,
1732 sizeof(struct rte_pktmbuf_pool_private),
1733 rte_pktmbuf_pool_init, NULL,
1734 rte_pktmbuf_init, NULL,
1736 if (pktmbuf_pool[socketid] == NULL)
1737 rte_exit(EXIT_FAILURE,
1738 "Cannot init mbuf pool on socket %d\n", socketid);
1740 printf("Allocated mbuf pool on socket %d\n", socketid);
1742 #if (APP_LOOKUP_METHOD == APP_LOOKUP_LPM)
1743 setup_lpm(socketid);
1745 setup_hash(socketid);
1748 qconf = &lcore_conf[lcore_id];
1749 qconf->ipv4_lookup_struct = ipv4_l3fwd_lookup_struct[socketid];
1750 qconf->ipv6_lookup_struct = ipv6_l3fwd_lookup_struct[socketid];
1755 /* Check the link status of all ports in up to 9s, and print them finally */
1757 check_all_ports_link_status(uint8_t port_num, uint32_t port_mask)
1759 #define CHECK_INTERVAL 100 /* 100ms */
1760 #define MAX_CHECK_TIME 90 /* 9s (90 * 100ms) in total */
1761 uint8_t portid, count, all_ports_up, print_flag = 0;
1762 struct rte_eth_link link;
1764 printf("\nChecking link status");
1766 for (count = 0; count <= MAX_CHECK_TIME; count++) {
1768 for (portid = 0; portid < port_num; portid++) {
1769 if ((port_mask & (1 << portid)) == 0)
1771 memset(&link, 0, sizeof(link));
1772 rte_eth_link_get_nowait(portid, &link);
1773 /* print link status if flag set */
1774 if (print_flag == 1) {
1775 if (link.link_status)
1776 printf("Port %d Link Up - speed %u "
1777 "Mbps - %s\n", (uint8_t)portid,
1778 (unsigned)link.link_speed,
1779 (link.link_duplex == ETH_LINK_FULL_DUPLEX) ?
1780 ("full-duplex") : ("half-duplex\n"));
1782 printf("Port %d Link Down\n",
1786 /* clear all_ports_up flag if any link down */
1787 if (link.link_status == 0) {
1792 /* after finally printing all link status, get out */
1793 if (print_flag == 1)
1796 if (all_ports_up == 0) {
1799 rte_delay_ms(CHECK_INTERVAL);
1802 /* set the print_flag if all ports up or timeout */
1803 if (all_ports_up == 1 || count == (MAX_CHECK_TIME - 1)) {
1811 MAIN(int argc, char **argv)
1813 struct lcore_conf *qconf;
1818 uint32_t n_tx_queue, nb_lcores;
1819 uint8_t portid, nb_rx_queue, queue, socketid;
1822 ret = rte_eal_init(argc, argv);
1824 rte_exit(EXIT_FAILURE, "Invalid EAL parameters\n");
1828 /* parse application arguments (after the EAL ones) */
1829 ret = parse_args(argc, argv);
1831 rte_exit(EXIT_FAILURE, "Invalid L3FWD parameters\n");
1833 if (check_lcore_params() < 0)
1834 rte_exit(EXIT_FAILURE, "check_lcore_params failed\n");
1836 ret = init_lcore_rx_queues();
1838 rte_exit(EXIT_FAILURE, "init_lcore_rx_queues failed\n");
1841 /* init driver(s) */
1842 if (rte_pmd_init_all() < 0)
1843 rte_exit(EXIT_FAILURE, "Cannot init pmd\n");
1845 if (rte_eal_pci_probe() < 0)
1846 rte_exit(EXIT_FAILURE, "Cannot probe PCI\n");
1848 nb_ports = rte_eth_dev_count();
1849 if (nb_ports > RTE_MAX_ETHPORTS)
1850 nb_ports = RTE_MAX_ETHPORTS;
1852 if (check_port_config(nb_ports) < 0)
1853 rte_exit(EXIT_FAILURE, "check_port_config failed\n");
1855 nb_lcores = rte_lcore_count();
1857 /* initialize all ports */
1858 for (portid = 0; portid < nb_ports; portid++) {
1859 /* skip ports that are not enabled */
1860 if ((enabled_port_mask & (1 << portid)) == 0) {
1861 printf("\nSkipping disabled port %d\n", portid);
1866 printf("Initializing port %d ... ", portid );
1869 nb_rx_queue = get_port_n_rx_queues(portid);
1870 n_tx_queue = nb_lcores;
1871 if (n_tx_queue > MAX_TX_QUEUE_PER_PORT)
1872 n_tx_queue = MAX_TX_QUEUE_PER_PORT;
1873 printf("Creating queues: nb_rxq=%d nb_txq=%u... ",
1874 nb_rx_queue, (unsigned)n_tx_queue );
1875 ret = rte_eth_dev_configure(portid, nb_rx_queue,
1876 (uint16_t)n_tx_queue, &port_conf);
1878 rte_exit(EXIT_FAILURE, "Cannot configure device: err=%d, port=%d\n",
1881 rte_eth_macaddr_get(portid, &ports_eth_addr[portid]);
1882 print_ethaddr(" Address:", &ports_eth_addr[portid]);
1886 ret = init_mem(NB_MBUF);
1888 rte_exit(EXIT_FAILURE, "init_mem failed\n");
1890 /* init one TX queue per couple (lcore,port) */
1892 for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
1893 if (rte_lcore_is_enabled(lcore_id) == 0)
1897 socketid = (uint8_t)rte_lcore_to_socket_id(lcore_id);
1901 printf("txq=%u,%d,%d ", lcore_id, queueid, socketid);
1903 ret = rte_eth_tx_queue_setup(portid, queueid, nb_txd,
1904 socketid, &tx_conf);
1906 rte_exit(EXIT_FAILURE, "rte_eth_tx_queue_setup: err=%d, "
1907 "port=%d\n", ret, portid);
1909 qconf = &lcore_conf[lcore_id];
1910 qconf->tx_queue_id[portid] = queueid;
1916 for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
1917 if (rte_lcore_is_enabled(lcore_id) == 0)
1919 qconf = &lcore_conf[lcore_id];
1920 printf("\nInitializing rx queues on lcore %u ... ", lcore_id );
1922 /* init RX queues */
1923 for(queue = 0; queue < qconf->n_rx_queue; ++queue) {
1924 portid = qconf->rx_queue_list[queue].port_id;
1925 queueid = qconf->rx_queue_list[queue].queue_id;
1928 socketid = (uint8_t)rte_lcore_to_socket_id(lcore_id);
1932 printf("rxq=%d,%d,%d ", portid, queueid, socketid);
1935 ret = rte_eth_rx_queue_setup(portid, queueid, nb_rxd,
1936 socketid, &rx_conf, pktmbuf_pool[socketid]);
1938 rte_exit(EXIT_FAILURE, "rte_eth_rx_queue_setup: err=%d,"
1939 "port=%d\n", ret, portid);
1946 for (portid = 0; portid < nb_ports; portid++) {
1947 if ((enabled_port_mask & (1 << portid)) == 0) {
1951 ret = rte_eth_dev_start(portid);
1953 rte_exit(EXIT_FAILURE, "rte_eth_dev_start: err=%d, port=%d\n",
1957 * If enabled, put device in promiscuous mode.
1958 * This allows IO forwarding mode to forward packets
1959 * to itself through 2 cross-connected ports of the
1963 rte_eth_promiscuous_enable(portid);
1966 check_all_ports_link_status((uint8_t)nb_ports, enabled_port_mask);
1968 /* launch per-lcore init on every lcore */
1969 rte_eal_mp_remote_launch(main_loop, NULL, CALL_MASTER);
1970 RTE_LCORE_FOREACH_SLAVE(lcore_id) {
1971 if (rte_eal_wait_lcore(lcore_id) < 0)