hc
2024-09-20 cf4ce59b3b70238352c7f1729f0f7223214828ad
kernel/samples/bpf/test_map_in_map_kern.c
....@@ -10,65 +10,68 @@
1010 #include <linux/version.h>
1111 #include <uapi/linux/bpf.h>
1212 #include <uapi/linux/in6.h>
13
-#include "bpf_helpers.h"
13
+#include <bpf/bpf_helpers.h>
14
+#include <bpf/bpf_tracing.h>
15
+#include <bpf/bpf_core_read.h>
16
+#include "trace_common.h"
1417
1518 #define MAX_NR_PORTS 65536
1619
1720 /* map #0 */
18
-struct bpf_map_def SEC("maps") port_a = {
19
- .type = BPF_MAP_TYPE_ARRAY,
20
- .key_size = sizeof(u32),
21
- .value_size = sizeof(int),
22
- .max_entries = MAX_NR_PORTS,
23
-};
21
+struct inner_a {
22
+ __uint(type, BPF_MAP_TYPE_ARRAY);
23
+ __type(key, u32);
24
+ __type(value, int);
25
+ __uint(max_entries, MAX_NR_PORTS);
26
+} port_a SEC(".maps");
2427
2528 /* map #1 */
26
-struct bpf_map_def SEC("maps") port_h = {
27
- .type = BPF_MAP_TYPE_HASH,
28
- .key_size = sizeof(u32),
29
- .value_size = sizeof(int),
30
- .max_entries = 1,
31
-};
29
+struct inner_h {
30
+ __uint(type, BPF_MAP_TYPE_HASH);
31
+ __type(key, u32);
32
+ __type(value, int);
33
+ __uint(max_entries, 1);
34
+} port_h SEC(".maps");
3235
3336 /* map #2 */
34
-struct bpf_map_def SEC("maps") reg_result_h = {
35
- .type = BPF_MAP_TYPE_HASH,
36
- .key_size = sizeof(u32),
37
- .value_size = sizeof(int),
38
- .max_entries = 1,
39
-};
37
+struct {
38
+ __uint(type, BPF_MAP_TYPE_HASH);
39
+ __type(key, u32);
40
+ __type(value, int);
41
+ __uint(max_entries, 1);
42
+} reg_result_h SEC(".maps");
4043
4144 /* map #3 */
42
-struct bpf_map_def SEC("maps") inline_result_h = {
43
- .type = BPF_MAP_TYPE_HASH,
44
- .key_size = sizeof(u32),
45
- .value_size = sizeof(int),
46
- .max_entries = 1,
47
-};
45
+struct {
46
+ __uint(type, BPF_MAP_TYPE_HASH);
47
+ __type(key, u32);
48
+ __type(value, int);
49
+ __uint(max_entries, 1);
50
+} inline_result_h SEC(".maps");
4851
4952 /* map #4 */ /* Test case #0 */
50
-struct bpf_map_def SEC("maps") a_of_port_a = {
51
- .type = BPF_MAP_TYPE_ARRAY_OF_MAPS,
52
- .key_size = sizeof(u32),
53
- .inner_map_idx = 0, /* map_fd[0] is port_a */
54
- .max_entries = MAX_NR_PORTS,
55
-};
53
+struct {
54
+ __uint(type, BPF_MAP_TYPE_ARRAY_OF_MAPS);
55
+ __uint(max_entries, MAX_NR_PORTS);
56
+ __uint(key_size, sizeof(u32));
57
+ __array(values, struct inner_a); /* use inner_a as inner map */
58
+} a_of_port_a SEC(".maps");
5659
5760 /* map #5 */ /* Test case #1 */
58
-struct bpf_map_def SEC("maps") h_of_port_a = {
59
- .type = BPF_MAP_TYPE_HASH_OF_MAPS,
60
- .key_size = sizeof(u32),
61
- .inner_map_idx = 0, /* map_fd[0] is port_a */
62
- .max_entries = 1,
63
-};
61
+struct {
62
+ __uint(type, BPF_MAP_TYPE_HASH_OF_MAPS);
63
+ __uint(max_entries, 1);
64
+ __uint(key_size, sizeof(u32));
65
+ __array(values, struct inner_a); /* use inner_a as inner map */
66
+} h_of_port_a SEC(".maps");
6467
6568 /* map #6 */ /* Test case #2 */
66
-struct bpf_map_def SEC("maps") h_of_port_h = {
67
- .type = BPF_MAP_TYPE_HASH_OF_MAPS,
68
- .key_size = sizeof(u32),
69
- .inner_map_idx = 1, /* map_fd[1] is port_h */
70
- .max_entries = 1,
71
-};
69
+struct {
70
+ __uint(type, BPF_MAP_TYPE_HASH_OF_MAPS);
71
+ __uint(max_entries, 1);
72
+ __uint(key_size, sizeof(u32));
73
+ __array(values, struct inner_h); /* use inner_h as inner map */
74
+} h_of_port_h SEC(".maps");
7275
7376 static __always_inline int do_reg_lookup(void *inner_map, u32 port)
7477 {
....@@ -100,7 +103,7 @@
100103 return result ? *result : -ENOENT;
101104 }
102105
103
-SEC("kprobe/sys_connect")
106
+SEC("kprobe/__sys_connect")
104107 int trace_sys_connect(struct pt_regs *ctx)
105108 {
106109 struct sockaddr_in6 *in6;
....@@ -110,13 +113,13 @@
110113 void *outer_map, *inner_map;
111114 bool inline_hash = false;
112115
113
- in6 = (struct sockaddr_in6 *)PT_REGS_PARM2(ctx);
114
- addrlen = (int)PT_REGS_PARM3(ctx);
116
+ in6 = (struct sockaddr_in6 *)PT_REGS_PARM2_CORE(ctx);
117
+ addrlen = (int)PT_REGS_PARM3_CORE(ctx);
115118
116119 if (addrlen != sizeof(*in6))
117120 return 0;
118121
119
- ret = bpf_probe_read(dst6, sizeof(dst6), &in6->sin6_addr);
122
+ ret = bpf_probe_read_user(dst6, sizeof(dst6), &in6->sin6_addr);
120123 if (ret) {
121124 inline_ret = ret;
122125 goto done;
....@@ -127,7 +130,7 @@
127130
128131 test_case = dst6[7];
129132
130
- ret = bpf_probe_read(&port, sizeof(port), &in6->sin6_port);
133
+ ret = bpf_probe_read_user(&port, sizeof(port), &in6->sin6_port);
131134 if (ret) {
132135 inline_ret = ret;
133136 goto done;