blob: f332aced7d8cdf205d5cf92da2ae37a974577ae2 [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)
Vladimir Isaev698eb872020-05-21 16:34:17 +030083 # Verify UDP checksum
84 self.assert_udp_checksum_valid(pkt)
Mohsin Kazmi61b94c62018-08-20 18:32:39 +020085 # Verify VNI
86 # pkt.show()
87 self.assertEqual(pkt[VXLAN].vni, vni)
88 # Verify Source Class
89 self.assertEqual(pkt[VXLAN].gpid, 0)
90
91 @classmethod
92 def create_vxlan_gbp_flood_test_bd(cls, vni, n_ucast_tunnels):
93 # Create 2 ucast vxlan tunnels under bd
94 ip_range_start = 10
95 ip_range_end = ip_range_start + n_ucast_tunnels
Neale Ranns097fa662018-05-01 05:17:55 -070096 next_hop_address = cls.pg0.remote_ip4
Neale Ranns79a05f52018-09-11 07:39:43 -070097 for dest_ip4 in ip4_range(cls.pg0.remote_ip4,
98 ip_range_start,
99 ip_range_end):
Paul Vinciguerra2f156312020-05-02 22:34:40 -0400100 # add host route so dest_ip4 will not be resolved
Neale Ranns097fa662018-05-01 05:17:55 -0700101 rip = VppIpRoute(cls, dest_ip4, 32,
102 [VppRoutePath(next_hop_address,
103 INVALID_INDEX)],
104 register=False)
105 rip.add_vpp_config()
Neale Ranns79a05f52018-09-11 07:39:43 -0700106 r = cls.vapi.vxlan_gbp_tunnel_add_del(
Paul Vinciguerra1b534f52019-06-15 20:31:31 -0400107 tunnel={
Neale Rannsefd7bc22019-11-11 08:32:34 +0000108 'src': cls.pg0.local_ip4,
109 'dst': dest_ip4,
Paul Vinciguerra1b534f52019-06-15 20:31:31 -0400110 'vni': vni,
111 'instance': INVALID_INDEX,
112 'mcast_sw_if_index': INVALID_INDEX,
113 'mode': 1,
114 },
115 is_add=1
116 )
Ole Troana5b2eec2019-03-11 19:23:25 +0100117 cls.vapi.sw_interface_set_l2_bridge(rx_sw_if_index=r.sw_if_index,
118 bd_id=vni)
Mohsin Kazmi61b94c62018-08-20 18:32:39 +0200119
120 # Class method to start the VXLAN GBP test case.
121 # Overrides setUpClass method in VppTestCase class.
122 # Python try..except statement is used to ensure that the tear down of
123 # the class will be executed even if exception is raised.
124 # @param cls The class pointer.
125 @classmethod
126 def setUpClass(cls):
127 super(TestVxlanGbp, cls).setUpClass()
128
129 try:
130 cls.dport = 48879
131 cls.flags = 0x88
132 cls.gpflags = 0x0
133 cls.sclass = 0
134
135 # Create 2 pg interfaces.
136 cls.create_pg_interfaces(range(4))
137 for pg in cls.pg_interfaces:
138 pg.admin_up()
139
140 # Configure IPv4 addresses on VPP pg0.
141 cls.pg0.config_ip4()
142
143 # Resolve MAC address for VPP's IP address on pg0.
144 cls.pg0.resolve_arp()
145
146 # Create VXLAN GBP VTEP on VPP pg0, and put vxlan_gbp_tunnel0 and
147 # pg1 into BD.
148 cls.single_tunnel_bd = 1
Neale Ranns91fd9102020-04-03 07:46:28 +0000149 cls.single_tunnel_vni = 0xabcde
Neale Ranns79a05f52018-09-11 07:39:43 -0700150 r = cls.vapi.vxlan_gbp_tunnel_add_del(
Paul Vinciguerra1b534f52019-06-15 20:31:31 -0400151 tunnel={
Neale Rannsefd7bc22019-11-11 08:32:34 +0000152 'src': cls.pg0.local_ip4,
153 'dst': cls.pg0.remote_ip4,
Neale Ranns91fd9102020-04-03 07:46:28 +0000154 'vni': cls.single_tunnel_vni,
Paul Vinciguerra1b534f52019-06-15 20:31:31 -0400155 'instance': INVALID_INDEX,
156 'mcast_sw_if_index': INVALID_INDEX,
157 'mode': 1,
158 },
159 is_add=1
160 )
Ole Troana5b2eec2019-03-11 19:23:25 +0100161 cls.vapi.sw_interface_set_l2_bridge(rx_sw_if_index=r.sw_if_index,
Mohsin Kazmi61b94c62018-08-20 18:32:39 +0200162 bd_id=cls.single_tunnel_bd)
Ole Troana5b2eec2019-03-11 19:23:25 +0100163 cls.vapi.sw_interface_set_l2_bridge(
Paul Vinciguerra1b534f52019-06-15 20:31:31 -0400164 rx_sw_if_index=cls.pg1.sw_if_index,
165 bd_id=cls.single_tunnel_bd)
Mohsin Kazmi61b94c62018-08-20 18:32:39 +0200166
167 # Setup vni 2 to test multicast flooding
168 cls.n_ucast_tunnels = 2
169 # Setup vni 3 to test unicast flooding
170 cls.ucast_flood_bd = 3
171 cls.create_vxlan_gbp_flood_test_bd(cls.ucast_flood_bd,
172 cls.n_ucast_tunnels)
Ole Troana5b2eec2019-03-11 19:23:25 +0100173 cls.vapi.sw_interface_set_l2_bridge(
Paul Vinciguerra1b534f52019-06-15 20:31:31 -0400174 rx_sw_if_index=cls.pg3.sw_if_index,
175 bd_id=cls.ucast_flood_bd)
Mohsin Kazmi61b94c62018-08-20 18:32:39 +0200176 except Exception:
177 super(TestVxlanGbp, cls).tearDownClass()
178 raise
179
Paul Vinciguerra7f9b7f92019-03-12 19:23:27 -0700180 @classmethod
181 def tearDownClass(cls):
182 super(TestVxlanGbp, cls).tearDownClass()
183
Mohsin Kazmi61b94c62018-08-20 18:32:39 +0200184 def assert_eq_pkts(self, pkt1, pkt2):
185 """ Verify the Ether, IP, UDP, payload are equal in both
186 packets
187 """
188 self.assertEqual(pkt1[Ether].src, pkt2[Ether].src)
189 self.assertEqual(pkt1[Ether].dst, pkt2[Ether].dst)
190 self.assertEqual(pkt1[IP].src, pkt2[IP].src)
191 self.assertEqual(pkt1[IP].dst, pkt2[IP].dst)
192 self.assertEqual(pkt1[UDP].sport, pkt2[UDP].sport)
193 self.assertEqual(pkt1[UDP].dport, pkt2[UDP].dport)
194 self.assertEqual(pkt1[Raw], pkt2[Raw])
195
196 def test_decap(self):
197 """ Decapsulation test
198 Send encapsulated frames from pg0
199 Verify receipt of decapsulated frames on pg1
200 """
201 encapsulated_pkt = self.encapsulate(self.frame_request,
Neale Ranns91fd9102020-04-03 07:46:28 +0000202 self.single_tunnel_vni)
Mohsin Kazmi61b94c62018-08-20 18:32:39 +0200203
204 self.pg0.add_stream([encapsulated_pkt, ])
205
206 self.pg1.enable_capture()
207
208 self.pg_start()
209
210 # Pick first received frame and check if it's the non-encapsulated
211 # frame
212 out = self.pg1.get_capture(1)
213 pkt = out[0]
214 self.assert_eq_pkts(pkt, self.frame_request)
215
216 def test_encap(self):
217 """ Encapsulation test
218 Send frames from pg1
219 Verify receipt of encapsulated frames on pg0
220 """
221 self.pg1.add_stream([self.frame_reply])
222
223 self.pg0.enable_capture()
224
225 self.pg_start()
226
Paul Vinciguerra8feeaff2019-03-27 11:25:48 -0700227 # Pick first received frame and check if it's correctly encapsulated.
Mohsin Kazmi61b94c62018-08-20 18:32:39 +0200228 out = self.pg0.get_capture(1)
229 pkt = out[0]
Neale Ranns91fd9102020-04-03 07:46:28 +0000230 self.check_encapsulation(pkt, self.single_tunnel_vni)
Mohsin Kazmi61b94c62018-08-20 18:32:39 +0200231
232 payload = self.decapsulate(pkt)
233 self.assert_eq_pkts(payload, self.frame_reply)
234
235 def test_ucast_flood(self):
236 """ Unicast flood test
237 Send frames from pg3
238 Verify receipt of encapsulated frames on pg0
239 """
240 self.pg3.add_stream([self.frame_reply])
241
242 self.pg0.enable_capture()
243
244 self.pg_start()
245
Paul Vinciguerra8feeaff2019-03-27 11:25:48 -0700246 # Get packet from each tunnel and assert it's correctly encapsulated.
Mohsin Kazmi61b94c62018-08-20 18:32:39 +0200247 out = self.pg0.get_capture(self.n_ucast_tunnels)
248 for pkt in out:
249 self.check_encapsulation(pkt, self.ucast_flood_bd, True)
250 payload = self.decapsulate(pkt)
251 self.assert_eq_pkts(payload, self.frame_reply)
252
253 def test_encap_big_packet(self):
254 """ Encapsulation test send big frame from pg1
255 Verify receipt of encapsulated frames on pg0
256 """
257
258 self.vapi.sw_interface_set_mtu(self.pg0.sw_if_index, [1500, 0, 0, 0])
259
260 frame = (Ether(src='00:00:00:00:00:02', dst='00:00:00:00:00:01') /
261 IP(src='4.3.2.1', dst='1.2.3.4') /
262 UDP(sport=20000, dport=10000) /
Ole Troan770a0de2019-11-07 13:52:21 +0100263 Raw(b'\xa5' * 1450))
Mohsin Kazmi61b94c62018-08-20 18:32:39 +0200264
265 self.pg1.add_stream([frame])
266
267 self.pg0.enable_capture()
268
269 self.pg_start()
270
271 # Pick first received frame and check if it's correctly encapsulated.
272 out = self.pg0.get_capture(2)
Ole Troan7f991832018-12-06 17:35:12 +0100273 pkt = reassemble4_ether(out)
Neale Ranns91fd9102020-04-03 07:46:28 +0000274 self.check_encapsulation(pkt, self.single_tunnel_vni)
Mohsin Kazmi61b94c62018-08-20 18:32:39 +0200275
276 payload = self.decapsulate(pkt)
277 self.assert_eq_pkts(payload, frame)
278
279# Method to define VPP actions before tear down of the test case.
280# Overrides tearDown method in VppTestCase class.
281# @param self The object pointer.
282 def tearDown(self):
283 super(TestVxlanGbp, self).tearDown()
Paul Vinciguerra90cf21b2019-03-13 09:23:05 -0700284
285 def show_commands_at_teardown(self):
286 self.logger.info(self.vapi.cli("show bridge-domain 1 detail"))
287 self.logger.info(self.vapi.cli("show bridge-domain 3 detail"))
288 self.logger.info(self.vapi.cli("show vxlan-gbp tunnel"))
289 self.logger.info(self.vapi.cli("show error"))
Mohsin Kazmi61b94c62018-08-20 18:32:39 +0200290
291
292if __name__ == '__main__':
293 unittest.main(testRunner=VppTestRunner)