bpf: allow any port in bpf_bind helper
Subsystems (8)
v3 cover bpf: allow any port in bpf_bind helper
We want to have a tighter control on what ports we bind to in the BPF_CGROUP_INET{4,6}_CONNECT hooks even if it means connect() becomes slightly more expensive.
The series goes like this:
1. selftests: move existing helpers that make it easy to create listener threads into common test_progs part
2. selftests: make sure the helpers above don't get stuck forever in case the tests fails
3. selftests: move some common functionality into network_helpers
4. do small refactoring of __inet{,6}_bind() flags to make it easy to extend them with the additional flags
5. remove the restriction on port being zero in bpf_bind() helper; add new bind flag to prevent POST_BIND hook from being called
Stanislav Fomichev (5):
selftests/bpf: generalize helpers to control background listener selftests/bpf: adopt accept_timeout from sockmap_listen selftests/bpf: move existing common networking parts into
network_helpers
net: refactor arguments of inet{,6}_bind
bpf: allow any port in bpf_bind helper
include/net/inet_common.h | 8 +- include/net/ipv6_stubs.h | 2 +- include/uapi/linux/bpf.h | 9 +- net/core/filter.c | 16 +- net/ipv4/af_inet.c | 20 +- net/ipv6/af_inet6.c | 22 +- tools/include/uapi/linux/bpf.h | 9 +- tools/testing/selftests/bpf/Makefile | 2 +- tools/testing/selftests/bpf/network_helpers.c | 208 ++++++++++++++++++ tools/testing/selftests/bpf/network_helpers.h | 45 ++++ .../bpf/prog_tests/connect_force_port.c | 115 ++++++++++ .../selftests/bpf/prog_tests/fexit_bpf2bpf.c | 1 + .../selftests/bpf/prog_tests/flow_dissector.c | 1 + .../prog_tests/flow_dissector_load_bytes.c | 1 + .../selftests/bpf/prog_tests/global_data.c | 1 + .../selftests/bpf/prog_tests/kfree_skb.c | 1 + .../selftests/bpf/prog_tests/l4lb_all.c | 1 + .../selftests/bpf/prog_tests/map_lock.c | 14 ++ .../selftests/bpf/prog_tests/pkt_access.c | 1 + .../selftests/bpf/prog_tests/pkt_md_access.c | 1 + .../selftests/bpf/prog_tests/prog_run_xattr.c | 1 + .../bpf/prog_tests/queue_stack_map.c | 1 + .../selftests/bpf/prog_tests/signal_pending.c | 1 + .../selftests/bpf/prog_tests/skb_ctx.c | 1 + .../selftests/bpf/prog_tests/sockmap_listen.c | 35 +-- .../selftests/bpf/prog_tests/spinlock.c | 14 ++ .../selftests/bpf/prog_tests/tcp_rtt.c | 116 +--------- tools/testing/selftests/bpf/prog_tests/xdp.c | 1 + .../bpf/prog_tests/xdp_adjust_tail.c | 1 + .../selftests/bpf/prog_tests/xdp_bpf2bpf.c | 1 + .../selftests/bpf/prog_tests/xdp_noinline.c | 1 + .../selftests/bpf/progs/connect_force_port4.c | 28 +++ .../selftests/bpf/progs/connect_force_port6.c | 28 +++ tools/testing/selftests/bpf/test_progs.c | 30 --- tools/testing/selftests/bpf/test_progs.h | 23 -- 35 files changed, 522 insertions(+), 238 deletions(-) create mode 100644 tools/testing/selftests/bpf/network_helpers.c create mode 100644 tools/testing/selftests/bpf/network_helpers.h create mode 100644 tools/testing/selftests/bpf/prog_tests/connect_force_port.c create mode 100644 tools/testing/selftests/bpf/progs/connect_force_port4.c create mode 100644 tools/testing/selftests/bpf/progs/connect_force_port6.c
Signature · 1 line
2.26.2.526.g744177e7f7-goog
PATCH v3 1/5 selftests/bpf: generalize helpers to control background listener
Move the following routines that let us start a background listener thread and connect to a server by fd to the test_prog:
* start_server_thread - start background INADDR_ANY thread
* stop_server_thread - stop the thread
* connect_to_fd - connect to the server identified by fd
These will be used in the next commit.
Also, extend these helpers to support AF_INET6 and accept the family as an argument.
v3:
* export extra helper to start server without a thread (Martin KaFai Lau)
v2:
* put helpers into network_helpers.c (Andrii Nakryiko)
--- tools/testing/selftests/bpf/Makefile | 2 +- tools/testing/selftests/bpf/network_helpers.c | 164 ++++++++++++++++++ tools/testing/selftests/bpf/network_helpers.h | 12 ++ .../selftests/bpf/prog_tests/tcp_rtt.c | 116 +------------ 4 files changed, 181 insertions(+), 113 deletions(-) create mode 100644 tools/testing/selftests/bpf/network_helpers.c create mode 100644 tools/testing/selftests/bpf/network_helpers.h
tools/testing/selftests/bpf/Makefile +1 -1
@@ -354,7 +354,7 @@ endef TRUNNER_TESTS_DIR := prog_tests TRUNNER_BPF_PROGS_DIR := progs TRUNNER_EXTRA_SOURCES := test_progs.c cgroup_helpers.c trace_helpers.c \ - flow_dissector_load.h + network_helpers.c flow_dissector_load.h TRUNNER_EXTRA_FILES := $(OUTPUT)/urandom_read \ $(wildcard progs/btf_dump_test_case_*.c) TRUNNER_BPF_BUILD_RULE := CLANG_BPF_BUILD_RULE
tools/testing/selftests/bpf/network_helpers.c new file +164 -0
164 lines not on this page · raw message
tools/testing/selftests/bpf/network_helpers.h new file +12 -0
@@ -0,0 +1,12 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +#ifndef __NETWORK_HELPERS_H +#define __NETWORK_HELPERS_H +#include <sys/socket.h> +#include <sys/types.h> + +int start_server (int family,int type); +int start_server_thread (int family,int type); +void stop_server_thread (int fd); +int connect_to_fd (int family,int type,int server_fd); + +#endif
tools/testing/selftests/bpf/prog_tests/tcp_rtt.c +4 -112
154 lines not on this page · raw message
Signature · 1 line
2.26.2.526.g744177e7f7-goog
Re: [PATCH bpf-next v3 1/5] selftests/bpf: generalize helpers to control background listener
Quoted 359 lines · Stanislav Fomichev
Move the following routines that let us start a background listener thread and connect to a server by fd to the
test_prog:
*start_server_thread- start backgroundINADDR_ANYthread
*stop_server_thread- stop the thread
*connect_to_fd- connect to the server identified by fdThese will be used in the next commit.
Also, extend these helpers to support
AF_INET6and accept the family as an argument.v3:
* export extra helper to start server without a thread (Martin KaFai Lau)v2:
* put helpers intonetwork_helpers.c(Andrii Nakryiko)Cc Andrey Ignatov <[email protected]>Cc Martin KaFai Lau <[email protected]>Signed-off-by Stanislav Fomichev <[email protected]>--- tools/testing/selftests/bpf/Makefile | 2 +- tools/testing/selftests/bpf/network_helpers.c | 164 ++++++++++++++++++ tools/testing/selftests/bpf/network_helpers.h | 12 ++ .../selftests/bpf/prog_tests/tcp_rtt.c | 116 +------------ 4 files changed, 181 insertions(+), 113 deletions(-) create mode 100644 tools/testing/selftests/bpf/network_helpers.c create mode 100644 tools/testing/selftests/bpf/network_helpers.htools/testing/selftests/bpf/Makefile +1
-1@@ -354,7 +354,7 @@ endef TRUNNER_TESTS_DIR := prog_tests TRUNNER_BPF_PROGS_DIR := progs TRUNNER_EXTRA_SOURCES := test_progs.c cgroup_helpers.c trace_helpers.c \ - flow_dissector_load.h + network_helpers.c flow_dissector_load.h TRUNNER_EXTRA_FILES := $(OUTPUT)/urandom_read \ $(wildcard progs/btf_dump_test_case_*.c) TRUNNER_BPF_BUILD_RULE := CLANG_BPF_BUILD_RULEtools/testing/selftests/bpf/network_helpers.c new file +164
-0@@ -0,0 +1,164 @@ +// SPDX-License-Identifier: GPL-2.0-only +#include <errno.h> +#include <pthread.h> +#include <stdbool.h> +#include <stdio.h> +#include <string.h> +#include <unistd.h> +#include <linux/err.h> +#include <linux/in.h> +#include <linux/in6.h> + +#include "network_helpers.h" + +#define CHECK_FAIL (condition) ({ \ + int __ret = !!(condition); \ + int __save_errno = errno; \ + if (__ret) { \ + fprintf(stdout, "%s:FAIL:%d\n", __func__, __LINE__); \ + } \ + errno = __save_errno; \ + __ret; \ +}) + +#define clean_errno () (errno == 0 ? "None" : strerror(errno)) +#define log_err (MSG , ...) fprintf(stderr, "(%s:%d: errno: %s) " MSG "\n", \ + __FILE__, __LINE__, clean_errno(), ##__VA_ARGS__) + +int start_server (int family,int type) +{ +struct sockaddr_storage addr = {}; +socklen_t len; +int fd; + +if (family ==AF_INET ) { +struct sockaddr_in *sin = (void *)&addr; + + sin->sin_family =AF_INET ; + len =sizeof (*sin); + }else { +struct sockaddr_in6 *sin6 = (void *)&addr; + + sin6->sin6_family =AF_INET6 ; + len =sizeof (*sin6); + } + + fd =socket (family, type |SOCK_NONBLOCK ,0 ); +if (fd <0 ) { +log_err ("Failed to create server socket" ); +return -1 ; + } + +if (bind (fd, (const struct sockaddr *)&addr, len) <0 ) { +log_err ("Failed to bind socket" ); +close (fd); +return -1 ; + } + +if (type ==SOCK_STREAM ) { +if (listen (fd,1 ) <0 ) { +log_err ("Failed to listed on socket" ); +close (fd); +return -1 ; + } + } + +return fd; +} + +static pthread_mutex_t server_started_mtx =PTHREAD_MUTEX_INITIALIZER ; +static pthread_cond_t server_started =PTHREAD_COND_INITIALIZER ; +static volatile bool server_done; +pthread_t server_tid; + +static void *server_thread (void *arg) +{ +struct sockaddr_storage addr; +socklen_t len =sizeof (addr); +int fd = *(int *)arg; +int client_fd; + +pthread_mutex_lock (&server_started_mtx); +pthread_cond_signal (&server_started); +pthread_mutex_unlock (&server_started_mtx); + +while (true) { + client_fd =accept (fd, (struct sockaddr *)&addr, &len); +if (client_fd ==-1 && errno ==EAGAIN ) { +usleep (50 ); +continue ; + } +break ; + } +if (CHECK_FAIL (client_fd <0 )) { +perror ("Failed to accept client" ); +return ERR_PTR (-1 ); + } + +while (!server_done) +usleep (50 ); + +close (client_fd); + +return NULL ; +} + +int start_server_thread (int family,int type) +{ +int fd =start_server (family, type); + +if (fd <0 ) +return -1 ; + +if (CHECK_FAIL (pthread_create (&server_tid,NULL , server_thread, + (void *)&fd))) + goto err; + +pthread_mutex_lock (&server_started_mtx); +pthread_cond_wait (&server_started, &server_started_mtx); +pthread_mutex_unlock (&server_started_mtx); + +return fd; +err: +close (fd); +return -1 ; +} + +void stop_server_thread (int fd) +{ +void *server_res; + + server_done = true; +CHECK_FAIL (pthread_join (server_tid, &server_res)); +CHECK_FAIL (IS_ERR (server_res)); +close (fd); +} + +int connect_to_fd (int family,int type,int server_fd) +{ +struct sockaddr_storage addr; +socklen_t len =sizeof (addr); +int fd; + + fd =socket (family, type,0 ); +if (fd <0 ) { +log_err ("Failed to create client socket" ); +return -1 ; + } + +if (getsockname (server_fd, (struct sockaddr *)&addr, &len)) { +log_err ("Failed to get server addr" ); + goto out; + } + +if (connect (fd, (const struct sockaddr *)&addr, len) <0 ) { +log_err ("Fail to connect to server with family %d" , family); + goto out; + } + +return fd; + +out: +close (fd); +return -1 ; +}tools/testing/selftests/bpf/network_helpers.h new file +12
-0@@ -0,0 +1,12 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +#ifndef __NETWORK_HELPERS_H +#define __NETWORK_HELPERS_H +#include <sys/socket.h> +#include <sys/types.h> + +int start_server (int family,int type); +int start_server_thread (int family,int type); +void stop_server_thread (int fd); +int connect_to_fd (int family,int type,int server_fd); + +#endif tools/testing/selftests/bpf/prog_tests/tcp_rtt.c +2
-96@@ -1,6 +1,7 @@// SPDX-License-Identifier: GPL-2.0 #include <test_progs.h> #include "cgroup_helpers.h" +#include "network_helpers.h" struct tcp_rtt_storage {__u32 invoked;@@ -87,34 +88,6 @@ static int verify_sk(int map_fd, int client_fd, const char *msg, __u32 invoked,return err; } -static int connect_to_server (int server_fd) -{ -struct sockaddr_storage addr; -socklen_t len =sizeof (addr); -int fd; - - fd =socket (AF_INET ,SOCK_STREAM ,0 ); -if (fd <0 ) { -log_err ("Failed to create client socket" ); -return -1 ; - } - -if (getsockname (server_fd, (struct sockaddr *)&addr, &len)) { -log_err ("Failed to get server addr" ); - goto out; - } - -if (connect (fd, (const struct sockaddr *)&addr, len) <0 ) { -log_err ("Fail to connect to server" ); - goto out; - } - -return fd; - -out: -close (fd); -return -1 ; -}static int run_test (int cgroup_fd,int server_fd) {@@ -145,7 +118,7 @@ static int run_test(int cgroup_fd, int server_fd) goto close_bpf_object; } - client_fd =connect_to_server (server_fd); + client_fd =connect_to_fd (AF_INET ,SOCK_STREAM , server_fd);if (client_fd <0 ) { err =-1 ; goto close_bpf_object;@@ -180,103 +153,22 @@ static int run_test(int cgroup_fd, int server_fd)return err; } -static int start_server (void ) -{ -struct sockaddr_in addr = { - .sin_family =AF_INET , - .sin_addr.s_addr =htonl (INADDR_LOOPBACK ), - }; -int fd; - - fd =socket (AF_INET ,SOCK_STREAM |SOCK_NONBLOCK ,0 ); -if (fd <0 ) { -log_err ("Failed to create server socket" ); -return -1 ; - } - -if (bind (fd, (const struct sockaddr *)&addr,sizeof (addr)) <0 ) { -log_err ("Failed to bind socket" ); -close (fd); -return -1 ; - } - -return fd; -} - -static pthread_mutex_t server_started_mtx =PTHREAD_MUTEX_INITIALIZER ; -static pthread_cond_t server_started =PTHREAD_COND_INITIALIZER ; -static volatile bool server_done = false; - -static void *server_thread (void *arg) -{ -struct sockaddr_storage addr; -socklen_t len =sizeof (addr); -int fd = *(int *)arg; -int client_fd; -int err; - - err =listen (fd,1 ); - -pthread_mutex_lock (&server_started_mtx); -pthread_cond_signal (&server_started); -pthread_mutex_unlock (&server_started_mtx); - -if (CHECK_FAIL (err <0 )) { -perror ("Failed to listed on socket" ); -return ERR_PTR (err); - } - -while (true) { - client_fd =accept (fd, (struct sockaddr *)&addr, &len); -if (client_fd ==-1 && errno ==EAGAIN ) { -usleep (50 ); -continue ; - } -break ; - } -if (CHECK_FAIL (client_fd <0 )) { -perror ("Failed to accept client" ); -return ERR_PTR (err); - } - -while (!server_done) -usleep (50 ); - -close (client_fd); - -return NULL ; -} -void test_tcp_rtt (void ) {
tools/testing/selftests/bpf/prog_tests/tcp_rtt.c static int run_test(int cgroup_fd, int server_fd) · line 164
Quoted 10 lines
tools/testing/selftests/bpf/prog_tests/tcp_rtt.c +1
-3int server_fd, cgroup_fd; -pthread_t tid; -void *server_res; cgroup_fd =test__join_cgroup ("/tcp_rtt" );if (CHECK_FAIL (cgroup_fd <0 ))return ; - server_fd =start_server (); + server_fd =start_server_thread (AF_INET ,SOCK_STREAM );
I still don't see a thread is needed in this existing test_tcp_rtt also.
I was hoping the start/stop_server_thread() helpers can be removed. I am not positive the future tests will find start/stop_server_thread() useful as is because it only does accept() and there is no easy way to get the accept-ed() fd.
If this test needs to keep the thread, may be only keep the start/stop_server_thread() in this test for now until there is another use case that can benefit from them.
Keep the start_server() and connect_to_fd() in the new network_helpers.c. I think at least sk_assign.c (and likely a few others) should be able to reuse them later. They are doing something very close to start_server() and connect_to_fd().
Thoughts?
Re: [PATCH bpf-next v3 1/5] selftests/bpf: generalize helpers to control background listener
Quoted 15 lines · On Wed, 6 May 2020 23:09:21 -0700 Martin KaFai Lau <[email protected]> wrote:
Quoted 14 lines · Stanislav Fomichev
Move the following routines that let us start a background listener thread and connect to a server by fd to the
test_prog:
*start_server_thread- start backgroundINADDR_ANYthread
*stop_server_thread- stop the thread
*connect_to_fd- connect to the server identified by fdThese will be used in the next commit.
Also, extend these helpers to support
AF_INET6and accept the family as an argument.v3:
* export extra helper to start server without a thread (Martin KaFai Lau)
Quoted 7 lines
Quoted 7 lines
v2:
* put helpers intonetwork_helpers.c(Andrii Nakryiko)Cc Andrey Ignatov <[email protected]>Cc Martin KaFai Lau <[email protected]>Signed-off-by Stanislav Fomichev <[email protected]>---
Thanks for extracting network_helpers. I've seen network syscall wrappers repeat between the tests I've touched. Will be super useful to have it in one place.
[...]
Quoted 11 lines
I still don't see a thread is needed in this existing
test_tcp_rttalso.I was hoping the start/stop_server_thread() helpers can be removed. I am not positive the future tests will find start/stop_server_thread() useful as is because it only does
accept()and there is no easy way to get the accept-ed() fd.If this test needs to keep the thread, may be only keep the start/stop_server_thread() in this test for now until there is another use case that can benefit from them.
Quoted 7 lines
Keep the
start_server()andconnect_to_fd()in the newnetwork_helpers.c. I think at leastsk_assign.c(and likely a few others) should be able to reuse them later. They are doing something very close tostart_server()andconnect_to_fd().Thoughts?
IMHO starting a thread to accept() a connection and close it is an overkill. I wouldn't spawn a thread if I can get away with interleaving client and server operations on sockets on the main thread. Makes the test code easier to follow, and strace, for me.
Re: [PATCH bpf-next v3 1/5] selftests/bpf: generalize helpers to control background listener
In reply to Martin KaFai LauQuoted 389 lines · Martin KaFai Lau
Quoted 6 lines · Stanislav Fomichev
Move the following routines that let us start a background listener thread and connect to a server by fd to the
test_prog:
*start_server_thread- start backgroundINADDR_ANYthread
*stop_server_thread- stop the thread
*connect_to_fd- connect to the server identified by fdQuoted 7 lines
These will be used in the next commit.
Also, extend these helpers to support
AF_INET6and accept the family as an argument.v3:
* export extra helper to start server without a thread (Martin KaFaiLau)
Quoted 16 lines
v2:
* put helpers intonetwork_helpers.c(Andrii Nakryiko)Cc Andrey Ignatov <[email protected]>Cc Martin KaFai Lau <[email protected]>Signed-off-by Stanislav Fomichev <[email protected]>--- tools/testing/selftests/bpf/Makefile | 2 +- tools/testing/selftests/bpf/network_helpers.c | 164 ++++++++++++++++++ tools/testing/selftests/bpf/network_helpers.h | 12 ++ .../selftests/bpf/prog_tests/tcp_rtt.c | 116 +------------ 4 files changed, 181 insertions(+), 113 deletions(-) create mode 100644 tools/testing/selftests/bpf/network_helpers.c create mode 100644 tools/testing/selftests/bpf/network_helpers.hReview
Quoted 1 line
(quoted diff) +0
-0b/tools/testing/selftests/bpf/Makefile
tools/testing/selftests/bpf/Makefile endef · line 356
Quoted 7 lines
index
3d942be23d09..8f25966b500b100644tools/testing/selftests/bpf/Makefile +0
-0@@ -354,7 +354,7 @@ endef TRUNNER_TESTS_DIR := prog_tests TRUNNER_BPF_PROGS_DIR := progs TRUNNER_EXTRA_SOURCES := test_progs.c cgroup_helpers.c trace_helpers.c\
Review
Quoted 6 lines
tools/testing/selftests/bpf/Makefile +1
-1- flow_dissector_load.h + network_helpers.c flow_dissector_load.h TRUNNER_EXTRA_FILES := $(OUTPUT)/urandom_read \ $(wildcard progs/btf_dump_test_case_*.c) TRUNNER_BPF_BUILD_RULE := CLANG_BPF_BUILD_RULE(quoted diff) +0
-0
b/tools/testing/selftests/bpf/network_helpers.cQuoted 20 lines
new file mode 100644
index 000000000000..6ad16dfebfb2tools/testing/selftests/bpf/network_helpers.c new file +15
-0@@ -0,0 +1,164 @@ +// SPDX-License-Identifier: GPL-2.0-only +#include <errno.h> +#include <pthread.h> +#include <stdbool.h> +#include <stdio.h> +#include <string.h> +#include <unistd.h> +#include <linux/err.h> +#include <linux/in.h> +#include <linux/in6.h> + +#include "network_helpers.h" + +#define CHECK_FAIL (condition) ({ \ + int __ret = !!(condition); \tools/testing/selftests/bpf/network_helpers.c line 25
Quoted 10 lines
tools/testing/selftests/bpf/network_helpers.c new file +10
-0+int __save_errno = errno; \ +if (__ret) { \ +fprintf (stdout,"%s:FAIL:%d\n" , __func__, __LINE__); \ + } \ + errno = __save_errno; \ + __ret; \ +}) + +#define clean_errno () (errno == 0 ? "None" : strerror(errno)) +#define log_err (MSG , ...) fprintf(stderr, "(%s:%d: errno: %s) "MSG "\n", \
Review
Quoted 140 lines
tools/testing/selftests/bpf/network_helpers.c new file +139
-0+ __FILE__, __LINE__, clean_errno(), ##__VA_ARGS__) + +int start_server (int family,int type) +{ +struct sockaddr_storage addr = {}; +socklen_t len; +int fd; + +if (family ==AF_INET ) { +struct sockaddr_in *sin = (void *)&addr; + + sin->sin_family =AF_INET ; + len =sizeof (*sin); + }else { +struct sockaddr_in6 *sin6 = (void *)&addr; + + sin6->sin6_family =AF_INET6 ; + len =sizeof (*sin6); + } + + fd =socket (family, type |SOCK_NONBLOCK ,0 ); +if (fd <0 ) { +log_err ("Failed to create server socket" ); +return -1 ; + } + +if (bind (fd, (const struct sockaddr *)&addr, len) <0 ) { +log_err ("Failed to bind socket" ); +close (fd); +return -1 ; + } + +if (type ==SOCK_STREAM ) { +if (listen (fd,1 ) <0 ) { +log_err ("Failed to listed on socket" ); +close (fd); +return -1 ; + } + } + +return fd; +} + +static pthread_mutex_t server_started_mtx =PTHREAD_MUTEX_INITIALIZER ; +static pthread_cond_t server_started =PTHREAD_COND_INITIALIZER ; +static volatile bool server_done; +pthread_t server_tid; + +static void *server_thread (void *arg) +{ +struct sockaddr_storage addr; +socklen_t len =sizeof (addr); +int fd = *(int *)arg; +int client_fd; + +pthread_mutex_lock (&server_started_mtx); +pthread_cond_signal (&server_started); +pthread_mutex_unlock (&server_started_mtx); + +while (true) { + client_fd =accept (fd, (struct sockaddr *)&addr, &len); +if (client_fd ==-1 && errno ==EAGAIN ) { +usleep (50 ); +continue ; + } +break ; + } +if (CHECK_FAIL (client_fd <0 )) { +perror ("Failed to accept client" ); +return ERR_PTR (-1 ); + } + +while (!server_done) +usleep (50 ); + +close (client_fd); + +return NULL ; +} + +int start_server_thread (int family,int type) +{ +int fd =start_server (family, type); + +if (fd <0 ) +return -1 ; + +if (CHECK_FAIL (pthread_create (&server_tid,NULL , server_thread, + (void *)&fd))) + goto err; + +pthread_mutex_lock (&server_started_mtx); +pthread_cond_wait (&server_started, &server_started_mtx); +pthread_mutex_unlock (&server_started_mtx); + +return fd; +err: +close (fd); +return -1 ; +} + +void stop_server_thread (int fd) +{ +void *server_res; + + server_done = true; +CHECK_FAIL (pthread_join (server_tid, &server_res)); +CHECK_FAIL (IS_ERR (server_res)); +close (fd); +} + +int connect_to_fd (int family,int type,int server_fd) +{ +struct sockaddr_storage addr; +socklen_t len =sizeof (addr); +int fd; + + fd =socket (family, type,0 ); +if (fd <0 ) { +log_err ("Failed to create client socket" ); +return -1 ; + } + +if (getsockname (server_fd, (struct sockaddr *)&addr, &len)) { +log_err ("Failed to get server addr" ); + goto out; + } + +if (connect (fd, (const struct sockaddr *)&addr, len) <0 ) { +log_err ("Fail to connect to server with family %d" , family); + goto out; + } + +return fd; + +out: +close (fd); +return -1 ; +}(quoted diff) +0
-0
b/tools/testing/selftests/bpf/network_helpers.hReview
Quoted 18 lines
new file mode 100644
index 000000000000..4ed31706b7f4tools/testing/selftests/bpf/network_helpers.h new file +12
-0@@ -0,0 +1,12 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +#ifndef __NETWORK_HELPERS_H +#define __NETWORK_HELPERS_H +#include <sys/socket.h> +#include <sys/types.h> + +int start_server (int family,int type); +int start_server_thread (int family,int type); +void stop_server_thread (int fd); +int connect_to_fd (int family,int type,int server_fd); + +#endif (quoted diff) +0
-0
b/tools/testing/selftests/bpf/prog_tests/tcp_rtt.ctools/testing/selftests/bpf/prog_tests/tcp_rtt.c static int verify_sk(int map_fd, int client_fd,
Quoted 12 lines
index
e56b52ab41da..4aaa1e6e33ad100644tools/testing/selftests/bpf/prog_tests/tcp_rtt.c +1
-0@@ -1,6 +1,7 @@// SPDX-License-Identifier: GPL-2.0 #include <test_progs.h> #include "cgroup_helpers.h" +#include "network_helpers.h" struct tcp_rtt_storage {__u32 invoked;@@ -87,34 +88,6 @@ static int verify_sk(int map_fd, int client_fd,const char *msg,
__u32invoked,Quoted 116 lines
tools/testing/selftests/bpf/prog_tests/tcp_rtt.c +1
-96return err; } -static int connect_to_server (int server_fd) -{ -struct sockaddr_storage addr; -socklen_t len =sizeof (addr); -int fd; - - fd =socket (AF_INET ,SOCK_STREAM ,0 ); -if (fd <0 ) { -log_err ("Failed to create client socket" ); -return -1 ; - } - -if (getsockname (server_fd, (struct sockaddr *)&addr, &len)) { -log_err ("Failed to get server addr" ); - goto out; - } - -if (connect (fd, (const struct sockaddr *)&addr, len) <0 ) { -log_err ("Fail to connect to server" ); - goto out; - } - -return fd; - -out: -close (fd); -return -1 ; -}static int run_test (int cgroup_fd,int server_fd) {@@ -145,7 +118,7 @@ static int run_test(int cgroup_fd, int server_fd) goto close_bpf_object; } - client_fd =connect_to_server (server_fd); + client_fd =connect_to_fd (AF_INET ,SOCK_STREAM , server_fd);if (client_fd <0 ) { err =-1 ; goto close_bpf_object;@@ -180,103 +153,22 @@ static int run_test(int cgroup_fd, int server_fd)return err; } -static int start_server (void ) -{ -struct sockaddr_in addr = { - .sin_family =AF_INET , - .sin_addr.s_addr =htonl (INADDR_LOOPBACK ), - }; -int fd; - - fd =socket (AF_INET ,SOCK_STREAM |SOCK_NONBLOCK ,0 ); -if (fd <0 ) { -log_err ("Failed to create server socket" ); -return -1 ; - } - -if (bind (fd, (const struct sockaddr *)&addr,sizeof (addr)) <0 ) { -log_err ("Failed to bind socket" ); -close (fd); -return -1 ; - } - -return fd; -} - -static pthread_mutex_t server_started_mtx =PTHREAD_MUTEX_INITIALIZER ; -static pthread_cond_t server_started =PTHREAD_COND_INITIALIZER ; -static volatile bool server_done = false; - -static void *server_thread (void *arg) -{ -struct sockaddr_storage addr; -socklen_t len =sizeof (addr); -int fd = *(int *)arg; -int client_fd; -int err; - - err =listen (fd,1 ); - -pthread_mutex_lock (&server_started_mtx); -pthread_cond_signal (&server_started); -pthread_mutex_unlock (&server_started_mtx); - -if (CHECK_FAIL (err <0 )) { -perror ("Failed to listed on socket" ); -return ERR_PTR (err); - } - -while (true) { - client_fd =accept (fd, (struct sockaddr *)&addr, &len); -if (client_fd ==-1 && errno ==EAGAIN ) { -usleep (50 ); -continue ; - } -break ; - } -if (CHECK_FAIL (client_fd <0 )) { -perror ("Failed to accept client" ); -return ERR_PTR (err); - } - -while (!server_done) -usleep (50 ); - -close (client_fd); - -return NULL ; -} -void test_tcp_rtt (void ) {tools/testing/selftests/bpf/prog_tests/tcp_rtt.c static int run_test(int cgroup_fd, int server_fd) · line 164
Quoted 10 lines
tools/testing/selftests/bpf/prog_tests/tcp_rtt.c +1
-3int server_fd, cgroup_fd; -pthread_t tid; -void *server_res; cgroup_fd =test__join_cgroup ("/tcp_rtt" );if (CHECK_FAIL (cgroup_fd <0 ))return ; - server_fd =start_server (); + server_fd =start_server_thread (AF_INET ,SOCK_STREAM );I still don't see a thread is needed in this existing
test_tcp_rttalso.I was hoping the start/stop_server_thread() helpers can be removed. I am not positive the future tests will find start/stop_server_thread() useful as is because it only does
accept()and there is no easy way to get the accept-ed() fd.If this test needs to keep the thread, may be only keep the start/stop_server_thread() in this test for now until there is another use case that can benefit from them.
Quoted 7 lines
Keep the
start_server()andconnect_to_fd()in the newnetwork_helpers.c. I think at leastsk_assign.c(and likely a few others) should be able to reuse them later. They are doing something very close tostart_server()andconnect_to_fd().Thoughts?
I think you're right and tcp_rtt might not need a thread as well. Let me fiddle with it a bit.
PATCH v3 2/5 selftests/bpf: adopt accept_timeout from sockmap_listen
In reply to Stanislav FomichevMove accept_timeout and recv_timeout to the common place so they can be reused by the other tests. Switch to accept_timeout() in test_progs instead of doing while loop around accept().
This prevents the tests that use start_server_thread/stop_server_thread from being stuck when the error occurs.
--- tools/testing/selftests/bpf/network_helpers.c | 43 +++++++++++++++---- tools/testing/selftests/bpf/network_helpers.h | 4 ++ .../selftests/bpf/prog_tests/sockmap_listen.c | 35 +-------------- 3 files changed, 40 insertions(+), 42 deletions(-)
tools/testing/selftests/bpf/network_helpers.c +35 -8
@@ -82,14 +82,7 @@ static void *server_thread(void *arg)pthread_cond_signal (&server_started);pthread_mutex_unlock (&server_started_mtx); -while (true) { - client_fd =accept (fd, (struct sockaddr *)&addr, &len); -if (client_fd ==-1 && errno ==EAGAIN ) { -usleep (50 ); -continue ; - } -break ; - } + client_fd =accept_timeout (fd, (struct sockaddr *)&addr, &len,3 );if (CHECK_FAIL (client_fd <0 )) {perror ("Failed to accept client" );return ERR_PTR (-1 );
@@ -162,3 +155,37 @@ int connect_to_fd(int family, int type, int server_fd)close (fd);return -1 ; } + +static int poll_read (int fd,unsigned int timeout_sec) +{ +struct timeval timeout = { .tv_sec = timeout_sec }; +fd_set rfds; +int r; + +FD_ZERO (&rfds); +FD_SET (fd, &rfds); + + r =select (fd +1 , &rfds,NULL ,NULL , &timeout); +if (r ==0 ) + errno =ETIME ; + +return r ==1 ?0 :-1 ; +} + +int accept_timeout (int fd,struct sockaddr *addr,socklen_t *len, +unsigned int timeout_sec) +{ +if (poll_read (fd, timeout_sec)) +return -1 ; + +return accept (fd, addr, len); +} + +int recv_timeout (int fd,void *buf,size_t len,int flags, +unsigned int timeout_sec) +{ +if (poll_read (fd, timeout_sec)) +return -1 ; + +return recv (fd, buf, len, flags); +}
tools/testing/selftests/bpf/network_helpers.h +4 -0
@@ -8,5 +8,9 @@ int start_server(int family, int type);int start_server_thread (int family,int type);void stop_server_thread (int fd);int connect_to_fd (int family,int type,int server_fd); +int accept_timeout (int fd,struct sockaddr *addr,socklen_t *len, +unsigned int timeout_sec); +int recv_timeout (int fd,void *buf,size_t len,int flags, +unsigned int timeout_sec);#endif
tools/testing/selftests/bpf/prog_tests/sockmap_listen.c +1 -34
@@ -24,6 +24,7 @@#include "bpf_util.h" #include "test_progs.h" +#include "network_helpers.h" #include "test_sockmap_listen.skel.h" #define IO_TIMEOUT_SEC 30
@@ -195,40 +196,6 @@ __ret; \ }) -static int poll_read (int fd,unsigned int timeout_sec) -{ -struct timeval timeout = { .tv_sec = timeout_sec }; -fd_set rfds; -int r; - -FD_ZERO (&rfds); -FD_SET (fd, &rfds); - - r =select (fd +1 , &rfds,NULL ,NULL , &timeout); -if (r ==0 ) - errno =ETIME ; - -return r ==1 ?0 :-1 ; -} - -static int accept_timeout (int fd,struct sockaddr *addr,socklen_t *len, -unsigned int timeout_sec) -{ -if (poll_read (fd, timeout_sec)) -return -1 ; - -return accept (fd, addr, len); -} - -static int recv_timeout (int fd,void *buf,size_t len,int flags, -unsigned int timeout_sec) -{ -if (poll_read (fd, timeout_sec)) -return -1 ; - -return recv (fd, buf, len, flags); -} -static void init_addr_loopback4 (struct sockaddr_storage *ss,socklen_t *len) {struct sockaddr_in *addr4 =memset (ss,0 ,sizeof (*ss));
Signature · 1 line
2.26.2.526.g744177e7f7-goog
PATCH v3 3/5 selftests/bpf: move existing common networking parts into network_helpers
In reply to Stanislav Fomichev1. Move pkt_v4 and pkt_v6 into network_helpers and adjust the users.
2. Copy-paste spin_lock_thread into two tests that use it.
--- tools/testing/selftests/bpf/network_helpers.c | 17 +++++++++++ tools/testing/selftests/bpf/network_helpers.h | 29 ++++++++++++++++++ .../selftests/bpf/prog_tests/fexit_bpf2bpf.c | 1 + .../selftests/bpf/prog_tests/flow_dissector.c | 1 + .../prog_tests/flow_dissector_load_bytes.c | 1 + .../selftests/bpf/prog_tests/global_data.c | 1 + .../selftests/bpf/prog_tests/kfree_skb.c | 1 + .../selftests/bpf/prog_tests/l4lb_all.c | 1 + .../selftests/bpf/prog_tests/map_lock.c | 14 +++++++++ .../selftests/bpf/prog_tests/pkt_access.c | 1 + .../selftests/bpf/prog_tests/pkt_md_access.c | 1 + .../selftests/bpf/prog_tests/prog_run_xattr.c | 1 + .../bpf/prog_tests/queue_stack_map.c | 1 + .../selftests/bpf/prog_tests/signal_pending.c | 1 + .../selftests/bpf/prog_tests/skb_ctx.c | 1 + .../selftests/bpf/prog_tests/spinlock.c | 14 +++++++++ tools/testing/selftests/bpf/prog_tests/xdp.c | 1 + .../bpf/prog_tests/xdp_adjust_tail.c | 1 + .../selftests/bpf/prog_tests/xdp_bpf2bpf.c | 1 + .../selftests/bpf/prog_tests/xdp_noinline.c | 1 + tools/testing/selftests/bpf/test_progs.c | 30 ------------------- tools/testing/selftests/bpf/test_progs.h | 23 -------------- 22 files changed, 90 insertions(+), 53 deletions(-)
tools/testing/selftests/bpf/network_helpers.c +17 -0
@@ -25,6 +25,23 @@#define log_err (MSG , ...) fprintf(stderr, "(%s:%d: errno: %s) " MSG "\n", \ __FILE__, __LINE__, clean_errno(), ##__VA_ARGS__) +struct ipv4_packet pkt_v4 = { + .eth.h_proto =__bpf_constant_htons (ETH_P_IP ), + .iph.ihl =5 , + .iph.protocol =IPPROTO_TCP , + .iph.tot_len =__bpf_constant_htons (MAGIC_BYTES ), + .tcp.urg_ptr =123 , + .tcp.doff =5 , +}; + +struct ipv6_packet pkt_v6 = { + .eth.h_proto =__bpf_constant_htons (ETH_P_IPV6 ), + .iph.nexthdr =IPPROTO_TCP , + .iph.payload_len =__bpf_constant_htons (MAGIC_BYTES ), + .tcp.urg_ptr =123 , + .tcp.doff =5 , +}; +int start_server (int family,int type) {struct sockaddr_storage addr = {};
tools/testing/selftests/bpf/network_helpers.h +29 -0
@@ -3,6 +3,35 @@#define __NETWORK_HELPERS_H#include <sys/socket.h> #include <sys/types.h> +#include <linux/types.h> +typedef __u16 __sum16 ; +#include <linux/if_ether.h> +#include <linux/if_packet.h> +#include <linux/ip.h> +#include <linux/ipv6.h> +#include <netinet/tcp.h> +#include <bpf/bpf_endian.h> + +#define MAGIC_VAL 0x1234 +#define NUM_ITER 100000 +#define VIP_NUM 5 +#define MAGIC_BYTES 123 + +/* ipv4 test vector */ +struct ipv4_packet { +struct ethhdr eth; +struct iphdr iph; +struct tcphdr tcp; +} __packed; +extern struct ipv4_packet pkt_v4; + +/* ipv6 test vector */ +struct ipv6_packet { +struct ethhdr eth; +struct ipv6hdr iph; +struct tcphdr tcp; +} __packed; +extern struct ipv6_packet pkt_v6;int start_server (int family,int type);int start_server_thread (int family,int type);
tools/testing/selftests/bpf/prog_tests/fexit_bpf2bpf.c +1 -0
@@ -1,6 +1,7 @@// SPDX-License-Identifier: GPL-2.0 /* Copyright (c) 2019 Facebook */ #include <test_progs.h> +#include <network_helpers.h> static void test_fexit_bpf2bpf_common(const char *obj_file, const char *target_obj_file,
tools/testing/selftests/bpf/prog_tests/flow_dissector.c +1 -0
@@ -1,5 +1,6 @@// SPDX-License-Identifier: GPL-2.0 #include <test_progs.h> +#include <network_helpers.h> #include <error.h> #include <linux/if.h> #include <linux/if_tun.h>
tools/testing/selftests/bpf/prog_tests/flow_dissector_load_bytes.c +1 -0
@@ -1,5 +1,6 @@// SPDX-License-Identifier: GPL-2.0 #include <test_progs.h> +#include <network_helpers.h> void test_flow_dissector_load_bytes (void ) {
tools/testing/selftests/bpf/prog_tests/global_data.c +1 -0
@@ -1,5 +1,6 @@// SPDX-License-Identifier: GPL-2.0 #include <test_progs.h> +#include <network_helpers.h> static void test_global_data_number (struct bpf_object *obj,__u32 duration) {
tools/testing/selftests/bpf/prog_tests/kfree_skb.c +1 -0
@@ -1,5 +1,6 @@// SPDX-License-Identifier: GPL-2.0 #include <test_progs.h> +#include <network_helpers.h> struct meta {int ifindex;
tools/testing/selftests/bpf/prog_tests/l4lb_all.c +1 -0
@@ -1,5 +1,6 @@// SPDX-License-Identifier: GPL-2.0 #include <test_progs.h> +#include <network_helpers.h> static void test_l4lb (const char *file) {
tools/testing/selftests/bpf/prog_tests/map_lock.c +14 -0
@@ -1,5 +1,19 @@// SPDX-License-Identifier: GPL-2.0 #include <test_progs.h> +#include <network_helpers.h> + +static void *spin_lock_thread (void *arg) +{ +__u32 duration, retval; +int err, prog_fd = *(u32 *) arg; + + err =bpf_prog_test_run (prog_fd,10000 , &pkt_v4,sizeof (pkt_v4), +NULL ,NULL , &retval, &duration); +CHECK (err || retval,"" , +"err %d errno %d retval %d duration %d\n" , + err, errno, retval, duration); +pthread_exit (arg); +}static void *parallel_map_access (void *arg) {
tools/testing/selftests/bpf/prog_tests/pkt_access.c +1 -0
@@ -1,5 +1,6 @@// SPDX-License-Identifier: GPL-2.0 #include <test_progs.h> +#include <network_helpers.h> void test_pkt_access (void ) {
tools/testing/selftests/bpf/prog_tests/pkt_md_access.c +1 -0
@@ -1,5 +1,6 @@// SPDX-License-Identifier: GPL-2.0 #include <test_progs.h> +#include <network_helpers.h> void test_pkt_md_access (void ) {
tools/testing/selftests/bpf/prog_tests/prog_run_xattr.c +1 -0
@@ -1,5 +1,6 @@// SPDX-License-Identifier: GPL-2.0 #include <test_progs.h> +#include <network_helpers.h> void test_prog_run_xattr (void ) {
tools/testing/selftests/bpf/prog_tests/queue_stack_map.c +1 -0
@@ -1,5 +1,6 @@// SPDX-License-Identifier: GPL-2.0 #include <test_progs.h> +#include <network_helpers.h> enum {QUEUE ,
tools/testing/selftests/bpf/prog_tests/signal_pending.c +1 -0
@@ -1,5 +1,6 @@// SPDX-License-Identifier: GPL-2.0 #include <test_progs.h> +#include <network_helpers.h> static void sigalrm_handler (int s) {}static struct sigaction sigalrm_action = {
tools/testing/selftests/bpf/prog_tests/skb_ctx.c +1 -0
@@ -1,5 +1,6 @@// SPDX-License-Identifier: GPL-2.0 #include <test_progs.h> +#include <network_helpers.h> void test_skb_ctx (void ) {
tools/testing/selftests/bpf/prog_tests/spinlock.c +14 -0
@@ -1,5 +1,19 @@// SPDX-License-Identifier: GPL-2.0 #include <test_progs.h> +#include <network_helpers.h> + +static void *spin_lock_thread (void *arg) +{ +__u32 duration, retval; +int err, prog_fd = *(u32 *) arg; + + err =bpf_prog_test_run (prog_fd,10000 , &pkt_v4,sizeof (pkt_v4), +NULL ,NULL , &retval, &duration); +CHECK (err || retval,"" , +"err %d errno %d retval %d duration %d\n" , + err, errno, retval, duration); +pthread_exit (arg); +}void test_spinlock (void ) {
tools/testing/selftests/bpf/prog_tests/xdp.c +1 -0
@@ -1,5 +1,6 @@// SPDX-License-Identifier: GPL-2.0 #include <test_progs.h> +#include <network_helpers.h> void test_xdp (void ) {
tools/testing/selftests/bpf/prog_tests/xdp_adjust_tail.c +1 -0
@@ -1,5 +1,6 @@// SPDX-License-Identifier: GPL-2.0 #include <test_progs.h> +#include <network_helpers.h> void test_xdp_adjust_tail (void ) {
tools/testing/selftests/bpf/prog_tests/xdp_bpf2bpf.c +1 -0
@@ -1,5 +1,6 @@// SPDX-License-Identifier: GPL-2.0 #include <test_progs.h> +#include <network_helpers.h> #include <net/if.h> #include "test_xdp.skel.h" #include "test_xdp_bpf2bpf.skel.h"
tools/testing/selftests/bpf/prog_tests/xdp_noinline.c +1 -0
@@ -1,5 +1,6 @@// SPDX-License-Identifier: GPL-2.0 #include <test_progs.h> +#include <network_helpers.h> void test_xdp_noinline (void ) {
tools/testing/selftests/bpf/test_progs.c +0 -30
@@ -222,23 +222,6 @@ int test__join_cgroup(const char *path)return fd; } -struct ipv4_packet pkt_v4 = { - .eth.h_proto =__bpf_constant_htons (ETH_P_IP ), - .iph.ihl =5 , - .iph.protocol =IPPROTO_TCP , - .iph.tot_len =__bpf_constant_htons (MAGIC_BYTES ), - .tcp.urg_ptr =123 , - .tcp.doff =5 , -}; - -struct ipv6_packet pkt_v6 = { - .eth.h_proto =__bpf_constant_htons (ETH_P_IPV6 ), - .iph.nexthdr =IPPROTO_TCP , - .iph.payload_len =__bpf_constant_htons (MAGIC_BYTES ), - .tcp.urg_ptr =123 , - .tcp.doff =5 , -}; -int bpf_find_map (const char *test,struct bpf_object *obj,const char *name) {struct bpf_map *map;
@@ -358,19 +341,6 @@ int extract_build_id(char *build_id, size_t size)return -1 ; } -void *spin_lock_thread (void *arg) -{ -__u32 duration, retval; -int err, prog_fd = *(u32 *) arg; - - err =bpf_prog_test_run (prog_fd,10000 , &pkt_v4,sizeof (pkt_v4), -NULL ,NULL , &retval, &duration); -CHECK (err || retval,"" , -"err %d errno %d retval %d duration %d\n" , - err, errno, retval, duration); -pthread_exit (arg); -} -/* extern declarations for test funcs */ #define DEFINE_TEST (name) extern void test_##name(void);#include <prog_tests/tests.h>
tools/testing/selftests/bpf/test_progs.h +0 -23
@@ -87,24 +87,6 @@ extern void test__skip(void);extern void test__fail (void );extern int test__join_cgroup (const char *path); -#define MAGIC_BYTES 123 - -/* ipv4 test vector */ -struct ipv4_packet { -struct ethhdr eth; -struct iphdr iph; -struct tcphdr tcp; -} __packed; -extern struct ipv4_packet pkt_v4; - -/* ipv6 test vector */ -struct ipv6_packet { -struct ethhdr eth; -struct ipv6hdr iph; -struct tcphdr tcp; -} __packed; -extern struct ipv6_packet pkt_v6; - #define PRINT_FAIL(format...) \ ({ \ test__fail(); \
@@ -143,10 +125,6 @@ extern struct ipv6_packet pkt_v6; #define CHECK_ATTR(condition, tag, format...) \ _CHECK(condition, tag, tattr.duration, format) -#define MAGIC_VAL 0x1234 -#define NUM_ITER 100000 -#define VIP_NUM 5 -static inline __u64 ptr_to_u64 (const void *ptr) {return (__u64 ) (unsigned long ) ptr;
@@ -156,7 +134,6 @@ int bpf_find_map(const char *test, struct bpf_object *obj, const char *name);int compare_map_keys (int map1_fd,int map2_fd);int compare_stack_ips (int smap_fd,int amap_fd,int stack_trace_len);int extract_build_id (char *build_id,size_t size); -void *spin_lock_thread (void *arg);#ifdef __x86_64__#define SYS_NANOSLEEP_KPROBE_NAME "__x64_sys_nanosleep"
Signature · 1 line
2.26.2.526.g744177e7f7-goog
Re: [PATCH bpf-next v3 3/5] selftests/bpf: move existing common networking parts into network_helpers
Quoted 2 lines · Stanislav Fomichev
1. Move
pkt_v4andpkt_v6intonetwork_helpersand adjust the users.
2. Copy-pastespin_lock_threadinto two tests that use it.
Instead of copying it into two tests,
can spin_lock_thread be moved to network-helpers.c?
Re: [PATCH bpf-next v3 3/5] selftests/bpf: move existing common networking parts into network_helpers
Quoted 5 lines · Martin KaFai Lau
Quoted 2 lines · Stanislav Fomichev
1. Move
pkt_v4andpkt_v6intonetwork_helpersand adjust the users.
2. Copy-pastespin_lock_threadinto two tests that use it.Instead of copying it into two tests,
canspin_lock_threadbe moved tonetwork-helpers.c?
spin_lock_thread doesn't look like something that can be reused by other networking tests and looks very specific to prog_tests/spinlock.c and prog_tests/map_lock.c. It might be better with the copy-paste now because that thread definition is closer to the place that uses it.
I don't feel strongly about it and can put it into network_helpers. Let me know if you prefer it that way.
Re: [PATCH bpf-next v3 3/5] selftests/bpf: move existing common networking parts into network_helpers
Quoted 7 lines · On Thu, May 7, 2020 at 9:14 AM <[email protected]> wrote:
Quoted 5 lines · Martin KaFai Lau
Quoted 2 lines · Stanislav Fomichev
1. Move
pkt_v4andpkt_v6intonetwork_helpersand adjust the users.
2. Copy-pastespin_lock_threadinto two tests that use it.Instead of copying it into two tests,
canspin_lock_threadbe moved tonetwork-helpers.c?
Quoted 8 lines
spin_lock_threaddoesn't look like something that can be reused by other networking tests and looks very specific toprog_tests/spinlock.candprog_tests/map_lock.c. It might be better with the copy-paste now because that thread definition is closer to the place that uses it.I don't feel strongly about it and can put it into
network_helpers. Let me know if you prefer it that way.
I like it like this more. It has nothing to do with network and is trivial enough, I don't see a problem having it in two tests that actually use it. It actually makes it easier to follow tests.
PATCH v3 4/5 net: refactor arguments of inet{,6}_bind
In reply to Stanislav FomichevThe intent is to add an additional bind parameter in the next commit. Instead of adding another argument, let's convert all existing flag arguments into an extendable bit field.
No functional changes.
--- include/net/inet_common.h | 6 +++++- include/net/ipv6_stubs.h | 2 +- net/core/filter.c | 6 ++++-- net/ipv4/af_inet.c | 10 +++++----- net/ipv6/af_inet6.c | 10 +++++----- 5 files changed, 20 insertions(+), 14 deletions(-)
include/net/inet_common.h +5 -1
@@ -35,8 +35,12 @@ int inet_shutdown(struct socket *sock, int how);int inet_listen (struct socket *sock,int backlog);void inet_sock_destruct (struct sock *sk);int inet_bind (struct socket *sock,struct sockaddr *uaddr,int addr_len); +/* Don't allocate port at this moment, defer to connect. */ +#define BIND_FORCE_ADDRESS_NO_PORT (1 << 0) +/* Grab and release socket lock. */ +#define BIND_WITH_LOCK (1 << 1)int __inet_bind (struct sock *sk,struct sockaddr *uaddr,int addr_len, -bool force_bind_address_no_port,bool with_lock); +u32 flags);int inet_getname (struct socket *sock,struct sockaddr *uaddr,int peer);int inet_ioctl (struct socket *sock,unsigned int cmd,unsigned long arg);
include/net/ipv6_stubs.h +1 -1
@@ -63,7 +63,7 @@ extern const struct ipv6_stub *ipv6_stub __read_mostly;/* A stub used by bpf helpers. Similarly ugly as ipv6_stub */ struct ipv6_bpf_stub {int (*inet6_bind)(struct sock *sk,struct sockaddr *uaddr,int addr_len, -bool force_bind_address_no_port,bool with_lock); +u32 flags); struct sock *(*udp6_lib_lookup)(struct net *net, const struct in6_addr *saddr, __be16 sport, const struct in6_addr *daddr, __be16 dport,
net/core/filter.c +4 -2
@@ -4538,7 +4538,8 @@ BPF_CALL_3(bpf_bind, struct bpf_sock_addr_kern *, ctx, struct sockaddr *, addr,return err;if (((struct sockaddr_in *)addr)->sin_port !=htons (0 ))return err; -return __inet_bind (sk, addr, addr_len, true, false); +return __inet_bind (sk, addr, addr_len, +BIND_FORCE_ADDRESS_NO_PORT );#if IS_ENABLED (CONFIG_IPV6 ) } else if (addr->sa_family == AF_INET6) {if (addr_len <SIN6_LEN_RFC2133 )
@@ -4548,7 +4549,8 @@ BPF_CALL_3(bpf_bind, struct bpf_sock_addr_kern *, ctx, struct sockaddr *, addr,/* ipv6_bpf_stub cannot be NULL, since it's called from * bpf_cgroup_inet6_connect hook and ipv6 is already loaded */ -return ipv6_bpf_stub->inet6_bind (sk, addr, addr_len, true, false); +return ipv6_bpf_stub->inet6_bind (sk, addr, addr_len, +BIND_FORCE_ADDRESS_NO_PORT ); #endif /* CONFIG_IPV6 */ }#endif /* CONFIG_INET */
net/ipv4/af_inet.c +5 -5
@@ -450,12 +450,12 @@ int inet_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len)if (err)return err; -return __inet_bind (sk, uaddr, addr_len, false, true); +return __inet_bind (sk, uaddr, addr_len,BIND_WITH_LOCK ); }EXPORT_SYMBOL (inet_bind);int __inet_bind (struct sock *sk,struct sockaddr *uaddr,int addr_len, -bool force_bind_address_no_port,bool with_lock) +u32 flags) {struct sockaddr_in *addr = (struct sockaddr_in *)uaddr;struct inet_sock *inet =inet_sk (sk);
@@ -506,7 +506,7 @@ int __inet_bind(struct sock *sk, struct sockaddr *uaddr, int addr_len,* would be illegal to use them (multicast/broadcast) in * which case the sending device address is used. */ -if (with_lock) +if (flags &BIND_WITH_LOCK )lock_sock (sk);/* Check these errors (active socket, double bind). */
@@ -520,7 +520,7 @@ int __inet_bind(struct sock *sk, struct sockaddr *uaddr, int addr_len,/* Make sure we are allowed to bind here. */ if (snum || !(inet->bind_address_no_port || - force_bind_address_no_port)) { + (flags &BIND_FORCE_ADDRESS_NO_PORT ))) {if (sk->sk_prot->get_port (sk, snum)) { inet->inet_saddr = inet->inet_rcv_saddr =0 ; err = -EADDRINUSE ;
@@ -543,7 +543,7 @@ int __inet_bind(struct sock *sk, struct sockaddr *uaddr, int addr_len,sk_dst_reset (sk); err =0 ; out_release_sock: -if (with_lock) +if (flags &BIND_WITH_LOCK )release_sock (sk); out:return err;
net/ipv6/af_inet6.c +5 -5
@@ -273,7 +273,7 @@ static int inet6_create(struct net *net, struct socket *sock, int protocol, }static int __inet6_bind (struct sock *sk,struct sockaddr *uaddr,int addr_len, -bool force_bind_address_no_port,bool with_lock) +u32 flags) {struct sockaddr_in6 *addr = (struct sockaddr_in6 *)uaddr;struct inet_sock *inet =inet_sk (sk);
@@ -297,7 +297,7 @@ static int __inet6_bind(struct sock *sk, struct sockaddr *uaddr, int addr_len, !ns_capable(net->user_ns, CAP_NET_BIND_SERVICE))return -EACCES ; -if (with_lock) +if (flags &BIND_WITH_LOCK )lock_sock (sk);/* Check these errors (active socket, double bind). */
@@ -400,7 +400,7 @@ static int __inet6_bind(struct sock *sk, struct sockaddr *uaddr, int addr_len,/* Make sure we are allowed to bind here. */ if (snum || !(inet->bind_address_no_port || - force_bind_address_no_port)) { + (flags &BIND_FORCE_ADDRESS_NO_PORT ))) {if (sk->sk_prot->get_port (sk, snum)) { sk->sk_ipv6only = saved_ipv6only;inet_reset_saddr (sk);
@@ -423,7 +423,7 @@ static int __inet6_bind(struct sock *sk, struct sockaddr *uaddr, int addr_len, inet->inet_dport =0 ; inet->inet_daddr =0 ; out: -if (with_lock) +if (flags &BIND_WITH_LOCK )release_sock (sk);return err; out_unlock:
@@ -451,7 +451,7 @@ int inet6_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len)if (err)return err; -return __inet6_bind (sk, uaddr, addr_len, false, true); +return __inet6_bind (sk, uaddr, addr_len,BIND_WITH_LOCK ); }EXPORT_SYMBOL (inet6_bind);
Signature · 1 line
2.26.2.526.g744177e7f7-goog
Re: [PATCH bpf-next v3 4/5] net: refactor arguments of inet{,6}_bind
Quoted 9 lines · Stanislav Fomichev
The intent is to add an additional bind parameter in the next commit. Instead of adding another argument, let's convert all existing flag arguments into an extendable bit field.
No functional changes.
Cc Andrey Ignatov <[email protected]>Cc Martin KaFai Lau <[email protected]>Signed-off-by Stanislav Fomichev <[email protected]>
PATCH v3 5/5 bpf: allow any port in bpf_bind helper
In reply to Stanislav FomichevWe want to have a tighter control on what ports we bind to in the BPF_CGROUP_INET{4,6}_CONNECT hooks even if it means connect() becomes slightly more expensive. The expensive part comes from the fact that we now need to call inet_csk_get_port() that verifies that the port is not used and allocates an entry in the hash table for it.
Since we can't rely on "snum || !bind_address_no_port" to prevent us from calling POST_BIND hook anymore, let's add another bind flag to indicate that the call site is BPF program.
v3:
* More bpf_bind documentation refinements (Martin KaFai Lau)
* Add UDP tests as well (Martin KaFai Lau)
* Don't start the thread, just do socket+bind+listen (Martin KaFai Lau)
v2:
* Update documentation (Andrey Ignatov)
* Pass BIND_FORCE_ADDRESS_NO_PORT conditionally (Andrey Ignatov)
--- include/net/inet_common.h | 2 + include/uapi/linux/bpf.h | 9 +- net/core/filter.c | 18 ++- net/ipv4/af_inet.c | 10 +- net/ipv6/af_inet6.c | 12 +- tools/include/uapi/linux/bpf.h | 9 +- .../bpf/prog_tests/connect_force_port.c | 115 ++++++++++++++++++ .../selftests/bpf/progs/connect_force_port4.c | 28 +++++ .../selftests/bpf/progs/connect_force_port6.c | 28 +++++ 9 files changed, 203 insertions(+), 28 deletions(-) create mode 100644 tools/testing/selftests/bpf/prog_tests/connect_force_port.c create mode 100644 tools/testing/selftests/bpf/progs/connect_force_port4.c create mode 100644 tools/testing/selftests/bpf/progs/connect_force_port6.c
include/net/inet_common.h +2 -0
@@ -39,6 +39,8 @@ int inet_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len);#define BIND_FORCE_ADDRESS_NO_PORT (1 << 0)/* Grab and release socket lock. */ #define BIND_WITH_LOCK (1 << 1) +/* Called from BPF program. */ +#define BIND_FROM_BPF (1 << 2)int __inet_bind (struct sock *sk,struct sockaddr *uaddr,int addr_len,u32 flags); int inet_getname(struct socket *sock, struct sockaddr *uaddr,
include/uapi/linux/bpf.h +5 -4
@@ -1994,10 +1994,11 @@ union bpf_attr {* * This helper works for IPv4 and IPv6, TCP and UDP sockets. The * domain (*addr*\ **->sa_family**) must be **AF_INET** (or -* **AF_INET6**). Looking for a free port to bind to can be -* expensive, therefore binding to port is not permitted by the -* helper: *addr*\ **->sin_port** (or **sin6_port**, respectively) -* must be set to zero. +* **AF_INET6**). It's advised to pass zero port (**sin_port** +* or **sin6_port**) which triggers IP_BIND_ADDRESS_NO_PORT-like +* behavior and lets the kernel efficiently pick up an unused +* port as long as 4-tuple is unique. Passing non-zero port might +* lead to degraded performance. * Return * 0 on success, or a negative error in case of failure. *
net/core/filter.c +7 -11
@@ -4525,32 +4525,28 @@ BPF_CALL_3(bpf_bind, struct bpf_sock_addr_kern *, ctx, struct sockaddr *, addr, {#ifdef CONFIG_INET struct sock *sk = ctx->sk; +u32 flags =BIND_FROM_BPF ;int err; -/* Binding to port can be expensive so it's prohibited in the helper. -* Only binding to IP is supported. -*/ err = -EINVAL ; if (addr_len < offsetofend(struct sockaddr, sa_family))return err;if (addr->sa_family ==AF_INET ) {if (addr_len <sizeof (struct sockaddr_in ))return err; -if (((struct sockaddr_in *)addr)->sin_port !=htons (0 )) -return err; -return __inet_bind (sk, addr, addr_len, -BIND_FORCE_ADDRESS_NO_PORT ); +if (((struct sockaddr_in *)addr)->sin_port ==htons (0 )) + flags |=BIND_FORCE_ADDRESS_NO_PORT ; +return __inet_bind (sk, addr, addr_len, flags);#if IS_ENABLED (CONFIG_IPV6 ) } else if (addr->sa_family == AF_INET6) {if (addr_len <SIN6_LEN_RFC2133 )return err; -if (((struct sockaddr_in6 *)addr)->sin6_port !=htons (0 )) -return err; +if (((struct sockaddr_in6 *)addr)->sin6_port ==htons (0 )) + flags |=BIND_FORCE_ADDRESS_NO_PORT ;/* ipv6_bpf_stub cannot be NULL, since it's called from * bpf_cgroup_inet6_connect hook and ipv6 is already loaded */ -return ipv6_bpf_stub->inet6_bind (sk, addr, addr_len, -BIND_FORCE_ADDRESS_NO_PORT ); +return ipv6_bpf_stub->inet6_bind (sk, addr, addr_len, flags);#endif /* CONFIG_IPV6 */ }#endif /* CONFIG_INET */
net/ipv4/af_inet.c +6 -4
@@ -526,10 +526,12 @@ int __inet_bind(struct sock *sk, struct sockaddr *uaddr, int addr_len, err = -EADDRINUSE ; goto out_release_sock; } - err =BPF_CGROUP_RUN_PROG_INET4_POST_BIND (sk); -if (err) { - inet->inet_saddr = inet->inet_rcv_saddr =0 ; - goto out_release_sock; +if (!(flags &BIND_FROM_BPF )) { + err =BPF_CGROUP_RUN_PROG_INET4_POST_BIND (sk); +if (err) { + inet->inet_saddr = inet->inet_rcv_saddr =0 ; + goto out_release_sock; + } } }
net/ipv6/af_inet6.c +7 -5
@@ -407,11 +407,13 @@ static int __inet6_bind(struct sock *sk, struct sockaddr *uaddr, int addr_len, err = -EADDRINUSE ; goto out; } - err =BPF_CGROUP_RUN_PROG_INET6_POST_BIND (sk); -if (err) { - sk->sk_ipv6only = saved_ipv6only; -inet_reset_saddr (sk); - goto out; +if (!(flags &BIND_FROM_BPF )) { + err =BPF_CGROUP_RUN_PROG_INET6_POST_BIND (sk); +if (err) { + sk->sk_ipv6only = saved_ipv6only; +inet_reset_saddr (sk); + goto out; + } } }
tools/include/uapi/linux/bpf.h +5 -4
@@ -1994,10 +1994,11 @@ union bpf_attr {* * This helper works for IPv4 and IPv6, TCP and UDP sockets. The * domain (*addr*\ **->sa_family**) must be **AF_INET** (or -* **AF_INET6**). Looking for a free port to bind to can be -* expensive, therefore binding to port is not permitted by the -* helper: *addr*\ **->sin_port** (or **sin6_port**, respectively) -* must be set to zero. +* **AF_INET6**). It's advised to pass zero port (**sin_port** +* or **sin6_port**) which triggers IP_BIND_ADDRESS_NO_PORT-like +* behavior and lets the kernel efficiently pick up an unused +* port as long as 4-tuple is unique. Passing non-zero port might +* lead to degraded performance. * Return * 0 on success, or a negative error in case of failure. *
tools/testing/selftests/bpf/prog_tests/connect_force_port.c new file +115 -0
115 lines not on this page · raw message
tools/testing/selftests/bpf/progs/connect_force_port4.c new file +28 -0
@@ -0,0 +1,28 @@ +// SPDX-License-Identifier: GPL-2.0 +#include <string.h> + +#include <linux/bpf.h> +#include <linux/in.h> +#include <linux/in6.h> +#include <sys/socket.h> + +#include <bpf/bpf_helpers.h> +#include <bpf/bpf_endian.h> + +char _license[] SEC("license") = "GPL"; +int _version SEC("version") = 1; + +SEC("cgroup/connect4") +int _connect4 (struct bpf_sock_addr *ctx) +{ +struct sockaddr_in sa = {}; + + sa.sin_family =AF_INET ; + sa.sin_port =bpf_htons (22222 ); + sa.sin_addr.s_addr =bpf_htonl (0x7f000001 );/* 127.0.0.1 */ + +if (bpf_bind (ctx, (struct sockaddr *)&sa,sizeof (sa)) !=0 ) +return 0 ; + +return 1 ; +}
tools/testing/selftests/bpf/progs/connect_force_port6.c new file +28 -0
@@ -0,0 +1,28 @@ +// SPDX-License-Identifier: GPL-2.0 +#include <string.h> + +#include <linux/bpf.h> +#include <linux/in.h> +#include <linux/in6.h> +#include <sys/socket.h> + +#include <bpf/bpf_helpers.h> +#include <bpf/bpf_endian.h> + +char _license[] SEC("license") = "GPL"; +int _version SEC("version") = 1; + +SEC("cgroup/connect6") +int _connect6 (struct bpf_sock_addr *ctx) +{ +struct sockaddr_in6 sa = {}; + + sa.sin6_family =AF_INET ; + sa.sin6_port =bpf_htons (22223 ); + sa.sin6_addr.s6_addr32[3 ] =bpf_htonl (1 );/* ::1 */ + +if (bpf_bind (ctx, (struct sockaddr *)&sa,sizeof (sa)) !=0 ) +return 0 ; + +return 1 ; +}
Signature · 1 line
2.26.2.526.g744177e7f7-goog
Quoted 16 lines · Stanislav Fomichev
We want to have a tighter control on what ports we bind to in the BPF_CGROUP_INET{4,6}_CONNECT hooks even if it means
connect()becomes slightly more expensive. The expensive part comes from the fact that we now need to callinet_csk_get_port()that verifies that the port is not used and allocates an entry in the hash table for it.Since we can't rely on "snum || !bind_address_no_port" to prevent us from calling
POST_BINDhook anymore, let's add another bind flag to indicate that the call site is BPF program.v3:
* Morebpf_binddocumentation refinements (Martin KaFai Lau)
* Add UDP tests as well (Martin KaFai Lau)
* Don't start the thread, just do socket+bind+listen (Martin KaFai Lau)
Quoted 7 lines
v2:
* Update documentation (Andrey Ignatov)
* PassBIND_FORCE_ADDRESS_NO_PORTconditionally (Andrey Ignatov)Cc Andrey Ignatov <[email protected]>Cc Martin KaFai Lau <[email protected]>Signed-off-by Stanislav Fomichev <[email protected]>