From patchwork Fri Nov 9 16:21:43 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stanislav Fomichev X-Patchwork-Id: 10676263 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 2E259175A for ; Fri, 9 Nov 2018 16:22:05 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 1A1352EEB9 for ; Fri, 9 Nov 2018 16:22:05 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 0D1932EE96; Fri, 9 Nov 2018 16:22:05 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-15.5 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,MAILING_LIST_MULTI,RCVD_IN_DNSWL_HI, USER_IN_DEF_DKIM_WL autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id A1C382EEAD for ; Fri, 9 Nov 2018 16:22:04 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728393AbeKJCDS (ORCPT ); Fri, 9 Nov 2018 21:03:18 -0500 Received: from mail-pf1-f202.google.com ([209.85.210.202]:42516 "EHLO mail-pf1-f202.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728507AbeKJCDP (ORCPT ); Fri, 9 Nov 2018 21:03:15 -0500 Received: by mail-pf1-f202.google.com with SMTP id g63-v6so1831839pfc.9 for ; Fri, 09 Nov 2018 08:22:00 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=date:in-reply-to:message-id:mime-version:references:subject:from:to :cc; bh=gddtQnFXho8NV4kgxCBew2hJsxrC9rWfQZi/NxObeqs=; b=XUUOTFGV0B7UHDJbsdWisfnBlirbRTvlRNMx0WqVf+qA+er2i/zbSecvHE/1wV8gVv spWlWtKCgJkN7RHhtPoQvgGC7Ww+vzkb6bWfmOT0YoAdtOss2lxb+Qo+/XAzaiLt7G4e YQK1y0s1QryMxX39JZXHstNYhigyMCqN/mLLx+Ml8L+xgtTKGVLKDcqx0nQ4UGgR2BfY zFjd0n0yVTrmqVJJTvIz0fZ66cCO6dJ+NBP8nSEQx57oApPoL/oqK0/tSjbRplo7Nz8w W9i6aPW88fUYovjHMMM9emrecH1lxcVeJQwdrR6h4E5+SPcEd105priCyTO5WxMEvhaF bAVA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=gddtQnFXho8NV4kgxCBew2hJsxrC9rWfQZi/NxObeqs=; b=dOPmNU0v5Z2+v7fW8IxCpeqkeTCGB9VRzfhPilQL4vsEgjn2aUFj3FCD9eVrdv+Waq fb3IHNNLrCZtVeqLJotjY1UB6a2IfP8YUZPwZPY8sIyj4SLKs/TegtvPB+4Yy4X7D5ZC EjYbSIZfbpy1X2x25yDy1ESGSLt7LHdP/GaoZ4GYTFMN5W3VcEXzPoZOSYuN/Ddebt6/ psyooCbD5p+wGxx3n43dmPQzUvaz4fywEpdLAdU6X7XDcUCf9G7ekSovTEcVX1C9+pmn MaUYzFjOXiIsyPeQk8MpI5MIShCOe5qVSa/BJPb7YIoG79Pig6XTn+4Vk+tsTBu7njVI K+mQ== X-Gm-Message-State: AGRZ1gIbUt23WVZUDwy9tG3TaLFmAJWnRNrFMjkcLy5mr8xbQcEwc8aS 7BJVhyBDSvQIpzt286fqo+5giWI= X-Google-Smtp-Source: AJdET5dEl4m1jqWvAGoHFicrqmF4qvF3PVxNaQQyjPDcjyiL14UcZHNJO5waVjIkL9LB6yDNqoi+AFE= X-Received: by 2002:a62:830b:: with SMTP id h11-v6mr76734pfe.30.1541780519611; Fri, 09 Nov 2018 08:21:59 -0800 (PST) Date: Fri, 9 Nov 2018 08:21:43 -0800 In-Reply-To: <20181109162146.78019-1-sdf@google.com> Message-Id: <20181109162146.78019-5-sdf@google.com> Mime-Version: 1.0 References: <20181109162146.78019-1-sdf@google.com> X-Mailer: git-send-email 2.19.1.930.g4563a0d9d0-goog Subject: [PATCH v5 bpf-next 4/7] libbpf: add internal pin_name From: Stanislav Fomichev To: netdev@vger.kernel.org, linux-kselftest@vger.kernel.org, ast@kernel.org, daniel@iogearbox.net, shuah@kernel.org, jakub.kicinski@netronome.com, quentin.monnet@netronome.com Cc: guro@fb.com, jiong.wang@netronome.com, sdf@google.com, bhole_prashant_q7@lab.ntt.co.jp, john.fastabend@gmail.com, jbenc@redhat.com, treeze.taeung@gmail.com, yhs@fb.com, osk@fb.com, sandipan@linux.vnet.ibm.com Sender: linux-kselftest-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kselftest@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP pin_name is the same as section_name where '/' is replaced by '_'. bpf_object__pin_programs is converted to use pin_name to avoid the situation where section_name would require creating another subdirectory for a pin (as, for example, when calling bpf_object__pin_programs for programs in sections like "cgroup/connect6"). Signed-off-by: Stanislav Fomichev --- tools/lib/bpf/libbpf.c | 29 ++++++++++++++++++++++++++--- 1 file changed, 26 insertions(+), 3 deletions(-) diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c index 97ce9f214002..e827542ffa3a 100644 --- a/tools/lib/bpf/libbpf.c +++ b/tools/lib/bpf/libbpf.c @@ -124,6 +124,10 @@ struct bpf_program { char *name; int prog_ifindex; char *section_name; + /* section_name with / replaced by _; makes recursive pinning + * in bpf_object__pin_programs easier + */ + char *pin_name; struct bpf_insn *insns; size_t insns_cnt, main_prog_cnt; enum bpf_prog_type type; @@ -253,6 +257,7 @@ static void bpf_program__exit(struct bpf_program *prog) bpf_program__unload(prog); zfree(&prog->name); zfree(&prog->section_name); + zfree(&prog->pin_name); zfree(&prog->insns); zfree(&prog->reloc_desc); @@ -261,6 +266,17 @@ static void bpf_program__exit(struct bpf_program *prog) prog->idx = -1; } +static char *__bpf_program__pin_name(struct bpf_program *prog) +{ + char *name, *p; + + name = p = strdup(prog->section_name); + while ((p = strchr(p, '/'))) + *p = '_'; + + return name; +} + static int bpf_program__init(void *data, size_t size, char *section_name, int idx, struct bpf_program *prog) @@ -279,6 +295,13 @@ bpf_program__init(void *data, size_t size, char *section_name, int idx, goto errout; } + prog->pin_name = __bpf_program__pin_name(prog); + if (!prog->pin_name) { + pr_warning("failed to alloc pin name for prog under section(%d) %s\n", + idx, section_name); + goto errout; + } + prog->insns = malloc(size); if (!prog->insns) { pr_warning("failed to alloc insns for prog under section %s\n", @@ -2006,7 +2029,7 @@ int bpf_object__pin_programs(struct bpf_object *obj, const char *path) int len; len = snprintf(buf, PATH_MAX, "%s/%s", path, - prog->section_name); + prog->pin_name); if (len < 0) { err = -EINVAL; goto err_unpin_programs; @@ -2028,7 +2051,7 @@ int bpf_object__pin_programs(struct bpf_object *obj, const char *path) int len; len = snprintf(buf, PATH_MAX, "%s/%s", path, - prog->section_name); + prog->pin_name); if (len < 0) continue; else if (len >= PATH_MAX) @@ -2053,7 +2076,7 @@ int bpf_object__unpin_programs(struct bpf_object *obj, const char *path) int len; len = snprintf(buf, PATH_MAX, "%s/%s", path, - prog->section_name); + prog->pin_name); if (len < 0) return -EINVAL; else if (len >= PATH_MAX)