test_maps.c 13.2 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
/*
 * Testsuite for eBPF maps
 *
 * Copyright (c) 2014 PLUMgrid, http://plumgrid.com
 * Copyright (c) 2016 Facebook
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of version 2 of the GNU General Public
 * License as published by the Free Software Foundation.
 */

#include <stdio.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <assert.h>
#include <stdlib.h>

#include <sys/wait.h>
#include <sys/resource.h>

#include <linux/bpf.h>

24
#include <bpf/bpf.h>
25
#include "bpf_sys.h"
26
#include "bpf_util.h"
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44

static int map_flags;

static void test_hashmap(int task, void *data)
{
	long long key, next_key, value;
	int fd;

	fd = bpf_map_create(BPF_MAP_TYPE_HASH, sizeof(key), sizeof(value),
			    2, map_flags);
	if (fd < 0) {
		printf("Failed to create hashmap '%s'!\n", strerror(errno));
		exit(1);
	}

	key = 1;
	value = 1234;
	/* Insert key=1 element. */
45
	assert(bpf_map_update_elem(fd, &key, &value, BPF_ANY) == 0);
46 47 48

	value = 0;
	/* BPF_NOEXIST means add new element if it doesn't exist. */
49
	assert(bpf_map_update_elem(fd, &key, &value, BPF_NOEXIST) == -1 &&
50 51 52 53
	       /* key=1 already exists. */
	       errno == EEXIST);

	/* -1 is an invalid flag. */
54 55
	assert(bpf_map_update_elem(fd, &key, &value, -1) == -1 &&
	       errno == EINVAL);
56 57

	/* Check that key=1 can be found. */
58
	assert(bpf_map_lookup_elem(fd, &key, &value) == 0 && value == 1234);
59 60 61

	key = 2;
	/* Check that key=2 is not found. */
62
	assert(bpf_map_lookup_elem(fd, &key, &value) == -1 && errno == ENOENT);
63 64

	/* BPF_EXIST means update existing element. */
65
	assert(bpf_map_update_elem(fd, &key, &value, BPF_EXIST) == -1 &&
66 67 68 69
	       /* key=2 is not there. */
	       errno == ENOENT);

	/* Insert key=2 element. */
70
	assert(bpf_map_update_elem(fd, &key, &value, BPF_NOEXIST) == 0);
71 72 73 74 75

	/* key=1 and key=2 were inserted, check that key=0 cannot be
	 * inserted due to max_entries limit.
	 */
	key = 0;
76
	assert(bpf_map_update_elem(fd, &key, &value, BPF_NOEXIST) == -1 &&
77 78 79 80
	       errno == E2BIG);

	/* Update existing element, though the map is full. */
	key = 1;
81
	assert(bpf_map_update_elem(fd, &key, &value, BPF_EXIST) == 0);
82
	key = 2;
83
	assert(bpf_map_update_elem(fd, &key, &value, BPF_ANY) == 0);
84
	key = 1;
85
	assert(bpf_map_update_elem(fd, &key, &value, BPF_ANY) == 0);
86 87 88

	/* Check that key = 0 doesn't exist. */
	key = 0;
89
	assert(bpf_map_delete_elem(fd, &key) == -1 && errno == ENOENT);
90 91

	/* Iterate over two elements. */
92
	assert(bpf_map_get_next_key(fd, &key, &next_key) == 0 &&
93
	       (next_key == 1 || next_key == 2));
94
	assert(bpf_map_get_next_key(fd, &next_key, &next_key) == 0 &&
95
	       (next_key == 1 || next_key == 2));
96
	assert(bpf_map_get_next_key(fd, &next_key, &next_key) == -1 &&
97 98 99 100
	       errno == ENOENT);

	/* Delete both elements. */
	key = 1;
101
	assert(bpf_map_delete_elem(fd, &key) == 0);
102
	key = 2;
103 104
	assert(bpf_map_delete_elem(fd, &key) == 0);
	assert(bpf_map_delete_elem(fd, &key) == -1 && errno == ENOENT);
105 106 107

	key = 0;
	/* Check that map is empty. */
108
	assert(bpf_map_get_next_key(fd, &key, &next_key) == -1 &&
109 110 111 112 113 114 115
	       errno == ENOENT);

	close(fd);
}

