blob: e5207ebfd40b74650840a88ab67b76239080b8ba [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 Tseng20f9e7b2018-05-24 23:27:39 +080019import org.onlab.packet.VlanId;
Yi Tseng0b809722017-11-03 10:23:26 -070020import org.onosproject.net.DeviceId;
Yi Tseng20f9e7b2018-05-24 23:27:39 +080021import org.onosproject.net.PortNumber;
Yi Tseng4fd28432018-02-01 14:48:03 -080022import org.onosproject.net.driver.Driver;
Yi Tseng0b809722017-11-03 10:23:26 -070023import org.onosproject.net.flow.DefaultFlowRule;
24import org.onosproject.net.flow.DefaultTrafficSelector;
25import org.onosproject.net.flow.DefaultTrafficTreatment;
Yi Tseng0b809722017-11-03 10:23:26 -070026import org.onosproject.net.flow.TrafficSelector;
27import org.onosproject.net.flow.TrafficTreatment;
Yi Tseng20f9e7b2018-05-24 23:27:39 +080028import org.onosproject.net.flow.criteria.Criterion;
Yi Tseng0b809722017-11-03 10:23:26 -070029import org.onosproject.net.flow.criteria.PiCriterion;
Yi Tseng20f9e7b2018-05-24 23:27:39 +080030import org.onosproject.net.flow.criteria.VlanIdCriterion;
Yi Tseng0b809722017-11-03 10:23:26 -070031import org.onosproject.net.flow.instructions.Instruction;
Yi Tseng20f9e7b2018-05-24 23:27:39 +080032import org.onosproject.net.flow.instructions.L2ModificationInstruction;
Yi Tseng4fd28432018-02-01 14:48:03 -080033import org.onosproject.net.flowobjective.DefaultNextObjective;
Yi Tseng0b809722017-11-03 10:23:26 -070034import org.onosproject.net.flowobjective.NextObjective;
Yi Tseng6e9b6f52018-02-27 10:40:51 +010035import org.onosproject.net.flowobjective.Objective;
Yi Tseng0b809722017-11-03 10:23:26 -070036import org.onosproject.net.flowobjective.ObjectiveError;
Yi Tseng1b154bd2017-11-20 17:48:19 -080037import org.onosproject.net.group.DefaultGroupBucket;
38import org.onosproject.net.group.DefaultGroupDescription;
Esin Karaman24fda8a2018-01-26 11:52:28 +000039import org.onosproject.net.group.DefaultGroupKey;
Yi Tseng1b154bd2017-11-20 17:48:19 -080040import org.onosproject.net.group.GroupBucket;
41import org.onosproject.net.group.GroupBuckets;
42import org.onosproject.net.group.GroupDescription;
Esin Karaman24fda8a2018-01-26 11:52:28 +000043import org.onosproject.net.group.GroupKey;
44import org.onosproject.net.pi.runtime.PiAction;
Yi Tseng1b154bd2017-11-20 17:48:19 -080045import org.onosproject.net.pi.runtime.PiActionGroupId;
Esin Karaman24fda8a2018-01-26 11:52:28 +000046import org.onosproject.net.pi.runtime.PiActionParam;
Yi Tseng1b154bd2017-11-20 17:48:19 -080047import org.onosproject.net.pi.runtime.PiGroupKey;
Yi Tseng43ee7e82018-04-12 16:37:34 +080048import org.onosproject.pipelines.fabric.FabricConstants;
Carmelo Cascone1e8843f2018-07-19 19:01:12 +020049import org.onosproject.pipelines.fabric.FabricUtils;
Yi Tseng0b809722017-11-03 10:23:26 -070050import org.slf4j.Logger;
51
Yi Tseng1b154bd2017-11-20 17:48:19 -080052import java.util.List;
Carmelo Cascone58136812018-07-19 03:40:16 +020053import java.util.Optional;
Yi Tseng1b154bd2017-11-20 17:48:19 -080054import java.util.stream.Collectors;
Yi Tseng0b809722017-11-03 10:23:26 -070055
Carmelo Cascone1e8843f2018-07-19 19:01:12 +020056import static org.onosproject.pipelines.fabric.FabricUtils.getOutputPort;
Yi Tseng0b809722017-11-03 10:23:26 -070057import static org.slf4j.LoggerFactory.getLogger;
58
59/**
60 * Handling next objective for fabric pipeliner.
61 */
62public class FabricNextPipeliner {
63 private static final Logger log = getLogger(FabricNextPipeliner.class);
Yi Tseng4fd28432018-02-01 14:48:03 -080064 private static final String NO_HASHED_TABLE = "noHashedTable";
Yi Tseng0b809722017-11-03 10:23:26 -070065
Yi Tseng0b809722017-11-03 10:23:26 -070066 protected DeviceId deviceId;
Yi Tseng4fd28432018-02-01 14:48:03 -080067 protected Driver driver;
Yi Tseng0b809722017-11-03 10:23:26 -070068
Yi Tseng4fd28432018-02-01 14:48:03 -080069 public FabricNextPipeliner(DeviceId deviceId, Driver driver) {
Yi Tseng0b809722017-11-03 10:23:26 -070070 this.deviceId = deviceId;
Yi Tseng4fd28432018-02-01 14:48:03 -080071 this.driver = driver;
Yi Tseng0b809722017-11-03 10:23:26 -070072 }
73
74 public PipelinerTranslationResult next(NextObjective nextObjective) {
75 PipelinerTranslationResult.Builder resultBuilder = PipelinerTranslationResult.builder();
Yi Tseng1b154bd2017-11-20 17:48:19 -080076
Yi Tseng20f9e7b2018-05-24 23:27:39 +080077 processNextVlanMeta(nextObjective, resultBuilder);
78
Yi Tseng0b809722017-11-03 10:23:26 -070079 switch (nextObjective.type()) {
80 case SIMPLE:
Yi Tseng1b154bd2017-11-20 17:48:19 -080081 processSimpleNext(nextObjective, resultBuilder);
82 break;
83 case HASHED:
84 processHashedNext(nextObjective, resultBuilder);
Yi Tseng0b809722017-11-03 10:23:26 -070085 break;
Esin Karaman24fda8a2018-01-26 11:52:28 +000086 case BROADCAST:
87 processBroadcastNext(nextObjective, resultBuilder);
88 break;
Yi Tseng0b809722017-11-03 10:23:26 -070089 default:
90 log.warn("Unsupported next type {}", nextObjective);
91 resultBuilder.setError(ObjectiveError.UNSUPPORTED);
92 break;
93 }
94
Yi Tseng0b809722017-11-03 10:23:26 -070095 return resultBuilder.build();
96 }
97
Yi Tseng20f9e7b2018-05-24 23:27:39 +080098 private void processNextVlanMeta(NextObjective next,
99 PipelinerTranslationResult.Builder resultBuilder) {
100 TrafficSelector meta = next.meta();
101 if (meta == null) {
102 // do nothing if there is no metadata in the next objective.
103 return;
104 }
105 VlanIdCriterion vlanIdCriterion =
106 (VlanIdCriterion) meta.getCriterion(Criterion.Type.VLAN_VID);
107
108 if (vlanIdCriterion == null) {
109 // do nothing if we can't find vlan from next objective metadata.
110 return;
111 }
112
113 VlanId vlanId = vlanIdCriterion.vlanId();
114 TrafficSelector selector = buildNextIdSelector(next.id());
115 TrafficTreatment treatment = DefaultTrafficTreatment.builder()
116 .setVlanId(vlanId)
117 .build();
118
119 resultBuilder.addFlowRule(DefaultFlowRule.builder()
120 .withSelector(selector)
121 .withTreatment(treatment)
Yi Tseng43ee7e82018-04-12 16:37:34 +0800122 .forTable(FabricConstants.FABRIC_INGRESS_NEXT_VLAN_META)
Yi Tseng20f9e7b2018-05-24 23:27:39 +0800123 .makePermanent()
124 .withPriority(next.priority())
125 .forDevice(deviceId)
126 .fromApp(next.appId())
127 .build());
128 }
129
Yi Tseng1b154bd2017-11-20 17:48:19 -0800130 private void processSimpleNext(NextObjective next,
131 PipelinerTranslationResult.Builder resultBuilder) {
132
Yi Tseng0b809722017-11-03 10:23:26 -0700133 if (next.next().size() > 1) {
134 log.warn("Only one treatment in simple next objective");
Yi Tseng1b154bd2017-11-20 17:48:19 -0800135 resultBuilder.setError(ObjectiveError.BADPARAMS);
136 return;
Yi Tseng0b809722017-11-03 10:23:26 -0700137 }
138
139 TrafficSelector selector = buildNextIdSelector(next.id());
140 TrafficTreatment treatment = next.next().iterator().next();
Yi Tseng20f9e7b2018-05-24 23:27:39 +0800141 PortNumber outputPort = getOutputPort(treatment);
Yi Tseng0b809722017-11-03 10:23:26 -0700142
Yi Tseng20f9e7b2018-05-24 23:27:39 +0800143 if (outputPort == null) {
Yi Tseng0b809722017-11-03 10:23:26 -0700144 log.warn("At least one output instruction in simple next objective");
Yi Tseng1b154bd2017-11-20 17:48:19 -0800145 resultBuilder.setError(ObjectiveError.BADPARAMS);
146 return;
Yi Tseng0b809722017-11-03 10:23:26 -0700147 }
Yi Tseng20f9e7b2018-05-24 23:27:39 +0800148
Yi Tseng1b154bd2017-11-20 17:48:19 -0800149 resultBuilder.addFlowRule(DefaultFlowRule.builder()
150 .withSelector(selector)
151 .withTreatment(treatment)
Yi Tseng43ee7e82018-04-12 16:37:34 +0800152 .forTable(FabricConstants.FABRIC_INGRESS_NEXT_SIMPLE)
Yi Tseng1b154bd2017-11-20 17:48:19 -0800153 .makePermanent()
154 .withPriority(next.priority())
155 .forDevice(deviceId)
156 .fromApp(next.appId())
157 .build());
Yi Tseng20f9e7b2018-05-24 23:27:39 +0800158
159 if (includesPopVlanInst(treatment)) {
160 processVlanPopRule(outputPort, next, resultBuilder);
161 }
Yi Tseng1b154bd2017-11-20 17:48:19 -0800162 }
163
Yi Tseng20f9e7b2018-05-24 23:27:39 +0800164 private boolean includesPopVlanInst(TrafficTreatment treatment) {
165 return treatment.allInstructions()
166 .stream()
167 .filter(inst -> inst.type() == Instruction.Type.L2MODIFICATION)
168 .map(inst -> (L2ModificationInstruction) inst)
169 .anyMatch(inst -> inst.subtype() == L2ModificationInstruction.L2SubType.VLAN_POP);
170 }
171
172 private void processVlanPopRule(PortNumber port, NextObjective next,
173 PipelinerTranslationResult.Builder resultBuilder) {
174 TrafficSelector meta = next.meta();
175 VlanIdCriterion vlanIdCriterion =
176 (VlanIdCriterion) meta.getCriterion(Criterion.Type.VLAN_VID);
177 VlanId vlanId = vlanIdCriterion.vlanId();
178
179 PiCriterion egressVlanTableMatch = PiCriterion.builder()
Yi Tseng43ee7e82018-04-12 16:37:34 +0800180 .matchExact(FabricConstants.STANDARD_METADATA_EGRESS_PORT,
Yi Tseng20f9e7b2018-05-24 23:27:39 +0800181 (short) port.toLong())
182 .build();
183 // Add VLAN pop rule to egress pipeline table
184 TrafficSelector selector = DefaultTrafficSelector.builder()
185 .matchPi(egressVlanTableMatch)
186 .matchVlanId(vlanId)
187 .build();
188 TrafficTreatment treatment = DefaultTrafficTreatment.builder()
189 .popVlan()
190 .build();
191 resultBuilder.addFlowRule(DefaultFlowRule.builder()
192 .withSelector(selector)
193 .withTreatment(treatment)
Yi Tseng43ee7e82018-04-12 16:37:34 +0800194 .forTable(FabricConstants.FABRIC_EGRESS_EGRESS_NEXT_EGRESS_VLAN)
Yi Tseng20f9e7b2018-05-24 23:27:39 +0800195 .makePermanent()
196 .withPriority(next.priority())
197 .forDevice(deviceId)
198 .fromApp(next.appId())
199 .build());
200 }
201
202 private void processHashedNext(NextObjective next, PipelinerTranslationResult.Builder resultBuilder) {
Yi Tseng4fd28432018-02-01 14:48:03 -0800203 boolean noHashedTable = Boolean.parseBoolean(driver.getProperty(NO_HASHED_TABLE));
204
205 if (noHashedTable) {
Yi Tseng20f9e7b2018-05-24 23:27:39 +0800206 if (next.next().isEmpty()) {
Yi Tseng4fd28432018-02-01 14:48:03 -0800207 return;
208 }
209 // use first action if not support hashed group
Yi Tseng20f9e7b2018-05-24 23:27:39 +0800210 TrafficTreatment treatment = next.next().iterator().next();
Yi Tseng4fd28432018-02-01 14:48:03 -0800211
212 NextObjective.Builder simpleNext = DefaultNextObjective.builder()
213 .addTreatment(treatment)
Yi Tseng20f9e7b2018-05-24 23:27:39 +0800214 .withId(next.id())
215 .fromApp(next.appId())
Yi Tseng4fd28432018-02-01 14:48:03 -0800216 .makePermanent()
Yi Tseng20f9e7b2018-05-24 23:27:39 +0800217 .withMeta(next.meta())
218 .withPriority(next.priority())
Yi Tseng4fd28432018-02-01 14:48:03 -0800219 .withType(NextObjective.Type.SIMPLE);
220
Yi Tseng20f9e7b2018-05-24 23:27:39 +0800221 if (next.context().isPresent()) {
222 processSimpleNext(simpleNext.add(next.context().get()), resultBuilder);
Yi Tseng4fd28432018-02-01 14:48:03 -0800223 } else {
224 processSimpleNext(simpleNext.add(), resultBuilder);
225 }
226 return;
227 }
228
Yi Tseng1b154bd2017-11-20 17:48:19 -0800229 // create hash groups
Yi Tseng20f9e7b2018-05-24 23:27:39 +0800230 int groupId = next.id();
231 List<GroupBucket> bucketList = next.next().stream()
Yi Tseng1b154bd2017-11-20 17:48:19 -0800232 .map(DefaultGroupBucket::createSelectGroupBucket)
233 .collect(Collectors.toList());
234
Yi Tseng20f9e7b2018-05-24 23:27:39 +0800235 // Egress VLAN handling
236 next.next().forEach(treatment -> {
237 PortNumber outputPort = getOutputPort(treatment);
238 if (includesPopVlanInst(treatment) && outputPort != null) {
239 processVlanPopRule(outputPort, next, resultBuilder);
240 }
241 });
242
243 if (bucketList.size() != next.next().size()) {
Yi Tseng1b154bd2017-11-20 17:48:19 -0800244 // some action not converted
245 // set error
Yi Tseng20f9e7b2018-05-24 23:27:39 +0800246 log.warn("Expected bucket size {}, got {}", next.next().size(), bucketList.size());
Yi Tseng1b154bd2017-11-20 17:48:19 -0800247 resultBuilder.setError(ObjectiveError.BADPARAMS);
248 return;
249 }
250
251 GroupBuckets buckets = new GroupBuckets(bucketList);
Yi Tseng43ee7e82018-04-12 16:37:34 +0800252 PiGroupKey groupKey = new PiGroupKey(FabricConstants.FABRIC_INGRESS_NEXT_HASHED,
253 FabricConstants.FABRIC_INGRESS_NEXT_ECMP_SELECTOR,
Yi Tseng1b154bd2017-11-20 17:48:19 -0800254 groupId);
255
256 resultBuilder.addGroup(new DefaultGroupDescription(deviceId,
257 GroupDescription.Type.SELECT,
258 buckets,
259 groupKey,
260 groupId,
Yi Tseng20f9e7b2018-05-24 23:27:39 +0800261 next.appId()));
Yi Tseng1b154bd2017-11-20 17:48:19 -0800262
263 // flow
Yi Tseng6e9b6f52018-02-27 10:40:51 +0100264 // If operation is ADD_TO_EXIST or REMOVE_FROM_EXIST, means we modify
265 // group buckets only, no changes for flow rule
Yi Tseng20f9e7b2018-05-24 23:27:39 +0800266 if (next.op() == Objective.Operation.ADD_TO_EXISTING ||
267 next.op() == Objective.Operation.REMOVE_FROM_EXISTING) {
Yi Tseng6e9b6f52018-02-27 10:40:51 +0100268 return;
269 }
Yi Tseng20f9e7b2018-05-24 23:27:39 +0800270 TrafficSelector selector = buildNextIdSelector(next.id());
Yi Tseng1b154bd2017-11-20 17:48:19 -0800271 TrafficTreatment treatment = DefaultTrafficTreatment.builder()
Yi Tseng20f9e7b2018-05-24 23:27:39 +0800272 .piTableAction(PiActionGroupId.of(next.id()))
Yi Tseng0b809722017-11-03 10:23:26 -0700273 .build();
Yi Tseng1b154bd2017-11-20 17:48:19 -0800274
275 resultBuilder.addFlowRule(DefaultFlowRule.builder()
276 .withSelector(selector)
277 .withTreatment(treatment)
Yi Tseng43ee7e82018-04-12 16:37:34 +0800278 .forTable(FabricConstants.FABRIC_INGRESS_NEXT_HASHED)
Yi Tseng1b154bd2017-11-20 17:48:19 -0800279 .makePermanent()
Yi Tseng20f9e7b2018-05-24 23:27:39 +0800280 .withPriority(next.priority())
Yi Tseng1b154bd2017-11-20 17:48:19 -0800281 .forDevice(deviceId)
Yi Tseng20f9e7b2018-05-24 23:27:39 +0800282 .fromApp(next.appId())
Yi Tseng1b154bd2017-11-20 17:48:19 -0800283 .build());
Yi Tseng0b809722017-11-03 10:23:26 -0700284 }
285
286 private TrafficSelector buildNextIdSelector(int nextId) {
Yi Tseng1b154bd2017-11-20 17:48:19 -0800287 PiCriterion nextIdCriterion = PiCriterion.builder()
Yi Tseng43ee7e82018-04-12 16:37:34 +0800288 .matchExact(FabricConstants.FABRIC_METADATA_NEXT_ID, nextId)
Yi Tseng0b809722017-11-03 10:23:26 -0700289 .build();
290 return DefaultTrafficSelector.builder()
Yi Tseng1b154bd2017-11-20 17:48:19 -0800291 .matchPi(nextIdCriterion)
Yi Tseng0b809722017-11-03 10:23:26 -0700292 .build();
293 }
Esin Karaman24fda8a2018-01-26 11:52:28 +0000294
295 private void processBroadcastNext(NextObjective next, PipelinerTranslationResult.Builder resultBuilder) {
Carmelo Cascone58136812018-07-19 03:40:16 +0200296 final GroupDescription allGroup = getAllGroup(next);
297 if (allGroup == null) {
298 // Error already logged.
Esin Karaman24fda8a2018-01-26 11:52:28 +0000299 resultBuilder.setError(ObjectiveError.BADPARAMS);
300 return;
301 }
302
Carmelo Cascone58136812018-07-19 03:40:16 +0200303 resultBuilder.addGroup(allGroup);
Esin Karaman24fda8a2018-01-26 11:52:28 +0000304 //flow rule
Carmelo Cascone58136812018-07-19 03:40:16 +0200305 final TrafficSelector selector = buildNextIdSelector(next.id());
306 final PiActionParam groupIdParam = new PiActionParam(
307 FabricConstants.GID, allGroup.givenGroupId());
Esin Karaman24fda8a2018-01-26 11:52:28 +0000308
Carmelo Cascone58136812018-07-19 03:40:16 +0200309 final PiAction setMcGroupAction = PiAction.builder()
Esin Karaman24fda8a2018-01-26 11:52:28 +0000310 .withId(FabricConstants.FABRIC_INGRESS_NEXT_SET_MCAST_GROUP)
311 .withParameter(groupIdParam)
312 .build();
Carmelo Cascone58136812018-07-19 03:40:16 +0200313 final TrafficTreatment treatment = DefaultTrafficTreatment.builder()
Esin Karaman24fda8a2018-01-26 11:52:28 +0000314 .piTableAction(setMcGroupAction)
315 .build();
316
Carmelo Cascone58136812018-07-19 03:40:16 +0200317 resultBuilder.addFlowRule(
318 DefaultFlowRule.builder()
319 .withSelector(selector)
320 .withTreatment(treatment)
321 .forTable(FabricConstants.FABRIC_INGRESS_NEXT_MULTICAST)
322 .makePermanent()
323 .withPriority(next.priority())
324 .forDevice(deviceId)
325 .fromApp(next.appId())
326 .build());
Esin Karaman24fda8a2018-01-26 11:52:28 +0000327
328 // Egress VLAN handling
Carmelo Cascone58136812018-07-19 03:40:16 +0200329 next.next().forEach(t -> {
330 PortNumber outputPort = getOutputPort(t);
331 if (includesPopVlanInst(t) && outputPort != null) {
Esin Karaman24fda8a2018-01-26 11:52:28 +0000332 processVlanPopRule(outputPort, next, resultBuilder);
333 }
Carmelo Cascone58136812018-07-19 03:40:16 +0200334 if (t.allInstructions().size() > 2) {
335 // More than OUTPUT and VLAN_POP...
336 log.warn("Some instructions of BROADCAST NextObjective might" +
337 "not have been applied, supported only " +
338 "OUTPUT and VLAN_POP, but found {}", t);
339 }
Esin Karaman24fda8a2018-01-26 11:52:28 +0000340 });
341 }
Carmelo Cascone58136812018-07-19 03:40:16 +0200342
343 private GroupDescription getAllGroup(NextObjective next) {
344 final List<GroupBucket> bucketList = next.next().stream()
Carmelo Cascone1e8843f2018-07-19 19:01:12 +0200345 .map(FabricUtils::getOutputInstruction)
Carmelo Cascone58136812018-07-19 03:40:16 +0200346 .filter(Optional::isPresent)
347 .map(Optional::get)
348 .map(i -> DefaultTrafficTreatment.builder().add(i).build())
349 .map(DefaultGroupBucket::createAllGroupBucket)
350 .collect(Collectors.toList());
351
352 if (bucketList.size() != next.next().size()) {
353 log.warn("Got BROADCAST NextObjective with {} treatments but " +
Carmelo Cascone1e8843f2018-07-19 19:01:12 +0200354 "found only {} OUTPUT instructions, cannot " +
Carmelo Cascone58136812018-07-19 03:40:16 +0200355 "translate to ALL groups",
356 next.next().size(), bucketList.size());
357 return null;
358 }
359
Carmelo Cascone1e8843f2018-07-19 19:01:12 +0200360 // FIXME: remove once support for clone sessions is available
361 // Right now we add a CPU port to all multicast groups. The egress
362 // pipeline is expected to drop replicated packets to the CPU if a clone
363 // was not requested in the ingress pipeline.
364 bucketList.add(
365 DefaultGroupBucket.createAllGroupBucket(
366 DefaultTrafficTreatment.builder()
367 .setOutput(PortNumber.CONTROLLER)
368 .build()));
369
Carmelo Cascone58136812018-07-19 03:40:16 +0200370 final int groupId = next.id();
371 final GroupBuckets buckets = new GroupBuckets(bucketList);
372 // Used DefaultGroupKey instead of PiGroupKey
373 // as we don't have any action profile to apply to the groups of ALL type
374 final GroupKey groupKey = new DefaultGroupKey(FabricPipeliner.KRYO.serialize(groupId));
375
376 return new DefaultGroupDescription(deviceId,
377 GroupDescription.Type.ALL,
378 buckets,
379 groupKey,
380 groupId,
381 next.appId());
382 }
Yi Tseng0b809722017-11-03 10:23:26 -0700383}