blob: 1978cf0c0184d7e2200f7cfcbf8f6fd00d528517 [file] [log] [blame]
Damjan Marionf56b77a2016-10-03 19:44:57 +02001#!/usr/bin/env python
2
3import unittest
4from framework import VppTestCase, VppTestRunner
Damjan Marionf56b77a2016-10-03 19:44:57 +02005from template_bd import BridgeDomain
6
7from scapy.layers.l2 import Ether
8from scapy.layers.inet import IP, UDP
Matej Klottondeb69842016-12-09 15:05:46 +01009from scapy.layers.vxlan import VXLAN
Damjan Marionf56b77a2016-10-03 19:44:57 +020010
11
Klement Sekeraf62ae122016-10-11 11:47:09 +020012class TestVxlan(BridgeDomain, VppTestCase):
Damjan Marionf56b77a2016-10-03 19:44:57 +020013 """ VXLAN Test Case """
14
Damjan Marionf56b77a2016-10-03 19:44:57 +020015 def __init__(self, *args):
16 BridgeDomain.__init__(self)
Damjan Marionf56b77a2016-10-03 19:44:57 +020017 VppTestCase.__init__(self, *args)
18
Damjan Marionf56b77a2016-10-03 19:44:57 +020019 def encapsulate(self, pkt):
Klement Sekeraf62ae122016-10-11 11:47:09 +020020 """
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 Klottondeb69842016-12-09 15:05:46 +010027 VXLAN(vni=self.vni, flags=self.flags) /
Damjan Marionf56b77a2016-10-03 19:44:57 +020028 pkt)
29
Damjan Marionf56b77a2016-10-03 19:44:57 +020030 def decapsulate(self, pkt):
Klement Sekeraf62ae122016-10-11 11:47:09 +020031 """
32 Decapsulate the original payload frame by removing VXLAN header
33 """
Matej Klottondeb69842016-12-09 15:05:46 +010034 # check if is set I flag
35 self.assertEqual(pkt[VXLAN].flags, int('0x8', 16))
Damjan Marionf56b77a2016-10-03 19:44:57 +020036 return pkt[VXLAN].payload
37
Klement Sekeraf62ae122016-10-11 11:47:09 +020038 # Method for checking VXLAN encapsulation.
Damjan Marionf56b77a2016-10-03 19:44:57 +020039 #
40 def check_encapsulation(self, pkt):
41 # TODO: add error messages
Klement Sekeraf62ae122016-10-11 11:47:09 +020042 # Verify source MAC is VPP_MAC and destination MAC is MY_MAC resolved
Damjan Marionf56b77a2016-10-03 19:44:57 +020043 # by VPP using ARP.
Klement Sekeraf62ae122016-10-11 11:47:09 +020044 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 Marionf56b77a2016-10-03 19:44:57 +020050 # arbitrary.
Klement Sekeraf62ae122016-10-11 11:47:09 +020051 self.assertEqual(pkt[UDP].dport, type(self).dport)
Damjan Marionf56b77a2016-10-03 19:44:57 +020052 # TODO: checksum check
Klement Sekeraf62ae122016-10-11 11:47:09 +020053 # Verify VNI, based on configuration it must be 1.
54 self.assertEqual(pkt[VXLAN].vni, type(self).vni)
Damjan Marionf56b77a2016-10-03 19:44:57 +020055
Klement Sekeraf62ae122016-10-11 11:47:09 +020056 # Class method to start the VXLAN test case.
Damjan Marionf56b77a2016-10-03 19:44:57 +020057 # 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 Marionf56b77a2016-10-03 19:44:57 +020064
Klement Sekeraf62ae122016-10-11 11:47:09 +020065 try:
66 cls.dport = 4789
Matej Klottondeb69842016-12-09 15:05:46 +010067 cls.flags = 0x8
Klement Sekeraf62ae122016-10-11 11:47:09 +020068 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 Marionf56b77a2016-10-03 19:44:57 +020082 # into BD.
Klement Sekeraf62ae122016-10-11 11:47:09 +020083 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 Marionf56b77a2016-10-03 19:44:57 +020091 raise
92
Klement Sekeraf62ae122016-10-11 11:47:09 +020093 # Method to define VPP actions before tear down of the test case.
Damjan Marionf56b77a2016-10-03 19:44:57 +020094 # Overrides tearDown method in VppTestCase class.
95 # @param self The object pointer.
96 def tearDown(self):
97 super(TestVxlan, self).tearDown()
Klement Sekeraf62ae122016-10-11 11:47:09 +020098 if not self.vpp_dead:
Klement Sekera7bb873a2016-11-18 07:38:42 +010099 self.logger.info(self.vapi.cli("show bridge-domain 1 detail"))
Damjan Marionf56b77a2016-10-03 19:44:57 +0200100
Matej Klottondeb69842016-12-09 15:05:46 +0100101
Damjan Marionf56b77a2016-10-03 19:44:57 +0200102if __name__ == '__main__':
103 unittest.main(testRunner=VppTestRunner)