1 // SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause)
2 /* Copyright (C) 2019 Netronome Systems, Inc. */
3 /* Copyright (C) 2020 Facebook, Inc. */
8 #include <bpf/libbpf.h>
9 #include "test_progs.h"
10 #include "testing_helpers.h"
12 int parse_num_list(const char *s, bool **num_set, int *num_set_len)
14 int i, set_len = 0, new_len, num, start = 0, end = -1;
15 bool *set = NULL, *tmp, parsing_end = false;
20 num = strtol(s, &next, 10);
29 if (!parsing_end && *next == '-') {
33 } else if (*next == ',') {
37 } else if (*next == '\0') {
48 if (end + 1 > set_len) {
50 tmp = realloc(set, new_len);
55 for (i = set_len; i < start; i++)
60 for (i = start; i <= end; i++)
64 if (!set || parsing_end)
68 *num_set_len = set_len;
73 int parse_test_list(const char *s,
74 struct test_filter_set *set,
77 char *input, *state = NULL, *next;
78 struct test_filter *tmp, *tests = NULL;
85 while ((next = strtok_r(state ? NULL : input, ",", &state))) {
86 char *subtest_str = strchr(next, '/');
90 tmp = realloc(tests, sizeof(*tests) * (cnt + 1));
95 tests[cnt].subtest_cnt = 0;
96 tests[cnt].subtests = NULL;
98 if (is_glob_pattern) {
106 char **tmp_subtests = NULL;
107 int subtest_cnt = tests[cnt].subtest_cnt;
111 tmp_subtests = realloc(tests[cnt].subtests,
112 sizeof(*tmp_subtests) *
116 tests[cnt].subtests = tmp_subtests;
118 tests[cnt].subtests[subtest_cnt] =
119 malloc(strlen(subtest_str) + glob_chars + 1);
120 if (!tests[cnt].subtests[subtest_cnt])
122 sprintf(tests[cnt].subtests[subtest_cnt],
126 tests[cnt].subtest_cnt++;
129 tests[cnt].name = malloc(strlen(next) + glob_chars + 1);
130 if (!tests[cnt].name)
132 sprintf(tests[cnt].name, pattern, next);
137 tmp = realloc(set->tests, sizeof(*tests) * (cnt + set->cnt));
141 memcpy(tmp + set->cnt, tests, sizeof(*tests) * cnt);
150 for (i = 0; i < cnt; i++) {
151 for (j = 0; j < tests[i].subtest_cnt; j++)
152 free(tests[i].subtests[j]);
161 __u32 link_info_prog_id(const struct bpf_link *link, struct bpf_link_info *info)
163 __u32 info_len = sizeof(*info);
166 memset(info, 0, sizeof(*info));
167 err = bpf_obj_get_info_by_fd(bpf_link__fd(link), info, &info_len);
169 printf("failed to get link info: %d\n", -errno);
172 return info->prog_id;
175 int extra_prog_load_log_flags = 0;
177 int bpf_prog_test_load(const char *file, enum bpf_prog_type type,
178 struct bpf_object **pobj, int *prog_fd)
180 LIBBPF_OPTS(bpf_object_open_opts, opts,
181 .kernel_log_level = extra_prog_load_log_flags,
183 struct bpf_object *obj;
184 struct bpf_program *prog;
188 obj = bpf_object__open_file(file, &opts);
192 prog = bpf_object__next_program(obj, NULL);
198 if (type != BPF_PROG_TYPE_UNSPEC)
199 bpf_program__set_type(prog, type);
201 flags = bpf_program__flags(prog) | BPF_F_TEST_RND_HI32;
202 bpf_program__set_flags(prog, flags);
204 err = bpf_object__load(obj);
209 *prog_fd = bpf_program__fd(prog);
213 bpf_object__close(obj);
217 int bpf_test_load_program(enum bpf_prog_type type, const struct bpf_insn *insns,
218 size_t insns_cnt, const char *license,
219 __u32 kern_version, char *log_buf,
222 LIBBPF_OPTS(bpf_prog_load_opts, opts,
223 .kern_version = kern_version,
224 .prog_flags = BPF_F_TEST_RND_HI32,
225 .log_level = extra_prog_load_log_flags,
227 .log_size = log_buf_sz,
230 return bpf_prog_load(type, NULL, license, insns, insns_cnt, &opts);