xref: /dpdk/examples/l3fwd/l3fwd_lpm.c (revision d5c4897ecfb2540dc4990d9b367ddbe5013d0e66)
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 #include <stdbool.h>
16 #include <sys/socket.h>
17 #include <arpa/inet.h>
18 
19 #include <rte_debug.h>
20 #include <rte_ether.h>
21 #include <rte_ethdev.h>
22 #include <rte_cycles.h>
23 #include <rte_mbuf.h>
24 #include <rte_ip.h>
25 #include <rte_tcp.h>
26 #include <rte_udp.h>
27 #include <rte_lpm.h>
28 #include <rte_lpm6.h>
29 
30 #include "l3fwd.h"
31 #include "l3fwd_common.h"
32 #include "l3fwd_event.h"
33 
34 #include "lpm_route_parse.c"
35 
36 #define IPV4_L3FWD_LPM_MAX_RULES         1024
37 #define IPV4_L3FWD_LPM_NUMBER_TBL8S (1 << 8)
38 #define IPV6_L3FWD_LPM_MAX_RULES         1024
39 #define IPV6_L3FWD_LPM_NUMBER_TBL8S (1 << 16)
40 
41 static struct rte_lpm *ipv4_l3fwd_lpm_lookup_struct[NB_SOCKETS];
42 static struct rte_lpm6 *ipv6_l3fwd_lpm_lookup_struct[NB_SOCKETS];
43 
44 /* Performing LPM-based lookups. 8< */
45 static inline uint16_t
46 lpm_get_ipv4_dst_port(const struct rte_ipv4_hdr *ipv4_hdr,
47 		      uint16_t portid,
48 		      struct rte_lpm *ipv4_l3fwd_lookup_struct)
49 {
50 	uint32_t dst_ip = rte_be_to_cpu_32(ipv4_hdr->dst_addr);
51 	uint32_t next_hop;
52 
53 	if (rte_lpm_lookup(ipv4_l3fwd_lookup_struct, dst_ip, &next_hop) == 0)
54 		return next_hop;
55 	else
56 		return portid;
57 }
58 /* >8 End of performing LPM-based lookups. */
59 
60 static inline uint16_t
61 lpm_get_ipv6_dst_port(const struct rte_ipv6_hdr *ipv6_hdr,
62 		      uint16_t portid,
63 		      struct rte_lpm6 *ipv6_l3fwd_lookup_struct)
64 {
65 	const struct rte_ipv6_addr *dst_ip = &ipv6_hdr->dst_addr;
66 	uint32_t next_hop;
67 
68 	if (rte_lpm6_lookup(ipv6_l3fwd_lookup_struct, dst_ip, &next_hop) == 0)
69 		return next_hop;
70 	else
71 		return portid;
72 }
73 
74 static __rte_always_inline uint16_t
75 lpm_get_dst_port(const struct lcore_conf *qconf, struct rte_mbuf *pkt,
76 		uint16_t portid)
77 {
78 	struct rte_ipv6_hdr *ipv6_hdr;
79 	struct rte_ipv4_hdr *ipv4_hdr;
80 	struct rte_ether_hdr *eth_hdr;
81 
82 	if (RTE_ETH_IS_IPV4_HDR(pkt->packet_type)) {
83 
84 		eth_hdr = rte_pktmbuf_mtod(pkt, struct rte_ether_hdr *);
85 		ipv4_hdr = (struct rte_ipv4_hdr *)(eth_hdr + 1);
86 
87 		return lpm_get_ipv4_dst_port(ipv4_hdr, portid,
88 					     qconf->ipv4_lookup_struct);
89 	} else if (RTE_ETH_IS_IPV6_HDR(pkt->packet_type)) {
90 
91 		eth_hdr = rte_pktmbuf_mtod(pkt, struct rte_ether_hdr *);
92 		ipv6_hdr = (struct rte_ipv6_hdr *)(eth_hdr + 1);
93 
94 		return lpm_get_ipv6_dst_port(ipv6_hdr, portid,
95 					     qconf->ipv6_lookup_struct);
96 	}
97 
98 	return portid;
99 }
100 
101 /*
102  * lpm_get_dst_port optimized routine for packets where dst_ipv4 is already
103  * precalculated. If packet is ipv6 dst_addr is taken directly from packet
104  * header and dst_ipv4 value is not used.
105  */
106 static __rte_always_inline uint16_t
107 lpm_get_dst_port_with_ipv4(const struct lcore_conf *qconf, struct rte_mbuf *pkt,
108 	uint32_t dst_ipv4, uint16_t portid)
109 {
110 	uint32_t next_hop;
111 	struct rte_ipv6_hdr *ipv6_hdr;
112 	struct rte_ether_hdr *eth_hdr;
113 
114 	if (RTE_ETH_IS_IPV4_HDR(pkt->packet_type)) {
115 		return (uint16_t) ((rte_lpm_lookup(qconf->ipv4_lookup_struct,
116 						   dst_ipv4, &next_hop) == 0)
117 				   ? next_hop : portid);
118 
119 	} else if (RTE_ETH_IS_IPV6_HDR(pkt->packet_type)) {
120 
121 		eth_hdr = rte_pktmbuf_mtod(pkt, struct rte_ether_hdr *);
122 		ipv6_hdr = (struct rte_ipv6_hdr *)(eth_hdr + 1);
123 
124 		return (uint16_t) ((rte_lpm6_lookup(qconf->ipv6_lookup_struct,
125 				&ipv6_hdr->dst_addr, &next_hop) == 0)
126 				? next_hop : portid);
127 
128 	}
129 
130 	return portid;
131 }
132 
133 #if defined(RTE_ARCH_X86)
134 #include "l3fwd_lpm_sse.h"
135 #elif defined __ARM_NEON
136 #include "l3fwd_lpm_neon.h"
137 #elif defined(RTE_ARCH_PPC_64)
138 #include "l3fwd_lpm_altivec.h"
139 #else
140 #include "l3fwd_lpm.h"
141 #endif
142 
143 /* main processing loop */
144 int
145 lpm_main_loop(__rte_unused void *dummy)
146 {
147 	struct rte_mbuf *pkts_burst[MAX_PKT_BURST];
148 	unsigned lcore_id;
149 	uint64_t prev_tsc, diff_tsc, cur_tsc;
150 	int i, nb_rx;
151 	uint16_t portid, queueid;
152 	struct lcore_conf *qconf;
153 	const uint64_t drain_tsc = (rte_get_tsc_hz() + US_PER_S - 1) /
154 		US_PER_S * BURST_TX_DRAIN_US;
155 
156 	lcore_id = rte_lcore_id();
157 	qconf = &lcore_conf[lcore_id];
158 
159 	const uint16_t n_rx_q = qconf->n_rx_queue;
160 	const uint16_t n_tx_p = qconf->n_tx_port;
161 	if (n_rx_q == 0) {
162 		RTE_LOG(INFO, L3FWD, "lcore %u has nothing to do\n", lcore_id);
163 		return 0;
164 	}
165 
166 	RTE_LOG(INFO, L3FWD, "entering main loop on lcore %u\n", lcore_id);
167 
168 	for (i = 0; i < n_rx_q; i++) {
169 
170 		portid = qconf->rx_queue_list[i].port_id;
171 		queueid = qconf->rx_queue_list[i].queue_id;
172 		RTE_LOG(INFO, L3FWD,
173 			" -- lcoreid=%u portid=%u rxqueueid=%" PRIu16 "\n",
174 			lcore_id, portid, queueid);
175 	}
176 
177 	cur_tsc = rte_rdtsc();
178 	prev_tsc = cur_tsc;
179 
180 	while (!force_quit) {
181 
182 		/*
183 		 * TX burst queue drain
184 		 */
185 		diff_tsc = cur_tsc - prev_tsc;
186 		if (unlikely(diff_tsc > drain_tsc)) {
187 
188 			for (i = 0; i < n_tx_p; ++i) {
189 				portid = qconf->tx_port_id[i];
190 				if (qconf->tx_mbufs[portid].len == 0)
191 					continue;
192 				send_burst(qconf,
193 					qconf->tx_mbufs[portid].len,
194 					portid);
195 				qconf->tx_mbufs[portid].len = 0;
196 			}
197 
198 			prev_tsc = cur_tsc;
199 		}
200 
201 		/*
202 		 * Read packet from RX queues
203 		 */
204 		for (i = 0; i < n_rx_q; ++i) {
205 			portid = qconf->rx_queue_list[i].port_id;
206 			queueid = qconf->rx_queue_list[i].queue_id;
207 			nb_rx = rte_eth_rx_burst(portid, queueid, pkts_burst,
208 				nb_pkt_per_burst);
209 			if (nb_rx == 0)
210 				continue;
211 
212 #if defined RTE_ARCH_X86 || defined __ARM_NEON \
213 			 || defined RTE_ARCH_PPC_64
214 			l3fwd_lpm_send_packets(nb_rx, pkts_burst,
215 						portid, qconf);
216 #else
217 			l3fwd_lpm_no_opt_send_packets(nb_rx, pkts_burst,
218 							portid, qconf);
219 #endif /* X86 */
220 		}
221 
222 		cur_tsc = rte_rdtsc();
223 	}
224 
225 	return 0;
226 }
227 
228 #ifdef RTE_LIB_EVENTDEV
229 static __rte_always_inline uint16_t
230 lpm_process_event_pkt(const struct lcore_conf *lconf, struct rte_mbuf *mbuf)
231 {
232 	mbuf->port = lpm_get_dst_port(lconf, mbuf, mbuf->port);
233 
234 #if defined RTE_ARCH_X86 || defined __ARM_NEON \
235 	|| defined RTE_ARCH_PPC_64
236 	process_packet(mbuf, &mbuf->port);
237 #else
238 
239 	struct rte_ether_hdr *eth_hdr = rte_pktmbuf_mtod(mbuf,
240 			struct rte_ether_hdr *);
241 
242 	/* dst addr */
243 	*(uint64_t *)&eth_hdr->dst_addr = dest_eth_addr[mbuf->port];
244 
245 	/* src addr */
246 	rte_ether_addr_copy(&ports_eth_addr[mbuf->port],
247 			&eth_hdr->src_addr);
248 
249 	rfc1812_process(rte_pktmbuf_mtod_offset(mbuf, struct rte_ipv4_hdr *,
250 						sizeof(struct rte_ether_hdr)),
251 			&mbuf->port, mbuf->packet_type);
252 #endif
253 	return mbuf->port;
254 }
255 
256 static __rte_always_inline void
257 lpm_event_loop_single(struct l3fwd_event_resources *evt_rsrc,
258 		const uint8_t flags)
259 {
260 	const int event_p_id = l3fwd_get_free_event_port(evt_rsrc);
261 	const uint8_t tx_q_id = evt_rsrc->evq.event_q_id[
262 		evt_rsrc->evq.nb_queues - 1];
263 	const uint8_t event_d_id = evt_rsrc->event_d_id;
264 	uint8_t enq = 0, deq = 0;
265 	struct lcore_conf *lconf;
266 	unsigned int lcore_id;
267 	struct rte_event ev;
268 
269 	if (event_p_id < 0)
270 		return;
271 
272 	lcore_id = rte_lcore_id();
273 	lconf = &lcore_conf[lcore_id];
274 
275 	RTE_LOG(INFO, L3FWD, "entering %s on lcore %u\n", __func__, lcore_id);
276 	while (!force_quit) {
277 		deq = rte_event_dequeue_burst(event_d_id, event_p_id, &ev, 1,
278 					      0);
279 		if (!deq)
280 			continue;
281 
282 		if (lpm_process_event_pkt(lconf, ev.mbuf) == BAD_PORT) {
283 			rte_pktmbuf_free(ev.mbuf);
284 			continue;
285 		}
286 
287 		if (flags & L3FWD_EVENT_TX_ENQ) {
288 			ev.queue_id = tx_q_id;
289 			ev.op = RTE_EVENT_OP_FORWARD;
290 			do {
291 				enq = rte_event_enqueue_burst(
292 					event_d_id, event_p_id, &ev, 1);
293 			} while (!enq && !force_quit);
294 		}
295 
296 		if (flags & L3FWD_EVENT_TX_DIRECT) {
297 			rte_event_eth_tx_adapter_txq_set(ev.mbuf, 0);
298 			do {
299 				enq = rte_event_eth_tx_adapter_enqueue(
300 					event_d_id, event_p_id, &ev, 1, 0);
301 			} while (!enq && !force_quit);
302 		}
303 	}
304 
305 	l3fwd_event_worker_cleanup(event_d_id, event_p_id, &ev, enq, deq, 0);
306 }
307 
308 static __rte_always_inline void
309 lpm_event_loop_burst(struct l3fwd_event_resources *evt_rsrc,
310 		const uint8_t flags)
311 {
312 	const int event_p_id = l3fwd_get_free_event_port(evt_rsrc);
313 	const uint8_t tx_q_id = evt_rsrc->evq.event_q_id[
314 		evt_rsrc->evq.nb_queues - 1];
315 	const uint8_t event_d_id = evt_rsrc->event_d_id;
316 	const uint16_t deq_len = evt_rsrc->deq_depth;
317 	struct rte_event events[MAX_PKT_BURST];
318 	int i, nb_enq = 0, nb_deq = 0;
319 	struct lcore_conf *lconf;
320 	unsigned int lcore_id;
321 
322 	if (event_p_id < 0)
323 		return;
324 
325 	lcore_id = rte_lcore_id();
326 
327 	lconf = &lcore_conf[lcore_id];
328 
329 	RTE_LOG(INFO, L3FWD, "entering %s on lcore %u\n", __func__, lcore_id);
330 
331 	while (!force_quit) {
332 		/* Read events from RX queues */
333 		nb_deq = rte_event_dequeue_burst(event_d_id, event_p_id,
334 				events, deq_len, 0);
335 		if (nb_deq == 0) {
336 			rte_pause();
337 			continue;
338 		}
339 
340 		for (i = 0; i < nb_deq; i++) {
341 			if (flags & L3FWD_EVENT_TX_ENQ) {
342 				events[i].queue_id = tx_q_id;
343 				events[i].op = RTE_EVENT_OP_FORWARD;
344 			}
345 
346 			if (flags & L3FWD_EVENT_TX_DIRECT)
347 				rte_event_eth_tx_adapter_txq_set(events[i].mbuf,
348 								 0);
349 
350 			lpm_process_event_pkt(lconf, events[i].mbuf);
351 		}
352 
353 		if (flags & L3FWD_EVENT_TX_ENQ) {
354 			nb_enq = rte_event_enqueue_burst(event_d_id, event_p_id,
355 					events, nb_deq);
356 			while (nb_enq < nb_deq && !force_quit)
357 				nb_enq += rte_event_enqueue_burst(event_d_id,
358 						event_p_id, events + nb_enq,
359 						nb_deq - nb_enq);
360 		}
361 
362 		if (flags & L3FWD_EVENT_TX_DIRECT) {
363 			nb_enq = rte_event_eth_tx_adapter_enqueue(event_d_id,
364 					event_p_id, events, nb_deq, 0);
365 			while (nb_enq < nb_deq && !force_quit)
366 				nb_enq += rte_event_eth_tx_adapter_enqueue(
367 						event_d_id, event_p_id,
368 						events + nb_enq,
369 						nb_deq - nb_enq, 0);
370 		}
371 	}
372 
373 	l3fwd_event_worker_cleanup(event_d_id, event_p_id, events, nb_enq,
374 				   nb_deq, 0);
375 }
376 
377 static __rte_always_inline void
378 lpm_event_loop(struct l3fwd_event_resources *evt_rsrc,
379 		 const uint8_t flags)
380 {
381 	if (flags & L3FWD_EVENT_SINGLE)
382 		lpm_event_loop_single(evt_rsrc, flags);
383 	if (flags & L3FWD_EVENT_BURST)
384 		lpm_event_loop_burst(evt_rsrc, flags);
385 }
386 
387 int __rte_noinline
388 lpm_event_main_loop_tx_d(__rte_unused void *dummy)
389 {
390 	struct l3fwd_event_resources *evt_rsrc =
391 					l3fwd_get_eventdev_rsrc();
392 
393 	lpm_event_loop(evt_rsrc, L3FWD_EVENT_TX_DIRECT | L3FWD_EVENT_SINGLE);
394 	return 0;
395 }
396 
397 int __rte_noinline
398 lpm_event_main_loop_tx_d_burst(__rte_unused void *dummy)
399 {
400 	struct l3fwd_event_resources *evt_rsrc =
401 					l3fwd_get_eventdev_rsrc();
402 
403 	lpm_event_loop(evt_rsrc, L3FWD_EVENT_TX_DIRECT | L3FWD_EVENT_BURST);
404 	return 0;
405 }
406 
407 int __rte_noinline
408 lpm_event_main_loop_tx_q(__rte_unused void *dummy)
409 {
410 	struct l3fwd_event_resources *evt_rsrc =
411 					l3fwd_get_eventdev_rsrc();
412 
413 	lpm_event_loop(evt_rsrc, L3FWD_EVENT_TX_ENQ | L3FWD_EVENT_SINGLE);
414 	return 0;
415 }
416 
417 int __rte_noinline
418 lpm_event_main_loop_tx_q_burst(__rte_unused void *dummy)
419 {
420 	struct l3fwd_event_resources *evt_rsrc =
421 					l3fwd_get_eventdev_rsrc();
422 
423 	lpm_event_loop(evt_rsrc, L3FWD_EVENT_TX_ENQ | L3FWD_EVENT_BURST);
424 	return 0;
425 }
426 
427 static __rte_always_inline void
428 lpm_process_event_vector(struct rte_event_vector *vec, struct lcore_conf *lconf,
429 			 uint16_t *dst_port)
430 {
431 	struct rte_mbuf **mbufs = vec->mbufs;
432 	int i;
433 
434 #if defined RTE_ARCH_X86 || defined __ARM_NEON || defined RTE_ARCH_PPC_64
435 	if (vec->attr_valid) {
436 		l3fwd_lpm_process_packets(vec->nb_elem, mbufs, vec->port,
437 					  dst_port, lconf, 1);
438 	} else {
439 		for (i = 0; i < vec->nb_elem; i++)
440 			l3fwd_lpm_process_packets(1, &mbufs[i], mbufs[i]->port,
441 						  &dst_port[i], lconf, 1);
442 	}
443 #else
444 	for (i = 0; i < vec->nb_elem; i++)
445 		dst_port[i] = lpm_process_event_pkt(lconf, mbufs[i]);
446 #endif
447 
448 	process_event_vector(vec, dst_port);
449 }
450 
451 /* Same eventdev loop for single and burst of vector */
452 static __rte_always_inline void
453 lpm_event_loop_vector(struct l3fwd_event_resources *evt_rsrc,
454 		      const uint8_t flags)
455 {
456 	const int event_p_id = l3fwd_get_free_event_port(evt_rsrc);
457 	const uint8_t tx_q_id =
458 		evt_rsrc->evq.event_q_id[evt_rsrc->evq.nb_queues - 1];
459 	const uint8_t event_d_id = evt_rsrc->event_d_id;
460 	const uint16_t deq_len = evt_rsrc->deq_depth;
461 	struct rte_event events[MAX_PKT_BURST];
462 	int i, nb_enq = 0, nb_deq = 0;
463 	struct lcore_conf *lconf;
464 	uint16_t *dst_port_list;
465 	unsigned int lcore_id;
466 
467 	if (event_p_id < 0)
468 		return;
469 
470 	lcore_id = rte_lcore_id();
471 	lconf = &lcore_conf[lcore_id];
472 	dst_port_list =
473 		rte_zmalloc("", sizeof(uint16_t) * evt_rsrc->vector_size,
474 			    RTE_CACHE_LINE_SIZE);
475 	if (dst_port_list == NULL)
476 		return;
477 	RTE_LOG(INFO, L3FWD, "entering %s on lcore %u\n", __func__, lcore_id);
478 
479 	while (!force_quit) {
480 		/* Read events from RX queues */
481 		nb_deq = rte_event_dequeue_burst(event_d_id, event_p_id, events,
482 						 deq_len, 0);
483 		if (nb_deq == 0) {
484 			rte_pause();
485 			continue;
486 		}
487 
488 		for (i = 0; i < nb_deq; i++) {
489 			if (flags & L3FWD_EVENT_TX_ENQ) {
490 				events[i].queue_id = tx_q_id;
491 				events[i].op = RTE_EVENT_OP_FORWARD;
492 			}
493 
494 			lpm_process_event_vector(events[i].vec, lconf,
495 						 dst_port_list);
496 		}
497 
498 		if (flags & L3FWD_EVENT_TX_ENQ) {
499 			nb_enq = rte_event_enqueue_burst(event_d_id, event_p_id,
500 							 events, nb_deq);
501 			while (nb_enq < nb_deq && !force_quit)
502 				nb_enq += rte_event_enqueue_burst(
503 					event_d_id, event_p_id, events + nb_enq,
504 					nb_deq - nb_enq);
505 		}
506 
507 		if (flags & L3FWD_EVENT_TX_DIRECT) {
508 			nb_enq = rte_event_eth_tx_adapter_enqueue(
509 				event_d_id, event_p_id, events, nb_deq, 0);
510 			while (nb_enq < nb_deq && !force_quit)
511 				nb_enq += rte_event_eth_tx_adapter_enqueue(
512 					event_d_id, event_p_id, events + nb_enq,
513 					nb_deq - nb_enq, 0);
514 		}
515 	}
516 
517 	l3fwd_event_worker_cleanup(event_d_id, event_p_id, events, nb_enq,
518 				   nb_deq, 1);
519 	rte_free(dst_port_list);
520 }
521 
522 int __rte_noinline
523 lpm_event_main_loop_tx_d_vector(__rte_unused void *dummy)
524 {
525 	struct l3fwd_event_resources *evt_rsrc = l3fwd_get_eventdev_rsrc();
526 
527 	lpm_event_loop_vector(evt_rsrc, L3FWD_EVENT_TX_DIRECT);
528 	return 0;
529 }
530 
531 int __rte_noinline
532 lpm_event_main_loop_tx_d_burst_vector(__rte_unused void *dummy)
533 {
534 	struct l3fwd_event_resources *evt_rsrc = l3fwd_get_eventdev_rsrc();
535 
536 	lpm_event_loop_vector(evt_rsrc, L3FWD_EVENT_TX_DIRECT);
537 	return 0;
538 }
539 
540 int __rte_noinline
541 lpm_event_main_loop_tx_q_vector(__rte_unused void *dummy)
542 {
543 	struct l3fwd_event_resources *evt_rsrc = l3fwd_get_eventdev_rsrc();
544 
545 	lpm_event_loop_vector(evt_rsrc, L3FWD_EVENT_TX_ENQ);
546 	return 0;
547 }
548 
549 int __rte_noinline
550 lpm_event_main_loop_tx_q_burst_vector(__rte_unused void *dummy)
551 {
552 	struct l3fwd_event_resources *evt_rsrc = l3fwd_get_eventdev_rsrc();
553 
554 	lpm_event_loop_vector(evt_rsrc, L3FWD_EVENT_TX_ENQ);
555 	return 0;
556 }
557 #endif
558 
559 void
560 setup_lpm(const int socketid)
561 {
562 	struct rte_eth_dev_info dev_info;
563 	struct rte_lpm6_config config;
564 	struct rte_lpm_config config_ipv4;
565 	int i;
566 	int ret;
567 	char s[64];
568 	char abuf[INET6_ADDRSTRLEN];
569 
570 	/* create the LPM table */
571 	config_ipv4.max_rules = IPV4_L3FWD_LPM_MAX_RULES;
572 	config_ipv4.number_tbl8s = IPV4_L3FWD_LPM_NUMBER_TBL8S;
573 	config_ipv4.flags = 0;
574 	snprintf(s, sizeof(s), "IPV4_L3FWD_LPM_%d", socketid);
575 	ipv4_l3fwd_lpm_lookup_struct[socketid] =
576 			rte_lpm_create(s, socketid, &config_ipv4);
577 	if (ipv4_l3fwd_lpm_lookup_struct[socketid] == NULL)
578 		rte_exit(EXIT_FAILURE,
579 			"Unable to create the l3fwd LPM table on socket %d\n",
580 			socketid);
581 
582 	/* populate the LPM table */
583 	for (i = 0; i < route_num_v4; i++) {
584 		struct in_addr in;
585 
586 		/* skip unused ports */
587 		if ((1 << route_base_v4[i].if_out &
588 				enabled_port_mask) == 0)
589 			continue;
590 
591 		ret = rte_eth_dev_info_get(route_base_v4[i].if_out, &dev_info);
592 		if (ret < 0)
593 			rte_exit(EXIT_FAILURE, "Unable to get device info for port %u\n",
594 				 route_base_v4[i].if_out);
595 
596 		ret = rte_lpm_add(ipv4_l3fwd_lpm_lookup_struct[socketid],
597 			route_base_v4[i].ip,
598 			route_base_v4[i].depth,
599 			route_base_v4[i].if_out);
600 
601 		if (ret < 0) {
602 			lpm_free_routes();
603 			rte_exit(EXIT_FAILURE,
604 				"Unable to add entry %u to the l3fwd LPM table on socket %d\n",
605 				i, socketid);
606 		}
607 
608 		in.s_addr = htonl(route_base_v4[i].ip);
609 		printf("LPM: Adding route %s / %d (%d) [%s]\n",
610 		       inet_ntop(AF_INET, &in, abuf, sizeof(abuf)),
611 		       route_base_v4[i].depth,
612 		       route_base_v4[i].if_out, rte_dev_name(dev_info.device));
613 	}
614 
615 	/* create the LPM6 table */
616 	snprintf(s, sizeof(s), "IPV6_L3FWD_LPM_%d", socketid);
617 
618 	config.max_rules = IPV6_L3FWD_LPM_MAX_RULES;
619 	config.number_tbl8s = IPV6_L3FWD_LPM_NUMBER_TBL8S;
620 	config.flags = 0;
621 	ipv6_l3fwd_lpm_lookup_struct[socketid] = rte_lpm6_create(s, socketid,
622 				&config);
623 	if (ipv6_l3fwd_lpm_lookup_struct[socketid] == NULL) {
624 		lpm_free_routes();
625 		rte_exit(EXIT_FAILURE,
626 			"Unable to create the l3fwd LPM table on socket %d\n",
627 			socketid);
628 	}
629 
630 	/* populate the LPM table */
631 	for (i = 0; i < route_num_v6; i++) {
632 
633 		/* skip unused ports */
634 		if ((1 << route_base_v6[i].if_out &
635 				enabled_port_mask) == 0)
636 			continue;
637 
638 		ret = rte_eth_dev_info_get(route_base_v6[i].if_out, &dev_info);
639 		if (ret < 0)
640 			rte_exit(EXIT_FAILURE, "Unable to get device info for port %u\n",
641 				 route_base_v6[i].if_out);
642 
643 		ret = rte_lpm6_add(ipv6_l3fwd_lpm_lookup_struct[socketid],
644 			&route_base_v6[i].ip6,
645 			route_base_v6[i].depth,
646 			route_base_v6[i].if_out);
647 
648 		if (ret < 0) {
649 			lpm_free_routes();
650 			rte_exit(EXIT_FAILURE,
651 				"Unable to add entry %u to the l3fwd LPM table on socket %d\n",
652 				i, socketid);
653 		}
654 
655 		printf("LPM: Adding route %s / %d (%d) [%s]\n",
656 		       inet_ntop(AF_INET6, &route_base_v6[i].ip6, abuf,
657 				 sizeof(abuf)),
658 		       route_base_v6[i].depth,
659 		       route_base_v6[i].if_out, rte_dev_name(dev_info.device));
660 	}
661 }
662 
663 int
664 lpm_check_ptype(int portid)
665 {
666 	int i, ret;
667 	int ptype_l3_ipv4 = 0, ptype_l3_ipv6 = 0;
668 	uint32_t ptype_mask = RTE_PTYPE_L3_MASK;
669 
670 	ret = rte_eth_dev_get_supported_ptypes(portid, ptype_mask, NULL, 0);
671 	if (ret <= 0)
672 		return 0;
673 
674 	uint32_t ptypes[ret];
675 
676 	ret = rte_eth_dev_get_supported_ptypes(portid, ptype_mask, ptypes, ret);
677 	for (i = 0; i < ret; ++i) {
678 		if (ptypes[i] & RTE_PTYPE_L3_IPV4)
679 			ptype_l3_ipv4 = 1;
680 		if (ptypes[i] & RTE_PTYPE_L3_IPV6)
681 			ptype_l3_ipv6 = 1;
682 	}
683 
684 	if (!ipv6 && !ptype_l3_ipv4) {
685 		printf("port %d cannot parse RTE_PTYPE_L3_IPV4\n", portid);
686 		return 0;
687 	}
688 
689 	if (ipv6 && !ptype_l3_ipv6) {
690 		printf("port %d cannot parse RTE_PTYPE_L3_IPV6\n", portid);
691 		return 0;
692 	}
693 
694 	return 1;
695 
696 }
697 
698 static inline void
699 lpm_parse_ptype(struct rte_mbuf *m)
700 {
701 	struct rte_ether_hdr *eth_hdr;
702 	uint32_t packet_type = RTE_PTYPE_UNKNOWN;
703 	uint16_t ether_type;
704 
705 	eth_hdr = rte_pktmbuf_mtod(m, struct rte_ether_hdr *);
706 	ether_type = eth_hdr->ether_type;
707 	if (ether_type == rte_cpu_to_be_16(RTE_ETHER_TYPE_IPV4))
708 		packet_type |= RTE_PTYPE_L3_IPV4_EXT_UNKNOWN;
709 	else if (ether_type == rte_cpu_to_be_16(RTE_ETHER_TYPE_IPV6))
710 		packet_type |= RTE_PTYPE_L3_IPV6_EXT_UNKNOWN;
711 
712 	m->packet_type = packet_type;
713 }
714 
715 uint16_t
716 lpm_cb_parse_ptype(uint16_t port __rte_unused, uint16_t queue __rte_unused,
717 		   struct rte_mbuf *pkts[], uint16_t nb_pkts,
718 		   uint16_t max_pkts __rte_unused,
719 		   void *user_param __rte_unused)
720 {
721 	unsigned int i;
722 
723 	if (unlikely(nb_pkts == 0))
724 		return nb_pkts;
725 	rte_prefetch0(rte_pktmbuf_mtod(pkts[0], struct ether_hdr *));
726 	for (i = 0; i < (unsigned int) (nb_pkts - 1); ++i) {
727 		rte_prefetch0(rte_pktmbuf_mtod(pkts[i+1],
728 			struct ether_hdr *));
729 		lpm_parse_ptype(pkts[i]);
730 	}
731 	lpm_parse_ptype(pkts[i]);
732 
733 	return nb_pkts;
734 }
735 
736 /* Return ipv4/ipv6 lpm fwd lookup struct. */
737 void *
738 lpm_get_ipv4_l3fwd_lookup_struct(const int socketid)
739 {
740 	return ipv4_l3fwd_lpm_lookup_struct[socketid];
741 }
742 
743 void *
744 lpm_get_ipv6_l3fwd_lookup_struct(const int socketid)
745 {
746 	return ipv6_l3fwd_lpm_lookup_struct[socketid];
747 }
748