bpf_iter.c 35.6 KB
Newer Older
1 2 3 4 5 6 7
// SPDX-License-Identifier: GPL-2.0
/* Copyright (c) 2020 Facebook */
#include <test_progs.h>
#include "bpf_iter_ipv6_route.skel.h"
#include "bpf_iter_netlink.skel.h"
#include "bpf_iter_bpf_map.skel.h"
#include "bpf_iter_task.skel.h"
8
#include "bpf_iter_task_stack.skel.h"
9
#include "bpf_iter_task_file.skel.h"
10
#include "bpf_iter_task_vma.skel.h"
11
#include "bpf_iter_task_btf.skel.h"
12 13 14 15
#include "bpf_iter_tcp4.skel.h"
#include "bpf_iter_tcp6.skel.h"
#include "bpf_iter_udp4.skel.h"
#include "bpf_iter_udp6.skel.h"
16
#include "bpf_iter_unix.skel.h"
17 18 19 20
#include "bpf_iter_test_kern1.skel.h"
#include "bpf_iter_test_kern2.skel.h"
#include "bpf_iter_test_kern3.skel.h"
#include "bpf_iter_test_kern4.skel.h"
21 22
#include "bpf_iter_bpf_hash_map.skel.h"
#include "bpf_iter_bpf_percpu_hash_map.skel.h"
23 24
#include "bpf_iter_bpf_array_map.skel.h"
#include "bpf_iter_bpf_percpu_array_map.skel.h"
25
#include "bpf_iter_bpf_sk_storage_helpers.skel.h"
26
#include "bpf_iter_bpf_sk_storage_map.skel.h"
27
#include "bpf_iter_test_kern5.skel.h"
28
#include "bpf_iter_test_kern6.skel.h"
29
#include "bpf_iter_bpf_link.skel.h"
30
#include "bpf_iter_ksym.skel.h"
31
#include "bpf_iter_sockmap.skel.h"
32 33 34 35 36 37 38 39

static int duration;

static void test_btf_id_or_null(void)
{
	struct bpf_iter_test_kern3 *skel;

	skel = bpf_iter_test_kern3__open_and_load();
40
	if (!ASSERT_ERR_PTR(skel, "bpf_iter_test_kern3__open_and_load")) {
41 42 43 44 45 46 47 48 49 50 51 52
		bpf_iter_test_kern3__destroy(skel);
		return;
	}
}

static void do_dummy_read(struct bpf_program *prog)
{
	struct bpf_link *link;
	char buf[16] = {};
	int iter_fd, len;

	link = bpf_program__attach_iter(prog, NULL);
53
	if (!ASSERT_OK_PTR(link, "attach_iter"))
54 55 56
		return;

	iter_fd = bpf_iter_create(bpf_link__fd(link));
57
	if (!ASSERT_GE(iter_fd, 0, "create_iter"))
58 59 60 61 62 63 64 65 66 67 68 69 70
		goto free_link;

	/* not check contents, but ensure read() ends without error */
	while ((len = read(iter_fd, buf, sizeof(buf))) > 0)
		;
	CHECK(len < 0, "read", "read failed: %s\n", strerror(errno));

	close(iter_fd);

free_link:
	bpf_link__destroy(link);
}

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
static void do_read_map_iter_fd(struct bpf_object_skeleton **skel, struct bpf_program *prog,
				struct bpf_map *map)
{
	DECLARE_LIBBPF_OPTS(bpf_iter_attach_opts, opts);
	union bpf_iter_link_info linfo;
	struct bpf_link *link;
	char buf[16] = {};
	int iter_fd, len;

	memset(&linfo, 0, sizeof(linfo));
	linfo.map.map_fd = bpf_map__fd(map);
	opts.link_info = &linfo;
	opts.link_info_len = sizeof(linfo);
	link = bpf_program__attach_iter(prog, &opts);
	if (!ASSERT_OK_PTR(link, "attach_map_iter"))
		return;

	iter_fd = bpf_iter_create(bpf_link__fd(link));
	if (!ASSERT_GE(iter_fd, 0, "create_map_iter")) {
		bpf_link__destroy(link);
		return;
	}

	/* Close link and map fd prematurely */
	bpf_link__destroy(link);
	bpf_object__destroy_skeleton(*skel);
	*skel = NULL;

	/* Try to let map free work to run first if map is freed */
	usleep(100);
	/* Memory used by both sock map and sock local storage map are
	 * freed after two synchronize_rcu() calls, so wait for it
	 */
	kern_sync_rcu();
	kern_sync_rcu();

	/* Read after both map fd and link fd are closed */
	while ((len = read(iter_fd, buf, sizeof(buf))) > 0)
		;
	ASSERT_GE(len, 0, "read_iterator");

	close(iter_fd);
}

115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
static int read_fd_into_buffer(int fd, char *buf, int size)
{
	int bufleft = size;
	int len;

	do {
		len = read(fd, buf, bufleft);
		if (len > 0) {
			buf += len;
			bufleft -= len;
		}
	} while (len > 0);

	return len < 0 ? len : size - bufleft;
}

131 132 133 134 135
static void test_ipv6_route(void)
{
	struct bpf_iter_ipv6_route *skel;

	skel = bpf_iter_ipv6_route__open_and_load();
136
	if (!ASSERT_OK_PTR(skel, "bpf_iter_ipv6_route__open_and_load"))
137 138 139 140 141 142 143 144 145 146 147 148
		return;

	do_dummy_read(skel->progs.dump_ipv6_route);

	bpf_iter_ipv6_route__destroy(skel);
}

static void test_netlink(void)
{
	struct bpf_iter_netlink *skel;

	skel = bpf_iter_netlink__open_and_load();
149
	if (!ASSERT_OK_PTR(skel, "bpf_iter_netlink__open_and_load"))
150 151 152 153 154 155 156 157 158 159 160 161
		return;

	do_dummy_read(skel->progs.dump_netlink);

	bpf_iter_netlink__destroy(skel);
}

static void test_bpf_map(void)
{
	struct bpf_iter_bpf_map *skel;

	skel = bpf_iter_bpf_map__open_and_load();
162
	if (!ASSERT_OK_PTR(skel, "bpf_iter_bpf_map__open_and_load"))
163 164 165 166 167 168 169 170 171 172 173 174
		return;

	do_dummy_read(skel->progs.dump_bpf_map);

	bpf_iter_bpf_map__destroy(skel);
}

static void test_task(void)
{
	struct bpf_iter_task *skel;

	skel = bpf_iter_task__open_and_load();
175
	if (!ASSERT_OK_PTR(skel, "bpf_iter_task__open_and_load"))
176 177 178 179 180 181 182
		return;

	do_dummy_read(skel->progs.dump_task);

	bpf_iter_task__destroy(skel);
}

