Commit da00d2f1 authored by KP Singh's avatar KP Singh Committed by Alexei Starovoitov
Browse files

bpf: Add test ops for BPF_PROG_TYPE_TRACING



The current fexit and fentry tests rely on a different program to
exercise the functions they attach to. Instead of doing this, implement
the test operations for tracing which will also be used for
BPF_MODIFY_RETURN in a subsequent patch.

Also, clean up the fexit test to use the generated skeleton.

Signed-off-by: default avatarKP Singh <kpsingh@google.com>
Signed-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
Acked-by: default avatarAndrii Nakryiko <andriin@fb.com>
Acked-by: default avatarDaniel Borkmann <daniel@iogearbox.net>
Link: https://lore.kernel.org/bpf/20200304191853.1529-7-kpsingh@chromium.org
parent aca228cd
Loading
Loading
Loading
Loading
+10 −0
Original line number Diff line number Diff line
@@ -1156,6 +1156,9 @@ int bpf_prog_test_run_xdp(struct bpf_prog *prog, const union bpf_attr *kattr,
			  union bpf_attr __user *uattr);
int bpf_prog_test_run_skb(struct bpf_prog *prog, const union bpf_attr *kattr,
			  union bpf_attr __user *uattr);
int bpf_prog_test_run_tracing(struct bpf_prog *prog,
			      const union bpf_attr *kattr,
			      union bpf_attr __user *uattr);
int bpf_prog_test_run_flow_dissector(struct bpf_prog *prog,
				     const union bpf_attr *kattr,
				     union bpf_attr __user *uattr);
@@ -1313,6 +1316,13 @@ static inline int bpf_prog_test_run_skb(struct bpf_prog *prog,
	return -ENOTSUPP;
}

static inline int bpf_prog_test_run_tracing(struct bpf_prog *prog,
					    const union bpf_attr *kattr,
					    union bpf_attr __user *uattr)
{
	return -ENOTSUPP;
}

static inline int bpf_prog_test_run_flow_dissector(struct bpf_prog *prog,
						   const union bpf_attr *kattr,
						   union bpf_attr __user *uattr)
+1 −0
Original line number Diff line number Diff line
@@ -1266,6 +1266,7 @@ const struct bpf_verifier_ops tracing_verifier_ops = {
};

const struct bpf_prog_ops tracing_prog_ops = {
	.test_run = bpf_prog_test_run_tracing,
};

static bool raw_tp_writable_prog_is_valid_access(int off, int size,
+28 −9
Original line number Diff line number Diff line
@@ -160,16 +160,35 @@ static void *bpf_test_init(const union bpf_attr *kattr, u32 size,
		kfree(data);
		return ERR_PTR(-EFAULT);
	}

	return data;
}

int bpf_prog_test_run_tracing(struct bpf_prog *prog,
			      const union bpf_attr *kattr,
			      union bpf_attr __user *uattr)
{
	int err = -EFAULT;

	switch (prog->expected_attach_type) {
	case BPF_TRACE_FENTRY:
	case BPF_TRACE_FEXIT:
		if (bpf_fentry_test1(1) != 2 ||
		    bpf_fentry_test2(2, 3) != 5 ||
		    bpf_fentry_test3(4, 5, 6) != 15 ||
		    bpf_fentry_test4((void *)7, 8, 9, 10) != 34 ||
		    bpf_fentry_test5(11, (void *)12, 13, 14, 15) != 65 ||
	    bpf_fentry_test6(16, (void *)17, 18, 19, (void *)20, 21) != 111) {
		kfree(data);
		return ERR_PTR(-EFAULT);
		    bpf_fentry_test6(16, (void *)17, 18, 19, (void *)20, 21) != 111)
			goto out;
		break;
	default:
		goto out;
	}
	return data;

	err = 0;
out:
	trace_bpf_test_finish(&err);
	return err;
}

