Hash support for sock
Subsystems (9)
John Fastabend
reviewerv2 cover Hash support for sock
In the original sockmap implementation we got away with using an array similar to devmap. However, unlike devmap where an ifindex has a nice 1:1 function into the map we have found some use cases with sockets that need to be referenced using longer keys.
This series adds support for a sockhash map reusing as much of the sockmap code as possible. I made the decision to add sockhash specific helpers vs trying to generalize the existing helpers because (a) they have sockmap in the name and (b) the keys are different types. I prefer to be explicit here rather than play type games or do something else tricky.
To test this we duplicate all the sockmap testing except swap out the sockmap with a sockhash.
v2: fix file stats and add v2 tag
---
John Fastabend (3):
bpf: sockmap, refactor sockmap routines to work with hashmap
bpf: sockmap, add hash map support
bpf: selftest additions for SOCKHASH
include/linux/bpf.h | 8 include/linux/bpf_types.h | 1 include/linux/filter.h | 3 include/net/tcp.h | 3 include/uapi/linux/bpf.h | 6 kernel/bpf/core.c | 1 kernel/bpf/sockmap.c | 638 +++++++++++++++++++--- kernel/bpf/verifier.c | 14 net/core/filter.c | 89 ++- tools/bpf/bpftool/map.c | 1 tools/include/uapi/linux/bpf.h | 6 tools/testing/selftests/bpf/Makefile | 3 tools/testing/selftests/bpf/test_sockhash_kern.c | 4 tools/testing/selftests/bpf/test_sockmap.c | 27 + tools/testing/selftests/bpf/test_sockmap_kern.c | 340 ------------ tools/testing/selftests/bpf/test_sockmap_kern.h | 340 ++++++++++++ 16 files changed, 1034 insertions(+), 450 deletions(-) create mode 100644 tools/testing/selftests/bpf/test_sockhash_kern.c create mode 100644 tools/testing/selftests/bpf/test_sockmap_kern.h
John Fastabend
reviewerPATCH v2 1/3 bpf: sockmap, refactor sockmap routines to work with hashmap
This patch only refactors the existing sockmap code. This will allow much of the psock initialization code path and bpf helper codes to work for both sockmap bpf map types that are backed by an array, the currently supported type, and the new hash backed bpf map type sockhash.
Most the fallout comes from three changes,
- Pushing bpf programs into an independent structure so we can use it from the htab struct in the next patch.
- Generalizing helpers to use void *key instead of the hardcoded
u32.
- Instead of passing map/key through the metadata we now do the lookup inline. This avoids storing the key in the metadata which will be useful when keys can be longer than 4 bytes. We rename the sk pointers to sk_redir at this point as well to avoid any confusion between the current sk pointer and the redirect pointer sk_redir.
--- include/linux/filter.h | 3 - include/net/tcp.h | 3 - kernel/bpf/sockmap.c | 148 +++++++++++++++++++++++++++++------------------- net/core/filter.c | 31 +++------- 4 files changed, 98 insertions(+), 87 deletions(-)
include/linux/filter.h +1 -2
@@ -512,9 +512,8 @@ struct sk_msg_buff {int sg_end;struct scatterlist sg_data[MAX_SKB_FRAGS ];bool sg_copy[MAX_SKB_FRAGS ]; -__u32 key;__u32 flags; -struct bpf_map *map; +struct sock *sk_redir;struct sk_buff *skb;struct list_head list; };
include/net/tcp.h +1 -2
@@ -814,9 +814,8 @@ struct tcp_skb_cb {#endif } header;/* For incoming skbs */ struct { -__u32 key;__u32 flags; -struct bpf_map *map; +struct sock *sk_redir;void *data_end; } bpf; };
kernel/bpf/sockmap.c +88 -60
281 lines not on this page · raw message
net/core/filter.c +8 -23
@@ -1860,9 +1860,10 @@ int skb_do_redirect(struct sk_buff *skb)if (unlikely (flags & ~(BPF_F_INGRESS )))return SK_DROP ; - tcb->bpf.key = key; tcb->bpf.flags = flags; - tcb->bpf.map = map; + tcb->bpf.sk_redir =__sock_map_lookup_elem (map, key); +if (!tcb->bpf.sk_redir) +return SK_DROP ;return SK_PASS ; }
@@ -1870,16 +1871,8 @@ int skb_do_redirect(struct sk_buff *skb)struct sock *do_sk_redirect_map (struct sk_buff *skb) {struct tcp_skb_cb *tcb =TCP_SKB_CB (skb); -struct sock *sk =NULL ; - -if (tcb->bpf.map) { - sk =__sock_map_lookup_elem (tcb->bpf.map, tcb->bpf.key); - tcb->bpf.key =0 ; - tcb->bpf.map =NULL ; - } - -return sk; +return tcb->bpf.sk_redir; }static const struct bpf_func_proto bpf_sk_redirect_map_proto = {
@@ -1899,25 +1892,17 @@ struct sock *do_sk_redirect_map(struct sk_buff *skb)if (unlikely (flags & ~(BPF_F_INGRESS )))return SK_DROP ; - msg->key = key; msg->flags = flags; - msg->map = map; + msg->sk_redir =__sock_map_lookup_elem (map, key); +if (!msg->sk_redir) +return SK_DROP ;return SK_PASS ; }struct sock *do_msg_redirect_map (struct sk_msg_buff *msg) { -struct sock *sk =NULL ; - -if (msg->map) { - sk =__sock_map_lookup_elem (msg->map, msg->key); - - msg->key =0 ; - msg->map =NULL ; - } - -return sk; +return msg->sk_redir; }static const struct bpf_func_proto bpf_msg_redirect_map_proto = {
John Fastabend
reviewerPATCH v2 2/3 bpf: sockmap, add hash map support
In reply to John FastabendSockmap is currently backed by an array and enforces keys to be four bytes. This works well for many use cases and was originally modeled after devmap which also uses four bytes keys. However, this has become limiting in larger use cases where a hash would be more appropriate. For example users may want to use the 5-tuple of the socket as the lookup key.
To support this add hash support.
--- include/linux/bpf.h | 8 + include/linux/bpf_types.h | 1 include/uapi/linux/bpf.h | 6 kernel/bpf/core.c | 1 kernel/bpf/sockmap.c | 494 +++++++++++++++++++++++++++++++++++++++- kernel/bpf/verifier.c | 14 + net/core/filter.c | 58 +++++ tools/bpf/bpftool/map.c | 1 tools/include/uapi/linux/bpf.h | 6 9 files changed, 570 insertions(+), 19 deletions(-)
include/linux/bpf.h +8 -0
@@ -661,6 +661,7 @@ static inline void bpf_map_offload_map_free(struct bpf_map *map)#if defined(CONFIG_STREAM_PARSER ) && defined(CONFIG_BPF_SYSCALL ) && defined(CONFIG_INET )struct sock *__sock_map_lookup_elem (struct bpf_map *map,u32 key); +struct sock *__sock_hash_lookup_elem (struct bpf_map *map,void *key);int sock_map_prog (struct bpf_map *map,struct bpf_prog *prog,u32 type);#else static inline struct sock *__sock_map_lookup_elem (struct bpf_map *map,u32 key)
@@ -668,6 +669,12 @@ static inline struct sock *__sock_map_lookup_elem(struct bpf_map *map, u32 key)return NULL ; } +static inline struct sock *__sock_hash_lookup_elem (struct bpf_map *map, +void *key) +{ +return NULL ; +} +static inline int sock_map_prog (struct bpf_map *map,struct bpf_prog *prog,u32 type)
@@ -693,6 +700,7 @@ static inline int sock_map_prog(struct bpf_map *map,extern const struct bpf_func_proto bpf_skb_vlan_pop_proto;extern const struct bpf_func_proto bpf_get_stackid_proto;extern const struct bpf_func_proto bpf_sock_map_update_proto; +extern const struct bpf_func_proto bpf_sock_hash_update_proto;/* Shared helpers among cBPF and eBPF. */ void bpf_user_rnd_init_once (void );
include/linux/bpf_types.h +1 -0
@@ -47,6 +47,7 @@ BPF_MAP_TYPE(BPF_MAP_TYPE_DEVMAP, dev_map_ops)#if defined(CONFIG_STREAM_PARSER ) && defined(CONFIG_INET ) BPF_MAP_TYPE(BPF_MAP_TYPE_SOCKMAP, sock_map_ops) +BPF_MAP_TYPE(BPF_MAP_TYPE_SOCKHASH, sock_hash_ops)#endif BPF_MAP_TYPE(BPF_MAP_TYPE_CPUMAP, cpu_map_ops)#endif
include/uapi/linux/bpf.h +5 -1
@@ -116,6 +116,7 @@ enum bpf_map_type {BPF_MAP_TYPE_DEVMAP ,BPF_MAP_TYPE_SOCKMAP ,BPF_MAP_TYPE_CPUMAP , + BPF_MAP_TYPE_SOCKHASH, }; enum bpf_prog_type {
@@ -1835,7 +1836,10 @@ struct bpf_stack_build_id {FN (msg_pull_data), \FN (bind), \FN (xdp_adjust_tail), \ -FN (skb_get_xfrm_state), +FN (skb_get_xfrm_state), \ +FN (sock_hash_update), \ +FN (msg_redirect_hash), \ +FN (sk_redirect_hash),/* integer value in 'imm' field of BPF_CALL instruction selects which helper * function eBPF program intends to call
kernel/bpf/core.c +1 -0
@@ -1782,6 +1782,7 @@ void bpf_user_rnd_init_once(void)const struct bpf_func_proto bpf_get_current_uid_gid_proto__weak ;const struct bpf_func_proto bpf_get_current_comm_proto__weak ;const struct bpf_func_proto bpf_sock_map_update_proto__weak ; +const struct bpf_func_proto bpf_sock_hash_update_proto__weak ;const struct bpf_func_proto *__weak bpf_get_trace_printk_proto (void ) {
kernel/bpf/sockmap.c +479 -15
588 lines not on this page · raw message
kernel/bpf/verifier.c +12 -2
@@ -2078,6 +2078,13 @@ static int check_map_func_compatibility(struct bpf_verifier_env *env, func_id != BPF_FUNC_msg_redirect_map) goto error;break ; +case BPF_MAP_TYPE_SOCKHASH : +if (func_id != BPF_FUNC_sk_redirect_hash && + func_id != BPF_FUNC_sock_hash_update && + func_id != BPF_FUNC_map_delete_elem && + func_id != BPF_FUNC_msg_redirect_hash) + goto error; +break ;default :break ; }
@@ -2114,11 +2121,14 @@ static int check_map_func_compatibility(struct bpf_verifier_env *env,break ;case BPF_FUNC_sk_redirect_map:case BPF_FUNC_msg_redirect_map: +case BPF_FUNC_sock_map_update:if (map->map_type !=BPF_MAP_TYPE_SOCKMAP ) goto error;break ; -case BPF_FUNC_sock_map_update: -if (map->map_type !=BPF_MAP_TYPE_SOCKMAP ) +case BPF_FUNC_sk_redirect_hash: +case BPF_FUNC_msg_redirect_hash: +case BPF_FUNC_sock_hash_update: +if (map->map_type !=BPF_MAP_TYPE_SOCKHASH ) goto error;break ;default :
net/core/filter.c +58 -0
88 lines not on this page · raw message
tools/bpf/bpftool/map.c +1 -0
@@ -67,6 +67,7 @@ [BPF_MAP_TYPE_DEVMAP] = "devmap", [BPF_MAP_TYPE_SOCKMAP] = "sockmap", [BPF_MAP_TYPE_CPUMAP] = "cpumap", + [BPF_MAP_TYPE_SOCKHASH] = "sockhash", };static unsigned int get_possible_cpus (void )
tools/include/uapi/linux/bpf.h +5 -1
@@ -116,6 +116,7 @@ enum bpf_map_type {BPF_MAP_TYPE_DEVMAP ,BPF_MAP_TYPE_SOCKMAP ,BPF_MAP_TYPE_CPUMAP , + BPF_MAP_TYPE_SOCKHASH, }; enum bpf_prog_type {
@@ -1835,7 +1836,10 @@ struct bpf_stack_build_id {FN (msg_pull_data), \FN (bind), \FN (xdp_adjust_tail), \ -FN (skb_get_xfrm_state), +FN (skb_get_xfrm_state), \ +FN (sock_hash_update), \ +FN (msg_redirect_hash), \ +FN (sk_redirect_hash),/* integer value in 'imm' field of BPF_CALL instruction selects which helper * function eBPF program intends to call
Re: [bpf-next PATCH v2 2/3] bpf: sockmap, add hash map support
Quoted 10 lines · John Fastabend
Sockmap is currently backed by an array and enforces keys to be four bytes. This works well for many use cases and was originally modeled after devmap which also uses four bytes keys. However, this has become limiting in larger use cases where a hash would be more appropriate. For example users may want to use the 5-tuple of the socket as the lookup key.
To support this add hash support.
Signed-off-by John Fastabend <[email protected]>
Quoted 11 lines
--- include/linux/bpf.h | 8 + include/linux/bpf_types.h | 1 include/uapi/linux/bpf.h | 6 kernel/bpf/core.c | 1 kernel/bpf/sockmap.c | 494 +++++++++++++++++++++++++++++++++++++++- kernel/bpf/verifier.c | 14 + net/core/filter.c | 58 +++++ tools/bpf/bpftool/map.c | 1 tools/include/uapi/linux/bpf.h | 6 9 files changed, 570 insertions(+), 19 deletions(-)
please split tools/* update into separate commit.
Also add man-page style documentation for new helpers to uapi/bpf.h
John Fastabend
reviewerPATCH v2 3/3 bpf: selftest additions for SOCKHASH
In reply to John FastabendThis runs existing SOCKMAP tests with SOCKHASH map type. To do this we push programs into include file and build two BPF programs. One for SOCKHASH and one for SOCKMAP.
We then run the entire test suite with each type.
--- tools/testing/selftests/bpf/Makefile | 3 tools/testing/selftests/bpf/test_sockhash_kern.c | 4 tools/testing/selftests/bpf/test_sockmap.c | 27 +- tools/testing/selftests/bpf/test_sockmap_kern.c | 340 ---------------------- tools/testing/selftests/bpf/test_sockmap_kern.h | 340 ++++++++++++++++++++++ 5 files changed, 368 insertions(+), 346 deletions(-) create mode 100644 tools/testing/selftests/bpf/test_sockhash_kern.c create mode 100644 tools/testing/selftests/bpf/test_sockmap_kern.h
tools/testing/selftests/bpf/Makefile +2 -1
@@ -32,7 +32,8 @@ TEST_GEN_FILES = test_pkt_access.o test_xdp.o test_l4lb.o test_tcp_estats.o test test_l4lb_noinline.o test_xdp_noinline.o test_stacktrace_map.o \ sample_map_ret0.o test_tcpbpf_kern.o test_stacktrace_build_id.o \ sockmap_tcp_msg_prog.o connect4_prog.o connect6_prog.o test_adjust_tail.o \ - test_btf_haskv.o test_btf_nokv.o test_sockmap_kern.o test_tunnel_kern.o + test_btf_haskv.o test_btf_nokv.o test_sockmap_kern.o test_tunnel_kern.o \ + test_sockmap_kern.o test_sockhash_kern.o # Order correspond to 'make run_tests' order TEST_PROGS := test_kmod.sh \
tools/testing/selftests/bpf/test_sockhash_kern.c new file +4 -0
@@ -0,0 +1,4 @@ +// SPDX-License-Identifier: GPL-2.0 +// Copyright (c) 2018 Covalent IO, Inc. http://covalent.io +#define TEST_MAP_TYPE BPF_MAP_TYPE_SOCKHASH +#include "./test_sockmap_kern.h"
tools/testing/selftests/bpf/test_sockmap.c +20 -7
@@ -47,7 +47,8 @@#define S1_PORT 10000#define S2_PORT 10001 -#define BPF_FILENAME "test_sockmap_kern.o" +#define BPF_SOCKMAP_FILENAME "test_sockmap_kern.o" +#define BPF_SOCKHASH_FILENAME "test_sockmap_kern.o"#define CG_PATH "/sockmap"/* global sockets */
@@ -1260,9 +1261,8 @@ static int test_start_end(int cgrp) BPF_PROG_TYPE_SK_MSG, }; -static int populate_progs (void ) +static int populate_progs (char *bpf_file) { -char *bpf_file =BPF_FILENAME ;struct bpf_program *prog;struct bpf_object *obj;int i =0 ;
@@ -1306,11 +1306,11 @@ static int populate_progs(void)return 0 ; } -static int test_suite (void ) +static int __test_suite (char *bpf_file) {int cg_fd, err; - err =populate_progs (); + err =populate_progs (bpf_file);if (err <0 ) {fprintf (stderr,"ERROR: (%i) load bpf failed\n" , err);return err;
@@ -1347,17 +1347,30 @@ static int test_suite(void) out:printf ("Summary: %i PASSED %i FAILED\n" , passed, failed); +cleanup_cgroup_environment ();close (cg_fd);return err; } +static int test_suite (void ) +{ +int err; + + err =__test_suite (BPF_SOCKMAP_FILENAME ); +if (err) + goto out; + err =__test_suite (BPF_SOCKHASH_FILENAME ); +out: +return err; +} +int main (int argc,char **argv) {struct rlimit r = {10 *1024 *1024 ,RLIM_INFINITY };int iov_count =1 , length =1024 , rate =1 ;struct sockmap_options options = {0 };int opt, longindex, err, cg_fd =0 ; -char *bpf_file =BPF_FILENAME ; +char *bpf_file =BPF_SOCKMAP_FILENAME ;int test =PING_PONG ;if (setrlimit (RLIMIT_MEMLOCK , &r)) {
@@ -1438,7 +1451,7 @@ int main(int argc, char **argv)return -1 ; } - err =populate_progs (); + err =populate_progs (bpf_file);if (err) {fprintf (stderr,"populate program: (%s) %s\n" , bpf_file,strerror (errno));
tools/testing/selftests/bpf/test_sockmap_kern.c +2 -338
342 lines not on this page · raw message
tools/testing/selftests/bpf/test_sockmap_kern.h new file +340 -0
340 lines not on this page · raw message
Re: [bpf-next PATCH v2 3/3] bpf: selftest additions for SOCKHASH
Quoted 7 lines · John Fastabend
This runs existing SOCKMAP tests with SOCKHASH map type. To do this we push programs into include file and build two BPF programs. One for SOCKHASH and one for SOCKMAP.
We then run the entire test suite with each type.
Signed-off-by John Fastabend <[email protected]>
Quoted 9 lines
--- tools/testing/selftests/bpf/Makefile | 3 tools/testing/selftests/bpf/test_sockhash_kern.c | 4 tools/testing/selftests/bpf/test_sockmap.c | 27 +- tools/testing/selftests/bpf/test_sockmap_kern.c | 340 ---------------------- tools/testing/selftests/bpf/test_sockmap_kern.h | 340 ++++++++++++++++++++++ 5 files changed, 368 insertions(+), 346 deletions(-) create mode 100644 tools/testing/selftests/bpf/test_sockhash_kern.c create mode 100644 tools/testing/selftests/bpf/test_sockmap_kern.h
Looks like it was mainly a rename of test_sockmap_kern.c into .h but commit doesn't show it as such.
Can you redo it with 'git mv' ?
John Fastabend
reviewerRe: [bpf-next PATCH v2 3/3] bpf: selftest additions for SOCKHASH
Quoted 18 lines · Alexei Starovoitov
Quoted 7 lines · John Fastabend
This runs existing SOCKMAP tests with SOCKHASH map type. To do this we push programs into include file and build two BPF programs. One for SOCKHASH and one for SOCKMAP.
We then run the entire test suite with each type.
Signed-off-by John Fastabend <[email protected]>Quoted 9 lines
--- tools/testing/selftests/bpf/Makefile | 3 tools/testing/selftests/bpf/test_sockhash_kern.c | 4 tools/testing/selftests/bpf/test_sockmap.c | 27 +- tools/testing/selftests/bpf/test_sockmap_kern.c | 340 ---------------------- tools/testing/selftests/bpf/test_sockmap_kern.h | 340 ++++++++++++++++++++++ 5 files changed, 368 insertions(+), 346 deletions(-) create mode 100644 tools/testing/selftests/bpf/test_sockhash_kern.c create mode 100644 tools/testing/selftests/bpf/test_sockmap_kern.h
Quoted 4 lines
Looks like it was mainly a rename of
test_sockmap_kern.cinto .h but commit doesn't show it as such.
Can you redo it with 'git mv' ?
Sure, my scripts didn't have the --find-renames. Anyways should be better now in v3. Also pushed tools updates into selftest patch.
Thanks,
John