Hongjun Ni | ef486b1 | 2017-04-12 19:21:16 +0800 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | |
| 3 | import socket |
Eyal Bari | cef1e2a | 2018-06-18 13:01:59 +0300 | [diff] [blame] | 4 | from util import ip4n_range, ip4_range |
Hongjun Ni | ef486b1 | 2017-04-12 19:21:16 +0800 | [diff] [blame] | 5 | import unittest |
| 6 | from framework import VppTestCase, VppTestRunner |
| 7 | from template_bd import BridgeDomain |
| 8 | |
| 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 |
| 13 | |
| 14 | |
| 15 | class TestGtpu(BridgeDomain, VppTestCase): |
| 16 | """ GTPU Test Case """ |
| 17 | |
| 18 | def __init__(self, *args): |
| 19 | BridgeDomain.__init__(self) |
| 20 | VppTestCase.__init__(self, *args) |
| 21 | |
| 22 | def encapsulate(self, pkt, vni): |
| 23 | """ |
| 24 | Encapsulate the original payload frame by adding GTPU header with its |
| 25 | UDP, IP and Ethernet fields |
| 26 | """ |
| 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) / |
Neale Ranns | 2bc9402 | 2018-02-25 12:27:18 -0800 | [diff] [blame] | 30 | GTP_U_Header(teid=vni, gtp_type=self.gtp_type, length=150) / |
Hongjun Ni | ef486b1 | 2017-04-12 19:21:16 +0800 | [diff] [blame] | 31 | pkt) |
| 32 | |
Eyal Bari | cef1e2a | 2018-06-18 13:01:59 +0300 | [diff] [blame] | 33 | def ip_range(self, start, end): |
| 34 | """ range of remote ip's """ |
| 35 | return ip4_range(self.pg0.remote_ip4, start, end) |
| 36 | |
Hongjun Ni | ef486b1 | 2017-04-12 19:21:16 +0800 | [diff] [blame] | 37 | def encap_mcast(self, pkt, src_ip, src_mac, vni): |
| 38 | """ |
| 39 | Encapsulate the original payload frame by adding GTPU header with its |
| 40 | UDP, IP and Ethernet fields |
| 41 | """ |
| 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) / |
Neale Ranns | 2bc9402 | 2018-02-25 12:27:18 -0800 | [diff] [blame] | 45 | GTP_U_Header(teid=vni, gtp_type=self.gtp_type, length=150) / |
Hongjun Ni | ef486b1 | 2017-04-12 19:21:16 +0800 | [diff] [blame] | 46 | pkt) |
| 47 | |
| 48 | def decapsulate(self, pkt): |
| 49 | """ |
| 50 | Decapsulate the original payload frame by removing GTPU header |
| 51 | """ |
| 52 | return pkt[GTP_U_Header].payload |
| 53 | |
| 54 | # Method for checking GTPU encapsulation. |
| 55 | # |
| 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 |
| 58 | # by VPP using ARP. |
| 59 | self.assertEqual(pkt[Ether].src, self.pg0.local_mac) |
| 60 | if not local_only: |
| 61 | if not mcast_pkt: |
| 62 | self.assertEqual(pkt[Ether].dst, self.pg0.remote_mac) |
| 63 | else: |
| 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) |
| 67 | if not local_only: |
| 68 | if not mcast_pkt: |
| 69 | self.assertEqual(pkt[IP].dst, self.pg0.remote_ip4) |
| 70 | else: |
| 71 | self.assertEqual(pkt[IP].dst, type(self).mcast_ip4) |
| 72 | # Verify UDP destination port is GTPU 2152, source UDP port could be |
| 73 | # arbitrary. |
| 74 | self.assertEqual(pkt[UDP].dport, type(self).dport) |
Neale Ranns | 2bc9402 | 2018-02-25 12:27:18 -0800 | [diff] [blame] | 75 | # Verify teid |
| 76 | self.assertEqual(pkt[GTP_U_Header].teid, vni) |
Hongjun Ni | ef486b1 | 2017-04-12 19:21:16 +0800 | [diff] [blame] | 77 | |
| 78 | def test_encap(self): |
| 79 | """ Encapsulation test |
| 80 | Send frames from pg1 |
| 81 | Verify receipt of encapsulated frames on pg0 |
| 82 | """ |
| 83 | self.pg1.add_stream([self.frame_reply]) |
| 84 | |
| 85 | self.pg0.enable_capture() |
| 86 | |
| 87 | self.pg_start() |
| 88 | |
Paul Vinciguerra | 8feeaff | 2019-03-27 11:25:48 -0700 | [diff] [blame] | 89 | # Pick first received frame and check if it's correctly encapsulated. |
Hongjun Ni | ef486b1 | 2017-04-12 19:21:16 +0800 | [diff] [blame] | 90 | out = self.pg0.get_capture(1) |
| 91 | pkt = out[0] |
| 92 | self.check_encapsulation(pkt, self.single_tunnel_bd) |
| 93 | |
| 94 | # payload = self.decapsulate(pkt) |
| 95 | # self.assert_eq_pkts(payload, self.frame_reply) |
| 96 | |
| 97 | def test_ucast_flood(self): |
| 98 | """ Unicast flood test |
| 99 | Send frames from pg3 |
| 100 | Verify receipt of encapsulated frames on pg0 |
| 101 | """ |
| 102 | self.pg3.add_stream([self.frame_reply]) |
| 103 | |
| 104 | self.pg0.enable_capture() |
| 105 | |
| 106 | self.pg_start() |
| 107 | |
Paul Vinciguerra | 8feeaff | 2019-03-27 11:25:48 -0700 | [diff] [blame] | 108 | # Get packet from each tunnel and assert it's correctly encapsulated. |
Hongjun Ni | ef486b1 | 2017-04-12 19:21:16 +0800 | [diff] [blame] | 109 | out = self.pg0.get_capture(self.n_ucast_tunnels) |
| 110 | for pkt in out: |
| 111 | self.check_encapsulation(pkt, self.ucast_flood_bd, True) |
| 112 | # payload = self.decapsulate(pkt) |
| 113 | # self.assert_eq_pkts(payload, self.frame_reply) |
| 114 | |
| 115 | def test_mcast_flood(self): |
| 116 | """ Multicast flood test |
| 117 | Send frames from pg2 |
| 118 | Verify receipt of encapsulated frames on pg0 |
| 119 | """ |
| 120 | self.pg2.add_stream([self.frame_reply]) |
| 121 | |
| 122 | self.pg0.enable_capture() |
| 123 | |
| 124 | self.pg_start() |
| 125 | |
Paul Vinciguerra | 8feeaff | 2019-03-27 11:25:48 -0700 | [diff] [blame] | 126 | # Pick first received frame and check if it's correctly encapsulated. |
Hongjun Ni | ef486b1 | 2017-04-12 19:21:16 +0800 | [diff] [blame] | 127 | out = self.pg0.get_capture(1) |
| 128 | pkt = out[0] |
| 129 | self.check_encapsulation(pkt, self.mcast_flood_bd, |
| 130 | local_only=False, mcast_pkt=True) |
| 131 | |
| 132 | # payload = self.decapsulate(pkt) |
| 133 | # self.assert_eq_pkts(payload, self.frame_reply) |
| 134 | |
| 135 | @classmethod |
| 136 | def create_gtpu_flood_test_bd(cls, teid, n_ucast_tunnels): |
| 137 | # Create 10 ucast gtpu tunnels under bd |
| 138 | ip_range_start = 10 |
| 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, |
| 142 | ip_range_end): |
| 143 | # add host route so dest_ip4n will not be resolved |
Ole Troan | a5b2eec | 2019-03-11 19:23:25 +0100 | [diff] [blame] | 144 | cls.vapi.ip_add_del_route(dst_address=dest_ip4n, |
| 145 | dst_address_length=32, |
| 146 | next_hop_address=next_hop_address) |
Hongjun Ni | ef486b1 | 2017-04-12 19:21:16 +0800 | [diff] [blame] | 147 | r = cls.vapi.gtpu_add_del_tunnel( |
| 148 | src_addr=cls.pg0.local_ip4n, |
| 149 | dst_addr=dest_ip4n, |
| 150 | teid=teid) |
Ole Troan | a5b2eec | 2019-03-11 19:23:25 +0100 | [diff] [blame] | 151 | cls.vapi.sw_interface_set_l2_bridge(rx_sw_if_index=r.sw_if_index, |
| 152 | bd_id=teid) |
Hongjun Ni | ef486b1 | 2017-04-12 19:21:16 +0800 | [diff] [blame] | 153 | |
| 154 | @classmethod |
| 155 | def add_del_shared_mcast_dst_load(cls, is_add): |
| 156 | """ |
| 157 | add or del tunnels sharing the same mcast dst |
| 158 | to test gtpu ref_count mechanism |
| 159 | """ |
| 160 | n_shared_dst_tunnels = 20 |
| 161 | teid_start = 1000 |
| 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, |
| 167 | mcast_sw_if_index=1, |
| 168 | teid=teid, |
| 169 | is_add=is_add) |
| 170 | if r.sw_if_index == 0xffffffff: |
Paul Vinciguerra | c599c6f | 2019-03-12 17:41:27 -0700 | [diff] [blame] | 171 | raise ValueError("bad sw_if_index: ~0") |
Hongjun Ni | ef486b1 | 2017-04-12 19:21:16 +0800 | [diff] [blame] | 172 | |
| 173 | @classmethod |
| 174 | def add_shared_mcast_dst_load(cls): |
| 175 | cls.add_del_shared_mcast_dst_load(is_add=1) |
| 176 | |
| 177 | @classmethod |
| 178 | def del_shared_mcast_dst_load(cls): |
| 179 | cls.add_del_shared_mcast_dst_load(is_add=0) |
| 180 | |
| 181 | @classmethod |
| 182 | def add_del_mcast_tunnels_load(cls, is_add): |
| 183 | """ |
| 184 | add or del tunnels to test gtpu stability |
| 185 | """ |
| 186 | n_distinct_dst_tunnels = 20 |
| 187 | ip_range_start = 10 |
| 188 | ip_range_end = ip_range_start + n_distinct_dst_tunnels |
| 189 | for dest_ip4n in ip4n_range(cls.mcast_ip4n, ip_range_start, |
| 190 | ip_range_end): |
| 191 | teid = bytearray(dest_ip4n)[3] |
| 192 | cls.vapi.gtpu_add_del_tunnel( |
| 193 | src_addr=cls.pg0.local_ip4n, |
| 194 | dst_addr=dest_ip4n, |
| 195 | mcast_sw_if_index=1, |
| 196 | teid=teid, |
| 197 | is_add=is_add) |
| 198 | |
| 199 | @classmethod |
| 200 | def add_mcast_tunnels_load(cls): |
| 201 | cls.add_del_mcast_tunnels_load(is_add=1) |
| 202 | |
| 203 | @classmethod |
| 204 | def del_mcast_tunnels_load(cls): |
| 205 | cls.add_del_mcast_tunnels_load(is_add=0) |
| 206 | |
| 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. |
| 212 | @classmethod |
| 213 | def setUpClass(cls): |
| 214 | super(TestGtpu, cls).setUpClass() |
| 215 | |
| 216 | try: |
| 217 | cls.dport = 2152 |
| 218 | cls.gtp_type = 0xff |
| 219 | |
| 220 | # Create 2 pg interfaces. |
| 221 | cls.create_pg_interfaces(range(4)) |
| 222 | for pg in cls.pg_interfaces: |
| 223 | pg.admin_up() |
| 224 | |
| 225 | # Configure IPv4 addresses on VPP pg0. |
| 226 | cls.pg0.config_ip4() |
| 227 | |
| 228 | # Resolve MAC address for VPP's IP address on pg0. |
| 229 | cls.pg0.resolve_arp() |
| 230 | |
| 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) |
| 237 | |
| 238 | # Create GTPU VTEP on VPP pg0, and put gtpu_tunnel0 and pg1 |
| 239 | # into BD. |
| 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) |
Ole Troan | a5b2eec | 2019-03-11 19:23:25 +0100 | [diff] [blame] | 245 | cls.vapi.sw_interface_set_l2_bridge(rx_sw_if_index=r.sw_if_index, |
Hongjun Ni | ef486b1 | 2017-04-12 19:21:16 +0800 | [diff] [blame] | 246 | bd_id=cls.single_tunnel_bd) |
Ole Troan | a5b2eec | 2019-03-11 19:23:25 +0100 | [diff] [blame] | 247 | cls.vapi.sw_interface_set_l2_bridge( |
| 248 | rx_sw_if_index=cls.pg1.sw_if_index, bd_id=cls.single_tunnel_bd) |
Hongjun Ni | ef486b1 | 2017-04-12 19:21:16 +0800 | [diff] [blame] | 249 | |
| 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, |
| 254 | cls.n_ucast_tunnels) |
| 255 | r = cls.vapi.gtpu_add_del_tunnel( |
| 256 | src_addr=cls.pg0.local_ip4n, |
| 257 | dst_addr=cls.mcast_ip4n, |
| 258 | mcast_sw_if_index=1, |
| 259 | teid=cls.mcast_flood_bd) |
Ole Troan | a5b2eec | 2019-03-11 19:23:25 +0100 | [diff] [blame] | 260 | cls.vapi.sw_interface_set_l2_bridge(rx_sw_if_index=r.sw_if_index, |
Hongjun Ni | ef486b1 | 2017-04-12 19:21:16 +0800 | [diff] [blame] | 261 | bd_id=cls.mcast_flood_bd) |
Ole Troan | a5b2eec | 2019-03-11 19:23:25 +0100 | [diff] [blame] | 262 | cls.vapi.sw_interface_set_l2_bridge( |
| 263 | rx_sw_if_index=cls.pg2.sw_if_index, bd_id=cls.mcast_flood_bd) |
Hongjun Ni | ef486b1 | 2017-04-12 19:21:16 +0800 | [diff] [blame] | 264 | |
| 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() |
| 270 | |
| 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, |
| 274 | cls.n_ucast_tunnels) |
Ole Troan | a5b2eec | 2019-03-11 19:23:25 +0100 | [diff] [blame] | 275 | cls.vapi.sw_interface_set_l2_bridge( |
| 276 | rx_sw_if_index=cls.pg3.sw_if_index, bd_id=cls.ucast_flood_bd) |
Hongjun Ni | ef486b1 | 2017-04-12 19:21:16 +0800 | [diff] [blame] | 277 | except Exception: |
| 278 | super(TestGtpu, cls).tearDownClass() |
| 279 | raise |
| 280 | |
Paul Vinciguerra | 7f9b7f9 | 2019-03-12 19:23:27 -0700 | [diff] [blame^] | 281 | @classmethod |
| 282 | def tearDownClass(cls): |
| 283 | super(TestGtpu, cls).tearDownClass() |
| 284 | |
Hongjun Ni | ef486b1 | 2017-04-12 19:21:16 +0800 | [diff] [blame] | 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. |
| 288 | def tearDown(self): |
| 289 | super(TestGtpu, self).tearDown() |
| 290 | if not self.vpp_dead: |
| 291 | self.logger.info(self.vapi.cli("show bridge-domain 11 detail")) |
| 292 | self.logger.info(self.vapi.cli("show bridge-domain 12 detail")) |
| 293 | self.logger.info(self.vapi.cli("show bridge-domain 13 detail")) |
| 294 | self.logger.info(self.vapi.cli("show int")) |
| 295 | self.logger.info(self.vapi.cli("show gtpu tunnel")) |
| 296 | self.logger.info(self.vapi.cli("show trace")) |
| 297 | |
| 298 | |
| 299 | if __name__ == '__main__': |
| 300 | unittest.main(testRunner=VppTestRunner) |