blob: e8d172cefa64698d0297d9bc585612686104c53b [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;
pier5b9b9e32019-03-11 15:14:02 -070019import com.google.common.collect.Lists;
Yi Tseng47f82dc2017-03-05 22:48:39 -080020import com.google.common.collect.Sets;
pier5b9b9e32019-03-11 15:14:02 -070021import org.apache.commons.lang3.tuple.Pair;
Saurav Das822c4e22015-10-23 10:51:11 -070022import org.onlab.osgi.ServiceDirectory;
Saurav Das822c4e22015-10-23 10:51:11 -070023import org.onlab.packet.Ethernet;
Alex Yashchuka3679532017-12-08 17:40:05 +020024import org.onlab.packet.EthType.EtherType;
Julia Ferguson65428c32017-08-10 18:15:24 +000025import org.onlab.packet.IpAddress;
Charles Chan14967c22015-12-07 11:11:50 -080026import org.onlab.packet.IpPrefix;
Charles Chand57552d2018-03-02 15:41:41 -080027import org.onlab.packet.MacAddress;
Saurav Das822c4e22015-10-23 10:51:11 -070028import org.onlab.packet.VlanId;
pier5b9b9e32019-03-11 15:14:02 -070029import org.onlab.util.AbstractAccumulator;
30import org.onlab.util.Accumulator;
Saurav Das822c4e22015-10-23 10:51:11 -070031import org.onlab.util.KryoNamespace;
32import org.onosproject.core.ApplicationId;
33import org.onosproject.core.CoreService;
Charles Chancad338a2016-09-16 18:03:11 -070034import org.onosproject.driver.extensions.Ofdpa3MplsType;
35import org.onosproject.driver.extensions.Ofdpa3SetMplsType;
Charles Chan14967c22015-12-07 11:11:50 -080036import org.onosproject.driver.extensions.OfdpaMatchVlanVid;
37import org.onosproject.driver.extensions.OfdpaSetVlanVid;
Saurav Das822c4e22015-10-23 10:51:11 -070038import org.onosproject.net.DeviceId;
39import org.onosproject.net.Port;
40import org.onosproject.net.PortNumber;
41import org.onosproject.net.behaviour.NextGroup;
42import org.onosproject.net.behaviour.Pipeliner;
43import org.onosproject.net.behaviour.PipelinerContext;
44import org.onosproject.net.device.DeviceService;
45import org.onosproject.net.driver.AbstractHandlerBehaviour;
pier5b9b9e32019-03-11 15:14:02 -070046import org.onosproject.net.driver.Driver;
Saurav Das822c4e22015-10-23 10:51:11 -070047import org.onosproject.net.flow.DefaultFlowRule;
48import org.onosproject.net.flow.DefaultTrafficSelector;
49import org.onosproject.net.flow.DefaultTrafficTreatment;
50import org.onosproject.net.flow.FlowRule;
51import org.onosproject.net.flow.FlowRuleOperations;
52import org.onosproject.net.flow.FlowRuleOperationsContext;
53import org.onosproject.net.flow.FlowRuleService;
54import org.onosproject.net.flow.TrafficSelector;
55import org.onosproject.net.flow.TrafficTreatment;
56import org.onosproject.net.flow.criteria.Criteria;
57import org.onosproject.net.flow.criteria.Criterion;
58import org.onosproject.net.flow.criteria.EthCriterion;
59import org.onosproject.net.flow.criteria.EthTypeCriterion;
jayakumarthazhathad3636a2018-10-01 00:51:54 +053060import org.onosproject.net.flow.criteria.ExtensionCriterion;
61import org.onosproject.net.flow.criteria.ExtensionSelector;
Saurav Das822c4e22015-10-23 10:51:11 -070062import org.onosproject.net.flow.criteria.IPCriterion;
Pier Ventree0ae7a32016-11-23 09:57:42 -080063import org.onosproject.net.flow.criteria.Icmpv6CodeCriterion;
64import org.onosproject.net.flow.criteria.Icmpv6TypeCriterion;
Saurav Das8a0732e2015-11-20 15:27:53 -080065import org.onosproject.net.flow.criteria.MplsBosCriterion;
66import org.onosproject.net.flow.criteria.MplsCriterion;
Saurav Das822c4e22015-10-23 10:51:11 -070067import org.onosproject.net.flow.criteria.PortCriterion;
Charles Chand9e47c62017-10-05 15:17:15 -070068import org.onosproject.net.flow.criteria.TcpPortCriterion;
69import org.onosproject.net.flow.criteria.UdpPortCriterion;
Saurav Das822c4e22015-10-23 10:51:11 -070070import org.onosproject.net.flow.criteria.VlanIdCriterion;
71import org.onosproject.net.flow.instructions.Instruction;
72import org.onosproject.net.flow.instructions.Instructions.OutputInstruction;
Charles Chan3fe71712018-06-15 18:54:18 -070073import org.onosproject.net.flow.instructions.Instructions.NoActionInstruction;
Saurav Das822c4e22015-10-23 10:51:11 -070074import org.onosproject.net.flow.instructions.L2ModificationInstruction;
Saurav Das8a0732e2015-11-20 15:27:53 -080075import org.onosproject.net.flow.instructions.L2ModificationInstruction.L2SubType;
Saurav Das822c4e22015-10-23 10:51:11 -070076import org.onosproject.net.flow.instructions.L2ModificationInstruction.ModVlanIdInstruction;
Charles Chand57552d2018-03-02 15:41:41 -080077import org.onosproject.net.flow.instructions.L2ModificationInstruction.ModEtherInstruction;
Alex Yashchuka3679532017-12-08 17:40:05 +020078import org.onosproject.net.flow.instructions.L2ModificationInstruction.ModMplsHeaderInstruction;
Charles Chan14967c22015-12-07 11:11:50 -080079import org.onosproject.net.flow.instructions.L3ModificationInstruction;
80import org.onosproject.net.flow.instructions.L3ModificationInstruction.L3SubType;
Saurav Das822c4e22015-10-23 10:51:11 -070081import org.onosproject.net.flowobjective.FilteringObjective;
82import org.onosproject.net.flowobjective.FlowObjectiveStore;
83import org.onosproject.net.flowobjective.ForwardingObjective;
84import org.onosproject.net.flowobjective.NextObjective;
85import org.onosproject.net.flowobjective.Objective;
86import org.onosproject.net.flowobjective.ObjectiveError;
Saurav Das822c4e22015-10-23 10:51:11 -070087import org.onosproject.net.group.DefaultGroupKey;
88import org.onosproject.net.group.Group;
Saurav Das822c4e22015-10-23 10:51:11 -070089import org.onosproject.net.group.GroupKey;
Saurav Das822c4e22015-10-23 10:51:11 -070090import org.onosproject.net.group.GroupService;
Saurav Das822c4e22015-10-23 10:51:11 -070091import org.onosproject.store.serializers.KryoNamespaces;
92import org.slf4j.Logger;
93
Pier Ventree0ae7a32016-11-23 09:57:42 -080094import java.util.ArrayDeque;
95import java.util.ArrayList;
96import java.util.Collection;
97import java.util.Collections;
98import java.util.Deque;
99import java.util.List;
100import java.util.Objects;
pier5b9b9e32019-03-11 15:14:02 -0700101import java.util.Timer;
Pier Ventree0ae7a32016-11-23 09:57:42 -0800102import java.util.concurrent.ScheduledExecutorService;
103import java.util.concurrent.TimeUnit;
104
105import static java.util.concurrent.Executors.newScheduledThreadPool;
pier5b9b9e32019-03-11 15:14:02 -0700106import static java.util.concurrent.Executors.newSingleThreadScheduledExecutor;
Yi Tseng3a77b4f2017-02-06 15:02:17 -0800107import static org.onlab.packet.MacAddress.BROADCAST;
Charles Chanb4879a52017-10-20 19:09:16 -0700108import static org.onlab.packet.MacAddress.IPV4_MULTICAST;
109import static org.onlab.packet.MacAddress.IPV6_MULTICAST;
Yi Tseng3a77b4f2017-02-06 15:02:17 -0800110import static org.onlab.packet.MacAddress.NONE;
Pier Ventree0ae7a32016-11-23 09:57:42 -0800111import static org.onlab.util.Tools.groupedThreads;
Yi Tsengef19de12017-04-24 11:33:05 -0700112import static org.onosproject.driver.pipeline.ofdpa.OfdpaGroupHandlerUtility.*;
Charles Chand9e47c62017-10-05 15:17:15 -0700113import static org.onosproject.net.flow.criteria.Criterion.Type.ETH_TYPE;
Pier Luigi5af88fe2018-02-01 10:23:12 +0100114import static org.onosproject.net.group.GroupDescription.Type.SELECT;
Pier Ventree0ae7a32016-11-23 09:57:42 -0800115import static org.slf4j.LoggerFactory.getLogger;
Pier Ventre140a8942016-11-02 07:26:38 -0700116import static org.onosproject.net.flow.criteria.Criterion.Type.MPLS_BOS;
Pier Ventree0ae7a32016-11-23 09:57:42 -0800117
Saurav Das822c4e22015-10-23 10:51:11 -0700118/**
119 * Driver for Broadcom's OF-DPA v2.0 TTP.
Saurav Das822c4e22015-10-23 10:51:11 -0700120 */
Charles Chan361154b2016-03-24 10:23:39 -0700121public class Ofdpa2Pipeline extends AbstractHandlerBehaviour implements Pipeliner {
pier5b9b9e32019-03-11 15:14:02 -0700122 // Timer for the accumulator
123 private static final Timer TIMER = new Timer("fwdobj-batching");
124 private Accumulator<Pair<ForwardingObjective, Collection<FlowRule>>> accumulator;
125
Saurav Das822c4e22015-10-23 10:51:11 -0700126 protected static final int PORT_TABLE = 0;
127 protected static final int VLAN_TABLE = 10;
Pier Ventre42287df2016-11-09 14:17:26 -0800128 protected static final int VLAN_1_TABLE = 11;
129 protected static final int MPLS_L2_PORT_FLOW_TABLE = 13;
130 protected static final int MPLS_L2_PORT_PCP_TRUST_FLOW_TABLE = 16;
Saurav Das822c4e22015-10-23 10:51:11 -0700131 protected static final int TMAC_TABLE = 20;
132 protected static final int UNICAST_ROUTING_TABLE = 30;
133 protected static final int MULTICAST_ROUTING_TABLE = 40;
134 protected static final int MPLS_TABLE_0 = 23;
135 protected static final int MPLS_TABLE_1 = 24;
Pier Ventre140a8942016-11-02 07:26:38 -0700136 protected static final int MPLS_L3_TYPE_TABLE = 27;
137 protected static final int MPLS_TYPE_TABLE = 29;
Saurav Das822c4e22015-10-23 10:51:11 -0700138 protected static final int BRIDGING_TABLE = 50;
139 protected static final int ACL_TABLE = 60;
140 protected static final int MAC_LEARNING_TABLE = 254;
141 protected static final long OFPP_MAX = 0xffffff00L;
142
Saurav Das52025962016-01-28 22:30:01 -0800143 protected static final int HIGHEST_PRIORITY = 0xffff;
Saurav Das2857f382015-11-03 14:39:27 -0800144 protected static final int DEFAULT_PRIORITY = 0x8000;
Saurav Das822c4e22015-10-23 10:51:11 -0700145 protected static final int LOWEST_PRIORITY = 0x0;
146
Pier Ventre42287df2016-11-09 14:17:26 -0800147 protected static final int MPLS_L2_PORT_PRIORITY = 2;
Pier Ventre42287df2016-11-09 14:17:26 -0800148 protected static final int MPLS_TUNNEL_ID_BASE = 0x10000;
149 protected static final int MPLS_TUNNEL_ID_MAX = 0x1FFFF;
150
Pier Ventre70d53ba2016-11-17 22:26:29 -0800151 protected static final int MPLS_UNI_PORT_MAX = 0x0000FFFF;
Pier Ventre70d53ba2016-11-17 22:26:29 -0800152 protected static final int MPLS_NNI_PORT_BASE = 0x00020000;
153 protected static final int MPLS_NNI_PORT_MAX = 0x0002FFFF;
154
Saurav Das822c4e22015-10-23 10:51:11 -0700155 private final Logger log = getLogger(getClass());
Charles Chan425854b2016-04-11 15:32:12 -0700156 protected ServiceDirectory serviceDirectory;
Saurav Das822c4e22015-10-23 10:51:11 -0700157 protected FlowRuleService flowRuleService;
Charles Chan425854b2016-04-11 15:32:12 -0700158 protected CoreService coreService;
Saurav Das8a0732e2015-11-20 15:27:53 -0800159 protected GroupService groupService;
160 protected FlowObjectiveStore flowObjectiveStore;
Saurav Das822c4e22015-10-23 10:51:11 -0700161 protected DeviceId deviceId;
162 protected ApplicationId driverId;
Saurav Das822c4e22015-10-23 10:51:11 -0700163 protected DeviceService deviceService;
Charles Chan188ebf52015-12-23 00:15:11 -0800164 protected static KryoNamespace appKryo = new KryoNamespace.Builder()
Yi Tsengef19de12017-04-24 11:33:05 -0700165 .register(KryoNamespaces.API)
166 .register(GroupKey.class)
167 .register(DefaultGroupKey.class)
168 .register(OfdpaNextGroup.class)
169 .register(ArrayDeque.class)
170 .build("Ofdpa2Pipeline");
Saurav Das822c4e22015-10-23 10:51:11 -0700171
Charles Chan425854b2016-04-11 15:32:12 -0700172 protected Ofdpa2GroupHandler groupHandler;
Saurav Das822c4e22015-10-23 10:51:11 -0700173
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700174 // flows installations to be retried
pier5b9b9e32019-03-11 15:14:02 -0700175 private ScheduledExecutorService retryExecutorService
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700176 = newScheduledThreadPool(5, groupedThreads("OfdpaPipeliner", "retry-%d", log));
Charles Chan93090352018-03-02 13:26:22 -0800177 private static final int MAX_RETRY_ATTEMPTS = 10;
178 private static final int RETRY_MS = 1000;
Saurav Das4f980082015-11-05 13:39:15 -0800179
pier5b9b9e32019-03-11 15:14:02 -0700180 // accumulator executor service
181 private ScheduledExecutorService accumulatorExecutorService
182 = newSingleThreadScheduledExecutor(groupedThreads("OfdpaPipeliner", "acc-%d", log));
183
Saurav Das822c4e22015-10-23 10:51:11 -0700184 @Override
185 public void init(DeviceId deviceId, PipelinerContext context) {
Saurav Das822c4e22015-10-23 10:51:11 -0700186 this.deviceId = deviceId;
187
Charles Chan425854b2016-04-11 15:32:12 -0700188 serviceDirectory = context.directory();
Saurav Das822c4e22015-10-23 10:51:11 -0700189 coreService = serviceDirectory.get(CoreService.class);
190 flowRuleService = serviceDirectory.get(FlowRuleService.class);
191 groupService = serviceDirectory.get(GroupService.class);
192 flowObjectiveStore = context.store();
Saurav Das822c4e22015-10-23 10:51:11 -0700193 deviceService = serviceDirectory.get(DeviceService.class);
pier5b9b9e32019-03-11 15:14:02 -0700194 // Init the accumulator, if enabled
195 if (isAccumulatorEnabled()) {
196 accumulator = new ForwardingObjectiveAccumulator(context.accumulatorMaxObjectives(),
197 context.accumulatorMaxBatchMillis(),
198 context.accumulatorMaxIdleMillis());
199 }
Saurav Das822c4e22015-10-23 10:51:11 -0700200
Charles Chan40132b32017-01-22 00:19:37 -0800201 initDriverId();
202 initGroupHander(context);
Saurav Das822c4e22015-10-23 10:51:11 -0700203
Saurav Das822c4e22015-10-23 10:51:11 -0700204 initializePipeline();
Saurav Das822c4e22015-10-23 10:51:11 -0700205 }
206
Charles Chan40132b32017-01-22 00:19:37 -0800207 protected void initDriverId() {
208 driverId = coreService.registerApplication(
209 "org.onosproject.driver.Ofdpa2Pipeline");
210 }
211
212 protected void initGroupHander(PipelinerContext context) {
213 groupHandler = new Ofdpa2GroupHandler();
214 groupHandler.init(deviceId, context);
215 }
216
Saurav Das822c4e22015-10-23 10:51:11 -0700217 protected void initializePipeline() {
Charles Chan188ebf52015-12-23 00:15:11 -0800218 // OF-DPA does not require initializing the pipeline as it puts default
219 // rules automatically in the hardware. However emulation of OFDPA in
220 // software switches does require table-miss-entries.
Saurav Das822c4e22015-10-23 10:51:11 -0700221 }
222
pier5b9b9e32019-03-11 15:14:02 -0700223 public boolean isAccumulatorEnabled() {
224 Driver driver = super.data().driver();
225 // we cannot determine the property
226 if (driver == null) {
227 return false;
228 }
229 return Boolean.parseBoolean(driver.getProperty(ACCUMULATOR_ENABLED));
230 }
231
Charles Chand1172632017-03-15 17:33:09 -0700232 /**
Alex Yashchuka3679532017-12-08 17:40:05 +0200233 * Determines whether this pipeline requires MPLS POP instruction.
234 *
235 * @return true to use MPLS POP instruction
236 */
237 public boolean requireMplsPop() {
238 return true;
239 }
240
241 /**
242 * Determines whether this pipeline requires MPLS BOS match.
243 *
244 * @return true to use MPLS BOS match
245 */
246 public boolean requireMplsBosMatch() {
247 return true;
248 }
249
250 /**
251 * Determines whether this pipeline requires MPLS TTL decrement and copy.
252 *
253 * @return true to use MPLS TTL decrement and copy
254 */
255 public boolean requireMplsTtlModification() {
256 return true;
257 }
258
259 /**
Charles Chand1172632017-03-15 17:33:09 -0700260 * Determines whether this pipeline requires OFDPA match and set VLAN extensions.
261 *
262 * @return true to use the extensions
263 */
264 protected boolean requireVlanExtensions() {
265 return true;
266 }
267
Saurav Das86d13e82017-04-28 17:03:48 -0700268 /**
269 * Determines whether in-port should be matched on in TMAC table rules.
270 *
271 * @return true if match on in-port should be programmed
272 */
273 protected boolean matchInPortTmacTable() {
274 return true;
275 }
276
Charles Chand9e47c62017-10-05 15:17:15 -0700277 /**
278 * Determines whether matching L4 destination port on IPv6 packets is supported in ACL table.
279 *
280 * @return true if matching L4 destination port on IPv6 packets is supported in ACL table.
281 */
282 protected boolean supportIpv6L4Dst() {
283 return true;
284 }
285
Saurav Dasf3f75942018-01-25 09:49:01 -0800286 /**
287 * Determines whether this driver should continue to retry flows that point
288 * to empty groups. See CORD-554.
289 *
290 * @return true if the driver should retry flows
291 */
292 protected boolean shouldRetry() {
293 return true;
294 }
295
Charles Chand57552d2018-03-02 15:41:41 -0800296 /**
297 * Determines whether this driver requires unicast flow to be installed before multicast flow
298 * in TMAC table.
299 *
300 * @return true if required
301 */
302 protected boolean requireUnicastBeforeMulticast() {
303 return false;
304 }
305
Andrea Campanella84ac4df2018-04-30 11:48:55 +0200306 /**
307 * Determines whether this driver supports installing a clearDeferred action on table 30.
308 *
309 * @return true if required
310 */
311 protected boolean supportsUnicastBlackHole() {
312 return true;
313 }
314
Saurav Das822c4e22015-10-23 10:51:11 -0700315 //////////////////////////////////////
316 // Flow Objectives
317 //////////////////////////////////////
318
319 @Override
320 public void filter(FilteringObjective filteringObjective) {
321 if (filteringObjective.type() == FilteringObjective.Type.PERMIT) {
322 processFilter(filteringObjective,
323 filteringObjective.op() == Objective.Operation.ADD,
324 filteringObjective.appId());
325 } else {
326 // Note that packets that don't match the PERMIT filter are
327 // automatically denied. The DENY filter is used to deny packets
328 // that are otherwise permitted by the PERMIT filter.
329 // Use ACL table flow rules here for DENY filtering objectives
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530330 log.warn("filter objective other than PERMIT currently not supported");
Saurav Das822c4e22015-10-23 10:51:11 -0700331 fail(filteringObjective, ObjectiveError.UNSUPPORTED);
332 }
333 }
334
335 @Override
336 public void forward(ForwardingObjective fwd) {
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700337 Collection<FlowRule> rules = processForward(fwd);
Saurav Das25190812016-05-27 13:54:07 -0700338 if (rules == null || rules.isEmpty()) {
339 // Assumes fail message has already been generated to the objective
340 // context. Returning here prevents spurious pass message to be
341 // generated by FlowRule service for empty flowOps.
342 return;
343 }
pier5b9b9e32019-03-11 15:14:02 -0700344 // Let's accumulate flow rules - otherwise send directly
345 if (accumulator != null) {
346 accumulator.add(Pair.of(fwd, rules));
347 } else {
348 sendForwards(Collections.singletonList(Pair.of(fwd, rules)));
349 }
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700350 }
351
pier5b9b9e32019-03-11 15:14:02 -0700352 // Builds the batch using the accumulated flow rules
353 private void sendForwards(List<Pair<ForwardingObjective, Collection<FlowRule>>> pairs) {
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700354 FlowRuleOperations.Builder flowOpsBuilder = FlowRuleOperations.builder();
pier5b9b9e32019-03-11 15:14:02 -0700355 log.debug("Sending {} fwd-objs", pairs.size());
356 List<Objective> fwdObjs = Lists.newArrayList();
357 // Iterates over all accumulated flow rules and then build an unique batch
358 pairs.forEach(pair -> {
359 ForwardingObjective fwd = pair.getLeft();
360 Collection<FlowRule> rules = pair.getRight();
361 switch (fwd.op()) {
362 case ADD:
363 rules.stream()
364 .filter(Objects::nonNull)
365 .forEach(flowOpsBuilder::add);
366 log.debug("Applying a add fwd-obj {} to sw:{}", fwd.id(), deviceId);
367 fwdObjs.add(fwd);
368 break;
369 case REMOVE:
370 rules.stream()
371 .filter(Objects::nonNull)
372 .forEach(flowOpsBuilder::remove);
373 log.debug("Deleting a flow rule to sw:{}", deviceId);
374 fwdObjs.add(fwd);
375 break;
376 default:
377 fail(fwd, ObjectiveError.UNKNOWN);
378 log.warn("Unknown forwarding type {}", fwd.op());
379 }
380 });
381 // Finally applies the operations
Saurav Das822c4e22015-10-23 10:51:11 -0700382 flowRuleService.apply(flowOpsBuilder.build(new FlowRuleOperationsContext() {
pier5b9b9e32019-03-11 15:14:02 -0700383
Saurav Das822c4e22015-10-23 10:51:11 -0700384 @Override
385 public void onSuccess(FlowRuleOperations ops) {
pier5b9b9e32019-03-11 15:14:02 -0700386 log.trace("Flow rule operations onSuccess {}", ops);
387 fwdObjs.forEach(Ofdpa2Pipeline::pass);
Saurav Das822c4e22015-10-23 10:51:11 -0700388 }
389
390 @Override
391 public void onError(FlowRuleOperations ops) {
pier5b9b9e32019-03-11 15:14:02 -0700392 ObjectiveError error = ObjectiveError.FLOWINSTALLATIONFAILED;
393 log.warn("Flow rule operations onError {}. Reason = {}", ops, error);
394 fwdObjs.forEach(fwdObj -> fail(fwdObj, error));
Saurav Das822c4e22015-10-23 10:51:11 -0700395 }
396 }));
Saurav Das822c4e22015-10-23 10:51:11 -0700397 }
398
399 @Override
400 public void next(NextObjective nextObjective) {
Saurav Das8a0732e2015-11-20 15:27:53 -0800401 NextGroup nextGroup = flowObjectiveStore.getNextGroup(nextObjective.id());
402 switch (nextObjective.op()) {
403 case ADD:
Saurav Das4f980082015-11-05 13:39:15 -0800404 if (nextGroup != null) {
Saurav Das8a0732e2015-11-20 15:27:53 -0800405 log.warn("Cannot add next {} that already exists in device {}",
406 nextObjective.id(), deviceId);
407 return;
408 }
Saurav Das1547b3f2017-05-05 17:01:08 -0700409 log.debug("Processing NextObjective id {} in dev {} - add group",
Saurav Das8a0732e2015-11-20 15:27:53 -0800410 nextObjective.id(), deviceId);
Charles Chan425854b2016-04-11 15:32:12 -0700411 groupHandler.addGroup(nextObjective);
Saurav Das8a0732e2015-11-20 15:27:53 -0800412 break;
413 case ADD_TO_EXISTING:
414 if (nextGroup != null) {
Saurav Das1547b3f2017-05-05 17:01:08 -0700415 log.debug("Processing NextObjective id {} in dev {} - add bucket",
Saurav Das8a0732e2015-11-20 15:27:53 -0800416 nextObjective.id(), deviceId);
Charles Chan425854b2016-04-11 15:32:12 -0700417 groupHandler.addBucketToGroup(nextObjective, nextGroup);
Saurav Das4f980082015-11-05 13:39:15 -0800418 } else {
Saurav Das8a0732e2015-11-20 15:27:53 -0800419 // it is possible that group-chain has not been fully created yet
Saurav Das423fe2b2015-12-04 10:52:59 -0800420 log.debug("Waiting to add bucket to group for next-id:{} in dev:{}",
421 nextObjective.id(), deviceId);
Yi Tseng47f82dc2017-03-05 22:48:39 -0800422
423 // by design multiple pending bucket is allowed for the group
424 groupHandler.pendingBuckets.compute(nextObjective.id(), (nextId, pendBkts) -> {
425 if (pendBkts == null) {
426 pendBkts = Sets.newHashSet();
427 }
428 pendBkts.add(nextObjective);
429 return pendBkts;
430 });
Saurav Das4f980082015-11-05 13:39:15 -0800431 }
Saurav Das8a0732e2015-11-20 15:27:53 -0800432 break;
433 case REMOVE:
434 if (nextGroup == null) {
435 log.warn("Cannot remove next {} that does not exist in device {}",
436 nextObjective.id(), deviceId);
437 return;
438 }
Saurav Das1547b3f2017-05-05 17:01:08 -0700439 log.debug("Processing NextObjective id {} in dev {} - remove group",
Saurav Das8a0732e2015-11-20 15:27:53 -0800440 nextObjective.id(), deviceId);
Charles Chan425854b2016-04-11 15:32:12 -0700441 groupHandler.removeGroup(nextObjective, nextGroup);
Saurav Das8a0732e2015-11-20 15:27:53 -0800442 break;
443 case REMOVE_FROM_EXISTING:
444 if (nextGroup == null) {
445 log.warn("Cannot remove from next {} that does not exist in device {}",
446 nextObjective.id(), deviceId);
447 return;
448 }
Saurav Das1547b3f2017-05-05 17:01:08 -0700449 log.debug("Processing NextObjective id {} in dev {} - remove bucket",
Saurav Das8a0732e2015-11-20 15:27:53 -0800450 nextObjective.id(), deviceId);
Charles Chan425854b2016-04-11 15:32:12 -0700451 groupHandler.removeBucketFromGroup(nextObjective, nextGroup);
Saurav Das8a0732e2015-11-20 15:27:53 -0800452 break;
Jonghwan Hyun25199f22018-02-12 16:43:45 +0900453 case MODIFY:
454 if (nextGroup == null) {
455 log.warn("Cannot modify next {} that does not exist in device {}",
456 nextObjective.id(), deviceId);
457 return;
458 }
Ruchi Sahotaf77dc0f2019-01-28 01:08:18 +0000459 log.debug("Processing NextObjective id {} in dev {} group {} - modify bucket",
460 nextObjective.id(), deviceId, nextGroup);
Jonghwan Hyun25199f22018-02-12 16:43:45 +0900461 groupHandler.modifyBucketFromGroup(nextObjective, nextGroup);
462 break;
Saurav Dasceccf242017-08-03 18:30:35 -0700463 case VERIFY:
464 if (nextGroup == null) {
465 log.warn("Cannot verify next {} that does not exist in device {}",
466 nextObjective.id(), deviceId);
467 return;
468 }
469 log.debug("Processing NextObjective id {} in dev {} - verify",
470 nextObjective.id(), deviceId);
471 groupHandler.verifyGroup(nextObjective, nextGroup);
472 break;
Saurav Das8a0732e2015-11-20 15:27:53 -0800473 default:
Saurav Das4f980082015-11-05 13:39:15 -0800474 log.warn("Unsupported operation {}", nextObjective.op());
Saurav Das822c4e22015-10-23 10:51:11 -0700475 }
476 }
477
478 //////////////////////////////////////
479 // Flow handling
480 //////////////////////////////////////
481
482 /**
Yi Tseng0fdc07e2017-04-24 19:17:08 -0700483 * As per OFDPA 2.0 TTP, filtering of VLAN ids and MAC addresses (for routing)
484 * configured on switch ports happen in different tables.
Saurav Das822c4e22015-10-23 10:51:11 -0700485 *
486 * @param filt the filtering objective
487 * @param install indicates whether to add or remove the objective
488 * @param applicationId the application that sent this objective
489 */
Saurav Das52025962016-01-28 22:30:01 -0800490 protected void processFilter(FilteringObjective filt,
491 boolean install, ApplicationId applicationId) {
Saurav Das822c4e22015-10-23 10:51:11 -0700492 // This driver only processes filtering criteria defined with switch
493 // ports as the key
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530494 PortCriterion portCriterion = null;
Saurav Das822c4e22015-10-23 10:51:11 -0700495 EthCriterion ethCriterion = null;
496 VlanIdCriterion vidCriterion = null;
Saurav Das822c4e22015-10-23 10:51:11 -0700497 if (!filt.key().equals(Criteria.dummy()) &&
498 filt.key().type() == Criterion.Type.IN_PORT) {
499 portCriterion = (PortCriterion) filt.key();
Saurav Das822c4e22015-10-23 10:51:11 -0700500 }
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530501 if (portCriterion == null) {
502 log.debug("No IN_PORT defined in filtering objective from app: {}",
503 applicationId);
504 } else {
505 log.debug("Received filtering objective for dev/port: {}/{}", deviceId,
506 portCriterion.port());
507 }
Saurav Das822c4e22015-10-23 10:51:11 -0700508 // convert filtering conditions for switch-intfs into flowrules
509 FlowRuleOperations.Builder ops = FlowRuleOperations.builder();
510 for (Criterion criterion : filt.conditions()) {
Yi Tseng0fdc07e2017-04-24 19:17:08 -0700511 switch (criterion.type()) {
512 case ETH_DST:
513 case ETH_DST_MASKED:
514 ethCriterion = (EthCriterion) criterion;
515 break;
516 case VLAN_VID:
517 vidCriterion = (VlanIdCriterion) criterion;
518 break;
519 default:
520 log.warn("Unsupported filter {}", criterion);
521 fail(filt, ObjectiveError.UNSUPPORTED);
522 return;
Saurav Das822c4e22015-10-23 10:51:11 -0700523 }
524 }
525
Saurav Das0e99e2b2015-10-28 12:39:42 -0700526 VlanId assignedVlan = null;
Charles Chane849c192016-01-11 18:28:54 -0800527 if (vidCriterion != null) {
Charles Chand55e84d2016-03-30 17:54:24 -0700528 // Use the VLAN in criterion if metadata is not present and the traffic is tagged
Charles Chan9174e6b2017-12-19 19:55:57 -0800529 if (!vidCriterion.vlanId().equals(VlanId.NONE)) {
Charles Chane849c192016-01-11 18:28:54 -0800530 assignedVlan = vidCriterion.vlanId();
Pier3cc7e662018-03-07 11:42:50 +0100531 }
532 // If the meta VLAN is present let's update the assigned vlan
533 if (filt.meta() != null) {
534 VlanId metaVlan = readVlanFromTreatment(filt.meta());
535 if (metaVlan != null) {
536 assignedVlan = metaVlan;
537 }
Charles Chand55e84d2016-03-30 17:54:24 -0700538 }
Charles Chane849c192016-01-11 18:28:54 -0800539
Charles Chand55e84d2016-03-30 17:54:24 -0700540 if (assignedVlan == null) {
541 log.error("Driver fails to extract VLAN information. "
Frank Wangd8ab0962017-08-11 11:09:30 +0800542 + "Not processing VLAN filters on device {}.", deviceId);
Charles Chand55e84d2016-03-30 17:54:24 -0700543 log.debug("VLAN ID in criterion={}, metadata={}",
544 readVlanFromTreatment(filt.meta()), vidCriterion.vlanId());
545 fail(filt, ObjectiveError.BADPARAMS);
546 return;
Saurav Das0e99e2b2015-10-28 12:39:42 -0700547 }
548 }
549
Yi Tseng3a77b4f2017-02-06 15:02:17 -0800550 if (ethCriterion == null || ethCriterion.mac().equals(NONE)) {
Charles Chan985b12e2016-05-11 19:47:22 -0700551 // NOTE: it is possible that a filtering objective only has vidCriterion
Daniel Ginsburg16e5ed42018-04-30 19:27:19 -0400552 log.debug("filtering objective missing dstMac, won't program TMAC table");
Saurav Das822c4e22015-10-23 10:51:11 -0700553 } else {
Charles Chand57552d2018-03-02 15:41:41 -0800554 MacAddress unicastMac = readEthDstFromTreatment(filt.meta());
Charles Chan206506b2018-03-02 16:43:28 -0800555 List<List<FlowRule>> allStages = processEthDstFilter(portCriterion, ethCriterion,
Charles Chand57552d2018-03-02 15:41:41 -0800556 vidCriterion, assignedVlan, unicastMac, applicationId);
Charles Chan206506b2018-03-02 16:43:28 -0800557 for (List<FlowRule> flowRules : allStages) {
Charles Chand57552d2018-03-02 15:41:41 -0800558 log.trace("Starting a new flow rule stage for TMAC table flow");
Charles Chan206506b2018-03-02 16:43:28 -0800559 ops.newStage();
Charles Chan9174e6b2017-12-19 19:55:57 -0800560
Charles Chan206506b2018-03-02 16:43:28 -0800561 for (FlowRule flowRule : flowRules) {
Charles Chan893a1d72018-05-10 22:19:25 +0000562 log.trace("{} flow rules in TMAC table: {} for dev: {}",
Charles Chan206506b2018-03-02 16:43:28 -0800563 (install) ? "adding" : "removing", flowRules, deviceId);
Charles Chan206506b2018-03-02 16:43:28 -0800564 if (install) {
565 ops = ops.add(flowRule);
Charles Chan9174e6b2017-12-19 19:55:57 -0800566 } else {
Charles Chan206506b2018-03-02 16:43:28 -0800567 // NOTE: Only remove TMAC flow when there is no more enabled port within the
568 // same VLAN on this device if TMAC doesn't support matching on in_port.
569 if (matchInPortTmacTable() || (filt.meta() != null && filt.meta().clearedDeferred())) {
570 ops = ops.remove(flowRule);
571 } else {
572 log.debug("Abort TMAC flow removal on {}. Some other ports still share this TMAC flow");
573 }
Charles Chan9174e6b2017-12-19 19:55:57 -0800574 }
575 }
Saurav Das822c4e22015-10-23 10:51:11 -0700576 }
577 }
578
Charles Chan985b12e2016-05-11 19:47:22 -0700579 if (vidCriterion == null) {
580 // NOTE: it is possible that a filtering objective only has ethCriterion
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530581 log.info("filtering objective missing VLAN, cannot program VLAN Table");
Saurav Das822c4e22015-10-23 10:51:11 -0700582 } else {
Charles Chan206506b2018-03-02 16:43:28 -0800583 List<List<FlowRule>> allStages = processVlanIdFilter(
Charles Chan14967c22015-12-07 11:11:50 -0800584 portCriterion, vidCriterion, assignedVlan, applicationId);
Charles Chan206506b2018-03-02 16:43:28 -0800585 for (List<FlowRule> flowRules : allStages) {
Charles Chand57552d2018-03-02 15:41:41 -0800586 log.trace("Starting a new flow rule stage for VLAN table flow");
Charles Chan206506b2018-03-02 16:43:28 -0800587 ops.newStage();
Charles Chan14967c22015-12-07 11:11:50 -0800588
Charles Chan206506b2018-03-02 16:43:28 -0800589 for (FlowRule flowRule : flowRules) {
590 log.trace("{} flow rules in VLAN table: {} for dev: {}",
591 (install) ? "adding" : "removing", flowRule, deviceId);
592 ops = install ? ops.add(flowRule) : ops.remove(flowRule);
Charles Chand1172632017-03-15 17:33:09 -0700593 }
Saurav Das822c4e22015-10-23 10:51:11 -0700594 }
595 }
596
Saurav Das822c4e22015-10-23 10:51:11 -0700597 // apply filtering flow rules
598 flowRuleService.apply(ops.build(new FlowRuleOperationsContext() {
599 @Override
600 public void onSuccess(FlowRuleOperations ops) {
Saurav Das018605f2017-02-18 14:05:44 -0800601 log.debug("Applied {} filtering rules in device {}",
Saurav Das822c4e22015-10-23 10:51:11 -0700602 ops.stages().get(0).size(), deviceId);
603 pass(filt);
604 }
605
606 @Override
607 public void onError(FlowRuleOperations ops) {
608 log.info("Failed to apply all filtering rules in dev {}", deviceId);
609 fail(filt, ObjectiveError.FLOWINSTALLATIONFAILED);
610 }
611 }));
612
613 }
614
615 /**
Charles Chand1172632017-03-15 17:33:09 -0700616 * Internal implementation of processVlanIdFilter.
617 * <p>
618 * The is_present bit in set_vlan_vid action is required to be 0 in OFDPA i12.
619 * Since it is non-OF spec, we need an extension treatment for that.
620 * The useVlanExtension must be set to false for OFDPA i12.
621 * </p>
Charles Chan206506b2018-03-02 16:43:28 -0800622 * <p>
623 * NOTE: Separate VLAN filtering rules and assignment rules
624 * into different stages in order to guarantee that filtering rules
625 * always go first, as required by OFDPA.
626 * </p>
Saurav Das0e99e2b2015-10-28 12:39:42 -0700627 *
Charles Chanf9e98652016-09-07 16:54:23 -0700628 * @param portCriterion port on device for which this filter is programmed
629 * @param vidCriterion vlan assigned to port, or NONE for untagged
630 * @param assignedVlan assigned vlan-id for untagged packets
631 * @param applicationId for application programming this filter
Charles Chan206506b2018-03-02 16:43:28 -0800632 * @return stages of flow rules for port-vlan filters
Saurav Das822c4e22015-10-23 10:51:11 -0700633 */
Charles Chan206506b2018-03-02 16:43:28 -0800634 protected List<List<FlowRule>> processVlanIdFilter(PortCriterion portCriterion,
Yi Tsengef19de12017-04-24 11:33:05 -0700635 VlanIdCriterion vidCriterion,
636 VlanId assignedVlan,
637 ApplicationId applicationId) {
Charles Chan206506b2018-03-02 16:43:28 -0800638 List<FlowRule> filteringRules = new ArrayList<>();
639 List<FlowRule> assignmentRules = new ArrayList<>();
Saurav Das822c4e22015-10-23 10:51:11 -0700640 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
641 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
Charles Chan14967c22015-12-07 11:11:50 -0800642 TrafficSelector.Builder preSelector = null;
643 TrafficTreatment.Builder preTreatment = null;
644
Saurav Das4f980082015-11-05 13:39:15 -0800645 treatment.transition(TMAC_TABLE);
646
Saurav Das822c4e22015-10-23 10:51:11 -0700647 if (vidCriterion.vlanId() == VlanId.NONE) {
648 // untagged packets are assigned vlans
Charles Chand1172632017-03-15 17:33:09 -0700649 preSelector = DefaultTrafficSelector.builder();
650 if (requireVlanExtensions()) {
651 OfdpaMatchVlanVid ofdpaMatchVlanVid = new OfdpaMatchVlanVid(VlanId.NONE);
652 selector.extension(ofdpaMatchVlanVid, deviceId);
Charles Chanf9e98652016-09-07 16:54:23 -0700653 OfdpaSetVlanVid ofdpaSetVlanVid = new OfdpaSetVlanVid(assignedVlan);
654 treatment.extension(ofdpaSetVlanVid, deviceId);
Charles Chand1172632017-03-15 17:33:09 -0700655
656 OfdpaMatchVlanVid preOfdpaMatchVlanVid = new OfdpaMatchVlanVid(assignedVlan);
657 preSelector.extension(preOfdpaMatchVlanVid, deviceId);
Charles Chanf9e98652016-09-07 16:54:23 -0700658 } else {
Charles Chand1172632017-03-15 17:33:09 -0700659 selector.matchVlanId(VlanId.NONE);
Charles Chanf9e98652016-09-07 16:54:23 -0700660 treatment.setVlanId(assignedVlan);
Charles Chand1172632017-03-15 17:33:09 -0700661
662 preSelector.matchVlanId(assignedVlan);
663 }
664 preTreatment = DefaultTrafficTreatment.builder().transition(TMAC_TABLE);
665 } else {
666 if (requireVlanExtensions()) {
667 OfdpaMatchVlanVid ofdpaMatchVlanVid = new OfdpaMatchVlanVid(vidCriterion.vlanId());
668 selector.extension(ofdpaMatchVlanVid, deviceId);
669 } else {
670 selector.matchVlanId(vidCriterion.vlanId());
Charles Chanf9e98652016-09-07 16:54:23 -0700671 }
Charles Chan14967c22015-12-07 11:11:50 -0800672
Charles Chand55e84d2016-03-30 17:54:24 -0700673 if (!assignedVlan.equals(vidCriterion.vlanId())) {
Charles Chand1172632017-03-15 17:33:09 -0700674 if (requireVlanExtensions()) {
Charles Chanc03782d2017-01-31 13:57:55 -0800675 OfdpaSetVlanVid ofdpaSetVlanVid = new OfdpaSetVlanVid(assignedVlan);
676 treatment.extension(ofdpaSetVlanVid, deviceId);
677 } else {
678 treatment.setVlanId(assignedVlan);
679 }
Charles Chand55e84d2016-03-30 17:54:24 -0700680 }
Saurav Das822c4e22015-10-23 10:51:11 -0700681 }
Saurav Das822c4e22015-10-23 10:51:11 -0700682
683 // ofdpa cannot match on ALL portnumber, so we need to use separate
684 // rules for each port.
Charles Chan14967c22015-12-07 11:11:50 -0800685 List<PortNumber> portnums = new ArrayList<>();
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530686 if (portCriterion != null) {
687 if (PortNumber.ALL.equals(portCriterion.port())) {
688 for (Port port : deviceService.getPorts(deviceId)) {
689 if (port.number().toLong() > 0 && port.number().toLong() < OFPP_MAX) {
690 portnums.add(port.number());
691 }
Saurav Das822c4e22015-10-23 10:51:11 -0700692 }
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530693 } else {
694 portnums.add(portCriterion.port());
Saurav Das822c4e22015-10-23 10:51:11 -0700695 }
696 } else {
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530697 log.warn("Filtering Objective missing Port Criterion . " +
Charles Chan206506b2018-03-02 16:43:28 -0800698 "VLAN Table cannot be programmed for {}", deviceId);
Saurav Das822c4e22015-10-23 10:51:11 -0700699 }
Saurav Das4f980082015-11-05 13:39:15 -0800700
Saurav Das822c4e22015-10-23 10:51:11 -0700701 for (PortNumber pnum : portnums) {
Saurav Das4f980082015-11-05 13:39:15 -0800702 // create rest of flowrule
Saurav Das822c4e22015-10-23 10:51:11 -0700703 selector.matchInPort(pnum);
704 FlowRule rule = DefaultFlowRule.builder()
705 .forDevice(deviceId)
706 .withSelector(selector.build())
707 .withTreatment(treatment.build())
708 .withPriority(DEFAULT_PRIORITY)
709 .fromApp(applicationId)
710 .makePermanent()
711 .forTable(VLAN_TABLE).build();
Charles Chan206506b2018-03-02 16:43:28 -0800712 assignmentRules.add(rule);
Charles Chan14967c22015-12-07 11:11:50 -0800713
714 if (preSelector != null) {
715 preSelector.matchInPort(pnum);
716 FlowRule preRule = DefaultFlowRule.builder()
717 .forDevice(deviceId)
718 .withSelector(preSelector.build())
719 .withTreatment(preTreatment.build())
720 .withPriority(DEFAULT_PRIORITY)
721 .fromApp(applicationId)
722 .makePermanent()
723 .forTable(VLAN_TABLE).build();
Charles Chan206506b2018-03-02 16:43:28 -0800724 filteringRules.add(preRule);
Charles Chan14967c22015-12-07 11:11:50 -0800725 }
Saurav Das822c4e22015-10-23 10:51:11 -0700726 }
Charles Chan1e58aa62018-03-06 22:10:15 -0800727 return ImmutableList.of(filteringRules, assignmentRules);
Saurav Das822c4e22015-10-23 10:51:11 -0700728 }
729
730 /**
731 * Allows routed packets with correct destination MAC to be directed
Charles Chand57552d2018-03-02 15:41:41 -0800732 * to unicast routing table, multicast routing table or MPLS forwarding table.
Saurav Das822c4e22015-10-23 10:51:11 -0700733 *
734 * @param portCriterion port on device for which this filter is programmed
735 * @param ethCriterion dstMac of device for which is filter is programmed
736 * @param vidCriterion vlan assigned to port, or NONE for untagged
Saurav Das0e99e2b2015-10-28 12:39:42 -0700737 * @param assignedVlan assigned vlan-id for untagged packets
Charles Chand57552d2018-03-02 15:41:41 -0800738 * @param unicastMac some switches require a unicast TMAC flow to be programmed before multicast
739 * TMAC flow. This MAC address will be used for the unicast TMAC flow.
740 * This is unused if the filtering objective is a unicast.
Saurav Das822c4e22015-10-23 10:51:11 -0700741 * @param applicationId for application programming this filter
Charles Chan206506b2018-03-02 16:43:28 -0800742 * @return stages of flow rules for port-vlan filters
Saurav Das822c4e22015-10-23 10:51:11 -0700743
744 */
Charles Chan206506b2018-03-02 16:43:28 -0800745 protected List<List<FlowRule>> processEthDstFilter(PortCriterion portCriterion,
Saurav Das822c4e22015-10-23 10:51:11 -0700746 EthCriterion ethCriterion,
747 VlanIdCriterion vidCriterion,
Saurav Das0e99e2b2015-10-28 12:39:42 -0700748 VlanId assignedVlan,
Charles Chand57552d2018-03-02 15:41:41 -0800749 MacAddress unicastMac,
Saurav Das822c4e22015-10-23 10:51:11 -0700750 ApplicationId applicationId) {
Charles Chan5270ed02016-01-30 23:22:37 -0800751 // Consider PortNumber.ANY as wildcard. Match ETH_DST only
Sivachidambaram Subramanianc598d062017-05-16 22:42:06 +0530752 if (portCriterion != null && PortNumber.ANY.equals(portCriterion.port())) {
Charles Chan5270ed02016-01-30 23:22:37 -0800753 return processEthDstOnlyFilter(ethCriterion, applicationId);
754 }
755
Charles Chan5b9df8d2016-03-28 22:21:40 -0700756 // Multicast MAC
757 if (ethCriterion.mask() != null) {
Charles Chand57552d2018-03-02 15:41:41 -0800758 return processMcastEthDstFilter(ethCriterion, assignedVlan, unicastMac, applicationId);
Charles Chan5b9df8d2016-03-28 22:21:40 -0700759 }
760
Saurav Das822c4e22015-10-23 10:51:11 -0700761 //handling untagged packets via assigned VLAN
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530762 if (vidCriterion != null && vidCriterion.vlanId() == VlanId.NONE) {
Saurav Das0e99e2b2015-10-28 12:39:42 -0700763 vidCriterion = (VlanIdCriterion) Criteria.matchVlanId(assignedVlan);
Saurav Das822c4e22015-10-23 10:51:11 -0700764 }
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530765 List<FlowRule> rules = new ArrayList<>();
766 OfdpaMatchVlanVid ofdpaMatchVlanVid = null;
767 if (vidCriterion != null && requireVlanExtensions()) {
768 ofdpaMatchVlanVid = new OfdpaMatchVlanVid(vidCriterion.vlanId());
769 }
Saurav Das822c4e22015-10-23 10:51:11 -0700770 // ofdpa cannot match on ALL portnumber, so we need to use separate
771 // rules for each port.
Yuta HIGUCHI2dce08a2017-04-20 21:57:48 -0700772 List<PortNumber> portnums = new ArrayList<>();
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530773 if (portCriterion != null) {
774 if (PortNumber.ALL.equals(portCriterion.port())) {
775 for (Port port : deviceService.getPorts(deviceId)) {
776 if (port.number().toLong() > 0 && port.number().toLong() < OFPP_MAX) {
777 portnums.add(port.number());
778 }
Saurav Das822c4e22015-10-23 10:51:11 -0700779 }
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530780 } else {
781 portnums.add(portCriterion.port());
782 }
783 for (PortNumber pnum : portnums) {
784 rules.add(buildTmacRuleForIpv4(ethCriterion,
785 vidCriterion,
786 ofdpaMatchVlanVid,
787 applicationId,
788 pnum));
789 rules.add(buildTmacRuleForMpls(ethCriterion,
790 vidCriterion,
791 ofdpaMatchVlanVid,
792 applicationId,
793 pnum));
794 rules.add(buildTmacRuleForIpv6(ethCriterion,
795 vidCriterion,
796 ofdpaMatchVlanVid,
797 applicationId,
798 pnum));
Saurav Das822c4e22015-10-23 10:51:11 -0700799 }
800 } else {
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530801 rules.add(buildTmacRuleForIpv4(ethCriterion,
802 vidCriterion,
803 ofdpaMatchVlanVid,
804 applicationId,
805 null));
806 rules.add(buildTmacRuleForMpls(ethCriterion,
807 vidCriterion,
808 ofdpaMatchVlanVid,
809 applicationId,
810 null));
811 rules.add(buildTmacRuleForIpv6(ethCriterion,
812 vidCriterion,
813 ofdpaMatchVlanVid,
814 applicationId,
815 null));
Saurav Das822c4e22015-10-23 10:51:11 -0700816 }
Charles Chan206506b2018-03-02 16:43:28 -0800817 return ImmutableList.of(rules);
Saurav Das822c4e22015-10-23 10:51:11 -0700818 }
819
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530820 /**
821 * Builds TMAC rules for IPv4 packets.
822 *
Charles Chan9174e6b2017-12-19 19:55:57 -0800823 * @param ethCriterion dst mac matching
824 * @param vidCriterion vlan id assigned to the port
825 * @param ofdpaMatchVlanVid OFDPA vlan id matching
826 * @param applicationId application id
827 * @param pnum port number
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530828 * @return TMAC rule for IPV4 packets
829 */
830 private FlowRule buildTmacRuleForIpv4(EthCriterion ethCriterion,
831 VlanIdCriterion vidCriterion,
832 OfdpaMatchVlanVid ofdpaMatchVlanVid,
833 ApplicationId applicationId,
834 PortNumber pnum) {
835 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
836 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
837 if (pnum != null) {
838 if (matchInPortTmacTable()) {
839 selector.matchInPort(pnum);
840 } else {
Saurav Das73a6cc12017-12-05 15:00:23 -0800841 log.debug("Pipeline does not support IN_PORT matching in TMAC table, " +
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530842 "ignoring the IN_PORT criteria");
843 }
844 }
845 if (vidCriterion != null) {
846 if (requireVlanExtensions()) {
847 selector.extension(ofdpaMatchVlanVid, deviceId);
848 } else {
849 selector.matchVlanId(vidCriterion.vlanId());
850 }
851 }
852 selector.matchEthType(Ethernet.TYPE_IPV4);
853 selector.matchEthDst(ethCriterion.mac());
854 treatment.transition(UNICAST_ROUTING_TABLE);
855 return DefaultFlowRule.builder()
856 .forDevice(deviceId)
857 .withSelector(selector.build())
858 .withTreatment(treatment.build())
859 .withPriority(DEFAULT_PRIORITY)
860 .fromApp(applicationId)
861 .makePermanent()
862 .forTable(TMAC_TABLE).build();
863 }
864
865 /**
866 * Builds TMAC rule for MPLS packets.
867 *
Charles Chan9174e6b2017-12-19 19:55:57 -0800868 * @param ethCriterion dst mac matching
869 * @param vidCriterion vlan id assigned to the port
870 * @param ofdpaMatchVlanVid OFDPA vlan id matching
871 * @param applicationId application id
872 * @param pnum port number
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530873 * @return TMAC rule for MPLS packets
874 */
875 private FlowRule buildTmacRuleForMpls(EthCriterion ethCriterion,
876 VlanIdCriterion vidCriterion,
877 OfdpaMatchVlanVid ofdpaMatchVlanVid,
878 ApplicationId applicationId,
879 PortNumber pnum) {
880 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
881 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
882 if (pnum != null) {
883 if (matchInPortTmacTable()) {
884 selector.matchInPort(pnum);
885 } else {
Saurav Das73a6cc12017-12-05 15:00:23 -0800886 log.debug("Pipeline does not support IN_PORT matching in TMAC table, " +
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530887 "ignoring the IN_PORT criteria");
888 }
889 }
890 if (vidCriterion != null) {
891 if (requireVlanExtensions()) {
892 selector.extension(ofdpaMatchVlanVid, deviceId);
893 } else {
894 selector.matchVlanId(vidCriterion.vlanId());
895 }
896 }
897 selector.matchEthType(Ethernet.MPLS_UNICAST);
898 selector.matchEthDst(ethCriterion.mac());
899 treatment.transition(MPLS_TABLE_0);
900 return DefaultFlowRule.builder()
901 .forDevice(deviceId)
902 .withSelector(selector.build())
903 .withTreatment(treatment.build())
904 .withPriority(DEFAULT_PRIORITY)
905 .fromApp(applicationId)
906 .makePermanent()
907 .forTable(TMAC_TABLE).build();
908 }
909
910 /**
911 * Builds TMAC rules for IPv6 packets.
912 *
Charles Chan9174e6b2017-12-19 19:55:57 -0800913 * @param ethCriterion dst mac matching
914 * @param vidCriterion vlan id assigned to the port
915 * @param ofdpaMatchVlanVid OFDPA vlan id matching
916 * @param applicationId application id
917 * @param pnum port number
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530918 * @return TMAC rule for IPV6 packets
919 */
920 private FlowRule buildTmacRuleForIpv6(EthCriterion ethCriterion,
921 VlanIdCriterion vidCriterion,
922 OfdpaMatchVlanVid ofdpaMatchVlanVid,
923 ApplicationId applicationId,
924 PortNumber pnum) {
925 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
926 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
927 if (pnum != null) {
928 if (matchInPortTmacTable()) {
929 selector.matchInPort(pnum);
930 } else {
Saurav Das73a6cc12017-12-05 15:00:23 -0800931 log.debug("Pipeline does not support IN_PORT matching in TMAC table, " +
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530932 "ignoring the IN_PORT criteria");
933 }
934 }
935 if (vidCriterion != null) {
936 if (requireVlanExtensions()) {
937 selector.extension(ofdpaMatchVlanVid, deviceId);
938 } else {
939 selector.matchVlanId(vidCriterion.vlanId());
940 }
941 }
942 selector.matchEthType(Ethernet.TYPE_IPV6);
943 selector.matchEthDst(ethCriterion.mac());
944 treatment.transition(UNICAST_ROUTING_TABLE);
945 return DefaultFlowRule.builder()
946 .forDevice(deviceId)
947 .withSelector(selector.build())
948 .withTreatment(treatment.build())
949 .withPriority(DEFAULT_PRIORITY)
950 .fromApp(applicationId)
951 .makePermanent()
952 .forTable(TMAC_TABLE).build();
953 }
954
Charles Chan206506b2018-03-02 16:43:28 -0800955 protected List<List<FlowRule>> processEthDstOnlyFilter(EthCriterion ethCriterion,
Yi Tsengef19de12017-04-24 11:33:05 -0700956 ApplicationId applicationId) {
Pier Luigi0e358632017-01-31 09:35:05 -0800957 ImmutableList.Builder<FlowRule> builder = ImmutableList.builder();
958
Charles Chan5270ed02016-01-30 23:22:37 -0800959 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
960 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
961 selector.matchEthType(Ethernet.TYPE_IPV4);
962 selector.matchEthDst(ethCriterion.mac());
963 treatment.transition(UNICAST_ROUTING_TABLE);
964 FlowRule rule = DefaultFlowRule.builder()
965 .forDevice(deviceId)
966 .withSelector(selector.build())
967 .withTreatment(treatment.build())
968 .withPriority(DEFAULT_PRIORITY)
969 .fromApp(applicationId)
970 .makePermanent()
971 .forTable(TMAC_TABLE).build();
Pier Luigi0e358632017-01-31 09:35:05 -0800972 builder.add(rule);
973
974 selector = DefaultTrafficSelector.builder();
975 treatment = DefaultTrafficTreatment.builder();
976 selector.matchEthType(Ethernet.TYPE_IPV6);
977 selector.matchEthDst(ethCriterion.mac());
978 treatment.transition(UNICAST_ROUTING_TABLE);
979 rule = DefaultFlowRule.builder()
980 .forDevice(deviceId)
981 .withSelector(selector.build())
982 .withTreatment(treatment.build())
983 .withPriority(DEFAULT_PRIORITY)
984 .fromApp(applicationId)
985 .makePermanent()
986 .forTable(TMAC_TABLE).build();
Charles Chan206506b2018-03-02 16:43:28 -0800987 return ImmutableList.of(builder.add(rule).build());
Charles Chan5270ed02016-01-30 23:22:37 -0800988 }
989
Charles Chan206506b2018-03-02 16:43:28 -0800990 List<List<FlowRule>> processMcastEthDstFilter(EthCriterion ethCriterion,
Charles Chanb4879a52017-10-20 19:09:16 -0700991 VlanId assignedVlan,
Charles Chand57552d2018-03-02 15:41:41 -0800992 MacAddress unicastMac,
Yi Tsengef19de12017-04-24 11:33:05 -0700993 ApplicationId applicationId) {
Charles Chand57552d2018-03-02 15:41:41 -0800994 ImmutableList.Builder<FlowRule> unicastFlows = ImmutableList.builder();
995 ImmutableList.Builder<FlowRule> multicastFlows = ImmutableList.builder();
996 TrafficSelector.Builder selector;
997 TrafficTreatment.Builder treatment;
Charles Chanb4879a52017-10-20 19:09:16 -0700998 FlowRule rule;
Julia Ferguson65428c32017-08-10 18:15:24 +0000999
Charles Chanb4879a52017-10-20 19:09:16 -07001000 if (IPV4_MULTICAST.equals(ethCriterion.mac())) {
Charles Chand57552d2018-03-02 15:41:41 -08001001 if (requireUnicastBeforeMulticast()) {
1002 selector = DefaultTrafficSelector.builder();
1003 treatment = DefaultTrafficTreatment.builder();
1004 selector.matchEthType(Ethernet.TYPE_IPV4);
1005 selector.matchEthDst(unicastMac);
Charles Chan893a1d72018-05-10 22:19:25 +00001006 selector.matchVlanId(assignedVlan);
Charles Chand57552d2018-03-02 15:41:41 -08001007 treatment.transition(UNICAST_ROUTING_TABLE);
1008 rule = DefaultFlowRule.builder()
1009 .forDevice(deviceId)
1010 .withSelector(selector.build())
1011 .withTreatment(treatment.build())
Charles Chan893a1d72018-05-10 22:19:25 +00001012 .withPriority(DEFAULT_PRIORITY)
Charles Chand57552d2018-03-02 15:41:41 -08001013 .fromApp(applicationId)
1014 .makePermanent()
1015 .forTable(TMAC_TABLE).build();
1016 unicastFlows.add(rule);
1017 }
1018
1019 selector = DefaultTrafficSelector.builder();
1020 treatment = DefaultTrafficTreatment.builder();
Charles Chanb4879a52017-10-20 19:09:16 -07001021 selector.matchEthType(Ethernet.TYPE_IPV4);
1022 selector.matchEthDstMasked(ethCriterion.mac(), ethCriterion.mask());
Charles Chan893a1d72018-05-10 22:19:25 +00001023 selector.matchVlanId(assignedVlan);
Charles Chanb4879a52017-10-20 19:09:16 -07001024 treatment.transition(MULTICAST_ROUTING_TABLE);
1025 rule = DefaultFlowRule.builder()
1026 .forDevice(deviceId)
1027 .withSelector(selector.build())
1028 .withTreatment(treatment.build())
Charles Chan893a1d72018-05-10 22:19:25 +00001029 .withPriority(DEFAULT_PRIORITY)
Charles Chanb4879a52017-10-20 19:09:16 -07001030 .fromApp(applicationId)
1031 .makePermanent()
1032 .forTable(TMAC_TABLE).build();
Charles Chand57552d2018-03-02 15:41:41 -08001033 multicastFlows.add(rule);
Charles Chanb4879a52017-10-20 19:09:16 -07001034 }
1035
1036 if (IPV6_MULTICAST.equals(ethCriterion.mac())) {
Charles Chand57552d2018-03-02 15:41:41 -08001037 if (requireUnicastBeforeMulticast()) {
1038 selector = DefaultTrafficSelector.builder();
1039 treatment = DefaultTrafficTreatment.builder();
1040 selector.matchEthType(Ethernet.TYPE_IPV6);
1041 selector.matchEthDst(unicastMac);
Charles Chan893a1d72018-05-10 22:19:25 +00001042 selector.matchVlanId(assignedVlan);
Charles Chand57552d2018-03-02 15:41:41 -08001043 treatment.transition(UNICAST_ROUTING_TABLE);
1044 rule = DefaultFlowRule.builder()
1045 .forDevice(deviceId)
1046 .withSelector(selector.build())
1047 .withTreatment(treatment.build())
Charles Chan893a1d72018-05-10 22:19:25 +00001048 .withPriority(DEFAULT_PRIORITY)
Charles Chand57552d2018-03-02 15:41:41 -08001049 .fromApp(applicationId)
1050 .makePermanent()
1051 .forTable(TMAC_TABLE).build();
1052 unicastFlows.add(rule);
1053 }
1054
Charles Chanb4879a52017-10-20 19:09:16 -07001055 selector = DefaultTrafficSelector.builder();
1056 treatment = DefaultTrafficTreatment.builder();
1057 selector.matchEthType(Ethernet.TYPE_IPV6);
1058 selector.matchEthDstMasked(ethCriterion.mac(), ethCriterion.mask());
Charles Chan893a1d72018-05-10 22:19:25 +00001059 selector.matchVlanId(assignedVlan);
Charles Chanb4879a52017-10-20 19:09:16 -07001060 treatment.transition(MULTICAST_ROUTING_TABLE);
1061 rule = DefaultFlowRule.builder()
1062 .forDevice(deviceId)
1063 .withSelector(selector.build())
1064 .withTreatment(treatment.build())
Charles Chan893a1d72018-05-10 22:19:25 +00001065 .withPriority(DEFAULT_PRIORITY)
Charles Chanb4879a52017-10-20 19:09:16 -07001066 .fromApp(applicationId)
1067 .makePermanent()
1068 .forTable(TMAC_TABLE).build();
Charles Chand57552d2018-03-02 15:41:41 -08001069 multicastFlows.add(rule);
Charles Chanb4879a52017-10-20 19:09:16 -07001070 }
Charles Chand57552d2018-03-02 15:41:41 -08001071 return ImmutableList.of(unicastFlows.build(), multicastFlows.build());
Charles Chan5b9df8d2016-03-28 22:21:40 -07001072 }
1073
Saurav Das822c4e22015-10-23 10:51:11 -07001074 private Collection<FlowRule> processForward(ForwardingObjective fwd) {
1075 switch (fwd.flag()) {
1076 case SPECIFIC:
1077 return processSpecific(fwd);
1078 case VERSATILE:
1079 return processVersatile(fwd);
1080 default:
1081 fail(fwd, ObjectiveError.UNKNOWN);
1082 log.warn("Unknown forwarding flag {}", fwd.flag());
1083 }
1084 return Collections.emptySet();
1085 }
1086
1087 /**
1088 * In the OF-DPA 2.0 pipeline, versatile forwarding objectives go to the
1089 * ACL table.
1090 * @param fwd the forwarding objective of type 'versatile'
1091 * @return a collection of flow rules to be sent to the switch. An empty
1092 * collection may be returned if there is a problem in processing
1093 * the flow rule
1094 */
Saurav Das52025962016-01-28 22:30:01 -08001095 protected Collection<FlowRule> processVersatile(ForwardingObjective fwd) {
Saurav Das018605f2017-02-18 14:05:44 -08001096 log.debug("Processing versatile forwarding objective:{} in dev:{}",
Saurav Dasd2fded02016-12-02 15:43:47 -08001097 fwd.id(), deviceId);
Saurav Das822c4e22015-10-23 10:51:11 -07001098
Saurav Das822c4e22015-10-23 10:51:11 -07001099 if (fwd.nextId() == null && fwd.treatment() == null) {
1100 log.error("Forwarding objective {} from {} must contain "
1101 + "nextId or Treatment", fwd.selector(), fwd.appId());
Zsolt Haraszti9faab752016-02-17 15:55:20 -08001102 fail(fwd, ObjectiveError.BADPARAMS);
Saurav Das822c4e22015-10-23 10:51:11 -07001103 return Collections.emptySet();
1104 }
Saurav Das49cb5a12016-01-16 22:54:07 -08001105
Saurav Das77b5e902016-01-27 17:01:59 -08001106 TrafficSelector.Builder sbuilder = DefaultTrafficSelector.builder();
1107 fwd.selector().criteria().forEach(criterion -> {
1108 if (criterion instanceof VlanIdCriterion) {
1109 VlanId vlanId = ((VlanIdCriterion) criterion).vlanId();
1110 // ensure that match does not include vlan = NONE as OF-DPA does not
1111 // match untagged packets this way in the ACL table.
1112 if (vlanId.equals(VlanId.NONE)) {
1113 return;
1114 }
Charles Chand1172632017-03-15 17:33:09 -07001115 if (requireVlanExtensions()) {
1116 OfdpaMatchVlanVid ofdpaMatchVlanVid = new OfdpaMatchVlanVid(vlanId);
1117 sbuilder.extension(ofdpaMatchVlanVid, deviceId);
1118 } else {
1119 sbuilder.matchVlanId(vlanId);
1120 }
Charles Chan393339f2018-01-11 11:48:18 -08001121 } else if (criterion instanceof Icmpv6TypeCriterion) {
1122 byte icmpv6Type = (byte) ((Icmpv6TypeCriterion) criterion).icmpv6Type();
1123 sbuilder.matchIcmpv6Type(icmpv6Type);
1124 } else if (criterion instanceof Icmpv6CodeCriterion) {
1125 byte icmpv6Code = (byte) ((Icmpv6CodeCriterion) criterion).icmpv6Code();
1126 sbuilder.matchIcmpv6Type(icmpv6Code);
Charles Chand9e47c62017-10-05 15:17:15 -07001127 } else if (criterion instanceof TcpPortCriterion || criterion instanceof UdpPortCriterion) {
1128 // FIXME: QMX switches do not support L4 dst port matching in ACL table.
1129 // Currently L4 dst port matching is only used by DHCP relay feature
1130 // and therefore is safe to be replaced with L4 src port matching.
1131 // We need to revisit this if L4 dst port is used for other purpose in the future.
1132 if (!supportIpv6L4Dst() && isIpv6(fwd.selector())) {
Charles Chand9e47c62017-10-05 15:17:15 -07001133 switch (criterion.type()) {
1134 case UDP_DST:
Charles Chand9e47c62017-10-05 15:17:15 -07001135 case UDP_DST_MASKED:
Charles Chand9e47c62017-10-05 15:17:15 -07001136 case TCP_DST:
Charles Chand9e47c62017-10-05 15:17:15 -07001137 case TCP_DST_MASKED:
Charles Chand9e47c62017-10-05 15:17:15 -07001138 break;
1139 default:
1140 sbuilder.add(criterion);
1141 }
1142 } else {
1143 sbuilder.add(criterion);
1144 }
Saurav Das77b5e902016-01-27 17:01:59 -08001145 } else {
1146 sbuilder.add(criterion);
1147 }
1148 });
1149
Saurav Das822c4e22015-10-23 10:51:11 -07001150 // XXX driver does not currently do type checking as per Tables 65-67 in
1151 // OFDPA 2.0 spec. The only allowed treatment is a punt to the controller.
Saurav Das49cb5a12016-01-16 22:54:07 -08001152 TrafficTreatment.Builder ttBuilder = DefaultTrafficTreatment.builder();
1153 if (fwd.treatment() != null) {
1154 for (Instruction ins : fwd.treatment().allInstructions()) {
1155 if (ins instanceof OutputInstruction) {
1156 OutputInstruction o = (OutputInstruction) ins;
Charles Chan93090352018-03-02 13:26:22 -08001157 if (PortNumber.CONTROLLER.equals(o.port())) {
Saurav Das49cb5a12016-01-16 22:54:07 -08001158 ttBuilder.add(o);
1159 } else {
1160 log.warn("Only allowed treatments in versatile forwarding "
1161 + "objectives are punts to the controller");
1162 }
Charles Chan3fe71712018-06-15 18:54:18 -07001163 } else if (ins instanceof NoActionInstruction) {
1164 // No action is allowed and nothing needs to be done
Saurav Das49cb5a12016-01-16 22:54:07 -08001165 } else {
1166 log.warn("Cannot process instruction in versatile fwd {}", ins);
1167 }
Saurav Das822c4e22015-10-23 10:51:11 -07001168 }
Charles Chan2df0e8a2017-01-09 11:45:08 -08001169 if (fwd.treatment().clearedDeferred()) {
1170 ttBuilder.wipeDeferred();
1171 }
Saurav Das822c4e22015-10-23 10:51:11 -07001172 }
Saurav Das822c4e22015-10-23 10:51:11 -07001173 if (fwd.nextId() != null) {
Saurav Das49cb5a12016-01-16 22:54:07 -08001174 // overide case
1175 NextGroup next = getGroupForNextObjective(fwd.nextId());
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +05301176 if (next == null) {
1177 fail(fwd, ObjectiveError.BADPARAMS);
1178 return Collections.emptySet();
1179 }
Saurav Das49cb5a12016-01-16 22:54:07 -08001180 List<Deque<GroupKey>> gkeys = appKryo.deserialize(next.data());
1181 // we only need the top level group's key to point the flow to it
1182 Group group = groupService.getGroup(deviceId, gkeys.get(0).peekFirst());
1183 if (group == null) {
1184 log.warn("Group with key:{} for next-id:{} not found in dev:{}",
1185 gkeys.get(0).peekFirst(), fwd.nextId(), deviceId);
1186 fail(fwd, ObjectiveError.GROUPMISSING);
1187 return Collections.emptySet();
1188 }
1189 ttBuilder.deferred().group(group.id());
Saurav Das822c4e22015-10-23 10:51:11 -07001190 }
Saurav Das49cb5a12016-01-16 22:54:07 -08001191
1192 FlowRule.Builder ruleBuilder = DefaultFlowRule.builder()
1193 .fromApp(fwd.appId())
1194 .withPriority(fwd.priority())
1195 .forDevice(deviceId)
Saurav Das77b5e902016-01-27 17:01:59 -08001196 .withSelector(sbuilder.build())
Saurav Das49cb5a12016-01-16 22:54:07 -08001197 .withTreatment(ttBuilder.build())
1198 .makePermanent()
1199 .forTable(ACL_TABLE);
1200 return Collections.singletonList(ruleBuilder.build());
Saurav Das822c4e22015-10-23 10:51:11 -07001201 }
1202
1203 /**
1204 * In the OF-DPA 2.0 pipeline, specific forwarding refers to the IP table
Saurav Das8a0732e2015-11-20 15:27:53 -08001205 * (unicast or multicast) or the L2 table (mac + vlan) or the MPLS table.
Saurav Das822c4e22015-10-23 10:51:11 -07001206 *
1207 * @param fwd the forwarding objective of type 'specific'
1208 * @return a collection of flow rules. Typically there will be only one
1209 * for this type of forwarding objective. An empty set may be
1210 * returned if there is an issue in processing the objective.
1211 */
Charles Chan93090352018-03-02 13:26:22 -08001212 private Collection<FlowRule> processSpecific(ForwardingObjective fwd) {
Saurav Das25190812016-05-27 13:54:07 -07001213 log.debug("Processing specific fwd objective:{} in dev:{} with next:{}",
Saurav Das4ce45962015-11-24 23:21:05 -08001214 fwd.id(), deviceId, fwd.nextId());
1215 boolean isEthTypeObj = isSupportedEthTypeObjective(fwd);
1216 boolean isEthDstObj = isSupportedEthDstObjective(fwd);
1217
1218 if (isEthTypeObj) {
1219 return processEthTypeSpecific(fwd);
1220 } else if (isEthDstObj) {
1221 return processEthDstSpecific(fwd);
1222 } else {
1223 log.warn("processSpecific: Unsupported forwarding objective "
1224 + "criteria fwd:{} in dev:{}", fwd.nextId(), deviceId);
Saurav Das822c4e22015-10-23 10:51:11 -07001225 fail(fwd, ObjectiveError.UNSUPPORTED);
1226 return Collections.emptySet();
1227 }
Saurav Das4ce45962015-11-24 23:21:05 -08001228 }
1229
Saurav Das4ce45962015-11-24 23:21:05 -08001230 /**
1231 * Handles forwarding rules to the IP and MPLS tables.
1232 *
1233 * @param fwd the forwarding objective
1234 * @return A collection of flow rules, or an empty set
1235 */
1236 protected Collection<FlowRule> processEthTypeSpecific(ForwardingObjective fwd) {
Charles Chancad338a2016-09-16 18:03:11 -07001237 return processEthTypeSpecificInternal(fwd, false, ACL_TABLE);
Charles Chanf9e98652016-09-07 16:54:23 -07001238 }
1239
1240 /**
1241 * Internal implementation of processEthTypeSpecific.
1242 * <p>
1243 * Wildcarded IPv4_DST is not supported in OFDPA i12. Therefore, we break
1244 * the rule into 0.0.0.0/1 and 128.0.0.0/1.
1245 * The allowDefaultRoute must be set to false for OFDPA i12.
1246 * </p>
1247 *
1248 * @param fwd the forwarding objective
1249 * @param allowDefaultRoute allow wildcarded IPv4_DST or not
Ray Milkey0bb1e102016-11-10 14:51:27 -08001250 * @param mplsNextTable next MPLS table
Charles Chanf9e98652016-09-07 16:54:23 -07001251 * @return A collection of flow rules, or an empty set
1252 */
1253 protected Collection<FlowRule> processEthTypeSpecificInternal(ForwardingObjective fwd,
Charles Chancad338a2016-09-16 18:03:11 -07001254 boolean allowDefaultRoute,
1255 int mplsNextTable) {
Saurav Das4ce45962015-11-24 23:21:05 -08001256 TrafficSelector selector = fwd.selector();
1257 EthTypeCriterion ethType =
1258 (EthTypeCriterion) selector.getCriterion(Criterion.Type.ETH_TYPE);
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001259 boolean emptyGroup = false;
Charles Chan188ebf52015-12-23 00:15:11 -08001260 int forTableId;
Saurav Das8a0732e2015-11-20 15:27:53 -08001261 TrafficSelector.Builder filteredSelector = DefaultTrafficSelector.builder();
Charles Chan14967c22015-12-07 11:11:50 -08001262 TrafficTreatment.Builder tb = DefaultTrafficTreatment.builder();
Flavio Castroe10fa242016-01-15 12:43:51 -08001263 TrafficSelector.Builder complementarySelector = DefaultTrafficSelector.builder();
Charles Chan14967c22015-12-07 11:11:50 -08001264
Saurav Das8a0732e2015-11-20 15:27:53 -08001265 if (ethType.ethType().toShort() == Ethernet.TYPE_IPV4) {
Pier Luigi3bfe32c2017-01-30 09:47:36 -08001266 if (buildIpv4Selector(filteredSelector, complementarySelector, fwd, allowDefaultRoute) < 0) {
1267 return Collections.emptyList();
1268 }
1269 // We need to set properly the next table
Flavio Castroe10fa242016-01-15 12:43:51 -08001270 IpPrefix ipv4Dst = ((IPCriterion) selector.getCriterion(Criterion.Type.IPV4_DST)).ip();
Charles Chan5b9df8d2016-03-28 22:21:40 -07001271 if (ipv4Dst.isMulticast()) {
Charles Chan5b9df8d2016-03-28 22:21:40 -07001272 forTableId = MULTICAST_ROUTING_TABLE;
Flavio Castroe10fa242016-01-15 12:43:51 -08001273 } else {
Charles Chan5b9df8d2016-03-28 22:21:40 -07001274 forTableId = UNICAST_ROUTING_TABLE;
Flavio Castroe10fa242016-01-15 12:43:51 -08001275 }
Charles Chan93090352018-03-02 13:26:22 -08001276
Charles Chan7feb6c82018-03-05 11:28:23 -08001277 // TODO decrementing IP ttl is done automatically for routing, this
Charles Chan93090352018-03-02 13:26:22 -08001278 // action is ignored or rejected in ofdpa as it is not fully implemented
Charles Chan7feb6c82018-03-05 11:28:23 -08001279 // if (fwd.treatment() != null) {
1280 // for (Instruction instr : fwd.treatment().allInstructions()) {
1281 // if (instr instanceof L3ModificationInstruction &&
1282 // ((L3ModificationInstruction) instr).subtype() == L3SubType.DEC_TTL) {
1283 // tb.deferred().add(instr);
1284 // }
1285 // }
1286 // }
1287
Pier Ventree0ae7a32016-11-23 09:57:42 -08001288 } else if (ethType.ethType().toShort() == Ethernet.TYPE_IPV6) {
1289 if (buildIpv6Selector(filteredSelector, fwd) < 0) {
1290 return Collections.emptyList();
1291 }
Kalhee Kim3d26bf02017-10-04 14:59:11 +00001292 //We need to set the proper next table
1293 IpPrefix ipv6Dst = ((IPCriterion) selector.getCriterion(Criterion.Type.IPV6_DST)).ip();
1294 if (ipv6Dst.isMulticast()) {
1295 forTableId = MULTICAST_ROUTING_TABLE;
1296 } else {
1297 forTableId = UNICAST_ROUTING_TABLE;
1298 }
1299
Charles Chan7feb6c82018-03-05 11:28:23 -08001300 // TODO decrementing IP ttl is done automatically for routing, this
Charles Chan93090352018-03-02 13:26:22 -08001301 // action is ignored or rejected in ofdpa as it is not fully implemented
Charles Chan7feb6c82018-03-05 11:28:23 -08001302 // if (fwd.treatment() != null) {
1303 // for (Instruction instr : fwd.treatment().allInstructions()) {
1304 // if (instr instanceof L3ModificationInstruction &&
1305 // ((L3ModificationInstruction) instr).subtype() == L3SubType.DEC_TTL) {
1306 // tb.deferred().add(instr);
1307 // }
1308 // }
1309 // }
Saurav Das8a0732e2015-11-20 15:27:53 -08001310 } else {
1311 filteredSelector
1312 .matchEthType(Ethernet.MPLS_UNICAST)
1313 .matchMplsLabel(((MplsCriterion)
1314 selector.getCriterion(Criterion.Type.MPLS_LABEL)).label());
1315 MplsBosCriterion bos = (MplsBosCriterion) selector
Pier Ventre140a8942016-11-02 07:26:38 -07001316 .getCriterion(MPLS_BOS);
Alex Yashchuka3679532017-12-08 17:40:05 +02001317 if (bos != null && requireMplsBosMatch()) {
Saurav Das8a0732e2015-11-20 15:27:53 -08001318 filteredSelector.matchMplsBos(bos.mplsBos());
1319 }
1320 forTableId = MPLS_TABLE_1;
Saurav Das4ce45962015-11-24 23:21:05 -08001321 log.debug("processing MPLS specific forwarding objective {} -> next:{}"
1322 + " in dev {}", fwd.id(), fwd.nextId(), deviceId);
Saurav Das822c4e22015-10-23 10:51:11 -07001323
Charles Chan14967c22015-12-07 11:11:50 -08001324 if (fwd.treatment() != null) {
1325 for (Instruction instr : fwd.treatment().allInstructions()) {
1326 if (instr instanceof L2ModificationInstruction &&
1327 ((L2ModificationInstruction) instr).subtype() == L2SubType.MPLS_POP) {
Pier Luigi3bfe32c2017-01-30 09:47:36 -08001328 // OF-DPA does not pop in MPLS table in some cases. For the L3 VPN, it requires
Saurav Das9c705342017-01-06 11:36:01 -08001329 // setting the MPLS_TYPE so pop can happen down the pipeline
Alex Yashchuka3679532017-12-08 17:40:05 +02001330 if (requireMplsPop()) {
1331 if (mplsNextTable == MPLS_TYPE_TABLE && isNotMplsBos(selector)) {
1332 tb.immediate().popMpls();
1333 }
1334 } else {
1335 // Skip mpls pop action for mpls_unicast label
1336 if (instr instanceof ModMplsHeaderInstruction &&
1337 !((ModMplsHeaderInstruction) instr).ethernetType()
1338 .equals(EtherType.MPLS_UNICAST.ethType())) {
1339 tb.immediate().add(instr);
1340 }
Pier Luigi3bfe32c2017-01-30 09:47:36 -08001341 }
Charles Chan14967c22015-12-07 11:11:50 -08001342 }
Alex Yashchuka3679532017-12-08 17:40:05 +02001343
1344 if (requireMplsTtlModification()) {
1345 if (instr instanceof L3ModificationInstruction &&
1346 ((L3ModificationInstruction) instr).subtype() == L3SubType.DEC_TTL) {
1347 // FIXME Should modify the app to send the correct DEC_MPLS_TTL instruction
1348 tb.immediate().decMplsTtl();
1349 }
1350 if (instr instanceof L3ModificationInstruction &&
1351 ((L3ModificationInstruction) instr).subtype() == L3SubType.TTL_IN) {
1352 tb.immediate().add(instr);
1353 }
Charles Chan14967c22015-12-07 11:11:50 -08001354 }
Saurav Das8a0732e2015-11-20 15:27:53 -08001355 }
1356 }
1357 }
Saurav Das822c4e22015-10-23 10:51:11 -07001358
1359 if (fwd.nextId() != null) {
Saurav Das423fe2b2015-12-04 10:52:59 -08001360 NextGroup next = getGroupForNextObjective(fwd.nextId());
1361 if (next != null) {
1362 List<Deque<GroupKey>> gkeys = appKryo.deserialize(next.data());
1363 // we only need the top level group's key to point the flow to it
1364 Group group = groupService.getGroup(deviceId, gkeys.get(0).peekFirst());
1365 if (group == null) {
1366 log.warn("Group with key:{} for next-id:{} not found in dev:{}",
1367 gkeys.get(0).peekFirst(), fwd.nextId(), deviceId);
1368 fail(fwd, ObjectiveError.GROUPMISSING);
1369 return Collections.emptySet();
1370 }
Saurav Dasa89b95a2018-02-14 14:14:54 -08001371 if (isNotMplsBos(selector) && group.type().equals(SELECT)) {
1372 log.warn("SR CONTINUE case cannot be handled as MPLS ECMP "
1373 + "is not implemented in OF-DPA yet. Aborting flow {} -> next:{} "
1374 + "in this device {}", fwd.id(), fwd.nextId(), deviceId);
1375 fail(fwd, ObjectiveError.FLOWINSTALLATIONFAILED);
1376 return Collections.emptySet();
1377 }
Saurav Das423fe2b2015-12-04 10:52:59 -08001378 tb.deferred().group(group.id());
Saurav Dasf3f75942018-01-25 09:49:01 -08001379 // retrying flows may be necessary due to bug CORD-554
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001380 if (gkeys.size() == 1 && gkeys.get(0).size() == 1) {
Saurav Dasf3f75942018-01-25 09:49:01 -08001381 if (shouldRetry()) {
1382 log.warn("Found empty group 0x{} in dev:{} .. will retry fwd:{}",
1383 Integer.toHexString(group.id().id()), deviceId,
1384 fwd.id());
1385 emptyGroup = true;
1386 }
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001387 }
Saurav Das25190812016-05-27 13:54:07 -07001388 } else {
1389 log.warn("Cannot find group for nextId:{} in dev:{}. Aborting fwd:{}",
1390 fwd.nextId(), deviceId, fwd.id());
1391 fail(fwd, ObjectiveError.FLOWINSTALLATIONFAILED);
1392 return Collections.emptySet();
Saurav Das822c4e22015-10-23 10:51:11 -07001393 }
Saurav Das822c4e22015-10-23 10:51:11 -07001394 }
Charles Chancad338a2016-09-16 18:03:11 -07001395
1396 if (forTableId == MPLS_TABLE_1) {
Pier Ventre140a8942016-11-02 07:26:38 -07001397 if (mplsNextTable == MPLS_L3_TYPE_TABLE) {
Charles Chancad338a2016-09-16 18:03:11 -07001398 Ofdpa3SetMplsType setMplsType = new Ofdpa3SetMplsType(Ofdpa3MplsType.L3_PHP);
Saurav Das9c705342017-01-06 11:36:01 -08001399 // set mpls type as apply_action
1400 tb.immediate().extension(setMplsType, deviceId);
Charles Chancad338a2016-09-16 18:03:11 -07001401 }
1402 tb.transition(mplsNextTable);
1403 } else {
1404 tb.transition(ACL_TABLE);
1405 }
1406
Andrea Campanella84ac4df2018-04-30 11:48:55 +02001407 if (fwd.treatment() != null && fwd.treatment().clearedDeferred()) {
1408 if (supportsUnicastBlackHole()) {
1409 tb.wipeDeferred();
1410 } else {
1411 log.warn("Clear Deferred is not supported Unicast Routing Table on device {}", deviceId);
1412 return Collections.emptySet();
1413 }
1414 }
1415
Saurav Das822c4e22015-10-23 10:51:11 -07001416 FlowRule.Builder ruleBuilder = DefaultFlowRule.builder()
1417 .fromApp(fwd.appId())
1418 .withPriority(fwd.priority())
1419 .forDevice(deviceId)
Saurav Das8a0732e2015-11-20 15:27:53 -08001420 .withSelector(filteredSelector.build())
1421 .withTreatment(tb.build())
1422 .forTable(forTableId);
Saurav Das822c4e22015-10-23 10:51:11 -07001423
1424 if (fwd.permanent()) {
1425 ruleBuilder.makePermanent();
1426 } else {
1427 ruleBuilder.makeTemporary(fwd.timeout());
1428 }
Flavio Castroe10fa242016-01-15 12:43:51 -08001429 Collection<FlowRule> flowRuleCollection = new ArrayList<>();
1430 flowRuleCollection.add(ruleBuilder.build());
Pier Luigi3bfe32c2017-01-30 09:47:36 -08001431 if (!allowDefaultRoute) {
Pier Ventree0ae7a32016-11-23 09:57:42 -08001432 flowRuleCollection.add(
1433 defaultRoute(fwd, complementarySelector, forTableId, tb)
1434 );
Flavio Castroe10fa242016-01-15 12:43:51 -08001435 log.debug("Default rule 0.0.0.0/0 is being installed two rules");
1436 }
Saurav Dasf3f75942018-01-25 09:49:01 -08001437
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001438 if (emptyGroup) {
pier5b9b9e32019-03-11 15:14:02 -07001439 retryExecutorService.schedule(new RetryFlows(fwd, flowRuleCollection),
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001440 RETRY_MS, TimeUnit.MILLISECONDS);
1441 }
Flavio Castroe10fa242016-01-15 12:43:51 -08001442 return flowRuleCollection;
Saurav Das822c4e22015-10-23 10:51:11 -07001443 }
1444
Charles Chan93090352018-03-02 13:26:22 -08001445 private int buildIpv4Selector(TrafficSelector.Builder builderToUpdate,
Pier Luigi3bfe32c2017-01-30 09:47:36 -08001446 TrafficSelector.Builder extBuilder,
1447 ForwardingObjective fwd,
1448 boolean allowDefaultRoute) {
1449 TrafficSelector selector = fwd.selector();
1450
1451 IpPrefix ipv4Dst = ((IPCriterion) selector.getCriterion(Criterion.Type.IPV4_DST)).ip();
1452 if (ipv4Dst.isMulticast()) {
1453 if (ipv4Dst.prefixLength() != 32) {
1454 log.warn("Multicast specific forwarding objective can only be /32");
1455 fail(fwd, ObjectiveError.BADPARAMS);
1456 return -1;
1457 }
1458 VlanId assignedVlan = readVlanFromSelector(fwd.meta());
1459 if (assignedVlan == null) {
1460 log.warn("VLAN ID required by multicast specific fwd obj is missing. Abort.");
1461 fail(fwd, ObjectiveError.BADPARAMS);
1462 return -1;
1463 }
Charles Chand1172632017-03-15 17:33:09 -07001464 if (requireVlanExtensions()) {
1465 OfdpaMatchVlanVid ofdpaMatchVlanVid = new OfdpaMatchVlanVid(assignedVlan);
1466 builderToUpdate.extension(ofdpaMatchVlanVid, deviceId);
1467 } else {
1468 builderToUpdate.matchVlanId(assignedVlan);
1469 }
Pier Luigi3bfe32c2017-01-30 09:47:36 -08001470 builderToUpdate.matchEthType(Ethernet.TYPE_IPV4).matchIPDst(ipv4Dst);
1471 log.debug("processing IPv4 multicast specific forwarding objective {} -> next:{}"
1472 + " in dev:{}", fwd.id(), fwd.nextId(), deviceId);
1473 } else {
1474 if (ipv4Dst.prefixLength() == 0) {
1475 if (allowDefaultRoute) {
1476 // The entire IPV4_DST field is wildcarded intentionally
1477 builderToUpdate.matchEthType(Ethernet.TYPE_IPV4);
1478 } else {
1479 // NOTE: The switch does not support matching 0.0.0.0/0
1480 // Split it into 0.0.0.0/1 and 128.0.0.0/1
1481 builderToUpdate.matchEthType(Ethernet.TYPE_IPV4)
1482 .matchIPDst(IpPrefix.valueOf("0.0.0.0/1"));
1483 extBuilder.matchEthType(Ethernet.TYPE_IPV4)
1484 .matchIPDst(IpPrefix.valueOf("128.0.0.0/1"));
1485 }
1486 } else {
1487 builderToUpdate.matchEthType(Ethernet.TYPE_IPV4).matchIPDst(ipv4Dst);
1488 }
1489 log.debug("processing IPv4 unicast specific forwarding objective {} -> next:{}"
1490 + " in dev:{}", fwd.id(), fwd.nextId(), deviceId);
1491 }
1492 return 0;
1493 }
1494
1495 /**
1496 * Helper method to build Ipv6 selector using the selector provided by
1497 * a forwarding objective.
1498 *
1499 * @param builderToUpdate the builder to update
1500 * @param fwd the selector to read
1501 * @return 0 if the update ends correctly. -1 if the matches
1502 * are not yet supported
1503 */
Charles Chan93090352018-03-02 13:26:22 -08001504 int buildIpv6Selector(TrafficSelector.Builder builderToUpdate,
Pier Luigi3bfe32c2017-01-30 09:47:36 -08001505 ForwardingObjective fwd) {
1506
1507 TrafficSelector selector = fwd.selector();
1508
1509 IpPrefix ipv6Dst = ((IPCriterion) selector.getCriterion(Criterion.Type.IPV6_DST)).ip();
1510 if (ipv6Dst.isMulticast()) {
Julia Ferguson65428c32017-08-10 18:15:24 +00001511 if (ipv6Dst.prefixLength() != IpAddress.INET6_BIT_LENGTH) {
1512 log.warn("Multicast specific forwarding objective can only be /128");
1513 fail(fwd, ObjectiveError.BADPARAMS);
1514 return -1;
1515 }
1516 VlanId assignedVlan = readVlanFromSelector(fwd.meta());
1517 if (assignedVlan == null) {
1518 log.warn("VLAN ID required by multicast specific fwd obj is missing. Abort.");
1519 fail(fwd, ObjectiveError.BADPARAMS);
1520 return -1;
1521 }
1522 if (requireVlanExtensions()) {
1523 OfdpaMatchVlanVid ofdpaMatchVlanVid = new OfdpaMatchVlanVid(assignedVlan);
1524 builderToUpdate.extension(ofdpaMatchVlanVid, deviceId);
1525 } else {
1526 builderToUpdate.matchVlanId(assignedVlan);
1527 }
1528 builderToUpdate.matchEthType(Ethernet.TYPE_IPV6).matchIPv6Dst(ipv6Dst);
1529 log.debug("processing IPv6 multicast specific forwarding objective {} -> next:{}"
1530 + " in dev:{}", fwd.id(), fwd.nextId(), deviceId);
1531 } else {
1532 if (ipv6Dst.prefixLength() != 0) {
1533 builderToUpdate.matchIPv6Dst(ipv6Dst);
1534 }
Pier Luigi3bfe32c2017-01-30 09:47:36 -08001535 builderToUpdate.matchEthType(Ethernet.TYPE_IPV6);
1536 log.debug("processing IPv6 unicast specific forwarding objective {} -> next:{}"
1537 + " in dev:{}", fwd.id(), fwd.nextId(), deviceId);
Julia Ferguson65428c32017-08-10 18:15:24 +00001538 }
Pier Luigi3bfe32c2017-01-30 09:47:36 -08001539 return 0;
1540 }
1541
Charles Chan93090352018-03-02 13:26:22 -08001542 FlowRule defaultRoute(ForwardingObjective fwd,
Pier Ventree0ae7a32016-11-23 09:57:42 -08001543 TrafficSelector.Builder complementarySelector,
1544 int forTableId,
1545 TrafficTreatment.Builder tb) {
1546 FlowRule.Builder rule = DefaultFlowRule.builder()
1547 .fromApp(fwd.appId())
1548 .withPriority(fwd.priority())
1549 .forDevice(deviceId)
1550 .withSelector(complementarySelector.build())
1551 .withTreatment(tb.build())
1552 .forTable(forTableId);
1553 if (fwd.permanent()) {
1554 rule.makePermanent();
1555 } else {
1556 rule.makeTemporary(fwd.timeout());
1557 }
1558 return rule.build();
1559 }
1560
Saurav Das4ce45962015-11-24 23:21:05 -08001561 /**
1562 * Handles forwarding rules to the L2 bridging table. Flow actions are not
1563 * allowed in the bridging table - instead we use L2 Interface group or
1564 * L2 flood group
1565 *
1566 * @param fwd the forwarding objective
1567 * @return A collection of flow rules, or an empty set
1568 */
1569 protected Collection<FlowRule> processEthDstSpecific(ForwardingObjective fwd) {
1570 List<FlowRule> rules = new ArrayList<>();
1571
1572 // Build filtered selector
1573 TrafficSelector selector = fwd.selector();
1574 EthCriterion ethCriterion = (EthCriterion) selector
1575 .getCriterion(Criterion.Type.ETH_DST);
1576 VlanIdCriterion vlanIdCriterion = (VlanIdCriterion) selector
1577 .getCriterion(Criterion.Type.VLAN_VID);
1578
1579 if (vlanIdCriterion == null) {
1580 log.warn("Forwarding objective for bridging requires vlan. Not "
1581 + "installing fwd:{} in dev:{}", fwd.id(), deviceId);
1582 fail(fwd, ObjectiveError.BADPARAMS);
1583 return Collections.emptySet();
1584 }
1585
1586 TrafficSelector.Builder filteredSelectorBuilder =
1587 DefaultTrafficSelector.builder();
Yi Tseng3a77b4f2017-02-06 15:02:17 -08001588
1589 if (!ethCriterion.mac().equals(NONE) &&
1590 !ethCriterion.mac().equals(BROADCAST)) {
Saurav Das4ce45962015-11-24 23:21:05 -08001591 filteredSelectorBuilder.matchEthDst(ethCriterion.mac());
1592 log.debug("processing L2 forwarding objective:{} -> next:{} in dev:{}",
1593 fwd.id(), fwd.nextId(), deviceId);
1594 } else {
Yi Tseng3a77b4f2017-02-06 15:02:17 -08001595 // Use wildcard DST_MAC if the MacAddress is None or Broadcast
Saurav Das4ce45962015-11-24 23:21:05 -08001596 log.debug("processing L2 Broadcast forwarding objective:{} -> next:{} "
1597 + "in dev:{} for vlan:{}",
1598 fwd.id(), fwd.nextId(), deviceId, vlanIdCriterion.vlanId());
1599 }
Charles Chand1172632017-03-15 17:33:09 -07001600 if (requireVlanExtensions()) {
1601 OfdpaMatchVlanVid ofdpaMatchVlanVid = new OfdpaMatchVlanVid(vlanIdCriterion.vlanId());
1602 filteredSelectorBuilder.extension(ofdpaMatchVlanVid, deviceId);
1603 } else {
1604 filteredSelectorBuilder.matchVlanId(vlanIdCriterion.vlanId());
1605 }
Saurav Das4ce45962015-11-24 23:21:05 -08001606 TrafficSelector filteredSelector = filteredSelectorBuilder.build();
1607
1608 if (fwd.treatment() != null) {
1609 log.warn("Ignoring traffic treatment in fwd rule {} meant for L2 table"
1610 + "for dev:{}. Expecting only nextId", fwd.id(), deviceId);
1611 }
1612
1613 TrafficTreatment.Builder treatmentBuilder = DefaultTrafficTreatment.builder();
1614 if (fwd.nextId() != null) {
Saurav Das423fe2b2015-12-04 10:52:59 -08001615 NextGroup next = getGroupForNextObjective(fwd.nextId());
Saurav Das4ce45962015-11-24 23:21:05 -08001616 if (next != null) {
1617 List<Deque<GroupKey>> gkeys = appKryo.deserialize(next.data());
1618 // we only need the top level group's key to point the flow to it
1619 Group group = groupService.getGroup(deviceId, gkeys.get(0).peekFirst());
1620 if (group != null) {
1621 treatmentBuilder.deferred().group(group.id());
1622 } else {
1623 log.warn("Group with key:{} for next-id:{} not found in dev:{}",
1624 gkeys.get(0).peekFirst(), fwd.nextId(), deviceId);
1625 fail(fwd, ObjectiveError.GROUPMISSING);
1626 return Collections.emptySet();
1627 }
1628 }
1629 }
1630 treatmentBuilder.immediate().transition(ACL_TABLE);
1631 TrafficTreatment filteredTreatment = treatmentBuilder.build();
1632
1633 // Build bridging table entries
1634 FlowRule.Builder flowRuleBuilder = DefaultFlowRule.builder();
1635 flowRuleBuilder.fromApp(fwd.appId())
1636 .withPriority(fwd.priority())
1637 .forDevice(deviceId)
1638 .withSelector(filteredSelector)
1639 .withTreatment(filteredTreatment)
1640 .forTable(BRIDGING_TABLE);
1641 if (fwd.permanent()) {
1642 flowRuleBuilder.makePermanent();
1643 } else {
1644 flowRuleBuilder.makeTemporary(fwd.timeout());
1645 }
1646 rules.add(flowRuleBuilder.build());
1647 return rules;
1648 }
1649
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001650 //////////////////////////////////////
1651 // Helper Methods and Classes
1652 //////////////////////////////////////
1653
1654 private boolean isSupportedEthTypeObjective(ForwardingObjective fwd) {
1655 TrafficSelector selector = fwd.selector();
1656 EthTypeCriterion ethType = (EthTypeCriterion) selector
1657 .getCriterion(Criterion.Type.ETH_TYPE);
1658 return !((ethType == null) ||
1659 ((ethType.ethType().toShort() != Ethernet.TYPE_IPV4) &&
Pier Ventree0ae7a32016-11-23 09:57:42 -08001660 (ethType.ethType().toShort() != Ethernet.MPLS_UNICAST)) &&
1661 (ethType.ethType().toShort() != Ethernet.TYPE_IPV6));
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001662 }
1663
1664 private boolean isSupportedEthDstObjective(ForwardingObjective fwd) {
1665 TrafficSelector selector = fwd.selector();
1666 EthCriterion ethDst = (EthCriterion) selector
1667 .getCriterion(Criterion.Type.ETH_DST);
1668 VlanIdCriterion vlanId = (VlanIdCriterion) selector
1669 .getCriterion(Criterion.Type.VLAN_VID);
1670 return !(ethDst == null && vlanId == null);
1671 }
1672
Charles Chan93090352018-03-02 13:26:22 -08001673 NextGroup getGroupForNextObjective(Integer nextId) {
Saurav Das423fe2b2015-12-04 10:52:59 -08001674 NextGroup next = flowObjectiveStore.getNextGroup(nextId);
1675 if (next != null) {
1676 List<Deque<GroupKey>> gkeys = appKryo.deserialize(next.data());
1677 if (gkeys != null && !gkeys.isEmpty()) {
1678 return next;
1679 } else {
1680 log.warn("Empty next group found in FlowObjective store for "
1681 + "next-id:{} in dev:{}", nextId, deviceId);
1682 }
1683 } else {
1684 log.warn("next-id {} not found in Flow objective store for dev:{}",
1685 nextId, deviceId);
1686 }
1687 return null;
1688 }
1689
Charles Chan188ebf52015-12-23 00:15:11 -08001690 protected static void pass(Objective obj) {
Sho SHIMIZUef7e2902016-02-12 18:38:29 -08001691 obj.context().ifPresent(context -> context.onSuccess(obj));
Saurav Das822c4e22015-10-23 10:51:11 -07001692 }
1693
Charles Chan188ebf52015-12-23 00:15:11 -08001694 protected static void fail(Objective obj, ObjectiveError error) {
Sho SHIMIZUef7e2902016-02-12 18:38:29 -08001695 obj.context().ifPresent(context -> context.onError(obj, error));
Saurav Das822c4e22015-10-23 10:51:11 -07001696 }
Saurav Das24431192016-03-07 19:13:00 -08001697
Saurav Das24431192016-03-07 19:13:00 -08001698 @Override
1699 public List<String> getNextMappings(NextGroup nextGroup) {
1700 List<String> mappings = new ArrayList<>();
1701 List<Deque<GroupKey>> gkeys = appKryo.deserialize(nextGroup.data());
1702 for (Deque<GroupKey> gkd : gkeys) {
1703 Group lastGroup = null;
Yuta HIGUCHI2dce08a2017-04-20 21:57:48 -07001704 StringBuilder gchain = new StringBuilder();
Saurav Das24431192016-03-07 19:13:00 -08001705 for (GroupKey gk : gkd) {
1706 Group g = groupService.getGroup(deviceId, gk);
Saurav Das8be4e3a2016-03-11 17:19:07 -08001707 if (g == null) {
Saurav Das25190812016-05-27 13:54:07 -07001708 gchain.append(" NoGrp").append(" -->");
Saurav Das8be4e3a2016-03-11 17:19:07 -08001709 continue;
1710 }
1711 gchain.append(" 0x").append(Integer.toHexString(g.id().id()))
1712 .append(" -->");
Saurav Das24431192016-03-07 19:13:00 -08001713 lastGroup = g;
1714 }
1715 // add port information for last group in group-chain
Yuta HIGUCHI2dce08a2017-04-20 21:57:48 -07001716 List<Instruction> lastGroupIns = new ArrayList<>();
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001717 if (lastGroup != null && !lastGroup.buckets().buckets().isEmpty()) {
Saurav Das25190812016-05-27 13:54:07 -07001718 lastGroupIns = lastGroup.buckets().buckets().get(0)
1719 .treatment().allInstructions();
1720 }
1721 for (Instruction i: lastGroupIns) {
Saurav Das24431192016-03-07 19:13:00 -08001722 if (i instanceof OutputInstruction) {
Saurav Das8be4e3a2016-03-11 17:19:07 -08001723 gchain.append(" port:").append(((OutputInstruction) i).port());
Saurav Das24431192016-03-07 19:13:00 -08001724 }
1725 }
Saurav Das8be4e3a2016-03-11 17:19:07 -08001726 mappings.add(gchain.toString());
Saurav Das24431192016-03-07 19:13:00 -08001727 }
1728 return mappings;
1729 }
Charles Chan5b9df8d2016-03-28 22:21:40 -07001730
Saurav Dasa89b95a2018-02-14 14:14:54 -08001731 /**
1732 * Returns true iff the given selector matches on BOS==true, indicating that
1733 * the selector is trying to match on a label that is bottom-of-stack.
1734 *
1735 * @param selector the given match
1736 * @return true iff BoS==true; false if BOS==false, or BOS matching is not
1737 * expressed in the given selector
1738 */
Pier Ventre140a8942016-11-02 07:26:38 -07001739 static boolean isMplsBos(TrafficSelector selector) {
1740 MplsBosCriterion bosCriterion = (MplsBosCriterion) selector.getCriterion(MPLS_BOS);
1741 return bosCriterion != null && bosCriterion.mplsBos();
1742 }
1743
Saurav Dasa89b95a2018-02-14 14:14:54 -08001744 /**
1745 * Returns true iff the given selector matches on BOS==false, indicating
1746 * that the selector is trying to match on a label that is not the
1747 * bottom-of-stack label.
1748 *
1749 * @param selector the given match
1750 * @return true iff BoS==false;
1751 * false if BOS==true, or BOS matching is not expressed in the given selector
1752 */
Pier Ventre140a8942016-11-02 07:26:38 -07001753 static boolean isNotMplsBos(TrafficSelector selector) {
1754 MplsBosCriterion bosCriterion = (MplsBosCriterion) selector.getCriterion(MPLS_BOS);
1755 return bosCriterion != null && !bosCriterion.mplsBos();
1756 }
1757
Saurav Dasa89b95a2018-02-14 14:14:54 -08001758 /**
1759 * Returns true iff the forwarding objective includes a treatment to pop the
1760 * MPLS label.
1761 *
1762 * @param fwd the given forwarding objective
1763 * @return true iff mpls pop treatment exists
1764 */
1765 static boolean isMplsPop(ForwardingObjective fwd) {
1766 if (fwd.treatment() != null) {
1767 for (Instruction instr : fwd.treatment().allInstructions()) {
1768 if (instr instanceof L2ModificationInstruction
1769 && ((L2ModificationInstruction) instr)
1770 .subtype() == L2SubType.MPLS_POP) {
1771 return true;
1772 }
1773 }
1774 }
1775 return false;
1776 }
1777
Charles Chand9e47c62017-10-05 15:17:15 -07001778 private static boolean isIpv6(TrafficSelector selector) {
1779 EthTypeCriterion ethTypeCriterion = (EthTypeCriterion) selector.getCriterion(ETH_TYPE);
1780 return ethTypeCriterion != null && ethTypeCriterion.ethType().toShort() == Ethernet.TYPE_IPV6;
1781 }
1782
Charles Chan93090352018-03-02 13:26:22 -08001783 static VlanId readVlanFromSelector(TrafficSelector selector) {
Saurav Das59232cf2016-04-27 18:35:50 -07001784 if (selector == null) {
1785 return null;
1786 }
Charles Chan5b9df8d2016-03-28 22:21:40 -07001787 Criterion criterion = selector.getCriterion(Criterion.Type.VLAN_VID);
1788 return (criterion == null)
1789 ? null : ((VlanIdCriterion) criterion).vlanId();
1790 }
1791
jayakumarthazhathad3636a2018-10-01 00:51:54 +05301792 static MacAddress readEthDstFromSelector(TrafficSelector selector) {
1793 if (selector == null) {
1794 return null;
1795 }
1796 Criterion criterion = selector.getCriterion(Criterion.Type.ETH_DST);
1797 return (criterion == null)
1798 ? null : ((EthCriterion) criterion).mac();
1799 }
1800
Charles Chand9e47c62017-10-05 15:17:15 -07001801 static IpPrefix readIpDstFromSelector(TrafficSelector selector) {
Saurav Das59232cf2016-04-27 18:35:50 -07001802 if (selector == null) {
1803 return null;
1804 }
Charles Chan5b9df8d2016-03-28 22:21:40 -07001805 Criterion criterion = selector.getCriterion(Criterion.Type.IPV4_DST);
1806 return (criterion == null) ? null : ((IPCriterion) criterion).ip();
1807 }
Charles Chand55e84d2016-03-30 17:54:24 -07001808
1809 private static VlanId readVlanFromTreatment(TrafficTreatment treatment) {
Saurav Das59232cf2016-04-27 18:35:50 -07001810 if (treatment == null) {
1811 return null;
1812 }
Charles Chand55e84d2016-03-30 17:54:24 -07001813 for (Instruction i : treatment.allInstructions()) {
1814 if (i instanceof ModVlanIdInstruction) {
1815 return ((ModVlanIdInstruction) i).vlanId();
1816 }
1817 }
1818 return null;
1819 }
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001820
Charles Chand57552d2018-03-02 15:41:41 -08001821 private static MacAddress readEthDstFromTreatment(TrafficTreatment treatment) {
1822 if (treatment == null) {
1823 return null;
1824 }
1825 for (Instruction i : treatment.allInstructions()) {
1826 if (i instanceof ModEtherInstruction) {
1827 ModEtherInstruction modEtherInstruction = (ModEtherInstruction) i;
1828 if (modEtherInstruction.subtype() == L2SubType.ETH_DST) {
1829 return modEtherInstruction.mac();
1830 }
1831 }
1832 }
1833 return null;
1834 }
1835
jayakumarthazhathad3636a2018-10-01 00:51:54 +05301836 static ExtensionSelector readExtensionFromSelector(TrafficSelector selector) {
1837 if (selector == null) {
1838 return null;
1839 }
1840 ExtensionCriterion criterion = (ExtensionCriterion) selector.getCriterion(Criterion.Type.EXTENSION);
1841 return (criterion == null) ? null : criterion.extensionSelector();
1842 }
1843
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001844 /**
1845 * Utility class that retries sending flows a fixed number of times, even if
1846 * some of the attempts are successful. Used only for forwarding objectives.
1847 */
Alex Yashchuka3679532017-12-08 17:40:05 +02001848 public final class RetryFlows implements Runnable {
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001849 int attempts = MAX_RETRY_ATTEMPTS;
1850 private Collection<FlowRule> retryFlows;
1851 private ForwardingObjective fwd;
1852
Charles Chan93090352018-03-02 13:26:22 -08001853 RetryFlows(ForwardingObjective fwd, Collection<FlowRule> retryFlows) {
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001854 this.fwd = fwd;
1855 this.retryFlows = retryFlows;
1856 }
1857
1858 @Override
1859 public void run() {
1860 log.info("RETRY FLOWS ATTEMPT# {} for fwd:{} rules:{}",
1861 MAX_RETRY_ATTEMPTS - attempts, fwd.id(), retryFlows.size());
pier5b9b9e32019-03-11 15:14:02 -07001862 sendForwards(Collections.singletonList(Pair.of(fwd, retryFlows)));
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001863 if (--attempts > 0) {
pier5b9b9e32019-03-11 15:14:02 -07001864 retryExecutorService.schedule(this, RETRY_MS, TimeUnit.MILLISECONDS);
1865 }
1866 }
1867 }
1868
1869 // Flow rules accumulator for reducing the number of transactions required to the devices.
1870 private final class ForwardingObjectiveAccumulator
1871 extends AbstractAccumulator<Pair<ForwardingObjective, Collection<FlowRule>>> {
1872
1873 ForwardingObjectiveAccumulator(int maxFwd, int maxBatchMS, int maxIdleMS) {
1874 super(TIMER, maxFwd, maxBatchMS, maxIdleMS);
1875 }
1876
1877 @Override
1878 public void processItems(List<Pair<ForwardingObjective, Collection<FlowRule>>> pairs) {
1879 // Triggers creation of a batch using the list of flowrules generated from fwdobjs.
1880 accumulatorExecutorService.execute(new FlowRulesBuilderTask(pairs));
1881 }
1882 }
1883
1884 // Task for building batch of flow rules in a separate thread.
1885 private final class FlowRulesBuilderTask implements Runnable {
1886 private final List<Pair<ForwardingObjective, Collection<FlowRule>>> pairs;
1887
1888 FlowRulesBuilderTask(List<Pair<ForwardingObjective, Collection<FlowRule>>> pairs) {
1889 this.pairs = pairs;
1890 }
1891
1892 @Override
1893 public void run() {
1894 try {
1895 sendForwards(pairs);
1896 } catch (Exception e) {
1897 log.warn("Unable to send forwards", e);
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001898 }
1899 }
1900 }
1901
Saurav Das822c4e22015-10-23 10:51:11 -07001902}