blob: d8b1f06816c068715e4319cb27ca2fcfdfd4cf5b [file] [log] [blame]
Saurav Das822c4e22015-10-23 10:51:11 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2016-present Open Networking Foundation
Saurav Das822c4e22015-10-23 10:51:11 -07003 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
Yi Tsengef19de12017-04-24 11:33:05 -070016package org.onosproject.driver.pipeline.ofdpa;
Saurav Das822c4e22015-10-23 10:51:11 -070017
Charles Chan5270ed02016-01-30 23:22:37 -080018import com.google.common.collect.ImmutableList;
pier8b3aef42019-03-11 15:14:02 -070019import com.google.common.collect.Lists;
Yi Tseng47f82dc2017-03-05 22:48:39 -080020import com.google.common.collect.Sets;
pier8b3aef42019-03-11 15:14:02 -070021import org.apache.commons.lang3.tuple.Pair;
Saurav Das822c4e22015-10-23 10:51:11 -070022import org.onlab.osgi.ServiceDirectory;
Jonghwan Hyun800d9d02018-04-09 09:40:50 -070023import org.onlab.packet.EthType;
Saurav Das822c4e22015-10-23 10:51:11 -070024import org.onlab.packet.Ethernet;
Alex Yashchuk4caa8e82017-12-08 17:40:05 +020025import org.onlab.packet.EthType.EtherType;
Julia Ferguson65428c32017-08-10 18:15:24 +000026import org.onlab.packet.IpAddress;
Charles Chan14967c22015-12-07 11:11:50 -080027import org.onlab.packet.IpPrefix;
Charles Chan45b69ab2018-03-02 15:41:41 -080028import org.onlab.packet.MacAddress;
Saurav Das822c4e22015-10-23 10:51:11 -070029import org.onlab.packet.VlanId;
pier8b3aef42019-03-11 15:14:02 -070030import org.onlab.util.AbstractAccumulator;
31import org.onlab.util.Accumulator;
Saurav Das822c4e22015-10-23 10:51:11 -070032import org.onlab.util.KryoNamespace;
33import org.onosproject.core.ApplicationId;
34import org.onosproject.core.CoreService;
Jonghwan Hyun800d9d02018-04-09 09:40:50 -070035import org.onosproject.driver.extensions.Ofdpa3CopyField;
Charles Chancad338a2016-09-16 18:03:11 -070036import org.onosproject.driver.extensions.Ofdpa3MplsType;
37import org.onosproject.driver.extensions.Ofdpa3SetMplsType;
Jonghwan Hyun800d9d02018-04-09 09:40:50 -070038import org.onosproject.driver.extensions.OfdpaMatchActsetOutput;
39import org.onosproject.driver.extensions.OfdpaMatchAllowVlanTranslation;
Charles Chan14967c22015-12-07 11:11:50 -080040import org.onosproject.driver.extensions.OfdpaMatchVlanVid;
41import org.onosproject.driver.extensions.OfdpaSetVlanVid;
Saurav Das822c4e22015-10-23 10:51:11 -070042import org.onosproject.net.DeviceId;
43import org.onosproject.net.Port;
44import org.onosproject.net.PortNumber;
45import org.onosproject.net.behaviour.NextGroup;
46import org.onosproject.net.behaviour.Pipeliner;
47import org.onosproject.net.behaviour.PipelinerContext;
48import org.onosproject.net.device.DeviceService;
49import org.onosproject.net.driver.AbstractHandlerBehaviour;
50import org.onosproject.net.flow.DefaultFlowRule;
51import org.onosproject.net.flow.DefaultTrafficSelector;
52import org.onosproject.net.flow.DefaultTrafficTreatment;
53import org.onosproject.net.flow.FlowRule;
54import org.onosproject.net.flow.FlowRuleOperations;
55import org.onosproject.net.flow.FlowRuleOperationsContext;
56import org.onosproject.net.flow.FlowRuleService;
57import org.onosproject.net.flow.TrafficSelector;
58import org.onosproject.net.flow.TrafficTreatment;
59import org.onosproject.net.flow.criteria.Criteria;
60import org.onosproject.net.flow.criteria.Criterion;
61import org.onosproject.net.flow.criteria.EthCriterion;
62import org.onosproject.net.flow.criteria.EthTypeCriterion;
63import org.onosproject.net.flow.criteria.IPCriterion;
Pier Ventree0ae7a32016-11-23 09:57:42 -080064import org.onosproject.net.flow.criteria.Icmpv6CodeCriterion;
65import org.onosproject.net.flow.criteria.Icmpv6TypeCriterion;
Saurav Das8a0732e2015-11-20 15:27:53 -080066import org.onosproject.net.flow.criteria.MplsBosCriterion;
67import org.onosproject.net.flow.criteria.MplsCriterion;
Saurav Das822c4e22015-10-23 10:51:11 -070068import org.onosproject.net.flow.criteria.PortCriterion;
Charles Chand9e47c62017-10-05 15:17:15 -070069import org.onosproject.net.flow.criteria.TcpPortCriterion;
70import org.onosproject.net.flow.criteria.UdpPortCriterion;
Saurav Das822c4e22015-10-23 10:51:11 -070071import org.onosproject.net.flow.criteria.VlanIdCriterion;
72import org.onosproject.net.flow.instructions.Instruction;
73import org.onosproject.net.flow.instructions.Instructions.OutputInstruction;
Charles Chanf76de302018-06-15 18:54:18 -070074import org.onosproject.net.flow.instructions.Instructions.NoActionInstruction;
Saurav Das822c4e22015-10-23 10:51:11 -070075import org.onosproject.net.flow.instructions.L2ModificationInstruction;
Saurav Das8a0732e2015-11-20 15:27:53 -080076import org.onosproject.net.flow.instructions.L2ModificationInstruction.L2SubType;
Alex Yashchuk4caa8e82017-12-08 17:40:05 +020077import org.onosproject.net.flow.instructions.L2ModificationInstruction.ModMplsHeaderInstruction;
Charles Chan14967c22015-12-07 11:11:50 -080078import org.onosproject.net.flow.instructions.L3ModificationInstruction;
79import org.onosproject.net.flow.instructions.L3ModificationInstruction.L3SubType;
Saurav Das822c4e22015-10-23 10:51:11 -070080import org.onosproject.net.flowobjective.FilteringObjective;
81import org.onosproject.net.flowobjective.FlowObjectiveStore;
82import org.onosproject.net.flowobjective.ForwardingObjective;
83import org.onosproject.net.flowobjective.NextObjective;
84import org.onosproject.net.flowobjective.Objective;
85import org.onosproject.net.flowobjective.ObjectiveError;
Saurav Das822c4e22015-10-23 10:51:11 -070086import org.onosproject.net.group.DefaultGroupKey;
87import org.onosproject.net.group.Group;
Saurav Das822c4e22015-10-23 10:51:11 -070088import org.onosproject.net.group.GroupKey;
Saurav Das822c4e22015-10-23 10:51:11 -070089import org.onosproject.net.group.GroupService;
Saurav Das822c4e22015-10-23 10:51:11 -070090import org.onosproject.store.serializers.KryoNamespaces;
91import org.slf4j.Logger;
92
Pier Ventree0ae7a32016-11-23 09:57:42 -080093import java.util.ArrayDeque;
94import java.util.ArrayList;
95import java.util.Collection;
96import java.util.Collections;
97import java.util.Deque;
98import java.util.List;
99import java.util.Objects;
Jonghwan Hyun800d9d02018-04-09 09:40:50 -0700100import java.util.Optional;
pier8b3aef42019-03-11 15:14:02 -0700101import java.util.Timer;
Pier Ventree0ae7a32016-11-23 09:57:42 -0800102import java.util.concurrent.ScheduledExecutorService;
103import java.util.concurrent.TimeUnit;
Harshada Chaundkarcb787512019-08-05 15:33:30 +0000104import java.util.concurrent.atomic.AtomicBoolean;
Pier Ventree0ae7a32016-11-23 09:57:42 -0800105
106import static java.util.concurrent.Executors.newScheduledThreadPool;
pier8b3aef42019-03-11 15:14:02 -0700107import static java.util.concurrent.Executors.newSingleThreadScheduledExecutor;
pier5be39042019-10-04 10:54:07 +0200108import static org.onlab.packet.MacAddress.*;
Pier Ventree0ae7a32016-11-23 09:57:42 -0800109import static org.onlab.util.Tools.groupedThreads;
Jonghwan Hyun800d9d02018-04-09 09:40:50 -0700110import static org.onosproject.driver.extensions.Ofdpa3CopyField.OXM_ID_PACKET_REG_1;
111import static org.onosproject.driver.extensions.Ofdpa3CopyField.OXM_ID_VLAN_VID;
Yi Tsengef19de12017-04-24 11:33:05 -0700112import static org.onosproject.driver.pipeline.ofdpa.OfdpaGroupHandlerUtility.*;
pier9469f3e2019-04-17 17:05:08 +0200113import static org.onosproject.driver.pipeline.ofdpa.OfdpaPipelineUtility.*;
pier7afc7522019-05-10 13:19:15 +0200114import static org.onosproject.net.flowobjective.ForwardingObjective.Flag.SPECIFIC;
Pier Luigi075f1012018-02-01 10:23:12 +0100115import static org.onosproject.net.group.GroupDescription.Type.SELECT;
Pier Ventree0ae7a32016-11-23 09:57:42 -0800116import static org.slf4j.LoggerFactory.getLogger;
Pier Ventre140a8942016-11-02 07:26:38 -0700117import static org.onosproject.net.flow.criteria.Criterion.Type.MPLS_BOS;
Pier Ventree0ae7a32016-11-23 09:57:42 -0800118
Saurav Das822c4e22015-10-23 10:51:11 -0700119/**
120 * Driver for Broadcom's OF-DPA v2.0 TTP.
Saurav Das822c4e22015-10-23 10:51:11 -0700121 */
Charles Chan361154b2016-03-24 10:23:39 -0700122public class Ofdpa2Pipeline extends AbstractHandlerBehaviour implements Pipeliner {
pier8b3aef42019-03-11 15:14:02 -0700123 // Timer for the accumulator
124 private static final Timer TIMER = new Timer("fwdobj-batching");
125 private Accumulator<Pair<ForwardingObjective, Collection<FlowRule>>> accumulator;
pier9469f3e2019-04-17 17:05:08 +0200126 // Internal objects
Saurav Das822c4e22015-10-23 10:51:11 -0700127 private final Logger log = getLogger(getClass());
Charles Chan425854b2016-04-11 15:32:12 -0700128 protected ServiceDirectory serviceDirectory;
Saurav Das822c4e22015-10-23 10:51:11 -0700129 protected FlowRuleService flowRuleService;
Charles Chan425854b2016-04-11 15:32:12 -0700130 protected CoreService coreService;
Saurav Das8a0732e2015-11-20 15:27:53 -0800131 protected GroupService groupService;
132 protected FlowObjectiveStore flowObjectiveStore;
Saurav Das822c4e22015-10-23 10:51:11 -0700133 protected DeviceId deviceId;
134 protected ApplicationId driverId;
Saurav Das822c4e22015-10-23 10:51:11 -0700135 protected DeviceService deviceService;
Charles Chan188ebf52015-12-23 00:15:11 -0800136 protected static KryoNamespace appKryo = new KryoNamespace.Builder()
Yi Tsengef19de12017-04-24 11:33:05 -0700137 .register(KryoNamespaces.API)
138 .register(GroupKey.class)
139 .register(DefaultGroupKey.class)
140 .register(OfdpaNextGroup.class)
141 .register(ArrayDeque.class)
142 .build("Ofdpa2Pipeline");
Saurav Das822c4e22015-10-23 10:51:11 -0700143
Charles Chan425854b2016-04-11 15:32:12 -0700144 protected Ofdpa2GroupHandler groupHandler;
Saurav Das822c4e22015-10-23 10:51:11 -0700145
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700146 // flows installations to be retried
pier8b3aef42019-03-11 15:14:02 -0700147 private ScheduledExecutorService retryExecutorService
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700148 = newScheduledThreadPool(5, groupedThreads("OfdpaPipeliner", "retry-%d", log));
Saurav Das4f980082015-11-05 13:39:15 -0800149
pier8b3aef42019-03-11 15:14:02 -0700150 // accumulator executor service
151 private ScheduledExecutorService accumulatorExecutorService
152 = newSingleThreadScheduledExecutor(groupedThreads("OfdpaPipeliner", "acc-%d", log));
153
pier1b7dd122020-02-28 09:24:11 +0100154 protected AtomicBoolean ready = new AtomicBoolean(false);
155
Saurav Das822c4e22015-10-23 10:51:11 -0700156 @Override
157 public void init(DeviceId deviceId, PipelinerContext context) {
pier1b7dd122020-02-28 09:24:11 +0100158 if (!ready.getAndSet(true)) {
159 this.deviceId = deviceId;
Saurav Das822c4e22015-10-23 10:51:11 -0700160
pier1b7dd122020-02-28 09:24:11 +0100161 serviceDirectory = context.directory();
162 coreService = serviceDirectory.get(CoreService.class);
163 flowRuleService = serviceDirectory.get(FlowRuleService.class);
164 groupService = serviceDirectory.get(GroupService.class);
165 flowObjectiveStore = context.store();
166 deviceService = serviceDirectory.get(DeviceService.class);
167 // Init the accumulator, if enabled
168 if (isAccumulatorEnabled(this)) {
169 accumulator = new ForwardingObjectiveAccumulator(context.accumulatorMaxObjectives(),
170 context.accumulatorMaxBatchMillis(),
171 context.accumulatorMaxIdleMillis());
172 }
173
174 initDriverId();
175 initGroupHander(context);
176
177 initializePipeline();
pier8b3aef42019-03-11 15:14:02 -0700178 }
pier1b7dd122020-02-28 09:24:11 +0100179 }
Saurav Das822c4e22015-10-23 10:51:11 -0700180
pier1b7dd122020-02-28 09:24:11 +0100181 @Override
182 public boolean isReady() {
183 return ready.get();
Saurav Das822c4e22015-10-23 10:51:11 -0700184 }
185
pier7afc7522019-05-10 13:19:15 +0200186 void setupAccumulatorForTests(int maxFwd, int maxBatchMS, int maxIdleMS) {
187 if (accumulator == null) {
188 accumulator = new ForwardingObjectiveAccumulator(maxFwd,
189 maxBatchMS,
190 maxIdleMS);
191 }
192 }
193
Charles Chan40132b32017-01-22 00:19:37 -0800194 protected void initDriverId() {
195 driverId = coreService.registerApplication(
196 "org.onosproject.driver.Ofdpa2Pipeline");
197 }
198
199 protected void initGroupHander(PipelinerContext context) {
200 groupHandler = new Ofdpa2GroupHandler();
201 groupHandler.init(deviceId, context);
202 }
203
Saurav Das822c4e22015-10-23 10:51:11 -0700204 protected void initializePipeline() {
Charles Chan188ebf52015-12-23 00:15:11 -0800205 // OF-DPA does not require initializing the pipeline as it puts default
206 // rules automatically in the hardware. However emulation of OFDPA in
207 // software switches does require table-miss-entries.
Saurav Das822c4e22015-10-23 10:51:11 -0700208 }
209
Charles Chand1172632017-03-15 17:33:09 -0700210 /**
Alex Yashchuk4caa8e82017-12-08 17:40:05 +0200211 * Determines whether this pipeline requires MPLS POP instruction.
212 *
213 * @return true to use MPLS POP instruction
214 */
215 public boolean requireMplsPop() {
216 return true;
217 }
218
219 /**
Harshada Chaundkarcb787512019-08-05 15:33:30 +0000220 * Determines whether this pipeline requires one additional flow matching on ethType 0x86dd in ACL table.
221 *
222 * @return true to create one additional flow matching on ethType 0x86dd in ACL table
223 */
224 protected boolean requireEthType() {
225 return true;
226 }
227
228 /**
Alex Yashchuk4caa8e82017-12-08 17:40:05 +0200229 * Determines whether this pipeline requires MPLS BOS match.
230 *
231 * @return true to use MPLS BOS match
232 */
233 public boolean requireMplsBosMatch() {
234 return true;
235 }
236
237 /**
238 * Determines whether this pipeline requires MPLS TTL decrement and copy.
239 *
240 * @return true to use MPLS TTL decrement and copy
241 */
242 public boolean requireMplsTtlModification() {
243 return true;
244 }
245
246 /**
Charles Chand1172632017-03-15 17:33:09 -0700247 * Determines whether this pipeline requires OFDPA match and set VLAN extensions.
248 *
249 * @return true to use the extensions
250 */
251 protected boolean requireVlanExtensions() {
252 return true;
253 }
254
Saurav Das86d13e82017-04-28 17:03:48 -0700255 /**
256 * Determines whether in-port should be matched on in TMAC table rules.
257 *
258 * @return true if match on in-port should be programmed
259 */
260 protected boolean matchInPortTmacTable() {
261 return true;
262 }
263
Charles Chand9e47c62017-10-05 15:17:15 -0700264 /**
265 * Determines whether matching L4 destination port on IPv6 packets is supported in ACL table.
266 *
267 * @return true if matching L4 destination port on IPv6 packets is supported in ACL table.
268 */
269 protected boolean supportIpv6L4Dst() {
270 return true;
271 }
272
Saurav Dasc568c342018-01-25 09:49:01 -0800273 /**
274 * Determines whether this driver should continue to retry flows that point
275 * to empty groups. See CORD-554.
276 *
277 * @return true if the driver should retry flows
278 */
279 protected boolean shouldRetry() {
280 return true;
281 }
282
Charles Chan45b69ab2018-03-02 15:41:41 -0800283 /**
284 * Determines whether this driver requires unicast flow to be installed before multicast flow
285 * in TMAC table.
286 *
287 * @return true if required
288 */
289 protected boolean requireUnicastBeforeMulticast() {
290 return false;
291 }
292
Andrea Campanellad980c6d2018-04-30 11:48:55 +0200293 /**
294 * Determines whether this driver supports installing a clearDeferred action on table 30.
295 *
296 * @return true if required
297 */
298 protected boolean supportsUnicastBlackHole() {
299 return true;
300 }
301
Charles Chanab591602019-01-22 17:25:04 -0800302 protected boolean requirePuntTable() {
303 return false;
304 }
305
Saurav Das822c4e22015-10-23 10:51:11 -0700306 //////////////////////////////////////
307 // Flow Objectives
308 //////////////////////////////////////
309
310 @Override
311 public void filter(FilteringObjective filteringObjective) {
312 if (filteringObjective.type() == FilteringObjective.Type.PERMIT) {
313 processFilter(filteringObjective,
314 filteringObjective.op() == Objective.Operation.ADD,
315 filteringObjective.appId());
316 } else {
317 // Note that packets that don't match the PERMIT filter are
318 // automatically denied. The DENY filter is used to deny packets
319 // that are otherwise permitted by the PERMIT filter.
320 // Use ACL table flow rules here for DENY filtering objectives
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530321 log.warn("filter objective other than PERMIT currently not supported");
Saurav Das822c4e22015-10-23 10:51:11 -0700322 fail(filteringObjective, ObjectiveError.UNSUPPORTED);
323 }
324 }
325
326 @Override
327 public void forward(ForwardingObjective fwd) {
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700328 Collection<FlowRule> rules = processForward(fwd);
Saurav Das25190812016-05-27 13:54:07 -0700329 if (rules == null || rules.isEmpty()) {
330 // Assumes fail message has already been generated to the objective
331 // context. Returning here prevents spurious pass message to be
332 // generated by FlowRule service for empty flowOps.
333 return;
334 }
pier7afc7522019-05-10 13:19:15 +0200335 // Let's accumulate flow rules if accumulator is active and fwd objective is not versatile.
336 // Otherwise send directly, without adding futher delay
337 if (accumulator != null && Objects.equals(fwd.flag(), SPECIFIC)) {
pier8b3aef42019-03-11 15:14:02 -0700338 accumulator.add(Pair.of(fwd, rules));
339 } else {
340 sendForwards(Collections.singletonList(Pair.of(fwd, rules)));
341 }
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700342 }
343
pier8b3aef42019-03-11 15:14:02 -0700344 // Builds the batch using the accumulated flow rules
345 private void sendForwards(List<Pair<ForwardingObjective, Collection<FlowRule>>> pairs) {
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700346 FlowRuleOperations.Builder flowOpsBuilder = FlowRuleOperations.builder();
pier8b3aef42019-03-11 15:14:02 -0700347 log.debug("Sending {} fwd-objs", pairs.size());
348 List<Objective> fwdObjs = Lists.newArrayList();
349 // Iterates over all accumulated flow rules and then build an unique batch
350 pairs.forEach(pair -> {
351 ForwardingObjective fwd = pair.getLeft();
352 Collection<FlowRule> rules = pair.getRight();
353 switch (fwd.op()) {
354 case ADD:
355 rules.stream()
356 .filter(Objects::nonNull)
357 .forEach(flowOpsBuilder::add);
358 log.debug("Applying a add fwd-obj {} to sw:{}", fwd.id(), deviceId);
359 fwdObjs.add(fwd);
360 break;
361 case REMOVE:
362 rules.stream()
363 .filter(Objects::nonNull)
364 .forEach(flowOpsBuilder::remove);
365 log.debug("Deleting a flow rule to sw:{}", deviceId);
366 fwdObjs.add(fwd);
367 break;
368 default:
369 fail(fwd, ObjectiveError.UNKNOWN);
370 log.warn("Unknown forwarding type {}", fwd.op());
371 }
372 });
373 // Finally applies the operations
Saurav Das822c4e22015-10-23 10:51:11 -0700374 flowRuleService.apply(flowOpsBuilder.build(new FlowRuleOperationsContext() {
pier8b3aef42019-03-11 15:14:02 -0700375
Saurav Das822c4e22015-10-23 10:51:11 -0700376 @Override
377 public void onSuccess(FlowRuleOperations ops) {
pier8b3aef42019-03-11 15:14:02 -0700378 log.trace("Flow rule operations onSuccess {}", ops);
pier9469f3e2019-04-17 17:05:08 +0200379 fwdObjs.forEach(OfdpaPipelineUtility::pass);
Saurav Das822c4e22015-10-23 10:51:11 -0700380 }
381
382 @Override
383 public void onError(FlowRuleOperations ops) {
pier8b3aef42019-03-11 15:14:02 -0700384 ObjectiveError error = ObjectiveError.FLOWINSTALLATIONFAILED;
385 log.warn("Flow rule operations onError {}. Reason = {}", ops, error);
386 fwdObjs.forEach(fwdObj -> fail(fwdObj, error));
Saurav Das822c4e22015-10-23 10:51:11 -0700387 }
388 }));
Saurav Das822c4e22015-10-23 10:51:11 -0700389 }
390
391 @Override
392 public void next(NextObjective nextObjective) {
Saurav Das8a0732e2015-11-20 15:27:53 -0800393 NextGroup nextGroup = flowObjectiveStore.getNextGroup(nextObjective.id());
394 switch (nextObjective.op()) {
395 case ADD:
Saurav Das4f980082015-11-05 13:39:15 -0800396 if (nextGroup != null) {
Saurav Das8a0732e2015-11-20 15:27:53 -0800397 log.warn("Cannot add next {} that already exists in device {}",
398 nextObjective.id(), deviceId);
399 return;
400 }
Saurav Das1547b3f2017-05-05 17:01:08 -0700401 log.debug("Processing NextObjective id {} in dev {} - add group",
Saurav Das8a0732e2015-11-20 15:27:53 -0800402 nextObjective.id(), deviceId);
Charles Chan425854b2016-04-11 15:32:12 -0700403 groupHandler.addGroup(nextObjective);
Saurav Das8a0732e2015-11-20 15:27:53 -0800404 break;
405 case ADD_TO_EXISTING:
406 if (nextGroup != null) {
Saurav Das1547b3f2017-05-05 17:01:08 -0700407 log.debug("Processing NextObjective id {} in dev {} - add bucket",
Saurav Das8a0732e2015-11-20 15:27:53 -0800408 nextObjective.id(), deviceId);
Charles Chan425854b2016-04-11 15:32:12 -0700409 groupHandler.addBucketToGroup(nextObjective, nextGroup);
Saurav Das4f980082015-11-05 13:39:15 -0800410 } else {
Saurav Das8a0732e2015-11-20 15:27:53 -0800411 // it is possible that group-chain has not been fully created yet
Saurav Das423fe2b2015-12-04 10:52:59 -0800412 log.debug("Waiting to add bucket to group for next-id:{} in dev:{}",
413 nextObjective.id(), deviceId);
Yi Tseng47f82dc2017-03-05 22:48:39 -0800414
415 // by design multiple pending bucket is allowed for the group
416 groupHandler.pendingBuckets.compute(nextObjective.id(), (nextId, pendBkts) -> {
417 if (pendBkts == null) {
418 pendBkts = Sets.newHashSet();
419 }
420 pendBkts.add(nextObjective);
421 return pendBkts;
422 });
Saurav Das4f980082015-11-05 13:39:15 -0800423 }
Saurav Das8a0732e2015-11-20 15:27:53 -0800424 break;
425 case REMOVE:
426 if (nextGroup == null) {
427 log.warn("Cannot remove next {} that does not exist in device {}",
428 nextObjective.id(), deviceId);
429 return;
430 }
Saurav Das1547b3f2017-05-05 17:01:08 -0700431 log.debug("Processing NextObjective id {} in dev {} - remove group",
Saurav Das8a0732e2015-11-20 15:27:53 -0800432 nextObjective.id(), deviceId);
Charles Chan425854b2016-04-11 15:32:12 -0700433 groupHandler.removeGroup(nextObjective, nextGroup);
Saurav Das8a0732e2015-11-20 15:27:53 -0800434 break;
435 case REMOVE_FROM_EXISTING:
436 if (nextGroup == null) {
437 log.warn("Cannot remove from next {} that does not exist in device {}",
438 nextObjective.id(), deviceId);
439 return;
440 }
Saurav Das1547b3f2017-05-05 17:01:08 -0700441 log.debug("Processing NextObjective id {} in dev {} - remove bucket",
Saurav Das8a0732e2015-11-20 15:27:53 -0800442 nextObjective.id(), deviceId);
Charles Chan425854b2016-04-11 15:32:12 -0700443 groupHandler.removeBucketFromGroup(nextObjective, nextGroup);
Saurav Das8a0732e2015-11-20 15:27:53 -0800444 break;
Jonghwan Hyunf810a7a2018-02-12 16:43:45 +0900445 case MODIFY:
446 if (nextGroup == null) {
447 log.warn("Cannot modify next {} that does not exist in device {}",
448 nextObjective.id(), deviceId);
449 return;
450 }
Ruchi Sahotaef0761c2019-01-28 01:08:18 +0000451 log.debug("Processing NextObjective id {} in dev {} group {} - modify bucket",
452 nextObjective.id(), deviceId, nextGroup);
Jonghwan Hyunf810a7a2018-02-12 16:43:45 +0900453 groupHandler.modifyBucketFromGroup(nextObjective, nextGroup);
454 break;
Saurav Dasceccf242017-08-03 18:30:35 -0700455 case VERIFY:
456 if (nextGroup == null) {
457 log.warn("Cannot verify next {} that does not exist in device {}",
458 nextObjective.id(), deviceId);
459 return;
460 }
461 log.debug("Processing NextObjective id {} in dev {} - verify",
462 nextObjective.id(), deviceId);
463 groupHandler.verifyGroup(nextObjective, nextGroup);
464 break;
Saurav Das8a0732e2015-11-20 15:27:53 -0800465 default:
Saurav Das4f980082015-11-05 13:39:15 -0800466 log.warn("Unsupported operation {}", nextObjective.op());
Saurav Das822c4e22015-10-23 10:51:11 -0700467 }
468 }
469
470 //////////////////////////////////////
471 // Flow handling
472 //////////////////////////////////////
473
474 /**
Yi Tseng0fdc07e2017-04-24 19:17:08 -0700475 * As per OFDPA 2.0 TTP, filtering of VLAN ids and MAC addresses (for routing)
476 * configured on switch ports happen in different tables.
Saurav Das822c4e22015-10-23 10:51:11 -0700477 *
478 * @param filt the filtering objective
479 * @param install indicates whether to add or remove the objective
480 * @param applicationId the application that sent this objective
481 */
Saurav Das52025962016-01-28 22:30:01 -0800482 protected void processFilter(FilteringObjective filt,
483 boolean install, ApplicationId applicationId) {
Saurav Das822c4e22015-10-23 10:51:11 -0700484 // This driver only processes filtering criteria defined with switch
485 // ports as the key
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530486 PortCriterion portCriterion = null;
Saurav Das822c4e22015-10-23 10:51:11 -0700487 EthCriterion ethCriterion = null;
488 VlanIdCriterion vidCriterion = null;
Saurav Das822c4e22015-10-23 10:51:11 -0700489 if (!filt.key().equals(Criteria.dummy()) &&
490 filt.key().type() == Criterion.Type.IN_PORT) {
491 portCriterion = (PortCriterion) filt.key();
Saurav Das822c4e22015-10-23 10:51:11 -0700492 }
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530493 if (portCriterion == null) {
494 log.debug("No IN_PORT defined in filtering objective from app: {}",
495 applicationId);
496 } else {
497 log.debug("Received filtering objective for dev/port: {}/{}", deviceId,
498 portCriterion.port());
499 }
Saurav Das822c4e22015-10-23 10:51:11 -0700500 // convert filtering conditions for switch-intfs into flowrules
501 FlowRuleOperations.Builder ops = FlowRuleOperations.builder();
502 for (Criterion criterion : filt.conditions()) {
Yi Tseng0fdc07e2017-04-24 19:17:08 -0700503 switch (criterion.type()) {
504 case ETH_DST:
505 case ETH_DST_MASKED:
506 ethCriterion = (EthCriterion) criterion;
507 break;
508 case VLAN_VID:
509 vidCriterion = (VlanIdCriterion) criterion;
510 break;
511 default:
512 log.warn("Unsupported filter {}", criterion);
513 fail(filt, ObjectiveError.UNSUPPORTED);
514 return;
Saurav Das822c4e22015-10-23 10:51:11 -0700515 }
516 }
517
Saurav Das0e99e2b2015-10-28 12:39:42 -0700518 VlanId assignedVlan = null;
Charles Chane849c192016-01-11 18:28:54 -0800519 if (vidCriterion != null) {
Charles Chand55e84d2016-03-30 17:54:24 -0700520 // Use the VLAN in criterion if metadata is not present and the traffic is tagged
Charles Chanc550f2e2017-12-19 19:55:57 -0800521 if (!vidCriterion.vlanId().equals(VlanId.NONE)) {
Charles Chane849c192016-01-11 18:28:54 -0800522 assignedVlan = vidCriterion.vlanId();
Piere5bff482018-03-07 11:42:50 +0100523 }
524 // If the meta VLAN is present let's update the assigned vlan
525 if (filt.meta() != null) {
526 VlanId metaVlan = readVlanFromTreatment(filt.meta());
527 if (metaVlan != null) {
528 assignedVlan = metaVlan;
529 }
Charles Chand55e84d2016-03-30 17:54:24 -0700530 }
Charles Chane849c192016-01-11 18:28:54 -0800531
Charles Chand55e84d2016-03-30 17:54:24 -0700532 if (assignedVlan == null) {
533 log.error("Driver fails to extract VLAN information. "
Frank Wangd8ab0962017-08-11 11:09:30 +0800534 + "Not processing VLAN filters on device {}.", deviceId);
Charles Chand55e84d2016-03-30 17:54:24 -0700535 log.debug("VLAN ID in criterion={}, metadata={}",
536 readVlanFromTreatment(filt.meta()), vidCriterion.vlanId());
537 fail(filt, ObjectiveError.BADPARAMS);
538 return;
Saurav Das0e99e2b2015-10-28 12:39:42 -0700539 }
540 }
541
Yi Tseng3a77b4f2017-02-06 15:02:17 -0800542 if (ethCriterion == null || ethCriterion.mac().equals(NONE)) {
Charles Chan985b12e2016-05-11 19:47:22 -0700543 // NOTE: it is possible that a filtering objective only has vidCriterion
Daniel Ginsburgc1d47e92018-04-30 19:27:19 -0400544 log.debug("filtering objective missing dstMac, won't program TMAC table");
Saurav Das822c4e22015-10-23 10:51:11 -0700545 } else {
Charles Chan45b69ab2018-03-02 15:41:41 -0800546 MacAddress unicastMac = readEthDstFromTreatment(filt.meta());
Charles Chan66291502018-03-02 16:43:28 -0800547 List<List<FlowRule>> allStages = processEthDstFilter(portCriterion, ethCriterion,
Charles Chan45b69ab2018-03-02 15:41:41 -0800548 vidCriterion, assignedVlan, unicastMac, applicationId);
Charles Chan66291502018-03-02 16:43:28 -0800549 for (List<FlowRule> flowRules : allStages) {
Charles Chan45b69ab2018-03-02 15:41:41 -0800550 log.trace("Starting a new flow rule stage for TMAC table flow");
Charles Chan66291502018-03-02 16:43:28 -0800551 ops.newStage();
Charles Chanc550f2e2017-12-19 19:55:57 -0800552
Charles Chan66291502018-03-02 16:43:28 -0800553 for (FlowRule flowRule : flowRules) {
pier5be39042019-10-04 10:54:07 +0200554 log.trace("{} flow rule in TMAC table: {} for dev: {}",
555 (install) ? "adding" : "removing", flowRule, deviceId);
Charles Chan66291502018-03-02 16:43:28 -0800556 if (install) {
557 ops = ops.add(flowRule);
Charles Chanc550f2e2017-12-19 19:55:57 -0800558 } else {
Charles Chan66291502018-03-02 16:43:28 -0800559 // NOTE: Only remove TMAC flow when there is no more enabled port within the
560 // same VLAN on this device if TMAC doesn't support matching on in_port.
pier5be39042019-10-04 10:54:07 +0200561 if (filt.meta() != null && filt.meta().clearedDeferred()) {
562 // TMac mcast does not match on the input port - we have to remove it
563 // only if this is the last port
564 FlowRule rule = buildTmacRuleForMcastFromUnicast(flowRule, applicationId);
565 // IPv6 or IPv4 tmac rule
566 if (rule != null) {
567 // Add first the mcast rule and then open a new stage for the unicast
568 ops = ops.remove(rule);
569 ops.newStage();
570 }
571 ops = ops.remove(flowRule);
572 } else if (matchInPortTmacTable()) {
Charles Chan66291502018-03-02 16:43:28 -0800573 ops = ops.remove(flowRule);
574 } else {
pier5be39042019-10-04 10:54:07 +0200575 log.debug("Abort TMAC flow removal on {}. " +
576 "Some other ports still share this TMAC flow", deviceId);
Charles Chan66291502018-03-02 16:43:28 -0800577 }
Charles Chanc550f2e2017-12-19 19:55:57 -0800578 }
579 }
Saurav Das822c4e22015-10-23 10:51:11 -0700580 }
581 }
582
Charles Chan985b12e2016-05-11 19:47:22 -0700583 if (vidCriterion == null) {
584 // NOTE: it is possible that a filtering objective only has ethCriterion
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530585 log.info("filtering objective missing VLAN, cannot program VLAN Table");
Saurav Das822c4e22015-10-23 10:51:11 -0700586 } else {
Charles Chan66291502018-03-02 16:43:28 -0800587 List<List<FlowRule>> allStages = processVlanIdFilter(
pier6aef5b72019-06-10 17:10:26 +0200588 portCriterion, vidCriterion, assignedVlan, applicationId, install);
Charles Chan66291502018-03-02 16:43:28 -0800589 for (List<FlowRule> flowRules : allStages) {
Charles Chan45b69ab2018-03-02 15:41:41 -0800590 log.trace("Starting a new flow rule stage for VLAN table flow");
Charles Chan66291502018-03-02 16:43:28 -0800591 ops.newStage();
Charles Chan14967c22015-12-07 11:11:50 -0800592
Charles Chan66291502018-03-02 16:43:28 -0800593 for (FlowRule flowRule : flowRules) {
594 log.trace("{} flow rules in VLAN table: {} for dev: {}",
595 (install) ? "adding" : "removing", flowRule, deviceId);
596 ops = install ? ops.add(flowRule) : ops.remove(flowRule);
Charles Chand1172632017-03-15 17:33:09 -0700597 }
Saurav Das822c4e22015-10-23 10:51:11 -0700598 }
599 }
600
Saurav Das822c4e22015-10-23 10:51:11 -0700601 // apply filtering flow rules
602 flowRuleService.apply(ops.build(new FlowRuleOperationsContext() {
603 @Override
604 public void onSuccess(FlowRuleOperations ops) {
Saurav Das018605f2017-02-18 14:05:44 -0800605 log.debug("Applied {} filtering rules in device {}",
Saurav Das822c4e22015-10-23 10:51:11 -0700606 ops.stages().get(0).size(), deviceId);
607 pass(filt);
608 }
609
610 @Override
611 public void onError(FlowRuleOperations ops) {
612 log.info("Failed to apply all filtering rules in dev {}", deviceId);
613 fail(filt, ObjectiveError.FLOWINSTALLATIONFAILED);
614 }
615 }));
616
617 }
618
619 /**
Charles Chand1172632017-03-15 17:33:09 -0700620 * Internal implementation of processVlanIdFilter.
621 * <p>
622 * The is_present bit in set_vlan_vid action is required to be 0 in OFDPA i12.
623 * Since it is non-OF spec, we need an extension treatment for that.
624 * The useVlanExtension must be set to false for OFDPA i12.
625 * </p>
Charles Chan66291502018-03-02 16:43:28 -0800626 * <p>
627 * NOTE: Separate VLAN filtering rules and assignment rules
628 * into different stages in order to guarantee that filtering rules
629 * always go first, as required by OFDPA.
630 * </p>
Saurav Das0e99e2b2015-10-28 12:39:42 -0700631 *
Charles Chanf9e98652016-09-07 16:54:23 -0700632 * @param portCriterion port on device for which this filter is programmed
633 * @param vidCriterion vlan assigned to port, or NONE for untagged
634 * @param assignedVlan assigned vlan-id for untagged packets
635 * @param applicationId for application programming this filter
pier6aef5b72019-06-10 17:10:26 +0200636 * @param install indicates whether to add or remove the objective
Charles Chan66291502018-03-02 16:43:28 -0800637 * @return stages of flow rules for port-vlan filters
Saurav Das822c4e22015-10-23 10:51:11 -0700638 */
Charles Chan66291502018-03-02 16:43:28 -0800639 protected List<List<FlowRule>> processVlanIdFilter(PortCriterion portCriterion,
pier6aef5b72019-06-10 17:10:26 +0200640 VlanIdCriterion vidCriterion,
641 VlanId assignedVlan,
642 ApplicationId applicationId,
643 boolean install) {
Charles Chan66291502018-03-02 16:43:28 -0800644 List<FlowRule> filteringRules = new ArrayList<>();
645 List<FlowRule> assignmentRules = new ArrayList<>();
Saurav Das822c4e22015-10-23 10:51:11 -0700646 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
647 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
Charles Chan14967c22015-12-07 11:11:50 -0800648 TrafficSelector.Builder preSelector = null;
649 TrafficTreatment.Builder preTreatment = null;
650
Saurav Das4f980082015-11-05 13:39:15 -0800651 treatment.transition(TMAC_TABLE);
652
Saurav Das822c4e22015-10-23 10:51:11 -0700653 if (vidCriterion.vlanId() == VlanId.NONE) {
654 // untagged packets are assigned vlans
Charles Chand1172632017-03-15 17:33:09 -0700655 preSelector = DefaultTrafficSelector.builder();
656 if (requireVlanExtensions()) {
657 OfdpaMatchVlanVid ofdpaMatchVlanVid = new OfdpaMatchVlanVid(VlanId.NONE);
658 selector.extension(ofdpaMatchVlanVid, deviceId);
Charles Chanf9e98652016-09-07 16:54:23 -0700659 OfdpaSetVlanVid ofdpaSetVlanVid = new OfdpaSetVlanVid(assignedVlan);
660 treatment.extension(ofdpaSetVlanVid, deviceId);
Charles Chand1172632017-03-15 17:33:09 -0700661
662 OfdpaMatchVlanVid preOfdpaMatchVlanVid = new OfdpaMatchVlanVid(assignedVlan);
663 preSelector.extension(preOfdpaMatchVlanVid, deviceId);
Charles Chanf9e98652016-09-07 16:54:23 -0700664 } else {
Charles Chand1172632017-03-15 17:33:09 -0700665 selector.matchVlanId(VlanId.NONE);
Charles Chanf9e98652016-09-07 16:54:23 -0700666 treatment.setVlanId(assignedVlan);
Charles Chand1172632017-03-15 17:33:09 -0700667
668 preSelector.matchVlanId(assignedVlan);
669 }
670 preTreatment = DefaultTrafficTreatment.builder().transition(TMAC_TABLE);
671 } else {
672 if (requireVlanExtensions()) {
673 OfdpaMatchVlanVid ofdpaMatchVlanVid = new OfdpaMatchVlanVid(vidCriterion.vlanId());
674 selector.extension(ofdpaMatchVlanVid, deviceId);
675 } else {
676 selector.matchVlanId(vidCriterion.vlanId());
Charles Chanf9e98652016-09-07 16:54:23 -0700677 }
Charles Chan14967c22015-12-07 11:11:50 -0800678
Charles Chand55e84d2016-03-30 17:54:24 -0700679 if (!assignedVlan.equals(vidCriterion.vlanId())) {
Charles Chand1172632017-03-15 17:33:09 -0700680 if (requireVlanExtensions()) {
Charles Chanc03782d2017-01-31 13:57:55 -0800681 OfdpaSetVlanVid ofdpaSetVlanVid = new OfdpaSetVlanVid(assignedVlan);
682 treatment.extension(ofdpaSetVlanVid, deviceId);
683 } else {
684 treatment.setVlanId(assignedVlan);
685 }
Charles Chand55e84d2016-03-30 17:54:24 -0700686 }
Saurav Das822c4e22015-10-23 10:51:11 -0700687 }
Saurav Das822c4e22015-10-23 10:51:11 -0700688
689 // ofdpa cannot match on ALL portnumber, so we need to use separate
690 // rules for each port.
Charles Chan14967c22015-12-07 11:11:50 -0800691 List<PortNumber> portnums = new ArrayList<>();
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530692 if (portCriterion != null) {
693 if (PortNumber.ALL.equals(portCriterion.port())) {
694 for (Port port : deviceService.getPorts(deviceId)) {
695 if (port.number().toLong() > 0 && port.number().toLong() < OFPP_MAX) {
696 portnums.add(port.number());
697 }
Saurav Das822c4e22015-10-23 10:51:11 -0700698 }
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530699 } else {
700 portnums.add(portCriterion.port());
Saurav Das822c4e22015-10-23 10:51:11 -0700701 }
702 } else {
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530703 log.warn("Filtering Objective missing Port Criterion . " +
Charles Chan66291502018-03-02 16:43:28 -0800704 "VLAN Table cannot be programmed for {}", deviceId);
Saurav Das822c4e22015-10-23 10:51:11 -0700705 }
Saurav Das4f980082015-11-05 13:39:15 -0800706
Saurav Das822c4e22015-10-23 10:51:11 -0700707 for (PortNumber pnum : portnums) {
Saurav Das4f980082015-11-05 13:39:15 -0800708 // create rest of flowrule
Saurav Das822c4e22015-10-23 10:51:11 -0700709 selector.matchInPort(pnum);
710 FlowRule rule = DefaultFlowRule.builder()
711 .forDevice(deviceId)
712 .withSelector(selector.build())
713 .withTreatment(treatment.build())
714 .withPriority(DEFAULT_PRIORITY)
715 .fromApp(applicationId)
716 .makePermanent()
717 .forTable(VLAN_TABLE).build();
Charles Chan66291502018-03-02 16:43:28 -0800718 assignmentRules.add(rule);
Charles Chan14967c22015-12-07 11:11:50 -0800719
720 if (preSelector != null) {
721 preSelector.matchInPort(pnum);
722 FlowRule preRule = DefaultFlowRule.builder()
723 .forDevice(deviceId)
724 .withSelector(preSelector.build())
725 .withTreatment(preTreatment.build())
726 .withPriority(DEFAULT_PRIORITY)
727 .fromApp(applicationId)
728 .makePermanent()
729 .forTable(VLAN_TABLE).build();
Charles Chan66291502018-03-02 16:43:28 -0800730 filteringRules.add(preRule);
Charles Chan14967c22015-12-07 11:11:50 -0800731 }
Saurav Das822c4e22015-10-23 10:51:11 -0700732 }
pier6aef5b72019-06-10 17:10:26 +0200733 return install ? ImmutableList.of(filteringRules, assignmentRules) :
734 ImmutableList.of(assignmentRules, filteringRules);
Saurav Das822c4e22015-10-23 10:51:11 -0700735 }
736
737 /**
738 * Allows routed packets with correct destination MAC to be directed
Charles Chan45b69ab2018-03-02 15:41:41 -0800739 * to unicast routing table, multicast routing table or MPLS forwarding table.
Saurav Das822c4e22015-10-23 10:51:11 -0700740 *
741 * @param portCriterion port on device for which this filter is programmed
742 * @param ethCriterion dstMac of device for which is filter is programmed
743 * @param vidCriterion vlan assigned to port, or NONE for untagged
Saurav Das0e99e2b2015-10-28 12:39:42 -0700744 * @param assignedVlan assigned vlan-id for untagged packets
Charles Chan45b69ab2018-03-02 15:41:41 -0800745 * @param unicastMac some switches require a unicast TMAC flow to be programmed before multicast
746 * TMAC flow. This MAC address will be used for the unicast TMAC flow.
747 * This is unused if the filtering objective is a unicast.
Saurav Das822c4e22015-10-23 10:51:11 -0700748 * @param applicationId for application programming this filter
Charles Chan66291502018-03-02 16:43:28 -0800749 * @return stages of flow rules for port-vlan filters
Saurav Das822c4e22015-10-23 10:51:11 -0700750
751 */
Charles Chan66291502018-03-02 16:43:28 -0800752 protected List<List<FlowRule>> processEthDstFilter(PortCriterion portCriterion,
Saurav Das822c4e22015-10-23 10:51:11 -0700753 EthCriterion ethCriterion,
754 VlanIdCriterion vidCriterion,
Saurav Das0e99e2b2015-10-28 12:39:42 -0700755 VlanId assignedVlan,
Charles Chan45b69ab2018-03-02 15:41:41 -0800756 MacAddress unicastMac,
Saurav Das822c4e22015-10-23 10:51:11 -0700757 ApplicationId applicationId) {
Charles Chan5270ed02016-01-30 23:22:37 -0800758 // Consider PortNumber.ANY as wildcard. Match ETH_DST only
Sivachidambaram Subramanianc598d062017-05-16 22:42:06 +0530759 if (portCriterion != null && PortNumber.ANY.equals(portCriterion.port())) {
Charles Chan5270ed02016-01-30 23:22:37 -0800760 return processEthDstOnlyFilter(ethCriterion, applicationId);
761 }
762
Charles Chan5b9df8d2016-03-28 22:21:40 -0700763 // Multicast MAC
764 if (ethCriterion.mask() != null) {
Charles Chan45b69ab2018-03-02 15:41:41 -0800765 return processMcastEthDstFilter(ethCriterion, assignedVlan, unicastMac, applicationId);
Charles Chan5b9df8d2016-03-28 22:21:40 -0700766 }
767
Saurav Das822c4e22015-10-23 10:51:11 -0700768 //handling untagged packets via assigned VLAN
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530769 if (vidCriterion != null && vidCriterion.vlanId() == VlanId.NONE) {
Saurav Das0e99e2b2015-10-28 12:39:42 -0700770 vidCriterion = (VlanIdCriterion) Criteria.matchVlanId(assignedVlan);
Saurav Das822c4e22015-10-23 10:51:11 -0700771 }
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530772 List<FlowRule> rules = new ArrayList<>();
773 OfdpaMatchVlanVid ofdpaMatchVlanVid = null;
774 if (vidCriterion != null && requireVlanExtensions()) {
775 ofdpaMatchVlanVid = new OfdpaMatchVlanVid(vidCriterion.vlanId());
776 }
Saurav Das822c4e22015-10-23 10:51:11 -0700777 // ofdpa cannot match on ALL portnumber, so we need to use separate
778 // rules for each port.
Yuta HIGUCHI2dce08a2017-04-20 21:57:48 -0700779 List<PortNumber> portnums = new ArrayList<>();
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530780 if (portCriterion != null) {
781 if (PortNumber.ALL.equals(portCriterion.port())) {
782 for (Port port : deviceService.getPorts(deviceId)) {
783 if (port.number().toLong() > 0 && port.number().toLong() < OFPP_MAX) {
784 portnums.add(port.number());
785 }
Saurav Das822c4e22015-10-23 10:51:11 -0700786 }
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530787 } else {
788 portnums.add(portCriterion.port());
789 }
790 for (PortNumber pnum : portnums) {
791 rules.add(buildTmacRuleForIpv4(ethCriterion,
792 vidCriterion,
793 ofdpaMatchVlanVid,
794 applicationId,
795 pnum));
796 rules.add(buildTmacRuleForMpls(ethCriterion,
797 vidCriterion,
798 ofdpaMatchVlanVid,
799 applicationId,
800 pnum));
801 rules.add(buildTmacRuleForIpv6(ethCriterion,
802 vidCriterion,
803 ofdpaMatchVlanVid,
804 applicationId,
805 pnum));
Saurav Das822c4e22015-10-23 10:51:11 -0700806 }
807 } else {
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530808 rules.add(buildTmacRuleForIpv4(ethCriterion,
809 vidCriterion,
810 ofdpaMatchVlanVid,
811 applicationId,
812 null));
813 rules.add(buildTmacRuleForMpls(ethCriterion,
814 vidCriterion,
815 ofdpaMatchVlanVid,
816 applicationId,
817 null));
818 rules.add(buildTmacRuleForIpv6(ethCriterion,
819 vidCriterion,
820 ofdpaMatchVlanVid,
821 applicationId,
822 null));
Saurav Das822c4e22015-10-23 10:51:11 -0700823 }
Charles Chan66291502018-03-02 16:43:28 -0800824 return ImmutableList.of(rules);
Saurav Das822c4e22015-10-23 10:51:11 -0700825 }
826
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530827 /**
828 * Builds TMAC rules for IPv4 packets.
829 *
Charles Chanc550f2e2017-12-19 19:55:57 -0800830 * @param ethCriterion dst mac matching
831 * @param vidCriterion vlan id assigned to the port
832 * @param ofdpaMatchVlanVid OFDPA vlan id matching
833 * @param applicationId application id
834 * @param pnum port number
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530835 * @return TMAC rule for IPV4 packets
836 */
837 private FlowRule buildTmacRuleForIpv4(EthCriterion ethCriterion,
838 VlanIdCriterion vidCriterion,
839 OfdpaMatchVlanVid ofdpaMatchVlanVid,
840 ApplicationId applicationId,
841 PortNumber pnum) {
842 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
843 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
844 if (pnum != null) {
845 if (matchInPortTmacTable()) {
846 selector.matchInPort(pnum);
847 } else {
Saurav Dasf14d9ef2017-12-05 15:00:23 -0800848 log.debug("Pipeline does not support IN_PORT matching in TMAC table, " +
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530849 "ignoring the IN_PORT criteria");
850 }
851 }
852 if (vidCriterion != null) {
853 if (requireVlanExtensions()) {
854 selector.extension(ofdpaMatchVlanVid, deviceId);
855 } else {
856 selector.matchVlanId(vidCriterion.vlanId());
857 }
858 }
859 selector.matchEthType(Ethernet.TYPE_IPV4);
860 selector.matchEthDst(ethCriterion.mac());
861 treatment.transition(UNICAST_ROUTING_TABLE);
862 return DefaultFlowRule.builder()
863 .forDevice(deviceId)
864 .withSelector(selector.build())
865 .withTreatment(treatment.build())
866 .withPriority(DEFAULT_PRIORITY)
867 .fromApp(applicationId)
868 .makePermanent()
869 .forTable(TMAC_TABLE).build();
870 }
871
872 /**
873 * Builds TMAC rule for MPLS packets.
874 *
Charles Chanc550f2e2017-12-19 19:55:57 -0800875 * @param ethCriterion dst mac matching
876 * @param vidCriterion vlan id assigned to the port
877 * @param ofdpaMatchVlanVid OFDPA vlan id matching
878 * @param applicationId application id
879 * @param pnum port number
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530880 * @return TMAC rule for MPLS packets
881 */
882 private FlowRule buildTmacRuleForMpls(EthCriterion ethCriterion,
883 VlanIdCriterion vidCriterion,
884 OfdpaMatchVlanVid ofdpaMatchVlanVid,
885 ApplicationId applicationId,
886 PortNumber pnum) {
887 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
888 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
889 if (pnum != null) {
890 if (matchInPortTmacTable()) {
891 selector.matchInPort(pnum);
892 } else {
Saurav Dasf14d9ef2017-12-05 15:00:23 -0800893 log.debug("Pipeline does not support IN_PORT matching in TMAC table, " +
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530894 "ignoring the IN_PORT criteria");
895 }
896 }
897 if (vidCriterion != null) {
898 if (requireVlanExtensions()) {
899 selector.extension(ofdpaMatchVlanVid, deviceId);
900 } else {
901 selector.matchVlanId(vidCriterion.vlanId());
902 }
903 }
904 selector.matchEthType(Ethernet.MPLS_UNICAST);
905 selector.matchEthDst(ethCriterion.mac());
906 treatment.transition(MPLS_TABLE_0);
907 return DefaultFlowRule.builder()
908 .forDevice(deviceId)
909 .withSelector(selector.build())
910 .withTreatment(treatment.build())
911 .withPriority(DEFAULT_PRIORITY)
912 .fromApp(applicationId)
913 .makePermanent()
914 .forTable(TMAC_TABLE).build();
915 }
916
917 /**
918 * Builds TMAC rules for IPv6 packets.
919 *
Charles Chanc550f2e2017-12-19 19:55:57 -0800920 * @param ethCriterion dst mac matching
921 * @param vidCriterion vlan id assigned to the port
922 * @param ofdpaMatchVlanVid OFDPA vlan id matching
923 * @param applicationId application id
924 * @param pnum port number
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530925 * @return TMAC rule for IPV6 packets
926 */
927 private FlowRule buildTmacRuleForIpv6(EthCriterion ethCriterion,
928 VlanIdCriterion vidCriterion,
929 OfdpaMatchVlanVid ofdpaMatchVlanVid,
930 ApplicationId applicationId,
931 PortNumber pnum) {
932 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
933 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
934 if (pnum != null) {
935 if (matchInPortTmacTable()) {
936 selector.matchInPort(pnum);
937 } else {
Saurav Dasf14d9ef2017-12-05 15:00:23 -0800938 log.debug("Pipeline does not support IN_PORT matching in TMAC table, " +
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530939 "ignoring the IN_PORT criteria");
940 }
941 }
942 if (vidCriterion != null) {
943 if (requireVlanExtensions()) {
944 selector.extension(ofdpaMatchVlanVid, deviceId);
945 } else {
946 selector.matchVlanId(vidCriterion.vlanId());
947 }
948 }
949 selector.matchEthType(Ethernet.TYPE_IPV6);
950 selector.matchEthDst(ethCriterion.mac());
951 treatment.transition(UNICAST_ROUTING_TABLE);
952 return 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();
960 }
961
Charles Chan66291502018-03-02 16:43:28 -0800962 protected List<List<FlowRule>> processEthDstOnlyFilter(EthCriterion ethCriterion,
Yi Tsengef19de12017-04-24 11:33:05 -0700963 ApplicationId applicationId) {
Pier Luigi0e358632017-01-31 09:35:05 -0800964 ImmutableList.Builder<FlowRule> builder = ImmutableList.builder();
965
Charles Chan5270ed02016-01-30 23:22:37 -0800966 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
967 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
968 selector.matchEthType(Ethernet.TYPE_IPV4);
969 selector.matchEthDst(ethCriterion.mac());
970 treatment.transition(UNICAST_ROUTING_TABLE);
971 FlowRule rule = DefaultFlowRule.builder()
972 .forDevice(deviceId)
973 .withSelector(selector.build())
974 .withTreatment(treatment.build())
975 .withPriority(DEFAULT_PRIORITY)
976 .fromApp(applicationId)
977 .makePermanent()
978 .forTable(TMAC_TABLE).build();
Pier Luigi0e358632017-01-31 09:35:05 -0800979 builder.add(rule);
980
981 selector = DefaultTrafficSelector.builder();
982 treatment = DefaultTrafficTreatment.builder();
983 selector.matchEthType(Ethernet.TYPE_IPV6);
984 selector.matchEthDst(ethCriterion.mac());
985 treatment.transition(UNICAST_ROUTING_TABLE);
986 rule = DefaultFlowRule.builder()
987 .forDevice(deviceId)
988 .withSelector(selector.build())
989 .withTreatment(treatment.build())
990 .withPriority(DEFAULT_PRIORITY)
991 .fromApp(applicationId)
992 .makePermanent()
993 .forTable(TMAC_TABLE).build();
Charles Chan66291502018-03-02 16:43:28 -0800994 return ImmutableList.of(builder.add(rule).build());
Charles Chan5270ed02016-01-30 23:22:37 -0800995 }
996
pier5be39042019-10-04 10:54:07 +0200997 private FlowRule buildTmacRuleForMcastFromUnicast(FlowRule tmacRuleForUnicast, ApplicationId applicationId) {
998 final TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
999 final TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
1000 FlowRule rule;
1001 // Build the selector
1002 for (Criterion criterion : tmacRuleForUnicast.selector().criteria()) {
1003 if (criterion instanceof VlanIdCriterion) {
1004 if (requireVlanExtensions()) {
1005 OfdpaMatchVlanVid ofdpaMatchVlanVid = new OfdpaMatchVlanVid(((VlanIdCriterion) criterion).vlanId());
1006 selector.extension(ofdpaMatchVlanVid, deviceId);
1007 } else {
1008 selector.add(criterion);
1009 }
1010 } else if (criterion instanceof EthTypeCriterion) {
1011 selector.add(criterion);
1012 if (Objects.equals(((EthTypeCriterion) criterion).ethType(),
1013 EtherType.IPV4.ethType())) {
1014 selector.matchEthDstMasked(IPV4_MULTICAST, IPV4_MULTICAST_MASK);
1015 } else if (Objects.equals(((EthTypeCriterion) criterion).ethType(),
1016 EtherType.IPV6.ethType())) {
1017 selector.matchEthDstMasked(IPV6_MULTICAST, IPV6_MULTICAST_MASK);
1018 } else {
1019 // We don't need for mpls rules
1020 return null;
1021 }
1022 }
1023 }
1024 // Build the treatment
1025 treatment.transition(MULTICAST_ROUTING_TABLE);
1026 // Build the flowrule
1027 rule = DefaultFlowRule.builder()
1028 .forDevice(deviceId)
1029 .withSelector(selector.build())
1030 .withTreatment(treatment.build())
1031 .withPriority(DEFAULT_PRIORITY)
1032 .fromApp(applicationId)
1033 .makePermanent()
1034 .forTable(TMAC_TABLE).build();
pierbf688182019-12-06 12:39:51 +01001035 log.debug("Building TMac Mcast flowRule {}", rule);
pier5be39042019-10-04 10:54:07 +02001036 return rule;
1037 }
1038
Charles Chan66291502018-03-02 16:43:28 -08001039 List<List<FlowRule>> processMcastEthDstFilter(EthCriterion ethCriterion,
Charles Chanb4879a52017-10-20 19:09:16 -07001040 VlanId assignedVlan,
Charles Chan45b69ab2018-03-02 15:41:41 -08001041 MacAddress unicastMac,
Yi Tsengef19de12017-04-24 11:33:05 -07001042 ApplicationId applicationId) {
Charles Chan45b69ab2018-03-02 15:41:41 -08001043 ImmutableList.Builder<FlowRule> unicastFlows = ImmutableList.builder();
1044 ImmutableList.Builder<FlowRule> multicastFlows = ImmutableList.builder();
1045 TrafficSelector.Builder selector;
1046 TrafficTreatment.Builder treatment;
Charles Chanb4879a52017-10-20 19:09:16 -07001047 FlowRule rule;
Julia Ferguson65428c32017-08-10 18:15:24 +00001048
Charles Chanb4879a52017-10-20 19:09:16 -07001049 if (IPV4_MULTICAST.equals(ethCriterion.mac())) {
Charles Chan45b69ab2018-03-02 15:41:41 -08001050 if (requireUnicastBeforeMulticast()) {
1051 selector = DefaultTrafficSelector.builder();
1052 treatment = DefaultTrafficTreatment.builder();
1053 selector.matchEthType(Ethernet.TYPE_IPV4);
1054 selector.matchEthDst(unicastMac);
Charles Chan056e0c12018-05-10 22:19:49 +00001055 selector.matchVlanId(assignedVlan);
Charles Chan45b69ab2018-03-02 15:41:41 -08001056 treatment.transition(UNICAST_ROUTING_TABLE);
1057 rule = DefaultFlowRule.builder()
1058 .forDevice(deviceId)
1059 .withSelector(selector.build())
1060 .withTreatment(treatment.build())
Charles Chan056e0c12018-05-10 22:19:49 +00001061 .withPriority(DEFAULT_PRIORITY)
Charles Chan45b69ab2018-03-02 15:41:41 -08001062 .fromApp(applicationId)
1063 .makePermanent()
1064 .forTable(TMAC_TABLE).build();
1065 unicastFlows.add(rule);
1066 }
1067
1068 selector = DefaultTrafficSelector.builder();
1069 treatment = DefaultTrafficTreatment.builder();
Charles Chanb4879a52017-10-20 19:09:16 -07001070 selector.matchEthType(Ethernet.TYPE_IPV4);
1071 selector.matchEthDstMasked(ethCriterion.mac(), ethCriterion.mask());
Charles Chan056e0c12018-05-10 22:19:49 +00001072 selector.matchVlanId(assignedVlan);
Charles Chanb4879a52017-10-20 19:09:16 -07001073 treatment.transition(MULTICAST_ROUTING_TABLE);
1074 rule = DefaultFlowRule.builder()
1075 .forDevice(deviceId)
1076 .withSelector(selector.build())
1077 .withTreatment(treatment.build())
Charles Chan056e0c12018-05-10 22:19:49 +00001078 .withPriority(DEFAULT_PRIORITY)
Charles Chanb4879a52017-10-20 19:09:16 -07001079 .fromApp(applicationId)
1080 .makePermanent()
1081 .forTable(TMAC_TABLE).build();
Charles Chan45b69ab2018-03-02 15:41:41 -08001082 multicastFlows.add(rule);
Charles Chanb4879a52017-10-20 19:09:16 -07001083 }
1084
1085 if (IPV6_MULTICAST.equals(ethCriterion.mac())) {
Charles Chan45b69ab2018-03-02 15:41:41 -08001086 if (requireUnicastBeforeMulticast()) {
1087 selector = DefaultTrafficSelector.builder();
1088 treatment = DefaultTrafficTreatment.builder();
1089 selector.matchEthType(Ethernet.TYPE_IPV6);
1090 selector.matchEthDst(unicastMac);
Charles Chan056e0c12018-05-10 22:19:49 +00001091 selector.matchVlanId(assignedVlan);
Charles Chan45b69ab2018-03-02 15:41:41 -08001092 treatment.transition(UNICAST_ROUTING_TABLE);
1093 rule = DefaultFlowRule.builder()
1094 .forDevice(deviceId)
1095 .withSelector(selector.build())
1096 .withTreatment(treatment.build())
Charles Chan056e0c12018-05-10 22:19:49 +00001097 .withPriority(DEFAULT_PRIORITY)
Charles Chan45b69ab2018-03-02 15:41:41 -08001098 .fromApp(applicationId)
1099 .makePermanent()
1100 .forTable(TMAC_TABLE).build();
1101 unicastFlows.add(rule);
1102 }
1103
Charles Chanb4879a52017-10-20 19:09:16 -07001104 selector = DefaultTrafficSelector.builder();
1105 treatment = DefaultTrafficTreatment.builder();
1106 selector.matchEthType(Ethernet.TYPE_IPV6);
1107 selector.matchEthDstMasked(ethCriterion.mac(), ethCriterion.mask());
Charles Chan056e0c12018-05-10 22:19:49 +00001108 selector.matchVlanId(assignedVlan);
Charles Chanb4879a52017-10-20 19:09:16 -07001109 treatment.transition(MULTICAST_ROUTING_TABLE);
1110 rule = DefaultFlowRule.builder()
1111 .forDevice(deviceId)
1112 .withSelector(selector.build())
1113 .withTreatment(treatment.build())
Charles Chan056e0c12018-05-10 22:19:49 +00001114 .withPriority(DEFAULT_PRIORITY)
Charles Chanb4879a52017-10-20 19:09:16 -07001115 .fromApp(applicationId)
1116 .makePermanent()
1117 .forTable(TMAC_TABLE).build();
Charles Chan45b69ab2018-03-02 15:41:41 -08001118 multicastFlows.add(rule);
Charles Chanb4879a52017-10-20 19:09:16 -07001119 }
Charles Chan45b69ab2018-03-02 15:41:41 -08001120 return ImmutableList.of(unicastFlows.build(), multicastFlows.build());
Charles Chan5b9df8d2016-03-28 22:21:40 -07001121 }
1122
Saurav Das822c4e22015-10-23 10:51:11 -07001123 private Collection<FlowRule> processForward(ForwardingObjective fwd) {
1124 switch (fwd.flag()) {
1125 case SPECIFIC:
1126 return processSpecific(fwd);
1127 case VERSATILE:
1128 return processVersatile(fwd);
Jonghwan Hyun800d9d02018-04-09 09:40:50 -07001129 case EGRESS:
1130 return processEgress(fwd);
Saurav Das822c4e22015-10-23 10:51:11 -07001131 default:
1132 fail(fwd, ObjectiveError.UNKNOWN);
1133 log.warn("Unknown forwarding flag {}", fwd.flag());
1134 }
1135 return Collections.emptySet();
1136 }
1137
1138 /**
Jonghwan Hyun800d9d02018-04-09 09:40:50 -07001139 * In the OF-DPA 2.0 pipeline, egress forwarding objectives go to the
1140 * egress tables.
1141 * @param fwd the forwarding objective of type 'egress'
1142 * @return a collection of flow rules to be sent to the switch. An empty
1143 * collection may be returned if there is a problem in processing
1144 * the flow rule
1145 */
1146 protected Collection<FlowRule> processEgress(ForwardingObjective fwd) {
1147 log.debug("Processing egress forwarding objective:{} in dev:{}",
1148 fwd, deviceId);
1149
1150 List<FlowRule> rules = new ArrayList<>();
1151
1152 // Build selector
1153 TrafficSelector.Builder sb = DefaultTrafficSelector.builder();
1154 VlanIdCriterion vlanIdCriterion = (VlanIdCriterion) fwd.selector().getCriterion(Criterion.Type.VLAN_VID);
1155 if (vlanIdCriterion == null) {
1156 log.error("Egress forwarding objective:{} must include vlanId", fwd.id());
1157 fail(fwd, ObjectiveError.BADPARAMS);
1158 return rules;
1159 }
1160
1161 Optional<Instruction> outInstr = fwd.treatment().allInstructions().stream()
1162 .filter(instruction -> instruction instanceof OutputInstruction).findFirst();
1163 if (!outInstr.isPresent()) {
1164 log.error("Egress forwarding objective:{} must include output port", fwd.id());
1165 fail(fwd, ObjectiveError.BADPARAMS);
1166 return rules;
1167 }
1168
1169 PortNumber portNumber = ((OutputInstruction) outInstr.get()).port();
1170
1171 sb.matchVlanId(vlanIdCriterion.vlanId());
1172 OfdpaMatchActsetOutput actsetOutput = new OfdpaMatchActsetOutput(portNumber);
1173 sb.extension(actsetOutput, deviceId);
1174
1175 sb.extension(new OfdpaMatchAllowVlanTranslation(ALLOW_VLAN_TRANSLATION), deviceId);
1176
1177 // Build a flow rule for Egress VLAN Flow table
1178 TrafficTreatment.Builder tb = DefaultTrafficTreatment.builder();
1179 tb.transition(EGRESS_DSCP_PCP_REMARK_FLOW_TABLE);
1180 if (fwd.treatment() != null) {
1181 for (Instruction instr : fwd.treatment().allInstructions()) {
1182 if (instr instanceof L2ModificationInstruction &&
1183 ((L2ModificationInstruction) instr).subtype() == L2SubType.VLAN_ID) {
1184 tb.immediate().add(instr);
1185 }
1186 if (instr instanceof L2ModificationInstruction &&
1187 ((L2ModificationInstruction) instr).subtype() == L2SubType.VLAN_PUSH) {
1188 tb.immediate().pushVlan();
1189 EthType ethType = ((L2ModificationInstruction.ModVlanHeaderInstruction) instr).ethernetType();
1190 if (ethType.equals(EtherType.QINQ.ethType())) {
1191 // Build a flow rule for Egress TPID Flow table
1192 TrafficSelector tpidSelector = DefaultTrafficSelector.builder()
1193 .extension(actsetOutput, deviceId)
1194 .matchVlanId(VlanId.ANY).build();
1195
1196 TrafficTreatment tpidTreatment = DefaultTrafficTreatment.builder()
1197 .extension(new Ofdpa3CopyField(COPY_FIELD_NBITS, COPY_FIELD_OFFSET,
1198 COPY_FIELD_OFFSET, OXM_ID_VLAN_VID,
1199 OXM_ID_PACKET_REG_1),
1200 deviceId)
1201 .popVlan()
1202 .pushVlan(EtherType.QINQ.ethType())
1203 .extension(new Ofdpa3CopyField(COPY_FIELD_NBITS, COPY_FIELD_OFFSET,
1204 COPY_FIELD_OFFSET, OXM_ID_PACKET_REG_1,
1205 OXM_ID_VLAN_VID),
1206 deviceId)
1207 .build();
1208
1209 FlowRule.Builder tpidRuleBuilder = DefaultFlowRule.builder()
1210 .fromApp(fwd.appId())
1211 .withPriority(fwd.priority())
1212 .forDevice(deviceId)
1213 .withSelector(tpidSelector)
1214 .withTreatment(tpidTreatment)
1215 .makePermanent()
1216 .forTable(EGRESS_TPID_FLOW_TABLE);
1217 rules.add(tpidRuleBuilder.build());
1218 }
1219 }
1220 }
1221 }
1222
1223 FlowRule.Builder ruleBuilder = DefaultFlowRule.builder()
1224 .fromApp(fwd.appId())
1225 .withPriority(fwd.priority())
1226 .forDevice(deviceId)
1227 .withSelector(sb.build())
1228 .withTreatment(tb.build())
1229 .makePermanent()
1230 .forTable(EGRESS_VLAN_FLOW_TABLE);
1231 rules.add(ruleBuilder.build());
1232 return rules;
1233 }
1234
1235 /**
Saurav Das822c4e22015-10-23 10:51:11 -07001236 * In the OF-DPA 2.0 pipeline, versatile forwarding objectives go to the
1237 * ACL table.
1238 * @param fwd the forwarding objective of type 'versatile'
1239 * @return a collection of flow rules to be sent to the switch. An empty
1240 * collection may be returned if there is a problem in processing
1241 * the flow rule
1242 */
Saurav Das52025962016-01-28 22:30:01 -08001243 protected Collection<FlowRule> processVersatile(ForwardingObjective fwd) {
Saurav Das018605f2017-02-18 14:05:44 -08001244 log.debug("Processing versatile forwarding objective:{} in dev:{}",
Saurav Dasd2fded02016-12-02 15:43:47 -08001245 fwd.id(), deviceId);
Harshada Chaundkarcb787512019-08-05 15:33:30 +00001246 List<FlowRule> flowRules = new ArrayList<>();
1247 final AtomicBoolean ethTypeUsed = new AtomicBoolean(false);
Saurav Das822c4e22015-10-23 10:51:11 -07001248
Saurav Das822c4e22015-10-23 10:51:11 -07001249 if (fwd.nextId() == null && fwd.treatment() == null) {
1250 log.error("Forwarding objective {} from {} must contain "
1251 + "nextId or Treatment", fwd.selector(), fwd.appId());
Zsolt Haraszti9faab752016-02-17 15:55:20 -08001252 fail(fwd, ObjectiveError.BADPARAMS);
Saurav Das822c4e22015-10-23 10:51:11 -07001253 return Collections.emptySet();
1254 }
Saurav Das49cb5a12016-01-16 22:54:07 -08001255
Saurav Das77b5e902016-01-27 17:01:59 -08001256 TrafficSelector.Builder sbuilder = DefaultTrafficSelector.builder();
1257 fwd.selector().criteria().forEach(criterion -> {
1258 if (criterion instanceof VlanIdCriterion) {
1259 VlanId vlanId = ((VlanIdCriterion) criterion).vlanId();
1260 // ensure that match does not include vlan = NONE as OF-DPA does not
1261 // match untagged packets this way in the ACL table.
1262 if (vlanId.equals(VlanId.NONE)) {
1263 return;
1264 }
Charles Chand1172632017-03-15 17:33:09 -07001265 if (requireVlanExtensions()) {
1266 OfdpaMatchVlanVid ofdpaMatchVlanVid = new OfdpaMatchVlanVid(vlanId);
1267 sbuilder.extension(ofdpaMatchVlanVid, deviceId);
1268 } else {
1269 sbuilder.matchVlanId(vlanId);
1270 }
Charles Chan09bf2692018-01-11 11:48:18 -08001271 } else if (criterion instanceof Icmpv6TypeCriterion) {
1272 byte icmpv6Type = (byte) ((Icmpv6TypeCriterion) criterion).icmpv6Type();
1273 sbuilder.matchIcmpv6Type(icmpv6Type);
1274 } else if (criterion instanceof Icmpv6CodeCriterion) {
1275 byte icmpv6Code = (byte) ((Icmpv6CodeCriterion) criterion).icmpv6Code();
1276 sbuilder.matchIcmpv6Type(icmpv6Code);
Charles Chand9e47c62017-10-05 15:17:15 -07001277 } else if (criterion instanceof TcpPortCriterion || criterion instanceof UdpPortCriterion) {
1278 // FIXME: QMX switches do not support L4 dst port matching in ACL table.
1279 // Currently L4 dst port matching is only used by DHCP relay feature
1280 // and therefore is safe to be replaced with L4 src port matching.
1281 // We need to revisit this if L4 dst port is used for other purpose in the future.
1282 if (!supportIpv6L4Dst() && isIpv6(fwd.selector())) {
Charles Chand9e47c62017-10-05 15:17:15 -07001283 switch (criterion.type()) {
1284 case UDP_DST:
Charles Chand9e47c62017-10-05 15:17:15 -07001285 case UDP_DST_MASKED:
Charles Chand9e47c62017-10-05 15:17:15 -07001286 case TCP_DST:
Charles Chand9e47c62017-10-05 15:17:15 -07001287 case TCP_DST_MASKED:
Charles Chand9e47c62017-10-05 15:17:15 -07001288 break;
1289 default:
1290 sbuilder.add(criterion);
1291 }
1292 } else {
1293 sbuilder.add(criterion);
1294 }
Harshada Chaundkarcb787512019-08-05 15:33:30 +00001295 } else if (criterion instanceof EthTypeCriterion) {
1296 sbuilder.add(criterion);
1297 ethTypeUsed.set(true);
Saurav Das77b5e902016-01-27 17:01:59 -08001298 } else {
1299 sbuilder.add(criterion);
1300 }
1301 });
Harshada Chaundkarcb787512019-08-05 15:33:30 +00001302
1303 TrafficTreatment.Builder ttBuilder = versatileTreatmentBuilder(fwd);
1304 if (ttBuilder == null) {
1305 return Collections.emptySet();
1306 }
1307
1308 FlowRule.Builder ruleBuilder = DefaultFlowRule.builder()
1309 .fromApp(fwd.appId())
1310 .withPriority(fwd.priority())
1311 .forDevice(deviceId)
1312 .withSelector(sbuilder.build())
1313 .withTreatment(ttBuilder.build())
1314 .makePermanent()
1315 .forTable(ACL_TABLE);
1316
1317 flowRules.add(ruleBuilder.build());
1318
1319 if (!ethTypeUsed.get() && requireEthType()) {
1320 log.debug("{} doesn't match on ethType but requireEthType is true, adding complementary ACL flow.",
1321 sbuilder.toString());
1322 sbuilder.matchEthType(Ethernet.TYPE_IPV6);
1323 FlowRule.Builder ethTypeRuleBuilder = DefaultFlowRule.builder()
1324 .fromApp(fwd.appId())
1325 .withPriority(fwd.priority())
1326 .forDevice(deviceId)
1327 .withSelector(sbuilder.build())
1328 .withTreatment(ttBuilder.build())
1329 .makePermanent()
1330 .forTable(ACL_TABLE);
1331 flowRules.add(ethTypeRuleBuilder.build());
1332 }
1333 return flowRules;
Charles Chanab591602019-01-22 17:25:04 -08001334 }
Saurav Das77b5e902016-01-27 17:01:59 -08001335
Charles Chanab591602019-01-22 17:25:04 -08001336 /**
1337 * Helper function to create traffic treatment builder for versatile forwarding objectives.
1338 *
1339 * @param fwd original forwarding objective
1340 * @return treatment builder for the flow rule, or null if there is an error.
1341 */
1342 protected TrafficTreatment.Builder versatileTreatmentBuilder(ForwardingObjective fwd) {
Saurav Das822c4e22015-10-23 10:51:11 -07001343 // XXX driver does not currently do type checking as per Tables 65-67 in
1344 // OFDPA 2.0 spec. The only allowed treatment is a punt to the controller.
Saurav Das49cb5a12016-01-16 22:54:07 -08001345 TrafficTreatment.Builder ttBuilder = DefaultTrafficTreatment.builder();
1346 if (fwd.treatment() != null) {
1347 for (Instruction ins : fwd.treatment().allInstructions()) {
1348 if (ins instanceof OutputInstruction) {
1349 OutputInstruction o = (OutputInstruction) ins;
Ray Milkeyfe0e0852018-01-18 11:14:05 -08001350 if (PortNumber.CONTROLLER.equals(o.port())) {
Saurav Das49cb5a12016-01-16 22:54:07 -08001351 ttBuilder.add(o);
1352 } else {
1353 log.warn("Only allowed treatments in versatile forwarding "
1354 + "objectives are punts to the controller");
1355 }
Charles Chanf76de302018-06-15 18:54:18 -07001356 } else if (ins instanceof NoActionInstruction) {
1357 // No action is allowed and nothing needs to be done
Saurav Das49cb5a12016-01-16 22:54:07 -08001358 } else {
1359 log.warn("Cannot process instruction in versatile fwd {}", ins);
1360 }
Saurav Das822c4e22015-10-23 10:51:11 -07001361 }
Charles Chan2df0e8a2017-01-09 11:45:08 -08001362 if (fwd.treatment().clearedDeferred()) {
1363 ttBuilder.wipeDeferred();
1364 }
Saurav Das822c4e22015-10-23 10:51:11 -07001365 }
Saurav Das822c4e22015-10-23 10:51:11 -07001366 if (fwd.nextId() != null) {
Charles Chanab591602019-01-22 17:25:04 -08001367 // Override case
Saurav Das49cb5a12016-01-16 22:54:07 -08001368 NextGroup next = getGroupForNextObjective(fwd.nextId());
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +05301369 if (next == null) {
1370 fail(fwd, ObjectiveError.BADPARAMS);
Charles Chanab591602019-01-22 17:25:04 -08001371 return null;
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +05301372 }
Saurav Das49cb5a12016-01-16 22:54:07 -08001373 List<Deque<GroupKey>> gkeys = appKryo.deserialize(next.data());
1374 // we only need the top level group's key to point the flow to it
1375 Group group = groupService.getGroup(deviceId, gkeys.get(0).peekFirst());
1376 if (group == null) {
1377 log.warn("Group with key:{} for next-id:{} not found in dev:{}",
Charles Chanab591602019-01-22 17:25:04 -08001378 gkeys.get(0).peekFirst(), fwd.nextId(), deviceId);
Saurav Das49cb5a12016-01-16 22:54:07 -08001379 fail(fwd, ObjectiveError.GROUPMISSING);
Charles Chanab591602019-01-22 17:25:04 -08001380 return null;
Saurav Das49cb5a12016-01-16 22:54:07 -08001381 }
1382 ttBuilder.deferred().group(group.id());
Saurav Das822c4e22015-10-23 10:51:11 -07001383 }
Charles Chanab591602019-01-22 17:25:04 -08001384 return ttBuilder;
Saurav Das822c4e22015-10-23 10:51:11 -07001385 }
1386
1387 /**
1388 * In the OF-DPA 2.0 pipeline, specific forwarding refers to the IP table
Saurav Das8a0732e2015-11-20 15:27:53 -08001389 * (unicast or multicast) or the L2 table (mac + vlan) or the MPLS table.
Saurav Das822c4e22015-10-23 10:51:11 -07001390 *
1391 * @param fwd the forwarding objective of type 'specific'
1392 * @return a collection of flow rules. Typically there will be only one
1393 * for this type of forwarding objective. An empty set may be
1394 * returned if there is an issue in processing the objective.
1395 */
Charles Chan50d900c2018-03-02 13:26:22 -08001396 private Collection<FlowRule> processSpecific(ForwardingObjective fwd) {
Saurav Das25190812016-05-27 13:54:07 -07001397 log.debug("Processing specific fwd objective:{} in dev:{} with next:{}",
Saurav Das4ce45962015-11-24 23:21:05 -08001398 fwd.id(), deviceId, fwd.nextId());
1399 boolean isEthTypeObj = isSupportedEthTypeObjective(fwd);
1400 boolean isEthDstObj = isSupportedEthDstObjective(fwd);
1401
1402 if (isEthTypeObj) {
1403 return processEthTypeSpecific(fwd);
1404 } else if (isEthDstObj) {
1405 return processEthDstSpecific(fwd);
1406 } else {
1407 log.warn("processSpecific: Unsupported forwarding objective "
1408 + "criteria fwd:{} in dev:{}", fwd.nextId(), deviceId);
Saurav Das822c4e22015-10-23 10:51:11 -07001409 fail(fwd, ObjectiveError.UNSUPPORTED);
1410 return Collections.emptySet();
1411 }
Saurav Das4ce45962015-11-24 23:21:05 -08001412 }
1413
Saurav Das4ce45962015-11-24 23:21:05 -08001414 /**
1415 * Handles forwarding rules to the IP and MPLS tables.
1416 *
1417 * @param fwd the forwarding objective
1418 * @return A collection of flow rules, or an empty set
1419 */
1420 protected Collection<FlowRule> processEthTypeSpecific(ForwardingObjective fwd) {
Charles Chancad338a2016-09-16 18:03:11 -07001421 return processEthTypeSpecificInternal(fwd, false, ACL_TABLE);
Charles Chanf9e98652016-09-07 16:54:23 -07001422 }
1423
1424 /**
1425 * Internal implementation of processEthTypeSpecific.
1426 * <p>
1427 * Wildcarded IPv4_DST is not supported in OFDPA i12. Therefore, we break
1428 * the rule into 0.0.0.0/1 and 128.0.0.0/1.
1429 * The allowDefaultRoute must be set to false for OFDPA i12.
1430 * </p>
1431 *
1432 * @param fwd the forwarding objective
1433 * @param allowDefaultRoute allow wildcarded IPv4_DST or not
Ray Milkey0bb1e102016-11-10 14:51:27 -08001434 * @param mplsNextTable next MPLS table
Charles Chanf9e98652016-09-07 16:54:23 -07001435 * @return A collection of flow rules, or an empty set
1436 */
1437 protected Collection<FlowRule> processEthTypeSpecificInternal(ForwardingObjective fwd,
Charles Chancad338a2016-09-16 18:03:11 -07001438 boolean allowDefaultRoute,
1439 int mplsNextTable) {
Saurav Das4ce45962015-11-24 23:21:05 -08001440 TrafficSelector selector = fwd.selector();
1441 EthTypeCriterion ethType =
1442 (EthTypeCriterion) selector.getCriterion(Criterion.Type.ETH_TYPE);
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001443 boolean emptyGroup = false;
Charles Chan188ebf52015-12-23 00:15:11 -08001444 int forTableId;
Saurav Das8a0732e2015-11-20 15:27:53 -08001445 TrafficSelector.Builder filteredSelector = DefaultTrafficSelector.builder();
Charles Chan14967c22015-12-07 11:11:50 -08001446 TrafficTreatment.Builder tb = DefaultTrafficTreatment.builder();
Flavio Castroe10fa242016-01-15 12:43:51 -08001447 TrafficSelector.Builder complementarySelector = DefaultTrafficSelector.builder();
Charles Chan14967c22015-12-07 11:11:50 -08001448
Saurav Das8a0732e2015-11-20 15:27:53 -08001449 if (ethType.ethType().toShort() == Ethernet.TYPE_IPV4) {
Pier Luigi3bfe32c2017-01-30 09:47:36 -08001450 if (buildIpv4Selector(filteredSelector, complementarySelector, fwd, allowDefaultRoute) < 0) {
1451 return Collections.emptyList();
1452 }
1453 // We need to set properly the next table
Flavio Castroe10fa242016-01-15 12:43:51 -08001454 IpPrefix ipv4Dst = ((IPCriterion) selector.getCriterion(Criterion.Type.IPV4_DST)).ip();
Charles Chan5b9df8d2016-03-28 22:21:40 -07001455 if (ipv4Dst.isMulticast()) {
Charles Chan5b9df8d2016-03-28 22:21:40 -07001456 forTableId = MULTICAST_ROUTING_TABLE;
Flavio Castroe10fa242016-01-15 12:43:51 -08001457 } else {
Charles Chan5b9df8d2016-03-28 22:21:40 -07001458 forTableId = UNICAST_ROUTING_TABLE;
Flavio Castroe10fa242016-01-15 12:43:51 -08001459 }
Charles Chan50d900c2018-03-02 13:26:22 -08001460
Charles Chan236653d2018-03-05 11:28:23 -08001461 // TODO decrementing IP ttl is done automatically for routing, this
Charles Chan50d900c2018-03-02 13:26:22 -08001462 // action is ignored or rejected in ofdpa as it is not fully implemented
Charles Chan236653d2018-03-05 11:28:23 -08001463 // if (fwd.treatment() != null) {
1464 // for (Instruction instr : fwd.treatment().allInstructions()) {
1465 // if (instr instanceof L3ModificationInstruction &&
1466 // ((L3ModificationInstruction) instr).subtype() == L3SubType.DEC_TTL) {
1467 // tb.deferred().add(instr);
1468 // }
1469 // }
1470 // }
1471
Pier Ventree0ae7a32016-11-23 09:57:42 -08001472 } else if (ethType.ethType().toShort() == Ethernet.TYPE_IPV6) {
1473 if (buildIpv6Selector(filteredSelector, fwd) < 0) {
1474 return Collections.emptyList();
1475 }
Kalhee Kim3d26bf02017-10-04 14:59:11 +00001476 //We need to set the proper next table
1477 IpPrefix ipv6Dst = ((IPCriterion) selector.getCriterion(Criterion.Type.IPV6_DST)).ip();
1478 if (ipv6Dst.isMulticast()) {
1479 forTableId = MULTICAST_ROUTING_TABLE;
1480 } else {
1481 forTableId = UNICAST_ROUTING_TABLE;
1482 }
1483
Charles Chan236653d2018-03-05 11:28:23 -08001484 // TODO decrementing IP ttl is done automatically for routing, this
Charles Chan50d900c2018-03-02 13:26:22 -08001485 // action is ignored or rejected in ofdpa as it is not fully implemented
Charles Chan236653d2018-03-05 11:28:23 -08001486 // if (fwd.treatment() != null) {
1487 // for (Instruction instr : fwd.treatment().allInstructions()) {
1488 // if (instr instanceof L3ModificationInstruction &&
1489 // ((L3ModificationInstruction) instr).subtype() == L3SubType.DEC_TTL) {
1490 // tb.deferred().add(instr);
1491 // }
1492 // }
1493 // }
Saurav Das8a0732e2015-11-20 15:27:53 -08001494 } else {
1495 filteredSelector
1496 .matchEthType(Ethernet.MPLS_UNICAST)
1497 .matchMplsLabel(((MplsCriterion)
1498 selector.getCriterion(Criterion.Type.MPLS_LABEL)).label());
1499 MplsBosCriterion bos = (MplsBosCriterion) selector
Pier Ventre140a8942016-11-02 07:26:38 -07001500 .getCriterion(MPLS_BOS);
Alex Yashchuk4caa8e82017-12-08 17:40:05 +02001501 if (bos != null && requireMplsBosMatch()) {
Saurav Das8a0732e2015-11-20 15:27:53 -08001502 filteredSelector.matchMplsBos(bos.mplsBos());
1503 }
1504 forTableId = MPLS_TABLE_1;
Saurav Das4ce45962015-11-24 23:21:05 -08001505 log.debug("processing MPLS specific forwarding objective {} -> next:{}"
1506 + " in dev {}", fwd.id(), fwd.nextId(), deviceId);
Saurav Das822c4e22015-10-23 10:51:11 -07001507
Charles Chan14967c22015-12-07 11:11:50 -08001508 if (fwd.treatment() != null) {
1509 for (Instruction instr : fwd.treatment().allInstructions()) {
1510 if (instr instanceof L2ModificationInstruction &&
1511 ((L2ModificationInstruction) instr).subtype() == L2SubType.MPLS_POP) {
Pier Luigi3bfe32c2017-01-30 09:47:36 -08001512 // OF-DPA does not pop in MPLS table in some cases. For the L3 VPN, it requires
Saurav Das9c705342017-01-06 11:36:01 -08001513 // setting the MPLS_TYPE so pop can happen down the pipeline
Alex Yashchuk4caa8e82017-12-08 17:40:05 +02001514 if (requireMplsPop()) {
1515 if (mplsNextTable == MPLS_TYPE_TABLE && isNotMplsBos(selector)) {
1516 tb.immediate().popMpls();
1517 }
1518 } else {
1519 // Skip mpls pop action for mpls_unicast label
1520 if (instr instanceof ModMplsHeaderInstruction &&
1521 !((ModMplsHeaderInstruction) instr).ethernetType()
1522 .equals(EtherType.MPLS_UNICAST.ethType())) {
1523 tb.immediate().add(instr);
1524 }
Pier Luigi3bfe32c2017-01-30 09:47:36 -08001525 }
Charles Chan14967c22015-12-07 11:11:50 -08001526 }
Alex Yashchuk4caa8e82017-12-08 17:40:05 +02001527
1528 if (requireMplsTtlModification()) {
1529 if (instr instanceof L3ModificationInstruction &&
1530 ((L3ModificationInstruction) instr).subtype() == L3SubType.DEC_TTL) {
1531 // FIXME Should modify the app to send the correct DEC_MPLS_TTL instruction
1532 tb.immediate().decMplsTtl();
1533 }
1534 if (instr instanceof L3ModificationInstruction &&
1535 ((L3ModificationInstruction) instr).subtype() == L3SubType.TTL_IN) {
1536 tb.immediate().add(instr);
1537 }
Charles Chan14967c22015-12-07 11:11:50 -08001538 }
Saurav Das8a0732e2015-11-20 15:27:53 -08001539 }
1540 }
1541 }
Saurav Das822c4e22015-10-23 10:51:11 -07001542
1543 if (fwd.nextId() != null) {
Saurav Das423fe2b2015-12-04 10:52:59 -08001544 NextGroup next = getGroupForNextObjective(fwd.nextId());
1545 if (next != null) {
1546 List<Deque<GroupKey>> gkeys = appKryo.deserialize(next.data());
1547 // we only need the top level group's key to point the flow to it
1548 Group group = groupService.getGroup(deviceId, gkeys.get(0).peekFirst());
1549 if (group == null) {
1550 log.warn("Group with key:{} for next-id:{} not found in dev:{}",
1551 gkeys.get(0).peekFirst(), fwd.nextId(), deviceId);
1552 fail(fwd, ObjectiveError.GROUPMISSING);
1553 return Collections.emptySet();
1554 }
Saurav Dasa4020382018-02-14 14:14:54 -08001555 if (isNotMplsBos(selector) && group.type().equals(SELECT)) {
1556 log.warn("SR CONTINUE case cannot be handled as MPLS ECMP "
1557 + "is not implemented in OF-DPA yet. Aborting flow {} -> next:{} "
1558 + "in this device {}", fwd.id(), fwd.nextId(), deviceId);
1559 fail(fwd, ObjectiveError.FLOWINSTALLATIONFAILED);
1560 return Collections.emptySet();
1561 }
Saurav Das423fe2b2015-12-04 10:52:59 -08001562 tb.deferred().group(group.id());
Saurav Dasc568c342018-01-25 09:49:01 -08001563 // retrying flows may be necessary due to bug CORD-554
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001564 if (gkeys.size() == 1 && gkeys.get(0).size() == 1) {
Saurav Dasc568c342018-01-25 09:49:01 -08001565 if (shouldRetry()) {
1566 log.warn("Found empty group 0x{} in dev:{} .. will retry fwd:{}",
1567 Integer.toHexString(group.id().id()), deviceId,
1568 fwd.id());
1569 emptyGroup = true;
1570 }
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001571 }
Saurav Das25190812016-05-27 13:54:07 -07001572 } else {
1573 log.warn("Cannot find group for nextId:{} in dev:{}. Aborting fwd:{}",
1574 fwd.nextId(), deviceId, fwd.id());
1575 fail(fwd, ObjectiveError.FLOWINSTALLATIONFAILED);
1576 return Collections.emptySet();
Saurav Das822c4e22015-10-23 10:51:11 -07001577 }
Saurav Das822c4e22015-10-23 10:51:11 -07001578 }
Charles Chancad338a2016-09-16 18:03:11 -07001579
1580 if (forTableId == MPLS_TABLE_1) {
Pier Ventre140a8942016-11-02 07:26:38 -07001581 if (mplsNextTable == MPLS_L3_TYPE_TABLE) {
Charles Chancad338a2016-09-16 18:03:11 -07001582 Ofdpa3SetMplsType setMplsType = new Ofdpa3SetMplsType(Ofdpa3MplsType.L3_PHP);
Saurav Das9c705342017-01-06 11:36:01 -08001583 // set mpls type as apply_action
1584 tb.immediate().extension(setMplsType, deviceId);
Charles Chancad338a2016-09-16 18:03:11 -07001585 }
1586 tb.transition(mplsNextTable);
1587 } else {
1588 tb.transition(ACL_TABLE);
1589 }
1590
Andrea Campanellad980c6d2018-04-30 11:48:55 +02001591 if (fwd.treatment() != null && fwd.treatment().clearedDeferred()) {
1592 if (supportsUnicastBlackHole()) {
1593 tb.wipeDeferred();
1594 } else {
1595 log.warn("Clear Deferred is not supported Unicast Routing Table on device {}", deviceId);
1596 return Collections.emptySet();
1597 }
1598 }
1599
Saurav Das822c4e22015-10-23 10:51:11 -07001600 FlowRule.Builder ruleBuilder = DefaultFlowRule.builder()
1601 .fromApp(fwd.appId())
1602 .withPriority(fwd.priority())
1603 .forDevice(deviceId)
Saurav Das8a0732e2015-11-20 15:27:53 -08001604 .withSelector(filteredSelector.build())
1605 .withTreatment(tb.build())
1606 .forTable(forTableId);
Saurav Das822c4e22015-10-23 10:51:11 -07001607
1608 if (fwd.permanent()) {
1609 ruleBuilder.makePermanent();
1610 } else {
1611 ruleBuilder.makeTemporary(fwd.timeout());
1612 }
Flavio Castroe10fa242016-01-15 12:43:51 -08001613 Collection<FlowRule> flowRuleCollection = new ArrayList<>();
1614 flowRuleCollection.add(ruleBuilder.build());
Pier Luigi3bfe32c2017-01-30 09:47:36 -08001615 if (!allowDefaultRoute) {
Pier Ventree0ae7a32016-11-23 09:57:42 -08001616 flowRuleCollection.add(
1617 defaultRoute(fwd, complementarySelector, forTableId, tb)
1618 );
Flavio Castroe10fa242016-01-15 12:43:51 -08001619 log.debug("Default rule 0.0.0.0/0 is being installed two rules");
1620 }
Saurav Dasc568c342018-01-25 09:49:01 -08001621
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001622 if (emptyGroup) {
pier8b3aef42019-03-11 15:14:02 -07001623 retryExecutorService.schedule(new RetryFlows(fwd, flowRuleCollection),
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001624 RETRY_MS, TimeUnit.MILLISECONDS);
1625 }
Flavio Castroe10fa242016-01-15 12:43:51 -08001626 return flowRuleCollection;
Saurav Das822c4e22015-10-23 10:51:11 -07001627 }
1628
Charles Chan50d900c2018-03-02 13:26:22 -08001629 private int buildIpv4Selector(TrafficSelector.Builder builderToUpdate,
Pier Luigi3bfe32c2017-01-30 09:47:36 -08001630 TrafficSelector.Builder extBuilder,
1631 ForwardingObjective fwd,
1632 boolean allowDefaultRoute) {
1633 TrafficSelector selector = fwd.selector();
1634
1635 IpPrefix ipv4Dst = ((IPCriterion) selector.getCriterion(Criterion.Type.IPV4_DST)).ip();
1636 if (ipv4Dst.isMulticast()) {
1637 if (ipv4Dst.prefixLength() != 32) {
1638 log.warn("Multicast specific forwarding objective can only be /32");
1639 fail(fwd, ObjectiveError.BADPARAMS);
1640 return -1;
1641 }
1642 VlanId assignedVlan = readVlanFromSelector(fwd.meta());
1643 if (assignedVlan == null) {
1644 log.warn("VLAN ID required by multicast specific fwd obj is missing. Abort.");
1645 fail(fwd, ObjectiveError.BADPARAMS);
1646 return -1;
1647 }
Charles Chand1172632017-03-15 17:33:09 -07001648 if (requireVlanExtensions()) {
1649 OfdpaMatchVlanVid ofdpaMatchVlanVid = new OfdpaMatchVlanVid(assignedVlan);
1650 builderToUpdate.extension(ofdpaMatchVlanVid, deviceId);
1651 } else {
1652 builderToUpdate.matchVlanId(assignedVlan);
1653 }
Pier Luigi3bfe32c2017-01-30 09:47:36 -08001654 builderToUpdate.matchEthType(Ethernet.TYPE_IPV4).matchIPDst(ipv4Dst);
1655 log.debug("processing IPv4 multicast specific forwarding objective {} -> next:{}"
1656 + " in dev:{}", fwd.id(), fwd.nextId(), deviceId);
1657 } else {
1658 if (ipv4Dst.prefixLength() == 0) {
1659 if (allowDefaultRoute) {
1660 // The entire IPV4_DST field is wildcarded intentionally
1661 builderToUpdate.matchEthType(Ethernet.TYPE_IPV4);
1662 } else {
1663 // NOTE: The switch does not support matching 0.0.0.0/0
1664 // Split it into 0.0.0.0/1 and 128.0.0.0/1
1665 builderToUpdate.matchEthType(Ethernet.TYPE_IPV4)
1666 .matchIPDst(IpPrefix.valueOf("0.0.0.0/1"));
1667 extBuilder.matchEthType(Ethernet.TYPE_IPV4)
1668 .matchIPDst(IpPrefix.valueOf("128.0.0.0/1"));
1669 }
1670 } else {
1671 builderToUpdate.matchEthType(Ethernet.TYPE_IPV4).matchIPDst(ipv4Dst);
1672 }
1673 log.debug("processing IPv4 unicast specific forwarding objective {} -> next:{}"
1674 + " in dev:{}", fwd.id(), fwd.nextId(), deviceId);
1675 }
1676 return 0;
1677 }
1678
1679 /**
1680 * Helper method to build Ipv6 selector using the selector provided by
1681 * a forwarding objective.
1682 *
1683 * @param builderToUpdate the builder to update
1684 * @param fwd the selector to read
1685 * @return 0 if the update ends correctly. -1 if the matches
1686 * are not yet supported
1687 */
Charles Chan50d900c2018-03-02 13:26:22 -08001688 int buildIpv6Selector(TrafficSelector.Builder builderToUpdate,
Pier Luigi3bfe32c2017-01-30 09:47:36 -08001689 ForwardingObjective fwd) {
1690
1691 TrafficSelector selector = fwd.selector();
1692
1693 IpPrefix ipv6Dst = ((IPCriterion) selector.getCriterion(Criterion.Type.IPV6_DST)).ip();
1694 if (ipv6Dst.isMulticast()) {
Julia Ferguson65428c32017-08-10 18:15:24 +00001695 if (ipv6Dst.prefixLength() != IpAddress.INET6_BIT_LENGTH) {
1696 log.warn("Multicast specific forwarding objective can only be /128");
1697 fail(fwd, ObjectiveError.BADPARAMS);
1698 return -1;
1699 }
1700 VlanId assignedVlan = readVlanFromSelector(fwd.meta());
1701 if (assignedVlan == null) {
1702 log.warn("VLAN ID required by multicast specific fwd obj is missing. Abort.");
1703 fail(fwd, ObjectiveError.BADPARAMS);
1704 return -1;
1705 }
1706 if (requireVlanExtensions()) {
1707 OfdpaMatchVlanVid ofdpaMatchVlanVid = new OfdpaMatchVlanVid(assignedVlan);
1708 builderToUpdate.extension(ofdpaMatchVlanVid, deviceId);
1709 } else {
1710 builderToUpdate.matchVlanId(assignedVlan);
1711 }
1712 builderToUpdate.matchEthType(Ethernet.TYPE_IPV6).matchIPv6Dst(ipv6Dst);
1713 log.debug("processing IPv6 multicast specific forwarding objective {} -> next:{}"
1714 + " in dev:{}", fwd.id(), fwd.nextId(), deviceId);
1715 } else {
1716 if (ipv6Dst.prefixLength() != 0) {
1717 builderToUpdate.matchIPv6Dst(ipv6Dst);
1718 }
Pier Luigi3bfe32c2017-01-30 09:47:36 -08001719 builderToUpdate.matchEthType(Ethernet.TYPE_IPV6);
1720 log.debug("processing IPv6 unicast specific forwarding objective {} -> next:{}"
1721 + " in dev:{}", fwd.id(), fwd.nextId(), deviceId);
Julia Ferguson65428c32017-08-10 18:15:24 +00001722 }
Pier Luigi3bfe32c2017-01-30 09:47:36 -08001723 return 0;
1724 }
1725
Charles Chan50d900c2018-03-02 13:26:22 -08001726 FlowRule defaultRoute(ForwardingObjective fwd,
Pier Ventree0ae7a32016-11-23 09:57:42 -08001727 TrafficSelector.Builder complementarySelector,
1728 int forTableId,
1729 TrafficTreatment.Builder tb) {
1730 FlowRule.Builder rule = DefaultFlowRule.builder()
1731 .fromApp(fwd.appId())
1732 .withPriority(fwd.priority())
1733 .forDevice(deviceId)
1734 .withSelector(complementarySelector.build())
1735 .withTreatment(tb.build())
1736 .forTable(forTableId);
1737 if (fwd.permanent()) {
1738 rule.makePermanent();
1739 } else {
1740 rule.makeTemporary(fwd.timeout());
1741 }
1742 return rule.build();
1743 }
1744
Saurav Das4ce45962015-11-24 23:21:05 -08001745 /**
1746 * Handles forwarding rules to the L2 bridging table. Flow actions are not
1747 * allowed in the bridging table - instead we use L2 Interface group or
1748 * L2 flood group
1749 *
1750 * @param fwd the forwarding objective
1751 * @return A collection of flow rules, or an empty set
1752 */
1753 protected Collection<FlowRule> processEthDstSpecific(ForwardingObjective fwd) {
1754 List<FlowRule> rules = new ArrayList<>();
1755
1756 // Build filtered selector
1757 TrafficSelector selector = fwd.selector();
1758 EthCriterion ethCriterion = (EthCriterion) selector
1759 .getCriterion(Criterion.Type.ETH_DST);
1760 VlanIdCriterion vlanIdCriterion = (VlanIdCriterion) selector
1761 .getCriterion(Criterion.Type.VLAN_VID);
1762
1763 if (vlanIdCriterion == null) {
1764 log.warn("Forwarding objective for bridging requires vlan. Not "
1765 + "installing fwd:{} in dev:{}", fwd.id(), deviceId);
1766 fail(fwd, ObjectiveError.BADPARAMS);
1767 return Collections.emptySet();
1768 }
1769
1770 TrafficSelector.Builder filteredSelectorBuilder =
1771 DefaultTrafficSelector.builder();
Yi Tseng3a77b4f2017-02-06 15:02:17 -08001772
1773 if (!ethCriterion.mac().equals(NONE) &&
1774 !ethCriterion.mac().equals(BROADCAST)) {
Saurav Das4ce45962015-11-24 23:21:05 -08001775 filteredSelectorBuilder.matchEthDst(ethCriterion.mac());
1776 log.debug("processing L2 forwarding objective:{} -> next:{} in dev:{}",
1777 fwd.id(), fwd.nextId(), deviceId);
1778 } else {
Yi Tseng3a77b4f2017-02-06 15:02:17 -08001779 // Use wildcard DST_MAC if the MacAddress is None or Broadcast
Saurav Das4ce45962015-11-24 23:21:05 -08001780 log.debug("processing L2 Broadcast forwarding objective:{} -> next:{} "
1781 + "in dev:{} for vlan:{}",
1782 fwd.id(), fwd.nextId(), deviceId, vlanIdCriterion.vlanId());
1783 }
Charles Chand1172632017-03-15 17:33:09 -07001784 if (requireVlanExtensions()) {
1785 OfdpaMatchVlanVid ofdpaMatchVlanVid = new OfdpaMatchVlanVid(vlanIdCriterion.vlanId());
1786 filteredSelectorBuilder.extension(ofdpaMatchVlanVid, deviceId);
1787 } else {
1788 filteredSelectorBuilder.matchVlanId(vlanIdCriterion.vlanId());
1789 }
Saurav Das4ce45962015-11-24 23:21:05 -08001790 TrafficSelector filteredSelector = filteredSelectorBuilder.build();
1791
1792 if (fwd.treatment() != null) {
1793 log.warn("Ignoring traffic treatment in fwd rule {} meant for L2 table"
1794 + "for dev:{}. Expecting only nextId", fwd.id(), deviceId);
1795 }
1796
1797 TrafficTreatment.Builder treatmentBuilder = DefaultTrafficTreatment.builder();
1798 if (fwd.nextId() != null) {
Saurav Das423fe2b2015-12-04 10:52:59 -08001799 NextGroup next = getGroupForNextObjective(fwd.nextId());
Saurav Das4ce45962015-11-24 23:21:05 -08001800 if (next != null) {
1801 List<Deque<GroupKey>> gkeys = appKryo.deserialize(next.data());
1802 // we only need the top level group's key to point the flow to it
1803 Group group = groupService.getGroup(deviceId, gkeys.get(0).peekFirst());
1804 if (group != null) {
1805 treatmentBuilder.deferred().group(group.id());
1806 } else {
1807 log.warn("Group with key:{} for next-id:{} not found in dev:{}",
1808 gkeys.get(0).peekFirst(), fwd.nextId(), deviceId);
1809 fail(fwd, ObjectiveError.GROUPMISSING);
1810 return Collections.emptySet();
1811 }
1812 }
1813 }
1814 treatmentBuilder.immediate().transition(ACL_TABLE);
1815 TrafficTreatment filteredTreatment = treatmentBuilder.build();
1816
1817 // Build bridging table entries
1818 FlowRule.Builder flowRuleBuilder = DefaultFlowRule.builder();
1819 flowRuleBuilder.fromApp(fwd.appId())
1820 .withPriority(fwd.priority())
1821 .forDevice(deviceId)
1822 .withSelector(filteredSelector)
1823 .withTreatment(filteredTreatment)
1824 .forTable(BRIDGING_TABLE);
1825 if (fwd.permanent()) {
1826 flowRuleBuilder.makePermanent();
1827 } else {
1828 flowRuleBuilder.makeTemporary(fwd.timeout());
1829 }
1830 rules.add(flowRuleBuilder.build());
1831 return rules;
1832 }
1833
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001834 //////////////////////////////////////
1835 // Helper Methods and Classes
1836 //////////////////////////////////////
1837
1838 private boolean isSupportedEthTypeObjective(ForwardingObjective fwd) {
1839 TrafficSelector selector = fwd.selector();
1840 EthTypeCriterion ethType = (EthTypeCriterion) selector
1841 .getCriterion(Criterion.Type.ETH_TYPE);
1842 return !((ethType == null) ||
1843 ((ethType.ethType().toShort() != Ethernet.TYPE_IPV4) &&
Pier Ventree0ae7a32016-11-23 09:57:42 -08001844 (ethType.ethType().toShort() != Ethernet.MPLS_UNICAST)) &&
1845 (ethType.ethType().toShort() != Ethernet.TYPE_IPV6));
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001846 }
1847
1848 private boolean isSupportedEthDstObjective(ForwardingObjective fwd) {
1849 TrafficSelector selector = fwd.selector();
1850 EthCriterion ethDst = (EthCriterion) selector
1851 .getCriterion(Criterion.Type.ETH_DST);
1852 VlanIdCriterion vlanId = (VlanIdCriterion) selector
1853 .getCriterion(Criterion.Type.VLAN_VID);
1854 return !(ethDst == null && vlanId == null);
1855 }
1856
Charles Chan50d900c2018-03-02 13:26:22 -08001857 NextGroup getGroupForNextObjective(Integer nextId) {
Saurav Das423fe2b2015-12-04 10:52:59 -08001858 NextGroup next = flowObjectiveStore.getNextGroup(nextId);
1859 if (next != null) {
1860 List<Deque<GroupKey>> gkeys = appKryo.deserialize(next.data());
1861 if (gkeys != null && !gkeys.isEmpty()) {
1862 return next;
1863 } else {
1864 log.warn("Empty next group found in FlowObjective store for "
1865 + "next-id:{} in dev:{}", nextId, deviceId);
1866 }
1867 } else {
1868 log.warn("next-id {} not found in Flow objective store for dev:{}",
1869 nextId, deviceId);
1870 }
1871 return null;
1872 }
1873
Saurav Das24431192016-03-07 19:13:00 -08001874 @Override
1875 public List<String> getNextMappings(NextGroup nextGroup) {
1876 List<String> mappings = new ArrayList<>();
1877 List<Deque<GroupKey>> gkeys = appKryo.deserialize(nextGroup.data());
1878 for (Deque<GroupKey> gkd : gkeys) {
1879 Group lastGroup = null;
Yuta HIGUCHI2dce08a2017-04-20 21:57:48 -07001880 StringBuilder gchain = new StringBuilder();
Saurav Das24431192016-03-07 19:13:00 -08001881 for (GroupKey gk : gkd) {
1882 Group g = groupService.getGroup(deviceId, gk);
Saurav Das8be4e3a2016-03-11 17:19:07 -08001883 if (g == null) {
Saurav Das25190812016-05-27 13:54:07 -07001884 gchain.append(" NoGrp").append(" -->");
Saurav Das8be4e3a2016-03-11 17:19:07 -08001885 continue;
1886 }
1887 gchain.append(" 0x").append(Integer.toHexString(g.id().id()))
1888 .append(" -->");
Saurav Das24431192016-03-07 19:13:00 -08001889 lastGroup = g;
1890 }
1891 // add port information for last group in group-chain
Yuta HIGUCHI2dce08a2017-04-20 21:57:48 -07001892 List<Instruction> lastGroupIns = new ArrayList<>();
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001893 if (lastGroup != null && !lastGroup.buckets().buckets().isEmpty()) {
Saurav Das25190812016-05-27 13:54:07 -07001894 lastGroupIns = lastGroup.buckets().buckets().get(0)
1895 .treatment().allInstructions();
1896 }
1897 for (Instruction i: lastGroupIns) {
Saurav Das24431192016-03-07 19:13:00 -08001898 if (i instanceof OutputInstruction) {
Saurav Das8be4e3a2016-03-11 17:19:07 -08001899 gchain.append(" port:").append(((OutputInstruction) i).port());
Saurav Das24431192016-03-07 19:13:00 -08001900 }
1901 }
Saurav Das8be4e3a2016-03-11 17:19:07 -08001902 mappings.add(gchain.toString());
Saurav Das24431192016-03-07 19:13:00 -08001903 }
1904 return mappings;
1905 }
Charles Chan5b9df8d2016-03-28 22:21:40 -07001906
Saurav Dasa4020382018-02-14 14:14:54 -08001907 /**
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001908 * Utility class that retries sending flows a fixed number of times, even if
1909 * some of the attempts are successful. Used only for forwarding objectives.
1910 */
Alex Yashchuk4caa8e82017-12-08 17:40:05 +02001911 public final class RetryFlows implements Runnable {
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001912 int attempts = MAX_RETRY_ATTEMPTS;
1913 private Collection<FlowRule> retryFlows;
1914 private ForwardingObjective fwd;
1915
Charles Chan50d900c2018-03-02 13:26:22 -08001916 RetryFlows(ForwardingObjective fwd, Collection<FlowRule> retryFlows) {
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001917 this.fwd = fwd;
1918 this.retryFlows = retryFlows;
1919 }
1920
1921 @Override
1922 public void run() {
1923 log.info("RETRY FLOWS ATTEMPT# {} for fwd:{} rules:{}",
1924 MAX_RETRY_ATTEMPTS - attempts, fwd.id(), retryFlows.size());
pier8b3aef42019-03-11 15:14:02 -07001925 sendForwards(Collections.singletonList(Pair.of(fwd, retryFlows)));
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001926 if (--attempts > 0) {
pier8b3aef42019-03-11 15:14:02 -07001927 retryExecutorService.schedule(this, RETRY_MS, TimeUnit.MILLISECONDS);
1928 }
1929 }
1930 }
1931
1932 // Flow rules accumulator for reducing the number of transactions required to the devices.
1933 private final class ForwardingObjectiveAccumulator
1934 extends AbstractAccumulator<Pair<ForwardingObjective, Collection<FlowRule>>> {
1935
1936 ForwardingObjectiveAccumulator(int maxFwd, int maxBatchMS, int maxIdleMS) {
1937 super(TIMER, maxFwd, maxBatchMS, maxIdleMS);
1938 }
1939
1940 @Override
1941 public void processItems(List<Pair<ForwardingObjective, Collection<FlowRule>>> pairs) {
1942 // Triggers creation of a batch using the list of flowrules generated from fwdobjs.
1943 accumulatorExecutorService.execute(new FlowRulesBuilderTask(pairs));
1944 }
1945 }
1946
1947 // Task for building batch of flow rules in a separate thread.
1948 private final class FlowRulesBuilderTask implements Runnable {
1949 private final List<Pair<ForwardingObjective, Collection<FlowRule>>> pairs;
1950
1951 FlowRulesBuilderTask(List<Pair<ForwardingObjective, Collection<FlowRule>>> pairs) {
1952 this.pairs = pairs;
1953 }
1954
1955 @Override
1956 public void run() {
1957 try {
1958 sendForwards(pairs);
1959 } catch (Exception e) {
1960 log.warn("Unable to send forwards", e);
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001961 }
1962 }
1963 }
1964
Saurav Das822c4e22015-10-23 10:51:11 -07001965}