summaryrefslogtreecommitdiffstats
path: root/tests/test_pfx_locks.c
blob: 1decf913564f2d58d5862273239f7569a5468a22 (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
/*
 * This file is part of RTRlib.
 *
 * This file is subject to the terms and conditions of the MIT license.
 * See the file LICENSE in the top level directory for more details.
 *
 * Website: http://rtrlib.realmv6.org/
 */

#include "rtrlib/lib/ip.h"
#include "rtrlib/pfx/pfx.h"
#include "rtrlib/pfx/trie/trie-pfx.h"

#include <arpa/inet.h>
#include <pthread.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <time.h>
#include <unistd.h>

uint32_t min_i = 0xFF000000;
uint32_t max_i = 0xFFFFFFF0;

/**
 * @brief Add records to prefix table
 */
static void *rec_add(struct pfx_table *pfxt)
{
	const int tid = getpid();
	struct pfx_record rec;

	rec.min_len = 32;
	rec.max_len = 32;
	rec.prefix.ver = LRTR_IPV4;
	rec.prefix.u.addr4.addr = 0;

	printf("Inserting %u records\n", (max_i - min_i) * 3);
	for (uint32_t i = max_i; i >= min_i; i--) {
		rec.min_len = 32;
		rec.max_len = 32;
		rec.socket = NULL;
		rec.asn = tid % 2;
		rec.prefix.u.addr4.addr = htonl(i);
		rec.prefix.ver = LRTR_IPV4;
		pfx_table_add(pfxt, &rec);
		rec.asn = (tid % 2) + 1;
		pfx_table_add(pfxt, &rec);

		rec.min_len = 128;
		rec.max_len = 128;
		rec.prefix.ver = LRTR_IPV6;
		rec.prefix.u.addr6.addr[1] = min_i + 0xFFFFFFFF;
		rec.prefix.u.addr6.addr[0] = htonl(i) + 0xFFFFFFFF;
		pfx_table_add(pfxt, &rec);
		usleep(rand() / (RAND_MAX / 20));
	}

	return NULL;
}

/**
 * @brief Validate records in prefix table
 */
static void *rec_val(struct pfx_table *pfxt)
{
	const int tid = getpid();
	struct pfx_record rec;
	enum pfxv_state res;

	rec.min_len = 32;
	rec.max_len = 32;
	rec.prefix.ver = LRTR_IPV4;
	rec.prefix.u.addr4.addr = 0;
	printf("validating..\n");
	for (uint32_t i = max_i; i >= min_i; i--) {
		rec.min_len = 32;
		rec.max_len = 32;
		rec.prefix.ver = LRTR_IPV4;
		rec.prefix.u.addr4.addr = htonl(i);
		pfx_table_validate(pfxt, (tid % 2), &rec.prefix, rec.min_len, &res);
		pfx_table_validate(pfxt, (tid % 2) + 1, &rec.prefix, rec.min_len, &res);

		rec.min_len = 128;
		rec.max_len = 128;
		rec.prefix.ver = LRTR_IPV6;
		rec.prefix.u.addr6.addr[1] = min_i + 0xFFFFFFFF;
		rec.prefix.u.addr6.addr[0] = htonl(i) + 0xFFFFFFFF;

		pfx_table_validate(pfxt, (tid % 2) + 1, &rec.prefix, rec.min_len, &res);
		usleep(rand() / (RAND_MAX / 20));
	}

	return NULL;
}

/**
 * @brief Delete records from prefix table
 */
static void *rec_del(struct pfx_table *pfxt)
{
	const int tid = getpid();
	struct pfx_record rec;

	rec.min_len = 32;
	rec.max_len = 32;
	rec.prefix.ver = LRTR_IPV4;
	rec.prefix.u.addr4.addr = 0;
	printf("removing records\n");
	for (uint32_t i = max_i; i >= min_i; i--) {
		rec.socket = NULL;
		rec.min_len = 32;
		rec.max_len = 32;
		rec.asn = tid % 2;
		rec.prefix.ver = LRTR_IPV4;
		rec.prefix.u.addr4.addr = htonl(i);
		pfx_table_remove(pfxt, &rec);
		rec.asn = (tid % 2) + 1;
		pfx_table_remove(pfxt, &rec);

		rec.prefix.ver = LRTR_IPV6;
		rec.min_len = 128;
		rec.max_len = 128;
		rec.prefix.u.addr6.addr[1] = min_i + 0xFFFFFFFF;
		rec.prefix.u.addr6.addr[0] = htonl(i) + 0xFFFFFFFF;

		pfx_table_remove(pfxt, &rec);
		usleep(rand() / (RAND_MAX / 20));
	}
	printf("Done\n");

	return NULL;
}

/**
 * @brief Test concurrent operations on pfx_table
 * This test creates 15 (random) threads that add, delete, or validate huge
 * numbers of entries in/to/from a pfx_table.
 *
 * NOTE: this test takes a (very) long time to complete, use with care!
 */
int main(void)
{
	unsigned int max_threads = 15;
	struct pfx_table pfxt;
	pthread_t threads[max_threads];

	pfx_table_init(&pfxt, NULL);
	srand(time(NULL));

	for (unsigned int i = 0; i < max_threads; i++) {
		int r = rand() / (RAND_MAX / 3);

		if (r == 0)
			pthread_create(&threads[i], NULL, (void *(*)(void *))rec_add, &pfxt);
		else if (r == 1)
			pthread_create(&threads[i], NULL, (void *(*)(void *))rec_del, &pfxt);
		else if (r == 2)
			pthread_create(&threads[i], NULL, (void *(*)(void *))rec_val, &pfxt);
		printf("Started Thread %d\n", i);
		usleep(200);
	}
	for (unsigned int i = 0; i < max_threads; i++) {
		pthread_join(threads[i], NULL);
		printf("Thread %i returned\n", i);
	}

	return EXIT_SUCCESS;
}