xref: /f-stack/dpdk/examples/l3fwd/l3fwd_lpm_sse.h (revision 4418919f)
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2010-2016 Intel Corporation
3  */
4 
5 #ifndef __L3FWD_LPM_SSE_H__
6 #define __L3FWD_LPM_SSE_H__
7 
8 #include "l3fwd_sse.h"
9 
10 /*
11  * Read packet_type and destination IPV4 addresses from 4 mbufs.
12  */
13 static inline void
processx4_step1(struct rte_mbuf * pkt[FWDSTEP],__m128i * dip,uint32_t * ipv4_flag)14 processx4_step1(struct rte_mbuf *pkt[FWDSTEP],
15 		__m128i *dip,
16 		uint32_t *ipv4_flag)
17 {
18 	struct rte_ipv4_hdr *ipv4_hdr;
19 	struct rte_ether_hdr *eth_hdr;
20 	uint32_t x0, x1, x2, x3;
21 
22 	eth_hdr = rte_pktmbuf_mtod(pkt[0], struct rte_ether_hdr *);
23 	ipv4_hdr = (struct rte_ipv4_hdr *)(eth_hdr + 1);
24 	x0 = ipv4_hdr->dst_addr;
25 	ipv4_flag[0] = pkt[0]->packet_type & RTE_PTYPE_L3_IPV4;
26 
27 	eth_hdr = rte_pktmbuf_mtod(pkt[1], struct rte_ether_hdr *);
28 	ipv4_hdr = (struct rte_ipv4_hdr *)(eth_hdr + 1);
29 	x1 = ipv4_hdr->dst_addr;
30 	ipv4_flag[0] &= pkt[1]->packet_type;
31 
32 	eth_hdr = rte_pktmbuf_mtod(pkt[2], struct rte_ether_hdr *);
33 	ipv4_hdr = (struct rte_ipv4_hdr *)(eth_hdr + 1);
34 	x2 = ipv4_hdr->dst_addr;
35 	ipv4_flag[0] &= pkt[2]->packet_type;
36 
37 	eth_hdr = rte_pktmbuf_mtod(pkt[3], struct rte_ether_hdr *);
38 	ipv4_hdr = (struct rte_ipv4_hdr *)(eth_hdr + 1);
39 	x3 = ipv4_hdr->dst_addr;
40 	ipv4_flag[0] &= pkt[3]->packet_type;
41 
42 	dip[0] = _mm_set_epi32(x3, x2, x1, x0);
43 }
44 
45 /*
46  * Lookup into LPM for destination port.
47  * If lookup fails, use incoming port (portid) as destination port.
48  */
49 static inline void
processx4_step2(const struct lcore_conf * qconf,__m128i dip,uint32_t ipv4_flag,uint16_t portid,struct rte_mbuf * pkt[FWDSTEP],uint16_t dprt[FWDSTEP])50 processx4_step2(const struct lcore_conf *qconf,
51 		__m128i dip,
52 		uint32_t ipv4_flag,
53 		uint16_t portid,
54 		struct rte_mbuf *pkt[FWDSTEP],
55 		uint16_t dprt[FWDSTEP])
56 {
57 	rte_xmm_t dst;
58 	const  __m128i bswap_mask = _mm_set_epi8(12, 13, 14, 15, 8, 9, 10, 11,
59 						4, 5, 6, 7, 0, 1, 2, 3);
60 
61 	/* Byte swap 4 IPV4 addresses. */
62 	dip = _mm_shuffle_epi8(dip, bswap_mask);
63 
64 	/* if all 4 packets are IPV4. */
65 	if (likely(ipv4_flag)) {
66 		rte_lpm_lookupx4(qconf->ipv4_lookup_struct, dip, dst.u32,
67 			portid);
68 		/* get rid of unused upper 16 bit for each dport. */
69 		dst.x = _mm_packs_epi32(dst.x, dst.x);
70 		*(uint64_t *)dprt = dst.u64[0];
71 	} else {
72 		dst.x = dip;
73 		dprt[0] = lpm_get_dst_port_with_ipv4(qconf, pkt[0], dst.u32[0], portid);
74 		dprt[1] = lpm_get_dst_port_with_ipv4(qconf, pkt[1], dst.u32[1], portid);
75 		dprt[2] = lpm_get_dst_port_with_ipv4(qconf, pkt[2], dst.u32[2], portid);
76 		dprt[3] = lpm_get_dst_port_with_ipv4(qconf, pkt[3], dst.u32[3], portid);
77 	}
78 }
79 
80 /*
81  * Buffer optimized handling of packets, invoked
82  * from main_loop.
83  */
84 static inline void
l3fwd_lpm_send_packets(int nb_rx,struct rte_mbuf ** pkts_burst,uint16_t portid,struct lcore_conf * qconf)85 l3fwd_lpm_send_packets(int nb_rx, struct rte_mbuf **pkts_burst,
86 			uint16_t portid, struct lcore_conf *qconf)
87 {
88 	int32_t j;
89 	uint16_t dst_port[MAX_PKT_BURST];
90 	__m128i dip[MAX_PKT_BURST / FWDSTEP];
91 	uint32_t ipv4_flag[MAX_PKT_BURST / FWDSTEP];
92 	const int32_t k = RTE_ALIGN_FLOOR(nb_rx, FWDSTEP);
93 
94 	for (j = 0; j != k; j += FWDSTEP)
95 		processx4_step1(&pkts_burst[j], &dip[j / FWDSTEP],
96 				&ipv4_flag[j / FWDSTEP]);
97 
98 	for (j = 0; j != k; j += FWDSTEP)
99 		processx4_step2(qconf, dip[j / FWDSTEP],
100 				ipv4_flag[j / FWDSTEP], portid, &pkts_burst[j], &dst_port[j]);
101 
102 	/* Classify last up to 3 packets one by one */
103 	switch (nb_rx % FWDSTEP) {
104 	case 3:
105 		dst_port[j] = lpm_get_dst_port(qconf, pkts_burst[j], portid);
106 		j++;
107 		/* fall-through */
108 	case 2:
109 		dst_port[j] = lpm_get_dst_port(qconf, pkts_burst[j], portid);
110 		j++;
111 		/* fall-through */
112 	case 1:
113 		dst_port[j] = lpm_get_dst_port(qconf, pkts_burst[j], portid);
114 		j++;
115 	}
116 
117 	send_packets_multi(qconf, pkts_burst, dst_port, nb_rx);
118 }
119 
120 #endif /* __L3FWD_LPM_SSE_H__ */
121