blob: 071bf983e723a66599cf2cf3219d834ac48c5108 [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;
104
105import static java.util.concurrent.Executors.newScheduledThreadPool;
pier8b3aef42019-03-11 15:14:02 -0700106import static java.util.concurrent.Executors.newSingleThreadScheduledExecutor;
Yi Tseng3a77b4f2017-02-06 15:02:17 -0800107import static org.onlab.packet.MacAddress.BROADCAST;
Charles Chanb4879a52017-10-20 19:09:16 -0700108import static org.onlab.packet.MacAddress.IPV4_MULTICAST;
109import static org.onlab.packet.MacAddress.IPV6_MULTICAST;
Yi Tseng3a77b4f2017-02-06 15:02:17 -0800110import static org.onlab.packet.MacAddress.NONE;
Pier Ventree0ae7a32016-11-23 09:57:42 -0800111import static org.onlab.util.Tools.groupedThreads;
Jonghwan Hyun800d9d02018-04-09 09:40:50 -0700112import static org.onosproject.driver.extensions.Ofdpa3CopyField.OXM_ID_PACKET_REG_1;
113import static org.onosproject.driver.extensions.Ofdpa3CopyField.OXM_ID_VLAN_VID;
Yi Tsengef19de12017-04-24 11:33:05 -0700114import static org.onosproject.driver.pipeline.ofdpa.OfdpaGroupHandlerUtility.*;
pier9469f3e2019-04-17 17:05:08 +0200115import static org.onosproject.driver.pipeline.ofdpa.OfdpaPipelineUtility.*;
Pier Luigi075f1012018-02-01 10:23:12 +0100116import static org.onosproject.net.group.GroupDescription.Type.SELECT;
Pier Ventree0ae7a32016-11-23 09:57:42 -0800117import static org.slf4j.LoggerFactory.getLogger;
Pier Ventre140a8942016-11-02 07:26:38 -0700118import static org.onosproject.net.flow.criteria.Criterion.Type.MPLS_BOS;
Pier Ventree0ae7a32016-11-23 09:57:42 -0800119
Saurav Das822c4e22015-10-23 10:51:11 -0700120/**
121 * Driver for Broadcom's OF-DPA v2.0 TTP.
Saurav Das822c4e22015-10-23 10:51:11 -0700122 */
Charles Chan361154b2016-03-24 10:23:39 -0700123public class Ofdpa2Pipeline extends AbstractHandlerBehaviour implements Pipeliner {
pier8b3aef42019-03-11 15:14:02 -0700124 // Timer for the accumulator
125 private static final Timer TIMER = new Timer("fwdobj-batching");
126 private Accumulator<Pair<ForwardingObjective, Collection<FlowRule>>> accumulator;
pier9469f3e2019-04-17 17:05:08 +0200127 // Internal objects
Saurav Das822c4e22015-10-23 10:51:11 -0700128 private final Logger log = getLogger(getClass());
Charles Chan425854b2016-04-11 15:32:12 -0700129 protected ServiceDirectory serviceDirectory;
Saurav Das822c4e22015-10-23 10:51:11 -0700130 protected FlowRuleService flowRuleService;
Charles Chan425854b2016-04-11 15:32:12 -0700131 protected CoreService coreService;
Saurav Das8a0732e2015-11-20 15:27:53 -0800132 protected GroupService groupService;
133 protected FlowObjectiveStore flowObjectiveStore;
Saurav Das822c4e22015-10-23 10:51:11 -0700134 protected DeviceId deviceId;
135 protected ApplicationId driverId;
Saurav Das822c4e22015-10-23 10:51:11 -0700136 protected DeviceService deviceService;
Charles Chan188ebf52015-12-23 00:15:11 -0800137 protected static KryoNamespace appKryo = new KryoNamespace.Builder()
Yi Tsengef19de12017-04-24 11:33:05 -0700138 .register(KryoNamespaces.API)
139 .register(GroupKey.class)
140 .register(DefaultGroupKey.class)
141 .register(OfdpaNextGroup.class)
142 .register(ArrayDeque.class)
143 .build("Ofdpa2Pipeline");
Saurav Das822c4e22015-10-23 10:51:11 -0700144
Charles Chan425854b2016-04-11 15:32:12 -0700145 protected Ofdpa2GroupHandler groupHandler;
Saurav Das822c4e22015-10-23 10:51:11 -0700146
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700147 // flows installations to be retried
pier8b3aef42019-03-11 15:14:02 -0700148 private ScheduledExecutorService retryExecutorService
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700149 = newScheduledThreadPool(5, groupedThreads("OfdpaPipeliner", "retry-%d", log));
Saurav Das4f980082015-11-05 13:39:15 -0800150
pier8b3aef42019-03-11 15:14:02 -0700151 // accumulator executor service
152 private ScheduledExecutorService accumulatorExecutorService
153 = newSingleThreadScheduledExecutor(groupedThreads("OfdpaPipeliner", "acc-%d", log));
154
Saurav Das822c4e22015-10-23 10:51:11 -0700155 @Override
156 public void init(DeviceId deviceId, PipelinerContext context) {
Saurav Das822c4e22015-10-23 10:51:11 -0700157 this.deviceId = deviceId;
158
Charles Chan425854b2016-04-11 15:32:12 -0700159 serviceDirectory = context.directory();
Saurav Das822c4e22015-10-23 10:51:11 -0700160 coreService = serviceDirectory.get(CoreService.class);
161 flowRuleService = serviceDirectory.get(FlowRuleService.class);
162 groupService = serviceDirectory.get(GroupService.class);
163 flowObjectiveStore = context.store();
Saurav Das822c4e22015-10-23 10:51:11 -0700164 deviceService = serviceDirectory.get(DeviceService.class);
pier8b3aef42019-03-11 15:14:02 -0700165 // Init the accumulator, if enabled
pier9469f3e2019-04-17 17:05:08 +0200166 if (isAccumulatorEnabled(this)) {
pier8b3aef42019-03-11 15:14:02 -0700167 accumulator = new ForwardingObjectiveAccumulator(context.accumulatorMaxObjectives(),
168 context.accumulatorMaxBatchMillis(),
169 context.accumulatorMaxIdleMillis());
170 }
Saurav Das822c4e22015-10-23 10:51:11 -0700171
Charles Chan40132b32017-01-22 00:19:37 -0800172 initDriverId();
173 initGroupHander(context);
Saurav Das822c4e22015-10-23 10:51:11 -0700174
Saurav Das822c4e22015-10-23 10:51:11 -0700175 initializePipeline();
Saurav Das822c4e22015-10-23 10:51:11 -0700176 }
177
Charles Chan40132b32017-01-22 00:19:37 -0800178 protected void initDriverId() {
179 driverId = coreService.registerApplication(
180 "org.onosproject.driver.Ofdpa2Pipeline");
181 }
182
183 protected void initGroupHander(PipelinerContext context) {
184 groupHandler = new Ofdpa2GroupHandler();
185 groupHandler.init(deviceId, context);
186 }
187
Saurav Das822c4e22015-10-23 10:51:11 -0700188 protected void initializePipeline() {
Charles Chan188ebf52015-12-23 00:15:11 -0800189 // OF-DPA does not require initializing the pipeline as it puts default
190 // rules automatically in the hardware. However emulation of OFDPA in
191 // software switches does require table-miss-entries.
Saurav Das822c4e22015-10-23 10:51:11 -0700192 }
193
Charles Chand1172632017-03-15 17:33:09 -0700194 /**
Alex Yashchuk4caa8e82017-12-08 17:40:05 +0200195 * Determines whether this pipeline requires MPLS POP instruction.
196 *
197 * @return true to use MPLS POP instruction
198 */
199 public boolean requireMplsPop() {
200 return true;
201 }
202
203 /**
204 * Determines whether this pipeline requires MPLS BOS match.
205 *
206 * @return true to use MPLS BOS match
207 */
208 public boolean requireMplsBosMatch() {
209 return true;
210 }
211
212 /**
213 * Determines whether this pipeline requires MPLS TTL decrement and copy.
214 *
215 * @return true to use MPLS TTL decrement and copy
216 */
217 public boolean requireMplsTtlModification() {
218 return true;
219 }
220
221 /**
Charles Chand1172632017-03-15 17:33:09 -0700222 * Determines whether this pipeline requires OFDPA match and set VLAN extensions.
223 *
224 * @return true to use the extensions
225 */
226 protected boolean requireVlanExtensions() {
227 return true;
228 }
229
Saurav Das86d13e82017-04-28 17:03:48 -0700230 /**
231 * Determines whether in-port should be matched on in TMAC table rules.
232 *
233 * @return true if match on in-port should be programmed
234 */
235 protected boolean matchInPortTmacTable() {
236 return true;
237 }
238
Charles Chand9e47c62017-10-05 15:17:15 -0700239 /**
240 * Determines whether matching L4 destination port on IPv6 packets is supported in ACL table.
241 *
242 * @return true if matching L4 destination port on IPv6 packets is supported in ACL table.
243 */
244 protected boolean supportIpv6L4Dst() {
245 return true;
246 }
247
Saurav Dasc568c342018-01-25 09:49:01 -0800248 /**
249 * Determines whether this driver should continue to retry flows that point
250 * to empty groups. See CORD-554.
251 *
252 * @return true if the driver should retry flows
253 */
254 protected boolean shouldRetry() {
255 return true;
256 }
257
Charles Chan45b69ab2018-03-02 15:41:41 -0800258 /**
259 * Determines whether this driver requires unicast flow to be installed before multicast flow
260 * in TMAC table.
261 *
262 * @return true if required
263 */
264 protected boolean requireUnicastBeforeMulticast() {
265 return false;
266 }
267
Andrea Campanellad980c6d2018-04-30 11:48:55 +0200268 /**
269 * Determines whether this driver supports installing a clearDeferred action on table 30.
270 *
271 * @return true if required
272 */
273 protected boolean supportsUnicastBlackHole() {
274 return true;
275 }
276
Charles Chanab591602019-01-22 17:25:04 -0800277 protected boolean requirePuntTable() {
278 return false;
279 }
280
Saurav Das822c4e22015-10-23 10:51:11 -0700281 //////////////////////////////////////
282 // Flow Objectives
283 //////////////////////////////////////
284
285 @Override
286 public void filter(FilteringObjective filteringObjective) {
287 if (filteringObjective.type() == FilteringObjective.Type.PERMIT) {
288 processFilter(filteringObjective,
289 filteringObjective.op() == Objective.Operation.ADD,
290 filteringObjective.appId());
291 } else {
292 // Note that packets that don't match the PERMIT filter are
293 // automatically denied. The DENY filter is used to deny packets
294 // that are otherwise permitted by the PERMIT filter.
295 // Use ACL table flow rules here for DENY filtering objectives
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530296 log.warn("filter objective other than PERMIT currently not supported");
Saurav Das822c4e22015-10-23 10:51:11 -0700297 fail(filteringObjective, ObjectiveError.UNSUPPORTED);
298 }
299 }
300
301 @Override
302 public void forward(ForwardingObjective fwd) {
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700303 Collection<FlowRule> rules = processForward(fwd);
Saurav Das25190812016-05-27 13:54:07 -0700304 if (rules == null || rules.isEmpty()) {
305 // Assumes fail message has already been generated to the objective
306 // context. Returning here prevents spurious pass message to be
307 // generated by FlowRule service for empty flowOps.
308 return;
309 }
pier8b3aef42019-03-11 15:14:02 -0700310 // Let's accumulate flow rules - otherwise send directly
311 if (accumulator != null) {
312 accumulator.add(Pair.of(fwd, rules));
313 } else {
314 sendForwards(Collections.singletonList(Pair.of(fwd, rules)));
315 }
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700316 }
317
pier8b3aef42019-03-11 15:14:02 -0700318 // Builds the batch using the accumulated flow rules
319 private void sendForwards(List<Pair<ForwardingObjective, Collection<FlowRule>>> pairs) {
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700320 FlowRuleOperations.Builder flowOpsBuilder = FlowRuleOperations.builder();
pier8b3aef42019-03-11 15:14:02 -0700321 log.debug("Sending {} fwd-objs", pairs.size());
322 List<Objective> fwdObjs = Lists.newArrayList();
323 // Iterates over all accumulated flow rules and then build an unique batch
324 pairs.forEach(pair -> {
325 ForwardingObjective fwd = pair.getLeft();
326 Collection<FlowRule> rules = pair.getRight();
327 switch (fwd.op()) {
328 case ADD:
329 rules.stream()
330 .filter(Objects::nonNull)
331 .forEach(flowOpsBuilder::add);
332 log.debug("Applying a add fwd-obj {} to sw:{}", fwd.id(), deviceId);
333 fwdObjs.add(fwd);
334 break;
335 case REMOVE:
336 rules.stream()
337 .filter(Objects::nonNull)
338 .forEach(flowOpsBuilder::remove);
339 log.debug("Deleting a flow rule to sw:{}", deviceId);
340 fwdObjs.add(fwd);
341 break;
342 default:
343 fail(fwd, ObjectiveError.UNKNOWN);
344 log.warn("Unknown forwarding type {}", fwd.op());
345 }
346 });
347 // Finally applies the operations
Saurav Das822c4e22015-10-23 10:51:11 -0700348 flowRuleService.apply(flowOpsBuilder.build(new FlowRuleOperationsContext() {
pier8b3aef42019-03-11 15:14:02 -0700349
Saurav Das822c4e22015-10-23 10:51:11 -0700350 @Override
351 public void onSuccess(FlowRuleOperations ops) {
pier8b3aef42019-03-11 15:14:02 -0700352 log.trace("Flow rule operations onSuccess {}", ops);
pier9469f3e2019-04-17 17:05:08 +0200353 fwdObjs.forEach(OfdpaPipelineUtility::pass);
Saurav Das822c4e22015-10-23 10:51:11 -0700354 }
355
356 @Override
357 public void onError(FlowRuleOperations ops) {
pier8b3aef42019-03-11 15:14:02 -0700358 ObjectiveError error = ObjectiveError.FLOWINSTALLATIONFAILED;
359 log.warn("Flow rule operations onError {}. Reason = {}", ops, error);
360 fwdObjs.forEach(fwdObj -> fail(fwdObj, error));
Saurav Das822c4e22015-10-23 10:51:11 -0700361 }
362 }));
Saurav Das822c4e22015-10-23 10:51:11 -0700363 }
364
365 @Override
366 public void next(NextObjective nextObjective) {
Saurav Das8a0732e2015-11-20 15:27:53 -0800367 NextGroup nextGroup = flowObjectiveStore.getNextGroup(nextObjective.id());
368 switch (nextObjective.op()) {
369 case ADD:
Saurav Das4f980082015-11-05 13:39:15 -0800370 if (nextGroup != null) {
Saurav Das8a0732e2015-11-20 15:27:53 -0800371 log.warn("Cannot add next {} that already exists in device {}",
372 nextObjective.id(), deviceId);
373 return;
374 }
Saurav Das1547b3f2017-05-05 17:01:08 -0700375 log.debug("Processing NextObjective id {} in dev {} - add group",
Saurav Das8a0732e2015-11-20 15:27:53 -0800376 nextObjective.id(), deviceId);
Charles Chan425854b2016-04-11 15:32:12 -0700377 groupHandler.addGroup(nextObjective);
Saurav Das8a0732e2015-11-20 15:27:53 -0800378 break;
379 case ADD_TO_EXISTING:
380 if (nextGroup != null) {
Saurav Das1547b3f2017-05-05 17:01:08 -0700381 log.debug("Processing NextObjective id {} in dev {} - add bucket",
Saurav Das8a0732e2015-11-20 15:27:53 -0800382 nextObjective.id(), deviceId);
Charles Chan425854b2016-04-11 15:32:12 -0700383 groupHandler.addBucketToGroup(nextObjective, nextGroup);
Saurav Das4f980082015-11-05 13:39:15 -0800384 } else {
Saurav Das8a0732e2015-11-20 15:27:53 -0800385 // it is possible that group-chain has not been fully created yet
Saurav Das423fe2b2015-12-04 10:52:59 -0800386 log.debug("Waiting to add bucket to group for next-id:{} in dev:{}",
387 nextObjective.id(), deviceId);
Yi Tseng47f82dc2017-03-05 22:48:39 -0800388
389 // by design multiple pending bucket is allowed for the group
390 groupHandler.pendingBuckets.compute(nextObjective.id(), (nextId, pendBkts) -> {
391 if (pendBkts == null) {
392 pendBkts = Sets.newHashSet();
393 }
394 pendBkts.add(nextObjective);
395 return pendBkts;
396 });
Saurav Das4f980082015-11-05 13:39:15 -0800397 }
Saurav Das8a0732e2015-11-20 15:27:53 -0800398 break;
399 case REMOVE:
400 if (nextGroup == null) {
401 log.warn("Cannot remove next {} that does not exist in device {}",
402 nextObjective.id(), deviceId);
403 return;
404 }
Saurav Das1547b3f2017-05-05 17:01:08 -0700405 log.debug("Processing NextObjective id {} in dev {} - remove group",
Saurav Das8a0732e2015-11-20 15:27:53 -0800406 nextObjective.id(), deviceId);
Charles Chan425854b2016-04-11 15:32:12 -0700407 groupHandler.removeGroup(nextObjective, nextGroup);
Saurav Das8a0732e2015-11-20 15:27:53 -0800408 break;
409 case REMOVE_FROM_EXISTING:
410 if (nextGroup == null) {
411 log.warn("Cannot remove from next {} that does not exist in device {}",
412 nextObjective.id(), deviceId);
413 return;
414 }
Saurav Das1547b3f2017-05-05 17:01:08 -0700415 log.debug("Processing NextObjective id {} in dev {} - remove bucket",
Saurav Das8a0732e2015-11-20 15:27:53 -0800416 nextObjective.id(), deviceId);
Charles Chan425854b2016-04-11 15:32:12 -0700417 groupHandler.removeBucketFromGroup(nextObjective, nextGroup);
Saurav Das8a0732e2015-11-20 15:27:53 -0800418 break;
Jonghwan Hyunf810a7a2018-02-12 16:43:45 +0900419 case MODIFY:
420 if (nextGroup == null) {
421 log.warn("Cannot modify next {} that does not exist in device {}",
422 nextObjective.id(), deviceId);
423 return;
424 }
Ruchi Sahotaef0761c2019-01-28 01:08:18 +0000425 log.debug("Processing NextObjective id {} in dev {} group {} - modify bucket",
426 nextObjective.id(), deviceId, nextGroup);
Jonghwan Hyunf810a7a2018-02-12 16:43:45 +0900427 groupHandler.modifyBucketFromGroup(nextObjective, nextGroup);
428 break;
Saurav Dasceccf242017-08-03 18:30:35 -0700429 case VERIFY:
430 if (nextGroup == null) {
431 log.warn("Cannot verify next {} that does not exist in device {}",
432 nextObjective.id(), deviceId);
433 return;
434 }
435 log.debug("Processing NextObjective id {} in dev {} - verify",
436 nextObjective.id(), deviceId);
437 groupHandler.verifyGroup(nextObjective, nextGroup);
438 break;
Saurav Das8a0732e2015-11-20 15:27:53 -0800439 default:
Saurav Das4f980082015-11-05 13:39:15 -0800440 log.warn("Unsupported operation {}", nextObjective.op());
Saurav Das822c4e22015-10-23 10:51:11 -0700441 }
442 }
443
444 //////////////////////////////////////
445 // Flow handling
446 //////////////////////////////////////
447
448 /**
Yi Tseng0fdc07e2017-04-24 19:17:08 -0700449 * As per OFDPA 2.0 TTP, filtering of VLAN ids and MAC addresses (for routing)
450 * configured on switch ports happen in different tables.
Saurav Das822c4e22015-10-23 10:51:11 -0700451 *
452 * @param filt the filtering objective
453 * @param install indicates whether to add or remove the objective
454 * @param applicationId the application that sent this objective
455 */
Saurav Das52025962016-01-28 22:30:01 -0800456 protected void processFilter(FilteringObjective filt,
457 boolean install, ApplicationId applicationId) {
Saurav Das822c4e22015-10-23 10:51:11 -0700458 // This driver only processes filtering criteria defined with switch
459 // ports as the key
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530460 PortCriterion portCriterion = null;
Saurav Das822c4e22015-10-23 10:51:11 -0700461 EthCriterion ethCriterion = null;
462 VlanIdCriterion vidCriterion = null;
Saurav Das822c4e22015-10-23 10:51:11 -0700463 if (!filt.key().equals(Criteria.dummy()) &&
464 filt.key().type() == Criterion.Type.IN_PORT) {
465 portCriterion = (PortCriterion) filt.key();
Saurav Das822c4e22015-10-23 10:51:11 -0700466 }
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530467 if (portCriterion == null) {
468 log.debug("No IN_PORT defined in filtering objective from app: {}",
469 applicationId);
470 } else {
471 log.debug("Received filtering objective for dev/port: {}/{}", deviceId,
472 portCriterion.port());
473 }
Saurav Das822c4e22015-10-23 10:51:11 -0700474 // convert filtering conditions for switch-intfs into flowrules
475 FlowRuleOperations.Builder ops = FlowRuleOperations.builder();
476 for (Criterion criterion : filt.conditions()) {
Yi Tseng0fdc07e2017-04-24 19:17:08 -0700477 switch (criterion.type()) {
478 case ETH_DST:
479 case ETH_DST_MASKED:
480 ethCriterion = (EthCriterion) criterion;
481 break;
482 case VLAN_VID:
483 vidCriterion = (VlanIdCriterion) criterion;
484 break;
485 default:
486 log.warn("Unsupported filter {}", criterion);
487 fail(filt, ObjectiveError.UNSUPPORTED);
488 return;
Saurav Das822c4e22015-10-23 10:51:11 -0700489 }
490 }
491
Saurav Das0e99e2b2015-10-28 12:39:42 -0700492 VlanId assignedVlan = null;
Charles Chane849c192016-01-11 18:28:54 -0800493 if (vidCriterion != null) {
Charles Chand55e84d2016-03-30 17:54:24 -0700494 // Use the VLAN in criterion if metadata is not present and the traffic is tagged
Charles Chanc550f2e2017-12-19 19:55:57 -0800495 if (!vidCriterion.vlanId().equals(VlanId.NONE)) {
Charles Chane849c192016-01-11 18:28:54 -0800496 assignedVlan = vidCriterion.vlanId();
Piere5bff482018-03-07 11:42:50 +0100497 }
498 // If the meta VLAN is present let's update the assigned vlan
499 if (filt.meta() != null) {
500 VlanId metaVlan = readVlanFromTreatment(filt.meta());
501 if (metaVlan != null) {
502 assignedVlan = metaVlan;
503 }
Charles Chand55e84d2016-03-30 17:54:24 -0700504 }
Charles Chane849c192016-01-11 18:28:54 -0800505
Charles Chand55e84d2016-03-30 17:54:24 -0700506 if (assignedVlan == null) {
507 log.error("Driver fails to extract VLAN information. "
Frank Wangd8ab0962017-08-11 11:09:30 +0800508 + "Not processing VLAN filters on device {}.", deviceId);
Charles Chand55e84d2016-03-30 17:54:24 -0700509 log.debug("VLAN ID in criterion={}, metadata={}",
510 readVlanFromTreatment(filt.meta()), vidCriterion.vlanId());
511 fail(filt, ObjectiveError.BADPARAMS);
512 return;
Saurav Das0e99e2b2015-10-28 12:39:42 -0700513 }
514 }
515
Yi Tseng3a77b4f2017-02-06 15:02:17 -0800516 if (ethCriterion == null || ethCriterion.mac().equals(NONE)) {
Charles Chan985b12e2016-05-11 19:47:22 -0700517 // NOTE: it is possible that a filtering objective only has vidCriterion
Daniel Ginsburgc1d47e92018-04-30 19:27:19 -0400518 log.debug("filtering objective missing dstMac, won't program TMAC table");
Saurav Das822c4e22015-10-23 10:51:11 -0700519 } else {
Charles Chan45b69ab2018-03-02 15:41:41 -0800520 MacAddress unicastMac = readEthDstFromTreatment(filt.meta());
Charles Chan66291502018-03-02 16:43:28 -0800521 List<List<FlowRule>> allStages = processEthDstFilter(portCriterion, ethCriterion,
Charles Chan45b69ab2018-03-02 15:41:41 -0800522 vidCriterion, assignedVlan, unicastMac, applicationId);
Charles Chan66291502018-03-02 16:43:28 -0800523 for (List<FlowRule> flowRules : allStages) {
Charles Chan45b69ab2018-03-02 15:41:41 -0800524 log.trace("Starting a new flow rule stage for TMAC table flow");
Charles Chan66291502018-03-02 16:43:28 -0800525 ops.newStage();
Charles Chanc550f2e2017-12-19 19:55:57 -0800526
Charles Chan66291502018-03-02 16:43:28 -0800527 for (FlowRule flowRule : flowRules) {
Charles Chan056e0c12018-05-10 22:19:49 +0000528 log.trace("{} flow rules in TMAC table: {} for dev: {}",
Charles Chan66291502018-03-02 16:43:28 -0800529 (install) ? "adding" : "removing", flowRules, deviceId);
Charles Chan66291502018-03-02 16:43:28 -0800530 if (install) {
531 ops = ops.add(flowRule);
Charles Chanc550f2e2017-12-19 19:55:57 -0800532 } else {
Charles Chan66291502018-03-02 16:43:28 -0800533 // NOTE: Only remove TMAC flow when there is no more enabled port within the
534 // same VLAN on this device if TMAC doesn't support matching on in_port.
535 if (matchInPortTmacTable() || (filt.meta() != null && filt.meta().clearedDeferred())) {
536 ops = ops.remove(flowRule);
537 } else {
538 log.debug("Abort TMAC flow removal on {}. Some other ports still share this TMAC flow");
539 }
Charles Chanc550f2e2017-12-19 19:55:57 -0800540 }
541 }
Saurav Das822c4e22015-10-23 10:51:11 -0700542 }
543 }
544
Charles Chan985b12e2016-05-11 19:47:22 -0700545 if (vidCriterion == null) {
546 // NOTE: it is possible that a filtering objective only has ethCriterion
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530547 log.info("filtering objective missing VLAN, cannot program VLAN Table");
Saurav Das822c4e22015-10-23 10:51:11 -0700548 } else {
Charles Chan66291502018-03-02 16:43:28 -0800549 List<List<FlowRule>> allStages = processVlanIdFilter(
Charles Chan14967c22015-12-07 11:11:50 -0800550 portCriterion, vidCriterion, assignedVlan, applicationId);
Charles Chan66291502018-03-02 16:43:28 -0800551 for (List<FlowRule> flowRules : allStages) {
Charles Chan45b69ab2018-03-02 15:41:41 -0800552 log.trace("Starting a new flow rule stage for VLAN table flow");
Charles Chan66291502018-03-02 16:43:28 -0800553 ops.newStage();
Charles Chan14967c22015-12-07 11:11:50 -0800554
Charles Chan66291502018-03-02 16:43:28 -0800555 for (FlowRule flowRule : flowRules) {
556 log.trace("{} flow rules in VLAN table: {} for dev: {}",
557 (install) ? "adding" : "removing", flowRule, deviceId);
558 ops = install ? ops.add(flowRule) : ops.remove(flowRule);
Charles Chand1172632017-03-15 17:33:09 -0700559 }
Saurav Das822c4e22015-10-23 10:51:11 -0700560 }
561 }
562
Saurav Das822c4e22015-10-23 10:51:11 -0700563 // apply filtering flow rules
564 flowRuleService.apply(ops.build(new FlowRuleOperationsContext() {
565 @Override
566 public void onSuccess(FlowRuleOperations ops) {
Saurav Das018605f2017-02-18 14:05:44 -0800567 log.debug("Applied {} filtering rules in device {}",
Saurav Das822c4e22015-10-23 10:51:11 -0700568 ops.stages().get(0).size(), deviceId);
569 pass(filt);
570 }
571
572 @Override
573 public void onError(FlowRuleOperations ops) {
574 log.info("Failed to apply all filtering rules in dev {}", deviceId);
575 fail(filt, ObjectiveError.FLOWINSTALLATIONFAILED);
576 }
577 }));
578
579 }
580
581 /**
Charles Chand1172632017-03-15 17:33:09 -0700582 * Internal implementation of processVlanIdFilter.
583 * <p>
584 * The is_present bit in set_vlan_vid action is required to be 0 in OFDPA i12.
585 * Since it is non-OF spec, we need an extension treatment for that.
586 * The useVlanExtension must be set to false for OFDPA i12.
587 * </p>
Charles Chan66291502018-03-02 16:43:28 -0800588 * <p>
589 * NOTE: Separate VLAN filtering rules and assignment rules
590 * into different stages in order to guarantee that filtering rules
591 * always go first, as required by OFDPA.
592 * </p>
Saurav Das0e99e2b2015-10-28 12:39:42 -0700593 *
Charles Chanf9e98652016-09-07 16:54:23 -0700594 * @param portCriterion port on device for which this filter is programmed
595 * @param vidCriterion vlan assigned to port, or NONE for untagged
596 * @param assignedVlan assigned vlan-id for untagged packets
597 * @param applicationId for application programming this filter
Charles Chan66291502018-03-02 16:43:28 -0800598 * @return stages of flow rules for port-vlan filters
Saurav Das822c4e22015-10-23 10:51:11 -0700599 */
Charles Chan66291502018-03-02 16:43:28 -0800600 protected List<List<FlowRule>> processVlanIdFilter(PortCriterion portCriterion,
Yi Tsengef19de12017-04-24 11:33:05 -0700601 VlanIdCriterion vidCriterion,
602 VlanId assignedVlan,
603 ApplicationId applicationId) {
Charles Chan66291502018-03-02 16:43:28 -0800604 List<FlowRule> filteringRules = new ArrayList<>();
605 List<FlowRule> assignmentRules = new ArrayList<>();
Saurav Das822c4e22015-10-23 10:51:11 -0700606 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
607 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
Charles Chan14967c22015-12-07 11:11:50 -0800608 TrafficSelector.Builder preSelector = null;
609 TrafficTreatment.Builder preTreatment = null;
610
Saurav Das4f980082015-11-05 13:39:15 -0800611 treatment.transition(TMAC_TABLE);
612
Saurav Das822c4e22015-10-23 10:51:11 -0700613 if (vidCriterion.vlanId() == VlanId.NONE) {
614 // untagged packets are assigned vlans
Charles Chand1172632017-03-15 17:33:09 -0700615 preSelector = DefaultTrafficSelector.builder();
616 if (requireVlanExtensions()) {
617 OfdpaMatchVlanVid ofdpaMatchVlanVid = new OfdpaMatchVlanVid(VlanId.NONE);
618 selector.extension(ofdpaMatchVlanVid, deviceId);
Charles Chanf9e98652016-09-07 16:54:23 -0700619 OfdpaSetVlanVid ofdpaSetVlanVid = new OfdpaSetVlanVid(assignedVlan);
620 treatment.extension(ofdpaSetVlanVid, deviceId);
Charles Chand1172632017-03-15 17:33:09 -0700621
622 OfdpaMatchVlanVid preOfdpaMatchVlanVid = new OfdpaMatchVlanVid(assignedVlan);
623 preSelector.extension(preOfdpaMatchVlanVid, deviceId);
Charles Chanf9e98652016-09-07 16:54:23 -0700624 } else {
Charles Chand1172632017-03-15 17:33:09 -0700625 selector.matchVlanId(VlanId.NONE);
Charles Chanf9e98652016-09-07 16:54:23 -0700626 treatment.setVlanId(assignedVlan);
Charles Chand1172632017-03-15 17:33:09 -0700627
628 preSelector.matchVlanId(assignedVlan);
629 }
630 preTreatment = DefaultTrafficTreatment.builder().transition(TMAC_TABLE);
631 } else {
632 if (requireVlanExtensions()) {
633 OfdpaMatchVlanVid ofdpaMatchVlanVid = new OfdpaMatchVlanVid(vidCriterion.vlanId());
634 selector.extension(ofdpaMatchVlanVid, deviceId);
635 } else {
636 selector.matchVlanId(vidCriterion.vlanId());
Charles Chanf9e98652016-09-07 16:54:23 -0700637 }
Charles Chan14967c22015-12-07 11:11:50 -0800638
Charles Chand55e84d2016-03-30 17:54:24 -0700639 if (!assignedVlan.equals(vidCriterion.vlanId())) {
Charles Chand1172632017-03-15 17:33:09 -0700640 if (requireVlanExtensions()) {
Charles Chanc03782d2017-01-31 13:57:55 -0800641 OfdpaSetVlanVid ofdpaSetVlanVid = new OfdpaSetVlanVid(assignedVlan);
642 treatment.extension(ofdpaSetVlanVid, deviceId);
643 } else {
644 treatment.setVlanId(assignedVlan);
645 }
Charles Chand55e84d2016-03-30 17:54:24 -0700646 }
Saurav Das822c4e22015-10-23 10:51:11 -0700647 }
Saurav Das822c4e22015-10-23 10:51:11 -0700648
649 // ofdpa cannot match on ALL portnumber, so we need to use separate
650 // rules for each port.
Charles Chan14967c22015-12-07 11:11:50 -0800651 List<PortNumber> portnums = new ArrayList<>();
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530652 if (portCriterion != null) {
653 if (PortNumber.ALL.equals(portCriterion.port())) {
654 for (Port port : deviceService.getPorts(deviceId)) {
655 if (port.number().toLong() > 0 && port.number().toLong() < OFPP_MAX) {
656 portnums.add(port.number());
657 }
Saurav Das822c4e22015-10-23 10:51:11 -0700658 }
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530659 } else {
660 portnums.add(portCriterion.port());
Saurav Das822c4e22015-10-23 10:51:11 -0700661 }
662 } else {
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530663 log.warn("Filtering Objective missing Port Criterion . " +
Charles Chan66291502018-03-02 16:43:28 -0800664 "VLAN Table cannot be programmed for {}", deviceId);
Saurav Das822c4e22015-10-23 10:51:11 -0700665 }
Saurav Das4f980082015-11-05 13:39:15 -0800666
Saurav Das822c4e22015-10-23 10:51:11 -0700667 for (PortNumber pnum : portnums) {
Saurav Das4f980082015-11-05 13:39:15 -0800668 // create rest of flowrule
Saurav Das822c4e22015-10-23 10:51:11 -0700669 selector.matchInPort(pnum);
670 FlowRule rule = DefaultFlowRule.builder()
671 .forDevice(deviceId)
672 .withSelector(selector.build())
673 .withTreatment(treatment.build())
674 .withPriority(DEFAULT_PRIORITY)
675 .fromApp(applicationId)
676 .makePermanent()
677 .forTable(VLAN_TABLE).build();
Charles Chan66291502018-03-02 16:43:28 -0800678 assignmentRules.add(rule);
Charles Chan14967c22015-12-07 11:11:50 -0800679
680 if (preSelector != null) {
681 preSelector.matchInPort(pnum);
682 FlowRule preRule = DefaultFlowRule.builder()
683 .forDevice(deviceId)
684 .withSelector(preSelector.build())
685 .withTreatment(preTreatment.build())
686 .withPriority(DEFAULT_PRIORITY)
687 .fromApp(applicationId)
688 .makePermanent()
689 .forTable(VLAN_TABLE).build();
Charles Chan66291502018-03-02 16:43:28 -0800690 filteringRules.add(preRule);
Charles Chan14967c22015-12-07 11:11:50 -0800691 }
Saurav Das822c4e22015-10-23 10:51:11 -0700692 }
Charles Chan2686dd72018-03-06 22:10:15 -0800693 return ImmutableList.of(filteringRules, assignmentRules);
Saurav Das822c4e22015-10-23 10:51:11 -0700694 }
695
696 /**
697 * Allows routed packets with correct destination MAC to be directed
Charles Chan45b69ab2018-03-02 15:41:41 -0800698 * to unicast routing table, multicast routing table or MPLS forwarding table.
Saurav Das822c4e22015-10-23 10:51:11 -0700699 *
700 * @param portCriterion port on device for which this filter is programmed
701 * @param ethCriterion dstMac of device for which is filter is programmed
702 * @param vidCriterion vlan assigned to port, or NONE for untagged
Saurav Das0e99e2b2015-10-28 12:39:42 -0700703 * @param assignedVlan assigned vlan-id for untagged packets
Charles Chan45b69ab2018-03-02 15:41:41 -0800704 * @param unicastMac some switches require a unicast TMAC flow to be programmed before multicast
705 * TMAC flow. This MAC address will be used for the unicast TMAC flow.
706 * This is unused if the filtering objective is a unicast.
Saurav Das822c4e22015-10-23 10:51:11 -0700707 * @param applicationId for application programming this filter
Charles Chan66291502018-03-02 16:43:28 -0800708 * @return stages of flow rules for port-vlan filters
Saurav Das822c4e22015-10-23 10:51:11 -0700709
710 */
Charles Chan66291502018-03-02 16:43:28 -0800711 protected List<List<FlowRule>> processEthDstFilter(PortCriterion portCriterion,
Saurav Das822c4e22015-10-23 10:51:11 -0700712 EthCriterion ethCriterion,
713 VlanIdCriterion vidCriterion,
Saurav Das0e99e2b2015-10-28 12:39:42 -0700714 VlanId assignedVlan,
Charles Chan45b69ab2018-03-02 15:41:41 -0800715 MacAddress unicastMac,
Saurav Das822c4e22015-10-23 10:51:11 -0700716 ApplicationId applicationId) {
Charles Chan5270ed02016-01-30 23:22:37 -0800717 // Consider PortNumber.ANY as wildcard. Match ETH_DST only
Sivachidambaram Subramanianc598d062017-05-16 22:42:06 +0530718 if (portCriterion != null && PortNumber.ANY.equals(portCriterion.port())) {
Charles Chan5270ed02016-01-30 23:22:37 -0800719 return processEthDstOnlyFilter(ethCriterion, applicationId);
720 }
721
Charles Chan5b9df8d2016-03-28 22:21:40 -0700722 // Multicast MAC
723 if (ethCriterion.mask() != null) {
Charles Chan45b69ab2018-03-02 15:41:41 -0800724 return processMcastEthDstFilter(ethCriterion, assignedVlan, unicastMac, applicationId);
Charles Chan5b9df8d2016-03-28 22:21:40 -0700725 }
726
Saurav Das822c4e22015-10-23 10:51:11 -0700727 //handling untagged packets via assigned VLAN
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530728 if (vidCriterion != null && vidCriterion.vlanId() == VlanId.NONE) {
Saurav Das0e99e2b2015-10-28 12:39:42 -0700729 vidCriterion = (VlanIdCriterion) Criteria.matchVlanId(assignedVlan);
Saurav Das822c4e22015-10-23 10:51:11 -0700730 }
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530731 List<FlowRule> rules = new ArrayList<>();
732 OfdpaMatchVlanVid ofdpaMatchVlanVid = null;
733 if (vidCriterion != null && requireVlanExtensions()) {
734 ofdpaMatchVlanVid = new OfdpaMatchVlanVid(vidCriterion.vlanId());
735 }
Saurav Das822c4e22015-10-23 10:51:11 -0700736 // ofdpa cannot match on ALL portnumber, so we need to use separate
737 // rules for each port.
Yuta HIGUCHI2dce08a2017-04-20 21:57:48 -0700738 List<PortNumber> portnums = new ArrayList<>();
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530739 if (portCriterion != null) {
740 if (PortNumber.ALL.equals(portCriterion.port())) {
741 for (Port port : deviceService.getPorts(deviceId)) {
742 if (port.number().toLong() > 0 && port.number().toLong() < OFPP_MAX) {
743 portnums.add(port.number());
744 }
Saurav Das822c4e22015-10-23 10:51:11 -0700745 }
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530746 } else {
747 portnums.add(portCriterion.port());
748 }
749 for (PortNumber pnum : portnums) {
750 rules.add(buildTmacRuleForIpv4(ethCriterion,
751 vidCriterion,
752 ofdpaMatchVlanVid,
753 applicationId,
754 pnum));
755 rules.add(buildTmacRuleForMpls(ethCriterion,
756 vidCriterion,
757 ofdpaMatchVlanVid,
758 applicationId,
759 pnum));
760 rules.add(buildTmacRuleForIpv6(ethCriterion,
761 vidCriterion,
762 ofdpaMatchVlanVid,
763 applicationId,
764 pnum));
Saurav Das822c4e22015-10-23 10:51:11 -0700765 }
766 } else {
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530767 rules.add(buildTmacRuleForIpv4(ethCriterion,
768 vidCriterion,
769 ofdpaMatchVlanVid,
770 applicationId,
771 null));
772 rules.add(buildTmacRuleForMpls(ethCriterion,
773 vidCriterion,
774 ofdpaMatchVlanVid,
775 applicationId,
776 null));
777 rules.add(buildTmacRuleForIpv6(ethCriterion,
778 vidCriterion,
779 ofdpaMatchVlanVid,
780 applicationId,
781 null));
Saurav Das822c4e22015-10-23 10:51:11 -0700782 }
Charles Chan66291502018-03-02 16:43:28 -0800783 return ImmutableList.of(rules);
Saurav Das822c4e22015-10-23 10:51:11 -0700784 }
785
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530786 /**
787 * Builds TMAC rules for IPv4 packets.
788 *
Charles Chanc550f2e2017-12-19 19:55:57 -0800789 * @param ethCriterion dst mac matching
790 * @param vidCriterion vlan id assigned to the port
791 * @param ofdpaMatchVlanVid OFDPA vlan id matching
792 * @param applicationId application id
793 * @param pnum port number
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530794 * @return TMAC rule for IPV4 packets
795 */
796 private FlowRule buildTmacRuleForIpv4(EthCriterion ethCriterion,
797 VlanIdCriterion vidCriterion,
798 OfdpaMatchVlanVid ofdpaMatchVlanVid,
799 ApplicationId applicationId,
800 PortNumber pnum) {
801 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
802 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
803 if (pnum != null) {
804 if (matchInPortTmacTable()) {
805 selector.matchInPort(pnum);
806 } else {
Saurav Dasf14d9ef2017-12-05 15:00:23 -0800807 log.debug("Pipeline does not support IN_PORT matching in TMAC table, " +
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530808 "ignoring the IN_PORT criteria");
809 }
810 }
811 if (vidCriterion != null) {
812 if (requireVlanExtensions()) {
813 selector.extension(ofdpaMatchVlanVid, deviceId);
814 } else {
815 selector.matchVlanId(vidCriterion.vlanId());
816 }
817 }
818 selector.matchEthType(Ethernet.TYPE_IPV4);
819 selector.matchEthDst(ethCriterion.mac());
820 treatment.transition(UNICAST_ROUTING_TABLE);
821 return DefaultFlowRule.builder()
822 .forDevice(deviceId)
823 .withSelector(selector.build())
824 .withTreatment(treatment.build())
825 .withPriority(DEFAULT_PRIORITY)
826 .fromApp(applicationId)
827 .makePermanent()
828 .forTable(TMAC_TABLE).build();
829 }
830
831 /**
832 * Builds TMAC rule for MPLS packets.
833 *
Charles Chanc550f2e2017-12-19 19:55:57 -0800834 * @param ethCriterion dst mac matching
835 * @param vidCriterion vlan id assigned to the port
836 * @param ofdpaMatchVlanVid OFDPA vlan id matching
837 * @param applicationId application id
838 * @param pnum port number
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530839 * @return TMAC rule for MPLS packets
840 */
841 private FlowRule buildTmacRuleForMpls(EthCriterion ethCriterion,
842 VlanIdCriterion vidCriterion,
843 OfdpaMatchVlanVid ofdpaMatchVlanVid,
844 ApplicationId applicationId,
845 PortNumber pnum) {
846 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
847 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
848 if (pnum != null) {
849 if (matchInPortTmacTable()) {
850 selector.matchInPort(pnum);
851 } else {
Saurav Dasf14d9ef2017-12-05 15:00:23 -0800852 log.debug("Pipeline does not support IN_PORT matching in TMAC table, " +
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530853 "ignoring the IN_PORT criteria");
854 }
855 }
856 if (vidCriterion != null) {
857 if (requireVlanExtensions()) {
858 selector.extension(ofdpaMatchVlanVid, deviceId);
859 } else {
860 selector.matchVlanId(vidCriterion.vlanId());
861 }
862 }
863 selector.matchEthType(Ethernet.MPLS_UNICAST);
864 selector.matchEthDst(ethCriterion.mac());
865 treatment.transition(MPLS_TABLE_0);
866 return DefaultFlowRule.builder()
867 .forDevice(deviceId)
868 .withSelector(selector.build())
869 .withTreatment(treatment.build())
870 .withPriority(DEFAULT_PRIORITY)
871 .fromApp(applicationId)
872 .makePermanent()
873 .forTable(TMAC_TABLE).build();
874 }
875
876 /**
877 * Builds TMAC rules for IPv6 packets.
878 *
Charles Chanc550f2e2017-12-19 19:55:57 -0800879 * @param ethCriterion dst mac matching
880 * @param vidCriterion vlan id assigned to the port
881 * @param ofdpaMatchVlanVid OFDPA vlan id matching
882 * @param applicationId application id
883 * @param pnum port number
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530884 * @return TMAC rule for IPV6 packets
885 */
886 private FlowRule buildTmacRuleForIpv6(EthCriterion ethCriterion,
887 VlanIdCriterion vidCriterion,
888 OfdpaMatchVlanVid ofdpaMatchVlanVid,
889 ApplicationId applicationId,
890 PortNumber pnum) {
891 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
892 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
893 if (pnum != null) {
894 if (matchInPortTmacTable()) {
895 selector.matchInPort(pnum);
896 } else {
Saurav Dasf14d9ef2017-12-05 15:00:23 -0800897 log.debug("Pipeline does not support IN_PORT matching in TMAC table, " +
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530898 "ignoring the IN_PORT criteria");
899 }
900 }
901 if (vidCriterion != null) {
902 if (requireVlanExtensions()) {
903 selector.extension(ofdpaMatchVlanVid, deviceId);
904 } else {
905 selector.matchVlanId(vidCriterion.vlanId());
906 }
907 }
908 selector.matchEthType(Ethernet.TYPE_IPV6);
909 selector.matchEthDst(ethCriterion.mac());
910 treatment.transition(UNICAST_ROUTING_TABLE);
911 return DefaultFlowRule.builder()
912 .forDevice(deviceId)
913 .withSelector(selector.build())
914 .withTreatment(treatment.build())
915 .withPriority(DEFAULT_PRIORITY)
916 .fromApp(applicationId)
917 .makePermanent()
918 .forTable(TMAC_TABLE).build();
919 }
920
Charles Chan66291502018-03-02 16:43:28 -0800921 protected List<List<FlowRule>> processEthDstOnlyFilter(EthCriterion ethCriterion,
Yi Tsengef19de12017-04-24 11:33:05 -0700922 ApplicationId applicationId) {
Pier Luigi0e358632017-01-31 09:35:05 -0800923 ImmutableList.Builder<FlowRule> builder = ImmutableList.builder();
924
Charles Chan5270ed02016-01-30 23:22:37 -0800925 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
926 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
927 selector.matchEthType(Ethernet.TYPE_IPV4);
928 selector.matchEthDst(ethCriterion.mac());
929 treatment.transition(UNICAST_ROUTING_TABLE);
930 FlowRule rule = 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();
Pier Luigi0e358632017-01-31 09:35:05 -0800938 builder.add(rule);
939
940 selector = DefaultTrafficSelector.builder();
941 treatment = DefaultTrafficTreatment.builder();
942 selector.matchEthType(Ethernet.TYPE_IPV6);
943 selector.matchEthDst(ethCriterion.mac());
944 treatment.transition(UNICAST_ROUTING_TABLE);
945 rule = DefaultFlowRule.builder()
946 .forDevice(deviceId)
947 .withSelector(selector.build())
948 .withTreatment(treatment.build())
949 .withPriority(DEFAULT_PRIORITY)
950 .fromApp(applicationId)
951 .makePermanent()
952 .forTable(TMAC_TABLE).build();
Charles Chan66291502018-03-02 16:43:28 -0800953 return ImmutableList.of(builder.add(rule).build());
Charles Chan5270ed02016-01-30 23:22:37 -0800954 }
955
Charles Chan66291502018-03-02 16:43:28 -0800956 List<List<FlowRule>> processMcastEthDstFilter(EthCriterion ethCriterion,
Charles Chanb4879a52017-10-20 19:09:16 -0700957 VlanId assignedVlan,
Charles Chan45b69ab2018-03-02 15:41:41 -0800958 MacAddress unicastMac,
Yi Tsengef19de12017-04-24 11:33:05 -0700959 ApplicationId applicationId) {
Charles Chan45b69ab2018-03-02 15:41:41 -0800960 ImmutableList.Builder<FlowRule> unicastFlows = ImmutableList.builder();
961 ImmutableList.Builder<FlowRule> multicastFlows = ImmutableList.builder();
962 TrafficSelector.Builder selector;
963 TrafficTreatment.Builder treatment;
Charles Chanb4879a52017-10-20 19:09:16 -0700964 FlowRule rule;
Julia Ferguson65428c32017-08-10 18:15:24 +0000965
Charles Chanb4879a52017-10-20 19:09:16 -0700966 if (IPV4_MULTICAST.equals(ethCriterion.mac())) {
Charles Chan45b69ab2018-03-02 15:41:41 -0800967 if (requireUnicastBeforeMulticast()) {
968 selector = DefaultTrafficSelector.builder();
969 treatment = DefaultTrafficTreatment.builder();
970 selector.matchEthType(Ethernet.TYPE_IPV4);
971 selector.matchEthDst(unicastMac);
Charles Chan056e0c12018-05-10 22:19:49 +0000972 selector.matchVlanId(assignedVlan);
Charles Chan45b69ab2018-03-02 15:41:41 -0800973 treatment.transition(UNICAST_ROUTING_TABLE);
974 rule = DefaultFlowRule.builder()
975 .forDevice(deviceId)
976 .withSelector(selector.build())
977 .withTreatment(treatment.build())
Charles Chan056e0c12018-05-10 22:19:49 +0000978 .withPriority(DEFAULT_PRIORITY)
Charles Chan45b69ab2018-03-02 15:41:41 -0800979 .fromApp(applicationId)
980 .makePermanent()
981 .forTable(TMAC_TABLE).build();
982 unicastFlows.add(rule);
983 }
984
985 selector = DefaultTrafficSelector.builder();
986 treatment = DefaultTrafficTreatment.builder();
Charles Chanb4879a52017-10-20 19:09:16 -0700987 selector.matchEthType(Ethernet.TYPE_IPV4);
988 selector.matchEthDstMasked(ethCriterion.mac(), ethCriterion.mask());
Charles Chan056e0c12018-05-10 22:19:49 +0000989 selector.matchVlanId(assignedVlan);
Charles Chanb4879a52017-10-20 19:09:16 -0700990 treatment.transition(MULTICAST_ROUTING_TABLE);
991 rule = DefaultFlowRule.builder()
992 .forDevice(deviceId)
993 .withSelector(selector.build())
994 .withTreatment(treatment.build())
Charles Chan056e0c12018-05-10 22:19:49 +0000995 .withPriority(DEFAULT_PRIORITY)
Charles Chanb4879a52017-10-20 19:09:16 -0700996 .fromApp(applicationId)
997 .makePermanent()
998 .forTable(TMAC_TABLE).build();
Charles Chan45b69ab2018-03-02 15:41:41 -0800999 multicastFlows.add(rule);
Charles Chanb4879a52017-10-20 19:09:16 -07001000 }
1001
1002 if (IPV6_MULTICAST.equals(ethCriterion.mac())) {
Charles Chan45b69ab2018-03-02 15:41:41 -08001003 if (requireUnicastBeforeMulticast()) {
1004 selector = DefaultTrafficSelector.builder();
1005 treatment = DefaultTrafficTreatment.builder();
1006 selector.matchEthType(Ethernet.TYPE_IPV6);
1007 selector.matchEthDst(unicastMac);
Charles Chan056e0c12018-05-10 22:19:49 +00001008 selector.matchVlanId(assignedVlan);
Charles Chan45b69ab2018-03-02 15:41:41 -08001009 treatment.transition(UNICAST_ROUTING_TABLE);
1010 rule = DefaultFlowRule.builder()
1011 .forDevice(deviceId)
1012 .withSelector(selector.build())
1013 .withTreatment(treatment.build())
Charles Chan056e0c12018-05-10 22:19:49 +00001014 .withPriority(DEFAULT_PRIORITY)
Charles Chan45b69ab2018-03-02 15:41:41 -08001015 .fromApp(applicationId)
1016 .makePermanent()
1017 .forTable(TMAC_TABLE).build();
1018 unicastFlows.add(rule);
1019 }
1020
Charles Chanb4879a52017-10-20 19:09:16 -07001021 selector = DefaultTrafficSelector.builder();
1022 treatment = DefaultTrafficTreatment.builder();
1023 selector.matchEthType(Ethernet.TYPE_IPV6);
1024 selector.matchEthDstMasked(ethCriterion.mac(), ethCriterion.mask());
Charles Chan056e0c12018-05-10 22:19:49 +00001025 selector.matchVlanId(assignedVlan);
Charles Chanb4879a52017-10-20 19:09:16 -07001026 treatment.transition(MULTICAST_ROUTING_TABLE);
1027 rule = DefaultFlowRule.builder()
1028 .forDevice(deviceId)
1029 .withSelector(selector.build())
1030 .withTreatment(treatment.build())
Charles Chan056e0c12018-05-10 22:19:49 +00001031 .withPriority(DEFAULT_PRIORITY)
Charles Chanb4879a52017-10-20 19:09:16 -07001032 .fromApp(applicationId)
1033 .makePermanent()
1034 .forTable(TMAC_TABLE).build();
Charles Chan45b69ab2018-03-02 15:41:41 -08001035 multicastFlows.add(rule);
Charles Chanb4879a52017-10-20 19:09:16 -07001036 }
Charles Chan45b69ab2018-03-02 15:41:41 -08001037 return ImmutableList.of(unicastFlows.build(), multicastFlows.build());
Charles Chan5b9df8d2016-03-28 22:21:40 -07001038 }
1039
Saurav Das822c4e22015-10-23 10:51:11 -07001040 private Collection<FlowRule> processForward(ForwardingObjective fwd) {
1041 switch (fwd.flag()) {
1042 case SPECIFIC:
1043 return processSpecific(fwd);
1044 case VERSATILE:
1045 return processVersatile(fwd);
Jonghwan Hyun800d9d02018-04-09 09:40:50 -07001046 case EGRESS:
1047 return processEgress(fwd);
Saurav Das822c4e22015-10-23 10:51:11 -07001048 default:
1049 fail(fwd, ObjectiveError.UNKNOWN);
1050 log.warn("Unknown forwarding flag {}", fwd.flag());
1051 }
1052 return Collections.emptySet();
1053 }
1054
1055 /**
Jonghwan Hyun800d9d02018-04-09 09:40:50 -07001056 * In the OF-DPA 2.0 pipeline, egress forwarding objectives go to the
1057 * egress tables.
1058 * @param fwd the forwarding objective of type 'egress'
1059 * @return a collection of flow rules to be sent to the switch. An empty
1060 * collection may be returned if there is a problem in processing
1061 * the flow rule
1062 */
1063 protected Collection<FlowRule> processEgress(ForwardingObjective fwd) {
1064 log.debug("Processing egress forwarding objective:{} in dev:{}",
1065 fwd, deviceId);
1066
1067 List<FlowRule> rules = new ArrayList<>();
1068
1069 // Build selector
1070 TrafficSelector.Builder sb = DefaultTrafficSelector.builder();
1071 VlanIdCriterion vlanIdCriterion = (VlanIdCriterion) fwd.selector().getCriterion(Criterion.Type.VLAN_VID);
1072 if (vlanIdCriterion == null) {
1073 log.error("Egress forwarding objective:{} must include vlanId", fwd.id());
1074 fail(fwd, ObjectiveError.BADPARAMS);
1075 return rules;
1076 }
1077
1078 Optional<Instruction> outInstr = fwd.treatment().allInstructions().stream()
1079 .filter(instruction -> instruction instanceof OutputInstruction).findFirst();
1080 if (!outInstr.isPresent()) {
1081 log.error("Egress forwarding objective:{} must include output port", fwd.id());
1082 fail(fwd, ObjectiveError.BADPARAMS);
1083 return rules;
1084 }
1085
1086 PortNumber portNumber = ((OutputInstruction) outInstr.get()).port();
1087
1088 sb.matchVlanId(vlanIdCriterion.vlanId());
1089 OfdpaMatchActsetOutput actsetOutput = new OfdpaMatchActsetOutput(portNumber);
1090 sb.extension(actsetOutput, deviceId);
1091
1092 sb.extension(new OfdpaMatchAllowVlanTranslation(ALLOW_VLAN_TRANSLATION), deviceId);
1093
1094 // Build a flow rule for Egress VLAN Flow table
1095 TrafficTreatment.Builder tb = DefaultTrafficTreatment.builder();
1096 tb.transition(EGRESS_DSCP_PCP_REMARK_FLOW_TABLE);
1097 if (fwd.treatment() != null) {
1098 for (Instruction instr : fwd.treatment().allInstructions()) {
1099 if (instr instanceof L2ModificationInstruction &&
1100 ((L2ModificationInstruction) instr).subtype() == L2SubType.VLAN_ID) {
1101 tb.immediate().add(instr);
1102 }
1103 if (instr instanceof L2ModificationInstruction &&
1104 ((L2ModificationInstruction) instr).subtype() == L2SubType.VLAN_PUSH) {
1105 tb.immediate().pushVlan();
1106 EthType ethType = ((L2ModificationInstruction.ModVlanHeaderInstruction) instr).ethernetType();
1107 if (ethType.equals(EtherType.QINQ.ethType())) {
1108 // Build a flow rule for Egress TPID Flow table
1109 TrafficSelector tpidSelector = DefaultTrafficSelector.builder()
1110 .extension(actsetOutput, deviceId)
1111 .matchVlanId(VlanId.ANY).build();
1112
1113 TrafficTreatment tpidTreatment = DefaultTrafficTreatment.builder()
1114 .extension(new Ofdpa3CopyField(COPY_FIELD_NBITS, COPY_FIELD_OFFSET,
1115 COPY_FIELD_OFFSET, OXM_ID_VLAN_VID,
1116 OXM_ID_PACKET_REG_1),
1117 deviceId)
1118 .popVlan()
1119 .pushVlan(EtherType.QINQ.ethType())
1120 .extension(new Ofdpa3CopyField(COPY_FIELD_NBITS, COPY_FIELD_OFFSET,
1121 COPY_FIELD_OFFSET, OXM_ID_PACKET_REG_1,
1122 OXM_ID_VLAN_VID),
1123 deviceId)
1124 .build();
1125
1126 FlowRule.Builder tpidRuleBuilder = DefaultFlowRule.builder()
1127 .fromApp(fwd.appId())
1128 .withPriority(fwd.priority())
1129 .forDevice(deviceId)
1130 .withSelector(tpidSelector)
1131 .withTreatment(tpidTreatment)
1132 .makePermanent()
1133 .forTable(EGRESS_TPID_FLOW_TABLE);
1134 rules.add(tpidRuleBuilder.build());
1135 }
1136 }
1137 }
1138 }
1139
1140 FlowRule.Builder ruleBuilder = DefaultFlowRule.builder()
1141 .fromApp(fwd.appId())
1142 .withPriority(fwd.priority())
1143 .forDevice(deviceId)
1144 .withSelector(sb.build())
1145 .withTreatment(tb.build())
1146 .makePermanent()
1147 .forTable(EGRESS_VLAN_FLOW_TABLE);
1148 rules.add(ruleBuilder.build());
1149 return rules;
1150 }
1151
1152 /**
Saurav Das822c4e22015-10-23 10:51:11 -07001153 * In the OF-DPA 2.0 pipeline, versatile forwarding objectives go to the
1154 * ACL table.
1155 * @param fwd the forwarding objective of type 'versatile'
1156 * @return a collection of flow rules to be sent to the switch. An empty
1157 * collection may be returned if there is a problem in processing
1158 * the flow rule
1159 */
Saurav Das52025962016-01-28 22:30:01 -08001160 protected Collection<FlowRule> processVersatile(ForwardingObjective fwd) {
Saurav Das018605f2017-02-18 14:05:44 -08001161 log.debug("Processing versatile forwarding objective:{} in dev:{}",
Saurav Dasd2fded02016-12-02 15:43:47 -08001162 fwd.id(), deviceId);
Saurav Das822c4e22015-10-23 10:51:11 -07001163
Saurav Das822c4e22015-10-23 10:51:11 -07001164 if (fwd.nextId() == null && fwd.treatment() == null) {
1165 log.error("Forwarding objective {} from {} must contain "
1166 + "nextId or Treatment", fwd.selector(), fwd.appId());
Zsolt Haraszti9faab752016-02-17 15:55:20 -08001167 fail(fwd, ObjectiveError.BADPARAMS);
Saurav Das822c4e22015-10-23 10:51:11 -07001168 return Collections.emptySet();
1169 }
Saurav Das49cb5a12016-01-16 22:54:07 -08001170
Charles Chanab591602019-01-22 17:25:04 -08001171 TrafficSelector.Builder sbuilder = versatileSelectorBuilder(fwd);
1172 TrafficTreatment.Builder ttBuilder = versatileTreatmentBuilder(fwd);
1173 if (ttBuilder == null) {
1174 return Collections.emptySet();
1175 }
1176
1177 FlowRule.Builder ruleBuilder = DefaultFlowRule.builder()
1178 .fromApp(fwd.appId())
1179 .withPriority(fwd.priority())
1180 .forDevice(deviceId)
1181 .withSelector(sbuilder.build())
1182 .withTreatment(ttBuilder.build())
1183 .makePermanent()
1184 .forTable(ACL_TABLE);
1185 return Collections.singletonList(ruleBuilder.build());
1186 }
1187
1188 /**
1189 * Helper function to create traffic selector builder for versatile forwarding objectives.
1190 *
1191 * @param fwd original forwarding objective
1192 * @return selector builder for the flow rule
1193 */
1194 protected TrafficSelector.Builder versatileSelectorBuilder(ForwardingObjective fwd) {
Saurav Das77b5e902016-01-27 17:01:59 -08001195 TrafficSelector.Builder sbuilder = DefaultTrafficSelector.builder();
1196 fwd.selector().criteria().forEach(criterion -> {
1197 if (criterion instanceof VlanIdCriterion) {
1198 VlanId vlanId = ((VlanIdCriterion) criterion).vlanId();
1199 // ensure that match does not include vlan = NONE as OF-DPA does not
1200 // match untagged packets this way in the ACL table.
1201 if (vlanId.equals(VlanId.NONE)) {
1202 return;
1203 }
Charles Chand1172632017-03-15 17:33:09 -07001204 if (requireVlanExtensions()) {
1205 OfdpaMatchVlanVid ofdpaMatchVlanVid = new OfdpaMatchVlanVid(vlanId);
1206 sbuilder.extension(ofdpaMatchVlanVid, deviceId);
1207 } else {
1208 sbuilder.matchVlanId(vlanId);
1209 }
Charles Chan09bf2692018-01-11 11:48:18 -08001210 } else if (criterion instanceof Icmpv6TypeCriterion) {
1211 byte icmpv6Type = (byte) ((Icmpv6TypeCriterion) criterion).icmpv6Type();
1212 sbuilder.matchIcmpv6Type(icmpv6Type);
1213 } else if (criterion instanceof Icmpv6CodeCriterion) {
1214 byte icmpv6Code = (byte) ((Icmpv6CodeCriterion) criterion).icmpv6Code();
1215 sbuilder.matchIcmpv6Type(icmpv6Code);
Charles Chand9e47c62017-10-05 15:17:15 -07001216 } else if (criterion instanceof TcpPortCriterion || criterion instanceof UdpPortCriterion) {
1217 // FIXME: QMX switches do not support L4 dst port matching in ACL table.
1218 // Currently L4 dst port matching is only used by DHCP relay feature
1219 // and therefore is safe to be replaced with L4 src port matching.
1220 // We need to revisit this if L4 dst port is used for other purpose in the future.
1221 if (!supportIpv6L4Dst() && isIpv6(fwd.selector())) {
Charles Chand9e47c62017-10-05 15:17:15 -07001222 switch (criterion.type()) {
1223 case UDP_DST:
Charles Chand9e47c62017-10-05 15:17:15 -07001224 case UDP_DST_MASKED:
Charles Chand9e47c62017-10-05 15:17:15 -07001225 case TCP_DST:
Charles Chand9e47c62017-10-05 15:17:15 -07001226 case TCP_DST_MASKED:
Charles Chand9e47c62017-10-05 15:17:15 -07001227 break;
1228 default:
1229 sbuilder.add(criterion);
1230 }
1231 } else {
1232 sbuilder.add(criterion);
1233 }
Saurav Das77b5e902016-01-27 17:01:59 -08001234 } else {
1235 sbuilder.add(criterion);
1236 }
1237 });
Charles Chanab591602019-01-22 17:25:04 -08001238 return sbuilder;
1239 }
Saurav Das77b5e902016-01-27 17:01:59 -08001240
Charles Chanab591602019-01-22 17:25:04 -08001241 /**
1242 * Helper function to create traffic treatment builder for versatile forwarding objectives.
1243 *
1244 * @param fwd original forwarding objective
1245 * @return treatment builder for the flow rule, or null if there is an error.
1246 */
1247 protected TrafficTreatment.Builder versatileTreatmentBuilder(ForwardingObjective fwd) {
Saurav Das822c4e22015-10-23 10:51:11 -07001248 // XXX driver does not currently do type checking as per Tables 65-67 in
1249 // OFDPA 2.0 spec. The only allowed treatment is a punt to the controller.
Saurav Das49cb5a12016-01-16 22:54:07 -08001250 TrafficTreatment.Builder ttBuilder = DefaultTrafficTreatment.builder();
1251 if (fwd.treatment() != null) {
1252 for (Instruction ins : fwd.treatment().allInstructions()) {
1253 if (ins instanceof OutputInstruction) {
1254 OutputInstruction o = (OutputInstruction) ins;
Ray Milkeyfe0e0852018-01-18 11:14:05 -08001255 if (PortNumber.CONTROLLER.equals(o.port())) {
Saurav Das49cb5a12016-01-16 22:54:07 -08001256 ttBuilder.add(o);
1257 } else {
1258 log.warn("Only allowed treatments in versatile forwarding "
1259 + "objectives are punts to the controller");
1260 }
Charles Chanf76de302018-06-15 18:54:18 -07001261 } else if (ins instanceof NoActionInstruction) {
1262 // No action is allowed and nothing needs to be done
Saurav Das49cb5a12016-01-16 22:54:07 -08001263 } else {
1264 log.warn("Cannot process instruction in versatile fwd {}", ins);
1265 }
Saurav Das822c4e22015-10-23 10:51:11 -07001266 }
Charles Chan2df0e8a2017-01-09 11:45:08 -08001267 if (fwd.treatment().clearedDeferred()) {
1268 ttBuilder.wipeDeferred();
1269 }
Saurav Das822c4e22015-10-23 10:51:11 -07001270 }
Saurav Das822c4e22015-10-23 10:51:11 -07001271 if (fwd.nextId() != null) {
Charles Chanab591602019-01-22 17:25:04 -08001272 // Override case
Saurav Das49cb5a12016-01-16 22:54:07 -08001273 NextGroup next = getGroupForNextObjective(fwd.nextId());
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +05301274 if (next == null) {
1275 fail(fwd, ObjectiveError.BADPARAMS);
Charles Chanab591602019-01-22 17:25:04 -08001276 return null;
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +05301277 }
Saurav Das49cb5a12016-01-16 22:54:07 -08001278 List<Deque<GroupKey>> gkeys = appKryo.deserialize(next.data());
1279 // we only need the top level group's key to point the flow to it
1280 Group group = groupService.getGroup(deviceId, gkeys.get(0).peekFirst());
1281 if (group == null) {
1282 log.warn("Group with key:{} for next-id:{} not found in dev:{}",
Charles Chanab591602019-01-22 17:25:04 -08001283 gkeys.get(0).peekFirst(), fwd.nextId(), deviceId);
Saurav Das49cb5a12016-01-16 22:54:07 -08001284 fail(fwd, ObjectiveError.GROUPMISSING);
Charles Chanab591602019-01-22 17:25:04 -08001285 return null;
Saurav Das49cb5a12016-01-16 22:54:07 -08001286 }
1287 ttBuilder.deferred().group(group.id());
Saurav Das822c4e22015-10-23 10:51:11 -07001288 }
Charles Chanab591602019-01-22 17:25:04 -08001289 return ttBuilder;
Saurav Das822c4e22015-10-23 10:51:11 -07001290 }
1291
1292 /**
1293 * In the OF-DPA 2.0 pipeline, specific forwarding refers to the IP table
Saurav Das8a0732e2015-11-20 15:27:53 -08001294 * (unicast or multicast) or the L2 table (mac + vlan) or the MPLS table.
Saurav Das822c4e22015-10-23 10:51:11 -07001295 *
1296 * @param fwd the forwarding objective of type 'specific'
1297 * @return a collection of flow rules. Typically there will be only one
1298 * for this type of forwarding objective. An empty set may be
1299 * returned if there is an issue in processing the objective.
1300 */
Charles Chan50d900c2018-03-02 13:26:22 -08001301 private Collection<FlowRule> processSpecific(ForwardingObjective fwd) {
Saurav Das25190812016-05-27 13:54:07 -07001302 log.debug("Processing specific fwd objective:{} in dev:{} with next:{}",
Saurav Das4ce45962015-11-24 23:21:05 -08001303 fwd.id(), deviceId, fwd.nextId());
1304 boolean isEthTypeObj = isSupportedEthTypeObjective(fwd);
1305 boolean isEthDstObj = isSupportedEthDstObjective(fwd);
1306
1307 if (isEthTypeObj) {
1308 return processEthTypeSpecific(fwd);
1309 } else if (isEthDstObj) {
1310 return processEthDstSpecific(fwd);
1311 } else {
1312 log.warn("processSpecific: Unsupported forwarding objective "
1313 + "criteria fwd:{} in dev:{}", fwd.nextId(), deviceId);
Saurav Das822c4e22015-10-23 10:51:11 -07001314 fail(fwd, ObjectiveError.UNSUPPORTED);
1315 return Collections.emptySet();
1316 }
Saurav Das4ce45962015-11-24 23:21:05 -08001317 }
1318
Saurav Das4ce45962015-11-24 23:21:05 -08001319 /**
1320 * Handles forwarding rules to the IP and MPLS tables.
1321 *
1322 * @param fwd the forwarding objective
1323 * @return A collection of flow rules, or an empty set
1324 */
1325 protected Collection<FlowRule> processEthTypeSpecific(ForwardingObjective fwd) {
Charles Chancad338a2016-09-16 18:03:11 -07001326 return processEthTypeSpecificInternal(fwd, false, ACL_TABLE);
Charles Chanf9e98652016-09-07 16:54:23 -07001327 }
1328
1329 /**
1330 * Internal implementation of processEthTypeSpecific.
1331 * <p>
1332 * Wildcarded IPv4_DST is not supported in OFDPA i12. Therefore, we break
1333 * the rule into 0.0.0.0/1 and 128.0.0.0/1.
1334 * The allowDefaultRoute must be set to false for OFDPA i12.
1335 * </p>
1336 *
1337 * @param fwd the forwarding objective
1338 * @param allowDefaultRoute allow wildcarded IPv4_DST or not
Ray Milkey0bb1e102016-11-10 14:51:27 -08001339 * @param mplsNextTable next MPLS table
Charles Chanf9e98652016-09-07 16:54:23 -07001340 * @return A collection of flow rules, or an empty set
1341 */
1342 protected Collection<FlowRule> processEthTypeSpecificInternal(ForwardingObjective fwd,
Charles Chancad338a2016-09-16 18:03:11 -07001343 boolean allowDefaultRoute,
1344 int mplsNextTable) {
Saurav Das4ce45962015-11-24 23:21:05 -08001345 TrafficSelector selector = fwd.selector();
1346 EthTypeCriterion ethType =
1347 (EthTypeCriterion) selector.getCriterion(Criterion.Type.ETH_TYPE);
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001348 boolean emptyGroup = false;
Charles Chan188ebf52015-12-23 00:15:11 -08001349 int forTableId;
Saurav Das8a0732e2015-11-20 15:27:53 -08001350 TrafficSelector.Builder filteredSelector = DefaultTrafficSelector.builder();
Charles Chan14967c22015-12-07 11:11:50 -08001351 TrafficTreatment.Builder tb = DefaultTrafficTreatment.builder();
Flavio Castroe10fa242016-01-15 12:43:51 -08001352 TrafficSelector.Builder complementarySelector = DefaultTrafficSelector.builder();
Charles Chan14967c22015-12-07 11:11:50 -08001353
Saurav Das8a0732e2015-11-20 15:27:53 -08001354 if (ethType.ethType().toShort() == Ethernet.TYPE_IPV4) {
Pier Luigi3bfe32c2017-01-30 09:47:36 -08001355 if (buildIpv4Selector(filteredSelector, complementarySelector, fwd, allowDefaultRoute) < 0) {
1356 return Collections.emptyList();
1357 }
1358 // We need to set properly the next table
Flavio Castroe10fa242016-01-15 12:43:51 -08001359 IpPrefix ipv4Dst = ((IPCriterion) selector.getCriterion(Criterion.Type.IPV4_DST)).ip();
Charles Chan5b9df8d2016-03-28 22:21:40 -07001360 if (ipv4Dst.isMulticast()) {
Charles Chan5b9df8d2016-03-28 22:21:40 -07001361 forTableId = MULTICAST_ROUTING_TABLE;
Flavio Castroe10fa242016-01-15 12:43:51 -08001362 } else {
Charles Chan5b9df8d2016-03-28 22:21:40 -07001363 forTableId = UNICAST_ROUTING_TABLE;
Flavio Castroe10fa242016-01-15 12:43:51 -08001364 }
Charles Chan50d900c2018-03-02 13:26:22 -08001365
Charles Chan236653d2018-03-05 11:28:23 -08001366 // TODO decrementing IP ttl is done automatically for routing, this
Charles Chan50d900c2018-03-02 13:26:22 -08001367 // action is ignored or rejected in ofdpa as it is not fully implemented
Charles Chan236653d2018-03-05 11:28:23 -08001368 // if (fwd.treatment() != null) {
1369 // for (Instruction instr : fwd.treatment().allInstructions()) {
1370 // if (instr instanceof L3ModificationInstruction &&
1371 // ((L3ModificationInstruction) instr).subtype() == L3SubType.DEC_TTL) {
1372 // tb.deferred().add(instr);
1373 // }
1374 // }
1375 // }
1376
Pier Ventree0ae7a32016-11-23 09:57:42 -08001377 } else if (ethType.ethType().toShort() == Ethernet.TYPE_IPV6) {
1378 if (buildIpv6Selector(filteredSelector, fwd) < 0) {
1379 return Collections.emptyList();
1380 }
Kalhee Kim3d26bf02017-10-04 14:59:11 +00001381 //We need to set the proper next table
1382 IpPrefix ipv6Dst = ((IPCriterion) selector.getCriterion(Criterion.Type.IPV6_DST)).ip();
1383 if (ipv6Dst.isMulticast()) {
1384 forTableId = MULTICAST_ROUTING_TABLE;
1385 } else {
1386 forTableId = UNICAST_ROUTING_TABLE;
1387 }
1388
Charles Chan236653d2018-03-05 11:28:23 -08001389 // TODO decrementing IP ttl is done automatically for routing, this
Charles Chan50d900c2018-03-02 13:26:22 -08001390 // action is ignored or rejected in ofdpa as it is not fully implemented
Charles Chan236653d2018-03-05 11:28:23 -08001391 // if (fwd.treatment() != null) {
1392 // for (Instruction instr : fwd.treatment().allInstructions()) {
1393 // if (instr instanceof L3ModificationInstruction &&
1394 // ((L3ModificationInstruction) instr).subtype() == L3SubType.DEC_TTL) {
1395 // tb.deferred().add(instr);
1396 // }
1397 // }
1398 // }
Saurav Das8a0732e2015-11-20 15:27:53 -08001399 } else {
1400 filteredSelector
1401 .matchEthType(Ethernet.MPLS_UNICAST)
1402 .matchMplsLabel(((MplsCriterion)
1403 selector.getCriterion(Criterion.Type.MPLS_LABEL)).label());
1404 MplsBosCriterion bos = (MplsBosCriterion) selector
Pier Ventre140a8942016-11-02 07:26:38 -07001405 .getCriterion(MPLS_BOS);
Alex Yashchuk4caa8e82017-12-08 17:40:05 +02001406 if (bos != null && requireMplsBosMatch()) {
Saurav Das8a0732e2015-11-20 15:27:53 -08001407 filteredSelector.matchMplsBos(bos.mplsBos());
1408 }
1409 forTableId = MPLS_TABLE_1;
Saurav Das4ce45962015-11-24 23:21:05 -08001410 log.debug("processing MPLS specific forwarding objective {} -> next:{}"
1411 + " in dev {}", fwd.id(), fwd.nextId(), deviceId);
Saurav Das822c4e22015-10-23 10:51:11 -07001412
Charles Chan14967c22015-12-07 11:11:50 -08001413 if (fwd.treatment() != null) {
1414 for (Instruction instr : fwd.treatment().allInstructions()) {
1415 if (instr instanceof L2ModificationInstruction &&
1416 ((L2ModificationInstruction) instr).subtype() == L2SubType.MPLS_POP) {
Pier Luigi3bfe32c2017-01-30 09:47:36 -08001417 // OF-DPA does not pop in MPLS table in some cases. For the L3 VPN, it requires
Saurav Das9c705342017-01-06 11:36:01 -08001418 // setting the MPLS_TYPE so pop can happen down the pipeline
Alex Yashchuk4caa8e82017-12-08 17:40:05 +02001419 if (requireMplsPop()) {
1420 if (mplsNextTable == MPLS_TYPE_TABLE && isNotMplsBos(selector)) {
1421 tb.immediate().popMpls();
1422 }
1423 } else {
1424 // Skip mpls pop action for mpls_unicast label
1425 if (instr instanceof ModMplsHeaderInstruction &&
1426 !((ModMplsHeaderInstruction) instr).ethernetType()
1427 .equals(EtherType.MPLS_UNICAST.ethType())) {
1428 tb.immediate().add(instr);
1429 }
Pier Luigi3bfe32c2017-01-30 09:47:36 -08001430 }
Charles Chan14967c22015-12-07 11:11:50 -08001431 }
Alex Yashchuk4caa8e82017-12-08 17:40:05 +02001432
1433 if (requireMplsTtlModification()) {
1434 if (instr instanceof L3ModificationInstruction &&
1435 ((L3ModificationInstruction) instr).subtype() == L3SubType.DEC_TTL) {
1436 // FIXME Should modify the app to send the correct DEC_MPLS_TTL instruction
1437 tb.immediate().decMplsTtl();
1438 }
1439 if (instr instanceof L3ModificationInstruction &&
1440 ((L3ModificationInstruction) instr).subtype() == L3SubType.TTL_IN) {
1441 tb.immediate().add(instr);
1442 }
Charles Chan14967c22015-12-07 11:11:50 -08001443 }
Saurav Das8a0732e2015-11-20 15:27:53 -08001444 }
1445 }
1446 }
Saurav Das822c4e22015-10-23 10:51:11 -07001447
1448 if (fwd.nextId() != null) {
Saurav Das423fe2b2015-12-04 10:52:59 -08001449 NextGroup next = getGroupForNextObjective(fwd.nextId());
1450 if (next != null) {
1451 List<Deque<GroupKey>> gkeys = appKryo.deserialize(next.data());
1452 // we only need the top level group's key to point the flow to it
1453 Group group = groupService.getGroup(deviceId, gkeys.get(0).peekFirst());
1454 if (group == null) {
1455 log.warn("Group with key:{} for next-id:{} not found in dev:{}",
1456 gkeys.get(0).peekFirst(), fwd.nextId(), deviceId);
1457 fail(fwd, ObjectiveError.GROUPMISSING);
1458 return Collections.emptySet();
1459 }
Saurav Dasa4020382018-02-14 14:14:54 -08001460 if (isNotMplsBos(selector) && group.type().equals(SELECT)) {
1461 log.warn("SR CONTINUE case cannot be handled as MPLS ECMP "
1462 + "is not implemented in OF-DPA yet. Aborting flow {} -> next:{} "
1463 + "in this device {}", fwd.id(), fwd.nextId(), deviceId);
1464 fail(fwd, ObjectiveError.FLOWINSTALLATIONFAILED);
1465 return Collections.emptySet();
1466 }
Saurav Das423fe2b2015-12-04 10:52:59 -08001467 tb.deferred().group(group.id());
Saurav Dasc568c342018-01-25 09:49:01 -08001468 // retrying flows may be necessary due to bug CORD-554
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001469 if (gkeys.size() == 1 && gkeys.get(0).size() == 1) {
Saurav Dasc568c342018-01-25 09:49:01 -08001470 if (shouldRetry()) {
1471 log.warn("Found empty group 0x{} in dev:{} .. will retry fwd:{}",
1472 Integer.toHexString(group.id().id()), deviceId,
1473 fwd.id());
1474 emptyGroup = true;
1475 }
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001476 }
Saurav Das25190812016-05-27 13:54:07 -07001477 } else {
1478 log.warn("Cannot find group for nextId:{} in dev:{}. Aborting fwd:{}",
1479 fwd.nextId(), deviceId, fwd.id());
1480 fail(fwd, ObjectiveError.FLOWINSTALLATIONFAILED);
1481 return Collections.emptySet();
Saurav Das822c4e22015-10-23 10:51:11 -07001482 }
Saurav Das822c4e22015-10-23 10:51:11 -07001483 }
Charles Chancad338a2016-09-16 18:03:11 -07001484
1485 if (forTableId == MPLS_TABLE_1) {
Pier Ventre140a8942016-11-02 07:26:38 -07001486 if (mplsNextTable == MPLS_L3_TYPE_TABLE) {
Charles Chancad338a2016-09-16 18:03:11 -07001487 Ofdpa3SetMplsType setMplsType = new Ofdpa3SetMplsType(Ofdpa3MplsType.L3_PHP);
Saurav Das9c705342017-01-06 11:36:01 -08001488 // set mpls type as apply_action
1489 tb.immediate().extension(setMplsType, deviceId);
Charles Chancad338a2016-09-16 18:03:11 -07001490 }
1491 tb.transition(mplsNextTable);
1492 } else {
1493 tb.transition(ACL_TABLE);
1494 }
1495
Andrea Campanellad980c6d2018-04-30 11:48:55 +02001496 if (fwd.treatment() != null && fwd.treatment().clearedDeferred()) {
1497 if (supportsUnicastBlackHole()) {
1498 tb.wipeDeferred();
1499 } else {
1500 log.warn("Clear Deferred is not supported Unicast Routing Table on device {}", deviceId);
1501 return Collections.emptySet();
1502 }
1503 }
1504
Saurav Das822c4e22015-10-23 10:51:11 -07001505 FlowRule.Builder ruleBuilder = DefaultFlowRule.builder()
1506 .fromApp(fwd.appId())
1507 .withPriority(fwd.priority())
1508 .forDevice(deviceId)
Saurav Das8a0732e2015-11-20 15:27:53 -08001509 .withSelector(filteredSelector.build())
1510 .withTreatment(tb.build())
1511 .forTable(forTableId);
Saurav Das822c4e22015-10-23 10:51:11 -07001512
1513 if (fwd.permanent()) {
1514 ruleBuilder.makePermanent();
1515 } else {
1516 ruleBuilder.makeTemporary(fwd.timeout());
1517 }
Flavio Castroe10fa242016-01-15 12:43:51 -08001518 Collection<FlowRule> flowRuleCollection = new ArrayList<>();
1519 flowRuleCollection.add(ruleBuilder.build());
Pier Luigi3bfe32c2017-01-30 09:47:36 -08001520 if (!allowDefaultRoute) {
Pier Ventree0ae7a32016-11-23 09:57:42 -08001521 flowRuleCollection.add(
1522 defaultRoute(fwd, complementarySelector, forTableId, tb)
1523 );
Flavio Castroe10fa242016-01-15 12:43:51 -08001524 log.debug("Default rule 0.0.0.0/0 is being installed two rules");
1525 }
Saurav Dasc568c342018-01-25 09:49:01 -08001526
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001527 if (emptyGroup) {
pier8b3aef42019-03-11 15:14:02 -07001528 retryExecutorService.schedule(new RetryFlows(fwd, flowRuleCollection),
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001529 RETRY_MS, TimeUnit.MILLISECONDS);
1530 }
Flavio Castroe10fa242016-01-15 12:43:51 -08001531 return flowRuleCollection;
Saurav Das822c4e22015-10-23 10:51:11 -07001532 }
1533
Charles Chan50d900c2018-03-02 13:26:22 -08001534 private int buildIpv4Selector(TrafficSelector.Builder builderToUpdate,
Pier Luigi3bfe32c2017-01-30 09:47:36 -08001535 TrafficSelector.Builder extBuilder,
1536 ForwardingObjective fwd,
1537 boolean allowDefaultRoute) {
1538 TrafficSelector selector = fwd.selector();
1539
1540 IpPrefix ipv4Dst = ((IPCriterion) selector.getCriterion(Criterion.Type.IPV4_DST)).ip();
1541 if (ipv4Dst.isMulticast()) {
1542 if (ipv4Dst.prefixLength() != 32) {
1543 log.warn("Multicast specific forwarding objective can only be /32");
1544 fail(fwd, ObjectiveError.BADPARAMS);
1545 return -1;
1546 }
1547 VlanId assignedVlan = readVlanFromSelector(fwd.meta());
1548 if (assignedVlan == null) {
1549 log.warn("VLAN ID required by multicast specific fwd obj is missing. Abort.");
1550 fail(fwd, ObjectiveError.BADPARAMS);
1551 return -1;
1552 }
Charles Chand1172632017-03-15 17:33:09 -07001553 if (requireVlanExtensions()) {
1554 OfdpaMatchVlanVid ofdpaMatchVlanVid = new OfdpaMatchVlanVid(assignedVlan);
1555 builderToUpdate.extension(ofdpaMatchVlanVid, deviceId);
1556 } else {
1557 builderToUpdate.matchVlanId(assignedVlan);
1558 }
Pier Luigi3bfe32c2017-01-30 09:47:36 -08001559 builderToUpdate.matchEthType(Ethernet.TYPE_IPV4).matchIPDst(ipv4Dst);
1560 log.debug("processing IPv4 multicast specific forwarding objective {} -> next:{}"
1561 + " in dev:{}", fwd.id(), fwd.nextId(), deviceId);
1562 } else {
1563 if (ipv4Dst.prefixLength() == 0) {
1564 if (allowDefaultRoute) {
1565 // The entire IPV4_DST field is wildcarded intentionally
1566 builderToUpdate.matchEthType(Ethernet.TYPE_IPV4);
1567 } else {
1568 // NOTE: The switch does not support matching 0.0.0.0/0
1569 // Split it into 0.0.0.0/1 and 128.0.0.0/1
1570 builderToUpdate.matchEthType(Ethernet.TYPE_IPV4)
1571 .matchIPDst(IpPrefix.valueOf("0.0.0.0/1"));
1572 extBuilder.matchEthType(Ethernet.TYPE_IPV4)
1573 .matchIPDst(IpPrefix.valueOf("128.0.0.0/1"));
1574 }
1575 } else {
1576 builderToUpdate.matchEthType(Ethernet.TYPE_IPV4).matchIPDst(ipv4Dst);
1577 }
1578 log.debug("processing IPv4 unicast specific forwarding objective {} -> next:{}"
1579 + " in dev:{}", fwd.id(), fwd.nextId(), deviceId);
1580 }
1581 return 0;
1582 }
1583
1584 /**
1585 * Helper method to build Ipv6 selector using the selector provided by
1586 * a forwarding objective.
1587 *
1588 * @param builderToUpdate the builder to update
1589 * @param fwd the selector to read
1590 * @return 0 if the update ends correctly. -1 if the matches
1591 * are not yet supported
1592 */
Charles Chan50d900c2018-03-02 13:26:22 -08001593 int buildIpv6Selector(TrafficSelector.Builder builderToUpdate,
Pier Luigi3bfe32c2017-01-30 09:47:36 -08001594 ForwardingObjective fwd) {
1595
1596 TrafficSelector selector = fwd.selector();
1597
1598 IpPrefix ipv6Dst = ((IPCriterion) selector.getCriterion(Criterion.Type.IPV6_DST)).ip();
1599 if (ipv6Dst.isMulticast()) {
Julia Ferguson65428c32017-08-10 18:15:24 +00001600 if (ipv6Dst.prefixLength() != IpAddress.INET6_BIT_LENGTH) {
1601 log.warn("Multicast specific forwarding objective can only be /128");
1602 fail(fwd, ObjectiveError.BADPARAMS);
1603 return -1;
1604 }
1605 VlanId assignedVlan = readVlanFromSelector(fwd.meta());
1606 if (assignedVlan == null) {
1607 log.warn("VLAN ID required by multicast specific fwd obj is missing. Abort.");
1608 fail(fwd, ObjectiveError.BADPARAMS);
1609 return -1;
1610 }
1611 if (requireVlanExtensions()) {
1612 OfdpaMatchVlanVid ofdpaMatchVlanVid = new OfdpaMatchVlanVid(assignedVlan);
1613 builderToUpdate.extension(ofdpaMatchVlanVid, deviceId);
1614 } else {
1615 builderToUpdate.matchVlanId(assignedVlan);
1616 }
1617 builderToUpdate.matchEthType(Ethernet.TYPE_IPV6).matchIPv6Dst(ipv6Dst);
1618 log.debug("processing IPv6 multicast specific forwarding objective {} -> next:{}"
1619 + " in dev:{}", fwd.id(), fwd.nextId(), deviceId);
1620 } else {
1621 if (ipv6Dst.prefixLength() != 0) {
1622 builderToUpdate.matchIPv6Dst(ipv6Dst);
1623 }
Pier Luigi3bfe32c2017-01-30 09:47:36 -08001624 builderToUpdate.matchEthType(Ethernet.TYPE_IPV6);
1625 log.debug("processing IPv6 unicast specific forwarding objective {} -> next:{}"
1626 + " in dev:{}", fwd.id(), fwd.nextId(), deviceId);
Julia Ferguson65428c32017-08-10 18:15:24 +00001627 }
Pier Luigi3bfe32c2017-01-30 09:47:36 -08001628 return 0;
1629 }
1630
Charles Chan50d900c2018-03-02 13:26:22 -08001631 FlowRule defaultRoute(ForwardingObjective fwd,
Pier Ventree0ae7a32016-11-23 09:57:42 -08001632 TrafficSelector.Builder complementarySelector,
1633 int forTableId,
1634 TrafficTreatment.Builder tb) {
1635 FlowRule.Builder rule = DefaultFlowRule.builder()
1636 .fromApp(fwd.appId())
1637 .withPriority(fwd.priority())
1638 .forDevice(deviceId)
1639 .withSelector(complementarySelector.build())
1640 .withTreatment(tb.build())
1641 .forTable(forTableId);
1642 if (fwd.permanent()) {
1643 rule.makePermanent();
1644 } else {
1645 rule.makeTemporary(fwd.timeout());
1646 }
1647 return rule.build();
1648 }
1649
Saurav Das4ce45962015-11-24 23:21:05 -08001650 /**
1651 * Handles forwarding rules to the L2 bridging table. Flow actions are not
1652 * allowed in the bridging table - instead we use L2 Interface group or
1653 * L2 flood group
1654 *
1655 * @param fwd the forwarding objective
1656 * @return A collection of flow rules, or an empty set
1657 */
1658 protected Collection<FlowRule> processEthDstSpecific(ForwardingObjective fwd) {
1659 List<FlowRule> rules = new ArrayList<>();
1660
1661 // Build filtered selector
1662 TrafficSelector selector = fwd.selector();
1663 EthCriterion ethCriterion = (EthCriterion) selector
1664 .getCriterion(Criterion.Type.ETH_DST);
1665 VlanIdCriterion vlanIdCriterion = (VlanIdCriterion) selector
1666 .getCriterion(Criterion.Type.VLAN_VID);
1667
1668 if (vlanIdCriterion == null) {
1669 log.warn("Forwarding objective for bridging requires vlan. Not "
1670 + "installing fwd:{} in dev:{}", fwd.id(), deviceId);
1671 fail(fwd, ObjectiveError.BADPARAMS);
1672 return Collections.emptySet();
1673 }
1674
1675 TrafficSelector.Builder filteredSelectorBuilder =
1676 DefaultTrafficSelector.builder();
Yi Tseng3a77b4f2017-02-06 15:02:17 -08001677
1678 if (!ethCriterion.mac().equals(NONE) &&
1679 !ethCriterion.mac().equals(BROADCAST)) {
Saurav Das4ce45962015-11-24 23:21:05 -08001680 filteredSelectorBuilder.matchEthDst(ethCriterion.mac());
1681 log.debug("processing L2 forwarding objective:{} -> next:{} in dev:{}",
1682 fwd.id(), fwd.nextId(), deviceId);
1683 } else {
Yi Tseng3a77b4f2017-02-06 15:02:17 -08001684 // Use wildcard DST_MAC if the MacAddress is None or Broadcast
Saurav Das4ce45962015-11-24 23:21:05 -08001685 log.debug("processing L2 Broadcast forwarding objective:{} -> next:{} "
1686 + "in dev:{} for vlan:{}",
1687 fwd.id(), fwd.nextId(), deviceId, vlanIdCriterion.vlanId());
1688 }
Charles Chand1172632017-03-15 17:33:09 -07001689 if (requireVlanExtensions()) {
1690 OfdpaMatchVlanVid ofdpaMatchVlanVid = new OfdpaMatchVlanVid(vlanIdCriterion.vlanId());
1691 filteredSelectorBuilder.extension(ofdpaMatchVlanVid, deviceId);
1692 } else {
1693 filteredSelectorBuilder.matchVlanId(vlanIdCriterion.vlanId());
1694 }
Saurav Das4ce45962015-11-24 23:21:05 -08001695 TrafficSelector filteredSelector = filteredSelectorBuilder.build();
1696
1697 if (fwd.treatment() != null) {
1698 log.warn("Ignoring traffic treatment in fwd rule {} meant for L2 table"
1699 + "for dev:{}. Expecting only nextId", fwd.id(), deviceId);
1700 }
1701
1702 TrafficTreatment.Builder treatmentBuilder = DefaultTrafficTreatment.builder();
1703 if (fwd.nextId() != null) {
Saurav Das423fe2b2015-12-04 10:52:59 -08001704 NextGroup next = getGroupForNextObjective(fwd.nextId());
Saurav Das4ce45962015-11-24 23:21:05 -08001705 if (next != null) {
1706 List<Deque<GroupKey>> gkeys = appKryo.deserialize(next.data());
1707 // we only need the top level group's key to point the flow to it
1708 Group group = groupService.getGroup(deviceId, gkeys.get(0).peekFirst());
1709 if (group != null) {
1710 treatmentBuilder.deferred().group(group.id());
1711 } else {
1712 log.warn("Group with key:{} for next-id:{} not found in dev:{}",
1713 gkeys.get(0).peekFirst(), fwd.nextId(), deviceId);
1714 fail(fwd, ObjectiveError.GROUPMISSING);
1715 return Collections.emptySet();
1716 }
1717 }
1718 }
1719 treatmentBuilder.immediate().transition(ACL_TABLE);
1720 TrafficTreatment filteredTreatment = treatmentBuilder.build();
1721
1722 // Build bridging table entries
1723 FlowRule.Builder flowRuleBuilder = DefaultFlowRule.builder();
1724 flowRuleBuilder.fromApp(fwd.appId())
1725 .withPriority(fwd.priority())
1726 .forDevice(deviceId)
1727 .withSelector(filteredSelector)
1728 .withTreatment(filteredTreatment)
1729 .forTable(BRIDGING_TABLE);
1730 if (fwd.permanent()) {
1731 flowRuleBuilder.makePermanent();
1732 } else {
1733 flowRuleBuilder.makeTemporary(fwd.timeout());
1734 }
1735 rules.add(flowRuleBuilder.build());
1736 return rules;
1737 }
1738
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001739 //////////////////////////////////////
1740 // Helper Methods and Classes
1741 //////////////////////////////////////
1742
1743 private boolean isSupportedEthTypeObjective(ForwardingObjective fwd) {
1744 TrafficSelector selector = fwd.selector();
1745 EthTypeCriterion ethType = (EthTypeCriterion) selector
1746 .getCriterion(Criterion.Type.ETH_TYPE);
1747 return !((ethType == null) ||
1748 ((ethType.ethType().toShort() != Ethernet.TYPE_IPV4) &&
Pier Ventree0ae7a32016-11-23 09:57:42 -08001749 (ethType.ethType().toShort() != Ethernet.MPLS_UNICAST)) &&
1750 (ethType.ethType().toShort() != Ethernet.TYPE_IPV6));
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001751 }
1752
1753 private boolean isSupportedEthDstObjective(ForwardingObjective fwd) {
1754 TrafficSelector selector = fwd.selector();
1755 EthCriterion ethDst = (EthCriterion) selector
1756 .getCriterion(Criterion.Type.ETH_DST);
1757 VlanIdCriterion vlanId = (VlanIdCriterion) selector
1758 .getCriterion(Criterion.Type.VLAN_VID);
1759 return !(ethDst == null && vlanId == null);
1760 }
1761
Charles Chan50d900c2018-03-02 13:26:22 -08001762 NextGroup getGroupForNextObjective(Integer nextId) {
Saurav Das423fe2b2015-12-04 10:52:59 -08001763 NextGroup next = flowObjectiveStore.getNextGroup(nextId);
1764 if (next != null) {
1765 List<Deque<GroupKey>> gkeys = appKryo.deserialize(next.data());
1766 if (gkeys != null && !gkeys.isEmpty()) {
1767 return next;
1768 } else {
1769 log.warn("Empty next group found in FlowObjective store for "
1770 + "next-id:{} in dev:{}", nextId, deviceId);
1771 }
1772 } else {
1773 log.warn("next-id {} not found in Flow objective store for dev:{}",
1774 nextId, deviceId);
1775 }
1776 return null;
1777 }
1778
Saurav Das24431192016-03-07 19:13:00 -08001779 @Override
1780 public List<String> getNextMappings(NextGroup nextGroup) {
1781 List<String> mappings = new ArrayList<>();
1782 List<Deque<GroupKey>> gkeys = appKryo.deserialize(nextGroup.data());
1783 for (Deque<GroupKey> gkd : gkeys) {
1784 Group lastGroup = null;
Yuta HIGUCHI2dce08a2017-04-20 21:57:48 -07001785 StringBuilder gchain = new StringBuilder();
Saurav Das24431192016-03-07 19:13:00 -08001786 for (GroupKey gk : gkd) {
1787 Group g = groupService.getGroup(deviceId, gk);
Saurav Das8be4e3a2016-03-11 17:19:07 -08001788 if (g == null) {
Saurav Das25190812016-05-27 13:54:07 -07001789 gchain.append(" NoGrp").append(" -->");
Saurav Das8be4e3a2016-03-11 17:19:07 -08001790 continue;
1791 }
1792 gchain.append(" 0x").append(Integer.toHexString(g.id().id()))
1793 .append(" -->");
Saurav Das24431192016-03-07 19:13:00 -08001794 lastGroup = g;
1795 }
1796 // add port information for last group in group-chain
Yuta HIGUCHI2dce08a2017-04-20 21:57:48 -07001797 List<Instruction> lastGroupIns = new ArrayList<>();
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001798 if (lastGroup != null && !lastGroup.buckets().buckets().isEmpty()) {
Saurav Das25190812016-05-27 13:54:07 -07001799 lastGroupIns = lastGroup.buckets().buckets().get(0)
1800 .treatment().allInstructions();
1801 }
1802 for (Instruction i: lastGroupIns) {
Saurav Das24431192016-03-07 19:13:00 -08001803 if (i instanceof OutputInstruction) {
Saurav Das8be4e3a2016-03-11 17:19:07 -08001804 gchain.append(" port:").append(((OutputInstruction) i).port());
Saurav Das24431192016-03-07 19:13:00 -08001805 }
1806 }
Saurav Das8be4e3a2016-03-11 17:19:07 -08001807 mappings.add(gchain.toString());
Saurav Das24431192016-03-07 19:13:00 -08001808 }
1809 return mappings;
1810 }
Charles Chan5b9df8d2016-03-28 22:21:40 -07001811
Saurav Dasa4020382018-02-14 14:14:54 -08001812 /**
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001813 * Utility class that retries sending flows a fixed number of times, even if
1814 * some of the attempts are successful. Used only for forwarding objectives.
1815 */
Alex Yashchuk4caa8e82017-12-08 17:40:05 +02001816 public final class RetryFlows implements Runnable {
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001817 int attempts = MAX_RETRY_ATTEMPTS;
1818 private Collection<FlowRule> retryFlows;
1819 private ForwardingObjective fwd;
1820
Charles Chan50d900c2018-03-02 13:26:22 -08001821 RetryFlows(ForwardingObjective fwd, Collection<FlowRule> retryFlows) {
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001822 this.fwd = fwd;
1823 this.retryFlows = retryFlows;
1824 }
1825
1826 @Override
1827 public void run() {
1828 log.info("RETRY FLOWS ATTEMPT# {} for fwd:{} rules:{}",
1829 MAX_RETRY_ATTEMPTS - attempts, fwd.id(), retryFlows.size());
pier8b3aef42019-03-11 15:14:02 -07001830 sendForwards(Collections.singletonList(Pair.of(fwd, retryFlows)));
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001831 if (--attempts > 0) {
pier8b3aef42019-03-11 15:14:02 -07001832 retryExecutorService.schedule(this, RETRY_MS, TimeUnit.MILLISECONDS);
1833 }
1834 }
1835 }
1836
1837 // Flow rules accumulator for reducing the number of transactions required to the devices.
1838 private final class ForwardingObjectiveAccumulator
1839 extends AbstractAccumulator<Pair<ForwardingObjective, Collection<FlowRule>>> {
1840
1841 ForwardingObjectiveAccumulator(int maxFwd, int maxBatchMS, int maxIdleMS) {
1842 super(TIMER, maxFwd, maxBatchMS, maxIdleMS);
1843 }
1844
1845 @Override
1846 public void processItems(List<Pair<ForwardingObjective, Collection<FlowRule>>> pairs) {
1847 // Triggers creation of a batch using the list of flowrules generated from fwdobjs.
1848 accumulatorExecutorService.execute(new FlowRulesBuilderTask(pairs));
1849 }
1850 }
1851
1852 // Task for building batch of flow rules in a separate thread.
1853 private final class FlowRulesBuilderTask implements Runnable {
1854 private final List<Pair<ForwardingObjective, Collection<FlowRule>>> pairs;
1855
1856 FlowRulesBuilderTask(List<Pair<ForwardingObjective, Collection<FlowRule>>> pairs) {
1857 this.pairs = pairs;
1858 }
1859
1860 @Override
1861 public void run() {
1862 try {
1863 sendForwards(pairs);
1864 } catch (Exception e) {
1865 log.warn("Unable to send forwards", e);
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001866 }
1867 }
1868 }
1869
Saurav Das822c4e22015-10-23 10:51:11 -07001870}