examples/ipsec-secgw: use HW parsed packet type in poll mode
[dpdk.git] / examples / l3fwd / l3fwd_altivec.h
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2016 Intel Corporation.
3  * Copyright(c) 2017 IBM Corporation.
4  * All rights reserved.
5  */
6
7 #ifndef _L3FWD_ALTIVEC_H_
8 #define _L3FWD_ALTIVEC_H_
9
10 #include "l3fwd.h"
11 #include "l3fwd_common.h"
12
13 /*
14  * Update source and destination MAC addresses in the ethernet header.
15  * Perform RFC1812 checks and updates for IPV4 packets.
16  */
17 static inline void
18 processx4_step3(struct rte_mbuf *pkt[FWDSTEP], uint16_t dst_port[FWDSTEP])
19 {
20         __vector unsigned int te[FWDSTEP];
21         __vector unsigned int ve[FWDSTEP];
22         __vector unsigned int *p[FWDSTEP];
23
24         p[0] = rte_pktmbuf_mtod(pkt[0], __vector unsigned int *);
25         p[1] = rte_pktmbuf_mtod(pkt[1], __vector unsigned int *);
26         p[2] = rte_pktmbuf_mtod(pkt[2], __vector unsigned int *);
27         p[3] = rte_pktmbuf_mtod(pkt[3], __vector unsigned int *);
28
29         ve[0] = (__vector unsigned int)val_eth[dst_port[0]];
30         te[0] = *p[0];
31
32         ve[1] = (__vector unsigned int)val_eth[dst_port[1]];
33         te[1] = *p[1];
34
35         ve[2] = (__vector unsigned int)val_eth[dst_port[2]];
36         te[2] = *p[2];
37
38         ve[3] = (__vector unsigned int)val_eth[dst_port[3]];
39         te[3] = *p[3];
40
41         /* Update first 12 bytes, keep rest bytes intact. */
42         te[0] = (__vector unsigned int)vec_sel(
43                         (__vector unsigned short)ve[0],
44                         (__vector unsigned short)te[0],
45                         (__vector unsigned short) {0, 0, 0, 0,
46                                                 0, 0, 0xffff, 0xffff});
47
48         te[1] = (__vector unsigned int)vec_sel(
49                         (__vector unsigned short)ve[1],
50                         (__vector unsigned short)te[1],
51                         (__vector unsigned short) {0, 0, 0, 0,
52                                                 0, 0, 0xffff, 0xffff});
53
54         te[2] = (__vector unsigned int)vec_sel(
55                         (__vector unsigned short)ve[2],
56                         (__vector unsigned short)te[2],
57                         (__vector unsigned short) {0, 0, 0, 0, 0,
58                                                 0, 0xffff, 0xffff});
59
60         te[3] = (__vector unsigned int)vec_sel(
61                         (__vector unsigned short)ve[3],
62                         (__vector unsigned short)te[3],
63                         (__vector unsigned short) {0, 0, 0, 0,
64                                                 0, 0, 0xffff, 0xffff});
65
66         *p[0] = te[0];
67         *p[1] = te[1];
68         *p[2] = te[2];
69         *p[3] = te[3];
70
71         rfc1812_process((struct rte_ipv4_hdr *)
72                         ((struct rte_ether_hdr *)p[0] + 1),
73                         &dst_port[0], pkt[0]->packet_type);
74         rfc1812_process((struct rte_ipv4_hdr *)
75                         ((struct rte_ether_hdr *)p[1] + 1),
76                         &dst_port[1], pkt[1]->packet_type);
77         rfc1812_process((struct rte_ipv4_hdr *)
78                         ((struct rte_ether_hdr *)p[2] + 1),
79                         &dst_port[2], pkt[2]->packet_type);
80         rfc1812_process((struct rte_ipv4_hdr *)
81                         ((struct rte_ether_hdr *)p[3] + 1),
82                         &dst_port[3], pkt[3]->packet_type);
83 }
84
85 /*
86  * Group consecutive packets with the same destination port in bursts of 4.
87  * Suppose we have array of destination ports:
88  * dst_port[] = {a, b, c, d,, e, ... }
89  * dp1 should contain: <a, b, c, d>, dp2: <b, c, d, e>.
90  * We doing 4 comparisons at once and the result is 4 bit mask.
91  * This mask is used as an index into prebuild array of pnum values.
92  */
93 static inline uint16_t *
94 port_groupx4(uint16_t pn[FWDSTEP + 1], uint16_t *lp,
95                 __vector unsigned short dp1,
96                 __vector unsigned short dp2)
97 {
98         union {
99                 uint16_t u16[FWDSTEP + 1];
100                 uint64_t u64;
101         } *pnum = (void *)pn;
102
103         int32_t v;
104
105         v = vec_any_eq(dp1, dp2);
106
107
108         /* update last port counter. */
109         lp[0] += gptbl[v].lpv;
110
111         /* if dest port value has changed. */
112         if (v != GRPMSK) {
113                 pnum->u64 = gptbl[v].pnum;
114                 pnum->u16[FWDSTEP] = 1;
115                 lp = pnum->u16 + gptbl[v].idx;
116         }
117
118         return lp;
119 }
120
121 /**
122  * Process one packet:
123  * Update source and destination MAC addresses in the ethernet header.
124  * Perform RFC1812 checks and updates for IPV4 packets.
125  */
126 static inline void
127 process_packet(struct rte_mbuf *pkt, uint16_t *dst_port)
128 {
129         struct rte_ether_hdr *eth_hdr;
130         __vector unsigned int te, ve;
131
132         eth_hdr = rte_pktmbuf_mtod(pkt, struct rte_ether_hdr *);
133
134         te = *(__vector unsigned int *)eth_hdr;
135         ve = (__vector unsigned int)val_eth[dst_port[0]];
136
137         rfc1812_process((struct rte_ipv4_hdr *)(eth_hdr + 1), dst_port,
138                         pkt->packet_type);
139
140         /* dynamically vec_sel te and ve for MASK_ETH (0x3f) */
141         te = (__vector unsigned int)vec_sel(
142                 (__vector unsigned short)ve,
143                 (__vector unsigned short)te,
144                 (__vector unsigned short){0, 0, 0, 0,
145                                         0, 0, 0xffff, 0xffff});
146
147         *(__vector unsigned int *)eth_hdr = te;
148 }
149
150 /**
151  * Send packets burst from pkts_burst to the ports in dst_port array
152  */
153 static __rte_always_inline void
154 send_packets_multi(struct lcore_conf *qconf, struct rte_mbuf **pkts_burst,
155                 uint16_t dst_port[MAX_PKT_BURST], int nb_rx)
156 {
157         int32_t k;
158         int j = 0;
159         uint16_t dlp;
160         uint16_t *lp;
161         uint16_t pnum[MAX_PKT_BURST + 1];
162
163         /*
164          * Finish packet processing and group consecutive
165          * packets with the same destination port.
166          */
167         k = RTE_ALIGN_FLOOR(nb_rx, FWDSTEP);
168         if (k != 0) {
169                 __vector unsigned short dp1, dp2;
170
171                 lp = pnum;
172                 lp[0] = 1;
173
174                 processx4_step3(pkts_burst, dst_port);
175
176                 /* dp1: <d[0], d[1], d[2], d[3], ... > */
177                 dp1 = *(__vector unsigned short *)dst_port;
178
179                 for (j = FWDSTEP; j != k; j += FWDSTEP) {
180                         processx4_step3(&pkts_burst[j], &dst_port[j]);
181
182                         /*
183                          * dp2:
184                          * <d[j-3], d[j-2], d[j-1], d[j], ... >
185                          */
186                         dp2 = *((__vector unsigned short *)
187                                         &dst_port[j - FWDSTEP + 1]);
188                         lp  = port_groupx4(&pnum[j - FWDSTEP], lp, dp1, dp2);
189
190                         /*
191                          * dp1:
192                          * <d[j], d[j+1], d[j+2], d[j+3], ... >
193                          */
194                         dp1 = vec_sro(dp2, (__vector unsigned char) {
195                                 0, 0, 0, 0, 0, 0, 0, 0,
196                                 0, 0, 0, (FWDSTEP - 1) * sizeof(dst_port[0])});
197                 }
198
199                 /*
200                  * dp2: <d[j-3], d[j-2], d[j-1], d[j-1], ... >
201                  */
202                 dp2 = vec_perm(dp1, (__vector unsigned short){},
203                                 (__vector unsigned char){0xf9});
204                 lp  = port_groupx4(&pnum[j - FWDSTEP], lp, dp1, dp2);
205
206                 /*
207                  * remove values added by the last repeated
208                  * dst port.
209                  */
210                 lp[0]--;
211                 dlp = dst_port[j - 1];
212         } else {
213                 /* set dlp and lp to the never used values. */
214                 dlp = BAD_PORT - 1;
215                 lp = pnum + MAX_PKT_BURST;
216         }
217
218         /* Process up to last 3 packets one by one. */
219         switch (nb_rx % FWDSTEP) {
220         case 3:
221                 process_packet(pkts_burst[j], dst_port + j);
222                 GROUP_PORT_STEP(dlp, dst_port, lp, pnum, j);
223                 j++;
224                 /* fall-through */
225         case 2:
226                 process_packet(pkts_burst[j], dst_port + j);
227                 GROUP_PORT_STEP(dlp, dst_port, lp, pnum, j);
228                 j++;
229                 /* fall-through */
230         case 1:
231                 process_packet(pkts_burst[j], dst_port + j);
232                 GROUP_PORT_STEP(dlp, dst_port, lp, pnum, j);
233                 j++;
234         }
235
236         /*
237          * Send packets out, through destination port.
238          * Consecutive packets with the same destination port
239          * are already grouped together.
240          * If destination port for the packet equals BAD_PORT,
241          * then free the packet without sending it out.
242          */
243         for (j = 0; j < nb_rx; j += k) {
244
245                 int32_t m;
246                 uint16_t pn;
247
248                 pn = dst_port[j];
249                 k = pnum[j];
250
251                 if (likely(pn != BAD_PORT))
252                         send_packetsx4(qconf, pn, pkts_burst + j, k);
253                 else
254                         for (m = j; m != j + k; m++)
255                                 rte_pktmbuf_free(pkts_burst[m]);
256
257         }
258 }
259
260 #endif /* _L3FWD_ALTIVEC_H_ */