From patchwork Wed Jul 10 14:15:48 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Joel Fernandes X-Patchwork-Id: 11038643 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 6C55913BD for ; Wed, 10 Jul 2019 14:16:31 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 5ED1D2883A for ; Wed, 10 Jul 2019 14:16:31 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 52BE0289CF; Wed, 10 Jul 2019 14:16:31 +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=-7.7 required=2.0 tests=BAYES_00,DKIM_INVALID, DKIM_SIGNED,MAILING_LIST_MULTI,RCVD_IN_DNSWL_HI 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 AED47289B3 for ; Wed, 10 Jul 2019 14:16:20 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727938AbfGJOQT (ORCPT ); Wed, 10 Jul 2019 10:16:19 -0400 Received: from mail-pf1-f196.google.com ([209.85.210.196]:32853 "EHLO mail-pf1-f196.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727932AbfGJOQN (ORCPT ); Wed, 10 Jul 2019 10:16:13 -0400 Received: by mail-pf1-f196.google.com with SMTP id g2so1189844pfq.0 for ; Wed, 10 Jul 2019 07:16:12 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=joelfernandes.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=Yg+Fia+4TffgvjfVtZaMrwTvAUof2HACdCWfE9Adz90=; b=puPKxWZQb+LZdgernOXSGHoCjjFfNqhgNCQgR3EAJVuYHTxPi0MWhE1gMoTZzoxDtL JPgeBqpXoNCnj35nK/tB1FCJMvJg2YoHOxNlJ0Vchnf+Zhz2VlASaorgKKmUkgTvSIJf 8FW/bsoH46dLIF8XeGGCXT64TEZRMHpQZe0+E= 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=Yg+Fia+4TffgvjfVtZaMrwTvAUof2HACdCWfE9Adz90=; b=Rc7Gdng1AtZHZQWn91n1t+zKzWRgVZce0OjfOLPDqeBPNPfALQTvmHbZtjjE3di8RI yPSil7du6hW8FS7L45FalBMyNrPdjBc35xRk/hJF4mmy8NIkJ+yYNLQoVjiuiaqhcWin oO8jRj9pPEhyNwnIR9wnSiY1GMeC4kur/28AaT4qUcGPmq3LxPA+UKoabx13xEHMIR3+ xo7bcLB2gQvVb08bI9wyDyk/8P+Uj4nSrs401kdVLIQN495gSEzi/ypAAwhwPzso9rk3 FjLdewhHHd941UgDLFW0YwOf3E/avC51AmAefGrjHHh5AxBZq+1QggAWolkYFR8litu2 uSHQ== X-Gm-Message-State: APjAAAUJUQ+uftoCJhrs3TvQbXOwwAC/HW9So2QM5L49T0XRsVsaDnfL /5IWs+SYLQAWSvyotfVVSdU= X-Google-Smtp-Source: APXvYqxireE36SRG7vQaha91GKI62TdGTjOZ+qzVeiQZzoJfKbV6cQyUm55T8TcIjbBj3K5Tt9zuKQ== X-Received: by 2002:a17:90a:290b:: with SMTP id g11mr7342173pjd.122.1562768172230; Wed, 10 Jul 2019 07:16:12 -0700 (PDT) Received: from joelaf.cam.corp.google.com ([2620:15c:6:12:9c46:e0da:efbf:69cc]) by smtp.gmail.com with ESMTPSA id l124sm2589249pgl.54.2019.07.10.07.16.08 (version=TLS1_3 cipher=AEAD-AES256-GCM-SHA384 bits=256/256); Wed, 10 Jul 2019 07:16:11 -0700 (PDT) From: "Joel Fernandes (Google)" To: linux-kernel@vger.kernel.org Cc: "Joel Fernandes (Google)" , Adrian Ratiu , Alexei Starovoitov , bpf@vger.kernel.org, Brendan Gregg , connoro@google.com, Daniel Borkmann , duyuchao , Ingo Molnar , jeffv@google.com, Karim Yaghmour , kernel-team@android.com, linux-kselftest@vger.kernel.org, Manali Shukla , Manjo Raja Rao , Martin KaFai Lau , Masami Hiramatsu , Matt Mullins , Michal Gregorczyk , Michal Gregorczyk , Mohammad Husain , namhyung@google.com, namhyung@kernel.org, netdev@vger.kernel.org, paul.chaignon@gmail.com, primiano@google.com, Qais Yousef , Shuah Khan , Song Liu , Srinivas Ramana , Steven Rostedt , Tamir Carmeli , Yonghong Song Subject: [PATCH RFC 4/4] selftests/bpf: Add test for ftrace-based BPF attach/detach Date: Wed, 10 Jul 2019 10:15:48 -0400 Message-Id: <20190710141548.132193-5-joel@joelfernandes.org> X-Mailer: git-send-email 2.22.0.410.gd8fdbe21b5-goog In-Reply-To: <20190710141548.132193-1-joel@joelfernandes.org> References: <20190710141548.132193-1-joel@joelfernandes.org> MIME-Version: 1.0 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 Here we add support for testing the attach and detach of a BPF program to a tracepoint through tracefs. Signed-off-by: Joel Fernandes (Google) --- .../raw_tp_writable_test_ftrace_run.c | 89 +++++++++++++++++++ 1 file changed, 89 insertions(+) create mode 100644 tools/testing/selftests/bpf/prog_tests/raw_tp_writable_test_ftrace_run.c diff --git a/tools/testing/selftests/bpf/prog_tests/raw_tp_writable_test_ftrace_run.c b/tools/testing/selftests/bpf/prog_tests/raw_tp_writable_test_ftrace_run.c new file mode 100644 index 000000000000..7b42e3a69b71 --- /dev/null +++ b/tools/testing/selftests/bpf/prog_tests/raw_tp_writable_test_ftrace_run.c @@ -0,0 +1,89 @@ +// SPDX-License-Identifier: GPL-2.0 + +#include +#include + +void test_raw_tp_writable_test_ftrace_run(void) +{ + __u32 duration = 0; + char error[4096]; + int ret; + + const struct bpf_insn trace_program[] = { + BPF_LDX_MEM(BPF_DW, BPF_REG_6, BPF_REG_1, 0), + BPF_LDX_MEM(BPF_W, BPF_REG_0, BPF_REG_6, 0), + BPF_MOV64_IMM(BPF_REG_0, 42), + BPF_STX_MEM(BPF_W, BPF_REG_6, BPF_REG_0, 0), + BPF_EXIT_INSN(), + }; + + struct bpf_load_program_attr load_attr = { + .prog_type = BPF_PROG_TYPE_RAW_TRACEPOINT_WRITABLE, + .license = "GPL v2", + .insns = trace_program, + .insns_cnt = sizeof(trace_program) / sizeof(struct bpf_insn), + .log_level = 2, + }; + + int bpf_fd = bpf_load_program_xattr(&load_attr, error, sizeof(error)); + + if (CHECK(bpf_fd < 0, "bpf_raw_tracepoint_writable loaded", + "failed: %d errno %d\n", bpf_fd, errno)) + return; + + const struct bpf_insn skb_program[] = { + BPF_MOV64_IMM(BPF_REG_0, 0), + BPF_EXIT_INSN(), + }; + + struct bpf_load_program_attr skb_load_attr = { + .prog_type = BPF_PROG_TYPE_SOCKET_FILTER, + .license = "GPL v2", + .insns = skb_program, + .insns_cnt = sizeof(skb_program) / sizeof(struct bpf_insn), + }; + + int filter_fd = + bpf_load_program_xattr(&skb_load_attr, error, sizeof(error)); + if (CHECK(filter_fd < 0, "test_program_loaded", "failed: %d errno %d\n", + filter_fd, errno)) + goto out_bpffd; + + ret = bpf_raw_tracepoint_ftrace_attach("bpf_test_run", + "bpf_test_finish", + bpf_fd); + if (CHECK(ret < 0, "bpf_raw_tracepoint_ftrace_attach", + "failed: %d errno %d\n", ret, errno)) + goto out_filterfd; + + char test_skb[128] = { + 0, + }; + + __u32 prog_ret; + int err = bpf_prog_test_run(filter_fd, 1, test_skb, sizeof(test_skb), 0, + 0, &prog_ret, 0); + CHECK(err != 42, "test_run", + "tracepoint did not modify return value\n"); + CHECK(prog_ret != 0, "test_run_ret", + "socket_filter did not return 0\n"); + + ret = bpf_raw_tracepoint_ftrace_detach("bpf_test_run", + "bpf_test_finish", + bpf_fd); + if (CHECK(ret < 0, "bpf_raw_tracepoint_ftrace_detach", + "failed: %d errno %d\n", ret, errno)) + goto out_filterfd; + + err = bpf_prog_test_run(filter_fd, 1, test_skb, sizeof(test_skb), 0, 0, + &prog_ret, 0); + CHECK(err != 0, "test_run_notrace", + "test_run failed with %d errno %d\n", err, errno); + CHECK(prog_ret != 0, "test_run_ret_notrace", + "socket_filter did not return 0\n"); + +out_filterfd: + close(filter_fd); +out_bpffd: + close(bpf_fd); +}