Skip to content

Commit 23a3cc7

Browse files
laoarNobody
authored andcommitted
bpf: selftests: Add test case for BPF_F_PROG_NO_CHARGE
Test case to check if BPF_F_PROG_NO_CHARGE valid. The result as follows, $ ./test_progs ... #103 no_charge:OK ... Signed-off-by: Yafang Shao <[email protected]>
1 parent e1f128e commit 23a3cc7

File tree

1 file changed

+49
-0
lines changed

1 file changed

+49
-0
lines changed
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
#include <unistd.h>
3+
#include <errno.h>
4+
#include <string.h>
5+
#include <linux/bpf.h>
6+
#include <sys/syscall.h>
7+
8+
#include "test_progs.h"
9+
10+
#define BPF_ALU64_IMM(OP, DST, IMM) \
11+
((struct bpf_insn) { \
12+
.code = BPF_ALU64 | BPF_OP(OP) | BPF_K, \
13+
.dst_reg = DST, \
14+
.src_reg = 0, \
15+
.off = 0, \
16+
.imm = IMM })
17+
18+
#define BPF_EXIT_INSN() \
19+
((struct bpf_insn) { \
20+
.code = BPF_JMP | BPF_EXIT, \
21+
.dst_reg = 0, \
22+
.src_reg = 0, \
23+
.off = 0, \
24+
.imm = 0 })
25+
26+
void test_no_charge(void)
27+
{
28+
struct bpf_insn prog[] = {
29+
BPF_ALU64_IMM(BPF_MOV, BPF_REG_0, 0),
30+
BPF_EXIT_INSN(),
31+
};
32+
union bpf_attr attr;
33+
int duration = 0;
34+
int fd;
35+
36+
bzero(&attr, sizeof(attr));
37+
attr.prog_type = BPF_PROG_TYPE_SCHED_CLS;
38+
attr.insn_cnt = 2;
39+
attr.insns = (__u64)prog;
40+
attr.license = (__u64)("GPL");
41+
attr.prog_flags |= BPF_F_PROG_NO_CHARGE;
42+
43+
fd = syscall(__NR_bpf, BPF_PROG_LOAD, &attr, sizeof(attr));
44+
CHECK(fd < 0 && fd != -EPERM, "no_charge", "error: %s\n",
45+
strerror(errno));
46+
47+
if (fd > 0)
48+
close(fd);
49+
}

0 commit comments

Comments
 (0)