aboutsummaryrefslogtreecommitdiff
path: root/helper/cuckootable.c
blob: 32800911a2d2e25bc539504c04eeeec928f03d68 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
/* Copyright (c) 2016, Linaro Limited
 * All rights reserved.
 *
 * SPDX-License-Identifier:     BSD-3-Clause
 */

/*-
 *   BSD LICENSE
 *
 *   Copyright(c) 2010-2016 Intel Corporation. All rights reserved.
 *   All rights reserved.
 *
 *   Redistribution and use in source and binary forms, with or without
 *   modification, are permitted provided that the following conditions
 *   are met:
 *
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in
 *       the documentation and/or other materials provided with the
 *       distribution.
 *     * Neither the name of Intel Corporation nor the names of its
 *       contributors may be used to endorse or promote products derived
 *       from this software without specific prior written permission.
 *
 *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#include <string.h>
#include <stdint.h>
#include <errno.h>
#include <stdio.h>

#include "odp/helper/odph_cuckootable.h"
#include "odph_debug.h"
#include <odp_api.h>

/* More efficient access to a map of single ullong */
#define ULLONG_FOR_EACH_1(IDX, MAP)	\
	for (; MAP && (((IDX) = __builtin_ctzll(MAP)), true); \
		 MAP = (MAP & (MAP - 1)))

/** @magic word, write to the first byte of the memory block
 *   to indicate this block is used by a cuckoo hash table
 */
#define ODPH_CUCKOO_TABLE_MAGIC_WORD 0xDFDFFDFD

/** Number of items per bucket. */
#define HASH_BUCKET_ENTRIES		4

#define NULL_SIGNATURE			0
#define KEY_ALIGNMENT			16

/** Maximum size of hash table that can be created. */
#define HASH_ENTRIES_MAX        1048576

/** @internal signature struct
 *   Structure storing both primary and secondary hashes
 */
struct cuckoo_table_signatures {
	union {
		struct {
			uint32_t current;
			uint32_t alt;
		};
		uint64_t sig;
	};
};

/** @internal kay-value struct
 *   Structure that stores key-value pair
 */
struct cuckoo_table_key_value {
	uint8_t *key;
	uint8_t *value;
};

/** @internal bucket structure
 *  Put the elements with defferent keys but a same signature
 *  into a bucket, and each bucket has at most HASH_BUCKET_ENTRIES
 *  elements.
 */
struct cuckoo_table_bucket {
	struct cuckoo_table_signatures signatures[HASH_BUCKET_ENTRIES];
	/* Includes dummy key index that always contains index 0 */
	odp_buffer_t key_buf[HASH_BUCKET_ENTRIES + 1];
	uint8_t flag[HASH_BUCKET_ENTRIES];
} ODP_ALIGNED_CACHE;

/* More efficient access to a map of single ullong */
#define ULLONG_FOR_EACH_1(IDX, MAP)	\
	for (; MAP && (((IDX) = __builtin_ctzll(MAP)), true); \
		 MAP = (MAP & (MAP - 1)))

/** A hash table structure. */
typedef struct {
	/**< for check */
	uint32_t magicword;
	/**< Name of the hash. */
	char name[ODPH_TABLE_NAME_LEN];
	/**< Total table entries. */
	uint32_t entries;
	/**< Number of buckets in table. */
	uint32_t num_buckets;
	/**< Length of hash key. */
	uint32_t key_len;
	/**< Length of value. */
	uint32_t value_len;
	/**< Bitmask for getting bucket index from hash signature. */
	uint32_t bucket_bitmask;
	/**< Queue that stores all free key-value slots*/
	odp_queue_t free_slots;
	/** Table with buckets storing all the hash values and key indexes
	  to the key table*/
	struct cuckoo_table_bucket *buckets;
} odph_cuckoo_table_impl ODP_ALIGNED_CACHE;

/**
 * Aligns input parameter to the next power of 2
 *
 * @param x
 *   The integer value to algin
 *
 * @return
 *   Input parameter aligned to the next power of 2
 */
static inline uint32_t
align32pow2(uint32_t x)
{
	x--;
	x |= x >> 1;
	x |= x >> 2;
	x |= x >> 4;
	x |= x >> 8;
	x |= x >> 16;

	return x + 1;
}

