fib: fib api updates
[vpp.git] / test / test_vxlan.py
index 3c824b5..c74efe7 100644 (file)
@@ -1,7 +1,7 @@
 #!/usr/bin/env python
 
 import socket
 #!/usr/bin/env python
 
 import socket
-from util import ip4n_range, ip4_range
+from util import ip4n_range, ip4_range, reassemble4
 import unittest
 from framework import VppTestCase, VppTestRunner
 from template_bd import BridgeDomain
 import unittest
 from framework import VppTestCase, VppTestRunner
 from template_bd import BridgeDomain
@@ -10,22 +10,8 @@ from scapy.layers.l2 import Ether, Raw
 from scapy.layers.inet import IP, UDP
 from scapy.layers.vxlan import VXLAN
 from scapy.utils import atol
 from scapy.layers.inet import IP, UDP
 from scapy.layers.vxlan import VXLAN
 from scapy.utils import atol
-
-import StringIO
-
-
-def reassemble(listoffragments):
-    buffer = StringIO.StringIO()
-    first = listoffragments[0]
-    buffer.seek(20)
-    for pkt in listoffragments:
-        buffer.seek(pkt[IP].frag*8)
-        buffer.write(pkt[IP].payload)
-    first.len = len(buffer.getvalue()) + 20
-    first.flags = 0
-    del(first.chksum)
-    header = str(first[IP])[:20]
-    return first[IP].__class__(header + buffer.getvalue())
+from vpp_ip_route import VppIpRoute, VppRoutePath
+from vpp_ip import INVALID_INDEX
 
 
 class TestVxlan(BridgeDomain, VppTestCase):
 
 
 class TestVxlan(BridgeDomain, VppTestCase):
@@ -100,15 +86,19 @@ class TestVxlan(BridgeDomain, VppTestCase):
         # Create 10 ucast vxlan tunnels under bd
         ip_range_start = 10
         ip_range_end = ip_range_start + n_ucast_tunnels
         # Create 10 ucast vxlan tunnels under bd
         ip_range_start = 10
         ip_range_end = ip_range_start + n_ucast_tunnels
-        next_hop_address = cls.pg0.remote_ip4n
-        for dest_ip4n in ip4n_range(next_hop_address, ip_range_start,
-                                    ip_range_end):
+        next_hop_address = cls.pg0.remote_ip4
+        for dest_ip4 in ip4_range(next_hop_address, ip_range_start,
+                                  ip_range_end):
             # add host route so dest_ip4n will not be resolved
             # add host route so dest_ip4n will not be resolved
-            cls.vapi.ip_add_del_route(dest_ip4n, 32, next_hop_address)
-            r = cls.vapi.vxlan_add_del_tunnel(
-                src_addr=cls.pg0.local_ip4n,
-                dst_addr=dest_ip4n,
-                vni=vni)
+            rip = VppIpRoute(cls, dest_ip4, 32,
+                             [VppRoutePath(next_hop_address,
+                                           INVALID_INDEX)],
+                             register=False)
+            rip.add_vpp_config()
+            dest_ip4n = socket.inet_pton(socket.AF_INET, dest_ip4)
+
+            r = cls.vapi.vxlan_add_del_tunnel(src_address=cls.pg0.local_ip4n,
+                                              dst_address=dest_ip4n, vni=vni)
             cls.vapi.sw_interface_set_l2_bridge(r.sw_if_index, bd_id=vni)
 
     @classmethod
             cls.vapi.sw_interface_set_l2_bridge(r.sw_if_index, bd_id=vni)
 
     @classmethod
@@ -121,14 +111,12 @@ class TestVxlan(BridgeDomain, VppTestCase):
         vni_start = 10000
         vni_end = vni_start + n_shared_dst_tunnels
         for vni in range(vni_start, vni_end):
         vni_start = 10000
         vni_end = vni_start + n_shared_dst_tunnels
         for vni in range(vni_start, vni_end):
