blob: 3f8cf7700701a82b6703b28736c17d34aaeca8f5 [file] [log] [blame]
Saurav Das822c4e22015-10-23 10:51:11 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2016-present Open Networking Foundation
Saurav Das822c4e22015-10-23 10:51:11 -07003 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
Yi Tsengef19de12017-04-24 11:33:05 -070016package org.onosproject.driver.pipeline.ofdpa;
Saurav Das822c4e22015-10-23 10:51:11 -070017
Charles Chan5270ed02016-01-30 23:22:37 -080018import com.google.common.collect.ImmutableList;
pier8b3aef42019-03-11 15:14:02 -070019import com.google.common.collect.Lists;
Yi Tseng47f82dc2017-03-05 22:48:39 -080020import com.google.common.collect.Sets;
pier8b3aef42019-03-11 15:14:02 -070021import org.apache.commons.lang3.tuple.Pair;
Saurav Das822c4e22015-10-23 10:51:11 -070022import org.onlab.osgi.ServiceDirectory;
Jonghwan Hyun800d9d02018-04-09 09:40:50 -070023import org.onlab.packet.EthType;
Saurav Das822c4e22015-10-23 10:51:11 -070024import org.onlab.packet.Ethernet;
Alex Yashchuk4caa8e82017-12-08 17:40:05 +020025import org.onlab.packet.EthType.EtherType;
Julia Ferguson65428c32017-08-10 18:15:24 +000026import org.onlab.packet.IpAddress;
Charles Chan14967c22015-12-07 11:11:50 -080027import org.onlab.packet.IpPrefix;
Charles Chan45b69ab2018-03-02 15:41:41 -080028import org.onlab.packet.MacAddress;
Saurav Das822c4e22015-10-23 10:51:11 -070029import org.onlab.packet.VlanId;
pier8b3aef42019-03-11 15:14:02 -070030import org.onlab.util.AbstractAccumulator;
31import org.onlab.util.Accumulator;
Saurav Das822c4e22015-10-23 10:51:11 -070032import org.onlab.util.KryoNamespace;
33import org.onosproject.core.ApplicationId;
34import org.onosproject.core.CoreService;
Jonghwan Hyun800d9d02018-04-09 09:40:50 -070035import org.onosproject.driver.extensions.Ofdpa3CopyField;
Charles Chancad338a2016-09-16 18:03:11 -070036import org.onosproject.driver.extensions.Ofdpa3MplsType;
37import org.onosproject.driver.extensions.Ofdpa3SetMplsType;
Jonghwan Hyun800d9d02018-04-09 09:40:50 -070038import org.onosproject.driver.extensions.OfdpaMatchActsetOutput;
39import org.onosproject.driver.extensions.OfdpaMatchAllowVlanTranslation;
Charles Chan14967c22015-12-07 11:11:50 -080040import org.onosproject.driver.extensions.OfdpaMatchVlanVid;
41import org.onosproject.driver.extensions.OfdpaSetVlanVid;
Saurav Das822c4e22015-10-23 10:51:11 -070042import org.onosproject.net.DeviceId;
43import org.onosproject.net.Port;
44import org.onosproject.net.PortNumber;
45import org.onosproject.net.behaviour.NextGroup;
46import org.onosproject.net.behaviour.Pipeliner;
47import org.onosproject.net.behaviour.PipelinerContext;
48import org.onosproject.net.device.DeviceService;
49import org.onosproject.net.driver.AbstractHandlerBehaviour;
50import org.onosproject.net.flow.DefaultFlowRule;
51import org.onosproject.net.flow.DefaultTrafficSelector;
52import org.onosproject.net.flow.DefaultTrafficTreatment;
53import org.onosproject.net.flow.FlowRule;
54import org.onosproject.net.flow.FlowRuleOperations;
55import org.onosproject.net.flow.FlowRuleOperationsContext;
56import org.onosproject.net.flow.FlowRuleService;
57import org.onosproject.net.flow.TrafficSelector;
58import org.onosproject.net.flow.TrafficTreatment;
59import org.onosproject.net.flow.criteria.Criteria;
60import org.onosproject.net.flow.criteria.Criterion;
61import org.onosproject.net.flow.criteria.EthCriterion;
62import org.onosproject.net.flow.criteria.EthTypeCriterion;
63import org.onosproject.net.flow.criteria.IPCriterion;
Pier Ventree0ae7a32016-11-23 09:57:42 -080064import org.onosproject.net.flow.criteria.Icmpv6CodeCriterion;
65import org.onosproject.net.flow.criteria.Icmpv6TypeCriterion;
Saurav Das8a0732e2015-11-20 15:27:53 -080066import org.onosproject.net.flow.criteria.MplsBosCriterion;
67import org.onosproject.net.flow.criteria.MplsCriterion;
Saurav Das822c4e22015-10-23 10:51:11 -070068import org.onosproject.net.flow.criteria.PortCriterion;
Charles Chand9e47c62017-10-05 15:17:15 -070069import org.onosproject.net.flow.criteria.TcpPortCriterion;
70import org.onosproject.net.flow.criteria.UdpPortCriterion;
Saurav Das822c4e22015-10-23 10:51:11 -070071import org.onosproject.net.flow.criteria.VlanIdCriterion;
72import org.onosproject.net.flow.instructions.Instruction;
73import org.onosproject.net.flow.instructions.Instructions.OutputInstruction;
Charles Chanf76de302018-06-15 18:54:18 -070074import org.onosproject.net.flow.instructions.Instructions.NoActionInstruction;
Saurav Das822c4e22015-10-23 10:51:11 -070075import org.onosproject.net.flow.instructions.L2ModificationInstruction;
Saurav Das8a0732e2015-11-20 15:27:53 -080076import org.onosproject.net.flow.instructions.L2ModificationInstruction.L2SubType;
Alex Yashchuk4caa8e82017-12-08 17:40:05 +020077import org.onosproject.net.flow.instructions.L2ModificationInstruction.ModMplsHeaderInstruction;
Charles Chan14967c22015-12-07 11:11:50 -080078import org.onosproject.net.flow.instructions.L3ModificationInstruction;
79import org.onosproject.net.flow.instructions.L3ModificationInstruction.L3SubType;
Saurav Das822c4e22015-10-23 10:51:11 -070080import org.onosproject.net.flowobjective.FilteringObjective;
81import org.onosproject.net.flowobjective.FlowObjectiveStore;
82import org.onosproject.net.flowobjective.ForwardingObjective;
83import org.onosproject.net.flowobjective.NextObjective;
84import org.onosproject.net.flowobjective.Objective;
85import org.onosproject.net.flowobjective.ObjectiveError;
Saurav Das822c4e22015-10-23 10:51:11 -070086import org.onosproject.net.group.DefaultGroupKey;
87import org.onosproject.net.group.Group;
Saurav Das822c4e22015-10-23 10:51:11 -070088import org.onosproject.net.group.GroupKey;
Saurav Das822c4e22015-10-23 10:51:11 -070089import org.onosproject.net.group.GroupService;
Saurav Das822c4e22015-10-23 10:51:11 -070090import org.onosproject.store.serializers.KryoNamespaces;
91import org.slf4j.Logger;
92
Pier Ventree0ae7a32016-11-23 09:57:42 -080093import java.util.ArrayDeque;
94import java.util.ArrayList;
95import java.util.Collection;
96import java.util.Collections;
97import java.util.Deque;
98import java.util.List;
99import java.util.Objects;
Jonghwan Hyun800d9d02018-04-09 09:40:50 -0700100import java.util.Optional;
pier8b3aef42019-03-11 15:14:02 -0700101import java.util.Timer;
Pier Ventree0ae7a32016-11-23 09:57:42 -0800102import java.util.concurrent.ScheduledExecutorService;
103import java.util.concurrent.TimeUnit;
Harshada Chaundkar77958a52019-08-05 15:33:30 +0000104import java.util.concurrent.atomic.AtomicBoolean;
Pier Ventree0ae7a32016-11-23 09:57:42 -0800105
106import static java.util.concurrent.Executors.newScheduledThreadPool;
pier8b3aef42019-03-11 15:14:02 -0700107import static java.util.concurrent.Executors.newSingleThreadScheduledExecutor;
pier5e8b2bc2019-10-04 10:54:07 +0200108import static org.onlab.packet.MacAddress.*;
Pier Ventree0ae7a32016-11-23 09:57:42 -0800109import static org.onlab.util.Tools.groupedThreads;
Jonghwan Hyun800d9d02018-04-09 09:40:50 -0700110import static org.onosproject.driver.extensions.Ofdpa3CopyField.OXM_ID_PACKET_REG_1;
111import static org.onosproject.driver.extensions.Ofdpa3CopyField.OXM_ID_VLAN_VID;
Yi Tsengef19de12017-04-24 11:33:05 -0700112import static org.onosproject.driver.pipeline.ofdpa.OfdpaGroupHandlerUtility.*;
pier9469f3e2019-04-17 17:05:08 +0200113import static org.onosproject.driver.pipeline.ofdpa.OfdpaPipelineUtility.*;
pier7afc7522019-05-10 13:19:15 +0200114import static org.onosproject.net.flowobjective.ForwardingObjective.Flag.SPECIFIC;
Pier Luigi075f1012018-02-01 10:23:12 +0100115import static org.onosproject.net.group.GroupDescription.Type.SELECT;
Pier Ventree0ae7a32016-11-23 09:57:42 -0800116import static org.slf4j.LoggerFactory.getLogger;
Pier Ventre140a8942016-11-02 07:26:38 -0700117import static org.onosproject.net.flow.criteria.Criterion.Type.MPLS_BOS;
Pier Ventree0ae7a32016-11-23 09:57:42 -0800118
Saurav Das822c4e22015-10-23 10:51:11 -0700119/**
120 * Driver for Broadcom's OF-DPA v2.0 TTP.
Saurav Das822c4e22015-10-23 10:51:11 -0700121 */
Charles Chan361154b2016-03-24 10:23:39 -0700122public class Ofdpa2Pipeline extends AbstractHandlerBehaviour implements Pipeliner {
pier8b3aef42019-03-11 15:14:02 -0700123 // Timer for the accumulator
124 private static final Timer TIMER = new Timer("fwdobj-batching");
125 private Accumulator<Pair<ForwardingObjective, Collection<FlowRule>>> accumulator;
pier9469f3e2019-04-17 17:05:08 +0200126 // Internal objects
Saurav Das822c4e22015-10-23 10:51:11 -0700127 private final Logger log = getLogger(getClass());
Charles Chan425854b2016-04-11 15:32:12 -0700128 protected ServiceDirectory serviceDirectory;
Saurav Das822c4e22015-10-23 10:51:11 -0700129 protected FlowRuleService flowRuleService;
Charles Chan425854b2016-04-11 15:32:12 -0700130 protected CoreService coreService;
Saurav Das8a0732e2015-11-20 15:27:53 -0800131 protected GroupService groupService;
132 protected FlowObjectiveStore flowObjectiveStore;
Saurav Das822c4e22015-10-23 10:51:11 -0700133 protected DeviceId deviceId;
134 protected ApplicationId driverId;
Saurav Das822c4e22015-10-23 10:51:11 -0700135 protected DeviceService deviceService;
Charles Chan188ebf52015-12-23 00:15:11 -0800136 protected static KryoNamespace appKryo = new KryoNamespace.Builder()
Yi Tsengef19de12017-04-24 11:33:05 -0700137 .register(KryoNamespaces.API)
138 .register(GroupKey.class)
139 .register(DefaultGroupKey.class)
140 .register(OfdpaNextGroup.class)
141 .register(ArrayDeque.class)
142 .build("Ofdpa2Pipeline");
Saurav Das822c4e22015-10-23 10:51:11 -0700143
Charles Chan425854b2016-04-11 15:32:12 -0700144 protected Ofdpa2GroupHandler groupHandler;
Saurav Das822c4e22015-10-23 10:51:11 -0700145
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700146 // flows installations to be retried
pier8b3aef42019-03-11 15:14:02 -0700147 private ScheduledExecutorService retryExecutorService
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700148 = newScheduledThreadPool(5, groupedThreads("OfdpaPipeliner", "retry-%d", log));
Saurav Das4f980082015-11-05 13:39:15 -0800149
pier8b3aef42019-03-11 15:14:02 -0700150 // accumulator executor service
151 private ScheduledExecutorService accumulatorExecutorService
152 = newSingleThreadScheduledExecutor(groupedThreads("OfdpaPipeliner", "acc-%d", log));
153
Saurav Das822c4e22015-10-23 10:51:11 -0700154 @Override
155 public void init(DeviceId deviceId, PipelinerContext context) {
Saurav Das822c4e22015-10-23 10:51:11 -0700156 this.deviceId = deviceId;
157
Charles Chan425854b2016-04-11 15:32:12 -0700158 serviceDirectory = context.directory();
Saurav Das822c4e22015-10-23 10:51:11 -0700159 coreService = serviceDirectory.get(CoreService.class);
160 flowRuleService = serviceDirectory.get(FlowRuleService.class);
161 groupService = serviceDirectory.get(GroupService.class);
162 flowObjectiveStore = context.store();
Saurav Das822c4e22015-10-23 10:51:11 -0700163 deviceService = serviceDirectory.get(DeviceService.class);
pier8b3aef42019-03-11 15:14:02 -0700164 // Init the accumulator, if enabled
pier9469f3e2019-04-17 17:05:08 +0200165 if (isAccumulatorEnabled(this)) {
pier8b3aef42019-03-11 15:14:02 -0700166 accumulator = new ForwardingObjectiveAccumulator(context.accumulatorMaxObjectives(),
167 context.accumulatorMaxBatchMillis(),
168 context.accumulatorMaxIdleMillis());
169 }
Saurav Das822c4e22015-10-23 10:51:11 -0700170
Charles Chan40132b32017-01-22 00:19:37 -0800171 initDriverId();
172 initGroupHander(context);
Saurav Das822c4e22015-10-23 10:51:11 -0700173
Saurav Das822c4e22015-10-23 10:51:11 -0700174 initializePipeline();
Saurav Das822c4e22015-10-23 10:51:11 -0700175 }
176
pier7afc7522019-05-10 13:19:15 +0200177 void setupAccumulatorForTests(int maxFwd, int maxBatchMS, int maxIdleMS) {
178 if (accumulator == null) {
179 accumulator = new ForwardingObjectiveAccumulator(maxFwd,
180 maxBatchMS,
181 maxIdleMS);
182 }
183 }
184
Charles Chan40132b32017-01-22 00:19:37 -0800185 protected void initDriverId() {
186 driverId = coreService.registerApplication(
187 "org.onosproject.driver.Ofdpa2Pipeline");
188 }
189
190 protected void initGroupHander(PipelinerContext context) {
191 groupHandler = new Ofdpa2GroupHandler();
192 groupHandler.init(deviceId, context);
193 }
194
Saurav Das822c4e22015-10-23 10:51:11 -0700195 protected void initializePipeline() {
Charles Chan188ebf52015-12-23 00:15:11 -0800196 // OF-DPA does not require initializing the pipeline as it puts default
197 // rules automatically in the hardware. However emulation of OFDPA in
198 // software switches does require table-miss-entries.
Saurav Das822c4e22015-10-23 10:51:11 -0700199 }
200
Charles Chand1172632017-03-15 17:33:09 -0700201 /**
Alex Yashchuk4caa8e82017-12-08 17:40:05 +0200202 * Determines whether this pipeline requires MPLS POP instruction.
203 *
204 * @return true to use MPLS POP instruction
205 */
206 public boolean requireMplsPop() {
207 return true;
208 }
209
210 /**
Harshada Chaundkar77958a52019-08-05 15:33:30 +0000211 * Determines whether this pipeline requires one additional flow matching on ethType 0x86dd in ACL table.
212 *
213 * @return true to create one additional flow matching on ethType 0x86dd in ACL table
214 */
215 protected boolean requireEthType() {
216 return true;
217 }
218
219 /**
Alex Yashchuk4caa8e82017-12-08 17:40:05 +0200220 * Determines whether this pipeline requires MPLS BOS match.
221 *
222 * @return true to use MPLS BOS match
223 */
224 public boolean requireMplsBosMatch() {
225 return true;
226 }
227
228 /**
229 * Determines whether this pipeline requires MPLS TTL decrement and copy.
230 *
231 * @return true to use MPLS TTL decrement and copy
232 */
233 public boolean requireMplsTtlModification() {
234 return true;
235 }
236
237 /**
Charles Chand1172632017-03-15 17:33:09 -0700238 * Determines whether this pipeline requires OFDPA match and set VLAN extensions.
239 *
240 * @return true to use the extensions
241 */
242 protected boolean requireVlanExtensions() {
243 return true;
244 }
245
Saurav Das86d13e82017-04-28 17:03:48 -0700246 /**
247 * Determines whether in-port should be matched on in TMAC table rules.
248 *
249 * @return true if match on in-port should be programmed
250 */
251 protected boolean matchInPortTmacTable() {
252 return true;
253 }
254
Charles Chand9e47c62017-10-05 15:17:15 -0700255 /**
256 * Determines whether matching L4 destination port on IPv6 packets is supported in ACL table.
257 *
258 * @return true if matching L4 destination port on IPv6 packets is supported in ACL table.
259 */
260 protected boolean supportIpv6L4Dst() {
261 return true;
262 }
263
Saurav Dasc568c342018-01-25 09:49:01 -0800264 /**
265 * Determines whether this driver should continue to retry flows that point
266 * to empty groups. See CORD-554.
267 *
268 * @return true if the driver should retry flows
269 */
270 protected boolean shouldRetry() {
271 return true;
272 }
273
Charles Chan45b69ab2018-03-02 15:41:41 -0800274 /**
275 * Determines whether this driver requires unicast flow to be installed before multicast flow
276 * in TMAC table.
277 *
278 * @return true if required
279 */
280 protected boolean requireUnicastBeforeMulticast() {
281 return false;
282 }
283
Andrea Campanellad980c6d2018-04-30 11:48:55 +0200284 /**
285 * Determines whether this driver supports installing a clearDeferred action on table 30.
286 *
287 * @return true if required
288 */
289 protected boolean supportsUnicastBlackHole() {
290 return true;
291 }
292
Charles Chanab591602019-01-22 17:25:04 -0800293 protected boolean requirePuntTable() {
294 return false;
295 }
296
Saurav Das822c4e22015-10-23 10:51:11 -0700297 //////////////////////////////////////
298 // Flow Objectives
299 //////////////////////////////////////
300
301 @Override
302 public void filter(FilteringObjective filteringObjective) {
303 if (filteringObjective.type() == FilteringObjective.Type.PERMIT) {
304 processFilter(filteringObjective,
305 filteringObjective.op() == Objective.Operation.ADD,
306 filteringObjective.appId());
307 } else {
308 // Note that packets that don't match the PERMIT filter are
309 // automatically denied. The DENY filter is used to deny packets
310 // that are otherwise permitted by the PERMIT filter.
311 // Use ACL table flow rules here for DENY filtering objectives
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530312 log.warn("filter objective other than PERMIT currently not supported");
Saurav Das822c4e22015-10-23 10:51:11 -0700313 fail(filteringObjective, ObjectiveError.UNSUPPORTED);
314 }
315 }
316
317 @Override
318 public void forward(ForwardingObjective fwd) {
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700319 Collection<FlowRule> rules = processForward(fwd);
Saurav Das25190812016-05-27 13:54:07 -0700320 if (rules == null || rules.isEmpty()) {
321 // Assumes fail message has already been generated to the objective
322 // context. Returning here prevents spurious pass message to be
323 // generated by FlowRule service for empty flowOps.
324 return;
325 }
pier7afc7522019-05-10 13:19:15 +0200326 // Let's accumulate flow rules if accumulator is active and fwd objective is not versatile.
327 // Otherwise send directly, without adding futher delay
328 if (accumulator != null && Objects.equals(fwd.flag(), SPECIFIC)) {
pier8b3aef42019-03-11 15:14:02 -0700329 accumulator.add(Pair.of(fwd, rules));
330 } else {
331 sendForwards(Collections.singletonList(Pair.of(fwd, rules)));
332 }
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700333 }
334
pier8b3aef42019-03-11 15:14:02 -0700335 // Builds the batch using the accumulated flow rules
336 private void sendForwards(List<Pair<ForwardingObjective, Collection<FlowRule>>> pairs) {
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700337 FlowRuleOperations.Builder flowOpsBuilder = FlowRuleOperations.builder();
pier8b3aef42019-03-11 15:14:02 -0700338 log.debug("Sending {} fwd-objs", pairs.size());
339 List<Objective> fwdObjs = Lists.newArrayList();
340 // Iterates over all accumulated flow rules and then build an unique batch
341 pairs.forEach(pair -> {
342 ForwardingObjective fwd = pair.getLeft();
343 Collection<FlowRule> rules = pair.getRight();
344 switch (fwd.op()) {
345 case ADD:
346 rules.stream()
347 .filter(Objects::nonNull)
348 .forEach(flowOpsBuilder::add);
349 log.debug("Applying a add fwd-obj {} to sw:{}", fwd.id(), deviceId);
350 fwdObjs.add(fwd);
351 break;
352 case REMOVE:
353 rules.stream()
354 .filter(Objects::nonNull)
355 .forEach(flowOpsBuilder::remove);
356 log.debug("Deleting a flow rule to sw:{}", deviceId);
357 fwdObjs.add(fwd);
358 break;
359 default:
360 fail(fwd, ObjectiveError.UNKNOWN);
361 log.warn("Unknown forwarding type {}", fwd.op());
362 }
363 });
364 // Finally applies the operations
Saurav Das822c4e22015-10-23 10:51:11 -0700365 flowRuleService.apply(flowOpsBuilder.build(new FlowRuleOperationsContext() {
pier8b3aef42019-03-11 15:14:02 -0700366
Saurav Das822c4e22015-10-23 10:51:11 -0700367 @Override
368 public void onSuccess(FlowRuleOperations ops) {
pier8b3aef42019-03-11 15:14:02 -0700369 log.trace("Flow rule operations onSuccess {}", ops);
pier9469f3e2019-04-17 17:05:08 +0200370 fwdObjs.forEach(OfdpaPipelineUtility::pass);
Saurav Das822c4e22015-10-23 10:51:11 -0700371 }
372
373 @Override
374 public void onError(FlowRuleOperations ops) {
pier8b3aef42019-03-11 15:14:02 -0700375 ObjectiveError error = ObjectiveError.FLOWINSTALLATIONFAILED;
376 log.warn("Flow rule operations onError {}. Reason = {}", ops, error);
377 fwdObjs.forEach(fwdObj -> fail(fwdObj, error));
Saurav Das822c4e22015-10-23 10:51:11 -0700378 }
379 }));
Saurav Das822c4e22015-10-23 10:51:11 -0700380 }
381
382 @Override
383 public void next(NextObjective nextObjective) {
Saurav Das8a0732e2015-11-20 15:27:53 -0800384 NextGroup nextGroup = flowObjectiveStore.getNextGroup(nextObjective.id());
385 switch (nextObjective.op()) {
386 case ADD:
Saurav Das4f980082015-11-05 13:39:15 -0800387 if (nextGroup != null) {
Saurav Das8a0732e2015-11-20 15:27:53 -0800388 log.warn("Cannot add next {} that already exists in device {}",
389 nextObjective.id(), deviceId);
390 return;
391 }
Saurav Das1547b3f2017-05-05 17:01:08 -0700392 log.debug("Processing NextObjective id {} in dev {} - add group",
Saurav Das8a0732e2015-11-20 15:27:53 -0800393 nextObjective.id(), deviceId);
Charles Chan425854b2016-04-11 15:32:12 -0700394 groupHandler.addGroup(nextObjective);
Saurav Das8a0732e2015-11-20 15:27:53 -0800395 break;
396 case ADD_TO_EXISTING:
397 if (nextGroup != null) {
Saurav Das1547b3f2017-05-05 17:01:08 -0700398 log.debug("Processing NextObjective id {} in dev {} - add bucket",
Saurav Das8a0732e2015-11-20 15:27:53 -0800399 nextObjective.id(), deviceId);
Charles Chan425854b2016-04-11 15:32:12 -0700400 groupHandler.addBucketToGroup(nextObjective, nextGroup);
Saurav Das4f980082015-11-05 13:39:15 -0800401 } else {
Saurav Das8a0732e2015-11-20 15:27:53 -0800402 // it is possible that group-chain has not been fully created yet
Saurav Das423fe2b2015-12-04 10:52:59 -0800403 log.debug("Waiting to add bucket to group for next-id:{} in dev:{}",
404 nextObjective.id(), deviceId);
Yi Tseng47f82dc2017-03-05 22:48:39 -0800405
406 // by design multiple pending bucket is allowed for the group
407 groupHandler.pendingBuckets.compute(nextObjective.id(), (nextId, pendBkts) -> {
408 if (pendBkts == null) {
409 pendBkts = Sets.newHashSet();
410 }
411 pendBkts.add(nextObjective);
412 return pendBkts;
413 });
Saurav Das4f980082015-11-05 13:39:15 -0800414 }
Saurav Das8a0732e2015-11-20 15:27:53 -0800415 break;
416 case REMOVE:
417 if (nextGroup == null) {
418 log.warn("Cannot remove next {} that does not exist in device {}",
419 nextObjective.id(), deviceId);
420 return;
421 }
Saurav Das1547b3f2017-05-05 17:01:08 -0700422 log.debug("Processing NextObjective id {} in dev {} - remove group",
Saurav Das8a0732e2015-11-20 15:27:53 -0800423 nextObjective.id(), deviceId);
Charles Chan425854b2016-04-11 15:32:12 -0700424 groupHandler.removeGroup(nextObjective, nextGroup);
Saurav Das8a0732e2015-11-20 15:27:53 -0800425 break;
426 case REMOVE_FROM_EXISTING:
427 if (nextGroup == null) {
428 log.warn("Cannot remove from next {} that does not exist in device {}",
429 nextObjective.id(), deviceId);
430 return;
431 }
Saurav Das1547b3f2017-05-05 17:01:08 -0700432 log.debug("Processing NextObjective id {} in dev {} - remove bucket",
Saurav Das8a0732e2015-11-20 15:27:53 -0800433 nextObjective.id(), deviceId);
Charles Chan425854b2016-04-11 15:32:12 -0700434 groupHandler.removeBucketFromGroup(nextObjective, nextGroup);
Saurav Das8a0732e2015-11-20 15:27:53 -0800435 break;
Jonghwan Hyunf810a7a2018-02-12 16:43:45 +0900436 case MODIFY:
437 if (nextGroup == null) {
438 log.warn("Cannot modify next {} that does not exist in device {}",
439 nextObjective.id(), deviceId);
440 return;
441 }
Ruchi Sahotaef0761c2019-01-28 01:08:18 +0000442 log.debug("Processing NextObjective id {} in dev {} group {} - modify bucket",
443 nextObjective.id(), deviceId, nextGroup);
Jonghwan Hyunf810a7a2018-02-12 16:43:45 +0900444 groupHandler.modifyBucketFromGroup(nextObjective, nextGroup);
445 break;
Saurav Dasceccf242017-08-03 18:30:35 -0700446 case VERIFY:
447 if (nextGroup == null) {
448 log.warn("Cannot verify next {} that does not exist in device {}",
449 nextObjective.id(), deviceId);
450 return;
451 }
452 log.debug("Processing NextObjective id {} in dev {} - verify",
453 nextObjective.id(), deviceId);
454 groupHandler.verifyGroup(nextObjective, nextGroup);
455 break;
Saurav Das8a0732e2015-11-20 15:27:53 -0800456 default:
Saurav Das4f980082015-11-05 13:39:15 -0800457 log.warn("Unsupported operation {}", nextObjective.op());
Saurav Das822c4e22015-10-23 10:51:11 -0700458 }
459 }
460
461 //////////////////////////////////////
462 // Flow handling
463 //////////////////////////////////////
464
465 /**
Yi Tseng0fdc07e2017-04-24 19:17:08 -0700466 * As per OFDPA 2.0 TTP, filtering of VLAN ids and MAC addresses (for routing)
467 * configured on switch ports happen in different tables.
Saurav Das822c4e22015-10-23 10:51:11 -0700468 *
469 * @param filt the filtering objective
470 * @param install indicates whether to add or remove the objective
471 * @param applicationId the application that sent this objective
472 */
Saurav Das52025962016-01-28 22:30:01 -0800473 protected void processFilter(FilteringObjective filt,
474 boolean install, ApplicationId applicationId) {
Saurav Das822c4e22015-10-23 10:51:11 -0700475 // This driver only processes filtering criteria defined with switch
476 // ports as the key
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530477 PortCriterion portCriterion = null;
Saurav Das822c4e22015-10-23 10:51:11 -0700478 EthCriterion ethCriterion = null;
479 VlanIdCriterion vidCriterion = null;
Saurav Das822c4e22015-10-23 10:51:11 -0700480 if (!filt.key().equals(Criteria.dummy()) &&
481 filt.key().type() == Criterion.Type.IN_PORT) {
482 portCriterion = (PortCriterion) filt.key();
Saurav Das822c4e22015-10-23 10:51:11 -0700483 }
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530484 if (portCriterion == null) {
485 log.debug("No IN_PORT defined in filtering objective from app: {}",
486 applicationId);
487 } else {
488 log.debug("Received filtering objective for dev/port: {}/{}", deviceId,
489 portCriterion.port());
490 }
Saurav Das822c4e22015-10-23 10:51:11 -0700491 // convert filtering conditions for switch-intfs into flowrules
492 FlowRuleOperations.Builder ops = FlowRuleOperations.builder();
493 for (Criterion criterion : filt.conditions()) {
Yi Tseng0fdc07e2017-04-24 19:17:08 -0700494 switch (criterion.type()) {
495 case ETH_DST:
496 case ETH_DST_MASKED:
497 ethCriterion = (EthCriterion) criterion;
498 break;
499 case VLAN_VID:
500 vidCriterion = (VlanIdCriterion) criterion;
501 break;
502 default:
503 log.warn("Unsupported filter {}", criterion);
504 fail(filt, ObjectiveError.UNSUPPORTED);
505 return;
Saurav Das822c4e22015-10-23 10:51:11 -0700506 }
507 }
508
Saurav Das0e99e2b2015-10-28 12:39:42 -0700509 VlanId assignedVlan = null;
Charles Chane849c192016-01-11 18:28:54 -0800510 if (vidCriterion != null) {
Charles Chand55e84d2016-03-30 17:54:24 -0700511 // Use the VLAN in criterion if metadata is not present and the traffic is tagged
Charles Chanc550f2e2017-12-19 19:55:57 -0800512 if (!vidCriterion.vlanId().equals(VlanId.NONE)) {
Charles Chane849c192016-01-11 18:28:54 -0800513 assignedVlan = vidCriterion.vlanId();
Piere5bff482018-03-07 11:42:50 +0100514 }
515 // If the meta VLAN is present let's update the assigned vlan
516 if (filt.meta() != null) {
517 VlanId metaVlan = readVlanFromTreatment(filt.meta());
518 if (metaVlan != null) {
519 assignedVlan = metaVlan;
520 }
Charles Chand55e84d2016-03-30 17:54:24 -0700521 }
Charles Chane849c192016-01-11 18:28:54 -0800522
Charles Chand55e84d2016-03-30 17:54:24 -0700523 if (assignedVlan == null) {
524 log.error("Driver fails to extract VLAN information. "
Frank Wangd8ab0962017-08-11 11:09:30 +0800525 + "Not processing VLAN filters on device {}.", deviceId);
Charles Chand55e84d2016-03-30 17:54:24 -0700526 log.debug("VLAN ID in criterion={}, metadata={}",
527 readVlanFromTreatment(filt.meta()), vidCriterion.vlanId());
528 fail(filt, ObjectiveError.BADPARAMS);
529 return;
Saurav Das0e99e2b2015-10-28 12:39:42 -0700530 }
531 }
532
Yi Tseng3a77b4f2017-02-06 15:02:17 -0800533 if (ethCriterion == null || ethCriterion.mac().equals(NONE)) {
Charles Chan985b12e2016-05-11 19:47:22 -0700534 // NOTE: it is possible that a filtering objective only has vidCriterion
Daniel Ginsburgc1d47e92018-04-30 19:27:19 -0400535 log.debug("filtering objective missing dstMac, won't program TMAC table");
Saurav Das822c4e22015-10-23 10:51:11 -0700536 } else {
Charles Chan45b69ab2018-03-02 15:41:41 -0800537 MacAddress unicastMac = readEthDstFromTreatment(filt.meta());
Charles Chan66291502018-03-02 16:43:28 -0800538 List<List<FlowRule>> allStages = processEthDstFilter(portCriterion, ethCriterion,
Charles Chan45b69ab2018-03-02 15:41:41 -0800539 vidCriterion, assignedVlan, unicastMac, applicationId);
Charles Chan66291502018-03-02 16:43:28 -0800540 for (List<FlowRule> flowRules : allStages) {
Charles Chan45b69ab2018-03-02 15:41:41 -0800541 log.trace("Starting a new flow rule stage for TMAC table flow");
Charles Chan66291502018-03-02 16:43:28 -0800542 ops.newStage();
Charles Chanc550f2e2017-12-19 19:55:57 -0800543
Charles Chan66291502018-03-02 16:43:28 -0800544 for (FlowRule flowRule : flowRules) {
pier5e8b2bc2019-10-04 10:54:07 +0200545 log.trace("{} flow rule in TMAC table: {} for dev: {}",
546 (install) ? "adding" : "removing", flowRule, deviceId);
Charles Chan66291502018-03-02 16:43:28 -0800547 if (install) {
548 ops = ops.add(flowRule);
Charles Chanc550f2e2017-12-19 19:55:57 -0800549 } else {
Charles Chan66291502018-03-02 16:43:28 -0800550 // NOTE: Only remove TMAC flow when there is no more enabled port within the
551 // same VLAN on this device if TMAC doesn't support matching on in_port.
pier5e8b2bc2019-10-04 10:54:07 +0200552 if (filt.meta() != null && filt.meta().clearedDeferred()) {
553 // TMac mcast does not match on the input port - we have to remove it
554 // only if this is the last port
555 FlowRule rule = buildTmacRuleForMcastFromUnicast(flowRule, applicationId);
556 // IPv6 or IPv4 tmac rule
557 if (rule != null) {
558 // Add first the mcast rule and then open a new stage for the unicast
559 ops = ops.remove(rule);
560 ops.newStage();
561 }
562 ops = ops.remove(flowRule);
563 } else if (matchInPortTmacTable()) {
Charles Chan66291502018-03-02 16:43:28 -0800564 ops = ops.remove(flowRule);
565 } else {
pier5e8b2bc2019-10-04 10:54:07 +0200566 log.debug("Abort TMAC flow removal on {}. " +
567 "Some other ports still share this TMAC flow", deviceId);
Charles Chan66291502018-03-02 16:43:28 -0800568 }
Charles Chanc550f2e2017-12-19 19:55:57 -0800569 }
570 }
Saurav Das822c4e22015-10-23 10:51:11 -0700571 }
572 }
573
Charles Chan985b12e2016-05-11 19:47:22 -0700574 if (vidCriterion == null) {
575 // NOTE: it is possible that a filtering objective only has ethCriterion
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530576 log.info("filtering objective missing VLAN, cannot program VLAN Table");
Saurav Das822c4e22015-10-23 10:51:11 -0700577 } else {
Charles Chan66291502018-03-02 16:43:28 -0800578 List<List<FlowRule>> allStages = processVlanIdFilter(
pier6aef5b72019-06-10 17:10:26 +0200579 portCriterion, vidCriterion, assignedVlan, applicationId, install);
Charles Chan66291502018-03-02 16:43:28 -0800580 for (List<FlowRule> flowRules : allStages) {
Charles Chan45b69ab2018-03-02 15:41:41 -0800581 log.trace("Starting a new flow rule stage for VLAN table flow");
Charles Chan66291502018-03-02 16:43:28 -0800582 ops.newStage();
Charles Chan14967c22015-12-07 11:11:50 -0800583
Charles Chan66291502018-03-02 16:43:28 -0800584 for (FlowRule flowRule : flowRules) {
585 log.trace("{} flow rules in VLAN table: {} for dev: {}",
586 (install) ? "adding" : "removing", flowRule, deviceId);
587 ops = install ? ops.add(flowRule) : ops.remove(flowRule);
Charles Chand1172632017-03-15 17:33:09 -0700588 }
Saurav Das822c4e22015-10-23 10:51:11 -0700589 }
590 }
591
Saurav Das822c4e22015-10-23 10:51:11 -0700592 // apply filtering flow rules
593 flowRuleService.apply(ops.build(new FlowRuleOperationsContext() {
594 @Override
595 public void onSuccess(FlowRuleOperations ops) {
Saurav Das018605f2017-02-18 14:05:44 -0800596 log.debug("Applied {} filtering rules in device {}",
Saurav Das822c4e22015-10-23 10:51:11 -0700597 ops.stages().get(0).size(), deviceId);
598 pass(filt);
599 }
600
601 @Override
602 public void onError(FlowRuleOperations ops) {
603 log.info("Failed to apply all filtering rules in dev {}", deviceId);
604 fail(filt, ObjectiveError.FLOWINSTALLATIONFAILED);
605 }
606 }));
607
608 }
609
610 /**
Charles Chand1172632017-03-15 17:33:09 -0700611 * Internal implementation of processVlanIdFilter.
612 * <p>
613 * The is_present bit in set_vlan_vid action is required to be 0 in OFDPA i12.
614 * Since it is non-OF spec, we need an extension treatment for that.
615 * The useVlanExtension must be set to false for OFDPA i12.
616 * </p>
Charles Chan66291502018-03-02 16:43:28 -0800617 * <p>
618 * NOTE: Separate VLAN filtering rules and assignment rules
619 * into different stages in order to guarantee that filtering rules
620 * always go first, as required by OFDPA.
621 * </p>
Saurav Das0e99e2b2015-10-28 12:39:42 -0700622 *
Charles Chanf9e98652016-09-07 16:54:23 -0700623 * @param portCriterion port on device for which this filter is programmed
624 * @param vidCriterion vlan assigned to port, or NONE for untagged
625 * @param assignedVlan assigned vlan-id for untagged packets
626 * @param applicationId for application programming this filter
pier6aef5b72019-06-10 17:10:26 +0200627 * @param install indicates whether to add or remove the objective
Charles Chan66291502018-03-02 16:43:28 -0800628 * @return stages of flow rules for port-vlan filters
Saurav Das822c4e22015-10-23 10:51:11 -0700629 */
Charles Chan66291502018-03-02 16:43:28 -0800630 protected List<List<FlowRule>> processVlanIdFilter(PortCriterion portCriterion,
pier6aef5b72019-06-10 17:10:26 +0200631 VlanIdCriterion vidCriterion,
632 VlanId assignedVlan,
633 ApplicationId applicationId,
634 boolean install) {
Charles Chan66291502018-03-02 16:43:28 -0800635 List<FlowRule> filteringRules = new ArrayList<>();
636 List<FlowRule> assignmentRules = new ArrayList<>();
Saurav Das822c4e22015-10-23 10:51:11 -0700637 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
638 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
Charles Chan14967c22015-12-07 11:11:50 -0800639 TrafficSelector.Builder preSelector = null;
640 TrafficTreatment.Builder preTreatment = null;
641
Saurav Das4f980082015-11-05 13:39:15 -0800642 treatment.transition(TMAC_TABLE);
643
Saurav Das822c4e22015-10-23 10:51:11 -0700644 if (vidCriterion.vlanId() == VlanId.NONE) {
645 // untagged packets are assigned vlans
Charles Chand1172632017-03-15 17:33:09 -0700646 preSelector = DefaultTrafficSelector.builder();
647 if (requireVlanExtensions()) {
648 OfdpaMatchVlanVid ofdpaMatchVlanVid = new OfdpaMatchVlanVid(VlanId.NONE);
649 selector.extension(ofdpaMatchVlanVid, deviceId);
Charles Chanf9e98652016-09-07 16:54:23 -0700650 OfdpaSetVlanVid ofdpaSetVlanVid = new OfdpaSetVlanVid(assignedVlan);
651 treatment.extension(ofdpaSetVlanVid, deviceId);
Charles Chand1172632017-03-15 17:33:09 -0700652
653 OfdpaMatchVlanVid preOfdpaMatchVlanVid = new OfdpaMatchVlanVid(assignedVlan);
654 preSelector.extension(preOfdpaMatchVlanVid, deviceId);
Charles Chanf9e98652016-09-07 16:54:23 -0700655 } else {
Charles Chand1172632017-03-15 17:33:09 -0700656 selector.matchVlanId(VlanId.NONE);
Charles Chanf9e98652016-09-07 16:54:23 -0700657 treatment.setVlanId(assignedVlan);
Charles Chand1172632017-03-15 17:33:09 -0700658
659 preSelector.matchVlanId(assignedVlan);
660 }
661 preTreatment = DefaultTrafficTreatment.builder().transition(TMAC_TABLE);
662 } else {
663 if (requireVlanExtensions()) {
664 OfdpaMatchVlanVid ofdpaMatchVlanVid = new OfdpaMatchVlanVid(vidCriterion.vlanId());
665 selector.extension(ofdpaMatchVlanVid, deviceId);
666 } else {
667 selector.matchVlanId(vidCriterion.vlanId());
Charles Chanf9e98652016-09-07 16:54:23 -0700668 }
Charles Chan14967c22015-12-07 11:11:50 -0800669
Charles Chand55e84d2016-03-30 17:54:24 -0700670 if (!assignedVlan.equals(vidCriterion.vlanId())) {
Charles Chand1172632017-03-15 17:33:09 -0700671 if (requireVlanExtensions()) {
Charles Chanc03782d2017-01-31 13:57:55 -0800672 OfdpaSetVlanVid ofdpaSetVlanVid = new OfdpaSetVlanVid(assignedVlan);
673 treatment.extension(ofdpaSetVlanVid, deviceId);
674 } else {
675 treatment.setVlanId(assignedVlan);
676 }
Charles Chand55e84d2016-03-30 17:54:24 -0700677 }
Saurav Das822c4e22015-10-23 10:51:11 -0700678 }
Saurav Das822c4e22015-10-23 10:51:11 -0700679
680 // ofdpa cannot match on ALL portnumber, so we need to use separate
681 // rules for each port.
Charles Chan14967c22015-12-07 11:11:50 -0800682 List<PortNumber> portnums = new ArrayList<>();
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530683 if (portCriterion != null) {
684 if (PortNumber.ALL.equals(portCriterion.port())) {
685 for (Port port : deviceService.getPorts(deviceId)) {
686 if (port.number().toLong() > 0 && port.number().toLong() < OFPP_MAX) {
687 portnums.add(port.number());
688 }
Saurav Das822c4e22015-10-23 10:51:11 -0700689 }
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530690 } else {
691 portnums.add(portCriterion.port());
Saurav Das822c4e22015-10-23 10:51:11 -0700692 }
693 } else {
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530694 log.warn("Filtering Objective missing Port Criterion . " +
Charles Chan66291502018-03-02 16:43:28 -0800695 "VLAN Table cannot be programmed for {}", deviceId);
Saurav Das822c4e22015-10-23 10:51:11 -0700696 }
Saurav Das4f980082015-11-05 13:39:15 -0800697
Saurav Das822c4e22015-10-23 10:51:11 -0700698 for (PortNumber pnum : portnums) {
Saurav Das4f980082015-11-05 13:39:15 -0800699 // create rest of flowrule
Saurav Das822c4e22015-10-23 10:51:11 -0700700 selector.matchInPort(pnum);
701 FlowRule rule = DefaultFlowRule.builder()
702 .forDevice(deviceId)
703 .withSelector(selector.build())
704 .withTreatment(treatment.build())
705 .withPriority(DEFAULT_PRIORITY)
706 .fromApp(applicationId)
707 .makePermanent()
708 .forTable(VLAN_TABLE).build();
Charles Chan66291502018-03-02 16:43:28 -0800709 assignmentRules.add(rule);
Charles Chan14967c22015-12-07 11:11:50 -0800710
711 if (preSelector != null) {
712 preSelector.matchInPort(pnum);
713 FlowRule preRule = DefaultFlowRule.builder()
714 .forDevice(deviceId)
715 .withSelector(preSelector.build())
716 .withTreatment(preTreatment.build())
717 .withPriority(DEFAULT_PRIORITY)
718 .fromApp(applicationId)
719 .makePermanent()
720 .forTable(VLAN_TABLE).build();
Charles Chan66291502018-03-02 16:43:28 -0800721 filteringRules.add(preRule);
Charles Chan14967c22015-12-07 11:11:50 -0800722 }
Saurav Das822c4e22015-10-23 10:51:11 -0700723 }
pier6aef5b72019-06-10 17:10:26 +0200724 return install ? ImmutableList.of(filteringRules, assignmentRules) :
725 ImmutableList.of(assignmentRules, filteringRules);
Saurav Das822c4e22015-10-23 10:51:11 -0700726 }
727
728 /**
729 * Allows routed packets with correct destination MAC to be directed
Charles Chan45b69ab2018-03-02 15:41:41 -0800730 * to unicast routing table, multicast routing table or MPLS forwarding table.
Saurav Das822c4e22015-10-23 10:51:11 -0700731 *
732 * @param portCriterion port on device for which this filter is programmed
733 * @param ethCriterion dstMac of device for which is filter is programmed
734 * @param vidCriterion vlan assigned to port, or NONE for untagged
Saurav Das0e99e2b2015-10-28 12:39:42 -0700735 * @param assignedVlan assigned vlan-id for untagged packets
Charles Chan45b69ab2018-03-02 15:41:41 -0800736 * @param unicastMac some switches require a unicast TMAC flow to be programmed before multicast
737 * TMAC flow. This MAC address will be used for the unicast TMAC flow.
738 * This is unused if the filtering objective is a unicast.
Saurav Das822c4e22015-10-23 10:51:11 -0700739 * @param applicationId for application programming this filter
Charles Chan66291502018-03-02 16:43:28 -0800740 * @return stages of flow rules for port-vlan filters
Saurav Das822c4e22015-10-23 10:51:11 -0700741
742 */
Charles Chan66291502018-03-02 16:43:28 -0800743 protected List<List<FlowRule>> processEthDstFilter(PortCriterion portCriterion,
Saurav Das822c4e22015-10-23 10:51:11 -0700744 EthCriterion ethCriterion,
745 VlanIdCriterion vidCriterion,
Saurav Das0e99e2b2015-10-28 12:39:42 -0700746 VlanId assignedVlan,
Charles Chan45b69ab2018-03-02 15:41:41 -0800747 MacAddress unicastMac,
Saurav Das822c4e22015-10-23 10:51:11 -0700748 ApplicationId applicationId) {
Charles Chan5270ed02016-01-30 23:22:37 -0800749 // Consider PortNumber.ANY as wildcard. Match ETH_DST only
Sivachidambaram Subramanianc598d062017-05-16 22:42:06 +0530750 if (portCriterion != null && PortNumber.ANY.equals(portCriterion.port())) {
Charles Chan5270ed02016-01-30 23:22:37 -0800751 return processEthDstOnlyFilter(ethCriterion, applicationId);
752 }
753
Charles Chan5b9df8d2016-03-28 22:21:40 -0700754 // Multicast MAC
755 if (ethCriterion.mask() != null) {
Charles Chan45b69ab2018-03-02 15:41:41 -0800756 return processMcastEthDstFilter(ethCriterion, assignedVlan, unicastMac, applicationId);
Charles Chan5b9df8d2016-03-28 22:21:40 -0700757 }
758
Saurav Das822c4e22015-10-23 10:51:11 -0700759 //handling untagged packets via assigned VLAN
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530760 if (vidCriterion != null && vidCriterion.vlanId() == VlanId.NONE) {
Saurav Das0e99e2b2015-10-28 12:39:42 -0700761 vidCriterion = (VlanIdCriterion) Criteria.matchVlanId(assignedVlan);
Saurav Das822c4e22015-10-23 10:51:11 -0700762 }
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530763 List<FlowRule> rules = new ArrayList<>();
764 OfdpaMatchVlanVid ofdpaMatchVlanVid = null;
765 if (vidCriterion != null && requireVlanExtensions()) {
766 ofdpaMatchVlanVid = new OfdpaMatchVlanVid(vidCriterion.vlanId());
767 }
Saurav Das822c4e22015-10-23 10:51:11 -0700768 // ofdpa cannot match on ALL portnumber, so we need to use separate
769 // rules for each port.
Yuta HIGUCHI2dce08a2017-04-20 21:57:48 -0700770 List<PortNumber> portnums = new ArrayList<>();
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530771 if (portCriterion != null) {
772 if (PortNumber.ALL.equals(portCriterion.port())) {
773 for (Port port : deviceService.getPorts(deviceId)) {
774 if (port.number().toLong() > 0 && port.number().toLong() < OFPP_MAX) {
775 portnums.add(port.number());
776 }
Saurav Das822c4e22015-10-23 10:51:11 -0700777 }
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530778 } else {
779 portnums.add(portCriterion.port());
780 }
781 for (PortNumber pnum : portnums) {
782 rules.add(buildTmacRuleForIpv4(ethCriterion,
783 vidCriterion,
784 ofdpaMatchVlanVid,
785 applicationId,
786 pnum));
787 rules.add(buildTmacRuleForMpls(ethCriterion,
788 vidCriterion,
789 ofdpaMatchVlanVid,
790 applicationId,
791 pnum));
792 rules.add(buildTmacRuleForIpv6(ethCriterion,
793 vidCriterion,
794 ofdpaMatchVlanVid,
795 applicationId,
796 pnum));
Saurav Das822c4e22015-10-23 10:51:11 -0700797 }
798 } else {
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530799 rules.add(buildTmacRuleForIpv4(ethCriterion,
800 vidCriterion,
801 ofdpaMatchVlanVid,
802 applicationId,
803 null));
804 rules.add(buildTmacRuleForMpls(ethCriterion,
805 vidCriterion,
806 ofdpaMatchVlanVid,
807 applicationId,
808 null));
809 rules.add(buildTmacRuleForIpv6(ethCriterion,
810 vidCriterion,
811 ofdpaMatchVlanVid,
812 applicationId,
813 null));
Saurav Das822c4e22015-10-23 10:51:11 -0700814 }
Charles Chan66291502018-03-02 16:43:28 -0800815 return ImmutableList.of(rules);
Saurav Das822c4e22015-10-23 10:51:11 -0700816 }
817
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530818 /**
819 * Builds TMAC rules for IPv4 packets.
820 *
Charles Chanc550f2e2017-12-19 19:55:57 -0800821 * @param ethCriterion dst mac matching
822 * @param vidCriterion vlan id assigned to the port
823 * @param ofdpaMatchVlanVid OFDPA vlan id matching
824 * @param applicationId application id
825 * @param pnum port number
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530826 * @return TMAC rule for IPV4 packets
827 */
828 private FlowRule buildTmacRuleForIpv4(EthCriterion ethCriterion,
829 VlanIdCriterion vidCriterion,
830 OfdpaMatchVlanVid ofdpaMatchVlanVid,
831 ApplicationId applicationId,
832 PortNumber pnum) {
833 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
834 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
835 if (pnum != null) {
836 if (matchInPortTmacTable()) {
837 selector.matchInPort(pnum);
838 } else {
Saurav Dasf14d9ef2017-12-05 15:00:23 -0800839 log.debug("Pipeline does not support IN_PORT matching in TMAC table, " +
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530840 "ignoring the IN_PORT criteria");
841 }
842 }
843 if (vidCriterion != null) {
844 if (requireVlanExtensions()) {
845 selector.extension(ofdpaMatchVlanVid, deviceId);
846 } else {
847 selector.matchVlanId(vidCriterion.vlanId());
848 }
849 }
850 selector.matchEthType(Ethernet.TYPE_IPV4);
851 selector.matchEthDst(ethCriterion.mac());
852 treatment.transition(UNICAST_ROUTING_TABLE);
853 return DefaultFlowRule.builder()
854 .forDevice(deviceId)
855 .withSelector(selector.build())
856 .withTreatment(treatment.build())
857 .withPriority(DEFAULT_PRIORITY)
858 .fromApp(applicationId)
859 .makePermanent()
860 .forTable(TMAC_TABLE).build();
861 }
862
863 /**
864 * Builds TMAC rule for MPLS packets.
865 *
Charles Chanc550f2e2017-12-19 19:55:57 -0800866 * @param ethCriterion dst mac matching
867 * @param vidCriterion vlan id assigned to the port
868 * @param ofdpaMatchVlanVid OFDPA vlan id matching
869 * @param applicationId application id
870 * @param pnum port number
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530871 * @return TMAC rule for MPLS packets
872 */
873 private FlowRule buildTmacRuleForMpls(EthCriterion ethCriterion,
874 VlanIdCriterion vidCriterion,
875 OfdpaMatchVlanVid ofdpaMatchVlanVid,
876 ApplicationId applicationId,
877 PortNumber pnum) {
878 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
879 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
880 if (pnum != null) {
881 if (matchInPortTmacTable()) {
882 selector.matchInPort(pnum);
883 } else {
Saurav Dasf14d9ef2017-12-05 15:00:23 -0800884 log.debug("Pipeline does not support IN_PORT matching in TMAC table, " +
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530885 "ignoring the IN_PORT criteria");
886 }
887 }
888 if (vidCriterion != null) {
889 if (requireVlanExtensions()) {
890 selector.extension(ofdpaMatchVlanVid, deviceId);
891 } else {
892 selector.matchVlanId(vidCriterion.vlanId());
893 }
894 }
895 selector.matchEthType(Ethernet.MPLS_UNICAST);
896 selector.matchEthDst(ethCriterion.mac());
897 treatment.transition(MPLS_TABLE_0);
898 return DefaultFlowRule.builder()
899 .forDevice(deviceId)
900 .withSelector(selector.build())
901 .withTreatment(treatment.build())
902 .withPriority(DEFAULT_PRIORITY)
903 .fromApp(applicationId)
904 .makePermanent()
905 .forTable(TMAC_TABLE).build();
906 }
907
908 /**
909 * Builds TMAC rules for IPv6 packets.
910 *
Charles Chanc550f2e2017-12-19 19:55:57 -0800911 * @param ethCriterion dst mac matching
912 * @param vidCriterion vlan id assigned to the port
913 * @param ofdpaMatchVlanVid OFDPA vlan id matching
914 * @param applicationId application id
915 * @param pnum port number
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530916 * @return TMAC rule for IPV6 packets
917 */
918 private FlowRule buildTmacRuleForIpv6(EthCriterion ethCriterion,
919 VlanIdCriterion vidCriterion,
920 OfdpaMatchVlanVid ofdpaMatchVlanVid,
921 ApplicationId applicationId,
922 PortNumber pnum) {
923 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
924 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
925 if (pnum != null) {
926 if (matchInPortTmacTable()) {
927 selector.matchInPort(pnum);
928 } else {
Saurav Dasf14d9ef2017-12-05 15:00:23 -0800929 log.debug("Pipeline does not support IN_PORT matching in TMAC table, " +
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530930 "ignoring the IN_PORT criteria");
931 }
932 }
933 if (vidCriterion != null) {
934 if (requireVlanExtensions()) {
935 selector.extension(ofdpaMatchVlanVid, deviceId);
936 } else {
937 selector.matchVlanId(vidCriterion.vlanId());
938 }
939 }
940 selector.matchEthType(Ethernet.TYPE_IPV6);
941 selector.matchEthDst(ethCriterion.mac());
942 treatment.transition(UNICAST_ROUTING_TABLE);
943 return DefaultFlowRule.builder()
944 .forDevice(deviceId)
945 .withSelector(selector.build())
946 .withTreatment(treatment.build())
947 .withPriority(DEFAULT_PRIORITY)
948 .fromApp(applicationId)
949 .makePermanent()
950 .forTable(TMAC_TABLE).build();
951 }
952
Charles Chan66291502018-03-02 16:43:28 -0800953 protected List<List<FlowRule>> processEthDstOnlyFilter(EthCriterion ethCriterion,
Yi Tsengef19de12017-04-24 11:33:05 -0700954 ApplicationId applicationId) {
Pier Luigi0e358632017-01-31 09:35:05 -0800955 ImmutableList.Builder<FlowRule> builder = ImmutableList.builder();
956
Charles Chan5270ed02016-01-30 23:22:37 -0800957 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
958 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
959 selector.matchEthType(Ethernet.TYPE_IPV4);
960 selector.matchEthDst(ethCriterion.mac());
961 treatment.transition(UNICAST_ROUTING_TABLE);
962 FlowRule rule = DefaultFlowRule.builder()
963 .forDevice(deviceId)
964 .withSelector(selector.build())
965 .withTreatment(treatment.build())
966 .withPriority(DEFAULT_PRIORITY)
967 .fromApp(applicationId)
968 .makePermanent()
969 .forTable(TMAC_TABLE).build();
Pier Luigi0e358632017-01-31 09:35:05 -0800970 builder.add(rule);
971
972 selector = DefaultTrafficSelector.builder();
973 treatment = DefaultTrafficTreatment.builder();
974 selector.matchEthType(Ethernet.TYPE_IPV6);
975 selector.matchEthDst(ethCriterion.mac());
976 treatment.transition(UNICAST_ROUTING_TABLE);
977 rule = DefaultFlowRule.builder()
978 .forDevice(deviceId)
979 .withSelector(selector.build())
980 .withTreatment(treatment.build())
981 .withPriority(DEFAULT_PRIORITY)
982 .fromApp(applicationId)
983 .makePermanent()
984 .forTable(TMAC_TABLE).build();
Charles Chan66291502018-03-02 16:43:28 -0800985 return ImmutableList.of(builder.add(rule).build());
Charles Chan5270ed02016-01-30 23:22:37 -0800986 }
987
pier5e8b2bc2019-10-04 10:54:07 +0200988 private FlowRule buildTmacRuleForMcastFromUnicast(FlowRule tmacRuleForUnicast, ApplicationId applicationId) {
989 final TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
990 final TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
991 FlowRule rule;
992 // Build the selector
993 for (Criterion criterion : tmacRuleForUnicast.selector().criteria()) {
994 if (criterion instanceof VlanIdCriterion) {
995 if (requireVlanExtensions()) {
996 OfdpaMatchVlanVid ofdpaMatchVlanVid = new OfdpaMatchVlanVid(((VlanIdCriterion) criterion).vlanId());
997 selector.extension(ofdpaMatchVlanVid, deviceId);
998 } else {
999 selector.add(criterion);
1000 }
1001 } else if (criterion instanceof EthTypeCriterion) {
1002 selector.add(criterion);
1003 if (Objects.equals(((EthTypeCriterion) criterion).ethType(),
1004 EtherType.IPV4.ethType())) {
1005 selector.matchEthDstMasked(IPV4_MULTICAST, IPV4_MULTICAST_MASK);
1006 } else if (Objects.equals(((EthTypeCriterion) criterion).ethType(),
1007 EtherType.IPV6.ethType())) {
1008 selector.matchEthDstMasked(IPV6_MULTICAST, IPV6_MULTICAST_MASK);
1009 } else {
1010 // We don't need for mpls rules
1011 return null;
1012 }
1013 }
1014 }
1015 // Build the treatment
1016 treatment.transition(MULTICAST_ROUTING_TABLE);
1017 // Build the flowrule
1018 rule = DefaultFlowRule.builder()
1019 .forDevice(deviceId)
1020 .withSelector(selector.build())
1021 .withTreatment(treatment.build())
1022 .withPriority(DEFAULT_PRIORITY)
1023 .fromApp(applicationId)
1024 .makePermanent()
1025 .forTable(TMAC_TABLE).build();
1026 log.info("Building flowRule {}", rule);
1027 return rule;
1028 }
1029
Charles Chan66291502018-03-02 16:43:28 -08001030 List<List<FlowRule>> processMcastEthDstFilter(EthCriterion ethCriterion,
Charles Chanb4879a52017-10-20 19:09:16 -07001031 VlanId assignedVlan,
Charles Chan45b69ab2018-03-02 15:41:41 -08001032 MacAddress unicastMac,
Yi Tsengef19de12017-04-24 11:33:05 -07001033 ApplicationId applicationId) {
Charles Chan45b69ab2018-03-02 15:41:41 -08001034 ImmutableList.Builder<FlowRule> unicastFlows = ImmutableList.builder();
1035 ImmutableList.Builder<FlowRule> multicastFlows = ImmutableList.builder();
1036 TrafficSelector.Builder selector;
1037 TrafficTreatment.Builder treatment;
Charles Chanb4879a52017-10-20 19:09:16 -07001038 FlowRule rule;
Julia Ferguson65428c32017-08-10 18:15:24 +00001039
Charles Chanb4879a52017-10-20 19:09:16 -07001040 if (IPV4_MULTICAST.equals(ethCriterion.mac())) {
Charles Chan45b69ab2018-03-02 15:41:41 -08001041 if (requireUnicastBeforeMulticast()) {
1042 selector = DefaultTrafficSelector.builder();
1043 treatment = DefaultTrafficTreatment.builder();
1044 selector.matchEthType(Ethernet.TYPE_IPV4);
1045 selector.matchEthDst(unicastMac);
Charles Chan056e0c12018-05-10 22:19:49 +00001046 selector.matchVlanId(assignedVlan);
Charles Chan45b69ab2018-03-02 15:41:41 -08001047 treatment.transition(UNICAST_ROUTING_TABLE);
1048 rule = DefaultFlowRule.builder()
1049 .forDevice(deviceId)
1050 .withSelector(selector.build())
1051 .withTreatment(treatment.build())
Charles Chan056e0c12018-05-10 22:19:49 +00001052 .withPriority(DEFAULT_PRIORITY)
Charles Chan45b69ab2018-03-02 15:41:41 -08001053 .fromApp(applicationId)
1054 .makePermanent()
1055 .forTable(TMAC_TABLE).build();
1056 unicastFlows.add(rule);
1057 }
1058
1059 selector = DefaultTrafficSelector.builder();
1060 treatment = DefaultTrafficTreatment.builder();
Charles Chanb4879a52017-10-20 19:09:16 -07001061 selector.matchEthType(Ethernet.TYPE_IPV4);
1062 selector.matchEthDstMasked(ethCriterion.mac(), ethCriterion.mask());
Charles Chan056e0c12018-05-10 22:19:49 +00001063 selector.matchVlanId(assignedVlan);
Charles Chanb4879a52017-10-20 19:09:16 -07001064 treatment.transition(MULTICAST_ROUTING_TABLE);
1065 rule = DefaultFlowRule.builder()
1066 .forDevice(deviceId)
1067 .withSelector(selector.build())
1068 .withTreatment(treatment.build())
Charles Chan056e0c12018-05-10 22:19:49 +00001069 .withPriority(DEFAULT_PRIORITY)
Charles Chanb4879a52017-10-20 19:09:16 -07001070 .fromApp(applicationId)
1071 .makePermanent()
1072 .forTable(TMAC_TABLE).build();
Charles Chan45b69ab2018-03-02 15:41:41 -08001073 multicastFlows.add(rule);
Charles Chanb4879a52017-10-20 19:09:16 -07001074 }
1075
1076 if (IPV6_MULTICAST.equals(ethCriterion.mac())) {
Charles Chan45b69ab2018-03-02 15:41:41 -08001077 if (requireUnicastBeforeMulticast()) {
1078 selector = DefaultTrafficSelector.builder();
1079 treatment = DefaultTrafficTreatment.builder();
1080 selector.matchEthType(Ethernet.TYPE_IPV6);
1081 selector.matchEthDst(unicastMac);
Charles Chan056e0c12018-05-10 22:19:49 +00001082 selector.matchVlanId(assignedVlan);
Charles Chan45b69ab2018-03-02 15:41:41 -08001083 treatment.transition(UNICAST_ROUTING_TABLE);
1084 rule = DefaultFlowRule.builder()
1085 .forDevice(deviceId)
1086 .withSelector(selector.build())
1087 .withTreatment(treatment.build())
Charles Chan056e0c12018-05-10 22:19:49 +00001088 .withPriority(DEFAULT_PRIORITY)
Charles Chan45b69ab2018-03-02 15:41:41 -08001089 .fromApp(applicationId)
1090 .makePermanent()
1091 .forTable(TMAC_TABLE).build();
1092 unicastFlows.add(rule);
1093 }
1094
Charles Chanb4879a52017-10-20 19:09:16 -07001095 selector = DefaultTrafficSelector.builder();
1096 treatment = DefaultTrafficTreatment.builder();
1097 selector.matchEthType(Ethernet.TYPE_IPV6);
1098 selector.matchEthDstMasked(ethCriterion.mac(), ethCriterion.mask());
Charles Chan056e0c12018-05-10 22:19:49 +00001099 selector.matchVlanId(assignedVlan);
Charles Chanb4879a52017-10-20 19:09:16 -07001100 treatment.transition(MULTICAST_ROUTING_TABLE);
1101 rule = DefaultFlowRule.builder()
1102 .forDevice(deviceId)
1103 .withSelector(selector.build())
1104 .withTreatment(treatment.build())
Charles Chan056e0c12018-05-10 22:19:49 +00001105 .withPriority(DEFAULT_PRIORITY)
Charles Chanb4879a52017-10-20 19:09:16 -07001106 .fromApp(applicationId)
1107 .makePermanent()
1108 .forTable(TMAC_TABLE).build();
Charles Chan45b69ab2018-03-02 15:41:41 -08001109 multicastFlows.add(rule);
Charles Chanb4879a52017-10-20 19:09:16 -07001110 }
Charles Chan45b69ab2018-03-02 15:41:41 -08001111 return ImmutableList.of(unicastFlows.build(), multicastFlows.build());
Charles Chan5b9df8d2016-03-28 22:21:40 -07001112 }
1113
Saurav Das822c4e22015-10-23 10:51:11 -07001114 private Collection<FlowRule> processForward(ForwardingObjective fwd) {
1115 switch (fwd.flag()) {
1116 case SPECIFIC:
1117 return processSpecific(fwd);
1118 case VERSATILE:
1119 return processVersatile(fwd);
Jonghwan Hyun800d9d02018-04-09 09:40:50 -07001120 case EGRESS:
1121 return processEgress(fwd);
Saurav Das822c4e22015-10-23 10:51:11 -07001122 default:
1123 fail(fwd, ObjectiveError.UNKNOWN);
1124 log.warn("Unknown forwarding flag {}", fwd.flag());
1125 }
1126 return Collections.emptySet();
1127 }
1128
1129 /**
Jonghwan Hyun800d9d02018-04-09 09:40:50 -07001130 * In the OF-DPA 2.0 pipeline, egress forwarding objectives go to the
1131 * egress tables.
1132 * @param fwd the forwarding objective of type 'egress'
1133 * @return a collection of flow rules to be sent to the switch. An empty
1134 * collection may be returned if there is a problem in processing
1135 * the flow rule
1136 */
1137 protected Collection<FlowRule> processEgress(ForwardingObjective fwd) {
1138 log.debug("Processing egress forwarding objective:{} in dev:{}",
1139 fwd, deviceId);
1140
1141 List<FlowRule> rules = new ArrayList<>();
1142
1143 // Build selector
1144 TrafficSelector.Builder sb = DefaultTrafficSelector.builder();
1145 VlanIdCriterion vlanIdCriterion = (VlanIdCriterion) fwd.selector().getCriterion(Criterion.Type.VLAN_VID);
1146 if (vlanIdCriterion == null) {
1147 log.error("Egress forwarding objective:{} must include vlanId", fwd.id());
1148 fail(fwd, ObjectiveError.BADPARAMS);
1149 return rules;
1150 }
1151
1152 Optional<Instruction> outInstr = fwd.treatment().allInstructions().stream()
1153 .filter(instruction -> instruction instanceof OutputInstruction).findFirst();
1154 if (!outInstr.isPresent()) {
1155 log.error("Egress forwarding objective:{} must include output port", fwd.id());
1156 fail(fwd, ObjectiveError.BADPARAMS);
1157 return rules;
1158 }
1159
1160 PortNumber portNumber = ((OutputInstruction) outInstr.get()).port();
1161
1162 sb.matchVlanId(vlanIdCriterion.vlanId());
1163 OfdpaMatchActsetOutput actsetOutput = new OfdpaMatchActsetOutput(portNumber);
1164 sb.extension(actsetOutput, deviceId);
1165
1166 sb.extension(new OfdpaMatchAllowVlanTranslation(ALLOW_VLAN_TRANSLATION), deviceId);
1167
1168 // Build a flow rule for Egress VLAN Flow table
1169 TrafficTreatment.Builder tb = DefaultTrafficTreatment.builder();
1170 tb.transition(EGRESS_DSCP_PCP_REMARK_FLOW_TABLE);
1171 if (fwd.treatment() != null) {
1172 for (Instruction instr : fwd.treatment().allInstructions()) {
1173 if (instr instanceof L2ModificationInstruction &&
1174 ((L2ModificationInstruction) instr).subtype() == L2SubType.VLAN_ID) {
1175 tb.immediate().add(instr);
1176 }
1177 if (instr instanceof L2ModificationInstruction &&
1178 ((L2ModificationInstruction) instr).subtype() == L2SubType.VLAN_PUSH) {
1179 tb.immediate().pushVlan();
1180 EthType ethType = ((L2ModificationInstruction.ModVlanHeaderInstruction) instr).ethernetType();
1181 if (ethType.equals(EtherType.QINQ.ethType())) {
1182 // Build a flow rule for Egress TPID Flow table
1183 TrafficSelector tpidSelector = DefaultTrafficSelector.builder()
1184 .extension(actsetOutput, deviceId)
1185 .matchVlanId(VlanId.ANY).build();
1186
1187 TrafficTreatment tpidTreatment = DefaultTrafficTreatment.builder()
1188 .extension(new Ofdpa3CopyField(COPY_FIELD_NBITS, COPY_FIELD_OFFSET,
1189 COPY_FIELD_OFFSET, OXM_ID_VLAN_VID,
1190 OXM_ID_PACKET_REG_1),
1191 deviceId)
1192 .popVlan()
1193 .pushVlan(EtherType.QINQ.ethType())
1194 .extension(new Ofdpa3CopyField(COPY_FIELD_NBITS, COPY_FIELD_OFFSET,
1195 COPY_FIELD_OFFSET, OXM_ID_PACKET_REG_1,
1196 OXM_ID_VLAN_VID),
1197 deviceId)
1198 .build();
1199
1200 FlowRule.Builder tpidRuleBuilder = DefaultFlowRule.builder()
1201 .fromApp(fwd.appId())
1202 .withPriority(fwd.priority())
1203 .forDevice(deviceId)
1204 .withSelector(tpidSelector)
1205 .withTreatment(tpidTreatment)
1206 .makePermanent()
1207 .forTable(EGRESS_TPID_FLOW_TABLE);
1208 rules.add(tpidRuleBuilder.build());
1209 }
1210 }
1211 }
1212 }
1213
1214 FlowRule.Builder ruleBuilder = DefaultFlowRule.builder()
1215 .fromApp(fwd.appId())
1216 .withPriority(fwd.priority())
1217 .forDevice(deviceId)
1218 .withSelector(sb.build())
1219 .withTreatment(tb.build())
1220 .makePermanent()
1221 .forTable(EGRESS_VLAN_FLOW_TABLE);
1222 rules.add(ruleBuilder.build());
1223 return rules;
1224 }
1225
1226 /**
Saurav Das822c4e22015-10-23 10:51:11 -07001227 * In the OF-DPA 2.0 pipeline, versatile forwarding objectives go to the
1228 * ACL table.
1229 * @param fwd the forwarding objective of type 'versatile'
1230 * @return a collection of flow rules to be sent to the switch. An empty
1231 * collection may be returned if there is a problem in processing
1232 * the flow rule
1233 */
Saurav Das52025962016-01-28 22:30:01 -08001234 protected Collection<FlowRule> processVersatile(ForwardingObjective fwd) {
Saurav Das018605f2017-02-18 14:05:44 -08001235 log.debug("Processing versatile forwarding objective:{} in dev:{}",
Saurav Dasd2fded02016-12-02 15:43:47 -08001236 fwd.id(), deviceId);
Harshada Chaundkar77958a52019-08-05 15:33:30 +00001237 List<FlowRule> flowRules = new ArrayList<>();
1238 final AtomicBoolean ethTypeUsed = new AtomicBoolean(false);
Saurav Das822c4e22015-10-23 10:51:11 -07001239
Saurav Das822c4e22015-10-23 10:51:11 -07001240 if (fwd.nextId() == null && fwd.treatment() == null) {
1241 log.error("Forwarding objective {} from {} must contain "
1242 + "nextId or Treatment", fwd.selector(), fwd.appId());
Zsolt Haraszti9faab752016-02-17 15:55:20 -08001243 fail(fwd, ObjectiveError.BADPARAMS);
Saurav Das822c4e22015-10-23 10:51:11 -07001244 return Collections.emptySet();
1245 }
Saurav Das49cb5a12016-01-16 22:54:07 -08001246
Saurav Das77b5e902016-01-27 17:01:59 -08001247 TrafficSelector.Builder sbuilder = DefaultTrafficSelector.builder();
1248 fwd.selector().criteria().forEach(criterion -> {
1249 if (criterion instanceof VlanIdCriterion) {
1250 VlanId vlanId = ((VlanIdCriterion) criterion).vlanId();
1251 // ensure that match does not include vlan = NONE as OF-DPA does not
1252 // match untagged packets this way in the ACL table.
1253 if (vlanId.equals(VlanId.NONE)) {
1254 return;
1255 }
Charles Chand1172632017-03-15 17:33:09 -07001256 if (requireVlanExtensions()) {
1257 OfdpaMatchVlanVid ofdpaMatchVlanVid = new OfdpaMatchVlanVid(vlanId);
1258 sbuilder.extension(ofdpaMatchVlanVid, deviceId);
1259 } else {
1260 sbuilder.matchVlanId(vlanId);
1261 }
Charles Chan09bf2692018-01-11 11:48:18 -08001262 } else if (criterion instanceof Icmpv6TypeCriterion) {
1263 byte icmpv6Type = (byte) ((Icmpv6TypeCriterion) criterion).icmpv6Type();
1264 sbuilder.matchIcmpv6Type(icmpv6Type);
1265 } else if (criterion instanceof Icmpv6CodeCriterion) {
1266 byte icmpv6Code = (byte) ((Icmpv6CodeCriterion) criterion).icmpv6Code();
1267 sbuilder.matchIcmpv6Type(icmpv6Code);
Charles Chand9e47c62017-10-05 15:17:15 -07001268 } else if (criterion instanceof TcpPortCriterion || criterion instanceof UdpPortCriterion) {
1269 // FIXME: QMX switches do not support L4 dst port matching in ACL table.
1270 // Currently L4 dst port matching is only used by DHCP relay feature
1271 // and therefore is safe to be replaced with L4 src port matching.
1272 // We need to revisit this if L4 dst port is used for other purpose in the future.
1273 if (!supportIpv6L4Dst() && isIpv6(fwd.selector())) {
Charles Chand9e47c62017-10-05 15:17:15 -07001274 switch (criterion.type()) {
1275 case UDP_DST:
Charles Chand9e47c62017-10-05 15:17:15 -07001276 case UDP_DST_MASKED:
Charles Chand9e47c62017-10-05 15:17:15 -07001277 case TCP_DST:
Charles Chand9e47c62017-10-05 15:17:15 -07001278 case TCP_DST_MASKED:
Charles Chand9e47c62017-10-05 15:17:15 -07001279 break;
1280 default:
1281 sbuilder.add(criterion);
1282 }
1283 } else {
1284 sbuilder.add(criterion);
1285 }
Harshada Chaundkar77958a52019-08-05 15:33:30 +00001286 } else if (criterion instanceof EthTypeCriterion) {
1287 sbuilder.add(criterion);
1288 ethTypeUsed.set(true);
Saurav Das77b5e902016-01-27 17:01:59 -08001289 } else {
1290 sbuilder.add(criterion);
1291 }
1292 });
Harshada Chaundkar77958a52019-08-05 15:33:30 +00001293
1294 TrafficTreatment.Builder ttBuilder = versatileTreatmentBuilder(fwd);
1295 if (ttBuilder == null) {
1296 return Collections.emptySet();
1297 }
1298
1299 FlowRule.Builder ruleBuilder = DefaultFlowRule.builder()
1300 .fromApp(fwd.appId())
1301 .withPriority(fwd.priority())
1302 .forDevice(deviceId)
1303 .withSelector(sbuilder.build())
1304 .withTreatment(ttBuilder.build())
1305 .makePermanent()
1306 .forTable(ACL_TABLE);
1307
1308 flowRules.add(ruleBuilder.build());
1309
1310 if (!ethTypeUsed.get() && requireEthType()) {
1311 log.debug("{} doesn't match on ethType but requireEthType is true, adding complementary ACL flow.",
1312 sbuilder.toString());
1313 sbuilder.matchEthType(Ethernet.TYPE_IPV6);
1314 FlowRule.Builder ethTypeRuleBuilder = DefaultFlowRule.builder()
1315 .fromApp(fwd.appId())
1316 .withPriority(fwd.priority())
1317 .forDevice(deviceId)
1318 .withSelector(sbuilder.build())
1319 .withTreatment(ttBuilder.build())
1320 .makePermanent()
1321 .forTable(ACL_TABLE);
1322 flowRules.add(ethTypeRuleBuilder.build());
1323 }
1324 return flowRules;
Charles Chanab591602019-01-22 17:25:04 -08001325 }
Saurav Das77b5e902016-01-27 17:01:59 -08001326
Charles Chanab591602019-01-22 17:25:04 -08001327 /**
1328 * Helper function to create traffic treatment builder for versatile forwarding objectives.
1329 *
1330 * @param fwd original forwarding objective
1331 * @return treatment builder for the flow rule, or null if there is an error.
1332 */
1333 protected TrafficTreatment.Builder versatileTreatmentBuilder(ForwardingObjective fwd) {
Saurav Das822c4e22015-10-23 10:51:11 -07001334 // XXX driver does not currently do type checking as per Tables 65-67 in
1335 // OFDPA 2.0 spec. The only allowed treatment is a punt to the controller.
Saurav Das49cb5a12016-01-16 22:54:07 -08001336 TrafficTreatment.Builder ttBuilder = DefaultTrafficTreatment.builder();
1337 if (fwd.treatment() != null) {
1338 for (Instruction ins : fwd.treatment().allInstructions()) {
1339 if (ins instanceof OutputInstruction) {
1340 OutputInstruction o = (OutputInstruction) ins;
Ray Milkeyfe0e0852018-01-18 11:14:05 -08001341 if (PortNumber.CONTROLLER.equals(o.port())) {
Saurav Das49cb5a12016-01-16 22:54:07 -08001342 ttBuilder.add(o);
1343 } else {
1344 log.warn("Only allowed treatments in versatile forwarding "
1345 + "objectives are punts to the controller");
1346 }
Charles Chanf76de302018-06-15 18:54:18 -07001347 } else if (ins instanceof NoActionInstruction) {
1348 // No action is allowed and nothing needs to be done
Saurav Das49cb5a12016-01-16 22:54:07 -08001349 } else {
1350 log.warn("Cannot process instruction in versatile fwd {}", ins);
1351 }
Saurav Das822c4e22015-10-23 10:51:11 -07001352 }
Charles Chan2df0e8a2017-01-09 11:45:08 -08001353 if (fwd.treatment().clearedDeferred()) {
1354 ttBuilder.wipeDeferred();
1355 }
Saurav Das822c4e22015-10-23 10:51:11 -07001356 }
Saurav Das822c4e22015-10-23 10:51:11 -07001357 if (fwd.nextId() != null) {
Charles Chanab591602019-01-22 17:25:04 -08001358 // Override case
Saurav Das49cb5a12016-01-16 22:54:07 -08001359 NextGroup next = getGroupForNextObjective(fwd.nextId());
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +05301360 if (next == null) {
1361 fail(fwd, ObjectiveError.BADPARAMS);
Charles Chanab591602019-01-22 17:25:04 -08001362 return null;
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +05301363 }
Saurav Das49cb5a12016-01-16 22:54:07 -08001364 List<Deque<GroupKey>> gkeys = appKryo.deserialize(next.data());
1365 // we only need the top level group's key to point the flow to it
1366 Group group = groupService.getGroup(deviceId, gkeys.get(0).peekFirst());
1367 if (group == null) {
1368 log.warn("Group with key:{} for next-id:{} not found in dev:{}",
Charles Chanab591602019-01-22 17:25:04 -08001369 gkeys.get(0).peekFirst(), fwd.nextId(), deviceId);
Saurav Das49cb5a12016-01-16 22:54:07 -08001370 fail(fwd, ObjectiveError.GROUPMISSING);
Charles Chanab591602019-01-22 17:25:04 -08001371 return null;
Saurav Das49cb5a12016-01-16 22:54:07 -08001372 }
1373 ttBuilder.deferred().group(group.id());
Saurav Das822c4e22015-10-23 10:51:11 -07001374 }
Charles Chanab591602019-01-22 17:25:04 -08001375 return ttBuilder;
Saurav Das822c4e22015-10-23 10:51:11 -07001376 }
1377
1378 /**
1379 * In the OF-DPA 2.0 pipeline, specific forwarding refers to the IP table
Saurav Das8a0732e2015-11-20 15:27:53 -08001380 * (unicast or multicast) or the L2 table (mac + vlan) or the MPLS table.
Saurav Das822c4e22015-10-23 10:51:11 -07001381 *
1382 * @param fwd the forwarding objective of type 'specific'
1383 * @return a collection of flow rules. Typically there will be only one
1384 * for this type of forwarding objective. An empty set may be
1385 * returned if there is an issue in processing the objective.
1386 */
Charles Chan50d900c2018-03-02 13:26:22 -08001387 private Collection<FlowRule> processSpecific(ForwardingObjective fwd) {
Saurav Das25190812016-05-27 13:54:07 -07001388 log.debug("Processing specific fwd objective:{} in dev:{} with next:{}",
Saurav Das4ce45962015-11-24 23:21:05 -08001389 fwd.id(), deviceId, fwd.nextId());
1390 boolean isEthTypeObj = isSupportedEthTypeObjective(fwd);
1391 boolean isEthDstObj = isSupportedEthDstObjective(fwd);
1392
1393 if (isEthTypeObj) {
1394 return processEthTypeSpecific(fwd);
1395 } else if (isEthDstObj) {
1396 return processEthDstSpecific(fwd);
1397 } else {
1398 log.warn("processSpecific: Unsupported forwarding objective "
1399 + "criteria fwd:{} in dev:{}", fwd.nextId(), deviceId);
Saurav Das822c4e22015-10-23 10:51:11 -07001400 fail(fwd, ObjectiveError.UNSUPPORTED);
1401 return Collections.emptySet();
1402 }
Saurav Das4ce45962015-11-24 23:21:05 -08001403 }
1404
Saurav Das4ce45962015-11-24 23:21:05 -08001405 /**
1406 * Handles forwarding rules to the IP and MPLS tables.
1407 *
1408 * @param fwd the forwarding objective
1409 * @return A collection of flow rules, or an empty set
1410 */
1411 protected Collection<FlowRule> processEthTypeSpecific(ForwardingObjective fwd) {
Charles Chancad338a2016-09-16 18:03:11 -07001412 return processEthTypeSpecificInternal(fwd, false, ACL_TABLE);
Charles Chanf9e98652016-09-07 16:54:23 -07001413 }
1414
1415 /**
1416 * Internal implementation of processEthTypeSpecific.
1417 * <p>
1418 * Wildcarded IPv4_DST is not supported in OFDPA i12. Therefore, we break
1419 * the rule into 0.0.0.0/1 and 128.0.0.0/1.
1420 * The allowDefaultRoute must be set to false for OFDPA i12.
1421 * </p>
1422 *
1423 * @param fwd the forwarding objective
1424 * @param allowDefaultRoute allow wildcarded IPv4_DST or not
Ray Milkey0bb1e102016-11-10 14:51:27 -08001425 * @param mplsNextTable next MPLS table
Charles Chanf9e98652016-09-07 16:54:23 -07001426 * @return A collection of flow rules, or an empty set
1427 */
1428 protected Collection<FlowRule> processEthTypeSpecificInternal(ForwardingObjective fwd,
Charles Chancad338a2016-09-16 18:03:11 -07001429 boolean allowDefaultRoute,
1430 int mplsNextTable) {
Saurav Das4ce45962015-11-24 23:21:05 -08001431 TrafficSelector selector = fwd.selector();
1432 EthTypeCriterion ethType =
1433 (EthTypeCriterion) selector.getCriterion(Criterion.Type.ETH_TYPE);
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001434 boolean emptyGroup = false;
Charles Chan188ebf52015-12-23 00:15:11 -08001435 int forTableId;
Saurav Das8a0732e2015-11-20 15:27:53 -08001436 TrafficSelector.Builder filteredSelector = DefaultTrafficSelector.builder();
Charles Chan14967c22015-12-07 11:11:50 -08001437 TrafficTreatment.Builder tb = DefaultTrafficTreatment.builder();
Flavio Castroe10fa242016-01-15 12:43:51 -08001438 TrafficSelector.Builder complementarySelector = DefaultTrafficSelector.builder();
Charles Chan14967c22015-12-07 11:11:50 -08001439
Saurav Das8a0732e2015-11-20 15:27:53 -08001440 if (ethType.ethType().toShort() == Ethernet.TYPE_IPV4) {
Pier Luigi3bfe32c2017-01-30 09:47:36 -08001441 if (buildIpv4Selector(filteredSelector, complementarySelector, fwd, allowDefaultRoute) < 0) {
1442 return Collections.emptyList();
1443 }
1444 // We need to set properly the next table
Flavio Castroe10fa242016-01-15 12:43:51 -08001445 IpPrefix ipv4Dst = ((IPCriterion) selector.getCriterion(Criterion.Type.IPV4_DST)).ip();
Charles Chan5b9df8d2016-03-28 22:21:40 -07001446 if (ipv4Dst.isMulticast()) {
Charles Chan5b9df8d2016-03-28 22:21:40 -07001447 forTableId = MULTICAST_ROUTING_TABLE;
Flavio Castroe10fa242016-01-15 12:43:51 -08001448 } else {
Charles Chan5b9df8d2016-03-28 22:21:40 -07001449 forTableId = UNICAST_ROUTING_TABLE;
Flavio Castroe10fa242016-01-15 12:43:51 -08001450 }
Charles Chan50d900c2018-03-02 13:26:22 -08001451
Charles Chan236653d2018-03-05 11:28:23 -08001452 // TODO decrementing IP ttl is done automatically for routing, this
Charles Chan50d900c2018-03-02 13:26:22 -08001453 // action is ignored or rejected in ofdpa as it is not fully implemented
Charles Chan236653d2018-03-05 11:28:23 -08001454 // if (fwd.treatment() != null) {
1455 // for (Instruction instr : fwd.treatment().allInstructions()) {
1456 // if (instr instanceof L3ModificationInstruction &&
1457 // ((L3ModificationInstruction) instr).subtype() == L3SubType.DEC_TTL) {
1458 // tb.deferred().add(instr);
1459 // }
1460 // }
1461 // }
1462
Pier Ventree0ae7a32016-11-23 09:57:42 -08001463 } else if (ethType.ethType().toShort() == Ethernet.TYPE_IPV6) {
1464 if (buildIpv6Selector(filteredSelector, fwd) < 0) {
1465 return Collections.emptyList();
1466 }
Kalhee Kim3d26bf02017-10-04 14:59:11 +00001467 //We need to set the proper next table
1468 IpPrefix ipv6Dst = ((IPCriterion) selector.getCriterion(Criterion.Type.IPV6_DST)).ip();
1469 if (ipv6Dst.isMulticast()) {
1470 forTableId = MULTICAST_ROUTING_TABLE;
1471 } else {
1472 forTableId = UNICAST_ROUTING_TABLE;
1473 }
1474
Charles Chan236653d2018-03-05 11:28:23 -08001475 // TODO decrementing IP ttl is done automatically for routing, this
Charles Chan50d900c2018-03-02 13:26:22 -08001476 // action is ignored or rejected in ofdpa as it is not fully implemented
Charles Chan236653d2018-03-05 11:28:23 -08001477 // if (fwd.treatment() != null) {
1478 // for (Instruction instr : fwd.treatment().allInstructions()) {
1479 // if (instr instanceof L3ModificationInstruction &&
1480 // ((L3ModificationInstruction) instr).subtype() == L3SubType.DEC_TTL) {
1481 // tb.deferred().add(instr);
1482 // }
1483 // }
1484 // }
Saurav Das8a0732e2015-11-20 15:27:53 -08001485 } else {
1486 filteredSelector
1487 .matchEthType(Ethernet.MPLS_UNICAST)
1488 .matchMplsLabel(((MplsCriterion)
1489 selector.getCriterion(Criterion.Type.MPLS_LABEL)).label());
1490 MplsBosCriterion bos = (MplsBosCriterion) selector
Pier Ventre140a8942016-11-02 07:26:38 -07001491 .getCriterion(MPLS_BOS);
Alex Yashchuk4caa8e82017-12-08 17:40:05 +02001492 if (bos != null && requireMplsBosMatch()) {
Saurav Das8a0732e2015-11-20 15:27:53 -08001493 filteredSelector.matchMplsBos(bos.mplsBos());
1494 }
1495 forTableId = MPLS_TABLE_1;
Saurav Das4ce45962015-11-24 23:21:05 -08001496 log.debug("processing MPLS specific forwarding objective {} -> next:{}"
1497 + " in dev {}", fwd.id(), fwd.nextId(), deviceId);
Saurav Das822c4e22015-10-23 10:51:11 -07001498
Charles Chan14967c22015-12-07 11:11:50 -08001499 if (fwd.treatment() != null) {
1500 for (Instruction instr : fwd.treatment().allInstructions()) {
1501 if (instr instanceof L2ModificationInstruction &&
1502 ((L2ModificationInstruction) instr).subtype() == L2SubType.MPLS_POP) {
Pier Luigi3bfe32c2017-01-30 09:47:36 -08001503 // OF-DPA does not pop in MPLS table in some cases. For the L3 VPN, it requires
Saurav Das9c705342017-01-06 11:36:01 -08001504 // setting the MPLS_TYPE so pop can happen down the pipeline
Alex Yashchuk4caa8e82017-12-08 17:40:05 +02001505 if (requireMplsPop()) {
1506 if (mplsNextTable == MPLS_TYPE_TABLE && isNotMplsBos(selector)) {
1507 tb.immediate().popMpls();
1508 }
1509 } else {
1510 // Skip mpls pop action for mpls_unicast label
1511 if (instr instanceof ModMplsHeaderInstruction &&
1512 !((ModMplsHeaderInstruction) instr).ethernetType()
1513 .equals(EtherType.MPLS_UNICAST.ethType())) {
1514 tb.immediate().add(instr);
1515 }
Pier Luigi3bfe32c2017-01-30 09:47:36 -08001516 }
Charles Chan14967c22015-12-07 11:11:50 -08001517 }
Alex Yashchuk4caa8e82017-12-08 17:40:05 +02001518
1519 if (requireMplsTtlModification()) {
1520 if (instr instanceof L3ModificationInstruction &&
1521 ((L3ModificationInstruction) instr).subtype() == L3SubType.DEC_TTL) {
1522 // FIXME Should modify the app to send the correct DEC_MPLS_TTL instruction
1523 tb.immediate().decMplsTtl();
1524 }
1525 if (instr instanceof L3ModificationInstruction &&
1526 ((L3ModificationInstruction) instr).subtype() == L3SubType.TTL_IN) {
1527 tb.immediate().add(instr);
1528 }
Charles Chan14967c22015-12-07 11:11:50 -08001529 }
Saurav Das8a0732e2015-11-20 15:27:53 -08001530 }
1531 }
1532 }
Saurav Das822c4e22015-10-23 10:51:11 -07001533
1534 if (fwd.nextId() != null) {
Saurav Das423fe2b2015-12-04 10:52:59 -08001535 NextGroup next = getGroupForNextObjective(fwd.nextId());
1536 if (next != null) {
1537 List<Deque<GroupKey>> gkeys = appKryo.deserialize(next.data());
1538 // we only need the top level group's key to point the flow to it
1539 Group group = groupService.getGroup(deviceId, gkeys.get(0).peekFirst());
1540 if (group == null) {
1541 log.warn("Group with key:{} for next-id:{} not found in dev:{}",
1542 gkeys.get(0).peekFirst(), fwd.nextId(), deviceId);
1543 fail(fwd, ObjectiveError.GROUPMISSING);
1544 return Collections.emptySet();
1545 }
Saurav Dasa4020382018-02-14 14:14:54 -08001546 if (isNotMplsBos(selector) && group.type().equals(SELECT)) {
1547 log.warn("SR CONTINUE case cannot be handled as MPLS ECMP "
1548 + "is not implemented in OF-DPA yet. Aborting flow {} -> next:{} "
1549 + "in this device {}", fwd.id(), fwd.nextId(), deviceId);
1550 fail(fwd, ObjectiveError.FLOWINSTALLATIONFAILED);
1551 return Collections.emptySet();
1552 }
Saurav Das423fe2b2015-12-04 10:52:59 -08001553 tb.deferred().group(group.id());
Saurav Dasc568c342018-01-25 09:49:01 -08001554 // retrying flows may be necessary due to bug CORD-554
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001555 if (gkeys.size() == 1 && gkeys.get(0).size() == 1) {
Saurav Dasc568c342018-01-25 09:49:01 -08001556 if (shouldRetry()) {
1557 log.warn("Found empty group 0x{} in dev:{} .. will retry fwd:{}",
1558 Integer.toHexString(group.id().id()), deviceId,
1559 fwd.id());
1560 emptyGroup = true;
1561 }
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001562 }
Saurav Das25190812016-05-27 13:54:07 -07001563 } else {
1564 log.warn("Cannot find group for nextId:{} in dev:{}. Aborting fwd:{}",
1565 fwd.nextId(), deviceId, fwd.id());
1566 fail(fwd, ObjectiveError.FLOWINSTALLATIONFAILED);
1567 return Collections.emptySet();
Saurav Das822c4e22015-10-23 10:51:11 -07001568 }
Saurav Das822c4e22015-10-23 10:51:11 -07001569 }
Charles Chancad338a2016-09-16 18:03:11 -07001570
1571 if (forTableId == MPLS_TABLE_1) {
Pier Ventre140a8942016-11-02 07:26:38 -07001572 if (mplsNextTable == MPLS_L3_TYPE_TABLE) {
Charles Chancad338a2016-09-16 18:03:11 -07001573 Ofdpa3SetMplsType setMplsType = new Ofdpa3SetMplsType(Ofdpa3MplsType.L3_PHP);
Saurav Das9c705342017-01-06 11:36:01 -08001574 // set mpls type as apply_action
1575 tb.immediate().extension(setMplsType, deviceId);
Charles Chancad338a2016-09-16 18:03:11 -07001576 }
1577 tb.transition(mplsNextTable);
1578 } else {
1579 tb.transition(ACL_TABLE);
1580 }
1581
Andrea Campanellad980c6d2018-04-30 11:48:55 +02001582 if (fwd.treatment() != null && fwd.treatment().clearedDeferred()) {
1583 if (supportsUnicastBlackHole()) {
1584 tb.wipeDeferred();
1585 } else {
1586 log.warn("Clear Deferred is not supported Unicast Routing Table on device {}", deviceId);
1587 return Collections.emptySet();
1588 }
1589 }
1590
Saurav Das822c4e22015-10-23 10:51:11 -07001591 FlowRule.Builder ruleBuilder = DefaultFlowRule.builder()
1592 .fromApp(fwd.appId())
1593 .withPriority(fwd.priority())
1594 .forDevice(deviceId)
Saurav Das8a0732e2015-11-20 15:27:53 -08001595 .withSelector(filteredSelector.build())
1596 .withTreatment(tb.build())
1597 .forTable(forTableId);
Saurav Das822c4e22015-10-23 10:51:11 -07001598
1599 if (fwd.permanent()) {
1600 ruleBuilder.makePermanent();
1601 } else {
1602 ruleBuilder.makeTemporary(fwd.timeout());
1603 }
Flavio Castroe10fa242016-01-15 12:43:51 -08001604 Collection<FlowRule> flowRuleCollection = new ArrayList<>();
1605 flowRuleCollection.add(ruleBuilder.build());
Pier Luigi3bfe32c2017-01-30 09:47:36 -08001606 if (!allowDefaultRoute) {
Pier Ventree0ae7a32016-11-23 09:57:42 -08001607 flowRuleCollection.add(
1608 defaultRoute(fwd, complementarySelector, forTableId, tb)
1609 );
Flavio Castroe10fa242016-01-15 12:43:51 -08001610 log.debug("Default rule 0.0.0.0/0 is being installed two rules");
1611 }
Saurav Dasc568c342018-01-25 09:49:01 -08001612
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001613 if (emptyGroup) {
pier8b3aef42019-03-11 15:14:02 -07001614 retryExecutorService.schedule(new RetryFlows(fwd, flowRuleCollection),
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001615 RETRY_MS, TimeUnit.MILLISECONDS);
1616 }
Flavio Castroe10fa242016-01-15 12:43:51 -08001617 return flowRuleCollection;
Saurav Das822c4e22015-10-23 10:51:11 -07001618 }
1619
Charles Chan50d900c2018-03-02 13:26:22 -08001620 private int buildIpv4Selector(TrafficSelector.Builder builderToUpdate,
Pier Luigi3bfe32c2017-01-30 09:47:36 -08001621 TrafficSelector.Builder extBuilder,
1622 ForwardingObjective fwd,
1623 boolean allowDefaultRoute) {
1624 TrafficSelector selector = fwd.selector();
1625
1626 IpPrefix ipv4Dst = ((IPCriterion) selector.getCriterion(Criterion.Type.IPV4_DST)).ip();
1627 if (ipv4Dst.isMulticast()) {
1628 if (ipv4Dst.prefixLength() != 32) {
1629 log.warn("Multicast specific forwarding objective can only be /32");
1630 fail(fwd, ObjectiveError.BADPARAMS);
1631 return -1;
1632 }
1633 VlanId assignedVlan = readVlanFromSelector(fwd.meta());
1634 if (assignedVlan == null) {
1635 log.warn("VLAN ID required by multicast specific fwd obj is missing. Abort.");
1636 fail(fwd, ObjectiveError.BADPARAMS);
1637 return -1;
1638 }
Charles Chand1172632017-03-15 17:33:09 -07001639 if (requireVlanExtensions()) {
1640 OfdpaMatchVlanVid ofdpaMatchVlanVid = new OfdpaMatchVlanVid(assignedVlan);
1641 builderToUpdate.extension(ofdpaMatchVlanVid, deviceId);
1642 } else {
1643 builderToUpdate.matchVlanId(assignedVlan);
1644 }
Pier Luigi3bfe32c2017-01-30 09:47:36 -08001645 builderToUpdate.matchEthType(Ethernet.TYPE_IPV4).matchIPDst(ipv4Dst);
1646 log.debug("processing IPv4 multicast specific forwarding objective {} -> next:{}"
1647 + " in dev:{}", fwd.id(), fwd.nextId(), deviceId);
1648 } else {
1649 if (ipv4Dst.prefixLength() == 0) {
1650 if (allowDefaultRoute) {
1651 // The entire IPV4_DST field is wildcarded intentionally
1652 builderToUpdate.matchEthType(Ethernet.TYPE_IPV4);
1653 } else {
1654 // NOTE: The switch does not support matching 0.0.0.0/0
1655 // Split it into 0.0.0.0/1 and 128.0.0.0/1
1656 builderToUpdate.matchEthType(Ethernet.TYPE_IPV4)
1657 .matchIPDst(IpPrefix.valueOf("0.0.0.0/1"));
1658 extBuilder.matchEthType(Ethernet.TYPE_IPV4)
1659 .matchIPDst(IpPrefix.valueOf("128.0.0.0/1"));
1660 }
1661 } else {
1662 builderToUpdate.matchEthType(Ethernet.TYPE_IPV4).matchIPDst(ipv4Dst);
1663 }
1664 log.debug("processing IPv4 unicast specific forwarding objective {} -> next:{}"
1665 + " in dev:{}", fwd.id(), fwd.nextId(), deviceId);
1666 }
1667 return 0;
1668 }
1669
1670 /**
1671 * Helper method to build Ipv6 selector using the selector provided by
1672 * a forwarding objective.
1673 *
1674 * @param builderToUpdate the builder to update
1675 * @param fwd the selector to read
1676 * @return 0 if the update ends correctly. -1 if the matches
1677 * are not yet supported
1678 */
Charles Chan50d900c2018-03-02 13:26:22 -08001679 int buildIpv6Selector(TrafficSelector.Builder builderToUpdate,
Pier Luigi3bfe32c2017-01-30 09:47:36 -08001680 ForwardingObjective fwd) {
1681
1682 TrafficSelector selector = fwd.selector();
1683
1684 IpPrefix ipv6Dst = ((IPCriterion) selector.getCriterion(Criterion.Type.IPV6_DST)).ip();
1685 if (ipv6Dst.isMulticast()) {
Julia Ferguson65428c32017-08-10 18:15:24 +00001686 if (ipv6Dst.prefixLength() != IpAddress.INET6_BIT_LENGTH) {
1687 log.warn("Multicast specific forwarding objective can only be /128");
1688 fail(fwd, ObjectiveError.BADPARAMS);
1689 return -1;
1690 }
1691 VlanId assignedVlan = readVlanFromSelector(fwd.meta());
1692 if (assignedVlan == null) {
1693 log.warn("VLAN ID required by multicast specific fwd obj is missing. Abort.");
1694 fail(fwd, ObjectiveError.BADPARAMS);
1695 return -1;
1696 }
1697 if (requireVlanExtensions()) {
1698 OfdpaMatchVlanVid ofdpaMatchVlanVid = new OfdpaMatchVlanVid(assignedVlan);
1699 builderToUpdate.extension(ofdpaMatchVlanVid, deviceId);
1700 } else {
1701 builderToUpdate.matchVlanId(assignedVlan);
1702 }
1703 builderToUpdate.matchEthType(Ethernet.TYPE_IPV6).matchIPv6Dst(ipv6Dst);
1704 log.debug("processing IPv6 multicast specific forwarding objective {} -> next:{}"
1705 + " in dev:{}", fwd.id(), fwd.nextId(), deviceId);
1706 } else {
1707 if (ipv6Dst.prefixLength() != 0) {
1708 builderToUpdate.matchIPv6Dst(ipv6Dst);
1709 }
Pier Luigi3bfe32c2017-01-30 09:47:36 -08001710 builderToUpdate.matchEthType(Ethernet.TYPE_IPV6);
1711 log.debug("processing IPv6 unicast specific forwarding objective {} -> next:{}"
1712 + " in dev:{}", fwd.id(), fwd.nextId(), deviceId);
Julia Ferguson65428c32017-08-10 18:15:24 +00001713 }
Pier Luigi3bfe32c2017-01-30 09:47:36 -08001714 return 0;
1715 }
1716
Charles Chan50d900c2018-03-02 13:26:22 -08001717 FlowRule defaultRoute(ForwardingObjective fwd,
Pier Ventree0ae7a32016-11-23 09:57:42 -08001718 TrafficSelector.Builder complementarySelector,
1719 int forTableId,
1720 TrafficTreatment.Builder tb) {
1721 FlowRule.Builder rule = DefaultFlowRule.builder()
1722 .fromApp(fwd.appId())
1723 .withPriority(fwd.priority())
1724 .forDevice(deviceId)
1725 .withSelector(complementarySelector.build())
1726 .withTreatment(tb.build())
1727 .forTable(forTableId);
1728 if (fwd.permanent()) {
1729 rule.makePermanent();
1730 } else {
1731 rule.makeTemporary(fwd.timeout());
1732 }
1733 return rule.build();
1734 }
1735
Saurav Das4ce45962015-11-24 23:21:05 -08001736 /**
1737 * Handles forwarding rules to the L2 bridging table. Flow actions are not
1738 * allowed in the bridging table - instead we use L2 Interface group or
1739 * L2 flood group
1740 *
1741 * @param fwd the forwarding objective
1742 * @return A collection of flow rules, or an empty set
1743 */
1744 protected Collection<FlowRule> processEthDstSpecific(ForwardingObjective fwd) {
1745 List<FlowRule> rules = new ArrayList<>();
1746
1747 // Build filtered selector
1748 TrafficSelector selector = fwd.selector();
1749 EthCriterion ethCriterion = (EthCriterion) selector
1750 .getCriterion(Criterion.Type.ETH_DST);
1751 VlanIdCriterion vlanIdCriterion = (VlanIdCriterion) selector
1752 .getCriterion(Criterion.Type.VLAN_VID);
1753
1754 if (vlanIdCriterion == null) {
1755 log.warn("Forwarding objective for bridging requires vlan. Not "
1756 + "installing fwd:{} in dev:{}", fwd.id(), deviceId);
1757 fail(fwd, ObjectiveError.BADPARAMS);
1758 return Collections.emptySet();
1759 }
1760
1761 TrafficSelector.Builder filteredSelectorBuilder =
1762 DefaultTrafficSelector.builder();
Yi Tseng3a77b4f2017-02-06 15:02:17 -08001763
1764 if (!ethCriterion.mac().equals(NONE) &&
1765 !ethCriterion.mac().equals(BROADCAST)) {
Saurav Das4ce45962015-11-24 23:21:05 -08001766 filteredSelectorBuilder.matchEthDst(ethCriterion.mac());
1767 log.debug("processing L2 forwarding objective:{} -> next:{} in dev:{}",
1768 fwd.id(), fwd.nextId(), deviceId);
1769 } else {
Yi Tseng3a77b4f2017-02-06 15:02:17 -08001770 // Use wildcard DST_MAC if the MacAddress is None or Broadcast
Saurav Das4ce45962015-11-24 23:21:05 -08001771 log.debug("processing L2 Broadcast forwarding objective:{} -> next:{} "
1772 + "in dev:{} for vlan:{}",
1773 fwd.id(), fwd.nextId(), deviceId, vlanIdCriterion.vlanId());
1774 }
Charles Chand1172632017-03-15 17:33:09 -07001775 if (requireVlanExtensions()) {
1776 OfdpaMatchVlanVid ofdpaMatchVlanVid = new OfdpaMatchVlanVid(vlanIdCriterion.vlanId());
1777 filteredSelectorBuilder.extension(ofdpaMatchVlanVid, deviceId);
1778 } else {
1779 filteredSelectorBuilder.matchVlanId(vlanIdCriterion.vlanId());
1780 }
Saurav Das4ce45962015-11-24 23:21:05 -08001781 TrafficSelector filteredSelector = filteredSelectorBuilder.build();
1782
1783 if (fwd.treatment() != null) {
1784 log.warn("Ignoring traffic treatment in fwd rule {} meant for L2 table"
1785 + "for dev:{}. Expecting only nextId", fwd.id(), deviceId);
1786 }
1787
1788 TrafficTreatment.Builder treatmentBuilder = DefaultTrafficTreatment.builder();
1789 if (fwd.nextId() != null) {
Saurav Das423fe2b2015-12-04 10:52:59 -08001790 NextGroup next = getGroupForNextObjective(fwd.nextId());
Saurav Das4ce45962015-11-24 23:21:05 -08001791 if (next != null) {
1792 List<Deque<GroupKey>> gkeys = appKryo.deserialize(next.data());
1793 // we only need the top level group's key to point the flow to it
1794 Group group = groupService.getGroup(deviceId, gkeys.get(0).peekFirst());
1795 if (group != null) {
1796 treatmentBuilder.deferred().group(group.id());
1797 } else {
1798 log.warn("Group with key:{} for next-id:{} not found in dev:{}",
1799 gkeys.get(0).peekFirst(), fwd.nextId(), deviceId);
1800 fail(fwd, ObjectiveError.GROUPMISSING);
1801 return Collections.emptySet();
1802 }
1803 }
1804 }
1805 treatmentBuilder.immediate().transition(ACL_TABLE);
1806 TrafficTreatment filteredTreatment = treatmentBuilder.build();
1807
1808 // Build bridging table entries
1809 FlowRule.Builder flowRuleBuilder = DefaultFlowRule.builder();
1810 flowRuleBuilder.fromApp(fwd.appId())
1811 .withPriority(fwd.priority())
1812 .forDevice(deviceId)
1813 .withSelector(filteredSelector)
1814 .withTreatment(filteredTreatment)
1815 .forTable(BRIDGING_TABLE);
1816 if (fwd.permanent()) {
1817 flowRuleBuilder.makePermanent();
1818 } else {
1819 flowRuleBuilder.makeTemporary(fwd.timeout());
1820 }
1821 rules.add(flowRuleBuilder.build());
1822 return rules;
1823 }
1824
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001825 //////////////////////////////////////
1826 // Helper Methods and Classes
1827 //////////////////////////////////////
1828
1829 private boolean isSupportedEthTypeObjective(ForwardingObjective fwd) {
1830 TrafficSelector selector = fwd.selector();
1831 EthTypeCriterion ethType = (EthTypeCriterion) selector
1832 .getCriterion(Criterion.Type.ETH_TYPE);
1833 return !((ethType == null) ||
1834 ((ethType.ethType().toShort() != Ethernet.TYPE_IPV4) &&
Pier Ventree0ae7a32016-11-23 09:57:42 -08001835 (ethType.ethType().toShort() != Ethernet.MPLS_UNICAST)) &&
1836 (ethType.ethType().toShort() != Ethernet.TYPE_IPV6));
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001837 }
1838
1839 private boolean isSupportedEthDstObjective(ForwardingObjective fwd) {
1840 TrafficSelector selector = fwd.selector();
1841 EthCriterion ethDst = (EthCriterion) selector
1842 .getCriterion(Criterion.Type.ETH_DST);
1843 VlanIdCriterion vlanId = (VlanIdCriterion) selector
1844 .getCriterion(Criterion.Type.VLAN_VID);
1845 return !(ethDst == null && vlanId == null);
1846 }
1847
Charles Chan50d900c2018-03-02 13:26:22 -08001848 NextGroup getGroupForNextObjective(Integer nextId) {
Saurav Das423fe2b2015-12-04 10:52:59 -08001849 NextGroup next = flowObjectiveStore.getNextGroup(nextId);
1850 if (next != null) {
1851 List<Deque<GroupKey>> gkeys = appKryo.deserialize(next.data());
1852 if (gkeys != null && !gkeys.isEmpty()) {
1853 return next;
1854 } else {
1855 log.warn("Empty next group found in FlowObjective store for "
1856 + "next-id:{} in dev:{}", nextId, deviceId);
1857 }
1858 } else {
1859 log.warn("next-id {} not found in Flow objective store for dev:{}",
1860 nextId, deviceId);
1861 }
1862 return null;
1863 }
1864
Saurav Das24431192016-03-07 19:13:00 -08001865 @Override
1866 public List<String> getNextMappings(NextGroup nextGroup) {
1867 List<String> mappings = new ArrayList<>();
1868 List<Deque<GroupKey>> gkeys = appKryo.deserialize(nextGroup.data());
1869 for (Deque<GroupKey> gkd : gkeys) {
1870 Group lastGroup = null;
Yuta HIGUCHI2dce08a2017-04-20 21:57:48 -07001871 StringBuilder gchain = new StringBuilder();
Saurav Das24431192016-03-07 19:13:00 -08001872 for (GroupKey gk : gkd) {
1873 Group g = groupService.getGroup(deviceId, gk);
Saurav Das8be4e3a2016-03-11 17:19:07 -08001874 if (g == null) {
Saurav Das25190812016-05-27 13:54:07 -07001875 gchain.append(" NoGrp").append(" -->");
Saurav Das8be4e3a2016-03-11 17:19:07 -08001876 continue;
1877 }
1878 gchain.append(" 0x").append(Integer.toHexString(g.id().id()))
1879 .append(" -->");
Saurav Das24431192016-03-07 19:13:00 -08001880 lastGroup = g;
1881 }
1882 // add port information for last group in group-chain
Yuta HIGUCHI2dce08a2017-04-20 21:57:48 -07001883 List<Instruction> lastGroupIns = new ArrayList<>();
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001884 if (lastGroup != null && !lastGroup.buckets().buckets().isEmpty()) {
Saurav Das25190812016-05-27 13:54:07 -07001885 lastGroupIns = lastGroup.buckets().buckets().get(0)
1886 .treatment().allInstructions();
1887 }
1888 for (Instruction i: lastGroupIns) {
Saurav Das24431192016-03-07 19:13:00 -08001889 if (i instanceof OutputInstruction) {
Saurav Das8be4e3a2016-03-11 17:19:07 -08001890 gchain.append(" port:").append(((OutputInstruction) i).port());
Saurav Das24431192016-03-07 19:13:00 -08001891 }
1892 }
Saurav Das8be4e3a2016-03-11 17:19:07 -08001893 mappings.add(gchain.toString());
Saurav Das24431192016-03-07 19:13:00 -08001894 }
1895 return mappings;
1896 }
Charles Chan5b9df8d2016-03-28 22:21:40 -07001897
Saurav Dasa4020382018-02-14 14:14:54 -08001898 /**
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001899 * Utility class that retries sending flows a fixed number of times, even if
1900 * some of the attempts are successful. Used only for forwarding objectives.
1901 */
Alex Yashchuk4caa8e82017-12-08 17:40:05 +02001902 public final class RetryFlows implements Runnable {
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001903 int attempts = MAX_RETRY_ATTEMPTS;
1904 private Collection<FlowRule> retryFlows;
1905 private ForwardingObjective fwd;
1906
Charles Chan50d900c2018-03-02 13:26:22 -08001907 RetryFlows(ForwardingObjective fwd, Collection<FlowRule> retryFlows) {
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001908 this.fwd = fwd;
1909 this.retryFlows = retryFlows;
1910 }
1911
1912 @Override
1913 public void run() {
1914 log.info("RETRY FLOWS ATTEMPT# {} for fwd:{} rules:{}",
1915 MAX_RETRY_ATTEMPTS - attempts, fwd.id(), retryFlows.size());
pier8b3aef42019-03-11 15:14:02 -07001916 sendForwards(Collections.singletonList(Pair.of(fwd, retryFlows)));
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001917 if (--attempts > 0) {
pier8b3aef42019-03-11 15:14:02 -07001918 retryExecutorService.schedule(this, RETRY_MS, TimeUnit.MILLISECONDS);
1919 }
1920 }
1921 }
1922
1923 // Flow rules accumulator for reducing the number of transactions required to the devices.
1924 private final class ForwardingObjectiveAccumulator
1925 extends AbstractAccumulator<Pair<ForwardingObjective, Collection<FlowRule>>> {
1926
1927 ForwardingObjectiveAccumulator(int maxFwd, int maxBatchMS, int maxIdleMS) {
1928 super(TIMER, maxFwd, maxBatchMS, maxIdleMS);
1929 }
1930
1931 @Override
1932 public void processItems(List<Pair<ForwardingObjective, Collection<FlowRule>>> pairs) {
1933 // Triggers creation of a batch using the list of flowrules generated from fwdobjs.
1934 accumulatorExecutorService.execute(new FlowRulesBuilderTask(pairs));
1935 }
1936 }
1937
1938 // Task for building batch of flow rules in a separate thread.
1939 private final class FlowRulesBuilderTask implements Runnable {
1940 private final List<Pair<ForwardingObjective, Collection<FlowRule>>> pairs;
1941
1942 FlowRulesBuilderTask(List<Pair<ForwardingObjective, Collection<FlowRule>>> pairs) {
1943 this.pairs = pairs;
1944 }
1945
1946 @Override
1947 public void run() {
1948 try {
1949 sendForwards(pairs);
1950 } catch (Exception e) {
1951 log.warn("Unable to send forwards", e);
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001952 }
1953 }
1954 }
1955
Saurav Das822c4e22015-10-23 10:51:11 -07001956}