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/sendmsg4",
128 {0, BPF_PROG_TYPE_CGROUP_SOCK_ADDR, BPF_CGROUP_UDP4_SENDMSG},
129 {0, BPF_CGROUP_UDP4_SENDMSG},
130 },
131 {
132 "cgroup/sendmsg6",
133 {0, BPF_PROG_TYPE_CGROUP_SOCK_ADDR, BPF_CGROUP_UDP6_SENDMSG},
134 {0, BPF_CGROUP_UDP6_SENDMSG},
135 },
136 {
137 "cgroup/recvmsg4",
138 {0, BPF_PROG_TYPE_CGROUP_SOCK_ADDR, BPF_CGROUP_UDP4_RECVMSG},
139 {0, BPF_CGROUP_UDP4_RECVMSG},
140 },
141 {
142 "cgroup/recvmsg6",
143 {0, BPF_PROG_TYPE_CGROUP_SOCK_ADDR, BPF_CGROUP_UDP6_RECVMSG},
144 {0, BPF_CGROUP_UDP6_RECVMSG},
145 },
146 {
147 "cgroup/sysctl",
148 {0, BPF_PROG_TYPE_CGROUP_SYSCTL, BPF_CGROUP_SYSCTL},
149 {0, BPF_CGROUP_SYSCTL},
150 },
151 {
152 "cgroup/getsockopt",
153 {0, BPF_PROG_TYPE_CGROUP_SOCKOPT, BPF_CGROUP_GETSOCKOPT},
154 {0, BPF_CGROUP_GETSOCKOPT},
155 },
156 {
157 "cgroup/setsockopt",
158 {0, BPF_PROG_TYPE_CGROUP_SOCKOPT, BPF_CGROUP_SETSOCKOPT},
159 {0, BPF_CGROUP_SETSOCKOPT},
160 },
161 };
162
test_prog_type_by_name(const struct sec_name_test * test)163 static void test_prog_type_by_name(const struct sec_name_test *test)
164 {
165 enum bpf_attach_type expected_attach_type;
166 enum bpf_prog_type prog_type;
167 int rc;
168
169 rc = libbpf_prog_type_by_name(test->sec_name, &prog_type,
170 &expected_attach_type);
171
172 CHECK(rc != test->expected_load.rc, "check_code",
173 "prog: unexpected rc=%d for %s\n", rc, test->sec_name);
174
175 if (rc)
176 return;
177
178 CHECK(prog_type != test->expected_load.prog_type, "check_prog_type",
179 "prog: unexpected prog_type=%d for %s\n",
180 prog_type, test->sec_name);
181
182 CHECK(expected_attach_type != test->expected_load.expected_attach_type,
183 "check_attach_type", "prog: unexpected expected_attach_type=%d for %s\n",
184 expected_attach_type, test->sec_name);
185 }
186
test_attach_type_by_name(const struct sec_name_test * test)187 static void test_attach_type_by_name(const struct sec_name_test *test)
188 {
189 enum bpf_attach_type attach_type;
190 int rc;
191
192 rc = libbpf_attach_type_by_name(test->sec_name, &attach_type);
193
194 CHECK(rc != test->expected_attach.rc, "check_ret",
195 "attach: unexpected rc=%d for %s\n", rc, test->sec_name);
196
197 if (rc)
198 return;
199
200 CHECK(attach_type != test->expected_attach.attach_type,
201 "check_attach_type", "attach: unexpected attach_type=%d for %s\n",
202 attach_type, test->sec_name);
203 }
204
test_section_names(void)205 void test_section_names(void)
206 {
207 int i;
208
209 for (i = 0; i < ARRAY_SIZE(tests); ++i) {
210 struct sec_name_test *test = &tests[i];
211
212 test_prog_type_by_name(test);
213 test_attach_type_by_name(test);
214 }
215 }
216