-            r = cls.vapi.vxlan_add_del_tunnel(
-                src_addr=cls.pg0.local_ip4n,
-                dst_addr=cls.mcast_ip4n,
-                mcast_sw_if_index=1,
-                vni=vni,
-                is_add=is_add)
+            r = cls.vapi.vxlan_add_del_tunnel(src_address=cls.pg0.local_ip4n,
+                                              dst_address=cls.mcast_ip4n,
+                                              mcast_sw_if_index=1,
+                                              is_add=is_add, vni=vni)
             if r.sw_if_index == 0xffffffff:
             if r.sw_if_index == 0xffffffff:
-                raise "bad sw_if_index"
+                raise ValueError("bad sw_if_index: ~0")
 
     @classmethod
     def add_shared_mcast_dst_load(cls):
 
     @classmethod
     def add_shared_mcast_dst_load(cls):
@@ -149,12 +137,10 @@ class TestVxlan(BridgeDomain, VppTestCase):
         for dest_ip4n in ip4n_range(cls.mcast_ip4n, ip_range_start,
                                     ip_range_end):
             vni = bytearray(dest_ip4n)[3]
         for dest_ip4n in ip4n_range(cls.mcast_ip4n, ip_range_start,
                                     ip_range_end):
             vni = bytearray(dest_ip4n)[3]
-            cls.vapi.vxlan_add_del_tunnel(
-                src_addr=cls.pg0.local_ip4n,
-                dst_addr=dest_ip4n,
-                mcast_sw_if_index=1,
-                vni=vni,
-                is_add=is_add)
+            cls.vapi.vxlan_add_del_tunnel(src_address=cls.pg0.local_ip4n,
+                                          dst_address=dest_ip4n,
+                                          mcast_sw_if_index=1, is_add=is_add,
+                                          vni=vni)
 
     @classmethod
     def add_mcast_tunnels_load(cls):
 
     @classmethod
     def add_mcast_tunnels_load(cls):
@@ -198,29 +184,27 @@ class TestVxlan(BridgeDomain, VppTestCase):
             # Create VXLAN VTEP on VPP pg0, and put vxlan_tunnel0 and pg1
             #  into BD.
             cls.single_tunnel_bd = 1
             # Create VXLAN VTEP on VPP pg0, and put vxlan_tunnel0 and pg1
             #  into BD.
             cls.single_tunnel_bd = 1
-            r = cls.vapi.vxlan_add_del_tunnel(
-                src_addr=cls.pg0.local_ip4n,
-                dst_addr=cls.pg0.remote_ip4n,
-                vni=cls.single_tunnel_bd)
-            cls.vapi.sw_interface_set_l2_bridge(r.sw_if_index,
-                                                bd_id=cls.single_tunnel_bd)
-            cls.vapi.sw_interface_set_l2_bridge(cls.pg1.sw_if_index,
+            r = cls.vapi.vxlan_add_del_tunnel(src_address=cls.pg0.local_ip4n,
+                                              dst_address=cls.pg0.remote_ip4n,
+                                              vni=cls.single_tunnel_bd)
+            cls.vapi.sw_interface_set_l2_bridge(rx_sw_if_index=r.sw_if_index,
                                                 bd_id=cls.single_tunnel_bd)
                                                 bd_id=cls.single_tunnel_bd)
+            cls.vapi.sw_interface_set_l2_bridge(
+                rx_sw_if_index=cls.pg1.sw_if_index, bd_id=cls.single_tunnel_bd)
 
             # Setup vni 2 to test multicast flooding
             cls.n_ucast_tunnels = 10
             cls.mcast_flood_bd = 2
             cls.create_vxlan_flood_test_bd(cls.mcast_flood_bd,
                                            cls.n_ucast_tunnels)
 
             # Setup vni 2 to test multicast flooding
             cls.n_ucast_tunnels = 10
             cls.mcast_flood_bd = 2
             cls.create_vxlan_flood_test_bd(cls.mcast_flood_bd,
                                            cls.n_ucast_tunnels)
