blob: 79eb23d45a4713be3bb13af16b2360deacdf8295 [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
snaramre5d4b8912019-12-13 23:39:35 +00009from scapy.layers.l2 import Ether
10from scapy.packet import Raw
Mohsin Kazmi61b94c62018-08-20 18:32:39 +020011from scapy.layers.inet import IP, UDP
12from scapy.layers.vxlan import VXLAN
Paul Vinciguerra2f156312020-05-02 22:34:40 -040013
Neale Ranns097fa662018-05-01 05:17:55 -070014from vpp_ip_route import VppIpRoute, VppRoutePath
15from vpp_ip import INVALID_INDEX
Mohsin Kazmi61b94c62018-08-20 18:32:39 +020016
Mohsin Kazmi61b94c62018-08-20 18:32:39 +020017
18class TestVxlanGbp(VppTestCase):
19 """ VXLAN GBP Test Case """
20
21 @property
22 def frame_request(self):
23 """ Ethernet frame modeling a generic request """
24 return (Ether(src='00:00:00:00:00:01', dst='00:00:00:00:00:02') /
25 IP(src='1.2.3.4', dst='4.3.2.1') /
26 UDP(sport=10000, dport=20000) /
Ole Troan770a0de2019-11-07 13:52:21 +010027 Raw(b'\xa5' * 100))
Mohsin Kazmi61b94c62018-08-20 18:32:39 +020028
29 @property
30 def frame_reply(self):
31 """ Ethernet frame modeling a generic reply """
32 return (Ether(src='00:00:00:00:00:02', dst='00:00:00:00:00:01') /
33 IP(src='4.3.2.1', dst='1.2.3.4') /
34 UDP(sport=20000, dport=10000) /
Ole Troan770a0de2019-11-07 13:52:21 +010035 Raw(b'\xa5' * 100))
Mohsin Kazmi61b94c62018-08-20 18:32:39 +020036
37 def encapsulate(self, pkt, vni):
38 """
39 Encapsulate the original payload frame by adding VXLAN GBP header with
40 its UDP, IP and Ethernet fields
41 """
42 return (Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac) /
43 IP(src=self.pg0.remote_ip4, dst=self.pg0.local_ip4) /
44 UDP(sport=self.dport, dport=self.dport, chksum=0) /
45 VXLAN(vni=vni, flags=self.flags, gpflags=self.gpflags,
46 gpid=self.sclass) / pkt)
47
48 def ip_range(self, start, end):
49 """ range of remote ip's """
50 return ip4_range(self.pg0.remote_ip4, start, end)
51
52 def decapsulate(self, pkt):
53 """
54 Decapsulate the original payload frame by removing VXLAN header
55 """
56 # check if is set G and I flag
57 self.assertEqual(pkt[VXLAN].flags, int('0x88', 16))
58 return pkt[VXLAN].payload
59
60 # Method for checking VXLAN GBP encapsulation.
61 #
62 def check_encapsulation(self, pkt, vni, local_only=False, mcast_pkt=False):
63 # TODO: add error messages
64 # Verify source MAC is VPP_MAC and destination MAC is MY_MAC resolved
65 # by VPP using ARP.
66 self.assertEqual(pkt[Ether].src, self.pg0.local_mac)
67 if not local_only:
68 if not mcast_pkt:
69 self.assertEqual(pkt[Ether].dst, self.pg0.remote_mac)
70 else:
71 self.assertEqual(pkt[Ether].dst, type(self).mcast_mac)
72 # Verify VXLAN GBP tunnel source IP is VPP_IP and destination IP is
73 # MY_IP.
74 self.assertEqual(pkt[IP].src, self.pg0.local_ip4)
75 if not local_only:
76 if not mcast_pkt:
77 self.assertEqual(pkt[IP].dst, self.pg0.remote_ip4)
78 else:
79 self.assertEqual(pkt[IP].dst, type(self).mcast_ip4)
80 # Verify UDP destination port is VXLAN GBP 48879, source UDP port could
81 # be arbitrary.
82 self.assertEqual(pkt[UDP].dport, type(self).dport)
83 # TODO: checksum check
84 # Verify VNI
85 # pkt.show()
86 self.assertEqual(pkt[VXLAN].vni, vni)
87 # Verify Source Class
88 self.assertEqual(pkt[VXLAN].gpid, 0)
89
90 @classmethod
91 def create_vxlan_gbp_flood_test_bd(cls, vni, n_ucast_tunnels):
92 # Create 2 ucast vxlan tunnels under bd
93 ip_range_start = 10
94 ip_range_end = ip_range_start + n_ucast_tunnels
Neale Ranns097fa662018-05-01 05:17:55 -070095 next_hop_address = cls.pg0.remote_ip4
Neale Ranns79a05f52018-09-11 07:39:43 -070096 for dest_ip4 in ip4_range(cls.pg0.remote_ip4,
97 ip_range_start,
98 ip_range_end):
Paul Vinciguerra2f156312020-05-02 22:34:40 -040099 # add host route so dest_ip4 will not be resolved
Neale Ranns097fa662018-05-01 05:17:55 -0700100 rip = VppIpRoute(cls, dest_ip4, 32,
101 [VppRoutePath(next_hop_address,
102 INVALID_INDEX)],
103 register=False)
104 rip.add_vpp_config()
Neale Ranns79a05f52018-09-11 07:39:43 -0700105 r = cls.vapi.vxlan_gbp_tunnel_add_del(
Paul Vinciguerra1b534f52019-06-15 20:31:31 -0400106 tunnel={
Neale Rannsefd7bc22019-11-11 08:32:34 +0000107 'src': cls.pg0.local_ip4,
108 'dst': dest_ip4,
Paul Vinciguerra1b534f52019-06-15 20:31:31 -0400109 'vni': vni,
110 'instance': INVALID_INDEX,
111 'mcast_sw_if_index': INVALID_INDEX,
112 'mode': 1,
113 },
114 is_add=1
115 )
Ole Troana5b2eec2019-03-11 19:23:25 +0100116 cls.vapi.sw_interface_set_l2_bridge(rx_sw_if_index=r.sw_if_index,
117 bd_id=vni)
Mohsin Kazmi61b94c62018-08-20 18:32:39 +0200118
119 # Class method to start the VXLAN GBP test case.
120 # Overrides setUpClass method in VppTestCase class.
121 # Python try..except statement is used to ensure that the tear down of
122 # the class will be executed even if exception is raised.
123 # @param cls The class pointer.
124 @classmethod
125 def setUpClass(cls):
126 super(TestVxlanGbp, cls).setUpClass()
127
128 try:
129 cls.dport = 48879
130 cls.flags = 0x88
131 cls.gpflags = 0x0
132 cls.sclass = 0
133
134 # Create 2 pg interfaces.
135 cls.create_pg_interfaces(range(4))
136 for pg in cls.pg_interfaces:
137 pg.admin_up()
138
139 # Configure IPv4 addresses on VPP pg0.
140 cls.pg0.config_ip4()
141
142 # Resolve MAC address for VPP's IP address on pg0.
143 cls.pg0.resolve_arp()
144
145 # Create VXLAN GBP VTEP on VPP pg0, and put vxlan_gbp_tunnel0 and
146 # pg1 into BD.
147 cls.single_tunnel_bd = 1
Neale Ranns91fd9102020-04-03 07:46:28 +0000148 cls.single_tunnel_vni = 0xabcde
Neale Ranns79a05f52018-09-11 07:39:43 -0700149 r = cls.vapi.vxlan_gbp_tunnel_add_del(
Paul Vinciguerra1b534f52019-06-15 20:31:31 -0400150 tunnel={
Neale Rannsefd7bc22019-11-11 08:32:34 +0000151 'src': cls.pg0.local_ip4,
152 'dst': cls.pg0.remote_ip4,
Neale Ranns91fd9102020-04-03 07:46:28 +0000153 'vni': cls.single_tunnel_vni,
Paul Vinciguerra1b534f52019-06-15 20:31:31 -0400154 'instance': INVALID_INDEX,
155 'mcast_sw_if_index': INVALID_INDEX,
156 'mode': 1,
157 },
158 is_add=1
159 )
Ole Troana5b2eec2019-03-11 19:23:25 +0100160 cls.vapi.sw_interface_set_l2_bridge(rx_sw_if_index=r.sw_if_index,
Mohsin Kazmi61b94c62018-08-20 18:32:39 +0200161 bd_id=cls.single_tunnel_bd)
Ole Troana5b2eec2019-03-11 19:23:25 +0100162 cls.vapi.sw_interface_set_l2_bridge(
Paul Vinciguerra1b534f52019-06-15 20:31:31 -0400163 rx_sw_if_index=cls.pg1.sw_if_index,
164 bd_id=cls.single_tunnel_bd)
Mohsin Kazmi61b94c62018-08-20 18:32:39 +0200165
166 # Setup vni 2 to test multicast flooding
167 cls.n_ucast_tunnels = 2
168 # Setup vni 3 to test unicast flooding
169 cls.ucast_flood_bd = 3
170 cls.create_vxlan_gbp_flood_test_bd(cls.ucast_flood_bd,
171 cls.n_ucast_tunnels)
Ole Troana5b2eec2019-03-11 19:23:25 +0100172 cls.vapi.sw_interface_set_l2_bridge(
Paul Vinciguerra1b534f52019-06-15 20:31:31 -0400173 rx_sw_if_index=cls.pg3.sw_if_index,
174 bd_id=cls.ucast_flood_bd)
Mohsin Kazmi61b94c62018-08-20 18:32:39 +0200175 except Exception:
176 super(TestVxlanGbp, cls).tearDownClass()
177 raise
178
Paul Vinciguerra7f9b7f92019-03-12 19:23:27 -0700179 @classmethod
180 def tearDownClass(cls):
181 super(TestVxlanGbp, cls).tearDownClass()
182
Mohsin Kazmi61b94c62018-08-20 18:32:39 +0200183 def assert_eq_pkts(self, pkt1, pkt2):
184 """ Verify the Ether, IP, UDP, payload are equal in both
185 packets
186 """
187 self.assertEqual(pkt1[Ether].src, pkt2[Ether].src)
188 self.assertEqual(pkt1[Ether].dst, pkt2[Ether].dst)
189 self.assertEqual(pkt1[IP].src, pkt2[IP].src)
190 self.assertEqual(pkt1[IP].dst, pkt2[IP].dst)
191 self.assertEqual(pkt1[UDP].sport, pkt2[UDP].sport)
192 self.assertEqual(pkt1[UDP].dport, pkt2[UDP].dport)
193 self.assertEqual(pkt1[Raw], pkt2[Raw])
194
195 def test_decap(self):
196 """ Decapsulation test
197 Send encapsulated frames from pg0
198 Verify receipt of decapsulated frames on pg1
199 """
200 encapsulated_pkt = self.encapsulate(self.frame_request,
Neale Ranns91fd9102020-04-03 07:46:28 +0000201 self.single_tunnel_vni)
Mohsin Kazmi61b94c62018-08-20 18:32:39 +0200202
203 self.pg0.add_stream([encapsulated_pkt, ])
204
205 self.pg1.enable_capture()
206
207 self.pg_start()
208
209 # Pick first received frame and check if it's the non-encapsulated
210 # frame
211 out = self.pg1.get_capture(1)
212 pkt = out[0]
213 self.assert_eq_pkts(pkt, self.frame_request)
214
215 def test_encap(self):
216 """ Encapsulation test
217 Send frames from pg1
218 Verify receipt of encapsulated frames on pg0
219 """
220 self.pg1.add_stream([self.frame_reply])
221
222 self.pg0.enable_capture()
223
224 self.pg_start()
225
Paul Vinciguerra8feeaff2019-03-27 11:25:48 -0700226 # Pick first received frame and check if it's correctly encapsulated.
Mohsin Kazmi61b94c62018-08-20 18:32:39 +0200227 out = self.pg0.get_capture(1)
228 pkt = out[0]
Neale Ranns91fd9102020-04-03 07:46:28 +0000229 self.check_encapsulation(pkt, self.single_tunnel_vni)
Mohsin Kazmi61b94c62018-08-20 18:32:39 +0200230
231 payload = self.decapsulate(pkt)
232 self.assert_eq_pkts(payload, self.frame_reply)
233
234 def test_ucast_flood(self):
235 """ Unicast flood test
236 Send frames from pg3
237 Verify receipt of encapsulated frames on pg0
238 """
239 self.pg3.add_stream([self.frame_reply])
240
241 self.pg0.enable_capture()
242
243 self.pg_start()
244
Paul Vinciguerra8feeaff2019-03-27 11:25:48 -0700245 # Get packet from each tunnel and assert it's correctly encapsulated.
Mohsin Kazmi61b94c62018-08-20 18:32:39 +0200246 out = self.pg0.get_capture(self.n_ucast_tunnels)
247 for pkt in out:
248 self.check_encapsulation(pkt, self.ucast_flood_bd, True)
249 payload = self.decapsulate(pkt)
250 self.assert_eq_pkts(payload, self.frame_reply)
251
252 def test_encap_big_packet(self):
253 """ Encapsulation test send big frame from pg1
254 Verify receipt of encapsulated frames on pg0
255 """
256
257 self.vapi.sw_interface_set_mtu(self.pg0.sw_if_index, [1500, 0, 0, 0])
258
259 frame = (Ether(src='00:00:00:00:00:02', dst='00:00:00:00:00:01') /
260 IP(src='4.3.2.1', dst='1.2.3.4') /
261 UDP(sport=20000, dport=10000) /
Ole Troan770a0de2019-11-07 13:52:21 +0100262 Raw(b'\xa5' * 1450))
Mohsin Kazmi61b94c62018-08-20 18:32:39 +0200263
264 self.pg1.add_stream([frame])
265
266 self.pg0.enable_capture()
267
268 self.pg_start()
269
270 # Pick first received frame and check if it's correctly encapsulated.
271 out = self.pg0.get_capture(2)
Ole Troan7f991832018-12-06 17:35:12 +0100272 pkt = reassemble4_ether(out)
Neale Ranns91fd9102020-04-03 07:46:28 +0000273 self.check_encapsulation(pkt, self.single_tunnel_vni)
Mohsin Kazmi61b94c62018-08-20 18:32:39 +0200274
275 payload = self.decapsulate(pkt)
276 self.assert_eq_pkts(payload, frame)
277
278# Method to define VPP actions before tear down of the test case.
279# Overrides tearDown method in VppTestCase class.
280# @param self The object pointer.
281 def tearDown(self):
282 super(TestVxlanGbp, self).tearDown()
Paul Vinciguerra90cf21b2019-03-13 09:23:05 -0700283
284 def show_commands_at_teardown(self):
285 self.logger.info(self.vapi.cli("show bridge-domain 1 detail"))
286 self.logger.info(self.vapi.cli("show bridge-domain 3 detail"))
287 self.logger.info(self.vapi.cli("show vxlan-gbp tunnel"))
288 self.logger.info(self.vapi.cli("show error"))
Mohsin Kazmi61b94c62018-08-20 18:32:39 +0200289
290
291if __name__ == '__main__':
292 unittest.main(testRunner=VppTestRunner)