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

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

File Facts

System
Linux kernel
Corpus path
tools/testing/selftests/bpf/prog_tests/bpftool_maps_access.c
Extension
.c
Size
8911 bytes
Lines
372
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_desc {
	char *name;
	enum map_protection protection;
	struct bpf_map *map;
	char *map_name;
	bool pinned;
	char pin_path[PATH_MAX_LEN];
	bool write_must_fail;
};

static struct security_bpf_map *general_setup(void)
{
	struct security_bpf_map *skel;
	uint32_t key, value;
	int ret, i;

	skel = security_bpf_map__open_and_load();
	if (!ASSERT_OK_PTR(skel, "open and load skeleton"))
		goto end;

	struct bpf_map *maps[] = {skel->maps.prot_map, skel->maps.not_prot_map};

	ret = security_bpf_map__attach(skel);
	if (!ASSERT_OK(ret, "attach maps security programs"))
		goto end_destroy;

	for (i = 0; i < sizeof(maps)/sizeof(struct bpf_map *); i++) {
		for (key = 0; key < 2; key++) {
			int ret = bpf_map__update_elem(maps[i], &key,
					sizeof(key), &key, sizeof(key),
					0);
			if (!ASSERT_OK(ret, "set initial map value"))
				goto end_destroy;
		}
	}

	key = 0;
	value = 1;
	ret = bpf_map__update_elem(skel->maps.prot_status_map, &key,
			sizeof(key), &value, sizeof(value), 0);
	if (!ASSERT_OK(ret, "configure map protection"))
		goto end_destroy;

	if (!ASSERT_OK(mkdir(BPFFS_PIN_DIR, S_IFDIR), "create bpffs pin dir"))
		goto end_destroy;

	return skel;
end_destroy:
	security_bpf_map__destroy(skel);
end:
	return NULL;
}

static void general_cleanup(struct security_bpf_map *skel)
{
	rmdir(BPFFS_PIN_DIR);
	security_bpf_map__destroy(skel);
}

static void update_test_desc(struct security_bpf_map *skel,
			      struct test_desc *test)
{
	/* Now that the skeleton is loaded, update all missing fields to
	 * have the subtest properly configured
	 */
	if (test->protection == PROTECTED) {
		test->map = skel->maps.prot_map;
		test->map_name = PROTECTED_MAP_NAME;
	} else {
		test->map = skel->maps.not_prot_map;
		test->map_name = UNPROTECTED_MAP_NAME;
	}
}

static int test_setup(struct security_bpf_map *skel, struct test_desc *desc)
{
	int ret;

	update_test_desc(skel, desc);

	if (desc->pinned) {
		ret = snprintf(desc->pin_path, PATH_MAX_LEN, "%s/%s", BPFFS_PIN_DIR,
				desc->name);
		if (!ASSERT_GT(ret, 0, "format pin path"))
			return 1;
		ret = bpf_map__pin(desc->map, desc->pin_path);
		if (!ASSERT_OK(ret, "pin map"))
			return 1;
	}

Annotation

Implementation Notes