4 from util import ip4n_range, ip4_range
6 from framework import VppTestCase, VppTestRunner
7 from template_bd import BridgeDomain
9 from scapy.layers.l2 import Ether, Raw
10 from scapy.layers.inet import IP, UDP
11 from scapy.layers.inet6 import IPv6
12 from scapy.contrib.gtp import GTP_U_Header
13 from scapy.utils import atol
14 from vpp_ip_route import VppIpRoute, VppRoutePath
15 from vpp_ip import INVALID_INDEX
18 class TestGtpuUDP(VppTestCase):
19 """ GTPU UDP ports Test Case """
22 super(TestGtpuUDP, self).setUp()
29 self.create_pg_interfaces(range(1))
30 for pg in self.pg_interfaces:
35 def _check_udp_port_ip4(self, enabled=True):
37 pkt = (Ether(src=self.pg0.local_mac, dst=self.pg0.remote_mac) /
38 IP(src=self.pg0.remote_ip4, dst=self.pg0.local_ip4) /
39 UDP(sport=self.dport, dport=self.dport, chksum=0))
41 self.pg0.add_stream(pkt)
44 err = self.statistics.get_counter(
45 '/err/ip4-udp-lookup/no listener for dst port')[0]
48 self.assertEqual(err, self.ip4_err)
50 self.assertEqual(err, self.ip4_err + 1)
54 def _check_udp_port_ip6(self, enabled=True):
56 pkt = (Ether(src=self.pg0.local_mac, dst=self.pg0.remote_mac) /
57 IPv6(src=self.pg0.remote_ip6, dst=self.pg0.local_ip6) /
58 UDP(sport=self.dport, dport=self.dport, chksum=0))
60 self.pg0.add_stream(pkt)
63 err = self.statistics.get_counter(
64 '/err/ip6-udp-lookup/no listener for dst port')[0]
67 self.assertEqual(err, self.ip6_err)
69 self.assertEqual(err, self.ip6_err + 1)
73 def test_udp_port(self):
75 Check if there are no udp listeners before gtpu is enabled
77 # UDP ports should be disabled unless a tunnel is configured
78 self._check_udp_port_ip4(False)
79 self._check_udp_port_ip6(False)
81 r = self.vapi.gtpu_add_del_tunnel(is_add=True,
82 mcast_sw_if_index=0xFFFFFFFF,
83 decap_next_index=0xFFFFFFFF,
84 src_address=self.pg0.local_ip4,
85 dst_address=self.pg0.remote_ip4)
87 # UDP port 2152 enabled for ip4
88 self._check_udp_port_ip4()
90 r = self.vapi.gtpu_add_del_tunnel(is_add=True,
91 mcast_sw_if_index=0xFFFFFFFF,
92 decap_next_index=0xFFFFFFFF,
93 src_address=self.pg0.local_ip6,
94 dst_address=self.pg0.remote_ip6)
96 # UDP port 2152 enabled for ip6
97 self._check_udp_port_ip6()
99 r = self.vapi.gtpu_add_del_tunnel(is_add=False,
100 mcast_sw_if_index=0xFFFFFFFF,
101 decap_next_index=0xFFFFFFFF,
102 src_address=self.pg0.local_ip4,
103 dst_address=self.pg0.remote_ip4)
105 r = self.vapi.gtpu_add_del_tunnel(is_add=False,
106 mcast_sw_if_index=0xFFFFFFFF,
107 decap_next_index=0xFFFFFFFF,
108 src_address=self.pg0.local_ip6,
109 dst_address=self.pg0.remote_ip6)
112 class TestGtpu(BridgeDomain, VppTestCase):
113 """ GTPU Test Case """
115 def __init__(self, *args):
116 BridgeDomain.__init__(self)
117 VppTestCase.__init__(self, *args)
119 def encapsulate(self, pkt, vni):
121 Encapsulate the original payload frame by adding GTPU header with its
122 UDP, IP and Ethernet fields
124 return (Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac) /
125 IP(src=self.pg0.remote_ip4, dst=self.pg0.local_ip4) /
126 UDP(sport=self.dport, dport=self.dport, chksum=0) /
127 GTP_U_Header(teid=vni, gtp_type=self.gtp_type, length=150) /
130 def ip_range(self, start, end):
131 """ range of remote ip's """
132 return ip4_range(self.pg0.remote_ip4, start, end)
134 def encap_mcast(self, pkt, src_ip, src_mac, vni):
136 Encapsulate the original payload frame by adding GTPU header with its
137 UDP, IP and Ethernet fields
139 return (Ether(src=src_mac, dst=self.mcast_mac) /
140 IP(src=src_ip, dst=self.mcast_ip4) /
141 UDP(sport=self.dport, dport=self.dport, chksum=0) /
142 GTP_U_Header(teid=vni, gtp_type=self.gtp_type, length=150) /
145 def decapsulate(self, pkt):
147 Decapsulate the original payload frame by removing GTPU header
149 return pkt[GTP_U_Header].payload
151 # Method for checking GTPU encapsulation.
153 def check_encapsulation(self, pkt, vni, local_only=False, mcast_pkt=False):
154 # Verify source MAC is VPP_MAC and destination MAC is MY_MAC resolved
156 self.assertEqual(pkt[Ether].src, self.pg0.local_mac)
159 self.assertEqual(pkt[Ether].dst, self.pg0.remote_mac)
161 self.assertEqual(pkt[Ether].dst, type(self).mcast_mac)
162 # Verify GTPU tunnel source IP is VPP_IP and destination IP is MY_IP.
163 self.assertEqual(pkt[IP].src, self.pg0.local_ip4)
166 self.assertEqual(pkt[IP].dst, self.pg0.remote_ip4)
168 self.assertEqual(pkt[IP].dst, type(self).mcast_ip4)
169 # Verify UDP destination port is GTPU 2152, source UDP port could be
171 self.assertEqual(pkt[UDP].dport, type(self).dport)
173 self.assertEqual(pkt[GTP_U_Header].teid, vni)
175 def test_encap(self):
176 """ Encapsulation test
178 Verify receipt of encapsulated frames on pg0
180 self.pg1.add_stream([self.frame_reply])
182 self.pg0.enable_capture()
186 # Pick first received frame and check if it's correctly encapsulated.
187 out = self.pg0.get_capture(1)
189 self.check_encapsulation(pkt, self.single_tunnel_bd)
191 # payload = self.decapsulate(pkt)
192 # self.assert_eq_pkts(payload, self.frame_reply)
194 def test_ucast_flood(self):
195 """ Unicast flood test
197 Verify receipt of encapsulated frames on pg0
199 self.pg3.add_stream([self.frame_reply])
201 self.pg0.enable_capture()
205 # Get packet from each tunnel and assert it's correctly encapsulated.
206 out = self.pg0.get_capture(self.n_ucast_tunnels)
208 self.check_encapsulation(pkt, self.ucast_flood_bd, True)
209 # payload = self.decapsulate(pkt)
210 # self.assert_eq_pkts(payload, self.frame_reply)
212 def test_mcast_flood(self):
213 """ Multicast flood test
215 Verify receipt of encapsulated frames on pg0
217 self.pg2.add_stream([self.frame_reply])
219 self.pg0.enable_capture()
223 # Pick first received frame and check if it's correctly encapsulated.
224 out = self.pg0.get_capture(1)
226 self.check_encapsulation(pkt, self.mcast_flood_bd,
227 local_only=False, mcast_pkt=True)
229 # payload = self.decapsulate(pkt)
230 # self.assert_eq_pkts(payload, self.frame_reply)
233 def create_gtpu_flood_test_bd(cls, teid, n_ucast_tunnels):
234 # Create 10 ucast gtpu tunnels under bd
236 ip_range_end = ip_range_start + n_ucast_tunnels
237 next_hop_address = cls.pg0.remote_ip4
238 for dest_ip4 in ip4_range(next_hop_address, ip_range_start,
240 # add host route so dest_ip4n will not be resolved
241 rip = VppIpRoute(cls, dest_ip4, 32,
242 [VppRoutePath(next_hop_address,
246 r = cls.vapi.gtpu_add_del_tunnel(
248 mcast_sw_if_index=0xFFFFFFFF,
249 decap_next_index=0xFFFFFFFF,
250 src_address=cls.pg0.local_ip4,
251 dst_address=dest_ip4,
253 cls.vapi.sw_interface_set_l2_bridge(rx_sw_if_index=r.sw_if_index,
257 def add_del_shared_mcast_dst_load(cls, is_add):
259 add or del tunnels sharing the same mcast dst
260 to test gtpu ref_count mechanism
262 n_shared_dst_tunnels = 20
264 teid_end = teid_start + n_shared_dst_tunnels
265 for teid in range(teid_start, teid_end):
266 r = cls.vapi.gtpu_add_del_tunnel(
267 decap_next_index=0xFFFFFFFF,
268 src_address=cls.pg0.local_ip4,
269 dst_address=cls.mcast_ip4,
273 if r.sw_if_index == 0xffffffff:
274 raise ValueError("bad sw_if_index: ~0")
277 def add_shared_mcast_dst_load(cls):
278 cls.add_del_shared_mcast_dst_load(is_add=1)
281 def del_shared_mcast_dst_load(cls):
282 cls.add_del_shared_mcast_dst_load(is_add=0)
285 def add_del_mcast_tunnels_load(cls, is_add):
287 add or del tunnels to test gtpu stability
289 n_distinct_dst_tunnels = 20
291 ip_range_end = ip_range_start + n_distinct_dst_tunnels
292 for dest_ip4 in ip4_range(cls.mcast_ip4, ip_range_start,
294 teid = int(dest_ip4.split('.')[3])
295 cls.vapi.gtpu_add_del_tunnel(
296 decap_next_index=0xFFFFFFFF,
297 src_address=cls.pg0.local_ip4,
298 dst_address=dest_ip4,
304 def add_mcast_tunnels_load(cls):
305 cls.add_del_mcast_tunnels_load(is_add=1)
308 def del_mcast_tunnels_load(cls):
309 cls.add_del_mcast_tunnels_load(is_add=0)
311 # Class method to start the GTPU test case.
312 # Overrides setUpClass method in VppTestCase class.
313 # Python try..except statement is used to ensure that the tear down of
314 # the class will be executed even if exception is raised.
315 # @param cls The class pointer.
318 super(TestGtpu, cls).setUpClass()
324 # Create 2 pg interfaces.
325 cls.create_pg_interfaces(range(4))
326 for pg in cls.pg_interfaces:
329 # Configure IPv4 addresses on VPP pg0.
332 # Resolve MAC address for VPP's IP address on pg0.
333 cls.pg0.resolve_arp()
335 # Our Multicast address
336 cls.mcast_ip4 = '239.1.1.1'
337 cls.mcast_ip4n = socket.inet_pton(socket.AF_INET, cls.mcast_ip4)
338 iplong = atol(cls.mcast_ip4)
339 cls.mcast_mac = "01:00:5e:%02x:%02x:%02x" % (
340 (iplong >> 16) & 0x7F, (iplong >> 8) & 0xFF, iplong & 0xFF)
342 # Create GTPU VTEP on VPP pg0, and put gtpu_tunnel0 and pg1
344 cls.single_tunnel_bd = 11
345 r = cls.vapi.gtpu_add_del_tunnel(
347 mcast_sw_if_index=0xFFFFFFFF,
348 decap_next_index=0xFFFFFFFF,
349 src_address=cls.pg0.local_ip4,
350 dst_address=cls.pg0.remote_ip4,
351 teid=cls.single_tunnel_bd)
352 cls.vapi.sw_interface_set_l2_bridge(rx_sw_if_index=r.sw_if_index,
353 bd_id=cls.single_tunnel_bd)
354 cls.vapi.sw_interface_set_l2_bridge(
355 rx_sw_if_index=cls.pg1.sw_if_index, bd_id=cls.single_tunnel_bd)
357 # Setup teid 2 to test multicast flooding
358 cls.n_ucast_tunnels = 10
359 cls.mcast_flood_bd = 12
360 cls.create_gtpu_flood_test_bd(cls.mcast_flood_bd,
362 r = cls.vapi.gtpu_add_del_tunnel(
364 src_address=cls.pg0.local_ip4,
365 dst_address=cls.mcast_ip4,
367 decap_next_index=0xFFFFFFFF,
368 teid=cls.mcast_flood_bd)
369 cls.vapi.sw_interface_set_l2_bridge(rx_sw_if_index=r.sw_if_index,
370 bd_id=cls.mcast_flood_bd)
371 cls.vapi.sw_interface_set_l2_bridge(
372 rx_sw_if_index=cls.pg2.sw_if_index, bd_id=cls.mcast_flood_bd)
374 # Add and delete mcast tunnels to check stability
375 cls.add_shared_mcast_dst_load()
376 cls.add_mcast_tunnels_load()
377 cls.del_shared_mcast_dst_load()
378 cls.del_mcast_tunnels_load()
380 # Setup teid 3 to test unicast flooding
381 cls.ucast_flood_bd = 13
382 cls.create_gtpu_flood_test_bd(cls.ucast_flood_bd,
384 cls.vapi.sw_interface_set_l2_bridge(
385 rx_sw_if_index=cls.pg3.sw_if_index, bd_id=cls.ucast_flood_bd)
387 super(TestGtpu, cls).tearDownClass()
391 def tearDownClass(cls):
392 super(TestGtpu, cls).tearDownClass()
394 # Method to define VPP actions before tear down of the test case.
395 # Overrides tearDown method in VppTestCase class.
396 # @param self The object pointer.
398 super(TestGtpu, self).tearDown()
400 def show_commands_at_teardown(self):
401 self.logger.info(self.vapi.cli("show bridge-domain 11 detail"))
402 self.logger.info(self.vapi.cli("show bridge-domain 12 detail"))
403 self.logger.info(self.vapi.cli("show bridge-domain 13 detail"))
404 self.logger.info(self.vapi.cli("show int"))
405 self.logger.info(self.vapi.cli("show gtpu tunnel"))
406 self.logger.info(self.vapi.cli("show trace"))
409 if __name__ == '__main__':
410 unittest.main(testRunner=VppTestRunner)