Hongjun Ni | ef486b1 | 2017-04-12 19:21:16 +0800 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | |
| 3 | import socket |
| 4 | from util import ip4n_range |
| 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 | |
| 33 | def encap_mcast(self, pkt, src_ip, src_mac, vni): |
| 34 | """ |
| 35 | Encapsulate the original payload frame by adding GTPU header with its |
| 36 | UDP, IP and Ethernet fields |
| 37 | """ |
| 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) / |
Neale Ranns | 2bc9402 | 2018-02-25 12:27:18 -0800 | [diff] [blame] | 41 | GTP_U_Header(teid=vni, gtp_type=self.gtp_type, length=150) / |
Hongjun Ni | ef486b1 | 2017-04-12 19:21:16 +0800 | [diff] [blame] | 42 | pkt) |
| 43 | |
| 44 | def decapsulate(self, pkt): |
| 45 | """ |
| 46 | Decapsulate the original payload frame by removing GTPU header |
| 47 | """ |
| 48 | return pkt[GTP_U_Header].payload |
| 49 | |
| 50 | # Method for checking GTPU encapsulation. |
| 51 | # |
| 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 |
| 54 | # by VPP using ARP. |
| 55 | self.assertEqual(pkt[Ether].src, self.pg0.local_mac) |
| 56 | if not local_only: |
| 57 | if not mcast_pkt: |
| 58 | self.assertEqual(pkt[Ether].dst, self.pg0.remote_mac) |
| 59 | else: |
| 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) |
| 63 | if not local_only: |
| 64 | if not mcast_pkt: |
| 65 | self.assertEqual(pkt[IP].dst, self.pg0.remote_ip4) |
| 66 | else: |
| 67 | self.assertEqual(pkt[IP].dst, type(self).mcast_ip4) |
| 68 | # Verify UDP destination port is GTPU 2152, source UDP port could be |
| 69 | # arbitrary. |
| 70 | self.assertEqual(pkt[UDP].dport, type(self).dport) |
Neale Ranns | 2bc9402 | 2018-02-25 12:27:18 -0800 | [diff] [blame] | 71 | # Verify teid |
| 72 | self.assertEqual(pkt[GTP_U_Header].teid, vni) |
Hongjun Ni | ef486b1 | 2017-04-12 19:21:16 +0800 | [diff] [blame] | 73 | |
| 74 | def test_encap(self): |
| 75 | """ Encapsulation test |
| 76 | Send frames from pg1 |
| 77 | Verify receipt of encapsulated frames on pg0 |
| 78 | """ |
| 79 | self.pg1.add_stream([self.frame_reply]) |
| 80 | |
| 81 | self.pg0.enable_capture() |
| 82 | |
| 83 | self.pg_start() |
| 84 | |
| 85 | # Pick first received frame and check if it's corectly encapsulated. |
| 86 | out = self.pg0.get_capture(1) |
| 87 | pkt = out[0] |
| 88 | self.check_encapsulation(pkt, self.single_tunnel_bd) |
| 89 | |
| 90 | # payload = self.decapsulate(pkt) |
| 91 | # self.assert_eq_pkts(payload, self.frame_reply) |
| 92 | |
| 93 | def test_ucast_flood(self): |
| 94 | """ Unicast flood test |
| 95 | Send frames from pg3 |
| 96 | Verify receipt of encapsulated frames on pg0 |
| 97 | """ |
| 98 | self.pg3.add_stream([self.frame_reply]) |
| 99 | |
| 100 | self.pg0.enable_capture() |
| 101 | |
| 102 | self.pg_start() |
| 103 | |
| 104 | # Get packet from each tunnel and assert it's corectly encapsulated. |
| 105 | out = self.pg0.get_capture(self.n_ucast_tunnels) |
| 106 | for pkt in out: |
| 107 | self.check_encapsulation(pkt, self.ucast_flood_bd, True) |
| 108 | # payload = self.decapsulate(pkt) |
| 109 | # self.assert_eq_pkts(payload, self.frame_reply) |
| 110 | |
| 111 | def test_mcast_flood(self): |
| 112 | """ Multicast flood test |
| 113 | Send frames from pg2 |
| 114 | Verify receipt of encapsulated frames on pg0 |
| 115 | """ |
| 116 | self.pg2.add_stream([self.frame_reply]) |
| 117 | |
| 118 | self.pg0.enable_capture() |
| 119 | |
| 120 | self.pg_start() |
| 121 | |
| 122 | # Pick first received frame and check if it's corectly encapsulated. |
| 123 | out = self.pg0.get_capture(1) |
| 124 | pkt = out[0] |
| 125 | self.check_encapsulation(pkt, self.mcast_flood_bd, |
| 126 | local_only=False, mcast_pkt=True) |
| 127 | |
| 128 | # payload = self.decapsulate(pkt) |
| 129 | # self.assert_eq_pkts(payload, self.frame_reply) |
| 130 | |
| 131 | @classmethod |
| 132 | def create_gtpu_flood_test_bd(cls, teid, n_ucast_tunnels): |
| 133 | # Create 10 ucast gtpu tunnels under bd |
| 134 | ip_range_start = 10 |
| 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, |
| 138 | ip_range_end): |
| 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, |
| 143 | dst_addr=dest_ip4n, |
| 144 | teid=teid) |
| 145 | cls.vapi.sw_interface_set_l2_bridge(r.sw_if_index, bd_id=teid) |
| 146 | |
| 147 | @classmethod |
| 148 | def add_del_shared_mcast_dst_load(cls, is_add): |
| 149 | """ |
| 150 | add or del tunnels sharing the same mcast dst |
| 151 | to test gtpu ref_count mechanism |
| 152 | """ |
| 153 | n_shared_dst_tunnels = 20 |
| 154 | teid_start = 1000 |
| 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, |
| 160 | mcast_sw_if_index=1, |
| 161 | teid=teid, |
| 162 | is_add=is_add) |
| 163 | if r.sw_if_index == 0xffffffff: |
| 164 | raise "bad sw_if_index" |
| 165 | |
| 166 | @classmethod |
| 167 | def add_shared_mcast_dst_load(cls): |
| 168 | cls.add_del_shared_mcast_dst_load(is_add=1) |
| 169 | |
| 170 | @classmethod |
| 171 | def del_shared_mcast_dst_load(cls): |
| 172 | cls.add_del_shared_mcast_dst_load(is_add=0) |
| 173 | |
| 174 | @classmethod |
| 175 | def add_del_mcast_tunnels_load(cls, is_add): |
| 176 | """ |
| 177 | add or del tunnels to test gtpu stability |
| 178 | """ |
| 179 | n_distinct_dst_tunnels = 20 |
| 180 | ip_range_start = 10 |
| 181 | ip_range_end = ip_range_start + n_distinct_dst_tunnels |
| 182 | for dest_ip4n in ip4n_range(cls.mcast_ip4n, ip_range_start, |
| 183 | ip_range_end): |
| 184 | teid = bytearray(dest_ip4n)[3] |
| 185 | cls.vapi.gtpu_add_del_tunnel( |
| 186 | src_addr=cls.pg0.local_ip4n, |
| 187 | dst_addr=dest_ip4n, |
| 188 | mcast_sw_if_index=1, |
| 189 | teid=teid, |
| 190 | is_add=is_add) |
| 191 | |
| 192 | @classmethod |
| 193 | def add_mcast_tunnels_load(cls): |
| 194 | cls.add_del_mcast_tunnels_load(is_add=1) |
| 195 | |
| 196 | @classmethod |
| 197 | def del_mcast_tunnels_load(cls): |
| 198 | cls.add_del_mcast_tunnels_load(is_add=0) |
| 199 | |
| 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. |
| 205 | @classmethod |
| 206 | def setUpClass(cls): |
| 207 | super(TestGtpu, cls).setUpClass() |
| 208 | |
| 209 | try: |
| 210 | cls.dport = 2152 |
| 211 | cls.gtp_type = 0xff |
| 212 | |
| 213 | # Create 2 pg interfaces. |
| 214 | cls.create_pg_interfaces(range(4)) |
| 215 | for pg in cls.pg_interfaces: |
| 216 | pg.admin_up() |
| 217 | |
| 218 | # Configure IPv4 addresses on VPP pg0. |
| 219 | cls.pg0.config_ip4() |
| 220 | |
| 221 | # Resolve MAC address for VPP's IP address on pg0. |
| 222 | cls.pg0.resolve_arp() |
| 223 | |
| 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) |
| 230 | |
| 231 | # Create GTPU VTEP on VPP pg0, and put gtpu_tunnel0 and pg1 |
| 232 | # into BD. |
| 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) |
| 242 | |
| 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, |
| 247 | cls.n_ucast_tunnels) |
| 248 | r = cls.vapi.gtpu_add_del_tunnel( |
| 249 | src_addr=cls.pg0.local_ip4n, |
| 250 | dst_addr=cls.mcast_ip4n, |
| 251 | mcast_sw_if_index=1, |
| 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) |
| 257 | |
| 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() |
| 263 | |
| 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, |
| 267 | cls.n_ucast_tunnels) |
| 268 | cls.vapi.sw_interface_set_l2_bridge(cls.pg3.sw_if_index, |
| 269 | bd_id=cls.ucast_flood_bd) |
| 270 | except Exception: |
| 271 | super(TestGtpu, cls).tearDownClass() |
| 272 | raise |
| 273 | |
| 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. |
| 277 | def tearDown(self): |
| 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")) |
| 286 | |
| 287 | |
| 288 | if __name__ == '__main__': |
| 289 | unittest.main(testRunner=VppTestRunner) |