tools/testing/selftests/bpf/progs/rhash.c

Source file repositories/reference/linux-study-clean/tools/testing/selftests/bpf/progs/rhash.c

File Facts

System
Linux kernel
Corpus path
tools/testing/selftests/bpf/progs/rhash.c
Extension
.c
Size
4439 bytes
Lines
249
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 elem {
	char arr[128];
	int val;
};

struct {
	__uint(type, BPF_MAP_TYPE_RHASH);
	__uint(map_flags, BPF_F_NO_PREALLOC);
	__uint(max_entries, 128);
	__type(key, int);
	__type(value, struct elem);
} rhmap SEC(".maps");

SEC("syscall")
int test_rhash_lookup_update(void *ctx)
{
	int key = 5;
	struct elem empty = {.val = 3, .arr = {0}};
	struct elem *e;

	err = 1;
	e = bpf_map_lookup_elem(&rhmap, &key);
	if (e)
		return 1;

	err = bpf_map_update_elem(&rhmap, &key, &empty, BPF_NOEXIST);
	if (err)
		return 1;

	e = bpf_map_lookup_elem(&rhmap, &key);
	if (!e || e->val != empty.val) {
		err = 2;
		return 2;
	}

	err = 0;
	return 0;
}

SEC("syscall")
int test_rhash_update_delete(void *ctx)
{
	int key = 6;
	struct elem empty = {.val = 4, .arr = {0}};
	struct elem *e;

	err = 1;
	e = bpf_map_lookup_elem(&rhmap, &key);
	if (e)
		return 1;

	err = bpf_map_update_elem(&rhmap, &key, &empty, BPF_NOEXIST);
	if (err)
		return 2;

	err = bpf_map_delete_elem(&rhmap, &key);
	if (err)
		return 3;

	e = bpf_map_lookup_elem(&rhmap, &key);
	if (e) {
		err = 4;
		return 4;
	}

	err = 0;
	return 0;
}

SEC("syscall")
int test_rhash_update_elements(void *ctx)
{
	int key = 0;
	struct elem empty = {.val = 4, .arr = {0}};
	struct elem *e;
	int i;

	err = 1;

	for (i = 0; i < 128; ++i) {
		key = i;
		e = bpf_map_lookup_elem(&rhmap, &key);
		if (e)
			return 1;

		empty.val = key;
		err = bpf_map_update_elem(&rhmap, &key, &empty, BPF_NOEXIST);
		if (err)
			return 2;

Annotation

Implementation Notes