1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2010-2016 Intel Corporation
3  */
4 
5 #include <stdio.h>
6 #include <stdlib.h>
7 #include <stdint.h>
8 #include <inttypes.h>
9 #include <sys/types.h>
10 #include <string.h>
11 #include <sys/queue.h>
12 #include <stdarg.h>
13 #include <errno.h>
14 #include <getopt.h>
15 
16 #include <rte_common.h>
17 #include <rte_vect.h>
18 #include <rte_byteorder.h>
19 #include <rte_log.h>
20 #include <rte_memory.h>
21 #include <rte_memcpy.h>
22 #include <rte_eal.h>
23 #include <rte_launch.h>
24 #include <rte_atomic.h>
25 #include <rte_cycles.h>
26 #include <rte_prefetch.h>
27 #include <rte_lcore.h>
28 #include <rte_per_lcore.h>
29 #include <rte_branch_prediction.h>
30 #include <rte_interrupts.h>
31 #include <rte_random.h>
32 #include <rte_debug.h>
33 #include <rte_ether.h>
34 #include <rte_ethdev.h>
35 #include <rte_ring.h>
36 #include <rte_mempool.h>
37 #include <rte_mbuf.h>
38 #include <rte_ip.h>
39 #include <rte_tcp.h>
40 #include <rte_udp.h>
41 #include <rte_string_fns.h>
42 #include <rte_pause.h>
43 #include <rte_timer.h>
44 
45 #include <cmdline_parse.h>
46 #include <cmdline_parse_etheraddr.h>
47 
48 #include <lthread_api.h>
49 
50 #define APP_LOOKUP_EXACT_MATCH          0
51 #define APP_LOOKUP_LPM                  1
52 #define DO_RFC_1812_CHECKS
53 
54 /* Enable cpu-load stats 0-off, 1-on */
55 #define APP_CPU_LOAD                 1
56 
57 #ifndef APP_LOOKUP_METHOD
58 #define APP_LOOKUP_METHOD             APP_LOOKUP_LPM
59 #endif
60 
61 #ifndef __GLIBC__ /* sched_getcpu() is glibc specific */
62 #define sched_getcpu() rte_lcore_id()
63 #endif
64 
65 static int
66 check_ptype(int portid)
67 {
68 	int i, ret;
69 	int ipv4 = 0, ipv6 = 0;
70 
71 	ret = rte_eth_dev_get_supported_ptypes(portid, RTE_PTYPE_L3_MASK, NULL,
72 			0);
73 	if (ret <= 0)
74 		return 0;
75 
76 	uint32_t ptypes[ret];
77 
78 	ret = rte_eth_dev_get_supported_ptypes(portid, RTE_PTYPE_L3_MASK,
79 			ptypes, ret);
80 	for (i = 0; i < ret; ++i) {
81 		if (ptypes[i] & RTE_PTYPE_L3_IPV4)
82 			ipv4 = 1;
83 		if (ptypes[i] & RTE_PTYPE_L3_IPV6)
84 			ipv6 = 1;
85 	}
86 
87 	if (ipv4 && ipv6)
88 		return 1;
89 
90 	return 0;
91 }
92 
93 static inline void
94 parse_ptype(struct rte_mbuf *m)
95 {
96 	struct ether_hdr *eth_hdr;
97 	uint32_t packet_type = RTE_PTYPE_UNKNOWN;
98 	uint16_t ether_type;
99 
100 	eth_hdr = rte_pktmbuf_mtod(m, struct ether_hdr *);
101 	ether_type = eth_hdr->ether_type;
102 	if (ether_type == rte_cpu_to_be_16(ETHER_TYPE_IPv4))
103 		packet_type |= RTE_PTYPE_L3_IPV4_EXT_UNKNOWN;
104 	else if (ether_type == rte_cpu_to_be_16(ETHER_TYPE_IPv6))
105 		packet_type |= RTE_PTYPE_L3_IPV6_EXT_UNKNOWN;
106 
107 	m->packet_type = packet_type;
108 }
109 
110 static uint16_t
111 cb_parse_ptype(__rte_unused uint16_t port, __rte_unused uint16_t queue,
112 		struct rte_mbuf *pkts[], uint16_t nb_pkts,
113 		__rte_unused uint16_t max_pkts, __rte_unused void *user_param)
114 {
115 	unsigned int i;
116 
117 	for (i = 0; i < nb_pkts; i++)
118 		parse_ptype(pkts[i]);
119 
120 	return nb_pkts;
121 }
122 
123 /*
124  *  When set to zero, simple forwaring path is eanbled.
125  *  When set to one, optimized forwarding path is enabled.
126  *  Note that LPM optimisation path uses SSE4.1 instructions.
127  */
128 #define ENABLE_MULTI_BUFFER_OPTIMIZE	1
129 
130 #if (APP_LOOKUP_METHOD == APP_LOOKUP_EXACT_MATCH)
131 #include <rte_hash.h>
132 #elif (APP_LOOKUP_METHOD == APP_LOOKUP_LPM)
133 #include <rte_lpm.h>
134 #include <rte_lpm6.h>
135 #else
136 #error "APP_LOOKUP_METHOD set to incorrect value"
137 #endif
138 
139 #define RTE_LOGTYPE_L3FWD RTE_LOGTYPE_USER1
140 
141 #define MAX_JUMBO_PKT_LEN  9600
142 
143 #define IPV6_ADDR_LEN 16
144 
145 #define MEMPOOL_CACHE_SIZE 256
146 
147 /*
148  * This expression is used to calculate the number of mbufs needed depending on
149  * user input, taking into account memory for rx and tx hardware rings, cache
150  * per lcore and mtable per port per lcore. RTE_MAX is used to ensure that
151  * NB_MBUF never goes below a minimum value of 8192
152  */
153 
154 #define NB_MBUF RTE_MAX(\
155 		(nb_ports*nb_rx_queue*nb_rxd +      \
156 		nb_ports*nb_lcores*MAX_PKT_BURST +  \
157 		nb_ports*n_tx_queue*nb_txd +        \
158 		nb_lcores*MEMPOOL_CACHE_SIZE),      \
159 		(unsigned)8192)
160 
161 #define MAX_PKT_BURST     32
162 #define BURST_TX_DRAIN_US 100 /* TX drain every ~100us */
163 
164 /*
165  * Try to avoid TX buffering if we have at least MAX_TX_BURST packets to send.
166  */
167 #define	MAX_TX_BURST  (MAX_PKT_BURST / 2)
168 #define BURST_SIZE    MAX_TX_BURST
169 
170 #define NB_SOCKETS 8
171 
172 /* Configure how many packets ahead to prefetch, when reading packets */
173 #define PREFETCH_OFFSET	3
174 
175 /* Used to mark destination port as 'invalid'. */
176 #define	BAD_PORT	((uint16_t)-1)
177 
178 #define FWDSTEP	4
179 
180 /*
181  * Configurable number of RX/TX ring descriptors
182  */
183 #define RTE_TEST_RX_DESC_DEFAULT 1024
184 #define RTE_TEST_TX_DESC_DEFAULT 1024
185 static uint16_t nb_rxd = RTE_TEST_RX_DESC_DEFAULT;
186 static uint16_t nb_txd = RTE_TEST_TX_DESC_DEFAULT;
187 
188 /* ethernet addresses of ports */
189 static uint64_t dest_eth_addr[RTE_MAX_ETHPORTS];
190 static struct ether_addr ports_eth_addr[RTE_MAX_ETHPORTS];
191 
192 static xmm_t val_eth[RTE_MAX_ETHPORTS];
193 
194 /* replace first 12B of the ethernet header. */
195 #define	MASK_ETH 0x3f
196 
197 /* mask of enabled ports */
198 static uint32_t enabled_port_mask;
199 static int promiscuous_on; /**< Set in promiscuous mode off by default. */
200 static int numa_on = 1;    /**< NUMA is enabled by default. */
201 static int parse_ptype_on;
202 
203 #if (APP_LOOKUP_METHOD == APP_LOOKUP_EXACT_MATCH)
204 static int ipv6;           /**< ipv6 is false by default. */
205 #endif
206 
207 #if (APP_CPU_LOAD == 1)
208 
209 #define MAX_CPU RTE_MAX_LCORE
210 #define CPU_LOAD_TIMEOUT_US (5 * 1000 * 1000)  /**< Timeout for collecting 5s */
211 
212 #define CPU_PROCESS     0
213 #define CPU_POLL        1
214 #define MAX_CPU_COUNTER 2
215 
216 struct cpu_load {
217 	uint16_t       n_cpu;
218 	uint64_t       counter;
219 	uint64_t       hits[MAX_CPU_COUNTER][MAX_CPU];
220 } __rte_cache_aligned;
221 
222 static struct cpu_load cpu_load;
223 static int cpu_load_lcore_id = -1;
224 
225 #define SET_CPU_BUSY(thread, counter) \
226 		thread->conf.busy[counter] = 1
227 
228 #define SET_CPU_IDLE(thread, counter) \
229 		thread->conf.busy[counter] = 0
230 
231 #define IS_CPU_BUSY(thread, counter) \
232 		(thread->conf.busy[counter] > 0)
233 
234 #else
235 
236 #define SET_CPU_BUSY(thread, counter)
237 #define SET_CPU_IDLE(thread, counter)
238 #define IS_CPU_BUSY(thread, counter) 0
239 
240 #endif
241 
242 struct mbuf_table {
243 	uint16_t len;
244 	struct rte_mbuf *m_table[MAX_PKT_BURST];
245 };
246 
247 struct lcore_rx_queue {
248 	uint16_t port_id;
249 	uint8_t queue_id;
250 } __rte_cache_aligned;
251 
252 #define MAX_RX_QUEUE_PER_LCORE 16
253 #define MAX_TX_QUEUE_PER_PORT  RTE_MAX_ETHPORTS
254 #define MAX_RX_QUEUE_PER_PORT  128
255 
256 #define MAX_LCORE_PARAMS       1024
257 struct rx_thread_params {
258 	uint16_t port_id;
259 	uint8_t queue_id;
260 	uint8_t lcore_id;
261 	uint8_t thread_id;
262 } __rte_cache_aligned;
263 
264 static struct rx_thread_params rx_thread_params_array[MAX_LCORE_PARAMS];
265 static struct rx_thread_params rx_thread_params_array_default[] = {
266 	{0, 0, 2, 0},
267 	{0, 1, 2, 1},
268 	{0, 2, 2, 2},
269 	{1, 0, 2, 3},
270 	{1, 1, 2, 4},
271 	{1, 2, 2, 5},
272 	{2, 0, 2, 6},
273 	{3, 0, 3, 7},
274 	{3, 1, 3, 8},
275 };
276 
277 static struct rx_thread_params *rx_thread_params =
278 		rx_thread_params_array_default;
279 static uint16_t nb_rx_thread_params = RTE_DIM(rx_thread_params_array_default);
280 
281 struct tx_thread_params {
282 	uint8_t lcore_id;
283 	uint8_t thread_id;
284 } __rte_cache_aligned;
285 
286 static struct tx_thread_params tx_thread_params_array[MAX_LCORE_PARAMS];
287 static struct tx_thread_params tx_thread_params_array_default[] = {
288 	{4, 0},
289 	{5, 1},
290 	{6, 2},
291 	{7, 3},
292 	{8, 4},
293 	{9, 5},
294 	{10, 6},
295 	{11, 7},
296 	{12, 8},
297 };
298 
299 static struct tx_thread_params *tx_thread_params =
300 		tx_thread_params_array_default;
301 static uint16_t nb_tx_thread_params = RTE_DIM(tx_thread_params_array_default);
302 
303 static struct rte_eth_conf port_conf = {
304 	.rxmode = {
305 		.mq_mode = ETH_MQ_RX_RSS,
306 		.max_rx_pkt_len = ETHER_MAX_LEN,
307 		.split_hdr_size = 0,
308 		.offloads = DEV_RX_OFFLOAD_CHECKSUM,
309 	},
310 	.rx_adv_conf = {
311 		.rss_conf = {
312 			.rss_key = NULL,
313 			.rss_hf = ETH_RSS_TCP,
314 		},
315 	},
316 	.txmode = {
317 		.mq_mode = ETH_MQ_TX_NONE,
318 	},
319 };
320 
321 static struct rte_mempool *pktmbuf_pool[NB_SOCKETS];
322 
323 #if (APP_LOOKUP_METHOD == APP_LOOKUP_EXACT_MATCH)
324 
325 #include <rte_hash_crc.h>
326 #define DEFAULT_HASH_FUNC       rte_hash_crc
327 
328 struct ipv4_5tuple {
329 	uint32_t ip_dst;
330 	uint32_t ip_src;
331 	uint16_t port_dst;
332 	uint16_t port_src;
333 	uint8_t  proto;
334 } __attribute__((__packed__));
335 
336 union ipv4_5tuple_host {
337 	struct {
338 		uint8_t  pad0;
339 		uint8_t  proto;
340 		uint16_t pad1;
341 		uint32_t ip_src;
342 		uint32_t ip_dst;
343 		uint16_t port_src;
344 		uint16_t port_dst;
345 	};
346 	__m128i xmm;
347 };
348 
349 #define XMM_NUM_IN_IPV6_5TUPLE 3
350 
351 struct ipv6_5tuple {
352 	uint8_t  ip_dst[IPV6_ADDR_LEN];
353 	uint8_t  ip_src[IPV6_ADDR_LEN];
354 	uint16_t port_dst;
355 	uint16_t port_src;
356 	uint8_t  proto;
357 } __attribute__((__packed__));
358 
359 union ipv6_5tuple_host {
360 	struct {
361 		uint16_t pad0;
362 		uint8_t  proto;
363 		uint8_t  pad1;
364 		uint8_t  ip_src[IPV6_ADDR_LEN];
365 		uint8_t  ip_dst[IPV6_ADDR_LEN];
366 		uint16_t port_src;
367 		uint16_t port_dst;
368 		uint64_t reserve;
369 	};
370 	__m128i xmm[XMM_NUM_IN_IPV6_5TUPLE];
371 };
372 
373 struct ipv4_l3fwd_route {
374 	struct ipv4_5tuple key;
375 	uint8_t if_out;
376 };
377 
378 struct ipv6_l3fwd_route {
379 	struct ipv6_5tuple key;
380 	uint8_t if_out;
381 };
382 
383 static struct ipv4_l3fwd_route ipv4_l3fwd_route_array[] = {
384 	{{IPv4(101, 0, 0, 0), IPv4(100, 10, 0, 1),  101, 11, IPPROTO_TCP}, 0},
385 	{{IPv4(201, 0, 0, 0), IPv4(200, 20, 0, 1),  102, 12, IPPROTO_TCP}, 1},
386 	{{IPv4(111, 0, 0, 0), IPv4(100, 30, 0, 1),  101, 11, IPPROTO_TCP}, 2},
387 	{{IPv4(211, 0, 0, 0), IPv4(200, 40, 0, 1),  102, 12, IPPROTO_TCP}, 3},
388 };
389 
390 static struct ipv6_l3fwd_route ipv6_l3fwd_route_array[] = {
391 	{{
392 	{0xfe, 0x80, 0, 0, 0, 0, 0, 0, 0x02, 0x1e, 0x67, 0xff, 0xfe, 0, 0, 0},
393 	{0xfe, 0x80, 0, 0, 0, 0, 0, 0, 0x02, 0x1b, 0x21, 0xff, 0xfe, 0x91, 0x38,
394 			0x05},
395 	101, 11, IPPROTO_TCP}, 0},
396 
397 	{{
398 	{0xfe, 0x90, 0, 0, 0, 0, 0, 0, 0x02, 0x1e, 0x67, 0xff, 0xfe, 0, 0, 0},
399 	{0xfe, 0x90, 0, 0, 0, 0, 0, 0, 0x02, 0x1b, 0x21, 0xff, 0xfe, 0x91, 0x38,
400 			0x05},
401 	102, 12, IPPROTO_TCP}, 1},
402 
403 	{{
404 	{0xfe, 0xa0, 0, 0, 0, 0, 0, 0, 0x02, 0x1e, 0x67, 0xff, 0xfe, 0, 0, 0},
405 	{0xfe, 0xa0, 0, 0, 0, 0, 0, 0, 0x02, 0x1b, 0x21, 0xff, 0xfe, 0x91, 0x38,
406 			0x05},
407 	101, 11, IPPROTO_TCP}, 2},
408 
409 	{{
410 	{0xfe, 0xb0, 0, 0, 0, 0, 0, 0, 0x02, 0x1e, 0x67, 0xff, 0xfe, 0, 0, 0},
411 	{0xfe, 0xb0, 0, 0, 0, 0, 0, 0, 0x02, 0x1b, 0x21, 0xff, 0xfe, 0x91, 0x38,
412 			0x05},
413 	102, 12, IPPROTO_TCP}, 3},
414 };
415 
416 typedef struct rte_hash lookup_struct_t;
417 static lookup_struct_t *ipv4_l3fwd_lookup_struct[NB_SOCKETS];
418 static lookup_struct_t *ipv6_l3fwd_lookup_struct[NB_SOCKETS];
419 
420 #ifdef RTE_ARCH_X86_64
421 /* default to 4 million hash entries (approx) */
422 #define L3FWD_HASH_ENTRIES (1024*1024*4)
423 #else
424 /* 32-bit has less address-space for hugepage memory, limit to 1M entries */
425 #define L3FWD_HASH_ENTRIES (1024*1024*1)
426 #endif
427 #define HASH_ENTRY_NUMBER_DEFAULT 4
428 
429 static uint32_t hash_entry_number = HASH_ENTRY_NUMBER_DEFAULT;
430 
431 static inline uint32_t
432 ipv4_hash_crc(const void *data, __rte_unused uint32_t data_len,
433 		uint32_t init_val)
434 {
435 	const union ipv4_5tuple_host *k;
436 	uint32_t t;
437 	const uint32_t *p;
438 
439 	k = data;
440 	t = k->proto;
441 	p = (const uint32_t *)&k->port_src;
442 
443 	init_val = rte_hash_crc_4byte(t, init_val);
444 	init_val = rte_hash_crc_4byte(k->ip_src, init_val);
445 	init_val = rte_hash_crc_4byte(k->ip_dst, init_val);
446 	init_val = rte_hash_crc_4byte(*p, init_val);
447 	return init_val;
448 }
449 
450 static inline uint32_t
451 ipv6_hash_crc(const void *data, __rte_unused uint32_t data_len,
452 		uint32_t init_val)
453 {
454 	const union ipv6_5tuple_host *k;
455 	uint32_t t;
456 	const uint32_t *p;
457 	const uint32_t *ip_src0, *ip_src1, *ip_src2, *ip_src3;
458 	const uint32_t *ip_dst0, *ip_dst1, *ip_dst2, *ip_dst3;
459 
460 	k = data;
461 	t = k->proto;
462 	p = (const uint32_t *)&k->port_src;
463 
464 	ip_src0 = (const uint32_t *) k->ip_src;
465 	ip_src1 = (const uint32_t *)(k->ip_src + 4);
466 	ip_src2 = (const uint32_t *)(k->ip_src + 8);
467 	ip_src3 = (const uint32_t *)(k->ip_src + 12);
468 	ip_dst0 = (const uint32_t *) k->ip_dst;
469 	ip_dst1 = (const uint32_t *)(k->ip_dst + 4);
470 	ip_dst2 = (const uint32_t *)(k->ip_dst + 8);
471 	ip_dst3 = (const uint32_t *)(k->ip_dst + 12);
472 	init_val = rte_hash_crc_4byte(t, init_val);
473 	init_val = rte_hash_crc_4byte(*ip_src0, init_val);
474 	init_val = rte_hash_crc_4byte(*ip_src1, init_val);
475 	init_val = rte_hash_crc_4byte(*ip_src2, init_val);
476 	init_val = rte_hash_crc_4byte(*ip_src3, init_val);
477 	init_val = rte_hash_crc_4byte(*ip_dst0, init_val);
478 	init_val = rte_hash_crc_4byte(*ip_dst1, init_val);
479 	init_val = rte_hash_crc_4byte(*ip_dst2, init_val);
480 	init_val = rte_hash_crc_4byte(*ip_dst3, init_val);
481 	init_val = rte_hash_crc_4byte(*p, init_val);
482 	return init_val;
483 }
484 
485 #define IPV4_L3FWD_NUM_ROUTES RTE_DIM(ipv4_l3fwd_route_array)
486 #define IPV6_L3FWD_NUM_ROUTES RTE_DIM(ipv6_l3fwd_route_array)
487 
488 static uint8_t ipv4_l3fwd_out_if[L3FWD_HASH_ENTRIES] __rte_cache_aligned;
489 static uint8_t ipv6_l3fwd_out_if[L3FWD_HASH_ENTRIES] __rte_cache_aligned;
490 
491 #endif
492 
493 #if (APP_LOOKUP_METHOD == APP_LOOKUP_LPM)
494 struct ipv4_l3fwd_route {
495 	uint32_t ip;
496 	uint8_t  depth;
497 	uint8_t  if_out;
498 };
499 
500 struct ipv6_l3fwd_route {
501 	uint8_t ip[16];
502 	uint8_t depth;
503 	uint8_t if_out;
504 };
505 
506 static struct ipv4_l3fwd_route ipv4_l3fwd_route_array[] = {
507 	{IPv4(1, 1, 1, 0), 24, 0},
508 	{IPv4(2, 1, 1, 0), 24, 1},
509 	{IPv4(3, 1, 1, 0), 24, 2},
510 	{IPv4(4, 1, 1, 0), 24, 3},
511 	{IPv4(5, 1, 1, 0), 24, 4},
512 	{IPv4(6, 1, 1, 0), 24, 5},
513 	{IPv4(7, 1, 1, 0), 24, 6},
514 	{IPv4(8, 1, 1, 0), 24, 7},
515 };
516 
517 static struct ipv6_l3fwd_route ipv6_l3fwd_route_array[] = {
518 	{{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, 48, 0},
519 	{{2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, 48, 1},
520 	{{3, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, 48, 2},
521 	{{4, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, 48, 3},
522 	{{5, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, 48, 4},
523 	{{6, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, 48, 5},
524 	{{7, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, 48, 6},
525 	{{8, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, 48, 7},
526 };
527 
528 #define IPV4_L3FWD_NUM_ROUTES RTE_DIM(ipv4_l3fwd_route_array)
529 #define IPV6_L3FWD_NUM_ROUTES RTE_DIM(ipv6_l3fwd_route_array)
530 
531 #define IPV4_L3FWD_LPM_MAX_RULES         1024
532 #define IPV6_L3FWD_LPM_MAX_RULES         1024
533 #define IPV6_L3FWD_LPM_NUMBER_TBL8S (1 << 16)
534 
535 typedef struct rte_lpm lookup_struct_t;
536 typedef struct rte_lpm6 lookup6_struct_t;
537 static lookup_struct_t *ipv4_l3fwd_lookup_struct[NB_SOCKETS];
538 static lookup6_struct_t *ipv6_l3fwd_lookup_struct[NB_SOCKETS];
539 #endif
540 
541 struct lcore_conf {
542 	lookup_struct_t *ipv4_lookup_struct;
543 #if (APP_LOOKUP_METHOD == APP_LOOKUP_LPM)
544 	lookup6_struct_t *ipv6_lookup_struct;
545 #else
546 	lookup_struct_t *ipv6_lookup_struct;
547 #endif
548 	void *data;
549 } __rte_cache_aligned;
550 
551 static struct lcore_conf lcore_conf[RTE_MAX_LCORE];
552 RTE_DEFINE_PER_LCORE(struct lcore_conf *, lcore_conf);
553 
554 #define MAX_RX_QUEUE_PER_THREAD 16
555 #define MAX_TX_PORT_PER_THREAD  RTE_MAX_ETHPORTS
556 #define MAX_TX_QUEUE_PER_PORT   RTE_MAX_ETHPORTS
557 #define MAX_RX_QUEUE_PER_PORT   128
558 
559 #define MAX_RX_THREAD 1024
560 #define MAX_TX_THREAD 1024
561 #define MAX_THREAD    (MAX_RX_THREAD + MAX_TX_THREAD)
562 
563 /**
564  * Producers and consumers threads configuration
565  */
566 static int lthreads_on = 1; /**< Use lthreads for processing*/
567 
568 rte_atomic16_t rx_counter;  /**< Number of spawned rx threads */
569 rte_atomic16_t tx_counter;  /**< Number of spawned tx threads */
570 
571 struct thread_conf {
572 	uint16_t lcore_id;      /**< Initial lcore for rx thread */
573 	uint16_t cpu_id;        /**< Cpu id for cpu load stats counter */
574 	uint16_t thread_id;     /**< Thread ID */
575 
576 #if (APP_CPU_LOAD > 0)
577 	int busy[MAX_CPU_COUNTER];
578 #endif
579 };
580 
581 struct thread_rx_conf {
582 	struct thread_conf conf;
583 
584 	uint16_t n_rx_queue;
585 	struct lcore_rx_queue rx_queue_list[MAX_RX_QUEUE_PER_LCORE];
586 
587 	uint16_t n_ring;        /**< Number of output rings */
588 	struct rte_ring *ring[RTE_MAX_LCORE];
589 	struct lthread_cond *ready[RTE_MAX_LCORE];
590 
591 #if (APP_CPU_LOAD > 0)
592 	int busy[MAX_CPU_COUNTER];
593 #endif
594 } __rte_cache_aligned;
595 
596 uint16_t n_rx_thread;
597 struct thread_rx_conf rx_thread[MAX_RX_THREAD];
598 
599 struct thread_tx_conf {
600 	struct thread_conf conf;
601 
602 	uint16_t tx_queue_id[RTE_MAX_LCORE];
603 	struct mbuf_table tx_mbufs[RTE_MAX_LCORE];
604 
605 	struct rte_ring *ring;
606 	struct lthread_cond **ready;
607 
608 } __rte_cache_aligned;
609 
610 uint16_t n_tx_thread;
611 struct thread_tx_conf tx_thread[MAX_TX_THREAD];
612 
613 /* Send burst of packets on an output interface */
614 static inline int
615 send_burst(struct thread_tx_conf *qconf, uint16_t n, uint16_t port)
616 {
617 	struct rte_mbuf **m_table;
618 	int ret;
619 	uint16_t queueid;
620 
621 	queueid = qconf->tx_queue_id[port];
622 	m_table = (struct rte_mbuf **)qconf->tx_mbufs[port].m_table;
623 
624 	ret = rte_eth_tx_burst(port, queueid, m_table, n);
625 	if (unlikely(ret < n)) {
626 		do {
627 			rte_pktmbuf_free(m_table[ret]);
628 		} while (++ret < n);
629 	}
630 
631 	return 0;
632 }
633 
634 /* Enqueue a single packet, and send burst if queue is filled */
635 static inline int
636 send_single_packet(struct rte_mbuf *m, uint16_t port)
637 {
638 	uint16_t len;
639 	struct thread_tx_conf *qconf;
640 
641 	if (lthreads_on)
642 		qconf = (struct thread_tx_conf *)lthread_get_data();
643 	else
644 		qconf = (struct thread_tx_conf *)RTE_PER_LCORE(lcore_conf)->data;
645 
646 	len = qconf->tx_mbufs[port].len;
647 	qconf->tx_mbufs[port].m_table[len] = m;
648 	len++;
649 
650 	/* enough pkts to be sent */
651 	if (unlikely(len == MAX_PKT_BURST)) {
652 		send_burst(qconf, MAX_PKT_BURST, port);
653 		len = 0;
654 	}
655 
656 	qconf->tx_mbufs[port].len = len;
657 	return 0;
658 }
659 
660 #if ((APP_LOOKUP_METHOD == APP_LOOKUP_LPM) && \
661 	(ENABLE_MULTI_BUFFER_OPTIMIZE == 1))
662 static __rte_always_inline void
663 send_packetsx4(uint16_t port,
664 	struct rte_mbuf *m[], uint32_t num)
665 {
666 	uint32_t len, j, n;
667 	struct thread_tx_conf *qconf;
668 
669 	if (lthreads_on)
670 		qconf = (struct thread_tx_conf *)lthread_get_data();
671 	else
672 		qconf = (struct thread_tx_conf *)RTE_PER_LCORE(lcore_conf)->data;
673 
674 	len = qconf->tx_mbufs[port].len;
675 
676 	/*
677 	 * If TX buffer for that queue is empty, and we have enough packets,
678 	 * then send them straightway.
679 	 */
680 	if (num >= MAX_TX_BURST && len == 0) {
681 		n = rte_eth_tx_burst(port, qconf->tx_queue_id[port], m, num);
682 		if (unlikely(n < num)) {
683 			do {
684 				rte_pktmbuf_free(m[n]);
685 			} while (++n < num);
686 		}
687 		return;
688 	}
689 
690 	/*
691 	 * Put packets into TX buffer for that queue.
692 	 */
693 
694 	n = len + num;
695 	n = (n > MAX_PKT_BURST) ? MAX_PKT_BURST - len : num;
696 
697 	j = 0;
698 	switch (n % FWDSTEP) {
699 	while (j < n) {
700 	case 0:
701 		qconf->tx_mbufs[port].m_table[len + j] = m[j];
702 		j++;
703 		/* fall-through */
704 	case 3:
705 		qconf->tx_mbufs[port].m_table[len + j] = m[j];
706 		j++;
707 		/* fall-through */
708 	case 2:
709 		qconf->tx_mbufs[port].m_table[len + j] = m[j];
710 		j++;
711 		/* fall-through */
712 	case 1:
713 		qconf->tx_mbufs[port].m_table[len + j] = m[j];
714 		j++;
715 	}
716 	}
717 
718 	len += n;
719 
720 	/* enough pkts to be sent */
721 	if (unlikely(len == MAX_PKT_BURST)) {
722 
723 		send_burst(qconf, MAX_PKT_BURST, port);
724 
725 		/* copy rest of the packets into the TX buffer. */
726 		len = num - n;
727 		j = 0;
728 		switch (len % FWDSTEP) {
729 		while (j < len) {
730 		case 0:
731 			qconf->tx_mbufs[port].m_table[j] = m[n + j];
732 			j++;
733 			/* fall-through */
734 		case 3:
735 			qconf->tx_mbufs[port].m_table[j] = m[n + j];
736 			j++;
737 			/* fall-through */
738 		case 2:
739 			qconf->tx_mbufs[port].m_table[j] = m[n + j];
740 			j++;
741 			/* fall-through */
742 		case 1:
743 			qconf->tx_mbufs[port].m_table[j] = m[n + j];
744 			j++;
745 		}
746 		}
747 	}
748 
749 	qconf->tx_mbufs[port].len = len;
750 }
751 #endif /* APP_LOOKUP_LPM */
752 
753 #ifdef DO_RFC_1812_CHECKS
754 static inline int
755 is_valid_ipv4_pkt(struct ipv4_hdr *pkt, uint32_t link_len)
756 {
757 	/* From http://www.rfc-editor.org/rfc/rfc1812.txt section 5.2.2 */
758 	/*
759 	 * 1. The packet length reported by the Link Layer must be large
760 	 * enough to hold the minimum length legal IP datagram (20 bytes).
761 	 */
762 	if (link_len < sizeof(struct ipv4_hdr))
763 		return -1;
764 
765 	/* 2. The IP checksum must be correct. */
766 	/* this is checked in H/W */
767 
768 	/*
769 	 * 3. The IP version number must be 4. If the version number is not 4
770 	 * then the packet may be another version of IP, such as IPng or
771 	 * ST-II.
772 	 */
773 	if (((pkt->version_ihl) >> 4) != 4)
774 		return -3;
775 	/*
776 	 * 4. The IP header length field must be large enough to hold the
777 	 * minimum length legal IP datagram (20 bytes = 5 words).
778 	 */
779 	if ((pkt->version_ihl & 0xf) < 5)
780 		return -4;
781 
782 	/*
783 	 * 5. The IP total length field must be large enough to hold the IP
784 	 * datagram header, whose length is specified in the IP header length
785 	 * field.
786 	 */
787 	if (rte_cpu_to_be_16(pkt->total_length) < sizeof(struct ipv4_hdr))
788 		return -5;
789 
790 	return 0;
791 }
792 #endif
793 
794 #if (APP_LOOKUP_METHOD == APP_LOOKUP_EXACT_MATCH)
795 
796 static __m128i mask0;
797 static __m128i mask1;
798 static __m128i mask2;
799 static inline uint16_t
800 get_ipv4_dst_port(void *ipv4_hdr, uint16_t portid,
801 		lookup_struct_t *ipv4_l3fwd_lookup_struct)
802 {
803 	int ret = 0;
804 	union ipv4_5tuple_host key;
805 
806 	ipv4_hdr = (uint8_t *)ipv4_hdr + offsetof(struct ipv4_hdr, time_to_live);
807 	__m128i data = _mm_loadu_si128((__m128i *)(ipv4_hdr));
808 	/* Get 5 tuple: dst port, src port, dst IP address, src IP address and
809 	   protocol */
810 	key.xmm = _mm_and_si128(data, mask0);
811 	/* Find destination port */
812 	ret = rte_hash_lookup(ipv4_l3fwd_lookup_struct, (const void *)&key);
813 	return ((ret < 0) ? portid : ipv4_l3fwd_out_if[ret]);
814 }
815 
816 static inline uint16_t
817 get_ipv6_dst_port(void *ipv6_hdr, uint16_t portid,
818 		lookup_struct_t *ipv6_l3fwd_lookup_struct)
819 {
820 	int ret = 0;
821 	union ipv6_5tuple_host key;
822 
823 	ipv6_hdr = (uint8_t *)ipv6_hdr + offsetof(struct ipv6_hdr, payload_len);
824 	__m128i data0 = _mm_loadu_si128((__m128i *)(ipv6_hdr));
825 	__m128i data1 = _mm_loadu_si128((__m128i *)(((uint8_t *)ipv6_hdr) +
826 			sizeof(__m128i)));
827 	__m128i data2 = _mm_loadu_si128((__m128i *)(((uint8_t *)ipv6_hdr) +
828 			sizeof(__m128i) + sizeof(__m128i)));
829 	/* Get part of 5 tuple: src IP address lower 96 bits and protocol */
830 	key.xmm[0] = _mm_and_si128(data0, mask1);
831 	/* Get part of 5 tuple: dst IP address lower 96 bits and src IP address
832 	   higher 32 bits */
833 	key.xmm[1] = data1;
834 	/* Get part of 5 tuple: dst port and src port and dst IP address higher
835 	   32 bits */
836 	key.xmm[2] = _mm_and_si128(data2, mask2);
837 
838 	/* Find destination port */
839 	ret = rte_hash_lookup(ipv6_l3fwd_lookup_struct, (const void *)&key);
840 	return ((ret < 0) ? portid : ipv6_l3fwd_out_if[ret]);
841 }
842 #endif
843 
844 #if (APP_LOOKUP_METHOD == APP_LOOKUP_LPM)
845 
846 static inline uint16_t
847 get_ipv4_dst_port(void *ipv4_hdr, uint16_t portid,
848 		lookup_struct_t *ipv4_l3fwd_lookup_struct)
849 {
850 	uint32_t next_hop;
851 
852 	return ((rte_lpm_lookup(ipv4_l3fwd_lookup_struct,
853 		rte_be_to_cpu_32(((struct ipv4_hdr *)ipv4_hdr)->dst_addr),
854 		&next_hop) == 0) ? next_hop : portid);
855 }
856 
857 static inline uint16_t
858 get_ipv6_dst_port(void *ipv6_hdr,  uint16_t portid,
859 		lookup6_struct_t *ipv6_l3fwd_lookup_struct)
860 {
861 	uint32_t next_hop;
862 
863 	return ((rte_lpm6_lookup(ipv6_l3fwd_lookup_struct,
864 			((struct ipv6_hdr *)ipv6_hdr)->dst_addr, &next_hop) == 0) ?
865 			next_hop : portid);
866 }
867 #endif
868 
869 static inline void l3fwd_simple_forward(struct rte_mbuf *m, uint16_t portid)
870 		__attribute__((unused));
871 
872 #if ((APP_LOOKUP_METHOD == APP_LOOKUP_EXACT_MATCH) && \
873 	(ENABLE_MULTI_BUFFER_OPTIMIZE == 1))
874 
875 #define MASK_ALL_PKTS   0xff
876 #define EXCLUDE_1ST_PKT 0xfe
877 #define EXCLUDE_2ND_PKT 0xfd
878 #define EXCLUDE_3RD_PKT 0xfb
879 #define EXCLUDE_4TH_PKT 0xf7
880 #define EXCLUDE_5TH_PKT 0xef
881 #define EXCLUDE_6TH_PKT 0xdf
882 #define EXCLUDE_7TH_PKT 0xbf
883 #define EXCLUDE_8TH_PKT 0x7f
884 
885 static inline void
886 simple_ipv4_fwd_8pkts(struct rte_mbuf *m[8], uint16_t portid)
887 {
888 	struct ether_hdr *eth_hdr[8];
889 	struct ipv4_hdr *ipv4_hdr[8];
890 	uint16_t dst_port[8];
891 	int32_t ret[8];
892 	union ipv4_5tuple_host key[8];
893 	__m128i data[8];
894 
895 	eth_hdr[0] = rte_pktmbuf_mtod(m[0], struct ether_hdr *);
896 	eth_hdr[1] = rte_pktmbuf_mtod(m[1], struct ether_hdr *);
897 	eth_hdr[2] = rte_pktmbuf_mtod(m[2], struct ether_hdr *);
898 	eth_hdr[3] = rte_pktmbuf_mtod(m[3], struct ether_hdr *);
899 	eth_hdr[4] = rte_pktmbuf_mtod(m[4], struct ether_hdr *);
900 	eth_hdr[5] = rte_pktmbuf_mtod(m[5], struct ether_hdr *);
901 	eth_hdr[6] = rte_pktmbuf_mtod(m[6], struct ether_hdr *);
902 	eth_hdr[7] = rte_pktmbuf_mtod(m[7], struct ether_hdr *);
903 
904 	/* Handle IPv4 headers.*/
905 	ipv4_hdr[0] = rte_pktmbuf_mtod_offset(m[0], struct ipv4_hdr *,
906 			sizeof(struct ether_hdr));
907 	ipv4_hdr[1] = rte_pktmbuf_mtod_offset(m[1], struct ipv4_hdr *,
908 			sizeof(struct ether_hdr));
909 	ipv4_hdr[2] = rte_pktmbuf_mtod_offset(m[2], struct ipv4_hdr *,
910 			sizeof(struct ether_hdr));
911 	ipv4_hdr[3] = rte_pktmbuf_mtod_offset(m[3], struct ipv4_hdr *,
912 			sizeof(struct ether_hdr));
913 	ipv4_hdr[4] = rte_pktmbuf_mtod_offset(m[4], struct ipv4_hdr *,
914 			sizeof(struct ether_hdr));
915 	ipv4_hdr[5] = rte_pktmbuf_mtod_offset(m[5], struct ipv4_hdr *,
916 			sizeof(struct ether_hdr));
917 	ipv4_hdr[6] = rte_pktmbuf_mtod_offset(m[6], struct ipv4_hdr *,
918 			sizeof(struct ether_hdr));
919 	ipv4_hdr[7] = rte_pktmbuf_mtod_offset(m[7], struct ipv4_hdr *,
920 			sizeof(struct ether_hdr));
921 
922 #ifdef DO_RFC_1812_CHECKS
923 	/* Check to make sure the packet is valid (RFC1812) */
924 	uint8_t valid_mask = MASK_ALL_PKTS;
925 
926 	if (is_valid_ipv4_pkt(ipv4_hdr[0], m[0]->pkt_len) < 0) {
927 		rte_pktmbuf_free(m[0]);
928 		valid_mask &= EXCLUDE_1ST_PKT;
929 	}
930 	if (is_valid_ipv4_pkt(ipv4_hdr[1], m[1]->pkt_len) < 0) {
931 		rte_pktmbuf_free(m[1]);
932 		valid_mask &= EXCLUDE_2ND_PKT;
933 	}
934 	if (is_valid_ipv4_pkt(ipv4_hdr[2], m[2]->pkt_len) < 0) {
935 		rte_pktmbuf_free(m[2]);
936 		valid_mask &= EXCLUDE_3RD_PKT;
937 	}
938 	if (is_valid_ipv4_pkt(ipv4_hdr[3], m[3]->pkt_len) < 0) {
939 		rte_pktmbuf_free(m[3]);
940 		valid_mask &= EXCLUDE_4TH_PKT;
941 	}
942 	if (is_valid_ipv4_pkt(ipv4_hdr[4], m[4]->pkt_len) < 0) {
943 		rte_pktmbuf_free(m[4]);
944 		valid_mask &= EXCLUDE_5TH_PKT;
945 	}
946 	if (is_valid_ipv4_pkt(ipv4_hdr[5], m[5]->pkt_len) < 0) {
947 		rte_pktmbuf_free(m[5]);
948 		valid_mask &= EXCLUDE_6TH_PKT;
949 	}
950 	if (is_valid_ipv4_pkt(ipv4_hdr[6], m[6]->pkt_len) < 0) {
951 		rte_pktmbuf_free(m[6]);
952 		valid_mask &= EXCLUDE_7TH_PKT;
953 	}
954 	if (is_valid_ipv4_pkt(ipv4_hdr[7], m[7]->pkt_len) < 0) {
955 		rte_pktmbuf_free(m[7]);
956 		valid_mask &= EXCLUDE_8TH_PKT;
957 	}
958 	if (unlikely(valid_mask != MASK_ALL_PKTS)) {
959 		if (valid_mask == 0)
960 			return;
961 
962 		uint8_t i = 0;
963 
964 		for (i = 0; i < 8; i++)
965 			if ((0x1 << i) & valid_mask)
966 				l3fwd_simple_forward(m[i], portid);
967 	}
968 #endif /* End of #ifdef DO_RFC_1812_CHECKS */
969 
970 	data[0] = _mm_loadu_si128(rte_pktmbuf_mtod_offset(m[0], __m128i *,
971 			sizeof(struct ether_hdr) +
972 			offsetof(struct ipv4_hdr, time_to_live)));
973 	data[1] = _mm_loadu_si128(rte_pktmbuf_mtod_offset(m[1], __m128i *,
974 			sizeof(struct ether_hdr) +
975 			offsetof(struct ipv4_hdr, time_to_live)));
976 	data[2] = _mm_loadu_si128(rte_pktmbuf_mtod_offset(m[2], __m128i *,
977 			sizeof(struct ether_hdr) +
978 			offsetof(struct ipv4_hdr, time_to_live)));
979 	data[3] = _mm_loadu_si128(rte_pktmbuf_mtod_offset(m[3], __m128i *,
980 			sizeof(struct ether_hdr) +
981 			offsetof(struct ipv4_hdr, time_to_live)));
982 	data[4] = _mm_loadu_si128(rte_pktmbuf_mtod_offset(m[4], __m128i *,
983 			sizeof(struct ether_hdr) +
984 			offsetof(struct ipv4_hdr, time_to_live)));
985 	data[5] = _mm_loadu_si128(rte_pktmbuf_mtod_offset(m[5], __m128i *,
986 			sizeof(struct ether_hdr) +
987 			offsetof(struct ipv4_hdr, time_to_live)));
988 	data[6] = _mm_loadu_si128(rte_pktmbuf_mtod_offset(m[6], __m128i *,
989 			sizeof(struct ether_hdr) +
990 			offsetof(struct ipv4_hdr, time_to_live)));
991 	data[7] = _mm_loadu_si128(rte_pktmbuf_mtod_offset(m[7], __m128i *,
992 			sizeof(struct ether_hdr) +
993 			offsetof(struct ipv4_hdr, time_to_live)));
994 
995 	key[0].xmm = _mm_and_si128(data[0], mask0);
996 	key[1].xmm = _mm_and_si128(data[1], mask0);
997 	key[2].xmm = _mm_and_si128(data[2], mask0);
998 	key[3].xmm = _mm_and_si128(data[3], mask0);
999 	key[4].xmm = _mm_and_si128(data[4], mask0);
1000 	key[5].xmm = _mm_and_si128(data[5], mask0);
1001 	key[6].xmm = _mm_and_si128(data[6], mask0);
1002 	key[7].xmm = _mm_and_si128(data[7], mask0);
1003 
1004 	const void *key_array[8] = {&key[0], &key[1], &key[2], &key[3],
1005 			&key[4], &key[5], &key[6], &key[7]};
1006 
1007 	rte_hash_lookup_bulk(RTE_PER_LCORE(lcore_conf)->ipv4_lookup_struct,
1008 			&key_array[0], 8, ret);
1009 	dst_port[0] = ((ret[0] < 0) ? portid : ipv4_l3fwd_out_if[ret[0]]);
1010 	dst_port[1] = ((ret[1] < 0) ? portid : ipv4_l3fwd_out_if[ret[1]]);
1011 	dst_port[2] = ((ret[2] < 0) ? portid : ipv4_l3fwd_out_if[ret[2]]);
1012 	dst_port[3] = ((ret[3] < 0) ? portid : ipv4_l3fwd_out_if[ret[3]]);
1013 	dst_port[4] = ((ret[4] < 0) ? portid : ipv4_l3fwd_out_if[ret[4]]);
1014 	dst_port[5] = ((ret[5] < 0) ? portid : ipv4_l3fwd_out_if[ret[5]]);
1015 	dst_port[6] = ((ret[6] < 0) ? portid : ipv4_l3fwd_out_if[ret[6]]);
1016 	dst_port[7] = ((ret[7] < 0) ? portid : ipv4_l3fwd_out_if[ret[7]]);
1017 
1018 	if (dst_port[0] >= RTE_MAX_ETHPORTS ||
1019 			(enabled_port_mask & 1 << dst_port[0]) == 0)
1020 		dst_port[0] = portid;
1021 	if (dst_port[1] >= RTE_MAX_ETHPORTS ||
1022 			(enabled_port_mask & 1 << dst_port[1]) == 0)
1023 		dst_port[1] = portid;
1024 	if (dst_port[2] >= RTE_MAX_ETHPORTS ||
1025 			(enabled_port_mask & 1 << dst_port[2]) == 0)
1026 		dst_port[2] = portid;
1027 	if (dst_port[3] >= RTE_MAX_ETHPORTS ||
1028 			(enabled_port_mask & 1 << dst_port[3]) == 0)
1029 		dst_port[3] = portid;
1030 	if (dst_port[4] >= RTE_MAX_ETHPORTS ||
1031 			(enabled_port_mask & 1 << dst_port[4]) == 0)
1032 		dst_port[4] = portid;
1033 	if (dst_port[5] >= RTE_MAX_ETHPORTS ||
1034 			(enabled_port_mask & 1 << dst_port[5]) == 0)
1035 		dst_port[5] = portid;
1036 	if (dst_port[6] >= RTE_MAX_ETHPORTS ||
1037 			(enabled_port_mask & 1 << dst_port[6]) == 0)
1038 		dst_port[6] = portid;
1039 	if (dst_port[7] >= RTE_MAX_ETHPORTS ||
1040 			(enabled_port_mask & 1 << dst_port[7]) == 0)
1041 		dst_port[7] = portid;
1042 
1043 #ifdef DO_RFC_1812_CHECKS
1044 	/* Update time to live and header checksum */
1045 	--(ipv4_hdr[0]->time_to_live);
1046 	--(ipv4_hdr[1]->time_to_live);
1047 	--(ipv4_hdr[2]->time_to_live);
1048 	--(ipv4_hdr[3]->time_to_live);
1049 	++(ipv4_hdr[0]->hdr_checksum);
1050 	++(ipv4_hdr[1]->hdr_checksum);
1051 	++(ipv4_hdr[2]->hdr_checksum);
1052 	++(ipv4_hdr[3]->hdr_checksum);
1053 	--(ipv4_hdr[4]->time_to_live);
1054 	--(ipv4_hdr[5]->time_to_live);
1055 	--(ipv4_hdr[6]->time_to_live);
1056 	--(ipv4_hdr[7]->time_to_live);
1057 	++(ipv4_hdr[4]->hdr_checksum);
1058 	++(ipv4_hdr[5]->hdr_checksum);
1059 	++(ipv4_hdr[6]->hdr_checksum);
1060 	++(ipv4_hdr[7]->hdr_checksum);
1061 #endif
1062 
1063 	/* dst addr */
1064 	*(uint64_t *)&eth_hdr[0]->d_addr = dest_eth_addr[dst_port[0]];
1065 	*(uint64_t *)&eth_hdr[1]->d_addr = dest_eth_addr[dst_port[1]];
1066 	*(uint64_t *)&eth_hdr[2]->d_addr = dest_eth_addr[dst_port[2]];
1067 	*(uint64_t *)&eth_hdr[3]->d_addr = dest_eth_addr[dst_port[3]];
1068 	*(uint64_t *)&eth_hdr[4]->d_addr = dest_eth_addr[dst_port[4]];
1069 	*(uint64_t *)&eth_hdr[5]->d_addr = dest_eth_addr[dst_port[5]];
1070 	*(uint64_t *)&eth_hdr[6]->d_addr = dest_eth_addr[dst_port[6]];
1071 	*(uint64_t *)&eth_hdr[7]->d_addr = dest_eth_addr[dst_port[7]];
1072 
1073 	/* src addr */
1074 	ether_addr_copy(&ports_eth_addr[dst_port[0]], &eth_hdr[0]->s_addr);
1075 	ether_addr_copy(&ports_eth_addr[dst_port[1]], &eth_hdr[1]->s_addr);
1076 	ether_addr_copy(&ports_eth_addr[dst_port[2]], &eth_hdr[2]->s_addr);
1077 	ether_addr_copy(&ports_eth_addr[dst_port[3]], &eth_hdr[3]->s_addr);
1078 	ether_addr_copy(&ports_eth_addr[dst_port[4]], &eth_hdr[4]->s_addr);
1079 	ether_addr_copy(&ports_eth_addr[dst_port[5]], &eth_hdr[5]->s_addr);
1080 	ether_addr_copy(&ports_eth_addr[dst_port[6]], &eth_hdr[6]->s_addr);
1081 	ether_addr_copy(&ports_eth_addr[dst_port[7]], &eth_hdr[7]->s_addr);
1082 
1083 	send_single_packet(m[0], (uint8_t)dst_port[0]);
1084 	send_single_packet(m[1], (uint8_t)dst_port[1]);
1085 	send_single_packet(m[2], (uint8_t)dst_port[2]);
1086 	send_single_packet(m[3], (uint8_t)dst_port[3]);
1087 	send_single_packet(m[4], (uint8_t)dst_port[4]);
1088 	send_single_packet(m[5], (uint8_t)dst_port[5]);
1089 	send_single_packet(m[6], (uint8_t)dst_port[6]);
1090 	send_single_packet(m[7], (uint8_t)dst_port[7]);
1091 
1092 }
1093 
1094 static inline void get_ipv6_5tuple(struct rte_mbuf *m0, __m128i mask0,
1095 		__m128i mask1, union ipv6_5tuple_host *key)
1096 {
1097 	__m128i tmpdata0 = _mm_loadu_si128(rte_pktmbuf_mtod_offset(m0,
1098 			__m128i *, sizeof(struct ether_hdr) +
1099 			offsetof(struct ipv6_hdr, payload_len)));
1100 	__m128i tmpdata1 = _mm_loadu_si128(rte_pktmbuf_mtod_offset(m0,
1101 			__m128i *, sizeof(struct ether_hdr) +
1102 			offsetof(struct ipv6_hdr, payload_len) + sizeof(__m128i)));
1103 	__m128i tmpdata2 = _mm_loadu_si128(rte_pktmbuf_mtod_offset(m0,
1104 			__m128i *, sizeof(struct ether_hdr) +
1105 			offsetof(struct ipv6_hdr, payload_len) + sizeof(__m128i) +
1106 			sizeof(__m128i)));
1107 	key->xmm[0] = _mm_and_si128(tmpdata0, mask0);
1108 	key->xmm[1] = tmpdata1;
1109 	key->xmm[2] = _mm_and_si128(tmpdata2, mask1);
1110 }
1111 
1112 static inline void
1113 simple_ipv6_fwd_8pkts(struct rte_mbuf *m[8], uint16_t portid)
1114 {
1115 	int32_t ret[8];
1116 	uint16_t dst_port[8];
1117 	struct ether_hdr *eth_hdr[8];
1118 	union ipv6_5tuple_host key[8];
1119 
1120 	__attribute__((unused)) struct ipv6_hdr *ipv6_hdr[8];
1121 
1122 	eth_hdr[0] = rte_pktmbuf_mtod(m[0], struct ether_hdr *);
1123 	eth_hdr[1] = rte_pktmbuf_mtod(m[1], struct ether_hdr *);
1124 	eth_hdr[2] = rte_pktmbuf_mtod(m[2], struct ether_hdr *);
1125 	eth_hdr[3] = rte_pktmbuf_mtod(m[3], struct ether_hdr *);
1126 	eth_hdr[4] = rte_pktmbuf_mtod(m[4], struct ether_hdr *);
1127 	eth_hdr[5] = rte_pktmbuf_mtod(m[5], struct ether_hdr *);
1128 	eth_hdr[6] = rte_pktmbuf_mtod(m[6], struct ether_hdr *);
1129 	eth_hdr[7] = rte_pktmbuf_mtod(m[7], struct ether_hdr *);
1130 
1131 	/* Handle IPv6 headers.*/
1132 	ipv6_hdr[0] = rte_pktmbuf_mtod_offset(m[0], struct ipv6_hdr *,
1133 			sizeof(struct ether_hdr));
1134 	ipv6_hdr[1] = rte_pktmbuf_mtod_offset(m[1], struct ipv6_hdr *,
1135 			sizeof(struct ether_hdr));
1136 	ipv6_hdr[2] = rte_pktmbuf_mtod_offset(m[2], struct ipv6_hdr *,
1137 			sizeof(struct ether_hdr));
1138 	ipv6_hdr[3] = rte_pktmbuf_mtod_offset(m[3], struct ipv6_hdr *,
1139 			sizeof(struct ether_hdr));
1140 	ipv6_hdr[4] = rte_pktmbuf_mtod_offset(m[4], struct ipv6_hdr *,
1141 			sizeof(struct ether_hdr));
1142 	ipv6_hdr[5] = rte_pktmbuf_mtod_offset(m[5], struct ipv6_hdr *,
1143 			sizeof(struct ether_hdr));
1144 	ipv6_hdr[6] = rte_pktmbuf_mtod_offset(m[6], struct ipv6_hdr *,
1145 			sizeof(struct ether_hdr));
1146 	ipv6_hdr[7] = rte_pktmbuf_mtod_offset(m[7], struct ipv6_hdr *,
1147 			sizeof(struct ether_hdr));
1148 
1149 	get_ipv6_5tuple(m[0], mask1, mask2, &key[0]);
1150 	get_ipv6_5tuple(m[1], mask1, mask2, &key[1]);
1151 	get_ipv6_5tuple(m[2], mask1, mask2, &key[2]);
1152 	get_ipv6_5tuple(m[3], mask1, mask2, &key[3]);
1153 	get_ipv6_5tuple(m[4], mask1, mask2, &key[4]);
1154 	get_ipv6_5tuple(m[5], mask1, mask2, &key[5]);
1155 	get_ipv6_5tuple(m[6], mask1, mask2, &key[6]);
1156 	get_ipv6_5tuple(m[7], mask1, mask2, &key[7]);
1157 
1158 	const void *key_array[8] = {&key[0], &key[1], &key[2], &key[3],
1159 			&key[4], &key[5], &key[6], &key[7]};
1160 
1161 	rte_hash_lookup_bulk(RTE_PER_LCORE(lcore_conf)->ipv6_lookup_struct,
1162 			&key_array[0], 4, ret);
1163 	dst_port[0] = ((ret[0] < 0) ? portid : ipv6_l3fwd_out_if[ret[0]]);
1164 	dst_port[1] = ((ret[1] < 0) ? portid : ipv6_l3fwd_out_if[ret[1]]);
1165 	dst_port[2] = ((ret[2] < 0) ? portid : ipv6_l3fwd_out_if[ret[2]]);
1166 	dst_port[3] = ((ret[3] < 0) ? portid : ipv6_l3fwd_out_if[ret[3]]);
1167 	dst_port[4] = ((ret[4] < 0) ? portid : ipv6_l3fwd_out_if[ret[4]]);
1168 	dst_port[5] = ((ret[5] < 0) ? portid : ipv6_l3fwd_out_if[ret[5]]);
1169 	dst_port[6] = ((ret[6] < 0) ? portid : ipv6_l3fwd_out_if[ret[6]]);
1170 	dst_port[7] = ((ret[7] < 0) ? portid : ipv6_l3fwd_out_if[ret[7]]);
1171 
1172 	if (dst_port[0] >= RTE_MAX_ETHPORTS ||
1173 			(enabled_port_mask & 1 << dst_port[0]) == 0)
1174 		dst_port[0] = portid;
1175 	if (dst_port[1] >= RTE_MAX_ETHPORTS ||
1176 			(enabled_port_mask & 1 << dst_port[1]) == 0)
1177 		dst_port[1] = portid;
1178 	if (dst_port[2] >= RTE_MAX_ETHPORTS ||
1179 			(enabled_port_mask & 1 << dst_port[2]) == 0)
1180 		dst_port[2] = portid;
1181 	if (dst_port[3] >= RTE_MAX_ETHPORTS ||
1182 			(enabled_port_mask & 1 << dst_port[3]) == 0)
1183 		dst_port[3] = portid;
1184 	if (dst_port[4] >= RTE_MAX_ETHPORTS ||
1185 			(enabled_port_mask & 1 << dst_port[4]) == 0)
1186 		dst_port[4] = portid;
1187 	if (dst_port[5] >= RTE_MAX_ETHPORTS ||
1188 			(enabled_port_mask & 1 << dst_port[5]) == 0)
1189 		dst_port[5] = portid;
1190 	if (dst_port[6] >= RTE_MAX_ETHPORTS ||
1191 			(enabled_port_mask & 1 << dst_port[6]) == 0)
1192 		dst_port[6] = portid;
1193 	if (dst_port[7] >= RTE_MAX_ETHPORTS ||
1194 			(enabled_port_mask & 1 << dst_port[7]) == 0)
1195 		dst_port[7] = portid;
1196 
1197 	/* dst addr */
1198 	*(uint64_t *)&eth_hdr[0]->d_addr = dest_eth_addr[dst_port[0]];
1199 	*(uint64_t *)&eth_hdr[1]->d_addr = dest_eth_addr[dst_port[1]];
1200 	*(uint64_t *)&eth_hdr[2]->d_addr = dest_eth_addr[dst_port[2]];
1201 	*(uint64_t *)&eth_hdr[3]->d_addr = dest_eth_addr[dst_port[3]];
1202 	*(uint64_t *)&eth_hdr[4]->d_addr = dest_eth_addr[dst_port[4]];
1203 	*(uint64_t *)&eth_hdr[5]->d_addr = dest_eth_addr[dst_port[5]];
1204 	*(uint64_t *)&eth_hdr[6]->d_addr = dest_eth_addr[dst_port[6]];
1205 	*(uint64_t *)&eth_hdr[7]->d_addr = dest_eth_addr[dst_port[7]];
1206 
1207 	/* src addr */
1208 	ether_addr_copy(&ports_eth_addr[dst_port[0]], &eth_hdr[0]->s_addr);
1209 	ether_addr_copy(&ports_eth_addr[dst_port[1]], &eth_hdr[1]->s_addr);
1210 	ether_addr_copy(&ports_eth_addr[dst_port[2]], &eth_hdr[2]->s_addr);
1211 	ether_addr_copy(&ports_eth_addr[dst_port[3]], &eth_hdr[3]->s_addr);
1212 	ether_addr_copy(&ports_eth_addr[dst_port[4]], &eth_hdr[4]->s_addr);
1213 	ether_addr_copy(&ports_eth_addr[dst_port[5]], &eth_hdr[5]->s_addr);
1214 	ether_addr_copy(&ports_eth_addr[dst_port[6]], &eth_hdr[6]->s_addr);
1215 	ether_addr_copy(&ports_eth_addr[dst_port[7]], &eth_hdr[7]->s_addr);
1216 
1217 	send_single_packet(m[0], dst_port[0]);
1218 	send_single_packet(m[1], dst_port[1]);
1219 	send_single_packet(m[2], dst_port[2]);
1220 	send_single_packet(m[3], dst_port[3]);
1221 	send_single_packet(m[4], dst_port[4]);
1222 	send_single_packet(m[5], dst_port[5]);
1223 	send_single_packet(m[6], dst_port[6]);
1224 	send_single_packet(m[7], dst_port[7]);
1225 
1226 }
1227 #endif /* APP_LOOKUP_METHOD */
1228 
1229 static __rte_always_inline void
1230 l3fwd_simple_forward(struct rte_mbuf *m, uint16_t portid)
1231 {
1232 	struct ether_hdr *eth_hdr;
1233 	struct ipv4_hdr *ipv4_hdr;
1234 	uint16_t dst_port;
1235 
1236 	eth_hdr = rte_pktmbuf_mtod(m, struct ether_hdr *);
1237 
1238 	if (RTE_ETH_IS_IPV4_HDR(m->packet_type)) {
1239 		/* Handle IPv4 headers.*/
1240 		ipv4_hdr = rte_pktmbuf_mtod_offset(m, struct ipv4_hdr *,
1241 				sizeof(struct ether_hdr));
1242 
1243 #ifdef DO_RFC_1812_CHECKS
1244 		/* Check to make sure the packet is valid (RFC1812) */
1245 		if (is_valid_ipv4_pkt(ipv4_hdr, m->pkt_len) < 0) {
1246 			rte_pktmbuf_free(m);
1247 			return;
1248 		}
1249 #endif
1250 
1251 		 dst_port = get_ipv4_dst_port(ipv4_hdr, portid,
1252 			RTE_PER_LCORE(lcore_conf)->ipv4_lookup_struct);
1253 		if (dst_port >= RTE_MAX_ETHPORTS ||
1254 				(enabled_port_mask & 1 << dst_port) == 0)
1255 			dst_port = portid;
1256 
1257 #ifdef DO_RFC_1812_CHECKS
1258 		/* Update time to live and header checksum */
1259 		--(ipv4_hdr->time_to_live);
1260 		++(ipv4_hdr->hdr_checksum);
1261 #endif
1262 		/* dst addr */
1263 		*(uint64_t *)&eth_hdr->d_addr = dest_eth_addr[dst_port];
1264 
1265 		/* src addr */
1266 		ether_addr_copy(&ports_eth_addr[dst_port], &eth_hdr->s_addr);
1267 
1268 		send_single_packet(m, dst_port);
1269 	} else if (RTE_ETH_IS_IPV6_HDR(m->packet_type)) {
1270 		/* Handle IPv6 headers.*/
1271 		struct ipv6_hdr *ipv6_hdr;
1272 
1273 		ipv6_hdr = rte_pktmbuf_mtod_offset(m, struct ipv6_hdr *,
1274 				sizeof(struct ether_hdr));
1275 
1276 		dst_port = get_ipv6_dst_port(ipv6_hdr, portid,
1277 				RTE_PER_LCORE(lcore_conf)->ipv6_lookup_struct);
1278 
1279 		if (dst_port >= RTE_MAX_ETHPORTS ||
1280 				(enabled_port_mask & 1 << dst_port) == 0)
1281 			dst_port = portid;
1282 
1283 		/* dst addr */
1284 		*(uint64_t *)&eth_hdr->d_addr = dest_eth_addr[dst_port];
1285 
1286 		/* src addr */
1287 		ether_addr_copy(&ports_eth_addr[dst_port], &eth_hdr->s_addr);
1288 
1289 		send_single_packet(m, dst_port);
1290 	} else
1291 		/* Free the mbuf that contains non-IPV4/IPV6 packet */
1292 		rte_pktmbuf_free(m);
1293 }
1294 
1295 #if ((APP_LOOKUP_METHOD == APP_LOOKUP_LPM) && \
1296 	(ENABLE_MULTI_BUFFER_OPTIMIZE == 1))
1297 #ifdef DO_RFC_1812_CHECKS
1298 
1299 #define	IPV4_MIN_VER_IHL	0x45
1300 #define	IPV4_MAX_VER_IHL	0x4f
1301 #define	IPV4_MAX_VER_IHL_DIFF	(IPV4_MAX_VER_IHL - IPV4_MIN_VER_IHL)
1302 
1303 /* Minimum value of IPV4 total length (20B) in network byte order. */
1304 #define	IPV4_MIN_LEN_BE	(sizeof(struct ipv4_hdr) << 8)
1305 
1306 /*
1307  * From http://www.rfc-editor.org/rfc/rfc1812.txt section 5.2.2:
1308  * - The IP version number must be 4.
1309  * - The IP header length field must be large enough to hold the
1310  *    minimum length legal IP datagram (20 bytes = 5 words).
1311  * - The IP total length field must be large enough to hold the IP
1312  *   datagram header, whose length is specified in the IP header length
1313  *   field.
1314  * If we encounter invalid IPV4 packet, then set destination port for it
1315  * to BAD_PORT value.
1316  */
1317 static __rte_always_inline void
1318 rfc1812_process(struct ipv4_hdr *ipv4_hdr, uint16_t *dp, uint32_t ptype)
1319 {
1320 	uint8_t ihl;
1321 
1322 	if (RTE_ETH_IS_IPV4_HDR(ptype)) {
1323 		ihl = ipv4_hdr->version_ihl - IPV4_MIN_VER_IHL;
1324 
1325 		ipv4_hdr->time_to_live--;
1326 		ipv4_hdr->hdr_checksum++;
1327 
1328 		if (ihl > IPV4_MAX_VER_IHL_DIFF ||
1329 				((uint8_t)ipv4_hdr->total_length == 0 &&
1330 				ipv4_hdr->total_length < IPV4_MIN_LEN_BE)) {
1331 			dp[0] = BAD_PORT;
1332 		}
1333 	}
1334 }
1335 
1336 #else
1337 #define	rfc1812_process(mb, dp, ptype)	do { } while (0)
1338 #endif /* DO_RFC_1812_CHECKS */
1339 #endif /* APP_LOOKUP_LPM && ENABLE_MULTI_BUFFER_OPTIMIZE */
1340 
1341 
1342 #if ((APP_LOOKUP_METHOD == APP_LOOKUP_LPM) && \
1343 	(ENABLE_MULTI_BUFFER_OPTIMIZE == 1))
1344 
1345 static __rte_always_inline uint16_t
1346 get_dst_port(struct rte_mbuf *pkt, uint32_t dst_ipv4, uint16_t portid)
1347 {
1348 	uint32_t next_hop;
1349 	struct ipv6_hdr *ipv6_hdr;
1350 	struct ether_hdr *eth_hdr;
1351 
1352 	if (RTE_ETH_IS_IPV4_HDR(pkt->packet_type)) {
1353 		return (uint16_t) ((rte_lpm_lookup(
1354 				RTE_PER_LCORE(lcore_conf)->ipv4_lookup_struct, dst_ipv4,
1355 				&next_hop) == 0) ? next_hop : portid);
1356 
1357 	} else if (RTE_ETH_IS_IPV6_HDR(pkt->packet_type)) {
1358 
1359 		eth_hdr = rte_pktmbuf_mtod(pkt, struct ether_hdr *);
1360 		ipv6_hdr = (struct ipv6_hdr *)(eth_hdr + 1);
1361 
1362 		return (uint16_t) ((rte_lpm6_lookup(
1363 				RTE_PER_LCORE(lcore_conf)->ipv6_lookup_struct,
1364 				ipv6_hdr->dst_addr, &next_hop) == 0) ?
1365 				next_hop : portid);
1366 
1367 	}
1368 
1369 	return portid;
1370 }
1371 
1372 static inline void
1373 process_packet(struct rte_mbuf *pkt, uint16_t *dst_port, uint16_t portid)
1374 {
1375 	struct ether_hdr *eth_hdr;
1376 	struct ipv4_hdr *ipv4_hdr;
1377 	uint32_t dst_ipv4;
1378 	uint16_t dp;
1379 	__m128i te, ve;
1380 
1381 	eth_hdr = rte_pktmbuf_mtod(pkt, struct ether_hdr *);
1382 	ipv4_hdr = (struct ipv4_hdr *)(eth_hdr + 1);
1383 
1384 	dst_ipv4 = ipv4_hdr->dst_addr;
1385 	dst_ipv4 = rte_be_to_cpu_32(dst_ipv4);
1386 	dp = get_dst_port(pkt, dst_ipv4, portid);
1387 
1388 	te = _mm_load_si128((__m128i *)eth_hdr);
1389 	ve = val_eth[dp];
1390 
1391 	dst_port[0] = dp;
1392 	rfc1812_process(ipv4_hdr, dst_port, pkt->packet_type);
1393 
1394 	te =  _mm_blend_epi16(te, ve, MASK_ETH);
1395 	_mm_store_si128((__m128i *)eth_hdr, te);
1396 }
1397 
1398 /*
1399  * Read packet_type and destination IPV4 addresses from 4 mbufs.
1400  */
1401 static inline void
1402 processx4_step1(struct rte_mbuf *pkt[FWDSTEP],
1403 		__m128i *dip,
1404 		uint32_t *ipv4_flag)
1405 {
1406 	struct ipv4_hdr *ipv4_hdr;
1407 	struct ether_hdr *eth_hdr;
1408 	uint32_t x0, x1, x2, x3;
1409 
1410 	eth_hdr = rte_pktmbuf_mtod(pkt[0], struct ether_hdr *);
1411 	ipv4_hdr = (struct ipv4_hdr *)(eth_hdr + 1);
1412 	x0 = ipv4_hdr->dst_addr;
1413 	ipv4_flag[0] = pkt[0]->packet_type & RTE_PTYPE_L3_IPV4;
1414 
1415 	eth_hdr = rte_pktmbuf_mtod(pkt[1], struct ether_hdr *);
1416 	ipv4_hdr = (struct ipv4_hdr *)(eth_hdr + 1);
1417 	x1 = ipv4_hdr->dst_addr;
1418 	ipv4_flag[0] &= pkt[1]->packet_type;
1419 
1420 	eth_hdr = rte_pktmbuf_mtod(pkt[2], struct ether_hdr *);
1421 	ipv4_hdr = (struct ipv4_hdr *)(eth_hdr + 1);
1422 	x2 = ipv4_hdr->dst_addr;
1423 	ipv4_flag[0] &= pkt[2]->packet_type;
1424 
1425 	eth_hdr = rte_pktmbuf_mtod(pkt[3], struct ether_hdr *);
1426 	ipv4_hdr = (struct ipv4_hdr *)(eth_hdr + 1);
1427 	x3 = ipv4_hdr->dst_addr;
1428 	ipv4_flag[0] &= pkt[3]->packet_type;
1429 
1430 	dip[0] = _mm_set_epi32(x3, x2, x1, x0);
1431 }
1432 
1433 /*
1434  * Lookup into LPM for destination port.
1435  * If lookup fails, use incoming port (portid) as destination port.
1436  */
1437 static inline void
1438 processx4_step2(__m128i dip,
1439 		uint32_t ipv4_flag,
1440 		uint16_t portid,
1441 		struct rte_mbuf *pkt[FWDSTEP],
1442 		uint16_t dprt[FWDSTEP])
1443 {
1444 	rte_xmm_t dst;
1445 	const __m128i bswap_mask = _mm_set_epi8(12, 13, 14, 15, 8, 9, 10, 11,
1446 			4, 5, 6, 7, 0, 1, 2, 3);
1447 
1448 	/* Byte swap 4 IPV4 addresses. */
1449 	dip = _mm_shuffle_epi8(dip, bswap_mask);
1450 
1451 	/* if all 4 packets are IPV4. */
1452 	if (likely(ipv4_flag)) {
1453 		rte_lpm_lookupx4(RTE_PER_LCORE(lcore_conf)->ipv4_lookup_struct, dip,
1454 				dst.u32, portid);
1455 
1456 		/* get rid of unused upper 16 bit for each dport. */
1457 		dst.x = _mm_packs_epi32(dst.x, dst.x);
1458 		*(uint64_t *)dprt = dst.u64[0];
1459 	} else {
1460 		dst.x = dip;
1461 		dprt[0] = get_dst_port(pkt[0], dst.u32[0], portid);
1462 		dprt[1] = get_dst_port(pkt[1], dst.u32[1], portid);
1463 		dprt[2] = get_dst_port(pkt[2], dst.u32[2], portid);
1464 		dprt[3] = get_dst_port(pkt[3], dst.u32[3], portid);
1465 	}
1466 }
1467 
1468 /*
1469  * Update source and destination MAC addresses in the ethernet header.
1470  * Perform RFC1812 checks and updates for IPV4 packets.
1471  */
1472 static inline void
1473 processx4_step3(struct rte_mbuf *pkt[FWDSTEP], uint16_t dst_port[FWDSTEP])
1474 {
1475 	__m128i te[FWDSTEP];
1476 	__m128i ve[FWDSTEP];
1477 	__m128i *p[FWDSTEP];
1478 
1479 	p[0] = rte_pktmbuf_mtod(pkt[0], __m128i *);
1480 	p[1] = rte_pktmbuf_mtod(pkt[1], __m128i *);
1481 	p[2] = rte_pktmbuf_mtod(pkt[2], __m128i *);
1482 	p[3] = rte_pktmbuf_mtod(pkt[3], __m128i *);
1483 
1484 	ve[0] = val_eth[dst_port[0]];
1485 	te[0] = _mm_load_si128(p[0]);
1486 
1487 	ve[1] = val_eth[dst_port[1]];
1488 	te[1] = _mm_load_si128(p[1]);
1489 
1490 	ve[2] = val_eth[dst_port[2]];
1491 	te[2] = _mm_load_si128(p[2]);
1492 
1493 	ve[3] = val_eth[dst_port[3]];
1494 	te[3] = _mm_load_si128(p[3]);
1495 
1496 	/* Update first 12 bytes, keep rest bytes intact. */
1497 	te[0] =  _mm_blend_epi16(te[0], ve[0], MASK_ETH);
1498 	te[1] =  _mm_blend_epi16(te[1], ve[1], MASK_ETH);
1499 	te[2] =  _mm_blend_epi16(te[2], ve[2], MASK_ETH);
1500 	te[3] =  _mm_blend_epi16(te[3], ve[3], MASK_ETH);
1501 
1502 	_mm_store_si128(p[0], te[0]);
1503 	_mm_store_si128(p[1], te[1]);
1504 	_mm_store_si128(p[2], te[2]);
1505 	_mm_store_si128(p[3], te[3]);
1506 
1507 	rfc1812_process((struct ipv4_hdr *)((struct ether_hdr *)p[0] + 1),
1508 			&dst_port[0], pkt[0]->packet_type);
1509 	rfc1812_process((struct ipv4_hdr *)((struct ether_hdr *)p[1] + 1),
1510 			&dst_port[1], pkt[1]->packet_type);
1511 	rfc1812_process((struct ipv4_hdr *)((struct ether_hdr *)p[2] + 1),
1512 			&dst_port[2], pkt[2]->packet_type);
1513 	rfc1812_process((struct ipv4_hdr *)((struct ether_hdr *)p[3] + 1),
1514 			&dst_port[3], pkt[3]->packet_type);
1515 }
1516 
1517 /*
1518  * We group consecutive packets with the same destionation port into one burst.
1519  * To avoid extra latency this is done together with some other packet
1520  * processing, but after we made a final decision about packet's destination.
1521  * To do this we maintain:
1522  * pnum - array of number of consecutive packets with the same dest port for
1523  * each packet in the input burst.
1524  * lp - pointer to the last updated element in the pnum.
1525  * dlp - dest port value lp corresponds to.
1526  */
1527 
1528 #define	GRPSZ	(1 << FWDSTEP)
1529 #define	GRPMSK	(GRPSZ - 1)
1530 
1531 #define GROUP_PORT_STEP(dlp, dcp, lp, pn, idx)	do { \
1532 	if (likely((dlp) == (dcp)[(idx)])) {         \
1533 		(lp)[0]++;                           \
1534 	} else {                                     \
1535 		(dlp) = (dcp)[idx];                  \
1536 		(lp) = (pn) + (idx);                 \
1537 		(lp)[0] = 1;                         \
1538 	}                                            \
1539 } while (0)
1540 
1541 /*
1542  * Group consecutive packets with the same destination port in bursts of 4.
1543  * Suppose we have array of destionation ports:
1544  * dst_port[] = {a, b, c, d,, e, ... }
1545  * dp1 should contain: <a, b, c, d>, dp2: <b, c, d, e>.
1546  * We doing 4 comparisons at once and the result is 4 bit mask.
1547  * This mask is used as an index into prebuild array of pnum values.
1548  */
1549 static inline uint16_t *
1550 port_groupx4(uint16_t pn[FWDSTEP + 1], uint16_t *lp, __m128i dp1, __m128i dp2)
1551 {
1552 	static const struct {
1553 		uint64_t pnum; /* prebuild 4 values for pnum[]. */
1554 		int32_t  idx;  /* index for new last updated elemnet. */
1555 		uint16_t lpv;  /* add value to the last updated element. */
1556 	} gptbl[GRPSZ] = {
1557 	{
1558 		/* 0: a != b, b != c, c != d, d != e */
1559 		.pnum = UINT64_C(0x0001000100010001),
1560 		.idx = 4,
1561 		.lpv = 0,
1562 	},
1563 	{
1564 		/* 1: a == b, b != c, c != d, d != e */
1565 		.pnum = UINT64_C(0x0001000100010002),
1566 		.idx = 4,
1567 		.lpv = 1,
1568 	},
1569 	{
1570 		/* 2: a != b, b == c, c != d, d != e */
1571 		.pnum = UINT64_C(0x0001000100020001),
1572 		.idx = 4,
1573 		.lpv = 0,
1574 	},
1575 	{
1576 		/* 3: a == b, b == c, c != d, d != e */
1577 		.pnum = UINT64_C(0x0001000100020003),
1578 		.idx = 4,
1579 		.lpv = 2,
1580 	},
1581 	{
1582 		/* 4: a != b, b != c, c == d, d != e */
1583 		.pnum = UINT64_C(0x0001000200010001),
1584 		.idx = 4,
1585 		.lpv = 0,
1586 	},
1587 	{
1588 		/* 5: a == b, b != c, c == d, d != e */
1589 		.pnum = UINT64_C(0x0001000200010002),
1590 		.idx = 4,
1591 		.lpv = 1,
1592 	},
1593 	{
1594 		/* 6: a != b, b == c, c == d, d != e */
1595 		.pnum = UINT64_C(0x0001000200030001),
1596 		.idx = 4,
1597 		.lpv = 0,
1598 	},
1599 	{
1600 		/* 7: a == b, b == c, c == d, d != e */
1601 		.pnum = UINT64_C(0x0001000200030004),
1602 		.idx = 4,
1603 		.lpv = 3,
1604 	},
1605 	{
1606 		/* 8: a != b, b != c, c != d, d == e */
1607 		.pnum = UINT64_C(0x0002000100010001),
1608 		.idx = 3,
1609 		.lpv = 0,
1610 	},
1611 	{
1612 		/* 9: a == b, b != c, c != d, d == e */
1613 		.pnum = UINT64_C(0x0002000100010002),
1614 		.idx = 3,
1615 		.lpv = 1,
1616 	},
1617 	{
1618 		/* 0xa: a != b, b == c, c != d, d == e */
1619 		.pnum = UINT64_C(0x0002000100020001),
1620 		.idx = 3,
1621 		.lpv = 0,
1622 	},
1623 	{
1624 		/* 0xb: a == b, b == c, c != d, d == e */
1625 		.pnum = UINT64_C(0x0002000100020003),
1626 		.idx = 3,
1627 		.lpv = 2,
1628 	},
1629 	{
1630 		/* 0xc: a != b, b != c, c == d, d == e */
1631 		.pnum = UINT64_C(0x0002000300010001),
1632 		.idx = 2,
1633 		.lpv = 0,
1634 	},
1635 	{
1636 		/* 0xd: a == b, b != c, c == d, d == e */
1637 		.pnum = UINT64_C(0x0002000300010002),
1638 		.idx = 2,
1639 		.lpv = 1,
1640 	},
1641 	{
1642 		/* 0xe: a != b, b == c, c == d, d == e */
1643 		.pnum = UINT64_C(0x0002000300040001),
1644 		.idx = 1,
1645 		.lpv = 0,
1646 	},
1647 	{
1648 		/* 0xf: a == b, b == c, c == d, d == e */
1649 		.pnum = UINT64_C(0x0002000300040005),
1650 		.idx = 0,
1651 		.lpv = 4,
1652 	},
1653 	};
1654 
1655 	union {
1656 		uint16_t u16[FWDSTEP + 1];
1657 		uint64_t u64;
1658 	} *pnum = (void *)pn;
1659 
1660 	int32_t v;
1661 
1662 	dp1 = _mm_cmpeq_epi16(dp1, dp2);
1663 	dp1 = _mm_unpacklo_epi16(dp1, dp1);
1664 	v = _mm_movemask_ps((__m128)dp1);
1665 
1666 	/* update last port counter. */
1667 	lp[0] += gptbl[v].lpv;
1668 
1669 	/* if dest port value has changed. */
1670 	if (v != GRPMSK) {
1671 		pnum->u64 = gptbl[v].pnum;
1672 		pnum->u16[FWDSTEP] = 1;
1673 		lp = pnum->u16 + gptbl[v].idx;
1674 	}
1675 
1676 	return lp;
1677 }
1678 
1679 #endif /* APP_LOOKUP_METHOD */
1680 
1681 static void
1682 process_burst(struct rte_mbuf *pkts_burst[MAX_PKT_BURST], int nb_rx,
1683 		uint16_t portid)
1684 {
1685 
1686 	int j;
1687 
1688 #if ((APP_LOOKUP_METHOD == APP_LOOKUP_LPM) && \
1689 	(ENABLE_MULTI_BUFFER_OPTIMIZE == 1))
1690 	int32_t k;
1691 	uint16_t dlp;
1692 	uint16_t *lp;
1693 	uint16_t dst_port[MAX_PKT_BURST];
1694 	__m128i dip[MAX_PKT_BURST / FWDSTEP];
1695 	uint32_t ipv4_flag[MAX_PKT_BURST / FWDSTEP];
1696 	uint16_t pnum[MAX_PKT_BURST + 1];
1697 #endif
1698 
1699 
1700 #if (ENABLE_MULTI_BUFFER_OPTIMIZE == 1)
1701 #if (APP_LOOKUP_METHOD == APP_LOOKUP_EXACT_MATCH)
1702 	{
1703 		/*
1704 		 * Send nb_rx - nb_rx%8 packets
1705 		 * in groups of 8.
1706 		 */
1707 		int32_t n = RTE_ALIGN_FLOOR(nb_rx, 8);
1708 
1709 		for (j = 0; j < n; j += 8) {
1710 			uint32_t pkt_type =
1711 				pkts_burst[j]->packet_type &
1712 				pkts_burst[j+1]->packet_type &
1713 				pkts_burst[j+2]->packet_type &
1714 				pkts_burst[j+3]->packet_type &
1715 				pkts_burst[j+4]->packet_type &
1716 				pkts_burst[j+5]->packet_type &
1717 				pkts_burst[j+6]->packet_type &
1718 				pkts_burst[j+7]->packet_type;
1719 			if (pkt_type & RTE_PTYPE_L3_IPV4) {
1720 				simple_ipv4_fwd_8pkts(&pkts_burst[j], portid);
1721 			} else if (pkt_type &
1722 				RTE_PTYPE_L3_IPV6) {
1723 				simple_ipv6_fwd_8pkts(&pkts_burst[j], portid);
1724 			} else {
1725 				l3fwd_simple_forward(pkts_burst[j], portid);
1726 				l3fwd_simple_forward(pkts_burst[j+1], portid);
1727 				l3fwd_simple_forward(pkts_burst[j+2], portid);
1728 				l3fwd_simple_forward(pkts_burst[j+3], portid);
1729 				l3fwd_simple_forward(pkts_burst[j+4], portid);
1730 				l3fwd_simple_forward(pkts_burst[j+5], portid);
1731 				l3fwd_simple_forward(pkts_burst[j+6], portid);
1732 				l3fwd_simple_forward(pkts_burst[j+7], portid);
1733 			}
1734 		}
1735 		for (; j < nb_rx ; j++)
1736 			l3fwd_simple_forward(pkts_burst[j], portid);
1737 	}
1738 #elif (APP_LOOKUP_METHOD == APP_LOOKUP_LPM)
1739 
1740 	k = RTE_ALIGN_FLOOR(nb_rx, FWDSTEP);
1741 	for (j = 0; j != k; j += FWDSTEP)
1742 		processx4_step1(&pkts_burst[j], &dip[j / FWDSTEP],
1743 				&ipv4_flag[j / FWDSTEP]);
1744 
1745 	k = RTE_ALIGN_FLOOR(nb_rx, FWDSTEP);
1746 	for (j = 0; j != k; j += FWDSTEP)
1747 		processx4_step2(dip[j / FWDSTEP], ipv4_flag[j / FWDSTEP],
1748 				portid, &pkts_burst[j], &dst_port[j]);
1749 
1750 	/*
1751 	 * Finish packet processing and group consecutive
1752 	 * packets with the same destination port.
1753 	 */
1754 	k = RTE_ALIGN_FLOOR(nb_rx, FWDSTEP);
1755 	if (k != 0) {
1756 		__m128i dp1, dp2;
1757 
1758 		lp = pnum;
1759 		lp[0] = 1;
1760 
1761 		processx4_step3(pkts_burst, dst_port);
1762 
1763 		/* dp1: <d[0], d[1], d[2], d[3], ... > */
1764 		dp1 = _mm_loadu_si128((__m128i *)dst_port);
1765 
1766 		for (j = FWDSTEP; j != k; j += FWDSTEP) {
1767 			processx4_step3(&pkts_burst[j], &dst_port[j]);
1768 
1769 			/*
1770 			 * dp2:
1771 			 * <d[j-3], d[j-2], d[j-1], d[j], ... >
1772 			 */
1773 			dp2 = _mm_loadu_si128(
1774 					(__m128i *)&dst_port[j - FWDSTEP + 1]);
1775 			lp  = port_groupx4(&pnum[j - FWDSTEP], lp, dp1, dp2);
1776 
1777 			/*
1778 			 * dp1:
1779 			 * <d[j], d[j+1], d[j+2], d[j+3], ... >
1780 			 */
1781 			dp1 = _mm_srli_si128(dp2, (FWDSTEP - 1) *
1782 					sizeof(dst_port[0]));
1783 		}
1784 
1785 		/*
1786 		 * dp2: <d[j-3], d[j-2], d[j-1], d[j-1], ... >
1787 		 */
1788 		dp2 = _mm_shufflelo_epi16(dp1, 0xf9);
1789 		lp  = port_groupx4(&pnum[j - FWDSTEP], lp, dp1, dp2);
1790 
1791 		/*
1792 		 * remove values added by the last repeated
1793 		 * dst port.
1794 		 */
1795 		lp[0]--;
1796 		dlp = dst_port[j - 1];
1797 	} else {
1798 		/* set dlp and lp to the never used values. */
1799 		dlp = BAD_PORT - 1;
1800 		lp = pnum + MAX_PKT_BURST;
1801 	}
1802 
1803 	/* Process up to last 3 packets one by one. */
1804 	switch (nb_rx % FWDSTEP) {
1805 	case 3:
1806 		process_packet(pkts_burst[j], dst_port + j, portid);
1807 		GROUP_PORT_STEP(dlp, dst_port, lp, pnum, j);
1808 		j++;
1809 		/* fall-through */
1810 	case 2:
1811 		process_packet(pkts_burst[j], dst_port + j, portid);
1812 		GROUP_PORT_STEP(dlp, dst_port, lp, pnum, j);
1813 		j++;
1814 		/* fall-through */
1815 	case 1:
1816 		process_packet(pkts_burst[j], dst_port + j, portid);
1817 		GROUP_PORT_STEP(dlp, dst_port, lp, pnum, j);
1818 		j++;
1819 	}
1820 
1821 	/*
1822 	 * Send packets out, through destination port.
1823 	 * Consecuteve pacekts with the same destination port
1824 	 * are already grouped together.
1825 	 * If destination port for the packet equals BAD_PORT,
1826 	 * then free the packet without sending it out.
1827 	 */
1828 	for (j = 0; j < nb_rx; j += k) {
1829 
1830 		int32_t m;
1831 		uint16_t pn;
1832 
1833 		pn = dst_port[j];
1834 		k = pnum[j];
1835 
1836 		if (likely(pn != BAD_PORT))
1837 			send_packetsx4(pn, pkts_burst + j, k);
1838 		else
1839 			for (m = j; m != j + k; m++)
1840 				rte_pktmbuf_free(pkts_burst[m]);
1841 
1842 	}
1843 
1844 #endif /* APP_LOOKUP_METHOD */
1845 #else /* ENABLE_MULTI_BUFFER_OPTIMIZE == 0 */
1846 
1847 	/* Prefetch first packets */
1848 	for (j = 0; j < PREFETCH_OFFSET && j < nb_rx; j++)
1849 		rte_prefetch0(rte_pktmbuf_mtod(pkts_burst[j], void *));
1850 
1851 	/* Prefetch and forward already prefetched packets */
1852 	for (j = 0; j < (nb_rx - PREFETCH_OFFSET); j++) {
1853 		rte_prefetch0(rte_pktmbuf_mtod(pkts_burst[
1854 				j + PREFETCH_OFFSET], void *));
1855 		l3fwd_simple_forward(pkts_burst[j], portid);
1856 	}
1857 
1858 	/* Forward remaining prefetched packets */
1859 	for (; j < nb_rx; j++)
1860 		l3fwd_simple_forward(pkts_burst[j], portid);
1861 
1862 #endif /* ENABLE_MULTI_BUFFER_OPTIMIZE */
1863 
1864 }
1865 
1866 #if (APP_CPU_LOAD > 0)
1867 
1868 /*
1869  * CPU-load stats collector
1870  */
1871 static int
1872 cpu_load_collector(__rte_unused void *arg) {
1873 	unsigned i, j, k;
1874 	uint64_t hits;
1875 	uint64_t prev_tsc, diff_tsc, cur_tsc;
1876 	uint64_t total[MAX_CPU] = { 0 };
1877 	unsigned min_cpu = MAX_CPU;
1878 	unsigned max_cpu = 0;
1879 	unsigned cpu_id;
1880 	int busy_total = 0;
1881 	int busy_flag = 0;
1882 
1883 	unsigned int n_thread_per_cpu[MAX_CPU] = { 0 };
1884 	struct thread_conf *thread_per_cpu[MAX_CPU][MAX_THREAD];
1885 
1886 	struct thread_conf *thread_conf;
1887 
1888 	const uint64_t interval_tsc = (rte_get_tsc_hz() + US_PER_S - 1) /
1889 		US_PER_S * CPU_LOAD_TIMEOUT_US;
1890 
1891 	prev_tsc = 0;
1892 	/*
1893 	 * Wait for all threads
1894 	 */
1895 
1896 	printf("Waiting for %d rx threads and %d tx threads\n", n_rx_thread,
1897 			n_tx_thread);
1898 
1899 	while (rte_atomic16_read(&rx_counter) < n_rx_thread)
1900 		rte_pause();
1901 
1902 	while (rte_atomic16_read(&tx_counter) < n_tx_thread)
1903 		rte_pause();
1904 
1905 	for (i = 0; i < n_rx_thread; i++) {
1906 
1907 		thread_conf = &rx_thread[i].conf;
1908 		cpu_id = thread_conf->cpu_id;
1909 		thread_per_cpu[cpu_id][n_thread_per_cpu[cpu_id]++] = thread_conf;
1910 
1911 		if (cpu_id > max_cpu)
1912 			max_cpu = cpu_id;
1913 		if (cpu_id < min_cpu)
1914 			min_cpu = cpu_id;
1915 	}
1916 	for (i = 0; i < n_tx_thread; i++) {
1917 
1918 		thread_conf = &tx_thread[i].conf;
1919 		cpu_id = thread_conf->cpu_id;
1920 		thread_per_cpu[cpu_id][n_thread_per_cpu[cpu_id]++] = thread_conf;
1921 
1922 		if (thread_conf->cpu_id > max_cpu)
1923 			max_cpu = thread_conf->cpu_id;
1924 		if (thread_conf->cpu_id < min_cpu)
1925 			min_cpu = thread_conf->cpu_id;
1926 	}
1927 
1928 	while (1) {
1929 
1930 		cpu_load.counter++;
1931 		for (i = min_cpu; i <= max_cpu; i++) {
1932 			for (j = 0; j < MAX_CPU_COUNTER; j++) {
1933 				for (k = 0; k < n_thread_per_cpu[i]; k++)
1934 					if (thread_per_cpu[i][k]->busy[j]) {
1935 						busy_flag = 1;
1936 						break;
1937 					}
1938 				if (busy_flag) {
1939 					cpu_load.hits[j][i]++;
1940 					busy_total = 1;
1941 					busy_flag = 0;
1942 				}
1943 			}
1944 
1945 			if (busy_total) {
1946 				total[i]++;
1947 				busy_total = 0;
1948 			}
1949 		}
1950 
1951 		cur_tsc = rte_rdtsc();
1952 
1953 		diff_tsc = cur_tsc - prev_tsc;
1954 		if (unlikely(diff_tsc > interval_tsc)) {
1955 
1956 			printf("\033c");
1957 
1958 			printf("Cpu usage for %d rx threads and %d tx threads:\n\n",
1959 					n_rx_thread, n_tx_thread);
1960 
1961 			printf("cpu#     proc%%  poll%%  overhead%%\n\n");
1962 
1963 			for (i = min_cpu; i <= max_cpu; i++) {
1964 				hits = 0;
1965 				printf("CPU %d:", i);
1966 				for (j = 0; j < MAX_CPU_COUNTER; j++) {
1967 					printf("%7" PRIu64 "",
1968 							cpu_load.hits[j][i] * 100 / cpu_load.counter);
1969 					hits += cpu_load.hits[j][i];
1970 					cpu_load.hits[j][i] = 0;
1971 				}
1972 				printf("%7" PRIu64 "\n",
1973 						100 - total[i] * 100 / cpu_load.counter);
1974 				total[i] = 0;
1975 			}
1976 			cpu_load.counter = 0;
1977 
1978 			prev_tsc = cur_tsc;
1979 		}
1980 
1981 	}
1982 }
1983 #endif /* APP_CPU_LOAD */
1984 
1985 /*
1986  * Null processing lthread loop
1987  *
1988  * This loop is used to start empty scheduler on lcore.
1989  */
1990 static void *
1991 lthread_null(__rte_unused void *args)
1992 {
1993 	int lcore_id = rte_lcore_id();
1994 
1995 	RTE_LOG(INFO, L3FWD, "Starting scheduler on lcore %d.\n", lcore_id);
1996 	lthread_exit(NULL);
1997 	return NULL;
1998 }
1999 
2000 /* main processing loop */
2001 static void *
2002 lthread_tx_per_ring(void *dummy)
2003 {
2004 	int nb_rx;
2005 	uint16_t portid;
2006 	struct rte_ring *ring;
2007 	struct thread_tx_conf *tx_conf;
2008 	struct rte_mbuf *pkts_burst[MAX_PKT_BURST];
2009 	struct lthread_cond *ready;
2010 
2011 	tx_conf = (struct thread_tx_conf *)dummy;
2012 	ring = tx_conf->ring;
2013 	ready = *tx_conf->ready;
2014 
2015 	lthread_set_data((void *)tx_conf);
2016 
2017 	/*
2018 	 * Move this lthread to lcore
2019 	 */
2020 	lthread_set_affinity(tx_conf->conf.lcore_id);
2021 
2022 	RTE_LOG(INFO, L3FWD, "entering main tx loop on lcore %u\n", rte_lcore_id());
2023 
2024 	nb_rx = 0;
2025 	rte_atomic16_inc(&tx_counter);
2026 	while (1) {
2027 
2028 		/*
2029 		 * Read packet from ring
2030 		 */
2031 		SET_CPU_BUSY(tx_conf, CPU_POLL);
2032 		nb_rx = rte_ring_sc_dequeue_burst(ring, (void **)pkts_burst,
2033 				MAX_PKT_BURST, NULL);
2034 		SET_CPU_IDLE(tx_conf, CPU_POLL);
2035 
2036 		if (nb_rx > 0) {
2037 			SET_CPU_BUSY(tx_conf, CPU_PROCESS);
2038 			portid = pkts_burst[0]->port;
2039 			process_burst(pkts_burst, nb_rx, portid);
2040 			SET_CPU_IDLE(tx_conf, CPU_PROCESS);
2041 			lthread_yield();
2042 		} else
2043 			lthread_cond_wait(ready, 0);
2044 
2045 	}
2046 	return NULL;
2047 }
2048 
2049 /*
2050  * Main tx-lthreads spawner lthread.
2051  *
2052  * This lthread is used to spawn one new lthread per ring from producers.
2053  *
2054  */
2055 static void *
2056 lthread_tx(void *args)
2057 {
2058 	struct lthread *lt;
2059 
2060 	unsigned lcore_id;
2061 	uint16_t portid;
2062 	struct thread_tx_conf *tx_conf;
2063 
2064 	tx_conf = (struct thread_tx_conf *)args;
2065 	lthread_set_data((void *)tx_conf);
2066 
2067 	/*
2068 	 * Move this lthread to the selected lcore
2069 	 */
2070 	lthread_set_affinity(tx_conf->conf.lcore_id);
2071 
2072 	/*
2073 	 * Spawn tx readers (one per input ring)
2074 	 */
2075 	lthread_create(&lt, tx_conf->conf.lcore_id, lthread_tx_per_ring,
2076 			(void *)tx_conf);
2077 
2078 	lcore_id = rte_lcore_id();
2079 
2080 	RTE_LOG(INFO, L3FWD, "Entering Tx main loop on lcore %u\n", lcore_id);
2081 
2082 	tx_conf->conf.cpu_id = sched_getcpu();
2083 	while (1) {
2084 
2085 		lthread_sleep(BURST_TX_DRAIN_US * 1000);
2086 
2087 		/*
2088 		 * TX burst queue drain
2089 		 */
2090 		for (portid = 0; portid < RTE_MAX_ETHPORTS; portid++) {
2091 			if (tx_conf->tx_mbufs[portid].len == 0)
2092 				continue;
2093 			SET_CPU_BUSY(tx_conf, CPU_PROCESS);
2094 			send_burst(tx_conf, tx_conf->tx_mbufs[portid].len, portid);
2095 			SET_CPU_IDLE(tx_conf, CPU_PROCESS);
2096 			tx_conf->tx_mbufs[portid].len = 0;
2097 		}
2098 
2099 	}
2100 	return NULL;
2101 }
2102 
2103 static void *
2104 lthread_rx(void *dummy)
2105 {
2106 	int ret;
2107 	uint16_t nb_rx;
2108 	int i;
2109 	uint16_t portid;
2110 	uint8_t queueid;
2111 	int worker_id;
2112 	int len[RTE_MAX_LCORE] = { 0 };
2113 	int old_len, new_len;
2114 	struct rte_mbuf *pkts_burst[MAX_PKT_BURST];
2115 	struct thread_rx_conf *rx_conf;
2116 
2117 	rx_conf = (struct thread_rx_conf *)dummy;
2118 	lthread_set_data((void *)rx_conf);
2119 
2120 	/*
2121 	 * Move this lthread to lcore
2122 	 */
2123 	lthread_set_affinity(rx_conf->conf.lcore_id);
2124 
2125 	if (rx_conf->n_rx_queue == 0) {
2126 		RTE_LOG(INFO, L3FWD, "lcore %u has nothing to do\n", rte_lcore_id());
2127 		return NULL;
2128 	}
2129 
2130 	RTE_LOG(INFO, L3FWD, "Entering main Rx loop on lcore %u\n", rte_lcore_id());
2131 
2132 	for (i = 0; i < rx_conf->n_rx_queue; i++) {
2133 
2134 		portid = rx_conf->rx_queue_list[i].port_id;
2135 		queueid = rx_conf->rx_queue_list[i].queue_id;
2136 		RTE_LOG(INFO, L3FWD,
2137 			" -- lcoreid=%u portid=%u rxqueueid=%hhu\n",
2138 				rte_lcore_id(), portid, queueid);
2139 	}
2140 
2141 	/*
2142 	 * Init all condition variables (one per rx thread)
2143 	 */
2144 	for (i = 0; i < rx_conf->n_rx_queue; i++)
2145 		lthread_cond_init(NULL, &rx_conf->ready[i], NULL);
2146 
2147 	worker_id = 0;
2148 
2149 	rx_conf->conf.cpu_id = sched_getcpu();
2150 	rte_atomic16_inc(&rx_counter);
2151 	while (1) {
2152 
2153 		/*
2154 		 * Read packet from RX queues
2155 		 */
2156 		for (i = 0; i < rx_conf->n_rx_queue; ++i) {
2157 			portid = rx_conf->rx_queue_list[i].port_id;
2158 			queueid = rx_conf->rx_queue_list[i].queue_id;
2159 
2160 			SET_CPU_BUSY(rx_conf, CPU_POLL);
2161 			nb_rx = rte_eth_rx_burst(portid, queueid, pkts_burst,
2162 				MAX_PKT_BURST);
2163 			SET_CPU_IDLE(rx_conf, CPU_POLL);
2164 
2165 			if (nb_rx != 0) {
2166 				worker_id = (worker_id + 1) % rx_conf->n_ring;
2167 				old_len = len[worker_id];
2168 
2169 				SET_CPU_BUSY(rx_conf, CPU_PROCESS);
2170 				ret = rte_ring_sp_enqueue_burst(
2171 						rx_conf->ring[worker_id],
2172 						(void **) pkts_burst,
2173 						nb_rx, NULL);
2174 
2175 				new_len = old_len + ret;
2176 
2177 				if (new_len >= BURST_SIZE) {
2178 					lthread_cond_signal(rx_conf->ready[worker_id]);
2179 					new_len = 0;
2180 				}
2181 
2182 				len[worker_id] = new_len;
2183 
2184 				if (unlikely(ret < nb_rx)) {
2185 					uint32_t k;
2186 
2187 					for (k = ret; k < nb_rx; k++) {
2188 						struct rte_mbuf *m = pkts_burst[k];
2189 
2190 						rte_pktmbuf_free(m);
2191 					}
2192 				}
2193 				SET_CPU_IDLE(rx_conf, CPU_PROCESS);
2194 			}
2195 
2196 			lthread_yield();
2197 		}
2198 	}
2199 	return NULL;
2200 }
2201 
2202 /*
2203  * Start scheduler with initial lthread on lcore
2204  *
2205  * This lthread loop spawns all rx and tx lthreads on master lcore
2206  */
2207 
2208 static void *
2209 lthread_spawner(__rte_unused void *arg)
2210 {
2211 	struct lthread *lt[MAX_THREAD];
2212 	int i;
2213 	int n_thread = 0;
2214 
2215 	printf("Entering lthread_spawner\n");
2216 
2217 	/*
2218 	 * Create producers (rx threads) on default lcore
2219 	 */
2220 	for (i = 0; i < n_rx_thread; i++) {
2221 		rx_thread[i].conf.thread_id = i;
2222 		lthread_create(&lt[n_thread], -1, lthread_rx,
2223 				(void *)&rx_thread[i]);
2224 		n_thread++;
2225 	}
2226 
2227 	/*
2228 	 * Wait for all producers. Until some producers can be started on the same
2229 	 * scheduler as this lthread, yielding is required to let them to run and
2230 	 * prevent deadlock here.
2231 	 */
2232 	while (rte_atomic16_read(&rx_counter) < n_rx_thread)
2233 		lthread_sleep(100000);
2234 
2235 	/*
2236 	 * Create consumers (tx threads) on default lcore_id
2237 	 */
2238 	for (i = 0; i < n_tx_thread; i++) {
2239 		tx_thread[i].conf.thread_id = i;
2240 		lthread_create(&lt[n_thread], -1, lthread_tx,
2241 				(void *)&tx_thread[i]);
2242 		n_thread++;
2243 	}
2244 
2245 	/*
2246 	 * Wait for all threads finished
2247 	 */
2248 	for (i = 0; i < n_thread; i++)
2249 		lthread_join(lt[i], NULL);
2250 
2251 	return NULL;
2252 }
2253 
2254 /*
2255  * Start master scheduler with initial lthread spawning rx and tx lthreads
2256  * (main_lthread_master).
2257  */
2258 static int
2259 lthread_master_spawner(__rte_unused void *arg) {
2260 	struct lthread *lt;
2261 	int lcore_id = rte_lcore_id();
2262 
2263 	RTE_PER_LCORE(lcore_conf) = &lcore_conf[lcore_id];
2264 	lthread_create(&lt, -1, lthread_spawner, NULL);
2265 	lthread_run();
2266 
2267 	return 0;
2268 }
2269 
2270 /*
2271  * Start scheduler on lcore.
2272  */
2273 static int
2274 sched_spawner(__rte_unused void *arg) {
2275 	struct lthread *lt;
2276 	int lcore_id = rte_lcore_id();
2277 
2278 #if (APP_CPU_LOAD)
2279 	if (lcore_id == cpu_load_lcore_id) {
2280 		cpu_load_collector(arg);
2281 		return 0;
2282 	}
2283 #endif /* APP_CPU_LOAD */
2284 
2285 	RTE_PER_LCORE(lcore_conf) = &lcore_conf[lcore_id];
2286 	lthread_create(&lt, -1, lthread_null, NULL);
2287 	lthread_run();
2288 
2289 	return 0;
2290 }
2291 
2292 /* main processing loop */
2293 static int
2294 pthread_tx(void *dummy)
2295 {
2296 	struct rte_mbuf *pkts_burst[MAX_PKT_BURST];
2297 	uint64_t prev_tsc, diff_tsc, cur_tsc;
2298 	int nb_rx;
2299 	uint16_t portid;
2300 	struct thread_tx_conf *tx_conf;
2301 
2302 	const uint64_t drain_tsc = (rte_get_tsc_hz() + US_PER_S - 1) /
2303 		US_PER_S * BURST_TX_DRAIN_US;
2304 
2305 	prev_tsc = 0;
2306 
2307 	tx_conf = (struct thread_tx_conf *)dummy;
2308 
2309 	RTE_LOG(INFO, L3FWD, "Entering main Tx loop on lcore %u\n", rte_lcore_id());
2310 
2311 	tx_conf->conf.cpu_id = sched_getcpu();
2312 	rte_atomic16_inc(&tx_counter);
2313 	while (1) {
2314 
2315 		cur_tsc = rte_rdtsc();
2316 
2317 		/*
2318 		 * TX burst queue drain
2319 		 */
2320 		diff_tsc = cur_tsc - prev_tsc;
2321 		if (unlikely(diff_tsc > drain_tsc)) {
2322 
2323 			/*
2324 			 * This could be optimized (use queueid instead of
2325 			 * portid), but it is not called so often
2326 			 */
2327 			SET_CPU_BUSY(tx_conf, CPU_PROCESS);
2328 			for (portid = 0; portid < RTE_MAX_ETHPORTS; portid++) {
2329 				if (tx_conf->tx_mbufs[portid].len == 0)
2330 					continue;
2331 				send_burst(tx_conf, tx_conf->tx_mbufs[portid].len, portid);
2332 				tx_conf->tx_mbufs[portid].len = 0;
2333 			}
2334 			SET_CPU_IDLE(tx_conf, CPU_PROCESS);
2335 
2336 			prev_tsc = cur_tsc;
2337 		}
2338 
2339 		/*
2340 		 * Read packet from ring
2341 		 */
2342 		SET_CPU_BUSY(tx_conf, CPU_POLL);
2343 		nb_rx = rte_ring_sc_dequeue_burst(tx_conf->ring,
2344 				(void **)pkts_burst, MAX_PKT_BURST, NULL);
2345 		SET_CPU_IDLE(tx_conf, CPU_POLL);
2346 
2347 		if (unlikely(nb_rx == 0)) {
2348 			sched_yield();
2349 			continue;
2350 		}
2351 
2352 		SET_CPU_BUSY(tx_conf, CPU_PROCESS);
2353 		portid = pkts_burst[0]->port;
2354 		process_burst(pkts_burst, nb_rx, portid);
2355 		SET_CPU_IDLE(tx_conf, CPU_PROCESS);
2356 
2357 	}
2358 }
2359 
2360 static int
2361 pthread_rx(void *dummy)
2362 {
2363 	int i;
2364 	int worker_id;
2365 	uint32_t n;
2366 	uint32_t nb_rx;
2367 	unsigned lcore_id;
2368 	uint8_t queueid;
2369 	uint16_t portid;
2370 	struct rte_mbuf *pkts_burst[MAX_PKT_BURST];
2371 
2372 	struct thread_rx_conf *rx_conf;
2373 
2374 	lcore_id = rte_lcore_id();
2375 	rx_conf = (struct thread_rx_conf *)dummy;
2376 
2377 	if (rx_conf->n_rx_queue == 0) {
2378 		RTE_LOG(INFO, L3FWD, "lcore %u has nothing to do\n", lcore_id);
2379 		return 0;
2380 	}
2381 
2382 	RTE_LOG(INFO, L3FWD, "entering main rx loop on lcore %u\n", lcore_id);
2383 
2384 	for (i = 0; i < rx_conf->n_rx_queue; i++) {
2385 
2386 		portid = rx_conf->rx_queue_list[i].port_id;
2387 		queueid = rx_conf->rx_queue_list[i].queue_id;
2388 		RTE_LOG(INFO, L3FWD,
2389 			" -- lcoreid=%u portid=%u rxqueueid=%hhu\n",
2390 				lcore_id, portid, queueid);
2391 	}
2392 
2393 	worker_id = 0;
2394 	rx_conf->conf.cpu_id = sched_getcpu();
2395 	rte_atomic16_inc(&rx_counter);
2396 	while (1) {
2397 
2398 		/*
2399 		 * Read packet from RX queues
2400 		 */
2401 		for (i = 0; i < rx_conf->n_rx_queue; ++i) {
2402 			portid = rx_conf->rx_queue_list[i].port_id;
2403 			queueid = rx_conf->rx_queue_list[i].queue_id;
2404 
2405 			SET_CPU_BUSY(rx_conf, CPU_POLL);
2406 			nb_rx = rte_eth_rx_burst(portid, queueid, pkts_burst,
2407 				MAX_PKT_BURST);
2408 			SET_CPU_IDLE(rx_conf, CPU_POLL);
2409 
2410 			if (nb_rx == 0) {
2411 				sched_yield();
2412 				continue;
2413 			}
2414 
2415 			SET_CPU_BUSY(rx_conf, CPU_PROCESS);
2416 			worker_id = (worker_id + 1) % rx_conf->n_ring;
2417 			n = rte_ring_sp_enqueue_burst(rx_conf->ring[worker_id],
2418 					(void **)pkts_burst, nb_rx, NULL);
2419 
2420 			if (unlikely(n != nb_rx)) {
2421 				uint32_t k;
2422 
2423 				for (k = n; k < nb_rx; k++) {
2424 					struct rte_mbuf *m = pkts_burst[k];
2425 
2426 					rte_pktmbuf_free(m);
2427 				}
2428 			}
2429 
2430 			SET_CPU_IDLE(rx_conf, CPU_PROCESS);
2431 
2432 		}
2433 	}
2434 }
2435 
2436 /*
2437  * P-Thread spawner.
2438  */
2439 static int
2440 pthread_run(__rte_unused void *arg) {
2441 	int lcore_id = rte_lcore_id();
2442 	int i;
2443 
2444 	for (i = 0; i < n_rx_thread; i++)
2445 		if (rx_thread[i].conf.lcore_id == lcore_id) {
2446 			printf("Start rx thread on %d...\n", lcore_id);
2447 			RTE_PER_LCORE(lcore_conf) = &lcore_conf[lcore_id];
2448 			RTE_PER_LCORE(lcore_conf)->data = (void *)&rx_thread[i];
2449 			pthread_rx((void *)&rx_thread[i]);
2450 			return 0;
2451 		}
2452 
2453 	for (i = 0; i < n_tx_thread; i++)
2454 		if (tx_thread[i].conf.lcore_id == lcore_id) {
2455 			printf("Start tx thread on %d...\n", lcore_id);
2456 			RTE_PER_LCORE(lcore_conf) = &lcore_conf[lcore_id];
2457 			RTE_PER_LCORE(lcore_conf)->data = (void *)&tx_thread[i];
2458 			pthread_tx((void *)&tx_thread[i]);
2459 			return 0;
2460 		}
2461 
2462 #if (APP_CPU_LOAD)
2463 	if (lcore_id == cpu_load_lcore_id)
2464 		cpu_load_collector(arg);
2465 #endif /* APP_CPU_LOAD */
2466 
2467 	return 0;
2468 }
2469 
2470 static int
2471 check_lcore_params(void)
2472 {
2473 	uint8_t queue, lcore;
2474 	uint16_t i;
2475 	int socketid;
2476 
2477 	for (i = 0; i < nb_rx_thread_params; ++i) {
2478 		queue = rx_thread_params[i].queue_id;
2479 		if (queue >= MAX_RX_QUEUE_PER_PORT) {
2480 			printf("invalid queue number: %hhu\n", queue);
2481 			return -1;
2482 		}
2483 		lcore = rx_thread_params[i].lcore_id;
2484 		if (!rte_lcore_is_enabled(lcore)) {
2485 			printf("error: lcore %hhu is not enabled in lcore mask\n", lcore);
2486 			return -1;
2487 		}
2488 		socketid = rte_lcore_to_socket_id(lcore);
2489 		if ((socketid != 0) && (numa_on == 0))
2490 			printf("warning: lcore %hhu is on socket %d with numa off\n",
2491 				lcore, socketid);
2492 	}
2493 	return 0;
2494 }
2495 
2496 static int
2497 check_port_config(void)
2498 {
2499 	unsigned portid;
2500 	uint16_t i;
2501 
2502 	for (i = 0; i < nb_rx_thread_params; ++i) {
2503 		portid = rx_thread_params[i].port_id;
2504 		if ((enabled_port_mask & (1 << portid)) == 0) {
2505 			printf("port %u is not enabled in port mask\n", portid);
2506 			return -1;
2507 		}
2508 		if (!rte_eth_dev_is_valid_port(portid)) {
2509 			printf("port %u is not present on the board\n", portid);
2510 			return -1;
2511 		}
2512 	}
2513 	return 0;
2514 }
2515 
2516 static uint8_t
2517 get_port_n_rx_queues(const uint16_t port)
2518 {
2519 	int queue = -1;
2520 	uint16_t i;
2521 
2522 	for (i = 0; i < nb_rx_thread_params; ++i)
2523 		if (rx_thread_params[i].port_id == port &&
2524 				rx_thread_params[i].queue_id > queue)
2525 			queue = rx_thread_params[i].queue_id;
2526 
2527 	return (uint8_t)(++queue);
2528 }
2529 
2530 static int
2531 init_rx_rings(void)
2532 {
2533 	unsigned socket_io;
2534 	struct thread_rx_conf *rx_conf;
2535 	struct thread_tx_conf *tx_conf;
2536 	unsigned rx_thread_id, tx_thread_id;
2537 	char name[256];
2538 	struct rte_ring *ring = NULL;
2539 
2540 	for (tx_thread_id = 0; tx_thread_id < n_tx_thread; tx_thread_id++) {
2541 
2542 		tx_conf = &tx_thread[tx_thread_id];
2543 
2544 		printf("Connecting tx-thread %d with rx-thread %d\n", tx_thread_id,
2545 				tx_conf->conf.thread_id);
2546 
2547 		rx_thread_id = tx_conf->conf.thread_id;
2548 		if (rx_thread_id > n_tx_thread) {
2549 			printf("connection from tx-thread %u to rx-thread %u fails "
2550 					"(rx-thread not defined)\n", tx_thread_id, rx_thread_id);
2551 			return -1;
2552 		}
2553 
2554 		rx_conf = &rx_thread[rx_thread_id];
2555 		socket_io = rte_lcore_to_socket_id(rx_conf->conf.lcore_id);
2556 
2557 		snprintf(name, sizeof(name), "app_ring_s%u_rx%u_tx%u",
2558 				socket_io, rx_thread_id, tx_thread_id);
2559 
2560 		ring = rte_ring_create(name, 1024 * 4, socket_io,
2561 				RING_F_SP_ENQ | RING_F_SC_DEQ);
2562 
2563 		if (ring == NULL) {
2564 			rte_panic("Cannot create ring to connect rx-thread %u "
2565 					"with tx-thread %u\n", rx_thread_id, tx_thread_id);
2566 		}
2567 
2568 		rx_conf->ring[rx_conf->n_ring] = ring;
2569 
2570 		tx_conf->ring = ring;
2571 		tx_conf->ready = &rx_conf->ready[rx_conf->n_ring];
2572 
2573 		rx_conf->n_ring++;
2574 	}
2575 	return 0;
2576 }
2577 
2578 static int
2579 init_rx_queues(void)
2580 {
2581 	uint16_t i, nb_rx_queue;
2582 	uint8_t thread;
2583 
2584 	n_rx_thread = 0;
2585 
2586 	for (i = 0; i < nb_rx_thread_params; ++i) {
2587 		thread = rx_thread_params[i].thread_id;
2588 		nb_rx_queue = rx_thread[thread].n_rx_queue;
2589 
2590 		if (nb_rx_queue >= MAX_RX_QUEUE_PER_LCORE) {
2591 			printf("error: too many queues (%u) for thread: %u\n",
2592 				(unsigned)nb_rx_queue + 1, (unsigned)thread);
2593 			return -1;
2594 		}
2595 
2596 		rx_thread[thread].conf.thread_id = thread;
2597 		rx_thread[thread].conf.lcore_id = rx_thread_params[i].lcore_id;
2598 		rx_thread[thread].rx_queue_list[nb_rx_queue].port_id =
2599 			rx_thread_params[i].port_id;
2600 		rx_thread[thread].rx_queue_list[nb_rx_queue].queue_id =
2601 			rx_thread_params[i].queue_id;
2602 		rx_thread[thread].n_rx_queue++;
2603 
2604 		if (thread >= n_rx_thread)
2605 			n_rx_thread = thread + 1;
2606 
2607 	}
2608 	return 0;
2609 }
2610 
2611 static int
2612 init_tx_threads(void)
2613 {
2614 	int i;
2615 
2616 	n_tx_thread = 0;
2617 	for (i = 0; i < nb_tx_thread_params; ++i) {
2618 		tx_thread[n_tx_thread].conf.thread_id = tx_thread_params[i].thread_id;
2619 		tx_thread[n_tx_thread].conf.lcore_id = tx_thread_params[i].lcore_id;
2620 		n_tx_thread++;
2621 	}
2622 	return 0;
2623 }
2624 
2625 /* display usage */
2626 static void
2627 print_usage(const char *prgname)
2628 {
2629 	printf("%s [EAL options] -- -p PORTMASK -P"
2630 		"  [--rx (port,queue,lcore,thread)[,(port,queue,lcore,thread]]"
2631 		"  [--tx (lcore,thread)[,(lcore,thread]]"
2632 		"  [--enable-jumbo [--max-pkt-len PKTLEN]]\n"
2633 		"  [--parse-ptype]\n\n"
2634 		"  -p PORTMASK: hexadecimal bitmask of ports to configure\n"
2635 		"  -P : enable promiscuous mode\n"
2636 		"  --rx (port,queue,lcore,thread): rx queues configuration\n"
2637 		"  --tx (lcore,thread): tx threads configuration\n"
2638 		"  --stat-lcore LCORE: use lcore for stat collector\n"
2639 		"  --eth-dest=X,MM:MM:MM:MM:MM:MM: optional, ethernet destination for port X\n"
2640 		"  --no-numa: optional, disable numa awareness\n"
2641 		"  --ipv6: optional, specify it if running ipv6 packets\n"
2642 		"  --enable-jumbo: enable jumbo frame"
2643 		" which max packet len is PKTLEN in decimal (64-9600)\n"
2644 		"  --hash-entry-num: specify the hash entry number in hexadecimal to be setup\n"
2645 		"  --no-lthreads: turn off lthread model\n"
2646 		"  --parse-ptype: set to use software to analyze packet type\n\n",
2647 		prgname);
2648 }
2649 
2650 static int parse_max_pkt_len(const char *pktlen)
2651 {
2652 	char *end = NULL;
2653 	unsigned long len;
2654 
2655 	/* parse decimal string */
2656 	len = strtoul(pktlen, &end, 10);
2657 	if ((pktlen[0] == '\0') || (end == NULL) || (*end != '\0'))
2658 		return -1;
2659 
2660 	if (len == 0)
2661 		return -1;
2662 
2663 	return len;
2664 }
2665 
2666 static int
2667 parse_portmask(const char *portmask)
2668 {
2669 	char *end = NULL;
2670 	unsigned long pm;
2671 
2672 	/* parse hexadecimal string */
2673 	pm = strtoul(portmask, &end, 16);
2674 	if ((portmask[0] == '\0') || (end == NULL) || (*end != '\0'))
2675 		return -1;
2676 
2677 	if (pm == 0)
2678 		return -1;
2679 
2680 	return pm;
2681 }
2682 
2683 #if (APP_LOOKUP_METHOD == APP_LOOKUP_EXACT_MATCH)
2684 static int
2685 parse_hash_entry_number(const char *hash_entry_num)
2686 {
2687 	char *end = NULL;
2688 	unsigned long hash_en;
2689 
2690 	/* parse hexadecimal string */
2691 	hash_en = strtoul(hash_entry_num, &end, 16);
2692 	if ((hash_entry_num[0] == '\0') || (end == NULL) || (*end != '\0'))
2693 		return -1;
2694 
2695 	if (hash_en == 0)
2696 		return -1;
2697 
2698 	return hash_en;
2699 }
2700 #endif
2701 
2702 static int
2703 parse_rx_config(const char *q_arg)
2704 {
2705 	char s[256];
2706 	const char *p, *p0 = q_arg;
2707 	char *end;
2708 	enum fieldnames {
2709 		FLD_PORT = 0,
2710 		FLD_QUEUE,
2711 		FLD_LCORE,
2712 		FLD_THREAD,
2713 		_NUM_FLD
2714 	};
2715 	unsigned long int_fld[_NUM_FLD];
2716 	char *str_fld[_NUM_FLD];
2717 	int i;
2718 	unsigned size;
2719 
2720 	nb_rx_thread_params = 0;
2721 
2722 	while ((p = strchr(p0, '(')) != NULL) {
2723 		++p;
2724 		p0 = strchr(p, ')');
2725 		if (p0 == NULL)
2726 			return -1;
2727 
2728 		size = p0 - p;
2729 		if (size >= sizeof(s))
2730 			return -1;
2731 
2732 		snprintf(s, sizeof(s), "%.*s", size, p);
2733 		if (rte_strsplit(s, sizeof(s), str_fld, _NUM_FLD, ',') != _NUM_FLD)
2734 			return -1;
2735 		for (i = 0; i < _NUM_FLD; i++) {
2736 			errno = 0;
2737 			int_fld[i] = strtoul(str_fld[i], &end, 0);
2738 			if (errno != 0 || end == str_fld[i] || int_fld[i] > 255)
2739 				return -1;
2740 		}
2741 		if (nb_rx_thread_params >= MAX_LCORE_PARAMS) {
2742 			printf("exceeded max number of rx params: %hu\n",
2743 					nb_rx_thread_params);
2744 			return -1;
2745 		}
2746 		rx_thread_params_array[nb_rx_thread_params].port_id =
2747 				int_fld[FLD_PORT];
2748 		rx_thread_params_array[nb_rx_thread_params].queue_id =
2749 				(uint8_t)int_fld[FLD_QUEUE];
2750 		rx_thread_params_array[nb_rx_thread_params].lcore_id =
2751 				(uint8_t)int_fld[FLD_LCORE];
2752 		rx_thread_params_array[nb_rx_thread_params].thread_id =
2753 				(uint8_t)int_fld[FLD_THREAD];
2754 		++nb_rx_thread_params;
2755 	}
2756 	rx_thread_params = rx_thread_params_array;
2757 	return 0;
2758 }
2759 
2760 static int
2761 parse_tx_config(const char *q_arg)
2762 {
2763 	char s[256];
2764 	const char *p, *p0 = q_arg;
2765 	char *end;
2766 	enum fieldnames {
2767 		FLD_LCORE = 0,
2768 		FLD_THREAD,
2769 		_NUM_FLD
2770 	};
2771 	unsigned long int_fld[_NUM_FLD];
2772 	char *str_fld[_NUM_FLD];
2773 	int i;
2774 	unsigned size;
2775 
2776 	nb_tx_thread_params = 0;
2777 
2778 	while ((p = strchr(p0, '(')) != NULL) {
2779 		++p;
2780 		p0 = strchr(p, ')');
2781 		if (p0 == NULL)
2782 			return -1;
2783 
2784 		size = p0 - p;
2785 		if (size >= sizeof(s))
2786 			return -1;
2787 
2788 		snprintf(s, sizeof(s), "%.*s", size, p);
2789 		if (rte_strsplit(s, sizeof(s), str_fld, _NUM_FLD, ',') != _NUM_FLD)
2790 			return -1;
2791 		for (i = 0; i < _NUM_FLD; i++) {
2792 			errno = 0;
2793 			int_fld[i] = strtoul(str_fld[i], &end, 0);
2794 			if (errno != 0 || end == str_fld[i] || int_fld[i] > 255)
2795 				return -1;
2796 		}
2797 		if (nb_tx_thread_params >= MAX_LCORE_PARAMS) {
2798 			printf("exceeded max number of tx params: %hu\n",
2799 				nb_tx_thread_params);
2800 			return -1;
2801 		}
2802 		tx_thread_params_array[nb_tx_thread_params].lcore_id =
2803 				(uint8_t)int_fld[FLD_LCORE];
2804 		tx_thread_params_array[nb_tx_thread_params].thread_id =
2805 				(uint8_t)int_fld[FLD_THREAD];
2806 		++nb_tx_thread_params;
2807 	}
2808 	tx_thread_params = tx_thread_params_array;
2809 
2810 	return 0;
2811 }
2812 
2813 #if (APP_CPU_LOAD > 0)
2814 static int
2815 parse_stat_lcore(const char *stat_lcore)
2816 {
2817 	char *end = NULL;
2818 	unsigned long lcore_id;
2819 
2820 	lcore_id = strtoul(stat_lcore, &end, 10);
2821 	if ((stat_lcore[0] == '\0') || (end == NULL) || (*end != '\0'))
2822 		return -1;
2823 
2824 	return lcore_id;
2825 }
2826 #endif
2827 
2828 static void
2829 parse_eth_dest(const char *optarg)
2830 {
2831 	uint16_t portid;
2832 	char *port_end;
2833 	uint8_t c, *dest, peer_addr[6];
2834 
2835 	errno = 0;
2836 	portid = strtoul(optarg, &port_end, 10);
2837 	if (errno != 0 || port_end == optarg || *port_end++ != ',')
2838 		rte_exit(EXIT_FAILURE,
2839 		"Invalid eth-dest: %s", optarg);
2840 	if (portid >= RTE_MAX_ETHPORTS)
2841 		rte_exit(EXIT_FAILURE,
2842 		"eth-dest: port %d >= RTE_MAX_ETHPORTS(%d)\n",
2843 		portid, RTE_MAX_ETHPORTS);
2844 
2845 	if (cmdline_parse_etheraddr(NULL, port_end,
2846 		&peer_addr, sizeof(peer_addr)) < 0)
2847 		rte_exit(EXIT_FAILURE,
2848 		"Invalid ethernet address: %s\n",
2849 		port_end);
2850 	dest = (uint8_t *)&dest_eth_addr[portid];
2851 	for (c = 0; c < 6; c++)
2852 		dest[c] = peer_addr[c];
2853 	*(uint64_t *)(val_eth + portid) = dest_eth_addr[portid];
2854 }
2855 
2856 #define CMD_LINE_OPT_RX_CONFIG "rx"
2857 #define CMD_LINE_OPT_TX_CONFIG "tx"
2858 #define CMD_LINE_OPT_STAT_LCORE "stat-lcore"
2859 #define CMD_LINE_OPT_ETH_DEST "eth-dest"
2860 #define CMD_LINE_OPT_NO_NUMA "no-numa"
2861 #define CMD_LINE_OPT_IPV6 "ipv6"
2862 #define CMD_LINE_OPT_ENABLE_JUMBO "enable-jumbo"
2863 #define CMD_LINE_OPT_HASH_ENTRY_NUM "hash-entry-num"
2864 #define CMD_LINE_OPT_NO_LTHREADS "no-lthreads"
2865 #define CMD_LINE_OPT_PARSE_PTYPE "parse-ptype"
2866 
2867 /* Parse the argument given in the command line of the application */
2868 static int
2869 parse_args(int argc, char **argv)
2870 {
2871 	int opt, ret;
2872 	char **argvopt;
2873 	int option_index;
2874 	char *prgname = argv[0];
2875 	static struct option lgopts[] = {
2876 		{CMD_LINE_OPT_RX_CONFIG, 1, 0, 0},
2877 		{CMD_LINE_OPT_TX_CONFIG, 1, 0, 0},
2878 		{CMD_LINE_OPT_STAT_LCORE, 1, 0, 0},
2879 		{CMD_LINE_OPT_ETH_DEST, 1, 0, 0},
2880 		{CMD_LINE_OPT_NO_NUMA, 0, 0, 0},
2881 		{CMD_LINE_OPT_IPV6, 0, 0, 0},
2882 		{CMD_LINE_OPT_ENABLE_JUMBO, 0, 0, 0},
2883 		{CMD_LINE_OPT_HASH_ENTRY_NUM, 1, 0, 0},
2884 		{CMD_LINE_OPT_NO_LTHREADS, 0, 0, 0},
2885 		{CMD_LINE_OPT_PARSE_PTYPE, 0, 0, 0},
2886 		{NULL, 0, 0, 0}
2887 	};
2888 
2889 	argvopt = argv;
2890 
2891 	while ((opt = getopt_long(argc, argvopt, "p:P",
2892 				lgopts, &option_index)) != EOF) {
2893 
2894 		switch (opt) {
2895 		/* portmask */
2896 		case 'p':
2897 			enabled_port_mask = parse_portmask(optarg);
2898 			if (enabled_port_mask == 0) {
2899 				printf("invalid portmask\n");
2900 				print_usage(prgname);
2901 				return -1;
2902 			}
2903 			break;
2904 		case 'P':
2905 			printf("Promiscuous mode selected\n");
2906 			promiscuous_on = 1;
2907 			break;
2908 
2909 		/* long options */
2910 		case 0:
2911 			if (!strncmp(lgopts[option_index].name, CMD_LINE_OPT_RX_CONFIG,
2912 				sizeof(CMD_LINE_OPT_RX_CONFIG))) {
2913 				ret = parse_rx_config(optarg);
2914 				if (ret) {
2915 					printf("invalid rx-config\n");
2916 					print_usage(prgname);
2917 					return -1;
2918 				}
2919 			}
2920 
2921 			if (!strncmp(lgopts[option_index].name, CMD_LINE_OPT_TX_CONFIG,
2922 				sizeof(CMD_LINE_OPT_TX_CONFIG))) {
2923 				ret = parse_tx_config(optarg);
2924 				if (ret) {
2925 					printf("invalid tx-config\n");
2926 					print_usage(prgname);
2927 					return -1;
2928 				}
2929 			}
2930 
2931 #if (APP_CPU_LOAD > 0)
2932 			if (!strncmp(lgopts[option_index].name, CMD_LINE_OPT_STAT_LCORE,
2933 					sizeof(CMD_LINE_OPT_STAT_LCORE))) {
2934 				cpu_load_lcore_id = parse_stat_lcore(optarg);
2935 			}
2936 #endif
2937 
2938 			if (!strncmp(lgopts[option_index].name, CMD_LINE_OPT_ETH_DEST,
2939 				sizeof(CMD_LINE_OPT_ETH_DEST)))
2940 					parse_eth_dest(optarg);
2941 
2942 			if (!strncmp(lgopts[option_index].name, CMD_LINE_OPT_NO_NUMA,
2943 				sizeof(CMD_LINE_OPT_NO_NUMA))) {
2944 				printf("numa is disabled\n");
2945 				numa_on = 0;
2946 			}
2947 
2948 #if (APP_LOOKUP_METHOD == APP_LOOKUP_EXACT_MATCH)
2949 			if (!strncmp(lgopts[option_index].name, CMD_LINE_OPT_IPV6,
2950 				sizeof(CMD_LINE_OPT_IPV6))) {
2951 				printf("ipv6 is specified\n");
2952 				ipv6 = 1;
2953 			}
2954 #endif
2955 
2956 			if (!strncmp(lgopts[option_index].name, CMD_LINE_OPT_NO_LTHREADS,
2957 					sizeof(CMD_LINE_OPT_NO_LTHREADS))) {
2958 				printf("l-threads model is disabled\n");
2959 				lthreads_on = 0;
2960 			}
2961 
2962 			if (!strncmp(lgopts[option_index].name, CMD_LINE_OPT_PARSE_PTYPE,
2963 					sizeof(CMD_LINE_OPT_PARSE_PTYPE))) {
2964 				printf("software packet type parsing enabled\n");
2965 				parse_ptype_on = 1;
2966 			}
2967 
2968 			if (!strncmp(lgopts[option_index].name, CMD_LINE_OPT_ENABLE_JUMBO,
2969 				sizeof(CMD_LINE_OPT_ENABLE_JUMBO))) {
2970 				struct option lenopts = {"max-pkt-len", required_argument, 0,
2971 						0};
2972 
2973 				printf("jumbo frame is enabled - disabling simple TX path\n");
2974 				port_conf.rxmode.offloads |=
2975 						DEV_RX_OFFLOAD_JUMBO_FRAME;
2976 				port_conf.txmode.offloads |=
2977 						DEV_TX_OFFLOAD_MULTI_SEGS;
2978 
2979 				/* if no max-pkt-len set, use the default value ETHER_MAX_LEN */
2980 				if (0 == getopt_long(argc, argvopt, "", &lenopts,
2981 						&option_index)) {
2982 
2983 					ret = parse_max_pkt_len(optarg);
2984 					if ((ret < 64) || (ret > MAX_JUMBO_PKT_LEN)) {
2985 						printf("invalid packet length\n");
2986 						print_usage(prgname);
2987 						return -1;
2988 					}
2989 					port_conf.rxmode.max_rx_pkt_len = ret;
2990 				}
2991 				printf("set jumbo frame max packet length to %u\n",
2992 						(unsigned int)port_conf.rxmode.max_rx_pkt_len);
2993 			}
2994 #if (APP_LOOKUP_METHOD == APP_LOOKUP_EXACT_MATCH)
2995 			if (!strncmp(lgopts[option_index].name, CMD_LINE_OPT_HASH_ENTRY_NUM,
2996 				sizeof(CMD_LINE_OPT_HASH_ENTRY_NUM))) {
2997 				ret = parse_hash_entry_number(optarg);
2998 				if ((ret > 0) && (ret <= L3FWD_HASH_ENTRIES)) {
2999 					hash_entry_number = ret;
3000 				} else {
3001 					printf("invalid hash entry number\n");
3002 					print_usage(prgname);
3003 					return -1;
3004 				}
3005 			}
3006 #endif
3007 			break;
3008 
3009 		default:
3010 			print_usage(prgname);
3011 			return -1;
3012 		}
3013 	}
3014 
3015 	if (optind >= 0)
3016 		argv[optind-1] = prgname;
3017 
3018 	ret = optind-1;
3019 	optind = 1; /* reset getopt lib */
3020 	return ret;
3021 }
3022 
3023 static void
3024 print_ethaddr(const char *name, const struct ether_addr *eth_addr)
3025 {
3026 	char buf[ETHER_ADDR_FMT_SIZE];
3027 
3028 	ether_format_addr(buf, ETHER_ADDR_FMT_SIZE, eth_addr);
3029 	printf("%s%s", name, buf);
3030 }
3031 
3032 #if (APP_LOOKUP_METHOD == APP_LOOKUP_EXACT_MATCH)
3033 
3034 static void convert_ipv4_5tuple(struct ipv4_5tuple *key1,
3035 		union ipv4_5tuple_host *key2)
3036 {
3037 	key2->ip_dst = rte_cpu_to_be_32(key1->ip_dst);
3038 	key2->ip_src = rte_cpu_to_be_32(key1->ip_src);
3039 	key2->port_dst = rte_cpu_to_be_16(key1->port_dst);
3040 	key2->port_src = rte_cpu_to_be_16(key1->port_src);
3041 	key2->proto = key1->proto;
3042 	key2->pad0 = 0;
3043 	key2->pad1 = 0;
3044 }
3045 
3046 static void convert_ipv6_5tuple(struct ipv6_5tuple *key1,
3047 		union ipv6_5tuple_host *key2)
3048 {
3049 	uint32_t i;
3050 
3051 	for (i = 0; i < 16; i++) {
3052 		key2->ip_dst[i] = key1->ip_dst[i];
3053 		key2->ip_src[i] = key1->ip_src[i];
3054 	}
3055 	key2->port_dst = rte_cpu_to_be_16(key1->port_dst);
3056 	key2->port_src = rte_cpu_to_be_16(key1->port_src);
3057 	key2->proto = key1->proto;
3058 	key2->pad0 = 0;
3059 	key2->pad1 = 0;
3060 	key2->reserve = 0;
3061 }
3062 
3063 #define BYTE_VALUE_MAX 256
3064 #define ALL_32_BITS 0xffffffff
3065 #define BIT_8_TO_15 0x0000ff00
3066 static inline void
3067 populate_ipv4_few_flow_into_table(const struct rte_hash *h)
3068 {
3069 	uint32_t i;
3070 	int32_t ret;
3071 	uint32_t array_len = RTE_DIM(ipv4_l3fwd_route_array);
3072 
3073 	mask0 = _mm_set_epi32(ALL_32_BITS, ALL_32_BITS, ALL_32_BITS, BIT_8_TO_15);
3074 	for (i = 0; i < array_len; i++) {
3075 		struct ipv4_l3fwd_route  entry;
3076 		union ipv4_5tuple_host newkey;
3077 
3078 		entry = ipv4_l3fwd_route_array[i];
3079 		convert_ipv4_5tuple(&entry.key, &newkey);
3080 		ret = rte_hash_add_key(h, (void *)&newkey);
3081 		if (ret < 0) {
3082 			rte_exit(EXIT_FAILURE, "Unable to add entry %" PRIu32
3083 				" to the l3fwd hash.\n", i);
3084 		}
3085 		ipv4_l3fwd_out_if[ret] = entry.if_out;
3086 	}
3087 	printf("Hash: Adding 0x%" PRIx32 " keys\n", array_len);
3088 }
3089 
3090 #define BIT_16_TO_23 0x00ff0000
3091 static inline void
3092 populate_ipv6_few_flow_into_table(const struct rte_hash *h)
3093 {
3094 	uint32_t i;
3095 	int32_t ret;
3096 	uint32_t array_len = RTE_DIM(ipv6_l3fwd_route_array);
3097 
3098 	mask1 = _mm_set_epi32(ALL_32_BITS, ALL_32_BITS, ALL_32_BITS, BIT_16_TO_23);
3099 	mask2 = _mm_set_epi32(0, 0, ALL_32_BITS, ALL_32_BITS);
3100 	for (i = 0; i < array_len; i++) {
3101 		struct ipv6_l3fwd_route entry;
3102 		union ipv6_5tuple_host newkey;
3103 
3104 		entry = ipv6_l3fwd_route_array[i];
3105 		convert_ipv6_5tuple(&entry.key, &newkey);
3106 		ret = rte_hash_add_key(h, (void *)&newkey);
3107 		if (ret < 0) {
3108 			rte_exit(EXIT_FAILURE, "Unable to add entry %" PRIu32
3109 				" to the l3fwd hash.\n", i);
3110 		}
3111 		ipv6_l3fwd_out_if[ret] = entry.if_out;
3112 	}
3113 	printf("Hash: Adding 0x%" PRIx32 "keys\n", array_len);
3114 }
3115 
3116 #define NUMBER_PORT_USED 4
3117 static inline void
3118 populate_ipv4_many_flow_into_table(const struct rte_hash *h,
3119 		unsigned int nr_flow)
3120 {
3121 	unsigned i;
3122 
3123 	mask0 = _mm_set_epi32(ALL_32_BITS, ALL_32_BITS, ALL_32_BITS, BIT_8_TO_15);
3124 
3125 	for (i = 0; i < nr_flow; i++) {
3126 		struct ipv4_l3fwd_route entry;
3127 		union ipv4_5tuple_host newkey;
3128 		uint8_t a = (uint8_t)((i / NUMBER_PORT_USED) % BYTE_VALUE_MAX);
3129 		uint8_t b = (uint8_t)(((i / NUMBER_PORT_USED) / BYTE_VALUE_MAX) %
3130 				BYTE_VALUE_MAX);
3131 		uint8_t c = (uint8_t)((i / NUMBER_PORT_USED) / (BYTE_VALUE_MAX *
3132 				BYTE_VALUE_MAX));
3133 		/* Create the ipv4 exact match flow */
3134 		memset(&entry, 0, sizeof(entry));
3135 		switch (i & (NUMBER_PORT_USED - 1)) {
3136 		case 0:
3137 			entry = ipv4_l3fwd_route_array[0];
3138 			entry.key.ip_dst = IPv4(101, c, b, a);
3139 			break;
3140 		case 1:
3141 			entry = ipv4_l3fwd_route_array[1];
3142 			entry.key.ip_dst = IPv4(201, c, b, a);
3143 			break;
3144 		case 2:
3145 			entry = ipv4_l3fwd_route_array[2];
3146 			entry.key.ip_dst = IPv4(111, c, b, a);
3147 			break;
3148 		case 3:
3149 			entry = ipv4_l3fwd_route_array[3];
3150 			entry.key.ip_dst = IPv4(211, c, b, a);
3151 			break;
3152 		};
3153 		convert_ipv4_5tuple(&entry.key, &newkey);
3154 		int32_t ret = rte_hash_add_key(h, (void *)&newkey);
3155 
3156 		if (ret < 0)
3157 			rte_exit(EXIT_FAILURE, "Unable to add entry %u\n", i);
3158 
3159 		ipv4_l3fwd_out_if[ret] = (uint8_t)entry.if_out;
3160 
3161 	}
3162 	printf("Hash: Adding 0x%x keys\n", nr_flow);
3163 }
3164 
3165 static inline void
3166 populate_ipv6_many_flow_into_table(const struct rte_hash *h,
3167 		unsigned int nr_flow)
3168 {
3169 	unsigned i;
3170 
3171 	mask1 = _mm_set_epi32(ALL_32_BITS, ALL_32_BITS, ALL_32_BITS, BIT_16_TO_23);
3172 	mask2 = _mm_set_epi32(0, 0, ALL_32_BITS, ALL_32_BITS);
3173 	for (i = 0; i < nr_flow; i++) {
3174 		struct ipv6_l3fwd_route entry;
3175 		union ipv6_5tuple_host newkey;
3176 
3177 		uint8_t a = (uint8_t) ((i / NUMBER_PORT_USED) % BYTE_VALUE_MAX);
3178 		uint8_t b = (uint8_t) (((i / NUMBER_PORT_USED) / BYTE_VALUE_MAX) %
3179 				BYTE_VALUE_MAX);
3180 		uint8_t c = (uint8_t) ((i / NUMBER_PORT_USED) / (BYTE_VALUE_MAX *
3181 				BYTE_VALUE_MAX));
3182 
3183 		/* Create the ipv6 exact match flow */
3184 		memset(&entry, 0, sizeof(entry));
3185 		switch (i & (NUMBER_PORT_USED - 1)) {
3186 		case 0:
3187 			entry = ipv6_l3fwd_route_array[0];
3188 			break;
3189 		case 1:
3190 			entry = ipv6_l3fwd_route_array[1];
3191 			break;
3192 		case 2:
3193 			entry = ipv6_l3fwd_route_array[2];
3194 			break;
3195 		case 3:
3196 			entry = ipv6_l3fwd_route_array[3];
3197 			break;
3198 		};
3199 		entry.key.ip_dst[13] = c;
3200 		entry.key.ip_dst[14] = b;
3201 		entry.key.ip_dst[15] = a;
3202 		convert_ipv6_5tuple(&entry.key, &newkey);
3203 		int32_t ret = rte_hash_add_key(h, (void *)&newkey);
3204 
3205 		if (ret < 0)
3206 			rte_exit(EXIT_FAILURE, "Unable to add entry %u\n", i);
3207 
3208 		ipv6_l3fwd_out_if[ret] = (uint8_t) entry.if_out;
3209 
3210 	}
3211 	printf("Hash: Adding 0x%x keys\n", nr_flow);
3212 }
3213 
3214 static void
3215 setup_hash(int socketid)
3216 {
3217 	struct rte_hash_parameters ipv4_l3fwd_hash_params = {
3218 		.name = NULL,
3219 		.entries = L3FWD_HASH_ENTRIES,
3220 		.key_len = sizeof(union ipv4_5tuple_host),
3221 		.hash_func = ipv4_hash_crc,
3222 		.hash_func_init_val = 0,
3223 	};
3224 
3225 	struct rte_hash_parameters ipv6_l3fwd_hash_params = {
3226 		.name = NULL,
3227 		.entries = L3FWD_HASH_ENTRIES,
3228 		.key_len = sizeof(union ipv6_5tuple_host),
3229 		.hash_func = ipv6_hash_crc,
3230 		.hash_func_init_val = 0,
3231 	};
3232 
3233 	char s[64];
3234 
3235 	/* create ipv4 hash */
3236 	snprintf(s, sizeof(s), "ipv4_l3fwd_hash_%d", socketid);
3237 	ipv4_l3fwd_hash_params.name = s;
3238 	ipv4_l3fwd_hash_params.socket_id = socketid;
3239 	ipv4_l3fwd_lookup_struct[socketid] =
3240 			rte_hash_create(&ipv4_l3fwd_hash_params);
3241 	if (ipv4_l3fwd_lookup_struct[socketid] == NULL)
3242 		rte_exit(EXIT_FAILURE, "Unable to create the l3fwd hash on "
3243 				"socket %d\n", socketid);
3244 
3245 	/* create ipv6 hash */
3246 	snprintf(s, sizeof(s), "ipv6_l3fwd_hash_%d", socketid);
3247 	ipv6_l3fwd_hash_params.name = s;
3248 	ipv6_l3fwd_hash_params.socket_id = socketid;
3249 	ipv6_l3fwd_lookup_struct[socketid] =
3250 			rte_hash_create(&ipv6_l3fwd_hash_params);
3251 	if (ipv6_l3fwd_lookup_struct[socketid] == NULL)
3252 		rte_exit(EXIT_FAILURE, "Unable to create the l3fwd hash on "
3253 				"socket %d\n", socketid);
3254 
3255 	if (hash_entry_number != HASH_ENTRY_NUMBER_DEFAULT) {
3256 		/* For testing hash matching with a large number of flows we
3257 		 * generate millions of IP 5-tuples with an incremented dst
3258 		 * address to initialize the hash table. */
3259 		if (ipv6 == 0) {
3260 			/* populate the ipv4 hash */
3261 			populate_ipv4_many_flow_into_table(
3262 				ipv4_l3fwd_lookup_struct[socketid], hash_entry_number);
3263 		} else {
3264 			/* populate the ipv6 hash */
3265 			populate_ipv6_many_flow_into_table(
3266 				ipv6_l3fwd_lookup_struct[socketid], hash_entry_number);
3267 		}
3268 	} else {
3269 		/* Use data in ipv4/ipv6 l3fwd lookup table directly to initialize
3270 		 * the hash table */
3271 		if (ipv6 == 0) {
3272 			/* populate the ipv4 hash */
3273 			populate_ipv4_few_flow_into_table(
3274 					ipv4_l3fwd_lookup_struct[socketid]);
3275 		} else {
3276 			/* populate the ipv6 hash */
3277 			populate_ipv6_few_flow_into_table(
3278 					ipv6_l3fwd_lookup_struct[socketid]);
3279 		}
3280 	}
3281 }
3282 #endif
3283 
3284 #if (APP_LOOKUP_METHOD == APP_LOOKUP_LPM)
3285 static void
3286 setup_lpm(int socketid)
3287 {
3288 	struct rte_lpm6_config config;
3289 	struct rte_lpm_config lpm_ipv4_config;
3290 	unsigned i;
3291 	int ret;
3292 	char s[64];
3293 
3294 	/* create the LPM table */
3295 	snprintf(s, sizeof(s), "IPV4_L3FWD_LPM_%d", socketid);
3296 	lpm_ipv4_config.max_rules = IPV4_L3FWD_LPM_MAX_RULES;
3297 	lpm_ipv4_config.number_tbl8s = 256;
3298 	lpm_ipv4_config.flags = 0;
3299 	ipv4_l3fwd_lookup_struct[socketid] =
3300 			rte_lpm_create(s, socketid, &lpm_ipv4_config);
3301 	if (ipv4_l3fwd_lookup_struct[socketid] == NULL)
3302 		rte_exit(EXIT_FAILURE, "Unable to create the l3fwd LPM table"
3303 				" on socket %d\n", socketid);
3304 
3305 	/* populate the LPM table */
3306 	for (i = 0; i < IPV4_L3FWD_NUM_ROUTES; i++) {
3307 
3308 		/* skip unused ports */
3309 		if ((1 << ipv4_l3fwd_route_array[i].if_out &
3310 				enabled_port_mask) == 0)
3311 			continue;
3312 
3313 		ret = rte_lpm_add(ipv4_l3fwd_lookup_struct[socketid],
3314 			ipv4_l3fwd_route_array[i].ip,
3315 			ipv4_l3fwd_route_array[i].depth,
3316 			ipv4_l3fwd_route_array[i].if_out);
3317 
3318 		if (ret < 0) {
3319 			rte_exit(EXIT_FAILURE, "Unable to add entry %u to the "
3320 				"l3fwd LPM table on socket %d\n",
3321 				i, socketid);
3322 		}
3323 
3324 		printf("LPM: Adding route 0x%08x / %d (%d)\n",
3325 			(unsigned)ipv4_l3fwd_route_array[i].ip,
3326 			ipv4_l3fwd_route_array[i].depth,
3327 			ipv4_l3fwd_route_array[i].if_out);
3328 	}
3329 
3330 	/* create the LPM6 table */
3331 	snprintf(s, sizeof(s), "IPV6_L3FWD_LPM_%d", socketid);
3332 
3333 	config.max_rules = IPV6_L3FWD_LPM_MAX_RULES;
3334 	config.number_tbl8s = IPV6_L3FWD_LPM_NUMBER_TBL8S;
3335 	config.flags = 0;
3336 	ipv6_l3fwd_lookup_struct[socketid] = rte_lpm6_create(s, socketid,
3337 				&config);
3338 	if (ipv6_l3fwd_lookup_struct[socketid] == NULL)
3339 		rte_exit(EXIT_FAILURE, "Unable to create the l3fwd LPM table"
3340 				" on socket %d\n", socketid);
3341 
3342 	/* populate the LPM table */
3343 	for (i = 0; i < IPV6_L3FWD_NUM_ROUTES; i++) {
3344 
3345 		/* skip unused ports */
3346 		if ((1 << ipv6_l3fwd_route_array[i].if_out &
3347 				enabled_port_mask) == 0)
3348 			continue;
3349 
3350 		ret = rte_lpm6_add(ipv6_l3fwd_lookup_struct[socketid],
3351 			ipv6_l3fwd_route_array[i].ip,
3352 			ipv6_l3fwd_route_array[i].depth,
3353 			ipv6_l3fwd_route_array[i].if_out);
3354 
3355 		if (ret < 0) {
3356 			rte_exit(EXIT_FAILURE, "Unable to add entry %u to the "
3357 				"l3fwd LPM table on socket %d\n",
3358 				i, socketid);
3359 		}
3360 
3361 		printf("LPM: Adding route %s / %d (%d)\n",
3362 			"IPV6",
3363 			ipv6_l3fwd_route_array[i].depth,
3364 			ipv6_l3fwd_route_array[i].if_out);
3365 	}
3366 }
3367 #endif
3368 
3369 static int
3370 init_mem(unsigned nb_mbuf)
3371 {
3372 	struct lcore_conf *qconf;
3373 	int socketid;
3374 	unsigned lcore_id;
3375 	char s[64];
3376 
3377 	for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
3378 		if (rte_lcore_is_enabled(lcore_id) == 0)
3379 			continue;
3380 
3381 		if (numa_on)
3382 			socketid = rte_lcore_to_socket_id(lcore_id);
3383 		else
3384 			socketid = 0;
3385 
3386 		if (socketid >= NB_SOCKETS) {
3387 			rte_exit(EXIT_FAILURE, "Socket %d of lcore %u is out of range %d\n",
3388 				socketid, lcore_id, NB_SOCKETS);
3389 		}
3390 		if (pktmbuf_pool[socketid] == NULL) {
3391 			snprintf(s, sizeof(s), "mbuf_pool_%d", socketid);
3392 			pktmbuf_pool[socketid] =
3393 				rte_pktmbuf_pool_create(s, nb_mbuf,
3394 					MEMPOOL_CACHE_SIZE, 0,
3395 					RTE_MBUF_DEFAULT_BUF_SIZE, socketid);
3396 			if (pktmbuf_pool[socketid] == NULL)
3397 				rte_exit(EXIT_FAILURE,
3398 						"Cannot init mbuf pool on socket %d\n", socketid);
3399 			else
3400 				printf("Allocated mbuf pool on socket %d\n", socketid);
3401 
3402 #if (APP_LOOKUP_METHOD == APP_LOOKUP_LPM)
3403 			setup_lpm(socketid);
3404 #else
3405 			setup_hash(socketid);
3406 #endif
3407 		}
3408 		qconf = &lcore_conf[lcore_id];
3409 		qconf->ipv4_lookup_struct = ipv4_l3fwd_lookup_struct[socketid];
3410 		qconf->ipv6_lookup_struct = ipv6_l3fwd_lookup_struct[socketid];
3411 	}
3412 	return 0;
3413 }
3414 
3415 /* Check the link status of all ports in up to 9s, and print them finally */
3416 static void
3417 check_all_ports_link_status(uint32_t port_mask)
3418 {
3419 #define CHECK_INTERVAL 100 /* 100ms */
3420 #define MAX_CHECK_TIME 90 /* 9s (90 * 100ms) in total */
3421 	uint16_t portid;
3422 	uint8_t count, all_ports_up, print_flag = 0;
3423 	struct rte_eth_link link;
3424 
3425 	printf("\nChecking link status");
3426 	fflush(stdout);
3427 	for (count = 0; count <= MAX_CHECK_TIME; count++) {
3428 		all_ports_up = 1;
3429 		RTE_ETH_FOREACH_DEV(portid) {
3430 			if ((port_mask & (1 << portid)) == 0)
3431 				continue;
3432 			memset(&link, 0, sizeof(link));
3433 			rte_eth_link_get_nowait(portid, &link);
3434 			/* print link status if flag set */
3435 			if (print_flag == 1) {
3436 				if (link.link_status)
3437 					printf(
3438 					"Port%d Link Up. Speed %u Mbps - %s\n",
3439 						portid, link.link_speed,
3440 				(link.link_duplex == ETH_LINK_FULL_DUPLEX) ?
3441 					("full-duplex") : ("half-duplex\n"));
3442 				else
3443 					printf("Port %d Link Down\n", portid);
3444 				continue;
3445 			}
3446 			/* clear all_ports_up flag if any link down */
3447 			if (link.link_status == ETH_LINK_DOWN) {
3448 				all_ports_up = 0;
3449 				break;
3450 			}
3451 		}
3452 		/* after finally printing all link status, get out */
3453 		if (print_flag == 1)
3454 			break;
3455 
3456 		if (all_ports_up == 0) {
3457 			printf(".");
3458 			fflush(stdout);
3459 			rte_delay_ms(CHECK_INTERVAL);
3460 		}
3461 
3462 		/* set the print_flag if all ports up or timeout */
3463 		if (all_ports_up == 1 || count == (MAX_CHECK_TIME - 1)) {
3464 			print_flag = 1;
3465 			printf("done\n");
3466 		}
3467 	}
3468 }
3469 
3470 int
3471 main(int argc, char **argv)
3472 {
3473 	struct rte_eth_dev_info dev_info;
3474 	struct rte_eth_txconf *txconf;
3475 	int ret;
3476 	int i;
3477 	unsigned nb_ports;
3478 	uint16_t queueid, portid;
3479 	unsigned lcore_id;
3480 	uint32_t n_tx_queue, nb_lcores;
3481 	uint8_t nb_rx_queue, queue, socketid;
3482 
3483 	/* init EAL */
3484 	ret = rte_eal_init(argc, argv);
3485 	if (ret < 0)
3486 		rte_exit(EXIT_FAILURE, "Invalid EAL parameters\n");
3487 	argc -= ret;
3488 	argv += ret;
3489 
3490 	rte_timer_subsystem_init();
3491 
3492 	/* pre-init dst MACs for all ports to 02:00:00:00:00:xx */
3493 	for (portid = 0; portid < RTE_MAX_ETHPORTS; portid++) {
3494 		dest_eth_addr[portid] = ETHER_LOCAL_ADMIN_ADDR +
3495 				((uint64_t)portid << 40);
3496 		*(uint64_t *)(val_eth + portid) = dest_eth_addr[portid];
3497 	}
3498 
3499 	/* parse application arguments (after the EAL ones) */
3500 	ret = parse_args(argc, argv);
3501 	if (ret < 0)
3502 		rte_exit(EXIT_FAILURE, "Invalid L3FWD parameters\n");
3503 
3504 	if (check_lcore_params() < 0)
3505 		rte_exit(EXIT_FAILURE, "check_lcore_params failed\n");
3506 
3507 	printf("Initializing rx-queues...\n");
3508 	ret = init_rx_queues();
3509 	if (ret < 0)
3510 		rte_exit(EXIT_FAILURE, "init_rx_queues failed\n");
3511 
3512 	printf("Initializing tx-threads...\n");
3513 	ret = init_tx_threads();
3514 	if (ret < 0)
3515 		rte_exit(EXIT_FAILURE, "init_tx_threads failed\n");
3516 
3517 	printf("Initializing rings...\n");
3518 	ret = init_rx_rings();
3519 	if (ret < 0)
3520 		rte_exit(EXIT_FAILURE, "init_rx_rings failed\n");
3521 
3522 	nb_ports = rte_eth_dev_count_avail();
3523 
3524 	if (check_port_config() < 0)
3525 		rte_exit(EXIT_FAILURE, "check_port_config failed\n");
3526 
3527 	nb_lcores = rte_lcore_count();
3528 
3529 	/* initialize all ports */
3530 	RTE_ETH_FOREACH_DEV(portid) {
3531 		struct rte_eth_conf local_port_conf = port_conf;
3532 
3533 		/* skip ports that are not enabled */
3534 		if ((enabled_port_mask & (1 << portid)) == 0) {
3535 			printf("\nSkipping disabled port %d\n", portid);
3536 			continue;
3537 		}
3538 
3539 		/* init port */
3540 		printf("Initializing port %d ... ", portid);
3541 		fflush(stdout);
3542 
3543 		nb_rx_queue = get_port_n_rx_queues(portid);
3544 		n_tx_queue = nb_lcores;
3545 		if (n_tx_queue > MAX_TX_QUEUE_PER_PORT)
3546 			n_tx_queue = MAX_TX_QUEUE_PER_PORT;
3547 		printf("Creating queues: nb_rxq=%d nb_txq=%u... ",
3548 			nb_rx_queue, (unsigned)n_tx_queue);
3549 		rte_eth_dev_info_get(portid, &dev_info);
3550 		if (dev_info.tx_offload_capa & DEV_TX_OFFLOAD_MBUF_FAST_FREE)
3551 			local_port_conf.txmode.offloads |=
3552 				DEV_TX_OFFLOAD_MBUF_FAST_FREE;
3553 
3554 		local_port_conf.rx_adv_conf.rss_conf.rss_hf &=
3555 			dev_info.flow_type_rss_offloads;
3556 		if (local_port_conf.rx_adv_conf.rss_conf.rss_hf !=
3557 				port_conf.rx_adv_conf.rss_conf.rss_hf) {
3558 			printf("Port %u modified RSS hash function based on hardware support,"
3559 				"requested:%#"PRIx64" configured:%#"PRIx64"\n",
3560 				portid,
3561 				port_conf.rx_adv_conf.rss_conf.rss_hf,
3562 				local_port_conf.rx_adv_conf.rss_conf.rss_hf);
3563 		}
3564 
3565 		ret = rte_eth_dev_configure(portid, nb_rx_queue,
3566 					(uint16_t)n_tx_queue, &local_port_conf);
3567 		if (ret < 0)
3568 			rte_exit(EXIT_FAILURE, "Cannot configure device: err=%d, port=%d\n",
3569 				ret, portid);
3570 
3571 		ret = rte_eth_dev_adjust_nb_rx_tx_desc(portid, &nb_rxd,
3572 						       &nb_txd);
3573 		if (ret < 0)
3574 			rte_exit(EXIT_FAILURE,
3575 				 "rte_eth_dev_adjust_nb_rx_tx_desc: err=%d, port=%d\n",
3576 				 ret, portid);
3577 
3578 		rte_eth_macaddr_get(portid, &ports_eth_addr[portid]);
3579 		print_ethaddr(" Address:", &ports_eth_addr[portid]);
3580 		printf(", ");
3581 		print_ethaddr("Destination:",
3582 			(const struct ether_addr *)&dest_eth_addr[portid]);
3583 		printf(", ");
3584 
3585 		/*
3586 		 * prepare src MACs for each port.
3587 		 */
3588 		ether_addr_copy(&ports_eth_addr[portid],
3589 			(struct ether_addr *)(val_eth + portid) + 1);
3590 
3591 		/* init memory */
3592 		ret = init_mem(NB_MBUF);
3593 		if (ret < 0)
3594 			rte_exit(EXIT_FAILURE, "init_mem failed\n");
3595 
3596 		/* init one TX queue per couple (lcore,port) */
3597 		queueid = 0;
3598 		for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
3599 			if (rte_lcore_is_enabled(lcore_id) == 0)
3600 				continue;
3601 
3602 			if (numa_on)
3603 				socketid = (uint8_t)rte_lcore_to_socket_id(lcore_id);
3604 			else
3605 				socketid = 0;
3606 
3607 			printf("txq=%u,%d,%d ", lcore_id, queueid, socketid);
3608 			fflush(stdout);
3609 
3610 			txconf = &dev_info.default_txconf;
3611 			txconf->offloads = local_port_conf.txmode.offloads;
3612 			ret = rte_eth_tx_queue_setup(portid, queueid, nb_txd,
3613 						     socketid, txconf);
3614 			if (ret < 0)
3615 				rte_exit(EXIT_FAILURE, "rte_eth_tx_queue_setup: err=%d, "
3616 					"port=%d\n", ret, portid);
3617 
3618 			tx_thread[lcore_id].tx_queue_id[portid] = queueid;
3619 			queueid++;
3620 		}
3621 		printf("\n");
3622 	}
3623 
3624 	for (i = 0; i < n_rx_thread; i++) {
3625 		lcore_id = rx_thread[i].conf.lcore_id;
3626 
3627 		if (rte_lcore_is_enabled(lcore_id) == 0) {
3628 			rte_exit(EXIT_FAILURE,
3629 					"Cannot start Rx thread on lcore %u: lcore disabled\n",
3630 					lcore_id
3631 				);
3632 		}
3633 
3634 		printf("\nInitializing rx queues for Rx thread %d on lcore %u ... ",
3635 				i, lcore_id);
3636 		fflush(stdout);
3637 
3638 		/* init RX queues */
3639 		for (queue = 0; queue < rx_thread[i].n_rx_queue; ++queue) {
3640 			struct rte_eth_rxconf rxq_conf;
3641 
3642 			portid = rx_thread[i].rx_queue_list[queue].port_id;
3643 			queueid = rx_thread[i].rx_queue_list[queue].queue_id;
3644 
3645 			if (numa_on)
3646 				socketid = (uint8_t)rte_lcore_to_socket_id(lcore_id);
3647 			else
3648 				socketid = 0;
3649 
3650 			printf("rxq=%d,%d,%d ", portid, queueid, socketid);
3651 			fflush(stdout);
3652 
3653 			rte_eth_dev_info_get(portid, &dev_info);
3654 			rxq_conf = dev_info.default_rxconf;
3655 			rxq_conf.offloads = port_conf.rxmode.offloads;
3656 			ret = rte_eth_rx_queue_setup(portid, queueid, nb_rxd,
3657 					socketid,
3658 					&rxq_conf,
3659 					pktmbuf_pool[socketid]);
3660 			if (ret < 0)
3661 				rte_exit(EXIT_FAILURE, "rte_eth_rx_queue_setup: err=%d, "
3662 						"port=%d\n", ret, portid);
3663 		}
3664 	}
3665 
3666 	printf("\n");
3667 
3668 	/* start ports */
3669 	RTE_ETH_FOREACH_DEV(portid) {
3670 		if ((enabled_port_mask & (1 << portid)) == 0)
3671 			continue;
3672 
3673 		/* Start device */
3674 		ret = rte_eth_dev_start(portid);
3675 		if (ret < 0)
3676 			rte_exit(EXIT_FAILURE, "rte_eth_dev_start: err=%d, port=%d\n",
3677 				ret, portid);
3678 
3679 		/*
3680 		 * If enabled, put device in promiscuous mode.
3681 		 * This allows IO forwarding mode to forward packets
3682 		 * to itself through 2 cross-connected  ports of the
3683 		 * target machine.
3684 		 */
3685 		if (promiscuous_on)
3686 			rte_eth_promiscuous_enable(portid);
3687 	}
3688 
3689 	for (i = 0; i < n_rx_thread; i++) {
3690 		lcore_id = rx_thread[i].conf.lcore_id;
3691 		if (rte_lcore_is_enabled(lcore_id) == 0)
3692 			continue;
3693 
3694 		/* check if hw packet type is supported */
3695 		for (queue = 0; queue < rx_thread[i].n_rx_queue; ++queue) {
3696 			portid = rx_thread[i].rx_queue_list[queue].port_id;
3697 			queueid = rx_thread[i].rx_queue_list[queue].queue_id;
3698 
3699 			if (parse_ptype_on) {
3700 				if (!rte_eth_add_rx_callback(portid, queueid,
3701 						cb_parse_ptype, NULL))
3702 					rte_exit(EXIT_FAILURE,
3703 						"Failed to add rx callback: "
3704 						"port=%d\n", portid);
3705 			} else if (!check_ptype(portid))
3706 				rte_exit(EXIT_FAILURE,
3707 					"Port %d cannot parse packet type.\n\n"
3708 					"Please add --parse-ptype to use sw "
3709 					"packet type analyzer.\n\n",
3710 					portid);
3711 		}
3712 	}
3713 
3714 	check_all_ports_link_status(enabled_port_mask);
3715 
3716 	if (lthreads_on) {
3717 		printf("Starting L-Threading Model\n");
3718 
3719 #if (APP_CPU_LOAD > 0)
3720 		if (cpu_load_lcore_id > 0)
3721 			/* Use one lcore for cpu load collector */
3722 			nb_lcores--;
3723 #endif
3724 
3725 		lthread_num_schedulers_set(nb_lcores);
3726 		rte_eal_mp_remote_launch(sched_spawner, NULL, SKIP_MASTER);
3727 		lthread_master_spawner(NULL);
3728 
3729 	} else {
3730 		printf("Starting P-Threading Model\n");
3731 		/* launch per-lcore init on every lcore */
3732 		rte_eal_mp_remote_launch(pthread_run, NULL, CALL_MASTER);
3733 		RTE_LCORE_FOREACH_SLAVE(lcore_id) {
3734 			if (rte_eal_wait_lcore(lcore_id) < 0)
3735 				return -1;
3736 		}
3737 	}
3738 
3739 	return 0;
3740 }
3741