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

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

File Facts

System
Linux kernel
Corpus path
tools/testing/selftests/bpf/progs/user_ringbuf_success.c
Extension
.c
Size
4178 bytes
Lines
213
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

if (status) {
			bpf_printk("bpf_dynptr_read() failed: %d\n", status);
			err = 1;
			return 1;
		}
	} else {
		sample = bpf_dynptr_data(dynptr, 0, sizeof(*sample));
		if (!sample) {
			bpf_printk("Unexpectedly failed to get sample\n");
			err = 2;
			return 1;
		}
		stack_sample = *sample;
	}

	__sync_fetch_and_add(&read, 1);
	return 0;
}

static void
handle_sample_msg(const struct test_msg *msg)
{
	switch (msg->msg_op) {
	case TEST_MSG_OP_INC64:
		kern_mutated += msg->operand_64;
		break;
	case TEST_MSG_OP_INC32:
		kern_mutated += msg->operand_32;
		break;
	case TEST_MSG_OP_MUL64:
		kern_mutated *= msg->operand_64;
		break;
	case TEST_MSG_OP_MUL32:
		kern_mutated *= msg->operand_32;
		break;
	default:
		bpf_printk("Unrecognized op %d\n", msg->msg_op);
		err = 2;
	}
}

static long
read_protocol_msg(struct bpf_dynptr *dynptr, void *context)
{
	const struct test_msg *msg = NULL;

	msg = bpf_dynptr_data(dynptr, 0, sizeof(*msg));
	if (!msg) {
		err = 1;
		bpf_printk("Unexpectedly failed to get msg\n");
		return 0;
	}

	handle_sample_msg(msg);

	return 0;
}

static int publish_next_kern_msg(__u32 index, void *context)
{
	struct test_msg *msg = NULL;
	int operand_64 = TEST_OP_64;
	int operand_32 = TEST_OP_32;

	msg = bpf_ringbuf_reserve(&kernel_ringbuf, sizeof(*msg), 0);
	if (!msg) {
		err = 4;
		return 1;
	}

	switch (index % TEST_MSG_OP_NUM_OPS) {
	case TEST_MSG_OP_INC64:
		msg->operand_64 = operand_64;
		msg->msg_op = TEST_MSG_OP_INC64;
		expected_user_mutated += operand_64;
		break;
	case TEST_MSG_OP_INC32:
		msg->operand_32 = operand_32;
		msg->msg_op = TEST_MSG_OP_INC32;
		expected_user_mutated += operand_32;
		break;
	case TEST_MSG_OP_MUL64:
		msg->operand_64 = operand_64;
		msg->msg_op = TEST_MSG_OP_MUL64;
		expected_user_mutated *= operand_64;
		break;
	case TEST_MSG_OP_MUL32:
		msg->operand_32 = operand_32;
		msg->msg_op = TEST_MSG_OP_MUL32;
		expected_user_mutated *= operand_32;

Annotation

Implementation Notes