From patchwork Wed Oct 14 12:41:36 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Wang Nan X-Patchwork-Id: 54948 Return-Path: X-Original-To: linaro@patches.linaro.org Delivered-To: linaro@patches.linaro.org Received: from mail-lb0-f200.google.com (mail-lb0-f200.google.com [209.85.217.200]) by patches.linaro.org (Postfix) with ESMTPS id 0DCC323012 for ; Wed, 14 Oct 2015 12:51:36 +0000 (UTC) Received: by lbwr8 with SMTP id r8sf25435784lbw.0 for ; Wed, 14 Oct 2015 05:51:34 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:delivered-to:from:to:cc:subject:date:message-id :in-reply-to:references:mime-version:content-type:sender:precedence :list-id:x-original-sender:x-original-authentication-results :mailing-list:list-post:list-help:list-archive:list-unsubscribe; bh=i/weqvVL+ow4b1jE1bCzDo7bcbVPk8FKPZd7pCeoXxY=; b=bMJsuli+m+DGps3wQ4zn6errUW+mGnnt9z8/jf7R/tUtvmo4pnnKpF598i8K+1WBFa HLd3ke/4V5DWLmbPOy/Pw98XuZn+IaaNxj3ATuHVFytHFSbGif0EHodbDIz52/DTVphL riSEo2z3ivJVWMkReVAgzoCIG/nO4IXRhI+FzPTUdupiQf3VUOPCIDs1UN3+ulC8xLPP WemcXs39j4gzWTfGmA3X3gZvq1J/lYrgm3xEnNIzC+7N1vHMZJZPde5aKvd/WA6WINH+ 2JKun/JihtQY+2A2ByYTwvlb3VALJULcntpW0zXg7YZeMywkdnDOBV6bw+GEGvE0c8Rr NzkA== X-Gm-Message-State: ALoCoQk113t8kmAd35hZOiRuT9tu7Go4fCCFi5byUK30JqWihP6I/7JyUWNqo/hGSLWP4y0kU2JC X-Received: by 10.112.63.165 with SMTP id h5mr753479lbs.18.1444827094724; Wed, 14 Oct 2015 05:51:34 -0700 (PDT) X-BeenThere: patchwork-forward@linaro.org Received: by 10.25.89.207 with SMTP id n198ls32604lfb.54.gmail; Wed, 14 Oct 2015 05:51:34 -0700 (PDT) X-Received: by 10.112.129.202 with SMTP id ny10mr1477166lbb.112.1444827094556; Wed, 14 Oct 2015 05:51:34 -0700 (PDT) Received: from mail-lb0-f181.google.com (mail-lb0-f181.google.com. [209.85.217.181]) by mx.google.com with ESMTPS id jn7si5440671lbc.125.2015.10.14.05.51.34 for (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 14 Oct 2015 05:51:34 -0700 (PDT) Received-SPF: pass (google.com: domain of patch+caf_=patchwork-forward=linaro.org@linaro.org designates 209.85.217.181 as permitted sender) client-ip=209.85.217.181; Received: by lbbck17 with SMTP id ck17so45577963lbb.1 for ; Wed, 14 Oct 2015 05:51:34 -0700 (PDT) X-Received: by 10.112.163.131 with SMTP id yi3mr1515981lbb.36.1444827094370; Wed, 14 Oct 2015 05:51:34 -0700 (PDT) X-Forwarded-To: patchwork-forward@linaro.org X-Forwarded-For: patch@linaro.org patchwork-forward@linaro.org Delivered-To: patch@linaro.org Received: by 10.112.59.35 with SMTP id w3csp2713893lbq; Wed, 14 Oct 2015 05:51:32 -0700 (PDT) X-Received: by 10.66.100.166 with SMTP id ez6mr3646858pab.78.1444827092287; Wed, 14 Oct 2015 05:51:32 -0700 (PDT) Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id ff10si13032960pab.240.2015.10.14.05.51.28; Wed, 14 Oct 2015 05:51:32 -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; Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S932464AbbJNMvZ (ORCPT + 30 others); Wed, 14 Oct 2015 08:51:25 -0400 Received: from szxga03-in.huawei.com ([119.145.14.66]:27780 "EHLO szxga03-in.huawei.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1753542AbbJNMmq (ORCPT ); Wed, 14 Oct 2015 08:42:46 -0400 Received: from 172.24.1.49 (EHLO szxeml425-hub.china.huawei.com) ([172.24.1.49]) by szxrg03-dlp.huawei.com (MOS 4.4.3-GA FastPath queued) with ESMTP id BOX06060; Wed, 14 Oct 2015 20:42:42 +0800 (CST) Received: from linux-4hy3.site (10.107.193.248) by szxeml425-hub.china.huawei.com (10.82.67.180) with Microsoft SMTP Server id 14.3.235.1; Wed, 14 Oct 2015 20:42:33 +0800 From: Wang Nan To: , , CC: , , , , , , , , , , , , Wang Nan , Arnaldo Carvalho de Melo Subject: [PATCH 25/31] bpf tools: Extract and collect map names from BPF object file Date: Wed, 14 Oct 2015 12:41:36 +0000 Message-ID: <1444826502-49291-26-git-send-email-wangnan0@huawei.com> X-Mailer: git-send-email 1.8.3.4 In-Reply-To: <1444826502-49291-1-git-send-email-wangnan0@huawei.com> References: <1444826502-49291-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.0A020203.561E4DC3.0143, 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: 0949f9426c1c90483035c8d5c5c177d8 Sender: linux-kernel-owner@vger.kernel.org Precedence: list List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Removed-Original-Auth: Dkim didn't pass. X-Original-Sender: wangnan0@huawei.com X-Original-Authentication-Results: mx.google.com; spf=pass (google.com: domain of patch+caf_=patchwork-forward=linaro.org@linaro.org designates 209.85.217.181 as permitted sender) smtp.mailfrom=patch+caf_=patchwork-forward=linaro.org@linaro.org Mailing-list: list patchwork-forward@linaro.org; contact patchwork-forward+owners@linaro.org X-Google-Group-Id: 836684582541 List-Post: , List-Help: , List-Archive: List-Unsubscribe: , This patch collect name of map in BPF object files and saves them into 'maps' field in 'struct bpf_object'. 'bpf_object__get_map_by_name' is introduced to retrive map fd and definitions through its name. Signed-off-by: Wang Nan Signed-off-by: He Kuang Cc: Arnaldo Carvalho de Melo Cc: Alexei Starovoitov Cc: Brendan Gregg Cc: Daniel Borkmann Cc: David Ahern Cc: He Kuang Cc: Jiri Olsa Cc: Kaixu Xia Cc: Masami Hiramatsu Cc: Namhyung Kim Cc: Paul Mackerras Cc: Peter Zijlstra Cc: Zefan Li Cc: pi3orama@163.com Link: http://lkml.kernel.org/n/ebpf-ccbekxapr4xy91a3z57xctj0@git.kernel.org --- tools/lib/bpf/libbpf.c | 63 ++++++++++++++++++++++++++++++++++++++++++++++++-- tools/lib/bpf/libbpf.h | 3 +++ 2 files changed, 64 insertions(+), 2 deletions(-) diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c index 8ae501b..c4283c7 100644 --- a/tools/lib/bpf/libbpf.c +++ b/tools/lib/bpf/libbpf.c @@ -111,6 +111,7 @@ struct bpf_program { struct bpf_map { int fd; + char *name; struct bpf_map_def def; void *priv; bpf_map_clear_priv_t clear_priv; @@ -471,12 +472,46 @@ bpf_object__init_maps(struct bpf_object *obj, void *data, return 0; } +static void +bpf_object__init_maps_name(struct bpf_object *obj, int maps_shndx) +{ + int i; + Elf_Data *symbols = obj->efile.symbols; + + if (!symbols || maps_shndx < 0) + return; + + for (i = 0; i < symbols->d_size / sizeof(GElf_Sym); i++) { + GElf_Sym sym; + size_t map_idx; + const char *map_name; + + if (!gelf_getsym(symbols, i, &sym)) + continue; + if (sym.st_shndx != maps_shndx) + continue; + + map_name = elf_strptr(obj->efile.elf, + obj->efile.ehdr.e_shstrndx, + sym.st_name); + map_idx = sym.st_value / sizeof(struct bpf_map_def); + if (map_idx >= obj->nr_maps) { + pr_warning("index of map \"%s\" is buggy: %zu > %zu\n", + map_name, map_idx, obj->nr_maps); + continue; + } + obj->maps[map_idx].name = strdup(map_name); + pr_debug("map %zu is \"%s\"\n", map_idx, + obj->maps[map_idx].name); + } +} + static int bpf_object__elf_collect(struct bpf_object *obj) { Elf *elf = obj->efile.elf; GElf_Ehdr *ep = &obj->efile.ehdr; Elf_Scn *scn = NULL; - int idx = 0, err = 0; + int idx = 0, err = 0, maps_shndx = -1; /* Elf is corrupted/truncated, avoid calling elf_strptr. */ if (!elf_rawdata(elf_getscn(elf, ep->e_shstrndx), NULL)) { @@ -526,9 +561,11 @@ static int bpf_object__elf_collect(struct bpf_object *obj) err = bpf_object__init_kversion(obj, data->d_buf, data->d_size); - else if (strcmp(name, "maps") == 0) + else if (strcmp(name, "maps") == 0) { err = bpf_object__init_maps(obj, data->d_buf, data->d_size); + maps_shndx = idx; + } else if (sh.sh_type == SHT_SYMTAB) { if (obj->efile.symbols) { pr_warning("bpf: multiple SYMTAB in %s\n", @@ -569,6 +606,9 @@ static int bpf_object__elf_collect(struct bpf_object *obj) if (err) goto out; } + + if (maps_shndx >= 0) + bpf_object__init_maps_name(obj, maps_shndx); out: return err; } @@ -1194,6 +1234,13 @@ int bpf_map__get_def(struct bpf_map *map, struct bpf_map_def *pdef) return 0; } +const char *bpf_map__get_name(struct bpf_map *map) +{ + if (!map) + return NULL; + return map->name; +} + int bpf_map__set_private(struct bpf_map *map, void *priv, bpf_map_clear_priv_t clear_priv) { @@ -1245,3 +1292,15 @@ bpf_map__next(struct bpf_map *prev, struct bpf_object *obj) return NULL; return &obj->maps[idx]; } + +struct bpf_map * +bpf_object__get_map_by_name(struct bpf_object *obj, const char *name) +{ + struct bpf_map *pos; + + bpf_map__for_each(pos, obj) { + if (strcmp(pos->name, name) == 0) + return pos; + } + return NULL; +} diff --git a/tools/lib/bpf/libbpf.h b/tools/lib/bpf/libbpf.h index a3bf71e..37b8f27 100644 --- a/tools/lib/bpf/libbpf.h +++ b/tools/lib/bpf/libbpf.h @@ -107,6 +107,8 @@ struct bpf_map_def { * it is not a uapi header so no need to consider name confliction. */ struct bpf_map; +struct bpf_map * +bpf_object__get_map_by_name(struct bpf_object *obj, const char *name); struct bpf_map * bpf_map__next(struct bpf_map *map, struct bpf_object *obj); @@ -117,6 +119,7 @@ bpf_map__next(struct bpf_map *map, struct bpf_object *obj); int bpf_map__get_fd(struct bpf_map *map); int bpf_map__get_def(struct bpf_map *map, struct bpf_map_def *pdef); +const char *bpf_map__get_name(struct bpf_map *map); typedef void (*bpf_map_clear_priv_t)(struct bpf_map *, void *); int bpf_map__set_private(struct bpf_map *map, void *priv,