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 corectly 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 corectly 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 corectly 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(dest_ip4n, 32, next_hop_address)
145 r = cls.vapi.gtpu_add_del_tunnel(
146 src_addr=cls.pg0.local_ip4n,
149 cls.vapi.sw_interface_set_l2_bridge(r.sw_if_index, bd_id=teid)
152 def add_del_shared_mcast_dst_load(cls, is_add):
154 add or del tunnels sharing the same mcast dst
155 to test gtpu ref_count mechanism
157 n_shared_dst_tunnels = 20
159 teid_end = teid_start + n_shared_dst_tunnels
160 for teid in range(teid_start, teid_end):
161 r = cls.vapi.gtpu_add_del_tunnel(
162 src_addr=cls.pg0.local_ip4n,
163 dst_addr=cls.mcast_ip4n,
167 if r.sw_if_index == 0xffffffff:
168 raise "bad sw_if_index"
171 def add_shared_mcast_dst_load(cls):
172 cls.add_del_shared_mcast_dst_load(is_add=1)
175 def del_shared_mcast_dst_load(cls):
176 cls.add_del_shared_mcast_dst_load(is_add=0)
179 def add_del_mcast_tunnels_load(cls, is_add):
181 add or del tunnels to test gtpu stability
183 n_distinct_dst_tunnels = 20
185 ip_range_end = ip_range_start + n_distinct_dst_tunnels
186 for dest_ip4n in ip4n_range(cls.mcast_ip4n, ip_range_start,
188 teid = bytearray(dest_ip4n)[3]
189 cls.vapi.gtpu_add_del_tunnel(
190 src_addr=cls.pg0.local_ip4n,
197 def add_mcast_tunnels_load(cls):
198 cls.add_del_mcast_tunnels_load(is_add=1)
201 def del_mcast_tunnels_load(cls):
202 cls.add_del_mcast_tunnels_load(is_add=0)
204 # Class method to start the GTPU test case.
205 # Overrides setUpClass method in VppTestCase class.
206 # Python try..except statement is used to ensure that the tear down of
207 # the class will be executed even if exception is raised.
208 # @param cls The class pointer.
211 super(TestGtpu, cls).setUpClass()
217 # Create 2 pg interfaces.
218 cls.create_pg_interfaces(range(4))
219 for pg in cls.pg_interfaces:
222 # Configure IPv4 addresses on VPP pg0.
225 # Resolve MAC address for VPP's IP address on pg0.
226 cls.pg0.resolve_arp()
228 # Our Multicast address
229 cls.mcast_ip4 = '239.1.1.1'
230 cls.mcast_ip4n = socket.inet_pton(socket.AF_INET, cls.mcast_ip4)
231 iplong = atol(cls.mcast_ip4)
232 cls.mcast_mac = "01:00:5e:%02x:%02x:%02x" % (
233 (iplong >> 16) & 0x7F, (iplong >> 8) & 0xFF, iplong & 0xFF)
235 # Create GTPU VTEP on VPP pg0, and put gtpu_tunnel0 and pg1
237 cls.single_tunnel_bd = 11
238 r = cls.vapi.gtpu_add_del_tunnel(
239 src_addr=cls.pg0.local_ip4n,
240 dst_addr=cls.pg0.remote_ip4n,
241 teid=cls.single_tunnel_bd)
242 cls.vapi.sw_interface_set_l2_bridge(r.sw_if_index,
243 bd_id=cls.single_tunnel_bd)
244 cls.vapi.sw_interface_set_l2_bridge(cls.pg1.sw_if_index,
245 bd_id=cls.single_tunnel_bd)
247 # Setup teid 2 to test multicast flooding
248 cls.n_ucast_tunnels = 10
249 cls.mcast_flood_bd = 12
250 cls.create_gtpu_flood_test_bd(cls.mcast_flood_bd,
252 r = cls.vapi.gtpu_add_del_tunnel(
253 src_addr=cls.pg0.local_ip4n,
254 dst_addr=cls.mcast_ip4n,
256 teid=cls.mcast_flood_bd)
257 cls.vapi.sw_interface_set_l2_bridge(r.sw_if_index,
258 bd_id=cls.mcast_flood_bd)
259 cls.vapi.sw_interface_set_l2_bridge(cls.pg2.sw_if_index,
260 bd_id=cls.mcast_flood_bd)
262 # Add and delete mcast tunnels to check stability
263 cls.add_shared_mcast_dst_load()
264 cls.add_mcast_tunnels_load()
265 cls.del_shared_mcast_dst_load()
266 cls.del_mcast_tunnels_load()
268 # Setup teid 3 to test unicast flooding
269 cls.ucast_flood_bd = 13
270 cls.create_gtpu_flood_test_bd(cls.ucast_flood_bd,
272 cls.vapi.sw_interface_set_l2_bridge(cls.pg3.sw_if_index,
273 bd_id=cls.ucast_flood_bd)
275 super(TestGtpu, cls).tearDownClass()
278 # Method to define VPP actions before tear down of the test case.
279 # Overrides tearDown method in VppTestCase class.
280 # @param self The object pointer.
282 super(TestGtpu, self).tearDown()
283 if not self.vpp_dead:
284 self.logger.info(self.vapi.cli("show bridge-domain 11 detail"))
285 self.logger.info(self.vapi.cli("show bridge-domain 12 detail"))
286 self.logger.info(self.vapi.cli("show bridge-domain 13 detail"))
287 self.logger.info(self.vapi.cli("show int"))
288 self.logger.info(self.vapi.cli("show gtpu tunnel"))
289 self.logger.info(self.vapi.cli("show trace"))
292 if __name__ == '__main__':
293 unittest.main(testRunner=VppTestRunner)