MAP: Convert from DPO to input feature.
[vpp.git] / test / test_vxlan_gpe.py
1 #!/usr/bin/env python
2
3 import socket
4 from util import ip4n_range, ip4_range
5 import unittest
6 from framework import VppTestCase, VppTestRunner, running_extended_tests
7 from template_bd import BridgeDomain
8
9 from scapy.layers.l2 import Ether, Raw
10 from scapy.layers.inet import IP, UDP
11 from scapy.layers.vxlan import VXLAN
12 from scapy.utils import atol
13
14
15 @unittest.skipUnless(running_extended_tests, "part of extended tests")
16 class TestVxlanGpe(BridgeDomain, VppTestCase):
17     """ VXLAN-GPE Test Case """
18
19     def __init__(self, *args):
20         BridgeDomain.__init__(self)
21         VppTestCase.__init__(self, *args)
22
23     def encapsulate(self, pkt, vni):
24         """
25         Encapsulate the original payload frame by adding VXLAN-GPE header
26         with its UDP, IP and Ethernet fields
27         """
28         return (Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac) /
29                 IP(src=self.pg0.remote_ip4, dst=self.pg0.local_ip4) /
30                 UDP(sport=self.dport, dport=self.dport, chksum=0) /
31                 VXLAN(vni=vni, flags=self.flags) /
32                 pkt)
33
34     def ip_range(self, start, end):
35         """ range of remote ip's """
36         return ip4_range(self.pg0.remote_ip4, start, end)
37
38     def encap_mcast(self, pkt, src_ip, src_mac, vni):
39         """
40         Encapsulate the original payload frame by adding VXLAN-GPE header
41         with its UDP, IP and Ethernet fields
42         """
43         return (Ether(src=src_mac, dst=self.mcast_mac) /
44                 IP(src=src_ip, dst=self.mcast_ip4) /
45                 UDP(sport=self.dport, dport=self.dport, chksum=0) /
46                 VXLAN(vni=vni, flags=self.flags) /
47                 pkt)
48
49     def decapsulate(self, pkt):
50         """
51         Decapsulate the original payload frame by removing VXLAN-GPE header
52         """
53         # check if is set I and P flag
54         self.assertEqual(pkt[VXLAN].flags, 0x0c)
55         return pkt[VXLAN].payload
56
57     # Method for checking VXLAN-GPE encapsulation.
58     #
59     def check_encapsulation(self, pkt, vni, local_only=False, mcast_pkt=False):
60         # Verify source MAC is VPP_MAC and destination MAC is MY_MAC resolved
61         #  by VPP using ARP.
62         self.assertEqual(pkt[Ether].src, self.pg0.local_mac)
63         if not local_only:
64             if not mcast_pkt:
65                 self.assertEqual(pkt[Ether].dst, self.pg0.remote_mac)
66             else:
67                 self.assertEqual(pkt[Ether].dst, type(self).mcast_mac)
68         # Verify VXLAN-GPE tunnel src IP is VPP_IP and dst IP is MY_IP.
69         self.assertEqual(pkt[IP].src, self.pg0.local_ip4)
70         if not local_only:
71             if not mcast_pkt:
72                 self.assertEqual(pkt[IP].dst, self.pg0.remote_ip4)
73             else:
74                 self.assertEqual(pkt[IP].dst, type(self).mcast_ip4)
75         # Verify UDP destination port is VXLAN-GPE 4790, source UDP port
76         #  could be arbitrary.
77         self.assertEqual(pkt[UDP].dport, type(self).dport)
78         # Verify VNI
79         self.assertEqual(pkt[VXLAN].vni, vni)
80
81     @classmethod
82     def create_vxlan_gpe_flood_test_bd(cls, vni, n_ucast_tunnels):
83         # Create 10 ucast vxlan_gpe tunnels under bd
84         ip_range_start = 10
85         ip_range_end = ip_range_start + n_ucast_tunnels
86         next_hop_address = cls.pg0.remote_ip4n
87         for dest_ip4n in ip4n_range(next_hop_address, ip_range_start,
88                                     ip_range_end):
89             # add host route so dest_ip4n will not be resolved
90             cls.vapi.ip_add_del_route(dest_ip4n, 32, next_hop_address)
91             r = cls.vapi.vxlan_gpe_add_del_tunnel(
92                 src_addr=cls.pg0.local_ip4n,
93                 dst_addr=dest_ip4n,
94                 vni=vni)
95             cls.vapi.sw_interface_set_l2_bridge(r.sw_if_index, bd_id=vni)
96
97     @classmethod
98     def add_del_shared_mcast_dst_load(cls, is_add):
99         """
100         add or del tunnels sharing the same mcast dst
101         to test vxlan_gpe ref_count mechanism
102         """
103         n_shared_dst_tunnels = 20
104         vni_start = 1000
105         vni_end = vni_start + n_shared_dst_tunnels
106         for vni in range(vni_start, vni_end):
107             r = cls.vapi.vxlan_gpe_add_del_tunnel(
108                 src_addr=cls.pg0.local_ip4n,
109                 dst_addr=cls.mcast_ip4n,
110                 mcast_sw_if_index=1,
111                 vni=vni,
112                 is_add=is_add)
113             if r.sw_if_index == 0xffffffff:
114                 raise "bad sw_if_index"
115
116     @classmethod
117     def add_shared_mcast_dst_load(cls):
118         cls.add_del_shared_mcast_dst_load(is_add=1)
119
120     @classmethod
121     def del_shared_mcast_dst_load(cls):
122         cls.add_del_shared_mcast_dst_load(is_add=0)
123
124     @classmethod
125     def add_del_mcast_tunnels_load(cls, is_add):
126         """
127         add or del tunnels to test vxlan_gpe stability
128         """
129         n_distinct_dst_tunnels = 20
130         ip_range_start = 10
131         ip_range_end = ip_range_start + n_distinct_dst_tunnels
132         for dest_ip4n in ip4n_range(cls.mcast_ip4n, ip_range_start,
133                                     ip_range_end):
134             vni = bytearray(dest_ip4n)[3]
135             cls.vapi.vxlan_gpe_add_del_tunnel(
136                 src_addr=cls.pg0.local_ip4n,
137                 dst_addr=dest_ip4n,
138                 mcast_sw_if_index=1,
139                 vni=vni,
140                 is_add=is_add)
141
142     @classmethod
143     def add_mcast_tunnels_load(cls):
144         cls.add_del_mcast_tunnels_load(is_add=1)
145
146     @classmethod
147     def del_mcast_tunnels_load(cls):
148         cls.add_del_mcast_tunnels_load(is_add=0)
149
150     # Class method to start the VXLAN-GPE test case.
151     #  Overrides setUpClass method in VppTestCase class.
152     #  Python try..except statement is used to ensure that the tear down of
153     #  the class will be executed even if exception is raised.
154     #  @param cls The class pointer.
155     @classmethod
156     def setUpClass(cls):
157         super(TestVxlanGpe, cls).setUpClass()
158
159         try:
160             cls.dport = 4790
161             cls.flags = 0x0c
162
163             # Create 2 pg interfaces.
164             cls.create_pg_interfaces(range(4))
165             for pg in cls.pg_interfaces:
166                 pg.admin_up()
167
168             # Configure IPv4 addresses on VPP pg0.
169             cls.pg0.config_ip4()
170
171             # Resolve MAC address for VPP's IP address on pg0.
172             cls.pg0.resolve_arp()
173
174             # Our Multicast address
175             cls.mcast_ip4 = '239.1.1.1'
176             cls.mcast_ip4n = socket.inet_pton(socket.AF_INET, cls.mcast_ip4)
177             iplong = atol(cls.mcast_ip4)
178             cls.mcast_mac = "01:00:5e:%02x:%02x:%02x" % (
179                 (iplong >> 16) & 0x7F, (iplong >> 8) & 0xFF, iplong & 0xFF)
180
181             # Create VXLAN-GPE VTEP on VPP pg0, and put vxlan_gpe_tunnel0
182             # and pg1 into BD.
183             cls.single_tunnel_bd = 11
184             r = cls.vapi.vxlan_gpe_add_del_tunnel(
185                 src_addr=cls.pg0.local_ip4n,
186                 dst_addr=cls.pg0.remote_ip4n,
187                 vni=cls.single_tunnel_bd)
188             cls.vapi.sw_interface_set_l2_bridge(r.sw_if_index,
189                                                 bd_id=cls.single_tunnel_bd)
190             cls.vapi.sw_interface_set_l2_bridge(cls.pg1.sw_if_index,
191                                                 bd_id=cls.single_tunnel_bd)
192
193             # Setup vni 2 to test multicast flooding
194             cls.n_ucast_tunnels = 10
195             cls.mcast_flood_bd = 12
196             cls.create_vxlan_gpe_flood_test_bd(cls.mcast_flood_bd,
197                                                cls.n_ucast_tunnels)
198             r = cls.vapi.vxlan_gpe_add_del_tunnel(
199                 src_addr=cls.pg0.local_ip4n,
200                 dst_addr=cls.mcast_ip4n,
201                 mcast_sw_if_index=1,
202                 vni=cls.mcast_flood_bd)
203             cls.vapi.sw_interface_set_l2_bridge(r.sw_if_index,
204                                                 bd_id=cls.mcast_flood_bd)
205             cls.vapi.sw_interface_set_l2_bridge(cls.pg2.sw_if_index,
206                                                 bd_id=cls.mcast_flood_bd)
207
208             # Add and delete mcast tunnels to check stability
209             cls.add_shared_mcast_dst_load()
210             cls.add_mcast_tunnels_load()
211             cls.del_shared_mcast_dst_load()
212             cls.del_mcast_tunnels_load()
213
214             # Setup vni 3 to test unicast flooding
215             cls.ucast_flood_bd = 13
216             cls.create_vxlan_gpe_flood_test_bd(cls.ucast_flood_bd,
217                                                cls.n_ucast_tunnels)
218             cls.vapi.sw_interface_set_l2_bridge(cls.pg3.sw_if_index,
219                                                 bd_id=cls.ucast_flood_bd)
220         except Exception:
221             super(TestVxlanGpe, cls).tearDownClass()
222             raise
223
224     @unittest.skip("test disabled for vxlan-gpe")
225     def test_mcast_flood(self):
226         """ inherited from BridgeDomain """
227         pass
228
229     @unittest.skip("test disabled for vxlan-gpe")
230     def test_mcast_rcv(self):
231         """ inherited from BridgeDomain """
232         pass
233
234     # Method to define VPP actions before tear down of the test case.
235     #  Overrides tearDown method in VppTestCase class.
236     #  @param self The object pointer.
237     def tearDown(self):
238         super(TestVxlanGpe, self).tearDown()
239         if not self.vpp_dead:
240             self.logger.info(self.vapi.cli("show bridge-domain 11 detail"))
241             self.logger.info(self.vapi.cli("show bridge-domain 12 detail"))
242             self.logger.info(self.vapi.cli("show bridge-domain 13 detail"))
243             self.logger.info(self.vapi.cli("show int"))
244             self.logger.info(self.vapi.cli("show vxlan-gpe"))
245             self.logger.info(self.vapi.cli("show trace"))
246
247
248 if __name__ == '__main__':
249     unittest.main(testRunner=VppTestRunner)