183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200
static void test_task_sleepable(void)
{
	struct bpf_iter_task *skel;

	skel = bpf_iter_task__open_and_load();
	if (!ASSERT_OK_PTR(skel, "bpf_iter_task__open_and_load"))
		return;

	do_dummy_read(skel->progs.dump_task_sleepable);

	ASSERT_GT(skel->bss->num_expected_failure_copy_from_user_task, 0,
		  "num_expected_failure_copy_from_user_task");
	ASSERT_GT(skel->bss->num_success_copy_from_user_task, 0,
		  "num_success_copy_from_user_task");

	bpf_iter_task__destroy(skel);
}

201 202 203 204 205
static void test_task_stack(void)
{
	struct bpf_iter_task_stack *skel;

	skel = bpf_iter_task_stack__open_and_load();
206
	if (!ASSERT_OK_PTR(skel, "bpf_iter_task_stack__open_and_load"))
207 208 209
		return;

	do_dummy_read(skel->progs.dump_task_stack);
210
	do_dummy_read(skel->progs.get_task_user_stacks);
211 212 213 214

	bpf_iter_task_stack__destroy(skel);
}

215 216 217 218 219
static void *do_nothing(void *arg)
{
	pthread_exit(arg);
}

220 221 222
static void test_task_file(void)
{
	struct bpf_iter_task_file *skel;
223 224
	pthread_t thread_id;
	void *ret;
225 226

	skel = bpf_iter_task_file__open_and_load();
227
	if (!ASSERT_OK_PTR(skel, "bpf_iter_task_file__open_and_load"))
228 229
		return;

230 231
	skel->bss->tgid = getpid();

232 233
	if (!ASSERT_OK(pthread_create(&thread_id, NULL, &do_nothing, NULL),
		  "pthread_create"))
234 235
		goto done;

236 237
	do_dummy_read(skel->progs.dump_task_file);

238 239
	if (!ASSERT_FALSE(pthread_join(thread_id, &ret) || ret != NULL,
		  "pthread_join"))
240 241
		goto done;

242
	ASSERT_EQ(skel->bss->count, 0, "check_count");
243 244

done:
245 246 247
	bpf_iter_task_file__destroy(skel);
}

248 249 250 251
#define TASKBUFSZ		32768

static char taskbuf[TASKBUFSZ];

252
static int do_btf_read(struct bpf_iter_task_btf *skel)
253 254 255
{
	struct bpf_program *prog = skel->progs.dump_task_struct;
	struct bpf_iter_task_btf__bss *bss = skel->bss;
256
	int iter_fd = -1, err;
257 258
	struct bpf_link *link;
	char *buf = taskbuf;
259
	int ret = 0;
260 261

	link = bpf_program__attach_iter(prog, NULL);
262
	if (!ASSERT_OK_PTR(link, "attach_iter"))
263
		return ret;
264 265

	iter_fd = bpf_iter_create(bpf_link__fd(link));
266
	if (!ASSERT_GE(iter_fd, 0, "create_iter"))
267 268
		goto free_link;

269
	err = read_fd_into_buffer(iter_fd, buf, TASKBUFSZ);
270 271
	if (bss->skip) {
		printf("%s:SKIP:no __builtin_btf_type_id\n", __func__);
272
		ret = 1;
273 274 275 276
		test__skip();
		goto free_link;
	}

277
	if (CHECK(err < 0, "read", "read failed: %s\n", strerror(errno)))
278 279
		goto free_link;

280 281
	ASSERT_HAS_SUBSTR(taskbuf, "(struct task_struct)",
	      "check for btf representation of task_struct in iter data");
282 283 284 285
free_link:
	if (iter_fd > 0)
		close(iter_fd);
	bpf_link__destroy(link);
286
	return ret;
287 288 289 290 291 292
}

static void test_task_btf(void)
{
	struct bpf_iter_task_btf__bss *bss;
	struct bpf_iter_task_btf *skel;
293
	int ret;
294 295

	skel = bpf_iter_task_btf__open_and_load();
296
	if (!ASSERT_OK_PTR(skel, "bpf_iter_task_btf__open_and_load"))
297 298 299 300
		return;

	bss = skel->bss;

301 302 303
	ret = do_btf_read(skel);
	if (ret)
		goto cleanup;
304

305
	if (!ASSERT_NEQ(bss->tasks, 0, "no task iteration, did BPF program run?"))
306 307
		goto cleanup;

308
	ASSERT_EQ(bss->seq_err, 0, "check for unexpected err");
309 310 311 312 313

cleanup:
	bpf_iter_task_btf__destroy(skel);
}

314 315 316 317 318
static void test_tcp4(void)
{
	struct bpf_iter_tcp4 *skel;

	skel = bpf_iter_tcp4__open_and_load();
319
	if (!ASSERT_OK_PTR(skel, "bpf_iter_tcp4__open_and_load"))
320 321 322 323 324 325 326 327 328 329 330 331
		return;

	do_dummy_read(skel->progs.dump_tcp4);

	bpf_iter_tcp4__destroy(skel);
}

static void test_tcp6(void)
{
	struct bpf_iter_tcp6 *skel;

	skel = bpf_iter_tcp6__open_and_load();
332
	if (!ASSERT_OK_PTR(skel, "bpf_iter_tcp6__open_and_load"))
333 334 335 336 337 338 339 340 341 342 343 344
		return;

	do_dummy_read(skel->progs.dump_tcp6);

	bpf_iter_tcp6__destroy(skel);
}

static void test_udp4(void)
{
	struct bpf_iter_udp4 *skel;

	skel = bpf_iter_udp4__open_and_load();
345
	if (!ASSERT_OK_PTR(skel, "bpf_iter_udp4__open_and_load"))
346 347 348 349 350 351 352 353 354 355 356 357
		return;

	do_dummy_read(skel->progs.dump_udp4);

	bpf_iter_udp4__destroy(skel);
}

static void test_udp6(void)
{
	struct bpf_iter_udp6 *skel;

	skel = bpf_iter_udp6__open_and_load();
358
	if (!ASSERT_OK_PTR(skel, "bpf_iter_udp6__open_and_load"))
359 360 361 362 363 364 365
		return;

	do_dummy_read(skel->progs.dump_udp6);

	bpf_iter_udp6__destroy(skel);
}

366 367 368 369 370 371 372 373 374 375 376 377 378
static void test_unix(void)
{
	struct bpf_iter_unix *skel;

	skel = bpf_iter_unix__open_and_load();
	if (!ASSERT_OK_PTR(skel, "bpf_iter_unix__open_and_load"))
		return;

	do_dummy_read(skel->progs.dump_unix);

	bpf_iter_unix__destroy(skel);
}

379 380 381 382
/* The expected string is less than 16 bytes */
static int do_read_with_fd(int iter_fd, const char *expected,
			   bool read_one_char)
{
383
	int len, read_buf_len, start;
384 385 386 387 388 389 390 391 392 393 394 395 396
	char buf[16] = {};

	read_buf_len = read_one_char ? 1 : 16;
	start = 0;
	while ((len = read(iter_fd, buf + start, read_buf_len)) > 0) {
		start += len;
		if (CHECK(start >= 16, "read", "read len %d\n", len))
			return -1;
		read_buf_len = read_one_char ? 1 : 16 - start;
	}
	if (CHECK(len < 0, "read", "read failed: %s\n", strerror(errno)))
		return -1;

397
	if (!ASSERT_STREQ(buf, expected, "read"))
398 399 400 401 402 403 404 405 406 407 408 409
		return -1;

	return 0;
}

