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 rte_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 rte_ether_hdr *);
101 	ether_type = eth_hdr->ether_type;
102 	if (ether_type == rte_cpu_to_be_16(RTE_ETHER_TYPE_IPV4))
103 		packet_type |= RTE_PTYPE_L3_IPV4_EXT_UNKNOWN;
104 	else if (ether_type == rte_cpu_to_be_16(RTE_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 rte_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 = RTE_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 	{{RTE_IPV4(101, 0, 0, 0), RTE_IPV4(100, 10, 0, 1),  101, 11, IPPROTO_TCP}, 0},
385 	{{RTE_IPV4(201, 0, 0, 0), RTE_IPV4(200, 20, 0, 1),  102, 12, IPPROTO_TCP}, 1},
386 	{{RTE_IPV4(111, 0, 0, 0), RTE_IPV4(100, 30, 0, 1),  101, 11, IPPROTO_TCP}, 2},
387 	{{RTE_IPV4(211, 0, 0, 0), RTE_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 	{RTE_IPV4(1, 1, 1, 0), 24, 0},
508 	{RTE_IPV4(2, 1, 1, 0), 24, 1},
509 	{RTE_IPV4(3, 1, 1, 0), 24, 2},
510 	{RTE_IPV4(4, 1, 1, 0), 24, 3},
511 	{RTE_IPV4(5, 1, 1, 0), 24, 4},
512 	{RTE_IPV4(6, 1, 1, 0), 24, 5},
513 	{RTE_IPV4(7, 1, 1, 0), 24, 6},
514 	{RTE_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 rte_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 rte_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 rte_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 +
807 		offsetof(struct rte_ipv4_hdr, time_to_live);
808 	__m128i data = _mm_loadu_si128((__m128i *)(ipv4_hdr));
809 	/* Get 5 tuple: dst port, src port, dst IP address, src IP address and
810 	   protocol */
811 	key.xmm = _mm_and_si128(data, mask0);
812 	/* Find destination port */
813 	ret = rte_hash_lookup(ipv4_l3fwd_lookup_struct, (const void *)&key);
814 	return ((ret < 0) ? portid : ipv4_l3fwd_out_if[ret]);
815 }
816 
817 static inline uint16_t
818 get_ipv6_dst_port(void *ipv6_hdr, uint16_t portid,
819 		lookup_struct_t *ipv6_l3fwd_lookup_struct)
820 {
821 	int ret = 0;
822 	union ipv6_5tuple_host key;
823 
824 	ipv6_hdr = (uint8_t *)ipv6_hdr +
825 		offsetof(struct rte_ipv6_hdr, payload_len);
826 	__m128i data0 = _mm_loadu_si128((__m128i *)(ipv6_hdr));
827 	__m128i data1 = _mm_loadu_si128((__m128i *)(((uint8_t *)ipv6_hdr) +
828 			sizeof(__m128i)));
829 	__m128i data2 = _mm_loadu_si128((__m128i *)(((uint8_t *)ipv6_hdr) +
830 			sizeof(__m128i) + sizeof(__m128i)));
831 	/* Get part of 5 tuple: src IP address lower 96 bits and protocol */
832 	key.xmm[0] = _mm_and_si128(data0, mask1);
833 	/* Get part of 5 tuple: dst IP address lower 96 bits and src IP address
834 	   higher 32 bits */
835 	key.xmm[1] = data1;
836 	/* Get part of 5 tuple: dst port and src port and dst IP address higher
837 	   32 bits */
838 	key.xmm[2] = _mm_and_si128(data2, mask2);
839 
840 	/* Find destination port */
841 	ret = rte_hash_lookup(ipv6_l3fwd_lookup_struct, (const void *)&key);
842 	return ((ret < 0) ? portid : ipv6_l3fwd_out_if[ret]);
843 }
844 #endif
845 
846 #if (APP_LOOKUP_METHOD == APP_LOOKUP_LPM)
847 
848 static inline uint16_t
849 get_ipv4_dst_port(void *ipv4_hdr, uint16_t portid,
850 		lookup_struct_t *ipv4_l3fwd_lookup_struct)
851 {
852 	uint32_t next_hop;
853 
854 	return ((rte_lpm_lookup(ipv4_l3fwd_lookup_struct,
855 		rte_be_to_cpu_32(((struct rte_ipv4_hdr *)ipv4_hdr)->dst_addr),
856 		&next_hop) == 0) ? next_hop : portid);
857 }
858 
859 static inline uint16_t
860 get_ipv6_dst_port(void *ipv6_hdr,  uint16_t portid,
861 		lookup6_struct_t *ipv6_l3fwd_lookup_struct)
862 {
863 	uint32_t next_hop;
864 
865 	return ((rte_lpm6_lookup(ipv6_l3fwd_lookup_struct,
866 		((struct rte_ipv6_hdr *)ipv6_hdr)->dst_addr, &next_hop) == 0) ?
867 		next_hop : portid);
868 }
869 #endif
870 
871 static inline void l3fwd_simple_forward(struct rte_mbuf *m, uint16_t portid)
872 		__attribute__((unused));
873 
874 #if ((APP_LOOKUP_METHOD == APP_LOOKUP_EXACT_MATCH) && \
875 	(ENABLE_MULTI_BUFFER_OPTIMIZE == 1))
876 
877 #define MASK_ALL_PKTS   0xff
878 #define EXCLUDE_1ST_PKT 0xfe
879 #define EXCLUDE_2ND_PKT 0xfd
880 #define EXCLUDE_3RD_PKT 0xfb
881 #define EXCLUDE_4TH_PKT 0xf7
882 #define EXCLUDE_5TH_PKT 0xef
883 #define EXCLUDE_6TH_PKT 0xdf
884 #define EXCLUDE_7TH_PKT 0xbf
885 #define EXCLUDE_8TH_PKT 0x7f
886 
887 static inline void
888 simple_ipv4_fwd_8pkts(struct rte_mbuf *m[8], uint16_t portid)
889 {
890 	struct rte_ether_hdr *eth_hdr[8];
891 	struct rte_ipv4_hdr *ipv4_hdr[8];
892 	uint16_t dst_port[8];
893 	int32_t ret[8];
894 	union ipv4_5tuple_host key[8];
895 	__m128i data[8];
896 
897 	eth_hdr[0] = rte_pktmbuf_mtod(m[0], struct rte_ether_hdr *);
898 	eth_hdr[1] = rte_pktmbuf_mtod(m[1], struct rte_ether_hdr *);
899 	eth_hdr[2] = rte_pktmbuf_mtod(m[2], struct rte_ether_hdr *);
900 	eth_hdr[3] = rte_pktmbuf_mtod(m[3], struct rte_ether_hdr *);
901 	eth_hdr[4] = rte_pktmbuf_mtod(m[4], struct rte_ether_hdr *);
902 	eth_hdr[5] = rte_pktmbuf_mtod(m[5], struct rte_ether_hdr *);
903 	eth_hdr[6] = rte_pktmbuf_mtod(m[6], struct rte_ether_hdr *);
904 	eth_hdr[7] = rte_pktmbuf_mtod(m[7], struct rte_ether_hdr *);
905 
906 	/* Handle IPv4 headers.*/
907 	ipv4_hdr[0] = rte_pktmbuf_mtod_offset(m[0], struct rte_ipv4_hdr *,
908 			sizeof(struct rte_ether_hdr));
909 	ipv4_hdr[1] = rte_pktmbuf_mtod_offset(m[1], struct rte_ipv4_hdr *,
910 			sizeof(struct rte_ether_hdr));
911 	ipv4_hdr[2] = rte_pktmbuf_mtod_offset(m[2], struct rte_ipv4_hdr *,
912 			sizeof(struct rte_ether_hdr));
913 	ipv4_hdr[3] = rte_pktmbuf_mtod_offset(m[3], struct rte_ipv4_hdr *,
914 			sizeof(struct rte_ether_hdr));
915 	ipv4_hdr[4] = rte_pktmbuf_mtod_offset(m[4], struct rte_ipv4_hdr *,
916 			sizeof(struct rte_ether_hdr));
917 	ipv4_hdr[5] = rte_pktmbuf_mtod_offset(m[5], struct rte_ipv4_hdr *,
918 			sizeof(struct rte_ether_hdr));
919 	ipv4_hdr[6] = rte_pktmbuf_mtod_offset(m[6], struct rte_ipv4_hdr *,
920 			sizeof(struct rte_ether_hdr));
921 	ipv4_hdr[7] = rte_pktmbuf_mtod_offset(m[7], struct rte_ipv4_hdr *,
922 			sizeof(struct rte_ether_hdr));
923 
924 #ifdef DO_RFC_1812_CHECKS
925 	/* Check to make sure the packet is valid (RFC1812) */
926 	uint8_t valid_mask = MASK_ALL_PKTS;
927 
928 	if (is_valid_ipv4_pkt(ipv4_hdr[0], m[0]->pkt_len) < 0) {
929 		rte_pktmbuf_free(m[0]);
930 		valid_mask &= EXCLUDE_1ST_PKT;
931 	}
932 	if (is_valid_ipv4_pkt(ipv4_hdr[1], m[1]->pkt_len) < 0) {
933 		rte_pktmbuf_free(m[1]);
934 		valid_mask &= EXCLUDE_2ND_PKT;
935 	}
936 	if (is_valid_ipv4_pkt(ipv4_hdr[2], m[2]->pkt_len) < 0) {
937 		rte_pktmbuf_free(m[2]);
938 		valid_mask &= EXCLUDE_3RD_PKT;
939 	}
940 	if (is_valid_ipv4_pkt(ipv4_hdr[3], m[3]->pkt_len) < 0) {
941 		rte_pktmbuf_free(m[3]);
942 		valid_mask &= EXCLUDE_4TH_PKT;
943 	}
944 	if (is_valid_ipv4_pkt(ipv4_hdr[4], m[4]->pkt_len) < 0) {
945 		rte_pktmbuf_free(m[4]);
946 		valid_mask &= EXCLUDE_5TH_PKT;
947 	}
948 	if (is_valid_ipv4_pkt(ipv4_hdr[5], m[5]->pkt_len) < 0) {
949 		rte_pktmbuf_free(m[5]);
950 		valid_mask &= EXCLUDE_6TH_PKT;
951 	}
952 	if (is_valid_ipv4_pkt(ipv4_hdr[6], m[6]->pkt_len) < 0) {
953 		rte_pktmbuf_free(m[6]);
954 		valid_mask &= EXCLUDE_7TH_PKT;
955 	}
956 	if (is_valid_ipv4_pkt(ipv4_hdr[7], m[7]->pkt_len) < 0) {
957 		rte_pktmbuf_free(m[7]);
958 		valid_mask &= EXCLUDE_8TH_PKT;
959 	}
960 	if (unlikely(valid_mask != MASK_ALL_PKTS)) {
961 		if (valid_mask == 0)
962 			return;
963 
964 		uint8_t i = 0;
965 
966 		for (i = 0; i < 8; i++)
967 			if ((0x1 << i) & valid_mask)
968 				l3fwd_simple_forward(m[i], portid);
969 	}
970 #endif /* End of #ifdef DO_RFC_1812_CHECKS */
971 
972 	data[0] = _mm_loadu_si128(rte_pktmbuf_mtod_offset(m[0], __m128i *,
973 			sizeof(struct rte_ether_hdr) +
974 			offsetof(struct rte_ipv4_hdr, time_to_live)));
975 	data[1] = _mm_loadu_si128(rte_pktmbuf_mtod_offset(m[1], __m128i *,
976 			sizeof(struct rte_ether_hdr) +
977 			offsetof(struct rte_ipv4_hdr, time_to_live)));
978 	data[2] = _mm_loadu_si128(rte_pktmbuf_mtod_offset(m[2], __m128i *,
979 			sizeof(struct rte_ether_hdr) +
980 			offsetof(struct rte_ipv4_hdr, time_to_live)));
981 	data[3] = _mm_loadu_si128(rte_pktmbuf_mtod_offset(m[3], __m128i *,
982 			sizeof(struct rte_ether_hdr) +
983 			offsetof(struct rte_ipv4_hdr, time_to_live)));
984 	data[4] = _mm_loadu_si128(rte_pktmbuf_mtod_offset(m[4], __m128i *,
985 			sizeof(struct rte_ether_hdr) +
986 			offsetof(struct rte_ipv4_hdr, time_to_live)));
987 	data[5] = _mm_loadu_si128(rte_pktmbuf_mtod_offset(m[5], __m128i *,
988 			sizeof(struct rte_ether_hdr) +
989 			offsetof(struct rte_ipv4_hdr, time_to_live)));
990 	data[6] = _mm_loadu_si128(rte_pktmbuf_mtod_offset(m[6], __m128i *,
991 			sizeof(struct rte_ether_hdr) +
992 			offsetof(struct rte_ipv4_hdr, time_to_live)));
993 	data[7] = _mm_loadu_si128(rte_pktmbuf_mtod_offset(m[7], __m128i *,
994 			sizeof(struct rte_ether_hdr) +
995 			offsetof(struct rte_ipv4_hdr, time_to_live)));
996 
997 	key[0].xmm = _mm_and_si128(data[0], mask0);
998 	key[1].xmm = _mm_and_si128(data[1], mask0);
999 	key[2].xmm = _mm_and_si128(data[2], mask0);
1000 	key[3].xmm = _mm_and_si128(data[3], mask0);
1001 	key[4].xmm = _mm_and_si128(data[4], mask0);
1002 	key[5].xmm = _mm_and_si128(data[5], mask0);
1003 	key[6].xmm = _mm_and_si128(data[6], mask0);
1004 	key[7].xmm = _mm_and_si128(data[7], mask0);
1005 
1006 	const void *key_array[8] = {&key[0], &key[1], &key[2], &key[3],
1007 			&key[4], &key[5], &key[6], &key[7]};
1008 
1009 	rte_hash_lookup_bulk(RTE_PER_LCORE(lcore_conf)->ipv4_lookup_struct,
1010 			&key_array[0], 8, ret);
1011 	dst_port[0] = ((ret[0] < 0) ? portid : ipv4_l3fwd_out_if[ret[0]]);
1012 	dst_port[1] = ((ret[1] < 0) ? portid : ipv4_l3fwd_out_if[ret[1]]);
1013 	dst_port[2] = ((ret[2] < 0) ? portid : ipv4_l3fwd_out_if[ret[2]]);
1014 	dst_port[3] = ((ret[3] < 0) ? portid : ipv4_l3fwd_out_if[ret[3]]);
1015 	dst_port[4] = ((ret[4] < 0) ? portid : ipv4_l3fwd_out_if[ret[4]]);
1016 	dst_port[5] = ((ret[5] < 0) ? portid : ipv4_l3fwd_out_if[ret[5]]);
1017 	dst_port[6] = ((ret[6] < 0) ? portid : ipv4_l3fwd_out_if[ret[6]]);
1018 	dst_port[7] = ((ret[7] < 0) ? portid : ipv4_l3fwd_out_if[ret[7]]);
1019 
1020 	if (dst_port[0] >= RTE_MAX_ETHPORTS ||
1021 			(enabled_port_mask & 1 << dst_port[0]) == 0)
1022 		dst_port[0] = portid;
1023 	if (dst_port[1] >= RTE_MAX_ETHPORTS ||
1024 			(enabled_port_mask & 1 << dst_port[1]) == 0)
1025 		dst_port[1] = portid;
1026 	if (dst_port[2] >= RTE_MAX_ETHPORTS ||
1027 			(enabled_port_mask & 1 << dst_port[2]) == 0)
1028 		dst_port[2] = portid;
1029 	if (dst_port[3] >= RTE_MAX_ETHPORTS ||
1030 			(enabled_port_mask & 1 << dst_port[3]) == 0)
1031 		dst_port[3] = portid;
1032 	if (dst_port[4] >= RTE_MAX_ETHPORTS ||
1033 			(enabled_port_mask & 1 << dst_port[4]) == 0)
1034 		dst_port[4] = portid;
1035 	if (dst_port[5] >= RTE_MAX_ETHPORTS ||
1036 			(enabled_port_mask & 1 << dst_port[5]) == 0)
1037 		dst_port[5] = portid;
1038 	if (dst_port[6] >= RTE_MAX_ETHPORTS ||
1039 			(enabled_port_mask & 1 << dst_port[6]) == 0)
1040 		dst_port[6] = portid;
1041 	if (dst_port[7] >= RTE_MAX_ETHPORTS ||
1042 			(enabled_port_mask & 1 << dst_port[7]) == 0)
1043 		dst_port[7] = portid;
1044 
1045 #ifdef DO_RFC_1812_CHECKS
1046 	/* Update time to live and header checksum */
1047 	--(ipv4_hdr[0]->time_to_live);
1048 	--(ipv4_hdr[1]->time_to_live);
1049 	--(ipv4_hdr[2]->time_to_live);
1050 	--(ipv4_hdr[3]->time_to_live);
1051 	++(ipv4_hdr[0]->hdr_checksum);
1052 	++(ipv4_hdr[1]->hdr_checksum);
1053 	++(ipv4_hdr[2]->hdr_checksum);
1054 	++(ipv4_hdr[3]->hdr_checksum);
1055 	--(ipv4_hdr[4]->time_to_live);
1056 	--(ipv4_hdr[5]->time_to_live);
1057 	--(ipv4_hdr[6]->time_to_live);
1058 	--(ipv4_hdr[7]->time_to_live);
1059 	++(ipv4_hdr[4]->hdr_checksum);
1060 	++(ipv4_hdr[5]->hdr_checksum);
1061 	++(ipv4_hdr[6]->hdr_checksum);
1062 	++(ipv4_hdr[7]->hdr_checksum);
1063 #endif
1064 
1065 	/* dst addr */
1066 	*(uint64_t *)&eth_hdr[0]->d_addr = dest_eth_addr[dst_port[0]];
1067 	*(uint64_t *)&eth_hdr[1]->d_addr = dest_eth_addr[dst_port[1]];
1068 	*(uint64_t *)&eth_hdr[2]->d_addr = dest_eth_addr[dst_port[2]];
1069 	*(uint64_t *)&eth_hdr[3]->d_addr = dest_eth_addr[dst_port[3]];
1070 	*(uint64_t *)&eth_hdr[4]->d_addr = dest_eth_addr[dst_port[4]];
1071 	*(uint64_t *)&eth_hdr[5]->d_addr = dest_eth_addr[dst_port[5]];
1072 	*(uint64_t *)&eth_hdr[6]->d_addr = dest_eth_addr[dst_port[6]];
1073 	*(uint64_t *)&eth_hdr[7]->d_addr = dest_eth_addr[dst_port[7]];
1074 
1075 	/* src addr */
1076 	rte_ether_addr_copy(&ports_eth_addr[dst_port[0]], &eth_hdr[0]->s_addr);
1077 	rte_ether_addr_copy(&ports_eth_addr[dst_port[1]], &eth_hdr[1]->s_addr);
1078 	rte_ether_addr_copy(&ports_eth_addr[dst_port[2]], &eth_hdr[2]->s_addr);
1079 	rte_ether_addr_copy(&ports_eth_addr[dst_port[3]], &eth_hdr[3]->s_addr);
1080 	rte_ether_addr_copy(&ports_eth_addr[dst_port[4]], &eth_hdr[4]->s_addr);
1081 	rte_ether_addr_copy(&ports_eth_addr[dst_port[5]], &eth_hdr[5]->s_addr);
1082 	rte_ether_addr_copy(&ports_eth_addr[dst_port[6]], &eth_hdr[6]->s_addr);
1083 	rte_ether_addr_copy(&ports_eth_addr[dst_port[7]], &eth_hdr[7]->s_addr);
1084 
1085 	send_single_packet(m[0], (uint8_t)dst_port[0]);
1086 	send_single_packet(m[1], (uint8_t)dst_port[1]);
1087 	send_single_packet(m[2], (uint8_t)dst_port[2]);
1088 	send_single_packet(m[3], (uint8_t)dst_port[3]);
1089 	send_single_packet(m[4], (uint8_t)dst_port[4]);
1090 	send_single_packet(m[5], (uint8_t)dst_port[5]);
1091 	send_single_packet(m[6], (uint8_t)dst_port[6]);
1092 	send_single_packet(m[7], (uint8_t)dst_port[7]);
1093 
1094 }
1095 
1096 static inline void get_ipv6_5tuple(struct rte_mbuf *m0, __m128i mask0,
1097 		__m128i mask1, union ipv6_5tuple_host *key)
1098 {
1099 	__m128i tmpdata0 = _mm_loadu_si128(rte_pktmbuf_mtod_offset(m0,
1100 			__m128i *, sizeof(struct rte_ether_hdr) +
1101 			offsetof(struct rte_ipv6_hdr, payload_len)));
1102 	__m128i tmpdata1 = _mm_loadu_si128(rte_pktmbuf_mtod_offset(m0,
1103 			__m128i *, sizeof(struct rte_ether_hdr) +
1104 			offsetof(struct rte_ipv6_hdr, payload_len) +
1105 			sizeof(__m128i)));
1106 	__m128i tmpdata2 = _mm_loadu_si128(rte_pktmbuf_mtod_offset(m0,
1107 			__m128i *, sizeof(struct rte_ether_hdr) +
1108 			offsetof(struct rte_ipv6_hdr, payload_len) +
1109 			sizeof(__m128i) + sizeof(__m128i)));
1110 	key->xmm[0] = _mm_and_si128(tmpdata0, mask0);
1111 	key->xmm[1] = tmpdata1;
1112 	key->xmm[2] = _mm_and_si128(tmpdata2, mask1);
1113 }
1114 
1115 static inline void
1116 simple_ipv6_fwd_8pkts(struct rte_mbuf *m[8], uint16_t portid)
1117 {
1118 	int32_t ret[8];
1119 	uint16_t dst_port[8];
1120 	struct rte_ether_hdr *eth_hdr[8];
1121 	union ipv6_5tuple_host key[8];
1122 
1123 	__attribute__((unused)) struct rte_ipv6_hdr *ipv6_hdr[8];
1124 
1125 	eth_hdr[0] = rte_pktmbuf_mtod(m[0], struct rte_ether_hdr *);
1126 	eth_hdr[1] = rte_pktmbuf_mtod(m[1], struct rte_ether_hdr *);
1127 	eth_hdr[2] = rte_pktmbuf_mtod(m[2], struct rte_ether_hdr *);
1128 	eth_hdr[3] = rte_pktmbuf_mtod(m[3], struct rte_ether_hdr *);
1129 	eth_hdr[4] = rte_pktmbuf_mtod(m[4], struct rte_ether_hdr *);
1130 	eth_hdr[5] = rte_pktmbuf_mtod(m[5], struct rte_ether_hdr *);
1131 	eth_hdr[6] = rte_pktmbuf_mtod(m[6], struct rte_ether_hdr *);
1132 	eth_hdr[7] = rte_pktmbuf_mtod(m[7], struct rte_ether_hdr *);
1133 
1134 	/* Handle IPv6 headers.*/
1135 	ipv6_hdr[0] = rte_pktmbuf_mtod_offset(m[0], struct rte_ipv6_hdr *,
1136 			sizeof(struct rte_ether_hdr));
1137 	ipv6_hdr[1] = rte_pktmbuf_mtod_offset(m[1], struct rte_ipv6_hdr *,
1138 			sizeof(struct rte_ether_hdr));
1139 	ipv6_hdr[2] = rte_pktmbuf_mtod_offset(m[2], struct rte_ipv6_hdr *,
1140 			sizeof(struct rte_ether_hdr));
1141 	ipv6_hdr[3] = rte_pktmbuf_mtod_offset(m[3], struct rte_ipv6_hdr *,
1142 			sizeof(struct rte_ether_hdr));
1143 	ipv6_hdr[4] = rte_pktmbuf_mtod_offset(m[4], struct rte_ipv6_hdr *,
1144 			sizeof(struct rte_ether_hdr));
1145 	ipv6_hdr[5] = rte_pktmbuf_mtod_offset(m[5], struct rte_ipv6_hdr *,
1146 			sizeof(struct rte_ether_hdr));
1147 	ipv6_hdr[6] = rte_pktmbuf_mtod_offset(m[6], struct rte_ipv6_hdr *,
1148 			sizeof(struct rte_ether_hdr));
1149 	ipv6_hdr[7] = rte_pktmbuf_mtod_offset(m[7], struct rte_ipv6_hdr *,
1150 			sizeof(struct rte_ether_hdr));
1151 
1152 	get_ipv6_5tuple(m[0], mask1, mask2, &key[0]);
1153 	get_ipv6_5tuple(m[1], mask1, mask2, &key[1]);
1154 	get_ipv6_5tuple(m[2], mask1, mask2, &key[2]);
1155 	get_ipv6_5tuple(m[3], mask1, mask2, &key[3]);
1156 	get_ipv6_5tuple(m[4], mask1, mask2, &key[4]);
1157 	get_ipv6_5tuple(m[5], mask1, mask2, &key[5]);
1158 	get_ipv6_5tuple(m[6], mask1, mask2, &key[6]);
1159 	get_ipv6_5tuple(m[7], mask1, mask2, &key[7]);
1160 
1161 	const void *key_array[8] = {&key[0], &key[1], &key[2], &key[3],
1162 			&key[4], &key[5], &key[6], &key[7]};
1163 
1164 	rte_hash_lookup_bulk(RTE_PER_LCORE(lcore_conf)->ipv6_lookup_struct,
1165 			&key_array[0], 4, ret);
1166 	dst_port[0] = ((ret[0] < 0) ? portid : ipv6_l3fwd_out_if[ret[0]]);
1167 	dst_port[1] = ((ret[1] < 0) ? portid : ipv6_l3fwd_out_if[ret[1]]);
1168 	dst_port[2] = ((ret[2] < 0) ? portid : ipv6_l3fwd_out_if[ret[2]]);
1169 	dst_port[3] = ((ret[3] < 0) ? portid : ipv6_l3fwd_out_if[ret[3]]);
1170 	dst_port[4] = ((ret[4] < 0) ? portid : ipv6_l3fwd_out_if[ret[4]]);
1171 	dst_port[5] = ((ret[5] < 0) ? portid : ipv6_l3fwd_out_if[ret[5]]);
1172 	dst_port[6] = ((ret[6] < 0) ? portid : ipv6_l3fwd_out_if[ret[6]]);
1173 	dst_port[7] = ((ret[7] < 0) ? portid : ipv6_l3fwd_out_if[ret[7]]);
1174 
1175 	if (dst_port[0] >= RTE_MAX_ETHPORTS ||
1176 			(enabled_port_mask & 1 << dst_port[0]) == 0)
1177 		dst_port[0] = portid;
1178 	if (dst_port[1] >= RTE_MAX_ETHPORTS ||
1179 			(enabled_port_mask & 1 << dst_port[1]) == 0)
1180 		dst_port[1] = portid;
1181 	if (dst_port[2] >= RTE_MAX_ETHPORTS ||
1182 			(enabled_port_mask & 1 << dst_port[2]) == 0)
1183 		dst_port[2] = portid;
1184 	if (dst_port[3] >= RTE_MAX_ETHPORTS ||
1185 			(enabled_port_mask & 1 << dst_port[3]) == 0)
1186 		dst_port[3] = portid;
1187 	if (dst_port[4] >= RTE_MAX_ETHPORTS ||
1188 			(enabled_port_mask & 1 << dst_port[4]) == 0)
1189 		dst_port[4] = portid;
1190 	if (dst_port[5] >= RTE_MAX_ETHPORTS ||
1191 			(enabled_port_mask & 1 << dst_port[5]) == 0)
1192 		dst_port[5] = portid;
1193 	if (dst_port[6] >= RTE_MAX_ETHPORTS ||
1194 			(enabled_port_mask & 1 << dst_port[6]) == 0)
1195 		dst_port[6] = portid;
1196 	if (dst_port[7] >= RTE_MAX_ETHPORTS ||
1197 			(enabled_port_mask & 1 << dst_port[7]) == 0)
1198 		dst_port[7] = portid;
1199 
1200 	/* dst addr */
1201 	*(uint64_t *)&eth_hdr[0]->d_addr = dest_eth_addr[dst_port[0]];
1202 	*(uint64_t *)&eth_hdr[1]->d_addr = dest_eth_addr[dst_port[1]];
1203 	*(uint64_t *)&eth_hdr[2]->d_addr = dest_eth_addr[dst_port[2]];
1204 	*(uint64_t *)&eth_hdr[3]->d_addr = dest_eth_addr[dst_port[3]];
1205 	*(uint64_t *)&eth_hdr[4]->d_addr = dest_eth_addr[dst_port[4]];
1206 	*(uint64_t *)&eth_hdr[5]->d_addr = dest_eth_addr[dst_port[5]];
1207 	*(uint64_t *)&eth_hdr[6]->d_addr = dest_eth_addr[dst_port[6]];
1208 	*(uint64_t *)&eth_hdr[7]->d_addr = dest_eth_addr[dst_port[7]];
1209 
1210 	/* src addr */
1211 	rte_ether_addr_copy(&ports_eth_addr[dst_port[0]], &eth_hdr[0]->s_addr);
1212 	rte_ether_addr_copy(&ports_eth_addr[dst_port[1]], &eth_hdr[1]->s_addr);
1213 	rte_ether_addr_copy(&ports_eth_addr[dst_port[2]], &eth_hdr[2]->s_addr);
1214 	rte_ether_addr_copy(&ports_eth_addr[dst_port[3]], &eth_hdr[3]->s_addr);
1215 	rte_ether_addr_copy(&ports_eth_addr[dst_port[4]], &eth_hdr[4]->s_addr);
1216 	rte_ether_addr_copy(&ports_eth_addr[dst_port[5]], &eth_hdr[5]->s_addr);
1217 	rte_ether_addr_copy(&ports_eth_addr[dst_port[6]], &eth_hdr[6]->s_addr);
1218 	rte_ether_addr_copy(&ports_eth_addr[dst_port[7]], &eth_hdr[7]->s_addr);
1219 
1220 	send_single_packet(m[0], dst_port[0]);
1221 	send_single_packet(m[1], dst_port[1]);
1222 	send_single_packet(m[2], dst_port[2]);
1223 	send_single_packet(m[3], dst_port[3]);
1224 	send_single_packet(m[4], dst_port[4]);
1225 	send_single_packet(m[5], dst_port[5]);
1226 	send_single_packet(m[6], dst_port[6]);
1227 	send_single_packet(m[7], dst_port[7]);
1228 
1229 }
1230 #endif /* APP_LOOKUP_METHOD */
1231 
1232 static __rte_always_inline void
1233 l3fwd_simple_forward(struct rte_mbuf *m, uint16_t portid)
1234 {
1235 	struct rte_ether_hdr *eth_hdr;
1236 	struct rte_ipv4_hdr *ipv4_hdr;
1237 	uint16_t dst_port;
1238 
1239 	eth_hdr = rte_pktmbuf_mtod(m, struct rte_ether_hdr *);
1240 
1241 	if (RTE_ETH_IS_IPV4_HDR(m->packet_type)) {
1242 		/* Handle IPv4 headers.*/
1243 		ipv4_hdr = rte_pktmbuf_mtod_offset(m, struct rte_ipv4_hdr *,
1244 				sizeof(struct rte_ether_hdr));
1245 
1246 #ifdef DO_RFC_1812_CHECKS
1247 		/* Check to make sure the packet is valid (RFC1812) */
1248 		if (is_valid_ipv4_pkt(ipv4_hdr, m->pkt_len) < 0) {
1249 			rte_pktmbuf_free(m);
1250 			return;
1251 		}
1252 #endif
1253 
1254 		 dst_port = get_ipv4_dst_port(ipv4_hdr, portid,
1255 			RTE_PER_LCORE(lcore_conf)->ipv4_lookup_struct);
1256 		if (dst_port >= RTE_MAX_ETHPORTS ||
1257 				(enabled_port_mask & 1 << dst_port) == 0)
1258 			dst_port = portid;
1259 
1260 #ifdef DO_RFC_1812_CHECKS
1261 		/* Update time to live and header checksum */
1262 		--(ipv4_hdr->time_to_live);
1263 		++(ipv4_hdr->hdr_checksum);
1264 #endif
1265 		/* dst addr */
1266 		*(uint64_t *)&eth_hdr->d_addr = dest_eth_addr[dst_port];
1267 
1268 		/* src addr */
1269 		rte_ether_addr_copy(&ports_eth_addr[dst_port],
1270 				&eth_hdr->s_addr);
1271 
1272 		send_single_packet(m, dst_port);
1273 	} else if (RTE_ETH_IS_IPV6_HDR(m->packet_type)) {
1274 		/* Handle IPv6 headers.*/
1275 		struct rte_ipv6_hdr *ipv6_hdr;
1276 
1277 		ipv6_hdr = rte_pktmbuf_mtod_offset(m, struct rte_ipv6_hdr *,
1278 				sizeof(struct rte_ether_hdr));
1279 
1280 		dst_port = get_ipv6_dst_port(ipv6_hdr, portid,
1281 				RTE_PER_LCORE(lcore_conf)->ipv6_lookup_struct);
1282 
1283 		if (dst_port >= RTE_MAX_ETHPORTS ||
1284 				(enabled_port_mask & 1 << dst_port) == 0)
1285 			dst_port = portid;
1286 
1287 		/* dst addr */
1288 		*(uint64_t *)&eth_hdr->d_addr = dest_eth_addr[dst_port];
1289 
1290 		/* src addr */
1291 		rte_ether_addr_copy(&ports_eth_addr[dst_port],
1292 				&eth_hdr->s_addr);
1293 
1294 		send_single_packet(m, dst_port);
1295 	} else
1296 		/* Free the mbuf that contains non-IPV4/IPV6 packet */
1297 		rte_pktmbuf_free(m);
1298 }
1299 
1300 #if ((APP_LOOKUP_METHOD == APP_LOOKUP_LPM) && \
1301 	(ENABLE_MULTI_BUFFER_OPTIMIZE == 1))
1302 #ifdef DO_RFC_1812_CHECKS
1303 
1304 #define	IPV4_MIN_VER_IHL	0x45
1305 #define	IPV4_MAX_VER_IHL	0x4f
1306 #define	IPV4_MAX_VER_IHL_DIFF	(IPV4_MAX_VER_IHL - IPV4_MIN_VER_IHL)
1307 
1308 /* Minimum value of IPV4 total length (20B) in network byte order. */
1309 #define	IPV4_MIN_LEN_BE	(sizeof(struct rte_ipv4_hdr) << 8)
1310 
1311 /*
1312  * From http://www.rfc-editor.org/rfc/rfc1812.txt section 5.2.2:
1313  * - The IP version number must be 4.
1314  * - The IP header length field must be large enough to hold the
1315  *    minimum length legal IP datagram (20 bytes = 5 words).
1316  * - The IP total length field must be large enough to hold the IP
1317  *   datagram header, whose length is specified in the IP header length
1318  *   field.
1319  * If we encounter invalid IPV4 packet, then set destination port for it
1320  * to BAD_PORT value.
1321  */
1322 static __rte_always_inline void
1323 rfc1812_process(struct rte_ipv4_hdr *ipv4_hdr, uint16_t *dp, uint32_t ptype)
1324 {
1325 	uint8_t ihl;
1326 
1327 	if (RTE_ETH_IS_IPV4_HDR(ptype)) {
1328 		ihl = ipv4_hdr->version_ihl - IPV4_MIN_VER_IHL;
1329 
1330 		ipv4_hdr->time_to_live--;
1331 		ipv4_hdr->hdr_checksum++;
1332 
1333 		if (ihl > IPV4_MAX_VER_IHL_DIFF ||
1334 				((uint8_t)ipv4_hdr->total_length == 0 &&
1335 				ipv4_hdr->total_length < IPV4_MIN_LEN_BE)) {
1336 			dp[0] = BAD_PORT;
1337 		}
1338 	}
1339 }
1340 
1341 #else
1342 #define	rfc1812_process(mb, dp, ptype)	do { } while (0)
1343 #endif /* DO_RFC_1812_CHECKS */
1344 #endif /* APP_LOOKUP_LPM && ENABLE_MULTI_BUFFER_OPTIMIZE */
1345 
1346 
1347 #if ((APP_LOOKUP_METHOD == APP_LOOKUP_LPM) && \
1348 	(ENABLE_MULTI_BUFFER_OPTIMIZE == 1))
1349 
1350 static __rte_always_inline uint16_t
1351 get_dst_port(struct rte_mbuf *pkt, uint32_t dst_ipv4, uint16_t portid)
1352 {
1353 	uint32_t next_hop;
1354 	struct rte_ipv6_hdr *ipv6_hdr;
1355 	struct rte_ether_hdr *eth_hdr;
1356 
1357 	if (RTE_ETH_IS_IPV4_HDR(pkt->packet_type)) {
1358 		return (uint16_t) ((rte_lpm_lookup(
1359 				RTE_PER_LCORE(lcore_conf)->ipv4_lookup_struct, dst_ipv4,
1360 				&next_hop) == 0) ? next_hop : portid);
1361 
1362 	} else if (RTE_ETH_IS_IPV6_HDR(pkt->packet_type)) {
1363 
1364 		eth_hdr = rte_pktmbuf_mtod(pkt, struct rte_ether_hdr *);
1365 		ipv6_hdr = (struct rte_ipv6_hdr *)(eth_hdr + 1);
1366 
1367 		return (uint16_t) ((rte_lpm6_lookup(
1368 				RTE_PER_LCORE(lcore_conf)->ipv6_lookup_struct,
1369 				ipv6_hdr->dst_addr, &next_hop) == 0) ?
1370 				next_hop : portid);
1371 
1372 	}
1373 
1374 	return portid;
1375 }
1376 
1377 static inline void
1378 process_packet(struct rte_mbuf *pkt, uint16_t *dst_port, uint16_t portid)
1379 {
1380 	struct rte_ether_hdr *eth_hdr;
1381 	struct rte_ipv4_hdr *ipv4_hdr;
1382 	uint32_t dst_ipv4;
1383 	uint16_t dp;
1384 	__m128i te, ve;
1385 
1386 	eth_hdr = rte_pktmbuf_mtod(pkt, struct rte_ether_hdr *);
1387 	ipv4_hdr = (struct rte_ipv4_hdr *)(eth_hdr + 1);
1388 
1389 	dst_ipv4 = ipv4_hdr->dst_addr;
1390 	dst_ipv4 = rte_be_to_cpu_32(dst_ipv4);
1391 	dp = get_dst_port(pkt, dst_ipv4, portid);
1392 
1393 	te = _mm_load_si128((__m128i *)eth_hdr);
1394 	ve = val_eth[dp];
1395 
1396 	dst_port[0] = dp;
1397 	rfc1812_process(ipv4_hdr, dst_port, pkt->packet_type);
1398 
1399 	te =  _mm_blend_epi16(te, ve, MASK_ETH);
1400 	_mm_store_si128((__m128i *)eth_hdr, te);
1401 }
1402 
1403 /*
1404  * Read packet_type and destination IPV4 addresses from 4 mbufs.
1405  */
1406 static inline void
1407 processx4_step1(struct rte_mbuf *pkt[FWDSTEP],
1408 		__m128i *dip,
1409 		uint32_t *ipv4_flag)
1410 {
1411 	struct rte_ipv4_hdr *ipv4_hdr;
1412 	struct rte_ether_hdr *eth_hdr;
1413 	uint32_t x0, x1, x2, x3;
1414 
1415 	eth_hdr = rte_pktmbuf_mtod(pkt[0], struct rte_ether_hdr *);
1416 	ipv4_hdr = (struct rte_ipv4_hdr *)(eth_hdr + 1);
1417 	x0 = ipv4_hdr->dst_addr;
1418 	ipv4_flag[0] = pkt[0]->packet_type & RTE_PTYPE_L3_IPV4;
1419 
1420 	eth_hdr = rte_pktmbuf_mtod(pkt[1], struct rte_ether_hdr *);
1421 	ipv4_hdr = (struct rte_ipv4_hdr *)(eth_hdr + 1);
1422 	x1 = ipv4_hdr->dst_addr;
1423 	ipv4_flag[0] &= pkt[1]->packet_type;
1424 
1425 	eth_hdr = rte_pktmbuf_mtod(pkt[2], struct rte_ether_hdr *);
1426 	ipv4_hdr = (struct rte_ipv4_hdr *)(eth_hdr + 1);
1427 	x2 = ipv4_hdr->dst_addr;
1428 	ipv4_flag[0] &= pkt[2]->packet_type;
1429 
1430 	eth_hdr = rte_pktmbuf_mtod(pkt[3], struct rte_ether_hdr *);
1431 	ipv4_hdr = (struct rte_ipv4_hdr *)(eth_hdr + 1);
1432 	x3 = ipv4_hdr->dst_addr;
1433 	ipv4_flag[0] &= pkt[3]->packet_type;
1434 
1435 	dip[0] = _mm_set_epi32(x3, x2, x1, x0);
1436 }
1437 
1438 /*
1439  * Lookup into LPM for destination port.
1440  * If lookup fails, use incoming port (portid) as destination port.
1441  */
1442 static inline void
1443 processx4_step2(__m128i dip,
1444 		uint32_t ipv4_flag,
1445 		uint16_t portid,
1446 		struct rte_mbuf *pkt[FWDSTEP],
1447 		uint16_t dprt[FWDSTEP])
1448 {
1449 	rte_xmm_t dst;
1450 	const __m128i bswap_mask = _mm_set_epi8(12, 13, 14, 15, 8, 9, 10, 11,
1451 			4, 5, 6, 7, 0, 1, 2, 3);
1452 
1453 	/* Byte swap 4 IPV4 addresses. */
1454 	dip = _mm_shuffle_epi8(dip, bswap_mask);
1455 
1456 	/* if all 4 packets are IPV4. */
1457 	if (likely(ipv4_flag)) {
1458 		rte_lpm_lookupx4(RTE_PER_LCORE(lcore_conf)->ipv4_lookup_struct, dip,
1459 				dst.u32, portid);
1460 
1461 		/* get rid of unused upper 16 bit for each dport. */
1462 		dst.x = _mm_packs_epi32(dst.x, dst.x);
1463 		*(uint64_t *)dprt = dst.u64[0];
1464 	} else {
1465 		dst.x = dip;
1466 		dprt[0] = get_dst_port(pkt[0], dst.u32[0], portid);
1467 		dprt[1] = get_dst_port(pkt[1], dst.u32[1], portid);
1468 		dprt[2] = get_dst_port(pkt[2], dst.u32[2], portid);
1469 		dprt[3] = get_dst_port(pkt[3], dst.u32[3], portid);
1470 	}
1471 }
1472 
1473 /*
1474  * Update source and destination MAC addresses in the ethernet header.
1475  * Perform RFC1812 checks and updates for IPV4 packets.
1476  */
1477 static inline void
1478 processx4_step3(struct rte_mbuf *pkt[FWDSTEP], uint16_t dst_port[FWDSTEP])
1479 {
1480 	__m128i te[FWDSTEP];
1481 	__m128i ve[FWDSTEP];
1482 	__m128i *p[FWDSTEP];
1483 
1484 	p[0] = rte_pktmbuf_mtod(pkt[0], __m128i *);
1485 	p[1] = rte_pktmbuf_mtod(pkt[1], __m128i *);
1486 	p[2] = rte_pktmbuf_mtod(pkt[2], __m128i *);
1487 	p[3] = rte_pktmbuf_mtod(pkt[3], __m128i *);
1488 
1489 	ve[0] = val_eth[dst_port[0]];
1490 	te[0] = _mm_load_si128(p[0]);
1491 
1492 	ve[1] = val_eth[dst_port[1]];
1493 	te[1] = _mm_load_si128(p[1]);
1494 
1495 	ve[2] = val_eth[dst_port[2]];
1496 	te[2] = _mm_load_si128(p[2]);
1497 
1498 	ve[3] = val_eth[dst_port[3]];
1499 	te[3] = _mm_load_si128(p[3]);
1500 
1501 	/* Update first 12 bytes, keep rest bytes intact. */
1502 	te[0] =  _mm_blend_epi16(te[0], ve[0], MASK_ETH);
1503 	te[1] =  _mm_blend_epi16(te[1], ve[1], MASK_ETH);
1504 	te[2] =  _mm_blend_epi16(te[2], ve[2], MASK_ETH);
1505 	te[3] =  _mm_blend_epi16(te[3], ve[3], MASK_ETH);
1506 
1507 	_mm_store_si128(p[0], te[0]);
1508 	_mm_store_si128(p[1], te[1]);
1509 	_mm_store_si128(p[2], te[2]);
1510 	_mm_store_si128(p[3], te[3]);
1511 
1512 	rfc1812_process((struct rte_ipv4_hdr *)
1513 			((struct rte_ether_hdr *)p[0] + 1),
1514 			&dst_port[0], pkt[0]->packet_type);
1515 	rfc1812_process((struct rte_ipv4_hdr *)
1516 			((struct rte_ether_hdr *)p[1] + 1),
1517 			&dst_port[1], pkt[1]->packet_type);
1518 	rfc1812_process((struct rte_ipv4_hdr *)
1519 			((struct rte_ether_hdr *)p[2] + 1),
1520 			&dst_port[2], pkt[2]->packet_type);
1521 	rfc1812_process((struct rte_ipv4_hdr *)
1522 			((struct rte_ether_hdr *)p[3] + 1),
1523 			&dst_port[3], pkt[3]->packet_type);
1524 }
1525 
1526 /*
1527  * We group consecutive packets with the same destionation port into one burst.
1528  * To avoid extra latency this is done together with some other packet
1529  * processing, but after we made a final decision about packet's destination.
1530  * To do this we maintain:
1531  * pnum - array of number of consecutive packets with the same dest port for
1532  * each packet in the input burst.
1533  * lp - pointer to the last updated element in the pnum.
1534  * dlp - dest port value lp corresponds to.
1535  */
1536 
1537 #define	GRPSZ	(1 << FWDSTEP)
1538 #define	GRPMSK	(GRPSZ - 1)
1539 
1540 #define GROUP_PORT_STEP(dlp, dcp, lp, pn, idx)	do { \
1541 	if (likely((dlp) == (dcp)[(idx)])) {         \
1542 		(lp)[0]++;                           \
1543 	} else {                                     \
1544 		(dlp) = (dcp)[idx];                  \
1545 		(lp) = (pn) + (idx);                 \
1546 		(lp)[0] = 1;                         \
1547 	}                                            \
1548 } while (0)
1549 
1550 /*
1551  * Group consecutive packets with the same destination port in bursts of 4.
1552  * Suppose we have array of destionation ports:
1553  * dst_port[] = {a, b, c, d,, e, ... }
1554  * dp1 should contain: <a, b, c, d>, dp2: <b, c, d, e>.
1555  * We doing 4 comparisons at once and the result is 4 bit mask.
1556  * This mask is used as an index into prebuild array of pnum values.
1557  */
1558 static inline uint16_t *
1559 port_groupx4(uint16_t pn[FWDSTEP + 1], uint16_t *lp, __m128i dp1, __m128i dp2)
1560 {
1561 	static const struct {
1562 		uint64_t pnum; /* prebuild 4 values for pnum[]. */
1563 		int32_t  idx;  /* index for new last updated elemnet. */
1564 		uint16_t lpv;  /* add value to the last updated element. */
1565 	} gptbl[GRPSZ] = {
1566 	{
1567 		/* 0: a != b, b != c, c != d, d != e */
1568 		.pnum = UINT64_C(0x0001000100010001),
1569 		.idx = 4,
1570 		.lpv = 0,
1571 	},
1572 	{
1573 		/* 1: a == b, b != c, c != d, d != e */
1574 		.pnum = UINT64_C(0x0001000100010002),
1575 		.idx = 4,
1576 		.lpv = 1,
1577 	},
1578 	{
1579 		/* 2: a != b, b == c, c != d, d != e */
1580 		.pnum = UINT64_C(0x0001000100020001),
1581 		.idx = 4,
1582 		.lpv = 0,
1583 	},
1584 	{
1585 		/* 3: a == b, b == c, c != d, d != e */
1586 		.pnum = UINT64_C(0x0001000100020003),
1587 		.idx = 4,
1588 		.lpv = 2,
1589 	},
1590 	{
1591 		/* 4: a != b, b != c, c == d, d != e */
1592 		.pnum = UINT64_C(0x0001000200010001),
1593 		.idx = 4,
1594 		.lpv = 0,
1595 	},
1596 	{
1597 		/* 5: a == b, b != c, c == d, d != e */
1598 		.pnum = UINT64_C(0x0001000200010002),
1599 		.idx = 4,
1600 		.lpv = 1,
1601 	},
1602 	{
1603 		/* 6: a != b, b == c, c == d, d != e */
1604 		.pnum = UINT64_C(0x0001000200030001),
1605 		.idx = 4,
1606 		.lpv = 0,
1607 	},
1608 	{
1609 		/* 7: a == b, b == c, c == d, d != e */
1610 		.pnum = UINT64_C(0x0001000200030004),
1611 		.idx = 4,
1612 		.lpv = 3,
1613 	},
1614 	{
1615 		/* 8: a != b, b != c, c != d, d == e */
1616 		.pnum = UINT64_C(0x0002000100010001),
1617 		.idx = 3,
1618 		.lpv = 0,
1619 	},
1620 	{
1621 		/* 9: a == b, b != c, c != d, d == e */
1622 		.pnum = UINT64_C(0x0002000100010002),
1623 		.idx = 3,
1624 		.lpv = 1,
1625 	},
1626 	{
1627 		/* 0xa: a != b, b == c, c != d, d == e */
1628 		.pnum = UINT64_C(0x0002000100020001),
1629 		.idx = 3,
1630 		.lpv = 0,
1631 	},
1632 	{
1633 		/* 0xb: a == b, b == c, c != d, d == e */
1634 		.pnum = UINT64_C(0x0002000100020003),
1635 		.idx = 3,
1636 		.lpv = 2,
1637 	},
1638 	{
1639 		/* 0xc: a != b, b != c, c == d, d == e */
1640 		.pnum = UINT64_C(0x0002000300010001),
1641 		.idx = 2,
1642 		.lpv = 0,
1643 	},
1644 	{
1645 		/* 0xd: a == b, b != c, c == d, d == e */
1646 		.pnum = UINT64_C(0x0002000300010002),
1647 		.idx = 2,
1648 		.lpv = 1,
1649 	},
1650 	{
1651 		/* 0xe: a != b, b == c, c == d, d == e */
1652 		.pnum = UINT64_C(0x0002000300040001),
1653 		.idx = 1,
1654 		.lpv = 0,
1655 	},
1656 	{
1657 		/* 0xf: a == b, b == c, c == d, d == e */
1658 		.pnum = UINT64_C(0x0002000300040005),
1659 		.idx = 0,
1660 		.lpv = 4,
1661 	},
1662 	};
1663 
1664 	union {
1665 		uint16_t u16[FWDSTEP + 1];
1666 		uint64_t u64;
1667 	} *pnum = (void *)pn;
1668 
1669 	int32_t v;
1670 
1671 	dp1 = _mm_cmpeq_epi16(dp1, dp2);
1672 	dp1 = _mm_unpacklo_epi16(dp1, dp1);
1673 	v = _mm_movemask_ps((__m128)dp1);
1674 
1675 	/* update last port counter. */
1676 	lp[0] += gptbl[v].lpv;
1677 
1678 	/* if dest port value has changed. */
1679 	if (v != GRPMSK) {
1680 		pnum->u64 = gptbl[v].pnum;
1681 		pnum->u16[FWDSTEP] = 1;
1682 		lp = pnum->u16 + gptbl[v].idx;
1683 	}
1684 
1685 	return lp;
1686 }
1687 
1688 #endif /* APP_LOOKUP_METHOD */
1689 
1690 static void
1691 process_burst(struct rte_mbuf *pkts_burst[MAX_PKT_BURST], int nb_rx,
1692 		uint16_t portid)
1693 {
1694 
1695 	int j;
1696 
1697 #if ((APP_LOOKUP_METHOD == APP_LOOKUP_LPM) && \
1698 	(ENABLE_MULTI_BUFFER_OPTIMIZE == 1))
1699 	int32_t k;
1700 	uint16_t dlp;
1701 	uint16_t *lp;
1702 	uint16_t dst_port[MAX_PKT_BURST];
1703 	__m128i dip[MAX_PKT_BURST / FWDSTEP];
1704 	uint32_t ipv4_flag[MAX_PKT_BURST / FWDSTEP];
1705 	uint16_t pnum[MAX_PKT_BURST + 1];
1706 #endif
1707 
1708 
1709 #if (ENABLE_MULTI_BUFFER_OPTIMIZE == 1)
1710 #if (APP_LOOKUP_METHOD == APP_LOOKUP_EXACT_MATCH)
1711 	{
1712 		/*
1713 		 * Send nb_rx - nb_rx%8 packets
1714 		 * in groups of 8.
1715 		 */
1716 		int32_t n = RTE_ALIGN_FLOOR(nb_rx, 8);
1717 
1718 		for (j = 0; j < n; j += 8) {
1719 			uint32_t pkt_type =
1720 				pkts_burst[j]->packet_type &
1721 				pkts_burst[j+1]->packet_type &
1722 				pkts_burst[j+2]->packet_type &
1723 				pkts_burst[j+3]->packet_type &
1724 				pkts_burst[j+4]->packet_type &
1725 				pkts_burst[j+5]->packet_type &
1726 				pkts_burst[j+6]->packet_type &
1727 				pkts_burst[j+7]->packet_type;
1728 			if (pkt_type & RTE_PTYPE_L3_IPV4) {
1729 				simple_ipv4_fwd_8pkts(&pkts_burst[j], portid);
1730 			} else if (pkt_type &
1731 				RTE_PTYPE_L3_IPV6) {
1732 				simple_ipv6_fwd_8pkts(&pkts_burst[j], portid);
1733 			} else {
1734 				l3fwd_simple_forward(pkts_burst[j], portid);
1735 				l3fwd_simple_forward(pkts_burst[j+1], portid);
1736 				l3fwd_simple_forward(pkts_burst[j+2], portid);
1737 				l3fwd_simple_forward(pkts_burst[j+3], portid);
1738 				l3fwd_simple_forward(pkts_burst[j+4], portid);
1739 				l3fwd_simple_forward(pkts_burst[j+5], portid);
1740 				l3fwd_simple_forward(pkts_burst[j+6], portid);
1741 				l3fwd_simple_forward(pkts_burst[j+7], portid);
1742 			}
1743 		}
1744 		for (; j < nb_rx ; j++)
1745 			l3fwd_simple_forward(pkts_burst[j], portid);
1746 	}
1747 #elif (APP_LOOKUP_METHOD == APP_LOOKUP_LPM)
1748 
1749 	k = RTE_ALIGN_FLOOR(nb_rx, FWDSTEP);
1750 	for (j = 0; j != k; j += FWDSTEP)
1751 		processx4_step1(&pkts_burst[j], &dip[j / FWDSTEP],
1752 				&ipv4_flag[j / FWDSTEP]);
1753 
1754 	k = RTE_ALIGN_FLOOR(nb_rx, FWDSTEP);
1755 	for (j = 0; j != k; j += FWDSTEP)
1756 		processx4_step2(dip[j / FWDSTEP], ipv4_flag[j / FWDSTEP],
1757 				portid, &pkts_burst[j], &dst_port[j]);
1758 
1759 	/*
1760 	 * Finish packet processing and group consecutive
1761 	 * packets with the same destination port.
1762 	 */
1763 	k = RTE_ALIGN_FLOOR(nb_rx, FWDSTEP);
1764 	if (k != 0) {
1765 		__m128i dp1, dp2;
1766 
1767 		lp = pnum;
1768 		lp[0] = 1;
1769 
1770 		processx4_step3(pkts_burst, dst_port);
1771 
1772 		/* dp1: <d[0], d[1], d[2], d[3], ... > */
1773 		dp1 = _mm_loadu_si128((__m128i *)dst_port);
1774 
1775 		for (j = FWDSTEP; j != k; j += FWDSTEP) {
1776 			processx4_step3(&pkts_burst[j], &dst_port[j]);
1777 
1778 			/*
1779 			 * dp2:
1780 			 * <d[j-3], d[j-2], d[j-1], d[j], ... >
1781 			 */
1782 			dp2 = _mm_loadu_si128(
1783 					(__m128i *)&dst_port[j - FWDSTEP + 1]);
1784 			lp  = port_groupx4(&pnum[j - FWDSTEP], lp, dp1, dp2);
1785 
1786 			/*
1787 			 * dp1:
1788 			 * <d[j], d[j+1], d[j+2], d[j+3], ... >
1789 			 */
1790 			dp1 = _mm_srli_si128(dp2, (FWDSTEP - 1) *
1791 					sizeof(dst_port[0]));
1792 		}
1793 
1794 		/*
1795 		 * dp2: <d[j-3], d[j-2], d[j-1], d[j-1], ... >
1796 		 */
1797 		dp2 = _mm_shufflelo_epi16(dp1, 0xf9);
1798 		lp  = port_groupx4(&pnum[j - FWDSTEP], lp, dp1, dp2);
1799 
1800 		/*
1801 		 * remove values added by the last repeated
1802 		 * dst port.
1803 		 */
1804 		lp[0]--;
1805 		dlp = dst_port[j - 1];
1806 	} else {
1807 		/* set dlp and lp to the never used values. */
1808 		dlp = BAD_PORT - 1;
1809 		lp = pnum + MAX_PKT_BURST;
1810 	}
1811 
1812 	/* Process up to last 3 packets one by one. */
1813 	switch (nb_rx % FWDSTEP) {
1814 	case 3:
1815 		process_packet(pkts_burst[j], dst_port + j, portid);
1816 		GROUP_PORT_STEP(dlp, dst_port, lp, pnum, j);
1817 		j++;
1818 		/* fall-through */
1819 	case 2:
1820 		process_packet(pkts_burst[j], dst_port + j, portid);
1821 		GROUP_PORT_STEP(dlp, dst_port, lp, pnum, j);
1822 		j++;
1823 		/* fall-through */
1824 	case 1:
1825 		process_packet(pkts_burst[j], dst_port + j, portid);
1826 		GROUP_PORT_STEP(dlp, dst_port, lp, pnum, j);
1827 		j++;
1828 	}
1829 
1830 	/*
1831 	 * Send packets out, through destination port.
1832 	 * Consecuteve pacekts with the same destination port
1833 	 * are already grouped together.
1834 	 * If destination port for the packet equals BAD_PORT,
1835 	 * then free the packet without sending it out.
1836 	 */
1837 	for (j = 0; j < nb_rx; j += k) {
1838 
1839 		int32_t m;
1840 		uint16_t pn;
1841 
1842 		pn = dst_port[j];
1843 		k = pnum[j];
1844 
1845 		if (likely(pn != BAD_PORT))
1846 			send_packetsx4(pn, pkts_burst + j, k);
1847 		else
1848 			for (m = j; m != j + k; m++)
1849 				rte_pktmbuf_free(pkts_burst[m]);
1850 
1851 	}
1852 
1853 #endif /* APP_LOOKUP_METHOD */
1854 #else /* ENABLE_MULTI_BUFFER_OPTIMIZE == 0 */
1855 
1856 	/* Prefetch first packets */
1857 	for (j = 0; j < PREFETCH_OFFSET && j < nb_rx; j++)
1858 		rte_prefetch0(rte_pktmbuf_mtod(pkts_burst[j], void *));
1859 
1860 	/* Prefetch and forward already prefetched packets */
1861 	for (j = 0; j < (nb_rx - PREFETCH_OFFSET); j++) {
1862 		rte_prefetch0(rte_pktmbuf_mtod(pkts_burst[
1863 				j + PREFETCH_OFFSET], void *));
1864 		l3fwd_simple_forward(pkts_burst[j], portid);
1865 	}
1866 
1867 	/* Forward remaining prefetched packets */
1868 	for (; j < nb_rx; j++)
1869 		l3fwd_simple_forward(pkts_burst[j], portid);
1870 
1871 #endif /* ENABLE_MULTI_BUFFER_OPTIMIZE */
1872 
1873 }
1874 
1875 #if (APP_CPU_LOAD > 0)
1876 
1877 /*
1878  * CPU-load stats collector
1879  */
1880 static int __attribute__((noreturn))
1881 cpu_load_collector(__rte_unused void *arg) {
1882 	unsigned i, j, k;
1883 	uint64_t hits;
1884 	uint64_t prev_tsc, diff_tsc, cur_tsc;
1885 	uint64_t total[MAX_CPU] = { 0 };
1886 	unsigned min_cpu = MAX_CPU;
1887 	unsigned max_cpu = 0;
1888 	unsigned cpu_id;
1889 	int busy_total = 0;
1890 	int busy_flag = 0;
1891 
1892 	unsigned int n_thread_per_cpu[MAX_CPU] = { 0 };
1893 	struct thread_conf *thread_per_cpu[MAX_CPU][MAX_THREAD];
1894 
1895 	struct thread_conf *thread_conf;
1896 
1897 	const uint64_t interval_tsc = (rte_get_tsc_hz() + US_PER_S - 1) /
1898 		US_PER_S * CPU_LOAD_TIMEOUT_US;
1899 
1900 	prev_tsc = 0;
1901 	/*
1902 	 * Wait for all threads
1903 	 */
1904 
1905 	printf("Waiting for %d rx threads and %d tx threads\n", n_rx_thread,
1906 			n_tx_thread);
1907 
1908 	while (rte_atomic16_read(&rx_counter) < n_rx_thread)
1909 		rte_pause();
1910 
1911 	while (rte_atomic16_read(&tx_counter) < n_tx_thread)
1912 		rte_pause();
1913 
1914 	for (i = 0; i < n_rx_thread; i++) {
1915 
1916 		thread_conf = &rx_thread[i].conf;
1917 		cpu_id = thread_conf->cpu_id;
1918 		thread_per_cpu[cpu_id][n_thread_per_cpu[cpu_id]++] = thread_conf;
1919 
1920 		if (cpu_id > max_cpu)
1921 			max_cpu = cpu_id;
1922 		if (cpu_id < min_cpu)
1923 			min_cpu = cpu_id;
1924 	}
1925 	for (i = 0; i < n_tx_thread; i++) {
1926 
1927 		thread_conf = &tx_thread[i].conf;
1928 		cpu_id = thread_conf->cpu_id;
1929 		thread_per_cpu[cpu_id][n_thread_per_cpu[cpu_id]++] = thread_conf;
1930 
1931 		if (thread_conf->cpu_id > max_cpu)
1932 			max_cpu = thread_conf->cpu_id;
1933 		if (thread_conf->cpu_id < min_cpu)
1934 			min_cpu = thread_conf->cpu_id;
1935 	}
1936 
1937 	while (1) {
1938 
1939 		cpu_load.counter++;
1940 		for (i = min_cpu; i <= max_cpu; i++) {
1941 			for (j = 0; j < MAX_CPU_COUNTER; j++) {
1942 				for (k = 0; k < n_thread_per_cpu[i]; k++)
1943 					if (thread_per_cpu[i][k]->busy[j]) {
1944 						busy_flag = 1;
1945 						break;
1946 					}
1947 				if (busy_flag) {
1948 					cpu_load.hits[j][i]++;
1949 					busy_total = 1;
1950 					busy_flag = 0;
1951 				}
1952 			}
1953 
1954 			if (busy_total) {
1955 				total[i]++;
1956 				busy_total = 0;
1957 			}
1958 		}
1959 
1960 		cur_tsc = rte_rdtsc();
1961 
1962 		diff_tsc = cur_tsc - prev_tsc;
1963 		if (unlikely(diff_tsc > interval_tsc)) {
1964 
1965 			printf("\033c");
1966 
1967 			printf("Cpu usage for %d rx threads and %d tx threads:\n\n",
1968 					n_rx_thread, n_tx_thread);
1969 
1970 			printf("cpu#     proc%%  poll%%  overhead%%\n\n");
1971 
1972 			for (i = min_cpu; i <= max_cpu; i++) {
1973 				hits = 0;
1974 				printf("CPU %d:", i);
1975 				for (j = 0; j < MAX_CPU_COUNTER; j++) {
1976 					printf("%7" PRIu64 "",
1977 							cpu_load.hits[j][i] * 100 / cpu_load.counter);
1978 					hits += cpu_load.hits[j][i];
1979 					cpu_load.hits[j][i] = 0;
1980 				}
1981 				printf("%7" PRIu64 "\n",
1982 						100 - total[i] * 100 / cpu_load.counter);
1983 				total[i] = 0;
1984 			}
1985 			cpu_load.counter = 0;
1986 
1987 			prev_tsc = cur_tsc;
1988 		}
1989 
1990 	}
1991 }
1992 #endif /* APP_CPU_LOAD */
1993 
1994 /*
1995  * Null processing lthread loop
1996  *
1997  * This loop is used to start empty scheduler on lcore.
1998  */
1999 static void *
2000 lthread_null(__rte_unused void *args)
2001 {
2002 	int lcore_id = rte_lcore_id();
2003 
2004 	RTE_LOG(INFO, L3FWD, "Starting scheduler on lcore %d.\n", lcore_id);
2005 	lthread_exit(NULL);
2006 	return NULL;
2007 }
2008 
2009 /* main processing loop */
2010 static void *
2011 lthread_tx_per_ring(void *dummy)
2012 {
2013 	int nb_rx;
2014 	uint16_t portid;
2015 	struct rte_ring *ring;
2016 	struct thread_tx_conf *tx_conf;
2017 	struct rte_mbuf *pkts_burst[MAX_PKT_BURST];
2018 	struct lthread_cond *ready;
2019 
2020 	tx_conf = (struct thread_tx_conf *)dummy;
2021 	ring = tx_conf->ring;
2022 	ready = *tx_conf->ready;
2023 
2024 	lthread_set_data((void *)tx_conf);
2025 
2026 	/*
2027 	 * Move this lthread to lcore
2028 	 */
2029 	lthread_set_affinity(tx_conf->conf.lcore_id);
2030 
2031 	RTE_LOG(INFO, L3FWD, "entering main tx loop on lcore %u\n", rte_lcore_id());
2032 
2033 	nb_rx = 0;
2034 	rte_atomic16_inc(&tx_counter);
2035 	while (1) {
2036 
2037 		/*
2038 		 * Read packet from ring
2039 		 */
2040 		SET_CPU_BUSY(tx_conf, CPU_POLL);
2041 		nb_rx = rte_ring_sc_dequeue_burst(ring, (void **)pkts_burst,
2042 				MAX_PKT_BURST, NULL);
2043 		SET_CPU_IDLE(tx_conf, CPU_POLL);
2044 
2045 		if (nb_rx > 0) {
2046 			SET_CPU_BUSY(tx_conf, CPU_PROCESS);
2047 			portid = pkts_burst[0]->port;
2048 			process_burst(pkts_burst, nb_rx, portid);
2049 			SET_CPU_IDLE(tx_conf, CPU_PROCESS);
2050 			lthread_yield();
2051 		} else
2052 			lthread_cond_wait(ready, 0);
2053 
2054 	}
2055 	return NULL;
2056 }
2057 
2058 /*
2059  * Main tx-lthreads spawner lthread.
2060  *
2061  * This lthread is used to spawn one new lthread per ring from producers.
2062  *
2063  */
2064 static void *
2065 lthread_tx(void *args)
2066 {
2067 	struct lthread *lt;
2068 
2069 	unsigned lcore_id;
2070 	uint16_t portid;
2071 	struct thread_tx_conf *tx_conf;
2072 
2073 	tx_conf = (struct thread_tx_conf *)args;
2074 	lthread_set_data((void *)tx_conf);
2075 
2076 	/*
2077 	 * Move this lthread to the selected lcore
2078 	 */
2079 	lthread_set_affinity(tx_conf->conf.lcore_id);
2080 
2081 	/*
2082 	 * Spawn tx readers (one per input ring)
2083 	 */
2084 	lthread_create(&lt, tx_conf->conf.lcore_id, lthread_tx_per_ring,
2085 			(void *)tx_conf);
2086 
2087 	lcore_id = rte_lcore_id();
2088 
2089 	RTE_LOG(INFO, L3FWD, "Entering Tx main loop on lcore %u\n", lcore_id);
2090 
2091 	tx_conf->conf.cpu_id = sched_getcpu();
2092 	while (1) {
2093 
2094 		lthread_sleep(BURST_TX_DRAIN_US * 1000);
2095 
2096 		/*
2097 		 * TX burst queue drain
2098 		 */
2099 		for (portid = 0; portid < RTE_MAX_ETHPORTS; portid++) {
2100 			if (tx_conf->tx_mbufs[portid].len == 0)
2101 				continue;
2102 			SET_CPU_BUSY(tx_conf, CPU_PROCESS);
2103 			send_burst(tx_conf, tx_conf->tx_mbufs[portid].len, portid);
2104 			SET_CPU_IDLE(tx_conf, CPU_PROCESS);
2105 			tx_conf->tx_mbufs[portid].len = 0;
2106 		}
2107 
2108 	}
2109 	return NULL;
2110 }
2111 
2112 static void *
2113 lthread_rx(void *dummy)
2114 {
2115 	int ret;
2116 	uint16_t nb_rx;
2117 	int i;
2118 	uint16_t portid;
2119 	uint8_t queueid;
2120 	int worker_id;
2121 	int len[RTE_MAX_LCORE] = { 0 };
2122 	int old_len, new_len;
2123 	struct rte_mbuf *pkts_burst[MAX_PKT_BURST];
2124 	struct thread_rx_conf *rx_conf;
2125 
2126 	rx_conf = (struct thread_rx_conf *)dummy;
2127 	lthread_set_data((void *)rx_conf);
2128 
2129 	/*
2130 	 * Move this lthread to lcore
2131 	 */
2132 	lthread_set_affinity(rx_conf->conf.lcore_id);
2133 
2134 	if (rx_conf->n_rx_queue == 0) {
2135 		RTE_LOG(INFO, L3FWD, "lcore %u has nothing to do\n", rte_lcore_id());
2136 		return NULL;
2137 	}
2138 
2139 	RTE_LOG(INFO, L3FWD, "Entering main Rx loop on lcore %u\n", rte_lcore_id());
2140 
2141 	for (i = 0; i < rx_conf->n_rx_queue; i++) {
2142 
2143 		portid = rx_conf->rx_queue_list[i].port_id;
2144 		queueid = rx_conf->rx_queue_list[i].queue_id;
2145 		RTE_LOG(INFO, L3FWD,
2146 			" -- lcoreid=%u portid=%u rxqueueid=%hhu\n",
2147 				rte_lcore_id(), portid, queueid);
2148 	}
2149 
2150 	/*
2151 	 * Init all condition variables (one per rx thread)
2152 	 */
2153 	for (i = 0; i < rx_conf->n_rx_queue; i++)
2154 		lthread_cond_init(NULL, &rx_conf->ready[i], NULL);
2155 
2156 	worker_id = 0;
2157 
2158 	rx_conf->conf.cpu_id = sched_getcpu();
2159 	rte_atomic16_inc(&rx_counter);
2160 	while (1) {
2161 
2162 		/*
2163 		 * Read packet from RX queues
2164 		 */
2165 		for (i = 0; i < rx_conf->n_rx_queue; ++i) {
2166 			portid = rx_conf->rx_queue_list[i].port_id;
2167 			queueid = rx_conf->rx_queue_list[i].queue_id;
2168 
2169 			SET_CPU_BUSY(rx_conf, CPU_POLL);
2170 			nb_rx = rte_eth_rx_burst(portid, queueid, pkts_burst,
2171 				MAX_PKT_BURST);
2172 			SET_CPU_IDLE(rx_conf, CPU_POLL);
2173 
2174 			if (nb_rx != 0) {
2175 				worker_id = (worker_id + 1) % rx_conf->n_ring;
2176 				old_len = len[worker_id];
2177 
2178 				SET_CPU_BUSY(rx_conf, CPU_PROCESS);
2179 				ret = rte_ring_sp_enqueue_burst(
2180 						rx_conf->ring[worker_id],
2181 						(void **) pkts_burst,
2182 						nb_rx, NULL);
2183 
2184 				new_len = old_len + ret;
2185 
2186 				if (new_len >= BURST_SIZE) {
2187 					lthread_cond_signal(rx_conf->ready[worker_id]);
2188 					new_len = 0;
2189 				}
2190 
2191 				len[worker_id] = new_len;
2192 
2193 				if (unlikely(ret < nb_rx)) {
2194 					uint32_t k;
2195 
2196 					for (k = ret; k < nb_rx; k++) {
2197 						struct rte_mbuf *m = pkts_burst[k];
2198 
2199 						rte_pktmbuf_free(m);
2200 					}
2201 				}
2202 				SET_CPU_IDLE(rx_conf, CPU_PROCESS);
2203 			}
2204 
2205 			lthread_yield();
2206 		}
2207 	}
2208 	return NULL;
2209 }
2210 
2211 /*
2212  * Start scheduler with initial lthread on lcore
2213  *
2214  * This lthread loop spawns all rx and tx lthreads on master lcore
2215  */
2216 
2217 static void *
2218 lthread_spawner(__rte_unused void *arg)
2219 {
2220 	struct lthread *lt[MAX_THREAD];
2221 	int i;
2222 	int n_thread = 0;
2223 
2224 	printf("Entering lthread_spawner\n");
2225 
2226 	/*
2227 	 * Create producers (rx threads) on default lcore
2228 	 */
2229 	for (i = 0; i < n_rx_thread; i++) {
2230 		rx_thread[i].conf.thread_id = i;
2231 		lthread_create(&lt[n_thread], -1, lthread_rx,
2232 				(void *)&rx_thread[i]);
2233 		n_thread++;
2234 	}
2235 
2236 	/*
2237 	 * Wait for all producers. Until some producers can be started on the same
2238 	 * scheduler as this lthread, yielding is required to let them to run and
2239 	 * prevent deadlock here.
2240 	 */
2241 	while (rte_atomic16_read(&rx_counter) < n_rx_thread)
2242 		lthread_sleep(100000);
2243 
2244 	/*
2245 	 * Create consumers (tx threads) on default lcore_id
2246 	 */
2247 	for (i = 0; i < n_tx_thread; i++) {
2248 		tx_thread[i].conf.thread_id = i;
2249 		lthread_create(&lt[n_thread], -1, lthread_tx,
2250 				(void *)&tx_thread[i]);
2251 		n_thread++;
2252 	}
2253 
2254 	/*
2255 	 * Wait for all threads finished
2256 	 */
2257 	for (i = 0; i < n_thread; i++)
2258 		lthread_join(lt[i], NULL);
2259 
2260 	return NULL;
2261 }
2262 
2263 /*
2264  * Start master scheduler with initial lthread spawning rx and tx lthreads
2265  * (main_lthread_master).
2266  */
2267 static int
2268 lthread_master_spawner(__rte_unused void *arg) {
2269 	struct lthread *lt;
2270 	int lcore_id = rte_lcore_id();
2271 
2272 	RTE_PER_LCORE(lcore_conf) = &lcore_conf[lcore_id];
2273 	lthread_create(&lt, -1, lthread_spawner, NULL);
2274 	lthread_run();
2275 
2276 	return 0;
2277 }
2278 
2279 /*
2280  * Start scheduler on lcore.
2281  */
2282 static int
2283 sched_spawner(__rte_unused void *arg) {
2284 	struct lthread *lt;
2285 	int lcore_id = rte_lcore_id();
2286 
2287 #if (APP_CPU_LOAD)
2288 	if (lcore_id == cpu_load_lcore_id) {
2289 		cpu_load_collector(arg);
2290 		return 0;
2291 	}
2292 #endif /* APP_CPU_LOAD */
2293 
2294 	RTE_PER_LCORE(lcore_conf) = &lcore_conf[lcore_id];
2295 	lthread_create(&lt, -1, lthread_null, NULL);
2296 	lthread_run();
2297 
2298 	return 0;
2299 }
2300 
2301 /* main processing loop */
2302 static int __attribute__((noreturn))
2303 pthread_tx(void *dummy)
2304 {
2305 	struct rte_mbuf *pkts_burst[MAX_PKT_BURST];
2306 	uint64_t prev_tsc, diff_tsc, cur_tsc;
2307 	int nb_rx;
2308 	uint16_t portid;
2309 	struct thread_tx_conf *tx_conf;
2310 
2311 	const uint64_t drain_tsc = (rte_get_tsc_hz() + US_PER_S - 1) /
2312 		US_PER_S * BURST_TX_DRAIN_US;
2313 
2314 	prev_tsc = 0;
2315 
2316 	tx_conf = (struct thread_tx_conf *)dummy;
2317 
2318 	RTE_LOG(INFO, L3FWD, "Entering main Tx loop on lcore %u\n", rte_lcore_id());
2319 
2320 	tx_conf->conf.cpu_id = sched_getcpu();
2321 	rte_atomic16_inc(&tx_counter);
2322 	while (1) {
2323 
2324 		cur_tsc = rte_rdtsc();
2325 
2326 		/*
2327 		 * TX burst queue drain
2328 		 */
2329 		diff_tsc = cur_tsc - prev_tsc;
2330 		if (unlikely(diff_tsc > drain_tsc)) {
2331 
2332 			/*
2333 			 * This could be optimized (use queueid instead of
2334 			 * portid), but it is not called so often
2335 			 */
2336 			SET_CPU_BUSY(tx_conf, CPU_PROCESS);
2337 			for (portid = 0; portid < RTE_MAX_ETHPORTS; portid++) {
2338 				if (tx_conf->tx_mbufs[portid].len == 0)
2339 					continue;
2340 				send_burst(tx_conf, tx_conf->tx_mbufs[portid].len, portid);
2341 				tx_conf->tx_mbufs[portid].len = 0;
2342 			}
2343 			SET_CPU_IDLE(tx_conf, CPU_PROCESS);
2344 
2345 			prev_tsc = cur_tsc;
2346 		}
2347 
2348 		/*
2349 		 * Read packet from ring
2350 		 */
2351 		SET_CPU_BUSY(tx_conf, CPU_POLL);
2352 		nb_rx = rte_ring_sc_dequeue_burst(tx_conf->ring,
2353 				(void **)pkts_burst, MAX_PKT_BURST, NULL);
2354 		SET_CPU_IDLE(tx_conf, CPU_POLL);
2355 
2356 		if (unlikely(nb_rx == 0)) {
2357 			sched_yield();
2358 			continue;
2359 		}
2360 
2361 		SET_CPU_BUSY(tx_conf, CPU_PROCESS);
2362 		portid = pkts_burst[0]->port;
2363 		process_burst(pkts_burst, nb_rx, portid);
2364 		SET_CPU_IDLE(tx_conf, CPU_PROCESS);
2365 
2366 	}
2367 }
2368 
2369 static int
2370 pthread_rx(void *dummy)
2371 {
2372 	int i;
2373 	int worker_id;
2374 	uint32_t n;
2375 	uint32_t nb_rx;
2376 	unsigned lcore_id;
2377 	uint8_t queueid;
2378 	uint16_t portid;
2379 	struct rte_mbuf *pkts_burst[MAX_PKT_BURST];
2380 
2381 	struct thread_rx_conf *rx_conf;
2382 
2383 	lcore_id = rte_lcore_id();
2384 	rx_conf = (struct thread_rx_conf *)dummy;
2385 
2386 	if (rx_conf->n_rx_queue == 0) {
2387 		RTE_LOG(INFO, L3FWD, "lcore %u has nothing to do\n", lcore_id);
2388 		return 0;
2389 	}
2390 
2391 	RTE_LOG(INFO, L3FWD, "entering main rx loop on lcore %u\n", lcore_id);
2392 
2393 	for (i = 0; i < rx_conf->n_rx_queue; i++) {
2394 
2395 		portid = rx_conf->rx_queue_list[i].port_id;
2396 		queueid = rx_conf->rx_queue_list[i].queue_id;
2397 		RTE_LOG(INFO, L3FWD,
2398 			" -- lcoreid=%u portid=%u rxqueueid=%hhu\n",
2399 				lcore_id, portid, queueid);
2400 	}
2401 
2402 	worker_id = 0;
2403 	rx_conf->conf.cpu_id = sched_getcpu();
2404 	rte_atomic16_inc(&rx_counter);
2405 	while (1) {
2406 
2407 		/*
2408 		 * Read packet from RX queues
2409 		 */
2410 		for (i = 0; i < rx_conf->n_rx_queue; ++i) {
2411 			portid = rx_conf->rx_queue_list[i].port_id;
2412 			queueid = rx_conf->rx_queue_list[i].queue_id;
2413 
2414 			SET_CPU_BUSY(rx_conf, CPU_POLL);
2415 			nb_rx = rte_eth_rx_burst(portid, queueid, pkts_burst,
2416 				MAX_PKT_BURST);
2417 			SET_CPU_IDLE(rx_conf, CPU_POLL);
2418 
2419 			if (nb_rx == 0) {
2420 				sched_yield();
2421 				continue;
2422 			}
2423 
2424 			SET_CPU_BUSY(rx_conf, CPU_PROCESS);
2425 			worker_id = (worker_id + 1) % rx_conf->n_ring;
2426 			n = rte_ring_sp_enqueue_burst(rx_conf->ring[worker_id],
2427 					(void **)pkts_burst, nb_rx, NULL);
2428 
2429 			if (unlikely(n != nb_rx)) {
2430 				uint32_t k;
2431 
2432 				for (k = n; k < nb_rx; k++) {
2433 					struct rte_mbuf *m = pkts_burst[k];
2434 
2435 					rte_pktmbuf_free(m);
2436 				}
2437 			}
2438 
2439 			SET_CPU_IDLE(rx_conf, CPU_PROCESS);
2440 
2441 		}
2442 	}
2443 }
2444 
2445 /*
2446  * P-Thread spawner.
2447  */
2448 static int
2449 pthread_run(__rte_unused void *arg) {
2450 	int lcore_id = rte_lcore_id();
2451 	int i;
2452 
2453 	for (i = 0; i < n_rx_thread; i++)
2454 		if (rx_thread[i].conf.lcore_id == lcore_id) {
2455 			printf("Start rx thread on %d...\n", lcore_id);
2456 			RTE_PER_LCORE(lcore_conf) = &lcore_conf[lcore_id];
2457 			RTE_PER_LCORE(lcore_conf)->data = (void *)&rx_thread[i];
2458 			pthread_rx((void *)&rx_thread[i]);
2459 			return 0;
2460 		}
2461 
2462 	for (i = 0; i < n_tx_thread; i++)
2463 		if (tx_thread[i].conf.lcore_id == lcore_id) {
2464 			printf("Start tx thread on %d...\n", lcore_id);
2465 			RTE_PER_LCORE(lcore_conf) = &lcore_conf[lcore_id];
2466 			RTE_PER_LCORE(lcore_conf)->data = (void *)&tx_thread[i];
2467 			pthread_tx((void *)&tx_thread[i]);
2468 			return 0;
2469 		}
2470 
2471 #if (APP_CPU_LOAD)
2472 	if (lcore_id == cpu_load_lcore_id)
2473 		cpu_load_collector(arg);
2474 #endif /* APP_CPU_LOAD */
2475 
2476 	return 0;
2477 }
2478 
2479 static int
2480 check_lcore_params(void)
2481 {
2482 	uint8_t queue, lcore;
2483 	uint16_t i;
2484 	int socketid;
2485 
2486 	for (i = 0; i < nb_rx_thread_params; ++i) {
2487 		queue = rx_thread_params[i].queue_id;
2488 		if (queue >= MAX_RX_QUEUE_PER_PORT) {
2489 			printf("invalid queue number: %hhu\n", queue);
2490 			return -1;
2491 		}
2492 		lcore = rx_thread_params[i].lcore_id;
2493 		if (!rte_lcore_is_enabled(lcore)) {
2494 			printf("error: lcore %hhu is not enabled in lcore mask\n", lcore);
2495 			return -1;
2496 		}
2497 		socketid = rte_lcore_to_socket_id(lcore);
2498 		if ((socketid != 0) && (numa_on == 0))
2499 			printf("warning: lcore %hhu is on socket %d with numa off\n",
2500 				lcore, socketid);
2501 	}
2502 	return 0;
2503 }
2504 
2505 static int
2506 check_port_config(void)
2507 {
2508 	unsigned portid;
2509 	uint16_t i;
2510 
2511 	for (i = 0; i < nb_rx_thread_params; ++i) {
2512 		portid = rx_thread_params[i].port_id;
2513 		if ((enabled_port_mask & (1 << portid)) == 0) {
2514 			printf("port %u is not enabled in port mask\n", portid);
2515 			return -1;
2516 		}
2517 		if (!rte_eth_dev_is_valid_port(portid)) {
2518 			printf("port %u is not present on the board\n", portid);
2519 			return -1;
2520 		}
2521 	}
2522 	return 0;
2523 }
2524 
2525 static uint8_t
2526 get_port_n_rx_queues(const uint16_t port)
2527 {
2528 	int queue = -1;
2529 	uint16_t i;
2530 
2531 	for (i = 0; i < nb_rx_thread_params; ++i)
2532 		if (rx_thread_params[i].port_id == port &&
2533 				rx_thread_params[i].queue_id > queue)
2534 			queue = rx_thread_params[i].queue_id;
2535 
2536 	return (uint8_t)(++queue);
2537 }
2538 
2539 static int
2540 init_rx_rings(void)
2541 {
2542 	unsigned socket_io;
2543 	struct thread_rx_conf *rx_conf;
2544 	struct thread_tx_conf *tx_conf;
2545 	unsigned rx_thread_id, tx_thread_id;
2546 	char name[256];
2547 	struct rte_ring *ring = NULL;
2548 
2549 	for (tx_thread_id = 0; tx_thread_id < n_tx_thread; tx_thread_id++) {
2550 
2551 		tx_conf = &tx_thread[tx_thread_id];
2552 
2553 		printf("Connecting tx-thread %d with rx-thread %d\n", tx_thread_id,
2554 				tx_conf->conf.thread_id);
2555 
2556 		rx_thread_id = tx_conf->conf.thread_id;
2557 		if (rx_thread_id > n_tx_thread) {
2558 			printf("connection from tx-thread %u to rx-thread %u fails "
2559 					"(rx-thread not defined)\n", tx_thread_id, rx_thread_id);
2560 			return -1;
2561 		}
2562 
2563 		rx_conf = &rx_thread[rx_thread_id];
2564 		socket_io = rte_lcore_to_socket_id(rx_conf->conf.lcore_id);
2565 
2566 		snprintf(name, sizeof(name), "app_ring_s%u_rx%u_tx%u",
2567 				socket_io, rx_thread_id, tx_thread_id);
2568 
2569 		ring = rte_ring_create(name, 1024 * 4, socket_io,
2570 				RING_F_SP_ENQ | RING_F_SC_DEQ);
2571 
2572 		if (ring == NULL) {
2573 			rte_panic("Cannot create ring to connect rx-thread %u "
2574 					"with tx-thread %u\n", rx_thread_id, tx_thread_id);
2575 		}
2576 
2577 		rx_conf->ring[rx_conf->n_ring] = ring;
2578 
2579 		tx_conf->ring = ring;
2580 		tx_conf->ready = &rx_conf->ready[rx_conf->n_ring];
2581 
2582 		rx_conf->n_ring++;
2583 	}
2584 	return 0;
2585 }
2586 
2587 static int
2588 init_rx_queues(void)
2589 {
2590 	uint16_t i, nb_rx_queue;
2591 	uint8_t thread;
2592 
2593 	n_rx_thread = 0;
2594 
2595 	for (i = 0; i < nb_rx_thread_params; ++i) {
2596 		thread = rx_thread_params[i].thread_id;
2597 		nb_rx_queue = rx_thread[thread].n_rx_queue;
2598 
2599 		if (nb_rx_queue >= MAX_RX_QUEUE_PER_LCORE) {
2600 			printf("error: too many queues (%u) for thread: %u\n",
2601 				(unsigned)nb_rx_queue + 1, (unsigned)thread);
2602 			return -1;
2603 		}
2604 
2605 		rx_thread[thread].conf.thread_id = thread;
2606 		rx_thread[thread].conf.lcore_id = rx_thread_params[i].lcore_id;
2607 		rx_thread[thread].rx_queue_list[nb_rx_queue].port_id =
2608 			rx_thread_params[i].port_id;
2609 		rx_thread[thread].rx_queue_list[nb_rx_queue].queue_id =
2610 			rx_thread_params[i].queue_id;
2611 		rx_thread[thread].n_rx_queue++;
2612 
2613 		if (thread >= n_rx_thread)
2614 			n_rx_thread = thread + 1;
2615 
2616 	}
2617 	return 0;
2618 }
2619 
2620 static int
2621 init_tx_threads(void)
2622 {
2623 	int i;
2624 
2625 	n_tx_thread = 0;
2626 	for (i = 0; i < nb_tx_thread_params; ++i) {
2627 		tx_thread[n_tx_thread].conf.thread_id = tx_thread_params[i].thread_id;
2628 		tx_thread[n_tx_thread].conf.lcore_id = tx_thread_params[i].lcore_id;
2629 		n_tx_thread++;
2630 	}
2631 	return 0;
2632 }
2633 
2634 /* display usage */
2635 static void
2636 print_usage(const char *prgname)
2637 {
2638 	printf("%s [EAL options] -- -p PORTMASK -P"
2639 		"  [--rx (port,queue,lcore,thread)[,(port,queue,lcore,thread]]"
2640 		"  [--tx (lcore,thread)[,(lcore,thread]]"
2641 		"  [--enable-jumbo [--max-pkt-len PKTLEN]]\n"
2642 		"  [--parse-ptype]\n\n"
2643 		"  -p PORTMASK: hexadecimal bitmask of ports to configure\n"
2644 		"  -P : enable promiscuous mode\n"
2645 		"  --rx (port,queue,lcore,thread): rx queues configuration\n"
2646 		"  --tx (lcore,thread): tx threads configuration\n"
2647 		"  --stat-lcore LCORE: use lcore for stat collector\n"
2648 		"  --eth-dest=X,MM:MM:MM:MM:MM:MM: optional, ethernet destination for port X\n"
2649 		"  --no-numa: optional, disable numa awareness\n"
2650 		"  --ipv6: optional, specify it if running ipv6 packets\n"
2651 		"  --enable-jumbo: enable jumbo frame"
2652 		" which max packet len is PKTLEN in decimal (64-9600)\n"
2653 		"  --hash-entry-num: specify the hash entry number in hexadecimal to be setup\n"
2654 		"  --no-lthreads: turn off lthread model\n"
2655 		"  --parse-ptype: set to use software to analyze packet type\n\n",
2656 		prgname);
2657 }
2658 
2659 static int parse_max_pkt_len(const char *pktlen)
2660 {
2661 	char *end = NULL;
2662 	unsigned long len;
2663 
2664 	/* parse decimal string */
2665 	len = strtoul(pktlen, &end, 10);
2666 	if ((pktlen[0] == '\0') || (end == NULL) || (*end != '\0'))
2667 		return -1;
2668 
2669 	if (len == 0)
2670 		return -1;
2671 
2672 	return len;
2673 }
2674 
2675 static int
2676 parse_portmask(const char *portmask)
2677 {
2678 	char *end = NULL;
2679 	unsigned long pm;
2680 
2681 	/* parse hexadecimal string */
2682 	pm = strtoul(portmask, &end, 16);
2683 	if ((portmask[0] == '\0') || (end == NULL) || (*end != '\0'))
2684 		return -1;
2685 
2686 	if (pm == 0)
2687 		return -1;
2688 
2689 	return pm;
2690 }
2691 
2692 #if (APP_LOOKUP_METHOD == APP_LOOKUP_EXACT_MATCH)
2693 static int
2694 parse_hash_entry_number(const char *hash_entry_num)
2695 {
2696 	char *end = NULL;
2697 	unsigned long hash_en;
2698 
2699 	/* parse hexadecimal string */
2700 	hash_en = strtoul(hash_entry_num, &end, 16);
2701 	if ((hash_entry_num[0] == '\0') || (end == NULL) || (*end != '\0'))
2702 		return -1;
2703 
2704 	if (hash_en == 0)
2705 		return -1;
2706 
2707 	return hash_en;
2708 }
2709 #endif
2710 
2711 static int
2712 parse_rx_config(const char *q_arg)
2713 {
2714 	char s[256];
2715 	const char *p, *p0 = q_arg;
2716 	char *end;
2717 	enum fieldnames {
2718 		FLD_PORT = 0,
2719 		FLD_QUEUE,
2720 		FLD_LCORE,
2721 		FLD_THREAD,
2722 		_NUM_FLD
2723 	};
2724 	unsigned long int_fld[_NUM_FLD];
2725 	char *str_fld[_NUM_FLD];
2726 	int i;
2727 	unsigned size;
2728 
2729 	nb_rx_thread_params = 0;
2730 
2731 	while ((p = strchr(p0, '(')) != NULL) {
2732 		++p;
2733 		p0 = strchr(p, ')');
2734 		if (p0 == NULL)
2735 			return -1;
2736 
2737 		size = p0 - p;
2738 		if (size >= sizeof(s))
2739 			return -1;
2740 
2741 		snprintf(s, sizeof(s), "%.*s", size, p);
2742 		if (rte_strsplit(s, sizeof(s), str_fld, _NUM_FLD, ',') != _NUM_FLD)
2743 			return -1;
2744 		for (i = 0; i < _NUM_FLD; i++) {
2745 			errno = 0;
2746 			int_fld[i] = strtoul(str_fld[i], &end, 0);
2747 			if (errno != 0 || end == str_fld[i] || int_fld[i] > 255)
2748 				return -1;
2749 		}
2750 		if (nb_rx_thread_params >= MAX_LCORE_PARAMS) {
2751 			printf("exceeded max number of rx params: %hu\n",
2752 					nb_rx_thread_params);
2753 			return -1;
2754 		}
2755 		rx_thread_params_array[nb_rx_thread_params].port_id =
2756 				int_fld[FLD_PORT];
2757 		rx_thread_params_array[nb_rx_thread_params].queue_id =
2758 				(uint8_t)int_fld[FLD_QUEUE];
2759 		rx_thread_params_array[nb_rx_thread_params].lcore_id =
2760 				(uint8_t)int_fld[FLD_LCORE];
2761 		rx_thread_params_array[nb_rx_thread_params].thread_id =
2762 				(uint8_t)int_fld[FLD_THREAD];
2763 		++nb_rx_thread_params;
2764 	}
2765 	rx_thread_params = rx_thread_params_array;
2766 	return 0;
2767 }
2768 
2769 static int
2770 parse_tx_config(const char *q_arg)
2771 {
2772 	char s[256];
2773 	const char *p, *p0 = q_arg;
2774 	char *end;
2775 	enum fieldnames {
2776 		FLD_LCORE = 0,
2777 		FLD_THREAD,
2778 		_NUM_FLD
2779 	};
2780 	unsigned long int_fld[_NUM_FLD];
2781 	char *str_fld[_NUM_FLD];
2782 	int i;
2783 	unsigned size;
2784 
2785 	nb_tx_thread_params = 0;
2786 
2787 	while ((p = strchr(p0, '(')) != NULL) {
2788 		++p;
2789 		p0 = strchr(p, ')');
2790 		if (p0 == NULL)
2791 			return -1;
2792 
2793 		size = p0 - p;
2794 		if (size >= sizeof(s))
2795 			return -1;
2796 
2797 		snprintf(s, sizeof(s), "%.*s", size, p);
2798 		if (rte_strsplit(s, sizeof(s), str_fld, _NUM_FLD, ',') != _NUM_FLD)
2799 			return -1;
2800 		for (i = 0; i < _NUM_FLD; i++) {
2801 			errno = 0;
2802 			int_fld[i] = strtoul(str_fld[i], &end, 0);
2803 			if (errno != 0 || end == str_fld[i] || int_fld[i] > 255)
2804 				return -1;
2805 		}
2806 		if (nb_tx_thread_params >= MAX_LCORE_PARAMS) {
2807 			printf("exceeded max number of tx params: %hu\n",
2808 				nb_tx_thread_params);
2809 			return -1;
2810 		}
2811 		tx_thread_params_array[nb_tx_thread_params].lcore_id =
2812 				(uint8_t)int_fld[FLD_LCORE];
2813 		tx_thread_params_array[nb_tx_thread_params].thread_id =
2814 				(uint8_t)int_fld[FLD_THREAD];
2815 		++nb_tx_thread_params;
2816 	}
2817 	tx_thread_params = tx_thread_params_array;
2818 
2819 	return 0;
2820 }
2821 
2822 #if (APP_CPU_LOAD > 0)
2823 static int
2824 parse_stat_lcore(const char *stat_lcore)
2825 {
2826 	char *end = NULL;
2827 	unsigned long lcore_id;
2828 
2829 	lcore_id = strtoul(stat_lcore, &end, 10);
2830 	if ((stat_lcore[0] == '\0') || (end == NULL) || (*end != '\0'))
2831 		return -1;
2832 
2833 	return lcore_id;
2834 }
2835 #endif
2836 
2837 static void
2838 parse_eth_dest(const char *optarg)
2839 {
2840 	uint16_t portid;
2841 	char *port_end;
2842 	uint8_t c, *dest, peer_addr[6];
2843 
2844 	errno = 0;
2845 	portid = strtoul(optarg, &port_end, 10);
2846 	if (errno != 0 || port_end == optarg || *port_end++ != ',')
2847 		rte_exit(EXIT_FAILURE,
2848 		"Invalid eth-dest: %s", optarg);
2849 	if (portid >= RTE_MAX_ETHPORTS)
2850 		rte_exit(EXIT_FAILURE,
2851 		"eth-dest: port %d >= RTE_MAX_ETHPORTS(%d)\n",
2852 		portid, RTE_MAX_ETHPORTS);
2853 
2854 	if (cmdline_parse_etheraddr(NULL, port_end,
2855 		&peer_addr, sizeof(peer_addr)) < 0)
2856 		rte_exit(EXIT_FAILURE,
2857 		"Invalid ethernet address: %s\n",
2858 		port_end);
2859 	dest = (uint8_t *)&dest_eth_addr[portid];
2860 	for (c = 0; c < 6; c++)
2861 		dest[c] = peer_addr[c];
2862 	*(uint64_t *)(val_eth + portid) = dest_eth_addr[portid];
2863 }
2864 
2865 #define CMD_LINE_OPT_RX_CONFIG "rx"
2866 #define CMD_LINE_OPT_TX_CONFIG "tx"
2867 #define CMD_LINE_OPT_STAT_LCORE "stat-lcore"
2868 #define CMD_LINE_OPT_ETH_DEST "eth-dest"
2869 #define CMD_LINE_OPT_NO_NUMA "no-numa"
2870 #define CMD_LINE_OPT_IPV6 "ipv6"
2871 #define CMD_LINE_OPT_ENABLE_JUMBO "enable-jumbo"
2872 #define CMD_LINE_OPT_HASH_ENTRY_NUM "hash-entry-num"
2873 #define CMD_LINE_OPT_NO_LTHREADS "no-lthreads"
2874 #define CMD_LINE_OPT_PARSE_PTYPE "parse-ptype"
2875 
2876 /* Parse the argument given in the command line of the application */
2877 static int
2878 parse_args(int argc, char **argv)
2879 {
2880 	int opt, ret;
2881 	char **argvopt;
2882 	int option_index;
2883 	char *prgname = argv[0];
2884 	static struct option lgopts[] = {
2885 		{CMD_LINE_OPT_RX_CONFIG, 1, 0, 0},
2886 		{CMD_LINE_OPT_TX_CONFIG, 1, 0, 0},
2887 		{CMD_LINE_OPT_STAT_LCORE, 1, 0, 0},
2888 		{CMD_LINE_OPT_ETH_DEST, 1, 0, 0},
2889 		{CMD_LINE_OPT_NO_NUMA, 0, 0, 0},
2890 		{CMD_LINE_OPT_IPV6, 0, 0, 0},
2891 		{CMD_LINE_OPT_ENABLE_JUMBO, 0, 0, 0},
2892 		{CMD_LINE_OPT_HASH_ENTRY_NUM, 1, 0, 0},
2893 		{CMD_LINE_OPT_NO_LTHREADS, 0, 0, 0},
2894 		{CMD_LINE_OPT_PARSE_PTYPE, 0, 0, 0},
2895 		{NULL, 0, 0, 0}
2896 	};
2897 
2898 	argvopt = argv;
2899 
2900 	while ((opt = getopt_long(argc, argvopt, "p:P",
2901 				lgopts, &option_index)) != EOF) {
2902 
2903 		switch (opt) {
2904 		/* portmask */
2905 		case 'p':
2906 			enabled_port_mask = parse_portmask(optarg);
2907 			if (enabled_port_mask == 0) {
2908 				printf("invalid portmask\n");
2909 				print_usage(prgname);
2910 				return -1;
2911 			}
2912 			break;
2913 		case 'P':
2914 			printf("Promiscuous mode selected\n");
2915 			promiscuous_on = 1;
2916 			break;
2917 
2918 		/* long options */
2919 		case 0:
2920 			if (!strncmp(lgopts[option_index].name, CMD_LINE_OPT_RX_CONFIG,
2921 				sizeof(CMD_LINE_OPT_RX_CONFIG))) {
2922 				ret = parse_rx_config(optarg);
2923 				if (ret) {
2924 					printf("invalid rx-config\n");
2925 					print_usage(prgname);
2926 					return -1;
2927 				}
2928 			}
2929 
2930 			if (!strncmp(lgopts[option_index].name, CMD_LINE_OPT_TX_CONFIG,
2931 				sizeof(CMD_LINE_OPT_TX_CONFIG))) {
2932 				ret = parse_tx_config(optarg);
2933 				if (ret) {
2934 					printf("invalid tx-config\n");
2935 					print_usage(prgname);
2936 					return -1;
2937 				}
2938 			}
2939 
2940 #if (APP_CPU_LOAD > 0)
2941 			if (!strncmp(lgopts[option_index].name, CMD_LINE_OPT_STAT_LCORE,
2942 					sizeof(CMD_LINE_OPT_STAT_LCORE))) {
2943 				cpu_load_lcore_id = parse_stat_lcore(optarg);
2944 			}
2945 #endif
2946 
2947 			if (!strncmp(lgopts[option_index].name, CMD_LINE_OPT_ETH_DEST,
2948 				sizeof(CMD_LINE_OPT_ETH_DEST)))
2949 					parse_eth_dest(optarg);
2950 
2951 			if (!strncmp(lgopts[option_index].name, CMD_LINE_OPT_NO_NUMA,
2952 				sizeof(CMD_LINE_OPT_NO_NUMA))) {
2953 				printf("numa is disabled\n");
2954 				numa_on = 0;
2955 			}
2956 
2957 #if (APP_LOOKUP_METHOD == APP_LOOKUP_EXACT_MATCH)
2958 			if (!strncmp(lgopts[option_index].name, CMD_LINE_OPT_IPV6,
2959 				sizeof(CMD_LINE_OPT_IPV6))) {
2960 				printf("ipv6 is specified\n");
2961 				ipv6 = 1;
2962 			}
2963 #endif
2964 
2965 			if (!strncmp(lgopts[option_index].name, CMD_LINE_OPT_NO_LTHREADS,
2966 					sizeof(CMD_LINE_OPT_NO_LTHREADS))) {
2967 				printf("l-threads model is disabled\n");
2968 				lthreads_on = 0;
2969 			}
2970 
2971 			if (!strncmp(lgopts[option_index].name, CMD_LINE_OPT_PARSE_PTYPE,
2972 					sizeof(CMD_LINE_OPT_PARSE_PTYPE))) {
2973 				printf("software packet type parsing enabled\n");
2974 				parse_ptype_on = 1;
2975 			}
2976 
2977 			if (!strncmp(lgopts[option_index].name, CMD_LINE_OPT_ENABLE_JUMBO,
2978 				sizeof(CMD_LINE_OPT_ENABLE_JUMBO))) {
2979 				struct option lenopts = {"max-pkt-len", required_argument, 0,
2980 						0};
2981 
2982 				printf("jumbo frame is enabled - disabling simple TX path\n");
2983 				port_conf.rxmode.offloads |=
2984 						DEV_RX_OFFLOAD_JUMBO_FRAME;
2985 				port_conf.txmode.offloads |=
2986 						DEV_TX_OFFLOAD_MULTI_SEGS;
2987 
2988 				/* if no max-pkt-len set, use the default value
2989 				 * RTE_ETHER_MAX_LEN
2990 				 */
2991 				if (0 == getopt_long(argc, argvopt, "", &lenopts,
2992 						&option_index)) {
2993 
2994 					ret = parse_max_pkt_len(optarg);
2995 					if ((ret < 64) || (ret > MAX_JUMBO_PKT_LEN)) {
2996 						printf("invalid packet length\n");
2997 						print_usage(prgname);
2998 						return -1;
2999 					}
3000 					port_conf.rxmode.max_rx_pkt_len = ret;
3001 				}
3002 				printf("set jumbo frame max packet length to %u\n",
3003 						(unsigned int)port_conf.rxmode.max_rx_pkt_len);
3004 			}
3005 #if (APP_LOOKUP_METHOD == APP_LOOKUP_EXACT_MATCH)
3006 			if (!strncmp(lgopts[option_index].name, CMD_LINE_OPT_HASH_ENTRY_NUM,
3007 				sizeof(CMD_LINE_OPT_HASH_ENTRY_NUM))) {
3008 				ret = parse_hash_entry_number(optarg);
3009 				if ((ret > 0) && (ret <= L3FWD_HASH_ENTRIES)) {
3010 					hash_entry_number = ret;
3011 				} else {
3012 					printf("invalid hash entry number\n");
3013 					print_usage(prgname);
3014 					return -1;
3015 				}
3016 			}
3017 #endif
3018 			break;
3019 
3020 		default:
3021 			print_usage(prgname);
3022 			return -1;
3023 		}
3024 	}
3025 
3026 	if (optind >= 0)
3027 		argv[optind-1] = prgname;
3028 
3029 	ret = optind-1;
3030 	optind = 1; /* reset getopt lib */
3031 	return ret;
3032 }
3033 
3034 static void
3035 print_ethaddr(const char *name, const struct rte_ether_addr *eth_addr)
3036 {
3037 	char buf[RTE_ETHER_ADDR_FMT_SIZE];
3038 
3039 	rte_ether_format_addr(buf, RTE_ETHER_ADDR_FMT_SIZE, eth_addr);
3040 	printf("%s%s", name, buf);
3041 }
3042 
3043 #if (APP_LOOKUP_METHOD == APP_LOOKUP_EXACT_MATCH)
3044 
3045 static void convert_ipv4_5tuple(struct ipv4_5tuple *key1,
3046 		union ipv4_5tuple_host *key2)
3047 {
3048 	key2->ip_dst = rte_cpu_to_be_32(key1->ip_dst);
3049 	key2->ip_src = rte_cpu_to_be_32(key1->ip_src);
3050 	key2->port_dst = rte_cpu_to_be_16(key1->port_dst);
3051 	key2->port_src = rte_cpu_to_be_16(key1->port_src);
3052 	key2->proto = key1->proto;
3053 	key2->pad0 = 0;
3054 	key2->pad1 = 0;
3055 }
3056 
3057 static void convert_ipv6_5tuple(struct ipv6_5tuple *key1,
3058 		union ipv6_5tuple_host *key2)
3059 {
3060 	uint32_t i;
3061 
3062 	for (i = 0; i < 16; i++) {
3063 		key2->ip_dst[i] = key1->ip_dst[i];
3064 		key2->ip_src[i] = key1->ip_src[i];
3065 	}
3066 	key2->port_dst = rte_cpu_to_be_16(key1->port_dst);
3067 	key2->port_src = rte_cpu_to_be_16(key1->port_src);
3068 	key2->proto = key1->proto;
3069 	key2->pad0 = 0;
3070 	key2->pad1 = 0;
3071 	key2->reserve = 0;
3072 }
3073 
3074 #define BYTE_VALUE_MAX 256
3075 #define ALL_32_BITS 0xffffffff
3076 #define BIT_8_TO_15 0x0000ff00
3077 static inline void
3078 populate_ipv4_few_flow_into_table(const struct rte_hash *h)
3079 {
3080 	uint32_t i;
3081 	int32_t ret;
3082 	uint32_t array_len = RTE_DIM(ipv4_l3fwd_route_array);
3083 
3084 	mask0 = _mm_set_epi32(ALL_32_BITS, ALL_32_BITS, ALL_32_BITS, BIT_8_TO_15);
3085 	for (i = 0; i < array_len; i++) {
3086 		struct ipv4_l3fwd_route  entry;
3087 		union ipv4_5tuple_host newkey;
3088 
3089 		entry = ipv4_l3fwd_route_array[i];
3090 		convert_ipv4_5tuple(&entry.key, &newkey);
3091 		ret = rte_hash_add_key(h, (void *)&newkey);
3092 		if (ret < 0) {
3093 			rte_exit(EXIT_FAILURE, "Unable to add entry %" PRIu32
3094 				" to the l3fwd hash.\n", i);
3095 		}
3096 		ipv4_l3fwd_out_if[ret] = entry.if_out;
3097 	}
3098 	printf("Hash: Adding 0x%" PRIx32 " keys\n", array_len);
3099 }
3100 
3101 #define BIT_16_TO_23 0x00ff0000
3102 static inline void
3103 populate_ipv6_few_flow_into_table(const struct rte_hash *h)
3104 {
3105 	uint32_t i;
3106 	int32_t ret;
3107 	uint32_t array_len = RTE_DIM(ipv6_l3fwd_route_array);
3108 
3109 	mask1 = _mm_set_epi32(ALL_32_BITS, ALL_32_BITS, ALL_32_BITS, BIT_16_TO_23);
3110 	mask2 = _mm_set_epi32(0, 0, ALL_32_BITS, ALL_32_BITS);
3111 	for (i = 0; i < array_len; i++) {
3112 		struct ipv6_l3fwd_route entry;
3113 		union ipv6_5tuple_host newkey;
3114 
3115 		entry = ipv6_l3fwd_route_array[i];
3116 		convert_ipv6_5tuple(&entry.key, &newkey);
3117 		ret = rte_hash_add_key(h, (void *)&newkey);
3118 		if (ret < 0) {
3119 			rte_exit(EXIT_FAILURE, "Unable to add entry %" PRIu32
3120 				" to the l3fwd hash.\n", i);
3121 		}
3122 		ipv6_l3fwd_out_if[ret] = entry.if_out;
3123 	}
3124 	printf("Hash: Adding 0x%" PRIx32 "keys\n", array_len);
3125 }
3126 
3127 #define NUMBER_PORT_USED 4
3128 static inline void
3129 populate_ipv4_many_flow_into_table(const struct rte_hash *h,
3130 		unsigned int nr_flow)
3131 {
3132 	unsigned i;
3133 
3134 	mask0 = _mm_set_epi32(ALL_32_BITS, ALL_32_BITS, ALL_32_BITS, BIT_8_TO_15);
3135 
3136 	for (i = 0; i < nr_flow; i++) {
3137 		struct ipv4_l3fwd_route entry;
3138 		union ipv4_5tuple_host newkey;
3139 		uint8_t a = (uint8_t)((i / NUMBER_PORT_USED) % BYTE_VALUE_MAX);
3140 		uint8_t b = (uint8_t)(((i / NUMBER_PORT_USED) / BYTE_VALUE_MAX) %
3141 				BYTE_VALUE_MAX);
3142 		uint8_t c = (uint8_t)((i / NUMBER_PORT_USED) / (BYTE_VALUE_MAX *
3143 				BYTE_VALUE_MAX));
3144 		/* Create the ipv4 exact match flow */
3145 		memset(&entry, 0, sizeof(entry));
3146 		switch (i & (NUMBER_PORT_USED - 1)) {
3147 		case 0:
3148 			entry = ipv4_l3fwd_route_array[0];
3149 			entry.key.ip_dst = RTE_IPV4(101, c, b, a);
3150 			break;
3151 		case 1:
3152 			entry = ipv4_l3fwd_route_array[1];
3153 			entry.key.ip_dst = RTE_IPV4(201, c, b, a);
3154 			break;
3155 		case 2:
3156 			entry = ipv4_l3fwd_route_array[2];
3157 			entry.key.ip_dst = RTE_IPV4(111, c, b, a);
3158 			break;
3159 		case 3:
3160 			entry = ipv4_l3fwd_route_array[3];
3161 			entry.key.ip_dst = RTE_IPV4(211, c, b, a);
3162 			break;
3163 		};
3164 		convert_ipv4_5tuple(&entry.key, &newkey);
3165 		int32_t ret = rte_hash_add_key(h, (void *)&newkey);
3166 
3167 		if (ret < 0)
3168 			rte_exit(EXIT_FAILURE, "Unable to add entry %u\n", i);
3169 
3170 		ipv4_l3fwd_out_if[ret] = (uint8_t)entry.if_out;
3171 
3172 	}
3173 	printf("Hash: Adding 0x%x keys\n", nr_flow);
3174 }
3175 
3176 static inline void
3177 populate_ipv6_many_flow_into_table(const struct rte_hash *h,
3178 		unsigned int nr_flow)
3179 {
3180 	unsigned i;
3181 
3182 	mask1 = _mm_set_epi32(ALL_32_BITS, ALL_32_BITS, ALL_32_BITS, BIT_16_TO_23);
3183 	mask2 = _mm_set_epi32(0, 0, ALL_32_BITS, ALL_32_BITS);
3184 	for (i = 0; i < nr_flow; i++) {
3185 		struct ipv6_l3fwd_route entry;
3186 		union ipv6_5tuple_host newkey;
3187 
3188 		uint8_t a = (uint8_t) ((i / NUMBER_PORT_USED) % BYTE_VALUE_MAX);
3189 		uint8_t b = (uint8_t) (((i / NUMBER_PORT_USED) / BYTE_VALUE_MAX) %
3190 				BYTE_VALUE_MAX);
3191 		uint8_t c = (uint8_t) ((i / NUMBER_PORT_USED) / (BYTE_VALUE_MAX *
3192 				BYTE_VALUE_MAX));
3193 
3194 		/* Create the ipv6 exact match flow */
3195 		memset(&entry, 0, sizeof(entry));
3196 		switch (i & (NUMBER_PORT_USED - 1)) {
3197 		case 0:
3198 			entry = ipv6_l3fwd_route_array[0];
3199 			break;
3200 		case 1:
3201 			entry = ipv6_l3fwd_route_array[1];
3202 			break;
3203 		case 2:
3204 			entry = ipv6_l3fwd_route_array[2];
3205 			break;
3206 		case 3:
3207 			entry = ipv6_l3fwd_route_array[3];
3208 			break;
3209 		};
3210 		entry.key.ip_dst[13] = c;
3211 		entry.key.ip_dst[14] = b;
3212 		entry.key.ip_dst[15] = a;
3213 		convert_ipv6_5tuple(&entry.key, &newkey);
3214 		int32_t ret = rte_hash_add_key(h, (void *)&newkey);
3215 
3216 		if (ret < 0)
3217 			rte_exit(EXIT_FAILURE, "Unable to add entry %u\n", i);
3218 
3219 		ipv6_l3fwd_out_if[ret] = (uint8_t) entry.if_out;
3220 
3221 	}
3222 	printf("Hash: Adding 0x%x keys\n", nr_flow);
3223 }
3224 
3225 static void
3226 setup_hash(int socketid)
3227 {
3228 	struct rte_hash_parameters ipv4_l3fwd_hash_params = {
3229 		.name = NULL,
3230 		.entries = L3FWD_HASH_ENTRIES,
3231 		.key_len = sizeof(union ipv4_5tuple_host),
3232 		.hash_func = ipv4_hash_crc,
3233 		.hash_func_init_val = 0,
3234 	};
3235 
3236 	struct rte_hash_parameters ipv6_l3fwd_hash_params = {
3237 		.name = NULL,
3238 		.entries = L3FWD_HASH_ENTRIES,
3239 		.key_len = sizeof(union ipv6_5tuple_host),
3240 		.hash_func = ipv6_hash_crc,
3241 		.hash_func_init_val = 0,
3242 	};
3243 
3244 	char s[64];
3245 
3246 	/* create ipv4 hash */
3247 	snprintf(s, sizeof(s), "ipv4_l3fwd_hash_%d", socketid);
3248 	ipv4_l3fwd_hash_params.name = s;
3249 	ipv4_l3fwd_hash_params.socket_id = socketid;
3250 	ipv4_l3fwd_lookup_struct[socketid] =
3251 			rte_hash_create(&ipv4_l3fwd_hash_params);
3252 	if (ipv4_l3fwd_lookup_struct[socketid] == NULL)
3253 		rte_exit(EXIT_FAILURE, "Unable to create the l3fwd hash on "
3254 				"socket %d\n", socketid);
3255 
3256 	/* create ipv6 hash */
3257 	snprintf(s, sizeof(s), "ipv6_l3fwd_hash_%d", socketid);
3258 	ipv6_l3fwd_hash_params.name = s;
3259 	ipv6_l3fwd_hash_params.socket_id = socketid;
3260 	ipv6_l3fwd_lookup_struct[socketid] =
3261 			rte_hash_create(&ipv6_l3fwd_hash_params);
3262 	if (ipv6_l3fwd_lookup_struct[socketid] == NULL)
3263 		rte_exit(EXIT_FAILURE, "Unable to create the l3fwd hash on "
3264 				"socket %d\n", socketid);
3265 
3266 	if (hash_entry_number != HASH_ENTRY_NUMBER_DEFAULT) {
3267 		/* For testing hash matching with a large number of flows we
3268 		 * generate millions of IP 5-tuples with an incremented dst
3269 		 * address to initialize the hash table. */
3270 		if (ipv6 == 0) {
3271 			/* populate the ipv4 hash */
3272 			populate_ipv4_many_flow_into_table(
3273 				ipv4_l3fwd_lookup_struct[socketid], hash_entry_number);
3274 		} else {
3275 			/* populate the ipv6 hash */
3276 			populate_ipv6_many_flow_into_table(
3277 				ipv6_l3fwd_lookup_struct[socketid], hash_entry_number);
3278 		}
3279 	} else {
3280 		/* Use data in ipv4/ipv6 l3fwd lookup table directly to initialize
3281 		 * the hash table */
3282 		if (ipv6 == 0) {
3283 			/* populate the ipv4 hash */
3284 			populate_ipv4_few_flow_into_table(
3285 					ipv4_l3fwd_lookup_struct[socketid]);
3286 		} else {
3287 			/* populate the ipv6 hash */
3288 			populate_ipv6_few_flow_into_table(
3289 					ipv6_l3fwd_lookup_struct[socketid]);
3290 		}
3291 	}
3292 }
3293 #endif
3294 
3295 #if (APP_LOOKUP_METHOD == APP_LOOKUP_LPM)
3296 static void
3297 setup_lpm(int socketid)
3298 {
3299 	struct rte_lpm6_config config;
3300 	struct rte_lpm_config lpm_ipv4_config;
3301 	unsigned i;
3302 	int ret;
3303 	char s[64];
3304 
3305 	/* create the LPM table */
3306 	snprintf(s, sizeof(s), "IPV4_L3FWD_LPM_%d", socketid);
3307 	lpm_ipv4_config.max_rules = IPV4_L3FWD_LPM_MAX_RULES;
3308 	lpm_ipv4_config.number_tbl8s = 256;
3309 	lpm_ipv4_config.flags = 0;
3310 	ipv4_l3fwd_lookup_struct[socketid] =
3311 			rte_lpm_create(s, socketid, &lpm_ipv4_config);
3312 	if (ipv4_l3fwd_lookup_struct[socketid] == NULL)
3313 		rte_exit(EXIT_FAILURE, "Unable to create the l3fwd LPM table"
3314 				" on socket %d\n", socketid);
3315 
3316 	/* populate the LPM table */
3317 	for (i = 0; i < IPV4_L3FWD_NUM_ROUTES; i++) {
3318 
3319 		/* skip unused ports */
3320 		if ((1 << ipv4_l3fwd_route_array[i].if_out &
3321 				enabled_port_mask) == 0)
3322 			continue;
3323 
3324 		ret = rte_lpm_add(ipv4_l3fwd_lookup_struct[socketid],
3325 			ipv4_l3fwd_route_array[i].ip,
3326 			ipv4_l3fwd_route_array[i].depth,
3327 			ipv4_l3fwd_route_array[i].if_out);
3328 
3329 		if (ret < 0) {
3330 			rte_exit(EXIT_FAILURE, "Unable to add entry %u to the "
3331 				"l3fwd LPM table on socket %d\n",
3332 				i, socketid);
3333 		}
3334 
3335 		printf("LPM: Adding route 0x%08x / %d (%d)\n",
3336 			(unsigned)ipv4_l3fwd_route_array[i].ip,
3337 			ipv4_l3fwd_route_array[i].depth,
3338 			ipv4_l3fwd_route_array[i].if_out);
3339 	}
3340 
3341 	/* create the LPM6 table */
3342 	snprintf(s, sizeof(s), "IPV6_L3FWD_LPM_%d", socketid);
3343 
3344 	config.max_rules = IPV6_L3FWD_LPM_MAX_RULES;
3345 	config.number_tbl8s = IPV6_L3FWD_LPM_NUMBER_TBL8S;
3346 	config.flags = 0;
3347 	ipv6_l3fwd_lookup_struct[socketid] = rte_lpm6_create(s, socketid,
3348 				&config);
3349 	if (ipv6_l3fwd_lookup_struct[socketid] == NULL)
3350 		rte_exit(EXIT_FAILURE, "Unable to create the l3fwd LPM table"
3351 				" on socket %d\n", socketid);
3352 
3353 	/* populate the LPM table */
3354 	for (i = 0; i < IPV6_L3FWD_NUM_ROUTES; i++) {
3355 
3356 		/* skip unused ports */
3357 		if ((1 << ipv6_l3fwd_route_array[i].if_out &
3358 				enabled_port_mask) == 0)
3359 			continue;
3360 
3361 		ret = rte_lpm6_add(ipv6_l3fwd_lookup_struct[socketid],
3362 			ipv6_l3fwd_route_array[i].ip,
3363 			ipv6_l3fwd_route_array[i].depth,
3364 			ipv6_l3fwd_route_array[i].if_out);
3365 
3366 		if (ret < 0) {
3367 			rte_exit(EXIT_FAILURE, "Unable to add entry %u to the "
3368 				"l3fwd LPM table on socket %d\n",
3369 				i, socketid);
3370 		}
3371 
3372 		printf("LPM: Adding route %s / %d (%d)\n",
3373 			"IPV6",
3374 			ipv6_l3fwd_route_array[i].depth,
3375 			ipv6_l3fwd_route_array[i].if_out);
3376 	}
3377 }
3378 #endif
3379 
3380 static int
3381 init_mem(unsigned nb_mbuf)
3382 {
3383 	struct lcore_conf *qconf;
3384 	int socketid;
3385 	unsigned lcore_id;
3386 	char s[64];
3387 
3388 	for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
3389 		if (rte_lcore_is_enabled(lcore_id) == 0)
3390 			continue;
3391 
3392 		if (numa_on)
3393 			socketid = rte_lcore_to_socket_id(lcore_id);
3394 		else
3395 			socketid = 0;
3396 
3397 		if (socketid >= NB_SOCKETS) {
3398 			rte_exit(EXIT_FAILURE, "Socket %d of lcore %u is out of range %d\n",
3399 				socketid, lcore_id, NB_SOCKETS);
3400 		}
3401 		if (pktmbuf_pool[socketid] == NULL) {
3402 			snprintf(s, sizeof(s), "mbuf_pool_%d", socketid);
3403 			pktmbuf_pool[socketid] =
3404 				rte_pktmbuf_pool_create(s, nb_mbuf,
3405 					MEMPOOL_CACHE_SIZE, 0,
3406 					RTE_MBUF_DEFAULT_BUF_SIZE, socketid);
3407 			if (pktmbuf_pool[socketid] == NULL)
3408 				rte_exit(EXIT_FAILURE,
3409 						"Cannot init mbuf pool on socket %d\n", socketid);
3410 			else
3411 				printf("Allocated mbuf pool on socket %d\n", socketid);
3412 
3413 #if (APP_LOOKUP_METHOD == APP_LOOKUP_LPM)
3414 			setup_lpm(socketid);
3415 #else
3416 			setup_hash(socketid);
3417 #endif
3418 		}
3419 		qconf = &lcore_conf[lcore_id];
3420 		qconf->ipv4_lookup_struct = ipv4_l3fwd_lookup_struct[socketid];
3421 		qconf->ipv6_lookup_struct = ipv6_l3fwd_lookup_struct[socketid];
3422 	}
3423 	return 0;
3424 }
3425 
3426 /* Check the link status of all ports in up to 9s, and print them finally */
3427 static void
3428 check_all_ports_link_status(uint32_t port_mask)
3429 {
3430 #define CHECK_INTERVAL 100 /* 100ms */
3431 #define MAX_CHECK_TIME 90 /* 9s (90 * 100ms) in total */
3432 	uint16_t portid;
3433 	uint8_t count, all_ports_up, print_flag = 0;
3434 	struct rte_eth_link link;
3435 	int ret;
3436 
3437 	printf("\nChecking link status");
3438 	fflush(stdout);
3439 	for (count = 0; count <= MAX_CHECK_TIME; count++) {
3440 		all_ports_up = 1;
3441 		RTE_ETH_FOREACH_DEV(portid) {
3442 			if ((port_mask & (1 << portid)) == 0)
3443 				continue;
3444 			memset(&link, 0, sizeof(link));
3445 			ret = rte_eth_link_get_nowait(portid, &link);
3446 			if (ret < 0) {
3447 				all_ports_up = 0;
3448 				if (print_flag == 1)
3449 					printf("Port %u link get failed: %s\n",
3450 						portid, rte_strerror(-ret));
3451 				continue;
3452 			}
3453 			/* print link status if flag set */
3454 			if (print_flag == 1) {
3455 				if (link.link_status)
3456 					printf(
3457 					"Port%d Link Up. Speed %u Mbps - %s\n",
3458 						portid, link.link_speed,
3459 				(link.link_duplex == ETH_LINK_FULL_DUPLEX) ?
3460 					("full-duplex") : ("half-duplex\n"));
3461 				else
3462 					printf("Port %d Link Down\n", portid);
3463 				continue;
3464 			}
3465 			/* clear all_ports_up flag if any link down */
3466 			if (link.link_status == ETH_LINK_DOWN) {
3467 				all_ports_up = 0;
3468 				break;
3469 			}
3470 		}
3471 		/* after finally printing all link status, get out */
3472 		if (print_flag == 1)
3473 			break;
3474 
3475 		if (all_ports_up == 0) {
3476 			printf(".");
3477 			fflush(stdout);
3478 			rte_delay_ms(CHECK_INTERVAL);
3479 		}
3480 
3481 		/* set the print_flag if all ports up or timeout */
3482 		if (all_ports_up == 1 || count == (MAX_CHECK_TIME - 1)) {
3483 			print_flag = 1;
3484 			printf("done\n");
3485 		}
3486 	}
3487 }
3488 
3489 int
3490 main(int argc, char **argv)
3491 {
3492 	struct rte_eth_dev_info dev_info;
3493 	struct rte_eth_txconf *txconf;
3494 	int ret;
3495 	int i;
3496 	unsigned nb_ports;
3497 	uint16_t queueid, portid;
3498 	unsigned lcore_id;
3499 	uint32_t n_tx_queue, nb_lcores;
3500 	uint8_t nb_rx_queue, queue, socketid;
3501 
3502 	/* init EAL */
3503 	ret = rte_eal_init(argc, argv);
3504 	if (ret < 0)
3505 		rte_exit(EXIT_FAILURE, "Invalid EAL parameters\n");
3506 	argc -= ret;
3507 	argv += ret;
3508 
3509 	ret = rte_timer_subsystem_init();
3510 	if (ret < 0)
3511 		rte_exit(EXIT_FAILURE, "Failed to initialize timer subystem\n");
3512 
3513 	/* pre-init dst MACs for all ports to 02:00:00:00:00:xx */
3514 	for (portid = 0; portid < RTE_MAX_ETHPORTS; portid++) {
3515 		dest_eth_addr[portid] = RTE_ETHER_LOCAL_ADMIN_ADDR +
3516 				((uint64_t)portid << 40);
3517 		*(uint64_t *)(val_eth + portid) = dest_eth_addr[portid];
3518 	}
3519 
3520 	/* parse application arguments (after the EAL ones) */
3521 	ret = parse_args(argc, argv);
3522 	if (ret < 0)
3523 		rte_exit(EXIT_FAILURE, "Invalid L3FWD parameters\n");
3524 
3525 	if (check_lcore_params() < 0)
3526 		rte_exit(EXIT_FAILURE, "check_lcore_params failed\n");
3527 
3528 	printf("Initializing rx-queues...\n");
3529 	ret = init_rx_queues();
3530 	if (ret < 0)
3531 		rte_exit(EXIT_FAILURE, "init_rx_queues failed\n");
3532 
3533 	printf("Initializing tx-threads...\n");
3534 	ret = init_tx_threads();
3535 	if (ret < 0)
3536 		rte_exit(EXIT_FAILURE, "init_tx_threads failed\n");
3537 
3538 	printf("Initializing rings...\n");
3539 	ret = init_rx_rings();
3540 	if (ret < 0)
3541 		rte_exit(EXIT_FAILURE, "init_rx_rings failed\n");
3542 
3543 	nb_ports = rte_eth_dev_count_avail();
3544 
3545 	if (check_port_config() < 0)
3546 		rte_exit(EXIT_FAILURE, "check_port_config failed\n");
3547 
3548 	nb_lcores = rte_lcore_count();
3549 
3550 	/* initialize all ports */
3551 	RTE_ETH_FOREACH_DEV(portid) {
3552 		struct rte_eth_conf local_port_conf = port_conf;
3553 
3554 		/* skip ports that are not enabled */
3555 		if ((enabled_port_mask & (1 << portid)) == 0) {
3556 			printf("\nSkipping disabled port %d\n", portid);
3557 			continue;
3558 		}
3559 
3560 		/* init port */
3561 		printf("Initializing port %d ... ", portid);
3562 		fflush(stdout);
3563 
3564 		nb_rx_queue = get_port_n_rx_queues(portid);
3565 		n_tx_queue = nb_lcores;
3566 		if (n_tx_queue > MAX_TX_QUEUE_PER_PORT)
3567 			n_tx_queue = MAX_TX_QUEUE_PER_PORT;
3568 		printf("Creating queues: nb_rxq=%d nb_txq=%u... ",
3569 			nb_rx_queue, (unsigned)n_tx_queue);
3570 
3571 		ret = rte_eth_dev_info_get(portid, &dev_info);
3572 		if (ret != 0)
3573 			rte_exit(EXIT_FAILURE,
3574 				"Error during getting device (port %u) info: %s\n",
3575 				portid, strerror(-ret));
3576 
3577 		if (dev_info.tx_offload_capa & DEV_TX_OFFLOAD_MBUF_FAST_FREE)
3578 			local_port_conf.txmode.offloads |=
3579 				DEV_TX_OFFLOAD_MBUF_FAST_FREE;
3580 
3581 		local_port_conf.rx_adv_conf.rss_conf.rss_hf &=
3582 			dev_info.flow_type_rss_offloads;
3583 		if (local_port_conf.rx_adv_conf.rss_conf.rss_hf !=
3584 				port_conf.rx_adv_conf.rss_conf.rss_hf) {
3585 			printf("Port %u modified RSS hash function based on hardware support,"
3586 				"requested:%#"PRIx64" configured:%#"PRIx64"\n",
3587 				portid,
3588 				port_conf.rx_adv_conf.rss_conf.rss_hf,
3589 				local_port_conf.rx_adv_conf.rss_conf.rss_hf);
3590 		}
3591 
3592 		ret = rte_eth_dev_configure(portid, nb_rx_queue,
3593 					(uint16_t)n_tx_queue, &local_port_conf);
3594 		if (ret < 0)
3595 			rte_exit(EXIT_FAILURE, "Cannot configure device: err=%d, port=%d\n",
3596 				ret, portid);
3597 
3598 		ret = rte_eth_dev_adjust_nb_rx_tx_desc(portid, &nb_rxd,
3599 						       &nb_txd);
3600 		if (ret < 0)
3601 			rte_exit(EXIT_FAILURE,
3602 				 "rte_eth_dev_adjust_nb_rx_tx_desc: err=%d, port=%d\n",
3603 				 ret, portid);
3604 
3605 		ret = rte_eth_macaddr_get(portid, &ports_eth_addr[portid]);
3606 		if (ret < 0)
3607 			rte_exit(EXIT_FAILURE,
3608 				 "rte_eth_macaddr_get: err=%d, port=%d\n",
3609 				 ret, portid);
3610 
3611 		print_ethaddr(" Address:", &ports_eth_addr[portid]);
3612 		printf(", ");
3613 		print_ethaddr("Destination:",
3614 			(const struct rte_ether_addr *)&dest_eth_addr[portid]);
3615 		printf(", ");
3616 
3617 		/*
3618 		 * prepare src MACs for each port.
3619 		 */
3620 		rte_ether_addr_copy(&ports_eth_addr[portid],
3621 			(struct rte_ether_addr *)(val_eth + portid) + 1);
3622 
3623 		/* init memory */
3624 		ret = init_mem(NB_MBUF);
3625 		if (ret < 0)
3626 			rte_exit(EXIT_FAILURE, "init_mem failed\n");
3627 
3628 		/* init one TX queue per couple (lcore,port) */
3629 		queueid = 0;
3630 		for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
3631 			if (rte_lcore_is_enabled(lcore_id) == 0)
3632 				continue;
3633 
3634 			if (numa_on)
3635 				socketid = (uint8_t)rte_lcore_to_socket_id(lcore_id);
3636 			else
3637 				socketid = 0;
3638 
3639 			printf("txq=%u,%d,%d ", lcore_id, queueid, socketid);
3640 			fflush(stdout);
3641 
3642 			txconf = &dev_info.default_txconf;
3643 			txconf->offloads = local_port_conf.txmode.offloads;
3644 			ret = rte_eth_tx_queue_setup(portid, queueid, nb_txd,
3645 						     socketid, txconf);
3646 			if (ret < 0)
3647 				rte_exit(EXIT_FAILURE, "rte_eth_tx_queue_setup: err=%d, "
3648 					"port=%d\n", ret, portid);
3649 
3650 			tx_thread[lcore_id].tx_queue_id[portid] = queueid;
3651 			queueid++;
3652 		}
3653 		printf("\n");
3654 	}
3655 
3656 	for (i = 0; i < n_rx_thread; i++) {
3657 		lcore_id = rx_thread[i].conf.lcore_id;
3658 
3659 		if (rte_lcore_is_enabled(lcore_id) == 0) {
3660 			rte_exit(EXIT_FAILURE,
3661 					"Cannot start Rx thread on lcore %u: lcore disabled\n",
3662 					lcore_id
3663 				);
3664 		}
3665 
3666 		printf("\nInitializing rx queues for Rx thread %d on lcore %u ... ",
3667 				i, lcore_id);
3668 		fflush(stdout);
3669 
3670 		/* init RX queues */
3671 		for (queue = 0; queue < rx_thread[i].n_rx_queue; ++queue) {
3672 			struct rte_eth_rxconf rxq_conf;
3673 
3674 			portid = rx_thread[i].rx_queue_list[queue].port_id;
3675 			queueid = rx_thread[i].rx_queue_list[queue].queue_id;
3676 
3677 			if (numa_on)
3678 				socketid = (uint8_t)rte_lcore_to_socket_id(lcore_id);
3679 			else
3680 				socketid = 0;
3681 
3682 			printf("rxq=%d,%d,%d ", portid, queueid, socketid);
3683 			fflush(stdout);
3684 
3685 			ret = rte_eth_dev_info_get(portid, &dev_info);
3686 			if (ret != 0)
3687 				rte_exit(EXIT_FAILURE,
3688 					"Error during getting device (port %u) info: %s\n",
3689 					portid, strerror(-ret));
3690 
3691 			rxq_conf = dev_info.default_rxconf;
3692 			rxq_conf.offloads = port_conf.rxmode.offloads;
3693 			ret = rte_eth_rx_queue_setup(portid, queueid, nb_rxd,
3694 					socketid,
3695 					&rxq_conf,
3696 					pktmbuf_pool[socketid]);
3697 			if (ret < 0)
3698 				rte_exit(EXIT_FAILURE, "rte_eth_rx_queue_setup: err=%d, "
3699 						"port=%d\n", ret, portid);
3700 		}
3701 	}
3702 
3703 	printf("\n");
3704 
3705 	/* start ports */
3706 	RTE_ETH_FOREACH_DEV(portid) {
3707 		if ((enabled_port_mask & (1 << portid)) == 0)
3708 			continue;
3709 
3710 		/* Start device */
3711 		ret = rte_eth_dev_start(portid);
3712 		if (ret < 0)
3713 			rte_exit(EXIT_FAILURE, "rte_eth_dev_start: err=%d, port=%d\n",
3714 				ret, portid);
3715 
3716 		/*
3717 		 * If enabled, put device in promiscuous mode.
3718 		 * This allows IO forwarding mode to forward packets
3719 		 * to itself through 2 cross-connected  ports of the
3720 		 * target machine.
3721 		 */
3722 		if (promiscuous_on) {
3723 			ret = rte_eth_promiscuous_enable(portid);
3724 			if (ret != 0)
3725 				rte_exit(EXIT_FAILURE,
3726 					"rte_eth_promiscuous_enable: err=%s, port=%u\n",
3727 					rte_strerror(-ret), portid);
3728 		}
3729 	}
3730 
3731 	for (i = 0; i < n_rx_thread; i++) {
3732 		lcore_id = rx_thread[i].conf.lcore_id;
3733 		if (rte_lcore_is_enabled(lcore_id) == 0)
3734 			continue;
3735 
3736 		/* check if hw packet type is supported */
3737 		for (queue = 0; queue < rx_thread[i].n_rx_queue; ++queue) {
3738 			portid = rx_thread[i].rx_queue_list[queue].port_id;
3739 			queueid = rx_thread[i].rx_queue_list[queue].queue_id;
3740 
3741 			if (parse_ptype_on) {
3742 				if (!rte_eth_add_rx_callback(portid, queueid,
3743 						cb_parse_ptype, NULL))
3744 					rte_exit(EXIT_FAILURE,
3745 						"Failed to add rx callback: "
3746 						"port=%d\n", portid);
3747 			} else if (!check_ptype(portid))
3748 				rte_exit(EXIT_FAILURE,
3749 					"Port %d cannot parse packet type.\n\n"
3750 					"Please add --parse-ptype to use sw "
3751 					"packet type analyzer.\n\n",
3752 					portid);
3753 		}
3754 	}
3755 
3756 	check_all_ports_link_status(enabled_port_mask);
3757 
3758 	if (lthreads_on) {
3759 		printf("Starting L-Threading Model\n");
3760 
3761 #if (APP_CPU_LOAD > 0)
3762 		if (cpu_load_lcore_id > 0)
3763 			/* Use one lcore for cpu load collector */
3764 			nb_lcores--;
3765 #endif
3766 
3767 		lthread_num_schedulers_set(nb_lcores);
3768 		rte_eal_mp_remote_launch(sched_spawner, NULL, SKIP_MASTER);
3769 		lthread_master_spawner(NULL);
3770 
3771 	} else {
3772 		printf("Starting P-Threading Model\n");
3773 		/* launch per-lcore init on every lcore */
3774 		rte_eal_mp_remote_launch(pthread_run, NULL, CALL_MASTER);
3775 		RTE_LCORE_FOREACH_SLAVE(lcore_id) {
3776 			if (rte_eal_wait_lcore(lcore_id) < 0)
3777 				return -1;
3778 		}
3779 	}
3780 
3781 	return 0;
3782 }
3783