Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1754125AbdLHOxp (ORCPT ); Fri, 8 Dec 2017 09:53:45 -0500 Received: from mx0b-00082601.pphosted.com ([67.231.153.30]:51884 "EHLO mx0a-00082601.pphosted.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1753760AbdLHOxi (ORCPT ); Fri, 8 Dec 2017 09:53:38 -0500 From: Roman Gushchin To: CC: , , , , , , , Quentin Monnet , David Ahern Subject: [PATCH v3 net-next 1/4] libbpf: add ability to guess program type based on section name Date: Fri, 8 Dec 2017 14:52:33 +0000 Message-ID: <20171208145236.12635-2-guro@fb.com> X-Mailer: git-send-email 2.14.3 In-Reply-To: <20171208145236.12635-1-guro@fb.com> References: <20171208145236.12635-1-guro@fb.com> MIME-Version: 1.0 Content-Type: text/plain X-Originating-IP: [2620:10d:c092:180::1:b43a] X-ClientProxiedBy: VI1PR0102CA0086.eurprd01.prod.exchangelabs.com (2603:10a6:803:15::27) To CO1PR15MB1078.namprd15.prod.outlook.com (2a01:111:e400:7b66::8) X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: 380ea017-e41e-429d-5b5e-08d53e4b669f X-Microsoft-Antispam: UriScan:;BCL:0;PCL:0;RULEID:(4534020)(4602075)(4627115)(201703031133081)(201702281549075)(5600026)(4604075)(2017052603307);SRVR:CO1PR15MB1078; X-Microsoft-Exchange-Diagnostics: 1;CO1PR15MB1078;3:bUord5enfNqtq6R9qMdm9DwnuxjEQlqZaMTwCJ3tx3gQUMiXSbeLqkPlSzH9MqiO6nFIMzsYqk4gc5yyxI5V1Te+SFqwRT5AhLxo90o+gh2jNfd3Txtg38EWemm6CN8jrshSISY9302yRLe0lv+CVfT6m3k2+Jf3URtRGq2zcV+sK/aD0Q6NIVRQGr+YcC8TVtoVg8UdobBlLwELE9nPAIHCK0xEUwjQDxaJEiQ+oVT4qu8crTVeJngASyKa1c2U;25:kLxfeghu+EarT6nyi4jLrK2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qIeqoFzhWyeppRt/XiCjf3IWcr0k1KTsjUVxx98isYSTZfxrHcrO2V2;4:PsVFohH02tODvSEc5eSiqxXZOmHPSwmgBuDJ3UtE3T5E7DT5UZbFTM1mIRmdw395K/I9nQOfxhWSO+3owbtJavK87vqoTuhQMwSM5geY1GWLPFxQSgdsfZImEJIE/Xc7x0+Gj8B8rCgf8FfTeqndYUPu856rP50NbGUxJ94H21MqLKJCRyCNyVOY0tRaBQiJOzvVIyMrEfKQeJlUyd+Hx+Fn0sYsHG1ZEliMoHuGtBwK/hBFIVY6pZ8EmTFAsHQA6MX2fJdLOX0j+pEF5CjKBIDEJJ3tbU3Rwi5azrDNiBEOQDQY6uwWVBmybUS84B+E X-Microsoft-Antispam-PRVS: X-Exchange-Antispam-Report-Test: UriScan:(67672495146484); X-Exchange-Antispam-Report-CFA-Test: BCL:0;PCL:0;RULEID:(11241501159)(6040450)(2401047)(8121501046)(5005006)(93006095)(93001095)(3002001)(10201501046)(3231022)(6041248)(201703131423075)(201702281528075)(201703061421075)(201703061406153)(20161123562025)(20161123564025)(20161123558100)(20161123560025)(20161123555025)(6072148)(201708071742011);SRVR:CO1PR15MB1078;BCL:0;PCL:0;RULEID:(100000803101)(100110400095);SRVR:CO1PR15MB1078; X-Forefront-PRVS: 0515208626 X-Forefront-Antispam-Report: SFV:NSPM;SFS:(10019020)(366004)(376002)(346002)(189003)(199004)(7736002)(68736007)(6116002)(86362001)(575784001)(1076002)(105586002)(48376002)(106356001)(33646002)(2351001)(50226002)(2361001)(305945005)(2906002)(53416004)(2950100002)(6666003)(8936002)(81166006)(50466002)(8676002)(6916009)(81156014)(5660300001)(52116002)(51416003)(69596002)(6506006)(478600001)(53936002)(52396003)(16586007)(316002)(6486002)(39060400002)(4326008)(36756003)(6512007)(76176011)(25786009)(47776003)(97736004)(54906003)(42262002);DIR:OUT;SFP:1102;SCL:1;SRVR:CO1PR15MB1078;H:castle.thefacebook.com;FPR:;SPF:None;PTR:InfoNoRecords;MX:1;A:1;LANG:en; X-Microsoft-Exchange-Diagnostics: =?us-ascii?Q?1;CO1PR15MB1078;23:DCb7rGOpadv44+KVuiFzvtwLZnwxNeJXrWKDSn4Wk?= =?us-ascii?Q?xByhMVvt3LcRBhDOn1yjVzzv9M4Xsggl0jbPHpT4PmLfFcLxznHoyBFGH4JO?= =?us-ascii?Q?JJ7flT7dcSkYzLNDl+NWRKO7lSrrvKBO3AybyvilSXVtuEfsBD24jSN06zol?= =?us-ascii?Q?7Va0IJHeB68f+yKmeo8IjXGwXtEPVujhHHRV4SFmvME1qmMHEZf3LxbiMqLE?= =?us-ascii?Q?gvnkdLY+dtdPQeaZtbgwkBxbptjb8Tv27kBLEcRZf2clGstf+qkuxCXovUi0?= =?us-ascii?Q?cqNsfh0NK656v4SCBljenP8cOyxuXZNY+CN3fD0TmuVPRdHTib4fSAln5zFt?= =?us-ascii?Q?qPijX2C52vGJt6yuAYGrLHxKN7xFo0zr/NZUIYu2ZnEIPHfsL/vFk5iuuIUw?= =?us-ascii?Q?Utc9ElkBu/Cr3yRq9skMo2q48IEp58KxRQhk433J6D4O+qeSfZehM/wGri4z?= =?us-ascii?Q?NoMBIYG0at/CGnvwNdHi2YLZoG50/OeBLt3GaOwVzYcKMhnYmv1CR5Xcp669?= =?us-ascii?Q?p0Wg54M8FK6MFknI8IEAHipxCGB0Vcitb6oynGbYgdfEnib0JxtYK76JuPb3?= =?us-ascii?Q?4Hlq2yUDZ65RMLqDKCIAjRqH+UPa7q6Iy/U6hP7bmve3Ew/B7B15TcykCzt2?= =?us-ascii?Q?RG5dsQJ/7r6+XxjZtnp32Cc+yBwBzrIRpKrf6UI1fSg7ykwPawhUQt2QOh2U?= =?us-ascii?Q?eLv+ShvfwAti1sVWmE6jsmkKGDtzHZuOh9KW0b8wuI07V0AZkHj8ifxFp4Rn?= =?us-ascii?Q?ruAiy9NewQCMVRISHMwvmObfXrrUPIKpdQ99/tIA5NNG0gqM0RHW6Nw3cmiz?= =?us-ascii?Q?801razs8q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drMDpHwjM=;7:ZlkIh9E37HQAXU6DYWikAacrs+aKwoWQIs/Npp98rIOSYljH7YOS0gejAOPO2qFIM2SqNoHH7B0CvYBWvuThJ6+tE5UXt1LuqtwJUD55YSm8XfxSmtMipzBhhoy6g9TJyaOmFzQOzWCMq/oyLRSXXYDADd9Xo4U11TBD+eAhZKR4B4JsjMC1Am5slEZhmooH/37bhmuBRH9wDVwL1e7llSU9sJiArKKTszqS6NoJJSbQjoPsBOEu4rlI/IlYAKfc SpamDiagnosticOutput: 1:99 SpamDiagnosticMetadata: NSPM X-Microsoft-Exchange-Diagnostics: 1;CO1PR15MB1078;20:+4mC8WJ7U6XWYPFwrDXxlqyGnjm+xUfTskS/D6dXPS5rQJgxF9XwiOaPPlaEnKlVTbzJTzQ7rOFb3ShrLcAKOLoDxIgNCH2rW+ktKJvkS2xl28H5TBtowLhgeSlL6HCY36ciQO6io267kUU5AM7U/bQ0+NAWutIryx797jvGSms= X-MS-Exchange-CrossTenant-OriginalArrivalTime: 08 Dec 2017 14:53:07.4543 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 380ea017-e41e-429d-5b5e-08d53e4b669f X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 8ae927fe-1255-47a7-a2af-5f3a069daaa2 X-MS-Exchange-Transport-CrossTenantHeadersStamped: CO1PR15MB1078 X-OriginatorOrg: fb.com X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10432:,, definitions=2017-12-08_08:,, signatures=0 X-Proofpoint-Spam-Reason: safe X-FB-Internal: Safe Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Length: 2812 Lines: 87 The bpf_prog_load() function will guess program type if it's not specified explicitly. This functionality will be used to implement loading of different programs without asking a user to specify the program type. In first order it will be used by bpftool. Signed-off-by: Roman Gushchin Cc: Alexei Starovoitov Cc: Daniel Borkmann Cc: Jakub Kicinski Cc: Martin KaFai Lau Cc: Quentin Monnet Cc: David Ahern --- tools/lib/bpf/libbpf.c | 51 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c index 5aa45f89da93..205b7822fa0a 100644 --- a/tools/lib/bpf/libbpf.c +++ b/tools/lib/bpf/libbpf.c @@ -1721,6 +1721,45 @@ BPF_PROG_TYPE_FNS(tracepoint, BPF_PROG_TYPE_TRACEPOINT); BPF_PROG_TYPE_FNS(xdp, BPF_PROG_TYPE_XDP); BPF_PROG_TYPE_FNS(perf_event, BPF_PROG_TYPE_PERF_EVENT); +#define BPF_PROG_SEC(string, type) { string, sizeof(string), type } +static const struct { + const char *sec; + size_t len; + enum bpf_prog_type prog_type; +} section_names[] = { + BPF_PROG_SEC("socket", BPF_PROG_TYPE_SOCKET_FILTER), + BPF_PROG_SEC("kprobe/", BPF_PROG_TYPE_KPROBE), + BPF_PROG_SEC("kretprobe/", BPF_PROG_TYPE_KPROBE), + BPF_PROG_SEC("tracepoint/", BPF_PROG_TYPE_TRACEPOINT), + BPF_PROG_SEC("xdp", BPF_PROG_TYPE_XDP), + BPF_PROG_SEC("perf_event", BPF_PROG_TYPE_PERF_EVENT), + BPF_PROG_SEC("cgroup/skb", BPF_PROG_TYPE_CGROUP_SKB), + BPF_PROG_SEC("cgroup/sock", BPF_PROG_TYPE_CGROUP_SOCK), + BPF_PROG_SEC("cgroup/dev", BPF_PROG_TYPE_CGROUP_DEVICE), + BPF_PROG_SEC("sockops", BPF_PROG_TYPE_SOCK_OPS), + BPF_PROG_SEC("sk_skb", BPF_PROG_TYPE_SK_SKB), +}; +#undef BPF_PROG_SEC + +static enum bpf_prog_type bpf_program__guess_type(struct bpf_program *prog) +{ + int i; + + if (!prog->section_name) + goto err; + + for (i = 0; i < ARRAY_SIZE(section_names); i++) + if (strncmp(prog->section_name, section_names[i].sec, + section_names[i].len) == 0) + return section_names[i].prog_type; + +err: + pr_warning("failed to guess program type based on section name %s\n", + prog->section_name); + + return BPF_PROG_TYPE_UNSPEC; +} + int bpf_map__fd(struct bpf_map *map) { return map ? map->fd : -EINVAL; @@ -1832,6 +1871,18 @@ int bpf_prog_load(const char *file, enum bpf_prog_type type, return -ENOENT; } + /* + * If type is not specified, try to guess it based on + * section name. + */ + if (type == BPF_PROG_TYPE_UNSPEC) { + type = bpf_program__guess_type(prog); + if (type == BPF_PROG_TYPE_UNSPEC) { + bpf_object__close(obj); + return -EINVAL; + } + } + bpf_program__set_type(prog, type); err = bpf_object__load(obj); if (err) { -- 2.14.3