Hash support for sock
Subsystems (9)
John Fastabend
reviewerv6 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
v6: 'git add' test_sockhash_kern.c which was previously missing
but was not causing issues because of typo in test script, noticed by Daniel. After this the git format-patch -M option no longer tracks the rename of the test_sockmap_kern files for some reason. I guess the diff has exceeded some threshold.
Just a note I pushed Dave's Acks through v4 into v5 due to small size of changes.
John Fastabend (4):
bpf: sockmap, refactor sockmap routines to work with hashmap
bpf: sockmap, add hash map support
bpf: selftest additions for SOCKHASH
bpf: bpftool, support 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 | 52 +- 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 | 52 +- tools/testing/selftests/bpf/Makefile | 2 +- tools/testing/selftests/bpf/bpf_helpers.h | 8 + tools/testing/selftests/bpf/test_sockhash_kern.c | 5 + tools/testing/selftests/bpf/test_sockmap.c | 27 +- tools/testing/selftests/bpf/test_sockmap_kern.c | 343 +----------- tools/testing/selftests/bpf/test_sockmap_kern.h | 363 +++++++++++++ 17 files changed, 1159 insertions(+), 451 deletions(-) create mode 100644 tools/testing/selftests/bpf/test_sockhash_kern.c create mode 100644 tools/testing/selftests/bpf/test_sockmap_kern.h
Signature · 1 line
1.9.1
John Fastabend
reviewerPATCH v6 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
@@ -2083,9 +2083,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 ; }
@@ -2093,16 +2094,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 = {
@@ -2122,25 +2115,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
Daniel Borkmann
maintainerRe: [PATCH bpf-next v6 1/4] bpf: sockmap, refactor sockmap routines to work with hashmap
On 05/14/2018 07:00 PM, John Fastabend wrote:
[...]
Quoted 6 lines
kernel/bpf/sockmap.c +5
-0+static int __sock_map_ctx_update_elem (struct bpf_map *map, +struct bpf_sock_progs *progs, +struct sock *sock, +struct sock **map_link, +void *key) {
[...]
Quoted 27 lines
kernel/bpf/sockmap.c +5
-5* sock being added. If the sock is already attached to BPF programs * this results in an error. */ - verdict =READ_ONCE (stab->bpf_verdict); - parse =READ_ONCE (stab->bpf_parse); - tx_msg =READ_ONCE (stab->bpf_tx_msg); + verdict =READ_ONCE (progs->bpf_verdict); + parse =READ_ONCE (progs->bpf_parse); + tx_msg =READ_ONCE (progs->bpf_tx_msg);if (parse && verdict) {/* bpf prog refcnt may be zero if a concurrent attach operation @@ -1703,11 +1691,11 @@ static int sock_map_ctx_update_elem(struct bpf_sock_ops_kern *skops,* we increment the refcnt. If this is the case abort with an * error. */ - verdict =bpf_prog_inc_not_zero (stab->bpf_verdict); + verdict =bpf_prog_inc_not_zero (progs->bpf_verdict);if (IS_ERR (verdict))return PTR_ERR (verdict); - parse =bpf_prog_inc_not_zero (stab->bpf_parse); + parse =bpf_prog_inc_not_zero (progs->bpf_parse);if (IS_ERR (parse)) {bpf_prog_put (verdict);return PTR_ERR (parse);
kernel/bpf/sockmap.c static int sock_map_ctx_update_elem(struct bpf_sock_ops_kern *skops, · line 1709
Quoted 8 lines
kernel/bpf/sockmap.c +1
-1@@ -1715,7 +1703,7 @@ static int sock_map_ctx_update_elem(struct bpf_sock_ops_kern *skops, }if (tx_msg) { - tx_msg =bpf_prog_inc_not_zero (stab->bpf_tx_msg); + tx_msg =bpf_prog_inc_not_zero (progs->bpf_tx_msg);if (IS_ERR (tx_msg)) {if (verdict)bpf_prog_put (verdict);
Not directly related to the patch since it doesn't change the logic, but it feels something is not quite right here (unless I'm missing something). The code is as follows:
[...]
verdict = READ_ONCE(progs->bpf_verdict);
parse = READ_ONCE(progs->bpf_parse);
tx_msg = READ_ONCE(progs->bpf_tx_msg);
if (parse && verdict) {
/* bpf prog refcnt may be zero if a concurrent attach operation
* removes the program after the above READ_ONCE() but before
* we increment the refcnt. If this is the case abort with an
* error.
*/
verdict = bpf_prog_inc_not_zero(progs->bpf_verdict);
if (IS_ERR(verdict))
return PTR_ERR(verdict);
parse = bpf_prog_inc_not_zero(progs->bpf_parse);
if (IS_ERR(parse)) {
bpf_prog_put(verdict);
return PTR_ERR(parse);
}
}
if (tx_msg) {
tx_msg = bpf_prog_inc_not_zero(progs->bpf_tx_msg);
if (IS_ERR(tx_msg)) {
if (verdict)
bpf_prog_put(verdict);
if (parse)
bpf_prog_put(parse);
return PTR_ERR(tx_msg);
}
}[...]
As you say in the comment above the function wrt locking notes that the __sock_map_ctx_update_elem() can be called concurrently.
All operations operate on sock_map using cmpxchg and xchg operations to ensure we do not get stale references. Any reads into the map must be done with READ_ONCE() because of this.
You initially use the READ_ONCE() on the verdict/parse/tx_msg, but later on when grabbing the reference you use again progs->bpf_verdict/bpf_parse/bpf_tx_msg which would potentially refetch it, but if updates would happen concurrently e.g. to the three progs, they could be NULL in the mean-time, no? bpf_prog_inc_not_zero() would then crash. Why are not the ones used that you fetched previously via READ_ONCE() for taking the ref?
The second question I had is that verdict/parse/tx_msg are updated independently from each other and each could be NULL or non-NULL. What if, say, parse is NULL and verdict as well as tx_msg is non-NULL and the bpf_prog_inc_not_zero() on the tx_msg prog fails. Doesn't this cause a use-after-free since a ref on verdict wasn't taken earlier but the bpf_prog_put() will cause accidental misbalance/free of the progs?
It would probably help to clarify the locking comment a bit more if indeed the above should be okay as is.
Thanks,
Daniel
John Fastabend
reviewerRe: [PATCH bpf-next v6 1/4] bpf: sockmap, refactor sockmap routines to work with hashmap
Quoted 2 lines · Daniel Borkmann
On 05/14/2018 07:00 PM, John Fastabend wrote:
[...]
[...]
Quoted 8 lines
As you say in the comment above the function wrt locking notes that the
__sock_map_ctx_update_elem()can be called concurrently.All operations operate on
sock_mapusing cmpxchg and xchg operations to ensure we do not get stale references. Any reads into the map must be done withREAD_ONCE()because of this.
Quoted 6 lines
You initially use the
READ_ONCE()on the verdict/parse/tx_msg, but later on when grabbing the reference you use again progs->bpf_verdict/bpf_parse/bpf_tx_msg which would potentially refetch it, but if updates would happen concurrently e.g. to the three progs, they could be NULL in the mean-time, no?bpf_prog_inc_not_zero()would then crash. Why are not the ones used that you fetched previously viaREAD_ONCE()for taking the ref?
Nice catch. We should use the reference fetched by READ_ONCE in all cases.
Quoted 7 lines
The second question I had is that verdict/parse/tx_msg are updated independently from each other and each could be NULL or non-NULL. What if, say, parse is NULL and verdict as well as
tx_msgis non-NULL and thebpf_prog_inc_not_zero()on thetx_msgprog fails. Doesn't this cause a use-after-free since a ref on verdict wasn't taken earlier but thebpf_prog_put()will cause accidental misbalance/free of the progs?
Also good catch. I'll send patches for both now. Thanks.
Quoted 7 lines
It would probably help to clarify the locking comment a bit more if indeed the above should be okay as is.
Thanks,
Daniel
John Fastabend
reviewerPATCH v6 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
Daniel Borkmann
maintainerRe: [PATCH bpf-next v6 2/4] bpf: sockmap, add hash map support
Quoted 79 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]>Acked-by David S. Miller <[email protected]>--- 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 +1
-0@@ -118,6 +118,7 @@ enum bpf_map_type {BPF_MAP_TYPE_SOCKMAP ,BPF_MAP_TYPE_CPUMAP ,BPF_MAP_TYPE_XSKMAP , + BPF_MAP_TYPE_SOCKHASH, }; enum bpf_prog_type {
include/uapi/linux/bpf.h struct bpf_stack_build_id { · line 1859
Quoted 4 lines
include/uapi/linux/bpf.h +1
-0@@ -1855,6 +1856,52 @@ struct bpf_stack_build_id {* Egress device index on success, 0 if packet needs to continue * up the stack for further processing 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)
When you rebase please fix this up properly next time and add a newline in between the helpers. I fixed this up while applying.
Quoted 17 lines
include/uapi/linux/bpf.h +17
-0+* 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.
[...]
Quoted 27 lines
include/uapi/linux/bpf.h +27
-0+static struct bpf_map *sock_hash_alloc (union bpf_attr *attr) +{ +struct bpf_htab *htab; +int i, err; +u64 cost; + +if (!capable (CAP_NET_ADMIN )) +return ERR_PTR (-EPERM ); + +/* check sanity of attributes */ +if (attr->max_entries ==0 || attr->value_size !=4 || + attr->map_flags & ~SOCK_CREATE_FLAG_MASK ) +return ERR_PTR (-EINVAL ); + + err =bpf_tcp_ulp_register (); +if (err && err != -EEXIST ) +return ERR_PTR (err); + + htab =kzalloc (sizeof (*htab),GFP_USER ); +if (!htab) +return ERR_PTR (-ENOMEM ); + +bpf_map_init_from_attr (&htab->map, attr); + + htab->n_buckets =roundup_pow_of_two (htab->map.max_entries); + htab->elem_size =sizeof (struct htab_elem ) + +round_up (htab->map.key_size,8 );
include/uapi/linux/bpf.h
Quoted 10 lines
include/uapi/linux/bpf.h +10
-0+ +if (htab->n_buckets ==0 || + htab->n_buckets >U32_MAX /sizeof (struct bucket )) + goto free_htab; + + cost = (u64 ) htab->n_buckets *sizeof (struct bucket ) + + (u64 ) htab->elem_size * htab->map.max_entries; + +if (cost >=U32_MAX -PAGE_SIZE ) + goto free_htab;
Also above two goto free_htab were buggy! Error after bpf_tcp_ulp_register() is either 0 or -EEXIST, if it's 0, then when you bail out here this will cause a NULL pointer dereference in subsequent map setup paths. Adapting the same logic from sock_map_alloc() would have been enough. I've added a err = -EINVAL to the first occasion above this time.
Quoted 22 lines
include/uapi/linux/bpf.h +22
-0+ htab->map.pages =round_up (cost,PAGE_SIZE ) >>PAGE_SHIFT ; + err =bpf_map_precharge_memlock (htab->map.pages); +if (err) + goto free_htab; + + err = -ENOMEM ; + htab->buckets =bpf_map_area_alloc ( + htab->n_buckets *sizeof (struct bucket ), + htab->map.numa_node); +if (!htab->buckets) + goto free_htab; + +for (i =0 ; i < htab->n_buckets; i++) { +INIT_HLIST_HEAD (&htab->buckets[i].head); +raw_spin_lock_init (&htab->buckets[i].lock); + } + +return &htab->map; +free_htab: +kfree (htab); +return ERR_PTR (err); +}
Re: [PATCH bpf-next v6 2/4] bpf: sockmap, add hash map support
Quoted 78 lines · Daniel Borkmann
Quoted 77 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]>Acked-by David S. Miller <[email protected]>--- 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 +1
-0@@ -118,6 +118,7 @@ enum bpf_map_type {BPF_MAP_TYPE_SOCKMAP ,BPF_MAP_TYPE_CPUMAP ,BPF_MAP_TYPE_XSKMAP , + BPF_MAP_TYPE_SOCKHASH, };
Quoted 9 lines
include/uapi/linux/bpf.h struct bpf_stack_build_id { · line 1859
Quoted 6 lines
include/uapi/linux/bpf.h +1
-0enum bpf_prog_type {@@ -1855,6 +1856,52 @@ struct bpf_stack_build_id {* Egress device index on success, 0 if packet needs to continue * up the stack for further processing 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) When you rebase please fix this up properly next time and add a newline in between the helpers. I fixed this up while applying.
I guess the tools/include/uapi/linux/bpf.h may also need fixup to be in sync with main bpf.h.
Daniel Borkmann
maintainerRe: [PATCH bpf-next v6 2/4] bpf: sockmap, add hash map support
Quoted 2 lines · Y Song
Quoted 1 line · Daniel Borkmann
On 05/14/2018 07:00 PM, John Fastabend wrote:
[...]
Quoted 10 lines
Quoted 9 lines
include/uapi/linux/bpf.h struct bpf_stack_build_id { · line 1859
Quoted 6 lines
enum
bpf_prog_type{include/uapi/linux/bpf.h +1
-0@@ -1855,6 +1856,52 @@ struct bpf_stack_build_id {* Egress device index on success, 0 if packet needs to continue * up the stack for further processing 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) When you rebase please fix this up properly next time and add a newline in between the helpers. I fixed this up while applying.
Quoted 2 lines
I guess the
tools/include/uapi/linux/bpf.hmay also need fixup to be in sync with mainbpf.h.
Yep agree, just fixed it up, thanks!
John Fastabend
reviewerPATCH v6 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/bpf_helpers.h | 8 + tools/testing/selftests/bpf/test_sockhash_kern.c | 5 + tools/testing/selftests/bpf/test_sockmap.c | 27 +- tools/testing/selftests/bpf/test_sockmap_kern.c | 343 +-------------------- tools/testing/selftests/bpf/test_sockmap_kern.h | 363 +++++++++++++++++++++++ 7 files changed, 452 insertions(+), 348 deletions(-) create mode 100644 tools/testing/selftests/bpf/test_sockhash_kern.c create mode 100644 tools/testing/selftests/bpf/test_sockmap_kern.h
tools/include/uapi/linux/bpf.h +51 -1
@@ -118,6 +118,7 @@ enum bpf_map_type {BPF_MAP_TYPE_SOCKMAP ,BPF_MAP_TYPE_CPUMAP ,BPF_MAP_TYPE_XSKMAP , + BPF_MAP_TYPE_SOCKHASH, }; enum bpf_prog_type {
@@ -1855,6 +1856,52 @@ struct bpf_stack_build_id {* Egress device index on success, 0 if packet needs to continue * up the stack for further processing 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), \
@@ -1926,7 +1973,10 @@ struct bpf_stack_build_id {FN (skb_get_xfrm_state), \FN (get_stack), \FN (skb_load_bytes_relative), \ -FN (fib_lookup), +FN (fib_lookup), \ +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/bpf_helpers.h +8 -0
@@ -75,9 +75,14 @@ static int (*bpf_sock_ops_cb_flags_set)(void *ctx, int flags) = (void *) BPF_FUNC_sock_ops_cb_flags_set;static int (*bpf_sk_redirect_map)(void *ctx,void *map,int key,int flags) = (void *) BPF_FUNC_sk_redirect_map; +static int (*bpf_sk_redirect_hash)(void *ctx,void *map,void *key,int flags) = + (void *) BPF_FUNC_sk_redirect_hash;static int (*bpf_sock_map_update)(void *map,void *key,void *value,unsigned long long flags) = (void *) BPF_FUNC_sock_map_update; +static int (*bpf_sock_hash_update)(void *map,void *key,void *value, +unsigned long long flags) = + (void *) BPF_FUNC_sock_hash_update;static int (*bpf_perf_event_read_value)(void *map,unsigned long long flags,void *buf,unsigned int buf_size) = (void *) BPF_FUNC_perf_event_read_value;
@@ -88,6 +93,9 @@ static int (*bpf_override_return)(void *ctx, unsigned long rc) = (void *) BPF_FUNC_override_return;static int (*bpf_msg_redirect_map)(void *ctx,void *map,int key,int flags) = (void *) BPF_FUNC_msg_redirect_map; +static int (*bpf_msg_redirect_hash)(void *ctx, +void *map,void *key,int flags) = + (void *) BPF_FUNC_msg_redirect_hash;static int (*bpf_msg_apply_bytes)(void *ctx,int len) = (void *) BPF_FUNC_msg_apply_bytes; static int (*bpf_msg_cork_bytes)(void *ctx, int len) =
tools/testing/selftests/bpf/test_sockhash_kern.c new file +5 -0
@@ -0,0 +1,5 @@ +// SPDX-License-Identifier: GPL-2.0 +// Copyright (c) 2018 Covalent IO, Inc. http://covalent.io +#undef SOCKMAP +#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_sockhash_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 +4 -339
344 lines not on this page · raw message
tools/testing/selftests/bpf/test_sockmap_kern.h new file +363 -0
363 lines not on this page · raw message
Signature · 1 line
1.9.1
John Fastabend
reviewerPATCH v6 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
Quoted 27 lines · John Fastabend
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 UAPIbpf.h
v5: Add documentation to tools UAPIbpf.h
v6: 'git add'test_sockhash_kern.cwhich was previously missing
but was not causing issues because of typo in test script, noticed by Daniel. After this the git format-patch -M option no longer tracks the rename of thetest_sockmap_kernfiles for some reason. I guess the diff has exceeded some threshold.
Quoted 8 lines
Just a note I pushed Dave's Acks through v4 into v5 due to small size of changes.
John Fastabend (4):
bpf: sockmap, refactor sockmap routines to work with hashmap
bpf: sockmap, add hash map support
bpf: selftest additions for SOCKHASH
bpf: bpftool, support for sockhash
Applied to bpf-next, thanks John! Couple of comments in the patches.