tools lib bpf: Add private field for bpf_object
authorWang Nan <wangnan0@huawei.com>
Sat, 26 Nov 2016 07:03:26 +0000 (07:03 +0000)
committerArnaldo Carvalho de Melo <acme@redhat.com>
Tue, 29 Nov 2016 15:09:41 +0000 (12:09 -0300)
Similar to other classes defined in libbpf.h (map and program), allow
'object' class has its own private data.

Signed-off-by: Wang Nan <wangnan0@huawei.com>
Acked-by: Alexei Starovoitov <ast@kernel.org>
Cc: He Kuang <hekuang@huawei.com>
Cc: Joe Stringer <joe@ovn.org>
Cc: Zefan Li <lizefan@huawei.com>
Cc: pi3orama@163.com
Link: http://lkml.kernel.org/r/20161126070354.141764-3-wangnan0@huawei.com
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
tools/lib/bpf/libbpf.c
tools/lib/bpf/libbpf.h

index 96a2b2ff1212e75fa99b86821cbeb23d24c2c5c7..866d5cdeffc74d42dce44de8c51c0aab6dbc854c 100644 (file)
@@ -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)
 {
index dd7a513efb10e6337d962fa50f554ad73831ba67..0c0b0127e03ed3c7bd1d4a4ae43ed72d65422fef 100644 (file)
@@ -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,