Skip to content

Commit 1f26622

Browse files
committed
Merge branch 'bpf-sock-migration'
Kuniyuki Iwashima says: ==================== The SO_REUSEPORT option allows sockets to listen on the same port and to accept connections evenly. However, there is a defect in the current implementation [1]. When a SYN packet is received, the connection is tied to a listening socket. Accordingly, when the listener is closed, in-flight requests during the three-way handshake and child sockets in the accept queue are dropped even if other listeners on the same port could accept such connections. This situation can happen when various server management tools restart server (such as nginx) processes. For instance, when we change nginx configurations and restart it, it spins up new workers that respect the new configuration and closes all listeners on the old workers, resulting in the in-flight ACK of 3WHS is responded by RST. To avoid such a situation, users have to know deeply how the kernel handles SYN packets and implement connection draining by eBPF [2]: 1. Stop routing SYN packets to the listener by eBPF. 2. Wait for all timers to expire to complete requests 3. Accept connections until EAGAIN, then close the listener. or 1. Start counting SYN packets and accept syscalls using the eBPF map. 2. Stop routing SYN packets. 3. Accept connections up to the count, then close the listener. In either way, we cannot close a listener immediately. However, ideally, the application need not drain the not yet accepted sockets because 3WHS and tying a connection to a listener are just the kernel behaviour. The root cause is within the kernel, so the issue should be addressed in kernel space and should not be visible to user space. This patchset fixes it so that users need not take care of kernel implementation and connection draining. With this patchset, the kernel redistributes requests and connections from a listener to the others in the same reuseport group at/after close or shutdown syscalls. Although some software does connection draining, there are still merits in migration. For some security reasons, such as replacing TLS certificates, we may want to apply new settings as soon as possible and/or we may not be able to wait for connection draining. The sockets in the accept queue have not started application sessions yet. So, if we do not drain such sockets, they can be handled by the newer listeners and could have a longer lifetime. It is difficult to drain all connections in every case, but we can decrease such aborted connections by migration. In that sense, migration is always better than draining. Moreover, auto-migration simplifies user space logic and also works well in a case where we cannot modify and build a server program to implement the workaround. Note that the source and destination listeners MUST have the same settings at the socket API level; otherwise, applications may face inconsistency and cause errors. In such a case, we have to use the eBPF program to select a specific listener or to cancel migration. Special thanks to Martin KaFai Lau for bouncing ideas and exchanging code snippets along the way. Link: [1] The SO_REUSEPORT socket option https://lwn.net/Articles/542629/ [2] Re: [PATCH 1/1] net: Add SO_REUSEPORT_LISTEN_OFF socket option as drain mode https://lore.kernel.org/netdev/1458828813.10868.65.camel@edumazet-glaptop3.roam.corp.google.com/ Changelog: v8: * Make reuse const in reuseport_sock_index() * Don't use __reuseport_add_sock() in reuseport_alloc() * Change the arg of the second memcpy() in reuseport_grow() * Fix coding style to use goto in reuseport_alloc() * Keep sk_refcnt uninitialized in inet_reqsk_clone() * Initialize ireq_opt and ipv6_opt separately in reqsk_migrate_reset() [ This series does not include a stats patch suggested by Yuchung Cheng not to drop Acked-by/Reviewed-by tags and save reviewer's time. I will post the patch as a follow up after this series is merged. ] v7: https://lore.kernel.org/bpf/20210521182104.18273-1-kuniyu@amazon.co.jp/ * Prevent attaching/detaching a bpf prog via shutdowned socket * Fix typo in commit messages * Split selftest into subtests v6: https://lore.kernel.org/bpf/20210517002258.75019-1-kuniyu@amazon.co.jp/ * Change description in ip-sysctl.rst * Test IPPROTO_TCP before reading tfo_listener * Move reqsk_clone() to inet_connection_sock.c and rename to inet_reqsk_clone() * Pass req->rsk_listener to inet_csk_reqsk_queue_drop() and reqsk_queue_removed() in the migration path of receiving ACK * s/ARG_PTR_TO_SOCKET/PTR_TO_SOCKET/ in sk_reuseport_is_valid_access() * In selftest, use atomic ops to increment global vars, drop ACK by XDP, enable force fastopen, use "skel->bss" instead of "skel->data" v5: https://lore.kernel.org/bpf/20210510034433.52818-1-kuniyu@amazon.co.jp/ * Move initializtion of sk_node from 6th to 5th patch * Initialize sk_refcnt in reqsk_clone() * Modify some definitions in reqsk_timer_handler() * Validate in which path/state migration happens in selftest v4: https://lore.kernel.org/bpf/20210427034623.46528-1-kuniyu@amazon.co.jp/ * Make some functions and variables 'static' in selftest * Remove 'scalability' from the cover letter v3: https://lore.kernel.org/bpf/20210420154140.80034-1-kuniyu@amazon.co.jp/ * Add sysctl back for reuseport_grow() * Add helper functions to manage socks[] * Separate migration related logic into functions: reuseport_resurrect(), reuseport_stop_listen_sock(), reuseport_migrate_sock() * Clone request_sock to be migrated * Migrate request one by one * Pass child socket to eBPF prog v2: https://lore.kernel.org/netdev/20201207132456.65472-1-kuniyu@amazon.co.jp/ * Do not save closed sockets in socks[] * Revert 607904c * Extract inet_csk_reqsk_queue_migrate() into a single patch * Change the spin_lock order to avoid lockdep warning * Add static to __reuseport_select_sock * Use refcount_inc_not_zero() in reuseport_select_migrated_sock() * Set the default attach type in bpf_prog_load_check_attach() * Define new proto of BPF_FUNC_get_socket_cookie * Fix test to be compiled successfully * Update commit messages v1: https://lore.kernel.org/netdev/20201201144418.35045-1-kuniyu@amazon.co.jp/ * Remove the sysctl option * Enable migration if eBPF progam is not attached * Add expected_attach_type to check if eBPF program can migrate sockets * Add a field to tell migration type to eBPF program * Support BPF_FUNC_get_socket_cookie to get the cookie of sk * Allocate an empty skb if skb is NULL * Pass req_to_sk(req)->sk_hash because listener's hash is zero * Update commit messages and coverletter RFC: https://lore.kernel.org/netdev/20201117094023.3685-1-kuniyu@amazon.co.jp/ ==================== Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
2 parents bbf29d3 + c9d0bde commit 1f26622

