From patchwork Wed Apr 20 18:01:45 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Wang Nan X-Patchwork-Id: 66243 Delivered-To: patch@linaro.org Received: by 10.140.93.198 with SMTP id d64csp2596324qge; Wed, 20 Apr 2016 11:04:59 -0700 (PDT) X-Received: by 10.98.72.199 with SMTP id q68mr5167166pfi.164.1461175499875; Wed, 20 Apr 2016 11:04:59 -0700 (PDT) Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id v11si2451848par.167.2016.04.20.11.04.59; Wed, 20 Apr 2016 11:04:59 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752702AbcDTSE5 (ORCPT + 29 others); Wed, 20 Apr 2016 14:04:57 -0400 Received: from szxga03-in.huawei.com ([119.145.14.66]:26304 "EHLO szxga03-in.huawei.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752295AbcDTSCd (ORCPT ); Wed, 20 Apr 2016 14:02:33 -0400 Received: from 172.24.1.137 (EHLO szxeml430-hub.china.huawei.com) ([172.24.1.137]) by szxrg03-dlp.huawei.com (MOS 4.4.3-GA FastPath queued) with ESMTP id CAG94114; Thu, 21 Apr 2016 02:02:13 +0800 (CST) Received: from linux-4hy3.site (10.107.193.248) by szxeml430-hub.china.huawei.com (10.82.67.185) with Microsoft SMTP Server id 14.3.235.1; Thu, 21 Apr 2016 02:02:03 +0800 From: Wang Nan To: , , CC: , , Wang Nan , Arnaldo Carvalho de Melo , "Alexei Starovoitov" , Jiri Olsa , Li Zefan Subject: [RFC PATCH 05/13] bpf tools: Save engine type in bpf_program Date: Wed, 20 Apr 2016 18:01:45 +0000 Message-ID: <1461175313-38310-6-git-send-email-wangnan0@huawei.com> X-Mailer: git-send-email 1.8.3.4 In-Reply-To: <1461175313-38310-1-git-send-email-wangnan0@huawei.com> References: <1461175313-38310-1-git-send-email-wangnan0@huawei.com> MIME-Version: 1.0 X-Originating-IP: [10.107.193.248] X-CFilter-Loop: Reflected X-Mirapoint-Virus-RAPID-Raw: score=unknown(0), refid=str=0001.0A090204.5717C425.00C0, ss=1, re=0.000, recu=0.000, reip=0.000, cl=1, cld=1, fgs=0, ip=0.0.0.0, so=2013-05-26 15:14:31, dmn=2013-03-21 17:37:32 X-Mirapoint-Loop-Id: 0269fea13e414d452e59d5a77cab19d7 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Add an 'engine' field in bpf_program to indicate whether a program is a ubpf program or kernel bpf program. For compatibility, the default engine is kernel bpf, unless explicitly set to ubpf using bpf_program__set_ubpf(). Signed-off-by: Wang Nan Cc: Arnaldo Carvalho de Melo Cc: Alexei Starovoitov Cc: Brendan Gregg Cc: Jiri Olsa Cc: Li Zefan --- tools/lib/bpf/libbpf.c | 40 ++++++++++++++++++++++++++++++++++++++++ tools/lib/bpf/libbpf.h | 4 ++++ 2 files changed, 44 insertions(+) -- 1.8.3.4 diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c index dd5a107..3755846 100644 --- a/tools/lib/bpf/libbpf.c +++ b/tools/lib/bpf/libbpf.c @@ -80,6 +80,7 @@ static const char *libbpf_strerror_table[NR_ERRNO] = { [ERRCODE_OFFSET(VERIFY)] = "Kernel verifier blocks program loading", [ERRCODE_OFFSET(PROG2BIG)] = "Program too big", [ERRCODE_OFFSET(KVER)] = "Incorrect kernel version", + [ERRCODE_OFFSET(NOUBPF)] = "UBPF support is not compiled", }; int libbpf_strerror(int err, char *buf, size_t size) @@ -147,6 +148,11 @@ union prog_instance { * linux/filter.h. */ struct bpf_program { + enum { + ENGINE_UNKNOWN, + ENGINE_KBPF, + ENGINE_UBPF, + } engine; /* Index in elf obj file, for relocation use. */ int idx; char *section_name; @@ -291,6 +297,7 @@ bpf_program__init(void *data, size_t size, char *name, int idx, memcpy(prog->insns, data, prog->insns_cnt * sizeof(struct bpf_insn)); prog->idx = idx; + prog->engine = ENGINE_UNKNOWN; prog->instances.array = NULL; prog->instances.nr = -1; @@ -941,6 +948,11 @@ bpf_program__load(struct bpf_program *prog, { int err = 0, fd, i; + if (prog->engine == ENGINE_UNKNOWN) + prog->engine = ENGINE_KBPF; + if (prog->engine != ENGINE_KBPF) + return -EINVAL; + if (prog->instances.nr < 0 || !prog->instances.array) { if (prog->preprocessor) { pr_warning("Internal error: can't load program '%s'\n", @@ -1318,6 +1330,34 @@ int bpf_program__nth_fd(struct bpf_program *prog, int n) return fd; } +#ifdef HAVE_UBPF_SUPPORT +int bpf_program__set_ubpf(struct bpf_program *prog) +{ + if (prog->engine != ENGINE_UNKNOWN) { + pr_warning("Can't set program %s to ubpf\n", + prog->section_name); + return -EINVAL; + } + prog->engine = ENGINE_UBPF; + return 0; +} + +bool bpf_program__is_ubpf(struct bpf_program *prog) +{ + return prog->engine == ENGINE_UBPF; +} +#else +int bpf_program__set_ubpf(struct bpf_program *prog __maybe_unused) +{ + return -LIBBPF_ERRNO__NOUBPF; +} + +bool bpf_program__is_ubpf(struct bpf_program *prog __maybe_unused) +{ + return false; +} +#endif + int bpf_map__get_fd(struct bpf_map *map) { if (!map) diff --git a/tools/lib/bpf/libbpf.h b/tools/lib/bpf/libbpf.h index a51594c..f6965ce 100644 --- a/tools/lib/bpf/libbpf.h +++ b/tools/lib/bpf/libbpf.h @@ -26,6 +26,7 @@ enum libbpf_errno { LIBBPF_ERRNO__VERIFY, /* Kernel verifier blocks program loading */ LIBBPF_ERRNO__PROG2BIG, /* Program too big */ LIBBPF_ERRNO__KVER, /* Incorrect kernel version */ + LIBBPF_ERRNO__NOUBPF, /* UBPF support is not compiled */ __LIBBPF_ERRNO__END, }; @@ -86,6 +87,9 @@ int bpf_program__get_private(struct bpf_program *prog, const char *bpf_program__title(struct bpf_program *prog, bool needs_copy); +int bpf_program__set_ubpf(struct bpf_program *prog); +bool bpf_program__is_ubpf(struct bpf_program *prog); + int bpf_program__fd(struct bpf_program *prog); struct bpf_insn;