tools/testing/selftests/bpf/prog_tests/flow_dissector_reattach.c

Source file repositories/reference/linux-study-clean/tools/testing/selftests/bpf/prog_tests/flow_dissector_reattach.c

File Facts

System
Linux kernel
Corpus path
tools/testing/selftests/bpf/prog_tests/flow_dissector_reattach.c
Extension
.c
Size
18448 bytes
Lines
679
Domain
Support Tooling And Documentation
Bucket
tools
Inferred role
Support Tooling And Documentation: implementation source
Status
source implementation candidate

Why This File Exists

Repository support layer: documentation, build tooling, samples, user-space helper tools, generated initramfs support, licenses, and validation utilities.

Dependency Surface

Detected Declarations

Annotated Snippet

struct test {
		const char *test_name;
		void (*test_func)(int netns, int prog1, int prog2);
	} tests[] = {
		{ "prog attach, prog attach",
		  test_prog_attach_prog_attach },
		{ "link create, link create",
		  test_link_create_link_create },
		{ "prog attach, link create",
		  test_prog_attach_link_create },
		{ "link create, prog attach",
		  test_link_create_prog_attach },
		{ "link create, prog detach",
		  test_link_create_prog_detach },
		{ "prog attach, detach, query",
		  test_prog_attach_detach_query },
		{ "link create, close, query",
		  test_link_create_close_query },
		{ "link update no old prog",
		  test_link_update_no_old_prog },
		{ "link update with replace old prog",
		  test_link_update_replace_old_prog },
		{ "link update with same prog",
		  test_link_update_same_prog },
		{ "link update invalid opts",
		  test_link_update_invalid_opts },
		{ "link update invalid prog",
		  test_link_update_invalid_prog },
		{ "link update netns gone",
		  test_link_update_netns_gone },
		{ "link get info",
		  test_link_get_info },
	};
	int i, progs[2] = { -1, -1 };
	char test_name[80];

	for (i = 0; i < ARRAY_SIZE(progs); i++) {
		progs[i] = load_prog(BPF_PROG_TYPE_FLOW_DISSECTOR);
		if (progs[i] < 0)
			goto out_close;
	}

	for (i = 0; i < ARRAY_SIZE(tests); i++) {
		snprintf(test_name, sizeof(test_name),
			 "flow dissector %s%s",
			 tests[i].test_name,
			 netns == init_net ? " (init_net)" : "");
		if (test__start_subtest(test_name))
			tests[i].test_func(netns, progs[0], progs[1]);
	}
out_close:
	for (i = 0; i < ARRAY_SIZE(progs); i++) {
		if (progs[i] >= 0)
			CHECK_FAIL(close(progs[i]));
	}
}

void serial_test_flow_dissector_reattach(void)
{
	int err, new_net, saved_net;

	saved_net = open("/proc/self/ns/net", O_RDONLY);
	if (CHECK_FAIL(saved_net < 0)) {
		perror("open(/proc/self/ns/net");
		return;
	}

	init_net = open("/proc/1/ns/net", O_RDONLY);
	if (CHECK_FAIL(init_net < 0)) {
		perror("open(/proc/1/ns/net)");
		goto out_close;
	}

	err = setns(init_net, CLONE_NEWNET);
	if (CHECK_FAIL(err)) {
		perror("setns(/proc/1/ns/net)");
		goto out_close;
	}

	if (prog_is_attached(init_net)) {
		test__skip();
		printf("Can't test with flow dissector attached to init_net\n");
		goto out_setns;
	}

	/* First run tests in root network namespace */
	run_tests(init_net);

	/* Then repeat tests in a non-root namespace */
	new_net = unshare_net(init_net);

Annotation

Implementation Notes