1d30ea906Sjfb8856606 /* SPDX-License-Identifier: BSD-3-Clause
2d30ea906Sjfb8856606  * Copyright(c) 2010-2014 Intel Corporation
3a9643ea8Slogwang  */
4a9643ea8Slogwang 
5a9643ea8Slogwang #include <stdio.h>
6a9643ea8Slogwang #include <stdlib.h>
7a9643ea8Slogwang #include <stdint.h>
8a9643ea8Slogwang #include <inttypes.h>
9a9643ea8Slogwang #include <sys/types.h>
10a9643ea8Slogwang #include <sys/param.h>
11a9643ea8Slogwang #include <string.h>
12a9643ea8Slogwang #include <sys/queue.h>
13a9643ea8Slogwang #include <stdarg.h>
14a9643ea8Slogwang #include <errno.h>
15a9643ea8Slogwang #include <getopt.h>
16a9643ea8Slogwang 
17a9643ea8Slogwang #include <rte_common.h>
18a9643ea8Slogwang #include <rte_byteorder.h>
19a9643ea8Slogwang #include <rte_log.h>
20a9643ea8Slogwang #include <rte_memory.h>
21a9643ea8Slogwang #include <rte_memcpy.h>
22a9643ea8Slogwang #include <rte_eal.h>
23a9643ea8Slogwang #include <rte_launch.h>
24a9643ea8Slogwang #include <rte_atomic.h>
25a9643ea8Slogwang #include <rte_cycles.h>
26a9643ea8Slogwang #include <rte_prefetch.h>
27a9643ea8Slogwang #include <rte_lcore.h>
28a9643ea8Slogwang #include <rte_per_lcore.h>
29a9643ea8Slogwang #include <rte_branch_prediction.h>
30a9643ea8Slogwang #include <rte_interrupts.h>
31a9643ea8Slogwang #include <rte_random.h>
32a9643ea8Slogwang #include <rte_debug.h>
33a9643ea8Slogwang #include <rte_ether.h>
34a9643ea8Slogwang #include <rte_ethdev.h>
35a9643ea8Slogwang #include <rte_mempool.h>
36a9643ea8Slogwang #include <rte_mbuf.h>
37a9643ea8Slogwang #include <rte_lpm.h>
38a9643ea8Slogwang #include <rte_lpm6.h>
39a9643ea8Slogwang #include <rte_ip.h>
40a9643ea8Slogwang #include <rte_string_fns.h>
41a9643ea8Slogwang 
42a9643ea8Slogwang #include <rte_ip_frag.h>
43a9643ea8Slogwang 
44a9643ea8Slogwang #define RTE_LOGTYPE_IP_FRAG RTE_LOGTYPE_USER1
45a9643ea8Slogwang 
46a9643ea8Slogwang /* allow max jumbo frame 9.5 KB */
47a9643ea8Slogwang #define JUMBO_FRAME_MAX_SIZE	0x2600
48a9643ea8Slogwang 
49a9643ea8Slogwang #define	ROUNDUP_DIV(a, b)	(((a) + (b) - 1) / (b))
50a9643ea8Slogwang 
51a9643ea8Slogwang /*
52a9643ea8Slogwang  * Default byte size for the IPv6 Maximum Transfer Unit (MTU).
53a9643ea8Slogwang  * This value includes the size of IPv6 header.
54a9643ea8Slogwang  */
554418919fSjohnjiang #define	IPV4_MTU_DEFAULT	RTE_ETHER_MTU
564418919fSjohnjiang #define	IPV6_MTU_DEFAULT	RTE_ETHER_MTU
574418919fSjohnjiang 
584418919fSjohnjiang /*
594418919fSjohnjiang  * The overhead from max frame size to MTU.
604418919fSjohnjiang  * We have to consider the max possible overhead.
614418919fSjohnjiang  */
624418919fSjohnjiang #define MTU_OVERHEAD	\
634418919fSjohnjiang 	(RTE_ETHER_HDR_LEN + RTE_ETHER_CRC_LEN + \
644418919fSjohnjiang 		2 * sizeof(struct rte_vlan_hdr))
65a9643ea8Slogwang 
66a9643ea8Slogwang /*
67a9643ea8Slogwang  * Default payload in bytes for the IPv6 packet.
68a9643ea8Slogwang  */
694418919fSjohnjiang #define	IPV4_DEFAULT_PAYLOAD	(IPV4_MTU_DEFAULT - sizeof(struct rte_ipv4_hdr))
704418919fSjohnjiang #define	IPV6_DEFAULT_PAYLOAD	(IPV6_MTU_DEFAULT - sizeof(struct rte_ipv6_hdr))
71a9643ea8Slogwang 
72a9643ea8Slogwang /*
73a9643ea8Slogwang  * Max number of fragments per packet expected - defined by config file.
74a9643ea8Slogwang  */
75a9643ea8Slogwang #define	MAX_PACKET_FRAG RTE_LIBRTE_IP_FRAG_MAX_FRAG
76a9643ea8Slogwang 
77a9643ea8Slogwang #define NB_MBUF   8192
78a9643ea8Slogwang 
79a9643ea8Slogwang #define MAX_PKT_BURST	32
80a9643ea8Slogwang #define BURST_TX_DRAIN_US 100 /* TX drain every ~100us */
81a9643ea8Slogwang 
82a9643ea8Slogwang /* Configure how many packets ahead to prefetch, when reading packets */
83a9643ea8Slogwang #define PREFETCH_OFFSET	3
84a9643ea8Slogwang 
85a9643ea8Slogwang /*
86a9643ea8Slogwang  * Configurable number of RX/TX ring descriptors
87a9643ea8Slogwang  */
88d30ea906Sjfb8856606 #define RTE_TEST_RX_DESC_DEFAULT 1024
89d30ea906Sjfb8856606 #define RTE_TEST_TX_DESC_DEFAULT 1024
90a9643ea8Slogwang static uint16_t nb_rxd = RTE_TEST_RX_DESC_DEFAULT;
91a9643ea8Slogwang static uint16_t nb_txd = RTE_TEST_TX_DESC_DEFAULT;
92a9643ea8Slogwang 
93a9643ea8Slogwang /* ethernet addresses of ports */
944418919fSjohnjiang static struct rte_ether_addr ports_eth_addr[RTE_MAX_ETHPORTS];
95a9643ea8Slogwang 
96a9643ea8Slogwang #ifndef IPv4_BYTES
97a9643ea8Slogwang #define IPv4_BYTES_FMT "%" PRIu8 ".%" PRIu8 ".%" PRIu8 ".%" PRIu8
98a9643ea8Slogwang #define IPv4_BYTES(addr) \
99a9643ea8Slogwang 		(uint8_t) (((addr) >> 24) & 0xFF),\
100a9643ea8Slogwang 		(uint8_t) (((addr) >> 16) & 0xFF),\
101a9643ea8Slogwang 		(uint8_t) (((addr) >> 8) & 0xFF),\
102a9643ea8Slogwang 		(uint8_t) ((addr) & 0xFF)
103a9643ea8Slogwang #endif
104a9643ea8Slogwang 
105a9643ea8Slogwang #ifndef IPv6_BYTES
106a9643ea8Slogwang #define IPv6_BYTES_FMT "%02x%02x:%02x%02x:%02x%02x:%02x%02x:"\
107a9643ea8Slogwang                        "%02x%02x:%02x%02x:%02x%02x:%02x%02x"
108a9643ea8Slogwang #define IPv6_BYTES(addr) \
109a9643ea8Slogwang 	addr[0],  addr[1], addr[2],  addr[3], \
110a9643ea8Slogwang 	addr[4],  addr[5], addr[6],  addr[7], \
111a9643ea8Slogwang 	addr[8],  addr[9], addr[10], addr[11],\
112a9643ea8Slogwang 	addr[12], addr[13],addr[14], addr[15]
113a9643ea8Slogwang #endif
114a9643ea8Slogwang 
115a9643ea8Slogwang #define IPV6_ADDR_LEN 16
116a9643ea8Slogwang 
117a9643ea8Slogwang /* mask of enabled ports */
118a9643ea8Slogwang static int enabled_port_mask = 0;
119a9643ea8Slogwang 
120a9643ea8Slogwang static int rx_queue_per_lcore = 1;
121a9643ea8Slogwang 
122a9643ea8Slogwang #define MBUF_TABLE_SIZE  (2 * MAX(MAX_PKT_BURST, MAX_PACKET_FRAG))
123a9643ea8Slogwang 
124a9643ea8Slogwang struct mbuf_table {
125a9643ea8Slogwang 	uint16_t len;
126a9643ea8Slogwang 	struct rte_mbuf *m_table[MBUF_TABLE_SIZE];
127a9643ea8Slogwang };
128a9643ea8Slogwang 
129a9643ea8Slogwang struct rx_queue {
130a9643ea8Slogwang 	struct rte_mempool *direct_pool;
131a9643ea8Slogwang 	struct rte_mempool *indirect_pool;
132a9643ea8Slogwang 	struct rte_lpm *lpm;
133a9643ea8Slogwang 	struct rte_lpm6 *lpm6;
1342bfe3f2eSlogwang 	uint16_t portid;
135a9643ea8Slogwang };
136a9643ea8Slogwang 
137a9643ea8Slogwang #define MAX_RX_QUEUE_PER_LCORE 16
138a9643ea8Slogwang #define MAX_TX_QUEUE_PER_PORT 16
139a9643ea8Slogwang struct lcore_queue_conf {
140a9643ea8Slogwang 	uint16_t n_rx_queue;
141a9643ea8Slogwang 	uint16_t tx_queue_id[RTE_MAX_ETHPORTS];
142a9643ea8Slogwang 	struct rx_queue rx_queue_list[MAX_RX_QUEUE_PER_LCORE];
143a9643ea8Slogwang 	struct mbuf_table tx_mbufs[RTE_MAX_ETHPORTS];
144a9643ea8Slogwang } __rte_cache_aligned;
145a9643ea8Slogwang struct lcore_queue_conf lcore_queue_conf[RTE_MAX_LCORE];
146a9643ea8Slogwang 
1472bfe3f2eSlogwang static struct rte_eth_conf port_conf = {
148a9643ea8Slogwang 	.rxmode = {
149a9643ea8Slogwang 		.max_rx_pkt_len = JUMBO_FRAME_MAX_SIZE,
150a9643ea8Slogwang 		.split_hdr_size = 0,
151d30ea906Sjfb8856606 		.offloads = (DEV_RX_OFFLOAD_CHECKSUM |
1524418919fSjohnjiang 			     DEV_RX_OFFLOAD_SCATTER |
153d30ea906Sjfb8856606 			     DEV_RX_OFFLOAD_JUMBO_FRAME),
154a9643ea8Slogwang 	},
155a9643ea8Slogwang 	.txmode = {
156a9643ea8Slogwang 		.mq_mode = ETH_MQ_TX_NONE,
157d30ea906Sjfb8856606 		.offloads = (DEV_TX_OFFLOAD_IPV4_CKSUM |
158d30ea906Sjfb8856606 			     DEV_TX_OFFLOAD_MULTI_SEGS),
159a9643ea8Slogwang 	},
160a9643ea8Slogwang };
161a9643ea8Slogwang 
162a9643ea8Slogwang /*
163a9643ea8Slogwang  * IPv4 forwarding table
164a9643ea8Slogwang  */
165a9643ea8Slogwang struct l3fwd_ipv4_route {
166a9643ea8Slogwang 	uint32_t ip;
167a9643ea8Slogwang 	uint8_t  depth;
168a9643ea8Slogwang 	uint8_t  if_out;
169a9643ea8Slogwang };
170a9643ea8Slogwang 
171a9643ea8Slogwang struct l3fwd_ipv4_route l3fwd_ipv4_route_array[] = {
1724418919fSjohnjiang 		{RTE_IPV4(100,10,0,0), 16, 0},
1734418919fSjohnjiang 		{RTE_IPV4(100,20,0,0), 16, 1},
1744418919fSjohnjiang 		{RTE_IPV4(100,30,0,0), 16, 2},
1754418919fSjohnjiang 		{RTE_IPV4(100,40,0,0), 16, 3},
1764418919fSjohnjiang 		{RTE_IPV4(100,50,0,0), 16, 4},
1774418919fSjohnjiang 		{RTE_IPV4(100,60,0,0), 16, 5},
1784418919fSjohnjiang 		{RTE_IPV4(100,70,0,0), 16, 6},
1794418919fSjohnjiang 		{RTE_IPV4(100,80,0,0), 16, 7},
180a9643ea8Slogwang };
181a9643ea8Slogwang 
182a9643ea8Slogwang /*
183a9643ea8Slogwang  * IPv6 forwarding table
184a9643ea8Slogwang  */
185a9643ea8Slogwang 
186a9643ea8Slogwang struct l3fwd_ipv6_route {
187a9643ea8Slogwang 	uint8_t ip[IPV6_ADDR_LEN];
188a9643ea8Slogwang 	uint8_t depth;
189a9643ea8Slogwang 	uint8_t if_out;
190a9643ea8Slogwang };
191a9643ea8Slogwang 
192a9643ea8Slogwang static struct l3fwd_ipv6_route l3fwd_ipv6_route_array[] = {
193a9643ea8Slogwang 	{{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}, 48, 0},
194a9643ea8Slogwang 	{{2,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}, 48, 1},
195a9643ea8Slogwang 	{{3,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}, 48, 2},
196a9643ea8Slogwang 	{{4,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}, 48, 3},
197a9643ea8Slogwang 	{{5,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}, 48, 4},
198a9643ea8Slogwang 	{{6,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}, 48, 5},
199a9643ea8Slogwang 	{{7,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}, 48, 6},
200a9643ea8Slogwang 	{{8,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}, 48, 7},
201a9643ea8Slogwang };
202a9643ea8Slogwang 
203a9643ea8Slogwang #define LPM_MAX_RULES         1024
204a9643ea8Slogwang #define LPM6_MAX_RULES         1024
205a9643ea8Slogwang #define LPM6_NUMBER_TBL8S (1 << 16)
206a9643ea8Slogwang 
207a9643ea8Slogwang struct rte_lpm6_config lpm6_config = {
208a9643ea8Slogwang 		.max_rules = LPM6_MAX_RULES,
209a9643ea8Slogwang 		.number_tbl8s = LPM6_NUMBER_TBL8S,
210a9643ea8Slogwang 		.flags = 0
211a9643ea8Slogwang };
212a9643ea8Slogwang 
213a9643ea8Slogwang static struct rte_mempool *socket_direct_pool[RTE_MAX_NUMA_NODES];
214a9643ea8Slogwang static struct rte_mempool *socket_indirect_pool[RTE_MAX_NUMA_NODES];
215a9643ea8Slogwang static struct rte_lpm *socket_lpm[RTE_MAX_NUMA_NODES];
216a9643ea8Slogwang static struct rte_lpm6 *socket_lpm6[RTE_MAX_NUMA_NODES];
217a9643ea8Slogwang 
218a9643ea8Slogwang /* Send burst of packets on an output interface */
219a9643ea8Slogwang static inline int
send_burst(struct lcore_queue_conf * qconf,uint16_t n,uint16_t port)2202bfe3f2eSlogwang send_burst(struct lcore_queue_conf *qconf, uint16_t n, uint16_t port)
221a9643ea8Slogwang {
222a9643ea8Slogwang 	struct rte_mbuf **m_table;
223a9643ea8Slogwang 	int ret;
224a9643ea8Slogwang 	uint16_t queueid;
225a9643ea8Slogwang 
226a9643ea8Slogwang 	queueid = qconf->tx_queue_id[port];
227a9643ea8Slogwang 	m_table = (struct rte_mbuf **)qconf->tx_mbufs[port].m_table;
228a9643ea8Slogwang 
229a9643ea8Slogwang 	ret = rte_eth_tx_burst(port, queueid, m_table, n);
230a9643ea8Slogwang 	if (unlikely(ret < n)) {
231a9643ea8Slogwang 		do {
232a9643ea8Slogwang 			rte_pktmbuf_free(m_table[ret]);
233a9643ea8Slogwang 		} while (++ret < n);
234a9643ea8Slogwang 	}
235a9643ea8Slogwang 
236a9643ea8Slogwang 	return 0;
237a9643ea8Slogwang }
238a9643ea8Slogwang 
239a9643ea8Slogwang static inline void
l3fwd_simple_forward(struct rte_mbuf * m,struct lcore_queue_conf * qconf,uint8_t queueid,uint16_t port_in)240a9643ea8Slogwang l3fwd_simple_forward(struct rte_mbuf *m, struct lcore_queue_conf *qconf,
2412bfe3f2eSlogwang 		uint8_t queueid, uint16_t port_in)
242a9643ea8Slogwang {
243a9643ea8Slogwang 	struct rx_queue *rxq;
2442bfe3f2eSlogwang 	uint32_t i, len, next_hop;
2454b05018fSfengbojiang 	uint16_t port_out, ether_type;
246a9643ea8Slogwang 	int32_t len2;
2474418919fSjohnjiang 	uint64_t ol_flags;
2484418919fSjohnjiang 	const struct rte_ether_hdr *eth;
249a9643ea8Slogwang 
2504418919fSjohnjiang 	ol_flags = 0;
251a9643ea8Slogwang 	rxq = &qconf->rx_queue_list[queueid];
252a9643ea8Slogwang 
253a9643ea8Slogwang 	/* by default, send everything back to the source port */
254a9643ea8Slogwang 	port_out = port_in;
255a9643ea8Slogwang 
2564b05018fSfengbojiang 	/* save ether type of the incoming packet */
2574418919fSjohnjiang 	eth = rte_pktmbuf_mtod(m, const struct rte_ether_hdr *);
2584b05018fSfengbojiang 	ether_type = eth->ether_type;
2594b05018fSfengbojiang 
260a9643ea8Slogwang 	/* Remove the Ethernet header and trailer from the input packet */
2614418919fSjohnjiang 	rte_pktmbuf_adj(m, (uint16_t)sizeof(struct rte_ether_hdr));
262a9643ea8Slogwang 
263a9643ea8Slogwang 	/* Build transmission burst */
264a9643ea8Slogwang 	len = qconf->tx_mbufs[port_out].len;
265a9643ea8Slogwang 
266a9643ea8Slogwang 	/* if this is an IPv4 packet */
267a9643ea8Slogwang 	if (RTE_ETH_IS_IPV4_HDR(m->packet_type)) {
2684418919fSjohnjiang 		struct rte_ipv4_hdr *ip_hdr;
269a9643ea8Slogwang 		uint32_t ip_dst;
270a9643ea8Slogwang 		/* Read the lookup key (i.e. ip_dst) from the input packet */
2714418919fSjohnjiang 		ip_hdr = rte_pktmbuf_mtod(m, struct rte_ipv4_hdr *);
272a9643ea8Slogwang 		ip_dst = rte_be_to_cpu_32(ip_hdr->dst_addr);
273a9643ea8Slogwang 
274a9643ea8Slogwang 		/* Find destination port */
2752bfe3f2eSlogwang 		if (rte_lpm_lookup(rxq->lpm, ip_dst, &next_hop) == 0 &&
2762bfe3f2eSlogwang 				(enabled_port_mask & 1 << next_hop) != 0) {
2772bfe3f2eSlogwang 			port_out = next_hop;
278a9643ea8Slogwang 
279a9643ea8Slogwang 			/* Build transmission burst for new port */
280a9643ea8Slogwang 			len = qconf->tx_mbufs[port_out].len;
281a9643ea8Slogwang 		}
282a9643ea8Slogwang 
283a9643ea8Slogwang 		/* if we don't need to do any fragmentation */
284a9643ea8Slogwang 		if (likely (IPV4_MTU_DEFAULT >= m->pkt_len)) {
285a9643ea8Slogwang 			qconf->tx_mbufs[port_out].m_table[len] = m;
286a9643ea8Slogwang 			len2 = 1;
287a9643ea8Slogwang 		} else {
288a9643ea8Slogwang 			len2 = rte_ipv4_fragment_packet(m,
289a9643ea8Slogwang 				&qconf->tx_mbufs[port_out].m_table[len],
290a9643ea8Slogwang 				(uint16_t)(MBUF_TABLE_SIZE - len),
291a9643ea8Slogwang 				IPV4_MTU_DEFAULT,
292a9643ea8Slogwang 				rxq->direct_pool, rxq->indirect_pool);
293a9643ea8Slogwang 
294a9643ea8Slogwang 			/* Free input packet */
295a9643ea8Slogwang 			rte_pktmbuf_free(m);
296a9643ea8Slogwang 
2974418919fSjohnjiang 			/* request HW to regenerate IPv4 cksum */
2984418919fSjohnjiang 			ol_flags |= (PKT_TX_IPV4 | PKT_TX_IP_CKSUM);
2994418919fSjohnjiang 
300a9643ea8Slogwang 			/* If we fail to fragment the packet */
301a9643ea8Slogwang 			if (unlikely (len2 < 0))
302a9643ea8Slogwang 				return;
303a9643ea8Slogwang 		}
304a9643ea8Slogwang 	} else if (RTE_ETH_IS_IPV6_HDR(m->packet_type)) {
305a9643ea8Slogwang 		/* if this is an IPv6 packet */
3064418919fSjohnjiang 		struct rte_ipv6_hdr *ip_hdr;
307a9643ea8Slogwang 
308a9643ea8Slogwang 		/* Read the lookup key (i.e. ip_dst) from the input packet */
3094418919fSjohnjiang 		ip_hdr = rte_pktmbuf_mtod(m, struct rte_ipv6_hdr *);
310a9643ea8Slogwang 
311a9643ea8Slogwang 		/* Find destination port */
3122bfe3f2eSlogwang 		if (rte_lpm6_lookup(rxq->lpm6, ip_hdr->dst_addr,
3132bfe3f2eSlogwang 						&next_hop) == 0 &&
3142bfe3f2eSlogwang 				(enabled_port_mask & 1 << next_hop) != 0) {
3152bfe3f2eSlogwang 			port_out = next_hop;
316a9643ea8Slogwang 
317a9643ea8Slogwang 			/* Build transmission burst for new port */
318a9643ea8Slogwang 			len = qconf->tx_mbufs[port_out].len;
319a9643ea8Slogwang 		}
320a9643ea8Slogwang 
321a9643ea8Slogwang 		/* if we don't need to do any fragmentation */
322a9643ea8Slogwang 		if (likely (IPV6_MTU_DEFAULT >= m->pkt_len)) {
323a9643ea8Slogwang 			qconf->tx_mbufs[port_out].m_table[len] = m;
324a9643ea8Slogwang 			len2 = 1;
325a9643ea8Slogwang 		} else {
326a9643ea8Slogwang 			len2 = rte_ipv6_fragment_packet(m,
327a9643ea8Slogwang 				&qconf->tx_mbufs[port_out].m_table[len],
328a9643ea8Slogwang 				(uint16_t)(MBUF_TABLE_SIZE - len),
329a9643ea8Slogwang 				IPV6_MTU_DEFAULT,
330a9643ea8Slogwang 				rxq->direct_pool, rxq->indirect_pool);
331a9643ea8Slogwang 
332a9643ea8Slogwang 			/* Free input packet */
333a9643ea8Slogwang 			rte_pktmbuf_free(m);
334a9643ea8Slogwang 
335a9643ea8Slogwang 			/* If we fail to fragment the packet */
336a9643ea8Slogwang 			if (unlikely (len2 < 0))
337a9643ea8Slogwang 				return;
338a9643ea8Slogwang 		}
339a9643ea8Slogwang 	}
340a9643ea8Slogwang 	/* else, just forward the packet */
341a9643ea8Slogwang 	else {
342a9643ea8Slogwang 		qconf->tx_mbufs[port_out].m_table[len] = m;
343a9643ea8Slogwang 		len2 = 1;
344a9643ea8Slogwang 	}
345a9643ea8Slogwang 
346a9643ea8Slogwang 	for (i = len; i < len + len2; i ++) {
347a9643ea8Slogwang 		void *d_addr_bytes;
348a9643ea8Slogwang 
349a9643ea8Slogwang 		m = qconf->tx_mbufs[port_out].m_table[i];
3504418919fSjohnjiang 		struct rte_ether_hdr *eth_hdr = (struct rte_ether_hdr *)
3514418919fSjohnjiang 			rte_pktmbuf_prepend(m,
3524418919fSjohnjiang 				(uint16_t)sizeof(struct rte_ether_hdr));
353a9643ea8Slogwang 		if (eth_hdr == NULL) {
354a9643ea8Slogwang 			rte_panic("No headroom in mbuf.\n");
355a9643ea8Slogwang 		}
356a9643ea8Slogwang 
3574418919fSjohnjiang 		m->ol_flags |= ol_flags;
3584418919fSjohnjiang 		m->l2_len = sizeof(struct rte_ether_hdr);
359a9643ea8Slogwang 
360a9643ea8Slogwang 		/* 02:00:00:00:00:xx */
361a9643ea8Slogwang 		d_addr_bytes = &eth_hdr->d_addr.addr_bytes[0];
3624418919fSjohnjiang 		*((uint64_t *)d_addr_bytes) = 0x000000000002 +
3634418919fSjohnjiang 			((uint64_t)port_out << 40);
364a9643ea8Slogwang 
365a9643ea8Slogwang 		/* src addr */
3664418919fSjohnjiang 		rte_ether_addr_copy(&ports_eth_addr[port_out],
3674418919fSjohnjiang 				&eth_hdr->s_addr);
3684b05018fSfengbojiang 		eth_hdr->ether_type = ether_type;
369a9643ea8Slogwang 	}
370a9643ea8Slogwang 
371a9643ea8Slogwang 	len += len2;
372a9643ea8Slogwang 
373a9643ea8Slogwang 	if (likely(len < MAX_PKT_BURST)) {
374a9643ea8Slogwang 		qconf->tx_mbufs[port_out].len = (uint16_t)len;
375a9643ea8Slogwang 		return;
376a9643ea8Slogwang 	}
377a9643ea8Slogwang 
378a9643ea8Slogwang 	/* Transmit packets */
379a9643ea8Slogwang 	send_burst(qconf, (uint16_t)len, port_out);
380a9643ea8Slogwang 	qconf->tx_mbufs[port_out].len = 0;
381a9643ea8Slogwang }
382a9643ea8Slogwang 
383a9643ea8Slogwang /* main processing loop */
384a9643ea8Slogwang static int
main_loop(__rte_unused void * dummy)385*2d9fd380Sjfb8856606 main_loop(__rte_unused void *dummy)
386a9643ea8Slogwang {
387a9643ea8Slogwang 	struct rte_mbuf *pkts_burst[MAX_PKT_BURST];
388a9643ea8Slogwang 	unsigned lcore_id;
389a9643ea8Slogwang 	uint64_t prev_tsc, diff_tsc, cur_tsc;
390a9643ea8Slogwang 	int i, j, nb_rx;
3912bfe3f2eSlogwang 	uint16_t portid;
392a9643ea8Slogwang 	struct lcore_queue_conf *qconf;
393a9643ea8Slogwang 	const uint64_t drain_tsc = (rte_get_tsc_hz() + US_PER_S - 1) / US_PER_S * BURST_TX_DRAIN_US;
394a9643ea8Slogwang 
395a9643ea8Slogwang 	prev_tsc = 0;
396a9643ea8Slogwang 
397a9643ea8Slogwang 	lcore_id = rte_lcore_id();
398a9643ea8Slogwang 	qconf = &lcore_queue_conf[lcore_id];
399a9643ea8Slogwang 
400a9643ea8Slogwang 	if (qconf->n_rx_queue == 0) {
401a9643ea8Slogwang 		RTE_LOG(INFO, IP_FRAG, "lcore %u has nothing to do\n", lcore_id);
402a9643ea8Slogwang 		return 0;
403a9643ea8Slogwang 	}
404a9643ea8Slogwang 
405a9643ea8Slogwang 	RTE_LOG(INFO, IP_FRAG, "entering main loop on lcore %u\n", lcore_id);
406a9643ea8Slogwang 
407a9643ea8Slogwang 	for (i = 0; i < qconf->n_rx_queue; i++) {
408a9643ea8Slogwang 
409a9643ea8Slogwang 		portid = qconf->rx_queue_list[i].portid;
410a9643ea8Slogwang 		RTE_LOG(INFO, IP_FRAG, " -- lcoreid=%u portid=%d\n", lcore_id,
4112bfe3f2eSlogwang 				portid);
412a9643ea8Slogwang 	}
413a9643ea8Slogwang 
414a9643ea8Slogwang 	while (1) {
415a9643ea8Slogwang 
416a9643ea8Slogwang 		cur_tsc = rte_rdtsc();
417a9643ea8Slogwang 
418a9643ea8Slogwang 		/*
419a9643ea8Slogwang 		 * TX burst queue drain
420a9643ea8Slogwang 		 */
421a9643ea8Slogwang 		diff_tsc = cur_tsc - prev_tsc;
422a9643ea8Slogwang 		if (unlikely(diff_tsc > drain_tsc)) {
423a9643ea8Slogwang 
424a9643ea8Slogwang 			/*
425a9643ea8Slogwang 			 * This could be optimized (use queueid instead of
426a9643ea8Slogwang 			 * portid), but it is not called so often
427a9643ea8Slogwang 			 */
428a9643ea8Slogwang 			for (portid = 0; portid < RTE_MAX_ETHPORTS; portid++) {
429a9643ea8Slogwang 				if (qconf->tx_mbufs[portid].len == 0)
430a9643ea8Slogwang 					continue;
431a9643ea8Slogwang 				send_burst(&lcore_queue_conf[lcore_id],
432a9643ea8Slogwang 					   qconf->tx_mbufs[portid].len,
433a9643ea8Slogwang 					   portid);
434a9643ea8Slogwang 				qconf->tx_mbufs[portid].len = 0;
435a9643ea8Slogwang 			}
436a9643ea8Slogwang 
437a9643ea8Slogwang 			prev_tsc = cur_tsc;
438a9643ea8Slogwang 		}
439a9643ea8Slogwang 
440a9643ea8Slogwang 		/*
441a9643ea8Slogwang 		 * Read packet from RX queues
442a9643ea8Slogwang 		 */
443a9643ea8Slogwang 		for (i = 0; i < qconf->n_rx_queue; i++) {
444a9643ea8Slogwang 
445a9643ea8Slogwang 			portid = qconf->rx_queue_list[i].portid;
446a9643ea8Slogwang 			nb_rx = rte_eth_rx_burst(portid, 0, pkts_burst,
447a9643ea8Slogwang 						 MAX_PKT_BURST);
448a9643ea8Slogwang 
449a9643ea8Slogwang 			/* Prefetch first packets */
450a9643ea8Slogwang 			for (j = 0; j < PREFETCH_OFFSET && j < nb_rx; j++) {
451a9643ea8Slogwang 				rte_prefetch0(rte_pktmbuf_mtod(
452a9643ea8Slogwang 						pkts_burst[j], void *));
453a9643ea8Slogwang 			}
454a9643ea8Slogwang 
455a9643ea8Slogwang 			/* Prefetch and forward already prefetched packets */
456a9643ea8Slogwang 			for (j = 0; j < (nb_rx - PREFETCH_OFFSET); j++) {
457a9643ea8Slogwang 				rte_prefetch0(rte_pktmbuf_mtod(pkts_burst[
458a9643ea8Slogwang 						j + PREFETCH_OFFSET], void *));
459a9643ea8Slogwang 				l3fwd_simple_forward(pkts_burst[j], qconf, i, portid);
460a9643ea8Slogwang 			}
461a9643ea8Slogwang 
462a9643ea8Slogwang 			/* Forward remaining prefetched packets */
463a9643ea8Slogwang 			for (; j < nb_rx; j++) {
464a9643ea8Slogwang 				l3fwd_simple_forward(pkts_burst[j], qconf, i, portid);
465a9643ea8Slogwang 			}
466a9643ea8Slogwang 		}
467a9643ea8Slogwang 	}
468a9643ea8Slogwang }
469a9643ea8Slogwang 
470a9643ea8Slogwang /* display usage */
471a9643ea8Slogwang static void
print_usage(const char * prgname)472a9643ea8Slogwang print_usage(const char *prgname)
473a9643ea8Slogwang {
474a9643ea8Slogwang 	printf("%s [EAL options] -- -p PORTMASK [-q NQ]\n"
475a9643ea8Slogwang 	       "  -p PORTMASK: hexadecimal bitmask of ports to configure\n"
476a9643ea8Slogwang 	       "  -q NQ: number of queue (=ports) per lcore (default is 1)\n",
477a9643ea8Slogwang 	       prgname);
478a9643ea8Slogwang }
479a9643ea8Slogwang 
480a9643ea8Slogwang static int
parse_portmask(const char * portmask)481a9643ea8Slogwang parse_portmask(const char *portmask)
482a9643ea8Slogwang {
483a9643ea8Slogwang 	char *end = NULL;
484a9643ea8Slogwang 	unsigned long pm;
485a9643ea8Slogwang 
486a9643ea8Slogwang 	/* parse hexadecimal string */
487a9643ea8Slogwang 	pm = strtoul(portmask, &end, 16);
488a9643ea8Slogwang 	if ((portmask[0] == '\0') || (end == NULL) || (*end != '\0'))
489a9643ea8Slogwang 		return -1;
490a9643ea8Slogwang 
491a9643ea8Slogwang 	if (pm == 0)
492a9643ea8Slogwang 		return -1;
493a9643ea8Slogwang 
494a9643ea8Slogwang 	return pm;
495a9643ea8Slogwang }
496a9643ea8Slogwang 
497a9643ea8Slogwang static int
parse_nqueue(const char * q_arg)498a9643ea8Slogwang parse_nqueue(const char *q_arg)
499a9643ea8Slogwang {
500a9643ea8Slogwang 	char *end = NULL;
501a9643ea8Slogwang 	unsigned long n;
502a9643ea8Slogwang 
503a9643ea8Slogwang 	/* parse hexadecimal string */
504a9643ea8Slogwang 	n = strtoul(q_arg, &end, 10);
505a9643ea8Slogwang 	if ((q_arg[0] == '\0') || (end == NULL) || (*end != '\0'))
506a9643ea8Slogwang 		return -1;
507a9643ea8Slogwang 	if (n == 0)
508a9643ea8Slogwang 		return -1;
509a9643ea8Slogwang 	if (n >= MAX_RX_QUEUE_PER_LCORE)
510a9643ea8Slogwang 		return -1;
511a9643ea8Slogwang 
512a9643ea8Slogwang 	return n;
513a9643ea8Slogwang }
514a9643ea8Slogwang 
515a9643ea8Slogwang /* Parse the argument given in the command line of the application */
516a9643ea8Slogwang static int
parse_args(int argc,char ** argv)517a9643ea8Slogwang parse_args(int argc, char **argv)
518a9643ea8Slogwang {
519a9643ea8Slogwang 	int opt, ret;
520a9643ea8Slogwang 	char **argvopt;
521a9643ea8Slogwang 	int option_index;
522a9643ea8Slogwang 	char *prgname = argv[0];
523a9643ea8Slogwang 	static struct option lgopts[] = {
524a9643ea8Slogwang 		{NULL, 0, 0, 0}
525a9643ea8Slogwang 	};
526a9643ea8Slogwang 
527a9643ea8Slogwang 	argvopt = argv;
528a9643ea8Slogwang 
529a9643ea8Slogwang 	while ((opt = getopt_long(argc, argvopt, "p:q:",
530a9643ea8Slogwang 				  lgopts, &option_index)) != EOF) {
531a9643ea8Slogwang 
532a9643ea8Slogwang 		switch (opt) {
533a9643ea8Slogwang 		/* portmask */
534a9643ea8Slogwang 		case 'p':
535a9643ea8Slogwang 			enabled_port_mask = parse_portmask(optarg);
536a9643ea8Slogwang 			if (enabled_port_mask < 0) {
537a9643ea8Slogwang 				printf("invalid portmask\n");
538a9643ea8Slogwang 				print_usage(prgname);
539a9643ea8Slogwang 				return -1;
540a9643ea8Slogwang 			}
541a9643ea8Slogwang 			break;
542a9643ea8Slogwang 
543a9643ea8Slogwang 		/* nqueue */
544a9643ea8Slogwang 		case 'q':
545a9643ea8Slogwang 			rx_queue_per_lcore = parse_nqueue(optarg);
546a9643ea8Slogwang 			if (rx_queue_per_lcore < 0) {
547a9643ea8Slogwang 				printf("invalid queue number\n");
548a9643ea8Slogwang 				print_usage(prgname);
549a9643ea8Slogwang 				return -1;
550a9643ea8Slogwang 			}
551a9643ea8Slogwang 			break;
552a9643ea8Slogwang 
553a9643ea8Slogwang 		/* long options */
554a9643ea8Slogwang 		case 0:
555a9643ea8Slogwang 			print_usage(prgname);
556a9643ea8Slogwang 			return -1;
557a9643ea8Slogwang 
558a9643ea8Slogwang 		default:
559a9643ea8Slogwang 			print_usage(prgname);
560a9643ea8Slogwang 			return -1;
561a9643ea8Slogwang 		}
562a9643ea8Slogwang 	}
563a9643ea8Slogwang 
564a9643ea8Slogwang 	if (enabled_port_mask == 0) {
565a9643ea8Slogwang 		printf("portmask not specified\n");
566a9643ea8Slogwang 		print_usage(prgname);
567a9643ea8Slogwang 		return -1;
568a9643ea8Slogwang 	}
569a9643ea8Slogwang 
570a9643ea8Slogwang 	if (optind >= 0)
571a9643ea8Slogwang 		argv[optind-1] = prgname;
572a9643ea8Slogwang 
573a9643ea8Slogwang 	ret = optind-1;
5742bfe3f2eSlogwang 	optind = 1; /* reset getopt lib */
575a9643ea8Slogwang 	return ret;
576a9643ea8Slogwang }
577a9643ea8Slogwang 
578a9643ea8Slogwang static void
print_ethaddr(const char * name,struct rte_ether_addr * eth_addr)5794418919fSjohnjiang print_ethaddr(const char *name, struct rte_ether_addr *eth_addr)
580a9643ea8Slogwang {
5814418919fSjohnjiang 	char buf[RTE_ETHER_ADDR_FMT_SIZE];
5824418919fSjohnjiang 	rte_ether_format_addr(buf, RTE_ETHER_ADDR_FMT_SIZE, eth_addr);
583a9643ea8Slogwang 	printf("%s%s", name, buf);
584a9643ea8Slogwang }
585a9643ea8Slogwang 
586a9643ea8Slogwang /* Check the link status of all ports in up to 9s, and print them finally */
587a9643ea8Slogwang static void
check_all_ports_link_status(uint32_t port_mask)588d30ea906Sjfb8856606 check_all_ports_link_status(uint32_t port_mask)
589a9643ea8Slogwang {
590a9643ea8Slogwang #define CHECK_INTERVAL 100 /* 100ms */
591a9643ea8Slogwang #define MAX_CHECK_TIME 90 /* 9s (90 * 100ms) in total */
5922bfe3f2eSlogwang 	uint16_t portid;
5932bfe3f2eSlogwang 	uint8_t count, all_ports_up, print_flag = 0;
594a9643ea8Slogwang 	struct rte_eth_link link;
5954418919fSjohnjiang 	int ret;
596*2d9fd380Sjfb8856606 	char link_status_text[RTE_ETH_LINK_MAX_STR_LEN];
597a9643ea8Slogwang 
598a9643ea8Slogwang 	printf("\nChecking link status");
599a9643ea8Slogwang 	fflush(stdout);
600a9643ea8Slogwang 	for (count = 0; count <= MAX_CHECK_TIME; count++) {
601a9643ea8Slogwang 		all_ports_up = 1;
602d30ea906Sjfb8856606 		RTE_ETH_FOREACH_DEV(portid) {
603a9643ea8Slogwang 			if ((port_mask & (1 << portid)) == 0)
604a9643ea8Slogwang 				continue;
605a9643ea8Slogwang 			memset(&link, 0, sizeof(link));
6064418919fSjohnjiang 			ret = rte_eth_link_get_nowait(portid, &link);
6074418919fSjohnjiang 			if (ret < 0) {
6084418919fSjohnjiang 				all_ports_up = 0;
6094418919fSjohnjiang 				if (print_flag == 1)
6104418919fSjohnjiang 					printf("Port %u link get failed: %s\n",
6114418919fSjohnjiang 						portid, rte_strerror(-ret));
6124418919fSjohnjiang 				continue;
6134418919fSjohnjiang 			}
614a9643ea8Slogwang 			/* print link status if flag set */
615a9643ea8Slogwang 			if (print_flag == 1) {
616*2d9fd380Sjfb8856606 				rte_eth_link_to_str(link_status_text,
617*2d9fd380Sjfb8856606 					sizeof(link_status_text), &link);
618*2d9fd380Sjfb8856606 				printf("Port %d %s\n", portid,
619*2d9fd380Sjfb8856606 				       link_status_text);
620a9643ea8Slogwang 				continue;
621a9643ea8Slogwang 			}
622a9643ea8Slogwang 			/* clear all_ports_up flag if any link down */
623a9643ea8Slogwang 			if (link.link_status == ETH_LINK_DOWN) {
624a9643ea8Slogwang 				all_ports_up = 0;
625a9643ea8Slogwang 				break;
626a9643ea8Slogwang 			}
627a9643ea8Slogwang 		}
628a9643ea8Slogwang 		/* after finally printing all link status, get out */
629a9643ea8Slogwang 		if (print_flag == 1)
630a9643ea8Slogwang 			break;
631a9643ea8Slogwang 
632a9643ea8Slogwang 		if (all_ports_up == 0) {
633a9643ea8Slogwang 			printf(".");
634a9643ea8Slogwang 			fflush(stdout);
635a9643ea8Slogwang 			rte_delay_ms(CHECK_INTERVAL);
636a9643ea8Slogwang 		}
637a9643ea8Slogwang 
638a9643ea8Slogwang 		/* set the print_flag if all ports up or timeout */
639a9643ea8Slogwang 		if (all_ports_up == 1 || count == (MAX_CHECK_TIME - 1)) {
640a9643ea8Slogwang 			print_flag = 1;
641a9643ea8Slogwang 			printf("\ndone\n");
642a9643ea8Slogwang 		}
643a9643ea8Slogwang 	}
644a9643ea8Slogwang }
645a9643ea8Slogwang 
6464418919fSjohnjiang /* Check L3 packet type detection capability of the NIC port */
6472bfe3f2eSlogwang static int
check_ptype(int portid)6482bfe3f2eSlogwang check_ptype(int portid)
6492bfe3f2eSlogwang {
6502bfe3f2eSlogwang 	int i, ret;
6512bfe3f2eSlogwang 	int ptype_l3_ipv4 = 0, ptype_l3_ipv6 = 0;
6522bfe3f2eSlogwang 	uint32_t ptype_mask = RTE_PTYPE_L3_MASK;
6532bfe3f2eSlogwang 
6542bfe3f2eSlogwang 	ret = rte_eth_dev_get_supported_ptypes(portid, ptype_mask, NULL, 0);
6552bfe3f2eSlogwang 	if (ret <= 0)
6562bfe3f2eSlogwang 		return 0;
6572bfe3f2eSlogwang 
6582bfe3f2eSlogwang 	uint32_t ptypes[ret];
6592bfe3f2eSlogwang 
6602bfe3f2eSlogwang 	ret = rte_eth_dev_get_supported_ptypes(portid, ptype_mask, ptypes, ret);
6612bfe3f2eSlogwang 	for (i = 0; i < ret; ++i) {
6622bfe3f2eSlogwang 		if (ptypes[i] & RTE_PTYPE_L3_IPV4)
6632bfe3f2eSlogwang 			ptype_l3_ipv4 = 1;
6642bfe3f2eSlogwang 		if (ptypes[i] & RTE_PTYPE_L3_IPV6)
6652bfe3f2eSlogwang 			ptype_l3_ipv6 = 1;
6662bfe3f2eSlogwang 	}
6672bfe3f2eSlogwang 
6682bfe3f2eSlogwang 	if (ptype_l3_ipv4 == 0)
6692bfe3f2eSlogwang 		printf("port %d cannot parse RTE_PTYPE_L3_IPV4\n", portid);
6702bfe3f2eSlogwang 
6712bfe3f2eSlogwang 	if (ptype_l3_ipv6 == 0)
6722bfe3f2eSlogwang 		printf("port %d cannot parse RTE_PTYPE_L3_IPV6\n", portid);
6732bfe3f2eSlogwang 
6742bfe3f2eSlogwang 	if (ptype_l3_ipv4 && ptype_l3_ipv6)
6752bfe3f2eSlogwang 		return 1;
6762bfe3f2eSlogwang 
6772bfe3f2eSlogwang 	return 0;
6782bfe3f2eSlogwang 
6792bfe3f2eSlogwang }
6802bfe3f2eSlogwang 
6812bfe3f2eSlogwang /* Parse packet type of a packet by SW */
6822bfe3f2eSlogwang static inline void
parse_ptype(struct rte_mbuf * m)6832bfe3f2eSlogwang parse_ptype(struct rte_mbuf *m)
6842bfe3f2eSlogwang {
6854418919fSjohnjiang 	struct rte_ether_hdr *eth_hdr;
6862bfe3f2eSlogwang 	uint32_t packet_type = RTE_PTYPE_UNKNOWN;
6872bfe3f2eSlogwang 	uint16_t ether_type;
6882bfe3f2eSlogwang 
6894418919fSjohnjiang 	eth_hdr = rte_pktmbuf_mtod(m, struct rte_ether_hdr *);
6902bfe3f2eSlogwang 	ether_type = eth_hdr->ether_type;
6914418919fSjohnjiang 	if (ether_type == rte_cpu_to_be_16(RTE_ETHER_TYPE_IPV4))
6922bfe3f2eSlogwang 		packet_type |= RTE_PTYPE_L3_IPV4_EXT_UNKNOWN;
6934418919fSjohnjiang 	else if (ether_type == rte_cpu_to_be_16(RTE_ETHER_TYPE_IPV6))
6942bfe3f2eSlogwang 		packet_type |= RTE_PTYPE_L3_IPV6_EXT_UNKNOWN;
6952bfe3f2eSlogwang 
6962bfe3f2eSlogwang 	m->packet_type = packet_type;
6972bfe3f2eSlogwang }
6982bfe3f2eSlogwang 
6992bfe3f2eSlogwang /* callback function to detect packet type for a queue of a port */
7002bfe3f2eSlogwang static uint16_t
cb_parse_ptype(uint16_t port __rte_unused,uint16_t queue __rte_unused,struct rte_mbuf * pkts[],uint16_t nb_pkts,uint16_t max_pkts __rte_unused,void * user_param __rte_unused)7012bfe3f2eSlogwang cb_parse_ptype(uint16_t port __rte_unused, uint16_t queue __rte_unused,
7022bfe3f2eSlogwang 		   struct rte_mbuf *pkts[], uint16_t nb_pkts,
7032bfe3f2eSlogwang 		   uint16_t max_pkts __rte_unused,
7042bfe3f2eSlogwang 		   void *user_param __rte_unused)
7052bfe3f2eSlogwang {
7062bfe3f2eSlogwang 	uint16_t i;
7072bfe3f2eSlogwang 
7082bfe3f2eSlogwang 	for (i = 0; i < nb_pkts; ++i)
7092bfe3f2eSlogwang 		parse_ptype(pkts[i]);
7102bfe3f2eSlogwang 
7112bfe3f2eSlogwang 	return nb_pkts;
7122bfe3f2eSlogwang }
7132bfe3f2eSlogwang 
714a9643ea8Slogwang static int
init_routing_table(void)715a9643ea8Slogwang init_routing_table(void)
716a9643ea8Slogwang {
717a9643ea8Slogwang 	struct rte_lpm *lpm;
718a9643ea8Slogwang 	struct rte_lpm6 *lpm6;
719a9643ea8Slogwang 	int socket, ret;
720a9643ea8Slogwang 	unsigned i;
721a9643ea8Slogwang 
722a9643ea8Slogwang 	for (socket = 0; socket < RTE_MAX_NUMA_NODES; socket++) {
723a9643ea8Slogwang 		if (socket_lpm[socket]) {
724a9643ea8Slogwang 			lpm = socket_lpm[socket];
725a9643ea8Slogwang 			/* populate the LPM table */
726a9643ea8Slogwang 			for (i = 0; i < RTE_DIM(l3fwd_ipv4_route_array); i++) {
727a9643ea8Slogwang 				ret = rte_lpm_add(lpm,
728a9643ea8Slogwang 					l3fwd_ipv4_route_array[i].ip,
729a9643ea8Slogwang 					l3fwd_ipv4_route_array[i].depth,
730a9643ea8Slogwang 					l3fwd_ipv4_route_array[i].if_out);
731a9643ea8Slogwang 
732a9643ea8Slogwang 				if (ret < 0) {
733a9643ea8Slogwang 					RTE_LOG(ERR, IP_FRAG, "Unable to add entry %i to the l3fwd "
734a9643ea8Slogwang 						"LPM table\n", i);
735a9643ea8Slogwang 					return -1;
736a9643ea8Slogwang 				}
737a9643ea8Slogwang 
738a9643ea8Slogwang 				RTE_LOG(INFO, IP_FRAG, "Socket %i: adding route " IPv4_BYTES_FMT
739a9643ea8Slogwang 						"/%d (port %d)\n",
740a9643ea8Slogwang 					socket,
741a9643ea8Slogwang 					IPv4_BYTES(l3fwd_ipv4_route_array[i].ip),
742a9643ea8Slogwang 					l3fwd_ipv4_route_array[i].depth,
743a9643ea8Slogwang 					l3fwd_ipv4_route_array[i].if_out);
744a9643ea8Slogwang 			}
745a9643ea8Slogwang 		}
746a9643ea8Slogwang 
747a9643ea8Slogwang 		if (socket_lpm6[socket]) {
748a9643ea8Slogwang 			lpm6 = socket_lpm6[socket];
749a9643ea8Slogwang 			/* populate the LPM6 table */
750a9643ea8Slogwang 			for (i = 0; i < RTE_DIM(l3fwd_ipv6_route_array); i++) {
751a9643ea8Slogwang 				ret = rte_lpm6_add(lpm6,
752a9643ea8Slogwang 					l3fwd_ipv6_route_array[i].ip,
753a9643ea8Slogwang 					l3fwd_ipv6_route_array[i].depth,
754a9643ea8Slogwang 					l3fwd_ipv6_route_array[i].if_out);
755a9643ea8Slogwang 
756a9643ea8Slogwang 				if (ret < 0) {
757a9643ea8Slogwang 					RTE_LOG(ERR, IP_FRAG, "Unable to add entry %i to the l3fwd "
758a9643ea8Slogwang 						"LPM6 table\n", i);
759a9643ea8Slogwang 					return -1;
760a9643ea8Slogwang 				}
761a9643ea8Slogwang 
762a9643ea8Slogwang 				RTE_LOG(INFO, IP_FRAG, "Socket %i: adding route " IPv6_BYTES_FMT
763a9643ea8Slogwang 						"/%d (port %d)\n",
764a9643ea8Slogwang 					socket,
765a9643ea8Slogwang 					IPv6_BYTES(l3fwd_ipv6_route_array[i].ip),
766a9643ea8Slogwang 					l3fwd_ipv6_route_array[i].depth,
767a9643ea8Slogwang 					l3fwd_ipv6_route_array[i].if_out);
768a9643ea8Slogwang 			}
769a9643ea8Slogwang 		}
770a9643ea8Slogwang 	}
771a9643ea8Slogwang 	return 0;
772a9643ea8Slogwang }
773a9643ea8Slogwang 
774a9643ea8Slogwang static int
init_mem(void)775a9643ea8Slogwang init_mem(void)
776a9643ea8Slogwang {
777a9643ea8Slogwang 	char buf[PATH_MAX];
778a9643ea8Slogwang 	struct rte_mempool *mp;
779a9643ea8Slogwang 	struct rte_lpm *lpm;
780a9643ea8Slogwang 	struct rte_lpm6 *lpm6;
781a9643ea8Slogwang 	struct rte_lpm_config lpm_config;
782a9643ea8Slogwang 	int socket;
783a9643ea8Slogwang 	unsigned lcore_id;
784a9643ea8Slogwang 
785a9643ea8Slogwang 	/* traverse through lcores and initialize structures on each socket */
786a9643ea8Slogwang 
787a9643ea8Slogwang 	for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
788a9643ea8Slogwang 
789a9643ea8Slogwang 		if (rte_lcore_is_enabled(lcore_id) == 0)
790a9643ea8Slogwang 			continue;
791a9643ea8Slogwang 
792a9643ea8Slogwang 		socket = rte_lcore_to_socket_id(lcore_id);
793a9643ea8Slogwang 
794a9643ea8Slogwang 		if (socket == SOCKET_ID_ANY)
795a9643ea8Slogwang 			socket = 0;
796a9643ea8Slogwang 
797a9643ea8Slogwang 		if (socket_direct_pool[socket] == NULL) {
798a9643ea8Slogwang 			RTE_LOG(INFO, IP_FRAG, "Creating direct mempool on socket %i\n",
799a9643ea8Slogwang 					socket);
800a9643ea8Slogwang 			snprintf(buf, sizeof(buf), "pool_direct_%i", socket);
801a9643ea8Slogwang 
802a9643ea8Slogwang 			mp = rte_pktmbuf_pool_create(buf, NB_MBUF, 32,
803a9643ea8Slogwang 				0, RTE_MBUF_DEFAULT_BUF_SIZE, socket);
804a9643ea8Slogwang 			if (mp == NULL) {
805a9643ea8Slogwang 				RTE_LOG(ERR, IP_FRAG, "Cannot create direct mempool\n");
806a9643ea8Slogwang 				return -1;
807a9643ea8Slogwang 			}
808a9643ea8Slogwang 			socket_direct_pool[socket] = mp;
809a9643ea8Slogwang 		}
810a9643ea8Slogwang 
811a9643ea8Slogwang 		if (socket_indirect_pool[socket] == NULL) {
812a9643ea8Slogwang 			RTE_LOG(INFO, IP_FRAG, "Creating indirect mempool on socket %i\n",
813a9643ea8Slogwang 					socket);
814a9643ea8Slogwang 			snprintf(buf, sizeof(buf), "pool_indirect_%i", socket);
815a9643ea8Slogwang 
816a9643ea8Slogwang 			mp = rte_pktmbuf_pool_create(buf, NB_MBUF, 32, 0, 0,
817a9643ea8Slogwang 				socket);
818a9643ea8Slogwang 			if (mp == NULL) {
819a9643ea8Slogwang 				RTE_LOG(ERR, IP_FRAG, "Cannot create indirect mempool\n");
820a9643ea8Slogwang 				return -1;
821a9643ea8Slogwang 			}
822a9643ea8Slogwang 			socket_indirect_pool[socket] = mp;
823a9643ea8Slogwang 		}
824a9643ea8Slogwang 
825a9643ea8Slogwang 		if (socket_lpm[socket] == NULL) {
826a9643ea8Slogwang 			RTE_LOG(INFO, IP_FRAG, "Creating LPM table on socket %i\n", socket);
827a9643ea8Slogwang 			snprintf(buf, sizeof(buf), "IP_FRAG_LPM_%i", socket);
828a9643ea8Slogwang 
829a9643ea8Slogwang 			lpm_config.max_rules = LPM_MAX_RULES;
830a9643ea8Slogwang 			lpm_config.number_tbl8s = 256;
831a9643ea8Slogwang 			lpm_config.flags = 0;
832a9643ea8Slogwang 
833a9643ea8Slogwang 			lpm = rte_lpm_create(buf, socket, &lpm_config);
834a9643ea8Slogwang 			if (lpm == NULL) {
835a9643ea8Slogwang 				RTE_LOG(ERR, IP_FRAG, "Cannot create LPM table\n");
836a9643ea8Slogwang 				return -1;
837a9643ea8Slogwang 			}
838a9643ea8Slogwang 			socket_lpm[socket] = lpm;
839a9643ea8Slogwang 		}
840a9643ea8Slogwang 
841a9643ea8Slogwang 		if (socket_lpm6[socket] == NULL) {
842a9643ea8Slogwang 			RTE_LOG(INFO, IP_FRAG, "Creating LPM6 table on socket %i\n", socket);
843a9643ea8Slogwang 			snprintf(buf, sizeof(buf), "IP_FRAG_LPM_%i", socket);
844a9643ea8Slogwang 
845a9643ea8Slogwang 			lpm6 = rte_lpm6_create(buf, socket, &lpm6_config);
846a9643ea8Slogwang 			if (lpm6 == NULL) {
847a9643ea8Slogwang 				RTE_LOG(ERR, IP_FRAG, "Cannot create LPM table\n");
848a9643ea8Slogwang 				return -1;
849a9643ea8Slogwang 			}
850a9643ea8Slogwang 			socket_lpm6[socket] = lpm6;
851a9643ea8Slogwang 		}
852a9643ea8Slogwang 	}
853a9643ea8Slogwang 
854a9643ea8Slogwang 	return 0;
855a9643ea8Slogwang }
856a9643ea8Slogwang 
857a9643ea8Slogwang int
main(int argc,char ** argv)858a9643ea8Slogwang main(int argc, char **argv)
859a9643ea8Slogwang {
860a9643ea8Slogwang 	struct lcore_queue_conf *qconf;
861a9643ea8Slogwang 	struct rte_eth_dev_info dev_info;
862a9643ea8Slogwang 	struct rte_eth_txconf *txconf;
863a9643ea8Slogwang 	struct rx_queue *rxq;
864a9643ea8Slogwang 	int socket, ret;
865d30ea906Sjfb8856606 	uint16_t nb_ports;
866a9643ea8Slogwang 	uint16_t queueid = 0;
867a9643ea8Slogwang 	unsigned lcore_id = 0, rx_lcore_id = 0;
868a9643ea8Slogwang 	uint32_t n_tx_queue, nb_lcores;
8692bfe3f2eSlogwang 	uint16_t portid;
870a9643ea8Slogwang 
871a9643ea8Slogwang 	/* init EAL */
872a9643ea8Slogwang 	ret = rte_eal_init(argc, argv);
873a9643ea8Slogwang 	if (ret < 0)
874a9643ea8Slogwang 		rte_exit(EXIT_FAILURE, "rte_eal_init failed");
875a9643ea8Slogwang 	argc -= ret;
876a9643ea8Slogwang 	argv += ret;
877a9643ea8Slogwang 
878a9643ea8Slogwang 	/* parse application arguments (after the EAL ones) */
879a9643ea8Slogwang 	ret = parse_args(argc, argv);
880a9643ea8Slogwang 	if (ret < 0)
881a9643ea8Slogwang 		rte_exit(EXIT_FAILURE, "Invalid arguments");
882a9643ea8Slogwang 
883d30ea906Sjfb8856606 	nb_ports = rte_eth_dev_count_avail();
884a9643ea8Slogwang 	if (nb_ports == 0)
885a9643ea8Slogwang 		rte_exit(EXIT_FAILURE, "No ports found!\n");
886a9643ea8Slogwang 
887a9643ea8Slogwang 	nb_lcores = rte_lcore_count();
888a9643ea8Slogwang 
889a9643ea8Slogwang 	/* initialize structures (mempools, lpm etc.) */
890a9643ea8Slogwang 	if (init_mem() < 0)
891a9643ea8Slogwang 		rte_panic("Cannot initialize memory structures!\n");
892a9643ea8Slogwang 
893a9643ea8Slogwang 	/* check if portmask has non-existent ports */
894a9643ea8Slogwang 	if (enabled_port_mask & ~(RTE_LEN2MASK(nb_ports, unsigned)))
895a9643ea8Slogwang 		rte_exit(EXIT_FAILURE, "Non-existent ports in portmask!\n");
896a9643ea8Slogwang 
897a9643ea8Slogwang 	/* initialize all ports */
898d30ea906Sjfb8856606 	RTE_ETH_FOREACH_DEV(portid) {
899d30ea906Sjfb8856606 		struct rte_eth_conf local_port_conf = port_conf;
900d30ea906Sjfb8856606 		struct rte_eth_rxconf rxq_conf;
901d30ea906Sjfb8856606 
902a9643ea8Slogwang 		/* skip ports that are not enabled */
903a9643ea8Slogwang 		if ((enabled_port_mask & (1 << portid)) == 0) {
904a9643ea8Slogwang 			printf("Skipping disabled port %d\n", portid);
905a9643ea8Slogwang 			continue;
906a9643ea8Slogwang 		}
907a9643ea8Slogwang 
908a9643ea8Slogwang 		qconf = &lcore_queue_conf[rx_lcore_id];
909a9643ea8Slogwang 
9102bfe3f2eSlogwang 		/* limit the frame size to the maximum supported by NIC */
9114418919fSjohnjiang 		ret = rte_eth_dev_info_get(portid, &dev_info);
9124418919fSjohnjiang 		if (ret != 0)
9134418919fSjohnjiang 			rte_exit(EXIT_FAILURE,
9144418919fSjohnjiang 				"Error during getting device (port %u) info: %s\n",
9154418919fSjohnjiang 				portid, strerror(-ret));
9164418919fSjohnjiang 
917d30ea906Sjfb8856606 		local_port_conf.rxmode.max_rx_pkt_len = RTE_MIN(
918d30ea906Sjfb8856606 		    dev_info.max_rx_pktlen,
919d30ea906Sjfb8856606 		    local_port_conf.rxmode.max_rx_pkt_len);
9202bfe3f2eSlogwang 
921a9643ea8Slogwang 		/* get the lcore_id for this port */
922a9643ea8Slogwang 		while (rte_lcore_is_enabled(rx_lcore_id) == 0 ||
923a9643ea8Slogwang 		       qconf->n_rx_queue == (unsigned)rx_queue_per_lcore) {
924a9643ea8Slogwang 
925a9643ea8Slogwang 			rx_lcore_id ++;
926a9643ea8Slogwang 			if (rx_lcore_id >= RTE_MAX_LCORE)
927a9643ea8Slogwang 				rte_exit(EXIT_FAILURE, "Not enough cores\n");
928a9643ea8Slogwang 
929a9643ea8Slogwang 			qconf = &lcore_queue_conf[rx_lcore_id];
930a9643ea8Slogwang 		}
931a9643ea8Slogwang 
932a9643ea8Slogwang 		socket = (int) rte_lcore_to_socket_id(rx_lcore_id);
933a9643ea8Slogwang 		if (socket == SOCKET_ID_ANY)
934a9643ea8Slogwang 			socket = 0;
935a9643ea8Slogwang 
936a9643ea8Slogwang 		rxq = &qconf->rx_queue_list[qconf->n_rx_queue];
937a9643ea8Slogwang 		rxq->portid = portid;
938a9643ea8Slogwang 		rxq->direct_pool = socket_direct_pool[socket];
939a9643ea8Slogwang 		rxq->indirect_pool = socket_indirect_pool[socket];
940a9643ea8Slogwang 		rxq->lpm = socket_lpm[socket];
941a9643ea8Slogwang 		rxq->lpm6 = socket_lpm6[socket];
942a9643ea8Slogwang 		qconf->n_rx_queue++;
943a9643ea8Slogwang 
944a9643ea8Slogwang 		/* init port */
945a9643ea8Slogwang 		printf("Initializing port %d on lcore %u...", portid,
946a9643ea8Slogwang 		       rx_lcore_id);
947a9643ea8Slogwang 		fflush(stdout);
948a9643ea8Slogwang 
949a9643ea8Slogwang 		n_tx_queue = nb_lcores;
950a9643ea8Slogwang 		if (n_tx_queue > MAX_TX_QUEUE_PER_PORT)
951a9643ea8Slogwang 			n_tx_queue = MAX_TX_QUEUE_PER_PORT;
952a9643ea8Slogwang 		ret = rte_eth_dev_configure(portid, 1, (uint16_t)n_tx_queue,
953d30ea906Sjfb8856606 					    &local_port_conf);
954a9643ea8Slogwang 		if (ret < 0) {
955a9643ea8Slogwang 			printf("\n");
956a9643ea8Slogwang 			rte_exit(EXIT_FAILURE, "Cannot configure device: "
957a9643ea8Slogwang 				"err=%d, port=%d\n",
958a9643ea8Slogwang 				ret, portid);
959a9643ea8Slogwang 		}
960a9643ea8Slogwang 
9614418919fSjohnjiang 		/* set the mtu to the maximum received packet size */
9624418919fSjohnjiang 		ret = rte_eth_dev_set_mtu(portid,
9634418919fSjohnjiang 			local_port_conf.rxmode.max_rx_pkt_len - MTU_OVERHEAD);
9644418919fSjohnjiang 		if (ret < 0) {
9654418919fSjohnjiang 			printf("\n");
9664418919fSjohnjiang 			rte_exit(EXIT_FAILURE, "Set MTU failed: "
9674418919fSjohnjiang 				"err=%d, port=%d\n",
9684418919fSjohnjiang 			ret, portid);
9694418919fSjohnjiang 		}
9704418919fSjohnjiang 
9712bfe3f2eSlogwang 		ret = rte_eth_dev_adjust_nb_rx_tx_desc(portid, &nb_rxd,
9722bfe3f2eSlogwang 					    &nb_txd);
9732bfe3f2eSlogwang 		if (ret < 0) {
9742bfe3f2eSlogwang 			printf("\n");
9752bfe3f2eSlogwang 			rte_exit(EXIT_FAILURE, "Cannot adjust number of "
9762bfe3f2eSlogwang 				"descriptors: err=%d, port=%d\n", ret, portid);
9772bfe3f2eSlogwang 		}
9782bfe3f2eSlogwang 
979a9643ea8Slogwang 		/* init one RX queue */
980d30ea906Sjfb8856606 		rxq_conf = dev_info.default_rxconf;
981d30ea906Sjfb8856606 		rxq_conf.offloads = local_port_conf.rxmode.offloads;
982a9643ea8Slogwang 		ret = rte_eth_rx_queue_setup(portid, 0, nb_rxd,
983d30ea906Sjfb8856606 					     socket, &rxq_conf,
984a9643ea8Slogwang 					     socket_direct_pool[socket]);
985a9643ea8Slogwang 		if (ret < 0) {
986a9643ea8Slogwang 			printf("\n");
987a9643ea8Slogwang 			rte_exit(EXIT_FAILURE, "rte_eth_rx_queue_setup: "
988a9643ea8Slogwang 				"err=%d, port=%d\n",
989a9643ea8Slogwang 				ret, portid);
990a9643ea8Slogwang 		}
991a9643ea8Slogwang 
9924418919fSjohnjiang 		ret = rte_eth_macaddr_get(portid, &ports_eth_addr[portid]);
9934418919fSjohnjiang 		if (ret < 0) {
9944418919fSjohnjiang 			printf("\n");
9954418919fSjohnjiang 			rte_exit(EXIT_FAILURE,
9964418919fSjohnjiang 				"rte_eth_macaddr_get: err=%d, port=%d\n",
9974418919fSjohnjiang 				ret, portid);
9984418919fSjohnjiang 		}
9994418919fSjohnjiang 
1000a9643ea8Slogwang 		print_ethaddr(" Address:", &ports_eth_addr[portid]);
1001a9643ea8Slogwang 		printf("\n");
1002a9643ea8Slogwang 
1003a9643ea8Slogwang 		/* init one TX queue per couple (lcore,port) */
10044418919fSjohnjiang 		ret = rte_eth_dev_info_get(portid, &dev_info);
10054418919fSjohnjiang 		if (ret != 0)
10064418919fSjohnjiang 			rte_exit(EXIT_FAILURE,
10074418919fSjohnjiang 				"Error during getting device (port %u) info: %s\n",
10084418919fSjohnjiang 				portid, strerror(-ret));
10094418919fSjohnjiang 
1010a9643ea8Slogwang 		queueid = 0;
1011a9643ea8Slogwang 		for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
1012a9643ea8Slogwang 			if (rte_lcore_is_enabled(lcore_id) == 0)
1013a9643ea8Slogwang 				continue;
1014a9643ea8Slogwang 
10154b05018fSfengbojiang 			if (queueid >= dev_info.nb_tx_queues)
10164b05018fSfengbojiang 				break;
10174b05018fSfengbojiang 
1018a9643ea8Slogwang 			socket = (int) rte_lcore_to_socket_id(lcore_id);
1019a9643ea8Slogwang 			printf("txq=%u,%d ", lcore_id, queueid);
1020a9643ea8Slogwang 			fflush(stdout);
1021a9643ea8Slogwang 
1022a9643ea8Slogwang 			txconf = &dev_info.default_txconf;
1023d30ea906Sjfb8856606 			txconf->offloads = local_port_conf.txmode.offloads;
1024a9643ea8Slogwang 			ret = rte_eth_tx_queue_setup(portid, queueid, nb_txd,
1025a9643ea8Slogwang 						     socket, txconf);
1026a9643ea8Slogwang 			if (ret < 0) {
1027a9643ea8Slogwang 				printf("\n");
1028a9643ea8Slogwang 				rte_exit(EXIT_FAILURE, "rte_eth_tx_queue_setup: "
1029a9643ea8Slogwang 					"err=%d, port=%d\n", ret, portid);
1030a9643ea8Slogwang 			}
1031a9643ea8Slogwang 
1032a9643ea8Slogwang 			qconf = &lcore_queue_conf[lcore_id];
1033a9643ea8Slogwang 			qconf->tx_queue_id[portid] = queueid;
1034a9643ea8Slogwang 			queueid++;
1035a9643ea8Slogwang 		}
1036a9643ea8Slogwang 
1037a9643ea8Slogwang 		printf("\n");
1038a9643ea8Slogwang 	}
1039a9643ea8Slogwang 
1040a9643ea8Slogwang 	printf("\n");
1041a9643ea8Slogwang 
1042a9643ea8Slogwang 	/* start ports */
1043d30ea906Sjfb8856606 	RTE_ETH_FOREACH_DEV(portid) {
1044a9643ea8Slogwang 		if ((enabled_port_mask & (1 << portid)) == 0) {
1045a9643ea8Slogwang 			continue;
1046a9643ea8Slogwang 		}
1047a9643ea8Slogwang 		/* Start device */
1048a9643ea8Slogwang 		ret = rte_eth_dev_start(portid);
1049a9643ea8Slogwang 		if (ret < 0)
1050a9643ea8Slogwang 			rte_exit(EXIT_FAILURE, "rte_eth_dev_start: err=%d, port=%d\n",
1051a9643ea8Slogwang 				ret, portid);
1052a9643ea8Slogwang 
10534418919fSjohnjiang 		ret = rte_eth_promiscuous_enable(portid);
10544418919fSjohnjiang 		if (ret != 0)
10554418919fSjohnjiang 			rte_exit(EXIT_FAILURE,
10564418919fSjohnjiang 				"rte_eth_promiscuous_enable: err=%s, port=%d\n",
10574418919fSjohnjiang 				rte_strerror(-ret), portid);
10582bfe3f2eSlogwang 
10592bfe3f2eSlogwang 		if (check_ptype(portid) == 0) {
10602bfe3f2eSlogwang 			rte_eth_add_rx_callback(portid, 0, cb_parse_ptype, NULL);
10612bfe3f2eSlogwang 			printf("Add Rx callback function to detect L3 packet type by SW :"
10622bfe3f2eSlogwang 				" port = %d\n", portid);
10632bfe3f2eSlogwang 		}
1064a9643ea8Slogwang 	}
1065a9643ea8Slogwang 
1066a9643ea8Slogwang 	if (init_routing_table() < 0)
1067a9643ea8Slogwang 		rte_exit(EXIT_FAILURE, "Cannot init routing table\n");
1068a9643ea8Slogwang 
1069d30ea906Sjfb8856606 	check_all_ports_link_status(enabled_port_mask);
1070a9643ea8Slogwang 
1071a9643ea8Slogwang 	/* launch per-lcore init on every lcore */
1072*2d9fd380Sjfb8856606 	rte_eal_mp_remote_launch(main_loop, NULL, CALL_MAIN);
1073*2d9fd380Sjfb8856606 	RTE_LCORE_FOREACH_WORKER(lcore_id) {
1074a9643ea8Slogwang 		if (rte_eal_wait_lcore(lcore_id) < 0)
1075a9643ea8Slogwang 			return -1;
1076a9643ea8Slogwang 	}
1077a9643ea8Slogwang 
1078a9643ea8Slogwang 	return 0;
1079a9643ea8Slogwang }
1080