Skip to content

Commit d5e4dda

Browse files
q2venborkmann
authored andcommitted
bpf: Support socket migration by eBPF.
This patch introduces a new bpf_attach_type for BPF_PROG_TYPE_SK_REUSEPORT to check if the attached eBPF program is capable of migrating sockets. When the eBPF program is attached, we run it for socket migration if the expected_attach_type is BPF_SK_REUSEPORT_SELECT_OR_MIGRATE or net.ipv4.tcp_migrate_req is enabled. Currently, the expected_attach_type is not enforced for the BPF_PROG_TYPE_SK_REUSEPORT type of program. Thus, this commit follows the earlier idea in the commit aac3fc3 ("bpf: Post-hooks for sys_bind") to fix up the zero expected_attach_type in bpf_prog_load_fixup_attach_type(). Moreover, this patch adds a new field (migrating_sk) to sk_reuseport_md to select a new listener based on the child socket. migrating_sk varies depending on if it is migrating a request in the accept queue or during 3WHS. - accept_queue : sock (ESTABLISHED/SYN_RECV) - 3WHS : request_sock (NEW_SYN_RECV) In the eBPF program, we can select a new listener by BPF_FUNC_sk_select_reuseport(). Also, we can cancel migration by returning SK_DROP. This feature is useful when listeners have different settings at the socket API level or when we want to free resources as soon as possible. - SK_PASS with selected_sk, select it as a new listener - SK_PASS with selected_sk NULL, fallbacks to the random selection - SK_DROP, cancel the migration. There is a noteworthy point. We select a listening socket in three places, but we do not have struct skb at closing a listener or retransmitting a SYN+ACK. On the other hand, some helper functions do not expect skb is NULL (e.g. skb_header_pointer() in BPF_FUNC_skb_load_bytes(), skb_tail_pointer() in BPF_FUNC_skb_load_bytes_relative()). So we allocate an empty skb temporarily before running the eBPF program. Suggested-by: Martin KaFai Lau <kafai@fb.com> Signed-off-by: Kuniyuki Iwashima <kuniyu@amazon.co.jp> Signed-off-by: Daniel Borkmann <daniel@iogearbox.net> Reviewed-by: Eric Dumazet <edumazet@google.com> Acked-by: Martin KaFai Lau <kafai@fb.com> Link: https://lore.kernel.org/netdev/20201123003828.xjpjdtk4ygl6tg6h@kafai-mbp.dhcp.thefacebook.com/ Link: https://lore.kernel.org/netdev/20201203042402.6cskdlit5f3mw4ru@kafai-mbp.dhcp.thefacebook.com/ Link: https://lore.kernel.org/netdev/20201209030903.hhow5r53l6fmozjn@kafai-mbp.dhcp.thefacebook.com/ Link: https://lore.kernel.org/bpf/20210612123224.12525-10-kuniyu@amazon.co.jp
1 parent e061047 commit d5e4dda

7 files changed

Lines changed: 88 additions & 5 deletions

File tree

‎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/uapi/linux/bpf.h‎

Lines changed: 15 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,7 +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+
*/
54195433
__bpf_md_ptr(struct bpf_sock *, sk);
5434+
__bpf_md_ptr(struct bpf_sock *, migrating_sk);
54205435
};
54215436

