1*3b2ad502SAndrii Nakryiko // SPDX-License-Identifier: GPL-2.0
2*3b2ad502SAndrii Nakryiko /* Copyright (c) 2021 Facebook */
3*3b2ad502SAndrii Nakryiko
4*3b2ad502SAndrii Nakryiko #include "vmlinux.h"
5*3b2ad502SAndrii Nakryiko #include <bpf/bpf_helpers.h>
6*3b2ad502SAndrii Nakryiko #include <bpf/bpf_tracing.h>
7*3b2ad502SAndrii Nakryiko
8*3b2ad502SAndrii Nakryiko struct my_key { long x; };
9*3b2ad502SAndrii Nakryiko struct my_value { long x; };
10*3b2ad502SAndrii Nakryiko
11*3b2ad502SAndrii Nakryiko struct {
12*3b2ad502SAndrii Nakryiko __uint(type, BPF_MAP_TYPE_HASH);
13*3b2ad502SAndrii Nakryiko __type(key, struct my_key);
14*3b2ad502SAndrii Nakryiko __type(value, struct my_value);
15*3b2ad502SAndrii Nakryiko __uint(max_entries, 16);
16*3b2ad502SAndrii Nakryiko } map1 SEC(".maps");
17*3b2ad502SAndrii Nakryiko
18*3b2ad502SAndrii Nakryiko /* Matches map2 definition in linked_maps2.c. Order of the attributes doesn't
19*3b2ad502SAndrii Nakryiko * matter.
20*3b2ad502SAndrii Nakryiko */
21*3b2ad502SAndrii Nakryiko typedef struct {
22*3b2ad502SAndrii Nakryiko __uint(max_entries, 8);
23*3b2ad502SAndrii Nakryiko __type(key, int);
24*3b2ad502SAndrii Nakryiko __type(value, int);
25*3b2ad502SAndrii Nakryiko __uint(type, BPF_MAP_TYPE_ARRAY);
26*3b2ad502SAndrii Nakryiko } map2_t;
27*3b2ad502SAndrii Nakryiko
28*3b2ad502SAndrii Nakryiko extern map2_t map2 SEC(".maps");
29*3b2ad502SAndrii Nakryiko
30*3b2ad502SAndrii Nakryiko /* This should be the winning map definition, but we have no way of verifying,
31*3b2ad502SAndrii Nakryiko * so we just make sure that it links and works without errors
32*3b2ad502SAndrii Nakryiko */
33*3b2ad502SAndrii Nakryiko struct {
34*3b2ad502SAndrii Nakryiko __uint(type, BPF_MAP_TYPE_ARRAY);
35*3b2ad502SAndrii Nakryiko __type(key, int);
36*3b2ad502SAndrii Nakryiko __type(value, int);
37*3b2ad502SAndrii Nakryiko __uint(max_entries, 16);
38*3b2ad502SAndrii Nakryiko } map_weak __weak SEC(".maps");
39*3b2ad502SAndrii Nakryiko
40*3b2ad502SAndrii Nakryiko int output_first1;
41*3b2ad502SAndrii Nakryiko int output_second1;
42*3b2ad502SAndrii Nakryiko int output_weak1;
43*3b2ad502SAndrii Nakryiko
44*3b2ad502SAndrii Nakryiko SEC("raw_tp/sys_enter")
BPF_PROG(handler_enter1)45*3b2ad502SAndrii Nakryiko int BPF_PROG(handler_enter1)
46*3b2ad502SAndrii Nakryiko {
47*3b2ad502SAndrii Nakryiko /* update values with key = 1 */
48*3b2ad502SAndrii Nakryiko int key = 1, val = 1;
49*3b2ad502SAndrii Nakryiko struct my_key key_struct = { .x = 1 };
50*3b2ad502SAndrii Nakryiko struct my_value val_struct = { .x = 1000 };
51*3b2ad502SAndrii Nakryiko
52*3b2ad502SAndrii Nakryiko bpf_map_update_elem(&map1, &key_struct, &val_struct, 0);
53*3b2ad502SAndrii Nakryiko bpf_map_update_elem(&map2, &key, &val, 0);
54*3b2ad502SAndrii Nakryiko bpf_map_update_elem(&map_weak, &key, &val, 0);
55*3b2ad502SAndrii Nakryiko
56*3b2ad502SAndrii Nakryiko return 0;
57*3b2ad502SAndrii Nakryiko }
58*3b2ad502SAndrii Nakryiko
59*3b2ad502SAndrii Nakryiko SEC("raw_tp/sys_exit")
BPF_PROG(handler_exit1)60*3b2ad502SAndrii Nakryiko int BPF_PROG(handler_exit1)
61*3b2ad502SAndrii Nakryiko {
62*3b2ad502SAndrii Nakryiko /* lookup values with key = 2, set in another file */
63*3b2ad502SAndrii Nakryiko int key = 2, *val;
64*3b2ad502SAndrii Nakryiko struct my_key key_struct = { .x = 2 };
65*3b2ad502SAndrii Nakryiko struct my_value *value_struct;
66*3b2ad502SAndrii Nakryiko
67*3b2ad502SAndrii Nakryiko value_struct = bpf_map_lookup_elem(&map1, &key_struct);
68*3b2ad502SAndrii Nakryiko if (value_struct)
69*3b2ad502SAndrii Nakryiko output_first1 = value_struct->x;
70*3b2ad502SAndrii Nakryiko
71*3b2ad502SAndrii Nakryiko val = bpf_map_lookup_elem(&map2, &key);
72*3b2ad502SAndrii Nakryiko if (val)
73*3b2ad502SAndrii Nakryiko output_second1 = *val;
74*3b2ad502SAndrii Nakryiko
75*3b2ad502SAndrii Nakryiko val = bpf_map_lookup_elem(&map_weak, &key);
76*3b2ad502SAndrii Nakryiko if (val)
77*3b2ad502SAndrii Nakryiko output_weak1 = *val;
78*3b2ad502SAndrii Nakryiko
79*3b2ad502SAndrii Nakryiko return 0;
80*3b2ad502SAndrii Nakryiko }
81*3b2ad502SAndrii Nakryiko
82*3b2ad502SAndrii Nakryiko char LICENSE[] SEC("license") = "GPL";
83