ikev2: support responder hostname
[vpp.git] / test / test_trace_filter.py
1 #!/usr/bin/env python3
2
3 import unittest
4
5 from framework import VppTestCase, VppTestRunner, running_extended_tests
6 from vpp_ip_route import VppIpTable, VppIpRoute, VppRoutePath
7
8 from scapy.contrib.geneve import GENEVE
9 from scapy.packet import Raw
10 from scapy.layers.l2 import Ether
11 from scapy.layers.inet import IP, UDP
12 from scapy.layers.vxlan import VXLAN
13 from scapy.compat import raw
14
15
16 class TestTracefilter(VppTestCase):
17     """ Packet Tracer Filter Test """
18
19     @classmethod
20     def setUpClass(cls):
21         super(TestTracefilter, cls).setUpClass()
22
23     @classmethod
24     def tearDownClass(cls):
25         super(TestTracefilter, cls).tearDownClass()
26
27     def setUp(self):
28         super(TestTracefilter, self).setUp()
29         self.create_pg_interfaces(range(2))
30         self.pg0.generate_remote_hosts(11)
31         for i in self.pg_interfaces:
32             i.admin_up()
33             i.config_ip4()
34             i.resolve_arp()
35
36     def tearDown(self):
37         super(TestTracefilter, self).tearDown()
38         for i in self.pg_interfaces:
39             i.unconfig()
40             i.admin_down()
41
42     def cli(self, cmd):
43         r = self.vapi.cli_return_response(cmd)
44         if r.retval != 0:
45             s = "reply '%s'" % r.reply if hasattr(
46                 r, "reply") else "retval '%s'" % r.retval
47             raise RuntimeError("cli command '%s' FAIL with %s" % (cmd, s))
48         return r
49
50     # check number of hits for classifier
51     def assert_hits(self, n):
52         r = self.cli("show classify table verbose 2")
53         self.assertTrue(r.reply.find("hits %i" % n) != -1)
54
55     def add_filter(self, mask, match):
56         r = self.cli("classify filter trace mask %s match %s" % (mask, match))
57         self.vapi.cli("clear trace")
58         r = self.cli("trace add pg-input 1000 filter")
59
60     def del_all_filters(self):
61         self.cli("classify filter trace del")
62         r = self.cli("show classify filter")
63         s = "packet tracer:                 first table none"
64         self.assertTrue(r.reply.find(s) != -1)
65
66     def test_basic(self):
67         """ Packet Tracer Filter Test """
68         self.add_filter(
69             "l3 ip4 src",
70             "l3 ip4 src %s" %
71             self.pg0.remote_hosts[5].ip4)
72         self.add_filter(
73             "l3 ip4 proto l4 src_port",
74             "l3 ip4 proto 17 l4 src_port 2345")
75         # the packet we are trying to match
76         p = list()
77         for i in range(100):
78             src = self.pg0.remote_hosts[i % len(self.pg0.remote_hosts)].ip4
79             p.append((Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac) /
80                       IP(src=src, dst=self.pg1.remote_ip4) /
81                       UDP(sport=1234, dport=2345) / Raw('\xa5' * 100)))
82         for i in range(17):
83             p.append((Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac) /
84                       IP(src=self.pg0.remote_hosts[0].ip4,
85                          dst=self.pg1.remote_ip4) /
86                       UDP(sport=2345, dport=1234) / Raw('\xa5' * 100)))
87
88         self.send_and_expect(self.pg0, p, self.pg1, trace=False)
89
90         # Check for 9 and 17 classifier hits, which is the right answer
91         self.assert_hits(9)
92         self.assert_hits(17)
93
94         self.del_all_filters()
95
96     # install a classify rule, inject traffic and check for hits
97     def assert_classify(self, mask, match, packets, n=None):
98         self.add_filter("hex %s" % mask, "hex %s" % match)
99         self.send_and_expect(self.pg0, packets, self.pg1, trace=False)
100         self.assert_hits(n if n is not None else len(packets))
101         self.del_all_filters()
102
103     def test_encap(self):
104         """ Packet Tracer Filter Test with encap """
105
106         # the packet we are trying to match
107         p = (Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac) /
108              IP(src=self.pg0.remote_ip4, dst=self.pg1.remote_ip4) /
109              UDP() /
110              VXLAN() /
111              Ether() /
112              IP() /
113              UDP() /
114              GENEVE(vni=1234) /
115              Ether() /
116              IP(src='192.168.4.167') /
117              UDP() /
118              Raw('\xa5' * 100))
119
120         #
121         # compute filter mask & value
122         # we compute it by XOR'ing a template packet with a modified packet
123         # we need to set checksums to 0 to make sure scapy will not recompute
124         # them
125         #
126         tmpl = (Ether() /
127                 IP(chksum=0) /
128                 UDP(chksum=0) /
129                 VXLAN() /
130                 Ether() /
131                 IP(chksum=0) /
132                 UDP(chksum=0) /
133                 GENEVE(vni=0) /
134                 Ether() /
135                 IP(src='0.0.0.0', chksum=0))
136         ori = raw(tmpl)
137
138         # the mask
139         tmpl[GENEVE].vni = 0xffffff
140         user = tmpl[GENEVE].payload
141         user[IP].src = '255.255.255.255'
142         new = raw(tmpl)
143         mask = "".join(("{:02x}".format(o ^ n) for o, n in zip(ori, new)))
144
145         # this does not match (wrong vni)
146         tmpl[GENEVE].vni = 1
147         user = tmpl[GENEVE].payload
148         user[IP].src = '192.168.4.167'
149         new = raw(tmpl)
150         match = "".join(("{:02x}".format(o ^ n) for o, n in zip(ori, new)))
151         self.assert_classify(mask, match, [p] * 11, 0)
152
153         # this must match
154         tmpl[GENEVE].vni = 1234
155         new = raw(tmpl)
156         match = "".join(("{:02x}".format(o ^ n) for o, n in zip(ori, new)))
157         self.assert_classify(mask, match, [p] * 17)
158
159
160 if __name__ == '__main__':
161     unittest.main(testRunner=VppTestRunner)