static void test_anon_iter(bool read_one_char)
{
	struct bpf_iter_test_kern1 *skel;
	struct bpf_link *link;
	int iter_fd, err;

	skel = bpf_iter_test_kern1__open_and_load();
410
	if (!ASSERT_OK_PTR(skel, "bpf_iter_test_kern1__open_and_load"))
411 412 413
		return;

	err = bpf_iter_test_kern1__attach(skel);
414
	if (!ASSERT_OK(err, "bpf_iter_test_kern1__attach")) {
415 416 417 418 419
		goto out;
	}

	link = skel->links.dump_task;
	iter_fd = bpf_iter_create(bpf_link__fd(link));
420
	if (!ASSERT_GE(iter_fd, 0, "create_iter"))
421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452
		goto out;

	do_read_with_fd(iter_fd, "abcd", read_one_char);
	close(iter_fd);

out:
	bpf_iter_test_kern1__destroy(skel);
}

static int do_read(const char *path, const char *expected)
{
	int err, iter_fd;

	iter_fd = open(path, O_RDONLY);
	if (CHECK(iter_fd < 0, "open", "open %s failed: %s\n",
		  path, strerror(errno)))
		return -1;

	err = do_read_with_fd(iter_fd, expected, false);
	close(iter_fd);
	return err;
}

static void test_file_iter(void)
{
	const char *path = "/sys/fs/bpf/bpf_iter_test1";
	struct bpf_iter_test_kern1 *skel1;
	struct bpf_iter_test_kern2 *skel2;
	struct bpf_link *link;
	int err;

	skel1 = bpf_iter_test_kern1__open_and_load();
453
	if (!ASSERT_OK_PTR(skel1, "bpf_iter_test_kern1__open_and_load"))
454 455 456
		return;

	link = bpf_program__attach_iter(skel1->progs.dump_task, NULL);
457
	if (!ASSERT_OK_PTR(link, "attach_iter"))
458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475
		goto out;

	/* unlink this path if it exists. */
	unlink(path);

	err = bpf_link__pin(link, path);
	if (CHECK(err, "pin_iter", "pin_iter to %s failed: %d\n", path, err))
		goto free_link;

	err = do_read(path, "abcd");
	if (err)
		goto unlink_path;

	/* file based iterator seems working fine. Let us a link update
	 * of the underlying link and `cat` the iterator again, its content
	 * should change.
	 */
	skel2 = bpf_iter_test_kern2__open_and_load();
476
	if (!ASSERT_OK_PTR(skel2, "bpf_iter_test_kern2__open_and_load"))
477 478 479
		goto unlink_path;

	err = bpf_link__update_program(link, skel2->progs.dump_task);
480
	if (!ASSERT_OK(err, "update_prog"))
481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501
		goto destroy_skel2;

	do_read(path, "ABCD");

destroy_skel2:
	bpf_iter_test_kern2__destroy(skel2);
unlink_path:
	unlink(path);
free_link:
	bpf_link__destroy(link);
out:
	bpf_iter_test_kern1__destroy(skel1);
}

static void test_overflow(bool test_e2big_overflow, bool ret1)
{
	__u32 map_info_len, total_read_len, expected_read_len;
	int err, iter_fd, map1_fd, map2_fd, len;
	struct bpf_map_info map_info = {};
	struct bpf_iter_test_kern4 *skel;
	struct bpf_link *link;
502
	__u32 iter_size;
503 504 505
	char *buf;

	skel = bpf_iter_test_kern4__open();
506
	if (!ASSERT_OK_PTR(skel, "bpf_iter_test_kern4__open"))
507 508 509 510 511 512 513
		return;

	/* create two maps: bpf program will only do bpf_seq_write
	 * for these two maps. The goal is one map output almost
	 * fills seq_file buffer and then the other will trigger
	 * overflow and needs restart.
	 */
514 515
	map1_fd = bpf_map_create(BPF_MAP_TYPE_ARRAY, NULL, 4, 8, 1, NULL);
	if (CHECK(map1_fd < 0, "bpf_map_create",
516 517
		  "map_creation failed: %s\n", strerror(errno)))
		goto out;
518 519
	map2_fd = bpf_map_create(BPF_MAP_TYPE_ARRAY, NULL, 4, 8, 1, NULL);
	if (CHECK(map2_fd < 0, "bpf_map_create",
520 521 522
		  "map_creation failed: %s\n", strerror(errno)))
		goto free_map1;

523
	/* bpf_seq_printf kernel buffer is 8 pages, so one map
524 525 526 527
	 * bpf_seq_write will mostly fill it, and the other map
	 * will partially fill and then trigger overflow and need
	 * bpf_seq_read restart.
	 */
528
	iter_size = sysconf(_SC_PAGE_SIZE) << 3;
529 530

	if (test_e2big_overflow) {
531 532
		skel->rodata->print_len = (iter_size + 8) / 8;
		expected_read_len = 2 * (iter_size + 8);
533
	} else if (!ret1) {
534 535
		skel->rodata->print_len = (iter_size - 8) / 8;
		expected_read_len = 2 * (iter_size - 8);
536 537 538 539 540 541
	} else {
		skel->rodata->print_len = 1;
		expected_read_len = 2 * 8;
	}
	skel->rodata->ret1 = ret1;

542 543
	if (!ASSERT_OK(bpf_iter_test_kern4__load(skel),
		  "bpf_iter_test_kern4__load"))
544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560
		goto free_map2;

	/* setup filtering map_id in bpf program */
	map_info_len = sizeof(map_info);
	err = bpf_obj_get_info_by_fd(map1_fd, &map_info, &map_info_len);
	if (CHECK(err, "get_map_info", "get map info failed: %s\n",
		  strerror(errno)))
		goto free_map2;
	skel->bss->map1_id = map_info.id;

	err = bpf_obj_get_info_by_fd(map2_fd, &map_info, &map_info_len);
	if (CHECK(err, "get_map_info", "get map info failed: %s\n",
		  strerror(errno)))
		goto free_map2;
	skel->bss->map2_id = map_info.id;

	link = bpf_program__attach_iter(skel->progs.dump_bpf_map, NULL);
561
	if (!ASSERT_OK_PTR(link, "attach_iter"))
562 563 564
		goto free_map2;

	iter_fd = bpf_iter_create(bpf_link__fd(link));
565
	if (!ASSERT_GE(iter_fd, 0, "create_iter"))
566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600
		goto free_link;

	buf = malloc(expected_read_len);
	if (!buf)
		goto close_iter;

	/* do read */
	total_read_len = 0;
	if (test_e2big_overflow) {
		while ((len = read(iter_fd, buf, expected_read_len)) > 0)
			total_read_len += len;

		CHECK(len != -1 || errno != E2BIG, "read",
		      "expected ret -1, errno E2BIG, but get ret %d, error %s\n",
			  len, strerror(errno));
		goto free_buf;
	} else if (!ret1) {
		while ((len = read(iter_fd, buf, expected_read_len)) > 0)
			total_read_len += len;

		if (CHECK(len < 0, "read", "read failed: %s\n",
			  strerror(errno)))
			goto free_buf;
	} else {
		do {
			len = read(iter_fd, buf, expected_read_len);
			if (len > 0)
				total_read_len += len;
		} while (len > 0 || len == -EAGAIN);

		if (CHECK(len < 0, "read", "read failed: %s\n",
			  strerror(errno)))
			goto free_buf;
	}

601
	if (!ASSERT_EQ(total_read_len, expected_read_len, "read"))
602 603
		goto free_buf;

604
	if (!ASSERT_EQ(skel->bss->map1_accessed, 1, "map1_accessed"))
605 606
		goto free_buf;

607
	if (!ASSERT_EQ(skel->bss->map2_accessed, 2, "map2_accessed"))
608 609
		goto free_buf;

610
	ASSERT_EQ(skel->bss->map2_seqnum1, skel->bss->map2_seqnum2, "map2_seqnum");
611 612 613 614 615 616 617 618 619 620 621 622 623 624 625

free_buf:
	free(buf);
close_iter:
	close(iter_fd);
free_link:
	bpf_link__destroy(link);
free_map2:
	close(map2_fd);
free_map1:
	close(map1_fd);
out:
	bpf_iter_test_kern4__destroy(skel);
}

