#!/usr/bin/env python """ACL IRB Test Case HLD: **config** - L2 MAC learning enabled in l2bd - 2 routed interfaces untagged, bvi (Bridge Virtual Interface) - 2 bridged interfaces in l2bd with bvi **test** - sending ip4 eth pkts between routed interfaces - 2 routed interfaces - 2 bridged interfaces - 64B, 512B, 1518B, 9200B (ether_size) - burst of pkts per interface - 257pkts per burst - routed pkts hitting different FIB entries - bridged pkts hitting different MAC entries **verify** - all packets received correctly """ import unittest from socket import inet_pton, AF_INET, AF_INET6 from random import choice, shuffle from pprint import pprint import scapy.compat from scapy.packet import Raw from scapy.layers.l2 import Ether from scapy.layers.inet import IP, UDP, ICMP, TCP from scapy.layers.inet6 import IPv6, ICMPv6Unknown, ICMPv6EchoRequest from scapy.layers.inet6 import ICMPv6EchoReply, IPv6ExtHdrRouting from scapy.layers.inet6 import IPv6ExtHdrFragment from framework import VppTestCase, VppTestRunner from vpp_l2 import L2_PORT_TYPE import time class TestACLpluginL2L3(VppTestCase): """TestACLpluginL2L3 Test Case""" @classmethod def setUpClass(cls): """ #. Create BD with MAC learning enabled and put interfaces to this BD. #. Configure IPv4 addresses on loopback interface and routed interface. #. Configure MAC address binding to IPv4 neighbors on loop0. #. Configure MAC address on pg2. #. Loopback BVI interface has remote hosts, one half of hosts are behind pg0 second behind pg1. """ super(TestACLpluginL2L3, cls).setUpClass() cls.pg_if_packet_sizes = [64, 512, 1518, 9018] # packet sizes cls.bd_id = 10 cls.remote_hosts_count = 250 # create 3 pg interfaces, 1 loopback interface cls.create_pg_interfaces(range(3)) cls.create_loopback_interfaces(1) cls.interfaces = list(cls.pg_interfaces) cls.interfaces.extend(cls.lo_interfaces) for i in cls.interfaces: i.admin_up() # Create BD with MAC learning enabled and put interfaces to this BD cls.vapi.sw_interface_set_l2_bridge( rx_sw_if_index=cls.loop0.sw_if_index, bd_id=cls.bd_id, port_type=L2_PORT_TYPE.BVI) cls.vapi.sw_interface_set_l2_bridge(rx_sw_if_index=cls.pg0.sw_if_index, bd_id=cls.bd_id) cls.vapi.sw_interface_set_l2_bridge(rx_sw_if_index=cls.pg1.sw_if_index, bd_id=cls.bd_id) # Configure IPv4 addresses on loopback interface and routed interface cls.loop0.config_ip4() cls.loop0.config_ip6() cls.pg2.config_ip4() cls.pg2.config_ip6() # Configure MAC address binding to IPv4 neighbors on loop0 cls.loop0.generate_remote_hosts(cls.remote_hosts_count) cls.loop0.configure_ipv4_neighbors() cls.loop0.configure_ipv6_neighbors() # configure MAC address on pg2 cls.pg2.resolve_arp() cls.pg2.resolve_ndp() cls.WITHOUT_EH = False cls.WITH_EH = True cls.STATELESS_ICMP = False cls.STATEFUL_ICMP = True # Loopback BVI interface has remote hosts, one half of hosts are behind # pg0 second behind pg1 half = cls.remote_hosts_count // 2 cls.pg0.remote_hosts = cls.loop0.remote_hosts[:half] cls.pg1.remote_hosts = cls.loop0.remote_hosts[half:] @classmethod def tearDownClass(cls): super(TestACLpluginL2L3, cls).tearDownClass() def tearDown(self): """Run standard test teardown and log ``show l2patch``, ``show l2fib verbose``,``show bridge-domain detail``, ``show ip arp``. """ super(TestACLpluginL2L3, self).tearDown() def show_commands_at_teardown(self): self.logger.info(self.vapi.cli("show l2patch")) self.logger.info(self.vapi.cli("show classify tables")) self.logger.info(self.vapi.cli("show l2fib verbose")) self.logger.info(self.vapi.cli("show bridge-domain %s detail" % self.bd_id)) self.logger.info(self.vapi.cli("show ip arp")) self.logger.info(self.vapi.cli("show ip6 neighbors")) cmd = "show acl-plugin sessions verbose 1" self.logger.info(self.vapi.cli(cmd)) self.logger.info(self.vapi.cli("show acl-plugin acl")) self.logger.info(self.vapi.cli("show acl-plugin interface")) self.logger.info(self.vapi.cli("show acl-plugin tables")) def create_stream(self, src_ip_if, dst_ip_if, reverse, packet_sizes, is_ip6, expect_blocked, expect_established, add_extension_header, icmp_stateful=False): pkts = [] rules = [] permit_rules = [] permit_and_reflect_rules = [] total_packet_count = 8 for i in range(0, total_packet_count): modulo = (i//2) % 2 icmp_type_delta = i % 2 icmp_code = i is_udp_packet = (modulo == 0) if is_udp_packet and icmp_stateful: continue is_reflectable_icmp = (icmp_stateful and icmp_type_delta == 0 and not is_udp_packet) is_reflected_icmp = is_reflectable_icmp and expect_established can_reflect_this_packet = is_udp_packet or is_reflectable_icmp is_permit = i % 2 remote_dst_index = i % len(dst_ip_if.remote_hosts) remote_dst_host = dst_ip_if.remote_hosts[remote_dst_index] if is_permit == 1: info = self.create_packet_info(src_ip_if, dst_ip_if) payload = self.info_to_payload(info) else: to_be_blocked = False if (expect_blocked and not expect_established): to_be_blocked = True if (not can_reflect_this_packet): to_be_blocked = True if to_be_blocked: payload = "to be blocked" else: info = self.create_packet_info(src_ip_if, dst_ip_if) payload = self.info_to_payload(info) if reverse: dst_mac = 'de:ad:00:00:00:00' src_mac = remote_dst_host._mac dst_ip6 = src_ip_if.remote_ip6 src_ip6 = remote_dst_host.ip6 dst_ip4 = src_ip_if.remote_ip4 src_ip4 = remote_dst_host.ip4 dst_l4 = 1234 + i src_l4 = 4321 + i else: dst_mac = src_ip_if.local_mac src_mac = src_ip_if.remote_mac src_ip6 = src_ip_if.remote_ip6 dst_ip6 = remote_dst_host.ip6 src_ip4 = src_ip_if.remote_ip4 dst_ip4 = remote_dst_host.ip4 src_l4 = 1234 + i dst_l4 = 4321 + i if is_reflected_icmp: icmp_type_delta = 1 # default ULP should be something we do not use in tests ulp_l4 = TCP(sport=src_l4, dport=dst_l4) # potentially a chain of protocols leading to ULP ulp = ulp_l4 if is_udp_packet: if is_ip6: ulp_l4 = UDP(sport=src_l4, dport=dst_l4) if add_extension_header: # prepend some extension headers ulp = (IPv6ExtHdrRouting() / IPv6ExtHdrRouting() / IPv6ExtHdrFragment(offset=0, m=1) / ulp_l4) # uncomment below to test invalid ones # ulp = IPv6ExtHdrRouting(len = 200) / ulp_l4 else: ulp = ulp_l4 p = (Ether(dst=dst_mac, src=src_mac) / IPv6(src=src_ip6, dst=dst_ip6) / ulp / Raw(payload)) else: ulp_l4 = UDP(sport=src_l4, dport=dst_l4) # IPv4 does not allow extension headers, # but we rather make it a first fragment flags = 1 if add_extension_header else 0 ulp = ulp_l4 p = (Ether(dst=dst_mac, src=src_mac) / IP(src=src_ip4, dst=dst_ip4, frag=0, flags=flags) / ulp / Raw(payload)) elif modulo == 1: if is_ip6: ulp_l4 = ICMPv6Unknown(type=128 + icmp_type_delta, code=icmp_code) ulp = ulp_l4 p = (Ether(dst=dst_mac, src=src_mac) / IPv6(src=src_ip6, dst=dst_ip6) / ulp / Raw(payload)) else: ulp_l4 = ICMP(type=8 - 8*icmp_type_delta, code=icmp_code) ulp = ulp_l4 p = (Ether(dst=dst_mac, src=src_mac) / IP(src=src_ip4, dst=dst_ip4) / ulp / Raw(payload)) if i % 2 == 1: info.data = p.copy() size = packet_sizes[(i // 2) % len(packet_sizes)] self.extend_packet(p, size) pkts.append(p) rule_family = AF_INET6 if p.haslayer(IPv6) else AF_INET rule_prefix_len = 128 if p.haslayer(IPv6) else 32 rule_l3_layer = IPv6 if p.haslayer(IPv6) else IP if p.haslayer(UDP): rule_l4_sport = p[UDP].sport rule_l4_dport = p[UDP].dport else: if p.haslayer(ICMP): rule_l4_sport = p[ICMP].type rule_l4_dport = p[ICMP].code else: rule_l4_sport = p[ICMPv6Unknown].type rule_l4_dport = p[ICMPv6Unknown].code if p.haslayer(IPv6): rule_l4_proto = ulp_l4.overload_fields[IPv6]['nh'] else: rule_l4_proto = p[IP].proto new_rule = { 'is_permit': is_permit, 'is_ipv6': p.haslayer(IPv6), 'src_ip_addr': inet_pton(rule_family, p[rule_l3_layer].src), 'src_ip_prefix_len': rule_prefix_len, 'dst_ip_addr': inet_pton(rule_family, p[rule_l3_layer].dst), 'dst_ip_prefix_len': rule_prefix_len, 'srcport_or_icmptype_first': rule_l4_sport, 'srcport_or_icmptype_last': rule_l4_sport, 'dstport_or_icmpcode_first': rule_l4_dport, 'dstport_or_icmpcode_last': rule_l4_dport, 'proto': rule_l4_proto, } rules.append(new_rule) new_rule_permit = new_rule.copy() new_rule_permit['is_permit'] = 1 permit_rules.append(new_rule_permit) new_rule_permit_and_reflect = new_rule.copy() if can_reflect_this_packet: new_rule_permit_and_reflect['is_permit'] = 2 else: new_rule_permit_and_reflect['is_permit'] = is_permit permit_and_reflect_rules.append(new_rule_permit_and_reflect) self.logger.info("create_stream pkt#%d: %s" % (i, payload)) return {'stream': pkts, 'rules': rules, 'permit_rules': permit_rules, 'permit_and_reflect_rules': permit_and_reflect_rules} def verify_capture(self, dst_ip_if, src_ip_if, capture, reverse): last_info = dict() for i in self.interfaces: last_info[i.sw_if_index] = None dst_ip_sw_if_index = dst_ip_if.sw_if_index for packet in capture: l3 = IP if packet.haslayer(IP) else IPv6 ip = packet[l3] if packet.haslayer(UDP): l4 = UDP else: if packet.haslayer(ICMP): l4 = ICMP else: l4 = ICMPv6Unknown # Scapy IPv6 stuff is too smart for its own good. # So we do this and coerce the ICMP into unknown type if packet.haslayer(UDP): data = scapy.compat.raw(packet[UDP][Raw]) else: if l3 == IP: data = scapy.compat.raw(ICMP( scapy.compat.raw(packet[l3].payload))[R
# Copyright (c) 2016 Cisco and/or its affiliates.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at:
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

