blob: 70aafd47651b5eabac2c42d89d1ba587ae9304d7 [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 Chaundkarcb787512019-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;
pier5be39042019-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) {
pier2d8ecd12020-05-20 22:50:30 +0200156 this.deviceId = deviceId;
pierf37ce522020-03-20 11:00:38 +0100157
pier2d8ecd12020-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
pier2d8ecd12020-05-20 22:50:30 +0200171 initDriverId();
172 initGroupHander(context);
Saurav Das822c4e22015-10-23 10:51:11 -0700173
pier2d8ecd12020-05-20 22:50:30 +0200174 initializePipeline();
pierf37ce522020-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) {
pierf37ce522020-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 Chaundkarcb787512019-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 /**
254 * Determines whether in-port should be matched on in TMAC table rules.
255 *
256 * @return true if match on in-port should be programmed
257 */
258 protected boolean matchInPortTmacTable() {
259 return true;
260 }
261
Charles Chand9e47c62017-10-05 15:17:15 -0700262 /**
263 * Determines whether matching L4 destination port on IPv6 packets is supported in ACL table.
264 *
265 * @return true if matching L4 destination port on IPv6 packets is supported in ACL table.
266 */
267 protected boolean supportIpv6L4Dst() {
268 return true;
269 }
270
Saurav Dasc568c342018-01-25 09:49:01 -0800271 /**
272 * Determines whether this driver should continue to retry flows that point
273 * to empty groups. See CORD-554.
274 *
275 * @return true if the driver should retry flows
276 */
277 protected boolean shouldRetry() {
278 return true;
279 }
280
Charles Chan45b69ab2018-03-02 15:41:41 -0800281 /**
282 * Determines whether this driver requires unicast flow to be installed before multicast flow
283 * in TMAC table.
284 *
285 * @return true if required
286 */
287 protected boolean requireUnicastBeforeMulticast() {
288 return false;
289 }
290
Andrea Campanellad980c6d2018-04-30 11:48:55 +0200291 /**
292 * Determines whether this driver supports installing a clearDeferred action on table 30.
293 *
294 * @return true if required
295 */
296 protected boolean supportsUnicastBlackHole() {
297 return true;
298 }
299
Charles Chanab591602019-01-22 17:25:04 -0800300 protected boolean requirePuntTable() {
301 return false;
302 }
303
Saurav Das822c4e22015-10-23 10:51:11 -0700304 //////////////////////////////////////
305 // Flow Objectives
306 //////////////////////////////////////
307
308 @Override
309 public void filter(FilteringObjective filteringObjective) {
310 if (filteringObjective.type() == FilteringObjective.Type.PERMIT) {
311 processFilter(filteringObjective,
312 filteringObjective.op() == Objective.Operation.ADD,
313 filteringObjective.appId());
314 } else {
315 // Note that packets that don't match the PERMIT filter are
316 // automatically denied. The DENY filter is used to deny packets
317 // that are otherwise permitted by the PERMIT filter.
318 // Use ACL table flow rules here for DENY filtering objectives
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530319 log.warn("filter objective other than PERMIT currently not supported");
Saurav Das822c4e22015-10-23 10:51:11 -0700320 fail(filteringObjective, ObjectiveError.UNSUPPORTED);
321 }
322 }
323
324 @Override
325 public void forward(ForwardingObjective fwd) {
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700326 Collection<FlowRule> rules = processForward(fwd);
Saurav Das25190812016-05-27 13:54:07 -0700327 if (rules == null || rules.isEmpty()) {
328 // Assumes fail message has already been generated to the objective
329 // context. Returning here prevents spurious pass message to be
330 // generated by FlowRule service for empty flowOps.
331 return;
332 }
pier7afc7522019-05-10 13:19:15 +0200333 // Let's accumulate flow rules if accumulator is active and fwd objective is not versatile.
334 // Otherwise send directly, without adding futher delay
335 if (accumulator != null && Objects.equals(fwd.flag(), SPECIFIC)) {
pier8b3aef42019-03-11 15:14:02 -0700336 accumulator.add(Pair.of(fwd, rules));
337 } else {
338 sendForwards(Collections.singletonList(Pair.of(fwd, rules)));
339 }
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700340 }
341
pier8b3aef42019-03-11 15:14:02 -0700342 // Builds the batch using the accumulated flow rules
343 private void sendForwards(List<Pair<ForwardingObjective, Collection<FlowRule>>> pairs) {
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700344 FlowRuleOperations.Builder flowOpsBuilder = FlowRuleOperations.builder();
pier8b3aef42019-03-11 15:14:02 -0700345 log.debug("Sending {} fwd-objs", pairs.size());
346 List<Objective> fwdObjs = Lists.newArrayList();
347 // Iterates over all accumulated flow rules and then build an unique batch
348 pairs.forEach(pair -> {
349 ForwardingObjective fwd = pair.getLeft();
350 Collection<FlowRule> rules = pair.getRight();
351 switch (fwd.op()) {
352 case ADD:
353 rules.stream()
354 .filter(Objects::nonNull)
355 .forEach(flowOpsBuilder::add);
356 log.debug("Applying a add fwd-obj {} to sw:{}", fwd.id(), deviceId);
357 fwdObjs.add(fwd);
358 break;
359 case REMOVE:
360 rules.stream()
361 .filter(Objects::nonNull)
362 .forEach(flowOpsBuilder::remove);
363 log.debug("Deleting a flow rule to sw:{}", deviceId);
364 fwdObjs.add(fwd);
365 break;
366 default:
367 fail(fwd, ObjectiveError.UNKNOWN);
368 log.warn("Unknown forwarding type {}", fwd.op());
369 }
370 });
371 // Finally applies the operations
Saurav Das822c4e22015-10-23 10:51:11 -0700372 flowRuleService.apply(flowOpsBuilder.build(new FlowRuleOperationsContext() {
pier8b3aef42019-03-11 15:14:02 -0700373
Saurav Das822c4e22015-10-23 10:51:11 -0700374 @Override
375 public void onSuccess(FlowRuleOperations ops) {
pier8b3aef42019-03-11 15:14:02 -0700376 log.trace("Flow rule operations onSuccess {}", ops);
pier9469f3e2019-04-17 17:05:08 +0200377 fwdObjs.forEach(OfdpaPipelineUtility::pass);
Saurav Das822c4e22015-10-23 10:51:11 -0700378 }
379
380 @Override
381 public void onError(FlowRuleOperations ops) {
pier8b3aef42019-03-11 15:14:02 -0700382 ObjectiveError error = ObjectiveError.FLOWINSTALLATIONFAILED;
383 log.warn("Flow rule operations onError {}. Reason = {}", ops, error);
384 fwdObjs.forEach(fwdObj -> fail(fwdObj, error));
Saurav Das822c4e22015-10-23 10:51:11 -0700385 }
386 }));
Saurav Das822c4e22015-10-23 10:51:11 -0700387 }
388
389 @Override
390 public void next(NextObjective nextObjective) {
Saurav Das8a0732e2015-11-20 15:27:53 -0800391 NextGroup nextGroup = flowObjectiveStore.getNextGroup(nextObjective.id());
392 switch (nextObjective.op()) {
393 case ADD:
Saurav Das4f980082015-11-05 13:39:15 -0800394 if (nextGroup != null) {
Saurav Das8a0732e2015-11-20 15:27:53 -0800395 log.warn("Cannot add next {} that already exists in device {}",
396 nextObjective.id(), deviceId);
397 return;
398 }
Saurav Das1547b3f2017-05-05 17:01:08 -0700399 log.debug("Processing NextObjective id {} in dev {} - add group",
Saurav Das8a0732e2015-11-20 15:27:53 -0800400 nextObjective.id(), deviceId);
Charles Chan425854b2016-04-11 15:32:12 -0700401 groupHandler.addGroup(nextObjective);
Saurav Das8a0732e2015-11-20 15:27:53 -0800402 break;
403 case ADD_TO_EXISTING:
404 if (nextGroup != null) {
Saurav Das1547b3f2017-05-05 17:01:08 -0700405 log.debug("Processing NextObjective id {} in dev {} - add bucket",
Saurav Das8a0732e2015-11-20 15:27:53 -0800406 nextObjective.id(), deviceId);
Charles Chan425854b2016-04-11 15:32:12 -0700407 groupHandler.addBucketToGroup(nextObjective, nextGroup);
Saurav Das4f980082015-11-05 13:39:15 -0800408 } else {
Saurav Das8a0732e2015-11-20 15:27:53 -0800409 // it is possible that group-chain has not been fully created yet
Saurav Das423fe2b2015-12-04 10:52:59 -0800410 log.debug("Waiting to add bucket to group for next-id:{} in dev:{}",
411 nextObjective.id(), deviceId);
Yi Tseng47f82dc2017-03-05 22:48:39 -0800412
413 // by design multiple pending bucket is allowed for the group
414 groupHandler.pendingBuckets.compute(nextObjective.id(), (nextId, pendBkts) -> {
415 if (pendBkts == null) {
416 pendBkts = Sets.newHashSet();
417 }
418 pendBkts.add(nextObjective);
419 return pendBkts;
420 });
Saurav Das4f980082015-11-05 13:39:15 -0800421 }
Saurav Das8a0732e2015-11-20 15:27:53 -0800422 break;
423 case REMOVE:
424 if (nextGroup == null) {
425 log.warn("Cannot remove next {} that does not exist in device {}",
426 nextObjective.id(), deviceId);
427 return;
428 }
Saurav Das1547b3f2017-05-05 17:01:08 -0700429 log.debug("Processing NextObjective id {} in dev {} - remove group",
Saurav Das8a0732e2015-11-20 15:27:53 -0800430 nextObjective.id(), deviceId);
Charles Chan425854b2016-04-11 15:32:12 -0700431 groupHandler.removeGroup(nextObjective, nextGroup);
Saurav Das8a0732e2015-11-20 15:27:53 -0800432 break;
433 case REMOVE_FROM_EXISTING:
434 if (nextGroup == null) {
435 log.warn("Cannot remove from next {} that does not exist in device {}",
436 nextObjective.id(), deviceId);
437 return;
438 }
Saurav Das1547b3f2017-05-05 17:01:08 -0700439 log.debug("Processing NextObjective id {} in dev {} - remove bucket",
Saurav Das8a0732e2015-11-20 15:27:53 -0800440 nextObjective.id(), deviceId);
Charles Chan425854b2016-04-11 15:32:12 -0700441 groupHandler.removeBucketFromGroup(nextObjective, nextGroup);
Saurav Das8a0732e2015-11-20 15:27:53 -0800442 break;
Jonghwan Hyunf810a7a2018-02-12 16:43:45 +0900443 case MODIFY:
444 if (nextGroup == null) {
445 log.warn("Cannot modify next {} that does not exist in device {}",
446 nextObjective.id(), deviceId);
447 return;
448 }
Ruchi Sahotaef0761c2019-01-28 01:08:18 +0000449 log.debug("Processing NextObjective id {} in dev {} group {} - modify bucket",
450 nextObjective.id(), deviceId, nextGroup);
Jonghwan Hyunf810a7a2018-02-12 16:43:45 +0900451 groupHandler.modifyBucketFromGroup(nextObjective, nextGroup);
452 break;
Saurav Dasceccf242017-08-03 18:30:35 -0700453 case VERIFY:
454 if (nextGroup == null) {
455 log.warn("Cannot verify next {} that does not exist in device {}",
456 nextObjective.id(), deviceId);
457 return;
458 }
459 log.debug("Processing NextObjective id {} in dev {} - verify",
460 nextObjective.id(), deviceId);
461 groupHandler.verifyGroup(nextObjective, nextGroup);
462 break;
Saurav Das8a0732e2015-11-20 15:27:53 -0800463 default:
Saurav Das4f980082015-11-05 13:39:15 -0800464 log.warn("Unsupported operation {}", nextObjective.op());
Saurav Das822c4e22015-10-23 10:51:11 -0700465 }
466 }
467
468 //////////////////////////////////////
469 // Flow handling
470 //////////////////////////////////////
471
472 /**
Yi Tseng0fdc07e2017-04-24 19:17:08 -0700473 * As per OFDPA 2.0 TTP, filtering of VLAN ids and MAC addresses (for routing)
474 * configured on switch ports happen in different tables.
Saurav Das822c4e22015-10-23 10:51:11 -0700475 *
476 * @param filt the filtering objective
477 * @param install indicates whether to add or remove the objective
478 * @param applicationId the application that sent this objective
479 */
Saurav Das52025962016-01-28 22:30:01 -0800480 protected void processFilter(FilteringObjective filt,
481 boolean install, ApplicationId applicationId) {
Saurav Das822c4e22015-10-23 10:51:11 -0700482 // This driver only processes filtering criteria defined with switch
483 // ports as the key
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530484 PortCriterion portCriterion = null;
Saurav Das822c4e22015-10-23 10:51:11 -0700485 EthCriterion ethCriterion = null;
486 VlanIdCriterion vidCriterion = null;
Saurav Das822c4e22015-10-23 10:51:11 -0700487 if (!filt.key().equals(Criteria.dummy()) &&
488 filt.key().type() == Criterion.Type.IN_PORT) {
489 portCriterion = (PortCriterion) filt.key();
Saurav Das822c4e22015-10-23 10:51:11 -0700490 }
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530491 if (portCriterion == null) {
492 log.debug("No IN_PORT defined in filtering objective from app: {}",
493 applicationId);
494 } else {
495 log.debug("Received filtering objective for dev/port: {}/{}", deviceId,
496 portCriterion.port());
497 }
Saurav Das822c4e22015-10-23 10:51:11 -0700498 // convert filtering conditions for switch-intfs into flowrules
499 FlowRuleOperations.Builder ops = FlowRuleOperations.builder();
500 for (Criterion criterion : filt.conditions()) {
Yi Tseng0fdc07e2017-04-24 19:17:08 -0700501 switch (criterion.type()) {
502 case ETH_DST:
503 case ETH_DST_MASKED:
504 ethCriterion = (EthCriterion) criterion;
505 break;
506 case VLAN_VID:
507 vidCriterion = (VlanIdCriterion) criterion;
508 break;
509 default:
510 log.warn("Unsupported filter {}", criterion);
511 fail(filt, ObjectiveError.UNSUPPORTED);
512 return;
Saurav Das822c4e22015-10-23 10:51:11 -0700513 }
514 }
515
Saurav Das0e99e2b2015-10-28 12:39:42 -0700516 VlanId assignedVlan = null;
Charles Chane849c192016-01-11 18:28:54 -0800517 if (vidCriterion != null) {
Charles Chand55e84d2016-03-30 17:54:24 -0700518 // Use the VLAN in criterion if metadata is not present and the traffic is tagged
Charles Chanc550f2e2017-12-19 19:55:57 -0800519 if (!vidCriterion.vlanId().equals(VlanId.NONE)) {
Charles Chane849c192016-01-11 18:28:54 -0800520 assignedVlan = vidCriterion.vlanId();
Piere5bff482018-03-07 11:42:50 +0100521 }
522 // If the meta VLAN is present let's update the assigned vlan
523 if (filt.meta() != null) {
524 VlanId metaVlan = readVlanFromTreatment(filt.meta());
525 if (metaVlan != null) {
526 assignedVlan = metaVlan;
527 }
Charles Chand55e84d2016-03-30 17:54:24 -0700528 }
Charles Chane849c192016-01-11 18:28:54 -0800529
Charles Chand55e84d2016-03-30 17:54:24 -0700530 if (assignedVlan == null) {
531 log.error("Driver fails to extract VLAN information. "
Frank Wangd8ab0962017-08-11 11:09:30 +0800532 + "Not processing VLAN filters on device {}.", deviceId);
Charles Chand55e84d2016-03-30 17:54:24 -0700533 log.debug("VLAN ID in criterion={}, metadata={}",
534 readVlanFromTreatment(filt.meta()), vidCriterion.vlanId());
535 fail(filt, ObjectiveError.BADPARAMS);
536 return;
Saurav Das0e99e2b2015-10-28 12:39:42 -0700537 }
538 }
539
Yi Tseng3a77b4f2017-02-06 15:02:17 -0800540 if (ethCriterion == null || ethCriterion.mac().equals(NONE)) {
Charles Chan985b12e2016-05-11 19:47:22 -0700541 // NOTE: it is possible that a filtering objective only has vidCriterion
Daniel Ginsburgc1d47e92018-04-30 19:27:19 -0400542 log.debug("filtering objective missing dstMac, won't program TMAC table");
Saurav Das822c4e22015-10-23 10:51:11 -0700543 } else {
Charles Chan45b69ab2018-03-02 15:41:41 -0800544 MacAddress unicastMac = readEthDstFromTreatment(filt.meta());
Charles Chan66291502018-03-02 16:43:28 -0800545 List<List<FlowRule>> allStages = processEthDstFilter(portCriterion, ethCriterion,
Charles Chan45b69ab2018-03-02 15:41:41 -0800546 vidCriterion, assignedVlan, unicastMac, applicationId);
Charles Chan66291502018-03-02 16:43:28 -0800547 for (List<FlowRule> flowRules : allStages) {
Charles Chan45b69ab2018-03-02 15:41:41 -0800548 log.trace("Starting a new flow rule stage for TMAC table flow");
Charles Chan66291502018-03-02 16:43:28 -0800549 ops.newStage();
Charles Chanc550f2e2017-12-19 19:55:57 -0800550
Charles Chan66291502018-03-02 16:43:28 -0800551 for (FlowRule flowRule : flowRules) {
pier5be39042019-10-04 10:54:07 +0200552 log.trace("{} flow rule in TMAC table: {} for dev: {}",
553 (install) ? "adding" : "removing", flowRule, deviceId);
Charles Chan66291502018-03-02 16:43:28 -0800554 if (install) {
555 ops = ops.add(flowRule);
Charles Chanc550f2e2017-12-19 19:55:57 -0800556 } else {
Charles Chan66291502018-03-02 16:43:28 -0800557 // NOTE: Only remove TMAC flow when there is no more enabled port within the
558 // same VLAN on this device if TMAC doesn't support matching on in_port.
pier5be39042019-10-04 10:54:07 +0200559 if (filt.meta() != null && filt.meta().clearedDeferred()) {
560 // TMac mcast does not match on the input port - we have to remove it
561 // only if this is the last port
562 FlowRule rule = buildTmacRuleForMcastFromUnicast(flowRule, applicationId);
563 // IPv6 or IPv4 tmac rule
564 if (rule != null) {
565 // Add first the mcast rule and then open a new stage for the unicast
566 ops = ops.remove(rule);
567 ops.newStage();
568 }
569 ops = ops.remove(flowRule);
570 } else if (matchInPortTmacTable()) {
Charles Chan66291502018-03-02 16:43:28 -0800571 ops = ops.remove(flowRule);
572 } else {
pier5be39042019-10-04 10:54:07 +0200573 log.debug("Abort TMAC flow removal on {}. " +
574 "Some other ports still share this TMAC flow", deviceId);
Charles Chan66291502018-03-02 16:43:28 -0800575 }
Charles Chanc550f2e2017-12-19 19:55:57 -0800576 }
577 }
Saurav Das822c4e22015-10-23 10:51:11 -0700578 }
579 }
580
Charles Chan985b12e2016-05-11 19:47:22 -0700581 if (vidCriterion == null) {
582 // NOTE: it is possible that a filtering objective only has ethCriterion
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530583 log.info("filtering objective missing VLAN, cannot program VLAN Table");
Saurav Das822c4e22015-10-23 10:51:11 -0700584 } else {
Charles Chan66291502018-03-02 16:43:28 -0800585 List<List<FlowRule>> allStages = processVlanIdFilter(
pier6aef5b72019-06-10 17:10:26 +0200586 portCriterion, vidCriterion, assignedVlan, applicationId, install);
Charles Chan66291502018-03-02 16:43:28 -0800587 for (List<FlowRule> flowRules : allStages) {
Charles Chan45b69ab2018-03-02 15:41:41 -0800588 log.trace("Starting a new flow rule stage for VLAN table flow");
Charles Chan66291502018-03-02 16:43:28 -0800589 ops.newStage();
Charles Chan14967c22015-12-07 11:11:50 -0800590
Charles Chan66291502018-03-02 16:43:28 -0800591 for (FlowRule flowRule : flowRules) {
592 log.trace("{} flow rules in VLAN table: {} for dev: {}",
593 (install) ? "adding" : "removing", flowRule, deviceId);
594 ops = install ? ops.add(flowRule) : ops.remove(flowRule);
Charles Chand1172632017-03-15 17:33:09 -0700595 }
Saurav Das822c4e22015-10-23 10:51:11 -0700596 }
597 }
598
Saurav Das822c4e22015-10-23 10:51:11 -0700599 // apply filtering flow rules
600 flowRuleService.apply(ops.build(new FlowRuleOperationsContext() {
601 @Override
602 public void onSuccess(FlowRuleOperations ops) {
Saurav Das018605f2017-02-18 14:05:44 -0800603 log.debug("Applied {} filtering rules in device {}",
Saurav Das822c4e22015-10-23 10:51:11 -0700604 ops.stages().get(0).size(), deviceId);
605 pass(filt);
606 }
607
608 @Override
609 public void onError(FlowRuleOperations ops) {
610 log.info("Failed to apply all filtering rules in dev {}", deviceId);
611 fail(filt, ObjectiveError.FLOWINSTALLATIONFAILED);
612 }
613 }));
614
615 }
616
617 /**
Charles Chand1172632017-03-15 17:33:09 -0700618 * Internal implementation of processVlanIdFilter.
619 * <p>
620 * The is_present bit in set_vlan_vid action is required to be 0 in OFDPA i12.
621 * Since it is non-OF spec, we need an extension treatment for that.
622 * The useVlanExtension must be set to false for OFDPA i12.
623 * </p>
Charles Chan66291502018-03-02 16:43:28 -0800624 * <p>
625 * NOTE: Separate VLAN filtering rules and assignment rules
626 * into different stages in order to guarantee that filtering rules
627 * always go first, as required by OFDPA.
628 * </p>
Saurav Das0e99e2b2015-10-28 12:39:42 -0700629 *
Charles Chanf9e98652016-09-07 16:54:23 -0700630 * @param portCriterion port on device for which this filter is programmed
631 * @param vidCriterion vlan assigned to port, or NONE for untagged
632 * @param assignedVlan assigned vlan-id for untagged packets
633 * @param applicationId for application programming this filter
pier6aef5b72019-06-10 17:10:26 +0200634 * @param install indicates whether to add or remove the objective
Charles Chan66291502018-03-02 16:43:28 -0800635 * @return stages of flow rules for port-vlan filters
Saurav Das822c4e22015-10-23 10:51:11 -0700636 */
Charles Chan66291502018-03-02 16:43:28 -0800637 protected List<List<FlowRule>> processVlanIdFilter(PortCriterion portCriterion,
pier6aef5b72019-06-10 17:10:26 +0200638 VlanIdCriterion vidCriterion,
639 VlanId assignedVlan,
640 ApplicationId applicationId,
641 boolean install) {
Charles Chan66291502018-03-02 16:43:28 -0800642 List<FlowRule> filteringRules = new ArrayList<>();
643 List<FlowRule> assignmentRules = new ArrayList<>();
Saurav Das822c4e22015-10-23 10:51:11 -0700644 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
645 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
Charles Chan14967c22015-12-07 11:11:50 -0800646 TrafficSelector.Builder preSelector = null;
647 TrafficTreatment.Builder preTreatment = null;
648
Saurav Das4f980082015-11-05 13:39:15 -0800649 treatment.transition(TMAC_TABLE);
650
Saurav Das822c4e22015-10-23 10:51:11 -0700651 if (vidCriterion.vlanId() == VlanId.NONE) {
652 // untagged packets are assigned vlans
Charles Chand1172632017-03-15 17:33:09 -0700653 preSelector = DefaultTrafficSelector.builder();
654 if (requireVlanExtensions()) {
655 OfdpaMatchVlanVid ofdpaMatchVlanVid = new OfdpaMatchVlanVid(VlanId.NONE);
656 selector.extension(ofdpaMatchVlanVid, deviceId);
Charles Chanf9e98652016-09-07 16:54:23 -0700657 OfdpaSetVlanVid ofdpaSetVlanVid = new OfdpaSetVlanVid(assignedVlan);
658 treatment.extension(ofdpaSetVlanVid, deviceId);
Charles Chand1172632017-03-15 17:33:09 -0700659
660 OfdpaMatchVlanVid preOfdpaMatchVlanVid = new OfdpaMatchVlanVid(assignedVlan);
661 preSelector.extension(preOfdpaMatchVlanVid, deviceId);
Charles Chanf9e98652016-09-07 16:54:23 -0700662 } else {
Charles Chand1172632017-03-15 17:33:09 -0700663 selector.matchVlanId(VlanId.NONE);
Charles Chanf9e98652016-09-07 16:54:23 -0700664 treatment.setVlanId(assignedVlan);
Charles Chand1172632017-03-15 17:33:09 -0700665
666 preSelector.matchVlanId(assignedVlan);
667 }
668 preTreatment = DefaultTrafficTreatment.builder().transition(TMAC_TABLE);
669 } else {
670 if (requireVlanExtensions()) {
671 OfdpaMatchVlanVid ofdpaMatchVlanVid = new OfdpaMatchVlanVid(vidCriterion.vlanId());
672 selector.extension(ofdpaMatchVlanVid, deviceId);
673 } else {
674 selector.matchVlanId(vidCriterion.vlanId());
Charles Chanf9e98652016-09-07 16:54:23 -0700675 }
Charles Chan14967c22015-12-07 11:11:50 -0800676
Charles Chand55e84d2016-03-30 17:54:24 -0700677 if (!assignedVlan.equals(vidCriterion.vlanId())) {
Charles Chand1172632017-03-15 17:33:09 -0700678 if (requireVlanExtensions()) {
Charles Chanc03782d2017-01-31 13:57:55 -0800679 OfdpaSetVlanVid ofdpaSetVlanVid = new OfdpaSetVlanVid(assignedVlan);
680 treatment.extension(ofdpaSetVlanVid, deviceId);
681 } else {
682 treatment.setVlanId(assignedVlan);
683 }
Charles Chand55e84d2016-03-30 17:54:24 -0700684 }
Saurav Das822c4e22015-10-23 10:51:11 -0700685 }
Saurav Das822c4e22015-10-23 10:51:11 -0700686
687 // ofdpa cannot match on ALL portnumber, so we need to use separate
688 // rules for each port.
Charles Chan14967c22015-12-07 11:11:50 -0800689 List<PortNumber> portnums = new ArrayList<>();
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530690 if (portCriterion != null) {
691 if (PortNumber.ALL.equals(portCriterion.port())) {
692 for (Port port : deviceService.getPorts(deviceId)) {
693 if (port.number().toLong() > 0 && port.number().toLong() < OFPP_MAX) {
694 portnums.add(port.number());
695 }
Saurav Das822c4e22015-10-23 10:51:11 -0700696 }
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530697 } else {
698 portnums.add(portCriterion.port());
Saurav Das822c4e22015-10-23 10:51:11 -0700699 }
700 } else {
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530701 log.warn("Filtering Objective missing Port Criterion . " +
Charles Chan66291502018-03-02 16:43:28 -0800702 "VLAN Table cannot be programmed for {}", deviceId);
Saurav Das822c4e22015-10-23 10:51:11 -0700703 }
Saurav Das4f980082015-11-05 13:39:15 -0800704
Saurav Das822c4e22015-10-23 10:51:11 -0700705 for (PortNumber pnum : portnums) {
Saurav Das4f980082015-11-05 13:39:15 -0800706 // create rest of flowrule
Saurav Das822c4e22015-10-23 10:51:11 -0700707 selector.matchInPort(pnum);
708 FlowRule rule = DefaultFlowRule.builder()
709 .forDevice(deviceId)
710 .withSelector(selector.build())
711 .withTreatment(treatment.build())
712 .withPriority(DEFAULT_PRIORITY)
713 .fromApp(applicationId)
714 .makePermanent()
715 .forTable(VLAN_TABLE).build();
Charles Chan66291502018-03-02 16:43:28 -0800716 assignmentRules.add(rule);
Charles Chan14967c22015-12-07 11:11:50 -0800717
718 if (preSelector != null) {
719 preSelector.matchInPort(pnum);
720 FlowRule preRule = DefaultFlowRule.builder()
721 .forDevice(deviceId)
722 .withSelector(preSelector.build())
723 .withTreatment(preTreatment.build())
724 .withPriority(DEFAULT_PRIORITY)
725 .fromApp(applicationId)
726 .makePermanent()
727 .forTable(VLAN_TABLE).build();
Charles Chan66291502018-03-02 16:43:28 -0800728 filteringRules.add(preRule);
Charles Chan14967c22015-12-07 11:11:50 -0800729 }
Saurav Das822c4e22015-10-23 10:51:11 -0700730 }
pier6aef5b72019-06-10 17:10:26 +0200731 return install ? ImmutableList.of(filteringRules, assignmentRules) :
732 ImmutableList.of(assignmentRules, filteringRules);
Saurav Das822c4e22015-10-23 10:51:11 -0700733 }
734
735 /**
736 * Allows routed packets with correct destination MAC to be directed
Charles Chan45b69ab2018-03-02 15:41:41 -0800737 * to unicast routing table, multicast routing table or MPLS forwarding table.
Saurav Das822c4e22015-10-23 10:51:11 -0700738 *
739 * @param portCriterion port on device for which this filter is programmed
740 * @param ethCriterion dstMac of device for which is filter is programmed
741 * @param vidCriterion vlan assigned to port, or NONE for untagged
Saurav Das0e99e2b2015-10-28 12:39:42 -0700742 * @param assignedVlan assigned vlan-id for untagged packets
Charles Chan45b69ab2018-03-02 15:41:41 -0800743 * @param unicastMac some switches require a unicast TMAC flow to be programmed before multicast
744 * TMAC flow. This MAC address will be used for the unicast TMAC flow.
745 * This is unused if the filtering objective is a unicast.
Saurav Das822c4e22015-10-23 10:51:11 -0700746 * @param applicationId for application programming this filter
Charles Chan66291502018-03-02 16:43:28 -0800747 * @return stages of flow rules for port-vlan filters
Saurav Das822c4e22015-10-23 10:51:11 -0700748
749 */
Charles Chan66291502018-03-02 16:43:28 -0800750 protected List<List<FlowRule>> processEthDstFilter(PortCriterion portCriterion,
Saurav Das822c4e22015-10-23 10:51:11 -0700751 EthCriterion ethCriterion,
752 VlanIdCriterion vidCriterion,
Saurav Das0e99e2b2015-10-28 12:39:42 -0700753 VlanId assignedVlan,
Charles Chan45b69ab2018-03-02 15:41:41 -0800754 MacAddress unicastMac,
Saurav Das822c4e22015-10-23 10:51:11 -0700755 ApplicationId applicationId) {
Charles Chan5270ed02016-01-30 23:22:37 -0800756 // Consider PortNumber.ANY as wildcard. Match ETH_DST only
Sivachidambaram Subramanianc598d062017-05-16 22:42:06 +0530757 if (portCriterion != null && PortNumber.ANY.equals(portCriterion.port())) {
Charles Chan5270ed02016-01-30 23:22:37 -0800758 return processEthDstOnlyFilter(ethCriterion, applicationId);
759 }
760
Charles Chan5b9df8d2016-03-28 22:21:40 -0700761 // Multicast MAC
762 if (ethCriterion.mask() != null) {
Charles Chan45b69ab2018-03-02 15:41:41 -0800763 return processMcastEthDstFilter(ethCriterion, assignedVlan, unicastMac, applicationId);
Charles Chan5b9df8d2016-03-28 22:21:40 -0700764 }
765
Saurav Das822c4e22015-10-23 10:51:11 -0700766 //handling untagged packets via assigned VLAN
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530767 if (vidCriterion != null && vidCriterion.vlanId() == VlanId.NONE) {
Saurav Das0e99e2b2015-10-28 12:39:42 -0700768 vidCriterion = (VlanIdCriterion) Criteria.matchVlanId(assignedVlan);
Saurav Das822c4e22015-10-23 10:51:11 -0700769 }
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530770 List<FlowRule> rules = new ArrayList<>();
771 OfdpaMatchVlanVid ofdpaMatchVlanVid = null;
772 if (vidCriterion != null && requireVlanExtensions()) {
773 ofdpaMatchVlanVid = new OfdpaMatchVlanVid(vidCriterion.vlanId());
774 }
Saurav Das822c4e22015-10-23 10:51:11 -0700775 // ofdpa cannot match on ALL portnumber, so we need to use separate
776 // rules for each port.
Yuta HIGUCHI2dce08a2017-04-20 21:57:48 -0700777 List<PortNumber> portnums = new ArrayList<>();
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530778 if (portCriterion != null) {
779 if (PortNumber.ALL.equals(portCriterion.port())) {
780 for (Port port : deviceService.getPorts(deviceId)) {
781 if (port.number().toLong() > 0 && port.number().toLong() < OFPP_MAX) {
782 portnums.add(port.number());
783 }
Saurav Das822c4e22015-10-23 10:51:11 -0700784 }
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530785 } else {
786 portnums.add(portCriterion.port());
787 }
788 for (PortNumber pnum : portnums) {
789 rules.add(buildTmacRuleForIpv4(ethCriterion,
790 vidCriterion,
791 ofdpaMatchVlanVid,
792 applicationId,
793 pnum));
794 rules.add(buildTmacRuleForMpls(ethCriterion,
795 vidCriterion,
796 ofdpaMatchVlanVid,
797 applicationId,
798 pnum));
799 rules.add(buildTmacRuleForIpv6(ethCriterion,
800 vidCriterion,
801 ofdpaMatchVlanVid,
802 applicationId,
803 pnum));
Saurav Das822c4e22015-10-23 10:51:11 -0700804 }
805 } else {
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530806 rules.add(buildTmacRuleForIpv4(ethCriterion,
807 vidCriterion,
808 ofdpaMatchVlanVid,
809 applicationId,
810 null));
811 rules.add(buildTmacRuleForMpls(ethCriterion,
812 vidCriterion,
813 ofdpaMatchVlanVid,
814 applicationId,
815 null));
816 rules.add(buildTmacRuleForIpv6(ethCriterion,
817 vidCriterion,
818 ofdpaMatchVlanVid,
819 applicationId,
820 null));
Saurav Das822c4e22015-10-23 10:51:11 -0700821 }
Charles Chan66291502018-03-02 16:43:28 -0800822 return ImmutableList.of(rules);
Saurav Das822c4e22015-10-23 10:51:11 -0700823 }
824
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530825 /**
826 * Builds TMAC rules for IPv4 packets.
827 *
Charles Chanc550f2e2017-12-19 19:55:57 -0800828 * @param ethCriterion dst mac matching
829 * @param vidCriterion vlan id assigned to the port
830 * @param ofdpaMatchVlanVid OFDPA vlan id matching
831 * @param applicationId application id
832 * @param pnum port number
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530833 * @return TMAC rule for IPV4 packets
834 */
835 private FlowRule buildTmacRuleForIpv4(EthCriterion ethCriterion,
836 VlanIdCriterion vidCriterion,
837 OfdpaMatchVlanVid ofdpaMatchVlanVid,
838 ApplicationId applicationId,
839 PortNumber pnum) {
840 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
841 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
842 if (pnum != null) {
843 if (matchInPortTmacTable()) {
844 selector.matchInPort(pnum);
845 } else {
Saurav Dasf14d9ef2017-12-05 15:00:23 -0800846 log.debug("Pipeline does not support IN_PORT matching in TMAC table, " +
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530847 "ignoring the IN_PORT criteria");
848 }
849 }
850 if (vidCriterion != null) {
851 if (requireVlanExtensions()) {
852 selector.extension(ofdpaMatchVlanVid, deviceId);
853 } else {
854 selector.matchVlanId(vidCriterion.vlanId());
855 }
856 }
857 selector.matchEthType(Ethernet.TYPE_IPV4);
858 selector.matchEthDst(ethCriterion.mac());
859 treatment.transition(UNICAST_ROUTING_TABLE);
860 return DefaultFlowRule.builder()
861 .forDevice(deviceId)
862 .withSelector(selector.build())
863 .withTreatment(treatment.build())
864 .withPriority(DEFAULT_PRIORITY)
865 .fromApp(applicationId)
866 .makePermanent()
867 .forTable(TMAC_TABLE).build();
868 }
869
870 /**
871 * Builds TMAC rule for MPLS packets.
872 *
Charles Chanc550f2e2017-12-19 19:55:57 -0800873 * @param ethCriterion dst mac matching
874 * @param vidCriterion vlan id assigned to the port
875 * @param ofdpaMatchVlanVid OFDPA vlan id matching
876 * @param applicationId application id
877 * @param pnum port number
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530878 * @return TMAC rule for MPLS packets
879 */
880 private FlowRule buildTmacRuleForMpls(EthCriterion ethCriterion,
881 VlanIdCriterion vidCriterion,
882 OfdpaMatchVlanVid ofdpaMatchVlanVid,
883 ApplicationId applicationId,
884 PortNumber pnum) {
885 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
886 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
887 if (pnum != null) {
888 if (matchInPortTmacTable()) {
889 selector.matchInPort(pnum);
890 } else {
Saurav Dasf14d9ef2017-12-05 15:00:23 -0800891 log.debug("Pipeline does not support IN_PORT matching in TMAC table, " +
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530892 "ignoring the IN_PORT criteria");
893 }
894 }
895 if (vidCriterion != null) {
896 if (requireVlanExtensions()) {
897 selector.extension(ofdpaMatchVlanVid, deviceId);
898 } else {
899 selector.matchVlanId(vidCriterion.vlanId());
900 }
901 }
902 selector.matchEthType(Ethernet.MPLS_UNICAST);
903 selector.matchEthDst(ethCriterion.mac());
904 treatment.transition(MPLS_TABLE_0);
905 return DefaultFlowRule.builder()
906 .forDevice(deviceId)
907 .withSelector(selector.build())
908 .withTreatment(treatment.build())
909 .withPriority(DEFAULT_PRIORITY)
910 .fromApp(applicationId)
911 .makePermanent()
912 .forTable(TMAC_TABLE).build();
913 }
914
915 /**
916 * Builds TMAC rules for IPv6 packets.
917 *
Charles Chanc550f2e2017-12-19 19:55:57 -0800918 * @param ethCriterion dst mac matching
919 * @param vidCriterion vlan id assigned to the port
920 * @param ofdpaMatchVlanVid OFDPA vlan id matching
921 * @param applicationId application id
922 * @param pnum port number
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530923 * @return TMAC rule for IPV6 packets
924 */
925 private FlowRule buildTmacRuleForIpv6(EthCriterion ethCriterion,
926 VlanIdCriterion vidCriterion,
927 OfdpaMatchVlanVid ofdpaMatchVlanVid,
928 ApplicationId applicationId,
929 PortNumber pnum) {
930 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
931 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
932 if (pnum != null) {
933 if (matchInPortTmacTable()) {
934 selector.matchInPort(pnum);
935 } else {
Saurav Dasf14d9ef2017-12-05 15:00:23 -0800936 log.debug("Pipeline does not support IN_PORT matching in TMAC table, " +
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530937 "ignoring the IN_PORT criteria");
938 }
939 }
940 if (vidCriterion != null) {
941 if (requireVlanExtensions()) {
942 selector.extension(ofdpaMatchVlanVid, deviceId);
943 } else {
944 selector.matchVlanId(vidCriterion.vlanId());
945 }
946 }
947 selector.matchEthType(Ethernet.TYPE_IPV6);
948 selector.matchEthDst(ethCriterion.mac());
949 treatment.transition(UNICAST_ROUTING_TABLE);
950 return DefaultFlowRule.builder()
951 .forDevice(deviceId)
952 .withSelector(selector.build())
953 .withTreatment(treatment.build())
954 .withPriority(DEFAULT_PRIORITY)
955 .fromApp(applicationId)
956 .makePermanent()
957 .forTable(TMAC_TABLE).build();
958 }
959
Charles Chan66291502018-03-02 16:43:28 -0800960 protected List<List<FlowRule>> processEthDstOnlyFilter(EthCriterion ethCriterion,
Yi Tsengef19de12017-04-24 11:33:05 -0700961 ApplicationId applicationId) {
Pier Luigi0e358632017-01-31 09:35:05 -0800962 ImmutableList.Builder<FlowRule> builder = ImmutableList.builder();
963
Charles Chan5270ed02016-01-30 23:22:37 -0800964 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
965 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
966 selector.matchEthType(Ethernet.TYPE_IPV4);
967 selector.matchEthDst(ethCriterion.mac());
968 treatment.transition(UNICAST_ROUTING_TABLE);
969 FlowRule rule = DefaultFlowRule.builder()
970 .forDevice(deviceId)
971 .withSelector(selector.build())
972 .withTreatment(treatment.build())
973 .withPriority(DEFAULT_PRIORITY)
974 .fromApp(applicationId)
975 .makePermanent()
976 .forTable(TMAC_TABLE).build();
Pier Luigi0e358632017-01-31 09:35:05 -0800977 builder.add(rule);
978
979 selector = DefaultTrafficSelector.builder();
980 treatment = DefaultTrafficTreatment.builder();
981 selector.matchEthType(Ethernet.TYPE_IPV6);
982 selector.matchEthDst(ethCriterion.mac());
983 treatment.transition(UNICAST_ROUTING_TABLE);
984 rule = DefaultFlowRule.builder()
985 .forDevice(deviceId)
986 .withSelector(selector.build())
987 .withTreatment(treatment.build())
988 .withPriority(DEFAULT_PRIORITY)
989 .fromApp(applicationId)
990 .makePermanent()
991 .forTable(TMAC_TABLE).build();
Charles Chan66291502018-03-02 16:43:28 -0800992 return ImmutableList.of(builder.add(rule).build());
Charles Chan5270ed02016-01-30 23:22:37 -0800993 }
994
pier5be39042019-10-04 10:54:07 +0200995 private FlowRule buildTmacRuleForMcastFromUnicast(FlowRule tmacRuleForUnicast, ApplicationId applicationId) {
996 final TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
997 final TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
998 FlowRule rule;
999 // Build the selector
1000 for (Criterion criterion : tmacRuleForUnicast.selector().criteria()) {
1001 if (criterion instanceof VlanIdCriterion) {
1002 if (requireVlanExtensions()) {
1003 OfdpaMatchVlanVid ofdpaMatchVlanVid = new OfdpaMatchVlanVid(((VlanIdCriterion) criterion).vlanId());
1004 selector.extension(ofdpaMatchVlanVid, deviceId);
1005 } else {
1006 selector.add(criterion);
1007 }
1008 } else if (criterion instanceof EthTypeCriterion) {
1009 selector.add(criterion);
1010 if (Objects.equals(((EthTypeCriterion) criterion).ethType(),
1011 EtherType.IPV4.ethType())) {
1012 selector.matchEthDstMasked(IPV4_MULTICAST, IPV4_MULTICAST_MASK);
1013 } else if (Objects.equals(((EthTypeCriterion) criterion).ethType(),
1014 EtherType.IPV6.ethType())) {
1015 selector.matchEthDstMasked(IPV6_MULTICAST, IPV6_MULTICAST_MASK);
1016 } else {
1017 // We don't need for mpls rules
1018 return null;
1019 }
1020 }
1021 }
1022 // Build the treatment
1023 treatment.transition(MULTICAST_ROUTING_TABLE);
1024 // Build the flowrule
1025 rule = DefaultFlowRule.builder()
1026 .forDevice(deviceId)
1027 .withSelector(selector.build())
1028 .withTreatment(treatment.build())
1029 .withPriority(DEFAULT_PRIORITY)
1030 .fromApp(applicationId)
1031 .makePermanent()
1032 .forTable(TMAC_TABLE).build();
pierbf688182019-12-06 12:39:51 +01001033 log.debug("Building TMac Mcast flowRule {}", rule);
pier5be39042019-10-04 10:54:07 +02001034 return rule;
1035 }
1036
Charles Chan66291502018-03-02 16:43:28 -08001037 List<List<FlowRule>> processMcastEthDstFilter(EthCriterion ethCriterion,
Charles Chanb4879a52017-10-20 19:09:16 -07001038 VlanId assignedVlan,
Charles Chan45b69ab2018-03-02 15:41:41 -08001039 MacAddress unicastMac,
Yi Tsengef19de12017-04-24 11:33:05 -07001040 ApplicationId applicationId) {
Charles Chan45b69ab2018-03-02 15:41:41 -08001041 ImmutableList.Builder<FlowRule> unicastFlows = ImmutableList.builder();
1042 ImmutableList.Builder<FlowRule> multicastFlows = ImmutableList.builder();
1043 TrafficSelector.Builder selector;
1044 TrafficTreatment.Builder treatment;
Charles Chanb4879a52017-10-20 19:09:16 -07001045 FlowRule rule;
Julia Ferguson65428c32017-08-10 18:15:24 +00001046
Charles Chanb4879a52017-10-20 19:09:16 -07001047 if (IPV4_MULTICAST.equals(ethCriterion.mac())) {
Charles Chan45b69ab2018-03-02 15:41:41 -08001048 if (requireUnicastBeforeMulticast()) {
1049 selector = DefaultTrafficSelector.builder();
1050 treatment = DefaultTrafficTreatment.builder();
1051 selector.matchEthType(Ethernet.TYPE_IPV4);
1052 selector.matchEthDst(unicastMac);
Charles Chan056e0c12018-05-10 22:19:49 +00001053 selector.matchVlanId(assignedVlan);
Charles Chan45b69ab2018-03-02 15:41:41 -08001054 treatment.transition(UNICAST_ROUTING_TABLE);
1055 rule = DefaultFlowRule.builder()
1056 .forDevice(deviceId)
1057 .withSelector(selector.build())
1058 .withTreatment(treatment.build())
Charles Chan056e0c12018-05-10 22:19:49 +00001059 .withPriority(DEFAULT_PRIORITY)
Charles Chan45b69ab2018-03-02 15:41:41 -08001060 .fromApp(applicationId)
1061 .makePermanent()
1062 .forTable(TMAC_TABLE).build();
1063 unicastFlows.add(rule);
1064 }
1065
1066 selector = DefaultTrafficSelector.builder();
1067 treatment = DefaultTrafficTreatment.builder();
Charles Chanb4879a52017-10-20 19:09:16 -07001068 selector.matchEthType(Ethernet.TYPE_IPV4);
1069 selector.matchEthDstMasked(ethCriterion.mac(), ethCriterion.mask());
Charles Chan056e0c12018-05-10 22:19:49 +00001070 selector.matchVlanId(assignedVlan);
Charles Chanb4879a52017-10-20 19:09:16 -07001071 treatment.transition(MULTICAST_ROUTING_TABLE);
1072 rule = DefaultFlowRule.builder()
1073 .forDevice(deviceId)
1074 .withSelector(selector.build())
1075 .withTreatment(treatment.build())
Charles Chan056e0c12018-05-10 22:19:49 +00001076 .withPriority(DEFAULT_PRIORITY)
Charles Chanb4879a52017-10-20 19:09:16 -07001077 .fromApp(applicationId)
1078 .makePermanent()
1079 .forTable(TMAC_TABLE).build();
Charles Chan45b69ab2018-03-02 15:41:41 -08001080 multicastFlows.add(rule);
Charles Chanb4879a52017-10-20 19:09:16 -07001081 }
1082
1083 if (IPV6_MULTICAST.equals(ethCriterion.mac())) {
Charles Chan45b69ab2018-03-02 15:41:41 -08001084 if (requireUnicastBeforeMulticast()) {
1085 selector = DefaultTrafficSelector.builder();
1086 treatment = DefaultTrafficTreatment.builder();
1087 selector.matchEthType(Ethernet.TYPE_IPV6);
1088 selector.matchEthDst(unicastMac);
Charles Chan056e0c12018-05-10 22:19:49 +00001089 selector.matchVlanId(assignedVlan);
Charles Chan45b69ab2018-03-02 15:41:41 -08001090 treatment.transition(UNICAST_ROUTING_TABLE);
1091 rule = DefaultFlowRule.builder()
1092 .forDevice(deviceId)
1093 .withSelector(selector.build())
1094 .withTreatment(treatment.build())
Charles Chan056e0c12018-05-10 22:19:49 +00001095 .withPriority(DEFAULT_PRIORITY)
Charles Chan45b69ab2018-03-02 15:41:41 -08001096 .fromApp(applicationId)
1097 .makePermanent()
1098 .forTable(TMAC_TABLE).build();
1099 unicastFlows.add(rule);
1100 }
1101
Charles Chanb4879a52017-10-20 19:09:16 -07001102 selector = DefaultTrafficSelector.builder();
1103 treatment = DefaultTrafficTreatment.builder();
1104 selector.matchEthType(Ethernet.TYPE_IPV6);
1105 selector.matchEthDstMasked(ethCriterion.mac(), ethCriterion.mask());
Charles Chan056e0c12018-05-10 22:19:49 +00001106 selector.matchVlanId(assignedVlan);
Charles Chanb4879a52017-10-20 19:09:16 -07001107 treatment.transition(MULTICAST_ROUTING_TABLE);
1108 rule = DefaultFlowRule.builder()
1109 .forDevice(deviceId)
1110 .withSelector(selector.build())
1111 .withTreatment(treatment.build())
Charles Chan056e0c12018-05-10 22:19:49 +00001112 .withPriority(DEFAULT_PRIORITY)
Charles Chanb4879a52017-10-20 19:09:16 -07001113 .fromApp(applicationId)
1114 .makePermanent()
1115 .forTable(TMAC_TABLE).build();
Charles Chan45b69ab2018-03-02 15:41:41 -08001116 multicastFlows.add(rule);
Charles Chanb4879a52017-10-20 19:09:16 -07001117 }
Charles Chan45b69ab2018-03-02 15:41:41 -08001118 return ImmutableList.of(unicastFlows.build(), multicastFlows.build());
Charles Chan5b9df8d2016-03-28 22:21:40 -07001119 }
1120
Saurav Das822c4e22015-10-23 10:51:11 -07001121 private Collection<FlowRule> processForward(ForwardingObjective fwd) {
1122 switch (fwd.flag()) {
1123 case SPECIFIC:
1124 return processSpecific(fwd);
1125 case VERSATILE:
1126 return processVersatile(fwd);
Jonghwan Hyun800d9d02018-04-09 09:40:50 -07001127 case EGRESS:
1128 return processEgress(fwd);
Saurav Das822c4e22015-10-23 10:51:11 -07001129 default:
1130 fail(fwd, ObjectiveError.UNKNOWN);
1131 log.warn("Unknown forwarding flag {}", fwd.flag());
1132 }
1133 return Collections.emptySet();
1134 }
1135
1136 /**
Jonghwan Hyun800d9d02018-04-09 09:40:50 -07001137 * In the OF-DPA 2.0 pipeline, egress forwarding objectives go to the
1138 * egress tables.
1139 * @param fwd the forwarding objective of type 'egress'
1140 * @return a collection of flow rules to be sent to the switch. An empty
1141 * collection may be returned if there is a problem in processing
1142 * the flow rule
1143 */
1144 protected Collection<FlowRule> processEgress(ForwardingObjective fwd) {
1145 log.debug("Processing egress forwarding objective:{} in dev:{}",
1146 fwd, deviceId);
1147
1148 List<FlowRule> rules = new ArrayList<>();
1149
1150 // Build selector
1151 TrafficSelector.Builder sb = DefaultTrafficSelector.builder();
1152 VlanIdCriterion vlanIdCriterion = (VlanIdCriterion) fwd.selector().getCriterion(Criterion.Type.VLAN_VID);
1153 if (vlanIdCriterion == null) {
1154 log.error("Egress forwarding objective:{} must include vlanId", fwd.id());
1155 fail(fwd, ObjectiveError.BADPARAMS);
1156 return rules;
1157 }
1158
1159 Optional<Instruction> outInstr = fwd.treatment().allInstructions().stream()
1160 .filter(instruction -> instruction instanceof OutputInstruction).findFirst();
1161 if (!outInstr.isPresent()) {
1162 log.error("Egress forwarding objective:{} must include output port", fwd.id());
1163 fail(fwd, ObjectiveError.BADPARAMS);
1164 return rules;
1165 }
1166
1167 PortNumber portNumber = ((OutputInstruction) outInstr.get()).port();
1168
1169 sb.matchVlanId(vlanIdCriterion.vlanId());
1170 OfdpaMatchActsetOutput actsetOutput = new OfdpaMatchActsetOutput(portNumber);
1171 sb.extension(actsetOutput, deviceId);
1172
1173 sb.extension(new OfdpaMatchAllowVlanTranslation(ALLOW_VLAN_TRANSLATION), deviceId);
1174
1175 // Build a flow rule for Egress VLAN Flow table
1176 TrafficTreatment.Builder tb = DefaultTrafficTreatment.builder();
1177 tb.transition(EGRESS_DSCP_PCP_REMARK_FLOW_TABLE);
1178 if (fwd.treatment() != null) {
1179 for (Instruction instr : fwd.treatment().allInstructions()) {
1180 if (instr instanceof L2ModificationInstruction &&
1181 ((L2ModificationInstruction) instr).subtype() == L2SubType.VLAN_ID) {
1182 tb.immediate().add(instr);
1183 }
1184 if (instr instanceof L2ModificationInstruction &&
1185 ((L2ModificationInstruction) instr).subtype() == L2SubType.VLAN_PUSH) {
1186 tb.immediate().pushVlan();
1187 EthType ethType = ((L2ModificationInstruction.ModVlanHeaderInstruction) instr).ethernetType();
1188 if (ethType.equals(EtherType.QINQ.ethType())) {
1189 // Build a flow rule for Egress TPID Flow table
1190 TrafficSelector tpidSelector = DefaultTrafficSelector.builder()
1191 .extension(actsetOutput, deviceId)
1192 .matchVlanId(VlanId.ANY).build();
1193
1194 TrafficTreatment tpidTreatment = DefaultTrafficTreatment.builder()
1195 .extension(new Ofdpa3CopyField(COPY_FIELD_NBITS, COPY_FIELD_OFFSET,
1196 COPY_FIELD_OFFSET, OXM_ID_VLAN_VID,
1197 OXM_ID_PACKET_REG_1),
1198 deviceId)
1199 .popVlan()
1200 .pushVlan(EtherType.QINQ.ethType())
1201 .extension(new Ofdpa3CopyField(COPY_FIELD_NBITS, COPY_FIELD_OFFSET,
1202 COPY_FIELD_OFFSET, OXM_ID_PACKET_REG_1,
1203 OXM_ID_VLAN_VID),
1204 deviceId)
1205 .build();
1206
1207 FlowRule.Builder tpidRuleBuilder = DefaultFlowRule.builder()
1208 .fromApp(fwd.appId())
1209 .withPriority(fwd.priority())
1210 .forDevice(deviceId)
1211 .withSelector(tpidSelector)
1212 .withTreatment(tpidTreatment)
1213 .makePermanent()
1214 .forTable(EGRESS_TPID_FLOW_TABLE);
1215 rules.add(tpidRuleBuilder.build());
1216 }
1217 }
1218 }
1219 }
1220
1221 FlowRule.Builder ruleBuilder = DefaultFlowRule.builder()
1222 .fromApp(fwd.appId())
1223 .withPriority(fwd.priority())
1224 .forDevice(deviceId)
1225 .withSelector(sb.build())
1226 .withTreatment(tb.build())
1227 .makePermanent()
1228 .forTable(EGRESS_VLAN_FLOW_TABLE);
1229 rules.add(ruleBuilder.build());
1230 return rules;
1231 }
1232
1233 /**
Saurav Das822c4e22015-10-23 10:51:11 -07001234 * In the OF-DPA 2.0 pipeline, versatile forwarding objectives go to the
1235 * ACL table.
1236 * @param fwd the forwarding objective of type 'versatile'
1237 * @return a collection of flow rules to be sent to the switch. An empty
1238 * collection may be returned if there is a problem in processing
1239 * the flow rule
1240 */
Saurav Das52025962016-01-28 22:30:01 -08001241 protected Collection<FlowRule> processVersatile(ForwardingObjective fwd) {
Saurav Das018605f2017-02-18 14:05:44 -08001242 log.debug("Processing versatile forwarding objective:{} in dev:{}",
Saurav Dasd2fded02016-12-02 15:43:47 -08001243 fwd.id(), deviceId);
Harshada Chaundkarcb787512019-08-05 15:33:30 +00001244 List<FlowRule> flowRules = new ArrayList<>();
1245 final AtomicBoolean ethTypeUsed = new AtomicBoolean(false);
Saurav Das822c4e22015-10-23 10:51:11 -07001246
Saurav Das822c4e22015-10-23 10:51:11 -07001247 if (fwd.nextId() == null && fwd.treatment() == null) {
1248 log.error("Forwarding objective {} from {} must contain "
1249 + "nextId or Treatment", fwd.selector(), fwd.appId());
Zsolt Haraszti9faab752016-02-17 15:55:20 -08001250 fail(fwd, ObjectiveError.BADPARAMS);
Saurav Das822c4e22015-10-23 10:51:11 -07001251 return Collections.emptySet();
1252 }
Saurav Das49cb5a12016-01-16 22:54:07 -08001253
Saurav Das77b5e902016-01-27 17:01:59 -08001254 TrafficSelector.Builder sbuilder = DefaultTrafficSelector.builder();
1255 fwd.selector().criteria().forEach(criterion -> {
1256 if (criterion instanceof VlanIdCriterion) {
1257 VlanId vlanId = ((VlanIdCriterion) criterion).vlanId();
1258 // ensure that match does not include vlan = NONE as OF-DPA does not
1259 // match untagged packets this way in the ACL table.
1260 if (vlanId.equals(VlanId.NONE)) {
1261 return;
1262 }
Charles Chand1172632017-03-15 17:33:09 -07001263 if (requireVlanExtensions()) {
1264 OfdpaMatchVlanVid ofdpaMatchVlanVid = new OfdpaMatchVlanVid(vlanId);
1265 sbuilder.extension(ofdpaMatchVlanVid, deviceId);
1266 } else {
1267 sbuilder.matchVlanId(vlanId);
1268 }
Charles Chan09bf2692018-01-11 11:48:18 -08001269 } else if (criterion instanceof Icmpv6TypeCriterion) {
1270 byte icmpv6Type = (byte) ((Icmpv6TypeCriterion) criterion).icmpv6Type();
1271 sbuilder.matchIcmpv6Type(icmpv6Type);
1272 } else if (criterion instanceof Icmpv6CodeCriterion) {
1273 byte icmpv6Code = (byte) ((Icmpv6CodeCriterion) criterion).icmpv6Code();
1274 sbuilder.matchIcmpv6Type(icmpv6Code);
Charles Chand9e47c62017-10-05 15:17:15 -07001275 } else if (criterion instanceof TcpPortCriterion || criterion instanceof UdpPortCriterion) {
1276 // FIXME: QMX switches do not support L4 dst port matching in ACL table.
1277 // Currently L4 dst port matching is only used by DHCP relay feature
1278 // and therefore is safe to be replaced with L4 src port matching.
1279 // We need to revisit this if L4 dst port is used for other purpose in the future.
1280 if (!supportIpv6L4Dst() && isIpv6(fwd.selector())) {
Charles Chand9e47c62017-10-05 15:17:15 -07001281 switch (criterion.type()) {
1282 case UDP_DST:
Charles Chand9e47c62017-10-05 15:17:15 -07001283 case UDP_DST_MASKED:
Charles Chand9e47c62017-10-05 15:17:15 -07001284 case TCP_DST:
Charles Chand9e47c62017-10-05 15:17:15 -07001285 case TCP_DST_MASKED:
Charles Chand9e47c62017-10-05 15:17:15 -07001286 break;
1287 default:
1288 sbuilder.add(criterion);
1289 }
1290 } else {
1291 sbuilder.add(criterion);
1292 }
Harshada Chaundkarcb787512019-08-05 15:33:30 +00001293 } else if (criterion instanceof EthTypeCriterion) {
1294 sbuilder.add(criterion);
1295 ethTypeUsed.set(true);
Saurav Das77b5e902016-01-27 17:01:59 -08001296 } else {
1297 sbuilder.add(criterion);
1298 }
1299 });
Harshada Chaundkarcb787512019-08-05 15:33:30 +00001300
1301 TrafficTreatment.Builder ttBuilder = versatileTreatmentBuilder(fwd);
1302 if (ttBuilder == null) {
1303 return Collections.emptySet();
1304 }
1305
1306 FlowRule.Builder ruleBuilder = DefaultFlowRule.builder()
1307 .fromApp(fwd.appId())
1308 .withPriority(fwd.priority())
1309 .forDevice(deviceId)
1310 .withSelector(sbuilder.build())
1311 .withTreatment(ttBuilder.build())
1312 .makePermanent()
1313 .forTable(ACL_TABLE);
1314
1315 flowRules.add(ruleBuilder.build());
1316
1317 if (!ethTypeUsed.get() && requireEthType()) {
1318 log.debug("{} doesn't match on ethType but requireEthType is true, adding complementary ACL flow.",
1319 sbuilder.toString());
1320 sbuilder.matchEthType(Ethernet.TYPE_IPV6);
1321 FlowRule.Builder ethTypeRuleBuilder = DefaultFlowRule.builder()
1322 .fromApp(fwd.appId())
1323 .withPriority(fwd.priority())
1324 .forDevice(deviceId)
1325 .withSelector(sbuilder.build())
1326 .withTreatment(ttBuilder.build())
1327 .makePermanent()
1328 .forTable(ACL_TABLE);
1329 flowRules.add(ethTypeRuleBuilder.build());
1330 }
1331 return flowRules;
Charles Chanab591602019-01-22 17:25:04 -08001332 }
Saurav Das77b5e902016-01-27 17:01:59 -08001333
Charles Chanab591602019-01-22 17:25:04 -08001334 /**
1335 * Helper function to create traffic treatment builder for versatile forwarding objectives.
1336 *
1337 * @param fwd original forwarding objective
1338 * @return treatment builder for the flow rule, or null if there is an error.
1339 */
1340 protected TrafficTreatment.Builder versatileTreatmentBuilder(ForwardingObjective fwd) {
Saurav Das822c4e22015-10-23 10:51:11 -07001341 // XXX driver does not currently do type checking as per Tables 65-67 in
1342 // OFDPA 2.0 spec. The only allowed treatment is a punt to the controller.
Saurav Das49cb5a12016-01-16 22:54:07 -08001343 TrafficTreatment.Builder ttBuilder = DefaultTrafficTreatment.builder();
1344 if (fwd.treatment() != null) {
1345 for (Instruction ins : fwd.treatment().allInstructions()) {
1346 if (ins instanceof OutputInstruction) {
1347 OutputInstruction o = (OutputInstruction) ins;
Ray Milkeyfe0e0852018-01-18 11:14:05 -08001348 if (PortNumber.CONTROLLER.equals(o.port())) {
Saurav Das49cb5a12016-01-16 22:54:07 -08001349 ttBuilder.add(o);
1350 } else {
1351 log.warn("Only allowed treatments in versatile forwarding "
1352 + "objectives are punts to the controller");
1353 }
Charles Chanf76de302018-06-15 18:54:18 -07001354 } else if (ins instanceof NoActionInstruction) {
1355 // No action is allowed and nothing needs to be done
Saurav Das49cb5a12016-01-16 22:54:07 -08001356 } else {
1357 log.warn("Cannot process instruction in versatile fwd {}", ins);
1358 }
Saurav Das822c4e22015-10-23 10:51:11 -07001359 }
Charles Chan2df0e8a2017-01-09 11:45:08 -08001360 if (fwd.treatment().clearedDeferred()) {
1361 ttBuilder.wipeDeferred();
1362 }
Saurav Das822c4e22015-10-23 10:51:11 -07001363 }
Saurav Das822c4e22015-10-23 10:51:11 -07001364 if (fwd.nextId() != null) {
Charles Chanab591602019-01-22 17:25:04 -08001365 // Override case
Saurav Das49cb5a12016-01-16 22:54:07 -08001366 NextGroup next = getGroupForNextObjective(fwd.nextId());
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +05301367 if (next == null) {
1368 fail(fwd, ObjectiveError.BADPARAMS);
Charles Chanab591602019-01-22 17:25:04 -08001369 return null;
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +05301370 }
Saurav Das49cb5a12016-01-16 22:54:07 -08001371 List<Deque<GroupKey>> gkeys = appKryo.deserialize(next.data());
1372 // we only need the top level group's key to point the flow to it
1373 Group group = groupService.getGroup(deviceId, gkeys.get(0).peekFirst());
1374 if (group == null) {
1375 log.warn("Group with key:{} for next-id:{} not found in dev:{}",
Charles Chanab591602019-01-22 17:25:04 -08001376 gkeys.get(0).peekFirst(), fwd.nextId(), deviceId);
Saurav Das49cb5a12016-01-16 22:54:07 -08001377 fail(fwd, ObjectiveError.GROUPMISSING);
Charles Chanab591602019-01-22 17:25:04 -08001378 return null;
Saurav Das49cb5a12016-01-16 22:54:07 -08001379 }
1380 ttBuilder.deferred().group(group.id());
Saurav Das822c4e22015-10-23 10:51:11 -07001381 }
Charles Chanab591602019-01-22 17:25:04 -08001382 return ttBuilder;
Saurav Das822c4e22015-10-23 10:51:11 -07001383 }
1384
1385 /**
1386 * In the OF-DPA 2.0 pipeline, specific forwarding refers to the IP table
Saurav Das8a0732e2015-11-20 15:27:53 -08001387 * (unicast or multicast) or the L2 table (mac + vlan) or the MPLS table.
Saurav Das822c4e22015-10-23 10:51:11 -07001388 *
1389 * @param fwd the forwarding objective of type 'specific'
1390 * @return a collection of flow rules. Typically there will be only one
1391 * for this type of forwarding objective. An empty set may be
1392 * returned if there is an issue in processing the objective.
1393 */
Charles Chan50d900c2018-03-02 13:26:22 -08001394 private Collection<FlowRule> processSpecific(ForwardingObjective fwd) {
Saurav Das25190812016-05-27 13:54:07 -07001395 log.debug("Processing specific fwd objective:{} in dev:{} with next:{}",
Saurav Das4ce45962015-11-24 23:21:05 -08001396 fwd.id(), deviceId, fwd.nextId());
1397 boolean isEthTypeObj = isSupportedEthTypeObjective(fwd);
1398 boolean isEthDstObj = isSupportedEthDstObjective(fwd);
1399
1400 if (isEthTypeObj) {
1401 return processEthTypeSpecific(fwd);
1402 } else if (isEthDstObj) {
1403 return processEthDstSpecific(fwd);
1404 } else {
1405 log.warn("processSpecific: Unsupported forwarding objective "
1406 + "criteria fwd:{} in dev:{}", fwd.nextId(), deviceId);
Saurav Das822c4e22015-10-23 10:51:11 -07001407 fail(fwd, ObjectiveError.UNSUPPORTED);
1408 return Collections.emptySet();
1409 }
Saurav Das4ce45962015-11-24 23:21:05 -08001410 }
1411
Saurav Das4ce45962015-11-24 23:21:05 -08001412 /**
1413 * Handles forwarding rules to the IP and MPLS tables.
1414 *
1415 * @param fwd the forwarding objective
1416 * @return A collection of flow rules, or an empty set
1417 */
1418 protected Collection<FlowRule> processEthTypeSpecific(ForwardingObjective fwd) {
Charles Chancad338a2016-09-16 18:03:11 -07001419 return processEthTypeSpecificInternal(fwd, false, ACL_TABLE);
Charles Chanf9e98652016-09-07 16:54:23 -07001420 }
1421
1422 /**
1423 * Internal implementation of processEthTypeSpecific.
1424 * <p>
1425 * Wildcarded IPv4_DST is not supported in OFDPA i12. Therefore, we break
1426 * the rule into 0.0.0.0/1 and 128.0.0.0/1.
1427 * The allowDefaultRoute must be set to false for OFDPA i12.
1428 * </p>
1429 *
1430 * @param fwd the forwarding objective
1431 * @param allowDefaultRoute allow wildcarded IPv4_DST or not
Ray Milkey0bb1e102016-11-10 14:51:27 -08001432 * @param mplsNextTable next MPLS table
Charles Chanf9e98652016-09-07 16:54:23 -07001433 * @return A collection of flow rules, or an empty set
1434 */
1435 protected Collection<FlowRule> processEthTypeSpecificInternal(ForwardingObjective fwd,
Charles Chancad338a2016-09-16 18:03:11 -07001436 boolean allowDefaultRoute,
1437 int mplsNextTable) {
Saurav Das4ce45962015-11-24 23:21:05 -08001438 TrafficSelector selector = fwd.selector();
1439 EthTypeCriterion ethType =
1440 (EthTypeCriterion) selector.getCriterion(Criterion.Type.ETH_TYPE);
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001441 boolean emptyGroup = false;
Charles Chan188ebf52015-12-23 00:15:11 -08001442 int forTableId;
Saurav Das8a0732e2015-11-20 15:27:53 -08001443 TrafficSelector.Builder filteredSelector = DefaultTrafficSelector.builder();
Charles Chan14967c22015-12-07 11:11:50 -08001444 TrafficTreatment.Builder tb = DefaultTrafficTreatment.builder();
Flavio Castroe10fa242016-01-15 12:43:51 -08001445 TrafficSelector.Builder complementarySelector = DefaultTrafficSelector.builder();
Charles Chan14967c22015-12-07 11:11:50 -08001446
Saurav Das8a0732e2015-11-20 15:27:53 -08001447 if (ethType.ethType().toShort() == Ethernet.TYPE_IPV4) {
Pier Luigi3bfe32c2017-01-30 09:47:36 -08001448 if (buildIpv4Selector(filteredSelector, complementarySelector, fwd, allowDefaultRoute) < 0) {
1449 return Collections.emptyList();
1450 }
1451 // We need to set properly the next table
Flavio Castroe10fa242016-01-15 12:43:51 -08001452 IpPrefix ipv4Dst = ((IPCriterion) selector.getCriterion(Criterion.Type.IPV4_DST)).ip();
Charles Chan5b9df8d2016-03-28 22:21:40 -07001453 if (ipv4Dst.isMulticast()) {
Charles Chan5b9df8d2016-03-28 22:21:40 -07001454 forTableId = MULTICAST_ROUTING_TABLE;
Flavio Castroe10fa242016-01-15 12:43:51 -08001455 } else {
Charles Chan5b9df8d2016-03-28 22:21:40 -07001456 forTableId = UNICAST_ROUTING_TABLE;
Flavio Castroe10fa242016-01-15 12:43:51 -08001457 }
Charles Chan50d900c2018-03-02 13:26:22 -08001458
Charles Chan236653d2018-03-05 11:28:23 -08001459 // TODO decrementing IP ttl is done automatically for routing, this
Charles Chan50d900c2018-03-02 13:26:22 -08001460 // action is ignored or rejected in ofdpa as it is not fully implemented
Charles Chan236653d2018-03-05 11:28:23 -08001461 // if (fwd.treatment() != null) {
1462 // for (Instruction instr : fwd.treatment().allInstructions()) {
1463 // if (instr instanceof L3ModificationInstruction &&
1464 // ((L3ModificationInstruction) instr).subtype() == L3SubType.DEC_TTL) {
1465 // tb.deferred().add(instr);
1466 // }
1467 // }
1468 // }
1469
Pier Ventree0ae7a32016-11-23 09:57:42 -08001470 } else if (ethType.ethType().toShort() == Ethernet.TYPE_IPV6) {
1471 if (buildIpv6Selector(filteredSelector, fwd) < 0) {
1472 return Collections.emptyList();
1473 }
Kalhee Kim3d26bf02017-10-04 14:59:11 +00001474 //We need to set the proper next table
1475 IpPrefix ipv6Dst = ((IPCriterion) selector.getCriterion(Criterion.Type.IPV6_DST)).ip();
1476 if (ipv6Dst.isMulticast()) {
1477 forTableId = MULTICAST_ROUTING_TABLE;
1478 } else {
1479 forTableId = UNICAST_ROUTING_TABLE;
1480 }
1481
Charles Chan236653d2018-03-05 11:28:23 -08001482 // TODO decrementing IP ttl is done automatically for routing, this
Charles Chan50d900c2018-03-02 13:26:22 -08001483 // action is ignored or rejected in ofdpa as it is not fully implemented
Charles Chan236653d2018-03-05 11:28:23 -08001484 // if (fwd.treatment() != null) {
1485 // for (Instruction instr : fwd.treatment().allInstructions()) {
1486 // if (instr instanceof L3ModificationInstruction &&
1487 // ((L3ModificationInstruction) instr).subtype() == L3SubType.DEC_TTL) {
1488 // tb.deferred().add(instr);
1489 // }
1490 // }
1491 // }
Saurav Das8a0732e2015-11-20 15:27:53 -08001492 } else {
1493 filteredSelector
1494 .matchEthType(Ethernet.MPLS_UNICAST)
1495 .matchMplsLabel(((MplsCriterion)
1496 selector.getCriterion(Criterion.Type.MPLS_LABEL)).label());
1497 MplsBosCriterion bos = (MplsBosCriterion) selector
Pier Ventre140a8942016-11-02 07:26:38 -07001498 .getCriterion(MPLS_BOS);
Alex Yashchuk4caa8e82017-12-08 17:40:05 +02001499 if (bos != null && requireMplsBosMatch()) {
Saurav Das8a0732e2015-11-20 15:27:53 -08001500 filteredSelector.matchMplsBos(bos.mplsBos());
1501 }
1502 forTableId = MPLS_TABLE_1;
Saurav Das4ce45962015-11-24 23:21:05 -08001503 log.debug("processing MPLS specific forwarding objective {} -> next:{}"
1504 + " in dev {}", fwd.id(), fwd.nextId(), deviceId);
Saurav Das822c4e22015-10-23 10:51:11 -07001505
Charles Chan14967c22015-12-07 11:11:50 -08001506 if (fwd.treatment() != null) {
1507 for (Instruction instr : fwd.treatment().allInstructions()) {
1508 if (instr instanceof L2ModificationInstruction &&
1509 ((L2ModificationInstruction) instr).subtype() == L2SubType.MPLS_POP) {
Pier Luigi3bfe32c2017-01-30 09:47:36 -08001510 // OF-DPA does not pop in MPLS table in some cases. For the L3 VPN, it requires
Saurav Das9c705342017-01-06 11:36:01 -08001511 // setting the MPLS_TYPE so pop can happen down the pipeline
Alex Yashchuk4caa8e82017-12-08 17:40:05 +02001512 if (requireMplsPop()) {
1513 if (mplsNextTable == MPLS_TYPE_TABLE && isNotMplsBos(selector)) {
1514 tb.immediate().popMpls();
1515 }
1516 } else {
1517 // Skip mpls pop action for mpls_unicast label
1518 if (instr instanceof ModMplsHeaderInstruction &&
1519 !((ModMplsHeaderInstruction) instr).ethernetType()
1520 .equals(EtherType.MPLS_UNICAST.ethType())) {
1521 tb.immediate().add(instr);
1522 }
Pier Luigi3bfe32c2017-01-30 09:47:36 -08001523 }
Charles Chan14967c22015-12-07 11:11:50 -08001524 }
Alex Yashchuk4caa8e82017-12-08 17:40:05 +02001525
1526 if (requireMplsTtlModification()) {
1527 if (instr instanceof L3ModificationInstruction &&
1528 ((L3ModificationInstruction) instr).subtype() == L3SubType.DEC_TTL) {
1529 // FIXME Should modify the app to send the correct DEC_MPLS_TTL instruction
1530 tb.immediate().decMplsTtl();
1531 }
1532 if (instr instanceof L3ModificationInstruction &&
1533 ((L3ModificationInstruction) instr).subtype() == L3SubType.TTL_IN) {
1534 tb.immediate().add(instr);
1535 }
Charles Chan14967c22015-12-07 11:11:50 -08001536 }
Saurav Das8a0732e2015-11-20 15:27:53 -08001537 }
1538 }
1539 }
Saurav Das822c4e22015-10-23 10:51:11 -07001540
1541 if (fwd.nextId() != null) {
Saurav Das423fe2b2015-12-04 10:52:59 -08001542 NextGroup next = getGroupForNextObjective(fwd.nextId());
1543 if (next != null) {
1544 List<Deque<GroupKey>> gkeys = appKryo.deserialize(next.data());
1545 // we only need the top level group's key to point the flow to it
1546 Group group = groupService.getGroup(deviceId, gkeys.get(0).peekFirst());
1547 if (group == null) {
1548 log.warn("Group with key:{} for next-id:{} not found in dev:{}",
1549 gkeys.get(0).peekFirst(), fwd.nextId(), deviceId);
1550 fail(fwd, ObjectiveError.GROUPMISSING);
1551 return Collections.emptySet();
1552 }
Saurav Dasa4020382018-02-14 14:14:54 -08001553 if (isNotMplsBos(selector) && group.type().equals(SELECT)) {
1554 log.warn("SR CONTINUE case cannot be handled as MPLS ECMP "
1555 + "is not implemented in OF-DPA yet. Aborting flow {} -> next:{} "
1556 + "in this device {}", fwd.id(), fwd.nextId(), deviceId);
1557 fail(fwd, ObjectiveError.FLOWINSTALLATIONFAILED);
1558 return Collections.emptySet();
1559 }
Saurav Das423fe2b2015-12-04 10:52:59 -08001560 tb.deferred().group(group.id());
Saurav Dasc568c342018-01-25 09:49:01 -08001561 // retrying flows may be necessary due to bug CORD-554
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001562 if (gkeys.size() == 1 && gkeys.get(0).size() == 1) {
Saurav Dasc568c342018-01-25 09:49:01 -08001563 if (shouldRetry()) {
1564 log.warn("Found empty group 0x{} in dev:{} .. will retry fwd:{}",
1565 Integer.toHexString(group.id().id()), deviceId,
1566 fwd.id());
1567 emptyGroup = true;
1568 }
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001569 }
Saurav Das25190812016-05-27 13:54:07 -07001570 } else {
1571 log.warn("Cannot find group for nextId:{} in dev:{}. Aborting fwd:{}",
1572 fwd.nextId(), deviceId, fwd.id());
1573 fail(fwd, ObjectiveError.FLOWINSTALLATIONFAILED);
1574 return Collections.emptySet();
Saurav Das822c4e22015-10-23 10:51:11 -07001575 }
Saurav Das822c4e22015-10-23 10:51:11 -07001576 }
Charles Chancad338a2016-09-16 18:03:11 -07001577
1578 if (forTableId == MPLS_TABLE_1) {
Pier Ventre140a8942016-11-02 07:26:38 -07001579 if (mplsNextTable == MPLS_L3_TYPE_TABLE) {
Charles Chancad338a2016-09-16 18:03:11 -07001580 Ofdpa3SetMplsType setMplsType = new Ofdpa3SetMplsType(Ofdpa3MplsType.L3_PHP);
Saurav Das9c705342017-01-06 11:36:01 -08001581 // set mpls type as apply_action
1582 tb.immediate().extension(setMplsType, deviceId);
Charles Chancad338a2016-09-16 18:03:11 -07001583 }
1584 tb.transition(mplsNextTable);
1585 } else {
1586 tb.transition(ACL_TABLE);
1587 }
1588
Andrea Campanellad980c6d2018-04-30 11:48:55 +02001589 if (fwd.treatment() != null && fwd.treatment().clearedDeferred()) {
1590 if (supportsUnicastBlackHole()) {
1591 tb.wipeDeferred();
1592 } else {
1593 log.warn("Clear Deferred is not supported Unicast Routing Table on device {}", deviceId);
1594 return Collections.emptySet();
1595 }
1596 }
1597
Saurav Das822c4e22015-10-23 10:51:11 -07001598 FlowRule.Builder ruleBuilder = DefaultFlowRule.builder()
1599 .fromApp(fwd.appId())
1600 .withPriority(fwd.priority())
1601 .forDevice(deviceId)
Saurav Das8a0732e2015-11-20 15:27:53 -08001602 .withSelector(filteredSelector.build())
1603 .withTreatment(tb.build())
1604 .forTable(forTableId);
Saurav Das822c4e22015-10-23 10:51:11 -07001605
1606 if (fwd.permanent()) {
1607 ruleBuilder.makePermanent();
1608 } else {
1609 ruleBuilder.makeTemporary(fwd.timeout());
1610 }
Flavio Castroe10fa242016-01-15 12:43:51 -08001611 Collection<FlowRule> flowRuleCollection = new ArrayList<>();
1612 flowRuleCollection.add(ruleBuilder.build());
Pier Luigi3bfe32c2017-01-30 09:47:36 -08001613 if (!allowDefaultRoute) {
Pier Ventree0ae7a32016-11-23 09:57:42 -08001614 flowRuleCollection.add(
1615 defaultRoute(fwd, complementarySelector, forTableId, tb)
1616 );
Flavio Castroe10fa242016-01-15 12:43:51 -08001617 log.debug("Default rule 0.0.0.0/0 is being installed two rules");
1618 }
Saurav Dasc568c342018-01-25 09:49:01 -08001619
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001620 if (emptyGroup) {
pier8b3aef42019-03-11 15:14:02 -07001621 retryExecutorService.schedule(new RetryFlows(fwd, flowRuleCollection),
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001622 RETRY_MS, TimeUnit.MILLISECONDS);
1623 }
Flavio Castroe10fa242016-01-15 12:43:51 -08001624 return flowRuleCollection;
Saurav Das822c4e22015-10-23 10:51:11 -07001625 }
1626
Charles Chan50d900c2018-03-02 13:26:22 -08001627 private int buildIpv4Selector(TrafficSelector.Builder builderToUpdate,
Pier Luigi3bfe32c2017-01-30 09:47:36 -08001628 TrafficSelector.Builder extBuilder,
1629 ForwardingObjective fwd,
1630 boolean allowDefaultRoute) {
1631 TrafficSelector selector = fwd.selector();
1632
1633 IpPrefix ipv4Dst = ((IPCriterion) selector.getCriterion(Criterion.Type.IPV4_DST)).ip();
1634 if (ipv4Dst.isMulticast()) {
1635 if (ipv4Dst.prefixLength() != 32) {
1636 log.warn("Multicast specific forwarding objective can only be /32");
1637 fail(fwd, ObjectiveError.BADPARAMS);
1638 return -1;
1639 }
1640 VlanId assignedVlan = readVlanFromSelector(fwd.meta());
1641 if (assignedVlan == null) {
1642 log.warn("VLAN ID required by multicast specific fwd obj is missing. Abort.");
1643 fail(fwd, ObjectiveError.BADPARAMS);
1644 return -1;
1645 }
Charles Chand1172632017-03-15 17:33:09 -07001646 if (requireVlanExtensions()) {
1647 OfdpaMatchVlanVid ofdpaMatchVlanVid = new OfdpaMatchVlanVid(assignedVlan);
1648 builderToUpdate.extension(ofdpaMatchVlanVid, deviceId);
1649 } else {
1650 builderToUpdate.matchVlanId(assignedVlan);
1651 }
Pier Luigi3bfe32c2017-01-30 09:47:36 -08001652 builderToUpdate.matchEthType(Ethernet.TYPE_IPV4).matchIPDst(ipv4Dst);
1653 log.debug("processing IPv4 multicast specific forwarding objective {} -> next:{}"
1654 + " in dev:{}", fwd.id(), fwd.nextId(), deviceId);
1655 } else {
1656 if (ipv4Dst.prefixLength() == 0) {
1657 if (allowDefaultRoute) {
1658 // The entire IPV4_DST field is wildcarded intentionally
1659 builderToUpdate.matchEthType(Ethernet.TYPE_IPV4);
1660 } else {
1661 // NOTE: The switch does not support matching 0.0.0.0/0
1662 // Split it into 0.0.0.0/1 and 128.0.0.0/1
1663 builderToUpdate.matchEthType(Ethernet.TYPE_IPV4)
1664 .matchIPDst(IpPrefix.valueOf("0.0.0.0/1"));
1665 extBuilder.matchEthType(Ethernet.TYPE_IPV4)
1666 .matchIPDst(IpPrefix.valueOf("128.0.0.0/1"));
1667 }
1668 } else {
1669 builderToUpdate.matchEthType(Ethernet.TYPE_IPV4).matchIPDst(ipv4Dst);
1670 }
1671 log.debug("processing IPv4 unicast specific forwarding objective {} -> next:{}"
1672 + " in dev:{}", fwd.id(), fwd.nextId(), deviceId);
1673 }
1674 return 0;
1675 }
1676
1677 /**
1678 * Helper method to build Ipv6 selector using the selector provided by
1679 * a forwarding objective.
1680 *
1681 * @param builderToUpdate the builder to update
1682 * @param fwd the selector to read
1683 * @return 0 if the update ends correctly. -1 if the matches
1684 * are not yet supported
1685 */
Charles Chan50d900c2018-03-02 13:26:22 -08001686 int buildIpv6Selector(TrafficSelector.Builder builderToUpdate,
Pier Luigi3bfe32c2017-01-30 09:47:36 -08001687 ForwardingObjective fwd) {
1688
1689 TrafficSelector selector = fwd.selector();
1690
1691 IpPrefix ipv6Dst = ((IPCriterion) selector.getCriterion(Criterion.Type.IPV6_DST)).ip();
1692 if (ipv6Dst.isMulticast()) {
Julia Ferguson65428c32017-08-10 18:15:24 +00001693 if (ipv6Dst.prefixLength() != IpAddress.INET6_BIT_LENGTH) {
1694 log.warn("Multicast specific forwarding objective can only be /128");
1695 fail(fwd, ObjectiveError.BADPARAMS);
1696 return -1;
1697 }
1698 VlanId assignedVlan = readVlanFromSelector(fwd.meta());
1699 if (assignedVlan == null) {
1700 log.warn("VLAN ID required by multicast specific fwd obj is missing. Abort.");
1701 fail(fwd, ObjectiveError.BADPARAMS);
1702 return -1;
1703 }
1704 if (requireVlanExtensions()) {
1705 OfdpaMatchVlanVid ofdpaMatchVlanVid = new OfdpaMatchVlanVid(assignedVlan);
1706 builderToUpdate.extension(ofdpaMatchVlanVid, deviceId);
1707 } else {
1708 builderToUpdate.matchVlanId(assignedVlan);
1709 }
1710 builderToUpdate.matchEthType(Ethernet.TYPE_IPV6).matchIPv6Dst(ipv6Dst);
1711 log.debug("processing IPv6 multicast specific forwarding objective {} -> next:{}"
1712 + " in dev:{}", fwd.id(), fwd.nextId(), deviceId);
1713 } else {
1714 if (ipv6Dst.prefixLength() != 0) {
1715 builderToUpdate.matchIPv6Dst(ipv6Dst);
1716 }
Pier Luigi3bfe32c2017-01-30 09:47:36 -08001717 builderToUpdate.matchEthType(Ethernet.TYPE_IPV6);
1718 log.debug("processing IPv6 unicast specific forwarding objective {} -> next:{}"
1719 + " in dev:{}", fwd.id(), fwd.nextId(), deviceId);
Julia Ferguson65428c32017-08-10 18:15:24 +00001720 }
Pier Luigi3bfe32c2017-01-30 09:47:36 -08001721 return 0;
1722 }
1723
Charles Chan50d900c2018-03-02 13:26:22 -08001724 FlowRule defaultRoute(ForwardingObjective fwd,
Pier Ventree0ae7a32016-11-23 09:57:42 -08001725 TrafficSelector.Builder complementarySelector,
1726 int forTableId,
1727 TrafficTreatment.Builder tb) {
1728 FlowRule.Builder rule = DefaultFlowRule.builder()
1729 .fromApp(fwd.appId())
1730 .withPriority(fwd.priority())
1731 .forDevice(deviceId)
1732 .withSelector(complementarySelector.build())
1733 .withTreatment(tb.build())
1734 .forTable(forTableId);
1735 if (fwd.permanent()) {
1736 rule.makePermanent();
1737 } else {
1738 rule.makeTemporary(fwd.timeout());
1739 }
1740 return rule.build();
1741 }
1742
Saurav Das4ce45962015-11-24 23:21:05 -08001743 /**
1744 * Handles forwarding rules to the L2 bridging table. Flow actions are not
1745 * allowed in the bridging table - instead we use L2 Interface group or
1746 * L2 flood group
1747 *
1748 * @param fwd the forwarding objective
1749 * @return A collection of flow rules, or an empty set
1750 */
1751 protected Collection<FlowRule> processEthDstSpecific(ForwardingObjective fwd) {
1752 List<FlowRule> rules = new ArrayList<>();
1753
1754 // Build filtered selector
1755 TrafficSelector selector = fwd.selector();
1756 EthCriterion ethCriterion = (EthCriterion) selector
1757 .getCriterion(Criterion.Type.ETH_DST);
1758 VlanIdCriterion vlanIdCriterion = (VlanIdCriterion) selector
1759 .getCriterion(Criterion.Type.VLAN_VID);
1760
1761 if (vlanIdCriterion == null) {
1762 log.warn("Forwarding objective for bridging requires vlan. Not "
1763 + "installing fwd:{} in dev:{}", fwd.id(), deviceId);
1764 fail(fwd, ObjectiveError.BADPARAMS);
1765 return Collections.emptySet();
1766 }
1767
1768 TrafficSelector.Builder filteredSelectorBuilder =
1769 DefaultTrafficSelector.builder();
Yi Tseng3a77b4f2017-02-06 15:02:17 -08001770
1771 if (!ethCriterion.mac().equals(NONE) &&
1772 !ethCriterion.mac().equals(BROADCAST)) {
Saurav Das4ce45962015-11-24 23:21:05 -08001773 filteredSelectorBuilder.matchEthDst(ethCriterion.mac());
1774 log.debug("processing L2 forwarding objective:{} -> next:{} in dev:{}",
1775 fwd.id(), fwd.nextId(), deviceId);
1776 } else {
Yi Tseng3a77b4f2017-02-06 15:02:17 -08001777 // Use wildcard DST_MAC if the MacAddress is None or Broadcast
Saurav Das4ce45962015-11-24 23:21:05 -08001778 log.debug("processing L2 Broadcast forwarding objective:{} -> next:{} "
1779 + "in dev:{} for vlan:{}",
1780 fwd.id(), fwd.nextId(), deviceId, vlanIdCriterion.vlanId());
1781 }
Charles Chand1172632017-03-15 17:33:09 -07001782 if (requireVlanExtensions()) {
1783 OfdpaMatchVlanVid ofdpaMatchVlanVid = new OfdpaMatchVlanVid(vlanIdCriterion.vlanId());
1784 filteredSelectorBuilder.extension(ofdpaMatchVlanVid, deviceId);
1785 } else {
1786 filteredSelectorBuilder.matchVlanId(vlanIdCriterion.vlanId());
1787 }
Saurav Das4ce45962015-11-24 23:21:05 -08001788 TrafficSelector filteredSelector = filteredSelectorBuilder.build();
1789
1790 if (fwd.treatment() != null) {
1791 log.warn("Ignoring traffic treatment in fwd rule {} meant for L2 table"
1792 + "for dev:{}. Expecting only nextId", fwd.id(), deviceId);
1793 }
1794
1795 TrafficTreatment.Builder treatmentBuilder = DefaultTrafficTreatment.builder();
1796 if (fwd.nextId() != null) {
Saurav Das423fe2b2015-12-04 10:52:59 -08001797 NextGroup next = getGroupForNextObjective(fwd.nextId());
Saurav Das4ce45962015-11-24 23:21:05 -08001798 if (next != null) {
1799 List<Deque<GroupKey>> gkeys = appKryo.deserialize(next.data());
1800 // we only need the top level group's key to point the flow to it
1801 Group group = groupService.getGroup(deviceId, gkeys.get(0).peekFirst());
1802 if (group != null) {
1803 treatmentBuilder.deferred().group(group.id());
1804 } else {
1805 log.warn("Group with key:{} for next-id:{} not found in dev:{}",
1806 gkeys.get(0).peekFirst(), fwd.nextId(), deviceId);
1807 fail(fwd, ObjectiveError.GROUPMISSING);
1808 return Collections.emptySet();
1809 }
1810 }
1811 }
1812 treatmentBuilder.immediate().transition(ACL_TABLE);
1813 TrafficTreatment filteredTreatment = treatmentBuilder.build();
1814
1815 // Build bridging table entries
1816 FlowRule.Builder flowRuleBuilder = DefaultFlowRule.builder();
1817 flowRuleBuilder.fromApp(fwd.appId())
1818 .withPriority(fwd.priority())
1819 .forDevice(deviceId)
1820 .withSelector(filteredSelector)
1821 .withTreatment(filteredTreatment)
1822 .forTable(BRIDGING_TABLE);
1823 if (fwd.permanent()) {
1824 flowRuleBuilder.makePermanent();
1825 } else {
1826 flowRuleBuilder.makeTemporary(fwd.timeout());
1827 }
1828 rules.add(flowRuleBuilder.build());
1829 return rules;
1830 }
1831
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001832 //////////////////////////////////////
1833 // Helper Methods and Classes
1834 //////////////////////////////////////
1835
1836 private boolean isSupportedEthTypeObjective(ForwardingObjective fwd) {
1837 TrafficSelector selector = fwd.selector();
1838 EthTypeCriterion ethType = (EthTypeCriterion) selector
1839 .getCriterion(Criterion.Type.ETH_TYPE);
1840 return !((ethType == null) ||
1841 ((ethType.ethType().toShort() != Ethernet.TYPE_IPV4) &&
Pier Ventree0ae7a32016-11-23 09:57:42 -08001842 (ethType.ethType().toShort() != Ethernet.MPLS_UNICAST)) &&
1843 (ethType.ethType().toShort() != Ethernet.TYPE_IPV6));
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001844 }
1845
1846 private boolean isSupportedEthDstObjective(ForwardingObjective fwd) {
1847 TrafficSelector selector = fwd.selector();
1848 EthCriterion ethDst = (EthCriterion) selector
1849 .getCriterion(Criterion.Type.ETH_DST);
1850 VlanIdCriterion vlanId = (VlanIdCriterion) selector
1851 .getCriterion(Criterion.Type.VLAN_VID);
1852 return !(ethDst == null && vlanId == null);
1853 }
1854
Charles Chan50d900c2018-03-02 13:26:22 -08001855 NextGroup getGroupForNextObjective(Integer nextId) {
Saurav Das423fe2b2015-12-04 10:52:59 -08001856 NextGroup next = flowObjectiveStore.getNextGroup(nextId);
1857 if (next != null) {
1858 List<Deque<GroupKey>> gkeys = appKryo.deserialize(next.data());
1859 if (gkeys != null && !gkeys.isEmpty()) {
1860 return next;
1861 } else {
1862 log.warn("Empty next group found in FlowObjective store for "
1863 + "next-id:{} in dev:{}", nextId, deviceId);
1864 }
1865 } else {
1866 log.warn("next-id {} not found in Flow objective store for dev:{}",
1867 nextId, deviceId);
1868 }
1869 return null;
1870 }
1871
Saurav Das24431192016-03-07 19:13:00 -08001872 @Override
1873 public List<String> getNextMappings(NextGroup nextGroup) {
1874 List<String> mappings = new ArrayList<>();
1875 List<Deque<GroupKey>> gkeys = appKryo.deserialize(nextGroup.data());
1876 for (Deque<GroupKey> gkd : gkeys) {
1877 Group lastGroup = null;
Yuta HIGUCHI2dce08a2017-04-20 21:57:48 -07001878 StringBuilder gchain = new StringBuilder();
Saurav Das24431192016-03-07 19:13:00 -08001879 for (GroupKey gk : gkd) {
1880 Group g = groupService.getGroup(deviceId, gk);
Saurav Das8be4e3a2016-03-11 17:19:07 -08001881 if (g == null) {
Saurav Das25190812016-05-27 13:54:07 -07001882 gchain.append(" NoGrp").append(" -->");
Saurav Das8be4e3a2016-03-11 17:19:07 -08001883 continue;
1884 }
1885 gchain.append(" 0x").append(Integer.toHexString(g.id().id()))
1886 .append(" -->");
Saurav Das24431192016-03-07 19:13:00 -08001887 lastGroup = g;
1888 }
1889 // add port information for last group in group-chain
Yuta HIGUCHI2dce08a2017-04-20 21:57:48 -07001890 List<Instruction> lastGroupIns = new ArrayList<>();
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001891 if (lastGroup != null && !lastGroup.buckets().buckets().isEmpty()) {
Saurav Das25190812016-05-27 13:54:07 -07001892 lastGroupIns = lastGroup.buckets().buckets().get(0)
1893 .treatment().allInstructions();
1894 }
1895 for (Instruction i: lastGroupIns) {
Saurav Das24431192016-03-07 19:13:00 -08001896 if (i instanceof OutputInstruction) {
Saurav Das8be4e3a2016-03-11 17:19:07 -08001897 gchain.append(" port:").append(((OutputInstruction) i).port());
Saurav Das24431192016-03-07 19:13:00 -08001898 }
1899 }
Saurav Das8be4e3a2016-03-11 17:19:07 -08001900 mappings.add(gchain.toString());
Saurav Das24431192016-03-07 19:13:00 -08001901 }
1902 return mappings;
1903 }
Charles Chan5b9df8d2016-03-28 22:21:40 -07001904
Saurav Dasa4020382018-02-14 14:14:54 -08001905 /**
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001906 * Utility class that retries sending flows a fixed number of times, even if
1907 * some of the attempts are successful. Used only for forwarding objectives.
1908 */
Alex Yashchuk4caa8e82017-12-08 17:40:05 +02001909 public final class RetryFlows implements Runnable {
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001910 int attempts = MAX_RETRY_ATTEMPTS;
1911 private Collection<FlowRule> retryFlows;
1912 private ForwardingObjective fwd;
1913
Charles Chan50d900c2018-03-02 13:26:22 -08001914 RetryFlows(ForwardingObjective fwd, Collection<FlowRule> retryFlows) {
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001915 this.fwd = fwd;
1916 this.retryFlows = retryFlows;
1917 }
1918
1919 @Override
1920 public void run() {
1921 log.info("RETRY FLOWS ATTEMPT# {} for fwd:{} rules:{}",
1922 MAX_RETRY_ATTEMPTS - attempts, fwd.id(), retryFlows.size());
pier8b3aef42019-03-11 15:14:02 -07001923 sendForwards(Collections.singletonList(Pair.of(fwd, retryFlows)));
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001924 if (--attempts > 0) {
pier8b3aef42019-03-11 15:14:02 -07001925 retryExecutorService.schedule(this, RETRY_MS, TimeUnit.MILLISECONDS);
1926 }
1927 }
1928 }
1929
1930 // Flow rules accumulator for reducing the number of transactions required to the devices.
1931 private final class ForwardingObjectiveAccumulator
1932 extends AbstractAccumulator<Pair<ForwardingObjective, Collection<FlowRule>>> {
1933
1934 ForwardingObjectiveAccumulator(int maxFwd, int maxBatchMS, int maxIdleMS) {
1935 super(TIMER, maxFwd, maxBatchMS, maxIdleMS);
1936 }
1937
1938 @Override
1939 public void processItems(List<Pair<ForwardingObjective, Collection<FlowRule>>> pairs) {
1940 // Triggers creation of a batch using the list of flowrules generated from fwdobjs.
1941 accumulatorExecutorService.execute(new FlowRulesBuilderTask(pairs));
1942 }
1943 }
1944
1945 // Task for building batch of flow rules in a separate thread.
1946 private final class FlowRulesBuilderTask implements Runnable {
1947 private final List<Pair<ForwardingObjective, Collection<FlowRule>>> pairs;
1948
1949 FlowRulesBuilderTask(List<Pair<ForwardingObjective, Collection<FlowRule>>> pairs) {
1950 this.pairs = pairs;
1951 }
1952
1953 @Override
1954 public void run() {
1955 try {
1956 sendForwards(pairs);
1957 } catch (Exception e) {
1958 log.warn("Unable to send forwards", e);
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001959 }
1960 }
1961 }
1962
Saurav Das822c4e22015-10-23 10:51:11 -07001963}