test_maps.c 25.5 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/libbpf.h>
26
#include "bpf_util.h"
27 28 29 30 31

static int map_flags;

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

35
	fd = bpf_create_map(BPF_MAP_TYPE_HASH, sizeof(key), sizeof(value),
36 37 38 39 40 41 42 43 44
			    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 85 86
	key = 3;
	assert(bpf_map_update_elem(fd, &key, &value, BPF_NOEXIST) == -1 &&
	       errno == E2BIG);
87 88 89

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

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

	/* Delete both elements. */
	key = 1;
105
	assert(bpf_map_delete_elem(fd, &key) == 0);
106
	key = 2;
107 108
	assert(bpf_map_delete_elem(fd, &key) == 0);
	assert(bpf_map_delete_elem(fd, &key) == -1 && errno == ENOENT);
109 110 111

	key = 0;
	/* Check that map is empty. */
112 113
	assert(bpf_map_get_next_key(fd, NULL, &next_key) == -1 &&
	       errno == ENOENT);
114
	assert(bpf_map_get_next_key(fd, &key, &next_key) == -1 &&
115 116 117 118 119
	       errno == ENOENT);

	close(fd);
}

120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
static void test_hashmap_sizes(int task, void *data)
{
	int fd, i, j;

	for (i = 1; i <= 512; i <<= 1)
		for (j = 1; j <= 1 << 18; j <<= 1) {
			fd = bpf_create_map(BPF_MAP_TYPE_HASH, i, j,
					    2, map_flags);
			if (fd < 0) {
				printf("Failed to create hashmap key=%d value=%d '%s'\n",
				       i, j, strerror(errno));
				exit(1);
			}
			close(fd);
			usleep(10); /* give kernel time to destroy */
		}
}

