Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /* Copyright (c) 2021 Facebook */
0003 #include <linux/bpf.h>
0004 #include <time.h>
0005 #include <errno.h>
0006 #include <bpf/bpf_helpers.h>
0007 #include "bpf_tcp_helpers.h"
0008 
0009 char _license[] SEC("license") = "GPL";
0010 struct hmap_elem {
0011     int pad; /* unused */
0012     struct bpf_timer timer;
0013 };
0014 
0015 struct inner_map {
0016     __uint(type, BPF_MAP_TYPE_HASH);
0017     __uint(max_entries, 1024);
0018     __type(key, int);
0019     __type(value, struct hmap_elem);
0020 } inner_htab SEC(".maps");
0021 
0022 #define ARRAY_KEY 1
0023 #define HASH_KEY 1234
0024 
0025 struct outer_arr {
0026     __uint(type, BPF_MAP_TYPE_ARRAY_OF_MAPS);
0027     __uint(max_entries, 2);
0028     __uint(key_size, sizeof(int));
0029     __uint(value_size, sizeof(int));
0030     __array(values, struct inner_map);
0031 } outer_arr SEC(".maps") = {
0032     .values = { [ARRAY_KEY] = &inner_htab },
0033 };
0034 
0035 __u64 err;
0036 __u64 ok;
0037 __u64 cnt;
0038 
0039 static int timer_cb1(void *map, int *key, struct hmap_elem *val);
0040 
0041 static int timer_cb2(void *map, int *key, struct hmap_elem *val)
0042 {
0043     cnt++;
0044     bpf_timer_set_callback(&val->timer, timer_cb1);
0045     if (bpf_timer_start(&val->timer, 1000, 0))
0046         err |= 1;
0047     ok |= 1;
0048     return 0;
0049 }
0050 
0051 /* callback for inner hash map */
0052 static int timer_cb1(void *map, int *key, struct hmap_elem *val)
0053 {
0054     cnt++;
0055     bpf_timer_set_callback(&val->timer, timer_cb2);
0056     if (bpf_timer_start(&val->timer, 1000, 0))
0057         err |= 2;
0058     /* Do a lookup to make sure 'map' and 'key' pointers are correct */
0059     bpf_map_lookup_elem(map, key);
0060     ok |= 2;
0061     return 0;
0062 }
0063 
0064 SEC("fentry/bpf_fentry_test1")
0065 int BPF_PROG(test1, int a)
0066 {
0067     struct hmap_elem init = {};
0068     struct bpf_map *inner_map;
0069     struct hmap_elem *val;
0070     int array_key = ARRAY_KEY;
0071     int hash_key = HASH_KEY;
0072 
0073     inner_map = bpf_map_lookup_elem(&outer_arr, &array_key);
0074     if (!inner_map)
0075         return 0;
0076 
0077     bpf_map_update_elem(inner_map, &hash_key, &init, 0);
0078     val = bpf_map_lookup_elem(inner_map, &hash_key);
0079     if (!val)
0080         return 0;
0081 
0082     bpf_timer_init(&val->timer, inner_map, CLOCK_MONOTONIC);
0083     if (bpf_timer_set_callback(&val->timer, timer_cb1))
0084         err |= 4;
0085     if (bpf_timer_start(&val->timer, 0, 0))
0086         err |= 8;
0087     return 0;
0088 }