blob: 4053fadff8bb5c474a44ead444eeca4251895b4b [file] [log] [blame]
Eyal Baricef1e2a2018-06-18 13:01:59 +03001#!/usr/bin/env python
2
3import socket
4import unittest
5from framework import VppTestCase, VppTestRunner
6from template_bd import BridgeDomain
7
8from scapy.layers.l2 import Ether
9from scapy.layers.inet6 import IPv6, UDP
10from scapy.layers.vxlan import VXLAN
11from scapy.utils import atol
12
13
Eyal Baridd47eca2018-07-08 08:15:56 +030014class TestVxlan6(BridgeDomain, VppTestCase):
15 """ VXLAN over IPv6 Test Case """
Eyal Baricef1e2a2018-06-18 13:01:59 +030016
17 def __init__(self, *args):
18 BridgeDomain.__init__(self)
19 VppTestCase.__init__(self, *args)
20
21 def encapsulate(self, pkt, vni):
22 """
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 IPv6(src=self.pg0.remote_ip6, dst=self.pg0.local_ip6) /
28 UDP(sport=self.dport, dport=self.dport, chksum=0) /
29 VXLAN(vni=vni, flags=self.flags) /
30 pkt)
31
32 @classmethod
33 def ip_range(cls, s, e):
34 """ range of remote ip's """
35 tmp = cls.pg0.remote_ip6.rsplit(':', 1)[0]
36 return ("%s:%x" % (tmp, i) for i in range(s, e))
37
38 def encap_mcast(self, pkt, src_ip, src_mac, vni):
39 """
40 Encapsulate the original payload frame by adding VXLAN header with its
41 UDP, IP and Ethernet fields
42 """
43 return (Ether(src=src_mac, dst=self.mcast_mac) /
44 IPv6(src=src_ip, dst=self.mcast_ip6) /
45 UDP(sport=self.dport, dport=self.dport, chksum=0) /
46 VXLAN(vni=vni, flags=self.flags) /
47 pkt)
48
49 def decapsulate(self, pkt):
50 """
51 Decapsulate the original payload frame by removing VXLAN header
52 """
53 # check if is set I flag
54 self.assertEqual(pkt[VXLAN].flags, int('0x8', 16))
55 return pkt[VXLAN].payload
56
57 # Method for checking VXLAN encapsulation.
58 #
59 def check_encapsulation(self, pkt, vni, local_only=False, mcast_pkt=False):
60 # TODO: add error messages
61 # Verify source MAC is VPP_MAC and destination MAC is MY_MAC resolved
62 # by VPP using ARP.
63 self.assertEqual(pkt[Ether].src, self.pg0.local_mac)
64 if not local_only:
65 if not mcast_pkt:
66 self.assertEqual(pkt[Ether].dst, self.pg0.remote_mac)
67 else:
68 self.assertEqual(pkt[Ether].dst, type(self).mcast_mac)
69 # Verify VXLAN tunnel source IP is VPP_IP and destination IP is MY_IP.
70 self.assertEqual(pkt[IPv6].src, self.pg0.local_ip6)
71 if not local_only:
72 if not mcast_pkt:
73 self.assertEqual(pkt[IPv6].dst, self.pg0.remote_ip6)
74 else:
75 self.assertEqual(pkt[IPv6].dst, type(self).mcast_ip6)
76 # Verify UDP destination port is VXLAN 4789, source UDP port could be
77 # arbitrary.
78 self.assertEqual(pkt[UDP].dport, type(self).dport)
79 # TODO: checksum check
80 # Verify VNI
81 self.assertEqual(pkt[VXLAN].vni, vni)
82
83 @classmethod
84 def create_vxlan_flood_test_bd(cls, vni, n_ucast_tunnels):
85 # Create 10 ucast vxlan tunnels under bd
86 start = 10
87 end = start + n_ucast_tunnels
88 next_hop = cls.pg0.remote_ip6n
89 for dest_ip6 in cls.ip_range(start, end):
90 dest_ip6n = socket.inet_pton(socket.AF_INET6, dest_ip6)
91 # add host route so dest ip will not be resolved
Ole Troana5b2eec2019-03-11 19:23:25 +010092 cls.vapi.ip_add_del_route(dst_address=dest_ip6n,
93 dst_address_length=128,
94 next_hop_address=next_hop, is_ipv6=1)
95 r = cls.vapi.vxlan_add_del_tunnel(src_address=cls.pg0.local_ip6n,
96 dst_address=dest_ip6n, is_ipv6=1,
97 vni=vni)
98 cls.vapi.sw_interface_set_l2_bridge(rx_sw_if_index=r.sw_if_index,
99 bd_id=vni)
Eyal Baricef1e2a2018-06-18 13:01:59 +0300100
101 @classmethod
102 def add_mcast_tunnels_load(cls):
103 cls.add_del_mcast_tunnels_load(is_add=1)
104
105 @classmethod
106 def del_mcast_tunnels_load(cls):
107 cls.add_del_mcast_tunnels_load(is_add=0)
108
109 # Class method to start the VXLAN test case.
110 # Overrides setUpClass method in VppTestCase class.
111 # Python try..except statement is used to ensure that the tear down of
112 # the class will be executed even if exception is raised.
113 # @param cls The class pointer.
114 @classmethod
115 def setUpClass(cls):
Eyal Baridd47eca2018-07-08 08:15:56 +0300116 super(TestVxlan6, cls).setUpClass()
Eyal Baricef1e2a2018-06-18 13:01:59 +0300117
118 try:
119 cls.dport = 4789
120 cls.flags = 0x8
121
122 # Create 2 pg interfaces.
123 cls.create_pg_interfaces(range(4))
124 for pg in cls.pg_interfaces:
125 pg.admin_up()
126
127 # Configure IPv4 addresses on VPP pg0.
128 cls.pg0.config_ip6()
129
130 # Resolve MAC address for VPP's IP address on pg0.
131 cls.pg0.resolve_ndp()
132
133 cls.mcast_ip6 = 'ff0e::1'
134 cls.mcast_ip6n = socket.inet_pton(socket.AF_INET6, cls.mcast_ip6)
135 cls.mcast_mac = "33:33:00:00:00:%02x" % (1)
136
137 # Create VXLAN VTEP on VPP pg0, and put vxlan_tunnel0 and pg1
138 # into BD.
139 cls.single_tunnel_bd = 1
Ole Troana5b2eec2019-03-11 19:23:25 +0100140 r = cls.vapi.vxlan_add_del_tunnel(src_address=cls.pg0.local_ip6n,
141 dst_address=cls.pg0.remote_ip6n,
142 is_ipv6=1,
143 vni=cls.single_tunnel_bd)
144 cls.vapi.sw_interface_set_l2_bridge(rx_sw_if_index=r.sw_if_index,
Eyal Baricef1e2a2018-06-18 13:01:59 +0300145 bd_id=cls.single_tunnel_bd)
Ole Troana5b2eec2019-03-11 19:23:25 +0100146 cls.vapi.sw_interface_set_l2_bridge(
147 rx_sw_if_index=cls.pg1.sw_if_index, bd_id=cls.single_tunnel_bd)
Eyal Baricef1e2a2018-06-18 13:01:59 +0300148
149 # Setup vni 2 to test multicast flooding
150 cls.n_ucast_tunnels = 10
151 cls.mcast_flood_bd = 2
152 cls.create_vxlan_flood_test_bd(cls.mcast_flood_bd,
153 cls.n_ucast_tunnels)
Ole Troana5b2eec2019-03-11 19:23:25 +0100154 r = cls.vapi.vxlan_add_del_tunnel(src_address=cls.pg0.local_ip6n,
155 dst_address=cls.mcast_ip6n,
156 mcast_sw_if_index=1, is_ipv6=1,
157 vni=cls.mcast_flood_bd)
158 cls.vapi.sw_interface_set_l2_bridge(rx_sw_if_index=r.sw_if_index,
Eyal Baricef1e2a2018-06-18 13:01:59 +0300159 bd_id=cls.mcast_flood_bd)
Ole Troana5b2eec2019-03-11 19:23:25 +0100160 cls.vapi.sw_interface_set_l2_bridge(
161 rx_sw_if_index=cls.pg2.sw_if_index, bd_id=cls.mcast_flood_bd)
Eyal Baricef1e2a2018-06-18 13:01:59 +0300162
163 # Setup vni 3 to test unicast flooding
164 cls.ucast_flood_bd = 3
165 cls.create_vxlan_flood_test_bd(cls.ucast_flood_bd,
166 cls.n_ucast_tunnels)
Ole Troana5b2eec2019-03-11 19:23:25 +0100167 cls.vapi.sw_interface_set_l2_bridge(
168 rx_sw_if_index=cls.pg3.sw_if_index, bd_id=cls.ucast_flood_bd)
Eyal Baricef1e2a2018-06-18 13:01:59 +0300169 except Exception:
Eyal Baridd47eca2018-07-08 08:15:56 +0300170 super(TestVxlan6, cls).tearDownClass()
Eyal Baricef1e2a2018-06-18 13:01:59 +0300171 raise
172
Paul Vinciguerra8d991d92019-01-25 14:05:48 -0800173 @classmethod
174 def tearDownClass(cls):
175 super(TestVxlan6, cls).tearDownClass()
176
Eyal Baricef1e2a2018-06-18 13:01:59 +0300177 # Method to define VPP actions before tear down of the test case.
178 # Overrides tearDown method in VppTestCase class.
179 # @param self The object pointer.
180 def tearDown(self):
Eyal Baridd47eca2018-07-08 08:15:56 +0300181 super(TestVxlan6, self).tearDown()
Paul Vinciguerra90cf21b2019-03-13 09:23:05 -0700182
183 def show_commands_at_teardown(self):
184 self.logger.info(self.vapi.cli("show bridge-domain 1 detail"))
185 self.logger.info(self.vapi.cli("show bridge-domain 2 detail"))
186 self.logger.info(self.vapi.cli("show bridge-domain 3 detail"))
187 self.logger.info(self.vapi.cli("show vxlan tunnel"))
Eyal Baricef1e2a2018-06-18 13:01:59 +0300188
189
190if __name__ == '__main__':
191 unittest.main(testRunner=VppTestRunner)