From patchwork Fri Sep 16 07:36:59 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: wangyufen X-Patchwork-Id: 12978199 X-Patchwork-Delegate: bpf@iogearbox.net Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 1D85CC6FA90 for ; Fri, 16 Sep 2022 07:16:40 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229821AbiIPHQh (ORCPT ); Fri, 16 Sep 2022 03:16:37 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40768 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229808AbiIPHQa (ORCPT ); Fri, 16 Sep 2022 03:16:30 -0400 Received: from szxga08-in.huawei.com (szxga08-in.huawei.com [45.249.212.255]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 89397A4065; Fri, 16 Sep 2022 00:16:27 -0700 (PDT) Received: from canpemm500010.china.huawei.com (unknown [172.30.72.54]) by szxga08-in.huawei.com (SkyGuard) with ESMTP id 4MTQHs2JYnz14QVj; Fri, 16 Sep 2022 15:12:25 +0800 (CST) Received: from localhost.localdomain (10.175.112.70) by canpemm500010.china.huawei.com (7.192.105.118) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2375.31; Fri, 16 Sep 2022 15:16:24 +0800 From: Wang Yufen To: , , , , , , , , , , , , , , , , , , , CC: , , , Subject: [bpf-next v2 1/2] libbpf: Add pathname_concat() helper Date: Fri, 16 Sep 2022 15:36:59 +0800 Message-ID: <1663313820-29918-1-git-send-email-wangyufen@huawei.com> X-Mailer: git-send-email 1.8.3.1 MIME-Version: 1.0 X-Originating-IP: [10.175.112.70] X-ClientProxiedBy: dggems704-chm.china.huawei.com (10.3.19.181) To canpemm500010.china.huawei.com (7.192.105.118) X-CFilter-Loop: Reflected Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org X-Patchwork-Delegate: bpf@iogearbox.net Move snprintf and len check to common helper pathname_concat() to make the code simpler. Signed-off-by: Wang Yufen --- tools/lib/bpf/libbpf.c | 76 +++++++++++++++++++------------------------------- 1 file changed, 29 insertions(+), 47 deletions(-) diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c index 3ad1392..7ab977c 100644 --- a/tools/lib/bpf/libbpf.c +++ b/tools/lib/bpf/libbpf.c @@ -2096,19 +2096,30 @@ static bool get_map_field_int(const char *map_name, const struct btf *btf, return true; } +static int pathname_concat(const char *path, const char *name, char *buf) +{ + int len; + + len = snprintf(buf, PATH_MAX, "%s/%s", path, name); + if (len < 0) + return -EINVAL; + if (len >= PATH_MAX) + return -ENAMETOOLONG; + + return 0; +} + static int build_map_pin_path(struct bpf_map *map, const char *path) { char buf[PATH_MAX]; - int len; + int err; if (!path) path = "/sys/fs/bpf"; - len = snprintf(buf, PATH_MAX, "%s/%s", path, bpf_map__name(map)); - if (len < 0) - return -EINVAL; - else if (len >= PATH_MAX) - return -ENAMETOOLONG; + err = pathname_concat(path, bpf_map__name(map), buf); + if (err) + return err; return bpf_map__set_pin_path(map, buf); } @@ -7961,17 +7972,9 @@ int bpf_object__pin_maps(struct bpf_object *obj, const char *path) continue; if (path) { - int len; - - len = snprintf(buf, PATH_MAX, "%s/%s", path, - bpf_map__name(map)); - if (len < 0) { - err = -EINVAL; - goto err_unpin_maps; - } else if (len >= PATH_MAX) { - err = -ENAMETOOLONG; + err = pathname_concat(path, bpf_map__name(map), buf); + if (err) goto err_unpin_maps; - } sanitize_pin_path(buf); pin_path = buf; } else if (!map->pin_path) { @@ -8009,14 +8012,9 @@ int bpf_object__unpin_maps(struct bpf_object *obj, const char *path) char buf[PATH_MAX]; if (path) { - int len; - - len = snprintf(buf, PATH_MAX, "%s/%s", path, - bpf_map__name(map)); - if (len < 0) - return libbpf_err(-EINVAL); - else if (len >= PATH_MAX) - return libbpf_err(-ENAMETOOLONG); + err = pathname_concat(path, bpf_map__name(map), buf); + if (err) + return err; sanitize_pin_path(buf); pin_path = buf; } else if (!map->pin_path) { @@ -8034,6 +8032,7 @@ int bpf_object__unpin_maps(struct bpf_object *obj, const char *path) int bpf_object__pin_programs(struct bpf_object *obj, const char *path) { struct bpf_program *prog; + char buf[PATH_MAX]; int err; if (!obj) @@ -8045,17 +8044,9 @@ int bpf_object__pin_programs(struct bpf_object *obj, const char *path) } bpf_object__for_each_program(prog, obj) { - char buf[PATH_MAX]; - int len; - - len = snprintf(buf, PATH_MAX, "%s/%s", path, prog->name); - if (len < 0) { - err = -EINVAL; - goto err_unpin_programs; - } else if (len >= PATH_MAX) { - err = -ENAMETOOLONG; + err = pathname_concat(path, prog->name, buf); + if (err) goto err_unpin_programs; - } err = bpf_program__pin(prog, buf); if (err) @@ -8066,13 +8057,7 @@ int bpf_object__pin_programs(struct bpf_object *obj, const char *path) err_unpin_programs: while ((prog = bpf_object__prev_program(obj, prog))) { - char buf[PATH_MAX]; - int len; - - len = snprintf(buf, PATH_MAX, "%s/%s", path, prog->name); - if (len < 0) - continue; - else if (len >= PATH_MAX) + if (pathname_concat(path, prog->name, buf)) continue; bpf_program__unpin(prog, buf); @@ -8091,13 +8076,10 @@ int bpf_object__unpin_programs(struct bpf_object *obj, const char *path) bpf_object__for_each_program(prog, obj) { char buf[PATH_MAX]; - int len; - len = snprintf(buf, PATH_MAX, "%s/%s", path, prog->name); - if (len < 0) - return libbpf_err(-EINVAL); - else if (len >= PATH_MAX) - return libbpf_err(-ENAMETOOLONG); + err = pathname_concat(path, prog->name, buf); + if (err) + return libbpf_err(err); err = bpf_program__unpin(prog, buf); if (err) From patchwork Fri Sep 16 07:37:00 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: wangyufen X-Patchwork-Id: 12978200 X-Patchwork-Delegate: bpf@iogearbox.net Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id C90B2C6FA82 for ; Fri, 16 Sep 2022 07:16:41 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229932AbiIPHQj (ORCPT ); Fri, 16 Sep 2022 03:16:39 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40774 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229812AbiIPHQa (ORCPT ); Fri, 16 Sep 2022 03:16:30 -0400 Received: from szxga03-in.huawei.com (szxga03-in.huawei.com [45.249.212.189]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 77BA6A50C2; Fri, 16 Sep 2022 00:16:28 -0700 (PDT) Received: from canpemm500010.china.huawei.com (unknown [172.30.72.55]) by szxga03-in.huawei.com (SkyGuard) with ESMTP id 4MTQL61vzSzBsPF; Fri, 16 Sep 2022 15:14:22 +0800 (CST) Received: from localhost.localdomain (10.175.112.70) by canpemm500010.china.huawei.com (7.192.105.118) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2375.31; Fri, 16 Sep 2022 15:16:25 +0800 From: Wang Yufen To: , , , , , , , , , , , , , , , , , , , CC: , , , Subject: [bpf-next v2 2/2] selftests/bpf: Add testcases for pinning to errpath Date: Fri, 16 Sep 2022 15:37:00 +0800 Message-ID: <1663313820-29918-2-git-send-email-wangyufen@huawei.com> X-Mailer: git-send-email 1.8.3.1 In-Reply-To: <1663313820-29918-1-git-send-email-wangyufen@huawei.com> References: <1663313820-29918-1-git-send-email-wangyufen@huawei.com> MIME-Version: 1.0 X-Originating-IP: [10.175.112.70] X-ClientProxiedBy: dggems704-chm.china.huawei.com (10.3.19.181) To canpemm500010.china.huawei.com (7.192.105.118) X-CFilter-Loop: Reflected Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org X-Patchwork-Delegate: bpf@iogearbox.net Add testcases for map and prog pin to errpath. Signed-off-by: Wang Yufen --- tools/testing/selftests/bpf/prog_tests/pinning.c | 67 ++++++++++++++++++++++ .../selftests/bpf/progs/test_pinning_path.c | 19 ++++++ 2 files changed, 86 insertions(+) create mode 100644 tools/testing/selftests/bpf/progs/test_pinning_path.c diff --git a/tools/testing/selftests/bpf/prog_tests/pinning.c b/tools/testing/selftests/bpf/prog_tests/pinning.c index d95cee5..ab7780f 100644 --- a/tools/testing/selftests/bpf/prog_tests/pinning.c +++ b/tools/testing/selftests/bpf/prog_tests/pinning.c @@ -24,6 +24,61 @@ __u32 get_map_id(struct bpf_object *obj, const char *name) return map_info.id; } +static void test_pin_path(void) +{ + const char *progfile = "./test_pinning_path.bpf.o"; + const char *progpinpath = "/sys/fs/bpf/test_pinpath"; + char errpath[PATH_MAX + 1]; + char command[64]; + int prog_fd, err; + struct bpf_object *obj; + __u32 duration = 0; + + /* Use libbpf 1.0 API mode */ + libbpf_set_strict_mode(LIBBPF_STRICT_ALL); + + err = bpf_prog_test_load(progfile, BPF_PROG_TYPE_SOCK_OPS, &obj, + &prog_fd); + CHECK(err, "bpf_prog_test_load", "err %d errno %d\n", err, errno); + + memset(&errpath, 't', PATH_MAX); + err = bpf_object__pin_maps(obj, errpath); + if (CHECK(err != -ENAMETOOLONG, "pin maps errpath", "err %d errno %d\n", err, errno)) + goto out; + + err = bpf_object__pin_maps(obj, progpinpath); + if (CHECK(err, "pin maps", "err %d errno %d\n", err, errno)) + goto out; + + err = bpf_object__pin_programs(obj, errpath); + if (CHECK(err != -ENAMETOOLONG, "pin progs errpath", "err %d errno %d\n", err, errno)) + goto out; + + err = bpf_object__pin_programs(obj, progpinpath); + if (CHECK(err, "pin prog", "err %d errno %d\n", err, errno)) + goto out; + + err = bpf_object__unpin_programs(obj, errpath); + if (CHECK(err != -ENAMETOOLONG, "pin progs errpath", "err %d errno %d\n", err, errno)) + goto out; + + err = bpf_object__unpin_programs(obj, progpinpath); + if (CHECK(err, "pin prog", "err %d errno %d\n", err, errno)) + goto out; + + err = bpf_object__unpin_maps(obj, errpath); + if (CHECK(err != -ENAMETOOLONG, "pin maps errpath", "err %d errno %d\n", err, errno)) + goto out; + + err = bpf_object__unpin_maps(obj, progpinpath); + if (CHECK(err, "pin maps", "err %d errno %d\n", err, errno)) + goto out; +out: + bpf_object__close(obj); + sprintf(command, "rm -r %s", progpinpath); + system(command); +} + void test_pinning(void) { const char *file_invalid = "./test_pinning_invalid.bpf.o"; @@ -32,6 +87,7 @@ void test_pinning(void) const char *nopinpath2 = "/sys/fs/bpf/nopinmap2"; const char *custpath = "/sys/fs/bpf/custom"; const char *pinpath = "/sys/fs/bpf/pinmap"; + char errpath[PATH_MAX + 1]; const char *file = "./test_pinning.bpf.o"; __u32 map_id, map_id2, duration = 0; struct stat statbuf = {}; @@ -206,7 +262,17 @@ void test_pinning(void) bpf_object__close(obj); + /* test auto-pinning at err path with open opt */ + memset(&errpath, 't', PATH_MAX); + opts.pin_root_path = errpath; + obj = bpf_object__open_file(file, &opts); + if (CHECK_FAIL(libbpf_get_error(obj) != -ENAMETOOLONG)) { + obj = NULL; + goto out; + } + /* test auto-pinning at custom path with open opt */ + opts.pin_root_path = custpath; obj = bpf_object__open_file(file, &opts); if (CHECK_FAIL(libbpf_get_error(obj))) { obj = NULL; @@ -277,4 +343,5 @@ void test_pinning(void) rmdir(custpath); if (obj) bpf_object__close(obj); + test_pin_path(); } diff --git a/tools/testing/selftests/bpf/progs/test_pinning_path.c b/tools/testing/selftests/bpf/progs/test_pinning_path.c new file mode 100644 index 0000000..b4e2099 --- /dev/null +++ b/tools/testing/selftests/bpf/progs/test_pinning_path.c @@ -0,0 +1,19 @@ +// SPDX-License-Identifier: GPL-2.0 + +#include "vmlinux.h" +#include + +struct { + __uint(type, BPF_MAP_TYPE_SOCKHASH); + __uint(max_entries, 64); + __type(key, __u32); + __type(value, __u64); +} sock_ops_map SEC(".maps"); + +SEC("sockops") +int bpf_sockmap(struct bpf_sock_ops *skops) +{ + return 0; +} + +char _license[] SEC("license") = "GPL";