aboutsummaryrefslogtreecommitdiff
path: root/ifstatd.py
blob: a6b4acc4935bf54d876f20aeb268efc5fe90c674 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#!/usr/bin/env python
#
# xdp_redirect_cpu.py Redirect the incoming packet to the specific CPU
#
# Copyright (c) 2018 Gary Lin
# Licensed under the Apache License, Version 2.0 (the "License")

from bcc import BPF
import time
import sys

flags = 0
def usage():
    print("Usage: {0} <in ifdev>".format(sys.argv[0]))
    print("e.g.: {0} eth0\n".format(sys.argv[0]))
    exit(1)

if len(sys.argv) != 2:
    usage()

in_if = sys.argv[1]

# load BPF program
b = BPF(src_file = "ifstat_kern.c", cflags = [
  "-DANY=-1",
  "-DFILTER0_ENABLED=1",
  "-DFILTER0_SRC_IP=-1",
  "-DFILTER0_DST_IP=-1",
  "-DFILTER0_SRC_PORT=-1",
  "-DFILTER0_DST_PORT=-1",
  "-DFILTER0_IPPROTO=-1"
])

in_fn = b.load_func("xdp_packet_handler", BPF.XDP)
b.attach_xdp(in_if, in_fn, flags)

dropcnt = b.get_table("filter0")
while 1:
    try:
        for k in dropcnt.keys():
            print("abc", dropcnt[k])
        time.sleep(1)
    except KeyboardInterrupt:
        print("Removing filter from device")
        break;

b.remove_xdp(in_if, flags)