aboutsummaryrefslogtreecommitdiff
path: root/helper/test/iplookuptable.c
blob: b5d774cbc275389b831b5a56f8a98f307a188fb9 (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
/* Copyright (c) 2016, Linaro Limited
 * All rights reserved.
 *
 * SPDX-License-Identifier:     BSD-3-Clause
 */

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

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

static void print_prefix_info(
		const char *msg, uint32_t ip, uint8_t cidr)
{
	int i = 0;
	uint8_t *ptr = (uint8_t *)(&ip);

	printf("%s IP prefix: ", msg);
	for (i = 3; i >= 0; i--) {
		if (i != 3)
			printf(".");
		printf("%d", ptr[i]);
	}
	printf("/%d\n", cidr);
}

/*
 * Basic sequence of operations for a single key:
 *	- put short prefix
 *	- put long prefix
 *	- get (hit long prefix)
 *	- remove long prefix
 *	- get (hit short prefix)
 */
static int test_ip_lookup_table(void)
{
	odph_iplookup_prefix_t prefix1, prefix2;
	odph_table_t table;
	int ret;
	uint64_t value1 = 1, value2 = 2, result = 0;
	uint32_t lkp_ip = 0;

	table = odph_iplookup_table_create(
			"prefix_test", 0, 0, sizeof(uint32_t));
	if (table == NULL) {
		printf("IP prefix lookup table creation failed\n");
		return -1;
	}

	ret = odph_ipv4_addr_parse(&prefix1.ip, "192.168.0.0");
	if (ret < 0) {
		printf("Failed to get IP addr from str\n");
		odph_iplookup_table_destroy(table);
		return -1;
	}
	prefix1.cidr = 11;

	ret = odph_ipv4_addr_parse(&prefix2.ip, "192.168.0.0");
	if (ret < 0) {
		printf("Failed to get IP addr from str\n");
		odph_iplookup_table_destroy(table);
		return -1;
	}
	prefix2.cidr = 24;

	ret = odph_ipv4_addr_parse(&lkp_ip, "192.168.0.1");
	if (ret < 0) {
		printf("Failed to get IP addr from str\n");
		odph_iplookup_table_destroy(table);
		return -1;
	}

	/* test with standard put/get/remove functions */
	ret = odph_iplookup_table_put_value(table, &prefix1, &value1);
	print_prefix_info("Add", prefix1.ip, prefix1.cidr);
	if (ret < 0) {
		printf("Failed to add ip prefix\n");
		odph_iplookup_table_destroy(table);
		return -1;
	}

	ret = odph_iplookup_table_get_value(table, &lkp_ip, &result, 0);
	print_prefix_info("Lkp", lkp_ip, 32);
	if (ret < 0 || result != 1) {
		printf("Failed to find longest prefix\n");
		odph_iplookup_table_destroy(table);
		return -1;
	}

	/* add a longer prefix */
	ret = odph_iplookup_table_put_value(table, &prefix2, &value2);
	print_prefix_info("Add", prefix2.ip, prefix2.cidr);
	if (ret < 0) {
		printf("Failed to add ip prefix\n");
		odph_iplookup_table_destroy(table);
		return -1;
	}

	ret = odph_iplookup_table_get_value(table, &lkp_ip, &result, 0);
	print_prefix_info("Lkp", lkp_ip, 32);
	if (ret < 0 || result != 2) {
		printf("Failed to find longest prefix\n");
		odph_iplookup_table_destroy(table);
		return -1;
	}

	ret = odph_iplookup_table_remove_value(table, &prefix2);
	print_prefix_info("Del", prefix2.ip, prefix2.cidr);
	if (ret < 0) {
		printf("Failed to delete ip prefix\n");
		odph_iplookup_table_destroy(table);
		return -1;
	}

	ret = odph_iplookup_table_get_value(table, &lkp_ip, &result, 0);
	print_prefix_info("Lkp", lkp_ip, 32);
	if (ret < 0 || result != 1) {
		printf("Error: found result ater deleting\n");
		odph_iplookup_table_destroy(table);
		return -1;
	}

	ret = odph_iplookup_table_remove_value(table, &prefix1);
	print_prefix_info("Del", prefix1.ip, prefix1.cidr);
	if (ret < 0) {
		printf("Failed to delete prefix\n");
		odph_iplookup_table_destroy(table);
		return -1;
	}

	odph_iplookup_table_destroy(table);
	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);
	}

	if (test_ip_lookup_table() < 0)
		printf("Test failed\n");
	else
		printf("All tests passed\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;
}