4 * Copyright(c) 2016 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 /* rte_cuckoo_hash_x86.h
35 * This file holds all x86 specific Cuckoo Hash functions
38 /* Only tries to insert at one bucket (@prim_bkt) without trying to push
41 static inline unsigned
42 rte_hash_cuckoo_insert_mw_tm(struct rte_hash_bucket *prim_bkt,
43 hash_sig_t sig, hash_sig_t alt_hash, uint32_t new_idx)
48 while (try < RTE_HASH_TSX_MAX_RETRY) {
49 status = rte_xbegin();
50 if (likely(status == RTE_XBEGIN_STARTED)) {
51 /* Insert new entry if there is room in the primary
54 for (i = 0; i < RTE_HASH_BUCKET_ENTRIES; i++) {
55 /* Check if slot is available */
56 if (likely(prim_bkt->signatures[i].sig ==
58 prim_bkt->signatures[i].current = sig;
59 prim_bkt->signatures[i].alt = alt_hash;
60 prim_bkt->key_idx[i] = new_idx;
66 if (i != RTE_HASH_BUCKET_ENTRIES)
69 break; /* break off try loop if transaction commits */
71 /* If we abort we give up this cuckoo path. */
80 /* Shift buckets along provided cuckoo_path (@leaf and @leaf_slot) and fill
81 * the path head with new entry (sig, alt_hash, new_idx)
84 rte_hash_cuckoo_move_insert_mw_tm(const struct rte_hash *h,
85 struct queue_node *leaf, uint32_t leaf_slot,
86 hash_sig_t sig, hash_sig_t alt_hash, uint32_t new_idx)
90 uint32_t prev_alt_bkt_idx;
92 struct queue_node *prev_node, *curr_node = leaf;
93 struct rte_hash_bucket *prev_bkt, *curr_bkt = leaf->bkt;
94 uint32_t prev_slot, curr_slot = leaf_slot;
96 while (try < RTE_HASH_TSX_MAX_RETRY) {
97 status = rte_xbegin();
98 if (likely(status == RTE_XBEGIN_STARTED)) {
99 while (likely(curr_node->prev != NULL)) {
100 prev_node = curr_node->prev;
101 prev_bkt = prev_node->bkt;
102 prev_slot = curr_node->prev_slot;
105 = prev_bkt->signatures[prev_slot].alt
108 if (unlikely(&h->buckets[prev_alt_bkt_idx]
110 rte_xabort(RTE_XABORT_CUCKOO_PATH_INVALIDED);
113 /* Need to swap current/alt sig to allow later
114 * Cuckoo insert to move elements back to its
115 * primary bucket if available
117 curr_bkt->signatures[curr_slot].alt =
118 prev_bkt->signatures[prev_slot].current;
119 curr_bkt->signatures[curr_slot].current =
120 prev_bkt->signatures[prev_slot].alt;
121 curr_bkt->key_idx[curr_slot]
122 = prev_bkt->key_idx[prev_slot];
124 curr_slot = prev_slot;
125 curr_node = prev_node;
126 curr_bkt = curr_node->bkt;
129 curr_bkt->signatures[curr_slot].current = sig;
130 curr_bkt->signatures[curr_slot].alt = alt_hash;
131 curr_bkt->key_idx[curr_slot] = new_idx;
138 /* If we abort we give up this cuckoo path, since most likely it's
139 * no longer valid as TSX detected data conflict
149 * Make space for new key, using bfs Cuckoo Search and Multi-Writer safe
153 rte_hash_cuckoo_make_space_mw_tm(const struct rte_hash *h,
154 struct rte_hash_bucket *bkt,
155 hash_sig_t sig, hash_sig_t alt_hash,
159 struct queue_node queue[RTE_HASH_BFS_QUEUE_MAX_LEN];
160 struct queue_node *tail, *head;
161 struct rte_hash_bucket *curr_bkt, *alt_bkt;
167 tail->prev_slot = -1;
169 /* Cuckoo bfs Search */
170 while (likely(tail != head && head <
171 queue + RTE_HASH_BFS_QUEUE_MAX_LEN - 4)) {
172 curr_bkt = tail->bkt;
173 for (i = 0; i < RTE_HASH_BUCKET_ENTRIES; i++) {
174 if (curr_bkt->signatures[i].sig == NULL_SIGNATURE) {
175 if (likely(rte_hash_cuckoo_move_insert_mw_tm(h,
177 alt_hash, new_idx) == 0))
181 /* Enqueue new node and keep prev node info */
182 alt_bkt = &(h->buckets[curr_bkt->signatures[i].alt
183 & h->bucket_bitmask]);