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_tcp_helpers.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 HASH_KEY 1234 24 25 struct outer_arr { 26 __uint(type, BPF_MAP_TYPE_ARRAY_OF_MAPS); 27 __uint(max_entries, 2); 28 __uint(key_size, sizeof(int)); 29 __uint(value_size, sizeof(int)); 30 __array(values, struct inner_map); 31 } outer_arr SEC(".maps") = { 32 .values = { [ARRAY_KEY] = &inner_htab }, 33 }; 34 35 __u64 err; 36 __u64 ok; 37 __u64 cnt; 38 39 static int timer_cb1(void *map, int *key, struct hmap_elem *val); 40 41 static int timer_cb2(void *map, int *key, struct hmap_elem *val) 42 { 43 cnt++; 44 bpf_timer_set_callback(&val->timer, timer_cb1); 45 if (bpf_timer_start(&val->timer, 1000, 0)) 46 err |= 1; 47 ok |= 1; 48 return 0; 49 } 50 51 /* callback for inner hash map */ 52 static int timer_cb1(void *map, int *key, struct hmap_elem *val) 53 { 54 cnt++; 55 bpf_timer_set_callback(&val->timer, timer_cb2); 56 if (bpf_timer_start(&val->timer, 1000, 0)) 57 err |= 2; 58 /* Do a lookup to make sure 'map' and 'key' pointers are correct */ 59 bpf_map_lookup_elem(map, key); 60 ok |= 2; 61 return 0; 62 } 63 64 SEC("fentry/bpf_fentry_test1") 65 int BPF_PROG(test1, int a) 66 { 67 struct hmap_elem init = {}; 68 struct bpf_map *inner_map; 69 struct hmap_elem *val; 70 int array_key = ARRAY_KEY; 71 int hash_key = HASH_KEY; 72 73 inner_map = bpf_map_lookup_elem(&outer_arr, &array_key); 74 if (!inner_map) 75 return 0; 76 77 bpf_map_update_elem(inner_map, &hash_key, &init, 0); 78 val = bpf_map_lookup_elem(inner_map, &hash_key); 79 if (!val) 80 return 0; 81 82 bpf_timer_init(&val->timer, inner_map, CLOCK_MONOTONIC); 83 if (bpf_timer_set_callback(&val->timer, timer_cb1)) 84 err |= 4; 85 if (bpf_timer_start(&val->timer, 0, 0)) 86 err |= 8; 87 return 0; 88 } 89