odph_table_t
odph_cuckoo_table_lookup(const char *name)
{
	odph_cuckoo_table_impl *tbl = NULL;
	odp_shm_t shm;

	if (name == NULL || strlen(name) >= ODPH_TABLE_NAME_LEN)
		return NULL;

	shm = odp_shm_lookup(name);
	if (shm != ODP_SHM_INVALID)
		tbl = (odph_cuckoo_table_impl *)odp_shm_addr(shm);
	if (!tbl || tbl->magicword != ODPH_CUCKOO_TABLE_MAGIC_WORD)
		return NULL;

	if (strcmp(tbl->name, name))
		return NULL;

	return (odph_table_t)tbl;
}

odph_table_t
odph_cuckoo_table_create(
		const char *name, uint32_t capacity, uint32_t key_size,
		uint32_t value_size)
{
	odph_cuckoo_table_impl *tbl;
	odp_shm_t shm_tbl;

	odp_pool_t pool;
	odp_pool_param_t param;

	odp_queue_t queue;
	odp_queue_param_t qparam;

	char pool_name[ODPH_TABLE_NAME_LEN + 3],
		 queue_name[ODPH_TABLE_NAME_LEN + 3];
	unsigned i;
	uint32_t impl_size, kv_entry_size,
			 bucket_num, bucket_size;

	/* Check for valid parameters */
	if (
	    (capacity > HASH_ENTRIES_MAX) ||
	    (capacity < HASH_BUCKET_ENTRIES) ||
	    (key_size == 0) ||
	    (strlen(name) == 0)) {
		ODPH_DBG("invalid parameters\n");
		return NULL;
	}

	/* Guarantee there's no existing */
	tbl = (odph_cuckoo_table_impl *)(void *)odph_cuckoo_table_lookup(name);
	if (tbl != NULL) {
		ODPH_DBG("cuckoo hash table %s already exists\n", name);
		return NULL;
	}

	/* Calculate the sizes of different parts of cuckoo hash table */
	impl_size = sizeof(odph_cuckoo_table_impl);
	kv_entry_size = sizeof(struct cuckoo_table_key_value)
					+ key_size + value_size;

	bucket_num = align32pow2(capacity) / HASH_BUCKET_ENTRIES;
	bucket_size = bucket_num * sizeof(struct cuckoo_table_bucket);

	shm_tbl = odp_shm_reserve(
				name, impl_size + bucket_size,
				ODP_CACHE_LINE_SIZE, ODP_SHM_SW_ONLY);

	if (shm_tbl == ODP_SHM_INVALID) {
		ODPH_DBG(
			"shm allocation failed for odph_cuckoo_table_impl %s\n",
			name);
		return NULL;
	}

	tbl = (odph_cuckoo_table_impl *)odp_shm_addr(shm_tbl);
	memset(tbl, 0, impl_size + bucket_size);

	/* header of this mem block is the table impl struct,
	 * then the bucket pool.
	 */
	tbl->buckets = (void *)((char *)tbl + impl_size);

	/* initialize key-value buffer pool */
	snprintf(pool_name, sizeof(pool_name), "kv_%s", name);
	pool = odp_pool_lookup(pool_name);

	if (pool != ODP_POOL_INVALID)
		odp_pool_destroy(pool);

	odp_pool_param_init(&param);
	param.type = ODP_POOL_BUFFER;
	param.buf.size = kv_entry_size;
	param.buf.align = ODP_CACHE_LINE_SIZE;
	param.buf.num = capacity;

	pool = odp_pool_create(pool_name, &param);

	if (pool == ODP_POOL_INVALID) {
		ODPH_DBG("failed to create key-value pool\n");
		odp_shm_free(shm_tbl);
		return NULL;
	}

	/* initialize free_slots queue */
	odp_queue_param_init(&qparam);
	qparam.type = ODP_QUEUE_TYPE_PLAIN;
	qparam.size = capacity;

	snprintf(queue_name, sizeof(queue_name), "fs_%s", name);
	queue = odp_queue_create(queue_name, &qparam);
	if (queue == ODP_QUEUE_INVALID) {
		ODPH_DBG("failed to create free_slots queue\n");
		odp_pool_destroy(pool);
		odp_shm_free(shm_tbl);
		return NULL;
	}

	/* Setup hash context */
	snprintf(tbl->name, sizeof(tbl->name), "%s", name);
	tbl->magicword = ODPH_CUCKOO_TABLE_MAGIC_WORD;
	tbl->entries = capacity;
	tbl->key_len = key_size;
	tbl->value_len = value_size;
	tbl->num_buckets = bucket_num;
	tbl->bucket_bitmask = bucket_num - 1;
	tbl->free_slots = queue;

	/* generate all free buffers, and put into queue */
	for (i = 0; i < capacity; i++) {
		odp_event_t ev = odp_buffer_to_event(
				odp_buffer_alloc(pool));
		if (ev == ODP_EVENT_INVALID) {
			ODPH_DBG("failed to generate free slots\n");
			odph_cuckoo_table_destroy((odph_table_t)tbl);
			return NULL;
		}

		if (odp_queue_enq(queue, ev) < 0) {
			ODPH_DBG("failed to enqueue free slots\n");
			odph_cuckoo_table_destroy((odph_table_t)tbl);
			return NULL;
		}
	}

	return (odph_table_t)tbl;
}

