Carmelo Cascone | 87b9b39 | 2017-10-02 18:33:20 +0200 | [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.net.pi.impl; |
| 18 | |
| 19 | import com.google.common.collect.ImmutableList; |
| 20 | import com.google.common.collect.ImmutableSet; |
| 21 | import com.google.common.testing.EqualsTester; |
| 22 | import org.junit.Before; |
| 23 | import org.junit.Test; |
| 24 | import org.onlab.packet.MacAddress; |
| 25 | import org.onlab.util.ImmutableByteSequence; |
| 26 | import org.onosproject.TestApplicationId; |
Carmelo Cascone | 87b9b39 | 2017-10-02 18:33:20 +0200 | [diff] [blame] | 27 | import org.onosproject.core.ApplicationId; |
| 28 | import org.onosproject.core.DefaultApplicationId; |
| 29 | import org.onosproject.core.GroupId; |
| 30 | import org.onosproject.net.DeviceId; |
| 31 | import org.onosproject.net.PortNumber; |
| 32 | import org.onosproject.net.flow.DefaultFlowRule; |
| 33 | import org.onosproject.net.flow.DefaultTrafficSelector; |
| 34 | import org.onosproject.net.flow.DefaultTrafficTreatment; |
| 35 | import org.onosproject.net.flow.FlowRule; |
| 36 | import org.onosproject.net.flow.TrafficSelector; |
| 37 | import org.onosproject.net.flow.TrafficTreatment; |
| 38 | import org.onosproject.net.flow.instructions.Instructions; |
| 39 | import org.onosproject.net.group.DefaultGroup; |
| 40 | import org.onosproject.net.group.DefaultGroupBucket; |
| 41 | import org.onosproject.net.group.DefaultGroupDescription; |
| 42 | import org.onosproject.net.group.Group; |
| 43 | import org.onosproject.net.group.GroupBucket; |
| 44 | import org.onosproject.net.group.GroupBuckets; |
| 45 | import org.onosproject.net.group.GroupDescription; |
Carmelo Cascone | 87b9b39 | 2017-10-02 18:33:20 +0200 | [diff] [blame] | 46 | import org.onosproject.net.pi.model.PiPipeconf; |
Carmelo Cascone | 87b9b39 | 2017-10-02 18:33:20 +0200 | [diff] [blame] | 47 | import org.onosproject.net.pi.runtime.PiAction; |
| 48 | import org.onosproject.net.pi.runtime.PiActionGroup; |
| 49 | import org.onosproject.net.pi.runtime.PiActionGroupMember; |
| 50 | import org.onosproject.net.pi.runtime.PiActionGroupMemberId; |
Carmelo Cascone | 87b9b39 | 2017-10-02 18:33:20 +0200 | [diff] [blame] | 51 | import org.onosproject.net.pi.runtime.PiActionParam; |
Carmelo Cascone | 87b9b39 | 2017-10-02 18:33:20 +0200 | [diff] [blame] | 52 | import org.onosproject.net.pi.runtime.PiGroupKey; |
| 53 | import org.onosproject.net.pi.runtime.PiTableAction; |
| 54 | import org.onosproject.net.pi.runtime.PiTableEntry; |
Carmelo Cascone | 87b9b39 | 2017-10-02 18:33:20 +0200 | [diff] [blame] | 55 | import org.onosproject.net.pi.runtime.PiTernaryFieldMatch; |
Carmelo Cascone | ca94bcf | 2017-10-27 14:16:59 -0700 | [diff] [blame] | 56 | import org.onosproject.pipelines.basic.PipeconfLoader; |
Carmelo Cascone | 87b9b39 | 2017-10-02 18:33:20 +0200 | [diff] [blame] | 57 | |
| 58 | import java.util.Collection; |
| 59 | import java.util.List; |
| 60 | import java.util.Optional; |
| 61 | import java.util.Random; |
| 62 | |
| 63 | import static org.hamcrest.CoreMatchers.equalTo; |
| 64 | import static org.hamcrest.CoreMatchers.is; |
| 65 | import static org.hamcrest.MatcherAssert.assertThat; |
| 66 | import static org.onlab.util.ImmutableByteSequence.copyFrom; |
| 67 | import static org.onlab.util.ImmutableByteSequence.fit; |
| 68 | import static org.onosproject.net.group.GroupDescription.Type.SELECT; |
Carmelo Cascone | 87b9b39 | 2017-10-02 18:33:20 +0200 | [diff] [blame] | 69 | import static org.onosproject.net.pi.impl.PiFlowRuleTranslator.MAX_PI_PRIORITY; |
Carmelo Cascone | ca94bcf | 2017-10-27 14:16:59 -0700 | [diff] [blame] | 70 | import static org.onosproject.pipelines.basic.BasicConstants.ACT_PRF_WCMP_SELECTOR_ID; |
| 71 | import static org.onosproject.pipelines.basic.BasicConstants.ACT_PRM_PORT_ID; |
| 72 | import static org.onosproject.pipelines.basic.BasicConstants.ACT_SET_EGRESS_PORT_ID; |
| 73 | import static org.onosproject.pipelines.basic.BasicConstants.HDR_ETH_DST_ID; |
| 74 | import static org.onosproject.pipelines.basic.BasicConstants.HDR_ETH_SRC_ID; |
| 75 | import static org.onosproject.pipelines.basic.BasicConstants.HDR_ETH_TYPE_ID; |
| 76 | import static org.onosproject.pipelines.basic.BasicConstants.HDR_IN_PORT_ID; |
| 77 | import static org.onosproject.pipelines.basic.BasicConstants.PORT_BITWIDTH; |
| 78 | import static org.onosproject.pipelines.basic.BasicConstants.TBL_TABLE0_ID; |
| 79 | import static org.onosproject.pipelines.basic.BasicConstants.TBL_WCMP_TABLE_ID; |
Carmelo Cascone | 87b9b39 | 2017-10-02 18:33:20 +0200 | [diff] [blame] | 80 | |
| 81 | /** |
| 82 | * Tests for {@link PiFlowRuleTranslator}. |
| 83 | */ |
| 84 | @SuppressWarnings("ConstantConditions") |
| 85 | public class PiTranslatorServiceTest { |
| 86 | |
Carmelo Cascone | 87b9b39 | 2017-10-02 18:33:20 +0200 | [diff] [blame] | 87 | private static final short IN_PORT_MASK = 0x01ff; // 9-bit mask |
| 88 | private static final short ETH_TYPE_MASK = (short) 0xffff; |
| 89 | private static final DeviceId DEVICE_ID = DeviceId.deviceId("device:dummy:1"); |
| 90 | private static final ApplicationId APP_ID = TestApplicationId.create("dummy"); |
Carmelo Cascone | 87b9b39 | 2017-10-02 18:33:20 +0200 | [diff] [blame] | 91 | private static final GroupId GROUP_ID = GroupId.valueOf(1); |
Carmelo Cascone | 87b9b39 | 2017-10-02 18:33:20 +0200 | [diff] [blame] | 92 | private static final List<GroupBucket> BUCKET_LIST = ImmutableList.of(outputBucket(1), |
| 93 | outputBucket(2), |
| 94 | outputBucket(3) |
| 95 | ); |
Carmelo Cascone | ca94bcf | 2017-10-27 14:16:59 -0700 | [diff] [blame] | 96 | private static final PiGroupKey GROUP_KEY = new PiGroupKey(TBL_WCMP_TABLE_ID, ACT_PRF_WCMP_SELECTOR_ID, |
| 97 | GROUP_ID.id()); |
Carmelo Cascone | 87b9b39 | 2017-10-02 18:33:20 +0200 | [diff] [blame] | 98 | private static final GroupBuckets BUCKETS = new GroupBuckets(BUCKET_LIST); |
| 99 | private static final GroupDescription GROUP_DESC = |
| 100 | new DefaultGroupDescription(DEVICE_ID, SELECT, BUCKETS, GROUP_KEY, GROUP_ID.id(), APP_ID); |
| 101 | private static final Group GROUP = new DefaultGroup(GROUP_ID, GROUP_DESC); |
| 102 | private static final int DEFAULT_MEMBER_WEIGHT = 1; |
| 103 | private static final int BASE_MEM_ID = 65535; |
| 104 | private Collection<PiActionGroupMember> expectedMembers; |
| 105 | |
| 106 | private Random random = new Random(); |
| 107 | private PiPipeconf pipeconf; |
| 108 | |
| 109 | @Before |
| 110 | public void setUp() throws Exception { |
Carmelo Cascone | ca94bcf | 2017-10-27 14:16:59 -0700 | [diff] [blame] | 111 | pipeconf = PipeconfLoader.BASIC_PIPECONF; |
Carmelo Cascone | 87b9b39 | 2017-10-02 18:33:20 +0200 | [diff] [blame] | 112 | expectedMembers = ImmutableSet.of(outputMember(1), |
| 113 | outputMember(2), |
| 114 | outputMember(3)); |
| 115 | } |
| 116 | |
| 117 | @Test |
| 118 | public void testTranslateFlowRules() throws Exception { |
| 119 | |
| 120 | ApplicationId appId = new DefaultApplicationId(1, "test"); |
| 121 | int tableId = 0; |
| 122 | MacAddress ethDstMac = MacAddress.valueOf(random.nextLong()); |
| 123 | MacAddress ethSrcMac = MacAddress.valueOf(random.nextLong()); |
| 124 | short ethType = (short) (0x0000FFFF & random.nextInt()); |
| 125 | short outPort = (short) random.nextInt(65); |
| 126 | short inPort = (short) random.nextInt(65); |
| 127 | int timeout = random.nextInt(100); |
| 128 | int priority = random.nextInt(100); |
| 129 | |
| 130 | TrafficSelector matchInPort1 = DefaultTrafficSelector |
| 131 | .builder() |
| 132 | .matchInPort(PortNumber.portNumber(inPort)) |
| 133 | .matchEthDst(ethDstMac) |
| 134 | .matchEthSrc(ethSrcMac) |
| 135 | .matchEthType(ethType) |
| 136 | .build(); |
| 137 | |
| 138 | TrafficTreatment outPort2 = DefaultTrafficTreatment |
| 139 | .builder() |
| 140 | .setOutput(PortNumber.portNumber(outPort)) |
| 141 | .build(); |
| 142 | |
| 143 | FlowRule rule1 = DefaultFlowRule.builder() |
| 144 | .forDevice(DEVICE_ID) |
| 145 | .forTable(tableId) |
| 146 | .fromApp(appId) |
| 147 | .withSelector(matchInPort1) |
| 148 | .withTreatment(outPort2) |
| 149 | .makeTemporary(timeout) |
| 150 | .withPriority(priority) |
| 151 | .build(); |
| 152 | |
| 153 | FlowRule rule2 = DefaultFlowRule.builder() |
| 154 | .forDevice(DEVICE_ID) |
| 155 | .forTable(tableId) |
| 156 | .fromApp(appId) |
| 157 | .withSelector(matchInPort1) |
| 158 | .withTreatment(outPort2) |
| 159 | .makeTemporary(timeout) |
| 160 | .withPriority(priority) |
| 161 | .build(); |
| 162 | |
| 163 | PiTableEntry entry1 = PiFlowRuleTranslator.translate(rule1, pipeconf, null); |
| 164 | PiTableEntry entry2 = PiFlowRuleTranslator.translate(rule1, pipeconf, null); |
| 165 | |
| 166 | // check equality, i.e. same rules must produce same entries |
| 167 | new EqualsTester() |
| 168 | .addEqualityGroup(rule1, rule2) |
| 169 | .addEqualityGroup(entry1, entry2) |
| 170 | .testEquals(); |
| 171 | |
Carmelo Cascone | ca94bcf | 2017-10-27 14:16:59 -0700 | [diff] [blame] | 172 | int numMatchParams = pipeconf.pipelineModel().table(TBL_TABLE0_ID.id()).get().matchFields().size(); |
Carmelo Cascone | 87b9b39 | 2017-10-02 18:33:20 +0200 | [diff] [blame] | 173 | // parse values stored in entry1 |
Carmelo Cascone | ca94bcf | 2017-10-27 14:16:59 -0700 | [diff] [blame] | 174 | PiTernaryFieldMatch inPortParam = (PiTernaryFieldMatch) entry1.matchKey().fieldMatch(HDR_IN_PORT_ID).get(); |
| 175 | PiTernaryFieldMatch ethDstParam = (PiTernaryFieldMatch) entry1.matchKey().fieldMatch(HDR_ETH_DST_ID).get(); |
| 176 | PiTernaryFieldMatch ethSrcParam = (PiTernaryFieldMatch) entry1.matchKey().fieldMatch(HDR_ETH_SRC_ID).get(); |
| 177 | PiTernaryFieldMatch ethTypeParam = (PiTernaryFieldMatch) entry1.matchKey().fieldMatch(HDR_ETH_TYPE_ID).get(); |
| 178 | Optional<Double> expectedTimeout = pipeconf.pipelineModel().table(TBL_TABLE0_ID.id()).get().supportsAging() |
Carmelo Cascone | 87b9b39 | 2017-10-02 18:33:20 +0200 | [diff] [blame] | 179 | ? Optional.of((double) rule1.timeout()) : Optional.empty(); |
| 180 | |
| 181 | // check that the number of parameters in the entry is the same as the number of table keys |
| 182 | assertThat("Incorrect number of match parameters", |
| 183 | entry1.matchKey().fieldMatches().size(), is(equalTo(numMatchParams))); |
| 184 | |
| 185 | // check that values stored in entry are the same used for the flow rule |
| 186 | assertThat("Incorrect inPort match param value", |
| 187 | inPortParam.value().asReadOnlyBuffer().getShort(), is(equalTo(inPort))); |
| 188 | assertThat("Incorrect inPort match param mask", |
| 189 | inPortParam.mask().asReadOnlyBuffer().getShort(), is(equalTo(IN_PORT_MASK))); |
| 190 | assertThat("Incorrect ethDestMac match param value", |
| 191 | ethDstParam.value().asArray(), is(equalTo(ethDstMac.toBytes()))); |
| 192 | assertThat("Incorrect ethDestMac match param mask", |
| 193 | ethDstParam.mask().asArray(), is(equalTo(MacAddress.BROADCAST.toBytes()))); |
| 194 | assertThat("Incorrect ethSrcMac match param value", |
| 195 | ethSrcParam.value().asArray(), is(equalTo(ethSrcMac.toBytes()))); |
| 196 | assertThat("Incorrect ethSrcMac match param mask", |
| 197 | ethSrcParam.mask().asArray(), is(equalTo(MacAddress.BROADCAST.toBytes()))); |
| 198 | assertThat("Incorrect ethType match param value", |
| 199 | ethTypeParam.value().asReadOnlyBuffer().getShort(), is(equalTo(ethType))); |
| 200 | assertThat("Incorrect ethType match param mask", |
| 201 | ethTypeParam.mask().asReadOnlyBuffer().getShort(), is(equalTo(ETH_TYPE_MASK))); |
| 202 | assertThat("Incorrect priority value", |
| 203 | entry1.priority().get(), is(equalTo(MAX_PI_PRIORITY - rule1.priority()))); |
| 204 | assertThat("Incorrect timeout value", |
| 205 | entry1.timeout(), is(equalTo(expectedTimeout))); |
| 206 | |
| 207 | } |
| 208 | |
| 209 | private static GroupBucket outputBucket(int portNum) { |
| 210 | ImmutableByteSequence paramVal = copyFrom(portNum); |
Carmelo Cascone | ca94bcf | 2017-10-27 14:16:59 -0700 | [diff] [blame] | 211 | PiActionParam param = new PiActionParam(ACT_PRM_PORT_ID, paramVal); |
| 212 | PiTableAction action = PiAction.builder().withId(ACT_SET_EGRESS_PORT_ID).withParameter(param).build(); |
Carmelo Cascone | 87b9b39 | 2017-10-02 18:33:20 +0200 | [diff] [blame] | 213 | TrafficTreatment treatment = DefaultTrafficTreatment.builder() |
| 214 | .add(Instructions.piTableAction(action)) |
| 215 | .build(); |
| 216 | return DefaultGroupBucket.createSelectGroupBucket(treatment); |
| 217 | } |
| 218 | |
| 219 | private static PiActionGroupMember outputMember(int portNum) |
| 220 | throws ImmutableByteSequence.ByteSequenceTrimException { |
Carmelo Cascone | ca94bcf | 2017-10-27 14:16:59 -0700 | [diff] [blame] | 221 | PiActionParam param = new PiActionParam(ACT_PRM_PORT_ID, fit(copyFrom(portNum), PORT_BITWIDTH)); |
Carmelo Cascone | 87b9b39 | 2017-10-02 18:33:20 +0200 | [diff] [blame] | 222 | PiAction piAction = PiAction.builder() |
Carmelo Cascone | ca94bcf | 2017-10-27 14:16:59 -0700 | [diff] [blame] | 223 | .withId(ACT_SET_EGRESS_PORT_ID) |
Carmelo Cascone | 87b9b39 | 2017-10-02 18:33:20 +0200 | [diff] [blame] | 224 | .withParameter(param).build(); |
| 225 | return PiActionGroupMember.builder() |
| 226 | .withAction(piAction) |
| 227 | .withId(PiActionGroupMemberId.of(BASE_MEM_ID + portNum)) |
| 228 | .withWeight(DEFAULT_MEMBER_WEIGHT) |
| 229 | .build(); |
| 230 | } |
| 231 | |
| 232 | /** |
| 233 | * Test add group with buckets. |
| 234 | */ |
| 235 | @Test |
| 236 | public void testTranslateGroups() throws Exception { |
| 237 | |
| 238 | PiActionGroup piGroup1 = PiGroupTranslator.translate(GROUP, pipeconf, null); |
| 239 | PiActionGroup piGroup2 = PiGroupTranslator.translate(GROUP, pipeconf, null); |
| 240 | |
| 241 | new EqualsTester() |
| 242 | .addEqualityGroup(piGroup1, piGroup2) |
| 243 | .testEquals(); |
| 244 | |
| 245 | assertThat("Group ID must be equal", |
| 246 | piGroup1.id().id(), is(equalTo(GROUP_ID.id()))); |
| 247 | assertThat("Group type must be SELECT", |
| 248 | piGroup1.type(), is(equalTo(PiActionGroup.Type.SELECT))); |
| 249 | assertThat("Action profile ID must be equal", |
Carmelo Cascone | ca94bcf | 2017-10-27 14:16:59 -0700 | [diff] [blame] | 250 | piGroup1.actionProfileId(), is(equalTo(ACT_PRF_WCMP_SELECTOR_ID))); |
Carmelo Cascone | 87b9b39 | 2017-10-02 18:33:20 +0200 | [diff] [blame] | 251 | |
| 252 | // members installed |
| 253 | Collection<PiActionGroupMember> members = piGroup1.members(); |
| 254 | assertThat("The number of group members must be equal", |
| 255 | piGroup1.members().size(), is(expectedMembers.size())); |
| 256 | assertThat("Group members must be equal", |
| 257 | members.containsAll(expectedMembers) && expectedMembers.containsAll(members)); |
| 258 | } |
| 259 | } |