*** Settings ***
| Resource | resources/libraries/robot/default.robot
| Resource | resources/libraries/robot/honeycomb/honeycomb.robot
| Resource | resources/libraries/robot/honeycomb/netconf.robot
| Resource | resources/libraries/robot/honeycomb/bridge_domain.robot
| Library | resources.libraries.python.honeycomb.HcAPIKwInterfaces.InterfaceKeywords
| ...     | WITH NAME | InterfaceAPI
| Variables | resources/test_data/honeycomb/netconf/triggers.py
| ...
| Documentation | *Netconf test suite. Contains test cases that need to bypass\
| ... | REST API.*
| ...
| Force Tags | HC_FUNC | HC_REST_ONLY
| ...
| Suite Setup | Set Up Honeycomb Functional Test Suite | ${node}
| ...
| Suite Teardown | Tear Down Honeycomb Functional Test Suite | ${node}
| ...

*** Variables ***
| ${interface}= | ${node['interfaces']['port1']['name']}
| &{bd_settings}= | flood=${True} | forward=${True} | learn=${True}
| ... | unknown-unicast-flood=${True} | arp-termination=${True}

*** Test Cases ***
| TC01: Honeycomb can create and delete interfaces
| | [Documentation] | Repeatedly create and delete an interface through Netconf\
| | ... | and check the reply for any errors.
| | ...
| | Given Netconf session should be established | ${node}
| | And Honeycomb creates first L2 bridge domain
| | ... | ${node} | bd_netconf | ${bd_settings}
| | :FOR | ${index} | IN RANGE | 20
| | | When Error trigger is sent | ${trigger_105}
| | | Then Replies should not contain RPC errors

