Mohsin Kazmi | 61b94c6 | 2018-08-20 18:32:39 +0200 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | |
| 3 | import socket |
Ole Troan | 7f99183 | 2018-12-06 17:35:12 +0100 | [diff] [blame] | 4 | from util import ip4_range, reassemble4_ether |
Mohsin Kazmi | 61b94c6 | 2018-08-20 18:32:39 +0200 | [diff] [blame] | 5 | import unittest |
| 6 | from framework import VppTestCase, VppTestRunner |
| 7 | from template_bd import BridgeDomain |
Neale Ranns | 79a05f5 | 2018-09-11 07:39:43 -0700 | [diff] [blame] | 8 | from vpp_ip import VppIpAddress |
Mohsin Kazmi | 61b94c6 | 2018-08-20 18:32:39 +0200 | [diff] [blame] | 9 | |
| 10 | from scapy.layers.l2 import Ether, Raw |
| 11 | from scapy.layers.inet import IP, UDP |
| 12 | from scapy.layers.vxlan import VXLAN |
| 13 | from scapy.utils import atol |
Neale Ranns | 097fa66 | 2018-05-01 05:17:55 -0700 | [diff] [blame^] | 14 | from vpp_ip_route import VppIpRoute, VppRoutePath |
| 15 | from vpp_ip import INVALID_INDEX |
Mohsin Kazmi | 61b94c6 | 2018-08-20 18:32:39 +0200 | [diff] [blame] | 16 | |
Mohsin Kazmi | 61b94c6 | 2018-08-20 18:32:39 +0200 | [diff] [blame] | 17 | |
| 18 | class TestVxlanGbp(VppTestCase): |
| 19 | """ VXLAN GBP Test Case """ |
| 20 | |
| 21 | @property |
| 22 | def frame_request(self): |
| 23 | """ Ethernet frame modeling a generic request """ |
| 24 | return (Ether(src='00:00:00:00:00:01', dst='00:00:00:00:00:02') / |
| 25 | IP(src='1.2.3.4', dst='4.3.2.1') / |
| 26 | UDP(sport=10000, dport=20000) / |
| 27 | Raw('\xa5' * 100)) |
| 28 | |
| 29 | @property |
| 30 | def frame_reply(self): |
| 31 | """ Ethernet frame modeling a generic reply """ |
| 32 | return (Ether(src='00:00:00:00:00:02', dst='00:00:00:00:00:01') / |
| 33 | IP(src='4.3.2.1', dst='1.2.3.4') / |
| 34 | UDP(sport=20000, dport=10000) / |
| 35 | Raw('\xa5' * 100)) |
| 36 | |
| 37 | def encapsulate(self, pkt, vni): |
| 38 | """ |
| 39 | Encapsulate the original payload frame by adding VXLAN GBP header with |
| 40 | its UDP, IP and Ethernet fields |
| 41 | """ |
| 42 | return (Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac) / |
| 43 | IP(src=self.pg0.remote_ip4, dst=self.pg0.local_ip4) / |
| 44 | UDP(sport=self.dport, dport=self.dport, chksum=0) / |
| 45 | VXLAN(vni=vni, flags=self.flags, gpflags=self.gpflags, |
| 46 | gpid=self.sclass) / pkt) |
| 47 | |
| 48 | def ip_range(self, start, end): |
| 49 | """ range of remote ip's """ |
| 50 | return ip4_range(self.pg0.remote_ip4, start, end) |
| 51 | |
| 52 | def decapsulate(self, pkt): |
| 53 | """ |
| 54 | Decapsulate the original payload frame by removing VXLAN header |
| 55 | """ |
| 56 | # check if is set G and I flag |
| 57 | self.assertEqual(pkt[VXLAN].flags, int('0x88', 16)) |
| 58 | return pkt[VXLAN].payload |
| 59 | |
| 60 | # Method for checking VXLAN GBP encapsulation. |
| 61 | # |
| 62 | def check_encapsulation(self, pkt, vni, local_only=False, mcast_pkt=False): |
| 63 | # TODO: add error messages |
| 64 | # Verify source MAC is VPP_MAC and destination MAC is MY_MAC resolved |
| 65 | # by VPP using ARP. |
| 66 | self.assertEqual(pkt[Ether].src, self.pg0.local_mac) |
| 67 | if not local_only: |
| 68 | if not mcast_pkt: |
| 69 | self.assertEqual(pkt[Ether].dst, self.pg0.remote_mac) |
| 70 | else: |
| 71 | self.assertEqual(pkt[Ether].dst, type(self).mcast_mac) |
| 72 | # Verify VXLAN GBP tunnel source IP is VPP_IP and destination IP is |
| 73 | # MY_IP. |
| 74 | self.assertEqual(pkt[IP].src, self.pg0.local_ip4) |
| 75 | if not local_only: |
| 76 | if not mcast_pkt: |
| 77 | self.assertEqual(pkt[IP].dst, self.pg0.remote_ip4) |
| 78 | else: |
| 79 | self.assertEqual(pkt[IP].dst, type(self).mcast_ip4) |
| 80 | # Verify UDP destination port is VXLAN GBP 48879, source UDP port could |
| 81 | # be arbitrary. |
| 82 | self.assertEqual(pkt[UDP].dport, type(self).dport) |
| 83 | # TODO: checksum check |
| 84 | # Verify VNI |
| 85 | # pkt.show() |
| 86 | self.assertEqual(pkt[VXLAN].vni, vni) |
| 87 | # Verify Source Class |
| 88 | self.assertEqual(pkt[VXLAN].gpid, 0) |
| 89 | |
| 90 | @classmethod |
| 91 | def create_vxlan_gbp_flood_test_bd(cls, vni, n_ucast_tunnels): |
| 92 | # Create 2 ucast vxlan tunnels under bd |
| 93 | ip_range_start = 10 |
| 94 | ip_range_end = ip_range_start + n_ucast_tunnels |
Neale Ranns | 097fa66 | 2018-05-01 05:17:55 -0700 | [diff] [blame^] | 95 | next_hop_address = cls.pg0.remote_ip4 |
Neale Ranns | 79a05f5 | 2018-09-11 07:39:43 -0700 | [diff] [blame] | 96 | for dest_ip4 in ip4_range(cls.pg0.remote_ip4, |
| 97 | ip_range_start, |
| 98 | ip_range_end): |
Mohsin Kazmi | 61b94c6 | 2018-08-20 18:32:39 +0200 | [diff] [blame] | 99 | # add host route so dest_ip4n will not be resolved |
Neale Ranns | 097fa66 | 2018-05-01 05:17:55 -0700 | [diff] [blame^] | 100 | rip = VppIpRoute(cls, dest_ip4, 32, |
| 101 | [VppRoutePath(next_hop_address, |
| 102 | INVALID_INDEX)], |
| 103 | register=False) |
| 104 | rip.add_vpp_config() |
Neale Ranns | 79a05f5 | 2018-09-11 07:39:43 -0700 | [diff] [blame] | 105 | r = cls.vapi.vxlan_gbp_tunnel_add_del( |
Neale Ranns | 097fa66 | 2018-05-01 05:17:55 -0700 | [diff] [blame^] | 106 | cls.pg0.local_ip4, |
| 107 | dest_ip4, |
Mohsin Kazmi | 61b94c6 | 2018-08-20 18:32:39 +0200 | [diff] [blame] | 108 | vni=vni) |
Ole Troan | a5b2eec | 2019-03-11 19:23:25 +0100 | [diff] [blame] | 109 | cls.vapi.sw_interface_set_l2_bridge(rx_sw_if_index=r.sw_if_index, |
| 110 | bd_id=vni) |
Mohsin Kazmi | 61b94c6 | 2018-08-20 18:32:39 +0200 | [diff] [blame] | 111 | |
| 112 | # Class method to start the VXLAN GBP test case. |
| 113 | # Overrides setUpClass method in VppTestCase class. |
| 114 | # Python try..except statement is used to ensure that the tear down of |
| 115 | # the class will be executed even if exception is raised. |
| 116 | # @param cls The class pointer. |
| 117 | @classmethod |
| 118 | def setUpClass(cls): |
| 119 | super(TestVxlanGbp, cls).setUpClass() |
| 120 | |
| 121 | try: |
| 122 | cls.dport = 48879 |
| 123 | cls.flags = 0x88 |
| 124 | cls.gpflags = 0x0 |
| 125 | cls.sclass = 0 |
| 126 | |
| 127 | # Create 2 pg interfaces. |
| 128 | cls.create_pg_interfaces(range(4)) |
| 129 | for pg in cls.pg_interfaces: |
| 130 | pg.admin_up() |
| 131 | |
| 132 | # Configure IPv4 addresses on VPP pg0. |
| 133 | cls.pg0.config_ip4() |
| 134 | |
| 135 | # Resolve MAC address for VPP's IP address on pg0. |
| 136 | cls.pg0.resolve_arp() |
| 137 | |
| 138 | # Create VXLAN GBP VTEP on VPP pg0, and put vxlan_gbp_tunnel0 and |
| 139 | # pg1 into BD. |
| 140 | cls.single_tunnel_bd = 1 |
Neale Ranns | 79a05f5 | 2018-09-11 07:39:43 -0700 | [diff] [blame] | 141 | r = cls.vapi.vxlan_gbp_tunnel_add_del( |
| 142 | VppIpAddress(cls.pg0.local_ip4).encode(), |
| 143 | VppIpAddress(cls.pg0.remote_ip4).encode(), |
Mohsin Kazmi | 61b94c6 | 2018-08-20 18:32:39 +0200 | [diff] [blame] | 144 | vni=cls.single_tunnel_bd) |
Ole Troan | a5b2eec | 2019-03-11 19:23:25 +0100 | [diff] [blame] | 145 | cls.vapi.sw_interface_set_l2_bridge(rx_sw_if_index=r.sw_if_index, |
Mohsin Kazmi | 61b94c6 | 2018-08-20 18:32:39 +0200 | [diff] [blame] | 146 | bd_id=cls.single_tunnel_bd) |
Ole Troan | a5b2eec | 2019-03-11 19:23:25 +0100 | [diff] [blame] | 147 | cls.vapi.sw_interface_set_l2_bridge( |
| 148 | rx_sw_if_index=cls.pg1.sw_if_index, bd_id=cls.single_tunnel_bd) |
Mohsin Kazmi | 61b94c6 | 2018-08-20 18:32:39 +0200 | [diff] [blame] | 149 | |
| 150 | # Setup vni 2 to test multicast flooding |
| 151 | cls.n_ucast_tunnels = 2 |
| 152 | # Setup vni 3 to test unicast flooding |
| 153 | cls.ucast_flood_bd = 3 |
| 154 | cls.create_vxlan_gbp_flood_test_bd(cls.ucast_flood_bd, |
| 155 | cls.n_ucast_tunnels) |
Ole Troan | a5b2eec | 2019-03-11 19:23:25 +0100 | [diff] [blame] | 156 | cls.vapi.sw_interface_set_l2_bridge( |
| 157 | rx_sw_if_index=cls.pg3.sw_if_index, bd_id=cls.ucast_flood_bd) |
Mohsin Kazmi | 61b94c6 | 2018-08-20 18:32:39 +0200 | [diff] [blame] | 158 | except Exception: |
| 159 | super(TestVxlanGbp, cls).tearDownClass() |
| 160 | raise |
| 161 | |
Paul Vinciguerra | 7f9b7f9 | 2019-03-12 19:23:27 -0700 | [diff] [blame] | 162 | @classmethod |
| 163 | def tearDownClass(cls): |
| 164 | super(TestVxlanGbp, cls).tearDownClass() |
| 165 | |
Mohsin Kazmi | 61b94c6 | 2018-08-20 18:32:39 +0200 | [diff] [blame] | 166 | def assert_eq_pkts(self, pkt1, pkt2): |
| 167 | """ Verify the Ether, IP, UDP, payload are equal in both |
| 168 | packets |
| 169 | """ |
| 170 | self.assertEqual(pkt1[Ether].src, pkt2[Ether].src) |
| 171 | self.assertEqual(pkt1[Ether].dst, pkt2[Ether].dst) |
| 172 | self.assertEqual(pkt1[IP].src, pkt2[IP].src) |
| 173 | self.assertEqual(pkt1[IP].dst, pkt2[IP].dst) |
| 174 | self.assertEqual(pkt1[UDP].sport, pkt2[UDP].sport) |
| 175 | self.assertEqual(pkt1[UDP].dport, pkt2[UDP].dport) |
| 176 | self.assertEqual(pkt1[Raw], pkt2[Raw]) |
| 177 | |
| 178 | def test_decap(self): |
| 179 | """ Decapsulation test |
| 180 | Send encapsulated frames from pg0 |
| 181 | Verify receipt of decapsulated frames on pg1 |
| 182 | """ |
| 183 | encapsulated_pkt = self.encapsulate(self.frame_request, |
| 184 | self.single_tunnel_bd) |
| 185 | |
| 186 | self.pg0.add_stream([encapsulated_pkt, ]) |
| 187 | |
| 188 | self.pg1.enable_capture() |
| 189 | |
| 190 | self.pg_start() |
| 191 | |
| 192 | # Pick first received frame and check if it's the non-encapsulated |
| 193 | # frame |
| 194 | out = self.pg1.get_capture(1) |
| 195 | pkt = out[0] |
| 196 | self.assert_eq_pkts(pkt, self.frame_request) |
| 197 | |
| 198 | def test_encap(self): |
| 199 | """ Encapsulation test |
| 200 | Send frames from pg1 |
| 201 | Verify receipt of encapsulated frames on pg0 |
| 202 | """ |
| 203 | self.pg1.add_stream([self.frame_reply]) |
| 204 | |
| 205 | self.pg0.enable_capture() |
| 206 | |
| 207 | self.pg_start() |
| 208 | |
Paul Vinciguerra | 8feeaff | 2019-03-27 11:25:48 -0700 | [diff] [blame] | 209 | # Pick first received frame and check if it's correctly encapsulated. |
Mohsin Kazmi | 61b94c6 | 2018-08-20 18:32:39 +0200 | [diff] [blame] | 210 | out = self.pg0.get_capture(1) |
| 211 | pkt = out[0] |
| 212 | self.check_encapsulation(pkt, self.single_tunnel_bd) |
| 213 | |
| 214 | payload = self.decapsulate(pkt) |
| 215 | self.assert_eq_pkts(payload, self.frame_reply) |
| 216 | |
| 217 | def test_ucast_flood(self): |
| 218 | """ Unicast flood test |
| 219 | Send frames from pg3 |
| 220 | Verify receipt of encapsulated frames on pg0 |
| 221 | """ |
| 222 | self.pg3.add_stream([self.frame_reply]) |
| 223 | |
| 224 | self.pg0.enable_capture() |
| 225 | |
| 226 | self.pg_start() |
| 227 | |
Paul Vinciguerra | 8feeaff | 2019-03-27 11:25:48 -0700 | [diff] [blame] | 228 | # Get packet from each tunnel and assert it's correctly encapsulated. |
Mohsin Kazmi | 61b94c6 | 2018-08-20 18:32:39 +0200 | [diff] [blame] | 229 | out = self.pg0.get_capture(self.n_ucast_tunnels) |
| 230 | for pkt in out: |
| 231 | self.check_encapsulation(pkt, self.ucast_flood_bd, True) |
| 232 | payload = self.decapsulate(pkt) |
| 233 | self.assert_eq_pkts(payload, self.frame_reply) |
| 234 | |
| 235 | def test_encap_big_packet(self): |
| 236 | """ Encapsulation test send big frame from pg1 |
| 237 | Verify receipt of encapsulated frames on pg0 |
| 238 | """ |
| 239 | |
| 240 | self.vapi.sw_interface_set_mtu(self.pg0.sw_if_index, [1500, 0, 0, 0]) |
| 241 | |
| 242 | frame = (Ether(src='00:00:00:00:00:02', dst='00:00:00:00:00:01') / |
| 243 | IP(src='4.3.2.1', dst='1.2.3.4') / |
| 244 | UDP(sport=20000, dport=10000) / |
| 245 | Raw('\xa5' * 1450)) |
| 246 | |
| 247 | self.pg1.add_stream([frame]) |
| 248 | |
| 249 | self.pg0.enable_capture() |
| 250 | |
| 251 | self.pg_start() |
| 252 | |
| 253 | # Pick first received frame and check if it's correctly encapsulated. |
| 254 | out = self.pg0.get_capture(2) |
Ole Troan | 7f99183 | 2018-12-06 17:35:12 +0100 | [diff] [blame] | 255 | pkt = reassemble4_ether(out) |
Mohsin Kazmi | 61b94c6 | 2018-08-20 18:32:39 +0200 | [diff] [blame] | 256 | self.check_encapsulation(pkt, self.single_tunnel_bd) |
| 257 | |
| 258 | payload = self.decapsulate(pkt) |
| 259 | self.assert_eq_pkts(payload, frame) |
| 260 | |
| 261 | # Method to define VPP actions before tear down of the test case. |
| 262 | # Overrides tearDown method in VppTestCase class. |
| 263 | # @param self The object pointer. |
| 264 | def tearDown(self): |
| 265 | super(TestVxlanGbp, self).tearDown() |
Paul Vinciguerra | 90cf21b | 2019-03-13 09:23:05 -0700 | [diff] [blame] | 266 | |
| 267 | def show_commands_at_teardown(self): |
| 268 | self.logger.info(self.vapi.cli("show bridge-domain 1 detail")) |
| 269 | self.logger.info(self.vapi.cli("show bridge-domain 3 detail")) |
| 270 | self.logger.info(self.vapi.cli("show vxlan-gbp tunnel")) |
| 271 | self.logger.info(self.vapi.cli("show error")) |
Mohsin Kazmi | 61b94c6 | 2018-08-20 18:32:39 +0200 | [diff] [blame] | 272 | |
| 273 | |
| 274 | if __name__ == '__main__': |
| 275 | unittest.main(testRunner=VppTestRunner) |