bpf: sockmap, sockhash: support storing UDP sockets
Subsystems (10)
v2 cover bpf: sockmap, sockhash: support storing UDP sockets
Thanks for all the reviews so far! I've fixed the identified bug and addressed feedback as much as possible.
I've not taken up Jakub's suggestion to get rid of sk_psock_hooks_init. My intention is to encapsulate initializing both v4 and v6 protos. Really, it's down to personal preference, and I'm happy to remove it if others prefer Jakub's approach. I'm also still eager for a solution that requires less machinery.
Changes since v1:
- Check newsk->sk_prot in tcp_bpf_clone
- Fix compilation with BPF_STREAM_PARSER disabled
- Use spin_lock_init instead of static initializer
- Elaborate on TCPF_SYN_RECV
- Cosmetic changes to TEST macros, and more tests
- Add Jakub and me as maintainers
Lorenz Bauer (9):
bpf: sockmap: only check ULP for TCP sockets
bpf: tcp: guard declarations with CONFIG_NET_SOCK_MSG
bpf: sockmap: move generic sockmap hooks from BPF TCP
skmsg: introduce sk_psock_hooks
bpf: sockmap: allow UDP sockets
selftests: bpf: don't listen() on UDP sockets
selftests: bpf: add tests for UDP sockets in sockmap
selftests: bpf: enable UDP sockmap reuseport tests bpf, doc: update maintainers for L7 BPF
MAINTAINERS | 3 + include/linux/bpf.h | 4 +- include/linux/skmsg.h | 72 +++---- include/linux/udp.h | 4 + include/net/tcp.h | 18 +- net/core/skmsg.c | 55 +++++ net/core/sock_map.c | 160 ++++++++++---- net/ipv4/Makefile | 1 + net/ipv4/tcp_bpf.c | 169 +++------------ net/ipv4/udp_bpf.c | 53 +++++ .../bpf/prog_tests/select_reuseport.c | 6 - .../selftests/bpf/prog_tests/sockmap_listen.c | 204 +++++++++++++----- 12 files changed, 465 insertions(+), 284 deletions(-) create mode 100644 net/ipv4/udp_bpf.c
Signature · 1 line
2.20.1
PATCH v2 1/9 bpf: sockmap: only check ULP for TCP sockets
The sock map code checks that a socket does not have an active upper layer protocol before inserting it into the map. This requires casting via inet_csk, which isn't valid for UDP sockets.
Guard checks for ULP by checking inet_sk(sk)->is_icsk first.
--- include/linux/skmsg.h | 8 +++++++- net/core/sock_map.c | 11 +++++++---- 2 files changed, 14 insertions(+), 5 deletions(-)
include/linux/skmsg.h +7 -1
@@ -360,7 +360,13 @@ static inline void sk_psock_restore_proto(struct sock *sk, struct sk_psock *psock) { sk->sk_prot->unhash = psock->saved_unhash; -tcp_update_ulp (sk, psock->sk_proto, psock->saved_write_space); +if (inet_sk (sk)->is_icsk) { +tcp_update_ulp (sk, psock->sk_proto, psock->saved_write_space); + }else { + sk->sk_write_space = psock->saved_write_space; +/* Pairs with lockless read in sk_clone_lock() */ +WRITE_ONCE (sk->sk_prot, psock->sk_proto); + } } static inline void sk_psock_set_state(struct sk_psock *psock,
net/core/sock_map.c +7 -4
@@ -94,6 +94,11 @@ static void sock_map_sk_release(struct sock *sk)release_sock (sk); } +static bool sock_map_sk_has_ulp (struct sock *sk) +{ +return inet_sk (sk)->is_icsk && !!inet_csk (sk)->icsk_ulp_ops; +} +static void sock_map_add_link (struct sk_psock *psock,struct sk_psock_link *link,struct bpf_map *map,void *link_raw)
@@ -384,7 +389,6 @@ static int sock_map_update_common(struct bpf_map *map, u32 idx, struct sock *sk, u64 flags) { struct bpf_stab *stab = container_of(map, struct bpf_stab, map); -struct inet_connection_sock *icsk =inet_csk (sk);struct sk_psock_link *link;struct sk_psock *psock;struct sock *osk;
@@ -395,7 +399,7 @@ static int sock_map_update_common(struct bpf_map *map, u32 idx,return -EINVAL ;if (unlikely (idx >= map->max_entries))return -E2BIG ; -if (unlikely (rcu_access_pointer (icsk->icsk_ulp_data))) +if (sock_map_sk_has_ulp (sk))return -EINVAL ; link =sk_psock_init_link ();
@@ -738,7 +742,6 @@ static int sock_hash_update_common(struct bpf_map *map, void *key, struct sock *sk, u64 flags) { struct bpf_htab *htab = container_of(map, struct bpf_htab, map); -struct inet_connection_sock *icsk =inet_csk (sk);u32 key_size = map->key_size, hash;struct bpf_htab_elem *elem, *elem_new;struct bpf_htab_bucket *bucket;
@@ -749,7 +752,7 @@ static int sock_hash_update_common(struct bpf_map *map, void *key,WARN_ON_ONCE (!rcu_read_lock_held ());if (unlikely (flags >BPF_EXIST ))return -EINVAL ; -if (unlikely (icsk->icsk_ulp_data)) +if (sock_map_sk_has_ulp (sk))return -EINVAL ; link =sk_psock_init_link ();
Signature · 1 line
2.20.1
John Fastabend
maintainerRE: [PATCH bpf-next v2 1/9] bpf: sockmap: only check ULP for TCP sockets
Quoted 17 lines · Lorenz Bauer
The sock map code checks that a socket does not have an active upper layer protocol before inserting it into the map. This requires casting via
inet_csk, which isn't valid for UDP sockets.Guard checks for ULP by checking inet_sk(sk)->is_icsk first.
Signed-off-by Lorenz Bauer <[email protected]>--- include/linux/skmsg.h | 8 +++++++- net/core/sock_map.c | 11 +++++++---- 2 files changed, 14 insertions(+), 5 deletions(-)include/linux/skmsg.h +0
-0
include/linux/skmsg.h static inline void sk_psock_restore_proto(struct sock *sk, · line 363
Quoted 5 lines
include/linux/skmsg.h +1
-1@@ -360,7 +360,13 @@ static inline void sk_psock_restore_proto(struct sock *sk, struct sk_psock *psock) { sk->sk_prot->unhash = psock->saved_unhash; -tcp_update_ulp (sk, psock->sk_proto, psock->saved_write_space); +if (inet_sk (sk)->is_icsk) {
use sock_map_sk_has_ulp() here as well and then drop the !icsk->icsk_ulp_ops case in tcp_update_ulp()?
Quoted 62 lines
include/linux/skmsg.h +6
-0+tcp_update_ulp (sk, psock->sk_proto, psock->saved_write_space); + }else { + sk->sk_write_space = psock->saved_write_space; +/* Pairs with lockless read in sk_clone_lock() */ +WRITE_ONCE (sk->sk_prot, psock->sk_proto); + } } static inline void sk_psock_set_state(struct sk_psock *psock,net/core/sock_map.c +7
-4@@ -94,6 +94,11 @@ static void sock_map_sk_release(struct sock *sk)release_sock (sk); } +static bool sock_map_sk_has_ulp (struct sock *sk) +{ +return inet_sk (sk)->is_icsk && !!inet_csk (sk)->icsk_ulp_ops; +} +static void sock_map_add_link (struct sk_psock *psock,struct sk_psock_link *link,struct bpf_map *map,void *link_raw)@@ -384,7 +389,6 @@ static int sock_map_update_common(struct bpf_map *map, u32 idx, struct sock *sk, u64 flags) { struct bpf_stab *stab = container_of(map, struct bpf_stab, map); -struct inet_connection_sock *icsk =inet_csk (sk);struct sk_psock_link *link;struct sk_psock *psock;struct sock *osk;@@ -395,7 +399,7 @@ static int sock_map_update_common(struct bpf_map *map, u32 idx,return -EINVAL ;if (unlikely (idx >= map->max_entries))return -E2BIG ; -if (unlikely (rcu_access_pointer (icsk->icsk_ulp_data))) +if (sock_map_sk_has_ulp (sk))return -EINVAL ; link =sk_psock_init_link ();@@ -738,7 +742,6 @@ static int sock_hash_update_common(struct bpf_map *map, void *key, struct sock *sk, u64 flags) { struct bpf_htab *htab = container_of(map, struct bpf_htab, map); -struct inet_connection_sock *icsk =inet_csk (sk);u32 key_size = map->key_size, hash;struct bpf_htab_elem *elem, *elem_new;struct bpf_htab_bucket *bucket;@@ -749,7 +752,7 @@ static int sock_hash_update_common(struct bpf_map *map, void *key,WARN_ON_ONCE (!rcu_read_lock_held ());if (unlikely (flags >BPF_EXIST ))return -EINVAL ; -if (unlikely (icsk->icsk_ulp_data)) +if (sock_map_sk_has_ulp (sk))return -EINVAL ; link =sk_psock_init_link ();Signature · 1 line
2.20.1
Re: [PATCH bpf-next v2 1/9] bpf: sockmap: only check ULP for TCP sockets
Quoted 14 lines · John Fastabend
include/linux/skmsg.h static inline void sk_psock_restore_proto(struct sock *sk, · line 363
Quoted 11 lines
include/linux/skmsg.h +1
-1@@ -360,7 +360,13 @@ static inline void sk_psock_restore_proto(struct sock *sk, struct sk_psock *psock) { sk->sk_prot->unhash = psock->saved_unhash; -tcp_update_ulp (sk, psock->sk_proto, psock->saved_write_space); +if (inet_sk (sk)->is_icsk) {use
sock_map_sk_has_ulp()here as well and then drop the !icsk->icsk_ulp_ops case intcp_update_ulp()?
This requires moving sock_map_sk_has_ulp to the header, which seemed like the incorrect place. How about adding bool inet_csk_has_ulp(sk) to inet_connection_sock.h?
Signature · 4 lines
Lorenz Bauer | Systems Engineer 6th Floor, County Hall/The Riverside Building, SE1 7PB, UK www.cloudflare.com
PATCH v2 2/9 bpf: tcp: guard declarations with CONFIG_NET_SOCK_MSG
In reply to Lorenz Bauertcp_bpf.c is only included in the build if CONFIG_NET_SOCK_MSG is selected. The declaration should therefore be guarded as such.
--- include/net/tcp.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
include/net/tcp.h +2 -2
@@ -2195,6 +2195,7 @@ void tcp_update_ulp(struct sock *sk, struct proto *p,struct sk_msg ;struct sk_psock ; +#ifdef CONFIG_NET_SOCK_MSG int tcp_bpf_init (struct sock *sk);void tcp_bpf_reinit (struct sock *sk); int tcp_bpf_sendmsg_redir(struct sock *sk, struct sk_msg *msg, u32 bytes,
@@ -2203,13 +2204,12 @@ int tcp_bpf_recvmsg(struct sock *sk, struct msghdr *msg, size_t len, int nonblock, int flags, int *addr_len);int __tcp_bpf_recvmsg (struct sock *sk,struct sk_psock *psock,struct msghdr *msg,int len,int flags); -#ifdef CONFIG_NET_SOCK_MSG void tcp_bpf_clone (const struct sock *sk,struct sock *newsk);#else static inline void tcp_bpf_clone (const struct sock *sk,struct sock *newsk) { } -#endif +#endif /* CONFIG_NET_SOCK_MSG */ /* Call BPF_SOCK_OPS program that returns an int. If the return value * is < 0, then the BPF op failed (for example if the loaded BPF
Signature · 1 line
2.20.1
RE: [PATCH bpf-next v2 2/9] bpf: tcp: guard declarations with CONFIG_NET_SOCK_MSG
Quoted 5 lines · Lorenz Bauer
tcp_bpf.cis only included in the build ifCONFIG_NET_SOCK_MSGis selected. The declaration should therefore be guarded as such.Signed-off-by Lorenz Bauer <[email protected]>---
Seems OK, stream parser configs require sock msg now so that dependency chain is already covered.
PATCH v2 3/9 bpf: sockmap: move generic sockmap hooks from BPF TCP
In reply to Lorenz BauerThe close, unhash and clone handlers from TCP sockmap are actually generic, and can be reused by UDP sockmap. Move the helpers into the sockmap code base and expose them. This requires tcp_bpf_(re)init and tcp_bpf_clone to be conditional on BPF_STREAM_PARSER.
--- include/linux/bpf.h | 4 ++- include/linux/skmsg.h | 28 ---------------- include/net/tcp.h | 15 +++++---- net/core/sock_map.c | 77 +++++++++++++++++++++++++++++++++++++++++-- net/ipv4/tcp_bpf.c | 59 ++++----------------------------- 5 files changed, 92 insertions(+), 91 deletions(-)
include/linux/bpf.h +3 -1
@@ -1385,6 +1385,8 @@ static inline void bpf_map_offload_map_free(struct bpf_map *map)#if defined(CONFIG_BPF_STREAM_PARSER )int sock_map_prog_update (struct bpf_map *map,struct bpf_prog *prog,u32 which);int sock_map_get_from_fd (const union bpf_attr *attr,struct bpf_prog *prog); +void sock_map_unhash (struct sock *sk); +void sock_map_close (struct sock *sk,long timeout);#else static inline int sock_map_prog_update (struct bpf_map *map,struct bpf_prog *prog,u32 which)
@@ -1397,7 +1399,7 @@ static inline int sock_map_get_from_fd(const union bpf_attr *attr, {return -EINVAL ; } -#endif +#endif /* CONFIG_BPF_STREAM_PARSER */ #if defined(CONFIG_INET ) && defined(CONFIG_BPF_SYSCALL )void bpf_sk_reuseport_detach (struct sock *sk);
include/linux/skmsg.h +0 -28
@@ -323,14 +323,6 @@ static inline void sk_psock_free_link(struct sk_psock_link *link) }struct sk_psock_link *sk_psock_link_pop (struct sk_psock *psock); -#if defined(CONFIG_BPF_STREAM_PARSER ) -void sk_psock_unlink (struct sock *sk,struct sk_psock_link *link); -#else -static inline void sk_psock_unlink (struct sock *sk, -struct sk_psock_link *link) -{ -} -#endif void __sk_psock_purge_ingress_msg (struct sk_psock *psock);
@@ -387,26 +379,6 @@ static inline bool sk_psock_test_state(const struct sk_psock *psock,return test_bit (bit, &psock->state); } -static inline struct sk_psock *sk_psock_get_checked (struct sock *sk) -{ -struct sk_psock *psock; - -rcu_read_lock (); - psock =sk_psock (sk); -if (psock) { -if (sk->sk_prot->recvmsg != tcp_bpf_recvmsg) { - psock =ERR_PTR (-EBUSY ); - goto out; - } - -if (!refcount_inc_not_zero (&psock->refcnt)) - psock =ERR_PTR (-EBUSY ); - } -out: -rcu_read_unlock (); -return psock; -} -static inline struct sk_psock *sk_psock_get (struct sock *sk) {struct sk_psock *psock;
include/net/tcp.h +9 -6
@@ -2195,20 +2195,23 @@ void tcp_update_ulp(struct sock *sk, struct proto *p,struct sk_msg ;struct sk_psock ; -#ifdef CONFIG_NET_SOCK_MSG +#ifdef CONFIG_BPF_STREAM_PARSER int tcp_bpf_init (struct sock *sk);void tcp_bpf_reinit (struct sock *sk); +void tcp_bpf_clone (const struct sock *sk,struct sock *newsk); +#else +static inline void tcp_bpf_clone (const struct sock *sk,struct sock *newsk) +{ +} +#endif /* CONFIG_BPF_STREAM_PARSER */ + +#ifdef CONFIG_NET_SOCK_MSG int tcp_bpf_sendmsg_redir (struct sock *sk,struct sk_msg *msg,u32 bytes,int flags);int tcp_bpf_recvmsg (struct sock *sk,struct msghdr *msg,size_t len,int nonblock,int flags,int *addr_len);int __tcp_bpf_recvmsg (struct sock *sk,struct sk_psock *psock,struct msghdr *msg,int len,int flags); -void tcp_bpf_clone (const struct sock *sk,struct sock *newsk); -#else -static inline void tcp_bpf_clone (const struct sock *sk,struct sock *newsk) -{ -}#endif /* CONFIG_NET_SOCK_MSG */ /* Call BPF_SOCK_OPS program that returns an int. If the return value
net/core/sock_map.c +74 -3
104 lines not on this page · raw message
net/ipv4/tcp_bpf.c +6 -53
92 lines not on this page · raw message
Signature · 1 line
2.20.1
RE: [PATCH bpf-next v2 3/9] bpf: sockmap: move generic sockmap hooks from BPF TCP
Quoted 5 lines · Lorenz Bauer
The close, unhash and clone handlers from TCP sockmap are actually generic, and can be reused by UDP sockmap. Move the helpers into the sockmap code base and expose them. This requires tcp_bpf_(re)init and
tcp_bpf_cloneto be conditional onBPF_STREAM_PARSER.
Quoted 8 lines
Signed-off-by Lorenz Bauer <[email protected]>--- include/linux/bpf.h | 4 ++- include/linux/skmsg.h | 28 ---------------- include/net/tcp.h | 15 +++++---- net/core/sock_map.c | 77 +++++++++++++++++++++++++++++++++++++++++-- net/ipv4/tcp_bpf.c | 59 ++++----------------------------- 5 files changed, 92 insertions(+), 91 deletions(-)
No changes just moving code around it seems.
Re: [PATCH bpf-next v2 3/9] bpf: sockmap: move generic sockmap hooks from BPF TCP
In reply to Lorenz BauerQuoted 2 lines · Lorenz Bauer
The close, unhash and clone handlers from TCP sockmap are actually generic, and can be reused by UDP sockmap. Move the helpers into the sockmap code
Is clone reused in UDP?
Quoted 3 lines
base and expose them. This requires tcp_bpf_(re)init and
tcp_bpf_cloneto be conditional onBPF_STREAM_PARSER.
[ ... ]
Quoted 6 lines
net/ipv4/tcp_bpf.c +1
-0@@ -707,3 +659,4 @@ void tcp_bpf_clone(const struct sock *sk, struct sock *newsk)if (prot == &tcp_bpf_prots[family][TCP_BPF_BASE ]) newsk->sk_prot = sk->sk_prot_creator; } +#endif /* CONFIG_BPF_STREAM_PARSER */
Re: [PATCH bpf-next v2 3/9] bpf: sockmap: move generic sockmap hooks from BPF TCP
Quoted 5 lines · Martin KaFai Lau
Quoted 2 lines · Lorenz Bauer
The close, unhash and clone handlers from TCP sockmap are actually generic, and can be reused by UDP sockmap. Move the helpers into the sockmap code
Is clone reused in UDP?
No, my bad. I'll update the commit message.
Signature · 4 lines
Lorenz Bauer | Systems Engineer 6th Floor, County Hall/The Riverside Building, SE1 7PB, UK www.cloudflare.com
PATCH v2 4/9 skmsg: introduce sk_psock_hooks
In reply to Lorenz BauerThe sockmap works by overriding some of the callbacks in sk->sk_prot, while leaving others untouched. This means that we need access to the struct proto for any protocol we want to support. For IPv4 this is trivial, since both TCP and UDP are always compiled in. IPv6 may be disabled or compiled as a module, so the existing TCP sockmap hooks use some trickery to lazily initialize the modified struct proto for TCPv6.
Pull this logic into a standalone struct sk_psock_hooks, so that it can be re-used by UDP sockmap.
--- include/linux/skmsg.h | 36 +++++++++----- include/net/tcp.h | 1 - net/core/skmsg.c | 55 +++++++++++++++++++++ net/core/sock_map.c | 24 ++++----- net/ipv4/tcp_bpf.c | 112 ++++++++++++------------------------------ 5 files changed, 118 insertions(+), 110 deletions(-)
include/linux/skmsg.h +23 -13
@@ -109,6 +109,16 @@ struct sk_psock { }; }; +struct sk_psock_hooks { +struct proto *base_ipv6; +struct proto *ipv4; +struct proto *ipv6; +spinlock_t ipv6_lock; +int (*rebuild_proto)(struct proto prot[],struct proto *base); +struct proto *(*choose_proto)(struct proto prot[], +struct sk_psock *psock); +}; +int sk_msg_alloc (struct sock *sk,struct sk_msg *msg,int len,int elem_first_coalesce); int sk_msg_clone(struct sock *sk, struct sk_msg *dst, struct sk_msg *src,
@@ -335,23 +345,14 @@ static inline void sk_psock_cork_free(struct sk_psock *psock) } } -static inline void sk_psock_update_proto (struct sock *sk, -struct sk_psock *psock, -struct proto *ops) -{ - psock->saved_unhash = sk->sk_prot->unhash; - psock->saved_close = sk->sk_prot->close; - psock->saved_write_space = sk->sk_write_space; - - psock->sk_proto = sk->sk_prot; -/* Pairs with lockless read in sk_clone_lock() */ -WRITE_ONCE (sk->sk_prot, ops); -} -static inline void sk_psock_restore_proto (struct sock *sk,struct sk_psock *psock) { +if (!psock->sk_proto) +return ; + sk->sk_prot->unhash = psock->saved_unhash; +if (inet_sk (sk)->is_icsk) {tcp_update_ulp (sk, psock->sk_proto, psock->saved_write_space); }else {
@@ -424,4 +425,13 @@ static inline void psock_progs_drop(struct sk_psock_progs *progs) psock_set_prog(&progs->skb_verdict, NULL); } +static inline int sk_psock_hooks_init (struct sk_psock_hooks *hooks, +struct proto *ipv4_base) +{ +spin_lock_init (&hooks->ipv6_lock); +return hooks->rebuild_proto (hooks->ipv4, ipv4_base); +} + +int sk_psock_hooks_install (struct sk_psock_hooks *hooks,struct sock *sk); +#endif /* _LINUX_SKMSG_H */
include/net/tcp.h +0 -1
@@ -2197,7 +2197,6 @@ struct sk_psock;#ifdef CONFIG_BPF_STREAM_PARSER int tcp_bpf_init (struct sock *sk); -void tcp_bpf_reinit (struct sock *sk);void tcp_bpf_clone (const struct sock *sk,struct sock *newsk);#else static inline void tcp_bpf_clone (const struct sock *sk,struct sock *newsk)
net/core/skmsg.c +55 -0
58 lines not on this page · raw message
net/core/sock_map.c +9 -15
@@ -170,8 +170,8 @@ static int sock_map_link(struct bpf_map *map, struct sk_psock_progs *progs, struct sock *sk) {struct bpf_prog *msg_parser, *skb_parser, *skb_verdict; -bool skb_progs, sk_psock_is_new = false;struct sk_psock *psock; +bool skb_progs;int ret; skb_verdict =READ_ONCE (progs->skb_verdict);
@@ -206,9 +206,8 @@ static int sock_map_link(struct bpf_map *map, struct sk_psock_progs *progs,if (psock) {if ((msg_parser &&READ_ONCE (psock->progs.msg_parser)) || (skb_progs &&READ_ONCE (psock->progs.skb_parser))) { -sk_psock_put (sk, psock); ret = -EBUSY ; - goto out_progs; + goto out_drop; } }else { psock =sk_psock_init (sk, map->numa_node);
@@ -216,18 +215,14 @@ static int sock_map_link(struct bpf_map *map, struct sk_psock_progs *progs, ret = -ENOMEM ; goto out_progs; } - sk_psock_is_new = true; }if (msg_parser)psock_set_prog (&psock->progs.msg_parser, msg_parser); -if (sk_psock_is_new) { - ret =tcp_bpf_init (sk); -if (ret <0 ) - goto out_drop; - }else { -tcp_bpf_reinit (sk); - } + + ret =tcp_bpf_init (sk); +if (ret <0 ) + goto out_drop;write_lock_bh (&sk->sk_callback_lock);if (skb_progs && !psock->parser.enabled) {
@@ -264,15 +259,14 @@ static int sock_map_link_no_progs(struct bpf_map *map, struct sock *sk)if (IS_ERR (psock))return PTR_ERR (psock); -if (psock) { -tcp_bpf_reinit (sk); -return 0 ; - } +if (psock) + goto init; psock =sk_psock_init (sk, map->numa_node);if (!psock)return -ENOMEM ; +init: ret =tcp_bpf_init (sk);if (ret <0 )sk_psock_put (sk, psock);
net/ipv4/tcp_bpf.c +31 -81
157 lines not on this page · raw message
Signature · 1 line
2.20.1
PATCH v2 5/9 bpf: sockmap: allow UDP sockets
In reply to Lorenz BauerAdd basic psock hooks for UDP sockets. This allows adding and removing sockets, as well as automatic removal on unhash and close.
sock_map_sk_state_allowed is called from the syscall path, and ensures that only established or listening sockets are added. No such check exists for the BPF path: we rely on sockets being in particular states when a BPF sock ops hook is executed. For the passive open hook this means that sockets are actually in TCP_SYN_RECV state (and unhashed) when they are added to the sock map.
UDP sockets are not saddled with this inconsistency, and so the checks for both syscall and BPF path should be identical. Rather than duplicating the logic into sock_map_sk_state_allowed merge it with sock_map_sk_is_suitable.
--- MAINTAINERS | 1 + include/linux/udp.h | 4 ++++ net/core/sock_map.c | 52 ++++++++++++++++++++++++++------------------ net/ipv4/Makefile | 1 + net/ipv4/udp_bpf.c | 53 +++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 90 insertions(+), 21 deletions(-) create mode 100644 net/ipv4/udp_bpf.c
MAINTAINERS +1 -0
@@ -9358,6 +9358,7 @@ F: include/linux/skmsg.h F: net/core/skmsg.c F: net/core/sock_map.c F: net/ipv4/tcp_bpf.c +F: net/ipv4/udp_bpf.c LANTIQ / INTEL Ethernet drivers M: Hauke Mehrtens <[email protected]>
include/linux/udp.h +4 -0
@@ -143,4 +143,8 @@ static inline bool udp_unexpected_gso(struct sock *sk, struct sk_buff *skb)#define IS_UDPLITE (__sk) (__sk->sk_protocol == IPPROTO_UDPLITE) +#ifdef CONFIG_BPF_STREAM_PARSER +int udp_bpf_init (struct sock *sk); +#endif +#endif /* _LINUX_UDP_H */
net/core/sock_map.c +31 -21
122 lines not on this page · raw message
net/ipv4/Makefile +1 -0
@@ -61,6 +61,7 @@ obj-$(CONFIG_TCP_CONG_LP) += tcp_lp.o obj-$(CONFIG_TCP_CONG_YEAH) += tcp_yeah.o obj-$(CONFIG_TCP_CONG_ILLINOIS) += tcp_illinois.o obj-$(CONFIG_NET_SOCK_MSG) += tcp_bpf.o +obj-$(CONFIG_BPF_STREAM_PARSER) += udp_bpf.o obj-$(CONFIG_NETLABEL) += cipso_ipv4.o obj-$(CONFIG_XFRM) += xfrm4_policy.o xfrm4_state.o xfrm4_input.o \
net/ipv4/udp_bpf.c new file +53 -0
53 lines not on this page · raw message
Signature · 1 line
2.20.1
Re: [PATCH bpf-next v2 5/9] bpf: sockmap: allow UDP sockets
Quoted 55 lines · Lorenz Bauer
Add basic psock hooks for UDP sockets. This allows adding and removing sockets, as well as automatic removal on unhash and close.
sock_map_sk_state_allowedis called from the syscall path, and ensures that only established or listening sockets are added. No such check exists for the BPF path: we rely on sockets being in particular states when a BPF sock ops hook is executed. For the passive open hook this means that sockets are actually inTCP_SYN_RECVstate (and unhashed) when they are added to the sock map.UDP sockets are not saddled with this inconsistency, and so the checks for both syscall and BPF path should be identical. Rather than duplicating the logic into
sock_map_sk_state_allowedmerge it withsock_map_sk_is_suitable.Signed-off-by Lorenz Bauer <[email protected]>--- MAINTAINERS | 1 + include/linux/udp.h | 4 ++++ net/core/sock_map.c | 52 ++++++++++++++++++++++++++------------------ net/ipv4/Makefile | 1 + net/ipv4/udp_bpf.c | 53 +++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 90 insertions(+), 21 deletions(-) create mode 100644 net/ipv4/udp_bpf.cMAINTAINERS +1
-0@@ -9358,6 +9358,7 @@ F: include/linux/skmsg.h F: net/core/skmsg.c F: net/core/sock_map.c F: net/ipv4/tcp_bpf.c +F: net/ipv4/udp_bpf.c LANTIQ / INTEL Ethernet drivers M: Hauke Mehrtens <[email protected]>include/linux/udp.h +4
-0@@ -143,4 +143,8 @@ static inline bool udp_unexpected_gso(struct sock *sk, struct sk_buff *skb)#define IS_UDPLITE (__sk) (__sk->sk_protocol == IPPROTO_UDPLITE) +#ifdef CONFIG_BPF_STREAM_PARSER +int udp_bpf_init (struct sock *sk); +#endif +#endif /* _LINUX_UDP_H */ net/core/sock_map.c +0
-0
[ ... ]
Quoted 4 lines
net/core/sock_map.c +0
-0@@ -466,15 +479,20 @@ static bool sock_map_op_okay(const struct bpf_sock_ops_kern *ops) ops->op ==BPF_SOCK_OPS_TCP_LISTEN_CB ; }
net/core/sock_map.c static bool sock_map_op_okay(const struct bpf_sock_ops_kern *ops) · line 487
Quoted 10 lines
net/core/sock_map.c +3
-4-static bool sock_map_sk_is_suitable (const struct sock *sk) +static bool sock_map_sk_is_udp (const struct sock *sk) { -return sk->sk_type ==SOCK_STREAM && - sk->sk_protocol ==IPPROTO_TCP ; +return sk->sk_type ==SOCK_DGRAM && sk->sk_protocol ==IPPROTO_UDP ; } -static bool sock_map_sk_state_allowed (const struct sock *sk) +static bool sock_map_sk_is_suitable (const struct sock *sk,bool from_bpf)
"from_bpf" seems unnecessary.
Quoted 12 lines
net/core/sock_map.c +7
-1{ -return (1 << sk->sk_state) & (TCPF_ESTABLISHED |TCPF_LISTEN ); +int tcp_flags =TCPF_ESTABLISHED |TCPF_LISTEN ; + +if (from_bpf) + tcp_flags |=TCPF_SYN_RECV ; + +return (sock_map_sk_is_udp (sk) &&sk_hashed (sk)) || + (sock_map_sk_is_tcp (sk) && (1 << sk->sk_state) & tcp_flags); } static int sock_map_update_elem(struct bpf_map *map, void *key,
PATCH v2 6/9 selftests: bpf: don't listen() on UDP sockets
In reply to Lorenz BauerMost tests for TCP sockmap can be adapted to UDP sockmap if the listen call is skipped. Rename listen_loopback, etc. to socket_loopback and skip listen() for SOCK_DGRAM.
--- .../selftests/bpf/prog_tests/sockmap_listen.c | 47 ++++++++++--------- 1 file changed, 25 insertions(+), 22 deletions(-)
tools/testing/selftests/bpf/prog_tests/sockmap_listen.c +25 -22
@@ -230,7 +230,7 @@ static int enable_reuseport(int s, int progfd)return 0 ; } -static int listen_loopback_reuseport (int family,int sotype,int progfd) +static int socket_loopback_reuseport (int family,int sotype,int progfd) {struct sockaddr_storage addr;socklen_t len;
@@ -249,6 +249,9 @@ static int listen_loopback_reuseport(int family, int sotype, int progfd)if (err) goto close; +if (sotype ==SOCK_DGRAM ) +return s; + err =xlisten (s,SOMAXCONN );if (err) goto close;
@@ -259,9 +262,9 @@ static int listen_loopback_reuseport(int family, int sotype, int progfd)return -1 ; } -static int listen_loopback (int family,int sotype) +static int socket_loopback (int family,int sotype) { -return listen_loopback_reuseport (family, sotype,-1 ); +return socket_loopback_reuseport (family, sotype,-1 ); }static void test_insert_invalid (int family,int sotype,int mapfd)
@@ -333,7 +336,7 @@ static void test_insert_listening(int family, int sotype, int mapfd)u32 key;int s; - s =listen_loopback (family, sotype); + s =socket_loopback (family, sotype);if (s <0 )return ;
@@ -349,7 +352,7 @@ static void test_delete_after_insert(int family, int sotype, int mapfd)u32 key;int s; - s =listen_loopback (family, sotype); + s =socket_loopback (family, sotype);if (s <0 )return ;
@@ -366,7 +369,7 @@ static void test_delete_after_close(int family, int sotype, int mapfd)u64 value;u32 key; - s =listen_loopback (family, sotype); + s =socket_loopback (family, sotype);if (s <0 )return ;
@@ -390,7 +393,7 @@ static void test_lookup_after_insert(int family, int sotype, int mapfd)u32 key;int s; - s =listen_loopback (family, sotype); + s =socket_loopback (family, sotype);if (s <0 )return ;
@@ -417,7 +420,7 @@ static void test_lookup_after_delete(int family, int sotype, int mapfd)u64 value;u32 key; - s =listen_loopback (family, sotype); + s =socket_loopback (family, sotype);if (s <0 )return ;
@@ -439,7 +442,7 @@ static void test_lookup_32_bit_value(int family, int sotype, int mapfd)u32 key, value32;int err, s; - s =listen_loopback (family, sotype); + s =socket_loopback (family, sotype);if (s <0 )return ;
@@ -470,11 +473,11 @@ static void test_update_listening(int family, int sotype, int mapfd)u64 value;u32 key; - s1 =listen_loopback (family, sotype); + s1 =socket_loopback (family, sotype);if (s1 <0 )return ; - s2 =listen_loopback (family, sotype); + s2 =socket_loopback (family, sotype);if (s2 <0 ) goto close_s1;
@@ -500,7 +503,7 @@ static void test_destroy_orphan_child(int family, int sotype, int mapfd)u64 value;u32 key; - s =listen_loopback (family, sotype); + s =socket_loopback (family, sotype);if (s <0 )return ;
@@ -534,7 +537,7 @@ static void test_clone_after_delete(int family, int sotype, int mapfd)u64 value;u32 key; - s =listen_loopback (family, sotype); + s =socket_loopback (family, sotype);if (s <0 )return ;
@@ -570,7 +573,7 @@ static void test_accept_after_delete(int family, int sotype, int mapfd)socklen_t len;u64 value; - s =listen_loopback (family, sotype); + s =socket_loopback (family, sotype);if (s ==-1 )return ;
@@ -624,7 +627,7 @@ static void test_accept_before_delete(int family, int sotype, int mapfd)socklen_t len;u64 value; - s =listen_loopback (family, sotype); + s =socket_loopback (family, sotype);if (s ==-1 )return ;
@@ -735,7 +738,7 @@ static void test_syn_recv_insert_delete(int family, int sotype, int mapfd)int err, s;u64 value; - s =listen_loopback (family, sotype |SOCK_NONBLOCK ); + s =socket_loopback (family, sotype |SOCK_NONBLOCK );if (s <0 )return ;
@@ -877,7 +880,7 @@ static void redir_to_connected(int family, int sotype, int sock_mapfd,zero_verdict_count (verd_mapfd); - s =listen_loopback (family, sotype |SOCK_NONBLOCK ); + s =socket_loopback (family, sotype |SOCK_NONBLOCK );if (s <0 )return ;
@@ -1009,7 +1012,7 @@ static void redir_to_listening(int family, int sotype, int sock_mapfd,zero_verdict_count (verd_mapfd); - s =listen_loopback (family, sotype |SOCK_NONBLOCK ); + s =socket_loopback (family, sotype |SOCK_NONBLOCK );if (s <0 )return ;
@@ -1120,7 +1123,7 @@ static void test_reuseport_select_listening(int family, int sotype,zero_verdict_count (verd_map); - s =listen_loopback_reuseport (family, sotype, reuseport_prog); + s =socket_loopback_reuseport (family, sotype, reuseport_prog);if (s <0 )return ;
@@ -1174,7 +1177,7 @@ static void test_reuseport_select_connected(int family, int sotype,zero_verdict_count (verd_map); - s =listen_loopback_reuseport (family, sotype, reuseport_prog); + s =socket_loopback_reuseport (family, sotype, reuseport_prog);if (s <0 )return ;
@@ -1249,11 +1252,11 @@ static void test_reuseport_mixed_groups(int family, int sotype, int sock_map,zero_verdict_count (verd_map);/* Create two listeners, each in its own reuseport group */ - s1 =listen_loopback_reuseport (family, sotype, reuseport_prog); + s1 =socket_loopback_reuseport (family, sotype, reuseport_prog);if (s1 <0 )return ; - s2 =listen_loopback_reuseport (family, sotype, reuseport_prog); + s2 =socket_loopback_reuseport (family, sotype, reuseport_prog);if (s2 <0 ) goto close_srv1;
Signature · 1 line
2.20.1
PATCH v2 7/9 selftests: bpf: add tests for UDP sockets in sockmap
In reply to Lorenz BauerExpand the TCP sockmap test suite to also check UDP sockets.
--- .../selftests/bpf/prog_tests/sockmap_listen.c | 157 ++++++++++++++---- 1 file changed, 127 insertions(+), 30 deletions(-)
tools/testing/selftests/bpf/prog_tests/sockmap_listen.c +127 -30
@@ -108,6 +108,22 @@ __ret; \ }) +#define xsend (fd, buf, len, flags) \ + ({ \ + ssize_t __ret = send((fd), (buf), (len), (flags)); \ + if (__ret == -1) \ + FAIL_ERRNO("send"); \ + __ret; \ + }) + +#define xrecv (fd, buf, len, flags) \ + ({ \ + ssize_t __ret = recv((fd), (buf), (len), (flags)); \ + if (__ret == -1) \ + FAIL_ERRNO("recv"); \ + __ret; \ + }) +#define xsocket (family, sotype, flags) \ ({ \ int __ret = socket(family, sotype, flags); \
@@ -330,7 +346,7 @@ static void test_insert_bound(int family, int sotype, int mapfd)xclose (s); } -static void test_insert_listening (int family,int sotype,int mapfd) +static void test_insert (int family,int sotype,int mapfd) {u64 value;u32 key;
@@ -467,7 +483,7 @@ static void test_lookup_32_bit_value(int family, int sotype, int mapfd)xclose (s); } -static void test_update_listening (int family,int sotype,int mapfd) +static void test_update_existing (int family,int sotype,int mapfd) {int s1, s2;u64 value;
@@ -1116,7 +1132,7 @@ static void test_reuseport_select_listening(int family, int sotype, {struct sockaddr_storage addr;unsigned int pass; -int s, c, p, err; +int s, c, err;socklen_t len;u64 value;u32 key;
@@ -1145,19 +1161,33 @@ static void test_reuseport_select_listening(int family, int sotype,if (err) goto close_cli; - p =xaccept (s,NULL ,NULL ); -if (p <0 ) - goto close_cli; +if (sotype ==SOCK_STREAM ) { +int p; + + p =xaccept (s,NULL ,NULL ); +if (p <0 ) + goto close_cli; +xclose (p); + }else { +char b ='a' ; +ssize_t n; + + n =xsend (c, &b,sizeof (b),0 ); +if (n ==-1 ) + goto close_cli; + + n =xrecv (s, &b,sizeof (b),0 ); +if (n ==-1 ) + goto close_cli; + } key =SK_PASS ; err =xbpf_map_lookup_elem (verd_map, &key, &pass);if (err) - goto close_peer; + goto close_cli;if (pass !=1 )FAIL ("want pass count 1, have %d" , pass); -close_peer: -xclose (p); close_cli:xclose (c); close_srv:
@@ -1201,9 +1231,24 @@ static void test_reuseport_select_connected(int family, int sotype,if (err) goto close_cli0; - p0 =xaccept (s,NULL ,NULL ); -if (err) - goto close_cli0; +if (sotype ==SOCK_STREAM ) { + p0 =xaccept (s,NULL ,NULL ); +if (p0 <0 ) + goto close_cli0; + }else { + p0 =xsocket (family, sotype,0 ); +if (p0 <0 ) + goto close_cli0; + + len =sizeof (addr); + err =xgetsockname (c0,sockaddr (&addr), &len); +if (err) + goto close_cli0; + + err =xconnect (p0,sockaddr (&addr), len); +if (err) + goto close_cli0; + }/* Update sock_map[0] to redirect to a connected socket */ key =0 ;
@@ -1216,8 +1261,24 @@ static void test_reuseport_select_connected(int family, int sotype,if (c1 <0 ) goto close_peer0; + len =sizeof (addr); + err =xgetsockname (s,sockaddr (&addr), &len); +if (err) + goto close_srv; + errno =0 ; err =connect (c1,sockaddr (&addr), len); +if (sotype ==SOCK_DGRAM ) { +char b ='a' ; +ssize_t n; + + n =xsend (c1, &b,sizeof (b),0 ); +if (n ==-1 ) + goto close_cli1; + + n =recv (c1, &b,sizeof (b),0 ); + err = n ==-1 ; + }if (!err || errno !=ECONNREFUSED )FAIL_ERRNO ("connect: expected ECONNREFUSED" );
@@ -1281,7 +1342,18 @@ static void test_reuseport_mixed_groups(int family, int sotype, int sock_map, goto close_srv2; err =connect (c,sockaddr (&addr), len); -if (err && errno !=ECONNREFUSED ) { +if (sotype ==SOCK_DGRAM ) { +char b ='a' ; +ssize_t n; + + n =xsend (c, &b,sizeof (b),0 ); +if (n ==-1 ) + goto close_cli; + + n =recv (c, &b,sizeof (b),0 ); + err = n ==-1 ; + } +if (!err || errno !=ECONNREFUSED ) {FAIL_ERRNO ("connect: expected ECONNREFUSED" ); goto close_cli; }
@@ -1302,9 +1374,9 @@ static void test_reuseport_mixed_groups(int family, int sotype, int sock_map,xclose (s1); } -#define TEST (fn) \ +#define TEST (fn, ...) \ { \ - fn, #fn \ + fn, #fn, __VA_ARGS__ \ }static void test_ops_cleanup (const struct bpf_map *map)
@@ -1353,18 +1425,31 @@ static const char *map_type_str(const struct bpf_map *map) } } +static const char *sotype_str (int sotype) +{ +switch (sotype) { +case SOCK_DGRAM : +return "UDP" ; +case SOCK_STREAM : +return "TCP" ; +default : +return "unknown" ; + } +} +static void test_ops (struct test_sockmap_listen *skel,struct bpf_map *map,int family,int sotype) {const struct op_test {void (*fn)(int family,int sotype,int mapfd);const char *name; +int sotype; } tests[] = {/* insert */ TEST (test_insert_invalid),TEST (test_insert_opened), -TEST (test_insert_bound), -TEST (test_insert_listening), +TEST (test_insert_bound,SOCK_STREAM ), +TEST (test_insert),/* delete */ TEST (test_delete_after_insert),TEST (test_delete_after_close),
@@ -1373,28 +1458,32 @@ static void test_ops(struct test_sockmap_listen *skel, struct bpf_map *map,TEST (test_lookup_after_delete),TEST (test_lookup_32_bit_value),/* update */ -TEST (test_update_listening), +TEST (test_update_existing),/* races with insert/delete */ -TEST (test_destroy_orphan_child), -TEST (test_syn_recv_insert_delete), -TEST (test_race_insert_listen), +TEST (test_destroy_orphan_child,SOCK_STREAM ), +TEST (test_syn_recv_insert_delete,SOCK_STREAM ), +TEST (test_race_insert_listen,SOCK_STREAM ),/* child clone */ -TEST (test_clone_after_delete), -TEST (test_accept_after_delete), - TEST(test_accept_before_delete), +TEST (test_clone_after_delete,SOCK_STREAM ), +TEST (test_accept_after_delete,SOCK_STREAM ), + TEST(test_accept_before_delete, SOCK_STREAM), }; -const char *family_name, *map_name; +const char *family_name, *map_name, *sotype_name;const struct op_test *t;char s[MAX_TEST_NAME ];int map_fd; family_name =family_str (family); map_name =map_type_str (map); + sotype_name =sotype_str (sotype); map_fd =bpf_map__fd (map);for (t = tests; t < tests +ARRAY_SIZE (tests); t++) { -snprintf (s,sizeof (s),"%s %s %s" , map_name, family_name, - t->name); +snprintf (s,sizeof (s),"%s %s %s %s" , map_name, family_name, + sotype_name, t->name); + +if (t->sotype !=0 && t->sotype != sotype) +continue ;if (!test__start_subtest (s))continue ;
@@ -1427,6 +1516,7 @@ static void test_redir(struct test_sockmap_listen *skel, struct bpf_map *map,for (t = tests; t < tests +ARRAY_SIZE (tests); t++) {snprintf (s,sizeof (s),"%s %s %s" , map_name, family_name, t->name); +if (!test__start_subtest (s))continue ;
@@ -1441,26 +1531,31 @@ static void test_reuseport(struct test_sockmap_listen *skel,void (*fn)(int family,int sotype,int socket_map,int verdict_map,int reuseport_prog);const char *name; +int sotype; } tests[] = {TEST (test_reuseport_select_listening),TEST (test_reuseport_select_connected), TEST(test_reuseport_mixed_groups), };int socket_map, verdict_map, reuseport_prog; -const char *family_name, *map_name; +const char *family_name, *map_name, *sotype_name;const struct reuseport_test *t;char s[MAX_TEST_NAME ]; family_name =family_str (family); map_name =map_type_str (map); + sotype_name =sotype_str (sotype); socket_map =bpf_map__fd (map); verdict_map =bpf_map__fd (skel->maps.verdict_map); reuseport_prog =bpf_program__fd (skel->progs.prog_reuseport);for (t = tests; t < tests +ARRAY_SIZE (tests); t++) { -snprintf (s,sizeof (s),"%s %s %s" , map_name, family_name, - t->name); +snprintf (s,sizeof (s),"%s %s %s %s" , map_name, family_name, + sotype_name, t->name); + +if (t->sotype !=0 && t->sotype != sotype) +continue ;if (!test__start_subtest (s))continue ;
@@ -1473,8 +1568,10 @@ static void run_tests(struct test_sockmap_listen *skel, struct bpf_map *map, int family) {test_ops (skel, map, family,SOCK_STREAM ); +test_ops (skel, map, family,SOCK_DGRAM );test_redir (skel, map, family,SOCK_STREAM );test_reuseport (skel, map, family,SOCK_STREAM ); +test_reuseport (skel, map, family,SOCK_DGRAM ); }void test_sockmap_listen (void )
Signature · 1 line
2.20.1
PATCH v2 8/9 selftests: bpf: enable UDP sockmap reuseport tests
In reply to Lorenz BauerRemove the guard that disables UDP tests now that sockmap has support for them.
--- tools/testing/selftests/bpf/prog_tests/select_reuseport.c | 6 ------ 1 file changed, 6 deletions(-)
tools/testing/selftests/bpf/prog_tests/select_reuseport.c +0 -6
@@ -805,12 +805,6 @@ static void test_config(int sotype, sa_family_t family, bool inany)char s[MAX_TEST_NAME ];const struct test *t; -/* SOCKMAP/SOCKHASH don't support UDP yet */ -if (sotype ==SOCK_DGRAM && - (inner_map_type ==BPF_MAP_TYPE_SOCKMAP || - inner_map_type ==BPF_MAP_TYPE_SOCKHASH )) -return ; -for (t = tests; t < tests +ARRAY_SIZE (tests); t++) {if (t->need_sotype && t->need_sotype != sotype)continue ;/* test not compatible with socket type */
Signature · 1 line
2.20.1
PATCH v2 9/9 bpf, doc: update maintainers for L7 BPF
In reply to Lorenz BauerAdd Jakub and myself as maintainers for sockmap related code.
--- MAINTAINERS | 2 ++ 1 file changed, 2 insertions(+)
MAINTAINERS +2 -0
@@ -9351,6 +9351,8 @@ F: include/net/l3mdev.h L7 BPF FRAMEWORK M: John Fastabend <[email protected]> M: Daniel Borkmann <[email protected]> +M: Jakub Sitnicki <[email protected]> +M: Lorenz Bauer <[email protected]> L: [email protected] L: [email protected] S: Maintained
Signature · 1 line
2.20.1
RE: [PATCH bpf-next v2 9/9] bpf, doc: update maintainers for L7 BPF
Quoted 23 lines · Lorenz Bauer
Add Jakub and myself as maintainers for sockmap related code.
Signed-off-by Lorenz Bauer <[email protected]>--- MAINTAINERS | 2 ++ 1 file changed, 2 insertions(+)MAINTAINERS +2
-0@@ -9351,6 +9351,8 @@ F: include/net/l3mdev.h L7 BPF FRAMEWORK M: John Fastabend <[email protected]> M: Daniel Borkmann <[email protected]> +M: Jakub Sitnicki <[email protected]> +M: Lorenz Bauer <[email protected]> L: [email protected] L: [email protected] S: MaintainedSignature · 1 line
2.20.1
Quoted 3 lines · Lorenz Bauer
Thanks for all the reviews so far! I've fixed the identified bug and addressed feedback as much as possible.
This should've been a reply to
https://lore.kernel.org/bpf/[email protected]/
"[PATCH bpf-next 0/7] bpf: sockmap, sockhash: support storing UDP sockets"
Sorry!
Signature · 4 lines
Lorenz Bauer | Systems Engineer 6th Floor, County Hall/The Riverside Building, SE1 7PB, UK www.cloudflare.com
Jakub Sitnicki
maintainerQuoted 8 lines · Lorenz Bauer
Thanks for all the reviews so far! I've fixed the identified bug and addressed feedback as much as possible.
I've not taken up Jakub's suggestion to get rid of
sk_psock_hooks_init. My intention is to encapsulate initializing both v4 and v6 protos. Really, it's down to personal preference, and I'm happy to remove it if others prefer Jakub's approach. I'm also still eager for a solution that requires less machinery.
I was going to do expenses but this seemed more fun. Challenge accepted.
I think we can massage tcp_bpf to extract sk_prot initialization bits out of it into skmsg, and have skmsg call back into tcp/udp_bpf.
3 callbacks from skmsg back to tcp/udp_bpf would be needed to get there:
- assert_proto_ops
- check_v6_needs_rebuild
- get_proto (akin to choose_proto in this series)
With that in place we could go for a direct dispatch based on sock type:
#define sk_psock_assert_proto_ops(sk, ops) \ (sk->sk_type == SOCK_STREAM \ ? tcp_bpf_assert_proto_ops(ops) \ : udp_bpf_assert_proto_ops(ops))
The steps to get there would be:
1. extract tcp_bpf_get_proto
2. fold tcp_bpf_update_sk_prot
3. move tcp_bpf_init -> sk_psock_init_proto
4. fold tcp_bpf_reinit_sk_prot
5. move tcp_bpf_reinit -> sk_psock_reinit_proto
6. add macros for callbacks into tcp_bpf
7. add udp_bpf
... which I've given a shot at, mixing it into your patches:
https://github.com/jsitnicki/linux/commits/extract-sk-psock-proto
Note, I didn't make an effort to share the code for rebuilding v6 proto between tcp_bpf and udp_bpf. This construct seems hard to read already as is without making it generic.
Final thought, I would probably place the bits common between tcp_bpf and udp_bpf in skmsg under sk_psock_* namespace, instead of sockmap.
It is just my interpretation, but I think that was the idea outlined in commit 604326b41a6f ("bpf, sockmap: convert to generic sk_msg interface"):
The code itself has been split and refactored into three bigger
pieces: i) the generic sk_msg API which deals with managing the scatter gather ring, providing helpers for walking and mangling, transferring application data from user space into it, and preparing it for BPF pre/post-processing, ii) the plain sock map itself where sockets can be attached to or detached from; these bits are independent of i) which can now be used also without sock map, and iii) the integration with plain TCP as one protocol to be used for processing L7 application data (later this could e.g. also be extended to other protocols like UDP).
As skmsg already hosts some sk_psock_* functions used by tcp_bpf, and they share the same build toggle NET_SK_MSG, that seems natural.
Quoted 34 lines
Changes since v1:
- Checknewsk->sk_protintcp_bpf_clone
- Fix compilation withBPF_STREAM_PARSERdisabled
- Usespin_lock_initinstead of static initializer
- Elaborate onTCPF_SYN_RECV
- Cosmetic changes to TEST macros, and more tests
- Add Jakub and me as maintainersLorenz Bauer (9):
bpf: sockmap: only check ULP for TCP sockets
bpf: tcp: guard declarations withCONFIG_NET_SOCK_MSG
bpf: sockmap: move generic sockmap hooks from BPF TCP
skmsg: introducesk_psock_hooks
bpf: sockmap: allow UDP sockets
selftests: bpf: don'tlisten()on UDP sockets
selftests: bpf: add tests for UDP sockets in sockmap
selftests: bpf: enable UDP sockmap reuseport tests bpf, doc: update maintainers for L7 BPFMAINTAINERS | 3 + include/linux/bpf.h | 4 +- include/linux/skmsg.h | 72 +++---- include/linux/udp.h | 4 + include/net/tcp.h | 18 +- net/core/skmsg.c | 55 +++++ net/core/sock_map.c | 160 ++++++++++---- net/ipv4/Makefile | 1 + net/ipv4/tcp_bpf.c | 169 +++------------ net/ipv4/udp_bpf.c | 53 +++++ .../bpf/prog_tests/select_reuseport.c | 6 - .../selftests/bpf/prog_tests/sockmap_listen.c | 204 +++++++++++++----- 12 files changed, 465 insertions(+), 284 deletions(-) create mode 100644 net/ipv4/udp_bpf.c