blob: 94c833192a553e7fa6f19ad9a2ed389beb50cdcb [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;
Yi Tseng47f82dc2017-03-05 22:48:39 -080019import com.google.common.collect.Sets;
Saurav Das822c4e22015-10-23 10:51:11 -070020import org.onlab.osgi.ServiceDirectory;
Saurav Das822c4e22015-10-23 10:51:11 -070021import org.onlab.packet.Ethernet;
Alex Yashchuka3679532017-12-08 17:40:05 +020022import org.onlab.packet.EthType.EtherType;
Julia Ferguson65428c32017-08-10 18:15:24 +000023import org.onlab.packet.IpAddress;
Charles Chan14967c22015-12-07 11:11:50 -080024import org.onlab.packet.IpPrefix;
Charles Chand57552d2018-03-02 15:41:41 -080025import org.onlab.packet.MacAddress;
Saurav Das822c4e22015-10-23 10:51:11 -070026import org.onlab.packet.VlanId;
27import org.onlab.util.KryoNamespace;
28import org.onosproject.core.ApplicationId;
29import org.onosproject.core.CoreService;
Charles Chancad338a2016-09-16 18:03:11 -070030import org.onosproject.driver.extensions.Ofdpa3MplsType;
31import org.onosproject.driver.extensions.Ofdpa3SetMplsType;
Charles Chan14967c22015-12-07 11:11:50 -080032import org.onosproject.driver.extensions.OfdpaMatchVlanVid;
33import org.onosproject.driver.extensions.OfdpaSetVlanVid;
Saurav Das822c4e22015-10-23 10:51:11 -070034import org.onosproject.net.DeviceId;
35import org.onosproject.net.Port;
36import org.onosproject.net.PortNumber;
37import org.onosproject.net.behaviour.NextGroup;
38import org.onosproject.net.behaviour.Pipeliner;
39import org.onosproject.net.behaviour.PipelinerContext;
40import org.onosproject.net.device.DeviceService;
41import org.onosproject.net.driver.AbstractHandlerBehaviour;
42import org.onosproject.net.flow.DefaultFlowRule;
43import org.onosproject.net.flow.DefaultTrafficSelector;
44import org.onosproject.net.flow.DefaultTrafficTreatment;
45import org.onosproject.net.flow.FlowRule;
46import org.onosproject.net.flow.FlowRuleOperations;
47import org.onosproject.net.flow.FlowRuleOperationsContext;
48import org.onosproject.net.flow.FlowRuleService;
49import org.onosproject.net.flow.TrafficSelector;
50import org.onosproject.net.flow.TrafficTreatment;
51import org.onosproject.net.flow.criteria.Criteria;
52import org.onosproject.net.flow.criteria.Criterion;
53import org.onosproject.net.flow.criteria.EthCriterion;
54import org.onosproject.net.flow.criteria.EthTypeCriterion;
55import org.onosproject.net.flow.criteria.IPCriterion;
Pier Ventree0ae7a32016-11-23 09:57:42 -080056import org.onosproject.net.flow.criteria.Icmpv6CodeCriterion;
57import org.onosproject.net.flow.criteria.Icmpv6TypeCriterion;
Saurav Das8a0732e2015-11-20 15:27:53 -080058import org.onosproject.net.flow.criteria.MplsBosCriterion;
59import org.onosproject.net.flow.criteria.MplsCriterion;
Saurav Das822c4e22015-10-23 10:51:11 -070060import org.onosproject.net.flow.criteria.PortCriterion;
Charles Chand9e47c62017-10-05 15:17:15 -070061import org.onosproject.net.flow.criteria.TcpPortCriterion;
62import org.onosproject.net.flow.criteria.UdpPortCriterion;
Saurav Das822c4e22015-10-23 10:51:11 -070063import org.onosproject.net.flow.criteria.VlanIdCriterion;
64import org.onosproject.net.flow.instructions.Instruction;
65import org.onosproject.net.flow.instructions.Instructions.OutputInstruction;
66import org.onosproject.net.flow.instructions.L2ModificationInstruction;
Saurav Das8a0732e2015-11-20 15:27:53 -080067import org.onosproject.net.flow.instructions.L2ModificationInstruction.L2SubType;
Saurav Das822c4e22015-10-23 10:51:11 -070068import org.onosproject.net.flow.instructions.L2ModificationInstruction.ModVlanIdInstruction;
Charles Chand57552d2018-03-02 15:41:41 -080069import org.onosproject.net.flow.instructions.L2ModificationInstruction.ModEtherInstruction;
Alex Yashchuka3679532017-12-08 17:40:05 +020070import org.onosproject.net.flow.instructions.L2ModificationInstruction.ModMplsHeaderInstruction;
Charles Chan14967c22015-12-07 11:11:50 -080071import org.onosproject.net.flow.instructions.L3ModificationInstruction;
72import org.onosproject.net.flow.instructions.L3ModificationInstruction.L3SubType;
Saurav Das822c4e22015-10-23 10:51:11 -070073import org.onosproject.net.flowobjective.FilteringObjective;
74import org.onosproject.net.flowobjective.FlowObjectiveStore;
75import org.onosproject.net.flowobjective.ForwardingObjective;
76import org.onosproject.net.flowobjective.NextObjective;
77import org.onosproject.net.flowobjective.Objective;
78import org.onosproject.net.flowobjective.ObjectiveError;
Saurav Das822c4e22015-10-23 10:51:11 -070079import org.onosproject.net.group.DefaultGroupKey;
80import org.onosproject.net.group.Group;
Saurav Das822c4e22015-10-23 10:51:11 -070081import org.onosproject.net.group.GroupKey;
Saurav Das822c4e22015-10-23 10:51:11 -070082import org.onosproject.net.group.GroupService;
Saurav Das822c4e22015-10-23 10:51:11 -070083import org.onosproject.store.serializers.KryoNamespaces;
84import org.slf4j.Logger;
85
Pier Ventree0ae7a32016-11-23 09:57:42 -080086import java.util.ArrayDeque;
87import java.util.ArrayList;
88import java.util.Collection;
89import java.util.Collections;
90import java.util.Deque;
91import java.util.List;
92import java.util.Objects;
Pier Ventree0ae7a32016-11-23 09:57:42 -080093import java.util.concurrent.ScheduledExecutorService;
94import java.util.concurrent.TimeUnit;
95
96import static java.util.concurrent.Executors.newScheduledThreadPool;
Yi Tseng3a77b4f2017-02-06 15:02:17 -080097import static org.onlab.packet.MacAddress.BROADCAST;
Charles Chanb4879a52017-10-20 19:09:16 -070098import static org.onlab.packet.MacAddress.IPV4_MULTICAST;
99import static org.onlab.packet.MacAddress.IPV6_MULTICAST;
Yi Tseng3a77b4f2017-02-06 15:02:17 -0800100import static org.onlab.packet.MacAddress.NONE;
Pier Ventree0ae7a32016-11-23 09:57:42 -0800101import static org.onlab.util.Tools.groupedThreads;
Yi Tsengef19de12017-04-24 11:33:05 -0700102import static org.onosproject.driver.pipeline.ofdpa.OfdpaGroupHandlerUtility.*;
Charles Chand9e47c62017-10-05 15:17:15 -0700103import static org.onosproject.net.flow.criteria.Criterion.Type.ETH_TYPE;
Pier Luigi5af88fe2018-02-01 10:23:12 +0100104import static org.onosproject.net.group.GroupDescription.Type.SELECT;
Pier Ventree0ae7a32016-11-23 09:57:42 -0800105import static org.slf4j.LoggerFactory.getLogger;
Pier Ventre140a8942016-11-02 07:26:38 -0700106import static org.onosproject.net.flow.criteria.Criterion.Type.MPLS_BOS;
Pier Ventree0ae7a32016-11-23 09:57:42 -0800107
Saurav Das822c4e22015-10-23 10:51:11 -0700108/**
109 * Driver for Broadcom's OF-DPA v2.0 TTP.
Saurav Das822c4e22015-10-23 10:51:11 -0700110 */
Charles Chan361154b2016-03-24 10:23:39 -0700111public class Ofdpa2Pipeline extends AbstractHandlerBehaviour implements Pipeliner {
Saurav Das822c4e22015-10-23 10:51:11 -0700112 protected static final int PORT_TABLE = 0;
113 protected static final int VLAN_TABLE = 10;
Pier Ventre42287df2016-11-09 14:17:26 -0800114 protected static final int VLAN_1_TABLE = 11;
115 protected static final int MPLS_L2_PORT_FLOW_TABLE = 13;
116 protected static final int MPLS_L2_PORT_PCP_TRUST_FLOW_TABLE = 16;
Saurav Das822c4e22015-10-23 10:51:11 -0700117 protected static final int TMAC_TABLE = 20;
118 protected static final int UNICAST_ROUTING_TABLE = 30;
119 protected static final int MULTICAST_ROUTING_TABLE = 40;
120 protected static final int MPLS_TABLE_0 = 23;
121 protected static final int MPLS_TABLE_1 = 24;
Pier Ventre140a8942016-11-02 07:26:38 -0700122 protected static final int MPLS_L3_TYPE_TABLE = 27;
123 protected static final int MPLS_TYPE_TABLE = 29;
Saurav Das822c4e22015-10-23 10:51:11 -0700124 protected static final int BRIDGING_TABLE = 50;
125 protected static final int ACL_TABLE = 60;
126 protected static final int MAC_LEARNING_TABLE = 254;
127 protected static final long OFPP_MAX = 0xffffff00L;
128
Saurav Das52025962016-01-28 22:30:01 -0800129 protected static final int HIGHEST_PRIORITY = 0xffff;
Saurav Das2857f382015-11-03 14:39:27 -0800130 protected static final int DEFAULT_PRIORITY = 0x8000;
Saurav Das822c4e22015-10-23 10:51:11 -0700131 protected static final int LOWEST_PRIORITY = 0x0;
132
Pier Ventre42287df2016-11-09 14:17:26 -0800133 protected static final int MPLS_L2_PORT_PRIORITY = 2;
Pier Ventre42287df2016-11-09 14:17:26 -0800134 protected static final int MPLS_TUNNEL_ID_BASE = 0x10000;
135 protected static final int MPLS_TUNNEL_ID_MAX = 0x1FFFF;
136
Pier Ventre70d53ba2016-11-17 22:26:29 -0800137 protected static final int MPLS_UNI_PORT_MAX = 0x0000FFFF;
Pier Ventre70d53ba2016-11-17 22:26:29 -0800138 protected static final int MPLS_NNI_PORT_BASE = 0x00020000;
139 protected static final int MPLS_NNI_PORT_MAX = 0x0002FFFF;
140
Saurav Das822c4e22015-10-23 10:51:11 -0700141 private final Logger log = getLogger(getClass());
Charles Chan425854b2016-04-11 15:32:12 -0700142 protected ServiceDirectory serviceDirectory;
Saurav Das822c4e22015-10-23 10:51:11 -0700143 protected FlowRuleService flowRuleService;
Charles Chan425854b2016-04-11 15:32:12 -0700144 protected CoreService coreService;
Saurav Das8a0732e2015-11-20 15:27:53 -0800145 protected GroupService groupService;
146 protected FlowObjectiveStore flowObjectiveStore;
Saurav Das822c4e22015-10-23 10:51:11 -0700147 protected DeviceId deviceId;
148 protected ApplicationId driverId;
Saurav Das822c4e22015-10-23 10:51:11 -0700149 protected DeviceService deviceService;
Charles Chan188ebf52015-12-23 00:15:11 -0800150 protected static KryoNamespace appKryo = new KryoNamespace.Builder()
Yi Tsengef19de12017-04-24 11:33:05 -0700151 .register(KryoNamespaces.API)
152 .register(GroupKey.class)
153 .register(DefaultGroupKey.class)
154 .register(OfdpaNextGroup.class)
155 .register(ArrayDeque.class)
156 .build("Ofdpa2Pipeline");
Saurav Das822c4e22015-10-23 10:51:11 -0700157
Charles Chan425854b2016-04-11 15:32:12 -0700158 protected Ofdpa2GroupHandler groupHandler;
Saurav Das822c4e22015-10-23 10:51:11 -0700159
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700160 // flows installations to be retried
Charles Chan93090352018-03-02 13:26:22 -0800161 private ScheduledExecutorService executorService
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700162 = newScheduledThreadPool(5, groupedThreads("OfdpaPipeliner", "retry-%d", log));
Charles Chan93090352018-03-02 13:26:22 -0800163 private static final int MAX_RETRY_ATTEMPTS = 10;
164 private static final int RETRY_MS = 1000;
Saurav Das4f980082015-11-05 13:39:15 -0800165
Saurav Das822c4e22015-10-23 10:51:11 -0700166 @Override
167 public void init(DeviceId deviceId, PipelinerContext context) {
Saurav Das822c4e22015-10-23 10:51:11 -0700168 this.deviceId = deviceId;
169
Charles Chan425854b2016-04-11 15:32:12 -0700170 serviceDirectory = context.directory();
Saurav Das822c4e22015-10-23 10:51:11 -0700171 coreService = serviceDirectory.get(CoreService.class);
172 flowRuleService = serviceDirectory.get(FlowRuleService.class);
173 groupService = serviceDirectory.get(GroupService.class);
174 flowObjectiveStore = context.store();
Saurav Das822c4e22015-10-23 10:51:11 -0700175 deviceService = serviceDirectory.get(DeviceService.class);
Saurav Das822c4e22015-10-23 10:51:11 -0700176
Charles Chan40132b32017-01-22 00:19:37 -0800177 initDriverId();
178 initGroupHander(context);
Saurav Das822c4e22015-10-23 10:51:11 -0700179
Saurav Das822c4e22015-10-23 10:51:11 -0700180 initializePipeline();
Saurav Das822c4e22015-10-23 10:51:11 -0700181 }
182
Charles Chan40132b32017-01-22 00:19:37 -0800183 protected void initDriverId() {
184 driverId = coreService.registerApplication(
185 "org.onosproject.driver.Ofdpa2Pipeline");
186 }
187
188 protected void initGroupHander(PipelinerContext context) {
189 groupHandler = new Ofdpa2GroupHandler();
190 groupHandler.init(deviceId, context);
191 }
192
Saurav Das822c4e22015-10-23 10:51:11 -0700193 protected void initializePipeline() {
Charles Chan188ebf52015-12-23 00:15:11 -0800194 // OF-DPA does not require initializing the pipeline as it puts default
195 // rules automatically in the hardware. However emulation of OFDPA in
196 // software switches does require table-miss-entries.
Saurav Das822c4e22015-10-23 10:51:11 -0700197 }
198
Charles Chand1172632017-03-15 17:33:09 -0700199 /**
Alex Yashchuka3679532017-12-08 17:40:05 +0200200 * Determines whether this pipeline requires MPLS POP instruction.
201 *
202 * @return true to use MPLS POP instruction
203 */
204 public boolean requireMplsPop() {
205 return true;
206 }
207
208 /**
209 * Determines whether this pipeline requires MPLS BOS match.
210 *
211 * @return true to use MPLS BOS match
212 */
213 public boolean requireMplsBosMatch() {
214 return true;
215 }
216
217 /**
218 * Determines whether this pipeline requires MPLS TTL decrement and copy.
219 *
220 * @return true to use MPLS TTL decrement and copy
221 */
222 public boolean requireMplsTtlModification() {
223 return true;
224 }
225
226 /**
Charles Chand1172632017-03-15 17:33:09 -0700227 * Determines whether this pipeline requires OFDPA match and set VLAN extensions.
228 *
229 * @return true to use the extensions
230 */
231 protected boolean requireVlanExtensions() {
232 return true;
233 }
234
Saurav Das86d13e82017-04-28 17:03:48 -0700235 /**
236 * Determines whether in-port should be matched on in TMAC table rules.
237 *
238 * @return true if match on in-port should be programmed
239 */
240 protected boolean matchInPortTmacTable() {
241 return true;
242 }
243
Charles Chand9e47c62017-10-05 15:17:15 -0700244 /**
245 * Determines whether matching L4 destination port on IPv6 packets is supported in ACL table.
246 *
247 * @return true if matching L4 destination port on IPv6 packets is supported in ACL table.
248 */
249 protected boolean supportIpv6L4Dst() {
250 return true;
251 }
252
Saurav Dasf3f75942018-01-25 09:49:01 -0800253 /**
254 * Determines whether this driver should continue to retry flows that point
255 * to empty groups. See CORD-554.
256 *
257 * @return true if the driver should retry flows
258 */
259 protected boolean shouldRetry() {
260 return true;
261 }
262
Charles Chand57552d2018-03-02 15:41:41 -0800263 /**
264 * Determines whether this driver requires unicast flow to be installed before multicast flow
265 * in TMAC table.
266 *
267 * @return true if required
268 */
269 protected boolean requireUnicastBeforeMulticast() {
270 return false;
271 }
272
Andrea Campanella84ac4df2018-04-30 11:48:55 +0200273 /**
274 * Determines whether this driver supports installing a clearDeferred action on table 30.
275 *
276 * @return true if required
277 */
278 protected boolean supportsUnicastBlackHole() {
279 return true;
280 }
281
Saurav Das822c4e22015-10-23 10:51:11 -0700282 //////////////////////////////////////
283 // Flow Objectives
284 //////////////////////////////////////
285
286 @Override
287 public void filter(FilteringObjective filteringObjective) {
288 if (filteringObjective.type() == FilteringObjective.Type.PERMIT) {
289 processFilter(filteringObjective,
290 filteringObjective.op() == Objective.Operation.ADD,
291 filteringObjective.appId());
292 } else {
293 // Note that packets that don't match the PERMIT filter are
294 // automatically denied. The DENY filter is used to deny packets
295 // that are otherwise permitted by the PERMIT filter.
296 // Use ACL table flow rules here for DENY filtering objectives
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530297 log.warn("filter objective other than PERMIT currently not supported");
Saurav Das822c4e22015-10-23 10:51:11 -0700298 fail(filteringObjective, ObjectiveError.UNSUPPORTED);
299 }
300 }
301
302 @Override
303 public void forward(ForwardingObjective fwd) {
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700304 Collection<FlowRule> rules = processForward(fwd);
Saurav Das25190812016-05-27 13:54:07 -0700305 if (rules == null || rules.isEmpty()) {
306 // Assumes fail message has already been generated to the objective
307 // context. Returning here prevents spurious pass message to be
308 // generated by FlowRule service for empty flowOps.
309 return;
310 }
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700311 sendForward(fwd, rules);
312 }
313
Charles Chan93090352018-03-02 13:26:22 -0800314 private void sendForward(ForwardingObjective fwd, Collection<FlowRule> rules) {
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700315 FlowRuleOperations.Builder flowOpsBuilder = FlowRuleOperations.builder();
Saurav Das822c4e22015-10-23 10:51:11 -0700316 switch (fwd.op()) {
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700317 case ADD:
318 rules.stream()
319 .filter(Objects::nonNull)
320 .forEach(flowOpsBuilder::add);
Saurav Dasd2fded02016-12-02 15:43:47 -0800321 log.debug("Applying a add fwd-obj {} to sw:{}", fwd.id(), deviceId);
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700322 break;
323 case REMOVE:
324 rules.stream()
325 .filter(Objects::nonNull)
326 .forEach(flowOpsBuilder::remove);
Pier Ventre42287df2016-11-09 14:17:26 -0800327 log.debug("Deleting a flow rule to sw:{}", deviceId);
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700328 break;
329 default:
330 fail(fwd, ObjectiveError.UNKNOWN);
331 log.warn("Unknown forwarding type {}", fwd.op());
Saurav Das822c4e22015-10-23 10:51:11 -0700332 }
333
Saurav Das822c4e22015-10-23 10:51:11 -0700334 flowRuleService.apply(flowOpsBuilder.build(new FlowRuleOperationsContext() {
335 @Override
336 public void onSuccess(FlowRuleOperations ops) {
337 pass(fwd);
338 }
339
340 @Override
341 public void onError(FlowRuleOperations ops) {
342 fail(fwd, ObjectiveError.FLOWINSTALLATIONFAILED);
343 }
344 }));
Saurav Das822c4e22015-10-23 10:51:11 -0700345 }
346
347 @Override
348 public void next(NextObjective nextObjective) {
Saurav Das8a0732e2015-11-20 15:27:53 -0800349 NextGroup nextGroup = flowObjectiveStore.getNextGroup(nextObjective.id());
350 switch (nextObjective.op()) {
351 case ADD:
Saurav Das4f980082015-11-05 13:39:15 -0800352 if (nextGroup != null) {
Saurav Das8a0732e2015-11-20 15:27:53 -0800353 log.warn("Cannot add next {} that already exists in device {}",
354 nextObjective.id(), deviceId);
355 return;
356 }
Saurav Das1547b3f2017-05-05 17:01:08 -0700357 log.debug("Processing NextObjective id {} in dev {} - add group",
Saurav Das8a0732e2015-11-20 15:27:53 -0800358 nextObjective.id(), deviceId);
Charles Chan425854b2016-04-11 15:32:12 -0700359 groupHandler.addGroup(nextObjective);
Saurav Das8a0732e2015-11-20 15:27:53 -0800360 break;
361 case ADD_TO_EXISTING:
362 if (nextGroup != null) {
Saurav Das1547b3f2017-05-05 17:01:08 -0700363 log.debug("Processing NextObjective id {} in dev {} - add bucket",
Saurav Das8a0732e2015-11-20 15:27:53 -0800364 nextObjective.id(), deviceId);
Charles Chan425854b2016-04-11 15:32:12 -0700365 groupHandler.addBucketToGroup(nextObjective, nextGroup);
Saurav Das4f980082015-11-05 13:39:15 -0800366 } else {
Saurav Das8a0732e2015-11-20 15:27:53 -0800367 // it is possible that group-chain has not been fully created yet
Saurav Das423fe2b2015-12-04 10:52:59 -0800368 log.debug("Waiting to add bucket to group for next-id:{} in dev:{}",
369 nextObjective.id(), deviceId);
Yi Tseng47f82dc2017-03-05 22:48:39 -0800370
371 // by design multiple pending bucket is allowed for the group
372 groupHandler.pendingBuckets.compute(nextObjective.id(), (nextId, pendBkts) -> {
373 if (pendBkts == null) {
374 pendBkts = Sets.newHashSet();
375 }
376 pendBkts.add(nextObjective);
377 return pendBkts;
378 });
Saurav Das4f980082015-11-05 13:39:15 -0800379 }
Saurav Das8a0732e2015-11-20 15:27:53 -0800380 break;
381 case REMOVE:
382 if (nextGroup == null) {
383 log.warn("Cannot remove next {} that does not exist in device {}",
384 nextObjective.id(), deviceId);
385 return;
386 }
Saurav Das1547b3f2017-05-05 17:01:08 -0700387 log.debug("Processing NextObjective id {} in dev {} - remove group",
Saurav Das8a0732e2015-11-20 15:27:53 -0800388 nextObjective.id(), deviceId);
Charles Chan425854b2016-04-11 15:32:12 -0700389 groupHandler.removeGroup(nextObjective, nextGroup);
Saurav Das8a0732e2015-11-20 15:27:53 -0800390 break;
391 case REMOVE_FROM_EXISTING:
392 if (nextGroup == null) {
393 log.warn("Cannot remove from next {} that does not exist in device {}",
394 nextObjective.id(), deviceId);
395 return;
396 }
Saurav Das1547b3f2017-05-05 17:01:08 -0700397 log.debug("Processing NextObjective id {} in dev {} - remove bucket",
Saurav Das8a0732e2015-11-20 15:27:53 -0800398 nextObjective.id(), deviceId);
Charles Chan425854b2016-04-11 15:32:12 -0700399 groupHandler.removeBucketFromGroup(nextObjective, nextGroup);
Saurav Das8a0732e2015-11-20 15:27:53 -0800400 break;
Jonghwan Hyun25199f22018-02-12 16:43:45 +0900401 case MODIFY:
402 if (nextGroup == null) {
403 log.warn("Cannot modify next {} that does not exist in device {}",
404 nextObjective.id(), deviceId);
405 return;
406 }
407 log.debug("Processing NextObjective id {} in dev {} - modify bucket",
408 nextObjective.id(), deviceId);
409 groupHandler.modifyBucketFromGroup(nextObjective, nextGroup);
410 break;
Saurav Dasceccf242017-08-03 18:30:35 -0700411 case VERIFY:
412 if (nextGroup == null) {
413 log.warn("Cannot verify next {} that does not exist in device {}",
414 nextObjective.id(), deviceId);
415 return;
416 }
417 log.debug("Processing NextObjective id {} in dev {} - verify",
418 nextObjective.id(), deviceId);
419 groupHandler.verifyGroup(nextObjective, nextGroup);
420 break;
Saurav Das8a0732e2015-11-20 15:27:53 -0800421 default:
Saurav Das4f980082015-11-05 13:39:15 -0800422 log.warn("Unsupported operation {}", nextObjective.op());
Saurav Das822c4e22015-10-23 10:51:11 -0700423 }
424 }
425
426 //////////////////////////////////////
427 // Flow handling
428 //////////////////////////////////////
429
430 /**
Yi Tseng0fdc07e2017-04-24 19:17:08 -0700431 * As per OFDPA 2.0 TTP, filtering of VLAN ids and MAC addresses (for routing)
432 * configured on switch ports happen in different tables.
Saurav Das822c4e22015-10-23 10:51:11 -0700433 *
434 * @param filt the filtering objective
435 * @param install indicates whether to add or remove the objective
436 * @param applicationId the application that sent this objective
437 */
Saurav Das52025962016-01-28 22:30:01 -0800438 protected void processFilter(FilteringObjective filt,
439 boolean install, ApplicationId applicationId) {
Saurav Das822c4e22015-10-23 10:51:11 -0700440 // This driver only processes filtering criteria defined with switch
441 // ports as the key
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530442 PortCriterion portCriterion = null;
Saurav Das822c4e22015-10-23 10:51:11 -0700443 EthCriterion ethCriterion = null;
444 VlanIdCriterion vidCriterion = null;
Saurav Das822c4e22015-10-23 10:51:11 -0700445 if (!filt.key().equals(Criteria.dummy()) &&
446 filt.key().type() == Criterion.Type.IN_PORT) {
447 portCriterion = (PortCriterion) filt.key();
Saurav Das822c4e22015-10-23 10:51:11 -0700448 }
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530449 if (portCriterion == null) {
450 log.debug("No IN_PORT defined in filtering objective from app: {}",
451 applicationId);
452 } else {
453 log.debug("Received filtering objective for dev/port: {}/{}", deviceId,
454 portCriterion.port());
455 }
Saurav Das822c4e22015-10-23 10:51:11 -0700456 // convert filtering conditions for switch-intfs into flowrules
457 FlowRuleOperations.Builder ops = FlowRuleOperations.builder();
458 for (Criterion criterion : filt.conditions()) {
Yi Tseng0fdc07e2017-04-24 19:17:08 -0700459 switch (criterion.type()) {
460 case ETH_DST:
461 case ETH_DST_MASKED:
462 ethCriterion = (EthCriterion) criterion;
463 break;
464 case VLAN_VID:
465 vidCriterion = (VlanIdCriterion) criterion;
466 break;
467 default:
468 log.warn("Unsupported filter {}", criterion);
469 fail(filt, ObjectiveError.UNSUPPORTED);
470 return;
Saurav Das822c4e22015-10-23 10:51:11 -0700471 }
472 }
473
Saurav Das0e99e2b2015-10-28 12:39:42 -0700474 VlanId assignedVlan = null;
Charles Chane849c192016-01-11 18:28:54 -0800475 if (vidCriterion != null) {
Charles Chand55e84d2016-03-30 17:54:24 -0700476 // Use the VLAN in criterion if metadata is not present and the traffic is tagged
Charles Chan9174e6b2017-12-19 19:55:57 -0800477 if (!vidCriterion.vlanId().equals(VlanId.NONE)) {
Charles Chane849c192016-01-11 18:28:54 -0800478 assignedVlan = vidCriterion.vlanId();
Pier3cc7e662018-03-07 11:42:50 +0100479 }
480 // If the meta VLAN is present let's update the assigned vlan
481 if (filt.meta() != null) {
482 VlanId metaVlan = readVlanFromTreatment(filt.meta());
483 if (metaVlan != null) {
484 assignedVlan = metaVlan;
485 }
Charles Chand55e84d2016-03-30 17:54:24 -0700486 }
Charles Chane849c192016-01-11 18:28:54 -0800487
Charles Chand55e84d2016-03-30 17:54:24 -0700488 if (assignedVlan == null) {
489 log.error("Driver fails to extract VLAN information. "
Frank Wangd8ab0962017-08-11 11:09:30 +0800490 + "Not processing VLAN filters on device {}.", deviceId);
Charles Chand55e84d2016-03-30 17:54:24 -0700491 log.debug("VLAN ID in criterion={}, metadata={}",
492 readVlanFromTreatment(filt.meta()), vidCriterion.vlanId());
493 fail(filt, ObjectiveError.BADPARAMS);
494 return;
Saurav Das0e99e2b2015-10-28 12:39:42 -0700495 }
496 }
497
Yi Tseng3a77b4f2017-02-06 15:02:17 -0800498 if (ethCriterion == null || ethCriterion.mac().equals(NONE)) {
Charles Chan985b12e2016-05-11 19:47:22 -0700499 // NOTE: it is possible that a filtering objective only has vidCriterion
Daniel Ginsburg16e5ed42018-04-30 19:27:19 -0400500 log.debug("filtering objective missing dstMac, won't program TMAC table");
Saurav Das822c4e22015-10-23 10:51:11 -0700501 } else {
Charles Chand57552d2018-03-02 15:41:41 -0800502 MacAddress unicastMac = readEthDstFromTreatment(filt.meta());
Charles Chan206506b2018-03-02 16:43:28 -0800503 List<List<FlowRule>> allStages = processEthDstFilter(portCriterion, ethCriterion,
Charles Chand57552d2018-03-02 15:41:41 -0800504 vidCriterion, assignedVlan, unicastMac, applicationId);
Charles Chan206506b2018-03-02 16:43:28 -0800505 for (List<FlowRule> flowRules : allStages) {
Charles Chand57552d2018-03-02 15:41:41 -0800506 log.trace("Starting a new flow rule stage for TMAC table flow");
Charles Chan206506b2018-03-02 16:43:28 -0800507 ops.newStage();
Charles Chan9174e6b2017-12-19 19:55:57 -0800508
Charles Chan206506b2018-03-02 16:43:28 -0800509 for (FlowRule flowRule : flowRules) {
510 log.trace("{} flow rules in TMAC table: {} for dev: {}",
511 (install) ? "adding" : "removing", flowRules, deviceId);
Charles Chan206506b2018-03-02 16:43:28 -0800512 if (install) {
513 ops = ops.add(flowRule);
Charles Chan9174e6b2017-12-19 19:55:57 -0800514 } else {
Charles Chan206506b2018-03-02 16:43:28 -0800515 // NOTE: Only remove TMAC flow when there is no more enabled port within the
516 // same VLAN on this device if TMAC doesn't support matching on in_port.
517 if (matchInPortTmacTable() || (filt.meta() != null && filt.meta().clearedDeferred())) {
518 ops = ops.remove(flowRule);
519 } else {
520 log.debug("Abort TMAC flow removal on {}. Some other ports still share this TMAC flow");
521 }
Charles Chan9174e6b2017-12-19 19:55:57 -0800522 }
523 }
Saurav Das822c4e22015-10-23 10:51:11 -0700524 }
525 }
526
Charles Chan985b12e2016-05-11 19:47:22 -0700527 if (vidCriterion == null) {
528 // NOTE: it is possible that a filtering objective only has ethCriterion
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530529 log.info("filtering objective missing VLAN, cannot program VLAN Table");
Saurav Das822c4e22015-10-23 10:51:11 -0700530 } else {
Charles Chan206506b2018-03-02 16:43:28 -0800531 List<List<FlowRule>> allStages = processVlanIdFilter(
Charles Chan14967c22015-12-07 11:11:50 -0800532 portCriterion, vidCriterion, assignedVlan, applicationId);
Charles Chan206506b2018-03-02 16:43:28 -0800533 for (List<FlowRule> flowRules : allStages) {
Charles Chand57552d2018-03-02 15:41:41 -0800534 log.trace("Starting a new flow rule stage for VLAN table flow");
Charles Chan206506b2018-03-02 16:43:28 -0800535 ops.newStage();
Charles Chan14967c22015-12-07 11:11:50 -0800536
Charles Chan206506b2018-03-02 16:43:28 -0800537 for (FlowRule flowRule : flowRules) {
538 log.trace("{} flow rules in VLAN table: {} for dev: {}",
539 (install) ? "adding" : "removing", flowRule, deviceId);
540 ops = install ? ops.add(flowRule) : ops.remove(flowRule);
Charles Chand1172632017-03-15 17:33:09 -0700541 }
Saurav Das822c4e22015-10-23 10:51:11 -0700542 }
543 }
544
Saurav Das822c4e22015-10-23 10:51:11 -0700545 // apply filtering flow rules
546 flowRuleService.apply(ops.build(new FlowRuleOperationsContext() {
547 @Override
548 public void onSuccess(FlowRuleOperations ops) {
Saurav Das018605f2017-02-18 14:05:44 -0800549 log.debug("Applied {} filtering rules in device {}",
Saurav Das822c4e22015-10-23 10:51:11 -0700550 ops.stages().get(0).size(), deviceId);
551 pass(filt);
552 }
553
554 @Override
555 public void onError(FlowRuleOperations ops) {
556 log.info("Failed to apply all filtering rules in dev {}", deviceId);
557 fail(filt, ObjectiveError.FLOWINSTALLATIONFAILED);
558 }
559 }));
560
561 }
562
563 /**
Charles Chand1172632017-03-15 17:33:09 -0700564 * Internal implementation of processVlanIdFilter.
565 * <p>
566 * The is_present bit in set_vlan_vid action is required to be 0 in OFDPA i12.
567 * Since it is non-OF spec, we need an extension treatment for that.
568 * The useVlanExtension must be set to false for OFDPA i12.
569 * </p>
Charles Chan206506b2018-03-02 16:43:28 -0800570 * <p>
571 * NOTE: Separate VLAN filtering rules and assignment rules
572 * into different stages in order to guarantee that filtering rules
573 * always go first, as required by OFDPA.
574 * </p>
Saurav Das0e99e2b2015-10-28 12:39:42 -0700575 *
Charles Chanf9e98652016-09-07 16:54:23 -0700576 * @param portCriterion port on device for which this filter is programmed
577 * @param vidCriterion vlan assigned to port, or NONE for untagged
578 * @param assignedVlan assigned vlan-id for untagged packets
579 * @param applicationId for application programming this filter
Charles Chan206506b2018-03-02 16:43:28 -0800580 * @return stages of flow rules for port-vlan filters
Saurav Das822c4e22015-10-23 10:51:11 -0700581 */
Charles Chan206506b2018-03-02 16:43:28 -0800582 protected List<List<FlowRule>> processVlanIdFilter(PortCriterion portCriterion,
Yi Tsengef19de12017-04-24 11:33:05 -0700583 VlanIdCriterion vidCriterion,
584 VlanId assignedVlan,
585 ApplicationId applicationId) {
Charles Chan206506b2018-03-02 16:43:28 -0800586 List<FlowRule> filteringRules = new ArrayList<>();
587 List<FlowRule> assignmentRules = new ArrayList<>();
Saurav Das822c4e22015-10-23 10:51:11 -0700588 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
589 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
Charles Chan14967c22015-12-07 11:11:50 -0800590 TrafficSelector.Builder preSelector = null;
591 TrafficTreatment.Builder preTreatment = null;
592
Saurav Das4f980082015-11-05 13:39:15 -0800593 treatment.transition(TMAC_TABLE);
594
Saurav Das822c4e22015-10-23 10:51:11 -0700595 if (vidCriterion.vlanId() == VlanId.NONE) {
596 // untagged packets are assigned vlans
Charles Chand1172632017-03-15 17:33:09 -0700597 preSelector = DefaultTrafficSelector.builder();
598 if (requireVlanExtensions()) {
599 OfdpaMatchVlanVid ofdpaMatchVlanVid = new OfdpaMatchVlanVid(VlanId.NONE);
600 selector.extension(ofdpaMatchVlanVid, deviceId);
Charles Chanf9e98652016-09-07 16:54:23 -0700601 OfdpaSetVlanVid ofdpaSetVlanVid = new OfdpaSetVlanVid(assignedVlan);
602 treatment.extension(ofdpaSetVlanVid, deviceId);
Charles Chand1172632017-03-15 17:33:09 -0700603
604 OfdpaMatchVlanVid preOfdpaMatchVlanVid = new OfdpaMatchVlanVid(assignedVlan);
605 preSelector.extension(preOfdpaMatchVlanVid, deviceId);
Charles Chanf9e98652016-09-07 16:54:23 -0700606 } else {
Charles Chand1172632017-03-15 17:33:09 -0700607 selector.matchVlanId(VlanId.NONE);
Charles Chanf9e98652016-09-07 16:54:23 -0700608 treatment.setVlanId(assignedVlan);
Charles Chand1172632017-03-15 17:33:09 -0700609
610 preSelector.matchVlanId(assignedVlan);
611 }
612 preTreatment = DefaultTrafficTreatment.builder().transition(TMAC_TABLE);
613 } else {
614 if (requireVlanExtensions()) {
615 OfdpaMatchVlanVid ofdpaMatchVlanVid = new OfdpaMatchVlanVid(vidCriterion.vlanId());
616 selector.extension(ofdpaMatchVlanVid, deviceId);
617 } else {
618 selector.matchVlanId(vidCriterion.vlanId());
Charles Chanf9e98652016-09-07 16:54:23 -0700619 }
Charles Chan14967c22015-12-07 11:11:50 -0800620
Charles Chand55e84d2016-03-30 17:54:24 -0700621 if (!assignedVlan.equals(vidCriterion.vlanId())) {
Charles Chand1172632017-03-15 17:33:09 -0700622 if (requireVlanExtensions()) {
Charles Chanc03782d2017-01-31 13:57:55 -0800623 OfdpaSetVlanVid ofdpaSetVlanVid = new OfdpaSetVlanVid(assignedVlan);
624 treatment.extension(ofdpaSetVlanVid, deviceId);
625 } else {
626 treatment.setVlanId(assignedVlan);
627 }
Charles Chand55e84d2016-03-30 17:54:24 -0700628 }
Saurav Das822c4e22015-10-23 10:51:11 -0700629 }
Saurav Das822c4e22015-10-23 10:51:11 -0700630
631 // ofdpa cannot match on ALL portnumber, so we need to use separate
632 // rules for each port.
Charles Chan14967c22015-12-07 11:11:50 -0800633 List<PortNumber> portnums = new ArrayList<>();
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530634 if (portCriterion != null) {
635 if (PortNumber.ALL.equals(portCriterion.port())) {
636 for (Port port : deviceService.getPorts(deviceId)) {
637 if (port.number().toLong() > 0 && port.number().toLong() < OFPP_MAX) {
638 portnums.add(port.number());
639 }
Saurav Das822c4e22015-10-23 10:51:11 -0700640 }
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530641 } else {
642 portnums.add(portCriterion.port());
Saurav Das822c4e22015-10-23 10:51:11 -0700643 }
644 } else {
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530645 log.warn("Filtering Objective missing Port Criterion . " +
Charles Chan206506b2018-03-02 16:43:28 -0800646 "VLAN Table cannot be programmed for {}", deviceId);
Saurav Das822c4e22015-10-23 10:51:11 -0700647 }
Saurav Das4f980082015-11-05 13:39:15 -0800648
Saurav Das822c4e22015-10-23 10:51:11 -0700649 for (PortNumber pnum : portnums) {
Saurav Das4f980082015-11-05 13:39:15 -0800650 // create rest of flowrule
Saurav Das822c4e22015-10-23 10:51:11 -0700651 selector.matchInPort(pnum);
652 FlowRule rule = DefaultFlowRule.builder()
653 .forDevice(deviceId)
654 .withSelector(selector.build())
655 .withTreatment(treatment.build())
656 .withPriority(DEFAULT_PRIORITY)
657 .fromApp(applicationId)
658 .makePermanent()
659 .forTable(VLAN_TABLE).build();
Charles Chan206506b2018-03-02 16:43:28 -0800660 assignmentRules.add(rule);
Charles Chan14967c22015-12-07 11:11:50 -0800661
662 if (preSelector != null) {
663 preSelector.matchInPort(pnum);
664 FlowRule preRule = DefaultFlowRule.builder()
665 .forDevice(deviceId)
666 .withSelector(preSelector.build())
667 .withTreatment(preTreatment.build())
668 .withPriority(DEFAULT_PRIORITY)
669 .fromApp(applicationId)
670 .makePermanent()
671 .forTable(VLAN_TABLE).build();
Charles Chan206506b2018-03-02 16:43:28 -0800672 filteringRules.add(preRule);
Charles Chan14967c22015-12-07 11:11:50 -0800673 }
Saurav Das822c4e22015-10-23 10:51:11 -0700674 }
Charles Chan1e58aa62018-03-06 22:10:15 -0800675 return ImmutableList.of(filteringRules, assignmentRules);
Saurav Das822c4e22015-10-23 10:51:11 -0700676 }
677
678 /**
679 * Allows routed packets with correct destination MAC to be directed
Charles Chand57552d2018-03-02 15:41:41 -0800680 * to unicast routing table, multicast routing table or MPLS forwarding table.
Saurav Das822c4e22015-10-23 10:51:11 -0700681 *
682 * @param portCriterion port on device for which this filter is programmed
683 * @param ethCriterion dstMac of device for which is filter is programmed
684 * @param vidCriterion vlan assigned to port, or NONE for untagged
Saurav Das0e99e2b2015-10-28 12:39:42 -0700685 * @param assignedVlan assigned vlan-id for untagged packets
Charles Chand57552d2018-03-02 15:41:41 -0800686 * @param unicastMac some switches require a unicast TMAC flow to be programmed before multicast
687 * TMAC flow. This MAC address will be used for the unicast TMAC flow.
688 * This is unused if the filtering objective is a unicast.
Saurav Das822c4e22015-10-23 10:51:11 -0700689 * @param applicationId for application programming this filter
Charles Chan206506b2018-03-02 16:43:28 -0800690 * @return stages of flow rules for port-vlan filters
Saurav Das822c4e22015-10-23 10:51:11 -0700691
692 */
Charles Chan206506b2018-03-02 16:43:28 -0800693 protected List<List<FlowRule>> processEthDstFilter(PortCriterion portCriterion,
Saurav Das822c4e22015-10-23 10:51:11 -0700694 EthCriterion ethCriterion,
695 VlanIdCriterion vidCriterion,
Saurav Das0e99e2b2015-10-28 12:39:42 -0700696 VlanId assignedVlan,
Charles Chand57552d2018-03-02 15:41:41 -0800697 MacAddress unicastMac,
Saurav Das822c4e22015-10-23 10:51:11 -0700698 ApplicationId applicationId) {
Charles Chan5270ed02016-01-30 23:22:37 -0800699 // Consider PortNumber.ANY as wildcard. Match ETH_DST only
Sivachidambaram Subramanianc598d062017-05-16 22:42:06 +0530700 if (portCriterion != null && PortNumber.ANY.equals(portCriterion.port())) {
Charles Chan5270ed02016-01-30 23:22:37 -0800701 return processEthDstOnlyFilter(ethCriterion, applicationId);
702 }
703
Charles Chan5b9df8d2016-03-28 22:21:40 -0700704 // Multicast MAC
705 if (ethCriterion.mask() != null) {
Charles Chand57552d2018-03-02 15:41:41 -0800706 return processMcastEthDstFilter(ethCriterion, assignedVlan, unicastMac, applicationId);
Charles Chan5b9df8d2016-03-28 22:21:40 -0700707 }
708
Saurav Das822c4e22015-10-23 10:51:11 -0700709 //handling untagged packets via assigned VLAN
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530710 if (vidCriterion != null && vidCriterion.vlanId() == VlanId.NONE) {
Saurav Das0e99e2b2015-10-28 12:39:42 -0700711 vidCriterion = (VlanIdCriterion) Criteria.matchVlanId(assignedVlan);
Saurav Das822c4e22015-10-23 10:51:11 -0700712 }
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530713 List<FlowRule> rules = new ArrayList<>();
714 OfdpaMatchVlanVid ofdpaMatchVlanVid = null;
715 if (vidCriterion != null && requireVlanExtensions()) {
716 ofdpaMatchVlanVid = new OfdpaMatchVlanVid(vidCriterion.vlanId());
717 }
Saurav Das822c4e22015-10-23 10:51:11 -0700718 // ofdpa cannot match on ALL portnumber, so we need to use separate
719 // rules for each port.
Yuta HIGUCHI2dce08a2017-04-20 21:57:48 -0700720 List<PortNumber> portnums = new ArrayList<>();
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530721 if (portCriterion != null) {
722 if (PortNumber.ALL.equals(portCriterion.port())) {
723 for (Port port : deviceService.getPorts(deviceId)) {
724 if (port.number().toLong() > 0 && port.number().toLong() < OFPP_MAX) {
725 portnums.add(port.number());
726 }
Saurav Das822c4e22015-10-23 10:51:11 -0700727 }
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530728 } else {
729 portnums.add(portCriterion.port());
730 }
731 for (PortNumber pnum : portnums) {
732 rules.add(buildTmacRuleForIpv4(ethCriterion,
733 vidCriterion,
734 ofdpaMatchVlanVid,
735 applicationId,
736 pnum));
737 rules.add(buildTmacRuleForMpls(ethCriterion,
738 vidCriterion,
739 ofdpaMatchVlanVid,
740 applicationId,
741 pnum));
742 rules.add(buildTmacRuleForIpv6(ethCriterion,
743 vidCriterion,
744 ofdpaMatchVlanVid,
745 applicationId,
746 pnum));
Saurav Das822c4e22015-10-23 10:51:11 -0700747 }
748 } else {
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530749 rules.add(buildTmacRuleForIpv4(ethCriterion,
750 vidCriterion,
751 ofdpaMatchVlanVid,
752 applicationId,
753 null));
754 rules.add(buildTmacRuleForMpls(ethCriterion,
755 vidCriterion,
756 ofdpaMatchVlanVid,
757 applicationId,
758 null));
759 rules.add(buildTmacRuleForIpv6(ethCriterion,
760 vidCriterion,
761 ofdpaMatchVlanVid,
762 applicationId,
763 null));
Saurav Das822c4e22015-10-23 10:51:11 -0700764 }
Charles Chan206506b2018-03-02 16:43:28 -0800765 return ImmutableList.of(rules);
Saurav Das822c4e22015-10-23 10:51:11 -0700766 }
767
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530768 /**
769 * Builds TMAC rules for IPv4 packets.
770 *
Charles Chan9174e6b2017-12-19 19:55:57 -0800771 * @param ethCriterion dst mac matching
772 * @param vidCriterion vlan id assigned to the port
773 * @param ofdpaMatchVlanVid OFDPA vlan id matching
774 * @param applicationId application id
775 * @param pnum port number
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530776 * @return TMAC rule for IPV4 packets
777 */
778 private FlowRule buildTmacRuleForIpv4(EthCriterion ethCriterion,
779 VlanIdCriterion vidCriterion,
780 OfdpaMatchVlanVid ofdpaMatchVlanVid,
781 ApplicationId applicationId,
782 PortNumber pnum) {
783 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
784 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
785 if (pnum != null) {
786 if (matchInPortTmacTable()) {
787 selector.matchInPort(pnum);
788 } else {
Saurav Das73a6cc12017-12-05 15:00:23 -0800789 log.debug("Pipeline does not support IN_PORT matching in TMAC table, " +
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530790 "ignoring the IN_PORT criteria");
791 }
792 }
793 if (vidCriterion != null) {
794 if (requireVlanExtensions()) {
795 selector.extension(ofdpaMatchVlanVid, deviceId);
796 } else {
797 selector.matchVlanId(vidCriterion.vlanId());
798 }
799 }
800 selector.matchEthType(Ethernet.TYPE_IPV4);
801 selector.matchEthDst(ethCriterion.mac());
802 treatment.transition(UNICAST_ROUTING_TABLE);
803 return DefaultFlowRule.builder()
804 .forDevice(deviceId)
805 .withSelector(selector.build())
806 .withTreatment(treatment.build())
807 .withPriority(DEFAULT_PRIORITY)
808 .fromApp(applicationId)
809 .makePermanent()
810 .forTable(TMAC_TABLE).build();
811 }
812
813 /**
814 * Builds TMAC rule for MPLS packets.
815 *
Charles Chan9174e6b2017-12-19 19:55:57 -0800816 * @param ethCriterion dst mac matching
817 * @param vidCriterion vlan id assigned to the port
818 * @param ofdpaMatchVlanVid OFDPA vlan id matching
819 * @param applicationId application id
820 * @param pnum port number
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530821 * @return TMAC rule for MPLS packets
822 */
823 private FlowRule buildTmacRuleForMpls(EthCriterion ethCriterion,
824 VlanIdCriterion vidCriterion,
825 OfdpaMatchVlanVid ofdpaMatchVlanVid,
826 ApplicationId applicationId,
827 PortNumber pnum) {
828 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
829 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
830 if (pnum != null) {
831 if (matchInPortTmacTable()) {
832 selector.matchInPort(pnum);
833 } else {
Saurav Das73a6cc12017-12-05 15:00:23 -0800834 log.debug("Pipeline does not support IN_PORT matching in TMAC table, " +
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530835 "ignoring the IN_PORT criteria");
836 }
837 }
838 if (vidCriterion != null) {
839 if (requireVlanExtensions()) {
840 selector.extension(ofdpaMatchVlanVid, deviceId);
841 } else {
842 selector.matchVlanId(vidCriterion.vlanId());
843 }
844 }
845 selector.matchEthType(Ethernet.MPLS_UNICAST);
846 selector.matchEthDst(ethCriterion.mac());
847 treatment.transition(MPLS_TABLE_0);
848 return DefaultFlowRule.builder()
849 .forDevice(deviceId)
850 .withSelector(selector.build())
851 .withTreatment(treatment.build())
852 .withPriority(DEFAULT_PRIORITY)
853 .fromApp(applicationId)
854 .makePermanent()
855 .forTable(TMAC_TABLE).build();
856 }
857
858 /**
859 * Builds TMAC rules for IPv6 packets.
860 *
Charles Chan9174e6b2017-12-19 19:55:57 -0800861 * @param ethCriterion dst mac matching
862 * @param vidCriterion vlan id assigned to the port
863 * @param ofdpaMatchVlanVid OFDPA vlan id matching
864 * @param applicationId application id
865 * @param pnum port number
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530866 * @return TMAC rule for IPV6 packets
867 */
868 private FlowRule buildTmacRuleForIpv6(EthCriterion ethCriterion,
869 VlanIdCriterion vidCriterion,
870 OfdpaMatchVlanVid ofdpaMatchVlanVid,
871 ApplicationId applicationId,
872 PortNumber pnum) {
873 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
874 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
875 if (pnum != null) {
876 if (matchInPortTmacTable()) {
877 selector.matchInPort(pnum);
878 } else {
Saurav Das73a6cc12017-12-05 15:00:23 -0800879 log.debug("Pipeline does not support IN_PORT matching in TMAC table, " +
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530880 "ignoring the IN_PORT criteria");
881 }
882 }
883 if (vidCriterion != null) {
884 if (requireVlanExtensions()) {
885 selector.extension(ofdpaMatchVlanVid, deviceId);
886 } else {
887 selector.matchVlanId(vidCriterion.vlanId());
888 }
889 }
890 selector.matchEthType(Ethernet.TYPE_IPV6);
891 selector.matchEthDst(ethCriterion.mac());
892 treatment.transition(UNICAST_ROUTING_TABLE);
893 return DefaultFlowRule.builder()
894 .forDevice(deviceId)
895 .withSelector(selector.build())
896 .withTreatment(treatment.build())
897 .withPriority(DEFAULT_PRIORITY)
898 .fromApp(applicationId)
899 .makePermanent()
900 .forTable(TMAC_TABLE).build();
901 }
902
Charles Chan206506b2018-03-02 16:43:28 -0800903 protected List<List<FlowRule>> processEthDstOnlyFilter(EthCriterion ethCriterion,
Yi Tsengef19de12017-04-24 11:33:05 -0700904 ApplicationId applicationId) {
Pier Luigi0e358632017-01-31 09:35:05 -0800905 ImmutableList.Builder<FlowRule> builder = ImmutableList.builder();
906
Charles Chan5270ed02016-01-30 23:22:37 -0800907 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
908 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
909 selector.matchEthType(Ethernet.TYPE_IPV4);
910 selector.matchEthDst(ethCriterion.mac());
911 treatment.transition(UNICAST_ROUTING_TABLE);
912 FlowRule rule = DefaultFlowRule.builder()
913 .forDevice(deviceId)
914 .withSelector(selector.build())
915 .withTreatment(treatment.build())
916 .withPriority(DEFAULT_PRIORITY)
917 .fromApp(applicationId)
918 .makePermanent()
919 .forTable(TMAC_TABLE).build();
Pier Luigi0e358632017-01-31 09:35:05 -0800920 builder.add(rule);
921
922 selector = DefaultTrafficSelector.builder();
923 treatment = DefaultTrafficTreatment.builder();
924 selector.matchEthType(Ethernet.TYPE_IPV6);
925 selector.matchEthDst(ethCriterion.mac());
926 treatment.transition(UNICAST_ROUTING_TABLE);
927 rule = DefaultFlowRule.builder()
928 .forDevice(deviceId)
929 .withSelector(selector.build())
930 .withTreatment(treatment.build())
931 .withPriority(DEFAULT_PRIORITY)
932 .fromApp(applicationId)
933 .makePermanent()
934 .forTable(TMAC_TABLE).build();
Charles Chan206506b2018-03-02 16:43:28 -0800935 return ImmutableList.of(builder.add(rule).build());
Charles Chan5270ed02016-01-30 23:22:37 -0800936 }
937
Charles Chan206506b2018-03-02 16:43:28 -0800938 List<List<FlowRule>> processMcastEthDstFilter(EthCriterion ethCriterion,
Charles Chanb4879a52017-10-20 19:09:16 -0700939 VlanId assignedVlan,
Charles Chand57552d2018-03-02 15:41:41 -0800940 MacAddress unicastMac,
Yi Tsengef19de12017-04-24 11:33:05 -0700941 ApplicationId applicationId) {
Charles Chand57552d2018-03-02 15:41:41 -0800942 ImmutableList.Builder<FlowRule> unicastFlows = ImmutableList.builder();
943 ImmutableList.Builder<FlowRule> multicastFlows = ImmutableList.builder();
944 TrafficSelector.Builder selector;
945 TrafficTreatment.Builder treatment;
Charles Chanb4879a52017-10-20 19:09:16 -0700946 FlowRule rule;
Julia Ferguson65428c32017-08-10 18:15:24 +0000947
Charles Chanb4879a52017-10-20 19:09:16 -0700948 if (IPV4_MULTICAST.equals(ethCriterion.mac())) {
Charles Chand57552d2018-03-02 15:41:41 -0800949 if (requireUnicastBeforeMulticast()) {
950 selector = DefaultTrafficSelector.builder();
951 treatment = DefaultTrafficTreatment.builder();
952 selector.matchEthType(Ethernet.TYPE_IPV4);
953 selector.matchEthDst(unicastMac);
954 selector.matchVlanId(assignedVlan);
955 treatment.transition(UNICAST_ROUTING_TABLE);
956 rule = DefaultFlowRule.builder()
957 .forDevice(deviceId)
958 .withSelector(selector.build())
959 .withTreatment(treatment.build())
960 .withPriority(DEFAULT_PRIORITY)
961 .fromApp(applicationId)
962 .makePermanent()
963 .forTable(TMAC_TABLE).build();
964 unicastFlows.add(rule);
965 }
966
967 selector = DefaultTrafficSelector.builder();
968 treatment = DefaultTrafficTreatment.builder();
Charles Chanb4879a52017-10-20 19:09:16 -0700969 selector.matchEthType(Ethernet.TYPE_IPV4);
970 selector.matchEthDstMasked(ethCriterion.mac(), ethCriterion.mask());
971 selector.matchVlanId(assignedVlan);
972 treatment.transition(MULTICAST_ROUTING_TABLE);
973 rule = DefaultFlowRule.builder()
974 .forDevice(deviceId)
975 .withSelector(selector.build())
976 .withTreatment(treatment.build())
977 .withPriority(DEFAULT_PRIORITY)
978 .fromApp(applicationId)
979 .makePermanent()
980 .forTable(TMAC_TABLE).build();
Charles Chand57552d2018-03-02 15:41:41 -0800981 multicastFlows.add(rule);
Charles Chanb4879a52017-10-20 19:09:16 -0700982 }
983
984 if (IPV6_MULTICAST.equals(ethCriterion.mac())) {
Charles Chand57552d2018-03-02 15:41:41 -0800985 if (requireUnicastBeforeMulticast()) {
986 selector = DefaultTrafficSelector.builder();
987 treatment = DefaultTrafficTreatment.builder();
988 selector.matchEthType(Ethernet.TYPE_IPV6);
989 selector.matchEthDst(unicastMac);
990 selector.matchVlanId(assignedVlan);
991 treatment.transition(UNICAST_ROUTING_TABLE);
992 rule = DefaultFlowRule.builder()
993 .forDevice(deviceId)
994 .withSelector(selector.build())
995 .withTreatment(treatment.build())
996 .withPriority(DEFAULT_PRIORITY)
997 .fromApp(applicationId)
998 .makePermanent()
999 .forTable(TMAC_TABLE).build();
1000 unicastFlows.add(rule);
1001 }
1002
Charles Chanb4879a52017-10-20 19:09:16 -07001003 selector = DefaultTrafficSelector.builder();
1004 treatment = DefaultTrafficTreatment.builder();
1005 selector.matchEthType(Ethernet.TYPE_IPV6);
1006 selector.matchEthDstMasked(ethCriterion.mac(), ethCriterion.mask());
1007 selector.matchVlanId(assignedVlan);
1008 treatment.transition(MULTICAST_ROUTING_TABLE);
1009 rule = DefaultFlowRule.builder()
1010 .forDevice(deviceId)
1011 .withSelector(selector.build())
1012 .withTreatment(treatment.build())
1013 .withPriority(DEFAULT_PRIORITY)
1014 .fromApp(applicationId)
1015 .makePermanent()
1016 .forTable(TMAC_TABLE).build();
Charles Chand57552d2018-03-02 15:41:41 -08001017 multicastFlows.add(rule);
Charles Chanb4879a52017-10-20 19:09:16 -07001018 }
Charles Chand57552d2018-03-02 15:41:41 -08001019 return ImmutableList.of(unicastFlows.build(), multicastFlows.build());
Charles Chan5b9df8d2016-03-28 22:21:40 -07001020 }
1021
Saurav Das822c4e22015-10-23 10:51:11 -07001022 private Collection<FlowRule> processForward(ForwardingObjective fwd) {
1023 switch (fwd.flag()) {
1024 case SPECIFIC:
1025 return processSpecific(fwd);
1026 case VERSATILE:
1027 return processVersatile(fwd);
1028 default:
1029 fail(fwd, ObjectiveError.UNKNOWN);
1030 log.warn("Unknown forwarding flag {}", fwd.flag());
1031 }
1032 return Collections.emptySet();
1033 }
1034
1035 /**
1036 * In the OF-DPA 2.0 pipeline, versatile forwarding objectives go to the
1037 * ACL table.
1038 * @param fwd the forwarding objective of type 'versatile'
1039 * @return a collection of flow rules to be sent to the switch. An empty
1040 * collection may be returned if there is a problem in processing
1041 * the flow rule
1042 */
Saurav Das52025962016-01-28 22:30:01 -08001043 protected Collection<FlowRule> processVersatile(ForwardingObjective fwd) {
Saurav Das018605f2017-02-18 14:05:44 -08001044 log.debug("Processing versatile forwarding objective:{} in dev:{}",
Saurav Dasd2fded02016-12-02 15:43:47 -08001045 fwd.id(), deviceId);
Saurav Das822c4e22015-10-23 10:51:11 -07001046
1047 EthTypeCriterion ethType =
Saurav Das77b5e902016-01-27 17:01:59 -08001048 (EthTypeCriterion) fwd.selector().getCriterion(Criterion.Type.ETH_TYPE);
Saurav Das822c4e22015-10-23 10:51:11 -07001049 if (ethType == null) {
Saurav Dasd2fded02016-12-02 15:43:47 -08001050 log.error("Versatile forwarding objective:{} must include ethType",
1051 fwd.id());
Saurav Das822c4e22015-10-23 10:51:11 -07001052 fail(fwd, ObjectiveError.BADPARAMS);
1053 return Collections.emptySet();
1054 }
1055 if (fwd.nextId() == null && fwd.treatment() == null) {
1056 log.error("Forwarding objective {} from {} must contain "
1057 + "nextId or Treatment", fwd.selector(), fwd.appId());
Zsolt Haraszti9faab752016-02-17 15:55:20 -08001058 fail(fwd, ObjectiveError.BADPARAMS);
Saurav Das822c4e22015-10-23 10:51:11 -07001059 return Collections.emptySet();
1060 }
Saurav Das49cb5a12016-01-16 22:54:07 -08001061
Saurav Das77b5e902016-01-27 17:01:59 -08001062 TrafficSelector.Builder sbuilder = DefaultTrafficSelector.builder();
1063 fwd.selector().criteria().forEach(criterion -> {
1064 if (criterion instanceof VlanIdCriterion) {
1065 VlanId vlanId = ((VlanIdCriterion) criterion).vlanId();
1066 // ensure that match does not include vlan = NONE as OF-DPA does not
1067 // match untagged packets this way in the ACL table.
1068 if (vlanId.equals(VlanId.NONE)) {
1069 return;
1070 }
Charles Chand1172632017-03-15 17:33:09 -07001071 if (requireVlanExtensions()) {
1072 OfdpaMatchVlanVid ofdpaMatchVlanVid = new OfdpaMatchVlanVid(vlanId);
1073 sbuilder.extension(ofdpaMatchVlanVid, deviceId);
1074 } else {
1075 sbuilder.matchVlanId(vlanId);
1076 }
Charles Chan393339f2018-01-11 11:48:18 -08001077 } else if (criterion instanceof Icmpv6TypeCriterion) {
1078 byte icmpv6Type = (byte) ((Icmpv6TypeCriterion) criterion).icmpv6Type();
1079 sbuilder.matchIcmpv6Type(icmpv6Type);
1080 } else if (criterion instanceof Icmpv6CodeCriterion) {
1081 byte icmpv6Code = (byte) ((Icmpv6CodeCriterion) criterion).icmpv6Code();
1082 sbuilder.matchIcmpv6Type(icmpv6Code);
Charles Chand9e47c62017-10-05 15:17:15 -07001083 } else if (criterion instanceof TcpPortCriterion || criterion instanceof UdpPortCriterion) {
1084 // FIXME: QMX switches do not support L4 dst port matching in ACL table.
1085 // Currently L4 dst port matching is only used by DHCP relay feature
1086 // and therefore is safe to be replaced with L4 src port matching.
1087 // We need to revisit this if L4 dst port is used for other purpose in the future.
1088 if (!supportIpv6L4Dst() && isIpv6(fwd.selector())) {
Charles Chand9e47c62017-10-05 15:17:15 -07001089 switch (criterion.type()) {
1090 case UDP_DST:
Charles Chand9e47c62017-10-05 15:17:15 -07001091 case UDP_DST_MASKED:
Charles Chand9e47c62017-10-05 15:17:15 -07001092 case TCP_DST:
Charles Chand9e47c62017-10-05 15:17:15 -07001093 case TCP_DST_MASKED:
Charles Chand9e47c62017-10-05 15:17:15 -07001094 break;
1095 default:
1096 sbuilder.add(criterion);
1097 }
1098 } else {
1099 sbuilder.add(criterion);
1100 }
Saurav Das77b5e902016-01-27 17:01:59 -08001101 } else {
1102 sbuilder.add(criterion);
1103 }
1104 });
1105
Saurav Das822c4e22015-10-23 10:51:11 -07001106 // XXX driver does not currently do type checking as per Tables 65-67 in
1107 // OFDPA 2.0 spec. The only allowed treatment is a punt to the controller.
Saurav Das49cb5a12016-01-16 22:54:07 -08001108 TrafficTreatment.Builder ttBuilder = DefaultTrafficTreatment.builder();
1109 if (fwd.treatment() != null) {
1110 for (Instruction ins : fwd.treatment().allInstructions()) {
1111 if (ins instanceof OutputInstruction) {
1112 OutputInstruction o = (OutputInstruction) ins;
Charles Chan93090352018-03-02 13:26:22 -08001113 if (PortNumber.CONTROLLER.equals(o.port())) {
Saurav Das49cb5a12016-01-16 22:54:07 -08001114 ttBuilder.add(o);
1115 } else {
1116 log.warn("Only allowed treatments in versatile forwarding "
1117 + "objectives are punts to the controller");
1118 }
1119 } else {
1120 log.warn("Cannot process instruction in versatile fwd {}", ins);
1121 }
Saurav Das822c4e22015-10-23 10:51:11 -07001122 }
Charles Chan2df0e8a2017-01-09 11:45:08 -08001123 if (fwd.treatment().clearedDeferred()) {
1124 ttBuilder.wipeDeferred();
1125 }
Saurav Das822c4e22015-10-23 10:51:11 -07001126 }
Saurav Das822c4e22015-10-23 10:51:11 -07001127 if (fwd.nextId() != null) {
Saurav Das49cb5a12016-01-16 22:54:07 -08001128 // overide case
1129 NextGroup next = getGroupForNextObjective(fwd.nextId());
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +05301130 if (next == null) {
1131 fail(fwd, ObjectiveError.BADPARAMS);
1132 return Collections.emptySet();
1133 }
Saurav Das49cb5a12016-01-16 22:54:07 -08001134 List<Deque<GroupKey>> gkeys = appKryo.deserialize(next.data());
1135 // we only need the top level group's key to point the flow to it
1136 Group group = groupService.getGroup(deviceId, gkeys.get(0).peekFirst());
1137 if (group == null) {
1138 log.warn("Group with key:{} for next-id:{} not found in dev:{}",
1139 gkeys.get(0).peekFirst(), fwd.nextId(), deviceId);
1140 fail(fwd, ObjectiveError.GROUPMISSING);
1141 return Collections.emptySet();
1142 }
1143 ttBuilder.deferred().group(group.id());
Saurav Das822c4e22015-10-23 10:51:11 -07001144 }
Saurav Das49cb5a12016-01-16 22:54:07 -08001145
1146 FlowRule.Builder ruleBuilder = DefaultFlowRule.builder()
1147 .fromApp(fwd.appId())
1148 .withPriority(fwd.priority())
1149 .forDevice(deviceId)
Saurav Das77b5e902016-01-27 17:01:59 -08001150 .withSelector(sbuilder.build())
Saurav Das49cb5a12016-01-16 22:54:07 -08001151 .withTreatment(ttBuilder.build())
1152 .makePermanent()
1153 .forTable(ACL_TABLE);
1154 return Collections.singletonList(ruleBuilder.build());
Saurav Das822c4e22015-10-23 10:51:11 -07001155 }
1156
1157 /**
1158 * In the OF-DPA 2.0 pipeline, specific forwarding refers to the IP table
Saurav Das8a0732e2015-11-20 15:27:53 -08001159 * (unicast or multicast) or the L2 table (mac + vlan) or the MPLS table.
Saurav Das822c4e22015-10-23 10:51:11 -07001160 *
1161 * @param fwd the forwarding objective of type 'specific'
1162 * @return a collection of flow rules. Typically there will be only one
1163 * for this type of forwarding objective. An empty set may be
1164 * returned if there is an issue in processing the objective.
1165 */
Charles Chan93090352018-03-02 13:26:22 -08001166 private Collection<FlowRule> processSpecific(ForwardingObjective fwd) {
Saurav Das25190812016-05-27 13:54:07 -07001167 log.debug("Processing specific fwd objective:{} in dev:{} with next:{}",
Saurav Das4ce45962015-11-24 23:21:05 -08001168 fwd.id(), deviceId, fwd.nextId());
1169 boolean isEthTypeObj = isSupportedEthTypeObjective(fwd);
1170 boolean isEthDstObj = isSupportedEthDstObjective(fwd);
1171
1172 if (isEthTypeObj) {
1173 return processEthTypeSpecific(fwd);
1174 } else if (isEthDstObj) {
1175 return processEthDstSpecific(fwd);
1176 } else {
1177 log.warn("processSpecific: Unsupported forwarding objective "
1178 + "criteria fwd:{} in dev:{}", fwd.nextId(), deviceId);
Saurav Das822c4e22015-10-23 10:51:11 -07001179 fail(fwd, ObjectiveError.UNSUPPORTED);
1180 return Collections.emptySet();
1181 }
Saurav Das4ce45962015-11-24 23:21:05 -08001182 }
1183
Saurav Das4ce45962015-11-24 23:21:05 -08001184 /**
1185 * Handles forwarding rules to the IP and MPLS tables.
1186 *
1187 * @param fwd the forwarding objective
1188 * @return A collection of flow rules, or an empty set
1189 */
1190 protected Collection<FlowRule> processEthTypeSpecific(ForwardingObjective fwd) {
Charles Chancad338a2016-09-16 18:03:11 -07001191 return processEthTypeSpecificInternal(fwd, false, ACL_TABLE);
Charles Chanf9e98652016-09-07 16:54:23 -07001192 }
1193
1194 /**
1195 * Internal implementation of processEthTypeSpecific.
1196 * <p>
1197 * Wildcarded IPv4_DST is not supported in OFDPA i12. Therefore, we break
1198 * the rule into 0.0.0.0/1 and 128.0.0.0/1.
1199 * The allowDefaultRoute must be set to false for OFDPA i12.
1200 * </p>
1201 *
1202 * @param fwd the forwarding objective
1203 * @param allowDefaultRoute allow wildcarded IPv4_DST or not
Ray Milkey0bb1e102016-11-10 14:51:27 -08001204 * @param mplsNextTable next MPLS table
Charles Chanf9e98652016-09-07 16:54:23 -07001205 * @return A collection of flow rules, or an empty set
1206 */
1207 protected Collection<FlowRule> processEthTypeSpecificInternal(ForwardingObjective fwd,
Charles Chancad338a2016-09-16 18:03:11 -07001208 boolean allowDefaultRoute,
1209 int mplsNextTable) {
Saurav Das4ce45962015-11-24 23:21:05 -08001210 TrafficSelector selector = fwd.selector();
1211 EthTypeCriterion ethType =
1212 (EthTypeCriterion) selector.getCriterion(Criterion.Type.ETH_TYPE);
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001213 boolean emptyGroup = false;
Charles Chan188ebf52015-12-23 00:15:11 -08001214 int forTableId;
Saurav Das8a0732e2015-11-20 15:27:53 -08001215 TrafficSelector.Builder filteredSelector = DefaultTrafficSelector.builder();
Charles Chan14967c22015-12-07 11:11:50 -08001216 TrafficTreatment.Builder tb = DefaultTrafficTreatment.builder();
Flavio Castroe10fa242016-01-15 12:43:51 -08001217 TrafficSelector.Builder complementarySelector = DefaultTrafficSelector.builder();
Charles Chan14967c22015-12-07 11:11:50 -08001218
Saurav Das8a0732e2015-11-20 15:27:53 -08001219 if (ethType.ethType().toShort() == Ethernet.TYPE_IPV4) {
Pier Luigi3bfe32c2017-01-30 09:47:36 -08001220 if (buildIpv4Selector(filteredSelector, complementarySelector, fwd, allowDefaultRoute) < 0) {
1221 return Collections.emptyList();
1222 }
1223 // We need to set properly the next table
Flavio Castroe10fa242016-01-15 12:43:51 -08001224 IpPrefix ipv4Dst = ((IPCriterion) selector.getCriterion(Criterion.Type.IPV4_DST)).ip();
Charles Chan5b9df8d2016-03-28 22:21:40 -07001225 if (ipv4Dst.isMulticast()) {
Charles Chan5b9df8d2016-03-28 22:21:40 -07001226 forTableId = MULTICAST_ROUTING_TABLE;
Flavio Castroe10fa242016-01-15 12:43:51 -08001227 } else {
Charles Chan5b9df8d2016-03-28 22:21:40 -07001228 forTableId = UNICAST_ROUTING_TABLE;
Flavio Castroe10fa242016-01-15 12:43:51 -08001229 }
Charles Chan93090352018-03-02 13:26:22 -08001230
Charles Chan7feb6c82018-03-05 11:28:23 -08001231 // TODO decrementing IP ttl is done automatically for routing, this
Charles Chan93090352018-03-02 13:26:22 -08001232 // action is ignored or rejected in ofdpa as it is not fully implemented
Charles Chan7feb6c82018-03-05 11:28:23 -08001233 // if (fwd.treatment() != null) {
1234 // for (Instruction instr : fwd.treatment().allInstructions()) {
1235 // if (instr instanceof L3ModificationInstruction &&
1236 // ((L3ModificationInstruction) instr).subtype() == L3SubType.DEC_TTL) {
1237 // tb.deferred().add(instr);
1238 // }
1239 // }
1240 // }
1241
Pier Ventree0ae7a32016-11-23 09:57:42 -08001242 } else if (ethType.ethType().toShort() == Ethernet.TYPE_IPV6) {
1243 if (buildIpv6Selector(filteredSelector, fwd) < 0) {
1244 return Collections.emptyList();
1245 }
Kalhee Kim3d26bf02017-10-04 14:59:11 +00001246 //We need to set the proper next table
1247 IpPrefix ipv6Dst = ((IPCriterion) selector.getCriterion(Criterion.Type.IPV6_DST)).ip();
1248 if (ipv6Dst.isMulticast()) {
1249 forTableId = MULTICAST_ROUTING_TABLE;
1250 } else {
1251 forTableId = UNICAST_ROUTING_TABLE;
1252 }
1253
Charles Chan7feb6c82018-03-05 11:28:23 -08001254 // TODO decrementing IP ttl is done automatically for routing, this
Charles Chan93090352018-03-02 13:26:22 -08001255 // action is ignored or rejected in ofdpa as it is not fully implemented
Charles Chan7feb6c82018-03-05 11:28:23 -08001256 // if (fwd.treatment() != null) {
1257 // for (Instruction instr : fwd.treatment().allInstructions()) {
1258 // if (instr instanceof L3ModificationInstruction &&
1259 // ((L3ModificationInstruction) instr).subtype() == L3SubType.DEC_TTL) {
1260 // tb.deferred().add(instr);
1261 // }
1262 // }
1263 // }
Saurav Das8a0732e2015-11-20 15:27:53 -08001264 } else {
1265 filteredSelector
1266 .matchEthType(Ethernet.MPLS_UNICAST)
1267 .matchMplsLabel(((MplsCriterion)
1268 selector.getCriterion(Criterion.Type.MPLS_LABEL)).label());
1269 MplsBosCriterion bos = (MplsBosCriterion) selector
Pier Ventre140a8942016-11-02 07:26:38 -07001270 .getCriterion(MPLS_BOS);
Alex Yashchuka3679532017-12-08 17:40:05 +02001271 if (bos != null && requireMplsBosMatch()) {
Saurav Das8a0732e2015-11-20 15:27:53 -08001272 filteredSelector.matchMplsBos(bos.mplsBos());
1273 }
1274 forTableId = MPLS_TABLE_1;
Saurav Das4ce45962015-11-24 23:21:05 -08001275 log.debug("processing MPLS specific forwarding objective {} -> next:{}"
1276 + " in dev {}", fwd.id(), fwd.nextId(), deviceId);
Saurav Das822c4e22015-10-23 10:51:11 -07001277
Charles Chan14967c22015-12-07 11:11:50 -08001278 if (fwd.treatment() != null) {
1279 for (Instruction instr : fwd.treatment().allInstructions()) {
1280 if (instr instanceof L2ModificationInstruction &&
1281 ((L2ModificationInstruction) instr).subtype() == L2SubType.MPLS_POP) {
Pier Luigi3bfe32c2017-01-30 09:47:36 -08001282 // OF-DPA does not pop in MPLS table in some cases. For the L3 VPN, it requires
Saurav Das9c705342017-01-06 11:36:01 -08001283 // setting the MPLS_TYPE so pop can happen down the pipeline
Alex Yashchuka3679532017-12-08 17:40:05 +02001284 if (requireMplsPop()) {
1285 if (mplsNextTable == MPLS_TYPE_TABLE && isNotMplsBos(selector)) {
1286 tb.immediate().popMpls();
1287 }
1288 } else {
1289 // Skip mpls pop action for mpls_unicast label
1290 if (instr instanceof ModMplsHeaderInstruction &&
1291 !((ModMplsHeaderInstruction) instr).ethernetType()
1292 .equals(EtherType.MPLS_UNICAST.ethType())) {
1293 tb.immediate().add(instr);
1294 }
Pier Luigi3bfe32c2017-01-30 09:47:36 -08001295 }
Charles Chan14967c22015-12-07 11:11:50 -08001296 }
Alex Yashchuka3679532017-12-08 17:40:05 +02001297
1298 if (requireMplsTtlModification()) {
1299 if (instr instanceof L3ModificationInstruction &&
1300 ((L3ModificationInstruction) instr).subtype() == L3SubType.DEC_TTL) {
1301 // FIXME Should modify the app to send the correct DEC_MPLS_TTL instruction
1302 tb.immediate().decMplsTtl();
1303 }
1304 if (instr instanceof L3ModificationInstruction &&
1305 ((L3ModificationInstruction) instr).subtype() == L3SubType.TTL_IN) {
1306 tb.immediate().add(instr);
1307 }
Charles Chan14967c22015-12-07 11:11:50 -08001308 }
Saurav Das8a0732e2015-11-20 15:27:53 -08001309 }
1310 }
1311 }
Saurav Das822c4e22015-10-23 10:51:11 -07001312
1313 if (fwd.nextId() != null) {
Saurav Das423fe2b2015-12-04 10:52:59 -08001314 NextGroup next = getGroupForNextObjective(fwd.nextId());
1315 if (next != null) {
1316 List<Deque<GroupKey>> gkeys = appKryo.deserialize(next.data());
1317 // we only need the top level group's key to point the flow to it
1318 Group group = groupService.getGroup(deviceId, gkeys.get(0).peekFirst());
1319 if (group == null) {
1320 log.warn("Group with key:{} for next-id:{} not found in dev:{}",
1321 gkeys.get(0).peekFirst(), fwd.nextId(), deviceId);
1322 fail(fwd, ObjectiveError.GROUPMISSING);
1323 return Collections.emptySet();
1324 }
Saurav Dasa89b95a2018-02-14 14:14:54 -08001325 if (isNotMplsBos(selector) && group.type().equals(SELECT)) {
1326 log.warn("SR CONTINUE case cannot be handled as MPLS ECMP "
1327 + "is not implemented in OF-DPA yet. Aborting flow {} -> next:{} "
1328 + "in this device {}", fwd.id(), fwd.nextId(), deviceId);
1329 fail(fwd, ObjectiveError.FLOWINSTALLATIONFAILED);
1330 return Collections.emptySet();
1331 }
Saurav Das423fe2b2015-12-04 10:52:59 -08001332 tb.deferred().group(group.id());
Saurav Dasf3f75942018-01-25 09:49:01 -08001333 // retrying flows may be necessary due to bug CORD-554
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001334 if (gkeys.size() == 1 && gkeys.get(0).size() == 1) {
Saurav Dasf3f75942018-01-25 09:49:01 -08001335 if (shouldRetry()) {
1336 log.warn("Found empty group 0x{} in dev:{} .. will retry fwd:{}",
1337 Integer.toHexString(group.id().id()), deviceId,
1338 fwd.id());
1339 emptyGroup = true;
1340 }
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001341 }
Saurav Das25190812016-05-27 13:54:07 -07001342 } else {
1343 log.warn("Cannot find group for nextId:{} in dev:{}. Aborting fwd:{}",
1344 fwd.nextId(), deviceId, fwd.id());
1345 fail(fwd, ObjectiveError.FLOWINSTALLATIONFAILED);
1346 return Collections.emptySet();
Saurav Das822c4e22015-10-23 10:51:11 -07001347 }
Saurav Das822c4e22015-10-23 10:51:11 -07001348 }
Charles Chancad338a2016-09-16 18:03:11 -07001349
1350 if (forTableId == MPLS_TABLE_1) {
Pier Ventre140a8942016-11-02 07:26:38 -07001351 if (mplsNextTable == MPLS_L3_TYPE_TABLE) {
Charles Chancad338a2016-09-16 18:03:11 -07001352 Ofdpa3SetMplsType setMplsType = new Ofdpa3SetMplsType(Ofdpa3MplsType.L3_PHP);
Saurav Das9c705342017-01-06 11:36:01 -08001353 // set mpls type as apply_action
1354 tb.immediate().extension(setMplsType, deviceId);
Charles Chancad338a2016-09-16 18:03:11 -07001355 }
1356 tb.transition(mplsNextTable);
1357 } else {
1358 tb.transition(ACL_TABLE);
1359 }
1360
Andrea Campanella84ac4df2018-04-30 11:48:55 +02001361 if (fwd.treatment() != null && fwd.treatment().clearedDeferred()) {
1362 if (supportsUnicastBlackHole()) {
1363 tb.wipeDeferred();
1364 } else {
1365 log.warn("Clear Deferred is not supported Unicast Routing Table on device {}", deviceId);
1366 return Collections.emptySet();
1367 }
1368 }
1369
Saurav Das822c4e22015-10-23 10:51:11 -07001370 FlowRule.Builder ruleBuilder = DefaultFlowRule.builder()
1371 .fromApp(fwd.appId())
1372 .withPriority(fwd.priority())
1373 .forDevice(deviceId)
Saurav Das8a0732e2015-11-20 15:27:53 -08001374 .withSelector(filteredSelector.build())
1375 .withTreatment(tb.build())
1376 .forTable(forTableId);
Saurav Das822c4e22015-10-23 10:51:11 -07001377
1378 if (fwd.permanent()) {
1379 ruleBuilder.makePermanent();
1380 } else {
1381 ruleBuilder.makeTemporary(fwd.timeout());
1382 }
Flavio Castroe10fa242016-01-15 12:43:51 -08001383 Collection<FlowRule> flowRuleCollection = new ArrayList<>();
1384 flowRuleCollection.add(ruleBuilder.build());
Pier Luigi3bfe32c2017-01-30 09:47:36 -08001385 if (!allowDefaultRoute) {
Pier Ventree0ae7a32016-11-23 09:57:42 -08001386 flowRuleCollection.add(
1387 defaultRoute(fwd, complementarySelector, forTableId, tb)
1388 );
Flavio Castroe10fa242016-01-15 12:43:51 -08001389 log.debug("Default rule 0.0.0.0/0 is being installed two rules");
1390 }
Saurav Dasf3f75942018-01-25 09:49:01 -08001391
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001392 if (emptyGroup) {
1393 executorService.schedule(new RetryFlows(fwd, flowRuleCollection),
1394 RETRY_MS, TimeUnit.MILLISECONDS);
1395 }
Flavio Castroe10fa242016-01-15 12:43:51 -08001396 return flowRuleCollection;
Saurav Das822c4e22015-10-23 10:51:11 -07001397 }
1398
Charles Chan93090352018-03-02 13:26:22 -08001399 private int buildIpv4Selector(TrafficSelector.Builder builderToUpdate,
Pier Luigi3bfe32c2017-01-30 09:47:36 -08001400 TrafficSelector.Builder extBuilder,
1401 ForwardingObjective fwd,
1402 boolean allowDefaultRoute) {
1403 TrafficSelector selector = fwd.selector();
1404
1405 IpPrefix ipv4Dst = ((IPCriterion) selector.getCriterion(Criterion.Type.IPV4_DST)).ip();
1406 if (ipv4Dst.isMulticast()) {
1407 if (ipv4Dst.prefixLength() != 32) {
1408 log.warn("Multicast specific forwarding objective can only be /32");
1409 fail(fwd, ObjectiveError.BADPARAMS);
1410 return -1;
1411 }
1412 VlanId assignedVlan = readVlanFromSelector(fwd.meta());
1413 if (assignedVlan == null) {
1414 log.warn("VLAN ID required by multicast specific fwd obj is missing. Abort.");
1415 fail(fwd, ObjectiveError.BADPARAMS);
1416 return -1;
1417 }
Charles Chand1172632017-03-15 17:33:09 -07001418 if (requireVlanExtensions()) {
1419 OfdpaMatchVlanVid ofdpaMatchVlanVid = new OfdpaMatchVlanVid(assignedVlan);
1420 builderToUpdate.extension(ofdpaMatchVlanVid, deviceId);
1421 } else {
1422 builderToUpdate.matchVlanId(assignedVlan);
1423 }
Pier Luigi3bfe32c2017-01-30 09:47:36 -08001424 builderToUpdate.matchEthType(Ethernet.TYPE_IPV4).matchIPDst(ipv4Dst);
1425 log.debug("processing IPv4 multicast specific forwarding objective {} -> next:{}"
1426 + " in dev:{}", fwd.id(), fwd.nextId(), deviceId);
1427 } else {
1428 if (ipv4Dst.prefixLength() == 0) {
1429 if (allowDefaultRoute) {
1430 // The entire IPV4_DST field is wildcarded intentionally
1431 builderToUpdate.matchEthType(Ethernet.TYPE_IPV4);
1432 } else {
1433 // NOTE: The switch does not support matching 0.0.0.0/0
1434 // Split it into 0.0.0.0/1 and 128.0.0.0/1
1435 builderToUpdate.matchEthType(Ethernet.TYPE_IPV4)
1436 .matchIPDst(IpPrefix.valueOf("0.0.0.0/1"));
1437 extBuilder.matchEthType(Ethernet.TYPE_IPV4)
1438 .matchIPDst(IpPrefix.valueOf("128.0.0.0/1"));
1439 }
1440 } else {
1441 builderToUpdate.matchEthType(Ethernet.TYPE_IPV4).matchIPDst(ipv4Dst);
1442 }
1443 log.debug("processing IPv4 unicast specific forwarding objective {} -> next:{}"
1444 + " in dev:{}", fwd.id(), fwd.nextId(), deviceId);
1445 }
1446 return 0;
1447 }
1448
1449 /**
1450 * Helper method to build Ipv6 selector using the selector provided by
1451 * a forwarding objective.
1452 *
1453 * @param builderToUpdate the builder to update
1454 * @param fwd the selector to read
1455 * @return 0 if the update ends correctly. -1 if the matches
1456 * are not yet supported
1457 */
Charles Chan93090352018-03-02 13:26:22 -08001458 int buildIpv6Selector(TrafficSelector.Builder builderToUpdate,
Pier Luigi3bfe32c2017-01-30 09:47:36 -08001459 ForwardingObjective fwd) {
1460
1461 TrafficSelector selector = fwd.selector();
1462
1463 IpPrefix ipv6Dst = ((IPCriterion) selector.getCriterion(Criterion.Type.IPV6_DST)).ip();
1464 if (ipv6Dst.isMulticast()) {
Julia Ferguson65428c32017-08-10 18:15:24 +00001465 if (ipv6Dst.prefixLength() != IpAddress.INET6_BIT_LENGTH) {
1466 log.warn("Multicast specific forwarding objective can only be /128");
1467 fail(fwd, ObjectiveError.BADPARAMS);
1468 return -1;
1469 }
1470 VlanId assignedVlan = readVlanFromSelector(fwd.meta());
1471 if (assignedVlan == null) {
1472 log.warn("VLAN ID required by multicast specific fwd obj is missing. Abort.");
1473 fail(fwd, ObjectiveError.BADPARAMS);
1474 return -1;
1475 }
1476 if (requireVlanExtensions()) {
1477 OfdpaMatchVlanVid ofdpaMatchVlanVid = new OfdpaMatchVlanVid(assignedVlan);
1478 builderToUpdate.extension(ofdpaMatchVlanVid, deviceId);
1479 } else {
1480 builderToUpdate.matchVlanId(assignedVlan);
1481 }
1482 builderToUpdate.matchEthType(Ethernet.TYPE_IPV6).matchIPv6Dst(ipv6Dst);
1483 log.debug("processing IPv6 multicast specific forwarding objective {} -> next:{}"
1484 + " in dev:{}", fwd.id(), fwd.nextId(), deviceId);
1485 } else {
1486 if (ipv6Dst.prefixLength() != 0) {
1487 builderToUpdate.matchIPv6Dst(ipv6Dst);
1488 }
Pier Luigi3bfe32c2017-01-30 09:47:36 -08001489 builderToUpdate.matchEthType(Ethernet.TYPE_IPV6);
1490 log.debug("processing IPv6 unicast specific forwarding objective {} -> next:{}"
1491 + " in dev:{}", fwd.id(), fwd.nextId(), deviceId);
Julia Ferguson65428c32017-08-10 18:15:24 +00001492 }
Pier Luigi3bfe32c2017-01-30 09:47:36 -08001493 return 0;
1494 }
1495
Charles Chan93090352018-03-02 13:26:22 -08001496 FlowRule defaultRoute(ForwardingObjective fwd,
Pier Ventree0ae7a32016-11-23 09:57:42 -08001497 TrafficSelector.Builder complementarySelector,
1498 int forTableId,
1499 TrafficTreatment.Builder tb) {
1500 FlowRule.Builder rule = DefaultFlowRule.builder()
1501 .fromApp(fwd.appId())
1502 .withPriority(fwd.priority())
1503 .forDevice(deviceId)
1504 .withSelector(complementarySelector.build())
1505 .withTreatment(tb.build())
1506 .forTable(forTableId);
1507 if (fwd.permanent()) {
1508 rule.makePermanent();
1509 } else {
1510 rule.makeTemporary(fwd.timeout());
1511 }
1512 return rule.build();
1513 }
1514
Saurav Das4ce45962015-11-24 23:21:05 -08001515 /**
1516 * Handles forwarding rules to the L2 bridging table. Flow actions are not
1517 * allowed in the bridging table - instead we use L2 Interface group or
1518 * L2 flood group
1519 *
1520 * @param fwd the forwarding objective
1521 * @return A collection of flow rules, or an empty set
1522 */
1523 protected Collection<FlowRule> processEthDstSpecific(ForwardingObjective fwd) {
1524 List<FlowRule> rules = new ArrayList<>();
1525
1526 // Build filtered selector
1527 TrafficSelector selector = fwd.selector();
1528 EthCriterion ethCriterion = (EthCriterion) selector
1529 .getCriterion(Criterion.Type.ETH_DST);
1530 VlanIdCriterion vlanIdCriterion = (VlanIdCriterion) selector
1531 .getCriterion(Criterion.Type.VLAN_VID);
1532
1533 if (vlanIdCriterion == null) {
1534 log.warn("Forwarding objective for bridging requires vlan. Not "
1535 + "installing fwd:{} in dev:{}", fwd.id(), deviceId);
1536 fail(fwd, ObjectiveError.BADPARAMS);
1537 return Collections.emptySet();
1538 }
1539
1540 TrafficSelector.Builder filteredSelectorBuilder =
1541 DefaultTrafficSelector.builder();
Yi Tseng3a77b4f2017-02-06 15:02:17 -08001542
1543 if (!ethCriterion.mac().equals(NONE) &&
1544 !ethCriterion.mac().equals(BROADCAST)) {
Saurav Das4ce45962015-11-24 23:21:05 -08001545 filteredSelectorBuilder.matchEthDst(ethCriterion.mac());
1546 log.debug("processing L2 forwarding objective:{} -> next:{} in dev:{}",
1547 fwd.id(), fwd.nextId(), deviceId);
1548 } else {
Yi Tseng3a77b4f2017-02-06 15:02:17 -08001549 // Use wildcard DST_MAC if the MacAddress is None or Broadcast
Saurav Das4ce45962015-11-24 23:21:05 -08001550 log.debug("processing L2 Broadcast forwarding objective:{} -> next:{} "
1551 + "in dev:{} for vlan:{}",
1552 fwd.id(), fwd.nextId(), deviceId, vlanIdCriterion.vlanId());
1553 }
Charles Chand1172632017-03-15 17:33:09 -07001554 if (requireVlanExtensions()) {
1555 OfdpaMatchVlanVid ofdpaMatchVlanVid = new OfdpaMatchVlanVid(vlanIdCriterion.vlanId());
1556 filteredSelectorBuilder.extension(ofdpaMatchVlanVid, deviceId);
1557 } else {
1558 filteredSelectorBuilder.matchVlanId(vlanIdCriterion.vlanId());
1559 }
Saurav Das4ce45962015-11-24 23:21:05 -08001560 TrafficSelector filteredSelector = filteredSelectorBuilder.build();
1561
1562 if (fwd.treatment() != null) {
1563 log.warn("Ignoring traffic treatment in fwd rule {} meant for L2 table"
1564 + "for dev:{}. Expecting only nextId", fwd.id(), deviceId);
1565 }
1566
1567 TrafficTreatment.Builder treatmentBuilder = DefaultTrafficTreatment.builder();
1568 if (fwd.nextId() != null) {
Saurav Das423fe2b2015-12-04 10:52:59 -08001569 NextGroup next = getGroupForNextObjective(fwd.nextId());
Saurav Das4ce45962015-11-24 23:21:05 -08001570 if (next != null) {
1571 List<Deque<GroupKey>> gkeys = appKryo.deserialize(next.data());
1572 // we only need the top level group's key to point the flow to it
1573 Group group = groupService.getGroup(deviceId, gkeys.get(0).peekFirst());
1574 if (group != null) {
1575 treatmentBuilder.deferred().group(group.id());
1576 } else {
1577 log.warn("Group with key:{} for next-id:{} not found in dev:{}",
1578 gkeys.get(0).peekFirst(), fwd.nextId(), deviceId);
1579 fail(fwd, ObjectiveError.GROUPMISSING);
1580 return Collections.emptySet();
1581 }
1582 }
1583 }
1584 treatmentBuilder.immediate().transition(ACL_TABLE);
1585 TrafficTreatment filteredTreatment = treatmentBuilder.build();
1586
1587 // Build bridging table entries
1588 FlowRule.Builder flowRuleBuilder = DefaultFlowRule.builder();
1589 flowRuleBuilder.fromApp(fwd.appId())
1590 .withPriority(fwd.priority())
1591 .forDevice(deviceId)
1592 .withSelector(filteredSelector)
1593 .withTreatment(filteredTreatment)
1594 .forTable(BRIDGING_TABLE);
1595 if (fwd.permanent()) {
1596 flowRuleBuilder.makePermanent();
1597 } else {
1598 flowRuleBuilder.makeTemporary(fwd.timeout());
1599 }
1600 rules.add(flowRuleBuilder.build());
1601 return rules;
1602 }
1603
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001604 //////////////////////////////////////
1605 // Helper Methods and Classes
1606 //////////////////////////////////////
1607
1608 private boolean isSupportedEthTypeObjective(ForwardingObjective fwd) {
1609 TrafficSelector selector = fwd.selector();
1610 EthTypeCriterion ethType = (EthTypeCriterion) selector
1611 .getCriterion(Criterion.Type.ETH_TYPE);
1612 return !((ethType == null) ||
1613 ((ethType.ethType().toShort() != Ethernet.TYPE_IPV4) &&
Pier Ventree0ae7a32016-11-23 09:57:42 -08001614 (ethType.ethType().toShort() != Ethernet.MPLS_UNICAST)) &&
1615 (ethType.ethType().toShort() != Ethernet.TYPE_IPV6));
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001616 }
1617
1618 private boolean isSupportedEthDstObjective(ForwardingObjective fwd) {
1619 TrafficSelector selector = fwd.selector();
1620 EthCriterion ethDst = (EthCriterion) selector
1621 .getCriterion(Criterion.Type.ETH_DST);
1622 VlanIdCriterion vlanId = (VlanIdCriterion) selector
1623 .getCriterion(Criterion.Type.VLAN_VID);
1624 return !(ethDst == null && vlanId == null);
1625 }
1626
Charles Chan93090352018-03-02 13:26:22 -08001627 NextGroup getGroupForNextObjective(Integer nextId) {
Saurav Das423fe2b2015-12-04 10:52:59 -08001628 NextGroup next = flowObjectiveStore.getNextGroup(nextId);
1629 if (next != null) {
1630 List<Deque<GroupKey>> gkeys = appKryo.deserialize(next.data());
1631 if (gkeys != null && !gkeys.isEmpty()) {
1632 return next;
1633 } else {
1634 log.warn("Empty next group found in FlowObjective store for "
1635 + "next-id:{} in dev:{}", nextId, deviceId);
1636 }
1637 } else {
1638 log.warn("next-id {} not found in Flow objective store for dev:{}",
1639 nextId, deviceId);
1640 }
1641 return null;
1642 }
1643
Charles Chan188ebf52015-12-23 00:15:11 -08001644 protected static void pass(Objective obj) {
Sho SHIMIZUef7e2902016-02-12 18:38:29 -08001645 obj.context().ifPresent(context -> context.onSuccess(obj));
Saurav Das822c4e22015-10-23 10:51:11 -07001646 }
1647
Charles Chan188ebf52015-12-23 00:15:11 -08001648 protected static void fail(Objective obj, ObjectiveError error) {
Sho SHIMIZUef7e2902016-02-12 18:38:29 -08001649 obj.context().ifPresent(context -> context.onError(obj, error));
Saurav Das822c4e22015-10-23 10:51:11 -07001650 }
Saurav Das24431192016-03-07 19:13:00 -08001651
Saurav Das24431192016-03-07 19:13:00 -08001652 @Override
1653 public List<String> getNextMappings(NextGroup nextGroup) {
1654 List<String> mappings = new ArrayList<>();
1655 List<Deque<GroupKey>> gkeys = appKryo.deserialize(nextGroup.data());
1656 for (Deque<GroupKey> gkd : gkeys) {
1657 Group lastGroup = null;
Yuta HIGUCHI2dce08a2017-04-20 21:57:48 -07001658 StringBuilder gchain = new StringBuilder();
Saurav Das24431192016-03-07 19:13:00 -08001659 for (GroupKey gk : gkd) {
1660 Group g = groupService.getGroup(deviceId, gk);
Saurav Das8be4e3a2016-03-11 17:19:07 -08001661 if (g == null) {
Saurav Das25190812016-05-27 13:54:07 -07001662 gchain.append(" NoGrp").append(" -->");
Saurav Das8be4e3a2016-03-11 17:19:07 -08001663 continue;
1664 }
1665 gchain.append(" 0x").append(Integer.toHexString(g.id().id()))
1666 .append(" -->");
Saurav Das24431192016-03-07 19:13:00 -08001667 lastGroup = g;
1668 }
1669 // add port information for last group in group-chain
Yuta HIGUCHI2dce08a2017-04-20 21:57:48 -07001670 List<Instruction> lastGroupIns = new ArrayList<>();
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001671 if (lastGroup != null && !lastGroup.buckets().buckets().isEmpty()) {
Saurav Das25190812016-05-27 13:54:07 -07001672 lastGroupIns = lastGroup.buckets().buckets().get(0)
1673 .treatment().allInstructions();
1674 }
1675 for (Instruction i: lastGroupIns) {
Saurav Das24431192016-03-07 19:13:00 -08001676 if (i instanceof OutputInstruction) {
Saurav Das8be4e3a2016-03-11 17:19:07 -08001677 gchain.append(" port:").append(((OutputInstruction) i).port());
Saurav Das24431192016-03-07 19:13:00 -08001678 }
1679 }
Saurav Das8be4e3a2016-03-11 17:19:07 -08001680 mappings.add(gchain.toString());
Saurav Das24431192016-03-07 19:13:00 -08001681 }
1682 return mappings;
1683 }
Charles Chan5b9df8d2016-03-28 22:21:40 -07001684
Saurav Dasa89b95a2018-02-14 14:14:54 -08001685 /**
1686 * Returns true iff the given selector matches on BOS==true, indicating that
1687 * the selector is trying to match on a label that is bottom-of-stack.
1688 *
1689 * @param selector the given match
1690 * @return true iff BoS==true; false if BOS==false, or BOS matching is not
1691 * expressed in the given selector
1692 */
Pier Ventre140a8942016-11-02 07:26:38 -07001693 static boolean isMplsBos(TrafficSelector selector) {
1694 MplsBosCriterion bosCriterion = (MplsBosCriterion) selector.getCriterion(MPLS_BOS);
1695 return bosCriterion != null && bosCriterion.mplsBos();
1696 }
1697
Saurav Dasa89b95a2018-02-14 14:14:54 -08001698 /**
1699 * Returns true iff the given selector matches on BOS==false, indicating
1700 * that the selector is trying to match on a label that is not the
1701 * bottom-of-stack label.
1702 *
1703 * @param selector the given match
1704 * @return true iff BoS==false;
1705 * false if BOS==true, or BOS matching is not expressed in the given selector
1706 */
Pier Ventre140a8942016-11-02 07:26:38 -07001707 static boolean isNotMplsBos(TrafficSelector selector) {
1708 MplsBosCriterion bosCriterion = (MplsBosCriterion) selector.getCriterion(MPLS_BOS);
1709 return bosCriterion != null && !bosCriterion.mplsBos();
1710 }
1711
Saurav Dasa89b95a2018-02-14 14:14:54 -08001712 /**
1713 * Returns true iff the forwarding objective includes a treatment to pop the
1714 * MPLS label.
1715 *
1716 * @param fwd the given forwarding objective
1717 * @return true iff mpls pop treatment exists
1718 */
1719 static boolean isMplsPop(ForwardingObjective fwd) {
1720 if (fwd.treatment() != null) {
1721 for (Instruction instr : fwd.treatment().allInstructions()) {
1722 if (instr instanceof L2ModificationInstruction
1723 && ((L2ModificationInstruction) instr)
1724 .subtype() == L2SubType.MPLS_POP) {
1725 return true;
1726 }
1727 }
1728 }
1729 return false;
1730 }
1731
Charles Chand9e47c62017-10-05 15:17:15 -07001732 private static boolean isIpv6(TrafficSelector selector) {
1733 EthTypeCriterion ethTypeCriterion = (EthTypeCriterion) selector.getCriterion(ETH_TYPE);
1734 return ethTypeCriterion != null && ethTypeCriterion.ethType().toShort() == Ethernet.TYPE_IPV6;
1735 }
1736
Charles Chan93090352018-03-02 13:26:22 -08001737 static VlanId readVlanFromSelector(TrafficSelector selector) {
Saurav Das59232cf2016-04-27 18:35:50 -07001738 if (selector == null) {
1739 return null;
1740 }
Charles Chan5b9df8d2016-03-28 22:21:40 -07001741 Criterion criterion = selector.getCriterion(Criterion.Type.VLAN_VID);
1742 return (criterion == null)
1743 ? null : ((VlanIdCriterion) criterion).vlanId();
1744 }
1745
Charles Chand9e47c62017-10-05 15:17:15 -07001746 static IpPrefix readIpDstFromSelector(TrafficSelector selector) {
Saurav Das59232cf2016-04-27 18:35:50 -07001747 if (selector == null) {
1748 return null;
1749 }
Charles Chan5b9df8d2016-03-28 22:21:40 -07001750 Criterion criterion = selector.getCriterion(Criterion.Type.IPV4_DST);
1751 return (criterion == null) ? null : ((IPCriterion) criterion).ip();
1752 }
Charles Chand55e84d2016-03-30 17:54:24 -07001753
1754 private static VlanId readVlanFromTreatment(TrafficTreatment treatment) {
Saurav Das59232cf2016-04-27 18:35:50 -07001755 if (treatment == null) {
1756 return null;
1757 }
Charles Chand55e84d2016-03-30 17:54:24 -07001758 for (Instruction i : treatment.allInstructions()) {
1759 if (i instanceof ModVlanIdInstruction) {
1760 return ((ModVlanIdInstruction) i).vlanId();
1761 }
1762 }
1763 return null;
1764 }
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001765
Charles Chand57552d2018-03-02 15:41:41 -08001766 private static MacAddress readEthDstFromTreatment(TrafficTreatment treatment) {
1767 if (treatment == null) {
1768 return null;
1769 }
1770 for (Instruction i : treatment.allInstructions()) {
1771 if (i instanceof ModEtherInstruction) {
1772 ModEtherInstruction modEtherInstruction = (ModEtherInstruction) i;
1773 if (modEtherInstruction.subtype() == L2SubType.ETH_DST) {
1774 return modEtherInstruction.mac();
1775 }
1776 }
1777 }
1778 return null;
1779 }
1780
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001781 /**
1782 * Utility class that retries sending flows a fixed number of times, even if
1783 * some of the attempts are successful. Used only for forwarding objectives.
1784 */
Alex Yashchuka3679532017-12-08 17:40:05 +02001785 public final class RetryFlows implements Runnable {
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001786 int attempts = MAX_RETRY_ATTEMPTS;
1787 private Collection<FlowRule> retryFlows;
1788 private ForwardingObjective fwd;
1789
Charles Chan93090352018-03-02 13:26:22 -08001790 RetryFlows(ForwardingObjective fwd, Collection<FlowRule> retryFlows) {
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001791 this.fwd = fwd;
1792 this.retryFlows = retryFlows;
1793 }
1794
1795 @Override
1796 public void run() {
1797 log.info("RETRY FLOWS ATTEMPT# {} for fwd:{} rules:{}",
1798 MAX_RETRY_ATTEMPTS - attempts, fwd.id(), retryFlows.size());
1799 sendForward(fwd, retryFlows);
1800 if (--attempts > 0) {
1801 executorService.schedule(this, RETRY_MS, TimeUnit.MILLISECONDS);
1802 }
1803 }
1804 }
1805
Saurav Das822c4e22015-10-23 10:51:11 -07001806}