From patchwork Wed Apr 15 19:27:48 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yonghong Song X-Patchwork-Id: 221144 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-9.8 required=3.0 tests=DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_PATCH, MAILING_LIST_MULTI, SIGNED_OFF_BY, SPF_HELO_NONE, SPF_PASS, URIBL_BLOCKED, USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 3E182C2BA19 for ; Wed, 15 Apr 2020 19:29:48 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 0862920774 for ; Wed, 15 Apr 2020 19:29:48 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (1024-bit key) header.d=fb.com header.i=@fb.com header.b="I59Yp5ur" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2437569AbgDOT3p (ORCPT ); Wed, 15 Apr 2020 15:29:45 -0400 Received: from mx0b-00082601.pphosted.com ([67.231.153.30]:30016 "EHLO mx0b-00082601.pphosted.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2411824AbgDOT23 (ORCPT ); Wed, 15 Apr 2020 15:28:29 -0400 Received: from pps.filterd (m0109331.ppops.net [127.0.0.1]) by mx0a-00082601.pphosted.com (8.16.0.42/8.16.0.42) with SMTP id 03FJSNVA024724 for ; Wed, 15 Apr 2020 12:28:27 -0700 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=fb.com; h=from : to : cc : subject : date : message-id : in-reply-to : references : mime-version : content-transfer-encoding : content-type; s=facebook; bh=gnrWsuQzGwdz6NH/Zp1T0ov1gMBeNN41luPHdjqNYYo=; b=I59Yp5urd+dUUkHs7QAYP8ZKspkrXSIttcpQMVlWMMNIO7V06TBmb+fin2cBQUwzPplw TvK3E7uAwC/Bd9DBb7SKwJVRtJXg2lsT8uQoqlCwmXwXxgRtnI79mqhjDP+l+iacXo08 LD8oOZjNdwpvzOCLMipRkkZye0Fs6S29utY= Received: from mail.thefacebook.com ([163.114.132.120]) by mx0a-00082601.pphosted.com with ESMTP id 30dn7gqkrj-8 (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128 verify=NOT) for ; Wed, 15 Apr 2020 12:28:27 -0700 Received: from intmgw003.03.ash8.facebook.com (2620:10d:c085:208::f) by mail.thefacebook.com (2620:10d:c085:11d::6) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.1847.3; Wed, 15 Apr 2020 12:28:01 -0700 Received: by devbig003.ftw2.facebook.com (Postfix, from userid 128203) id 351353700AF5; Wed, 15 Apr 2020 12:27:48 -0700 (PDT) Smtp-Origin-Hostprefix: devbig From: Yonghong Song Smtp-Origin-Hostname: devbig003.ftw2.facebook.com To: Andrii Nakryiko , , Martin KaFai Lau , CC: Alexei Starovoitov , Daniel Borkmann , Smtp-Origin-Cluster: ftw2c04 Subject: [RFC PATCH bpf-next v2 07/17] bpf: add netlink and ipv6_route targets Date: Wed, 15 Apr 2020 12:27:48 -0700 Message-ID: <20200415192748.4083243-1-yhs@fb.com> X-Mailer: git-send-email 2.24.1 In-Reply-To: <20200415192740.4082659-1-yhs@fb.com> References: <20200415192740.4082659-1-yhs@fb.com> MIME-Version: 1.0 X-FB-Internal: Safe X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10434:6.0.138, 18.0.676 definitions=2020-04-15_07:2020-04-14,2020-04-15 signatures=0 X-Proofpoint-Spam-Details: rule=fb_default_notspam policy=fb_default score=0 priorityscore=1501 bulkscore=0 phishscore=0 adultscore=0 spamscore=0 mlxscore=0 mlxlogscore=937 lowpriorityscore=0 suspectscore=0 clxscore=1015 malwarescore=0 impostorscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2003020000 definitions=main-2004150144 X-FB-Internal: deliver Sender: netdev-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org This patch added netlink and ipv6_route targets, using the same seq_ops (except show()) for /proc/net/{netlink,ipv6_route}. Since module is not supported for now, ipv6_route is supported only if the IPV6 is built-in, i.e., not compiled as a module. The restriction can be lifted once module is properly supported for bpfdump. Signed-off-by: Yonghong Song --- include/linux/bpf.h | 8 +++- kernel/bpf/dump.c | 13 ++++++ net/ipv6/ip6_fib.c | 71 +++++++++++++++++++++++++++++- net/ipv6/route.c | 29 +++++++++++++ net/netlink/af_netlink.c | 94 +++++++++++++++++++++++++++++++++++++++- 5 files changed, 210 insertions(+), 5 deletions(-) diff --git a/include/linux/bpf.h b/include/linux/bpf.h index 1179ca3d0230..401e5bf921a2 100644 --- a/include/linux/bpf.h +++ b/include/linux/bpf.h @@ -1124,6 +1124,12 @@ struct bpf_dump_reg { u32 target_feature; }; +struct bpf_dump_meta { + struct seq_file *seq; + u64 session_id; + u64 seq_num; +}; + int bpf_dump_reg_target(struct bpf_dump_reg *reg_info); int bpf_dump_set_target_info(u32 target_fd, struct bpf_prog *prog); int bpf_fd_dump_create(u32 prog_fd, const char __user *dumper_name, @@ -1131,7 +1137,7 @@ int bpf_fd_dump_create(u32 prog_fd, const char __user *dumper_name, int bpf_prog_dump_create(struct bpf_prog *prog); struct bpf_prog *bpf_dump_get_prog(struct seq_file *seq, u32 priv_data_size, u64 *session_id, u64 *seq_num, bool is_last); - +int bpf_dump_run_prog(struct bpf_prog *prog, void *ctx); int bpf_percpu_hash_copy(struct bpf_map *map, void *key, void *value); int bpf_percpu_array_copy(struct bpf_map *map, void *key, void *value); int bpf_percpu_hash_update(struct bpf_map *map, void *key, void *value, diff --git a/kernel/bpf/dump.c b/kernel/bpf/dump.c index c6d4d64aaa8e..789b35772a81 100644 --- a/kernel/bpf/dump.c +++ b/kernel/bpf/dump.c @@ -487,6 +487,19 @@ struct bpf_prog *bpf_dump_get_prog(struct seq_file *seq, u32 priv_data_size, return extra_data->prog; } +int bpf_dump_run_prog(struct bpf_prog *prog, void *ctx) +{ + int ret; + + migrate_disable(); + rcu_read_lock(); + ret = BPF_PROG_RUN(prog, ctx); + rcu_read_unlock(); + migrate_enable(); + + return ret; +} + int bpf_dump_reg_target(struct bpf_dump_reg *reg_info) { struct bpfdump_target_info *tinfo, *ptinfo; diff --git a/net/ipv6/ip6_fib.c b/net/ipv6/ip6_fib.c index 46ed56719476..f5a48511d233 100644 --- a/net/ipv6/ip6_fib.c +++ b/net/ipv6/ip6_fib.c @@ -2467,7 +2467,7 @@ void fib6_gc_cleanup(void) } #ifdef CONFIG_PROC_FS -static int ipv6_route_seq_show(struct seq_file *seq, void *v) +static int ipv6_route_native_seq_show(struct seq_file *seq, void *v) { struct fib6_info *rt = v; struct ipv6_route_iter *iter = seq->private; @@ -2625,7 +2625,7 @@ static bool ipv6_route_iter_active(struct ipv6_route_iter *iter) return w->node && !(w->state == FWS_U && w->node == w->root); } -static void ipv6_route_seq_stop(struct seq_file *seq, void *v) +static void ipv6_route_native_seq_stop(struct seq_file *seq, void *v) __releases(RCU_BH) { struct net *net = seq_file_net(seq); @@ -2637,6 +2637,73 @@ static void ipv6_route_seq_stop(struct seq_file *seq, void *v) rcu_read_unlock_bh(); } +#if IS_BUILTIN(CONFIG_IPV6) && defined(CONFIG_BPF_SYSCALL) +struct bpfdump__ipv6_route { + struct bpf_dump_meta *meta; + struct fib6_info *rt; +}; + +static int ipv6_route_prog_seq_show(struct bpf_prog *prog, struct seq_file *seq, + u64 session_id, u64 seq_num, void *v) +{ + struct bpfdump__ipv6_route ctx; + struct bpf_dump_meta meta; + int ret; + + meta.seq = seq; + meta.session_id = session_id; + meta.seq_num = seq_num; + ctx.meta = &meta; + ctx.rt = v; + ret = bpf_dump_run_prog(prog, &ctx); + return ret == 0 ? 0 : -EINVAL; +} + +static int ipv6_route_seq_show(struct seq_file *seq, void *v) +{ + struct ipv6_route_iter *iter = seq->private; + u64 session_id, seq_num; + struct bpf_prog *prog; + int ret; + + prog = bpf_dump_get_prog(seq, sizeof(struct ipv6_route_iter), + &session_id, &seq_num, false); + if (!prog) + return ipv6_route_native_seq_show(seq, v); + + ret = ipv6_route_prog_seq_show(prog, seq, session_id, seq_num, v); + iter->w.leaf = NULL; + + return ret; +} + +static void ipv6_route_seq_stop(struct seq_file *seq, void *v) +{ + u64 session_id, seq_num; + struct bpf_prog *prog; + + if (!v) { + prog = bpf_dump_get_prog(seq, sizeof(struct ipv6_route_iter), + &session_id, &seq_num, true); + if (prog) + ipv6_route_prog_seq_show(prog, seq, session_id, + seq_num, v); + } + + ipv6_route_native_seq_stop(seq, v); +} +#else +static int ipv6_route_seq_show(struct seq_file *seq, void *v) +{ + return ipv6_route_native_seq_show(seq, v); +} + +static void ipv6_route_seq_stop(struct seq_file *seq, void *v) +{ + ipv6_route_native_seq_stop(seq, v); +} +#endif + const struct seq_operations ipv6_route_seq_ops = { .start = ipv6_route_seq_start, .next = ipv6_route_seq_next, diff --git a/net/ipv6/route.c b/net/ipv6/route.c index 310cbddaa533..ea87d3f2c363 100644 --- a/net/ipv6/route.c +++ b/net/ipv6/route.c @@ -6390,10 +6390,31 @@ void __init ip6_route_init_special_entries(void) #endif } +#if IS_BUILTIN(CONFIG_IPV6) +#if defined(CONFIG_BPF_SYSCALL) && defined(CONFIG_PROC_FS) +int __init __bpfdump__ipv6_route(struct bpf_dump_meta *meta, struct fib6_info *rt) +{ + return 0; +} +#endif +#endif + int __init ip6_route_init(void) { int ret; int cpu; +#if IS_BUILTIN(CONFIG_IPV6) +#if defined(CONFIG_BPF_SYSCALL) && defined(CONFIG_PROC_FS) + struct bpf_dump_reg reg_info = { + .target = "ipv6_route", + .target_proto = "__bpfdump__ipv6_route", + .prog_ctx_type_name = "bpfdump__ipv6_route", + .seq_ops = &ipv6_route_seq_ops, + .seq_priv_size = sizeof(struct ipv6_route_iter), + .target_feature = BPF_DUMP_SEQ_NET_PRIVATE, + }; +#endif +#endif ret = -ENOMEM; ip6_dst_ops_template.kmem_cachep = @@ -6452,6 +6473,14 @@ int __init ip6_route_init(void) if (ret) goto out_register_late_subsys; +#if IS_BUILTIN(CONFIG_IPV6) +#if defined(CONFIG_BPF_SYSCALL) && defined(CONFIG_PROC_FS) + ret = bpf_dump_reg_target(®_info); + if (ret) + goto out_register_late_subsys; +#endif +#endif + for_each_possible_cpu(cpu) { struct uncached_list *ul = per_cpu_ptr(&rt6_uncached_list, cpu); diff --git a/net/netlink/af_netlink.c b/net/netlink/af_netlink.c index 5ded01ca8b20..fe9a10642c39 100644 --- a/net/netlink/af_netlink.c +++ b/net/netlink/af_netlink.c @@ -2596,7 +2596,7 @@ static void *netlink_seq_next(struct seq_file *seq, void *v, loff_t *pos) return __netlink_seq_next(seq); } -static void netlink_seq_stop(struct seq_file *seq, void *v) +static void netlink_native_seq_stop(struct seq_file *seq, void *v) { struct nl_seq_iter *iter = seq->private; @@ -2607,7 +2607,7 @@ static void netlink_seq_stop(struct seq_file *seq, void *v) } -static int netlink_seq_show(struct seq_file *seq, void *v) +static int netlink_native_seq_show(struct seq_file *seq, void *v) { if (v == SEQ_START_TOKEN) { seq_puts(seq, @@ -2634,6 +2634,80 @@ static int netlink_seq_show(struct seq_file *seq, void *v) return 0; } +#ifdef CONFIG_BPF_SYSCALL +struct bpfdump__netlink { + struct bpf_dump_meta *meta; + struct netlink_sock *sk; +}; + +int __init __bpfdump__netlink(struct bpf_dump_meta *meta, struct netlink_sock *sk) +{ + return 0; +} + +static int netlink_prog_seq_show(struct bpf_prog *prog, struct seq_file *seq, + u64 session_id, u64 seq_num, void *v) +{ + struct bpfdump__netlink ctx; + struct bpf_dump_meta meta; + int ret = 0; + + meta.seq = seq; + meta.session_id = session_id; + meta.seq_num = seq_num; + ctx.meta = &meta; + ctx.sk = nlk_sk((struct sock *)v); + ret = bpf_dump_run_prog(prog, &ctx); + + return ret == 0 ? 0 : -EINVAL; +} + +static int netlink_seq_show(struct seq_file *seq, void *v) +{ + u64 session_id, seq_num; + struct bpf_prog *prog; + + prog = bpf_dump_get_prog(seq, sizeof(struct nl_seq_iter), + &session_id, &seq_num, false); + if (!prog) + return netlink_native_seq_show(seq, v); + + if (v == SEQ_START_TOKEN) + return 0; + + return netlink_prog_seq_show(prog, seq, session_id, + seq_num - 1, v); +} + +static void netlink_seq_stop(struct seq_file *seq, void *v) +{ + u64 session_id, seq_num; + struct bpf_prog *prog; + + if (!v) { + prog = bpf_dump_get_prog(seq, sizeof(struct nl_seq_iter), + &session_id, &seq_num, true); + if (prog) { + if (seq_num) + seq_num = seq_num - 1; + netlink_prog_seq_show(prog, seq, session_id, + seq_num, v); + } + } + + netlink_native_seq_stop(seq, v); +} +#else +static int netlink_seq_show(struct seq_file *seq, void *v) +{ + return netlink_native_seq_show(seq, v); +} +static void netlink_seq_stop(struct seq_file *seq, void *v) +{ + netlink_native_seq_stop(seq, v); +} +#endif + static const struct seq_operations netlink_seq_ops = { .start = netlink_seq_start, .next = netlink_seq_next, @@ -2744,6 +2818,16 @@ static int __init netlink_proto_init(void) { int i; int err = proto_register(&netlink_proto, 0); +#if defined(CONFIG_BPF_SYSCALL) && defined(CONFIG_PROC_FS) + struct bpf_dump_reg reg_info = { + .target = "netlink", + .target_proto = "__bpfdump__netlink", + .prog_ctx_type_name = "bpfdump_netlink", + .seq_ops = &netlink_seq_ops, + .seq_priv_size = sizeof(struct nl_seq_iter), + .target_feature = BPF_DUMP_SEQ_NET_PRIVATE, + }; +#endif if (err != 0) goto out; @@ -2764,6 +2848,12 @@ static int __init netlink_proto_init(void) } } +#if defined(CONFIG_BPF_SYSCALL) && defined(CONFIG_PROC_FS) + err = bpf_dump_reg_target(®_info); + if (err) + goto out; +#endif + netlink_add_usersock_entry(); sock_register(&netlink_family_ops);