21 files changed

Lines changed: 1335 additions & 68 deletions

File tree

‎Documentation/networking/ip-sysctl.rst‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -761,6 +761,31 @@ tcp_syncookies - INTEGER
761761
network connections you can set this knob to 2 to enable
762762
unconditionally generation of syncookies.
763763

764+
tcp_migrate_req - BOOLEAN
765+
The incoming connection is tied to a specific listening socket when
766+
the initial SYN packet is received during the three-way handshake.
767+
When a listener is closed, in-flight request sockets during the
768+
handshake and established sockets in the accept queue are aborted.
769+
770+
If the listener has SO_REUSEPORT enabled, other listeners on the
771+
same port should have been able to accept such connections. This
772+
option makes it possible to migrate such child sockets to another
773+
listener after close() or shutdown().
774+
775+
The BPF_SK_REUSEPORT_SELECT_OR_MIGRATE type of eBPF program should
776+
usually be used to define the policy to pick an alive listener.
777+
Otherwise, the kernel will randomly pick an alive listener only if
778+
this option is enabled.
779+
780+
Note that migration between listeners with different settings may
781+
crash applications. Let's say migration happens from listener A to
782+
B, and only B has TCP_SAVE_SYN enabled. B cannot read SYN data from
783+
the requests migrated from A. To avoid such a situation, cancel
784+
migration by returning SK_DROP in the type of eBPF program, or
785+
disable this option.
786+
787+
Default: 0
788+
764789
tcp_fastopen - INTEGER
765790
Enable TCP Fast Open (RFC7413) to send and accept data in the opening
766791
SYN packet.

