blob: a22ecbefc5bc9de3c9a19de33c0c038dbd7721fe [file] [log] [blame]
Saurav Das822c4e22015-10-23 10:51:11 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2016-present Open Networking Foundation
Saurav Das822c4e22015-10-23 10:51:11 -07003 *
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 */
Yi Tsengef19de12017-04-24 11:33:05 -070016package org.onosproject.driver.pipeline.ofdpa;
Saurav Das822c4e22015-10-23 10:51:11 -070017
Charles Chan5270ed02016-01-30 23:22:37 -080018import com.google.common.collect.ImmutableList;
pier8b3aef42019-03-11 15:14:02 -070019import com.google.common.collect.Lists;
Yi Tseng47f82dc2017-03-05 22:48:39 -080020import com.google.common.collect.Sets;
pier8b3aef42019-03-11 15:14:02 -070021import org.apache.commons.lang3.tuple.Pair;
Saurav Das822c4e22015-10-23 10:51:11 -070022import org.onlab.osgi.ServiceDirectory;
Jonghwan Hyun800d9d02018-04-09 09:40:50 -070023import org.onlab.packet.EthType;
Saurav Das822c4e22015-10-23 10:51:11 -070024import org.onlab.packet.Ethernet;
Alex Yashchuk4caa8e82017-12-08 17:40:05 +020025import org.onlab.packet.EthType.EtherType;
Julia Ferguson65428c32017-08-10 18:15:24 +000026import org.onlab.packet.IpAddress;
Charles Chan14967c22015-12-07 11:11:50 -080027import org.onlab.packet.IpPrefix;
Charles Chan45b69ab2018-03-02 15:41:41 -080028import org.onlab.packet.MacAddress;
Saurav Das822c4e22015-10-23 10:51:11 -070029import org.onlab.packet.VlanId;
pier8b3aef42019-03-11 15:14:02 -070030import org.onlab.util.AbstractAccumulator;
31import org.onlab.util.Accumulator;
Saurav Das822c4e22015-10-23 10:51:11 -070032import org.onlab.util.KryoNamespace;
33import org.onosproject.core.ApplicationId;
34import org.onosproject.core.CoreService;
Jonghwan Hyun800d9d02018-04-09 09:40:50 -070035import org.onosproject.driver.extensions.Ofdpa3CopyField;
Charles Chancad338a2016-09-16 18:03:11 -070036import org.onosproject.driver.extensions.Ofdpa3MplsType;
37import org.onosproject.driver.extensions.Ofdpa3SetMplsType;
Jonghwan Hyun800d9d02018-04-09 09:40:50 -070038import org.onosproject.driver.extensions.OfdpaMatchActsetOutput;
39import org.onosproject.driver.extensions.OfdpaMatchAllowVlanTranslation;
Charles Chan14967c22015-12-07 11:11:50 -080040import org.onosproject.driver.extensions.OfdpaMatchVlanVid;
41import org.onosproject.driver.extensions.OfdpaSetVlanVid;
Saurav Das822c4e22015-10-23 10:51:11 -070042import org.onosproject.net.DeviceId;
43import org.onosproject.net.Port;
44import org.onosproject.net.PortNumber;
45import org.onosproject.net.behaviour.NextGroup;
46import org.onosproject.net.behaviour.Pipeliner;
47import org.onosproject.net.behaviour.PipelinerContext;
48import org.onosproject.net.device.DeviceService;
49import org.onosproject.net.driver.AbstractHandlerBehaviour;
50import org.onosproject.net.flow.DefaultFlowRule;
51import org.onosproject.net.flow.DefaultTrafficSelector;
52import org.onosproject.net.flow.DefaultTrafficTreatment;
53import org.onosproject.net.flow.FlowRule;
54import org.onosproject.net.flow.FlowRuleOperations;
55import org.onosproject.net.flow.FlowRuleOperationsContext;
56import org.onosproject.net.flow.FlowRuleService;
57import org.onosproject.net.flow.TrafficSelector;
58import org.onosproject.net.flow.TrafficTreatment;
59import org.onosproject.net.flow.criteria.Criteria;
60import org.onosproject.net.flow.criteria.Criterion;
61import org.onosproject.net.flow.criteria.EthCriterion;
62import org.onosproject.net.flow.criteria.EthTypeCriterion;
63import org.onosproject.net.flow.criteria.IPCriterion;
Pier Ventree0ae7a32016-11-23 09:57:42 -080064import org.onosproject.net.flow.criteria.Icmpv6CodeCriterion;
65import org.onosproject.net.flow.criteria.Icmpv6TypeCriterion;
Saurav Das8a0732e2015-11-20 15:27:53 -080066import org.onosproject.net.flow.criteria.MplsBosCriterion;
67import org.onosproject.net.flow.criteria.MplsCriterion;
Saurav Das822c4e22015-10-23 10:51:11 -070068import org.onosproject.net.flow.criteria.PortCriterion;
Charles Chand9e47c62017-10-05 15:17:15 -070069import org.onosproject.net.flow.criteria.TcpPortCriterion;
70import org.onosproject.net.flow.criteria.UdpPortCriterion;
Saurav Das822c4e22015-10-23 10:51:11 -070071import org.onosproject.net.flow.criteria.VlanIdCriterion;
72import org.onosproject.net.flow.instructions.Instruction;
73import org.onosproject.net.flow.instructions.Instructions.OutputInstruction;
Charles Chanf76de302018-06-15 18:54:18 -070074import org.onosproject.net.flow.instructions.Instructions.NoActionInstruction;
Saurav Das822c4e22015-10-23 10:51:11 -070075import org.onosproject.net.flow.instructions.L2ModificationInstruction;
Saurav Das8a0732e2015-11-20 15:27:53 -080076import org.onosproject.net.flow.instructions.L2ModificationInstruction.L2SubType;
Alex Yashchuk4caa8e82017-12-08 17:40:05 +020077import org.onosproject.net.flow.instructions.L2ModificationInstruction.ModMplsHeaderInstruction;
Charles Chan14967c22015-12-07 11:11:50 -080078import org.onosproject.net.flow.instructions.L3ModificationInstruction;
79import org.onosproject.net.flow.instructions.L3ModificationInstruction.L3SubType;
Saurav Das822c4e22015-10-23 10:51:11 -070080import org.onosproject.net.flowobjective.FilteringObjective;
81import org.onosproject.net.flowobjective.FlowObjectiveStore;
82import org.onosproject.net.flowobjective.ForwardingObjective;
83import org.onosproject.net.flowobjective.NextObjective;
84import org.onosproject.net.flowobjective.Objective;
85import org.onosproject.net.flowobjective.ObjectiveError;
Saurav Das822c4e22015-10-23 10:51:11 -070086import org.onosproject.net.group.DefaultGroupKey;
87import org.onosproject.net.group.Group;
Saurav Das822c4e22015-10-23 10:51:11 -070088import org.onosproject.net.group.GroupKey;
Saurav Das822c4e22015-10-23 10:51:11 -070089import org.onosproject.net.group.GroupService;
Saurav Das822c4e22015-10-23 10:51:11 -070090import org.onosproject.store.serializers.KryoNamespaces;
91import org.slf4j.Logger;
92
Pier Ventree0ae7a32016-11-23 09:57:42 -080093import java.util.ArrayDeque;
94import java.util.ArrayList;
95import java.util.Collection;
96import java.util.Collections;
97import java.util.Deque;
98import java.util.List;
99import java.util.Objects;
Jonghwan Hyun800d9d02018-04-09 09:40:50 -0700100import java.util.Optional;
pier8b3aef42019-03-11 15:14:02 -0700101import java.util.Timer;
Pier Ventree0ae7a32016-11-23 09:57:42 -0800102import java.util.concurrent.ScheduledExecutorService;
103import java.util.concurrent.TimeUnit;
Harshada Chaundkar77958a52019-08-05 15:33:30 +0000104import java.util.concurrent.atomic.AtomicBoolean;
Pier Ventree0ae7a32016-11-23 09:57:42 -0800105
106import static java.util.concurrent.Executors.newScheduledThreadPool;
pier8b3aef42019-03-11 15:14:02 -0700107import static java.util.concurrent.Executors.newSingleThreadScheduledExecutor;
pier5e8b2bc2019-10-04 10:54:07 +0200108import static org.onlab.packet.MacAddress.*;
Pier Ventree0ae7a32016-11-23 09:57:42 -0800109import static org.onlab.util.Tools.groupedThreads;
Jonghwan Hyun800d9d02018-04-09 09:40:50 -0700110import static org.onosproject.driver.extensions.Ofdpa3CopyField.OXM_ID_PACKET_REG_1;
111import static org.onosproject.driver.extensions.Ofdpa3CopyField.OXM_ID_VLAN_VID;
Yi Tsengef19de12017-04-24 11:33:05 -0700112import static org.onosproject.driver.pipeline.ofdpa.OfdpaGroupHandlerUtility.*;
pier9469f3e2019-04-17 17:05:08 +0200113import static org.onosproject.driver.pipeline.ofdpa.OfdpaPipelineUtility.*;
pier7afc7522019-05-10 13:19:15 +0200114import static org.onosproject.net.flowobjective.ForwardingObjective.Flag.SPECIFIC;
Pier Luigi075f1012018-02-01 10:23:12 +0100115import static org.onosproject.net.group.GroupDescription.Type.SELECT;
Pier Ventree0ae7a32016-11-23 09:57:42 -0800116import static org.slf4j.LoggerFactory.getLogger;
Pier Ventre140a8942016-11-02 07:26:38 -0700117import static org.onosproject.net.flow.criteria.Criterion.Type.MPLS_BOS;
Pier Ventree0ae7a32016-11-23 09:57:42 -0800118
Saurav Das822c4e22015-10-23 10:51:11 -0700119/**
120 * Driver for Broadcom's OF-DPA v2.0 TTP.
Saurav Das822c4e22015-10-23 10:51:11 -0700121 */
Charles Chan361154b2016-03-24 10:23:39 -0700122public class Ofdpa2Pipeline extends AbstractHandlerBehaviour implements Pipeliner {
pier8b3aef42019-03-11 15:14:02 -0700123 // Timer for the accumulator
124 private static final Timer TIMER = new Timer("fwdobj-batching");
125 private Accumulator<Pair<ForwardingObjective, Collection<FlowRule>>> accumulator;
pier9469f3e2019-04-17 17:05:08 +0200126 // Internal objects
Saurav Das822c4e22015-10-23 10:51:11 -0700127 private final Logger log = getLogger(getClass());
Charles Chan425854b2016-04-11 15:32:12 -0700128 protected ServiceDirectory serviceDirectory;
Saurav Das822c4e22015-10-23 10:51:11 -0700129 protected FlowRuleService flowRuleService;
Charles Chan425854b2016-04-11 15:32:12 -0700130 protected CoreService coreService;
Saurav Das8a0732e2015-11-20 15:27:53 -0800131 protected GroupService groupService;
132 protected FlowObjectiveStore flowObjectiveStore;
Saurav Das822c4e22015-10-23 10:51:11 -0700133 protected DeviceId deviceId;
134 protected ApplicationId driverId;
Saurav Das822c4e22015-10-23 10:51:11 -0700135 protected DeviceService deviceService;
Charles Chan188ebf52015-12-23 00:15:11 -0800136 protected static KryoNamespace appKryo = new KryoNamespace.Builder()
Yi Tsengef19de12017-04-24 11:33:05 -0700137 .register(KryoNamespaces.API)
138 .register(GroupKey.class)
139 .register(DefaultGroupKey.class)
140 .register(OfdpaNextGroup.class)
141 .register(ArrayDeque.class)
142 .build("Ofdpa2Pipeline");
Saurav Das822c4e22015-10-23 10:51:11 -0700143
Charles Chan425854b2016-04-11 15:32:12 -0700144 protected Ofdpa2GroupHandler groupHandler;
Saurav Das822c4e22015-10-23 10:51:11 -0700145
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700146 // flows installations to be retried
pier8b3aef42019-03-11 15:14:02 -0700147 private ScheduledExecutorService retryExecutorService
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700148 = newScheduledThreadPool(5, groupedThreads("OfdpaPipeliner", "retry-%d", log));
Saurav Das4f980082015-11-05 13:39:15 -0800149
pier8b3aef42019-03-11 15:14:02 -0700150 // accumulator executor service
151 private ScheduledExecutorService accumulatorExecutorService
152 = newSingleThreadScheduledExecutor(groupedThreads("OfdpaPipeliner", "acc-%d", log));
153
Saurav Das822c4e22015-10-23 10:51:11 -0700154 @Override
155 public void init(DeviceId deviceId, PipelinerContext context) {
pier561c8812020-05-20 22:50:30 +0200156 this.deviceId = deviceId;
pier9ada4192020-03-20 11:00:38 +0100157
pier561c8812020-05-20 22:50:30 +0200158 serviceDirectory = context.directory();
159 coreService = serviceDirectory.get(CoreService.class);
160 flowRuleService = serviceDirectory.get(FlowRuleService.class);
161 groupService = serviceDirectory.get(GroupService.class);
162 flowObjectiveStore = context.store();
163 deviceService = serviceDirectory.get(DeviceService.class);
164 // Init the accumulator, if enabled
165 if (isAccumulatorEnabled(this)) {
166 accumulator = new ForwardingObjectiveAccumulator(context.accumulatorMaxObjectives(),
167 context.accumulatorMaxBatchMillis(),
168 context.accumulatorMaxIdleMillis());
pier8b3aef42019-03-11 15:14:02 -0700169 }
Saurav Das822c4e22015-10-23 10:51:11 -0700170
pier561c8812020-05-20 22:50:30 +0200171 initDriverId();
172 initGroupHander(context);
Saurav Das822c4e22015-10-23 10:51:11 -0700173
pier561c8812020-05-20 22:50:30 +0200174 initializePipeline();
pier9ada4192020-03-20 11:00:38 +0100175 }
176
pier7afc7522019-05-10 13:19:15 +0200177 void setupAccumulatorForTests(int maxFwd, int maxBatchMS, int maxIdleMS) {
178 if (accumulator == null) {
179 accumulator = new ForwardingObjectiveAccumulator(maxFwd,
180 maxBatchMS,
181 maxIdleMS);
182 }
183 }
184
Charles Chan40132b32017-01-22 00:19:37 -0800185 protected void initDriverId() {
186 driverId = coreService.registerApplication(
187 "org.onosproject.driver.Ofdpa2Pipeline");
188 }
189
190 protected void initGroupHander(PipelinerContext context) {
pier9ada4192020-03-20 11:00:38 +0100191 // Terminate internal references
192 // We are terminating the references here
193 // because when the device is offline the apps
194 // are still sending flowobjectives
195 if (groupHandler != null) {
196 groupHandler.terminate();
197 }
Charles Chan40132b32017-01-22 00:19:37 -0800198 groupHandler = new Ofdpa2GroupHandler();
199 groupHandler.init(deviceId, context);
200 }
201
Saurav Das822c4e22015-10-23 10:51:11 -0700202 protected void initializePipeline() {
Charles Chan188ebf52015-12-23 00:15:11 -0800203 // OF-DPA does not require initializing the pipeline as it puts default
204 // rules automatically in the hardware. However emulation of OFDPA in
205 // software switches does require table-miss-entries.
Saurav Das822c4e22015-10-23 10:51:11 -0700206 }
207
Charles Chand1172632017-03-15 17:33:09 -0700208 /**
Alex Yashchuk4caa8e82017-12-08 17:40:05 +0200209 * Determines whether this pipeline requires MPLS POP instruction.
210 *
211 * @return true to use MPLS POP instruction
212 */
213 public boolean requireMplsPop() {
214 return true;
215 }
216
217 /**
Harshada Chaundkar77958a52019-08-05 15:33:30 +0000218 * Determines whether this pipeline requires one additional flow matching on ethType 0x86dd in ACL table.
219 *
220 * @return true to create one additional flow matching on ethType 0x86dd in ACL table
221 */
222 protected boolean requireEthType() {
223 return true;
224 }
225
226 /**
Alex Yashchuk4caa8e82017-12-08 17:40:05 +0200227 * Determines whether this pipeline requires MPLS BOS match.
228 *
229 * @return true to use MPLS BOS match
230 */
231 public boolean requireMplsBosMatch() {
232 return true;
233 }
234
235 /**
236 * Determines whether this pipeline requires MPLS TTL decrement and copy.
237 *
238 * @return true to use MPLS TTL decrement and copy
239 */
240 public boolean requireMplsTtlModification() {
241 return true;
242 }
243
244 /**
Charles Chand1172632017-03-15 17:33:09 -0700245 * Determines whether this pipeline requires OFDPA match and set VLAN extensions.
246 *
247 * @return true to use the extensions
248 */
249 protected boolean requireVlanExtensions() {
250 return true;
251 }
252
Saurav Das86d13e82017-04-28 17:03:48 -0700253 /**
pierventreb58e9c92020-05-22 09:42:31 -0700254 * Determines whether this pipeline requires second VLAN entry in VLAN table.
255 * OF-DPA hardware requires one VLAN filtering rule and one VLAN assignment
256 * flow in the VLAN table in the case of untagged packets. Software emulations
257 * just use one flow.
258 *
259 * @return true if required
260 */
261 public boolean requireSecondVlanTableEntry() {
262 return true;
263 }
264
265 /**
Saurav Das86d13e82017-04-28 17:03:48 -0700266 * Determines whether in-port should be matched on in TMAC table rules.
267 *
268 * @return true if match on in-port should be programmed
269 */
270 protected boolean matchInPortTmacTable() {
271 return true;
272 }
273
Charles Chand9e47c62017-10-05 15:17:15 -0700274 /**
275 * Determines whether matching L4 destination port on IPv6 packets is supported in ACL table.
276 *
277 * @return true if matching L4 destination port on IPv6 packets is supported in ACL table.
278 */
279 protected boolean supportIpv6L4Dst() {
280 return true;
281 }
282
Saurav Dasc568c342018-01-25 09:49:01 -0800283 /**
284 * Determines whether this driver should continue to retry flows that point
285 * to empty groups. See CORD-554.
286 *
287 * @return true if the driver should retry flows
288 */
289 protected boolean shouldRetry() {
290 return true;
291 }
292
Charles Chan45b69ab2018-03-02 15:41:41 -0800293 /**
294 * Determines whether this driver requires unicast flow to be installed before multicast flow
295 * in TMAC table.
296 *
297 * @return true if required
298 */
299 protected boolean requireUnicastBeforeMulticast() {
300 return false;
301 }
302
Andrea Campanellad980c6d2018-04-30 11:48:55 +0200303 /**
304 * Determines whether this driver supports installing a clearDeferred action on table 30.
305 *
306 * @return true if required
307 */
308 protected boolean supportsUnicastBlackHole() {
309 return true;
310 }
311
Charles Chanab591602019-01-22 17:25:04 -0800312 protected boolean requirePuntTable() {
313 return false;
314 }
315
Saurav Das822c4e22015-10-23 10:51:11 -0700316 //////////////////////////////////////
317 // Flow Objectives
318 //////////////////////////////////////
319
320 @Override
321 public void filter(FilteringObjective filteringObjective) {
322 if (filteringObjective.type() == FilteringObjective.Type.PERMIT) {
323 processFilter(filteringObjective,
324 filteringObjective.op() == Objective.Operation.ADD,
325 filteringObjective.appId());
326 } else {
327 // Note that packets that don't match the PERMIT filter are
328 // automatically denied. The DENY filter is used to deny packets
329 // that are otherwise permitted by the PERMIT filter.
330 // Use ACL table flow rules here for DENY filtering objectives
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530331 log.warn("filter objective other than PERMIT currently not supported");
Saurav Das822c4e22015-10-23 10:51:11 -0700332 fail(filteringObjective, ObjectiveError.UNSUPPORTED);
333 }
334 }
335
336 @Override
337 public void forward(ForwardingObjective fwd) {
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700338 Collection<FlowRule> rules = processForward(fwd);
Saurav Das25190812016-05-27 13:54:07 -0700339 if (rules == null || rules.isEmpty()) {
340 // Assumes fail message has already been generated to the objective
341 // context. Returning here prevents spurious pass message to be
342 // generated by FlowRule service for empty flowOps.
343 return;
344 }
pier7afc7522019-05-10 13:19:15 +0200345 // Let's accumulate flow rules if accumulator is active and fwd objective is not versatile.
346 // Otherwise send directly, without adding futher delay
347 if (accumulator != null && Objects.equals(fwd.flag(), SPECIFIC)) {
pier8b3aef42019-03-11 15:14:02 -0700348 accumulator.add(Pair.of(fwd, rules));
349 } else {
350 sendForwards(Collections.singletonList(Pair.of(fwd, rules)));
351 }
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700352 }
353
pier8b3aef42019-03-11 15:14:02 -0700354 // Builds the batch using the accumulated flow rules
355 private void sendForwards(List<Pair<ForwardingObjective, Collection<FlowRule>>> pairs) {
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700356 FlowRuleOperations.Builder flowOpsBuilder = FlowRuleOperations.builder();
pier8b3aef42019-03-11 15:14:02 -0700357 log.debug("Sending {} fwd-objs", pairs.size());
358 List<Objective> fwdObjs = Lists.newArrayList();
359 // Iterates over all accumulated flow rules and then build an unique batch
360 pairs.forEach(pair -> {
361 ForwardingObjective fwd = pair.getLeft();
362 Collection<FlowRule> rules = pair.getRight();
363 switch (fwd.op()) {
364 case ADD:
365 rules.stream()
366 .filter(Objects::nonNull)
367 .forEach(flowOpsBuilder::add);
368 log.debug("Applying a add fwd-obj {} to sw:{}", fwd.id(), deviceId);
369 fwdObjs.add(fwd);
370 break;
371 case REMOVE:
372 rules.stream()
373 .filter(Objects::nonNull)
374 .forEach(flowOpsBuilder::remove);
375 log.debug("Deleting a flow rule to sw:{}", deviceId);
376 fwdObjs.add(fwd);
377 break;
378 default:
379 fail(fwd, ObjectiveError.UNKNOWN);
380 log.warn("Unknown forwarding type {}", fwd.op());
381 }
382 });
383 // Finally applies the operations
Saurav Das822c4e22015-10-23 10:51:11 -0700384 flowRuleService.apply(flowOpsBuilder.build(new FlowRuleOperationsContext() {
pier8b3aef42019-03-11 15:14:02 -0700385
Saurav Das822c4e22015-10-23 10:51:11 -0700386 @Override
387 public void onSuccess(FlowRuleOperations ops) {
pier8b3aef42019-03-11 15:14:02 -0700388 log.trace("Flow rule operations onSuccess {}", ops);
pier9469f3e2019-04-17 17:05:08 +0200389 fwdObjs.forEach(OfdpaPipelineUtility::pass);
Saurav Das822c4e22015-10-23 10:51:11 -0700390 }
391
392 @Override
393 public void onError(FlowRuleOperations ops) {
pier8b3aef42019-03-11 15:14:02 -0700394 ObjectiveError error = ObjectiveError.FLOWINSTALLATIONFAILED;
395 log.warn("Flow rule operations onError {}. Reason = {}", ops, error);
396 fwdObjs.forEach(fwdObj -> fail(fwdObj, error));
Saurav Das822c4e22015-10-23 10:51:11 -0700397 }
398 }));
Saurav Das822c4e22015-10-23 10:51:11 -0700399 }
400
401 @Override
402 public void next(NextObjective nextObjective) {
Saurav Das8a0732e2015-11-20 15:27:53 -0800403 NextGroup nextGroup = flowObjectiveStore.getNextGroup(nextObjective.id());
404 switch (nextObjective.op()) {
405 case ADD:
Saurav Das4f980082015-11-05 13:39:15 -0800406 if (nextGroup != null) {
Saurav Das8a0732e2015-11-20 15:27:53 -0800407 log.warn("Cannot add next {} that already exists in device {}",
408 nextObjective.id(), deviceId);
409 return;
410 }
Saurav Das1547b3f2017-05-05 17:01:08 -0700411 log.debug("Processing NextObjective id {} in dev {} - add group",
Saurav Das8a0732e2015-11-20 15:27:53 -0800412 nextObjective.id(), deviceId);
Charles Chan425854b2016-04-11 15:32:12 -0700413 groupHandler.addGroup(nextObjective);
Saurav Das8a0732e2015-11-20 15:27:53 -0800414 break;
415 case ADD_TO_EXISTING:
416 if (nextGroup != null) {
Saurav Das1547b3f2017-05-05 17:01:08 -0700417 log.debug("Processing NextObjective id {} in dev {} - add bucket",
Saurav Das8a0732e2015-11-20 15:27:53 -0800418 nextObjective.id(), deviceId);
Charles Chan425854b2016-04-11 15:32:12 -0700419 groupHandler.addBucketToGroup(nextObjective, nextGroup);
Saurav Das4f980082015-11-05 13:39:15 -0800420 } else {
Saurav Das8a0732e2015-11-20 15:27:53 -0800421 // it is possible that group-chain has not been fully created yet
Saurav Das423fe2b2015-12-04 10:52:59 -0800422 log.debug("Waiting to add bucket to group for next-id:{} in dev:{}",
423 nextObjective.id(), deviceId);
Yi Tseng47f82dc2017-03-05 22:48:39 -0800424
425 // by design multiple pending bucket is allowed for the group
426 groupHandler.pendingBuckets.compute(nextObjective.id(), (nextId, pendBkts) -> {
427 if (pendBkts == null) {
428 pendBkts = Sets.newHashSet();
429 }
430 pendBkts.add(nextObjective);
431 return pendBkts;
432 });
Saurav Das4f980082015-11-05 13:39:15 -0800433 }
Saurav Das8a0732e2015-11-20 15:27:53 -0800434 break;
435 case REMOVE:
436 if (nextGroup == null) {
437 log.warn("Cannot remove next {} that does not exist in device {}",
438 nextObjective.id(), deviceId);
439 return;
440 }
Saurav Das1547b3f2017-05-05 17:01:08 -0700441 log.debug("Processing NextObjective id {} in dev {} - remove group",
Saurav Das8a0732e2015-11-20 15:27:53 -0800442 nextObjective.id(), deviceId);
Charles Chan425854b2016-04-11 15:32:12 -0700443 groupHandler.removeGroup(nextObjective, nextGroup);
Saurav Das8a0732e2015-11-20 15:27:53 -0800444 break;
445 case REMOVE_FROM_EXISTING:
446 if (nextGroup == null) {
447 log.warn("Cannot remove from next {} that does not exist in device {}",
448 nextObjective.id(), deviceId);
449 return;
450 }
Saurav Das1547b3f2017-05-05 17:01:08 -0700451 log.debug("Processing NextObjective id {} in dev {} - remove bucket",
Saurav Das8a0732e2015-11-20 15:27:53 -0800452 nextObjective.id(), deviceId);
Charles Chan425854b2016-04-11 15:32:12 -0700453 groupHandler.removeBucketFromGroup(nextObjective, nextGroup);
Saurav Das8a0732e2015-11-20 15:27:53 -0800454 break;
Jonghwan Hyunf810a7a2018-02-12 16:43:45 +0900455 case MODIFY:
456 if (nextGroup == null) {
457 log.warn("Cannot modify next {} that does not exist in device {}",
458 nextObjective.id(), deviceId);
459 return;
460 }
Ruchi Sahotaef0761c2019-01-28 01:08:18 +0000461 log.debug("Processing NextObjective id {} in dev {} group {} - modify bucket",
462 nextObjective.id(), deviceId, nextGroup);
Jonghwan Hyunf810a7a2018-02-12 16:43:45 +0900463 groupHandler.modifyBucketFromGroup(nextObjective, nextGroup);
464 break;
Saurav Dasceccf242017-08-03 18:30:35 -0700465 case VERIFY:
466 if (nextGroup == null) {
467 log.warn("Cannot verify next {} that does not exist in device {}",
468 nextObjective.id(), deviceId);
469 return;
470 }
471 log.debug("Processing NextObjective id {} in dev {} - verify",
472 nextObjective.id(), deviceId);
473 groupHandler.verifyGroup(nextObjective, nextGroup);
474 break;
Saurav Das8a0732e2015-11-20 15:27:53 -0800475 default:
Saurav Das4f980082015-11-05 13:39:15 -0800476 log.warn("Unsupported operation {}", nextObjective.op());
Saurav Das822c4e22015-10-23 10:51:11 -0700477 }
478 }
479
Daniele Moro06aac702021-07-19 22:39:22 +0200480 @Override
481 public void purgeAll(ApplicationId appId) {
482 flowRuleService.purgeFlowRules(deviceId, appId);
483 groupService.purgeGroupEntries(deviceId, appId);
484 }
485
Saurav Das822c4e22015-10-23 10:51:11 -0700486 //////////////////////////////////////
487 // Flow handling
488 //////////////////////////////////////
489
490 /**
Yi Tseng0fdc07e2017-04-24 19:17:08 -0700491 * As per OFDPA 2.0 TTP, filtering of VLAN ids and MAC addresses (for routing)
492 * configured on switch ports happen in different tables.
Saurav Das822c4e22015-10-23 10:51:11 -0700493 *
494 * @param filt the filtering objective
495 * @param install indicates whether to add or remove the objective
496 * @param applicationId the application that sent this objective
497 */
Saurav Das52025962016-01-28 22:30:01 -0800498 protected void processFilter(FilteringObjective filt,
499 boolean install, ApplicationId applicationId) {
Saurav Das822c4e22015-10-23 10:51:11 -0700500 // This driver only processes filtering criteria defined with switch
501 // ports as the key
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530502 PortCriterion portCriterion = null;
Saurav Das822c4e22015-10-23 10:51:11 -0700503 EthCriterion ethCriterion = null;
504 VlanIdCriterion vidCriterion = null;
Saurav Das822c4e22015-10-23 10:51:11 -0700505 if (!filt.key().equals(Criteria.dummy()) &&
506 filt.key().type() == Criterion.Type.IN_PORT) {
507 portCriterion = (PortCriterion) filt.key();
Saurav Das822c4e22015-10-23 10:51:11 -0700508 }
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530509 if (portCriterion == null) {
510 log.debug("No IN_PORT defined in filtering objective from app: {}",
511 applicationId);
512 } else {
513 log.debug("Received filtering objective for dev/port: {}/{}", deviceId,
514 portCriterion.port());
515 }
Saurav Das822c4e22015-10-23 10:51:11 -0700516 // convert filtering conditions for switch-intfs into flowrules
517 FlowRuleOperations.Builder ops = FlowRuleOperations.builder();
518 for (Criterion criterion : filt.conditions()) {
Yi Tseng0fdc07e2017-04-24 19:17:08 -0700519 switch (criterion.type()) {
520 case ETH_DST:
521 case ETH_DST_MASKED:
522 ethCriterion = (EthCriterion) criterion;
523 break;
524 case VLAN_VID:
525 vidCriterion = (VlanIdCriterion) criterion;
526 break;
527 default:
528 log.warn("Unsupported filter {}", criterion);
529 fail(filt, ObjectiveError.UNSUPPORTED);
530 return;
Saurav Das822c4e22015-10-23 10:51:11 -0700531 }
532 }
533
Saurav Das0e99e2b2015-10-28 12:39:42 -0700534 VlanId assignedVlan = null;
Charles Chane849c192016-01-11 18:28:54 -0800535 if (vidCriterion != null) {
Charles Chand55e84d2016-03-30 17:54:24 -0700536 // Use the VLAN in criterion if metadata is not present and the traffic is tagged
Charles Chanc550f2e2017-12-19 19:55:57 -0800537 if (!vidCriterion.vlanId().equals(VlanId.NONE)) {
Charles Chane849c192016-01-11 18:28:54 -0800538 assignedVlan = vidCriterion.vlanId();
Piere5bff482018-03-07 11:42:50 +0100539 }
540 // If the meta VLAN is present let's update the assigned vlan
541 if (filt.meta() != null) {
542 VlanId metaVlan = readVlanFromTreatment(filt.meta());
543 if (metaVlan != null) {
544 assignedVlan = metaVlan;
545 }
Charles Chand55e84d2016-03-30 17:54:24 -0700546 }
Charles Chane849c192016-01-11 18:28:54 -0800547
Charles Chand55e84d2016-03-30 17:54:24 -0700548 if (assignedVlan == null) {
549 log.error("Driver fails to extract VLAN information. "
Frank Wangd8ab0962017-08-11 11:09:30 +0800550 + "Not processing VLAN filters on device {}.", deviceId);
Charles Chand55e84d2016-03-30 17:54:24 -0700551 log.debug("VLAN ID in criterion={}, metadata={}",
552 readVlanFromTreatment(filt.meta()), vidCriterion.vlanId());
553 fail(filt, ObjectiveError.BADPARAMS);
554 return;
Saurav Das0e99e2b2015-10-28 12:39:42 -0700555 }
556 }
557
Yi Tseng3a77b4f2017-02-06 15:02:17 -0800558 if (ethCriterion == null || ethCriterion.mac().equals(NONE)) {
Charles Chan985b12e2016-05-11 19:47:22 -0700559 // NOTE: it is possible that a filtering objective only has vidCriterion
Daniel Ginsburgc1d47e92018-04-30 19:27:19 -0400560 log.debug("filtering objective missing dstMac, won't program TMAC table");
Saurav Das822c4e22015-10-23 10:51:11 -0700561 } else {
Charles Chan45b69ab2018-03-02 15:41:41 -0800562 MacAddress unicastMac = readEthDstFromTreatment(filt.meta());
Charles Chan66291502018-03-02 16:43:28 -0800563 List<List<FlowRule>> allStages = processEthDstFilter(portCriterion, ethCriterion,
Charles Chan45b69ab2018-03-02 15:41:41 -0800564 vidCriterion, assignedVlan, unicastMac, applicationId);
Charles Chan66291502018-03-02 16:43:28 -0800565 for (List<FlowRule> flowRules : allStages) {
Charles Chan45b69ab2018-03-02 15:41:41 -0800566 log.trace("Starting a new flow rule stage for TMAC table flow");
Charles Chan66291502018-03-02 16:43:28 -0800567 ops.newStage();
Charles Chanc550f2e2017-12-19 19:55:57 -0800568
Charles Chan66291502018-03-02 16:43:28 -0800569 for (FlowRule flowRule : flowRules) {
pier5e8b2bc2019-10-04 10:54:07 +0200570 log.trace("{} flow rule in TMAC table: {} for dev: {}",
571 (install) ? "adding" : "removing", flowRule, deviceId);
Charles Chan66291502018-03-02 16:43:28 -0800572 if (install) {
573 ops = ops.add(flowRule);
Charles Chanc550f2e2017-12-19 19:55:57 -0800574 } else {
Charles Chan66291502018-03-02 16:43:28 -0800575 // NOTE: Only remove TMAC flow when there is no more enabled port within the
576 // same VLAN on this device if TMAC doesn't support matching on in_port.
pier5e8b2bc2019-10-04 10:54:07 +0200577 if (filt.meta() != null && filt.meta().clearedDeferred()) {
578 // TMac mcast does not match on the input port - we have to remove it
579 // only if this is the last port
580 FlowRule rule = buildTmacRuleForMcastFromUnicast(flowRule, applicationId);
581 // IPv6 or IPv4 tmac rule
582 if (rule != null) {
583 // Add first the mcast rule and then open a new stage for the unicast
584 ops = ops.remove(rule);
585 ops.newStage();
586 }
587 ops = ops.remove(flowRule);
588 } else if (matchInPortTmacTable()) {
Charles Chan66291502018-03-02 16:43:28 -0800589 ops = ops.remove(flowRule);
590 } else {
pier5e8b2bc2019-10-04 10:54:07 +0200591 log.debug("Abort TMAC flow removal on {}. " +
592 "Some other ports still share this TMAC flow", deviceId);
Charles Chan66291502018-03-02 16:43:28 -0800593 }
Charles Chanc550f2e2017-12-19 19:55:57 -0800594 }
595 }
Saurav Das822c4e22015-10-23 10:51:11 -0700596 }
597 }
598
Charles Chan985b12e2016-05-11 19:47:22 -0700599 if (vidCriterion == null) {
600 // NOTE: it is possible that a filtering objective only has ethCriterion
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530601 log.info("filtering objective missing VLAN, cannot program VLAN Table");
Saurav Das822c4e22015-10-23 10:51:11 -0700602 } else {
Charles Chan66291502018-03-02 16:43:28 -0800603 List<List<FlowRule>> allStages = processVlanIdFilter(
pier6aef5b72019-06-10 17:10:26 +0200604 portCriterion, vidCriterion, assignedVlan, applicationId, install);
Charles Chan66291502018-03-02 16:43:28 -0800605 for (List<FlowRule> flowRules : allStages) {
Charles Chan45b69ab2018-03-02 15:41:41 -0800606 log.trace("Starting a new flow rule stage for VLAN table flow");
Charles Chan66291502018-03-02 16:43:28 -0800607 ops.newStage();
Charles Chan14967c22015-12-07 11:11:50 -0800608
Charles Chan66291502018-03-02 16:43:28 -0800609 for (FlowRule flowRule : flowRules) {
610 log.trace("{} flow rules in VLAN table: {} for dev: {}",
611 (install) ? "adding" : "removing", flowRule, deviceId);
612 ops = install ? ops.add(flowRule) : ops.remove(flowRule);
Charles Chand1172632017-03-15 17:33:09 -0700613 }
Saurav Das822c4e22015-10-23 10:51:11 -0700614 }
615 }
616
Saurav Das822c4e22015-10-23 10:51:11 -0700617 // apply filtering flow rules
618 flowRuleService.apply(ops.build(new FlowRuleOperationsContext() {
619 @Override
620 public void onSuccess(FlowRuleOperations ops) {
Saurav Das018605f2017-02-18 14:05:44 -0800621 log.debug("Applied {} filtering rules in device {}",
Saurav Das822c4e22015-10-23 10:51:11 -0700622 ops.stages().get(0).size(), deviceId);
623 pass(filt);
624 }
625
626 @Override
627 public void onError(FlowRuleOperations ops) {
628 log.info("Failed to apply all filtering rules in dev {}", deviceId);
629 fail(filt, ObjectiveError.FLOWINSTALLATIONFAILED);
630 }
631 }));
632
633 }
634
635 /**
Charles Chand1172632017-03-15 17:33:09 -0700636 * Internal implementation of processVlanIdFilter.
637 * <p>
638 * The is_present bit in set_vlan_vid action is required to be 0 in OFDPA i12.
639 * Since it is non-OF spec, we need an extension treatment for that.
640 * The useVlanExtension must be set to false for OFDPA i12.
641 * </p>
Charles Chan66291502018-03-02 16:43:28 -0800642 * <p>
643 * NOTE: Separate VLAN filtering rules and assignment rules
644 * into different stages in order to guarantee that filtering rules
645 * always go first, as required by OFDPA.
646 * </p>
Saurav Das0e99e2b2015-10-28 12:39:42 -0700647 *
Charles Chanf9e98652016-09-07 16:54:23 -0700648 * @param portCriterion port on device for which this filter is programmed
649 * @param vidCriterion vlan assigned to port, or NONE for untagged
650 * @param assignedVlan assigned vlan-id for untagged packets
651 * @param applicationId for application programming this filter
pier6aef5b72019-06-10 17:10:26 +0200652 * @param install indicates whether to add or remove the objective
Charles Chan66291502018-03-02 16:43:28 -0800653 * @return stages of flow rules for port-vlan filters
Saurav Das822c4e22015-10-23 10:51:11 -0700654 */
Charles Chan66291502018-03-02 16:43:28 -0800655 protected List<List<FlowRule>> processVlanIdFilter(PortCriterion portCriterion,
pier6aef5b72019-06-10 17:10:26 +0200656 VlanIdCriterion vidCriterion,
657 VlanId assignedVlan,
658 ApplicationId applicationId,
659 boolean install) {
Charles Chan66291502018-03-02 16:43:28 -0800660 List<FlowRule> filteringRules = new ArrayList<>();
661 List<FlowRule> assignmentRules = new ArrayList<>();
Saurav Das822c4e22015-10-23 10:51:11 -0700662 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
663 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
Charles Chan14967c22015-12-07 11:11:50 -0800664 TrafficSelector.Builder preSelector = null;
665 TrafficTreatment.Builder preTreatment = null;
666
Saurav Das4f980082015-11-05 13:39:15 -0800667 treatment.transition(TMAC_TABLE);
668
Saurav Das822c4e22015-10-23 10:51:11 -0700669 if (vidCriterion.vlanId() == VlanId.NONE) {
670 // untagged packets are assigned vlans
Charles Chand1172632017-03-15 17:33:09 -0700671 preSelector = DefaultTrafficSelector.builder();
672 if (requireVlanExtensions()) {
673 OfdpaMatchVlanVid ofdpaMatchVlanVid = new OfdpaMatchVlanVid(VlanId.NONE);
674 selector.extension(ofdpaMatchVlanVid, deviceId);
Charles Chanf9e98652016-09-07 16:54:23 -0700675 OfdpaSetVlanVid ofdpaSetVlanVid = new OfdpaSetVlanVid(assignedVlan);
676 treatment.extension(ofdpaSetVlanVid, deviceId);
Charles Chand1172632017-03-15 17:33:09 -0700677
678 OfdpaMatchVlanVid preOfdpaMatchVlanVid = new OfdpaMatchVlanVid(assignedVlan);
679 preSelector.extension(preOfdpaMatchVlanVid, deviceId);
Charles Chanf9e98652016-09-07 16:54:23 -0700680 } else {
Charles Chand1172632017-03-15 17:33:09 -0700681 selector.matchVlanId(VlanId.NONE);
Charles Chanf9e98652016-09-07 16:54:23 -0700682 treatment.setVlanId(assignedVlan);
Charles Chand1172632017-03-15 17:33:09 -0700683
684 preSelector.matchVlanId(assignedVlan);
685 }
686 preTreatment = DefaultTrafficTreatment.builder().transition(TMAC_TABLE);
687 } else {
688 if (requireVlanExtensions()) {
689 OfdpaMatchVlanVid ofdpaMatchVlanVid = new OfdpaMatchVlanVid(vidCriterion.vlanId());
690 selector.extension(ofdpaMatchVlanVid, deviceId);
691 } else {
692 selector.matchVlanId(vidCriterion.vlanId());
Charles Chanf9e98652016-09-07 16:54:23 -0700693 }
Charles Chan14967c22015-12-07 11:11:50 -0800694
Charles Chand55e84d2016-03-30 17:54:24 -0700695 if (!assignedVlan.equals(vidCriterion.vlanId())) {
Charles Chand1172632017-03-15 17:33:09 -0700696 if (requireVlanExtensions()) {
Charles Chanc03782d2017-01-31 13:57:55 -0800697 OfdpaSetVlanVid ofdpaSetVlanVid = new OfdpaSetVlanVid(assignedVlan);
698 treatment.extension(ofdpaSetVlanVid, deviceId);
699 } else {
700 treatment.setVlanId(assignedVlan);
701 }
Charles Chand55e84d2016-03-30 17:54:24 -0700702 }
Saurav Das822c4e22015-10-23 10:51:11 -0700703 }
Saurav Das822c4e22015-10-23 10:51:11 -0700704
705 // ofdpa cannot match on ALL portnumber, so we need to use separate
706 // rules for each port.
Charles Chan14967c22015-12-07 11:11:50 -0800707 List<PortNumber> portnums = new ArrayList<>();
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530708 if (portCriterion != null) {
709 if (PortNumber.ALL.equals(portCriterion.port())) {
710 for (Port port : deviceService.getPorts(deviceId)) {
711 if (port.number().toLong() > 0 && port.number().toLong() < OFPP_MAX) {
712 portnums.add(port.number());
713 }
Saurav Das822c4e22015-10-23 10:51:11 -0700714 }
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530715 } else {
716 portnums.add(portCriterion.port());
Saurav Das822c4e22015-10-23 10:51:11 -0700717 }
718 } else {
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530719 log.warn("Filtering Objective missing Port Criterion . " +
Charles Chan66291502018-03-02 16:43:28 -0800720 "VLAN Table cannot be programmed for {}", deviceId);
Saurav Das822c4e22015-10-23 10:51:11 -0700721 }
Saurav Das4f980082015-11-05 13:39:15 -0800722
Saurav Das822c4e22015-10-23 10:51:11 -0700723 for (PortNumber pnum : portnums) {
Saurav Das4f980082015-11-05 13:39:15 -0800724 // create rest of flowrule
Saurav Das822c4e22015-10-23 10:51:11 -0700725 selector.matchInPort(pnum);
726 FlowRule rule = DefaultFlowRule.builder()
727 .forDevice(deviceId)
728 .withSelector(selector.build())
729 .withTreatment(treatment.build())
730 .withPriority(DEFAULT_PRIORITY)
731 .fromApp(applicationId)
732 .makePermanent()
733 .forTable(VLAN_TABLE).build();
Charles Chan66291502018-03-02 16:43:28 -0800734 assignmentRules.add(rule);
Charles Chan14967c22015-12-07 11:11:50 -0800735
736 if (preSelector != null) {
737 preSelector.matchInPort(pnum);
738 FlowRule preRule = DefaultFlowRule.builder()
739 .forDevice(deviceId)
740 .withSelector(preSelector.build())
741 .withTreatment(preTreatment.build())
742 .withPriority(DEFAULT_PRIORITY)
743 .fromApp(applicationId)
744 .makePermanent()
745 .forTable(VLAN_TABLE).build();
Charles Chan66291502018-03-02 16:43:28 -0800746 filteringRules.add(preRule);
Charles Chan14967c22015-12-07 11:11:50 -0800747 }
Saurav Das822c4e22015-10-23 10:51:11 -0700748 }
pier6aef5b72019-06-10 17:10:26 +0200749 return install ? ImmutableList.of(filteringRules, assignmentRules) :
750 ImmutableList.of(assignmentRules, filteringRules);
Saurav Das822c4e22015-10-23 10:51:11 -0700751 }
752
753 /**
754 * Allows routed packets with correct destination MAC to be directed
Charles Chan45b69ab2018-03-02 15:41:41 -0800755 * to unicast routing table, multicast routing table or MPLS forwarding table.
Saurav Das822c4e22015-10-23 10:51:11 -0700756 *
757 * @param portCriterion port on device for which this filter is programmed
758 * @param ethCriterion dstMac of device for which is filter is programmed
759 * @param vidCriterion vlan assigned to port, or NONE for untagged
Saurav Das0e99e2b2015-10-28 12:39:42 -0700760 * @param assignedVlan assigned vlan-id for untagged packets
Charles Chan45b69ab2018-03-02 15:41:41 -0800761 * @param unicastMac some switches require a unicast TMAC flow to be programmed before multicast
762 * TMAC flow. This MAC address will be used for the unicast TMAC flow.
763 * This is unused if the filtering objective is a unicast.
Saurav Das822c4e22015-10-23 10:51:11 -0700764 * @param applicationId for application programming this filter
Charles Chan66291502018-03-02 16:43:28 -0800765 * @return stages of flow rules for port-vlan filters
Saurav Das822c4e22015-10-23 10:51:11 -0700766
767 */
Charles Chan66291502018-03-02 16:43:28 -0800768 protected List<List<FlowRule>> processEthDstFilter(PortCriterion portCriterion,
Saurav Das822c4e22015-10-23 10:51:11 -0700769 EthCriterion ethCriterion,
770 VlanIdCriterion vidCriterion,
Saurav Das0e99e2b2015-10-28 12:39:42 -0700771 VlanId assignedVlan,
Charles Chan45b69ab2018-03-02 15:41:41 -0800772 MacAddress unicastMac,
Saurav Das822c4e22015-10-23 10:51:11 -0700773 ApplicationId applicationId) {
Charles Chan5270ed02016-01-30 23:22:37 -0800774 // Consider PortNumber.ANY as wildcard. Match ETH_DST only
Sivachidambaram Subramanianc598d062017-05-16 22:42:06 +0530775 if (portCriterion != null && PortNumber.ANY.equals(portCriterion.port())) {
Charles Chan5270ed02016-01-30 23:22:37 -0800776 return processEthDstOnlyFilter(ethCriterion, applicationId);
777 }
778
Charles Chan5b9df8d2016-03-28 22:21:40 -0700779 // Multicast MAC
780 if (ethCriterion.mask() != null) {
Charles Chan45b69ab2018-03-02 15:41:41 -0800781 return processMcastEthDstFilter(ethCriterion, assignedVlan, unicastMac, applicationId);
Charles Chan5b9df8d2016-03-28 22:21:40 -0700782 }
783
Saurav Das822c4e22015-10-23 10:51:11 -0700784 //handling untagged packets via assigned VLAN
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530785 if (vidCriterion != null && vidCriterion.vlanId() == VlanId.NONE) {
Saurav Das0e99e2b2015-10-28 12:39:42 -0700786 vidCriterion = (VlanIdCriterion) Criteria.matchVlanId(assignedVlan);
Saurav Das822c4e22015-10-23 10:51:11 -0700787 }
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530788 List<FlowRule> rules = new ArrayList<>();
789 OfdpaMatchVlanVid ofdpaMatchVlanVid = null;
790 if (vidCriterion != null && requireVlanExtensions()) {
791 ofdpaMatchVlanVid = new OfdpaMatchVlanVid(vidCriterion.vlanId());
792 }
Saurav Das822c4e22015-10-23 10:51:11 -0700793 // ofdpa cannot match on ALL portnumber, so we need to use separate
794 // rules for each port.
Yuta HIGUCHI2dce08a2017-04-20 21:57:48 -0700795 List<PortNumber> portnums = new ArrayList<>();
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530796 if (portCriterion != null) {
797 if (PortNumber.ALL.equals(portCriterion.port())) {
798 for (Port port : deviceService.getPorts(deviceId)) {
799 if (port.number().toLong() > 0 && port.number().toLong() < OFPP_MAX) {
800 portnums.add(port.number());
801 }
Saurav Das822c4e22015-10-23 10:51:11 -0700802 }
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530803 } else {
804 portnums.add(portCriterion.port());
805 }
806 for (PortNumber pnum : portnums) {
807 rules.add(buildTmacRuleForIpv4(ethCriterion,
808 vidCriterion,
809 ofdpaMatchVlanVid,
810 applicationId,
811 pnum));
812 rules.add(buildTmacRuleForMpls(ethCriterion,
813 vidCriterion,
814 ofdpaMatchVlanVid,
815 applicationId,
816 pnum));
817 rules.add(buildTmacRuleForIpv6(ethCriterion,
818 vidCriterion,
819 ofdpaMatchVlanVid,
820 applicationId,
821 pnum));
Saurav Das822c4e22015-10-23 10:51:11 -0700822 }
823 } else {
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530824 rules.add(buildTmacRuleForIpv4(ethCriterion,
825 vidCriterion,
826 ofdpaMatchVlanVid,
827 applicationId,
828 null));
829 rules.add(buildTmacRuleForMpls(ethCriterion,
830 vidCriterion,
831 ofdpaMatchVlanVid,
832 applicationId,
833 null));
834 rules.add(buildTmacRuleForIpv6(ethCriterion,
835 vidCriterion,
836 ofdpaMatchVlanVid,
837 applicationId,
838 null));
Saurav Das822c4e22015-10-23 10:51:11 -0700839 }
Charles Chan66291502018-03-02 16:43:28 -0800840 return ImmutableList.of(rules);
Saurav Das822c4e22015-10-23 10:51:11 -0700841 }
842
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530843 /**
844 * Builds TMAC rules for IPv4 packets.
845 *
Charles Chanc550f2e2017-12-19 19:55:57 -0800846 * @param ethCriterion dst mac matching
847 * @param vidCriterion vlan id assigned to the port
848 * @param ofdpaMatchVlanVid OFDPA vlan id matching
849 * @param applicationId application id
850 * @param pnum port number
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530851 * @return TMAC rule for IPV4 packets
852 */
853 private FlowRule buildTmacRuleForIpv4(EthCriterion ethCriterion,
854 VlanIdCriterion vidCriterion,
855 OfdpaMatchVlanVid ofdpaMatchVlanVid,
856 ApplicationId applicationId,
857 PortNumber pnum) {
858 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
859 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
860 if (pnum != null) {
861 if (matchInPortTmacTable()) {
862 selector.matchInPort(pnum);
863 } else {
Saurav Dasf14d9ef2017-12-05 15:00:23 -0800864 log.debug("Pipeline does not support IN_PORT matching in TMAC table, " +
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530865 "ignoring the IN_PORT criteria");
866 }
867 }
868 if (vidCriterion != null) {
869 if (requireVlanExtensions()) {
870 selector.extension(ofdpaMatchVlanVid, deviceId);
871 } else {
872 selector.matchVlanId(vidCriterion.vlanId());
873 }
874 }
875 selector.matchEthType(Ethernet.TYPE_IPV4);
876 selector.matchEthDst(ethCriterion.mac());
877 treatment.transition(UNICAST_ROUTING_TABLE);
878 return DefaultFlowRule.builder()
879 .forDevice(deviceId)
880 .withSelector(selector.build())
881 .withTreatment(treatment.build())
882 .withPriority(DEFAULT_PRIORITY)
883 .fromApp(applicationId)
884 .makePermanent()
885 .forTable(TMAC_TABLE).build();
886 }
887
888 /**
889 * Builds TMAC rule for MPLS packets.
890 *
Charles Chanc550f2e2017-12-19 19:55:57 -0800891 * @param ethCriterion dst mac matching
892 * @param vidCriterion vlan id assigned to the port
893 * @param ofdpaMatchVlanVid OFDPA vlan id matching
894 * @param applicationId application id
895 * @param pnum port number
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530896 * @return TMAC rule for MPLS packets
897 */
898 private FlowRule buildTmacRuleForMpls(EthCriterion ethCriterion,
899 VlanIdCriterion vidCriterion,
900 OfdpaMatchVlanVid ofdpaMatchVlanVid,
901 ApplicationId applicationId,
902 PortNumber pnum) {
903 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
904 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
905 if (pnum != null) {
906 if (matchInPortTmacTable()) {
907 selector.matchInPort(pnum);
908 } else {
Saurav Dasf14d9ef2017-12-05 15:00:23 -0800909 log.debug("Pipeline does not support IN_PORT matching in TMAC table, " +
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530910 "ignoring the IN_PORT criteria");
911 }
912 }
913 if (vidCriterion != null) {
914 if (requireVlanExtensions()) {
915 selector.extension(ofdpaMatchVlanVid, deviceId);
916 } else {
917 selector.matchVlanId(vidCriterion.vlanId());
918 }
919 }
920 selector.matchEthType(Ethernet.MPLS_UNICAST);
921 selector.matchEthDst(ethCriterion.mac());
922 treatment.transition(MPLS_TABLE_0);
923 return DefaultFlowRule.builder()
924 .forDevice(deviceId)
925 .withSelector(selector.build())
926 .withTreatment(treatment.build())
927 .withPriority(DEFAULT_PRIORITY)
928 .fromApp(applicationId)
929 .makePermanent()
930 .forTable(TMAC_TABLE).build();
931 }
932
933 /**
934 * Builds TMAC rules for IPv6 packets.
935 *
Charles Chanc550f2e2017-12-19 19:55:57 -0800936 * @param ethCriterion dst mac matching
937 * @param vidCriterion vlan id assigned to the port
938 * @param ofdpaMatchVlanVid OFDPA vlan id matching
939 * @param applicationId application id
940 * @param pnum port number
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530941 * @return TMAC rule for IPV6 packets
942 */
943 private FlowRule buildTmacRuleForIpv6(EthCriterion ethCriterion,
944 VlanIdCriterion vidCriterion,
945 OfdpaMatchVlanVid ofdpaMatchVlanVid,
946 ApplicationId applicationId,
947 PortNumber pnum) {
948 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
949 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
950 if (pnum != null) {
951 if (matchInPortTmacTable()) {
952 selector.matchInPort(pnum);
953 } else {
Saurav Dasf14d9ef2017-12-05 15:00:23 -0800954 log.debug("Pipeline does not support IN_PORT matching in TMAC table, " +
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530955 "ignoring the IN_PORT criteria");
956 }
957 }
958 if (vidCriterion != null) {
959 if (requireVlanExtensions()) {
960 selector.extension(ofdpaMatchVlanVid, deviceId);
961 } else {
962 selector.matchVlanId(vidCriterion.vlanId());
963 }
964 }
965 selector.matchEthType(Ethernet.TYPE_IPV6);
966 selector.matchEthDst(ethCriterion.mac());
967 treatment.transition(UNICAST_ROUTING_TABLE);
968 return DefaultFlowRule.builder()
969 .forDevice(deviceId)
970 .withSelector(selector.build())
971 .withTreatment(treatment.build())
972 .withPriority(DEFAULT_PRIORITY)
973 .fromApp(applicationId)
974 .makePermanent()
975 .forTable(TMAC_TABLE).build();
976 }
977
Charles Chan66291502018-03-02 16:43:28 -0800978 protected List<List<FlowRule>> processEthDstOnlyFilter(EthCriterion ethCriterion,
Yi Tsengef19de12017-04-24 11:33:05 -0700979 ApplicationId applicationId) {
Pier Luigi0e358632017-01-31 09:35:05 -0800980 ImmutableList.Builder<FlowRule> builder = ImmutableList.builder();
981
Charles Chan5270ed02016-01-30 23:22:37 -0800982 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
983 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
984 selector.matchEthType(Ethernet.TYPE_IPV4);
985 selector.matchEthDst(ethCriterion.mac());
986 treatment.transition(UNICAST_ROUTING_TABLE);
987 FlowRule rule = DefaultFlowRule.builder()
988 .forDevice(deviceId)
989 .withSelector(selector.build())
990 .withTreatment(treatment.build())
991 .withPriority(DEFAULT_PRIORITY)
992 .fromApp(applicationId)
993 .makePermanent()
994 .forTable(TMAC_TABLE).build();
Pier Luigi0e358632017-01-31 09:35:05 -0800995 builder.add(rule);
996
997 selector = DefaultTrafficSelector.builder();
998 treatment = DefaultTrafficTreatment.builder();
999 selector.matchEthType(Ethernet.TYPE_IPV6);
1000 selector.matchEthDst(ethCriterion.mac());
1001 treatment.transition(UNICAST_ROUTING_TABLE);
1002 rule = DefaultFlowRule.builder()
1003 .forDevice(deviceId)
1004 .withSelector(selector.build())
1005 .withTreatment(treatment.build())
1006 .withPriority(DEFAULT_PRIORITY)
1007 .fromApp(applicationId)
1008 .makePermanent()
1009 .forTable(TMAC_TABLE).build();
Charles Chan66291502018-03-02 16:43:28 -08001010 return ImmutableList.of(builder.add(rule).build());
Charles Chan5270ed02016-01-30 23:22:37 -08001011 }
1012
pier5e8b2bc2019-10-04 10:54:07 +02001013 private FlowRule buildTmacRuleForMcastFromUnicast(FlowRule tmacRuleForUnicast, ApplicationId applicationId) {
1014 final TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
1015 final TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
1016 FlowRule rule;
1017 // Build the selector
1018 for (Criterion criterion : tmacRuleForUnicast.selector().criteria()) {
1019 if (criterion instanceof VlanIdCriterion) {
1020 if (requireVlanExtensions()) {
1021 OfdpaMatchVlanVid ofdpaMatchVlanVid = new OfdpaMatchVlanVid(((VlanIdCriterion) criterion).vlanId());
1022 selector.extension(ofdpaMatchVlanVid, deviceId);
1023 } else {
1024 selector.add(criterion);
1025 }
1026 } else if (criterion instanceof EthTypeCriterion) {
1027 selector.add(criterion);
1028 if (Objects.equals(((EthTypeCriterion) criterion).ethType(),
1029 EtherType.IPV4.ethType())) {
1030 selector.matchEthDstMasked(IPV4_MULTICAST, IPV4_MULTICAST_MASK);
1031 } else if (Objects.equals(((EthTypeCriterion) criterion).ethType(),
1032 EtherType.IPV6.ethType())) {
1033 selector.matchEthDstMasked(IPV6_MULTICAST, IPV6_MULTICAST_MASK);
1034 } else {
1035 // We don't need for mpls rules
1036 return null;
1037 }
1038 }
1039 }
1040 // Build the treatment
1041 treatment.transition(MULTICAST_ROUTING_TABLE);
1042 // Build the flowrule
1043 rule = DefaultFlowRule.builder()
1044 .forDevice(deviceId)
1045 .withSelector(selector.build())
1046 .withTreatment(treatment.build())
1047 .withPriority(DEFAULT_PRIORITY)
1048 .fromApp(applicationId)
1049 .makePermanent()
1050 .forTable(TMAC_TABLE).build();
pier776ce252019-12-06 12:39:51 +01001051 log.debug("Building TMac Mcast flowRule {}", rule);
pier5e8b2bc2019-10-04 10:54:07 +02001052 return rule;
1053 }
1054
Charles Chan66291502018-03-02 16:43:28 -08001055 List<List<FlowRule>> processMcastEthDstFilter(EthCriterion ethCriterion,
Charles Chanb4879a52017-10-20 19:09:16 -07001056 VlanId assignedVlan,
Charles Chan45b69ab2018-03-02 15:41:41 -08001057 MacAddress unicastMac,
Yi Tsengef19de12017-04-24 11:33:05 -07001058 ApplicationId applicationId) {
Charles Chan45b69ab2018-03-02 15:41:41 -08001059 ImmutableList.Builder<FlowRule> unicastFlows = ImmutableList.builder();
1060 ImmutableList.Builder<FlowRule> multicastFlows = ImmutableList.builder();
1061 TrafficSelector.Builder selector;
1062 TrafficTreatment.Builder treatment;
Charles Chanb4879a52017-10-20 19:09:16 -07001063 FlowRule rule;
Julia Ferguson65428c32017-08-10 18:15:24 +00001064
Charles Chanb4879a52017-10-20 19:09:16 -07001065 if (IPV4_MULTICAST.equals(ethCriterion.mac())) {
Charles Chan45b69ab2018-03-02 15:41:41 -08001066 if (requireUnicastBeforeMulticast()) {
1067 selector = DefaultTrafficSelector.builder();
1068 treatment = DefaultTrafficTreatment.builder();
1069 selector.matchEthType(Ethernet.TYPE_IPV4);
1070 selector.matchEthDst(unicastMac);
Charles Chan056e0c12018-05-10 22:19:49 +00001071 selector.matchVlanId(assignedVlan);
Charles Chan45b69ab2018-03-02 15:41:41 -08001072 treatment.transition(UNICAST_ROUTING_TABLE);
1073 rule = DefaultFlowRule.builder()
1074 .forDevice(deviceId)
1075 .withSelector(selector.build())
1076 .withTreatment(treatment.build())
Charles Chan056e0c12018-05-10 22:19:49 +00001077 .withPriority(DEFAULT_PRIORITY)
Charles Chan45b69ab2018-03-02 15:41:41 -08001078 .fromApp(applicationId)
1079 .makePermanent()
1080 .forTable(TMAC_TABLE).build();
1081 unicastFlows.add(rule);
1082 }
1083
1084 selector = DefaultTrafficSelector.builder();
1085 treatment = DefaultTrafficTreatment.builder();
Charles Chanb4879a52017-10-20 19:09:16 -07001086 selector.matchEthType(Ethernet.TYPE_IPV4);
1087 selector.matchEthDstMasked(ethCriterion.mac(), ethCriterion.mask());
Charles Chan056e0c12018-05-10 22:19:49 +00001088 selector.matchVlanId(assignedVlan);
Charles Chanb4879a52017-10-20 19:09:16 -07001089 treatment.transition(MULTICAST_ROUTING_TABLE);
1090 rule = DefaultFlowRule.builder()
1091 .forDevice(deviceId)
1092 .withSelector(selector.build())
1093 .withTreatment(treatment.build())
Charles Chan056e0c12018-05-10 22:19:49 +00001094 .withPriority(DEFAULT_PRIORITY)
Charles Chanb4879a52017-10-20 19:09:16 -07001095 .fromApp(applicationId)
1096 .makePermanent()
1097 .forTable(TMAC_TABLE).build();
Charles Chan45b69ab2018-03-02 15:41:41 -08001098 multicastFlows.add(rule);
Charles Chanb4879a52017-10-20 19:09:16 -07001099 }
1100
1101 if (IPV6_MULTICAST.equals(ethCriterion.mac())) {
Charles Chan45b69ab2018-03-02 15:41:41 -08001102 if (requireUnicastBeforeMulticast()) {
1103 selector = DefaultTrafficSelector.builder();
1104 treatment = DefaultTrafficTreatment.builder();
1105 selector.matchEthType(Ethernet.TYPE_IPV6);
1106 selector.matchEthDst(unicastMac);
Charles Chan056e0c12018-05-10 22:19:49 +00001107 selector.matchVlanId(assignedVlan);
Charles Chan45b69ab2018-03-02 15:41:41 -08001108 treatment.transition(UNICAST_ROUTING_TABLE);
1109 rule = DefaultFlowRule.builder()
1110 .forDevice(deviceId)
1111 .withSelector(selector.build())
1112 .withTreatment(treatment.build())
Charles Chan056e0c12018-05-10 22:19:49 +00001113 .withPriority(DEFAULT_PRIORITY)
Charles Chan45b69ab2018-03-02 15:41:41 -08001114 .fromApp(applicationId)
1115 .makePermanent()
1116 .forTable(TMAC_TABLE).build();
1117 unicastFlows.add(rule);
1118 }
1119
Charles Chanb4879a52017-10-20 19:09:16 -07001120 selector = DefaultTrafficSelector.builder();
1121 treatment = DefaultTrafficTreatment.builder();
1122 selector.matchEthType(Ethernet.TYPE_IPV6);
1123 selector.matchEthDstMasked(ethCriterion.mac(), ethCriterion.mask());
Charles Chan056e0c12018-05-10 22:19:49 +00001124 selector.matchVlanId(assignedVlan);
Charles Chanb4879a52017-10-20 19:09:16 -07001125 treatment.transition(MULTICAST_ROUTING_TABLE);
1126 rule = DefaultFlowRule.builder()
1127 .forDevice(deviceId)
1128 .withSelector(selector.build())
1129 .withTreatment(treatment.build())
Charles Chan056e0c12018-05-10 22:19:49 +00001130 .withPriority(DEFAULT_PRIORITY)
Charles Chanb4879a52017-10-20 19:09:16 -07001131 .fromApp(applicationId)
1132 .makePermanent()
1133 .forTable(TMAC_TABLE).build();
Charles Chan45b69ab2018-03-02 15:41:41 -08001134 multicastFlows.add(rule);
Charles Chanb4879a52017-10-20 19:09:16 -07001135 }
Charles Chan45b69ab2018-03-02 15:41:41 -08001136 return ImmutableList.of(unicastFlows.build(), multicastFlows.build());
Charles Chan5b9df8d2016-03-28 22:21:40 -07001137 }
1138
Saurav Das822c4e22015-10-23 10:51:11 -07001139 private Collection<FlowRule> processForward(ForwardingObjective fwd) {
1140 switch (fwd.flag()) {
1141 case SPECIFIC:
1142 return processSpecific(fwd);
1143 case VERSATILE:
1144 return processVersatile(fwd);
Jonghwan Hyun800d9d02018-04-09 09:40:50 -07001145 case EGRESS:
1146 return processEgress(fwd);
Saurav Das822c4e22015-10-23 10:51:11 -07001147 default:
1148 fail(fwd, ObjectiveError.UNKNOWN);
1149 log.warn("Unknown forwarding flag {}", fwd.flag());
1150 }
1151 return Collections.emptySet();
1152 }
1153
1154 /**
Jonghwan Hyun800d9d02018-04-09 09:40:50 -07001155 * In the OF-DPA 2.0 pipeline, egress forwarding objectives go to the
1156 * egress tables.
1157 * @param fwd the forwarding objective of type 'egress'
1158 * @return a collection of flow rules to be sent to the switch. An empty
1159 * collection may be returned if there is a problem in processing
1160 * the flow rule
1161 */
1162 protected Collection<FlowRule> processEgress(ForwardingObjective fwd) {
1163 log.debug("Processing egress forwarding objective:{} in dev:{}",
1164 fwd, deviceId);
1165
1166 List<FlowRule> rules = new ArrayList<>();
1167
1168 // Build selector
1169 TrafficSelector.Builder sb = DefaultTrafficSelector.builder();
1170 VlanIdCriterion vlanIdCriterion = (VlanIdCriterion) fwd.selector().getCriterion(Criterion.Type.VLAN_VID);
1171 if (vlanIdCriterion == null) {
1172 log.error("Egress forwarding objective:{} must include vlanId", fwd.id());
1173 fail(fwd, ObjectiveError.BADPARAMS);
1174 return rules;
1175 }
1176
1177 Optional<Instruction> outInstr = fwd.treatment().allInstructions().stream()
1178 .filter(instruction -> instruction instanceof OutputInstruction).findFirst();
1179 if (!outInstr.isPresent()) {
1180 log.error("Egress forwarding objective:{} must include output port", fwd.id());
1181 fail(fwd, ObjectiveError.BADPARAMS);
1182 return rules;
1183 }
1184
1185 PortNumber portNumber = ((OutputInstruction) outInstr.get()).port();
1186
1187 sb.matchVlanId(vlanIdCriterion.vlanId());
1188 OfdpaMatchActsetOutput actsetOutput = new OfdpaMatchActsetOutput(portNumber);
1189 sb.extension(actsetOutput, deviceId);
1190
1191 sb.extension(new OfdpaMatchAllowVlanTranslation(ALLOW_VLAN_TRANSLATION), deviceId);
1192
1193 // Build a flow rule for Egress VLAN Flow table
1194 TrafficTreatment.Builder tb = DefaultTrafficTreatment.builder();
1195 tb.transition(EGRESS_DSCP_PCP_REMARK_FLOW_TABLE);
1196 if (fwd.treatment() != null) {
1197 for (Instruction instr : fwd.treatment().allInstructions()) {
1198 if (instr instanceof L2ModificationInstruction &&
1199 ((L2ModificationInstruction) instr).subtype() == L2SubType.VLAN_ID) {
1200 tb.immediate().add(instr);
1201 }
1202 if (instr instanceof L2ModificationInstruction &&
1203 ((L2ModificationInstruction) instr).subtype() == L2SubType.VLAN_PUSH) {
1204 tb.immediate().pushVlan();
1205 EthType ethType = ((L2ModificationInstruction.ModVlanHeaderInstruction) instr).ethernetType();
1206 if (ethType.equals(EtherType.QINQ.ethType())) {
1207 // Build a flow rule for Egress TPID Flow table
1208 TrafficSelector tpidSelector = DefaultTrafficSelector.builder()
1209 .extension(actsetOutput, deviceId)
1210 .matchVlanId(VlanId.ANY).build();
1211
1212 TrafficTreatment tpidTreatment = DefaultTrafficTreatment.builder()
1213 .extension(new Ofdpa3CopyField(COPY_FIELD_NBITS, COPY_FIELD_OFFSET,
1214 COPY_FIELD_OFFSET, OXM_ID_VLAN_VID,
1215 OXM_ID_PACKET_REG_1),
1216 deviceId)
1217 .popVlan()
1218 .pushVlan(EtherType.QINQ.ethType())
1219 .extension(new Ofdpa3CopyField(COPY_FIELD_NBITS, COPY_FIELD_OFFSET,
1220 COPY_FIELD_OFFSET, OXM_ID_PACKET_REG_1,
1221 OXM_ID_VLAN_VID),
1222 deviceId)
1223 .build();
1224
1225 FlowRule.Builder tpidRuleBuilder = DefaultFlowRule.builder()
1226 .fromApp(fwd.appId())
1227 .withPriority(fwd.priority())
1228 .forDevice(deviceId)
1229 .withSelector(tpidSelector)
1230 .withTreatment(tpidTreatment)
1231 .makePermanent()
1232 .forTable(EGRESS_TPID_FLOW_TABLE);
1233 rules.add(tpidRuleBuilder.build());
1234 }
1235 }
1236 }
1237 }
1238
1239 FlowRule.Builder ruleBuilder = DefaultFlowRule.builder()
1240 .fromApp(fwd.appId())
1241 .withPriority(fwd.priority())
1242 .forDevice(deviceId)
1243 .withSelector(sb.build())
1244 .withTreatment(tb.build())
1245 .makePermanent()
1246 .forTable(EGRESS_VLAN_FLOW_TABLE);
1247 rules.add(ruleBuilder.build());
1248 return rules;
1249 }
1250
1251 /**
Saurav Das822c4e22015-10-23 10:51:11 -07001252 * In the OF-DPA 2.0 pipeline, versatile forwarding objectives go to the
1253 * ACL table.
1254 * @param fwd the forwarding objective of type 'versatile'
1255 * @return a collection of flow rules to be sent to the switch. An empty
1256 * collection may be returned if there is a problem in processing
1257 * the flow rule
1258 */
Saurav Das52025962016-01-28 22:30:01 -08001259 protected Collection<FlowRule> processVersatile(ForwardingObjective fwd) {
Saurav Das018605f2017-02-18 14:05:44 -08001260 log.debug("Processing versatile forwarding objective:{} in dev:{}",
Saurav Dasd2fded02016-12-02 15:43:47 -08001261 fwd.id(), deviceId);
Harshada Chaundkar77958a52019-08-05 15:33:30 +00001262 List<FlowRule> flowRules = new ArrayList<>();
1263 final AtomicBoolean ethTypeUsed = new AtomicBoolean(false);
Saurav Das822c4e22015-10-23 10:51:11 -07001264
Saurav Das822c4e22015-10-23 10:51:11 -07001265 if (fwd.nextId() == null && fwd.treatment() == null) {
1266 log.error("Forwarding objective {} from {} must contain "
1267 + "nextId or Treatment", fwd.selector(), fwd.appId());
Zsolt Haraszti9faab752016-02-17 15:55:20 -08001268 fail(fwd, ObjectiveError.BADPARAMS);
Saurav Das822c4e22015-10-23 10:51:11 -07001269 return Collections.emptySet();
1270 }
Saurav Das49cb5a12016-01-16 22:54:07 -08001271
Saurav Das77b5e902016-01-27 17:01:59 -08001272 TrafficSelector.Builder sbuilder = DefaultTrafficSelector.builder();
1273 fwd.selector().criteria().forEach(criterion -> {
1274 if (criterion instanceof VlanIdCriterion) {
1275 VlanId vlanId = ((VlanIdCriterion) criterion).vlanId();
1276 // ensure that match does not include vlan = NONE as OF-DPA does not
1277 // match untagged packets this way in the ACL table.
1278 if (vlanId.equals(VlanId.NONE)) {
1279 return;
1280 }
Charles Chand1172632017-03-15 17:33:09 -07001281 if (requireVlanExtensions()) {
1282 OfdpaMatchVlanVid ofdpaMatchVlanVid = new OfdpaMatchVlanVid(vlanId);
1283 sbuilder.extension(ofdpaMatchVlanVid, deviceId);
1284 } else {
1285 sbuilder.matchVlanId(vlanId);
1286 }
Charles Chan09bf2692018-01-11 11:48:18 -08001287 } else if (criterion instanceof Icmpv6TypeCriterion) {
1288 byte icmpv6Type = (byte) ((Icmpv6TypeCriterion) criterion).icmpv6Type();
1289 sbuilder.matchIcmpv6Type(icmpv6Type);
1290 } else if (criterion instanceof Icmpv6CodeCriterion) {
1291 byte icmpv6Code = (byte) ((Icmpv6CodeCriterion) criterion).icmpv6Code();
1292 sbuilder.matchIcmpv6Type(icmpv6Code);
Charles Chand9e47c62017-10-05 15:17:15 -07001293 } else if (criterion instanceof TcpPortCriterion || criterion instanceof UdpPortCriterion) {
1294 // FIXME: QMX switches do not support L4 dst port matching in ACL table.
1295 // Currently L4 dst port matching is only used by DHCP relay feature
1296 // and therefore is safe to be replaced with L4 src port matching.
1297 // We need to revisit this if L4 dst port is used for other purpose in the future.
1298 if (!supportIpv6L4Dst() && isIpv6(fwd.selector())) {
Charles Chand9e47c62017-10-05 15:17:15 -07001299 switch (criterion.type()) {
1300 case UDP_DST:
Charles Chand9e47c62017-10-05 15:17:15 -07001301 case UDP_DST_MASKED:
Charles Chand9e47c62017-10-05 15:17:15 -07001302 case TCP_DST:
Charles Chand9e47c62017-10-05 15:17:15 -07001303 case TCP_DST_MASKED:
Charles Chand9e47c62017-10-05 15:17:15 -07001304 break;
1305 default:
1306 sbuilder.add(criterion);
1307 }
1308 } else {
1309 sbuilder.add(criterion);
1310 }
Harshada Chaundkar77958a52019-08-05 15:33:30 +00001311 } else if (criterion instanceof EthTypeCriterion) {
1312 sbuilder.add(criterion);
1313 ethTypeUsed.set(true);
Saurav Das77b5e902016-01-27 17:01:59 -08001314 } else {
1315 sbuilder.add(criterion);
1316 }
1317 });
Harshada Chaundkar77958a52019-08-05 15:33:30 +00001318
1319 TrafficTreatment.Builder ttBuilder = versatileTreatmentBuilder(fwd);
1320 if (ttBuilder == null) {
1321 return Collections.emptySet();
1322 }
1323
1324 FlowRule.Builder ruleBuilder = DefaultFlowRule.builder()
1325 .fromApp(fwd.appId())
1326 .withPriority(fwd.priority())
1327 .forDevice(deviceId)
1328 .withSelector(sbuilder.build())
1329 .withTreatment(ttBuilder.build())
1330 .makePermanent()
1331 .forTable(ACL_TABLE);
1332
1333 flowRules.add(ruleBuilder.build());
1334
1335 if (!ethTypeUsed.get() && requireEthType()) {
1336 log.debug("{} doesn't match on ethType but requireEthType is true, adding complementary ACL flow.",
1337 sbuilder.toString());
1338 sbuilder.matchEthType(Ethernet.TYPE_IPV6);
1339 FlowRule.Builder ethTypeRuleBuilder = DefaultFlowRule.builder()
1340 .fromApp(fwd.appId())
1341 .withPriority(fwd.priority())
1342 .forDevice(deviceId)
1343 .withSelector(sbuilder.build())
1344 .withTreatment(ttBuilder.build())
1345 .makePermanent()
1346 .forTable(ACL_TABLE);
1347 flowRules.add(ethTypeRuleBuilder.build());
1348 }
1349 return flowRules;
Charles Chanab591602019-01-22 17:25:04 -08001350 }
Saurav Das77b5e902016-01-27 17:01:59 -08001351
Charles Chanab591602019-01-22 17:25:04 -08001352 /**
1353 * Helper function to create traffic treatment builder for versatile forwarding objectives.
1354 *
1355 * @param fwd original forwarding objective
1356 * @return treatment builder for the flow rule, or null if there is an error.
1357 */
1358 protected TrafficTreatment.Builder versatileTreatmentBuilder(ForwardingObjective fwd) {
Saurav Das822c4e22015-10-23 10:51:11 -07001359 // XXX driver does not currently do type checking as per Tables 65-67 in
1360 // OFDPA 2.0 spec. The only allowed treatment is a punt to the controller.
Saurav Das49cb5a12016-01-16 22:54:07 -08001361 TrafficTreatment.Builder ttBuilder = DefaultTrafficTreatment.builder();
1362 if (fwd.treatment() != null) {
1363 for (Instruction ins : fwd.treatment().allInstructions()) {
1364 if (ins instanceof OutputInstruction) {
1365 OutputInstruction o = (OutputInstruction) ins;
Ray Milkeyfe0e0852018-01-18 11:14:05 -08001366 if (PortNumber.CONTROLLER.equals(o.port())) {
Saurav Das49cb5a12016-01-16 22:54:07 -08001367 ttBuilder.add(o);
1368 } else {
1369 log.warn("Only allowed treatments in versatile forwarding "
1370 + "objectives are punts to the controller");
1371 }
Charles Chanf76de302018-06-15 18:54:18 -07001372 } else if (ins instanceof NoActionInstruction) {
1373 // No action is allowed and nothing needs to be done
Saurav Das49cb5a12016-01-16 22:54:07 -08001374 } else {
1375 log.warn("Cannot process instruction in versatile fwd {}", ins);
1376 }
Saurav Das822c4e22015-10-23 10:51:11 -07001377 }
Charles Chan2df0e8a2017-01-09 11:45:08 -08001378 if (fwd.treatment().clearedDeferred()) {
1379 ttBuilder.wipeDeferred();
1380 }
Saurav Das822c4e22015-10-23 10:51:11 -07001381 }
Saurav Das822c4e22015-10-23 10:51:11 -07001382 if (fwd.nextId() != null) {
Charles Chanab591602019-01-22 17:25:04 -08001383 // Override case
Saurav Das49cb5a12016-01-16 22:54:07 -08001384 NextGroup next = getGroupForNextObjective(fwd.nextId());
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +05301385 if (next == null) {
1386 fail(fwd, ObjectiveError.BADPARAMS);
Charles Chanab591602019-01-22 17:25:04 -08001387 return null;
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +05301388 }
Saurav Das49cb5a12016-01-16 22:54:07 -08001389 List<Deque<GroupKey>> gkeys = appKryo.deserialize(next.data());
1390 // we only need the top level group's key to point the flow to it
1391 Group group = groupService.getGroup(deviceId, gkeys.get(0).peekFirst());
1392 if (group == null) {
1393 log.warn("Group with key:{} for next-id:{} not found in dev:{}",
Charles Chanab591602019-01-22 17:25:04 -08001394 gkeys.get(0).peekFirst(), fwd.nextId(), deviceId);
Saurav Das49cb5a12016-01-16 22:54:07 -08001395 fail(fwd, ObjectiveError.GROUPMISSING);
Charles Chanab591602019-01-22 17:25:04 -08001396 return null;
Saurav Das49cb5a12016-01-16 22:54:07 -08001397 }
1398 ttBuilder.deferred().group(group.id());
Saurav Das822c4e22015-10-23 10:51:11 -07001399 }
Charles Chanab591602019-01-22 17:25:04 -08001400 return ttBuilder;
Saurav Das822c4e22015-10-23 10:51:11 -07001401 }
1402
1403 /**
1404 * In the OF-DPA 2.0 pipeline, specific forwarding refers to the IP table
Saurav Das8a0732e2015-11-20 15:27:53 -08001405 * (unicast or multicast) or the L2 table (mac + vlan) or the MPLS table.
Saurav Das822c4e22015-10-23 10:51:11 -07001406 *
1407 * @param fwd the forwarding objective of type 'specific'
1408 * @return a collection of flow rules. Typically there will be only one
1409 * for this type of forwarding objective. An empty set may be
1410 * returned if there is an issue in processing the objective.
1411 */
Charles Chan50d900c2018-03-02 13:26:22 -08001412 private Collection<FlowRule> processSpecific(ForwardingObjective fwd) {
Saurav Das25190812016-05-27 13:54:07 -07001413 log.debug("Processing specific fwd objective:{} in dev:{} with next:{}",
Saurav Das4ce45962015-11-24 23:21:05 -08001414 fwd.id(), deviceId, fwd.nextId());
1415 boolean isEthTypeObj = isSupportedEthTypeObjective(fwd);
1416 boolean isEthDstObj = isSupportedEthDstObjective(fwd);
1417
1418 if (isEthTypeObj) {
1419 return processEthTypeSpecific(fwd);
1420 } else if (isEthDstObj) {
1421 return processEthDstSpecific(fwd);
1422 } else {
1423 log.warn("processSpecific: Unsupported forwarding objective "
1424 + "criteria fwd:{} in dev:{}", fwd.nextId(), deviceId);
Saurav Das822c4e22015-10-23 10:51:11 -07001425 fail(fwd, ObjectiveError.UNSUPPORTED);
1426 return Collections.emptySet();
1427 }
Saurav Das4ce45962015-11-24 23:21:05 -08001428 }
1429
Saurav Das4ce45962015-11-24 23:21:05 -08001430 /**
1431 * Handles forwarding rules to the IP and MPLS tables.
1432 *
1433 * @param fwd the forwarding objective
1434 * @return A collection of flow rules, or an empty set
1435 */
1436 protected Collection<FlowRule> processEthTypeSpecific(ForwardingObjective fwd) {
Charles Chancad338a2016-09-16 18:03:11 -07001437 return processEthTypeSpecificInternal(fwd, false, ACL_TABLE);
Charles Chanf9e98652016-09-07 16:54:23 -07001438 }
1439
1440 /**
1441 * Internal implementation of processEthTypeSpecific.
1442 * <p>
1443 * Wildcarded IPv4_DST is not supported in OFDPA i12. Therefore, we break
1444 * the rule into 0.0.0.0/1 and 128.0.0.0/1.
1445 * The allowDefaultRoute must be set to false for OFDPA i12.
1446 * </p>
1447 *
1448 * @param fwd the forwarding objective
1449 * @param allowDefaultRoute allow wildcarded IPv4_DST or not
Ray Milkey0bb1e102016-11-10 14:51:27 -08001450 * @param mplsNextTable next MPLS table
Charles Chanf9e98652016-09-07 16:54:23 -07001451 * @return A collection of flow rules, or an empty set
1452 */
1453 protected Collection<FlowRule> processEthTypeSpecificInternal(ForwardingObjective fwd,
Charles Chancad338a2016-09-16 18:03:11 -07001454 boolean allowDefaultRoute,
1455 int mplsNextTable) {
Saurav Das4ce45962015-11-24 23:21:05 -08001456 TrafficSelector selector = fwd.selector();
1457 EthTypeCriterion ethType =
1458 (EthTypeCriterion) selector.getCriterion(Criterion.Type.ETH_TYPE);
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001459 boolean emptyGroup = false;
Charles Chan188ebf52015-12-23 00:15:11 -08001460 int forTableId;
Saurav Das8a0732e2015-11-20 15:27:53 -08001461 TrafficSelector.Builder filteredSelector = DefaultTrafficSelector.builder();
Charles Chan14967c22015-12-07 11:11:50 -08001462 TrafficTreatment.Builder tb = DefaultTrafficTreatment.builder();
Flavio Castroe10fa242016-01-15 12:43:51 -08001463 TrafficSelector.Builder complementarySelector = DefaultTrafficSelector.builder();
Charles Chan14967c22015-12-07 11:11:50 -08001464
Saurav Das8a0732e2015-11-20 15:27:53 -08001465 if (ethType.ethType().toShort() == Ethernet.TYPE_IPV4) {
Pier Luigi3bfe32c2017-01-30 09:47:36 -08001466 if (buildIpv4Selector(filteredSelector, complementarySelector, fwd, allowDefaultRoute) < 0) {
1467 return Collections.emptyList();
1468 }
1469 // We need to set properly the next table
Flavio Castroe10fa242016-01-15 12:43:51 -08001470 IpPrefix ipv4Dst = ((IPCriterion) selector.getCriterion(Criterion.Type.IPV4_DST)).ip();
Charles Chan5b9df8d2016-03-28 22:21:40 -07001471 if (ipv4Dst.isMulticast()) {
Charles Chan5b9df8d2016-03-28 22:21:40 -07001472 forTableId = MULTICAST_ROUTING_TABLE;
Flavio Castroe10fa242016-01-15 12:43:51 -08001473 } else {
Charles Chan5b9df8d2016-03-28 22:21:40 -07001474 forTableId = UNICAST_ROUTING_TABLE;
Flavio Castroe10fa242016-01-15 12:43:51 -08001475 }
Charles Chan50d900c2018-03-02 13:26:22 -08001476
Charles Chan236653d2018-03-05 11:28:23 -08001477 // TODO decrementing IP ttl is done automatically for routing, this
Charles Chan50d900c2018-03-02 13:26:22 -08001478 // action is ignored or rejected in ofdpa as it is not fully implemented
Charles Chan236653d2018-03-05 11:28:23 -08001479 // if (fwd.treatment() != null) {
1480 // for (Instruction instr : fwd.treatment().allInstructions()) {
1481 // if (instr instanceof L3ModificationInstruction &&
1482 // ((L3ModificationInstruction) instr).subtype() == L3SubType.DEC_TTL) {
1483 // tb.deferred().add(instr);
1484 // }
1485 // }
1486 // }
1487
Pier Ventree0ae7a32016-11-23 09:57:42 -08001488 } else if (ethType.ethType().toShort() == Ethernet.TYPE_IPV6) {
1489 if (buildIpv6Selector(filteredSelector, fwd) < 0) {
1490 return Collections.emptyList();
1491 }
Kalhee Kim3d26bf02017-10-04 14:59:11 +00001492 //We need to set the proper next table
1493 IpPrefix ipv6Dst = ((IPCriterion) selector.getCriterion(Criterion.Type.IPV6_DST)).ip();
1494 if (ipv6Dst.isMulticast()) {
1495 forTableId = MULTICAST_ROUTING_TABLE;
1496 } else {
1497 forTableId = UNICAST_ROUTING_TABLE;
1498 }
1499
Charles Chan236653d2018-03-05 11:28:23 -08001500 // TODO decrementing IP ttl is done automatically for routing, this
Charles Chan50d900c2018-03-02 13:26:22 -08001501 // action is ignored or rejected in ofdpa as it is not fully implemented
Charles Chan236653d2018-03-05 11:28:23 -08001502 // if (fwd.treatment() != null) {
1503 // for (Instruction instr : fwd.treatment().allInstructions()) {
1504 // if (instr instanceof L3ModificationInstruction &&
1505 // ((L3ModificationInstruction) instr).subtype() == L3SubType.DEC_TTL) {
1506 // tb.deferred().add(instr);
1507 // }
1508 // }
1509 // }
Saurav Das8a0732e2015-11-20 15:27:53 -08001510 } else {
1511 filteredSelector
1512 .matchEthType(Ethernet.MPLS_UNICAST)
1513 .matchMplsLabel(((MplsCriterion)
1514 selector.getCriterion(Criterion.Type.MPLS_LABEL)).label());
1515 MplsBosCriterion bos = (MplsBosCriterion) selector
Pier Ventre140a8942016-11-02 07:26:38 -07001516 .getCriterion(MPLS_BOS);
Alex Yashchuk4caa8e82017-12-08 17:40:05 +02001517 if (bos != null && requireMplsBosMatch()) {
Saurav Das8a0732e2015-11-20 15:27:53 -08001518 filteredSelector.matchMplsBos(bos.mplsBos());
1519 }
1520 forTableId = MPLS_TABLE_1;
Saurav Das4ce45962015-11-24 23:21:05 -08001521 log.debug("processing MPLS specific forwarding objective {} -> next:{}"
1522 + " in dev {}", fwd.id(), fwd.nextId(), deviceId);
Saurav Das822c4e22015-10-23 10:51:11 -07001523
Charles Chan14967c22015-12-07 11:11:50 -08001524 if (fwd.treatment() != null) {
1525 for (Instruction instr : fwd.treatment().allInstructions()) {
1526 if (instr instanceof L2ModificationInstruction &&
1527 ((L2ModificationInstruction) instr).subtype() == L2SubType.MPLS_POP) {
Pier Luigi3bfe32c2017-01-30 09:47:36 -08001528 // OF-DPA does not pop in MPLS table in some cases. For the L3 VPN, it requires
Saurav Das9c705342017-01-06 11:36:01 -08001529 // setting the MPLS_TYPE so pop can happen down the pipeline
Alex Yashchuk4caa8e82017-12-08 17:40:05 +02001530 if (requireMplsPop()) {
1531 if (mplsNextTable == MPLS_TYPE_TABLE && isNotMplsBos(selector)) {
1532 tb.immediate().popMpls();
1533 }
1534 } else {
1535 // Skip mpls pop action for mpls_unicast label
1536 if (instr instanceof ModMplsHeaderInstruction &&
1537 !((ModMplsHeaderInstruction) instr).ethernetType()
1538 .equals(EtherType.MPLS_UNICAST.ethType())) {
1539 tb.immediate().add(instr);
1540 }
Pier Luigi3bfe32c2017-01-30 09:47:36 -08001541 }
Charles Chan14967c22015-12-07 11:11:50 -08001542 }
Alex Yashchuk4caa8e82017-12-08 17:40:05 +02001543
1544 if (requireMplsTtlModification()) {
1545 if (instr instanceof L3ModificationInstruction &&
1546 ((L3ModificationInstruction) instr).subtype() == L3SubType.DEC_TTL) {
1547 // FIXME Should modify the app to send the correct DEC_MPLS_TTL instruction
1548 tb.immediate().decMplsTtl();
1549 }
1550 if (instr instanceof L3ModificationInstruction &&
1551 ((L3ModificationInstruction) instr).subtype() == L3SubType.TTL_IN) {
1552 tb.immediate().add(instr);
1553 }
Charles Chan14967c22015-12-07 11:11:50 -08001554 }
Saurav Das8a0732e2015-11-20 15:27:53 -08001555 }
1556 }
1557 }
Saurav Das822c4e22015-10-23 10:51:11 -07001558
1559 if (fwd.nextId() != null) {
Saurav Das423fe2b2015-12-04 10:52:59 -08001560 NextGroup next = getGroupForNextObjective(fwd.nextId());
1561 if (next != null) {
1562 List<Deque<GroupKey>> gkeys = appKryo.deserialize(next.data());
1563 // we only need the top level group's key to point the flow to it
1564 Group group = groupService.getGroup(deviceId, gkeys.get(0).peekFirst());
1565 if (group == null) {
1566 log.warn("Group with key:{} for next-id:{} not found in dev:{}",
1567 gkeys.get(0).peekFirst(), fwd.nextId(), deviceId);
1568 fail(fwd, ObjectiveError.GROUPMISSING);
1569 return Collections.emptySet();
1570 }
Saurav Dasa4020382018-02-14 14:14:54 -08001571 if (isNotMplsBos(selector) && group.type().equals(SELECT)) {
1572 log.warn("SR CONTINUE case cannot be handled as MPLS ECMP "
1573 + "is not implemented in OF-DPA yet. Aborting flow {} -> next:{} "
1574 + "in this device {}", fwd.id(), fwd.nextId(), deviceId);
1575 fail(fwd, ObjectiveError.FLOWINSTALLATIONFAILED);
1576 return Collections.emptySet();
1577 }
Saurav Das423fe2b2015-12-04 10:52:59 -08001578 tb.deferred().group(group.id());
Saurav Dasc568c342018-01-25 09:49:01 -08001579 // retrying flows may be necessary due to bug CORD-554
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001580 if (gkeys.size() == 1 && gkeys.get(0).size() == 1) {
Saurav Dasc568c342018-01-25 09:49:01 -08001581 if (shouldRetry()) {
1582 log.warn("Found empty group 0x{} in dev:{} .. will retry fwd:{}",
1583 Integer.toHexString(group.id().id()), deviceId,
1584 fwd.id());
1585 emptyGroup = true;
1586 }
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001587 }
Saurav Das25190812016-05-27 13:54:07 -07001588 } else {
1589 log.warn("Cannot find group for nextId:{} in dev:{}. Aborting fwd:{}",
1590 fwd.nextId(), deviceId, fwd.id());
1591 fail(fwd, ObjectiveError.FLOWINSTALLATIONFAILED);
1592 return Collections.emptySet();
Saurav Das822c4e22015-10-23 10:51:11 -07001593 }
Saurav Das822c4e22015-10-23 10:51:11 -07001594 }
Charles Chancad338a2016-09-16 18:03:11 -07001595
1596 if (forTableId == MPLS_TABLE_1) {
Pier Ventre140a8942016-11-02 07:26:38 -07001597 if (mplsNextTable == MPLS_L3_TYPE_TABLE) {
Charles Chancad338a2016-09-16 18:03:11 -07001598 Ofdpa3SetMplsType setMplsType = new Ofdpa3SetMplsType(Ofdpa3MplsType.L3_PHP);
Saurav Das9c705342017-01-06 11:36:01 -08001599 // set mpls type as apply_action
1600 tb.immediate().extension(setMplsType, deviceId);
Charles Chancad338a2016-09-16 18:03:11 -07001601 }
1602 tb.transition(mplsNextTable);
1603 } else {
1604 tb.transition(ACL_TABLE);
1605 }
1606
Andrea Campanellad980c6d2018-04-30 11:48:55 +02001607 if (fwd.treatment() != null && fwd.treatment().clearedDeferred()) {
1608 if (supportsUnicastBlackHole()) {
1609 tb.wipeDeferred();
1610 } else {
1611 log.warn("Clear Deferred is not supported Unicast Routing Table on device {}", deviceId);
1612 return Collections.emptySet();
1613 }
1614 }
1615
Saurav Das822c4e22015-10-23 10:51:11 -07001616 FlowRule.Builder ruleBuilder = DefaultFlowRule.builder()
1617 .fromApp(fwd.appId())
1618 .withPriority(fwd.priority())
1619 .forDevice(deviceId)
Saurav Das8a0732e2015-11-20 15:27:53 -08001620 .withSelector(filteredSelector.build())
1621 .withTreatment(tb.build())
1622 .forTable(forTableId);
Saurav Das822c4e22015-10-23 10:51:11 -07001623
1624 if (fwd.permanent()) {
1625 ruleBuilder.makePermanent();
1626 } else {
1627 ruleBuilder.makeTemporary(fwd.timeout());
1628 }
Flavio Castroe10fa242016-01-15 12:43:51 -08001629 Collection<FlowRule> flowRuleCollection = new ArrayList<>();
1630 flowRuleCollection.add(ruleBuilder.build());
Pier Luigi3bfe32c2017-01-30 09:47:36 -08001631 if (!allowDefaultRoute) {
Pier Ventree0ae7a32016-11-23 09:57:42 -08001632 flowRuleCollection.add(
1633 defaultRoute(fwd, complementarySelector, forTableId, tb)
1634 );
Flavio Castroe10fa242016-01-15 12:43:51 -08001635 log.debug("Default rule 0.0.0.0/0 is being installed two rules");
1636 }
Saurav Dasc568c342018-01-25 09:49:01 -08001637
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001638 if (emptyGroup) {
pier8b3aef42019-03-11 15:14:02 -07001639 retryExecutorService.schedule(new RetryFlows(fwd, flowRuleCollection),
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001640 RETRY_MS, TimeUnit.MILLISECONDS);
1641 }
Flavio Castroe10fa242016-01-15 12:43:51 -08001642 return flowRuleCollection;
Saurav Das822c4e22015-10-23 10:51:11 -07001643 }
1644
Charles Chan50d900c2018-03-02 13:26:22 -08001645 private int buildIpv4Selector(TrafficSelector.Builder builderToUpdate,
Pier Luigi3bfe32c2017-01-30 09:47:36 -08001646 TrafficSelector.Builder extBuilder,
1647 ForwardingObjective fwd,
1648 boolean allowDefaultRoute) {
1649 TrafficSelector selector = fwd.selector();
1650
1651 IpPrefix ipv4Dst = ((IPCriterion) selector.getCriterion(Criterion.Type.IPV4_DST)).ip();
1652 if (ipv4Dst.isMulticast()) {
1653 if (ipv4Dst.prefixLength() != 32) {
1654 log.warn("Multicast specific forwarding objective can only be /32");
1655 fail(fwd, ObjectiveError.BADPARAMS);
1656 return -1;
1657 }
1658 VlanId assignedVlan = readVlanFromSelector(fwd.meta());
1659 if (assignedVlan == null) {
1660 log.warn("VLAN ID required by multicast specific fwd obj is missing. Abort.");
1661 fail(fwd, ObjectiveError.BADPARAMS);
1662 return -1;
1663 }
Charles Chand1172632017-03-15 17:33:09 -07001664 if (requireVlanExtensions()) {
1665 OfdpaMatchVlanVid ofdpaMatchVlanVid = new OfdpaMatchVlanVid(assignedVlan);
1666 builderToUpdate.extension(ofdpaMatchVlanVid, deviceId);
1667 } else {
1668 builderToUpdate.matchVlanId(assignedVlan);
1669 }
Pier Luigi3bfe32c2017-01-30 09:47:36 -08001670 builderToUpdate.matchEthType(Ethernet.TYPE_IPV4).matchIPDst(ipv4Dst);
1671 log.debug("processing IPv4 multicast specific forwarding objective {} -> next:{}"
1672 + " in dev:{}", fwd.id(), fwd.nextId(), deviceId);
1673 } else {
1674 if (ipv4Dst.prefixLength() == 0) {
1675 if (allowDefaultRoute) {
1676 // The entire IPV4_DST field is wildcarded intentionally
1677 builderToUpdate.matchEthType(Ethernet.TYPE_IPV4);
1678 } else {
1679 // NOTE: The switch does not support matching 0.0.0.0/0
1680 // Split it into 0.0.0.0/1 and 128.0.0.0/1
1681 builderToUpdate.matchEthType(Ethernet.TYPE_IPV4)
1682 .matchIPDst(IpPrefix.valueOf("0.0.0.0/1"));
1683 extBuilder.matchEthType(Ethernet.TYPE_IPV4)
1684 .matchIPDst(IpPrefix.valueOf("128.0.0.0/1"));
1685 }
1686 } else {
1687 builderToUpdate.matchEthType(Ethernet.TYPE_IPV4).matchIPDst(ipv4Dst);
1688 }
1689 log.debug("processing IPv4 unicast specific forwarding objective {} -> next:{}"
1690 + " in dev:{}", fwd.id(), fwd.nextId(), deviceId);
1691 }
1692 return 0;
1693 }
1694
1695 /**
1696 * Helper method to build Ipv6 selector using the selector provided by
1697 * a forwarding objective.
1698 *
1699 * @param builderToUpdate the builder to update
1700 * @param fwd the selector to read
1701 * @return 0 if the update ends correctly. -1 if the matches
1702 * are not yet supported
1703 */
Charles Chan50d900c2018-03-02 13:26:22 -08001704 int buildIpv6Selector(TrafficSelector.Builder builderToUpdate,
Pier Luigi3bfe32c2017-01-30 09:47:36 -08001705 ForwardingObjective fwd) {
1706
1707 TrafficSelector selector = fwd.selector();
1708
1709 IpPrefix ipv6Dst = ((IPCriterion) selector.getCriterion(Criterion.Type.IPV6_DST)).ip();
1710 if (ipv6Dst.isMulticast()) {
Julia Ferguson65428c32017-08-10 18:15:24 +00001711 if (ipv6Dst.prefixLength() != IpAddress.INET6_BIT_LENGTH) {
1712 log.warn("Multicast specific forwarding objective can only be /128");
1713 fail(fwd, ObjectiveError.BADPARAMS);
1714 return -1;
1715 }
1716 VlanId assignedVlan = readVlanFromSelector(fwd.meta());
1717 if (assignedVlan == null) {
1718 log.warn("VLAN ID required by multicast specific fwd obj is missing. Abort.");
1719 fail(fwd, ObjectiveError.BADPARAMS);
1720 return -1;
1721 }
1722 if (requireVlanExtensions()) {
1723 OfdpaMatchVlanVid ofdpaMatchVlanVid = new OfdpaMatchVlanVid(assignedVlan);
1724 builderToUpdate.extension(ofdpaMatchVlanVid, deviceId);
1725 } else {
1726 builderToUpdate.matchVlanId(assignedVlan);
1727 }
1728 builderToUpdate.matchEthType(Ethernet.TYPE_IPV6).matchIPv6Dst(ipv6Dst);
1729 log.debug("processing IPv6 multicast specific forwarding objective {} -> next:{}"
1730 + " in dev:{}", fwd.id(), fwd.nextId(), deviceId);
1731 } else {
1732 if (ipv6Dst.prefixLength() != 0) {
1733 builderToUpdate.matchIPv6Dst(ipv6Dst);
1734 }
Pier Luigi3bfe32c2017-01-30 09:47:36 -08001735 builderToUpdate.matchEthType(Ethernet.TYPE_IPV6);
1736 log.debug("processing IPv6 unicast specific forwarding objective {} -> next:{}"
1737 + " in dev:{}", fwd.id(), fwd.nextId(), deviceId);
Julia Ferguson65428c32017-08-10 18:15:24 +00001738 }
Pier Luigi3bfe32c2017-01-30 09:47:36 -08001739 return 0;
1740 }
1741
Charles Chan50d900c2018-03-02 13:26:22 -08001742 FlowRule defaultRoute(ForwardingObjective fwd,
Pier Ventree0ae7a32016-11-23 09:57:42 -08001743 TrafficSelector.Builder complementarySelector,
1744 int forTableId,
1745 TrafficTreatment.Builder tb) {
1746 FlowRule.Builder rule = DefaultFlowRule.builder()
1747 .fromApp(fwd.appId())
1748 .withPriority(fwd.priority())
1749 .forDevice(deviceId)
1750 .withSelector(complementarySelector.build())
1751 .withTreatment(tb.build())
1752 .forTable(forTableId);
1753 if (fwd.permanent()) {
1754 rule.makePermanent();
1755 } else {
1756 rule.makeTemporary(fwd.timeout());
1757 }
1758 return rule.build();
1759 }
1760
Saurav Das4ce45962015-11-24 23:21:05 -08001761 /**
1762 * Handles forwarding rules to the L2 bridging table. Flow actions are not
1763 * allowed in the bridging table - instead we use L2 Interface group or
1764 * L2 flood group
1765 *
1766 * @param fwd the forwarding objective
1767 * @return A collection of flow rules, or an empty set
1768 */
1769 protected Collection<FlowRule> processEthDstSpecific(ForwardingObjective fwd) {
1770 List<FlowRule> rules = new ArrayList<>();
1771
1772 // Build filtered selector
1773 TrafficSelector selector = fwd.selector();
1774 EthCriterion ethCriterion = (EthCriterion) selector
1775 .getCriterion(Criterion.Type.ETH_DST);
1776 VlanIdCriterion vlanIdCriterion = (VlanIdCriterion) selector
1777 .getCriterion(Criterion.Type.VLAN_VID);
1778
1779 if (vlanIdCriterion == null) {
1780 log.warn("Forwarding objective for bridging requires vlan. Not "
1781 + "installing fwd:{} in dev:{}", fwd.id(), deviceId);
1782 fail(fwd, ObjectiveError.BADPARAMS);
1783 return Collections.emptySet();
1784 }
1785
1786 TrafficSelector.Builder filteredSelectorBuilder =
1787 DefaultTrafficSelector.builder();
Yi Tseng3a77b4f2017-02-06 15:02:17 -08001788
1789 if (!ethCriterion.mac().equals(NONE) &&
1790 !ethCriterion.mac().equals(BROADCAST)) {
Saurav Das4ce45962015-11-24 23:21:05 -08001791 filteredSelectorBuilder.matchEthDst(ethCriterion.mac());
1792 log.debug("processing L2 forwarding objective:{} -> next:{} in dev:{}",
1793 fwd.id(), fwd.nextId(), deviceId);
1794 } else {
Yi Tseng3a77b4f2017-02-06 15:02:17 -08001795 // Use wildcard DST_MAC if the MacAddress is None or Broadcast
Saurav Das4ce45962015-11-24 23:21:05 -08001796 log.debug("processing L2 Broadcast forwarding objective:{} -> next:{} "
1797 + "in dev:{} for vlan:{}",
1798 fwd.id(), fwd.nextId(), deviceId, vlanIdCriterion.vlanId());
1799 }
Charles Chand1172632017-03-15 17:33:09 -07001800 if (requireVlanExtensions()) {
1801 OfdpaMatchVlanVid ofdpaMatchVlanVid = new OfdpaMatchVlanVid(vlanIdCriterion.vlanId());
1802 filteredSelectorBuilder.extension(ofdpaMatchVlanVid, deviceId);
1803 } else {
1804 filteredSelectorBuilder.matchVlanId(vlanIdCriterion.vlanId());
1805 }
Saurav Das4ce45962015-11-24 23:21:05 -08001806 TrafficSelector filteredSelector = filteredSelectorBuilder.build();
1807
1808 if (fwd.treatment() != null) {
1809 log.warn("Ignoring traffic treatment in fwd rule {} meant for L2 table"
1810 + "for dev:{}. Expecting only nextId", fwd.id(), deviceId);
1811 }
1812
1813 TrafficTreatment.Builder treatmentBuilder = DefaultTrafficTreatment.builder();
1814 if (fwd.nextId() != null) {
Saurav Das423fe2b2015-12-04 10:52:59 -08001815 NextGroup next = getGroupForNextObjective(fwd.nextId());
Saurav Das4ce45962015-11-24 23:21:05 -08001816 if (next != null) {
1817 List<Deque<GroupKey>> gkeys = appKryo.deserialize(next.data());
1818 // we only need the top level group's key to point the flow to it
1819 Group group = groupService.getGroup(deviceId, gkeys.get(0).peekFirst());
1820 if (group != null) {
1821 treatmentBuilder.deferred().group(group.id());
1822 } else {
1823 log.warn("Group with key:{} for next-id:{} not found in dev:{}",
1824 gkeys.get(0).peekFirst(), fwd.nextId(), deviceId);
1825 fail(fwd, ObjectiveError.GROUPMISSING);
1826 return Collections.emptySet();
1827 }
1828 }
1829 }
1830 treatmentBuilder.immediate().transition(ACL_TABLE);
1831 TrafficTreatment filteredTreatment = treatmentBuilder.build();
1832
1833 // Build bridging table entries
1834 FlowRule.Builder flowRuleBuilder = DefaultFlowRule.builder();
1835 flowRuleBuilder.fromApp(fwd.appId())
1836 .withPriority(fwd.priority())
1837 .forDevice(deviceId)
1838 .withSelector(filteredSelector)
1839 .withTreatment(filteredTreatment)
1840 .forTable(BRIDGING_TABLE);
1841 if (fwd.permanent()) {
1842 flowRuleBuilder.makePermanent();
1843 } else {
1844 flowRuleBuilder.makeTemporary(fwd.timeout());
1845 }
1846 rules.add(flowRuleBuilder.build());
1847 return rules;
1848 }
1849
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001850 //////////////////////////////////////
1851 // Helper Methods and Classes
1852 //////////////////////////////////////
1853
1854 private boolean isSupportedEthTypeObjective(ForwardingObjective fwd) {
1855 TrafficSelector selector = fwd.selector();
1856 EthTypeCriterion ethType = (EthTypeCriterion) selector
1857 .getCriterion(Criterion.Type.ETH_TYPE);
1858 return !((ethType == null) ||
1859 ((ethType.ethType().toShort() != Ethernet.TYPE_IPV4) &&
Pier Ventree0ae7a32016-11-23 09:57:42 -08001860 (ethType.ethType().toShort() != Ethernet.MPLS_UNICAST)) &&
1861 (ethType.ethType().toShort() != Ethernet.TYPE_IPV6));
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001862 }
1863
1864 private boolean isSupportedEthDstObjective(ForwardingObjective fwd) {
1865 TrafficSelector selector = fwd.selector();
1866 EthCriterion ethDst = (EthCriterion) selector
1867 .getCriterion(Criterion.Type.ETH_DST);
1868 VlanIdCriterion vlanId = (VlanIdCriterion) selector
1869 .getCriterion(Criterion.Type.VLAN_VID);
1870 return !(ethDst == null && vlanId == null);
1871 }
1872
Charles Chan50d900c2018-03-02 13:26:22 -08001873 NextGroup getGroupForNextObjective(Integer nextId) {
Saurav Das423fe2b2015-12-04 10:52:59 -08001874 NextGroup next = flowObjectiveStore.getNextGroup(nextId);
1875 if (next != null) {
1876 List<Deque<GroupKey>> gkeys = appKryo.deserialize(next.data());
1877 if (gkeys != null && !gkeys.isEmpty()) {
1878 return next;
1879 } else {
1880 log.warn("Empty next group found in FlowObjective store for "
1881 + "next-id:{} in dev:{}", nextId, deviceId);
1882 }
1883 } else {
1884 log.warn("next-id {} not found in Flow objective store for dev:{}",
1885 nextId, deviceId);
1886 }
1887 return null;
1888 }
1889
Saurav Das24431192016-03-07 19:13:00 -08001890 @Override
1891 public List<String> getNextMappings(NextGroup nextGroup) {
1892 List<String> mappings = new ArrayList<>();
1893 List<Deque<GroupKey>> gkeys = appKryo.deserialize(nextGroup.data());
1894 for (Deque<GroupKey> gkd : gkeys) {
1895 Group lastGroup = null;
Yuta HIGUCHI2dce08a2017-04-20 21:57:48 -07001896 StringBuilder gchain = new StringBuilder();
Saurav Das24431192016-03-07 19:13:00 -08001897 for (GroupKey gk : gkd) {
1898 Group g = groupService.getGroup(deviceId, gk);
Saurav Das8be4e3a2016-03-11 17:19:07 -08001899 if (g == null) {
Saurav Das25190812016-05-27 13:54:07 -07001900 gchain.append(" NoGrp").append(" -->");
Saurav Das8be4e3a2016-03-11 17:19:07 -08001901 continue;
1902 }
1903 gchain.append(" 0x").append(Integer.toHexString(g.id().id()))
1904 .append(" -->");
Saurav Das24431192016-03-07 19:13:00 -08001905 lastGroup = g;
1906 }
1907 // add port information for last group in group-chain
Yuta HIGUCHI2dce08a2017-04-20 21:57:48 -07001908 List<Instruction> lastGroupIns = new ArrayList<>();
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001909 if (lastGroup != null && !lastGroup.buckets().buckets().isEmpty()) {
Saurav Das25190812016-05-27 13:54:07 -07001910 lastGroupIns = lastGroup.buckets().buckets().get(0)
1911 .treatment().allInstructions();
1912 }
1913 for (Instruction i: lastGroupIns) {
Saurav Das24431192016-03-07 19:13:00 -08001914 if (i instanceof OutputInstruction) {
Saurav Das8be4e3a2016-03-11 17:19:07 -08001915 gchain.append(" port:").append(((OutputInstruction) i).port());
Saurav Das24431192016-03-07 19:13:00 -08001916 }
1917 }
Saurav Das8be4e3a2016-03-11 17:19:07 -08001918 mappings.add(gchain.toString());
Saurav Das24431192016-03-07 19:13:00 -08001919 }
1920 return mappings;
1921 }
Charles Chan5b9df8d2016-03-28 22:21:40 -07001922
Saurav Dasa4020382018-02-14 14:14:54 -08001923 /**
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001924 * Utility class that retries sending flows a fixed number of times, even if
1925 * some of the attempts are successful. Used only for forwarding objectives.
1926 */
Alex Yashchuk4caa8e82017-12-08 17:40:05 +02001927 public final class RetryFlows implements Runnable {
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001928 int attempts = MAX_RETRY_ATTEMPTS;
1929 private Collection<FlowRule> retryFlows;
1930 private ForwardingObjective fwd;
1931
Charles Chan50d900c2018-03-02 13:26:22 -08001932 RetryFlows(ForwardingObjective fwd, Collection<FlowRule> retryFlows) {
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001933 this.fwd = fwd;
1934 this.retryFlows = retryFlows;
1935 }
1936
1937 @Override
1938 public void run() {
1939 log.info("RETRY FLOWS ATTEMPT# {} for fwd:{} rules:{}",
1940 MAX_RETRY_ATTEMPTS - attempts, fwd.id(), retryFlows.size());
pier8b3aef42019-03-11 15:14:02 -07001941 sendForwards(Collections.singletonList(Pair.of(fwd, retryFlows)));
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001942 if (--attempts > 0) {
pier8b3aef42019-03-11 15:14:02 -07001943 retryExecutorService.schedule(this, RETRY_MS, TimeUnit.MILLISECONDS);
1944 }
1945 }
1946 }
1947
1948 // Flow rules accumulator for reducing the number of transactions required to the devices.
1949 private final class ForwardingObjectiveAccumulator
1950 extends AbstractAccumulator<Pair<ForwardingObjective, Collection<FlowRule>>> {
1951
1952 ForwardingObjectiveAccumulator(int maxFwd, int maxBatchMS, int maxIdleMS) {
1953 super(TIMER, maxFwd, maxBatchMS, maxIdleMS);
1954 }
1955
1956 @Override
1957 public void processItems(List<Pair<ForwardingObjective, Collection<FlowRule>>> pairs) {
1958 // Triggers creation of a batch using the list of flowrules generated from fwdobjs.
1959 accumulatorExecutorService.execute(new FlowRulesBuilderTask(pairs));
1960 }
1961 }
1962
1963 // Task for building batch of flow rules in a separate thread.
1964 private final class FlowRulesBuilderTask implements Runnable {
1965 private final List<Pair<ForwardingObjective, Collection<FlowRule>>> pairs;
1966
1967 FlowRulesBuilderTask(List<Pair<ForwardingObjective, Collection<FlowRule>>> pairs) {
1968 this.pairs = pairs;
1969 }
1970
1971 @Override
1972 public void run() {
1973 try {
1974 sendForwards(pairs);
1975 } catch (Exception e) {
1976 log.warn("Unable to send forwards", e);
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001977 }
1978 }
1979 }
1980
Saurav Das822c4e22015-10-23 10:51:11 -07001981}