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.contrib.gtp import GTP_U_Header
12 from scapy.utils import atol
15 class TestGtpu(BridgeDomain, VppTestCase):
16 """ GTPU Test Case """
18 def __init__(self, *args):
19 BridgeDomain.__init__(self)
20 VppTestCase.__init__(self, *args)
22 def encapsulate(self, pkt, vni):
24 Encapsulate the original payload frame by adding GTPU header with its
25 UDP, IP and Ethernet fields
27 return (Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac) /
28 IP(src=self.pg0.remote_ip4, dst=self.pg0.local_ip4) /
29 UDP(sport=self.dport, dport=self.dport, chksum=0) /
30 GTP_U_Header(teid=vni, gtp_type=self.gtp_type, length=150) /
33 def ip_range(self, start, end):
34 """ range of remote ip's """
35 return ip4_range(self.pg0.remote_ip4, start, end)
37 def encap_mcast(self, pkt, src_ip, src_mac, vni):
39 Encapsulate the original payload frame by adding GTPU header with its
40 UDP, IP and Ethernet fields
42 return (Ether(src=src_mac, dst=self.mcast_mac) /
43 IP(src=src_ip, dst=self.mcast_ip4) /
44 UDP(sport=self.dport, dport=self.dport, chksum=0) /
45 GTP_U_Header(teid=vni, gtp_type=self.gtp_type, length=150) /
48 def decapsulate(self, pkt):
50 Decapsulate the original payload frame by removing GTPU header
52 return pkt[GTP_U_Header].payload
54 # Method for checking GTPU encapsulation.
56 def check_encapsulation(self, pkt, vni, local_only=False, mcast_pkt=False):
57 # Verify source MAC is VPP_MAC and destination MAC is MY_MAC resolved
59 self.assertEqual(pkt[Ether].src, self.pg0.local_mac)
62 self.assertEqual(pkt[Ether].dst, self.pg0.remote_mac)
64 self.assertEqual(pkt[Ether].dst, type(self).mcast_mac)
65 # Verify GTPU tunnel source IP is VPP_IP and destination IP is MY_IP.
66 self.assertEqual(pkt[IP].src, self.pg0.local_ip4)
69 self.assertEqual(pkt[IP].dst, self.pg0.remote_ip4)
71 self.assertEqual(pkt[IP].dst, type(self).mcast_ip4)
72 # Verify UDP destination port is GTPU 2152, source UDP port could be
74 self.assertEqual(pkt[UDP].dport, type(self).dport)
76 self.assertEqual(pkt[GTP_U_Header].teid, vni)
79 """ Encapsulation test
81 Verify receipt of encapsulated frames on pg0
83 self.pg1.add_stream([self.frame_reply])
85 self.pg0.enable_capture()
89 # Pick first received frame and check if it's correctly encapsulated.
90 out = self.pg0.get_capture(1)
92 self.check_encapsulation(pkt, self.single_tunnel_bd)
94 # payload = self.decapsulate(pkt)
95 # self.assert_eq_pkts(payload, self.frame_reply)
97 def test_ucast_flood(self):
98 """ Unicast flood test
100 Verify receipt of encapsulated frames on pg0
102 self.pg3.add_stream([self.frame_reply])
104 self.pg0.enable_capture()
108 # Get packet from each tunnel and assert it's correctly encapsulated.
109 out = self.pg0.get_capture(self.n_ucast_tunnels)
111 self.check_encapsulation(pkt, self.ucast_flood_bd, True)
112 # payload = self.decapsulate(pkt)
113 # self.assert_eq_pkts(payload, self.frame_reply)
115 def test_mcast_flood(self):
116 """ Multicast flood test
118 Verify receipt of encapsulated frames on pg0
120 self.pg2.add_stream([self.frame_reply])
122 self.pg0.enable_capture()
126 # Pick first received frame and check if it's correctly encapsulated.
127 out = self.pg0.get_capture(1)
129 self.check_encapsulation(pkt, self.mcast_flood_bd,
130 local_only=False, mcast_pkt=True)
132 # payload = self.decapsulate(pkt)
133 # self.assert_eq_pkts(payload, self.frame_reply)
136 def create_gtpu_flood_test_bd(cls, teid, n_ucast_tunnels):
137 # Create 10 ucast gtpu tunnels under bd
139 ip_range_end = ip_range_start + n_ucast_tunnels
140 next_hop_address = cls.pg0.remote_ip4n
141 for dest_ip4n in ip4n_range(next_hop_address, ip_range_start,
143 # add host route so dest_ip4n will not be resolved
144 cls.vapi.ip_add_del_route(dst_address=dest_ip4n,
145 dst_address_length=32,
146 next_hop_address=next_hop_address)
147 r = cls.vapi.gtpu_add_del_tunnel(
148 src_addr=cls.pg0.local_ip4n,
151 cls.vapi.sw_interface_set_l2_bridge(rx_sw_if_index=r.sw_if_index,
155 def add_del_shared_mcast_dst_load(cls, is_add):
157 add or del tunnels sharing the same mcast dst
158 to test gtpu ref_count mechanism
160 n_shared_dst_tunnels = 20
162 teid_end = teid_start + n_shared_dst_tunnels
163 for teid in range(teid_start, teid_end):
164 r = cls.vapi.gtpu_add_del_tunnel(
165 src_addr=cls.pg0.local_ip4n,
166 dst_addr=cls.mcast_ip4n,
170 if r.sw_if_index == 0xffffffff:
171 raise ValueError("bad sw_if_index: ~0")
174 def add_shared_mcast_dst_load(cls):
175 cls.add_del_shared_mcast_dst_load(is_add=1)
178 def del_shared_mcast_dst_load(cls):
179 cls.add_del_shared_mcast_dst_load(is_add=0)
182 def add_del_mcast_tunnels_load(cls, is_add):
184 add or del tunnels to test gtpu stability
186 n_distinct_dst_tunnels = 20
188 ip_range_end = ip_range_start + n_distinct_dst_tunnels
189 for dest_ip4n in ip4n_range(cls.mcast_ip4n, ip_range_start,
191 teid = bytearray(dest_ip4n)[3]
192 cls.vapi.gtpu_add_del_tunnel(
193 src_addr=cls.pg0.local_ip4n,
200 def add_mcast_tunnels_load(cls):
201 cls.add_del_mcast_tunnels_load(is_add=1)
204 def del_mcast_tunnels_load(cls):
205 cls.add_del_mcast_tunnels_load(is_add=0)
207 # Class method to start the GTPU test case.
208 # Overrides setUpClass method in VppTestCase class.
209 # Python try..except statement is used to ensure that the tear down of
210 # the class will be executed even if exception is raised.
211 # @param cls The class pointer.
214 super(TestGtpu, cls).setUpClass()
220 # Create 2 pg interfaces.
221 cls.create_pg_interfaces(range(4))
222 for pg in cls.pg_interfaces:
225 # Configure IPv4 addresses on VPP pg0.
228 # Resolve MAC address for VPP's IP address on pg0.
229 cls.pg0.resolve_arp()
231 # Our Multicast address
232 cls.mcast_ip4 = '239.1.1.1'
233 cls.mcast_ip4n = socket.inet_pton(socket.AF_INET, cls.mcast_ip4)
234 iplong = atol(cls.mcast_ip4)
235 cls.mcast_mac = "01:00:5e:%02x:%02x:%02x" % (
236 (iplong >> 16) & 0x7F, (iplong >> 8) & 0xFF, iplong & 0xFF)
238 # Create GTPU VTEP on VPP pg0, and put gtpu_tunnel0 and pg1
240 cls.single_tunnel_bd = 11
241 r = cls.vapi.gtpu_add_del_tunnel(
242 src_addr=cls.pg0.local_ip4n,
243 dst_addr=cls.pg0.remote_ip4n,
244 teid=cls.single_tunnel_bd)
245 cls.vapi.sw_interface_set_l2_bridge(rx_sw_if_index=r.sw_if_index,
246 bd_id=cls.single_tunnel_bd)
247 cls.vapi.sw_interface_set_l2_bridge(
248 rx_sw_if_index=cls.pg1.sw_if_index, bd_id=cls.single_tunnel_bd)
250 # Setup teid 2 to test multicast flooding
251 cls.n_ucast_tunnels = 10
252 cls.mcast_flood_bd = 12
253 cls.create_gtpu_flood_test_bd(cls.mcast_flood_bd,
255 r = cls.vapi.gtpu_add_del_tunnel(
256 src_addr=cls.pg0.local_ip4n,
257 dst_addr=cls.mcast_ip4n,
259 teid=cls.mcast_flood_bd)
260 cls.vapi.sw_interface_set_l2_bridge(rx_sw_if_index=r.sw_if_index,
261 bd_id=cls.mcast_flood_bd)
262 cls.vapi.sw_interface_set_l2_bridge(
263 rx_sw_if_index=cls.pg2.sw_if_index, bd_id=cls.mcast_flood_bd)
265 # Add and delete mcast tunnels to check stability
266 cls.add_shared_mcast_dst_load()
267 cls.add_mcast_tunnels_load()
268 cls.del_shared_mcast_dst_load()
269 cls.del_mcast_tunnels_load()
271 # Setup teid 3 to test unicast flooding
272 cls.ucast_flood_bd = 13
273 cls.create_gtpu_flood_test_bd(cls.ucast_flood_bd,
275 cls.vapi.sw_interface_set_l2_bridge(
276 rx_sw_if_index=cls.pg3.sw_if_index, bd_id=cls.ucast_flood_bd)
278 super(TestGtpu, cls).tearDownClass()
282 def tearDownClass(cls):
283 super(TestGtpu, cls).tearDownClass()
285 # Method to define VPP actions before tear down of the test case.
286 # Overrides tearDown method in VppTestCase class.
287 # @param self The object pointer.
289 super(TestGtpu, self).tearDown()
291 def show_commands_at_teardown(self):
292 self.logger.info(self.vapi.cli("show bridge-domain 11 detail"))
293 self.logger.info(self.vapi.cli("show bridge-domain 12 detail"))
294 self.logger.info(self.vapi.cli("show bridge-domain 13 detail"))
295 self.logger.info(self.vapi.cli("show int"))
296 self.logger.info(self.vapi.cli("show gtpu tunnel"))
297 self.logger.info(self.vapi.cli("show trace"))
300 if __name__ == '__main__':
301 unittest.main(testRunner=VppTestRunner)