Imported Upstream version 16.04
[deb_dpdk.git] / lib / librte_acl / acl_run_avx2.h
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
5  *   All rights reserved.
6  *
7  *   Redistribution and use in source and binary forms, with or without
8  *   modification, are permitted provided that the following conditions
9  *   are met:
10  *
11  *     * Redistributions of source code must retain the above copyright
12  *       notice, this list of conditions and the following disclaimer.
13  *     * Redistributions in binary form must reproduce the above copyright
14  *       notice, this list of conditions and the following disclaimer in
15  *       the documentation and/or other materials provided with the
16  *       distribution.
17  *     * Neither the name of Intel Corporation nor the names of its
18  *       contributors may be used to endorse or promote products derived
19  *       from this software without specific prior written permission.
20  *
21  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33
34 #include "acl_run_sse.h"
35
36 static const rte_ymm_t ymm_match_mask = {
37         .u32 = {
38                 RTE_ACL_NODE_MATCH,
39                 RTE_ACL_NODE_MATCH,
40                 RTE_ACL_NODE_MATCH,
41                 RTE_ACL_NODE_MATCH,
42                 RTE_ACL_NODE_MATCH,
43                 RTE_ACL_NODE_MATCH,
44                 RTE_ACL_NODE_MATCH,
45                 RTE_ACL_NODE_MATCH,
46         },
47 };
48
49 static const rte_ymm_t ymm_index_mask = {
50         .u32 = {
51                 RTE_ACL_NODE_INDEX,
52                 RTE_ACL_NODE_INDEX,
53                 RTE_ACL_NODE_INDEX,
54                 RTE_ACL_NODE_INDEX,
55                 RTE_ACL_NODE_INDEX,
56                 RTE_ACL_NODE_INDEX,
57                 RTE_ACL_NODE_INDEX,
58                 RTE_ACL_NODE_INDEX,
59         },
60 };
61
62 static const rte_ymm_t ymm_shuffle_input = {
63         .u32 = {
64                 0x00000000, 0x04040404, 0x08080808, 0x0c0c0c0c,
65                 0x00000000, 0x04040404, 0x08080808, 0x0c0c0c0c,
66         },
67 };
68
69 static const rte_ymm_t ymm_ones_16 = {
70         .u16 = {
71                 1, 1, 1, 1, 1, 1, 1, 1,
72                 1, 1, 1, 1, 1, 1, 1, 1,
73         },
74 };
75
76 static const rte_ymm_t ymm_range_base = {
77         .u32 = {
78                 0xffffff00, 0xffffff04, 0xffffff08, 0xffffff0c,
79                 0xffffff00, 0xffffff04, 0xffffff08, 0xffffff0c,
80         },
81 };
82
83 /*
84  * Process 8 transitions in parallel.
85  * tr_lo contains low 32 bits for 8 transition.
86  * tr_hi contains high 32 bits for 8 transition.
87  * next_input contains up to 4 input bytes for 8 flows.
88  */
89 static inline __attribute__((always_inline)) ymm_t
90 transition8(ymm_t next_input, const uint64_t *trans, ymm_t *tr_lo, ymm_t *tr_hi)
91 {
92         const int32_t *tr;
93         ymm_t addr;
94
95         tr = (const int32_t *)(uintptr_t)trans;
96
97         /* Calculate the address (array index) for all 8 transitions. */
98         ACL_TR_CALC_ADDR(mm256, 256, addr, ymm_index_mask.y, next_input,
99                 ymm_shuffle_input.y, ymm_ones_16.y, ymm_range_base.y,
100                 *tr_lo, *tr_hi);
101
102         /* load lower 32 bits of 8 transactions at once. */
103         *tr_lo = _mm256_i32gather_epi32(tr, addr, sizeof(trans[0]));
104
105         next_input = _mm256_srli_epi32(next_input, CHAR_BIT);
106
107         /* load high 32 bits of 8 transactions at once. */
108         *tr_hi = _mm256_i32gather_epi32(tr + 1, addr, sizeof(trans[0]));
109
110         return next_input;
111 }
112
113 /*
114  * Process matches for  8 flows.
115  * tr_lo contains low 32 bits for 8 transition.
116  * tr_hi contains high 32 bits for 8 transition.
117  */
118 static inline void
119 acl_process_matches_avx2x8(const struct rte_acl_ctx *ctx,
120         struct parms *parms, struct acl_flow_data *flows, uint32_t slot,
121         ymm_t matches, ymm_t *tr_lo, ymm_t *tr_hi)
122 {
123         ymm_t t0, t1;
124         ymm_t lo, hi;
125         xmm_t l0, l1;
126         uint32_t i;
127         uint64_t tr[MAX_SEARCHES_SSE8];
128
129         l1 = _mm256_extracti128_si256(*tr_lo, 1);
130         l0 = _mm256_castsi256_si128(*tr_lo);
131
132         for (i = 0; i != RTE_DIM(tr) / 2; i++) {
133
134                 /*
135                  * Extract low 32bits of each transition.
136                  * That's enough to process the match.
137                  */
138                 tr[i] = (uint32_t)_mm_cvtsi128_si32(l0);
139                 tr[i + 4] = (uint32_t)_mm_cvtsi128_si32(l1);
140
141                 l0 = _mm_srli_si128(l0, sizeof(uint32_t));
142                 l1 = _mm_srli_si128(l1, sizeof(uint32_t));
143
144                 tr[i] = acl_match_check(tr[i], slot + i,
145                         ctx, parms, flows, resolve_priority_sse);
146                 tr[i + 4] = acl_match_check(tr[i + 4], slot + i + 4,
147                         ctx, parms, flows, resolve_priority_sse);
148         }
149
150         /* Collect new transitions into 2 YMM registers. */
151         t0 = _mm256_set_epi64x(tr[5], tr[4], tr[1], tr[0]);
152         t1 = _mm256_set_epi64x(tr[7], tr[6], tr[3], tr[2]);
153
154         /* For each transition: put low 32 into tr_lo and high 32 into tr_hi */
155         ACL_TR_HILO(mm256, __m256, t0, t1, lo, hi);
156
157         /* Keep transitions wth NOMATCH intact. */
158         *tr_lo = _mm256_blendv_epi8(*tr_lo, lo, matches);
159         *tr_hi = _mm256_blendv_epi8(*tr_hi, hi, matches);
160 }
161
162 static inline void
163 acl_match_check_avx2x8(const struct rte_acl_ctx *ctx, struct parms *parms,
164         struct acl_flow_data *flows, uint32_t slot,
165         ymm_t *tr_lo, ymm_t *tr_hi, ymm_t match_mask)
166 {
167         uint32_t msk;
168         ymm_t matches, temp;
169
170         /* test for match node */
171         temp = _mm256_and_si256(match_mask, *tr_lo);
172         matches = _mm256_cmpeq_epi32(temp, match_mask);
173         msk = _mm256_movemask_epi8(matches);
174
175         while (msk != 0) {
176
177                 acl_process_matches_avx2x8(ctx, parms, flows, slot,
178                         matches, tr_lo, tr_hi);
179                 temp = _mm256_and_si256(match_mask, *tr_lo);
180                 matches = _mm256_cmpeq_epi32(temp, match_mask);
181                 msk = _mm256_movemask_epi8(matches);
182         }
183 }
184
185 /*
186  * Execute trie traversal for up to 16 flows in parallel.
187  */
188 static inline int
189 search_avx2x16(const struct rte_acl_ctx *ctx, const uint8_t **data,
190         uint32_t *results, uint32_t total_packets, uint32_t categories)
191 {
192         uint32_t n;
193         struct acl_flow_data flows;
194         uint64_t index_array[MAX_SEARCHES_AVX16];
195         struct completion cmplt[MAX_SEARCHES_AVX16];
196         struct parms parms[MAX_SEARCHES_AVX16];
197         ymm_t input[2], tr_lo[2], tr_hi[2];
198         ymm_t t0, t1;
199
200         acl_set_flow(&flows, cmplt, RTE_DIM(cmplt), data, results,
201                 total_packets, categories, ctx->trans_table);
202
203         for (n = 0; n < RTE_DIM(cmplt); n++) {
204                 cmplt[n].count = 0;
205                 index_array[n] = acl_start_next_trie(&flows, parms, n, ctx);
206         }
207
208         t0 = _mm256_set_epi64x(index_array[5], index_array[4],
209                 index_array[1], index_array[0]);
210         t1 = _mm256_set_epi64x(index_array[7], index_array[6],
211                 index_array[3], index_array[2]);
212
213         ACL_TR_HILO(mm256, __m256, t0, t1, tr_lo[0], tr_hi[0]);
214
215         t0 = _mm256_set_epi64x(index_array[13], index_array[12],
216                 index_array[9], index_array[8]);
217         t1 = _mm256_set_epi64x(index_array[15], index_array[14],
218                 index_array[11], index_array[10]);
219
220         ACL_TR_HILO(mm256, __m256, t0, t1, tr_lo[1], tr_hi[1]);
221
222          /* Check for any matches. */
223         acl_match_check_avx2x8(ctx, parms, &flows, 0, &tr_lo[0], &tr_hi[0],
224                 ymm_match_mask.y);
225         acl_match_check_avx2x8(ctx, parms, &flows, 8, &tr_lo[1], &tr_hi[1],
226                 ymm_match_mask.y);
227
228         while (flows.started > 0) {
229
230                 uint32_t in[MAX_SEARCHES_SSE8];
231
232                 /* Gather 4 bytes of input data for first 8 flows. */
233                 in[0] = GET_NEXT_4BYTES(parms, 0);
234                 in[4] = GET_NEXT_4BYTES(parms, 4);
235                 in[1] = GET_NEXT_4BYTES(parms, 1);
236                 in[5] = GET_NEXT_4BYTES(parms, 5);
237                 in[2] = GET_NEXT_4BYTES(parms, 2);
238                 in[6] = GET_NEXT_4BYTES(parms, 6);
239                 in[3] = GET_NEXT_4BYTES(parms, 3);
240                 in[7] = GET_NEXT_4BYTES(parms, 7);
241                 input[0] = _mm256_set_epi32(in[7], in[6], in[5], in[4],
242                         in[3], in[2], in[1], in[0]);
243
244                 /* Gather 4 bytes of input data for last 8 flows. */
245                 in[0] = GET_NEXT_4BYTES(parms, 8);
246                 in[4] = GET_NEXT_4BYTES(parms, 12);
247                 in[1] = GET_NEXT_4BYTES(parms, 9);
248                 in[5] = GET_NEXT_4BYTES(parms, 13);
249                 in[2] = GET_NEXT_4BYTES(parms, 10);
250                 in[6] = GET_NEXT_4BYTES(parms, 14);
251                 in[3] = GET_NEXT_4BYTES(parms, 11);
252                 in[7] = GET_NEXT_4BYTES(parms, 15);
253                 input[1] = _mm256_set_epi32(in[7], in[6], in[5], in[4],
254                         in[3], in[2], in[1], in[0]);
255
256                 input[0] = transition8(input[0], flows.trans,
257                         &tr_lo[0], &tr_hi[0]);
258                 input[1] = transition8(input[1], flows.trans,
259                         &tr_lo[1], &tr_hi[1]);
260
261                 input[0] = transition8(input[0], flows.trans,
262                         &tr_lo[0], &tr_hi[0]);
263                 input[1] = transition8(input[1], flows.trans,
264                         &tr_lo[1], &tr_hi[1]);
265
266                 input[0] = transition8(input[0], flows.trans,
267                         &tr_lo[0], &tr_hi[0]);
268                 input[1] = transition8(input[1], flows.trans,
269                         &tr_lo[1], &tr_hi[1]);
270
271                 input[0] = transition8(input[0], flows.trans,
272                         &tr_lo[0], &tr_hi[0]);
273                 input[1] = transition8(input[1], flows.trans,
274                         &tr_lo[1], &tr_hi[1]);
275
276                  /* Check for any matches. */
277                 acl_match_check_avx2x8(ctx, parms, &flows, 0,
278                         &tr_lo[0], &tr_hi[0], ymm_match_mask.y);
279                 acl_match_check_avx2x8(ctx, parms, &flows, 8,
280                         &tr_lo[1], &tr_hi[1], ymm_match_mask.y);
281         }
282
283         return 0;
284 }