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 | |
| 17 | package org.onosproject.pipelines.fabric.pipeliner; |
| 18 | |
Yi Tseng | 1b154bd | 2017-11-20 17:48:19 -0800 | [diff] [blame] | 19 | import com.google.common.collect.ImmutableList; |
Yi Tseng | 0b80972 | 2017-11-03 10:23:26 -0700 | [diff] [blame] | 20 | import org.junit.Ignore; |
| 21 | import org.junit.Test; |
| 22 | import org.onlab.util.ImmutableByteSequence; |
| 23 | import org.onosproject.net.flow.DefaultFlowRule; |
| 24 | import org.onosproject.net.flow.DefaultTrafficSelector; |
| 25 | import org.onosproject.net.flow.DefaultTrafficTreatment; |
| 26 | import org.onosproject.net.flow.FlowRule; |
| 27 | import org.onosproject.net.flow.TrafficSelector; |
| 28 | import org.onosproject.net.flow.TrafficTreatment; |
| 29 | import org.onosproject.net.flow.criteria.PiCriterion; |
| 30 | import org.onosproject.net.flowobjective.DefaultNextObjective; |
| 31 | import org.onosproject.net.flowobjective.NextObjective; |
Yi Tseng | 1b154bd | 2017-11-20 17:48:19 -0800 | [diff] [blame] | 32 | import org.onosproject.net.group.DefaultGroupBucket; |
| 33 | import org.onosproject.net.group.DefaultGroupDescription; |
| 34 | import org.onosproject.net.group.GroupBucket; |
| 35 | import org.onosproject.net.group.GroupBuckets; |
Yi Tseng | 0b80972 | 2017-11-03 10:23:26 -0700 | [diff] [blame] | 36 | import org.onosproject.net.group.GroupDescription; |
| 37 | import org.onosproject.net.pi.runtime.PiAction; |
Yi Tseng | 1b154bd | 2017-11-20 17:48:19 -0800 | [diff] [blame] | 38 | import org.onosproject.net.pi.runtime.PiActionGroupId; |
Yi Tseng | 0b80972 | 2017-11-03 10:23:26 -0700 | [diff] [blame] | 39 | import org.onosproject.net.pi.runtime.PiActionParam; |
Yi Tseng | 1b154bd | 2017-11-20 17:48:19 -0800 | [diff] [blame] | 40 | import org.onosproject.net.pi.runtime.PiGroupKey; |
Yi Tseng | 0b80972 | 2017-11-03 10:23:26 -0700 | [diff] [blame] | 41 | import org.onosproject.pipelines.fabric.FabricConstants; |
| 42 | |
| 43 | import java.util.List; |
Yi Tseng | 1b154bd | 2017-11-20 17:48:19 -0800 | [diff] [blame] | 44 | import java.util.stream.Collectors; |
Yi Tseng | 0b80972 | 2017-11-03 10:23:26 -0700 | [diff] [blame] | 45 | |
| 46 | import static org.junit.Assert.assertEquals; |
| 47 | import static org.junit.Assert.assertTrue; |
Yi Tseng | 1b154bd | 2017-11-20 17:48:19 -0800 | [diff] [blame] | 48 | import static org.onosproject.pipelines.fabric.FabricConstants.ACT_PRF_ECMP_SELECTOR_ID; |
| 49 | import static org.onosproject.pipelines.fabric.FabricConstants.TBL_HASHED_ID; |
Yi Tseng | 0b80972 | 2017-11-03 10:23:26 -0700 | [diff] [blame] | 50 | |
| 51 | /** |
| 52 | * Test cases for fabric.p4 pipeline next control block. |
| 53 | */ |
| 54 | public class FabricNextPipelinerTest extends FabricPipelinerTest { |
| 55 | |
| 56 | /** |
| 57 | * Test program output rule for Simple table. |
| 58 | */ |
| 59 | @Test |
| 60 | public void testSimpleOutput() { |
| 61 | TrafficTreatment treatment = DefaultTrafficTreatment.builder() |
| 62 | .setOutput(PORT_1) |
| 63 | .build(); |
| 64 | testSimple(treatment); |
| 65 | } |
| 66 | |
| 67 | /** |
| 68 | * Test program set vlan and output rule for Simple table. |
| 69 | */ |
| 70 | @Test |
| 71 | public void testSimpleOutputWithVlanTranslation() { |
| 72 | TrafficTreatment treatment = DefaultTrafficTreatment.builder() |
| 73 | .setVlanId(VLAN_100) |
| 74 | .setOutput(PORT_1) |
| 75 | .build(); |
| 76 | testSimple(treatment); |
| 77 | } |
| 78 | |
Yi Tseng | dbe0560 | 2017-11-17 18:02:43 -0800 | [diff] [blame] | 79 | /** |
| 80 | * Test program set mac and output rule for Simple table. |
| 81 | */ |
| 82 | @Test |
| 83 | public void testSimpleOutputWithMacTranslation() { |
| 84 | TrafficTreatment treatment = DefaultTrafficTreatment.builder() |
| 85 | .setEthSrc(ROUTER_MAC) |
| 86 | .setEthDst(HOST_MAC) |
| 87 | .setOutput(PORT_1) |
| 88 | .build(); |
| 89 | testSimple(treatment); |
| 90 | } |
| 91 | |
Yi Tseng | 0b80972 | 2017-11-03 10:23:26 -0700 | [diff] [blame] | 92 | private void testSimple(TrafficTreatment treatment) { |
| 93 | NextObjective nextObjective = DefaultNextObjective.builder() |
| 94 | .withId(NEXT_ID_1) |
| 95 | .withPriority(PRIORITY) |
| 96 | .addTreatment(treatment) |
| 97 | .withType(NextObjective.Type.SIMPLE) |
| 98 | .makePermanent() |
| 99 | .fromApp(APP_ID) |
| 100 | .add(); |
| 101 | |
| 102 | PipelinerTranslationResult result = pipeliner.pipelinerNext.next(nextObjective); |
| 103 | |
| 104 | List<FlowRule> flowRulesInstalled = (List<FlowRule>) result.flowRules(); |
| 105 | List<GroupDescription> groupsInstalled = (List<GroupDescription>) result.groups(); |
| 106 | assertEquals(2, flowRulesInstalled.size()); |
| 107 | assertTrue(groupsInstalled.isEmpty()); |
| 108 | |
Yi Tseng | 1b154bd | 2017-11-20 17:48:19 -0800 | [diff] [blame] | 109 | verifyNextIdMapping(flowRulesInstalled.get(0), NEXT_TYPE_SIMPLE); |
Yi Tseng | 0b80972 | 2017-11-03 10:23:26 -0700 | [diff] [blame] | 110 | |
Yi Tseng | 1b154bd | 2017-11-20 17:48:19 -0800 | [diff] [blame] | 111 | // Simple table |
Yi Tseng | 0b80972 | 2017-11-03 10:23:26 -0700 | [diff] [blame] | 112 | PiCriterion nextIdCriterion = PiCriterion.builder() |
Yi Tseng | 1b154bd | 2017-11-20 17:48:19 -0800 | [diff] [blame] | 113 | .matchExact(FabricConstants.HF_FABRIC_METADATA_NEXT_ID_ID, NEXT_ID_1) |
| 114 | .build(); |
| 115 | TrafficSelector nextIdSelector = DefaultTrafficSelector.builder() |
| 116 | .matchPi(nextIdCriterion) |
| 117 | .build(); |
| 118 | FlowRule actualFlowRule = flowRulesInstalled.get(1); |
| 119 | FlowRule expectedFlowRule = DefaultFlowRule.builder() |
| 120 | .forDevice(DEVICE_ID) |
| 121 | .fromApp(APP_ID) |
| 122 | .makePermanent() |
| 123 | // FIXME: currently next objective doesn't support priority, ignore this |
| 124 | .withPriority(0) |
| 125 | .forTable(FabricConstants.TBL_SIMPLE_ID) |
| 126 | .withSelector(nextIdSelector) |
| 127 | .withTreatment(treatment) |
| 128 | .build(); |
| 129 | assertTrue(expectedFlowRule.exactMatch(actualFlowRule)); |
| 130 | } |
| 131 | |
| 132 | private void verifyNextIdMapping(FlowRule flowRule, byte nextType) { |
| 133 | FlowRule expectedFlowRule; |
| 134 | PiCriterion nextIdCriterion = PiCriterion.builder() |
| 135 | .matchExact(FabricConstants.HF_FABRIC_METADATA_NEXT_ID_ID, NEXT_ID_1) |
Yi Tseng | 0b80972 | 2017-11-03 10:23:26 -0700 | [diff] [blame] | 136 | .build(); |
| 137 | TrafficSelector nextIdSelector = DefaultTrafficSelector.builder() |
| 138 | .matchPi(nextIdCriterion) |
| 139 | .build(); |
| 140 | PiActionParam setNextToSimpleParam = new PiActionParam(FabricConstants.ACT_PRM_NEXT_TYPE_ID, |
Yi Tseng | 1b154bd | 2017-11-20 17:48:19 -0800 | [diff] [blame] | 141 | ImmutableByteSequence.copyFrom(nextType)); |
Yi Tseng | 0b80972 | 2017-11-03 10:23:26 -0700 | [diff] [blame] | 142 | PiAction setNextToSimpleAction = PiAction.builder() |
| 143 | .withId(FabricConstants.ACT_SET_NEXT_TYPE_ID) |
| 144 | .withParameter(setNextToSimpleParam) |
| 145 | .build(); |
| 146 | TrafficTreatment setNextTypeTreatment = DefaultTrafficTreatment.builder() |
| 147 | .piTableAction(setNextToSimpleAction) |
| 148 | .build(); |
| 149 | expectedFlowRule = DefaultFlowRule.builder() |
| 150 | .forDevice(DEVICE_ID) |
| 151 | .fromApp(APP_ID) |
| 152 | .makePermanent() |
| 153 | // FIXME: currently next objective doesn't support priority, set priority to zero |
| 154 | .withPriority(0) |
| 155 | .forTable(FabricConstants.TBL_NEXT_ID_MAPPING_ID) |
| 156 | .withSelector(nextIdSelector) |
| 157 | .withTreatment(setNextTypeTreatment) |
| 158 | .build(); |
Yi Tseng | 1b154bd | 2017-11-20 17:48:19 -0800 | [diff] [blame] | 159 | assertTrue(expectedFlowRule.exactMatch(flowRule)); |
Yi Tseng | 0b80972 | 2017-11-03 10:23:26 -0700 | [diff] [blame] | 160 | } |
| 161 | |
| 162 | /** |
| 163 | * Test program ecmp output group for Hashed table. |
| 164 | */ |
| 165 | @Test |
Yi Tseng | 1b154bd | 2017-11-20 17:48:19 -0800 | [diff] [blame] | 166 | public void testHashedOutput() throws Exception { |
| 167 | TrafficTreatment treatment1 = DefaultTrafficTreatment.builder() |
| 168 | .setEthSrc(ROUTER_MAC) |
| 169 | .setEthDst(HOST_MAC) |
| 170 | .setOutput(PORT_1) |
| 171 | .build(); |
| 172 | TrafficTreatment treatment2 = DefaultTrafficTreatment.builder() |
| 173 | .setEthSrc(ROUTER_MAC) |
| 174 | .setEthDst(HOST_MAC) |
| 175 | .setOutput(PORT_2) |
| 176 | .build(); |
| 177 | |
| 178 | NextObjective nextObjective = DefaultNextObjective.builder() |
| 179 | .withId(NEXT_ID_1) |
| 180 | .withPriority(PRIORITY) |
| 181 | .addTreatment(treatment1) |
| 182 | .addTreatment(treatment2) |
| 183 | .withType(NextObjective.Type.HASHED) |
| 184 | .makePermanent() |
| 185 | .fromApp(APP_ID) |
| 186 | .add(); |
| 187 | |
| 188 | PipelinerTranslationResult result = pipeliner.pipelinerNext.next(nextObjective); |
| 189 | |
| 190 | // Should generates 2 flows and 1 group |
| 191 | List<FlowRule> flowRulesInstalled = (List<FlowRule>) result.flowRules(); |
| 192 | List<GroupDescription> groupsInstalled = (List<GroupDescription>) result.groups(); |
| 193 | assertEquals(2, flowRulesInstalled.size()); |
| 194 | assertEquals(1, groupsInstalled.size()); |
| 195 | |
| 196 | verifyNextIdMapping(flowRulesInstalled.get(0), NEXT_TYPE_HASHED); |
| 197 | |
| 198 | // Hashed table |
| 199 | PiCriterion nextIdCriterion = PiCriterion.builder() |
| 200 | .matchExact(FabricConstants.HF_FABRIC_METADATA_NEXT_ID_ID, NEXT_ID_1) |
| 201 | .build(); |
| 202 | TrafficSelector nextIdSelector = DefaultTrafficSelector.builder() |
| 203 | .matchPi(nextIdCriterion) |
| 204 | .build(); |
| 205 | PiActionGroupId actionGroupId = PiActionGroupId.of(NEXT_ID_1); |
| 206 | TrafficTreatment treatment = DefaultTrafficTreatment.builder() |
| 207 | .piTableAction(actionGroupId) |
| 208 | .build(); |
| 209 | FlowRule actualFlowRule = flowRulesInstalled.get(1); |
| 210 | FlowRule expectedFlowRule = DefaultFlowRule.builder() |
| 211 | .forDevice(DEVICE_ID) |
| 212 | .fromApp(APP_ID) |
| 213 | .makePermanent() |
| 214 | // FIXME: currently next objective doesn't support priority, ignore this |
| 215 | .withPriority(0) |
| 216 | .forTable(TBL_HASHED_ID) |
| 217 | .withSelector(nextIdSelector) |
| 218 | .withTreatment(treatment) |
| 219 | .build(); |
| 220 | assertTrue(expectedFlowRule.exactMatch(actualFlowRule)); |
| 221 | |
| 222 | // Group |
| 223 | GroupDescription actualGroup = groupsInstalled.get(0); |
| 224 | List<TrafficTreatment> treatments = ImmutableList.of(treatment1, treatment2); |
| 225 | |
| 226 | List<GroupBucket> buckets = treatments.stream() |
| 227 | .map(DefaultGroupBucket::createSelectGroupBucket) |
| 228 | .collect(Collectors.toList()); |
| 229 | GroupBuckets groupBuckets = new GroupBuckets(buckets); |
| 230 | PiGroupKey groupKey = new PiGroupKey(TBL_HASHED_ID, ACT_PRF_ECMP_SELECTOR_ID, NEXT_ID_1); |
| 231 | GroupDescription expectedGroup = new DefaultGroupDescription( |
| 232 | DEVICE_ID, |
| 233 | GroupDescription.Type.SELECT, |
| 234 | groupBuckets, |
| 235 | groupKey, |
| 236 | NEXT_ID_1, |
| 237 | APP_ID |
| 238 | ); |
| 239 | assertEquals(expectedGroup, actualGroup); |
Yi Tseng | 0b80972 | 2017-11-03 10:23:26 -0700 | [diff] [blame] | 240 | |
| 241 | } |
| 242 | |
| 243 | /** |
| 244 | * Test program output group for Broadcast table. |
| 245 | */ |
| 246 | @Test |
| 247 | @Ignore |
| 248 | public void testBroadcastOutput() { |
| 249 | |
| 250 | } |
| 251 | } |