626 627
static void test_bpf_hash_map(void)
{
628
	__u32 expected_key_a = 0, expected_key_b = 0;
629 630 631
	DECLARE_LIBBPF_OPTS(bpf_iter_attach_opts, opts);
	struct bpf_iter_bpf_hash_map *skel;
	int err, i, len, map_fd, iter_fd;
632
	union bpf_iter_link_info linfo;
633 634 635 636 637 638 639 640 641 642
	__u64 val, expected_val = 0;
	struct bpf_link *link;
	struct key_t {
		int a;
		int b;
		int c;
	} key;
	char buf[64];

	skel = bpf_iter_bpf_hash_map__open();
643
	if (!ASSERT_OK_PTR(skel, "bpf_iter_bpf_hash_map__open"))
644 645 646 647 648
		return;

	skel->bss->in_test_mode = true;

	err = bpf_iter_bpf_hash_map__load(skel);
649
	if (!ASSERT_OK(err, "bpf_iter_bpf_hash_map__load"))
650 651 652
		goto out;

	/* iterator with hashmap2 and hashmap3 should fail */
653 654 655 656
	memset(&linfo, 0, sizeof(linfo));
	linfo.map.map_fd = bpf_map__fd(skel->maps.hashmap2);
	opts.link_info = &linfo;
	opts.link_info_len = sizeof(linfo);
657
	link = bpf_program__attach_iter(skel->progs.dump_bpf_hash_map, &opts);
658
	if (!ASSERT_ERR_PTR(link, "attach_iter"))
659 660
		goto out;

661
	linfo.map.map_fd = bpf_map__fd(skel->maps.hashmap3);
662
	link = bpf_program__attach_iter(skel->progs.dump_bpf_hash_map, &opts);
663
	if (!ASSERT_ERR_PTR(link, "attach_iter"))
664 665 666 667 668 669 670 671 672 673 674 675 676 677
		goto out;

	/* hashmap1 should be good, update map values here */
	map_fd = bpf_map__fd(skel->maps.hashmap1);
	for (i = 0; i < bpf_map__max_entries(skel->maps.hashmap1); i++) {
		key.a = i + 1;
		key.b = i + 2;
		key.c = i + 3;
		val = i + 4;
		expected_key_a += key.a;
		expected_key_b += key.b;
		expected_val += val;

		err = bpf_map_update_elem(map_fd, &key, &val, BPF_ANY);
678
		if (!ASSERT_OK(err, "map_update"))
679 680 681
			goto out;
	}

682 683 684 685 686 687
	/* Sleepable program is prohibited for hash map iterator */
	linfo.map.map_fd = map_fd;
	link = bpf_program__attach_iter(skel->progs.sleepable_dummy_dump, &opts);
	if (!ASSERT_ERR_PTR(link, "attach_sleepable_prog_to_iter"))
		goto out;

688
	linfo.map.map_fd = map_fd;
689
	link = bpf_program__attach_iter(skel->progs.dump_bpf_hash_map, &opts);
690
	if (!ASSERT_OK_PTR(link, "attach_iter"))
691 692 693
		goto out;

	iter_fd = bpf_iter_create(bpf_link__fd(link));
694
	if (!ASSERT_GE(iter_fd, 0, "create_iter"))
695 696 697 698 699 700 701 702 703
		goto free_link;

	/* do some tests */
	while ((len = read(iter_fd, buf, sizeof(buf))) > 0)
		;
	if (CHECK(len < 0, "read", "read failed: %s\n", strerror(errno)))
		goto close_iter;

	/* test results */
704
	if (!ASSERT_EQ(skel->bss->key_sum_a, expected_key_a, "key_sum_a"))
705
		goto close_iter;
706
	if (!ASSERT_EQ(skel->bss->key_sum_b, expected_key_b, "key_sum_b"))
707
		goto close_iter;
708
	if (!ASSERT_EQ(skel->bss->val_sum, expected_val, "val_sum"))
709 710 711 712 713 714 715 716 717 718 719 720
		goto close_iter;

close_iter:
	close(iter_fd);
free_link:
	bpf_link__destroy(link);
out:
	bpf_iter_bpf_hash_map__destroy(skel);
}

