Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1422705AbbEOHxn (ORCPT ); Fri, 15 May 2015 03:53:43 -0400 Received: from szxga01-in.huawei.com ([58.251.152.64]:62619 "EHLO szxga01-in.huawei.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S964928AbbEOHxj (ORCPT ); Fri, 15 May 2015 03:53:39 -0400 From: Wang Nan To: , , , , , , , , , , , CC: , , , Subject: [RFC PATCH v2 13/37] tools lib bpf: collect config section in object. Date: Fri, 15 May 2015 07:51:06 +0000 Message-ID: <1431676290-1230-14-git-send-email-wangnan0@huawei.com> X-Mailer: git-send-email 1.8.3.4 In-Reply-To: <1431676290-1230-1-git-send-email-wangnan0@huawei.com> References: <1431676290-1230-1-git-send-email-wangnan0@huawei.com> MIME-Version: 1.0 Content-Type: text/plain X-Originating-IP: [10.107.197.200] X-CFilter-Loop: Reflected Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Length: 2657 Lines: 98 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 --- 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 majordomo@vger.kernel.org More majordomo info at http://vger.kernel.org/majordomo-info.html Please read the FAQ at http://www.tux.org/lkml/