xref: /linux/tools/testing/selftests/bpf/progs/timer_mim_reject.c (revision 2beb1b31a12b57e19cd5c82ea6d54e56520605e8)
1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (c) 2021 Facebook */
3 #include <linux/bpf.h>
4 #include <time.h>
5 #include <errno.h>
6 #include <bpf/bpf_helpers.h>
7 #include <bpf/bpf_tracing.h>
8 
9 char _license[] SEC("license") = "GPL";
10 struct hmap_elem {
11 	int pad; /* unused */
12 	struct bpf_timer timer;
13 };
14 
15 struct inner_map {
16 	__uint(type, BPF_MAP_TYPE_HASH);
17 	__uint(max_entries, 1024);
18 	__type(key, int);
19 	__type(value, struct hmap_elem);
20 } inner_htab SEC(".maps");
21 
22 #define ARRAY_KEY 1
23 #define ARRAY_KEY2 2
24 #define HASH_KEY 1234
25 
26 struct outer_arr {
27 	__uint(type, BPF_MAP_TYPE_ARRAY_OF_MAPS);
28 	__uint(max_entries, 2);
29 	__uint(key_size, sizeof(int));
30 	__uint(value_size, sizeof(int));
31 	__array(values, struct inner_map);
32 } outer_arr SEC(".maps") = {
33 	.values = { [ARRAY_KEY] = &inner_htab },
34 };
35 
36 __u64 err;
37 __u64 ok;
38 __u64 cnt;
39 
40 /* callback for inner hash map */
timer_cb(void * map,int * key,struct hmap_elem * val)41 static int timer_cb(void *map, int *key, struct hmap_elem *val)
42 {
43 	return 0;
44 }
45 
46 SEC("?fentry/bpf_fentry_test1")
BPF_PROG(test1,int a)47 int BPF_PROG(test1, int a)
48 {
49 	struct hmap_elem init = {};
50 	struct bpf_map *inner_map, *inner_map2;
51 	struct hmap_elem *val;
52 	int array_key = ARRAY_KEY;
53 	int array_key2 = ARRAY_KEY2;
54 	int hash_key = HASH_KEY;
55 
56 	inner_map = bpf_map_lookup_elem(&outer_arr, &array_key);
57 	if (!inner_map)
58 		return 0;
59 
60 	inner_map2 = bpf_map_lookup_elem(&outer_arr, &array_key2);
61 	if (!inner_map2)
62 		return 0;
63 	bpf_map_update_elem(inner_map, &hash_key, &init, 0);
64 	val = bpf_map_lookup_elem(inner_map, &hash_key);
65 	if (!val)
66 		return 0;
67 
68 	bpf_timer_init(&val->timer, inner_map2, CLOCK_MONOTONIC);
69 	if (bpf_timer_set_callback(&val->timer, timer_cb))
70 		err |= 4;
71 	if (bpf_timer_start(&val->timer, 0, 0))
72 		err |= 8;
73 	return 0;
74 }
75 
76 struct callback_ctx {
77 	void *map;
78 };
79 
mismatch_iter_cb(void * map,int * key,struct hmap_elem * val,struct callback_ctx * ctx)80 static int mismatch_iter_cb(void *map, int *key, struct hmap_elem *val, struct callback_ctx *ctx)
81 {
82 	bpf_timer_init(&val->timer, ctx->map, CLOCK_MONOTONIC);
83 	return 0;
84 }
85 
timer_mismatch_cb(void * map,int * key,struct hmap_elem * val)86 static int timer_mismatch_cb(void *map, int *key, struct hmap_elem *val)
87 {
88 	struct callback_ctx ctx = { .map = map };
89 	struct bpf_map *inner_map2;
90 	int array_key2 = ARRAY_KEY2;
91 
92 	inner_map2 = bpf_map_lookup_elem(&outer_arr, &array_key2);
93 	if (!inner_map2)
94 		return 0;
95 	bpf_for_each_map_elem(inner_map2, mismatch_iter_cb, &ctx, 0);
96 	return 0;
97 }
98 
match_iter_cb(void * map,int * key,struct hmap_elem * val,struct callback_ctx * ctx)99 static int match_iter_cb(void *map, int *key, struct hmap_elem *val, struct callback_ctx *ctx)
100 {
101 	bpf_timer_init(&val->timer, map, CLOCK_MONOTONIC);
102 	return 0;
103 }
104 
timer_match_cb(void * map,int * key,struct hmap_elem * val)105 static int timer_match_cb(void *map, int *key, struct hmap_elem *val)
106 {
107 	struct callback_ctx ctx = {};
108 	struct bpf_map *inner_map2;
109 	int array_key2 = ARRAY_KEY2;
110 
111 	inner_map2 = bpf_map_lookup_elem(&outer_arr, &array_key2);
112 	if (!inner_map2)
113 		return 0;
114 	bpf_for_each_map_elem(inner_map2, match_iter_cb, &ctx, 0);
115 	return 0;
116 }
117 
118 SEC("?fentry/bpf_fentry_test1")
BPF_PROG(callback_map_uid_mismatch,int a)119 int BPF_PROG(callback_map_uid_mismatch, int a)
120 {
121 	struct hmap_elem *val;
122 	struct bpf_map *inner_map;
123 	int array_key = ARRAY_KEY;
124 	int hash_key = HASH_KEY;
125 
126 	inner_map = bpf_map_lookup_elem(&outer_arr, &array_key);
127 	if (!inner_map)
128 		return 0;
129 	val = bpf_map_lookup_elem(inner_map, &hash_key);
130 	if (!val)
131 		return 0;
132 
133 	bpf_timer_init(&val->timer, inner_map, CLOCK_MONOTONIC);
134 	bpf_timer_set_callback(&val->timer, timer_mismatch_cb);
135 	return 0;
136 }
137 
138 SEC("?fentry/bpf_fentry_test1")
BPF_PROG(callback_map_uid_match,int a)139 int BPF_PROG(callback_map_uid_match, int a)
140 {
141 	struct hmap_elem *val;
142 	struct bpf_map *inner_map;
143 	int array_key = ARRAY_KEY;
144 	int hash_key = HASH_KEY;
145 
146 	inner_map = bpf_map_lookup_elem(&outer_arr, &array_key);
147 	if (!inner_map)
148 		return 0;
149 	val = bpf_map_lookup_elem(inner_map, &hash_key);
150 	if (!val)
151 		return 0;
152 
153 	bpf_timer_init(&val->timer, inner_map, CLOCK_MONOTONIC);
154 	bpf_timer_set_callback(&val->timer, timer_match_cb);
155 	return 0;
156 }
157