blob: 845b8175b7bc16d108e447d3eaf3a7247895cd75 [file] [log] [blame]
Damjan Marionf56b77a2016-10-03 19:44:57 +02001#!/usr/bin/env python
2
Eyal Baric4aaee12016-12-20 18:36:46 +02003import socket
Damjan Marionf56b77a2016-10-03 19:44:57 +02004import unittest
5from framework import VppTestCase, VppTestRunner
Damjan Marionf56b77a2016-10-03 19:44:57 +02006from template_bd import BridgeDomain
7
8from scapy.layers.l2 import Ether
9from scapy.layers.inet import IP, UDP
Matej Klottondeb69842016-12-09 15:05:46 +010010from scapy.layers.vxlan import VXLAN
Eyal Baric4aaee12016-12-20 18:36:46 +020011from scapy.utils import atol
Damjan Marionf56b77a2016-10-03 19:44:57 +020012
13
Klement Sekeraf62ae122016-10-11 11:47:09 +020014class TestVxlan(BridgeDomain, VppTestCase):
Damjan Marionf56b77a2016-10-03 19:44:57 +020015 """ VXLAN Test Case """
16
Damjan Marionf56b77a2016-10-03 19:44:57 +020017 def __init__(self, *args):
18 BridgeDomain.__init__(self)
Damjan Marionf56b77a2016-10-03 19:44:57 +020019 VppTestCase.__init__(self, *args)
20
Eyal Baric4aaee12016-12-20 18:36:46 +020021 def encapsulate(self, pkt, vni):
Klement Sekeraf62ae122016-10-11 11:47:09 +020022 """
23 Encapsulate the original payload frame by adding VXLAN header with its
24 UDP, IP and Ethernet fields
25 """
26 return (Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac) /
27 IP(src=self.pg0.remote_ip4, dst=self.pg0.local_ip4) /
28 UDP(sport=self.dport, dport=self.dport, chksum=0) /
Eyal Baric4aaee12016-12-20 18:36:46 +020029 VXLAN(vni=vni, flags=self.flags) /
30 pkt)
31
32 def encap_mcast(self, pkt, src_ip, src_mac, vni):
33 """
34 Encapsulate the original payload frame by adding VXLAN header with its
35 UDP, IP and Ethernet fields
36 """
37 return (Ether(src=src_mac, dst=self.mcast_mac4) /
38 IP(src=src_ip, dst=self.mcast_ip4) /
39 UDP(sport=self.dport, dport=self.dport, chksum=0) /
40 VXLAN(vni=vni, flags=self.flags) /
Damjan Marionf56b77a2016-10-03 19:44:57 +020041 pkt)
42
Damjan Marionf56b77a2016-10-03 19:44:57 +020043 def decapsulate(self, pkt):
Klement Sekeraf62ae122016-10-11 11:47:09 +020044 """
45 Decapsulate the original payload frame by removing VXLAN header
46 """
Matej Klottondeb69842016-12-09 15:05:46 +010047 # check if is set I flag
48 self.assertEqual(pkt[VXLAN].flags, int('0x8', 16))
Damjan Marionf56b77a2016-10-03 19:44:57 +020049 return pkt[VXLAN].payload
50
Klement Sekeraf62ae122016-10-11 11:47:09 +020051 # Method for checking VXLAN encapsulation.
Damjan Marionf56b77a2016-10-03 19:44:57 +020052 #
Eyal Baric4aaee12016-12-20 18:36:46 +020053 def check_encapsulation(self, pkt, vni, local_only=False):
Damjan Marionf56b77a2016-10-03 19:44:57 +020054 # TODO: add error messages
Klement Sekeraf62ae122016-10-11 11:47:09 +020055 # Verify source MAC is VPP_MAC and destination MAC is MY_MAC resolved
Damjan Marionf56b77a2016-10-03 19:44:57 +020056 # by VPP using ARP.
Klement Sekeraf62ae122016-10-11 11:47:09 +020057 self.assertEqual(pkt[Ether].src, self.pg0.local_mac)
Eyal Baric4aaee12016-12-20 18:36:46 +020058 if not local_only:
59 self.assertEqual(pkt[Ether].dst, self.pg0.remote_mac)
Klement Sekeraf62ae122016-10-11 11:47:09 +020060 # Verify VXLAN tunnel source IP is VPP_IP and destination IP is MY_IP.
61 self.assertEqual(pkt[IP].src, self.pg0.local_ip4)
Eyal Baric4aaee12016-12-20 18:36:46 +020062 if not local_only:
63 self.assertEqual(pkt[IP].dst, self.pg0.remote_ip4)
Klement Sekeraf62ae122016-10-11 11:47:09 +020064 # Verify UDP destination port is VXLAN 4789, source UDP port could be
Damjan Marionf56b77a2016-10-03 19:44:57 +020065 # arbitrary.
Klement Sekeraf62ae122016-10-11 11:47:09 +020066 self.assertEqual(pkt[UDP].dport, type(self).dport)
Damjan Marionf56b77a2016-10-03 19:44:57 +020067 # TODO: checksum check
Eyal Baric4aaee12016-12-20 18:36:46 +020068 # Verify VNI
69 self.assertEqual(pkt[VXLAN].vni, vni)
70
71 @staticmethod
72 def ip4_range(ip4n, s=10, e=20):
73 base = str(bytearray(ip4n)[:3])
74 return ((base + ip) for ip in str(bytearray(range(s, e))))
75
76 @classmethod
77 def create_vxlan_flood_test_bd(cls, vni):
78 # Create 10 ucast vxlan tunnels under bd
79 ip_range_start = 10
80 ip_range_end = 20
81 next_hop_address = cls.pg0.remote_ip4n
82 for dest_addr in cls.ip4_range(next_hop_address, ip_range_start,
83 ip_range_end):
84 # add host route so dest_addr will not be resolved
85 cls.vapi.ip_add_del_route(dest_addr, 32, next_hop_address)
86 r = cls.vapi.vxlan_add_del_tunnel(
87 src_addr=cls.pg0.local_ip4n,
88 dst_addr=dest_addr,
89 vni=vni)
90 cls.vapi.sw_interface_set_l2_bridge(r.sw_if_index, bd_id=vni)
91
92 @classmethod
93 def add_del_mcast_load(cls, is_add):
94 ip_range_start = 10
95 ip_range_end = 210
96 for dest_addr in cls.ip4_range(cls.mcast_ip4n, ip_range_start,
97 ip_range_end):
98 vni = bytearray(dest_addr)[3]
99 cls.vapi.vxlan_add_del_tunnel(
100 src_addr=cls.pg0.local_ip4n,
101 dst_addr=dest_addr,
102 mcast_sw_if_index=1,
103 vni=vni,
104 is_add=is_add)
105
106 @classmethod
107 def add_mcast_load(cls):
108 cls.add_del_mcast_load(is_add=1)
109
110 @classmethod
111 def del_mcast_load(cls):
112 cls.add_del_mcast_load(is_add=0)
Damjan Marionf56b77a2016-10-03 19:44:57 +0200113
Klement Sekeraf62ae122016-10-11 11:47:09 +0200114 # Class method to start the VXLAN test case.
Damjan Marionf56b77a2016-10-03 19:44:57 +0200115 # Overrides setUpClass method in VppTestCase class.
116 # Python try..except statement is used to ensure that the tear down of
117 # the class will be executed even if exception is raised.
118 # @param cls The class pointer.
119 @classmethod
120 def setUpClass(cls):
121 super(TestVxlan, cls).setUpClass()
Damjan Marionf56b77a2016-10-03 19:44:57 +0200122
Klement Sekeraf62ae122016-10-11 11:47:09 +0200123 try:
124 cls.dport = 4789
Matej Klottondeb69842016-12-09 15:05:46 +0100125 cls.flags = 0x8
Klement Sekeraf62ae122016-10-11 11:47:09 +0200126
127 # Create 2 pg interfaces.
Eyal Baric4aaee12016-12-20 18:36:46 +0200128 cls.create_pg_interfaces(range(4))
129 for pg in cls.pg_interfaces:
130 pg.admin_up()
Klement Sekeraf62ae122016-10-11 11:47:09 +0200131
132 # Configure IPv4 addresses on VPP pg0.
133 cls.pg0.config_ip4()
134
135 # Resolve MAC address for VPP's IP address on pg0.
136 cls.pg0.resolve_arp()
137
Eyal Baric4aaee12016-12-20 18:36:46 +0200138 # Our Multicast address
139 cls.mcast_ip4 = '239.1.1.1'
140 cls.mcast_ip4n = socket.inet_pton(socket.AF_INET, cls.mcast_ip4)
141 iplong = atol(cls.mcast_ip4)
142 cls.mcast_mac4 = "01:00:5e:%02x:%02x:%02x" % (
143 (iplong >> 16) & 0x7F, (iplong >> 8) & 0xFF, iplong & 0xFF)
144
Klement Sekeraf62ae122016-10-11 11:47:09 +0200145 # Create VXLAN VTEP on VPP pg0, and put vxlan_tunnel0 and pg1
Damjan Marionf56b77a2016-10-03 19:44:57 +0200146 # into BD.
Eyal Baric4aaee12016-12-20 18:36:46 +0200147 cls.single_tunnel_bd = 1
Klement Sekeraf62ae122016-10-11 11:47:09 +0200148 r = cls.vapi.vxlan_add_del_tunnel(
149 src_addr=cls.pg0.local_ip4n,
150 dst_addr=cls.pg0.remote_ip4n,
Eyal Baric4aaee12016-12-20 18:36:46 +0200151 vni=cls.single_tunnel_bd)
152 cls.vapi.sw_interface_set_l2_bridge(r.sw_if_index,
153 bd_id=cls.single_tunnel_bd)
154 cls.vapi.sw_interface_set_l2_bridge(cls.pg1.sw_if_index,
155 bd_id=cls.single_tunnel_bd)
156
157 # Setup vni 2 to test multicast flooding
158 cls.mcast_flood_bd = 2
159 cls.create_vxlan_flood_test_bd(cls.mcast_flood_bd)
160 r = cls.vapi.vxlan_add_del_tunnel(
161 src_addr=cls.pg0.local_ip4n,
162 dst_addr=cls.mcast_ip4n,
163 mcast_sw_if_index=1,
164 vni=cls.mcast_flood_bd)
165 cls.vapi.sw_interface_set_l2_bridge(r.sw_if_index,
166 bd_id=cls.mcast_flood_bd)
167 cls.vapi.sw_interface_set_l2_bridge(cls.pg2.sw_if_index,
168 bd_id=cls.mcast_flood_bd)
169
170 # Add and delete mcast tunnels to check stability
171 cls.add_mcast_load()
172 cls.del_mcast_load()
173
174 # Setup vni 3 to test unicast flooding
175 cls.ucast_flood_bd = 3
176 cls.create_vxlan_flood_test_bd(cls.ucast_flood_bd)
177 cls.vapi.sw_interface_set_l2_bridge(cls.pg3.sw_if_index,
178 bd_id=cls.ucast_flood_bd)
Klement Sekeraf62ae122016-10-11 11:47:09 +0200179 except Exception:
180 super(TestVxlan, cls).tearDownClass()
Damjan Marionf56b77a2016-10-03 19:44:57 +0200181 raise
182
Klement Sekeraf62ae122016-10-11 11:47:09 +0200183 # Method to define VPP actions before tear down of the test case.
Damjan Marionf56b77a2016-10-03 19:44:57 +0200184 # Overrides tearDown method in VppTestCase class.
185 # @param self The object pointer.
186 def tearDown(self):
187 super(TestVxlan, self).tearDown()
Klement Sekeraf62ae122016-10-11 11:47:09 +0200188 if not self.vpp_dead:
Klement Sekera7bb873a2016-11-18 07:38:42 +0100189 self.logger.info(self.vapi.cli("show bridge-domain 1 detail"))
Eyal Baric4aaee12016-12-20 18:36:46 +0200190 self.logger.info(self.vapi.cli("show bridge-domain 2 detail"))
191 self.logger.info(self.vapi.cli("show bridge-domain 3 detail"))
192 self.logger.info(self.vapi.cli("show vxlan tunnel"))
Damjan Marionf56b77a2016-10-03 19:44:57 +0200193
Matej Klottondeb69842016-12-09 15:05:46 +0100194
Damjan Marionf56b77a2016-10-03 19:44:57 +0200195if __name__ == '__main__':
196 unittest.main(testRunner=VppTestRunner)