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