4 from util import ip4_range, reassemble4_ether
6 from framework import VppTestCase, VppTestRunner
7 from template_bd import BridgeDomain
8 from vpp_ip import VppIpAddress
10 from scapy.layers.l2 import Ether, Raw
11 from scapy.layers.inet import IP, UDP
12 from scapy.layers.vxlan import VXLAN
13 from scapy.utils import atol
16 class TestVxlanGbp(VppTestCase):
17 """ VXLAN GBP Test Case """
20 def frame_request(self):
21 """ Ethernet frame modeling a generic request """
22 return (Ether(src='00:00:00:00:00:01', dst='00:00:00:00:00:02') /
23 IP(src='1.2.3.4', dst='4.3.2.1') /
24 UDP(sport=10000, dport=20000) /
28 def frame_reply(self):
29 """ Ethernet frame modeling a generic reply """
30 return (Ether(src='00:00:00:00:00:02', dst='00:00:00:00:00:01') /
31 IP(src='4.3.2.1', dst='1.2.3.4') /
32 UDP(sport=20000, dport=10000) /
35 def encapsulate(self, pkt, vni):
37 Encapsulate the original payload frame by adding VXLAN GBP header with
38 its UDP, IP and Ethernet fields
40 return (Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac) /
41 IP(src=self.pg0.remote_ip4, dst=self.pg0.local_ip4) /
42 UDP(sport=self.dport, dport=self.dport, chksum=0) /
43 VXLAN(vni=vni, flags=self.flags, gpflags=self.gpflags,
44 gpid=self.sclass) / pkt)
46 def ip_range(self, start, end):
47 """ range of remote ip's """
48 return ip4_range(self.pg0.remote_ip4, start, end)
50 def decapsulate(self, pkt):
52 Decapsulate the original payload frame by removing VXLAN header
54 # check if is set G and I flag
55 self.assertEqual(pkt[VXLAN].flags, int('0x88', 16))
56 return pkt[VXLAN].payload
58 # Method for checking VXLAN GBP encapsulation.
60 def check_encapsulation(self, pkt, vni, local_only=False, mcast_pkt=False):
61 # TODO: add error messages
62 # Verify source MAC is VPP_MAC and destination MAC is MY_MAC resolved
64 self.assertEqual(pkt[Ether].src, self.pg0.local_mac)
67 self.assertEqual(pkt[Ether].dst, self.pg0.remote_mac)
69 self.assertEqual(pkt[Ether].dst, type(self).mcast_mac)
70 # Verify VXLAN GBP tunnel source IP is VPP_IP and destination IP is
72 self.assertEqual(pkt[IP].src, self.pg0.local_ip4)
75 self.assertEqual(pkt[IP].dst, self.pg0.remote_ip4)
77 self.assertEqual(pkt[IP].dst, type(self).mcast_ip4)
78 # Verify UDP destination port is VXLAN GBP 48879, source UDP port could
80 self.assertEqual(pkt[UDP].dport, type(self).dport)
81 # TODO: checksum check
84 self.assertEqual(pkt[VXLAN].vni, vni)
86 self.assertEqual(pkt[VXLAN].gpid, 0)
89 def create_vxlan_gbp_flood_test_bd(cls, vni, n_ucast_tunnels):
90 # Create 2 ucast vxlan tunnels under bd
92 ip_range_end = ip_range_start + n_ucast_tunnels
93 next_hop_address = cls.pg0.remote_ip4n
94 for dest_ip4 in ip4_range(cls.pg0.remote_ip4,
97 # add host route so dest_ip4n will not be resolved
98 vip = VppIpAddress(dest_ip4)
99 cls.vapi.ip_add_del_route(dst_address=vip.bytes,
100 dst_address_length=32,
101 next_hop_address=next_hop_address)
102 r = cls.vapi.vxlan_gbp_tunnel_add_del(
103 VppIpAddress(cls.pg0.local_ip4).encode(),
106 cls.vapi.sw_interface_set_l2_bridge(rx_sw_if_index=r.sw_if_index,
109 # Class method to start the VXLAN GBP 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.
116 super(TestVxlanGbp, cls).setUpClass()
124 # Create 2 pg interfaces.
125 cls.create_pg_interfaces(range(4))
126 for pg in cls.pg_interfaces:
129 # Configure IPv4 addresses on VPP pg0.
132 # Resolve MAC address for VPP's IP address on pg0.
133 cls.pg0.resolve_arp()
135 # Create VXLAN GBP VTEP on VPP pg0, and put vxlan_gbp_tunnel0 and
137 cls.single_tunnel_bd = 1
138 r = cls.vapi.vxlan_gbp_tunnel_add_del(
139 VppIpAddress(cls.pg0.local_ip4).encode(),
140 VppIpAddress(cls.pg0.remote_ip4).encode(),
141 vni=cls.single_tunnel_bd)
142 cls.vapi.sw_interface_set_l2_bridge(rx_sw_if_index=r.sw_if_index,
143 bd_id=cls.single_tunnel_bd)
144 cls.vapi.sw_interface_set_l2_bridge(
145 rx_sw_if_index=cls.pg1.sw_if_index, bd_id=cls.single_tunnel_bd)
147 # Setup vni 2 to test multicast flooding
148 cls.n_ucast_tunnels = 2
149 # Setup vni 3 to test unicast flooding
150 cls.ucast_flood_bd = 3
151 cls.create_vxlan_gbp_flood_test_bd(cls.ucast_flood_bd,
153 cls.vapi.sw_interface_set_l2_bridge(
154 rx_sw_if_index=cls.pg3.sw_if_index, bd_id=cls.ucast_flood_bd)
156 super(TestVxlanGbp, cls).tearDownClass()
159 def assert_eq_pkts(self, pkt1, pkt2):
160 """ Verify the Ether, IP, UDP, payload are equal in both
163 self.assertEqual(pkt1[Ether].src, pkt2[Ether].src)
164 self.assertEqual(pkt1[Ether].dst, pkt2[Ether].dst)
165 self.assertEqual(pkt1[IP].src, pkt2[IP].src)
166 self.assertEqual(pkt1[IP].dst, pkt2[IP].dst)
167 self.assertEqual(pkt1[UDP].sport, pkt2[UDP].sport)
168 self.assertEqual(pkt1[UDP].dport, pkt2[UDP].dport)
169 self.assertEqual(pkt1[Raw], pkt2[Raw])
171 def test_decap(self):
172 """ Decapsulation test
173 Send encapsulated frames from pg0
174 Verify receipt of decapsulated frames on pg1
176 encapsulated_pkt = self.encapsulate(self.frame_request,
177 self.single_tunnel_bd)
179 self.pg0.add_stream([encapsulated_pkt, ])
181 self.pg1.enable_capture()
185 # Pick first received frame and check if it's the non-encapsulated
187 out = self.pg1.get_capture(1)
189 self.assert_eq_pkts(pkt, self.frame_request)
191 def test_encap(self):
192 """ Encapsulation test
194 Verify receipt of encapsulated frames on pg0
196 self.pg1.add_stream([self.frame_reply])
198 self.pg0.enable_capture()
202 # Pick first received frame and check if it's corectly encapsulated.
203 out = self.pg0.get_capture(1)
205 self.check_encapsulation(pkt, self.single_tunnel_bd)
207 payload = self.decapsulate(pkt)
208 self.assert_eq_pkts(payload, self.frame_reply)
210 def test_ucast_flood(self):
211 """ Unicast flood test
213 Verify receipt of encapsulated frames on pg0
215 self.pg3.add_stream([self.frame_reply])
217 self.pg0.enable_capture()
221 # Get packet from each tunnel and assert it's corectly encapsulated.
222 out = self.pg0.get_capture(self.n_ucast_tunnels)
224 self.check_encapsulation(pkt, self.ucast_flood_bd, True)
225 payload = self.decapsulate(pkt)
226 self.assert_eq_pkts(payload, self.frame_reply)
228 def test_encap_big_packet(self):
229 """ Encapsulation test send big frame from pg1
230 Verify receipt of encapsulated frames on pg0
233 self.vapi.sw_interface_set_mtu(self.pg0.sw_if_index, [1500, 0, 0, 0])
235 frame = (Ether(src='00:00:00:00:00:02', dst='00:00:00:00:00:01') /
236 IP(src='4.3.2.1', dst='1.2.3.4') /
237 UDP(sport=20000, dport=10000) /
240 self.pg1.add_stream([frame])
242 self.pg0.enable_capture()
246 # Pick first received frame and check if it's correctly encapsulated.
247 out = self.pg0.get_capture(2)
248 pkt = reassemble4_ether(out)
249 self.check_encapsulation(pkt, self.single_tunnel_bd)
251 payload = self.decapsulate(pkt)
252 self.assert_eq_pkts(payload, frame)
254 # Method to define VPP actions before tear down of the test case.
255 # Overrides tearDown method in VppTestCase class.
256 # @param self The object pointer.
258 super(TestVxlanGbp, self).tearDown()
259 if not self.vpp_dead:
260 self.logger.info(self.vapi.cli("show bridge-domain 1 detail"))
261 self.logger.info(self.vapi.cli("show bridge-domain 3 detail"))
262 self.logger.info(self.vapi.cli("show vxlan-gbp tunnel"))
263 self.logger.info(self.vapi.cli("show error"))
266 if __name__ == '__main__':
267 unittest.main(testRunner=VppTestRunner)