blob: a891e403efb31dda5d359166e0b5fb7a77af7f82 [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) {
pierf37ce522020-03-20 11:00:38 +0100158 synchronized (this) {
159 if (isReady()) {
160 return;
161 }
162
pier1b7dd122020-02-28 09:24:11 +0100163 this.deviceId = deviceId;
Saurav Das822c4e22015-10-23 10:51:11 -0700164
pier1b7dd122020-02-28 09:24:11 +0100165 serviceDirectory = context.directory();
166 coreService = serviceDirectory.get(CoreService.class);
167 flowRuleService = serviceDirectory.get(FlowRuleService.class);
168 groupService = serviceDirectory.get(GroupService.class);
169 flowObjectiveStore = context.store();
170 deviceService = serviceDirectory.get(DeviceService.class);
171 // Init the accumulator, if enabled
172 if (isAccumulatorEnabled(this)) {
173 accumulator = new ForwardingObjectiveAccumulator(context.accumulatorMaxObjectives(),
174 context.accumulatorMaxBatchMillis(),
175 context.accumulatorMaxIdleMillis());
176 }
177
178 initDriverId();
179 initGroupHander(context);
180
181 initializePipeline();
pierf37ce522020-03-20 11:00:38 +0100182 ready.set(true);
pier8b3aef42019-03-11 15:14:02 -0700183 }
pier1b7dd122020-02-28 09:24:11 +0100184 }
Saurav Das822c4e22015-10-23 10:51:11 -0700185
pier1b7dd122020-02-28 09:24:11 +0100186 @Override
187 public boolean isReady() {
188 return ready.get();
Saurav Das822c4e22015-10-23 10:51:11 -0700189 }
190
pierf37ce522020-03-20 11:00:38 +0100191 @Override
192 public void cleanUp() {
193 synchronized (this) {
194 if (!isReady()) {
195 return;
196 }
197 ready.set(false);
198 }
199 log.info("Cleaning up...");
200 }
201
pier7afc7522019-05-10 13:19:15 +0200202 void setupAccumulatorForTests(int maxFwd, int maxBatchMS, int maxIdleMS) {
203 if (accumulator == null) {
204 accumulator = new ForwardingObjectiveAccumulator(maxFwd,
205 maxBatchMS,
206 maxIdleMS);
207 }
208 }
209
Charles Chan40132b32017-01-22 00:19:37 -0800210 protected void initDriverId() {
211 driverId = coreService.registerApplication(
212 "org.onosproject.driver.Ofdpa2Pipeline");
213 }
214
215 protected void initGroupHander(PipelinerContext context) {
pierf37ce522020-03-20 11:00:38 +0100216 // Terminate internal references
217 // We are terminating the references here
218 // because when the device is offline the apps
219 // are still sending flowobjectives
220 if (groupHandler != null) {
221 groupHandler.terminate();
222 }
Charles Chan40132b32017-01-22 00:19:37 -0800223 groupHandler = new Ofdpa2GroupHandler();
224 groupHandler.init(deviceId, context);
225 }
226
Saurav Das822c4e22015-10-23 10:51:11 -0700227 protected void initializePipeline() {
Charles Chan188ebf52015-12-23 00:15:11 -0800228 // OF-DPA does not require initializing the pipeline as it puts default
229 // rules automatically in the hardware. However emulation of OFDPA in
230 // software switches does require table-miss-entries.
Saurav Das822c4e22015-10-23 10:51:11 -0700231 }
232
Charles Chand1172632017-03-15 17:33:09 -0700233 /**
Alex Yashchuk4caa8e82017-12-08 17:40:05 +0200234 * Determines whether this pipeline requires MPLS POP instruction.
235 *
236 * @return true to use MPLS POP instruction
237 */
238 public boolean requireMplsPop() {
239 return true;
240 }
241
242 /**
Harshada Chaundkarcb787512019-08-05 15:33:30 +0000243 * Determines whether this pipeline requires one additional flow matching on ethType 0x86dd in ACL table.
244 *
245 * @return true to create one additional flow matching on ethType 0x86dd in ACL table
246 */
247 protected boolean requireEthType() {
248 return true;
249 }
250
251 /**
Alex Yashchuk4caa8e82017-12-08 17:40:05 +0200252 * Determines whether this pipeline requires MPLS BOS match.
253 *
254 * @return true to use MPLS BOS match
255 */
256 public boolean requireMplsBosMatch() {
257 return true;
258 }
259
260 /**
261 * Determines whether this pipeline requires MPLS TTL decrement and copy.
262 *
263 * @return true to use MPLS TTL decrement and copy
264 */
265 public boolean requireMplsTtlModification() {
266 return true;
267 }
268
269 /**
Charles Chand1172632017-03-15 17:33:09 -0700270 * Determines whether this pipeline requires OFDPA match and set VLAN extensions.
271 *
272 * @return true to use the extensions
273 */
274 protected boolean requireVlanExtensions() {
275 return true;
276 }
277
Saurav Das86d13e82017-04-28 17:03:48 -0700278 /**
279 * Determines whether in-port should be matched on in TMAC table rules.
280 *
281 * @return true if match on in-port should be programmed
282 */
283 protected boolean matchInPortTmacTable() {
284 return true;
285 }
286
Charles Chand9e47c62017-10-05 15:17:15 -0700287 /**
288 * Determines whether matching L4 destination port on IPv6 packets is supported in ACL table.
289 *
290 * @return true if matching L4 destination port on IPv6 packets is supported in ACL table.
291 */
292 protected boolean supportIpv6L4Dst() {
293 return true;
294 }
295
Saurav Dasc568c342018-01-25 09:49:01 -0800296 /**
297 * Determines whether this driver should continue to retry flows that point
298 * to empty groups. See CORD-554.
299 *
300 * @return true if the driver should retry flows
301 */
302 protected boolean shouldRetry() {
303 return true;
304 }
305
Charles Chan45b69ab2018-03-02 15:41:41 -0800306 /**
307 * Determines whether this driver requires unicast flow to be installed before multicast flow
308 * in TMAC table.
309 *
310 * @return true if required
311 */
312 protected boolean requireUnicastBeforeMulticast() {
313 return false;
314 }
315
Andrea Campanellad980c6d2018-04-30 11:48:55 +0200316 /**
317 * Determines whether this driver supports installing a clearDeferred action on table 30.
318 *
319 * @return true if required
320 */
321 protected boolean supportsUnicastBlackHole() {
322 return true;
323 }
324
Charles Chanab591602019-01-22 17:25:04 -0800325 protected boolean requirePuntTable() {
326 return false;
327 }
328
Saurav Das822c4e22015-10-23 10:51:11 -0700329 //////////////////////////////////////
330 // Flow Objectives
331 //////////////////////////////////////
332
333 @Override
334 public void filter(FilteringObjective filteringObjective) {
335 if (filteringObjective.type() == FilteringObjective.Type.PERMIT) {
336 processFilter(filteringObjective,
337 filteringObjective.op() == Objective.Operation.ADD,
338 filteringObjective.appId());
339 } else {
340 // Note that packets that don't match the PERMIT filter are
341 // automatically denied. The DENY filter is used to deny packets
342 // that are otherwise permitted by the PERMIT filter.
343 // Use ACL table flow rules here for DENY filtering objectives
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530344 log.warn("filter objective other than PERMIT currently not supported");
Saurav Das822c4e22015-10-23 10:51:11 -0700345 fail(filteringObjective, ObjectiveError.UNSUPPORTED);
346 }
347 }
348
349 @Override
350 public void forward(ForwardingObjective fwd) {
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700351 Collection<FlowRule> rules = processForward(fwd);
Saurav Das25190812016-05-27 13:54:07 -0700352 if (rules == null || rules.isEmpty()) {
353 // Assumes fail message has already been generated to the objective
354 // context. Returning here prevents spurious pass message to be
355 // generated by FlowRule service for empty flowOps.
356 return;
357 }
pier7afc7522019-05-10 13:19:15 +0200358 // Let's accumulate flow rules if accumulator is active and fwd objective is not versatile.
359 // Otherwise send directly, without adding futher delay
360 if (accumulator != null && Objects.equals(fwd.flag(), SPECIFIC)) {
pier8b3aef42019-03-11 15:14:02 -0700361 accumulator.add(Pair.of(fwd, rules));
362 } else {
363 sendForwards(Collections.singletonList(Pair.of(fwd, rules)));
364 }
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700365 }
366
pier8b3aef42019-03-11 15:14:02 -0700367 // Builds the batch using the accumulated flow rules
368 private void sendForwards(List<Pair<ForwardingObjective, Collection<FlowRule>>> pairs) {
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700369 FlowRuleOperations.Builder flowOpsBuilder = FlowRuleOperations.builder();
pier8b3aef42019-03-11 15:14:02 -0700370 log.debug("Sending {} fwd-objs", pairs.size());
371 List<Objective> fwdObjs = Lists.newArrayList();
372 // Iterates over all accumulated flow rules and then build an unique batch
373 pairs.forEach(pair -> {
374 ForwardingObjective fwd = pair.getLeft();
375 Collection<FlowRule> rules = pair.getRight();
376 switch (fwd.op()) {
377 case ADD:
378 rules.stream()
379 .filter(Objects::nonNull)
380 .forEach(flowOpsBuilder::add);
381 log.debug("Applying a add fwd-obj {} to sw:{}", fwd.id(), deviceId);
382 fwdObjs.add(fwd);
383 break;
384 case REMOVE:
385 rules.stream()
386 .filter(Objects::nonNull)
387 .forEach(flowOpsBuilder::remove);
388 log.debug("Deleting a flow rule to sw:{}", deviceId);
389 fwdObjs.add(fwd);
390 break;
391 default:
392 fail(fwd, ObjectiveError.UNKNOWN);
393 log.warn("Unknown forwarding type {}", fwd.op());
394 }
395 });
396 // Finally applies the operations
Saurav Das822c4e22015-10-23 10:51:11 -0700397 flowRuleService.apply(flowOpsBuilder.build(new FlowRuleOperationsContext() {
pier8b3aef42019-03-11 15:14:02 -0700398
Saurav Das822c4e22015-10-23 10:51:11 -0700399 @Override
400 public void onSuccess(FlowRuleOperations ops) {
pier8b3aef42019-03-11 15:14:02 -0700401 log.trace("Flow rule operations onSuccess {}", ops);
pier9469f3e2019-04-17 17:05:08 +0200402 fwdObjs.forEach(OfdpaPipelineUtility::pass);
Saurav Das822c4e22015-10-23 10:51:11 -0700403 }
404
405 @Override
406 public void onError(FlowRuleOperations ops) {
pier8b3aef42019-03-11 15:14:02 -0700407 ObjectiveError error = ObjectiveError.FLOWINSTALLATIONFAILED;
408 log.warn("Flow rule operations onError {}. Reason = {}", ops, error);
409 fwdObjs.forEach(fwdObj -> fail(fwdObj, error));
Saurav Das822c4e22015-10-23 10:51:11 -0700410 }
411 }));
Saurav Das822c4e22015-10-23 10:51:11 -0700412 }
413
414 @Override
415 public void next(NextObjective nextObjective) {
Saurav Das8a0732e2015-11-20 15:27:53 -0800416 NextGroup nextGroup = flowObjectiveStore.getNextGroup(nextObjective.id());
417 switch (nextObjective.op()) {
418 case ADD:
Saurav Das4f980082015-11-05 13:39:15 -0800419 if (nextGroup != null) {
Saurav Das8a0732e2015-11-20 15:27:53 -0800420 log.warn("Cannot add next {} that already exists in device {}",
421 nextObjective.id(), deviceId);
422 return;
423 }
Saurav Das1547b3f2017-05-05 17:01:08 -0700424 log.debug("Processing NextObjective id {} in dev {} - add group",
Saurav Das8a0732e2015-11-20 15:27:53 -0800425 nextObjective.id(), deviceId);
Charles Chan425854b2016-04-11 15:32:12 -0700426 groupHandler.addGroup(nextObjective);
Saurav Das8a0732e2015-11-20 15:27:53 -0800427 break;
428 case ADD_TO_EXISTING:
429 if (nextGroup != null) {
Saurav Das1547b3f2017-05-05 17:01:08 -0700430 log.debug("Processing NextObjective id {} in dev {} - add bucket",
Saurav Das8a0732e2015-11-20 15:27:53 -0800431 nextObjective.id(), deviceId);
Charles Chan425854b2016-04-11 15:32:12 -0700432 groupHandler.addBucketToGroup(nextObjective, nextGroup);
Saurav Das4f980082015-11-05 13:39:15 -0800433 } else {
Saurav Das8a0732e2015-11-20 15:27:53 -0800434 // it is possible that group-chain has not been fully created yet
Saurav Das423fe2b2015-12-04 10:52:59 -0800435 log.debug("Waiting to add bucket to group for next-id:{} in dev:{}",
436 nextObjective.id(), deviceId);
Yi Tseng47f82dc2017-03-05 22:48:39 -0800437
438 // by design multiple pending bucket is allowed for the group
439 groupHandler.pendingBuckets.compute(nextObjective.id(), (nextId, pendBkts) -> {
440 if (pendBkts == null) {
441 pendBkts = Sets.newHashSet();
442 }
443 pendBkts.add(nextObjective);
444 return pendBkts;
445 });
Saurav Das4f980082015-11-05 13:39:15 -0800446 }
Saurav Das8a0732e2015-11-20 15:27:53 -0800447 break;
448 case REMOVE:
449 if (nextGroup == null) {
450 log.warn("Cannot remove next {} that does not exist in device {}",
451 nextObjective.id(), deviceId);
452 return;
453 }
Saurav Das1547b3f2017-05-05 17:01:08 -0700454 log.debug("Processing NextObjective id {} in dev {} - remove group",
Saurav Das8a0732e2015-11-20 15:27:53 -0800455 nextObjective.id(), deviceId);
Charles Chan425854b2016-04-11 15:32:12 -0700456 groupHandler.removeGroup(nextObjective, nextGroup);
Saurav Das8a0732e2015-11-20 15:27:53 -0800457 break;
458 case REMOVE_FROM_EXISTING:
459 if (nextGroup == null) {
460 log.warn("Cannot remove from next {} that does not exist in device {}",
461 nextObjective.id(), deviceId);
462 return;
463 }
Saurav Das1547b3f2017-05-05 17:01:08 -0700464 log.debug("Processing NextObjective id {} in dev {} - remove bucket",
Saurav Das8a0732e2015-11-20 15:27:53 -0800465 nextObjective.id(), deviceId);
Charles Chan425854b2016-04-11 15:32:12 -0700466 groupHandler.removeBucketFromGroup(nextObjective, nextGroup);
Saurav Das8a0732e2015-11-20 15:27:53 -0800467 break;
Jonghwan Hyunf810a7a2018-02-12 16:43:45 +0900468 case MODIFY:
469 if (nextGroup == null) {
470 log.warn("Cannot modify next {} that does not exist in device {}",
471 nextObjective.id(), deviceId);
472 return;
473 }
Ruchi Sahotaef0761c2019-01-28 01:08:18 +0000474 log.debug("Processing NextObjective id {} in dev {} group {} - modify bucket",
475 nextObjective.id(), deviceId, nextGroup);
Jonghwan Hyunf810a7a2018-02-12 16:43:45 +0900476 groupHandler.modifyBucketFromGroup(nextObjective, nextGroup);
477 break;
Saurav Dasceccf242017-08-03 18:30:35 -0700478 case VERIFY:
479 if (nextGroup == null) {
480 log.warn("Cannot verify next {} that does not exist in device {}",
481 nextObjective.id(), deviceId);
482 return;
483 }
484 log.debug("Processing NextObjective id {} in dev {} - verify",
485 nextObjective.id(), deviceId);
486 groupHandler.verifyGroup(nextObjective, nextGroup);
487 break;
Saurav Das8a0732e2015-11-20 15:27:53 -0800488 default:
Saurav Das4f980082015-11-05 13:39:15 -0800489 log.warn("Unsupported operation {}", nextObjective.op());
Saurav Das822c4e22015-10-23 10:51:11 -0700490 }
491 }
492
493 //////////////////////////////////////
494 // Flow handling
495 //////////////////////////////////////
496
497 /**
Yi Tseng0fdc07e2017-04-24 19:17:08 -0700498 * As per OFDPA 2.0 TTP, filtering of VLAN ids and MAC addresses (for routing)
499 * configured on switch ports happen in different tables.
Saurav Das822c4e22015-10-23 10:51:11 -0700500 *
501 * @param filt the filtering objective
502 * @param install indicates whether to add or remove the objective
503 * @param applicationId the application that sent this objective
504 */
Saurav Das52025962016-01-28 22:30:01 -0800505 protected void processFilter(FilteringObjective filt,
506 boolean install, ApplicationId applicationId) {
Saurav Das822c4e22015-10-23 10:51:11 -0700507 // This driver only processes filtering criteria defined with switch
508 // ports as the key
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530509 PortCriterion portCriterion = null;
Saurav Das822c4e22015-10-23 10:51:11 -0700510 EthCriterion ethCriterion = null;
511 VlanIdCriterion vidCriterion = null;
Saurav Das822c4e22015-10-23 10:51:11 -0700512 if (!filt.key().equals(Criteria.dummy()) &&
513 filt.key().type() == Criterion.Type.IN_PORT) {
514 portCriterion = (PortCriterion) filt.key();
Saurav Das822c4e22015-10-23 10:51:11 -0700515 }
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530516 if (portCriterion == null) {
517 log.debug("No IN_PORT defined in filtering objective from app: {}",
518 applicationId);
519 } else {
520 log.debug("Received filtering objective for dev/port: {}/{}", deviceId,
521 portCriterion.port());
522 }
Saurav Das822c4e22015-10-23 10:51:11 -0700523 // convert filtering conditions for switch-intfs into flowrules
524 FlowRuleOperations.Builder ops = FlowRuleOperations.builder();
525 for (Criterion criterion : filt.conditions()) {
Yi Tseng0fdc07e2017-04-24 19:17:08 -0700526 switch (criterion.type()) {
527 case ETH_DST:
528 case ETH_DST_MASKED:
529 ethCriterion = (EthCriterion) criterion;
530 break;
531 case VLAN_VID:
532 vidCriterion = (VlanIdCriterion) criterion;
533 break;
534 default:
535 log.warn("Unsupported filter {}", criterion);
536 fail(filt, ObjectiveError.UNSUPPORTED);
537 return;
Saurav Das822c4e22015-10-23 10:51:11 -0700538 }
539 }
540
Saurav Das0e99e2b2015-10-28 12:39:42 -0700541 VlanId assignedVlan = null;
Charles Chane849c192016-01-11 18:28:54 -0800542 if (vidCriterion != null) {
Charles Chand55e84d2016-03-30 17:54:24 -0700543 // Use the VLAN in criterion if metadata is not present and the traffic is tagged
Charles Chanc550f2e2017-12-19 19:55:57 -0800544 if (!vidCriterion.vlanId().equals(VlanId.NONE)) {
Charles Chane849c192016-01-11 18:28:54 -0800545 assignedVlan = vidCriterion.vlanId();
Piere5bff482018-03-07 11:42:50 +0100546 }
547 // If the meta VLAN is present let's update the assigned vlan
548 if (filt.meta() != null) {
549 VlanId metaVlan = readVlanFromTreatment(filt.meta());
550 if (metaVlan != null) {
551 assignedVlan = metaVlan;
552 }
Charles Chand55e84d2016-03-30 17:54:24 -0700553 }
Charles Chane849c192016-01-11 18:28:54 -0800554
Charles Chand55e84d2016-03-30 17:54:24 -0700555 if (assignedVlan == null) {
556 log.error("Driver fails to extract VLAN information. "
Frank Wangd8ab0962017-08-11 11:09:30 +0800557 + "Not processing VLAN filters on device {}.", deviceId);
Charles Chand55e84d2016-03-30 17:54:24 -0700558 log.debug("VLAN ID in criterion={}, metadata={}",
559 readVlanFromTreatment(filt.meta()), vidCriterion.vlanId());
560 fail(filt, ObjectiveError.BADPARAMS);
561 return;
Saurav Das0e99e2b2015-10-28 12:39:42 -0700562 }
563 }
564
Yi Tseng3a77b4f2017-02-06 15:02:17 -0800565 if (ethCriterion == null || ethCriterion.mac().equals(NONE)) {
Charles Chan985b12e2016-05-11 19:47:22 -0700566 // NOTE: it is possible that a filtering objective only has vidCriterion
Daniel Ginsburgc1d47e92018-04-30 19:27:19 -0400567 log.debug("filtering objective missing dstMac, won't program TMAC table");
Saurav Das822c4e22015-10-23 10:51:11 -0700568 } else {
Charles Chan45b69ab2018-03-02 15:41:41 -0800569 MacAddress unicastMac = readEthDstFromTreatment(filt.meta());
Charles Chan66291502018-03-02 16:43:28 -0800570 List<List<FlowRule>> allStages = processEthDstFilter(portCriterion, ethCriterion,
Charles Chan45b69ab2018-03-02 15:41:41 -0800571 vidCriterion, assignedVlan, unicastMac, applicationId);
Charles Chan66291502018-03-02 16:43:28 -0800572 for (List<FlowRule> flowRules : allStages) {
Charles Chan45b69ab2018-03-02 15:41:41 -0800573 log.trace("Starting a new flow rule stage for TMAC table flow");
Charles Chan66291502018-03-02 16:43:28 -0800574 ops.newStage();
Charles Chanc550f2e2017-12-19 19:55:57 -0800575
Charles Chan66291502018-03-02 16:43:28 -0800576 for (FlowRule flowRule : flowRules) {
pier5be39042019-10-04 10:54:07 +0200577 log.trace("{} flow rule in TMAC table: {} for dev: {}",
578 (install) ? "adding" : "removing", flowRule, deviceId);
Charles Chan66291502018-03-02 16:43:28 -0800579 if (install) {
580 ops = ops.add(flowRule);
Charles Chanc550f2e2017-12-19 19:55:57 -0800581 } else {
Charles Chan66291502018-03-02 16:43:28 -0800582 // NOTE: Only remove TMAC flow when there is no more enabled port within the
583 // same VLAN on this device if TMAC doesn't support matching on in_port.
pier5be39042019-10-04 10:54:07 +0200584 if (filt.meta() != null && filt.meta().clearedDeferred()) {
585 // TMac mcast does not match on the input port - we have to remove it
586 // only if this is the last port
587 FlowRule rule = buildTmacRuleForMcastFromUnicast(flowRule, applicationId);
588 // IPv6 or IPv4 tmac rule
589 if (rule != null) {
590 // Add first the mcast rule and then open a new stage for the unicast
591 ops = ops.remove(rule);
592 ops.newStage();
593 }
594 ops = ops.remove(flowRule);
595 } else if (matchInPortTmacTable()) {
Charles Chan66291502018-03-02 16:43:28 -0800596 ops = ops.remove(flowRule);
597 } else {
pier5be39042019-10-04 10:54:07 +0200598 log.debug("Abort TMAC flow removal on {}. " +
599 "Some other ports still share this TMAC flow", deviceId);
Charles Chan66291502018-03-02 16:43:28 -0800600 }
Charles Chanc550f2e2017-12-19 19:55:57 -0800601 }
602 }
Saurav Das822c4e22015-10-23 10:51:11 -0700603 }
604 }
605
Charles Chan985b12e2016-05-11 19:47:22 -0700606 if (vidCriterion == null) {
607 // NOTE: it is possible that a filtering objective only has ethCriterion
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530608 log.info("filtering objective missing VLAN, cannot program VLAN Table");
Saurav Das822c4e22015-10-23 10:51:11 -0700609 } else {
Charles Chan66291502018-03-02 16:43:28 -0800610 List<List<FlowRule>> allStages = processVlanIdFilter(
pier6aef5b72019-06-10 17:10:26 +0200611 portCriterion, vidCriterion, assignedVlan, applicationId, install);
Charles Chan66291502018-03-02 16:43:28 -0800612 for (List<FlowRule> flowRules : allStages) {
Charles Chan45b69ab2018-03-02 15:41:41 -0800613 log.trace("Starting a new flow rule stage for VLAN table flow");
Charles Chan66291502018-03-02 16:43:28 -0800614 ops.newStage();
Charles Chan14967c22015-12-07 11:11:50 -0800615
Charles Chan66291502018-03-02 16:43:28 -0800616 for (FlowRule flowRule : flowRules) {
617 log.trace("{} flow rules in VLAN table: {} for dev: {}",
618 (install) ? "adding" : "removing", flowRule, deviceId);
619 ops = install ? ops.add(flowRule) : ops.remove(flowRule);
Charles Chand1172632017-03-15 17:33:09 -0700620 }
Saurav Das822c4e22015-10-23 10:51:11 -0700621 }
622 }
623
Saurav Das822c4e22015-10-23 10:51:11 -0700624 // apply filtering flow rules
625 flowRuleService.apply(ops.build(new FlowRuleOperationsContext() {
626 @Override
627 public void onSuccess(FlowRuleOperations ops) {
Saurav Das018605f2017-02-18 14:05:44 -0800628 log.debug("Applied {} filtering rules in device {}",
Saurav Das822c4e22015-10-23 10:51:11 -0700629 ops.stages().get(0).size(), deviceId);
630 pass(filt);
631 }
632
633 @Override
634 public void onError(FlowRuleOperations ops) {
635 log.info("Failed to apply all filtering rules in dev {}", deviceId);
636 fail(filt, ObjectiveError.FLOWINSTALLATIONFAILED);
637 }
638 }));
639
640 }
641
642 /**
Charles Chand1172632017-03-15 17:33:09 -0700643 * Internal implementation of processVlanIdFilter.
644 * <p>
645 * The is_present bit in set_vlan_vid action is required to be 0 in OFDPA i12.
646 * Since it is non-OF spec, we need an extension treatment for that.
647 * The useVlanExtension must be set to false for OFDPA i12.
648 * </p>
Charles Chan66291502018-03-02 16:43:28 -0800649 * <p>
650 * NOTE: Separate VLAN filtering rules and assignment rules
651 * into different stages in order to guarantee that filtering rules
652 * always go first, as required by OFDPA.
653 * </p>
Saurav Das0e99e2b2015-10-28 12:39:42 -0700654 *
Charles Chanf9e98652016-09-07 16:54:23 -0700655 * @param portCriterion port on device for which this filter is programmed
656 * @param vidCriterion vlan assigned to port, or NONE for untagged
657 * @param assignedVlan assigned vlan-id for untagged packets
658 * @param applicationId for application programming this filter
pier6aef5b72019-06-10 17:10:26 +0200659 * @param install indicates whether to add or remove the objective
Charles Chan66291502018-03-02 16:43:28 -0800660 * @return stages of flow rules for port-vlan filters
Saurav Das822c4e22015-10-23 10:51:11 -0700661 */
Charles Chan66291502018-03-02 16:43:28 -0800662 protected List<List<FlowRule>> processVlanIdFilter(PortCriterion portCriterion,
pier6aef5b72019-06-10 17:10:26 +0200663 VlanIdCriterion vidCriterion,
664 VlanId assignedVlan,
665 ApplicationId applicationId,
666 boolean install) {
Charles Chan66291502018-03-02 16:43:28 -0800667 List<FlowRule> filteringRules = new ArrayList<>();
668 List<FlowRule> assignmentRules = new ArrayList<>();
Saurav Das822c4e22015-10-23 10:51:11 -0700669 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
670 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
Charles Chan14967c22015-12-07 11:11:50 -0800671 TrafficSelector.Builder preSelector = null;
672 TrafficTreatment.Builder preTreatment = null;
673
Saurav Das4f980082015-11-05 13:39:15 -0800674 treatment.transition(TMAC_TABLE);
675
Saurav Das822c4e22015-10-23 10:51:11 -0700676 if (vidCriterion.vlanId() == VlanId.NONE) {
677 // untagged packets are assigned vlans
Charles Chand1172632017-03-15 17:33:09 -0700678 preSelector = DefaultTrafficSelector.builder();
679 if (requireVlanExtensions()) {
680 OfdpaMatchVlanVid ofdpaMatchVlanVid = new OfdpaMatchVlanVid(VlanId.NONE);
681 selector.extension(ofdpaMatchVlanVid, deviceId);
Charles Chanf9e98652016-09-07 16:54:23 -0700682 OfdpaSetVlanVid ofdpaSetVlanVid = new OfdpaSetVlanVid(assignedVlan);
683 treatment.extension(ofdpaSetVlanVid, deviceId);
Charles Chand1172632017-03-15 17:33:09 -0700684
685 OfdpaMatchVlanVid preOfdpaMatchVlanVid = new OfdpaMatchVlanVid(assignedVlan);
686 preSelector.extension(preOfdpaMatchVlanVid, deviceId);
Charles Chanf9e98652016-09-07 16:54:23 -0700687 } else {
Charles Chand1172632017-03-15 17:33:09 -0700688 selector.matchVlanId(VlanId.NONE);
Charles Chanf9e98652016-09-07 16:54:23 -0700689 treatment.setVlanId(assignedVlan);
Charles Chand1172632017-03-15 17:33:09 -0700690
691 preSelector.matchVlanId(assignedVlan);
692 }
693 preTreatment = DefaultTrafficTreatment.builder().transition(TMAC_TABLE);
694 } else {
695 if (requireVlanExtensions()) {
696 OfdpaMatchVlanVid ofdpaMatchVlanVid = new OfdpaMatchVlanVid(vidCriterion.vlanId());
697 selector.extension(ofdpaMatchVlanVid, deviceId);
698 } else {
699 selector.matchVlanId(vidCriterion.vlanId());
Charles Chanf9e98652016-09-07 16:54:23 -0700700 }
Charles Chan14967c22015-12-07 11:11:50 -0800701
Charles Chand55e84d2016-03-30 17:54:24 -0700702 if (!assignedVlan.equals(vidCriterion.vlanId())) {
Charles Chand1172632017-03-15 17:33:09 -0700703 if (requireVlanExtensions()) {
Charles Chanc03782d2017-01-31 13:57:55 -0800704 OfdpaSetVlanVid ofdpaSetVlanVid = new OfdpaSetVlanVid(assignedVlan);
705 treatment.extension(ofdpaSetVlanVid, deviceId);
706 } else {
707 treatment.setVlanId(assignedVlan);
708 }
Charles Chand55e84d2016-03-30 17:54:24 -0700709 }
Saurav Das822c4e22015-10-23 10:51:11 -0700710 }
Saurav Das822c4e22015-10-23 10:51:11 -0700711
712 // ofdpa cannot match on ALL portnumber, so we need to use separate
713 // rules for each port.
Charles Chan14967c22015-12-07 11:11:50 -0800714 List<PortNumber> portnums = new ArrayList<>();
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530715 if (portCriterion != null) {
716 if (PortNumber.ALL.equals(portCriterion.port())) {
717 for (Port port : deviceService.getPorts(deviceId)) {
718 if (port.number().toLong() > 0 && port.number().toLong() < OFPP_MAX) {
719 portnums.add(port.number());
720 }
Saurav Das822c4e22015-10-23 10:51:11 -0700721 }
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530722 } else {
723 portnums.add(portCriterion.port());
Saurav Das822c4e22015-10-23 10:51:11 -0700724 }
725 } else {
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530726 log.warn("Filtering Objective missing Port Criterion . " +
Charles Chan66291502018-03-02 16:43:28 -0800727 "VLAN Table cannot be programmed for {}", deviceId);
Saurav Das822c4e22015-10-23 10:51:11 -0700728 }
Saurav Das4f980082015-11-05 13:39:15 -0800729
Saurav Das822c4e22015-10-23 10:51:11 -0700730 for (PortNumber pnum : portnums) {
Saurav Das4f980082015-11-05 13:39:15 -0800731 // create rest of flowrule
Saurav Das822c4e22015-10-23 10:51:11 -0700732 selector.matchInPort(pnum);
733 FlowRule rule = DefaultFlowRule.builder()
734 .forDevice(deviceId)
735 .withSelector(selector.build())
736 .withTreatment(treatment.build())
737 .withPriority(DEFAULT_PRIORITY)
738 .fromApp(applicationId)
739 .makePermanent()
740 .forTable(VLAN_TABLE).build();
Charles Chan66291502018-03-02 16:43:28 -0800741 assignmentRules.add(rule);
Charles Chan14967c22015-12-07 11:11:50 -0800742
743 if (preSelector != null) {
744 preSelector.matchInPort(pnum);
745 FlowRule preRule = DefaultFlowRule.builder()
746 .forDevice(deviceId)
747 .withSelector(preSelector.build())
748 .withTreatment(preTreatment.build())
749 .withPriority(DEFAULT_PRIORITY)
750 .fromApp(applicationId)
751 .makePermanent()
752 .forTable(VLAN_TABLE).build();
Charles Chan66291502018-03-02 16:43:28 -0800753 filteringRules.add(preRule);
Charles Chan14967c22015-12-07 11:11:50 -0800754 }
Saurav Das822c4e22015-10-23 10:51:11 -0700755 }
pier6aef5b72019-06-10 17:10:26 +0200756 return install ? ImmutableList.of(filteringRules, assignmentRules) :
757 ImmutableList.of(assignmentRules, filteringRules);
Saurav Das822c4e22015-10-23 10:51:11 -0700758 }
759
760 /**
761 * Allows routed packets with correct destination MAC to be directed
Charles Chan45b69ab2018-03-02 15:41:41 -0800762 * to unicast routing table, multicast routing table or MPLS forwarding table.
Saurav Das822c4e22015-10-23 10:51:11 -0700763 *
764 * @param portCriterion port on device for which this filter is programmed
765 * @param ethCriterion dstMac of device for which is filter is programmed
766 * @param vidCriterion vlan assigned to port, or NONE for untagged
Saurav Das0e99e2b2015-10-28 12:39:42 -0700767 * @param assignedVlan assigned vlan-id for untagged packets
Charles Chan45b69ab2018-03-02 15:41:41 -0800768 * @param unicastMac some switches require a unicast TMAC flow to be programmed before multicast
769 * TMAC flow. This MAC address will be used for the unicast TMAC flow.
770 * This is unused if the filtering objective is a unicast.
Saurav Das822c4e22015-10-23 10:51:11 -0700771 * @param applicationId for application programming this filter
Charles Chan66291502018-03-02 16:43:28 -0800772 * @return stages of flow rules for port-vlan filters
Saurav Das822c4e22015-10-23 10:51:11 -0700773
774 */
Charles Chan66291502018-03-02 16:43:28 -0800775 protected List<List<FlowRule>> processEthDstFilter(PortCriterion portCriterion,
Saurav Das822c4e22015-10-23 10:51:11 -0700776 EthCriterion ethCriterion,
777 VlanIdCriterion vidCriterion,
Saurav Das0e99e2b2015-10-28 12:39:42 -0700778 VlanId assignedVlan,
Charles Chan45b69ab2018-03-02 15:41:41 -0800779 MacAddress unicastMac,
Saurav Das822c4e22015-10-23 10:51:11 -0700780 ApplicationId applicationId) {
Charles Chan5270ed02016-01-30 23:22:37 -0800781 // Consider PortNumber.ANY as wildcard. Match ETH_DST only
Sivachidambaram Subramanianc598d062017-05-16 22:42:06 +0530782 if (portCriterion != null && PortNumber.ANY.equals(portCriterion.port())) {
Charles Chan5270ed02016-01-30 23:22:37 -0800783 return processEthDstOnlyFilter(ethCriterion, applicationId);
784 }
785
Charles Chan5b9df8d2016-03-28 22:21:40 -0700786 // Multicast MAC
787 if (ethCriterion.mask() != null) {
Charles Chan45b69ab2018-03-02 15:41:41 -0800788 return processMcastEthDstFilter(ethCriterion, assignedVlan, unicastMac, applicationId);
Charles Chan5b9df8d2016-03-28 22:21:40 -0700789 }
790
Saurav Das822c4e22015-10-23 10:51:11 -0700791 //handling untagged packets via assigned VLAN
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530792 if (vidCriterion != null && vidCriterion.vlanId() == VlanId.NONE) {
Saurav Das0e99e2b2015-10-28 12:39:42 -0700793 vidCriterion = (VlanIdCriterion) Criteria.matchVlanId(assignedVlan);
Saurav Das822c4e22015-10-23 10:51:11 -0700794 }
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530795 List<FlowRule> rules = new ArrayList<>();
796 OfdpaMatchVlanVid ofdpaMatchVlanVid = null;
797 if (vidCriterion != null && requireVlanExtensions()) {
798 ofdpaMatchVlanVid = new OfdpaMatchVlanVid(vidCriterion.vlanId());
799 }
Saurav Das822c4e22015-10-23 10:51:11 -0700800 // ofdpa cannot match on ALL portnumber, so we need to use separate
801 // rules for each port.
Yuta HIGUCHI2dce08a2017-04-20 21:57:48 -0700802 List<PortNumber> portnums = new ArrayList<>();
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530803 if (portCriterion != null) {
804 if (PortNumber.ALL.equals(portCriterion.port())) {
805 for (Port port : deviceService.getPorts(deviceId)) {
806 if (port.number().toLong() > 0 && port.number().toLong() < OFPP_MAX) {
807 portnums.add(port.number());
808 }
Saurav Das822c4e22015-10-23 10:51:11 -0700809 }
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530810 } else {
811 portnums.add(portCriterion.port());
812 }
813 for (PortNumber pnum : portnums) {
814 rules.add(buildTmacRuleForIpv4(ethCriterion,
815 vidCriterion,
816 ofdpaMatchVlanVid,
817 applicationId,
818 pnum));
819 rules.add(buildTmacRuleForMpls(ethCriterion,
820 vidCriterion,
821 ofdpaMatchVlanVid,
822 applicationId,
823 pnum));
824 rules.add(buildTmacRuleForIpv6(ethCriterion,
825 vidCriterion,
826 ofdpaMatchVlanVid,
827 applicationId,
828 pnum));
Saurav Das822c4e22015-10-23 10:51:11 -0700829 }
830 } else {
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530831 rules.add(buildTmacRuleForIpv4(ethCriterion,
832 vidCriterion,
833 ofdpaMatchVlanVid,
834 applicationId,
835 null));
836 rules.add(buildTmacRuleForMpls(ethCriterion,
837 vidCriterion,
838 ofdpaMatchVlanVid,
839 applicationId,
840 null));
841 rules.add(buildTmacRuleForIpv6(ethCriterion,
842 vidCriterion,
843 ofdpaMatchVlanVid,
844 applicationId,
845 null));
Saurav Das822c4e22015-10-23 10:51:11 -0700846 }
Charles Chan66291502018-03-02 16:43:28 -0800847 return ImmutableList.of(rules);
Saurav Das822c4e22015-10-23 10:51:11 -0700848 }
849
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530850 /**
851 * Builds TMAC rules for IPv4 packets.
852 *
Charles Chanc550f2e2017-12-19 19:55:57 -0800853 * @param ethCriterion dst mac matching
854 * @param vidCriterion vlan id assigned to the port
855 * @param ofdpaMatchVlanVid OFDPA vlan id matching
856 * @param applicationId application id
857 * @param pnum port number
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530858 * @return TMAC rule for IPV4 packets
859 */
860 private FlowRule buildTmacRuleForIpv4(EthCriterion ethCriterion,
861 VlanIdCriterion vidCriterion,
862 OfdpaMatchVlanVid ofdpaMatchVlanVid,
863 ApplicationId applicationId,
864 PortNumber pnum) {
865 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
866 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
867 if (pnum != null) {
868 if (matchInPortTmacTable()) {
869 selector.matchInPort(pnum);
870 } else {
Saurav Dasf14d9ef2017-12-05 15:00:23 -0800871 log.debug("Pipeline does not support IN_PORT matching in TMAC table, " +
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530872 "ignoring the IN_PORT criteria");
873 }
874 }
875 if (vidCriterion != null) {
876 if (requireVlanExtensions()) {
877 selector.extension(ofdpaMatchVlanVid, deviceId);
878 } else {
879 selector.matchVlanId(vidCriterion.vlanId());
880 }
881 }
882 selector.matchEthType(Ethernet.TYPE_IPV4);
883 selector.matchEthDst(ethCriterion.mac());
884 treatment.transition(UNICAST_ROUTING_TABLE);
885 return DefaultFlowRule.builder()
886 .forDevice(deviceId)
887 .withSelector(selector.build())
888 .withTreatment(treatment.build())
889 .withPriority(DEFAULT_PRIORITY)
890 .fromApp(applicationId)
891 .makePermanent()
892 .forTable(TMAC_TABLE).build();
893 }
894
895 /**
896 * Builds TMAC rule for MPLS packets.
897 *
Charles Chanc550f2e2017-12-19 19:55:57 -0800898 * @param ethCriterion dst mac matching
899 * @param vidCriterion vlan id assigned to the port
900 * @param ofdpaMatchVlanVid OFDPA vlan id matching
901 * @param applicationId application id
902 * @param pnum port number
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530903 * @return TMAC rule for MPLS packets
904 */
905 private FlowRule buildTmacRuleForMpls(EthCriterion ethCriterion,
906 VlanIdCriterion vidCriterion,
907 OfdpaMatchVlanVid ofdpaMatchVlanVid,
908 ApplicationId applicationId,
909 PortNumber pnum) {
910 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
911 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
912 if (pnum != null) {
913 if (matchInPortTmacTable()) {
914 selector.matchInPort(pnum);
915 } else {
Saurav Dasf14d9ef2017-12-05 15:00:23 -0800916 log.debug("Pipeline does not support IN_PORT matching in TMAC table, " +
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530917 "ignoring the IN_PORT criteria");
918 }
919 }
920 if (vidCriterion != null) {
921 if (requireVlanExtensions()) {
922 selector.extension(ofdpaMatchVlanVid, deviceId);
923 } else {
924 selector.matchVlanId(vidCriterion.vlanId());
925 }
926 }
927 selector.matchEthType(Ethernet.MPLS_UNICAST);
928 selector.matchEthDst(ethCriterion.mac());
929 treatment.transition(MPLS_TABLE_0);
930 return DefaultFlowRule.builder()
931 .forDevice(deviceId)
932 .withSelector(selector.build())
933 .withTreatment(treatment.build())
934 .withPriority(DEFAULT_PRIORITY)
935 .fromApp(applicationId)
936 .makePermanent()
937 .forTable(TMAC_TABLE).build();
938 }
939
940 /**
941 * Builds TMAC rules for IPv6 packets.
942 *
Charles Chanc550f2e2017-12-19 19:55:57 -0800943 * @param ethCriterion dst mac matching
944 * @param vidCriterion vlan id assigned to the port
945 * @param ofdpaMatchVlanVid OFDPA vlan id matching
946 * @param applicationId application id
947 * @param pnum port number
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530948 * @return TMAC rule for IPV6 packets
949 */
950 private FlowRule buildTmacRuleForIpv6(EthCriterion ethCriterion,
951 VlanIdCriterion vidCriterion,
952 OfdpaMatchVlanVid ofdpaMatchVlanVid,
953 ApplicationId applicationId,
954 PortNumber pnum) {
955 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
956 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
957 if (pnum != null) {
958 if (matchInPortTmacTable()) {
959 selector.matchInPort(pnum);
960 } else {
Saurav Dasf14d9ef2017-12-05 15:00:23 -0800961 log.debug("Pipeline does not support IN_PORT matching in TMAC table, " +
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530962 "ignoring the IN_PORT criteria");
963 }
964 }
965 if (vidCriterion != null) {
966 if (requireVlanExtensions()) {
967 selector.extension(ofdpaMatchVlanVid, deviceId);
968 } else {
969 selector.matchVlanId(vidCriterion.vlanId());
970 }
971 }
972 selector.matchEthType(Ethernet.TYPE_IPV6);
973 selector.matchEthDst(ethCriterion.mac());
974 treatment.transition(UNICAST_ROUTING_TABLE);
975 return DefaultFlowRule.builder()
976 .forDevice(deviceId)
977 .withSelector(selector.build())
978 .withTreatment(treatment.build())
979 .withPriority(DEFAULT_PRIORITY)
980 .fromApp(applicationId)
981 .makePermanent()
982 .forTable(TMAC_TABLE).build();
983 }
984
Charles Chan66291502018-03-02 16:43:28 -0800985 protected List<List<FlowRule>> processEthDstOnlyFilter(EthCriterion ethCriterion,
Yi Tsengef19de12017-04-24 11:33:05 -0700986 ApplicationId applicationId) {
Pier Luigi0e358632017-01-31 09:35:05 -0800987 ImmutableList.Builder<FlowRule> builder = ImmutableList.builder();
988
Charles Chan5270ed02016-01-30 23:22:37 -0800989 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
990 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
991 selector.matchEthType(Ethernet.TYPE_IPV4);
992 selector.matchEthDst(ethCriterion.mac());
993 treatment.transition(UNICAST_ROUTING_TABLE);
994 FlowRule rule = DefaultFlowRule.builder()
995 .forDevice(deviceId)
996 .withSelector(selector.build())
997 .withTreatment(treatment.build())
998 .withPriority(DEFAULT_PRIORITY)
999 .fromApp(applicationId)
1000 .makePermanent()
1001 .forTable(TMAC_TABLE).build();
Pier Luigi0e358632017-01-31 09:35:05 -08001002 builder.add(rule);
1003
1004 selector = DefaultTrafficSelector.builder();
1005 treatment = DefaultTrafficTreatment.builder();
1006 selector.matchEthType(Ethernet.TYPE_IPV6);
1007 selector.matchEthDst(ethCriterion.mac());
1008 treatment.transition(UNICAST_ROUTING_TABLE);
1009 rule = DefaultFlowRule.builder()
1010 .forDevice(deviceId)
1011 .withSelector(selector.build())
1012 .withTreatment(treatment.build())
1013 .withPriority(DEFAULT_PRIORITY)
1014 .fromApp(applicationId)
1015 .makePermanent()
1016 .forTable(TMAC_TABLE).build();
Charles Chan66291502018-03-02 16:43:28 -08001017 return ImmutableList.of(builder.add(rule).build());
Charles Chan5270ed02016-01-30 23:22:37 -08001018 }
1019
pier5be39042019-10-04 10:54:07 +02001020 private FlowRule buildTmacRuleForMcastFromUnicast(FlowRule tmacRuleForUnicast, ApplicationId applicationId) {
1021 final TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
1022 final TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
1023 FlowRule rule;
1024 // Build the selector
1025 for (Criterion criterion : tmacRuleForUnicast.selector().criteria()) {
1026 if (criterion instanceof VlanIdCriterion) {
1027 if (requireVlanExtensions()) {
1028 OfdpaMatchVlanVid ofdpaMatchVlanVid = new OfdpaMatchVlanVid(((VlanIdCriterion) criterion).vlanId());
1029 selector.extension(ofdpaMatchVlanVid, deviceId);
1030 } else {
1031 selector.add(criterion);
1032 }
1033 } else if (criterion instanceof EthTypeCriterion) {
1034 selector.add(criterion);
1035 if (Objects.equals(((EthTypeCriterion) criterion).ethType(),
1036 EtherType.IPV4.ethType())) {
1037 selector.matchEthDstMasked(IPV4_MULTICAST, IPV4_MULTICAST_MASK);
1038 } else if (Objects.equals(((EthTypeCriterion) criterion).ethType(),
1039 EtherType.IPV6.ethType())) {
1040 selector.matchEthDstMasked(IPV6_MULTICAST, IPV6_MULTICAST_MASK);
1041 } else {
1042 // We don't need for mpls rules
1043 return null;
1044 }
1045 }
1046 }
1047 // Build the treatment
1048 treatment.transition(MULTICAST_ROUTING_TABLE);
1049 // Build the flowrule
1050 rule = DefaultFlowRule.builder()
1051 .forDevice(deviceId)
1052 .withSelector(selector.build())
1053 .withTreatment(treatment.build())
1054 .withPriority(DEFAULT_PRIORITY)
1055 .fromApp(applicationId)
1056 .makePermanent()
1057 .forTable(TMAC_TABLE).build();
pierbf688182019-12-06 12:39:51 +01001058 log.debug("Building TMac Mcast flowRule {}", rule);
pier5be39042019-10-04 10:54:07 +02001059 return rule;
1060 }
1061
Charles Chan66291502018-03-02 16:43:28 -08001062 List<List<FlowRule>> processMcastEthDstFilter(EthCriterion ethCriterion,
Charles Chanb4879a52017-10-20 19:09:16 -07001063 VlanId assignedVlan,
Charles Chan45b69ab2018-03-02 15:41:41 -08001064 MacAddress unicastMac,
Yi Tsengef19de12017-04-24 11:33:05 -07001065 ApplicationId applicationId) {
Charles Chan45b69ab2018-03-02 15:41:41 -08001066 ImmutableList.Builder<FlowRule> unicastFlows = ImmutableList.builder();
1067 ImmutableList.Builder<FlowRule> multicastFlows = ImmutableList.builder();
1068 TrafficSelector.Builder selector;
1069 TrafficTreatment.Builder treatment;
Charles Chanb4879a52017-10-20 19:09:16 -07001070 FlowRule rule;
Julia Ferguson65428c32017-08-10 18:15:24 +00001071
Charles Chanb4879a52017-10-20 19:09:16 -07001072 if (IPV4_MULTICAST.equals(ethCriterion.mac())) {
Charles Chan45b69ab2018-03-02 15:41:41 -08001073 if (requireUnicastBeforeMulticast()) {
1074 selector = DefaultTrafficSelector.builder();
1075 treatment = DefaultTrafficTreatment.builder();
1076 selector.matchEthType(Ethernet.TYPE_IPV4);
1077 selector.matchEthDst(unicastMac);
Charles Chan056e0c12018-05-10 22:19:49 +00001078 selector.matchVlanId(assignedVlan);
Charles Chan45b69ab2018-03-02 15:41:41 -08001079 treatment.transition(UNICAST_ROUTING_TABLE);
1080 rule = DefaultFlowRule.builder()
1081 .forDevice(deviceId)
1082 .withSelector(selector.build())
1083 .withTreatment(treatment.build())
Charles Chan056e0c12018-05-10 22:19:49 +00001084 .withPriority(DEFAULT_PRIORITY)
Charles Chan45b69ab2018-03-02 15:41:41 -08001085 .fromApp(applicationId)
1086 .makePermanent()
1087 .forTable(TMAC_TABLE).build();
1088 unicastFlows.add(rule);
1089 }
1090
1091 selector = DefaultTrafficSelector.builder();
1092 treatment = DefaultTrafficTreatment.builder();
Charles Chanb4879a52017-10-20 19:09:16 -07001093 selector.matchEthType(Ethernet.TYPE_IPV4);
1094 selector.matchEthDstMasked(ethCriterion.mac(), ethCriterion.mask());
Charles Chan056e0c12018-05-10 22:19:49 +00001095 selector.matchVlanId(assignedVlan);
Charles Chanb4879a52017-10-20 19:09:16 -07001096 treatment.transition(MULTICAST_ROUTING_TABLE);
1097 rule = DefaultFlowRule.builder()
1098 .forDevice(deviceId)
1099 .withSelector(selector.build())
1100 .withTreatment(treatment.build())
Charles Chan056e0c12018-05-10 22:19:49 +00001101 .withPriority(DEFAULT_PRIORITY)
Charles Chanb4879a52017-10-20 19:09:16 -07001102 .fromApp(applicationId)
1103 .makePermanent()
1104 .forTable(TMAC_TABLE).build();
Charles Chan45b69ab2018-03-02 15:41:41 -08001105 multicastFlows.add(rule);
Charles Chanb4879a52017-10-20 19:09:16 -07001106 }
1107
1108 if (IPV6_MULTICAST.equals(ethCriterion.mac())) {
Charles Chan45b69ab2018-03-02 15:41:41 -08001109 if (requireUnicastBeforeMulticast()) {
1110 selector = DefaultTrafficSelector.builder();
1111 treatment = DefaultTrafficTreatment.builder();
1112 selector.matchEthType(Ethernet.TYPE_IPV6);
1113 selector.matchEthDst(unicastMac);
Charles Chan056e0c12018-05-10 22:19:49 +00001114 selector.matchVlanId(assignedVlan);
Charles Chan45b69ab2018-03-02 15:41:41 -08001115 treatment.transition(UNICAST_ROUTING_TABLE);
1116 rule = DefaultFlowRule.builder()
1117 .forDevice(deviceId)
1118 .withSelector(selector.build())
1119 .withTreatment(treatment.build())
Charles Chan056e0c12018-05-10 22:19:49 +00001120 .withPriority(DEFAULT_PRIORITY)
Charles Chan45b69ab2018-03-02 15:41:41 -08001121 .fromApp(applicationId)
1122 .makePermanent()
1123 .forTable(TMAC_TABLE).build();
1124 unicastFlows.add(rule);
1125 }
1126
Charles Chanb4879a52017-10-20 19:09:16 -07001127 selector = DefaultTrafficSelector.builder();
1128 treatment = DefaultTrafficTreatment.builder();
1129 selector.matchEthType(Ethernet.TYPE_IPV6);
1130 selector.matchEthDstMasked(ethCriterion.mac(), ethCriterion.mask());
Charles Chan056e0c12018-05-10 22:19:49 +00001131 selector.matchVlanId(assignedVlan);
Charles Chanb4879a52017-10-20 19:09:16 -07001132 treatment.transition(MULTICAST_ROUTING_TABLE);
1133 rule = DefaultFlowRule.builder()
1134 .forDevice(deviceId)
1135 .withSelector(selector.build())
1136 .withTreatment(treatment.build())
Charles Chan056e0c12018-05-10 22:19:49 +00001137 .withPriority(DEFAULT_PRIORITY)
Charles Chanb4879a52017-10-20 19:09:16 -07001138 .fromApp(applicationId)
1139 .makePermanent()
1140 .forTable(TMAC_TABLE).build();
Charles Chan45b69ab2018-03-02 15:41:41 -08001141 multicastFlows.add(rule);
Charles Chanb4879a52017-10-20 19:09:16 -07001142 }
Charles Chan45b69ab2018-03-02 15:41:41 -08001143 return ImmutableList.of(unicastFlows.build(), multicastFlows.build());
Charles Chan5b9df8d2016-03-28 22:21:40 -07001144 }
1145
Saurav Das822c4e22015-10-23 10:51:11 -07001146 private Collection<FlowRule> processForward(ForwardingObjective fwd) {
1147 switch (fwd.flag()) {
1148 case SPECIFIC:
1149 return processSpecific(fwd);
1150 case VERSATILE:
1151 return processVersatile(fwd);
Jonghwan Hyun800d9d02018-04-09 09:40:50 -07001152 case EGRESS:
1153 return processEgress(fwd);
Saurav Das822c4e22015-10-23 10:51:11 -07001154 default:
1155 fail(fwd, ObjectiveError.UNKNOWN);
1156 log.warn("Unknown forwarding flag {}", fwd.flag());
1157 }
1158 return Collections.emptySet();
1159 }
1160
1161 /**
Jonghwan Hyun800d9d02018-04-09 09:40:50 -07001162 * In the OF-DPA 2.0 pipeline, egress forwarding objectives go to the
1163 * egress tables.
1164 * @param fwd the forwarding objective of type 'egress'
1165 * @return a collection of flow rules to be sent to the switch. An empty
1166 * collection may be returned if there is a problem in processing
1167 * the flow rule
1168 */
1169 protected Collection<FlowRule> processEgress(ForwardingObjective fwd) {
1170 log.debug("Processing egress forwarding objective:{} in dev:{}",
1171 fwd, deviceId);
1172
1173 List<FlowRule> rules = new ArrayList<>();
1174
1175 // Build selector
1176 TrafficSelector.Builder sb = DefaultTrafficSelector.builder();
1177 VlanIdCriterion vlanIdCriterion = (VlanIdCriterion) fwd.selector().getCriterion(Criterion.Type.VLAN_VID);
1178 if (vlanIdCriterion == null) {
1179 log.error("Egress forwarding objective:{} must include vlanId", fwd.id());
1180 fail(fwd, ObjectiveError.BADPARAMS);
1181 return rules;
1182 }
1183
1184 Optional<Instruction> outInstr = fwd.treatment().allInstructions().stream()
1185 .filter(instruction -> instruction instanceof OutputInstruction).findFirst();
1186 if (!outInstr.isPresent()) {
1187 log.error("Egress forwarding objective:{} must include output port", fwd.id());
1188 fail(fwd, ObjectiveError.BADPARAMS);
1189 return rules;
1190 }
1191
1192 PortNumber portNumber = ((OutputInstruction) outInstr.get()).port();
1193
1194 sb.matchVlanId(vlanIdCriterion.vlanId());
1195 OfdpaMatchActsetOutput actsetOutput = new OfdpaMatchActsetOutput(portNumber);
1196 sb.extension(actsetOutput, deviceId);
1197
1198 sb.extension(new OfdpaMatchAllowVlanTranslation(ALLOW_VLAN_TRANSLATION), deviceId);
1199
1200 // Build a flow rule for Egress VLAN Flow table
1201 TrafficTreatment.Builder tb = DefaultTrafficTreatment.builder();
1202 tb.transition(EGRESS_DSCP_PCP_REMARK_FLOW_TABLE);
1203 if (fwd.treatment() != null) {
1204 for (Instruction instr : fwd.treatment().allInstructions()) {
1205 if (instr instanceof L2ModificationInstruction &&
1206 ((L2ModificationInstruction) instr).subtype() == L2SubType.VLAN_ID) {
1207 tb.immediate().add(instr);
1208 }
1209 if (instr instanceof L2ModificationInstruction &&
1210 ((L2ModificationInstruction) instr).subtype() == L2SubType.VLAN_PUSH) {
1211 tb.immediate().pushVlan();
1212 EthType ethType = ((L2ModificationInstruction.ModVlanHeaderInstruction) instr).ethernetType();
1213 if (ethType.equals(EtherType.QINQ.ethType())) {
1214 // Build a flow rule for Egress TPID Flow table
1215 TrafficSelector tpidSelector = DefaultTrafficSelector.builder()
1216 .extension(actsetOutput, deviceId)
1217 .matchVlanId(VlanId.ANY).build();
1218
1219 TrafficTreatment tpidTreatment = DefaultTrafficTreatment.builder()
1220 .extension(new Ofdpa3CopyField(COPY_FIELD_NBITS, COPY_FIELD_OFFSET,
1221 COPY_FIELD_OFFSET, OXM_ID_VLAN_VID,
1222 OXM_ID_PACKET_REG_1),
1223 deviceId)
1224 .popVlan()
1225 .pushVlan(EtherType.QINQ.ethType())
1226 .extension(new Ofdpa3CopyField(COPY_FIELD_NBITS, COPY_FIELD_OFFSET,
1227 COPY_FIELD_OFFSET, OXM_ID_PACKET_REG_1,
1228 OXM_ID_VLAN_VID),
1229 deviceId)
1230 .build();
1231
1232 FlowRule.Builder tpidRuleBuilder = DefaultFlowRule.builder()
1233 .fromApp(fwd.appId())
1234 .withPriority(fwd.priority())
1235 .forDevice(deviceId)
1236 .withSelector(tpidSelector)
1237 .withTreatment(tpidTreatment)
1238 .makePermanent()
1239 .forTable(EGRESS_TPID_FLOW_TABLE);
1240 rules.add(tpidRuleBuilder.build());
1241 }
1242 }
1243 }
1244 }
1245
1246 FlowRule.Builder ruleBuilder = DefaultFlowRule.builder()
1247 .fromApp(fwd.appId())
1248 .withPriority(fwd.priority())
1249 .forDevice(deviceId)
1250 .withSelector(sb.build())
1251 .withTreatment(tb.build())
1252 .makePermanent()
1253 .forTable(EGRESS_VLAN_FLOW_TABLE);
1254 rules.add(ruleBuilder.build());
1255 return rules;
1256 }
1257
1258 /**
Saurav Das822c4e22015-10-23 10:51:11 -07001259 * In the OF-DPA 2.0 pipeline, versatile forwarding objectives go to the
1260 * ACL table.
1261 * @param fwd the forwarding objective of type 'versatile'
1262 * @return a collection of flow rules to be sent to the switch. An empty
1263 * collection may be returned if there is a problem in processing
1264 * the flow rule
1265 */
Saurav Das52025962016-01-28 22:30:01 -08001266 protected Collection<FlowRule> processVersatile(ForwardingObjective fwd) {
Saurav Das018605f2017-02-18 14:05:44 -08001267 log.debug("Processing versatile forwarding objective:{} in dev:{}",
Saurav Dasd2fded02016-12-02 15:43:47 -08001268 fwd.id(), deviceId);
Harshada Chaundkarcb787512019-08-05 15:33:30 +00001269 List<FlowRule> flowRules = new ArrayList<>();
1270 final AtomicBoolean ethTypeUsed = new AtomicBoolean(false);
Saurav Das822c4e22015-10-23 10:51:11 -07001271
Saurav Das822c4e22015-10-23 10:51:11 -07001272 if (fwd.nextId() == null && fwd.treatment() == null) {
1273 log.error("Forwarding objective {} from {} must contain "
1274 + "nextId or Treatment", fwd.selector(), fwd.appId());
Zsolt Haraszti9faab752016-02-17 15:55:20 -08001275 fail(fwd, ObjectiveError.BADPARAMS);
Saurav Das822c4e22015-10-23 10:51:11 -07001276 return Collections.emptySet();
1277 }
Saurav Das49cb5a12016-01-16 22:54:07 -08001278
Saurav Das77b5e902016-01-27 17:01:59 -08001279 TrafficSelector.Builder sbuilder = DefaultTrafficSelector.builder();
1280 fwd.selector().criteria().forEach(criterion -> {
1281 if (criterion instanceof VlanIdCriterion) {
1282 VlanId vlanId = ((VlanIdCriterion) criterion).vlanId();
1283 // ensure that match does not include vlan = NONE as OF-DPA does not
1284 // match untagged packets this way in the ACL table.
1285 if (vlanId.equals(VlanId.NONE)) {
1286 return;
1287 }
Charles Chand1172632017-03-15 17:33:09 -07001288 if (requireVlanExtensions()) {
1289 OfdpaMatchVlanVid ofdpaMatchVlanVid = new OfdpaMatchVlanVid(vlanId);
1290 sbuilder.extension(ofdpaMatchVlanVid, deviceId);
1291 } else {
1292 sbuilder.matchVlanId(vlanId);
1293 }
Charles Chan09bf2692018-01-11 11:48:18 -08001294 } else if (criterion instanceof Icmpv6TypeCriterion) {
1295 byte icmpv6Type = (byte) ((Icmpv6TypeCriterion) criterion).icmpv6Type();
1296 sbuilder.matchIcmpv6Type(icmpv6Type);
1297 } else if (criterion instanceof Icmpv6CodeCriterion) {
1298 byte icmpv6Code = (byte) ((Icmpv6CodeCriterion) criterion).icmpv6Code();
1299 sbuilder.matchIcmpv6Type(icmpv6Code);
Charles Chand9e47c62017-10-05 15:17:15 -07001300 } else if (criterion instanceof TcpPortCriterion || criterion instanceof UdpPortCriterion) {
1301 // FIXME: QMX switches do not support L4 dst port matching in ACL table.
1302 // Currently L4 dst port matching is only used by DHCP relay feature
1303 // and therefore is safe to be replaced with L4 src port matching.
1304 // We need to revisit this if L4 dst port is used for other purpose in the future.
1305 if (!supportIpv6L4Dst() && isIpv6(fwd.selector())) {
Charles Chand9e47c62017-10-05 15:17:15 -07001306 switch (criterion.type()) {
1307 case UDP_DST:
Charles Chand9e47c62017-10-05 15:17:15 -07001308 case UDP_DST_MASKED:
Charles Chand9e47c62017-10-05 15:17:15 -07001309 case TCP_DST:
Charles Chand9e47c62017-10-05 15:17:15 -07001310 case TCP_DST_MASKED:
Charles Chand9e47c62017-10-05 15:17:15 -07001311 break;
1312 default:
1313 sbuilder.add(criterion);
1314 }
1315 } else {
1316 sbuilder.add(criterion);
1317 }
Harshada Chaundkarcb787512019-08-05 15:33:30 +00001318 } else if (criterion instanceof EthTypeCriterion) {
1319 sbuilder.add(criterion);
1320 ethTypeUsed.set(true);
Saurav Das77b5e902016-01-27 17:01:59 -08001321 } else {
1322 sbuilder.add(criterion);
1323 }
1324 });
Harshada Chaundkarcb787512019-08-05 15:33:30 +00001325
1326 TrafficTreatment.Builder ttBuilder = versatileTreatmentBuilder(fwd);
1327 if (ttBuilder == null) {
1328 return Collections.emptySet();
1329 }
1330
1331 FlowRule.Builder ruleBuilder = DefaultFlowRule.builder()
1332 .fromApp(fwd.appId())
1333 .withPriority(fwd.priority())
1334 .forDevice(deviceId)
1335 .withSelector(sbuilder.build())
1336 .withTreatment(ttBuilder.build())
1337 .makePermanent()
1338 .forTable(ACL_TABLE);
1339
1340 flowRules.add(ruleBuilder.build());
1341
1342 if (!ethTypeUsed.get() && requireEthType()) {
1343 log.debug("{} doesn't match on ethType but requireEthType is true, adding complementary ACL flow.",
1344 sbuilder.toString());
1345 sbuilder.matchEthType(Ethernet.TYPE_IPV6);
1346 FlowRule.Builder ethTypeRuleBuilder = DefaultFlowRule.builder()
1347 .fromApp(fwd.appId())
1348 .withPriority(fwd.priority())
1349 .forDevice(deviceId)
1350 .withSelector(sbuilder.build())
1351 .withTreatment(ttBuilder.build())
1352 .makePermanent()
1353 .forTable(ACL_TABLE);
1354 flowRules.add(ethTypeRuleBuilder.build());
1355 }
1356 return flowRules;
Charles Chanab591602019-01-22 17:25:04 -08001357 }
Saurav Das77b5e902016-01-27 17:01:59 -08001358
Charles Chanab591602019-01-22 17:25:04 -08001359 /**
1360 * Helper function to create traffic treatment builder for versatile forwarding objectives.
1361 *
1362 * @param fwd original forwarding objective
1363 * @return treatment builder for the flow rule, or null if there is an error.
1364 */
1365 protected TrafficTreatment.Builder versatileTreatmentBuilder(ForwardingObjective fwd) {
Saurav Das822c4e22015-10-23 10:51:11 -07001366 // XXX driver does not currently do type checking as per Tables 65-67 in
1367 // OFDPA 2.0 spec. The only allowed treatment is a punt to the controller.
Saurav Das49cb5a12016-01-16 22:54:07 -08001368 TrafficTreatment.Builder ttBuilder = DefaultTrafficTreatment.builder();
1369 if (fwd.treatment() != null) {
1370 for (Instruction ins : fwd.treatment().allInstructions()) {
1371 if (ins instanceof OutputInstruction) {
1372 OutputInstruction o = (OutputInstruction) ins;
Ray Milkeyfe0e0852018-01-18 11:14:05 -08001373 if (PortNumber.CONTROLLER.equals(o.port())) {
Saurav Das49cb5a12016-01-16 22:54:07 -08001374 ttBuilder.add(o);
1375 } else {
1376 log.warn("Only allowed treatments in versatile forwarding "
1377 + "objectives are punts to the controller");
1378 }
Charles Chanf76de302018-06-15 18:54:18 -07001379 } else if (ins instanceof NoActionInstruction) {
1380 // No action is allowed and nothing needs to be done
Saurav Das49cb5a12016-01-16 22:54:07 -08001381 } else {
1382 log.warn("Cannot process instruction in versatile fwd {}", ins);
1383 }
Saurav Das822c4e22015-10-23 10:51:11 -07001384 }
Charles Chan2df0e8a2017-01-09 11:45:08 -08001385 if (fwd.treatment().clearedDeferred()) {
1386 ttBuilder.wipeDeferred();
1387 }
Saurav Das822c4e22015-10-23 10:51:11 -07001388 }
Saurav Das822c4e22015-10-23 10:51:11 -07001389 if (fwd.nextId() != null) {
Charles Chanab591602019-01-22 17:25:04 -08001390 // Override case
Saurav Das49cb5a12016-01-16 22:54:07 -08001391 NextGroup next = getGroupForNextObjective(fwd.nextId());
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +05301392 if (next == null) {
1393 fail(fwd, ObjectiveError.BADPARAMS);
Charles Chanab591602019-01-22 17:25:04 -08001394 return null;
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +05301395 }
Saurav Das49cb5a12016-01-16 22:54:07 -08001396 List<Deque<GroupKey>> gkeys = appKryo.deserialize(next.data());
1397 // we only need the top level group's key to point the flow to it
1398 Group group = groupService.getGroup(deviceId, gkeys.get(0).peekFirst());
1399 if (group == null) {
1400 log.warn("Group with key:{} for next-id:{} not found in dev:{}",
Charles Chanab591602019-01-22 17:25:04 -08001401 gkeys.get(0).peekFirst(), fwd.nextId(), deviceId);
Saurav Das49cb5a12016-01-16 22:54:07 -08001402 fail(fwd, ObjectiveError.GROUPMISSING);
Charles Chanab591602019-01-22 17:25:04 -08001403 return null;
Saurav Das49cb5a12016-01-16 22:54:07 -08001404 }
1405 ttBuilder.deferred().group(group.id());
Saurav Das822c4e22015-10-23 10:51:11 -07001406 }
Charles Chanab591602019-01-22 17:25:04 -08001407 return ttBuilder;
Saurav Das822c4e22015-10-23 10:51:11 -07001408 }
1409
1410 /**
1411 * In the OF-DPA 2.0 pipeline, specific forwarding refers to the IP table
Saurav Das8a0732e2015-11-20 15:27:53 -08001412 * (unicast or multicast) or the L2 table (mac + vlan) or the MPLS table.
Saurav Das822c4e22015-10-23 10:51:11 -07001413 *
1414 * @param fwd the forwarding objective of type 'specific'
1415 * @return a collection of flow rules. Typically there will be only one
1416 * for this type of forwarding objective. An empty set may be
1417 * returned if there is an issue in processing the objective.
1418 */
Charles Chan50d900c2018-03-02 13:26:22 -08001419 private Collection<FlowRule> processSpecific(ForwardingObjective fwd) {
Saurav Das25190812016-05-27 13:54:07 -07001420 log.debug("Processing specific fwd objective:{} in dev:{} with next:{}",
Saurav Das4ce45962015-11-24 23:21:05 -08001421 fwd.id(), deviceId, fwd.nextId());
1422 boolean isEthTypeObj = isSupportedEthTypeObjective(fwd);
1423 boolean isEthDstObj = isSupportedEthDstObjective(fwd);
1424
1425 if (isEthTypeObj) {
1426 return processEthTypeSpecific(fwd);
1427 } else if (isEthDstObj) {
1428 return processEthDstSpecific(fwd);
1429 } else {
1430 log.warn("processSpecific: Unsupported forwarding objective "
1431 + "criteria fwd:{} in dev:{}", fwd.nextId(), deviceId);
Saurav Das822c4e22015-10-23 10:51:11 -07001432 fail(fwd, ObjectiveError.UNSUPPORTED);
1433 return Collections.emptySet();
1434 }
Saurav Das4ce45962015-11-24 23:21:05 -08001435 }
1436
Saurav Das4ce45962015-11-24 23:21:05 -08001437 /**
1438 * Handles forwarding rules to the IP and MPLS tables.
1439 *
1440 * @param fwd the forwarding objective
1441 * @return A collection of flow rules, or an empty set
1442 */
1443 protected Collection<FlowRule> processEthTypeSpecific(ForwardingObjective fwd) {
Charles Chancad338a2016-09-16 18:03:11 -07001444 return processEthTypeSpecificInternal(fwd, false, ACL_TABLE);
Charles Chanf9e98652016-09-07 16:54:23 -07001445 }
1446
1447 /**
1448 * Internal implementation of processEthTypeSpecific.
1449 * <p>
1450 * Wildcarded IPv4_DST is not supported in OFDPA i12. Therefore, we break
1451 * the rule into 0.0.0.0/1 and 128.0.0.0/1.
1452 * The allowDefaultRoute must be set to false for OFDPA i12.
1453 * </p>
1454 *
1455 * @param fwd the forwarding objective
1456 * @param allowDefaultRoute allow wildcarded IPv4_DST or not
Ray Milkey0bb1e102016-11-10 14:51:27 -08001457 * @param mplsNextTable next MPLS table
Charles Chanf9e98652016-09-07 16:54:23 -07001458 * @return A collection of flow rules, or an empty set
1459 */
1460 protected Collection<FlowRule> processEthTypeSpecificInternal(ForwardingObjective fwd,
Charles Chancad338a2016-09-16 18:03:11 -07001461 boolean allowDefaultRoute,
1462 int mplsNextTable) {
Saurav Das4ce45962015-11-24 23:21:05 -08001463 TrafficSelector selector = fwd.selector();
1464 EthTypeCriterion ethType =
1465 (EthTypeCriterion) selector.getCriterion(Criterion.Type.ETH_TYPE);
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001466 boolean emptyGroup = false;
Charles Chan188ebf52015-12-23 00:15:11 -08001467 int forTableId;
Saurav Das8a0732e2015-11-20 15:27:53 -08001468 TrafficSelector.Builder filteredSelector = DefaultTrafficSelector.builder();
Charles Chan14967c22015-12-07 11:11:50 -08001469 TrafficTreatment.Builder tb = DefaultTrafficTreatment.builder();
Flavio Castroe10fa242016-01-15 12:43:51 -08001470 TrafficSelector.Builder complementarySelector = DefaultTrafficSelector.builder();
Charles Chan14967c22015-12-07 11:11:50 -08001471
Saurav Das8a0732e2015-11-20 15:27:53 -08001472 if (ethType.ethType().toShort() == Ethernet.TYPE_IPV4) {
Pier Luigi3bfe32c2017-01-30 09:47:36 -08001473 if (buildIpv4Selector(filteredSelector, complementarySelector, fwd, allowDefaultRoute) < 0) {
1474 return Collections.emptyList();
1475 }
1476 // We need to set properly the next table
Flavio Castroe10fa242016-01-15 12:43:51 -08001477 IpPrefix ipv4Dst = ((IPCriterion) selector.getCriterion(Criterion.Type.IPV4_DST)).ip();
Charles Chan5b9df8d2016-03-28 22:21:40 -07001478 if (ipv4Dst.isMulticast()) {
Charles Chan5b9df8d2016-03-28 22:21:40 -07001479 forTableId = MULTICAST_ROUTING_TABLE;
Flavio Castroe10fa242016-01-15 12:43:51 -08001480 } else {
Charles Chan5b9df8d2016-03-28 22:21:40 -07001481 forTableId = UNICAST_ROUTING_TABLE;
Flavio Castroe10fa242016-01-15 12:43:51 -08001482 }
Charles Chan50d900c2018-03-02 13:26:22 -08001483
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 // }
1494
Pier Ventree0ae7a32016-11-23 09:57:42 -08001495 } else if (ethType.ethType().toShort() == Ethernet.TYPE_IPV6) {
1496 if (buildIpv6Selector(filteredSelector, fwd) < 0) {
1497 return Collections.emptyList();
1498 }
Kalhee Kim3d26bf02017-10-04 14:59:11 +00001499 //We need to set the proper next table
1500 IpPrefix ipv6Dst = ((IPCriterion) selector.getCriterion(Criterion.Type.IPV6_DST)).ip();
1501 if (ipv6Dst.isMulticast()) {
1502 forTableId = MULTICAST_ROUTING_TABLE;
1503 } else {
1504 forTableId = UNICAST_ROUTING_TABLE;
1505 }
1506
Charles Chan236653d2018-03-05 11:28:23 -08001507 // TODO decrementing IP ttl is done automatically for routing, this
Charles Chan50d900c2018-03-02 13:26:22 -08001508 // action is ignored or rejected in ofdpa as it is not fully implemented
Charles Chan236653d2018-03-05 11:28:23 -08001509 // if (fwd.treatment() != null) {
1510 // for (Instruction instr : fwd.treatment().allInstructions()) {
1511 // if (instr instanceof L3ModificationInstruction &&
1512 // ((L3ModificationInstruction) instr).subtype() == L3SubType.DEC_TTL) {
1513 // tb.deferred().add(instr);
1514 // }
1515 // }
1516 // }
Saurav Das8a0732e2015-11-20 15:27:53 -08001517 } else {
1518 filteredSelector
1519 .matchEthType(Ethernet.MPLS_UNICAST)
1520 .matchMplsLabel(((MplsCriterion)
1521 selector.getCriterion(Criterion.Type.MPLS_LABEL)).label());
1522 MplsBosCriterion bos = (MplsBosCriterion) selector
Pier Ventre140a8942016-11-02 07:26:38 -07001523 .getCriterion(MPLS_BOS);
Alex Yashchuk4caa8e82017-12-08 17:40:05 +02001524 if (bos != null && requireMplsBosMatch()) {
Saurav Das8a0732e2015-11-20 15:27:53 -08001525 filteredSelector.matchMplsBos(bos.mplsBos());
1526 }
1527 forTableId = MPLS_TABLE_1;
Saurav Das4ce45962015-11-24 23:21:05 -08001528 log.debug("processing MPLS specific forwarding objective {} -> next:{}"
1529 + " in dev {}", fwd.id(), fwd.nextId(), deviceId);
Saurav Das822c4e22015-10-23 10:51:11 -07001530
Charles Chan14967c22015-12-07 11:11:50 -08001531 if (fwd.treatment() != null) {
1532 for (Instruction instr : fwd.treatment().allInstructions()) {
1533 if (instr instanceof L2ModificationInstruction &&
1534 ((L2ModificationInstruction) instr).subtype() == L2SubType.MPLS_POP) {
Pier Luigi3bfe32c2017-01-30 09:47:36 -08001535 // OF-DPA does not pop in MPLS table in some cases. For the L3 VPN, it requires
Saurav Das9c705342017-01-06 11:36:01 -08001536 // setting the MPLS_TYPE so pop can happen down the pipeline
Alex Yashchuk4caa8e82017-12-08 17:40:05 +02001537 if (requireMplsPop()) {
1538 if (mplsNextTable == MPLS_TYPE_TABLE && isNotMplsBos(selector)) {
1539 tb.immediate().popMpls();
1540 }
1541 } else {
1542 // Skip mpls pop action for mpls_unicast label
1543 if (instr instanceof ModMplsHeaderInstruction &&
1544 !((ModMplsHeaderInstruction) instr).ethernetType()
1545 .equals(EtherType.MPLS_UNICAST.ethType())) {
1546 tb.immediate().add(instr);
1547 }
Pier Luigi3bfe32c2017-01-30 09:47:36 -08001548 }
Charles Chan14967c22015-12-07 11:11:50 -08001549 }
Alex Yashchuk4caa8e82017-12-08 17:40:05 +02001550
1551 if (requireMplsTtlModification()) {
1552 if (instr instanceof L3ModificationInstruction &&
1553 ((L3ModificationInstruction) instr).subtype() == L3SubType.DEC_TTL) {
1554 // FIXME Should modify the app to send the correct DEC_MPLS_TTL instruction
1555 tb.immediate().decMplsTtl();
1556 }
1557 if (instr instanceof L3ModificationInstruction &&
1558 ((L3ModificationInstruction) instr).subtype() == L3SubType.TTL_IN) {
1559 tb.immediate().add(instr);
1560 }
Charles Chan14967c22015-12-07 11:11:50 -08001561 }
Saurav Das8a0732e2015-11-20 15:27:53 -08001562 }
1563 }
1564 }
Saurav Das822c4e22015-10-23 10:51:11 -07001565
1566 if (fwd.nextId() != null) {
Saurav Das423fe2b2015-12-04 10:52:59 -08001567 NextGroup next = getGroupForNextObjective(fwd.nextId());
1568 if (next != null) {
1569 List<Deque<GroupKey>> gkeys = appKryo.deserialize(next.data());
1570 // we only need the top level group's key to point the flow to it
1571 Group group = groupService.getGroup(deviceId, gkeys.get(0).peekFirst());
1572 if (group == null) {
1573 log.warn("Group with key:{} for next-id:{} not found in dev:{}",
1574 gkeys.get(0).peekFirst(), fwd.nextId(), deviceId);
1575 fail(fwd, ObjectiveError.GROUPMISSING);
1576 return Collections.emptySet();
1577 }
Saurav Dasa4020382018-02-14 14:14:54 -08001578 if (isNotMplsBos(selector) && group.type().equals(SELECT)) {
1579 log.warn("SR CONTINUE case cannot be handled as MPLS ECMP "
1580 + "is not implemented in OF-DPA yet. Aborting flow {} -> next:{} "
1581 + "in this device {}", fwd.id(), fwd.nextId(), deviceId);
1582 fail(fwd, ObjectiveError.FLOWINSTALLATIONFAILED);
1583 return Collections.emptySet();
1584 }
Saurav Das423fe2b2015-12-04 10:52:59 -08001585 tb.deferred().group(group.id());
Saurav Dasc568c342018-01-25 09:49:01 -08001586 // retrying flows may be necessary due to bug CORD-554
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001587 if (gkeys.size() == 1 && gkeys.get(0).size() == 1) {
Saurav Dasc568c342018-01-25 09:49:01 -08001588 if (shouldRetry()) {
1589 log.warn("Found empty group 0x{} in dev:{} .. will retry fwd:{}",
1590 Integer.toHexString(group.id().id()), deviceId,
1591 fwd.id());
1592 emptyGroup = true;
1593 }
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001594 }
Saurav Das25190812016-05-27 13:54:07 -07001595 } else {
1596 log.warn("Cannot find group for nextId:{} in dev:{}. Aborting fwd:{}",
1597 fwd.nextId(), deviceId, fwd.id());
1598 fail(fwd, ObjectiveError.FLOWINSTALLATIONFAILED);
1599 return Collections.emptySet();
Saurav Das822c4e22015-10-23 10:51:11 -07001600 }
Saurav Das822c4e22015-10-23 10:51:11 -07001601 }
Charles Chancad338a2016-09-16 18:03:11 -07001602
1603 if (forTableId == MPLS_TABLE_1) {
Pier Ventre140a8942016-11-02 07:26:38 -07001604 if (mplsNextTable == MPLS_L3_TYPE_TABLE) {
Charles Chancad338a2016-09-16 18:03:11 -07001605 Ofdpa3SetMplsType setMplsType = new Ofdpa3SetMplsType(Ofdpa3MplsType.L3_PHP);
Saurav Das9c705342017-01-06 11:36:01 -08001606 // set mpls type as apply_action
1607 tb.immediate().extension(setMplsType, deviceId);
Charles Chancad338a2016-09-16 18:03:11 -07001608 }
1609 tb.transition(mplsNextTable);
1610 } else {
1611 tb.transition(ACL_TABLE);
1612 }
1613
Andrea Campanellad980c6d2018-04-30 11:48:55 +02001614 if (fwd.treatment() != null && fwd.treatment().clearedDeferred()) {
1615 if (supportsUnicastBlackHole()) {
1616 tb.wipeDeferred();
1617 } else {
1618 log.warn("Clear Deferred is not supported Unicast Routing Table on device {}", deviceId);
1619 return Collections.emptySet();
1620 }
1621 }
1622
Saurav Das822c4e22015-10-23 10:51:11 -07001623 FlowRule.Builder ruleBuilder = DefaultFlowRule.builder()
1624 .fromApp(fwd.appId())
1625 .withPriority(fwd.priority())
1626 .forDevice(deviceId)
Saurav Das8a0732e2015-11-20 15:27:53 -08001627 .withSelector(filteredSelector.build())
1628 .withTreatment(tb.build())
1629 .forTable(forTableId);
Saurav Das822c4e22015-10-23 10:51:11 -07001630
1631 if (fwd.permanent()) {
1632 ruleBuilder.makePermanent();
1633 } else {
1634 ruleBuilder.makeTemporary(fwd.timeout());
1635 }
Flavio Castroe10fa242016-01-15 12:43:51 -08001636 Collection<FlowRule> flowRuleCollection = new ArrayList<>();
1637 flowRuleCollection.add(ruleBuilder.build());
Pier Luigi3bfe32c2017-01-30 09:47:36 -08001638 if (!allowDefaultRoute) {
Pier Ventree0ae7a32016-11-23 09:57:42 -08001639 flowRuleCollection.add(
1640 defaultRoute(fwd, complementarySelector, forTableId, tb)
1641 );
Flavio Castroe10fa242016-01-15 12:43:51 -08001642 log.debug("Default rule 0.0.0.0/0 is being installed two rules");
1643 }
Saurav Dasc568c342018-01-25 09:49:01 -08001644
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001645 if (emptyGroup) {
pier8b3aef42019-03-11 15:14:02 -07001646 retryExecutorService.schedule(new RetryFlows(fwd, flowRuleCollection),
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001647 RETRY_MS, TimeUnit.MILLISECONDS);
1648 }
Flavio Castroe10fa242016-01-15 12:43:51 -08001649 return flowRuleCollection;
Saurav Das822c4e22015-10-23 10:51:11 -07001650 }
1651
Charles Chan50d900c2018-03-02 13:26:22 -08001652 private int buildIpv4Selector(TrafficSelector.Builder builderToUpdate,
Pier Luigi3bfe32c2017-01-30 09:47:36 -08001653 TrafficSelector.Builder extBuilder,
1654 ForwardingObjective fwd,
1655 boolean allowDefaultRoute) {
1656 TrafficSelector selector = fwd.selector();
1657
1658 IpPrefix ipv4Dst = ((IPCriterion) selector.getCriterion(Criterion.Type.IPV4_DST)).ip();
1659 if (ipv4Dst.isMulticast()) {
1660 if (ipv4Dst.prefixLength() != 32) {
1661 log.warn("Multicast specific forwarding objective can only be /32");
1662 fail(fwd, ObjectiveError.BADPARAMS);
1663 return -1;
1664 }
1665 VlanId assignedVlan = readVlanFromSelector(fwd.meta());
1666 if (assignedVlan == null) {
1667 log.warn("VLAN ID required by multicast specific fwd obj is missing. Abort.");
1668 fail(fwd, ObjectiveError.BADPARAMS);
1669 return -1;
1670 }
Charles Chand1172632017-03-15 17:33:09 -07001671 if (requireVlanExtensions()) {
1672 OfdpaMatchVlanVid ofdpaMatchVlanVid = new OfdpaMatchVlanVid(assignedVlan);
1673 builderToUpdate.extension(ofdpaMatchVlanVid, deviceId);
1674 } else {
1675 builderToUpdate.matchVlanId(assignedVlan);
1676 }
Pier Luigi3bfe32c2017-01-30 09:47:36 -08001677 builderToUpdate.matchEthType(Ethernet.TYPE_IPV4).matchIPDst(ipv4Dst);
1678 log.debug("processing IPv4 multicast specific forwarding objective {} -> next:{}"
1679 + " in dev:{}", fwd.id(), fwd.nextId(), deviceId);
1680 } else {
1681 if (ipv4Dst.prefixLength() == 0) {
1682 if (allowDefaultRoute) {
1683 // The entire IPV4_DST field is wildcarded intentionally
1684 builderToUpdate.matchEthType(Ethernet.TYPE_IPV4);
1685 } else {
1686 // NOTE: The switch does not support matching 0.0.0.0/0
1687 // Split it into 0.0.0.0/1 and 128.0.0.0/1
1688 builderToUpdate.matchEthType(Ethernet.TYPE_IPV4)
1689 .matchIPDst(IpPrefix.valueOf("0.0.0.0/1"));
1690 extBuilder.matchEthType(Ethernet.TYPE_IPV4)
1691 .matchIPDst(IpPrefix.valueOf("128.0.0.0/1"));
1692 }
1693 } else {
1694 builderToUpdate.matchEthType(Ethernet.TYPE_IPV4).matchIPDst(ipv4Dst);
1695 }
1696 log.debug("processing IPv4 unicast specific forwarding objective {} -> next:{}"
1697 + " in dev:{}", fwd.id(), fwd.nextId(), deviceId);
1698 }
1699 return 0;
1700 }
1701
1702 /**
1703 * Helper method to build Ipv6 selector using the selector provided by
1704 * a forwarding objective.
1705 *
1706 * @param builderToUpdate the builder to update
1707 * @param fwd the selector to read
1708 * @return 0 if the update ends correctly. -1 if the matches
1709 * are not yet supported
1710 */
Charles Chan50d900c2018-03-02 13:26:22 -08001711 int buildIpv6Selector(TrafficSelector.Builder builderToUpdate,
Pier Luigi3bfe32c2017-01-30 09:47:36 -08001712 ForwardingObjective fwd) {
1713
1714 TrafficSelector selector = fwd.selector();
1715
1716 IpPrefix ipv6Dst = ((IPCriterion) selector.getCriterion(Criterion.Type.IPV6_DST)).ip();
1717 if (ipv6Dst.isMulticast()) {
Julia Ferguson65428c32017-08-10 18:15:24 +00001718 if (ipv6Dst.prefixLength() != IpAddress.INET6_BIT_LENGTH) {
1719 log.warn("Multicast specific forwarding objective can only be /128");
1720 fail(fwd, ObjectiveError.BADPARAMS);
1721 return -1;
1722 }
1723 VlanId assignedVlan = readVlanFromSelector(fwd.meta());
1724 if (assignedVlan == null) {
1725 log.warn("VLAN ID required by multicast specific fwd obj is missing. Abort.");
1726 fail(fwd, ObjectiveError.BADPARAMS);
1727 return -1;
1728 }
1729 if (requireVlanExtensions()) {
1730 OfdpaMatchVlanVid ofdpaMatchVlanVid = new OfdpaMatchVlanVid(assignedVlan);
1731 builderToUpdate.extension(ofdpaMatchVlanVid, deviceId);
1732 } else {
1733 builderToUpdate.matchVlanId(assignedVlan);
1734 }
1735 builderToUpdate.matchEthType(Ethernet.TYPE_IPV6).matchIPv6Dst(ipv6Dst);
1736 log.debug("processing IPv6 multicast specific forwarding objective {} -> next:{}"
1737 + " in dev:{}", fwd.id(), fwd.nextId(), deviceId);
1738 } else {
1739 if (ipv6Dst.prefixLength() != 0) {
1740 builderToUpdate.matchIPv6Dst(ipv6Dst);
1741 }
Pier Luigi3bfe32c2017-01-30 09:47:36 -08001742 builderToUpdate.matchEthType(Ethernet.TYPE_IPV6);
1743 log.debug("processing IPv6 unicast specific forwarding objective {} -> next:{}"
1744 + " in dev:{}", fwd.id(), fwd.nextId(), deviceId);
Julia Ferguson65428c32017-08-10 18:15:24 +00001745 }
Pier Luigi3bfe32c2017-01-30 09:47:36 -08001746 return 0;
1747 }
1748
Charles Chan50d900c2018-03-02 13:26:22 -08001749 FlowRule defaultRoute(ForwardingObjective fwd,
Pier Ventree0ae7a32016-11-23 09:57:42 -08001750 TrafficSelector.Builder complementarySelector,
1751 int forTableId,
1752 TrafficTreatment.Builder tb) {
1753 FlowRule.Builder rule = DefaultFlowRule.builder()
1754 .fromApp(fwd.appId())
1755 .withPriority(fwd.priority())
1756 .forDevice(deviceId)
1757 .withSelector(complementarySelector.build())
1758 .withTreatment(tb.build())
1759 .forTable(forTableId);
1760 if (fwd.permanent()) {
1761 rule.makePermanent();
1762 } else {
1763 rule.makeTemporary(fwd.timeout());
1764 }
1765 return rule.build();
1766 }
1767
Saurav Das4ce45962015-11-24 23:21:05 -08001768 /**
1769 * Handles forwarding rules to the L2 bridging table. Flow actions are not
1770 * allowed in the bridging table - instead we use L2 Interface group or
1771 * L2 flood group
1772 *
1773 * @param fwd the forwarding objective
1774 * @return A collection of flow rules, or an empty set
1775 */
1776 protected Collection<FlowRule> processEthDstSpecific(ForwardingObjective fwd) {
1777 List<FlowRule> rules = new ArrayList<>();
1778
1779 // Build filtered selector
1780 TrafficSelector selector = fwd.selector();
1781 EthCriterion ethCriterion = (EthCriterion) selector
1782 .getCriterion(Criterion.Type.ETH_DST);
1783 VlanIdCriterion vlanIdCriterion = (VlanIdCriterion) selector
1784 .getCriterion(Criterion.Type.VLAN_VID);
1785
1786 if (vlanIdCriterion == null) {
1787 log.warn("Forwarding objective for bridging requires vlan. Not "
1788 + "installing fwd:{} in dev:{}", fwd.id(), deviceId);
1789 fail(fwd, ObjectiveError.BADPARAMS);
1790 return Collections.emptySet();
1791 }
1792
1793 TrafficSelector.Builder filteredSelectorBuilder =
1794 DefaultTrafficSelector.builder();
Yi Tseng3a77b4f2017-02-06 15:02:17 -08001795
1796 if (!ethCriterion.mac().equals(NONE) &&
1797 !ethCriterion.mac().equals(BROADCAST)) {
Saurav Das4ce45962015-11-24 23:21:05 -08001798 filteredSelectorBuilder.matchEthDst(ethCriterion.mac());
1799 log.debug("processing L2 forwarding objective:{} -> next:{} in dev:{}",
1800 fwd.id(), fwd.nextId(), deviceId);
1801 } else {
Yi Tseng3a77b4f2017-02-06 15:02:17 -08001802 // Use wildcard DST_MAC if the MacAddress is None or Broadcast
Saurav Das4ce45962015-11-24 23:21:05 -08001803 log.debug("processing L2 Broadcast forwarding objective:{} -> next:{} "
1804 + "in dev:{} for vlan:{}",
1805 fwd.id(), fwd.nextId(), deviceId, vlanIdCriterion.vlanId());
1806 }
Charles Chand1172632017-03-15 17:33:09 -07001807 if (requireVlanExtensions()) {
1808 OfdpaMatchVlanVid ofdpaMatchVlanVid = new OfdpaMatchVlanVid(vlanIdCriterion.vlanId());
1809 filteredSelectorBuilder.extension(ofdpaMatchVlanVid, deviceId);
1810 } else {
1811 filteredSelectorBuilder.matchVlanId(vlanIdCriterion.vlanId());
1812 }
Saurav Das4ce45962015-11-24 23:21:05 -08001813 TrafficSelector filteredSelector = filteredSelectorBuilder.build();
1814
1815 if (fwd.treatment() != null) {
1816 log.warn("Ignoring traffic treatment in fwd rule {} meant for L2 table"
1817 + "for dev:{}. Expecting only nextId", fwd.id(), deviceId);
1818 }
1819
1820 TrafficTreatment.Builder treatmentBuilder = DefaultTrafficTreatment.builder();
1821 if (fwd.nextId() != null) {
Saurav Das423fe2b2015-12-04 10:52:59 -08001822 NextGroup next = getGroupForNextObjective(fwd.nextId());
Saurav Das4ce45962015-11-24 23:21:05 -08001823 if (next != null) {
1824 List<Deque<GroupKey>> gkeys = appKryo.deserialize(next.data());
1825 // we only need the top level group's key to point the flow to it
1826 Group group = groupService.getGroup(deviceId, gkeys.get(0).peekFirst());
1827 if (group != null) {
1828 treatmentBuilder.deferred().group(group.id());
1829 } else {
1830 log.warn("Group with key:{} for next-id:{} not found in dev:{}",
1831 gkeys.get(0).peekFirst(), fwd.nextId(), deviceId);
1832 fail(fwd, ObjectiveError.GROUPMISSING);
1833 return Collections.emptySet();
1834 }
1835 }
1836 }
1837 treatmentBuilder.immediate().transition(ACL_TABLE);
1838 TrafficTreatment filteredTreatment = treatmentBuilder.build();
1839
1840 // Build bridging table entries
1841 FlowRule.Builder flowRuleBuilder = DefaultFlowRule.builder();
1842 flowRuleBuilder.fromApp(fwd.appId())
1843 .withPriority(fwd.priority())
1844 .forDevice(deviceId)
1845 .withSelector(filteredSelector)
1846 .withTreatment(filteredTreatment)
1847 .forTable(BRIDGING_TABLE);
1848 if (fwd.permanent()) {
1849 flowRuleBuilder.makePermanent();
1850 } else {
1851 flowRuleBuilder.makeTemporary(fwd.timeout());
1852 }
1853 rules.add(flowRuleBuilder.build());
1854 return rules;
1855 }
1856
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001857 //////////////////////////////////////
1858 // Helper Methods and Classes
1859 //////////////////////////////////////
1860
1861 private boolean isSupportedEthTypeObjective(ForwardingObjective fwd) {
1862 TrafficSelector selector = fwd.selector();
1863 EthTypeCriterion ethType = (EthTypeCriterion) selector
1864 .getCriterion(Criterion.Type.ETH_TYPE);
1865 return !((ethType == null) ||
1866 ((ethType.ethType().toShort() != Ethernet.TYPE_IPV4) &&
Pier Ventree0ae7a32016-11-23 09:57:42 -08001867 (ethType.ethType().toShort() != Ethernet.MPLS_UNICAST)) &&
1868 (ethType.ethType().toShort() != Ethernet.TYPE_IPV6));
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001869 }
1870
1871 private boolean isSupportedEthDstObjective(ForwardingObjective fwd) {
1872 TrafficSelector selector = fwd.selector();
1873 EthCriterion ethDst = (EthCriterion) selector
1874 .getCriterion(Criterion.Type.ETH_DST);
1875 VlanIdCriterion vlanId = (VlanIdCriterion) selector
1876 .getCriterion(Criterion.Type.VLAN_VID);
1877 return !(ethDst == null && vlanId == null);
1878 }
1879
Charles Chan50d900c2018-03-02 13:26:22 -08001880 NextGroup getGroupForNextObjective(Integer nextId) {
Saurav Das423fe2b2015-12-04 10:52:59 -08001881 NextGroup next = flowObjectiveStore.getNextGroup(nextId);
1882 if (next != null) {
1883 List<Deque<GroupKey>> gkeys = appKryo.deserialize(next.data());
1884 if (gkeys != null && !gkeys.isEmpty()) {
1885 return next;
1886 } else {
1887 log.warn("Empty next group found in FlowObjective store for "
1888 + "next-id:{} in dev:{}", nextId, deviceId);
1889 }
1890 } else {
1891 log.warn("next-id {} not found in Flow objective store for dev:{}",
1892 nextId, deviceId);
1893 }
1894 return null;
1895 }
1896
Saurav Das24431192016-03-07 19:13:00 -08001897 @Override
1898 public List<String> getNextMappings(NextGroup nextGroup) {
1899 List<String> mappings = new ArrayList<>();
1900 List<Deque<GroupKey>> gkeys = appKryo.deserialize(nextGroup.data());
1901 for (Deque<GroupKey> gkd : gkeys) {
1902 Group lastGroup = null;
Yuta HIGUCHI2dce08a2017-04-20 21:57:48 -07001903 StringBuilder gchain = new StringBuilder();
Saurav Das24431192016-03-07 19:13:00 -08001904 for (GroupKey gk : gkd) {
1905 Group g = groupService.getGroup(deviceId, gk);
Saurav Das8be4e3a2016-03-11 17:19:07 -08001906 if (g == null) {
Saurav Das25190812016-05-27 13:54:07 -07001907 gchain.append(" NoGrp").append(" -->");
Saurav Das8be4e3a2016-03-11 17:19:07 -08001908 continue;
1909 }
1910 gchain.append(" 0x").append(Integer.toHexString(g.id().id()))
1911 .append(" -->");
Saurav Das24431192016-03-07 19:13:00 -08001912 lastGroup = g;
1913 }
1914 // add port information for last group in group-chain
Yuta HIGUCHI2dce08a2017-04-20 21:57:48 -07001915 List<Instruction> lastGroupIns = new ArrayList<>();
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001916 if (lastGroup != null && !lastGroup.buckets().buckets().isEmpty()) {
Saurav Das25190812016-05-27 13:54:07 -07001917 lastGroupIns = lastGroup.buckets().buckets().get(0)
1918 .treatment().allInstructions();
1919 }
1920 for (Instruction i: lastGroupIns) {
Saurav Das24431192016-03-07 19:13:00 -08001921 if (i instanceof OutputInstruction) {
Saurav Das8be4e3a2016-03-11 17:19:07 -08001922 gchain.append(" port:").append(((OutputInstruction) i).port());
Saurav Das24431192016-03-07 19:13:00 -08001923 }
1924 }
Saurav Das8be4e3a2016-03-11 17:19:07 -08001925 mappings.add(gchain.toString());
Saurav Das24431192016-03-07 19:13:00 -08001926 }
1927 return mappings;
1928 }
Charles Chan5b9df8d2016-03-28 22:21:40 -07001929
Saurav Dasa4020382018-02-14 14:14:54 -08001930 /**
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001931 * Utility class that retries sending flows a fixed number of times, even if
1932 * some of the attempts are successful. Used only for forwarding objectives.
1933 */
Alex Yashchuk4caa8e82017-12-08 17:40:05 +02001934 public final class RetryFlows implements Runnable {
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001935 int attempts = MAX_RETRY_ATTEMPTS;
1936 private Collection<FlowRule> retryFlows;
1937 private ForwardingObjective fwd;
1938
Charles Chan50d900c2018-03-02 13:26:22 -08001939 RetryFlows(ForwardingObjective fwd, Collection<FlowRule> retryFlows) {
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001940 this.fwd = fwd;
1941 this.retryFlows = retryFlows;
1942 }
1943
1944 @Override
1945 public void run() {
1946 log.info("RETRY FLOWS ATTEMPT# {} for fwd:{} rules:{}",
1947 MAX_RETRY_ATTEMPTS - attempts, fwd.id(), retryFlows.size());
pier8b3aef42019-03-11 15:14:02 -07001948 sendForwards(Collections.singletonList(Pair.of(fwd, retryFlows)));
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001949 if (--attempts > 0) {
pier8b3aef42019-03-11 15:14:02 -07001950 retryExecutorService.schedule(this, RETRY_MS, TimeUnit.MILLISECONDS);
1951 }
1952 }
1953 }
1954
1955 // Flow rules accumulator for reducing the number of transactions required to the devices.
1956 private final class ForwardingObjectiveAccumulator
1957 extends AbstractAccumulator<Pair<ForwardingObjective, Collection<FlowRule>>> {
1958
1959 ForwardingObjectiveAccumulator(int maxFwd, int maxBatchMS, int maxIdleMS) {
1960 super(TIMER, maxFwd, maxBatchMS, maxIdleMS);
1961 }
1962
1963 @Override
1964 public void processItems(List<Pair<ForwardingObjective, Collection<FlowRule>>> pairs) {
1965 // Triggers creation of a batch using the list of flowrules generated from fwdobjs.
1966 accumulatorExecutorService.execute(new FlowRulesBuilderTask(pairs));
1967 }
1968 }
1969
1970 // Task for building batch of flow rules in a separate thread.
1971 private final class FlowRulesBuilderTask implements Runnable {
1972 private final List<Pair<ForwardingObjective, Collection<FlowRule>>> pairs;
1973
1974 FlowRulesBuilderTask(List<Pair<ForwardingObjective, Collection<FlowRule>>> pairs) {
1975 this.pairs = pairs;
1976 }
1977
1978 @Override
1979 public void run() {
1980 try {
1981 sendForwards(pairs);
1982 } catch (Exception e) {
1983 log.warn("Unable to send forwards", e);
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001984 }
1985 }
1986 }
1987
Saurav Das822c4e22015-10-23 10:51:11 -07001988}