hc
2024-09-20 cf4ce59b3b70238352c7f1729f0f7223214828ad
kernel/samples/bpf/tracex2_user.c
....@@ -3,16 +3,18 @@
33 #include <unistd.h>
44 #include <stdlib.h>
55 #include <signal.h>
6
-#include <linux/bpf.h>
76 #include <string.h>
87 #include <sys/resource.h>
98
109 #include <bpf/bpf.h>
11
-#include "bpf_load.h"
10
+#include <bpf/libbpf.h>
1211 #include "bpf_util.h"
1312
1413 #define MAX_INDEX 64
1514 #define MAX_STARS 38
15
+
16
+/* my_map, my_hist_map */
17
+static int map_fd[2];
1618
1719 static void stars(char *str, long val, long max, int width)
1820 {
....@@ -114,33 +116,59 @@
114116
115117 int main(int ac, char **argv)
116118 {
117
- struct rlimit r = {1024*1024, RLIM_INFINITY};
118
- char filename[256];
119
+ struct rlimit r = {RLIM_INFINITY, RLIM_INFINITY};
119120 long key, next_key, value;
121
+ struct bpf_link *links[2];
122
+ struct bpf_program *prog;
123
+ struct bpf_object *obj;
124
+ char filename[256];
125
+ int i, j = 0;
120126 FILE *f;
121
- int i;
122
-
123
- snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]);
124127
125128 if (setrlimit(RLIMIT_MEMLOCK, &r)) {
126129 perror("setrlimit(RLIMIT_MEMLOCK)");
127130 return 1;
128131 }
129132
133
+ snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]);
134
+ obj = bpf_object__open_file(filename, NULL);
135
+ if (libbpf_get_error(obj)) {
136
+ fprintf(stderr, "ERROR: opening BPF object file failed\n");
137
+ return 0;
138
+ }
139
+
140
+ /* load BPF program */
141
+ if (bpf_object__load(obj)) {
142
+ fprintf(stderr, "ERROR: loading BPF object file failed\n");
143
+ goto cleanup;
144
+ }
145
+
146
+ map_fd[0] = bpf_object__find_map_fd_by_name(obj, "my_map");
147
+ map_fd[1] = bpf_object__find_map_fd_by_name(obj, "my_hist_map");
148
+ if (map_fd[0] < 0 || map_fd[1] < 0) {
149
+ fprintf(stderr, "ERROR: finding a map in obj file failed\n");
150
+ goto cleanup;
151
+ }
152
+
130153 signal(SIGINT, int_exit);
131154 signal(SIGTERM, int_exit);
132155
133156 /* start 'ping' in the background to have some kfree_skb events */
134
- f = popen("ping -c5 localhost", "r");
157
+ f = popen("ping -4 -c5 localhost", "r");
135158 (void) f;
136159
137160 /* start 'dd' in the background to have plenty of 'write' syscalls */
138161 f = popen("dd if=/dev/zero of=/dev/null count=5000000", "r");
139162 (void) f;
140163
141
- if (load_bpf_file(filename)) {
142
- printf("%s", bpf_log_buf);
143
- return 1;
164
+ bpf_object__for_each_program(prog, obj) {
165
+ links[j] = bpf_program__attach(prog);
166
+ if (libbpf_get_error(links[j])) {
167
+ fprintf(stderr, "ERROR: bpf_program__attach failed\n");
168
+ links[j] = NULL;
169
+ goto cleanup;
170
+ }
171
+ j++;
144172 }
145173
146174 for (i = 0; i < 5; i++) {
....@@ -156,5 +184,10 @@
156184 }
157185 print_hist(map_fd[1]);
158186
187
+cleanup:
188
+ for (j--; j >= 0; j--)
189
+ bpf_link__destroy(links[j]);
190
+
191
+ bpf_object__close(obj);
159192 return 0;
160193 }