blob: b80919b7ca46f6726a10a510b9a0e8b3804a853d [file] [log] [blame]
Saurav Das822c4e22015-10-23 10:51:11 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2016-present Open Networking Foundation
Saurav Das822c4e22015-10-23 10:51:11 -07003 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
Yi Tsengef19de12017-04-24 11:33:05 -070016package org.onosproject.driver.pipeline.ofdpa;
Saurav Das822c4e22015-10-23 10:51:11 -070017
Charles Chan5270ed02016-01-30 23:22:37 -080018import com.google.common.collect.ImmutableList;
pier8b3aef42019-03-11 15:14:02 -070019import com.google.common.collect.Lists;
Yi Tseng47f82dc2017-03-05 22:48:39 -080020import com.google.common.collect.Sets;
pier8b3aef42019-03-11 15:14:02 -070021import org.apache.commons.lang3.tuple.Pair;
Saurav Das822c4e22015-10-23 10:51:11 -070022import org.onlab.osgi.ServiceDirectory;
Jonghwan Hyun800d9d02018-04-09 09:40:50 -070023import org.onlab.packet.EthType;
Saurav Das822c4e22015-10-23 10:51:11 -070024import org.onlab.packet.Ethernet;
Alex Yashchuk4caa8e82017-12-08 17:40:05 +020025import org.onlab.packet.EthType.EtherType;
Julia Ferguson65428c32017-08-10 18:15:24 +000026import org.onlab.packet.IpAddress;
Charles Chan14967c22015-12-07 11:11:50 -080027import org.onlab.packet.IpPrefix;
Charles Chan45b69ab2018-03-02 15:41:41 -080028import org.onlab.packet.MacAddress;
Saurav Das822c4e22015-10-23 10:51:11 -070029import org.onlab.packet.VlanId;
pier8b3aef42019-03-11 15:14:02 -070030import org.onlab.util.AbstractAccumulator;
31import org.onlab.util.Accumulator;
Saurav Das822c4e22015-10-23 10:51:11 -070032import org.onlab.util.KryoNamespace;
33import org.onosproject.core.ApplicationId;
34import org.onosproject.core.CoreService;
Jonghwan Hyun800d9d02018-04-09 09:40:50 -070035import org.onosproject.driver.extensions.Ofdpa3CopyField;
Charles Chancad338a2016-09-16 18:03:11 -070036import org.onosproject.driver.extensions.Ofdpa3MplsType;
37import org.onosproject.driver.extensions.Ofdpa3SetMplsType;
Jonghwan Hyun800d9d02018-04-09 09:40:50 -070038import org.onosproject.driver.extensions.OfdpaMatchActsetOutput;
39import org.onosproject.driver.extensions.OfdpaMatchAllowVlanTranslation;
Charles Chan14967c22015-12-07 11:11:50 -080040import org.onosproject.driver.extensions.OfdpaMatchVlanVid;
41import org.onosproject.driver.extensions.OfdpaSetVlanVid;
Saurav Das822c4e22015-10-23 10:51:11 -070042import org.onosproject.net.DeviceId;
43import org.onosproject.net.Port;
44import org.onosproject.net.PortNumber;
45import org.onosproject.net.behaviour.NextGroup;
46import org.onosproject.net.behaviour.Pipeliner;
47import org.onosproject.net.behaviour.PipelinerContext;
48import org.onosproject.net.device.DeviceService;
49import org.onosproject.net.driver.AbstractHandlerBehaviour;
50import org.onosproject.net.flow.DefaultFlowRule;
51import org.onosproject.net.flow.DefaultTrafficSelector;
52import org.onosproject.net.flow.DefaultTrafficTreatment;
53import org.onosproject.net.flow.FlowRule;
54import org.onosproject.net.flow.FlowRuleOperations;
55import org.onosproject.net.flow.FlowRuleOperationsContext;
56import org.onosproject.net.flow.FlowRuleService;
57import org.onosproject.net.flow.TrafficSelector;
58import org.onosproject.net.flow.TrafficTreatment;
59import org.onosproject.net.flow.criteria.Criteria;
60import org.onosproject.net.flow.criteria.Criterion;
61import org.onosproject.net.flow.criteria.EthCriterion;
62import org.onosproject.net.flow.criteria.EthTypeCriterion;
63import org.onosproject.net.flow.criteria.IPCriterion;
Pier Ventree0ae7a32016-11-23 09:57:42 -080064import org.onosproject.net.flow.criteria.Icmpv6CodeCriterion;
65import org.onosproject.net.flow.criteria.Icmpv6TypeCriterion;
Saurav Das8a0732e2015-11-20 15:27:53 -080066import org.onosproject.net.flow.criteria.MplsBosCriterion;
67import org.onosproject.net.flow.criteria.MplsCriterion;
Saurav Das822c4e22015-10-23 10:51:11 -070068import org.onosproject.net.flow.criteria.PortCriterion;
Charles Chand9e47c62017-10-05 15:17:15 -070069import org.onosproject.net.flow.criteria.TcpPortCriterion;
70import org.onosproject.net.flow.criteria.UdpPortCriterion;
Saurav Das822c4e22015-10-23 10:51:11 -070071import org.onosproject.net.flow.criteria.VlanIdCriterion;
72import org.onosproject.net.flow.instructions.Instruction;
73import org.onosproject.net.flow.instructions.Instructions.OutputInstruction;
Charles Chanf76de302018-06-15 18:54:18 -070074import org.onosproject.net.flow.instructions.Instructions.NoActionInstruction;
Saurav Das822c4e22015-10-23 10:51:11 -070075import org.onosproject.net.flow.instructions.L2ModificationInstruction;
Saurav Das8a0732e2015-11-20 15:27:53 -080076import org.onosproject.net.flow.instructions.L2ModificationInstruction.L2SubType;
Alex Yashchuk4caa8e82017-12-08 17:40:05 +020077import org.onosproject.net.flow.instructions.L2ModificationInstruction.ModMplsHeaderInstruction;
Charles Chan14967c22015-12-07 11:11:50 -080078import org.onosproject.net.flow.instructions.L3ModificationInstruction;
79import org.onosproject.net.flow.instructions.L3ModificationInstruction.L3SubType;
Saurav Das822c4e22015-10-23 10:51:11 -070080import org.onosproject.net.flowobjective.FilteringObjective;
81import org.onosproject.net.flowobjective.FlowObjectiveStore;
82import org.onosproject.net.flowobjective.ForwardingObjective;
83import org.onosproject.net.flowobjective.NextObjective;
84import org.onosproject.net.flowobjective.Objective;
85import org.onosproject.net.flowobjective.ObjectiveError;
Saurav Das822c4e22015-10-23 10:51:11 -070086import org.onosproject.net.group.DefaultGroupKey;
87import org.onosproject.net.group.Group;
Saurav Das822c4e22015-10-23 10:51:11 -070088import org.onosproject.net.group.GroupKey;
Saurav Das822c4e22015-10-23 10:51:11 -070089import org.onosproject.net.group.GroupService;
Saurav Das822c4e22015-10-23 10:51:11 -070090import org.onosproject.store.serializers.KryoNamespaces;
91import org.slf4j.Logger;
92
Pier Ventree0ae7a32016-11-23 09:57:42 -080093import java.util.ArrayDeque;
94import java.util.ArrayList;
95import java.util.Collection;
96import java.util.Collections;
97import java.util.Deque;
98import java.util.List;
99import java.util.Objects;
Jonghwan Hyun800d9d02018-04-09 09:40:50 -0700100import java.util.Optional;
pier8b3aef42019-03-11 15:14:02 -0700101import java.util.Timer;
Pier Ventree0ae7a32016-11-23 09:57:42 -0800102import java.util.concurrent.ScheduledExecutorService;
103import java.util.concurrent.TimeUnit;
Harshada Chaundkar77958a52019-08-05 15:33:30 +0000104import java.util.concurrent.atomic.AtomicBoolean;
Pier Ventree0ae7a32016-11-23 09:57:42 -0800105
106import static java.util.concurrent.Executors.newScheduledThreadPool;
pier8b3aef42019-03-11 15:14:02 -0700107import static java.util.concurrent.Executors.newSingleThreadScheduledExecutor;
Yi Tseng3a77b4f2017-02-06 15:02:17 -0800108import static org.onlab.packet.MacAddress.BROADCAST;
Charles Chanb4879a52017-10-20 19:09:16 -0700109import static org.onlab.packet.MacAddress.IPV4_MULTICAST;
110import static org.onlab.packet.MacAddress.IPV6_MULTICAST;
Yi Tseng3a77b4f2017-02-06 15:02:17 -0800111import static org.onlab.packet.MacAddress.NONE;
Pier Ventree0ae7a32016-11-23 09:57:42 -0800112import static org.onlab.util.Tools.groupedThreads;
Jonghwan Hyun800d9d02018-04-09 09:40:50 -0700113import static org.onosproject.driver.extensions.Ofdpa3CopyField.OXM_ID_PACKET_REG_1;
114import static org.onosproject.driver.extensions.Ofdpa3CopyField.OXM_ID_VLAN_VID;
Yi Tsengef19de12017-04-24 11:33:05 -0700115import static org.onosproject.driver.pipeline.ofdpa.OfdpaGroupHandlerUtility.*;
pier9469f3e2019-04-17 17:05:08 +0200116import static org.onosproject.driver.pipeline.ofdpa.OfdpaPipelineUtility.*;
pier7afc7522019-05-10 13:19:15 +0200117import static org.onosproject.net.flowobjective.ForwardingObjective.Flag.SPECIFIC;
Pier Luigi075f1012018-02-01 10:23:12 +0100118import static org.onosproject.net.group.GroupDescription.Type.SELECT;
Pier Ventree0ae7a32016-11-23 09:57:42 -0800119import static org.slf4j.LoggerFactory.getLogger;
Pier Ventre140a8942016-11-02 07:26:38 -0700120import static org.onosproject.net.flow.criteria.Criterion.Type.MPLS_BOS;
Pier Ventree0ae7a32016-11-23 09:57:42 -0800121
Saurav Das822c4e22015-10-23 10:51:11 -0700122/**
123 * Driver for Broadcom's OF-DPA v2.0 TTP.
Saurav Das822c4e22015-10-23 10:51:11 -0700124 */
Charles Chan361154b2016-03-24 10:23:39 -0700125public class Ofdpa2Pipeline extends AbstractHandlerBehaviour implements Pipeliner {
pier8b3aef42019-03-11 15:14:02 -0700126 // Timer for the accumulator
127 private static final Timer TIMER = new Timer("fwdobj-batching");
128 private Accumulator<Pair<ForwardingObjective, Collection<FlowRule>>> accumulator;
pier9469f3e2019-04-17 17:05:08 +0200129 // Internal objects
Saurav Das822c4e22015-10-23 10:51:11 -0700130 private final Logger log = getLogger(getClass());
Charles Chan425854b2016-04-11 15:32:12 -0700131 protected ServiceDirectory serviceDirectory;
Saurav Das822c4e22015-10-23 10:51:11 -0700132 protected FlowRuleService flowRuleService;
Charles Chan425854b2016-04-11 15:32:12 -0700133 protected CoreService coreService;
Saurav Das8a0732e2015-11-20 15:27:53 -0800134 protected GroupService groupService;
135 protected FlowObjectiveStore flowObjectiveStore;
Saurav Das822c4e22015-10-23 10:51:11 -0700136 protected DeviceId deviceId;
137 protected ApplicationId driverId;
Saurav Das822c4e22015-10-23 10:51:11 -0700138 protected DeviceService deviceService;
Charles Chan188ebf52015-12-23 00:15:11 -0800139 protected static KryoNamespace appKryo = new KryoNamespace.Builder()
Yi Tsengef19de12017-04-24 11:33:05 -0700140 .register(KryoNamespaces.API)
141 .register(GroupKey.class)
142 .register(DefaultGroupKey.class)
143 .register(OfdpaNextGroup.class)
144 .register(ArrayDeque.class)
145 .build("Ofdpa2Pipeline");
Saurav Das822c4e22015-10-23 10:51:11 -0700146
Charles Chan425854b2016-04-11 15:32:12 -0700147 protected Ofdpa2GroupHandler groupHandler;
Saurav Das822c4e22015-10-23 10:51:11 -0700148
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700149 // flows installations to be retried
pier8b3aef42019-03-11 15:14:02 -0700150 private ScheduledExecutorService retryExecutorService
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700151 = newScheduledThreadPool(5, groupedThreads("OfdpaPipeliner", "retry-%d", log));
Saurav Das4f980082015-11-05 13:39:15 -0800152
pier8b3aef42019-03-11 15:14:02 -0700153 // accumulator executor service
154 private ScheduledExecutorService accumulatorExecutorService
155 = newSingleThreadScheduledExecutor(groupedThreads("OfdpaPipeliner", "acc-%d", log));
156
Saurav Das822c4e22015-10-23 10:51:11 -0700157 @Override
158 public void init(DeviceId deviceId, PipelinerContext context) {
Saurav Das822c4e22015-10-23 10:51:11 -0700159 this.deviceId = deviceId;
160
Charles Chan425854b2016-04-11 15:32:12 -0700161 serviceDirectory = context.directory();
Saurav Das822c4e22015-10-23 10:51:11 -0700162 coreService = serviceDirectory.get(CoreService.class);
163 flowRuleService = serviceDirectory.get(FlowRuleService.class);
164 groupService = serviceDirectory.get(GroupService.class);
165 flowObjectiveStore = context.store();
Saurav Das822c4e22015-10-23 10:51:11 -0700166 deviceService = serviceDirectory.get(DeviceService.class);
pier8b3aef42019-03-11 15:14:02 -0700167 // Init the accumulator, if enabled
pier9469f3e2019-04-17 17:05:08 +0200168 if (isAccumulatorEnabled(this)) {
pier8b3aef42019-03-11 15:14:02 -0700169 accumulator = new ForwardingObjectiveAccumulator(context.accumulatorMaxObjectives(),
170 context.accumulatorMaxBatchMillis(),
171 context.accumulatorMaxIdleMillis());
172 }
Saurav Das822c4e22015-10-23 10:51:11 -0700173
Charles Chan40132b32017-01-22 00:19:37 -0800174 initDriverId();
175 initGroupHander(context);
Saurav Das822c4e22015-10-23 10:51:11 -0700176
Saurav Das822c4e22015-10-23 10:51:11 -0700177 initializePipeline();
Saurav Das822c4e22015-10-23 10:51:11 -0700178 }
179
pier7afc7522019-05-10 13:19:15 +0200180 void setupAccumulatorForTests(int maxFwd, int maxBatchMS, int maxIdleMS) {
181 if (accumulator == null) {
182 accumulator = new ForwardingObjectiveAccumulator(maxFwd,
183 maxBatchMS,
184 maxIdleMS);
185 }
186 }
187
Charles Chan40132b32017-01-22 00:19:37 -0800188 protected void initDriverId() {
189 driverId = coreService.registerApplication(
190 "org.onosproject.driver.Ofdpa2Pipeline");
191 }
192
193 protected void initGroupHander(PipelinerContext context) {
194 groupHandler = new Ofdpa2GroupHandler();
195 groupHandler.init(deviceId, context);
196 }
197
Saurav Das822c4e22015-10-23 10:51:11 -0700198 protected void initializePipeline() {
Charles Chan188ebf52015-12-23 00:15:11 -0800199 // OF-DPA does not require initializing the pipeline as it puts default
200 // rules automatically in the hardware. However emulation of OFDPA in
201 // software switches does require table-miss-entries.
Saurav Das822c4e22015-10-23 10:51:11 -0700202 }
203
Charles Chand1172632017-03-15 17:33:09 -0700204 /**
Alex Yashchuk4caa8e82017-12-08 17:40:05 +0200205 * Determines whether this pipeline requires MPLS POP instruction.
206 *
207 * @return true to use MPLS POP instruction
208 */
209 public boolean requireMplsPop() {
210 return true;
211 }
212
213 /**
Harshada Chaundkar77958a52019-08-05 15:33:30 +0000214 * Determines whether this pipeline requires one additional flow matching on ethType 0x86dd in ACL table.
215 *
216 * @return true to create one additional flow matching on ethType 0x86dd in ACL table
217 */
218 protected boolean requireEthType() {
219 return true;
220 }
221
222 /**
Alex Yashchuk4caa8e82017-12-08 17:40:05 +0200223 * Determines whether this pipeline requires MPLS BOS match.
224 *
225 * @return true to use MPLS BOS match
226 */
227 public boolean requireMplsBosMatch() {
228 return true;
229 }
230
231 /**
232 * Determines whether this pipeline requires MPLS TTL decrement and copy.
233 *
234 * @return true to use MPLS TTL decrement and copy
235 */
236 public boolean requireMplsTtlModification() {
237 return true;
238 }
239
240 /**
Charles Chand1172632017-03-15 17:33:09 -0700241 * Determines whether this pipeline requires OFDPA match and set VLAN extensions.
242 *
243 * @return true to use the extensions
244 */
245 protected boolean requireVlanExtensions() {
246 return true;
247 }
248
Saurav Das86d13e82017-04-28 17:03:48 -0700249 /**
250 * Determines whether in-port should be matched on in TMAC table rules.
251 *
252 * @return true if match on in-port should be programmed
253 */
254 protected boolean matchInPortTmacTable() {
255 return true;
256 }
257
Charles Chand9e47c62017-10-05 15:17:15 -0700258 /**
259 * Determines whether matching L4 destination port on IPv6 packets is supported in ACL table.
260 *
261 * @return true if matching L4 destination port on IPv6 packets is supported in ACL table.
262 */
263 protected boolean supportIpv6L4Dst() {
264 return true;
265 }
266
Saurav Dasc568c342018-01-25 09:49:01 -0800267 /**
268 * Determines whether this driver should continue to retry flows that point
269 * to empty groups. See CORD-554.
270 *
271 * @return true if the driver should retry flows
272 */
273 protected boolean shouldRetry() {
274 return true;
275 }
276
Charles Chan45b69ab2018-03-02 15:41:41 -0800277 /**
278 * Determines whether this driver requires unicast flow to be installed before multicast flow
279 * in TMAC table.
280 *
281 * @return true if required
282 */
283 protected boolean requireUnicastBeforeMulticast() {
284 return false;
285 }
286
Andrea Campanellad980c6d2018-04-30 11:48:55 +0200287 /**
288 * Determines whether this driver supports installing a clearDeferred action on table 30.
289 *
290 * @return true if required
291 */
292 protected boolean supportsUnicastBlackHole() {
293 return true;
294 }
295
Charles Chanab591602019-01-22 17:25:04 -0800296 protected boolean requirePuntTable() {
297 return false;
298 }
299
Saurav Das822c4e22015-10-23 10:51:11 -0700300 //////////////////////////////////////
301 // Flow Objectives
302 //////////////////////////////////////
303
304 @Override
305 public void filter(FilteringObjective filteringObjective) {
306 if (filteringObjective.type() == FilteringObjective.Type.PERMIT) {
307 processFilter(filteringObjective,
308 filteringObjective.op() == Objective.Operation.ADD,
309 filteringObjective.appId());
310 } else {
311 // Note that packets that don't match the PERMIT filter are
312 // automatically denied. The DENY filter is used to deny packets
313 // that are otherwise permitted by the PERMIT filter.
314 // Use ACL table flow rules here for DENY filtering objectives
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530315 log.warn("filter objective other than PERMIT currently not supported");
Saurav Das822c4e22015-10-23 10:51:11 -0700316 fail(filteringObjective, ObjectiveError.UNSUPPORTED);
317 }
318 }
319
320 @Override
321 public void forward(ForwardingObjective fwd) {
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700322 Collection<FlowRule> rules = processForward(fwd);
Saurav Das25190812016-05-27 13:54:07 -0700323 if (rules == null || rules.isEmpty()) {
324 // Assumes fail message has already been generated to the objective
325 // context. Returning here prevents spurious pass message to be
326 // generated by FlowRule service for empty flowOps.
327 return;
328 }
pier7afc7522019-05-10 13:19:15 +0200329 // Let's accumulate flow rules if accumulator is active and fwd objective is not versatile.
330 // Otherwise send directly, without adding futher delay
331 if (accumulator != null && Objects.equals(fwd.flag(), SPECIFIC)) {
pier8b3aef42019-03-11 15:14:02 -0700332 accumulator.add(Pair.of(fwd, rules));
333 } else {
334 sendForwards(Collections.singletonList(Pair.of(fwd, rules)));
335 }
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700336 }
337
pier8b3aef42019-03-11 15:14:02 -0700338 // Builds the batch using the accumulated flow rules
339 private void sendForwards(List<Pair<ForwardingObjective, Collection<FlowRule>>> pairs) {
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700340 FlowRuleOperations.Builder flowOpsBuilder = FlowRuleOperations.builder();
pier8b3aef42019-03-11 15:14:02 -0700341 log.debug("Sending {} fwd-objs", pairs.size());
342 List<Objective> fwdObjs = Lists.newArrayList();
343 // Iterates over all accumulated flow rules and then build an unique batch
344 pairs.forEach(pair -> {
345 ForwardingObjective fwd = pair.getLeft();
346 Collection<FlowRule> rules = pair.getRight();
347 switch (fwd.op()) {
348 case ADD:
349 rules.stream()
350 .filter(Objects::nonNull)
351 .forEach(flowOpsBuilder::add);
352 log.debug("Applying a add fwd-obj {} to sw:{}", fwd.id(), deviceId);
353 fwdObjs.add(fwd);
354 break;
355 case REMOVE:
356 rules.stream()
357 .filter(Objects::nonNull)
358 .forEach(flowOpsBuilder::remove);
359 log.debug("Deleting a flow rule to sw:{}", deviceId);
360 fwdObjs.add(fwd);
361 break;
362 default:
363 fail(fwd, ObjectiveError.UNKNOWN);
364 log.warn("Unknown forwarding type {}", fwd.op());
365 }
366 });
367 // Finally applies the operations
Saurav Das822c4e22015-10-23 10:51:11 -0700368 flowRuleService.apply(flowOpsBuilder.build(new FlowRuleOperationsContext() {
pier8b3aef42019-03-11 15:14:02 -0700369
Saurav Das822c4e22015-10-23 10:51:11 -0700370 @Override
371 public void onSuccess(FlowRuleOperations ops) {
pier8b3aef42019-03-11 15:14:02 -0700372 log.trace("Flow rule operations onSuccess {}", ops);
pier9469f3e2019-04-17 17:05:08 +0200373 fwdObjs.forEach(OfdpaPipelineUtility::pass);
Saurav Das822c4e22015-10-23 10:51:11 -0700374 }
375
376 @Override
377 public void onError(FlowRuleOperations ops) {
pier8b3aef42019-03-11 15:14:02 -0700378 ObjectiveError error = ObjectiveError.FLOWINSTALLATIONFAILED;
379 log.warn("Flow rule operations onError {}. Reason = {}", ops, error);
380 fwdObjs.forEach(fwdObj -> fail(fwdObj, error));
Saurav Das822c4e22015-10-23 10:51:11 -0700381 }
382 }));
Saurav Das822c4e22015-10-23 10:51:11 -0700383 }
384
385 @Override
386 public void next(NextObjective nextObjective) {
Saurav Das8a0732e2015-11-20 15:27:53 -0800387 NextGroup nextGroup = flowObjectiveStore.getNextGroup(nextObjective.id());
388 switch (nextObjective.op()) {
389 case ADD:
Saurav Das4f980082015-11-05 13:39:15 -0800390 if (nextGroup != null) {
Saurav Das8a0732e2015-11-20 15:27:53 -0800391 log.warn("Cannot add next {} that already exists in device {}",
392 nextObjective.id(), deviceId);
393 return;
394 }
Saurav Das1547b3f2017-05-05 17:01:08 -0700395 log.debug("Processing NextObjective id {} in dev {} - add group",
Saurav Das8a0732e2015-11-20 15:27:53 -0800396 nextObjective.id(), deviceId);
Charles Chan425854b2016-04-11 15:32:12 -0700397 groupHandler.addGroup(nextObjective);
Saurav Das8a0732e2015-11-20 15:27:53 -0800398 break;
399 case ADD_TO_EXISTING:
400 if (nextGroup != null) {
Saurav Das1547b3f2017-05-05 17:01:08 -0700401 log.debug("Processing NextObjective id {} in dev {} - add bucket",
Saurav Das8a0732e2015-11-20 15:27:53 -0800402 nextObjective.id(), deviceId);
Charles Chan425854b2016-04-11 15:32:12 -0700403 groupHandler.addBucketToGroup(nextObjective, nextGroup);
Saurav Das4f980082015-11-05 13:39:15 -0800404 } else {
Saurav Das8a0732e2015-11-20 15:27:53 -0800405 // it is possible that group-chain has not been fully created yet
Saurav Das423fe2b2015-12-04 10:52:59 -0800406 log.debug("Waiting to add bucket to group for next-id:{} in dev:{}",
407 nextObjective.id(), deviceId);
Yi Tseng47f82dc2017-03-05 22:48:39 -0800408
409 // by design multiple pending bucket is allowed for the group
410 groupHandler.pendingBuckets.compute(nextObjective.id(), (nextId, pendBkts) -> {
411 if (pendBkts == null) {
412 pendBkts = Sets.newHashSet();
413 }
414 pendBkts.add(nextObjective);
415 return pendBkts;
416 });
Saurav Das4f980082015-11-05 13:39:15 -0800417 }
Saurav Das8a0732e2015-11-20 15:27:53 -0800418 break;
419 case REMOVE:
420 if (nextGroup == null) {
421 log.warn("Cannot remove next {} that does not exist in device {}",
422 nextObjective.id(), deviceId);
423 return;
424 }
Saurav Das1547b3f2017-05-05 17:01:08 -0700425 log.debug("Processing NextObjective id {} in dev {} - remove group",
Saurav Das8a0732e2015-11-20 15:27:53 -0800426 nextObjective.id(), deviceId);
Charles Chan425854b2016-04-11 15:32:12 -0700427 groupHandler.removeGroup(nextObjective, nextGroup);
Saurav Das8a0732e2015-11-20 15:27:53 -0800428 break;
429 case REMOVE_FROM_EXISTING:
430 if (nextGroup == null) {
431 log.warn("Cannot remove from next {} that does not exist in device {}",
432 nextObjective.id(), deviceId);
433 return;
434 }
Saurav Das1547b3f2017-05-05 17:01:08 -0700435 log.debug("Processing NextObjective id {} in dev {} - remove bucket",
Saurav Das8a0732e2015-11-20 15:27:53 -0800436 nextObjective.id(), deviceId);
Charles Chan425854b2016-04-11 15:32:12 -0700437 groupHandler.removeBucketFromGroup(nextObjective, nextGroup);
Saurav Das8a0732e2015-11-20 15:27:53 -0800438 break;
Jonghwan Hyunf810a7a2018-02-12 16:43:45 +0900439 case MODIFY:
440 if (nextGroup == null) {
441 log.warn("Cannot modify next {} that does not exist in device {}",
442 nextObjective.id(), deviceId);
443 return;
444 }
Ruchi Sahotaef0761c2019-01-28 01:08:18 +0000445 log.debug("Processing NextObjective id {} in dev {} group {} - modify bucket",
446 nextObjective.id(), deviceId, nextGroup);
Jonghwan Hyunf810a7a2018-02-12 16:43:45 +0900447 groupHandler.modifyBucketFromGroup(nextObjective, nextGroup);
448 break;
Saurav Dasceccf242017-08-03 18:30:35 -0700449 case VERIFY:
450 if (nextGroup == null) {
451 log.warn("Cannot verify next {} that does not exist in device {}",
452 nextObjective.id(), deviceId);
453 return;
454 }
455 log.debug("Processing NextObjective id {} in dev {} - verify",
456 nextObjective.id(), deviceId);
457 groupHandler.verifyGroup(nextObjective, nextGroup);
458 break;
Saurav Das8a0732e2015-11-20 15:27:53 -0800459 default:
Saurav Das4f980082015-11-05 13:39:15 -0800460 log.warn("Unsupported operation {}", nextObjective.op());
Saurav Das822c4e22015-10-23 10:51:11 -0700461 }
462 }
463
464 //////////////////////////////////////
465 // Flow handling
466 //////////////////////////////////////
467
468 /**
Yi Tseng0fdc07e2017-04-24 19:17:08 -0700469 * As per OFDPA 2.0 TTP, filtering of VLAN ids and MAC addresses (for routing)
470 * configured on switch ports happen in different tables.
Saurav Das822c4e22015-10-23 10:51:11 -0700471 *
472 * @param filt the filtering objective
473 * @param install indicates whether to add or remove the objective
474 * @param applicationId the application that sent this objective
475 */
Saurav Das52025962016-01-28 22:30:01 -0800476 protected void processFilter(FilteringObjective filt,
477 boolean install, ApplicationId applicationId) {
Saurav Das822c4e22015-10-23 10:51:11 -0700478 // This driver only processes filtering criteria defined with switch
479 // ports as the key
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530480 PortCriterion portCriterion = null;
Saurav Das822c4e22015-10-23 10:51:11 -0700481 EthCriterion ethCriterion = null;
482 VlanIdCriterion vidCriterion = null;
Saurav Das822c4e22015-10-23 10:51:11 -0700483 if (!filt.key().equals(Criteria.dummy()) &&
484 filt.key().type() == Criterion.Type.IN_PORT) {
485 portCriterion = (PortCriterion) filt.key();
Saurav Das822c4e22015-10-23 10:51:11 -0700486 }
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530487 if (portCriterion == null) {
488 log.debug("No IN_PORT defined in filtering objective from app: {}",
489 applicationId);
490 } else {
491 log.debug("Received filtering objective for dev/port: {}/{}", deviceId,
492 portCriterion.port());
493 }
Saurav Das822c4e22015-10-23 10:51:11 -0700494 // convert filtering conditions for switch-intfs into flowrules
495 FlowRuleOperations.Builder ops = FlowRuleOperations.builder();
496 for (Criterion criterion : filt.conditions()) {
Yi Tseng0fdc07e2017-04-24 19:17:08 -0700497 switch (criterion.type()) {
498 case ETH_DST:
499 case ETH_DST_MASKED:
500 ethCriterion = (EthCriterion) criterion;
501 break;
502 case VLAN_VID:
503 vidCriterion = (VlanIdCriterion) criterion;
504 break;
505 default:
506 log.warn("Unsupported filter {}", criterion);
507 fail(filt, ObjectiveError.UNSUPPORTED);
508 return;
Saurav Das822c4e22015-10-23 10:51:11 -0700509 }
510 }
511
Saurav Das0e99e2b2015-10-28 12:39:42 -0700512 VlanId assignedVlan = null;
Charles Chane849c192016-01-11 18:28:54 -0800513 if (vidCriterion != null) {
Charles Chand55e84d2016-03-30 17:54:24 -0700514 // Use the VLAN in criterion if metadata is not present and the traffic is tagged
Charles Chanc550f2e2017-12-19 19:55:57 -0800515 if (!vidCriterion.vlanId().equals(VlanId.NONE)) {
Charles Chane849c192016-01-11 18:28:54 -0800516 assignedVlan = vidCriterion.vlanId();
Piere5bff482018-03-07 11:42:50 +0100517 }
518 // If the meta VLAN is present let's update the assigned vlan
519 if (filt.meta() != null) {
520 VlanId metaVlan = readVlanFromTreatment(filt.meta());
521 if (metaVlan != null) {
522 assignedVlan = metaVlan;
523 }
Charles Chand55e84d2016-03-30 17:54:24 -0700524 }
Charles Chane849c192016-01-11 18:28:54 -0800525
Charles Chand55e84d2016-03-30 17:54:24 -0700526 if (assignedVlan == null) {
527 log.error("Driver fails to extract VLAN information. "
Frank Wangd8ab0962017-08-11 11:09:30 +0800528 + "Not processing VLAN filters on device {}.", deviceId);
Charles Chand55e84d2016-03-30 17:54:24 -0700529 log.debug("VLAN ID in criterion={}, metadata={}",
530 readVlanFromTreatment(filt.meta()), vidCriterion.vlanId());
531 fail(filt, ObjectiveError.BADPARAMS);
532 return;
Saurav Das0e99e2b2015-10-28 12:39:42 -0700533 }
534 }
535
Yi Tseng3a77b4f2017-02-06 15:02:17 -0800536 if (ethCriterion == null || ethCriterion.mac().equals(NONE)) {
Charles Chan985b12e2016-05-11 19:47:22 -0700537 // NOTE: it is possible that a filtering objective only has vidCriterion
Daniel Ginsburgc1d47e92018-04-30 19:27:19 -0400538 log.debug("filtering objective missing dstMac, won't program TMAC table");
Saurav Das822c4e22015-10-23 10:51:11 -0700539 } else {
Charles Chan45b69ab2018-03-02 15:41:41 -0800540 MacAddress unicastMac = readEthDstFromTreatment(filt.meta());
Charles Chan66291502018-03-02 16:43:28 -0800541 List<List<FlowRule>> allStages = processEthDstFilter(portCriterion, ethCriterion,
Charles Chan45b69ab2018-03-02 15:41:41 -0800542 vidCriterion, assignedVlan, unicastMac, applicationId);
Charles Chan66291502018-03-02 16:43:28 -0800543 for (List<FlowRule> flowRules : allStages) {
Charles Chan45b69ab2018-03-02 15:41:41 -0800544 log.trace("Starting a new flow rule stage for TMAC table flow");
Charles Chan66291502018-03-02 16:43:28 -0800545 ops.newStage();
Charles Chanc550f2e2017-12-19 19:55:57 -0800546
Charles Chan66291502018-03-02 16:43:28 -0800547 for (FlowRule flowRule : flowRules) {
Charles Chan056e0c12018-05-10 22:19:49 +0000548 log.trace("{} flow rules in TMAC table: {} for dev: {}",
Charles Chan66291502018-03-02 16:43:28 -0800549 (install) ? "adding" : "removing", flowRules, deviceId);
Charles Chan66291502018-03-02 16:43:28 -0800550 if (install) {
551 ops = ops.add(flowRule);
Charles Chanc550f2e2017-12-19 19:55:57 -0800552 } else {
Charles Chan66291502018-03-02 16:43:28 -0800553 // NOTE: Only remove TMAC flow when there is no more enabled port within the
554 // same VLAN on this device if TMAC doesn't support matching on in_port.
555 if (matchInPortTmacTable() || (filt.meta() != null && filt.meta().clearedDeferred())) {
556 ops = ops.remove(flowRule);
557 } else {
558 log.debug("Abort TMAC flow removal on {}. Some other ports still share this TMAC flow");
559 }
Charles Chanc550f2e2017-12-19 19:55:57 -0800560 }
561 }
Saurav Das822c4e22015-10-23 10:51:11 -0700562 }
563 }
564
Charles Chan985b12e2016-05-11 19:47:22 -0700565 if (vidCriterion == null) {
566 // NOTE: it is possible that a filtering objective only has ethCriterion
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530567 log.info("filtering objective missing VLAN, cannot program VLAN Table");
Saurav Das822c4e22015-10-23 10:51:11 -0700568 } else {
Charles Chan66291502018-03-02 16:43:28 -0800569 List<List<FlowRule>> allStages = processVlanIdFilter(
pier6aef5b72019-06-10 17:10:26 +0200570 portCriterion, vidCriterion, assignedVlan, applicationId, install);
Charles Chan66291502018-03-02 16:43:28 -0800571 for (List<FlowRule> flowRules : allStages) {
Charles Chan45b69ab2018-03-02 15:41:41 -0800572 log.trace("Starting a new flow rule stage for VLAN table flow");
Charles Chan66291502018-03-02 16:43:28 -0800573 ops.newStage();
Charles Chan14967c22015-12-07 11:11:50 -0800574
Charles Chan66291502018-03-02 16:43:28 -0800575 for (FlowRule flowRule : flowRules) {
576 log.trace("{} flow rules in VLAN table: {} for dev: {}",
577 (install) ? "adding" : "removing", flowRule, deviceId);
578 ops = install ? ops.add(flowRule) : ops.remove(flowRule);
Charles Chand1172632017-03-15 17:33:09 -0700579 }
Saurav Das822c4e22015-10-23 10:51:11 -0700580 }
581 }
582
Saurav Das822c4e22015-10-23 10:51:11 -0700583 // apply filtering flow rules
584 flowRuleService.apply(ops.build(new FlowRuleOperationsContext() {
585 @Override
586 public void onSuccess(FlowRuleOperations ops) {
Saurav Das018605f2017-02-18 14:05:44 -0800587 log.debug("Applied {} filtering rules in device {}",
Saurav Das822c4e22015-10-23 10:51:11 -0700588 ops.stages().get(0).size(), deviceId);
589 pass(filt);
590 }
591
592 @Override
593 public void onError(FlowRuleOperations ops) {
594 log.info("Failed to apply all filtering rules in dev {}", deviceId);
595 fail(filt, ObjectiveError.FLOWINSTALLATIONFAILED);
596 }
597 }));
598
599 }
600
601 /**
Charles Chand1172632017-03-15 17:33:09 -0700602 * Internal implementation of processVlanIdFilter.
603 * <p>
604 * The is_present bit in set_vlan_vid action is required to be 0 in OFDPA i12.
605 * Since it is non-OF spec, we need an extension treatment for that.
606 * The useVlanExtension must be set to false for OFDPA i12.
607 * </p>
Charles Chan66291502018-03-02 16:43:28 -0800608 * <p>
609 * NOTE: Separate VLAN filtering rules and assignment rules
610 * into different stages in order to guarantee that filtering rules
611 * always go first, as required by OFDPA.
612 * </p>
Saurav Das0e99e2b2015-10-28 12:39:42 -0700613 *
Charles Chanf9e98652016-09-07 16:54:23 -0700614 * @param portCriterion port on device for which this filter is programmed
615 * @param vidCriterion vlan assigned to port, or NONE for untagged
616 * @param assignedVlan assigned vlan-id for untagged packets
617 * @param applicationId for application programming this filter
pier6aef5b72019-06-10 17:10:26 +0200618 * @param install indicates whether to add or remove the objective
Charles Chan66291502018-03-02 16:43:28 -0800619 * @return stages of flow rules for port-vlan filters
Saurav Das822c4e22015-10-23 10:51:11 -0700620 */
Charles Chan66291502018-03-02 16:43:28 -0800621 protected List<List<FlowRule>> processVlanIdFilter(PortCriterion portCriterion,
pier6aef5b72019-06-10 17:10:26 +0200622 VlanIdCriterion vidCriterion,
623 VlanId assignedVlan,
624 ApplicationId applicationId,
625 boolean install) {
Charles Chan66291502018-03-02 16:43:28 -0800626 List<FlowRule> filteringRules = new ArrayList<>();
627 List<FlowRule> assignmentRules = new ArrayList<>();
Saurav Das822c4e22015-10-23 10:51:11 -0700628 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
629 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
Charles Chan14967c22015-12-07 11:11:50 -0800630 TrafficSelector.Builder preSelector = null;
631 TrafficTreatment.Builder preTreatment = null;
632
Saurav Das4f980082015-11-05 13:39:15 -0800633 treatment.transition(TMAC_TABLE);
634
Saurav Das822c4e22015-10-23 10:51:11 -0700635 if (vidCriterion.vlanId() == VlanId.NONE) {
636 // untagged packets are assigned vlans
Charles Chand1172632017-03-15 17:33:09 -0700637 preSelector = DefaultTrafficSelector.builder();
638 if (requireVlanExtensions()) {
639 OfdpaMatchVlanVid ofdpaMatchVlanVid = new OfdpaMatchVlanVid(VlanId.NONE);
640 selector.extension(ofdpaMatchVlanVid, deviceId);
Charles Chanf9e98652016-09-07 16:54:23 -0700641 OfdpaSetVlanVid ofdpaSetVlanVid = new OfdpaSetVlanVid(assignedVlan);
642 treatment.extension(ofdpaSetVlanVid, deviceId);
Charles Chand1172632017-03-15 17:33:09 -0700643
644 OfdpaMatchVlanVid preOfdpaMatchVlanVid = new OfdpaMatchVlanVid(assignedVlan);
645 preSelector.extension(preOfdpaMatchVlanVid, deviceId);
Charles Chanf9e98652016-09-07 16:54:23 -0700646 } else {
Charles Chand1172632017-03-15 17:33:09 -0700647 selector.matchVlanId(VlanId.NONE);
Charles Chanf9e98652016-09-07 16:54:23 -0700648 treatment.setVlanId(assignedVlan);
Charles Chand1172632017-03-15 17:33:09 -0700649
650 preSelector.matchVlanId(assignedVlan);
651 }
652 preTreatment = DefaultTrafficTreatment.builder().transition(TMAC_TABLE);
653 } else {
654 if (requireVlanExtensions()) {
655 OfdpaMatchVlanVid ofdpaMatchVlanVid = new OfdpaMatchVlanVid(vidCriterion.vlanId());
656 selector.extension(ofdpaMatchVlanVid, deviceId);
657 } else {
658 selector.matchVlanId(vidCriterion.vlanId());
Charles Chanf9e98652016-09-07 16:54:23 -0700659 }
Charles Chan14967c22015-12-07 11:11:50 -0800660
Charles Chand55e84d2016-03-30 17:54:24 -0700661 if (!assignedVlan.equals(vidCriterion.vlanId())) {
Charles Chand1172632017-03-15 17:33:09 -0700662 if (requireVlanExtensions()) {
Charles Chanc03782d2017-01-31 13:57:55 -0800663 OfdpaSetVlanVid ofdpaSetVlanVid = new OfdpaSetVlanVid(assignedVlan);
664 treatment.extension(ofdpaSetVlanVid, deviceId);
665 } else {
666 treatment.setVlanId(assignedVlan);
667 }
Charles Chand55e84d2016-03-30 17:54:24 -0700668 }
Saurav Das822c4e22015-10-23 10:51:11 -0700669 }
Saurav Das822c4e22015-10-23 10:51:11 -0700670
671 // ofdpa cannot match on ALL portnumber, so we need to use separate
672 // rules for each port.
Charles Chan14967c22015-12-07 11:11:50 -0800673 List<PortNumber> portnums = new ArrayList<>();
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530674 if (portCriterion != null) {
675 if (PortNumber.ALL.equals(portCriterion.port())) {
676 for (Port port : deviceService.getPorts(deviceId)) {
677 if (port.number().toLong() > 0 && port.number().toLong() < OFPP_MAX) {
678 portnums.add(port.number());
679 }
Saurav Das822c4e22015-10-23 10:51:11 -0700680 }
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530681 } else {
682 portnums.add(portCriterion.port());
Saurav Das822c4e22015-10-23 10:51:11 -0700683 }
684 } else {
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530685 log.warn("Filtering Objective missing Port Criterion . " +
Charles Chan66291502018-03-02 16:43:28 -0800686 "VLAN Table cannot be programmed for {}", deviceId);
Saurav Das822c4e22015-10-23 10:51:11 -0700687 }
Saurav Das4f980082015-11-05 13:39:15 -0800688
Saurav Das822c4e22015-10-23 10:51:11 -0700689 for (PortNumber pnum : portnums) {
Saurav Das4f980082015-11-05 13:39:15 -0800690 // create rest of flowrule
Saurav Das822c4e22015-10-23 10:51:11 -0700691 selector.matchInPort(pnum);
692 FlowRule rule = DefaultFlowRule.builder()
693 .forDevice(deviceId)
694 .withSelector(selector.build())
695 .withTreatment(treatment.build())
696 .withPriority(DEFAULT_PRIORITY)
697 .fromApp(applicationId)
698 .makePermanent()
699 .forTable(VLAN_TABLE).build();
Charles Chan66291502018-03-02 16:43:28 -0800700 assignmentRules.add(rule);
Charles Chan14967c22015-12-07 11:11:50 -0800701
702 if (preSelector != null) {
703 preSelector.matchInPort(pnum);
704 FlowRule preRule = DefaultFlowRule.builder()
705 .forDevice(deviceId)
706 .withSelector(preSelector.build())
707 .withTreatment(preTreatment.build())
708 .withPriority(DEFAULT_PRIORITY)
709 .fromApp(applicationId)
710 .makePermanent()
711 .forTable(VLAN_TABLE).build();
Charles Chan66291502018-03-02 16:43:28 -0800712 filteringRules.add(preRule);
Charles Chan14967c22015-12-07 11:11:50 -0800713 }
Saurav Das822c4e22015-10-23 10:51:11 -0700714 }
pier6aef5b72019-06-10 17:10:26 +0200715 return install ? ImmutableList.of(filteringRules, assignmentRules) :
716 ImmutableList.of(assignmentRules, filteringRules);
Saurav Das822c4e22015-10-23 10:51:11 -0700717 }
718
719 /**
720 * Allows routed packets with correct destination MAC to be directed
Charles Chan45b69ab2018-03-02 15:41:41 -0800721 * to unicast routing table, multicast routing table or MPLS forwarding table.
Saurav Das822c4e22015-10-23 10:51:11 -0700722 *
723 * @param portCriterion port on device for which this filter is programmed
724 * @param ethCriterion dstMac of device for which is filter is programmed
725 * @param vidCriterion vlan assigned to port, or NONE for untagged
Saurav Das0e99e2b2015-10-28 12:39:42 -0700726 * @param assignedVlan assigned vlan-id for untagged packets
Charles Chan45b69ab2018-03-02 15:41:41 -0800727 * @param unicastMac some switches require a unicast TMAC flow to be programmed before multicast
728 * TMAC flow. This MAC address will be used for the unicast TMAC flow.
729 * This is unused if the filtering objective is a unicast.
Saurav Das822c4e22015-10-23 10:51:11 -0700730 * @param applicationId for application programming this filter
Charles Chan66291502018-03-02 16:43:28 -0800731 * @return stages of flow rules for port-vlan filters
Saurav Das822c4e22015-10-23 10:51:11 -0700732
733 */
Charles Chan66291502018-03-02 16:43:28 -0800734 protected List<List<FlowRule>> processEthDstFilter(PortCriterion portCriterion,
Saurav Das822c4e22015-10-23 10:51:11 -0700735 EthCriterion ethCriterion,
736 VlanIdCriterion vidCriterion,
Saurav Das0e99e2b2015-10-28 12:39:42 -0700737 VlanId assignedVlan,
Charles Chan45b69ab2018-03-02 15:41:41 -0800738 MacAddress unicastMac,
Saurav Das822c4e22015-10-23 10:51:11 -0700739 ApplicationId applicationId) {
Charles Chan5270ed02016-01-30 23:22:37 -0800740 // Consider PortNumber.ANY as wildcard. Match ETH_DST only
Sivachidambaram Subramanianc598d062017-05-16 22:42:06 +0530741 if (portCriterion != null && PortNumber.ANY.equals(portCriterion.port())) {
Charles Chan5270ed02016-01-30 23:22:37 -0800742 return processEthDstOnlyFilter(ethCriterion, applicationId);
743 }
744
Charles Chan5b9df8d2016-03-28 22:21:40 -0700745 // Multicast MAC
746 if (ethCriterion.mask() != null) {
Charles Chan45b69ab2018-03-02 15:41:41 -0800747 return processMcastEthDstFilter(ethCriterion, assignedVlan, unicastMac, applicationId);
Charles Chan5b9df8d2016-03-28 22:21:40 -0700748 }
749
Saurav Das822c4e22015-10-23 10:51:11 -0700750 //handling untagged packets via assigned VLAN
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530751 if (vidCriterion != null && vidCriterion.vlanId() == VlanId.NONE) {
Saurav Das0e99e2b2015-10-28 12:39:42 -0700752 vidCriterion = (VlanIdCriterion) Criteria.matchVlanId(assignedVlan);
Saurav Das822c4e22015-10-23 10:51:11 -0700753 }
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530754 List<FlowRule> rules = new ArrayList<>();
755 OfdpaMatchVlanVid ofdpaMatchVlanVid = null;
756 if (vidCriterion != null && requireVlanExtensions()) {
757 ofdpaMatchVlanVid = new OfdpaMatchVlanVid(vidCriterion.vlanId());
758 }
Saurav Das822c4e22015-10-23 10:51:11 -0700759 // ofdpa cannot match on ALL portnumber, so we need to use separate
760 // rules for each port.
Yuta HIGUCHI2dce08a2017-04-20 21:57:48 -0700761 List<PortNumber> portnums = new ArrayList<>();
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530762 if (portCriterion != null) {
763 if (PortNumber.ALL.equals(portCriterion.port())) {
764 for (Port port : deviceService.getPorts(deviceId)) {
765 if (port.number().toLong() > 0 && port.number().toLong() < OFPP_MAX) {
766 portnums.add(port.number());
767 }
Saurav Das822c4e22015-10-23 10:51:11 -0700768 }
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530769 } else {
770 portnums.add(portCriterion.port());
771 }
772 for (PortNumber pnum : portnums) {
773 rules.add(buildTmacRuleForIpv4(ethCriterion,
774 vidCriterion,
775 ofdpaMatchVlanVid,
776 applicationId,
777 pnum));
778 rules.add(buildTmacRuleForMpls(ethCriterion,
779 vidCriterion,
780 ofdpaMatchVlanVid,
781 applicationId,
782 pnum));
783 rules.add(buildTmacRuleForIpv6(ethCriterion,
784 vidCriterion,
785 ofdpaMatchVlanVid,
786 applicationId,
787 pnum));
Saurav Das822c4e22015-10-23 10:51:11 -0700788 }
789 } else {
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530790 rules.add(buildTmacRuleForIpv4(ethCriterion,
791 vidCriterion,
792 ofdpaMatchVlanVid,
793 applicationId,
794 null));
795 rules.add(buildTmacRuleForMpls(ethCriterion,
796 vidCriterion,
797 ofdpaMatchVlanVid,
798 applicationId,
799 null));
800 rules.add(buildTmacRuleForIpv6(ethCriterion,
801 vidCriterion,
802 ofdpaMatchVlanVid,
803 applicationId,
804 null));
Saurav Das822c4e22015-10-23 10:51:11 -0700805 }
Charles Chan66291502018-03-02 16:43:28 -0800806 return ImmutableList.of(rules);
Saurav Das822c4e22015-10-23 10:51:11 -0700807 }
808
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530809 /**
810 * Builds TMAC rules for IPv4 packets.
811 *
Charles Chanc550f2e2017-12-19 19:55:57 -0800812 * @param ethCriterion dst mac matching
813 * @param vidCriterion vlan id assigned to the port
814 * @param ofdpaMatchVlanVid OFDPA vlan id matching
815 * @param applicationId application id
816 * @param pnum port number
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530817 * @return TMAC rule for IPV4 packets
818 */
819 private FlowRule buildTmacRuleForIpv4(EthCriterion ethCriterion,
820 VlanIdCriterion vidCriterion,
821 OfdpaMatchVlanVid ofdpaMatchVlanVid,
822 ApplicationId applicationId,
823 PortNumber pnum) {
824 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
825 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
826 if (pnum != null) {
827 if (matchInPortTmacTable()) {
828 selector.matchInPort(pnum);
829 } else {
Saurav Dasf14d9ef2017-12-05 15:00:23 -0800830 log.debug("Pipeline does not support IN_PORT matching in TMAC table, " +
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530831 "ignoring the IN_PORT criteria");
832 }
833 }
834 if (vidCriterion != null) {
835 if (requireVlanExtensions()) {
836 selector.extension(ofdpaMatchVlanVid, deviceId);
837 } else {
838 selector.matchVlanId(vidCriterion.vlanId());
839 }
840 }
841 selector.matchEthType(Ethernet.TYPE_IPV4);
842 selector.matchEthDst(ethCriterion.mac());
843 treatment.transition(UNICAST_ROUTING_TABLE);
844 return DefaultFlowRule.builder()
845 .forDevice(deviceId)
846 .withSelector(selector.build())
847 .withTreatment(treatment.build())
848 .withPriority(DEFAULT_PRIORITY)
849 .fromApp(applicationId)
850 .makePermanent()
851 .forTable(TMAC_TABLE).build();
852 }
853
854 /**
855 * Builds TMAC rule for MPLS packets.
856 *
Charles Chanc550f2e2017-12-19 19:55:57 -0800857 * @param ethCriterion dst mac matching
858 * @param vidCriterion vlan id assigned to the port
859 * @param ofdpaMatchVlanVid OFDPA vlan id matching
860 * @param applicationId application id
861 * @param pnum port number
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530862 * @return TMAC rule for MPLS packets
863 */
864 private FlowRule buildTmacRuleForMpls(EthCriterion ethCriterion,
865 VlanIdCriterion vidCriterion,
866 OfdpaMatchVlanVid ofdpaMatchVlanVid,
867 ApplicationId applicationId,
868 PortNumber pnum) {
869 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
870 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
871 if (pnum != null) {
872 if (matchInPortTmacTable()) {
873 selector.matchInPort(pnum);
874 } else {
Saurav Dasf14d9ef2017-12-05 15:00:23 -0800875 log.debug("Pipeline does not support IN_PORT matching in TMAC table, " +
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530876 "ignoring the IN_PORT criteria");
877 }
878 }
879 if (vidCriterion != null) {
880 if (requireVlanExtensions()) {
881 selector.extension(ofdpaMatchVlanVid, deviceId);
882 } else {
883 selector.matchVlanId(vidCriterion.vlanId());
884 }
885 }
886 selector.matchEthType(Ethernet.MPLS_UNICAST);
887 selector.matchEthDst(ethCriterion.mac());
888 treatment.transition(MPLS_TABLE_0);
889 return DefaultFlowRule.builder()
890 .forDevice(deviceId)
891 .withSelector(selector.build())
892 .withTreatment(treatment.build())
893 .withPriority(DEFAULT_PRIORITY)
894 .fromApp(applicationId)
895 .makePermanent()
896 .forTable(TMAC_TABLE).build();
897 }
898
899 /**
900 * Builds TMAC rules for IPv6 packets.
901 *
Charles Chanc550f2e2017-12-19 19:55:57 -0800902 * @param ethCriterion dst mac matching
903 * @param vidCriterion vlan id assigned to the port
904 * @param ofdpaMatchVlanVid OFDPA vlan id matching
905 * @param applicationId application id
906 * @param pnum port number
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530907 * @return TMAC rule for IPV6 packets
908 */
909 private FlowRule buildTmacRuleForIpv6(EthCriterion ethCriterion,
910 VlanIdCriterion vidCriterion,
911 OfdpaMatchVlanVid ofdpaMatchVlanVid,
912 ApplicationId applicationId,
913 PortNumber pnum) {
914 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
915 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
916 if (pnum != null) {
917 if (matchInPortTmacTable()) {
918 selector.matchInPort(pnum);
919 } else {
Saurav Dasf14d9ef2017-12-05 15:00:23 -0800920 log.debug("Pipeline does not support IN_PORT matching in TMAC table, " +
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530921 "ignoring the IN_PORT criteria");
922 }
923 }
924 if (vidCriterion != null) {
925 if (requireVlanExtensions()) {
926 selector.extension(ofdpaMatchVlanVid, deviceId);
927 } else {
928 selector.matchVlanId(vidCriterion.vlanId());
929 }
930 }
931 selector.matchEthType(Ethernet.TYPE_IPV6);
932 selector.matchEthDst(ethCriterion.mac());
933 treatment.transition(UNICAST_ROUTING_TABLE);
934 return DefaultFlowRule.builder()
935 .forDevice(deviceId)
936 .withSelector(selector.build())
937 .withTreatment(treatment.build())
938 .withPriority(DEFAULT_PRIORITY)
939 .fromApp(applicationId)
940 .makePermanent()
941 .forTable(TMAC_TABLE).build();
942 }
943
Charles Chan66291502018-03-02 16:43:28 -0800944 protected List<List<FlowRule>> processEthDstOnlyFilter(EthCriterion ethCriterion,
Yi Tsengef19de12017-04-24 11:33:05 -0700945 ApplicationId applicationId) {
Pier Luigi0e358632017-01-31 09:35:05 -0800946 ImmutableList.Builder<FlowRule> builder = ImmutableList.builder();
947
Charles Chan5270ed02016-01-30 23:22:37 -0800948 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
949 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
950 selector.matchEthType(Ethernet.TYPE_IPV4);
951 selector.matchEthDst(ethCriterion.mac());
952 treatment.transition(UNICAST_ROUTING_TABLE);
953 FlowRule rule = DefaultFlowRule.builder()
954 .forDevice(deviceId)
955 .withSelector(selector.build())
956 .withTreatment(treatment.build())
957 .withPriority(DEFAULT_PRIORITY)
958 .fromApp(applicationId)
959 .makePermanent()
960 .forTable(TMAC_TABLE).build();
Pier Luigi0e358632017-01-31 09:35:05 -0800961 builder.add(rule);
962
963 selector = DefaultTrafficSelector.builder();
964 treatment = DefaultTrafficTreatment.builder();
965 selector.matchEthType(Ethernet.TYPE_IPV6);
966 selector.matchEthDst(ethCriterion.mac());
967 treatment.transition(UNICAST_ROUTING_TABLE);
968 rule = DefaultFlowRule.builder()
969 .forDevice(deviceId)
970 .withSelector(selector.build())
971 .withTreatment(treatment.build())
972 .withPriority(DEFAULT_PRIORITY)
973 .fromApp(applicationId)
974 .makePermanent()
975 .forTable(TMAC_TABLE).build();
Charles Chan66291502018-03-02 16:43:28 -0800976 return ImmutableList.of(builder.add(rule).build());
Charles Chan5270ed02016-01-30 23:22:37 -0800977 }
978
Charles Chan66291502018-03-02 16:43:28 -0800979 List<List<FlowRule>> processMcastEthDstFilter(EthCriterion ethCriterion,
Charles Chanb4879a52017-10-20 19:09:16 -0700980 VlanId assignedVlan,
Charles Chan45b69ab2018-03-02 15:41:41 -0800981 MacAddress unicastMac,
Yi Tsengef19de12017-04-24 11:33:05 -0700982 ApplicationId applicationId) {
Charles Chan45b69ab2018-03-02 15:41:41 -0800983 ImmutableList.Builder<FlowRule> unicastFlows = ImmutableList.builder();
984 ImmutableList.Builder<FlowRule> multicastFlows = ImmutableList.builder();
985 TrafficSelector.Builder selector;
986 TrafficTreatment.Builder treatment;
Charles Chanb4879a52017-10-20 19:09:16 -0700987 FlowRule rule;
Julia Ferguson65428c32017-08-10 18:15:24 +0000988
Charles Chanb4879a52017-10-20 19:09:16 -0700989 if (IPV4_MULTICAST.equals(ethCriterion.mac())) {
Charles Chan45b69ab2018-03-02 15:41:41 -0800990 if (requireUnicastBeforeMulticast()) {
991 selector = DefaultTrafficSelector.builder();
992 treatment = DefaultTrafficTreatment.builder();
993 selector.matchEthType(Ethernet.TYPE_IPV4);
994 selector.matchEthDst(unicastMac);
Charles Chan056e0c12018-05-10 22:19:49 +0000995 selector.matchVlanId(assignedVlan);
Charles Chan45b69ab2018-03-02 15:41:41 -0800996 treatment.transition(UNICAST_ROUTING_TABLE);
997 rule = DefaultFlowRule.builder()
998 .forDevice(deviceId)
999 .withSelector(selector.build())
1000 .withTreatment(treatment.build())
Charles Chan056e0c12018-05-10 22:19:49 +00001001 .withPriority(DEFAULT_PRIORITY)
Charles Chan45b69ab2018-03-02 15:41:41 -08001002 .fromApp(applicationId)
1003 .makePermanent()
1004 .forTable(TMAC_TABLE).build();
1005 unicastFlows.add(rule);
1006 }
1007
1008 selector = DefaultTrafficSelector.builder();
1009 treatment = DefaultTrafficTreatment.builder();
Charles Chanb4879a52017-10-20 19:09:16 -07001010 selector.matchEthType(Ethernet.TYPE_IPV4);
1011 selector.matchEthDstMasked(ethCriterion.mac(), ethCriterion.mask());
Charles Chan056e0c12018-05-10 22:19:49 +00001012 selector.matchVlanId(assignedVlan);
Charles Chanb4879a52017-10-20 19:09:16 -07001013 treatment.transition(MULTICAST_ROUTING_TABLE);
1014 rule = DefaultFlowRule.builder()
1015 .forDevice(deviceId)
1016 .withSelector(selector.build())
1017 .withTreatment(treatment.build())
Charles Chan056e0c12018-05-10 22:19:49 +00001018 .withPriority(DEFAULT_PRIORITY)
Charles Chanb4879a52017-10-20 19:09:16 -07001019 .fromApp(applicationId)
1020 .makePermanent()
1021 .forTable(TMAC_TABLE).build();
Charles Chan45b69ab2018-03-02 15:41:41 -08001022 multicastFlows.add(rule);
Charles Chanb4879a52017-10-20 19:09:16 -07001023 }
1024
1025 if (IPV6_MULTICAST.equals(ethCriterion.mac())) {
Charles Chan45b69ab2018-03-02 15:41:41 -08001026 if (requireUnicastBeforeMulticast()) {
1027 selector = DefaultTrafficSelector.builder();
1028 treatment = DefaultTrafficTreatment.builder();
1029 selector.matchEthType(Ethernet.TYPE_IPV6);
1030 selector.matchEthDst(unicastMac);
Charles Chan056e0c12018-05-10 22:19:49 +00001031 selector.matchVlanId(assignedVlan);
Charles Chan45b69ab2018-03-02 15:41:41 -08001032 treatment.transition(UNICAST_ROUTING_TABLE);
1033 rule = DefaultFlowRule.builder()
1034 .forDevice(deviceId)
1035 .withSelector(selector.build())
1036 .withTreatment(treatment.build())
Charles Chan056e0c12018-05-10 22:19:49 +00001037 .withPriority(DEFAULT_PRIORITY)
Charles Chan45b69ab2018-03-02 15:41:41 -08001038 .fromApp(applicationId)
1039 .makePermanent()
1040 .forTable(TMAC_TABLE).build();
1041 unicastFlows.add(rule);
1042 }
1043
Charles Chanb4879a52017-10-20 19:09:16 -07001044 selector = DefaultTrafficSelector.builder();
1045 treatment = DefaultTrafficTreatment.builder();
1046 selector.matchEthType(Ethernet.TYPE_IPV6);
1047 selector.matchEthDstMasked(ethCriterion.mac(), ethCriterion.mask());
Charles Chan056e0c12018-05-10 22:19:49 +00001048 selector.matchVlanId(assignedVlan);
Charles Chanb4879a52017-10-20 19:09:16 -07001049 treatment.transition(MULTICAST_ROUTING_TABLE);
1050 rule = DefaultFlowRule.builder()
1051 .forDevice(deviceId)
1052 .withSelector(selector.build())
1053 .withTreatment(treatment.build())
Charles Chan056e0c12018-05-10 22:19:49 +00001054 .withPriority(DEFAULT_PRIORITY)
Charles Chanb4879a52017-10-20 19:09:16 -07001055 .fromApp(applicationId)
1056 .makePermanent()
1057 .forTable(TMAC_TABLE).build();
Charles Chan45b69ab2018-03-02 15:41:41 -08001058 multicastFlows.add(rule);
Charles Chanb4879a52017-10-20 19:09:16 -07001059 }
Charles Chan45b69ab2018-03-02 15:41:41 -08001060 return ImmutableList.of(unicastFlows.build(), multicastFlows.build());
Charles Chan5b9df8d2016-03-28 22:21:40 -07001061 }
1062
Saurav Das822c4e22015-10-23 10:51:11 -07001063 private Collection<FlowRule> processForward(ForwardingObjective fwd) {
1064 switch (fwd.flag()) {
1065 case SPECIFIC:
1066 return processSpecific(fwd);
1067 case VERSATILE:
1068 return processVersatile(fwd);
Jonghwan Hyun800d9d02018-04-09 09:40:50 -07001069 case EGRESS:
1070 return processEgress(fwd);
Saurav Das822c4e22015-10-23 10:51:11 -07001071 default:
1072 fail(fwd, ObjectiveError.UNKNOWN);
1073 log.warn("Unknown forwarding flag {}", fwd.flag());
1074 }
1075 return Collections.emptySet();
1076 }
1077
1078 /**
Jonghwan Hyun800d9d02018-04-09 09:40:50 -07001079 * In the OF-DPA 2.0 pipeline, egress forwarding objectives go to the
1080 * egress tables.
1081 * @param fwd the forwarding objective of type 'egress'
1082 * @return a collection of flow rules to be sent to the switch. An empty
1083 * collection may be returned if there is a problem in processing
1084 * the flow rule
1085 */
1086 protected Collection<FlowRule> processEgress(ForwardingObjective fwd) {
1087 log.debug("Processing egress forwarding objective:{} in dev:{}",
1088 fwd, deviceId);
1089
1090 List<FlowRule> rules = new ArrayList<>();
1091
1092 // Build selector
1093 TrafficSelector.Builder sb = DefaultTrafficSelector.builder();
1094 VlanIdCriterion vlanIdCriterion = (VlanIdCriterion) fwd.selector().getCriterion(Criterion.Type.VLAN_VID);
1095 if (vlanIdCriterion == null) {
1096 log.error("Egress forwarding objective:{} must include vlanId", fwd.id());
1097 fail(fwd, ObjectiveError.BADPARAMS);
1098 return rules;
1099 }
1100
1101 Optional<Instruction> outInstr = fwd.treatment().allInstructions().stream()
1102 .filter(instruction -> instruction instanceof OutputInstruction).findFirst();
1103 if (!outInstr.isPresent()) {
1104 log.error("Egress forwarding objective:{} must include output port", fwd.id());
1105 fail(fwd, ObjectiveError.BADPARAMS);
1106 return rules;
1107 }
1108
1109 PortNumber portNumber = ((OutputInstruction) outInstr.get()).port();
1110
1111 sb.matchVlanId(vlanIdCriterion.vlanId());
1112 OfdpaMatchActsetOutput actsetOutput = new OfdpaMatchActsetOutput(portNumber);
1113 sb.extension(actsetOutput, deviceId);
1114
1115 sb.extension(new OfdpaMatchAllowVlanTranslation(ALLOW_VLAN_TRANSLATION), deviceId);
1116
1117 // Build a flow rule for Egress VLAN Flow table
1118 TrafficTreatment.Builder tb = DefaultTrafficTreatment.builder();
1119 tb.transition(EGRESS_DSCP_PCP_REMARK_FLOW_TABLE);
1120 if (fwd.treatment() != null) {
1121 for (Instruction instr : fwd.treatment().allInstructions()) {
1122 if (instr instanceof L2ModificationInstruction &&
1123 ((L2ModificationInstruction) instr).subtype() == L2SubType.VLAN_ID) {
1124 tb.immediate().add(instr);
1125 }
1126 if (instr instanceof L2ModificationInstruction &&
1127 ((L2ModificationInstruction) instr).subtype() == L2SubType.VLAN_PUSH) {
1128 tb.immediate().pushVlan();
1129 EthType ethType = ((L2ModificationInstruction.ModVlanHeaderInstruction) instr).ethernetType();
1130 if (ethType.equals(EtherType.QINQ.ethType())) {
1131 // Build a flow rule for Egress TPID Flow table
1132 TrafficSelector tpidSelector = DefaultTrafficSelector.builder()
1133 .extension(actsetOutput, deviceId)
1134 .matchVlanId(VlanId.ANY).build();
1135
1136 TrafficTreatment tpidTreatment = DefaultTrafficTreatment.builder()
1137 .extension(new Ofdpa3CopyField(COPY_FIELD_NBITS, COPY_FIELD_OFFSET,
1138 COPY_FIELD_OFFSET, OXM_ID_VLAN_VID,
1139 OXM_ID_PACKET_REG_1),
1140 deviceId)
1141 .popVlan()
1142 .pushVlan(EtherType.QINQ.ethType())
1143 .extension(new Ofdpa3CopyField(COPY_FIELD_NBITS, COPY_FIELD_OFFSET,
1144 COPY_FIELD_OFFSET, OXM_ID_PACKET_REG_1,
1145 OXM_ID_VLAN_VID),
1146 deviceId)
1147 .build();
1148
1149 FlowRule.Builder tpidRuleBuilder = DefaultFlowRule.builder()
1150 .fromApp(fwd.appId())
1151 .withPriority(fwd.priority())
1152 .forDevice(deviceId)
1153 .withSelector(tpidSelector)
1154 .withTreatment(tpidTreatment)
1155 .makePermanent()
1156 .forTable(EGRESS_TPID_FLOW_TABLE);
1157 rules.add(tpidRuleBuilder.build());
1158 }
1159 }
1160 }
1161 }
1162
1163 FlowRule.Builder ruleBuilder = DefaultFlowRule.builder()
1164 .fromApp(fwd.appId())
1165 .withPriority(fwd.priority())
1166 .forDevice(deviceId)
1167 .withSelector(sb.build())
1168 .withTreatment(tb.build())
1169 .makePermanent()
1170 .forTable(EGRESS_VLAN_FLOW_TABLE);
1171 rules.add(ruleBuilder.build());
1172 return rules;
1173 }
1174
1175 /**
Saurav Das822c4e22015-10-23 10:51:11 -07001176 * In the OF-DPA 2.0 pipeline, versatile forwarding objectives go to the
1177 * ACL table.
1178 * @param fwd the forwarding objective of type 'versatile'
1179 * @return a collection of flow rules to be sent to the switch. An empty
1180 * collection may be returned if there is a problem in processing
1181 * the flow rule
1182 */
Saurav Das52025962016-01-28 22:30:01 -08001183 protected Collection<FlowRule> processVersatile(ForwardingObjective fwd) {
Saurav Das018605f2017-02-18 14:05:44 -08001184 log.debug("Processing versatile forwarding objective:{} in dev:{}",
Saurav Dasd2fded02016-12-02 15:43:47 -08001185 fwd.id(), deviceId);
Harshada Chaundkar77958a52019-08-05 15:33:30 +00001186 List<FlowRule> flowRules = new ArrayList<>();
1187 final AtomicBoolean ethTypeUsed = new AtomicBoolean(false);
Saurav Das822c4e22015-10-23 10:51:11 -07001188
Saurav Das822c4e22015-10-23 10:51:11 -07001189 if (fwd.nextId() == null && fwd.treatment() == null) {
1190 log.error("Forwarding objective {} from {} must contain "
1191 + "nextId or Treatment", fwd.selector(), fwd.appId());
Zsolt Haraszti9faab752016-02-17 15:55:20 -08001192 fail(fwd, ObjectiveError.BADPARAMS);
Saurav Das822c4e22015-10-23 10:51:11 -07001193 return Collections.emptySet();
1194 }
Saurav Das49cb5a12016-01-16 22:54:07 -08001195
Saurav Das77b5e902016-01-27 17:01:59 -08001196 TrafficSelector.Builder sbuilder = DefaultTrafficSelector.builder();
1197 fwd.selector().criteria().forEach(criterion -> {
1198 if (criterion instanceof VlanIdCriterion) {
1199 VlanId vlanId = ((VlanIdCriterion) criterion).vlanId();
1200 // ensure that match does not include vlan = NONE as OF-DPA does not
1201 // match untagged packets this way in the ACL table.
1202 if (vlanId.equals(VlanId.NONE)) {
1203 return;
1204 }
Charles Chand1172632017-03-15 17:33:09 -07001205 if (requireVlanExtensions()) {
1206 OfdpaMatchVlanVid ofdpaMatchVlanVid = new OfdpaMatchVlanVid(vlanId);
1207 sbuilder.extension(ofdpaMatchVlanVid, deviceId);
1208 } else {
1209 sbuilder.matchVlanId(vlanId);
1210 }
Charles Chan09bf2692018-01-11 11:48:18 -08001211 } else if (criterion instanceof Icmpv6TypeCriterion) {
1212 byte icmpv6Type = (byte) ((Icmpv6TypeCriterion) criterion).icmpv6Type();
1213 sbuilder.matchIcmpv6Type(icmpv6Type);
1214 } else if (criterion instanceof Icmpv6CodeCriterion) {
1215 byte icmpv6Code = (byte) ((Icmpv6CodeCriterion) criterion).icmpv6Code();
1216 sbuilder.matchIcmpv6Type(icmpv6Code);
Charles Chand9e47c62017-10-05 15:17:15 -07001217 } else if (criterion instanceof TcpPortCriterion || criterion instanceof UdpPortCriterion) {
1218 // FIXME: QMX switches do not support L4 dst port matching in ACL table.
1219 // Currently L4 dst port matching is only used by DHCP relay feature
1220 // and therefore is safe to be replaced with L4 src port matching.
1221 // We need to revisit this if L4 dst port is used for other purpose in the future.
1222 if (!supportIpv6L4Dst() && isIpv6(fwd.selector())) {
Charles Chand9e47c62017-10-05 15:17:15 -07001223 switch (criterion.type()) {
1224 case UDP_DST:
Charles Chand9e47c62017-10-05 15:17:15 -07001225 case UDP_DST_MASKED:
Charles Chand9e47c62017-10-05 15:17:15 -07001226 case TCP_DST:
Charles Chand9e47c62017-10-05 15:17:15 -07001227 case TCP_DST_MASKED:
Charles Chand9e47c62017-10-05 15:17:15 -07001228 break;
1229 default:
1230 sbuilder.add(criterion);
1231 }
1232 } else {
1233 sbuilder.add(criterion);
1234 }
Harshada Chaundkar77958a52019-08-05 15:33:30 +00001235 } else if (criterion instanceof EthTypeCriterion) {
1236 sbuilder.add(criterion);
1237 ethTypeUsed.set(true);
Saurav Das77b5e902016-01-27 17:01:59 -08001238 } else {
1239 sbuilder.add(criterion);
1240 }
1241 });
Harshada Chaundkar77958a52019-08-05 15:33:30 +00001242
1243 TrafficTreatment.Builder ttBuilder = versatileTreatmentBuilder(fwd);
1244 if (ttBuilder == null) {
1245 return Collections.emptySet();
1246 }
1247
1248 FlowRule.Builder ruleBuilder = DefaultFlowRule.builder()
1249 .fromApp(fwd.appId())
1250 .withPriority(fwd.priority())
1251 .forDevice(deviceId)
1252 .withSelector(sbuilder.build())
1253 .withTreatment(ttBuilder.build())
1254 .makePermanent()
1255 .forTable(ACL_TABLE);
1256
1257 flowRules.add(ruleBuilder.build());
1258
1259 if (!ethTypeUsed.get() && requireEthType()) {
1260 log.debug("{} doesn't match on ethType but requireEthType is true, adding complementary ACL flow.",
1261 sbuilder.toString());
1262 sbuilder.matchEthType(Ethernet.TYPE_IPV6);
1263 FlowRule.Builder ethTypeRuleBuilder = DefaultFlowRule.builder()
1264 .fromApp(fwd.appId())
1265 .withPriority(fwd.priority())
1266 .forDevice(deviceId)
1267 .withSelector(sbuilder.build())
1268 .withTreatment(ttBuilder.build())
1269 .makePermanent()
1270 .forTable(ACL_TABLE);
1271 flowRules.add(ethTypeRuleBuilder.build());
1272 }
1273 return flowRules;
Charles Chanab591602019-01-22 17:25:04 -08001274 }
Saurav Das77b5e902016-01-27 17:01:59 -08001275
Charles Chanab591602019-01-22 17:25:04 -08001276 /**
1277 * Helper function to create traffic treatment builder for versatile forwarding objectives.
1278 *
1279 * @param fwd original forwarding objective
1280 * @return treatment builder for the flow rule, or null if there is an error.
1281 */
1282 protected TrafficTreatment.Builder versatileTreatmentBuilder(ForwardingObjective fwd) {
Saurav Das822c4e22015-10-23 10:51:11 -07001283 // XXX driver does not currently do type checking as per Tables 65-67 in
1284 // OFDPA 2.0 spec. The only allowed treatment is a punt to the controller.
Saurav Das49cb5a12016-01-16 22:54:07 -08001285 TrafficTreatment.Builder ttBuilder = DefaultTrafficTreatment.builder();
1286 if (fwd.treatment() != null) {
1287 for (Instruction ins : fwd.treatment().allInstructions()) {
1288 if (ins instanceof OutputInstruction) {
1289 OutputInstruction o = (OutputInstruction) ins;
Ray Milkeyfe0e0852018-01-18 11:14:05 -08001290 if (PortNumber.CONTROLLER.equals(o.port())) {
Saurav Das49cb5a12016-01-16 22:54:07 -08001291 ttBuilder.add(o);
1292 } else {
1293 log.warn("Only allowed treatments in versatile forwarding "
1294 + "objectives are punts to the controller");
1295 }
Charles Chanf76de302018-06-15 18:54:18 -07001296 } else if (ins instanceof NoActionInstruction) {
1297 // No action is allowed and nothing needs to be done
Saurav Das49cb5a12016-01-16 22:54:07 -08001298 } else {
1299 log.warn("Cannot process instruction in versatile fwd {}", ins);
1300 }
Saurav Das822c4e22015-10-23 10:51:11 -07001301 }
Charles Chan2df0e8a2017-01-09 11:45:08 -08001302 if (fwd.treatment().clearedDeferred()) {
1303 ttBuilder.wipeDeferred();
1304 }
Saurav Das822c4e22015-10-23 10:51:11 -07001305 }
Saurav Das822c4e22015-10-23 10:51:11 -07001306 if (fwd.nextId() != null) {
Charles Chanab591602019-01-22 17:25:04 -08001307 // Override case
Saurav Das49cb5a12016-01-16 22:54:07 -08001308 NextGroup next = getGroupForNextObjective(fwd.nextId());
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +05301309 if (next == null) {
1310 fail(fwd, ObjectiveError.BADPARAMS);
Charles Chanab591602019-01-22 17:25:04 -08001311 return null;
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +05301312 }
Saurav Das49cb5a12016-01-16 22:54:07 -08001313 List<Deque<GroupKey>> gkeys = appKryo.deserialize(next.data());
1314 // we only need the top level group's key to point the flow to it
1315 Group group = groupService.getGroup(deviceId, gkeys.get(0).peekFirst());
1316 if (group == null) {
1317 log.warn("Group with key:{} for next-id:{} not found in dev:{}",
Charles Chanab591602019-01-22 17:25:04 -08001318 gkeys.get(0).peekFirst(), fwd.nextId(), deviceId);
Saurav Das49cb5a12016-01-16 22:54:07 -08001319 fail(fwd, ObjectiveError.GROUPMISSING);
Charles Chanab591602019-01-22 17:25:04 -08001320 return null;
Saurav Das49cb5a12016-01-16 22:54:07 -08001321 }
1322 ttBuilder.deferred().group(group.id());
Saurav Das822c4e22015-10-23 10:51:11 -07001323 }
Charles Chanab591602019-01-22 17:25:04 -08001324 return ttBuilder;
Saurav Das822c4e22015-10-23 10:51:11 -07001325 }
1326
1327 /**
1328 * In the OF-DPA 2.0 pipeline, specific forwarding refers to the IP table
Saurav Das8a0732e2015-11-20 15:27:53 -08001329 * (unicast or multicast) or the L2 table (mac + vlan) or the MPLS table.
Saurav Das822c4e22015-10-23 10:51:11 -07001330 *
1331 * @param fwd the forwarding objective of type 'specific'
1332 * @return a collection of flow rules. Typically there will be only one
1333 * for this type of forwarding objective. An empty set may be
1334 * returned if there is an issue in processing the objective.
1335 */
Charles Chan50d900c2018-03-02 13:26:22 -08001336 private Collection<FlowRule> processSpecific(ForwardingObjective fwd) {
Saurav Das25190812016-05-27 13:54:07 -07001337 log.debug("Processing specific fwd objective:{} in dev:{} with next:{}",
Saurav Das4ce45962015-11-24 23:21:05 -08001338 fwd.id(), deviceId, fwd.nextId());
1339 boolean isEthTypeObj = isSupportedEthTypeObjective(fwd);
1340 boolean isEthDstObj = isSupportedEthDstObjective(fwd);
1341
1342 if (isEthTypeObj) {
1343 return processEthTypeSpecific(fwd);
1344 } else if (isEthDstObj) {
1345 return processEthDstSpecific(fwd);
1346 } else {
1347 log.warn("processSpecific: Unsupported forwarding objective "
1348 + "criteria fwd:{} in dev:{}", fwd.nextId(), deviceId);
Saurav Das822c4e22015-10-23 10:51:11 -07001349 fail(fwd, ObjectiveError.UNSUPPORTED);
1350 return Collections.emptySet();
1351 }
Saurav Das4ce45962015-11-24 23:21:05 -08001352 }
1353
Saurav Das4ce45962015-11-24 23:21:05 -08001354 /**
1355 * Handles forwarding rules to the IP and MPLS tables.
1356 *
1357 * @param fwd the forwarding objective
1358 * @return A collection of flow rules, or an empty set
1359 */
1360 protected Collection<FlowRule> processEthTypeSpecific(ForwardingObjective fwd) {
Charles Chancad338a2016-09-16 18:03:11 -07001361 return processEthTypeSpecificInternal(fwd, false, ACL_TABLE);
Charles Chanf9e98652016-09-07 16:54:23 -07001362 }
1363
1364 /**
1365 * Internal implementation of processEthTypeSpecific.
1366 * <p>
1367 * Wildcarded IPv4_DST is not supported in OFDPA i12. Therefore, we break
1368 * the rule into 0.0.0.0/1 and 128.0.0.0/1.
1369 * The allowDefaultRoute must be set to false for OFDPA i12.
1370 * </p>
1371 *
1372 * @param fwd the forwarding objective
1373 * @param allowDefaultRoute allow wildcarded IPv4_DST or not
Ray Milkey0bb1e102016-11-10 14:51:27 -08001374 * @param mplsNextTable next MPLS table
Charles Chanf9e98652016-09-07 16:54:23 -07001375 * @return A collection of flow rules, or an empty set
1376 */
1377 protected Collection<FlowRule> processEthTypeSpecificInternal(ForwardingObjective fwd,
Charles Chancad338a2016-09-16 18:03:11 -07001378 boolean allowDefaultRoute,
1379 int mplsNextTable) {
Saurav Das4ce45962015-11-24 23:21:05 -08001380 TrafficSelector selector = fwd.selector();
1381 EthTypeCriterion ethType =
1382 (EthTypeCriterion) selector.getCriterion(Criterion.Type.ETH_TYPE);
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001383 boolean emptyGroup = false;
Charles Chan188ebf52015-12-23 00:15:11 -08001384 int forTableId;
Saurav Das8a0732e2015-11-20 15:27:53 -08001385 TrafficSelector.Builder filteredSelector = DefaultTrafficSelector.builder();
Charles Chan14967c22015-12-07 11:11:50 -08001386 TrafficTreatment.Builder tb = DefaultTrafficTreatment.builder();
Flavio Castroe10fa242016-01-15 12:43:51 -08001387 TrafficSelector.Builder complementarySelector = DefaultTrafficSelector.builder();
Charles Chan14967c22015-12-07 11:11:50 -08001388
Saurav Das8a0732e2015-11-20 15:27:53 -08001389 if (ethType.ethType().toShort() == Ethernet.TYPE_IPV4) {
Pier Luigi3bfe32c2017-01-30 09:47:36 -08001390 if (buildIpv4Selector(filteredSelector, complementarySelector, fwd, allowDefaultRoute) < 0) {
1391 return Collections.emptyList();
1392 }
1393 // We need to set properly the next table
Flavio Castroe10fa242016-01-15 12:43:51 -08001394 IpPrefix ipv4Dst = ((IPCriterion) selector.getCriterion(Criterion.Type.IPV4_DST)).ip();
Charles Chan5b9df8d2016-03-28 22:21:40 -07001395 if (ipv4Dst.isMulticast()) {
Charles Chan5b9df8d2016-03-28 22:21:40 -07001396 forTableId = MULTICAST_ROUTING_TABLE;
Flavio Castroe10fa242016-01-15 12:43:51 -08001397 } else {
Charles Chan5b9df8d2016-03-28 22:21:40 -07001398 forTableId = UNICAST_ROUTING_TABLE;
Flavio Castroe10fa242016-01-15 12:43:51 -08001399 }
Charles Chan50d900c2018-03-02 13:26:22 -08001400
Charles Chan236653d2018-03-05 11:28:23 -08001401 // TODO decrementing IP ttl is done automatically for routing, this
Charles Chan50d900c2018-03-02 13:26:22 -08001402 // action is ignored or rejected in ofdpa as it is not fully implemented
Charles Chan236653d2018-03-05 11:28:23 -08001403 // if (fwd.treatment() != null) {
1404 // for (Instruction instr : fwd.treatment().allInstructions()) {
1405 // if (instr instanceof L3ModificationInstruction &&
1406 // ((L3ModificationInstruction) instr).subtype() == L3SubType.DEC_TTL) {
1407 // tb.deferred().add(instr);
1408 // }
1409 // }
1410 // }
1411
Pier Ventree0ae7a32016-11-23 09:57:42 -08001412 } else if (ethType.ethType().toShort() == Ethernet.TYPE_IPV6) {
1413 if (buildIpv6Selector(filteredSelector, fwd) < 0) {
1414 return Collections.emptyList();
1415 }
Kalhee Kim3d26bf02017-10-04 14:59:11 +00001416 //We need to set the proper next table
1417 IpPrefix ipv6Dst = ((IPCriterion) selector.getCriterion(Criterion.Type.IPV6_DST)).ip();
1418 if (ipv6Dst.isMulticast()) {
1419 forTableId = MULTICAST_ROUTING_TABLE;
1420 } else {
1421 forTableId = UNICAST_ROUTING_TABLE;
1422 }
1423
Charles Chan236653d2018-03-05 11:28:23 -08001424 // TODO decrementing IP ttl is done automatically for routing, this
Charles Chan50d900c2018-03-02 13:26:22 -08001425 // action is ignored or rejected in ofdpa as it is not fully implemented
Charles Chan236653d2018-03-05 11:28:23 -08001426 // if (fwd.treatment() != null) {
1427 // for (Instruction instr : fwd.treatment().allInstructions()) {
1428 // if (instr instanceof L3ModificationInstruction &&
1429 // ((L3ModificationInstruction) instr).subtype() == L3SubType.DEC_TTL) {
1430 // tb.deferred().add(instr);
1431 // }
1432 // }
1433 // }
Saurav Das8a0732e2015-11-20 15:27:53 -08001434 } else {
1435 filteredSelector
1436 .matchEthType(Ethernet.MPLS_UNICAST)
1437 .matchMplsLabel(((MplsCriterion)
1438 selector.getCriterion(Criterion.Type.MPLS_LABEL)).label());
1439 MplsBosCriterion bos = (MplsBosCriterion) selector
Pier Ventre140a8942016-11-02 07:26:38 -07001440 .getCriterion(MPLS_BOS);
Alex Yashchuk4caa8e82017-12-08 17:40:05 +02001441 if (bos != null && requireMplsBosMatch()) {
Saurav Das8a0732e2015-11-20 15:27:53 -08001442 filteredSelector.matchMplsBos(bos.mplsBos());
1443 }
1444 forTableId = MPLS_TABLE_1;
Saurav Das4ce45962015-11-24 23:21:05 -08001445 log.debug("processing MPLS specific forwarding objective {} -> next:{}"
1446 + " in dev {}", fwd.id(), fwd.nextId(), deviceId);
Saurav Das822c4e22015-10-23 10:51:11 -07001447
Charles Chan14967c22015-12-07 11:11:50 -08001448 if (fwd.treatment() != null) {
1449 for (Instruction instr : fwd.treatment().allInstructions()) {
1450 if (instr instanceof L2ModificationInstruction &&
1451 ((L2ModificationInstruction) instr).subtype() == L2SubType.MPLS_POP) {
Pier Luigi3bfe32c2017-01-30 09:47:36 -08001452 // OF-DPA does not pop in MPLS table in some cases. For the L3 VPN, it requires
Saurav Das9c705342017-01-06 11:36:01 -08001453 // setting the MPLS_TYPE so pop can happen down the pipeline
Alex Yashchuk4caa8e82017-12-08 17:40:05 +02001454 if (requireMplsPop()) {
1455 if (mplsNextTable == MPLS_TYPE_TABLE && isNotMplsBos(selector)) {
1456 tb.immediate().popMpls();
1457 }
1458 } else {
1459 // Skip mpls pop action for mpls_unicast label
1460 if (instr instanceof ModMplsHeaderInstruction &&
1461 !((ModMplsHeaderInstruction) instr).ethernetType()
1462 .equals(EtherType.MPLS_UNICAST.ethType())) {
1463 tb.immediate().add(instr);
1464 }
Pier Luigi3bfe32c2017-01-30 09:47:36 -08001465 }
Charles Chan14967c22015-12-07 11:11:50 -08001466 }
Alex Yashchuk4caa8e82017-12-08 17:40:05 +02001467
1468 if (requireMplsTtlModification()) {
1469 if (instr instanceof L3ModificationInstruction &&
1470 ((L3ModificationInstruction) instr).subtype() == L3SubType.DEC_TTL) {
1471 // FIXME Should modify the app to send the correct DEC_MPLS_TTL instruction
1472 tb.immediate().decMplsTtl();
1473 }
1474 if (instr instanceof L3ModificationInstruction &&
1475 ((L3ModificationInstruction) instr).subtype() == L3SubType.TTL_IN) {
1476 tb.immediate().add(instr);
1477 }
Charles Chan14967c22015-12-07 11:11:50 -08001478 }
Saurav Das8a0732e2015-11-20 15:27:53 -08001479 }
1480 }
1481 }
Saurav Das822c4e22015-10-23 10:51:11 -07001482
1483 if (fwd.nextId() != null) {
Saurav Das423fe2b2015-12-04 10:52:59 -08001484 NextGroup next = getGroupForNextObjective(fwd.nextId());
1485 if (next != null) {
1486 List<Deque<GroupKey>> gkeys = appKryo.deserialize(next.data());
1487 // we only need the top level group's key to point the flow to it
1488 Group group = groupService.getGroup(deviceId, gkeys.get(0).peekFirst());
1489 if (group == null) {
1490 log.warn("Group with key:{} for next-id:{} not found in dev:{}",
1491 gkeys.get(0).peekFirst(), fwd.nextId(), deviceId);
1492 fail(fwd, ObjectiveError.GROUPMISSING);
1493 return Collections.emptySet();
1494 }
Saurav Dasa4020382018-02-14 14:14:54 -08001495 if (isNotMplsBos(selector) && group.type().equals(SELECT)) {
1496 log.warn("SR CONTINUE case cannot be handled as MPLS ECMP "
1497 + "is not implemented in OF-DPA yet. Aborting flow {} -> next:{} "
1498 + "in this device {}", fwd.id(), fwd.nextId(), deviceId);
1499 fail(fwd, ObjectiveError.FLOWINSTALLATIONFAILED);
1500 return Collections.emptySet();
1501 }
Saurav Das423fe2b2015-12-04 10:52:59 -08001502 tb.deferred().group(group.id());
Saurav Dasc568c342018-01-25 09:49:01 -08001503 // retrying flows may be necessary due to bug CORD-554
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001504 if (gkeys.size() == 1 && gkeys.get(0).size() == 1) {
Saurav Dasc568c342018-01-25 09:49:01 -08001505 if (shouldRetry()) {
1506 log.warn("Found empty group 0x{} in dev:{} .. will retry fwd:{}",
1507 Integer.toHexString(group.id().id()), deviceId,
1508 fwd.id());
1509 emptyGroup = true;
1510 }
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001511 }
Saurav Das25190812016-05-27 13:54:07 -07001512 } else {
1513 log.warn("Cannot find group for nextId:{} in dev:{}. Aborting fwd:{}",
1514 fwd.nextId(), deviceId, fwd.id());
1515 fail(fwd, ObjectiveError.FLOWINSTALLATIONFAILED);
1516 return Collections.emptySet();
Saurav Das822c4e22015-10-23 10:51:11 -07001517 }
Saurav Das822c4e22015-10-23 10:51:11 -07001518 }
Charles Chancad338a2016-09-16 18:03:11 -07001519
1520 if (forTableId == MPLS_TABLE_1) {
Pier Ventre140a8942016-11-02 07:26:38 -07001521 if (mplsNextTable == MPLS_L3_TYPE_TABLE) {
Charles Chancad338a2016-09-16 18:03:11 -07001522 Ofdpa3SetMplsType setMplsType = new Ofdpa3SetMplsType(Ofdpa3MplsType.L3_PHP);
Saurav Das9c705342017-01-06 11:36:01 -08001523 // set mpls type as apply_action
1524 tb.immediate().extension(setMplsType, deviceId);
Charles Chancad338a2016-09-16 18:03:11 -07001525 }
1526 tb.transition(mplsNextTable);
1527 } else {
1528 tb.transition(ACL_TABLE);
1529 }
1530
Andrea Campanellad980c6d2018-04-30 11:48:55 +02001531 if (fwd.treatment() != null && fwd.treatment().clearedDeferred()) {
1532 if (supportsUnicastBlackHole()) {
1533 tb.wipeDeferred();
1534 } else {
1535 log.warn("Clear Deferred is not supported Unicast Routing Table on device {}", deviceId);
1536 return Collections.emptySet();
1537 }
1538 }
1539
Saurav Das822c4e22015-10-23 10:51:11 -07001540 FlowRule.Builder ruleBuilder = DefaultFlowRule.builder()
1541 .fromApp(fwd.appId())
1542 .withPriority(fwd.priority())
1543 .forDevice(deviceId)
Saurav Das8a0732e2015-11-20 15:27:53 -08001544 .withSelector(filteredSelector.build())
1545 .withTreatment(tb.build())
1546 .forTable(forTableId);
Saurav Das822c4e22015-10-23 10:51:11 -07001547
1548 if (fwd.permanent()) {
1549 ruleBuilder.makePermanent();
1550 } else {
1551 ruleBuilder.makeTemporary(fwd.timeout());
1552 }
Flavio Castroe10fa242016-01-15 12:43:51 -08001553 Collection<FlowRule> flowRuleCollection = new ArrayList<>();
1554 flowRuleCollection.add(ruleBuilder.build());
Pier Luigi3bfe32c2017-01-30 09:47:36 -08001555 if (!allowDefaultRoute) {
Pier Ventree0ae7a32016-11-23 09:57:42 -08001556 flowRuleCollection.add(
1557 defaultRoute(fwd, complementarySelector, forTableId, tb)
1558 );
Flavio Castroe10fa242016-01-15 12:43:51 -08001559 log.debug("Default rule 0.0.0.0/0 is being installed two rules");
1560 }
Saurav Dasc568c342018-01-25 09:49:01 -08001561
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001562 if (emptyGroup) {
pier8b3aef42019-03-11 15:14:02 -07001563 retryExecutorService.schedule(new RetryFlows(fwd, flowRuleCollection),
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001564 RETRY_MS, TimeUnit.MILLISECONDS);
1565 }
Flavio Castroe10fa242016-01-15 12:43:51 -08001566 return flowRuleCollection;
Saurav Das822c4e22015-10-23 10:51:11 -07001567 }
1568
Charles Chan50d900c2018-03-02 13:26:22 -08001569 private int buildIpv4Selector(TrafficSelector.Builder builderToUpdate,
Pier Luigi3bfe32c2017-01-30 09:47:36 -08001570 TrafficSelector.Builder extBuilder,
1571 ForwardingObjective fwd,
1572 boolean allowDefaultRoute) {
1573 TrafficSelector selector = fwd.selector();
1574
1575 IpPrefix ipv4Dst = ((IPCriterion) selector.getCriterion(Criterion.Type.IPV4_DST)).ip();
1576 if (ipv4Dst.isMulticast()) {
1577 if (ipv4Dst.prefixLength() != 32) {
1578 log.warn("Multicast specific forwarding objective can only be /32");
1579 fail(fwd, ObjectiveError.BADPARAMS);
1580 return -1;
1581 }
1582 VlanId assignedVlan = readVlanFromSelector(fwd.meta());
1583 if (assignedVlan == null) {
1584 log.warn("VLAN ID required by multicast specific fwd obj is missing. Abort.");
1585 fail(fwd, ObjectiveError.BADPARAMS);
1586 return -1;
1587 }
Charles Chand1172632017-03-15 17:33:09 -07001588 if (requireVlanExtensions()) {
1589 OfdpaMatchVlanVid ofdpaMatchVlanVid = new OfdpaMatchVlanVid(assignedVlan);
1590 builderToUpdate.extension(ofdpaMatchVlanVid, deviceId);
1591 } else {
1592 builderToUpdate.matchVlanId(assignedVlan);
1593 }
Pier Luigi3bfe32c2017-01-30 09:47:36 -08001594 builderToUpdate.matchEthType(Ethernet.TYPE_IPV4).matchIPDst(ipv4Dst);
1595 log.debug("processing IPv4 multicast specific forwarding objective {} -> next:{}"
1596 + " in dev:{}", fwd.id(), fwd.nextId(), deviceId);
1597 } else {
1598 if (ipv4Dst.prefixLength() == 0) {
1599 if (allowDefaultRoute) {
1600 // The entire IPV4_DST field is wildcarded intentionally
1601 builderToUpdate.matchEthType(Ethernet.TYPE_IPV4);
1602 } else {
1603 // NOTE: The switch does not support matching 0.0.0.0/0
1604 // Split it into 0.0.0.0/1 and 128.0.0.0/1
1605 builderToUpdate.matchEthType(Ethernet.TYPE_IPV4)
1606 .matchIPDst(IpPrefix.valueOf("0.0.0.0/1"));
1607 extBuilder.matchEthType(Ethernet.TYPE_IPV4)
1608 .matchIPDst(IpPrefix.valueOf("128.0.0.0/1"));
1609 }
1610 } else {
1611 builderToUpdate.matchEthType(Ethernet.TYPE_IPV4).matchIPDst(ipv4Dst);
1612 }
1613 log.debug("processing IPv4 unicast specific forwarding objective {} -> next:{}"
1614 + " in dev:{}", fwd.id(), fwd.nextId(), deviceId);
1615 }
1616 return 0;
1617 }
1618
1619 /**
1620 * Helper method to build Ipv6 selector using the selector provided by
1621 * a forwarding objective.
1622 *
1623 * @param builderToUpdate the builder to update
1624 * @param fwd the selector to read
1625 * @return 0 if the update ends correctly. -1 if the matches
1626 * are not yet supported
1627 */
Charles Chan50d900c2018-03-02 13:26:22 -08001628 int buildIpv6Selector(TrafficSelector.Builder builderToUpdate,
Pier Luigi3bfe32c2017-01-30 09:47:36 -08001629 ForwardingObjective fwd) {
1630
1631 TrafficSelector selector = fwd.selector();
1632
1633 IpPrefix ipv6Dst = ((IPCriterion) selector.getCriterion(Criterion.Type.IPV6_DST)).ip();
1634 if (ipv6Dst.isMulticast()) {
Julia Ferguson65428c32017-08-10 18:15:24 +00001635 if (ipv6Dst.prefixLength() != IpAddress.INET6_BIT_LENGTH) {
1636 log.warn("Multicast specific forwarding objective can only be /128");
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 }
1646 if (requireVlanExtensions()) {
1647 OfdpaMatchVlanVid ofdpaMatchVlanVid = new OfdpaMatchVlanVid(assignedVlan);
1648 builderToUpdate.extension(ofdpaMatchVlanVid, deviceId);
1649 } else {
1650 builderToUpdate.matchVlanId(assignedVlan);
1651 }
1652 builderToUpdate.matchEthType(Ethernet.TYPE_IPV6).matchIPv6Dst(ipv6Dst);
1653 log.debug("processing IPv6 multicast specific forwarding objective {} -> next:{}"
1654 + " in dev:{}", fwd.id(), fwd.nextId(), deviceId);
1655 } else {
1656 if (ipv6Dst.prefixLength() != 0) {
1657 builderToUpdate.matchIPv6Dst(ipv6Dst);
1658 }
Pier Luigi3bfe32c2017-01-30 09:47:36 -08001659 builderToUpdate.matchEthType(Ethernet.TYPE_IPV6);
1660 log.debug("processing IPv6 unicast specific forwarding objective {} -> next:{}"
1661 + " in dev:{}", fwd.id(), fwd.nextId(), deviceId);
Julia Ferguson65428c32017-08-10 18:15:24 +00001662 }
Pier Luigi3bfe32c2017-01-30 09:47:36 -08001663 return 0;
1664 }
1665
Charles Chan50d900c2018-03-02 13:26:22 -08001666 FlowRule defaultRoute(ForwardingObjective fwd,
Pier Ventree0ae7a32016-11-23 09:57:42 -08001667 TrafficSelector.Builder complementarySelector,
1668 int forTableId,
1669 TrafficTreatment.Builder tb) {
1670 FlowRule.Builder rule = DefaultFlowRule.builder()
1671 .fromApp(fwd.appId())
1672 .withPriority(fwd.priority())
1673 .forDevice(deviceId)
1674 .withSelector(complementarySelector.build())
1675 .withTreatment(tb.build())
1676 .forTable(forTableId);
1677 if (fwd.permanent()) {
1678 rule.makePermanent();
1679 } else {
1680 rule.makeTemporary(fwd.timeout());
1681 }
1682 return rule.build();
1683 }
1684
Saurav Das4ce45962015-11-24 23:21:05 -08001685 /**
1686 * Handles forwarding rules to the L2 bridging table. Flow actions are not
1687 * allowed in the bridging table - instead we use L2 Interface group or
1688 * L2 flood group
1689 *
1690 * @param fwd the forwarding objective
1691 * @return A collection of flow rules, or an empty set
1692 */
1693 protected Collection<FlowRule> processEthDstSpecific(ForwardingObjective fwd) {
1694 List<FlowRule> rules = new ArrayList<>();
1695
1696 // Build filtered selector
1697 TrafficSelector selector = fwd.selector();
1698 EthCriterion ethCriterion = (EthCriterion) selector
1699 .getCriterion(Criterion.Type.ETH_DST);
1700 VlanIdCriterion vlanIdCriterion = (VlanIdCriterion) selector
1701 .getCriterion(Criterion.Type.VLAN_VID);
1702
1703 if (vlanIdCriterion == null) {
1704 log.warn("Forwarding objective for bridging requires vlan. Not "
1705 + "installing fwd:{} in dev:{}", fwd.id(), deviceId);
1706 fail(fwd, ObjectiveError.BADPARAMS);
1707 return Collections.emptySet();
1708 }
1709
1710 TrafficSelector.Builder filteredSelectorBuilder =
1711 DefaultTrafficSelector.builder();
Yi Tseng3a77b4f2017-02-06 15:02:17 -08001712
1713 if (!ethCriterion.mac().equals(NONE) &&
1714 !ethCriterion.mac().equals(BROADCAST)) {
Saurav Das4ce45962015-11-24 23:21:05 -08001715 filteredSelectorBuilder.matchEthDst(ethCriterion.mac());
1716 log.debug("processing L2 forwarding objective:{} -> next:{} in dev:{}",
1717 fwd.id(), fwd.nextId(), deviceId);
1718 } else {
Yi Tseng3a77b4f2017-02-06 15:02:17 -08001719 // Use wildcard DST_MAC if the MacAddress is None or Broadcast
Saurav Das4ce45962015-11-24 23:21:05 -08001720 log.debug("processing L2 Broadcast forwarding objective:{} -> next:{} "
1721 + "in dev:{} for vlan:{}",
1722 fwd.id(), fwd.nextId(), deviceId, vlanIdCriterion.vlanId());
1723 }
Charles Chand1172632017-03-15 17:33:09 -07001724 if (requireVlanExtensions()) {
1725 OfdpaMatchVlanVid ofdpaMatchVlanVid = new OfdpaMatchVlanVid(vlanIdCriterion.vlanId());
1726 filteredSelectorBuilder.extension(ofdpaMatchVlanVid, deviceId);
1727 } else {
1728 filteredSelectorBuilder.matchVlanId(vlanIdCriterion.vlanId());
1729 }
Saurav Das4ce45962015-11-24 23:21:05 -08001730 TrafficSelector filteredSelector = filteredSelectorBuilder.build();
1731
1732 if (fwd.treatment() != null) {
1733 log.warn("Ignoring traffic treatment in fwd rule {} meant for L2 table"
1734 + "for dev:{}. Expecting only nextId", fwd.id(), deviceId);
1735 }
1736
1737 TrafficTreatment.Builder treatmentBuilder = DefaultTrafficTreatment.builder();
1738 if (fwd.nextId() != null) {
Saurav Das423fe2b2015-12-04 10:52:59 -08001739 NextGroup next = getGroupForNextObjective(fwd.nextId());
Saurav Das4ce45962015-11-24 23:21:05 -08001740 if (next != null) {
1741 List<Deque<GroupKey>> gkeys = appKryo.deserialize(next.data());
1742 // we only need the top level group's key to point the flow to it
1743 Group group = groupService.getGroup(deviceId, gkeys.get(0).peekFirst());
1744 if (group != null) {
1745 treatmentBuilder.deferred().group(group.id());
1746 } else {
1747 log.warn("Group with key:{} for next-id:{} not found in dev:{}",
1748 gkeys.get(0).peekFirst(), fwd.nextId(), deviceId);
1749 fail(fwd, ObjectiveError.GROUPMISSING);
1750 return Collections.emptySet();
1751 }
1752 }
1753 }
1754 treatmentBuilder.immediate().transition(ACL_TABLE);
1755 TrafficTreatment filteredTreatment = treatmentBuilder.build();
1756
1757 // Build bridging table entries
1758 FlowRule.Builder flowRuleBuilder = DefaultFlowRule.builder();
1759 flowRuleBuilder.fromApp(fwd.appId())
1760 .withPriority(fwd.priority())
1761 .forDevice(deviceId)
1762 .withSelector(filteredSelector)
1763 .withTreatment(filteredTreatment)
1764 .forTable(BRIDGING_TABLE);
1765 if (fwd.permanent()) {
1766 flowRuleBuilder.makePermanent();
1767 } else {
1768 flowRuleBuilder.makeTemporary(fwd.timeout());
1769 }
1770 rules.add(flowRuleBuilder.build());
1771 return rules;
1772 }
1773
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001774 //////////////////////////////////////
1775 // Helper Methods and Classes
1776 //////////////////////////////////////
1777
1778 private boolean isSupportedEthTypeObjective(ForwardingObjective fwd) {
1779 TrafficSelector selector = fwd.selector();
1780 EthTypeCriterion ethType = (EthTypeCriterion) selector
1781 .getCriterion(Criterion.Type.ETH_TYPE);
1782 return !((ethType == null) ||
1783 ((ethType.ethType().toShort() != Ethernet.TYPE_IPV4) &&
Pier Ventree0ae7a32016-11-23 09:57:42 -08001784 (ethType.ethType().toShort() != Ethernet.MPLS_UNICAST)) &&
1785 (ethType.ethType().toShort() != Ethernet.TYPE_IPV6));
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001786 }
1787
1788 private boolean isSupportedEthDstObjective(ForwardingObjective fwd) {
1789 TrafficSelector selector = fwd.selector();
1790 EthCriterion ethDst = (EthCriterion) selector
1791 .getCriterion(Criterion.Type.ETH_DST);
1792 VlanIdCriterion vlanId = (VlanIdCriterion) selector
1793 .getCriterion(Criterion.Type.VLAN_VID);
1794 return !(ethDst == null && vlanId == null);
1795 }
1796
Charles Chan50d900c2018-03-02 13:26:22 -08001797 NextGroup getGroupForNextObjective(Integer nextId) {
Saurav Das423fe2b2015-12-04 10:52:59 -08001798 NextGroup next = flowObjectiveStore.getNextGroup(nextId);
1799 if (next != null) {
1800 List<Deque<GroupKey>> gkeys = appKryo.deserialize(next.data());
1801 if (gkeys != null && !gkeys.isEmpty()) {
1802 return next;
1803 } else {
1804 log.warn("Empty next group found in FlowObjective store for "
1805 + "next-id:{} in dev:{}", nextId, deviceId);
1806 }
1807 } else {
1808 log.warn("next-id {} not found in Flow objective store for dev:{}",
1809 nextId, deviceId);
1810 }
1811 return null;
1812 }
1813
Saurav Das24431192016-03-07 19:13:00 -08001814 @Override
1815 public List<String> getNextMappings(NextGroup nextGroup) {
1816 List<String> mappings = new ArrayList<>();
1817 List<Deque<GroupKey>> gkeys = appKryo.deserialize(nextGroup.data());
1818 for (Deque<GroupKey> gkd : gkeys) {
1819 Group lastGroup = null;
Yuta HIGUCHI2dce08a2017-04-20 21:57:48 -07001820 StringBuilder gchain = new StringBuilder();
Saurav Das24431192016-03-07 19:13:00 -08001821 for (GroupKey gk : gkd) {
1822 Group g = groupService.getGroup(deviceId, gk);
Saurav Das8be4e3a2016-03-11 17:19:07 -08001823 if (g == null) {
Saurav Das25190812016-05-27 13:54:07 -07001824 gchain.append(" NoGrp").append(" -->");
Saurav Das8be4e3a2016-03-11 17:19:07 -08001825 continue;
1826 }
1827 gchain.append(" 0x").append(Integer.toHexString(g.id().id()))
1828 .append(" -->");
Saurav Das24431192016-03-07 19:13:00 -08001829 lastGroup = g;
1830 }
1831 // add port information for last group in group-chain
Yuta HIGUCHI2dce08a2017-04-20 21:57:48 -07001832 List<Instruction> lastGroupIns = new ArrayList<>();
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001833 if (lastGroup != null && !lastGroup.buckets().buckets().isEmpty()) {
Saurav Das25190812016-05-27 13:54:07 -07001834 lastGroupIns = lastGroup.buckets().buckets().get(0)
1835 .treatment().allInstructions();
1836 }
1837 for (Instruction i: lastGroupIns) {
Saurav Das24431192016-03-07 19:13:00 -08001838 if (i instanceof OutputInstruction) {
Saurav Das8be4e3a2016-03-11 17:19:07 -08001839 gchain.append(" port:").append(((OutputInstruction) i).port());
Saurav Das24431192016-03-07 19:13:00 -08001840 }
1841 }
Saurav Das8be4e3a2016-03-11 17:19:07 -08001842 mappings.add(gchain.toString());
Saurav Das24431192016-03-07 19:13:00 -08001843 }
1844 return mappings;
1845 }
Charles Chan5b9df8d2016-03-28 22:21:40 -07001846
Saurav Dasa4020382018-02-14 14:14:54 -08001847 /**
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001848 * Utility class that retries sending flows a fixed number of times, even if
1849 * some of the attempts are successful. Used only for forwarding objectives.
1850 */
Alex Yashchuk4caa8e82017-12-08 17:40:05 +02001851 public final class RetryFlows implements Runnable {
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001852 int attempts = MAX_RETRY_ATTEMPTS;
1853 private Collection<FlowRule> retryFlows;
1854 private ForwardingObjective fwd;
1855
Charles Chan50d900c2018-03-02 13:26:22 -08001856 RetryFlows(ForwardingObjective fwd, Collection<FlowRule> retryFlows) {
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001857 this.fwd = fwd;
1858 this.retryFlows = retryFlows;
1859 }
1860
1861 @Override
1862 public void run() {
1863 log.info("RETRY FLOWS ATTEMPT# {} for fwd:{} rules:{}",
1864 MAX_RETRY_ATTEMPTS - attempts, fwd.id(), retryFlows.size());
pier8b3aef42019-03-11 15:14:02 -07001865 sendForwards(Collections.singletonList(Pair.of(fwd, retryFlows)));
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001866 if (--attempts > 0) {
pier8b3aef42019-03-11 15:14:02 -07001867 retryExecutorService.schedule(this, RETRY_MS, TimeUnit.MILLISECONDS);
1868 }
1869 }
1870 }
1871
1872 // Flow rules accumulator for reducing the number of transactions required to the devices.
1873 private final class ForwardingObjectiveAccumulator
1874 extends AbstractAccumulator<Pair<ForwardingObjective, Collection<FlowRule>>> {
1875
1876 ForwardingObjectiveAccumulator(int maxFwd, int maxBatchMS, int maxIdleMS) {
1877 super(TIMER, maxFwd, maxBatchMS, maxIdleMS);
1878 }
1879
1880 @Override
1881 public void processItems(List<Pair<ForwardingObjective, Collection<FlowRule>>> pairs) {
1882 // Triggers creation of a batch using the list of flowrules generated from fwdobjs.
1883 accumulatorExecutorService.execute(new FlowRulesBuilderTask(pairs));
1884 }
1885 }
1886
1887 // Task for building batch of flow rules in a separate thread.
1888 private final class FlowRulesBuilderTask implements Runnable {
1889 private final List<Pair<ForwardingObjective, Collection<FlowRule>>> pairs;
1890
1891 FlowRulesBuilderTask(List<Pair<ForwardingObjective, Collection<FlowRule>>> pairs) {
1892 this.pairs = pairs;
1893 }
1894
1895 @Override
1896 public void run() {
1897 try {
1898 sendForwards(pairs);
1899 } catch (Exception e) {
1900 log.warn("Unable to send forwards", e);
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001901 }
1902 }
1903 }
1904
Saurav Das822c4e22015-10-23 10:51:11 -07001905}