blob: bd70eefa68555e74dc9509a218cceafc71542164 [file] [log] [blame]
Mohsin Kazmi61b94c62018-08-20 18:32:39 +02001#!/usr/bin/env python
2
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
Neale Ranns79a05f52018-09-11 07:39:43 -07008from vpp_ip import VppIpAddress
Mohsin Kazmi61b94c62018-08-20 18:32:39 +02009
10from scapy.layers.l2 import Ether, Raw
11from scapy.layers.inet import IP, UDP
12from scapy.layers.vxlan import VXLAN
13from scapy.utils import atol
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) /
27 Raw('\xa5' * 100))
28
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) /
35 Raw('\xa5' * 100))
36
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):
Mohsin Kazmi61b94c62018-08-20 18:32:39 +020099 # add host route so dest_ip4n 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={
107 'src': VppIpAddress(cls.pg0.local_ip4).encode(),
108 'dst': VppIpAddress(dest_ip4).encode(),
109 '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 Ranns79a05f52018-09-11 07:39:43 -0700148 r = cls.vapi.vxlan_gbp_tunnel_add_del(
Paul Vinciguerra1b534f52019-06-15 20:31:31 -0400149 tunnel={
150 'src': VppIpAddress(cls.pg0.local_ip4).encode(),
151 'dst': VppIpAddress(cls.pg0.remote_ip4).encode(),
152 'vni': cls.single_tunnel_bd,
153 'instance': INVALID_INDEX,
154 'mcast_sw_if_index': INVALID_INDEX,
155 'mode': 1,
156 },
157 is_add=1
158 )
Ole Troana5b2eec2019-03-11 19:23:25 +0100159 cls.vapi.sw_interface_set_l2_bridge(rx_sw_if_index=r.sw_if_index,
Mohsin Kazmi61b94c62018-08-20 18:32:39 +0200160 bd_id=cls.single_tunnel_bd)
Ole Troana5b2eec2019-03-11 19:23:25 +0100161 cls.vapi.sw_interface_set_l2_bridge(
Paul Vinciguerra1b534f52019-06-15 20:31:31 -0400162 rx_sw_if_index=cls.pg1.sw_if_index,
163 bd_id=cls.single_tunnel_bd)
Mohsin Kazmi61b94c62018-08-20 18:32:39 +0200164
165 # Setup vni 2 to test multicast flooding
166 cls.n_ucast_tunnels = 2
167 # Setup vni 3 to test unicast flooding
168 cls.ucast_flood_bd = 3
169 cls.create_vxlan_gbp_flood_test_bd(cls.ucast_flood_bd,
170 cls.n_ucast_tunnels)
Ole Troana5b2eec2019-03-11 19:23:25 +0100171 cls.vapi.sw_interface_set_l2_bridge(
Paul Vinciguerra1b534f52019-06-15 20:31:31 -0400172 rx_sw_if_index=cls.pg3.sw_if_index,
173 bd_id=cls.ucast_flood_bd)
Mohsin Kazmi61b94c62018-08-20 18:32:39 +0200174 except Exception:
175 super(TestVxlanGbp, cls).tearDownClass()
176 raise
177
Paul Vinciguerra7f9b7f92019-03-12 19:23:27 -0700178 @classmethod
179 def tearDownClass(cls):
180 super(TestVxlanGbp, cls).tearDownClass()
181
Mohsin Kazmi61b94c62018-08-20 18:32:39 +0200182 def assert_eq_pkts(self, pkt1, pkt2):
183 """ Verify the Ether, IP, UDP, payload are equal in both
184 packets
185 """
186 self.assertEqual(pkt1[Ether].src, pkt2[Ether].src)
187 self.assertEqual(pkt1[Ether].dst, pkt2[Ether].dst)
188 self.assertEqual(pkt1[IP].src, pkt2[IP].src)
189 self.assertEqual(pkt1[IP].dst, pkt2[IP].dst)
190 self.assertEqual(pkt1[UDP].sport, pkt2[UDP].sport)
191 self.assertEqual(pkt1[UDP].dport, pkt2[UDP].dport)
192 self.assertEqual(pkt1[Raw], pkt2[Raw])
193
194 def test_decap(self):
195 """ Decapsulation test
196 Send encapsulated frames from pg0
197 Verify receipt of decapsulated frames on pg1
198 """
199 encapsulated_pkt = self.encapsulate(self.frame_request,
200 self.single_tunnel_bd)
201
202 self.pg0.add_stream([encapsulated_pkt, ])
203
204 self.pg1.enable_capture()
205
206 self.pg_start()
207
208 # Pick first received frame and check if it's the non-encapsulated
209 # frame
210 out = self.pg1.get_capture(1)
211 pkt = out[0]
212 self.assert_eq_pkts(pkt, self.frame_request)
213
214 def test_encap(self):
215 """ Encapsulation test
216 Send frames from pg1
217 Verify receipt of encapsulated frames on pg0
218 """
219 self.pg1.add_stream([self.frame_reply])
220
221 self.pg0.enable_capture()
222
223 self.pg_start()
224
Paul Vinciguerra8feeaff2019-03-27 11:25:48 -0700225 # Pick first received frame and check if it's correctly encapsulated.
Mohsin Kazmi61b94c62018-08-20 18:32:39 +0200226 out = self.pg0.get_capture(1)
227 pkt = out[0]
228 self.check_encapsulation(pkt, self.single_tunnel_bd)
229
230 payload = self.decapsulate(pkt)
231 self.assert_eq_pkts(payload, self.frame_reply)
232
233 def test_ucast_flood(self):
234 """ Unicast flood test
235 Send frames from pg3
236 Verify receipt of encapsulated frames on pg0
237 """
238 self.pg3.add_stream([self.frame_reply])
239
240 self.pg0.enable_capture()
241
242 self.pg_start()
243
Paul Vinciguerra8feeaff2019-03-27 11:25:48 -0700244 # Get packet from each tunnel and assert it's correctly encapsulated.
Mohsin Kazmi61b94c62018-08-20 18:32:39 +0200245 out = self.pg0.get_capture(self.n_ucast_tunnels)
246 for pkt in out:
247 self.check_encapsulation(pkt, self.ucast_flood_bd, True)
248 payload = self.decapsulate(pkt)
249 self.assert_eq_pkts(payload, self.frame_reply)
250
251 def test_encap_big_packet(self):
252 """ Encapsulation test send big frame from pg1
253 Verify receipt of encapsulated frames on pg0
254 """
255
256 self.vapi.sw_interface_set_mtu(self.pg0.sw_if_index, [1500, 0, 0, 0])
257
258 frame = (Ether(src='00:00:00:00:00:02', dst='00:00:00:00:00:01') /
259 IP(src='4.3.2.1', dst='1.2.3.4') /
260 UDP(sport=20000, dport=10000) /
261 Raw('\xa5' * 1450))
262
263 self.pg1.add_stream([frame])
264
265 self.pg0.enable_capture()
266
267 self.pg_start()
268
269 # Pick first received frame and check if it's correctly encapsulated.
270 out = self.pg0.get_capture(2)
Ole Troan7f991832018-12-06 17:35:12 +0100271 pkt = reassemble4_ether(out)
Mohsin Kazmi61b94c62018-08-20 18:32:39 +0200272 self.check_encapsulation(pkt, self.single_tunnel_bd)
273
274 payload = self.decapsulate(pkt)
275 self.assert_eq_pkts(payload, frame)
276
277# Method to define VPP actions before tear down of the test case.
278# Overrides tearDown method in VppTestCase class.
279# @param self The object pointer.
280 def tearDown(self):
281 super(TestVxlanGbp, self).tearDown()
Paul Vinciguerra90cf21b2019-03-13 09:23:05 -0700282
283 def show_commands_at_teardown(self):
284 self.logger.info(self.vapi.cli("show bridge-domain 1 detail"))
285 self.logger.info(self.vapi.cli("show bridge-domain 3 detail"))
286 self.logger.info(self.vapi.cli("show vxlan-gbp tunnel"))
287 self.logger.info(self.vapi.cli("show error"))
Mohsin Kazmi61b94c62018-08-20 18:32:39 +0200288
289
290if __name__ == '__main__':
291 unittest.main(testRunner=VppTestRunner)