tools/testing/selftests/bpf/benchs/bench_sockmap.c

Source file repositories/reference/linux-study-clean/tools/testing/selftests/bpf/benchs/bench_sockmap.c

File Facts

System
Linux kernel
Corpus path
tools/testing/selftests/bpf/benchs/bench_sockmap.c
Extension
.c
Size
14495 bytes
Lines
600
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 (err) {
			fprintf(stderr, "setup_rx_sockmap error:%d\n", err);
			goto err;
		}
	} else if (TXMODE_BPF()) {
		err = setup_tx_sockmap();
		if (err) {
			fprintf(stderr, "setup_tx_sockmap error:%d\n", err);
			goto err;
		}
	} else {
		fprintf(stderr, "unknown sockmap bench mode: %d\n", ctx.mode);
		goto err;
	}

	return;

err:
	bench_sockmap_prog_destroy();
	exit(1);
}

static void measure(struct bench_res *res)
{
	res->drops = atomic_swap(&ctx.prod_send, 0);
	res->hits = atomic_swap(&ctx.skel->bss->process_byte, 0);
	res->false_hits = atomic_swap(&ctx.user_read, 0);
	res->important_hits = atomic_swap(&ctx.send_calls, 0);
	res->important_hits |= atomic_swap(&ctx.read_calls, 0) << 32;
}

static void verify_data(int *check_pos, char *buf, int rcv)
{
	for (int i = 0 ; i < rcv; i++) {
		if (buf[i] != snd_data[(*check_pos) % DATA_REPEAT_SIZE]) {
			fprintf(stderr, "verify data fail");
			exit(1);
		}
		(*check_pos)++;
		if (*check_pos >= FILE_SIZE)
			*check_pos = 0;
	}
}

static void *consumer(void *input)
{
	int rcv, sent;
	int check_pos = 0;
	int tid = (long)input;
	int recv_buf_size = FILE_SIZE;
	char *buf = malloc(recv_buf_size);
	int delay_read = ctx.delay_consumer;

	if (!buf) {
		fprintf(stderr, "fail to init read buffer");
		return NULL;
	}

	while (true) {
		if (tid == 1) {
			/* consumer 1 is unused for tx test and stream verdict test */
			if (RXMODE_BPF() || TXMODE())
				return NULL;
			/* it's only for RX_NORMAL which service as reserve-proxy mode */
			rcv = read(ctx.p1, buf, recv_buf_size);
			if (rcv < 0) {
				fprintf(stderr, "fail to read p1");
				return NULL;
			}

			sent = send(ctx.p2, buf, recv_buf_size, 0);
			if (sent < 0) {
				fprintf(stderr, "fail to send p2");
				return NULL;
			}
		} else {
			if (delay_read != 0) {
				if (delay_read < 0)
					return NULL;
				sleep(delay_read);
				delay_read = 0;
			}
			/* read real endpoint by consumer 0 */
			atomic_inc(&ctx.read_calls);
			rcv = read(ctx.c2, buf, recv_buf_size);
			if (rcv < 0 && errno != EAGAIN) {
				fprintf(stderr, "%s fail to read c2 %d\n", __func__, errno);
				return NULL;
			}
			verify_data(&check_pos, buf, rcv);

Annotation

Implementation Notes