blob: 17ee66205b61c12046d28a0bb3ab524ec25bb9a6 [file] [log] [blame]
Renato Botelho do Coutoead1e532019-10-31 13:31:07 -05001#!/usr/bin/env python3
Mohsin Kazmi61b94c62018-08-20 18:32:39 +02002
3import socket
Ole Troan7f991832018-12-06 17:35:12 +01004from util import ip4_range, reassemble4_ether
Mohsin Kazmi61b94c62018-08-20 18:32:39 +02005import unittest
6from framework import VppTestCase, VppTestRunner
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
Neale Ranns097fa662018-05-01 05:17:55 -070013from vpp_ip_route import VppIpRoute, VppRoutePath
14from vpp_ip import INVALID_INDEX
Mohsin Kazmi61b94c62018-08-20 18:32:39 +020015
Mohsin Kazmi61b94c62018-08-20 18:32:39 +020016
17class TestVxlanGbp(VppTestCase):
18 """ VXLAN GBP Test Case """
19
20 @property
21 def frame_request(self):
22 """ Ethernet frame modeling a generic request """
23 return (Ether(src='00:00:00:00:00:01', dst='00:00:00:00:00:02') /
24 IP(src='1.2.3.4', dst='4.3.2.1') /
25 UDP(sport=10000, dport=20000) /
Ole Troan770a0de2019-11-07 13:52:21 +010026 Raw(b'\xa5' * 100))
Mohsin Kazmi61b94c62018-08-20 18:32:39 +020027
28 @property
29 def frame_reply(self):
30 """ Ethernet frame modeling a generic reply """
31 return (Ether(src='00:00:00:00:00:02', dst='00:00:00:00:00:01') /
32 IP(src='4.3.2.1', dst='1.2.3.4') /
33 UDP(sport=20000, dport=10000) /
Ole Troan770a0de2019-11-07 13:52:21 +010034 Raw(b'\xa5' * 100))
Mohsin Kazmi61b94c62018-08-20 18:32:39 +020035
36 def encapsulate(self, pkt, vni):
37 """
38 Encapsulate the original payload frame by adding VXLAN GBP header with
39 its UDP, IP and Ethernet fields
40 """
41 return (Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac) /
42 IP(src=self.pg0.remote_ip4, dst=self.pg0.local_ip4) /
43 UDP(sport=self.dport, dport=self.dport, chksum=0) /
44 VXLAN(vni=vni, flags=self.flags, gpflags=self.gpflags,
45 gpid=self.sclass) / pkt)
46
47 def ip_range(self, start, end):
48 """ range of remote ip's """
49 return ip4_range(self.pg0.remote_ip4, start, end)
50
51 def decapsulate(self, pkt):
52 """
53 Decapsulate the original payload frame by removing VXLAN header
54 """
55 # check if is set G and I flag
56 self.assertEqual(pkt[VXLAN].flags, int('0x88', 16))
57 return pkt[VXLAN].payload
58
59 # Method for checking VXLAN GBP encapsulation.
60 #
61 def check_encapsulation(self, pkt, vni, local_only=False, mcast_pkt=False):
62 # TODO: add error messages
63 # Verify source MAC is VPP_MAC and destination MAC is MY_MAC resolved
64 # by VPP using ARP.
65 self.assertEqual(pkt[Ether].src, self.pg0.local_mac)
66 if not local_only:
67 if not mcast_pkt:
68 self.assertEqual(pkt[Ether].dst, self.pg0.remote_mac)
69 else:
70 self.assertEqual(pkt[Ether].dst, type(self).mcast_mac)
71 # Verify VXLAN GBP tunnel source IP is VPP_IP and destination IP is
72 # MY_IP.
73 self.assertEqual(pkt[IP].src, self.pg0.local_ip4)
74 if not local_only:
75 if not mcast_pkt:
76 self.assertEqual(pkt[IP].dst, self.pg0.remote_ip4)
77 else:
78 self.assertEqual(pkt[IP].dst, type(self).mcast_ip4)
79 # Verify UDP destination port is VXLAN GBP 48879, source UDP port could
80 # be arbitrary.
81 self.assertEqual(pkt[UDP].dport, type(self).dport)
82 # TODO: checksum check
83 # Verify VNI
84 # pkt.show()
85 self.assertEqual(pkt[VXLAN].vni, vni)
86 # Verify Source Class
87 self.assertEqual(pkt[VXLAN].gpid, 0)
88
89 @classmethod
90 def create_vxlan_gbp_flood_test_bd(cls, vni, n_ucast_tunnels):
91 # Create 2 ucast vxlan tunnels under bd
92 ip_range_start = 10
93 ip_range_end = ip_range_start + n_ucast_tunnels
Neale Ranns097fa662018-05-01 05:17:55 -070094 next_hop_address = cls.pg0.remote_ip4
Neale Ranns79a05f52018-09-11 07:39:43 -070095 for dest_ip4 in ip4_range(cls.pg0.remote_ip4,
96 ip_range_start,
97 ip_range_end):
Mohsin Kazmi61b94c62018-08-20 18:32:39 +020098 # add host route so dest_ip4n will not be resolved
Neale Ranns097fa662018-05-01 05:17:55 -070099 rip = VppIpRoute(cls, dest_ip4, 32,
100 [VppRoutePath(next_hop_address,
101 INVALID_INDEX)],
102 register=False)
103 rip.add_vpp_config()
Neale Ranns79a05f52018-09-11 07:39:43 -0700104 r = cls.vapi.vxlan_gbp_tunnel_add_del(
Paul Vinciguerra1b534f52019-06-15 20:31:31 -0400105 tunnel={
Neale Rannsefd7bc22019-11-11 08:32:34 +0000106 'src': cls.pg0.local_ip4,
107 'dst': dest_ip4,
Paul Vinciguerra1b534f52019-06-15 20:31:31 -0400108 'vni': vni,
109 'instance': INVALID_INDEX,
110 'mcast_sw_if_index': INVALID_INDEX,
111 'mode': 1,
112 },
113 is_add=1
114 )
Ole Troana5b2eec2019-03-11 19:23:25 +0100115 cls.vapi.sw_interface_set_l2_bridge(rx_sw_if_index=r.sw_if_index,
116 bd_id=vni)
Mohsin Kazmi61b94c62018-08-20 18:32:39 +0200117
118 # Class method to start the VXLAN GBP test case.
119 # Overrides setUpClass method in VppTestCase class.
120 # Python try..except statement is used to ensure that the tear down of
121 # the class will be executed even if exception is raised.
122 # @param cls The class pointer.
123 @classmethod
124 def setUpClass(cls):
125 super(TestVxlanGbp, cls).setUpClass()
126
127 try:
128 cls.dport = 48879
129 cls.flags = 0x88
130 cls.gpflags = 0x0
131 cls.sclass = 0
132
133 # Create 2 pg interfaces.
134 cls.create_pg_interfaces(range(4))
135 for pg in cls.pg_interfaces:
136 pg.admin_up()
137
138 # Configure IPv4 addresses on VPP pg0.
139 cls.pg0.config_ip4()
140
141 # Resolve MAC address for VPP's IP address on pg0.
142 cls.pg0.resolve_arp()
143
144 # Create VXLAN GBP VTEP on VPP pg0, and put vxlan_gbp_tunnel0 and
145 # pg1 into BD.
146 cls.single_tunnel_bd = 1
Neale Ranns79a05f52018-09-11 07:39:43 -0700147 r = cls.vapi.vxlan_gbp_tunnel_add_del(
Paul Vinciguerra1b534f52019-06-15 20:31:31 -0400148 tunnel={
Neale Rannsefd7bc22019-11-11 08:32:34 +0000149 'src': cls.pg0.local_ip4,
150 'dst': cls.pg0.remote_ip4,
Paul Vinciguerra1b534f52019-06-15 20:31:31 -0400151 'vni': cls.single_tunnel_bd,
152 'instance': INVALID_INDEX,
153 'mcast_sw_if_index': INVALID_INDEX,
154 'mode': 1,
155 },
156 is_add=1
157 )
Ole Troana5b2eec2019-03-11 19:23:25 +0100158 cls.vapi.sw_interface_set_l2_bridge(rx_sw_if_index=r.sw_if_index,
Mohsin Kazmi61b94c62018-08-20 18:32:39 +0200159 bd_id=cls.single_tunnel_bd)
Ole Troana5b2eec2019-03-11 19:23:25 +0100160 cls.vapi.sw_interface_set_l2_bridge(
Paul Vinciguerra1b534f52019-06-15 20:31:31 -0400161 rx_sw_if_index=cls.pg1.sw_if_index,
162 bd_id=cls.single_tunnel_bd)
Mohsin Kazmi61b94c62018-08-20 18:32:39 +0200163
164 # Setup vni 2 to test multicast flooding
165 cls.n_ucast_tunnels = 2
166 # Setup vni 3 to test unicast flooding
167 cls.ucast_flood_bd = 3
168 cls.create_vxlan_gbp_flood_test_bd(cls.ucast_flood_bd,
169 cls.n_ucast_tunnels)
Ole Troana5b2eec2019-03-11 19:23:25 +0100170 cls.vapi.sw_interface_set_l2_bridge(
Paul Vinciguerra1b534f52019-06-15 20:31:31 -0400171 rx_sw_if_index=cls.pg3.sw_if_index,
172 bd_id=cls.ucast_flood_bd)
Mohsin Kazmi61b94c62018-08-20 18:32:39 +0200173 except Exception:
174 super(TestVxlanGbp, cls).tearDownClass()
175 raise
176
Paul Vinciguerra7f9b7f92019-03-12 19:23:27 -0700177 @classmethod
178 def tearDownClass(cls):
179 super(TestVxlanGbp, cls).tearDownClass()
180
Mohsin Kazmi61b94c62018-08-20 18:32:39 +0200181 def assert_eq_pkts(self, pkt1, pkt2):
182 """ Verify the Ether, IP, UDP, payload are equal in both
183 packets
184 """
185 self.assertEqual(pkt1[Ether].src, pkt2[Ether].src)
186 self.assertEqual(pkt1[Ether].dst, pkt2[Ether].dst)
187 self.assertEqual(pkt1[IP].src, pkt2[IP].src)
188 self.assertEqual(pkt1[IP].dst, pkt2[IP].dst)
189 self.assertEqual(pkt1[UDP].sport, pkt2[UDP].sport)
190 self.assertEqual(pkt1[UDP].dport, pkt2[UDP].dport)
191 self.assertEqual(pkt1[Raw], pkt2[Raw])
192
193 def test_decap(self):
194 """ Decapsulation test
195 Send encapsulated frames from pg0
196 Verify receipt of decapsulated frames on pg1
197 """
198 encapsulated_pkt = self.encapsulate(self.frame_request,
199 self.single_tunnel_bd)
200
201 self.pg0.add_stream([encapsulated_pkt, ])
202
203 self.pg1.enable_capture()
204
205 self.pg_start()
206
207 # Pick first received frame and check if it's the non-encapsulated
208 # frame
209 out = self.pg1.get_capture(1)
210 pkt = out[0]
211 self.assert_eq_pkts(pkt, self.frame_request)
212
213 def test_encap(self):
214 """ Encapsulation test
215 Send frames from pg1
216 Verify receipt of encapsulated frames on pg0
217 """
218 self.pg1.add_stream([self.frame_reply])
219
220 self.pg0.enable_capture()
221
222 self.pg_start()
223
Paul Vinciguerra8feeaff2019-03-27 11:25:48 -0700224 # Pick first received frame and check if it's correctly encapsulated.
Mohsin Kazmi61b94c62018-08-20 18:32:39 +0200225 out = self.pg0.get_capture(1)
226 pkt = out[0]
227 self.check_encapsulation(pkt, self.single_tunnel_bd)
228
229 payload = self.decapsulate(pkt)
230 self.assert_eq_pkts(payload, self.frame_reply)
231
232 def test_ucast_flood(self):
233 """ Unicast flood test
234 Send frames from pg3
235 Verify receipt of encapsulated frames on pg0
236 """
237 self.pg3.add_stream([self.frame_reply])
238
239 self.pg0.enable_capture()
240
241 self.pg_start()
242
Paul Vinciguerra8feeaff2019-03-27 11:25:48 -0700243 # Get packet from each tunnel and assert it's correctly encapsulated.
Mohsin Kazmi61b94c62018-08-20 18:32:39 +0200244 out = self.pg0.get_capture(self.n_ucast_tunnels)
245 for pkt in out:
246 self.check_encapsulation(pkt, self.ucast_flood_bd, True)
247 payload = self.decapsulate(pkt)
248 self.assert_eq_pkts(payload, self.frame_reply)
249
250 def test_encap_big_packet(self):
251 """ Encapsulation test send big frame from pg1
252 Verify receipt of encapsulated frames on pg0
253 """
254
255 self.vapi.sw_interface_set_mtu(self.pg0.sw_if_index, [1500, 0, 0, 0])
256
257 frame = (Ether(src='00:00:00:00:00:02', dst='00:00:00:00:00:01') /
258 IP(src='4.3.2.1', dst='1.2.3.4') /
259 UDP(sport=20000, dport=10000) /
Ole Troan770a0de2019-11-07 13:52:21 +0100260 Raw(b'\xa5' * 1450))
Mohsin Kazmi61b94c62018-08-20 18:32:39 +0200261
262 self.pg1.add_stream([frame])
263
264 self.pg0.enable_capture()
265
266 self.pg_start()
267
268 # Pick first received frame and check if it's correctly encapsulated.
269 out = self.pg0.get_capture(2)
Ole Troan7f991832018-12-06 17:35:12 +0100270 pkt = reassemble4_ether(out)
Mohsin Kazmi61b94c62018-08-20 18:32:39 +0200271 self.check_encapsulation(pkt, self.single_tunnel_bd)
272
273 payload = self.decapsulate(pkt)
274 self.assert_eq_pkts(payload, frame)
275
276# Method to define VPP actions before tear down of the test case.
277# Overrides tearDown method in VppTestCase class.
278# @param self The object pointer.
279 def tearDown(self):
280 super(TestVxlanGbp, self).tearDown()
Paul Vinciguerra90cf21b2019-03-13 09:23:05 -0700281
282 def show_commands_at_teardown(self):
283 self.logger.info(self.vapi.cli("show bridge-domain 1 detail"))
284 self.logger.info(self.vapi.cli("show bridge-domain 3 detail"))
285 self.logger.info(self.vapi.cli("show vxlan-gbp tunnel"))
286 self.logger.info(self.vapi.cli("show error"))
Mohsin Kazmi61b94c62018-08-20 18:32:39 +0200287
288
289if __name__ == '__main__':
290 unittest.main(testRunner=VppTestRunner)