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