xref: /freebsd/tests/sys/netinet/carp.py (revision 9bc300465e48e19d794d88d0c158a2adb92c7197)
1import pytest
2from atf_python.sys.net.tools import ToolsHelper
3from atf_python.sys.net.vnet import VnetTestTemplate
4
5sc = None
6
7
8def filter_f(x):
9    ip = x.getlayer(sc.IP)
10    if not ip:
11        return False
12
13    return ip.proto == 112
14
15
16class TestCarp(VnetTestTemplate):
17    REQUIRED_MODULES = ["carp"]
18    TOPOLOGY = {
19        "vnet1": {"ifaces": ["if1"]},
20        "if1": {"prefixes4": [("192.0.2.1/24", "192.0.2.2/24")]},
21    }
22
23    def setup_method(self, method):
24        global sc
25        if sc is None:
26            import scapy.all as _sc
27
28            sc = _sc
29        super().setup_method(method)
30
31    @classmethod
32    def check_carp_src_mac(self, pkts):
33        for p in pkts:
34            if not filter_f(p):
35                continue
36
37            print("Packet src mac {}".format(p.src))
38
39            if p.src != "00:00:5e:00:01:01":
40                raise
41
42    def test_source_mac(self):
43        "Test carp packets source address"
44
45        if1 = self.vnet.iface_alias_map["if1"]
46
47        ToolsHelper.print_output(
48            "ifconfig {} add vhid 1 192.0.2.203/24".format(if1.name)
49        )
50
51        carp_pkts = sc.sniff(iface=if1.name, stop_filter=filter_f, timeout=5)
52
53        self.check_carp_src_mac(carp_pkts)
54
55    def test_source_mac_vrrp(self):
56        "Test VRRP packets source address"
57
58        if1 = self.vnet.iface_alias_map["if1"]
59
60        ToolsHelper.print_output(
61            "ifconfig {} add vhid 1 carpver 3 192.0.2.203/24".format(if1.name)
62        )
63
64        carp_pkts = sc.sniff(iface=if1.name, stop_filter=filter_f, timeout=5)
65
66        self.check_carp_src_mac(carp_pkts)
67
68