static void test_hashmap_percpu(int task, void *data)
{
116
	unsigned int nr_cpus = bpf_num_possible_cpus();
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
	long long value[nr_cpus];
	long long key, next_key;
	int expected_key_mask = 0;
	int fd, i;

	fd = bpf_map_create(BPF_MAP_TYPE_PERCPU_HASH, sizeof(key),
			    sizeof(value[0]), 2, map_flags);
	if (fd < 0) {
		printf("Failed to create hashmap '%s'!\n", strerror(errno));
		exit(1);
	}

	for (i = 0; i < nr_cpus; i++)
		value[i] = i + 100;

	key = 1;
	/* Insert key=1 element. */
	assert(!(expected_key_mask & key));
135
	assert(bpf_map_update_elem(fd, &key, value, BPF_ANY) == 0);
136 137 138
	expected_key_mask |= key;

	/* BPF_NOEXIST means add new element if it doesn't exist. */
139
	assert(bpf_map_update_elem(fd, &key, value, BPF_NOEXIST) == -1 &&
140 141 142 143
	       /* key=1 already exists. */
	       errno == EEXIST);

	/* -1 is an invalid flag. */
144 145
	assert(bpf_map_update_elem(fd, &key, value, -1) == -1 &&
	       errno == EINVAL);
146 147 148 149 150

	/* Check that key=1 can be found. Value could be 0 if the lookup
	 * was run from a different CPU.
	 */
	value[0] = 1;
151
	assert(bpf_map_lookup_elem(fd, &key, value) == 0 && value[0] == 100);
152 153 154

	key = 2;
	/* Check that key=2 is not found. */
155
	assert(bpf_map_lookup_elem(fd, &key, value) == -1 && errno == ENOENT);
156 157

	/* BPF_EXIST means update existing element. */
158
	assert(bpf_map_update_elem(fd, &key, value, BPF_EXIST) == -1 &&
159 160 161 162 163
	       /* key=2 is not there. */
	       errno == ENOENT);

	/* Insert key=2 element. */
	assert(!(expected_key_mask & key));
164
	assert(bpf_map_update_elem(fd, &key, value, BPF_NOEXIST) == 0);
165 166 167 168 169 170
	expected_key_mask |= key;

	/* key=1 and key=2 were inserted, check that key=0 cannot be
	 * inserted due to max_entries limit.
	 */
	key = 0;
171
	assert(bpf_map_update_elem(fd, &key, value, BPF_NOEXIST) == -1 &&
172 173 174
	       errno == E2BIG);

	/* Check that key = 0 doesn't exist. */
175
	assert(bpf_map_delete_elem(fd, &key) == -1 && errno == ENOENT);
176 177

	/* Iterate over two elements. */
178
	while (!bpf_map_get_next_key(fd, &key, &next_key)) {
179 180 181
		assert((expected_key_mask & next_key) == next_key);
		expected_key_mask &= ~next_key;

182
		assert(bpf_map_lookup_elem(fd, &next_key, value) == 0);
183 184 185 186 187 188 189 190 191 192

		for (i = 0; i < nr_cpus; i++)
			assert(value[i] == i + 100);

		key = next_key;
	}
	assert(errno == ENOENT);

	/* Update with BPF_EXIST. */
	key = 1;
193
	assert(bpf_map_update_elem(fd, &key, value, BPF_EXIST) == 0);
194 195 196

	/* Delete both elements. */
	key = 1;
197
	assert(bpf_map_delete_elem(fd, &key) == 0);
198
	key = 2;
199 200
	assert(bpf_map_delete_elem(fd, &key) == 0);
	assert(bpf_map_delete_elem(fd, &key) == -1 && errno == ENOENT);
201 202 203

	key = 0;
	/* Check that map is empty. */
204
	assert(bpf_map_get_next_key(fd, &key, &next_key) == -1 &&
205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224
	       errno == ENOENT);

	close(fd);
}

