Skip to content

Commit 9fed900

Browse files
jsitnickiborkmann
authored andcommitted
bpf: Allow selecting reuseport socket from a SOCKMAP/SOCKHASH
SOCKMAP & SOCKHASH now support storing references to listening sockets. Nothing keeps us from using these map types a collection of sockets to select from in BPF reuseport programs. Whitelist the map types with the bpf_sk_select_reuseport helper. The restriction that the socket has to be a member of a reuseport group still applies. Sockets in SOCKMAP/SOCKHASH that don't have sk_reuseport_cb set are not a valid target and we signal it with -EINVAL. The main benefit from this change is that, in contrast to REUSEPORT_SOCKARRAY, SOCK{MAP,HASH} don't impose a restriction that a listening socket can be just one BPF map at the same time. Signed-off-by: Jakub Sitnicki <jakub@cloudflare.com> Signed-off-by: Daniel Borkmann <daniel@iogearbox.net> Link: https://lore.kernel.org/bpf/20200218171023.844439-9-jakub@cloudflare.com
1 parent 1d59f3b commit 9fed900

2 files changed

Lines changed: 17 additions & 8 deletions

File tree

‎kernel/bpf/verifier.c‎

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3693,14 +3693,16 @@ static int check_map_func_compatibility(struct bpf_verifier_env *env,
36933693
if (func_id != BPF_FUNC_sk_redirect_map &&
36943694
func_id != BPF_FUNC_sock_map_update &&
36953695
func_id != BPF_FUNC_map_delete_elem &&
3696-
func_id != BPF_FUNC_msg_redirect_map)
3696+
func_id != BPF_FUNC_msg_redirect_map &&
3697+
func_id != BPF_FUNC_sk_select_reuseport)
36973698
goto error;
36983699
break;
36993700
case BPF_MAP_TYPE_SOCKHASH:
37003701
if (func_id != BPF_FUNC_sk_redirect_hash &&
37013702
func_id != BPF_FUNC_sock_hash_update &&
37023703
func_id != BPF_FUNC_map_delete_elem &&
3703-
func_id != BPF_FUNC_msg_redirect_hash)
3704+
func_id != BPF_FUNC_msg_redirect_hash &&
3705+
func_id != BPF_FUNC_sk_select_reuseport)
37043706
goto error;
37053707
break;
37063708
case BPF_MAP_TYPE_REUSEPORT_SOCKARRAY:
@@ -3774,7 +3776,9 @@ static int check_map_func_compatibility(struct bpf_verifier_env *env,
37743776
goto error;
37753777
break;
37763778
case BPF_FUNC_sk_select_reuseport:
3777-
if (map->map_type != BPF_MAP_TYPE_REUSEPORT_SOCKARRAY)
3779+
if (map->map_type != BPF_MAP_TYPE_REUSEPORT_SOCKARRAY &&
3780+
map->map_type != BPF_MAP_TYPE_SOCKMAP &&
3781+
map->map_type != BPF_MAP_TYPE_SOCKHASH)
37783782
goto error;
37793783
break;
37803784
case BPF_FUNC_map_peek_elem:

‎net/core/filter.c‎

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8620,6 +8620,7 @@ struct sock *bpf_run_sk_reuseport(struct sock_reuseport *reuse, struct sock *sk,
86208620
BPF_CALL_4(sk_select_reuseport, struct sk_reuseport_kern *, reuse_kern,
86218621
struct bpf_map *, map, void *, key, u32, flags)
86228622
{
8623+
bool is_sockarray = map->map_type == BPF_MAP_TYPE_REUSEPORT_SOCKARRAY;
86238624
struct sock_reuseport *reuse;
86248625
struct sock *selected_sk;
86258626

@@ -8628,12 +8629,16 @@ BPF_CALL_4(sk_select_reuseport, struct sk_reuseport_kern *, reuse_kern,
86288629
return -ENOENT;
86298630

86308631
reuse = rcu_dereference(selected_sk->sk_reuseport_cb);
8631-
if (!reuse)
8632-
/* selected_sk is unhashed (e.g. by close()) after the
8633-
* above map_lookup_elem(). Treat selected_sk has already
8634-
* been removed from the map.
8632+
if (!reuse) {
8633+
/* reuseport_array has only sk with non NULL sk_reuseport_cb.
8634+
* The only (!reuse) case here is - the sk has already been
8635+
* unhashed (e.g. by close()), so treat it as -ENOENT.
8636+
*
8637+
* Other maps (e.g. sock_map) do not provide this guarantee and
8638+
* the sk may never be in the reuseport group to begin with.
86358639
*/
8636-
return -ENOENT;
8640+
return is_sockarray ? -ENOENT : -EINVAL;
8641+
}
86378642

86388643
if (unlikely(reuse->reuseport_id != reuse_kern->reuseport_id)) {
86398644
struct sock *sk;

0 commit comments

Comments
 (0)