Commit e078255a authored by Geliang Tang's avatar Geliang Tang Committed by Martin KaFai Lau

selftests/bpf: Use post_socket_cb in connect_to_fd_opts

Since the post_socket_cb() callback is added in struct network_helper_opts,
it's make sense to use it not only in __start_server(), but also in
connect_to_fd_opts(). Then it can be used to set TCP_CONGESTION sockopt.

Add a "void *" type member cb_opts into struct network_helper_opts, and add
a new struct named cb_opts in prog_tests/bpf_tcp_ca.c, then cc can be moved
into struct cb_opts from network_helper_opts. Define a new callback cc_cb()
to set TCP_CONGESTION sockopt, and set it to post_socket_cb pointer of opts.
Define a new cb_opts cubic, set it to cb_opts of opts. Pass this opts to
connect_to_fd_opts() in test_dctcp_fallback().
Signed-off-by: default avatarGeliang Tang <tanggeliang@kylinos.cn>
Link: https://lore.kernel.org/r/b512bb8d8f6854c9ea5c409b69d1bf37c6f272c6.1716638248.git.tanggeliang@kylinos.cnSigned-off-by: default avatarMartin KaFai Lau <martin.lau@kernel.org>
parent 6f802cb8
...@@ -348,9 +348,8 @@ int connect_to_fd_opts(int server_fd, const struct network_helper_opts *opts) ...@@ -348,9 +348,8 @@ int connect_to_fd_opts(int server_fd, const struct network_helper_opts *opts)
if (settimeo(fd, opts->timeout_ms)) if (settimeo(fd, opts->timeout_ms))
goto error_close; goto error_close;
if (opts->cc && opts->cc[0] && if (opts->post_socket_cb &&
setsockopt(fd, SOL_TCP, TCP_CONGESTION, opts->cc, opts->post_socket_cb(fd, opts->cb_opts))
strlen(opts->cc) + 1))
goto error_close; goto error_close;
if (!opts->noconnect) if (!opts->noconnect)
......
...@@ -22,13 +22,13 @@ typedef __u16 __sum16; ...@@ -22,13 +22,13 @@ typedef __u16 __sum16;
#define MAGIC_BYTES 123 #define MAGIC_BYTES 123
struct network_helper_opts { struct network_helper_opts {
const char *cc;
int timeout_ms; int timeout_ms;
bool must_fail; bool must_fail;
bool noconnect; bool noconnect;
int type; int type;
int proto; int proto;
int (*post_socket_cb)(int fd, void *opts); int (*post_socket_cb)(int fd, void *opts);
void *cb_opts;
}; };
/* ipv4 test vector */ /* ipv4 test vector */
......
...@@ -23,6 +23,10 @@ ...@@ -23,6 +23,10 @@
static const unsigned int total_bytes = 10 * 1024 * 1024; static const unsigned int total_bytes = 10 * 1024 * 1024;
static int expected_stg = 0xeB9F; static int expected_stg = 0xeB9F;
struct cb_opts {
const char *cc;
};
static int settcpca(int fd, const char *tcp_ca) static int settcpca(int fd, const char *tcp_ca)
{ {
int err; int err;
...@@ -81,6 +85,13 @@ static void do_test(const char *tcp_ca, const struct bpf_map *sk_stg_map) ...@@ -81,6 +85,13 @@ static void do_test(const char *tcp_ca, const struct bpf_map *sk_stg_map)
close(fd); 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) static void test_cubic(void)
{ {
struct bpf_cubic *cubic_skel; struct bpf_cubic *cubic_skel;
...@@ -172,10 +183,13 @@ static void test_dctcp_fallback(void) ...@@ -172,10 +183,13 @@ static void test_dctcp_fallback(void)
{ {
int err, lfd = -1, cli_fd = -1, srv_fd = -1; int err, lfd = -1, cli_fd = -1, srv_fd = -1;
struct network_helper_opts opts = { struct network_helper_opts opts = {
.cc = "cubic", .post_socket_cb = cc_cb,
}; };
struct bpf_dctcp *dctcp_skel; struct bpf_dctcp *dctcp_skel;
struct bpf_link *link = NULL; struct bpf_link *link = NULL;
struct cb_opts cubic = {
.cc = "cubic",
};
char srv_cc[16]; char srv_cc[16];
socklen_t cc_len = sizeof(srv_cc); socklen_t cc_len = sizeof(srv_cc);
...@@ -195,6 +209,7 @@ static void test_dctcp_fallback(void) ...@@ -195,6 +209,7 @@ static void test_dctcp_fallback(void)
!ASSERT_OK(settcpca(lfd, "bpf_dctcp"), "lfd=>bpf_dctcp")) !ASSERT_OK(settcpca(lfd, "bpf_dctcp"), "lfd=>bpf_dctcp"))
goto done; goto done;
opts.cb_opts = &cubic;
cli_fd = connect_to_fd_opts(lfd, &opts); cli_fd = connect_to_fd_opts(lfd, &opts);
if (!ASSERT_GE(cli_fd, 0, "cli_fd")) if (!ASSERT_GE(cli_fd, 0, "cli_fd"))
goto done; goto done;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment