File: test_call1.py

package info (click to toggle)
bpfcc 0.18.0%2Bds-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 12,368 kB
  • sloc: ansic: 132,727; python: 36,226; cpp: 26,973; sh: 710; yacc: 525; makefile: 141; lex: 94
file content (48 lines) | stat: -rwxr-xr-x 1,632 bytes parent folder | download | duplicates (2)
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
48
#!/usr/bin/env python
# Copyright (c) PLUMgrid, Inc.
# Licensed under the Apache License, Version 2.0 (the "License")

from ctypes import c_ushort, c_int, c_ulonglong
from netaddr import IPAddress
from bcc import BPF
from pyroute2 import IPRoute
from socket import socket, AF_INET, SOCK_DGRAM
import sys
from time import sleep
from unittest import main, TestCase

arg1 = sys.argv.pop(1)

S_EOP = 1
S_ETHER = 2
S_ARP = 3
S_IP = 4

class TestBPFSocket(TestCase):
    def setUp(self):
        b = BPF(src_file=arg1, debug=0)
        ether_fn = b.load_func("parse_ether", BPF.SCHED_CLS)
        arp_fn = b.load_func("parse_arp", BPF.SCHED_CLS)
        ip_fn = b.load_func("parse_ip", BPF.SCHED_CLS)
        eop_fn = b.load_func("eop", BPF.SCHED_CLS)
        ip = IPRoute()
        ifindex = ip.link_lookup(ifname="eth0")[0]
        ip.tc("add", "sfq", ifindex, "1:")
        ip.tc("add-filter", "bpf", ifindex, ":1", fd=ether_fn.fd,
              name=ether_fn.name, parent="1:", action="ok", classid=1)
        self.jump = b.get_table("jump", c_int, c_int)
        self.jump[c_int(S_ARP)] = c_int(arp_fn.fd)
        self.jump[c_int(S_IP)] = c_int(ip_fn.fd)
        self.jump[c_int(S_EOP)] = c_int(eop_fn.fd)
        self.stats = b.get_table("stats", c_int, c_ulonglong)

    def test_jumps(self):
        udp = socket(AF_INET, SOCK_DGRAM)
        udp.sendto(b"a" * 10, ("172.16.1.1", 5000))
        udp.close()
        self.assertGreater(self.stats[c_int(S_IP)].value, 0)
        self.assertGreater(self.stats[c_int(S_ARP)].value, 0)
        self.assertGreater(self.stats[c_int(S_EOP)].value, 1)

if __name__ == "__main__":
    main()