A 'config' section is allowed to enable eBPF object file to pass
something to user. libbpf doesn't use config string.

To make further processing easiler, this patch converts '\0' in the
whole config strings into '\n'

Signed-off-by: Wang Nan <wangn...@huawei.com>
---
 tools/lib/bpf/libbpf.c | 48 ++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 48 insertions(+)

diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c
index 6ee5f3c..43b22a5 100644
--- a/tools/lib/bpf/libbpf.c
+++ b/tools/lib/bpf/libbpf.c
@@ -84,6 +84,7 @@ struct bpf_object {
        u32 kern_version;
        void *maps_buf;
        size_t maps_buf_sz;
+       char *config_str;
 
        /*
         * Information when doing elf related work. Only valid if fd
@@ -267,6 +268,48 @@ static int bpf_obj_maps_init(struct bpf_object *obj, void 
*data,
        return 0;
 }
 
+static int bpf_obj_config_init(struct bpf_object *obj, void *data,
+                              size_t size)
+{
+       char *config_str;
+       char *p, *pend;
+
+       if (size == 0) {
+               pr_debug("bpf: config section in %s empty\n",
+                        obj->path);
+               return 0;
+       }
+       if (obj->config_str) {
+               pr_warning("bpf: multiple config section in %s\n",
+                          obj->path);
+               return -EEXIST;
+       }
+
+       config_str = malloc(size + 1);
+       if (!config_str) {
+               pr_warning("bpf: malloc config string failed\n");
+               return -ENOMEM;
+       }
+
+       memcpy(config_str, data, size);
+
+       /*
+        * It is possible that config section contains multiple
+        * Make it a big string by converting all '\0' to '\n' and
+        * append final '\0'.
+        */
+       pend = config_str + size;
+       for (p = config_str; p < pend; p++)
+               *p == '\0' ? *p = '\n' : 0 ;
+       *pend = '\0';
+
+       obj->config_str = config_str;
+       pr_debug("--- CONFIG STRING IN %s: ---\n%s\n",
+                obj->path, config_str);
+       pr_debug("----------------------------\n");
+       return 0;
+}
+
 static int bpf_obj_elf_collect(struct bpf_object *obj)
 {
        Elf *elf = obj->elf.elf;
@@ -324,6 +367,9 @@ static int bpf_obj_elf_collect(struct bpf_object *obj)
                else if (strcmp(name, "maps") == 0)
                        err = bpf_obj_maps_init(obj, data->d_buf,
                                                data->d_size);
+               else if (strcmp(name, "config") == 0)
+                       err = bpf_obj_config_init(obj, data->d_buf,
+                                                 data->d_size);
                if (err)
                        goto out;
        }
@@ -387,5 +433,7 @@ void bpf_close_object(struct bpf_object *obj)
                free(obj->path);
        if (obj->maps_buf)
                free(obj->maps_buf);
+       if (obj->config_str)
+               free(obj->config_str);
        free(obj);
 }
-- 
1.8.3.4

--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Please read the FAQ at  http://www.tux.org/lkml/

Reply via email to