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.
38 SHUFFLE32_SLOT1 = 0xe5,
39 SHUFFLE32_SLOT2 = 0xe6,
40 SHUFFLE32_SLOT3 = 0xe7,
41 SHUFFLE32_SWAP64 = 0x4e,
44 static const rte_xmm_t xmm_shuffle_input = {
45 .u32 = {0x00000000, 0x04040404, 0x08080808, 0x0c0c0c0c},
48 static const rte_xmm_t xmm_ones_16 = {
49 .u16 = {1, 1, 1, 1, 1, 1, 1, 1},
52 static const rte_xmm_t xmm_match_mask = {
61 static const rte_xmm_t xmm_index_mask = {
71 * Resolve priority for multiple results (sse version).
72 * This consists comparing the priority of the current traversal with the
73 * running set of results for the packet.
74 * For each result, keep a running array of the result (rule number) and
75 * its priority for each category.
78 resolve_priority_sse(uint64_t transition, int n, const struct rte_acl_ctx *ctx,
79 struct parms *parms, const struct rte_acl_match_results *p,
83 xmm_t results, priority, results1, priority1, selector;
84 xmm_t *saved_results, *saved_priority;
86 for (x = 0; x < categories; x += RTE_ACL_RESULTS_MULTIPLIER) {
88 saved_results = (xmm_t *)(&parms[n].cmplt->results[x]);
90 (xmm_t *)(&parms[n].cmplt->priority[x]);
92 /* get results and priorities for completed trie */
93 results = MM_LOADU((const xmm_t *)&p[transition].results[x]);
94 priority = MM_LOADU((const xmm_t *)&p[transition].priority[x]);
96 /* if this is not the first completed trie */
97 if (parms[n].cmplt->count != ctx->num_tries) {
99 /* get running best results and their priorities */
100 results1 = MM_LOADU(saved_results);
101 priority1 = MM_LOADU(saved_priority);
103 /* select results that are highest priority */
104 selector = MM_CMPGT32(priority1, priority);
105 results = MM_BLENDV8(results, results1, selector);
106 priority = MM_BLENDV8(priority, priority1, selector);
109 /* save running best results and their priorities */
110 MM_STOREU(saved_results, results);
111 MM_STOREU(saved_priority, priority);
116 * Extract transitions from an XMM register and check for any matches
119 acl_process_matches(xmm_t *indices, int slot, const struct rte_acl_ctx *ctx,
120 struct parms *parms, struct acl_flow_data *flows)
122 uint64_t transition1, transition2;
124 /* extract transition from low 64 bits. */
125 transition1 = MM_CVT64(*indices);
127 /* extract transition from high 64 bits. */
128 *indices = MM_SHUFFLE32(*indices, SHUFFLE32_SWAP64);
129 transition2 = MM_CVT64(*indices);
131 transition1 = acl_match_check(transition1, slot, ctx,
132 parms, flows, resolve_priority_sse);
133 transition2 = acl_match_check(transition2, slot + 1, ctx,
134 parms, flows, resolve_priority_sse);
136 /* update indices with new transitions. */
137 *indices = MM_SET64(transition2, transition1);
141 * Check for any match in 4 transitions (contained in 2 SSE registers)
143 static inline __attribute__((always_inline)) void
144 acl_match_check_x4(int slot, const struct rte_acl_ctx *ctx, struct parms *parms,
145 struct acl_flow_data *flows, xmm_t *indices1, xmm_t *indices2,
150 /* put low 32 bits of each transition into one register */
151 temp = (xmm_t)MM_SHUFFLEPS((__m128)*indices1, (__m128)*indices2,
153 /* test for match node */
154 temp = MM_AND(match_mask, temp);
156 while (!MM_TESTZ(temp, temp)) {
157 acl_process_matches(indices1, slot, ctx, parms, flows);
158 acl_process_matches(indices2, slot + 2, ctx, parms, flows);
160 temp = (xmm_t)MM_SHUFFLEPS((__m128)*indices1,
163 temp = MM_AND(match_mask, temp);
168 * Calculate the address of the next transition for
169 * all types of nodes. Note that only DFA nodes and range
170 * nodes actually transition to another node. Match
173 static inline __attribute__((always_inline)) xmm_t
174 calc_addr_sse(xmm_t index_mask, xmm_t next_input, xmm_t shuffle_input,
175 xmm_t ones_16, xmm_t tr_lo, xmm_t tr_hi)
177 xmm_t addr, node_types;
178 xmm_t dfa_msk, dfa_ofs, quad_ofs;
181 const xmm_t range_base = _mm_set_epi32(0xffffff0c, 0xffffff08,
182 0xffffff04, 0xffffff00);
185 * Note that no transition is done for a match
186 * node and therefore a stream freezes when
187 * it reaches a match.
190 t = MM_XOR(index_mask, index_mask);
192 /* shuffle input byte to all 4 positions of 32 bit value */
193 in = MM_SHUFFLE8(next_input, shuffle_input);
195 /* Calc node type and node addr */
196 node_types = MM_ANDNOT(index_mask, tr_lo);
197 addr = MM_AND(index_mask, tr_lo);
200 * Calc addr for DFAs - addr = dfa_index + input_byte
203 /* mask for DFA type (0) nodes */
204 dfa_msk = MM_CMPEQ32(node_types, t);
206 r = _mm_srli_epi32(in, 30);
207 r = _mm_add_epi8(r, range_base);
209 t = _mm_srli_epi32(in, 24);
210 r = _mm_shuffle_epi8(tr_hi, r);
212 dfa_ofs = _mm_sub_epi32(t, r);
215 * Calculate number of range boundaries that are less than the
216 * input value. Range boundaries for each node are in signed 8 bit,
217 * ordered from -128 to 127 in the indices2 register.
218 * This is effectively a popcnt of bytes that are greater than the
223 t = MM_CMPGT8(in, tr_hi);
225 /* convert -1 to 1 (bytes greater than input byte */
228 /* horizontal add pairs of bytes into words */
231 /* horizontal add pairs of words into dwords */
232 quad_ofs = MM_MADD16(t, ones_16);
234 /* blend DFA and QUAD/SINGLE. */
235 t = _mm_blendv_epi8(quad_ofs, dfa_ofs, dfa_msk);
237 /* add index into node position */
238 return MM_ADD32(addr, t);
242 * Process 4 transitions (in 2 SIMD registers) in parallel
244 static inline __attribute__((always_inline)) xmm_t
245 transition4(xmm_t next_input, const uint64_t *trans,
246 xmm_t *indices1, xmm_t *indices2)
248 xmm_t addr, tr_lo, tr_hi;
249 uint64_t trans0, trans2;
251 /* Shuffle low 32 into tr_lo and high 32 into tr_hi */
252 tr_lo = (xmm_t)_mm_shuffle_ps((__m128)*indices1, (__m128)*indices2,
254 tr_hi = (xmm_t)_mm_shuffle_ps((__m128)*indices1, (__m128)*indices2,
257 /* Calculate the address (array index) for all 4 transitions. */
259 addr = calc_addr_sse(xmm_index_mask.x, next_input, xmm_shuffle_input.x,
260 xmm_ones_16.x, tr_lo, tr_hi);
262 /* Gather 64 bit transitions and pack back into 2 registers. */
264 trans0 = trans[MM_CVT32(addr)];
268 /* {x0, x1, x2, x3} -> {x2, x1, x2, x3} */
269 addr = MM_SHUFFLE32(addr, SHUFFLE32_SLOT2);
270 trans2 = trans[MM_CVT32(addr)];
274 /* {x2, x1, x2, x3} -> {x1, x1, x2, x3} */
275 addr = MM_SHUFFLE32(addr, SHUFFLE32_SLOT1);
276 *indices1 = MM_SET64(trans[MM_CVT32(addr)], trans0);
280 /* {x1, x1, x2, x3} -> {x3, x1, x2, x3} */
281 addr = MM_SHUFFLE32(addr, SHUFFLE32_SLOT3);
282 *indices2 = MM_SET64(trans[MM_CVT32(addr)], trans2);
284 return MM_SRL32(next_input, CHAR_BIT);
288 * Execute trie traversal with 8 traversals in parallel
291 search_sse_8(const struct rte_acl_ctx *ctx, const uint8_t **data,
292 uint32_t *results, uint32_t total_packets, uint32_t categories)
295 struct acl_flow_data flows;
296 uint64_t index_array[MAX_SEARCHES_SSE8];
297 struct completion cmplt[MAX_SEARCHES_SSE8];
298 struct parms parms[MAX_SEARCHES_SSE8];
299 xmm_t input0, input1;
300 xmm_t indices1, indices2, indices3, indices4;
302 acl_set_flow(&flows, cmplt, RTE_DIM(cmplt), data, results,
303 total_packets, categories, ctx->trans_table);
305 for (n = 0; n < MAX_SEARCHES_SSE8; n++) {
307 index_array[n] = acl_start_next_trie(&flows, parms, n, ctx);
311 * indices1 contains index_array[0,1]
312 * indices2 contains index_array[2,3]
313 * indices3 contains index_array[4,5]
314 * indices4 contains index_array[6,7]
317 indices1 = MM_LOADU((xmm_t *) &index_array[0]);
318 indices2 = MM_LOADU((xmm_t *) &index_array[2]);
320 indices3 = MM_LOADU((xmm_t *) &index_array[4]);
321 indices4 = MM_LOADU((xmm_t *) &index_array[6]);
323 /* Check for any matches. */
324 acl_match_check_x4(0, ctx, parms, &flows,
325 &indices1, &indices2, xmm_match_mask.x);
326 acl_match_check_x4(4, ctx, parms, &flows,
327 &indices3, &indices4, xmm_match_mask.x);
329 while (flows.started > 0) {
331 /* Gather 4 bytes of input data for each stream. */
332 input0 = _mm_cvtsi32_si128(GET_NEXT_4BYTES(parms, 0));
333 input1 = _mm_cvtsi32_si128(GET_NEXT_4BYTES(parms, 4));
335 input0 = MM_INSERT32(input0, GET_NEXT_4BYTES(parms, 1), 1);
336 input1 = MM_INSERT32(input1, GET_NEXT_4BYTES(parms, 5), 1);
338 input0 = MM_INSERT32(input0, GET_NEXT_4BYTES(parms, 2), 2);
339 input1 = MM_INSERT32(input1, GET_NEXT_4BYTES(parms, 6), 2);
341 input0 = MM_INSERT32(input0, GET_NEXT_4BYTES(parms, 3), 3);
342 input1 = MM_INSERT32(input1, GET_NEXT_4BYTES(parms, 7), 3);
344 /* Process the 4 bytes of input on each stream. */
346 input0 = transition4(input0, flows.trans,
347 &indices1, &indices2);
348 input1 = transition4(input1, flows.trans,
349 &indices3, &indices4);
351 input0 = transition4(input0, flows.trans,
352 &indices1, &indices2);
353 input1 = transition4(input1, flows.trans,
354 &indices3, &indices4);
356 input0 = transition4(input0, flows.trans,
357 &indices1, &indices2);
358 input1 = transition4(input1, flows.trans,
359 &indices3, &indices4);
361 input0 = transition4(input0, flows.trans,
362 &indices1, &indices2);
363 input1 = transition4(input1, flows.trans,
364 &indices3, &indices4);
366 /* Check for any matches. */
367 acl_match_check_x4(0, ctx, parms, &flows,
368 &indices1, &indices2, xmm_match_mask.x);
369 acl_match_check_x4(4, ctx, parms, &flows,
370 &indices3, &indices4, xmm_match_mask.x);
377 * Execute trie traversal with 4 traversals in parallel
380 search_sse_4(const struct rte_acl_ctx *ctx, const uint8_t **data,
381 uint32_t *results, int total_packets, uint32_t categories)
384 struct acl_flow_data flows;
385 uint64_t index_array[MAX_SEARCHES_SSE4];
386 struct completion cmplt[MAX_SEARCHES_SSE4];
387 struct parms parms[MAX_SEARCHES_SSE4];
388 xmm_t input, indices1, indices2;
390 acl_set_flow(&flows, cmplt, RTE_DIM(cmplt), data, results,
391 total_packets, categories, ctx->trans_table);
393 for (n = 0; n < MAX_SEARCHES_SSE4; n++) {
395 index_array[n] = acl_start_next_trie(&flows, parms, n, ctx);
398 indices1 = MM_LOADU((xmm_t *) &index_array[0]);
399 indices2 = MM_LOADU((xmm_t *) &index_array[2]);
401 /* Check for any matches. */
402 acl_match_check_x4(0, ctx, parms, &flows,
403 &indices1, &indices2, xmm_match_mask.x);
405 while (flows.started > 0) {
407 /* Gather 4 bytes of input data for each stream. */
408 input = _mm_cvtsi32_si128(GET_NEXT_4BYTES(parms, 0));
409 input = MM_INSERT32(input, GET_NEXT_4BYTES(parms, 1), 1);
410 input = MM_INSERT32(input, GET_NEXT_4BYTES(parms, 2), 2);
411 input = MM_INSERT32(input, GET_NEXT_4BYTES(parms, 3), 3);
413 /* Process the 4 bytes of input on each stream. */
414 input = transition4(input, flows.trans, &indices1, &indices2);
415 input = transition4(input, flows.trans, &indices1, &indices2);
416 input = transition4(input, flows.trans, &indices1, &indices2);
417 input = transition4(input, flows.trans, &indices1, &indices2);
419 /* Check for any matches. */
420 acl_match_check_x4(0, ctx, parms, &flows,
421 &indices1, &indices2, xmm_match_mask.x);