summaryrefslogtreecommitdiffstats
path: root/tests/lib/test_srcdest_table.c
blob: 935ee500a376fe209711ee7730a2c670f8510b51 (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
/*
 * Test srcdest table for correctness.
 *
 * Copyright (C) 2017 by David Lamparter & Christian Franke,
 *                       Open Source Routing / NetDEF Inc.
 *
 * This file is part of FRRouting (FRR)
 *
 * FRR is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 2, or (at your option) any
 * later version.
 *
 * FRR is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; see the file COPYING; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
 */

#include <zebra.h>

#include "hash.h"
#include "memory.h"
#include "prefix.h"
#include "prng.h"
#include "srcdest_table.h"
#include "table.h"

/* Copied from ripngd/ripng_nexthop.h - maybe the whole s6_addr32 thing
 * should be added by autoconf if not present?
 */
#ifndef s6_addr32
#if defined(SUNOS_5)
/* Some SunOS define s6_addr32 only to kernel */
#define s6_addr32 _S6_un._S6_u32
#else
#define s6_addr32 __u6_addr.__u6_addr32
#endif /* SUNOS_5 */
#endif /*s6_addr32*/

struct thread_master *master;

/* This structure is copied from lib/srcdest_table.c to which it is
 * private as far as other parts of Quagga are concerned.
 */
struct srcdest_rnode {
	/* must be first in structure for casting to/from route_node */
	ROUTE_NODE_FIELDS;

	struct route_table *src_table;
};

struct test_state {
	struct route_table *table;
	struct hash *log;
};

static char *format_srcdest(const struct prefix_ipv6 *dst_p,
			    const struct prefix_ipv6 *src_p)
{
	char dst_str[BUFSIZ];
	char src_str[BUFSIZ];
	char *rv;
	int ec;

	prefix2str((const struct prefix *)dst_p, dst_str, sizeof(dst_str));
	if (src_p && src_p->prefixlen)
		prefix2str((const struct prefix *)src_p, src_str,
			   sizeof(src_str));
	else
		src_str[0] = '\0';

	ec = asprintf(&rv, "%s%s%s", dst_str,
		      (src_str[0] != '\0') ? " from " : "", src_str);

	assert(ec > 0);
	return rv;
}

static unsigned int log_key(const void *data)
{
	const struct prefix *hash_entry = data;
	struct prefix_ipv6 *dst_p = (struct prefix_ipv6 *)&hash_entry[0];
	struct prefix_ipv6 *src_p = (struct prefix_ipv6 *)&hash_entry[1];
	unsigned int hash = 0;
	unsigned int i;

	hash = (hash * 33) ^ (unsigned int)dst_p->prefixlen;
	for (i = 0; i < 4; i++)
		hash = (hash * 33) ^ (unsigned int)dst_p->prefix.s6_addr32[i];

	hash = (hash * 33) ^ (unsigned int)src_p->prefixlen;
	if (src_p->prefixlen)
		for (i = 0; i < 4; i++)
			hash = (hash * 33)
			       ^ (unsigned int)src_p->prefix.s6_addr32[i];

	return hash;
}

static bool log_cmp(const void *a, const void *b)
{
	if (a == NULL || b == NULL)
		return false;

	return !memcmp(a, b, 2 * sizeof(struct prefix));
}

static void log_free(void *data)
{
	XFREE(MTYPE_TMP, data);
}

static void *log_alloc(void *data)
{
	void *rv = XMALLOC(MTYPE_TMP, 2 * sizeof(struct prefix));
	memcpy(rv, data, 2 * sizeof(struct prefix));
	return rv;
}

static struct test_state *test_state_new(void)
{
	struct test_state *rv;

	rv = XCALLOC(MTYPE_TMP, sizeof(*rv));
	assert(rv);

	rv->table = srcdest_table_init();
	assert(rv->table);

	rv->log = hash_create(log_key, log_cmp, NULL);
	return rv;
}

static void test_state_free(struct test_state *test)
{
	route_table_finish(test->table);
	hash_clean(test->log, log_free);
	hash_free(test->log);
	XFREE(MTYPE_TMP, test);
}

static void test_state_add_route(struct test_state *test,
				 struct prefix_ipv6 *dst_p,
				 struct prefix_ipv6 *src_p)
{
	struct route_node *rn =
		srcdest_rnode_get(test->table, (struct prefix *)dst_p, src_p);
	struct prefix hash_entry[2];

	memset(hash_entry, 0, sizeof(hash_entry));
	memcpy(&hash_entry[0], dst_p, sizeof(*dst_p));
	memcpy(&hash_entry[1], src_p, sizeof(*src_p));

	if (rn->info) {
		route_unlock_node(rn);
		assert(hash_lookup(test->log, hash_entry) != NULL);
		return;
	} else {
		assert(hash_lookup(test->log, hash_entry) == NULL);
	}

	rn->info = (void *)0xdeadbeef;
	hash_get(test->log, hash_entry, log_alloc);
};

static void test_state_del_route(struct test_state *test,
				 struct prefix_ipv6 *dst_p,
				 struct prefix_ipv6 *src_p)
{
	struct route_node *rn = srcdest_rnode_lookup(
		test->table, (struct prefix *)dst_p, src_p);
	struct prefix hash_entry[2];

	memset(hash_entry, 0, sizeof(hash_entry));
	memcpy(&hash_entry[0], dst_p, sizeof(*dst_p));
	memcpy(&hash_entry[1], src_p, sizeof(*src_p));

	if (!rn) {
		assert(!hash_lookup(test->log, hash_entry));
		return;
	}

	assert(rn->info == (void *)0xdeadbeef);
	rn->info = NULL;
	route_unlock_node(rn);
	route_unlock_node(rn);

	struct prefix *hash_entry_intern = hash_release(test->log, hash_entry);
	assert(hash_entry_intern != NULL);
	XFREE(MTYPE_TMP, hash_entry_intern);
}

static void verify_log(struct hash_bucket *bucket, void *arg)
{
	struct test_state *test = arg;
	struct prefix *hash_entry = bucket->data;
	struct prefix *dst_p = &hash_entry[0];
	struct prefix_ipv6 *src_p = (struct prefix_ipv6 *)&hash_entry[1];
	struct route_node *rn = srcdest_rnode_lookup(test->table, dst_p, src_p);

	assert(rn);
	assert(rn->info == (void *)0xdeadbeef);

	route_unlock_node(rn);
}

static void dump_log(struct hash_bucket *bucket, void *arg)
{
	struct prefix *hash_entry = bucket->data;
	struct prefix_ipv6 *dst_p = (struct prefix_ipv6 *)&hash_entry[0];
	struct prefix_ipv6 *src_p = (struct prefix_ipv6 *)&hash_entry[1];
	char *route_id = format_srcdest(dst_p, src_p);

	fprintf(stderr, "  %s\n", route_id);
	free(route_id);
}

static void test_dump(struct test_state *test)
{
	fprintf(stderr, "Contents of hash table:\n");
	hash_iterate(test->log, dump_log, test);
	fprintf(stderr, "\n");
}

static void test_failed(struct test_state *test, const char *message,
			const struct prefix_ipv6 *dst_p,
			const struct prefix_ipv6 *src_p)
{
	char *route_id = format_srcdest(dst_p, src_p);

	fprintf(stderr, "Test failed. Error: %s\n", message);
	fprintf(stderr, "Route in question: %s\n", route_id);
	free(route_id);

	test_dump(test);
	assert(3 == 4);
}

static void test_state_verify(struct test_state *test)
{
	struct route_node *rn;
	struct prefix hash_entry[2];

	memset(hash_entry, 0, sizeof(hash_entry));

	/* Verify that there are no elements in the table which have never
	 * been added */
	for (rn = route_top(test->table); rn; rn = srcdest_route_next(rn)) {
		const struct prefix_ipv6 *dst_p, *src_p;

		/* While we are iterating, we hold a lock on the current
		 * route_node,
		 * so all the lock counts we check for take that into account;
		 * in idle
		 * state all the numbers will be exactly one less.
		 *
		 * Also this makes quite some assumptions based on the current
		 * implementation details of route_table and srcdest_table -
		 * another
		 * valid implementation might trigger assertions here.
		 */

		if (rnode_is_dstnode(rn)) {
			struct srcdest_rnode *srn = (struct srcdest_rnode *)rn;
			unsigned int expected_lock = 1; /* We are in the loop */

			if (rn->info
			    != NULL) /* The route node is not internal */
				expected_lock++;
			if (srn->src_table != NULL) /* There's a source table
						       associated with rn */
				expected_lock++;

			if (rn->lock != expected_lock)
				test_failed(
					test,
					"Dest rnode lock count doesn't match expected count!",
					(struct prefix_ipv6 *)&rn->p, NULL);
		} else {
			unsigned int expected_lock = 1; /* We are in the loop */

			if (rn->info
			    != NULL) /* The route node is not internal */
				expected_lock++;

			if (rn->lock != expected_lock) {
				srcdest_rnode_prefixes(
					rn, (const struct prefix **)&dst_p,
					(const struct prefix **)&src_p);

				test_failed(
					test,
					"Src rnode lock count doesn't match expected count!",
					dst_p, src_p);
			}
		}

		if (!rn->info)
			continue;

		assert(rn->info == (void *)0xdeadbeef);

		srcdest_rnode_prefixes(rn, (const struct prefix **)&dst_p,
				       (const struct prefix **)&src_p);
		memcpy(&hash_entry[0], dst_p, sizeof(*dst_p));
		if (src_p)
			memcpy(&hash_entry[1], src_p, sizeof(*src_p));
		else
			memset(&hash_entry[1], 0, sizeof(hash_entry[1]));

		if (hash_lookup(test->log, hash_entry) == NULL)
			test_failed(test, "Route is missing in hash", dst_p,
				    src_p);
	}

	/* Verify that all added elements are still in the table */
	hash_iterate(test->log, verify_log, test);
}

static void get_rand_prefix(struct prng *prng, struct prefix_ipv6 *p)
{
	int i;

	memset(p, 0, sizeof(*p));

	for (i = 0; i < 4; i++)
		p->prefix.s6_addr32[i] = prng_rand(prng);
	p->prefixlen = prng_rand(prng) % 129;
	p->family = AF_INET6;

	apply_mask((struct prefix *)p);
}

static void get_rand_prefix_pair(struct prng *prng, struct prefix_ipv6 *dst_p,
				 struct prefix_ipv6 *src_p)
{
	get_rand_prefix(prng, dst_p);
	if ((prng_rand(prng) % 4) == 0) {
		get_rand_prefix(prng, src_p);
		if (src_p->prefixlen)
			return;
	}

	memset(src_p, 0, sizeof(*src_p));
}

static void test_state_add_rand_route(struct test_state *test,
				      struct prng *prng)
{
	struct prefix_ipv6 dst_p, src_p;

	get_rand_prefix_pair(prng, &dst_p, &src_p);
	test_state_add_route(test, &dst_p, &src_p);
}

static void test_state_del_rand_route(struct test_state *test,
				      struct prng *prng)
{
	struct prefix_ipv6 dst_p, src_p;

	get_rand_prefix_pair(prng, &dst_p, &src_p);
	test_state_del_route(test, &dst_p, &src_p);
}

static void test_state_del_one_route(struct test_state *test, struct prng *prng)
{
	unsigned int which_route;

	if (test->log->count == 0)
		return;

	which_route = prng_rand(prng) % test->log->count;

	struct route_node *rn;
	const struct prefix *dst_p, *src_p;
	struct prefix_ipv6 dst6_p, src6_p;

	for (rn = route_top(test->table); rn; rn = srcdest_route_next(rn)) {
		if (!rn->info)
			continue;
		if (!which_route) {
			route_unlock_node(rn);
			break;
		}
		which_route--;
	}

	assert(rn);
	srcdest_rnode_prefixes(rn, (const struct prefix **)&dst_p,
			       (const struct prefix **)&src_p);
	memcpy(&dst6_p, dst_p, sizeof(dst6_p));
	if (src_p)
		memcpy(&src6_p, src_p, sizeof(src6_p));
	else
		memset(&src6_p, 0, sizeof(src6_p));

	test_state_del_route(test, &dst6_p, &src6_p);
}

static void run_prng_test(void)
{
	struct test_state *test = test_state_new();
	struct prng *prng = prng_new(0);
	size_t i;

	for (i = 0; i < 1000; i++) {
		switch (prng_rand(prng) % 10) {
		case 0:
		case 1:
		case 2:
		case 3:
		case 4:
			test_state_add_rand_route(test, prng);
			break;
		case 5:
		case 6:
		case 7:
			test_state_del_one_route(test, prng);
			break;
		case 8:
		case 9:
			test_state_del_rand_route(test, prng);
			break;
		}
		test_state_verify(test);
	}

	prng_free(prng);
	test_state_free(test);
}

int main(int argc, char *argv[])
{
	run_prng_test();
	printf("PRNG Test successful.\n");
	return 0;
}