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