blob: a797a8355addcade42ea5993755af255130060dd [file] [log] [blame]
Yi Tseng0b809722017-11-03 10:23:26 -07001/*
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.pipelines.fabric.pipeliner;
18
Yi Tseng1b154bd2017-11-20 17:48:19 -080019import com.google.common.collect.ImmutableList;
Yi Tseng0b809722017-11-03 10:23:26 -070020import org.junit.Ignore;
21import org.junit.Test;
Yi Tseng0b809722017-11-03 10:23:26 -070022import org.onosproject.net.flow.DefaultFlowRule;
23import org.onosproject.net.flow.DefaultTrafficSelector;
24import org.onosproject.net.flow.DefaultTrafficTreatment;
25import org.onosproject.net.flow.FlowRule;
26import org.onosproject.net.flow.TrafficSelector;
27import org.onosproject.net.flow.TrafficTreatment;
28import org.onosproject.net.flow.criteria.PiCriterion;
29import org.onosproject.net.flowobjective.DefaultNextObjective;
30import org.onosproject.net.flowobjective.NextObjective;
Yi Tseng1b154bd2017-11-20 17:48:19 -080031import org.onosproject.net.group.DefaultGroupBucket;
32import org.onosproject.net.group.DefaultGroupDescription;
33import org.onosproject.net.group.GroupBucket;
34import org.onosproject.net.group.GroupBuckets;
Yi Tseng0b809722017-11-03 10:23:26 -070035import org.onosproject.net.group.GroupDescription;
Yi Tseng1b154bd2017-11-20 17:48:19 -080036import org.onosproject.net.pi.runtime.PiActionGroupId;
Yi Tseng1b154bd2017-11-20 17:48:19 -080037import org.onosproject.net.pi.runtime.PiGroupKey;
Yi Tseng0b809722017-11-03 10:23:26 -070038import org.onosproject.pipelines.fabric.FabricConstants;
39
40import java.util.List;
Yi Tseng1b154bd2017-11-20 17:48:19 -080041import java.util.stream.Collectors;
Yi Tseng0b809722017-11-03 10:23:26 -070042
43import static org.junit.Assert.assertEquals;
44import static org.junit.Assert.assertTrue;
Yi Tseng27b9bc02018-04-12 14:52:40 +080045import static org.onosproject.pipelines.fabric.FabricConstants.ACT_PRF_FABRICINGRESS_NEXT_ECMP_SELECTOR_ID;
Yi Tseng1b154bd2017-11-20 17:48:19 -080046import static org.onosproject.pipelines.fabric.FabricConstants.TBL_HASHED_ID;
Yi Tseng20f9e7b2018-05-24 23:27:39 +080047import static org.onosproject.pipelines.fabric.FabricConstants.TBL_VLAN_META_ID;
Yi Tseng0b809722017-11-03 10:23:26 -070048
49/**
50 * Test cases for fabric.p4 pipeline next control block.
51 */
52public class FabricNextPipelinerTest extends FabricPipelinerTest {
Yi Tseng20f9e7b2018-05-24 23:27:39 +080053 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 Tseng0b809722017-11-03 10:23:26 -070077
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 Tsengdbe05602017-11-17 18:02:43 -0800101 /**
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 Tseng20f9e7b2018-05-24 23:27:39 +0800114 /**
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 Tseng0b809722017-11-03 10:23:26 -0700128 private void testSimple(TrafficTreatment treatment) {
129 NextObjective nextObjective = DefaultNextObjective.builder()
130 .withId(NEXT_ID_1)
131 .withPriority(PRIORITY)
Yi Tseng20f9e7b2018-05-24 23:27:39 +0800132 .withMeta(VLAN_META)
Yi Tseng0b809722017-11-03 10:23:26 -0700133 .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 Tseng20f9e7b2018-05-24 23:27:39 +0800143 assertEquals(2, flowRulesInstalled.size());
Yi Tseng0b809722017-11-03 10:23:26 -0700144 assertTrue(groupsInstalled.isEmpty());
145
Yi Tseng1b154bd2017-11-20 17:48:19 -0800146 // Simple table
Yi Tseng0b809722017-11-03 10:23:26 -0700147 PiCriterion nextIdCriterion = PiCriterion.builder()
Yi Tseng1b154bd2017-11-20 17:48:19 -0800148 .matchExact(FabricConstants.HF_FABRIC_METADATA_NEXT_ID_ID, NEXT_ID_1)
149 .build();
150 TrafficSelector nextIdSelector = DefaultTrafficSelector.builder()
151 .matchPi(nextIdCriterion)
152 .build();
Yi Tseng20f9e7b2018-05-24 23:27:39 +0800153
154 // VLAN meta table
Yi Tsengf55eaa82017-11-29 15:51:28 -0800155 FlowRule actualFlowRule = flowRulesInstalled.get(0);
Yi Tseng20f9e7b2018-05-24 23:27:39 +0800156 assertTrue(actualFlowRule.exactMatch(vlanMetaFlowRule));
157
158 actualFlowRule = flowRulesInstalled.get(1);
Yi Tseng1b154bd2017-11-20 17:48:19 -0800159 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 Tseng0b809722017-11-03 10:23:26 -0700172 /**
173 * Test program ecmp output group for Hashed table.
174 */
175 @Test
Yi Tseng1b154bd2017-11-20 17:48:19 -0800176 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 Tseng20f9e7b2018-05-24 23:27:39 +0800191 .withMeta(VLAN_META)
Yi Tseng1b154bd2017-11-20 17:48:19 -0800192 .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 Tseng20f9e7b2018-05-24 23:27:39 +0800204 assertEquals(2, flowRulesInstalled.size());
Yi Tseng1b154bd2017-11-20 17:48:19 -0800205 assertEquals(1, groupsInstalled.size());
206
Yi Tseng1b154bd2017-11-20 17:48:19 -0800207 // 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 Tseng20f9e7b2018-05-24 23:27:39 +0800218
219 // VLAN meta table
Yi Tsengf55eaa82017-11-29 15:51:28 -0800220 FlowRule actualFlowRule = flowRulesInstalled.get(0);
Yi Tseng20f9e7b2018-05-24 23:27:39 +0800221 assertTrue(actualFlowRule.exactMatch(vlanMetaFlowRule));
222
223 actualFlowRule = flowRulesInstalled.get(1);
Yi Tseng1b154bd2017-11-20 17:48:19 -0800224 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 Tseng27b9bc02018-04-12 14:52:40 +0800244 PiGroupKey groupKey = new PiGroupKey(TBL_HASHED_ID, ACT_PRF_FABRICINGRESS_NEXT_ECMP_SELECTOR_ID, NEXT_ID_1);
Yi Tseng1b154bd2017-11-20 17:48:19 -0800245 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 Tseng0b809722017-11-03 10:23:26 -0700254
255 }
256
257 /**
258 * Test program output group for Broadcast table.
259 */
260 @Test
261 @Ignore
262 public void testBroadcastOutput() {
263
264 }
265}