‎include/linux/bpf.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2048,6 +2048,7 @@ struct sk_reuseport_kern {
20482048
struct sk_buff *skb;
20492049
struct sock *sk;
20502050
struct sock *selected_sk;
2051+
struct sock *migrating_sk;
20512052
void *data_end;
20522053
u32 hash;
20532054
u32 reuseport_id;

‎include/linux/filter.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -996,11 +996,13 @@ void bpf_warn_invalid_xdp_action(u32 act);
996996
#ifdef CONFIG_INET
997997
struct sock *bpf_run_sk_reuseport(struct sock_reuseport *reuse, struct sock *sk,
998998
struct bpf_prog *prog, struct sk_buff *skb,
999+
struct sock *migrating_sk,
9991000
u32 hash);
10001001
#else
10011002
static inline struct sock *
10021003
bpf_run_sk_reuseport(struct sock_reuseport *reuse, struct sock *sk,
10031004
struct bpf_prog *prog, struct sk_buff *skb,
1005+
struct sock *migrating_sk,
10041006
u32 hash)
10051007
{
10061008
return NULL;

‎include/net/netns/ipv4.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,7 @@ struct netns_ipv4 {
126126
u8 sysctl_tcp_syn_retries;
127127
u8 sysctl_tcp_synack_retries;
128128
u8 sysctl_tcp_syncookies;
129+
u8 sysctl_tcp_migrate_req;
129130
int sysctl_tcp_reordering;
130131
u8 sysctl_tcp_retries1;
131132
u8 sysctl_tcp_retries2;

‎include/net/sock_reuseport.h‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,9 @@ extern spinlock_t reuseport_lock;
1313
struct sock_reuseport {
1414
struct rcu_head rcu;
1515

16-
u16 max_socks; /* length of socks */
17-
u16 num_socks; /* elements in socks */
16+
u16 max_socks; /* length of socks */
17+
u16 num_socks; /* elements in socks */
18+
u16 num_closed_socks; /* closed elements in socks */
1819
/* The last synq overflow event timestamp of this
1920
* reuse->socks[] group.
2021
*/
@@ -31,10 +32,14 @@ extern int reuseport_alloc(struct sock *sk, bool bind_inany);
3132
extern int reuseport_add_sock(struct sock *sk, struct sock *sk2,
3233
bool bind_inany);
3334
extern void reuseport_detach_sock(struct sock *sk);
35+
void reuseport_stop_listen_sock(struct sock *sk);
3436
extern struct sock *reuseport_select_sock(struct sock *sk,
3537
u32 hash,
3638
struct sk_buff *skb,
3739
int hdr_len);
40+
struct sock *reuseport_migrate_sock(struct sock *sk,
41+
struct sock *migrating_sk,
42+
struct sk_buff *skb);
3843
extern int reuseport_attach_prog(struct sock *sk, struct bpf_prog *prog);
3944
extern int reuseport_detach_prog(struct sock *sk);
4045

‎include/uapi/linux/bpf.h‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -994,6 +994,8 @@ enum bpf_attach_type {
994994
BPF_SK_LOOKUP,
995995
BPF_XDP,
996996
BPF_SK_SKB_VERDICT,
997+
BPF_SK_REUSEPORT_SELECT,
998+
BPF_SK_REUSEPORT_SELECT_OR_MIGRATE,
997999
__MAX_BPF_ATTACH_TYPE
9981000
};
9991001

@@ -5416,6 +5418,20 @@ struct sk_reuseport_md {
54165418
__u32 ip_protocol; /* IP protocol. e.g. IPPROTO_TCP, IPPROTO_UDP */
54175419
__u32 bind_inany; /* Is sock bound to an INANY address? */
54185420
__u32 hash; /* A hash of the packet 4 tuples */
5421+
/* When reuse->migrating_sk is NULL, it is selecting a sk for the
5422+
* new incoming connection request (e.g. selecting a listen sk for
5423+
* the received SYN in the TCP case). reuse->sk is one of the sk
5424+
* in the reuseport group. The bpf prog can use reuse->sk to learn
5425+
* the local listening ip/port without looking into the skb.
5426+
*
5427+
* When reuse->migrating_sk is not NULL, reuse->sk is closed and
5428+
* reuse->migrating_sk is the socket that needs to be migrated
5429+
* to another listening socket. migrating_sk could be a fullsock
5430+
* sk that is fully established or a reqsk that is in-the-middle
5431+
* of 3-way handshake.
5432+
*/
5433+
__bpf_md_ptr(struct bpf_sock *, sk);
5434+
__bpf_md_ptr(struct bpf_sock *, migrating_sk);
54195435
};
54205436

54215437
#define BPF_TAG_SIZE 8

‎kernel/bpf/syscall.c‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1972,6 +1972,11 @@ static void bpf_prog_load_fixup_attach_type(union bpf_attr *attr)
19721972
attr->expected_attach_type =
19731973
BPF_CGROUP_INET_SOCK_CREATE;
19741974
break;
1975+
case BPF_PROG_TYPE_SK_REUSEPORT:
1976+
if (!attr->expected_attach_type)
1977+
attr->expected_attach_type =
1978+
BPF_SK_REUSEPORT_SELECT;
1979+
break;
19751980
}
19761981
}
19771982

@@ -2055,6 +2060,14 @@ bpf_prog_load_check_attach(enum bpf_prog_type prog_type,
20552060
if (expected_attach_type == BPF_SK_LOOKUP)
20562061
return 0;
20572062
return -EINVAL;
2063+
case BPF_PROG_TYPE_SK_REUSEPORT:
2064+
switch (expected_attach_type) {
2065+
case BPF_SK_REUSEPORT_SELECT:
2066+
case BPF_SK_REUSEPORT_SELECT_OR_MIGRATE:
2067+
return 0;
2068+
default:
2069+
return -EINVAL;
2070+
}
20582071
case BPF_PROG_TYPE_SYSCALL:
20592072
case BPF_PROG_TYPE_EXT:
20602073
if (expected_attach_type)

‎net/core/filter.c‎

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10044,11 +10044,13 @@ int sk_get_filter(struct sock *sk, struct sock_filter __user *ubuf,
1004410044
static void bpf_init_reuseport_kern(struct sk_reuseport_kern *reuse_kern,
1004510045
struct sock_reuseport *reuse,
1004610046
struct sock *sk, struct sk_buff *skb,
10047+
struct sock *migrating_sk,
1004710048
u32 hash)
1004810049
{
1004910050
reuse_kern->skb = skb;
1005010051
reuse_kern->sk = sk;
1005110052
reuse_kern->selected_sk = NULL;
10053+
reuse_kern->migrating_sk = migrating_sk;
1005210054
reuse_kern->data_end = skb->data + skb_headlen(skb);
1005310055
reuse_kern->hash = hash;
1005410056
reuse_kern->reuseport_id = reuse->reuseport_id;
@@ -10057,12 +10059,13 @@ static void bpf_init_reuseport_kern(struct sk_reuseport_kern *reuse_kern,
1005710059

1005810060
struct sock *bpf_run_sk_reuseport(struct sock_reuseport *reuse, struct sock *sk,
1005910061
struct bpf_prog *prog, struct sk_buff *skb,
10062+
struct sock *migrating_sk,
1006010063
u32 hash)
1006110064
{
1006210065
struct sk_reuseport_kern reuse_kern;
1006310066
enum sk_action action;
1006410067

10065-
bpf_init_reuseport_kern(&reuse_kern, reuse, sk, skb, hash);
10068+
bpf_init_reuseport_kern(&reuse_kern, reuse, sk, skb, migrating_sk, hash);
1006610069
action = BPF_PROG_RUN(prog, &reuse_kern);
1006710070

1006810071
if (action == SK_PASS)
@@ -10172,6 +10175,8 @@ sk_reuseport_func_proto(enum bpf_func_id func_id,
1017210175
return &sk_reuseport_load_bytes_proto;
1017310176
case BPF_FUNC_skb_load_bytes_relative:
1017410177
return &sk_reuseport_load_bytes_relative_proto;
10178+
case BPF_FUNC_get_socket_cookie:
10179+
return &bpf_get_socket_ptr_cookie_proto;
1017510180
default:
1017610181
return bpf_base_func_proto(func_id);
1017710182
}
@@ -10201,6 +10206,14 @@ sk_reuseport_is_valid_access(int off, int size,
1020110206
case offsetof(struct sk_reuseport_md, hash):
1020210207
return size == size_default;
1020310208

10209+
case offsetof(struct sk_reuseport_md, sk):
10210+
info->reg_type = PTR_TO_SOCKET;
10211+
return size == sizeof(__u64);
10212+
10213+
case offsetof(struct sk_reuseport_md, migrating_sk):
10214+
info->reg_type = PTR_TO_SOCK_COMMON_OR_NULL;
10215+
return size == sizeof(__u64);
10216+
1020410217
/* Fields that allow narrowing */
1020510218
case bpf_ctx_range(struct sk_reuseport_md, eth_protocol):
1020610219
if (size < sizeof_field(struct sk_buff, protocol))
@@ -10273,6 +10286,14 @@ static u32 sk_reuseport_convert_ctx_access(enum bpf_access_type type,
1027310286
case offsetof(struct sk_reuseport_md, bind_inany):
1027410287
SK_REUSEPORT_LOAD_FIELD(bind_inany);
1027510288
break;
10289+
10290+
case offsetof(struct sk_reuseport_md, sk):
10291+
SK_REUSEPORT_LOAD_FIELD(sk);
10292+
break;
10293+
10294+
case offsetof(struct sk_reuseport_md, migrating_sk):
10295+
SK_REUSEPORT_LOAD_FIELD(migrating_sk);
10296+
break;
1027610297
}
1027710298

1027810299
return insn - insn_buf;

0 commit comments

Comments
 (0)