4 from util import ip4_range
6 from framework import VppTestCase, VppTestRunner
7 from template_bd import BridgeDomain
9 from scapy.layers.l2 import Ether
10 from scapy.packet import Raw
11 from scapy.layers.inet import IP, UDP
12 from scapy.layers.inet6 import IPv6
13 from scapy.contrib.gtp import GTP_U_Header
14 from scapy.utils import atol
17 from vpp_ip_route import VppIpRoute, VppRoutePath
18 from vpp_ip import INVALID_INDEX
21 class TestGtpuUDP(VppTestCase):
22 """ GTPU UDP ports Test Case """
25 super(TestGtpuUDP, self).setUp()
32 self.create_pg_interfaces(range(1))
33 for pg in self.pg_interfaces:
38 def _check_udp_port_ip4(self, enabled=True):
40 pkt = (Ether(src=self.pg0.local_mac, dst=self.pg0.remote_mac) /
41 IP(src=self.pg0.remote_ip4, dst=self.pg0.local_ip4) /
42 UDP(sport=self.dport, dport=self.dport, chksum=0))
44 self.pg0.add_stream(pkt)
47 err = self.statistics.get_counter(
48 '/err/ip4-udp-lookup/No listener for dst port')[0]
51 self.assertEqual(err, self.ip4_err)
53 self.assertEqual(err, self.ip4_err + 1)
57 def _check_udp_port_ip6(self, enabled=True):
59 pkt = (Ether(src=self.pg0.local_mac, dst=self.pg0.remote_mac) /
60 IPv6(src=self.pg0.remote_ip6, dst=self.pg0.local_ip6) /
61 UDP(sport=self.dport, dport=self.dport, chksum=0))
63 self.pg0.add_stream(pkt)
66 err = self.statistics.get_counter(
67 '/err/ip6-udp-lookup/No listener for dst port')[0]
70 self.assertEqual(err, self.ip6_err)
72 self.assertEqual(err, self.ip6_err + 1)
76 def test_udp_port(self):
78 Check if there are no udp listeners before gtpu is enabled
80 # UDP ports should be disabled unless a tunnel is configured
81 self._check_udp_port_ip4(False)
82 self._check_udp_port_ip6(False)
84 r = self.vapi.gtpu_add_del_tunnel(is_add=True,
85 mcast_sw_if_index=0xFFFFFFFF,
86 decap_next_index=0xFFFFFFFF,
87 src_address=self.pg0.local_ip4,
88 dst_address=self.pg0.remote_ip4)
90 # UDP port 2152 enabled for ip4
91 self._check_udp_port_ip4()
93 r = self.vapi.gtpu_add_del_tunnel(is_add=True,
94 mcast_sw_if_index=0xFFFFFFFF,
95 decap_next_index=0xFFFFFFFF,
96 src_address=self.pg0.local_ip6,
97 dst_address=self.pg0.remote_ip6)
99 # UDP port 2152 enabled for ip6
100 self._check_udp_port_ip6()
102 r = self.vapi.gtpu_add_del_tunnel(is_add=False,
103 mcast_sw_if_index=0xFFFFFFFF,
104 decap_next_index=0xFFFFFFFF,
105 src_address=self.pg0.local_ip4,
106 dst_address=self.pg0.remote_ip4)
108 r = self.vapi.gtpu_add_del_tunnel(is_add=False,
109 mcast_sw_if_index=0xFFFFFFFF,
110 decap_next_index=0xFFFFFFFF,
111 src_address=self.pg0.local_ip6,
112 dst_address=self.pg0.remote_ip6)
115 class TestGtpu(BridgeDomain, VppTestCase):
116 """ GTPU Test Case """
118 def __init__(self, *args):
119 BridgeDomain.__init__(self)
120 VppTestCase.__init__(self, *args)
122 def encapsulate(self, pkt, vni):
124 Encapsulate the original payload frame by adding GTPU header with its
125 UDP, IP and Ethernet fields
127 return (Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac) /
128 IP(src=self.pg0.remote_ip4, dst=self.pg0.local_ip4) /
129 UDP(sport=self.dport, dport=self.dport, chksum=0) /
130 GTP_U_Header(teid=vni, gtp_type=self.gtp_type, length=150) /
133 def ip_range(self, start, end):
134 """ range of remote ip's """
135 return ip4_range(self.pg0.remote_ip4, start, end)
137 def encap_mcast(self, pkt, src_ip, src_mac, vni):
139 Encapsulate the original payload frame by adding GTPU header with its
140 UDP, IP and Ethernet fields
142 return (Ether(src=src_mac, dst=self.mcast_mac) /
143 IP(src=src_ip, dst=self.mcast_ip4) /
144 UDP(sport=self.dport, dport=self.dport, chksum=0) /
145 GTP_U_Header(teid=vni, gtp_type=self.gtp_type, length=150) /
148 def decapsulate(self, pkt):
150 Decapsulate the original payload frame by removing GTPU header
152 return pkt[GTP_U_Header].payload
154 # Method for checking GTPU encapsulation.
156 def check_encapsulation(self, pkt, vni, local_only=False, mcast_pkt=False):
157 # Verify source MAC is VPP_MAC and destination MAC is MY_MAC resolved
159 self.assertEqual(pkt[Ether].src, self.pg0.local_mac)
162 self.assertEqual(pkt[Ether].dst, self.pg0.remote_mac)
164 self.assertEqual(pkt[Ether].dst, type(self).mcast_mac)
165 # Verify GTPU tunnel source IP is VPP_IP and destination IP is MY_IP.
166 self.assertEqual(pkt[IP].src, self.pg0.local_ip4)
169 self.assertEqual(pkt[IP].dst, self.pg0.remote_ip4)
171 self.assertEqual(pkt[IP].dst, type(self).mcast_ip4)
172 # Verify UDP destination port is GTPU 2152, source UDP port could be
174 self.assertEqual(pkt[UDP].dport, type(self).dport)
176 self.assertEqual(pkt[GTP_U_Header].teid, vni)
178 def test_encap(self):
179 """ Encapsulation test
181 Verify receipt of encapsulated frames on pg0
183 self.pg1.add_stream([self.frame_reply])
185 self.pg0.enable_capture()
189 # Pick first received frame and check if it's correctly encapsulated.
190 out = self.pg0.get_capture(1)
192 self.check_encapsulation(pkt, self.single_tunnel_vni)
194 # payload = self.decapsulate(pkt)
195 # self.assert_eq_pkts(payload, self.frame_reply)
197 def test_ucast_flood(self):
198 """ Unicast flood test
200 Verify receipt of encapsulated frames on pg0
202 self.pg3.add_stream([self.frame_reply])
204 self.pg0.enable_capture()
208 # Get packet from each tunnel and assert it's correctly encapsulated.
209 out = self.pg0.get_capture(self.n_ucast_tunnels)
211 self.check_encapsulation(pkt, self.ucast_flood_bd, True)
212 # payload = self.decapsulate(pkt)
213 # self.assert_eq_pkts(payload, self.frame_reply)
215 def test_mcast_flood(self):
216 """ Multicast flood test
218 Verify receipt of encapsulated frames on pg0
220 self.pg2.add_stream([self.frame_reply])
222 self.pg0.enable_capture()
226 # Pick first received frame and check if it's correctly encapsulated.
227 out = self.pg0.get_capture(1)
229 self.check_encapsulation(pkt, self.mcast_flood_bd,
230 local_only=False, mcast_pkt=True)
232 # payload = self.decapsulate(pkt)
233 # self.assert_eq_pkts(payload, self.frame_reply)
236 def create_gtpu_flood_test_bd(cls, teid, n_ucast_tunnels):
237 # Create 10 ucast gtpu tunnels under bd
239 ip_range_end = ip_range_start + n_ucast_tunnels
240 next_hop_address = cls.pg0.remote_ip4
241 for dest_ip4 in ip4_range(next_hop_address, ip_range_start,
243 # add host route so dest_ip4 will not be resolved
244 rip = VppIpRoute(cls, dest_ip4, 32,
245 [VppRoutePath(next_hop_address,
249 r = cls.vapi.gtpu_add_del_tunnel(
251 mcast_sw_if_index=0xFFFFFFFF,
252 decap_next_index=0xFFFFFFFF,
253 src_address=cls.pg0.local_ip4,
254 dst_address=dest_ip4,
256 cls.vapi.sw_interface_set_l2_bridge(rx_sw_if_index=r.sw_if_index,
260 def add_del_shared_mcast_dst_load(cls, is_add):
262 add or del tunnels sharing the same mcast dst
263 to test gtpu ref_count mechanism
265 n_shared_dst_tunnels = 20
267 teid_end = teid_start + n_shared_dst_tunnels
268 for teid in range(teid_start, teid_end):
269 r = cls.vapi.gtpu_add_del_tunnel(
270 decap_next_index=0xFFFFFFFF,
271 src_address=cls.pg0.local_ip4,
272 dst_address=cls.mcast_ip4,
276 if r.sw_if_index == 0xffffffff:
277 raise ValueError("bad sw_if_index: ~0")
280 def add_shared_mcast_dst_load(cls):
281 cls.add_del_shared_mcast_dst_load(is_add=1)
284 def del_shared_mcast_dst_load(cls):
285 cls.add_del_shared_mcast_dst_load(is_add=0)
288 def add_del_mcast_tunnels_load(cls, is_add):
290 add or del tunnels to test gtpu stability
292 n_distinct_dst_tunnels = 20
294 ip_range_end = ip_range_start + n_distinct_dst_tunnels
295 for dest_ip4 in ip4_range(cls.mcast_ip4, ip_range_start,
297 teid = int(dest_ip4.split('.')[3])
298 cls.vapi.gtpu_add_del_tunnel(
299 decap_next_index=0xFFFFFFFF,
300 src_address=cls.pg0.local_ip4,
301 dst_address=dest_ip4,
307 def add_mcast_tunnels_load(cls):
308 cls.add_del_mcast_tunnels_load(is_add=1)
311 def del_mcast_tunnels_load(cls):
312 cls.add_del_mcast_tunnels_load(is_add=0)
314 # Class method to start the GTPU test case.
315 # Overrides setUpClass method in VppTestCase class.
316 # Python try..except statement is used to ensure that the tear down of
317 # the class will be executed even if exception is raised.
318 # @param cls The class pointer.
321 super(TestGtpu, cls).setUpClass()
327 # Create 2 pg interfaces.
328 cls.create_pg_interfaces(range(4))
329 for pg in cls.pg_interfaces:
332 # Configure IPv4 addresses on VPP pg0.
335 # Resolve MAC address for VPP's IP address on pg0.
336 cls.pg0.resolve_arp()
338 # Our Multicast address
339 cls.mcast_ip4 = '239.1.1.1'
340 cls.mcast_mac = util.mcast_ip_to_mac(cls.mcast_ip4)
342 # Create GTPU VTEP on VPP pg0, and put gtpu_tunnel0 and pg1
344 cls.single_tunnel_bd = 11
345 cls.single_tunnel_vni = 11
346 r = cls.vapi.gtpu_add_del_tunnel(
348 mcast_sw_if_index=0xFFFFFFFF,
349 decap_next_index=0xFFFFFFFF,
350 src_address=cls.pg0.local_ip4,
351 dst_address=cls.pg0.remote_ip4,
352 teid=cls.single_tunnel_vni)
353 cls.vapi.sw_interface_set_l2_bridge(rx_sw_if_index=r.sw_if_index,
354 bd_id=cls.single_tunnel_bd)
355 cls.vapi.sw_interface_set_l2_bridge(
356 rx_sw_if_index=cls.pg1.sw_if_index, bd_id=cls.single_tunnel_bd)
358 # Setup teid 2 to test multicast flooding
359 cls.n_ucast_tunnels = 10
360 cls.mcast_flood_bd = 12
361 cls.create_gtpu_flood_test_bd(cls.mcast_flood_bd,
363 r = cls.vapi.gtpu_add_del_tunnel(
365 src_address=cls.pg0.local_ip4,
366 dst_address=cls.mcast_ip4,
368 decap_next_index=0xFFFFFFFF,
369 teid=cls.mcast_flood_bd)
370 cls.vapi.sw_interface_set_l2_bridge(rx_sw_if_index=r.sw_if_index,
371 bd_id=cls.mcast_flood_bd)
372 cls.vapi.sw_interface_set_l2_bridge(
373 rx_sw_if_index=cls.pg2.sw_if_index, bd_id=cls.mcast_flood_bd)
375 # Add and delete mcast tunnels to check stability
376 cls.add_shared_mcast_dst_load()
377 cls.add_mcast_tunnels_load()
378 cls.del_shared_mcast_dst_load()
379 cls.del_mcast_tunnels_load()
381 # Setup teid 3 to test unicast flooding
382 cls.ucast_flood_bd = 13
383 cls.create_gtpu_flood_test_bd(cls.ucast_flood_bd,
385 cls.vapi.sw_interface_set_l2_bridge(
386 rx_sw_if_index=cls.pg3.sw_if_index, bd_id=cls.ucast_flood_bd)
388 super(TestGtpu, cls).tearDownClass()
392 def tearDownClass(cls):
393 super(TestGtpu, cls).tearDownClass()
395 # Method to define VPP actions before tear down of the test case.
396 # Overrides tearDown method in VppTestCase class.
397 # @param self The object pointer.
399 super(TestGtpu, self).tearDown()
401 def show_commands_at_teardown(self):
402 self.logger.info(self.vapi.cli("show bridge-domain 11 detail"))
403 self.logger.info(self.vapi.cli("show bridge-domain 12 detail"))
404 self.logger.info(self.vapi.cli("show bridge-domain 13 detail"))
405 self.logger.info(self.vapi.cli("show int"))
406 self.logger.info(self.vapi.cli("show gtpu tunnel"))
407 self.logger.info(self.vapi.cli("show trace"))
410 if __name__ == '__main__':
411 unittest.main(testRunner=VppTestRunner)