blob: 81a14ef920c505aa73c601f498ce45ce623627dd [file] [log] [blame]
Saurav Das822c4e22015-10-23 10:51:11 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2016-present Open Networking Foundation
Saurav Das822c4e22015-10-23 10:51:11 -07003 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
Yi Tsengef19de12017-04-24 11:33:05 -070016package org.onosproject.driver.pipeline.ofdpa;
Saurav Das822c4e22015-10-23 10:51:11 -070017
Charles Chan5270ed02016-01-30 23:22:37 -080018import com.google.common.collect.ImmutableList;
pier8b3aef42019-03-11 15:14:02 -070019import com.google.common.collect.Lists;
Yi Tseng47f82dc2017-03-05 22:48:39 -080020import com.google.common.collect.Sets;
pier8b3aef42019-03-11 15:14:02 -070021import org.apache.commons.lang3.tuple.Pair;
Saurav Das822c4e22015-10-23 10:51:11 -070022import org.onlab.osgi.ServiceDirectory;
Jonghwan Hyun800d9d02018-04-09 09:40:50 -070023import org.onlab.packet.EthType;
Saurav Das822c4e22015-10-23 10:51:11 -070024import org.onlab.packet.Ethernet;
Alex Yashchuk4caa8e82017-12-08 17:40:05 +020025import org.onlab.packet.EthType.EtherType;
Julia Ferguson65428c32017-08-10 18:15:24 +000026import org.onlab.packet.IpAddress;
Charles Chan14967c22015-12-07 11:11:50 -080027import org.onlab.packet.IpPrefix;
Charles Chan45b69ab2018-03-02 15:41:41 -080028import org.onlab.packet.MacAddress;
Saurav Das822c4e22015-10-23 10:51:11 -070029import org.onlab.packet.VlanId;
pier8b3aef42019-03-11 15:14:02 -070030import org.onlab.util.AbstractAccumulator;
31import org.onlab.util.Accumulator;
Saurav Das822c4e22015-10-23 10:51:11 -070032import org.onlab.util.KryoNamespace;
33import org.onosproject.core.ApplicationId;
34import org.onosproject.core.CoreService;
Jonghwan Hyun800d9d02018-04-09 09:40:50 -070035import org.onosproject.driver.extensions.Ofdpa3CopyField;
Charles Chancad338a2016-09-16 18:03:11 -070036import org.onosproject.driver.extensions.Ofdpa3MplsType;
37import org.onosproject.driver.extensions.Ofdpa3SetMplsType;
Jonghwan Hyun800d9d02018-04-09 09:40:50 -070038import org.onosproject.driver.extensions.OfdpaMatchActsetOutput;
39import org.onosproject.driver.extensions.OfdpaMatchAllowVlanTranslation;
Charles Chan14967c22015-12-07 11:11:50 -080040import org.onosproject.driver.extensions.OfdpaMatchVlanVid;
41import org.onosproject.driver.extensions.OfdpaSetVlanVid;
Saurav Das822c4e22015-10-23 10:51:11 -070042import org.onosproject.net.DeviceId;
43import org.onosproject.net.Port;
44import org.onosproject.net.PortNumber;
45import org.onosproject.net.behaviour.NextGroup;
46import org.onosproject.net.behaviour.Pipeliner;
47import org.onosproject.net.behaviour.PipelinerContext;
48import org.onosproject.net.device.DeviceService;
49import org.onosproject.net.driver.AbstractHandlerBehaviour;
50import org.onosproject.net.flow.DefaultFlowRule;
51import org.onosproject.net.flow.DefaultTrafficSelector;
52import org.onosproject.net.flow.DefaultTrafficTreatment;
53import org.onosproject.net.flow.FlowRule;
54import org.onosproject.net.flow.FlowRuleOperations;
55import org.onosproject.net.flow.FlowRuleOperationsContext;
56import org.onosproject.net.flow.FlowRuleService;
57import org.onosproject.net.flow.TrafficSelector;
58import org.onosproject.net.flow.TrafficTreatment;
59import org.onosproject.net.flow.criteria.Criteria;
60import org.onosproject.net.flow.criteria.Criterion;
61import org.onosproject.net.flow.criteria.EthCriterion;
62import org.onosproject.net.flow.criteria.EthTypeCriterion;
63import org.onosproject.net.flow.criteria.IPCriterion;
Pier Ventree0ae7a32016-11-23 09:57:42 -080064import org.onosproject.net.flow.criteria.Icmpv6CodeCriterion;
65import org.onosproject.net.flow.criteria.Icmpv6TypeCriterion;
Saurav Das8a0732e2015-11-20 15:27:53 -080066import org.onosproject.net.flow.criteria.MplsBosCriterion;
67import org.onosproject.net.flow.criteria.MplsCriterion;
Saurav Das822c4e22015-10-23 10:51:11 -070068import org.onosproject.net.flow.criteria.PortCriterion;
Charles Chand9e47c62017-10-05 15:17:15 -070069import org.onosproject.net.flow.criteria.TcpPortCriterion;
70import org.onosproject.net.flow.criteria.UdpPortCriterion;
Saurav Das822c4e22015-10-23 10:51:11 -070071import org.onosproject.net.flow.criteria.VlanIdCriterion;
72import org.onosproject.net.flow.instructions.Instruction;
73import org.onosproject.net.flow.instructions.Instructions.OutputInstruction;
Charles Chanf76de302018-06-15 18:54:18 -070074import org.onosproject.net.flow.instructions.Instructions.NoActionInstruction;
Saurav Das822c4e22015-10-23 10:51:11 -070075import org.onosproject.net.flow.instructions.L2ModificationInstruction;
Saurav Das8a0732e2015-11-20 15:27:53 -080076import org.onosproject.net.flow.instructions.L2ModificationInstruction.L2SubType;
Alex Yashchuk4caa8e82017-12-08 17:40:05 +020077import org.onosproject.net.flow.instructions.L2ModificationInstruction.ModMplsHeaderInstruction;
Charles Chan14967c22015-12-07 11:11:50 -080078import org.onosproject.net.flow.instructions.L3ModificationInstruction;
79import org.onosproject.net.flow.instructions.L3ModificationInstruction.L3SubType;
Saurav Das822c4e22015-10-23 10:51:11 -070080import org.onosproject.net.flowobjective.FilteringObjective;
81import org.onosproject.net.flowobjective.FlowObjectiveStore;
82import org.onosproject.net.flowobjective.ForwardingObjective;
83import org.onosproject.net.flowobjective.NextObjective;
84import org.onosproject.net.flowobjective.Objective;
85import org.onosproject.net.flowobjective.ObjectiveError;
Saurav Das822c4e22015-10-23 10:51:11 -070086import org.onosproject.net.group.DefaultGroupKey;
87import org.onosproject.net.group.Group;
Saurav Das822c4e22015-10-23 10:51:11 -070088import org.onosproject.net.group.GroupKey;
Saurav Das822c4e22015-10-23 10:51:11 -070089import org.onosproject.net.group.GroupService;
Saurav Das822c4e22015-10-23 10:51:11 -070090import org.onosproject.store.serializers.KryoNamespaces;
91import org.slf4j.Logger;
92
Pier Ventree0ae7a32016-11-23 09:57:42 -080093import java.util.ArrayDeque;
94import java.util.ArrayList;
95import java.util.Collection;
96import java.util.Collections;
97import java.util.Deque;
98import java.util.List;
99import java.util.Objects;
Jonghwan Hyun800d9d02018-04-09 09:40:50 -0700100import java.util.Optional;
pier8b3aef42019-03-11 15:14:02 -0700101import java.util.Timer;
Pier Ventree0ae7a32016-11-23 09:57:42 -0800102import java.util.concurrent.ScheduledExecutorService;
103import java.util.concurrent.TimeUnit;
104
105import static java.util.concurrent.Executors.newScheduledThreadPool;
pier8b3aef42019-03-11 15:14:02 -0700106import static java.util.concurrent.Executors.newSingleThreadScheduledExecutor;
Yi Tseng3a77b4f2017-02-06 15:02:17 -0800107import static org.onlab.packet.MacAddress.BROADCAST;
Charles Chanb4879a52017-10-20 19:09:16 -0700108import static org.onlab.packet.MacAddress.IPV4_MULTICAST;
109import static org.onlab.packet.MacAddress.IPV6_MULTICAST;
Yi Tseng3a77b4f2017-02-06 15:02:17 -0800110import static org.onlab.packet.MacAddress.NONE;
Pier Ventree0ae7a32016-11-23 09:57:42 -0800111import static org.onlab.util.Tools.groupedThreads;
Jonghwan Hyun800d9d02018-04-09 09:40:50 -0700112import static org.onosproject.driver.extensions.Ofdpa3CopyField.OXM_ID_PACKET_REG_1;
113import static org.onosproject.driver.extensions.Ofdpa3CopyField.OXM_ID_VLAN_VID;
Yi Tsengef19de12017-04-24 11:33:05 -0700114import static org.onosproject.driver.pipeline.ofdpa.OfdpaGroupHandlerUtility.*;
pier9469f3e2019-04-17 17:05:08 +0200115import static org.onosproject.driver.pipeline.ofdpa.OfdpaPipelineUtility.*;
pier7afc7522019-05-10 13:19:15 +0200116import static org.onosproject.net.flowobjective.ForwardingObjective.Flag.SPECIFIC;
Pier Luigi075f1012018-02-01 10:23:12 +0100117import static org.onosproject.net.group.GroupDescription.Type.SELECT;
Pier Ventree0ae7a32016-11-23 09:57:42 -0800118import static org.slf4j.LoggerFactory.getLogger;
Pier Ventre140a8942016-11-02 07:26:38 -0700119import static org.onosproject.net.flow.criteria.Criterion.Type.MPLS_BOS;
Pier Ventree0ae7a32016-11-23 09:57:42 -0800120
Saurav Das822c4e22015-10-23 10:51:11 -0700121/**
122 * Driver for Broadcom's OF-DPA v2.0 TTP.
Saurav Das822c4e22015-10-23 10:51:11 -0700123 */
Charles Chan361154b2016-03-24 10:23:39 -0700124public class Ofdpa2Pipeline extends AbstractHandlerBehaviour implements Pipeliner {
pier8b3aef42019-03-11 15:14:02 -0700125 // Timer for the accumulator
126 private static final Timer TIMER = new Timer("fwdobj-batching");
127 private Accumulator<Pair<ForwardingObjective, Collection<FlowRule>>> accumulator;
pier9469f3e2019-04-17 17:05:08 +0200128 // Internal objects
Saurav Das822c4e22015-10-23 10:51:11 -0700129 private final Logger log = getLogger(getClass());
Charles Chan425854b2016-04-11 15:32:12 -0700130 protected ServiceDirectory serviceDirectory;
Saurav Das822c4e22015-10-23 10:51:11 -0700131 protected FlowRuleService flowRuleService;
Charles Chan425854b2016-04-11 15:32:12 -0700132 protected CoreService coreService;
Saurav Das8a0732e2015-11-20 15:27:53 -0800133 protected GroupService groupService;
134 protected FlowObjectiveStore flowObjectiveStore;
Saurav Das822c4e22015-10-23 10:51:11 -0700135 protected DeviceId deviceId;
136 protected ApplicationId driverId;
Saurav Das822c4e22015-10-23 10:51:11 -0700137 protected DeviceService deviceService;
Charles Chan188ebf52015-12-23 00:15:11 -0800138 protected static KryoNamespace appKryo = new KryoNamespace.Builder()
Yi Tsengef19de12017-04-24 11:33:05 -0700139 .register(KryoNamespaces.API)
140 .register(GroupKey.class)
141 .register(DefaultGroupKey.class)
142 .register(OfdpaNextGroup.class)
143 .register(ArrayDeque.class)
144 .build("Ofdpa2Pipeline");
Saurav Das822c4e22015-10-23 10:51:11 -0700145
Charles Chan425854b2016-04-11 15:32:12 -0700146 protected Ofdpa2GroupHandler groupHandler;
Saurav Das822c4e22015-10-23 10:51:11 -0700147
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700148 // flows installations to be retried
pier8b3aef42019-03-11 15:14:02 -0700149 private ScheduledExecutorService retryExecutorService
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700150 = newScheduledThreadPool(5, groupedThreads("OfdpaPipeliner", "retry-%d", log));
Saurav Das4f980082015-11-05 13:39:15 -0800151
pier8b3aef42019-03-11 15:14:02 -0700152 // accumulator executor service
153 private ScheduledExecutorService accumulatorExecutorService
154 = newSingleThreadScheduledExecutor(groupedThreads("OfdpaPipeliner", "acc-%d", log));
155
Saurav Das822c4e22015-10-23 10:51:11 -0700156 @Override
157 public void init(DeviceId deviceId, PipelinerContext context) {
Saurav Das822c4e22015-10-23 10:51:11 -0700158 this.deviceId = deviceId;
159
Charles Chan425854b2016-04-11 15:32:12 -0700160 serviceDirectory = context.directory();
Saurav Das822c4e22015-10-23 10:51:11 -0700161 coreService = serviceDirectory.get(CoreService.class);
162 flowRuleService = serviceDirectory.get(FlowRuleService.class);
163 groupService = serviceDirectory.get(GroupService.class);
164 flowObjectiveStore = context.store();
Saurav Das822c4e22015-10-23 10:51:11 -0700165 deviceService = serviceDirectory.get(DeviceService.class);
pier8b3aef42019-03-11 15:14:02 -0700166 // Init the accumulator, if enabled
pier9469f3e2019-04-17 17:05:08 +0200167 if (isAccumulatorEnabled(this)) {
pier8b3aef42019-03-11 15:14:02 -0700168 accumulator = new ForwardingObjectiveAccumulator(context.accumulatorMaxObjectives(),
169 context.accumulatorMaxBatchMillis(),
170 context.accumulatorMaxIdleMillis());
171 }
Saurav Das822c4e22015-10-23 10:51:11 -0700172
Charles Chan40132b32017-01-22 00:19:37 -0800173 initDriverId();
174 initGroupHander(context);
Saurav Das822c4e22015-10-23 10:51:11 -0700175
Saurav Das822c4e22015-10-23 10:51:11 -0700176 initializePipeline();
Saurav Das822c4e22015-10-23 10:51:11 -0700177 }
178
pier7afc7522019-05-10 13:19:15 +0200179 void setupAccumulatorForTests(int maxFwd, int maxBatchMS, int maxIdleMS) {
180 if (accumulator == null) {
181 accumulator = new ForwardingObjectiveAccumulator(maxFwd,
182 maxBatchMS,
183 maxIdleMS);
184 }
185 }
186
Charles Chan40132b32017-01-22 00:19:37 -0800187 protected void initDriverId() {
188 driverId = coreService.registerApplication(
189 "org.onosproject.driver.Ofdpa2Pipeline");
190 }
191
192 protected void initGroupHander(PipelinerContext context) {
193 groupHandler = new Ofdpa2GroupHandler();
194 groupHandler.init(deviceId, context);
195 }
196
Saurav Das822c4e22015-10-23 10:51:11 -0700197 protected void initializePipeline() {
Charles Chan188ebf52015-12-23 00:15:11 -0800198 // OF-DPA does not require initializing the pipeline as it puts default
199 // rules automatically in the hardware. However emulation of OFDPA in
200 // software switches does require table-miss-entries.
Saurav Das822c4e22015-10-23 10:51:11 -0700201 }
202
Charles Chand1172632017-03-15 17:33:09 -0700203 /**
Alex Yashchuk4caa8e82017-12-08 17:40:05 +0200204 * Determines whether this pipeline requires MPLS POP instruction.
205 *
206 * @return true to use MPLS POP instruction
207 */
208 public boolean requireMplsPop() {
209 return true;
210 }
211
212 /**
213 * Determines whether this pipeline requires MPLS BOS match.
214 *
215 * @return true to use MPLS BOS match
216 */
217 public boolean requireMplsBosMatch() {
218 return true;
219 }
220
221 /**
222 * Determines whether this pipeline requires MPLS TTL decrement and copy.
223 *
224 * @return true to use MPLS TTL decrement and copy
225 */
226 public boolean requireMplsTtlModification() {
227 return true;
228 }
229
230 /**
Charles Chand1172632017-03-15 17:33:09 -0700231 * Determines whether this pipeline requires OFDPA match and set VLAN extensions.
232 *
233 * @return true to use the extensions
234 */
235 protected boolean requireVlanExtensions() {
236 return true;
237 }
238
Saurav Das86d13e82017-04-28 17:03:48 -0700239 /**
240 * Determines whether in-port should be matched on in TMAC table rules.
241 *
242 * @return true if match on in-port should be programmed
243 */
244 protected boolean matchInPortTmacTable() {
245 return true;
246 }
247
Charles Chand9e47c62017-10-05 15:17:15 -0700248 /**
249 * Determines whether matching L4 destination port on IPv6 packets is supported in ACL table.
250 *
251 * @return true if matching L4 destination port on IPv6 packets is supported in ACL table.
252 */
253 protected boolean supportIpv6L4Dst() {
254 return true;
255 }
256
Saurav Dasc568c342018-01-25 09:49:01 -0800257 /**
258 * Determines whether this driver should continue to retry flows that point
259 * to empty groups. See CORD-554.
260 *
261 * @return true if the driver should retry flows
262 */
263 protected boolean shouldRetry() {
264 return true;
265 }
266
Charles Chan45b69ab2018-03-02 15:41:41 -0800267 /**
268 * Determines whether this driver requires unicast flow to be installed before multicast flow
269 * in TMAC table.
270 *
271 * @return true if required
272 */
273 protected boolean requireUnicastBeforeMulticast() {
274 return false;
275 }
276
Andrea Campanellad980c6d2018-04-30 11:48:55 +0200277 /**
278 * Determines whether this driver supports installing a clearDeferred action on table 30.
279 *
280 * @return true if required
281 */
282 protected boolean supportsUnicastBlackHole() {
283 return true;
284 }
285
Charles Chanab591602019-01-22 17:25:04 -0800286 protected boolean requirePuntTable() {
287 return false;
288 }
289
Saurav Das822c4e22015-10-23 10:51:11 -0700290 //////////////////////////////////////
291 // Flow Objectives
292 //////////////////////////////////////
293
294 @Override
295 public void filter(FilteringObjective filteringObjective) {
296 if (filteringObjective.type() == FilteringObjective.Type.PERMIT) {
297 processFilter(filteringObjective,
298 filteringObjective.op() == Objective.Operation.ADD,
299 filteringObjective.appId());
300 } else {
301 // Note that packets that don't match the PERMIT filter are
302 // automatically denied. The DENY filter is used to deny packets
303 // that are otherwise permitted by the PERMIT filter.
304 // Use ACL table flow rules here for DENY filtering objectives
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530305 log.warn("filter objective other than PERMIT currently not supported");
Saurav Das822c4e22015-10-23 10:51:11 -0700306 fail(filteringObjective, ObjectiveError.UNSUPPORTED);
307 }
308 }
309
310 @Override
311 public void forward(ForwardingObjective fwd) {
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700312 Collection<FlowRule> rules = processForward(fwd);
Saurav Das25190812016-05-27 13:54:07 -0700313 if (rules == null || rules.isEmpty()) {
314 // Assumes fail message has already been generated to the objective
315 // context. Returning here prevents spurious pass message to be
316 // generated by FlowRule service for empty flowOps.
317 return;
318 }
pier7afc7522019-05-10 13:19:15 +0200319 // Let's accumulate flow rules if accumulator is active and fwd objective is not versatile.
320 // Otherwise send directly, without adding futher delay
321 if (accumulator != null && Objects.equals(fwd.flag(), SPECIFIC)) {
pier8b3aef42019-03-11 15:14:02 -0700322 accumulator.add(Pair.of(fwd, rules));
323 } else {
324 sendForwards(Collections.singletonList(Pair.of(fwd, rules)));
325 }
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700326 }
327
pier8b3aef42019-03-11 15:14:02 -0700328 // Builds the batch using the accumulated flow rules
329 private void sendForwards(List<Pair<ForwardingObjective, Collection<FlowRule>>> pairs) {
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700330 FlowRuleOperations.Builder flowOpsBuilder = FlowRuleOperations.builder();
pier8b3aef42019-03-11 15:14:02 -0700331 log.debug("Sending {} fwd-objs", pairs.size());
332 List<Objective> fwdObjs = Lists.newArrayList();
333 // Iterates over all accumulated flow rules and then build an unique batch
334 pairs.forEach(pair -> {
335 ForwardingObjective fwd = pair.getLeft();
336 Collection<FlowRule> rules = pair.getRight();
337 switch (fwd.op()) {
338 case ADD:
339 rules.stream()
340 .filter(Objects::nonNull)
341 .forEach(flowOpsBuilder::add);
342 log.debug("Applying a add fwd-obj {} to sw:{}", fwd.id(), deviceId);
343 fwdObjs.add(fwd);
344 break;
345 case REMOVE:
346 rules.stream()
347 .filter(Objects::nonNull)
348 .forEach(flowOpsBuilder::remove);
349 log.debug("Deleting a flow rule to sw:{}", deviceId);
350 fwdObjs.add(fwd);
351 break;
352 default:
353 fail(fwd, ObjectiveError.UNKNOWN);
354 log.warn("Unknown forwarding type {}", fwd.op());
355 }
356 });
357 // Finally applies the operations
Saurav Das822c4e22015-10-23 10:51:11 -0700358 flowRuleService.apply(flowOpsBuilder.build(new FlowRuleOperationsContext() {
pier8b3aef42019-03-11 15:14:02 -0700359
Saurav Das822c4e22015-10-23 10:51:11 -0700360 @Override
361 public void onSuccess(FlowRuleOperations ops) {
pier8b3aef42019-03-11 15:14:02 -0700362 log.trace("Flow rule operations onSuccess {}", ops);
pier9469f3e2019-04-17 17:05:08 +0200363 fwdObjs.forEach(OfdpaPipelineUtility::pass);
Saurav Das822c4e22015-10-23 10:51:11 -0700364 }
365
366 @Override
367 public void onError(FlowRuleOperations ops) {
pier8b3aef42019-03-11 15:14:02 -0700368 ObjectiveError error = ObjectiveError.FLOWINSTALLATIONFAILED;
369 log.warn("Flow rule operations onError {}. Reason = {}", ops, error);
370 fwdObjs.forEach(fwdObj -> fail(fwdObj, error));
Saurav Das822c4e22015-10-23 10:51:11 -0700371 }
372 }));
Saurav Das822c4e22015-10-23 10:51:11 -0700373 }
374
375 @Override
376 public void next(NextObjective nextObjective) {
Saurav Das8a0732e2015-11-20 15:27:53 -0800377 NextGroup nextGroup = flowObjectiveStore.getNextGroup(nextObjective.id());
378 switch (nextObjective.op()) {
379 case ADD:
Saurav Das4f980082015-11-05 13:39:15 -0800380 if (nextGroup != null) {
Saurav Das8a0732e2015-11-20 15:27:53 -0800381 log.warn("Cannot add next {} that already exists in device {}",
382 nextObjective.id(), deviceId);
383 return;
384 }
Saurav Das1547b3f2017-05-05 17:01:08 -0700385 log.debug("Processing NextObjective id {} in dev {} - add group",
Saurav Das8a0732e2015-11-20 15:27:53 -0800386 nextObjective.id(), deviceId);
Charles Chan425854b2016-04-11 15:32:12 -0700387 groupHandler.addGroup(nextObjective);
Saurav Das8a0732e2015-11-20 15:27:53 -0800388 break;
389 case ADD_TO_EXISTING:
390 if (nextGroup != null) {
Saurav Das1547b3f2017-05-05 17:01:08 -0700391 log.debug("Processing NextObjective id {} in dev {} - add bucket",
Saurav Das8a0732e2015-11-20 15:27:53 -0800392 nextObjective.id(), deviceId);
Charles Chan425854b2016-04-11 15:32:12 -0700393 groupHandler.addBucketToGroup(nextObjective, nextGroup);
Saurav Das4f980082015-11-05 13:39:15 -0800394 } else {
Saurav Das8a0732e2015-11-20 15:27:53 -0800395 // it is possible that group-chain has not been fully created yet
Saurav Das423fe2b2015-12-04 10:52:59 -0800396 log.debug("Waiting to add bucket to group for next-id:{} in dev:{}",
397 nextObjective.id(), deviceId);
Yi Tseng47f82dc2017-03-05 22:48:39 -0800398
399 // by design multiple pending bucket is allowed for the group
400 groupHandler.pendingBuckets.compute(nextObjective.id(), (nextId, pendBkts) -> {
401 if (pendBkts == null) {
402 pendBkts = Sets.newHashSet();
403 }
404 pendBkts.add(nextObjective);
405 return pendBkts;
406 });
Saurav Das4f980082015-11-05 13:39:15 -0800407 }
Saurav Das8a0732e2015-11-20 15:27:53 -0800408 break;
409 case REMOVE:
410 if (nextGroup == null) {
411 log.warn("Cannot remove next {} that does not exist in device {}",
412 nextObjective.id(), deviceId);
413 return;
414 }
Saurav Das1547b3f2017-05-05 17:01:08 -0700415 log.debug("Processing NextObjective id {} in dev {} - remove group",
Saurav Das8a0732e2015-11-20 15:27:53 -0800416 nextObjective.id(), deviceId);
Charles Chan425854b2016-04-11 15:32:12 -0700417 groupHandler.removeGroup(nextObjective, nextGroup);
Saurav Das8a0732e2015-11-20 15:27:53 -0800418 break;
419 case REMOVE_FROM_EXISTING:
420 if (nextGroup == null) {
421 log.warn("Cannot remove from next {} that does not exist in device {}",
422 nextObjective.id(), deviceId);
423 return;
424 }
Saurav Das1547b3f2017-05-05 17:01:08 -0700425 log.debug("Processing NextObjective id {} in dev {} - remove bucket",
Saurav Das8a0732e2015-11-20 15:27:53 -0800426 nextObjective.id(), deviceId);
Charles Chan425854b2016-04-11 15:32:12 -0700427 groupHandler.removeBucketFromGroup(nextObjective, nextGroup);
Saurav Das8a0732e2015-11-20 15:27:53 -0800428 break;
Jonghwan Hyunf810a7a2018-02-12 16:43:45 +0900429 case MODIFY:
430 if (nextGroup == null) {
431 log.warn("Cannot modify next {} that does not exist in device {}",
432 nextObjective.id(), deviceId);
433 return;
434 }
Ruchi Sahotaef0761c2019-01-28 01:08:18 +0000435 log.debug("Processing NextObjective id {} in dev {} group {} - modify bucket",
436 nextObjective.id(), deviceId, nextGroup);
Jonghwan Hyunf810a7a2018-02-12 16:43:45 +0900437 groupHandler.modifyBucketFromGroup(nextObjective, nextGroup);
438 break;
Saurav Dasceccf242017-08-03 18:30:35 -0700439 case VERIFY:
440 if (nextGroup == null) {
441 log.warn("Cannot verify next {} that does not exist in device {}",
442 nextObjective.id(), deviceId);
443 return;
444 }
445 log.debug("Processing NextObjective id {} in dev {} - verify",
446 nextObjective.id(), deviceId);
447 groupHandler.verifyGroup(nextObjective, nextGroup);
448 break;
Saurav Das8a0732e2015-11-20 15:27:53 -0800449 default:
Saurav Das4f980082015-11-05 13:39:15 -0800450 log.warn("Unsupported operation {}", nextObjective.op());
Saurav Das822c4e22015-10-23 10:51:11 -0700451 }
452 }
453
454 //////////////////////////////////////
455 // Flow handling
456 //////////////////////////////////////
457
458 /**
Yi Tseng0fdc07e2017-04-24 19:17:08 -0700459 * As per OFDPA 2.0 TTP, filtering of VLAN ids and MAC addresses (for routing)
460 * configured on switch ports happen in different tables.
Saurav Das822c4e22015-10-23 10:51:11 -0700461 *
462 * @param filt the filtering objective
463 * @param install indicates whether to add or remove the objective
464 * @param applicationId the application that sent this objective
465 */
Saurav Das52025962016-01-28 22:30:01 -0800466 protected void processFilter(FilteringObjective filt,
467 boolean install, ApplicationId applicationId) {
Saurav Das822c4e22015-10-23 10:51:11 -0700468 // This driver only processes filtering criteria defined with switch
469 // ports as the key
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530470 PortCriterion portCriterion = null;
Saurav Das822c4e22015-10-23 10:51:11 -0700471 EthCriterion ethCriterion = null;
472 VlanIdCriterion vidCriterion = null;
Saurav Das822c4e22015-10-23 10:51:11 -0700473 if (!filt.key().equals(Criteria.dummy()) &&
474 filt.key().type() == Criterion.Type.IN_PORT) {
475 portCriterion = (PortCriterion) filt.key();
Saurav Das822c4e22015-10-23 10:51:11 -0700476 }
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530477 if (portCriterion == null) {
478 log.debug("No IN_PORT defined in filtering objective from app: {}",
479 applicationId);
480 } else {
481 log.debug("Received filtering objective for dev/port: {}/{}", deviceId,
482 portCriterion.port());
483 }
Saurav Das822c4e22015-10-23 10:51:11 -0700484 // convert filtering conditions for switch-intfs into flowrules
485 FlowRuleOperations.Builder ops = FlowRuleOperations.builder();
486 for (Criterion criterion : filt.conditions()) {
Yi Tseng0fdc07e2017-04-24 19:17:08 -0700487 switch (criterion.type()) {
488 case ETH_DST:
489 case ETH_DST_MASKED:
490 ethCriterion = (EthCriterion) criterion;
491 break;
492 case VLAN_VID:
493 vidCriterion = (VlanIdCriterion) criterion;
494 break;
495 default:
496 log.warn("Unsupported filter {}", criterion);
497 fail(filt, ObjectiveError.UNSUPPORTED);
498 return;
Saurav Das822c4e22015-10-23 10:51:11 -0700499 }
500 }
501
Saurav Das0e99e2b2015-10-28 12:39:42 -0700502 VlanId assignedVlan = null;
Charles Chane849c192016-01-11 18:28:54 -0800503 if (vidCriterion != null) {
Charles Chand55e84d2016-03-30 17:54:24 -0700504 // Use the VLAN in criterion if metadata is not present and the traffic is tagged
Charles Chanc550f2e2017-12-19 19:55:57 -0800505 if (!vidCriterion.vlanId().equals(VlanId.NONE)) {
Charles Chane849c192016-01-11 18:28:54 -0800506 assignedVlan = vidCriterion.vlanId();
Piere5bff482018-03-07 11:42:50 +0100507 }
508 // If the meta VLAN is present let's update the assigned vlan
509 if (filt.meta() != null) {
510 VlanId metaVlan = readVlanFromTreatment(filt.meta());
511 if (metaVlan != null) {
512 assignedVlan = metaVlan;
513 }
Charles Chand55e84d2016-03-30 17:54:24 -0700514 }
Charles Chane849c192016-01-11 18:28:54 -0800515
Charles Chand55e84d2016-03-30 17:54:24 -0700516 if (assignedVlan == null) {
517 log.error("Driver fails to extract VLAN information. "
Frank Wangd8ab0962017-08-11 11:09:30 +0800518 + "Not processing VLAN filters on device {}.", deviceId);
Charles Chand55e84d2016-03-30 17:54:24 -0700519 log.debug("VLAN ID in criterion={}, metadata={}",
520 readVlanFromTreatment(filt.meta()), vidCriterion.vlanId());
521 fail(filt, ObjectiveError.BADPARAMS);
522 return;
Saurav Das0e99e2b2015-10-28 12:39:42 -0700523 }
524 }
525
Yi Tseng3a77b4f2017-02-06 15:02:17 -0800526 if (ethCriterion == null || ethCriterion.mac().equals(NONE)) {
Charles Chan985b12e2016-05-11 19:47:22 -0700527 // NOTE: it is possible that a filtering objective only has vidCriterion
Daniel Ginsburgc1d47e92018-04-30 19:27:19 -0400528 log.debug("filtering objective missing dstMac, won't program TMAC table");
Saurav Das822c4e22015-10-23 10:51:11 -0700529 } else {
Charles Chan45b69ab2018-03-02 15:41:41 -0800530 MacAddress unicastMac = readEthDstFromTreatment(filt.meta());
Charles Chan66291502018-03-02 16:43:28 -0800531 List<List<FlowRule>> allStages = processEthDstFilter(portCriterion, ethCriterion,
Charles Chan45b69ab2018-03-02 15:41:41 -0800532 vidCriterion, assignedVlan, unicastMac, applicationId);
Charles Chan66291502018-03-02 16:43:28 -0800533 for (List<FlowRule> flowRules : allStages) {
Charles Chan45b69ab2018-03-02 15:41:41 -0800534 log.trace("Starting a new flow rule stage for TMAC table flow");
Charles Chan66291502018-03-02 16:43:28 -0800535 ops.newStage();
Charles Chanc550f2e2017-12-19 19:55:57 -0800536
Charles Chan66291502018-03-02 16:43:28 -0800537 for (FlowRule flowRule : flowRules) {
Charles Chan056e0c12018-05-10 22:19:49 +0000538 log.trace("{} flow rules in TMAC table: {} for dev: {}",
Charles Chan66291502018-03-02 16:43:28 -0800539 (install) ? "adding" : "removing", flowRules, deviceId);
Charles Chan66291502018-03-02 16:43:28 -0800540 if (install) {
541 ops = ops.add(flowRule);
Charles Chanc550f2e2017-12-19 19:55:57 -0800542 } else {
Charles Chan66291502018-03-02 16:43:28 -0800543 // NOTE: Only remove TMAC flow when there is no more enabled port within the
544 // same VLAN on this device if TMAC doesn't support matching on in_port.
545 if (matchInPortTmacTable() || (filt.meta() != null && filt.meta().clearedDeferred())) {
546 ops = ops.remove(flowRule);
547 } else {
548 log.debug("Abort TMAC flow removal on {}. Some other ports still share this TMAC flow");
549 }
Charles Chanc550f2e2017-12-19 19:55:57 -0800550 }
551 }
Saurav Das822c4e22015-10-23 10:51:11 -0700552 }
553 }
554
Charles Chan985b12e2016-05-11 19:47:22 -0700555 if (vidCriterion == null) {
556 // NOTE: it is possible that a filtering objective only has ethCriterion
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530557 log.info("filtering objective missing VLAN, cannot program VLAN Table");
Saurav Das822c4e22015-10-23 10:51:11 -0700558 } else {
Charles Chan66291502018-03-02 16:43:28 -0800559 List<List<FlowRule>> allStages = processVlanIdFilter(
pier6aef5b72019-06-10 17:10:26 +0200560 portCriterion, vidCriterion, assignedVlan, applicationId, install);
Charles Chan66291502018-03-02 16:43:28 -0800561 for (List<FlowRule> flowRules : allStages) {
Charles Chan45b69ab2018-03-02 15:41:41 -0800562 log.trace("Starting a new flow rule stage for VLAN table flow");
Charles Chan66291502018-03-02 16:43:28 -0800563 ops.newStage();
Charles Chan14967c22015-12-07 11:11:50 -0800564
Charles Chan66291502018-03-02 16:43:28 -0800565 for (FlowRule flowRule : flowRules) {
566 log.trace("{} flow rules in VLAN table: {} for dev: {}",
567 (install) ? "adding" : "removing", flowRule, deviceId);
568 ops = install ? ops.add(flowRule) : ops.remove(flowRule);
Charles Chand1172632017-03-15 17:33:09 -0700569 }
Saurav Das822c4e22015-10-23 10:51:11 -0700570 }
571 }
572
Saurav Das822c4e22015-10-23 10:51:11 -0700573 // apply filtering flow rules
574 flowRuleService.apply(ops.build(new FlowRuleOperationsContext() {
575 @Override
576 public void onSuccess(FlowRuleOperations ops) {
Saurav Das018605f2017-02-18 14:05:44 -0800577 log.debug("Applied {} filtering rules in device {}",
Saurav Das822c4e22015-10-23 10:51:11 -0700578 ops.stages().get(0).size(), deviceId);
579 pass(filt);
580 }
581
582 @Override
583 public void onError(FlowRuleOperations ops) {
584 log.info("Failed to apply all filtering rules in dev {}", deviceId);
585 fail(filt, ObjectiveError.FLOWINSTALLATIONFAILED);
586 }
587 }));
588
589 }
590
591 /**
Charles Chand1172632017-03-15 17:33:09 -0700592 * Internal implementation of processVlanIdFilter.
593 * <p>
594 * The is_present bit in set_vlan_vid action is required to be 0 in OFDPA i12.
595 * Since it is non-OF spec, we need an extension treatment for that.
596 * The useVlanExtension must be set to false for OFDPA i12.
597 * </p>
Charles Chan66291502018-03-02 16:43:28 -0800598 * <p>
599 * NOTE: Separate VLAN filtering rules and assignment rules
600 * into different stages in order to guarantee that filtering rules
601 * always go first, as required by OFDPA.
602 * </p>
Saurav Das0e99e2b2015-10-28 12:39:42 -0700603 *
Charles Chanf9e98652016-09-07 16:54:23 -0700604 * @param portCriterion port on device for which this filter is programmed
605 * @param vidCriterion vlan assigned to port, or NONE for untagged
606 * @param assignedVlan assigned vlan-id for untagged packets
607 * @param applicationId for application programming this filter
pier6aef5b72019-06-10 17:10:26 +0200608 * @param install indicates whether to add or remove the objective
Charles Chan66291502018-03-02 16:43:28 -0800609 * @return stages of flow rules for port-vlan filters
Saurav Das822c4e22015-10-23 10:51:11 -0700610 */
Charles Chan66291502018-03-02 16:43:28 -0800611 protected List<List<FlowRule>> processVlanIdFilter(PortCriterion portCriterion,
pier6aef5b72019-06-10 17:10:26 +0200612 VlanIdCriterion vidCriterion,
613 VlanId assignedVlan,
614 ApplicationId applicationId,
615 boolean install) {
Charles Chan66291502018-03-02 16:43:28 -0800616 List<FlowRule> filteringRules = new ArrayList<>();
617 List<FlowRule> assignmentRules = new ArrayList<>();
Saurav Das822c4e22015-10-23 10:51:11 -0700618 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
619 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
Charles Chan14967c22015-12-07 11:11:50 -0800620 TrafficSelector.Builder preSelector = null;
621 TrafficTreatment.Builder preTreatment = null;
622
Saurav Das4f980082015-11-05 13:39:15 -0800623 treatment.transition(TMAC_TABLE);
624
Saurav Das822c4e22015-10-23 10:51:11 -0700625 if (vidCriterion.vlanId() == VlanId.NONE) {
626 // untagged packets are assigned vlans
Charles Chand1172632017-03-15 17:33:09 -0700627 preSelector = DefaultTrafficSelector.builder();
628 if (requireVlanExtensions()) {
629 OfdpaMatchVlanVid ofdpaMatchVlanVid = new OfdpaMatchVlanVid(VlanId.NONE);
630 selector.extension(ofdpaMatchVlanVid, deviceId);
Charles Chanf9e98652016-09-07 16:54:23 -0700631 OfdpaSetVlanVid ofdpaSetVlanVid = new OfdpaSetVlanVid(assignedVlan);
632 treatment.extension(ofdpaSetVlanVid, deviceId);
Charles Chand1172632017-03-15 17:33:09 -0700633
634 OfdpaMatchVlanVid preOfdpaMatchVlanVid = new OfdpaMatchVlanVid(assignedVlan);
635 preSelector.extension(preOfdpaMatchVlanVid, deviceId);
Charles Chanf9e98652016-09-07 16:54:23 -0700636 } else {
Charles Chand1172632017-03-15 17:33:09 -0700637 selector.matchVlanId(VlanId.NONE);
Charles Chanf9e98652016-09-07 16:54:23 -0700638 treatment.setVlanId(assignedVlan);
Charles Chand1172632017-03-15 17:33:09 -0700639
640 preSelector.matchVlanId(assignedVlan);
641 }
642 preTreatment = DefaultTrafficTreatment.builder().transition(TMAC_TABLE);
643 } else {
644 if (requireVlanExtensions()) {
645 OfdpaMatchVlanVid ofdpaMatchVlanVid = new OfdpaMatchVlanVid(vidCriterion.vlanId());
646 selector.extension(ofdpaMatchVlanVid, deviceId);
647 } else {
648 selector.matchVlanId(vidCriterion.vlanId());
Charles Chanf9e98652016-09-07 16:54:23 -0700649 }
Charles Chan14967c22015-12-07 11:11:50 -0800650
Charles Chand55e84d2016-03-30 17:54:24 -0700651 if (!assignedVlan.equals(vidCriterion.vlanId())) {
Charles Chand1172632017-03-15 17:33:09 -0700652 if (requireVlanExtensions()) {
Charles Chanc03782d2017-01-31 13:57:55 -0800653 OfdpaSetVlanVid ofdpaSetVlanVid = new OfdpaSetVlanVid(assignedVlan);
654 treatment.extension(ofdpaSetVlanVid, deviceId);
655 } else {
656 treatment.setVlanId(assignedVlan);
657 }
Charles Chand55e84d2016-03-30 17:54:24 -0700658 }
Saurav Das822c4e22015-10-23 10:51:11 -0700659 }
Saurav Das822c4e22015-10-23 10:51:11 -0700660
661 // ofdpa cannot match on ALL portnumber, so we need to use separate
662 // rules for each port.
Charles Chan14967c22015-12-07 11:11:50 -0800663 List<PortNumber> portnums = new ArrayList<>();
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530664 if (portCriterion != null) {
665 if (PortNumber.ALL.equals(portCriterion.port())) {
666 for (Port port : deviceService.getPorts(deviceId)) {
667 if (port.number().toLong() > 0 && port.number().toLong() < OFPP_MAX) {
668 portnums.add(port.number());
669 }
Saurav Das822c4e22015-10-23 10:51:11 -0700670 }
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530671 } else {
672 portnums.add(portCriterion.port());
Saurav Das822c4e22015-10-23 10:51:11 -0700673 }
674 } else {
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530675 log.warn("Filtering Objective missing Port Criterion . " +
Charles Chan66291502018-03-02 16:43:28 -0800676 "VLAN Table cannot be programmed for {}", deviceId);
Saurav Das822c4e22015-10-23 10:51:11 -0700677 }
Saurav Das4f980082015-11-05 13:39:15 -0800678
Saurav Das822c4e22015-10-23 10:51:11 -0700679 for (PortNumber pnum : portnums) {
Saurav Das4f980082015-11-05 13:39:15 -0800680 // create rest of flowrule
Saurav Das822c4e22015-10-23 10:51:11 -0700681 selector.matchInPort(pnum);
682 FlowRule rule = DefaultFlowRule.builder()
683 .forDevice(deviceId)
684 .withSelector(selector.build())
685 .withTreatment(treatment.build())
686 .withPriority(DEFAULT_PRIORITY)
687 .fromApp(applicationId)
688 .makePermanent()
689 .forTable(VLAN_TABLE).build();
Charles Chan66291502018-03-02 16:43:28 -0800690 assignmentRules.add(rule);
Charles Chan14967c22015-12-07 11:11:50 -0800691
692 if (preSelector != null) {
693 preSelector.matchInPort(pnum);
694 FlowRule preRule = DefaultFlowRule.builder()
695 .forDevice(deviceId)
696 .withSelector(preSelector.build())
697 .withTreatment(preTreatment.build())
698 .withPriority(DEFAULT_PRIORITY)
699 .fromApp(applicationId)
700 .makePermanent()
701 .forTable(VLAN_TABLE).build();
Charles Chan66291502018-03-02 16:43:28 -0800702 filteringRules.add(preRule);
Charles Chan14967c22015-12-07 11:11:50 -0800703 }
Saurav Das822c4e22015-10-23 10:51:11 -0700704 }
pier6aef5b72019-06-10 17:10:26 +0200705 return install ? ImmutableList.of(filteringRules, assignmentRules) :
706 ImmutableList.of(assignmentRules, filteringRules);
Saurav Das822c4e22015-10-23 10:51:11 -0700707 }
708
709 /**
710 * Allows routed packets with correct destination MAC to be directed
Charles Chan45b69ab2018-03-02 15:41:41 -0800711 * to unicast routing table, multicast routing table or MPLS forwarding table.
Saurav Das822c4e22015-10-23 10:51:11 -0700712 *
713 * @param portCriterion port on device for which this filter is programmed
714 * @param ethCriterion dstMac of device for which is filter is programmed
715 * @param vidCriterion vlan assigned to port, or NONE for untagged
Saurav Das0e99e2b2015-10-28 12:39:42 -0700716 * @param assignedVlan assigned vlan-id for untagged packets
Charles Chan45b69ab2018-03-02 15:41:41 -0800717 * @param unicastMac some switches require a unicast TMAC flow to be programmed before multicast
718 * TMAC flow. This MAC address will be used for the unicast TMAC flow.
719 * This is unused if the filtering objective is a unicast.
Saurav Das822c4e22015-10-23 10:51:11 -0700720 * @param applicationId for application programming this filter
Charles Chan66291502018-03-02 16:43:28 -0800721 * @return stages of flow rules for port-vlan filters
Saurav Das822c4e22015-10-23 10:51:11 -0700722
723 */
Charles Chan66291502018-03-02 16:43:28 -0800724 protected List<List<FlowRule>> processEthDstFilter(PortCriterion portCriterion,
Saurav Das822c4e22015-10-23 10:51:11 -0700725 EthCriterion ethCriterion,
726 VlanIdCriterion vidCriterion,
Saurav Das0e99e2b2015-10-28 12:39:42 -0700727 VlanId assignedVlan,
Charles Chan45b69ab2018-03-02 15:41:41 -0800728 MacAddress unicastMac,
Saurav Das822c4e22015-10-23 10:51:11 -0700729 ApplicationId applicationId) {
Charles Chan5270ed02016-01-30 23:22:37 -0800730 // Consider PortNumber.ANY as wildcard. Match ETH_DST only
Sivachidambaram Subramanianc598d062017-05-16 22:42:06 +0530731 if (portCriterion != null && PortNumber.ANY.equals(portCriterion.port())) {
Charles Chan5270ed02016-01-30 23:22:37 -0800732 return processEthDstOnlyFilter(ethCriterion, applicationId);
733 }
734
Charles Chan5b9df8d2016-03-28 22:21:40 -0700735 // Multicast MAC
736 if (ethCriterion.mask() != null) {
Charles Chan45b69ab2018-03-02 15:41:41 -0800737 return processMcastEthDstFilter(ethCriterion, assignedVlan, unicastMac, applicationId);
Charles Chan5b9df8d2016-03-28 22:21:40 -0700738 }
739
Saurav Das822c4e22015-10-23 10:51:11 -0700740 //handling untagged packets via assigned VLAN
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530741 if (vidCriterion != null && vidCriterion.vlanId() == VlanId.NONE) {
Saurav Das0e99e2b2015-10-28 12:39:42 -0700742 vidCriterion = (VlanIdCriterion) Criteria.matchVlanId(assignedVlan);
Saurav Das822c4e22015-10-23 10:51:11 -0700743 }
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530744 List<FlowRule> rules = new ArrayList<>();
745 OfdpaMatchVlanVid ofdpaMatchVlanVid = null;
746 if (vidCriterion != null && requireVlanExtensions()) {
747 ofdpaMatchVlanVid = new OfdpaMatchVlanVid(vidCriterion.vlanId());
748 }
Saurav Das822c4e22015-10-23 10:51:11 -0700749 // ofdpa cannot match on ALL portnumber, so we need to use separate
750 // rules for each port.
Yuta HIGUCHI2dce08a2017-04-20 21:57:48 -0700751 List<PortNumber> portnums = new ArrayList<>();
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530752 if (portCriterion != null) {
753 if (PortNumber.ALL.equals(portCriterion.port())) {
754 for (Port port : deviceService.getPorts(deviceId)) {
755 if (port.number().toLong() > 0 && port.number().toLong() < OFPP_MAX) {
756 portnums.add(port.number());
757 }
Saurav Das822c4e22015-10-23 10:51:11 -0700758 }
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530759 } else {
760 portnums.add(portCriterion.port());
761 }
762 for (PortNumber pnum : portnums) {
763 rules.add(buildTmacRuleForIpv4(ethCriterion,
764 vidCriterion,
765 ofdpaMatchVlanVid,
766 applicationId,
767 pnum));
768 rules.add(buildTmacRuleForMpls(ethCriterion,
769 vidCriterion,
770 ofdpaMatchVlanVid,
771 applicationId,
772 pnum));
773 rules.add(buildTmacRuleForIpv6(ethCriterion,
774 vidCriterion,
775 ofdpaMatchVlanVid,
776 applicationId,
777 pnum));
Saurav Das822c4e22015-10-23 10:51:11 -0700778 }
779 } else {
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530780 rules.add(buildTmacRuleForIpv4(ethCriterion,
781 vidCriterion,
782 ofdpaMatchVlanVid,
783 applicationId,
784 null));
785 rules.add(buildTmacRuleForMpls(ethCriterion,
786 vidCriterion,
787 ofdpaMatchVlanVid,
788 applicationId,
789 null));
790 rules.add(buildTmacRuleForIpv6(ethCriterion,
791 vidCriterion,
792 ofdpaMatchVlanVid,
793 applicationId,
794 null));
Saurav Das822c4e22015-10-23 10:51:11 -0700795 }
Charles Chan66291502018-03-02 16:43:28 -0800796 return ImmutableList.of(rules);
Saurav Das822c4e22015-10-23 10:51:11 -0700797 }
798
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530799 /**
800 * Builds TMAC rules for IPv4 packets.
801 *
Charles Chanc550f2e2017-12-19 19:55:57 -0800802 * @param ethCriterion dst mac matching
803 * @param vidCriterion vlan id assigned to the port
804 * @param ofdpaMatchVlanVid OFDPA vlan id matching
805 * @param applicationId application id
806 * @param pnum port number
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530807 * @return TMAC rule for IPV4 packets
808 */
809 private FlowRule buildTmacRuleForIpv4(EthCriterion ethCriterion,
810 VlanIdCriterion vidCriterion,
811 OfdpaMatchVlanVid ofdpaMatchVlanVid,
812 ApplicationId applicationId,
813 PortNumber pnum) {
814 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
815 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
816 if (pnum != null) {
817 if (matchInPortTmacTable()) {
818 selector.matchInPort(pnum);
819 } else {
Saurav Dasf14d9ef2017-12-05 15:00:23 -0800820 log.debug("Pipeline does not support IN_PORT matching in TMAC table, " +
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530821 "ignoring the IN_PORT criteria");
822 }
823 }
824 if (vidCriterion != null) {
825 if (requireVlanExtensions()) {
826 selector.extension(ofdpaMatchVlanVid, deviceId);
827 } else {
828 selector.matchVlanId(vidCriterion.vlanId());
829 }
830 }
831 selector.matchEthType(Ethernet.TYPE_IPV4);
832 selector.matchEthDst(ethCriterion.mac());
833 treatment.transition(UNICAST_ROUTING_TABLE);
834 return DefaultFlowRule.builder()
835 .forDevice(deviceId)
836 .withSelector(selector.build())
837 .withTreatment(treatment.build())
838 .withPriority(DEFAULT_PRIORITY)
839 .fromApp(applicationId)
840 .makePermanent()
841 .forTable(TMAC_TABLE).build();
842 }
843
844 /**
845 * Builds TMAC rule for MPLS packets.
846 *
Charles Chanc550f2e2017-12-19 19:55:57 -0800847 * @param ethCriterion dst mac matching
848 * @param vidCriterion vlan id assigned to the port
849 * @param ofdpaMatchVlanVid OFDPA vlan id matching
850 * @param applicationId application id
851 * @param pnum port number
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530852 * @return TMAC rule for MPLS packets
853 */
854 private FlowRule buildTmacRuleForMpls(EthCriterion ethCriterion,
855 VlanIdCriterion vidCriterion,
856 OfdpaMatchVlanVid ofdpaMatchVlanVid,
857 ApplicationId applicationId,
858 PortNumber pnum) {
859 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
860 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
861 if (pnum != null) {
862 if (matchInPortTmacTable()) {
863 selector.matchInPort(pnum);
864 } else {
Saurav Dasf14d9ef2017-12-05 15:00:23 -0800865 log.debug("Pipeline does not support IN_PORT matching in TMAC table, " +
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530866 "ignoring the IN_PORT criteria");
867 }
868 }
869 if (vidCriterion != null) {
870 if (requireVlanExtensions()) {
871 selector.extension(ofdpaMatchVlanVid, deviceId);
872 } else {
873 selector.matchVlanId(vidCriterion.vlanId());
874 }
875 }
876 selector.matchEthType(Ethernet.MPLS_UNICAST);
877 selector.matchEthDst(ethCriterion.mac());
878 treatment.transition(MPLS_TABLE_0);
879 return DefaultFlowRule.builder()
880 .forDevice(deviceId)
881 .withSelector(selector.build())
882 .withTreatment(treatment.build())
883 .withPriority(DEFAULT_PRIORITY)
884 .fromApp(applicationId)
885 .makePermanent()
886 .forTable(TMAC_TABLE).build();
887 }
888
889 /**
890 * Builds TMAC rules for IPv6 packets.
891 *
Charles Chanc550f2e2017-12-19 19:55:57 -0800892 * @param ethCriterion dst mac matching
893 * @param vidCriterion vlan id assigned to the port
894 * @param ofdpaMatchVlanVid OFDPA vlan id matching
895 * @param applicationId application id
896 * @param pnum port number
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530897 * @return TMAC rule for IPV6 packets
898 */
899 private FlowRule buildTmacRuleForIpv6(EthCriterion ethCriterion,
900 VlanIdCriterion vidCriterion,
901 OfdpaMatchVlanVid ofdpaMatchVlanVid,
902 ApplicationId applicationId,
903 PortNumber pnum) {
904 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
905 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
906 if (pnum != null) {
907 if (matchInPortTmacTable()) {
908 selector.matchInPort(pnum);
909 } else {
Saurav Dasf14d9ef2017-12-05 15:00:23 -0800910 log.debug("Pipeline does not support IN_PORT matching in TMAC table, " +
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530911 "ignoring the IN_PORT criteria");
912 }
913 }
914 if (vidCriterion != null) {
915 if (requireVlanExtensions()) {
916 selector.extension(ofdpaMatchVlanVid, deviceId);
917 } else {
918 selector.matchVlanId(vidCriterion.vlanId());
919 }
920 }
921 selector.matchEthType(Ethernet.TYPE_IPV6);
922 selector.matchEthDst(ethCriterion.mac());
923 treatment.transition(UNICAST_ROUTING_TABLE);
924 return DefaultFlowRule.builder()
925 .forDevice(deviceId)
926 .withSelector(selector.build())
927 .withTreatment(treatment.build())
928 .withPriority(DEFAULT_PRIORITY)
929 .fromApp(applicationId)
930 .makePermanent()
931 .forTable(TMAC_TABLE).build();
932 }
933
Charles Chan66291502018-03-02 16:43:28 -0800934 protected List<List<FlowRule>> processEthDstOnlyFilter(EthCriterion ethCriterion,
Yi Tsengef19de12017-04-24 11:33:05 -0700935 ApplicationId applicationId) {
Pier Luigi0e358632017-01-31 09:35:05 -0800936 ImmutableList.Builder<FlowRule> builder = ImmutableList.builder();
937
Charles Chan5270ed02016-01-30 23:22:37 -0800938 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
939 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
940 selector.matchEthType(Ethernet.TYPE_IPV4);
941 selector.matchEthDst(ethCriterion.mac());
942 treatment.transition(UNICAST_ROUTING_TABLE);
943 FlowRule rule = DefaultFlowRule.builder()
944 .forDevice(deviceId)
945 .withSelector(selector.build())
946 .withTreatment(treatment.build())
947 .withPriority(DEFAULT_PRIORITY)
948 .fromApp(applicationId)
949 .makePermanent()
950 .forTable(TMAC_TABLE).build();
Pier Luigi0e358632017-01-31 09:35:05 -0800951 builder.add(rule);
952
953 selector = DefaultTrafficSelector.builder();
954 treatment = DefaultTrafficTreatment.builder();
955 selector.matchEthType(Ethernet.TYPE_IPV6);
956 selector.matchEthDst(ethCriterion.mac());
957 treatment.transition(UNICAST_ROUTING_TABLE);
958 rule = DefaultFlowRule.builder()
959 .forDevice(deviceId)
960 .withSelector(selector.build())
961 .withTreatment(treatment.build())
962 .withPriority(DEFAULT_PRIORITY)
963 .fromApp(applicationId)
964 .makePermanent()
965 .forTable(TMAC_TABLE).build();
Charles Chan66291502018-03-02 16:43:28 -0800966 return ImmutableList.of(builder.add(rule).build());
Charles Chan5270ed02016-01-30 23:22:37 -0800967 }
968
Charles Chan66291502018-03-02 16:43:28 -0800969 List<List<FlowRule>> processMcastEthDstFilter(EthCriterion ethCriterion,
Charles Chanb4879a52017-10-20 19:09:16 -0700970 VlanId assignedVlan,
Charles Chan45b69ab2018-03-02 15:41:41 -0800971 MacAddress unicastMac,
Yi Tsengef19de12017-04-24 11:33:05 -0700972 ApplicationId applicationId) {
Charles Chan45b69ab2018-03-02 15:41:41 -0800973 ImmutableList.Builder<FlowRule> unicastFlows = ImmutableList.builder();
974 ImmutableList.Builder<FlowRule> multicastFlows = ImmutableList.builder();
975 TrafficSelector.Builder selector;
976 TrafficTreatment.Builder treatment;
Charles Chanb4879a52017-10-20 19:09:16 -0700977 FlowRule rule;
Julia Ferguson65428c32017-08-10 18:15:24 +0000978
Charles Chanb4879a52017-10-20 19:09:16 -0700979 if (IPV4_MULTICAST.equals(ethCriterion.mac())) {
Charles Chan45b69ab2018-03-02 15:41:41 -0800980 if (requireUnicastBeforeMulticast()) {
981 selector = DefaultTrafficSelector.builder();
982 treatment = DefaultTrafficTreatment.builder();
983 selector.matchEthType(Ethernet.TYPE_IPV4);
984 selector.matchEthDst(unicastMac);
Charles Chan056e0c12018-05-10 22:19:49 +0000985 selector.matchVlanId(assignedVlan);
Charles Chan45b69ab2018-03-02 15:41:41 -0800986 treatment.transition(UNICAST_ROUTING_TABLE);
987 rule = DefaultFlowRule.builder()
988 .forDevice(deviceId)
989 .withSelector(selector.build())
990 .withTreatment(treatment.build())
Charles Chan056e0c12018-05-10 22:19:49 +0000991 .withPriority(DEFAULT_PRIORITY)
Charles Chan45b69ab2018-03-02 15:41:41 -0800992 .fromApp(applicationId)
993 .makePermanent()
994 .forTable(TMAC_TABLE).build();
995 unicastFlows.add(rule);
996 }
997
998 selector = DefaultTrafficSelector.builder();
999 treatment = DefaultTrafficTreatment.builder();
Charles Chanb4879a52017-10-20 19:09:16 -07001000 selector.matchEthType(Ethernet.TYPE_IPV4);
1001 selector.matchEthDstMasked(ethCriterion.mac(), ethCriterion.mask());
Charles Chan056e0c12018-05-10 22:19:49 +00001002 selector.matchVlanId(assignedVlan);
Charles Chanb4879a52017-10-20 19:09:16 -07001003 treatment.transition(MULTICAST_ROUTING_TABLE);
1004 rule = DefaultFlowRule.builder()
1005 .forDevice(deviceId)
1006 .withSelector(selector.build())
1007 .withTreatment(treatment.build())
Charles Chan056e0c12018-05-10 22:19:49 +00001008 .withPriority(DEFAULT_PRIORITY)
Charles Chanb4879a52017-10-20 19:09:16 -07001009 .fromApp(applicationId)
1010 .makePermanent()
1011 .forTable(TMAC_TABLE).build();
Charles Chan45b69ab2018-03-02 15:41:41 -08001012 multicastFlows.add(rule);
Charles Chanb4879a52017-10-20 19:09:16 -07001013 }
1014
1015 if (IPV6_MULTICAST.equals(ethCriterion.mac())) {
Charles Chan45b69ab2018-03-02 15:41:41 -08001016 if (requireUnicastBeforeMulticast()) {
1017 selector = DefaultTrafficSelector.builder();
1018 treatment = DefaultTrafficTreatment.builder();
1019 selector.matchEthType(Ethernet.TYPE_IPV6);
1020 selector.matchEthDst(unicastMac);
Charles Chan056e0c12018-05-10 22:19:49 +00001021 selector.matchVlanId(assignedVlan);
Charles Chan45b69ab2018-03-02 15:41:41 -08001022 treatment.transition(UNICAST_ROUTING_TABLE);
1023 rule = DefaultFlowRule.builder()
1024 .forDevice(deviceId)
1025 .withSelector(selector.build())
1026 .withTreatment(treatment.build())
Charles Chan056e0c12018-05-10 22:19:49 +00001027 .withPriority(DEFAULT_PRIORITY)
Charles Chan45b69ab2018-03-02 15:41:41 -08001028 .fromApp(applicationId)
1029 .makePermanent()
1030 .forTable(TMAC_TABLE).build();
1031 unicastFlows.add(rule);
1032 }
1033
Charles Chanb4879a52017-10-20 19:09:16 -07001034 selector = DefaultTrafficSelector.builder();
1035 treatment = DefaultTrafficTreatment.builder();
1036 selector.matchEthType(Ethernet.TYPE_IPV6);
1037 selector.matchEthDstMasked(ethCriterion.mac(), ethCriterion.mask());
Charles Chan056e0c12018-05-10 22:19:49 +00001038 selector.matchVlanId(assignedVlan);
Charles Chanb4879a52017-10-20 19:09:16 -07001039 treatment.transition(MULTICAST_ROUTING_TABLE);
1040 rule = DefaultFlowRule.builder()
1041 .forDevice(deviceId)
1042 .withSelector(selector.build())
1043 .withTreatment(treatment.build())
Charles Chan056e0c12018-05-10 22:19:49 +00001044 .withPriority(DEFAULT_PRIORITY)
Charles Chanb4879a52017-10-20 19:09:16 -07001045 .fromApp(applicationId)
1046 .makePermanent()
1047 .forTable(TMAC_TABLE).build();
Charles Chan45b69ab2018-03-02 15:41:41 -08001048 multicastFlows.add(rule);
Charles Chanb4879a52017-10-20 19:09:16 -07001049 }
Charles Chan45b69ab2018-03-02 15:41:41 -08001050 return ImmutableList.of(unicastFlows.build(), multicastFlows.build());
Charles Chan5b9df8d2016-03-28 22:21:40 -07001051 }
1052
Saurav Das822c4e22015-10-23 10:51:11 -07001053 private Collection<FlowRule> processForward(ForwardingObjective fwd) {
1054 switch (fwd.flag()) {
1055 case SPECIFIC:
1056 return processSpecific(fwd);
1057 case VERSATILE:
1058 return processVersatile(fwd);
Jonghwan Hyun800d9d02018-04-09 09:40:50 -07001059 case EGRESS:
1060 return processEgress(fwd);
Saurav Das822c4e22015-10-23 10:51:11 -07001061 default:
1062 fail(fwd, ObjectiveError.UNKNOWN);
1063 log.warn("Unknown forwarding flag {}", fwd.flag());
1064 }
1065 return Collections.emptySet();
1066 }
1067
1068 /**
Jonghwan Hyun800d9d02018-04-09 09:40:50 -07001069 * In the OF-DPA 2.0 pipeline, egress forwarding objectives go to the
1070 * egress tables.
1071 * @param fwd the forwarding objective of type 'egress'
1072 * @return a collection of flow rules to be sent to the switch. An empty
1073 * collection may be returned if there is a problem in processing
1074 * the flow rule
1075 */
1076 protected Collection<FlowRule> processEgress(ForwardingObjective fwd) {
1077 log.debug("Processing egress forwarding objective:{} in dev:{}",
1078 fwd, deviceId);
1079
1080 List<FlowRule> rules = new ArrayList<>();
1081
1082 // Build selector
1083 TrafficSelector.Builder sb = DefaultTrafficSelector.builder();
1084 VlanIdCriterion vlanIdCriterion = (VlanIdCriterion) fwd.selector().getCriterion(Criterion.Type.VLAN_VID);
1085 if (vlanIdCriterion == null) {
1086 log.error("Egress forwarding objective:{} must include vlanId", fwd.id());
1087 fail(fwd, ObjectiveError.BADPARAMS);
1088 return rules;
1089 }
1090
1091 Optional<Instruction> outInstr = fwd.treatment().allInstructions().stream()
1092 .filter(instruction -> instruction instanceof OutputInstruction).findFirst();
1093 if (!outInstr.isPresent()) {
1094 log.error("Egress forwarding objective:{} must include output port", fwd.id());
1095 fail(fwd, ObjectiveError.BADPARAMS);
1096 return rules;
1097 }
1098
1099 PortNumber portNumber = ((OutputInstruction) outInstr.get()).port();
1100
1101 sb.matchVlanId(vlanIdCriterion.vlanId());
1102 OfdpaMatchActsetOutput actsetOutput = new OfdpaMatchActsetOutput(portNumber);
1103 sb.extension(actsetOutput, deviceId);
1104
1105 sb.extension(new OfdpaMatchAllowVlanTranslation(ALLOW_VLAN_TRANSLATION), deviceId);
1106
1107 // Build a flow rule for Egress VLAN Flow table
1108 TrafficTreatment.Builder tb = DefaultTrafficTreatment.builder();
1109 tb.transition(EGRESS_DSCP_PCP_REMARK_FLOW_TABLE);
1110 if (fwd.treatment() != null) {
1111 for (Instruction instr : fwd.treatment().allInstructions()) {
1112 if (instr instanceof L2ModificationInstruction &&
1113 ((L2ModificationInstruction) instr).subtype() == L2SubType.VLAN_ID) {
1114 tb.immediate().add(instr);
1115 }
1116 if (instr instanceof L2ModificationInstruction &&
1117 ((L2ModificationInstruction) instr).subtype() == L2SubType.VLAN_PUSH) {
1118 tb.immediate().pushVlan();
1119 EthType ethType = ((L2ModificationInstruction.ModVlanHeaderInstruction) instr).ethernetType();
1120 if (ethType.equals(EtherType.QINQ.ethType())) {
1121 // Build a flow rule for Egress TPID Flow table
1122 TrafficSelector tpidSelector = DefaultTrafficSelector.builder()
1123 .extension(actsetOutput, deviceId)
1124 .matchVlanId(VlanId.ANY).build();
1125
1126 TrafficTreatment tpidTreatment = DefaultTrafficTreatment.builder()
1127 .extension(new Ofdpa3CopyField(COPY_FIELD_NBITS, COPY_FIELD_OFFSET,
1128 COPY_FIELD_OFFSET, OXM_ID_VLAN_VID,
1129 OXM_ID_PACKET_REG_1),
1130 deviceId)
1131 .popVlan()
1132 .pushVlan(EtherType.QINQ.ethType())
1133 .extension(new Ofdpa3CopyField(COPY_FIELD_NBITS, COPY_FIELD_OFFSET,
1134 COPY_FIELD_OFFSET, OXM_ID_PACKET_REG_1,
1135 OXM_ID_VLAN_VID),
1136 deviceId)
1137 .build();
1138
1139 FlowRule.Builder tpidRuleBuilder = DefaultFlowRule.builder()
1140 .fromApp(fwd.appId())
1141 .withPriority(fwd.priority())
1142 .forDevice(deviceId)
1143 .withSelector(tpidSelector)
1144 .withTreatment(tpidTreatment)
1145 .makePermanent()
1146 .forTable(EGRESS_TPID_FLOW_TABLE);
1147 rules.add(tpidRuleBuilder.build());
1148 }
1149 }
1150 }
1151 }
1152
1153 FlowRule.Builder ruleBuilder = DefaultFlowRule.builder()
1154 .fromApp(fwd.appId())
1155 .withPriority(fwd.priority())
1156 .forDevice(deviceId)
1157 .withSelector(sb.build())
1158 .withTreatment(tb.build())
1159 .makePermanent()
1160 .forTable(EGRESS_VLAN_FLOW_TABLE);
1161 rules.add(ruleBuilder.build());
1162 return rules;
1163 }
1164
1165 /**
Saurav Das822c4e22015-10-23 10:51:11 -07001166 * In the OF-DPA 2.0 pipeline, versatile forwarding objectives go to the
1167 * ACL table.
1168 * @param fwd the forwarding objective of type 'versatile'
1169 * @return a collection of flow rules to be sent to the switch. An empty
1170 * collection may be returned if there is a problem in processing
1171 * the flow rule
1172 */
Saurav Das52025962016-01-28 22:30:01 -08001173 protected Collection<FlowRule> processVersatile(ForwardingObjective fwd) {
Saurav Das018605f2017-02-18 14:05:44 -08001174 log.debug("Processing versatile forwarding objective:{} in dev:{}",
Saurav Dasd2fded02016-12-02 15:43:47 -08001175 fwd.id(), deviceId);
Saurav Das822c4e22015-10-23 10:51:11 -07001176
Saurav Das822c4e22015-10-23 10:51:11 -07001177 if (fwd.nextId() == null && fwd.treatment() == null) {
1178 log.error("Forwarding objective {} from {} must contain "
1179 + "nextId or Treatment", fwd.selector(), fwd.appId());
Zsolt Haraszti9faab752016-02-17 15:55:20 -08001180 fail(fwd, ObjectiveError.BADPARAMS);
Saurav Das822c4e22015-10-23 10:51:11 -07001181 return Collections.emptySet();
1182 }
Saurav Das49cb5a12016-01-16 22:54:07 -08001183
Charles Chanab591602019-01-22 17:25:04 -08001184 TrafficSelector.Builder sbuilder = versatileSelectorBuilder(fwd);
1185 TrafficTreatment.Builder ttBuilder = versatileTreatmentBuilder(fwd);
1186 if (ttBuilder == null) {
1187 return Collections.emptySet();
1188 }
1189
1190 FlowRule.Builder ruleBuilder = DefaultFlowRule.builder()
1191 .fromApp(fwd.appId())
1192 .withPriority(fwd.priority())
1193 .forDevice(deviceId)
1194 .withSelector(sbuilder.build())
1195 .withTreatment(ttBuilder.build())
1196 .makePermanent()
1197 .forTable(ACL_TABLE);
1198 return Collections.singletonList(ruleBuilder.build());
1199 }
1200
1201 /**
1202 * Helper function to create traffic selector builder for versatile forwarding objectives.
1203 *
1204 * @param fwd original forwarding objective
1205 * @return selector builder for the flow rule
1206 */
1207 protected TrafficSelector.Builder versatileSelectorBuilder(ForwardingObjective fwd) {
Saurav Das77b5e902016-01-27 17:01:59 -08001208 TrafficSelector.Builder sbuilder = DefaultTrafficSelector.builder();
1209 fwd.selector().criteria().forEach(criterion -> {
1210 if (criterion instanceof VlanIdCriterion) {
1211 VlanId vlanId = ((VlanIdCriterion) criterion).vlanId();
1212 // ensure that match does not include vlan = NONE as OF-DPA does not
1213 // match untagged packets this way in the ACL table.
1214 if (vlanId.equals(VlanId.NONE)) {
1215 return;
1216 }
Charles Chand1172632017-03-15 17:33:09 -07001217 if (requireVlanExtensions()) {
1218 OfdpaMatchVlanVid ofdpaMatchVlanVid = new OfdpaMatchVlanVid(vlanId);
1219 sbuilder.extension(ofdpaMatchVlanVid, deviceId);
1220 } else {
1221 sbuilder.matchVlanId(vlanId);
1222 }
Charles Chan09bf2692018-01-11 11:48:18 -08001223 } else if (criterion instanceof Icmpv6TypeCriterion) {
1224 byte icmpv6Type = (byte) ((Icmpv6TypeCriterion) criterion).icmpv6Type();
1225 sbuilder.matchIcmpv6Type(icmpv6Type);
1226 } else if (criterion instanceof Icmpv6CodeCriterion) {
1227 byte icmpv6Code = (byte) ((Icmpv6CodeCriterion) criterion).icmpv6Code();
1228 sbuilder.matchIcmpv6Type(icmpv6Code);
Charles Chand9e47c62017-10-05 15:17:15 -07001229 } else if (criterion instanceof TcpPortCriterion || criterion instanceof UdpPortCriterion) {
1230 // FIXME: QMX switches do not support L4 dst port matching in ACL table.
1231 // Currently L4 dst port matching is only used by DHCP relay feature
1232 // and therefore is safe to be replaced with L4 src port matching.
1233 // We need to revisit this if L4 dst port is used for other purpose in the future.
1234 if (!supportIpv6L4Dst() && isIpv6(fwd.selector())) {
Charles Chand9e47c62017-10-05 15:17:15 -07001235 switch (criterion.type()) {
1236 case UDP_DST:
Charles Chand9e47c62017-10-05 15:17:15 -07001237 case UDP_DST_MASKED:
Charles Chand9e47c62017-10-05 15:17:15 -07001238 case TCP_DST:
Charles Chand9e47c62017-10-05 15:17:15 -07001239 case TCP_DST_MASKED:
Charles Chand9e47c62017-10-05 15:17:15 -07001240 break;
1241 default:
1242 sbuilder.add(criterion);
1243 }
1244 } else {
1245 sbuilder.add(criterion);
1246 }
Saurav Das77b5e902016-01-27 17:01:59 -08001247 } else {
1248 sbuilder.add(criterion);
1249 }
1250 });
Charles Chanab591602019-01-22 17:25:04 -08001251 return sbuilder;
1252 }
Saurav Das77b5e902016-01-27 17:01:59 -08001253
Charles Chanab591602019-01-22 17:25:04 -08001254 /**
1255 * Helper function to create traffic treatment builder for versatile forwarding objectives.
1256 *
1257 * @param fwd original forwarding objective
1258 * @return treatment builder for the flow rule, or null if there is an error.
1259 */
1260 protected TrafficTreatment.Builder versatileTreatmentBuilder(ForwardingObjective fwd) {
Saurav Das822c4e22015-10-23 10:51:11 -07001261 // XXX driver does not currently do type checking as per Tables 65-67 in
1262 // OFDPA 2.0 spec. The only allowed treatment is a punt to the controller.
Saurav Das49cb5a12016-01-16 22:54:07 -08001263 TrafficTreatment.Builder ttBuilder = DefaultTrafficTreatment.builder();
1264 if (fwd.treatment() != null) {
1265 for (Instruction ins : fwd.treatment().allInstructions()) {
1266 if (ins instanceof OutputInstruction) {
1267 OutputInstruction o = (OutputInstruction) ins;
Ray Milkeyfe0e0852018-01-18 11:14:05 -08001268 if (PortNumber.CONTROLLER.equals(o.port())) {
Saurav Das49cb5a12016-01-16 22:54:07 -08001269 ttBuilder.add(o);
1270 } else {
1271 log.warn("Only allowed treatments in versatile forwarding "
1272 + "objectives are punts to the controller");
1273 }
Charles Chanf76de302018-06-15 18:54:18 -07001274 } else if (ins instanceof NoActionInstruction) {
1275 // No action is allowed and nothing needs to be done
Saurav Das49cb5a12016-01-16 22:54:07 -08001276 } else {
1277 log.warn("Cannot process instruction in versatile fwd {}", ins);
1278 }
Saurav Das822c4e22015-10-23 10:51:11 -07001279 }
Charles Chan2df0e8a2017-01-09 11:45:08 -08001280 if (fwd.treatment().clearedDeferred()) {
1281 ttBuilder.wipeDeferred();
1282 }
Saurav Das822c4e22015-10-23 10:51:11 -07001283 }
Saurav Das822c4e22015-10-23 10:51:11 -07001284 if (fwd.nextId() != null) {
Charles Chanab591602019-01-22 17:25:04 -08001285 // Override case
Saurav Das49cb5a12016-01-16 22:54:07 -08001286 NextGroup next = getGroupForNextObjective(fwd.nextId());
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +05301287 if (next == null) {
1288 fail(fwd, ObjectiveError.BADPARAMS);
Charles Chanab591602019-01-22 17:25:04 -08001289 return null;
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +05301290 }
Saurav Das49cb5a12016-01-16 22:54:07 -08001291 List<Deque<GroupKey>> gkeys = appKryo.deserialize(next.data());
1292 // we only need the top level group's key to point the flow to it
1293 Group group = groupService.getGroup(deviceId, gkeys.get(0).peekFirst());
1294 if (group == null) {
1295 log.warn("Group with key:{} for next-id:{} not found in dev:{}",
Charles Chanab591602019-01-22 17:25:04 -08001296 gkeys.get(0).peekFirst(), fwd.nextId(), deviceId);
Saurav Das49cb5a12016-01-16 22:54:07 -08001297 fail(fwd, ObjectiveError.GROUPMISSING);
Charles Chanab591602019-01-22 17:25:04 -08001298 return null;
Saurav Das49cb5a12016-01-16 22:54:07 -08001299 }
1300 ttBuilder.deferred().group(group.id());
Saurav Das822c4e22015-10-23 10:51:11 -07001301 }
Charles Chanab591602019-01-22 17:25:04 -08001302 return ttBuilder;
Saurav Das822c4e22015-10-23 10:51:11 -07001303 }
1304
1305 /**
1306 * In the OF-DPA 2.0 pipeline, specific forwarding refers to the IP table
Saurav Das8a0732e2015-11-20 15:27:53 -08001307 * (unicast or multicast) or the L2 table (mac + vlan) or the MPLS table.
Saurav Das822c4e22015-10-23 10:51:11 -07001308 *
1309 * @param fwd the forwarding objective of type 'specific'
1310 * @return a collection of flow rules. Typically there will be only one
1311 * for this type of forwarding objective. An empty set may be
1312 * returned if there is an issue in processing the objective.
1313 */
Charles Chan50d900c2018-03-02 13:26:22 -08001314 private Collection<FlowRule> processSpecific(ForwardingObjective fwd) {
Saurav Das25190812016-05-27 13:54:07 -07001315 log.debug("Processing specific fwd objective:{} in dev:{} with next:{}",
Saurav Das4ce45962015-11-24 23:21:05 -08001316 fwd.id(), deviceId, fwd.nextId());
1317 boolean isEthTypeObj = isSupportedEthTypeObjective(fwd);
1318 boolean isEthDstObj = isSupportedEthDstObjective(fwd);
1319
1320 if (isEthTypeObj) {
1321 return processEthTypeSpecific(fwd);
1322 } else if (isEthDstObj) {
1323 return processEthDstSpecific(fwd);
1324 } else {
1325 log.warn("processSpecific: Unsupported forwarding objective "
1326 + "criteria fwd:{} in dev:{}", fwd.nextId(), deviceId);
Saurav Das822c4e22015-10-23 10:51:11 -07001327 fail(fwd, ObjectiveError.UNSUPPORTED);
1328 return Collections.emptySet();
1329 }
Saurav Das4ce45962015-11-24 23:21:05 -08001330 }
1331
Saurav Das4ce45962015-11-24 23:21:05 -08001332 /**
1333 * Handles forwarding rules to the IP and MPLS tables.
1334 *
1335 * @param fwd the forwarding objective
1336 * @return A collection of flow rules, or an empty set
1337 */
1338 protected Collection<FlowRule> processEthTypeSpecific(ForwardingObjective fwd) {
Charles Chancad338a2016-09-16 18:03:11 -07001339 return processEthTypeSpecificInternal(fwd, false, ACL_TABLE);
Charles Chanf9e98652016-09-07 16:54:23 -07001340 }
1341
1342 /**
1343 * Internal implementation of processEthTypeSpecific.
1344 * <p>
1345 * Wildcarded IPv4_DST is not supported in OFDPA i12. Therefore, we break
1346 * the rule into 0.0.0.0/1 and 128.0.0.0/1.
1347 * The allowDefaultRoute must be set to false for OFDPA i12.
1348 * </p>
1349 *
1350 * @param fwd the forwarding objective
1351 * @param allowDefaultRoute allow wildcarded IPv4_DST or not
Ray Milkey0bb1e102016-11-10 14:51:27 -08001352 * @param mplsNextTable next MPLS table
Charles Chanf9e98652016-09-07 16:54:23 -07001353 * @return A collection of flow rules, or an empty set
1354 */
1355 protected Collection<FlowRule> processEthTypeSpecificInternal(ForwardingObjective fwd,
Charles Chancad338a2016-09-16 18:03:11 -07001356 boolean allowDefaultRoute,
1357 int mplsNextTable) {
Saurav Das4ce45962015-11-24 23:21:05 -08001358 TrafficSelector selector = fwd.selector();
1359 EthTypeCriterion ethType =
1360 (EthTypeCriterion) selector.getCriterion(Criterion.Type.ETH_TYPE);
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001361 boolean emptyGroup = false;
Charles Chan188ebf52015-12-23 00:15:11 -08001362 int forTableId;
Saurav Das8a0732e2015-11-20 15:27:53 -08001363 TrafficSelector.Builder filteredSelector = DefaultTrafficSelector.builder();
Charles Chan14967c22015-12-07 11:11:50 -08001364 TrafficTreatment.Builder tb = DefaultTrafficTreatment.builder();
Flavio Castroe10fa242016-01-15 12:43:51 -08001365 TrafficSelector.Builder complementarySelector = DefaultTrafficSelector.builder();
Charles Chan14967c22015-12-07 11:11:50 -08001366
Saurav Das8a0732e2015-11-20 15:27:53 -08001367 if (ethType.ethType().toShort() == Ethernet.TYPE_IPV4) {
Pier Luigi3bfe32c2017-01-30 09:47:36 -08001368 if (buildIpv4Selector(filteredSelector, complementarySelector, fwd, allowDefaultRoute) < 0) {
1369 return Collections.emptyList();
1370 }
1371 // We need to set properly the next table
Flavio Castroe10fa242016-01-15 12:43:51 -08001372 IpPrefix ipv4Dst = ((IPCriterion) selector.getCriterion(Criterion.Type.IPV4_DST)).ip();
Charles Chan5b9df8d2016-03-28 22:21:40 -07001373 if (ipv4Dst.isMulticast()) {
Charles Chan5b9df8d2016-03-28 22:21:40 -07001374 forTableId = MULTICAST_ROUTING_TABLE;
Flavio Castroe10fa242016-01-15 12:43:51 -08001375 } else {
Charles Chan5b9df8d2016-03-28 22:21:40 -07001376 forTableId = UNICAST_ROUTING_TABLE;
Flavio Castroe10fa242016-01-15 12:43:51 -08001377 }
Charles Chan50d900c2018-03-02 13:26:22 -08001378
Charles Chan236653d2018-03-05 11:28:23 -08001379 // TODO decrementing IP ttl is done automatically for routing, this
Charles Chan50d900c2018-03-02 13:26:22 -08001380 // action is ignored or rejected in ofdpa as it is not fully implemented
Charles Chan236653d2018-03-05 11:28:23 -08001381 // if (fwd.treatment() != null) {
1382 // for (Instruction instr : fwd.treatment().allInstructions()) {
1383 // if (instr instanceof L3ModificationInstruction &&
1384 // ((L3ModificationInstruction) instr).subtype() == L3SubType.DEC_TTL) {
1385 // tb.deferred().add(instr);
1386 // }
1387 // }
1388 // }
1389
Pier Ventree0ae7a32016-11-23 09:57:42 -08001390 } else if (ethType.ethType().toShort() == Ethernet.TYPE_IPV6) {
1391 if (buildIpv6Selector(filteredSelector, fwd) < 0) {
1392 return Collections.emptyList();
1393 }
Kalhee Kim3d26bf02017-10-04 14:59:11 +00001394 //We need to set the proper next table
1395 IpPrefix ipv6Dst = ((IPCriterion) selector.getCriterion(Criterion.Type.IPV6_DST)).ip();
1396 if (ipv6Dst.isMulticast()) {
1397 forTableId = MULTICAST_ROUTING_TABLE;
1398 } else {
1399 forTableId = UNICAST_ROUTING_TABLE;
1400 }
1401
Charles Chan236653d2018-03-05 11:28:23 -08001402 // TODO decrementing IP ttl is done automatically for routing, this
Charles Chan50d900c2018-03-02 13:26:22 -08001403 // action is ignored or rejected in ofdpa as it is not fully implemented
Charles Chan236653d2018-03-05 11:28:23 -08001404 // if (fwd.treatment() != null) {
1405 // for (Instruction instr : fwd.treatment().allInstructions()) {
1406 // if (instr instanceof L3ModificationInstruction &&
1407 // ((L3ModificationInstruction) instr).subtype() == L3SubType.DEC_TTL) {
1408 // tb.deferred().add(instr);
1409 // }
1410 // }
1411 // }
Saurav Das8a0732e2015-11-20 15:27:53 -08001412 } else {
1413 filteredSelector
1414 .matchEthType(Ethernet.MPLS_UNICAST)
1415 .matchMplsLabel(((MplsCriterion)
1416 selector.getCriterion(Criterion.Type.MPLS_LABEL)).label());
1417 MplsBosCriterion bos = (MplsBosCriterion) selector
Pier Ventre140a8942016-11-02 07:26:38 -07001418 .getCriterion(MPLS_BOS);
Alex Yashchuk4caa8e82017-12-08 17:40:05 +02001419 if (bos != null && requireMplsBosMatch()) {
Saurav Das8a0732e2015-11-20 15:27:53 -08001420 filteredSelector.matchMplsBos(bos.mplsBos());
1421 }
1422 forTableId = MPLS_TABLE_1;
Saurav Das4ce45962015-11-24 23:21:05 -08001423 log.debug("processing MPLS specific forwarding objective {} -> next:{}"
1424 + " in dev {}", fwd.id(), fwd.nextId(), deviceId);
Saurav Das822c4e22015-10-23 10:51:11 -07001425
Charles Chan14967c22015-12-07 11:11:50 -08001426 if (fwd.treatment() != null) {
1427 for (Instruction instr : fwd.treatment().allInstructions()) {
1428 if (instr instanceof L2ModificationInstruction &&
1429 ((L2ModificationInstruction) instr).subtype() == L2SubType.MPLS_POP) {
Pier Luigi3bfe32c2017-01-30 09:47:36 -08001430 // OF-DPA does not pop in MPLS table in some cases. For the L3 VPN, it requires
Saurav Das9c705342017-01-06 11:36:01 -08001431 // setting the MPLS_TYPE so pop can happen down the pipeline
Alex Yashchuk4caa8e82017-12-08 17:40:05 +02001432 if (requireMplsPop()) {
1433 if (mplsNextTable == MPLS_TYPE_TABLE && isNotMplsBos(selector)) {
1434 tb.immediate().popMpls();
1435 }
1436 } else {
1437 // Skip mpls pop action for mpls_unicast label
1438 if (instr instanceof ModMplsHeaderInstruction &&
1439 !((ModMplsHeaderInstruction) instr).ethernetType()
1440 .equals(EtherType.MPLS_UNICAST.ethType())) {
1441 tb.immediate().add(instr);
1442 }
Pier Luigi3bfe32c2017-01-30 09:47:36 -08001443 }
Charles Chan14967c22015-12-07 11:11:50 -08001444 }
Alex Yashchuk4caa8e82017-12-08 17:40:05 +02001445
1446 if (requireMplsTtlModification()) {
1447 if (instr instanceof L3ModificationInstruction &&
1448 ((L3ModificationInstruction) instr).subtype() == L3SubType.DEC_TTL) {
1449 // FIXME Should modify the app to send the correct DEC_MPLS_TTL instruction
1450 tb.immediate().decMplsTtl();
1451 }
1452 if (instr instanceof L3ModificationInstruction &&
1453 ((L3ModificationInstruction) instr).subtype() == L3SubType.TTL_IN) {
1454 tb.immediate().add(instr);
1455 }
Charles Chan14967c22015-12-07 11:11:50 -08001456 }
Saurav Das8a0732e2015-11-20 15:27:53 -08001457 }
1458 }
1459 }
Saurav Das822c4e22015-10-23 10:51:11 -07001460
1461 if (fwd.nextId() != null) {
Saurav Das423fe2b2015-12-04 10:52:59 -08001462 NextGroup next = getGroupForNextObjective(fwd.nextId());
1463 if (next != null) {
1464 List<Deque<GroupKey>> gkeys = appKryo.deserialize(next.data());
1465 // we only need the top level group's key to point the flow to it
1466 Group group = groupService.getGroup(deviceId, gkeys.get(0).peekFirst());
1467 if (group == null) {
1468 log.warn("Group with key:{} for next-id:{} not found in dev:{}",
1469 gkeys.get(0).peekFirst(), fwd.nextId(), deviceId);
1470 fail(fwd, ObjectiveError.GROUPMISSING);
1471 return Collections.emptySet();
1472 }
Saurav Dasa4020382018-02-14 14:14:54 -08001473 if (isNotMplsBos(selector) && group.type().equals(SELECT)) {
1474 log.warn("SR CONTINUE case cannot be handled as MPLS ECMP "
1475 + "is not implemented in OF-DPA yet. Aborting flow {} -> next:{} "
1476 + "in this device {}", fwd.id(), fwd.nextId(), deviceId);
1477 fail(fwd, ObjectiveError.FLOWINSTALLATIONFAILED);
1478 return Collections.emptySet();
1479 }
Saurav Das423fe2b2015-12-04 10:52:59 -08001480 tb.deferred().group(group.id());
Saurav Dasc568c342018-01-25 09:49:01 -08001481 // retrying flows may be necessary due to bug CORD-554
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001482 if (gkeys.size() == 1 && gkeys.get(0).size() == 1) {
Saurav Dasc568c342018-01-25 09:49:01 -08001483 if (shouldRetry()) {
1484 log.warn("Found empty group 0x{} in dev:{} .. will retry fwd:{}",
1485 Integer.toHexString(group.id().id()), deviceId,
1486 fwd.id());
1487 emptyGroup = true;
1488 }
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001489 }
Saurav Das25190812016-05-27 13:54:07 -07001490 } else {
1491 log.warn("Cannot find group for nextId:{} in dev:{}. Aborting fwd:{}",
1492 fwd.nextId(), deviceId, fwd.id());
1493 fail(fwd, ObjectiveError.FLOWINSTALLATIONFAILED);
1494 return Collections.emptySet();
Saurav Das822c4e22015-10-23 10:51:11 -07001495 }
Saurav Das822c4e22015-10-23 10:51:11 -07001496 }
Charles Chancad338a2016-09-16 18:03:11 -07001497
1498 if (forTableId == MPLS_TABLE_1) {
Pier Ventre140a8942016-11-02 07:26:38 -07001499 if (mplsNextTable == MPLS_L3_TYPE_TABLE) {
Charles Chancad338a2016-09-16 18:03:11 -07001500 Ofdpa3SetMplsType setMplsType = new Ofdpa3SetMplsType(Ofdpa3MplsType.L3_PHP);
Saurav Das9c705342017-01-06 11:36:01 -08001501 // set mpls type as apply_action
1502 tb.immediate().extension(setMplsType, deviceId);
Charles Chancad338a2016-09-16 18:03:11 -07001503 }
1504 tb.transition(mplsNextTable);
1505 } else {
1506 tb.transition(ACL_TABLE);
1507 }
1508
Andrea Campanellad980c6d2018-04-30 11:48:55 +02001509 if (fwd.treatment() != null && fwd.treatment().clearedDeferred()) {
1510 if (supportsUnicastBlackHole()) {
1511 tb.wipeDeferred();
1512 } else {
1513 log.warn("Clear Deferred is not supported Unicast Routing Table on device {}", deviceId);
1514 return Collections.emptySet();
1515 }
1516 }
1517
Saurav Das822c4e22015-10-23 10:51:11 -07001518 FlowRule.Builder ruleBuilder = DefaultFlowRule.builder()
1519 .fromApp(fwd.appId())
1520 .withPriority(fwd.priority())
1521 .forDevice(deviceId)
Saurav Das8a0732e2015-11-20 15:27:53 -08001522 .withSelector(filteredSelector.build())
1523 .withTreatment(tb.build())
1524 .forTable(forTableId);
Saurav Das822c4e22015-10-23 10:51:11 -07001525
1526 if (fwd.permanent()) {
1527 ruleBuilder.makePermanent();
1528 } else {
1529 ruleBuilder.makeTemporary(fwd.timeout());
1530 }
Flavio Castroe10fa242016-01-15 12:43:51 -08001531 Collection<FlowRule> flowRuleCollection = new ArrayList<>();
1532 flowRuleCollection.add(ruleBuilder.build());
Pier Luigi3bfe32c2017-01-30 09:47:36 -08001533 if (!allowDefaultRoute) {
Pier Ventree0ae7a32016-11-23 09:57:42 -08001534 flowRuleCollection.add(
1535 defaultRoute(fwd, complementarySelector, forTableId, tb)
1536 );
Flavio Castroe10fa242016-01-15 12:43:51 -08001537 log.debug("Default rule 0.0.0.0/0 is being installed two rules");
1538 }
Saurav Dasc568c342018-01-25 09:49:01 -08001539
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001540 if (emptyGroup) {
pier8b3aef42019-03-11 15:14:02 -07001541 retryExecutorService.schedule(new RetryFlows(fwd, flowRuleCollection),
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001542 RETRY_MS, TimeUnit.MILLISECONDS);
1543 }
Flavio Castroe10fa242016-01-15 12:43:51 -08001544 return flowRuleCollection;
Saurav Das822c4e22015-10-23 10:51:11 -07001545 }
1546
Charles Chan50d900c2018-03-02 13:26:22 -08001547 private int buildIpv4Selector(TrafficSelector.Builder builderToUpdate,
Pier Luigi3bfe32c2017-01-30 09:47:36 -08001548 TrafficSelector.Builder extBuilder,
1549 ForwardingObjective fwd,
1550 boolean allowDefaultRoute) {
1551 TrafficSelector selector = fwd.selector();
1552
1553 IpPrefix ipv4Dst = ((IPCriterion) selector.getCriterion(Criterion.Type.IPV4_DST)).ip();
1554 if (ipv4Dst.isMulticast()) {
1555 if (ipv4Dst.prefixLength() != 32) {
1556 log.warn("Multicast specific forwarding objective can only be /32");
1557 fail(fwd, ObjectiveError.BADPARAMS);
1558 return -1;
1559 }
1560 VlanId assignedVlan = readVlanFromSelector(fwd.meta());
1561 if (assignedVlan == null) {
1562 log.warn("VLAN ID required by multicast specific fwd obj is missing. Abort.");
1563 fail(fwd, ObjectiveError.BADPARAMS);
1564 return -1;
1565 }
Charles Chand1172632017-03-15 17:33:09 -07001566 if (requireVlanExtensions()) {
1567 OfdpaMatchVlanVid ofdpaMatchVlanVid = new OfdpaMatchVlanVid(assignedVlan);
1568 builderToUpdate.extension(ofdpaMatchVlanVid, deviceId);
1569 } else {
1570 builderToUpdate.matchVlanId(assignedVlan);
1571 }
Pier Luigi3bfe32c2017-01-30 09:47:36 -08001572 builderToUpdate.matchEthType(Ethernet.TYPE_IPV4).matchIPDst(ipv4Dst);
1573 log.debug("processing IPv4 multicast specific forwarding objective {} -> next:{}"
1574 + " in dev:{}", fwd.id(), fwd.nextId(), deviceId);
1575 } else {
1576 if (ipv4Dst.prefixLength() == 0) {
1577 if (allowDefaultRoute) {
1578 // The entire IPV4_DST field is wildcarded intentionally
1579 builderToUpdate.matchEthType(Ethernet.TYPE_IPV4);
1580 } else {
1581 // NOTE: The switch does not support matching 0.0.0.0/0
1582 // Split it into 0.0.0.0/1 and 128.0.0.0/1
1583 builderToUpdate.matchEthType(Ethernet.TYPE_IPV4)
1584 .matchIPDst(IpPrefix.valueOf("0.0.0.0/1"));
1585 extBuilder.matchEthType(Ethernet.TYPE_IPV4)
1586 .matchIPDst(IpPrefix.valueOf("128.0.0.0/1"));
1587 }
1588 } else {
1589 builderToUpdate.matchEthType(Ethernet.TYPE_IPV4).matchIPDst(ipv4Dst);
1590 }
1591 log.debug("processing IPv4 unicast specific forwarding objective {} -> next:{}"
1592 + " in dev:{}", fwd.id(), fwd.nextId(), deviceId);
1593 }
1594 return 0;
1595 }
1596
1597 /**
1598 * Helper method to build Ipv6 selector using the selector provided by
1599 * a forwarding objective.
1600 *
1601 * @param builderToUpdate the builder to update
1602 * @param fwd the selector to read
1603 * @return 0 if the update ends correctly. -1 if the matches
1604 * are not yet supported
1605 */
Charles Chan50d900c2018-03-02 13:26:22 -08001606 int buildIpv6Selector(TrafficSelector.Builder builderToUpdate,
Pier Luigi3bfe32c2017-01-30 09:47:36 -08001607 ForwardingObjective fwd) {
1608
1609 TrafficSelector selector = fwd.selector();
1610
1611 IpPrefix ipv6Dst = ((IPCriterion) selector.getCriterion(Criterion.Type.IPV6_DST)).ip();
1612 if (ipv6Dst.isMulticast()) {
Julia Ferguson65428c32017-08-10 18:15:24 +00001613 if (ipv6Dst.prefixLength() != IpAddress.INET6_BIT_LENGTH) {
1614 log.warn("Multicast specific forwarding objective can only be /128");
1615 fail(fwd, ObjectiveError.BADPARAMS);
1616 return -1;
1617 }
1618 VlanId assignedVlan = readVlanFromSelector(fwd.meta());
1619 if (assignedVlan == null) {
1620 log.warn("VLAN ID required by multicast specific fwd obj is missing. Abort.");
1621 fail(fwd, ObjectiveError.BADPARAMS);
1622 return -1;
1623 }
1624 if (requireVlanExtensions()) {
1625 OfdpaMatchVlanVid ofdpaMatchVlanVid = new OfdpaMatchVlanVid(assignedVlan);
1626 builderToUpdate.extension(ofdpaMatchVlanVid, deviceId);
1627 } else {
1628 builderToUpdate.matchVlanId(assignedVlan);
1629 }
1630 builderToUpdate.matchEthType(Ethernet.TYPE_IPV6).matchIPv6Dst(ipv6Dst);
1631 log.debug("processing IPv6 multicast specific forwarding objective {} -> next:{}"
1632 + " in dev:{}", fwd.id(), fwd.nextId(), deviceId);
1633 } else {
1634 if (ipv6Dst.prefixLength() != 0) {
1635 builderToUpdate.matchIPv6Dst(ipv6Dst);
1636 }
Pier Luigi3bfe32c2017-01-30 09:47:36 -08001637 builderToUpdate.matchEthType(Ethernet.TYPE_IPV6);
1638 log.debug("processing IPv6 unicast specific forwarding objective {} -> next:{}"
1639 + " in dev:{}", fwd.id(), fwd.nextId(), deviceId);
Julia Ferguson65428c32017-08-10 18:15:24 +00001640 }
Pier Luigi3bfe32c2017-01-30 09:47:36 -08001641 return 0;
1642 }
1643
Charles Chan50d900c2018-03-02 13:26:22 -08001644 FlowRule defaultRoute(ForwardingObjective fwd,
Pier Ventree0ae7a32016-11-23 09:57:42 -08001645 TrafficSelector.Builder complementarySelector,
1646 int forTableId,
1647 TrafficTreatment.Builder tb) {
1648 FlowRule.Builder rule = DefaultFlowRule.builder()
1649 .fromApp(fwd.appId())
1650 .withPriority(fwd.priority())
1651 .forDevice(deviceId)
1652 .withSelector(complementarySelector.build())
1653 .withTreatment(tb.build())
1654 .forTable(forTableId);
1655 if (fwd.permanent()) {
1656 rule.makePermanent();
1657 } else {
1658 rule.makeTemporary(fwd.timeout());
1659 }
1660 return rule.build();
1661 }
1662
Saurav Das4ce45962015-11-24 23:21:05 -08001663 /**
1664 * Handles forwarding rules to the L2 bridging table. Flow actions are not
1665 * allowed in the bridging table - instead we use L2 Interface group or
1666 * L2 flood group
1667 *
1668 * @param fwd the forwarding objective
1669 * @return A collection of flow rules, or an empty set
1670 */
1671 protected Collection<FlowRule> processEthDstSpecific(ForwardingObjective fwd) {
1672 List<FlowRule> rules = new ArrayList<>();
1673
1674 // Build filtered selector
1675 TrafficSelector selector = fwd.selector();
1676 EthCriterion ethCriterion = (EthCriterion) selector
1677 .getCriterion(Criterion.Type.ETH_DST);
1678 VlanIdCriterion vlanIdCriterion = (VlanIdCriterion) selector
1679 .getCriterion(Criterion.Type.VLAN_VID);
1680
1681 if (vlanIdCriterion == null) {
1682 log.warn("Forwarding objective for bridging requires vlan. Not "
1683 + "installing fwd:{} in dev:{}", fwd.id(), deviceId);
1684 fail(fwd, ObjectiveError.BADPARAMS);
1685 return Collections.emptySet();
1686 }
1687
1688 TrafficSelector.Builder filteredSelectorBuilder =
1689 DefaultTrafficSelector.builder();
Yi Tseng3a77b4f2017-02-06 15:02:17 -08001690
1691 if (!ethCriterion.mac().equals(NONE) &&
1692 !ethCriterion.mac().equals(BROADCAST)) {
Saurav Das4ce45962015-11-24 23:21:05 -08001693 filteredSelectorBuilder.matchEthDst(ethCriterion.mac());
1694 log.debug("processing L2 forwarding objective:{} -> next:{} in dev:{}",
1695 fwd.id(), fwd.nextId(), deviceId);
1696 } else {
Yi Tseng3a77b4f2017-02-06 15:02:17 -08001697 // Use wildcard DST_MAC if the MacAddress is None or Broadcast
Saurav Das4ce45962015-11-24 23:21:05 -08001698 log.debug("processing L2 Broadcast forwarding objective:{} -> next:{} "
1699 + "in dev:{} for vlan:{}",
1700 fwd.id(), fwd.nextId(), deviceId, vlanIdCriterion.vlanId());
1701 }
Charles Chand1172632017-03-15 17:33:09 -07001702 if (requireVlanExtensions()) {
1703 OfdpaMatchVlanVid ofdpaMatchVlanVid = new OfdpaMatchVlanVid(vlanIdCriterion.vlanId());
1704 filteredSelectorBuilder.extension(ofdpaMatchVlanVid, deviceId);
1705 } else {
1706 filteredSelectorBuilder.matchVlanId(vlanIdCriterion.vlanId());
1707 }
Saurav Das4ce45962015-11-24 23:21:05 -08001708 TrafficSelector filteredSelector = filteredSelectorBuilder.build();
1709
1710 if (fwd.treatment() != null) {
1711 log.warn("Ignoring traffic treatment in fwd rule {} meant for L2 table"
1712 + "for dev:{}. Expecting only nextId", fwd.id(), deviceId);
1713 }
1714
1715 TrafficTreatment.Builder treatmentBuilder = DefaultTrafficTreatment.builder();
1716 if (fwd.nextId() != null) {
Saurav Das423fe2b2015-12-04 10:52:59 -08001717 NextGroup next = getGroupForNextObjective(fwd.nextId());
Saurav Das4ce45962015-11-24 23:21:05 -08001718 if (next != null) {
1719 List<Deque<GroupKey>> gkeys = appKryo.deserialize(next.data());
1720 // we only need the top level group's key to point the flow to it
1721 Group group = groupService.getGroup(deviceId, gkeys.get(0).peekFirst());
1722 if (group != null) {
1723 treatmentBuilder.deferred().group(group.id());
1724 } else {
1725 log.warn("Group with key:{} for next-id:{} not found in dev:{}",
1726 gkeys.get(0).peekFirst(), fwd.nextId(), deviceId);
1727 fail(fwd, ObjectiveError.GROUPMISSING);
1728 return Collections.emptySet();
1729 }
1730 }
1731 }
1732 treatmentBuilder.immediate().transition(ACL_TABLE);
1733 TrafficTreatment filteredTreatment = treatmentBuilder.build();
1734
1735 // Build bridging table entries
1736 FlowRule.Builder flowRuleBuilder = DefaultFlowRule.builder();
1737 flowRuleBuilder.fromApp(fwd.appId())
1738 .withPriority(fwd.priority())
1739 .forDevice(deviceId)
1740 .withSelector(filteredSelector)
1741 .withTreatment(filteredTreatment)
1742 .forTable(BRIDGING_TABLE);
1743 if (fwd.permanent()) {
1744 flowRuleBuilder.makePermanent();
1745 } else {
1746 flowRuleBuilder.makeTemporary(fwd.timeout());
1747 }
1748 rules.add(flowRuleBuilder.build());
1749 return rules;
1750 }
1751
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001752 //////////////////////////////////////
1753 // Helper Methods and Classes
1754 //////////////////////////////////////
1755
1756 private boolean isSupportedEthTypeObjective(ForwardingObjective fwd) {
1757 TrafficSelector selector = fwd.selector();
1758 EthTypeCriterion ethType = (EthTypeCriterion) selector
1759 .getCriterion(Criterion.Type.ETH_TYPE);
1760 return !((ethType == null) ||
1761 ((ethType.ethType().toShort() != Ethernet.TYPE_IPV4) &&
Pier Ventree0ae7a32016-11-23 09:57:42 -08001762 (ethType.ethType().toShort() != Ethernet.MPLS_UNICAST)) &&
1763 (ethType.ethType().toShort() != Ethernet.TYPE_IPV6));
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001764 }
1765
1766 private boolean isSupportedEthDstObjective(ForwardingObjective fwd) {
1767 TrafficSelector selector = fwd.selector();
1768 EthCriterion ethDst = (EthCriterion) selector
1769 .getCriterion(Criterion.Type.ETH_DST);
1770 VlanIdCriterion vlanId = (VlanIdCriterion) selector
1771 .getCriterion(Criterion.Type.VLAN_VID);
1772 return !(ethDst == null && vlanId == null);
1773 }
1774
Charles Chan50d900c2018-03-02 13:26:22 -08001775 NextGroup getGroupForNextObjective(Integer nextId) {
Saurav Das423fe2b2015-12-04 10:52:59 -08001776 NextGroup next = flowObjectiveStore.getNextGroup(nextId);
1777 if (next != null) {
1778 List<Deque<GroupKey>> gkeys = appKryo.deserialize(next.data());
1779 if (gkeys != null && !gkeys.isEmpty()) {
1780 return next;
1781 } else {
1782 log.warn("Empty next group found in FlowObjective store for "
1783 + "next-id:{} in dev:{}", nextId, deviceId);
1784 }
1785 } else {
1786 log.warn("next-id {} not found in Flow objective store for dev:{}",
1787 nextId, deviceId);
1788 }
1789 return null;
1790 }
1791
Saurav Das24431192016-03-07 19:13:00 -08001792 @Override
1793 public List<String> getNextMappings(NextGroup nextGroup) {
1794 List<String> mappings = new ArrayList<>();
1795 List<Deque<GroupKey>> gkeys = appKryo.deserialize(nextGroup.data());
1796 for (Deque<GroupKey> gkd : gkeys) {
1797 Group lastGroup = null;
Yuta HIGUCHI2dce08a2017-04-20 21:57:48 -07001798 StringBuilder gchain = new StringBuilder();
Saurav Das24431192016-03-07 19:13:00 -08001799 for (GroupKey gk : gkd) {
1800 Group g = groupService.getGroup(deviceId, gk);
Saurav Das8be4e3a2016-03-11 17:19:07 -08001801 if (g == null) {
Saurav Das25190812016-05-27 13:54:07 -07001802 gchain.append(" NoGrp").append(" -->");
Saurav Das8be4e3a2016-03-11 17:19:07 -08001803 continue;
1804 }
1805 gchain.append(" 0x").append(Integer.toHexString(g.id().id()))
1806 .append(" -->");
Saurav Das24431192016-03-07 19:13:00 -08001807 lastGroup = g;
1808 }
1809 // add port information for last group in group-chain
Yuta HIGUCHI2dce08a2017-04-20 21:57:48 -07001810 List<Instruction> lastGroupIns = new ArrayList<>();
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001811 if (lastGroup != null && !lastGroup.buckets().buckets().isEmpty()) {
Saurav Das25190812016-05-27 13:54:07 -07001812 lastGroupIns = lastGroup.buckets().buckets().get(0)
1813 .treatment().allInstructions();
1814 }
1815 for (Instruction i: lastGroupIns) {
Saurav Das24431192016-03-07 19:13:00 -08001816 if (i instanceof OutputInstruction) {
Saurav Das8be4e3a2016-03-11 17:19:07 -08001817 gchain.append(" port:").append(((OutputInstruction) i).port());
Saurav Das24431192016-03-07 19:13:00 -08001818 }
1819 }
Saurav Das8be4e3a2016-03-11 17:19:07 -08001820 mappings.add(gchain.toString());
Saurav Das24431192016-03-07 19:13:00 -08001821 }
1822 return mappings;
1823 }
Charles Chan5b9df8d2016-03-28 22:21:40 -07001824
Saurav Dasa4020382018-02-14 14:14:54 -08001825 /**
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001826 * Utility class that retries sending flows a fixed number of times, even if
1827 * some of the attempts are successful. Used only for forwarding objectives.
1828 */
Alex Yashchuk4caa8e82017-12-08 17:40:05 +02001829 public final class RetryFlows implements Runnable {
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001830 int attempts = MAX_RETRY_ATTEMPTS;
1831 private Collection<FlowRule> retryFlows;
1832 private ForwardingObjective fwd;
1833
Charles Chan50d900c2018-03-02 13:26:22 -08001834 RetryFlows(ForwardingObjective fwd, Collection<FlowRule> retryFlows) {
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001835 this.fwd = fwd;
1836 this.retryFlows = retryFlows;
1837 }
1838
1839 @Override
1840 public void run() {
1841 log.info("RETRY FLOWS ATTEMPT# {} for fwd:{} rules:{}",
1842 MAX_RETRY_ATTEMPTS - attempts, fwd.id(), retryFlows.size());
pier8b3aef42019-03-11 15:14:02 -07001843 sendForwards(Collections.singletonList(Pair.of(fwd, retryFlows)));
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001844 if (--attempts > 0) {
pier8b3aef42019-03-11 15:14:02 -07001845 retryExecutorService.schedule(this, RETRY_MS, TimeUnit.MILLISECONDS);
1846 }
1847 }
1848 }
1849
1850 // Flow rules accumulator for reducing the number of transactions required to the devices.
1851 private final class ForwardingObjectiveAccumulator
1852 extends AbstractAccumulator<Pair<ForwardingObjective, Collection<FlowRule>>> {
1853
1854 ForwardingObjectiveAccumulator(int maxFwd, int maxBatchMS, int maxIdleMS) {
1855 super(TIMER, maxFwd, maxBatchMS, maxIdleMS);
1856 }
1857
1858 @Override
1859 public void processItems(List<Pair<ForwardingObjective, Collection<FlowRule>>> pairs) {
1860 // Triggers creation of a batch using the list of flowrules generated from fwdobjs.
1861 accumulatorExecutorService.execute(new FlowRulesBuilderTask(pairs));
1862 }
1863 }
1864
1865 // Task for building batch of flow rules in a separate thread.
1866 private final class FlowRulesBuilderTask implements Runnable {
1867 private final List<Pair<ForwardingObjective, Collection<FlowRule>>> pairs;
1868
1869 FlowRulesBuilderTask(List<Pair<ForwardingObjective, Collection<FlowRule>>> pairs) {
1870 this.pairs = pairs;
1871 }
1872
1873 @Override
1874 public void run() {
1875 try {
1876 sendForwards(pairs);
1877 } catch (Exception e) {
1878 log.warn("Unable to send forwards", e);
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001879 }
1880 }
1881 }
1882
Saurav Das822c4e22015-10-23 10:51:11 -07001883}