Introduce BPF_MAP_TYPE_REUSEPORT_SOCKARRAY and BPF_PROG_TYPE_SK_REUSEPORT
Subsystems (12)
cover Introduce BPF_MAP_TYPE_REUSEPORT_SOCKARRAY and BPF_PROG_TYPE_SK_REUSEPORT
This series introduces a new map type "BPF_MAP_TYPE_REUSEPORT_SOCKARRAY" and a new prog type BPF_PROG_TYPE_SK_REUSEPORT.
Here is a snippet from a commit message:
"To unleash the full potential of a bpf prog, it is essential for the userspace to be capable of directly setting up a bpf map which can then be consumed by the bpf prog to make decision. In this case, decide which SO_REUSEPORT sk to serve the incoming request.
By adding BPF_MAP_TYPE_REUSEPORT_SOCKARRAY, the userspace has total control and visibility on where a SO_REUSEPORT sk should be located in a bpf map. The later patch will introduce BPF_PROG_TYPE_SK_REUSEPORT such that the bpf prog can directly select a sk from the bpf map. That will raise the programmability of the bpf prog attached to a reuseport group (a group of sk serving the same IP:PORT).
For example, in UDP, the bpf prog can peek into the payload (e.g. through the "data" pointer introduced in the later patch) to learn the application level's connection information and then decide which sk to pick from a bpf map. The userspace can tightly couple the sk's location in a bpf map with the application logic in generating the UDP payload's connection information. This connection info contact/API stays within the userspace.
Also, when used with map-in-map, the userspace can switch the old-server-process's inner map to a new-server-process's inner map in one call "bpf_map_update_elem(outer_map, &index, &new_reuseport_array)". The bpf prog will then direct incoming requests to the new process instead of the old process. The old process can finish draining the pending requests (e.g. by "accept()") before closing the old-fds. [Note that deleting a fd from a bpf map does not necessary mean the fd is closed]"
Please see individual patch for details
Martin KaFai Lau (9):
tcp: Avoid TCP syncookie rejected by SO_REUSEPORT socket
net: Add ID (if needed) to sock_reuseport and expose reuseport_lock
bpf: Introduce BPF_MAP_TYPE_REUSEPORT_SOCKARRAY
bpf: Introduce BPF_PROG_TYPE_SK_REUSEPORT
bpf: Enable BPF_PROG_TYPE_SK_REUSEPORT bpf prog in reuseport selection
bpf: Refactor ARRAY_SIZE macro to bpf_util.h
bpf: Sync bpf.h uapi to tools/
bpf: test BPF_MAP_TYPE_REUSEPORT_SOCKARRAY
bpf: Test BPF_PROG_TYPE_SK_REUSEPORT
include/linux/bpf.h | 28 + include/linux/bpf_types.h | 6 + include/linux/filter.h | 16 + include/net/addrconf.h | 1 + include/net/sock_reuseport.h | 19 +- include/net/tcp.h | 30 +- include/uapi/linux/bpf.h | 37 +- kernel/bpf/Makefile | 3 + kernel/bpf/arraymap.c | 2 +- kernel/bpf/reuseport_array.c | 363 +++++++++ kernel/bpf/syscall.c | 6 + kernel/bpf/verifier.c | 9 + net/core/filter.c | 354 ++++++++- net/core/sock_reuseport.c | 92 ++- net/ipv4/inet_connection_sock.c | 9 + net/ipv4/inet_hashtables.c | 19 +- net/ipv4/udp.c | 9 +- net/ipv6/inet6_hashtables.c | 14 +- net/ipv6/udp.c | 4 + tools/include/uapi/linux/bpf.h | 37 +- tools/lib/bpf/bpf.c | 1 + tools/lib/bpf/bpf.h | 1 + tools/lib/bpf/libbpf.c | 1 + tools/testing/selftests/bpf/Makefile | 4 +- tools/testing/selftests/bpf/bpf_helpers.h | 4 + tools/testing/selftests/bpf/bpf_util.h | 4 + tools/testing/selftests/bpf/test_align.c | 5 +- tools/testing/selftests/bpf/test_btf.c | 5 +- tools/testing/selftests/bpf/test_maps.c | 262 ++++++- .../selftests/bpf/test_select_reuseport.c | 688 ++++++++++++++++++ .../bpf/test_select_reuseport_common.h | 36 + .../bpf/test_select_reuseport_kern.c | 180 +++++ tools/testing/selftests/bpf/test_sock.c | 5 +- tools/testing/selftests/bpf/test_sock_addr.c | 5 +- tools/testing/selftests/bpf/test_verifier.c | 5 +- 35 files changed, 2167 insertions(+), 97 deletions(-) create mode 100644 kernel/bpf/reuseport_array.c create mode 100644 tools/testing/selftests/bpf/test_select_reuseport.c create mode 100644 tools/testing/selftests/bpf/test_select_reuseport_common.h create mode 100644 tools/testing/selftests/bpf/test_select_reuseport_kern.c
Signature · 1 line
2.17.1
PATCH 1/9 tcp: Avoid TCP syncookie rejected by SO_REUSEPORT socket
Although the actual cookie check "__cookie_v[46]_check()" does not involve sk specific info, it checks whether the sk has recent synq overflow event in "tcp_synq_no_recent_overflow()". The tcp_sk(sk)->rx_opt.ts_recent_stamp is updated every second when it has sent out a syncookie (through "tcp_synq_overflow()").
The above per sk "recent synq overflow event timestamp" works well for non SO_REUSEPORT use case. However, it may cause random connection request reject/discard when SO_REUSEPORT is used with syncookie because it fails the "tcp_synq_no_recent_overflow()" test.
When SO_REUSEPORT is used, it usually has multiple listening socks serving TCP connection requests destinated to the same local IP:PORT. There are cases that the TCP-ACK-COOKIE may not be received
by the same sk that sent out the syncookie. For example,
if reuse->socks[] began with {sk0, sk1},
1) sk1 sent out syncookies and tcp_sk(sk1)->rx_opt.ts_recent_stamp was updated.
2) the reuse->socks[] became {sk1, sk2} later. e.g. sk0 was first closed and then sk2 was added. Here, sk2 does not have ts_recent_stamp set. There are other ordering that will trigger the similar situation below but the idea is the same.
3) When the TCP-ACK-COOKIE comes back, sk2 was selected. "tcp_synq_no_recent_overflow(sk2)" returns true. In this case, all syncookies sent by sk1 will be handled (and rejected)
by sk2 while sk1 is still alive.
The userspace may create and remove listening SO_REUSEPORT sockets as it sees fit. E.g. Adding new thread (and SO_REUSEPORT sock) to handle incoming requests, old process stopping and new process starting...etc. With or without SO_ATTACH_REUSEPORT_[CB]BPF,
the sockets leaving and joining a reuseport group makes picking the same sk to check the syncookie very difficult (if not impossible).
The later patches will allow bpf prog more flexibility in deciding where a sk should be located in a bpf map and selecting a particular SO_REUSEPORT sock as it sees fit. e.g. Without closing any sock, replace the whole bpf reuseport_array in one map_update() by using map-in-map. Getting the syncookie check working smoothly across socks in the same "reuse->socks[]" is important.
A partial solution is to set the newly added sk's ts_recent_stamp to the max ts_recent_stamp of a reuseport group but that will require to iterate through reuse->socks[] OR
pessimistically set it to "now - TCP_SYNCOOKIE_VALID" when a sk is joining a reuseport group. However, neither of them will solve the existing sk getting moved around the reuse->socks[] and that sk may not have ts_recent_stamp updated, unlikely under continuous synflood but not impossible.
This patch opts to treat the reuseport group as a whole when considering the last synq overflow timestamp since they are serving the same IP:PORT from the userspace (and BPF program) perspective.
"synq_overflow_ts" is added to "struct sock_reuseport". The tcp_synq_overflow() and tcp_synq_no_recent_overflow() will update/check reuse->synq_overflow_ts if the sk is in a reuseport group. Similar to the reuseport decision in __inet_lookup_listener(), both sk->sk_reuseport and sk->sk_reuseport_cb are tested for SO_REUSEPORT usage. Update on "synq_overflow_ts" happens at roughly once every second.
A synflood test was done with a 16 rx-queues and 16 reuseport sockets. No meaningful performance change is observed. Before and after the change is ~9Mpps in IPv4.
--- include/net/sock_reuseport.h | 4 ++++ include/net/tcp.h | 30 ++++++++++++++++++++++++++++-- net/core/sock_reuseport.c | 1 + 3 files changed, 33 insertions(+), 2 deletions(-)
include/net/sock_reuseport.h +4 -0
@@ -12,6 +12,10 @@ struct sock_reuseport {u16 max_socks;/* length of socks */ u16 num_socks;/* elements in socks */ +/* The last synq overflow event timestamp of this +* reuse->socks[] group. +*/ +unsigned int synq_overflow_ts;struct bpf_prog __rcu *prog;/* optional BPF sock selector */ struct sock *socks[0 ];/* array of sock pointers */ };
include/net/tcp.h +28 -2
@@ -36,6 +36,7 @@#include <net/inet_hashtables.h> #include <net/checksum.h> #include <net/request_sock.h> +#include <net/sock_reuseport.h> #include <net/sock.h> #include <net/snmp.h> #include <net/ip.h>
@@ -472,9 +473,22 @@ struct sock *cookie_v4_check(struct sock *sk, struct sk_buff *skb);*/ static inline void tcp_synq_overflow (const struct sock *sk) { -unsigned int last_overflow =tcp_sk (sk)->rx_opt.ts_recent_stamp; +unsigned int last_overflow;unsigned int now = jiffies; +if (sk->sk_reuseport) { +struct sock_reuseport *reuse; + + reuse =rcu_dereference (sk->sk_reuseport_cb); +if (likely (reuse)) { + last_overflow =READ_ONCE (reuse->synq_overflow_ts); +if (time_after32 (now, last_overflow +HZ )) +WRITE_ONCE (reuse->synq_overflow_ts, now); +return ; + } + } + + last_overflow =tcp_sk (sk)->rx_opt.ts_recent_stamp;if (time_after32 (now, last_overflow +HZ ))tcp_sk (sk)->rx_opt.ts_recent_stamp = now; }
@@ -482,9 +496,21 @@ static inline void tcp_synq_overflow(const struct sock *sk)/* syncookies: no recent synqueue overflow on this listening socket? */ static inline bool tcp_synq_no_recent_overflow (const struct sock *sk) { -unsigned int last_overflow =tcp_sk (sk)->rx_opt.ts_recent_stamp; +unsigned int last_overflow;unsigned int now = jiffies; +if (sk->sk_reuseport) { +struct sock_reuseport *reuse; + + reuse =rcu_dereference (sk->sk_reuseport_cb); +if (likely (reuse)) { + last_overflow =READ_ONCE (reuse->synq_overflow_ts); +return time_after32 (now, last_overflow + +TCP_SYNCOOKIE_VALID ); + } + } + + last_overflow =tcp_sk (sk)->rx_opt.ts_recent_stamp;return time_after32 (now, last_overflow +TCP_SYNCOOKIE_VALID ); }
net/core/sock_reuseport.c +1 -0
@@ -81,6 +81,7 @@ static struct sock_reuseport *reuseport_grow(struct sock_reuseport *reuse)memcpy (more_reuse->socks, reuse->socks, reuse->num_socks *sizeof (struct sock *)); + more_reuse->synq_overflow_ts =READ_ONCE (reuse->synq_overflow_ts);for (i =0 ; i < reuse->num_socks; ++i) rcu_assign_pointer(reuse->socks[i]->sk_reuseport_cb,
Signature · 1 line
2.17.1
PATCH 2/9 net: Add ID (if needed) to sock_reuseport and expose reuseport_lock
In reply to Martin KaFai LauA later patch will introduce a BPF_MAP_TYPE_REUSEPORT_ARRAY which allows a SO_REUSEPORT sk to be added to a bpf map. When a sk is removed from reuse->socks[], it also needs to be removed from the bpf map. Also, when adding a sk to a bpf map, the bpf map needs to ensure it is indeed in a reuse->socks[]. Hence, reuseport_lock is needed by the bpf map to ensure its map_update_elem() and map_delete_elem() operations are in-sync with the reuse->socks[]. The BPF_MAP_TYPE_REUSEPORT_ARRAY map will only acquire the reuseport_lock after ensuring the adding sk is already in a reuseport group (i.e. reuse->socks[]). The map_lookup_elem() will be lockless.
This patch also adds an ID to sock_reuseport. A later patch will introduce BPF_PROG_TYPE_SK_REUSEPORT which allows
a bpf prog to select a sk from a bpf map. It is inflexible to statically enforce a bpf map can only contain the sk belonging to a particular reuse->socks[] (i.e. same IP:PORT) during the bpf verification time. For example, think about the the map-in-map situation where the inner map can be dynamically changed in runtime and the outer map may have inner maps belonging to different reuseport groups. Hence, when the bpf prog (in the new BPF_PROG_TYPE_SK_REUSEPORT type) selects a sk, this selected sk has to be checked to ensure it belongs to the requesting reuseport group (i.e. the group serving that IP:PORT).
The "sk->sk_reuseport_cb" pointer cannot be used for this checking purpose because the pointer value will change after reuseport_grow(). Instead of saving all checking conditions like the ones preced calling "reuseport_add_sock()" and compare them everytime a bpf_prog is run, a 32bits ID is introduced to survive the reuseport_grow(). The ID is only acquired if any of the reuse->socks[] is added to the newly introduced "BPF_MAP_TYPE_REUSEPORT_ARRAY" map.
If "BPF_MAP_TYPE_REUSEPORT_ARRAY" is not used, the changes in this patch is a no-op.
--- include/net/sock_reuseport.h | 6 ++++++ net/core/sock_reuseport.c | 27 ++++++++++++++++++++++++++- 2 files changed, 32 insertions(+), 1 deletion(-)
include/net/sock_reuseport.h +6 -0
@@ -5,8 +5,11 @@#include <linux/filter.h> #include <linux/skbuff.h> #include <linux/types.h> +#include <linux/spinlock.h> #include <net/sock.h> +extern spinlock_t reuseport_lock; +struct sock_reuseport {struct rcu_head rcu;
@@ -16,6 +19,8 @@ struct sock_reuseport {* reuse->socks[] group. */ unsigned int synq_overflow_ts; +/* ID stays the same even after the size of socks[] grows. */ +unsigned int reuseport_id;struct bpf_prog __rcu *prog;/* optional BPF sock selector */ struct sock *socks[0 ];/* array of sock pointers */ };
@@ -29,5 +34,6 @@ extern struct sock *reuseport_select_sock(struct sock *sk, int hdr_len);extern struct bpf_prog *reuseport_attach_prog (struct sock *sk,struct bpf_prog *prog); +int reuseport_get_id (struct sock_reuseport *reuse);#endif /* _SOCK_REUSEPORT_H */
net/core/sock_reuseport.c +26 -1
@@ -8,11 +8,33 @@#include <net/sock_reuseport.h> #include <linux/bpf.h> +#include <linux/idr.h> #include <linux/rcupdate.h> #define INIT_SOCKS 128 -static DEFINE_SPINLOCK (reuseport_lock); +DEFINE_SPINLOCK (reuseport_lock); + +#define REUSEPORT_MIN_ID 1 +static DEFINE_IDA (reuseport_ida); + +int reuseport_get_id (struct sock_reuseport *reuse) +{ +int id; + +if (reuse->reuseport_id) +return reuse->reuseport_id; + + id =ida_simple_get (&reuseport_ida,REUSEPORT_MIN_ID ,0 , +/* Called under reuseport_lock */ +GFP_ATOMIC ); +if (id <0 ) +return id; + + reuse->reuseport_id = id; + +return reuse->reuseport_id; +}static struct sock_reuseport *__reuseport_alloc (unsigned int max_socks) {
@@ -78,6 +100,7 @@ static struct sock_reuseport *reuseport_grow(struct sock_reuseport *reuse) more_reuse->max_socks = more_socks_size; more_reuse->num_socks = reuse->num_socks; more_reuse->prog = reuse->prog; + more_reuse->reuseport_id = reuse->reuseport_id;memcpy (more_reuse->socks, reuse->socks, reuse->num_socks *sizeof (struct sock *));
@@ -102,6 +125,8 @@ static void reuseport_free_rcu(struct rcu_head *head) reuse = container_of(head, struct sock_reuseport, rcu);if (reuse->prog)bpf_prog_destroy (reuse->prog); +if (reuse->reuseport_id) +ida_simple_remove (&reuseport_ida, reuse->reuseport_id);kfree (reuse); }
Signature · 1 line
2.17.1
PATCH 3/9 bpf: Introduce BPF_MAP_TYPE_REUSEPORT_SOCKARRAY
In reply to Martin KaFai LauThis patch introduces a new map type BPF_MAP_TYPE_REUSEPORT_SOCKARRAY.
To unleash the full potential of a bpf prog, it is essential for the userspace to be capable of directly setting up a bpf map which can then be consumed by the bpf prog to make decision. In this case, decide which SO_REUSEPORT sk to serve the incoming request.
By adding BPF_MAP_TYPE_REUSEPORT_SOCKARRAY, the userspace has total control and visibility on where a SO_REUSEPORT sk should be located in a bpf map. The later patch will introduce BPF_PROG_TYPE_SK_REUSEPORT such that the bpf prog can directly select a sk from the bpf map. That will raise the programmability of the bpf prog attached to a reuseport group (a group of sk serving the same IP:PORT).
For example, in UDP, the bpf prog can peek into the payload (e.g. through the "data" pointer introduced in the later patch) to learn the application level's connection information and then decide which sk to pick from a bpf map. The userspace can tightly couple the sk's location in a bpf map with the application logic in generating the UDP payload's connection information. This connection info contact/API stays within the userspace.
Also, when used with map-in-map, the userspace can switch the old-server-process's inner map to a new-server-process's inner map in one call "bpf_map_update_elem(outer_map, &index, &new_reuseport_array)". The bpf prog will then direct incoming requests to the new process instead of the old process. The old process can finish draining the pending requests (e.g. by "accept()") before closing the old-fds. [Note that deleting a fd from a bpf map does not necessary mean the fd is closed]
During map_update_elem(),
Only SO_REUSEPORT sk (i.e. which has already been added
to a reuse->socks[]) can be used. That means a SO_REUSEPORT sk that is "bind()" for UDP or "bind()+listen()" for TCP. These conditions are ensured in "reuseport_array_update_check()".
A SO_REUSEPORT sk can only be added once to a map (i.e. the same sk cannot be added twice even to the same map). SO_REUSEPORT already allows another sk to be created for the same IP:PORT. There is no need to re-create a similar usage in the BPF side.
When a SO_REUSEPORT is deleted from the "reuse->socks[]" (e.g. "close()"), it will notify the bpf map to remove it from the map also. It is done through "bpf_sk_reuseport_detach()" and it will only be called if >=1 of the "reuse->sock[]" has ever been added to a bpf map.
The map_update()/map_delete() has to be in-sync with the "reuse->socks[]". Hence, the same "reuseport_lock" used by "reuse->socks[]" has to be used here also. Care has been taken to ensure the lock is only acquired when the adding sk passes some strict tests. and
freeing the map does not require the reuseport_lock.
The reuseport_array will also support lookup from the syscall side. It will return a sock_gen_cookie(). The sock_gen_cookie() is on-demand (i.e. a sk's cookie is not generated until the very first map_lookup_elem()).
The lookup cookie is 64bits but it goes against the logical userspace expectation on 32bits sizeof(fd) (and as other fd based bpf maps do also). It may catch user in surprise if we enforce value_size=8 while userspace still pass a 32bits fd during update. Supporting different value_size between lookup and update seems unintuitive also.
We also need to consider what if other existing fd based maps want to return 64bits value from syscall's lookup in the future. Hence, reuseport_array supports both value_size 4 and 8, and assuming user will usually use value_size=4. The syscall's lookup will return ENOSPC on value_size=4. It will will only return 64bits value from sock_gen_cookie() when user consciously choose value_size=8 (as a signal that lookup is desired) which then requires a 64bits value in both lookup and update.
--- include/linux/bpf.h | 28 +++ include/linux/bpf_types.h | 3 + include/uapi/linux/bpf.h | 1 + kernel/bpf/Makefile | 3 + kernel/bpf/arraymap.c | 2 +- kernel/bpf/reuseport_array.c | 363 +++++++++++++++++++++++++++++++++++ kernel/bpf/syscall.c | 6 + net/core/sock_reuseport.c | 8 + 8 files changed, 413 insertions(+), 1 deletion(-) create mode 100644 kernel/bpf/reuseport_array.c
include/linux/bpf.h +28 -0
@@ -524,6 +524,7 @@ static inline int bpf_map_attr_numa_node(const union bpf_attr *attr) }struct bpf_prog *bpf_prog_get_type_path (const char *name,enum bpf_prog_type type); +int array_map_alloc_check (union bpf_attr *attr);#else /* !CONFIG_BPF_SYSCALL */ static inline struct bpf_prog *bpf_prog_get (u32 ufd)
@@ -769,6 +770,33 @@ static inline void __xsk_map_flush(struct bpf_map *map) }#endif +#if defined(CONFIG_INET ) && defined(CONFIG_BPF_SYSCALL ) +void bpf_sk_reuseport_detach (struct sock *sk); +int bpf_fd_reuseport_array_lookup_elem (struct bpf_map *map,void *key, +void *value); +int bpf_fd_reuseport_array_update_elem (struct bpf_map *map,void *key, +void *value,u64 map_flags); +#else +static inline void bpf_sk_reuseport_detach (struct sock *sk) +{ +} + +#ifdef CONFIG_BPF_SYSCALL +static inline int bpf_fd_reuseport_array_lookup_elem (struct bpf_map *map, +void *key,void *value) +{ +return -EOPNOTSUPP ; +} + +static inline int bpf_fd_reuseport_array_update_elem (struct bpf_map *map, +void *key,void *value, +u64 map_flags) +{ +return -EOPNOTSUPP ; +} +#endif /* CONFIG_BPF_SYSCALL */ +#endif /* defined(CONFIG_INET) && defined(CONFIG_BPF_SYSCALL) */ +/* verifier prototypes for helper functions called from eBPF programs */ extern const struct bpf_func_proto bpf_map_lookup_elem_proto;extern const struct bpf_func_proto bpf_map_update_elem_proto;
include/linux/bpf_types.h +3 -0
@@ -60,4 +60,7 @@ BPF_MAP_TYPE(BPF_MAP_TYPE_CPUMAP, cpu_map_ops)#if defined(CONFIG_XDP_SOCKETS ) BPF_MAP_TYPE(BPF_MAP_TYPE_XSKMAP, xsk_map_ops)#endif +#ifdef CONFIG_INET +BPF_MAP_TYPE(BPF_MAP_TYPE_REUSEPORT_SOCKARRAY, reuseport_array_ops) +#endif #endif
include/uapi/linux/bpf.h +1 -0
@@ -126,6 +126,7 @@ enum bpf_map_type {BPF_MAP_TYPE_XSKMAP ,BPF_MAP_TYPE_SOCKHASH ,BPF_MAP_TYPE_CGROUP_STORAGE , + BPF_MAP_TYPE_REUSEPORT_SOCKARRAY, }; enum bpf_prog_type {
kernel/bpf/Makefile +3 -0
@@ -23,3 +23,6 @@ ifeq ($(CONFIG_PERF_EVENTS),y) obj-$(CONFIG_BPF_SYSCALL) += stackmap.o endif obj-$(CONFIG_CGROUP_BPF) += cgroup.o +ifeq ($(CONFIG_INET),y) +obj-$(CONFIG_BPF_SYSCALL) += reuseport_array.o +endif
kernel/bpf/arraymap.c +1 -1
@@ -54,7 +54,7 @@ static int bpf_array_alloc_percpu(struct bpf_array *array) }/* Called from syscall */ -static int array_map_alloc_check (union bpf_attr *attr) +int array_map_alloc_check (union bpf_attr *attr) {bool percpu = attr->map_type ==BPF_MAP_TYPE_PERCPU_ARRAY ;int numa_node =bpf_map_attr_numa_node (attr);
kernel/bpf/reuseport_array.c new file +363 -0
363 lines not on this page · raw message
kernel/bpf/syscall.c +6 -0
@@ -684,6 +684,8 @@ static int map_lookup_elem(union bpf_attr *attr) err =bpf_fd_array_map_lookup_elem (map, key, value); }else if (IS_FD_HASH (map)) { err =bpf_fd_htab_map_lookup_elem (map, key, value); + }else if (map->map_type ==BPF_MAP_TYPE_REUSEPORT_SOCKARRAY ) { + err =bpf_fd_reuseport_array_lookup_elem (map, key, value); }else {rcu_read_lock (); ptr = map->ops->map_lookup_elem (map, key);
@@ -790,6 +792,10 @@ static int map_update_elem(union bpf_attr *attr) err =bpf_fd_htab_map_update_elem (map, f.file, key, value, attr->flags);rcu_read_unlock (); + }else if (map->map_type ==BPF_MAP_TYPE_REUSEPORT_SOCKARRAY ) { +/* rcu_read_lock() is not needed */ + err =bpf_fd_reuseport_array_update_elem (map, key, value, + attr->flags); }else {rcu_read_lock (); err = map->ops->map_update_elem (map, key, value, attr->flags);
net/core/sock_reuseport.c +8 -0
@@ -186,6 +186,14 @@ void reuseport_detach_sock(struct sock *sk)spin_lock_bh (&reuseport_lock); reuse =rcu_dereference_protected (sk->sk_reuseport_cb,lockdep_is_held (&reuseport_lock)); + +/* At least one of the sk in this reuseport group is added to +* a bpf map. Notify the bpf side. The bpf map logic will +* remove the sk if it is indeed added to a bpf map. +*/ +if (reuse->reuseport_id) +bpf_sk_reuseport_detach (sk); +rcu_assign_pointer (sk->sk_reuseport_cb,NULL );for (i =0 ; i < reuse->num_socks; i++) {
Signature · 1 line
2.17.1
PATCH 4/9 bpf: Introduce BPF_PROG_TYPE_SK_REUSEPORT
In reply to Martin KaFai LauThis patch adds a BPF_PROG_TYPE_SK_REUSEPORT which can select a SO_REUSEPORT sk from a BPF_MAP_TYPE_REUSEPORT_ARRAY. Like other non SK_FILTER/CGROUP_SKB program, it requires CAP_SYS_ADMIN.
BPF_PROG_TYPE_SK_REUSEPORT introduces "struct sk_reuseport_kern" to store the bpf context instead of using the skb->cb[48].
At the SO_REUSEPORT sk lookup time, it is in the middle of transiting from a lower layer (ipv4/ipv6) to a upper layer (udp/tcp). At this point, it is not always clear where the bpf context can be appended in the skb->cb[48] to avoid saving-and-restoring cb[]. Even putting aside the difference between ipv4-vs-ipv6 and udp-vs-tcp. It is not clear if the lower layer is only ipv4 and ipv6 in the future and will it not touch the cb[] again before transiting to the upper layer.
For example, in udp_gro_receive(), it uses the 48 byte NAPI_GRO_CB instead of IP[6]CB and it may still modify the cb[] after calling the udp[46]_lib_lookup_skb(). Because of the above reason, if sk->cb is used for the bpf ctx, saving-and-restoring is needed and likely the whole 48 bytes cb[] has to be saved and restored.
Instead of saving, setting and restoring the cb[], this patch opts to create a new "struct sk_reuseport_kern" and setting the needed values in there.
The new BPF_PROG_TYPE_SK_REUSEPORT and "struct sk_reuseport_(kern|md)" will serve all ipv4/ipv6 + udp/tcp combinations. There is no protocol specific usage at this point and it is also inline with the current sock_reuseport.c implementation (i.e. no protocol specific requirement).
In "struct sk_reuseport_md", this patch exposes data/data_end/len with semantic similar to other existing usages. Together with "bpf_skb_load_bytes()" and "bpf_skb_load_bytes_relative()", the bpf prog can peek anywhere in the skb. The "bind_inany" tells the bpf prog that the reuseport group is bind-ed to a local INANY address which cannot be learned from skb.
The new "bind_inany" is added to "struct sock_reuseport" which will be used when running the new "BPF_PROG_TYPE_SK_REUSEPORT" bpf prog in order to avoid repeating the "bind INANY" test on "sk_v6_rcv_saddr/sk->sk_rcv_saddr" every time a bpf prog is run. It can only be properly initialized when a "sk->sk_reuseport" enabled sk is adding to a hashtable (i.e. during "reuseport_alloc()" and "reuseport_add_sock()").
The new "sk_select_reuseport()" is the main helper that the bpf prog will use to select a SO_REUSEPORT sk. It is the only function that can use the new BPF_MAP_TYPE_REUSEPORT_ARRAY. As mentioned in the earlier patch, the validity of a selected sk is checked in run time in "sk_select_reuseport()". Doing the check in verification time is difficult and inflexible (consider the map-in-map use case). The runtime check is to compare the selected sk's reuseport_id with the reuseport_id that we want. This helper will return -EXXX if the selected sk cannot serve the incoming request (e.g. reuseport_id not match). The bpf prog can decide if it wants to do SK_DROP as its discretion.
When the bpf prog returns SK_PASS, the kernel will check if a valid sk has been selected (i.e. "reuse_kern->selected_sk != NULL"). If it does , it will use the selected sk. If not, the kernel will select one from "reuse->socks[]" (as before this patch).
The SK_DROP and SK_PASS handling logic will be in the next patch.
--- include/linux/bpf_types.h | 3 + include/linux/filter.h | 15 ++ include/net/addrconf.h | 1 + include/net/sock_reuseport.h | 6 +- include/uapi/linux/bpf.h | 36 ++++- kernel/bpf/verifier.c | 9 ++ net/core/filter.c | 269 +++++++++++++++++++++++++++++++- net/core/sock_reuseport.c | 20 ++- net/ipv4/inet_connection_sock.c | 9 ++ net/ipv4/inet_hashtables.c | 5 +- net/ipv4/udp.c | 5 +- 11 files changed, 365 insertions(+), 13 deletions(-)
include/linux/bpf_types.h +3 -0
@@ -29,6 +29,9 @@ BPF_PROG_TYPE(BPF_PROG_TYPE_CGROUP_DEVICE, cg_dev)#ifdef CONFIG_BPF_LIRC_MODE2 BPF_PROG_TYPE(BPF_PROG_TYPE_LIRC_MODE2, lirc_mode2)#endif +#ifdef CONFIG_INET +BPF_PROG_TYPE(BPF_PROG_TYPE_SK_REUSEPORT, sk_reuseport) +#endif BPF_MAP_TYPE(BPF_MAP_TYPE_ARRAY, array_map_ops)BPF_MAP_TYPE (BPF_MAP_TYPE_PERCPU_ARRAY , percpu_array_map_ops)
include/linux/filter.h +15 -0
@@ -32,6 +32,7 @@ struct seccomp_data;struct bpf_prog_aux ;struct xdp_rxq_info ;struct xdp_buff ; +struct sock_reuseport ;/* ArgX, context and stack frame pointer register positions. Note, * Arg1, Arg2, Arg3, etc are used as argument mappings of function
@@ -798,6 +799,20 @@ void bpf_warn_invalid_xdp_action(u32 act);struct sock *do_sk_redirect_map (struct sk_buff *skb);struct sock *do_msg_redirect_map (struct sk_msg_buff *md); +#ifdef CONFIG_INET +struct sock *bpf_run_sk_reuseport (struct sock_reuseport *reuse,struct sock *sk, +struct bpf_prog *prog,struct sk_buff *skb, +u32 hash); +#else +static inline struct sock * +bpf_run_sk_reuseport (struct sock_reuseport *reuse,struct sock *sk, +struct bpf_prog *prog,struct sk_buff *skb, +u32 hash) +{ +return NULL ; +} +#endif +#ifdef CONFIG_BPF_JIT extern int bpf_jit_enable;extern int bpf_jit_harden;
include/net/addrconf.h +1 -0
@@ -108,6 +108,7 @@ int ipv6_get_lladdr(struct net_device *dev, struct in6_addr *addr, u32 banned_flags);bool inet_rcv_saddr_equal (const struct sock *sk,const struct sock *sk2,bool match_wildcard); +bool inet_rcv_saddr_any (const struct sock *sk);void addrconf_join_solict (struct net_device *dev,const struct in6_addr *addr);void addrconf_leave_solict (struct inet6_dev *idev,const struct in6_addr *addr);
include/net/sock_reuseport.h +4 -2
@@ -21,12 +21,14 @@ struct sock_reuseport {unsigned int synq_overflow_ts;/* ID stays the same even after the size of socks[] grows. */ unsigned int reuseport_id; +bool bind_inany;struct bpf_prog __rcu *prog;/* optional BPF sock selector */ struct sock *socks[0 ];/* array of sock pointers */ }; -extern int reuseport_alloc (struct sock *sk); -extern int reuseport_add_sock (struct sock *sk,struct sock *sk2); +extern int reuseport_alloc (struct sock *sk,bool bind_inany); +extern int reuseport_add_sock (struct sock *sk,struct sock *sk2, +bool bind_inany);extern void reuseport_detach_sock (struct sock *sk); extern struct sock *reuseport_select_sock(struct sock *sk, u32 hash,
include/uapi/linux/bpf.h +35 -1
@@ -151,6 +151,7 @@ enum bpf_prog_type {BPF_PROG_TYPE_CGROUP_SOCK_ADDR ,BPF_PROG_TYPE_LWT_SEG6LOCAL ,BPF_PROG_TYPE_LIRC_MODE2 , + BPF_PROG_TYPE_SK_REUSEPORT, }; enum bpf_attach_type {
@@ -2114,6 +2115,14 @@ union bpf_attr {* the shared data. * Return * Pointer to the local storage area. +* +* int bpf_sk_select_reuseport(struct sk_reuseport_md *reuse, struct bpf_map *map, void *key, u64 flags) +* Description +* Select a SO_REUSEPORT sk from a BPF_MAP_TYPE_REUSEPORT_ARRAY map +* It checks the selected sk is matching the incoming +* request in the skb. +* Return +* 0 on success, or a negative error in case of failure. */ #define __BPF_FUNC_MAPPER (FN ) \ FN(unspec), \
@@ -2197,7 +2206,8 @@ union bpf_attr {FN (rc_keydown), \FN (skb_cgroup_id), \FN (get_current_cgroup_id), \ -FN (get_local_storage), +FN (get_local_storage), \ +FN (sk_select_reuseport),/* integer value in 'imm' field of BPF_CALL instruction selects which helper * function eBPF program intends to call
@@ -2414,6 +2424,30 @@ struct sk_msg_md {__u32 local_port;/* stored in host byte order */ }; +struct sk_reuseport_md { +/* +* Start of directly accessible data. It begins from +* the tcp/udp header. +*/ +void *data; +void *data_end;/* End of directly accessible data */ +/* +* Total length of packet (starting from the tcp/udp header). +* Note that the directly accessible bytes (data_end - data) +* could be less than this "len". Those bytes could be +* indirectly read by a helper "bpf_skb_load_bytes()". +*/ +__u32 len; +/* +* Eth protocol in the mac header (network byte order). e.g. +* ETH_P_IP(0x0800) and ETH_P_IPV6(0x86DD) +*/ +__u32 eth_protocol; +__u32 ip_protocol;/* IP protocol. e.g. IPPROTO_TCP, IPPROTO_UDP */ +__u32 bind_inany;/* Is sock bound to an INANY address? */ +__u32 hash;/* A hash of the packet 4 tuples */ +}; +#define BPF_TAG_SIZE 8struct bpf_prog_info {
kernel/bpf/verifier.c +9 -0
@@ -1310,6 +1310,7 @@ static bool may_access_direct_pkt_data(struct bpf_verifier_env *env,case BPF_PROG_TYPE_LWT_IN :case BPF_PROG_TYPE_LWT_OUT :case BPF_PROG_TYPE_LWT_SEG6LOCAL : +case BPF_PROG_TYPE_SK_REUSEPORT :/* dst_input() and dst_output() can't write for now */ if (t ==BPF_WRITE )return false;
@@ -2166,6 +2167,10 @@ static int check_map_func_compatibility(struct bpf_verifier_env *env, func_id != BPF_FUNC_msg_redirect_hash) goto error;break ; +case BPF_MAP_TYPE_REUSEPORT_SOCKARRAY : +if (func_id != BPF_FUNC_sk_select_reuseport) + goto error; +break ;default :break ; }
@@ -2217,6 +2222,10 @@ static int check_map_func_compatibility(struct bpf_verifier_env *env,if (map->map_type !=BPF_MAP_TYPE_CGROUP_STORAGE ) goto error;break ; +case BPF_FUNC_sk_select_reuseport: +if (map->map_type !=BPF_MAP_TYPE_REUSEPORT_SOCKARRAY ) + goto error; +break ;default :break ; }
net/core/filter.c +268 -1
278 lines not on this page · raw message
net/core/sock_reuseport.c +15 -5
@@ -51,7 +51,7 @@ static struct sock_reuseport *__reuseport_alloc(unsigned int max_socks)return reuse; } -int reuseport_alloc (struct sock *sk) +int reuseport_alloc (struct sock *sk,bool bind_inany) {struct sock_reuseport *reuse;
@@ -63,9 +63,17 @@ int reuseport_alloc(struct sock *sk)/* Allocation attempts can occur concurrently via the setsockopt path * and the bind/hash path. Nothing to do when we lose the race. */ -if (rcu_dereference_protected (sk->sk_reuseport_cb, -lockdep_is_held (&reuseport_lock))) + reuse =rcu_dereference_protected (sk->sk_reuseport_cb, +lockdep_is_held (&reuseport_lock)); +if (reuse) { +/* Only set reuse->bind_inany if the bind_inany is true. +* Otherwise, it will overwrite the reuse->bind_inany +* which was set by the bind/hash path. +*/ +if (bind_inany) + reuse->bind_inany = bind_inany; goto out; + } reuse =__reuseport_alloc (INIT_SOCKS );if (!reuse) {
@@ -75,6 +83,7 @@ int reuseport_alloc(struct sock *sk) reuse->socks[0 ] = sk; reuse->num_socks =1 ; + reuse->bind_inany = bind_inany;rcu_assign_pointer (sk->sk_reuseport_cb, reuse); out:
@@ -101,6 +110,7 @@ static struct sock_reuseport *reuseport_grow(struct sock_reuseport *reuse) more_reuse->num_socks = reuse->num_socks; more_reuse->prog = reuse->prog; more_reuse->reuseport_id = reuse->reuseport_id; + more_reuse->bind_inany = reuse->bind_inany;memcpy (more_reuse->socks, reuse->socks, reuse->num_socks *sizeof (struct sock *));
@@ -136,12 +146,12 @@ static void reuseport_free_rcu(struct rcu_head *head)* @sk2: Socket belonging to the existing reuseport group. * May return ENOMEM and not add socket to group under memory pressure. */ -int reuseport_add_sock (struct sock *sk,struct sock *sk2) +int reuseport_add_sock (struct sock *sk,struct sock *sk2,bool bind_inany) {struct sock_reuseport *old_reuse, *reuse;if (!rcu_access_pointer (sk2->sk_reuseport_cb)) { -int err =reuseport_alloc (sk2); +int err =reuseport_alloc (sk2, bind_inany);if (err)return err;
net/ipv4/inet_connection_sock.c +9 -0
@@ -107,6 +107,15 @@ bool inet_rcv_saddr_equal(const struct sock *sk, const struct sock *sk2, }EXPORT_SYMBOL (inet_rcv_saddr_equal); +bool inet_rcv_saddr_any (const struct sock *sk) +{ +#if IS_ENABLED (CONFIG_IPV6 ) +if (sk->sk_family ==AF_INET6 ) +return ipv6_addr_any (&sk->sk_v6_rcv_saddr); +#endif +return !sk->sk_rcv_saddr; +} +void inet_get_local_port_range (struct net *net,int *low,int *high) {unsigned int seq;
net/ipv4/inet_hashtables.c +3 -2
@@ -567,10 +567,11 @@ static int inet_reuseport_add_sock(struct sock *sk, inet_csk(sk2)->icsk_bind_hash == tb && sk2->sk_reuseport && uid_eq(uid, sock_i_uid(sk2)) && inet_rcv_saddr_equal(sk, sk2, false)) -return reuseport_add_sock (sk, sk2); +return reuseport_add_sock (sk, sk2, +inet_rcv_saddr_any (sk)); } -return reuseport_alloc (sk); +return reuseport_alloc (sk,inet_rcv_saddr_any (sk)); }int __inet_hash (struct sock *sk,struct sock *osk)
net/ipv4/udp.c +3 -2
@@ -221,11 +221,12 @@ static int udp_reuseport_add_sock(struct sock *sk, struct udp_hslot *hslot) (sk2->sk_bound_dev_if == sk->sk_bound_dev_if) && sk2->sk_reuseport && uid_eq(uid, sock_i_uid(sk2)) && inet_rcv_saddr_equal(sk, sk2, false)) { -return reuseport_add_sock (sk, sk2); +return reuseport_add_sock (sk, sk2, +inet_rcv_saddr_any (sk)); } } -return reuseport_alloc (sk); +return reuseport_alloc (sk,inet_rcv_saddr_any (sk)); }/**
Signature · 1 line
2.17.1
PATCH 5/9 bpf: Enable BPF_PROG_TYPE_SK_REUSEPORT bpf prog in reuseport selection
In reply to Martin KaFai LauThis patch allows a BPF_PROG_TYPE_SK_REUSEPORT bpf prog to select a SO_REUSEPORT sk from a BPF_MAP_TYPE_REUSEPORT_ARRAY introduced in the earlier patch. "bpf_run_sk_reuseport()" will return -ECONNREFUSED when the BPF_PROG_TYPE_SK_REUSEPORT prog returns SK_DROP. The callers, in inet[6]_hashtable.c and ipv[46]/udp.c, are modified to handle this case and return NULL immediately instead of continuing the sk search from its hashtable.
It re-uses the existing SO_ATTACH_REUSEPORT_EBPF setsockopt to attach BPF_PROG_TYPE_SK_REUSEPORT. The "sk_reuseport_attach_bpf()" will check if the attaching bpf prog is in the new SK_REUSEPORT or the existing SOCKET_FILTER type and then check different things accordingly.
One level of "__reuseport_attach_prog()" call is removed. The "sk_unhashed() && ..." and "sk->sk_reuseport_cb" tests are pushed back to "reuseport_attach_prog()" in sock_reuseport.c. sock_reuseport.c seems to have more knowledge on those test requirements than filter.c. In "reuseport_attach_prog()", after new_prog is attached to reuse->prog, the old_prog (if any) is also directly freed instead of returning the old_prog to the caller and asking the caller to free.
The sysctl_optmem_max check is moved back to the "sk_reuseport_attach_filter()" and "sk_reuseport_attach_bpf()". As of other bpf prog types, the new BPF_PROG_TYPE_SK_REUSEPORT is only bounded by the usual "bpf_prog_charge_memlock()" during load time instead of bounded by both bpf_prog_charge_memlock and sysctl_optmem_max.
--- include/linux/filter.h | 1 + include/net/sock_reuseport.h | 3 +- net/core/filter.c | 87 +++++++++++++++++++++--------------- net/core/sock_reuseport.c | 36 ++++++++++----- net/ipv4/inet_hashtables.c | 14 +++--- net/ipv4/udp.c | 4 ++ net/ipv6/inet6_hashtables.c | 14 +++--- net/ipv6/udp.c | 4 ++ 8 files changed, 106 insertions(+), 57 deletions(-)
include/linux/filter.h +1 -0
@@ -739,6 +739,7 @@ int sk_attach_filter(struct sock_fprog *fprog, struct sock *sk);int sk_attach_bpf (u32 ufd,struct sock *sk);int sk_reuseport_attach_filter (struct sock_fprog *fprog,struct sock *sk);int sk_reuseport_attach_bpf (u32 ufd,struct sock *sk); +void sk_reuseport_prog_free (struct bpf_prog *prog);int sk_detach_filter (struct sock *sk);int sk_get_filter (struct sock *sk,struct sock_filter __user *filter,unsigned int len);
include/net/sock_reuseport.h +1 -2
@@ -34,8 +34,7 @@ extern struct sock *reuseport_select_sock(struct sock *sk,u32 hash, struct sk_buff *skb, int hdr_len); -extern struct bpf_prog *reuseport_attach_prog (struct sock *sk, -struct bpf_prog *prog); +extern int reuseport_attach_prog (struct sock *sk,struct bpf_prog *prog);int reuseport_get_id (struct sock_reuseport *reuse);#endif /* _SOCK_REUSEPORT_H */
net/core/filter.c +52 -35
114 lines not on this page · raw message
net/core/sock_reuseport.c +26 -10
@@ -9,6 +9,7 @@#include <net/sock_reuseport.h> #include <linux/bpf.h> #include <linux/idr.h> +#include <linux/filter.h> #include <linux/rcupdate.h> #define INIT_SOCKS 128
@@ -133,8 +134,7 @@ static void reuseport_free_rcu(struct rcu_head *head)struct sock_reuseport *reuse; reuse = container_of(head, struct sock_reuseport, rcu); -if (reuse->prog) -bpf_prog_destroy (reuse->prog); +sk_reuseport_prog_free (rcu_dereference_protected (reuse->prog,1 ));if (reuse->reuseport_id)ida_simple_remove (&reuseport_ida, reuse->reuseport_id);kfree (reuse);
@@ -219,9 +219,9 @@ void reuseport_detach_sock(struct sock *sk) }EXPORT_SYMBOL (reuseport_detach_sock); -static struct sock *run_bpf (struct sock_reuseport *reuse,u16 socks, -struct bpf_prog *prog,struct sk_buff *skb, -int hdr_len) +static struct sock *run_bpf_filter (struct sock_reuseport *reuse,u16 socks, +struct bpf_prog *prog,struct sk_buff *skb, +int hdr_len) {struct sk_buff *nskb =NULL ;u32 index;
@@ -282,9 +282,15 @@ struct sock *reuseport_select_sock(struct sock *sk,/* paired with smp_wmb() in reuseport_add_sock() */ smp_rmb (); -if (prog && skb) - sk2 =run_bpf (reuse, socks, prog, skb, hdr_len); +if (!prog || !skb) + goto select_by_hash; + +if (prog->type ==BPF_PROG_TYPE_SK_REUSEPORT ) + sk2 =bpf_run_sk_reuseport (reuse, sk, prog, skb, hash); +else + sk2 =run_bpf_filter (reuse, socks, prog, skb, hdr_len); +select_by_hash:/* no bpf or invalid bpf result: fall back to hash usage */ if (!sk2) sk2 = reuse->socks[reciprocal_scale (hash, socks)];
@@ -296,12 +302,21 @@ struct sock *reuseport_select_sock(struct sock *sk, }EXPORT_SYMBOL (reuseport_select_sock); -struct bpf_prog * -reuseport_attach_prog (struct sock *sk,struct bpf_prog *prog) +int reuseport_attach_prog (struct sock *sk,struct bpf_prog *prog) {struct sock_reuseport *reuse;struct bpf_prog *old_prog; +if (sk_unhashed (sk) && sk->sk_reuseport) { +int err =reuseport_alloc (sk, false); + +if (err) +return err; + }else if (!rcu_access_pointer (sk->sk_reuseport_cb)) { +/* The socket wasn't bound with SO_REUSEPORT */ +return -EINVAL ; + } +spin_lock_bh (&reuseport_lock); reuse =rcu_dereference_protected (sk->sk_reuseport_cb,lockdep_is_held (&reuseport_lock));
@@ -310,6 +325,7 @@ reuseport_attach_prog(struct sock *sk, struct bpf_prog *prog)rcu_assign_pointer (reuse->prog, prog);spin_unlock_bh (&reuseport_lock); -return old_prog; +sk_reuseport_prog_free (old_prog); +return 0 ; }EXPORT_SYMBOL (reuseport_attach_prog);
net/ipv4/inet_hashtables.c +9 -5
@@ -328,7 +328,7 @@ struct sock *__inet_lookup_listener(struct net *net, saddr, sport, daddr, hnum, dif, sdif);if (result) -return result; + goto done;/* Lookup lhash2 with INADDR_ANY */
@@ -337,9 +337,10 @@ struct sock *__inet_lookup_listener(struct net *net,if (ilb2->count > ilb->count) goto port_lookup; -return inet_lhash2_lookup (net, ilb2, skb, doff, - saddr, sport, daddr, hnum, - dif, sdif); + result =inet_lhash2_lookup (net, ilb2, skb, doff, + saddr, sport, daddr, hnum, + dif, sdif); + goto done; port_lookup:sk_for_each_rcu (sk, &ilb->head) {
@@ -352,12 +353,15 @@ struct sock *__inet_lookup_listener(struct net *net, result =reuseport_select_sock (sk, phash, skb, doff);if (result) -return result; + goto done; } result = sk; hiscore = score; } } +done: +if (unlikely (IS_ERR (result))) +return NULL ;return result; }EXPORT_SYMBOL_GPL (__inet_lookup_listener);
net/ipv4/udp.c +4 -0
@@ -499,6 +499,8 @@ struct sock *__udp4_lib_lookup(struct net *net, __be32 saddr, daddr, hnum, dif, sdif, exact_dif, hslot2, skb); } +if (unlikely (IS_ERR (result))) +return NULL ;return result; } begin:
@@ -513,6 +515,8 @@ struct sock *__udp4_lib_lookup(struct net *net, __be32 saddr, saddr, sport); result =reuseport_select_sock (sk, hash, skb,sizeof (struct udphdr )); +if (unlikely (IS_ERR (result))) +return NULL ;if (result)return result; }
net/ipv6/inet6_hashtables.c +9 -5
@@ -191,7 +191,7 @@ struct sock *inet6_lookup_listener(struct net *net, saddr, sport, daddr, hnum, dif, sdif);if (result) -return result; + goto done;/* Lookup lhash2 with in6addr_any */
@@ -200,9 +200,10 @@ struct sock *inet6_lookup_listener(struct net *net,if (ilb2->count > ilb->count) goto port_lookup; -return inet6_lhash2_lookup (net, ilb2, skb, doff, - saddr, sport, daddr, hnum, - dif, sdif); + result =inet6_lhash2_lookup (net, ilb2, skb, doff, + saddr, sport, daddr, hnum, + dif, sdif); + goto done; port_lookup:sk_for_each (sk, &ilb->head) {
@@ -214,12 +215,15 @@ struct sock *inet6_lookup_listener(struct net *net, result =reuseport_select_sock (sk, phash, skb, doff);if (result) -return result; + goto done; } result = sk; hiscore = score; } } +done: +if (unlikely (IS_ERR (result))) +return NULL ;return result; }EXPORT_SYMBOL_GPL (inet6_lookup_listener);
net/ipv6/udp.c +4 -0
@@ -235,6 +235,8 @@ struct sock *__udp6_lib_lookup(struct net *net, exact_dif, hslot2, skb); } +if (unlikely (IS_ERR (result))) +return NULL ;return result; } begin:
@@ -249,6 +251,8 @@ struct sock *__udp6_lib_lookup(struct net *net, saddr, sport); result =reuseport_select_sock (sk, hash, skb,sizeof (struct udphdr )); +if (unlikely (IS_ERR (result))) +return NULL ;if (result)return result; }
Signature · 1 line
2.17.1
PATCH 6/9 bpf: Refactor ARRAY_SIZE macro to bpf_util.h
In reply to Martin KaFai LauThis patch refactors the ARRAY_SIZE macro to bpf_util.h.
--- tools/testing/selftests/bpf/bpf_util.h | 4 ++++ tools/testing/selftests/bpf/test_align.c | 5 +---- tools/testing/selftests/bpf/test_btf.c | 5 +---- tools/testing/selftests/bpf/test_sock.c | 5 +---- tools/testing/selftests/bpf/test_sock_addr.c | 5 +---- tools/testing/selftests/bpf/test_verifier.c | 5 +---- 6 files changed, 9 insertions(+), 20 deletions(-)
tools/testing/selftests/bpf/bpf_util.h +4 -0
@@ -44,4 +44,8 @@ static inline unsigned int bpf_num_possible_cpus(void) name[bpf_num_possible_cpus()]#define bpf_percpu (name, cpu) name[(cpu)].v +#ifndef ARRAY_SIZE +# define ARRAY_SIZE (x) (sizeof(x) / sizeof((x)[0])) +#endif +#endif /* __BPF_UTIL__ */
tools/testing/selftests/bpf/test_align.c +1 -4
@@ -18,10 +18,7 @@#include "../../../include/linux/filter.h" #include "bpf_rlimit.h" - -#ifndef ARRAY_SIZE -# define ARRAY_SIZE (x) (sizeof(x) / sizeof((x)[0])) -#endif +#include "bpf_util.h" #define MAX_INSNS 512#define MAX_MATCHES 16
tools/testing/selftests/bpf/test_btf.c +1 -4
@@ -19,6 +19,7 @@#include <bpf/btf.h> #include "bpf_rlimit.h" +#include "bpf_util.h" static uint32_t pass_cnt;static uint32_t error_cnt;
@@ -93,10 +94,6 @@ static int __base_pr(const char *format, ...)#define MAX_NR_RAW_TYPES 1024#define BTF_LOG_BUF_SIZE 65535 -#ifndef ARRAY_SIZE -# define ARRAY_SIZE (x) (sizeof(x) / sizeof((x)[0])) -#endif - static struct args { unsigned int raw_test_num; unsigned int file_test_num;
tools/testing/selftests/bpf/test_sock.c +1 -4
@@ -14,10 +14,7 @@#include "cgroup_helpers.h" #include "bpf_rlimit.h" - -#ifndef ARRAY_SIZE -# define ARRAY_SIZE (x) (sizeof(x) / sizeof((x)[0])) -#endif +#include "bpf_util.h" #define CG_PATH "/foo"#define MAX_INSNS 512
tools/testing/selftests/bpf/test_sock_addr.c +1 -4
@@ -20,15 +20,12 @@#include "cgroup_helpers.h" #include "bpf_rlimit.h" +#include "bpf_util.h" #ifndef ENOTSUPP # define ENOTSUPP 524#endif -#ifndef ARRAY_SIZE -# define ARRAY_SIZE (x) (sizeof(x) / sizeof((x)[0])) -#endif -#define CG_PATH "/foo"#define CONNECT4_PROG_PATH "./connect4_prog.o"#define CONNECT6_PROG_PATH "./connect6_prog.o"
tools/testing/selftests/bpf/test_verifier.c +1 -4
@@ -42,12 +42,9 @@#endif #include "bpf_rlimit.h" #include "bpf_rand.h" +#include "bpf_util.h" #include "../../../include/linux/filter.h" -#ifndef ARRAY_SIZE -# define ARRAY_SIZE (x) (sizeof(x) / sizeof((x)[0])) -#endif -#define MAX_INSNS BPF_MAXINSNS#define MAX_FIXUPS 8#define MAX_NR_MAPS 8
Signature · 1 line
2.17.1
PATCH 7/9 bpf: Sync bpf.h uapi to tools/
In reply to Martin KaFai LauThis patch sync include/uapi/linux/bpf.h totools/include/uapi/linux/
--- tools/include/uapi/linux/bpf.h | 37 +++++++++++++++++++++++++++++++++- 1 file changed, 36 insertions(+), 1 deletion(-)
tools/include/uapi/linux/bpf.h +36 -1
@@ -126,6 +126,7 @@ enum bpf_map_type {BPF_MAP_TYPE_XSKMAP ,BPF_MAP_TYPE_SOCKHASH ,BPF_MAP_TYPE_CGROUP_STORAGE , + BPF_MAP_TYPE_REUSEPORT_SOCKARRAY, }; enum bpf_prog_type {
@@ -150,6 +151,7 @@ enum bpf_prog_type {BPF_PROG_TYPE_CGROUP_SOCK_ADDR ,BPF_PROG_TYPE_LWT_SEG6LOCAL ,BPF_PROG_TYPE_LIRC_MODE2 , + BPF_PROG_TYPE_SK_REUSEPORT, }; enum bpf_attach_type {
@@ -2113,6 +2115,14 @@ union bpf_attr {* the shared data. * Return * Pointer to the local storage area. +* +* int bpf_sk_select_reuseport(struct sk_reuseport_md *reuse, struct bpf_map *map, void *key, u64 flags) +* Description +* Select a SO_REUSEPORT sk from a BPF_MAP_TYPE_REUSEPORT_ARRAY map +* It checks the selected sk is matching the incoming +* request in the skb. +* Return +* 0 on success, or a negative error in case of failure. */ #define __BPF_FUNC_MAPPER (FN ) \ FN(unspec), \
@@ -2196,7 +2206,8 @@ union bpf_attr {FN (rc_keydown), \FN (skb_cgroup_id), \FN (get_current_cgroup_id), \ -FN (get_local_storage), +FN (get_local_storage), \ +FN (sk_select_reuseport),/* integer value in 'imm' field of BPF_CALL instruction selects which helper * function eBPF program intends to call
@@ -2413,6 +2424,30 @@ struct sk_msg_md {__u32 local_port;/* stored in host byte order */ }; +struct sk_reuseport_md { +/* +* Start of directly accessible data. It begins from +* the tcp/udp header. +*/ +void *data; +void *data_end;/* End of directly accessible data */ +/* +* Total length of packet (starting from the tcp/udp header). +* Note that the directly accessible bytes (data_end - data) +* could be less than this "len". Those bytes could be +* indirectly read by a helper "bpf_skb_load_bytes()". +*/ +__u32 len; +/* +* Eth protocol in the mac header (network byte order). e.g. +* ETH_P_IP(0x0800) and ETH_P_IPV6(0x86DD) +*/ +__u32 eth_protocol; +__u32 ip_protocol;/* IP protocol. e.g. IPPROTO_TCP, IPPROTO_UDP */ +__u32 bind_inany;/* Is sock bound to an INANY address? */ +__u32 hash;/* A hash of the packet 4 tuples */ +}; +#define BPF_TAG_SIZE 8struct bpf_prog_info {
Signature · 1 line
2.17.1
PATCH 8/9 bpf: test BPF_MAP_TYPE_REUSEPORT_SOCKARRAY
In reply to Martin KaFai LauThis patch adds tests for the new BPF_MAP_TYPE_REUSEPORT_SOCKARRAY.
--- tools/lib/bpf/libbpf.c | 1 + tools/testing/selftests/bpf/test_maps.c | 262 +++++++++++++++++++++++- 2 files changed, 262 insertions(+), 1 deletion(-)
tools/lib/bpf/libbpf.c +1 -0
@@ -1496,6 +1496,7 @@ static bool bpf_prog_type__needs_kver(enum bpf_prog_type type)case BPF_PROG_TYPE_SK_MSG :case BPF_PROG_TYPE_CGROUP_SOCK_ADDR :case BPF_PROG_TYPE_LIRC_MODE2 : +case BPF_PROG_TYPE_SK_REUSEPORT :return false;case BPF_PROG_TYPE_UNSPEC :case BPF_PROG_TYPE_KPROBE :
tools/testing/selftests/bpf/test_maps.c +261 -1
288 lines not on this page · raw message
Signature · 1 line
2.17.1
PATCH 9/9 bpf: Test BPF_PROG_TYPE_SK_REUSEPORT
In reply to Martin KaFai LauThis patch add tests for the new BPF_PROG_TYPE_SK_REUSEPORT.
The tests cover:
- IPv4/IPv6 + TCP/UDP
- TCP syncookie
- TCP fastopen
- Cases when the bpf_sk_select_reuseport() returning errors
- Cases when the bpf prog returns SK_DROP
- Values from sk_reuseport_md
- outer_map => reuseport_array
The test depends on
commit 3eee1f75f2b9 ("bpf: fix bpf_skb_load_bytes_relative pkt length check")
--- tools/lib/bpf/bpf.c | 1 + tools/lib/bpf/bpf.h | 1 + tools/testing/selftests/bpf/Makefile | 4 +- tools/testing/selftests/bpf/bpf_helpers.h | 4 + .../selftests/bpf/test_select_reuseport.c | 688 ++++++++++++++++++ .../bpf/test_select_reuseport_common.h | 36 + .../bpf/test_select_reuseport_kern.c | 180 +++++ 7 files changed, 912 insertions(+), 2 deletions(-) create mode 100644 tools/testing/selftests/bpf/test_select_reuseport.c create mode 100644 tools/testing/selftests/bpf/test_select_reuseport_common.h create mode 100644 tools/testing/selftests/bpf/test_select_reuseport_kern.c
tools/lib/bpf/bpf.c +1 -0
@@ -92,6 +92,7 @@ int bpf_create_map_xattr(const struct bpf_create_map_attr *create_attr) attr.btf_key_type_id = create_attr->btf_key_type_id; attr.btf_value_type_id = create_attr->btf_value_type_id; attr.map_ifindex = create_attr->map_ifindex; + attr.inner_map_fd = create_attr->inner_map_fd;return sys_bpf (BPF_MAP_CREATE , &attr,sizeof (attr)); }
tools/lib/bpf/bpf.h +1 -0
@@ -39,6 +39,7 @@ struct bpf_create_map_attr {__u32 btf_key_type_id;__u32 btf_value_type_id;__u32 map_ifindex; +__u32 inner_map_fd; };int bpf_create_map_xattr (const struct bpf_create_map_attr *create_attr);
tools/testing/selftests/bpf/Makefile +2 -2
@@ -23,7 +23,7 @@ $(TEST_CUSTOM_PROGS): $(OUTPUT)/%: %.c TEST_GEN_PROGS = test_verifier test_tag test_maps test_lru_map test_lpm_map test_progs \ test_align test_verifier_log test_dev_cgroup test_tcpbpf_user \ test_sock test_btf test_sockmap test_lirc_mode2_user get_cgroup_id_user \ - test_socket_cookie test_cgroup_storage + test_socket_cookie test_cgroup_storage test_select_reuseport TEST_GEN_FILES = test_pkt_access.o test_xdp.o test_l4lb.o test_tcp_estats.o test_obj_id.o \ test_pkt_md_access.o test_xdp_redirect.o test_xdp_meta.o sockmap_parse_prog.o \
@@ -34,7 +34,7 @@ TEST_GEN_FILES = test_pkt_access.o test_xdp.o test_l4lb.o test_tcp_estats.o test test_btf_haskv.o test_btf_nokv.o test_sockmap_kern.o test_tunnel_kern.o \ test_get_stack_rawtp.o test_sockmap_kern.o test_sockhash_kern.o \ test_lwt_seg6local.o sendmsg4_prog.o sendmsg6_prog.o test_lirc_mode2_kern.o \ - get_cgroup_id_kern.o socket_cookie_prog.o + get_cgroup_id_kern.o socket_cookie_prog.o test_select_reuseport_kern.o # Order correspond to 'make run_tests' order TEST_PROGS := test_kmod.sh \
tools/testing/selftests/bpf/bpf_helpers.h +4 -0
@@ -111,6 +111,8 @@ static int (*bpf_xdp_adjust_tail)(void *ctx, int offset) =static int (*bpf_skb_get_xfrm_state)(void *ctx,int index,void *state,int size,int flags) = (void *) BPF_FUNC_skb_get_xfrm_state; +static int (*bpf_sk_select_reuseport)(void *ctx,void *map,void *key,__u32 flags) = + (void *) BPF_FUNC_sk_select_reuseport;static int (*bpf_get_stack)(void *ctx,void *buf,int size,int flags) = (void *) BPF_FUNC_get_stack; static int (*bpf_fib_lookup)(void *ctx, struct bpf_fib_lookup *params,
@@ -164,6 +166,8 @@ struct bpf_map_def {static int (*bpf_skb_load_bytes)(void *ctx,int off,void *to,int len) = (void *) BPF_FUNC_skb_load_bytes; +static int (*bpf_skb_load_bytes_relative)(void *ctx,int off,void *to,int len,__u32 start_header) = + (void *) BPF_FUNC_skb_load_bytes_relative;static int (*bpf_skb_store_bytes)(void *ctx,int off,void *from,int len,int flags) = (void *) BPF_FUNC_skb_store_bytes; static int (*bpf_l3_csum_replace)(void *ctx, int off, int from, int to, int flags) =
tools/testing/selftests/bpf/test_select_reuseport.c new file +688 -0
688 lines not on this page · raw message
tools/testing/selftests/bpf/test_select_reuseport_common.h new file +36 -0
@@ -0,0 +1,36 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* Copyright (c) 2018 Facebook */ + +#ifndef __TEST_SELECT_REUSEPORT_COMMON_H +#define __TEST_SELECT_REUSEPORT_COMMON_H + +#include <linux/types.h> + +enum result { +DROP_ERR_INNER_MAP , +DROP_ERR_SKB_DATA , +DROP_ERR_SK_SELECT_REUSEPORT , +DROP_MISC , +PASS , +PASS_ERR_SK_SELECT_REUSEPORT , +NR_RESULTS , +}; + +struct cmd { +__u32 reuseport_index; +__u32 pass_on_failure; +}; + +struct data_check { +__u32 ip_protocol; +__u32 skb_addrs[8 ]; +__u16 skb_ports[2 ]; +__u16 eth_protocol; +__u8 bind_inany; +__u8 equal_check_end[0 ]; + +__u32 len; +__u32 hash; +}; + +#endif
tools/testing/selftests/bpf/test_select_reuseport_kern.c new file +180 -0
180 lines not on this page · raw message
Signature · 1 line
2.17.1
Quoted 35 lines · Martin KaFai Lau
This series introduces a new map type "
BPF_MAP_TYPE_REUSEPORT_SOCKARRAY" and a new prog typeBPF_PROG_TYPE_SK_REUSEPORT.Here is a snippet from a commit message:
"To unleash the full potential of a bpf prog, it is essential for the userspace to be capable of directly setting up a bpf map which can then be consumed by the bpf prog to make decision. In this case, decide which
SO_REUSEPORTsk to serve the incoming request.By adding
BPF_MAP_TYPE_REUSEPORT_SOCKARRAY, the userspace has total control and visibility on where aSO_REUSEPORTsk should be located in a bpf map. The later patch will introduceBPF_PROG_TYPE_SK_REUSEPORTsuch that the bpf prog can directly select a sk from the bpf map. That will raise the programmability of the bpf prog attached to a reuseport group (a group of sk serving the same IP:PORT).For example, in UDP, the bpf prog can peek into the payload (e.g. through the "data" pointer introduced in the later patch) to learn the application level's connection information and then decide which sk to pick from a bpf map. The userspace can tightly couple the sk's location in a bpf map with the application logic in generating the UDP payload's connection information. This connection info contact/API stays within the userspace.
Also, when used with map-in-map, the userspace can switch the old-server-process's inner map to a new-server-process's inner map in one call "
bpf_map_update_elem(outer_map, &index, &new_reuseport_array)". The bpf prog will then direct incoming requests to the new process instead of the old process. The old process can finish draining the pending requests (e.g. by "accept()") before closing the old-fds. [Note that deleting a fd from a bpf map does not necessary mean the fd is closed]"Please see individual patch for details
Quoted 10 lines
Martin KaFai Lau (9):
tcp: Avoid TCP syncookie rejected bySO_REUSEPORTsocket
net: Add ID (if needed) tosock_reuseportand exposereuseport_lock
bpf: IntroduceBPF_MAP_TYPE_REUSEPORT_SOCKARRAY
bpf: IntroduceBPF_PROG_TYPE_SK_REUSEPORT
bpf: EnableBPF_PROG_TYPE_SK_REUSEPORTbpf prog in reuseport selection
bpf: RefactorARRAY_SIZEmacro tobpf_util.h
bpf: Syncbpf.huapi totools/
bpf: testBPF_MAP_TYPE_REUSEPORT_SOCKARRAY
bpf: TestBPF_PROG_TYPE_SK_REUSEPORT
Applied to bpf-next, thanks Martin!