aboutsummaryrefslogtreecommitdiff
path: root/helper/test/cuckootable.c
blob: be655911ff9093ef1d08a9d0092c9bcdb6850f6d (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
/* 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 <stdio.h>
#include <stdint.h>
#include <string.h>
#include <stdlib.h>
#include <stdarg.h>
#include <errno.h>
#include <sys/queue.h>
#include <sys/time.h>
#include <time.h>

#include <odp_api.h>
#include <odph_debug.h>
#include <odp/helper/odph_api.h>

/*******************************************************************************
 * Hash function performance test configuration section.
 *
 * The five arrays below control what tests are performed. Every combination
 * from the array entries is tested.
 */
/******************************************************************************/

/* 5-tuple key type */
struct flow_key {
	uint32_t ip_src;
	uint32_t ip_dst;
	uint16_t port_src;
	uint16_t port_dst;
	uint8_t proto;
} __packed;

/*
 * Print out result of unit test hash operation.
 */
static void print_key_info(
		const char *msg, const struct flow_key *key)
{
	const uint8_t *p = (const uint8_t *)key;
	unsigned i;

	printf("%s key:0x", msg);
	for (i = 0; i < sizeof(struct flow_key); i++)
		printf("%02X", p[i]);
	printf("\n");
}

static double get_time_diff(struct timeval *start, struct timeval *end)
{
	int sec = end->tv_sec - start->tv_sec;
	int usec = end->tv_usec - start->tv_usec;

	if (usec < 0) {
		sec--;
		usec += 1000000;
	}
	double diff = sec + (double)usec / 1000000;

	return diff;
}

/** Create IPv4 address */
#define IPv4(a, b, c, d) ((uint32_t)(((a) & 0xff) << 24) | \
	(((b) & 0xff) << 16) | \
	(((c) & 0xff) << 8)  | \
	((d) & 0xff))

/* Keys used by unit test functions */
static struct flow_key keys[5] = { {
	.ip_src = IPv4(0x03, 0x02, 0x01, 0x00),
	.ip_dst = IPv4(0x07, 0x06, 0x05, 0x04),
	.port_src = 0x0908,
	.port_dst = 0x0b0a,
	.proto = 0x0c,
}, {
	.ip_src = IPv4(0x13, 0x12, 0x11, 0x10),
	.ip_dst = IPv4(0x17, 0x16, 0x15, 0x14),
	.port_src = 0x1918,
	.port_dst = 0x1b1a,
	.proto = 0x1c,
}, {
	.ip_src = IPv4(0x23, 0x22, 0x21, 0x20),
	.ip_dst = IPv4(0x27, 0x26, 0x25, 0x24),
	.port_src = 0x2928,
	.port_dst = 0x2b2a,
	.proto = 0x2c,
}, {
	.ip_src = IPv4(0x33, 0x32, 0x31, 0x30),
	.ip_dst = IPv4(0x37, 0x36, 0x35, 0x34),
	.port_src = 0x3938,
	.port_dst = 0x3b3a,
	.proto = 0x3c,
}, {
	.ip_src = IPv4(0x43, 0x42, 0x41, 0x40),
	.ip_dst = IPv4(0x47, 0x46, 0x45, 0x44),
	.port_src = 0x4948,
	.port_dst = 0x4b4a,
	.proto = 0x4c,
} };

/*
 * Basic sequence of operations for a single key:
 *	- put
 *	- get (hit)
 *	- remove
 *	- get (miss)
 */
