blob: a020a647a2c1c0a1b4b86cb2ed0dce02ca8d008b [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
480 //////////////////////////////////////
481 // Flow handling
482 //////////////////////////////////////
483
484 /**
Yi Tseng0fdc07e2017-04-24 19:17:08 -0700485 * As per OFDPA 2.0 TTP, filtering of VLAN ids and MAC addresses (for routing)
486 * configured on switch ports happen in different tables.
Saurav Das822c4e22015-10-23 10:51:11 -0700487 *
488 * @param filt the filtering objective
489 * @param install indicates whether to add or remove the objective
490 * @param applicationId the application that sent this objective
491 */
Saurav Das52025962016-01-28 22:30:01 -0800492 protected void processFilter(FilteringObjective filt,
493 boolean install, ApplicationId applicationId) {
Saurav Das822c4e22015-10-23 10:51:11 -0700494 // This driver only processes filtering criteria defined with switch
495 // ports as the key
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530496 PortCriterion portCriterion = null;
Saurav Das822c4e22015-10-23 10:51:11 -0700497 EthCriterion ethCriterion = null;
498 VlanIdCriterion vidCriterion = null;
Saurav Das822c4e22015-10-23 10:51:11 -0700499 if (!filt.key().equals(Criteria.dummy()) &&
500 filt.key().type() == Criterion.Type.IN_PORT) {
501 portCriterion = (PortCriterion) filt.key();
Saurav Das822c4e22015-10-23 10:51:11 -0700502 }
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530503 if (portCriterion == null) {
504 log.debug("No IN_PORT defined in filtering objective from app: {}",
505 applicationId);
506 } else {
507 log.debug("Received filtering objective for dev/port: {}/{}", deviceId,
508 portCriterion.port());
509 }
Saurav Das822c4e22015-10-23 10:51:11 -0700510 // convert filtering conditions for switch-intfs into flowrules
511 FlowRuleOperations.Builder ops = FlowRuleOperations.builder();
512 for (Criterion criterion : filt.conditions()) {
Yi Tseng0fdc07e2017-04-24 19:17:08 -0700513 switch (criterion.type()) {
514 case ETH_DST:
515 case ETH_DST_MASKED:
516 ethCriterion = (EthCriterion) criterion;
517 break;
518 case VLAN_VID:
519 vidCriterion = (VlanIdCriterion) criterion;
520 break;
521 default:
522 log.warn("Unsupported filter {}", criterion);
523 fail(filt, ObjectiveError.UNSUPPORTED);
524 return;
Saurav Das822c4e22015-10-23 10:51:11 -0700525 }
526 }
527
Saurav Das0e99e2b2015-10-28 12:39:42 -0700528 VlanId assignedVlan = null;
Charles Chane849c192016-01-11 18:28:54 -0800529 if (vidCriterion != null) {
Charles Chand55e84d2016-03-30 17:54:24 -0700530 // Use the VLAN in criterion if metadata is not present and the traffic is tagged
Charles Chanc550f2e2017-12-19 19:55:57 -0800531 if (!vidCriterion.vlanId().equals(VlanId.NONE)) {
Charles Chane849c192016-01-11 18:28:54 -0800532 assignedVlan = vidCriterion.vlanId();
Piere5bff482018-03-07 11:42:50 +0100533 }
534 // If the meta VLAN is present let's update the assigned vlan
535 if (filt.meta() != null) {
536 VlanId metaVlan = readVlanFromTreatment(filt.meta());
537 if (metaVlan != null) {
538 assignedVlan = metaVlan;
539 }
Charles Chand55e84d2016-03-30 17:54:24 -0700540 }
Charles Chane849c192016-01-11 18:28:54 -0800541
Charles Chand55e84d2016-03-30 17:54:24 -0700542 if (assignedVlan == null) {
543 log.error("Driver fails to extract VLAN information. "
Frank Wangd8ab0962017-08-11 11:09:30 +0800544 + "Not processing VLAN filters on device {}.", deviceId);
Charles Chand55e84d2016-03-30 17:54:24 -0700545 log.debug("VLAN ID in criterion={}, metadata={}",
546 readVlanFromTreatment(filt.meta()), vidCriterion.vlanId());
547 fail(filt, ObjectiveError.BADPARAMS);
548 return;
Saurav Das0e99e2b2015-10-28 12:39:42 -0700549 }
550 }
551
Yi Tseng3a77b4f2017-02-06 15:02:17 -0800552 if (ethCriterion == null || ethCriterion.mac().equals(NONE)) {
Charles Chan985b12e2016-05-11 19:47:22 -0700553 // NOTE: it is possible that a filtering objective only has vidCriterion
Daniel Ginsburgc1d47e92018-04-30 19:27:19 -0400554 log.debug("filtering objective missing dstMac, won't program TMAC table");
Saurav Das822c4e22015-10-23 10:51:11 -0700555 } else {
Charles Chan45b69ab2018-03-02 15:41:41 -0800556 MacAddress unicastMac = readEthDstFromTreatment(filt.meta());
Charles Chan66291502018-03-02 16:43:28 -0800557 List<List<FlowRule>> allStages = processEthDstFilter(portCriterion, ethCriterion,
Charles Chan45b69ab2018-03-02 15:41:41 -0800558 vidCriterion, assignedVlan, unicastMac, applicationId);
Charles Chan66291502018-03-02 16:43:28 -0800559 for (List<FlowRule> flowRules : allStages) {
Charles Chan45b69ab2018-03-02 15:41:41 -0800560 log.trace("Starting a new flow rule stage for TMAC table flow");
Charles Chan66291502018-03-02 16:43:28 -0800561 ops.newStage();
Charles Chanc550f2e2017-12-19 19:55:57 -0800562
Charles Chan66291502018-03-02 16:43:28 -0800563 for (FlowRule flowRule : flowRules) {
pier5e8b2bc2019-10-04 10:54:07 +0200564 log.trace("{} flow rule in TMAC table: {} for dev: {}",
565 (install) ? "adding" : "removing", flowRule, deviceId);
Charles Chan66291502018-03-02 16:43:28 -0800566 if (install) {
567 ops = ops.add(flowRule);
Charles Chanc550f2e2017-12-19 19:55:57 -0800568 } else {
Charles Chan66291502018-03-02 16:43:28 -0800569 // NOTE: Only remove TMAC flow when there is no more enabled port within the
570 // same VLAN on this device if TMAC doesn't support matching on in_port.
pier5e8b2bc2019-10-04 10:54:07 +0200571 if (filt.meta() != null && filt.meta().clearedDeferred()) {
572 // TMac mcast does not match on the input port - we have to remove it
573 // only if this is the last port
574 FlowRule rule = buildTmacRuleForMcastFromUnicast(flowRule, applicationId);
575 // IPv6 or IPv4 tmac rule
576 if (rule != null) {
577 // Add first the mcast rule and then open a new stage for the unicast
578 ops = ops.remove(rule);
579 ops.newStage();
580 }
581 ops = ops.remove(flowRule);
582 } else if (matchInPortTmacTable()) {
Charles Chan66291502018-03-02 16:43:28 -0800583 ops = ops.remove(flowRule);
584 } else {
pier5e8b2bc2019-10-04 10:54:07 +0200585 log.debug("Abort TMAC flow removal on {}. " +
586 "Some other ports still share this TMAC flow", deviceId);
Charles Chan66291502018-03-02 16:43:28 -0800587 }
Charles Chanc550f2e2017-12-19 19:55:57 -0800588 }
589 }
Saurav Das822c4e22015-10-23 10:51:11 -0700590 }
591 }
592
Charles Chan985b12e2016-05-11 19:47:22 -0700593 if (vidCriterion == null) {
594 // NOTE: it is possible that a filtering objective only has ethCriterion
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530595 log.info("filtering objective missing VLAN, cannot program VLAN Table");
Saurav Das822c4e22015-10-23 10:51:11 -0700596 } else {
Charles Chan66291502018-03-02 16:43:28 -0800597 List<List<FlowRule>> allStages = processVlanIdFilter(
pier6aef5b72019-06-10 17:10:26 +0200598 portCriterion, vidCriterion, assignedVlan, applicationId, install);
Charles Chan66291502018-03-02 16:43:28 -0800599 for (List<FlowRule> flowRules : allStages) {
Charles Chan45b69ab2018-03-02 15:41:41 -0800600 log.trace("Starting a new flow rule stage for VLAN table flow");
Charles Chan66291502018-03-02 16:43:28 -0800601 ops.newStage();
Charles Chan14967c22015-12-07 11:11:50 -0800602
Charles Chan66291502018-03-02 16:43:28 -0800603 for (FlowRule flowRule : flowRules) {
604 log.trace("{} flow rules in VLAN table: {} for dev: {}",
605 (install) ? "adding" : "removing", flowRule, deviceId);
606 ops = install ? ops.add(flowRule) : ops.remove(flowRule);
Charles Chand1172632017-03-15 17:33:09 -0700607 }
Saurav Das822c4e22015-10-23 10:51:11 -0700608 }
609 }
610
Saurav Das822c4e22015-10-23 10:51:11 -0700611 // apply filtering flow rules
612 flowRuleService.apply(ops.build(new FlowRuleOperationsContext() {
613 @Override
614 public void onSuccess(FlowRuleOperations ops) {
Saurav Das018605f2017-02-18 14:05:44 -0800615 log.debug("Applied {} filtering rules in device {}",
Saurav Das822c4e22015-10-23 10:51:11 -0700616 ops.stages().get(0).size(), deviceId);
617 pass(filt);
618 }
619
620 @Override
621 public void onError(FlowRuleOperations ops) {
622 log.info("Failed to apply all filtering rules in dev {}", deviceId);
623 fail(filt, ObjectiveError.FLOWINSTALLATIONFAILED);
624 }
625 }));
626
627 }
628
629 /**
Charles Chand1172632017-03-15 17:33:09 -0700630 * Internal implementation of processVlanIdFilter.
631 * <p>
632 * The is_present bit in set_vlan_vid action is required to be 0 in OFDPA i12.
633 * Since it is non-OF spec, we need an extension treatment for that.
634 * The useVlanExtension must be set to false for OFDPA i12.
635 * </p>
Charles Chan66291502018-03-02 16:43:28 -0800636 * <p>
637 * NOTE: Separate VLAN filtering rules and assignment rules
638 * into different stages in order to guarantee that filtering rules
639 * always go first, as required by OFDPA.
640 * </p>
Saurav Das0e99e2b2015-10-28 12:39:42 -0700641 *
Charles Chanf9e98652016-09-07 16:54:23 -0700642 * @param portCriterion port on device for which this filter is programmed
643 * @param vidCriterion vlan assigned to port, or NONE for untagged
644 * @param assignedVlan assigned vlan-id for untagged packets
645 * @param applicationId for application programming this filter
pier6aef5b72019-06-10 17:10:26 +0200646 * @param install indicates whether to add or remove the objective
Charles Chan66291502018-03-02 16:43:28 -0800647 * @return stages of flow rules for port-vlan filters
Saurav Das822c4e22015-10-23 10:51:11 -0700648 */
Charles Chan66291502018-03-02 16:43:28 -0800649 protected List<List<FlowRule>> processVlanIdFilter(PortCriterion portCriterion,
pier6aef5b72019-06-10 17:10:26 +0200650 VlanIdCriterion vidCriterion,
651 VlanId assignedVlan,
652 ApplicationId applicationId,
653 boolean install) {
Charles Chan66291502018-03-02 16:43:28 -0800654 List<FlowRule> filteringRules = new ArrayList<>();
655 List<FlowRule> assignmentRules = new ArrayList<>();
Saurav Das822c4e22015-10-23 10:51:11 -0700656 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
657 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
Charles Chan14967c22015-12-07 11:11:50 -0800658 TrafficSelector.Builder preSelector = null;
659 TrafficTreatment.Builder preTreatment = null;
660
Saurav Das4f980082015-11-05 13:39:15 -0800661 treatment.transition(TMAC_TABLE);
662
Saurav Das822c4e22015-10-23 10:51:11 -0700663 if (vidCriterion.vlanId() == VlanId.NONE) {
664 // untagged packets are assigned vlans
Charles Chand1172632017-03-15 17:33:09 -0700665 preSelector = DefaultTrafficSelector.builder();
666 if (requireVlanExtensions()) {
667 OfdpaMatchVlanVid ofdpaMatchVlanVid = new OfdpaMatchVlanVid(VlanId.NONE);
668 selector.extension(ofdpaMatchVlanVid, deviceId);
Charles Chanf9e98652016-09-07 16:54:23 -0700669 OfdpaSetVlanVid ofdpaSetVlanVid = new OfdpaSetVlanVid(assignedVlan);
670 treatment.extension(ofdpaSetVlanVid, deviceId);
Charles Chand1172632017-03-15 17:33:09 -0700671
672 OfdpaMatchVlanVid preOfdpaMatchVlanVid = new OfdpaMatchVlanVid(assignedVlan);
673 preSelector.extension(preOfdpaMatchVlanVid, deviceId);
Charles Chanf9e98652016-09-07 16:54:23 -0700674 } else {
Charles Chand1172632017-03-15 17:33:09 -0700675 selector.matchVlanId(VlanId.NONE);
Charles Chanf9e98652016-09-07 16:54:23 -0700676 treatment.setVlanId(assignedVlan);
Charles Chand1172632017-03-15 17:33:09 -0700677
678 preSelector.matchVlanId(assignedVlan);
679 }
680 preTreatment = DefaultTrafficTreatment.builder().transition(TMAC_TABLE);
681 } else {
682 if (requireVlanExtensions()) {
683 OfdpaMatchVlanVid ofdpaMatchVlanVid = new OfdpaMatchVlanVid(vidCriterion.vlanId());
684 selector.extension(ofdpaMatchVlanVid, deviceId);
685 } else {
686 selector.matchVlanId(vidCriterion.vlanId());
Charles Chanf9e98652016-09-07 16:54:23 -0700687 }
Charles Chan14967c22015-12-07 11:11:50 -0800688
Charles Chand55e84d2016-03-30 17:54:24 -0700689 if (!assignedVlan.equals(vidCriterion.vlanId())) {
Charles Chand1172632017-03-15 17:33:09 -0700690 if (requireVlanExtensions()) {
Charles Chanc03782d2017-01-31 13:57:55 -0800691 OfdpaSetVlanVid ofdpaSetVlanVid = new OfdpaSetVlanVid(assignedVlan);
692 treatment.extension(ofdpaSetVlanVid, deviceId);
693 } else {
694 treatment.setVlanId(assignedVlan);
695 }
Charles Chand55e84d2016-03-30 17:54:24 -0700696 }
Saurav Das822c4e22015-10-23 10:51:11 -0700697 }
Saurav Das822c4e22015-10-23 10:51:11 -0700698
699 // ofdpa cannot match on ALL portnumber, so we need to use separate
700 // rules for each port.
Charles Chan14967c22015-12-07 11:11:50 -0800701 List<PortNumber> portnums = new ArrayList<>();
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530702 if (portCriterion != null) {
703 if (PortNumber.ALL.equals(portCriterion.port())) {
704 for (Port port : deviceService.getPorts(deviceId)) {
705 if (port.number().toLong() > 0 && port.number().toLong() < OFPP_MAX) {
706 portnums.add(port.number());
707 }
Saurav Das822c4e22015-10-23 10:51:11 -0700708 }
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530709 } else {
710 portnums.add(portCriterion.port());
Saurav Das822c4e22015-10-23 10:51:11 -0700711 }
712 } else {
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530713 log.warn("Filtering Objective missing Port Criterion . " +
Charles Chan66291502018-03-02 16:43:28 -0800714 "VLAN Table cannot be programmed for {}", deviceId);
Saurav Das822c4e22015-10-23 10:51:11 -0700715 }
Saurav Das4f980082015-11-05 13:39:15 -0800716
Saurav Das822c4e22015-10-23 10:51:11 -0700717 for (PortNumber pnum : portnums) {
Saurav Das4f980082015-11-05 13:39:15 -0800718 // create rest of flowrule
Saurav Das822c4e22015-10-23 10:51:11 -0700719 selector.matchInPort(pnum);
720 FlowRule rule = DefaultFlowRule.builder()
721 .forDevice(deviceId)
722 .withSelector(selector.build())
723 .withTreatment(treatment.build())
724 .withPriority(DEFAULT_PRIORITY)
725 .fromApp(applicationId)
726 .makePermanent()
727 .forTable(VLAN_TABLE).build();
Charles Chan66291502018-03-02 16:43:28 -0800728 assignmentRules.add(rule);
Charles Chan14967c22015-12-07 11:11:50 -0800729
730 if (preSelector != null) {
731 preSelector.matchInPort(pnum);
732 FlowRule preRule = DefaultFlowRule.builder()
733 .forDevice(deviceId)
734 .withSelector(preSelector.build())
735 .withTreatment(preTreatment.build())
736 .withPriority(DEFAULT_PRIORITY)
737 .fromApp(applicationId)
738 .makePermanent()
739 .forTable(VLAN_TABLE).build();
Charles Chan66291502018-03-02 16:43:28 -0800740 filteringRules.add(preRule);
Charles Chan14967c22015-12-07 11:11:50 -0800741 }
Saurav Das822c4e22015-10-23 10:51:11 -0700742 }
pier6aef5b72019-06-10 17:10:26 +0200743 return install ? ImmutableList.of(filteringRules, assignmentRules) :
744 ImmutableList.of(assignmentRules, filteringRules);
Saurav Das822c4e22015-10-23 10:51:11 -0700745 }
746
747 /**
748 * Allows routed packets with correct destination MAC to be directed
Charles Chan45b69ab2018-03-02 15:41:41 -0800749 * to unicast routing table, multicast routing table or MPLS forwarding table.
Saurav Das822c4e22015-10-23 10:51:11 -0700750 *
751 * @param portCriterion port on device for which this filter is programmed
752 * @param ethCriterion dstMac of device for which is filter is programmed
753 * @param vidCriterion vlan assigned to port, or NONE for untagged
Saurav Das0e99e2b2015-10-28 12:39:42 -0700754 * @param assignedVlan assigned vlan-id for untagged packets
Charles Chan45b69ab2018-03-02 15:41:41 -0800755 * @param unicastMac some switches require a unicast TMAC flow to be programmed before multicast
756 * TMAC flow. This MAC address will be used for the unicast TMAC flow.
757 * This is unused if the filtering objective is a unicast.
Saurav Das822c4e22015-10-23 10:51:11 -0700758 * @param applicationId for application programming this filter
Charles Chan66291502018-03-02 16:43:28 -0800759 * @return stages of flow rules for port-vlan filters
Saurav Das822c4e22015-10-23 10:51:11 -0700760
761 */
Charles Chan66291502018-03-02 16:43:28 -0800762 protected List<List<FlowRule>> processEthDstFilter(PortCriterion portCriterion,
Saurav Das822c4e22015-10-23 10:51:11 -0700763 EthCriterion ethCriterion,
764 VlanIdCriterion vidCriterion,
Saurav Das0e99e2b2015-10-28 12:39:42 -0700765 VlanId assignedVlan,
Charles Chan45b69ab2018-03-02 15:41:41 -0800766 MacAddress unicastMac,
Saurav Das822c4e22015-10-23 10:51:11 -0700767 ApplicationId applicationId) {
Charles Chan5270ed02016-01-30 23:22:37 -0800768 // Consider PortNumber.ANY as wildcard. Match ETH_DST only
Sivachidambaram Subramanianc598d062017-05-16 22:42:06 +0530769 if (portCriterion != null && PortNumber.ANY.equals(portCriterion.port())) {
Charles Chan5270ed02016-01-30 23:22:37 -0800770 return processEthDstOnlyFilter(ethCriterion, applicationId);
771 }
772
Charles Chan5b9df8d2016-03-28 22:21:40 -0700773 // Multicast MAC
774 if (ethCriterion.mask() != null) {
Charles Chan45b69ab2018-03-02 15:41:41 -0800775 return processMcastEthDstFilter(ethCriterion, assignedVlan, unicastMac, applicationId);
Charles Chan5b9df8d2016-03-28 22:21:40 -0700776 }
777
Saurav Das822c4e22015-10-23 10:51:11 -0700778 //handling untagged packets via assigned VLAN
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530779 if (vidCriterion != null && vidCriterion.vlanId() == VlanId.NONE) {
Saurav Das0e99e2b2015-10-28 12:39:42 -0700780 vidCriterion = (VlanIdCriterion) Criteria.matchVlanId(assignedVlan);
Saurav Das822c4e22015-10-23 10:51:11 -0700781 }
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530782 List<FlowRule> rules = new ArrayList<>();
783 OfdpaMatchVlanVid ofdpaMatchVlanVid = null;
784 if (vidCriterion != null && requireVlanExtensions()) {
785 ofdpaMatchVlanVid = new OfdpaMatchVlanVid(vidCriterion.vlanId());
786 }
Saurav Das822c4e22015-10-23 10:51:11 -0700787 // ofdpa cannot match on ALL portnumber, so we need to use separate
788 // rules for each port.
Yuta HIGUCHI2dce08a2017-04-20 21:57:48 -0700789 List<PortNumber> portnums = new ArrayList<>();
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530790 if (portCriterion != null) {
791 if (PortNumber.ALL.equals(portCriterion.port())) {
792 for (Port port : deviceService.getPorts(deviceId)) {
793 if (port.number().toLong() > 0 && port.number().toLong() < OFPP_MAX) {
794 portnums.add(port.number());
795 }
Saurav Das822c4e22015-10-23 10:51:11 -0700796 }
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530797 } else {
798 portnums.add(portCriterion.port());
799 }
800 for (PortNumber pnum : portnums) {
801 rules.add(buildTmacRuleForIpv4(ethCriterion,
802 vidCriterion,
803 ofdpaMatchVlanVid,
804 applicationId,
805 pnum));
806 rules.add(buildTmacRuleForMpls(ethCriterion,
807 vidCriterion,
808 ofdpaMatchVlanVid,
809 applicationId,
810 pnum));
811 rules.add(buildTmacRuleForIpv6(ethCriterion,
812 vidCriterion,
813 ofdpaMatchVlanVid,
814 applicationId,
815 pnum));
Saurav Das822c4e22015-10-23 10:51:11 -0700816 }
817 } else {
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530818 rules.add(buildTmacRuleForIpv4(ethCriterion,
819 vidCriterion,
820 ofdpaMatchVlanVid,
821 applicationId,
822 null));
823 rules.add(buildTmacRuleForMpls(ethCriterion,
824 vidCriterion,
825 ofdpaMatchVlanVid,
826 applicationId,
827 null));
828 rules.add(buildTmacRuleForIpv6(ethCriterion,
829 vidCriterion,
830 ofdpaMatchVlanVid,
831 applicationId,
832 null));
Saurav Das822c4e22015-10-23 10:51:11 -0700833 }
Charles Chan66291502018-03-02 16:43:28 -0800834 return ImmutableList.of(rules);
Saurav Das822c4e22015-10-23 10:51:11 -0700835 }
836
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530837 /**
838 * Builds TMAC rules for IPv4 packets.
839 *
Charles Chanc550f2e2017-12-19 19:55:57 -0800840 * @param ethCriterion dst mac matching
841 * @param vidCriterion vlan id assigned to the port
842 * @param ofdpaMatchVlanVid OFDPA vlan id matching
843 * @param applicationId application id
844 * @param pnum port number
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530845 * @return TMAC rule for IPV4 packets
846 */
847 private FlowRule buildTmacRuleForIpv4(EthCriterion ethCriterion,
848 VlanIdCriterion vidCriterion,
849 OfdpaMatchVlanVid ofdpaMatchVlanVid,
850 ApplicationId applicationId,
851 PortNumber pnum) {
852 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
853 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
854 if (pnum != null) {
855 if (matchInPortTmacTable()) {
856 selector.matchInPort(pnum);
857 } else {
Saurav Dasf14d9ef2017-12-05 15:00:23 -0800858 log.debug("Pipeline does not support IN_PORT matching in TMAC table, " +
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530859 "ignoring the IN_PORT criteria");
860 }
861 }
862 if (vidCriterion != null) {
863 if (requireVlanExtensions()) {
864 selector.extension(ofdpaMatchVlanVid, deviceId);
865 } else {
866 selector.matchVlanId(vidCriterion.vlanId());
867 }
868 }
869 selector.matchEthType(Ethernet.TYPE_IPV4);
870 selector.matchEthDst(ethCriterion.mac());
871 treatment.transition(UNICAST_ROUTING_TABLE);
872 return DefaultFlowRule.builder()
873 .forDevice(deviceId)
874 .withSelector(selector.build())
875 .withTreatment(treatment.build())
876 .withPriority(DEFAULT_PRIORITY)
877 .fromApp(applicationId)
878 .makePermanent()
879 .forTable(TMAC_TABLE).build();
880 }
881
882 /**
883 * Builds TMAC rule for MPLS packets.
884 *
Charles Chanc550f2e2017-12-19 19:55:57 -0800885 * @param ethCriterion dst mac matching
886 * @param vidCriterion vlan id assigned to the port
887 * @param ofdpaMatchVlanVid OFDPA vlan id matching
888 * @param applicationId application id
889 * @param pnum port number
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530890 * @return TMAC rule for MPLS packets
891 */
892 private FlowRule buildTmacRuleForMpls(EthCriterion ethCriterion,
893 VlanIdCriterion vidCriterion,
894 OfdpaMatchVlanVid ofdpaMatchVlanVid,
895 ApplicationId applicationId,
896 PortNumber pnum) {
897 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
898 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
899 if (pnum != null) {
900 if (matchInPortTmacTable()) {
901 selector.matchInPort(pnum);
902 } else {
Saurav Dasf14d9ef2017-12-05 15:00:23 -0800903 log.debug("Pipeline does not support IN_PORT matching in TMAC table, " +
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530904 "ignoring the IN_PORT criteria");
905 }
906 }
907 if (vidCriterion != null) {
908 if (requireVlanExtensions()) {
909 selector.extension(ofdpaMatchVlanVid, deviceId);
910 } else {
911 selector.matchVlanId(vidCriterion.vlanId());
912 }
913 }
914 selector.matchEthType(Ethernet.MPLS_UNICAST);
915 selector.matchEthDst(ethCriterion.mac());
916 treatment.transition(MPLS_TABLE_0);
917 return DefaultFlowRule.builder()
918 .forDevice(deviceId)
919 .withSelector(selector.build())
920 .withTreatment(treatment.build())
921 .withPriority(DEFAULT_PRIORITY)
922 .fromApp(applicationId)
923 .makePermanent()
924 .forTable(TMAC_TABLE).build();
925 }
926
927 /**
928 * Builds TMAC rules for IPv6 packets.
929 *
Charles Chanc550f2e2017-12-19 19:55:57 -0800930 * @param ethCriterion dst mac matching
931 * @param vidCriterion vlan id assigned to the port
932 * @param ofdpaMatchVlanVid OFDPA vlan id matching
933 * @param applicationId application id
934 * @param pnum port number
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530935 * @return TMAC rule for IPV6 packets
936 */
937 private FlowRule buildTmacRuleForIpv6(EthCriterion ethCriterion,
938 VlanIdCriterion vidCriterion,
939 OfdpaMatchVlanVid ofdpaMatchVlanVid,
940 ApplicationId applicationId,
941 PortNumber pnum) {
942 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
943 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
944 if (pnum != null) {
945 if (matchInPortTmacTable()) {
946 selector.matchInPort(pnum);
947 } else {
Saurav Dasf14d9ef2017-12-05 15:00:23 -0800948 log.debug("Pipeline does not support IN_PORT matching in TMAC table, " +
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530949 "ignoring the IN_PORT criteria");
950 }
951 }
952 if (vidCriterion != null) {
953 if (requireVlanExtensions()) {
954 selector.extension(ofdpaMatchVlanVid, deviceId);
955 } else {
956 selector.matchVlanId(vidCriterion.vlanId());
957 }
958 }
959 selector.matchEthType(Ethernet.TYPE_IPV6);
960 selector.matchEthDst(ethCriterion.mac());
961 treatment.transition(UNICAST_ROUTING_TABLE);
962 return DefaultFlowRule.builder()
963 .forDevice(deviceId)
964 .withSelector(selector.build())
965 .withTreatment(treatment.build())
966 .withPriority(DEFAULT_PRIORITY)
967 .fromApp(applicationId)
968 .makePermanent()
969 .forTable(TMAC_TABLE).build();
970 }
971
Charles Chan66291502018-03-02 16:43:28 -0800972 protected List<List<FlowRule>> processEthDstOnlyFilter(EthCriterion ethCriterion,
Yi Tsengef19de12017-04-24 11:33:05 -0700973 ApplicationId applicationId) {
Pier Luigi0e358632017-01-31 09:35:05 -0800974 ImmutableList.Builder<FlowRule> builder = ImmutableList.builder();
975
Charles Chan5270ed02016-01-30 23:22:37 -0800976 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
977 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
978 selector.matchEthType(Ethernet.TYPE_IPV4);
979 selector.matchEthDst(ethCriterion.mac());
980 treatment.transition(UNICAST_ROUTING_TABLE);
981 FlowRule rule = DefaultFlowRule.builder()
982 .forDevice(deviceId)
983 .withSelector(selector.build())
984 .withTreatment(treatment.build())
985 .withPriority(DEFAULT_PRIORITY)
986 .fromApp(applicationId)
987 .makePermanent()
988 .forTable(TMAC_TABLE).build();
Pier Luigi0e358632017-01-31 09:35:05 -0800989 builder.add(rule);
990
991 selector = DefaultTrafficSelector.builder();
992 treatment = DefaultTrafficTreatment.builder();
993 selector.matchEthType(Ethernet.TYPE_IPV6);
994 selector.matchEthDst(ethCriterion.mac());
995 treatment.transition(UNICAST_ROUTING_TABLE);
996 rule = DefaultFlowRule.builder()
997 .forDevice(deviceId)
998 .withSelector(selector.build())
999 .withTreatment(treatment.build())
1000 .withPriority(DEFAULT_PRIORITY)
1001 .fromApp(applicationId)
1002 .makePermanent()
1003 .forTable(TMAC_TABLE).build();
Charles Chan66291502018-03-02 16:43:28 -08001004 return ImmutableList.of(builder.add(rule).build());
Charles Chan5270ed02016-01-30 23:22:37 -08001005 }
1006
pier5e8b2bc2019-10-04 10:54:07 +02001007 private FlowRule buildTmacRuleForMcastFromUnicast(FlowRule tmacRuleForUnicast, ApplicationId applicationId) {
1008 final TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
1009 final TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
1010 FlowRule rule;
1011 // Build the selector
1012 for (Criterion criterion : tmacRuleForUnicast.selector().criteria()) {
1013 if (criterion instanceof VlanIdCriterion) {
1014 if (requireVlanExtensions()) {
1015 OfdpaMatchVlanVid ofdpaMatchVlanVid = new OfdpaMatchVlanVid(((VlanIdCriterion) criterion).vlanId());
1016 selector.extension(ofdpaMatchVlanVid, deviceId);
1017 } else {
1018 selector.add(criterion);
1019 }
1020 } else if (criterion instanceof EthTypeCriterion) {
1021 selector.add(criterion);
1022 if (Objects.equals(((EthTypeCriterion) criterion).ethType(),
1023 EtherType.IPV4.ethType())) {
1024 selector.matchEthDstMasked(IPV4_MULTICAST, IPV4_MULTICAST_MASK);
1025 } else if (Objects.equals(((EthTypeCriterion) criterion).ethType(),
1026 EtherType.IPV6.ethType())) {
1027 selector.matchEthDstMasked(IPV6_MULTICAST, IPV6_MULTICAST_MASK);
1028 } else {
1029 // We don't need for mpls rules
1030 return null;
1031 }
1032 }
1033 }
1034 // Build the treatment
1035 treatment.transition(MULTICAST_ROUTING_TABLE);
1036 // Build the flowrule
1037 rule = DefaultFlowRule.builder()
1038 .forDevice(deviceId)
1039 .withSelector(selector.build())
1040 .withTreatment(treatment.build())
1041 .withPriority(DEFAULT_PRIORITY)
1042 .fromApp(applicationId)
1043 .makePermanent()
1044 .forTable(TMAC_TABLE).build();
pier776ce252019-12-06 12:39:51 +01001045 log.debug("Building TMac Mcast flowRule {}", rule);
pier5e8b2bc2019-10-04 10:54:07 +02001046 return rule;
1047 }
1048
Charles Chan66291502018-03-02 16:43:28 -08001049 List<List<FlowRule>> processMcastEthDstFilter(EthCriterion ethCriterion,
Charles Chanb4879a52017-10-20 19:09:16 -07001050 VlanId assignedVlan,
Charles Chan45b69ab2018-03-02 15:41:41 -08001051 MacAddress unicastMac,
Yi Tsengef19de12017-04-24 11:33:05 -07001052 ApplicationId applicationId) {
Charles Chan45b69ab2018-03-02 15:41:41 -08001053 ImmutableList.Builder<FlowRule> unicastFlows = ImmutableList.builder();
1054 ImmutableList.Builder<FlowRule> multicastFlows = ImmutableList.builder();
1055 TrafficSelector.Builder selector;
1056 TrafficTreatment.Builder treatment;
Charles Chanb4879a52017-10-20 19:09:16 -07001057 FlowRule rule;
Julia Ferguson65428c32017-08-10 18:15:24 +00001058
Charles Chanb4879a52017-10-20 19:09:16 -07001059 if (IPV4_MULTICAST.equals(ethCriterion.mac())) {
Charles Chan45b69ab2018-03-02 15:41:41 -08001060 if (requireUnicastBeforeMulticast()) {
1061 selector = DefaultTrafficSelector.builder();
1062 treatment = DefaultTrafficTreatment.builder();
1063 selector.matchEthType(Ethernet.TYPE_IPV4);
1064 selector.matchEthDst(unicastMac);
Charles Chan056e0c12018-05-10 22:19:49 +00001065 selector.matchVlanId(assignedVlan);
Charles Chan45b69ab2018-03-02 15:41:41 -08001066 treatment.transition(UNICAST_ROUTING_TABLE);
1067 rule = DefaultFlowRule.builder()
1068 .forDevice(deviceId)
1069 .withSelector(selector.build())
1070 .withTreatment(treatment.build())
Charles Chan056e0c12018-05-10 22:19:49 +00001071 .withPriority(DEFAULT_PRIORITY)
Charles Chan45b69ab2018-03-02 15:41:41 -08001072 .fromApp(applicationId)
1073 .makePermanent()
1074 .forTable(TMAC_TABLE).build();
1075 unicastFlows.add(rule);
1076 }
1077
1078 selector = DefaultTrafficSelector.builder();
1079 treatment = DefaultTrafficTreatment.builder();
Charles Chanb4879a52017-10-20 19:09:16 -07001080 selector.matchEthType(Ethernet.TYPE_IPV4);
1081 selector.matchEthDstMasked(ethCriterion.mac(), ethCriterion.mask());
Charles Chan056e0c12018-05-10 22:19:49 +00001082 selector.matchVlanId(assignedVlan);
Charles Chanb4879a52017-10-20 19:09:16 -07001083 treatment.transition(MULTICAST_ROUTING_TABLE);
1084 rule = DefaultFlowRule.builder()
1085 .forDevice(deviceId)
1086 .withSelector(selector.build())
1087 .withTreatment(treatment.build())
Charles Chan056e0c12018-05-10 22:19:49 +00001088 .withPriority(DEFAULT_PRIORITY)
Charles Chanb4879a52017-10-20 19:09:16 -07001089 .fromApp(applicationId)
1090 .makePermanent()
1091 .forTable(TMAC_TABLE).build();
Charles Chan45b69ab2018-03-02 15:41:41 -08001092 multicastFlows.add(rule);
Charles Chanb4879a52017-10-20 19:09:16 -07001093 }
1094
1095 if (IPV6_MULTICAST.equals(ethCriterion.mac())) {
Charles Chan45b69ab2018-03-02 15:41:41 -08001096 if (requireUnicastBeforeMulticast()) {
1097 selector = DefaultTrafficSelector.builder();
1098 treatment = DefaultTrafficTreatment.builder();
1099 selector.matchEthType(Ethernet.TYPE_IPV6);
1100 selector.matchEthDst(unicastMac);
Charles Chan056e0c12018-05-10 22:19:49 +00001101 selector.matchVlanId(assignedVlan);
Charles Chan45b69ab2018-03-02 15:41:41 -08001102 treatment.transition(UNICAST_ROUTING_TABLE);
1103 rule = DefaultFlowRule.builder()
1104 .forDevice(deviceId)
1105 .withSelector(selector.build())
1106 .withTreatment(treatment.build())
Charles Chan056e0c12018-05-10 22:19:49 +00001107 .withPriority(DEFAULT_PRIORITY)
Charles Chan45b69ab2018-03-02 15:41:41 -08001108 .fromApp(applicationId)
1109 .makePermanent()
1110 .forTable(TMAC_TABLE).build();
1111 unicastFlows.add(rule);
1112 }
1113
Charles Chanb4879a52017-10-20 19:09:16 -07001114 selector = DefaultTrafficSelector.builder();
1115 treatment = DefaultTrafficTreatment.builder();
1116 selector.matchEthType(Ethernet.TYPE_IPV6);
1117 selector.matchEthDstMasked(ethCriterion.mac(), ethCriterion.mask());
Charles Chan056e0c12018-05-10 22:19:49 +00001118 selector.matchVlanId(assignedVlan);
Charles Chanb4879a52017-10-20 19:09:16 -07001119 treatment.transition(MULTICAST_ROUTING_TABLE);
1120 rule = DefaultFlowRule.builder()
1121 .forDevice(deviceId)
1122 .withSelector(selector.build())
1123 .withTreatment(treatment.build())
Charles Chan056e0c12018-05-10 22:19:49 +00001124 .withPriority(DEFAULT_PRIORITY)
Charles Chanb4879a52017-10-20 19:09:16 -07001125 .fromApp(applicationId)
1126 .makePermanent()
1127 .forTable(TMAC_TABLE).build();
Charles Chan45b69ab2018-03-02 15:41:41 -08001128 multicastFlows.add(rule);
Charles Chanb4879a52017-10-20 19:09:16 -07001129 }
Charles Chan45b69ab2018-03-02 15:41:41 -08001130 return ImmutableList.of(unicastFlows.build(), multicastFlows.build());
Charles Chan5b9df8d2016-03-28 22:21:40 -07001131 }
1132
Saurav Das822c4e22015-10-23 10:51:11 -07001133 private Collection<FlowRule> processForward(ForwardingObjective fwd) {
1134 switch (fwd.flag()) {
1135 case SPECIFIC:
1136 return processSpecific(fwd);
1137 case VERSATILE:
1138 return processVersatile(fwd);
Jonghwan Hyun800d9d02018-04-09 09:40:50 -07001139 case EGRESS:
1140 return processEgress(fwd);
Saurav Das822c4e22015-10-23 10:51:11 -07001141 default:
1142 fail(fwd, ObjectiveError.UNKNOWN);
1143 log.warn("Unknown forwarding flag {}", fwd.flag());
1144 }
1145 return Collections.emptySet();
1146 }
1147
1148 /**
Jonghwan Hyun800d9d02018-04-09 09:40:50 -07001149 * In the OF-DPA 2.0 pipeline, egress forwarding objectives go to the
1150 * egress tables.
1151 * @param fwd the forwarding objective of type 'egress'
1152 * @return a collection of flow rules to be sent to the switch. An empty
1153 * collection may be returned if there is a problem in processing
1154 * the flow rule
1155 */
1156 protected Collection<FlowRule> processEgress(ForwardingObjective fwd) {
1157 log.debug("Processing egress forwarding objective:{} in dev:{}",
1158 fwd, deviceId);
1159
1160 List<FlowRule> rules = new ArrayList<>();
1161
1162 // Build selector
1163 TrafficSelector.Builder sb = DefaultTrafficSelector.builder();
1164 VlanIdCriterion vlanIdCriterion = (VlanIdCriterion) fwd.selector().getCriterion(Criterion.Type.VLAN_VID);
1165 if (vlanIdCriterion == null) {
1166 log.error("Egress forwarding objective:{} must include vlanId", fwd.id());
1167 fail(fwd, ObjectiveError.BADPARAMS);
1168 return rules;
1169 }
1170
1171 Optional<Instruction> outInstr = fwd.treatment().allInstructions().stream()
1172 .filter(instruction -> instruction instanceof OutputInstruction).findFirst();
1173 if (!outInstr.isPresent()) {
1174 log.error("Egress forwarding objective:{} must include output port", fwd.id());
1175 fail(fwd, ObjectiveError.BADPARAMS);
1176 return rules;
1177 }
1178
1179 PortNumber portNumber = ((OutputInstruction) outInstr.get()).port();
1180
1181 sb.matchVlanId(vlanIdCriterion.vlanId());
1182 OfdpaMatchActsetOutput actsetOutput = new OfdpaMatchActsetOutput(portNumber);
1183 sb.extension(actsetOutput, deviceId);
1184
1185 sb.extension(new OfdpaMatchAllowVlanTranslation(ALLOW_VLAN_TRANSLATION), deviceId);
1186
1187 // Build a flow rule for Egress VLAN Flow table
1188 TrafficTreatment.Builder tb = DefaultTrafficTreatment.builder();
1189 tb.transition(EGRESS_DSCP_PCP_REMARK_FLOW_TABLE);
1190 if (fwd.treatment() != null) {
1191 for (Instruction instr : fwd.treatment().allInstructions()) {
1192 if (instr instanceof L2ModificationInstruction &&
1193 ((L2ModificationInstruction) instr).subtype() == L2SubType.VLAN_ID) {
1194 tb.immediate().add(instr);
1195 }
1196 if (instr instanceof L2ModificationInstruction &&
1197 ((L2ModificationInstruction) instr).subtype() == L2SubType.VLAN_PUSH) {
1198 tb.immediate().pushVlan();
1199 EthType ethType = ((L2ModificationInstruction.ModVlanHeaderInstruction) instr).ethernetType();
1200 if (ethType.equals(EtherType.QINQ.ethType())) {
1201 // Build a flow rule for Egress TPID Flow table
1202 TrafficSelector tpidSelector = DefaultTrafficSelector.builder()
1203 .extension(actsetOutput, deviceId)
1204 .matchVlanId(VlanId.ANY).build();
1205
1206 TrafficTreatment tpidTreatment = DefaultTrafficTreatment.builder()
1207 .extension(new Ofdpa3CopyField(COPY_FIELD_NBITS, COPY_FIELD_OFFSET,
1208 COPY_FIELD_OFFSET, OXM_ID_VLAN_VID,
1209 OXM_ID_PACKET_REG_1),
1210 deviceId)
1211 .popVlan()
1212 .pushVlan(EtherType.QINQ.ethType())
1213 .extension(new Ofdpa3CopyField(COPY_FIELD_NBITS, COPY_FIELD_OFFSET,
1214 COPY_FIELD_OFFSET, OXM_ID_PACKET_REG_1,
1215 OXM_ID_VLAN_VID),
1216 deviceId)
1217 .build();
1218
1219 FlowRule.Builder tpidRuleBuilder = DefaultFlowRule.builder()
1220 .fromApp(fwd.appId())
1221 .withPriority(fwd.priority())
1222 .forDevice(deviceId)
1223 .withSelector(tpidSelector)
1224 .withTreatment(tpidTreatment)
1225 .makePermanent()
1226 .forTable(EGRESS_TPID_FLOW_TABLE);
1227 rules.add(tpidRuleBuilder.build());
1228 }
1229 }
1230 }
1231 }
1232
1233 FlowRule.Builder ruleBuilder = DefaultFlowRule.builder()
1234 .fromApp(fwd.appId())
1235 .withPriority(fwd.priority())
1236 .forDevice(deviceId)
1237 .withSelector(sb.build())
1238 .withTreatment(tb.build())
1239 .makePermanent()
1240 .forTable(EGRESS_VLAN_FLOW_TABLE);
1241 rules.add(ruleBuilder.build());
1242 return rules;
1243 }
1244
1245 /**
Saurav Das822c4e22015-10-23 10:51:11 -07001246 * In the OF-DPA 2.0 pipeline, versatile forwarding objectives go to the
1247 * ACL table.
1248 * @param fwd the forwarding objective of type 'versatile'
1249 * @return a collection of flow rules to be sent to the switch. An empty
1250 * collection may be returned if there is a problem in processing
1251 * the flow rule
1252 */
Saurav Das52025962016-01-28 22:30:01 -08001253 protected Collection<FlowRule> processVersatile(ForwardingObjective fwd) {
Saurav Das018605f2017-02-18 14:05:44 -08001254 log.debug("Processing versatile forwarding objective:{} in dev:{}",
Saurav Dasd2fded02016-12-02 15:43:47 -08001255 fwd.id(), deviceId);
Harshada Chaundkar77958a52019-08-05 15:33:30 +00001256 List<FlowRule> flowRules = new ArrayList<>();
1257 final AtomicBoolean ethTypeUsed = new AtomicBoolean(false);
Saurav Das822c4e22015-10-23 10:51:11 -07001258
Saurav Das822c4e22015-10-23 10:51:11 -07001259 if (fwd.nextId() == null && fwd.treatment() == null) {
1260 log.error("Forwarding objective {} from {} must contain "
1261 + "nextId or Treatment", fwd.selector(), fwd.appId());
Zsolt Haraszti9faab752016-02-17 15:55:20 -08001262 fail(fwd, ObjectiveError.BADPARAMS);
Saurav Das822c4e22015-10-23 10:51:11 -07001263 return Collections.emptySet();
1264 }
Saurav Das49cb5a12016-01-16 22:54:07 -08001265
Saurav Das77b5e902016-01-27 17:01:59 -08001266 TrafficSelector.Builder sbuilder = DefaultTrafficSelector.builder();
1267 fwd.selector().criteria().forEach(criterion -> {
1268 if (criterion instanceof VlanIdCriterion) {
1269 VlanId vlanId = ((VlanIdCriterion) criterion).vlanId();
1270 // ensure that match does not include vlan = NONE as OF-DPA does not
1271 // match untagged packets this way in the ACL table.
1272 if (vlanId.equals(VlanId.NONE)) {
1273 return;
1274 }
Charles Chand1172632017-03-15 17:33:09 -07001275 if (requireVlanExtensions()) {
1276 OfdpaMatchVlanVid ofdpaMatchVlanVid = new OfdpaMatchVlanVid(vlanId);
1277 sbuilder.extension(ofdpaMatchVlanVid, deviceId);
1278 } else {
1279 sbuilder.matchVlanId(vlanId);
1280 }
Charles Chan09bf2692018-01-11 11:48:18 -08001281 } else if (criterion instanceof Icmpv6TypeCriterion) {
1282 byte icmpv6Type = (byte) ((Icmpv6TypeCriterion) criterion).icmpv6Type();
1283 sbuilder.matchIcmpv6Type(icmpv6Type);
1284 } else if (criterion instanceof Icmpv6CodeCriterion) {
1285 byte icmpv6Code = (byte) ((Icmpv6CodeCriterion) criterion).icmpv6Code();
1286 sbuilder.matchIcmpv6Type(icmpv6Code);
Charles Chand9e47c62017-10-05 15:17:15 -07001287 } else if (criterion instanceof TcpPortCriterion || criterion instanceof UdpPortCriterion) {
1288 // FIXME: QMX switches do not support L4 dst port matching in ACL table.
1289 // Currently L4 dst port matching is only used by DHCP relay feature
1290 // and therefore is safe to be replaced with L4 src port matching.
1291 // We need to revisit this if L4 dst port is used for other purpose in the future.
1292 if (!supportIpv6L4Dst() && isIpv6(fwd.selector())) {
Charles Chand9e47c62017-10-05 15:17:15 -07001293 switch (criterion.type()) {
1294 case UDP_DST:
Charles Chand9e47c62017-10-05 15:17:15 -07001295 case UDP_DST_MASKED:
Charles Chand9e47c62017-10-05 15:17:15 -07001296 case TCP_DST:
Charles Chand9e47c62017-10-05 15:17:15 -07001297 case TCP_DST_MASKED:
Charles Chand9e47c62017-10-05 15:17:15 -07001298 break;
1299 default:
1300 sbuilder.add(criterion);
1301 }
1302 } else {
1303 sbuilder.add(criterion);
1304 }
Harshada Chaundkar77958a52019-08-05 15:33:30 +00001305 } else if (criterion instanceof EthTypeCriterion) {
1306 sbuilder.add(criterion);
1307 ethTypeUsed.set(true);
Saurav Das77b5e902016-01-27 17:01:59 -08001308 } else {
1309 sbuilder.add(criterion);
1310 }
1311 });
Harshada Chaundkar77958a52019-08-05 15:33:30 +00001312
1313 TrafficTreatment.Builder ttBuilder = versatileTreatmentBuilder(fwd);
1314 if (ttBuilder == null) {
1315 return Collections.emptySet();
1316 }
1317
1318 FlowRule.Builder ruleBuilder = DefaultFlowRule.builder()
1319 .fromApp(fwd.appId())
1320 .withPriority(fwd.priority())
1321 .forDevice(deviceId)
1322 .withSelector(sbuilder.build())
1323 .withTreatment(ttBuilder.build())
1324 .makePermanent()
1325 .forTable(ACL_TABLE);
1326
1327 flowRules.add(ruleBuilder.build());
1328
1329 if (!ethTypeUsed.get() && requireEthType()) {
1330 log.debug("{} doesn't match on ethType but requireEthType is true, adding complementary ACL flow.",
1331 sbuilder.toString());
1332 sbuilder.matchEthType(Ethernet.TYPE_IPV6);
1333 FlowRule.Builder ethTypeRuleBuilder = DefaultFlowRule.builder()
1334 .fromApp(fwd.appId())
1335 .withPriority(fwd.priority())
1336 .forDevice(deviceId)
1337 .withSelector(sbuilder.build())
1338 .withTreatment(ttBuilder.build())
1339 .makePermanent()
1340 .forTable(ACL_TABLE);
1341 flowRules.add(ethTypeRuleBuilder.build());
1342 }
1343 return flowRules;
Charles Chanab591602019-01-22 17:25:04 -08001344 }
Saurav Das77b5e902016-01-27 17:01:59 -08001345
Charles Chanab591602019-01-22 17:25:04 -08001346 /**
1347 * Helper function to create traffic treatment builder for versatile forwarding objectives.
1348 *
1349 * @param fwd original forwarding objective
1350 * @return treatment builder for the flow rule, or null if there is an error.
1351 */
1352 protected TrafficTreatment.Builder versatileTreatmentBuilder(ForwardingObjective fwd) {
Saurav Das822c4e22015-10-23 10:51:11 -07001353 // XXX driver does not currently do type checking as per Tables 65-67 in
1354 // OFDPA 2.0 spec. The only allowed treatment is a punt to the controller.
Saurav Das49cb5a12016-01-16 22:54:07 -08001355 TrafficTreatment.Builder ttBuilder = DefaultTrafficTreatment.builder();
1356 if (fwd.treatment() != null) {
1357 for (Instruction ins : fwd.treatment().allInstructions()) {
1358 if (ins instanceof OutputInstruction) {
1359 OutputInstruction o = (OutputInstruction) ins;
Ray Milkeyfe0e0852018-01-18 11:14:05 -08001360 if (PortNumber.CONTROLLER.equals(o.port())) {
Saurav Das49cb5a12016-01-16 22:54:07 -08001361 ttBuilder.add(o);
1362 } else {
1363 log.warn("Only allowed treatments in versatile forwarding "
1364 + "objectives are punts to the controller");
1365 }
Charles Chanf76de302018-06-15 18:54:18 -07001366 } else if (ins instanceof NoActionInstruction) {
1367 // No action is allowed and nothing needs to be done
Saurav Das49cb5a12016-01-16 22:54:07 -08001368 } else {
1369 log.warn("Cannot process instruction in versatile fwd {}", ins);
1370 }
Saurav Das822c4e22015-10-23 10:51:11 -07001371 }
Charles Chan2df0e8a2017-01-09 11:45:08 -08001372 if (fwd.treatment().clearedDeferred()) {
1373 ttBuilder.wipeDeferred();
1374 }
Saurav Das822c4e22015-10-23 10:51:11 -07001375 }
Saurav Das822c4e22015-10-23 10:51:11 -07001376 if (fwd.nextId() != null) {
Charles Chanab591602019-01-22 17:25:04 -08001377 // Override case
Saurav Das49cb5a12016-01-16 22:54:07 -08001378 NextGroup next = getGroupForNextObjective(fwd.nextId());
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +05301379 if (next == null) {
1380 fail(fwd, ObjectiveError.BADPARAMS);
Charles Chanab591602019-01-22 17:25:04 -08001381 return null;
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +05301382 }
Saurav Das49cb5a12016-01-16 22:54:07 -08001383 List<Deque<GroupKey>> gkeys = appKryo.deserialize(next.data());
1384 // we only need the top level group's key to point the flow to it
1385 Group group = groupService.getGroup(deviceId, gkeys.get(0).peekFirst());
1386 if (group == null) {
1387 log.warn("Group with key:{} for next-id:{} not found in dev:{}",
Charles Chanab591602019-01-22 17:25:04 -08001388 gkeys.get(0).peekFirst(), fwd.nextId(), deviceId);
Saurav Das49cb5a12016-01-16 22:54:07 -08001389 fail(fwd, ObjectiveError.GROUPMISSING);
Charles Chanab591602019-01-22 17:25:04 -08001390 return null;
Saurav Das49cb5a12016-01-16 22:54:07 -08001391 }
1392 ttBuilder.deferred().group(group.id());
Saurav Das822c4e22015-10-23 10:51:11 -07001393 }
Charles Chanab591602019-01-22 17:25:04 -08001394 return ttBuilder;
Saurav Das822c4e22015-10-23 10:51:11 -07001395 }
1396
1397 /**
1398 * In the OF-DPA 2.0 pipeline, specific forwarding refers to the IP table
Saurav Das8a0732e2015-11-20 15:27:53 -08001399 * (unicast or multicast) or the L2 table (mac + vlan) or the MPLS table.
Saurav Das822c4e22015-10-23 10:51:11 -07001400 *
1401 * @param fwd the forwarding objective of type 'specific'
1402 * @return a collection of flow rules. Typically there will be only one
1403 * for this type of forwarding objective. An empty set may be
1404 * returned if there is an issue in processing the objective.
1405 */
Charles Chan50d900c2018-03-02 13:26:22 -08001406 private Collection<FlowRule> processSpecific(ForwardingObjective fwd) {
Saurav Das25190812016-05-27 13:54:07 -07001407 log.debug("Processing specific fwd objective:{} in dev:{} with next:{}",
Saurav Das4ce45962015-11-24 23:21:05 -08001408 fwd.id(), deviceId, fwd.nextId());
1409 boolean isEthTypeObj = isSupportedEthTypeObjective(fwd);
1410 boolean isEthDstObj = isSupportedEthDstObjective(fwd);
1411
1412 if (isEthTypeObj) {
1413 return processEthTypeSpecific(fwd);
1414 } else if (isEthDstObj) {
1415 return processEthDstSpecific(fwd);
1416 } else {
1417 log.warn("processSpecific: Unsupported forwarding objective "
1418 + "criteria fwd:{} in dev:{}", fwd.nextId(), deviceId);
Saurav Das822c4e22015-10-23 10:51:11 -07001419 fail(fwd, ObjectiveError.UNSUPPORTED);
1420 return Collections.emptySet();
1421 }
Saurav Das4ce45962015-11-24 23:21:05 -08001422 }
1423
Saurav Das4ce45962015-11-24 23:21:05 -08001424 /**
1425 * Handles forwarding rules to the IP and MPLS tables.
1426 *
1427 * @param fwd the forwarding objective
1428 * @return A collection of flow rules, or an empty set
1429 */
1430 protected Collection<FlowRule> processEthTypeSpecific(ForwardingObjective fwd) {
Charles Chancad338a2016-09-16 18:03:11 -07001431 return processEthTypeSpecificInternal(fwd, false, ACL_TABLE);
Charles Chanf9e98652016-09-07 16:54:23 -07001432 }
1433
1434 /**
1435 * Internal implementation of processEthTypeSpecific.
1436 * <p>
1437 * Wildcarded IPv4_DST is not supported in OFDPA i12. Therefore, we break
1438 * the rule into 0.0.0.0/1 and 128.0.0.0/1.
1439 * The allowDefaultRoute must be set to false for OFDPA i12.
1440 * </p>
1441 *
1442 * @param fwd the forwarding objective
1443 * @param allowDefaultRoute allow wildcarded IPv4_DST or not
Ray Milkey0bb1e102016-11-10 14:51:27 -08001444 * @param mplsNextTable next MPLS table
Charles Chanf9e98652016-09-07 16:54:23 -07001445 * @return A collection of flow rules, or an empty set
1446 */
1447 protected Collection<FlowRule> processEthTypeSpecificInternal(ForwardingObjective fwd,
Charles Chancad338a2016-09-16 18:03:11 -07001448 boolean allowDefaultRoute,
1449 int mplsNextTable) {
Saurav Das4ce45962015-11-24 23:21:05 -08001450 TrafficSelector selector = fwd.selector();
1451 EthTypeCriterion ethType =
1452 (EthTypeCriterion) selector.getCriterion(Criterion.Type.ETH_TYPE);
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001453 boolean emptyGroup = false;
Charles Chan188ebf52015-12-23 00:15:11 -08001454 int forTableId;
Saurav Das8a0732e2015-11-20 15:27:53 -08001455 TrafficSelector.Builder filteredSelector = DefaultTrafficSelector.builder();
Charles Chan14967c22015-12-07 11:11:50 -08001456 TrafficTreatment.Builder tb = DefaultTrafficTreatment.builder();
Flavio Castroe10fa242016-01-15 12:43:51 -08001457 TrafficSelector.Builder complementarySelector = DefaultTrafficSelector.builder();
Charles Chan14967c22015-12-07 11:11:50 -08001458
Saurav Das8a0732e2015-11-20 15:27:53 -08001459 if (ethType.ethType().toShort() == Ethernet.TYPE_IPV4) {
Pier Luigi3bfe32c2017-01-30 09:47:36 -08001460 if (buildIpv4Selector(filteredSelector, complementarySelector, fwd, allowDefaultRoute) < 0) {
1461 return Collections.emptyList();
1462 }
1463 // We need to set properly the next table
Flavio Castroe10fa242016-01-15 12:43:51 -08001464 IpPrefix ipv4Dst = ((IPCriterion) selector.getCriterion(Criterion.Type.IPV4_DST)).ip();
Charles Chan5b9df8d2016-03-28 22:21:40 -07001465 if (ipv4Dst.isMulticast()) {
Charles Chan5b9df8d2016-03-28 22:21:40 -07001466 forTableId = MULTICAST_ROUTING_TABLE;
Flavio Castroe10fa242016-01-15 12:43:51 -08001467 } else {
Charles Chan5b9df8d2016-03-28 22:21:40 -07001468 forTableId = UNICAST_ROUTING_TABLE;
Flavio Castroe10fa242016-01-15 12:43:51 -08001469 }
Charles Chan50d900c2018-03-02 13:26:22 -08001470
Charles Chan236653d2018-03-05 11:28:23 -08001471 // TODO decrementing IP ttl is done automatically for routing, this
Charles Chan50d900c2018-03-02 13:26:22 -08001472 // action is ignored or rejected in ofdpa as it is not fully implemented
Charles Chan236653d2018-03-05 11:28:23 -08001473 // if (fwd.treatment() != null) {
1474 // for (Instruction instr : fwd.treatment().allInstructions()) {
1475 // if (instr instanceof L3ModificationInstruction &&
1476 // ((L3ModificationInstruction) instr).subtype() == L3SubType.DEC_TTL) {
1477 // tb.deferred().add(instr);
1478 // }
1479 // }
1480 // }
1481
Pier Ventree0ae7a32016-11-23 09:57:42 -08001482 } else if (ethType.ethType().toShort() == Ethernet.TYPE_IPV6) {
1483 if (buildIpv6Selector(filteredSelector, fwd) < 0) {
1484 return Collections.emptyList();
1485 }
Kalhee Kim3d26bf02017-10-04 14:59:11 +00001486 //We need to set the proper next table
1487 IpPrefix ipv6Dst = ((IPCriterion) selector.getCriterion(Criterion.Type.IPV6_DST)).ip();
1488 if (ipv6Dst.isMulticast()) {
1489 forTableId = MULTICAST_ROUTING_TABLE;
1490 } else {
1491 forTableId = UNICAST_ROUTING_TABLE;
1492 }
1493
Charles Chan236653d2018-03-05 11:28:23 -08001494 // TODO decrementing IP ttl is done automatically for routing, this
Charles Chan50d900c2018-03-02 13:26:22 -08001495 // action is ignored or rejected in ofdpa as it is not fully implemented
Charles Chan236653d2018-03-05 11:28:23 -08001496 // if (fwd.treatment() != null) {
1497 // for (Instruction instr : fwd.treatment().allInstructions()) {
1498 // if (instr instanceof L3ModificationInstruction &&
1499 // ((L3ModificationInstruction) instr).subtype() == L3SubType.DEC_TTL) {
1500 // tb.deferred().add(instr);
1501 // }
1502 // }
1503 // }
Saurav Das8a0732e2015-11-20 15:27:53 -08001504 } else {
1505 filteredSelector
1506 .matchEthType(Ethernet.MPLS_UNICAST)
1507 .matchMplsLabel(((MplsCriterion)
1508 selector.getCriterion(Criterion.Type.MPLS_LABEL)).label());
1509 MplsBosCriterion bos = (MplsBosCriterion) selector
Pier Ventre140a8942016-11-02 07:26:38 -07001510 .getCriterion(MPLS_BOS);
Alex Yashchuk4caa8e82017-12-08 17:40:05 +02001511 if (bos != null && requireMplsBosMatch()) {
Saurav Das8a0732e2015-11-20 15:27:53 -08001512 filteredSelector.matchMplsBos(bos.mplsBos());
1513 }
1514 forTableId = MPLS_TABLE_1;
Saurav Das4ce45962015-11-24 23:21:05 -08001515 log.debug("processing MPLS specific forwarding objective {} -> next:{}"
1516 + " in dev {}", fwd.id(), fwd.nextId(), deviceId);
Saurav Das822c4e22015-10-23 10:51:11 -07001517
Charles Chan14967c22015-12-07 11:11:50 -08001518 if (fwd.treatment() != null) {
1519 for (Instruction instr : fwd.treatment().allInstructions()) {
1520 if (instr instanceof L2ModificationInstruction &&
1521 ((L2ModificationInstruction) instr).subtype() == L2SubType.MPLS_POP) {
Pier Luigi3bfe32c2017-01-30 09:47:36 -08001522 // OF-DPA does not pop in MPLS table in some cases. For the L3 VPN, it requires
Saurav Das9c705342017-01-06 11:36:01 -08001523 // setting the MPLS_TYPE so pop can happen down the pipeline
Alex Yashchuk4caa8e82017-12-08 17:40:05 +02001524 if (requireMplsPop()) {
1525 if (mplsNextTable == MPLS_TYPE_TABLE && isNotMplsBos(selector)) {
1526 tb.immediate().popMpls();
1527 }
1528 } else {
1529 // Skip mpls pop action for mpls_unicast label
1530 if (instr instanceof ModMplsHeaderInstruction &&
1531 !((ModMplsHeaderInstruction) instr).ethernetType()
1532 .equals(EtherType.MPLS_UNICAST.ethType())) {
1533 tb.immediate().add(instr);
1534 }
Pier Luigi3bfe32c2017-01-30 09:47:36 -08001535 }
Charles Chan14967c22015-12-07 11:11:50 -08001536 }
Alex Yashchuk4caa8e82017-12-08 17:40:05 +02001537
1538 if (requireMplsTtlModification()) {
1539 if (instr instanceof L3ModificationInstruction &&
1540 ((L3ModificationInstruction) instr).subtype() == L3SubType.DEC_TTL) {
1541 // FIXME Should modify the app to send the correct DEC_MPLS_TTL instruction
1542 tb.immediate().decMplsTtl();
1543 }
1544 if (instr instanceof L3ModificationInstruction &&
1545 ((L3ModificationInstruction) instr).subtype() == L3SubType.TTL_IN) {
1546 tb.immediate().add(instr);
1547 }
Charles Chan14967c22015-12-07 11:11:50 -08001548 }
Saurav Das8a0732e2015-11-20 15:27:53 -08001549 }
1550 }
1551 }
Saurav Das822c4e22015-10-23 10:51:11 -07001552
1553 if (fwd.nextId() != null) {
Saurav Das423fe2b2015-12-04 10:52:59 -08001554 NextGroup next = getGroupForNextObjective(fwd.nextId());
1555 if (next != null) {
1556 List<Deque<GroupKey>> gkeys = appKryo.deserialize(next.data());
1557 // we only need the top level group's key to point the flow to it
1558 Group group = groupService.getGroup(deviceId, gkeys.get(0).peekFirst());
1559 if (group == null) {
1560 log.warn("Group with key:{} for next-id:{} not found in dev:{}",
1561 gkeys.get(0).peekFirst(), fwd.nextId(), deviceId);
1562 fail(fwd, ObjectiveError.GROUPMISSING);
1563 return Collections.emptySet();
1564 }
Saurav Dasa4020382018-02-14 14:14:54 -08001565 if (isNotMplsBos(selector) && group.type().equals(SELECT)) {
1566 log.warn("SR CONTINUE case cannot be handled as MPLS ECMP "
1567 + "is not implemented in OF-DPA yet. Aborting flow {} -> next:{} "
1568 + "in this device {}", fwd.id(), fwd.nextId(), deviceId);
1569 fail(fwd, ObjectiveError.FLOWINSTALLATIONFAILED);
1570 return Collections.emptySet();
1571 }
Saurav Das423fe2b2015-12-04 10:52:59 -08001572 tb.deferred().group(group.id());
Saurav Dasc568c342018-01-25 09:49:01 -08001573 // retrying flows may be necessary due to bug CORD-554
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001574 if (gkeys.size() == 1 && gkeys.get(0).size() == 1) {
Saurav Dasc568c342018-01-25 09:49:01 -08001575 if (shouldRetry()) {
1576 log.warn("Found empty group 0x{} in dev:{} .. will retry fwd:{}",
1577 Integer.toHexString(group.id().id()), deviceId,
1578 fwd.id());
1579 emptyGroup = true;
1580 }
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001581 }
Saurav Das25190812016-05-27 13:54:07 -07001582 } else {
1583 log.warn("Cannot find group for nextId:{} in dev:{}. Aborting fwd:{}",
1584 fwd.nextId(), deviceId, fwd.id());
1585 fail(fwd, ObjectiveError.FLOWINSTALLATIONFAILED);
1586 return Collections.emptySet();
Saurav Das822c4e22015-10-23 10:51:11 -07001587 }
Saurav Das822c4e22015-10-23 10:51:11 -07001588 }
Charles Chancad338a2016-09-16 18:03:11 -07001589
1590 if (forTableId == MPLS_TABLE_1) {
Pier Ventre140a8942016-11-02 07:26:38 -07001591 if (mplsNextTable == MPLS_L3_TYPE_TABLE) {
Charles Chancad338a2016-09-16 18:03:11 -07001592 Ofdpa3SetMplsType setMplsType = new Ofdpa3SetMplsType(Ofdpa3MplsType.L3_PHP);
Saurav Das9c705342017-01-06 11:36:01 -08001593 // set mpls type as apply_action
1594 tb.immediate().extension(setMplsType, deviceId);
Charles Chancad338a2016-09-16 18:03:11 -07001595 }
1596 tb.transition(mplsNextTable);
1597 } else {
1598 tb.transition(ACL_TABLE);
1599 }
1600
Andrea Campanellad980c6d2018-04-30 11:48:55 +02001601 if (fwd.treatment() != null && fwd.treatment().clearedDeferred()) {
1602 if (supportsUnicastBlackHole()) {
1603 tb.wipeDeferred();
1604 } else {
1605 log.warn("Clear Deferred is not supported Unicast Routing Table on device {}", deviceId);
1606 return Collections.emptySet();
1607 }
1608 }
1609
Saurav Das822c4e22015-10-23 10:51:11 -07001610 FlowRule.Builder ruleBuilder = DefaultFlowRule.builder()
1611 .fromApp(fwd.appId())
1612 .withPriority(fwd.priority())
1613 .forDevice(deviceId)
Saurav Das8a0732e2015-11-20 15:27:53 -08001614 .withSelector(filteredSelector.build())
1615 .withTreatment(tb.build())
1616 .forTable(forTableId);
Saurav Das822c4e22015-10-23 10:51:11 -07001617
1618 if (fwd.permanent()) {
1619 ruleBuilder.makePermanent();
1620 } else {
1621 ruleBuilder.makeTemporary(fwd.timeout());
1622 }
Flavio Castroe10fa242016-01-15 12:43:51 -08001623 Collection<FlowRule> flowRuleCollection = new ArrayList<>();
1624 flowRuleCollection.add(ruleBuilder.build());
Pier Luigi3bfe32c2017-01-30 09:47:36 -08001625 if (!allowDefaultRoute) {
Pier Ventree0ae7a32016-11-23 09:57:42 -08001626 flowRuleCollection.add(
1627 defaultRoute(fwd, complementarySelector, forTableId, tb)
1628 );
Flavio Castroe10fa242016-01-15 12:43:51 -08001629 log.debug("Default rule 0.0.0.0/0 is being installed two rules");
1630 }
Saurav Dasc568c342018-01-25 09:49:01 -08001631
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001632 if (emptyGroup) {
pier8b3aef42019-03-11 15:14:02 -07001633 retryExecutorService.schedule(new RetryFlows(fwd, flowRuleCollection),
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001634 RETRY_MS, TimeUnit.MILLISECONDS);
1635 }
Flavio Castroe10fa242016-01-15 12:43:51 -08001636 return flowRuleCollection;
Saurav Das822c4e22015-10-23 10:51:11 -07001637 }
1638
Charles Chan50d900c2018-03-02 13:26:22 -08001639 private int buildIpv4Selector(TrafficSelector.Builder builderToUpdate,
Pier Luigi3bfe32c2017-01-30 09:47:36 -08001640 TrafficSelector.Builder extBuilder,
1641 ForwardingObjective fwd,
1642 boolean allowDefaultRoute) {
1643 TrafficSelector selector = fwd.selector();
1644
1645 IpPrefix ipv4Dst = ((IPCriterion) selector.getCriterion(Criterion.Type.IPV4_DST)).ip();
1646 if (ipv4Dst.isMulticast()) {
1647 if (ipv4Dst.prefixLength() != 32) {
1648 log.warn("Multicast specific forwarding objective can only be /32");
1649 fail(fwd, ObjectiveError.BADPARAMS);
1650 return -1;
1651 }
1652 VlanId assignedVlan = readVlanFromSelector(fwd.meta());
1653 if (assignedVlan == null) {
1654 log.warn("VLAN ID required by multicast specific fwd obj is missing. Abort.");
1655 fail(fwd, ObjectiveError.BADPARAMS);
1656 return -1;
1657 }
Charles Chand1172632017-03-15 17:33:09 -07001658 if (requireVlanExtensions()) {
1659 OfdpaMatchVlanVid ofdpaMatchVlanVid = new OfdpaMatchVlanVid(assignedVlan);
1660 builderToUpdate.extension(ofdpaMatchVlanVid, deviceId);
1661 } else {
1662 builderToUpdate.matchVlanId(assignedVlan);
1663 }
Pier Luigi3bfe32c2017-01-30 09:47:36 -08001664 builderToUpdate.matchEthType(Ethernet.TYPE_IPV4).matchIPDst(ipv4Dst);
1665 log.debug("processing IPv4 multicast specific forwarding objective {} -> next:{}"
1666 + " in dev:{}", fwd.id(), fwd.nextId(), deviceId);
1667 } else {
1668 if (ipv4Dst.prefixLength() == 0) {
1669 if (allowDefaultRoute) {
1670 // The entire IPV4_DST field is wildcarded intentionally
1671 builderToUpdate.matchEthType(Ethernet.TYPE_IPV4);
1672 } else {
1673 // NOTE: The switch does not support matching 0.0.0.0/0
1674 // Split it into 0.0.0.0/1 and 128.0.0.0/1
1675 builderToUpdate.matchEthType(Ethernet.TYPE_IPV4)
1676 .matchIPDst(IpPrefix.valueOf("0.0.0.0/1"));
1677 extBuilder.matchEthType(Ethernet.TYPE_IPV4)
1678 .matchIPDst(IpPrefix.valueOf("128.0.0.0/1"));
1679 }
1680 } else {
1681 builderToUpdate.matchEthType(Ethernet.TYPE_IPV4).matchIPDst(ipv4Dst);
1682 }
1683 log.debug("processing IPv4 unicast specific forwarding objective {} -> next:{}"
1684 + " in dev:{}", fwd.id(), fwd.nextId(), deviceId);
1685 }
1686 return 0;
1687 }
1688
1689 /**
1690 * Helper method to build Ipv6 selector using the selector provided by
1691 * a forwarding objective.
1692 *
1693 * @param builderToUpdate the builder to update
1694 * @param fwd the selector to read
1695 * @return 0 if the update ends correctly. -1 if the matches
1696 * are not yet supported
1697 */
Charles Chan50d900c2018-03-02 13:26:22 -08001698 int buildIpv6Selector(TrafficSelector.Builder builderToUpdate,
Pier Luigi3bfe32c2017-01-30 09:47:36 -08001699 ForwardingObjective fwd) {
1700
1701 TrafficSelector selector = fwd.selector();
1702
1703 IpPrefix ipv6Dst = ((IPCriterion) selector.getCriterion(Criterion.Type.IPV6_DST)).ip();
1704 if (ipv6Dst.isMulticast()) {
Julia Ferguson65428c32017-08-10 18:15:24 +00001705 if (ipv6Dst.prefixLength() != IpAddress.INET6_BIT_LENGTH) {
1706 log.warn("Multicast specific forwarding objective can only be /128");
1707 fail(fwd, ObjectiveError.BADPARAMS);
1708 return -1;
1709 }
1710 VlanId assignedVlan = readVlanFromSelector(fwd.meta());
1711 if (assignedVlan == null) {
1712 log.warn("VLAN ID required by multicast specific fwd obj is missing. Abort.");
1713 fail(fwd, ObjectiveError.BADPARAMS);
1714 return -1;
1715 }
1716 if (requireVlanExtensions()) {
1717 OfdpaMatchVlanVid ofdpaMatchVlanVid = new OfdpaMatchVlanVid(assignedVlan);
1718 builderToUpdate.extension(ofdpaMatchVlanVid, deviceId);
1719 } else {
1720 builderToUpdate.matchVlanId(assignedVlan);
1721 }
1722 builderToUpdate.matchEthType(Ethernet.TYPE_IPV6).matchIPv6Dst(ipv6Dst);
1723 log.debug("processing IPv6 multicast specific forwarding objective {} -> next:{}"
1724 + " in dev:{}", fwd.id(), fwd.nextId(), deviceId);
1725 } else {
1726 if (ipv6Dst.prefixLength() != 0) {
1727 builderToUpdate.matchIPv6Dst(ipv6Dst);
1728 }
Pier Luigi3bfe32c2017-01-30 09:47:36 -08001729 builderToUpdate.matchEthType(Ethernet.TYPE_IPV6);
1730 log.debug("processing IPv6 unicast specific forwarding objective {} -> next:{}"
1731 + " in dev:{}", fwd.id(), fwd.nextId(), deviceId);
Julia Ferguson65428c32017-08-10 18:15:24 +00001732 }
Pier Luigi3bfe32c2017-01-30 09:47:36 -08001733 return 0;
1734 }
1735
Charles Chan50d900c2018-03-02 13:26:22 -08001736 FlowRule defaultRoute(ForwardingObjective fwd,
Pier Ventree0ae7a32016-11-23 09:57:42 -08001737 TrafficSelector.Builder complementarySelector,
1738 int forTableId,
1739 TrafficTreatment.Builder tb) {
1740 FlowRule.Builder rule = DefaultFlowRule.builder()
1741 .fromApp(fwd.appId())
1742 .withPriority(fwd.priority())
1743 .forDevice(deviceId)
1744 .withSelector(complementarySelector.build())
1745 .withTreatment(tb.build())
1746 .forTable(forTableId);
1747 if (fwd.permanent()) {
1748 rule.makePermanent();
1749 } else {
1750 rule.makeTemporary(fwd.timeout());
1751 }
1752 return rule.build();
1753 }
1754
Saurav Das4ce45962015-11-24 23:21:05 -08001755 /**
1756 * Handles forwarding rules to the L2 bridging table. Flow actions are not
1757 * allowed in the bridging table - instead we use L2 Interface group or
1758 * L2 flood group
1759 *
1760 * @param fwd the forwarding objective
1761 * @return A collection of flow rules, or an empty set
1762 */
1763 protected Collection<FlowRule> processEthDstSpecific(ForwardingObjective fwd) {
1764 List<FlowRule> rules = new ArrayList<>();
1765
1766 // Build filtered selector
1767 TrafficSelector selector = fwd.selector();
1768 EthCriterion ethCriterion = (EthCriterion) selector
1769 .getCriterion(Criterion.Type.ETH_DST);
1770 VlanIdCriterion vlanIdCriterion = (VlanIdCriterion) selector
1771 .getCriterion(Criterion.Type.VLAN_VID);
1772
1773 if (vlanIdCriterion == null) {
1774 log.warn("Forwarding objective for bridging requires vlan. Not "
1775 + "installing fwd:{} in dev:{}", fwd.id(), deviceId);
1776 fail(fwd, ObjectiveError.BADPARAMS);
1777 return Collections.emptySet();
1778 }
1779
1780 TrafficSelector.Builder filteredSelectorBuilder =
1781 DefaultTrafficSelector.builder();
Yi Tseng3a77b4f2017-02-06 15:02:17 -08001782
1783 if (!ethCriterion.mac().equals(NONE) &&
1784 !ethCriterion.mac().equals(BROADCAST)) {
Saurav Das4ce45962015-11-24 23:21:05 -08001785 filteredSelectorBuilder.matchEthDst(ethCriterion.mac());
1786 log.debug("processing L2 forwarding objective:{} -> next:{} in dev:{}",
1787 fwd.id(), fwd.nextId(), deviceId);
1788 } else {
Yi Tseng3a77b4f2017-02-06 15:02:17 -08001789 // Use wildcard DST_MAC if the MacAddress is None or Broadcast
Saurav Das4ce45962015-11-24 23:21:05 -08001790 log.debug("processing L2 Broadcast forwarding objective:{} -> next:{} "
1791 + "in dev:{} for vlan:{}",
1792 fwd.id(), fwd.nextId(), deviceId, vlanIdCriterion.vlanId());
1793 }
Charles Chand1172632017-03-15 17:33:09 -07001794 if (requireVlanExtensions()) {
1795 OfdpaMatchVlanVid ofdpaMatchVlanVid = new OfdpaMatchVlanVid(vlanIdCriterion.vlanId());
1796 filteredSelectorBuilder.extension(ofdpaMatchVlanVid, deviceId);
1797 } else {
1798 filteredSelectorBuilder.matchVlanId(vlanIdCriterion.vlanId());
1799 }
Saurav Das4ce45962015-11-24 23:21:05 -08001800 TrafficSelector filteredSelector = filteredSelectorBuilder.build();
1801
1802 if (fwd.treatment() != null) {
1803 log.warn("Ignoring traffic treatment in fwd rule {} meant for L2 table"
1804 + "for dev:{}. Expecting only nextId", fwd.id(), deviceId);
1805 }
1806
1807 TrafficTreatment.Builder treatmentBuilder = DefaultTrafficTreatment.builder();
1808 if (fwd.nextId() != null) {
Saurav Das423fe2b2015-12-04 10:52:59 -08001809 NextGroup next = getGroupForNextObjective(fwd.nextId());
Saurav Das4ce45962015-11-24 23:21:05 -08001810 if (next != null) {
1811 List<Deque<GroupKey>> gkeys = appKryo.deserialize(next.data());
1812 // we only need the top level group's key to point the flow to it
1813 Group group = groupService.getGroup(deviceId, gkeys.get(0).peekFirst());
1814 if (group != null) {
1815 treatmentBuilder.deferred().group(group.id());
1816 } else {
1817 log.warn("Group with key:{} for next-id:{} not found in dev:{}",
1818 gkeys.get(0).peekFirst(), fwd.nextId(), deviceId);
1819 fail(fwd, ObjectiveError.GROUPMISSING);
1820 return Collections.emptySet();
1821 }
1822 }
1823 }
1824 treatmentBuilder.immediate().transition(ACL_TABLE);
1825 TrafficTreatment filteredTreatment = treatmentBuilder.build();
1826
1827 // Build bridging table entries
1828 FlowRule.Builder flowRuleBuilder = DefaultFlowRule.builder();
1829 flowRuleBuilder.fromApp(fwd.appId())
1830 .withPriority(fwd.priority())
1831 .forDevice(deviceId)
1832 .withSelector(filteredSelector)
1833 .withTreatment(filteredTreatment)
1834 .forTable(BRIDGING_TABLE);
1835 if (fwd.permanent()) {
1836 flowRuleBuilder.makePermanent();
1837 } else {
1838 flowRuleBuilder.makeTemporary(fwd.timeout());
1839 }
1840 rules.add(flowRuleBuilder.build());
1841 return rules;
1842 }
1843
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001844 //////////////////////////////////////
1845 // Helper Methods and Classes
1846 //////////////////////////////////////
1847
1848 private boolean isSupportedEthTypeObjective(ForwardingObjective fwd) {
1849 TrafficSelector selector = fwd.selector();
1850 EthTypeCriterion ethType = (EthTypeCriterion) selector
1851 .getCriterion(Criterion.Type.ETH_TYPE);
1852 return !((ethType == null) ||
1853 ((ethType.ethType().toShort() != Ethernet.TYPE_IPV4) &&
Pier Ventree0ae7a32016-11-23 09:57:42 -08001854 (ethType.ethType().toShort() != Ethernet.MPLS_UNICAST)) &&
1855 (ethType.ethType().toShort() != Ethernet.TYPE_IPV6));
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001856 }
1857
1858 private boolean isSupportedEthDstObjective(ForwardingObjective fwd) {
1859 TrafficSelector selector = fwd.selector();
1860 EthCriterion ethDst = (EthCriterion) selector
1861 .getCriterion(Criterion.Type.ETH_DST);
1862 VlanIdCriterion vlanId = (VlanIdCriterion) selector
1863 .getCriterion(Criterion.Type.VLAN_VID);
1864 return !(ethDst == null && vlanId == null);
1865 }
1866
Charles Chan50d900c2018-03-02 13:26:22 -08001867 NextGroup getGroupForNextObjective(Integer nextId) {
Saurav Das423fe2b2015-12-04 10:52:59 -08001868 NextGroup next = flowObjectiveStore.getNextGroup(nextId);
1869 if (next != null) {
1870 List<Deque<GroupKey>> gkeys = appKryo.deserialize(next.data());
1871 if (gkeys != null && !gkeys.isEmpty()) {
1872 return next;
1873 } else {
1874 log.warn("Empty next group found in FlowObjective store for "
1875 + "next-id:{} in dev:{}", nextId, deviceId);
1876 }
1877 } else {
1878 log.warn("next-id {} not found in Flow objective store for dev:{}",
1879 nextId, deviceId);
1880 }
1881 return null;
1882 }
1883
Saurav Das24431192016-03-07 19:13:00 -08001884 @Override
1885 public List<String> getNextMappings(NextGroup nextGroup) {
1886 List<String> mappings = new ArrayList<>();
1887 List<Deque<GroupKey>> gkeys = appKryo.deserialize(nextGroup.data());
1888 for (Deque<GroupKey> gkd : gkeys) {
1889 Group lastGroup = null;
Yuta HIGUCHI2dce08a2017-04-20 21:57:48 -07001890 StringBuilder gchain = new StringBuilder();
Saurav Das24431192016-03-07 19:13:00 -08001891 for (GroupKey gk : gkd) {
1892 Group g = groupService.getGroup(deviceId, gk);
Saurav Das8be4e3a2016-03-11 17:19:07 -08001893 if (g == null) {
Saurav Das25190812016-05-27 13:54:07 -07001894 gchain.append(" NoGrp").append(" -->");
Saurav Das8be4e3a2016-03-11 17:19:07 -08001895 continue;
1896 }
1897 gchain.append(" 0x").append(Integer.toHexString(g.id().id()))
1898 .append(" -->");
Saurav Das24431192016-03-07 19:13:00 -08001899 lastGroup = g;
1900 }
1901 // add port information for last group in group-chain
Yuta HIGUCHI2dce08a2017-04-20 21:57:48 -07001902 List<Instruction> lastGroupIns = new ArrayList<>();
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001903 if (lastGroup != null && !lastGroup.buckets().buckets().isEmpty()) {
Saurav Das25190812016-05-27 13:54:07 -07001904 lastGroupIns = lastGroup.buckets().buckets().get(0)
1905 .treatment().allInstructions();
1906 }
1907 for (Instruction i: lastGroupIns) {
Saurav Das24431192016-03-07 19:13:00 -08001908 if (i instanceof OutputInstruction) {
Saurav Das8be4e3a2016-03-11 17:19:07 -08001909 gchain.append(" port:").append(((OutputInstruction) i).port());
Saurav Das24431192016-03-07 19:13:00 -08001910 }
1911 }
Saurav Das8be4e3a2016-03-11 17:19:07 -08001912 mappings.add(gchain.toString());
Saurav Das24431192016-03-07 19:13:00 -08001913 }
1914 return mappings;
1915 }
Charles Chan5b9df8d2016-03-28 22:21:40 -07001916
Saurav Dasa4020382018-02-14 14:14:54 -08001917 /**
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001918 * Utility class that retries sending flows a fixed number of times, even if
1919 * some of the attempts are successful. Used only for forwarding objectives.
1920 */
Alex Yashchuk4caa8e82017-12-08 17:40:05 +02001921 public final class RetryFlows implements Runnable {
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001922 int attempts = MAX_RETRY_ATTEMPTS;
1923 private Collection<FlowRule> retryFlows;
1924 private ForwardingObjective fwd;
1925
Charles Chan50d900c2018-03-02 13:26:22 -08001926 RetryFlows(ForwardingObjective fwd, Collection<FlowRule> retryFlows) {
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001927 this.fwd = fwd;
1928 this.retryFlows = retryFlows;
1929 }
1930
1931 @Override
1932 public void run() {
1933 log.info("RETRY FLOWS ATTEMPT# {} for fwd:{} rules:{}",
1934 MAX_RETRY_ATTEMPTS - attempts, fwd.id(), retryFlows.size());
pier8b3aef42019-03-11 15:14:02 -07001935 sendForwards(Collections.singletonList(Pair.of(fwd, retryFlows)));
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001936 if (--attempts > 0) {
pier8b3aef42019-03-11 15:14:02 -07001937 retryExecutorService.schedule(this, RETRY_MS, TimeUnit.MILLISECONDS);
1938 }
1939 }
1940 }
1941
1942 // Flow rules accumulator for reducing the number of transactions required to the devices.
1943 private final class ForwardingObjectiveAccumulator
1944 extends AbstractAccumulator<Pair<ForwardingObjective, Collection<FlowRule>>> {
1945
1946 ForwardingObjectiveAccumulator(int maxFwd, int maxBatchMS, int maxIdleMS) {
1947 super(TIMER, maxFwd, maxBatchMS, maxIdleMS);
1948 }
1949
1950 @Override
1951 public void processItems(List<Pair<ForwardingObjective, Collection<FlowRule>>> pairs) {
1952 // Triggers creation of a batch using the list of flowrules generated from fwdobjs.
1953 accumulatorExecutorService.execute(new FlowRulesBuilderTask(pairs));
1954 }
1955 }
1956
1957 // Task for building batch of flow rules in a separate thread.
1958 private final class FlowRulesBuilderTask implements Runnable {
1959 private final List<Pair<ForwardingObjective, Collection<FlowRule>>> pairs;
1960
1961 FlowRulesBuilderTask(List<Pair<ForwardingObjective, Collection<FlowRule>>> pairs) {
1962 this.pairs = pairs;
1963 }
1964
1965 @Override
1966 public void run() {
1967 try {
1968 sendForwards(pairs);
1969 } catch (Exception e) {
1970 log.warn("Unable to send forwards", e);
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001971 }
1972 }
1973 }
1974
Saurav Das822c4e22015-10-23 10:51:11 -07001975}