Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

at v5.0-rc7 77 lines 1.8 kB view raw
1// SPDX-License-Identifier: GPL-2.0 2#include <linux/bpf.h> 3#include <linux/version.h> 4 5#include "bpf_helpers.h" 6#include "netcnt_common.h" 7 8#define MAX_BPS (3 * 1024 * 1024) 9 10#define REFRESH_TIME_NS 100000000 11#define NS_PER_SEC 1000000000 12 13struct bpf_map_def SEC("maps") percpu_netcnt = { 14 .type = BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE, 15 .key_size = sizeof(struct bpf_cgroup_storage_key), 16 .value_size = sizeof(struct percpu_net_cnt), 17}; 18 19BPF_ANNOTATE_KV_PAIR(percpu_netcnt, struct bpf_cgroup_storage_key, 20 struct percpu_net_cnt); 21 22struct bpf_map_def SEC("maps") netcnt = { 23 .type = BPF_MAP_TYPE_CGROUP_STORAGE, 24 .key_size = sizeof(struct bpf_cgroup_storage_key), 25 .value_size = sizeof(struct net_cnt), 26}; 27 28BPF_ANNOTATE_KV_PAIR(netcnt, struct bpf_cgroup_storage_key, 29 struct net_cnt); 30 31SEC("cgroup/skb") 32int bpf_nextcnt(struct __sk_buff *skb) 33{ 34 struct percpu_net_cnt *percpu_cnt; 35 char fmt[] = "%d %llu %llu\n"; 36 struct net_cnt *cnt; 37 __u64 ts, dt; 38 int ret; 39 40 cnt = bpf_get_local_storage(&netcnt, 0); 41 percpu_cnt = bpf_get_local_storage(&percpu_netcnt, 0); 42 43 percpu_cnt->packets++; 44 percpu_cnt->bytes += skb->len; 45 46 if (percpu_cnt->packets > MAX_PERCPU_PACKETS) { 47 __sync_fetch_and_add(&cnt->packets, 48 percpu_cnt->packets); 49 percpu_cnt->packets = 0; 50 51 __sync_fetch_and_add(&cnt->bytes, 52 percpu_cnt->bytes); 53 percpu_cnt->bytes = 0; 54 } 55 56 ts = bpf_ktime_get_ns(); 57 dt = ts - percpu_cnt->prev_ts; 58 59 dt *= MAX_BPS; 60 dt /= NS_PER_SEC; 61 62 if (cnt->bytes + percpu_cnt->bytes - percpu_cnt->prev_bytes < dt) 63 ret = 1; 64 else 65 ret = 0; 66 67 if (dt > REFRESH_TIME_NS) { 68 percpu_cnt->prev_ts = ts; 69 percpu_cnt->prev_packets = cnt->packets; 70 percpu_cnt->prev_bytes = cnt->bytes; 71 } 72 73 return !!ret; 74} 75 76char _license[] SEC("license") = "GPL"; 77__u32 _version SEC("version") = LINUX_VERSION_CODE;