int
odph_cuckoo_table_destroy(odph_table_t tbl)
{
	int ret;
	odph_cuckoo_table_impl *impl = NULL;
	char pool_name[ODPH_TABLE_NAME_LEN + 3];
	odp_event_t ev;
	odp_shm_t shm;
	odp_pool_t pool;
	uint32_t i, j;

	if (tbl == NULL)
		return -1;

	impl = (odph_cuckoo_table_impl *)(void *)tbl;

	/* check magic word */
	if (impl->magicword != ODPH_CUCKOO_TABLE_MAGIC_WORD) {
		ODPH_DBG("wrong magicword for cuckoo table\n");
		return -1;
	}

	/* free all used buffers*/
	for (i = 0; i < impl->num_buckets; i++) {
		for (j = 0; j < HASH_BUCKET_ENTRIES; j++) {
			if (impl->buckets[i].signatures[j].current
					!= NULL_SIGNATURE)
				odp_buffer_free(impl->buckets[i].key_buf[j]);
		}
	}

	/* free all free buffers */
	while ((ev = odp_queue_deq(impl->free_slots))
			!= ODP_EVENT_INVALID) {
		odp_buffer_free(odp_buffer_from_event(ev));
	}

	/* destroy free_slots queue */
	ret = odp_queue_destroy(impl->free_slots);
	if (ret < 0)
		ODPH_DBG("failed to destroy free_slots queue\n");

	/* destroy key-value pool */
	snprintf(pool_name, sizeof(pool_name), "kv_%s", impl->name);
	pool = odp_pool_lookup(pool_name);
	if (pool == ODP_POOL_INVALID) {
		ODPH_DBG("invalid pool\n");
		return -1;
	}

	ret = odp_pool_destroy(pool);
	if (ret != 0) {
		ODPH_DBG("failed to destroy key-value buffer pool\n");
		return -1;
	}

	/* free impl */
	shm = odp_shm_lookup(impl->name);
	if (shm == ODP_SHM_INVALID) {
		ODPH_DBG("unable look up shm\n");
		return -1;
	}

	return odp_shm_free(shm);
}

static uint32_t hash(const odph_cuckoo_table_impl *h, const void *key)
{
	/* calc hash result by key */
	return odp_hash_crc32c(key, h->key_len, 0);
}

/* Calc the secondary hash value from the primary hash value of a given key */
static inline uint32_t
hash_secondary(const uint32_t primary_hash)
{
	static const unsigned all_bits_shift = 12;
	static const unsigned alt_bits_xor = 0x5bd1e995;

	uint32_t tag = primary_hash >> all_bits_shift;

	return (primary_hash ^ ((tag + 1) * alt_bits_xor));
}

/* Search for an entry that can be pushed to its alternative location */
static inline int
make_space_bucket(
	const odph_cuckoo_table_impl *impl,
	struct cuckoo_table_bucket *bkt)
{
	unsigned i, j;
	int ret;
	uint32_t next_bucket_idx;
	struct cuckoo_table_bucket *next_bkt[HASH_BUCKET_ENTRIES];

