Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | |
| 3 | import unittest |
| 4 | from framework import VppTestCase, VppTestRunner |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 5 | from template_bd import BridgeDomain |
| 6 | |
| 7 | from scapy.layers.l2 import Ether |
| 8 | from scapy.layers.inet import IP, UDP |
Matej Klotton | deb6984 | 2016-12-09 15:05:46 +0100 | [diff] [blame] | 9 | from scapy.layers.vxlan import VXLAN |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 10 | |
| 11 | |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 12 | class TestVxlan(BridgeDomain, VppTestCase): |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 13 | """ VXLAN Test Case """ |
| 14 | |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 15 | def __init__(self, *args): |
| 16 | BridgeDomain.__init__(self) |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 17 | VppTestCase.__init__(self, *args) |
| 18 | |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 19 | def encapsulate(self, pkt): |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 20 | """ |
| 21 | Encapsulate the original payload frame by adding VXLAN header with its |
| 22 | UDP, IP and Ethernet fields |
| 23 | """ |
| 24 | return (Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac) / |
| 25 | IP(src=self.pg0.remote_ip4, dst=self.pg0.local_ip4) / |
| 26 | UDP(sport=self.dport, dport=self.dport, chksum=0) / |
Matej Klotton | deb6984 | 2016-12-09 15:05:46 +0100 | [diff] [blame] | 27 | VXLAN(vni=self.vni, flags=self.flags) / |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 28 | pkt) |
| 29 | |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 30 | def decapsulate(self, pkt): |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 31 | """ |
| 32 | Decapsulate the original payload frame by removing VXLAN header |
| 33 | """ |
Matej Klotton | deb6984 | 2016-12-09 15:05:46 +0100 | [diff] [blame] | 34 | # check if is set I flag |
| 35 | self.assertEqual(pkt[VXLAN].flags, int('0x8', 16)) |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 36 | return pkt[VXLAN].payload |
| 37 | |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 38 | # Method for checking VXLAN encapsulation. |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 39 | # |
| 40 | def check_encapsulation(self, pkt): |
| 41 | # TODO: add error messages |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 42 | # Verify source MAC is VPP_MAC and destination MAC is MY_MAC resolved |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 43 | # by VPP using ARP. |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 44 | self.assertEqual(pkt[Ether].src, self.pg0.local_mac) |
| 45 | self.assertEqual(pkt[Ether].dst, self.pg0.remote_mac) |
| 46 | # Verify VXLAN tunnel source IP is VPP_IP and destination IP is MY_IP. |
| 47 | self.assertEqual(pkt[IP].src, self.pg0.local_ip4) |
| 48 | self.assertEqual(pkt[IP].dst, self.pg0.remote_ip4) |
| 49 | # Verify UDP destination port is VXLAN 4789, source UDP port could be |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 50 | # arbitrary. |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 51 | self.assertEqual(pkt[UDP].dport, type(self).dport) |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 52 | # TODO: checksum check |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 53 | # Verify VNI, based on configuration it must be 1. |
| 54 | self.assertEqual(pkt[VXLAN].vni, type(self).vni) |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 55 | |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 56 | # Class method to start the VXLAN test case. |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 57 | # Overrides setUpClass method in VppTestCase class. |
| 58 | # Python try..except statement is used to ensure that the tear down of |
| 59 | # the class will be executed even if exception is raised. |
| 60 | # @param cls The class pointer. |
| 61 | @classmethod |
| 62 | def setUpClass(cls): |
| 63 | super(TestVxlan, cls).setUpClass() |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 64 | |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 65 | try: |
| 66 | cls.dport = 4789 |
Matej Klotton | deb6984 | 2016-12-09 15:05:46 +0100 | [diff] [blame] | 67 | cls.flags = 0x8 |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 68 | cls.vni = 1 |
| 69 | |
| 70 | # Create 2 pg interfaces. |
| 71 | cls.create_pg_interfaces(range(2)) |
| 72 | cls.pg0.admin_up() |
| 73 | cls.pg1.admin_up() |
| 74 | |
| 75 | # Configure IPv4 addresses on VPP pg0. |
| 76 | cls.pg0.config_ip4() |
| 77 | |
| 78 | # Resolve MAC address for VPP's IP address on pg0. |
| 79 | cls.pg0.resolve_arp() |
| 80 | |
| 81 | # Create VXLAN VTEP on VPP pg0, and put vxlan_tunnel0 and pg1 |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 82 | # into BD. |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 83 | r = cls.vapi.vxlan_add_del_tunnel( |
| 84 | src_addr=cls.pg0.local_ip4n, |
| 85 | dst_addr=cls.pg0.remote_ip4n, |
| 86 | vni=cls.vni) |
| 87 | cls.vapi.sw_interface_set_l2_bridge(r.sw_if_index, bd_id=1) |
| 88 | cls.vapi.sw_interface_set_l2_bridge(cls.pg1.sw_if_index, bd_id=1) |
| 89 | except Exception: |
| 90 | super(TestVxlan, cls).tearDownClass() |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 91 | raise |
| 92 | |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 93 | # Method to define VPP actions before tear down of the test case. |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 94 | # Overrides tearDown method in VppTestCase class. |
| 95 | # @param self The object pointer. |
| 96 | def tearDown(self): |
| 97 | super(TestVxlan, self).tearDown() |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 98 | if not self.vpp_dead: |
Klement Sekera | 7bb873a | 2016-11-18 07:38:42 +0100 | [diff] [blame] | 99 | self.logger.info(self.vapi.cli("show bridge-domain 1 detail")) |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 100 | |
Matej Klotton | deb6984 | 2016-12-09 15:05:46 +0100 | [diff] [blame] | 101 | |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 102 | if __name__ == '__main__': |
| 103 | unittest.main(testRunner=VppTestRunner) |