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

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

File Facts

System
Linux kernel
Corpus path
tools/testing/selftests/bpf/prog_tests/bloom_filter_map.c
Extension
.c
Size
6283 bytes
Lines
225
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

// SPDX-License-Identifier: GPL-2.0
/* Copyright (c) 2021 Facebook */

#include <sys/syscall.h>
#include <limits.h>
#include <test_progs.h>
#include "bloom_filter_map.skel.h"

#ifndef NUMA_NO_NODE
#define NUMA_NO_NODE	(-1)
#endif

static void test_fail_cases(void)
{
	LIBBPF_OPTS(bpf_map_create_opts, opts);
	__u32 value = 0;
	int fd, err;

	/* Invalid key size */
	fd = bpf_map_create(BPF_MAP_TYPE_BLOOM_FILTER, NULL, 4, sizeof(value), 100, NULL);
	if (!ASSERT_LT(fd, 0, "bpf_map_create bloom filter invalid key size"))
		close(fd);

	/* Invalid value size */
	fd = bpf_map_create(BPF_MAP_TYPE_BLOOM_FILTER, NULL, 0, 0, 100, NULL);
	if (!ASSERT_LT(fd, 0, "bpf_map_create bloom filter invalid value size 0"))
		close(fd);

	/* Invalid value size: too big */
	fd = bpf_map_create(BPF_MAP_TYPE_BLOOM_FILTER, NULL, 0, INT32_MAX, 100, NULL);
	if (!ASSERT_LT(fd, 0, "bpf_map_create bloom filter invalid value too large"))
		close(fd);

	/* Invalid max entries size */
	fd = bpf_map_create(BPF_MAP_TYPE_BLOOM_FILTER, NULL, 0, sizeof(value), 0, NULL);
	if (!ASSERT_LT(fd, 0, "bpf_map_create bloom filter invalid max entries size"))
		close(fd);

	/* Bloom filter maps do not support BPF_F_NO_PREALLOC */
	opts.map_flags = BPF_F_NO_PREALLOC;
	fd = bpf_map_create(BPF_MAP_TYPE_BLOOM_FILTER, NULL, 0, sizeof(value), 100, &opts);
	if (!ASSERT_LT(fd, 0, "bpf_map_create bloom filter invalid flags"))
		close(fd);

	fd = bpf_map_create(BPF_MAP_TYPE_BLOOM_FILTER, NULL, 0, sizeof(value), 100, NULL);
	if (!ASSERT_GE(fd, 0, "bpf_map_create bloom filter"))
		return;

	/* Test invalid flags */
	err = bpf_map_update_elem(fd, NULL, &value, -1);
	ASSERT_EQ(err, -EINVAL, "bpf_map_update_elem bloom filter invalid flags");

	err = bpf_map_update_elem(fd, NULL, &value, BPF_EXIST);
	ASSERT_EQ(err, -EINVAL, "bpf_map_update_elem bloom filter invalid flags");

	err = bpf_map_update_elem(fd, NULL, &value, BPF_F_LOCK);
	ASSERT_EQ(err, -EINVAL, "bpf_map_update_elem bloom filter invalid flags");

	err = bpf_map_update_elem(fd, NULL, &value, BPF_NOEXIST);
	ASSERT_EQ(err, -EINVAL, "bpf_map_update_elem bloom filter invalid flags");

	err = bpf_map_update_elem(fd, NULL, &value, 10000);
	ASSERT_EQ(err, -EINVAL, "bpf_map_update_elem bloom filter invalid flags");

	close(fd);
}

static void test_success_cases(void)
{
	LIBBPF_OPTS(bpf_map_create_opts, opts);
	char value[11];
	int fd, err;

	/* Create a map */
	opts.map_flags = BPF_F_ZERO_SEED | BPF_F_NUMA_NODE;
	opts.numa_node = NUMA_NO_NODE;
	fd = bpf_map_create(BPF_MAP_TYPE_BLOOM_FILTER, NULL, 0, sizeof(value), 100, &opts);
	if (!ASSERT_GE(fd, 0, "bpf_map_create bloom filter success case"))
		return;

	/* Add a value to the bloom filter */
	err = bpf_map_update_elem(fd, NULL, &value, 0);
	if (!ASSERT_OK(err, "bpf_map_update_elem bloom filter success case"))
		goto done;

	 /* Lookup a value in the bloom filter */
	err = bpf_map_lookup_elem(fd, NULL, &value);
	ASSERT_OK(err, "bpf_map_update_elem bloom filter success case");

done:

Annotation

Implementation Notes