	/*
	 * Push existing item (search for bucket with space in
	 * alternative locations) to its alternative location
	 */
	for (i = 0; i < HASH_BUCKET_ENTRIES; i++) {
		/* Search for space in alternative locations */
		next_bucket_idx = bkt->signatures[i].alt & impl->bucket_bitmask;
		next_bkt[i] = &impl->buckets[next_bucket_idx];
		for (j = 0; j < HASH_BUCKET_ENTRIES; j++) {
			if (next_bkt[i]->signatures[j].sig == NULL_SIGNATURE)
				break;
		}

		if (j != HASH_BUCKET_ENTRIES)
			break;
	}

	/* Alternative location has spare room (end of recursive function) */
	if (i != HASH_BUCKET_ENTRIES) {
		next_bkt[i]->signatures[j].alt = bkt->signatures[i].current;
		next_bkt[i]->signatures[j].current = bkt->signatures[i].alt;
		next_bkt[i]->key_buf[j] = bkt->key_buf[i];
		return i;
	}

	/* Pick entry that has not been pushed yet */
	for (i = 0; i < HASH_BUCKET_ENTRIES; i++)
		if (bkt->flag[i] == 0)
			break;

	/* All entries have been pushed, so entry cannot be added */
	if (i == HASH_BUCKET_ENTRIES)
		return -ENOSPC;

	/* Set flag to indicate that this entry is going to be pushed */
	bkt->flag[i] = 1;
	/* Need room in alternative bucket to insert the pushed entry */
	ret = make_space_bucket(impl, next_bkt[i]);
	/*
	 * After recursive function.
	 * Clear flags and insert the pushed entry
	 * in its alternative location if successful,
	 * or return error
	 */
	bkt->flag[i] = 0;
	if (ret >= 0) {
		next_bkt[i]->signatures[ret].alt = bkt->signatures[i].current;
		next_bkt[i]->signatures[ret].current = bkt->signatures[i].alt;
		next_bkt[i]->key_buf[ret] = bkt->key_buf[i];
		return i;
	}

	return ret;
}