static void test_arraymap(int task, void *data)
{
	int key, next_key, fd;
	long long value;

	fd = bpf_map_create(BPF_MAP_TYPE_ARRAY, sizeof(key), sizeof(value),
			    2, 0);
	if (fd < 0) {
		printf("Failed to create arraymap '%s'!\n", strerror(errno));
		exit(1);
	}

	key = 1;
	value = 1234;
	/* Insert key=1 element. */
225
	assert(bpf_map_update_elem(fd, &key, &value, BPF_ANY) == 0);
226 227

	value = 0;
228
	assert(bpf_map_update_elem(fd, &key, &value, BPF_NOEXIST) == -1 &&
229 230 231
	       errno == EEXIST);

	/* Check that key=1 can be found. */
232
	assert(bpf_map_lookup_elem(fd, &key, &value) == 0 && value == 1234);
233 234 235

	key = 0;
	/* Check that key=0 is also found and zero initialized. */
236
	assert(bpf_map_lookup_elem(fd, &key, &value) == 0 && value == 0);
237 238 239 240 241

	/* key=0 and key=1 were inserted, check that key=2 cannot be inserted
	 * due to max_entries limit.
	 */
	key = 2;
242
	assert(bpf_map_update_elem(fd, &key, &value, BPF_EXIST) == -1 &&
243 244 245
	       errno == E2BIG);

	/* Check that key = 2 doesn't exist. */
246
	assert(bpf_map_lookup_elem(fd, &key, &value) == -1 && errno == ENOENT);
247 248

	/* Iterate over two elements. */
249
	assert(bpf_map_get_next_key(fd, &key, &next_key) == 0 &&
250
	       next_key == 0);
251
	assert(bpf_map_get_next_key(fd, &next_key, &next_key) == 0 &&
252
	       next_key == 1);
253
	assert(bpf_map_get_next_key(fd, &next_key, &next_key) == -1 &&
254 255 256 257
	       errno == ENOENT);

	/* Delete shouldn't succeed. */
	key = 1;
258
	assert(bpf_map_delete_elem(fd, &key) == -1 && errno == EINVAL);
259 260 261 262 263 264

	close(fd);
}

static void test_arraymap_percpu(int task, void *data)
{
265
	unsigned int nr_cpus = bpf_num_possible_cpus();
266 267 268 269 270 271 272 273 274 275 276 277 278 279 280
	int key, next_key, fd, i;
	long values[nr_cpus];

	fd = bpf_map_create(BPF_MAP_TYPE_PERCPU_ARRAY, sizeof(key),
			    sizeof(values[0]), 2, 0);
	if (fd < 0) {
		printf("Failed to create arraymap '%s'!\n", strerror(errno));
		exit(1);
	}

	for (i = 0; i < nr_cpus; i++)
		values[i] = i + 100;

	key = 1;
	/* Insert key=1 element. */
281
	assert(bpf_map_update_elem(fd, &key, values, BPF_ANY) == 0);
282 283

	values[0] = 0;
284
	assert(bpf_map_update_elem(fd, &key, values, BPF_NOEXIST) == -1 &&
285 286 287
	       errno == EEXIST);

	/* Check that key=1 can be found. */
288
	assert(bpf_map_lookup_elem(fd, &key, values) == 0 && values[0] == 100);
289 290 291

	key = 0;
	/* Check that key=0 is also found and zero initialized. */
292
	assert(bpf_map_lookup_elem(fd, &key, values) == 0 &&
293 294 295 296
	       values[0] == 0 && values[nr_cpus - 1] == 0);

	/* Check that key=2 cannot be inserted due to max_entries limit. */
	key = 2;
297
	assert(bpf_map_update_elem(fd, &key, values, BPF_EXIST) == -1 &&
298 299 300
	       errno == E2BIG);

	/* Check that key = 2 doesn't exist. */
301
	assert(bpf_map_lookup_elem(fd, &key, values) == -1 && errno == ENOENT);
302 303

	/* Iterate over two elements. */
304
	assert(bpf_map_get_next_key(fd, &key, &next_key) == 0 &&
305
	       next_key == 0);
306
	assert(bpf_map_get_next_key(fd, &next_key, &next_key) == 0 &&
307
	       next_key == 1);
308
	assert(bpf_map_get_next_key(fd, &next_key, &next_key) == -1 &&
309 310 311 312
	       errno == ENOENT);

	/* Delete shouldn't succeed. */
	key = 1;
313
	assert(bpf_map_delete_elem(fd, &key) == -1 && errno == EINVAL);
314 315 316 317 318 319

	close(fd);
}