static void test_bpf_percpu_hash_map(void)
{
721
	__u32 expected_key_a = 0, expected_key_b = 0;
722 723 724
	DECLARE_LIBBPF_OPTS(bpf_iter_attach_opts, opts);
	struct bpf_iter_bpf_percpu_hash_map *skel;
	int err, i, j, len, map_fd, iter_fd;
725
	union bpf_iter_link_info linfo;
726 727 728 729 730 731 732 733 734 735 736
	__u32 expected_val = 0;
	struct bpf_link *link;
	struct key_t {
		int a;
		int b;
		int c;
	} key;
	char buf[64];
	void *val;

	skel = bpf_iter_bpf_percpu_hash_map__open();
737
	if (!ASSERT_OK_PTR(skel, "bpf_iter_bpf_percpu_hash_map__open"))
738 739 740
		return;

	skel->rodata->num_cpus = bpf_num_possible_cpus();
741
	val = malloc(8 * bpf_num_possible_cpus());
742 743

	err = bpf_iter_bpf_percpu_hash_map__load(skel);
744
	if (!ASSERT_OK_PTR(skel, "bpf_iter_bpf_percpu_hash_map__load"))
745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761
		goto out;

	/* update map values here */
	map_fd = bpf_map__fd(skel->maps.hashmap1);
	for (i = 0; i < bpf_map__max_entries(skel->maps.hashmap1); i++) {
		key.a = i + 1;
		key.b = i + 2;
		key.c = i + 3;
		expected_key_a += key.a;
		expected_key_b += key.b;

		for (j = 0; j < bpf_num_possible_cpus(); j++) {
			*(__u32 *)(val + j * 8) = i + j;
			expected_val += i + j;
		}

		err = bpf_map_update_elem(map_fd, &key, val, BPF_ANY);
762
		if (!ASSERT_OK(err, "map_update"))
763 764 765
			goto out;
	}

766 767 768 769
	memset(&linfo, 0, sizeof(linfo));
	linfo.map.map_fd = map_fd;
	opts.link_info = &linfo;
	opts.link_info_len = sizeof(linfo);
770
	link = bpf_program__attach_iter(skel->progs.dump_bpf_percpu_hash_map, &opts);
771
	if (!ASSERT_OK_PTR(link, "attach_iter"))
772 773 774
		goto out;

	iter_fd = bpf_iter_create(bpf_link__fd(link));
775
	if (!ASSERT_GE(iter_fd, 0, "create_iter"))
776 777 778 779 780 781 782 783 784
		goto free_link;

	/* do some tests */
	while ((len = read(iter_fd, buf, sizeof(buf))) > 0)
		;
	if (CHECK(len < 0, "read", "read failed: %s\n", strerror(errno)))
		goto close_iter;

	/* test results */
785
	if (!ASSERT_EQ(skel->bss->key_sum_a, expected_key_a, "key_sum_a"))
786
		goto close_iter;
787
	if (!ASSERT_EQ(skel->bss->key_sum_b, expected_key_b, "key_sum_b"))
788
		goto close_iter;
789
	if (!ASSERT_EQ(skel->bss->val_sum, expected_val, "val_sum"))
790 791 792 793 794 795 796 797
		goto close_iter;

close_iter:
	close(iter_fd);
free_link:
	bpf_link__destroy(link);
out:
	bpf_iter_bpf_percpu_hash_map__destroy(skel);
798
	free(val);
799 800
}

801 802 803 804 805 806
static void test_bpf_array_map(void)
{
	__u64 val, expected_val = 0, res_first_val, first_val = 0;
	DECLARE_LIBBPF_OPTS(bpf_iter_attach_opts, opts);
	__u32 expected_key = 0, res_first_key;
	struct bpf_iter_bpf_array_map *skel;
807
	union bpf_iter_link_info linfo;
808 809 810 811 812 813
	int err, i, map_fd, iter_fd;
	struct bpf_link *link;
	char buf[64] = {};
	int len, start;

	skel = bpf_iter_bpf_array_map__open_and_load();
814
	if (!ASSERT_OK_PTR(skel, "bpf_iter_bpf_array_map__open_and_load"))
815 816 817 818 819 820 821 822 823 824 825 826
		return;

	map_fd = bpf_map__fd(skel->maps.arraymap1);
	for (i = 0; i < bpf_map__max_entries(skel->maps.arraymap1); i++) {
		val = i + 4;
		expected_key += i;
		expected_val += val;

		if (i == 0)
			first_val = val;

		err = bpf_map_update_elem(map_fd, &i, &val, BPF_ANY);
827
		if (!ASSERT_OK(err, "map_update"))
828 829 830
			goto out;
	}

831 832 833 834
	memset(&linfo, 0, sizeof(linfo));
	linfo.map.map_fd = map_fd;
	opts.link_info = &linfo;
	opts.link_info_len = sizeof(linfo);
835
	link = bpf_program__attach_iter(skel->progs.dump_bpf_array_map, &opts);
836
	if (!ASSERT_OK_PTR(link, "attach_iter"))
837 838 839
		goto out;

	iter_fd = bpf_iter_create(bpf_link__fd(link));
840
	if (!ASSERT_GE(iter_fd, 0, "create_iter"))
841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859
		goto free_link;

	/* do some tests */
	start = 0;
	while ((len = read(iter_fd, buf + start, sizeof(buf) - start)) > 0)
		start += len;
	if (CHECK(len < 0, "read", "read failed: %s\n", strerror(errno)))
		goto close_iter;

	/* test results */
	res_first_key = *(__u32 *)buf;
	res_first_val = *(__u64 *)(buf + sizeof(__u32));
	if (CHECK(res_first_key != 0 || res_first_val != first_val,
		  "bpf_seq_write",
		  "seq_write failure: first key %u vs expected 0, "
		  " first value %llu vs expected %llu\n",
		  res_first_key, res_first_val, first_val))
		goto close_iter;

860
	if (!ASSERT_EQ(skel->bss->key_sum, expected_key, "key_sum"))
861
		goto close_iter;
862
	if (!ASSERT_EQ(skel->bss->val_sum, expected_val, "val_sum"))
863 864 865 866
		goto close_iter;

	for (i = 0; i < bpf_map__max_entries(skel->maps.arraymap1); i++) {
		err = bpf_map_lookup_elem(map_fd, &i, &val);
867
		if (!ASSERT_OK(err, "map_lookup"))
868
			goto out;
869
		if (!ASSERT_EQ(i, val, "invalid_val"))
870 871 872 873 874 875 876 877 878 879 880
			goto out;
	}

close_iter:
	close(iter_fd);
free_link:
	bpf_link__destroy(link);
out:
	bpf_iter_bpf_array_map__destroy(skel);
}

881 882 883 884 885 886 887 888 889 890 891 892 893 894
static void test_bpf_array_map_iter_fd(void)
{
	struct bpf_iter_bpf_array_map *skel;

	skel = bpf_iter_bpf_array_map__open_and_load();
	if (!ASSERT_OK_PTR(skel, "bpf_iter_bpf_array_map__open_and_load"))
		return;

	do_read_map_iter_fd(&skel->skeleton, skel->progs.dump_bpf_array_map,
			    skel->maps.arraymap1);

	bpf_iter_bpf_array_map__destroy(skel);
}

