staging: rtl8192u: remove redundant assignment to pointer crypt
[linux/fpc-iii.git] / tools / testing / selftests / bpf / progs / netcnt_prog.c
blob38a997852cadb2cad2e094b8415d7527a2d0ab80
1 // SPDX-License-Identifier: GPL-2.0
2 #include <linux/bpf.h>
3 #include <linux/version.h>
5 #include "bpf_helpers.h"
6 #include "netcnt_common.h"
8 #define MAX_BPS (3 * 1024 * 1024)
10 #define REFRESH_TIME_NS 100000000
11 #define NS_PER_SEC 1000000000
13 struct {
14 __uint(type, BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE);
15 __type(key, struct bpf_cgroup_storage_key);
16 __type(value, struct percpu_net_cnt);
17 } percpu_netcnt SEC(".maps");
19 struct {
20 __uint(type, BPF_MAP_TYPE_CGROUP_STORAGE);
21 __type(key, struct bpf_cgroup_storage_key);
22 __type(value, struct net_cnt);
23 } netcnt SEC(".maps");
25 SEC("cgroup/skb")
26 int bpf_nextcnt(struct __sk_buff *skb)
28 struct percpu_net_cnt *percpu_cnt;
29 char fmt[] = "%d %llu %llu\n";
30 struct net_cnt *cnt;
31 __u64 ts, dt;
32 int ret;
34 cnt = bpf_get_local_storage(&netcnt, 0);
35 percpu_cnt = bpf_get_local_storage(&percpu_netcnt, 0);
37 percpu_cnt->packets++;
38 percpu_cnt->bytes += skb->len;
40 if (percpu_cnt->packets > MAX_PERCPU_PACKETS) {
41 __sync_fetch_and_add(&cnt->packets,
42 percpu_cnt->packets);
43 percpu_cnt->packets = 0;
45 __sync_fetch_and_add(&cnt->bytes,
46 percpu_cnt->bytes);
47 percpu_cnt->bytes = 0;
50 ts = bpf_ktime_get_ns();
51 dt = ts - percpu_cnt->prev_ts;
53 dt *= MAX_BPS;
54 dt /= NS_PER_SEC;
56 if (cnt->bytes + percpu_cnt->bytes - percpu_cnt->prev_bytes < dt)
57 ret = 1;
58 else
59 ret = 0;
61 if (dt > REFRESH_TIME_NS) {
62 percpu_cnt->prev_ts = ts;
63 percpu_cnt->prev_packets = cnt->packets;
64 percpu_cnt->prev_bytes = cnt->bytes;
67 return !!ret;
70 char _license[] SEC("license") = "GPL";
71 __u32 _version SEC("version") = LINUX_VERSION_CODE;