1 // SPDX-License-Identifier: GPL-2.0
2 // Copyright (c) 2018 Facebook
5 #include <bpf/libbpf.h>
10 const char sec_name
[32];
13 enum bpf_prog_type prog_type
;
14 enum bpf_attach_type expected_attach_type
;
18 enum bpf_attach_type attach_type
;
22 static struct sec_name_test tests
[] = {
23 {"InvAliD", {-EINVAL
, 0, 0}, {-EINVAL
, 0} },
24 {"cgroup", {-EINVAL
, 0, 0}, {-EINVAL
, 0} },
25 {"socket", {0, BPF_PROG_TYPE_SOCKET_FILTER
, 0}, {-EINVAL
, 0} },
26 {"kprobe/", {0, BPF_PROG_TYPE_KPROBE
, 0}, {-EINVAL
, 0} },
27 {"kretprobe/", {0, BPF_PROG_TYPE_KPROBE
, 0}, {-EINVAL
, 0} },
28 {"classifier", {0, BPF_PROG_TYPE_SCHED_CLS
, 0}, {-EINVAL
, 0} },
29 {"action", {0, BPF_PROG_TYPE_SCHED_ACT
, 0}, {-EINVAL
, 0} },
30 {"tracepoint/", {0, BPF_PROG_TYPE_TRACEPOINT
, 0}, {-EINVAL
, 0} },
33 {0, BPF_PROG_TYPE_RAW_TRACEPOINT
, 0},
36 {"xdp", {0, BPF_PROG_TYPE_XDP
, 0}, {-EINVAL
, 0} },
37 {"perf_event", {0, BPF_PROG_TYPE_PERF_EVENT
, 0}, {-EINVAL
, 0} },
38 {"lwt_in", {0, BPF_PROG_TYPE_LWT_IN
, 0}, {-EINVAL
, 0} },
39 {"lwt_out", {0, BPF_PROG_TYPE_LWT_OUT
, 0}, {-EINVAL
, 0} },
40 {"lwt_xmit", {0, BPF_PROG_TYPE_LWT_XMIT
, 0}, {-EINVAL
, 0} },
41 {"lwt_seg6local", {0, BPF_PROG_TYPE_LWT_SEG6LOCAL
, 0}, {-EINVAL
, 0} },
44 {0, BPF_PROG_TYPE_CGROUP_SKB
, 0},
45 {0, BPF_CGROUP_INET_INGRESS
},
49 {0, BPF_PROG_TYPE_CGROUP_SKB
, 0},
50 {0, BPF_CGROUP_INET_EGRESS
},
52 {"cgroup/skb", {0, BPF_PROG_TYPE_CGROUP_SKB
, 0}, {-EINVAL
, 0} },
55 {0, BPF_PROG_TYPE_CGROUP_SOCK
, 0},
56 {0, BPF_CGROUP_INET_SOCK_CREATE
},
60 {0, BPF_PROG_TYPE_CGROUP_SOCK
, BPF_CGROUP_INET4_POST_BIND
},
61 {0, BPF_CGROUP_INET4_POST_BIND
},
65 {0, BPF_PROG_TYPE_CGROUP_SOCK
, BPF_CGROUP_INET6_POST_BIND
},
66 {0, BPF_CGROUP_INET6_POST_BIND
},
70 {0, BPF_PROG_TYPE_CGROUP_DEVICE
, 0},
71 {0, BPF_CGROUP_DEVICE
},
73 {"sockops", {0, BPF_PROG_TYPE_SOCK_OPS
, 0}, {0, BPF_CGROUP_SOCK_OPS
} },
75 "sk_skb/stream_parser",
76 {0, BPF_PROG_TYPE_SK_SKB
, 0},
77 {0, BPF_SK_SKB_STREAM_PARSER
},
80 "sk_skb/stream_verdict",
81 {0, BPF_PROG_TYPE_SK_SKB
, 0},
82 {0, BPF_SK_SKB_STREAM_VERDICT
},
84 {"sk_skb", {0, BPF_PROG_TYPE_SK_SKB
, 0}, {-EINVAL
, 0} },
85 {"sk_msg", {0, BPF_PROG_TYPE_SK_MSG
, 0}, {0, BPF_SK_MSG_VERDICT
} },
86 {"lirc_mode2", {0, BPF_PROG_TYPE_LIRC_MODE2
, 0}, {0, BPF_LIRC_MODE2
} },
89 {0, BPF_PROG_TYPE_FLOW_DISSECTOR
, 0},
90 {0, BPF_FLOW_DISSECTOR
},
94 {0, BPF_PROG_TYPE_CGROUP_SOCK_ADDR
, BPF_CGROUP_INET4_BIND
},
95 {0, BPF_CGROUP_INET4_BIND
},
99 {0, BPF_PROG_TYPE_CGROUP_SOCK_ADDR
, BPF_CGROUP_INET6_BIND
},
100 {0, BPF_CGROUP_INET6_BIND
},
104 {0, BPF_PROG_TYPE_CGROUP_SOCK_ADDR
, BPF_CGROUP_INET4_CONNECT
},
105 {0, BPF_CGROUP_INET4_CONNECT
},
109 {0, BPF_PROG_TYPE_CGROUP_SOCK_ADDR
, BPF_CGROUP_INET6_CONNECT
},
110 {0, BPF_CGROUP_INET6_CONNECT
},
114 {0, BPF_PROG_TYPE_CGROUP_SOCK_ADDR
, BPF_CGROUP_UDP4_SENDMSG
},
115 {0, BPF_CGROUP_UDP4_SENDMSG
},
119 {0, BPF_PROG_TYPE_CGROUP_SOCK_ADDR
, BPF_CGROUP_UDP6_SENDMSG
},
120 {0, BPF_CGROUP_UDP6_SENDMSG
},
124 {0, BPF_PROG_TYPE_CGROUP_SOCK_ADDR
, BPF_CGROUP_UDP4_RECVMSG
},
125 {0, BPF_CGROUP_UDP4_RECVMSG
},
129 {0, BPF_PROG_TYPE_CGROUP_SOCK_ADDR
, BPF_CGROUP_UDP6_RECVMSG
},
130 {0, BPF_CGROUP_UDP6_RECVMSG
},
134 {0, BPF_PROG_TYPE_CGROUP_SYSCTL
, BPF_CGROUP_SYSCTL
},
135 {0, BPF_CGROUP_SYSCTL
},
139 {0, BPF_PROG_TYPE_CGROUP_SOCKOPT
, BPF_CGROUP_GETSOCKOPT
},
140 {0, BPF_CGROUP_GETSOCKOPT
},
144 {0, BPF_PROG_TYPE_CGROUP_SOCKOPT
, BPF_CGROUP_SETSOCKOPT
},
145 {0, BPF_CGROUP_SETSOCKOPT
},
149 static int test_prog_type_by_name(const struct sec_name_test
*test
)
151 enum bpf_attach_type expected_attach_type
;
152 enum bpf_prog_type prog_type
;
155 rc
= libbpf_prog_type_by_name(test
->sec_name
, &prog_type
,
156 &expected_attach_type
);
158 if (rc
!= test
->expected_load
.rc
) {
159 warnx("prog: unexpected rc=%d for %s", rc
, test
->sec_name
);
166 if (prog_type
!= test
->expected_load
.prog_type
) {
167 warnx("prog: unexpected prog_type=%d for %s", prog_type
,
172 if (expected_attach_type
!= test
->expected_load
.expected_attach_type
) {
173 warnx("prog: unexpected expected_attach_type=%d for %s",
174 expected_attach_type
, test
->sec_name
);
181 static int test_attach_type_by_name(const struct sec_name_test
*test
)
183 enum bpf_attach_type attach_type
;
186 rc
= libbpf_attach_type_by_name(test
->sec_name
, &attach_type
);
188 if (rc
!= test
->expected_attach
.rc
) {
189 warnx("attach: unexpected rc=%d for %s", rc
, test
->sec_name
);
196 if (attach_type
!= test
->expected_attach
.attach_type
) {
197 warnx("attach: unexpected attach_type=%d for %s", attach_type
,
205 static int run_test_case(const struct sec_name_test
*test
)
207 if (test_prog_type_by_name(test
))
209 if (test_attach_type_by_name(test
))
214 static int run_tests(void)
220 for (i
= 0; i
< ARRAY_SIZE(tests
); ++i
) {
221 if (run_test_case(&tests
[i
]))
226 printf("Summary: %d PASSED, %d FAILED\n", passes
, fails
);
227 return fails
? -1 : 0;
230 int main(int argc
, char **argv
)