1 // SPDX-License-Identifier: GPL-2.0
2 // Copyright (c) 2019 Facebook
4 #include <linux/version.h>
5 #include "bpf_helpers.h"
9 struct bpf_spin_lock lock
;
14 __uint(type
, BPF_MAP_TYPE_HASH
);
15 __uint(max_entries
, 1);
17 __type(value
, struct hmap_elem
);
21 struct bpf_spin_lock lock
;
26 __uint(type
, BPF_MAP_TYPE_CGROUP_STORAGE
);
27 __type(key
, struct bpf_cgroup_storage_key
);
28 __type(value
, struct cls_elem
);
29 } cls_map
SEC(".maps");
32 struct bpf_spin_lock lock
;
34 unsigned long long lasttime
;
40 __uint(type
, BPF_MAP_TYPE_ARRAY
);
41 __uint(max_entries
, 1);
43 __type(value
, struct bpf_vqueue
);
44 } vqueue
SEC(".maps");
46 #define CREDIT_PER_NS(delta, rate) (((delta) * rate) >> 20)
49 int bpf_sping_lock_test(struct __sk_buff
*skb
)
51 volatile int credit
= 0, max_credit
= 100, pkt_len
= 64;
52 struct hmap_elem zero
= {}, *val
;
53 unsigned long long curtime
;
59 val
= bpf_map_lookup_elem(&hmap
, &key
);
61 bpf_map_update_elem(&hmap
, &key
, &zero
, 0);
62 val
= bpf_map_lookup_elem(&hmap
, &key
);
68 /* spin_lock in hash map run time test */
69 bpf_spin_lock(&val
->lock
);
74 if (val
->cnt
!= 0 && val
->cnt
!= 1)
76 bpf_spin_unlock(&val
->lock
);
78 /* spin_lock in array. virtual queue demo */
79 q
= bpf_map_lookup_elem(&vqueue
, &key
);
82 curtime
= bpf_ktime_get_ns();
83 bpf_spin_lock(&q
->lock
);
84 q
->credit
+= CREDIT_PER_NS(curtime
- q
->lasttime
, q
->rate
);
85 q
->lasttime
= curtime
;
86 if (q
->credit
> max_credit
)
87 q
->credit
= max_credit
;
90 bpf_spin_unlock(&q
->lock
);
92 /* spin_lock in cgroup local storage */
93 cls
= bpf_get_local_storage(&cls_map
, 0);
94 bpf_spin_lock(&cls
->lock
);
96 bpf_spin_unlock(&cls
->lock
);
101 char _license
[] SEC("license") = "GPL";