blob: d33442122985f03a82596f2507c49bef6aa68872 [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(
pier6aef5b72019-06-10 17:10:26 +0200550 portCriterion, vidCriterion, assignedVlan, applicationId, install);
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
pier6aef5b72019-06-10 17:10:26 +0200598 * @param install indicates whether to add or remove the objective
Charles Chan66291502018-03-02 16:43:28 -0800599 * @return stages of flow rules for port-vlan filters
Saurav Das822c4e22015-10-23 10:51:11 -0700600 */
Charles Chan66291502018-03-02 16:43:28 -0800601 protected List<List<FlowRule>> processVlanIdFilter(PortCriterion portCriterion,
pier6aef5b72019-06-10 17:10:26 +0200602 VlanIdCriterion vidCriterion,
603 VlanId assignedVlan,
604 ApplicationId applicationId,
605 boolean install) {
Charles Chan66291502018-03-02 16:43:28 -0800606 List<FlowRule> filteringRules = new ArrayList<>();
607 List<FlowRule> assignmentRules = new ArrayList<>();
Saurav Das822c4e22015-10-23 10:51:11 -0700608 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
609 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
Charles Chan14967c22015-12-07 11:11:50 -0800610 TrafficSelector.Builder preSelector = null;
611 TrafficTreatment.Builder preTreatment = null;
612
Saurav Das4f980082015-11-05 13:39:15 -0800613 treatment.transition(TMAC_TABLE);
614
Saurav Das822c4e22015-10-23 10:51:11 -0700615 if (vidCriterion.vlanId() == VlanId.NONE) {
616 // untagged packets are assigned vlans
Charles Chand1172632017-03-15 17:33:09 -0700617 preSelector = DefaultTrafficSelector.builder();
618 if (requireVlanExtensions()) {
619 OfdpaMatchVlanVid ofdpaMatchVlanVid = new OfdpaMatchVlanVid(VlanId.NONE);
620 selector.extension(ofdpaMatchVlanVid, deviceId);
Charles Chanf9e98652016-09-07 16:54:23 -0700621 OfdpaSetVlanVid ofdpaSetVlanVid = new OfdpaSetVlanVid(assignedVlan);
622 treatment.extension(ofdpaSetVlanVid, deviceId);
Charles Chand1172632017-03-15 17:33:09 -0700623
624 OfdpaMatchVlanVid preOfdpaMatchVlanVid = new OfdpaMatchVlanVid(assignedVlan);
625 preSelector.extension(preOfdpaMatchVlanVid, deviceId);
Charles Chanf9e98652016-09-07 16:54:23 -0700626 } else {
Charles Chand1172632017-03-15 17:33:09 -0700627 selector.matchVlanId(VlanId.NONE);
Charles Chanf9e98652016-09-07 16:54:23 -0700628 treatment.setVlanId(assignedVlan);
Charles Chand1172632017-03-15 17:33:09 -0700629
630 preSelector.matchVlanId(assignedVlan);
631 }
632 preTreatment = DefaultTrafficTreatment.builder().transition(TMAC_TABLE);
633 } else {
634 if (requireVlanExtensions()) {
635 OfdpaMatchVlanVid ofdpaMatchVlanVid = new OfdpaMatchVlanVid(vidCriterion.vlanId());
636 selector.extension(ofdpaMatchVlanVid, deviceId);
637 } else {
638 selector.matchVlanId(vidCriterion.vlanId());
Charles Chanf9e98652016-09-07 16:54:23 -0700639 }
Charles Chan14967c22015-12-07 11:11:50 -0800640
Charles Chand55e84d2016-03-30 17:54:24 -0700641 if (!assignedVlan.equals(vidCriterion.vlanId())) {
Charles Chand1172632017-03-15 17:33:09 -0700642 if (requireVlanExtensions()) {
Charles Chanc03782d2017-01-31 13:57:55 -0800643 OfdpaSetVlanVid ofdpaSetVlanVid = new OfdpaSetVlanVid(assignedVlan);
644 treatment.extension(ofdpaSetVlanVid, deviceId);
645 } else {
646 treatment.setVlanId(assignedVlan);
647 }
Charles Chand55e84d2016-03-30 17:54:24 -0700648 }
Saurav Das822c4e22015-10-23 10:51:11 -0700649 }
Saurav Das822c4e22015-10-23 10:51:11 -0700650
651 // ofdpa cannot match on ALL portnumber, so we need to use separate
652 // rules for each port.
Charles Chan14967c22015-12-07 11:11:50 -0800653 List<PortNumber> portnums = new ArrayList<>();
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530654 if (portCriterion != null) {
655 if (PortNumber.ALL.equals(portCriterion.port())) {
656 for (Port port : deviceService.getPorts(deviceId)) {
657 if (port.number().toLong() > 0 && port.number().toLong() < OFPP_MAX) {
658 portnums.add(port.number());
659 }
Saurav Das822c4e22015-10-23 10:51:11 -0700660 }
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530661 } else {
662 portnums.add(portCriterion.port());
Saurav Das822c4e22015-10-23 10:51:11 -0700663 }
664 } else {
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530665 log.warn("Filtering Objective missing Port Criterion . " +
Charles Chan66291502018-03-02 16:43:28 -0800666 "VLAN Table cannot be programmed for {}", deviceId);
Saurav Das822c4e22015-10-23 10:51:11 -0700667 }
Saurav Das4f980082015-11-05 13:39:15 -0800668
Saurav Das822c4e22015-10-23 10:51:11 -0700669 for (PortNumber pnum : portnums) {
Saurav Das4f980082015-11-05 13:39:15 -0800670 // create rest of flowrule
Saurav Das822c4e22015-10-23 10:51:11 -0700671 selector.matchInPort(pnum);
672 FlowRule rule = DefaultFlowRule.builder()
673 .forDevice(deviceId)
674 .withSelector(selector.build())
675 .withTreatment(treatment.build())
676 .withPriority(DEFAULT_PRIORITY)
677 .fromApp(applicationId)
678 .makePermanent()
679 .forTable(VLAN_TABLE).build();
Charles Chan66291502018-03-02 16:43:28 -0800680 assignmentRules.add(rule);
Charles Chan14967c22015-12-07 11:11:50 -0800681
682 if (preSelector != null) {
683 preSelector.matchInPort(pnum);
684 FlowRule preRule = DefaultFlowRule.builder()
685 .forDevice(deviceId)
686 .withSelector(preSelector.build())
687 .withTreatment(preTreatment.build())
688 .withPriority(DEFAULT_PRIORITY)
689 .fromApp(applicationId)
690 .makePermanent()
691 .forTable(VLAN_TABLE).build();
Charles Chan66291502018-03-02 16:43:28 -0800692 filteringRules.add(preRule);
Charles Chan14967c22015-12-07 11:11:50 -0800693 }
Saurav Das822c4e22015-10-23 10:51:11 -0700694 }
pier6aef5b72019-06-10 17:10:26 +0200695 return install ? ImmutableList.of(filteringRules, assignmentRules) :
696 ImmutableList.of(assignmentRules, filteringRules);
Saurav Das822c4e22015-10-23 10:51:11 -0700697 }
698
699 /**
700 * Allows routed packets with correct destination MAC to be directed
Charles Chan45b69ab2018-03-02 15:41:41 -0800701 * to unicast routing table, multicast routing table or MPLS forwarding table.
Saurav Das822c4e22015-10-23 10:51:11 -0700702 *
703 * @param portCriterion port on device for which this filter is programmed
704 * @param ethCriterion dstMac of device for which is filter is programmed
705 * @param vidCriterion vlan assigned to port, or NONE for untagged
Saurav Das0e99e2b2015-10-28 12:39:42 -0700706 * @param assignedVlan assigned vlan-id for untagged packets
Charles Chan45b69ab2018-03-02 15:41:41 -0800707 * @param unicastMac some switches require a unicast TMAC flow to be programmed before multicast
708 * TMAC flow. This MAC address will be used for the unicast TMAC flow.
709 * This is unused if the filtering objective is a unicast.
Saurav Das822c4e22015-10-23 10:51:11 -0700710 * @param applicationId for application programming this filter
Charles Chan66291502018-03-02 16:43:28 -0800711 * @return stages of flow rules for port-vlan filters
Saurav Das822c4e22015-10-23 10:51:11 -0700712
713 */
Charles Chan66291502018-03-02 16:43:28 -0800714 protected List<List<FlowRule>> processEthDstFilter(PortCriterion portCriterion,
Saurav Das822c4e22015-10-23 10:51:11 -0700715 EthCriterion ethCriterion,
716 VlanIdCriterion vidCriterion,
Saurav Das0e99e2b2015-10-28 12:39:42 -0700717 VlanId assignedVlan,
Charles Chan45b69ab2018-03-02 15:41:41 -0800718 MacAddress unicastMac,
Saurav Das822c4e22015-10-23 10:51:11 -0700719 ApplicationId applicationId) {
Charles Chan5270ed02016-01-30 23:22:37 -0800720 // Consider PortNumber.ANY as wildcard. Match ETH_DST only
Sivachidambaram Subramanianc598d062017-05-16 22:42:06 +0530721 if (portCriterion != null && PortNumber.ANY.equals(portCriterion.port())) {
Charles Chan5270ed02016-01-30 23:22:37 -0800722 return processEthDstOnlyFilter(ethCriterion, applicationId);
723 }
724
Charles Chan5b9df8d2016-03-28 22:21:40 -0700725 // Multicast MAC
726 if (ethCriterion.mask() != null) {
Charles Chan45b69ab2018-03-02 15:41:41 -0800727 return processMcastEthDstFilter(ethCriterion, assignedVlan, unicastMac, applicationId);
Charles Chan5b9df8d2016-03-28 22:21:40 -0700728 }
729
Saurav Das822c4e22015-10-23 10:51:11 -0700730 //handling untagged packets via assigned VLAN
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530731 if (vidCriterion != null && vidCriterion.vlanId() == VlanId.NONE) {
Saurav Das0e99e2b2015-10-28 12:39:42 -0700732 vidCriterion = (VlanIdCriterion) Criteria.matchVlanId(assignedVlan);
Saurav Das822c4e22015-10-23 10:51:11 -0700733 }
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530734 List<FlowRule> rules = new ArrayList<>();
735 OfdpaMatchVlanVid ofdpaMatchVlanVid = null;
736 if (vidCriterion != null && requireVlanExtensions()) {
737 ofdpaMatchVlanVid = new OfdpaMatchVlanVid(vidCriterion.vlanId());
738 }
Saurav Das822c4e22015-10-23 10:51:11 -0700739 // ofdpa cannot match on ALL portnumber, so we need to use separate
740 // rules for each port.
Yuta HIGUCHI2dce08a2017-04-20 21:57:48 -0700741 List<PortNumber> portnums = new ArrayList<>();
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530742 if (portCriterion != null) {
743 if (PortNumber.ALL.equals(portCriterion.port())) {
744 for (Port port : deviceService.getPorts(deviceId)) {
745 if (port.number().toLong() > 0 && port.number().toLong() < OFPP_MAX) {
746 portnums.add(port.number());
747 }
Saurav Das822c4e22015-10-23 10:51:11 -0700748 }
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530749 } else {
750 portnums.add(portCriterion.port());
751 }
752 for (PortNumber pnum : portnums) {
753 rules.add(buildTmacRuleForIpv4(ethCriterion,
754 vidCriterion,
755 ofdpaMatchVlanVid,
756 applicationId,
757 pnum));
758 rules.add(buildTmacRuleForMpls(ethCriterion,
759 vidCriterion,
760 ofdpaMatchVlanVid,
761 applicationId,
762 pnum));
763 rules.add(buildTmacRuleForIpv6(ethCriterion,
764 vidCriterion,
765 ofdpaMatchVlanVid,
766 applicationId,
767 pnum));
Saurav Das822c4e22015-10-23 10:51:11 -0700768 }
769 } else {
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530770 rules.add(buildTmacRuleForIpv4(ethCriterion,
771 vidCriterion,
772 ofdpaMatchVlanVid,
773 applicationId,
774 null));
775 rules.add(buildTmacRuleForMpls(ethCriterion,
776 vidCriterion,
777 ofdpaMatchVlanVid,
778 applicationId,
779 null));
780 rules.add(buildTmacRuleForIpv6(ethCriterion,
781 vidCriterion,
782 ofdpaMatchVlanVid,
783 applicationId,
784 null));
Saurav Das822c4e22015-10-23 10:51:11 -0700785 }
Charles Chan66291502018-03-02 16:43:28 -0800786 return ImmutableList.of(rules);
Saurav Das822c4e22015-10-23 10:51:11 -0700787 }
788
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530789 /**
790 * Builds TMAC rules for IPv4 packets.
791 *
Charles Chanc550f2e2017-12-19 19:55:57 -0800792 * @param ethCriterion dst mac matching
793 * @param vidCriterion vlan id assigned to the port
794 * @param ofdpaMatchVlanVid OFDPA vlan id matching
795 * @param applicationId application id
796 * @param pnum port number
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530797 * @return TMAC rule for IPV4 packets
798 */
799 private FlowRule buildTmacRuleForIpv4(EthCriterion ethCriterion,
800 VlanIdCriterion vidCriterion,
801 OfdpaMatchVlanVid ofdpaMatchVlanVid,
802 ApplicationId applicationId,
803 PortNumber pnum) {
804 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
805 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
806 if (pnum != null) {
807 if (matchInPortTmacTable()) {
808 selector.matchInPort(pnum);
809 } else {
Saurav Dasf14d9ef2017-12-05 15:00:23 -0800810 log.debug("Pipeline does not support IN_PORT matching in TMAC table, " +
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530811 "ignoring the IN_PORT criteria");
812 }
813 }
814 if (vidCriterion != null) {
815 if (requireVlanExtensions()) {
816 selector.extension(ofdpaMatchVlanVid, deviceId);
817 } else {
818 selector.matchVlanId(vidCriterion.vlanId());
819 }
820 }
821 selector.matchEthType(Ethernet.TYPE_IPV4);
822 selector.matchEthDst(ethCriterion.mac());
823 treatment.transition(UNICAST_ROUTING_TABLE);
824 return DefaultFlowRule.builder()
825 .forDevice(deviceId)
826 .withSelector(selector.build())
827 .withTreatment(treatment.build())
828 .withPriority(DEFAULT_PRIORITY)
829 .fromApp(applicationId)
830 .makePermanent()
831 .forTable(TMAC_TABLE).build();
832 }
833
834 /**
835 * Builds TMAC rule for MPLS packets.
836 *
Charles Chanc550f2e2017-12-19 19:55:57 -0800837 * @param ethCriterion dst mac matching
838 * @param vidCriterion vlan id assigned to the port
839 * @param ofdpaMatchVlanVid OFDPA vlan id matching
840 * @param applicationId application id
841 * @param pnum port number
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530842 * @return TMAC rule for MPLS packets
843 */
844 private FlowRule buildTmacRuleForMpls(EthCriterion ethCriterion,
845 VlanIdCriterion vidCriterion,
846 OfdpaMatchVlanVid ofdpaMatchVlanVid,
847 ApplicationId applicationId,
848 PortNumber pnum) {
849 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
850 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
851 if (pnum != null) {
852 if (matchInPortTmacTable()) {
853 selector.matchInPort(pnum);
854 } else {
Saurav Dasf14d9ef2017-12-05 15:00:23 -0800855 log.debug("Pipeline does not support IN_PORT matching in TMAC table, " +
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530856 "ignoring the IN_PORT criteria");
857 }
858 }
859 if (vidCriterion != null) {
860 if (requireVlanExtensions()) {
861 selector.extension(ofdpaMatchVlanVid, deviceId);
862 } else {
863 selector.matchVlanId(vidCriterion.vlanId());
864 }
865 }
866 selector.matchEthType(Ethernet.MPLS_UNICAST);
867 selector.matchEthDst(ethCriterion.mac());
868 treatment.transition(MPLS_TABLE_0);
869 return DefaultFlowRule.builder()
870 .forDevice(deviceId)
871 .withSelector(selector.build())
872 .withTreatment(treatment.build())
873 .withPriority(DEFAULT_PRIORITY)
874 .fromApp(applicationId)
875 .makePermanent()
876 .forTable(TMAC_TABLE).build();
877 }
878
879 /**
880 * Builds TMAC rules for IPv6 packets.
881 *
Charles Chanc550f2e2017-12-19 19:55:57 -0800882 * @param ethCriterion dst mac matching
883 * @param vidCriterion vlan id assigned to the port
884 * @param ofdpaMatchVlanVid OFDPA vlan id matching
885 * @param applicationId application id
886 * @param pnum port number
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530887 * @return TMAC rule for IPV6 packets
888 */
889 private FlowRule buildTmacRuleForIpv6(EthCriterion ethCriterion,
890 VlanIdCriterion vidCriterion,
891 OfdpaMatchVlanVid ofdpaMatchVlanVid,
892 ApplicationId applicationId,
893 PortNumber pnum) {
894 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
895 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
896 if (pnum != null) {
897 if (matchInPortTmacTable()) {
898 selector.matchInPort(pnum);
899 } else {
Saurav Dasf14d9ef2017-12-05 15:00:23 -0800900 log.debug("Pipeline does not support IN_PORT matching in TMAC table, " +
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530901 "ignoring the IN_PORT criteria");
902 }
903 }
904 if (vidCriterion != null) {
905 if (requireVlanExtensions()) {
906 selector.extension(ofdpaMatchVlanVid, deviceId);
907 } else {
908 selector.matchVlanId(vidCriterion.vlanId());
909 }
910 }
911 selector.matchEthType(Ethernet.TYPE_IPV6);
912 selector.matchEthDst(ethCriterion.mac());
913 treatment.transition(UNICAST_ROUTING_TABLE);
914 return DefaultFlowRule.builder()
915 .forDevice(deviceId)
916 .withSelector(selector.build())
917 .withTreatment(treatment.build())
918 .withPriority(DEFAULT_PRIORITY)
919 .fromApp(applicationId)
920 .makePermanent()
921 .forTable(TMAC_TABLE).build();
922 }
923
Charles Chan66291502018-03-02 16:43:28 -0800924 protected List<List<FlowRule>> processEthDstOnlyFilter(EthCriterion ethCriterion,
Yi Tsengef19de12017-04-24 11:33:05 -0700925 ApplicationId applicationId) {
Pier Luigi0e358632017-01-31 09:35:05 -0800926 ImmutableList.Builder<FlowRule> builder = ImmutableList.builder();
927
Charles Chan5270ed02016-01-30 23:22:37 -0800928 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
929 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
930 selector.matchEthType(Ethernet.TYPE_IPV4);
931 selector.matchEthDst(ethCriterion.mac());
932 treatment.transition(UNICAST_ROUTING_TABLE);
933 FlowRule rule = DefaultFlowRule.builder()
934 .forDevice(deviceId)
935 .withSelector(selector.build())
936 .withTreatment(treatment.build())
937 .withPriority(DEFAULT_PRIORITY)
938 .fromApp(applicationId)
939 .makePermanent()
940 .forTable(TMAC_TABLE).build();
Pier Luigi0e358632017-01-31 09:35:05 -0800941 builder.add(rule);
942
943 selector = DefaultTrafficSelector.builder();
944 treatment = DefaultTrafficTreatment.builder();
945 selector.matchEthType(Ethernet.TYPE_IPV6);
946 selector.matchEthDst(ethCriterion.mac());
947 treatment.transition(UNICAST_ROUTING_TABLE);
948 rule = DefaultFlowRule.builder()
949 .forDevice(deviceId)
950 .withSelector(selector.build())
951 .withTreatment(treatment.build())
952 .withPriority(DEFAULT_PRIORITY)
953 .fromApp(applicationId)
954 .makePermanent()
955 .forTable(TMAC_TABLE).build();
Charles Chan66291502018-03-02 16:43:28 -0800956 return ImmutableList.of(builder.add(rule).build());
Charles Chan5270ed02016-01-30 23:22:37 -0800957 }
958
Charles Chan66291502018-03-02 16:43:28 -0800959 List<List<FlowRule>> processMcastEthDstFilter(EthCriterion ethCriterion,
Charles Chanb4879a52017-10-20 19:09:16 -0700960 VlanId assignedVlan,
Charles Chan45b69ab2018-03-02 15:41:41 -0800961 MacAddress unicastMac,
Yi Tsengef19de12017-04-24 11:33:05 -0700962 ApplicationId applicationId) {
Charles Chan45b69ab2018-03-02 15:41:41 -0800963 ImmutableList.Builder<FlowRule> unicastFlows = ImmutableList.builder();
964 ImmutableList.Builder<FlowRule> multicastFlows = ImmutableList.builder();
965 TrafficSelector.Builder selector;
966 TrafficTreatment.Builder treatment;
Charles Chanb4879a52017-10-20 19:09:16 -0700967 FlowRule rule;
Julia Ferguson65428c32017-08-10 18:15:24 +0000968
Charles Chanb4879a52017-10-20 19:09:16 -0700969 if (IPV4_MULTICAST.equals(ethCriterion.mac())) {
Charles Chan45b69ab2018-03-02 15:41:41 -0800970 if (requireUnicastBeforeMulticast()) {
971 selector = DefaultTrafficSelector.builder();
972 treatment = DefaultTrafficTreatment.builder();
973 selector.matchEthType(Ethernet.TYPE_IPV4);
974 selector.matchEthDst(unicastMac);
Charles Chan056e0c12018-05-10 22:19:49 +0000975 selector.matchVlanId(assignedVlan);
Charles Chan45b69ab2018-03-02 15:41:41 -0800976 treatment.transition(UNICAST_ROUTING_TABLE);
977 rule = DefaultFlowRule.builder()
978 .forDevice(deviceId)
979 .withSelector(selector.build())
980 .withTreatment(treatment.build())
Charles Chan056e0c12018-05-10 22:19:49 +0000981 .withPriority(DEFAULT_PRIORITY)
Charles Chan45b69ab2018-03-02 15:41:41 -0800982 .fromApp(applicationId)
983 .makePermanent()
984 .forTable(TMAC_TABLE).build();
985 unicastFlows.add(rule);
986 }
987
988 selector = DefaultTrafficSelector.builder();
989 treatment = DefaultTrafficTreatment.builder();
Charles Chanb4879a52017-10-20 19:09:16 -0700990 selector.matchEthType(Ethernet.TYPE_IPV4);
991 selector.matchEthDstMasked(ethCriterion.mac(), ethCriterion.mask());
Charles Chan056e0c12018-05-10 22:19:49 +0000992 selector.matchVlanId(assignedVlan);
Charles Chanb4879a52017-10-20 19:09:16 -0700993 treatment.transition(MULTICAST_ROUTING_TABLE);
994 rule = DefaultFlowRule.builder()
995 .forDevice(deviceId)
996 .withSelector(selector.build())
997 .withTreatment(treatment.build())
Charles Chan056e0c12018-05-10 22:19:49 +0000998 .withPriority(DEFAULT_PRIORITY)
Charles Chanb4879a52017-10-20 19:09:16 -0700999 .fromApp(applicationId)
1000 .makePermanent()
1001 .forTable(TMAC_TABLE).build();
Charles Chan45b69ab2018-03-02 15:41:41 -08001002 multicastFlows.add(rule);
Charles Chanb4879a52017-10-20 19:09:16 -07001003 }
1004
1005 if (IPV6_MULTICAST.equals(ethCriterion.mac())) {
Charles Chan45b69ab2018-03-02 15:41:41 -08001006 if (requireUnicastBeforeMulticast()) {
1007 selector = DefaultTrafficSelector.builder();
1008 treatment = DefaultTrafficTreatment.builder();
1009 selector.matchEthType(Ethernet.TYPE_IPV6);
1010 selector.matchEthDst(unicastMac);
Charles Chan056e0c12018-05-10 22:19:49 +00001011 selector.matchVlanId(assignedVlan);
Charles Chan45b69ab2018-03-02 15:41:41 -08001012 treatment.transition(UNICAST_ROUTING_TABLE);
1013 rule = DefaultFlowRule.builder()
1014 .forDevice(deviceId)
1015 .withSelector(selector.build())
1016 .withTreatment(treatment.build())
Charles Chan056e0c12018-05-10 22:19:49 +00001017 .withPriority(DEFAULT_PRIORITY)
Charles Chan45b69ab2018-03-02 15:41:41 -08001018 .fromApp(applicationId)
1019 .makePermanent()
1020 .forTable(TMAC_TABLE).build();
1021 unicastFlows.add(rule);
1022 }
1023
Charles Chanb4879a52017-10-20 19:09:16 -07001024 selector = DefaultTrafficSelector.builder();
1025 treatment = DefaultTrafficTreatment.builder();
1026 selector.matchEthType(Ethernet.TYPE_IPV6);
1027 selector.matchEthDstMasked(ethCriterion.mac(), ethCriterion.mask());
Charles Chan056e0c12018-05-10 22:19:49 +00001028 selector.matchVlanId(assignedVlan);
Charles Chanb4879a52017-10-20 19:09:16 -07001029 treatment.transition(MULTICAST_ROUTING_TABLE);
1030 rule = DefaultFlowRule.builder()
1031 .forDevice(deviceId)
1032 .withSelector(selector.build())
1033 .withTreatment(treatment.build())
Charles Chan056e0c12018-05-10 22:19:49 +00001034 .withPriority(DEFAULT_PRIORITY)
Charles Chanb4879a52017-10-20 19:09:16 -07001035 .fromApp(applicationId)
1036 .makePermanent()
1037 .forTable(TMAC_TABLE).build();
Charles Chan45b69ab2018-03-02 15:41:41 -08001038 multicastFlows.add(rule);
Charles Chanb4879a52017-10-20 19:09:16 -07001039 }
Charles Chan45b69ab2018-03-02 15:41:41 -08001040 return ImmutableList.of(unicastFlows.build(), multicastFlows.build());
Charles Chan5b9df8d2016-03-28 22:21:40 -07001041 }
1042
Saurav Das822c4e22015-10-23 10:51:11 -07001043 private Collection<FlowRule> processForward(ForwardingObjective fwd) {
1044 switch (fwd.flag()) {
1045 case SPECIFIC:
1046 return processSpecific(fwd);
1047 case VERSATILE:
1048 return processVersatile(fwd);
Jonghwan Hyun800d9d02018-04-09 09:40:50 -07001049 case EGRESS:
1050 return processEgress(fwd);
Saurav Das822c4e22015-10-23 10:51:11 -07001051 default:
1052 fail(fwd, ObjectiveError.UNKNOWN);
1053 log.warn("Unknown forwarding flag {}", fwd.flag());
1054 }
1055 return Collections.emptySet();
1056 }
1057
1058 /**
Jonghwan Hyun800d9d02018-04-09 09:40:50 -07001059 * In the OF-DPA 2.0 pipeline, egress forwarding objectives go to the
1060 * egress tables.
1061 * @param fwd the forwarding objective of type 'egress'
1062 * @return a collection of flow rules to be sent to the switch. An empty
1063 * collection may be returned if there is a problem in processing
1064 * the flow rule
1065 */
1066 protected Collection<FlowRule> processEgress(ForwardingObjective fwd) {
1067 log.debug("Processing egress forwarding objective:{} in dev:{}",
1068 fwd, deviceId);
1069
1070 List<FlowRule> rules = new ArrayList<>();
1071
1072 // Build selector
1073 TrafficSelector.Builder sb = DefaultTrafficSelector.builder();
1074 VlanIdCriterion vlanIdCriterion = (VlanIdCriterion) fwd.selector().getCriterion(Criterion.Type.VLAN_VID);
1075 if (vlanIdCriterion == null) {
1076 log.error("Egress forwarding objective:{} must include vlanId", fwd.id());
1077 fail(fwd, ObjectiveError.BADPARAMS);
1078 return rules;
1079 }
1080
1081 Optional<Instruction> outInstr = fwd.treatment().allInstructions().stream()
1082 .filter(instruction -> instruction instanceof OutputInstruction).findFirst();
1083 if (!outInstr.isPresent()) {
1084 log.error("Egress forwarding objective:{} must include output port", fwd.id());
1085 fail(fwd, ObjectiveError.BADPARAMS);
1086 return rules;
1087 }
1088
1089 PortNumber portNumber = ((OutputInstruction) outInstr.get()).port();
1090
1091 sb.matchVlanId(vlanIdCriterion.vlanId());
1092 OfdpaMatchActsetOutput actsetOutput = new OfdpaMatchActsetOutput(portNumber);
1093 sb.extension(actsetOutput, deviceId);
1094
1095 sb.extension(new OfdpaMatchAllowVlanTranslation(ALLOW_VLAN_TRANSLATION), deviceId);
1096
1097 // Build a flow rule for Egress VLAN Flow table
1098 TrafficTreatment.Builder tb = DefaultTrafficTreatment.builder();
1099 tb.transition(EGRESS_DSCP_PCP_REMARK_FLOW_TABLE);
1100 if (fwd.treatment() != null) {
1101 for (Instruction instr : fwd.treatment().allInstructions()) {
1102 if (instr instanceof L2ModificationInstruction &&
1103 ((L2ModificationInstruction) instr).subtype() == L2SubType.VLAN_ID) {
1104 tb.immediate().add(instr);
1105 }
1106 if (instr instanceof L2ModificationInstruction &&
1107 ((L2ModificationInstruction) instr).subtype() == L2SubType.VLAN_PUSH) {
1108 tb.immediate().pushVlan();
1109 EthType ethType = ((L2ModificationInstruction.ModVlanHeaderInstruction) instr).ethernetType();
1110 if (ethType.equals(EtherType.QINQ.ethType())) {
1111 // Build a flow rule for Egress TPID Flow table
1112 TrafficSelector tpidSelector = DefaultTrafficSelector.builder()
1113 .extension(actsetOutput, deviceId)
1114 .matchVlanId(VlanId.ANY).build();
1115
1116 TrafficTreatment tpidTreatment = DefaultTrafficTreatment.builder()
1117 .extension(new Ofdpa3CopyField(COPY_FIELD_NBITS, COPY_FIELD_OFFSET,
1118 COPY_FIELD_OFFSET, OXM_ID_VLAN_VID,
1119 OXM_ID_PACKET_REG_1),
1120 deviceId)
1121 .popVlan()
1122 .pushVlan(EtherType.QINQ.ethType())
1123 .extension(new Ofdpa3CopyField(COPY_FIELD_NBITS, COPY_FIELD_OFFSET,
1124 COPY_FIELD_OFFSET, OXM_ID_PACKET_REG_1,
1125 OXM_ID_VLAN_VID),
1126 deviceId)
1127 .build();
1128
1129 FlowRule.Builder tpidRuleBuilder = DefaultFlowRule.builder()
1130 .fromApp(fwd.appId())
1131 .withPriority(fwd.priority())
1132 .forDevice(deviceId)
1133 .withSelector(tpidSelector)
1134 .withTreatment(tpidTreatment)
1135 .makePermanent()
1136 .forTable(EGRESS_TPID_FLOW_TABLE);
1137 rules.add(tpidRuleBuilder.build());
1138 }
1139 }
1140 }
1141 }
1142
1143 FlowRule.Builder ruleBuilder = DefaultFlowRule.builder()
1144 .fromApp(fwd.appId())
1145 .withPriority(fwd.priority())
1146 .forDevice(deviceId)
1147 .withSelector(sb.build())
1148 .withTreatment(tb.build())
1149 .makePermanent()
1150 .forTable(EGRESS_VLAN_FLOW_TABLE);
1151 rules.add(ruleBuilder.build());
1152 return rules;
1153 }
1154
1155 /**
Saurav Das822c4e22015-10-23 10:51:11 -07001156 * In the OF-DPA 2.0 pipeline, versatile forwarding objectives go to the
1157 * ACL table.
1158 * @param fwd the forwarding objective of type 'versatile'
1159 * @return a collection of flow rules to be sent to the switch. An empty
1160 * collection may be returned if there is a problem in processing
1161 * the flow rule
1162 */
Saurav Das52025962016-01-28 22:30:01 -08001163 protected Collection<FlowRule> processVersatile(ForwardingObjective fwd) {
Saurav Das018605f2017-02-18 14:05:44 -08001164 log.debug("Processing versatile forwarding objective:{} in dev:{}",
Saurav Dasd2fded02016-12-02 15:43:47 -08001165 fwd.id(), deviceId);
Saurav Das822c4e22015-10-23 10:51:11 -07001166
Saurav Das822c4e22015-10-23 10:51:11 -07001167 if (fwd.nextId() == null && fwd.treatment() == null) {
1168 log.error("Forwarding objective {} from {} must contain "
1169 + "nextId or Treatment", fwd.selector(), fwd.appId());
Zsolt Haraszti9faab752016-02-17 15:55:20 -08001170 fail(fwd, ObjectiveError.BADPARAMS);
Saurav Das822c4e22015-10-23 10:51:11 -07001171 return Collections.emptySet();
1172 }
Saurav Das49cb5a12016-01-16 22:54:07 -08001173
Charles Chanab591602019-01-22 17:25:04 -08001174 TrafficSelector.Builder sbuilder = versatileSelectorBuilder(fwd);
1175 TrafficTreatment.Builder ttBuilder = versatileTreatmentBuilder(fwd);
1176 if (ttBuilder == null) {
1177 return Collections.emptySet();
1178 }
1179
1180 FlowRule.Builder ruleBuilder = DefaultFlowRule.builder()
1181 .fromApp(fwd.appId())
1182 .withPriority(fwd.priority())
1183 .forDevice(deviceId)
1184 .withSelector(sbuilder.build())
1185 .withTreatment(ttBuilder.build())
1186 .makePermanent()
1187 .forTable(ACL_TABLE);
1188 return Collections.singletonList(ruleBuilder.build());
1189 }
1190
1191 /**
1192 * Helper function to create traffic selector builder for versatile forwarding objectives.
1193 *
1194 * @param fwd original forwarding objective
1195 * @return selector builder for the flow rule
1196 */
1197 protected TrafficSelector.Builder versatileSelectorBuilder(ForwardingObjective fwd) {
Saurav Das77b5e902016-01-27 17:01:59 -08001198 TrafficSelector.Builder sbuilder = DefaultTrafficSelector.builder();
1199 fwd.selector().criteria().forEach(criterion -> {
1200 if (criterion instanceof VlanIdCriterion) {
1201 VlanId vlanId = ((VlanIdCriterion) criterion).vlanId();
1202 // ensure that match does not include vlan = NONE as OF-DPA does not
1203 // match untagged packets this way in the ACL table.
1204 if (vlanId.equals(VlanId.NONE)) {
1205 return;
1206 }
Charles Chand1172632017-03-15 17:33:09 -07001207 if (requireVlanExtensions()) {
1208 OfdpaMatchVlanVid ofdpaMatchVlanVid = new OfdpaMatchVlanVid(vlanId);
1209 sbuilder.extension(ofdpaMatchVlanVid, deviceId);
1210 } else {
1211 sbuilder.matchVlanId(vlanId);
1212 }
Charles Chan09bf2692018-01-11 11:48:18 -08001213 } else if (criterion instanceof Icmpv6TypeCriterion) {
1214 byte icmpv6Type = (byte) ((Icmpv6TypeCriterion) criterion).icmpv6Type();
1215 sbuilder.matchIcmpv6Type(icmpv6Type);
1216 } else if (criterion instanceof Icmpv6CodeCriterion) {
1217 byte icmpv6Code = (byte) ((Icmpv6CodeCriterion) criterion).icmpv6Code();
1218 sbuilder.matchIcmpv6Type(icmpv6Code);
Charles Chand9e47c62017-10-05 15:17:15 -07001219 } else if (criterion instanceof TcpPortCriterion || criterion instanceof UdpPortCriterion) {
1220 // FIXME: QMX switches do not support L4 dst port matching in ACL table.
1221 // Currently L4 dst port matching is only used by DHCP relay feature
1222 // and therefore is safe to be replaced with L4 src port matching.
1223 // We need to revisit this if L4 dst port is used for other purpose in the future.
1224 if (!supportIpv6L4Dst() && isIpv6(fwd.selector())) {
Charles Chand9e47c62017-10-05 15:17:15 -07001225 switch (criterion.type()) {
1226 case UDP_DST:
Charles Chand9e47c62017-10-05 15:17:15 -07001227 case UDP_DST_MASKED:
Charles Chand9e47c62017-10-05 15:17:15 -07001228 case TCP_DST:
Charles Chand9e47c62017-10-05 15:17:15 -07001229 case TCP_DST_MASKED:
Charles Chand9e47c62017-10-05 15:17:15 -07001230 break;
1231 default:
1232 sbuilder.add(criterion);
1233 }
1234 } else {
1235 sbuilder.add(criterion);
1236 }
Saurav Das77b5e902016-01-27 17:01:59 -08001237 } else {
1238 sbuilder.add(criterion);
1239 }
1240 });
Charles Chanab591602019-01-22 17:25:04 -08001241 return sbuilder;
1242 }
Saurav Das77b5e902016-01-27 17:01:59 -08001243
Charles Chanab591602019-01-22 17:25:04 -08001244 /**
1245 * Helper function to create traffic treatment builder for versatile forwarding objectives.
1246 *
1247 * @param fwd original forwarding objective
1248 * @return treatment builder for the flow rule, or null if there is an error.
1249 */
1250 protected TrafficTreatment.Builder versatileTreatmentBuilder(ForwardingObjective fwd) {
Saurav Das822c4e22015-10-23 10:51:11 -07001251 // XXX driver does not currently do type checking as per Tables 65-67 in
1252 // OFDPA 2.0 spec. The only allowed treatment is a punt to the controller.
Saurav Das49cb5a12016-01-16 22:54:07 -08001253 TrafficTreatment.Builder ttBuilder = DefaultTrafficTreatment.builder();
1254 if (fwd.treatment() != null) {
1255 for (Instruction ins : fwd.treatment().allInstructions()) {
1256 if (ins instanceof OutputInstruction) {
1257 OutputInstruction o = (OutputInstruction) ins;
Ray Milkeyfe0e0852018-01-18 11:14:05 -08001258 if (PortNumber.CONTROLLER.equals(o.port())) {
Saurav Das49cb5a12016-01-16 22:54:07 -08001259 ttBuilder.add(o);
1260 } else {
1261 log.warn("Only allowed treatments in versatile forwarding "
1262 + "objectives are punts to the controller");
1263 }
Charles Chanf76de302018-06-15 18:54:18 -07001264 } else if (ins instanceof NoActionInstruction) {
1265 // No action is allowed and nothing needs to be done
Saurav Das49cb5a12016-01-16 22:54:07 -08001266 } else {
1267 log.warn("Cannot process instruction in versatile fwd {}", ins);
1268 }
Saurav Das822c4e22015-10-23 10:51:11 -07001269 }
Charles Chan2df0e8a2017-01-09 11:45:08 -08001270 if (fwd.treatment().clearedDeferred()) {
1271 ttBuilder.wipeDeferred();
1272 }
Saurav Das822c4e22015-10-23 10:51:11 -07001273 }
Saurav Das822c4e22015-10-23 10:51:11 -07001274 if (fwd.nextId() != null) {
Charles Chanab591602019-01-22 17:25:04 -08001275 // Override case
Saurav Das49cb5a12016-01-16 22:54:07 -08001276 NextGroup next = getGroupForNextObjective(fwd.nextId());
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +05301277 if (next == null) {
1278 fail(fwd, ObjectiveError.BADPARAMS);
Charles Chanab591602019-01-22 17:25:04 -08001279 return null;
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +05301280 }
Saurav Das49cb5a12016-01-16 22:54:07 -08001281 List<Deque<GroupKey>> gkeys = appKryo.deserialize(next.data());
1282 // we only need the top level group's key to point the flow to it
1283 Group group = groupService.getGroup(deviceId, gkeys.get(0).peekFirst());
1284 if (group == null) {
1285 log.warn("Group with key:{} for next-id:{} not found in dev:{}",
Charles Chanab591602019-01-22 17:25:04 -08001286 gkeys.get(0).peekFirst(), fwd.nextId(), deviceId);
Saurav Das49cb5a12016-01-16 22:54:07 -08001287 fail(fwd, ObjectiveError.GROUPMISSING);
Charles Chanab591602019-01-22 17:25:04 -08001288 return null;
Saurav Das49cb5a12016-01-16 22:54:07 -08001289 }
1290 ttBuilder.deferred().group(group.id());
Saurav Das822c4e22015-10-23 10:51:11 -07001291 }
Charles Chanab591602019-01-22 17:25:04 -08001292 return ttBuilder;
Saurav Das822c4e22015-10-23 10:51:11 -07001293 }
1294
1295 /**
1296 * In the OF-DPA 2.0 pipeline, specific forwarding refers to the IP table
Saurav Das8a0732e2015-11-20 15:27:53 -08001297 * (unicast or multicast) or the L2 table (mac + vlan) or the MPLS table.
Saurav Das822c4e22015-10-23 10:51:11 -07001298 *
1299 * @param fwd the forwarding objective of type 'specific'
1300 * @return a collection of flow rules. Typically there will be only one
1301 * for this type of forwarding objective. An empty set may be
1302 * returned if there is an issue in processing the objective.
1303 */
Charles Chan50d900c2018-03-02 13:26:22 -08001304 private Collection<FlowRule> processSpecific(ForwardingObjective fwd) {
Saurav Das25190812016-05-27 13:54:07 -07001305 log.debug("Processing specific fwd objective:{} in dev:{} with next:{}",
Saurav Das4ce45962015-11-24 23:21:05 -08001306 fwd.id(), deviceId, fwd.nextId());
1307 boolean isEthTypeObj = isSupportedEthTypeObjective(fwd);
1308 boolean isEthDstObj = isSupportedEthDstObjective(fwd);
1309
1310 if (isEthTypeObj) {
1311 return processEthTypeSpecific(fwd);
1312 } else if (isEthDstObj) {
1313 return processEthDstSpecific(fwd);
1314 } else {
1315 log.warn("processSpecific: Unsupported forwarding objective "
1316 + "criteria fwd:{} in dev:{}", fwd.nextId(), deviceId);
Saurav Das822c4e22015-10-23 10:51:11 -07001317 fail(fwd, ObjectiveError.UNSUPPORTED);
1318 return Collections.emptySet();
1319 }
Saurav Das4ce45962015-11-24 23:21:05 -08001320 }
1321
Saurav Das4ce45962015-11-24 23:21:05 -08001322 /**
1323 * Handles forwarding rules to the IP and MPLS tables.
1324 *
1325 * @param fwd the forwarding objective
1326 * @return A collection of flow rules, or an empty set
1327 */
1328 protected Collection<FlowRule> processEthTypeSpecific(ForwardingObjective fwd) {
Charles Chancad338a2016-09-16 18:03:11 -07001329 return processEthTypeSpecificInternal(fwd, false, ACL_TABLE);
Charles Chanf9e98652016-09-07 16:54:23 -07001330 }
1331
1332 /**
1333 * Internal implementation of processEthTypeSpecific.
1334 * <p>
1335 * Wildcarded IPv4_DST is not supported in OFDPA i12. Therefore, we break
1336 * the rule into 0.0.0.0/1 and 128.0.0.0/1.
1337 * The allowDefaultRoute must be set to false for OFDPA i12.
1338 * </p>
1339 *
1340 * @param fwd the forwarding objective
1341 * @param allowDefaultRoute allow wildcarded IPv4_DST or not
Ray Milkey0bb1e102016-11-10 14:51:27 -08001342 * @param mplsNextTable next MPLS table
Charles Chanf9e98652016-09-07 16:54:23 -07001343 * @return A collection of flow rules, or an empty set
1344 */
1345 protected Collection<FlowRule> processEthTypeSpecificInternal(ForwardingObjective fwd,
Charles Chancad338a2016-09-16 18:03:11 -07001346 boolean allowDefaultRoute,
1347 int mplsNextTable) {
Saurav Das4ce45962015-11-24 23:21:05 -08001348 TrafficSelector selector = fwd.selector();
1349 EthTypeCriterion ethType =
1350 (EthTypeCriterion) selector.getCriterion(Criterion.Type.ETH_TYPE);
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001351 boolean emptyGroup = false;
Charles Chan188ebf52015-12-23 00:15:11 -08001352 int forTableId;
Saurav Das8a0732e2015-11-20 15:27:53 -08001353 TrafficSelector.Builder filteredSelector = DefaultTrafficSelector.builder();
Charles Chan14967c22015-12-07 11:11:50 -08001354 TrafficTreatment.Builder tb = DefaultTrafficTreatment.builder();
Flavio Castroe10fa242016-01-15 12:43:51 -08001355 TrafficSelector.Builder complementarySelector = DefaultTrafficSelector.builder();
Charles Chan14967c22015-12-07 11:11:50 -08001356
Saurav Das8a0732e2015-11-20 15:27:53 -08001357 if (ethType.ethType().toShort() == Ethernet.TYPE_IPV4) {
Pier Luigi3bfe32c2017-01-30 09:47:36 -08001358 if (buildIpv4Selector(filteredSelector, complementarySelector, fwd, allowDefaultRoute) < 0) {
1359 return Collections.emptyList();
1360 }
1361 // We need to set properly the next table
Flavio Castroe10fa242016-01-15 12:43:51 -08001362 IpPrefix ipv4Dst = ((IPCriterion) selector.getCriterion(Criterion.Type.IPV4_DST)).ip();
Charles Chan5b9df8d2016-03-28 22:21:40 -07001363 if (ipv4Dst.isMulticast()) {
Charles Chan5b9df8d2016-03-28 22:21:40 -07001364 forTableId = MULTICAST_ROUTING_TABLE;
Flavio Castroe10fa242016-01-15 12:43:51 -08001365 } else {
Charles Chan5b9df8d2016-03-28 22:21:40 -07001366 forTableId = UNICAST_ROUTING_TABLE;
Flavio Castroe10fa242016-01-15 12:43:51 -08001367 }
Charles Chan50d900c2018-03-02 13:26:22 -08001368
Charles Chan236653d2018-03-05 11:28:23 -08001369 // TODO decrementing IP ttl is done automatically for routing, this
Charles Chan50d900c2018-03-02 13:26:22 -08001370 // action is ignored or rejected in ofdpa as it is not fully implemented
Charles Chan236653d2018-03-05 11:28:23 -08001371 // if (fwd.treatment() != null) {
1372 // for (Instruction instr : fwd.treatment().allInstructions()) {
1373 // if (instr instanceof L3ModificationInstruction &&
1374 // ((L3ModificationInstruction) instr).subtype() == L3SubType.DEC_TTL) {
1375 // tb.deferred().add(instr);
1376 // }
1377 // }
1378 // }
1379
Pier Ventree0ae7a32016-11-23 09:57:42 -08001380 } else if (ethType.ethType().toShort() == Ethernet.TYPE_IPV6) {
1381 if (buildIpv6Selector(filteredSelector, fwd) < 0) {
1382 return Collections.emptyList();
1383 }
Kalhee Kim3d26bf02017-10-04 14:59:11 +00001384 //We need to set the proper next table
1385 IpPrefix ipv6Dst = ((IPCriterion) selector.getCriterion(Criterion.Type.IPV6_DST)).ip();
1386 if (ipv6Dst.isMulticast()) {
1387 forTableId = MULTICAST_ROUTING_TABLE;
1388 } else {
1389 forTableId = UNICAST_ROUTING_TABLE;
1390 }
1391
Charles Chan236653d2018-03-05 11:28:23 -08001392 // TODO decrementing IP ttl is done automatically for routing, this
Charles Chan50d900c2018-03-02 13:26:22 -08001393 // action is ignored or rejected in ofdpa as it is not fully implemented
Charles Chan236653d2018-03-05 11:28:23 -08001394 // if (fwd.treatment() != null) {
1395 // for (Instruction instr : fwd.treatment().allInstructions()) {
1396 // if (instr instanceof L3ModificationInstruction &&
1397 // ((L3ModificationInstruction) instr).subtype() == L3SubType.DEC_TTL) {
1398 // tb.deferred().add(instr);
1399 // }
1400 // }
1401 // }
Saurav Das8a0732e2015-11-20 15:27:53 -08001402 } else {
1403 filteredSelector
1404 .matchEthType(Ethernet.MPLS_UNICAST)
1405 .matchMplsLabel(((MplsCriterion)
1406 selector.getCriterion(Criterion.Type.MPLS_LABEL)).label());
1407 MplsBosCriterion bos = (MplsBosCriterion) selector
Pier Ventre140a8942016-11-02 07:26:38 -07001408 .getCriterion(MPLS_BOS);
Alex Yashchuk4caa8e82017-12-08 17:40:05 +02001409 if (bos != null && requireMplsBosMatch()) {
Saurav Das8a0732e2015-11-20 15:27:53 -08001410 filteredSelector.matchMplsBos(bos.mplsBos());
1411 }
1412 forTableId = MPLS_TABLE_1;
Saurav Das4ce45962015-11-24 23:21:05 -08001413 log.debug("processing MPLS specific forwarding objective {} -> next:{}"
1414 + " in dev {}", fwd.id(), fwd.nextId(), deviceId);
Saurav Das822c4e22015-10-23 10:51:11 -07001415
Charles Chan14967c22015-12-07 11:11:50 -08001416 if (fwd.treatment() != null) {
1417 for (Instruction instr : fwd.treatment().allInstructions()) {
1418 if (instr instanceof L2ModificationInstruction &&
1419 ((L2ModificationInstruction) instr).subtype() == L2SubType.MPLS_POP) {
Pier Luigi3bfe32c2017-01-30 09:47:36 -08001420 // OF-DPA does not pop in MPLS table in some cases. For the L3 VPN, it requires
Saurav Das9c705342017-01-06 11:36:01 -08001421 // setting the MPLS_TYPE so pop can happen down the pipeline
Alex Yashchuk4caa8e82017-12-08 17:40:05 +02001422 if (requireMplsPop()) {
1423 if (mplsNextTable == MPLS_TYPE_TABLE && isNotMplsBos(selector)) {
1424 tb.immediate().popMpls();
1425 }
1426 } else {
1427 // Skip mpls pop action for mpls_unicast label
1428 if (instr instanceof ModMplsHeaderInstruction &&
1429 !((ModMplsHeaderInstruction) instr).ethernetType()
1430 .equals(EtherType.MPLS_UNICAST.ethType())) {
1431 tb.immediate().add(instr);
1432 }
Pier Luigi3bfe32c2017-01-30 09:47:36 -08001433 }
Charles Chan14967c22015-12-07 11:11:50 -08001434 }
Alex Yashchuk4caa8e82017-12-08 17:40:05 +02001435
1436 if (requireMplsTtlModification()) {
1437 if (instr instanceof L3ModificationInstruction &&
1438 ((L3ModificationInstruction) instr).subtype() == L3SubType.DEC_TTL) {
1439 // FIXME Should modify the app to send the correct DEC_MPLS_TTL instruction
1440 tb.immediate().decMplsTtl();
1441 }
1442 if (instr instanceof L3ModificationInstruction &&
1443 ((L3ModificationInstruction) instr).subtype() == L3SubType.TTL_IN) {
1444 tb.immediate().add(instr);
1445 }
Charles Chan14967c22015-12-07 11:11:50 -08001446 }
Saurav Das8a0732e2015-11-20 15:27:53 -08001447 }
1448 }
1449 }
Saurav Das822c4e22015-10-23 10:51:11 -07001450
1451 if (fwd.nextId() != null) {
Saurav Das423fe2b2015-12-04 10:52:59 -08001452 NextGroup next = getGroupForNextObjective(fwd.nextId());
1453 if (next != null) {
1454 List<Deque<GroupKey>> gkeys = appKryo.deserialize(next.data());
1455 // we only need the top level group's key to point the flow to it
1456 Group group = groupService.getGroup(deviceId, gkeys.get(0).peekFirst());
1457 if (group == null) {
1458 log.warn("Group with key:{} for next-id:{} not found in dev:{}",
1459 gkeys.get(0).peekFirst(), fwd.nextId(), deviceId);
1460 fail(fwd, ObjectiveError.GROUPMISSING);
1461 return Collections.emptySet();
1462 }
Saurav Dasa4020382018-02-14 14:14:54 -08001463 if (isNotMplsBos(selector) && group.type().equals(SELECT)) {
1464 log.warn("SR CONTINUE case cannot be handled as MPLS ECMP "
1465 + "is not implemented in OF-DPA yet. Aborting flow {} -> next:{} "
1466 + "in this device {}", fwd.id(), fwd.nextId(), deviceId);
1467 fail(fwd, ObjectiveError.FLOWINSTALLATIONFAILED);
1468 return Collections.emptySet();
1469 }
Saurav Das423fe2b2015-12-04 10:52:59 -08001470 tb.deferred().group(group.id());
Saurav Dasc568c342018-01-25 09:49:01 -08001471 // retrying flows may be necessary due to bug CORD-554
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001472 if (gkeys.size() == 1 && gkeys.get(0).size() == 1) {
Saurav Dasc568c342018-01-25 09:49:01 -08001473 if (shouldRetry()) {
1474 log.warn("Found empty group 0x{} in dev:{} .. will retry fwd:{}",
1475 Integer.toHexString(group.id().id()), deviceId,
1476 fwd.id());
1477 emptyGroup = true;
1478 }
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001479 }
Saurav Das25190812016-05-27 13:54:07 -07001480 } else {
1481 log.warn("Cannot find group for nextId:{} in dev:{}. Aborting fwd:{}",
1482 fwd.nextId(), deviceId, fwd.id());
1483 fail(fwd, ObjectiveError.FLOWINSTALLATIONFAILED);
1484 return Collections.emptySet();
Saurav Das822c4e22015-10-23 10:51:11 -07001485 }
Saurav Das822c4e22015-10-23 10:51:11 -07001486 }
Charles Chancad338a2016-09-16 18:03:11 -07001487
1488 if (forTableId == MPLS_TABLE_1) {
Pier Ventre140a8942016-11-02 07:26:38 -07001489 if (mplsNextTable == MPLS_L3_TYPE_TABLE) {
Charles Chancad338a2016-09-16 18:03:11 -07001490 Ofdpa3SetMplsType setMplsType = new Ofdpa3SetMplsType(Ofdpa3MplsType.L3_PHP);
Saurav Das9c705342017-01-06 11:36:01 -08001491 // set mpls type as apply_action
1492 tb.immediate().extension(setMplsType, deviceId);
Charles Chancad338a2016-09-16 18:03:11 -07001493 }
1494 tb.transition(mplsNextTable);
1495 } else {
1496 tb.transition(ACL_TABLE);
1497 }
1498
Andrea Campanellad980c6d2018-04-30 11:48:55 +02001499 if (fwd.treatment() != null && fwd.treatment().clearedDeferred()) {
1500 if (supportsUnicastBlackHole()) {
1501 tb.wipeDeferred();
1502 } else {
1503 log.warn("Clear Deferred is not supported Unicast Routing Table on device {}", deviceId);
1504 return Collections.emptySet();
1505 }
1506 }
1507
Saurav Das822c4e22015-10-23 10:51:11 -07001508 FlowRule.Builder ruleBuilder = DefaultFlowRule.builder()
1509 .fromApp(fwd.appId())
1510 .withPriority(fwd.priority())
1511 .forDevice(deviceId)
Saurav Das8a0732e2015-11-20 15:27:53 -08001512 .withSelector(filteredSelector.build())
1513 .withTreatment(tb.build())
1514 .forTable(forTableId);
Saurav Das822c4e22015-10-23 10:51:11 -07001515
1516 if (fwd.permanent()) {
1517 ruleBuilder.makePermanent();
1518 } else {
1519 ruleBuilder.makeTemporary(fwd.timeout());
1520 }
Flavio Castroe10fa242016-01-15 12:43:51 -08001521 Collection<FlowRule> flowRuleCollection = new ArrayList<>();
1522 flowRuleCollection.add(ruleBuilder.build());
Pier Luigi3bfe32c2017-01-30 09:47:36 -08001523 if (!allowDefaultRoute) {
Pier Ventree0ae7a32016-11-23 09:57:42 -08001524 flowRuleCollection.add(
1525 defaultRoute(fwd, complementarySelector, forTableId, tb)
1526 );
Flavio Castroe10fa242016-01-15 12:43:51 -08001527 log.debug("Default rule 0.0.0.0/0 is being installed two rules");
1528 }
Saurav Dasc568c342018-01-25 09:49:01 -08001529
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001530 if (emptyGroup) {
pier8b3aef42019-03-11 15:14:02 -07001531 retryExecutorService.schedule(new RetryFlows(fwd, flowRuleCollection),
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001532 RETRY_MS, TimeUnit.MILLISECONDS);
1533 }
Flavio Castroe10fa242016-01-15 12:43:51 -08001534 return flowRuleCollection;
Saurav Das822c4e22015-10-23 10:51:11 -07001535 }
1536
Charles Chan50d900c2018-03-02 13:26:22 -08001537 private int buildIpv4Selector(TrafficSelector.Builder builderToUpdate,
Pier Luigi3bfe32c2017-01-30 09:47:36 -08001538 TrafficSelector.Builder extBuilder,
1539 ForwardingObjective fwd,
1540 boolean allowDefaultRoute) {
1541 TrafficSelector selector = fwd.selector();
1542
1543 IpPrefix ipv4Dst = ((IPCriterion) selector.getCriterion(Criterion.Type.IPV4_DST)).ip();
1544 if (ipv4Dst.isMulticast()) {
1545 if (ipv4Dst.prefixLength() != 32) {
1546 log.warn("Multicast specific forwarding objective can only be /32");
1547 fail(fwd, ObjectiveError.BADPARAMS);
1548 return -1;
1549 }
1550 VlanId assignedVlan = readVlanFromSelector(fwd.meta());
1551 if (assignedVlan == null) {
1552 log.warn("VLAN ID required by multicast specific fwd obj is missing. Abort.");
1553 fail(fwd, ObjectiveError.BADPARAMS);
1554 return -1;
1555 }
Charles Chand1172632017-03-15 17:33:09 -07001556 if (requireVlanExtensions()) {
1557 OfdpaMatchVlanVid ofdpaMatchVlanVid = new OfdpaMatchVlanVid(assignedVlan);
1558 builderToUpdate.extension(ofdpaMatchVlanVid, deviceId);
1559 } else {
1560 builderToUpdate.matchVlanId(assignedVlan);
1561 }
Pier Luigi3bfe32c2017-01-30 09:47:36 -08001562 builderToUpdate.matchEthType(Ethernet.TYPE_IPV4).matchIPDst(ipv4Dst);
1563 log.debug("processing IPv4 multicast specific forwarding objective {} -> next:{}"
1564 + " in dev:{}", fwd.id(), fwd.nextId(), deviceId);
1565 } else {
1566 if (ipv4Dst.prefixLength() == 0) {
1567 if (allowDefaultRoute) {
1568 // The entire IPV4_DST field is wildcarded intentionally
1569 builderToUpdate.matchEthType(Ethernet.TYPE_IPV4);
1570 } else {
1571 // NOTE: The switch does not support matching 0.0.0.0/0
1572 // Split it into 0.0.0.0/1 and 128.0.0.0/1
1573 builderToUpdate.matchEthType(Ethernet.TYPE_IPV4)
1574 .matchIPDst(IpPrefix.valueOf("0.0.0.0/1"));
1575 extBuilder.matchEthType(Ethernet.TYPE_IPV4)
1576 .matchIPDst(IpPrefix.valueOf("128.0.0.0/1"));
1577 }
1578 } else {
1579 builderToUpdate.matchEthType(Ethernet.TYPE_IPV4).matchIPDst(ipv4Dst);
1580 }
1581 log.debug("processing IPv4 unicast specific forwarding objective {} -> next:{}"
1582 + " in dev:{}", fwd.id(), fwd.nextId(), deviceId);
1583 }
1584 return 0;
1585 }
1586
1587 /**
1588 * Helper method to build Ipv6 selector using the selector provided by
1589 * a forwarding objective.
1590 *
1591 * @param builderToUpdate the builder to update
1592 * @param fwd the selector to read
1593 * @return 0 if the update ends correctly. -1 if the matches
1594 * are not yet supported
1595 */
Charles Chan50d900c2018-03-02 13:26:22 -08001596 int buildIpv6Selector(TrafficSelector.Builder builderToUpdate,
Pier Luigi3bfe32c2017-01-30 09:47:36 -08001597 ForwardingObjective fwd) {
1598
1599 TrafficSelector selector = fwd.selector();
1600
1601 IpPrefix ipv6Dst = ((IPCriterion) selector.getCriterion(Criterion.Type.IPV6_DST)).ip();
1602 if (ipv6Dst.isMulticast()) {
Julia Ferguson65428c32017-08-10 18:15:24 +00001603 if (ipv6Dst.prefixLength() != IpAddress.INET6_BIT_LENGTH) {
1604 log.warn("Multicast specific forwarding objective can only be /128");
1605 fail(fwd, ObjectiveError.BADPARAMS);
1606 return -1;
1607 }
1608 VlanId assignedVlan = readVlanFromSelector(fwd.meta());
1609 if (assignedVlan == null) {
1610 log.warn("VLAN ID required by multicast specific fwd obj is missing. Abort.");
1611 fail(fwd, ObjectiveError.BADPARAMS);
1612 return -1;
1613 }
1614 if (requireVlanExtensions()) {
1615 OfdpaMatchVlanVid ofdpaMatchVlanVid = new OfdpaMatchVlanVid(assignedVlan);
1616 builderToUpdate.extension(ofdpaMatchVlanVid, deviceId);
1617 } else {
1618 builderToUpdate.matchVlanId(assignedVlan);
1619 }
1620 builderToUpdate.matchEthType(Ethernet.TYPE_IPV6).matchIPv6Dst(ipv6Dst);
1621 log.debug("processing IPv6 multicast specific forwarding objective {} -> next:{}"
1622 + " in dev:{}", fwd.id(), fwd.nextId(), deviceId);
1623 } else {
1624 if (ipv6Dst.prefixLength() != 0) {
1625 builderToUpdate.matchIPv6Dst(ipv6Dst);
1626 }
Pier Luigi3bfe32c2017-01-30 09:47:36 -08001627 builderToUpdate.matchEthType(Ethernet.TYPE_IPV6);
1628 log.debug("processing IPv6 unicast specific forwarding objective {} -> next:{}"
1629 + " in dev:{}", fwd.id(), fwd.nextId(), deviceId);
Julia Ferguson65428c32017-08-10 18:15:24 +00001630 }
Pier Luigi3bfe32c2017-01-30 09:47:36 -08001631 return 0;
1632 }
1633
Charles Chan50d900c2018-03-02 13:26:22 -08001634 FlowRule defaultRoute(ForwardingObjective fwd,
Pier Ventree0ae7a32016-11-23 09:57:42 -08001635 TrafficSelector.Builder complementarySelector,
1636 int forTableId,
1637 TrafficTreatment.Builder tb) {
1638 FlowRule.Builder rule = DefaultFlowRule.builder()
1639 .fromApp(fwd.appId())
1640 .withPriority(fwd.priority())
1641 .forDevice(deviceId)
1642 .withSelector(complementarySelector.build())
1643 .withTreatment(tb.build())
1644 .forTable(forTableId);
1645 if (fwd.permanent()) {
1646 rule.makePermanent();
1647 } else {
1648 rule.makeTemporary(fwd.timeout());
1649 }
1650 return rule.build();
1651 }
1652
Saurav Das4ce45962015-11-24 23:21:05 -08001653 /**
1654 * Handles forwarding rules to the L2 bridging table. Flow actions are not
1655 * allowed in the bridging table - instead we use L2 Interface group or
1656 * L2 flood group
1657 *
1658 * @param fwd the forwarding objective
1659 * @return A collection of flow rules, or an empty set
1660 */
1661 protected Collection<FlowRule> processEthDstSpecific(ForwardingObjective fwd) {
1662 List<FlowRule> rules = new ArrayList<>();
1663
1664 // Build filtered selector
1665 TrafficSelector selector = fwd.selector();
1666 EthCriterion ethCriterion = (EthCriterion) selector
1667 .getCriterion(Criterion.Type.ETH_DST);
1668 VlanIdCriterion vlanIdCriterion = (VlanIdCriterion) selector
1669 .getCriterion(Criterion.Type.VLAN_VID);
1670
1671 if (vlanIdCriterion == null) {
1672 log.warn("Forwarding objective for bridging requires vlan. Not "
1673 + "installing fwd:{} in dev:{}", fwd.id(), deviceId);
1674 fail(fwd, ObjectiveError.BADPARAMS);
1675 return Collections.emptySet();
1676 }
1677
1678 TrafficSelector.Builder filteredSelectorBuilder =
1679 DefaultTrafficSelector.builder();
Yi Tseng3a77b4f2017-02-06 15:02:17 -08001680
1681 if (!ethCriterion.mac().equals(NONE) &&
1682 !ethCriterion.mac().equals(BROADCAST)) {
Saurav Das4ce45962015-11-24 23:21:05 -08001683 filteredSelectorBuilder.matchEthDst(ethCriterion.mac());
1684 log.debug("processing L2 forwarding objective:{} -> next:{} in dev:{}",
1685 fwd.id(), fwd.nextId(), deviceId);
1686 } else {
Yi Tseng3a77b4f2017-02-06 15:02:17 -08001687 // Use wildcard DST_MAC if the MacAddress is None or Broadcast
Saurav Das4ce45962015-11-24 23:21:05 -08001688 log.debug("processing L2 Broadcast forwarding objective:{} -> next:{} "
1689 + "in dev:{} for vlan:{}",
1690 fwd.id(), fwd.nextId(), deviceId, vlanIdCriterion.vlanId());
1691 }
Charles Chand1172632017-03-15 17:33:09 -07001692 if (requireVlanExtensions()) {
1693 OfdpaMatchVlanVid ofdpaMatchVlanVid = new OfdpaMatchVlanVid(vlanIdCriterion.vlanId());
1694 filteredSelectorBuilder.extension(ofdpaMatchVlanVid, deviceId);
1695 } else {
1696 filteredSelectorBuilder.matchVlanId(vlanIdCriterion.vlanId());
1697 }
Saurav Das4ce45962015-11-24 23:21:05 -08001698 TrafficSelector filteredSelector = filteredSelectorBuilder.build();
1699
1700 if (fwd.treatment() != null) {
1701 log.warn("Ignoring traffic treatment in fwd rule {} meant for L2 table"
1702 + "for dev:{}. Expecting only nextId", fwd.id(), deviceId);
1703 }
1704
1705 TrafficTreatment.Builder treatmentBuilder = DefaultTrafficTreatment.builder();
1706 if (fwd.nextId() != null) {
Saurav Das423fe2b2015-12-04 10:52:59 -08001707 NextGroup next = getGroupForNextObjective(fwd.nextId());
Saurav Das4ce45962015-11-24 23:21:05 -08001708 if (next != null) {
1709 List<Deque<GroupKey>> gkeys = appKryo.deserialize(next.data());
1710 // we only need the top level group's key to point the flow to it
1711 Group group = groupService.getGroup(deviceId, gkeys.get(0).peekFirst());
1712 if (group != null) {
1713 treatmentBuilder.deferred().group(group.id());
1714 } else {
1715 log.warn("Group with key:{} for next-id:{} not found in dev:{}",
1716 gkeys.get(0).peekFirst(), fwd.nextId(), deviceId);
1717 fail(fwd, ObjectiveError.GROUPMISSING);
1718 return Collections.emptySet();
1719 }
1720 }
1721 }
1722 treatmentBuilder.immediate().transition(ACL_TABLE);
1723 TrafficTreatment filteredTreatment = treatmentBuilder.build();
1724
1725 // Build bridging table entries
1726 FlowRule.Builder flowRuleBuilder = DefaultFlowRule.builder();
1727 flowRuleBuilder.fromApp(fwd.appId())
1728 .withPriority(fwd.priority())
1729 .forDevice(deviceId)
1730 .withSelector(filteredSelector)
1731 .withTreatment(filteredTreatment)
1732 .forTable(BRIDGING_TABLE);
1733 if (fwd.permanent()) {
1734 flowRuleBuilder.makePermanent();
1735 } else {
1736 flowRuleBuilder.makeTemporary(fwd.timeout());
1737 }
1738 rules.add(flowRuleBuilder.build());
1739 return rules;
1740 }
1741
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001742 //////////////////////////////////////
1743 // Helper Methods and Classes
1744 //////////////////////////////////////
1745
1746 private boolean isSupportedEthTypeObjective(ForwardingObjective fwd) {
1747 TrafficSelector selector = fwd.selector();
1748 EthTypeCriterion ethType = (EthTypeCriterion) selector
1749 .getCriterion(Criterion.Type.ETH_TYPE);
1750 return !((ethType == null) ||
1751 ((ethType.ethType().toShort() != Ethernet.TYPE_IPV4) &&
Pier Ventree0ae7a32016-11-23 09:57:42 -08001752 (ethType.ethType().toShort() != Ethernet.MPLS_UNICAST)) &&
1753 (ethType.ethType().toShort() != Ethernet.TYPE_IPV6));
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001754 }
1755
1756 private boolean isSupportedEthDstObjective(ForwardingObjective fwd) {
1757 TrafficSelector selector = fwd.selector();
1758 EthCriterion ethDst = (EthCriterion) selector
1759 .getCriterion(Criterion.Type.ETH_DST);
1760 VlanIdCriterion vlanId = (VlanIdCriterion) selector
1761 .getCriterion(Criterion.Type.VLAN_VID);
1762 return !(ethDst == null && vlanId == null);
1763 }
1764
Charles Chan50d900c2018-03-02 13:26:22 -08001765 NextGroup getGroupForNextObjective(Integer nextId) {
Saurav Das423fe2b2015-12-04 10:52:59 -08001766 NextGroup next = flowObjectiveStore.getNextGroup(nextId);
1767 if (next != null) {
1768 List<Deque<GroupKey>> gkeys = appKryo.deserialize(next.data());
1769 if (gkeys != null && !gkeys.isEmpty()) {
1770 return next;
1771 } else {
1772 log.warn("Empty next group found in FlowObjective store for "
1773 + "next-id:{} in dev:{}", nextId, deviceId);
1774 }
1775 } else {
1776 log.warn("next-id {} not found in Flow objective store for dev:{}",
1777 nextId, deviceId);
1778 }
1779 return null;
1780 }
1781
Saurav Das24431192016-03-07 19:13:00 -08001782 @Override
1783 public List<String> getNextMappings(NextGroup nextGroup) {
1784 List<String> mappings = new ArrayList<>();
1785 List<Deque<GroupKey>> gkeys = appKryo.deserialize(nextGroup.data());
1786 for (Deque<GroupKey> gkd : gkeys) {
1787 Group lastGroup = null;
Yuta HIGUCHI2dce08a2017-04-20 21:57:48 -07001788 StringBuilder gchain = new StringBuilder();
Saurav Das24431192016-03-07 19:13:00 -08001789 for (GroupKey gk : gkd) {
1790 Group g = groupService.getGroup(deviceId, gk);
Saurav Das8be4e3a2016-03-11 17:19:07 -08001791 if (g == null) {
Saurav Das25190812016-05-27 13:54:07 -07001792 gchain.append(" NoGrp").append(" -->");
Saurav Das8be4e3a2016-03-11 17:19:07 -08001793 continue;
1794 }
1795 gchain.append(" 0x").append(Integer.toHexString(g.id().id()))
1796 .append(" -->");
Saurav Das24431192016-03-07 19:13:00 -08001797 lastGroup = g;
1798 }
1799 // add port information for last group in group-chain
Yuta HIGUCHI2dce08a2017-04-20 21:57:48 -07001800 List<Instruction> lastGroupIns = new ArrayList<>();
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001801 if (lastGroup != null && !lastGroup.buckets().buckets().isEmpty()) {
Saurav Das25190812016-05-27 13:54:07 -07001802 lastGroupIns = lastGroup.buckets().buckets().get(0)
1803 .treatment().allInstructions();
1804 }
1805 for (Instruction i: lastGroupIns) {
Saurav Das24431192016-03-07 19:13:00 -08001806 if (i instanceof OutputInstruction) {
Saurav Das8be4e3a2016-03-11 17:19:07 -08001807 gchain.append(" port:").append(((OutputInstruction) i).port());
Saurav Das24431192016-03-07 19:13:00 -08001808 }
1809 }
Saurav Das8be4e3a2016-03-11 17:19:07 -08001810 mappings.add(gchain.toString());
Saurav Das24431192016-03-07 19:13:00 -08001811 }
1812 return mappings;
1813 }
Charles Chan5b9df8d2016-03-28 22:21:40 -07001814
Saurav Dasa4020382018-02-14 14:14:54 -08001815 /**
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001816 * Utility class that retries sending flows a fixed number of times, even if
1817 * some of the attempts are successful. Used only for forwarding objectives.
1818 */
Alex Yashchuk4caa8e82017-12-08 17:40:05 +02001819 public final class RetryFlows implements Runnable {
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001820 int attempts = MAX_RETRY_ATTEMPTS;
1821 private Collection<FlowRule> retryFlows;
1822 private ForwardingObjective fwd;
1823
Charles Chan50d900c2018-03-02 13:26:22 -08001824 RetryFlows(ForwardingObjective fwd, Collection<FlowRule> retryFlows) {
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001825 this.fwd = fwd;
1826 this.retryFlows = retryFlows;
1827 }
1828
1829 @Override
1830 public void run() {
1831 log.info("RETRY FLOWS ATTEMPT# {} for fwd:{} rules:{}",
1832 MAX_RETRY_ATTEMPTS - attempts, fwd.id(), retryFlows.size());
pier8b3aef42019-03-11 15:14:02 -07001833 sendForwards(Collections.singletonList(Pair.of(fwd, retryFlows)));
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001834 if (--attempts > 0) {
pier8b3aef42019-03-11 15:14:02 -07001835 retryExecutorService.schedule(this, RETRY_MS, TimeUnit.MILLISECONDS);
1836 }
1837 }
1838 }
1839
1840 // Flow rules accumulator for reducing the number of transactions required to the devices.
1841 private final class ForwardingObjectiveAccumulator
1842 extends AbstractAccumulator<Pair<ForwardingObjective, Collection<FlowRule>>> {
1843
1844 ForwardingObjectiveAccumulator(int maxFwd, int maxBatchMS, int maxIdleMS) {
1845 super(TIMER, maxFwd, maxBatchMS, maxIdleMS);
1846 }
1847
1848 @Override
1849 public void processItems(List<Pair<ForwardingObjective, Collection<FlowRule>>> pairs) {
1850 // Triggers creation of a batch using the list of flowrules generated from fwdobjs.
1851 accumulatorExecutorService.execute(new FlowRulesBuilderTask(pairs));
1852 }
1853 }
1854
1855 // Task for building batch of flow rules in a separate thread.
1856 private final class FlowRulesBuilderTask implements Runnable {
1857 private final List<Pair<ForwardingObjective, Collection<FlowRule>>> pairs;
1858
1859 FlowRulesBuilderTask(List<Pair<ForwardingObjective, Collection<FlowRule>>> pairs) {
1860 this.pairs = pairs;
1861 }
1862
1863 @Override
1864 public void run() {
1865 try {
1866 sendForwards(pairs);
1867 } catch (Exception e) {
1868 log.warn("Unable to send forwards", e);
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001869 }
1870 }
1871 }
1872
Saurav Das822c4e22015-10-23 10:51:11 -07001873}