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