static int test_put_remove(void)
{
	odph_table_t table;
	odph_table_ops_t *ops;

	ops = &odph_cuckoo_table_ops;

	/* test with standard put/get/remove functions */
	int ret;

	table = ops->f_create("put_remove", 10, sizeof(struct flow_key), 0);
	if (table == NULL) {
		printf("cuckoo hash table creation failed\n");
		return -1;
	}

	ret = odph_cuckoo_table_put_value(table, &keys[0], NULL);
	print_key_info("Add", &keys[0]);
	if (ret < 0) {
		printf("failed to add key\n");
		odph_cuckoo_table_destroy(table);
		return -1;
	}

	ret = odph_cuckoo_table_get_value(table, &keys[0], NULL, 0);
	print_key_info("Lkp", &keys[0]);
	if (ret < 0) {
		printf("failed to find key\n");
		odph_cuckoo_table_destroy(table);
		return -1;
	}

	ret = odph_cuckoo_table_remove_value(table, &keys[0]);
	print_key_info("Del", &keys[0]);
	if (ret < 0) {
		printf("failed to delete key\n");
		odph_cuckoo_table_destroy(table);
		return -1;
	}

	ret = odph_cuckoo_table_get_value(table, &keys[0], NULL, 0);
	print_key_info("Lkp", &keys[0]);
	if (ret >= 0) {
		printf("error: found key after deleting!\n");
		odph_cuckoo_table_destroy(table);
		return -1;
	}

	odph_cuckoo_table_destroy(table);
	return 0;
}

/*
 * Sequence of operations for a single key:
 * key type : struct flow_key
 * value type: uint8_t
 *	- remove: miss
 *	- put
 *	- get: hit
 *	- put: update
 *	- get: hit (updated data)
 *	- remove: hit
 *	- remove: miss
 */
static int test_put_update_remove(void)
{
	odph_table_t table;
	int ret;
	uint8_t val1 = 1, val2 = 2, val = 0;

	table = odph_cuckoo_table_create(
			"put_update_remove",
			10, sizeof(struct flow_key), sizeof(uint8_t));
	if (table == NULL) {
		printf("failed to create table\n");
		return -1;
	}

	ret = odph_cuckoo_table_remove_value(table, &keys[0]);
	print_key_info("Del", &keys[0]);
	if (ret >= 0) {
		printf("error: found non-existent key\n");
		odph_cuckoo_table_destroy(table);
		return -1;
	}

	ret = odph_cuckoo_table_put_value(table, &keys[0], &val1);
	print_key_info("Add", &keys[0]);
	if (ret < 0) {
		printf("failed to add key\n");
		odph_cuckoo_table_destroy(table);
		return -1;
	}

	ret = odph_cuckoo_table_get_value(
			table, &keys[0], &val, sizeof(uint8_t));
	print_key_info("Lkp", &keys[0]);
	if (ret < 0) {
		printf("failed to find key\n");
		odph_cuckoo_table_destroy(table);
		return -1;
	}

	ret = odph_cuckoo_table_put_value(table, &keys[0], &val2);
	if (ret < 0) {
		printf("failed to re-add key\n");
		odph_cuckoo_table_destroy(table);
		return -1;
	}

	ret = odph_cuckoo_table_get_value(
			table, &keys[0], &val, sizeof(uint8_t));
	print_key_info("Lkp", &keys[0]);
	if (ret < 0) {
		printf("failed to find key\n");
		odph_cuckoo_table_destroy(table);
		return -1;
	}

	ret = odph_cuckoo_table_remove_value(table, &keys[0]);
	print_key_info("Del", &keys[0]);
	if (ret < 0) {
		printf("failed to delete key\n");
		odph_cuckoo_table_destroy(table);
		return -1;
	}

	ret = odph_cuckoo_table_remove_value(table, &keys[0]);
	print_key_info("Del", &keys[0]);
	if (ret >= 0) {
		printf("error: deleted already deleted key\n");
		odph_cuckoo_table_destroy(table);
		return -1;
	}

	odph_cuckoo_table_destroy(table);
	return 0;
}

/*
 * Sequence of operations for find existing hash table
 *
 *  - create table
 *  - find existing table: hit
 *  - find non-existing table: miss
 *
 */
