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
9char _license[] SEC("license") = "GPL";
10struct hmap_elem {
11	int pad; /* unused */
12	struct bpf_timer timer;
13};
14
15struct 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
26struct 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 */
41static int timer_cb(void *map, int *key, struct hmap_elem *val)
42{
43	return 0;
44}
45
46SEC("fentry/bpf_fentry_test1")
47int 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