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

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

File Facts

System
Linux kernel
Corpus path
tools/testing/selftests/bpf/prog_tests/bpf_tcp_ca.c
Extension
.c
Size
15674 bytes
Lines
645
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 cb_opts {
	const char *cc;
	int map_fd;
};

static int settcpca(int fd, const char *tcp_ca)
{
	int err;

	err = setsockopt(fd, IPPROTO_TCP, TCP_CONGESTION, tcp_ca, strlen(tcp_ca));
	if (!ASSERT_NEQ(err, -1, "setsockopt"))
		return -1;

	return 0;
}

static bool start_test(char *addr_str,
		       const struct network_helper_opts *srv_opts,
		       const struct network_helper_opts *cli_opts,
		       int *srv_fd, int *cli_fd)
{
	*srv_fd = start_server_str(AF_INET6, SOCK_STREAM, addr_str, 0, srv_opts);
	if (!ASSERT_NEQ(*srv_fd, -1, "start_server_str"))
		goto err;

	/* connect to server */
	*cli_fd = connect_to_fd_opts(*srv_fd, cli_opts);
	if (!ASSERT_NEQ(*cli_fd, -1, "connect_to_fd_opts"))
		goto err;

	return true;

err:
	if (*srv_fd != -1) {
		close(*srv_fd);
		*srv_fd = -1;
	}
	if (*cli_fd != -1) {
		close(*cli_fd);
		*cli_fd = -1;
	}
	return false;
}

static void do_test(const struct network_helper_opts *opts)
{
	int lfd = -1, fd = -1;

	if (!start_test(NULL, opts, opts, &lfd, &fd))
		goto done;

	ASSERT_OK(send_recv_data(lfd, fd, total_bytes), "send_recv_data");

done:
	if (lfd != -1)
		close(lfd);
	if (fd != -1)
		close(fd);
}

static int cc_cb(int fd, void *opts)
{
	struct cb_opts *cb_opts = (struct cb_opts *)opts;

	return settcpca(fd, cb_opts->cc);
}

static void test_cubic(void)
{
	struct cb_opts cb_opts = {
		.cc = "bpf_cubic",
	};
	struct network_helper_opts opts = {
		.post_socket_cb	= cc_cb,
		.cb_opts	= &cb_opts,
	};
	struct bpf_cubic *cubic_skel;
	struct bpf_link *link;

	cubic_skel = bpf_cubic__open_and_load();
	if (!ASSERT_OK_PTR(cubic_skel, "bpf_cubic__open_and_load"))
		return;

	link = bpf_map__attach_struct_ops(cubic_skel->maps.cubic);
	if (!ASSERT_OK_PTR(link, "bpf_map__attach_struct_ops")) {
		bpf_cubic__destroy(cubic_skel);
		return;
	}

	do_test(&opts);

Annotation

Implementation Notes