xref: /freebsd/tests/sys/netinet/carp.py (revision 65cc5af1cf88ed124ab16091624e918faa61c7f2)
1 import pytest
2 from atf_python.sys.net.tools import ToolsHelper
3 from atf_python.sys.net.vnet import VnetTestTemplate
4 
5 sc = None
6 
7 
8 def filter_f(x):
9     ip = x.getlayer(sc.IP)
10     if not ip:
11         return False
12 
13     return ip.proto == 112
14 
15 
16 class 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     @pytest.mark.require_progs(["scapy"])
43     def test_source_mac(self):
44         "Test carp packets source address"
45 
46         if1 = self.vnet.iface_alias_map["if1"]
47 
48         ToolsHelper.print_output(
49             "ifconfig {} add vhid 1 192.0.2.203/24".format(if1.name)
50         )
51 
52         carp_pkts = sc.sniff(iface=if1.name, stop_filter=filter_f, timeout=5)
53 
54         self.check_carp_src_mac(carp_pkts)
55 
56     @pytest.mark.require_progs(["scapy"])
57     def test_source_mac_vrrp(self):
58         "Test VRRP packets source address"
59 
60         if1 = self.vnet.iface_alias_map["if1"]
61 
62         ToolsHelper.print_output(
63             "ifconfig {} add vhid 1 carpver 3 192.0.2.203/24".format(if1.name)
64         )
65 
66         carp_pkts = sc.sniff(iface=if1.name, stop_filter=filter_f, timeout=5)
67 
68         self.check_carp_src_mac(carp_pkts)
69 
70