138 139
static void test_hashmap_percpu(int task, void *data)
{
140
	unsigned int nr_cpus = bpf_num_possible_cpus();
141
	BPF_DECLARE_PERCPU(long, value);
142
	long long key, next_key, first_key;
143 144 145
	int expected_key_mask = 0;
	int fd, i;

146
	fd = bpf_create_map(BPF_MAP_TYPE_PERCPU_HASH, sizeof(key),
147
			    sizeof(bpf_percpu(value, 0)), 2, map_flags);
148 149 150 151 152 153
	if (fd < 0) {
		printf("Failed to create hashmap '%s'!\n", strerror(errno));
		exit(1);
	}

	for (i = 0; i < nr_cpus; i++)
154
		bpf_percpu(value, i) = i + 100;
155 156 157 158

	key = 1;
	/* Insert key=1 element. */
	assert(!(expected_key_mask & key));
159
	assert(bpf_map_update_elem(fd, &key, value, BPF_ANY) == 0);
160 161 162
	expected_key_mask |= key;

	/* BPF_NOEXIST means add new element if it doesn't exist. */
163
	assert(bpf_map_update_elem(fd, &key, value, BPF_NOEXIST) == -1 &&
164 165 166 167
	       /* key=1 already exists. */
	       errno == EEXIST);

	/* -1 is an invalid flag. */
168 169
	assert(bpf_map_update_elem(fd, &key, value, -1) == -1 &&
	       errno == EINVAL);
170 171 172 173

	/* Check that key=1 can be found. Value could be 0 if the lookup
	 * was run from a different CPU.
	 */
174 175 176
	bpf_percpu(value, 0) = 1;
	assert(bpf_map_lookup_elem(fd, &key, value) == 0 &&
	       bpf_percpu(value, 0) == 100);
177 178 179

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

	/* BPF_EXIST means update existing element. */
183
	assert(bpf_map_update_elem(fd, &key, value, BPF_EXIST) == -1 &&
184 185 186 187 188
	       /* key=2 is not there. */
	       errno == ENOENT);

	/* Insert key=2 element. */
	assert(!(expected_key_mask & key));
189
	assert(bpf_map_update_elem(fd, &key, value, BPF_NOEXIST) == 0);
190 191 192 193 194 195
	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;
196
	assert(bpf_map_update_elem(fd, &key, value, BPF_NOEXIST) == -1 &&
197 198 199
	       errno == E2BIG);

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

	/* Iterate over two elements. */
203 204
	assert(bpf_map_get_next_key(fd, NULL, &first_key) == 0 &&
	       ((expected_key_mask & first_key) == first_key));
205
	while (!bpf_map_get_next_key(fd, &key, &next_key)) {
206 207 208 209
		if (first_key) {
			assert(next_key == first_key);
			first_key = 0;
		}
210 211 212
		assert((expected_key_mask & next_key) == next_key);
		expected_key_mask &= ~next_key;

213
		assert(bpf_map_lookup_elem(fd, &next_key, value) == 0);
214 215

		for (i = 0; i < nr_cpus; i++)
216
			assert(bpf_percpu(value, i) == i + 100);
217 218 219 220 221 222 223

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

	/* Update with BPF_EXIST. */
	key = 1;
224
	assert(bpf_map_update_elem(fd, &key, value, BPF_EXIST) == 0);
225 226 227

	/* Delete both elements. */
	key = 1;
228
	assert(bpf_map_delete_elem(fd, &key) == 0);
229
	key = 2;
230 231
	assert(bpf_map_delete_elem(fd, &key) == 0);
	assert(bpf_map_delete_elem(fd, &key) == -1 && errno == ENOENT);
232 233 234

	key = 0;
	/* Check that map is empty. */
235 236
	assert(bpf_map_get_next_key(fd, NULL, &next_key) == -1 &&
	       errno == ENOENT);
237
	assert(bpf_map_get_next_key(fd, &key, &next_key) == -1 &&
238 239 240 241 242
	       errno == ENOENT);

	close(fd);
}

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
static void test_hashmap_walk(int task, void *data)
{
	int fd, i, max_entries = 100000;
	long long key, value, next_key;
	bool next_key_valid = true;

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

	for (i = 0; i < max_entries; i++) {
		key = i; value = key;
		assert(bpf_map_update_elem(fd, &key, &value, BPF_NOEXIST) == 0);
	}

	for (i = 0; bpf_map_get_next_key(fd, !i ? NULL : &key,
					 &next_key) == 0; i++) {
		key = next_key;
		assert(bpf_map_lookup_elem(fd, &key, &value) == 0);
	}

	assert(i == max_entries);

	assert(bpf_map_get_next_key(fd, NULL, &key) == 0);
	for (i = 0; next_key_valid; i++) {
		next_key_valid = bpf_map_get_next_key(fd, &key, &next_key) == 0;
		assert(bpf_map_lookup_elem(fd, &key, &value) == 0);
		value++;
		assert(bpf_map_update_elem(fd, &key, &value, BPF_EXIST) == 0);
		key = next_key;
	}

	assert(i == max_entries);

	for (i = 0; bpf_map_get_next_key(fd, !i ? NULL : &key,
					 &next_key) == 0; i++) {
		key = next_key;
		assert(bpf_map_lookup_elem(fd, &key, &value) == 0);
		assert(value - 1 == key);
	}

	assert(i == max_entries);
	close(fd);
}

291 292 293 294 295
static void test_arraymap(int task, void *data)
{
	int key, next_key, fd;
	long long value;

296
	fd = bpf_create_map(BPF_MAP_TYPE_ARRAY, sizeof(key), sizeof(value),
297 298 299 300 301 302 303 304 305
			    2, 0);
	if (fd < 0) {
		printf("Failed to create arraymap '%s'!\n", strerror(errno));
		exit(1);
	}

	key = 1;
	value = 1234;
	/* Insert key=1 element. */
306
	assert(bpf_map_update_elem(fd, &key, &value, BPF_ANY) == 0);
307 308

	value = 0;
309
	assert(bpf_map_update_elem(fd, &key, &value, BPF_NOEXIST) == -1 &&
310 311 312
	       errno == EEXIST);

	/* Check that key=1 can be found. */
313
	assert(bpf_map_lookup_elem(fd, &key, &value) == 0 && value == 1234);
314 315 316

	key = 0;
	/* Check that key=0 is also found and zero initialized. */
317
	assert(bpf_map_lookup_elem(fd, &key, &value) == 0 && value == 0);
318 319 320 321 322

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

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

	/* Iterate over two elements. */
330 331
	assert(bpf_map_get_next_key(fd, NULL, &next_key) == 0 &&
	       next_key == 0);
332
	assert(bpf_map_get_next_key(fd, &key, &next_key) == 0 &&
333
	       next_key == 0);
334
	assert(bpf_map_get_next_key(fd, &next_key, &next_key) == 0 &&
335
	       next_key == 1);
336
	assert(bpf_map_get_next_key(fd, &next_key, &next_key) == -1 &&
337 338 339 340
	       errno == ENOENT);

	/* Delete shouldn't succeed. */
	key = 1;
341
	assert(bpf_map_delete_elem(fd, &key) == -1 && errno == EINVAL);
342 343 344 345 346 347

	close(fd);
}

static void test_arraymap_percpu(int task, void *data)
{
348
	unsigned int nr_cpus = bpf_num_possible_cpus();
349
	BPF_DECLARE_PERCPU(long, values);
350 351
	int key, next_key, fd, i;

352
	fd = bpf_create_map(BPF_MAP_TYPE_PERCPU_ARRAY, sizeof(key),
353
			    sizeof(bpf_percpu(values, 0)), 2, 0);
354 355 356 357 358 359
	if (fd < 0) {
		printf("Failed to create arraymap '%s'!\n", strerror(errno));
		exit(1);
	}

	for (i = 0; i < nr_cpus; i++)
360
		bpf_percpu(values, i) = i + 100;
361 362 363

	key = 1;
	/* Insert key=1 element. */
364
	assert(bpf_map_update_elem(fd, &key, values, BPF_ANY) == 0);
365

366
	bpf_percpu(values, 0) = 0;
367
	assert(bpf_map_update_elem(fd, &key, values, BPF_NOEXIST) == -1 &&
368 369 370
	       errno == EEXIST);

	/* Check that key=1 can be found. */
371 372
	assert(bpf_map_lookup_elem(fd, &key, values) == 0 &&
	       bpf_percpu(values, 0) == 100);
373 374 375

	key = 0;
	/* Check that key=0 is also found and zero initialized. */
376
	assert(bpf_map_lookup_elem(fd, &key, values) == 0 &&
377 378
	       bpf_percpu(values, 0) == 0 &&
	       bpf_percpu(values, nr_cpus - 1) == 0);
379 380 381

	/* Check that key=2 cannot be inserted due to max_entries limit. */
	key = 2;
382
	assert(bpf_map_update_elem(fd, &key, values, BPF_EXIST) == -1 &&
383 384 385
	       errno == E2BIG);

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

	/* Iterate over two elements. */
389 390
	assert(bpf_map_get_next_key(fd, NULL, &next_key) == 0 &&
	       next_key == 0);
391
	assert(bpf_map_get_next_key(fd, &key, &next_key) == 0 &&
392
	       next_key == 0);
393
	assert(bpf_map_get_next_key(fd, &next_key, &next_key) == 0 &&
394
	       next_key == 1);
395
	assert(bpf_map_get_next_key(fd, &next_key, &next_key) == -1 &&
396 397 398 399
	       errno == ENOENT);

	/* Delete shouldn't succeed. */
	key = 1;
400
	assert(bpf_map_delete_elem(fd, &key) == -1 && errno == EINVAL);
401 402 403 404 405 406

	close(fd);
}

static void test_arraymap_percpu_many_keys(void)
{
407
	unsigned int nr_cpus = bpf_num_possible_cpus();
408
	BPF_DECLARE_PERCPU(long, values);
409 410 411 412
	/* nr_keys is not too large otherwise the test stresses percpu
	 * allocator more than anything else
	 */
	unsigned int nr_keys = 2000;
413 414
	int key, fd, i;

415
	fd = bpf_create_map(BPF_MAP_TYPE_PERCPU_ARRAY, sizeof(key),
416
			    sizeof(bpf_percpu(values, 0)), nr_keys, 0);
417 418 419 420 421 422 423
	if (fd < 0) {
		printf("Failed to create per-cpu arraymap '%s'!\n",
		       strerror(errno));
		exit(1);
	}

	for (i = 0; i < nr_cpus; i++)
424
		bpf_percpu(values, i) = i + 10;
425 426

	for (key = 0; key < nr_keys; key++)
427
		assert(bpf_map_update_elem(fd, &key, values, BPF_ANY) == 0);
428 429 430

	for (key = 0; key < nr_keys; key++) {
		for (i = 0; i < nr_cpus; i++)
431
			bpf_percpu(values, i) = 0;
432

433
		assert(bpf_map_lookup_elem(fd, &key, values) == 0);
434 435

		for (i = 0; i < nr_cpus; i++)
436
			assert(bpf_percpu(values, i) == i + 10);
437 438 439 440 441
	}

	close(fd);
}

442 443
static void test_devmap(int task, void *data)
{
J
John Fastabend 已提交
444
	int fd;
445 446 447 448 449 450 451 452 453 454 455 456
	__u32 key, value;

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

	close(fd);
}

457 458 459 460 461 462 463
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <arpa/inet.h>
#include <sys/select.h>
#include <linux/err.h>
#define SOCKMAP_PARSE_PROG "./sockmap_parse_prog.o"
#define SOCKMAP_VERDICT_PROG "./sockmap_verdict_prog.o"
464
static void test_sockmap(int tasks, void *data)
465
{
466 467
	int one = 1, map_fd_rx, map_fd_tx, map_fd_break, s, sc, rc;
	struct bpf_map *bpf_map_rx, *bpf_map_tx, *bpf_map_break;
468 469
	int ports[] = {50200, 50201, 50202, 50204};
	int err, i, fd, sfd[6] = {0xdeadbeef};
470
	u8 buf[20] = {0x0, 0x5, 0x3, 0x2, 0x1, 0x0};
471 472 473 474 475
	int parse_prog, verdict_prog;
	struct sockaddr_in addr;
	struct bpf_object *obj;
	struct timeval to;
	__u32 key, value;
476
	pid_t pid[tasks];
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
	fd_set w;

	/* Create some sockets to use with sockmap */
	for (i = 0; i < 2; i++) {
		sfd[i] = socket(AF_INET, SOCK_STREAM, 0);
		if (sfd[i] < 0)
			goto out;
		err = setsockopt(sfd[i], SOL_SOCKET, SO_REUSEADDR,
				 (char *)&one, sizeof(one));
		if (err) {
			printf("failed to setsockopt\n");
			goto out;
		}
		err = ioctl(sfd[i], FIONBIO, (char *)&one);
		if (err < 0) {
			printf("failed to ioctl\n");
			goto out;
		}
		memset(&addr, 0, sizeof(struct sockaddr_in));
		addr.sin_family = AF_INET;
		addr.sin_addr.s_addr = inet_addr("127.0.0.1");
		addr.sin_port = htons(ports[i]);
		err = bind(sfd[i], (struct sockaddr *)&addr, sizeof(addr));
		if (err < 0) {
			printf("failed to bind: err %i: %i:%i\n",
			       err, i, sfd[i]);
			goto out;
		}
		err = listen(sfd[i], 32);
		if (err < 0) {
507
			printf("failed to listen\n");
508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527
			goto out;
		}
	}

	for (i = 2; i < 4; i++) {
		sfd[i] = socket(AF_INET, SOCK_STREAM, 0);
		if (sfd[i] < 0)
			goto out;
		err = setsockopt(sfd[i], SOL_SOCKET, SO_REUSEADDR,
				 (char *)&one, sizeof(one));
		if (err) {
			printf("set sock opt\n");
			goto out;
		}
		memset(&addr, 0, sizeof(struct sockaddr_in));
		addr.sin_family = AF_INET;
		addr.sin_addr.s_addr = inet_addr("127.0.0.1");
		addr.sin_port = htons(ports[i - 2]);
		err = connect(sfd[i], (struct sockaddr *)&addr, sizeof(addr));
		if (err) {
528
			printf("failed to connect\n");
529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550
			goto out;
		}
	}


	for (i = 4; i < 6; i++) {
		sfd[i] = accept(sfd[i - 4], NULL, NULL);
		if (sfd[i] < 0) {
			printf("accept failed\n");
			goto out;
		}
	}

	/* Test sockmap with connected sockets */
	fd = bpf_create_map(BPF_MAP_TYPE_SOCKMAP,
			    sizeof(key), sizeof(value),
			    6, 0);
	if (fd < 0) {
		printf("Failed to create sockmap %i\n", fd);
		goto out_sockmap;
	}

551
	/* Test update without programs */
552 553
	for (i = 0; i < 6; i++) {
		err = bpf_map_update_elem(fd, &i, &sfd[i], BPF_ANY);
554 555
		if (err) {
			printf("Failed noprog update sockmap '%i:%i'\n",
556 557 558 559 560 561
			       i, sfd[i]);
			goto out_sockmap;
		}
	}

	/* Test attaching bad fds */
562
	err = bpf_prog_attach(-1, fd, BPF_SK_SKB_STREAM_PARSER, 0);
563
	if (!err) {
564 565 566 567 568 569 570
		printf("Failed invalid parser prog attach\n");
		goto out_sockmap;
	}

	err = bpf_prog_attach(-1, fd, BPF_SK_SKB_STREAM_VERDICT, 0);
	if (!err) {
		printf("Failed invalid verdict prog attach\n");
571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588
		goto out_sockmap;
	}

	/* Load SK_SKB program and Attach */
	err = bpf_prog_load(SOCKMAP_PARSE_PROG,
			    BPF_PROG_TYPE_SK_SKB, &obj, &parse_prog);
	if (err) {
		printf("Failed to load SK_SKB parse prog\n");
		goto out_sockmap;
	}

	err = bpf_prog_load(SOCKMAP_VERDICT_PROG,
			    BPF_PROG_TYPE_SK_SKB, &obj, &verdict_prog);
	if (err) {
		printf("Failed to load SK_SKB verdict prog\n");
		goto out_sockmap;
	}

589 590 591
	bpf_map_rx = bpf_object__find_map_by_name(obj, "sock_map_rx");
	if (IS_ERR(bpf_map_rx)) {
		printf("Failed to load map rx from verdict prog\n");
592 593 594
		goto out_sockmap;
	}

595 596
	map_fd_rx = bpf_map__fd(bpf_map_rx);
	if (map_fd_rx < 0) {
597 598 599 600
		printf("Failed to get map fd\n");
		goto out_sockmap;
	}

601 602 603 604 605 606 607 608 609 610 611 612
	bpf_map_tx = bpf_object__find_map_by_name(obj, "sock_map_tx");
	if (IS_ERR(bpf_map_tx)) {
		printf("Failed to load map tx from verdict prog\n");
		goto out_sockmap;
	}

	map_fd_tx = bpf_map__fd(bpf_map_tx);
	if (map_fd_tx < 0) {
		printf("Failed to get map tx fd\n");
		goto out_sockmap;
	}

613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631
	bpf_map_break = bpf_object__find_map_by_name(obj, "sock_map_break");
	if (IS_ERR(bpf_map_break)) {
		printf("Failed to load map tx from verdict prog\n");
		goto out_sockmap;
	}

	map_fd_break = bpf_map__fd(bpf_map_break);
	if (map_fd_break < 0) {
		printf("Failed to get map tx fd\n");
		goto out_sockmap;
	}

	err = bpf_prog_attach(parse_prog, map_fd_break,
			      BPF_SK_SKB_STREAM_PARSER, 0);
	if (!err) {
		printf("Allowed attaching SK_SKB program to invalid map\n");
		goto out_sockmap;
	}

632
	err = bpf_prog_attach(parse_prog, map_fd_rx,
633 634
		      BPF_SK_SKB_STREAM_PARSER, 0);
	if (err) {
635
		printf("Failed stream parser bpf prog attach\n");
636 637 638
		goto out_sockmap;
	}

639
	err = bpf_prog_attach(verdict_prog, map_fd_rx,
640
			      BPF_SK_SKB_STREAM_VERDICT, 0);
641
	if (err) {
642
		printf("Failed stream verdict bpf prog attach\n");
643 644 645
		goto out_sockmap;
	}

646
	/* Test map update elem afterwards fd lives in fd and map_fd */
647
	for (i = 0; i < 6; i++) {
648 649 650 651 652 653 654
		err = bpf_map_update_elem(map_fd_rx, &i, &sfd[i], BPF_ANY);
		if (err) {
			printf("Failed map_fd_rx update sockmap %i '%i:%i'\n",
			       err, i, sfd[i]);
			goto out_sockmap;
		}
		err = bpf_map_update_elem(map_fd_tx, &i, &sfd[i], BPF_ANY);
655
		if (err) {
656
			printf("Failed map_fd_tx update sockmap %i '%i:%i'\n",
657 658 659 660 661 662 663
			       err, i, sfd[i]);
			goto out_sockmap;
		}
	}

	/* Test map delete elem and remove send/recv sockets */
	for (i = 2; i < 4; i++) {
664 665 666 667 668 669 670
		err = bpf_map_delete_elem(map_fd_rx, &i);
		if (err) {
			printf("Failed delete sockmap rx %i '%i:%i'\n",
			       err, i, sfd[i]);
			goto out_sockmap;
		}
		err = bpf_map_delete_elem(map_fd_tx, &i);
671
		if (err) {
672
			printf("Failed delete sockmap tx %i '%i:%i'\n",
673 674 675 676 677 678
			       err, i, sfd[i]);
			goto out_sockmap;
		}
	}

	/* Test map send/recv */
679 680 681 682 683 684 685 686
	for (i = 0; i < 2; i++) {
		buf[0] = i;
		buf[1] = 0x5;
		sc = send(sfd[2], buf, 20, 0);
		if (sc < 0) {
			printf("Failed sockmap send\n");
			goto out_sockmap;
		}
687

688 689 690 691 692 693 694 695 696 697 698 699
		FD_ZERO(&w);
		FD_SET(sfd[3], &w);
		to.tv_sec = 1;
		to.tv_usec = 0;
		s = select(sfd[3] + 1, &w, NULL, NULL, &to);
		if (s == -1) {
			perror("Failed sockmap select()");
			goto out_sockmap;
		} else if (!s) {
			printf("Failed sockmap unexpected timeout\n");
			goto out_sockmap;
		}
700

701 702 703 704 705 706 707 708 709 710
		if (!FD_ISSET(sfd[3], &w)) {
			printf("Failed sockmap select/recv\n");
			goto out_sockmap;
		}

		rc = recv(sfd[3], buf, sizeof(buf), 0);
		if (rc < 0) {
			printf("Failed sockmap recv\n");
			goto out_sockmap;
		}
711 712
	}

713 714 715 716 717 718
	/* Negative null entry lookup from datapath should be dropped */
	buf[0] = 1;
	buf[1] = 12;
	sc = send(sfd[2], buf, 20, 0);
	if (sc < 0) {
		printf("Failed sockmap send\n");
719 720 721
		goto out_sockmap;
	}

722 723
	/* Push fd into same slot */
	i = 2;
724 725
	err = bpf_map_update_elem(fd, &i, &sfd[i], BPF_NOEXIST);
	if (!err) {
726
		printf("Failed allowed sockmap dup slot BPF_NOEXIST\n");
727 728 729 730 731
		goto out_sockmap;
	}

	err = bpf_map_update_elem(fd, &i, &sfd[i], BPF_ANY);
	if (err) {
732
		printf("Failed sockmap update new slot BPF_ANY\n");
733 734 735 736 737
		goto out_sockmap;
	}

	err = bpf_map_update_elem(fd, &i, &sfd[i], BPF_EXIST);
	if (err) {
738
		printf("Failed sockmap update new slot BPF_EXIST\n");
739 740 741
		goto out_sockmap;
	}

742 743 744 745 746 747 748
	/* Delete the elems without programs */
	for (i = 0; i < 6; i++) {
		err = bpf_map_delete_elem(fd, &i);
		if (err) {
			printf("Failed delete sockmap %i '%i:%i'\n",
			       err, i, sfd[i]);
		}
749 750
	}

751 752 753
	/* Test having multiple maps open and set with programs on same fds */
	err = bpf_prog_attach(parse_prog, fd,
			      BPF_SK_SKB_STREAM_PARSER, 0);
754
	if (err) {
755
		printf("Failed fd bpf parse prog attach\n");
756 757
		goto out_sockmap;
	}
758 759
	err = bpf_prog_attach(verdict_prog, fd,
			      BPF_SK_SKB_STREAM_VERDICT, 0);
760
	if (err) {
761
		printf("Failed fd bpf verdict prog attach\n");
762 763 764
		goto out_sockmap;
	}

765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783
	for (i = 4; i < 6; i++) {
		err = bpf_map_update_elem(fd, &i, &sfd[i], BPF_ANY);
		if (!err) {
			printf("Failed allowed duplicate programs in update ANY sockmap %i '%i:%i'\n",
			       err, i, sfd[i]);
			goto out_sockmap;
		}
		err = bpf_map_update_elem(fd, &i, &sfd[i], BPF_NOEXIST);
		if (!err) {
			printf("Failed allowed duplicate program in update NOEXIST sockmap  %i '%i:%i'\n",
			       err, i, sfd[i]);
			goto out_sockmap;
		}
		err = bpf_map_update_elem(fd, &i, &sfd[i], BPF_EXIST);
		if (!err) {
			printf("Failed allowed duplicate program in update EXIST sockmap  %i '%i:%i'\n",
			       err, i, sfd[i]);
			goto out_sockmap;
		}
784 785
	}

786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811
	/* Test tasks number of forked operations */
	for (i = 0; i < tasks; i++) {
		pid[i] = fork();
		if (pid[i] == 0) {
			for (i = 0; i < 6; i++) {
				bpf_map_delete_elem(map_fd_tx, &i);
				bpf_map_delete_elem(map_fd_rx, &i);
				bpf_map_update_elem(map_fd_tx, &i,
						    &sfd[i], BPF_ANY);
				bpf_map_update_elem(map_fd_rx, &i,
						    &sfd[i], BPF_ANY);
			}
			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);
	}

812 813 814 815
	/* Test map close sockets */
	for (i = 0; i < 6; i++)
		close(sfd[i]);
	close(fd);
816
	close(map_fd_rx);
817 818 819 820 821 822 823 824 825 826 827 828 829 830
	bpf_object__close(obj);
	return;
out:
	for (i = 0; i < 6; i++)
		close(sfd[i]);
	printf("Failed to create sockmap '%i:%s'!\n", i, strerror(errno));
	exit(1);
out_sockmap:
	for (i = 0; i < 6; i++)
		close(sfd[i]);
	close(fd);
	exit(1);
}

831 832 833 834 835 836 837 838 839 840 841
#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;

842
	fd = bpf_create_map(BPF_MAP_TYPE_HASH, sizeof(key), sizeof(value),
843 844 845 846 847 848 849 850 851 852
			    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;

853
		assert(bpf_map_update_elem(fd, &key, &value, BPF_NOEXIST) == 0);
854 855 856
	}

	key.c = -1;
857
	assert(bpf_map_update_elem(fd, &key, &value, BPF_NOEXIST) == -1 &&
858 859 860
	       errno == E2BIG);

	/* Iterate through all elements. */
861 862
	assert(bpf_map_get_next_key(fd, NULL, &key) == 0);
	key.c = -1;
863
	for (i = 0; i < MAP_SIZE; i++)
864 865
		assert(bpf_map_get_next_key(fd, &key, &key) == 0);
	assert(bpf_map_get_next_key(fd, &key, &key) == -1 && errno == ENOENT);
866 867

	key.c = 0;
868
	assert(bpf_map_lookup_elem(fd, &key, &value) == 0 && value == 0);
869
	key.a = 1;
870
	assert(bpf_map_lookup_elem(fd, &key, &value) == -1 && errno == ENOENT);
871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903

	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);
