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

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

File Facts

System
Linux kernel
Corpus path
tools/testing/selftests/bpf/prog_tests/queue_stack_map.c
Extension
.c
Size
2870 bytes
Lines
109
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 (type == QUEUE) {
			val = vals[i];
			pkt_v4.iph.saddr = vals[i] * 5;
		} else if (type == STACK) {
			val = vals[MAP_SIZE - 1 - i];
			pkt_v4.iph.saddr = vals[MAP_SIZE - 1 - i] * 5;
		}

		topts.data_size_out = sizeof(buf);
		err = bpf_prog_test_run_opts(prog_fd, &topts);
		if (err || topts.retval ||
		    topts.data_size_out != sizeof(pkt_v4))
			break;
		memcpy(&iph, buf + sizeof(struct ethhdr), sizeof(iph));
		if (iph.daddr != val)
			break;
	}

	ASSERT_OK(err, "bpf_map_pop_elem");
	ASSERT_OK(topts.retval, "bpf_map_pop_elem test retval");
	ASSERT_EQ(topts.data_size_out, sizeof(pkt_v4),
		  "bpf_map_pop_elem data_size_out");
	ASSERT_EQ(iph.daddr, val, "bpf_map_pop_elem iph.daddr");

	/* Queue is empty, program should return TC_ACT_SHOT */
	topts.data_size_out = sizeof(buf);
	err = bpf_prog_test_run_opts(prog_fd, &topts);
	ASSERT_OK(err, "check-queue-stack-map-empty");
	ASSERT_EQ(topts.retval, 2  /* TC_ACT_SHOT */,
		  "check-queue-stack-map-empty test retval");
	ASSERT_EQ(topts.data_size_out, sizeof(pkt_v4),
		  "check-queue-stack-map-empty data_size_out");

	/* Check that the program pushed elements correctly */
	for (i = 0; i < MAP_SIZE; i++) {
		err = bpf_map_lookup_and_delete_elem(map_out_fd, NULL, &val);
		ASSERT_OK(err, "bpf_map_lookup_and_delete_elem");
		ASSERT_EQ(val, vals[i] * 5, "bpf_map_push_elem val");
	}
out:
	pkt_v4.iph.saddr = 0;
	bpf_object__close(obj);
}

void test_queue_stack_map(void)
{
	test_queue_stack_map_by_type(QUEUE);
	test_queue_stack_map_by_type(STACK);
}

Annotation

Implementation Notes