54225437
#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: 12 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)
@@ -10207,6 +10210,10 @@ sk_reuseport_is_valid_access(int off, int size,
1020710210
info->reg_type = PTR_TO_SOCKET;
1020810211
return size == sizeof(__u64);
1020910212

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+
1021010217
/* Fields that allow narrowing */
1021110218
case bpf_ctx_range(struct sk_reuseport_md, eth_protocol):
1021210219
if (size < sizeof_field(struct sk_buff, protocol))
@@ -10283,6 +10290,10 @@ static u32 sk_reuseport_convert_ctx_access(enum bpf_access_type type,
1028310290
case offsetof(struct sk_reuseport_md, sk):
1028410291
SK_REUSEPORT_LOAD_FIELD(sk);
1028510292
break;
10293+
10294+
case offsetof(struct sk_reuseport_md, migrating_sk):
10295+
SK_REUSEPORT_LOAD_FIELD(migrating_sk);
10296+
break;
1028610297
}
1028710298

1028810299
return insn - insn_buf;

‎net/core/sock_reuseport.c‎

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -377,13 +377,17 @@ void reuseport_stop_listen_sock(struct sock *sk)
377377
{
378378
if (sk->sk_protocol == IPPROTO_TCP) {
379379
struct sock_reuseport *reuse;
380+
struct bpf_prog *prog;
380381

381382
spin_lock_bh(&reuseport_lock);
382383

383384
reuse = rcu_dereference_protected(sk->sk_reuseport_cb,
384385
lockdep_is_held(&reuseport_lock));
386+
prog = rcu_dereference_protected(reuse->prog,
387+
lockdep_is_held(&reuseport_lock));
385388

386-
if (sock_net(sk)->ipv4.sysctl_tcp_migrate_req) {
389+
if (sock_net(sk)->ipv4.sysctl_tcp_migrate_req ||
390+
(prog && prog->expected_attach_type == BPF_SK_REUSEPORT_SELECT_OR_MIGRATE)) {
387391
/* Migration capable, move sk from the listening section
388392
* to the closed section.
389393
*/
@@ -488,7 +492,7 @@ struct sock *reuseport_select_sock(struct sock *sk,
488492
goto select_by_hash;
489493

490494
if (prog->type == BPF_PROG_TYPE_SK_REUSEPORT)
491-
sk2 = bpf_run_sk_reuseport(reuse, sk, prog, skb, hash);
495+
sk2 = bpf_run_sk_reuseport(reuse, sk, prog, skb, NULL, hash);
492496
else
493497
sk2 = run_bpf_filter(reuse, socks, prog, skb, hdr_len);
494498

@@ -519,6 +523,8 @@ struct sock *reuseport_migrate_sock(struct sock *sk,
519523
{
520524
struct sock_reuseport *reuse;
521525
struct sock *nsk = NULL;
526+
bool allocated = false;
527+
struct bpf_prog *prog;
522528
u16 socks;
523529
u32 hash;
524530

@@ -536,10 +542,30 @@ struct sock *reuseport_migrate_sock(struct sock *sk,
536542
smp_rmb();
537543

538544
hash = migrating_sk->sk_hash;
539-
if (sock_net(sk)->ipv4.sysctl_tcp_migrate_req)
545+
prog = rcu_dereference(reuse->prog);
546+
if (!prog || prog->expected_attach_type != BPF_SK_REUSEPORT_SELECT_OR_MIGRATE) {
547+
if (sock_net(sk)->ipv4.sysctl_tcp_migrate_req)
548+
goto select_by_hash;
549+
goto out;
550+
}
551+
552+
if (!skb) {
553+
skb = alloc_skb(0, GFP_ATOMIC);
554+
if (!skb)
555+
goto out;
556+
allocated = true;
557+
}
558+
559+
nsk = bpf_run_sk_reuseport(reuse, sk, prog, skb, migrating_sk, hash);
560+
561+
if (allocated)
562+
kfree_skb(skb);
563+
564+
select_by_hash:
565+
if (!nsk)
540566
nsk = reuseport_select_sock_by_hash(reuse, hash, socks);
541567

542-
if (nsk && unlikely(!refcount_inc_not_zero(&nsk->sk_refcnt)))
568+
if (IS_ERR_OR_NULL(nsk) || unlikely(!refcount_inc_not_zero(&nsk->sk_refcnt)))
543569
nsk = NULL;
544570

545571
out:

‎tools/include/uapi/linux/bpf.h‎

Lines changed: 15 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,7 +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+
*/
54195433
__bpf_md_ptr(struct bpf_sock *, sk);
5434+
__bpf_md_ptr(struct bpf_sock *, migrating_sk);
54205435
};
54215436

54225437
#define BPF_TAG_SIZE 8

0 commit comments

Comments
 (0)