blob: 661d3e99ecb8ab5df32fa667bb7d265998cf54f2 [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 Luigi075f1012018-02-01 10:23:12 +0100103import static org.onosproject.net.group.GroupDescription.Type.SELECT;
Pier Ventree0ae7a32016-11-23 09:57:42 -0800104import static org.slf4j.LoggerFactory.getLogger;
Pier Ventre140a8942016-11-02 07:26:38 -0700105import static org.onosproject.net.flow.criteria.Criterion.Type.MPLS_BOS;
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 Dasc568c342018-01-25 09:49:01 -0800255 /**
256 * Determines whether this driver should continue to retry flows that point
257 * to empty groups. See CORD-554.
258 *
259 * @return true if the driver should retry flows
260 */
261 protected boolean shouldRetry() {
262 return true;
263 }
264
Saurav Das822c4e22015-10-23 10:51:11 -0700265 //////////////////////////////////////
266 // Flow Objectives
267 //////////////////////////////////////
268
269 @Override
270 public void filter(FilteringObjective filteringObjective) {
271 if (filteringObjective.type() == FilteringObjective.Type.PERMIT) {
272 processFilter(filteringObjective,
273 filteringObjective.op() == Objective.Operation.ADD,
274 filteringObjective.appId());
275 } else {
276 // Note that packets that don't match the PERMIT filter are
277 // automatically denied. The DENY filter is used to deny packets
278 // that are otherwise permitted by the PERMIT filter.
279 // Use ACL table flow rules here for DENY filtering objectives
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530280 log.warn("filter objective other than PERMIT currently not supported");
Saurav Das822c4e22015-10-23 10:51:11 -0700281 fail(filteringObjective, ObjectiveError.UNSUPPORTED);
282 }
283 }
284
285 @Override
286 public void forward(ForwardingObjective fwd) {
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700287 Collection<FlowRule> rules = processForward(fwd);
Saurav Das25190812016-05-27 13:54:07 -0700288 if (rules == null || rules.isEmpty()) {
289 // Assumes fail message has already been generated to the objective
290 // context. Returning here prevents spurious pass message to be
291 // generated by FlowRule service for empty flowOps.
292 return;
293 }
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700294 sendForward(fwd, rules);
295 }
296
297 protected void sendForward(ForwardingObjective fwd, Collection<FlowRule> rules) {
298 FlowRuleOperations.Builder flowOpsBuilder = FlowRuleOperations.builder();
Saurav Das822c4e22015-10-23 10:51:11 -0700299 switch (fwd.op()) {
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700300 case ADD:
301 rules.stream()
302 .filter(Objects::nonNull)
303 .forEach(flowOpsBuilder::add);
Saurav Dasd2fded02016-12-02 15:43:47 -0800304 log.debug("Applying a add fwd-obj {} to sw:{}", fwd.id(), deviceId);
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700305 break;
306 case REMOVE:
307 rules.stream()
308 .filter(Objects::nonNull)
309 .forEach(flowOpsBuilder::remove);
Pier Ventre42287df2016-11-09 14:17:26 -0800310 log.debug("Deleting a flow rule to sw:{}", deviceId);
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700311 break;
312 default:
313 fail(fwd, ObjectiveError.UNKNOWN);
314 log.warn("Unknown forwarding type {}", fwd.op());
Saurav Das822c4e22015-10-23 10:51:11 -0700315 }
316
Saurav Das822c4e22015-10-23 10:51:11 -0700317 flowRuleService.apply(flowOpsBuilder.build(new FlowRuleOperationsContext() {
318 @Override
319 public void onSuccess(FlowRuleOperations ops) {
320 pass(fwd);
321 }
322
323 @Override
324 public void onError(FlowRuleOperations ops) {
325 fail(fwd, ObjectiveError.FLOWINSTALLATIONFAILED);
326 }
327 }));
Saurav Das822c4e22015-10-23 10:51:11 -0700328 }
329
330 @Override
331 public void next(NextObjective nextObjective) {
Saurav Das8a0732e2015-11-20 15:27:53 -0800332 NextGroup nextGroup = flowObjectiveStore.getNextGroup(nextObjective.id());
333 switch (nextObjective.op()) {
334 case ADD:
Saurav Das4f980082015-11-05 13:39:15 -0800335 if (nextGroup != null) {
Saurav Das8a0732e2015-11-20 15:27:53 -0800336 log.warn("Cannot add next {} that already exists in device {}",
337 nextObjective.id(), deviceId);
338 return;
339 }
Saurav Das1547b3f2017-05-05 17:01:08 -0700340 log.debug("Processing NextObjective id {} in dev {} - add group",
Saurav Das8a0732e2015-11-20 15:27:53 -0800341 nextObjective.id(), deviceId);
Charles Chan425854b2016-04-11 15:32:12 -0700342 groupHandler.addGroup(nextObjective);
Saurav Das8a0732e2015-11-20 15:27:53 -0800343 break;
344 case ADD_TO_EXISTING:
345 if (nextGroup != null) {
Saurav Das1547b3f2017-05-05 17:01:08 -0700346 log.debug("Processing NextObjective id {} in dev {} - add bucket",
Saurav Das8a0732e2015-11-20 15:27:53 -0800347 nextObjective.id(), deviceId);
Charles Chan425854b2016-04-11 15:32:12 -0700348 groupHandler.addBucketToGroup(nextObjective, nextGroup);
Saurav Das4f980082015-11-05 13:39:15 -0800349 } else {
Saurav Das8a0732e2015-11-20 15:27:53 -0800350 // it is possible that group-chain has not been fully created yet
Saurav Das423fe2b2015-12-04 10:52:59 -0800351 log.debug("Waiting to add bucket to group for next-id:{} in dev:{}",
352 nextObjective.id(), deviceId);
Yi Tseng47f82dc2017-03-05 22:48:39 -0800353
354 // by design multiple pending bucket is allowed for the group
355 groupHandler.pendingBuckets.compute(nextObjective.id(), (nextId, pendBkts) -> {
356 if (pendBkts == null) {
357 pendBkts = Sets.newHashSet();
358 }
359 pendBkts.add(nextObjective);
360 return pendBkts;
361 });
Saurav Das4f980082015-11-05 13:39:15 -0800362 }
Saurav Das8a0732e2015-11-20 15:27:53 -0800363 break;
364 case REMOVE:
365 if (nextGroup == null) {
366 log.warn("Cannot remove 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 group",
Saurav Das8a0732e2015-11-20 15:27:53 -0800371 nextObjective.id(), deviceId);
Charles Chan425854b2016-04-11 15:32:12 -0700372 groupHandler.removeGroup(nextObjective, nextGroup);
Saurav Das8a0732e2015-11-20 15:27:53 -0800373 break;
374 case REMOVE_FROM_EXISTING:
375 if (nextGroup == null) {
376 log.warn("Cannot remove from next {} that does not exist in device {}",
377 nextObjective.id(), deviceId);
378 return;
379 }
Saurav Das1547b3f2017-05-05 17:01:08 -0700380 log.debug("Processing NextObjective id {} in dev {} - remove bucket",
Saurav Das8a0732e2015-11-20 15:27:53 -0800381 nextObjective.id(), deviceId);
Charles Chan425854b2016-04-11 15:32:12 -0700382 groupHandler.removeBucketFromGroup(nextObjective, nextGroup);
Saurav Das8a0732e2015-11-20 15:27:53 -0800383 break;
Saurav Dasceccf242017-08-03 18:30:35 -0700384 case VERIFY:
385 if (nextGroup == null) {
386 log.warn("Cannot verify next {} that does not exist in device {}",
387 nextObjective.id(), deviceId);
388 return;
389 }
390 log.debug("Processing NextObjective id {} in dev {} - verify",
391 nextObjective.id(), deviceId);
392 groupHandler.verifyGroup(nextObjective, nextGroup);
393 break;
Saurav Das8a0732e2015-11-20 15:27:53 -0800394 default:
Saurav Das4f980082015-11-05 13:39:15 -0800395 log.warn("Unsupported operation {}", nextObjective.op());
Saurav Das822c4e22015-10-23 10:51:11 -0700396 }
397 }
398
399 //////////////////////////////////////
400 // Flow handling
401 //////////////////////////////////////
402
403 /**
Yi Tseng0fdc07e2017-04-24 19:17:08 -0700404 * As per OFDPA 2.0 TTP, filtering of VLAN ids and MAC addresses (for routing)
405 * configured on switch ports happen in different tables.
Saurav Das822c4e22015-10-23 10:51:11 -0700406 *
407 * @param filt the filtering objective
408 * @param install indicates whether to add or remove the objective
409 * @param applicationId the application that sent this objective
410 */
Saurav Das52025962016-01-28 22:30:01 -0800411 protected void processFilter(FilteringObjective filt,
412 boolean install, ApplicationId applicationId) {
Saurav Das822c4e22015-10-23 10:51:11 -0700413 // This driver only processes filtering criteria defined with switch
414 // ports as the key
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530415 PortCriterion portCriterion = null;
Saurav Das822c4e22015-10-23 10:51:11 -0700416 EthCriterion ethCriterion = null;
417 VlanIdCriterion vidCriterion = null;
Saurav Das822c4e22015-10-23 10:51:11 -0700418 if (!filt.key().equals(Criteria.dummy()) &&
419 filt.key().type() == Criterion.Type.IN_PORT) {
420 portCriterion = (PortCriterion) filt.key();
Saurav Das822c4e22015-10-23 10:51:11 -0700421 }
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530422 if (portCriterion == null) {
423 log.debug("No IN_PORT defined in filtering objective from app: {}",
424 applicationId);
425 } else {
426 log.debug("Received filtering objective for dev/port: {}/{}", deviceId,
427 portCriterion.port());
428 }
Saurav Das822c4e22015-10-23 10:51:11 -0700429 // convert filtering conditions for switch-intfs into flowrules
430 FlowRuleOperations.Builder ops = FlowRuleOperations.builder();
431 for (Criterion criterion : filt.conditions()) {
Yi Tseng0fdc07e2017-04-24 19:17:08 -0700432 switch (criterion.type()) {
433 case ETH_DST:
434 case ETH_DST_MASKED:
435 ethCriterion = (EthCriterion) criterion;
436 break;
437 case VLAN_VID:
438 vidCriterion = (VlanIdCriterion) criterion;
439 break;
440 default:
441 log.warn("Unsupported filter {}", criterion);
442 fail(filt, ObjectiveError.UNSUPPORTED);
443 return;
Saurav Das822c4e22015-10-23 10:51:11 -0700444 }
445 }
446
Saurav Das0e99e2b2015-10-28 12:39:42 -0700447 VlanId assignedVlan = null;
Charles Chane849c192016-01-11 18:28:54 -0800448 if (vidCriterion != null) {
Charles Chand55e84d2016-03-30 17:54:24 -0700449 // Use the VLAN in criterion if metadata is not present and the traffic is tagged
Charles Chanc550f2e2017-12-19 19:55:57 -0800450 if (!vidCriterion.vlanId().equals(VlanId.NONE)) {
Charles Chane849c192016-01-11 18:28:54 -0800451 assignedVlan = vidCriterion.vlanId();
Charles Chanc550f2e2017-12-19 19:55:57 -0800452 } else if (filt.meta() != null) {
453 assignedVlan = readVlanFromTreatment(filt.meta());
Charles Chand55e84d2016-03-30 17:54:24 -0700454 }
Charles Chane849c192016-01-11 18:28:54 -0800455
Charles Chand55e84d2016-03-30 17:54:24 -0700456 if (assignedVlan == null) {
457 log.error("Driver fails to extract VLAN information. "
Frank Wangd8ab0962017-08-11 11:09:30 +0800458 + "Not processing VLAN filters on device {}.", deviceId);
Charles Chand55e84d2016-03-30 17:54:24 -0700459 log.debug("VLAN ID in criterion={}, metadata={}",
460 readVlanFromTreatment(filt.meta()), vidCriterion.vlanId());
461 fail(filt, ObjectiveError.BADPARAMS);
462 return;
Saurav Das0e99e2b2015-10-28 12:39:42 -0700463 }
464 }
465
Yi Tseng3a77b4f2017-02-06 15:02:17 -0800466 if (ethCriterion == null || ethCriterion.mac().equals(NONE)) {
Charles Chan985b12e2016-05-11 19:47:22 -0700467 // NOTE: it is possible that a filtering objective only has vidCriterion
Saurav Das961beb22017-03-29 19:09:17 -0700468 log.warn("filtering objective missing dstMac, cannot program TMAC table");
Saurav Das822c4e22015-10-23 10:51:11 -0700469 } else {
470 for (FlowRule tmacRule : processEthDstFilter(portCriterion, ethCriterion,
Saurav Das0e99e2b2015-10-28 12:39:42 -0700471 vidCriterion, assignedVlan,
472 applicationId)) {
Saurav Das961beb22017-03-29 19:09:17 -0700473 log.trace("{} MAC filtering rules in TMAC table: {} for dev: {}",
Saurav Das018605f2017-02-18 14:05:44 -0800474 (install) ? "adding" : "removing", tmacRule, deviceId);
Charles Chanc550f2e2017-12-19 19:55:57 -0800475
476 if (install) {
477 ops = ops.add(tmacRule);
478 } else {
479 // NOTE: Only remove TMAC flow when there is no more enabled port within the
480 // same VLAN on this device if TMAC doesn't support matching on in_port.
481 if (matchInPortTmacTable() || (filt.meta() != null && filt.meta().clearedDeferred())) {
482 ops = ops.remove(tmacRule);
483 } else {
484 log.debug("Abort TMAC flow removal on {}. Some other ports still share this TMAC flow");
485 }
486 }
Saurav Das822c4e22015-10-23 10:51:11 -0700487 }
488 }
489
Charles Chan985b12e2016-05-11 19:47:22 -0700490 if (vidCriterion == null) {
491 // NOTE: it is possible that a filtering objective only has ethCriterion
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530492 log.info("filtering objective missing VLAN, cannot program VLAN Table");
Saurav Das822c4e22015-10-23 10:51:11 -0700493 } else {
Charles Chan14967c22015-12-07 11:11:50 -0800494 /*
495 * NOTE: Separate vlan filtering rules and assignment rules
496 * into different stage in order to guarantee that filtering rules
Saurav Das52025962016-01-28 22:30:01 -0800497 * always go first, as required by ofdpa.
Charles Chan14967c22015-12-07 11:11:50 -0800498 */
499 List<FlowRule> allRules = processVlanIdFilter(
500 portCriterion, vidCriterion, assignedVlan, applicationId);
501 List<FlowRule> filteringRules = new ArrayList<>();
502 List<FlowRule> assignmentRules = new ArrayList<>();
503
504 allRules.forEach(flowRule -> {
Charles Chand1172632017-03-15 17:33:09 -0700505 VlanId vlanId;
506 if (requireVlanExtensions()) {
507 ExtensionCriterion extCriterion =
508 (ExtensionCriterion) flowRule.selector().getCriterion(Criterion.Type.EXTENSION);
509 vlanId = ((OfdpaMatchVlanVid) extCriterion.extensionSelector()).vlanId();
510 } else {
511 VlanIdCriterion vlanIdCriterion =
512 (VlanIdCriterion) flowRule.selector().getCriterion(Criterion.Type.VLAN_VID);
513 vlanId = vlanIdCriterion.vlanId();
514 }
Charles Chanbe8aea42016-02-24 12:04:47 -0800515 if (!vlanId.equals(VlanId.NONE)) {
Charles Chan14967c22015-12-07 11:11:50 -0800516 filteringRules.add(flowRule);
517 } else {
518 assignmentRules.add(flowRule);
519 }
520 });
521
522 for (FlowRule filteringRule : filteringRules) {
Saurav Das961beb22017-03-29 19:09:17 -0700523 log.trace("{} VLAN filtering rule in VLAN table: {} for dev: {}",
Saurav Das018605f2017-02-18 14:05:44 -0800524 (install) ? "adding" : "removing", filteringRule, deviceId);
Charles Chan14967c22015-12-07 11:11:50 -0800525 ops = install ? ops.add(filteringRule) : ops.remove(filteringRule);
526 }
527
528 ops.newStage();
529
530 for (FlowRule assignmentRule : assignmentRules) {
Saurav Das961beb22017-03-29 19:09:17 -0700531 log.trace("{} VLAN assignment rule in VLAN table: {} for dev: {}",
Saurav Das018605f2017-02-18 14:05:44 -0800532 (install) ? "adding" : "removing", assignmentRule, deviceId);
Charles Chan14967c22015-12-07 11:11:50 -0800533 ops = install ? ops.add(assignmentRule) : ops.remove(assignmentRule);
Saurav Das822c4e22015-10-23 10:51:11 -0700534 }
535 }
536
Saurav Das822c4e22015-10-23 10:51:11 -0700537 // apply filtering flow rules
538 flowRuleService.apply(ops.build(new FlowRuleOperationsContext() {
539 @Override
540 public void onSuccess(FlowRuleOperations ops) {
Saurav Das018605f2017-02-18 14:05:44 -0800541 log.debug("Applied {} filtering rules in device {}",
Saurav Das822c4e22015-10-23 10:51:11 -0700542 ops.stages().get(0).size(), deviceId);
543 pass(filt);
544 }
545
546 @Override
547 public void onError(FlowRuleOperations ops) {
548 log.info("Failed to apply all filtering rules in dev {}", deviceId);
549 fail(filt, ObjectiveError.FLOWINSTALLATIONFAILED);
550 }
551 }));
552
553 }
554
555 /**
Charles Chand1172632017-03-15 17:33:09 -0700556 * Internal implementation of processVlanIdFilter.
557 * <p>
558 * The is_present bit in set_vlan_vid action is required to be 0 in OFDPA i12.
559 * Since it is non-OF spec, we need an extension treatment for that.
560 * The useVlanExtension must be set to false for OFDPA i12.
561 * </p>
Saurav Das0e99e2b2015-10-28 12:39:42 -0700562 *
Charles Chanf9e98652016-09-07 16:54:23 -0700563 * @param portCriterion port on device for which this filter is programmed
564 * @param vidCriterion vlan assigned to port, or NONE for untagged
565 * @param assignedVlan assigned vlan-id for untagged packets
566 * @param applicationId for application programming this filter
Saurav Das822c4e22015-10-23 10:51:11 -0700567 * @return list of FlowRule for port-vlan filters
568 */
569 protected List<FlowRule> processVlanIdFilter(PortCriterion portCriterion,
Yi Tsengef19de12017-04-24 11:33:05 -0700570 VlanIdCriterion vidCriterion,
571 VlanId assignedVlan,
572 ApplicationId applicationId) {
Charles Chan14967c22015-12-07 11:11:50 -0800573 List<FlowRule> rules = new ArrayList<>();
Saurav Das822c4e22015-10-23 10:51:11 -0700574 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
575 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
Charles Chan14967c22015-12-07 11:11:50 -0800576 TrafficSelector.Builder preSelector = null;
577 TrafficTreatment.Builder preTreatment = null;
578
Saurav Das4f980082015-11-05 13:39:15 -0800579 treatment.transition(TMAC_TABLE);
580
Saurav Das822c4e22015-10-23 10:51:11 -0700581 if (vidCriterion.vlanId() == VlanId.NONE) {
582 // untagged packets are assigned vlans
Charles Chand1172632017-03-15 17:33:09 -0700583 preSelector = DefaultTrafficSelector.builder();
584 if (requireVlanExtensions()) {
585 OfdpaMatchVlanVid ofdpaMatchVlanVid = new OfdpaMatchVlanVid(VlanId.NONE);
586 selector.extension(ofdpaMatchVlanVid, deviceId);
Charles Chanf9e98652016-09-07 16:54:23 -0700587 OfdpaSetVlanVid ofdpaSetVlanVid = new OfdpaSetVlanVid(assignedVlan);
588 treatment.extension(ofdpaSetVlanVid, deviceId);
Charles Chand1172632017-03-15 17:33:09 -0700589
590 OfdpaMatchVlanVid preOfdpaMatchVlanVid = new OfdpaMatchVlanVid(assignedVlan);
591 preSelector.extension(preOfdpaMatchVlanVid, deviceId);
Charles Chanf9e98652016-09-07 16:54:23 -0700592 } else {
Charles Chand1172632017-03-15 17:33:09 -0700593 selector.matchVlanId(VlanId.NONE);
Charles Chanf9e98652016-09-07 16:54:23 -0700594 treatment.setVlanId(assignedVlan);
Charles Chand1172632017-03-15 17:33:09 -0700595
596 preSelector.matchVlanId(assignedVlan);
597 }
598 preTreatment = DefaultTrafficTreatment.builder().transition(TMAC_TABLE);
599 } else {
600 if (requireVlanExtensions()) {
601 OfdpaMatchVlanVid ofdpaMatchVlanVid = new OfdpaMatchVlanVid(vidCriterion.vlanId());
602 selector.extension(ofdpaMatchVlanVid, deviceId);
603 } else {
604 selector.matchVlanId(vidCriterion.vlanId());
Charles Chanf9e98652016-09-07 16:54:23 -0700605 }
Charles Chan14967c22015-12-07 11:11:50 -0800606
Charles Chand55e84d2016-03-30 17:54:24 -0700607 if (!assignedVlan.equals(vidCriterion.vlanId())) {
Charles Chand1172632017-03-15 17:33:09 -0700608 if (requireVlanExtensions()) {
Charles Chanc03782d2017-01-31 13:57:55 -0800609 OfdpaSetVlanVid ofdpaSetVlanVid = new OfdpaSetVlanVid(assignedVlan);
610 treatment.extension(ofdpaSetVlanVid, deviceId);
611 } else {
612 treatment.setVlanId(assignedVlan);
613 }
Charles Chand55e84d2016-03-30 17:54:24 -0700614 }
Saurav Das822c4e22015-10-23 10:51:11 -0700615 }
Saurav Das822c4e22015-10-23 10:51:11 -0700616
617 // ofdpa cannot match on ALL portnumber, so we need to use separate
618 // rules for each port.
Charles Chan14967c22015-12-07 11:11:50 -0800619 List<PortNumber> portnums = new ArrayList<>();
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530620 if (portCriterion != null) {
621 if (PortNumber.ALL.equals(portCriterion.port())) {
622 for (Port port : deviceService.getPorts(deviceId)) {
623 if (port.number().toLong() > 0 && port.number().toLong() < OFPP_MAX) {
624 portnums.add(port.number());
625 }
Saurav Das822c4e22015-10-23 10:51:11 -0700626 }
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530627 } else {
628 portnums.add(portCriterion.port());
Saurav Das822c4e22015-10-23 10:51:11 -0700629 }
630 } else {
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530631 log.warn("Filtering Objective missing Port Criterion . " +
632 "VLAN Table cannot be programmed for {}",
633 deviceId);
Saurav Das822c4e22015-10-23 10:51:11 -0700634 }
Saurav Das4f980082015-11-05 13:39:15 -0800635
Saurav Das822c4e22015-10-23 10:51:11 -0700636 for (PortNumber pnum : portnums) {
Saurav Das4f980082015-11-05 13:39:15 -0800637 // create rest of flowrule
Saurav Das822c4e22015-10-23 10:51:11 -0700638 selector.matchInPort(pnum);
639 FlowRule rule = DefaultFlowRule.builder()
640 .forDevice(deviceId)
641 .withSelector(selector.build())
642 .withTreatment(treatment.build())
643 .withPriority(DEFAULT_PRIORITY)
644 .fromApp(applicationId)
645 .makePermanent()
646 .forTable(VLAN_TABLE).build();
Charles Chan14967c22015-12-07 11:11:50 -0800647
648 if (preSelector != null) {
649 preSelector.matchInPort(pnum);
650 FlowRule preRule = DefaultFlowRule.builder()
651 .forDevice(deviceId)
652 .withSelector(preSelector.build())
653 .withTreatment(preTreatment.build())
654 .withPriority(DEFAULT_PRIORITY)
655 .fromApp(applicationId)
656 .makePermanent()
657 .forTable(VLAN_TABLE).build();
658 rules.add(preRule);
659 }
660
Saurav Das822c4e22015-10-23 10:51:11 -0700661 rules.add(rule);
662 }
663 return rules;
664 }
665
666 /**
667 * Allows routed packets with correct destination MAC to be directed
668 * to unicast-IP routing table or MPLS forwarding table.
Saurav Das822c4e22015-10-23 10:51:11 -0700669 *
670 * @param portCriterion port on device for which this filter is programmed
671 * @param ethCriterion dstMac of device for which is filter is programmed
672 * @param vidCriterion vlan assigned to port, or NONE for untagged
Saurav Das0e99e2b2015-10-28 12:39:42 -0700673 * @param assignedVlan assigned vlan-id for untagged packets
Saurav Das822c4e22015-10-23 10:51:11 -0700674 * @param applicationId for application programming this filter
675 * @return list of FlowRule for port-vlan filters
676
677 */
678 protected List<FlowRule> processEthDstFilter(PortCriterion portCriterion,
679 EthCriterion ethCriterion,
680 VlanIdCriterion vidCriterion,
Saurav Das0e99e2b2015-10-28 12:39:42 -0700681 VlanId assignedVlan,
Saurav Das822c4e22015-10-23 10:51:11 -0700682 ApplicationId applicationId) {
Charles Chan5270ed02016-01-30 23:22:37 -0800683 // Consider PortNumber.ANY as wildcard. Match ETH_DST only
Sivachidambaram Subramanianc598d062017-05-16 22:42:06 +0530684 if (portCriterion != null && PortNumber.ANY.equals(portCriterion.port())) {
Charles Chan5270ed02016-01-30 23:22:37 -0800685 return processEthDstOnlyFilter(ethCriterion, applicationId);
686 }
687
Charles Chan5b9df8d2016-03-28 22:21:40 -0700688 // Multicast MAC
689 if (ethCriterion.mask() != null) {
Charles Chanb4879a52017-10-20 19:09:16 -0700690 return processMcastEthDstFilter(ethCriterion, assignedVlan, applicationId);
Charles Chan5b9df8d2016-03-28 22:21:40 -0700691 }
692
Saurav Das822c4e22015-10-23 10:51:11 -0700693 //handling untagged packets via assigned VLAN
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530694 if (vidCriterion != null && vidCriterion.vlanId() == VlanId.NONE) {
Saurav Das0e99e2b2015-10-28 12:39:42 -0700695 vidCriterion = (VlanIdCriterion) Criteria.matchVlanId(assignedVlan);
Saurav Das822c4e22015-10-23 10:51:11 -0700696 }
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530697 List<FlowRule> rules = new ArrayList<>();
698 OfdpaMatchVlanVid ofdpaMatchVlanVid = null;
699 if (vidCriterion != null && requireVlanExtensions()) {
700 ofdpaMatchVlanVid = new OfdpaMatchVlanVid(vidCriterion.vlanId());
701 }
Saurav Das822c4e22015-10-23 10:51:11 -0700702 // ofdpa cannot match on ALL portnumber, so we need to use separate
703 // rules for each port.
Yuta HIGUCHI2dce08a2017-04-20 21:57:48 -0700704 List<PortNumber> portnums = new ArrayList<>();
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530705 if (portCriterion != null) {
706 if (PortNumber.ALL.equals(portCriterion.port())) {
707 for (Port port : deviceService.getPorts(deviceId)) {
708 if (port.number().toLong() > 0 && port.number().toLong() < OFPP_MAX) {
709 portnums.add(port.number());
710 }
Saurav Das822c4e22015-10-23 10:51:11 -0700711 }
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530712 } else {
713 portnums.add(portCriterion.port());
714 }
715 for (PortNumber pnum : portnums) {
716 rules.add(buildTmacRuleForIpv4(ethCriterion,
717 vidCriterion,
718 ofdpaMatchVlanVid,
719 applicationId,
720 pnum));
721 rules.add(buildTmacRuleForMpls(ethCriterion,
722 vidCriterion,
723 ofdpaMatchVlanVid,
724 applicationId,
725 pnum));
726 rules.add(buildTmacRuleForIpv6(ethCriterion,
727 vidCriterion,
728 ofdpaMatchVlanVid,
729 applicationId,
730 pnum));
Saurav Das822c4e22015-10-23 10:51:11 -0700731 }
732 } else {
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530733 rules.add(buildTmacRuleForIpv4(ethCriterion,
734 vidCriterion,
735 ofdpaMatchVlanVid,
736 applicationId,
737 null));
738 rules.add(buildTmacRuleForMpls(ethCriterion,
739 vidCriterion,
740 ofdpaMatchVlanVid,
741 applicationId,
742 null));
743 rules.add(buildTmacRuleForIpv6(ethCriterion,
744 vidCriterion,
745 ofdpaMatchVlanVid,
746 applicationId,
747 null));
Saurav Das822c4e22015-10-23 10:51:11 -0700748 }
749 return rules;
750 }
751
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530752 /**
753 * Builds TMAC rules for IPv4 packets.
754 *
Charles Chanc550f2e2017-12-19 19:55:57 -0800755 * @param ethCriterion dst mac matching
756 * @param vidCriterion vlan id assigned to the port
757 * @param ofdpaMatchVlanVid OFDPA vlan id matching
758 * @param applicationId application id
759 * @param pnum port number
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530760 * @return TMAC rule for IPV4 packets
761 */
762 private FlowRule buildTmacRuleForIpv4(EthCriterion ethCriterion,
763 VlanIdCriterion vidCriterion,
764 OfdpaMatchVlanVid ofdpaMatchVlanVid,
765 ApplicationId applicationId,
766 PortNumber pnum) {
767 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
768 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
769 if (pnum != null) {
770 if (matchInPortTmacTable()) {
771 selector.matchInPort(pnum);
772 } else {
Saurav Dasf14d9ef2017-12-05 15:00:23 -0800773 log.debug("Pipeline does not support IN_PORT matching in TMAC table, " +
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530774 "ignoring the IN_PORT criteria");
775 }
776 }
777 if (vidCriterion != null) {
778 if (requireVlanExtensions()) {
779 selector.extension(ofdpaMatchVlanVid, deviceId);
780 } else {
781 selector.matchVlanId(vidCriterion.vlanId());
782 }
783 }
784 selector.matchEthType(Ethernet.TYPE_IPV4);
785 selector.matchEthDst(ethCriterion.mac());
786 treatment.transition(UNICAST_ROUTING_TABLE);
787 return DefaultFlowRule.builder()
788 .forDevice(deviceId)
789 .withSelector(selector.build())
790 .withTreatment(treatment.build())
791 .withPriority(DEFAULT_PRIORITY)
792 .fromApp(applicationId)
793 .makePermanent()
794 .forTable(TMAC_TABLE).build();
795 }
796
797 /**
798 * Builds TMAC rule for MPLS packets.
799 *
Charles Chanc550f2e2017-12-19 19:55:57 -0800800 * @param ethCriterion dst mac matching
801 * @param vidCriterion vlan id assigned to the port
802 * @param ofdpaMatchVlanVid OFDPA vlan id matching
803 * @param applicationId application id
804 * @param pnum port number
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530805 * @return TMAC rule for MPLS packets
806 */
807 private FlowRule buildTmacRuleForMpls(EthCriterion ethCriterion,
808 VlanIdCriterion vidCriterion,
809 OfdpaMatchVlanVid ofdpaMatchVlanVid,
810 ApplicationId applicationId,
811 PortNumber pnum) {
812 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
813 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
814 if (pnum != null) {
815 if (matchInPortTmacTable()) {
816 selector.matchInPort(pnum);
817 } else {
Saurav Dasf14d9ef2017-12-05 15:00:23 -0800818 log.debug("Pipeline does not support IN_PORT matching in TMAC table, " +
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530819 "ignoring the IN_PORT criteria");
820 }
821 }
822 if (vidCriterion != null) {
823 if (requireVlanExtensions()) {
824 selector.extension(ofdpaMatchVlanVid, deviceId);
825 } else {
826 selector.matchVlanId(vidCriterion.vlanId());
827 }
828 }
829 selector.matchEthType(Ethernet.MPLS_UNICAST);
830 selector.matchEthDst(ethCriterion.mac());
831 treatment.transition(MPLS_TABLE_0);
832 return DefaultFlowRule.builder()
833 .forDevice(deviceId)
834 .withSelector(selector.build())
835 .withTreatment(treatment.build())
836 .withPriority(DEFAULT_PRIORITY)
837 .fromApp(applicationId)
838 .makePermanent()
839 .forTable(TMAC_TABLE).build();
840 }
841
842 /**
843 * Builds TMAC rules for IPv6 packets.
844 *
Charles Chanc550f2e2017-12-19 19:55:57 -0800845 * @param ethCriterion dst mac matching
846 * @param vidCriterion vlan id assigned to the port
847 * @param ofdpaMatchVlanVid OFDPA vlan id matching
848 * @param applicationId application id
849 * @param pnum port number
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530850 * @return TMAC rule for IPV6 packets
851 */
852 private FlowRule buildTmacRuleForIpv6(EthCriterion ethCriterion,
853 VlanIdCriterion vidCriterion,
854 OfdpaMatchVlanVid ofdpaMatchVlanVid,
855 ApplicationId applicationId,
856 PortNumber pnum) {
857 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
858 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
859 if (pnum != null) {
860 if (matchInPortTmacTable()) {
861 selector.matchInPort(pnum);
862 } else {
Saurav Dasf14d9ef2017-12-05 15:00:23 -0800863 log.debug("Pipeline does not support IN_PORT matching in TMAC table, " +
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530864 "ignoring the IN_PORT criteria");
865 }
866 }
867 if (vidCriterion != null) {
868 if (requireVlanExtensions()) {
869 selector.extension(ofdpaMatchVlanVid, deviceId);
870 } else {
871 selector.matchVlanId(vidCriterion.vlanId());
872 }
873 }
874 selector.matchEthType(Ethernet.TYPE_IPV6);
875 selector.matchEthDst(ethCriterion.mac());
876 treatment.transition(UNICAST_ROUTING_TABLE);
877 return DefaultFlowRule.builder()
878 .forDevice(deviceId)
879 .withSelector(selector.build())
880 .withTreatment(treatment.build())
881 .withPriority(DEFAULT_PRIORITY)
882 .fromApp(applicationId)
883 .makePermanent()
884 .forTable(TMAC_TABLE).build();
885 }
886
Charles Chan5270ed02016-01-30 23:22:37 -0800887 protected List<FlowRule> processEthDstOnlyFilter(EthCriterion ethCriterion,
Yi Tsengef19de12017-04-24 11:33:05 -0700888 ApplicationId applicationId) {
Pier Luigi0e358632017-01-31 09:35:05 -0800889 ImmutableList.Builder<FlowRule> builder = ImmutableList.builder();
890
Charles Chan5270ed02016-01-30 23:22:37 -0800891 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
892 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
893 selector.matchEthType(Ethernet.TYPE_IPV4);
894 selector.matchEthDst(ethCriterion.mac());
895 treatment.transition(UNICAST_ROUTING_TABLE);
896 FlowRule rule = DefaultFlowRule.builder()
897 .forDevice(deviceId)
898 .withSelector(selector.build())
899 .withTreatment(treatment.build())
900 .withPriority(DEFAULT_PRIORITY)
901 .fromApp(applicationId)
902 .makePermanent()
903 .forTable(TMAC_TABLE).build();
Pier Luigi0e358632017-01-31 09:35:05 -0800904 builder.add(rule);
905
906 selector = DefaultTrafficSelector.builder();
907 treatment = DefaultTrafficTreatment.builder();
908 selector.matchEthType(Ethernet.TYPE_IPV6);
909 selector.matchEthDst(ethCriterion.mac());
910 treatment.transition(UNICAST_ROUTING_TABLE);
911 rule = DefaultFlowRule.builder()
912 .forDevice(deviceId)
913 .withSelector(selector.build())
914 .withTreatment(treatment.build())
915 .withPriority(DEFAULT_PRIORITY)
916 .fromApp(applicationId)
917 .makePermanent()
918 .forTable(TMAC_TABLE).build();
919 return builder.add(rule).build();
Charles Chan5270ed02016-01-30 23:22:37 -0800920 }
921
Charles Chan5b9df8d2016-03-28 22:21:40 -0700922 protected List<FlowRule> processMcastEthDstFilter(EthCriterion ethCriterion,
Charles Chanb4879a52017-10-20 19:09:16 -0700923 VlanId assignedVlan,
Yi Tsengef19de12017-04-24 11:33:05 -0700924 ApplicationId applicationId) {
Julia Ferguson65428c32017-08-10 18:15:24 +0000925 ImmutableList.Builder<FlowRule> builder = ImmutableList.builder();
Charles Chan5b9df8d2016-03-28 22:21:40 -0700926 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
927 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
Charles Chanb4879a52017-10-20 19:09:16 -0700928 FlowRule rule;
Julia Ferguson65428c32017-08-10 18:15:24 +0000929
Charles Chanb4879a52017-10-20 19:09:16 -0700930 if (IPV4_MULTICAST.equals(ethCriterion.mac())) {
931 selector.matchEthType(Ethernet.TYPE_IPV4);
932 selector.matchEthDstMasked(ethCriterion.mac(), ethCriterion.mask());
933 selector.matchVlanId(assignedVlan);
934 treatment.transition(MULTICAST_ROUTING_TABLE);
935 rule = DefaultFlowRule.builder()
936 .forDevice(deviceId)
937 .withSelector(selector.build())
938 .withTreatment(treatment.build())
939 .withPriority(DEFAULT_PRIORITY)
940 .fromApp(applicationId)
941 .makePermanent()
942 .forTable(TMAC_TABLE).build();
943 builder.add(rule);
944 }
945
946 if (IPV6_MULTICAST.equals(ethCriterion.mac())) {
947 selector = DefaultTrafficSelector.builder();
948 treatment = DefaultTrafficTreatment.builder();
949 selector.matchEthType(Ethernet.TYPE_IPV6);
950 selector.matchEthDstMasked(ethCriterion.mac(), ethCriterion.mask());
951 selector.matchVlanId(assignedVlan);
952 treatment.transition(MULTICAST_ROUTING_TABLE);
953 rule = DefaultFlowRule.builder()
954 .forDevice(deviceId)
955 .withSelector(selector.build())
956 .withTreatment(treatment.build())
957 .withPriority(DEFAULT_PRIORITY)
958 .fromApp(applicationId)
959 .makePermanent()
960 .forTable(TMAC_TABLE).build();
961 builder.add(rule);
962 }
963 return builder.build();
Charles Chan5b9df8d2016-03-28 22:21:40 -0700964 }
965
Saurav Das822c4e22015-10-23 10:51:11 -0700966 private Collection<FlowRule> processForward(ForwardingObjective fwd) {
967 switch (fwd.flag()) {
968 case SPECIFIC:
969 return processSpecific(fwd);
970 case VERSATILE:
971 return processVersatile(fwd);
972 default:
973 fail(fwd, ObjectiveError.UNKNOWN);
974 log.warn("Unknown forwarding flag {}", fwd.flag());
975 }
976 return Collections.emptySet();
977 }
978
979 /**
980 * In the OF-DPA 2.0 pipeline, versatile forwarding objectives go to the
981 * ACL table.
982 * @param fwd the forwarding objective of type 'versatile'
983 * @return a collection of flow rules to be sent to the switch. An empty
984 * collection may be returned if there is a problem in processing
985 * the flow rule
986 */
Saurav Das52025962016-01-28 22:30:01 -0800987 protected Collection<FlowRule> processVersatile(ForwardingObjective fwd) {
Saurav Das018605f2017-02-18 14:05:44 -0800988 log.debug("Processing versatile forwarding objective:{} in dev:{}",
Saurav Dasd2fded02016-12-02 15:43:47 -0800989 fwd.id(), deviceId);
Saurav Das822c4e22015-10-23 10:51:11 -0700990
991 EthTypeCriterion ethType =
Saurav Das77b5e902016-01-27 17:01:59 -0800992 (EthTypeCriterion) fwd.selector().getCriterion(Criterion.Type.ETH_TYPE);
Saurav Das822c4e22015-10-23 10:51:11 -0700993 if (ethType == null) {
Saurav Dasd2fded02016-12-02 15:43:47 -0800994 log.error("Versatile forwarding objective:{} must include ethType",
995 fwd.id());
Saurav Das822c4e22015-10-23 10:51:11 -0700996 fail(fwd, ObjectiveError.BADPARAMS);
997 return Collections.emptySet();
998 }
999 if (fwd.nextId() == null && fwd.treatment() == null) {
1000 log.error("Forwarding objective {} from {} must contain "
1001 + "nextId or Treatment", fwd.selector(), fwd.appId());
Zsolt Haraszti9faab752016-02-17 15:55:20 -08001002 fail(fwd, ObjectiveError.BADPARAMS);
Saurav Das822c4e22015-10-23 10:51:11 -07001003 return Collections.emptySet();
1004 }
Saurav Das49cb5a12016-01-16 22:54:07 -08001005
Saurav Das77b5e902016-01-27 17:01:59 -08001006 TrafficSelector.Builder sbuilder = DefaultTrafficSelector.builder();
1007 fwd.selector().criteria().forEach(criterion -> {
1008 if (criterion instanceof VlanIdCriterion) {
1009 VlanId vlanId = ((VlanIdCriterion) criterion).vlanId();
1010 // ensure that match does not include vlan = NONE as OF-DPA does not
1011 // match untagged packets this way in the ACL table.
1012 if (vlanId.equals(VlanId.NONE)) {
1013 return;
1014 }
Charles Chand1172632017-03-15 17:33:09 -07001015 if (requireVlanExtensions()) {
1016 OfdpaMatchVlanVid ofdpaMatchVlanVid = new OfdpaMatchVlanVid(vlanId);
1017 sbuilder.extension(ofdpaMatchVlanVid, deviceId);
1018 } else {
1019 sbuilder.matchVlanId(vlanId);
1020 }
Charles Chan09bf2692018-01-11 11:48:18 -08001021 } else if (criterion instanceof Icmpv6TypeCriterion) {
1022 byte icmpv6Type = (byte) ((Icmpv6TypeCriterion) criterion).icmpv6Type();
1023 sbuilder.matchIcmpv6Type(icmpv6Type);
1024 } else if (criterion instanceof Icmpv6CodeCriterion) {
1025 byte icmpv6Code = (byte) ((Icmpv6CodeCriterion) criterion).icmpv6Code();
1026 sbuilder.matchIcmpv6Type(icmpv6Code);
Charles Chand9e47c62017-10-05 15:17:15 -07001027 } else if (criterion instanceof TcpPortCriterion || criterion instanceof UdpPortCriterion) {
1028 // FIXME: QMX switches do not support L4 dst port matching in ACL table.
1029 // Currently L4 dst port matching is only used by DHCP relay feature
1030 // and therefore is safe to be replaced with L4 src port matching.
1031 // We need to revisit this if L4 dst port is used for other purpose in the future.
1032 if (!supportIpv6L4Dst() && isIpv6(fwd.selector())) {
Charles Chand9e47c62017-10-05 15:17:15 -07001033 switch (criterion.type()) {
1034 case UDP_DST:
Charles Chand9e47c62017-10-05 15:17:15 -07001035 case UDP_DST_MASKED:
Charles Chand9e47c62017-10-05 15:17:15 -07001036 case TCP_DST:
Charles Chand9e47c62017-10-05 15:17:15 -07001037 case TCP_DST_MASKED:
Charles Chand9e47c62017-10-05 15:17:15 -07001038 break;
1039 default:
1040 sbuilder.add(criterion);
1041 }
1042 } else {
1043 sbuilder.add(criterion);
1044 }
Saurav Das77b5e902016-01-27 17:01:59 -08001045 } else {
1046 sbuilder.add(criterion);
1047 }
1048 });
1049
Saurav Das822c4e22015-10-23 10:51:11 -07001050 // XXX driver does not currently do type checking as per Tables 65-67 in
1051 // OFDPA 2.0 spec. The only allowed treatment is a punt to the controller.
Saurav Das49cb5a12016-01-16 22:54:07 -08001052 TrafficTreatment.Builder ttBuilder = DefaultTrafficTreatment.builder();
1053 if (fwd.treatment() != null) {
1054 for (Instruction ins : fwd.treatment().allInstructions()) {
1055 if (ins instanceof OutputInstruction) {
1056 OutputInstruction o = (OutputInstruction) ins;
Ray Milkeyfe0e0852018-01-18 11:14:05 -08001057 if (PortNumber.CONTROLLER.equals(o.port())) {
Saurav Das49cb5a12016-01-16 22:54:07 -08001058 ttBuilder.add(o);
1059 } else {
1060 log.warn("Only allowed treatments in versatile forwarding "
1061 + "objectives are punts to the controller");
1062 }
1063 } else {
1064 log.warn("Cannot process instruction in versatile fwd {}", ins);
1065 }
Saurav Das822c4e22015-10-23 10:51:11 -07001066 }
Charles Chan2df0e8a2017-01-09 11:45:08 -08001067 if (fwd.treatment().clearedDeferred()) {
1068 ttBuilder.wipeDeferred();
1069 }
Saurav Das822c4e22015-10-23 10:51:11 -07001070 }
Saurav Das822c4e22015-10-23 10:51:11 -07001071 if (fwd.nextId() != null) {
Saurav Das49cb5a12016-01-16 22:54:07 -08001072 // overide case
1073 NextGroup next = getGroupForNextObjective(fwd.nextId());
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +05301074 if (next == null) {
1075 fail(fwd, ObjectiveError.BADPARAMS);
1076 return Collections.emptySet();
1077 }
Saurav Das49cb5a12016-01-16 22:54:07 -08001078 List<Deque<GroupKey>> gkeys = appKryo.deserialize(next.data());
1079 // we only need the top level group's key to point the flow to it
1080 Group group = groupService.getGroup(deviceId, gkeys.get(0).peekFirst());
1081 if (group == null) {
1082 log.warn("Group with key:{} for next-id:{} not found in dev:{}",
1083 gkeys.get(0).peekFirst(), fwd.nextId(), deviceId);
1084 fail(fwd, ObjectiveError.GROUPMISSING);
1085 return Collections.emptySet();
1086 }
1087 ttBuilder.deferred().group(group.id());
Saurav Das822c4e22015-10-23 10:51:11 -07001088 }
Saurav Das49cb5a12016-01-16 22:54:07 -08001089
1090 FlowRule.Builder ruleBuilder = DefaultFlowRule.builder()
1091 .fromApp(fwd.appId())
1092 .withPriority(fwd.priority())
1093 .forDevice(deviceId)
Saurav Das77b5e902016-01-27 17:01:59 -08001094 .withSelector(sbuilder.build())
Saurav Das49cb5a12016-01-16 22:54:07 -08001095 .withTreatment(ttBuilder.build())
1096 .makePermanent()
1097 .forTable(ACL_TABLE);
1098 return Collections.singletonList(ruleBuilder.build());
Saurav Das822c4e22015-10-23 10:51:11 -07001099 }
1100
1101 /**
1102 * In the OF-DPA 2.0 pipeline, specific forwarding refers to the IP table
Saurav Das8a0732e2015-11-20 15:27:53 -08001103 * (unicast or multicast) or the L2 table (mac + vlan) or the MPLS table.
Saurav Das822c4e22015-10-23 10:51:11 -07001104 *
1105 * @param fwd the forwarding objective of type 'specific'
1106 * @return a collection of flow rules. Typically there will be only one
1107 * for this type of forwarding objective. An empty set may be
1108 * returned if there is an issue in processing the objective.
1109 */
Saurav Das8a0732e2015-11-20 15:27:53 -08001110 protected Collection<FlowRule> processSpecific(ForwardingObjective fwd) {
Saurav Das25190812016-05-27 13:54:07 -07001111 log.debug("Processing specific fwd objective:{} in dev:{} with next:{}",
Saurav Das4ce45962015-11-24 23:21:05 -08001112 fwd.id(), deviceId, fwd.nextId());
1113 boolean isEthTypeObj = isSupportedEthTypeObjective(fwd);
1114 boolean isEthDstObj = isSupportedEthDstObjective(fwd);
1115
1116 if (isEthTypeObj) {
1117 return processEthTypeSpecific(fwd);
1118 } else if (isEthDstObj) {
1119 return processEthDstSpecific(fwd);
1120 } else {
1121 log.warn("processSpecific: Unsupported forwarding objective "
1122 + "criteria fwd:{} in dev:{}", fwd.nextId(), deviceId);
Saurav Das822c4e22015-10-23 10:51:11 -07001123 fail(fwd, ObjectiveError.UNSUPPORTED);
1124 return Collections.emptySet();
1125 }
Saurav Das4ce45962015-11-24 23:21:05 -08001126 }
1127
Saurav Das4ce45962015-11-24 23:21:05 -08001128 /**
1129 * Handles forwarding rules to the IP and MPLS tables.
1130 *
1131 * @param fwd the forwarding objective
1132 * @return A collection of flow rules, or an empty set
1133 */
1134 protected Collection<FlowRule> processEthTypeSpecific(ForwardingObjective fwd) {
Charles Chancad338a2016-09-16 18:03:11 -07001135 return processEthTypeSpecificInternal(fwd, false, ACL_TABLE);
Charles Chanf9e98652016-09-07 16:54:23 -07001136 }
1137
1138 /**
1139 * Internal implementation of processEthTypeSpecific.
1140 * <p>
1141 * Wildcarded IPv4_DST is not supported in OFDPA i12. Therefore, we break
1142 * the rule into 0.0.0.0/1 and 128.0.0.0/1.
1143 * The allowDefaultRoute must be set to false for OFDPA i12.
1144 * </p>
1145 *
1146 * @param fwd the forwarding objective
1147 * @param allowDefaultRoute allow wildcarded IPv4_DST or not
Ray Milkey0bb1e102016-11-10 14:51:27 -08001148 * @param mplsNextTable next MPLS table
Charles Chanf9e98652016-09-07 16:54:23 -07001149 * @return A collection of flow rules, or an empty set
1150 */
1151 protected Collection<FlowRule> processEthTypeSpecificInternal(ForwardingObjective fwd,
Charles Chancad338a2016-09-16 18:03:11 -07001152 boolean allowDefaultRoute,
1153 int mplsNextTable) {
Saurav Das4ce45962015-11-24 23:21:05 -08001154 TrafficSelector selector = fwd.selector();
1155 EthTypeCriterion ethType =
1156 (EthTypeCriterion) selector.getCriterion(Criterion.Type.ETH_TYPE);
Flavio Castroe10fa242016-01-15 12:43:51 -08001157 boolean popMpls = false;
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001158 boolean emptyGroup = false;
Charles Chan188ebf52015-12-23 00:15:11 -08001159 int forTableId;
Saurav Das8a0732e2015-11-20 15:27:53 -08001160 TrafficSelector.Builder filteredSelector = DefaultTrafficSelector.builder();
Charles Chan14967c22015-12-07 11:11:50 -08001161 TrafficTreatment.Builder tb = DefaultTrafficTreatment.builder();
Flavio Castroe10fa242016-01-15 12:43:51 -08001162 TrafficSelector.Builder complementarySelector = DefaultTrafficSelector.builder();
Charles Chan14967c22015-12-07 11:11:50 -08001163
Saurav Das8a0732e2015-11-20 15:27:53 -08001164 if (ethType.ethType().toShort() == Ethernet.TYPE_IPV4) {
Pier Luigi3bfe32c2017-01-30 09:47:36 -08001165 if (buildIpv4Selector(filteredSelector, complementarySelector, fwd, allowDefaultRoute) < 0) {
1166 return Collections.emptyList();
1167 }
1168 // We need to set properly the next table
Flavio Castroe10fa242016-01-15 12:43:51 -08001169 IpPrefix ipv4Dst = ((IPCriterion) selector.getCriterion(Criterion.Type.IPV4_DST)).ip();
Charles Chan5b9df8d2016-03-28 22:21:40 -07001170 if (ipv4Dst.isMulticast()) {
Charles Chan5b9df8d2016-03-28 22:21:40 -07001171 forTableId = MULTICAST_ROUTING_TABLE;
Flavio Castroe10fa242016-01-15 12:43:51 -08001172 } else {
Charles Chan5b9df8d2016-03-28 22:21:40 -07001173 forTableId = UNICAST_ROUTING_TABLE;
Flavio Castroe10fa242016-01-15 12:43:51 -08001174 }
Charles Chan14967c22015-12-07 11:11:50 -08001175 if (fwd.treatment() != null) {
1176 for (Instruction instr : fwd.treatment().allInstructions()) {
1177 if (instr instanceof L3ModificationInstruction &&
1178 ((L3ModificationInstruction) instr).subtype() == L3SubType.DEC_TTL) {
Saurav Das9c705342017-01-06 11:36:01 -08001179 // XXX decrementing IP ttl is done automatically for routing, this
1180 // action is ignored or rejected in ofdpa as it is not fully implemented
1181 //tb.deferred().add(instr);
Charles Chan14967c22015-12-07 11:11:50 -08001182 }
1183 }
1184 }
1185
Pier Ventree0ae7a32016-11-23 09:57:42 -08001186 } else if (ethType.ethType().toShort() == Ethernet.TYPE_IPV6) {
1187 if (buildIpv6Selector(filteredSelector, fwd) < 0) {
1188 return Collections.emptyList();
1189 }
Kalhee Kim3d26bf02017-10-04 14:59:11 +00001190 //We need to set the proper next table
1191 IpPrefix ipv6Dst = ((IPCriterion) selector.getCriterion(Criterion.Type.IPV6_DST)).ip();
1192 if (ipv6Dst.isMulticast()) {
1193 forTableId = MULTICAST_ROUTING_TABLE;
1194 } else {
1195 forTableId = UNICAST_ROUTING_TABLE;
1196 }
1197
Pier Ventree0ae7a32016-11-23 09:57:42 -08001198 if (fwd.treatment() != null) {
1199 for (Instruction instr : fwd.treatment().allInstructions()) {
1200 if (instr instanceof L3ModificationInstruction &&
1201 ((L3ModificationInstruction) instr).subtype() == L3SubType.DEC_TTL) {
1202 // XXX decrementing IP ttl is done automatically for routing, this
1203 // action is ignored or rejected in ofdpa as it is not fully implemented
1204 //tb.deferred().add(instr);
1205 }
1206 }
1207 }
Saurav Das8a0732e2015-11-20 15:27:53 -08001208 } else {
1209 filteredSelector
1210 .matchEthType(Ethernet.MPLS_UNICAST)
1211 .matchMplsLabel(((MplsCriterion)
1212 selector.getCriterion(Criterion.Type.MPLS_LABEL)).label());
1213 MplsBosCriterion bos = (MplsBosCriterion) selector
Pier Ventre140a8942016-11-02 07:26:38 -07001214 .getCriterion(MPLS_BOS);
Alex Yashchuk4caa8e82017-12-08 17:40:05 +02001215 if (bos != null && requireMplsBosMatch()) {
Saurav Das8a0732e2015-11-20 15:27:53 -08001216 filteredSelector.matchMplsBos(bos.mplsBos());
1217 }
1218 forTableId = MPLS_TABLE_1;
Saurav Das4ce45962015-11-24 23:21:05 -08001219 log.debug("processing MPLS specific forwarding objective {} -> next:{}"
1220 + " in dev {}", fwd.id(), fwd.nextId(), deviceId);
Saurav Das822c4e22015-10-23 10:51:11 -07001221
Charles Chan14967c22015-12-07 11:11:50 -08001222 if (fwd.treatment() != null) {
1223 for (Instruction instr : fwd.treatment().allInstructions()) {
1224 if (instr instanceof L2ModificationInstruction &&
1225 ((L2ModificationInstruction) instr).subtype() == L2SubType.MPLS_POP) {
Saurav Das8a0732e2015-11-20 15:27:53 -08001226 popMpls = true;
Pier Luigi3bfe32c2017-01-30 09:47:36 -08001227 // OF-DPA does not pop in MPLS table in some cases. For the L3 VPN, it requires
Saurav Das9c705342017-01-06 11:36:01 -08001228 // setting the MPLS_TYPE so pop can happen down the pipeline
Alex Yashchuk4caa8e82017-12-08 17:40:05 +02001229 if (requireMplsPop()) {
1230 if (mplsNextTable == MPLS_TYPE_TABLE && isNotMplsBos(selector)) {
1231 tb.immediate().popMpls();
1232 }
1233 } else {
1234 // Skip mpls pop action for mpls_unicast label
1235 if (instr instanceof ModMplsHeaderInstruction &&
1236 !((ModMplsHeaderInstruction) instr).ethernetType()
1237 .equals(EtherType.MPLS_UNICAST.ethType())) {
1238 tb.immediate().add(instr);
1239 }
Pier Luigi3bfe32c2017-01-30 09:47:36 -08001240 }
Charles Chan14967c22015-12-07 11:11:50 -08001241 }
Alex Yashchuk4caa8e82017-12-08 17:40:05 +02001242
1243 if (requireMplsTtlModification()) {
1244 if (instr instanceof L3ModificationInstruction &&
1245 ((L3ModificationInstruction) instr).subtype() == L3SubType.DEC_TTL) {
1246 // FIXME Should modify the app to send the correct DEC_MPLS_TTL instruction
1247 tb.immediate().decMplsTtl();
1248 }
1249 if (instr instanceof L3ModificationInstruction &&
1250 ((L3ModificationInstruction) instr).subtype() == L3SubType.TTL_IN) {
1251 tb.immediate().add(instr);
1252 }
Charles Chan14967c22015-12-07 11:11:50 -08001253 }
Saurav Das8a0732e2015-11-20 15:27:53 -08001254 }
1255 }
1256 }
Saurav Das822c4e22015-10-23 10:51:11 -07001257
1258 if (fwd.nextId() != null) {
Saurav Das8a0732e2015-11-20 15:27:53 -08001259 if (forTableId == MPLS_TABLE_1 && !popMpls) {
1260 log.warn("SR CONTINUE case cannot be handled as MPLS ECMP "
Saurav Das25190812016-05-27 13:54:07 -07001261 + "is not implemented in OF-DPA yet. Aborting this flow {} -> next:{}"
1262 + "in this device {}", fwd.id(), fwd.nextId(), deviceId);
Pier Ventree0ae7a32016-11-23 09:57:42 -08001263 // XXX We could convert to forwarding to a single-port, via a MPLS interface,
1264 // or a MPLS SWAP (with-same) but that would have to be handled in the next-objective.
1265 // Also the pop-mpls logic used here won't work in non-BoS case.
Saurav Das4ce45962015-11-24 23:21:05 -08001266 fail(fwd, ObjectiveError.FLOWINSTALLATIONFAILED);
Saurav Das8a0732e2015-11-20 15:27:53 -08001267 return Collections.emptySet();
1268 }
1269
Saurav Das423fe2b2015-12-04 10:52:59 -08001270 NextGroup next = getGroupForNextObjective(fwd.nextId());
1271 if (next != null) {
1272 List<Deque<GroupKey>> gkeys = appKryo.deserialize(next.data());
1273 // we only need the top level group's key to point the flow to it
1274 Group group = groupService.getGroup(deviceId, gkeys.get(0).peekFirst());
Pier Luigi075f1012018-02-01 10:23:12 +01001275 if (isNotMplsBos(selector) && group.type().equals(SELECT)) {
Pier Ventre140a8942016-11-02 07:26:38 -07001276 log.warn("SR CONTINUE case cannot be handled as MPLS ECMP "
1277 + "is not implemented in OF-DPA yet. Aborting this flow {} -> next:{}"
1278 + "in this device {}", fwd.id(), fwd.nextId(), deviceId);
1279 fail(fwd, ObjectiveError.FLOWINSTALLATIONFAILED);
1280 return Collections.emptySet();
1281 }
Saurav Das423fe2b2015-12-04 10:52:59 -08001282 if (group == null) {
1283 log.warn("Group with key:{} for next-id:{} not found in dev:{}",
1284 gkeys.get(0).peekFirst(), fwd.nextId(), deviceId);
1285 fail(fwd, ObjectiveError.GROUPMISSING);
1286 return Collections.emptySet();
1287 }
1288 tb.deferred().group(group.id());
Saurav Dasc568c342018-01-25 09:49:01 -08001289 // retrying flows may be necessary due to bug CORD-554
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001290 if (gkeys.size() == 1 && gkeys.get(0).size() == 1) {
Saurav Dasc568c342018-01-25 09:49:01 -08001291 if (shouldRetry()) {
1292 log.warn("Found empty group 0x{} in dev:{} .. will retry fwd:{}",
1293 Integer.toHexString(group.id().id()), deviceId,
1294 fwd.id());
1295 emptyGroup = true;
1296 }
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001297 }
Saurav Das25190812016-05-27 13:54:07 -07001298 } else {
1299 log.warn("Cannot find group for nextId:{} in dev:{}. Aborting fwd:{}",
1300 fwd.nextId(), deviceId, fwd.id());
1301 fail(fwd, ObjectiveError.FLOWINSTALLATIONFAILED);
1302 return Collections.emptySet();
Saurav Das822c4e22015-10-23 10:51:11 -07001303 }
Saurav Das822c4e22015-10-23 10:51:11 -07001304 }
Charles Chancad338a2016-09-16 18:03:11 -07001305
1306 if (forTableId == MPLS_TABLE_1) {
Pier Ventre140a8942016-11-02 07:26:38 -07001307 if (mplsNextTable == MPLS_L3_TYPE_TABLE) {
Charles Chancad338a2016-09-16 18:03:11 -07001308 Ofdpa3SetMplsType setMplsType = new Ofdpa3SetMplsType(Ofdpa3MplsType.L3_PHP);
Saurav Das9c705342017-01-06 11:36:01 -08001309 // set mpls type as apply_action
1310 tb.immediate().extension(setMplsType, deviceId);
Charles Chancad338a2016-09-16 18:03:11 -07001311 }
1312 tb.transition(mplsNextTable);
1313 } else {
1314 tb.transition(ACL_TABLE);
1315 }
1316
Saurav Das822c4e22015-10-23 10:51:11 -07001317 FlowRule.Builder ruleBuilder = DefaultFlowRule.builder()
1318 .fromApp(fwd.appId())
1319 .withPriority(fwd.priority())
1320 .forDevice(deviceId)
Saurav Das8a0732e2015-11-20 15:27:53 -08001321 .withSelector(filteredSelector.build())
1322 .withTreatment(tb.build())
1323 .forTable(forTableId);
Saurav Das822c4e22015-10-23 10:51:11 -07001324
1325 if (fwd.permanent()) {
1326 ruleBuilder.makePermanent();
1327 } else {
1328 ruleBuilder.makeTemporary(fwd.timeout());
1329 }
Flavio Castroe10fa242016-01-15 12:43:51 -08001330 Collection<FlowRule> flowRuleCollection = new ArrayList<>();
1331 flowRuleCollection.add(ruleBuilder.build());
Pier Luigi3bfe32c2017-01-30 09:47:36 -08001332 if (!allowDefaultRoute) {
Pier Ventree0ae7a32016-11-23 09:57:42 -08001333 flowRuleCollection.add(
1334 defaultRoute(fwd, complementarySelector, forTableId, tb)
1335 );
Flavio Castroe10fa242016-01-15 12:43:51 -08001336 log.debug("Default rule 0.0.0.0/0 is being installed two rules");
1337 }
Saurav Dasc568c342018-01-25 09:49:01 -08001338
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001339 if (emptyGroup) {
1340 executorService.schedule(new RetryFlows(fwd, flowRuleCollection),
1341 RETRY_MS, TimeUnit.MILLISECONDS);
1342 }
Flavio Castroe10fa242016-01-15 12:43:51 -08001343 return flowRuleCollection;
Saurav Das822c4e22015-10-23 10:51:11 -07001344 }
1345
Pier Luigi3bfe32c2017-01-30 09:47:36 -08001346 protected int buildIpv4Selector(TrafficSelector.Builder builderToUpdate,
1347 TrafficSelector.Builder extBuilder,
1348 ForwardingObjective fwd,
1349 boolean allowDefaultRoute) {
1350 TrafficSelector selector = fwd.selector();
1351
1352 IpPrefix ipv4Dst = ((IPCriterion) selector.getCriterion(Criterion.Type.IPV4_DST)).ip();
1353 if (ipv4Dst.isMulticast()) {
1354 if (ipv4Dst.prefixLength() != 32) {
1355 log.warn("Multicast specific forwarding objective can only be /32");
1356 fail(fwd, ObjectiveError.BADPARAMS);
1357 return -1;
1358 }
1359 VlanId assignedVlan = readVlanFromSelector(fwd.meta());
1360 if (assignedVlan == null) {
1361 log.warn("VLAN ID required by multicast specific fwd obj is missing. Abort.");
1362 fail(fwd, ObjectiveError.BADPARAMS);
1363 return -1;
1364 }
Charles Chand1172632017-03-15 17:33:09 -07001365 if (requireVlanExtensions()) {
1366 OfdpaMatchVlanVid ofdpaMatchVlanVid = new OfdpaMatchVlanVid(assignedVlan);
1367 builderToUpdate.extension(ofdpaMatchVlanVid, deviceId);
1368 } else {
1369 builderToUpdate.matchVlanId(assignedVlan);
1370 }
Pier Luigi3bfe32c2017-01-30 09:47:36 -08001371 builderToUpdate.matchEthType(Ethernet.TYPE_IPV4).matchIPDst(ipv4Dst);
1372 log.debug("processing IPv4 multicast specific forwarding objective {} -> next:{}"
1373 + " in dev:{}", fwd.id(), fwd.nextId(), deviceId);
1374 } else {
1375 if (ipv4Dst.prefixLength() == 0) {
1376 if (allowDefaultRoute) {
1377 // The entire IPV4_DST field is wildcarded intentionally
1378 builderToUpdate.matchEthType(Ethernet.TYPE_IPV4);
1379 } else {
1380 // NOTE: The switch does not support matching 0.0.0.0/0
1381 // Split it into 0.0.0.0/1 and 128.0.0.0/1
1382 builderToUpdate.matchEthType(Ethernet.TYPE_IPV4)
1383 .matchIPDst(IpPrefix.valueOf("0.0.0.0/1"));
1384 extBuilder.matchEthType(Ethernet.TYPE_IPV4)
1385 .matchIPDst(IpPrefix.valueOf("128.0.0.0/1"));
1386 }
1387 } else {
1388 builderToUpdate.matchEthType(Ethernet.TYPE_IPV4).matchIPDst(ipv4Dst);
1389 }
1390 log.debug("processing IPv4 unicast specific forwarding objective {} -> next:{}"
1391 + " in dev:{}", fwd.id(), fwd.nextId(), deviceId);
1392 }
1393 return 0;
1394 }
1395
1396 /**
1397 * Helper method to build Ipv6 selector using the selector provided by
1398 * a forwarding objective.
1399 *
1400 * @param builderToUpdate the builder to update
1401 * @param fwd the selector to read
1402 * @return 0 if the update ends correctly. -1 if the matches
1403 * are not yet supported
1404 */
1405 protected int buildIpv6Selector(TrafficSelector.Builder builderToUpdate,
1406 ForwardingObjective fwd) {
1407
1408 TrafficSelector selector = fwd.selector();
1409
1410 IpPrefix ipv6Dst = ((IPCriterion) selector.getCriterion(Criterion.Type.IPV6_DST)).ip();
1411 if (ipv6Dst.isMulticast()) {
Julia Ferguson65428c32017-08-10 18:15:24 +00001412 if (ipv6Dst.prefixLength() != IpAddress.INET6_BIT_LENGTH) {
1413 log.warn("Multicast specific forwarding objective can only be /128");
1414 fail(fwd, ObjectiveError.BADPARAMS);
1415 return -1;
1416 }
1417 VlanId assignedVlan = readVlanFromSelector(fwd.meta());
1418 if (assignedVlan == null) {
1419 log.warn("VLAN ID required by multicast specific fwd obj is missing. Abort.");
1420 fail(fwd, ObjectiveError.BADPARAMS);
1421 return -1;
1422 }
1423 if (requireVlanExtensions()) {
1424 OfdpaMatchVlanVid ofdpaMatchVlanVid = new OfdpaMatchVlanVid(assignedVlan);
1425 builderToUpdate.extension(ofdpaMatchVlanVid, deviceId);
1426 } else {
1427 builderToUpdate.matchVlanId(assignedVlan);
1428 }
1429 builderToUpdate.matchEthType(Ethernet.TYPE_IPV6).matchIPv6Dst(ipv6Dst);
1430 log.debug("processing IPv6 multicast specific forwarding objective {} -> next:{}"
1431 + " in dev:{}", fwd.id(), fwd.nextId(), deviceId);
1432 } else {
1433 if (ipv6Dst.prefixLength() != 0) {
1434 builderToUpdate.matchIPv6Dst(ipv6Dst);
1435 }
Pier Luigi3bfe32c2017-01-30 09:47:36 -08001436 builderToUpdate.matchEthType(Ethernet.TYPE_IPV6);
1437 log.debug("processing IPv6 unicast specific forwarding objective {} -> next:{}"
1438 + " in dev:{}", fwd.id(), fwd.nextId(), deviceId);
Julia Ferguson65428c32017-08-10 18:15:24 +00001439 }
Pier Luigi3bfe32c2017-01-30 09:47:36 -08001440 return 0;
1441 }
1442
Pier Ventree0ae7a32016-11-23 09:57:42 -08001443 protected FlowRule defaultRoute(ForwardingObjective fwd,
1444 TrafficSelector.Builder complementarySelector,
1445 int forTableId,
1446 TrafficTreatment.Builder tb) {
1447 FlowRule.Builder rule = DefaultFlowRule.builder()
1448 .fromApp(fwd.appId())
1449 .withPriority(fwd.priority())
1450 .forDevice(deviceId)
1451 .withSelector(complementarySelector.build())
1452 .withTreatment(tb.build())
1453 .forTable(forTableId);
1454 if (fwd.permanent()) {
1455 rule.makePermanent();
1456 } else {
1457 rule.makeTemporary(fwd.timeout());
1458 }
1459 return rule.build();
1460 }
1461
Saurav Das4ce45962015-11-24 23:21:05 -08001462 /**
1463 * Handles forwarding rules to the L2 bridging table. Flow actions are not
1464 * allowed in the bridging table - instead we use L2 Interface group or
1465 * L2 flood group
1466 *
1467 * @param fwd the forwarding objective
1468 * @return A collection of flow rules, or an empty set
1469 */
1470 protected Collection<FlowRule> processEthDstSpecific(ForwardingObjective fwd) {
1471 List<FlowRule> rules = new ArrayList<>();
1472
1473 // Build filtered selector
1474 TrafficSelector selector = fwd.selector();
1475 EthCriterion ethCriterion = (EthCriterion) selector
1476 .getCriterion(Criterion.Type.ETH_DST);
1477 VlanIdCriterion vlanIdCriterion = (VlanIdCriterion) selector
1478 .getCriterion(Criterion.Type.VLAN_VID);
1479
1480 if (vlanIdCriterion == null) {
1481 log.warn("Forwarding objective for bridging requires vlan. Not "
1482 + "installing fwd:{} in dev:{}", fwd.id(), deviceId);
1483 fail(fwd, ObjectiveError.BADPARAMS);
1484 return Collections.emptySet();
1485 }
1486
1487 TrafficSelector.Builder filteredSelectorBuilder =
1488 DefaultTrafficSelector.builder();
Yi Tseng3a77b4f2017-02-06 15:02:17 -08001489
1490 if (!ethCriterion.mac().equals(NONE) &&
1491 !ethCriterion.mac().equals(BROADCAST)) {
Saurav Das4ce45962015-11-24 23:21:05 -08001492 filteredSelectorBuilder.matchEthDst(ethCriterion.mac());
1493 log.debug("processing L2 forwarding objective:{} -> next:{} in dev:{}",
1494 fwd.id(), fwd.nextId(), deviceId);
1495 } else {
Yi Tseng3a77b4f2017-02-06 15:02:17 -08001496 // Use wildcard DST_MAC if the MacAddress is None or Broadcast
Saurav Das4ce45962015-11-24 23:21:05 -08001497 log.debug("processing L2 Broadcast forwarding objective:{} -> next:{} "
1498 + "in dev:{} for vlan:{}",
1499 fwd.id(), fwd.nextId(), deviceId, vlanIdCriterion.vlanId());
1500 }
Charles Chand1172632017-03-15 17:33:09 -07001501 if (requireVlanExtensions()) {
1502 OfdpaMatchVlanVid ofdpaMatchVlanVid = new OfdpaMatchVlanVid(vlanIdCriterion.vlanId());
1503 filteredSelectorBuilder.extension(ofdpaMatchVlanVid, deviceId);
1504 } else {
1505 filteredSelectorBuilder.matchVlanId(vlanIdCriterion.vlanId());
1506 }
Saurav Das4ce45962015-11-24 23:21:05 -08001507 TrafficSelector filteredSelector = filteredSelectorBuilder.build();
1508
1509 if (fwd.treatment() != null) {
1510 log.warn("Ignoring traffic treatment in fwd rule {} meant for L2 table"
1511 + "for dev:{}. Expecting only nextId", fwd.id(), deviceId);
1512 }
1513
1514 TrafficTreatment.Builder treatmentBuilder = DefaultTrafficTreatment.builder();
1515 if (fwd.nextId() != null) {
Saurav Das423fe2b2015-12-04 10:52:59 -08001516 NextGroup next = getGroupForNextObjective(fwd.nextId());
Saurav Das4ce45962015-11-24 23:21:05 -08001517 if (next != null) {
1518 List<Deque<GroupKey>> gkeys = appKryo.deserialize(next.data());
1519 // we only need the top level group's key to point the flow to it
1520 Group group = groupService.getGroup(deviceId, gkeys.get(0).peekFirst());
1521 if (group != null) {
1522 treatmentBuilder.deferred().group(group.id());
1523 } else {
1524 log.warn("Group with key:{} for next-id:{} not found in dev:{}",
1525 gkeys.get(0).peekFirst(), fwd.nextId(), deviceId);
1526 fail(fwd, ObjectiveError.GROUPMISSING);
1527 return Collections.emptySet();
1528 }
1529 }
1530 }
1531 treatmentBuilder.immediate().transition(ACL_TABLE);
1532 TrafficTreatment filteredTreatment = treatmentBuilder.build();
1533
1534 // Build bridging table entries
1535 FlowRule.Builder flowRuleBuilder = DefaultFlowRule.builder();
1536 flowRuleBuilder.fromApp(fwd.appId())
1537 .withPriority(fwd.priority())
1538 .forDevice(deviceId)
1539 .withSelector(filteredSelector)
1540 .withTreatment(filteredTreatment)
1541 .forTable(BRIDGING_TABLE);
1542 if (fwd.permanent()) {
1543 flowRuleBuilder.makePermanent();
1544 } else {
1545 flowRuleBuilder.makeTemporary(fwd.timeout());
1546 }
1547 rules.add(flowRuleBuilder.build());
1548 return rules;
1549 }
1550
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001551 //////////////////////////////////////
1552 // Helper Methods and Classes
1553 //////////////////////////////////////
1554
1555 private boolean isSupportedEthTypeObjective(ForwardingObjective fwd) {
1556 TrafficSelector selector = fwd.selector();
1557 EthTypeCriterion ethType = (EthTypeCriterion) selector
1558 .getCriterion(Criterion.Type.ETH_TYPE);
1559 return !((ethType == null) ||
1560 ((ethType.ethType().toShort() != Ethernet.TYPE_IPV4) &&
Pier Ventree0ae7a32016-11-23 09:57:42 -08001561 (ethType.ethType().toShort() != Ethernet.MPLS_UNICAST)) &&
1562 (ethType.ethType().toShort() != Ethernet.TYPE_IPV6));
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001563 }
1564
1565 private boolean isSupportedEthDstObjective(ForwardingObjective fwd) {
1566 TrafficSelector selector = fwd.selector();
1567 EthCriterion ethDst = (EthCriterion) selector
1568 .getCriterion(Criterion.Type.ETH_DST);
1569 VlanIdCriterion vlanId = (VlanIdCriterion) selector
1570 .getCriterion(Criterion.Type.VLAN_VID);
1571 return !(ethDst == null && vlanId == null);
1572 }
1573
Saurav Das423fe2b2015-12-04 10:52:59 -08001574 protected NextGroup getGroupForNextObjective(Integer nextId) {
1575 NextGroup next = flowObjectiveStore.getNextGroup(nextId);
1576 if (next != null) {
1577 List<Deque<GroupKey>> gkeys = appKryo.deserialize(next.data());
1578 if (gkeys != null && !gkeys.isEmpty()) {
1579 return next;
1580 } else {
1581 log.warn("Empty next group found in FlowObjective store for "
1582 + "next-id:{} in dev:{}", nextId, deviceId);
1583 }
1584 } else {
1585 log.warn("next-id {} not found in Flow objective store for dev:{}",
1586 nextId, deviceId);
1587 }
1588 return null;
1589 }
1590
Charles Chan188ebf52015-12-23 00:15:11 -08001591 protected static void pass(Objective obj) {
Sho SHIMIZUef7e2902016-02-12 18:38:29 -08001592 obj.context().ifPresent(context -> context.onSuccess(obj));
Saurav Das822c4e22015-10-23 10:51:11 -07001593 }
1594
Charles Chan188ebf52015-12-23 00:15:11 -08001595 protected static void fail(Objective obj, ObjectiveError error) {
Sho SHIMIZUef7e2902016-02-12 18:38:29 -08001596 obj.context().ifPresent(context -> context.onError(obj, error));
Saurav Das822c4e22015-10-23 10:51:11 -07001597 }
Saurav Das24431192016-03-07 19:13:00 -08001598
Saurav Das24431192016-03-07 19:13:00 -08001599 @Override
1600 public List<String> getNextMappings(NextGroup nextGroup) {
1601 List<String> mappings = new ArrayList<>();
1602 List<Deque<GroupKey>> gkeys = appKryo.deserialize(nextGroup.data());
1603 for (Deque<GroupKey> gkd : gkeys) {
1604 Group lastGroup = null;
Yuta HIGUCHI2dce08a2017-04-20 21:57:48 -07001605 StringBuilder gchain = new StringBuilder();
Saurav Das24431192016-03-07 19:13:00 -08001606 for (GroupKey gk : gkd) {
1607 Group g = groupService.getGroup(deviceId, gk);
Saurav Das8be4e3a2016-03-11 17:19:07 -08001608 if (g == null) {
Saurav Das25190812016-05-27 13:54:07 -07001609 gchain.append(" NoGrp").append(" -->");
Saurav Das8be4e3a2016-03-11 17:19:07 -08001610 continue;
1611 }
1612 gchain.append(" 0x").append(Integer.toHexString(g.id().id()))
1613 .append(" -->");
Saurav Das24431192016-03-07 19:13:00 -08001614 lastGroup = g;
1615 }
1616 // add port information for last group in group-chain
Yuta HIGUCHI2dce08a2017-04-20 21:57:48 -07001617 List<Instruction> lastGroupIns = new ArrayList<>();
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001618 if (lastGroup != null && !lastGroup.buckets().buckets().isEmpty()) {
Saurav Das25190812016-05-27 13:54:07 -07001619 lastGroupIns = lastGroup.buckets().buckets().get(0)
1620 .treatment().allInstructions();
1621 }
1622 for (Instruction i: lastGroupIns) {
Saurav Das24431192016-03-07 19:13:00 -08001623 if (i instanceof OutputInstruction) {
Saurav Das8be4e3a2016-03-11 17:19:07 -08001624 gchain.append(" port:").append(((OutputInstruction) i).port());
Saurav Das24431192016-03-07 19:13:00 -08001625 }
1626 }
Saurav Das8be4e3a2016-03-11 17:19:07 -08001627 mappings.add(gchain.toString());
Saurav Das24431192016-03-07 19:13:00 -08001628 }
1629 return mappings;
1630 }
Charles Chan5b9df8d2016-03-28 22:21:40 -07001631
Pier Ventre140a8942016-11-02 07:26:38 -07001632 static boolean isMplsBos(TrafficSelector selector) {
1633 MplsBosCriterion bosCriterion = (MplsBosCriterion) selector.getCriterion(MPLS_BOS);
1634 return bosCriterion != null && bosCriterion.mplsBos();
1635 }
1636
1637 static boolean isNotMplsBos(TrafficSelector selector) {
1638 MplsBosCriterion bosCriterion = (MplsBosCriterion) selector.getCriterion(MPLS_BOS);
1639 return bosCriterion != null && !bosCriterion.mplsBos();
1640 }
1641
Charles Chand9e47c62017-10-05 15:17:15 -07001642 private static boolean isIpv6(TrafficSelector selector) {
1643 EthTypeCriterion ethTypeCriterion = (EthTypeCriterion) selector.getCriterion(ETH_TYPE);
1644 return ethTypeCriterion != null && ethTypeCriterion.ethType().toShort() == Ethernet.TYPE_IPV6;
1645 }
1646
Alex Yashchuk4caa8e82017-12-08 17:40:05 +02001647 public static VlanId readVlanFromSelector(TrafficSelector selector) {
Saurav Das59232cf2016-04-27 18:35:50 -07001648 if (selector == null) {
1649 return null;
1650 }
Charles Chan5b9df8d2016-03-28 22:21:40 -07001651 Criterion criterion = selector.getCriterion(Criterion.Type.VLAN_VID);
1652 return (criterion == null)
1653 ? null : ((VlanIdCriterion) criterion).vlanId();
1654 }
1655
Charles Chand9e47c62017-10-05 15:17:15 -07001656 static IpPrefix readIpDstFromSelector(TrafficSelector selector) {
Saurav Das59232cf2016-04-27 18:35:50 -07001657 if (selector == null) {
1658 return null;
1659 }
Charles Chan5b9df8d2016-03-28 22:21:40 -07001660 Criterion criterion = selector.getCriterion(Criterion.Type.IPV4_DST);
1661 return (criterion == null) ? null : ((IPCriterion) criterion).ip();
1662 }
Charles Chand55e84d2016-03-30 17:54:24 -07001663
1664 private static VlanId readVlanFromTreatment(TrafficTreatment treatment) {
Saurav Das59232cf2016-04-27 18:35:50 -07001665 if (treatment == null) {
1666 return null;
1667 }
Charles Chand55e84d2016-03-30 17:54:24 -07001668 for (Instruction i : treatment.allInstructions()) {
1669 if (i instanceof ModVlanIdInstruction) {
1670 return ((ModVlanIdInstruction) i).vlanId();
1671 }
1672 }
1673 return null;
1674 }
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001675
1676 /**
1677 * Utility class that retries sending flows a fixed number of times, even if
1678 * some of the attempts are successful. Used only for forwarding objectives.
1679 */
Alex Yashchuk4caa8e82017-12-08 17:40:05 +02001680 public final class RetryFlows implements Runnable {
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001681 int attempts = MAX_RETRY_ATTEMPTS;
1682 private Collection<FlowRule> retryFlows;
1683 private ForwardingObjective fwd;
1684
Alex Yashchuk4caa8e82017-12-08 17:40:05 +02001685 public RetryFlows(ForwardingObjective fwd, Collection<FlowRule> retryFlows) {
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001686 this.fwd = fwd;
1687 this.retryFlows = retryFlows;
1688 }
1689
1690 @Override
1691 public void run() {
1692 log.info("RETRY FLOWS ATTEMPT# {} for fwd:{} rules:{}",
1693 MAX_RETRY_ATTEMPTS - attempts, fwd.id(), retryFlows.size());
1694 sendForward(fwd, retryFlows);
1695 if (--attempts > 0) {
1696 executorService.schedule(this, RETRY_MS, TimeUnit.MILLISECONDS);
1697 }
1698 }
1699 }
1700
Saurav Das822c4e22015-10-23 10:51:11 -07001701}