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