~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/tools/testing/selftests/bpf/progs/netcnt_prog.c

Version: ~ [ linux-6.11.5 ] ~ [ linux-6.10.14 ] ~ [ linux-6.9.12 ] ~ [ linux-6.8.12 ] ~ [ linux-6.7.12 ] ~ [ linux-6.6.58 ] ~ [ linux-6.5.13 ] ~ [ linux-6.4.16 ] ~ [ linux-6.3.13 ] ~ [ linux-6.2.16 ] ~ [ linux-6.1.114 ] ~ [ linux-6.0.19 ] ~ [ linux-5.19.17 ] ~ [ linux-5.18.19 ] ~ [ linux-5.17.15 ] ~ [ linux-5.16.20 ] ~ [ linux-5.15.169 ] ~ [ linux-5.14.21 ] ~ [ linux-5.13.19 ] ~ [ linux-5.12.19 ] ~ [ linux-5.11.22 ] ~ [ linux-5.10.228 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.284 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.322 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.336 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.337 ] ~ [ linux-4.4.302 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.9 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

  1 // SPDX-License-Identifier: GPL-2.0
  2 #include <linux/bpf.h>
  3 #include <linux/version.h>
  4 
  5 #include <bpf/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 
 13 struct {
 14         __uint(type, BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE);
 15         __type(key, struct bpf_cgroup_storage_key);
 16         __type(value, union percpu_net_cnt);
 17 } percpu_netcnt SEC(".maps");
 18 
 19 struct {
 20         __uint(type, BPF_MAP_TYPE_CGROUP_STORAGE);
 21         __type(key, struct bpf_cgroup_storage_key);
 22         __type(value, union net_cnt);
 23 } netcnt SEC(".maps");
 24 
 25 SEC("cgroup/skb")
 26 int bpf_nextcnt(struct __sk_buff *skb)
 27 {
 28         union percpu_net_cnt *percpu_cnt;
 29         union net_cnt *cnt;
 30         __u64 ts, dt;
 31         int ret;
 32 
 33         cnt = bpf_get_local_storage(&netcnt, 0);
 34         percpu_cnt = bpf_get_local_storage(&percpu_netcnt, 0);
 35 
 36         percpu_cnt->packets++;
 37         percpu_cnt->bytes += skb->len;
 38 
 39         if (percpu_cnt->packets > MAX_PERCPU_PACKETS) {
 40                 __sync_fetch_and_add(&cnt->packets,
 41                                      percpu_cnt->packets);
 42                 percpu_cnt->packets = 0;
 43 
 44                 __sync_fetch_and_add(&cnt->bytes,
 45                                      percpu_cnt->bytes);
 46                 percpu_cnt->bytes = 0;
 47         }
 48 
 49         ts = bpf_ktime_get_ns();
 50         dt = ts - percpu_cnt->prev_ts;
 51 
 52         dt *= MAX_BPS;
 53         dt /= NS_PER_SEC;
 54 
 55         if (cnt->bytes + percpu_cnt->bytes - percpu_cnt->prev_bytes < dt)
 56                 ret = 1;
 57         else
 58                 ret = 0;
 59 
 60         if (dt > REFRESH_TIME_NS) {
 61                 percpu_cnt->prev_ts = ts;
 62                 percpu_cnt->prev_packets = cnt->packets;
 63                 percpu_cnt->prev_bytes = cnt->bytes;
 64         }
 65 
 66         return !!ret;
 67 }
 68 
 69 char _license[] SEC("license") = "GPL";
 70 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

sflogo.php