bpf: allow any port in bpf_bind helper
Subsystems (8)
v2 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. do small refactoring of __inet{,6}_bind() flags to make it easy to extend them with the additional flags
4. remove the restriction on port being zero in bpf_bind() helper; add new bind flag to prevent POST_BIND hook from being called
5. selftests: move some common functionality into network_helpers
Stanislav Fomichev (5):
selftests/bpf: generalize helpers to control background listener selftests/bpf: adopt accept_timeout from sockmap_listen
net: refactor arguments of inet{,6}_bind
bpf: allow any port in bpf_bind helper
selftests/bpf: move existing common networking parts into
network_helpers
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 | 44 ++++ .../bpf/prog_tests/connect_force_port.c | 104 +++++++++ .../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, 510 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 v2 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.
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 | 11 ++ .../selftests/bpf/prog_tests/tcp_rtt.c | 116 +------------ 4 files changed, 180 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 +11 -0
@@ -0,0 +1,11 @@ +/* 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_thread (int family); +void stop_server_thread (int fd); +int connect_to_fd (int family,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 v2 1/5] selftests/bpf: generalize helpers to control background listener
Quoted 7 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.
The refactoring itself looks fine.
If I read it correctly, it is a simple connect() test.
I am not sure a thread is even needed. accept() is also unnecessary. Can all be done in one thread?
Re: [PATCH bpf-next v2 1/5] selftests/bpf: generalize helpers to control background listener
Quoted 7 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 fd
Quoted 6 lines
Quoted 1 line
These will be used in the next commit.
The refactoring itself looks fine.
If I read it correctly, it is a simple
connect()test.
I am not sure a thread is even needed.accept()is also unnecessary. Can all be done in one thread?
I'm looking at the socket address after connection is established (to verify that the port is the one we were supposed to be using), so I fail to understand how accept() is unnecessary. Care to clarify?
I thought about doing a "listen() > non-blocking connect() > accept()" in a single thread instead of background thread, but then decided that it's better to reuse existing helpers and do proper connection instead of writing all this new code.
Re: [PATCH bpf-next v2 1/5] selftests/bpf: generalize helpers to control background listener
Quoted 8 lines · On Wed, May 06, 2020 at 09:28:02AM -0700, [email protected] wrote:
Quoted 7 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 fd
Quoted 7 lines
Quoted 6 lines
Quoted 1 line
These will be used in the next commit.
The refactoring itself looks fine.
If I read it correctly, it is a simple
connect()test.
I am not sure a thread is even needed.accept()is also unnecessary. Can all be done in one thread?I'm looking at the socket address after connection is established (to
If I read it correctly, it is checking the local address (getsockname()) of the client's connect-ed() fd instead of the server's accept-ed() fd.
Quoted 4 lines
verify that the port is the one we were supposed to be using), so I fail to understand how
accept()is unnecessary. Care to clarify?I thought about doing a "
listen()> non-blockingconnect()>accept()"
It should not need non-blocking connect().
The client connect() (3WHS) can still finish before the server side accept() is called. If the test does not need the accept-ed() fd, then calling it or not is optional.
Just took a quick look, sk_assign.c and test_sock_addr.c could be good examples. They use SO_RCVTIMEO/SO_SNDTIMEO for timeout also.
Quoted 3 lines
in a single thread instead of background thread, but then decided that it's better to reuse existing helpers and do proper connection instead of writing all this new code.
Re: [PATCH bpf-next v2 1/5] selftests/bpf: generalize helpers to control background listener
Quoted 19 lines · Martin KaFai Lau
Quoted 8 lines · On Wed, May 06, 2020 at 09:28:02AM -0700, [email protected] wrote:
Quoted 7 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
Quoted 6 lines
Quoted 1 line
These will be used in the next commit.
The refactoring itself looks fine.
If I read it correctly, it is a simple
connect()test.
I am not sure a thread is even needed.accept()is also unnecessary. Can all be done in one thread?I'm looking at the socket address after connection is established (to
If I read it correctly, it is checking the local address (
getsockname()) of the client's connect-ed() fd instead of the server's accept-ed() fd.
Quoted 8 lines
Quoted 4 lines
verify that the port is the one we were supposed to be using), so I fail to understand how
accept()is unnecessary. Care to clarify?I thought about doing a "
listen()> non-blockingconnect()>accept()"It should not need non-blocking
connect().
The clientconnect()(3WHS) can still finish before the server sideaccept()is called. If the test does not need the accept-ed() fd, then calling it or not is optional.
Ah, I see what you're saying, in this case I can expose extra helper from network_helpers to do only socket+bind part for the server. Thanks for the explanation!
Quoted 6 lines
Just took a quick look,
sk_assign.candtest_sock_addr.ccould be good examples. They use SO_RCVTIMEO/SO_SNDTIMEO for timeout also.Quoted 3 lines
in a single thread instead of background thread, but then decided that it's better to reuse existing helpers and do proper connection instead of writing all this new code.
PATCH v2 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)return ERR_PTR (err); } -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 (err);
@@ -162,3 +155,37 @@ int connect_to_fd(int family, 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
@@ -7,5 +7,9 @@int start_server_thread (int family);void stop_server_thread (int fd);int connect_to_fd (int family,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 v2 3/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
PATCH v2 4/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.
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 | 104 ++++++++++++++++++ .../selftests/bpf/progs/connect_force_port4.c | 28 +++++ .../selftests/bpf/progs/connect_force_port6.c | 28 +++++ 9 files changed, 192 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 reuse the same source 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 reuse the same source 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 +104 -0
104 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
Stanislav Fomichev <[email protected]> [Tue, 2020-05-05 13:27 -0700]:
Quoted 11 lines
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.
Quoted 6 lines
v2:
* Update documentation (Andrey Ignatov)
* PassBIND_FORCE_ADDRESS_NO_PORTconditionally (Andrey Ignatov)Cc Andrey Ignatov <[email protected]>Signed-off-by Stanislav Fomichev <[email protected]>
Thanks. LGMT.
Signature · 1 line
Andrey Ignatov
Quoted 50 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.v2:
* Update documentation (Andrey Ignatov)
* PassBIND_FORCE_ADDRESS_NO_PORTconditionally (Andrey Ignatov)Cc Andrey Ignatov <[email protected]>Signed-off-by Stanislav Fomichev <[email protected]>--- 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 | 104 ++++++++++++++++++ .../selftests/bpf/progs/connect_force_port4.c | 28 +++++ .../selftests/bpf/progs/connect_force_port6.c | 28 +++++ 9 files changed, 192 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.cinclude/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 +0
-0
include/uapi/linux/bpf.h union bpf_attr { · line 1999
Quoted 10 lines
include/uapi/linux/bpf.h +3
-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 reuse the same source port
Reading "zero port" and "the same source port" together is confusing.
include/uapi/linux/bpf.h union bpf_attr { · line 2001
Quoted 2 lines
include/uapi/linux/bpf.h +2
-0+* as long as 4-tuple is unique. Passing non-zero port might +* lead to degraded performance.
Is the "degraded performance" also true for UDP?
Quoted 3 lines
include/uapi/linux/bpf.h +0
-0* Return * 0 on success, or a negative error in case of failure. *
[ ... ]
Quoted 96 lines
tools/testing/selftests/bpf/prog_tests/connect_force_port.c new file +90
-0@@ -0,0 +1,104 @@ +// SPDX-License-Identifier: GPL-2.0 + +#include <test_progs.h> +#include "cgroup_helpers.h" +#include "network_helpers.h" + +static int verify_port (int family,int fd,int expected) +{ +struct sockaddr_storage addr; +socklen_t len =sizeof (addr); +__u16 port; + + +if (getsockname (fd, (struct sockaddr *)&addr, &len)) { +log_err ("Failed to get server addr" ); +return -1 ; + } + +if (family ==AF_INET ) + port = ((struct sockaddr_in *)&addr)->sin_port; +else + port = ((struct sockaddr_in6 *)&addr)->sin6_port; + +if (ntohs (port) != expected) { +log_err ("Unexpected port %d, expected %d" ,ntohs (port), + expected); +return -1 ; + } + +return 0 ; +} + +static int run_test (int cgroup_fd,int server_fd,int family) +{ +struct bpf_prog_load_attr attr = { + .prog_type =BPF_PROG_TYPE_CGROUP_SOCK_ADDR , + }; +struct bpf_object *obj; +int expected_port; +int prog_fd; +int err; +int fd; + +if (family ==AF_INET ) { + attr.file ="./connect_force_port4.o" ; + attr.expected_attach_type =BPF_CGROUP_INET4_CONNECT ; + expected_port =22222 ; + }else { + attr.file ="./connect_force_port6.o" ; + attr.expected_attach_type =BPF_CGROUP_INET6_CONNECT ; + expected_port =22223 ; + } + + err =bpf_prog_load_xattr (&attr, &obj, &prog_fd); +if (err) { +log_err ("Failed to load BPF object" ); +return -1 ; + } + + err =bpf_prog_attach (prog_fd, cgroup_fd, attr.expected_attach_type, +0 ); +if (err) { +log_err ("Failed to attach BPF program" ); + goto close_bpf_object; + } + + fd =connect_to_fd (family, server_fd); +if (fd <0 ) { + err =-1 ; + goto close_bpf_object; + } + + err =verify_port (family, fd, expected_port); + +close (fd); + +close_bpf_object: +bpf_object__close (obj); +return err; +} + +void test_connect_force_port (void ) +{ +int server_fd, cgroup_fd; + + cgroup_fd =test__join_cgroup ("/connect_force_port" ); +if (CHECK_FAIL (cgroup_fd <0 )) +return ; + + server_fd =start_server_thread (AF_INET );
tools/testing/selftests/bpf/prog_tests/connect_force_port.c line 100
Quoted 10 lines
tools/testing/selftests/bpf/prog_tests/connect_force_port.c new file +10
-0+if (CHECK_FAIL (server_fd <0 )) + goto close_cgroup_fd; +CHECK_FAIL (run_test (cgroup_fd, server_fd,AF_INET )); +stop_server_thread (server_fd); + + server_fd =start_server_thread (AF_INET6 ); +if (CHECK_FAIL (server_fd <0 )) + goto close_cgroup_fd; +CHECK_FAIL (run_test (cgroup_fd, server_fd,AF_INET6 )); +stop_server_thread (server_fd);
Thanks for testing both v6 and v4.
The UDP path should be tested also.
Quoted 58 lines · Martin KaFai Lau
Quoted 17 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.v2:
* Update documentation (Andrey Ignatov)
* PassBIND_FORCE_ADDRESS_NO_PORTconditionally (Andrey Ignatov)Cc Andrey Ignatov <[email protected]>Signed-off-by Stanislav Fomichev <[email protected]>Quoted 12 lines
--- 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 | 104 ++++++++++++++++++ .../selftests/bpf/progs/connect_force_port4.c | 28 +++++ .../selftests/bpf/progs/connect_force_port6.c | 28 +++++ 9 files changed, 192 insertions(+), 28 deletions(-) create mode 100644
tools/testing/selftests/bpf/prog_tests/connect_force_port.cQuoted 1 line
create mode 100644
tools/testing/selftests/bpf/progs/connect_force_port4.cQuoted 1 line
create mode 100644
tools/testing/selftests/bpf/progs/connect_force_port6.cinclude/net/inet_common.h int inet_bind(struct socket *sock, struct sockaddr
Quoted 6 lines
include/net/inet_common.h +0
-0@@ -39,6 +39,8 @@ int inet_bind(struct socket *sock, struct sockaddr*uaddr, int
addr_len);Quoted 16 lines
include/net/inet_common.h +2
-0#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 +0
-0@@ -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
Quoted 8 lines
include/uapi/linux/bpf.h union bpf_attr { · line 1999
Quoted 7 lines
include/uapi/linux/bpf.h +3
-4-* **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 reuse the same source port Reading "zero port" and "the same source port" together is confusing.
Ack, let me try rephrase it a bit to make it more clear.
Quoted 3 lines
include/uapi/linux/bpf.h union bpf_attr { · line 2001
Quoted 2 lines
include/uapi/linux/bpf.h +2
-0+* as long as 4-tuple is unique. Passing non-zero port might +* lead to degraded performance. Is the "degraded performance" also true for UDP?
I suppose everything that is "allocating" port at bind time can lead to a faster port exhaustion, so UDP should be also affected. Although, looking at udp_v4_get_port, it looks less involved than its TCP counterpart.
Quoted 115 lines
Quoted 3 lines
include/uapi/linux/bpf.h +0
-0* Return * 0 on success, or a negative error in case of failure. * [ ... ]
Review
Quoted 1 line
(quoted diff) +0
-0
a/tools/testing/selftests/bpf/prog_tests/connect_force_port.cb/tools/testing/selftests/bpf/prog_tests/connect_force_port.cQuoted 95 lines
new file mode 100644
index 000000000000..97104e6410b6tools/testing/selftests/bpf/prog_tests/connect_force_port.c new file +90
-0@@ -0,0 +1,104 @@ +// SPDX-License-Identifier: GPL-2.0 + +#include <test_progs.h> +#include "cgroup_helpers.h" +#include "network_helpers.h" + +static int verify_port (int family,int fd,int expected) +{ +struct sockaddr_storage addr; +socklen_t len =sizeof (addr); +__u16 port; + + +if (getsockname (fd, (struct sockaddr *)&addr, &len)) { +log_err ("Failed to get server addr" ); +return -1 ; + } + +if (family ==AF_INET ) + port = ((struct sockaddr_in *)&addr)->sin_port; +else + port = ((struct sockaddr_in6 *)&addr)->sin6_port; + +if (ntohs (port) != expected) { +log_err ("Unexpected port %d, expected %d" ,ntohs (port), + expected); +return -1 ; + } + +return 0 ; +} + +static int run_test (int cgroup_fd,int server_fd,int family) +{ +struct bpf_prog_load_attr attr = { + .prog_type =BPF_PROG_TYPE_CGROUP_SOCK_ADDR , + }; +struct bpf_object *obj; +int expected_port; +int prog_fd; +int err; +int fd; + +if (family ==AF_INET ) { + attr.file ="./connect_force_port4.o" ; + attr.expected_attach_type =BPF_CGROUP_INET4_CONNECT ; + expected_port =22222 ; + }else { + attr.file ="./connect_force_port6.o" ; + attr.expected_attach_type =BPF_CGROUP_INET6_CONNECT ; + expected_port =22223 ; + } + + err =bpf_prog_load_xattr (&attr, &obj, &prog_fd); +if (err) { +log_err ("Failed to load BPF object" ); +return -1 ; + } + + err =bpf_prog_attach (prog_fd, cgroup_fd, attr.expected_attach_type, +0 ); +if (err) { +log_err ("Failed to attach BPF program" ); + goto close_bpf_object; + } + + fd =connect_to_fd (family, server_fd); +if (fd <0 ) { + err =-1 ; + goto close_bpf_object; + } + + err =verify_port (family, fd, expected_port); + +close (fd); + +close_bpf_object: +bpf_object__close (obj); +return err; +} + +void test_connect_force_port (void ) +{ +int server_fd, cgroup_fd; + + cgroup_fd =test__join_cgroup ("/connect_force_port" ); +if (CHECK_FAIL (cgroup_fd <0 )) +return ; + + server_fd =start_server_thread (AF_INET );tools/testing/selftests/bpf/prog_tests/connect_force_port.c line 100
Quoted 10 lines
tools/testing/selftests/bpf/prog_tests/connect_force_port.c new file +10
-0+if (CHECK_FAIL (server_fd <0 )) + goto close_cgroup_fd; +CHECK_FAIL (run_test (cgroup_fd, server_fd,AF_INET )); +stop_server_thread (server_fd); + + server_fd =start_server_thread (AF_INET6 ); +if (CHECK_FAIL (server_fd <0 )) + goto close_cgroup_fd; +CHECK_FAIL (run_test (cgroup_fd, server_fd,AF_INET6 )); +stop_server_thread (server_fd);Thanks for testing both v6 and v4.
Quoted 1 line
The UDP path should be tested also.
Good point, will do!
Quoted 69 lines · On Wed, May 06, 2020 at 09:22:45AM -0700, [email protected] wrote:
Quoted 58 lines · Martin KaFai Lau
Quoted 17 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.v2:
* Update documentation (Andrey Ignatov)
* PassBIND_FORCE_ADDRESS_NO_PORTconditionally (Andrey Ignatov)Cc Andrey Ignatov <[email protected]>Signed-off-by Stanislav Fomichev <[email protected]>Quoted 12 lines
--- 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 | 104 ++++++++++++++++++ .../selftests/bpf/progs/connect_force_port4.c | 28 +++++ .../selftests/bpf/progs/connect_force_port6.c | 28 +++++ 9 files changed, 192 insertions(+), 28 deletions(-) create mode 100644
tools/testing/selftests/bpf/prog_tests/connect_force_port.cQuoted 1 line
create mode 100644
tools/testing/selftests/bpf/progs/connect_force_port4.cQuoted 1 line
create mode 100644
tools/testing/selftests/bpf/progs/connect_force_port6.cinclude/net/inet_common.h int inet_bind(struct socket *sock, struct sockaddr
Quoted 6 lines
include/net/inet_common.h +0
-0@@ -39,6 +39,8 @@ int inet_bind(struct socket *sock, struct sockaddr*uaddr, int
addr_len);Quoted 16 lines
include/net/inet_common.h +2
-0#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 +0
-0@@ -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 Quoted 8 lines
include/uapi/linux/bpf.h union bpf_attr { · line 1999
Quoted 7 lines
include/uapi/linux/bpf.h +3
-4-* **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 reuse the same source port Reading "zero port" and "the same source port" together is confusing.
Ack, let me try rephrase it a bit to make it more clear.
Quoted 7 lines
Quoted 3 lines
include/uapi/linux/bpf.h union bpf_attr { · line 2001
Quoted 2 lines
include/uapi/linux/bpf.h +2
-0+* as long as 4-tuple is unique. Passing non-zero port might +* lead to degraded performance. Is the "degraded performance" also true for UDP?
I suppose everything that is "allocating" port at bind time can lead to a faster port exhaustion, so UDP should be also affected. Although, looking at
udp_v4_get_port, it looks less involved than its TCP counterpart.
I was mostly curious after taking a quick look in both TCP and UDP. It does not affect the patch here. May be something for optimization later.
From skimming the codes,
I was thinking UDP should be more or less the same since they have to go through get_port() eventually.
TCP will be degraded from inet_hash_connect() [which seems to be using the ehash and 4 tuples] to get_port().
PATCH v2 5/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 , +}; +static int start_server (int family) {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_thread (int family);void stop_server_thread (int fd);
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