blob: 98dce1f3f205aee3488d02955346ed4075fde2da [file] [log] [blame]
Yi Tseng0b809722017-11-03 10:23:26 -07001/*
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 Cascone36d5e7a2019-09-25 01:02:53 -070017package org.onosproject.pipelines.fabric.impl.behaviour.pipeliner;
Yi Tseng0b809722017-11-03 10:23:26 -070018
Daniele Moro693d76f2019-09-24 14:34:07 -070019import com.google.common.collect.Lists;
Carmelo Casconeb5324e72018-11-25 02:26:32 -080020import org.junit.Before;
Yi Tseng0b809722017-11-03 10:23:26 -070021import org.junit.Test;
22import org.onlab.packet.Ethernet;
23import org.onlab.packet.MacAddress;
24import org.onlab.packet.VlanId;
25import org.onlab.util.ImmutableByteSequence;
26import org.onosproject.net.PortNumber;
27import org.onosproject.net.flow.DefaultFlowRule;
28import org.onosproject.net.flow.DefaultTrafficSelector;
29import org.onosproject.net.flow.DefaultTrafficTreatment;
30import org.onosproject.net.flow.FlowRule;
31import org.onosproject.net.flow.TableId;
32import org.onosproject.net.flow.TrafficSelector;
33import org.onosproject.net.flow.TrafficTreatment;
34import org.onosproject.net.flow.criteria.Criteria;
Daniele Moro7c3a0022019-07-12 13:38:34 -070035import org.onosproject.net.flow.criteria.PiCriterion;
Yi Tseng0b809722017-11-03 10:23:26 -070036import org.onosproject.net.flowobjective.DefaultFilteringObjective;
37import org.onosproject.net.flowobjective.FilteringObjective;
38import org.onosproject.net.flowobjective.ObjectiveError;
Yi Tseng0b809722017-11-03 10:23:26 -070039import org.onosproject.net.pi.runtime.PiAction;
40import org.onosproject.net.pi.runtime.PiActionParam;
Carmelo Cascone8f6b5cd2020-06-30 18:56:12 -070041import org.onosproject.pipelines.fabric.FabricConstants;
Yi Tseng0b809722017-11-03 10:23:26 -070042
Daniele Moro693d76f2019-09-24 14:34:07 -070043import java.util.Collection;
44import java.util.List;
45
Yi Tseng0b809722017-11-03 10:23:26 -070046import static org.junit.Assert.assertEquals;
Yi Tseng0b809722017-11-03 10:23:26 -070047
48/**
49 * Test cases for fabric.p4 pipeline filtering control block.
50 */
51public class FabricFilteringPipelinerTest extends FabricPipelinerTest {
52
Daniele Moro7c3a0022019-07-12 13:38:34 -070053 public static final byte[] ONE = {1};
54 public static final byte[] ZERO = {0};
Daniele Moro693d76f2019-09-24 14:34:07 -070055 public static final short EXACT_MATCH_ETH_TYPE = (short) 0xFFFF;
Carmelo Casconeb5324e72018-11-25 02:26:32 -080056 private FilteringObjectiveTranslator translator;
57
58 @Before
59 public void setup() {
60 super.doSetup();
61 translator = new FilteringObjectiveTranslator(DEVICE_ID, capabilitiesHashed);
62 }
63
Yi Tseng0b809722017-11-03 10:23:26 -070064 /**
65 * Creates one rule for ingress_port_vlan table and 3 rules for
Carmelo Cascone36d5e7a2019-09-25 01:02:53 -070066 * fwd_classifier table (IPv4, IPv6 and MPLS unicast) when the condition is
67 * VLAN + MAC.
Yi Tseng0b809722017-11-03 10:23:26 -070068 */
69 @Test
Carmelo Casconeb5324e72018-11-25 02:26:32 -080070 public void testRouterMacAndVlanFilter() throws FabricPipelinerException {
Yi Tseng0b809722017-11-03 10:23:26 -070071 FilteringObjective filteringObjective = buildFilteringObjective(ROUTER_MAC);
Carmelo Casconeb5324e72018-11-25 02:26:32 -080072 ObjectiveTranslation actualTranslation = translator.translate(filteringObjective);
Daniele Moro693d76f2019-09-24 14:34:07 -070073 Collection<FlowRule> expectedFlowRules = Lists.newArrayList();
Yi Tseng0b809722017-11-03 10:23:26 -070074 // in port vlan flow rule
Daniele Moro693d76f2019-09-24 14:34:07 -070075 expectedFlowRules.add(buildExpectedVlanInPortRule(
Carmelo Cascone36d5e7a2019-09-25 01:02:53 -070076 PORT_1,
77 VlanId.NONE,
78 VlanId.NONE,
79 VLAN_100,
Daniele Moro693d76f2019-09-24 14:34:07 -070080 FabricConstants.FABRIC_INGRESS_FILTERING_INGRESS_PORT_VLAN));
Yi Tseng0b809722017-11-03 10:23:26 -070081
82 // forwarding classifier ipv4
Daniele Moro693d76f2019-09-24 14:34:07 -070083 expectedFlowRules.addAll(buildExpectedFwdClassifierRule(
Carmelo Cascone36d5e7a2019-09-25 01:02:53 -070084 PORT_1,
85 ROUTER_MAC,
86 null,
87 Ethernet.TYPE_IPV4,
Daniele Moro693d76f2019-09-24 14:34:07 -070088 FilteringObjectiveTranslator.FWD_IPV4_ROUTING));
Yi Tseng0b809722017-11-03 10:23:26 -070089
90 // forwarding classifier ipv6
Daniele Moro693d76f2019-09-24 14:34:07 -070091 expectedFlowRules.addAll(buildExpectedFwdClassifierRule(
Carmelo Cascone36d5e7a2019-09-25 01:02:53 -070092 PORT_1,
93 ROUTER_MAC,
94 null,
95 Ethernet.TYPE_IPV6,
Daniele Moro693d76f2019-09-24 14:34:07 -070096 FilteringObjectiveTranslator.FWD_IPV6_ROUTING));
Yi Tseng0b809722017-11-03 10:23:26 -070097
98 // forwarding classifier mpls
Daniele Moro693d76f2019-09-24 14:34:07 -070099 expectedFlowRules.addAll(buildExpectedFwdClassifierRule(
Carmelo Cascone36d5e7a2019-09-25 01:02:53 -0700100 PORT_1,
101 ROUTER_MAC,
102 null,
103 Ethernet.MPLS_UNICAST,
Daniele Moro693d76f2019-09-24 14:34:07 -0700104 FilteringObjectiveTranslator.FWD_MPLS));
Carmelo Casconeb5324e72018-11-25 02:26:32 -0800105
Daniele Moro693d76f2019-09-24 14:34:07 -0700106// ObjectiveTranslation.Builder expectedTranslationBuilder = ObjectiveTranslation.builder()
107// .addFlowRule(inportFlowRuleExpected);
108// for (FlowRule flowRule : classifierV4FlowRuleExpected) {
109// expectedTranslationBuilder.addFlowRule(flowRule);
110// }
111// for (FlowRule flowRule : classifierV6FlowRuleExpected) {
112// expectedTranslationBuilder.addFlowRule(flowRule);
113// }
114// for (FlowRule flowRule : classifierMplsFlowRuleExpected) {
115// expectedTranslationBuilder.addFlowRule(flowRule);
116// }
117 ObjectiveTranslation expectedTranslation = buildExpectedTranslation(expectedFlowRules);
Carmelo Casconeb5324e72018-11-25 02:26:32 -0800118
119 assertEquals(expectedTranslation, actualTranslation);
Yi Tseng0b809722017-11-03 10:23:26 -0700120 }
121
122 /**
123 * Creates one rule for ingress_port_vlan table and one rule for
124 * fwd_classifier table (IPv4 multicast) when the condition is ipv4
125 * multicast mac address.
126 */
127 @Test
Carmelo Casconeb5324e72018-11-25 02:26:32 -0800128 public void testIpv4MulticastFwdClass() throws FabricPipelinerException {
Yi Tseng0b809722017-11-03 10:23:26 -0700129 TrafficTreatment treatment = DefaultTrafficTreatment.builder()
130 .pushVlan()
131 .setVlanId(VLAN_100)
132 .build();
133 FilteringObjective filteringObjective = DefaultFilteringObjective.builder()
134 .permit()
135 .withPriority(PRIORITY)
136 .withKey(Criteria.matchInPort(PORT_1))
Charles Chan384aea22018-08-23 22:08:02 -0700137 .addCondition(Criteria.matchEthDstMasked(MacAddress.IPV4_MULTICAST, MacAddress.IPV4_MULTICAST_MASK))
Yi Tseng0b809722017-11-03 10:23:26 -0700138 .addCondition(Criteria.matchVlanId(VlanId.NONE))
139 .withMeta(treatment)
140 .fromApp(APP_ID)
141 .makePermanent()
142 .add();
Carmelo Casconeb5324e72018-11-25 02:26:32 -0800143 ObjectiveTranslation actualTranslation = translator.translate(filteringObjective);
Daniele Moro693d76f2019-09-24 14:34:07 -0700144 List<FlowRule> expectedFlowRules = Lists.newArrayList();
Yi Tseng0b809722017-11-03 10:23:26 -0700145 // in port vlan flow rule
Daniele Moro693d76f2019-09-24 14:34:07 -0700146 expectedFlowRules.add(buildExpectedVlanInPortRule(
Carmelo Cascone36d5e7a2019-09-25 01:02:53 -0700147 PORT_1,
148 VlanId.NONE,
149 VlanId.NONE,
150 VLAN_100,
Daniele Moro693d76f2019-09-24 14:34:07 -0700151 FabricConstants.FABRIC_INGRESS_FILTERING_INGRESS_PORT_VLAN));
Yi Tseng0b809722017-11-03 10:23:26 -0700152
153 // forwarding classifier
Daniele Moro693d76f2019-09-24 14:34:07 -0700154 expectedFlowRules.addAll(buildExpectedFwdClassifierRule(
Carmelo Cascone36d5e7a2019-09-25 01:02:53 -0700155 PORT_1,
156 MacAddress.IPV4_MULTICAST,
157 MacAddress.IPV4_MULTICAST_MASK,
158 Ethernet.TYPE_IPV4,
Daniele Moro693d76f2019-09-24 14:34:07 -0700159 FilteringObjectiveTranslator.FWD_IPV4_ROUTING));
Carmelo Casconeb5324e72018-11-25 02:26:32 -0800160
Daniele Moro693d76f2019-09-24 14:34:07 -0700161 ObjectiveTranslation expectedTranslation = buildExpectedTranslation(expectedFlowRules);
Carmelo Casconeb5324e72018-11-25 02:26:32 -0800162
163 assertEquals(expectedTranslation, actualTranslation);
Yi Tseng0b809722017-11-03 10:23:26 -0700164 }
165
166 /**
167 * Creates one rule for ingress_port_vlan table and one rule for
168 * fwd_classifier table (IPv6 multicast) when the condition is ipv6
169 * multicast mac address.
170 */
171 @Test
Carmelo Casconeb5324e72018-11-25 02:26:32 -0800172 public void testIpv6MulticastFwdClass() throws FabricPipelinerException {
Yi Tseng0b809722017-11-03 10:23:26 -0700173 TrafficTreatment treatment = DefaultTrafficTreatment.builder()
174 .pushVlan()
175 .setVlanId(VLAN_100)
176 .build();
177 FilteringObjective filteringObjective = DefaultFilteringObjective.builder()
178 .permit()
179 .withPriority(PRIORITY)
180 .withKey(Criteria.matchInPort(PORT_1))
Charles Chan384aea22018-08-23 22:08:02 -0700181 .addCondition(Criteria.matchEthDstMasked(MacAddress.IPV6_MULTICAST, MacAddress.IPV6_MULTICAST_MASK))
Yi Tseng0b809722017-11-03 10:23:26 -0700182 .addCondition(Criteria.matchVlanId(VlanId.NONE))
183 .withMeta(treatment)
184 .fromApp(APP_ID)
185 .makePermanent()
186 .add();
Carmelo Casconeb5324e72018-11-25 02:26:32 -0800187 ObjectiveTranslation actualTranslation = translator.translate(filteringObjective);
Daniele Moro693d76f2019-09-24 14:34:07 -0700188 Collection<FlowRule> flowRules = Lists.newArrayList();
Yi Tseng0b809722017-11-03 10:23:26 -0700189 // in port vlan flow rule
Daniele Moro693d76f2019-09-24 14:34:07 -0700190 flowRules.add(buildExpectedVlanInPortRule(
Carmelo Cascone36d5e7a2019-09-25 01:02:53 -0700191 PORT_1,
192 VlanId.NONE,
193 VlanId.NONE,
194 VLAN_100,
Daniele Moro693d76f2019-09-24 14:34:07 -0700195 FabricConstants.FABRIC_INGRESS_FILTERING_INGRESS_PORT_VLAN));
Yi Tseng0b809722017-11-03 10:23:26 -0700196
Daniele Moro693d76f2019-09-24 14:34:07 -0700197 flowRules.addAll(buildExpectedFwdClassifierRule(
Carmelo Cascone36d5e7a2019-09-25 01:02:53 -0700198 PORT_1,
199 MacAddress.IPV6_MULTICAST,
200 MacAddress.IPV6_MULTICAST_MASK,
201 Ethernet.TYPE_IPV6,
Daniele Moro693d76f2019-09-24 14:34:07 -0700202 FilteringObjectiveTranslator.FWD_IPV6_ROUTING));
Carmelo Casconeb5324e72018-11-25 02:26:32 -0800203
Daniele Moro693d76f2019-09-24 14:34:07 -0700204 ObjectiveTranslation expectedTranslation = buildExpectedTranslation(flowRules);
Carmelo Casconeb5324e72018-11-25 02:26:32 -0800205
206 assertEquals(expectedTranslation, actualTranslation);
Yi Tseng0b809722017-11-03 10:23:26 -0700207 }
208
209 /**
Carmelo Cascone36d5e7a2019-09-25 01:02:53 -0700210 * Creates only one rule for ingress_port_vlan table if there is no
211 * condition of destination mac address. The packet will be handled by
212 * bridging table by default.
Yi Tseng0b809722017-11-03 10:23:26 -0700213 */
214 @Test
Carmelo Casconeb5324e72018-11-25 02:26:32 -0800215 public void testFwdBridging() throws Exception {
Yi Tseng0b809722017-11-03 10:23:26 -0700216 FilteringObjective filteringObjective = buildFilteringObjective(null);
Carmelo Casconeb5324e72018-11-25 02:26:32 -0800217 ObjectiveTranslation actualTranslation = translator.translate(filteringObjective);
Yi Tseng0b809722017-11-03 10:23:26 -0700218
219 // in port vlan flow rule
Carmelo Cascone36d5e7a2019-09-25 01:02:53 -0700220 FlowRule flowRuleExpected = buildExpectedVlanInPortRule(
221 PORT_1,
222 VlanId.NONE,
223 VlanId.NONE,
224 VLAN_100,
225 FabricConstants.FABRIC_INGRESS_FILTERING_INGRESS_PORT_VLAN);
Yi Tseng0b809722017-11-03 10:23:26 -0700226
227 // No rules in forwarding classifier, will do default action: set fwd type to bridging
Carmelo Casconeb5324e72018-11-25 02:26:32 -0800228
229 ObjectiveTranslation expectedTranslation = ObjectiveTranslation.builder()
230 .addFlowRule(flowRuleExpected)
231 .build();
232
233 assertEquals(expectedTranslation, actualTranslation);
Yi Tseng0b809722017-11-03 10:23:26 -0700234 }
235
236 /**
Carmelo Casconeb5324e72018-11-25 02:26:32 -0800237 * Test DENY objective.
Yi Tseng0b809722017-11-03 10:23:26 -0700238 */
239 @Test
Carmelo Casconeb5324e72018-11-25 02:26:32 -0800240 public void testDenyObjective() throws FabricPipelinerException {
Yi Tseng0b809722017-11-03 10:23:26 -0700241 FilteringObjective filteringObjective = DefaultFilteringObjective.builder()
242 .deny()
243 .withKey(Criteria.matchInPort(PORT_1))
Carmelo Casconeb5324e72018-11-25 02:26:32 -0800244 .addCondition(Criteria.matchVlanId(VlanId.NONE))
Yi Tseng0b809722017-11-03 10:23:26 -0700245 .fromApp(APP_ID)
246 .makePermanent()
Carmelo Casconeb5324e72018-11-25 02:26:32 -0800247 .withPriority(PRIORITY)
Yi Tseng0b809722017-11-03 10:23:26 -0700248 .add();
249
Carmelo Casconeb5324e72018-11-25 02:26:32 -0800250 ObjectiveTranslation actualTranslation = translator.translate(filteringObjective);
Yi Tseng0b809722017-11-03 10:23:26 -0700251
Carmelo Casconeb5324e72018-11-25 02:26:32 -0800252 TrafficSelector.Builder selector = DefaultTrafficSelector.builder()
253 .matchInPort(PORT_1)
Daniele Moro7c3a0022019-07-12 13:38:34 -0700254 .matchPi(buildPiCriterionVlan(null, null));
Carmelo Casconeb5324e72018-11-25 02:26:32 -0800255 PiAction piAction = PiAction.builder()
Carmelo Cascone36d5e7a2019-09-25 01:02:53 -0700256 .withId(FabricConstants.FABRIC_INGRESS_FILTERING_DENY)
257 .build();
Carmelo Casconeb5324e72018-11-25 02:26:32 -0800258 FlowRule expectedFlowRule = DefaultFlowRule.builder()
259 .withPriority(PRIORITY)
260 .withSelector(selector.build())
261 .withTreatment(DefaultTrafficTreatment.builder()
262 .piTableAction(piAction).build())
263 .fromApp(APP_ID)
264 .forDevice(DEVICE_ID)
265 .makePermanent()
266 .forTable(FabricConstants.FABRIC_INGRESS_FILTERING_INGRESS_PORT_VLAN)
267 .build();
Yi Tseng0b809722017-11-03 10:23:26 -0700268
Carmelo Casconeb5324e72018-11-25 02:26:32 -0800269 ObjectiveTranslation expectedTranslation = ObjectiveTranslation.builder()
270 .addFlowRule(expectedFlowRule)
271 .build();
Yi Tseng0b809722017-11-03 10:23:26 -0700272
Carmelo Casconeb5324e72018-11-25 02:26:32 -0800273 assertEquals(expectedTranslation, actualTranslation);
274
Yi Tseng0b809722017-11-03 10:23:26 -0700275 }
276
277 /**
Carmelo Cascone36d5e7a2019-09-25 01:02:53 -0700278 * Test double VLAN pop filtering objective Creates one rule for
279 * ingress_port_vlan table and 3 rules for fwd_classifier table (IPv4, IPv6
280 * and MPLS unicast) when the condition is MAC + VLAN + INNER_VLAN.
Daniele Moro77ef35c2019-07-30 10:43:10 -0700281 */
282 @Test
283 public void testPopVlan() throws FabricPipelinerException {
284 FilteringObjective filteringObjective = DefaultFilteringObjective.builder()
285 .withKey(Criteria.matchInPort(PORT_1))
286 .addCondition(Criteria.matchEthDst(ROUTER_MAC))
287 .addCondition(Criteria.matchVlanId(VLAN_100))
288 .addCondition(Criteria.matchInnerVlanId(VLAN_200))
289 .withPriority(PRIORITY)
290 .fromApp(APP_ID)
291 .withMeta(DefaultTrafficTreatment.builder()
292 .popVlan()
293 .build())
294 .permit()
295 .add();
296 ObjectiveTranslation actualTranslation = translator.translate(filteringObjective);
Daniele Moro693d76f2019-09-24 14:34:07 -0700297 Collection<FlowRule> expectedFlowRules = Lists.newArrayList();
Daniele Moro77ef35c2019-07-30 10:43:10 -0700298 // Ingress port vlan rule
Daniele Moro693d76f2019-09-24 14:34:07 -0700299 expectedFlowRules.add(buildExpectedVlanInPortRule(
Daniele Moro77ef35c2019-07-30 10:43:10 -0700300 PORT_1, VLAN_100, VLAN_200, VlanId.NONE,
Daniele Moro693d76f2019-09-24 14:34:07 -0700301 FabricConstants.FABRIC_INGRESS_FILTERING_INGRESS_PORT_VLAN));
Daniele Moro77ef35c2019-07-30 10:43:10 -0700302 // Forwarding classifier rules (ipv6, ipv4, mpls)
Daniele Moro693d76f2019-09-24 14:34:07 -0700303 expectedFlowRules.addAll(buildExpectedFwdClassifierRule(
Carmelo Cascone36d5e7a2019-09-25 01:02:53 -0700304 PORT_1, ROUTER_MAC, null, Ethernet.TYPE_IPV4,
Daniele Moro693d76f2019-09-24 14:34:07 -0700305 FilteringObjectiveTranslator.FWD_IPV4_ROUTING));
306 expectedFlowRules.addAll(buildExpectedFwdClassifierRule(
Carmelo Cascone36d5e7a2019-09-25 01:02:53 -0700307 PORT_1, ROUTER_MAC, null, Ethernet.TYPE_IPV6,
Daniele Moro693d76f2019-09-24 14:34:07 -0700308 FilteringObjectiveTranslator.FWD_IPV6_ROUTING));
309 expectedFlowRules.addAll(buildExpectedFwdClassifierRule(
Carmelo Cascone36d5e7a2019-09-25 01:02:53 -0700310 PORT_1, ROUTER_MAC, null, Ethernet.MPLS_UNICAST,
Daniele Moro693d76f2019-09-24 14:34:07 -0700311 FilteringObjectiveTranslator.FWD_MPLS));
312 ObjectiveTranslation expectedTranslation = buildExpectedTranslation(expectedFlowRules);
Daniele Moro77ef35c2019-07-30 10:43:10 -0700313
314 assertEquals(expectedTranslation, actualTranslation);
315 }
316
317 /**
Yi Tseng0b809722017-11-03 10:23:26 -0700318 * Incorrect filtering key or filtering conditions test.
319 */
320 @Test
321 public void badParamTest() {
322 // Filtering objective should contains filtering key
323 FilteringObjective filteringObjective = DefaultFilteringObjective.builder()
324 .permit()
325 .addCondition(Criteria.matchVlanId(VLAN_100))
326 .fromApp(APP_ID)
327 .makePermanent()
328 .add();
329
Carmelo Casconeb5324e72018-11-25 02:26:32 -0800330 ObjectiveTranslation result1 = translator.translate(filteringObjective);
331 assertError(ObjectiveError.BADPARAMS, result1);
Yi Tseng0b809722017-11-03 10:23:26 -0700332
333 // Filtering objective should use in_port as key
334 filteringObjective = DefaultFilteringObjective.builder()
335 .permit()
336 .withKey(Criteria.matchEthDst(ROUTER_MAC))
337 .addCondition(Criteria.matchVlanId(VLAN_100))
338 .withMeta(DefaultTrafficTreatment.emptyTreatment())
339 .fromApp(APP_ID)
340 .makePermanent()
341 .add();
342
Carmelo Casconeb5324e72018-11-25 02:26:32 -0800343 ObjectiveTranslation result2 = translator.translate(filteringObjective);
344 assertError(ObjectiveError.BADPARAMS, result2);
Yi Tseng0b809722017-11-03 10:23:26 -0700345 }
346
pierventref29d97b2021-01-08 17:35:47 +0100347 /**
348 * Test port update scenarios for filtering objective. Creates only one rule for
349 * ingress_port_vlan table.
350 */
351 @Test
352 public void testPortUpdate() throws FabricPipelinerException {
353 // Tagged port scenario
354 FilteringObjective filteringObjective = DefaultFilteringObjective.builder()
355 .withKey(Criteria.matchInPort(PORT_1))
356 .addCondition(Criteria.matchEthDst(ROUTER_MAC))
357 .addCondition(Criteria.matchVlanId(VLAN_100))
358 .withPriority(PRIORITY)
359 .fromApp(APP_ID)
360 .withMeta(DefaultTrafficTreatment.builder()
361 .writeMetadata(2, 0xffffffffffffffffL)
362 .build())
363 .permit()
364 .add();
365 ObjectiveTranslation actualTranslation = translator.translate(filteringObjective);
366 Collection<FlowRule> expectedFlowRules = Lists.newArrayList();
367 // Ingress port vlan rule
368 expectedFlowRules.add(buildExpectedVlanInPortRule(
369 PORT_1, VLAN_100, null, VlanId.NONE,
370 FabricConstants.FABRIC_INGRESS_FILTERING_INGRESS_PORT_VLAN));
371 ObjectiveTranslation expectedTranslation = buildExpectedTranslation(expectedFlowRules);
372 assertEquals(expectedTranslation, actualTranslation);
373
374 // Untagged port scenario
375 filteringObjective = DefaultFilteringObjective.builder()
376 .withKey(Criteria.matchInPort(PORT_1))
377 .addCondition(Criteria.matchEthDst(ROUTER_MAC))
378 .addCondition(Criteria.matchVlanId(VlanId.NONE))
379 .withPriority(PRIORITY)
380 .fromApp(APP_ID)
381 .withMeta(DefaultTrafficTreatment.builder()
382 .pushVlan()
383 .setVlanId(VLAN_200)
384 .writeMetadata(2, 0xffffffffffffffffL)
385 .build())
386 .permit()
387 .add();
388 actualTranslation = translator.translate(filteringObjective);
389 expectedFlowRules = Lists.newArrayList();
390 // Ingress port vlan rule
391 expectedFlowRules.add(buildExpectedVlanInPortRule(
392 PORT_1, VlanId.NONE, null, VLAN_200,
393 FabricConstants.FABRIC_INGRESS_FILTERING_INGRESS_PORT_VLAN));
394 expectedTranslation = buildExpectedTranslation(expectedFlowRules);
395 assertEquals(expectedTranslation, actualTranslation);
396 }
397
Yi Tseng0b809722017-11-03 10:23:26 -0700398 /* Utilities */
399
Carmelo Casconeb5324e72018-11-25 02:26:32 -0800400 private void assertError(ObjectiveError error, ObjectiveTranslation actualTranslation) {
401 ObjectiveTranslation expectedTranslation = ObjectiveTranslation.ofError(error);
402 assertEquals(expectedTranslation, actualTranslation);
403 }
404
Yi Tseng0b809722017-11-03 10:23:26 -0700405 private FilteringObjective buildFilteringObjective(MacAddress dstMac) {
406 TrafficTreatment treatment = DefaultTrafficTreatment.builder()
407 .pushVlan()
408 .setVlanId(VLAN_100)
409 .build();
410 DefaultFilteringObjective.Builder builder = DefaultFilteringObjective.builder()
411 .permit()
412 .withPriority(PRIORITY)
413 .withKey(Criteria.matchInPort(PORT_1));
414 if (dstMac != null) {
415 builder.addCondition(Criteria.matchEthDst(dstMac));
416 }
417
418 builder.addCondition(Criteria.matchVlanId(VlanId.NONE))
419 .withMeta(treatment)
420 .fromApp(APP_ID)
421 .makePermanent();
422 return builder.add();
423 }
424
Daniele Moro7c3a0022019-07-12 13:38:34 -0700425 private FlowRule buildExpectedVlanInPortRule(PortNumber inPort,
426 VlanId vlanId,
427 VlanId innerVlanId,
Yi Tseng0b809722017-11-03 10:23:26 -0700428 VlanId internalVlan,
429 TableId tableId) {
430
431 TrafficSelector.Builder selector = DefaultTrafficSelector.builder()
432 .matchInPort(inPort);
Carmelo Casconeb5324e72018-11-25 02:26:32 -0800433 PiAction piAction;
Daniele Moro7c3a0022019-07-12 13:38:34 -0700434 selector.matchPi(buildPiCriterionVlan(vlanId, innerVlanId));
435 if (!vlanValid(vlanId)) {
Carmelo Casconeb5324e72018-11-25 02:26:32 -0800436 piAction = PiAction.builder()
437 .withId(FabricConstants.FABRIC_INGRESS_FILTERING_PERMIT_WITH_INTERNAL_VLAN)
438 .withParameter(new PiActionParam(
439 FabricConstants.VLAN_ID, internalVlan.toShort()))
440 .build();
Yi Tseng0b809722017-11-03 10:23:26 -0700441 } else {
Yi Tseng0b809722017-11-03 10:23:26 -0700442 selector.matchVlanId(vlanId);
Daniele Moro77ef35c2019-07-30 10:43:10 -0700443 if (vlanValid(innerVlanId)) {
444 selector.matchInnerVlanId(innerVlanId);
445 }
Carmelo Casconeb5324e72018-11-25 02:26:32 -0800446 piAction = PiAction.builder()
447 .withId(FabricConstants.FABRIC_INGRESS_FILTERING_PERMIT)
448 .build();
Yi Tseng0b809722017-11-03 10:23:26 -0700449 }
450
451 return DefaultFlowRule.builder()
452 .withPriority(PRIORITY)
453 .withSelector(selector.build())
Carmelo Casconeb5324e72018-11-25 02:26:32 -0800454 .withTreatment(DefaultTrafficTreatment.builder()
455 .piTableAction(piAction).build())
Yi Tseng0b809722017-11-03 10:23:26 -0700456 .fromApp(APP_ID)
457 .forDevice(DEVICE_ID)
458 .makePermanent()
459 .forTable(tableId)
460 .build();
461 }
462
Daniele Moro7c3a0022019-07-12 13:38:34 -0700463 private boolean vlanValid(VlanId vlanId) {
464 return (vlanId != null && !vlanId.equals(VlanId.NONE));
465 }
466
467 private PiCriterion buildPiCriterionVlan(VlanId vlanId,
468 VlanId innerVlanId) {
469 PiCriterion.Builder piCriterionBuilder = PiCriterion.builder()
470 .matchExact(FabricConstants.HDR_VLAN_IS_VALID,
471 vlanValid(vlanId) ? ONE : ZERO);
472 return piCriterionBuilder.build();
473 }
474
Daniele Moro693d76f2019-09-24 14:34:07 -0700475 private Collection<FlowRule> buildExpectedFwdClassifierRule(PortNumber inPort,
476 MacAddress dstMac,
477 MacAddress dstMacMask,
478 short ethType,
479 byte fwdClass) {
Yi Tseng43ee7e82018-04-12 16:37:34 +0800480 PiActionParam classParam = new PiActionParam(FabricConstants.FWD_TYPE,
Yi Tseng0b809722017-11-03 10:23:26 -0700481 ImmutableByteSequence.copyFrom(fwdClass));
482 PiAction fwdClassifierAction = PiAction.builder()
Yi Tseng43ee7e82018-04-12 16:37:34 +0800483 .withId(FabricConstants.FABRIC_INGRESS_FILTERING_SET_FORWARDING_TYPE)
Yi Tseng0b809722017-11-03 10:23:26 -0700484 .withParameter(classParam)
485 .build();
486 TrafficTreatment treatment = DefaultTrafficTreatment.builder()
487 .piTableAction(fwdClassifierAction)
488 .build();
489
Daniele Moro693d76f2019-09-24 14:34:07 -0700490 TrafficSelector.Builder sbuilder = DefaultTrafficSelector.builder()
491 .matchInPort(inPort);
492 if (dstMacMask != null) {
493 sbuilder.matchEthDstMasked(dstMac, dstMacMask);
494 } else {
495 sbuilder.matchEthDstMasked(dstMac, MacAddress.EXACT_MASK);
496 }
497 // Special case for MPLS UNICAST forwarding, need to build 2 rules for MPLS+IPv4 and MPLS+IPv6
498 if (ethType == Ethernet.MPLS_UNICAST) {
499 return buildExpectedFwdClassifierRulesMpls(fwdClassifierAction, treatment, sbuilder);
500 }
501 sbuilder.matchPi(PiCriterion.builder()
502 .matchExact(FabricConstants.HDR_IP_ETH_TYPE, ethType)
503 .build());
504 TrafficSelector selector = sbuilder.build();
505 return List.of(DefaultFlowRule.builder()
506 .withPriority(PRIORITY)
507 .withSelector(selector)
508 .withTreatment(treatment)
509 .fromApp(APP_ID)
510 .forDevice(DEVICE_ID)
511 .makePermanent()
512 .forTable(FabricConstants.FABRIC_INGRESS_FILTERING_FWD_CLASSIFIER)
513 .build());
514 }
515
516 private Collection<FlowRule> buildExpectedFwdClassifierRulesMpls(PiAction fwdClassifierAction,
517 TrafficTreatment treatment,
518 TrafficSelector.Builder selectorBuilder) {
519
520 Collection<FlowRule> flowRules = Lists.newArrayList();
521 TrafficSelector selectorIpv4 = selectorBuilder
522 .add(PiCriterion.builder()
523 .matchTernary(FabricConstants.HDR_ETH_TYPE, Ethernet.MPLS_UNICAST, EXACT_MATCH_ETH_TYPE)
524 .matchExact(FabricConstants.HDR_IP_ETH_TYPE, Ethernet.TYPE_IPV4)
525 .build())
Yi Tseng0b809722017-11-03 10:23:26 -0700526 .build();
Daniele Moro693d76f2019-09-24 14:34:07 -0700527 TrafficSelector selectorIpv6 = selectorBuilder
528 .add(PiCriterion.builder()
529 .matchTernary(FabricConstants.HDR_ETH_TYPE, Ethernet.MPLS_UNICAST, EXACT_MATCH_ETH_TYPE)
530 .matchExact(FabricConstants.HDR_IP_ETH_TYPE, Ethernet.TYPE_IPV6)
531 .build())
532 .build();
533 flowRules.add(DefaultFlowRule.builder()
534 .withPriority(PRIORITY + 1)
535 .withSelector(selectorIpv4)
536 .withTreatment(treatment)
537 .fromApp(APP_ID)
538 .forDevice(DEVICE_ID)
539 .makePermanent()
540 .forTable(FabricConstants.FABRIC_INGRESS_FILTERING_FWD_CLASSIFIER)
541 .build());
542 flowRules.add(DefaultFlowRule.builder()
543 .withPriority(PRIORITY + 1)
544 .withSelector(selectorIpv6)
545 .withTreatment(treatment)
546 .fromApp(APP_ID)
547 .forDevice(DEVICE_ID)
548 .makePermanent()
549 .forTable(FabricConstants.FABRIC_INGRESS_FILTERING_FWD_CLASSIFIER)
550 .build());
551 return flowRules;
552 }
553
554 private ObjectiveTranslation buildExpectedTranslation(Collection<FlowRule> flowRules)
555 throws FabricPipelinerException {
556 ObjectiveTranslation.Builder expectedTranslationBuilder = ObjectiveTranslation.builder();
557 for (FlowRule flowRule : flowRules) {
558 expectedTranslationBuilder.addFlowRule(flowRule);
559 }
560 return expectedTranslationBuilder.build();
Yi Tseng0b809722017-11-03 10:23:26 -0700561 }
562}