test_vxlan_gpe.py revision ead1e536
1#!/usr/bin/env python3
2
3import socket
4from util import ip4n_range, ip4_range
5import unittest
6from framework import VppTestCase, VppTestRunner, running_extended_tests
7from template_bd import BridgeDomain
8
9from scapy.layers.l2 import Ether, Raw
10from scapy.layers.inet import IP, UDP
11from scapy.layers.vxlan import VXLAN
12from scapy.utils import atol
13from vpp_ip_route import VppIpRoute, VppRoutePath
14from vpp_ip import INVALID_INDEX
15
16
17@unittest.skipUnless(running_extended_tests, "part of extended tests")
18class TestVxlanGpe(BridgeDomain, VppTestCase):
19    """ VXLAN-GPE Test Case """
20
21    def __init__(self, *args):
22        BridgeDomain.__init__(self)
23        VppTestCase.__init__(self, *args)
24
25    def encapsulate(self, pkt, vni):
26        """
27        Encapsulate the original payload frame by adding VXLAN-GPE header
28        with its UDP, IP and Ethernet fields
29        """
30        return (Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac) /
31                IP(src=self.pg0.remote_ip4, dst=self.pg0.local_ip4) /
32                UDP(sport=self.dport, dport=self.dport, chksum=0) /
33                VXLAN(vni=vni, flags=self.flags) /
34                pkt)
35
36    def ip_range(self, start, end):
37        """ range of remote ip's """
38        return ip4_range(self.pg0.remote_ip4, start, end)
39
40    def encap_mcast(self, pkt, src_ip, src_mac, vni):
41        """
42        Encapsulate the original payload frame by adding VXLAN-GPE header
43        with its UDP, IP and Ethernet fields
44        """
45        return (Ether(src=src_mac, dst=self.mcast_mac) /
46                IP(src=src_ip, dst=self.mcast_ip4) /
47                UDP(sport=self.dport, dport=self.dport, chksum=0) /
48                VXLAN(vni=vni, flags=self.flags) /
49                pkt)
50
51    def decapsulate(self, pkt):
52        """
53        Decapsulate the original payload frame by removing VXLAN-GPE header
54        """
55        # check if is set I and P flag
56        self.assertEqual(pkt[VXLAN].flags, 0x0c)
57        return pkt[VXLAN].payload
58
59    # Method for checking VXLAN-GPE encapsulation.
60    #
61    def check_encapsulation(self, pkt, vni, local_only=False, mcast_pkt=False):
62        # Verify source MAC is VPP_MAC and destination MAC is MY_MAC resolved
63        #  by VPP using ARP.
64        self.assertEqual(pkt[Ether].src, self.pg0.local_mac)
65        if not local_only:
66            if not mcast_pkt:
67                self.assertEqual(pkt[Ether].dst, self.pg0.remote_mac)
68            else:
69                self.assertEqual(pkt[Ether].dst, type(self).mcast_mac)
70        # Verify VXLAN-GPE tunnel src IP is VPP_IP and dst IP is MY_IP.
71        self.assertEqual(pkt[IP].src, self.pg0.local_ip4)
72        if not local_only:
73            if not mcast_pkt:
74                self.assertEqual(pkt[IP].dst, self.pg0.remote_ip4)
75            else:
76                self.assertEqual(pkt[IP].dst, type(self).mcast_ip4)
77        # Verify UDP destination port is VXLAN-GPE 4790, source UDP port
78        #  could be arbitrary.
79        self.assertEqual(pkt[UDP].dport, type(self).dport)
80        # Verify VNI
81        self.assertEqual(pkt[VXLAN].vni, vni)
82
83    @classmethod
84    def create_vxlan_gpe_flood_test_bd(cls, vni, n_ucast_tunnels):
85        # Create 10 ucast vxlan tunnels under bd
86        ip_range_start = 10
87        ip_range_end = ip_range_start + n_ucast_tunnels
88        next_hop_address = cls.pg0.remote_ip4
89        for dest_ip4 in ip4_range(next_hop_address, ip_range_start,
90                                  ip_range_end):
91            # add host route so dest_ip4n will not be resolved
92            rip = VppIpRoute(cls, dest_ip4, 32,
93                             [VppRoutePath(next_hop_address,
94                                           INVALID_INDEX)],
95                             register=False)
96            rip.add_vpp_config()
97            dest_ip4n = socket.inet_pton(socket.AF_INET, dest_ip4)
98
99            r = cls.vapi.vxlan_gpe_add_del_tunnel(
100                src_addr=cls.pg0.local_ip4n,
101                dst_addr=dest_ip4n,
102                vni=vni)
103            cls.vapi.sw_interface_set_l2_bridge(rx_sw_if_index=r.sw_if_index,
104                                                bd_id=vni)
105
106    @classmethod
107    def add_del_shared_mcast_dst_load(cls, is_add):
108        """
109        add or del tunnels sharing the same mcast dst
110        to test vxlan_gpe ref_count mechanism
111        """
112        n_shared_dst_tunnels = 20
113        vni_start = 1000
114        vni_end = vni_start + n_shared_dst_tunnels
115        for vni in range(vni_start, vni_end):
116            r = cls.vapi.vxlan_gpe_add_del_tunnel(
117                src_addr=cls.pg0.local_ip4n,
118                dst_addr=cls.mcast_ip4n,
119                mcast_sw_if_index=1,
120                vni=vni,
121                is_add=is_add)
122            if r.sw_if_index == 0xffffffff:
123                raise ValueError("bad sw_if_index: ~0")
124
125    @classmethod
126    def add_shared_mcast_dst_load(cls):
127        cls.add_del_shared_mcast_dst_load(is_add=1)
128
129    @classmethod
130    def del_shared_mcast_dst_load(cls):
131        cls.add_del_shared_mcast_dst_load(is_add=0)
132
133    @classmethod
134    def add_del_mcast_tunnels_load(cls, is_add):
135        """
136        add or del tunnels to test vxlan_gpe stability
137        """
138        n_distinct_dst_tunnels = 20
139        ip_range_start = 10
140        ip_range_end = ip_range_start + n_distinct_dst_tunnels
141        for dest_ip4n in ip4n_range(cls.mcast_ip4n, ip_range_start,
142                                    ip_range_end):
143            vni = bytearray(dest_ip4n)[3]
144            cls.vapi.vxlan_gpe_add_del_tunnel(
145                src_addr=cls.pg0.local_ip4n,
146                dst_addr=dest_ip4n,
147                mcast_sw_if_index=1,
148                vni=vni,
149                is_add=is_add)
150
151    @classmethod
152    def add_mcast_tunnels_load(cls):
153        cls.add_del_mcast_tunnels_load(is_add=1)
154
155    @classmethod
156    def del_mcast_tunnels_load(cls):
157        cls.add_del_mcast_tunnels_load(is_add=0)
158
159    # Class method to start the VXLAN-GPE test case.
160    #  Overrides setUpClass method in VppTestCase class.
161    #  Python try..except statement is used to ensure that the tear down of
162    #  the class will be executed even if exception is raised.
163    #  @param cls The class pointer.
164    @classmethod
165    def setUpClass(cls):
166        super(TestVxlanGpe, cls).setUpClass()
167
168        try:
169            cls.dport = 4790
170            cls.flags = 0x0c
171
172            # Create 2 pg interfaces.
173            cls.create_pg_interfaces(range(4))
174            for pg in cls.pg_interfaces:
175                pg.admin_up()
176
177            # Configure IPv4 addresses on VPP pg0.
178            cls.pg0.config_ip4()
179
180            # Resolve MAC address for VPP's IP address on pg0.
181            cls.pg0.resolve_arp()
182
183            # Our Multicast address
184            cls.mcast_ip4 = '239.1.1.1'
185            cls.mcast_ip4n = socket.inet_pton(socket.AF_INET, cls.mcast_ip4)
186            iplong = atol(cls.mcast_ip4)
187            cls.mcast_mac = "01:00:5e:%02x:%02x:%02x" % (
188                (iplong >> 16) & 0x7F, (iplong >> 8) & 0xFF, iplong & 0xFF)
189
190            # Create VXLAN-GPE VTEP on VPP pg0, and put vxlan_gpe_tunnel0
191            # and pg1 into BD.
192            cls.single_tunnel_bd = 11
193            r = cls.vapi.vxlan_gpe_add_del_tunnel(
194                src_addr=cls.pg0.local_ip4n,
195                dst_addr=cls.pg0.remote_ip4n,
196                vni=cls.single_tunnel_bd)
197            cls.vapi.sw_interface_set_l2_bridge(rx_sw_if_index=r.sw_if_index,
198                                                bd_id=cls.single_tunnel_bd)
199            cls.vapi.sw_interface_set_l2_bridge(
200                rx_sw_if_index=cls.pg1.sw_if_index, bd_id=cls.single_tunnel_bd)
201
202            # Setup vni 2 to test multicast flooding
203            cls.n_ucast_tunnels = 10
204            cls.mcast_flood_bd = 12
205            cls.create_vxlan_gpe_flood_test_bd(cls.mcast_flood_bd,
206                                               cls.n_ucast_tunnels)
207            r = cls.vapi.vxlan_gpe_add_del_tunnel(
208                src_addr=cls.pg0.local_ip4n,
209                dst_addr=cls.mcast_ip4n,
210                mcast_sw_if_index=1,
211                vni=cls.mcast_flood_bd)
212            cls.vapi.sw_interface_set_l2_bridge(rx_sw_if_index=r.sw_if_index,
213                                                bd_id=cls.mcast_flood_bd)
214            cls.vapi.sw_interface_set_l2_bridge(
215                rx_sw_if_index=cls.pg2.sw_if_index, bd_id=cls.mcast_flood_bd)
216
217            # Add and delete mcast tunnels to check stability
218            cls.add_shared_mcast_dst_load()
219            cls.add_mcast_tunnels_load()
220            cls.del_shared_mcast_dst_load()
221            cls.del_mcast_tunnels_load()
222
223            # Setup vni 3 to test unicast flooding
224            cls.ucast_flood_bd = 13
225            cls.create_vxlan_gpe_flood_test_bd(cls.ucast_flood_bd,
226                                               cls.n_ucast_tunnels)
227            cls.vapi.sw_interface_set_l2_bridge(
228                rx_sw_if_index=cls.pg3.sw_if_index, bd_id=cls.ucast_flood_bd)
229        except Exception:
230            super(TestVxlanGpe, cls).tearDownClass()
231            raise
232
233    @classmethod
234    def tearDownClass(cls):
235        super(TestVxlanGpe, cls).tearDownClass()
236
237    @unittest.skip("test disabled for vxlan-gpe")
238    def test_mcast_flood(self):
239        """ inherited from BridgeDomain """
240        pass
241
242    @unittest.skip("test disabled for vxlan-gpe")
243    def test_mcast_rcv(self):
244        """ inherited from BridgeDomain """
245        pass
246
247    # Method to define VPP actions before tear down of the test case.
248    #  Overrides tearDown method in VppTestCase class.
249    #  @param self The object pointer.
250    def tearDown(self):
251        super(TestVxlanGpe, self).tearDown()
252
253    def show_commands_at_teardown(self):
254        self.logger.info(self.vapi.cli("show bridge-domain 11 detail"))
255        self.logger.info(self.vapi.cli("show bridge-domain 12 detail"))
256        self.logger.info(self.vapi.cli("show bridge-domain 13 detail"))
257        self.logger.info(self.vapi.cli("show int"))
258        self.logger.info(self.vapi.cli("show vxlan-gpe"))
259        self.logger.info(self.vapi.cli("show trace"))
260
261
262if __name__ == '__main__':
263    unittest.main(testRunner=VppTestRunner)
264