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