xref: /linux/tools/testing/selftests/bpf/prog_tests/section_names.c (revision c532de5a67a70f8533d495f8f2aaa9a0491c3ad0)
1 // SPDX-License-Identifier: GPL-2.0
2 // Copyright (c) 2018 Facebook
3 #include <test_progs.h>
4 
5 static int duration = 0;
6 
7 struct sec_name_test {
8 	const char sec_name[32];
9 	struct {
10 		int rc;
11 		enum bpf_prog_type prog_type;
12 		enum bpf_attach_type expected_attach_type;
13 	} expected_load;
14 	struct {
15 		int rc;
16 		enum bpf_attach_type attach_type;
17 	} expected_attach;
18 };
19 
20 static struct sec_name_test tests[] = {
21 	{"InvAliD", {-ESRCH, 0, 0}, {-EINVAL, 0} },
22 	{"cgroup", {-ESRCH, 0, 0}, {-EINVAL, 0} },
23 	{"socket", {0, BPF_PROG_TYPE_SOCKET_FILTER, 0}, {-EINVAL, 0} },
24 	{"kprobe/", {0, BPF_PROG_TYPE_KPROBE, 0}, {-EINVAL, 0} },
25 	{"uprobe/", {0, BPF_PROG_TYPE_KPROBE, 0}, {-EINVAL, 0} },
26 	{"kretprobe/", {0, BPF_PROG_TYPE_KPROBE, 0}, {-EINVAL, 0} },
27 	{"uretprobe/", {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 	{"tp/", {0, BPF_PROG_TYPE_TRACEPOINT, 0}, {-EINVAL, 0} },
32 	{
33 		"raw_tracepoint/",
34 		{0, BPF_PROG_TYPE_RAW_TRACEPOINT, 0},
35 		{-EINVAL, 0},
36 	},
37 	{"raw_tp/", {0, BPF_PROG_TYPE_RAW_TRACEPOINT, 0}, {-EINVAL, 0} },
38 	{"xdp", {0, BPF_PROG_TYPE_XDP, BPF_XDP}, {0, BPF_XDP} },
39 	{"perf_event", {0, BPF_PROG_TYPE_PERF_EVENT, 0}, {-EINVAL, 0} },
40 	{"lwt_in", {0, BPF_PROG_TYPE_LWT_IN, 0}, {-EINVAL, 0} },
41 	{"lwt_out", {0, BPF_PROG_TYPE_LWT_OUT, 0}, {-EINVAL, 0} },
42 	{"lwt_xmit", {0, BPF_PROG_TYPE_LWT_XMIT, 0}, {-EINVAL, 0} },
43 	{"lwt_seg6local", {0, BPF_PROG_TYPE_LWT_SEG6LOCAL, 0}, {-EINVAL, 0} },
44 	{
45 		"cgroup_skb/ingress",
46 		{0, BPF_PROG_TYPE_CGROUP_SKB, BPF_CGROUP_INET_INGRESS},
47 		{0, BPF_CGROUP_INET_INGRESS},
48 	},
49 	{
50 		"cgroup_skb/egress",
51 		{0, BPF_PROG_TYPE_CGROUP_SKB, BPF_CGROUP_INET_EGRESS},
52 		{0, BPF_CGROUP_INET_EGRESS},
53 	},
54 	{"cgroup/skb", {0, BPF_PROG_TYPE_CGROUP_SKB, 0}, {-EINVAL, 0} },
55 	{
56 		"cgroup/sock",
57 		{0, BPF_PROG_TYPE_CGROUP_SOCK, BPF_CGROUP_INET_SOCK_CREATE},
58 		{0, BPF_CGROUP_INET_SOCK_CREATE},
59 	},
60 	{
61 		"cgroup/post_bind4",
62 		{0, BPF_PROG_TYPE_CGROUP_SOCK, BPF_CGROUP_INET4_POST_BIND},
63 		{0, BPF_CGROUP_INET4_POST_BIND},
64 	},
65 	{
66 		"cgroup/post_bind6",
67 		{0, BPF_PROG_TYPE_CGROUP_SOCK, BPF_CGROUP_INET6_POST_BIND},
68 		{0, BPF_CGROUP_INET6_POST_BIND},
69 	},
70 	{
71 		"cgroup/dev",
72 		{0, BPF_PROG_TYPE_CGROUP_DEVICE, BPF_CGROUP_DEVICE},
73 		{0, BPF_CGROUP_DEVICE},
74 	},
75 	{
76 		"sockops",
77 		{0, BPF_PROG_TYPE_SOCK_OPS, BPF_CGROUP_SOCK_OPS},
78 		{0, BPF_CGROUP_SOCK_OPS},
79 	},
80 	{
81 		"sk_skb/stream_parser",
82 		{0, BPF_PROG_TYPE_SK_SKB, BPF_SK_SKB_STREAM_PARSER},
83 		{0, BPF_SK_SKB_STREAM_PARSER},
84 	},
85 	{
86 		"sk_skb/stream_verdict",
87 		{0, BPF_PROG_TYPE_SK_SKB, BPF_SK_SKB_STREAM_VERDICT},
88 		{0, BPF_SK_SKB_STREAM_VERDICT},
89 	},
90 	{"sk_skb", {0, BPF_PROG_TYPE_SK_SKB, 0}, {-EINVAL, 0} },
91 	{
92 		"sk_msg",
93 		{0, BPF_PROG_TYPE_SK_MSG, BPF_SK_MSG_VERDICT},
94 		{0, BPF_SK_MSG_VERDICT},
95 	},
96 	{
97 		"lirc_mode2",
98 		{0, BPF_PROG_TYPE_LIRC_MODE2, BPF_LIRC_MODE2},
99 		{0, BPF_LIRC_MODE2},
100 	},
101 	{
102 		"flow_dissector",
103 		{0, BPF_PROG_TYPE_FLOW_DISSECTOR, BPF_FLOW_DISSECTOR},
104 		{0, BPF_FLOW_DISSECTOR},
105 	},
106 	{
107 		"cgroup/bind4",
108 		{0, BPF_PROG_TYPE_CGROUP_SOCK_ADDR, BPF_CGROUP_INET4_BIND},
109 		{0, BPF_CGROUP_INET4_BIND},
110 	},
111 	{
112 		"cgroup/bind6",
113 		{0, BPF_PROG_TYPE_CGROUP_SOCK_ADDR, BPF_CGROUP_INET6_BIND},
114 		{0, BPF_CGROUP_INET6_BIND},
115 	},
116 	{
117 		"cgroup/connect4",
118 		{0, BPF_PROG_TYPE_CGROUP_SOCK_ADDR, BPF_CGROUP_INET4_CONNECT},
119 		{0, BPF_CGROUP_INET4_CONNECT},
120 	},
121 	{
122 		"cgroup/connect6",
123 		{0, BPF_PROG_TYPE_CGROUP_SOCK_ADDR, BPF_CGROUP_INET6_CONNECT},
124 		{0, BPF_CGROUP_INET6_CONNECT},
125 	},
126 	{
127 		"cgroup/connect_unix",
128 		{0, BPF_PROG_TYPE_CGROUP_SOCK_ADDR, BPF_CGROUP_UNIX_CONNECT},
129 		{0, BPF_CGROUP_UNIX_CONNECT},
130 	},
131 	{
132 		"cgroup/sendmsg4",
133 		{0, BPF_PROG_TYPE_CGROUP_SOCK_ADDR, BPF_CGROUP_UDP4_SENDMSG},
134 		{0, BPF_CGROUP_UDP4_SENDMSG},
135 	},
136 	{
137 		"cgroup/sendmsg6",
138 		{0, BPF_PROG_TYPE_CGROUP_SOCK_ADDR, BPF_CGROUP_UDP6_SENDMSG},
139 		{0, BPF_CGROUP_UDP6_SENDMSG},
140 	},
141 	{
142 		"cgroup/sendmsg_unix",
143 		{0, BPF_PROG_TYPE_CGROUP_SOCK_ADDR, BPF_CGROUP_UNIX_SENDMSG},
144 		{0, BPF_CGROUP_UNIX_SENDMSG},
145 	},
146 	{
147 		"cgroup/recvmsg4",
148 		{0, BPF_PROG_TYPE_CGROUP_SOCK_ADDR, BPF_CGROUP_UDP4_RECVMSG},
149 		{0, BPF_CGROUP_UDP4_RECVMSG},
150 	},
151 	{
152 		"cgroup/recvmsg6",
153 		{0, BPF_PROG_TYPE_CGROUP_SOCK_ADDR, BPF_CGROUP_UDP6_RECVMSG},
154 		{0, BPF_CGROUP_UDP6_RECVMSG},
155 	},
156 	{
157 		"cgroup/recvmsg_unix",
158 		{0, BPF_PROG_TYPE_CGROUP_SOCK_ADDR, BPF_CGROUP_UNIX_RECVMSG},
159 		{0, BPF_CGROUP_UNIX_RECVMSG},
160 	},
161 	{
162 		"cgroup/sysctl",
163 		{0, BPF_PROG_TYPE_CGROUP_SYSCTL, BPF_CGROUP_SYSCTL},
164 		{0, BPF_CGROUP_SYSCTL},
165 	},
166 	{
167 		"cgroup/getsockopt",
168 		{0, BPF_PROG_TYPE_CGROUP_SOCKOPT, BPF_CGROUP_GETSOCKOPT},
169 		{0, BPF_CGROUP_GETSOCKOPT},
170 	},
171 	{
172 		"cgroup/setsockopt",
173 		{0, BPF_PROG_TYPE_CGROUP_SOCKOPT, BPF_CGROUP_SETSOCKOPT},
174 		{0, BPF_CGROUP_SETSOCKOPT},
175 	},
176 	{
177 		"cgroup/getpeername4",
178 		{0, BPF_PROG_TYPE_CGROUP_SOCK_ADDR, BPF_CGROUP_INET4_GETPEERNAME},
179 		{0, BPF_CGROUP_INET4_GETPEERNAME},
180 	},
181 	{
182 		"cgroup/getpeername6",
183 		{0, BPF_PROG_TYPE_CGROUP_SOCK_ADDR, BPF_CGROUP_INET6_GETPEERNAME},
184 		{0, BPF_CGROUP_INET6_GETPEERNAME},
185 	},
186 	{
187 		"cgroup/getpeername_unix",
188 		{0, BPF_PROG_TYPE_CGROUP_SOCK_ADDR, BPF_CGROUP_UNIX_GETPEERNAME},
189 		{0, BPF_CGROUP_UNIX_GETPEERNAME},
190 	},
191 	{
192 		"cgroup/getsockname4",
193 		{0, BPF_PROG_TYPE_CGROUP_SOCK_ADDR, BPF_CGROUP_INET4_GETSOCKNAME},
194 		{0, BPF_CGROUP_INET4_GETSOCKNAME},
195 	},
196 	{
197 		"cgroup/getsockname6",
198 		{0, BPF_PROG_TYPE_CGROUP_SOCK_ADDR, BPF_CGROUP_INET6_GETSOCKNAME},
199 		{0, BPF_CGROUP_INET6_GETSOCKNAME},
200 	},
201 	{
202 		"cgroup/getsockname_unix",
203 		{0, BPF_PROG_TYPE_CGROUP_SOCK_ADDR, BPF_CGROUP_UNIX_GETSOCKNAME},
204 		{0, BPF_CGROUP_UNIX_GETSOCKNAME},
205 	},
206 };
207 
208 static void test_prog_type_by_name(const struct sec_name_test *test)
209 {
210 	enum bpf_attach_type expected_attach_type;
211 	enum bpf_prog_type prog_type;
212 	int rc;
213 
214 	rc = libbpf_prog_type_by_name(test->sec_name, &prog_type,
215 				      &expected_attach_type);
216 
217 	CHECK(rc != test->expected_load.rc, "check_code",
218 	      "prog: unexpected rc=%d for %s\n", rc, test->sec_name);
219 
220 	if (rc)
221 		return;
222 
223 	CHECK(prog_type != test->expected_load.prog_type, "check_prog_type",
224 	      "prog: unexpected prog_type=%d for %s\n",
225 	      prog_type, test->sec_name);
226 
227 	CHECK(expected_attach_type != test->expected_load.expected_attach_type,
228 	      "check_attach_type", "prog: unexpected expected_attach_type=%d for %s\n",
229 	      expected_attach_type, test->sec_name);
230 }
231 
232 static void test_attach_type_by_name(const struct sec_name_test *test)
233 {
234 	enum bpf_attach_type attach_type;
235 	int rc;
236 
237 	rc = libbpf_attach_type_by_name(test->sec_name, &attach_type);
238 
239 	CHECK(rc != test->expected_attach.rc, "check_ret",
240 	      "attach: unexpected rc=%d for %s\n", rc, test->sec_name);
241 
242 	if (rc)
243 		return;
244 
245 	CHECK(attach_type != test->expected_attach.attach_type,
246 	      "check_attach_type", "attach: unexpected attach_type=%d for %s\n",
247 	      attach_type, test->sec_name);
248 }
249 
250 void test_section_names(void)
251 {
252 	int i;
253 
254 	for (i = 0; i < ARRAY_SIZE(tests); ++i) {
255 		struct sec_name_test *test = &tests[i];
256 
257 		test_prog_type_by_name(test);
258 		test_attach_type_by_name(test);
259 	}
260 }
261