1 // SPDX-License-Identifier: GPL-2.0
2 // Copyright (c) 2018 Facebook
3
4 #include <err.h>
5 #include <bpf/libbpf.h>
6
7 #include "bpf_util.h"
8
9 struct sec_name_test {
10 const char sec_name[32];
11 struct {
12 int rc;
13 enum bpf_prog_type prog_type;
14 enum bpf_attach_type expected_attach_type;
15 } expected_load;
16 struct {
17 int rc;
18 enum bpf_attach_type attach_type;
19 } expected_attach;
20 };
21
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} },
31 {
32 "raw_tracepoint/",
33 {0, BPF_PROG_TYPE_RAW_TRACEPOINT, 0},
34 {-EINVAL, 0},
35 },
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} },
42 {
43 "cgroup_skb/ingress",
44 {0, BPF_PROG_TYPE_CGROUP_SKB, 0},
45 {0, BPF_CGROUP_INET_INGRESS},
46 },
47 {
48 "cgroup_skb/egress",
49 {0, BPF_PROG_TYPE_CGROUP_SKB, 0},
50 {0, BPF_CGROUP_INET_EGRESS},
51 },
52 {"cgroup/skb", {0, BPF_PROG_TYPE_CGROUP_SKB, 0}, {-EINVAL, 0} },
53 {
54 "cgroup/sock",
55 {0, BPF_PROG_TYPE_CGROUP_SOCK, 0},
56 {0, BPF_CGROUP_INET_SOCK_CREATE},
57 },
58 {
59 "cgroup/post_bind4",
60 {0, BPF_PROG_TYPE_CGROUP_SOCK, BPF_CGROUP_INET4_POST_BIND},
61 {0, BPF_CGROUP_INET4_POST_BIND},
62 },
63 {
64 "cgroup/post_bind6",
65 {0, BPF_PROG_TYPE_CGROUP_SOCK, BPF_CGROUP_INET6_POST_BIND},
66 {0, BPF_CGROUP_INET6_POST_BIND},
67 },
68 {
69 "cgroup/dev",
70 {0, BPF_PROG_TYPE_CGROUP_DEVICE, 0},
71 {0, BPF_CGROUP_DEVICE},
72 },
73 {"sockops", {0, BPF_PROG_TYPE_SOCK_OPS, 0}, {0, BPF_CGROUP_SOCK_OPS} },
74 {
75 "sk_skb/stream_parser",
76 {0, BPF_PROG_TYPE_SK_SKB, 0},
77 {0, BPF_SK_SKB_STREAM_PARSER},
78 },
79 {
80 "sk_skb/stream_verdict",
81 {0, BPF_PROG_TYPE_SK_SKB, 0},
82 {0, BPF_SK_SKB_STREAM_VERDICT},
83 },
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} },
87 {
88 "flow_dissector",
89 {0, BPF_PROG_TYPE_FLOW_DISSECTOR, 0},
90 {0, BPF_FLOW_DISSECTOR},
91 },
92 {
93 "cgroup/bind4",
94 {0, BPF_PROG_TYPE_CGROUP_SOCK_ADDR, BPF_CGROUP_INET4_BIND},
95 {0, BPF_CGROUP_INET4_BIND},
96 },
97 {
98 "cgroup/bind6",
99 {0, BPF_PROG_TYPE_CGROUP_SOCK_ADDR, BPF_CGROUP_INET6_BIND},
100 {0, BPF_CGROUP_INET6_BIND},
101 },
102 {
103 "cgroup/connect4",
104 {0, BPF_PROG_TYPE_CGROUP_SOCK_ADDR, BPF_CGROUP_INET4_CONNECT},
105 {0, BPF_CGROUP_INET4_CONNECT},
106 },
107 {
108 "cgroup/connect6",
109 {0, BPF_PROG_TYPE_CGROUP_SOCK_ADDR, BPF_CGROUP_INET6_CONNECT},
110 {0, BPF_CGROUP_INET6_CONNECT},
111 },
112 {
113 "cgroup/sendmsg4",
114 {0, BPF_PROG_TYPE_CGROUP_SOCK_ADDR, BPF_CGROUP_UDP4_SENDMSG},
115 {0, BPF_CGROUP_UDP4_SENDMSG},
116 },
117 {
118 "cgroup/sendmsg6",
119 {0, BPF_PROG_TYPE_CGROUP_SOCK_ADDR, BPF_CGROUP_UDP6_SENDMSG},
120 {0, BPF_CGROUP_UDP6_SENDMSG},
121 },
122 };
123
test_prog_type_by_name(const struct sec_name_test * test)124 static int test_prog_type_by_name(const struct sec_name_test *test)
125 {
126 enum bpf_attach_type expected_attach_type;
127 enum bpf_prog_type prog_type;
128 int rc;
129
130 rc = libbpf_prog_type_by_name(test->sec_name, &prog_type,
131 &expected_attach_type);
132
133 if (rc != test->expected_load.rc) {
134 warnx("prog: unexpected rc=%d for %s", rc, test->sec_name);
135 return -1;
136 }
137
138 if (rc)
139 return 0;
140
141 if (prog_type != test->expected_load.prog_type) {
142 warnx("prog: unexpected prog_type=%d for %s", prog_type,
143 test->sec_name);
144 return -1;
145 }
146
147 if (expected_attach_type != test->expected_load.expected_attach_type) {
148 warnx("prog: unexpected expected_attach_type=%d for %s",
149 expected_attach_type, test->sec_name);
150 return -1;
151 }
152
153 return 0;
154 }
155
test_attach_type_by_name(const struct sec_name_test * test)156 static int test_attach_type_by_name(const struct sec_name_test *test)
157 {
158 enum bpf_attach_type attach_type;
159 int rc;
160
161 rc = libbpf_attach_type_by_name(test->sec_name, &attach_type);
162
163 if (rc != test->expected_attach.rc) {
164 warnx("attach: unexpected rc=%d for %s", rc, test->sec_name);
165 return -1;
166 }
167
168 if (rc)
169 return 0;
170
171 if (attach_type != test->expected_attach.attach_type) {
172 warnx("attach: unexpected attach_type=%d for %s", attach_type,
173 test->sec_name);
174 return -1;
175 }
176
177 return 0;
178 }
179
run_test_case(const struct sec_name_test * test)180 static int run_test_case(const struct sec_name_test *test)
181 {
182 if (test_prog_type_by_name(test))
183 return -1;
184 if (test_attach_type_by_name(test))
185 return -1;
186 return 0;
187 }
188
run_tests(void)189 static int run_tests(void)
190 {
191 int passes = 0;
192 int fails = 0;
193 int i;
194
195 for (i = 0; i < ARRAY_SIZE(tests); ++i) {
196 if (run_test_case(&tests[i]))
197 ++fails;
198 else
199 ++passes;
200 }
201 printf("Summary: %d PASSED, %d FAILED\n", passes, fails);
202 return fails ? -1 : 0;
203 }
204
main(int argc,char ** argv)205 int main(int argc, char **argv)
206 {
207 return run_tests();
208 }
209