blob: 083a14bdf379a1a76af56a8318e526ebe2aebeba [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;
pier5b9b9e32019-03-11 15:14:02 -070019import com.google.common.collect.Lists;
Yi Tseng47f82dc2017-03-05 22:48:39 -080020import com.google.common.collect.Sets;
pier5b9b9e32019-03-11 15:14:02 -070021import org.apache.commons.lang3.tuple.Pair;
Saurav Das822c4e22015-10-23 10:51:11 -070022import org.onlab.osgi.ServiceDirectory;
Saurav Das822c4e22015-10-23 10:51:11 -070023import org.onlab.packet.Ethernet;
Alex Yashchuka3679532017-12-08 17:40:05 +020024import org.onlab.packet.EthType.EtherType;
Julia Ferguson65428c32017-08-10 18:15:24 +000025import org.onlab.packet.IpAddress;
Charles Chan14967c22015-12-07 11:11:50 -080026import org.onlab.packet.IpPrefix;
Charles Chand57552d2018-03-02 15:41:41 -080027import org.onlab.packet.MacAddress;
Saurav Das822c4e22015-10-23 10:51:11 -070028import org.onlab.packet.VlanId;
pier5b9b9e32019-03-11 15:14:02 -070029import org.onlab.util.AbstractAccumulator;
30import org.onlab.util.Accumulator;
Saurav Das822c4e22015-10-23 10:51:11 -070031import org.onlab.util.KryoNamespace;
32import org.onosproject.core.ApplicationId;
33import org.onosproject.core.CoreService;
Charles Chancad338a2016-09-16 18:03:11 -070034import org.onosproject.driver.extensions.Ofdpa3MplsType;
35import org.onosproject.driver.extensions.Ofdpa3SetMplsType;
Charles Chan14967c22015-12-07 11:11:50 -080036import org.onosproject.driver.extensions.OfdpaMatchVlanVid;
37import org.onosproject.driver.extensions.OfdpaSetVlanVid;
Saurav Das822c4e22015-10-23 10:51:11 -070038import org.onosproject.net.DeviceId;
39import org.onosproject.net.Port;
40import org.onosproject.net.PortNumber;
41import org.onosproject.net.behaviour.NextGroup;
42import org.onosproject.net.behaviour.Pipeliner;
43import org.onosproject.net.behaviour.PipelinerContext;
44import org.onosproject.net.device.DeviceService;
45import org.onosproject.net.driver.AbstractHandlerBehaviour;
46import org.onosproject.net.flow.DefaultFlowRule;
47import org.onosproject.net.flow.DefaultTrafficSelector;
48import org.onosproject.net.flow.DefaultTrafficTreatment;
49import org.onosproject.net.flow.FlowRule;
50import org.onosproject.net.flow.FlowRuleOperations;
51import org.onosproject.net.flow.FlowRuleOperationsContext;
52import org.onosproject.net.flow.FlowRuleService;
53import org.onosproject.net.flow.TrafficSelector;
54import org.onosproject.net.flow.TrafficTreatment;
55import org.onosproject.net.flow.criteria.Criteria;
56import org.onosproject.net.flow.criteria.Criterion;
57import org.onosproject.net.flow.criteria.EthCriterion;
58import org.onosproject.net.flow.criteria.EthTypeCriterion;
59import org.onosproject.net.flow.criteria.IPCriterion;
Pier Ventree0ae7a32016-11-23 09:57:42 -080060import org.onosproject.net.flow.criteria.Icmpv6CodeCriterion;
61import org.onosproject.net.flow.criteria.Icmpv6TypeCriterion;
Saurav Das8a0732e2015-11-20 15:27:53 -080062import org.onosproject.net.flow.criteria.MplsBosCriterion;
63import org.onosproject.net.flow.criteria.MplsCriterion;
Saurav Das822c4e22015-10-23 10:51:11 -070064import org.onosproject.net.flow.criteria.PortCriterion;
Charles Chand9e47c62017-10-05 15:17:15 -070065import org.onosproject.net.flow.criteria.TcpPortCriterion;
66import org.onosproject.net.flow.criteria.UdpPortCriterion;
Saurav Das822c4e22015-10-23 10:51:11 -070067import org.onosproject.net.flow.criteria.VlanIdCriterion;
68import org.onosproject.net.flow.instructions.Instruction;
69import org.onosproject.net.flow.instructions.Instructions.OutputInstruction;
Charles Chan3fe71712018-06-15 18:54:18 -070070import org.onosproject.net.flow.instructions.Instructions.NoActionInstruction;
Saurav Das822c4e22015-10-23 10:51:11 -070071import org.onosproject.net.flow.instructions.L2ModificationInstruction;
Saurav Das8a0732e2015-11-20 15:27:53 -080072import org.onosproject.net.flow.instructions.L2ModificationInstruction.L2SubType;
Alex Yashchuka3679532017-12-08 17:40:05 +020073import org.onosproject.net.flow.instructions.L2ModificationInstruction.ModMplsHeaderInstruction;
Charles Chan14967c22015-12-07 11:11:50 -080074import org.onosproject.net.flow.instructions.L3ModificationInstruction;
75import org.onosproject.net.flow.instructions.L3ModificationInstruction.L3SubType;
Saurav Das822c4e22015-10-23 10:51:11 -070076import org.onosproject.net.flowobjective.FilteringObjective;
77import org.onosproject.net.flowobjective.FlowObjectiveStore;
78import org.onosproject.net.flowobjective.ForwardingObjective;
79import org.onosproject.net.flowobjective.NextObjective;
80import org.onosproject.net.flowobjective.Objective;
81import org.onosproject.net.flowobjective.ObjectiveError;
Saurav Das822c4e22015-10-23 10:51:11 -070082import org.onosproject.net.group.DefaultGroupKey;
83import org.onosproject.net.group.Group;
Saurav Das822c4e22015-10-23 10:51:11 -070084import org.onosproject.net.group.GroupKey;
Saurav Das822c4e22015-10-23 10:51:11 -070085import org.onosproject.net.group.GroupService;
Saurav Das822c4e22015-10-23 10:51:11 -070086import org.onosproject.store.serializers.KryoNamespaces;
87import org.slf4j.Logger;
88
Pier Ventree0ae7a32016-11-23 09:57:42 -080089import java.util.ArrayDeque;
90import java.util.ArrayList;
91import java.util.Collection;
92import java.util.Collections;
93import java.util.Deque;
94import java.util.List;
95import java.util.Objects;
pier5b9b9e32019-03-11 15:14:02 -070096import java.util.Timer;
Pier Ventree0ae7a32016-11-23 09:57:42 -080097import java.util.concurrent.ScheduledExecutorService;
98import java.util.concurrent.TimeUnit;
Harshada Chaundkarb73d5d62019-08-05 15:33:30 +000099import java.util.concurrent.atomic.AtomicBoolean;
Pier Ventree0ae7a32016-11-23 09:57:42 -0800100
101import static java.util.concurrent.Executors.newScheduledThreadPool;
pier5b9b9e32019-03-11 15:14:02 -0700102import static java.util.concurrent.Executors.newSingleThreadScheduledExecutor;
pier497dcb42019-10-04 10:54:07 +0200103import static org.onlab.packet.MacAddress.*;
Pier Ventree0ae7a32016-11-23 09:57:42 -0800104import static org.onlab.util.Tools.groupedThreads;
Yi Tsengef19de12017-04-24 11:33:05 -0700105import static org.onosproject.driver.pipeline.ofdpa.OfdpaGroupHandlerUtility.*;
pier5c817582019-04-17 17:05:08 +0200106import static org.onosproject.driver.pipeline.ofdpa.OfdpaPipelineUtility.*;
pier4baa8cc2019-05-10 13:19:15 +0200107import static org.onosproject.net.flowobjective.ForwardingObjective.Flag.SPECIFIC;
Pier Luigi5af88fe2018-02-01 10:23:12 +0100108import static org.onosproject.net.group.GroupDescription.Type.SELECT;
Pier Ventree0ae7a32016-11-23 09:57:42 -0800109import static org.slf4j.LoggerFactory.getLogger;
Pier Ventre140a8942016-11-02 07:26:38 -0700110import static org.onosproject.net.flow.criteria.Criterion.Type.MPLS_BOS;
Pier Ventree0ae7a32016-11-23 09:57:42 -0800111
Saurav Das822c4e22015-10-23 10:51:11 -0700112/**
113 * Driver for Broadcom's OF-DPA v2.0 TTP.
Saurav Das822c4e22015-10-23 10:51:11 -0700114 */
Charles Chan361154b2016-03-24 10:23:39 -0700115public class Ofdpa2Pipeline extends AbstractHandlerBehaviour implements Pipeliner {
pier5b9b9e32019-03-11 15:14:02 -0700116 // Timer for the accumulator
117 private static final Timer TIMER = new Timer("fwdobj-batching");
118 private Accumulator<Pair<ForwardingObjective, Collection<FlowRule>>> accumulator;
pier5c817582019-04-17 17:05:08 +0200119 // Internal objects
Saurav Das822c4e22015-10-23 10:51:11 -0700120 private final Logger log = getLogger(getClass());
Charles Chan425854b2016-04-11 15:32:12 -0700121 protected ServiceDirectory serviceDirectory;
Saurav Das822c4e22015-10-23 10:51:11 -0700122 protected FlowRuleService flowRuleService;
Charles Chan425854b2016-04-11 15:32:12 -0700123 protected CoreService coreService;
Saurav Das8a0732e2015-11-20 15:27:53 -0800124 protected GroupService groupService;
125 protected FlowObjectiveStore flowObjectiveStore;
Saurav Das822c4e22015-10-23 10:51:11 -0700126 protected DeviceId deviceId;
127 protected ApplicationId driverId;
Saurav Das822c4e22015-10-23 10:51:11 -0700128 protected DeviceService deviceService;
Charles Chan188ebf52015-12-23 00:15:11 -0800129 protected static KryoNamespace appKryo = new KryoNamespace.Builder()
Yi Tsengef19de12017-04-24 11:33:05 -0700130 .register(KryoNamespaces.API)
131 .register(GroupKey.class)
132 .register(DefaultGroupKey.class)
133 .register(OfdpaNextGroup.class)
134 .register(ArrayDeque.class)
135 .build("Ofdpa2Pipeline");
Saurav Das822c4e22015-10-23 10:51:11 -0700136
Charles Chan425854b2016-04-11 15:32:12 -0700137 protected Ofdpa2GroupHandler groupHandler;
Saurav Das822c4e22015-10-23 10:51:11 -0700138
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700139 // flows installations to be retried
pier5b9b9e32019-03-11 15:14:02 -0700140 private ScheduledExecutorService retryExecutorService
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700141 = newScheduledThreadPool(5, groupedThreads("OfdpaPipeliner", "retry-%d", log));
Saurav Das4f980082015-11-05 13:39:15 -0800142
pier5b9b9e32019-03-11 15:14:02 -0700143 // accumulator executor service
144 private ScheduledExecutorService accumulatorExecutorService
145 = newSingleThreadScheduledExecutor(groupedThreads("OfdpaPipeliner", "acc-%d", log));
146
Saurav Das822c4e22015-10-23 10:51:11 -0700147 @Override
148 public void init(DeviceId deviceId, PipelinerContext context) {
Saurav Das822c4e22015-10-23 10:51:11 -0700149 this.deviceId = deviceId;
150
Charles Chan425854b2016-04-11 15:32:12 -0700151 serviceDirectory = context.directory();
Saurav Das822c4e22015-10-23 10:51:11 -0700152 coreService = serviceDirectory.get(CoreService.class);
153 flowRuleService = serviceDirectory.get(FlowRuleService.class);
154 groupService = serviceDirectory.get(GroupService.class);
155 flowObjectiveStore = context.store();
Saurav Das822c4e22015-10-23 10:51:11 -0700156 deviceService = serviceDirectory.get(DeviceService.class);
pier5b9b9e32019-03-11 15:14:02 -0700157 // Init the accumulator, if enabled
pier5c817582019-04-17 17:05:08 +0200158 if (isAccumulatorEnabled(this)) {
pier5b9b9e32019-03-11 15:14:02 -0700159 accumulator = new ForwardingObjectiveAccumulator(context.accumulatorMaxObjectives(),
160 context.accumulatorMaxBatchMillis(),
161 context.accumulatorMaxIdleMillis());
162 }
Saurav Das822c4e22015-10-23 10:51:11 -0700163
Charles Chan40132b32017-01-22 00:19:37 -0800164 initDriverId();
165 initGroupHander(context);
Saurav Das822c4e22015-10-23 10:51:11 -0700166
Saurav Das822c4e22015-10-23 10:51:11 -0700167 initializePipeline();
Saurav Das822c4e22015-10-23 10:51:11 -0700168 }
169
pier4baa8cc2019-05-10 13:19:15 +0200170 void setupAccumulatorForTests(int maxFwd, int maxBatchMS, int maxIdleMS) {
171 if (accumulator == null) {
172 accumulator = new ForwardingObjectiveAccumulator(maxFwd,
173 maxBatchMS,
174 maxIdleMS);
175 }
176 }
177
Charles Chan40132b32017-01-22 00:19:37 -0800178 protected void initDriverId() {
179 driverId = coreService.registerApplication(
180 "org.onosproject.driver.Ofdpa2Pipeline");
181 }
182
183 protected void initGroupHander(PipelinerContext context) {
184 groupHandler = new Ofdpa2GroupHandler();
185 groupHandler.init(deviceId, context);
186 }
187
Saurav Das822c4e22015-10-23 10:51:11 -0700188 protected void initializePipeline() {
Charles Chan188ebf52015-12-23 00:15:11 -0800189 // OF-DPA does not require initializing the pipeline as it puts default
190 // rules automatically in the hardware. However emulation of OFDPA in
191 // software switches does require table-miss-entries.
Saurav Das822c4e22015-10-23 10:51:11 -0700192 }
193
Charles Chand1172632017-03-15 17:33:09 -0700194 /**
Alex Yashchuka3679532017-12-08 17:40:05 +0200195 * Determines whether this pipeline requires MPLS POP instruction.
196 *
197 * @return true to use MPLS POP instruction
198 */
199 public boolean requireMplsPop() {
200 return true;
201 }
202
203 /**
Harshada Chaundkarb73d5d62019-08-05 15:33:30 +0000204 * Determines whether this pipeline requires one additional flow matching on ethType 0x86dd in ACL table.
205 *
206 * @return true to create one additional flow matching on ethType 0x86dd in ACL table
207 */
208 protected boolean requireEthType() {
209 return true;
210 }
211
212
213 /**
Alex Yashchuka3679532017-12-08 17:40:05 +0200214 * Determines whether this pipeline requires MPLS BOS match.
215 *
216 * @return true to use MPLS BOS match
217 */
218 public boolean requireMplsBosMatch() {
219 return true;
220 }
221
222 /**
223 * Determines whether this pipeline requires MPLS TTL decrement and copy.
224 *
225 * @return true to use MPLS TTL decrement and copy
226 */
227 public boolean requireMplsTtlModification() {
228 return true;
229 }
230
231 /**
Charles Chand1172632017-03-15 17:33:09 -0700232 * Determines whether this pipeline requires OFDPA match and set VLAN extensions.
233 *
234 * @return true to use the extensions
235 */
236 protected boolean requireVlanExtensions() {
237 return true;
238 }
239
Saurav Das86d13e82017-04-28 17:03:48 -0700240 /**
241 * Determines whether in-port should be matched on in TMAC table rules.
242 *
243 * @return true if match on in-port should be programmed
244 */
245 protected boolean matchInPortTmacTable() {
246 return true;
247 }
248
Charles Chand9e47c62017-10-05 15:17:15 -0700249 /**
250 * Determines whether matching L4 destination port on IPv6 packets is supported in ACL table.
251 *
252 * @return true if matching L4 destination port on IPv6 packets is supported in ACL table.
253 */
254 protected boolean supportIpv6L4Dst() {
255 return true;
256 }
257
Saurav Dasf3f75942018-01-25 09:49:01 -0800258 /**
259 * Determines whether this driver should continue to retry flows that point
260 * to empty groups. See CORD-554.
261 *
262 * @return true if the driver should retry flows
263 */
264 protected boolean shouldRetry() {
265 return true;
266 }
267
Charles Chand57552d2018-03-02 15:41:41 -0800268 /**
269 * Determines whether this driver requires unicast flow to be installed before multicast flow
270 * in TMAC table.
271 *
272 * @return true if required
273 */
274 protected boolean requireUnicastBeforeMulticast() {
275 return false;
276 }
277
Andrea Campanella84ac4df2018-04-30 11:48:55 +0200278 /**
279 * Determines whether this driver supports installing a clearDeferred action on table 30.
280 *
281 * @return true if required
282 */
283 protected boolean supportsUnicastBlackHole() {
284 return true;
285 }
286
Saurav Das822c4e22015-10-23 10:51:11 -0700287 //////////////////////////////////////
288 // Flow Objectives
289 //////////////////////////////////////
290
291 @Override
292 public void filter(FilteringObjective filteringObjective) {
293 if (filteringObjective.type() == FilteringObjective.Type.PERMIT) {
294 processFilter(filteringObjective,
295 filteringObjective.op() == Objective.Operation.ADD,
296 filteringObjective.appId());
297 } else {
298 // Note that packets that don't match the PERMIT filter are
299 // automatically denied. The DENY filter is used to deny packets
300 // that are otherwise permitted by the PERMIT filter.
301 // Use ACL table flow rules here for DENY filtering objectives
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530302 log.warn("filter objective other than PERMIT currently not supported");
Saurav Das822c4e22015-10-23 10:51:11 -0700303 fail(filteringObjective, ObjectiveError.UNSUPPORTED);
304 }
305 }
306
307 @Override
308 public void forward(ForwardingObjective fwd) {
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700309 Collection<FlowRule> rules = processForward(fwd);
Saurav Das25190812016-05-27 13:54:07 -0700310 if (rules == null || rules.isEmpty()) {
311 // Assumes fail message has already been generated to the objective
312 // context. Returning here prevents spurious pass message to be
313 // generated by FlowRule service for empty flowOps.
314 return;
315 }
pier4baa8cc2019-05-10 13:19:15 +0200316 // Let's accumulate flow rules if accumulator is active and fwd objective is not versatile.
317 // Otherwise send directly, without adding futher delay
318 if (accumulator != null && Objects.equals(fwd.flag(), SPECIFIC)) {
pier5b9b9e32019-03-11 15:14:02 -0700319 accumulator.add(Pair.of(fwd, rules));
320 } else {
321 sendForwards(Collections.singletonList(Pair.of(fwd, rules)));
322 }
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700323 }
324
pier5b9b9e32019-03-11 15:14:02 -0700325 // Builds the batch using the accumulated flow rules
326 private void sendForwards(List<Pair<ForwardingObjective, Collection<FlowRule>>> pairs) {
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700327 FlowRuleOperations.Builder flowOpsBuilder = FlowRuleOperations.builder();
pier5b9b9e32019-03-11 15:14:02 -0700328 log.debug("Sending {} fwd-objs", pairs.size());
329 List<Objective> fwdObjs = Lists.newArrayList();
330 // Iterates over all accumulated flow rules and then build an unique batch
331 pairs.forEach(pair -> {
332 ForwardingObjective fwd = pair.getLeft();
333 Collection<FlowRule> rules = pair.getRight();
334 switch (fwd.op()) {
335 case ADD:
336 rules.stream()
337 .filter(Objects::nonNull)
338 .forEach(flowOpsBuilder::add);
339 log.debug("Applying a add fwd-obj {} to sw:{}", fwd.id(), deviceId);
340 fwdObjs.add(fwd);
341 break;
342 case REMOVE:
343 rules.stream()
344 .filter(Objects::nonNull)
345 .forEach(flowOpsBuilder::remove);
346 log.debug("Deleting a flow rule to sw:{}", deviceId);
347 fwdObjs.add(fwd);
348 break;
349 default:
350 fail(fwd, ObjectiveError.UNKNOWN);
351 log.warn("Unknown forwarding type {}", fwd.op());
352 }
353 });
354 // Finally applies the operations
Saurav Das822c4e22015-10-23 10:51:11 -0700355 flowRuleService.apply(flowOpsBuilder.build(new FlowRuleOperationsContext() {
pier5b9b9e32019-03-11 15:14:02 -0700356
Saurav Das822c4e22015-10-23 10:51:11 -0700357 @Override
358 public void onSuccess(FlowRuleOperations ops) {
pier5b9b9e32019-03-11 15:14:02 -0700359 log.trace("Flow rule operations onSuccess {}", ops);
pier5c817582019-04-17 17:05:08 +0200360 fwdObjs.forEach(OfdpaPipelineUtility::pass);
Saurav Das822c4e22015-10-23 10:51:11 -0700361 }
362
363 @Override
364 public void onError(FlowRuleOperations ops) {
pier5b9b9e32019-03-11 15:14:02 -0700365 ObjectiveError error = ObjectiveError.FLOWINSTALLATIONFAILED;
366 log.warn("Flow rule operations onError {}. Reason = {}", ops, error);
367 fwdObjs.forEach(fwdObj -> fail(fwdObj, error));
Saurav Das822c4e22015-10-23 10:51:11 -0700368 }
369 }));
Saurav Das822c4e22015-10-23 10:51:11 -0700370 }
371
372 @Override
373 public void next(NextObjective nextObjective) {
Saurav Das8a0732e2015-11-20 15:27:53 -0800374 NextGroup nextGroup = flowObjectiveStore.getNextGroup(nextObjective.id());
375 switch (nextObjective.op()) {
376 case ADD:
Saurav Das4f980082015-11-05 13:39:15 -0800377 if (nextGroup != null) {
Saurav Das8a0732e2015-11-20 15:27:53 -0800378 log.warn("Cannot add next {} that already exists in device {}",
379 nextObjective.id(), deviceId);
380 return;
381 }
Saurav Das1547b3f2017-05-05 17:01:08 -0700382 log.debug("Processing NextObjective id {} in dev {} - add group",
Saurav Das8a0732e2015-11-20 15:27:53 -0800383 nextObjective.id(), deviceId);
Charles Chan425854b2016-04-11 15:32:12 -0700384 groupHandler.addGroup(nextObjective);
Saurav Das8a0732e2015-11-20 15:27:53 -0800385 break;
386 case ADD_TO_EXISTING:
387 if (nextGroup != null) {
Saurav Das1547b3f2017-05-05 17:01:08 -0700388 log.debug("Processing NextObjective id {} in dev {} - add bucket",
Saurav Das8a0732e2015-11-20 15:27:53 -0800389 nextObjective.id(), deviceId);
Charles Chan425854b2016-04-11 15:32:12 -0700390 groupHandler.addBucketToGroup(nextObjective, nextGroup);
Saurav Das4f980082015-11-05 13:39:15 -0800391 } else {
Saurav Das8a0732e2015-11-20 15:27:53 -0800392 // it is possible that group-chain has not been fully created yet
Saurav Das423fe2b2015-12-04 10:52:59 -0800393 log.debug("Waiting to add bucket to group for next-id:{} in dev:{}",
394 nextObjective.id(), deviceId);
Yi Tseng47f82dc2017-03-05 22:48:39 -0800395
396 // by design multiple pending bucket is allowed for the group
397 groupHandler.pendingBuckets.compute(nextObjective.id(), (nextId, pendBkts) -> {
398 if (pendBkts == null) {
399 pendBkts = Sets.newHashSet();
400 }
401 pendBkts.add(nextObjective);
402 return pendBkts;
403 });
Saurav Das4f980082015-11-05 13:39:15 -0800404 }
Saurav Das8a0732e2015-11-20 15:27:53 -0800405 break;
406 case REMOVE:
407 if (nextGroup == null) {
408 log.warn("Cannot remove next {} that does not exist in device {}",
409 nextObjective.id(), deviceId);
410 return;
411 }
Saurav Das1547b3f2017-05-05 17:01:08 -0700412 log.debug("Processing NextObjective id {} in dev {} - remove group",
Saurav Das8a0732e2015-11-20 15:27:53 -0800413 nextObjective.id(), deviceId);
Charles Chan425854b2016-04-11 15:32:12 -0700414 groupHandler.removeGroup(nextObjective, nextGroup);
Saurav Das8a0732e2015-11-20 15:27:53 -0800415 break;
416 case REMOVE_FROM_EXISTING:
417 if (nextGroup == null) {
418 log.warn("Cannot remove from next {} that does not exist in device {}",
419 nextObjective.id(), deviceId);
420 return;
421 }
Saurav Das1547b3f2017-05-05 17:01:08 -0700422 log.debug("Processing NextObjective id {} in dev {} - remove bucket",
Saurav Das8a0732e2015-11-20 15:27:53 -0800423 nextObjective.id(), deviceId);
Charles Chan425854b2016-04-11 15:32:12 -0700424 groupHandler.removeBucketFromGroup(nextObjective, nextGroup);
Saurav Das8a0732e2015-11-20 15:27:53 -0800425 break;
Jonghwan Hyun25199f22018-02-12 16:43:45 +0900426 case MODIFY:
427 if (nextGroup == null) {
428 log.warn("Cannot modify next {} that does not exist in device {}",
429 nextObjective.id(), deviceId);
430 return;
431 }
Ruchi Sahotaf77dc0f2019-01-28 01:08:18 +0000432 log.debug("Processing NextObjective id {} in dev {} group {} - modify bucket",
433 nextObjective.id(), deviceId, nextGroup);
Jonghwan Hyun25199f22018-02-12 16:43:45 +0900434 groupHandler.modifyBucketFromGroup(nextObjective, nextGroup);
435 break;
Saurav Dasceccf242017-08-03 18:30:35 -0700436 case VERIFY:
437 if (nextGroup == null) {
438 log.warn("Cannot verify next {} that does not exist in device {}",
439 nextObjective.id(), deviceId);
440 return;
441 }
442 log.debug("Processing NextObjective id {} in dev {} - verify",
443 nextObjective.id(), deviceId);
444 groupHandler.verifyGroup(nextObjective, nextGroup);
445 break;
Saurav Das8a0732e2015-11-20 15:27:53 -0800446 default:
Saurav Das4f980082015-11-05 13:39:15 -0800447 log.warn("Unsupported operation {}", nextObjective.op());
Saurav Das822c4e22015-10-23 10:51:11 -0700448 }
449 }
450
451 //////////////////////////////////////
452 // Flow handling
453 //////////////////////////////////////
454
455 /**
Yi Tseng0fdc07e2017-04-24 19:17:08 -0700456 * As per OFDPA 2.0 TTP, filtering of VLAN ids and MAC addresses (for routing)
457 * configured on switch ports happen in different tables.
Saurav Das822c4e22015-10-23 10:51:11 -0700458 *
459 * @param filt the filtering objective
460 * @param install indicates whether to add or remove the objective
461 * @param applicationId the application that sent this objective
462 */
Saurav Das52025962016-01-28 22:30:01 -0800463 protected void processFilter(FilteringObjective filt,
464 boolean install, ApplicationId applicationId) {
Saurav Das822c4e22015-10-23 10:51:11 -0700465 // This driver only processes filtering criteria defined with switch
466 // ports as the key
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530467 PortCriterion portCriterion = null;
Saurav Das822c4e22015-10-23 10:51:11 -0700468 EthCriterion ethCriterion = null;
469 VlanIdCriterion vidCriterion = null;
Saurav Das822c4e22015-10-23 10:51:11 -0700470 if (!filt.key().equals(Criteria.dummy()) &&
471 filt.key().type() == Criterion.Type.IN_PORT) {
472 portCriterion = (PortCriterion) filt.key();
Saurav Das822c4e22015-10-23 10:51:11 -0700473 }
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530474 if (portCriterion == null) {
475 log.debug("No IN_PORT defined in filtering objective from app: {}",
476 applicationId);
477 } else {
478 log.debug("Received filtering objective for dev/port: {}/{}", deviceId,
479 portCriterion.port());
480 }
Saurav Das822c4e22015-10-23 10:51:11 -0700481 // convert filtering conditions for switch-intfs into flowrules
482 FlowRuleOperations.Builder ops = FlowRuleOperations.builder();
483 for (Criterion criterion : filt.conditions()) {
Yi Tseng0fdc07e2017-04-24 19:17:08 -0700484 switch (criterion.type()) {
485 case ETH_DST:
486 case ETH_DST_MASKED:
487 ethCriterion = (EthCriterion) criterion;
488 break;
489 case VLAN_VID:
490 vidCriterion = (VlanIdCriterion) criterion;
491 break;
492 default:
493 log.warn("Unsupported filter {}", criterion);
494 fail(filt, ObjectiveError.UNSUPPORTED);
495 return;
Saurav Das822c4e22015-10-23 10:51:11 -0700496 }
497 }
498
Saurav Das0e99e2b2015-10-28 12:39:42 -0700499 VlanId assignedVlan = null;
Charles Chane849c192016-01-11 18:28:54 -0800500 if (vidCriterion != null) {
Charles Chand55e84d2016-03-30 17:54:24 -0700501 // Use the VLAN in criterion if metadata is not present and the traffic is tagged
Charles Chan9174e6b2017-12-19 19:55:57 -0800502 if (!vidCriterion.vlanId().equals(VlanId.NONE)) {
Charles Chane849c192016-01-11 18:28:54 -0800503 assignedVlan = vidCriterion.vlanId();
Pier3cc7e662018-03-07 11:42:50 +0100504 }
505 // If the meta VLAN is present let's update the assigned vlan
506 if (filt.meta() != null) {
507 VlanId metaVlan = readVlanFromTreatment(filt.meta());
508 if (metaVlan != null) {
509 assignedVlan = metaVlan;
510 }
Charles Chand55e84d2016-03-30 17:54:24 -0700511 }
Charles Chane849c192016-01-11 18:28:54 -0800512
Charles Chand55e84d2016-03-30 17:54:24 -0700513 if (assignedVlan == null) {
514 log.error("Driver fails to extract VLAN information. "
Frank Wangd8ab0962017-08-11 11:09:30 +0800515 + "Not processing VLAN filters on device {}.", deviceId);
Charles Chand55e84d2016-03-30 17:54:24 -0700516 log.debug("VLAN ID in criterion={}, metadata={}",
517 readVlanFromTreatment(filt.meta()), vidCriterion.vlanId());
518 fail(filt, ObjectiveError.BADPARAMS);
519 return;
Saurav Das0e99e2b2015-10-28 12:39:42 -0700520 }
521 }
522
Yi Tseng3a77b4f2017-02-06 15:02:17 -0800523 if (ethCriterion == null || ethCriterion.mac().equals(NONE)) {
Charles Chan985b12e2016-05-11 19:47:22 -0700524 // NOTE: it is possible that a filtering objective only has vidCriterion
Daniel Ginsburg16e5ed42018-04-30 19:27:19 -0400525 log.debug("filtering objective missing dstMac, won't program TMAC table");
Saurav Das822c4e22015-10-23 10:51:11 -0700526 } else {
Charles Chand57552d2018-03-02 15:41:41 -0800527 MacAddress unicastMac = readEthDstFromTreatment(filt.meta());
Charles Chan206506b2018-03-02 16:43:28 -0800528 List<List<FlowRule>> allStages = processEthDstFilter(portCriterion, ethCriterion,
Charles Chand57552d2018-03-02 15:41:41 -0800529 vidCriterion, assignedVlan, unicastMac, applicationId);
Charles Chan206506b2018-03-02 16:43:28 -0800530 for (List<FlowRule> flowRules : allStages) {
Charles Chand57552d2018-03-02 15:41:41 -0800531 log.trace("Starting a new flow rule stage for TMAC table flow");
Charles Chan206506b2018-03-02 16:43:28 -0800532 ops.newStage();
Charles Chan9174e6b2017-12-19 19:55:57 -0800533
Charles Chan206506b2018-03-02 16:43:28 -0800534 for (FlowRule flowRule : flowRules) {
pier497dcb42019-10-04 10:54:07 +0200535 log.trace("{} flow rule in TMAC table: {} for dev: {}",
536 (install) ? "adding" : "removing", flowRule, deviceId);
Charles Chan206506b2018-03-02 16:43:28 -0800537 if (install) {
538 ops = ops.add(flowRule);
Charles Chan9174e6b2017-12-19 19:55:57 -0800539 } else {
Charles Chan206506b2018-03-02 16:43:28 -0800540 // NOTE: Only remove TMAC flow when there is no more enabled port within the
541 // same VLAN on this device if TMAC doesn't support matching on in_port.
pier497dcb42019-10-04 10:54:07 +0200542 if (filt.meta() != null && filt.meta().clearedDeferred()) {
543 // TMac mcast does not match on the input port - we have to remove it
544 // only if this is the last port
545 FlowRule rule = buildTmacRuleForMcastFromUnicast(flowRule, applicationId);
546 // IPv6 or IPv4 tmac rule
547 if (rule != null) {
548 // Add first the mcast rule and then open a new stage for the unicast
549 ops = ops.remove(rule);
550 ops.newStage();
551 }
552 ops = ops.remove(flowRule);
553 } else if (matchInPortTmacTable()) {
Charles Chan206506b2018-03-02 16:43:28 -0800554 ops = ops.remove(flowRule);
555 } else {
pier497dcb42019-10-04 10:54:07 +0200556 log.debug("Abort TMAC flow removal on {}. " +
557 "Some other ports still share this TMAC flow", deviceId);
Charles Chan206506b2018-03-02 16:43:28 -0800558 }
Charles Chan9174e6b2017-12-19 19:55:57 -0800559 }
560 }
Saurav Das822c4e22015-10-23 10:51:11 -0700561 }
562 }
563
Charles Chan985b12e2016-05-11 19:47:22 -0700564 if (vidCriterion == null) {
565 // NOTE: it is possible that a filtering objective only has ethCriterion
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530566 log.info("filtering objective missing VLAN, cannot program VLAN Table");
Saurav Das822c4e22015-10-23 10:51:11 -0700567 } else {
Charles Chan206506b2018-03-02 16:43:28 -0800568 List<List<FlowRule>> allStages = processVlanIdFilter(
pierb2f71142019-06-10 17:10:26 +0200569 portCriterion, vidCriterion, assignedVlan, applicationId, install);
Charles Chan206506b2018-03-02 16:43:28 -0800570 for (List<FlowRule> flowRules : allStages) {
Charles Chand57552d2018-03-02 15:41:41 -0800571 log.trace("Starting a new flow rule stage for VLAN table flow");
Charles Chan206506b2018-03-02 16:43:28 -0800572 ops.newStage();
Charles Chan14967c22015-12-07 11:11:50 -0800573
Charles Chan206506b2018-03-02 16:43:28 -0800574 for (FlowRule flowRule : flowRules) {
575 log.trace("{} flow rules in VLAN table: {} for dev: {}",
576 (install) ? "adding" : "removing", flowRule, deviceId);
577 ops = install ? ops.add(flowRule) : ops.remove(flowRule);
Charles Chand1172632017-03-15 17:33:09 -0700578 }
Saurav Das822c4e22015-10-23 10:51:11 -0700579 }
580 }
581
Saurav Das822c4e22015-10-23 10:51:11 -0700582 // apply filtering flow rules
583 flowRuleService.apply(ops.build(new FlowRuleOperationsContext() {
584 @Override
585 public void onSuccess(FlowRuleOperations ops) {
Saurav Das018605f2017-02-18 14:05:44 -0800586 log.debug("Applied {} filtering rules in device {}",
Saurav Das822c4e22015-10-23 10:51:11 -0700587 ops.stages().get(0).size(), deviceId);
588 pass(filt);
589 }
590
591 @Override
592 public void onError(FlowRuleOperations ops) {
593 log.info("Failed to apply all filtering rules in dev {}", deviceId);
594 fail(filt, ObjectiveError.FLOWINSTALLATIONFAILED);
595 }
596 }));
597
598 }
599
600 /**
Charles Chand1172632017-03-15 17:33:09 -0700601 * Internal implementation of processVlanIdFilter.
602 * <p>
603 * The is_present bit in set_vlan_vid action is required to be 0 in OFDPA i12.
604 * Since it is non-OF spec, we need an extension treatment for that.
605 * The useVlanExtension must be set to false for OFDPA i12.
606 * </p>
Charles Chan206506b2018-03-02 16:43:28 -0800607 * <p>
608 * NOTE: Separate VLAN filtering rules and assignment rules
609 * into different stages in order to guarantee that filtering rules
610 * always go first, as required by OFDPA.
611 * </p>
Saurav Das0e99e2b2015-10-28 12:39:42 -0700612 *
Charles Chanf9e98652016-09-07 16:54:23 -0700613 * @param portCriterion port on device for which this filter is programmed
614 * @param vidCriterion vlan assigned to port, or NONE for untagged
615 * @param assignedVlan assigned vlan-id for untagged packets
616 * @param applicationId for application programming this filter
pierb2f71142019-06-10 17:10:26 +0200617 * @param install indicates whether to add or remove the objective
Charles Chan206506b2018-03-02 16:43:28 -0800618 * @return stages of flow rules for port-vlan filters
Saurav Das822c4e22015-10-23 10:51:11 -0700619 */
Charles Chan206506b2018-03-02 16:43:28 -0800620 protected List<List<FlowRule>> processVlanIdFilter(PortCriterion portCriterion,
pierb2f71142019-06-10 17:10:26 +0200621 VlanIdCriterion vidCriterion,
622 VlanId assignedVlan,
623 ApplicationId applicationId,
624 boolean install) {
Charles Chan206506b2018-03-02 16:43:28 -0800625 List<FlowRule> filteringRules = new ArrayList<>();
626 List<FlowRule> assignmentRules = new ArrayList<>();
Saurav Das822c4e22015-10-23 10:51:11 -0700627 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
628 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
Charles Chan14967c22015-12-07 11:11:50 -0800629 TrafficSelector.Builder preSelector = null;
630 TrafficTreatment.Builder preTreatment = null;
631
Saurav Das4f980082015-11-05 13:39:15 -0800632 treatment.transition(TMAC_TABLE);
633
Saurav Das822c4e22015-10-23 10:51:11 -0700634 if (vidCriterion.vlanId() == VlanId.NONE) {
635 // untagged packets are assigned vlans
Charles Chand1172632017-03-15 17:33:09 -0700636 preSelector = DefaultTrafficSelector.builder();
637 if (requireVlanExtensions()) {
638 OfdpaMatchVlanVid ofdpaMatchVlanVid = new OfdpaMatchVlanVid(VlanId.NONE);
639 selector.extension(ofdpaMatchVlanVid, deviceId);
Charles Chanf9e98652016-09-07 16:54:23 -0700640 OfdpaSetVlanVid ofdpaSetVlanVid = new OfdpaSetVlanVid(assignedVlan);
641 treatment.extension(ofdpaSetVlanVid, deviceId);
Charles Chand1172632017-03-15 17:33:09 -0700642
643 OfdpaMatchVlanVid preOfdpaMatchVlanVid = new OfdpaMatchVlanVid(assignedVlan);
644 preSelector.extension(preOfdpaMatchVlanVid, deviceId);
Charles Chanf9e98652016-09-07 16:54:23 -0700645 } else {
Charles Chand1172632017-03-15 17:33:09 -0700646 selector.matchVlanId(VlanId.NONE);
Charles Chanf9e98652016-09-07 16:54:23 -0700647 treatment.setVlanId(assignedVlan);
Charles Chand1172632017-03-15 17:33:09 -0700648
649 preSelector.matchVlanId(assignedVlan);
650 }
651 preTreatment = DefaultTrafficTreatment.builder().transition(TMAC_TABLE);
652 } else {
653 if (requireVlanExtensions()) {
654 OfdpaMatchVlanVid ofdpaMatchVlanVid = new OfdpaMatchVlanVid(vidCriterion.vlanId());
655 selector.extension(ofdpaMatchVlanVid, deviceId);
656 } else {
657 selector.matchVlanId(vidCriterion.vlanId());
Charles Chanf9e98652016-09-07 16:54:23 -0700658 }
Charles Chan14967c22015-12-07 11:11:50 -0800659
Charles Chand55e84d2016-03-30 17:54:24 -0700660 if (!assignedVlan.equals(vidCriterion.vlanId())) {
Charles Chand1172632017-03-15 17:33:09 -0700661 if (requireVlanExtensions()) {
Charles Chanc03782d2017-01-31 13:57:55 -0800662 OfdpaSetVlanVid ofdpaSetVlanVid = new OfdpaSetVlanVid(assignedVlan);
663 treatment.extension(ofdpaSetVlanVid, deviceId);
664 } else {
665 treatment.setVlanId(assignedVlan);
666 }
Charles Chand55e84d2016-03-30 17:54:24 -0700667 }
Saurav Das822c4e22015-10-23 10:51:11 -0700668 }
Saurav Das822c4e22015-10-23 10:51:11 -0700669
670 // ofdpa cannot match on ALL portnumber, so we need to use separate
671 // rules for each port.
Charles Chan14967c22015-12-07 11:11:50 -0800672 List<PortNumber> portnums = new ArrayList<>();
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530673 if (portCriterion != null) {
674 if (PortNumber.ALL.equals(portCriterion.port())) {
675 for (Port port : deviceService.getPorts(deviceId)) {
676 if (port.number().toLong() > 0 && port.number().toLong() < OFPP_MAX) {
677 portnums.add(port.number());
678 }
Saurav Das822c4e22015-10-23 10:51:11 -0700679 }
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530680 } else {
681 portnums.add(portCriterion.port());
Saurav Das822c4e22015-10-23 10:51:11 -0700682 }
683 } else {
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530684 log.warn("Filtering Objective missing Port Criterion . " +
Charles Chan206506b2018-03-02 16:43:28 -0800685 "VLAN Table cannot be programmed for {}", deviceId);
Saurav Das822c4e22015-10-23 10:51:11 -0700686 }
Saurav Das4f980082015-11-05 13:39:15 -0800687
Saurav Das822c4e22015-10-23 10:51:11 -0700688 for (PortNumber pnum : portnums) {
Saurav Das4f980082015-11-05 13:39:15 -0800689 // create rest of flowrule
Saurav Das822c4e22015-10-23 10:51:11 -0700690 selector.matchInPort(pnum);
691 FlowRule rule = DefaultFlowRule.builder()
692 .forDevice(deviceId)
693 .withSelector(selector.build())
694 .withTreatment(treatment.build())
695 .withPriority(DEFAULT_PRIORITY)
696 .fromApp(applicationId)
697 .makePermanent()
698 .forTable(VLAN_TABLE).build();
Charles Chan206506b2018-03-02 16:43:28 -0800699 assignmentRules.add(rule);
Charles Chan14967c22015-12-07 11:11:50 -0800700
701 if (preSelector != null) {
702 preSelector.matchInPort(pnum);
703 FlowRule preRule = DefaultFlowRule.builder()
704 .forDevice(deviceId)
705 .withSelector(preSelector.build())
706 .withTreatment(preTreatment.build())
707 .withPriority(DEFAULT_PRIORITY)
708 .fromApp(applicationId)
709 .makePermanent()
710 .forTable(VLAN_TABLE).build();
Charles Chan206506b2018-03-02 16:43:28 -0800711 filteringRules.add(preRule);
Charles Chan14967c22015-12-07 11:11:50 -0800712 }
Saurav Das822c4e22015-10-23 10:51:11 -0700713 }
pierb2f71142019-06-10 17:10:26 +0200714 return install ? ImmutableList.of(filteringRules, assignmentRules) :
715 ImmutableList.of(assignmentRules, filteringRules);
Saurav Das822c4e22015-10-23 10:51:11 -0700716 }
717
718 /**
719 * Allows routed packets with correct destination MAC to be directed
Charles Chand57552d2018-03-02 15:41:41 -0800720 * to unicast routing table, multicast routing table or MPLS forwarding table.
Saurav Das822c4e22015-10-23 10:51:11 -0700721 *
722 * @param portCriterion port on device for which this filter is programmed
723 * @param ethCriterion dstMac of device for which is filter is programmed
724 * @param vidCriterion vlan assigned to port, or NONE for untagged
Saurav Das0e99e2b2015-10-28 12:39:42 -0700725 * @param assignedVlan assigned vlan-id for untagged packets
Charles Chand57552d2018-03-02 15:41:41 -0800726 * @param unicastMac some switches require a unicast TMAC flow to be programmed before multicast
727 * TMAC flow. This MAC address will be used for the unicast TMAC flow.
728 * This is unused if the filtering objective is a unicast.
Saurav Das822c4e22015-10-23 10:51:11 -0700729 * @param applicationId for application programming this filter
Charles Chan206506b2018-03-02 16:43:28 -0800730 * @return stages of flow rules for port-vlan filters
Saurav Das822c4e22015-10-23 10:51:11 -0700731
732 */
Charles Chan206506b2018-03-02 16:43:28 -0800733 protected List<List<FlowRule>> processEthDstFilter(PortCriterion portCriterion,
Saurav Das822c4e22015-10-23 10:51:11 -0700734 EthCriterion ethCriterion,
735 VlanIdCriterion vidCriterion,
Saurav Das0e99e2b2015-10-28 12:39:42 -0700736 VlanId assignedVlan,
Charles Chand57552d2018-03-02 15:41:41 -0800737 MacAddress unicastMac,
Saurav Das822c4e22015-10-23 10:51:11 -0700738 ApplicationId applicationId) {
Charles Chan5270ed02016-01-30 23:22:37 -0800739 // Consider PortNumber.ANY as wildcard. Match ETH_DST only
Sivachidambaram Subramanianc598d062017-05-16 22:42:06 +0530740 if (portCriterion != null && PortNumber.ANY.equals(portCriterion.port())) {
Charles Chan5270ed02016-01-30 23:22:37 -0800741 return processEthDstOnlyFilter(ethCriterion, applicationId);
742 }
743
Charles Chan5b9df8d2016-03-28 22:21:40 -0700744 // Multicast MAC
745 if (ethCriterion.mask() != null) {
Charles Chand57552d2018-03-02 15:41:41 -0800746 return processMcastEthDstFilter(ethCriterion, assignedVlan, unicastMac, applicationId);
Charles Chan5b9df8d2016-03-28 22:21:40 -0700747 }
748
Saurav Das822c4e22015-10-23 10:51:11 -0700749 //handling untagged packets via assigned VLAN
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530750 if (vidCriterion != null && vidCriterion.vlanId() == VlanId.NONE) {
Saurav Das0e99e2b2015-10-28 12:39:42 -0700751 vidCriterion = (VlanIdCriterion) Criteria.matchVlanId(assignedVlan);
Saurav Das822c4e22015-10-23 10:51:11 -0700752 }
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530753 List<FlowRule> rules = new ArrayList<>();
754 OfdpaMatchVlanVid ofdpaMatchVlanVid = null;
755 if (vidCriterion != null && requireVlanExtensions()) {
756 ofdpaMatchVlanVid = new OfdpaMatchVlanVid(vidCriterion.vlanId());
757 }
Saurav Das822c4e22015-10-23 10:51:11 -0700758 // ofdpa cannot match on ALL portnumber, so we need to use separate
759 // rules for each port.
Yuta HIGUCHI2dce08a2017-04-20 21:57:48 -0700760 List<PortNumber> portnums = new ArrayList<>();
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530761 if (portCriterion != null) {
762 if (PortNumber.ALL.equals(portCriterion.port())) {
763 for (Port port : deviceService.getPorts(deviceId)) {
764 if (port.number().toLong() > 0 && port.number().toLong() < OFPP_MAX) {
765 portnums.add(port.number());
766 }
Saurav Das822c4e22015-10-23 10:51:11 -0700767 }
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530768 } else {
769 portnums.add(portCriterion.port());
770 }
771 for (PortNumber pnum : portnums) {
772 rules.add(buildTmacRuleForIpv4(ethCriterion,
773 vidCriterion,
774 ofdpaMatchVlanVid,
775 applicationId,
776 pnum));
777 rules.add(buildTmacRuleForMpls(ethCriterion,
778 vidCriterion,
779 ofdpaMatchVlanVid,
780 applicationId,
781 pnum));
782 rules.add(buildTmacRuleForIpv6(ethCriterion,
783 vidCriterion,
784 ofdpaMatchVlanVid,
785 applicationId,
786 pnum));
Saurav Das822c4e22015-10-23 10:51:11 -0700787 }
788 } else {
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530789 rules.add(buildTmacRuleForIpv4(ethCriterion,
790 vidCriterion,
791 ofdpaMatchVlanVid,
792 applicationId,
793 null));
794 rules.add(buildTmacRuleForMpls(ethCriterion,
795 vidCriterion,
796 ofdpaMatchVlanVid,
797 applicationId,
798 null));
799 rules.add(buildTmacRuleForIpv6(ethCriterion,
800 vidCriterion,
801 ofdpaMatchVlanVid,
802 applicationId,
803 null));
Saurav Das822c4e22015-10-23 10:51:11 -0700804 }
Charles Chan206506b2018-03-02 16:43:28 -0800805 return ImmutableList.of(rules);
Saurav Das822c4e22015-10-23 10:51:11 -0700806 }
807
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530808 /**
809 * Builds TMAC rules for IPv4 packets.
810 *
Charles Chan9174e6b2017-12-19 19:55:57 -0800811 * @param ethCriterion dst mac matching
812 * @param vidCriterion vlan id assigned to the port
813 * @param ofdpaMatchVlanVid OFDPA vlan id matching
814 * @param applicationId application id
815 * @param pnum port number
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530816 * @return TMAC rule for IPV4 packets
817 */
818 private FlowRule buildTmacRuleForIpv4(EthCriterion ethCriterion,
819 VlanIdCriterion vidCriterion,
820 OfdpaMatchVlanVid ofdpaMatchVlanVid,
821 ApplicationId applicationId,
822 PortNumber pnum) {
823 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
824 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
825 if (pnum != null) {
826 if (matchInPortTmacTable()) {
827 selector.matchInPort(pnum);
828 } else {
Saurav Das73a6cc12017-12-05 15:00:23 -0800829 log.debug("Pipeline does not support IN_PORT matching in TMAC table, " +
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530830 "ignoring the IN_PORT criteria");
831 }
832 }
833 if (vidCriterion != null) {
834 if (requireVlanExtensions()) {
835 selector.extension(ofdpaMatchVlanVid, deviceId);
836 } else {
837 selector.matchVlanId(vidCriterion.vlanId());
838 }
839 }
840 selector.matchEthType(Ethernet.TYPE_IPV4);
841 selector.matchEthDst(ethCriterion.mac());
842 treatment.transition(UNICAST_ROUTING_TABLE);
843 return DefaultFlowRule.builder()
844 .forDevice(deviceId)
845 .withSelector(selector.build())
846 .withTreatment(treatment.build())
847 .withPriority(DEFAULT_PRIORITY)
848 .fromApp(applicationId)
849 .makePermanent()
850 .forTable(TMAC_TABLE).build();
851 }
852
853 /**
854 * Builds TMAC rule for MPLS packets.
855 *
Charles Chan9174e6b2017-12-19 19:55:57 -0800856 * @param ethCriterion dst mac matching
857 * @param vidCriterion vlan id assigned to the port
858 * @param ofdpaMatchVlanVid OFDPA vlan id matching
859 * @param applicationId application id
860 * @param pnum port number
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530861 * @return TMAC rule for MPLS packets
862 */
863 private FlowRule buildTmacRuleForMpls(EthCriterion ethCriterion,
864 VlanIdCriterion vidCriterion,
865 OfdpaMatchVlanVid ofdpaMatchVlanVid,
866 ApplicationId applicationId,
867 PortNumber pnum) {
868 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
869 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
870 if (pnum != null) {
871 if (matchInPortTmacTable()) {
872 selector.matchInPort(pnum);
873 } else {
Saurav Das73a6cc12017-12-05 15:00:23 -0800874 log.debug("Pipeline does not support IN_PORT matching in TMAC table, " +
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530875 "ignoring the IN_PORT criteria");
876 }
877 }
878 if (vidCriterion != null) {
879 if (requireVlanExtensions()) {
880 selector.extension(ofdpaMatchVlanVid, deviceId);
881 } else {
882 selector.matchVlanId(vidCriterion.vlanId());
883 }
884 }
885 selector.matchEthType(Ethernet.MPLS_UNICAST);
886 selector.matchEthDst(ethCriterion.mac());
887 treatment.transition(MPLS_TABLE_0);
888 return DefaultFlowRule.builder()
889 .forDevice(deviceId)
890 .withSelector(selector.build())
891 .withTreatment(treatment.build())
892 .withPriority(DEFAULT_PRIORITY)
893 .fromApp(applicationId)
894 .makePermanent()
895 .forTable(TMAC_TABLE).build();
896 }
897
898 /**
899 * Builds TMAC rules for IPv6 packets.
900 *
Charles Chan9174e6b2017-12-19 19:55:57 -0800901 * @param ethCriterion dst mac matching
902 * @param vidCriterion vlan id assigned to the port
903 * @param ofdpaMatchVlanVid OFDPA vlan id matching
904 * @param applicationId application id
905 * @param pnum port number
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530906 * @return TMAC rule for IPV6 packets
907 */
908 private FlowRule buildTmacRuleForIpv6(EthCriterion ethCriterion,
909 VlanIdCriterion vidCriterion,
910 OfdpaMatchVlanVid ofdpaMatchVlanVid,
911 ApplicationId applicationId,
912 PortNumber pnum) {
913 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
914 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
915 if (pnum != null) {
916 if (matchInPortTmacTable()) {
917 selector.matchInPort(pnum);
918 } else {
Saurav Das73a6cc12017-12-05 15:00:23 -0800919 log.debug("Pipeline does not support IN_PORT matching in TMAC table, " +
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530920 "ignoring the IN_PORT criteria");
921 }
922 }
923 if (vidCriterion != null) {
924 if (requireVlanExtensions()) {
925 selector.extension(ofdpaMatchVlanVid, deviceId);
926 } else {
927 selector.matchVlanId(vidCriterion.vlanId());
928 }
929 }
930 selector.matchEthType(Ethernet.TYPE_IPV6);
931 selector.matchEthDst(ethCriterion.mac());
932 treatment.transition(UNICAST_ROUTING_TABLE);
933 return DefaultFlowRule.builder()
934 .forDevice(deviceId)
935 .withSelector(selector.build())
936 .withTreatment(treatment.build())
937 .withPriority(DEFAULT_PRIORITY)
938 .fromApp(applicationId)
939 .makePermanent()
940 .forTable(TMAC_TABLE).build();
941 }
942
Charles Chan206506b2018-03-02 16:43:28 -0800943 protected List<List<FlowRule>> processEthDstOnlyFilter(EthCriterion ethCriterion,
Yi Tsengef19de12017-04-24 11:33:05 -0700944 ApplicationId applicationId) {
Pier Luigi0e358632017-01-31 09:35:05 -0800945 ImmutableList.Builder<FlowRule> builder = ImmutableList.builder();
946
Charles Chan5270ed02016-01-30 23:22:37 -0800947 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
948 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
949 selector.matchEthType(Ethernet.TYPE_IPV4);
950 selector.matchEthDst(ethCriterion.mac());
951 treatment.transition(UNICAST_ROUTING_TABLE);
952 FlowRule rule = DefaultFlowRule.builder()
953 .forDevice(deviceId)
954 .withSelector(selector.build())
955 .withTreatment(treatment.build())
956 .withPriority(DEFAULT_PRIORITY)
957 .fromApp(applicationId)
958 .makePermanent()
959 .forTable(TMAC_TABLE).build();
Pier Luigi0e358632017-01-31 09:35:05 -0800960 builder.add(rule);
961
962 selector = DefaultTrafficSelector.builder();
963 treatment = DefaultTrafficTreatment.builder();
964 selector.matchEthType(Ethernet.TYPE_IPV6);
965 selector.matchEthDst(ethCriterion.mac());
966 treatment.transition(UNICAST_ROUTING_TABLE);
967 rule = DefaultFlowRule.builder()
968 .forDevice(deviceId)
969 .withSelector(selector.build())
970 .withTreatment(treatment.build())
971 .withPriority(DEFAULT_PRIORITY)
972 .fromApp(applicationId)
973 .makePermanent()
974 .forTable(TMAC_TABLE).build();
Charles Chan206506b2018-03-02 16:43:28 -0800975 return ImmutableList.of(builder.add(rule).build());
Charles Chan5270ed02016-01-30 23:22:37 -0800976 }
977
pier497dcb42019-10-04 10:54:07 +0200978 private FlowRule buildTmacRuleForMcastFromUnicast(FlowRule tmacRuleForUnicast, ApplicationId applicationId) {
979 final TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
980 final TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
981 FlowRule rule;
982 // Build the selector
983 for (Criterion criterion : tmacRuleForUnicast.selector().criteria()) {
984 if (criterion instanceof VlanIdCriterion) {
985 if (requireVlanExtensions()) {
986 OfdpaMatchVlanVid ofdpaMatchVlanVid = new OfdpaMatchVlanVid(((VlanIdCriterion) criterion).vlanId());
987 selector.extension(ofdpaMatchVlanVid, deviceId);
988 } else {
989 selector.add(criterion);
990 }
991 } else if (criterion instanceof EthTypeCriterion) {
992 selector.add(criterion);
993 if (Objects.equals(((EthTypeCriterion) criterion).ethType(),
994 EtherType.IPV4.ethType())) {
995 selector.matchEthDstMasked(IPV4_MULTICAST, IPV4_MULTICAST_MASK);
996 } else if (Objects.equals(((EthTypeCriterion) criterion).ethType(),
997 EtherType.IPV6.ethType())) {
998 selector.matchEthDstMasked(IPV6_MULTICAST, IPV6_MULTICAST_MASK);
999 } else {
1000 // We don't need for mpls rules
1001 return null;
1002 }
1003 }
1004 }
1005 // Build the treatment
1006 treatment.transition(MULTICAST_ROUTING_TABLE);
1007 // Build the flowrule
1008 rule = DefaultFlowRule.builder()
1009 .forDevice(deviceId)
1010 .withSelector(selector.build())
1011 .withTreatment(treatment.build())
1012 .withPriority(DEFAULT_PRIORITY)
1013 .fromApp(applicationId)
1014 .makePermanent()
1015 .forTable(TMAC_TABLE).build();
pier05551092019-12-06 12:39:51 +01001016 log.debug("Building TMac Mcast flowRule {}", rule);
pier497dcb42019-10-04 10:54:07 +02001017 return rule;
1018 }
1019
Charles Chan206506b2018-03-02 16:43:28 -08001020 List<List<FlowRule>> processMcastEthDstFilter(EthCriterion ethCriterion,
Charles Chanb4879a52017-10-20 19:09:16 -07001021 VlanId assignedVlan,
Charles Chand57552d2018-03-02 15:41:41 -08001022 MacAddress unicastMac,
Yi Tsengef19de12017-04-24 11:33:05 -07001023 ApplicationId applicationId) {
Charles Chand57552d2018-03-02 15:41:41 -08001024 ImmutableList.Builder<FlowRule> unicastFlows = ImmutableList.builder();
1025 ImmutableList.Builder<FlowRule> multicastFlows = ImmutableList.builder();
1026 TrafficSelector.Builder selector;
1027 TrafficTreatment.Builder treatment;
Charles Chanb4879a52017-10-20 19:09:16 -07001028 FlowRule rule;
Julia Ferguson65428c32017-08-10 18:15:24 +00001029
Charles Chanb4879a52017-10-20 19:09:16 -07001030 if (IPV4_MULTICAST.equals(ethCriterion.mac())) {
Charles Chand57552d2018-03-02 15:41:41 -08001031 if (requireUnicastBeforeMulticast()) {
1032 selector = DefaultTrafficSelector.builder();
1033 treatment = DefaultTrafficTreatment.builder();
1034 selector.matchEthType(Ethernet.TYPE_IPV4);
1035 selector.matchEthDst(unicastMac);
Charles Chan893a1d72018-05-10 22:19:25 +00001036 selector.matchVlanId(assignedVlan);
Charles Chand57552d2018-03-02 15:41:41 -08001037 treatment.transition(UNICAST_ROUTING_TABLE);
1038 rule = DefaultFlowRule.builder()
1039 .forDevice(deviceId)
1040 .withSelector(selector.build())
1041 .withTreatment(treatment.build())
Charles Chan893a1d72018-05-10 22:19:25 +00001042 .withPriority(DEFAULT_PRIORITY)
Charles Chand57552d2018-03-02 15:41:41 -08001043 .fromApp(applicationId)
1044 .makePermanent()
1045 .forTable(TMAC_TABLE).build();
1046 unicastFlows.add(rule);
1047 }
1048
1049 selector = DefaultTrafficSelector.builder();
1050 treatment = DefaultTrafficTreatment.builder();
Charles Chanb4879a52017-10-20 19:09:16 -07001051 selector.matchEthType(Ethernet.TYPE_IPV4);
1052 selector.matchEthDstMasked(ethCriterion.mac(), ethCriterion.mask());
Charles Chan893a1d72018-05-10 22:19:25 +00001053 selector.matchVlanId(assignedVlan);
Charles Chanb4879a52017-10-20 19:09:16 -07001054 treatment.transition(MULTICAST_ROUTING_TABLE);
1055 rule = DefaultFlowRule.builder()
1056 .forDevice(deviceId)
1057 .withSelector(selector.build())
1058 .withTreatment(treatment.build())
Charles Chan893a1d72018-05-10 22:19:25 +00001059 .withPriority(DEFAULT_PRIORITY)
Charles Chanb4879a52017-10-20 19:09:16 -07001060 .fromApp(applicationId)
1061 .makePermanent()
1062 .forTable(TMAC_TABLE).build();
Charles Chand57552d2018-03-02 15:41:41 -08001063 multicastFlows.add(rule);
Charles Chanb4879a52017-10-20 19:09:16 -07001064 }
1065
1066 if (IPV6_MULTICAST.equals(ethCriterion.mac())) {
Charles Chand57552d2018-03-02 15:41:41 -08001067 if (requireUnicastBeforeMulticast()) {
1068 selector = DefaultTrafficSelector.builder();
1069 treatment = DefaultTrafficTreatment.builder();
1070 selector.matchEthType(Ethernet.TYPE_IPV6);
1071 selector.matchEthDst(unicastMac);
Charles Chan893a1d72018-05-10 22:19:25 +00001072 selector.matchVlanId(assignedVlan);
Charles Chand57552d2018-03-02 15:41:41 -08001073 treatment.transition(UNICAST_ROUTING_TABLE);
1074 rule = DefaultFlowRule.builder()
1075 .forDevice(deviceId)
1076 .withSelector(selector.build())
1077 .withTreatment(treatment.build())
Charles Chan893a1d72018-05-10 22:19:25 +00001078 .withPriority(DEFAULT_PRIORITY)
Charles Chand57552d2018-03-02 15:41:41 -08001079 .fromApp(applicationId)
1080 .makePermanent()
1081 .forTable(TMAC_TABLE).build();
1082 unicastFlows.add(rule);
1083 }
1084
Charles Chanb4879a52017-10-20 19:09:16 -07001085 selector = DefaultTrafficSelector.builder();
1086 treatment = DefaultTrafficTreatment.builder();
1087 selector.matchEthType(Ethernet.TYPE_IPV6);
1088 selector.matchEthDstMasked(ethCriterion.mac(), ethCriterion.mask());
Charles Chan893a1d72018-05-10 22:19:25 +00001089 selector.matchVlanId(assignedVlan);
Charles Chanb4879a52017-10-20 19:09:16 -07001090 treatment.transition(MULTICAST_ROUTING_TABLE);
1091 rule = DefaultFlowRule.builder()
1092 .forDevice(deviceId)
1093 .withSelector(selector.build())
1094 .withTreatment(treatment.build())
Charles Chan893a1d72018-05-10 22:19:25 +00001095 .withPriority(DEFAULT_PRIORITY)
Charles Chanb4879a52017-10-20 19:09:16 -07001096 .fromApp(applicationId)
1097 .makePermanent()
1098 .forTable(TMAC_TABLE).build();
Charles Chand57552d2018-03-02 15:41:41 -08001099 multicastFlows.add(rule);
Charles Chanb4879a52017-10-20 19:09:16 -07001100 }
Charles Chand57552d2018-03-02 15:41:41 -08001101 return ImmutableList.of(unicastFlows.build(), multicastFlows.build());
Charles Chan5b9df8d2016-03-28 22:21:40 -07001102 }
1103
Saurav Das822c4e22015-10-23 10:51:11 -07001104 private Collection<FlowRule> processForward(ForwardingObjective fwd) {
1105 switch (fwd.flag()) {
1106 case SPECIFIC:
1107 return processSpecific(fwd);
1108 case VERSATILE:
1109 return processVersatile(fwd);
1110 default:
1111 fail(fwd, ObjectiveError.UNKNOWN);
1112 log.warn("Unknown forwarding flag {}", fwd.flag());
1113 }
1114 return Collections.emptySet();
1115 }
1116
1117 /**
1118 * In the OF-DPA 2.0 pipeline, versatile forwarding objectives go to the
1119 * ACL table.
1120 * @param fwd the forwarding objective of type 'versatile'
1121 * @return a collection of flow rules to be sent to the switch. An empty
1122 * collection may be returned if there is a problem in processing
1123 * the flow rule
1124 */
Saurav Das52025962016-01-28 22:30:01 -08001125 protected Collection<FlowRule> processVersatile(ForwardingObjective fwd) {
Saurav Das018605f2017-02-18 14:05:44 -08001126 log.debug("Processing versatile forwarding objective:{} in dev:{}",
Saurav Dasd2fded02016-12-02 15:43:47 -08001127 fwd.id(), deviceId);
Harshada Chaundkarb73d5d62019-08-05 15:33:30 +00001128 List<FlowRule> flowRules = new ArrayList<>();
1129 final AtomicBoolean ethTypeUsed = new AtomicBoolean(false);
Saurav Das822c4e22015-10-23 10:51:11 -07001130
Saurav Das822c4e22015-10-23 10:51:11 -07001131 if (fwd.nextId() == null && fwd.treatment() == null) {
1132 log.error("Forwarding objective {} from {} must contain "
1133 + "nextId or Treatment", fwd.selector(), fwd.appId());
Zsolt Haraszti9faab752016-02-17 15:55:20 -08001134 fail(fwd, ObjectiveError.BADPARAMS);
Saurav Das822c4e22015-10-23 10:51:11 -07001135 return Collections.emptySet();
1136 }
Saurav Das49cb5a12016-01-16 22:54:07 -08001137
Saurav Das77b5e902016-01-27 17:01:59 -08001138 TrafficSelector.Builder sbuilder = DefaultTrafficSelector.builder();
1139 fwd.selector().criteria().forEach(criterion -> {
1140 if (criterion instanceof VlanIdCriterion) {
1141 VlanId vlanId = ((VlanIdCriterion) criterion).vlanId();
1142 // ensure that match does not include vlan = NONE as OF-DPA does not
1143 // match untagged packets this way in the ACL table.
1144 if (vlanId.equals(VlanId.NONE)) {
1145 return;
1146 }
Charles Chand1172632017-03-15 17:33:09 -07001147 if (requireVlanExtensions()) {
1148 OfdpaMatchVlanVid ofdpaMatchVlanVid = new OfdpaMatchVlanVid(vlanId);
1149 sbuilder.extension(ofdpaMatchVlanVid, deviceId);
1150 } else {
1151 sbuilder.matchVlanId(vlanId);
1152 }
Charles Chan393339f2018-01-11 11:48:18 -08001153 } else if (criterion instanceof Icmpv6TypeCriterion) {
1154 byte icmpv6Type = (byte) ((Icmpv6TypeCriterion) criterion).icmpv6Type();
1155 sbuilder.matchIcmpv6Type(icmpv6Type);
1156 } else if (criterion instanceof Icmpv6CodeCriterion) {
1157 byte icmpv6Code = (byte) ((Icmpv6CodeCriterion) criterion).icmpv6Code();
1158 sbuilder.matchIcmpv6Type(icmpv6Code);
Charles Chand9e47c62017-10-05 15:17:15 -07001159 } else if (criterion instanceof TcpPortCriterion || criterion instanceof UdpPortCriterion) {
1160 // FIXME: QMX switches do not support L4 dst port matching in ACL table.
1161 // Currently L4 dst port matching is only used by DHCP relay feature
1162 // and therefore is safe to be replaced with L4 src port matching.
1163 // We need to revisit this if L4 dst port is used for other purpose in the future.
1164 if (!supportIpv6L4Dst() && isIpv6(fwd.selector())) {
Charles Chand9e47c62017-10-05 15:17:15 -07001165 switch (criterion.type()) {
1166 case UDP_DST:
Charles Chand9e47c62017-10-05 15:17:15 -07001167 case UDP_DST_MASKED:
Charles Chand9e47c62017-10-05 15:17:15 -07001168 case TCP_DST:
Charles Chand9e47c62017-10-05 15:17:15 -07001169 case TCP_DST_MASKED:
Charles Chand9e47c62017-10-05 15:17:15 -07001170 break;
1171 default:
1172 sbuilder.add(criterion);
1173 }
1174 } else {
1175 sbuilder.add(criterion);
1176 }
Harshada Chaundkarb73d5d62019-08-05 15:33:30 +00001177 } else if (criterion instanceof EthTypeCriterion) {
1178 sbuilder.add(criterion);
1179 ethTypeUsed.set(true);
1180 } else {
Saurav Das77b5e902016-01-27 17:01:59 -08001181 sbuilder.add(criterion);
1182 }
1183 });
1184
Saurav Das822c4e22015-10-23 10:51:11 -07001185 // XXX driver does not currently do type checking as per Tables 65-67 in
1186 // OFDPA 2.0 spec. The only allowed treatment is a punt to the controller.
Saurav Das49cb5a12016-01-16 22:54:07 -08001187 TrafficTreatment.Builder ttBuilder = DefaultTrafficTreatment.builder();
1188 if (fwd.treatment() != null) {
1189 for (Instruction ins : fwd.treatment().allInstructions()) {
1190 if (ins instanceof OutputInstruction) {
1191 OutputInstruction o = (OutputInstruction) ins;
Charles Chan93090352018-03-02 13:26:22 -08001192 if (PortNumber.CONTROLLER.equals(o.port())) {
Saurav Das49cb5a12016-01-16 22:54:07 -08001193 ttBuilder.add(o);
1194 } else {
1195 log.warn("Only allowed treatments in versatile forwarding "
1196 + "objectives are punts to the controller");
1197 }
Charles Chan3fe71712018-06-15 18:54:18 -07001198 } else if (ins instanceof NoActionInstruction) {
1199 // No action is allowed and nothing needs to be done
Saurav Das49cb5a12016-01-16 22:54:07 -08001200 } else {
1201 log.warn("Cannot process instruction in versatile fwd {}", ins);
1202 }
Saurav Das822c4e22015-10-23 10:51:11 -07001203 }
Charles Chan2df0e8a2017-01-09 11:45:08 -08001204 if (fwd.treatment().clearedDeferred()) {
1205 ttBuilder.wipeDeferred();
1206 }
Saurav Das822c4e22015-10-23 10:51:11 -07001207 }
Saurav Das822c4e22015-10-23 10:51:11 -07001208 if (fwd.nextId() != null) {
Saurav Das49cb5a12016-01-16 22:54:07 -08001209 // overide case
1210 NextGroup next = getGroupForNextObjective(fwd.nextId());
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +05301211 if (next == null) {
1212 fail(fwd, ObjectiveError.BADPARAMS);
1213 return Collections.emptySet();
1214 }
Saurav Das49cb5a12016-01-16 22:54:07 -08001215 List<Deque<GroupKey>> gkeys = appKryo.deserialize(next.data());
1216 // we only need the top level group's key to point the flow to it
1217 Group group = groupService.getGroup(deviceId, gkeys.get(0).peekFirst());
1218 if (group == null) {
1219 log.warn("Group with key:{} for next-id:{} not found in dev:{}",
1220 gkeys.get(0).peekFirst(), fwd.nextId(), deviceId);
1221 fail(fwd, ObjectiveError.GROUPMISSING);
1222 return Collections.emptySet();
1223 }
1224 ttBuilder.deferred().group(group.id());
Saurav Das822c4e22015-10-23 10:51:11 -07001225 }
Saurav Das49cb5a12016-01-16 22:54:07 -08001226
1227 FlowRule.Builder ruleBuilder = DefaultFlowRule.builder()
1228 .fromApp(fwd.appId())
1229 .withPriority(fwd.priority())
1230 .forDevice(deviceId)
Saurav Das77b5e902016-01-27 17:01:59 -08001231 .withSelector(sbuilder.build())
Saurav Das49cb5a12016-01-16 22:54:07 -08001232 .withTreatment(ttBuilder.build())
1233 .makePermanent()
1234 .forTable(ACL_TABLE);
Harshada Chaundkarb73d5d62019-08-05 15:33:30 +00001235
1236 flowRules.add(ruleBuilder.build());
1237
1238 if (!ethTypeUsed.get() && requireEthType()) {
1239 log.debug("{} doesn't match on ethType but requireEthType is true, adding complementary ACL flow.",
1240 sbuilder.toString());
1241 sbuilder.matchEthType(Ethernet.TYPE_IPV6);
1242 FlowRule.Builder ethTypeRuleBuilder = DefaultFlowRule.builder()
1243 .fromApp(fwd.appId())
1244 .withPriority(fwd.priority())
1245 .forDevice(deviceId)
1246 .withSelector(sbuilder.build())
1247 .withTreatment(ttBuilder.build())
1248 .makePermanent()
1249 .forTable(ACL_TABLE);
1250 flowRules.add(ethTypeRuleBuilder.build());
1251 }
1252 return flowRules;
Saurav Das822c4e22015-10-23 10:51:11 -07001253 }
1254
1255 /**
1256 * In the OF-DPA 2.0 pipeline, specific forwarding refers to the IP table
Saurav Das8a0732e2015-11-20 15:27:53 -08001257 * (unicast or multicast) or the L2 table (mac + vlan) or the MPLS table.
Saurav Das822c4e22015-10-23 10:51:11 -07001258 *
1259 * @param fwd the forwarding objective of type 'specific'
1260 * @return a collection of flow rules. Typically there will be only one
1261 * for this type of forwarding objective. An empty set may be
1262 * returned if there is an issue in processing the objective.
1263 */
Charles Chan93090352018-03-02 13:26:22 -08001264 private Collection<FlowRule> processSpecific(ForwardingObjective fwd) {
Saurav Das25190812016-05-27 13:54:07 -07001265 log.debug("Processing specific fwd objective:{} in dev:{} with next:{}",
Saurav Das4ce45962015-11-24 23:21:05 -08001266 fwd.id(), deviceId, fwd.nextId());
1267 boolean isEthTypeObj = isSupportedEthTypeObjective(fwd);
1268 boolean isEthDstObj = isSupportedEthDstObjective(fwd);
1269
1270 if (isEthTypeObj) {
1271 return processEthTypeSpecific(fwd);
1272 } else if (isEthDstObj) {
1273 return processEthDstSpecific(fwd);
1274 } else {
1275 log.warn("processSpecific: Unsupported forwarding objective "
1276 + "criteria fwd:{} in dev:{}", fwd.nextId(), deviceId);
Saurav Das822c4e22015-10-23 10:51:11 -07001277 fail(fwd, ObjectiveError.UNSUPPORTED);
1278 return Collections.emptySet();
1279 }
Saurav Das4ce45962015-11-24 23:21:05 -08001280 }
1281
Saurav Das4ce45962015-11-24 23:21:05 -08001282 /**
1283 * Handles forwarding rules to the IP and MPLS tables.
1284 *
1285 * @param fwd the forwarding objective
1286 * @return A collection of flow rules, or an empty set
1287 */
1288 protected Collection<FlowRule> processEthTypeSpecific(ForwardingObjective fwd) {
Charles Chancad338a2016-09-16 18:03:11 -07001289 return processEthTypeSpecificInternal(fwd, false, ACL_TABLE);
Charles Chanf9e98652016-09-07 16:54:23 -07001290 }
1291
1292 /**
1293 * Internal implementation of processEthTypeSpecific.
1294 * <p>
1295 * Wildcarded IPv4_DST is not supported in OFDPA i12. Therefore, we break
1296 * the rule into 0.0.0.0/1 and 128.0.0.0/1.
1297 * The allowDefaultRoute must be set to false for OFDPA i12.
1298 * </p>
1299 *
1300 * @param fwd the forwarding objective
1301 * @param allowDefaultRoute allow wildcarded IPv4_DST or not
Ray Milkey0bb1e102016-11-10 14:51:27 -08001302 * @param mplsNextTable next MPLS table
Charles Chanf9e98652016-09-07 16:54:23 -07001303 * @return A collection of flow rules, or an empty set
1304 */
1305 protected Collection<FlowRule> processEthTypeSpecificInternal(ForwardingObjective fwd,
Charles Chancad338a2016-09-16 18:03:11 -07001306 boolean allowDefaultRoute,
1307 int mplsNextTable) {
Saurav Das4ce45962015-11-24 23:21:05 -08001308 TrafficSelector selector = fwd.selector();
1309 EthTypeCriterion ethType =
1310 (EthTypeCriterion) selector.getCriterion(Criterion.Type.ETH_TYPE);
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001311 boolean emptyGroup = false;
Charles Chan188ebf52015-12-23 00:15:11 -08001312 int forTableId;
Saurav Das8a0732e2015-11-20 15:27:53 -08001313 TrafficSelector.Builder filteredSelector = DefaultTrafficSelector.builder();
Charles Chan14967c22015-12-07 11:11:50 -08001314 TrafficTreatment.Builder tb = DefaultTrafficTreatment.builder();
Flavio Castroe10fa242016-01-15 12:43:51 -08001315 TrafficSelector.Builder complementarySelector = DefaultTrafficSelector.builder();
Charles Chan14967c22015-12-07 11:11:50 -08001316
Saurav Das8a0732e2015-11-20 15:27:53 -08001317 if (ethType.ethType().toShort() == Ethernet.TYPE_IPV4) {
Pier Luigi3bfe32c2017-01-30 09:47:36 -08001318 if (buildIpv4Selector(filteredSelector, complementarySelector, fwd, allowDefaultRoute) < 0) {
1319 return Collections.emptyList();
1320 }
1321 // We need to set properly the next table
Flavio Castroe10fa242016-01-15 12:43:51 -08001322 IpPrefix ipv4Dst = ((IPCriterion) selector.getCriterion(Criterion.Type.IPV4_DST)).ip();
Charles Chan5b9df8d2016-03-28 22:21:40 -07001323 if (ipv4Dst.isMulticast()) {
Charles Chan5b9df8d2016-03-28 22:21:40 -07001324 forTableId = MULTICAST_ROUTING_TABLE;
Flavio Castroe10fa242016-01-15 12:43:51 -08001325 } else {
Charles Chan5b9df8d2016-03-28 22:21:40 -07001326 forTableId = UNICAST_ROUTING_TABLE;
Flavio Castroe10fa242016-01-15 12:43:51 -08001327 }
Charles Chan93090352018-03-02 13:26:22 -08001328
Charles Chan7feb6c82018-03-05 11:28:23 -08001329 // TODO decrementing IP ttl is done automatically for routing, this
Charles Chan93090352018-03-02 13:26:22 -08001330 // action is ignored or rejected in ofdpa as it is not fully implemented
Charles Chan7feb6c82018-03-05 11:28:23 -08001331 // if (fwd.treatment() != null) {
1332 // for (Instruction instr : fwd.treatment().allInstructions()) {
1333 // if (instr instanceof L3ModificationInstruction &&
1334 // ((L3ModificationInstruction) instr).subtype() == L3SubType.DEC_TTL) {
1335 // tb.deferred().add(instr);
1336 // }
1337 // }
1338 // }
1339
Pier Ventree0ae7a32016-11-23 09:57:42 -08001340 } else if (ethType.ethType().toShort() == Ethernet.TYPE_IPV6) {
1341 if (buildIpv6Selector(filteredSelector, fwd) < 0) {
1342 return Collections.emptyList();
1343 }
Kalhee Kim3d26bf02017-10-04 14:59:11 +00001344 //We need to set the proper next table
1345 IpPrefix ipv6Dst = ((IPCriterion) selector.getCriterion(Criterion.Type.IPV6_DST)).ip();
1346 if (ipv6Dst.isMulticast()) {
1347 forTableId = MULTICAST_ROUTING_TABLE;
1348 } else {
1349 forTableId = UNICAST_ROUTING_TABLE;
1350 }
1351
Charles Chan7feb6c82018-03-05 11:28:23 -08001352 // TODO decrementing IP ttl is done automatically for routing, this
Charles Chan93090352018-03-02 13:26:22 -08001353 // action is ignored or rejected in ofdpa as it is not fully implemented
Charles Chan7feb6c82018-03-05 11:28:23 -08001354 // if (fwd.treatment() != null) {
1355 // for (Instruction instr : fwd.treatment().allInstructions()) {
1356 // if (instr instanceof L3ModificationInstruction &&
1357 // ((L3ModificationInstruction) instr).subtype() == L3SubType.DEC_TTL) {
1358 // tb.deferred().add(instr);
1359 // }
1360 // }
1361 // }
Saurav Das8a0732e2015-11-20 15:27:53 -08001362 } else {
1363 filteredSelector
1364 .matchEthType(Ethernet.MPLS_UNICAST)
1365 .matchMplsLabel(((MplsCriterion)
1366 selector.getCriterion(Criterion.Type.MPLS_LABEL)).label());
1367 MplsBosCriterion bos = (MplsBosCriterion) selector
Pier Ventre140a8942016-11-02 07:26:38 -07001368 .getCriterion(MPLS_BOS);
Alex Yashchuka3679532017-12-08 17:40:05 +02001369 if (bos != null && requireMplsBosMatch()) {
Saurav Das8a0732e2015-11-20 15:27:53 -08001370 filteredSelector.matchMplsBos(bos.mplsBos());
1371 }
1372 forTableId = MPLS_TABLE_1;
Saurav Das4ce45962015-11-24 23:21:05 -08001373 log.debug("processing MPLS specific forwarding objective {} -> next:{}"
1374 + " in dev {}", fwd.id(), fwd.nextId(), deviceId);
Saurav Das822c4e22015-10-23 10:51:11 -07001375
Charles Chan14967c22015-12-07 11:11:50 -08001376 if (fwd.treatment() != null) {
1377 for (Instruction instr : fwd.treatment().allInstructions()) {
1378 if (instr instanceof L2ModificationInstruction &&
1379 ((L2ModificationInstruction) instr).subtype() == L2SubType.MPLS_POP) {
Pier Luigi3bfe32c2017-01-30 09:47:36 -08001380 // OF-DPA does not pop in MPLS table in some cases. For the L3 VPN, it requires
Saurav Das9c705342017-01-06 11:36:01 -08001381 // setting the MPLS_TYPE so pop can happen down the pipeline
Alex Yashchuka3679532017-12-08 17:40:05 +02001382 if (requireMplsPop()) {
1383 if (mplsNextTable == MPLS_TYPE_TABLE && isNotMplsBos(selector)) {
1384 tb.immediate().popMpls();
1385 }
1386 } else {
1387 // Skip mpls pop action for mpls_unicast label
1388 if (instr instanceof ModMplsHeaderInstruction &&
1389 !((ModMplsHeaderInstruction) instr).ethernetType()
1390 .equals(EtherType.MPLS_UNICAST.ethType())) {
1391 tb.immediate().add(instr);
1392 }
Pier Luigi3bfe32c2017-01-30 09:47:36 -08001393 }
Charles Chan14967c22015-12-07 11:11:50 -08001394 }
Alex Yashchuka3679532017-12-08 17:40:05 +02001395
1396 if (requireMplsTtlModification()) {
1397 if (instr instanceof L3ModificationInstruction &&
1398 ((L3ModificationInstruction) instr).subtype() == L3SubType.DEC_TTL) {
1399 // FIXME Should modify the app to send the correct DEC_MPLS_TTL instruction
1400 tb.immediate().decMplsTtl();
1401 }
1402 if (instr instanceof L3ModificationInstruction &&
1403 ((L3ModificationInstruction) instr).subtype() == L3SubType.TTL_IN) {
1404 tb.immediate().add(instr);
1405 }
Charles Chan14967c22015-12-07 11:11:50 -08001406 }
Saurav Das8a0732e2015-11-20 15:27:53 -08001407 }
1408 }
1409 }
Saurav Das822c4e22015-10-23 10:51:11 -07001410
1411 if (fwd.nextId() != null) {
Saurav Das423fe2b2015-12-04 10:52:59 -08001412 NextGroup next = getGroupForNextObjective(fwd.nextId());
1413 if (next != null) {
1414 List<Deque<GroupKey>> gkeys = appKryo.deserialize(next.data());
1415 // we only need the top level group's key to point the flow to it
1416 Group group = groupService.getGroup(deviceId, gkeys.get(0).peekFirst());
1417 if (group == null) {
1418 log.warn("Group with key:{} for next-id:{} not found in dev:{}",
1419 gkeys.get(0).peekFirst(), fwd.nextId(), deviceId);
1420 fail(fwd, ObjectiveError.GROUPMISSING);
1421 return Collections.emptySet();
1422 }
Saurav Dasa89b95a2018-02-14 14:14:54 -08001423 if (isNotMplsBos(selector) && group.type().equals(SELECT)) {
1424 log.warn("SR CONTINUE case cannot be handled as MPLS ECMP "
1425 + "is not implemented in OF-DPA yet. Aborting flow {} -> next:{} "
1426 + "in this device {}", fwd.id(), fwd.nextId(), deviceId);
1427 fail(fwd, ObjectiveError.FLOWINSTALLATIONFAILED);
1428 return Collections.emptySet();
1429 }
Saurav Das423fe2b2015-12-04 10:52:59 -08001430 tb.deferred().group(group.id());
Saurav Dasf3f75942018-01-25 09:49:01 -08001431 // retrying flows may be necessary due to bug CORD-554
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001432 if (gkeys.size() == 1 && gkeys.get(0).size() == 1) {
Saurav Dasf3f75942018-01-25 09:49:01 -08001433 if (shouldRetry()) {
1434 log.warn("Found empty group 0x{} in dev:{} .. will retry fwd:{}",
1435 Integer.toHexString(group.id().id()), deviceId,
1436 fwd.id());
1437 emptyGroup = true;
1438 }
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001439 }
Saurav Das25190812016-05-27 13:54:07 -07001440 } else {
1441 log.warn("Cannot find group for nextId:{} in dev:{}. Aborting fwd:{}",
1442 fwd.nextId(), deviceId, fwd.id());
1443 fail(fwd, ObjectiveError.FLOWINSTALLATIONFAILED);
1444 return Collections.emptySet();
Saurav Das822c4e22015-10-23 10:51:11 -07001445 }
Saurav Das822c4e22015-10-23 10:51:11 -07001446 }
Charles Chancad338a2016-09-16 18:03:11 -07001447
1448 if (forTableId == MPLS_TABLE_1) {
Pier Ventre140a8942016-11-02 07:26:38 -07001449 if (mplsNextTable == MPLS_L3_TYPE_TABLE) {
Charles Chancad338a2016-09-16 18:03:11 -07001450 Ofdpa3SetMplsType setMplsType = new Ofdpa3SetMplsType(Ofdpa3MplsType.L3_PHP);
Saurav Das9c705342017-01-06 11:36:01 -08001451 // set mpls type as apply_action
1452 tb.immediate().extension(setMplsType, deviceId);
Charles Chancad338a2016-09-16 18:03:11 -07001453 }
1454 tb.transition(mplsNextTable);
1455 } else {
1456 tb.transition(ACL_TABLE);
1457 }
1458
Andrea Campanella84ac4df2018-04-30 11:48:55 +02001459 if (fwd.treatment() != null && fwd.treatment().clearedDeferred()) {
1460 if (supportsUnicastBlackHole()) {
1461 tb.wipeDeferred();
1462 } else {
1463 log.warn("Clear Deferred is not supported Unicast Routing Table on device {}", deviceId);
1464 return Collections.emptySet();
1465 }
1466 }
1467
Saurav Das822c4e22015-10-23 10:51:11 -07001468 FlowRule.Builder ruleBuilder = DefaultFlowRule.builder()
1469 .fromApp(fwd.appId())
1470 .withPriority(fwd.priority())
1471 .forDevice(deviceId)
Saurav Das8a0732e2015-11-20 15:27:53 -08001472 .withSelector(filteredSelector.build())
1473 .withTreatment(tb.build())
1474 .forTable(forTableId);
Saurav Das822c4e22015-10-23 10:51:11 -07001475
1476 if (fwd.permanent()) {
1477 ruleBuilder.makePermanent();
1478 } else {
1479 ruleBuilder.makeTemporary(fwd.timeout());
1480 }
Flavio Castroe10fa242016-01-15 12:43:51 -08001481 Collection<FlowRule> flowRuleCollection = new ArrayList<>();
1482 flowRuleCollection.add(ruleBuilder.build());
Pier Luigi3bfe32c2017-01-30 09:47:36 -08001483 if (!allowDefaultRoute) {
Pier Ventree0ae7a32016-11-23 09:57:42 -08001484 flowRuleCollection.add(
1485 defaultRoute(fwd, complementarySelector, forTableId, tb)
1486 );
Flavio Castroe10fa242016-01-15 12:43:51 -08001487 log.debug("Default rule 0.0.0.0/0 is being installed two rules");
1488 }
Saurav Dasf3f75942018-01-25 09:49:01 -08001489
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001490 if (emptyGroup) {
pier5b9b9e32019-03-11 15:14:02 -07001491 retryExecutorService.schedule(new RetryFlows(fwd, flowRuleCollection),
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001492 RETRY_MS, TimeUnit.MILLISECONDS);
1493 }
Flavio Castroe10fa242016-01-15 12:43:51 -08001494 return flowRuleCollection;
Saurav Das822c4e22015-10-23 10:51:11 -07001495 }
1496
Charles Chan93090352018-03-02 13:26:22 -08001497 private int buildIpv4Selector(TrafficSelector.Builder builderToUpdate,
Pier Luigi3bfe32c2017-01-30 09:47:36 -08001498 TrafficSelector.Builder extBuilder,
1499 ForwardingObjective fwd,
1500 boolean allowDefaultRoute) {
1501 TrafficSelector selector = fwd.selector();
1502
1503 IpPrefix ipv4Dst = ((IPCriterion) selector.getCriterion(Criterion.Type.IPV4_DST)).ip();
1504 if (ipv4Dst.isMulticast()) {
1505 if (ipv4Dst.prefixLength() != 32) {
1506 log.warn("Multicast specific forwarding objective can only be /32");
1507 fail(fwd, ObjectiveError.BADPARAMS);
1508 return -1;
1509 }
1510 VlanId assignedVlan = readVlanFromSelector(fwd.meta());
1511 if (assignedVlan == null) {
1512 log.warn("VLAN ID required by multicast specific fwd obj is missing. Abort.");
1513 fail(fwd, ObjectiveError.BADPARAMS);
1514 return -1;
1515 }
Charles Chand1172632017-03-15 17:33:09 -07001516 if (requireVlanExtensions()) {
1517 OfdpaMatchVlanVid ofdpaMatchVlanVid = new OfdpaMatchVlanVid(assignedVlan);
1518 builderToUpdate.extension(ofdpaMatchVlanVid, deviceId);
1519 } else {
1520 builderToUpdate.matchVlanId(assignedVlan);
1521 }
Pier Luigi3bfe32c2017-01-30 09:47:36 -08001522 builderToUpdate.matchEthType(Ethernet.TYPE_IPV4).matchIPDst(ipv4Dst);
1523 log.debug("processing IPv4 multicast specific forwarding objective {} -> next:{}"
1524 + " in dev:{}", fwd.id(), fwd.nextId(), deviceId);
1525 } else {
1526 if (ipv4Dst.prefixLength() == 0) {
1527 if (allowDefaultRoute) {
1528 // The entire IPV4_DST field is wildcarded intentionally
1529 builderToUpdate.matchEthType(Ethernet.TYPE_IPV4);
1530 } else {
1531 // NOTE: The switch does not support matching 0.0.0.0/0
1532 // Split it into 0.0.0.0/1 and 128.0.0.0/1
1533 builderToUpdate.matchEthType(Ethernet.TYPE_IPV4)
1534 .matchIPDst(IpPrefix.valueOf("0.0.0.0/1"));
1535 extBuilder.matchEthType(Ethernet.TYPE_IPV4)
1536 .matchIPDst(IpPrefix.valueOf("128.0.0.0/1"));
1537 }
1538 } else {
1539 builderToUpdate.matchEthType(Ethernet.TYPE_IPV4).matchIPDst(ipv4Dst);
1540 }
1541 log.debug("processing IPv4 unicast specific forwarding objective {} -> next:{}"
1542 + " in dev:{}", fwd.id(), fwd.nextId(), deviceId);
1543 }
1544 return 0;
1545 }
1546
1547 /**
1548 * Helper method to build Ipv6 selector using the selector provided by
1549 * a forwarding objective.
1550 *
1551 * @param builderToUpdate the builder to update
1552 * @param fwd the selector to read
1553 * @return 0 if the update ends correctly. -1 if the matches
1554 * are not yet supported
1555 */
Charles Chan93090352018-03-02 13:26:22 -08001556 int buildIpv6Selector(TrafficSelector.Builder builderToUpdate,
Pier Luigi3bfe32c2017-01-30 09:47:36 -08001557 ForwardingObjective fwd) {
1558
1559 TrafficSelector selector = fwd.selector();
1560
1561 IpPrefix ipv6Dst = ((IPCriterion) selector.getCriterion(Criterion.Type.IPV6_DST)).ip();
1562 if (ipv6Dst.isMulticast()) {
Julia Ferguson65428c32017-08-10 18:15:24 +00001563 if (ipv6Dst.prefixLength() != IpAddress.INET6_BIT_LENGTH) {
1564 log.warn("Multicast specific forwarding objective can only be /128");
1565 fail(fwd, ObjectiveError.BADPARAMS);
1566 return -1;
1567 }
1568 VlanId assignedVlan = readVlanFromSelector(fwd.meta());
1569 if (assignedVlan == null) {
1570 log.warn("VLAN ID required by multicast specific fwd obj is missing. Abort.");
1571 fail(fwd, ObjectiveError.BADPARAMS);
1572 return -1;
1573 }
1574 if (requireVlanExtensions()) {
1575 OfdpaMatchVlanVid ofdpaMatchVlanVid = new OfdpaMatchVlanVid(assignedVlan);
1576 builderToUpdate.extension(ofdpaMatchVlanVid, deviceId);
1577 } else {
1578 builderToUpdate.matchVlanId(assignedVlan);
1579 }
1580 builderToUpdate.matchEthType(Ethernet.TYPE_IPV6).matchIPv6Dst(ipv6Dst);
1581 log.debug("processing IPv6 multicast specific forwarding objective {} -> next:{}"
1582 + " in dev:{}", fwd.id(), fwd.nextId(), deviceId);
1583 } else {
1584 if (ipv6Dst.prefixLength() != 0) {
1585 builderToUpdate.matchIPv6Dst(ipv6Dst);
1586 }
Pier Luigi3bfe32c2017-01-30 09:47:36 -08001587 builderToUpdate.matchEthType(Ethernet.TYPE_IPV6);
1588 log.debug("processing IPv6 unicast specific forwarding objective {} -> next:{}"
1589 + " in dev:{}", fwd.id(), fwd.nextId(), deviceId);
Julia Ferguson65428c32017-08-10 18:15:24 +00001590 }
Pier Luigi3bfe32c2017-01-30 09:47:36 -08001591 return 0;
1592 }
1593
Charles Chan93090352018-03-02 13:26:22 -08001594 FlowRule defaultRoute(ForwardingObjective fwd,
Pier Ventree0ae7a32016-11-23 09:57:42 -08001595 TrafficSelector.Builder complementarySelector,
1596 int forTableId,
1597 TrafficTreatment.Builder tb) {
1598 FlowRule.Builder rule = DefaultFlowRule.builder()
1599 .fromApp(fwd.appId())
1600 .withPriority(fwd.priority())
1601 .forDevice(deviceId)
1602 .withSelector(complementarySelector.build())
1603 .withTreatment(tb.build())
1604 .forTable(forTableId);
1605 if (fwd.permanent()) {
1606 rule.makePermanent();
1607 } else {
1608 rule.makeTemporary(fwd.timeout());
1609 }
1610 return rule.build();
1611 }
1612
Saurav Das4ce45962015-11-24 23:21:05 -08001613 /**
1614 * Handles forwarding rules to the L2 bridging table. Flow actions are not
1615 * allowed in the bridging table - instead we use L2 Interface group or
1616 * L2 flood group
1617 *
1618 * @param fwd the forwarding objective
1619 * @return A collection of flow rules, or an empty set
1620 */
1621 protected Collection<FlowRule> processEthDstSpecific(ForwardingObjective fwd) {
1622 List<FlowRule> rules = new ArrayList<>();
1623
1624 // Build filtered selector
1625 TrafficSelector selector = fwd.selector();
1626 EthCriterion ethCriterion = (EthCriterion) selector
1627 .getCriterion(Criterion.Type.ETH_DST);
1628 VlanIdCriterion vlanIdCriterion = (VlanIdCriterion) selector
1629 .getCriterion(Criterion.Type.VLAN_VID);
1630
1631 if (vlanIdCriterion == null) {
1632 log.warn("Forwarding objective for bridging requires vlan. Not "
1633 + "installing fwd:{} in dev:{}", fwd.id(), deviceId);
1634 fail(fwd, ObjectiveError.BADPARAMS);
1635 return Collections.emptySet();
1636 }
1637
1638 TrafficSelector.Builder filteredSelectorBuilder =
1639 DefaultTrafficSelector.builder();
Yi Tseng3a77b4f2017-02-06 15:02:17 -08001640
1641 if (!ethCriterion.mac().equals(NONE) &&
1642 !ethCriterion.mac().equals(BROADCAST)) {
Saurav Das4ce45962015-11-24 23:21:05 -08001643 filteredSelectorBuilder.matchEthDst(ethCriterion.mac());
1644 log.debug("processing L2 forwarding objective:{} -> next:{} in dev:{}",
1645 fwd.id(), fwd.nextId(), deviceId);
1646 } else {
Yi Tseng3a77b4f2017-02-06 15:02:17 -08001647 // Use wildcard DST_MAC if the MacAddress is None or Broadcast
Saurav Das4ce45962015-11-24 23:21:05 -08001648 log.debug("processing L2 Broadcast forwarding objective:{} -> next:{} "
1649 + "in dev:{} for vlan:{}",
1650 fwd.id(), fwd.nextId(), deviceId, vlanIdCriterion.vlanId());
1651 }
Charles Chand1172632017-03-15 17:33:09 -07001652 if (requireVlanExtensions()) {
1653 OfdpaMatchVlanVid ofdpaMatchVlanVid = new OfdpaMatchVlanVid(vlanIdCriterion.vlanId());
1654 filteredSelectorBuilder.extension(ofdpaMatchVlanVid, deviceId);
1655 } else {
1656 filteredSelectorBuilder.matchVlanId(vlanIdCriterion.vlanId());
1657 }
Saurav Das4ce45962015-11-24 23:21:05 -08001658 TrafficSelector filteredSelector = filteredSelectorBuilder.build();
1659
1660 if (fwd.treatment() != null) {
1661 log.warn("Ignoring traffic treatment in fwd rule {} meant for L2 table"
1662 + "for dev:{}. Expecting only nextId", fwd.id(), deviceId);
1663 }
1664
1665 TrafficTreatment.Builder treatmentBuilder = DefaultTrafficTreatment.builder();
1666 if (fwd.nextId() != null) {
Saurav Das423fe2b2015-12-04 10:52:59 -08001667 NextGroup next = getGroupForNextObjective(fwd.nextId());
Saurav Das4ce45962015-11-24 23:21:05 -08001668 if (next != null) {
1669 List<Deque<GroupKey>> gkeys = appKryo.deserialize(next.data());
1670 // we only need the top level group's key to point the flow to it
1671 Group group = groupService.getGroup(deviceId, gkeys.get(0).peekFirst());
1672 if (group != null) {
1673 treatmentBuilder.deferred().group(group.id());
1674 } else {
1675 log.warn("Group with key:{} for next-id:{} not found in dev:{}",
1676 gkeys.get(0).peekFirst(), fwd.nextId(), deviceId);
1677 fail(fwd, ObjectiveError.GROUPMISSING);
1678 return Collections.emptySet();
1679 }
1680 }
1681 }
1682 treatmentBuilder.immediate().transition(ACL_TABLE);
1683 TrafficTreatment filteredTreatment = treatmentBuilder.build();
1684
1685 // Build bridging table entries
1686 FlowRule.Builder flowRuleBuilder = DefaultFlowRule.builder();
1687 flowRuleBuilder.fromApp(fwd.appId())
1688 .withPriority(fwd.priority())
1689 .forDevice(deviceId)
1690 .withSelector(filteredSelector)
1691 .withTreatment(filteredTreatment)
1692 .forTable(BRIDGING_TABLE);
1693 if (fwd.permanent()) {
1694 flowRuleBuilder.makePermanent();
1695 } else {
1696 flowRuleBuilder.makeTemporary(fwd.timeout());
1697 }
1698 rules.add(flowRuleBuilder.build());
1699 return rules;
1700 }
1701
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001702 //////////////////////////////////////
1703 // Helper Methods and Classes
1704 //////////////////////////////////////
1705
1706 private boolean isSupportedEthTypeObjective(ForwardingObjective fwd) {
1707 TrafficSelector selector = fwd.selector();
1708 EthTypeCriterion ethType = (EthTypeCriterion) selector
1709 .getCriterion(Criterion.Type.ETH_TYPE);
1710 return !((ethType == null) ||
1711 ((ethType.ethType().toShort() != Ethernet.TYPE_IPV4) &&
Pier Ventree0ae7a32016-11-23 09:57:42 -08001712 (ethType.ethType().toShort() != Ethernet.MPLS_UNICAST)) &&
1713 (ethType.ethType().toShort() != Ethernet.TYPE_IPV6));
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001714 }
1715
1716 private boolean isSupportedEthDstObjective(ForwardingObjective fwd) {
1717 TrafficSelector selector = fwd.selector();
1718 EthCriterion ethDst = (EthCriterion) selector
1719 .getCriterion(Criterion.Type.ETH_DST);
1720 VlanIdCriterion vlanId = (VlanIdCriterion) selector
1721 .getCriterion(Criterion.Type.VLAN_VID);
1722 return !(ethDst == null && vlanId == null);
1723 }
1724
Charles Chan93090352018-03-02 13:26:22 -08001725 NextGroup getGroupForNextObjective(Integer nextId) {
Saurav Das423fe2b2015-12-04 10:52:59 -08001726 NextGroup next = flowObjectiveStore.getNextGroup(nextId);
1727 if (next != null) {
1728 List<Deque<GroupKey>> gkeys = appKryo.deserialize(next.data());
1729 if (gkeys != null && !gkeys.isEmpty()) {
1730 return next;
1731 } else {
1732 log.warn("Empty next group found in FlowObjective store for "
1733 + "next-id:{} in dev:{}", nextId, deviceId);
1734 }
1735 } else {
1736 log.warn("next-id {} not found in Flow objective store for dev:{}",
1737 nextId, deviceId);
1738 }
1739 return null;
1740 }
1741
Saurav Das24431192016-03-07 19:13:00 -08001742 @Override
1743 public List<String> getNextMappings(NextGroup nextGroup) {
1744 List<String> mappings = new ArrayList<>();
1745 List<Deque<GroupKey>> gkeys = appKryo.deserialize(nextGroup.data());
1746 for (Deque<GroupKey> gkd : gkeys) {
1747 Group lastGroup = null;
Yuta HIGUCHI2dce08a2017-04-20 21:57:48 -07001748 StringBuilder gchain = new StringBuilder();
Saurav Das24431192016-03-07 19:13:00 -08001749 for (GroupKey gk : gkd) {
1750 Group g = groupService.getGroup(deviceId, gk);
Saurav Das8be4e3a2016-03-11 17:19:07 -08001751 if (g == null) {
Saurav Das25190812016-05-27 13:54:07 -07001752 gchain.append(" NoGrp").append(" -->");
Saurav Das8be4e3a2016-03-11 17:19:07 -08001753 continue;
1754 }
1755 gchain.append(" 0x").append(Integer.toHexString(g.id().id()))
1756 .append(" -->");
Saurav Das24431192016-03-07 19:13:00 -08001757 lastGroup = g;
1758 }
1759 // add port information for last group in group-chain
Yuta HIGUCHI2dce08a2017-04-20 21:57:48 -07001760 List<Instruction> lastGroupIns = new ArrayList<>();
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001761 if (lastGroup != null && !lastGroup.buckets().buckets().isEmpty()) {
Saurav Das25190812016-05-27 13:54:07 -07001762 lastGroupIns = lastGroup.buckets().buckets().get(0)
1763 .treatment().allInstructions();
1764 }
1765 for (Instruction i: lastGroupIns) {
Saurav Das24431192016-03-07 19:13:00 -08001766 if (i instanceof OutputInstruction) {
Saurav Das8be4e3a2016-03-11 17:19:07 -08001767 gchain.append(" port:").append(((OutputInstruction) i).port());
Saurav Das24431192016-03-07 19:13:00 -08001768 }
1769 }
Saurav Das8be4e3a2016-03-11 17:19:07 -08001770 mappings.add(gchain.toString());
Saurav Das24431192016-03-07 19:13:00 -08001771 }
1772 return mappings;
1773 }
Charles Chan5b9df8d2016-03-28 22:21:40 -07001774
Saurav Dasa89b95a2018-02-14 14:14:54 -08001775 /**
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001776 * Utility class that retries sending flows a fixed number of times, even if
1777 * some of the attempts are successful. Used only for forwarding objectives.
1778 */
Alex Yashchuka3679532017-12-08 17:40:05 +02001779 public final class RetryFlows implements Runnable {
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001780 int attempts = MAX_RETRY_ATTEMPTS;
1781 private Collection<FlowRule> retryFlows;
1782 private ForwardingObjective fwd;
1783
Charles Chan93090352018-03-02 13:26:22 -08001784 RetryFlows(ForwardingObjective fwd, Collection<FlowRule> retryFlows) {
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001785 this.fwd = fwd;
1786 this.retryFlows = retryFlows;
1787 }
1788
1789 @Override
1790 public void run() {
1791 log.info("RETRY FLOWS ATTEMPT# {} for fwd:{} rules:{}",
1792 MAX_RETRY_ATTEMPTS - attempts, fwd.id(), retryFlows.size());
pier5b9b9e32019-03-11 15:14:02 -07001793 sendForwards(Collections.singletonList(Pair.of(fwd, retryFlows)));
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001794 if (--attempts > 0) {
pier5b9b9e32019-03-11 15:14:02 -07001795 retryExecutorService.schedule(this, RETRY_MS, TimeUnit.MILLISECONDS);
1796 }
1797 }
1798 }
1799
1800 // Flow rules accumulator for reducing the number of transactions required to the devices.
1801 private final class ForwardingObjectiveAccumulator
1802 extends AbstractAccumulator<Pair<ForwardingObjective, Collection<FlowRule>>> {
1803
1804 ForwardingObjectiveAccumulator(int maxFwd, int maxBatchMS, int maxIdleMS) {
1805 super(TIMER, maxFwd, maxBatchMS, maxIdleMS);
1806 }
1807
1808 @Override
1809 public void processItems(List<Pair<ForwardingObjective, Collection<FlowRule>>> pairs) {
1810 // Triggers creation of a batch using the list of flowrules generated from fwdobjs.
1811 accumulatorExecutorService.execute(new FlowRulesBuilderTask(pairs));
1812 }
1813 }
1814
1815 // Task for building batch of flow rules in a separate thread.
1816 private final class FlowRulesBuilderTask implements Runnable {
1817 private final List<Pair<ForwardingObjective, Collection<FlowRule>>> pairs;
1818
1819 FlowRulesBuilderTask(List<Pair<ForwardingObjective, Collection<FlowRule>>> pairs) {
1820 this.pairs = pairs;
1821 }
1822
1823 @Override
1824 public void run() {
1825 try {
1826 sendForwards(pairs);
1827 } catch (Exception e) {
1828 log.warn("Unable to send forwards", e);
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001829 }
1830 }
1831 }
1832
Saurav Das822c4e22015-10-23 10:51:11 -07001833}