895 896 897 898 899
static void test_bpf_percpu_array_map(void)
{
	DECLARE_LIBBPF_OPTS(bpf_iter_attach_opts, opts);
	struct bpf_iter_bpf_percpu_array_map *skel;
	__u32 expected_key = 0, expected_val = 0;
900
	union bpf_iter_link_info linfo;
901 902 903 904 905 906 907
	int err, i, j, map_fd, iter_fd;
	struct bpf_link *link;
	char buf[64];
	void *val;
	int len;

	skel = bpf_iter_bpf_percpu_array_map__open();
908
	if (!ASSERT_OK_PTR(skel, "bpf_iter_bpf_percpu_array_map__open"))
909 910 911
		return;

	skel->rodata->num_cpus = bpf_num_possible_cpus();
912
	val = malloc(8 * bpf_num_possible_cpus());
913 914

	err = bpf_iter_bpf_percpu_array_map__load(skel);
915
	if (!ASSERT_OK_PTR(skel, "bpf_iter_bpf_percpu_array_map__load"))
916 917 918 919 920 921 922 923 924 925 926 927 928
		goto out;

	/* update map values here */
	map_fd = bpf_map__fd(skel->maps.arraymap1);
	for (i = 0; i < bpf_map__max_entries(skel->maps.arraymap1); i++) {
		expected_key += i;

		for (j = 0; j < bpf_num_possible_cpus(); j++) {
			*(__u32 *)(val + j * 8) = i + j;
			expected_val += i + j;
		}

		err = bpf_map_update_elem(map_fd, &i, val, BPF_ANY);
929
		if (!ASSERT_OK(err, "map_update"))
930 931 932
			goto out;
	}

933 934 935 936
	memset(&linfo, 0, sizeof(linfo));
	linfo.map.map_fd = map_fd;
	opts.link_info = &linfo;
	opts.link_info_len = sizeof(linfo);
937
	link = bpf_program__attach_iter(skel->progs.dump_bpf_percpu_array_map, &opts);
938
	if (!ASSERT_OK_PTR(link, "attach_iter"))
939 940 941
		goto out;

	iter_fd = bpf_iter_create(bpf_link__fd(link));
942
	if (!ASSERT_GE(iter_fd, 0, "create_iter"))
943 944 945 946 947 948 949 950 951
		goto free_link;

	/* do some tests */
	while ((len = read(iter_fd, buf, sizeof(buf))) > 0)
		;
	if (CHECK(len < 0, "read", "read failed: %s\n", strerror(errno)))
		goto close_iter;

	/* test results */
952
	if (!ASSERT_EQ(skel->bss->key_sum, expected_key, "key_sum"))
953
		goto close_iter;
954
	if (!ASSERT_EQ(skel->bss->val_sum, expected_val, "val_sum"))
955 956 957 958 959 960 961 962
		goto close_iter;

close_iter:
	close(iter_fd);
free_link:
	bpf_link__destroy(link);
out:
	bpf_iter_bpf_percpu_array_map__destroy(skel);
963
	free(val);
964 965
}

966 967 968 969 970 971 972 973 974 975 976 977 978
/* An iterator program deletes all local storage in a map. */
static void test_bpf_sk_storage_delete(void)
{
	DECLARE_LIBBPF_OPTS(bpf_iter_attach_opts, opts);
	struct bpf_iter_bpf_sk_storage_helpers *skel;
	union bpf_iter_link_info linfo;
	int err, len, map_fd, iter_fd;
	struct bpf_link *link;
	int sock_fd = -1;
	__u32 val = 42;
	char buf[64];

	skel = bpf_iter_bpf_sk_storage_helpers__open_and_load();
979
	if (!ASSERT_OK_PTR(skel, "bpf_iter_bpf_sk_storage_helpers__open_and_load"))
980 981 982 983 984
		return;

	map_fd = bpf_map__fd(skel->maps.sk_stg_map);

	sock_fd = socket(AF_INET6, SOCK_STREAM, 0);
985
	if (!ASSERT_GE(sock_fd, 0, "socket"))
986 987
		goto out;
	err = bpf_map_update_elem(map_fd, &sock_fd, &val, BPF_NOEXIST);
988
	if (!ASSERT_OK(err, "map_update"))
989 990 991 992 993 994 995 996
		goto out;

	memset(&linfo, 0, sizeof(linfo));
	linfo.map.map_fd = map_fd;
	opts.link_info = &linfo;
	opts.link_info_len = sizeof(linfo);
	link = bpf_program__attach_iter(skel->progs.delete_bpf_sk_storage_map,
					&opts);
997
	if (!ASSERT_OK_PTR(link, "attach_iter"))
998 999 1000
		goto out;

	iter_fd = bpf_iter_create(bpf_link__fd(link));
1001
	if (!ASSERT_GE(iter_fd, 0, "create_iter"))
1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025
		goto free_link;

	/* do some tests */
	while ((len = read(iter_fd, buf, sizeof(buf))) > 0)
		;
	if (CHECK(len < 0, "read", "read failed: %s\n", strerror(errno)))
		goto close_iter;

	/* test results */
	err = bpf_map_lookup_elem(map_fd, &sock_fd, &val);
	if (CHECK(!err || errno != ENOENT, "bpf_map_lookup_elem",
		  "map value wasn't deleted (err=%d, errno=%d)\n", err, errno))
		goto close_iter;

close_iter:
	close(iter_fd);
free_link:
	bpf_link__destroy(link);
out:
	if (sock_fd >= 0)
		close(sock_fd);
	bpf_iter_bpf_sk_storage_helpers__destroy(skel);
}

1026 1027 1028
/* This creates a socket and its local storage. It then runs a task_iter BPF
 * program that replaces the existing socket local storage with the tgid of the
 * only task owning a file descriptor to this socket, this process, prog_tests.
1029 1030
 * It then runs a tcp socket iterator that negates the value in the existing
 * socket local storage, the test verifies that the resulting value is -pid.
1031 1032 1033 1034 1035 1036 1037 1038
 */
static void test_bpf_sk_storage_get(void)
{
	struct bpf_iter_bpf_sk_storage_helpers *skel;
	int err, map_fd, val = -1;
	int sock_fd = -1;

	skel = bpf_iter_bpf_sk_storage_helpers__open_and_load();
1039
	if (!ASSERT_OK_PTR(skel, "bpf_iter_bpf_sk_storage_helpers__open_and_load"))
1040 1041 1042
		return;

	sock_fd = socket(AF_INET6, SOCK_STREAM, 0);
1043
	if (!ASSERT_GE(sock_fd, 0, "socket"))
1044 1045
		goto out;

1046
	err = listen(sock_fd, 1);
1047
	if (!ASSERT_OK(err, "listen"))
1048 1049
		goto close_socket;

1050 1051 1052
	map_fd = bpf_map__fd(skel->maps.sk_stg_map);

	err = bpf_map_update_elem(map_fd, &sock_fd, &val, BPF_NOEXIST);
1053
	if (!ASSERT_OK(err, "bpf_map_update_elem"))
1054 1055 1056 1057 1058
		goto close_socket;

	do_dummy_read(skel->progs.fill_socket_owner);

	err = bpf_map_lookup_elem(map_fd, &sock_fd, &val);
1059 1060 1061 1062 1063 1064 1065 1066 1067
	if (CHECK(err || val != getpid(), "bpf_map_lookup_elem",
	    "map value wasn't set correctly (expected %d, got %d, err=%d)\n",
	    getpid(), val, err))
		goto close_socket;

	do_dummy_read(skel->progs.negate_socket_local_storage);

	err = bpf_map_lookup_elem(map_fd, &sock_fd, &val);
	CHECK(err || val != -getpid(), "bpf_map_lookup_elem",
1068
	      "map value wasn't set correctly (expected %d, got %d, err=%d)\n",
1069
	      -getpid(), val, err);
1070 1071 1072 1073 1074 1075 1076

close_socket:
	close(sock_fd);
out:
	bpf_iter_bpf_sk_storage_helpers__destroy(skel);
}

