Yi Tseng | 0b80972 | 2017-11-03 10:23:26 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2017-present Open Networking Foundation |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
Carmelo Cascone | 356ab8b | 2019-09-25 01:02:53 -0700 | [diff] [blame] | 17 | package org.onosproject.pipelines.fabric.impl.behaviour.pipeliner; |
Yi Tseng | 0b80972 | 2017-11-03 10:23:26 -0700 | [diff] [blame] | 18 | |
Daniele Moro | 5a2de71 | 2019-09-24 14:34:07 -0700 | [diff] [blame] | 19 | import com.google.common.collect.Lists; |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame] | 20 | import org.junit.Before; |
Yi Tseng | 0b80972 | 2017-11-03 10:23:26 -0700 | [diff] [blame] | 21 | import org.junit.Test; |
| 22 | import org.onlab.packet.Ethernet; |
| 23 | import org.onlab.packet.MacAddress; |
| 24 | import org.onlab.packet.VlanId; |
| 25 | import org.onlab.util.ImmutableByteSequence; |
| 26 | import org.onosproject.net.PortNumber; |
| 27 | import org.onosproject.net.flow.DefaultFlowRule; |
| 28 | import org.onosproject.net.flow.DefaultTrafficSelector; |
| 29 | import org.onosproject.net.flow.DefaultTrafficTreatment; |
| 30 | import org.onosproject.net.flow.FlowRule; |
| 31 | import org.onosproject.net.flow.TableId; |
| 32 | import org.onosproject.net.flow.TrafficSelector; |
| 33 | import org.onosproject.net.flow.TrafficTreatment; |
| 34 | import org.onosproject.net.flow.criteria.Criteria; |
Daniele Moro | 7c3a002 | 2019-07-12 13:38:34 -0700 | [diff] [blame] | 35 | import org.onosproject.net.flow.criteria.PiCriterion; |
Yi Tseng | 0b80972 | 2017-11-03 10:23:26 -0700 | [diff] [blame] | 36 | import org.onosproject.net.flowobjective.DefaultFilteringObjective; |
| 37 | import org.onosproject.net.flowobjective.FilteringObjective; |
| 38 | import org.onosproject.net.flowobjective.ObjectiveError; |
Yi Tseng | 0b80972 | 2017-11-03 10:23:26 -0700 | [diff] [blame] | 39 | import org.onosproject.net.pi.runtime.PiAction; |
| 40 | import org.onosproject.net.pi.runtime.PiActionParam; |
Carmelo Cascone | 2102bfb | 2020-12-04 16:54:24 -0800 | [diff] [blame] | 41 | import org.onosproject.pipelines.fabric.FabricConstants; |
Yi Tseng | 0b80972 | 2017-11-03 10:23:26 -0700 | [diff] [blame] | 42 | |
Daniele Moro | 5a2de71 | 2019-09-24 14:34:07 -0700 | [diff] [blame] | 43 | import java.util.Collection; |
| 44 | import java.util.List; |
| 45 | |
Yi Tseng | 0b80972 | 2017-11-03 10:23:26 -0700 | [diff] [blame] | 46 | import static org.junit.Assert.assertEquals; |
Wailok Shum | fb7e787 | 2021-06-18 17:30:08 +0800 | [diff] [blame] | 47 | import static org.onosproject.pipelines.fabric.impl.behaviour.Constants.DEFAULT_PW_TRANSPORT_VLAN; |
| 48 | import static org.onosproject.pipelines.fabric.impl.behaviour.Constants.DEFAULT_VLAN; |
pierventre | 4d29d3c | 2021-08-27 17:20:00 +0200 | [diff] [blame^] | 49 | import static org.onosproject.pipelines.fabric.impl.behaviour.Constants.EDGE_PORT; |
Wailok Shum | fb7e787 | 2021-06-18 17:30:08 +0800 | [diff] [blame] | 50 | import static org.onosproject.pipelines.fabric.impl.behaviour.Constants.ETH_TYPE_EXACT_MASK; |
| 51 | import static org.onosproject.pipelines.fabric.impl.behaviour.Constants.FWD_IPV4_ROUTING; |
| 52 | import static org.onosproject.pipelines.fabric.impl.behaviour.Constants.FWD_IPV6_ROUTING; |
| 53 | import static org.onosproject.pipelines.fabric.impl.behaviour.Constants.FWD_MPLS; |
pierventre | 4d29d3c | 2021-08-27 17:20:00 +0200 | [diff] [blame^] | 54 | import static org.onosproject.pipelines.fabric.impl.behaviour.Constants.INFRA_PORT; |
Wailok Shum | fb7e787 | 2021-06-18 17:30:08 +0800 | [diff] [blame] | 55 | import static org.onosproject.pipelines.fabric.impl.behaviour.Constants.ONE; |
pierventre | 4d29d3c | 2021-08-27 17:20:00 +0200 | [diff] [blame^] | 56 | import static org.onosproject.pipelines.fabric.impl.behaviour.Constants.PAIR_PORT; |
Wailok Shum | fb7e787 | 2021-06-18 17:30:08 +0800 | [diff] [blame] | 57 | import static org.onosproject.pipelines.fabric.impl.behaviour.Constants.PORT_TYPE_EDGE; |
| 58 | import static org.onosproject.pipelines.fabric.impl.behaviour.Constants.PORT_TYPE_INFRA; |
| 59 | import static org.onosproject.pipelines.fabric.impl.behaviour.Constants.ZERO; |
Yi Tseng | 0b80972 | 2017-11-03 10:23:26 -0700 | [diff] [blame] | 60 | |
| 61 | /** |
| 62 | * Test cases for fabric.p4 pipeline filtering control block. |
| 63 | */ |
Carmelo Cascone | 2388cc1 | 2021-05-26 19:30:30 +0200 | [diff] [blame] | 64 | public class FilteringObjectiveTranslatorTest extends BaseObjectiveTranslatorTest { |
Yi Tseng | 0b80972 | 2017-11-03 10:23:26 -0700 | [diff] [blame] | 65 | |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame] | 66 | private FilteringObjectiveTranslator translator; |
| 67 | |
| 68 | @Before |
| 69 | public void setup() { |
| 70 | super.doSetup(); |
| 71 | translator = new FilteringObjectiveTranslator(DEVICE_ID, capabilitiesHashed); |
| 72 | } |
| 73 | |
Yi Tseng | 0b80972 | 2017-11-03 10:23:26 -0700 | [diff] [blame] | 74 | /** |
| 75 | * Creates one rule for ingress_port_vlan table and 3 rules for |
Carmelo Cascone | 356ab8b | 2019-09-25 01:02:53 -0700 | [diff] [blame] | 76 | * fwd_classifier table (IPv4, IPv6 and MPLS unicast) when the condition is |
| 77 | * VLAN + MAC. |
Yi Tseng | 0b80972 | 2017-11-03 10:23:26 -0700 | [diff] [blame] | 78 | */ |
| 79 | @Test |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame] | 80 | public void testRouterMacAndVlanFilter() throws FabricPipelinerException { |
pierventre | 4d29d3c | 2021-08-27 17:20:00 +0200 | [diff] [blame^] | 81 | FilteringObjective filteringObjective = buildFilteringObjective(ROUTER_MAC, EDGE_PORT); |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame] | 82 | ObjectiveTranslation actualTranslation = translator.translate(filteringObjective); |
Daniele Moro | 5a2de71 | 2019-09-24 14:34:07 -0700 | [diff] [blame] | 83 | Collection<FlowRule> expectedFlowRules = Lists.newArrayList(); |
Yi Tseng | 0b80972 | 2017-11-03 10:23:26 -0700 | [diff] [blame] | 84 | // in port vlan flow rule |
Daniele Moro | 5a2de71 | 2019-09-24 14:34:07 -0700 | [diff] [blame] | 85 | expectedFlowRules.add(buildExpectedVlanInPortRule( |
Carmelo Cascone | 356ab8b | 2019-09-25 01:02:53 -0700 | [diff] [blame] | 86 | PORT_1, |
| 87 | VlanId.NONE, |
| 88 | VlanId.NONE, |
| 89 | VLAN_100, |
Wailok Shum | fb7e787 | 2021-06-18 17:30:08 +0800 | [diff] [blame] | 90 | PORT_TYPE_EDGE, |
Daniele Moro | 5a2de71 | 2019-09-24 14:34:07 -0700 | [diff] [blame] | 91 | FabricConstants.FABRIC_INGRESS_FILTERING_INGRESS_PORT_VLAN)); |
Yi Tseng | 0b80972 | 2017-11-03 10:23:26 -0700 | [diff] [blame] | 92 | |
| 93 | // forwarding classifier ipv4 |
Daniele Moro | 5a2de71 | 2019-09-24 14:34:07 -0700 | [diff] [blame] | 94 | expectedFlowRules.addAll(buildExpectedFwdClassifierRule( |
Carmelo Cascone | 356ab8b | 2019-09-25 01:02:53 -0700 | [diff] [blame] | 95 | PORT_1, |
| 96 | ROUTER_MAC, |
| 97 | null, |
| 98 | Ethernet.TYPE_IPV4, |
Wailok Shum | fb7e787 | 2021-06-18 17:30:08 +0800 | [diff] [blame] | 99 | FWD_IPV4_ROUTING)); |
Yi Tseng | 0b80972 | 2017-11-03 10:23:26 -0700 | [diff] [blame] | 100 | |
| 101 | // forwarding classifier ipv6 |
Daniele Moro | 5a2de71 | 2019-09-24 14:34:07 -0700 | [diff] [blame] | 102 | expectedFlowRules.addAll(buildExpectedFwdClassifierRule( |
Carmelo Cascone | 356ab8b | 2019-09-25 01:02:53 -0700 | [diff] [blame] | 103 | PORT_1, |
| 104 | ROUTER_MAC, |
| 105 | null, |
| 106 | Ethernet.TYPE_IPV6, |
Wailok Shum | fb7e787 | 2021-06-18 17:30:08 +0800 | [diff] [blame] | 107 | FWD_IPV6_ROUTING)); |
Yi Tseng | 0b80972 | 2017-11-03 10:23:26 -0700 | [diff] [blame] | 108 | |
| 109 | // forwarding classifier mpls |
Daniele Moro | 5a2de71 | 2019-09-24 14:34:07 -0700 | [diff] [blame] | 110 | expectedFlowRules.addAll(buildExpectedFwdClassifierRule( |
Carmelo Cascone | 356ab8b | 2019-09-25 01:02:53 -0700 | [diff] [blame] | 111 | PORT_1, |
| 112 | ROUTER_MAC, |
| 113 | null, |
| 114 | Ethernet.MPLS_UNICAST, |
Wailok Shum | fb7e787 | 2021-06-18 17:30:08 +0800 | [diff] [blame] | 115 | FWD_MPLS)); |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame] | 116 | |
Daniele Moro | 5a2de71 | 2019-09-24 14:34:07 -0700 | [diff] [blame] | 117 | // ObjectiveTranslation.Builder expectedTranslationBuilder = ObjectiveTranslation.builder() |
| 118 | // .addFlowRule(inportFlowRuleExpected); |
| 119 | // for (FlowRule flowRule : classifierV4FlowRuleExpected) { |
| 120 | // expectedTranslationBuilder.addFlowRule(flowRule); |
| 121 | // } |
| 122 | // for (FlowRule flowRule : classifierV6FlowRuleExpected) { |
| 123 | // expectedTranslationBuilder.addFlowRule(flowRule); |
| 124 | // } |
| 125 | // for (FlowRule flowRule : classifierMplsFlowRuleExpected) { |
| 126 | // expectedTranslationBuilder.addFlowRule(flowRule); |
| 127 | // } |
| 128 | ObjectiveTranslation expectedTranslation = buildExpectedTranslation(expectedFlowRules); |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame] | 129 | |
| 130 | assertEquals(expectedTranslation, actualTranslation); |
Yi Tseng | 0b80972 | 2017-11-03 10:23:26 -0700 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | /** |
| 134 | * Creates one rule for ingress_port_vlan table and one rule for |
| 135 | * fwd_classifier table (IPv4 multicast) when the condition is ipv4 |
| 136 | * multicast mac address. |
| 137 | */ |
| 138 | @Test |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame] | 139 | public void testIpv4MulticastFwdClass() throws FabricPipelinerException { |
Yi Tseng | 0b80972 | 2017-11-03 10:23:26 -0700 | [diff] [blame] | 140 | TrafficTreatment treatment = DefaultTrafficTreatment.builder() |
| 141 | .pushVlan() |
| 142 | .setVlanId(VLAN_100) |
pierventre | 4d29d3c | 2021-08-27 17:20:00 +0200 | [diff] [blame^] | 143 | .writeMetadata(EDGE_PORT, 0xffffffffffffffffL) |
Yi Tseng | 0b80972 | 2017-11-03 10:23:26 -0700 | [diff] [blame] | 144 | .build(); |
| 145 | FilteringObjective filteringObjective = DefaultFilteringObjective.builder() |
| 146 | .permit() |
| 147 | .withPriority(PRIORITY) |
| 148 | .withKey(Criteria.matchInPort(PORT_1)) |
Charles Chan | 384aea2 | 2018-08-23 22:08:02 -0700 | [diff] [blame] | 149 | .addCondition(Criteria.matchEthDstMasked(MacAddress.IPV4_MULTICAST, MacAddress.IPV4_MULTICAST_MASK)) |
Yi Tseng | 0b80972 | 2017-11-03 10:23:26 -0700 | [diff] [blame] | 150 | .addCondition(Criteria.matchVlanId(VlanId.NONE)) |
| 151 | .withMeta(treatment) |
| 152 | .fromApp(APP_ID) |
| 153 | .makePermanent() |
| 154 | .add(); |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame] | 155 | ObjectiveTranslation actualTranslation = translator.translate(filteringObjective); |
Daniele Moro | 5a2de71 | 2019-09-24 14:34:07 -0700 | [diff] [blame] | 156 | List<FlowRule> expectedFlowRules = Lists.newArrayList(); |
Yi Tseng | 0b80972 | 2017-11-03 10:23:26 -0700 | [diff] [blame] | 157 | // in port vlan flow rule |
Daniele Moro | 5a2de71 | 2019-09-24 14:34:07 -0700 | [diff] [blame] | 158 | expectedFlowRules.add(buildExpectedVlanInPortRule( |
Carmelo Cascone | 356ab8b | 2019-09-25 01:02:53 -0700 | [diff] [blame] | 159 | PORT_1, |
| 160 | VlanId.NONE, |
| 161 | VlanId.NONE, |
| 162 | VLAN_100, |
Wailok Shum | fb7e787 | 2021-06-18 17:30:08 +0800 | [diff] [blame] | 163 | PORT_TYPE_EDGE, |
Daniele Moro | 5a2de71 | 2019-09-24 14:34:07 -0700 | [diff] [blame] | 164 | FabricConstants.FABRIC_INGRESS_FILTERING_INGRESS_PORT_VLAN)); |
Yi Tseng | 0b80972 | 2017-11-03 10:23:26 -0700 | [diff] [blame] | 165 | |
| 166 | // forwarding classifier |
Daniele Moro | 5a2de71 | 2019-09-24 14:34:07 -0700 | [diff] [blame] | 167 | expectedFlowRules.addAll(buildExpectedFwdClassifierRule( |
Carmelo Cascone | 356ab8b | 2019-09-25 01:02:53 -0700 | [diff] [blame] | 168 | PORT_1, |
| 169 | MacAddress.IPV4_MULTICAST, |
| 170 | MacAddress.IPV4_MULTICAST_MASK, |
| 171 | Ethernet.TYPE_IPV4, |
Wailok Shum | fb7e787 | 2021-06-18 17:30:08 +0800 | [diff] [blame] | 172 | FWD_IPV4_ROUTING)); |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame] | 173 | |
Daniele Moro | 5a2de71 | 2019-09-24 14:34:07 -0700 | [diff] [blame] | 174 | ObjectiveTranslation expectedTranslation = buildExpectedTranslation(expectedFlowRules); |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame] | 175 | |
| 176 | assertEquals(expectedTranslation, actualTranslation); |
Yi Tseng | 0b80972 | 2017-11-03 10:23:26 -0700 | [diff] [blame] | 177 | } |
| 178 | |
| 179 | /** |
| 180 | * Creates one rule for ingress_port_vlan table and one rule for |
| 181 | * fwd_classifier table (IPv6 multicast) when the condition is ipv6 |
| 182 | * multicast mac address. |
| 183 | */ |
| 184 | @Test |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame] | 185 | public void testIpv6MulticastFwdClass() throws FabricPipelinerException { |
Yi Tseng | 0b80972 | 2017-11-03 10:23:26 -0700 | [diff] [blame] | 186 | TrafficTreatment treatment = DefaultTrafficTreatment.builder() |
| 187 | .pushVlan() |
| 188 | .setVlanId(VLAN_100) |
pierventre | 4d29d3c | 2021-08-27 17:20:00 +0200 | [diff] [blame^] | 189 | .writeMetadata(EDGE_PORT, 0xffffffffffffffffL) |
Yi Tseng | 0b80972 | 2017-11-03 10:23:26 -0700 | [diff] [blame] | 190 | .build(); |
| 191 | FilteringObjective filteringObjective = DefaultFilteringObjective.builder() |
| 192 | .permit() |
| 193 | .withPriority(PRIORITY) |
| 194 | .withKey(Criteria.matchInPort(PORT_1)) |
Charles Chan | 384aea2 | 2018-08-23 22:08:02 -0700 | [diff] [blame] | 195 | .addCondition(Criteria.matchEthDstMasked(MacAddress.IPV6_MULTICAST, MacAddress.IPV6_MULTICAST_MASK)) |
Yi Tseng | 0b80972 | 2017-11-03 10:23:26 -0700 | [diff] [blame] | 196 | .addCondition(Criteria.matchVlanId(VlanId.NONE)) |
| 197 | .withMeta(treatment) |
| 198 | .fromApp(APP_ID) |
| 199 | .makePermanent() |
| 200 | .add(); |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame] | 201 | ObjectiveTranslation actualTranslation = translator.translate(filteringObjective); |
Daniele Moro | 5a2de71 | 2019-09-24 14:34:07 -0700 | [diff] [blame] | 202 | Collection<FlowRule> flowRules = Lists.newArrayList(); |
Yi Tseng | 0b80972 | 2017-11-03 10:23:26 -0700 | [diff] [blame] | 203 | // in port vlan flow rule |
Daniele Moro | 5a2de71 | 2019-09-24 14:34:07 -0700 | [diff] [blame] | 204 | flowRules.add(buildExpectedVlanInPortRule( |
Carmelo Cascone | 356ab8b | 2019-09-25 01:02:53 -0700 | [diff] [blame] | 205 | PORT_1, |
| 206 | VlanId.NONE, |
| 207 | VlanId.NONE, |
| 208 | VLAN_100, |
Wailok Shum | fb7e787 | 2021-06-18 17:30:08 +0800 | [diff] [blame] | 209 | PORT_TYPE_EDGE, |
Daniele Moro | 5a2de71 | 2019-09-24 14:34:07 -0700 | [diff] [blame] | 210 | FabricConstants.FABRIC_INGRESS_FILTERING_INGRESS_PORT_VLAN)); |
Yi Tseng | 0b80972 | 2017-11-03 10:23:26 -0700 | [diff] [blame] | 211 | |
Daniele Moro | 5a2de71 | 2019-09-24 14:34:07 -0700 | [diff] [blame] | 212 | flowRules.addAll(buildExpectedFwdClassifierRule( |
Carmelo Cascone | 356ab8b | 2019-09-25 01:02:53 -0700 | [diff] [blame] | 213 | PORT_1, |
| 214 | MacAddress.IPV6_MULTICAST, |
| 215 | MacAddress.IPV6_MULTICAST_MASK, |
| 216 | Ethernet.TYPE_IPV6, |
Wailok Shum | fb7e787 | 2021-06-18 17:30:08 +0800 | [diff] [blame] | 217 | FWD_IPV6_ROUTING)); |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame] | 218 | |
Daniele Moro | 5a2de71 | 2019-09-24 14:34:07 -0700 | [diff] [blame] | 219 | ObjectiveTranslation expectedTranslation = buildExpectedTranslation(flowRules); |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame] | 220 | |
| 221 | assertEquals(expectedTranslation, actualTranslation); |
Yi Tseng | 0b80972 | 2017-11-03 10:23:26 -0700 | [diff] [blame] | 222 | } |
| 223 | |
| 224 | /** |
Carmelo Cascone | 356ab8b | 2019-09-25 01:02:53 -0700 | [diff] [blame] | 225 | * Creates only one rule for ingress_port_vlan table if there is no |
| 226 | * condition of destination mac address. The packet will be handled by |
| 227 | * bridging table by default. |
Yi Tseng | 0b80972 | 2017-11-03 10:23:26 -0700 | [diff] [blame] | 228 | */ |
| 229 | @Test |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame] | 230 | public void testFwdBridging() throws Exception { |
pierventre | 4d29d3c | 2021-08-27 17:20:00 +0200 | [diff] [blame^] | 231 | FilteringObjective filteringObjective = buildFilteringObjective(null, EDGE_PORT); |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame] | 232 | ObjectiveTranslation actualTranslation = translator.translate(filteringObjective); |
Yi Tseng | 0b80972 | 2017-11-03 10:23:26 -0700 | [diff] [blame] | 233 | |
| 234 | // in port vlan flow rule |
Carmelo Cascone | 356ab8b | 2019-09-25 01:02:53 -0700 | [diff] [blame] | 235 | FlowRule flowRuleExpected = buildExpectedVlanInPortRule( |
| 236 | PORT_1, |
| 237 | VlanId.NONE, |
| 238 | VlanId.NONE, |
| 239 | VLAN_100, |
Wailok Shum | fb7e787 | 2021-06-18 17:30:08 +0800 | [diff] [blame] | 240 | PORT_TYPE_EDGE, |
Carmelo Cascone | 356ab8b | 2019-09-25 01:02:53 -0700 | [diff] [blame] | 241 | FabricConstants.FABRIC_INGRESS_FILTERING_INGRESS_PORT_VLAN); |
Yi Tseng | 0b80972 | 2017-11-03 10:23:26 -0700 | [diff] [blame] | 242 | |
| 243 | // No rules in forwarding classifier, will do default action: set fwd type to bridging |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame] | 244 | |
| 245 | ObjectiveTranslation expectedTranslation = ObjectiveTranslation.builder() |
| 246 | .addFlowRule(flowRuleExpected) |
| 247 | .build(); |
| 248 | |
| 249 | assertEquals(expectedTranslation, actualTranslation); |
Yi Tseng | 0b80972 | 2017-11-03 10:23:26 -0700 | [diff] [blame] | 250 | } |
| 251 | |
| 252 | /** |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame] | 253 | * Test DENY objective. |
Yi Tseng | 0b80972 | 2017-11-03 10:23:26 -0700 | [diff] [blame] | 254 | */ |
| 255 | @Test |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame] | 256 | public void testDenyObjective() throws FabricPipelinerException { |
Yi Tseng | 0b80972 | 2017-11-03 10:23:26 -0700 | [diff] [blame] | 257 | FilteringObjective filteringObjective = DefaultFilteringObjective.builder() |
| 258 | .deny() |
| 259 | .withKey(Criteria.matchInPort(PORT_1)) |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame] | 260 | .addCondition(Criteria.matchVlanId(VlanId.NONE)) |
Yi Tseng | 0b80972 | 2017-11-03 10:23:26 -0700 | [diff] [blame] | 261 | .fromApp(APP_ID) |
| 262 | .makePermanent() |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame] | 263 | .withPriority(PRIORITY) |
Yi Tseng | 0b80972 | 2017-11-03 10:23:26 -0700 | [diff] [blame] | 264 | .add(); |
| 265 | |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame] | 266 | ObjectiveTranslation actualTranslation = translator.translate(filteringObjective); |
Yi Tseng | 0b80972 | 2017-11-03 10:23:26 -0700 | [diff] [blame] | 267 | |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame] | 268 | TrafficSelector.Builder selector = DefaultTrafficSelector.builder() |
| 269 | .matchInPort(PORT_1) |
Daniele Moro | 7c3a002 | 2019-07-12 13:38:34 -0700 | [diff] [blame] | 270 | .matchPi(buildPiCriterionVlan(null, null)); |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame] | 271 | PiAction piAction = PiAction.builder() |
Carmelo Cascone | 356ab8b | 2019-09-25 01:02:53 -0700 | [diff] [blame] | 272 | .withId(FabricConstants.FABRIC_INGRESS_FILTERING_DENY) |
| 273 | .build(); |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame] | 274 | FlowRule expectedFlowRule = DefaultFlowRule.builder() |
| 275 | .withPriority(PRIORITY) |
| 276 | .withSelector(selector.build()) |
| 277 | .withTreatment(DefaultTrafficTreatment.builder() |
| 278 | .piTableAction(piAction).build()) |
| 279 | .fromApp(APP_ID) |
| 280 | .forDevice(DEVICE_ID) |
| 281 | .makePermanent() |
| 282 | .forTable(FabricConstants.FABRIC_INGRESS_FILTERING_INGRESS_PORT_VLAN) |
| 283 | .build(); |
Yi Tseng | 0b80972 | 2017-11-03 10:23:26 -0700 | [diff] [blame] | 284 | |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame] | 285 | ObjectiveTranslation expectedTranslation = ObjectiveTranslation.builder() |
| 286 | .addFlowRule(expectedFlowRule) |
| 287 | .build(); |
Yi Tseng | 0b80972 | 2017-11-03 10:23:26 -0700 | [diff] [blame] | 288 | |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame] | 289 | assertEquals(expectedTranslation, actualTranslation); |
| 290 | |
Yi Tseng | 0b80972 | 2017-11-03 10:23:26 -0700 | [diff] [blame] | 291 | } |
| 292 | |
| 293 | /** |
Carmelo Cascone | 356ab8b | 2019-09-25 01:02:53 -0700 | [diff] [blame] | 294 | * Test double VLAN pop filtering objective Creates one rule for |
| 295 | * ingress_port_vlan table and 3 rules for fwd_classifier table (IPv4, IPv6 |
| 296 | * and MPLS unicast) when the condition is MAC + VLAN + INNER_VLAN. |
Daniele Moro | f51d0c1 | 2019-07-30 10:43:10 -0700 | [diff] [blame] | 297 | */ |
| 298 | @Test |
| 299 | public void testPopVlan() throws FabricPipelinerException { |
| 300 | FilteringObjective filteringObjective = DefaultFilteringObjective.builder() |
| 301 | .withKey(Criteria.matchInPort(PORT_1)) |
| 302 | .addCondition(Criteria.matchEthDst(ROUTER_MAC)) |
| 303 | .addCondition(Criteria.matchVlanId(VLAN_100)) |
| 304 | .addCondition(Criteria.matchInnerVlanId(VLAN_200)) |
| 305 | .withPriority(PRIORITY) |
| 306 | .fromApp(APP_ID) |
| 307 | .withMeta(DefaultTrafficTreatment.builder() |
pierventre | 4d29d3c | 2021-08-27 17:20:00 +0200 | [diff] [blame^] | 308 | .popVlan() |
| 309 | .writeMetadata(EDGE_PORT, 0xffffffffffffffffL) |
| 310 | .build()) |
Daniele Moro | f51d0c1 | 2019-07-30 10:43:10 -0700 | [diff] [blame] | 311 | .permit() |
| 312 | .add(); |
| 313 | ObjectiveTranslation actualTranslation = translator.translate(filteringObjective); |
Daniele Moro | 5a2de71 | 2019-09-24 14:34:07 -0700 | [diff] [blame] | 314 | Collection<FlowRule> expectedFlowRules = Lists.newArrayList(); |
Daniele Moro | f51d0c1 | 2019-07-30 10:43:10 -0700 | [diff] [blame] | 315 | // Ingress port vlan rule |
Daniele Moro | 5a2de71 | 2019-09-24 14:34:07 -0700 | [diff] [blame] | 316 | expectedFlowRules.add(buildExpectedVlanInPortRule( |
Wailok Shum | fb7e787 | 2021-06-18 17:30:08 +0800 | [diff] [blame] | 317 | PORT_1, VLAN_100, VLAN_200, VlanId.NONE, PORT_TYPE_EDGE, |
Daniele Moro | 5a2de71 | 2019-09-24 14:34:07 -0700 | [diff] [blame] | 318 | FabricConstants.FABRIC_INGRESS_FILTERING_INGRESS_PORT_VLAN)); |
Daniele Moro | f51d0c1 | 2019-07-30 10:43:10 -0700 | [diff] [blame] | 319 | // Forwarding classifier rules (ipv6, ipv4, mpls) |
Daniele Moro | 5a2de71 | 2019-09-24 14:34:07 -0700 | [diff] [blame] | 320 | expectedFlowRules.addAll(buildExpectedFwdClassifierRule( |
Carmelo Cascone | 356ab8b | 2019-09-25 01:02:53 -0700 | [diff] [blame] | 321 | PORT_1, ROUTER_MAC, null, Ethernet.TYPE_IPV4, |
Wailok Shum | fb7e787 | 2021-06-18 17:30:08 +0800 | [diff] [blame] | 322 | FWD_IPV4_ROUTING)); |
Daniele Moro | 5a2de71 | 2019-09-24 14:34:07 -0700 | [diff] [blame] | 323 | expectedFlowRules.addAll(buildExpectedFwdClassifierRule( |
Carmelo Cascone | 356ab8b | 2019-09-25 01:02:53 -0700 | [diff] [blame] | 324 | PORT_1, ROUTER_MAC, null, Ethernet.TYPE_IPV6, |
Wailok Shum | fb7e787 | 2021-06-18 17:30:08 +0800 | [diff] [blame] | 325 | FWD_IPV6_ROUTING)); |
Daniele Moro | 5a2de71 | 2019-09-24 14:34:07 -0700 | [diff] [blame] | 326 | expectedFlowRules.addAll(buildExpectedFwdClassifierRule( |
Carmelo Cascone | 356ab8b | 2019-09-25 01:02:53 -0700 | [diff] [blame] | 327 | PORT_1, ROUTER_MAC, null, Ethernet.MPLS_UNICAST, |
Wailok Shum | fb7e787 | 2021-06-18 17:30:08 +0800 | [diff] [blame] | 328 | FWD_MPLS)); |
Daniele Moro | 5a2de71 | 2019-09-24 14:34:07 -0700 | [diff] [blame] | 329 | ObjectiveTranslation expectedTranslation = buildExpectedTranslation(expectedFlowRules); |
Daniele Moro | f51d0c1 | 2019-07-30 10:43:10 -0700 | [diff] [blame] | 330 | |
| 331 | assertEquals(expectedTranslation, actualTranslation); |
| 332 | } |
| 333 | |
| 334 | /** |
Yi Tseng | 0b80972 | 2017-11-03 10:23:26 -0700 | [diff] [blame] | 335 | * Incorrect filtering key or filtering conditions test. |
| 336 | */ |
| 337 | @Test |
| 338 | public void badParamTest() { |
| 339 | // Filtering objective should contains filtering key |
| 340 | FilteringObjective filteringObjective = DefaultFilteringObjective.builder() |
| 341 | .permit() |
| 342 | .addCondition(Criteria.matchVlanId(VLAN_100)) |
| 343 | .fromApp(APP_ID) |
| 344 | .makePermanent() |
| 345 | .add(); |
| 346 | |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame] | 347 | ObjectiveTranslation result1 = translator.translate(filteringObjective); |
| 348 | assertError(ObjectiveError.BADPARAMS, result1); |
Yi Tseng | 0b80972 | 2017-11-03 10:23:26 -0700 | [diff] [blame] | 349 | |
| 350 | // Filtering objective should use in_port as key |
| 351 | filteringObjective = DefaultFilteringObjective.builder() |
| 352 | .permit() |
| 353 | .withKey(Criteria.matchEthDst(ROUTER_MAC)) |
| 354 | .addCondition(Criteria.matchVlanId(VLAN_100)) |
| 355 | .withMeta(DefaultTrafficTreatment.emptyTreatment()) |
| 356 | .fromApp(APP_ID) |
| 357 | .makePermanent() |
| 358 | .add(); |
| 359 | |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame] | 360 | ObjectiveTranslation result2 = translator.translate(filteringObjective); |
| 361 | assertError(ObjectiveError.BADPARAMS, result2); |
Yi Tseng | 0b80972 | 2017-11-03 10:23:26 -0700 | [diff] [blame] | 362 | } |
| 363 | |
pierventre | 167d448 | 2021-01-08 17:35:47 +0100 | [diff] [blame] | 364 | /** |
| 365 | * Test port update scenarios for filtering objective. Creates only one rule for |
| 366 | * ingress_port_vlan table. |
| 367 | */ |
| 368 | @Test |
Wailok Shum | fb7e787 | 2021-06-18 17:30:08 +0800 | [diff] [blame] | 369 | public void testIsPortUpdate() throws FabricPipelinerException { |
pierventre | 167d448 | 2021-01-08 17:35:47 +0100 | [diff] [blame] | 370 | // Tagged port scenario |
| 371 | FilteringObjective filteringObjective = DefaultFilteringObjective.builder() |
| 372 | .withKey(Criteria.matchInPort(PORT_1)) |
| 373 | .addCondition(Criteria.matchEthDst(ROUTER_MAC)) |
| 374 | .addCondition(Criteria.matchVlanId(VLAN_100)) |
| 375 | .withPriority(PRIORITY) |
| 376 | .fromApp(APP_ID) |
| 377 | .withMeta(DefaultTrafficTreatment.builder() |
pierventre | 4d29d3c | 2021-08-27 17:20:00 +0200 | [diff] [blame^] | 378 | .writeMetadata(10, 0xffffffffffffffffL) |
pierventre | 167d448 | 2021-01-08 17:35:47 +0100 | [diff] [blame] | 379 | .build()) |
| 380 | .permit() |
| 381 | .add(); |
| 382 | ObjectiveTranslation actualTranslation = translator.translate(filteringObjective); |
| 383 | Collection<FlowRule> expectedFlowRules = Lists.newArrayList(); |
| 384 | // Ingress port vlan rule |
| 385 | expectedFlowRules.add(buildExpectedVlanInPortRule( |
Wailok Shum | fb7e787 | 2021-06-18 17:30:08 +0800 | [diff] [blame] | 386 | PORT_1, VLAN_100, null, VlanId.NONE, PORT_TYPE_EDGE, |
pierventre | 167d448 | 2021-01-08 17:35:47 +0100 | [diff] [blame] | 387 | FabricConstants.FABRIC_INGRESS_FILTERING_INGRESS_PORT_VLAN)); |
| 388 | ObjectiveTranslation expectedTranslation = buildExpectedTranslation(expectedFlowRules); |
| 389 | assertEquals(expectedTranslation, actualTranslation); |
| 390 | |
| 391 | // Untagged port scenario |
| 392 | filteringObjective = DefaultFilteringObjective.builder() |
| 393 | .withKey(Criteria.matchInPort(PORT_1)) |
| 394 | .addCondition(Criteria.matchEthDst(ROUTER_MAC)) |
| 395 | .addCondition(Criteria.matchVlanId(VlanId.NONE)) |
| 396 | .withPriority(PRIORITY) |
| 397 | .fromApp(APP_ID) |
| 398 | .withMeta(DefaultTrafficTreatment.builder() |
| 399 | .pushVlan() |
| 400 | .setVlanId(VLAN_200) |
pierventre | 4d29d3c | 2021-08-27 17:20:00 +0200 | [diff] [blame^] | 401 | .writeMetadata(10, 0xffffffffffffffffL) |
pierventre | 167d448 | 2021-01-08 17:35:47 +0100 | [diff] [blame] | 402 | .build()) |
| 403 | .permit() |
| 404 | .add(); |
| 405 | actualTranslation = translator.translate(filteringObjective); |
| 406 | expectedFlowRules = Lists.newArrayList(); |
| 407 | // Ingress port vlan rule |
| 408 | expectedFlowRules.add(buildExpectedVlanInPortRule( |
Wailok Shum | fb7e787 | 2021-06-18 17:30:08 +0800 | [diff] [blame] | 409 | PORT_1, VlanId.NONE, null, VLAN_200, PORT_TYPE_EDGE, |
pierventre | 167d448 | 2021-01-08 17:35:47 +0100 | [diff] [blame] | 410 | FabricConstants.FABRIC_INGRESS_FILTERING_INGRESS_PORT_VLAN)); |
| 411 | expectedTranslation = buildExpectedTranslation(expectedFlowRules); |
| 412 | assertEquals(expectedTranslation, actualTranslation); |
| 413 | } |
| 414 | |
Wailok Shum | 4f51bde | 2021-06-11 22:48:41 +0800 | [diff] [blame] | 415 | /** |
| 416 | * Test no more ports scenario for filtering objective. |
| 417 | */ |
| 418 | @Test |
| 419 | public void testNoMorePorts() throws FabricPipelinerException { |
| 420 | // Tagged port scenario |
| 421 | FilteringObjective filteringObjective = DefaultFilteringObjective.builder() |
| 422 | .withKey(Criteria.matchInPort(PORT_1)) |
| 423 | .addCondition(Criteria.matchEthDst(ROUTER_MAC)) |
| 424 | .addCondition(Criteria.matchVlanId(VLAN_100)) |
| 425 | .withPriority(PRIORITY) |
| 426 | .fromApp(APP_ID) |
pierventre | 4d29d3c | 2021-08-27 17:20:00 +0200 | [diff] [blame^] | 427 | .withMeta(DefaultTrafficTreatment.builder() |
| 428 | .writeMetadata(EDGE_PORT, 0xffffffffffffffffL) |
| 429 | .wipeDeferred() |
| 430 | .build()) |
Wailok Shum | 4f51bde | 2021-06-11 22:48:41 +0800 | [diff] [blame] | 431 | .permit() |
| 432 | .add(); |
| 433 | ObjectiveTranslation actualTranslation = translator.translate(filteringObjective); |
| 434 | Collection<FlowRule> expectedFlowRules = Lists.newArrayList(); |
| 435 | // Ingress port vlan rule |
| 436 | expectedFlowRules.add(buildExpectedVlanInPortRule( |
Wailok Shum | fb7e787 | 2021-06-18 17:30:08 +0800 | [diff] [blame] | 437 | PORT_1, VLAN_100, null, VlanId.NONE, PORT_TYPE_EDGE, |
Wailok Shum | 4f51bde | 2021-06-11 22:48:41 +0800 | [diff] [blame] | 438 | FabricConstants.FABRIC_INGRESS_FILTERING_INGRESS_PORT_VLAN)); |
| 439 | // forwarding classifier ipv4 |
| 440 | expectedFlowRules.addAll(buildExpectedFwdClassifierRule( |
| 441 | PORT_1, |
| 442 | ROUTER_MAC, |
| 443 | null, |
| 444 | Ethernet.TYPE_IPV4, |
Wailok Shum | fb7e787 | 2021-06-18 17:30:08 +0800 | [diff] [blame] | 445 | FWD_IPV4_ROUTING)); |
Wailok Shum | 4f51bde | 2021-06-11 22:48:41 +0800 | [diff] [blame] | 446 | // forwarding classifier ipv6 |
| 447 | expectedFlowRules.addAll(buildExpectedFwdClassifierRule( |
| 448 | PORT_1, |
| 449 | ROUTER_MAC, |
| 450 | null, |
| 451 | Ethernet.TYPE_IPV6, |
Wailok Shum | fb7e787 | 2021-06-18 17:30:08 +0800 | [diff] [blame] | 452 | FWD_IPV6_ROUTING)); |
Wailok Shum | 4f51bde | 2021-06-11 22:48:41 +0800 | [diff] [blame] | 453 | // forwarding classifier mpls |
| 454 | expectedFlowRules.addAll(buildExpectedFwdClassifierRule( |
| 455 | PORT_1, |
| 456 | ROUTER_MAC, |
| 457 | null, |
| 458 | Ethernet.MPLS_UNICAST, |
Wailok Shum | fb7e787 | 2021-06-18 17:30:08 +0800 | [diff] [blame] | 459 | FWD_MPLS)); |
Wailok Shum | 4f51bde | 2021-06-11 22:48:41 +0800 | [diff] [blame] | 460 | |
| 461 | ObjectiveTranslation expectedTranslation = buildExpectedTranslation(expectedFlowRules); |
| 462 | assertEquals(expectedTranslation, actualTranslation); |
| 463 | } |
| 464 | |
Wailok Shum | fb7e787 | 2021-06-18 17:30:08 +0800 | [diff] [blame] | 465 | /** |
| 466 | * Test is infra port scenarios for filtering objective. |
| 467 | */ |
| 468 | @Test |
| 469 | public void testIsInfraPort() throws FabricPipelinerException { |
| 470 | // PW transport vlan |
| 471 | FilteringObjective filteringObjective = DefaultFilteringObjective.builder() |
| 472 | .withKey(Criteria.matchInPort(PORT_1)) |
| 473 | .addCondition(Criteria.matchEthDst(ROUTER_MAC)) |
| 474 | .addCondition(Criteria.matchVlanId(VlanId.vlanId((short) DEFAULT_PW_TRANSPORT_VLAN))) |
| 475 | .withPriority(PRIORITY) |
pierventre | 4d29d3c | 2021-08-27 17:20:00 +0200 | [diff] [blame^] | 476 | .withMeta(DefaultTrafficTreatment.builder() |
| 477 | .writeMetadata(INFRA_PORT, 0xffffffffffffffffL) |
| 478 | .build()) |
Wailok Shum | fb7e787 | 2021-06-18 17:30:08 +0800 | [diff] [blame] | 479 | .fromApp(APP_ID) |
| 480 | .permit() |
| 481 | .add(); |
| 482 | ObjectiveTranslation actualTranslation = translator.translate(filteringObjective); |
| 483 | |
| 484 | Collection<FlowRule> expectedFlowRules = Lists.newArrayList(); |
| 485 | expectedFlowRules.add(buildExpectedVlanInPortRule( |
| 486 | PORT_1, VlanId.vlanId((short) DEFAULT_PW_TRANSPORT_VLAN), null, VlanId.NONE, |
| 487 | PORT_TYPE_INFRA, FabricConstants.FABRIC_INGRESS_FILTERING_INGRESS_PORT_VLAN)); |
| 488 | expectedFlowRules.addAll(buildExpectedFwdClassifierRule( |
| 489 | PORT_1, |
| 490 | ROUTER_MAC, |
| 491 | null, |
| 492 | Ethernet.TYPE_IPV4, |
| 493 | FWD_IPV4_ROUTING)); |
| 494 | expectedFlowRules.addAll(buildExpectedFwdClassifierRule( |
| 495 | PORT_1, |
| 496 | ROUTER_MAC, |
| 497 | null, |
| 498 | Ethernet.TYPE_IPV6, |
| 499 | FWD_IPV6_ROUTING)); |
| 500 | expectedFlowRules.addAll(buildExpectedFwdClassifierRule( |
| 501 | PORT_1, |
| 502 | ROUTER_MAC, |
| 503 | null, |
| 504 | Ethernet.MPLS_UNICAST, |
| 505 | FWD_MPLS)); |
| 506 | |
| 507 | ObjectiveTranslation expectedTranslation = buildExpectedTranslation(expectedFlowRules); |
| 508 | assertEquals(expectedTranslation, actualTranslation); |
| 509 | |
| 510 | // Untagged port scenario |
| 511 | filteringObjective = DefaultFilteringObjective.builder() |
| 512 | .withKey(Criteria.matchInPort(PORT_1)) |
| 513 | .addCondition(Criteria.matchEthDst(ROUTER_MAC)) |
| 514 | .addCondition(Criteria.matchVlanId(VlanId.NONE)) |
| 515 | .withPriority(PRIORITY) |
| 516 | .fromApp(APP_ID) |
| 517 | .withMeta(DefaultTrafficTreatment.builder() |
| 518 | .pushVlan() |
| 519 | .setVlanId(VlanId.vlanId((short) DEFAULT_VLAN)) |
pierventre | 4d29d3c | 2021-08-27 17:20:00 +0200 | [diff] [blame^] | 520 | .writeMetadata(INFRA_PORT, 0xffffffffffffffffL) |
Wailok Shum | fb7e787 | 2021-06-18 17:30:08 +0800 | [diff] [blame] | 521 | .build()) |
| 522 | .permit() |
| 523 | .add(); |
| 524 | actualTranslation = translator.translate(filteringObjective); |
| 525 | expectedFlowRules = Lists.newArrayList(); |
| 526 | expectedFlowRules.add(buildExpectedVlanInPortRule( |
| 527 | PORT_1, null, null, VlanId.vlanId((short) DEFAULT_VLAN), |
| 528 | PORT_TYPE_INFRA, FabricConstants.FABRIC_INGRESS_FILTERING_INGRESS_PORT_VLAN)); |
| 529 | expectedFlowRules.addAll(buildExpectedFwdClassifierRule( |
| 530 | PORT_1, |
| 531 | ROUTER_MAC, |
| 532 | null, |
| 533 | Ethernet.TYPE_IPV4, |
| 534 | FWD_IPV4_ROUTING)); |
| 535 | expectedFlowRules.addAll(buildExpectedFwdClassifierRule( |
| 536 | PORT_1, |
| 537 | ROUTER_MAC, |
| 538 | null, |
| 539 | Ethernet.TYPE_IPV6, |
| 540 | FWD_IPV6_ROUTING)); |
| 541 | expectedFlowRules.addAll(buildExpectedFwdClassifierRule( |
| 542 | PORT_1, |
| 543 | ROUTER_MAC, |
| 544 | null, |
| 545 | Ethernet.MPLS_UNICAST, |
| 546 | FWD_MPLS)); |
| 547 | |
| 548 | expectedTranslation = buildExpectedTranslation(expectedFlowRules); |
| 549 | assertEquals(expectedTranslation, actualTranslation); |
| 550 | } |
| 551 | |
pierventre | 4d29d3c | 2021-08-27 17:20:00 +0200 | [diff] [blame^] | 552 | /** |
| 553 | * Test is pair port scenarios for filtering objective. |
| 554 | */ |
| 555 | @Test |
| 556 | public void testIsPairPort() throws FabricPipelinerException { |
| 557 | // Only pair port flag |
| 558 | FilteringObjective filteringObjective = DefaultFilteringObjective.builder() |
| 559 | .withKey(Criteria.matchInPort(PORT_1)) |
| 560 | .addCondition(Criteria.matchEthDst(ROUTER_MAC)) |
| 561 | .addCondition(Criteria.matchVlanId(VLAN_100)) |
| 562 | .withPriority(PRIORITY) |
| 563 | .withMeta(DefaultTrafficTreatment.builder() |
| 564 | .writeMetadata(PAIR_PORT, 0xffffffffffffffffL) |
| 565 | .build()) |
| 566 | .fromApp(APP_ID) |
| 567 | .permit() |
| 568 | .add(); |
| 569 | ObjectiveTranslation actualTranslation = translator.translate(filteringObjective); |
| 570 | |
| 571 | Collection<FlowRule> expectedFlowRules = Lists.newArrayList(); |
| 572 | expectedFlowRules.add(buildExpectedVlanInPortRule( |
| 573 | PORT_1, VLAN_100, null, VlanId.NONE, |
| 574 | PORT_TYPE_INFRA, FabricConstants.FABRIC_INGRESS_FILTERING_INGRESS_PORT_VLAN)); |
| 575 | expectedFlowRules.addAll(buildExpectedFwdClassifierRule( |
| 576 | PORT_1, |
| 577 | ROUTER_MAC, |
| 578 | null, |
| 579 | Ethernet.TYPE_IPV4, |
| 580 | FWD_IPV4_ROUTING)); |
| 581 | expectedFlowRules.addAll(buildExpectedFwdClassifierRule( |
| 582 | PORT_1, |
| 583 | ROUTER_MAC, |
| 584 | null, |
| 585 | Ethernet.TYPE_IPV6, |
| 586 | FWD_IPV6_ROUTING)); |
| 587 | expectedFlowRules.addAll(buildExpectedFwdClassifierRule( |
| 588 | PORT_1, |
| 589 | ROUTER_MAC, |
| 590 | null, |
| 591 | Ethernet.MPLS_UNICAST, |
| 592 | FWD_MPLS)); |
| 593 | |
| 594 | ObjectiveTranslation expectedTranslation = buildExpectedTranslation(expectedFlowRules); |
| 595 | assertEquals(expectedTranslation, actualTranslation); |
| 596 | |
| 597 | // Pair port and config update flags |
| 598 | filteringObjective = DefaultFilteringObjective.builder() |
| 599 | .withKey(Criteria.matchInPort(PORT_1)) |
| 600 | .addCondition(Criteria.matchEthDst(ROUTER_MAC)) |
| 601 | .addCondition(Criteria.matchVlanId(VLAN_100)) |
| 602 | .withPriority(PRIORITY) |
| 603 | .fromApp(APP_ID) |
| 604 | .withMeta(DefaultTrafficTreatment.builder() |
| 605 | .writeMetadata(6, 0xffffffffffffffffL) |
| 606 | .build()) |
| 607 | .permit() |
| 608 | .add(); |
| 609 | |
| 610 | actualTranslation = translator.translate(filteringObjective); |
| 611 | expectedFlowRules = Lists.newArrayList(); |
| 612 | // Ingress port vlan rule |
| 613 | expectedFlowRules.add(buildExpectedVlanInPortRule( |
| 614 | PORT_1, VLAN_100, null, VlanId.NONE, PORT_TYPE_INFRA, |
| 615 | FabricConstants.FABRIC_INGRESS_FILTERING_INGRESS_PORT_VLAN)); |
| 616 | expectedTranslation = buildExpectedTranslation(expectedFlowRules); |
| 617 | assertEquals(expectedTranslation, actualTranslation); |
| 618 | } |
| 619 | |
Yi Tseng | 0b80972 | 2017-11-03 10:23:26 -0700 | [diff] [blame] | 620 | /* Utilities */ |
| 621 | |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame] | 622 | private void assertError(ObjectiveError error, ObjectiveTranslation actualTranslation) { |
| 623 | ObjectiveTranslation expectedTranslation = ObjectiveTranslation.ofError(error); |
| 624 | assertEquals(expectedTranslation, actualTranslation); |
| 625 | } |
| 626 | |
pierventre | 4d29d3c | 2021-08-27 17:20:00 +0200 | [diff] [blame^] | 627 | private FilteringObjective buildFilteringObjective(MacAddress dstMac, long portType) { |
Yi Tseng | 0b80972 | 2017-11-03 10:23:26 -0700 | [diff] [blame] | 628 | TrafficTreatment treatment = DefaultTrafficTreatment.builder() |
| 629 | .pushVlan() |
| 630 | .setVlanId(VLAN_100) |
pierventre | 4d29d3c | 2021-08-27 17:20:00 +0200 | [diff] [blame^] | 631 | .writeMetadata(portType, 0xffffffffffffffffL) |
Yi Tseng | 0b80972 | 2017-11-03 10:23:26 -0700 | [diff] [blame] | 632 | .build(); |
| 633 | DefaultFilteringObjective.Builder builder = DefaultFilteringObjective.builder() |
| 634 | .permit() |
| 635 | .withPriority(PRIORITY) |
| 636 | .withKey(Criteria.matchInPort(PORT_1)); |
| 637 | if (dstMac != null) { |
| 638 | builder.addCondition(Criteria.matchEthDst(dstMac)); |
| 639 | } |
| 640 | |
| 641 | builder.addCondition(Criteria.matchVlanId(VlanId.NONE)) |
| 642 | .withMeta(treatment) |
| 643 | .fromApp(APP_ID) |
| 644 | .makePermanent(); |
| 645 | return builder.add(); |
| 646 | } |
| 647 | |
Daniele Moro | 7c3a002 | 2019-07-12 13:38:34 -0700 | [diff] [blame] | 648 | private FlowRule buildExpectedVlanInPortRule(PortNumber inPort, |
| 649 | VlanId vlanId, |
| 650 | VlanId innerVlanId, |
Yi Tseng | 0b80972 | 2017-11-03 10:23:26 -0700 | [diff] [blame] | 651 | VlanId internalVlan, |
Wailok Shum | fb7e787 | 2021-06-18 17:30:08 +0800 | [diff] [blame] | 652 | byte portType, |
Yi Tseng | 0b80972 | 2017-11-03 10:23:26 -0700 | [diff] [blame] | 653 | TableId tableId) { |
| 654 | |
| 655 | TrafficSelector.Builder selector = DefaultTrafficSelector.builder() |
| 656 | .matchInPort(inPort); |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame] | 657 | PiAction piAction; |
Daniele Moro | 7c3a002 | 2019-07-12 13:38:34 -0700 | [diff] [blame] | 658 | selector.matchPi(buildPiCriterionVlan(vlanId, innerVlanId)); |
| 659 | if (!vlanValid(vlanId)) { |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame] | 660 | piAction = PiAction.builder() |
| 661 | .withId(FabricConstants.FABRIC_INGRESS_FILTERING_PERMIT_WITH_INTERNAL_VLAN) |
Wailok Shum | fb7e787 | 2021-06-18 17:30:08 +0800 | [diff] [blame] | 662 | .withParameter(new PiActionParam(FabricConstants.VLAN_ID, internalVlan.toShort())) |
| 663 | .withParameter(new PiActionParam(FabricConstants.PORT_TYPE, portType)) |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame] | 664 | .build(); |
Yi Tseng | 0b80972 | 2017-11-03 10:23:26 -0700 | [diff] [blame] | 665 | } else { |
Yi Tseng | 0b80972 | 2017-11-03 10:23:26 -0700 | [diff] [blame] | 666 | selector.matchVlanId(vlanId); |
Daniele Moro | f51d0c1 | 2019-07-30 10:43:10 -0700 | [diff] [blame] | 667 | if (vlanValid(innerVlanId)) { |
| 668 | selector.matchInnerVlanId(innerVlanId); |
| 669 | } |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame] | 670 | piAction = PiAction.builder() |
| 671 | .withId(FabricConstants.FABRIC_INGRESS_FILTERING_PERMIT) |
Wailok Shum | fb7e787 | 2021-06-18 17:30:08 +0800 | [diff] [blame] | 672 | .withParameter(new PiActionParam(FabricConstants.PORT_TYPE, portType)) |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame] | 673 | .build(); |
Yi Tseng | 0b80972 | 2017-11-03 10:23:26 -0700 | [diff] [blame] | 674 | } |
| 675 | |
| 676 | return DefaultFlowRule.builder() |
| 677 | .withPriority(PRIORITY) |
| 678 | .withSelector(selector.build()) |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame] | 679 | .withTreatment(DefaultTrafficTreatment.builder() |
| 680 | .piTableAction(piAction).build()) |
Yi Tseng | 0b80972 | 2017-11-03 10:23:26 -0700 | [diff] [blame] | 681 | .fromApp(APP_ID) |
| 682 | .forDevice(DEVICE_ID) |
| 683 | .makePermanent() |
| 684 | .forTable(tableId) |
| 685 | .build(); |
| 686 | } |
| 687 | |
Daniele Moro | 7c3a002 | 2019-07-12 13:38:34 -0700 | [diff] [blame] | 688 | private boolean vlanValid(VlanId vlanId) { |
| 689 | return (vlanId != null && !vlanId.equals(VlanId.NONE)); |
| 690 | } |
| 691 | |
| 692 | private PiCriterion buildPiCriterionVlan(VlanId vlanId, |
| 693 | VlanId innerVlanId) { |
| 694 | PiCriterion.Builder piCriterionBuilder = PiCriterion.builder() |
| 695 | .matchExact(FabricConstants.HDR_VLAN_IS_VALID, |
| 696 | vlanValid(vlanId) ? ONE : ZERO); |
| 697 | return piCriterionBuilder.build(); |
| 698 | } |
| 699 | |
Daniele Moro | 5a2de71 | 2019-09-24 14:34:07 -0700 | [diff] [blame] | 700 | private Collection<FlowRule> buildExpectedFwdClassifierRule(PortNumber inPort, |
| 701 | MacAddress dstMac, |
| 702 | MacAddress dstMacMask, |
| 703 | short ethType, |
| 704 | byte fwdClass) { |
Yi Tseng | 43ee7e8 | 2018-04-12 16:37:34 +0800 | [diff] [blame] | 705 | PiActionParam classParam = new PiActionParam(FabricConstants.FWD_TYPE, |
Yi Tseng | 0b80972 | 2017-11-03 10:23:26 -0700 | [diff] [blame] | 706 | ImmutableByteSequence.copyFrom(fwdClass)); |
| 707 | PiAction fwdClassifierAction = PiAction.builder() |
Yi Tseng | 43ee7e8 | 2018-04-12 16:37:34 +0800 | [diff] [blame] | 708 | .withId(FabricConstants.FABRIC_INGRESS_FILTERING_SET_FORWARDING_TYPE) |
Yi Tseng | 0b80972 | 2017-11-03 10:23:26 -0700 | [diff] [blame] | 709 | .withParameter(classParam) |
| 710 | .build(); |
| 711 | TrafficTreatment treatment = DefaultTrafficTreatment.builder() |
| 712 | .piTableAction(fwdClassifierAction) |
| 713 | .build(); |
| 714 | |
Daniele Moro | 5a2de71 | 2019-09-24 14:34:07 -0700 | [diff] [blame] | 715 | TrafficSelector.Builder sbuilder = DefaultTrafficSelector.builder() |
| 716 | .matchInPort(inPort); |
| 717 | if (dstMacMask != null) { |
| 718 | sbuilder.matchEthDstMasked(dstMac, dstMacMask); |
| 719 | } else { |
| 720 | sbuilder.matchEthDstMasked(dstMac, MacAddress.EXACT_MASK); |
| 721 | } |
| 722 | // Special case for MPLS UNICAST forwarding, need to build 2 rules for MPLS+IPv4 and MPLS+IPv6 |
| 723 | if (ethType == Ethernet.MPLS_UNICAST) { |
| 724 | return buildExpectedFwdClassifierRulesMpls(fwdClassifierAction, treatment, sbuilder); |
| 725 | } |
| 726 | sbuilder.matchPi(PiCriterion.builder() |
| 727 | .matchExact(FabricConstants.HDR_IP_ETH_TYPE, ethType) |
| 728 | .build()); |
| 729 | TrafficSelector selector = sbuilder.build(); |
| 730 | return List.of(DefaultFlowRule.builder() |
| 731 | .withPriority(PRIORITY) |
| 732 | .withSelector(selector) |
| 733 | .withTreatment(treatment) |
| 734 | .fromApp(APP_ID) |
| 735 | .forDevice(DEVICE_ID) |
| 736 | .makePermanent() |
| 737 | .forTable(FabricConstants.FABRIC_INGRESS_FILTERING_FWD_CLASSIFIER) |
| 738 | .build()); |
| 739 | } |
| 740 | |
| 741 | private Collection<FlowRule> buildExpectedFwdClassifierRulesMpls(PiAction fwdClassifierAction, |
| 742 | TrafficTreatment treatment, |
| 743 | TrafficSelector.Builder selectorBuilder) { |
| 744 | |
| 745 | Collection<FlowRule> flowRules = Lists.newArrayList(); |
| 746 | TrafficSelector selectorIpv4 = selectorBuilder |
| 747 | .add(PiCriterion.builder() |
Wailok Shum | fb7e787 | 2021-06-18 17:30:08 +0800 | [diff] [blame] | 748 | .matchTernary(FabricConstants.HDR_ETH_TYPE, Ethernet.MPLS_UNICAST, ETH_TYPE_EXACT_MASK) |
Daniele Moro | 5a2de71 | 2019-09-24 14:34:07 -0700 | [diff] [blame] | 749 | .matchExact(FabricConstants.HDR_IP_ETH_TYPE, Ethernet.TYPE_IPV4) |
| 750 | .build()) |
Yi Tseng | 0b80972 | 2017-11-03 10:23:26 -0700 | [diff] [blame] | 751 | .build(); |
Daniele Moro | 5a2de71 | 2019-09-24 14:34:07 -0700 | [diff] [blame] | 752 | TrafficSelector selectorIpv6 = selectorBuilder |
| 753 | .add(PiCriterion.builder() |
Wailok Shum | fb7e787 | 2021-06-18 17:30:08 +0800 | [diff] [blame] | 754 | .matchTernary(FabricConstants.HDR_ETH_TYPE, Ethernet.MPLS_UNICAST, ETH_TYPE_EXACT_MASK) |
Daniele Moro | 5a2de71 | 2019-09-24 14:34:07 -0700 | [diff] [blame] | 755 | .matchExact(FabricConstants.HDR_IP_ETH_TYPE, Ethernet.TYPE_IPV6) |
| 756 | .build()) |
| 757 | .build(); |
| 758 | flowRules.add(DefaultFlowRule.builder() |
| 759 | .withPriority(PRIORITY + 1) |
| 760 | .withSelector(selectorIpv4) |
| 761 | .withTreatment(treatment) |
| 762 | .fromApp(APP_ID) |
| 763 | .forDevice(DEVICE_ID) |
| 764 | .makePermanent() |
| 765 | .forTable(FabricConstants.FABRIC_INGRESS_FILTERING_FWD_CLASSIFIER) |
| 766 | .build()); |
| 767 | flowRules.add(DefaultFlowRule.builder() |
| 768 | .withPriority(PRIORITY + 1) |
| 769 | .withSelector(selectorIpv6) |
| 770 | .withTreatment(treatment) |
| 771 | .fromApp(APP_ID) |
| 772 | .forDevice(DEVICE_ID) |
| 773 | .makePermanent() |
| 774 | .forTable(FabricConstants.FABRIC_INGRESS_FILTERING_FWD_CLASSIFIER) |
| 775 | .build()); |
| 776 | return flowRules; |
| 777 | } |
| 778 | |
| 779 | private ObjectiveTranslation buildExpectedTranslation(Collection<FlowRule> flowRules) |
| 780 | throws FabricPipelinerException { |
| 781 | ObjectiveTranslation.Builder expectedTranslationBuilder = ObjectiveTranslation.builder(); |
| 782 | for (FlowRule flowRule : flowRules) { |
| 783 | expectedTranslationBuilder.addFlowRule(flowRule); |
| 784 | } |
| 785 | return expectedTranslationBuilder.build(); |
Yi Tseng | 0b80972 | 2017-11-03 10:23:26 -0700 | [diff] [blame] | 786 | } |
| 787 | } |