diff options
Diffstat (limited to 'test')
-rw-r--r-- | test/template_bd.py | 125 | ||||
-rw-r--r-- | test/test_vxlan.py | 121 |
2 files changed, 206 insertions, 40 deletions
diff --git a/test/template_bd.py b/test/template_bd.py index 01e8b855004..e240a2807c7 100644 --- a/test/template_bd.py +++ b/test/template_bd.py @@ -11,23 +11,28 @@ class BridgeDomain(object): __metaclass__ = ABCMeta @property - def frame_pg0_to_pg1(self): - """ Ethernet frame sent from pg0 and expected to arrive at pg1 """ + def frame_request(self): + """ Ethernet frame modeling a generic request """ return (Ether(src='00:00:00:00:00:01', dst='00:00:00:00:00:02') / IP(src='1.2.3.4', dst='4.3.2.1') / UDP(sport=10000, dport=20000) / Raw('\xa5' * 100)) @property - def frame_pg1_to_pg0(self): - """ Ethernet frame sent from pg1 and expected to arrive at pg0 """ + def frame_reply(self): + """ Ethernet frame modeling a generic reply """ return (Ether(src='00:00:00:00:00:02', dst='00:00:00:00:00:01') / IP(src='4.3.2.1', dst='1.2.3.4') / UDP(sport=20000, dport=10000) / Raw('\xa5' * 100)) @abstractmethod - def encapsulate(self, pkt): + def encap_mcast(self, pkt, src_ip, src_mac, vni): + """ Encapsulate mcast packet """ + pass + + @abstractmethod + def encapsulate(self, pkt, vni): """ Encapsulate packet """ pass @@ -37,17 +42,30 @@ class BridgeDomain(object): pass @abstractmethod - def check_encapsulation(self, pkt): + def check_encapsulation(self, pkt, vni, local_only=False): """ Verify the encapsulation """ pass + def assert_eq_pkts(self, pkt1, pkt2): + """ Verify the Ether, IP, UDP, payload are equal in both + packets + """ + self.assertEqual(pkt1[Ether].src, pkt2[Ether].src) + self.assertEqual(pkt1[Ether].dst, pkt2[Ether].dst) + self.assertEqual(pkt1[IP].src, pkt2[IP].src) + self.assertEqual(pkt1[IP].dst, pkt2[IP].dst) + self.assertEqual(pkt1[UDP].sport, pkt2[UDP].sport) + self.assertEqual(pkt1[UDP].dport, pkt2[UDP].dport) + self.assertEqual(pkt1[Raw], pkt2[Raw]) + def test_decap(self): """ Decapsulation test Send encapsulated frames from pg0 Verify receipt of decapsulated frames on pg1 """ - encapsulated_pkt = self.encapsulate(self.frame_pg0_to_pg1) + encapsulated_pkt = self.encapsulate(self.frame_request, + self.single_tunnel_bd) self.pg0.add_stream([encapsulated_pkt, ]) @@ -61,22 +79,57 @@ class BridgeDomain(object): 'Invalid number of packets on ' 'output: {}'.format(len(out))) pkt = out[0] - - # TODO: add error messages - self.assertEqual(pkt[Ether].src, self.frame_pg0_to_pg1[Ether].src) - self.assertEqual(pkt[Ether].dst, self.frame_pg0_to_pg1[Ether].dst) - self.assertEqual(pkt[IP].src, self.frame_pg0_to_pg1[IP].src) - self.assertEqual(pkt[IP].dst, self.frame_pg0_to_pg1[IP].dst) - self.assertEqual(pkt[UDP].sport, self.frame_pg0_to_pg1[UDP].sport) - self.assertEqual(pkt[UDP].dport, self.frame_pg0_to_pg1[UDP].dport) - self.assertEqual(pkt[Raw], self.frame_pg0_to_pg1[Raw]) + self.assert_eq_pkts(pkt, self.frame_request) def test_encap(self): """ Encapsulation test Send frames from pg1 Verify receipt of encapsulated frames on pg0 """ - self.pg1.add_stream([self.frame_pg1_to_pg0]) + self.pg1.add_stream([self.frame_reply]) + + self.pg0.enable_capture() + + self.pg_start() + + # Pick first received frame and check if it's corectly encapsulated. + out = self.pg0.get_capture() + self.assertEqual(len(out), 1, + 'Invalid number of packets on ' + 'output: {}'.format(len(out))) + pkt = out[0] + self.check_encapsulation(pkt, self.single_tunnel_bd) + + payload = self.decapsulate(pkt) + self.assert_eq_pkts(payload, self.frame_reply) + + def test_ucast_flood(self): + """ Unicast flood test + Send frames from pg3 + Verify receipt of encapsulated frames on pg0 + """ + self.pg3.add_stream([self.frame_reply]) + + self.pg0.enable_capture() + + self.pg_start() + + # Get packet from each tunnel and assert it's corectly encapsulated. + out = self.pg0.get_capture() + self.assertEqual(len(out), 10, + 'Invalid number of packets on ' + 'output: {}'.format(len(out))) + for pkt in out: + self.check_encapsulation(pkt, self.ucast_flood_bd, True) + payload = self.decapsulate(pkt) + self.assert_eq_pkts(payload, self.frame_reply) + + def test_mcast_flood(self): + """ Multicast flood test + Send frames from pg2 + Verify receipt of encapsulated frames on pg0 + """ + self.pg2.add_stream([self.frame_reply]) self.pg0.enable_capture() @@ -88,14 +141,34 @@ class BridgeDomain(object): 'Invalid number of packets on ' 'output: {}'.format(len(out))) pkt = out[0] - self.check_encapsulation(pkt) + self.check_encapsulation(pkt, self.mcast_flood_bd, True) payload = self.decapsulate(pkt) - # TODO: add error messages - self.assertEqual(payload[Ether].src, self.frame_pg1_to_pg0[Ether].src) - self.assertEqual(payload[Ether].dst, self.frame_pg1_to_pg0[Ether].dst) - self.assertEqual(payload[IP].src, self.frame_pg1_to_pg0[IP].src) - self.assertEqual(payload[IP].dst, self.frame_pg1_to_pg0[IP].dst) - self.assertEqual(payload[UDP].sport, self.frame_pg1_to_pg0[UDP].sport) - self.assertEqual(payload[UDP].dport, self.frame_pg1_to_pg0[UDP].dport) - self.assertEqual(payload[Raw], self.frame_pg1_to_pg0[Raw]) + self.assert_eq_pkts(payload, self.frame_reply) + + @staticmethod + def ipn_to_ip(ipn): + return '.'.join(str(i) for i in bytearray(ipn)) + + def test_mcast_rcv(self): + """ Multicast receive test + Send 20 encapsulated frames from pg0 only 10 match unicast tunnels + Verify receipt of 10 decap frames on pg2 + """ + mac = self.pg0.remote_mac + ip_range_start = 10 + ip_range_end = 30 + mcast_stream = [ + self.encap_mcast(self.frame_request, self.ipn_to_ip(ip), mac, + self.mcast_flood_bd) + for ip in self.ip4_range(self.pg0.remote_ip4n, + ip_range_start, ip_range_end)] + self.pg0.add_stream(mcast_stream) + self.pg2.enable_capture() + self.pg_start() + out = self.pg2.get_capture() + self.assertEqual(len(out), 10, + 'Invalid number of packets on ' + 'output: {}'.format(len(out))) + for pkt in out: + self.assert_eq_pkts(pkt, self.frame_request) diff --git a/test/test_vxlan.py b/test/test_vxlan.py index 1978cf0c018..845b8175b7b 100644 --- a/test/test_vxlan.py +++ b/test/test_vxlan.py @@ -1,5 +1,6 @@ #!/usr/bin/env python +import socket import unittest from framework import VppTestCase, VppTestRunner from template_bd import BridgeDomain @@ -7,6 +8,7 @@ from template_bd import BridgeDomain from scapy.layers.l2 import Ether from scapy.layers.inet import IP, UDP from scapy.layers.vxlan import VXLAN +from scapy.utils import atol class TestVxlan(BridgeDomain, VppTestCase): @@ -16,7 +18,7 @@ class TestVxlan(BridgeDomain, VppTestCase): BridgeDomain.__init__(self) VppTestCase.__init__(self, *args) - def encapsulate(self, pkt): + def encapsulate(self, pkt, vni): """ Encapsulate the original payload frame by adding VXLAN header with its UDP, IP and Ethernet fields @@ -24,7 +26,18 @@ class TestVxlan(BridgeDomain, VppTestCase): return (Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac) / IP(src=self.pg0.remote_ip4, dst=self.pg0.local_ip4) / UDP(sport=self.dport, dport=self.dport, chksum=0) / - VXLAN(vni=self.vni, flags=self.flags) / + VXLAN(vni=vni, flags=self.flags) / + pkt) + + def encap_mcast(self, pkt, src_ip, src_mac, vni): + """ + Encapsulate the original payload frame by adding VXLAN header with its + UDP, IP and Ethernet fields + """ + return (Ether(src=src_mac, dst=self.mcast_mac4) / + IP(src=src_ip, dst=self.mcast_ip4) / + UDP(sport=self.dport, dport=self.dport, chksum=0) / + VXLAN(vni=vni, flags=self.flags) / pkt) def decapsulate(self, pkt): @@ -37,21 +50,66 @@ class TestVxlan(BridgeDomain, VppTestCase): # Method for checking VXLAN encapsulation. # - def check_encapsulation(self, pkt): + def check_encapsulation(self, pkt, vni, local_only=False): # TODO: add error messages # Verify source MAC is VPP_MAC and destination MAC is MY_MAC resolved # by VPP using ARP. self.assertEqual(pkt[Ether].src, self.pg0.local_mac) - self.assertEqual(pkt[Ether].dst, self.pg0.remote_mac) + if not local_only: + self.assertEqual(pkt[Ether].dst, self.pg0.remote_mac) # Verify VXLAN tunnel source IP is VPP_IP and destination IP is MY_IP. self.assertEqual(pkt[IP].src, self.pg0.local_ip4) - self.assertEqual(pkt[IP].dst, self.pg0.remote_ip4) + if not local_only: + self.assertEqual(pkt[IP].dst, self.pg0.remote_ip4) # Verify UDP destination port is VXLAN 4789, source UDP port could be # arbitrary. self.assertEqual(pkt[UDP].dport, type(self).dport) # TODO: checksum check - # Verify VNI, based on configuration it must be 1. - self.assertEqual(pkt[VXLAN].vni, type(self).vni) + # Verify VNI + self.assertEqual(pkt[VXLAN].vni, vni) + + @staticmethod + def ip4_range(ip4n, s=10, e=20): + base = str(bytearray(ip4n)[:3]) + return ((base + ip) for ip in str(bytearray(range(s, e)))) + + @classmethod + def create_vxlan_flood_test_bd(cls, vni): + # Create 10 ucast vxlan tunnels under bd + ip_range_start = 10 + ip_range_end = 20 + next_hop_address = cls.pg0.remote_ip4n + for dest_addr in cls.ip4_range(next_hop_address, ip_range_start, + ip_range_end): + # add host route so dest_addr will not be resolved + cls.vapi.ip_add_del_route(dest_addr, 32, next_hop_address) + r = cls.vapi.vxlan_add_del_tunnel( + src_addr=cls.pg0.local_ip4n, + dst_addr=dest_addr, + vni=vni) + cls.vapi.sw_interface_set_l2_bridge(r.sw_if_index, bd_id=vni) + + @classmethod + def add_del_mcast_load(cls, is_add): + ip_range_start = 10 + ip_range_end = 210 + for dest_addr in cls.ip4_range(cls.mcast_ip4n, ip_range_start, + ip_range_end): + vni = bytearray(dest_addr)[3] + cls.vapi.vxlan_add_del_tunnel( + src_addr=cls.pg0.local_ip4n, + dst_addr=dest_addr, + mcast_sw_if_index=1, + vni=vni, + is_add=is_add) + + @classmethod + def add_mcast_load(cls): + cls.add_del_mcast_load(is_add=1) + + @classmethod + def del_mcast_load(cls): + cls.add_del_mcast_load(is_add=0) # Class method to start the VXLAN test case. # Overrides setUpClass method in VppTestCase class. @@ -65,12 +123,11 @@ class TestVxlan(BridgeDomain, VppTestCase): try: cls.dport = 4789 cls.flags = 0x8 - cls.vni = 1 # Create 2 pg interfaces. - cls.create_pg_interfaces(range(2)) - cls.pg0.admin_up() - cls.pg1.admin_up() + cls.create_pg_interfaces(range(4)) + for pg in cls.pg_interfaces: + pg.admin_up() # Configure IPv4 addresses on VPP pg0. cls.pg0.config_ip4() @@ -78,14 +135,47 @@ class TestVxlan(BridgeDomain, VppTestCase): # Resolve MAC address for VPP's IP address on pg0. cls.pg0.resolve_arp() + # Our Multicast address + cls.mcast_ip4 = '239.1.1.1' + cls.mcast_ip4n = socket.inet_pton(socket.AF_INET, cls.mcast_ip4) + iplong = atol(cls.mcast_ip4) + cls.mcast_mac4 = "01:00:5e:%02x:%02x:%02x" % ( + (iplong >> 16) & 0x7F, (iplong >> 8) & 0xFF, iplong & 0xFF) + # 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.vni) - cls.vapi.sw_interface_set_l2_bridge(r.sw_if_index, bd_id=1) - cls.vapi.sw_interface_set_l2_bridge(cls.pg1.sw_if_index, bd_id=1) + 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, + bd_id=cls.single_tunnel_bd) + + # Setup vni 2 to test multicast flooding + cls.mcast_flood_bd = 2 + cls.create_vxlan_flood_test_bd(cls.mcast_flood_bd) + 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, + bd_id=cls.mcast_flood_bd) + + # Add and delete mcast tunnels to check stability + cls.add_mcast_load() + cls.del_mcast_load() + + # Setup vni 3 to test unicast flooding + cls.ucast_flood_bd = 3 + cls.create_vxlan_flood_test_bd(cls.ucast_flood_bd) + cls.vapi.sw_interface_set_l2_bridge(cls.pg3.sw_if_index, + bd_id=cls.ucast_flood_bd) except Exception: super(TestVxlan, cls).tearDownClass() raise @@ -97,6 +187,9 @@ class TestVxlan(BridgeDomain, VppTestCase): 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")) if __name__ == '__main__': |