hc
2024-05-10 37f49e37ab4cb5d0bc4c60eb5c6d4dd57db767bb
kernel/samples/bpf/test_map_in_map_user.c
....@@ -1,9 +1,6 @@
1
+// SPDX-License-Identifier: GPL-2.0-only
12 /*
23 * Copyright (c) 2017 Facebook
3
- *
4
- * This program is free software; you can redistribute it and/or
5
- * modify it under the terms of version 2 of the GNU General Public
6
- * License as published by the Free Software Foundation.
74 */
85 #include <sys/resource.h>
96 #include <sys/socket.h>
....@@ -14,7 +11,9 @@
1411 #include <stdlib.h>
1512 #include <stdio.h>
1613 #include <bpf/bpf.h>
17
-#include "bpf_load.h"
14
+#include <bpf/libbpf.h>
15
+
16
+static int map_fd[7];
1817
1918 #define PORT_A (map_fd[0])
2019 #define PORT_H (map_fd[1])
....@@ -116,18 +115,59 @@
116115 int main(int argc, char **argv)
117116 {
118117 struct rlimit r = {RLIM_INFINITY, RLIM_INFINITY};
118
+ struct bpf_link *link = NULL;
119
+ struct bpf_program *prog;
120
+ struct bpf_object *obj;
119121 char filename[256];
120122
121
- assert(!setrlimit(RLIMIT_MEMLOCK, &r));
123
+ if (setrlimit(RLIMIT_MEMLOCK, &r)) {
124
+ perror("setrlimit(RLIMIT_MEMLOCK)");
125
+ return 1;
126
+ }
122127
123128 snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]);
129
+ obj = bpf_object__open_file(filename, NULL);
130
+ if (libbpf_get_error(obj)) {
131
+ fprintf(stderr, "ERROR: opening BPF object file failed\n");
132
+ return 0;
133
+ }
124134
125
- if (load_bpf_file(filename)) {
126
- printf("%s", bpf_log_buf);
127
- return 1;
135
+ prog = bpf_object__find_program_by_name(obj, "trace_sys_connect");
136
+ if (!prog) {
137
+ printf("finding a prog in obj file failed\n");
138
+ goto cleanup;
139
+ }
140
+
141
+ /* load BPF program */
142
+ if (bpf_object__load(obj)) {
143
+ fprintf(stderr, "ERROR: loading BPF object file failed\n");
144
+ goto cleanup;
145
+ }
146
+
147
+ map_fd[0] = bpf_object__find_map_fd_by_name(obj, "port_a");
148
+ map_fd[1] = bpf_object__find_map_fd_by_name(obj, "port_h");
149
+ map_fd[2] = bpf_object__find_map_fd_by_name(obj, "reg_result_h");
150
+ map_fd[3] = bpf_object__find_map_fd_by_name(obj, "inline_result_h");
151
+ map_fd[4] = bpf_object__find_map_fd_by_name(obj, "a_of_port_a");
152
+ map_fd[5] = bpf_object__find_map_fd_by_name(obj, "h_of_port_a");
153
+ map_fd[6] = bpf_object__find_map_fd_by_name(obj, "h_of_port_h");
154
+ if (map_fd[0] < 0 || map_fd[1] < 0 || map_fd[2] < 0 ||
155
+ map_fd[3] < 0 || map_fd[4] < 0 || map_fd[5] < 0 || map_fd[6] < 0) {
156
+ fprintf(stderr, "ERROR: finding a map in obj file failed\n");
157
+ goto cleanup;
158
+ }
159
+
160
+ link = bpf_program__attach(prog);
161
+ if (libbpf_get_error(link)) {
162
+ fprintf(stderr, "ERROR: bpf_program__attach failed\n");
163
+ link = NULL;
164
+ goto cleanup;
128165 }
129166
130167 test_map_in_map();
131168
169
+cleanup:
170
+ bpf_link__destroy(link);
171
+ bpf_object__close(obj);
132172 return 0;
133173 }