static void test_arraymap_percpu_many_keys(void)
{
320
	unsigned int nr_cpus = bpf_num_possible_cpus();
321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336
	unsigned int nr_keys = 20000;
	long values[nr_cpus];
	int key, fd, i;

	fd = bpf_map_create(BPF_MAP_TYPE_PERCPU_ARRAY, sizeof(key),
			    sizeof(values[0]), nr_keys, 0);
	if (fd < 0) {
		printf("Failed to create per-cpu arraymap '%s'!\n",
		       strerror(errno));
		exit(1);
	}

	for (i = 0; i < nr_cpus; i++)
		values[i] = i + 10;

	for (key = 0; key < nr_keys; key++)
337
		assert(bpf_map_update_elem(fd, &key, values, BPF_ANY) == 0);
338 339 340 341 342

	for (key = 0; key < nr_keys; key++) {
		for (i = 0; i < nr_cpus; i++)
			values[i] = 0;

343
		assert(bpf_map_lookup_elem(fd, &key, values) == 0);
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

		for (i = 0; i < nr_cpus; i++)
			assert(values[i] == i + 10);
	}

	close(fd);
}

#define MAP_SIZE (32 * 1024)

static void test_map_large(void)
{
	struct bigkey {
		int a;
		char b[116];
		long long c;
	} key;
	int fd, i, value;

	fd = bpf_map_create(BPF_MAP_TYPE_HASH, sizeof(key), sizeof(value),
			    MAP_SIZE, map_flags);
	if (fd < 0) {
		printf("Failed to create large map '%s'!\n", strerror(errno));
		exit(1);
	}

	for (i = 0; i < MAP_SIZE; i++) {
		key = (struct bigkey) { .c = i };
		value = i;

374
		assert(bpf_map_update_elem(fd, &key, &value, BPF_NOEXIST) == 0);
375 376 377
	}

	key.c = -1;
378
	assert(bpf_map_update_elem(fd, &key, &value, BPF_NOEXIST) == -1 &&
379 380 381 382
	       errno == E2BIG);

	/* Iterate through all elements. */
	for (i = 0; i < MAP_SIZE; i++)
383 384
		assert(bpf_map_get_next_key(fd, &key, &key) == 0);
	assert(bpf_map_get_next_key(fd, &key, &key) == -1 && errno == ENOENT);
385 386

	key.c = 0;
387
	assert(bpf_map_lookup_elem(fd, &key, &value) == 0 && value == 0);
388
	key.a = 1;
389
	assert(bpf_map_lookup_elem(fd, &key, &value) == -1 && errno == ENOENT);
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

	close(fd);
}