1077 1078 1079 1080 1081 1082 1083 1084
static void test_bpf_sk_stoarge_map_iter_fd(void)
{
	struct bpf_iter_bpf_sk_storage_map *skel;

	skel = bpf_iter_bpf_sk_storage_map__open_and_load();
	if (!ASSERT_OK_PTR(skel, "bpf_iter_bpf_sk_storage_map__open_and_load"))
		return;

1085
	do_read_map_iter_fd(&skel->skeleton, skel->progs.rw_bpf_sk_storage_map,
1086 1087 1088 1089 1090
			    skel->maps.sk_stg_map);

	bpf_iter_bpf_sk_storage_map__destroy(skel);
}

1091 1092 1093 1094 1095
static void test_bpf_sk_storage_map(void)
{
	DECLARE_LIBBPF_OPTS(bpf_iter_attach_opts, opts);
	int err, i, len, map_fd, iter_fd, num_sockets;
	struct bpf_iter_bpf_sk_storage_map *skel;
1096
	union bpf_iter_link_info linfo;
1097 1098 1099 1100 1101 1102
	int sock_fd[3] = {-1, -1, -1};
	__u32 val, expected_val = 0;
	struct bpf_link *link;
	char buf[64];

	skel = bpf_iter_bpf_sk_storage_map__open_and_load();
1103
	if (!ASSERT_OK_PTR(skel, "bpf_iter_bpf_sk_storage_map__open_and_load"))
1104 1105 1106 1107 1108 1109
		return;

	map_fd = bpf_map__fd(skel->maps.sk_stg_map);
	num_sockets = ARRAY_SIZE(sock_fd);
	for (i = 0; i < num_sockets; i++) {
		sock_fd[i] = socket(AF_INET6, SOCK_STREAM, 0);
1110
		if (!ASSERT_GE(sock_fd[i], 0, "socket"))
1111 1112 1113 1114 1115 1116 1117
			goto out;

		val = i + 1;
		expected_val += val;

		err = bpf_map_update_elem(map_fd, &sock_fd[i], &val,
					  BPF_NOEXIST);
1118
		if (!ASSERT_OK(err, "map_update"))
1119 1120 1121
			goto out;
	}

1122 1123 1124 1125
	memset(&linfo, 0, sizeof(linfo));
	linfo.map.map_fd = map_fd;
	opts.link_info = &linfo;
	opts.link_info_len = sizeof(linfo);
1126 1127 1128 1129 1130 1131 1132 1133 1134
	link = bpf_program__attach_iter(skel->progs.oob_write_bpf_sk_storage_map, &opts);
	err = libbpf_get_error(link);
	if (!ASSERT_EQ(err, -EACCES, "attach_oob_write_iter")) {
		if (!err)
			bpf_link__destroy(link);
		goto out;
	}

	link = bpf_program__attach_iter(skel->progs.rw_bpf_sk_storage_map, &opts);
1135
	if (!ASSERT_OK_PTR(link, "attach_iter"))
1136 1137 1138
		goto out;

	iter_fd = bpf_iter_create(bpf_link__fd(link));
1139
	if (!ASSERT_GE(iter_fd, 0, "create_iter"))
1140 1141
		goto free_link;

1142
	skel->bss->to_add_val = time(NULL);
1143 1144 1145 1146 1147 1148 1149
	/* do some tests */
	while ((len = read(iter_fd, buf, sizeof(buf))) > 0)
		;
	if (CHECK(len < 0, "read", "read failed: %s\n", strerror(errno)))
		goto close_iter;

	/* test results */
1150
	if (!ASSERT_EQ(skel->bss->ipv6_sk_count, num_sockets, "ipv6_sk_count"))
1151 1152
		goto close_iter;

1153
	if (!ASSERT_EQ(skel->bss->val_sum, expected_val, "val_sum"))
1154 1155
		goto close_iter;

1156 1157 1158 1159 1160 1161 1162
	for (i = 0; i < num_sockets; i++) {
		err = bpf_map_lookup_elem(map_fd, &sock_fd[i], &val);
		if (!ASSERT_OK(err, "map_lookup") ||
		    !ASSERT_EQ(val, i + 1 + skel->bss->to_add_val, "check_map_value"))
			break;
	}

1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174
close_iter:
	close(iter_fd);
free_link:
	bpf_link__destroy(link);
out:
	for (i = 0; i < num_sockets; i++) {
		if (sock_fd[i] >= 0)
			close(sock_fd[i]);
	}
	bpf_iter_bpf_sk_storage_map__destroy(skel);
}

1175 1176 1177 1178
static void test_rdonly_buf_out_of_bound(void)
{
	DECLARE_LIBBPF_OPTS(bpf_iter_attach_opts, opts);
	struct bpf_iter_test_kern5 *skel;
1179
	union bpf_iter_link_info linfo;
1180 1181 1182
	struct bpf_link *link;

	skel = bpf_iter_test_kern5__open_and_load();
1183
	if (!ASSERT_OK_PTR(skel, "bpf_iter_test_kern5__open_and_load"))
1184 1185
		return;

1186 1187 1188 1189
	memset(&linfo, 0, sizeof(linfo));
	linfo.map.map_fd = bpf_map__fd(skel->maps.hashmap1);
	opts.link_info = &linfo;
	opts.link_info_len = sizeof(linfo);
1190
	link = bpf_program__attach_iter(skel->progs.dump_bpf_hash_map, &opts);
1191
	if (!ASSERT_ERR_PTR(link, "attach_iter"))
1192 1193 1194 1195 1196
		bpf_link__destroy(link);

	bpf_iter_test_kern5__destroy(skel);
}

1197 1198 1199 1200 1201
static void test_buf_neg_offset(void)
{
	struct bpf_iter_test_kern6 *skel;

	skel = bpf_iter_test_kern6__open_and_load();
1202
	if (!ASSERT_ERR_PTR(skel, "bpf_iter_test_kern6__open_and_load"))
1203 1204 1205
		bpf_iter_test_kern6__destroy(skel);
}

1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218
static void test_link_iter(void)
{
	struct bpf_iter_bpf_link *skel;

	skel = bpf_iter_bpf_link__open_and_load();
	if (!ASSERT_OK_PTR(skel, "bpf_iter_bpf_link__open_and_load"))
		return;

	do_dummy_read(skel->progs.dump_bpf_link);

	bpf_iter_bpf_link__destroy(skel);
}

1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231
static void test_ksym_iter(void)
{
	struct bpf_iter_ksym *skel;

	skel = bpf_iter_ksym__open_and_load();
	if (!ASSERT_OK_PTR(skel, "bpf_iter_ksym__open_and_load"))
		return;

	do_dummy_read(skel->progs.dump_ksym);

	bpf_iter_ksym__destroy(skel);
}

