Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S965810AbcKOEIG (ORCPT ); Mon, 14 Nov 2016 23:08:06 -0500 Received: from szxga02-in.huawei.com ([119.145.14.65]:54604 "EHLO szxga02-in.huawei.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1755250AbcKOEIE (ORCPT ); Mon, 14 Nov 2016 23:08:04 -0500 From: Wang Nan To: , CC: , , , , Wang Nan Subject: [PATCH 06/34] tools lib bpf: Add private field for bpf_object Date: Tue, 15 Nov 2016 04:05:49 +0000 Message-ID: <20161115040617.69788-7-wangnan0@huawei.com> X-Mailer: git-send-email 2.10.1 In-Reply-To: <20161115040617.69788-1-wangnan0@huawei.com> References: <20161115040617.69788-1-wangnan0@huawei.com> MIME-Version: 1.0 Content-Type: text/plain X-Originating-IP: [10.107.193.248] X-CFilter-Loop: Reflected Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Length: 2211 Lines: 78 Like other classes defined in libbpf.h (like map and program), allow 'object' class has its own private data. Signed-off-by: Wang Nan Cc: Alexei Starovoitov Cc: Arnaldo Carvalho de Melo Cc: Li Zefan --- tools/lib/bpf/libbpf.c | 23 +++++++++++++++++++++++ tools/lib/bpf/libbpf.h | 5 +++++ 2 files changed, 28 insertions(+) diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c index 96a2b2f..866d5cd 100644 --- a/tools/lib/bpf/libbpf.c +++ b/tools/lib/bpf/libbpf.c @@ -229,6 +229,10 @@ struct bpf_object { * all objects. */ struct list_head list; + + void *priv; + bpf_object_clear_priv_t clear_priv; + char path[]; }; #define obj_elf_valid(o) ((o)->efile.elf) @@ -1229,6 +1233,9 @@ void bpf_object__close(struct bpf_object *obj) if (!obj) return; + if (obj->clear_priv) + obj->clear_priv(obj, obj->priv); + bpf_object__elf_finish(obj); bpf_object__unload(obj); @@ -1282,6 +1289,22 @@ unsigned int bpf_object__kversion(struct bpf_object *obj) return obj ? obj->kern_version : 0; } +int bpf_object__set_priv(struct bpf_object *obj, void *priv, + bpf_object_clear_priv_t clear_priv) +{ + if (obj->priv && obj->clear_priv) + obj->clear_priv(obj, obj->priv); + + obj->priv = priv; + obj->clear_priv = clear_priv; + return 0; +} + +void *bpf_object__priv(struct bpf_object *obj) +{ + return obj ? obj->priv : ERR_PTR(-EINVAL); +} + struct bpf_program * bpf_program__next(struct bpf_program *prev, struct bpf_object *obj) { diff --git a/tools/lib/bpf/libbpf.h b/tools/lib/bpf/libbpf.h index dd7a513..0c0b012 100644 --- a/tools/lib/bpf/libbpf.h +++ b/tools/lib/bpf/libbpf.h @@ -79,6 +79,11 @@ struct bpf_object *bpf_object__next(struct bpf_object *prev); (pos) != NULL; \ (pos) = (tmp), (tmp) = bpf_object__next(tmp)) +typedef void (*bpf_object_clear_priv_t)(struct bpf_object *, void *); +int bpf_object__set_priv(struct bpf_object *obj, void *priv, + bpf_object_clear_priv_t clear_priv); +void *bpf_object__priv(struct bpf_object *prog); + /* Accessors of bpf_program. */ struct bpf_program; struct bpf_program *bpf_program__next(struct bpf_program *prog, -- 2.10.1