xref: /dpdk/examples/l3fwd/l3fwd_lpm.c (revision a137eb2b)
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
lpm_get_ipv4_dst_port(const struct rte_ipv4_hdr * ipv4_hdr,uint16_t portid,struct rte_lpm * ipv4_l3fwd_lookup_struct)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
lpm_get_ipv6_dst_port(const struct rte_ipv6_hdr * ipv6_hdr,uint16_t portid,struct rte_lpm6 * ipv6_l3fwd_lookup_struct)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 uint8_t *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
lpm_get_dst_port(const struct lcore_conf * qconf,struct rte_mbuf * pkt,uint16_t portid)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
lpm_get_dst_port_with_ipv4(const struct lcore_conf * qconf,struct rte_mbuf * pkt,uint32_t dst_ipv4,uint16_t portid)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
lpm_main_loop(__rte_unused void * dummy)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;
152 	uint8_t queueid;
153 	struct lcore_conf *qconf;
154 	const uint64_t drain_tsc = (rte_get_tsc_hz() + US_PER_S - 1) /
155 		US_PER_S * BURST_TX_DRAIN_US;
156 
157 	lcore_id = rte_lcore_id();
158 	qconf = &lcore_conf[lcore_id];
159 
160 	const uint16_t n_rx_q = qconf->n_rx_queue;
161 	const uint16_t n_tx_p = qconf->n_tx_port;
162 	if (n_rx_q == 0) {
163 		RTE_LOG(INFO, L3FWD, "lcore %u has nothing to do\n", lcore_id);
164 		return 0;
165 	}
166 
167 	RTE_LOG(INFO, L3FWD, "entering main loop on lcore %u\n", lcore_id);
168 
169 	for (i = 0; i < n_rx_q; i++) {
170 
171 		portid = qconf->rx_queue_list[i].port_id;
172 		queueid = qconf->rx_queue_list[i].queue_id;
173 		RTE_LOG(INFO, L3FWD,
174 			" -- lcoreid=%u portid=%u rxqueueid=%hhu\n",
175 			lcore_id, portid, queueid);
176 	}
177 
178 	cur_tsc = rte_rdtsc();
179 	prev_tsc = cur_tsc;
180 
181 	while (!force_quit) {
182 
183 		/*
184 		 * TX burst queue drain
185 		 */
186 		diff_tsc = cur_tsc - prev_tsc;
187 		if (unlikely(diff_tsc > drain_tsc)) {
188 
189 			for (i = 0; i < n_tx_p; ++i) {
190 				portid = qconf->tx_port_id[i];
191 				if (qconf->tx_mbufs[portid].len == 0)
192 					continue;
193 				send_burst(qconf,
194 					qconf->tx_mbufs[portid].len,
195 					portid);
196 				qconf->tx_mbufs[portid].len = 0;
197 			}
198 
199 			prev_tsc = cur_tsc;
200 		}
201 
202 		/*
203 		 * Read packet from RX queues
204 		 */
205 		for (i = 0; i < n_rx_q; ++i) {
206 			portid = qconf->rx_queue_list[i].port_id;
207 			queueid = qconf->rx_queue_list[i].queue_id;
208 			nb_rx = rte_eth_rx_burst(portid, queueid, pkts_burst,
209 				MAX_PKT_BURST);
210 			if (nb_rx == 0)
211 				continue;
212 
213 #if defined RTE_ARCH_X86 || defined __ARM_NEON \
214 			 || defined RTE_ARCH_PPC_64
215 			l3fwd_lpm_send_packets(nb_rx, pkts_burst,
216 						portid, qconf);
217 #else
218 			l3fwd_lpm_no_opt_send_packets(nb_rx, pkts_burst,
219 							portid, qconf);
220 #endif /* X86 */
221 		}
222 
223 		cur_tsc = rte_rdtsc();
224 	}
225 
226 	return 0;
227 }
228 
229 static __rte_always_inline uint16_t
lpm_process_event_pkt(const struct lcore_conf * lconf,struct rte_mbuf * mbuf)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
lpm_event_loop_single(struct l3fwd_event_resources * evt_rsrc,const uint8_t flags)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
lpm_event_loop_burst(struct l3fwd_event_resources * evt_rsrc,const uint8_t flags)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
lpm_event_loop(struct l3fwd_event_resources * evt_rsrc,const uint8_t flags)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
lpm_event_main_loop_tx_d(__rte_unused void * dummy)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
lpm_event_main_loop_tx_d_burst(__rte_unused void * dummy)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
lpm_event_main_loop_tx_q(__rte_unused void * dummy)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
lpm_event_main_loop_tx_q_burst(__rte_unused void * dummy)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
lpm_process_event_vector(struct rte_event_vector * vec,struct lcore_conf * lconf)428 lpm_process_event_vector(struct rte_event_vector *vec, struct lcore_conf *lconf)
429 {
430 	struct rte_mbuf **mbufs = vec->mbufs;
431 	int i;
432 
433 	/* Process first packet to init vector attributes */
434 	lpm_process_event_pkt(lconf, mbufs[0]);
435 	if (vec->attr_valid) {
436 		if (mbufs[0]->port != BAD_PORT)
437 			vec->port = mbufs[0]->port;
438 		else
439 			vec->attr_valid = 0;
440 	}
441 
442 	for (i = 1; i < vec->nb_elem; i++) {
443 		lpm_process_event_pkt(lconf, mbufs[i]);
444 		event_vector_attr_validate(vec, mbufs[i]);
445 	}
446 }
447 
448 /* Same eventdev loop for single and burst of vector */
449 static __rte_always_inline void
lpm_event_loop_vector(struct l3fwd_event_resources * evt_rsrc,const uint8_t flags)450 lpm_event_loop_vector(struct l3fwd_event_resources *evt_rsrc,
451 		      const uint8_t flags)
452 {
453 	const int event_p_id = l3fwd_get_free_event_port(evt_rsrc);
454 	const uint8_t tx_q_id =
455 		evt_rsrc->evq.event_q_id[evt_rsrc->evq.nb_queues - 1];
456 	const uint8_t event_d_id = evt_rsrc->event_d_id;
457 	const uint16_t deq_len = evt_rsrc->deq_depth;
458 	struct rte_event events[MAX_PKT_BURST];
459 	int i, nb_enq = 0, nb_deq = 0;
460 	struct lcore_conf *lconf;
461 	unsigned int lcore_id;
462 
463 	if (event_p_id < 0)
464 		return;
465 
466 	lcore_id = rte_lcore_id();
467 	lconf = &lcore_conf[lcore_id];
468 
469 	RTE_LOG(INFO, L3FWD, "entering %s on lcore %u\n", __func__, lcore_id);
470 
471 	while (!force_quit) {
472 		/* Read events from RX queues */
473 		nb_deq = rte_event_dequeue_burst(event_d_id, event_p_id, events,
474 						 deq_len, 0);
475 		if (nb_deq == 0) {
476 			rte_pause();
477 			continue;
478 		}
479 
480 		for (i = 0; i < nb_deq; i++) {
481 			if (flags & L3FWD_EVENT_TX_ENQ) {
482 				events[i].queue_id = tx_q_id;
483 				events[i].op = RTE_EVENT_OP_FORWARD;
484 			}
485 
486 			lpm_process_event_vector(events[i].vec, lconf);
487 
488 			if (flags & L3FWD_EVENT_TX_DIRECT)
489 				event_vector_txq_set(events[i].vec, 0);
490 		}
491 
492 		if (flags & L3FWD_EVENT_TX_ENQ) {
493 			nb_enq = rte_event_enqueue_burst(event_d_id, event_p_id,
494 							 events, nb_deq);
495 			while (nb_enq < nb_deq && !force_quit)
496 				nb_enq += rte_event_enqueue_burst(
497 					event_d_id, event_p_id, events + nb_enq,
498 					nb_deq - nb_enq);
499 		}
500 
501 		if (flags & L3FWD_EVENT_TX_DIRECT) {
502 			nb_enq = rte_event_eth_tx_adapter_enqueue(
503 				event_d_id, event_p_id, events, nb_deq, 0);
504 			while (nb_enq < nb_deq && !force_quit)
505 				nb_enq += rte_event_eth_tx_adapter_enqueue(
506 					event_d_id, event_p_id, events + nb_enq,
507 					nb_deq - nb_enq, 0);
508 		}
509 	}
510 
511 	l3fwd_event_worker_cleanup(event_d_id, event_p_id, events, nb_enq,
512 				   nb_deq, 1);
513 }
514 
515 int __rte_noinline
lpm_event_main_loop_tx_d_vector(__rte_unused void * dummy)516 lpm_event_main_loop_tx_d_vector(__rte_unused void *dummy)
517 {
518 	struct l3fwd_event_resources *evt_rsrc = l3fwd_get_eventdev_rsrc();
519 
520 	lpm_event_loop_vector(evt_rsrc, L3FWD_EVENT_TX_DIRECT);
521 	return 0;
522 }
523 
524 int __rte_noinline
lpm_event_main_loop_tx_d_burst_vector(__rte_unused void * dummy)525 lpm_event_main_loop_tx_d_burst_vector(__rte_unused void *dummy)
526 {
527 	struct l3fwd_event_resources *evt_rsrc = l3fwd_get_eventdev_rsrc();
528 
529 	lpm_event_loop_vector(evt_rsrc, L3FWD_EVENT_TX_DIRECT);
530 	return 0;
531 }
532 
533 int __rte_noinline
lpm_event_main_loop_tx_q_vector(__rte_unused void * dummy)534 lpm_event_main_loop_tx_q_vector(__rte_unused void *dummy)
535 {
536 	struct l3fwd_event_resources *evt_rsrc = l3fwd_get_eventdev_rsrc();
537 
538 	lpm_event_loop_vector(evt_rsrc, L3FWD_EVENT_TX_ENQ);
539 	return 0;
540 }
541 
542 int __rte_noinline
lpm_event_main_loop_tx_q_burst_vector(__rte_unused void * dummy)543 lpm_event_main_loop_tx_q_burst_vector(__rte_unused void *dummy)
544 {
545 	struct l3fwd_event_resources *evt_rsrc = l3fwd_get_eventdev_rsrc();
546 
547 	lpm_event_loop_vector(evt_rsrc, L3FWD_EVENT_TX_ENQ);
548 	return 0;
549 }
550 
551 void
setup_lpm(const int socketid)552 setup_lpm(const int socketid)
553 {
554 	struct rte_eth_dev_info dev_info;
555 	struct rte_lpm6_config config;
556 	struct rte_lpm_config config_ipv4;
557 	int i;
558 	int ret;
559 	char s[64];
560 	char abuf[INET6_ADDRSTRLEN];
561 
562 	/* create the LPM table */
563 	config_ipv4.max_rules = IPV4_L3FWD_LPM_MAX_RULES;
564 	config_ipv4.number_tbl8s = IPV4_L3FWD_LPM_NUMBER_TBL8S;
565 	config_ipv4.flags = 0;
566 	snprintf(s, sizeof(s), "IPV4_L3FWD_LPM_%d", socketid);
567 	ipv4_l3fwd_lpm_lookup_struct[socketid] =
568 			rte_lpm_create(s, socketid, &config_ipv4);
569 	if (ipv4_l3fwd_lpm_lookup_struct[socketid] == NULL)
570 		rte_exit(EXIT_FAILURE,
571 			"Unable to create the l3fwd LPM table on socket %d\n",
572 			socketid);
573 
574 	/* populate the LPM table */
575 	for (i = 0; i < route_num_v4; i++) {
576 		struct in_addr in;
577 
578 		/* skip unused ports */
579 		if ((1 << route_base_v4[i].if_out &
580 				enabled_port_mask) == 0)
581 			continue;
582 
583 		rte_eth_dev_info_get(route_base_v4[i].if_out,
584 				     &dev_info);
585 		ret = rte_lpm_add(ipv4_l3fwd_lpm_lookup_struct[socketid],
586 			route_base_v4[i].ip,
587 			route_base_v4[i].depth,
588 			route_base_v4[i].if_out);
589 
590 		if (ret < 0) {
591 			lpm_free_routes();
592 			rte_exit(EXIT_FAILURE,
593 				"Unable to add entry %u to the l3fwd LPM table on socket %d\n",
594 				i, socketid);
595 		}
596 
597 		in.s_addr = htonl(route_base_v4[i].ip);
598 		printf("LPM: Adding route %s / %d (%d) [%s]\n",
599 		       inet_ntop(AF_INET, &in, abuf, sizeof(abuf)),
600 		       route_base_v4[i].depth,
601 		       route_base_v4[i].if_out, dev_info.device->name);
602 	}
603 
604 	/* create the LPM6 table */
605 	snprintf(s, sizeof(s), "IPV6_L3FWD_LPM_%d", socketid);
606 
607 	config.max_rules = IPV6_L3FWD_LPM_MAX_RULES;
608 	config.number_tbl8s = IPV6_L3FWD_LPM_NUMBER_TBL8S;
609 	config.flags = 0;
610 	ipv6_l3fwd_lpm_lookup_struct[socketid] = rte_lpm6_create(s, socketid,
611 				&config);
612 	if (ipv6_l3fwd_lpm_lookup_struct[socketid] == NULL) {
613 		lpm_free_routes();
614 		rte_exit(EXIT_FAILURE,
615 			"Unable to create the l3fwd LPM table on socket %d\n",
616 			socketid);
617 	}
618 
619 	/* populate the LPM table */
620 	for (i = 0; i < route_num_v6; i++) {
621 
622 		/* skip unused ports */
623 		if ((1 << route_base_v6[i].if_out &
624 				enabled_port_mask) == 0)
625 			continue;
626 
627 		rte_eth_dev_info_get(route_base_v6[i].if_out,
628 				     &dev_info);
629 		ret = rte_lpm6_add(ipv6_l3fwd_lpm_lookup_struct[socketid],
630 			route_base_v6[i].ip_8,
631 			route_base_v6[i].depth,
632 			route_base_v6[i].if_out);
633 
634 		if (ret < 0) {
635 			lpm_free_routes();
636 			rte_exit(EXIT_FAILURE,
637 				"Unable to add entry %u to the l3fwd LPM table on socket %d\n",
638 				i, socketid);
639 		}
640 
641 		printf("LPM: Adding route %s / %d (%d) [%s]\n",
642 		       inet_ntop(AF_INET6, route_base_v6[i].ip_8, abuf,
643 				 sizeof(abuf)),
644 		       route_base_v6[i].depth,
645 		       route_base_v6[i].if_out, dev_info.device->name);
646 	}
647 }
648 
649 int
lpm_check_ptype(int portid)650 lpm_check_ptype(int portid)
651 {
652 	int i, ret;
653 	int ptype_l3_ipv4 = 0, ptype_l3_ipv6 = 0;
654 	uint32_t ptype_mask = RTE_PTYPE_L3_MASK;
655 
656 	ret = rte_eth_dev_get_supported_ptypes(portid, ptype_mask, NULL, 0);
657 	if (ret <= 0)
658 		return 0;
659 
660 	uint32_t ptypes[ret];
661 
662 	ret = rte_eth_dev_get_supported_ptypes(portid, ptype_mask, ptypes, ret);
663 	for (i = 0; i < ret; ++i) {
664 		if (ptypes[i] & RTE_PTYPE_L3_IPV4)
665 			ptype_l3_ipv4 = 1;
666 		if (ptypes[i] & RTE_PTYPE_L3_IPV6)
667 			ptype_l3_ipv6 = 1;
668 	}
669 
670 	if (ptype_l3_ipv4 == 0)
671 		printf("port %d cannot parse RTE_PTYPE_L3_IPV4\n", portid);
672 
673 	if (ptype_l3_ipv6 == 0)
674 		printf("port %d cannot parse RTE_PTYPE_L3_IPV6\n", portid);
675 
676 	if (ptype_l3_ipv4 && ptype_l3_ipv6)
677 		return 1;
678 
679 	return 0;
680 
681 }
682 
683 static inline void
lpm_parse_ptype(struct rte_mbuf * m)684 lpm_parse_ptype(struct rte_mbuf *m)
685 {
686 	struct rte_ether_hdr *eth_hdr;
687 	uint32_t packet_type = RTE_PTYPE_UNKNOWN;
688 	uint16_t ether_type;
689 
690 	eth_hdr = rte_pktmbuf_mtod(m, struct rte_ether_hdr *);
691 	ether_type = eth_hdr->ether_type;
692 	if (ether_type == rte_cpu_to_be_16(RTE_ETHER_TYPE_IPV4))
693 		packet_type |= RTE_PTYPE_L3_IPV4_EXT_UNKNOWN;
694 	else if (ether_type == rte_cpu_to_be_16(RTE_ETHER_TYPE_IPV6))
695 		packet_type |= RTE_PTYPE_L3_IPV6_EXT_UNKNOWN;
696 
697 	m->packet_type = packet_type;
698 }
699 
700 uint16_t
lpm_cb_parse_ptype(uint16_t port __rte_unused,uint16_t queue __rte_unused,struct rte_mbuf * pkts[],uint16_t nb_pkts,uint16_t max_pkts __rte_unused,void * user_param __rte_unused)701 lpm_cb_parse_ptype(uint16_t port __rte_unused, uint16_t queue __rte_unused,
702 		   struct rte_mbuf *pkts[], uint16_t nb_pkts,
703 		   uint16_t max_pkts __rte_unused,
704 		   void *user_param __rte_unused)
705 {
706 	unsigned int i;
707 
708 	if (unlikely(nb_pkts == 0))
709 		return nb_pkts;
710 	rte_prefetch0(rte_pktmbuf_mtod(pkts[0], struct ether_hdr *));
711 	for (i = 0; i < (unsigned int) (nb_pkts - 1); ++i) {
712 		rte_prefetch0(rte_pktmbuf_mtod(pkts[i+1],
713 			struct ether_hdr *));
714 		lpm_parse_ptype(pkts[i]);
715 	}
716 	lpm_parse_ptype(pkts[i]);
717 
718 	return nb_pkts;
719 }
720 
721 /* Return ipv4/ipv6 lpm fwd lookup struct. */
722 void *
lpm_get_ipv4_l3fwd_lookup_struct(const int socketid)723 lpm_get_ipv4_l3fwd_lookup_struct(const int socketid)
724 {
725 	return ipv4_l3fwd_lpm_lookup_struct[socketid];
726 }
727 
728 void *
lpm_get_ipv6_l3fwd_lookup_struct(const int socketid)729 lpm_get_ipv6_l3fwd_lookup_struct(const int socketid)
730 {
731 	return ipv6_l3fwd_lpm_lookup_struct[socketid];
732 }
733