-            r = cls.vapi.vxlan_add_del_tunnel(
-                src_addr=cls.pg0.local_ip4n,
-                dst_addr=cls.mcast_ip4n,
-                mcast_sw_if_index=1,
-                vni=cls.mcast_flood_bd)
-            cls.vapi.sw_interface_set_l2_bridge(r.sw_if_index,
-                                                bd_id=cls.mcast_flood_bd)
-            cls.vapi.sw_interface_set_l2_bridge(cls.pg2.sw_if_index,
+            r = cls.vapi.vxlan_add_del_tunnel(src_address=cls.pg0.local_ip4n,
+                                              dst_address=cls.mcast_ip4n,
+                                              mcast_sw_if_index=1,
+                                              vni=cls.mcast_flood_bd)
+            cls.vapi.sw_interface_set_l2_bridge(rx_sw_if_index=r.sw_if_index,
                                                 bd_id=cls.mcast_flood_bd)
                                                 bd_id=cls.mcast_flood_bd)
+            cls.vapi.sw_interface_set_l2_bridge(
+                rx_sw_if_index=cls.pg2.sw_if_index, bd_id=cls.mcast_flood_bd)
 
             # Add and delete mcast tunnels to check stability
             cls.add_shared_mcast_dst_load()
 
             # Add and delete mcast tunnels to check stability
             cls.add_shared_mcast_dst_load()
@@ -232,12 +216,16 @@ class TestVxlan(BridgeDomain, VppTestCase):
             cls.ucast_flood_bd = 3
             cls.create_vxlan_flood_test_bd(cls.ucast_flood_bd,
                                            cls.n_ucast_tunnels)
             cls.ucast_flood_bd = 3
             cls.create_vxlan_flood_test_bd(cls.ucast_flood_bd,
                                            cls.n_ucast_tunnels)
-            cls.vapi.sw_interface_set_l2_bridge(cls.pg3.sw_if_index,
-                                                bd_id=cls.ucast_flood_bd)
+            cls.vapi.sw_interface_set_l2_bridge(
+                rx_sw_if_index=cls.pg3.sw_if_index, bd_id=cls.ucast_flood_bd)
         except Exception:
             super(TestVxlan, cls).tearDownClass()
             raise
 
         except Exception:
             super(TestVxlan, cls).tearDownClass()
             raise
 
+    @classmethod
+    def tearDownClass(cls):
+        super(TestVxlan, cls).tearDownClass()
+
     def test_encap_big_packet(self):
         """ Encapsulation test send big frame from pg1
         Verify receipt of encapsulated frames on pg0
     def test_encap_big_packet(self):
         """ Encapsulation test send big frame from pg1
         Verify receipt of encapsulated frames on pg0
@@ -259,7 +247,7 @@ class TestVxlan(BridgeDomain, VppTestCase):
         # Pick first received frame and check if it's correctly encapsulated.
         out = self.pg0.get_capture(2)
         ether = out[0]
         # Pick first received frame and check if it's correctly encapsulated.
         out = self.pg0.get_capture(2)
         ether = out[0]
-        pkt = reassemble(out)
+        pkt = reassemble4(out)
         pkt = ether / pkt
         self.check_encapsulation(pkt, self.single_tunnel_bd)
 
         pkt = ether / pkt
         self.check_encapsulation(pkt, self.single_tunnel_bd)
 
@@ -272,11 +260,12 @@ class TestVxlan(BridgeDomain, VppTestCase):
     #  @param self The object pointer.
     def tearDown(self):
         super(TestVxlan, self).tearDown()
     #  @param self The object pointer.
     def tearDown(self):
         super(TestVxlan, self).tearDown()
-        if not self.vpp_dead:
-            self.logger.info(self.vapi.cli("show bridge-domain 1 detail"))
-            self.logger.info(self.vapi.cli("show bridge-domain 2 detail"))
-            self.logger.info(self.vapi.cli("show bridge-domain 3 detail"))
-            self.logger.info(self.vapi.cli("show vxlan tunnel"))
+
+    def show_commands_at_teardown(self):
+        self.logger.info(self.vapi.cli("show bridge-domain 1 detail"))
+        self.logger.info(self.vapi.cli("show bridge-domain 2 detail"))
+        self.logger.info(self.vapi.cli("show bridge-domain 3 detail"))
+        self.logger.info(self.vapi.cli("show vxlan tunnel"))
 
 
 if __name__ == '__main__':
 
 
 if __name__ == '__main__':