Skip to content

Commit df47fc3

Browse files
lmbAlexei Starovoitov
authored andcommitted
selftests: add a test for bpf_prog_test_run_xattr
Make sure that bpf_prog_test_run_xattr returns the correct length and that the kernel respects the output size hint. Also check that errno indicates ENOSPC if there is a short output buffer given. Signed-off-by: Lorenz Bauer <lmb@cloudflare.com> Signed-off-by: Alexei Starovoitov <ast@kernel.org>
1 parent 64a9759 commit df47fc3

File tree

1 file changed

+54
-1
lines changed

1 file changed

+54
-1
lines changed

tools/testing/selftests/bpf/test_progs.c

Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ static struct {
7070
.tcp.urg_ptr = 123,
7171
};
7272

73-
#define CHECK(condition, tag, format...) ({ \
73+
#define _CHECK(condition, tag, duration, format...) ({ \
7474
int __ret = !!(condition); \
7575
if (__ret) { \
7676
error_cnt++; \
@@ -83,6 +83,11 @@ static struct {
8383
__ret; \
8484
})
8585

86+
#define CHECK(condition, tag, format...) \
87+
_CHECK(condition, tag, duration, format)
88+
#define CHECK_ATTR(condition, tag, format...) \
89+
_CHECK(condition, tag, tattr.duration, format)
90+
8691
static int bpf_find_map(const char *test, struct bpf_object *obj,
8792
const char *name)
8893
{
@@ -124,6 +129,53 @@ static void test_pkt_access(void)
124129
bpf_object__close(obj);
125130
}
126131

132+
static void test_prog_run_xattr(void)
133+
{
134+
const char *file = "./test_pkt_access.o";
135+
struct bpf_object *obj;
136+
char buf[10];
137+
int err;
138+
struct bpf_prog_test_run_attr tattr = {
139+
.repeat = 1,
140+
.data_in = &pkt_v4,
141+
.data_size_in = sizeof(pkt_v4),
142+
.data_out = buf,
143+
.data_size_out = 5,
144+
};
145+
146+
err = bpf_prog_load(file, BPF_PROG_TYPE_SCHED_CLS, &obj,
147+
&tattr.prog_fd);
148+
if (CHECK_ATTR(err, "load", "err %d errno %d\n", err, errno))
149+
return;
150+
151+
memset(buf, 0, sizeof(buf));
152+
153+
err = bpf_prog_test_run_xattr(&tattr);
154+
CHECK_ATTR(err != -1 || errno != ENOSPC || tattr.retval, "run",
155+
"err %d errno %d retval %d\n", err, errno, tattr.retval);
156+
157+
CHECK_ATTR(tattr.data_size_out != sizeof(pkt_v4), "data_size_out",
158+
"incorrect output size, want %lu have %u\n",
159+
sizeof(pkt_v4), tattr.data_size_out);
160+
161+
CHECK_ATTR(buf[5] != 0, "overflow",
162+
"BPF_PROG_TEST_RUN ignored size hint\n");
163+
164+
tattr.data_out = NULL;
165+
tattr.data_size_out = 0;
166+
errno = 0;
167+
168+
err = bpf_prog_test_run_xattr(&tattr);
169+
CHECK_ATTR(err || errno || tattr.retval, "run_no_output",
170+
"err %d errno %d retval %d\n", err, errno, tattr.retval);
171+
172+
tattr.data_size_out = 1;
173+
err = bpf_prog_test_run_xattr(&tattr);
174+
CHECK_ATTR(err != -EINVAL, "run_wrong_size_out", "err %d\n", err);
175+
176+
bpf_object__close(obj);
177+
}
178+
127179
static void test_xdp(void)
128180
{
129181
struct vip key4 = {.protocol = 6, .family = AF_INET};
@@ -1837,6 +1889,7 @@ int main(void)
18371889
jit_enabled = is_jit_enabled();
18381890

18391891
test_pkt_access();
1892+
test_prog_run_xattr();
18401893
test_xdp();
18411894
test_xdp_adjust_tail();
18421895
test_l4lb_all();

0 commit comments

Comments
 (0)