blob: fdefb88201f6d16d5f73d01880ef33afbf324f5a [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 Yashchuk4caa8e82017-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;
Saurav Das822c4e22015-10-23 10:51:11 -070025import org.onlab.packet.VlanId;
26import org.onlab.util.KryoNamespace;
27import org.onosproject.core.ApplicationId;
28import org.onosproject.core.CoreService;
Charles Chancad338a2016-09-16 18:03:11 -070029import org.onosproject.driver.extensions.Ofdpa3MplsType;
30import org.onosproject.driver.extensions.Ofdpa3SetMplsType;
Charles Chan14967c22015-12-07 11:11:50 -080031import org.onosproject.driver.extensions.OfdpaMatchVlanVid;
32import org.onosproject.driver.extensions.OfdpaSetVlanVid;
Saurav Das822c4e22015-10-23 10:51:11 -070033import org.onosproject.net.DeviceId;
34import org.onosproject.net.Port;
35import org.onosproject.net.PortNumber;
36import org.onosproject.net.behaviour.NextGroup;
37import org.onosproject.net.behaviour.Pipeliner;
38import org.onosproject.net.behaviour.PipelinerContext;
39import org.onosproject.net.device.DeviceService;
40import org.onosproject.net.driver.AbstractHandlerBehaviour;
41import org.onosproject.net.flow.DefaultFlowRule;
42import org.onosproject.net.flow.DefaultTrafficSelector;
43import org.onosproject.net.flow.DefaultTrafficTreatment;
44import org.onosproject.net.flow.FlowRule;
45import org.onosproject.net.flow.FlowRuleOperations;
46import org.onosproject.net.flow.FlowRuleOperationsContext;
47import org.onosproject.net.flow.FlowRuleService;
48import org.onosproject.net.flow.TrafficSelector;
49import org.onosproject.net.flow.TrafficTreatment;
50import org.onosproject.net.flow.criteria.Criteria;
51import org.onosproject.net.flow.criteria.Criterion;
52import org.onosproject.net.flow.criteria.EthCriterion;
53import org.onosproject.net.flow.criteria.EthTypeCriterion;
Charles Chan14967c22015-12-07 11:11:50 -080054import org.onosproject.net.flow.criteria.ExtensionCriterion;
Saurav Das822c4e22015-10-23 10:51:11 -070055import 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;
Alex Yashchuk4caa8e82017-12-08 17:40:05 +020069import org.onosproject.net.flow.instructions.L2ModificationInstruction.ModMplsHeaderInstruction;
Charles Chan14967c22015-12-07 11:11:50 -080070import org.onosproject.net.flow.instructions.L3ModificationInstruction;
71import org.onosproject.net.flow.instructions.L3ModificationInstruction.L3SubType;
Saurav Das822c4e22015-10-23 10:51:11 -070072import org.onosproject.net.flowobjective.FilteringObjective;
73import org.onosproject.net.flowobjective.FlowObjectiveStore;
74import org.onosproject.net.flowobjective.ForwardingObjective;
75import org.onosproject.net.flowobjective.NextObjective;
76import org.onosproject.net.flowobjective.Objective;
77import org.onosproject.net.flowobjective.ObjectiveError;
Saurav Das822c4e22015-10-23 10:51:11 -070078import org.onosproject.net.group.DefaultGroupKey;
79import org.onosproject.net.group.Group;
Saurav Das822c4e22015-10-23 10:51:11 -070080import org.onosproject.net.group.GroupKey;
Saurav Das822c4e22015-10-23 10:51:11 -070081import org.onosproject.net.group.GroupService;
Saurav Das822c4e22015-10-23 10:51:11 -070082import org.onosproject.store.serializers.KryoNamespaces;
83import org.slf4j.Logger;
84
Pier Ventree0ae7a32016-11-23 09:57:42 -080085import java.util.ArrayDeque;
86import java.util.ArrayList;
87import java.util.Collection;
88import java.util.Collections;
89import java.util.Deque;
90import java.util.List;
91import java.util.Objects;
Pier Ventree0ae7a32016-11-23 09:57:42 -080092import java.util.concurrent.ScheduledExecutorService;
93import java.util.concurrent.TimeUnit;
94
95import static java.util.concurrent.Executors.newScheduledThreadPool;
Yi Tseng3a77b4f2017-02-06 15:02:17 -080096import static org.onlab.packet.MacAddress.BROADCAST;
Charles Chanb4879a52017-10-20 19:09:16 -070097import static org.onlab.packet.MacAddress.IPV4_MULTICAST;
98import static org.onlab.packet.MacAddress.IPV6_MULTICAST;
Yi Tseng3a77b4f2017-02-06 15:02:17 -080099import static org.onlab.packet.MacAddress.NONE;
Pier Ventree0ae7a32016-11-23 09:57:42 -0800100import static org.onlab.util.Tools.groupedThreads;
Yi Tsengef19de12017-04-24 11:33:05 -0700101import static org.onosproject.driver.pipeline.ofdpa.OfdpaGroupHandlerUtility.*;
Charles Chand9e47c62017-10-05 15:17:15 -0700102import static org.onosproject.net.flow.criteria.Criterion.Type.ETH_TYPE;
Pier Ventree0ae7a32016-11-23 09:57:42 -0800103import static org.slf4j.LoggerFactory.getLogger;
Pier Ventre140a8942016-11-02 07:26:38 -0700104import static org.onosproject.net.flow.criteria.Criterion.Type.MPLS_BOS;
105import static org.onosproject.net.flowobjective.NextObjective.Type.HASHED;
Pier Ventree0ae7a32016-11-23 09:57:42 -0800106
Saurav Das822c4e22015-10-23 10:51:11 -0700107/**
108 * Driver for Broadcom's OF-DPA v2.0 TTP.
Saurav Das822c4e22015-10-23 10:51:11 -0700109 */
Charles Chan361154b2016-03-24 10:23:39 -0700110public class Ofdpa2Pipeline extends AbstractHandlerBehaviour implements Pipeliner {
Pier Ventre42287df2016-11-09 14:17:26 -0800111
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;
134
135 protected static final int MPLS_TUNNEL_ID_BASE = 0x10000;
136 protected static final int MPLS_TUNNEL_ID_MAX = 0x1FFFF;
137
Pier Ventre70d53ba2016-11-17 22:26:29 -0800138 protected static final int MPLS_UNI_PORT_MAX = 0x0000FFFF;
139
140 protected static final int MPLS_NNI_PORT_BASE = 0x00020000;
141 protected static final int MPLS_NNI_PORT_MAX = 0x0002FFFF;
142
Saurav Das822c4e22015-10-23 10:51:11 -0700143 private final Logger log = getLogger(getClass());
Charles Chan425854b2016-04-11 15:32:12 -0700144 protected ServiceDirectory serviceDirectory;
Saurav Das822c4e22015-10-23 10:51:11 -0700145 protected FlowRuleService flowRuleService;
Charles Chan425854b2016-04-11 15:32:12 -0700146 protected CoreService coreService;
Saurav Das8a0732e2015-11-20 15:27:53 -0800147 protected GroupService groupService;
148 protected FlowObjectiveStore flowObjectiveStore;
Saurav Das822c4e22015-10-23 10:51:11 -0700149 protected DeviceId deviceId;
150 protected ApplicationId driverId;
Saurav Das822c4e22015-10-23 10:51:11 -0700151 protected DeviceService deviceService;
Charles Chan188ebf52015-12-23 00:15:11 -0800152 protected static KryoNamespace appKryo = new KryoNamespace.Builder()
Yi Tsengef19de12017-04-24 11:33:05 -0700153 .register(KryoNamespaces.API)
154 .register(GroupKey.class)
155 .register(DefaultGroupKey.class)
156 .register(OfdpaNextGroup.class)
157 .register(ArrayDeque.class)
158 .build("Ofdpa2Pipeline");
Saurav Das822c4e22015-10-23 10:51:11 -0700159
Charles Chan425854b2016-04-11 15:32:12 -0700160 protected Ofdpa2GroupHandler groupHandler;
Saurav Das822c4e22015-10-23 10:51:11 -0700161
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700162 // flows installations to be retried
163 protected ScheduledExecutorService executorService
164 = newScheduledThreadPool(5, groupedThreads("OfdpaPipeliner", "retry-%d", log));
165 protected static final int MAX_RETRY_ATTEMPTS = 10;
166 protected static final int RETRY_MS = 1000;
Saurav Das4f980082015-11-05 13:39:15 -0800167
Saurav Das822c4e22015-10-23 10:51:11 -0700168 @Override
169 public void init(DeviceId deviceId, PipelinerContext context) {
Saurav Das822c4e22015-10-23 10:51:11 -0700170 this.deviceId = deviceId;
171
Charles Chan425854b2016-04-11 15:32:12 -0700172 serviceDirectory = context.directory();
Saurav Das822c4e22015-10-23 10:51:11 -0700173 coreService = serviceDirectory.get(CoreService.class);
174 flowRuleService = serviceDirectory.get(FlowRuleService.class);
175 groupService = serviceDirectory.get(GroupService.class);
176 flowObjectiveStore = context.store();
Saurav Das822c4e22015-10-23 10:51:11 -0700177 deviceService = serviceDirectory.get(DeviceService.class);
Saurav Das822c4e22015-10-23 10:51:11 -0700178
Charles Chan40132b32017-01-22 00:19:37 -0800179 initDriverId();
180 initGroupHander(context);
Saurav Das822c4e22015-10-23 10:51:11 -0700181
Saurav Das822c4e22015-10-23 10:51:11 -0700182 initializePipeline();
Saurav Das822c4e22015-10-23 10:51:11 -0700183 }
184
Charles Chan40132b32017-01-22 00:19:37 -0800185 protected void initDriverId() {
186 driverId = coreService.registerApplication(
187 "org.onosproject.driver.Ofdpa2Pipeline");
188 }
189
190 protected void initGroupHander(PipelinerContext context) {
191 groupHandler = new Ofdpa2GroupHandler();
192 groupHandler.init(deviceId, context);
193 }
194
Saurav Das822c4e22015-10-23 10:51:11 -0700195 protected void initializePipeline() {
Charles Chan188ebf52015-12-23 00:15:11 -0800196 // OF-DPA does not require initializing the pipeline as it puts default
197 // rules automatically in the hardware. However emulation of OFDPA in
198 // software switches does require table-miss-entries.
Saurav Das822c4e22015-10-23 10:51:11 -0700199 }
200
Charles Chand1172632017-03-15 17:33:09 -0700201 /**
Alex Yashchuk4caa8e82017-12-08 17:40:05 +0200202 * Determines whether this pipeline requires MPLS POP instruction.
203 *
204 * @return true to use MPLS POP instruction
205 */
206 public boolean requireMplsPop() {
207 return true;
208 }
209
210 /**
211 * Determines whether this pipeline requires MPLS BOS match.
212 *
213 * @return true to use MPLS BOS match
214 */
215 public boolean requireMplsBosMatch() {
216 return true;
217 }
218
219 /**
220 * Determines whether this pipeline requires MPLS TTL decrement and copy.
221 *
222 * @return true to use MPLS TTL decrement and copy
223 */
224 public boolean requireMplsTtlModification() {
225 return true;
226 }
227
228 /**
Charles Chand1172632017-03-15 17:33:09 -0700229 * Determines whether this pipeline requires OFDPA match and set VLAN extensions.
230 *
231 * @return true to use the extensions
232 */
233 protected boolean requireVlanExtensions() {
234 return true;
235 }
236
Saurav Das86d13e82017-04-28 17:03:48 -0700237 /**
238 * Determines whether in-port should be matched on in TMAC table rules.
239 *
240 * @return true if match on in-port should be programmed
241 */
242 protected boolean matchInPortTmacTable() {
243 return true;
244 }
245
Charles Chand9e47c62017-10-05 15:17:15 -0700246 /**
247 * Determines whether matching L4 destination port on IPv6 packets is supported in ACL table.
248 *
249 * @return true if matching L4 destination port on IPv6 packets is supported in ACL table.
250 */
251 protected boolean supportIpv6L4Dst() {
252 return true;
253 }
254
Saurav Das822c4e22015-10-23 10:51:11 -0700255 //////////////////////////////////////
256 // Flow Objectives
257 //////////////////////////////////////
258
259 @Override
260 public void filter(FilteringObjective filteringObjective) {
261 if (filteringObjective.type() == FilteringObjective.Type.PERMIT) {
262 processFilter(filteringObjective,
263 filteringObjective.op() == Objective.Operation.ADD,
264 filteringObjective.appId());
265 } else {
266 // Note that packets that don't match the PERMIT filter are
267 // automatically denied. The DENY filter is used to deny packets
268 // that are otherwise permitted by the PERMIT filter.
269 // Use ACL table flow rules here for DENY filtering objectives
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530270 log.warn("filter objective other than PERMIT currently not supported");
Saurav Das822c4e22015-10-23 10:51:11 -0700271 fail(filteringObjective, ObjectiveError.UNSUPPORTED);
272 }
273 }
274
275 @Override
276 public void forward(ForwardingObjective fwd) {
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700277 Collection<FlowRule> rules = processForward(fwd);
Saurav Das25190812016-05-27 13:54:07 -0700278 if (rules == null || rules.isEmpty()) {
279 // Assumes fail message has already been generated to the objective
280 // context. Returning here prevents spurious pass message to be
281 // generated by FlowRule service for empty flowOps.
282 return;
283 }
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700284 sendForward(fwd, rules);
285 }
286
287 protected void sendForward(ForwardingObjective fwd, Collection<FlowRule> rules) {
288 FlowRuleOperations.Builder flowOpsBuilder = FlowRuleOperations.builder();
Saurav Das822c4e22015-10-23 10:51:11 -0700289 switch (fwd.op()) {
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700290 case ADD:
291 rules.stream()
292 .filter(Objects::nonNull)
293 .forEach(flowOpsBuilder::add);
Saurav Dasd2fded02016-12-02 15:43:47 -0800294 log.debug("Applying a add fwd-obj {} to sw:{}", fwd.id(), deviceId);
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700295 break;
296 case REMOVE:
297 rules.stream()
298 .filter(Objects::nonNull)
299 .forEach(flowOpsBuilder::remove);
Pier Ventre42287df2016-11-09 14:17:26 -0800300 log.debug("Deleting a flow rule to sw:{}", deviceId);
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700301 break;
302 default:
303 fail(fwd, ObjectiveError.UNKNOWN);
304 log.warn("Unknown forwarding type {}", fwd.op());
Saurav Das822c4e22015-10-23 10:51:11 -0700305 }
306
Saurav Das822c4e22015-10-23 10:51:11 -0700307 flowRuleService.apply(flowOpsBuilder.build(new FlowRuleOperationsContext() {
308 @Override
309 public void onSuccess(FlowRuleOperations ops) {
310 pass(fwd);
311 }
312
313 @Override
314 public void onError(FlowRuleOperations ops) {
315 fail(fwd, ObjectiveError.FLOWINSTALLATIONFAILED);
316 }
317 }));
Saurav Das822c4e22015-10-23 10:51:11 -0700318 }
319
320 @Override
321 public void next(NextObjective nextObjective) {
Saurav Das8a0732e2015-11-20 15:27:53 -0800322 NextGroup nextGroup = flowObjectiveStore.getNextGroup(nextObjective.id());
323 switch (nextObjective.op()) {
324 case ADD:
Saurav Das4f980082015-11-05 13:39:15 -0800325 if (nextGroup != null) {
Saurav Das8a0732e2015-11-20 15:27:53 -0800326 log.warn("Cannot add next {} that already exists in device {}",
327 nextObjective.id(), deviceId);
328 return;
329 }
Saurav Das1547b3f2017-05-05 17:01:08 -0700330 log.debug("Processing NextObjective id {} in dev {} - add group",
Saurav Das8a0732e2015-11-20 15:27:53 -0800331 nextObjective.id(), deviceId);
Charles Chan425854b2016-04-11 15:32:12 -0700332 groupHandler.addGroup(nextObjective);
Saurav Das8a0732e2015-11-20 15:27:53 -0800333 break;
334 case ADD_TO_EXISTING:
335 if (nextGroup != null) {
Saurav Das1547b3f2017-05-05 17:01:08 -0700336 log.debug("Processing NextObjective id {} in dev {} - add bucket",
Saurav Das8a0732e2015-11-20 15:27:53 -0800337 nextObjective.id(), deviceId);
Charles Chan425854b2016-04-11 15:32:12 -0700338 groupHandler.addBucketToGroup(nextObjective, nextGroup);
Saurav Das4f980082015-11-05 13:39:15 -0800339 } else {
Saurav Das8a0732e2015-11-20 15:27:53 -0800340 // it is possible that group-chain has not been fully created yet
Saurav Das423fe2b2015-12-04 10:52:59 -0800341 log.debug("Waiting to add bucket to group for next-id:{} in dev:{}",
342 nextObjective.id(), deviceId);
Yi Tseng47f82dc2017-03-05 22:48:39 -0800343
344 // by design multiple pending bucket is allowed for the group
345 groupHandler.pendingBuckets.compute(nextObjective.id(), (nextId, pendBkts) -> {
346 if (pendBkts == null) {
347 pendBkts = Sets.newHashSet();
348 }
349 pendBkts.add(nextObjective);
350 return pendBkts;
351 });
Saurav Das4f980082015-11-05 13:39:15 -0800352 }
Saurav Das8a0732e2015-11-20 15:27:53 -0800353 break;
354 case REMOVE:
355 if (nextGroup == null) {
356 log.warn("Cannot remove next {} that does not exist in device {}",
357 nextObjective.id(), deviceId);
358 return;
359 }
Saurav Das1547b3f2017-05-05 17:01:08 -0700360 log.debug("Processing NextObjective id {} in dev {} - remove group",
Saurav Das8a0732e2015-11-20 15:27:53 -0800361 nextObjective.id(), deviceId);
Charles Chan425854b2016-04-11 15:32:12 -0700362 groupHandler.removeGroup(nextObjective, nextGroup);
Saurav Das8a0732e2015-11-20 15:27:53 -0800363 break;
364 case REMOVE_FROM_EXISTING:
365 if (nextGroup == null) {
366 log.warn("Cannot remove from next {} that does not exist in device {}",
367 nextObjective.id(), deviceId);
368 return;
369 }
Saurav Das1547b3f2017-05-05 17:01:08 -0700370 log.debug("Processing NextObjective id {} in dev {} - remove bucket",
Saurav Das8a0732e2015-11-20 15:27:53 -0800371 nextObjective.id(), deviceId);
Charles Chan425854b2016-04-11 15:32:12 -0700372 groupHandler.removeBucketFromGroup(nextObjective, nextGroup);
Saurav Das8a0732e2015-11-20 15:27:53 -0800373 break;
Saurav Dasceccf242017-08-03 18:30:35 -0700374 case VERIFY:
375 if (nextGroup == null) {
376 log.warn("Cannot verify next {} that does not exist in device {}",
377 nextObjective.id(), deviceId);
378 return;
379 }
380 log.debug("Processing NextObjective id {} in dev {} - verify",
381 nextObjective.id(), deviceId);
382 groupHandler.verifyGroup(nextObjective, nextGroup);
383 break;
Saurav Das8a0732e2015-11-20 15:27:53 -0800384 default:
Saurav Das4f980082015-11-05 13:39:15 -0800385 log.warn("Unsupported operation {}", nextObjective.op());
Saurav Das822c4e22015-10-23 10:51:11 -0700386 }
387 }
388
389 //////////////////////////////////////
390 // Flow handling
391 //////////////////////////////////////
392
393 /**
Yi Tseng0fdc07e2017-04-24 19:17:08 -0700394 * As per OFDPA 2.0 TTP, filtering of VLAN ids and MAC addresses (for routing)
395 * configured on switch ports happen in different tables.
Saurav Das822c4e22015-10-23 10:51:11 -0700396 *
397 * @param filt the filtering objective
398 * @param install indicates whether to add or remove the objective
399 * @param applicationId the application that sent this objective
400 */
Saurav Das52025962016-01-28 22:30:01 -0800401 protected void processFilter(FilteringObjective filt,
402 boolean install, ApplicationId applicationId) {
Saurav Das822c4e22015-10-23 10:51:11 -0700403 // This driver only processes filtering criteria defined with switch
404 // ports as the key
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530405 PortCriterion portCriterion = null;
Saurav Das822c4e22015-10-23 10:51:11 -0700406 EthCriterion ethCriterion = null;
407 VlanIdCriterion vidCriterion = null;
Saurav Das822c4e22015-10-23 10:51:11 -0700408 if (!filt.key().equals(Criteria.dummy()) &&
409 filt.key().type() == Criterion.Type.IN_PORT) {
410 portCriterion = (PortCriterion) filt.key();
Saurav Das822c4e22015-10-23 10:51:11 -0700411 }
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530412 if (portCriterion == null) {
413 log.debug("No IN_PORT defined in filtering objective from app: {}",
414 applicationId);
415 } else {
416 log.debug("Received filtering objective for dev/port: {}/{}", deviceId,
417 portCriterion.port());
418 }
Saurav Das822c4e22015-10-23 10:51:11 -0700419 // convert filtering conditions for switch-intfs into flowrules
420 FlowRuleOperations.Builder ops = FlowRuleOperations.builder();
421 for (Criterion criterion : filt.conditions()) {
Yi Tseng0fdc07e2017-04-24 19:17:08 -0700422 switch (criterion.type()) {
423 case ETH_DST:
424 case ETH_DST_MASKED:
425 ethCriterion = (EthCriterion) criterion;
426 break;
427 case VLAN_VID:
428 vidCriterion = (VlanIdCriterion) criterion;
429 break;
430 default:
431 log.warn("Unsupported filter {}", criterion);
432 fail(filt, ObjectiveError.UNSUPPORTED);
433 return;
Saurav Das822c4e22015-10-23 10:51:11 -0700434 }
435 }
436
Saurav Das0e99e2b2015-10-28 12:39:42 -0700437 VlanId assignedVlan = null;
Charles Chane849c192016-01-11 18:28:54 -0800438 if (vidCriterion != null) {
Charles Chand55e84d2016-03-30 17:54:24 -0700439 // Use the VLAN in criterion if metadata is not present and the traffic is tagged
Charles Chanc550f2e2017-12-19 19:55:57 -0800440 if (!vidCriterion.vlanId().equals(VlanId.NONE)) {
Charles Chane849c192016-01-11 18:28:54 -0800441 assignedVlan = vidCriterion.vlanId();
Charles Chanc550f2e2017-12-19 19:55:57 -0800442 } else if (filt.meta() != null) {
443 assignedVlan = readVlanFromTreatment(filt.meta());
Charles Chand55e84d2016-03-30 17:54:24 -0700444 }
Charles Chane849c192016-01-11 18:28:54 -0800445
Charles Chand55e84d2016-03-30 17:54:24 -0700446 if (assignedVlan == null) {
447 log.error("Driver fails to extract VLAN information. "
Frank Wangd8ab0962017-08-11 11:09:30 +0800448 + "Not processing VLAN filters on device {}.", deviceId);
Charles Chand55e84d2016-03-30 17:54:24 -0700449 log.debug("VLAN ID in criterion={}, metadata={}",
450 readVlanFromTreatment(filt.meta()), vidCriterion.vlanId());
451 fail(filt, ObjectiveError.BADPARAMS);
452 return;
Saurav Das0e99e2b2015-10-28 12:39:42 -0700453 }
454 }
455
Yi Tseng3a77b4f2017-02-06 15:02:17 -0800456 if (ethCriterion == null || ethCriterion.mac().equals(NONE)) {
Charles Chan985b12e2016-05-11 19:47:22 -0700457 // NOTE: it is possible that a filtering objective only has vidCriterion
Saurav Das961beb22017-03-29 19:09:17 -0700458 log.warn("filtering objective missing dstMac, cannot program TMAC table");
Saurav Das822c4e22015-10-23 10:51:11 -0700459 } else {
460 for (FlowRule tmacRule : processEthDstFilter(portCriterion, ethCriterion,
Saurav Das0e99e2b2015-10-28 12:39:42 -0700461 vidCriterion, assignedVlan,
462 applicationId)) {
Saurav Das961beb22017-03-29 19:09:17 -0700463 log.trace("{} MAC filtering rules in TMAC table: {} for dev: {}",
Saurav Das018605f2017-02-18 14:05:44 -0800464 (install) ? "adding" : "removing", tmacRule, deviceId);
Charles Chanc550f2e2017-12-19 19:55:57 -0800465
466 if (install) {
467 ops = ops.add(tmacRule);
468 } else {
469 // NOTE: Only remove TMAC flow when there is no more enabled port within the
470 // same VLAN on this device if TMAC doesn't support matching on in_port.
471 if (matchInPortTmacTable() || (filt.meta() != null && filt.meta().clearedDeferred())) {
472 ops = ops.remove(tmacRule);
473 } else {
474 log.debug("Abort TMAC flow removal on {}. Some other ports still share this TMAC flow");
475 }
476 }
Saurav Das822c4e22015-10-23 10:51:11 -0700477 }
478 }
479
Charles Chan985b12e2016-05-11 19:47:22 -0700480 if (vidCriterion == null) {
481 // NOTE: it is possible that a filtering objective only has ethCriterion
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530482 log.info("filtering objective missing VLAN, cannot program VLAN Table");
Saurav Das822c4e22015-10-23 10:51:11 -0700483 } else {
Charles Chan14967c22015-12-07 11:11:50 -0800484 /*
485 * NOTE: Separate vlan filtering rules and assignment rules
486 * into different stage in order to guarantee that filtering rules
Saurav Das52025962016-01-28 22:30:01 -0800487 * always go first, as required by ofdpa.
Charles Chan14967c22015-12-07 11:11:50 -0800488 */
489 List<FlowRule> allRules = processVlanIdFilter(
490 portCriterion, vidCriterion, assignedVlan, applicationId);
491 List<FlowRule> filteringRules = new ArrayList<>();
492 List<FlowRule> assignmentRules = new ArrayList<>();
493
494 allRules.forEach(flowRule -> {
Charles Chand1172632017-03-15 17:33:09 -0700495 VlanId vlanId;
496 if (requireVlanExtensions()) {
497 ExtensionCriterion extCriterion =
498 (ExtensionCriterion) flowRule.selector().getCriterion(Criterion.Type.EXTENSION);
499 vlanId = ((OfdpaMatchVlanVid) extCriterion.extensionSelector()).vlanId();
500 } else {
501 VlanIdCriterion vlanIdCriterion =
502 (VlanIdCriterion) flowRule.selector().getCriterion(Criterion.Type.VLAN_VID);
503 vlanId = vlanIdCriterion.vlanId();
504 }
Charles Chanbe8aea42016-02-24 12:04:47 -0800505 if (!vlanId.equals(VlanId.NONE)) {
Charles Chan14967c22015-12-07 11:11:50 -0800506 filteringRules.add(flowRule);
507 } else {
508 assignmentRules.add(flowRule);
509 }
510 });
511
512 for (FlowRule filteringRule : filteringRules) {
Saurav Das961beb22017-03-29 19:09:17 -0700513 log.trace("{} VLAN filtering rule in VLAN table: {} for dev: {}",
Saurav Das018605f2017-02-18 14:05:44 -0800514 (install) ? "adding" : "removing", filteringRule, deviceId);
Charles Chan14967c22015-12-07 11:11:50 -0800515 ops = install ? ops.add(filteringRule) : ops.remove(filteringRule);
516 }
517
518 ops.newStage();
519
520 for (FlowRule assignmentRule : assignmentRules) {
Saurav Das961beb22017-03-29 19:09:17 -0700521 log.trace("{} VLAN assignment rule in VLAN table: {} for dev: {}",
Saurav Das018605f2017-02-18 14:05:44 -0800522 (install) ? "adding" : "removing", assignmentRule, deviceId);
Charles Chan14967c22015-12-07 11:11:50 -0800523 ops = install ? ops.add(assignmentRule) : ops.remove(assignmentRule);
Saurav Das822c4e22015-10-23 10:51:11 -0700524 }
525 }
526
Saurav Das822c4e22015-10-23 10:51:11 -0700527 // apply filtering flow rules
528 flowRuleService.apply(ops.build(new FlowRuleOperationsContext() {
529 @Override
530 public void onSuccess(FlowRuleOperations ops) {
Saurav Das018605f2017-02-18 14:05:44 -0800531 log.debug("Applied {} filtering rules in device {}",
Saurav Das822c4e22015-10-23 10:51:11 -0700532 ops.stages().get(0).size(), deviceId);
533 pass(filt);
534 }
535
536 @Override
537 public void onError(FlowRuleOperations ops) {
538 log.info("Failed to apply all filtering rules in dev {}", deviceId);
539 fail(filt, ObjectiveError.FLOWINSTALLATIONFAILED);
540 }
541 }));
542
543 }
544
545 /**
Charles Chand1172632017-03-15 17:33:09 -0700546 * Internal implementation of processVlanIdFilter.
547 * <p>
548 * The is_present bit in set_vlan_vid action is required to be 0 in OFDPA i12.
549 * Since it is non-OF spec, we need an extension treatment for that.
550 * The useVlanExtension must be set to false for OFDPA i12.
551 * </p>
Saurav Das0e99e2b2015-10-28 12:39:42 -0700552 *
Charles Chanf9e98652016-09-07 16:54:23 -0700553 * @param portCriterion port on device for which this filter is programmed
554 * @param vidCriterion vlan assigned to port, or NONE for untagged
555 * @param assignedVlan assigned vlan-id for untagged packets
556 * @param applicationId for application programming this filter
Saurav Das822c4e22015-10-23 10:51:11 -0700557 * @return list of FlowRule for port-vlan filters
558 */
559 protected List<FlowRule> processVlanIdFilter(PortCriterion portCriterion,
Yi Tsengef19de12017-04-24 11:33:05 -0700560 VlanIdCriterion vidCriterion,
561 VlanId assignedVlan,
562 ApplicationId applicationId) {
Charles Chan14967c22015-12-07 11:11:50 -0800563 List<FlowRule> rules = new ArrayList<>();
Saurav Das822c4e22015-10-23 10:51:11 -0700564 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
565 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
Charles Chan14967c22015-12-07 11:11:50 -0800566 TrafficSelector.Builder preSelector = null;
567 TrafficTreatment.Builder preTreatment = null;
568
Saurav Das4f980082015-11-05 13:39:15 -0800569 treatment.transition(TMAC_TABLE);
570
Saurav Das822c4e22015-10-23 10:51:11 -0700571 if (vidCriterion.vlanId() == VlanId.NONE) {
572 // untagged packets are assigned vlans
Charles Chand1172632017-03-15 17:33:09 -0700573 preSelector = DefaultTrafficSelector.builder();
574 if (requireVlanExtensions()) {
575 OfdpaMatchVlanVid ofdpaMatchVlanVid = new OfdpaMatchVlanVid(VlanId.NONE);
576 selector.extension(ofdpaMatchVlanVid, deviceId);
Charles Chanf9e98652016-09-07 16:54:23 -0700577 OfdpaSetVlanVid ofdpaSetVlanVid = new OfdpaSetVlanVid(assignedVlan);
578 treatment.extension(ofdpaSetVlanVid, deviceId);
Charles Chand1172632017-03-15 17:33:09 -0700579
580 OfdpaMatchVlanVid preOfdpaMatchVlanVid = new OfdpaMatchVlanVid(assignedVlan);
581 preSelector.extension(preOfdpaMatchVlanVid, deviceId);
Charles Chanf9e98652016-09-07 16:54:23 -0700582 } else {
Charles Chand1172632017-03-15 17:33:09 -0700583 selector.matchVlanId(VlanId.NONE);
Charles Chanf9e98652016-09-07 16:54:23 -0700584 treatment.setVlanId(assignedVlan);
Charles Chand1172632017-03-15 17:33:09 -0700585
586 preSelector.matchVlanId(assignedVlan);
587 }
588 preTreatment = DefaultTrafficTreatment.builder().transition(TMAC_TABLE);
589 } else {
590 if (requireVlanExtensions()) {
591 OfdpaMatchVlanVid ofdpaMatchVlanVid = new OfdpaMatchVlanVid(vidCriterion.vlanId());
592 selector.extension(ofdpaMatchVlanVid, deviceId);
593 } else {
594 selector.matchVlanId(vidCriterion.vlanId());
Charles Chanf9e98652016-09-07 16:54:23 -0700595 }
Charles Chan14967c22015-12-07 11:11:50 -0800596
Charles Chand55e84d2016-03-30 17:54:24 -0700597 if (!assignedVlan.equals(vidCriterion.vlanId())) {
Charles Chand1172632017-03-15 17:33:09 -0700598 if (requireVlanExtensions()) {
Charles Chanc03782d2017-01-31 13:57:55 -0800599 OfdpaSetVlanVid ofdpaSetVlanVid = new OfdpaSetVlanVid(assignedVlan);
600 treatment.extension(ofdpaSetVlanVid, deviceId);
601 } else {
602 treatment.setVlanId(assignedVlan);
603 }
Charles Chand55e84d2016-03-30 17:54:24 -0700604 }
Saurav Das822c4e22015-10-23 10:51:11 -0700605 }
Saurav Das822c4e22015-10-23 10:51:11 -0700606
607 // ofdpa cannot match on ALL portnumber, so we need to use separate
608 // rules for each port.
Charles Chan14967c22015-12-07 11:11:50 -0800609 List<PortNumber> portnums = new ArrayList<>();
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530610 if (portCriterion != null) {
611 if (PortNumber.ALL.equals(portCriterion.port())) {
612 for (Port port : deviceService.getPorts(deviceId)) {
613 if (port.number().toLong() > 0 && port.number().toLong() < OFPP_MAX) {
614 portnums.add(port.number());
615 }
Saurav Das822c4e22015-10-23 10:51:11 -0700616 }
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530617 } else {
618 portnums.add(portCriterion.port());
Saurav Das822c4e22015-10-23 10:51:11 -0700619 }
620 } else {
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530621 log.warn("Filtering Objective missing Port Criterion . " +
622 "VLAN Table cannot be programmed for {}",
623 deviceId);
Saurav Das822c4e22015-10-23 10:51:11 -0700624 }
Saurav Das4f980082015-11-05 13:39:15 -0800625
Saurav Das822c4e22015-10-23 10:51:11 -0700626 for (PortNumber pnum : portnums) {
Saurav Das4f980082015-11-05 13:39:15 -0800627 // create rest of flowrule
Saurav Das822c4e22015-10-23 10:51:11 -0700628 selector.matchInPort(pnum);
629 FlowRule rule = DefaultFlowRule.builder()
630 .forDevice(deviceId)
631 .withSelector(selector.build())
632 .withTreatment(treatment.build())
633 .withPriority(DEFAULT_PRIORITY)
634 .fromApp(applicationId)
635 .makePermanent()
636 .forTable(VLAN_TABLE).build();
Charles Chan14967c22015-12-07 11:11:50 -0800637
638 if (preSelector != null) {
639 preSelector.matchInPort(pnum);
640 FlowRule preRule = DefaultFlowRule.builder()
641 .forDevice(deviceId)
642 .withSelector(preSelector.build())
643 .withTreatment(preTreatment.build())
644 .withPriority(DEFAULT_PRIORITY)
645 .fromApp(applicationId)
646 .makePermanent()
647 .forTable(VLAN_TABLE).build();
648 rules.add(preRule);
649 }
650
Saurav Das822c4e22015-10-23 10:51:11 -0700651 rules.add(rule);
652 }
653 return rules;
654 }
655
656 /**
657 * Allows routed packets with correct destination MAC to be directed
658 * to unicast-IP routing table or MPLS forwarding table.
Saurav Das822c4e22015-10-23 10:51:11 -0700659 *
660 * @param portCriterion port on device for which this filter is programmed
661 * @param ethCriterion dstMac of device for which is filter is programmed
662 * @param vidCriterion vlan assigned to port, or NONE for untagged
Saurav Das0e99e2b2015-10-28 12:39:42 -0700663 * @param assignedVlan assigned vlan-id for untagged packets
Saurav Das822c4e22015-10-23 10:51:11 -0700664 * @param applicationId for application programming this filter
665 * @return list of FlowRule for port-vlan filters
666
667 */
668 protected List<FlowRule> processEthDstFilter(PortCriterion portCriterion,
669 EthCriterion ethCriterion,
670 VlanIdCriterion vidCriterion,
Saurav Das0e99e2b2015-10-28 12:39:42 -0700671 VlanId assignedVlan,
Saurav Das822c4e22015-10-23 10:51:11 -0700672 ApplicationId applicationId) {
Charles Chan5270ed02016-01-30 23:22:37 -0800673 // Consider PortNumber.ANY as wildcard. Match ETH_DST only
Sivachidambaram Subramanianc598d062017-05-16 22:42:06 +0530674 if (portCriterion != null && PortNumber.ANY.equals(portCriterion.port())) {
Charles Chan5270ed02016-01-30 23:22:37 -0800675 return processEthDstOnlyFilter(ethCriterion, applicationId);
676 }
677
Charles Chan5b9df8d2016-03-28 22:21:40 -0700678 // Multicast MAC
679 if (ethCriterion.mask() != null) {
Charles Chanb4879a52017-10-20 19:09:16 -0700680 return processMcastEthDstFilter(ethCriterion, assignedVlan, applicationId);
Charles Chan5b9df8d2016-03-28 22:21:40 -0700681 }
682
Saurav Das822c4e22015-10-23 10:51:11 -0700683 //handling untagged packets via assigned VLAN
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530684 if (vidCriterion != null && vidCriterion.vlanId() == VlanId.NONE) {
Saurav Das0e99e2b2015-10-28 12:39:42 -0700685 vidCriterion = (VlanIdCriterion) Criteria.matchVlanId(assignedVlan);
Saurav Das822c4e22015-10-23 10:51:11 -0700686 }
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530687 List<FlowRule> rules = new ArrayList<>();
688 OfdpaMatchVlanVid ofdpaMatchVlanVid = null;
689 if (vidCriterion != null && requireVlanExtensions()) {
690 ofdpaMatchVlanVid = new OfdpaMatchVlanVid(vidCriterion.vlanId());
691 }
Saurav Das822c4e22015-10-23 10:51:11 -0700692 // ofdpa cannot match on ALL portnumber, so we need to use separate
693 // rules for each port.
Yuta HIGUCHI2dce08a2017-04-20 21:57:48 -0700694 List<PortNumber> portnums = new ArrayList<>();
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530695 if (portCriterion != null) {
696 if (PortNumber.ALL.equals(portCriterion.port())) {
697 for (Port port : deviceService.getPorts(deviceId)) {
698 if (port.number().toLong() > 0 && port.number().toLong() < OFPP_MAX) {
699 portnums.add(port.number());
700 }
Saurav Das822c4e22015-10-23 10:51:11 -0700701 }
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530702 } else {
703 portnums.add(portCriterion.port());
704 }
705 for (PortNumber pnum : portnums) {
706 rules.add(buildTmacRuleForIpv4(ethCriterion,
707 vidCriterion,
708 ofdpaMatchVlanVid,
709 applicationId,
710 pnum));
711 rules.add(buildTmacRuleForMpls(ethCriterion,
712 vidCriterion,
713 ofdpaMatchVlanVid,
714 applicationId,
715 pnum));
716 rules.add(buildTmacRuleForIpv6(ethCriterion,
717 vidCriterion,
718 ofdpaMatchVlanVid,
719 applicationId,
720 pnum));
Saurav Das822c4e22015-10-23 10:51:11 -0700721 }
722 } else {
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530723 rules.add(buildTmacRuleForIpv4(ethCriterion,
724 vidCriterion,
725 ofdpaMatchVlanVid,
726 applicationId,
727 null));
728 rules.add(buildTmacRuleForMpls(ethCriterion,
729 vidCriterion,
730 ofdpaMatchVlanVid,
731 applicationId,
732 null));
733 rules.add(buildTmacRuleForIpv6(ethCriterion,
734 vidCriterion,
735 ofdpaMatchVlanVid,
736 applicationId,
737 null));
Saurav Das822c4e22015-10-23 10:51:11 -0700738 }
739 return rules;
740 }
741
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530742 /**
743 * Builds TMAC rules for IPv4 packets.
744 *
Charles Chanc550f2e2017-12-19 19:55:57 -0800745 * @param ethCriterion dst mac matching
746 * @param vidCriterion vlan id assigned to the port
747 * @param ofdpaMatchVlanVid OFDPA vlan id matching
748 * @param applicationId application id
749 * @param pnum port number
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530750 * @return TMAC rule for IPV4 packets
751 */
752 private FlowRule buildTmacRuleForIpv4(EthCriterion ethCriterion,
753 VlanIdCriterion vidCriterion,
754 OfdpaMatchVlanVid ofdpaMatchVlanVid,
755 ApplicationId applicationId,
756 PortNumber pnum) {
757 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
758 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
759 if (pnum != null) {
760 if (matchInPortTmacTable()) {
761 selector.matchInPort(pnum);
762 } else {
Saurav Dasf14d9ef2017-12-05 15:00:23 -0800763 log.debug("Pipeline does not support IN_PORT matching in TMAC table, " +
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530764 "ignoring the IN_PORT criteria");
765 }
766 }
767 if (vidCriterion != null) {
768 if (requireVlanExtensions()) {
769 selector.extension(ofdpaMatchVlanVid, deviceId);
770 } else {
771 selector.matchVlanId(vidCriterion.vlanId());
772 }
773 }
774 selector.matchEthType(Ethernet.TYPE_IPV4);
775 selector.matchEthDst(ethCriterion.mac());
776 treatment.transition(UNICAST_ROUTING_TABLE);
777 return DefaultFlowRule.builder()
778 .forDevice(deviceId)
779 .withSelector(selector.build())
780 .withTreatment(treatment.build())
781 .withPriority(DEFAULT_PRIORITY)
782 .fromApp(applicationId)
783 .makePermanent()
784 .forTable(TMAC_TABLE).build();
785 }
786
787 /**
788 * Builds TMAC rule for MPLS packets.
789 *
Charles Chanc550f2e2017-12-19 19:55:57 -0800790 * @param ethCriterion dst mac matching
791 * @param vidCriterion vlan id assigned to the port
792 * @param ofdpaMatchVlanVid OFDPA vlan id matching
793 * @param applicationId application id
794 * @param pnum port number
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530795 * @return TMAC rule for MPLS packets
796 */
797 private FlowRule buildTmacRuleForMpls(EthCriterion ethCriterion,
798 VlanIdCriterion vidCriterion,
799 OfdpaMatchVlanVid ofdpaMatchVlanVid,
800 ApplicationId applicationId,
801 PortNumber pnum) {
802 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
803 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
804 if (pnum != null) {
805 if (matchInPortTmacTable()) {
806 selector.matchInPort(pnum);
807 } else {
Saurav Dasf14d9ef2017-12-05 15:00:23 -0800808 log.debug("Pipeline does not support IN_PORT matching in TMAC table, " +
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530809 "ignoring the IN_PORT criteria");
810 }
811 }
812 if (vidCriterion != null) {
813 if (requireVlanExtensions()) {
814 selector.extension(ofdpaMatchVlanVid, deviceId);
815 } else {
816 selector.matchVlanId(vidCriterion.vlanId());
817 }
818 }
819 selector.matchEthType(Ethernet.MPLS_UNICAST);
820 selector.matchEthDst(ethCriterion.mac());
821 treatment.transition(MPLS_TABLE_0);
822 return DefaultFlowRule.builder()
823 .forDevice(deviceId)
824 .withSelector(selector.build())
825 .withTreatment(treatment.build())
826 .withPriority(DEFAULT_PRIORITY)
827 .fromApp(applicationId)
828 .makePermanent()
829 .forTable(TMAC_TABLE).build();
830 }
831
832 /**
833 * Builds TMAC rules for IPv6 packets.
834 *
Charles Chanc550f2e2017-12-19 19:55:57 -0800835 * @param ethCriterion dst mac matching
836 * @param vidCriterion vlan id assigned to the port
837 * @param ofdpaMatchVlanVid OFDPA vlan id matching
838 * @param applicationId application id
839 * @param pnum port number
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530840 * @return TMAC rule for IPV6 packets
841 */
842 private FlowRule buildTmacRuleForIpv6(EthCriterion ethCriterion,
843 VlanIdCriterion vidCriterion,
844 OfdpaMatchVlanVid ofdpaMatchVlanVid,
845 ApplicationId applicationId,
846 PortNumber pnum) {
847 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
848 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
849 if (pnum != null) {
850 if (matchInPortTmacTable()) {
851 selector.matchInPort(pnum);
852 } else {
Saurav Dasf14d9ef2017-12-05 15:00:23 -0800853 log.debug("Pipeline does not support IN_PORT matching in TMAC table, " +
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530854 "ignoring the IN_PORT criteria");
855 }
856 }
857 if (vidCriterion != null) {
858 if (requireVlanExtensions()) {
859 selector.extension(ofdpaMatchVlanVid, deviceId);
860 } else {
861 selector.matchVlanId(vidCriterion.vlanId());
862 }
863 }
864 selector.matchEthType(Ethernet.TYPE_IPV6);
865 selector.matchEthDst(ethCriterion.mac());
866 treatment.transition(UNICAST_ROUTING_TABLE);
867 return DefaultFlowRule.builder()
868 .forDevice(deviceId)
869 .withSelector(selector.build())
870 .withTreatment(treatment.build())
871 .withPriority(DEFAULT_PRIORITY)
872 .fromApp(applicationId)
873 .makePermanent()
874 .forTable(TMAC_TABLE).build();
875 }
876
Charles Chan5270ed02016-01-30 23:22:37 -0800877 protected List<FlowRule> processEthDstOnlyFilter(EthCriterion ethCriterion,
Yi Tsengef19de12017-04-24 11:33:05 -0700878 ApplicationId applicationId) {
Pier Luigi0e358632017-01-31 09:35:05 -0800879 ImmutableList.Builder<FlowRule> builder = ImmutableList.builder();
880
Charles Chan5270ed02016-01-30 23:22:37 -0800881 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
882 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
883 selector.matchEthType(Ethernet.TYPE_IPV4);
884 selector.matchEthDst(ethCriterion.mac());
885 treatment.transition(UNICAST_ROUTING_TABLE);
886 FlowRule rule = DefaultFlowRule.builder()
887 .forDevice(deviceId)
888 .withSelector(selector.build())
889 .withTreatment(treatment.build())
890 .withPriority(DEFAULT_PRIORITY)
891 .fromApp(applicationId)
892 .makePermanent()
893 .forTable(TMAC_TABLE).build();
Pier Luigi0e358632017-01-31 09:35:05 -0800894 builder.add(rule);
895
896 selector = DefaultTrafficSelector.builder();
897 treatment = DefaultTrafficTreatment.builder();
898 selector.matchEthType(Ethernet.TYPE_IPV6);
899 selector.matchEthDst(ethCriterion.mac());
900 treatment.transition(UNICAST_ROUTING_TABLE);
901 rule = DefaultFlowRule.builder()
902 .forDevice(deviceId)
903 .withSelector(selector.build())
904 .withTreatment(treatment.build())
905 .withPriority(DEFAULT_PRIORITY)
906 .fromApp(applicationId)
907 .makePermanent()
908 .forTable(TMAC_TABLE).build();
909 return builder.add(rule).build();
Charles Chan5270ed02016-01-30 23:22:37 -0800910 }
911
Charles Chan5b9df8d2016-03-28 22:21:40 -0700912 protected List<FlowRule> processMcastEthDstFilter(EthCriterion ethCriterion,
Charles Chanb4879a52017-10-20 19:09:16 -0700913 VlanId assignedVlan,
Yi Tsengef19de12017-04-24 11:33:05 -0700914 ApplicationId applicationId) {
Julia Ferguson65428c32017-08-10 18:15:24 +0000915 ImmutableList.Builder<FlowRule> builder = ImmutableList.builder();
Charles Chan5b9df8d2016-03-28 22:21:40 -0700916 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
917 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
Charles Chanb4879a52017-10-20 19:09:16 -0700918 FlowRule rule;
Julia Ferguson65428c32017-08-10 18:15:24 +0000919
Charles Chanb4879a52017-10-20 19:09:16 -0700920 if (IPV4_MULTICAST.equals(ethCriterion.mac())) {
921 selector.matchEthType(Ethernet.TYPE_IPV4);
922 selector.matchEthDstMasked(ethCriterion.mac(), ethCriterion.mask());
923 selector.matchVlanId(assignedVlan);
924 treatment.transition(MULTICAST_ROUTING_TABLE);
925 rule = DefaultFlowRule.builder()
926 .forDevice(deviceId)
927 .withSelector(selector.build())
928 .withTreatment(treatment.build())
929 .withPriority(DEFAULT_PRIORITY)
930 .fromApp(applicationId)
931 .makePermanent()
932 .forTable(TMAC_TABLE).build();
933 builder.add(rule);
934 }
935
936 if (IPV6_MULTICAST.equals(ethCriterion.mac())) {
937 selector = DefaultTrafficSelector.builder();
938 treatment = DefaultTrafficTreatment.builder();
939 selector.matchEthType(Ethernet.TYPE_IPV6);
940 selector.matchEthDstMasked(ethCriterion.mac(), ethCriterion.mask());
941 selector.matchVlanId(assignedVlan);
942 treatment.transition(MULTICAST_ROUTING_TABLE);
943 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();
951 builder.add(rule);
952 }
953 return builder.build();
Charles Chan5b9df8d2016-03-28 22:21:40 -0700954 }
955
Saurav Das822c4e22015-10-23 10:51:11 -0700956 private Collection<FlowRule> processForward(ForwardingObjective fwd) {
957 switch (fwd.flag()) {
958 case SPECIFIC:
959 return processSpecific(fwd);
960 case VERSATILE:
961 return processVersatile(fwd);
962 default:
963 fail(fwd, ObjectiveError.UNKNOWN);
964 log.warn("Unknown forwarding flag {}", fwd.flag());
965 }
966 return Collections.emptySet();
967 }
968
969 /**
970 * In the OF-DPA 2.0 pipeline, versatile forwarding objectives go to the
971 * ACL table.
972 * @param fwd the forwarding objective of type 'versatile'
973 * @return a collection of flow rules to be sent to the switch. An empty
974 * collection may be returned if there is a problem in processing
975 * the flow rule
976 */
Saurav Das52025962016-01-28 22:30:01 -0800977 protected Collection<FlowRule> processVersatile(ForwardingObjective fwd) {
Saurav Das018605f2017-02-18 14:05:44 -0800978 log.debug("Processing versatile forwarding objective:{} in dev:{}",
Saurav Dasd2fded02016-12-02 15:43:47 -0800979 fwd.id(), deviceId);
Saurav Das822c4e22015-10-23 10:51:11 -0700980
981 EthTypeCriterion ethType =
Saurav Das77b5e902016-01-27 17:01:59 -0800982 (EthTypeCriterion) fwd.selector().getCriterion(Criterion.Type.ETH_TYPE);
Saurav Das822c4e22015-10-23 10:51:11 -0700983 if (ethType == null) {
Saurav Dasd2fded02016-12-02 15:43:47 -0800984 log.error("Versatile forwarding objective:{} must include ethType",
985 fwd.id());
Saurav Das822c4e22015-10-23 10:51:11 -0700986 fail(fwd, ObjectiveError.BADPARAMS);
987 return Collections.emptySet();
988 }
989 if (fwd.nextId() == null && fwd.treatment() == null) {
990 log.error("Forwarding objective {} from {} must contain "
991 + "nextId or Treatment", fwd.selector(), fwd.appId());
Zsolt Haraszti9faab752016-02-17 15:55:20 -0800992 fail(fwd, ObjectiveError.BADPARAMS);
Saurav Das822c4e22015-10-23 10:51:11 -0700993 return Collections.emptySet();
994 }
Saurav Das49cb5a12016-01-16 22:54:07 -0800995
Saurav Das77b5e902016-01-27 17:01:59 -0800996 TrafficSelector.Builder sbuilder = DefaultTrafficSelector.builder();
997 fwd.selector().criteria().forEach(criterion -> {
998 if (criterion instanceof VlanIdCriterion) {
999 VlanId vlanId = ((VlanIdCriterion) criterion).vlanId();
1000 // ensure that match does not include vlan = NONE as OF-DPA does not
1001 // match untagged packets this way in the ACL table.
1002 if (vlanId.equals(VlanId.NONE)) {
1003 return;
1004 }
Charles Chand1172632017-03-15 17:33:09 -07001005 if (requireVlanExtensions()) {
1006 OfdpaMatchVlanVid ofdpaMatchVlanVid = new OfdpaMatchVlanVid(vlanId);
1007 sbuilder.extension(ofdpaMatchVlanVid, deviceId);
1008 } else {
1009 sbuilder.matchVlanId(vlanId);
1010 }
Pier Ventree0ae7a32016-11-23 09:57:42 -08001011 } else if (criterion instanceof Icmpv6TypeCriterion ||
1012 criterion instanceof Icmpv6CodeCriterion) {
1013 /*
1014 * We silenty discard these criterions, our current
1015 * OFDPA platform does not support these matches on
1016 * the ACL table.
1017 */
1018 log.warn("ICMPv6 Type and ICMPv6 Code are not supported");
Charles Chand9e47c62017-10-05 15:17:15 -07001019 } else if (criterion instanceof TcpPortCriterion || criterion instanceof UdpPortCriterion) {
1020 // FIXME: QMX switches do not support L4 dst port matching in ACL table.
1021 // Currently L4 dst port matching is only used by DHCP relay feature
1022 // and therefore is safe to be replaced with L4 src port matching.
1023 // We need to revisit this if L4 dst port is used for other purpose in the future.
1024 if (!supportIpv6L4Dst() && isIpv6(fwd.selector())) {
Charles Chand9e47c62017-10-05 15:17:15 -07001025 switch (criterion.type()) {
1026 case UDP_DST:
Charles Chand9e47c62017-10-05 15:17:15 -07001027 case UDP_DST_MASKED:
Charles Chand9e47c62017-10-05 15:17:15 -07001028 case TCP_DST:
Charles Chand9e47c62017-10-05 15:17:15 -07001029 case TCP_DST_MASKED:
Charles Chand9e47c62017-10-05 15:17:15 -07001030 break;
1031 default:
1032 sbuilder.add(criterion);
1033 }
1034 } else {
1035 sbuilder.add(criterion);
1036 }
Saurav Das77b5e902016-01-27 17:01:59 -08001037 } else {
1038 sbuilder.add(criterion);
1039 }
1040 });
1041
Saurav Das822c4e22015-10-23 10:51:11 -07001042 // XXX driver does not currently do type checking as per Tables 65-67 in
1043 // OFDPA 2.0 spec. The only allowed treatment is a punt to the controller.
Saurav Das49cb5a12016-01-16 22:54:07 -08001044 TrafficTreatment.Builder ttBuilder = DefaultTrafficTreatment.builder();
1045 if (fwd.treatment() != null) {
1046 for (Instruction ins : fwd.treatment().allInstructions()) {
1047 if (ins instanceof OutputInstruction) {
1048 OutputInstruction o = (OutputInstruction) ins;
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +05301049 if (o != null && PortNumber.CONTROLLER.equals(o.port())) {
Saurav Das49cb5a12016-01-16 22:54:07 -08001050 ttBuilder.add(o);
1051 } else {
1052 log.warn("Only allowed treatments in versatile forwarding "
1053 + "objectives are punts to the controller");
1054 }
1055 } else {
1056 log.warn("Cannot process instruction in versatile fwd {}", ins);
1057 }
Saurav Das822c4e22015-10-23 10:51:11 -07001058 }
Charles Chan2df0e8a2017-01-09 11:45:08 -08001059 if (fwd.treatment().clearedDeferred()) {
1060 ttBuilder.wipeDeferred();
1061 }
Saurav Das822c4e22015-10-23 10:51:11 -07001062 }
Saurav Das822c4e22015-10-23 10:51:11 -07001063 if (fwd.nextId() != null) {
Saurav Das49cb5a12016-01-16 22:54:07 -08001064 // overide case
1065 NextGroup next = getGroupForNextObjective(fwd.nextId());
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +05301066 if (next == null) {
1067 fail(fwd, ObjectiveError.BADPARAMS);
1068 return Collections.emptySet();
1069 }
Saurav Das49cb5a12016-01-16 22:54:07 -08001070 List<Deque<GroupKey>> gkeys = appKryo.deserialize(next.data());
1071 // we only need the top level group's key to point the flow to it
1072 Group group = groupService.getGroup(deviceId, gkeys.get(0).peekFirst());
1073 if (group == null) {
1074 log.warn("Group with key:{} for next-id:{} not found in dev:{}",
1075 gkeys.get(0).peekFirst(), fwd.nextId(), deviceId);
1076 fail(fwd, ObjectiveError.GROUPMISSING);
1077 return Collections.emptySet();
1078 }
1079 ttBuilder.deferred().group(group.id());
Saurav Das822c4e22015-10-23 10:51:11 -07001080 }
Saurav Das49cb5a12016-01-16 22:54:07 -08001081
1082 FlowRule.Builder ruleBuilder = DefaultFlowRule.builder()
1083 .fromApp(fwd.appId())
1084 .withPriority(fwd.priority())
1085 .forDevice(deviceId)
Saurav Das77b5e902016-01-27 17:01:59 -08001086 .withSelector(sbuilder.build())
Saurav Das49cb5a12016-01-16 22:54:07 -08001087 .withTreatment(ttBuilder.build())
1088 .makePermanent()
1089 .forTable(ACL_TABLE);
1090 return Collections.singletonList(ruleBuilder.build());
Saurav Das822c4e22015-10-23 10:51:11 -07001091 }
1092
1093 /**
1094 * In the OF-DPA 2.0 pipeline, specific forwarding refers to the IP table
Saurav Das8a0732e2015-11-20 15:27:53 -08001095 * (unicast or multicast) or the L2 table (mac + vlan) or the MPLS table.
Saurav Das822c4e22015-10-23 10:51:11 -07001096 *
1097 * @param fwd the forwarding objective of type 'specific'
1098 * @return a collection of flow rules. Typically there will be only one
1099 * for this type of forwarding objective. An empty set may be
1100 * returned if there is an issue in processing the objective.
1101 */
Saurav Das8a0732e2015-11-20 15:27:53 -08001102 protected Collection<FlowRule> processSpecific(ForwardingObjective fwd) {
Saurav Das25190812016-05-27 13:54:07 -07001103 log.debug("Processing specific fwd objective:{} in dev:{} with next:{}",
Saurav Das4ce45962015-11-24 23:21:05 -08001104 fwd.id(), deviceId, fwd.nextId());
1105 boolean isEthTypeObj = isSupportedEthTypeObjective(fwd);
1106 boolean isEthDstObj = isSupportedEthDstObjective(fwd);
1107
1108 if (isEthTypeObj) {
1109 return processEthTypeSpecific(fwd);
1110 } else if (isEthDstObj) {
1111 return processEthDstSpecific(fwd);
1112 } else {
1113 log.warn("processSpecific: Unsupported forwarding objective "
1114 + "criteria fwd:{} in dev:{}", fwd.nextId(), deviceId);
Saurav Das822c4e22015-10-23 10:51:11 -07001115 fail(fwd, ObjectiveError.UNSUPPORTED);
1116 return Collections.emptySet();
1117 }
Saurav Das4ce45962015-11-24 23:21:05 -08001118 }
1119
Saurav Das4ce45962015-11-24 23:21:05 -08001120 /**
1121 * Handles forwarding rules to the IP and MPLS tables.
1122 *
1123 * @param fwd the forwarding objective
1124 * @return A collection of flow rules, or an empty set
1125 */
1126 protected Collection<FlowRule> processEthTypeSpecific(ForwardingObjective fwd) {
Charles Chancad338a2016-09-16 18:03:11 -07001127 return processEthTypeSpecificInternal(fwd, false, ACL_TABLE);
Charles Chanf9e98652016-09-07 16:54:23 -07001128 }
1129
1130 /**
1131 * Internal implementation of processEthTypeSpecific.
1132 * <p>
1133 * Wildcarded IPv4_DST is not supported in OFDPA i12. Therefore, we break
1134 * the rule into 0.0.0.0/1 and 128.0.0.0/1.
1135 * The allowDefaultRoute must be set to false for OFDPA i12.
1136 * </p>
1137 *
1138 * @param fwd the forwarding objective
1139 * @param allowDefaultRoute allow wildcarded IPv4_DST or not
Ray Milkey0bb1e102016-11-10 14:51:27 -08001140 * @param mplsNextTable next MPLS table
Charles Chanf9e98652016-09-07 16:54:23 -07001141 * @return A collection of flow rules, or an empty set
1142 */
1143 protected Collection<FlowRule> processEthTypeSpecificInternal(ForwardingObjective fwd,
Charles Chancad338a2016-09-16 18:03:11 -07001144 boolean allowDefaultRoute,
1145 int mplsNextTable) {
Saurav Das4ce45962015-11-24 23:21:05 -08001146 TrafficSelector selector = fwd.selector();
1147 EthTypeCriterion ethType =
1148 (EthTypeCriterion) selector.getCriterion(Criterion.Type.ETH_TYPE);
Flavio Castroe10fa242016-01-15 12:43:51 -08001149 boolean popMpls = false;
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001150 boolean emptyGroup = false;
Charles Chan188ebf52015-12-23 00:15:11 -08001151 int forTableId;
Saurav Das8a0732e2015-11-20 15:27:53 -08001152 TrafficSelector.Builder filteredSelector = DefaultTrafficSelector.builder();
Charles Chan14967c22015-12-07 11:11:50 -08001153 TrafficTreatment.Builder tb = DefaultTrafficTreatment.builder();
Flavio Castroe10fa242016-01-15 12:43:51 -08001154 TrafficSelector.Builder complementarySelector = DefaultTrafficSelector.builder();
Charles Chan14967c22015-12-07 11:11:50 -08001155
Saurav Das8a0732e2015-11-20 15:27:53 -08001156 if (ethType.ethType().toShort() == Ethernet.TYPE_IPV4) {
Pier Luigi3bfe32c2017-01-30 09:47:36 -08001157 if (buildIpv4Selector(filteredSelector, complementarySelector, fwd, allowDefaultRoute) < 0) {
1158 return Collections.emptyList();
1159 }
1160 // We need to set properly the next table
Flavio Castroe10fa242016-01-15 12:43:51 -08001161 IpPrefix ipv4Dst = ((IPCriterion) selector.getCriterion(Criterion.Type.IPV4_DST)).ip();
Charles Chan5b9df8d2016-03-28 22:21:40 -07001162 if (ipv4Dst.isMulticast()) {
Charles Chan5b9df8d2016-03-28 22:21:40 -07001163 forTableId = MULTICAST_ROUTING_TABLE;
Flavio Castroe10fa242016-01-15 12:43:51 -08001164 } else {
Charles Chan5b9df8d2016-03-28 22:21:40 -07001165 forTableId = UNICAST_ROUTING_TABLE;
Flavio Castroe10fa242016-01-15 12:43:51 -08001166 }
Charles Chan14967c22015-12-07 11:11:50 -08001167 if (fwd.treatment() != null) {
1168 for (Instruction instr : fwd.treatment().allInstructions()) {
1169 if (instr instanceof L3ModificationInstruction &&
1170 ((L3ModificationInstruction) instr).subtype() == L3SubType.DEC_TTL) {
Saurav Das9c705342017-01-06 11:36:01 -08001171 // XXX decrementing IP ttl is done automatically for routing, this
1172 // action is ignored or rejected in ofdpa as it is not fully implemented
1173 //tb.deferred().add(instr);
Charles Chan14967c22015-12-07 11:11:50 -08001174 }
1175 }
1176 }
1177
Pier Ventree0ae7a32016-11-23 09:57:42 -08001178 } else if (ethType.ethType().toShort() == Ethernet.TYPE_IPV6) {
1179 if (buildIpv6Selector(filteredSelector, fwd) < 0) {
1180 return Collections.emptyList();
1181 }
Kalhee Kim3d26bf02017-10-04 14:59:11 +00001182 //We need to set the proper next table
1183 IpPrefix ipv6Dst = ((IPCriterion) selector.getCriterion(Criterion.Type.IPV6_DST)).ip();
1184 if (ipv6Dst.isMulticast()) {
1185 forTableId = MULTICAST_ROUTING_TABLE;
1186 } else {
1187 forTableId = UNICAST_ROUTING_TABLE;
1188 }
1189
Pier Ventree0ae7a32016-11-23 09:57:42 -08001190 if (fwd.treatment() != null) {
1191 for (Instruction instr : fwd.treatment().allInstructions()) {
1192 if (instr instanceof L3ModificationInstruction &&
1193 ((L3ModificationInstruction) instr).subtype() == L3SubType.DEC_TTL) {
1194 // XXX decrementing IP ttl is done automatically for routing, this
1195 // action is ignored or rejected in ofdpa as it is not fully implemented
1196 //tb.deferred().add(instr);
1197 }
1198 }
1199 }
Saurav Das8a0732e2015-11-20 15:27:53 -08001200 } else {
1201 filteredSelector
1202 .matchEthType(Ethernet.MPLS_UNICAST)
1203 .matchMplsLabel(((MplsCriterion)
1204 selector.getCriterion(Criterion.Type.MPLS_LABEL)).label());
1205 MplsBosCriterion bos = (MplsBosCriterion) selector
Pier Ventre140a8942016-11-02 07:26:38 -07001206 .getCriterion(MPLS_BOS);
Alex Yashchuk4caa8e82017-12-08 17:40:05 +02001207 if (bos != null && requireMplsBosMatch()) {
Saurav Das8a0732e2015-11-20 15:27:53 -08001208 filteredSelector.matchMplsBos(bos.mplsBos());
1209 }
1210 forTableId = MPLS_TABLE_1;
Saurav Das4ce45962015-11-24 23:21:05 -08001211 log.debug("processing MPLS specific forwarding objective {} -> next:{}"
1212 + " in dev {}", fwd.id(), fwd.nextId(), deviceId);
Saurav Das822c4e22015-10-23 10:51:11 -07001213
Charles Chan14967c22015-12-07 11:11:50 -08001214 if (fwd.treatment() != null) {
1215 for (Instruction instr : fwd.treatment().allInstructions()) {
1216 if (instr instanceof L2ModificationInstruction &&
1217 ((L2ModificationInstruction) instr).subtype() == L2SubType.MPLS_POP) {
Saurav Das8a0732e2015-11-20 15:27:53 -08001218 popMpls = true;
Pier Luigi3bfe32c2017-01-30 09:47:36 -08001219 // OF-DPA does not pop in MPLS table in some cases. For the L3 VPN, it requires
Saurav Das9c705342017-01-06 11:36:01 -08001220 // setting the MPLS_TYPE so pop can happen down the pipeline
Alex Yashchuk4caa8e82017-12-08 17:40:05 +02001221 if (requireMplsPop()) {
1222 if (mplsNextTable == MPLS_TYPE_TABLE && isNotMplsBos(selector)) {
1223 tb.immediate().popMpls();
1224 }
1225 } else {
1226 // Skip mpls pop action for mpls_unicast label
1227 if (instr instanceof ModMplsHeaderInstruction &&
1228 !((ModMplsHeaderInstruction) instr).ethernetType()
1229 .equals(EtherType.MPLS_UNICAST.ethType())) {
1230 tb.immediate().add(instr);
1231 }
Pier Luigi3bfe32c2017-01-30 09:47:36 -08001232 }
Charles Chan14967c22015-12-07 11:11:50 -08001233 }
Alex Yashchuk4caa8e82017-12-08 17:40:05 +02001234
1235 if (requireMplsTtlModification()) {
1236 if (instr instanceof L3ModificationInstruction &&
1237 ((L3ModificationInstruction) instr).subtype() == L3SubType.DEC_TTL) {
1238 // FIXME Should modify the app to send the correct DEC_MPLS_TTL instruction
1239 tb.immediate().decMplsTtl();
1240 }
1241 if (instr instanceof L3ModificationInstruction &&
1242 ((L3ModificationInstruction) instr).subtype() == L3SubType.TTL_IN) {
1243 tb.immediate().add(instr);
1244 }
Charles Chan14967c22015-12-07 11:11:50 -08001245 }
Saurav Das8a0732e2015-11-20 15:27:53 -08001246 }
1247 }
1248 }
Saurav Das822c4e22015-10-23 10:51:11 -07001249
1250 if (fwd.nextId() != null) {
Saurav Das8a0732e2015-11-20 15:27:53 -08001251 if (forTableId == MPLS_TABLE_1 && !popMpls) {
1252 log.warn("SR CONTINUE case cannot be handled as MPLS ECMP "
Saurav Das25190812016-05-27 13:54:07 -07001253 + "is not implemented in OF-DPA yet. Aborting this flow {} -> next:{}"
1254 + "in this device {}", fwd.id(), fwd.nextId(), deviceId);
Pier Ventree0ae7a32016-11-23 09:57:42 -08001255 // XXX We could convert to forwarding to a single-port, via a MPLS interface,
1256 // or a MPLS SWAP (with-same) but that would have to be handled in the next-objective.
1257 // Also the pop-mpls logic used here won't work in non-BoS case.
Saurav Das4ce45962015-11-24 23:21:05 -08001258 fail(fwd, ObjectiveError.FLOWINSTALLATIONFAILED);
Saurav Das8a0732e2015-11-20 15:27:53 -08001259 return Collections.emptySet();
1260 }
1261
Saurav Das423fe2b2015-12-04 10:52:59 -08001262 NextGroup next = getGroupForNextObjective(fwd.nextId());
1263 if (next != null) {
1264 List<Deque<GroupKey>> gkeys = appKryo.deserialize(next.data());
1265 // we only need the top level group's key to point the flow to it
1266 Group group = groupService.getGroup(deviceId, gkeys.get(0).peekFirst());
Pier Ventre140a8942016-11-02 07:26:38 -07001267 if (isNotMplsBos(selector) && group.type().equals(HASHED)) {
1268 log.warn("SR CONTINUE case cannot be handled as MPLS ECMP "
1269 + "is not implemented in OF-DPA yet. Aborting this flow {} -> next:{}"
1270 + "in this device {}", fwd.id(), fwd.nextId(), deviceId);
1271 fail(fwd, ObjectiveError.FLOWINSTALLATIONFAILED);
1272 return Collections.emptySet();
1273 }
Saurav Das423fe2b2015-12-04 10:52:59 -08001274 if (group == null) {
1275 log.warn("Group with key:{} for next-id:{} not found in dev:{}",
1276 gkeys.get(0).peekFirst(), fwd.nextId(), deviceId);
1277 fail(fwd, ObjectiveError.GROUPMISSING);
1278 return Collections.emptySet();
1279 }
1280 tb.deferred().group(group.id());
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001281 // check if group is empty
1282 if (gkeys.size() == 1 && gkeys.get(0).size() == 1) {
1283 log.warn("Found empty group 0x{} in dev:{} .. will retry fwd:{}",
1284 Integer.toHexString(group.id().id()), deviceId, fwd.id());
1285 emptyGroup = true;
1286 }
Saurav Das25190812016-05-27 13:54:07 -07001287 } else {
1288 log.warn("Cannot find group for nextId:{} in dev:{}. Aborting fwd:{}",
1289 fwd.nextId(), deviceId, fwd.id());
1290 fail(fwd, ObjectiveError.FLOWINSTALLATIONFAILED);
1291 return Collections.emptySet();
Saurav Das822c4e22015-10-23 10:51:11 -07001292 }
Saurav Das822c4e22015-10-23 10:51:11 -07001293 }
Charles Chancad338a2016-09-16 18:03:11 -07001294
1295 if (forTableId == MPLS_TABLE_1) {
Pier Ventre140a8942016-11-02 07:26:38 -07001296 if (mplsNextTable == MPLS_L3_TYPE_TABLE) {
Charles Chancad338a2016-09-16 18:03:11 -07001297 Ofdpa3SetMplsType setMplsType = new Ofdpa3SetMplsType(Ofdpa3MplsType.L3_PHP);
Saurav Das9c705342017-01-06 11:36:01 -08001298 // set mpls type as apply_action
1299 tb.immediate().extension(setMplsType, deviceId);
Charles Chancad338a2016-09-16 18:03:11 -07001300 }
1301 tb.transition(mplsNextTable);
1302 } else {
1303 tb.transition(ACL_TABLE);
1304 }
1305
Saurav Das822c4e22015-10-23 10:51:11 -07001306 FlowRule.Builder ruleBuilder = DefaultFlowRule.builder()
1307 .fromApp(fwd.appId())
1308 .withPriority(fwd.priority())
1309 .forDevice(deviceId)
Saurav Das8a0732e2015-11-20 15:27:53 -08001310 .withSelector(filteredSelector.build())
1311 .withTreatment(tb.build())
1312 .forTable(forTableId);
Saurav Das822c4e22015-10-23 10:51:11 -07001313
1314 if (fwd.permanent()) {
1315 ruleBuilder.makePermanent();
1316 } else {
1317 ruleBuilder.makeTemporary(fwd.timeout());
1318 }
Flavio Castroe10fa242016-01-15 12:43:51 -08001319 Collection<FlowRule> flowRuleCollection = new ArrayList<>();
1320 flowRuleCollection.add(ruleBuilder.build());
Pier Luigi3bfe32c2017-01-30 09:47:36 -08001321 if (!allowDefaultRoute) {
Pier Ventree0ae7a32016-11-23 09:57:42 -08001322 flowRuleCollection.add(
1323 defaultRoute(fwd, complementarySelector, forTableId, tb)
1324 );
Flavio Castroe10fa242016-01-15 12:43:51 -08001325 log.debug("Default rule 0.0.0.0/0 is being installed two rules");
1326 }
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001327 // XXX retrying flows may be necessary due to bug CORD-554
1328 if (emptyGroup) {
1329 executorService.schedule(new RetryFlows(fwd, flowRuleCollection),
1330 RETRY_MS, TimeUnit.MILLISECONDS);
1331 }
Flavio Castroe10fa242016-01-15 12:43:51 -08001332 return flowRuleCollection;
Saurav Das822c4e22015-10-23 10:51:11 -07001333 }
1334
Pier Luigi3bfe32c2017-01-30 09:47:36 -08001335 protected int buildIpv4Selector(TrafficSelector.Builder builderToUpdate,
1336 TrafficSelector.Builder extBuilder,
1337 ForwardingObjective fwd,
1338 boolean allowDefaultRoute) {
1339 TrafficSelector selector = fwd.selector();
1340
1341 IpPrefix ipv4Dst = ((IPCriterion) selector.getCriterion(Criterion.Type.IPV4_DST)).ip();
1342 if (ipv4Dst.isMulticast()) {
1343 if (ipv4Dst.prefixLength() != 32) {
1344 log.warn("Multicast specific forwarding objective can only be /32");
1345 fail(fwd, ObjectiveError.BADPARAMS);
1346 return -1;
1347 }
1348 VlanId assignedVlan = readVlanFromSelector(fwd.meta());
1349 if (assignedVlan == null) {
1350 log.warn("VLAN ID required by multicast specific fwd obj is missing. Abort.");
1351 fail(fwd, ObjectiveError.BADPARAMS);
1352 return -1;
1353 }
Charles Chand1172632017-03-15 17:33:09 -07001354 if (requireVlanExtensions()) {
1355 OfdpaMatchVlanVid ofdpaMatchVlanVid = new OfdpaMatchVlanVid(assignedVlan);
1356 builderToUpdate.extension(ofdpaMatchVlanVid, deviceId);
1357 } else {
1358 builderToUpdate.matchVlanId(assignedVlan);
1359 }
Pier Luigi3bfe32c2017-01-30 09:47:36 -08001360 builderToUpdate.matchEthType(Ethernet.TYPE_IPV4).matchIPDst(ipv4Dst);
1361 log.debug("processing IPv4 multicast specific forwarding objective {} -> next:{}"
1362 + " in dev:{}", fwd.id(), fwd.nextId(), deviceId);
1363 } else {
1364 if (ipv4Dst.prefixLength() == 0) {
1365 if (allowDefaultRoute) {
1366 // The entire IPV4_DST field is wildcarded intentionally
1367 builderToUpdate.matchEthType(Ethernet.TYPE_IPV4);
1368 } else {
1369 // NOTE: The switch does not support matching 0.0.0.0/0
1370 // Split it into 0.0.0.0/1 and 128.0.0.0/1
1371 builderToUpdate.matchEthType(Ethernet.TYPE_IPV4)
1372 .matchIPDst(IpPrefix.valueOf("0.0.0.0/1"));
1373 extBuilder.matchEthType(Ethernet.TYPE_IPV4)
1374 .matchIPDst(IpPrefix.valueOf("128.0.0.0/1"));
1375 }
1376 } else {
1377 builderToUpdate.matchEthType(Ethernet.TYPE_IPV4).matchIPDst(ipv4Dst);
1378 }
1379 log.debug("processing IPv4 unicast specific forwarding objective {} -> next:{}"
1380 + " in dev:{}", fwd.id(), fwd.nextId(), deviceId);
1381 }
1382 return 0;
1383 }
1384
1385 /**
1386 * Helper method to build Ipv6 selector using the selector provided by
1387 * a forwarding objective.
1388 *
1389 * @param builderToUpdate the builder to update
1390 * @param fwd the selector to read
1391 * @return 0 if the update ends correctly. -1 if the matches
1392 * are not yet supported
1393 */
1394 protected int buildIpv6Selector(TrafficSelector.Builder builderToUpdate,
1395 ForwardingObjective fwd) {
1396
1397 TrafficSelector selector = fwd.selector();
1398
1399 IpPrefix ipv6Dst = ((IPCriterion) selector.getCriterion(Criterion.Type.IPV6_DST)).ip();
1400 if (ipv6Dst.isMulticast()) {
Julia Ferguson65428c32017-08-10 18:15:24 +00001401 if (ipv6Dst.prefixLength() != IpAddress.INET6_BIT_LENGTH) {
1402 log.warn("Multicast specific forwarding objective can only be /128");
1403 fail(fwd, ObjectiveError.BADPARAMS);
1404 return -1;
1405 }
1406 VlanId assignedVlan = readVlanFromSelector(fwd.meta());
1407 if (assignedVlan == null) {
1408 log.warn("VLAN ID required by multicast specific fwd obj is missing. Abort.");
1409 fail(fwd, ObjectiveError.BADPARAMS);
1410 return -1;
1411 }
1412 if (requireVlanExtensions()) {
1413 OfdpaMatchVlanVid ofdpaMatchVlanVid = new OfdpaMatchVlanVid(assignedVlan);
1414 builderToUpdate.extension(ofdpaMatchVlanVid, deviceId);
1415 } else {
1416 builderToUpdate.matchVlanId(assignedVlan);
1417 }
1418 builderToUpdate.matchEthType(Ethernet.TYPE_IPV6).matchIPv6Dst(ipv6Dst);
1419 log.debug("processing IPv6 multicast specific forwarding objective {} -> next:{}"
1420 + " in dev:{}", fwd.id(), fwd.nextId(), deviceId);
1421 } else {
1422 if (ipv6Dst.prefixLength() != 0) {
1423 builderToUpdate.matchIPv6Dst(ipv6Dst);
1424 }
Pier Luigi3bfe32c2017-01-30 09:47:36 -08001425 builderToUpdate.matchEthType(Ethernet.TYPE_IPV6);
1426 log.debug("processing IPv6 unicast specific forwarding objective {} -> next:{}"
1427 + " in dev:{}", fwd.id(), fwd.nextId(), deviceId);
Julia Ferguson65428c32017-08-10 18:15:24 +00001428 }
Pier Luigi3bfe32c2017-01-30 09:47:36 -08001429 return 0;
1430 }
1431
Pier Ventree0ae7a32016-11-23 09:57:42 -08001432 protected FlowRule defaultRoute(ForwardingObjective fwd,
1433 TrafficSelector.Builder complementarySelector,
1434 int forTableId,
1435 TrafficTreatment.Builder tb) {
1436 FlowRule.Builder rule = DefaultFlowRule.builder()
1437 .fromApp(fwd.appId())
1438 .withPriority(fwd.priority())
1439 .forDevice(deviceId)
1440 .withSelector(complementarySelector.build())
1441 .withTreatment(tb.build())
1442 .forTable(forTableId);
1443 if (fwd.permanent()) {
1444 rule.makePermanent();
1445 } else {
1446 rule.makeTemporary(fwd.timeout());
1447 }
1448 return rule.build();
1449 }
1450
Saurav Das4ce45962015-11-24 23:21:05 -08001451 /**
1452 * Handles forwarding rules to the L2 bridging table. Flow actions are not
1453 * allowed in the bridging table - instead we use L2 Interface group or
1454 * L2 flood group
1455 *
1456 * @param fwd the forwarding objective
1457 * @return A collection of flow rules, or an empty set
1458 */
1459 protected Collection<FlowRule> processEthDstSpecific(ForwardingObjective fwd) {
1460 List<FlowRule> rules = new ArrayList<>();
1461
1462 // Build filtered selector
1463 TrafficSelector selector = fwd.selector();
1464 EthCriterion ethCriterion = (EthCriterion) selector
1465 .getCriterion(Criterion.Type.ETH_DST);
1466 VlanIdCriterion vlanIdCriterion = (VlanIdCriterion) selector
1467 .getCriterion(Criterion.Type.VLAN_VID);
1468
1469 if (vlanIdCriterion == null) {
1470 log.warn("Forwarding objective for bridging requires vlan. Not "
1471 + "installing fwd:{} in dev:{}", fwd.id(), deviceId);
1472 fail(fwd, ObjectiveError.BADPARAMS);
1473 return Collections.emptySet();
1474 }
1475
1476 TrafficSelector.Builder filteredSelectorBuilder =
1477 DefaultTrafficSelector.builder();
Yi Tseng3a77b4f2017-02-06 15:02:17 -08001478
1479 if (!ethCriterion.mac().equals(NONE) &&
1480 !ethCriterion.mac().equals(BROADCAST)) {
Saurav Das4ce45962015-11-24 23:21:05 -08001481 filteredSelectorBuilder.matchEthDst(ethCriterion.mac());
1482 log.debug("processing L2 forwarding objective:{} -> next:{} in dev:{}",
1483 fwd.id(), fwd.nextId(), deviceId);
1484 } else {
Yi Tseng3a77b4f2017-02-06 15:02:17 -08001485 // Use wildcard DST_MAC if the MacAddress is None or Broadcast
Saurav Das4ce45962015-11-24 23:21:05 -08001486 log.debug("processing L2 Broadcast forwarding objective:{} -> next:{} "
1487 + "in dev:{} for vlan:{}",
1488 fwd.id(), fwd.nextId(), deviceId, vlanIdCriterion.vlanId());
1489 }
Charles Chand1172632017-03-15 17:33:09 -07001490 if (requireVlanExtensions()) {
1491 OfdpaMatchVlanVid ofdpaMatchVlanVid = new OfdpaMatchVlanVid(vlanIdCriterion.vlanId());
1492 filteredSelectorBuilder.extension(ofdpaMatchVlanVid, deviceId);
1493 } else {
1494 filteredSelectorBuilder.matchVlanId(vlanIdCriterion.vlanId());
1495 }
Saurav Das4ce45962015-11-24 23:21:05 -08001496 TrafficSelector filteredSelector = filteredSelectorBuilder.build();
1497
1498 if (fwd.treatment() != null) {
1499 log.warn("Ignoring traffic treatment in fwd rule {} meant for L2 table"
1500 + "for dev:{}. Expecting only nextId", fwd.id(), deviceId);
1501 }
1502
1503 TrafficTreatment.Builder treatmentBuilder = DefaultTrafficTreatment.builder();
1504 if (fwd.nextId() != null) {
Saurav Das423fe2b2015-12-04 10:52:59 -08001505 NextGroup next = getGroupForNextObjective(fwd.nextId());
Saurav Das4ce45962015-11-24 23:21:05 -08001506 if (next != null) {
1507 List<Deque<GroupKey>> gkeys = appKryo.deserialize(next.data());
1508 // we only need the top level group's key to point the flow to it
1509 Group group = groupService.getGroup(deviceId, gkeys.get(0).peekFirst());
1510 if (group != null) {
1511 treatmentBuilder.deferred().group(group.id());
1512 } else {
1513 log.warn("Group with key:{} for next-id:{} not found in dev:{}",
1514 gkeys.get(0).peekFirst(), fwd.nextId(), deviceId);
1515 fail(fwd, ObjectiveError.GROUPMISSING);
1516 return Collections.emptySet();
1517 }
1518 }
1519 }
1520 treatmentBuilder.immediate().transition(ACL_TABLE);
1521 TrafficTreatment filteredTreatment = treatmentBuilder.build();
1522
1523 // Build bridging table entries
1524 FlowRule.Builder flowRuleBuilder = DefaultFlowRule.builder();
1525 flowRuleBuilder.fromApp(fwd.appId())
1526 .withPriority(fwd.priority())
1527 .forDevice(deviceId)
1528 .withSelector(filteredSelector)
1529 .withTreatment(filteredTreatment)
1530 .forTable(BRIDGING_TABLE);
1531 if (fwd.permanent()) {
1532 flowRuleBuilder.makePermanent();
1533 } else {
1534 flowRuleBuilder.makeTemporary(fwd.timeout());
1535 }
1536 rules.add(flowRuleBuilder.build());
1537 return rules;
1538 }
1539
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001540 //////////////////////////////////////
1541 // Helper Methods and Classes
1542 //////////////////////////////////////
1543
1544 private boolean isSupportedEthTypeObjective(ForwardingObjective fwd) {
1545 TrafficSelector selector = fwd.selector();
1546 EthTypeCriterion ethType = (EthTypeCriterion) selector
1547 .getCriterion(Criterion.Type.ETH_TYPE);
1548 return !((ethType == null) ||
1549 ((ethType.ethType().toShort() != Ethernet.TYPE_IPV4) &&
Pier Ventree0ae7a32016-11-23 09:57:42 -08001550 (ethType.ethType().toShort() != Ethernet.MPLS_UNICAST)) &&
1551 (ethType.ethType().toShort() != Ethernet.TYPE_IPV6));
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001552 }
1553
1554 private boolean isSupportedEthDstObjective(ForwardingObjective fwd) {
1555 TrafficSelector selector = fwd.selector();
1556 EthCriterion ethDst = (EthCriterion) selector
1557 .getCriterion(Criterion.Type.ETH_DST);
1558 VlanIdCriterion vlanId = (VlanIdCriterion) selector
1559 .getCriterion(Criterion.Type.VLAN_VID);
1560 return !(ethDst == null && vlanId == null);
1561 }
1562
Saurav Das423fe2b2015-12-04 10:52:59 -08001563 protected NextGroup getGroupForNextObjective(Integer nextId) {
1564 NextGroup next = flowObjectiveStore.getNextGroup(nextId);
1565 if (next != null) {
1566 List<Deque<GroupKey>> gkeys = appKryo.deserialize(next.data());
1567 if (gkeys != null && !gkeys.isEmpty()) {
1568 return next;
1569 } else {
1570 log.warn("Empty next group found in FlowObjective store for "
1571 + "next-id:{} in dev:{}", nextId, deviceId);
1572 }
1573 } else {
1574 log.warn("next-id {} not found in Flow objective store for dev:{}",
1575 nextId, deviceId);
1576 }
1577 return null;
1578 }
1579
Charles Chan188ebf52015-12-23 00:15:11 -08001580 protected static void pass(Objective obj) {
Sho SHIMIZUef7e2902016-02-12 18:38:29 -08001581 obj.context().ifPresent(context -> context.onSuccess(obj));
Saurav Das822c4e22015-10-23 10:51:11 -07001582 }
1583
Charles Chan188ebf52015-12-23 00:15:11 -08001584 protected static void fail(Objective obj, ObjectiveError error) {
Sho SHIMIZUef7e2902016-02-12 18:38:29 -08001585 obj.context().ifPresent(context -> context.onError(obj, error));
Saurav Das822c4e22015-10-23 10:51:11 -07001586 }
Saurav Das24431192016-03-07 19:13:00 -08001587
Saurav Das24431192016-03-07 19:13:00 -08001588 @Override
1589 public List<String> getNextMappings(NextGroup nextGroup) {
1590 List<String> mappings = new ArrayList<>();
1591 List<Deque<GroupKey>> gkeys = appKryo.deserialize(nextGroup.data());
1592 for (Deque<GroupKey> gkd : gkeys) {
1593 Group lastGroup = null;
Yuta HIGUCHI2dce08a2017-04-20 21:57:48 -07001594 StringBuilder gchain = new StringBuilder();
Saurav Das24431192016-03-07 19:13:00 -08001595 for (GroupKey gk : gkd) {
1596 Group g = groupService.getGroup(deviceId, gk);
Saurav Das8be4e3a2016-03-11 17:19:07 -08001597 if (g == null) {
Saurav Das25190812016-05-27 13:54:07 -07001598 gchain.append(" NoGrp").append(" -->");
Saurav Das8be4e3a2016-03-11 17:19:07 -08001599 continue;
1600 }
1601 gchain.append(" 0x").append(Integer.toHexString(g.id().id()))
1602 .append(" -->");
Saurav Das24431192016-03-07 19:13:00 -08001603 lastGroup = g;
1604 }
1605 // add port information for last group in group-chain
Yuta HIGUCHI2dce08a2017-04-20 21:57:48 -07001606 List<Instruction> lastGroupIns = new ArrayList<>();
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001607 if (lastGroup != null && !lastGroup.buckets().buckets().isEmpty()) {
Saurav Das25190812016-05-27 13:54:07 -07001608 lastGroupIns = lastGroup.buckets().buckets().get(0)
1609 .treatment().allInstructions();
1610 }
1611 for (Instruction i: lastGroupIns) {
Saurav Das24431192016-03-07 19:13:00 -08001612 if (i instanceof OutputInstruction) {
Saurav Das8be4e3a2016-03-11 17:19:07 -08001613 gchain.append(" port:").append(((OutputInstruction) i).port());
Saurav Das24431192016-03-07 19:13:00 -08001614 }
1615 }
Saurav Das8be4e3a2016-03-11 17:19:07 -08001616 mappings.add(gchain.toString());
Saurav Das24431192016-03-07 19:13:00 -08001617 }
1618 return mappings;
1619 }
Charles Chan5b9df8d2016-03-28 22:21:40 -07001620
Pier Ventre140a8942016-11-02 07:26:38 -07001621 static boolean isMplsBos(TrafficSelector selector) {
1622 MplsBosCriterion bosCriterion = (MplsBosCriterion) selector.getCriterion(MPLS_BOS);
1623 return bosCriterion != null && bosCriterion.mplsBos();
1624 }
1625
1626 static boolean isNotMplsBos(TrafficSelector selector) {
1627 MplsBosCriterion bosCriterion = (MplsBosCriterion) selector.getCriterion(MPLS_BOS);
1628 return bosCriterion != null && !bosCriterion.mplsBos();
1629 }
1630
Charles Chand9e47c62017-10-05 15:17:15 -07001631 private static boolean isIpv6(TrafficSelector selector) {
1632 EthTypeCriterion ethTypeCriterion = (EthTypeCriterion) selector.getCriterion(ETH_TYPE);
1633 return ethTypeCriterion != null && ethTypeCriterion.ethType().toShort() == Ethernet.TYPE_IPV6;
1634 }
1635
Alex Yashchuk4caa8e82017-12-08 17:40:05 +02001636 public static VlanId readVlanFromSelector(TrafficSelector selector) {
Saurav Das59232cf2016-04-27 18:35:50 -07001637 if (selector == null) {
1638 return null;
1639 }
Charles Chan5b9df8d2016-03-28 22:21:40 -07001640 Criterion criterion = selector.getCriterion(Criterion.Type.VLAN_VID);
1641 return (criterion == null)
1642 ? null : ((VlanIdCriterion) criterion).vlanId();
1643 }
1644
Charles Chand9e47c62017-10-05 15:17:15 -07001645 static IpPrefix readIpDstFromSelector(TrafficSelector selector) {
Saurav Das59232cf2016-04-27 18:35:50 -07001646 if (selector == null) {
1647 return null;
1648 }
Charles Chan5b9df8d2016-03-28 22:21:40 -07001649 Criterion criterion = selector.getCriterion(Criterion.Type.IPV4_DST);
1650 return (criterion == null) ? null : ((IPCriterion) criterion).ip();
1651 }
Charles Chand55e84d2016-03-30 17:54:24 -07001652
1653 private static VlanId readVlanFromTreatment(TrafficTreatment treatment) {
Saurav Das59232cf2016-04-27 18:35:50 -07001654 if (treatment == null) {
1655 return null;
1656 }
Charles Chand55e84d2016-03-30 17:54:24 -07001657 for (Instruction i : treatment.allInstructions()) {
1658 if (i instanceof ModVlanIdInstruction) {
1659 return ((ModVlanIdInstruction) i).vlanId();
1660 }
1661 }
1662 return null;
1663 }
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001664
1665 /**
1666 * Utility class that retries sending flows a fixed number of times, even if
1667 * some of the attempts are successful. Used only for forwarding objectives.
1668 */
Alex Yashchuk4caa8e82017-12-08 17:40:05 +02001669 public final class RetryFlows implements Runnable {
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001670 int attempts = MAX_RETRY_ATTEMPTS;
1671 private Collection<FlowRule> retryFlows;
1672 private ForwardingObjective fwd;
1673
Alex Yashchuk4caa8e82017-12-08 17:40:05 +02001674 public RetryFlows(ForwardingObjective fwd, Collection<FlowRule> retryFlows) {
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001675 this.fwd = fwd;
1676 this.retryFlows = retryFlows;
1677 }
1678
1679 @Override
1680 public void run() {
1681 log.info("RETRY FLOWS ATTEMPT# {} for fwd:{} rules:{}",
1682 MAX_RETRY_ATTEMPTS - attempts, fwd.id(), retryFlows.size());
1683 sendForward(fwd, retryFlows);
1684 if (--attempts > 0) {
1685 executorService.schedule(this, RETRY_MS, TimeUnit.MILLISECONDS);
1686 }
1687 }
1688 }
1689
Saurav Das822c4e22015-10-23 10:51:11 -07001690}