4 * Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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
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.
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.
34 #include "acl_run_sse.h"
36 static const rte_ymm_t ymm_match_mask = {
49 static const rte_ymm_t ymm_index_mask = {
62 static const rte_ymm_t ymm_shuffle_input = {
64 0x00000000, 0x04040404, 0x08080808, 0x0c0c0c0c,
65 0x00000000, 0x04040404, 0x08080808, 0x0c0c0c0c,
69 static const rte_ymm_t ymm_ones_16 = {
71 1, 1, 1, 1, 1, 1, 1, 1,
72 1, 1, 1, 1, 1, 1, 1, 1,
76 static const rte_ymm_t ymm_range_base = {
78 0xffffff00, 0xffffff04, 0xffffff08, 0xffffff0c,
79 0xffffff00, 0xffffff04, 0xffffff08, 0xffffff0c,
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.
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)
95 tr = (const int32_t *)(uintptr_t)trans;
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,
102 /* load lower 32 bits of 8 transactions at once. */
103 *tr_lo = _mm256_i32gather_epi32(tr, addr, sizeof(trans[0]));
105 next_input = _mm256_srli_epi32(next_input, CHAR_BIT);
107 /* load high 32 bits of 8 transactions at once. */
108 *tr_hi = _mm256_i32gather_epi32(tr + 1, addr, sizeof(trans[0]));
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.
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)
127 uint64_t tr[MAX_SEARCHES_SSE8];
129 l1 = _mm256_extracti128_si256(*tr_lo, 1);
130 l0 = _mm256_castsi256_si128(*tr_lo);
132 for (i = 0; i != RTE_DIM(tr) / 2; i++) {
135 * Extract low 32bits of each transition.
136 * That's enough to process the match.
138 tr[i] = (uint32_t)_mm_cvtsi128_si32(l0);
139 tr[i + 4] = (uint32_t)_mm_cvtsi128_si32(l1);
141 l0 = _mm_srli_si128(l0, sizeof(uint32_t));
142 l1 = _mm_srli_si128(l1, sizeof(uint32_t));
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);
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]);
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);
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);
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)
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);
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);
186 * Execute trie traversal for up to 16 flows in parallel.
189 search_avx2x16(const struct rte_acl_ctx *ctx, const uint8_t **data,
190 uint32_t *results, uint32_t total_packets, uint32_t categories)
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];
200 acl_set_flow(&flows, cmplt, RTE_DIM(cmplt), data, results,
201 total_packets, categories, ctx->trans_table);
203 for (n = 0; n < RTE_DIM(cmplt); n++) {
205 index_array[n] = acl_start_next_trie(&flows, parms, n, ctx);
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]);
213 ACL_TR_HILO(mm256, __m256, t0, t1, tr_lo[0], tr_hi[0]);
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]);
220 ACL_TR_HILO(mm256, __m256, t0, t1, tr_lo[1], tr_hi[1]);
222 /* Check for any matches. */
223 acl_match_check_avx2x8(ctx, parms, &flows, 0, &tr_lo[0], &tr_hi[0],
225 acl_match_check_avx2x8(ctx, parms, &flows, 8, &tr_lo[1], &tr_hi[1],
228 while (flows.started > 0) {
230 uint32_t in[MAX_SEARCHES_SSE8];
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]);
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]);
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]);
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]);
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]);
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]);
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);