static int test_table_lookup(void)
{
	odph_table_t table, result;

	/* Create cuckoo hash table. */
	table = odph_cuckoo_table_create("table_lookup", 10, 4, 0);
	if (table == NULL) {
		printf("failed to create table\n");
		return -1;
	}

	/* Try to find existing hash table */
	result = odph_cuckoo_table_lookup("table_lookup");
	if (result != table) {
		printf("error: could not find existing table\n");
		odph_cuckoo_table_destroy(table);
		return -1;
	}

	/* Try to find non-existing hash table */
	result = odph_cuckoo_table_lookup("non_existing");
	if (result != NULL) {
		printf("error: found table that shouldn't exist.\n");
		odph_cuckoo_table_destroy(table);
		return -1;
	}

	/* Cleanup. */
	odph_cuckoo_table_destroy(table);
	return 0;
}

/*
 * Sequence of operations for 5 keys
 *	- put keys
 *	- get keys: hit
 *	- remove keys : hit
 *	- get keys: miss
 */
static int test_five_keys(void)
{
	odph_table_t table;
	unsigned i;
	int ret;

	table = odph_cuckoo_table_create(
			"five_keys", 10, sizeof(struct flow_key), 0);
	if (table == NULL) {
		printf("failed to create table\n");
		return -1;
	}

	/* put */
	for (i = 0; i < 5; i++) {
		ret = odph_cuckoo_table_put_value(table, &keys[i], NULL);
		print_key_info("Add", &keys[i]);
		if (ret < 0) {
			printf("failed to add key %d\n", i);
			odph_cuckoo_table_destroy(table);
			return -1;
		}
	}

	/* get */
	for (i = 0; i < 5; i++) {
		ret = odph_cuckoo_table_get_value(table, &keys[i], NULL, 0);
		print_key_info("Lkp", &keys[i]);
		if (ret < 0) {
			printf("failed to find key %d\n", i);
			odph_cuckoo_table_destroy(table);
			return -1;
		}
	}

	/* remove */
	for (i = 0; i < 5; i++) {
		ret = odph_cuckoo_table_remove_value(table, &keys[i]);
		print_key_info("Del", &keys[i]);
		if (ret < 0) {
			printf("failed to delete key %d\n", i);
			odph_cuckoo_table_destroy(table);
			return -1;
		}
	}

	/* get */
	for (i = 0; i < 5; i++) {
		ret = odph_cuckoo_table_get_value(table, &keys[i], NULL, 0);
		print_key_info("Lkp", &keys[i]);
		if (ret >= 0) {
			printf("found non-existing key %d\n", i);
			odph_cuckoo_table_destroy(table);
			return -1;
		}
	}

	odph_cuckoo_table_destroy(table);
	return 0;
}

#define BUCKET_ENTRIES 4
#define HASH_ENTRIES_MAX 1048576
/*
 * Do tests for cuchoo tabke creation with bad parameters.
 */
static int test_creation_with_bad_parameters(void)
{
	odph_table_t table;

	table = odph_cuckoo_table_create(
			"bad_param_0", HASH_ENTRIES_MAX + 1, 4, 0);
	if (table != NULL) {
		odph_cuckoo_table_destroy(table);
		printf("Impossible creating table successfully with entries in parameter exceeded\n");
		return -1;
	}

	table = odph_cuckoo_table_create(
			"bad_param_1", BUCKET_ENTRIES - 1, 4, 0);
	if (table != NULL) {
		odph_cuckoo_table_destroy(table);
		printf("Impossible creating hash successfully if entries less than bucket_entries in parameter\n");
		return -1;
	}

	table = odph_cuckoo_table_create("bad_param_2", 10, 0, 0);
	if (table != NULL) {
		odph_cuckoo_table_destroy(table);
		printf("Impossible creating hash successfully if key_len in parameter is zero\n");
		return -1;
	}

	printf("# Test successful. No more errors expected\n");

	return 0;
}

#define PERFORMANCE_CAPACITY 1000000

/*
 * Test the performance of cuckoo hash table.
 *   table capacity : 1,000,000
 *   key size : 4 bytes
 *   value size : 0
 * Insert at most number random keys into the table. If one
 * insertion is failed, the rest insertions will be cancelled.
 * The table utilization of the report will show actual number
 * of items inserted.
 * Then search all inserted items.
 */