static inline int32_t
cuckoo_table_add_key_with_hash(
	const odph_cuckoo_table_impl *h, const void *key,
	uint32_t sig, void *data)
{
	uint32_t alt_hash;
	uint32_t prim_bucket_idx, sec_bucket_idx;
	unsigned i;
	struct cuckoo_table_bucket *prim_bkt, *sec_bkt;
	struct cuckoo_table_key_value *new_kv, *kv;

	odp_buffer_t new_buf;
	int ret;

	prim_bucket_idx = sig & h->bucket_bitmask;
	prim_bkt = &h->buckets[prim_bucket_idx];
	__builtin_prefetch((const void *)(uintptr_t)prim_bkt, 0, 3);

	alt_hash = hash_secondary(sig);
	sec_bucket_idx = alt_hash & h->bucket_bitmask;
	sec_bkt = &h->buckets[sec_bucket_idx];
	__builtin_prefetch((const void *)(uintptr_t)sec_bkt, 0, 3);

	/* Get a new slot for storing the new key */
	new_buf = odp_buffer_from_event(odp_queue_deq(h->free_slots));
	if (new_buf == ODP_BUFFER_INVALID)
		return -ENOSPC;

	/* Check if key is already inserted in primary location */
	for (i = 0; i < HASH_BUCKET_ENTRIES; i++) {
		if (
			prim_bkt->signatures[i].current == sig &&
			prim_bkt->signatures[i].alt == alt_hash)  {
			kv = (struct cuckoo_table_key_value *)odp_buffer_addr(
					prim_bkt->key_buf[i]);
			if (memcmp(key, kv->key, h->key_len) == 0) {
				odp_queue_enq(
						h->free_slots,
						odp_buffer_to_event(new_buf));
				/* Update data */
				if (kv->value != NULL)
					memcpy(kv->value, data, h->value_len);

				/* Return bucket index */
				return prim_bucket_idx;
			}
		}
	}

	/* Check if key is already inserted in secondary location */
	for (i = 0; i < HASH_BUCKET_ENTRIES; i++) {
		if (
			sec_bkt->signatures[i].alt == sig &&
			sec_bkt->signatures[i].current == alt_hash)  {
			kv = (struct cuckoo_table_key_value *)odp_buffer_addr(
					sec_bkt->key_buf[i]);
			if (memcmp(key, kv->key, h->key_len) == 0) {
				odp_queue_enq(
						h->free_slots,
						odp_buffer_to_event(new_buf));
				/* Update data */
				if (kv->value != NULL)
					memcpy(kv->value, data, h->value_len);

				/* Return bucket index */
				return sec_bucket_idx;
			}
		}
	}

	new_kv = (struct cuckoo_table_key_value *)odp_buffer_addr(new_buf);
	__builtin_prefetch((const void *)(uintptr_t)new_kv, 0, 3);

	/* Copy key and value.
	 * key-value mem block : struct cuckoo_table_key_value
	 *		+ key (key_len) + value (value_len)
	 */
	new_kv->key = (uint8_t *)new_kv
					+ sizeof(struct cuckoo_table_key_value);
	memcpy(new_kv->key, key, h->key_len);

	if (h->value_len > 0) {
		new_kv->value = new_kv->key + h->key_len;
		memcpy(new_kv->value, data, h->value_len);
	} else {
		new_kv->value = NULL;
	}

	/* Insert new entry is there is room in the primary bucket */
	for (i = 0; i < HASH_BUCKET_ENTRIES; i++) {
		/* Check if slot is available */
		if (odp_likely(prim_bkt->signatures[i].sig == NULL_SIGNATURE)) {
			prim_bkt->signatures[i].current = sig;
			prim_bkt->signatures[i].alt = alt_hash;
			prim_bkt->key_buf[i] = new_buf;
			return prim_bucket_idx;
		}
	}

	/* Primary bucket is full, so we need to make space for new entry */
	ret = make_space_bucket(h, prim_bkt);

	/*
	 * After recursive function.
	 * Insert the new entry in the position of the pushed entry
	 * if successful or return error and
	 * store the new slot back in the pool
	 */
	if (ret >= 0) {
		prim_bkt->signatures[ret].current = sig;
		prim_bkt->signatures[ret].alt = alt_hash;
		prim_bkt->key_buf[ret] = new_buf;
		return prim_bucket_idx;
	}

	/* Error in addition, store new slot back in the free_slots */
	odp_queue_enq(h->free_slots, odp_buffer_to_event(new_buf));
	return ret;
}

int
odph_cuckoo_table_put_value(odph_table_t tbl, void *key, void *value)
{
	odph_cuckoo_table_impl *impl;
	int ret;

	if ((tbl == NULL) || (key == NULL))
		return -EINVAL;

	impl = (odph_cuckoo_table_impl *)(void *)tbl;
	ret = cuckoo_table_add_key_with_hash(
			impl, key, hash(impl, key), value);

	if (ret < 0)
		return -1;

	return 0;
}

static inline int32_t
cuckoo_table_lookup_with_hash(
	const odph_cuckoo_table_impl *h, const void *key,
	uint32_t sig, void **data_ptr)
{
	uint32_t bucket_idx;
	uint32_t alt_hash;
	unsigned i;
	struct cuckoo_table_bucket *bkt;
	struct cuckoo_table_key_value *kv;

	bucket_idx = sig & h->bucket_bitmask;
	bkt = &h->buckets[bucket_idx];

	/* Check if key is in primary location */
	for (i = 0; i < HASH_BUCKET_ENTRIES; i++) {
		if (
			bkt->signatures[i].current == sig &&
			bkt->signatures[i].sig != NULL_SIGNATURE) {
			kv = (struct cuckoo_table_key_value *)odp_buffer_addr(
					bkt->key_buf[i]);
			if (memcmp(key, kv->key, h->key_len) == 0) {
				if (data_ptr != NULL)
					*data_ptr = kv->value;
				/*
				 * Return index where key is stored,
				 * subtracting the first dummy index
				 */
				return bucket_idx;
			}
		}
	}

	/* Calculate secondary hash */
	alt_hash = hash_secondary(sig);
	bucket_idx = alt_hash & h->bucket_bitmask;
	bkt = &h->buckets[bucket_idx];

	/* Check if key is in secondary location */
	for (i = 0; i < HASH_BUCKET_ENTRIES; i++) {
		if (
			bkt->signatures[i].current == alt_hash &&
			bkt->signatures[i].alt == sig) {
			kv = (struct cuckoo_table_key_value *)odp_buffer_addr(
					bkt->key_buf[i]);
			if (memcmp(key, kv->key, h->key_len) == 0) {
				if (data_ptr != NULL)
					*data_ptr = kv->value;
				/*
				 * Return index where key is stored,
				 * subtracting the first dummy index
				 */
				return bucket_idx;
			}
		}
	}

	return -ENOENT;
}

