vxlan unit test - minor fixes
[vpp.git] / test / test_vxlan.py
1 #!/usr/bin/env python
2
3 import socket
4 from util import ip4n_range
5 import unittest
6 from framework import VppTestCase, VppTestRunner
7 from template_bd import BridgeDomain
8
9 from scapy.layers.l2 import Ether
10 from scapy.layers.inet import IP, UDP
11 from scapy.layers.vxlan import VXLAN
12 from scapy.utils import atol
13
14
15 class TestVxlan(BridgeDomain, VppTestCase):
16     """ VXLAN Test Case """
17
18     def __init__(self, *args):
19         BridgeDomain.__init__(self)
20         VppTestCase.__init__(self, *args)
21
22     def encapsulate(self, pkt, vni):
23         """
24         Encapsulate the original payload frame by adding VXLAN header with its
25         UDP, IP and Ethernet fields
26         """
27         return (Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac) /
28                 IP(src=self.pg0.remote_ip4, dst=self.pg0.local_ip4) /
29                 UDP(sport=self.dport, dport=self.dport, chksum=0) /
30                 VXLAN(vni=vni, flags=self.flags) /
31                 pkt)
32
33     def encap_mcast(self, pkt, src_ip, src_mac, vni):
34         """
35         Encapsulate the original payload frame by adding VXLAN header with its
36         UDP, IP and Ethernet fields
37         """
38         return (Ether(src=src_mac, dst=self.mcast_mac) /
39                 IP(src=src_ip, dst=self.mcast_ip4) /
40                 UDP(sport=self.dport, dport=self.dport, chksum=0) /
41                 VXLAN(vni=vni, flags=self.flags) /
42                 pkt)
43
44     def decapsulate(self, pkt):
45         """
46         Decapsulate the original payload frame by removing VXLAN header
47         """
48         # check if is set I flag
49         self.assertEqual(pkt[VXLAN].flags, int('0x8', 16))
50         return pkt[VXLAN].payload
51
52     # Method for checking VXLAN encapsulation.
53     #
54     def check_encapsulation(self, pkt, vni, local_only=False):
55         # TODO: add error messages
56         # Verify source MAC is VPP_MAC and destination MAC is MY_MAC resolved
57         #  by VPP using ARP.
58         self.assertEqual(pkt[Ether].src, self.pg0.local_mac)
59         if not local_only:
60             self.assertEqual(pkt[Ether].dst, self.pg0.remote_mac)
61         # Verify VXLAN tunnel source IP is VPP_IP and destination IP is MY_IP.
62         self.assertEqual(pkt[IP].src, self.pg0.local_ip4)
63         if not local_only:
64             self.assertEqual(pkt[IP].dst, self.pg0.remote_ip4)
65         # Verify UDP destination port is VXLAN 4789, source UDP port could be
66         #  arbitrary.
67         self.assertEqual(pkt[UDP].dport, type(self).dport)
68         # TODO: checksum check
69         # Verify VNI
70         self.assertEqual(pkt[VXLAN].vni, vni)
71
72     @classmethod
73     def create_vxlan_flood_test_bd(cls, vni, n_ucast_tunnels):
74         # Create 10 ucast vxlan tunnels under bd
75         ip_range_start = 10
76         ip_range_end = ip_range_start + n_ucast_tunnels
77         next_hop_address = cls.pg0.remote_ip4n
78         for dest_ip4n in ip4n_range(next_hop_address, ip_range_start,
79                                     ip_range_end):
80             # add host route so dest_ip4n will not be resolved
81             cls.vapi.ip_add_del_route(dest_ip4n, 32, next_hop_address)
82             r = cls.vapi.vxlan_add_del_tunnel(
83                 src_addr=cls.pg0.local_ip4n,
84                 dst_addr=dest_ip4n,
85                 vni=vni)
86             cls.vapi.sw_interface_set_l2_bridge(r.sw_if_index, bd_id=vni)
87
88     @classmethod
89     def add_del_mcast_load(cls, is_add):
90         ip_range_start = 10
91         ip_range_end = 210
92         for dest_ip4n in ip4n_range(cls.mcast_ip4n, ip_range_start,
93                                     ip_range_end):
94             vni = bytearray(dest_ip4n)[3]
95             cls.vapi.vxlan_add_del_tunnel(
96                 src_addr=cls.pg0.local_ip4n,
97                 dst_addr=dest_ip4n,
98                 mcast_sw_if_index=1,
99                 vni=vni,
100                 is_add=is_add)
101
102     @classmethod
103     def add_mcast_load(cls):
104         cls.add_del_mcast_load(is_add=1)
105
106     @classmethod
107     def del_mcast_load(cls):
108         cls.add_del_mcast_load(is_add=0)
109
110     # Class method to start the VXLAN test case.
111     #  Overrides setUpClass method in VppTestCase class.
112     #  Python try..except statement is used to ensure that the tear down of
113     #  the class will be executed even if exception is raised.
114     #  @param cls The class pointer.
115     @classmethod
116     def setUpClass(cls):
117         super(TestVxlan, cls).setUpClass()
118
119         try:
120             cls.dport = 4789
121             cls.flags = 0x8
122
123             # Create 2 pg interfaces.
124             cls.create_pg_interfaces(range(4))
125             for pg in cls.pg_interfaces:
126                 pg.admin_up()
127
128             # Configure IPv4 addresses on VPP pg0.
129             cls.pg0.config_ip4()
130
131             # Resolve MAC address for VPP's IP address on pg0.
132             cls.pg0.resolve_arp()
133
134             # Our Multicast address
135             cls.mcast_ip4 = '239.1.1.1'
136             cls.mcast_ip4n = socket.inet_pton(socket.AF_INET, cls.mcast_ip4)
137             iplong = atol(cls.mcast_ip4)
138             cls.mcast_mac = "01:00:5e:%02x:%02x:%02x" % (
139                 (iplong >> 16) & 0x7F, (iplong >> 8) & 0xFF, iplong & 0xFF)
140
141             # Create VXLAN VTEP on VPP pg0, and put vxlan_tunnel0 and pg1
142             #  into BD.
143             cls.single_tunnel_bd = 1
144             r = cls.vapi.vxlan_add_del_tunnel(
145                 src_addr=cls.pg0.local_ip4n,
146                 dst_addr=cls.pg0.remote_ip4n,
147                 vni=cls.single_tunnel_bd)
148             cls.vapi.sw_interface_set_l2_bridge(r.sw_if_index,
149                                                 bd_id=cls.single_tunnel_bd)
150             cls.vapi.sw_interface_set_l2_bridge(cls.pg1.sw_if_index,
151                                                 bd_id=cls.single_tunnel_bd)
152
153             # Setup vni 2 to test multicast flooding
154             cls.n_ucast_tunnels = 10
155             cls.mcast_flood_bd = 2
156             cls.create_vxlan_flood_test_bd(cls.mcast_flood_bd,
157                                            cls.n_ucast_tunnels)
158             r = cls.vapi.vxlan_add_del_tunnel(
159                 src_addr=cls.pg0.local_ip4n,
160                 dst_addr=cls.mcast_ip4n,
161                 mcast_sw_if_index=1,
162                 vni=cls.mcast_flood_bd)
163             cls.vapi.sw_interface_set_l2_bridge(r.sw_if_index,
164                                                 bd_id=cls.mcast_flood_bd)
165             cls.vapi.sw_interface_set_l2_bridge(cls.pg2.sw_if_index,
166                                                 bd_id=cls.mcast_flood_bd)
167
168             # Add and delete mcast tunnels to check stability
169             cls.add_mcast_load()
170             cls.del_mcast_load()
171
172             # Setup vni 3 to test unicast flooding
173             cls.ucast_flood_bd = 3
174             cls.create_vxlan_flood_test_bd(cls.ucast_flood_bd,
175                                            cls.n_ucast_tunnels)
176             cls.vapi.sw_interface_set_l2_bridge(cls.pg3.sw_if_index,
177                                                 bd_id=cls.ucast_flood_bd)
178         except Exception:
179             super(TestVxlan, cls).tearDownClass()
180             raise
181
182     # Method to define VPP actions before tear down of the test case.
183     #  Overrides tearDown method in VppTestCase class.
184     #  @param self The object pointer.
185     def tearDown(self):
186         super(TestVxlan, self).tearDown()
187         if not self.vpp_dead:
188             self.logger.info(self.vapi.cli("show bridge-domain 1 detail"))
189             self.logger.info(self.vapi.cli("show bridge-domain 2 detail"))
190             self.logger.info(self.vapi.cli("show bridge-domain 3 detail"))
191             self.logger.info(self.vapi.cli("show vxlan tunnel"))
192
193
194 if __name__ == '__main__':
195     unittest.main(testRunner=VppTestRunner)