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 ARRAY_KEY2 2
0024 #define HASH_KEY 1234
0025 
0026 struct outer_arr {
0027     __uint(type, BPF_MAP_TYPE_ARRAY_OF_MAPS);
0028     __uint(max_entries, 2);
0029     __uint(key_size, sizeof(int));
0030     __uint(value_size, sizeof(int));
0031     __array(values, struct inner_map);
0032 } outer_arr SEC(".maps") = {
0033     .values = { [ARRAY_KEY] = &inner_htab },
0034 };
0035 
0036 __u64 err;
0037 __u64 ok;
0038 __u64 cnt;
0039 
0040 /* callback for inner hash map */
0041 static int timer_cb(void *map, int *key, struct hmap_elem *val)
0042 {
0043     return 0;
0044 }
0045 
0046 SEC("fentry/bpf_fentry_test1")
0047 int BPF_PROG(test1, int a)
0048 {
0049     struct hmap_elem init = {};
0050     struct bpf_map *inner_map, *inner_map2;
0051     struct hmap_elem *val;
0052     int array_key = ARRAY_KEY;
0053     int array_key2 = ARRAY_KEY2;
0054     int hash_key = HASH_KEY;
0055 
0056     inner_map = bpf_map_lookup_elem(&outer_arr, &array_key);
0057     if (!inner_map)
0058         return 0;
0059 
0060     inner_map2 = bpf_map_lookup_elem(&outer_arr, &array_key2);
0061     if (!inner_map2)
0062         return 0;
0063     bpf_map_update_elem(inner_map, &hash_key, &init, 0);
0064     val = bpf_map_lookup_elem(inner_map, &hash_key);
0065     if (!val)
0066         return 0;
0067 
0068     bpf_timer_init(&val->timer, inner_map2, CLOCK_MONOTONIC);
0069     if (bpf_timer_set_callback(&val->timer, timer_cb))
0070         err |= 4;
0071     if (bpf_timer_start(&val->timer, 0, 0))
0072         err |= 8;
0073     return 0;
0074 }