use network helpers, part 7
Subsystems (4)
Geliang Tang
reviewercover use network helpers, part 7
From: Geliang Tang <[email protected]>
Drop type, noconnect and must_fail from network_helper_opts. And use start_server_str in mptcp and test_tcp_check_syncookie_user.
Patches 1-3 address Martin's comments in the previous series.
Geliang Tang (5):
selftests/bpf: Drop type from network_helper_opts selftests/bpf: Drop noconnect from network_helper_opts selftests/bpf: Drop must_fail from network_helper_opts selftests/bpf: Use start_server_str in mptcp selftests/bpf: Use start_server_str in test_tcp_check_syncookie_user
tools/testing/selftests/bpf/network_helpers.c | 20 ++++++++----- tools/testing/selftests/bpf/network_helpers.h | 5 ++-- .../selftests/bpf/prog_tests/cgroup_v1v2.c | 5 +--- .../bpf/prog_tests/ip_check_defrag.c | 7 ++--- .../testing/selftests/bpf/prog_tests/mptcp.c | 7 +---- .../bpf/test_tcp_check_syncookie_user.c | 29 ++----------------- 6 files changed, 21 insertions(+), 52 deletions(-)
Signature · 1 line
2.43.0
Geliang Tang
reviewerPATCH 1/5 selftests/bpf: Drop type from network_helper_opts
From: Geliang Tang <[email protected]>
The opts.{type, noconnect, must_fail} is at least a bit non intuitive or unnecessary. The only use case now is in test_bpf_ip_check_defrag_ok which ends up bypassing most (or at least some) of the connect_to_fd_opts() logic. It's much better that test should have its own connect_to_fd_opts() instead.
This patch adds a new helper named __connect_to_fd_opts() to do this. It accepts a new "type" parameter, then opts->type can be replaced by "type" parameter in this helper. In test_bpf_ip_check_defrag_ok, different types are passed to it. And the strcut member "type" of network_helper_opts can be dropped now.
Then connect_to_fd_opts can implement as a wrapper of this new helper.
--- tools/testing/selftests/bpf/network_helpers.c | 14 +++++++++----- tools/testing/selftests/bpf/network_helpers.h | 3 ++- .../selftests/bpf/prog_tests/ip_check_defrag.c | 5 ++--- 3 files changed, 13 insertions(+), 9 deletions(-)
tools/testing/selftests/bpf/network_helpers.c +9 -5
@@ -303,21 +303,20 @@ int connect_to_addr(int type, const struct sockaddr_storage *addr, socklen_t addreturn -1 ; } -int connect_to_fd_opts (int server_fd,const struct network_helper_opts *opts) +int __connect_to_fd_opts (int server_fd,int type, +const struct network_helper_opts *opts) {struct sockaddr_storage addr;struct sockaddr_in *addr_in;socklen_t addrlen, optlen; -int fd, type, protocol; +int fd, protocol;if (!opts) opts = &default_opts; optlen =sizeof (type); -if (opts->type) { - type = opts->type; - }else { +if (!type) {if (getsockopt (server_fd,SOL_SOCKET ,SO_TYPE , &type, &optlen)) {log_err ("getsockopt(SOL_TYPE)" );return -1 ;
@@ -364,6 +363,11 @@ int connect_to_fd_opts(int server_fd, const struct network_helper_opts *opts)return -1 ; } +int connect_to_fd_opts (int server_fd,const struct network_helper_opts *opts) +{ +return __connect_to_fd_opts (server_fd,0 , opts); +} +int connect_to_fd (int server_fd,int timeout_ms) {struct network_helper_opts opts = {
tools/testing/selftests/bpf/network_helpers.h +2 -1
@@ -25,7 +25,6 @@ struct network_helper_opts {int timeout_ms;bool must_fail;bool noconnect; -int type;int proto;int (*post_socket_cb)(int fd,void *opts);void *cb_opts;
@@ -61,6 +60,8 @@ void free_fds(int *fds, unsigned int nr_close_fds);int connect_to_addr (int type,const struct sockaddr_storage *addr,socklen_t len,const struct network_helper_opts *opts);int connect_to_fd (int server_fd,int timeout_ms); +int __connect_to_fd_opts (int server_fd,int type, +const struct network_helper_opts *opts);int connect_to_fd_opts (int server_fd,const struct network_helper_opts *opts);int connect_fd_to_fd (int client_fd,int server_fd,int timeout_ms); int fastopen_connect(int server_fd, const char *data, unsigned int data_len,
tools/testing/selftests/bpf/prog_tests/ip_check_defrag.c +2 -3
@@ -164,7 +164,6 @@ void test_bpf_ip_check_defrag_ok(bool ipv6) };struct network_helper_opts tx_ops = { .timeout_ms =1000 , - .type =SOCK_RAW , .proto =IPPROTO_RAW , .noconnect = true, };
@@ -201,7 +200,7 @@ void test_bpf_ip_check_defrag_ok(bool ipv6) nstoken =open_netns (NS0 );if (!ASSERT_OK_PTR (nstoken,"setns ns0" )) goto out; - client_tx_fd =connect_to_fd_opts (srv_fd, &tx_ops); + client_tx_fd =__connect_to_fd_opts (srv_fd,SOCK_RAW , &tx_ops);close_netns (nstoken);if (!ASSERT_GE (client_tx_fd,0 ,"connect_to_fd_opts" )) goto out;
@@ -210,7 +209,7 @@ void test_bpf_ip_check_defrag_ok(bool ipv6) nstoken =open_netns (NS0 );if (!ASSERT_OK_PTR (nstoken,"setns ns0" )) goto out; - client_rx_fd =connect_to_fd_opts (srv_fd, &rx_opts); + client_rx_fd =__connect_to_fd_opts (srv_fd,0 , &rx_opts);close_netns (nstoken);if (!ASSERT_GE (client_rx_fd,0 ,"connect_to_fd_opts" )) goto out;
Signature · 1 line
2.43.0
Eduard Zingerman
maintainerRe: [PATCH bpf-next 1/5] selftests/bpf: Drop type from network_helper_opts
Quoted 14 lines · Geliang Tang
From: Geliang Tang <[email protected]>
The opts.{type, noconnect,
must_fail} is at least a bit non intuitive or unnecessary. The only use case now is intest_bpf_ip_check_defrag_okwhich ends up bypassing most (or at least some) of theconnect_to_fd_opts()logic. It's much better that test should have its ownconnect_to_fd_opts()instead.This patch adds a new helper named
__connect_to_fd_opts()to do this. It accepts a new "type" parameter, thenopts->typecan be replaced by "type" parameter in this helper. Intest_bpf_ip_check_defrag_ok, different types are passed to it. And the strcut member "type" ofnetwork_helper_optscan be dropped now.
Quoted 4 lines
Then
connect_to_fd_optscan implement as a wrapper of this new helper.Signed-off-by Geliang Tang <[email protected]>---
Patches #1,2,3 trade options specified as struct fields for options specified as function parameters. Tbh, this seems to be an opinionated stylistic change, what is the need for it?
If anything, I think that this is less readable:
tools/testing/selftests/bpf/prog_tests/ip_check_defrag.c void test_bpf_ip_check_defrag_ok(bool ipv6) · line 212
Quoted 1 line
tools/testing/selftests/bpf/prog_tests/ip_check_defrag.c +1
-0+ client_rx_fd =__connect_to_fd_opts (srv_fd,0 , &rx_opts);
compared to this:
Quoted 6 lines
tools/testing/selftests/bpf/prog_tests/ip_check_defrag.c +0
-1struct network_helper_opts tx_ops = { .timeout_ms =1000 , - .type =SOCK_RAW , .proto =IPPROTO_RAW , .noconnect = true, };
...
tools/testing/selftests/bpf/prog_tests/ip_check_defrag.c void test_bpf_ip_check_defrag_ok(bool ipv6) · line 213
Quoted 1 line
tools/testing/selftests/bpf/prog_tests/ip_check_defrag.c +0
-1- client_rx_fd =connect_to_fd_opts (srv_fd, &rx_opts);
(given that by patch #3 three parameters are added to
__connect_to_fd_opts() *and* it also accepts options).
[...]
Geliang Tang
reviewerRe: [PATCH bpf-next 1/5] selftests/bpf: Drop type from network_helper_opts
Quoted 26 lines · Eduard Zingerman
Quoted 25 lines · Geliang Tang
From: Geliang Tang <[email protected]>
The opts.{type, noconnect,
must_fail} is at least a bit non intuitive or
unnecessary. The only use case now is intest_bpf_ip_check_defrag_okwhich
ends up bypassing most (or at least some) of theconnect_to_fd_opts()
logic. It's much better that test should have its ownconnect_to_fd_opts()
instead.This patch adds a new helper named
__connect_to_fd_opts()to do this. It
accepts a new "type" parameter, thenopts->typecan be replaced by "type"
parameter in this helper. Intest_bpf_ip_check_defrag_ok, different types
are passed to it. And the strcut member "type" ofnetwork_helper_optscan
be dropped now.Then
connect_to_fd_optscan implement as a wrapper of this new helper.
Quoted 7 lines
Quoted 2 lines
Signed-off-by Geliang Tang <[email protected]>---
Patches #1,2,3 trade options specified as struct fields for options specified as function parameters. Tbh, this seems to be an opinionated
stylistic change, what is the need for it?
Thanks for your review.
Patches 1-3 address Martin's comment for "Drop type parameter of start_server_addr" [1].
Since opts.{type, noconnect} are only used in ip_check_defrag.c and opts.must_fail is only used in cgroup_v1v2.c, they are not generic enough to be added into network_helper_opts. So this set removes them from network_helper_opts and use them as function parameters.
Thanks,
-Geliang
Quoted 20 lines
If anything, I think that this is less readable:
tools/testing/selftests/bpf/prog_tests/ip_check_defrag.c void test_bpf_ip_check_defrag_ok(bool ipv6) · line 212
Quoted 1 line
tools/testing/selftests/bpf/prog_tests/ip_check_defrag.c +1
-0+ client_rx_fd =__connect_to_fd_opts (srv_fd,0 , &rx_opts);compared to this:
Quoted 6 lines
struct
network_helper_optstx_ops= {
.timeout_ms = 1000,tools/testing/selftests/bpf/prog_tests/ip_check_defrag.c +0
-1- .type = SOCK_RAW,.proto =
IPPROTO_RAW,
.noconnect = true,
};...
tools/testing/selftests/bpf/prog_tests/ip_check_defrag.c void test_bpf_ip_check_defrag_ok(bool ipv6) · line 213
Quoted 1 line
tools/testing/selftests/bpf/prog_tests/ip_check_defrag.c +0
-1- client_rx_fd =connect_to_fd_opts (srv_fd, &rx_opts);(given that by patch #3 three parameters are added to
__connect_to_fd_opts()*and* it also accepts options).[...]
Geliang Tang
reviewerPATCH 2/5 selftests/bpf: Drop noconnect from network_helper_opts
In reply to Geliang TangFrom: Geliang Tang <[email protected]>
This patch adds another new parameter "noconnect" for __connect_to_fd_opts, then opts->noconnect can be replaced by "noconnect" parameter in it. In test_bpf_ip_check_defrag_ok(), true is passed to it. And the strcut member "noconnect" of network_helper_opts can be dropped now.
--- tools/testing/selftests/bpf/network_helpers.c | 6 +++--- tools/testing/selftests/bpf/network_helpers.h | 3 +-- tools/testing/selftests/bpf/prog_tests/ip_check_defrag.c | 6 ++---- 3 files changed, 6 insertions(+), 9 deletions(-)
tools/testing/selftests/bpf/network_helpers.c +3 -3
@@ -303,7 +303,7 @@ int connect_to_addr(int type, const struct sockaddr_storage *addr, socklen_t addreturn -1 ; } -int __connect_to_fd_opts (int server_fd,int type, +int __connect_to_fd_opts (int server_fd,int type,bool noconnect,const struct network_helper_opts *opts) {struct sockaddr_storage addr;
@@ -352,7 +352,7 @@ int __connect_to_fd_opts(int server_fd, int type, opts->post_socket_cb(fd, opts->cb_opts)) goto error_close; -if (!opts->noconnect) +if (!noconnect)if (connect_fd_to_addr (fd, &addr, addrlen, opts->must_fail)) goto error_close;
@@ -365,7 +365,7 @@ int __connect_to_fd_opts(int server_fd, int type,int connect_to_fd_opts (int server_fd,const struct network_helper_opts *opts) { -return __connect_to_fd_opts (server_fd,0 , opts); +return __connect_to_fd_opts (server_fd,0 , false, opts); }int connect_to_fd (int server_fd,int timeout_ms)
tools/testing/selftests/bpf/network_helpers.h +1 -2
@@ -24,7 +24,6 @@ typedef __u16 __sum16;struct network_helper_opts {int timeout_ms;bool must_fail; -bool noconnect;int proto;int (*post_socket_cb)(int fd,void *opts);void *cb_opts;
@@ -60,7 +59,7 @@ void free_fds(int *fds, unsigned int nr_close_fds);int connect_to_addr (int type,const struct sockaddr_storage *addr,socklen_t len,const struct network_helper_opts *opts);int connect_to_fd (int server_fd,int timeout_ms); -int __connect_to_fd_opts (int server_fd,int type, +int __connect_to_fd_opts (int server_fd,int type,bool noconnect,const struct network_helper_opts *opts);int connect_to_fd_opts (int server_fd,const struct network_helper_opts *opts);int connect_fd_to_fd (int client_fd,int server_fd,int timeout_ms);
tools/testing/selftests/bpf/prog_tests/ip_check_defrag.c +2 -4
@@ -160,12 +160,10 @@ void test_bpf_ip_check_defrag_ok(bool ipv6) {struct network_helper_opts rx_opts = { .timeout_ms =1000 , - .noconnect = true, };struct network_helper_opts tx_ops = { .timeout_ms =1000 , .proto =IPPROTO_RAW , - .noconnect = true, };struct sockaddr_storage caddr;struct ip_check_defrag *skel;
@@ -200,7 +198,7 @@ void test_bpf_ip_check_defrag_ok(bool ipv6) nstoken =open_netns (NS0 );if (!ASSERT_OK_PTR (nstoken,"setns ns0" )) goto out; - client_tx_fd =__connect_to_fd_opts (srv_fd,SOCK_RAW , &tx_ops); + client_tx_fd =__connect_to_fd_opts (srv_fd,SOCK_RAW , true, &tx_ops);close_netns (nstoken);if (!ASSERT_GE (client_tx_fd,0 ,"connect_to_fd_opts" )) goto out;
@@ -209,7 +207,7 @@ void test_bpf_ip_check_defrag_ok(bool ipv6) nstoken =open_netns (NS0 );if (!ASSERT_OK_PTR (nstoken,"setns ns0" )) goto out; - client_rx_fd =__connect_to_fd_opts (srv_fd,0 , &rx_opts); + client_rx_fd =__connect_to_fd_opts (srv_fd,0 , true, &rx_opts);close_netns (nstoken);if (!ASSERT_GE (client_rx_fd,0 ,"connect_to_fd_opts" )) goto out;
Signature · 1 line
2.43.0
Geliang Tang
reviewerPATCH 3/5 selftests/bpf: Drop must_fail from network_helper_opts
In reply to Geliang TangFrom: Geliang Tang <[email protected]>
This patch adds another new parameter "must_fail" for __connect_to_fd_opts, then opts->must_fail can be replaced by "must_fail" parameter in it. In run_test() of cgroup_v1v2, true is passed to it, while false is passed in other places. And the strcut member "must_fail" of network_helper_opts can be dropped now.
--- tools/testing/selftests/bpf/network_helpers.c | 8 ++++---- tools/testing/selftests/bpf/network_helpers.h | 3 +-- tools/testing/selftests/bpf/prog_tests/cgroup_v1v2.c | 5 +---- tools/testing/selftests/bpf/prog_tests/ip_check_defrag.c | 4 ++-- 4 files changed, 8 insertions(+), 12 deletions(-)
tools/testing/selftests/bpf/network_helpers.c +4 -4
@@ -293,7 +293,7 @@ int connect_to_addr(int type, const struct sockaddr_storage *addr, socklen_t addif (settimeo (fd, opts->timeout_ms)) goto error_close; -if (connect_fd_to_addr (fd, addr, addrlen, opts->must_fail)) +if (connect_fd_to_addr (fd, addr, addrlen, false)) goto error_close;return fd;
@@ -303,7 +303,7 @@ int connect_to_addr(int type, const struct sockaddr_storage *addr, socklen_t addreturn -1 ; } -int __connect_to_fd_opts (int server_fd,int type,bool noconnect, +int __connect_to_fd_opts (int server_fd,int type,bool noconnect,bool must_fail,const struct network_helper_opts *opts) {struct sockaddr_storage addr;
@@ -353,7 +353,7 @@ int __connect_to_fd_opts(int server_fd, int type, bool noconnect, goto error_close;if (!noconnect) -if (connect_fd_to_addr (fd, &addr, addrlen, opts->must_fail)) +if (connect_fd_to_addr (fd, &addr, addrlen, must_fail)) goto error_close;return fd;
@@ -365,7 +365,7 @@ int __connect_to_fd_opts(int server_fd, int type, bool noconnect,int connect_to_fd_opts (int server_fd,const struct network_helper_opts *opts) { -return __connect_to_fd_opts (server_fd,0 , false, opts); +return __connect_to_fd_opts (server_fd,0 , false, false, opts); }int connect_to_fd (int server_fd,int timeout_ms)
tools/testing/selftests/bpf/network_helpers.h +1 -2
@@ -23,7 +23,6 @@ typedef __u16 __sum16;struct network_helper_opts {int timeout_ms; -bool must_fail;int proto;int (*post_socket_cb)(int fd,void *opts);void *cb_opts;
@@ -59,7 +58,7 @@ void free_fds(int *fds, unsigned int nr_close_fds);int connect_to_addr (int type,const struct sockaddr_storage *addr,socklen_t len,const struct network_helper_opts *opts);int connect_to_fd (int server_fd,int timeout_ms); -int __connect_to_fd_opts (int server_fd,int type,bool noconnect, +int __connect_to_fd_opts (int server_fd,int type,bool noconnect,bool must_fail,const struct network_helper_opts *opts);int connect_to_fd_opts (int server_fd,const struct network_helper_opts *opts);int connect_fd_to_fd (int client_fd,int server_fd,int timeout_ms);
tools/testing/selftests/bpf/prog_tests/cgroup_v1v2.c +1 -4
@@ -9,9 +9,6 @@static int run_test (int cgroup_fd,int server_fd,bool classid) { -struct network_helper_opts opts = { - .must_fail = true, - };struct connect4_dropper *skel;int fd, err =0 ;
@@ -32,7 +29,7 @@ static int run_test(int cgroup_fd, int server_fd, bool classid) goto out; } - fd =connect_to_fd_opts (server_fd, &opts); + fd =__connect_to_fd_opts (server_fd,0 , false, true,NULL );if (fd <0 ) err =-1 ;else
tools/testing/selftests/bpf/prog_tests/ip_check_defrag.c +2 -2
@@ -198,7 +198,7 @@ void test_bpf_ip_check_defrag_ok(bool ipv6) nstoken =open_netns (NS0 );if (!ASSERT_OK_PTR (nstoken,"setns ns0" )) goto out; - client_tx_fd =__connect_to_fd_opts (srv_fd,SOCK_RAW , true, &tx_ops); + client_tx_fd =__connect_to_fd_opts (srv_fd,SOCK_RAW , true, false, &tx_ops);close_netns (nstoken);if (!ASSERT_GE (client_tx_fd,0 ,"connect_to_fd_opts" )) goto out;
@@ -207,7 +207,7 @@ void test_bpf_ip_check_defrag_ok(bool ipv6) nstoken =open_netns (NS0 );if (!ASSERT_OK_PTR (nstoken,"setns ns0" )) goto out; - client_rx_fd =__connect_to_fd_opts (srv_fd,0 , true, &rx_opts); + client_rx_fd =__connect_to_fd_opts (srv_fd,0 , true, false, &rx_opts);close_netns (nstoken);if (!ASSERT_GE (client_rx_fd,0 ,"connect_to_fd_opts" )) goto out;
Signature · 1 line
2.43.0
Geliang Tang
reviewerPATCH 4/5 selftests/bpf: Use start_server_str in mptcp
In reply to Geliang TangFrom: Geliang Tang <[email protected]>
Since start_server_str() is added now, it can be used in mptcp.c in start_mptcp_server() instead of using helpers make_sockaddr() and start_server_addr() to simplify the code.
--- tools/testing/selftests/bpf/prog_tests/mptcp.c | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-)
tools/testing/selftests/bpf/prog_tests/mptcp.c +1 -6
@@ -104,13 +104,8 @@ static int start_mptcp_server(int family, const char *addr_str, __u16 port, .timeout_ms = timeout_ms, .proto = IPPROTO_MPTCP, }; -struct sockaddr_storage addr; -socklen_t addrlen; -if (make_sockaddr (family, addr_str, port, &addr, &addrlen)) -return -1 ; - -return start_server_addr (SOCK_STREAM , &addr, addrlen, &opts); +return start_server_str (family,SOCK_STREAM , addr_str, port, &opts); }static int verify_tsk (int map_fd,int client_fd)
Signature · 1 line
2.43.0
Re: [PATCH bpf-next 4/5] selftests/bpf: Use start_server_str in mptcp
Quoted 8 lines · Geliang Tang
From: Geliang Tang <[email protected]>
Since
start_server_str()is added now, it can be used inmptcp.cinstart_mptcp_server()instead of using helpersmake_sockaddr()andstart_server_addr()to simplify the code.Signed-off-by Geliang Tang <[email protected]>---
Geliang Tang
reviewerPATCH 5/5 selftests/bpf: Use start_server_str in test_tcp_check_syncookie_user
In reply to Geliang TangFrom: Geliang Tang <[email protected]>
Since start_server_str() is added now, it can be used in script test_tcp_check_syncookie_user.c instead of start_server_addr() to simplify the code.
--- .../bpf/test_tcp_check_syncookie_user.c | 29 ++----------------- 1 file changed, 3 insertions(+), 26 deletions(-)
tools/testing/selftests/bpf/test_tcp_check_syncookie_user.c +3 -26
@@ -156,10 +156,6 @@ static int v6only_false(int fd, void *opts)int main (int argc,char **argv) {struct network_helper_opts opts = {0 }; -struct sockaddr_in addr4; -struct sockaddr_in6 addr6; -struct sockaddr_in addr4dual; -struct sockaddr_in6 addr6dual;int server =-1 ;int server_v6 =-1 ;int server_dual =-1 ;
@@ -181,36 +177,17 @@ int main(int argc, char **argv) goto err; } -memset (&addr4,0 ,sizeof (addr4)); - addr4.sin_family =AF_INET ; - addr4.sin_addr.s_addr =htonl (INADDR_LOOPBACK ); - addr4.sin_port =0 ; -memcpy (&addr4dual, &addr4,sizeof (addr4dual)); - -memset (&addr6,0 ,sizeof (addr6)); - addr6.sin6_family =AF_INET6 ; - addr6.sin6_addr = in6addr_loopback; - addr6.sin6_port =0 ; - -memset (&addr6dual,0 ,sizeof (addr6dual)); - addr6dual.sin6_family =AF_INET6 ; - addr6dual.sin6_addr = in6addr_any; - addr6dual.sin6_port =0 ; - - server =start_server_addr (SOCK_STREAM , (struct sockaddr_storage *)&addr4, -sizeof (addr4),NULL ); + server =start_server_str (AF_INET ,SOCK_STREAM ,"127.0.0.1" ,0 ,NULL );if (server ==-1 ) goto err; opts.post_socket_cb = v6only_true; - server_v6 =start_server_addr (SOCK_STREAM , (struct sockaddr_storage *)&addr6, -sizeof (addr6), &opts); + server_v6 =start_server_str (AF_INET6 ,SOCK_STREAM ,"::1" ,0 , &opts);if (server_v6 ==-1 ) goto err; opts.post_socket_cb = v6only_false; - server_dual =start_server_addr (SOCK_STREAM , (struct sockaddr_storage *)&addr6dual, -sizeof (addr6dual), &opts); + server_dual =start_server_str (AF_INET6 ,SOCK_STREAM ,"::0" ,0 , &opts);if (server_dual ==-1 ) goto err;
Signature · 1 line
2.43.0
Re: [PATCH bpf-next 5/5] selftests/bpf: Use start_server_str in test_tcp_check_syncookie_user
Quoted 8 lines · Geliang Tang
From: Geliang Tang <[email protected]>
Since
start_server_str()is added now, it can be used in scripttest_tcp_check_syncookie_user.cinstead ofstart_server_addr()to simplify the code.Signed-off-by Geliang Tang <[email protected]>---