904
	run_parallel(100, test_hashmap_sizes, NULL);
905
	run_parallel(100, test_hashmap_walk, NULL);
906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925

	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) {
926 927 928 929
			assert(bpf_map_update_elem(fd, &key, &value,
						   BPF_NOEXIST) == 0);
			assert(bpf_map_update_elem(fd, &key, &value,
						   BPF_EXIST) == 0);
930
		} else {
931
			assert(bpf_map_delete_elem(fd, &key) == 0);
932 933 934 935 936 937 938 939 940
		}
	}
}

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

941
	fd = bpf_create_map(BPF_MAP_TYPE_HASH, sizeof(key), sizeof(value),
942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958
			    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. */
959
	assert(bpf_map_update_elem(fd, &key, &value, BPF_NOEXIST) == -1 &&
960 961 962
	       errno == EEXIST);

	/* Check that all elements were inserted. */
963
	assert(bpf_map_get_next_key(fd, NULL, &key) == 0);
964 965
	key = -1;
	for (i = 0; i < MAP_SIZE; i++)
966 967
		assert(bpf_map_get_next_key(fd, &key, &key) == 0);
	assert(bpf_map_get_next_key(fd, &key, &key) == -1 && errno == ENOENT);
968 969 970 971 972

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

973
		assert(bpf_map_lookup_elem(fd, &key, &value) == 0 &&
974 975 976 977 978 979 980 981 982
		       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;
983
	assert(bpf_map_get_next_key(fd, NULL, &key) == -1 && errno == ENOENT);
984
	assert(bpf_map_get_next_key(fd, &key, &key) == -1 && errno == ENOENT);
985 986 987 988 989 990
}

static void run_all_tests(void)
{
	test_hashmap(0, NULL);
	test_hashmap_percpu(0, NULL);
991
	test_hashmap_walk(0, NULL);
992 993 994 995 996 997

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

	test_arraymap_percpu_many_keys();

J
John Fastabend 已提交
998
	test_devmap(0, NULL);
999
	test_sockmap(0, NULL);
J
John Fastabend 已提交
1000

1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020
	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;
}