static void *bpf_ctx_init(const union bpf_attr *kattr, u32 max_size)
+3 −9
Original line number Diff line number Diff line
// SPDX-License-Identifier: GPL-2.0
/* Copyright (c) 2019 Facebook */
#include <test_progs.h>
#include "test_pkt_access.skel.h"
#include "fentry_test.skel.h"
#include "fexit_test.skel.h"

void test_fentry_fexit(void)
{
	struct test_pkt_access *pkt_skel = NULL;
	struct fentry_test *fentry_skel = NULL;
	struct fexit_test *fexit_skel = NULL;
	__u64 *fentry_res, *fexit_res;
	__u32 duration = 0, retval;
	int err, pkt_fd, i;
	int err, prog_fd, i;

	pkt_skel = test_pkt_access__open_and_load();
	if (CHECK(!pkt_skel, "pkt_skel_load", "pkt_access skeleton failed\n"))
		return;
	fentry_skel = fentry_test__open_and_load();
	if (CHECK(!fentry_skel, "fentry_skel_load", "fentry skeleton failed\n"))
		goto close_prog;
@@ -31,8 +26,8 @@ void test_fentry_fexit(void)
	if (CHECK(err, "fexit_attach", "fexit attach failed: %d\n", err))
		goto close_prog;

	pkt_fd = bpf_program__fd(pkt_skel->progs.test_pkt_access);
	err = bpf_prog_test_run(pkt_fd, 1, &pkt_v6, sizeof(pkt_v6),
	prog_fd = bpf_program__fd(fexit_skel->progs.test1);
	err = bpf_prog_test_run(prog_fd, 1, NULL, 0,
				NULL, NULL, &retval, &duration);
	CHECK(err || retval, "ipv6",
	      "err %d errno %d retval %d duration %d\n",
@@ -49,7 +44,6 @@ void test_fentry_fexit(void)
	}

close_prog:
	test_pkt_access__destroy(pkt_skel);
	fentry_test__destroy(fentry_skel);
	fexit_test__destroy(fexit_skel);
}
+4 −10
Original line number Diff line number Diff line
// SPDX-License-Identifier: GPL-2.0
/* Copyright (c) 2019 Facebook */
#include <test_progs.h>
#include "test_pkt_access.skel.h"
#include "fentry_test.skel.h"

void test_fentry_test(void)
{
	struct test_pkt_access *pkt_skel = NULL;
	struct fentry_test *fentry_skel = NULL;
	int err, pkt_fd, i;
	int err, prog_fd, i;
	__u32 duration = 0, retval;
	__u64 *result;

	pkt_skel = test_pkt_access__open_and_load();
	if (CHECK(!pkt_skel, "pkt_skel_load", "pkt_access skeleton failed\n"))
		return;
	fentry_skel = fentry_test__open_and_load();
	if (CHECK(!fentry_skel, "fentry_skel_load", "fentry skeleton failed\n"))
		goto cleanup;
@@ -23,10 +18,10 @@ void test_fentry_test(void)
	if (CHECK(err, "fentry_attach", "fentry attach failed: %d\n", err))
		goto cleanup;

	pkt_fd = bpf_program__fd(pkt_skel->progs.test_pkt_access);
	err = bpf_prog_test_run(pkt_fd, 1, &pkt_v6, sizeof(pkt_v6),
	prog_fd = bpf_program__fd(fentry_skel->progs.test1);
	err = bpf_prog_test_run(prog_fd, 1, NULL, 0,
				NULL, NULL, &retval, &duration);
	CHECK(err || retval, "ipv6",
	CHECK(err || retval, "test_run",
	      "err %d errno %d retval %d duration %d\n",
	      err, errno, retval, duration);

@@ -39,5 +34,4 @@ void test_fentry_test(void)

cleanup:
	fentry_test__destroy(fentry_skel);
	test_pkt_access__destroy(pkt_skel);
}
Loading