static void run_parallel(int tasks, void (*fn)(int task, void *data),
			 void *data)
{
	pid_t pid[tasks];
	int i;

	for (i = 0; i < tasks; i++) {
		pid[i] = fork();
		if (pid[i] == 0) {
			fn(i, data);
			exit(0);
		} else if (pid[i] == -1) {
			printf("Couldn't spawn #%d process!\n", i);
			exit(1);
		}
	}

	for (i = 0; i < tasks; i++) {
		int status;

		assert(waitpid(pid[i], &status, 0) == pid[i]);
		assert(status == 0);
	}
}

static void test_map_stress(void)
{
	run_parallel(100, test_hashmap, NULL);
	run_parallel(100, test_hashmap_percpu, NULL);

	run_parallel(100, test_arraymap, NULL);
	run_parallel(100, test_arraymap_percpu, NULL);
}

#define TASKS 1024

#define DO_UPDATE 1
#define DO_DELETE 0

static void do_work(int fn, void *data)
{
	int do_update = ((int *)data)[1];
	int fd = ((int *)data)[0];
	int i, key, value;

	for (i = fn; i < MAP_SIZE; i += TASKS) {
		key = value = i;

		if (do_update) {
443 444 445 446
			assert(bpf_map_update_elem(fd, &key, &value,
						   BPF_NOEXIST) == 0);
			assert(bpf_map_update_elem(fd, &key, &value,
						   BPF_EXIST) == 0);
447
		} else {
448
			assert(bpf_map_delete_elem(fd, &key) == 0);
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
		}
	}
}

static void test_map_parallel(void)
{
	int i, fd, key = 0, value = 0;
	int data[2];

	fd = bpf_map_create(BPF_MAP_TYPE_HASH, sizeof(key), sizeof(value),
			    MAP_SIZE, map_flags);
	if (fd < 0) {
		printf("Failed to create map for parallel test '%s'!\n",
		       strerror(errno));
		exit(1);
	}

	/* Use the same fd in children to add elements to this map:
	 * child_0 adds key=0, key=1024, key=2048, ...
	 * child_1 adds key=1, key=1025, key=2049, ...
	 * child_1023 adds key=1023, ...
	 */
	data[0] = fd;
	data[1] = DO_UPDATE;
	run_parallel(TASKS, do_work, data);

	/* Check that key=0 is already there. */
476
	assert(bpf_map_update_elem(fd, &key, &value, BPF_NOEXIST) == -1 &&
477 478 479 480 481
	       errno == EEXIST);

	/* Check that all elements were inserted. */
	key = -1;
	for (i = 0; i < MAP_SIZE; i++)
482 483
		assert(bpf_map_get_next_key(fd, &key, &key) == 0);
	assert(bpf_map_get_next_key(fd, &key, &key) == -1 && errno == ENOENT);
484 485 486 487 488

	/* Another check for all elements */
	for (i = 0; i < MAP_SIZE; i++) {
		key = MAP_SIZE - i - 1;

489
		assert(bpf_map_lookup_elem(fd, &key, &value) == 0 &&
490 491 492 493 494 495 496 497 498
		       value == key);
	}

	/* Now let's delete all elemenets in parallel. */
	data[1] = DO_DELETE;
	run_parallel(TASKS, do_work, data);

	/* Nothing should be left. */
	key = -1;
499
	assert(bpf_map_get_next_key(fd, &key, &key) == -1 && errno == ENOENT);
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
}

static void run_all_tests(void)
{
	test_hashmap(0, NULL);
	test_hashmap_percpu(0, NULL);

	test_arraymap(0, NULL);
	test_arraymap_percpu(0, NULL);

	test_arraymap_percpu_many_keys();

	test_map_large();
	test_map_parallel();
	test_map_stress();
}

int main(void)
{
	struct rlimit rinf = { RLIM_INFINITY, RLIM_INFINITY };

	setrlimit(RLIMIT_MEMLOCK, &rinf);

	map_flags = 0;
	run_all_tests();

	map_flags = BPF_F_NO_PREALLOC;
	run_all_tests();

	printf("test_maps: OK\n");
	return 0;
}