| TC02: Transaction revert test case 1
| | [Documentation] | Configure two conflicting VxLAN tunnels, then verify\
| | ... | that neither tunnel exists.
| | ...
| | Given Netconf session should be established | ${node}
| | ${if_data}= | And InterfaceAPI.Get all interfaces oper data | ${node}
| | When Error trigger is sent | ${trigger_revert1}
| | ${if_data_new}= | And InterfaceAPI.Get all interfaces oper data | ${node}
| | Then Should be equal | ${if_data} | ${if_data_new}

| TC03: Transaction revert test case 2
| | [Documentation] | Configure two conflicting TAP interfaces, then verify\
| | ... | that neither interface exists.
| | ...
| | Given Netconf session should be established | ${node}
| | ${if_data}= | And InterfaceAPI.Get all interfaces oper data | ${node}
| | When Error trigger is sent | ${trigger_revert1}
| | ${if_data_new}= | And InterfaceAPI.Get all interfaces oper data | ${node}
| | Then Should be equal | ${if_data} | ${if_data_new}

| TC04: Vlan subinterface creation
| | [Documentation] | Configure a Vlan sub-interface under a physical interface.
| | ...
| | Given Netconf session should be established | ${node}
| | When Error Trigger Is Sent
| | ... | ${trigger_vlan} | interface=${interface}
| | Then Replies should not contain RPC errors
st_ip46_bridged_to_routed_and_back(False, False, self.WITHOUT_EH) # A block of tests with extension headers def test_1001_ip6_irb_1(self): """ ACL IPv6+EH routed -> bridged, L2 ACL deny""" self.run_test_ip46_routed_to_bridged(True, True, False, self.WITH_EH) def test_1002_ip6_irb_1(self): """ ACL IPv6+EH routed -> bridged, L3 ACL deny""" self.run_test_ip46_routed_to_bridged(False, True, False, self.WITH_EH) def test_1005_ip6_irb_1(self): """ ACL IPv6+EH bridged -> routed, L2 ACL deny """ self.run_test_ip46_bridged_to_routed(True, True, False, self.WITH_EH) def test_1006_ip6_irb_1(self): """ ACL IPv6+EH bridged -> routed, L3 ACL deny """ self.run_test_ip46_bridged_to_routed(False, True, False, self.WITH_EH) def test_1101_ip6_irb_1(self): """ ACL IPv6+EH routed -> bridged, L2 ACL permit+reflect""" self.run_test_ip46_routed_to_bridged_and_back(True, True, self.WITH_EH) def test_1102_ip6_irb_1(self): """ ACL IPv6+EH bridged -> routed, L2 ACL permit+reflect""" self.run_test_ip46_bridged_to_routed_and_back(True, True, self.WITH_EH) def test_1111_ip6_irb_1(self): """ ACL IPv6+EH routed -> bridged, L3 ACL permit+reflect""" self.run_test_ip46_routed_to_bridged_and_back(False, True, self.WITH_EH) def test_1112_ip6_irb_1(self): """ ACL IPv6+EH bridged -> routed, L3 ACL permit+reflect""" self.run_test_ip46_bridged_to_routed_and_back(False, True, self.WITH_EH) # IPv4 with "MF" bit set def test_1201_ip6_irb_1(self): """ ACL IPv4+MF routed -> bridged, L2 ACL deny""" self.run_test_ip46_routed_to_bridged(True, False, False, self.WITH_EH) def test_1202_ip6_irb_1(self): """ ACL IPv4+MF routed -> bridged, L3 ACL deny""" self.run_test_ip46_routed_to_bridged(False, False, False, self.WITH_EH) def test_1205_ip6_irb_1(self): """ ACL IPv4+MF bridged -> routed, L2 ACL deny """ self.run_test_ip46_bridged_to_routed(True, False, False, self.WITH_EH) def test_1206_ip6_irb_1(self): """ ACL IPv4+MF bridged -> routed, L3 ACL deny """ self.run_test_ip46_bridged_to_routed(False, False, False, self.WITH_EH) def test_1301_ip6_irb_1(self): """ ACL IPv4+MF routed -> bridged, L2 ACL permit+reflect""" self.run_test_ip46_routed_to_bridged_and_back(True, False, self.WITH_EH) def test_1302_ip6_irb_1(self): """ ACL IPv4+MF bridged -> routed, L2 ACL permit+reflect""" self.run_test_ip46_bridged_to_routed_and_back(True, False, self.WITH_EH) def test_1311_ip6_irb_1(self): """ ACL IPv4+MF routed -> bridged, L3 ACL permit+reflect""" self.run_test_ip46_routed_to_bridged_and_back(False, False, self.WITH_EH) def test_1312_ip6_irb_1(self): """ ACL IPv4+MF bridged -> routed, L3 ACL permit+reflect""" self.run_test_ip46_bridged_to_routed_and_back(False, False, self.WITH_EH) # Stateful ACL tests with stateful ICMP def test_1401_ip6_irb_1(self): """ IPv6 routed -> bridged, L2 ACL permit+reflect, ICMP reflect""" self.run_test_ip46_routed_to_bridged_and_back(True, True, self.WITHOUT_EH, self.STATEFUL_ICMP) def test_1402_ip6_irb_1(self): """ IPv6 bridged -> routed, L2 ACL permit+reflect, ICMP reflect""" self.run_test_ip46_bridged_to_routed_and_back(True, True, self.WITHOUT_EH, self.STATEFUL_ICMP) def test_1403_ip4_irb_1(self): """ IPv4 routed -> bridged, L2 ACL permit+reflect, ICMP reflect""" self.run_test_ip46_routed_to_bridged_and_back(True, False, self.WITHOUT_EH, self.STATEFUL_ICMP) def test_1404_ip4_irb_1(self): """ IPv4 bridged -> routed, L2 ACL permit+reflect, ICMP reflect""" self.run_test_ip46_bridged_to_routed_and_back(True, False, self.WITHOUT_EH, self.STATEFUL_ICMP) def test_1411_ip6_irb_1(self): """ IPv6 routed -> bridged, L3 ACL permit+reflect, ICMP reflect""" self.run_test_ip46_routed_to_bridged_and_back(False, True, self.WITHOUT_EH, self.STATEFUL_ICMP) def test_1412_ip6_irb_1(self): """ IPv6 bridged -> routed, L3 ACL permit+reflect, ICMP reflect""" self.run_test_ip46_bridged_to_routed_and_back(False, True, self.WITHOUT_EH, self.STATEFUL_ICMP) def test_1413_ip4_irb_1(self): """ IPv4 routed -> bridged, L3 ACL permit+reflect, ICMP reflect""" self.run_test_ip46_routed_to_bridged_and_back(False, False, self.WITHOUT_EH, self.STATEFUL_ICMP) def test_1414_ip4_irb_1(self): """ IPv4 bridged -> routed, L3 ACL permit+reflect, ICMP reflect""" self.run_test_ip46_bridged_to_routed_and_back(False, False, self.WITHOUT_EH, self.STATEFUL_ICMP) if __name__ == '__main__': unittest.main(testRunner=VppTestRunner)