4 from util import ip4_range
6 from framework import tag_fixme_vpp_workers
7 from framework import VppTestCase, VppTestRunner
8 from template_bd import BridgeDomain
10 from scapy.layers.l2 import Ether
11 from scapy.packet import Raw
12 from scapy.layers.inet import IP, UDP
13 from scapy.layers.inet6 import IPv6
14 from scapy.contrib.gtp import GTP_U_Header
15 from scapy.utils import atol
18 from vpp_ip_route import VppIpRoute, VppRoutePath
19 from vpp_ip import INVALID_INDEX
22 @tag_fixme_vpp_workers
23 class TestGtpuUDP(VppTestCase):
24 """ GTPU UDP ports Test Case """
27 super(TestGtpuUDP, self).setUp()
34 self.create_pg_interfaces(range(1))
35 for pg in self.pg_interfaces:
40 def _check_udp_port_ip4(self, enabled=True):
42 pkt = (Ether(src=self.pg0.local_mac, dst=self.pg0.remote_mac) /
43 IP(src=self.pg0.remote_ip4, dst=self.pg0.local_ip4) /
44 UDP(sport=self.dport, dport=self.dport, chksum=0))
46 self.pg0.add_stream(pkt)
49 err = self.statistics.get_counter(
50 '/err/ip4-udp-lookup/No listener for dst port')[0]
53 self.assertEqual(err, self.ip4_err)
55 self.assertEqual(err, self.ip4_err + 1)
59 def _check_udp_port_ip6(self, enabled=True):
61 pkt = (Ether(src=self.pg0.local_mac, dst=self.pg0.remote_mac) /
62 IPv6(src=self.pg0.remote_ip6, dst=self.pg0.local_ip6) /
63 UDP(sport=self.dport, dport=self.dport, chksum=0))
65 self.pg0.add_stream(pkt)
68 err = self.statistics.get_counter(
69 '/err/ip6-udp-lookup/No listener for dst port')[0]
72 self.assertEqual(err, self.ip6_err)
74 self.assertEqual(err, self.ip6_err + 1)
78 def test_udp_port(self):
80 Check if there are no udp listeners before gtpu is enabled
82 # UDP ports should be disabled unless a tunnel is configured
83 self._check_udp_port_ip4(False)
84 self._check_udp_port_ip6(False)
86 r = self.vapi.gtpu_add_del_tunnel(is_add=True,
87 mcast_sw_if_index=0xFFFFFFFF,
88 decap_next_index=0xFFFFFFFF,
89 src_address=self.pg0.local_ip4,
90 dst_address=self.pg0.remote_ip4)
92 # UDP port 2152 enabled for ip4
93 self._check_udp_port_ip4()
95 r = self.vapi.gtpu_add_del_tunnel(is_add=True,
96 mcast_sw_if_index=0xFFFFFFFF,
97 decap_next_index=0xFFFFFFFF,
98 src_address=self.pg0.local_ip6,
99 dst_address=self.pg0.remote_ip6)
101 # UDP port 2152 enabled for ip6
102 self._check_udp_port_ip6()
104 r = self.vapi.gtpu_add_del_tunnel(is_add=False,
105 mcast_sw_if_index=0xFFFFFFFF,
106 decap_next_index=0xFFFFFFFF,
107 src_address=self.pg0.local_ip4,
108 dst_address=self.pg0.remote_ip4)
110 r = self.vapi.gtpu_add_del_tunnel(is_add=False,
111 mcast_sw_if_index=0xFFFFFFFF,
112 decap_next_index=0xFFFFFFFF,
113 src_address=self.pg0.local_ip6,
114 dst_address=self.pg0.remote_ip6)
117 class TestGtpu(BridgeDomain, VppTestCase):
118 """ GTPU Test Case """
120 def __init__(self, *args):
121 BridgeDomain.__init__(self)
122 VppTestCase.__init__(self, *args)
124 def encapsulate(self, pkt, vni):
126 Encapsulate the original payload frame by adding GTPU header with its
127 UDP, IP and Ethernet fields
129 return (Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac) /
130 IP(src=self.pg0.remote_ip4, dst=self.pg0.local_ip4) /
131 UDP(sport=self.dport, dport=self.dport, chksum=0) /
132 GTP_U_Header(teid=vni, gtp_type=self.gtp_type, length=150) /
135 def ip_range(self, start, end):
136 """ range of remote ip's """
137 return ip4_range(self.pg0.remote_ip4, start, end)
139 def encap_mcast(self, pkt, src_ip, src_mac, vni):
141 Encapsulate the original payload frame by adding GTPU header with its
142 UDP, IP and Ethernet fields
144 return (Ether(src=src_mac, dst=self.mcast_mac) /
145 IP(src=src_ip, dst=self.mcast_ip4) /
146 UDP(sport=self.dport, dport=self.dport, chksum=0) /
147 GTP_U_Header(teid=vni, gtp_type=self.gtp_type, length=150) /
150 def decapsulate(self, pkt):
152 Decapsulate the original payload frame by removing GTPU header
154 return pkt[GTP_U_Header].payload
156 # Method for checking GTPU encapsulation.
158 def check_encapsulation(self, pkt, vni, local_only=False, mcast_pkt=False):
159 # Verify source MAC is VPP_MAC and destination MAC is MY_MAC resolved
161 self.assertEqual(pkt[Ether].src, self.pg0.local_mac)
164 self.assertEqual(pkt[Ether].dst, self.pg0.remote_mac)
166 self.assertEqual(pkt[Ether].dst, type(self).mcast_mac)
167 # Verify GTPU tunnel source IP is VPP_IP and destination IP is MY_IP.
168 self.assertEqual(pkt[IP].src, self.pg0.local_ip4)
171 self.assertEqual(pkt[IP].dst, self.pg0.remote_ip4)
173 self.assertEqual(pkt[IP].dst, type(self).mcast_ip4)
174 # Verify UDP destination port is GTPU 2152, source UDP port could be
176 self.assertEqual(pkt[UDP].dport, type(self).dport)
178 self.assertEqual(pkt[GTP_U_Header].teid, vni)
180 def test_encap(self):
181 """ Encapsulation test
183 Verify receipt of encapsulated frames on pg0
185 self.pg1.add_stream([self.frame_reply])
187 self.pg0.enable_capture()
191 # Pick first received frame and check if it's correctly encapsulated.
192 out = self.pg0.get_capture(1)
194 self.check_encapsulation(pkt, self.single_tunnel_vni)
196 # payload = self.decapsulate(pkt)
197 # self.assert_eq_pkts(payload, self.frame_reply)
199 def test_ucast_flood(self):
200 """ Unicast flood test
202 Verify receipt of encapsulated frames on pg0
204 self.pg3.add_stream([self.frame_reply])
206 self.pg0.enable_capture()
210 # Get packet from each tunnel and assert it's correctly encapsulated.
211 out = self.pg0.get_capture(self.n_ucast_tunnels)
213 self.check_encapsulation(pkt, self.ucast_flood_bd, True)
214 # payload = self.decapsulate(pkt)
215 # self.assert_eq_pkts(payload, self.frame_reply)
217 def test_mcast_flood(self):
218 """ Multicast flood test
220 Verify receipt of encapsulated frames on pg0
222 self.pg2.add_stream([self.frame_reply])
224 self.pg0.enable_capture()
228 # Pick first received frame and check if it's correctly encapsulated.
229 out = self.pg0.get_capture(1)
231 self.check_encapsulation(pkt, self.mcast_flood_bd,
232 local_only=False, mcast_pkt=True)
234 # payload = self.decapsulate(pkt)
235 # self.assert_eq_pkts(payload, self.frame_reply)
238 def create_gtpu_flood_test_bd(cls, teid, n_ucast_tunnels):
239 # Create 10 ucast gtpu tunnels under bd
241 ip_range_end = ip_range_start + n_ucast_tunnels
242 next_hop_address = cls.pg0.remote_ip4
243 for dest_ip4 in ip4_range(next_hop_address, ip_range_start,
245 # add host route so dest_ip4 will not be resolved
246 rip = VppIpRoute(cls, dest_ip4, 32,
247 [VppRoutePath(next_hop_address,
251 r = cls.vapi.gtpu_add_del_tunnel(
253 mcast_sw_if_index=0xFFFFFFFF,
254 decap_next_index=0xFFFFFFFF,
255 src_address=cls.pg0.local_ip4,
256 dst_address=dest_ip4,
258 cls.vapi.sw_interface_set_l2_bridge(rx_sw_if_index=r.sw_if_index,
262 def add_del_shared_mcast_dst_load(cls, is_add):
264 add or del tunnels sharing the same mcast dst
265 to test gtpu ref_count mechanism
267 n_shared_dst_tunnels = 20
269 teid_end = teid_start + n_shared_dst_tunnels
270 for teid in range(teid_start, teid_end):
271 r = cls.vapi.gtpu_add_del_tunnel(
272 decap_next_index=0xFFFFFFFF,
273 src_address=cls.pg0.local_ip4,
274 dst_address=cls.mcast_ip4,
278 if r.sw_if_index == 0xffffffff:
279 raise ValueError("bad sw_if_index: ~0")
282 def add_shared_mcast_dst_load(cls):
283 cls.add_del_shared_mcast_dst_load(is_add=1)
286 def del_shared_mcast_dst_load(cls):
287 cls.add_del_shared_mcast_dst_load(is_add=0)
290 def add_del_mcast_tunnels_load(cls, is_add):
292 add or del tunnels to test gtpu stability
294 n_distinct_dst_tunnels = 20
296 ip_range_end = ip_range_start + n_distinct_dst_tunnels
297 for dest_ip4 in ip4_range(cls.mcast_ip4, ip_range_start,
299 teid = int(dest_ip4.split('.')[3])
300 cls.vapi.gtpu_add_del_tunnel(
301 decap_next_index=0xFFFFFFFF,
302 src_address=cls.pg0.local_ip4,
303 dst_address=dest_ip4,
309 def add_mcast_tunnels_load(cls):
310 cls.add_del_mcast_tunnels_load(is_add=1)
313 def del_mcast_tunnels_load(cls):
314 cls.add_del_mcast_tunnels_load(is_add=0)
316 # Class method to start the GTPU test case.
317 # Overrides setUpClass method in VppTestCase class.
318 # Python try..except statement is used to ensure that the tear down of
319 # the class will be executed even if exception is raised.
320 # @param cls The class pointer.
323 super(TestGtpu, cls).setUpClass()
329 # Create 2 pg interfaces.
330 cls.create_pg_interfaces(range(4))
331 for pg in cls.pg_interfaces:
334 # Configure IPv4 addresses on VPP pg0.
337 # Resolve MAC address for VPP's IP address on pg0.
338 cls.pg0.resolve_arp()
340 # Our Multicast address
341 cls.mcast_ip4 = '239.1.1.1'
342 cls.mcast_mac = util.mcast_ip_to_mac(cls.mcast_ip4)
344 # Create GTPU VTEP on VPP pg0, and put gtpu_tunnel0 and pg1
346 cls.single_tunnel_bd = 11
347 cls.single_tunnel_vni = 11
348 r = cls.vapi.gtpu_add_del_tunnel(
350 mcast_sw_if_index=0xFFFFFFFF,
351 decap_next_index=0xFFFFFFFF,
352 src_address=cls.pg0.local_ip4,
353 dst_address=cls.pg0.remote_ip4,
354 teid=cls.single_tunnel_vni)
355 cls.vapi.sw_interface_set_l2_bridge(rx_sw_if_index=r.sw_if_index,
356 bd_id=cls.single_tunnel_bd)
357 cls.vapi.sw_interface_set_l2_bridge(
358 rx_sw_if_index=cls.pg1.sw_if_index, bd_id=cls.single_tunnel_bd)
360 # Setup teid 2 to test multicast flooding
361 cls.n_ucast_tunnels = 10
362 cls.mcast_flood_bd = 12
363 cls.create_gtpu_flood_test_bd(cls.mcast_flood_bd,
365 r = cls.vapi.gtpu_add_del_tunnel(
367 src_address=cls.pg0.local_ip4,
368 dst_address=cls.mcast_ip4,
370 decap_next_index=0xFFFFFFFF,
371 teid=cls.mcast_flood_bd)
372 cls.vapi.sw_interface_set_l2_bridge(rx_sw_if_index=r.sw_if_index,
373 bd_id=cls.mcast_flood_bd)
374 cls.vapi.sw_interface_set_l2_bridge(
375 rx_sw_if_index=cls.pg2.sw_if_index, bd_id=cls.mcast_flood_bd)
377 # Add and delete mcast tunnels to check stability
378 cls.add_shared_mcast_dst_load()
379 cls.add_mcast_tunnels_load()
380 cls.del_shared_mcast_dst_load()
381 cls.del_mcast_tunnels_load()
383 # Setup teid 3 to test unicast flooding
384 cls.ucast_flood_bd = 13
385 cls.create_gtpu_flood_test_bd(cls.ucast_flood_bd,
387 cls.vapi.sw_interface_set_l2_bridge(
388 rx_sw_if_index=cls.pg3.sw_if_index, bd_id=cls.ucast_flood_bd)
390 super(TestGtpu, cls).tearDownClass()
394 def tearDownClass(cls):
395 super(TestGtpu, cls).tearDownClass()
397 # Method to define VPP actions before tear down of the test case.
398 # Overrides tearDown method in VppTestCase class.
399 # @param self The object pointer.
401 super(TestGtpu, self).tearDown()
403 def show_commands_at_teardown(self):
404 self.logger.info(self.vapi.cli("show bridge-domain 11 detail"))
405 self.logger.info(self.vapi.cli("show bridge-domain 12 detail"))
406 self.logger.info(self.vapi.cli("show bridge-domain 13 detail"))
407 self.logger.info(self.vapi.cli("show int"))
408 self.logger.info(self.vapi.cli("show gtpu tunnel"))
409 self.logger.info(self.vapi.cli("show trace"))
412 if __name__ == '__main__':
413 unittest.main(testRunner=VppTestRunner)