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

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

File Facts

System
Linux kernel
Corpus path
tools/testing/selftests/bpf/prog_tests/fd_htab_lookup.c
Extension
.c
Size
3916 bytes
Lines
193
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 htab_op_ctx {
	int fd;
	int loop;
	unsigned int entries;
	bool stop;
};

#define ERR_TO_RETVAL(where, err) ((void *)(long)(((where) << 12) | (-err)))

static void *htab_lookup_fn(void *arg)
{
	struct htab_op_ctx *ctx = arg;
	int i = 0;

	while (i++ < ctx->loop && !ctx->stop) {
		unsigned int j;

		for (j = 0; j < ctx->entries; j++) {
			unsigned int key = j, zero = 0, value;
			int inner_fd, err;

			err = bpf_map_lookup_elem(ctx->fd, &key, &value);
			if (err) {
				ctx->stop = true;
				return ERR_TO_RETVAL(1, err);
			}

			inner_fd = bpf_map_get_fd_by_id(value);
			if (inner_fd < 0) {
				/* The old map has been freed */
				if (inner_fd == -ENOENT)
					continue;
				ctx->stop = true;
				return ERR_TO_RETVAL(2, inner_fd);
			}

			err = bpf_map_lookup_elem(inner_fd, &zero, &value);
			if (err) {
				close(inner_fd);
				ctx->stop = true;
				return ERR_TO_RETVAL(3, err);
			}
			close(inner_fd);

			if (value != key) {
				ctx->stop = true;
				return ERR_TO_RETVAL(4, -EINVAL);
			}
		}
	}

	return NULL;
}

static void *htab_update_fn(void *arg)
{
	struct htab_op_ctx *ctx = arg;
	int i = 0;

	while (i++ < ctx->loop && !ctx->stop) {
		unsigned int j;

		for (j = 0; j < ctx->entries; j++) {
			unsigned int key = j, zero = 0;
			int inner_fd, err;

			inner_fd = bpf_map_create(BPF_MAP_TYPE_ARRAY, NULL, 4, 4, 1, NULL);
			if (inner_fd < 0) {
				ctx->stop = true;
				return ERR_TO_RETVAL(1, inner_fd);
			}

			err = bpf_map_update_elem(inner_fd, &zero, &key, 0);
			if (err) {
				close(inner_fd);
				ctx->stop = true;
				return ERR_TO_RETVAL(2, err);
			}

			err = bpf_map_update_elem(ctx->fd, &key, &inner_fd, BPF_EXIST);
			if (err) {
				close(inner_fd);
				ctx->stop = true;
				return ERR_TO_RETVAL(3, err);
			}
			close(inner_fd);
		}
	}

	return NULL;

Annotation

Implementation Notes