Hash support for sock
Subsystems (9)
John Fastabend
reviewerv5 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
v3: move tool updates into test patch, move bpftool updates into
its own patch, and fixup the test patch stats to catch the renamed file and provide only diffs +/- on that.
v4: Add documentation to UAPI bpf.h
v5: Add documentation to tools UAPI bpf.h
Just a note I pushed Dave's Acks through v4 into v5 due to small size of change.
John Fastabend (4):
bpf: sockmap, refactor sockmap routines to work with hashmap
bpf: sockmap, add hash map support
bpf: bpftool, support for sockhash
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 +-
.../{test_sockmap_kern.c => test_sockmap_kern.h} | 6 +-
15 files changed, 695 insertions(+), 115 deletions(-)
create mode 100644 tools/testing/selftests/bpf/test_sockhash_kern.c
rename tools/testing/selftests/bpf/{test_sockmap_kern.c => test_sockmap_kern.h} (98%)Signature · 1 line
1.9.1
John Fastabend
reviewerPATCH v5 1/4 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
@@ -515,9 +515,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
@@ -2079,9 +2079,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 ; }
@@ -2089,16 +2090,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 = {
@@ -2118,25 +2111,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 = {
Signature · 1 line
1.9.1
John Fastabend
reviewerPATCH v5 2/4 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 | 52 ++++- kernel/bpf/core.c | 1 + kernel/bpf/sockmap.c | 494 ++++++++++++++++++++++++++++++++++++++++++++-- kernel/bpf/verifier.c | 14 +- net/core/filter.c | 58 ++++++ 7 files changed, 610 insertions(+), 18 deletions(-)
include/linux/bpf.h +8 -0
@@ -668,6 +668,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)
@@ -675,6 +676,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)
@@ -724,6 +731,7 @@ static inline void __xsk_map_flush(struct bpf_map *map)extern const struct bpf_func_proto bpf_get_stackid_proto;extern const struct bpf_func_proto bpf_get_stack_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)#if defined(CONFIG_XDP_SOCKETS )
include/uapi/linux/bpf.h +51 -1
70 lines not on this page · raw message
kernel/bpf/core.c +1 -0
@@ -1707,6 +1707,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
@@ -2093,6 +2093,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 ; }
@@ -2130,11 +2137,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
Signature · 1 line
1.9.1
John Fastabend
reviewerPATCH v5 3/4 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/include/uapi/linux/bpf.h | 52 +++++++++++++++++++++-
tools/testing/selftests/bpf/Makefile | 2 +-
tools/testing/selftests/bpf/test_sockhash_kern.c | 4 ++
tools/testing/selftests/bpf/test_sockmap.c | 27 ++++++++---
.../{test_sockmap_kern.c => test_sockmap_kern.h} | 10 ++---
5 files changed, 81 insertions(+), 14 deletions(-)
create mode 100644 tools/testing/selftests/bpf/test_sockhash_kern.c
rename tools/testing/selftests/bpf/{test_sockmap_kern.c => test_sockmap_kern.h} (97%)tools/include/uapi/linux/bpf.h +51 -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 {
@@ -1825,6 +1826,52 @@ struct bpf_stack_build_id {* Return * 0 on success, or a negative error in case of failure. * +* int bpf_sock_hash_update(struct bpf_sock_ops_kern *skops, struct bpf_map *map, void *key, u64 flags) +* Description +* Add an entry to, or update a sockhash *map* referencing sockets. +* The *skops* is used as a new value for the entry associated to +* *key*. *flags* is one of: +* +* **BPF_NOEXIST** +* The entry for *key* must not exist in the map. +* **BPF_EXIST** +* The entry for *key* must already exist in the map. +* **BPF_ANY** +* No condition on the existence of the entry for *key*. +* +* If the *map* has eBPF programs (parser and verdict), those will +* be inherited by the socket being added. If the socket is +* already attached to eBPF programs, this results in an error. +* Return +* 0 on success, or a negative error in case of failure. +* +* int bpf_msg_redirect_hash(struct sk_msg_buff *msg, struct bpf_map *map, void *key, u64 flags) +* Description +* This helper is used in programs implementing policies at the +* socket level. If the message *msg* is allowed to pass (i.e. if +* the verdict eBPF program returns **SK_PASS**), redirect it to +* the socket referenced by *map* (of type +* **BPF_MAP_TYPE_SOCKHASH**) using hash *key*. Both ingress and +* egress interfaces can be used for redirection. The +* **BPF_F_INGRESS** value in *flags* is used to make the +* distinction (ingress path is selected if the flag is present, +* egress path otherwise). This is the only flag supported for now. +* Return +* **SK_PASS** on success, or **SK_DROP** on error. +* +* int bpf_sk_redirect_hash(struct sk_buff *skb, struct bpf_map *map, void *key, u64 flags) +* Description +* This helper is used in programs implementing policies at the +* skb socket level. If the sk_buff *skb* is allowed to pass (i.e. +* if the verdeict eBPF program returns **SK_PASS**), redirect it +* to the socket referenced by *map* (of type +* **BPF_MAP_TYPE_SOCKHASH**) using hash *key*. Both ingress and +* egress interfaces can be used for redirection. The +* **BPF_F_INGRESS** value in *flags* is used to make the +* distinction (ingress path is selected if the flag is present, +* egress otherwise). This is the only flag supported for now. +* Return +* **SK_PASS** on success, or **SK_DROP** on error. */ #define __BPF_FUNC_MAPPER (FN ) \ FN(unspec), \
@@ -1895,7 +1942,10 @@ struct bpf_stack_build_id {FN (xdp_adjust_tail), \FN (skb_get_xfrm_state), \FN (get_stack), \ -FN (skb_load_bytes_relative), +FN (skb_load_bytes_relative), \ +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
tools/testing/selftests/bpf/Makefile +1 -1
@@ -33,7 +33,7 @@ TEST_GEN_FILES = test_pkt_access.o test_xdp.o test_l4lb.o test_tcp_estats.o test 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_get_stack_rawtp.o + test_get_stack_rawtp.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.h renamed from tools/testing/selftests/bpf/test_sockmap_kern.c +5 -5
@@ -1,5 +1,5 @@ -// SPDX-License-Identifier: GPL-2.0 -// Copyright (c) 2017-2018 Covalent IO, Inc. http://covalent.io +/* SPDX-License-Identifier: GPL-2.0 */ +/* Copyright (c) 2017-2018 Covalent IO, Inc. http://covalent.io */ #include <stddef.h> #include <string.h> #include <linux/bpf.h>
@@ -36,21 +36,21 @@ }) struct bpf_map_def SEC("maps") sock_map = { - .type = BPF_MAP_TYPE_SOCKMAP, + .type = TEST_MAP_TYPE, .key_size = sizeof(int), .value_size = sizeof(int), .max_entries = 20, }; struct bpf_map_def SEC("maps") sock_map_txmsg = { - .type = BPF_MAP_TYPE_SOCKMAP, + .type = TEST_MAP_TYPE, .key_size = sizeof(int), .value_size = sizeof(int), .max_entries = 20, }; struct bpf_map_def SEC("maps") sock_map_redir = { - .type = BPF_MAP_TYPE_SOCKMAP, + .type = TEST_MAP_TYPE, .key_size = sizeof(int), .value_size = sizeof(int), .max_entries = 20,
Signature · 1 line
1.9.1
Daniel Borkmann
maintainerRe: [PATCH bpf-next v5 3/4] bpf: selftest additions for SOCKHASH
Hi John,
Quoted 8 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]>Acked-by David S. Miller <[email protected]>
[...]
Quoted 28 lines
tools/testing/selftests/bpf/Makefile +1
-1@@ -33,7 +33,7 @@ TEST_GEN_FILES = test_pkt_access.o test_xdp.o test_l4lb.o test_tcp_estats.o test 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_get_stack_rawtp.o + test_get_stack_rawtp.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 +0
-0
tools/testing/selftests/bpf/test_sockmap.c line 51
Quoted 6 lines
tools/testing/selftests/bpf/test_sockmap.c +2
-1@@ -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"
Is this testing the right thing? Shouldn't above BPF_SOCKHASH_FILENAME say "test_sockhash_kern.o" in order to select the correct BPF prog for hashmap? Seems here we're testing sock/arraymap twice.
Quoted 2 lines
tools/testing/selftests/bpf/test_sockmap.c +0
-0#define CG_PATH "/sockmap"
[...]
tools/testing/selftests/bpf/test_sockmap.c static int test_suite(void) · line 1366
Quoted 12 lines
tools/testing/selftests/bpf/test_sockmap.c +12
-0+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; +} +
Thanks,
Daniel
John Fastabend
reviewerPATCH v5 4/4 bpf: bpftool, support for sockhash
In reply to John FastabendThis adds the SOCKHASH map type to bpftools so that we get correct pretty printing.
--- tools/bpf/bpftool/map.c | 1 + 1 file changed, 1 insertion(+)
tools/bpf/bpftool/map.c +1 -0
@@ -66,6 +66,7 @@ [BPF_MAP_TYPE_DEVMAP] = "devmap", [BPF_MAP_TYPE_SOCKMAP] = "sockmap", [BPF_MAP_TYPE_CPUMAP] = "cpumap", + [BPF_MAP_TYPE_SOCKHASH] = "sockhash", };static bool map_is_per_cpu (__u32 type)
Signature · 1 line
1.9.1