blob: ad025b4ea1a3c45f54c9ab1c7fa4220b64dcd812 [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;
46import org.onosproject.net.flow.DefaultFlowRule;
47import org.onosproject.net.flow.DefaultTrafficSelector;
48import org.onosproject.net.flow.DefaultTrafficTreatment;
49import org.onosproject.net.flow.FlowRule;
50import org.onosproject.net.flow.FlowRuleOperations;
51import org.onosproject.net.flow.FlowRuleOperationsContext;
52import org.onosproject.net.flow.FlowRuleService;
53import org.onosproject.net.flow.TrafficSelector;
54import org.onosproject.net.flow.TrafficTreatment;
55import org.onosproject.net.flow.criteria.Criteria;
56import org.onosproject.net.flow.criteria.Criterion;
57import org.onosproject.net.flow.criteria.EthCriterion;
58import org.onosproject.net.flow.criteria.EthTypeCriterion;
59import org.onosproject.net.flow.criteria.IPCriterion;
Pier Ventree0ae7a32016-11-23 09:57:42 -080060import org.onosproject.net.flow.criteria.Icmpv6CodeCriterion;
61import org.onosproject.net.flow.criteria.Icmpv6TypeCriterion;
Saurav Das8a0732e2015-11-20 15:27:53 -080062import org.onosproject.net.flow.criteria.MplsBosCriterion;
63import org.onosproject.net.flow.criteria.MplsCriterion;
Saurav Das822c4e22015-10-23 10:51:11 -070064import org.onosproject.net.flow.criteria.PortCriterion;
Charles Chand9e47c62017-10-05 15:17:15 -070065import org.onosproject.net.flow.criteria.TcpPortCriterion;
66import org.onosproject.net.flow.criteria.UdpPortCriterion;
Saurav Das822c4e22015-10-23 10:51:11 -070067import org.onosproject.net.flow.criteria.VlanIdCriterion;
68import org.onosproject.net.flow.instructions.Instruction;
69import org.onosproject.net.flow.instructions.Instructions.OutputInstruction;
Charles Chan3fe71712018-06-15 18:54:18 -070070import org.onosproject.net.flow.instructions.Instructions.NoActionInstruction;
Saurav Das822c4e22015-10-23 10:51:11 -070071import org.onosproject.net.flow.instructions.L2ModificationInstruction;
Saurav Das8a0732e2015-11-20 15:27:53 -080072import org.onosproject.net.flow.instructions.L2ModificationInstruction.L2SubType;
Alex Yashchuka3679532017-12-08 17:40:05 +020073import org.onosproject.net.flow.instructions.L2ModificationInstruction.ModMplsHeaderInstruction;
Charles Chan14967c22015-12-07 11:11:50 -080074import org.onosproject.net.flow.instructions.L3ModificationInstruction;
75import org.onosproject.net.flow.instructions.L3ModificationInstruction.L3SubType;
Saurav Das822c4e22015-10-23 10:51:11 -070076import org.onosproject.net.flowobjective.FilteringObjective;
77import org.onosproject.net.flowobjective.FlowObjectiveStore;
78import org.onosproject.net.flowobjective.ForwardingObjective;
79import org.onosproject.net.flowobjective.NextObjective;
80import org.onosproject.net.flowobjective.Objective;
81import org.onosproject.net.flowobjective.ObjectiveError;
Saurav Das822c4e22015-10-23 10:51:11 -070082import org.onosproject.net.group.DefaultGroupKey;
83import org.onosproject.net.group.Group;
Saurav Das822c4e22015-10-23 10:51:11 -070084import org.onosproject.net.group.GroupKey;
Saurav Das822c4e22015-10-23 10:51:11 -070085import org.onosproject.net.group.GroupService;
Saurav Das822c4e22015-10-23 10:51:11 -070086import org.onosproject.store.serializers.KryoNamespaces;
87import org.slf4j.Logger;
88
Pier Ventree0ae7a32016-11-23 09:57:42 -080089import java.util.ArrayDeque;
90import java.util.ArrayList;
91import java.util.Collection;
92import java.util.Collections;
93import java.util.Deque;
94import java.util.List;
95import java.util.Objects;
pier5b9b9e32019-03-11 15:14:02 -070096import java.util.Timer;
Pier Ventree0ae7a32016-11-23 09:57:42 -080097import java.util.concurrent.ScheduledExecutorService;
98import java.util.concurrent.TimeUnit;
99
100import static java.util.concurrent.Executors.newScheduledThreadPool;
pier5b9b9e32019-03-11 15:14:02 -0700101import static java.util.concurrent.Executors.newSingleThreadScheduledExecutor;
Yi Tseng3a77b4f2017-02-06 15:02:17 -0800102import static org.onlab.packet.MacAddress.BROADCAST;
Charles Chanb4879a52017-10-20 19:09:16 -0700103import static org.onlab.packet.MacAddress.IPV4_MULTICAST;
104import static org.onlab.packet.MacAddress.IPV6_MULTICAST;
Yi Tseng3a77b4f2017-02-06 15:02:17 -0800105import static org.onlab.packet.MacAddress.NONE;
Pier Ventree0ae7a32016-11-23 09:57:42 -0800106import static org.onlab.util.Tools.groupedThreads;
Yi Tsengef19de12017-04-24 11:33:05 -0700107import static org.onosproject.driver.pipeline.ofdpa.OfdpaGroupHandlerUtility.*;
pier5c817582019-04-17 17:05:08 +0200108import static org.onosproject.driver.pipeline.ofdpa.OfdpaPipelineUtility.*;
pier4baa8cc2019-05-10 13:19:15 +0200109import static org.onosproject.net.flowobjective.ForwardingObjective.Flag.SPECIFIC;
Pier Luigi5af88fe2018-02-01 10:23:12 +0100110import static org.onosproject.net.group.GroupDescription.Type.SELECT;
Pier Ventree0ae7a32016-11-23 09:57:42 -0800111import static org.slf4j.LoggerFactory.getLogger;
Pier Ventre140a8942016-11-02 07:26:38 -0700112import static org.onosproject.net.flow.criteria.Criterion.Type.MPLS_BOS;
Pier Ventree0ae7a32016-11-23 09:57:42 -0800113
Saurav Das822c4e22015-10-23 10:51:11 -0700114/**
115 * Driver for Broadcom's OF-DPA v2.0 TTP.
Saurav Das822c4e22015-10-23 10:51:11 -0700116 */
Charles Chan361154b2016-03-24 10:23:39 -0700117public class Ofdpa2Pipeline extends AbstractHandlerBehaviour implements Pipeliner {
pier5b9b9e32019-03-11 15:14:02 -0700118 // Timer for the accumulator
119 private static final Timer TIMER = new Timer("fwdobj-batching");
120 private Accumulator<Pair<ForwardingObjective, Collection<FlowRule>>> accumulator;
pier5c817582019-04-17 17:05:08 +0200121 // Internal objects
Saurav Das822c4e22015-10-23 10:51:11 -0700122 private final Logger log = getLogger(getClass());
Charles Chan425854b2016-04-11 15:32:12 -0700123 protected ServiceDirectory serviceDirectory;
Saurav Das822c4e22015-10-23 10:51:11 -0700124 protected FlowRuleService flowRuleService;
Charles Chan425854b2016-04-11 15:32:12 -0700125 protected CoreService coreService;
Saurav Das8a0732e2015-11-20 15:27:53 -0800126 protected GroupService groupService;
127 protected FlowObjectiveStore flowObjectiveStore;
Saurav Das822c4e22015-10-23 10:51:11 -0700128 protected DeviceId deviceId;
129 protected ApplicationId driverId;
Saurav Das822c4e22015-10-23 10:51:11 -0700130 protected DeviceService deviceService;
Charles Chan188ebf52015-12-23 00:15:11 -0800131 protected static KryoNamespace appKryo = new KryoNamespace.Builder()
Yi Tsengef19de12017-04-24 11:33:05 -0700132 .register(KryoNamespaces.API)
133 .register(GroupKey.class)
134 .register(DefaultGroupKey.class)
135 .register(OfdpaNextGroup.class)
136 .register(ArrayDeque.class)
137 .build("Ofdpa2Pipeline");
Saurav Das822c4e22015-10-23 10:51:11 -0700138
Charles Chan425854b2016-04-11 15:32:12 -0700139 protected Ofdpa2GroupHandler groupHandler;
Saurav Das822c4e22015-10-23 10:51:11 -0700140
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700141 // flows installations to be retried
pier5b9b9e32019-03-11 15:14:02 -0700142 private ScheduledExecutorService retryExecutorService
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700143 = newScheduledThreadPool(5, groupedThreads("OfdpaPipeliner", "retry-%d", log));
Saurav Das4f980082015-11-05 13:39:15 -0800144
pier5b9b9e32019-03-11 15:14:02 -0700145 // accumulator executor service
146 private ScheduledExecutorService accumulatorExecutorService
147 = newSingleThreadScheduledExecutor(groupedThreads("OfdpaPipeliner", "acc-%d", log));
148
Saurav Das822c4e22015-10-23 10:51:11 -0700149 @Override
150 public void init(DeviceId deviceId, PipelinerContext context) {
Saurav Das822c4e22015-10-23 10:51:11 -0700151 this.deviceId = deviceId;
152
Charles Chan425854b2016-04-11 15:32:12 -0700153 serviceDirectory = context.directory();
Saurav Das822c4e22015-10-23 10:51:11 -0700154 coreService = serviceDirectory.get(CoreService.class);
155 flowRuleService = serviceDirectory.get(FlowRuleService.class);
156 groupService = serviceDirectory.get(GroupService.class);
157 flowObjectiveStore = context.store();
Saurav Das822c4e22015-10-23 10:51:11 -0700158 deviceService = serviceDirectory.get(DeviceService.class);
pier5b9b9e32019-03-11 15:14:02 -0700159 // Init the accumulator, if enabled
pier5c817582019-04-17 17:05:08 +0200160 if (isAccumulatorEnabled(this)) {
pier5b9b9e32019-03-11 15:14:02 -0700161 accumulator = new ForwardingObjectiveAccumulator(context.accumulatorMaxObjectives(),
162 context.accumulatorMaxBatchMillis(),
163 context.accumulatorMaxIdleMillis());
164 }
Saurav Das822c4e22015-10-23 10:51:11 -0700165
Charles Chan40132b32017-01-22 00:19:37 -0800166 initDriverId();
167 initGroupHander(context);
Saurav Das822c4e22015-10-23 10:51:11 -0700168
Saurav Das822c4e22015-10-23 10:51:11 -0700169 initializePipeline();
Saurav Das822c4e22015-10-23 10:51:11 -0700170 }
171
pier4baa8cc2019-05-10 13:19:15 +0200172 void setupAccumulatorForTests(int maxFwd, int maxBatchMS, int maxIdleMS) {
173 if (accumulator == null) {
174 accumulator = new ForwardingObjectiveAccumulator(maxFwd,
175 maxBatchMS,
176 maxIdleMS);
177 }
178 }
179
Charles Chan40132b32017-01-22 00:19:37 -0800180 protected void initDriverId() {
181 driverId = coreService.registerApplication(
182 "org.onosproject.driver.Ofdpa2Pipeline");
183 }
184
185 protected void initGroupHander(PipelinerContext context) {
186 groupHandler = new Ofdpa2GroupHandler();
187 groupHandler.init(deviceId, context);
188 }
189
Saurav Das822c4e22015-10-23 10:51:11 -0700190 protected void initializePipeline() {
Charles Chan188ebf52015-12-23 00:15:11 -0800191 // OF-DPA does not require initializing the pipeline as it puts default
192 // rules automatically in the hardware. However emulation of OFDPA in
193 // software switches does require table-miss-entries.
Saurav Das822c4e22015-10-23 10:51:11 -0700194 }
195
Charles Chand1172632017-03-15 17:33:09 -0700196 /**
Alex Yashchuka3679532017-12-08 17:40:05 +0200197 * Determines whether this pipeline requires MPLS POP instruction.
198 *
199 * @return true to use MPLS POP instruction
200 */
201 public boolean requireMplsPop() {
202 return true;
203 }
204
205 /**
206 * Determines whether this pipeline requires MPLS BOS match.
207 *
208 * @return true to use MPLS BOS match
209 */
210 public boolean requireMplsBosMatch() {
211 return true;
212 }
213
214 /**
215 * Determines whether this pipeline requires MPLS TTL decrement and copy.
216 *
217 * @return true to use MPLS TTL decrement and copy
218 */
219 public boolean requireMplsTtlModification() {
220 return true;
221 }
222
223 /**
Charles Chand1172632017-03-15 17:33:09 -0700224 * Determines whether this pipeline requires OFDPA match and set VLAN extensions.
225 *
226 * @return true to use the extensions
227 */
228 protected boolean requireVlanExtensions() {
229 return true;
230 }
231
Saurav Das86d13e82017-04-28 17:03:48 -0700232 /**
233 * Determines whether in-port should be matched on in TMAC table rules.
234 *
235 * @return true if match on in-port should be programmed
236 */
237 protected boolean matchInPortTmacTable() {
238 return true;
239 }
240
Charles Chand9e47c62017-10-05 15:17:15 -0700241 /**
242 * Determines whether matching L4 destination port on IPv6 packets is supported in ACL table.
243 *
244 * @return true if matching L4 destination port on IPv6 packets is supported in ACL table.
245 */
246 protected boolean supportIpv6L4Dst() {
247 return true;
248 }
249
Saurav Dasf3f75942018-01-25 09:49:01 -0800250 /**
251 * Determines whether this driver should continue to retry flows that point
252 * to empty groups. See CORD-554.
253 *
254 * @return true if the driver should retry flows
255 */
256 protected boolean shouldRetry() {
257 return true;
258 }
259
Charles Chand57552d2018-03-02 15:41:41 -0800260 /**
261 * Determines whether this driver requires unicast flow to be installed before multicast flow
262 * in TMAC table.
263 *
264 * @return true if required
265 */
266 protected boolean requireUnicastBeforeMulticast() {
267 return false;
268 }
269
Andrea Campanella84ac4df2018-04-30 11:48:55 +0200270 /**
271 * Determines whether this driver supports installing a clearDeferred action on table 30.
272 *
273 * @return true if required
274 */
275 protected boolean supportsUnicastBlackHole() {
276 return true;
277 }
278
Saurav Das822c4e22015-10-23 10:51:11 -0700279 //////////////////////////////////////
280 // Flow Objectives
281 //////////////////////////////////////
282
283 @Override
284 public void filter(FilteringObjective filteringObjective) {
285 if (filteringObjective.type() == FilteringObjective.Type.PERMIT) {
286 processFilter(filteringObjective,
287 filteringObjective.op() == Objective.Operation.ADD,
288 filteringObjective.appId());
289 } else {
290 // Note that packets that don't match the PERMIT filter are
291 // automatically denied. The DENY filter is used to deny packets
292 // that are otherwise permitted by the PERMIT filter.
293 // Use ACL table flow rules here for DENY filtering objectives
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530294 log.warn("filter objective other than PERMIT currently not supported");
Saurav Das822c4e22015-10-23 10:51:11 -0700295 fail(filteringObjective, ObjectiveError.UNSUPPORTED);
296 }
297 }
298
299 @Override
300 public void forward(ForwardingObjective fwd) {
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700301 Collection<FlowRule> rules = processForward(fwd);
Saurav Das25190812016-05-27 13:54:07 -0700302 if (rules == null || rules.isEmpty()) {
303 // Assumes fail message has already been generated to the objective
304 // context. Returning here prevents spurious pass message to be
305 // generated by FlowRule service for empty flowOps.
306 return;
307 }
pier4baa8cc2019-05-10 13:19:15 +0200308 // Let's accumulate flow rules if accumulator is active and fwd objective is not versatile.
309 // Otherwise send directly, without adding futher delay
310 if (accumulator != null && Objects.equals(fwd.flag(), SPECIFIC)) {
pier5b9b9e32019-03-11 15:14:02 -0700311 accumulator.add(Pair.of(fwd, rules));
312 } else {
313 sendForwards(Collections.singletonList(Pair.of(fwd, rules)));
314 }
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700315 }
316
pier5b9b9e32019-03-11 15:14:02 -0700317 // Builds the batch using the accumulated flow rules
318 private void sendForwards(List<Pair<ForwardingObjective, Collection<FlowRule>>> pairs) {
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700319 FlowRuleOperations.Builder flowOpsBuilder = FlowRuleOperations.builder();
pier5b9b9e32019-03-11 15:14:02 -0700320 log.debug("Sending {} fwd-objs", pairs.size());
321 List<Objective> fwdObjs = Lists.newArrayList();
322 // Iterates over all accumulated flow rules and then build an unique batch
323 pairs.forEach(pair -> {
324 ForwardingObjective fwd = pair.getLeft();
325 Collection<FlowRule> rules = pair.getRight();
326 switch (fwd.op()) {
327 case ADD:
328 rules.stream()
329 .filter(Objects::nonNull)
330 .forEach(flowOpsBuilder::add);
331 log.debug("Applying a add fwd-obj {} to sw:{}", fwd.id(), deviceId);
332 fwdObjs.add(fwd);
333 break;
334 case REMOVE:
335 rules.stream()
336 .filter(Objects::nonNull)
337 .forEach(flowOpsBuilder::remove);
338 log.debug("Deleting a flow rule to sw:{}", deviceId);
339 fwdObjs.add(fwd);
340 break;
341 default:
342 fail(fwd, ObjectiveError.UNKNOWN);
343 log.warn("Unknown forwarding type {}", fwd.op());
344 }
345 });
346 // Finally applies the operations
Saurav Das822c4e22015-10-23 10:51:11 -0700347 flowRuleService.apply(flowOpsBuilder.build(new FlowRuleOperationsContext() {
pier5b9b9e32019-03-11 15:14:02 -0700348
Saurav Das822c4e22015-10-23 10:51:11 -0700349 @Override
350 public void onSuccess(FlowRuleOperations ops) {
pier5b9b9e32019-03-11 15:14:02 -0700351 log.trace("Flow rule operations onSuccess {}", ops);
pier5c817582019-04-17 17:05:08 +0200352 fwdObjs.forEach(OfdpaPipelineUtility::pass);
Saurav Das822c4e22015-10-23 10:51:11 -0700353 }
354
355 @Override
356 public void onError(FlowRuleOperations ops) {
pier5b9b9e32019-03-11 15:14:02 -0700357 ObjectiveError error = ObjectiveError.FLOWINSTALLATIONFAILED;
358 log.warn("Flow rule operations onError {}. Reason = {}", ops, error);
359 fwdObjs.forEach(fwdObj -> fail(fwdObj, error));
Saurav Das822c4e22015-10-23 10:51:11 -0700360 }
361 }));
Saurav Das822c4e22015-10-23 10:51:11 -0700362 }
363
364 @Override
365 public void next(NextObjective nextObjective) {
Saurav Das8a0732e2015-11-20 15:27:53 -0800366 NextGroup nextGroup = flowObjectiveStore.getNextGroup(nextObjective.id());
367 switch (nextObjective.op()) {
368 case ADD:
Saurav Das4f980082015-11-05 13:39:15 -0800369 if (nextGroup != null) {
Saurav Das8a0732e2015-11-20 15:27:53 -0800370 log.warn("Cannot add next {} that already exists in device {}",
371 nextObjective.id(), deviceId);
372 return;
373 }
Saurav Das1547b3f2017-05-05 17:01:08 -0700374 log.debug("Processing NextObjective id {} in dev {} - add group",
Saurav Das8a0732e2015-11-20 15:27:53 -0800375 nextObjective.id(), deviceId);
Charles Chan425854b2016-04-11 15:32:12 -0700376 groupHandler.addGroup(nextObjective);
Saurav Das8a0732e2015-11-20 15:27:53 -0800377 break;
378 case ADD_TO_EXISTING:
379 if (nextGroup != null) {
Saurav Das1547b3f2017-05-05 17:01:08 -0700380 log.debug("Processing NextObjective id {} in dev {} - add bucket",
Saurav Das8a0732e2015-11-20 15:27:53 -0800381 nextObjective.id(), deviceId);
Charles Chan425854b2016-04-11 15:32:12 -0700382 groupHandler.addBucketToGroup(nextObjective, nextGroup);
Saurav Das4f980082015-11-05 13:39:15 -0800383 } else {
Saurav Das8a0732e2015-11-20 15:27:53 -0800384 // it is possible that group-chain has not been fully created yet
Saurav Das423fe2b2015-12-04 10:52:59 -0800385 log.debug("Waiting to add bucket to group for next-id:{} in dev:{}",
386 nextObjective.id(), deviceId);
Yi Tseng47f82dc2017-03-05 22:48:39 -0800387
388 // by design multiple pending bucket is allowed for the group
389 groupHandler.pendingBuckets.compute(nextObjective.id(), (nextId, pendBkts) -> {
390 if (pendBkts == null) {
391 pendBkts = Sets.newHashSet();
392 }
393 pendBkts.add(nextObjective);
394 return pendBkts;
395 });
Saurav Das4f980082015-11-05 13:39:15 -0800396 }
Saurav Das8a0732e2015-11-20 15:27:53 -0800397 break;
398 case REMOVE:
399 if (nextGroup == null) {
400 log.warn("Cannot remove next {} that does not exist in device {}",
401 nextObjective.id(), deviceId);
402 return;
403 }
Saurav Das1547b3f2017-05-05 17:01:08 -0700404 log.debug("Processing NextObjective id {} in dev {} - remove group",
Saurav Das8a0732e2015-11-20 15:27:53 -0800405 nextObjective.id(), deviceId);
Charles Chan425854b2016-04-11 15:32:12 -0700406 groupHandler.removeGroup(nextObjective, nextGroup);
Saurav Das8a0732e2015-11-20 15:27:53 -0800407 break;
408 case REMOVE_FROM_EXISTING:
409 if (nextGroup == null) {
410 log.warn("Cannot remove from next {} that does not exist in device {}",
411 nextObjective.id(), deviceId);
412 return;
413 }
Saurav Das1547b3f2017-05-05 17:01:08 -0700414 log.debug("Processing NextObjective id {} in dev {} - remove bucket",
Saurav Das8a0732e2015-11-20 15:27:53 -0800415 nextObjective.id(), deviceId);
Charles Chan425854b2016-04-11 15:32:12 -0700416 groupHandler.removeBucketFromGroup(nextObjective, nextGroup);
Saurav Das8a0732e2015-11-20 15:27:53 -0800417 break;
Jonghwan Hyun25199f22018-02-12 16:43:45 +0900418 case MODIFY:
419 if (nextGroup == null) {
420 log.warn("Cannot modify next {} that does not exist in device {}",
421 nextObjective.id(), deviceId);
422 return;
423 }
Ruchi Sahotaf77dc0f2019-01-28 01:08:18 +0000424 log.debug("Processing NextObjective id {} in dev {} group {} - modify bucket",
425 nextObjective.id(), deviceId, nextGroup);
Jonghwan Hyun25199f22018-02-12 16:43:45 +0900426 groupHandler.modifyBucketFromGroup(nextObjective, nextGroup);
427 break;
Saurav Dasceccf242017-08-03 18:30:35 -0700428 case VERIFY:
429 if (nextGroup == null) {
430 log.warn("Cannot verify next {} that does not exist in device {}",
431 nextObjective.id(), deviceId);
432 return;
433 }
434 log.debug("Processing NextObjective id {} in dev {} - verify",
435 nextObjective.id(), deviceId);
436 groupHandler.verifyGroup(nextObjective, nextGroup);
437 break;
Saurav Das8a0732e2015-11-20 15:27:53 -0800438 default:
Saurav Das4f980082015-11-05 13:39:15 -0800439 log.warn("Unsupported operation {}", nextObjective.op());
Saurav Das822c4e22015-10-23 10:51:11 -0700440 }
441 }
442
443 //////////////////////////////////////
444 // Flow handling
445 //////////////////////////////////////
446
447 /**
Yi Tseng0fdc07e2017-04-24 19:17:08 -0700448 * As per OFDPA 2.0 TTP, filtering of VLAN ids and MAC addresses (for routing)
449 * configured on switch ports happen in different tables.
Saurav Das822c4e22015-10-23 10:51:11 -0700450 *
451 * @param filt the filtering objective
452 * @param install indicates whether to add or remove the objective
453 * @param applicationId the application that sent this objective
454 */
Saurav Das52025962016-01-28 22:30:01 -0800455 protected void processFilter(FilteringObjective filt,
456 boolean install, ApplicationId applicationId) {
Saurav Das822c4e22015-10-23 10:51:11 -0700457 // This driver only processes filtering criteria defined with switch
458 // ports as the key
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530459 PortCriterion portCriterion = null;
Saurav Das822c4e22015-10-23 10:51:11 -0700460 EthCriterion ethCriterion = null;
461 VlanIdCriterion vidCriterion = null;
Saurav Das822c4e22015-10-23 10:51:11 -0700462 if (!filt.key().equals(Criteria.dummy()) &&
463 filt.key().type() == Criterion.Type.IN_PORT) {
464 portCriterion = (PortCriterion) filt.key();
Saurav Das822c4e22015-10-23 10:51:11 -0700465 }
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530466 if (portCriterion == null) {
467 log.debug("No IN_PORT defined in filtering objective from app: {}",
468 applicationId);
469 } else {
470 log.debug("Received filtering objective for dev/port: {}/{}", deviceId,
471 portCriterion.port());
472 }
Saurav Das822c4e22015-10-23 10:51:11 -0700473 // convert filtering conditions for switch-intfs into flowrules
474 FlowRuleOperations.Builder ops = FlowRuleOperations.builder();
475 for (Criterion criterion : filt.conditions()) {
Yi Tseng0fdc07e2017-04-24 19:17:08 -0700476 switch (criterion.type()) {
477 case ETH_DST:
478 case ETH_DST_MASKED:
479 ethCriterion = (EthCriterion) criterion;
480 break;
481 case VLAN_VID:
482 vidCriterion = (VlanIdCriterion) criterion;
483 break;
484 default:
485 log.warn("Unsupported filter {}", criterion);
486 fail(filt, ObjectiveError.UNSUPPORTED);
487 return;
Saurav Das822c4e22015-10-23 10:51:11 -0700488 }
489 }
490
Saurav Das0e99e2b2015-10-28 12:39:42 -0700491 VlanId assignedVlan = null;
Charles Chane849c192016-01-11 18:28:54 -0800492 if (vidCriterion != null) {
Charles Chand55e84d2016-03-30 17:54:24 -0700493 // Use the VLAN in criterion if metadata is not present and the traffic is tagged
Charles Chan9174e6b2017-12-19 19:55:57 -0800494 if (!vidCriterion.vlanId().equals(VlanId.NONE)) {
Charles Chane849c192016-01-11 18:28:54 -0800495 assignedVlan = vidCriterion.vlanId();
Pier3cc7e662018-03-07 11:42:50 +0100496 }
497 // If the meta VLAN is present let's update the assigned vlan
498 if (filt.meta() != null) {
499 VlanId metaVlan = readVlanFromTreatment(filt.meta());
500 if (metaVlan != null) {
501 assignedVlan = metaVlan;
502 }
Charles Chand55e84d2016-03-30 17:54:24 -0700503 }
Charles Chane849c192016-01-11 18:28:54 -0800504
Charles Chand55e84d2016-03-30 17:54:24 -0700505 if (assignedVlan == null) {
506 log.error("Driver fails to extract VLAN information. "
Frank Wangd8ab0962017-08-11 11:09:30 +0800507 + "Not processing VLAN filters on device {}.", deviceId);
Charles Chand55e84d2016-03-30 17:54:24 -0700508 log.debug("VLAN ID in criterion={}, metadata={}",
509 readVlanFromTreatment(filt.meta()), vidCriterion.vlanId());
510 fail(filt, ObjectiveError.BADPARAMS);
511 return;
Saurav Das0e99e2b2015-10-28 12:39:42 -0700512 }
513 }
514
Yi Tseng3a77b4f2017-02-06 15:02:17 -0800515 if (ethCriterion == null || ethCriterion.mac().equals(NONE)) {
Charles Chan985b12e2016-05-11 19:47:22 -0700516 // NOTE: it is possible that a filtering objective only has vidCriterion
Daniel Ginsburg16e5ed42018-04-30 19:27:19 -0400517 log.debug("filtering objective missing dstMac, won't program TMAC table");
Saurav Das822c4e22015-10-23 10:51:11 -0700518 } else {
Charles Chand57552d2018-03-02 15:41:41 -0800519 MacAddress unicastMac = readEthDstFromTreatment(filt.meta());
Charles Chan206506b2018-03-02 16:43:28 -0800520 List<List<FlowRule>> allStages = processEthDstFilter(portCriterion, ethCriterion,
Charles Chand57552d2018-03-02 15:41:41 -0800521 vidCriterion, assignedVlan, unicastMac, applicationId);
Charles Chan206506b2018-03-02 16:43:28 -0800522 for (List<FlowRule> flowRules : allStages) {
Charles Chand57552d2018-03-02 15:41:41 -0800523 log.trace("Starting a new flow rule stage for TMAC table flow");
Charles Chan206506b2018-03-02 16:43:28 -0800524 ops.newStage();
Charles Chan9174e6b2017-12-19 19:55:57 -0800525
Charles Chan206506b2018-03-02 16:43:28 -0800526 for (FlowRule flowRule : flowRules) {
Charles Chan893a1d72018-05-10 22:19:25 +0000527 log.trace("{} flow rules in TMAC table: {} for dev: {}",
Charles Chan206506b2018-03-02 16:43:28 -0800528 (install) ? "adding" : "removing", flowRules, deviceId);
Charles Chan206506b2018-03-02 16:43:28 -0800529 if (install) {
530 ops = ops.add(flowRule);
Charles Chan9174e6b2017-12-19 19:55:57 -0800531 } else {
Charles Chan206506b2018-03-02 16:43:28 -0800532 // NOTE: Only remove TMAC flow when there is no more enabled port within the
533 // same VLAN on this device if TMAC doesn't support matching on in_port.
534 if (matchInPortTmacTable() || (filt.meta() != null && filt.meta().clearedDeferred())) {
535 ops = ops.remove(flowRule);
536 } else {
537 log.debug("Abort TMAC flow removal on {}. Some other ports still share this TMAC flow");
538 }
Charles Chan9174e6b2017-12-19 19:55:57 -0800539 }
540 }
Saurav Das822c4e22015-10-23 10:51:11 -0700541 }
542 }
543
Charles Chan985b12e2016-05-11 19:47:22 -0700544 if (vidCriterion == null) {
545 // NOTE: it is possible that a filtering objective only has ethCriterion
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530546 log.info("filtering objective missing VLAN, cannot program VLAN Table");
Saurav Das822c4e22015-10-23 10:51:11 -0700547 } else {
Charles Chan206506b2018-03-02 16:43:28 -0800548 List<List<FlowRule>> allStages = processVlanIdFilter(
pierb2f71142019-06-10 17:10:26 +0200549 portCriterion, vidCriterion, assignedVlan, applicationId, install);
Charles Chan206506b2018-03-02 16:43:28 -0800550 for (List<FlowRule> flowRules : allStages) {
Charles Chand57552d2018-03-02 15:41:41 -0800551 log.trace("Starting a new flow rule stage for VLAN table flow");
Charles Chan206506b2018-03-02 16:43:28 -0800552 ops.newStage();
Charles Chan14967c22015-12-07 11:11:50 -0800553
Charles Chan206506b2018-03-02 16:43:28 -0800554 for (FlowRule flowRule : flowRules) {
555 log.trace("{} flow rules in VLAN table: {} for dev: {}",
556 (install) ? "adding" : "removing", flowRule, deviceId);
557 ops = install ? ops.add(flowRule) : ops.remove(flowRule);
Charles Chand1172632017-03-15 17:33:09 -0700558 }
Saurav Das822c4e22015-10-23 10:51:11 -0700559 }
560 }
561
Saurav Das822c4e22015-10-23 10:51:11 -0700562 // apply filtering flow rules
563 flowRuleService.apply(ops.build(new FlowRuleOperationsContext() {
564 @Override
565 public void onSuccess(FlowRuleOperations ops) {
Saurav Das018605f2017-02-18 14:05:44 -0800566 log.debug("Applied {} filtering rules in device {}",
Saurav Das822c4e22015-10-23 10:51:11 -0700567 ops.stages().get(0).size(), deviceId);
568 pass(filt);
569 }
570
571 @Override
572 public void onError(FlowRuleOperations ops) {
573 log.info("Failed to apply all filtering rules in dev {}", deviceId);
574 fail(filt, ObjectiveError.FLOWINSTALLATIONFAILED);
575 }
576 }));
577
578 }
579
580 /**
Charles Chand1172632017-03-15 17:33:09 -0700581 * Internal implementation of processVlanIdFilter.
582 * <p>
583 * The is_present bit in set_vlan_vid action is required to be 0 in OFDPA i12.
584 * Since it is non-OF spec, we need an extension treatment for that.
585 * The useVlanExtension must be set to false for OFDPA i12.
586 * </p>
Charles Chan206506b2018-03-02 16:43:28 -0800587 * <p>
588 * NOTE: Separate VLAN filtering rules and assignment rules
589 * into different stages in order to guarantee that filtering rules
590 * always go first, as required by OFDPA.
591 * </p>
Saurav Das0e99e2b2015-10-28 12:39:42 -0700592 *
Charles Chanf9e98652016-09-07 16:54:23 -0700593 * @param portCriterion port on device for which this filter is programmed
594 * @param vidCriterion vlan assigned to port, or NONE for untagged
595 * @param assignedVlan assigned vlan-id for untagged packets
596 * @param applicationId for application programming this filter
pierb2f71142019-06-10 17:10:26 +0200597 * @param install indicates whether to add or remove the objective
Charles Chan206506b2018-03-02 16:43:28 -0800598 * @return stages of flow rules for port-vlan filters
Saurav Das822c4e22015-10-23 10:51:11 -0700599 */
Charles Chan206506b2018-03-02 16:43:28 -0800600 protected List<List<FlowRule>> processVlanIdFilter(PortCriterion portCriterion,
pierb2f71142019-06-10 17:10:26 +0200601 VlanIdCriterion vidCriterion,
602 VlanId assignedVlan,
603 ApplicationId applicationId,
604 boolean install) {
Charles Chan206506b2018-03-02 16:43:28 -0800605 List<FlowRule> filteringRules = new ArrayList<>();
606 List<FlowRule> assignmentRules = new ArrayList<>();
Saurav Das822c4e22015-10-23 10:51:11 -0700607 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
608 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
Charles Chan14967c22015-12-07 11:11:50 -0800609 TrafficSelector.Builder preSelector = null;
610 TrafficTreatment.Builder preTreatment = null;
611
Saurav Das4f980082015-11-05 13:39:15 -0800612 treatment.transition(TMAC_TABLE);
613
Saurav Das822c4e22015-10-23 10:51:11 -0700614 if (vidCriterion.vlanId() == VlanId.NONE) {
615 // untagged packets are assigned vlans
Charles Chand1172632017-03-15 17:33:09 -0700616 preSelector = DefaultTrafficSelector.builder();
617 if (requireVlanExtensions()) {
618 OfdpaMatchVlanVid ofdpaMatchVlanVid = new OfdpaMatchVlanVid(VlanId.NONE);
619 selector.extension(ofdpaMatchVlanVid, deviceId);
Charles Chanf9e98652016-09-07 16:54:23 -0700620 OfdpaSetVlanVid ofdpaSetVlanVid = new OfdpaSetVlanVid(assignedVlan);
621 treatment.extension(ofdpaSetVlanVid, deviceId);
Charles Chand1172632017-03-15 17:33:09 -0700622
623 OfdpaMatchVlanVid preOfdpaMatchVlanVid = new OfdpaMatchVlanVid(assignedVlan);
624 preSelector.extension(preOfdpaMatchVlanVid, deviceId);
Charles Chanf9e98652016-09-07 16:54:23 -0700625 } else {
Charles Chand1172632017-03-15 17:33:09 -0700626 selector.matchVlanId(VlanId.NONE);
Charles Chanf9e98652016-09-07 16:54:23 -0700627 treatment.setVlanId(assignedVlan);
Charles Chand1172632017-03-15 17:33:09 -0700628
629 preSelector.matchVlanId(assignedVlan);
630 }
631 preTreatment = DefaultTrafficTreatment.builder().transition(TMAC_TABLE);
632 } else {
633 if (requireVlanExtensions()) {
634 OfdpaMatchVlanVid ofdpaMatchVlanVid = new OfdpaMatchVlanVid(vidCriterion.vlanId());
635 selector.extension(ofdpaMatchVlanVid, deviceId);
636 } else {
637 selector.matchVlanId(vidCriterion.vlanId());
Charles Chanf9e98652016-09-07 16:54:23 -0700638 }
Charles Chan14967c22015-12-07 11:11:50 -0800639
Charles Chand55e84d2016-03-30 17:54:24 -0700640 if (!assignedVlan.equals(vidCriterion.vlanId())) {
Charles Chand1172632017-03-15 17:33:09 -0700641 if (requireVlanExtensions()) {
Charles Chanc03782d2017-01-31 13:57:55 -0800642 OfdpaSetVlanVid ofdpaSetVlanVid = new OfdpaSetVlanVid(assignedVlan);
643 treatment.extension(ofdpaSetVlanVid, deviceId);
644 } else {
645 treatment.setVlanId(assignedVlan);
646 }
Charles Chand55e84d2016-03-30 17:54:24 -0700647 }
Saurav Das822c4e22015-10-23 10:51:11 -0700648 }
Saurav Das822c4e22015-10-23 10:51:11 -0700649
650 // ofdpa cannot match on ALL portnumber, so we need to use separate
651 // rules for each port.
Charles Chan14967c22015-12-07 11:11:50 -0800652 List<PortNumber> portnums = new ArrayList<>();
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530653 if (portCriterion != null) {
654 if (PortNumber.ALL.equals(portCriterion.port())) {
655 for (Port port : deviceService.getPorts(deviceId)) {
656 if (port.number().toLong() > 0 && port.number().toLong() < OFPP_MAX) {
657 portnums.add(port.number());
658 }
Saurav Das822c4e22015-10-23 10:51:11 -0700659 }
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530660 } else {
661 portnums.add(portCriterion.port());
Saurav Das822c4e22015-10-23 10:51:11 -0700662 }
663 } else {
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530664 log.warn("Filtering Objective missing Port Criterion . " +
Charles Chan206506b2018-03-02 16:43:28 -0800665 "VLAN Table cannot be programmed for {}", deviceId);
Saurav Das822c4e22015-10-23 10:51:11 -0700666 }
Saurav Das4f980082015-11-05 13:39:15 -0800667
Saurav Das822c4e22015-10-23 10:51:11 -0700668 for (PortNumber pnum : portnums) {
Saurav Das4f980082015-11-05 13:39:15 -0800669 // create rest of flowrule
Saurav Das822c4e22015-10-23 10:51:11 -0700670 selector.matchInPort(pnum);
671 FlowRule rule = DefaultFlowRule.builder()
672 .forDevice(deviceId)
673 .withSelector(selector.build())
674 .withTreatment(treatment.build())
675 .withPriority(DEFAULT_PRIORITY)
676 .fromApp(applicationId)
677 .makePermanent()
678 .forTable(VLAN_TABLE).build();
Charles Chan206506b2018-03-02 16:43:28 -0800679 assignmentRules.add(rule);
Charles Chan14967c22015-12-07 11:11:50 -0800680
681 if (preSelector != null) {
682 preSelector.matchInPort(pnum);
683 FlowRule preRule = DefaultFlowRule.builder()
684 .forDevice(deviceId)
685 .withSelector(preSelector.build())
686 .withTreatment(preTreatment.build())
687 .withPriority(DEFAULT_PRIORITY)
688 .fromApp(applicationId)
689 .makePermanent()
690 .forTable(VLAN_TABLE).build();
Charles Chan206506b2018-03-02 16:43:28 -0800691 filteringRules.add(preRule);
Charles Chan14967c22015-12-07 11:11:50 -0800692 }
Saurav Das822c4e22015-10-23 10:51:11 -0700693 }
pierb2f71142019-06-10 17:10:26 +0200694 return install ? ImmutableList.of(filteringRules, assignmentRules) :
695 ImmutableList.of(assignmentRules, filteringRules);
Saurav Das822c4e22015-10-23 10:51:11 -0700696 }
697
698 /**
699 * Allows routed packets with correct destination MAC to be directed
Charles Chand57552d2018-03-02 15:41:41 -0800700 * to unicast routing table, multicast routing table or MPLS forwarding table.
Saurav Das822c4e22015-10-23 10:51:11 -0700701 *
702 * @param portCriterion port on device for which this filter is programmed
703 * @param ethCriterion dstMac of device for which is filter is programmed
704 * @param vidCriterion vlan assigned to port, or NONE for untagged
Saurav Das0e99e2b2015-10-28 12:39:42 -0700705 * @param assignedVlan assigned vlan-id for untagged packets
Charles Chand57552d2018-03-02 15:41:41 -0800706 * @param unicastMac some switches require a unicast TMAC flow to be programmed before multicast
707 * TMAC flow. This MAC address will be used for the unicast TMAC flow.
708 * This is unused if the filtering objective is a unicast.
Saurav Das822c4e22015-10-23 10:51:11 -0700709 * @param applicationId for application programming this filter
Charles Chan206506b2018-03-02 16:43:28 -0800710 * @return stages of flow rules for port-vlan filters
Saurav Das822c4e22015-10-23 10:51:11 -0700711
712 */
Charles Chan206506b2018-03-02 16:43:28 -0800713 protected List<List<FlowRule>> processEthDstFilter(PortCriterion portCriterion,
Saurav Das822c4e22015-10-23 10:51:11 -0700714 EthCriterion ethCriterion,
715 VlanIdCriterion vidCriterion,
Saurav Das0e99e2b2015-10-28 12:39:42 -0700716 VlanId assignedVlan,
Charles Chand57552d2018-03-02 15:41:41 -0800717 MacAddress unicastMac,
Saurav Das822c4e22015-10-23 10:51:11 -0700718 ApplicationId applicationId) {
Charles Chan5270ed02016-01-30 23:22:37 -0800719 // Consider PortNumber.ANY as wildcard. Match ETH_DST only
Sivachidambaram Subramanianc598d062017-05-16 22:42:06 +0530720 if (portCriterion != null && PortNumber.ANY.equals(portCriterion.port())) {
Charles Chan5270ed02016-01-30 23:22:37 -0800721 return processEthDstOnlyFilter(ethCriterion, applicationId);
722 }
723
Charles Chan5b9df8d2016-03-28 22:21:40 -0700724 // Multicast MAC
725 if (ethCriterion.mask() != null) {
Charles Chand57552d2018-03-02 15:41:41 -0800726 return processMcastEthDstFilter(ethCriterion, assignedVlan, unicastMac, applicationId);
Charles Chan5b9df8d2016-03-28 22:21:40 -0700727 }
728
Saurav Das822c4e22015-10-23 10:51:11 -0700729 //handling untagged packets via assigned VLAN
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530730 if (vidCriterion != null && vidCriterion.vlanId() == VlanId.NONE) {
Saurav Das0e99e2b2015-10-28 12:39:42 -0700731 vidCriterion = (VlanIdCriterion) Criteria.matchVlanId(assignedVlan);
Saurav Das822c4e22015-10-23 10:51:11 -0700732 }
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530733 List<FlowRule> rules = new ArrayList<>();
734 OfdpaMatchVlanVid ofdpaMatchVlanVid = null;
735 if (vidCriterion != null && requireVlanExtensions()) {
736 ofdpaMatchVlanVid = new OfdpaMatchVlanVid(vidCriterion.vlanId());
737 }
Saurav Das822c4e22015-10-23 10:51:11 -0700738 // ofdpa cannot match on ALL portnumber, so we need to use separate
739 // rules for each port.
Yuta HIGUCHI2dce08a2017-04-20 21:57:48 -0700740 List<PortNumber> portnums = new ArrayList<>();
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530741 if (portCriterion != null) {
742 if (PortNumber.ALL.equals(portCriterion.port())) {
743 for (Port port : deviceService.getPorts(deviceId)) {
744 if (port.number().toLong() > 0 && port.number().toLong() < OFPP_MAX) {
745 portnums.add(port.number());
746 }
Saurav Das822c4e22015-10-23 10:51:11 -0700747 }
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530748 } else {
749 portnums.add(portCriterion.port());
750 }
751 for (PortNumber pnum : portnums) {
752 rules.add(buildTmacRuleForIpv4(ethCriterion,
753 vidCriterion,
754 ofdpaMatchVlanVid,
755 applicationId,
756 pnum));
757 rules.add(buildTmacRuleForMpls(ethCriterion,
758 vidCriterion,
759 ofdpaMatchVlanVid,
760 applicationId,
761 pnum));
762 rules.add(buildTmacRuleForIpv6(ethCriterion,
763 vidCriterion,
764 ofdpaMatchVlanVid,
765 applicationId,
766 pnum));
Saurav Das822c4e22015-10-23 10:51:11 -0700767 }
768 } else {
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530769 rules.add(buildTmacRuleForIpv4(ethCriterion,
770 vidCriterion,
771 ofdpaMatchVlanVid,
772 applicationId,
773 null));
774 rules.add(buildTmacRuleForMpls(ethCriterion,
775 vidCriterion,
776 ofdpaMatchVlanVid,
777 applicationId,
778 null));
779 rules.add(buildTmacRuleForIpv6(ethCriterion,
780 vidCriterion,
781 ofdpaMatchVlanVid,
782 applicationId,
783 null));
Saurav Das822c4e22015-10-23 10:51:11 -0700784 }
Charles Chan206506b2018-03-02 16:43:28 -0800785 return ImmutableList.of(rules);
Saurav Das822c4e22015-10-23 10:51:11 -0700786 }
787
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530788 /**
789 * Builds TMAC rules for IPv4 packets.
790 *
Charles Chan9174e6b2017-12-19 19:55:57 -0800791 * @param ethCriterion dst mac matching
792 * @param vidCriterion vlan id assigned to the port
793 * @param ofdpaMatchVlanVid OFDPA vlan id matching
794 * @param applicationId application id
795 * @param pnum port number
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530796 * @return TMAC rule for IPV4 packets
797 */
798 private FlowRule buildTmacRuleForIpv4(EthCriterion ethCriterion,
799 VlanIdCriterion vidCriterion,
800 OfdpaMatchVlanVid ofdpaMatchVlanVid,
801 ApplicationId applicationId,
802 PortNumber pnum) {
803 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
804 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
805 if (pnum != null) {
806 if (matchInPortTmacTable()) {
807 selector.matchInPort(pnum);
808 } else {
Saurav Das73a6cc12017-12-05 15:00:23 -0800809 log.debug("Pipeline does not support IN_PORT matching in TMAC table, " +
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530810 "ignoring the IN_PORT criteria");
811 }
812 }
813 if (vidCriterion != null) {
814 if (requireVlanExtensions()) {
815 selector.extension(ofdpaMatchVlanVid, deviceId);
816 } else {
817 selector.matchVlanId(vidCriterion.vlanId());
818 }
819 }
820 selector.matchEthType(Ethernet.TYPE_IPV4);
821 selector.matchEthDst(ethCriterion.mac());
822 treatment.transition(UNICAST_ROUTING_TABLE);
823 return DefaultFlowRule.builder()
824 .forDevice(deviceId)
825 .withSelector(selector.build())
826 .withTreatment(treatment.build())
827 .withPriority(DEFAULT_PRIORITY)
828 .fromApp(applicationId)
829 .makePermanent()
830 .forTable(TMAC_TABLE).build();
831 }
832
833 /**
834 * Builds TMAC rule for MPLS packets.
835 *
Charles Chan9174e6b2017-12-19 19:55:57 -0800836 * @param ethCriterion dst mac matching
837 * @param vidCriterion vlan id assigned to the port
838 * @param ofdpaMatchVlanVid OFDPA vlan id matching
839 * @param applicationId application id
840 * @param pnum port number
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530841 * @return TMAC rule for MPLS packets
842 */
843 private FlowRule buildTmacRuleForMpls(EthCriterion ethCriterion,
844 VlanIdCriterion vidCriterion,
845 OfdpaMatchVlanVid ofdpaMatchVlanVid,
846 ApplicationId applicationId,
847 PortNumber pnum) {
848 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
849 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
850 if (pnum != null) {
851 if (matchInPortTmacTable()) {
852 selector.matchInPort(pnum);
853 } else {
Saurav Das73a6cc12017-12-05 15:00:23 -0800854 log.debug("Pipeline does not support IN_PORT matching in TMAC table, " +
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530855 "ignoring the IN_PORT criteria");
856 }
857 }
858 if (vidCriterion != null) {
859 if (requireVlanExtensions()) {
860 selector.extension(ofdpaMatchVlanVid, deviceId);
861 } else {
862 selector.matchVlanId(vidCriterion.vlanId());
863 }
864 }
865 selector.matchEthType(Ethernet.MPLS_UNICAST);
866 selector.matchEthDst(ethCriterion.mac());
867 treatment.transition(MPLS_TABLE_0);
868 return DefaultFlowRule.builder()
869 .forDevice(deviceId)
870 .withSelector(selector.build())
871 .withTreatment(treatment.build())
872 .withPriority(DEFAULT_PRIORITY)
873 .fromApp(applicationId)
874 .makePermanent()
875 .forTable(TMAC_TABLE).build();
876 }
877
878 /**
879 * Builds TMAC rules for IPv6 packets.
880 *
Charles Chan9174e6b2017-12-19 19:55:57 -0800881 * @param ethCriterion dst mac matching
882 * @param vidCriterion vlan id assigned to the port
883 * @param ofdpaMatchVlanVid OFDPA vlan id matching
884 * @param applicationId application id
885 * @param pnum port number
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530886 * @return TMAC rule for IPV6 packets
887 */
888 private FlowRule buildTmacRuleForIpv6(EthCriterion ethCriterion,
889 VlanIdCriterion vidCriterion,
890 OfdpaMatchVlanVid ofdpaMatchVlanVid,
891 ApplicationId applicationId,
892 PortNumber pnum) {
893 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
894 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
895 if (pnum != null) {
896 if (matchInPortTmacTable()) {
897 selector.matchInPort(pnum);
898 } else {
Saurav Das73a6cc12017-12-05 15:00:23 -0800899 log.debug("Pipeline does not support IN_PORT matching in TMAC table, " +
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530900 "ignoring the IN_PORT criteria");
901 }
902 }
903 if (vidCriterion != null) {
904 if (requireVlanExtensions()) {
905 selector.extension(ofdpaMatchVlanVid, deviceId);
906 } else {
907 selector.matchVlanId(vidCriterion.vlanId());
908 }
909 }
910 selector.matchEthType(Ethernet.TYPE_IPV6);
911 selector.matchEthDst(ethCriterion.mac());
912 treatment.transition(UNICAST_ROUTING_TABLE);
913 return DefaultFlowRule.builder()
914 .forDevice(deviceId)
915 .withSelector(selector.build())
916 .withTreatment(treatment.build())
917 .withPriority(DEFAULT_PRIORITY)
918 .fromApp(applicationId)
919 .makePermanent()
920 .forTable(TMAC_TABLE).build();
921 }
922
Charles Chan206506b2018-03-02 16:43:28 -0800923 protected List<List<FlowRule>> processEthDstOnlyFilter(EthCriterion ethCriterion,
Yi Tsengef19de12017-04-24 11:33:05 -0700924 ApplicationId applicationId) {
Pier Luigi0e358632017-01-31 09:35:05 -0800925 ImmutableList.Builder<FlowRule> builder = ImmutableList.builder();
926
Charles Chan5270ed02016-01-30 23:22:37 -0800927 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
928 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
929 selector.matchEthType(Ethernet.TYPE_IPV4);
930 selector.matchEthDst(ethCriterion.mac());
931 treatment.transition(UNICAST_ROUTING_TABLE);
932 FlowRule rule = DefaultFlowRule.builder()
933 .forDevice(deviceId)
934 .withSelector(selector.build())
935 .withTreatment(treatment.build())
936 .withPriority(DEFAULT_PRIORITY)
937 .fromApp(applicationId)
938 .makePermanent()
939 .forTable(TMAC_TABLE).build();
Pier Luigi0e358632017-01-31 09:35:05 -0800940 builder.add(rule);
941
942 selector = DefaultTrafficSelector.builder();
943 treatment = DefaultTrafficTreatment.builder();
944 selector.matchEthType(Ethernet.TYPE_IPV6);
945 selector.matchEthDst(ethCriterion.mac());
946 treatment.transition(UNICAST_ROUTING_TABLE);
947 rule = DefaultFlowRule.builder()
948 .forDevice(deviceId)
949 .withSelector(selector.build())
950 .withTreatment(treatment.build())
951 .withPriority(DEFAULT_PRIORITY)
952 .fromApp(applicationId)
953 .makePermanent()
954 .forTable(TMAC_TABLE).build();
Charles Chan206506b2018-03-02 16:43:28 -0800955 return ImmutableList.of(builder.add(rule).build());
Charles Chan5270ed02016-01-30 23:22:37 -0800956 }
957
Charles Chan206506b2018-03-02 16:43:28 -0800958 List<List<FlowRule>> processMcastEthDstFilter(EthCriterion ethCriterion,
Charles Chanb4879a52017-10-20 19:09:16 -0700959 VlanId assignedVlan,
Charles Chand57552d2018-03-02 15:41:41 -0800960 MacAddress unicastMac,
Yi Tsengef19de12017-04-24 11:33:05 -0700961 ApplicationId applicationId) {
Charles Chand57552d2018-03-02 15:41:41 -0800962 ImmutableList.Builder<FlowRule> unicastFlows = ImmutableList.builder();
963 ImmutableList.Builder<FlowRule> multicastFlows = ImmutableList.builder();
964 TrafficSelector.Builder selector;
965 TrafficTreatment.Builder treatment;
Charles Chanb4879a52017-10-20 19:09:16 -0700966 FlowRule rule;
Julia Ferguson65428c32017-08-10 18:15:24 +0000967
Charles Chanb4879a52017-10-20 19:09:16 -0700968 if (IPV4_MULTICAST.equals(ethCriterion.mac())) {
Charles Chand57552d2018-03-02 15:41:41 -0800969 if (requireUnicastBeforeMulticast()) {
970 selector = DefaultTrafficSelector.builder();
971 treatment = DefaultTrafficTreatment.builder();
972 selector.matchEthType(Ethernet.TYPE_IPV4);
973 selector.matchEthDst(unicastMac);
Charles Chan893a1d72018-05-10 22:19:25 +0000974 selector.matchVlanId(assignedVlan);
Charles Chand57552d2018-03-02 15:41:41 -0800975 treatment.transition(UNICAST_ROUTING_TABLE);
976 rule = DefaultFlowRule.builder()
977 .forDevice(deviceId)
978 .withSelector(selector.build())
979 .withTreatment(treatment.build())
Charles Chan893a1d72018-05-10 22:19:25 +0000980 .withPriority(DEFAULT_PRIORITY)
Charles Chand57552d2018-03-02 15:41:41 -0800981 .fromApp(applicationId)
982 .makePermanent()
983 .forTable(TMAC_TABLE).build();
984 unicastFlows.add(rule);
985 }
986
987 selector = DefaultTrafficSelector.builder();
988 treatment = DefaultTrafficTreatment.builder();
Charles Chanb4879a52017-10-20 19:09:16 -0700989 selector.matchEthType(Ethernet.TYPE_IPV4);
990 selector.matchEthDstMasked(ethCriterion.mac(), ethCriterion.mask());
Charles Chan893a1d72018-05-10 22:19:25 +0000991 selector.matchVlanId(assignedVlan);
Charles Chanb4879a52017-10-20 19:09:16 -0700992 treatment.transition(MULTICAST_ROUTING_TABLE);
993 rule = DefaultFlowRule.builder()
994 .forDevice(deviceId)
995 .withSelector(selector.build())
996 .withTreatment(treatment.build())
Charles Chan893a1d72018-05-10 22:19:25 +0000997 .withPriority(DEFAULT_PRIORITY)
Charles Chanb4879a52017-10-20 19:09:16 -0700998 .fromApp(applicationId)
999 .makePermanent()
1000 .forTable(TMAC_TABLE).build();
Charles Chand57552d2018-03-02 15:41:41 -08001001 multicastFlows.add(rule);
Charles Chanb4879a52017-10-20 19:09:16 -07001002 }
1003
1004 if (IPV6_MULTICAST.equals(ethCriterion.mac())) {
Charles Chand57552d2018-03-02 15:41:41 -08001005 if (requireUnicastBeforeMulticast()) {
1006 selector = DefaultTrafficSelector.builder();
1007 treatment = DefaultTrafficTreatment.builder();
1008 selector.matchEthType(Ethernet.TYPE_IPV6);
1009 selector.matchEthDst(unicastMac);
Charles Chan893a1d72018-05-10 22:19:25 +00001010 selector.matchVlanId(assignedVlan);
Charles Chand57552d2018-03-02 15:41:41 -08001011 treatment.transition(UNICAST_ROUTING_TABLE);
1012 rule = DefaultFlowRule.builder()
1013 .forDevice(deviceId)
1014 .withSelector(selector.build())
1015 .withTreatment(treatment.build())
Charles Chan893a1d72018-05-10 22:19:25 +00001016 .withPriority(DEFAULT_PRIORITY)
Charles Chand57552d2018-03-02 15:41:41 -08001017 .fromApp(applicationId)
1018 .makePermanent()
1019 .forTable(TMAC_TABLE).build();
1020 unicastFlows.add(rule);
1021 }
1022
Charles Chanb4879a52017-10-20 19:09:16 -07001023 selector = DefaultTrafficSelector.builder();
1024 treatment = DefaultTrafficTreatment.builder();
1025 selector.matchEthType(Ethernet.TYPE_IPV6);
1026 selector.matchEthDstMasked(ethCriterion.mac(), ethCriterion.mask());
Charles Chan893a1d72018-05-10 22:19:25 +00001027 selector.matchVlanId(assignedVlan);
Charles Chanb4879a52017-10-20 19:09:16 -07001028 treatment.transition(MULTICAST_ROUTING_TABLE);
1029 rule = DefaultFlowRule.builder()
1030 .forDevice(deviceId)
1031 .withSelector(selector.build())
1032 .withTreatment(treatment.build())
Charles Chan893a1d72018-05-10 22:19:25 +00001033 .withPriority(DEFAULT_PRIORITY)
Charles Chanb4879a52017-10-20 19:09:16 -07001034 .fromApp(applicationId)
1035 .makePermanent()
1036 .forTable(TMAC_TABLE).build();
Charles Chand57552d2018-03-02 15:41:41 -08001037 multicastFlows.add(rule);
Charles Chanb4879a52017-10-20 19:09:16 -07001038 }
Charles Chand57552d2018-03-02 15:41:41 -08001039 return ImmutableList.of(unicastFlows.build(), multicastFlows.build());
Charles Chan5b9df8d2016-03-28 22:21:40 -07001040 }
1041
Saurav Das822c4e22015-10-23 10:51:11 -07001042 private Collection<FlowRule> processForward(ForwardingObjective fwd) {
1043 switch (fwd.flag()) {
1044 case SPECIFIC:
1045 return processSpecific(fwd);
1046 case VERSATILE:
1047 return processVersatile(fwd);
1048 default:
1049 fail(fwd, ObjectiveError.UNKNOWN);
1050 log.warn("Unknown forwarding flag {}", fwd.flag());
1051 }
1052 return Collections.emptySet();
1053 }
1054
1055 /**
1056 * In the OF-DPA 2.0 pipeline, versatile forwarding objectives go to the
1057 * ACL table.
1058 * @param fwd the forwarding objective of type 'versatile'
1059 * @return a collection of flow rules to be sent to the switch. An empty
1060 * collection may be returned if there is a problem in processing
1061 * the flow rule
1062 */
Saurav Das52025962016-01-28 22:30:01 -08001063 protected Collection<FlowRule> processVersatile(ForwardingObjective fwd) {
Saurav Das018605f2017-02-18 14:05:44 -08001064 log.debug("Processing versatile forwarding objective:{} in dev:{}",
Saurav Dasd2fded02016-12-02 15:43:47 -08001065 fwd.id(), deviceId);
Saurav Das822c4e22015-10-23 10:51:11 -07001066
Saurav Das822c4e22015-10-23 10:51:11 -07001067 if (fwd.nextId() == null && fwd.treatment() == null) {
1068 log.error("Forwarding objective {} from {} must contain "
1069 + "nextId or Treatment", fwd.selector(), fwd.appId());
Zsolt Haraszti9faab752016-02-17 15:55:20 -08001070 fail(fwd, ObjectiveError.BADPARAMS);
Saurav Das822c4e22015-10-23 10:51:11 -07001071 return Collections.emptySet();
1072 }
Saurav Das49cb5a12016-01-16 22:54:07 -08001073
Saurav Das77b5e902016-01-27 17:01:59 -08001074 TrafficSelector.Builder sbuilder = DefaultTrafficSelector.builder();
1075 fwd.selector().criteria().forEach(criterion -> {
1076 if (criterion instanceof VlanIdCriterion) {
1077 VlanId vlanId = ((VlanIdCriterion) criterion).vlanId();
1078 // ensure that match does not include vlan = NONE as OF-DPA does not
1079 // match untagged packets this way in the ACL table.
1080 if (vlanId.equals(VlanId.NONE)) {
1081 return;
1082 }
Charles Chand1172632017-03-15 17:33:09 -07001083 if (requireVlanExtensions()) {
1084 OfdpaMatchVlanVid ofdpaMatchVlanVid = new OfdpaMatchVlanVid(vlanId);
1085 sbuilder.extension(ofdpaMatchVlanVid, deviceId);
1086 } else {
1087 sbuilder.matchVlanId(vlanId);
1088 }
Charles Chan393339f2018-01-11 11:48:18 -08001089 } else if (criterion instanceof Icmpv6TypeCriterion) {
1090 byte icmpv6Type = (byte) ((Icmpv6TypeCriterion) criterion).icmpv6Type();
1091 sbuilder.matchIcmpv6Type(icmpv6Type);
1092 } else if (criterion instanceof Icmpv6CodeCriterion) {
1093 byte icmpv6Code = (byte) ((Icmpv6CodeCriterion) criterion).icmpv6Code();
1094 sbuilder.matchIcmpv6Type(icmpv6Code);
Charles Chand9e47c62017-10-05 15:17:15 -07001095 } else if (criterion instanceof TcpPortCriterion || criterion instanceof UdpPortCriterion) {
1096 // FIXME: QMX switches do not support L4 dst port matching in ACL table.
1097 // Currently L4 dst port matching is only used by DHCP relay feature
1098 // and therefore is safe to be replaced with L4 src port matching.
1099 // We need to revisit this if L4 dst port is used for other purpose in the future.
1100 if (!supportIpv6L4Dst() && isIpv6(fwd.selector())) {
Charles Chand9e47c62017-10-05 15:17:15 -07001101 switch (criterion.type()) {
1102 case UDP_DST:
Charles Chand9e47c62017-10-05 15:17:15 -07001103 case UDP_DST_MASKED:
Charles Chand9e47c62017-10-05 15:17:15 -07001104 case TCP_DST:
Charles Chand9e47c62017-10-05 15:17:15 -07001105 case TCP_DST_MASKED:
Charles Chand9e47c62017-10-05 15:17:15 -07001106 break;
1107 default:
1108 sbuilder.add(criterion);
1109 }
1110 } else {
1111 sbuilder.add(criterion);
1112 }
Saurav Das77b5e902016-01-27 17:01:59 -08001113 } else {
1114 sbuilder.add(criterion);
1115 }
1116 });
1117
Saurav Das822c4e22015-10-23 10:51:11 -07001118 // XXX driver does not currently do type checking as per Tables 65-67 in
1119 // OFDPA 2.0 spec. The only allowed treatment is a punt to the controller.
Saurav Das49cb5a12016-01-16 22:54:07 -08001120 TrafficTreatment.Builder ttBuilder = DefaultTrafficTreatment.builder();
1121 if (fwd.treatment() != null) {
1122 for (Instruction ins : fwd.treatment().allInstructions()) {
1123 if (ins instanceof OutputInstruction) {
1124 OutputInstruction o = (OutputInstruction) ins;
Charles Chan93090352018-03-02 13:26:22 -08001125 if (PortNumber.CONTROLLER.equals(o.port())) {
Saurav Das49cb5a12016-01-16 22:54:07 -08001126 ttBuilder.add(o);
1127 } else {
1128 log.warn("Only allowed treatments in versatile forwarding "
1129 + "objectives are punts to the controller");
1130 }
Charles Chan3fe71712018-06-15 18:54:18 -07001131 } else if (ins instanceof NoActionInstruction) {
1132 // No action is allowed and nothing needs to be done
Saurav Das49cb5a12016-01-16 22:54:07 -08001133 } else {
1134 log.warn("Cannot process instruction in versatile fwd {}", ins);
1135 }
Saurav Das822c4e22015-10-23 10:51:11 -07001136 }
Charles Chan2df0e8a2017-01-09 11:45:08 -08001137 if (fwd.treatment().clearedDeferred()) {
1138 ttBuilder.wipeDeferred();
1139 }
Saurav Das822c4e22015-10-23 10:51:11 -07001140 }
Saurav Das822c4e22015-10-23 10:51:11 -07001141 if (fwd.nextId() != null) {
Saurav Das49cb5a12016-01-16 22:54:07 -08001142 // overide case
1143 NextGroup next = getGroupForNextObjective(fwd.nextId());
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +05301144 if (next == null) {
1145 fail(fwd, ObjectiveError.BADPARAMS);
1146 return Collections.emptySet();
1147 }
Saurav Das49cb5a12016-01-16 22:54:07 -08001148 List<Deque<GroupKey>> gkeys = appKryo.deserialize(next.data());
1149 // we only need the top level group's key to point the flow to it
1150 Group group = groupService.getGroup(deviceId, gkeys.get(0).peekFirst());
1151 if (group == null) {
1152 log.warn("Group with key:{} for next-id:{} not found in dev:{}",
1153 gkeys.get(0).peekFirst(), fwd.nextId(), deviceId);
1154 fail(fwd, ObjectiveError.GROUPMISSING);
1155 return Collections.emptySet();
1156 }
1157 ttBuilder.deferred().group(group.id());
Saurav Das822c4e22015-10-23 10:51:11 -07001158 }
Saurav Das49cb5a12016-01-16 22:54:07 -08001159
1160 FlowRule.Builder ruleBuilder = DefaultFlowRule.builder()
1161 .fromApp(fwd.appId())
1162 .withPriority(fwd.priority())
1163 .forDevice(deviceId)
Saurav Das77b5e902016-01-27 17:01:59 -08001164 .withSelector(sbuilder.build())
Saurav Das49cb5a12016-01-16 22:54:07 -08001165 .withTreatment(ttBuilder.build())
1166 .makePermanent()
1167 .forTable(ACL_TABLE);
1168 return Collections.singletonList(ruleBuilder.build());
Saurav Das822c4e22015-10-23 10:51:11 -07001169 }
1170
1171 /**
1172 * In the OF-DPA 2.0 pipeline, specific forwarding refers to the IP table
Saurav Das8a0732e2015-11-20 15:27:53 -08001173 * (unicast or multicast) or the L2 table (mac + vlan) or the MPLS table.
Saurav Das822c4e22015-10-23 10:51:11 -07001174 *
1175 * @param fwd the forwarding objective of type 'specific'
1176 * @return a collection of flow rules. Typically there will be only one
1177 * for this type of forwarding objective. An empty set may be
1178 * returned if there is an issue in processing the objective.
1179 */
Charles Chan93090352018-03-02 13:26:22 -08001180 private Collection<FlowRule> processSpecific(ForwardingObjective fwd) {
Saurav Das25190812016-05-27 13:54:07 -07001181 log.debug("Processing specific fwd objective:{} in dev:{} with next:{}",
Saurav Das4ce45962015-11-24 23:21:05 -08001182 fwd.id(), deviceId, fwd.nextId());
1183 boolean isEthTypeObj = isSupportedEthTypeObjective(fwd);
1184 boolean isEthDstObj = isSupportedEthDstObjective(fwd);
1185
1186 if (isEthTypeObj) {
1187 return processEthTypeSpecific(fwd);
1188 } else if (isEthDstObj) {
1189 return processEthDstSpecific(fwd);
1190 } else {
1191 log.warn("processSpecific: Unsupported forwarding objective "
1192 + "criteria fwd:{} in dev:{}", fwd.nextId(), deviceId);
Saurav Das822c4e22015-10-23 10:51:11 -07001193 fail(fwd, ObjectiveError.UNSUPPORTED);
1194 return Collections.emptySet();
1195 }
Saurav Das4ce45962015-11-24 23:21:05 -08001196 }
1197
Saurav Das4ce45962015-11-24 23:21:05 -08001198 /**
1199 * Handles forwarding rules to the IP and MPLS tables.
1200 *
1201 * @param fwd the forwarding objective
1202 * @return A collection of flow rules, or an empty set
1203 */
1204 protected Collection<FlowRule> processEthTypeSpecific(ForwardingObjective fwd) {
Charles Chancad338a2016-09-16 18:03:11 -07001205 return processEthTypeSpecificInternal(fwd, false, ACL_TABLE);
Charles Chanf9e98652016-09-07 16:54:23 -07001206 }
1207
1208 /**
1209 * Internal implementation of processEthTypeSpecific.
1210 * <p>
1211 * Wildcarded IPv4_DST is not supported in OFDPA i12. Therefore, we break
1212 * the rule into 0.0.0.0/1 and 128.0.0.0/1.
1213 * The allowDefaultRoute must be set to false for OFDPA i12.
1214 * </p>
1215 *
1216 * @param fwd the forwarding objective
1217 * @param allowDefaultRoute allow wildcarded IPv4_DST or not
Ray Milkey0bb1e102016-11-10 14:51:27 -08001218 * @param mplsNextTable next MPLS table
Charles Chanf9e98652016-09-07 16:54:23 -07001219 * @return A collection of flow rules, or an empty set
1220 */
1221 protected Collection<FlowRule> processEthTypeSpecificInternal(ForwardingObjective fwd,
Charles Chancad338a2016-09-16 18:03:11 -07001222 boolean allowDefaultRoute,
1223 int mplsNextTable) {
Saurav Das4ce45962015-11-24 23:21:05 -08001224 TrafficSelector selector = fwd.selector();
1225 EthTypeCriterion ethType =
1226 (EthTypeCriterion) selector.getCriterion(Criterion.Type.ETH_TYPE);
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001227 boolean emptyGroup = false;
Charles Chan188ebf52015-12-23 00:15:11 -08001228 int forTableId;
Saurav Das8a0732e2015-11-20 15:27:53 -08001229 TrafficSelector.Builder filteredSelector = DefaultTrafficSelector.builder();
Charles Chan14967c22015-12-07 11:11:50 -08001230 TrafficTreatment.Builder tb = DefaultTrafficTreatment.builder();
Flavio Castroe10fa242016-01-15 12:43:51 -08001231 TrafficSelector.Builder complementarySelector = DefaultTrafficSelector.builder();
Charles Chan14967c22015-12-07 11:11:50 -08001232
Saurav Das8a0732e2015-11-20 15:27:53 -08001233 if (ethType.ethType().toShort() == Ethernet.TYPE_IPV4) {
Pier Luigi3bfe32c2017-01-30 09:47:36 -08001234 if (buildIpv4Selector(filteredSelector, complementarySelector, fwd, allowDefaultRoute) < 0) {
1235 return Collections.emptyList();
1236 }
1237 // We need to set properly the next table
Flavio Castroe10fa242016-01-15 12:43:51 -08001238 IpPrefix ipv4Dst = ((IPCriterion) selector.getCriterion(Criterion.Type.IPV4_DST)).ip();
Charles Chan5b9df8d2016-03-28 22:21:40 -07001239 if (ipv4Dst.isMulticast()) {
Charles Chan5b9df8d2016-03-28 22:21:40 -07001240 forTableId = MULTICAST_ROUTING_TABLE;
Flavio Castroe10fa242016-01-15 12:43:51 -08001241 } else {
Charles Chan5b9df8d2016-03-28 22:21:40 -07001242 forTableId = UNICAST_ROUTING_TABLE;
Flavio Castroe10fa242016-01-15 12:43:51 -08001243 }
Charles Chan93090352018-03-02 13:26:22 -08001244
Charles Chan7feb6c82018-03-05 11:28:23 -08001245 // TODO decrementing IP ttl is done automatically for routing, this
Charles Chan93090352018-03-02 13:26:22 -08001246 // action is ignored or rejected in ofdpa as it is not fully implemented
Charles Chan7feb6c82018-03-05 11:28:23 -08001247 // if (fwd.treatment() != null) {
1248 // for (Instruction instr : fwd.treatment().allInstructions()) {
1249 // if (instr instanceof L3ModificationInstruction &&
1250 // ((L3ModificationInstruction) instr).subtype() == L3SubType.DEC_TTL) {
1251 // tb.deferred().add(instr);
1252 // }
1253 // }
1254 // }
1255
Pier Ventree0ae7a32016-11-23 09:57:42 -08001256 } else if (ethType.ethType().toShort() == Ethernet.TYPE_IPV6) {
1257 if (buildIpv6Selector(filteredSelector, fwd) < 0) {
1258 return Collections.emptyList();
1259 }
Kalhee Kim3d26bf02017-10-04 14:59:11 +00001260 //We need to set the proper next table
1261 IpPrefix ipv6Dst = ((IPCriterion) selector.getCriterion(Criterion.Type.IPV6_DST)).ip();
1262 if (ipv6Dst.isMulticast()) {
1263 forTableId = MULTICAST_ROUTING_TABLE;
1264 } else {
1265 forTableId = UNICAST_ROUTING_TABLE;
1266 }
1267
Charles Chan7feb6c82018-03-05 11:28:23 -08001268 // TODO decrementing IP ttl is done automatically for routing, this
Charles Chan93090352018-03-02 13:26:22 -08001269 // action is ignored or rejected in ofdpa as it is not fully implemented
Charles Chan7feb6c82018-03-05 11:28:23 -08001270 // if (fwd.treatment() != null) {
1271 // for (Instruction instr : fwd.treatment().allInstructions()) {
1272 // if (instr instanceof L3ModificationInstruction &&
1273 // ((L3ModificationInstruction) instr).subtype() == L3SubType.DEC_TTL) {
1274 // tb.deferred().add(instr);
1275 // }
1276 // }
1277 // }
Saurav Das8a0732e2015-11-20 15:27:53 -08001278 } else {
1279 filteredSelector
1280 .matchEthType(Ethernet.MPLS_UNICAST)
1281 .matchMplsLabel(((MplsCriterion)
1282 selector.getCriterion(Criterion.Type.MPLS_LABEL)).label());
1283 MplsBosCriterion bos = (MplsBosCriterion) selector
Pier Ventre140a8942016-11-02 07:26:38 -07001284 .getCriterion(MPLS_BOS);
Alex Yashchuka3679532017-12-08 17:40:05 +02001285 if (bos != null && requireMplsBosMatch()) {
Saurav Das8a0732e2015-11-20 15:27:53 -08001286 filteredSelector.matchMplsBos(bos.mplsBos());
1287 }
1288 forTableId = MPLS_TABLE_1;
Saurav Das4ce45962015-11-24 23:21:05 -08001289 log.debug("processing MPLS specific forwarding objective {} -> next:{}"
1290 + " in dev {}", fwd.id(), fwd.nextId(), deviceId);
Saurav Das822c4e22015-10-23 10:51:11 -07001291
Charles Chan14967c22015-12-07 11:11:50 -08001292 if (fwd.treatment() != null) {
1293 for (Instruction instr : fwd.treatment().allInstructions()) {
1294 if (instr instanceof L2ModificationInstruction &&
1295 ((L2ModificationInstruction) instr).subtype() == L2SubType.MPLS_POP) {
Pier Luigi3bfe32c2017-01-30 09:47:36 -08001296 // OF-DPA does not pop in MPLS table in some cases. For the L3 VPN, it requires
Saurav Das9c705342017-01-06 11:36:01 -08001297 // setting the MPLS_TYPE so pop can happen down the pipeline
Alex Yashchuka3679532017-12-08 17:40:05 +02001298 if (requireMplsPop()) {
1299 if (mplsNextTable == MPLS_TYPE_TABLE && isNotMplsBos(selector)) {
1300 tb.immediate().popMpls();
1301 }
1302 } else {
1303 // Skip mpls pop action for mpls_unicast label
1304 if (instr instanceof ModMplsHeaderInstruction &&
1305 !((ModMplsHeaderInstruction) instr).ethernetType()
1306 .equals(EtherType.MPLS_UNICAST.ethType())) {
1307 tb.immediate().add(instr);
1308 }
Pier Luigi3bfe32c2017-01-30 09:47:36 -08001309 }
Charles Chan14967c22015-12-07 11:11:50 -08001310 }
Alex Yashchuka3679532017-12-08 17:40:05 +02001311
1312 if (requireMplsTtlModification()) {
1313 if (instr instanceof L3ModificationInstruction &&
1314 ((L3ModificationInstruction) instr).subtype() == L3SubType.DEC_TTL) {
1315 // FIXME Should modify the app to send the correct DEC_MPLS_TTL instruction
1316 tb.immediate().decMplsTtl();
1317 }
1318 if (instr instanceof L3ModificationInstruction &&
1319 ((L3ModificationInstruction) instr).subtype() == L3SubType.TTL_IN) {
1320 tb.immediate().add(instr);
1321 }
Charles Chan14967c22015-12-07 11:11:50 -08001322 }
Saurav Das8a0732e2015-11-20 15:27:53 -08001323 }
1324 }
1325 }
Saurav Das822c4e22015-10-23 10:51:11 -07001326
1327 if (fwd.nextId() != null) {
Saurav Das423fe2b2015-12-04 10:52:59 -08001328 NextGroup next = getGroupForNextObjective(fwd.nextId());
1329 if (next != null) {
1330 List<Deque<GroupKey>> gkeys = appKryo.deserialize(next.data());
1331 // we only need the top level group's key to point the flow to it
1332 Group group = groupService.getGroup(deviceId, gkeys.get(0).peekFirst());
1333 if (group == null) {
1334 log.warn("Group with key:{} for next-id:{} not found in dev:{}",
1335 gkeys.get(0).peekFirst(), fwd.nextId(), deviceId);
1336 fail(fwd, ObjectiveError.GROUPMISSING);
1337 return Collections.emptySet();
1338 }
Saurav Dasa89b95a2018-02-14 14:14:54 -08001339 if (isNotMplsBos(selector) && group.type().equals(SELECT)) {
1340 log.warn("SR CONTINUE case cannot be handled as MPLS ECMP "
1341 + "is not implemented in OF-DPA yet. Aborting flow {} -> next:{} "
1342 + "in this device {}", fwd.id(), fwd.nextId(), deviceId);
1343 fail(fwd, ObjectiveError.FLOWINSTALLATIONFAILED);
1344 return Collections.emptySet();
1345 }
Saurav Das423fe2b2015-12-04 10:52:59 -08001346 tb.deferred().group(group.id());
Saurav Dasf3f75942018-01-25 09:49:01 -08001347 // retrying flows may be necessary due to bug CORD-554
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001348 if (gkeys.size() == 1 && gkeys.get(0).size() == 1) {
Saurav Dasf3f75942018-01-25 09:49:01 -08001349 if (shouldRetry()) {
1350 log.warn("Found empty group 0x{} in dev:{} .. will retry fwd:{}",
1351 Integer.toHexString(group.id().id()), deviceId,
1352 fwd.id());
1353 emptyGroup = true;
1354 }
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001355 }
Saurav Das25190812016-05-27 13:54:07 -07001356 } else {
1357 log.warn("Cannot find group for nextId:{} in dev:{}. Aborting fwd:{}",
1358 fwd.nextId(), deviceId, fwd.id());
1359 fail(fwd, ObjectiveError.FLOWINSTALLATIONFAILED);
1360 return Collections.emptySet();
Saurav Das822c4e22015-10-23 10:51:11 -07001361 }
Saurav Das822c4e22015-10-23 10:51:11 -07001362 }
Charles Chancad338a2016-09-16 18:03:11 -07001363
1364 if (forTableId == MPLS_TABLE_1) {
Pier Ventre140a8942016-11-02 07:26:38 -07001365 if (mplsNextTable == MPLS_L3_TYPE_TABLE) {
Charles Chancad338a2016-09-16 18:03:11 -07001366 Ofdpa3SetMplsType setMplsType = new Ofdpa3SetMplsType(Ofdpa3MplsType.L3_PHP);
Saurav Das9c705342017-01-06 11:36:01 -08001367 // set mpls type as apply_action
1368 tb.immediate().extension(setMplsType, deviceId);
Charles Chancad338a2016-09-16 18:03:11 -07001369 }
1370 tb.transition(mplsNextTable);
1371 } else {
1372 tb.transition(ACL_TABLE);
1373 }
1374
Andrea Campanella84ac4df2018-04-30 11:48:55 +02001375 if (fwd.treatment() != null && fwd.treatment().clearedDeferred()) {
1376 if (supportsUnicastBlackHole()) {
1377 tb.wipeDeferred();
1378 } else {
1379 log.warn("Clear Deferred is not supported Unicast Routing Table on device {}", deviceId);
1380 return Collections.emptySet();
1381 }
1382 }
1383
Saurav Das822c4e22015-10-23 10:51:11 -07001384 FlowRule.Builder ruleBuilder = DefaultFlowRule.builder()
1385 .fromApp(fwd.appId())
1386 .withPriority(fwd.priority())
1387 .forDevice(deviceId)
Saurav Das8a0732e2015-11-20 15:27:53 -08001388 .withSelector(filteredSelector.build())
1389 .withTreatment(tb.build())
1390 .forTable(forTableId);
Saurav Das822c4e22015-10-23 10:51:11 -07001391
1392 if (fwd.permanent()) {
1393 ruleBuilder.makePermanent();
1394 } else {
1395 ruleBuilder.makeTemporary(fwd.timeout());
1396 }
Flavio Castroe10fa242016-01-15 12:43:51 -08001397 Collection<FlowRule> flowRuleCollection = new ArrayList<>();
1398 flowRuleCollection.add(ruleBuilder.build());
Pier Luigi3bfe32c2017-01-30 09:47:36 -08001399 if (!allowDefaultRoute) {
Pier Ventree0ae7a32016-11-23 09:57:42 -08001400 flowRuleCollection.add(
1401 defaultRoute(fwd, complementarySelector, forTableId, tb)
1402 );
Flavio Castroe10fa242016-01-15 12:43:51 -08001403 log.debug("Default rule 0.0.0.0/0 is being installed two rules");
1404 }
Saurav Dasf3f75942018-01-25 09:49:01 -08001405
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001406 if (emptyGroup) {
pier5b9b9e32019-03-11 15:14:02 -07001407 retryExecutorService.schedule(new RetryFlows(fwd, flowRuleCollection),
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001408 RETRY_MS, TimeUnit.MILLISECONDS);
1409 }
Flavio Castroe10fa242016-01-15 12:43:51 -08001410 return flowRuleCollection;
Saurav Das822c4e22015-10-23 10:51:11 -07001411 }
1412
Charles Chan93090352018-03-02 13:26:22 -08001413 private int buildIpv4Selector(TrafficSelector.Builder builderToUpdate,
Pier Luigi3bfe32c2017-01-30 09:47:36 -08001414 TrafficSelector.Builder extBuilder,
1415 ForwardingObjective fwd,
1416 boolean allowDefaultRoute) {
1417 TrafficSelector selector = fwd.selector();
1418
1419 IpPrefix ipv4Dst = ((IPCriterion) selector.getCriterion(Criterion.Type.IPV4_DST)).ip();
1420 if (ipv4Dst.isMulticast()) {
1421 if (ipv4Dst.prefixLength() != 32) {
1422 log.warn("Multicast specific forwarding objective can only be /32");
1423 fail(fwd, ObjectiveError.BADPARAMS);
1424 return -1;
1425 }
1426 VlanId assignedVlan = readVlanFromSelector(fwd.meta());
1427 if (assignedVlan == null) {
1428 log.warn("VLAN ID required by multicast specific fwd obj is missing. Abort.");
1429 fail(fwd, ObjectiveError.BADPARAMS);
1430 return -1;
1431 }
Charles Chand1172632017-03-15 17:33:09 -07001432 if (requireVlanExtensions()) {
1433 OfdpaMatchVlanVid ofdpaMatchVlanVid = new OfdpaMatchVlanVid(assignedVlan);
1434 builderToUpdate.extension(ofdpaMatchVlanVid, deviceId);
1435 } else {
1436 builderToUpdate.matchVlanId(assignedVlan);
1437 }
Pier Luigi3bfe32c2017-01-30 09:47:36 -08001438 builderToUpdate.matchEthType(Ethernet.TYPE_IPV4).matchIPDst(ipv4Dst);
1439 log.debug("processing IPv4 multicast specific forwarding objective {} -> next:{}"
1440 + " in dev:{}", fwd.id(), fwd.nextId(), deviceId);
1441 } else {
1442 if (ipv4Dst.prefixLength() == 0) {
1443 if (allowDefaultRoute) {
1444 // The entire IPV4_DST field is wildcarded intentionally
1445 builderToUpdate.matchEthType(Ethernet.TYPE_IPV4);
1446 } else {
1447 // NOTE: The switch does not support matching 0.0.0.0/0
1448 // Split it into 0.0.0.0/1 and 128.0.0.0/1
1449 builderToUpdate.matchEthType(Ethernet.TYPE_IPV4)
1450 .matchIPDst(IpPrefix.valueOf("0.0.0.0/1"));
1451 extBuilder.matchEthType(Ethernet.TYPE_IPV4)
1452 .matchIPDst(IpPrefix.valueOf("128.0.0.0/1"));
1453 }
1454 } else {
1455 builderToUpdate.matchEthType(Ethernet.TYPE_IPV4).matchIPDst(ipv4Dst);
1456 }
1457 log.debug("processing IPv4 unicast specific forwarding objective {} -> next:{}"
1458 + " in dev:{}", fwd.id(), fwd.nextId(), deviceId);
1459 }
1460 return 0;
1461 }
1462
1463 /**
1464 * Helper method to build Ipv6 selector using the selector provided by
1465 * a forwarding objective.
1466 *
1467 * @param builderToUpdate the builder to update
1468 * @param fwd the selector to read
1469 * @return 0 if the update ends correctly. -1 if the matches
1470 * are not yet supported
1471 */
Charles Chan93090352018-03-02 13:26:22 -08001472 int buildIpv6Selector(TrafficSelector.Builder builderToUpdate,
Pier Luigi3bfe32c2017-01-30 09:47:36 -08001473 ForwardingObjective fwd) {
1474
1475 TrafficSelector selector = fwd.selector();
1476
1477 IpPrefix ipv6Dst = ((IPCriterion) selector.getCriterion(Criterion.Type.IPV6_DST)).ip();
1478 if (ipv6Dst.isMulticast()) {
Julia Ferguson65428c32017-08-10 18:15:24 +00001479 if (ipv6Dst.prefixLength() != IpAddress.INET6_BIT_LENGTH) {
1480 log.warn("Multicast specific forwarding objective can only be /128");
1481 fail(fwd, ObjectiveError.BADPARAMS);
1482 return -1;
1483 }
1484 VlanId assignedVlan = readVlanFromSelector(fwd.meta());
1485 if (assignedVlan == null) {
1486 log.warn("VLAN ID required by multicast specific fwd obj is missing. Abort.");
1487 fail(fwd, ObjectiveError.BADPARAMS);
1488 return -1;
1489 }
1490 if (requireVlanExtensions()) {
1491 OfdpaMatchVlanVid ofdpaMatchVlanVid = new OfdpaMatchVlanVid(assignedVlan);
1492 builderToUpdate.extension(ofdpaMatchVlanVid, deviceId);
1493 } else {
1494 builderToUpdate.matchVlanId(assignedVlan);
1495 }
1496 builderToUpdate.matchEthType(Ethernet.TYPE_IPV6).matchIPv6Dst(ipv6Dst);
1497 log.debug("processing IPv6 multicast specific forwarding objective {} -> next:{}"
1498 + " in dev:{}", fwd.id(), fwd.nextId(), deviceId);
1499 } else {
1500 if (ipv6Dst.prefixLength() != 0) {
1501 builderToUpdate.matchIPv6Dst(ipv6Dst);
1502 }
Pier Luigi3bfe32c2017-01-30 09:47:36 -08001503 builderToUpdate.matchEthType(Ethernet.TYPE_IPV6);
1504 log.debug("processing IPv6 unicast specific forwarding objective {} -> next:{}"
1505 + " in dev:{}", fwd.id(), fwd.nextId(), deviceId);
Julia Ferguson65428c32017-08-10 18:15:24 +00001506 }
Pier Luigi3bfe32c2017-01-30 09:47:36 -08001507 return 0;
1508 }
1509
Charles Chan93090352018-03-02 13:26:22 -08001510 FlowRule defaultRoute(ForwardingObjective fwd,
Pier Ventree0ae7a32016-11-23 09:57:42 -08001511 TrafficSelector.Builder complementarySelector,
1512 int forTableId,
1513 TrafficTreatment.Builder tb) {
1514 FlowRule.Builder rule = DefaultFlowRule.builder()
1515 .fromApp(fwd.appId())
1516 .withPriority(fwd.priority())
1517 .forDevice(deviceId)
1518 .withSelector(complementarySelector.build())
1519 .withTreatment(tb.build())
1520 .forTable(forTableId);
1521 if (fwd.permanent()) {
1522 rule.makePermanent();
1523 } else {
1524 rule.makeTemporary(fwd.timeout());
1525 }
1526 return rule.build();
1527 }
1528
Saurav Das4ce45962015-11-24 23:21:05 -08001529 /**
1530 * Handles forwarding rules to the L2 bridging table. Flow actions are not
1531 * allowed in the bridging table - instead we use L2 Interface group or
1532 * L2 flood group
1533 *
1534 * @param fwd the forwarding objective
1535 * @return A collection of flow rules, or an empty set
1536 */
1537 protected Collection<FlowRule> processEthDstSpecific(ForwardingObjective fwd) {
1538 List<FlowRule> rules = new ArrayList<>();
1539
1540 // Build filtered selector
1541 TrafficSelector selector = fwd.selector();
1542 EthCriterion ethCriterion = (EthCriterion) selector
1543 .getCriterion(Criterion.Type.ETH_DST);
1544 VlanIdCriterion vlanIdCriterion = (VlanIdCriterion) selector
1545 .getCriterion(Criterion.Type.VLAN_VID);
1546
1547 if (vlanIdCriterion == null) {
1548 log.warn("Forwarding objective for bridging requires vlan. Not "
1549 + "installing fwd:{} in dev:{}", fwd.id(), deviceId);
1550 fail(fwd, ObjectiveError.BADPARAMS);
1551 return Collections.emptySet();
1552 }
1553
1554 TrafficSelector.Builder filteredSelectorBuilder =
1555 DefaultTrafficSelector.builder();
Yi Tseng3a77b4f2017-02-06 15:02:17 -08001556
1557 if (!ethCriterion.mac().equals(NONE) &&
1558 !ethCriterion.mac().equals(BROADCAST)) {
Saurav Das4ce45962015-11-24 23:21:05 -08001559 filteredSelectorBuilder.matchEthDst(ethCriterion.mac());
1560 log.debug("processing L2 forwarding objective:{} -> next:{} in dev:{}",
1561 fwd.id(), fwd.nextId(), deviceId);
1562 } else {
Yi Tseng3a77b4f2017-02-06 15:02:17 -08001563 // Use wildcard DST_MAC if the MacAddress is None or Broadcast
Saurav Das4ce45962015-11-24 23:21:05 -08001564 log.debug("processing L2 Broadcast forwarding objective:{} -> next:{} "
1565 + "in dev:{} for vlan:{}",
1566 fwd.id(), fwd.nextId(), deviceId, vlanIdCriterion.vlanId());
1567 }
Charles Chand1172632017-03-15 17:33:09 -07001568 if (requireVlanExtensions()) {
1569 OfdpaMatchVlanVid ofdpaMatchVlanVid = new OfdpaMatchVlanVid(vlanIdCriterion.vlanId());
1570 filteredSelectorBuilder.extension(ofdpaMatchVlanVid, deviceId);
1571 } else {
1572 filteredSelectorBuilder.matchVlanId(vlanIdCriterion.vlanId());
1573 }
Saurav Das4ce45962015-11-24 23:21:05 -08001574 TrafficSelector filteredSelector = filteredSelectorBuilder.build();
1575
1576 if (fwd.treatment() != null) {
1577 log.warn("Ignoring traffic treatment in fwd rule {} meant for L2 table"
1578 + "for dev:{}. Expecting only nextId", fwd.id(), deviceId);
1579 }
1580
1581 TrafficTreatment.Builder treatmentBuilder = DefaultTrafficTreatment.builder();
1582 if (fwd.nextId() != null) {
Saurav Das423fe2b2015-12-04 10:52:59 -08001583 NextGroup next = getGroupForNextObjective(fwd.nextId());
Saurav Das4ce45962015-11-24 23:21:05 -08001584 if (next != null) {
1585 List<Deque<GroupKey>> gkeys = appKryo.deserialize(next.data());
1586 // we only need the top level group's key to point the flow to it
1587 Group group = groupService.getGroup(deviceId, gkeys.get(0).peekFirst());
1588 if (group != null) {
1589 treatmentBuilder.deferred().group(group.id());
1590 } else {
1591 log.warn("Group with key:{} for next-id:{} not found in dev:{}",
1592 gkeys.get(0).peekFirst(), fwd.nextId(), deviceId);
1593 fail(fwd, ObjectiveError.GROUPMISSING);
1594 return Collections.emptySet();
1595 }
1596 }
1597 }
1598 treatmentBuilder.immediate().transition(ACL_TABLE);
1599 TrafficTreatment filteredTreatment = treatmentBuilder.build();
1600
1601 // Build bridging table entries
1602 FlowRule.Builder flowRuleBuilder = DefaultFlowRule.builder();
1603 flowRuleBuilder.fromApp(fwd.appId())
1604 .withPriority(fwd.priority())
1605 .forDevice(deviceId)
1606 .withSelector(filteredSelector)
1607 .withTreatment(filteredTreatment)
1608 .forTable(BRIDGING_TABLE);
1609 if (fwd.permanent()) {
1610 flowRuleBuilder.makePermanent();
1611 } else {
1612 flowRuleBuilder.makeTemporary(fwd.timeout());
1613 }
1614 rules.add(flowRuleBuilder.build());
1615 return rules;
1616 }
1617
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001618 //////////////////////////////////////
1619 // Helper Methods and Classes
1620 //////////////////////////////////////
1621
1622 private boolean isSupportedEthTypeObjective(ForwardingObjective fwd) {
1623 TrafficSelector selector = fwd.selector();
1624 EthTypeCriterion ethType = (EthTypeCriterion) selector
1625 .getCriterion(Criterion.Type.ETH_TYPE);
1626 return !((ethType == null) ||
1627 ((ethType.ethType().toShort() != Ethernet.TYPE_IPV4) &&
Pier Ventree0ae7a32016-11-23 09:57:42 -08001628 (ethType.ethType().toShort() != Ethernet.MPLS_UNICAST)) &&
1629 (ethType.ethType().toShort() != Ethernet.TYPE_IPV6));
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001630 }
1631
1632 private boolean isSupportedEthDstObjective(ForwardingObjective fwd) {
1633 TrafficSelector selector = fwd.selector();
1634 EthCriterion ethDst = (EthCriterion) selector
1635 .getCriterion(Criterion.Type.ETH_DST);
1636 VlanIdCriterion vlanId = (VlanIdCriterion) selector
1637 .getCriterion(Criterion.Type.VLAN_VID);
1638 return !(ethDst == null && vlanId == null);
1639 }
1640
Charles Chan93090352018-03-02 13:26:22 -08001641 NextGroup getGroupForNextObjective(Integer nextId) {
Saurav Das423fe2b2015-12-04 10:52:59 -08001642 NextGroup next = flowObjectiveStore.getNextGroup(nextId);
1643 if (next != null) {
1644 List<Deque<GroupKey>> gkeys = appKryo.deserialize(next.data());
1645 if (gkeys != null && !gkeys.isEmpty()) {
1646 return next;
1647 } else {
1648 log.warn("Empty next group found in FlowObjective store for "
1649 + "next-id:{} in dev:{}", nextId, deviceId);
1650 }
1651 } else {
1652 log.warn("next-id {} not found in Flow objective store for dev:{}",
1653 nextId, deviceId);
1654 }
1655 return null;
1656 }
1657
Saurav Das24431192016-03-07 19:13:00 -08001658 @Override
1659 public List<String> getNextMappings(NextGroup nextGroup) {
1660 List<String> mappings = new ArrayList<>();
1661 List<Deque<GroupKey>> gkeys = appKryo.deserialize(nextGroup.data());
1662 for (Deque<GroupKey> gkd : gkeys) {
1663 Group lastGroup = null;
Yuta HIGUCHI2dce08a2017-04-20 21:57:48 -07001664 StringBuilder gchain = new StringBuilder();
Saurav Das24431192016-03-07 19:13:00 -08001665 for (GroupKey gk : gkd) {
1666 Group g = groupService.getGroup(deviceId, gk);
Saurav Das8be4e3a2016-03-11 17:19:07 -08001667 if (g == null) {
Saurav Das25190812016-05-27 13:54:07 -07001668 gchain.append(" NoGrp").append(" -->");
Saurav Das8be4e3a2016-03-11 17:19:07 -08001669 continue;
1670 }
1671 gchain.append(" 0x").append(Integer.toHexString(g.id().id()))
1672 .append(" -->");
Saurav Das24431192016-03-07 19:13:00 -08001673 lastGroup = g;
1674 }
1675 // add port information for last group in group-chain
Yuta HIGUCHI2dce08a2017-04-20 21:57:48 -07001676 List<Instruction> lastGroupIns = new ArrayList<>();
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001677 if (lastGroup != null && !lastGroup.buckets().buckets().isEmpty()) {
Saurav Das25190812016-05-27 13:54:07 -07001678 lastGroupIns = lastGroup.buckets().buckets().get(0)
1679 .treatment().allInstructions();
1680 }
1681 for (Instruction i: lastGroupIns) {
Saurav Das24431192016-03-07 19:13:00 -08001682 if (i instanceof OutputInstruction) {
Saurav Das8be4e3a2016-03-11 17:19:07 -08001683 gchain.append(" port:").append(((OutputInstruction) i).port());
Saurav Das24431192016-03-07 19:13:00 -08001684 }
1685 }
Saurav Das8be4e3a2016-03-11 17:19:07 -08001686 mappings.add(gchain.toString());
Saurav Das24431192016-03-07 19:13:00 -08001687 }
1688 return mappings;
1689 }
Charles Chan5b9df8d2016-03-28 22:21:40 -07001690
Saurav Dasa89b95a2018-02-14 14:14:54 -08001691 /**
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001692 * Utility class that retries sending flows a fixed number of times, even if
1693 * some of the attempts are successful. Used only for forwarding objectives.
1694 */
Alex Yashchuka3679532017-12-08 17:40:05 +02001695 public final class RetryFlows implements Runnable {
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001696 int attempts = MAX_RETRY_ATTEMPTS;
1697 private Collection<FlowRule> retryFlows;
1698 private ForwardingObjective fwd;
1699
Charles Chan93090352018-03-02 13:26:22 -08001700 RetryFlows(ForwardingObjective fwd, Collection<FlowRule> retryFlows) {
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001701 this.fwd = fwd;
1702 this.retryFlows = retryFlows;
1703 }
1704
1705 @Override
1706 public void run() {
1707 log.info("RETRY FLOWS ATTEMPT# {} for fwd:{} rules:{}",
1708 MAX_RETRY_ATTEMPTS - attempts, fwd.id(), retryFlows.size());
pier5b9b9e32019-03-11 15:14:02 -07001709 sendForwards(Collections.singletonList(Pair.of(fwd, retryFlows)));
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001710 if (--attempts > 0) {
pier5b9b9e32019-03-11 15:14:02 -07001711 retryExecutorService.schedule(this, RETRY_MS, TimeUnit.MILLISECONDS);
1712 }
1713 }
1714 }
1715
1716 // Flow rules accumulator for reducing the number of transactions required to the devices.
1717 private final class ForwardingObjectiveAccumulator
1718 extends AbstractAccumulator<Pair<ForwardingObjective, Collection<FlowRule>>> {
1719
1720 ForwardingObjectiveAccumulator(int maxFwd, int maxBatchMS, int maxIdleMS) {
1721 super(TIMER, maxFwd, maxBatchMS, maxIdleMS);
1722 }
1723
1724 @Override
1725 public void processItems(List<Pair<ForwardingObjective, Collection<FlowRule>>> pairs) {
1726 // Triggers creation of a batch using the list of flowrules generated from fwdobjs.
1727 accumulatorExecutorService.execute(new FlowRulesBuilderTask(pairs));
1728 }
1729 }
1730
1731 // Task for building batch of flow rules in a separate thread.
1732 private final class FlowRulesBuilderTask implements Runnable {
1733 private final List<Pair<ForwardingObjective, Collection<FlowRule>>> pairs;
1734
1735 FlowRulesBuilderTask(List<Pair<ForwardingObjective, Collection<FlowRule>>> pairs) {
1736 this.pairs = pairs;
1737 }
1738
1739 @Override
1740 public void run() {
1741 try {
1742 sendForwards(pairs);
1743 } catch (Exception e) {
1744 log.warn("Unable to send forwards", e);
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001745 }
1746 }
1747 }
1748
Saurav Das822c4e22015-10-23 10:51:11 -07001749}