From patchwork Thu Nov 19 15:06:12 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Daniel T. Lee" X-Patchwork-Id: 328975 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-15.7 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI, SPF_HELO_NONE, SPF_PASS, URIBL_BLOCKED, USER_AGENT_GIT autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id CE217C63798 for ; Thu, 19 Nov 2020 15:06:41 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 6D0E722264 for ; Thu, 19 Nov 2020 15:06:41 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="YAin+UCD" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728303AbgKSPGl (ORCPT ); Thu, 19 Nov 2020 10:06:41 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41382 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728275AbgKSPGk (ORCPT ); Thu, 19 Nov 2020 10:06:40 -0500 Received: from mail-pg1-x533.google.com (mail-pg1-x533.google.com [IPv6:2607:f8b0:4864:20::533]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 4F823C0613CF; Thu, 19 Nov 2020 07:06:40 -0800 (PST) Received: by mail-pg1-x533.google.com with SMTP id m9so4455823pgb.4; Thu, 19 Nov 2020 07:06:40 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=tcGrIg4lu2Vq+X0LFqj+xsS0QCmfTMGdXi+7qZxwkTA=; b=YAin+UCD+AS3sgdMl35UFWDqQUIAL2QhCxLgLqHFBzJnVIG4IMkf8+VJwaVKCqUpmJ AYOfrYqume/fcqh105YNk8n4vt5fp+/RtrCi2mmRL4ioCrZE7d4iIGEee+jUv58kU7kP q3MNdOLTyPmn+c1MbnfvJ3M6kZK8kkmUJBtNTc3xXhjC3bhiQk8u3jDQ9vj0oD/6Fxxg uwZB+3mO9s/2ktz3NvHhWsYb3cfVe7YF3Pf/har/hWp7N8FAyLYe/0u5FOOG4TEShzr1 Fa1PPex9eup54KY2nxX/GmmHsJMhQNWo19USzkyO76XbFxKonh7BYYeBsv3J8EzaEYcV NNYA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=tcGrIg4lu2Vq+X0LFqj+xsS0QCmfTMGdXi+7qZxwkTA=; b=p2XCt3AlZ1z6NW8JMyx4U3Z8ads34CLTVnjPWIBDP+uq99rr9cRcPcBwbeu8bGKUp7 gmxWwc0MI8ACYbob16d/AyZN+dA5dXlN1X/FztrvTDuH+WYEI3rc6/qacSUQkKUgIUXy zH+kk9YO1knOlc0LTHKAof/RgAwEqDMN+IDJOlKTNbXe9TdhUFJYxPFmKa65sMnUSVFW iu8RqunTu2A0zIH4i0t7cviULou8emAq8sy8Q5cs7RLm5UVdKwiq3J7CPYqDl+DWfOKb g9E+ddXSYDIXN0euq7wQw/g3jmVnwOMEIRoVhtZOHPEge29z6aMD1RoAcS8GWghFjEFH rNJw== X-Gm-Message-State: AOAM5327pD58s/rYXNoiA60KFpflxKsMTwXsD8sjWe+SwDCY/wa8791C gabFVrPAfA49WsXr7O9f6Q== X-Google-Smtp-Source: ABdhPJyxKQXE9E1huE4dRCHOkbAFHfZqHcF2Rx1bNbF1HEHGc9cY+t895+IBsAhLxdu/dsIiHQV31A== X-Received: by 2002:a17:90a:7409:: with SMTP id a9mr5072799pjg.48.1605798399791; Thu, 19 Nov 2020 07:06:39 -0800 (PST) Received: from localhost.localdomain ([182.209.58.45]) by smtp.gmail.com with ESMTPSA id b80sm77783pfb.40.2020.11.19.07.06.35 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 19 Nov 2020 07:06:39 -0800 (PST) From: "Daniel T. Lee" To: Daniel Borkmann , Alexei Starovoitov , Andrii Nakryiko , brakmo , Jesper Dangaard Brouer , Andrii Nakryiko , Lorenzo Bianconi , David Ahern , Yonghong Song , =?utf-8?b?VG9rZSBIw7hpbGFuZC1Kw7hy?= =?utf-8?q?gensen?= , Ira Weiny , Thomas Graf , Jakub Kicinski , Martin KaFai Lau , John Fastabend Cc: bpf@vger.kernel.org, netdev@vger.kernel.org, Xdp Subject: [PATCH bpf-next v2 2/7] samples: bpf: refactor test_cgrp2_sock2 program with libbpf Date: Thu, 19 Nov 2020 15:06:12 +0000 Message-Id: <20201119150617.92010-3-danieltimlee@gmail.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20201119150617.92010-1-danieltimlee@gmail.com> References: <20201119150617.92010-1-danieltimlee@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org This commit refactors the existing cgroup program with libbpf bpf loader. The original test_cgrp2_sock2 has keeped the bpf program attached to the cgroup hierarchy even after the exit of user program. To implement the same functionality with libbpf, this commit uses the BPF_LINK_PINNING to pin the link attachment even after it is closed. Since this uses LINK instead of ATTACH, detach of bpf program from cgroup with 'test_cgrp2_sock' is not used anymore. The code to mount the bpf was added to the .sh file in case the bpff was not mounted on /sys/fs/bpf. Additionally, to fix the problem that shell script cannot find the binary object from the current path, relative path './' has been added in front of binary. Fixes: 554ae6e792ef3 ("samples/bpf: add userspace example for prohibiting sockets") Signed-off-by: Daniel T. Lee --- Changes in v2: - change to destroy link even after link__pin() - enhance error message samples/bpf/Makefile | 2 +- samples/bpf/test_cgrp2_sock2.c | 61 ++++++++++++++++++++++++--------- samples/bpf/test_cgrp2_sock2.sh | 21 +++++++++--- 3 files changed, 62 insertions(+), 22 deletions(-) diff --git a/samples/bpf/Makefile b/samples/bpf/Makefile index 7c61118525f7..d31e082c369e 100644 --- a/samples/bpf/Makefile +++ b/samples/bpf/Makefile @@ -82,7 +82,7 @@ test_overhead-objs := bpf_load.o test_overhead_user.o test_cgrp2_array_pin-objs := test_cgrp2_array_pin.o test_cgrp2_attach-objs := test_cgrp2_attach.o test_cgrp2_sock-objs := test_cgrp2_sock.o -test_cgrp2_sock2-objs := bpf_load.o test_cgrp2_sock2.o +test_cgrp2_sock2-objs := test_cgrp2_sock2.o xdp1-objs := xdp1_user.o # reuse xdp1 source intentionally xdp2-objs := xdp1_user.o diff --git a/samples/bpf/test_cgrp2_sock2.c b/samples/bpf/test_cgrp2_sock2.c index a9277b118c33..e7060aaa2f5a 100644 --- a/samples/bpf/test_cgrp2_sock2.c +++ b/samples/bpf/test_cgrp2_sock2.c @@ -20,9 +20,9 @@ #include #include #include +#include #include "bpf_insn.h" -#include "bpf_load.h" static int usage(const char *argv0) { @@ -32,37 +32,64 @@ static int usage(const char *argv0) int main(int argc, char **argv) { - int cg_fd, ret, filter_id = 0; + int cg_fd, err, ret = EXIT_FAILURE, filter_id = 0, prog_cnt = 0; + const char *link_pin_path = "/sys/fs/bpf/test_cgrp2_sock2"; + struct bpf_link *link = NULL; + struct bpf_program *progs[2]; + struct bpf_program *prog; + struct bpf_object *obj; if (argc < 3) return usage(argv[0]); + if (argc > 3) + filter_id = atoi(argv[3]); + cg_fd = open(argv[1], O_DIRECTORY | O_RDONLY); if (cg_fd < 0) { printf("Failed to open cgroup path: '%s'\n", strerror(errno)); - return EXIT_FAILURE; + return ret; } - if (load_bpf_file(argv[2])) - return EXIT_FAILURE; - - printf("Output from kernel verifier:\n%s\n-------\n", bpf_log_buf); + obj = bpf_object__open_file(argv[2], NULL); + if (libbpf_get_error(obj)) { + printf("ERROR: opening BPF object file failed\n"); + return ret; + } - if (argc > 3) - filter_id = atoi(argv[3]); + bpf_object__for_each_program(prog, obj) { + progs[prog_cnt] = prog; + prog_cnt++; + } if (filter_id >= prog_cnt) { printf("Invalid program id; program not found in file\n"); - return EXIT_FAILURE; + goto cleanup; + } + + /* load BPF program */ + if (bpf_object__load(obj)) { + printf("ERROR: loading BPF object file failed\n"); + goto cleanup; } - ret = bpf_prog_attach(prog_fd[filter_id], cg_fd, - BPF_CGROUP_INET_SOCK_CREATE, 0); - if (ret < 0) { - printf("Failed to attach prog to cgroup: '%s'\n", - strerror(errno)); - return EXIT_FAILURE; + link = bpf_program__attach_cgroup(progs[filter_id], cg_fd); + if (libbpf_get_error(link)) { + printf("ERROR: bpf_program__attach failed\n"); + link = NULL; + goto cleanup; } - return EXIT_SUCCESS; + err = bpf_link__pin(link, link_pin_path); + if (err < 0) { + printf("ERROR: bpf_link__pin failed: %d\n", err); + goto cleanup; + } + + ret = EXIT_SUCCESS; + +cleanup: + bpf_link__destroy(link); + bpf_object__close(obj); + return ret; } diff --git a/samples/bpf/test_cgrp2_sock2.sh b/samples/bpf/test_cgrp2_sock2.sh index 0f396a86e0cb..6a3dbe642b2b 100755 --- a/samples/bpf/test_cgrp2_sock2.sh +++ b/samples/bpf/test_cgrp2_sock2.sh @@ -1,6 +1,9 @@ #!/bin/bash # SPDX-License-Identifier: GPL-2.0 +BPFFS=/sys/fs/bpf +LINK_PIN=$BPFFS/test_cgrp2_sock2 + function config_device { ip netns add at_ns0 ip link add veth0 type veth peer name veth0b @@ -21,16 +24,22 @@ function config_cgroup { echo $$ >> /tmp/cgroupv2/foo/cgroup.procs } +function config_bpffs { + if mount | grep $BPFFS > /dev/null; then + echo "bpffs already mounted" + else + echo "bpffs not mounted. Mounting..." + mount -t bpf none $BPFFS + fi +} function attach_bpf { - test_cgrp2_sock2 /tmp/cgroupv2/foo sock_flags_kern.o $1 + ./test_cgrp2_sock2 /tmp/cgroupv2/foo sock_flags_kern.o $1 [ $? -ne 0 ] && exit 1 } function cleanup { - if [ -d /tmp/cgroupv2/foo ]; then - test_cgrp2_sock -d /tmp/cgroupv2/foo - fi + rm -rf $LINK_PIN ip link del veth0b ip netns delete at_ns0 umount /tmp/cgroupv2 @@ -42,6 +51,7 @@ cleanup 2>/dev/null set -e config_device config_cgroup +config_bpffs set +e # @@ -62,6 +72,9 @@ if [ $? -eq 0 ]; then exit 1 fi +rm -rf $LINK_PIN +sleep 1 # Wait for link detach + # # Test 2 - fail ping # From patchwork Thu Nov 19 15:06:14 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Daniel T. Lee" X-Patchwork-Id: 328974 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-15.7 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI, SPF_HELO_NONE, SPF_PASS, USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id E625DC63697 for ; Thu, 19 Nov 2020 15:06:51 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 872BA2468B for ; Thu, 19 Nov 2020 15:06:51 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="LlHuo2N9" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728327AbgKSPGu (ORCPT ); Thu, 19 Nov 2020 10:06:50 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41408 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727086AbgKSPGu (ORCPT ); Thu, 19 Nov 2020 10:06:50 -0500 Received: from mail-pf1-x441.google.com (mail-pf1-x441.google.com [IPv6:2607:f8b0:4864:20::441]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 27F39C0613CF; Thu, 19 Nov 2020 07:06:50 -0800 (PST) Received: by mail-pf1-x441.google.com with SMTP id q5so4757994pfk.6; Thu, 19 Nov 2020 07:06:50 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=HPngOSM82/AQhAOxg6kInXieW/P6ExxWnm99recQX+Y=; b=LlHuo2N9wiFZzvXCk91aNPD3Pdsn3MNTBuKuRW2BpcVckdbzemldh6aCqOf6jkKqy0 j45TIJBJNkrFnR9VYpREwtBiVL7IQjGRHGgaRw3+6xtoBeABXQnHgMeOeigSitXWMbTA dTMLpzktTgAKJAv32T4g6Kql9UWqksuEgNqHTb2O/ZeMtNg7+skLXNIEKaG79xOA/Ed9 6yscXMrWE74+PktoAwLf9ucMHhctmCc85zCyllDJA4BUAMM6t3D1BemI3guDFUto9J3m XKwLTgR0iBEXkLv9oS0CwVwHBIUPmPbn81EvpwJb/4Lb2KTQQ4sqi/aPCztFqXp3eyy6 QlrA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=HPngOSM82/AQhAOxg6kInXieW/P6ExxWnm99recQX+Y=; b=OAIszGrZF1pYO4+fqlvR7mWe3st+awYM/nQlT4/jY7+0HcyL5HbOj5d1hDOEVj4Qwb wRfHDVXXOsELylpB7T2y2vOVrEKdyu0pc8FBPMqaPkNBjZMe6+Q6IDgU5bpTE9uxXxPO kgHBxr/MrBTIB0fi3SrhB9iZ5YOaR6IOH7PArIitZDfzxUy0K0efiEbPqxzLEofVR3yo 9/SROCmr2//83ZPPLDNiTP3lCDyRUaYx6AFywWbpBET0KinmrT1039KEnL639zq74J8f 9gkN1O763aGMc1l99Jd5JWzsArI3MygUgLko4nOctXCs6y9ZP1KkPyXW4tiXmlKac1Sw ynNg== X-Gm-Message-State: AOAM530hvBE1UOAcLAC8Ak76oRR6p79K6ItWjfe0ERLXLkVmpJr74A7I 825Iw/mdWD77XVAM8G19IQ== X-Google-Smtp-Source: ABdhPJxMeb577aQYRkjmLQp/px80a/X6BjuqcTjLJCtTzmCGlfw6/nOgc+IVDiuhU/OjHVu3s/KpDQ== X-Received: by 2002:a63:4558:: with SMTP id u24mr12970647pgk.376.1605798409666; Thu, 19 Nov 2020 07:06:49 -0800 (PST) Received: from localhost.localdomain ([182.209.58.45]) by smtp.gmail.com with ESMTPSA id b80sm77783pfb.40.2020.11.19.07.06.44 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 19 Nov 2020 07:06:49 -0800 (PST) From: "Daniel T. Lee" To: Daniel Borkmann , Alexei Starovoitov , Andrii Nakryiko , brakmo , Jesper Dangaard Brouer , Andrii Nakryiko , Lorenzo Bianconi , David Ahern , Yonghong Song , =?utf-8?b?VG9rZSBIw7hpbGFuZC1Kw7hy?= =?utf-8?q?gensen?= , Ira Weiny , Thomas Graf , Jakub Kicinski , Martin KaFai Lau , John Fastabend Cc: bpf@vger.kernel.org, netdev@vger.kernel.org, Xdp Subject: [PATCH bpf-next v2 4/7] samples: bpf: refactor ibumad program with libbpf Date: Thu, 19 Nov 2020 15:06:14 +0000 Message-Id: <20201119150617.92010-5-danieltimlee@gmail.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20201119150617.92010-1-danieltimlee@gmail.com> References: <20201119150617.92010-1-danieltimlee@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org This commit refactors the existing ibumad program with libbpf bpf loader. Attach/detach of Tracepoint bpf programs has been managed with the generic bpf_program__attach() and bpf_link__destroy() from the libbpf. Also, instead of using the previous BPF MAP definition, this commit refactors ibumad MAP definition with the new BTF-defined MAP format. To verify that this bpf program works without an infiniband device, try loading ib_umad kernel module and test the program as follows: # modprobe ib_umad # ./ibumad Moreover, TRACE_HELPERS has been removed from the Makefile since it is not used on this program. Signed-off-by: Daniel T. Lee --- Changes in v2: - add static at global variable and drop {} - fix return error code on exit samples/bpf/Makefile | 2 +- samples/bpf/ibumad_kern.c | 26 +++++++------- samples/bpf/ibumad_user.c | 71 +++++++++++++++++++++++++++++---------- 3 files changed, 68 insertions(+), 31 deletions(-) diff --git a/samples/bpf/Makefile b/samples/bpf/Makefile index 3bffd42e1482..09a249477554 100644 --- a/samples/bpf/Makefile +++ b/samples/bpf/Makefile @@ -109,7 +109,7 @@ xsk_fwd-objs := xsk_fwd.o xdp_fwd-objs := xdp_fwd_user.o task_fd_query-objs := task_fd_query_user.o $(TRACE_HELPERS) xdp_sample_pkts-objs := xdp_sample_pkts_user.o $(TRACE_HELPERS) -ibumad-objs := bpf_load.o ibumad_user.o $(TRACE_HELPERS) +ibumad-objs := ibumad_user.o hbm-objs := hbm.o $(CGROUP_HELPERS) # Tell kbuild to always build the programs diff --git a/samples/bpf/ibumad_kern.c b/samples/bpf/ibumad_kern.c index 3a91b4c1989a..26dcd4dde946 100644 --- a/samples/bpf/ibumad_kern.c +++ b/samples/bpf/ibumad_kern.c @@ -16,19 +16,19 @@ #include -struct bpf_map_def SEC("maps") read_count = { - .type = BPF_MAP_TYPE_ARRAY, - .key_size = sizeof(u32), /* class; u32 required */ - .value_size = sizeof(u64), /* count of mads read */ - .max_entries = 256, /* Room for all Classes */ -}; - -struct bpf_map_def SEC("maps") write_count = { - .type = BPF_MAP_TYPE_ARRAY, - .key_size = sizeof(u32), /* class; u32 required */ - .value_size = sizeof(u64), /* count of mads written */ - .max_entries = 256, /* Room for all Classes */ -}; +struct { + __uint(type, BPF_MAP_TYPE_ARRAY); + __type(key, u32); /* class; u32 required */ + __type(value, u64); /* count of mads read */ + __uint(max_entries, 256); /* Room for all Classes */ +} read_count SEC(".maps"); + +struct { + __uint(type, BPF_MAP_TYPE_ARRAY); + __type(key, u32); /* class; u32 required */ + __type(value, u64); /* count of mads written */ + __uint(max_entries, 256); /* Room for all Classes */ +} write_count SEC(".maps"); #undef DEBUG #ifndef DEBUG diff --git a/samples/bpf/ibumad_user.c b/samples/bpf/ibumad_user.c index fa06eef31a84..d83d8102f489 100644 --- a/samples/bpf/ibumad_user.c +++ b/samples/bpf/ibumad_user.c @@ -23,10 +23,15 @@ #include #include -#include "bpf_load.h" +#include #include "bpf_util.h" #include +static struct bpf_link *tp_links[3]; +static struct bpf_object *obj; +static int map_fd[2]; +static int tp_cnt; + static void dump_counts(int fd) { __u32 key; @@ -53,6 +58,11 @@ static void dump_all_counts(void) static void dump_exit(int sig) { dump_all_counts(); + /* Detach tracepoints */ + while (tp_cnt) + bpf_link__destroy(tp_links[--tp_cnt]); + + bpf_object__close(obj); exit(0); } @@ -73,19 +83,11 @@ static void usage(char *cmd) int main(int argc, char **argv) { + struct bpf_program *prog; unsigned long delay = 5; + char filename[256]; int longindex = 0; - int opt; - char bpf_file[256]; - - /* Create the eBPF kernel code path name. - * This follows the pattern of all of the other bpf samples - */ - snprintf(bpf_file, sizeof(bpf_file), "%s_kern.o", argv[0]); - - /* Do one final dump when exiting */ - signal(SIGINT, dump_exit); - signal(SIGTERM, dump_exit); + int opt, err = -1; while ((opt = getopt_long(argc, argv, "hd:rSw", long_options, &longindex)) != -1) { @@ -107,16 +109,51 @@ int main(int argc, char **argv) } } - if (load_bpf_file(bpf_file)) { - fprintf(stderr, "ERROR: failed to load eBPF from file : %s\n", - bpf_file); - return 1; + /* Do one final dump when exiting */ + signal(SIGINT, dump_exit); + signal(SIGTERM, dump_exit); + + snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]); + obj = bpf_object__open_file(filename, NULL); + if (libbpf_get_error(obj)) { + fprintf(stderr, "ERROR: opening BPF object file failed\n"); + return err; + } + + /* load BPF program */ + if (bpf_object__load(obj)) { + fprintf(stderr, "ERROR: loading BPF object file failed\n"); + goto cleanup; + } + + map_fd[0] = bpf_object__find_map_fd_by_name(obj, "read_count"); + map_fd[1] = bpf_object__find_map_fd_by_name(obj, "write_count"); + if (map_fd[0] < 0 || map_fd[1] < 0) { + fprintf(stderr, "ERROR: finding a map in obj file failed\n"); + goto cleanup; + } + + bpf_object__for_each_program(prog, obj) { + tp_links[tp_cnt] = bpf_program__attach(prog); + if (libbpf_get_error(tp_links[tp_cnt])) { + fprintf(stderr, "ERROR: bpf_program__attach failed\n"); + tp_links[tp_cnt] = NULL; + goto cleanup; + } + tp_cnt++; } while (1) { sleep(delay); dump_all_counts(); } + err = 0; + +cleanup: + /* Detach tracepoints */ + while (tp_cnt) + bpf_link__destroy(tp_links[--tp_cnt]); - return 0; + bpf_object__close(obj); + return err; } From patchwork Thu Nov 19 15:06:16 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Daniel T. Lee" X-Patchwork-Id: 328973 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-15.7 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI, SPF_HELO_NONE, SPF_PASS, URIBL_BLOCKED, USER_AGENT_GIT autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id BD66DC63697 for ; Thu, 19 Nov 2020 15:07:01 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 6E6EF246A7 for ; Thu, 19 Nov 2020 15:07:01 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="TtA55Cby" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728339AbgKSPHA (ORCPT ); Thu, 19 Nov 2020 10:07:00 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41436 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726820AbgKSPG7 (ORCPT ); Thu, 19 Nov 2020 10:06:59 -0500 Received: from mail-pl1-x643.google.com (mail-pl1-x643.google.com [IPv6:2607:f8b0:4864:20::643]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id AD27BC0613CF; Thu, 19 Nov 2020 07:06:59 -0800 (PST) Received: by mail-pl1-x643.google.com with SMTP id 5so3082879plj.8; Thu, 19 Nov 2020 07:06:59 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=RS/qSpAFGukp3/v16zb33bFhkDVWoKnFpkOAjHD2Ues=; b=TtA55CbywFGEZgmG2AIEnrf1G1aCZ62XjuGX9tKDdFyZx8TDeAMi9hvpdP1KFnkdqB IhdJXXjEpEJyCXC8/+hN6sKY3KNuvEsidW0kiGas0eh91wPMSa94DfOEeq8YxHXRWOpZ DAaOritJmqPXKTLfDapId7Iae/QL6gQRpsto7levO+WJ0njdBZuvwzHBXbMv4bEq32tV s70BgRO4tLQYq+lx0q2xhXJ0DW34LGY/Qa0mWMU6fTEfLUjRzDOmQvuQ1KGEMGlhs2cK CnEBiFdslPWczmJgLVEEnAIzaSssfA0gN+R/FdedihC0Lo0TIpaOzxj/pAVQkh2wURvz 2bgA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=RS/qSpAFGukp3/v16zb33bFhkDVWoKnFpkOAjHD2Ues=; b=iff1xE38hHiO05ehOsWuY6NmorJNndLBXkZ0ndOWCnGgnWR8MM15LWOEI9d2dMUcfL w+nmTeL1lbT2jf6N25po9chTUDADAO0TypRRyL7zyBi9x836EH6FLNsUb+eq57JNO+sh KpQPG3ezdvaamE+tO2nBq34ob22BlUfTy3mzJN6nbT6/0KUVqfOfZtAAz9QrOIMITi8V TlKSRNrJq0kRFZglJH2epV1AjaawPcpHAlCjEjoNBy7PA6SyR0pXfms9+W3OcnvO7cec 2pFBRWjOB1F3kqtes8O1lpJUcGJg8xze9Pxcb1858CuYZvdZcmK3Kj/3V7CvBHROKtaY S63A== X-Gm-Message-State: AOAM532poH/mB+Dyk830/XgnZjHm/L2KWJiGUC1dETnxdNC45Pi9StLV TmjDBbqMZOVJrVXwCMdhGw== X-Google-Smtp-Source: ABdhPJzZki7x+0zz4MBNmiQBfAG6eLca/O+2oHgu9w9coQHVQE8Th4CDsKriLmD0aczGF4BJfifa3g== X-Received: by 2002:a17:902:9f87:b029:d9:e311:fc86 with SMTP id g7-20020a1709029f87b02900d9e311fc86mr1082667plq.74.1605798419245; Thu, 19 Nov 2020 07:06:59 -0800 (PST) Received: from localhost.localdomain ([182.209.58.45]) by smtp.gmail.com with ESMTPSA id b80sm77783pfb.40.2020.11.19.07.06.54 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 19 Nov 2020 07:06:58 -0800 (PST) From: "Daniel T. Lee" To: Daniel Borkmann , Alexei Starovoitov , Andrii Nakryiko , brakmo , Jesper Dangaard Brouer , Andrii Nakryiko , Lorenzo Bianconi , David Ahern , Yonghong Song , =?utf-8?b?VG9rZSBIw7hpbGFuZC1Kw7hy?= =?utf-8?q?gensen?= , Ira Weiny , Thomas Graf , Jakub Kicinski , Martin KaFai Lau , John Fastabend Cc: bpf@vger.kernel.org, netdev@vger.kernel.org, Xdp Subject: [PATCH bpf-next v2 6/7] samples: bpf: fix lwt_len_hist reusing previous BPF map Date: Thu, 19 Nov 2020 15:06:16 +0000 Message-Id: <20201119150617.92010-7-danieltimlee@gmail.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20201119150617.92010-1-danieltimlee@gmail.com> References: <20201119150617.92010-1-danieltimlee@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org Currently, lwt_len_hist's map lwt_len_hist_map is uses pinning, and the map isn't cleared on test end. This leds to reuse of that map for each test, which prevents the results of the test from being accurate. This commit fixes the problem by removing of pinned map from bpffs. Also, this commit add the executable permission to shell script files. Fixes: f74599f7c5309 ("bpf: Add tests and samples for LWT-BPF") Signed-off-by: Daniel T. Lee --- samples/bpf/lwt_len_hist.sh | 2 ++ samples/bpf/test_lwt_bpf.sh | 0 2 files changed, 2 insertions(+) mode change 100644 => 100755 samples/bpf/lwt_len_hist.sh mode change 100644 => 100755 samples/bpf/test_lwt_bpf.sh diff --git a/samples/bpf/test_lwt_bpf.sh b/samples/bpf/test_lwt_bpf.sh old mode 100644 new mode 100755 diff --git a/samples/bpf/lwt_len_hist.sh b/samples/bpf/lwt_len_hist.sh old mode 100644 new mode 100755 index 090b96eaf7f7..0eda9754f50b --- a/samples/bpf/lwt_len_hist.sh +++ b/samples/bpf/lwt_len_hist.sh @@ -8,6 +8,8 @@ VETH1=tst_lwt1b TRACE_ROOT=/sys/kernel/debug/tracing function cleanup { + # To reset saved histogram, remove pinned map + rm /sys/fs/bpf/tc/globals/lwt_len_hist_map ip route del 192.168.253.2/32 dev $VETH0 2> /dev/null ip link del $VETH0 2> /dev/null ip link del $VETH1 2> /dev/null