1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259
#define CMP_BUFFER_SIZE 1024
static char task_vma_output[CMP_BUFFER_SIZE];
static char proc_maps_output[CMP_BUFFER_SIZE];

/* remove \0 and \t from str, and only keep the first line */
static void str_strip_first_line(char *str)
{
	char *dst = str, *src = str;

	do {
		if (*src == ' ' || *src == '\t')
			src++;
		else
			*(dst++) = *(src++);

	} while (*src != '\0' && *src != '\n');

	*dst = '\0';
}

static void test_task_vma(void)
{
	int err, iter_fd = -1, proc_maps_fd = -1;
	struct bpf_iter_task_vma *skel;
	int len, read_size = 4;
	char maps_path[64];

	skel = bpf_iter_task_vma__open();
1260
	if (!ASSERT_OK_PTR(skel, "bpf_iter_task_vma__open"))
1261 1262 1263 1264 1265
		return;

	skel->bss->pid = getpid();

	err = bpf_iter_task_vma__load(skel);
1266
	if (!ASSERT_OK(err, "bpf_iter_task_vma__load"))
1267 1268 1269 1270 1271
		goto out;

	skel->links.proc_maps = bpf_program__attach_iter(
		skel->progs.proc_maps, NULL);

1272
	if (!ASSERT_OK_PTR(skel->links.proc_maps, "bpf_program__attach_iter")) {
1273 1274 1275 1276 1277
		skel->links.proc_maps = NULL;
		goto out;
	}

	iter_fd = bpf_iter_create(bpf_link__fd(skel->links.proc_maps));
1278
	if (!ASSERT_GE(iter_fd, 0, "create_iter"))
1279 1280 1281
		goto out;

	/* Read CMP_BUFFER_SIZE (1kB) from bpf_iter. Read in small chunks
1282
	 * to trigger seq_file corner cases.
1283 1284 1285 1286
	 */
	len = 0;
	while (len < CMP_BUFFER_SIZE) {
		err = read_fd_into_buffer(iter_fd, task_vma_output + len,
1287
					  MIN(read_size, CMP_BUFFER_SIZE - len));
1288 1289
		if (!err)
			break;
1290
		if (!ASSERT_GE(err, 0, "read_iter_fd"))
1291 1292 1293 1294 1295 1296 1297
			goto out;
		len += err;
	}

	/* read CMP_BUFFER_SIZE (1kB) from /proc/pid/maps */
	snprintf(maps_path, 64, "/proc/%u/maps", skel->bss->pid);
	proc_maps_fd = open(maps_path, O_RDONLY);
1298
	if (!ASSERT_GE(proc_maps_fd, 0, "open_proc_maps"))
1299 1300
		goto out;
	err = read_fd_into_buffer(proc_maps_fd, proc_maps_output, CMP_BUFFER_SIZE);
1301
	if (!ASSERT_GE(err, 0, "read_prog_maps_fd"))
1302 1303 1304 1305 1306 1307
		goto out;

	/* strip and compare the first line of the two files */
	str_strip_first_line(task_vma_output);
	str_strip_first_line(proc_maps_output);

1308
	ASSERT_STREQ(task_vma_output, proc_maps_output, "compare_output");
1309 1310 1311 1312 1313 1314
out:
	close(proc_maps_fd);
	close(iter_fd);
	bpf_iter_task_vma__destroy(skel);
}

1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327
void test_bpf_sockmap_map_iter_fd(void)
{
	struct bpf_iter_sockmap *skel;

	skel = bpf_iter_sockmap__open_and_load();
	if (!ASSERT_OK_PTR(skel, "bpf_iter_sockmap__open_and_load"))
		return;

	do_read_map_iter_fd(&skel->skeleton, skel->progs.copy, skel->maps.sockmap);

	bpf_iter_sockmap__destroy(skel);
}

1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339
void test_bpf_iter(void)
{
	if (test__start_subtest("btf_id_or_null"))
		test_btf_id_or_null();
	if (test__start_subtest("ipv6_route"))
		test_ipv6_route();
	if (test__start_subtest("netlink"))
		test_netlink();
	if (test__start_subtest("bpf_map"))
		test_bpf_map();
	if (test__start_subtest("task"))
		test_task();
1340 1341
	if (test__start_subtest("task_sleepable"))
		test_task_sleepable();
1342 1343
	if (test__start_subtest("task_stack"))
		test_task_stack();
1344 1345
	if (test__start_subtest("task_file"))
		test_task_file();
1346 1347
	if (test__start_subtest("task_vma"))
		test_task_vma();
1348 1349
	if (test__start_subtest("task_btf"))
		test_task_btf();
1350 1351 1352 1353 1354 1355 1356 1357
	if (test__start_subtest("tcp4"))
		test_tcp4();
	if (test__start_subtest("tcp6"))
		test_tcp6();
	if (test__start_subtest("udp4"))
		test_udp4();
	if (test__start_subtest("udp6"))
		test_udp6();
1358 1359
	if (test__start_subtest("unix"))
		test_unix();
1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371
	if (test__start_subtest("anon"))
		test_anon_iter(false);
	if (test__start_subtest("anon-read-one-char"))
		test_anon_iter(true);
	if (test__start_subtest("file"))
		test_file_iter();
	if (test__start_subtest("overflow"))
		test_overflow(false, false);
	if (test__start_subtest("overflow-e2big"))
		test_overflow(true, false);
	if (test__start_subtest("prog-ret-1"))
		test_overflow(false, true);
1372 1373 1374 1375
	if (test__start_subtest("bpf_hash_map"))
		test_bpf_hash_map();
	if (test__start_subtest("bpf_percpu_hash_map"))
		test_bpf_percpu_hash_map();
1376 1377
	if (test__start_subtest("bpf_array_map"))
		test_bpf_array_map();
1378 1379
	if (test__start_subtest("bpf_array_map_iter_fd"))
		test_bpf_array_map_iter_fd();
1380 1381
	if (test__start_subtest("bpf_percpu_array_map"))
		test_bpf_percpu_array_map();
1382 1383
	if (test__start_subtest("bpf_sk_storage_map"))
		test_bpf_sk_storage_map();
1384 1385
	if (test__start_subtest("bpf_sk_storage_map_iter_fd"))
		test_bpf_sk_stoarge_map_iter_fd();
1386 1387
	if (test__start_subtest("bpf_sk_storage_delete"))
		test_bpf_sk_storage_delete();
1388 1389
	if (test__start_subtest("bpf_sk_storage_get"))
		test_bpf_sk_storage_get();
1390 1391
	if (test__start_subtest("rdonly-buf-out-of-bound"))
		test_rdonly_buf_out_of_bound();
1392 1393
	if (test__start_subtest("buf-neg-offset"))
		test_buf_neg_offset();
1394 1395
	if (test__start_subtest("link-iter"))
		test_link_iter();
1396 1397
	if (test__start_subtest("ksym"))
		test_ksym_iter();
1398 1399
	if (test__start_subtest("bpf_sockmap_map_iter_fd"))
		test_bpf_sockmap_map_iter_fd();
1400
}