4 from util import ip4n_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 encap_mcast(self, pkt, src_ip, src_mac, vni):
35 Encapsulate the original payload frame by adding GTPU header with its
36 UDP, IP and Ethernet fields
38 return (Ether(src=src_mac, dst=self.mcast_mac) /
39 IP(src=src_ip, dst=self.mcast_ip4) /
40 UDP(sport=self.dport, dport=self.dport, chksum=0) /
41 GTP_U_Header(teid=vni, gtp_type=self.gtp_type, length=150) /
44 def decapsulate(self, pkt):
46 Decapsulate the original payload frame by removing GTPU header
48 return pkt[GTP_U_Header].payload
50 # Method for checking GTPU encapsulation.
52 def check_encapsulation(self, pkt, vni, local_only=False, mcast_pkt=False):
53 # Verify source MAC is VPP_MAC and destination MAC is MY_MAC resolved
55 self.assertEqual(pkt[Ether].src, self.pg0.local_mac)
58 self.assertEqual(pkt[Ether].dst, self.pg0.remote_mac)
60 self.assertEqual(pkt[Ether].dst, type(self).mcast_mac)
61 # Verify GTPU tunnel source IP is VPP_IP and destination IP is MY_IP.
62 self.assertEqual(pkt[IP].src, self.pg0.local_ip4)
65 self.assertEqual(pkt[IP].dst, self.pg0.remote_ip4)
67 self.assertEqual(pkt[IP].dst, type(self).mcast_ip4)
68 # Verify UDP destination port is GTPU 2152, source UDP port could be
70 self.assertEqual(pkt[UDP].dport, type(self).dport)
72 self.assertEqual(pkt[GTP_U_Header].teid, vni)
75 """ Encapsulation test
77 Verify receipt of encapsulated frames on pg0
79 self.pg1.add_stream([self.frame_reply])
81 self.pg0.enable_capture()
85 # Pick first received frame and check if it's corectly encapsulated.
86 out = self.pg0.get_capture(1)
88 self.check_encapsulation(pkt, self.single_tunnel_bd)
90 # payload = self.decapsulate(pkt)
91 # self.assert_eq_pkts(payload, self.frame_reply)
93 def test_ucast_flood(self):
94 """ Unicast flood test
96 Verify receipt of encapsulated frames on pg0
98 self.pg3.add_stream([self.frame_reply])
100 self.pg0.enable_capture()
104 # Get packet from each tunnel and assert it's corectly encapsulated.
105 out = self.pg0.get_capture(self.n_ucast_tunnels)
107 self.check_encapsulation(pkt, self.ucast_flood_bd, True)
108 # payload = self.decapsulate(pkt)
109 # self.assert_eq_pkts(payload, self.frame_reply)
111 def test_mcast_flood(self):
112 """ Multicast flood test
114 Verify receipt of encapsulated frames on pg0
116 self.pg2.add_stream([self.frame_reply])
118 self.pg0.enable_capture()
122 # Pick first received frame and check if it's corectly encapsulated.
123 out = self.pg0.get_capture(1)
125 self.check_encapsulation(pkt, self.mcast_flood_bd,
126 local_only=False, mcast_pkt=True)
128 # payload = self.decapsulate(pkt)
129 # self.assert_eq_pkts(payload, self.frame_reply)
132 def create_gtpu_flood_test_bd(cls, teid, n_ucast_tunnels):
133 # Create 10 ucast gtpu tunnels under bd
135 ip_range_end = ip_range_start + n_ucast_tunnels
136 next_hop_address = cls.pg0.remote_ip4n
137 for dest_ip4n in ip4n_range(next_hop_address, ip_range_start,
139 # add host route so dest_ip4n will not be resolved
140 cls.vapi.ip_add_del_route(dest_ip4n, 32, next_hop_address)
141 r = cls.vapi.gtpu_add_del_tunnel(
142 src_addr=cls.pg0.local_ip4n,
145 cls.vapi.sw_interface_set_l2_bridge(r.sw_if_index, bd_id=teid)
148 def add_del_shared_mcast_dst_load(cls, is_add):
150 add or del tunnels sharing the same mcast dst
151 to test gtpu ref_count mechanism
153 n_shared_dst_tunnels = 20
155 teid_end = teid_start + n_shared_dst_tunnels
156 for teid in range(teid_start, teid_end):
157 r = cls.vapi.gtpu_add_del_tunnel(
158 src_addr=cls.pg0.local_ip4n,
159 dst_addr=cls.mcast_ip4n,
163 if r.sw_if_index == 0xffffffff:
164 raise "bad sw_if_index"
167 def add_shared_mcast_dst_load(cls):
168 cls.add_del_shared_mcast_dst_load(is_add=1)
171 def del_shared_mcast_dst_load(cls):
172 cls.add_del_shared_mcast_dst_load(is_add=0)
175 def add_del_mcast_tunnels_load(cls, is_add):
177 add or del tunnels to test gtpu stability
179 n_distinct_dst_tunnels = 20
181 ip_range_end = ip_range_start + n_distinct_dst_tunnels
182 for dest_ip4n in ip4n_range(cls.mcast_ip4n, ip_range_start,
184 teid = bytearray(dest_ip4n)[3]
185 cls.vapi.gtpu_add_del_tunnel(
186 src_addr=cls.pg0.local_ip4n,
193 def add_mcast_tunnels_load(cls):
194 cls.add_del_mcast_tunnels_load(is_add=1)
197 def del_mcast_tunnels_load(cls):
198 cls.add_del_mcast_tunnels_load(is_add=0)
200 # Class method to start the GTPU test case.
201 # Overrides setUpClass method in VppTestCase class.
202 # Python try..except statement is used to ensure that the tear down of
203 # the class will be executed even if exception is raised.
204 # @param cls The class pointer.
207 super(TestGtpu, cls).setUpClass()
213 # Create 2 pg interfaces.
214 cls.create_pg_interfaces(range(4))
215 for pg in cls.pg_interfaces:
218 # Configure IPv4 addresses on VPP pg0.
221 # Resolve MAC address for VPP's IP address on pg0.
222 cls.pg0.resolve_arp()
224 # Our Multicast address
225 cls.mcast_ip4 = '239.1.1.1'
226 cls.mcast_ip4n = socket.inet_pton(socket.AF_INET, cls.mcast_ip4)
227 iplong = atol(cls.mcast_ip4)
228 cls.mcast_mac = "01:00:5e:%02x:%02x:%02x" % (
229 (iplong >> 16) & 0x7F, (iplong >> 8) & 0xFF, iplong & 0xFF)
231 # Create GTPU VTEP on VPP pg0, and put gtpu_tunnel0 and pg1
233 cls.single_tunnel_bd = 11
234 r = cls.vapi.gtpu_add_del_tunnel(
235 src_addr=cls.pg0.local_ip4n,
236 dst_addr=cls.pg0.remote_ip4n,
237 teid=cls.single_tunnel_bd)
238 cls.vapi.sw_interface_set_l2_bridge(r.sw_if_index,
239 bd_id=cls.single_tunnel_bd)
240 cls.vapi.sw_interface_set_l2_bridge(cls.pg1.sw_if_index,
241 bd_id=cls.single_tunnel_bd)
243 # Setup teid 2 to test multicast flooding
244 cls.n_ucast_tunnels = 10
245 cls.mcast_flood_bd = 12
246 cls.create_gtpu_flood_test_bd(cls.mcast_flood_bd,
248 r = cls.vapi.gtpu_add_del_tunnel(
249 src_addr=cls.pg0.local_ip4n,
250 dst_addr=cls.mcast_ip4n,
252 teid=cls.mcast_flood_bd)
253 cls.vapi.sw_interface_set_l2_bridge(r.sw_if_index,
254 bd_id=cls.mcast_flood_bd)
255 cls.vapi.sw_interface_set_l2_bridge(cls.pg2.sw_if_index,
256 bd_id=cls.mcast_flood_bd)
258 # Add and delete mcast tunnels to check stability
259 cls.add_shared_mcast_dst_load()
260 cls.add_mcast_tunnels_load()
261 cls.del_shared_mcast_dst_load()
262 cls.del_mcast_tunnels_load()
264 # Setup teid 3 to test unicast flooding
265 cls.ucast_flood_bd = 13
266 cls.create_gtpu_flood_test_bd(cls.ucast_flood_bd,
268 cls.vapi.sw_interface_set_l2_bridge(cls.pg3.sw_if_index,
269 bd_id=cls.ucast_flood_bd)
271 super(TestGtpu, cls).tearDownClass()
274 # Method to define VPP actions before tear down of the test case.
275 # Overrides tearDown method in VppTestCase class.
276 # @param self The object pointer.
278 super(TestGtpu, self).tearDown()
279 if not self.vpp_dead:
280 self.logger.info(self.vapi.cli("show bridge-domain 11 detail"))
281 self.logger.info(self.vapi.cli("show bridge-domain 12 detail"))
282 self.logger.info(self.vapi.cli("show bridge-domain 13 detail"))
283 self.logger.info(self.vapi.cli("show int"))
284 self.logger.info(self.vapi.cli("show gtpu tunnel"))
285 self.logger.info(self.vapi.cli("show trace"))
288 if __name__ == '__main__':
289 unittest.main(testRunner=VppTestRunner)