xref: /dpdk/examples/l3fwd/l3fwd_lpm_sse.h (revision ebab0e8b2257aa049dd35dedc7efd230b0f45b88)
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
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
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
85 l3fwd_lpm_process_packets(int nb_rx, struct rte_mbuf **pkts_burst,
86 			  uint16_t portid, uint16_t *dst_port,
87 			  struct lcore_conf *qconf, const uint8_t do_step3)
88 {
89 	int32_t j;
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 	if (do_step3)
103 		for (j = 0; j != k; j += FWDSTEP)
104 			processx4_step3(&pkts_burst[j], &dst_port[j]);
105 
106 	/* Classify last up to 3 packets one by one */
107 	switch (nb_rx % FWDSTEP) {
108 	case 3:
109 		dst_port[j] = lpm_get_dst_port(qconf, pkts_burst[j], portid);
110 		if (do_step3)
111 			process_packet(pkts_burst[j], &dst_port[j]);
112 		j++;
113 		/* fall-through */
114 	case 2:
115 		dst_port[j] = lpm_get_dst_port(qconf, pkts_burst[j], portid);
116 		if (do_step3)
117 			process_packet(pkts_burst[j], &dst_port[j]);
118 		j++;
119 		/* fall-through */
120 	case 1:
121 		dst_port[j] = lpm_get_dst_port(qconf, pkts_burst[j], portid);
122 		if (do_step3)
123 			process_packet(pkts_burst[j], &dst_port[j]);
124 		j++;
125 	}
126 }
127 
128 static inline void
129 l3fwd_lpm_send_packets(int nb_rx, struct rte_mbuf **pkts_burst, uint16_t portid,
130 		       struct lcore_conf *qconf)
131 {
132 	uint16_t dst_port[SENDM_PORT_OVERHEAD(MAX_PKT_BURST)];
133 
134 	l3fwd_lpm_process_packets(nb_rx, pkts_burst, portid, dst_port, qconf,
135 				  0);
136 	send_packets_multi(qconf, pkts_burst, dst_port, nb_rx);
137 }
138 
139 #endif /* __L3FWD_LPM_SSE_H__ */
140