static int test_performance(int number)
{
	odph_table_t table;

	/* generate random keys */
	uint8_t *key_space = NULL;
	const void **key_ptr = NULL;
	unsigned key_len = 4, j;
	unsigned elem_num = (number > PERFORMANCE_CAPACITY) ?
						PERFORMANCE_CAPACITY : number;
	unsigned key_num = key_len * elem_num;

	key_space = (uint8_t *)malloc(key_num);
	if (key_space == NULL)
		return -ENOENT;

	key_ptr = (const void **)malloc(sizeof(void *) * elem_num);
	if (key_ptr == NULL) {
		free(key_space);
		return -ENOENT;
	}

	for (j = 0; j < key_num; j++) {
		key_space[j] = rand() % 255;
		if (j % key_len == 0)
			key_ptr[j / key_len] = &key_space[j];
	}

	unsigned num;
	int ret = 0;
	struct timeval start, end;
	double add_time = 0;

	fflush(stdout);
	table = odph_cuckoo_table_create(
			"performance_test", PERFORMANCE_CAPACITY, key_len, 0);
	if (table == NULL) {
		printf("cuckoo table creation failed\n");
		free(key_ptr);
		free(key_space);
		return -ENOENT;
	}

	/* insert (put) */
	gettimeofday(&start, 0);
	for (j = 0; j < elem_num; j++) {
		ret = odph_cuckoo_table_put_value(
				table, &key_space[j * key_len], NULL);
		if (ret < 0)
			break;
	}
	gettimeofday(&end, 0);
	num = j;
	add_time = get_time_diff(&start, &end);
	printf(
		"add %u/%u (%.2f) items, time = %.9lfs\n",
		num, PERFORMANCE_CAPACITY,
		(double)num / PERFORMANCE_CAPACITY, add_time);

	/* search (get) */
	gettimeofday(&start, 0);
	for (j = 0; j < num; j++) {
		ret = odph_cuckoo_table_get_value(
				table, &key_space[j * key_len], NULL, 0);

		if (ret < 0)
			printf("lookup error\n");
	}
	gettimeofday(&end, 0);
	printf(
			"lookup %u items, time = %.9lfs\n",
			num, get_time_diff(&start, &end));

	odph_cuckoo_table_destroy(table);
	free(key_ptr);
	free(key_space);
	return ret;
}

/*
 * Do all unit and performance tests.
 */
static int
test_cuckoo_hash_table(void)
{
	if (test_put_remove() < 0)
		return -1;
	if (test_table_lookup() < 0)
		return -1;
	if (test_put_update_remove() < 0)
		return -1;
	if (test_five_keys() < 0)
		return -1;
	if (test_creation_with_bad_parameters() < 0)
		return -1;
	if (test_performance(950000) < 0)
		return -1;

	return 0;
}

int main(int argc ODPH_UNUSED, char *argv[] ODPH_UNUSED)
{
	odp_instance_t instance;
	int ret = 0;

	ret = odp_init_global(&instance, NULL, NULL);
	if (ret != 0) {
		fprintf(stderr, "Error: ODP global init failed.\n");
		exit(EXIT_FAILURE);
	}

	ret = odp_init_local(instance, ODP_THREAD_WORKER);
	if (ret != 0) {
		fprintf(stderr, "Error: ODP local init failed.\n");
		exit(EXIT_FAILURE);
	}

	srand(time(0));
	ret = test_cuckoo_hash_table();

	if (ret < 0)
		printf("cuckoo hash table test fail!!\n");
	else
		printf("All Tests pass!!\n");

	if (odp_term_local()) {
		fprintf(stderr, "Error: ODP local term failed.\n");
		exit(EXIT_FAILURE);
	}

	if (odp_term_global(instance)) {
		fprintf(stderr, "Error: ODP global term failed.\n");
		exit(EXIT_FAILURE);
	}

	return ret;
}