tools: libbpf: add a correctly named define for map iteration
[sfrench/cifs-2.6.git] / tools / testing / selftests / bpf / test_libbpf_open.c
1 /* SPDX-License-Identifier: GPL-2.0
2  * Copyright (c) 2018 Jesper Dangaard Brouer, Red Hat Inc.
3  */
4 static const char *__doc__ =
5         "Libbpf test program for loading BPF ELF object files";
6
7 #include <stdlib.h>
8 #include <stdio.h>
9 #include <string.h>
10 #include <stdarg.h>
11 #include <bpf/libbpf.h>
12 #include <getopt.h>
13
14 static const struct option long_options[] = {
15         {"help",        no_argument,            NULL, 'h' },
16         {"debug",       no_argument,            NULL, 'D' },
17         {"quiet",       no_argument,            NULL, 'q' },
18         {0, 0, NULL,  0 }
19 };
20
21 static void usage(char *argv[])
22 {
23         int i;
24
25         printf("\nDOCUMENTATION:\n%s\n\n", __doc__);
26         printf(" Usage: %s (options-see-below) BPF_FILE\n", argv[0]);
27         printf(" Listing options:\n");
28         for (i = 0; long_options[i].name != 0; i++) {
29                 printf(" --%-12s", long_options[i].name);
30                 printf(" short-option: -%c",
31                        long_options[i].val);
32                 printf("\n");
33         }
34         printf("\n");
35 }
36
37 static bool debug = 0;
38 static int libbpf_debug_print(enum libbpf_print_level level,
39                               const char *fmt, va_list args)
40 {
41         if (level == LIBBPF_DEBUG && !debug)
42                 return 0;
43
44         fprintf(stderr, "[%d] ", level);
45         return vfprintf(stderr, fmt, args);
46 }
47
48 #define EXIT_FAIL_LIBBPF EXIT_FAILURE
49 #define EXIT_FAIL_OPTION 2
50
51 int test_walk_progs(struct bpf_object *obj, bool verbose)
52 {
53         struct bpf_program *prog;
54         int cnt = 0;
55
56         bpf_object__for_each_program(prog, obj) {
57                 cnt++;
58                 if (verbose)
59                         printf("Prog (count:%d) section_name: %s\n", cnt,
60                                bpf_program__title(prog, false));
61         }
62         return 0;
63 }
64
65 int test_walk_maps(struct bpf_object *obj, bool verbose)
66 {
67         struct bpf_map *map;
68         int cnt = 0;
69
70         bpf_object__for_each_map(map, obj) {
71                 cnt++;
72                 if (verbose)
73                         printf("Map (count:%d) name: %s\n", cnt,
74                                bpf_map__name(map));
75         }
76         return 0;
77 }
78
79 int test_open_file(char *filename, bool verbose)
80 {
81         struct bpf_object *bpfobj = NULL;
82         long err;
83
84         if (verbose)
85                 printf("Open BPF ELF-file with libbpf: %s\n", filename);
86
87         /* Load BPF ELF object file and check for errors */
88         bpfobj = bpf_object__open(filename);
89         err = libbpf_get_error(bpfobj);
90         if (err) {
91                 char err_buf[128];
92                 libbpf_strerror(err, err_buf, sizeof(err_buf));
93                 if (verbose)
94                         printf("Unable to load eBPF objects in file '%s': %s\n",
95                                filename, err_buf);
96                 return EXIT_FAIL_LIBBPF;
97         }
98         test_walk_progs(bpfobj, verbose);
99         test_walk_maps(bpfobj, verbose);
100
101         if (verbose)
102                 printf("Close BPF ELF-file with libbpf: %s\n",
103                        bpf_object__name(bpfobj));
104         bpf_object__close(bpfobj);
105
106         return 0;
107 }
108
109 int main(int argc, char **argv)
110 {
111         char filename[1024] = { 0 };
112         bool verbose = 1;
113         int longindex = 0;
114         int opt;
115
116         libbpf_set_print(libbpf_debug_print);
117
118         /* Parse commands line args */
119         while ((opt = getopt_long(argc, argv, "hDq",
120                                   long_options, &longindex)) != -1) {
121                 switch (opt) {
122                 case 'D':
123                         debug = 1;
124                         break;
125                 case 'q': /* Use in scripting mode */
126                         verbose = 0;
127                         break;
128                 case 'h':
129                 default:
130                         usage(argv);
131                         return EXIT_FAIL_OPTION;
132                 }
133         }
134         if (optind >= argc) {
135                 usage(argv);
136                 printf("ERROR: Expected BPF_FILE argument after options\n");
137                 return EXIT_FAIL_OPTION;
138         }
139         snprintf(filename, sizeof(filename), "%s", argv[optind]);
140
141         return test_open_file(filename, verbose);
142 }