int odph_cuckoo_table_get_value(odph_table_t tbl, void *key,
				void *buffer, uint32_t buffer_size ODP_UNUSED)
{
	odph_cuckoo_table_impl *impl = (odph_cuckoo_table_impl *)(void *)tbl;
	void *tmp = NULL;
	int ret;

	if ((tbl == NULL) || (key == NULL))
		return -EINVAL;

	ret = cuckoo_table_lookup_with_hash(impl, key, hash(impl, key), &tmp);

	if (ret < 0)
		return -1;

	if (impl->value_len > 0)
		memcpy(buffer, tmp, impl->value_len);

	return 0;
}

static inline int32_t
cuckoo_table_del_key_with_hash(
	const odph_cuckoo_table_impl *h,
	const void *key, uint32_t sig)
{
	uint32_t bucket_idx;
	uint32_t alt_hash;
	unsigned i;
	struct cuckoo_table_bucket *bkt;
	struct cuckoo_table_key_value *kv;

	bucket_idx = sig & h->bucket_bitmask;
	bkt = &h->buckets[bucket_idx];

	/* Check if key is in primary location */
	for (i = 0; i < HASH_BUCKET_ENTRIES; i++) {
		if (
			bkt->signatures[i].current == sig &&
			bkt->signatures[i].sig != NULL_SIGNATURE) {
			kv = (struct cuckoo_table_key_value *)odp_buffer_addr(
					bkt->key_buf[i]);
			if (memcmp(key, kv->key, h->key_len) == 0) {
				bkt->signatures[i].sig = NULL_SIGNATURE;
				odp_queue_enq(
						h->free_slots,
						odp_buffer_to_event(
							bkt->key_buf[i]));
				return bucket_idx;
			}
		}
	}

	/* Calculate secondary hash */
	alt_hash = hash_secondary(sig);
	bucket_idx = alt_hash & h->bucket_bitmask;
	bkt = &h->buckets[bucket_idx];

	/* Check if key is in secondary location */
	for (i = 0; i < HASH_BUCKET_ENTRIES; i++) {
		if (
			bkt->signatures[i].current == alt_hash &&
			bkt->signatures[i].sig != NULL_SIGNATURE) {
			kv = (struct cuckoo_table_key_value *)odp_buffer_addr(
					bkt->key_buf[i]);
			if (memcmp(key, kv->key, h->key_len) == 0) {
				bkt->signatures[i].sig = NULL_SIGNATURE;
				odp_queue_enq(
						h->free_slots,
						odp_buffer_to_event(
							bkt->key_buf[i]));
				return bucket_idx;
			}
		}
	}

	return -ENOENT;
}

int
odph_cuckoo_table_remove_value(odph_table_t tbl, void *key)
{
	odph_cuckoo_table_impl *impl = (void *)tbl;
	int ret;

	if ((tbl == NULL) || (key == NULL))
		return -EINVAL;

	ret = cuckoo_table_del_key_with_hash(impl, key, hash(impl, key));
	if (ret < 0)
		return -1;

	return 0;
}

odph_table_ops_t odph_cuckoo_table_ops = {
	odph_cuckoo_table_create,
	odph_cuckoo_table_lookup,
	odph_cuckoo_table_destroy,
	odph_cuckoo_table_put_value,
	odph_cuckoo_table_get_value,
	odph_cuckoo_table_remove_value
};