selftests/bpf: test_progs: remove global fail/success counts
[muen/linux.git] / tools / testing / selftests / bpf / prog_tests / bpf_obj_id.c
1 // SPDX-License-Identifier: GPL-2.0
2 #include <test_progs.h>
3
4 void test_bpf_obj_id(void)
5 {
6         const __u64 array_magic_value = 0xfaceb00c;
7         const __u32 array_key = 0;
8         const int nr_iters = 2;
9         const char *file = "./test_obj_id.o";
10         const char *expected_prog_name = "test_obj_id";
11         const char *expected_map_name = "test_map_id";
12         const __u64 nsec_per_sec = 1000000000;
13
14         struct bpf_object *objs[nr_iters];
15         int prog_fds[nr_iters], map_fds[nr_iters];
16         /* +1 to test for the info_len returned by kernel */
17         struct bpf_prog_info prog_infos[nr_iters + 1];
18         struct bpf_map_info map_infos[nr_iters + 1];
19         /* Each prog only uses one map. +1 to test nr_map_ids
20          * returned by kernel.
21          */
22         __u32 map_ids[nr_iters + 1];
23         char jited_insns[128], xlated_insns[128], zeros[128];
24         __u32 i, next_id, info_len, nr_id_found, duration = 0;
25         struct timespec real_time_ts, boot_time_ts;
26         int err = 0;
27         __u64 array_value;
28         uid_t my_uid = getuid();
29         time_t now, load_time;
30
31         err = bpf_prog_get_fd_by_id(0);
32         CHECK(err >= 0 || errno != ENOENT,
33               "get-fd-by-notexist-prog-id", "err %d errno %d\n", err, errno);
34
35         err = bpf_map_get_fd_by_id(0);
36         CHECK(err >= 0 || errno != ENOENT,
37               "get-fd-by-notexist-map-id", "err %d errno %d\n", err, errno);
38
39         for (i = 0; i < nr_iters; i++)
40                 objs[i] = NULL;
41
42         /* Check bpf_obj_get_info_by_fd() */
43         bzero(zeros, sizeof(zeros));
44         for (i = 0; i < nr_iters; i++) {
45                 now = time(NULL);
46                 err = bpf_prog_load(file, BPF_PROG_TYPE_SOCKET_FILTER,
47                                     &objs[i], &prog_fds[i]);
48                 /* test_obj_id.o is a dumb prog. It should never fail
49                  * to load.
50                  */
51                 CHECK_FAIL(err);
52                 assert(!err);
53
54                 /* Insert a magic value to the map */
55                 map_fds[i] = bpf_find_map(__func__, objs[i], "test_map_id");
56                 assert(map_fds[i] >= 0);
57                 err = bpf_map_update_elem(map_fds[i], &array_key,
58                                           &array_magic_value, 0);
59                 assert(!err);
60
61                 /* Check getting map info */
62                 info_len = sizeof(struct bpf_map_info) * 2;
63                 bzero(&map_infos[i], info_len);
64                 err = bpf_obj_get_info_by_fd(map_fds[i], &map_infos[i],
65                                              &info_len);
66                 if (CHECK(err ||
67                           map_infos[i].type != BPF_MAP_TYPE_ARRAY ||
68                           map_infos[i].key_size != sizeof(__u32) ||
69                           map_infos[i].value_size != sizeof(__u64) ||
70                           map_infos[i].max_entries != 1 ||
71                           map_infos[i].map_flags != 0 ||
72                           info_len != sizeof(struct bpf_map_info) ||
73                           strcmp((char *)map_infos[i].name, expected_map_name),
74                           "get-map-info(fd)",
75                           "err %d errno %d type %d(%d) info_len %u(%zu) key_size %u value_size %u max_entries %u map_flags %X name %s(%s)\n",
76                           err, errno,
77                           map_infos[i].type, BPF_MAP_TYPE_ARRAY,
78                           info_len, sizeof(struct bpf_map_info),
79                           map_infos[i].key_size,
80                           map_infos[i].value_size,
81                           map_infos[i].max_entries,
82                           map_infos[i].map_flags,
83                           map_infos[i].name, expected_map_name))
84                         goto done;
85
86                 /* Check getting prog info */
87                 info_len = sizeof(struct bpf_prog_info) * 2;
88                 bzero(&prog_infos[i], info_len);
89                 bzero(jited_insns, sizeof(jited_insns));
90                 bzero(xlated_insns, sizeof(xlated_insns));
91                 prog_infos[i].jited_prog_insns = ptr_to_u64(jited_insns);
92                 prog_infos[i].jited_prog_len = sizeof(jited_insns);
93                 prog_infos[i].xlated_prog_insns = ptr_to_u64(xlated_insns);
94                 prog_infos[i].xlated_prog_len = sizeof(xlated_insns);
95                 prog_infos[i].map_ids = ptr_to_u64(map_ids + i);
96                 prog_infos[i].nr_map_ids = 2;
97                 err = clock_gettime(CLOCK_REALTIME, &real_time_ts);
98                 assert(!err);
99                 err = clock_gettime(CLOCK_BOOTTIME, &boot_time_ts);
100                 assert(!err);
101                 err = bpf_obj_get_info_by_fd(prog_fds[i], &prog_infos[i],
102                                              &info_len);
103                 load_time = (real_time_ts.tv_sec - boot_time_ts.tv_sec)
104                         + (prog_infos[i].load_time / nsec_per_sec);
105                 if (CHECK(err ||
106                           prog_infos[i].type != BPF_PROG_TYPE_SOCKET_FILTER ||
107                           info_len != sizeof(struct bpf_prog_info) ||
108                           (env.jit_enabled && !prog_infos[i].jited_prog_len) ||
109                           (env.jit_enabled &&
110                            !memcmp(jited_insns, zeros, sizeof(zeros))) ||
111                           !prog_infos[i].xlated_prog_len ||
112                           !memcmp(xlated_insns, zeros, sizeof(zeros)) ||
113                           load_time < now - 60 || load_time > now + 60 ||
114                           prog_infos[i].created_by_uid != my_uid ||
115                           prog_infos[i].nr_map_ids != 1 ||
116                           *(int *)(long)prog_infos[i].map_ids != map_infos[i].id ||
117                           strcmp((char *)prog_infos[i].name, expected_prog_name),
118                           "get-prog-info(fd)",
119                           "err %d errno %d i %d type %d(%d) info_len %u(%zu) jit_enabled %d jited_prog_len %u xlated_prog_len %u jited_prog %d xlated_prog %d load_time %lu(%lu) uid %u(%u) nr_map_ids %u(%u) map_id %u(%u) name %s(%s)\n",
120                           err, errno, i,
121                           prog_infos[i].type, BPF_PROG_TYPE_SOCKET_FILTER,
122                           info_len, sizeof(struct bpf_prog_info),
123                           env.jit_enabled,
124                           prog_infos[i].jited_prog_len,
125                           prog_infos[i].xlated_prog_len,
126                           !!memcmp(jited_insns, zeros, sizeof(zeros)),
127                           !!memcmp(xlated_insns, zeros, sizeof(zeros)),
128                           load_time, now,
129                           prog_infos[i].created_by_uid, my_uid,
130                           prog_infos[i].nr_map_ids, 1,
131                           *(int *)(long)prog_infos[i].map_ids, map_infos[i].id,
132                           prog_infos[i].name, expected_prog_name))
133                         goto done;
134         }
135
136         /* Check bpf_prog_get_next_id() */
137         nr_id_found = 0;
138         next_id = 0;
139         while (!bpf_prog_get_next_id(next_id, &next_id)) {
140                 struct bpf_prog_info prog_info = {};
141                 __u32 saved_map_id;
142                 int prog_fd;
143
144                 info_len = sizeof(prog_info);
145
146                 prog_fd = bpf_prog_get_fd_by_id(next_id);
147                 if (prog_fd < 0 && errno == ENOENT)
148                         /* The bpf_prog is in the dead row */
149                         continue;
150                 if (CHECK(prog_fd < 0, "get-prog-fd(next_id)",
151                           "prog_fd %d next_id %d errno %d\n",
152                           prog_fd, next_id, errno))
153                         break;
154
155                 for (i = 0; i < nr_iters; i++)
156                         if (prog_infos[i].id == next_id)
157                                 break;
158
159                 if (i == nr_iters)
160                         continue;
161
162                 nr_id_found++;
163
164                 /* Negative test:
165                  * prog_info.nr_map_ids = 1
166                  * prog_info.map_ids = NULL
167                  */
168                 prog_info.nr_map_ids = 1;
169                 err = bpf_obj_get_info_by_fd(prog_fd, &prog_info, &info_len);
170                 if (CHECK(!err || errno != EFAULT,
171                           "get-prog-fd-bad-nr-map-ids", "err %d errno %d(%d)",
172                           err, errno, EFAULT))
173                         break;
174                 bzero(&prog_info, sizeof(prog_info));
175                 info_len = sizeof(prog_info);
176
177                 saved_map_id = *(int *)((long)prog_infos[i].map_ids);
178                 prog_info.map_ids = prog_infos[i].map_ids;
179                 prog_info.nr_map_ids = 2;
180                 err = bpf_obj_get_info_by_fd(prog_fd, &prog_info, &info_len);
181                 prog_infos[i].jited_prog_insns = 0;
182                 prog_infos[i].xlated_prog_insns = 0;
183                 CHECK(err || info_len != sizeof(struct bpf_prog_info) ||
184                       memcmp(&prog_info, &prog_infos[i], info_len) ||
185                       *(int *)(long)prog_info.map_ids != saved_map_id,
186                       "get-prog-info(next_id->fd)",
187                       "err %d errno %d info_len %u(%zu) memcmp %d map_id %u(%u)\n",
188                       err, errno, info_len, sizeof(struct bpf_prog_info),
189                       memcmp(&prog_info, &prog_infos[i], info_len),
190                       *(int *)(long)prog_info.map_ids, saved_map_id);
191                 close(prog_fd);
192         }
193         CHECK(nr_id_found != nr_iters,
194               "check total prog id found by get_next_id",
195               "nr_id_found %u(%u)\n",
196               nr_id_found, nr_iters);
197
198         /* Check bpf_map_get_next_id() */
199         nr_id_found = 0;
200         next_id = 0;
201         while (!bpf_map_get_next_id(next_id, &next_id)) {
202                 struct bpf_map_info map_info = {};
203                 int map_fd;
204
205                 info_len = sizeof(map_info);
206
207                 map_fd = bpf_map_get_fd_by_id(next_id);
208                 if (map_fd < 0 && errno == ENOENT)
209                         /* The bpf_map is in the dead row */
210                         continue;
211                 if (CHECK(map_fd < 0, "get-map-fd(next_id)",
212                           "map_fd %d next_id %u errno %d\n",
213                           map_fd, next_id, errno))
214                         break;
215
216                 for (i = 0; i < nr_iters; i++)
217                         if (map_infos[i].id == next_id)
218                                 break;
219
220                 if (i == nr_iters)
221                         continue;
222
223                 nr_id_found++;
224
225                 err = bpf_map_lookup_elem(map_fd, &array_key, &array_value);
226                 assert(!err);
227
228                 err = bpf_obj_get_info_by_fd(map_fd, &map_info, &info_len);
229                 CHECK(err || info_len != sizeof(struct bpf_map_info) ||
230                       memcmp(&map_info, &map_infos[i], info_len) ||
231                       array_value != array_magic_value,
232                       "check get-map-info(next_id->fd)",
233                       "err %d errno %d info_len %u(%zu) memcmp %d array_value %llu(%llu)\n",
234                       err, errno, info_len, sizeof(struct bpf_map_info),
235                       memcmp(&map_info, &map_infos[i], info_len),
236                       array_value, array_magic_value);
237
238                 close(map_fd);
239         }
240         CHECK(nr_id_found != nr_iters,
241               "check total map id found by get_next_id",
242               "nr_id_found %u(%u)\n",
243               nr_id_found, nr_iters);
244
245 done:
246         for (i = 0; i < nr_iters; i++)
247                 bpf_object__close(objs[i]);
248 }