blob: b481c3fe209814c78eb86543a5d9eb93cb6d3c48 [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;
Jonghwan Hyunf810a7a2018-02-12 16:43:45 +0900384 case MODIFY:
385 if (nextGroup == null) {
386 log.warn("Cannot modify next {} that does not exist in device {}",
387 nextObjective.id(), deviceId);
388 return;
389 }
390 log.debug("Processing NextObjective id {} in dev {} - modify bucket",
391 nextObjective.id(), deviceId);
392 groupHandler.modifyBucketFromGroup(nextObjective, nextGroup);
393 break;
Saurav Dasceccf242017-08-03 18:30:35 -0700394 case VERIFY:
395 if (nextGroup == null) {
396 log.warn("Cannot verify next {} that does not exist in device {}",
397 nextObjective.id(), deviceId);
398 return;
399 }
400 log.debug("Processing NextObjective id {} in dev {} - verify",
401 nextObjective.id(), deviceId);
402 groupHandler.verifyGroup(nextObjective, nextGroup);
403 break;
Saurav Das8a0732e2015-11-20 15:27:53 -0800404 default:
Saurav Das4f980082015-11-05 13:39:15 -0800405 log.warn("Unsupported operation {}", nextObjective.op());
Saurav Das822c4e22015-10-23 10:51:11 -0700406 }
407 }
408
409 //////////////////////////////////////
410 // Flow handling
411 //////////////////////////////////////
412
413 /**
Yi Tseng0fdc07e2017-04-24 19:17:08 -0700414 * As per OFDPA 2.0 TTP, filtering of VLAN ids and MAC addresses (for routing)
415 * configured on switch ports happen in different tables.
Saurav Das822c4e22015-10-23 10:51:11 -0700416 *
417 * @param filt the filtering objective
418 * @param install indicates whether to add or remove the objective
419 * @param applicationId the application that sent this objective
420 */
Saurav Das52025962016-01-28 22:30:01 -0800421 protected void processFilter(FilteringObjective filt,
422 boolean install, ApplicationId applicationId) {
Saurav Das822c4e22015-10-23 10:51:11 -0700423 // This driver only processes filtering criteria defined with switch
424 // ports as the key
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530425 PortCriterion portCriterion = null;
Saurav Das822c4e22015-10-23 10:51:11 -0700426 EthCriterion ethCriterion = null;
427 VlanIdCriterion vidCriterion = null;
Saurav Das822c4e22015-10-23 10:51:11 -0700428 if (!filt.key().equals(Criteria.dummy()) &&
429 filt.key().type() == Criterion.Type.IN_PORT) {
430 portCriterion = (PortCriterion) filt.key();
Saurav Das822c4e22015-10-23 10:51:11 -0700431 }
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530432 if (portCriterion == null) {
433 log.debug("No IN_PORT defined in filtering objective from app: {}",
434 applicationId);
435 } else {
436 log.debug("Received filtering objective for dev/port: {}/{}", deviceId,
437 portCriterion.port());
438 }
Saurav Das822c4e22015-10-23 10:51:11 -0700439 // convert filtering conditions for switch-intfs into flowrules
440 FlowRuleOperations.Builder ops = FlowRuleOperations.builder();
441 for (Criterion criterion : filt.conditions()) {
Yi Tseng0fdc07e2017-04-24 19:17:08 -0700442 switch (criterion.type()) {
443 case ETH_DST:
444 case ETH_DST_MASKED:
445 ethCriterion = (EthCriterion) criterion;
446 break;
447 case VLAN_VID:
448 vidCriterion = (VlanIdCriterion) criterion;
449 break;
450 default:
451 log.warn("Unsupported filter {}", criterion);
452 fail(filt, ObjectiveError.UNSUPPORTED);
453 return;
Saurav Das822c4e22015-10-23 10:51:11 -0700454 }
455 }
456
Saurav Das0e99e2b2015-10-28 12:39:42 -0700457 VlanId assignedVlan = null;
Charles Chane849c192016-01-11 18:28:54 -0800458 if (vidCriterion != null) {
Charles Chand55e84d2016-03-30 17:54:24 -0700459 // Use the VLAN in criterion if metadata is not present and the traffic is tagged
Charles Chanc550f2e2017-12-19 19:55:57 -0800460 if (!vidCriterion.vlanId().equals(VlanId.NONE)) {
Charles Chane849c192016-01-11 18:28:54 -0800461 assignedVlan = vidCriterion.vlanId();
Charles Chanc550f2e2017-12-19 19:55:57 -0800462 } else if (filt.meta() != null) {
463 assignedVlan = readVlanFromTreatment(filt.meta());
Charles Chand55e84d2016-03-30 17:54:24 -0700464 }
Charles Chane849c192016-01-11 18:28:54 -0800465
Charles Chand55e84d2016-03-30 17:54:24 -0700466 if (assignedVlan == null) {
467 log.error("Driver fails to extract VLAN information. "
Frank Wangd8ab0962017-08-11 11:09:30 +0800468 + "Not processing VLAN filters on device {}.", deviceId);
Charles Chand55e84d2016-03-30 17:54:24 -0700469 log.debug("VLAN ID in criterion={}, metadata={}",
470 readVlanFromTreatment(filt.meta()), vidCriterion.vlanId());
471 fail(filt, ObjectiveError.BADPARAMS);
472 return;
Saurav Das0e99e2b2015-10-28 12:39:42 -0700473 }
474 }
475
Yi Tseng3a77b4f2017-02-06 15:02:17 -0800476 if (ethCriterion == null || ethCriterion.mac().equals(NONE)) {
Charles Chan985b12e2016-05-11 19:47:22 -0700477 // NOTE: it is possible that a filtering objective only has vidCriterion
Saurav Das961beb22017-03-29 19:09:17 -0700478 log.warn("filtering objective missing dstMac, cannot program TMAC table");
Saurav Das822c4e22015-10-23 10:51:11 -0700479 } else {
480 for (FlowRule tmacRule : processEthDstFilter(portCriterion, ethCriterion,
Saurav Das0e99e2b2015-10-28 12:39:42 -0700481 vidCriterion, assignedVlan,
482 applicationId)) {
Saurav Das961beb22017-03-29 19:09:17 -0700483 log.trace("{} MAC filtering rules in TMAC table: {} for dev: {}",
Saurav Das018605f2017-02-18 14:05:44 -0800484 (install) ? "adding" : "removing", tmacRule, deviceId);
Charles Chanc550f2e2017-12-19 19:55:57 -0800485
486 if (install) {
487 ops = ops.add(tmacRule);
488 } else {
489 // NOTE: Only remove TMAC flow when there is no more enabled port within the
490 // same VLAN on this device if TMAC doesn't support matching on in_port.
491 if (matchInPortTmacTable() || (filt.meta() != null && filt.meta().clearedDeferred())) {
492 ops = ops.remove(tmacRule);
493 } else {
494 log.debug("Abort TMAC flow removal on {}. Some other ports still share this TMAC flow");
495 }
496 }
Saurav Das822c4e22015-10-23 10:51:11 -0700497 }
498 }
499
Charles Chan985b12e2016-05-11 19:47:22 -0700500 if (vidCriterion == null) {
501 // NOTE: it is possible that a filtering objective only has ethCriterion
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530502 log.info("filtering objective missing VLAN, cannot program VLAN Table");
Saurav Das822c4e22015-10-23 10:51:11 -0700503 } else {
Charles Chan14967c22015-12-07 11:11:50 -0800504 /*
505 * NOTE: Separate vlan filtering rules and assignment rules
506 * into different stage in order to guarantee that filtering rules
Saurav Das52025962016-01-28 22:30:01 -0800507 * always go first, as required by ofdpa.
Charles Chan14967c22015-12-07 11:11:50 -0800508 */
509 List<FlowRule> allRules = processVlanIdFilter(
510 portCriterion, vidCriterion, assignedVlan, applicationId);
511 List<FlowRule> filteringRules = new ArrayList<>();
512 List<FlowRule> assignmentRules = new ArrayList<>();
513
514 allRules.forEach(flowRule -> {
Charles Chand1172632017-03-15 17:33:09 -0700515 VlanId vlanId;
516 if (requireVlanExtensions()) {
517 ExtensionCriterion extCriterion =
518 (ExtensionCriterion) flowRule.selector().getCriterion(Criterion.Type.EXTENSION);
519 vlanId = ((OfdpaMatchVlanVid) extCriterion.extensionSelector()).vlanId();
520 } else {
521 VlanIdCriterion vlanIdCriterion =
522 (VlanIdCriterion) flowRule.selector().getCriterion(Criterion.Type.VLAN_VID);
523 vlanId = vlanIdCriterion.vlanId();
524 }
Charles Chanbe8aea42016-02-24 12:04:47 -0800525 if (!vlanId.equals(VlanId.NONE)) {
Charles Chan14967c22015-12-07 11:11:50 -0800526 filteringRules.add(flowRule);
527 } else {
528 assignmentRules.add(flowRule);
529 }
530 });
531
532 for (FlowRule filteringRule : filteringRules) {
Saurav Das961beb22017-03-29 19:09:17 -0700533 log.trace("{} VLAN filtering rule in VLAN table: {} for dev: {}",
Saurav Das018605f2017-02-18 14:05:44 -0800534 (install) ? "adding" : "removing", filteringRule, deviceId);
Charles Chan14967c22015-12-07 11:11:50 -0800535 ops = install ? ops.add(filteringRule) : ops.remove(filteringRule);
536 }
537
538 ops.newStage();
539
540 for (FlowRule assignmentRule : assignmentRules) {
Saurav Das961beb22017-03-29 19:09:17 -0700541 log.trace("{} VLAN assignment rule in VLAN table: {} for dev: {}",
Saurav Das018605f2017-02-18 14:05:44 -0800542 (install) ? "adding" : "removing", assignmentRule, deviceId);
Charles Chan14967c22015-12-07 11:11:50 -0800543 ops = install ? ops.add(assignmentRule) : ops.remove(assignmentRule);
Saurav Das822c4e22015-10-23 10:51:11 -0700544 }
545 }
546
Saurav Das822c4e22015-10-23 10:51:11 -0700547 // apply filtering flow rules
548 flowRuleService.apply(ops.build(new FlowRuleOperationsContext() {
549 @Override
550 public void onSuccess(FlowRuleOperations ops) {
Saurav Das018605f2017-02-18 14:05:44 -0800551 log.debug("Applied {} filtering rules in device {}",
Saurav Das822c4e22015-10-23 10:51:11 -0700552 ops.stages().get(0).size(), deviceId);
553 pass(filt);
554 }
555
556 @Override
557 public void onError(FlowRuleOperations ops) {
558 log.info("Failed to apply all filtering rules in dev {}", deviceId);
559 fail(filt, ObjectiveError.FLOWINSTALLATIONFAILED);
560 }
561 }));
562
563 }
564
565 /**
Charles Chand1172632017-03-15 17:33:09 -0700566 * Internal implementation of processVlanIdFilter.
567 * <p>
568 * The is_present bit in set_vlan_vid action is required to be 0 in OFDPA i12.
569 * Since it is non-OF spec, we need an extension treatment for that.
570 * The useVlanExtension must be set to false for OFDPA i12.
571 * </p>
Saurav Das0e99e2b2015-10-28 12:39:42 -0700572 *
Charles Chanf9e98652016-09-07 16:54:23 -0700573 * @param portCriterion port on device for which this filter is programmed
574 * @param vidCriterion vlan assigned to port, or NONE for untagged
575 * @param assignedVlan assigned vlan-id for untagged packets
576 * @param applicationId for application programming this filter
Saurav Das822c4e22015-10-23 10:51:11 -0700577 * @return list of FlowRule for port-vlan filters
578 */
579 protected List<FlowRule> processVlanIdFilter(PortCriterion portCriterion,
Yi Tsengef19de12017-04-24 11:33:05 -0700580 VlanIdCriterion vidCriterion,
581 VlanId assignedVlan,
582 ApplicationId applicationId) {
Charles Chan14967c22015-12-07 11:11:50 -0800583 List<FlowRule> rules = new ArrayList<>();
Saurav Das822c4e22015-10-23 10:51:11 -0700584 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
585 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
Charles Chan14967c22015-12-07 11:11:50 -0800586 TrafficSelector.Builder preSelector = null;
587 TrafficTreatment.Builder preTreatment = null;
588
Saurav Das4f980082015-11-05 13:39:15 -0800589 treatment.transition(TMAC_TABLE);
590
Saurav Das822c4e22015-10-23 10:51:11 -0700591 if (vidCriterion.vlanId() == VlanId.NONE) {
592 // untagged packets are assigned vlans
Charles Chand1172632017-03-15 17:33:09 -0700593 preSelector = DefaultTrafficSelector.builder();
594 if (requireVlanExtensions()) {
595 OfdpaMatchVlanVid ofdpaMatchVlanVid = new OfdpaMatchVlanVid(VlanId.NONE);
596 selector.extension(ofdpaMatchVlanVid, deviceId);
Charles Chanf9e98652016-09-07 16:54:23 -0700597 OfdpaSetVlanVid ofdpaSetVlanVid = new OfdpaSetVlanVid(assignedVlan);
598 treatment.extension(ofdpaSetVlanVid, deviceId);
Charles Chand1172632017-03-15 17:33:09 -0700599
600 OfdpaMatchVlanVid preOfdpaMatchVlanVid = new OfdpaMatchVlanVid(assignedVlan);
601 preSelector.extension(preOfdpaMatchVlanVid, deviceId);
Charles Chanf9e98652016-09-07 16:54:23 -0700602 } else {
Charles Chand1172632017-03-15 17:33:09 -0700603 selector.matchVlanId(VlanId.NONE);
Charles Chanf9e98652016-09-07 16:54:23 -0700604 treatment.setVlanId(assignedVlan);
Charles Chand1172632017-03-15 17:33:09 -0700605
606 preSelector.matchVlanId(assignedVlan);
607 }
608 preTreatment = DefaultTrafficTreatment.builder().transition(TMAC_TABLE);
609 } else {
610 if (requireVlanExtensions()) {
611 OfdpaMatchVlanVid ofdpaMatchVlanVid = new OfdpaMatchVlanVid(vidCriterion.vlanId());
612 selector.extension(ofdpaMatchVlanVid, deviceId);
613 } else {
614 selector.matchVlanId(vidCriterion.vlanId());
Charles Chanf9e98652016-09-07 16:54:23 -0700615 }
Charles Chan14967c22015-12-07 11:11:50 -0800616
Charles Chand55e84d2016-03-30 17:54:24 -0700617 if (!assignedVlan.equals(vidCriterion.vlanId())) {
Charles Chand1172632017-03-15 17:33:09 -0700618 if (requireVlanExtensions()) {
Charles Chanc03782d2017-01-31 13:57:55 -0800619 OfdpaSetVlanVid ofdpaSetVlanVid = new OfdpaSetVlanVid(assignedVlan);
620 treatment.extension(ofdpaSetVlanVid, deviceId);
621 } else {
622 treatment.setVlanId(assignedVlan);
623 }
Charles Chand55e84d2016-03-30 17:54:24 -0700624 }
Saurav Das822c4e22015-10-23 10:51:11 -0700625 }
Saurav Das822c4e22015-10-23 10:51:11 -0700626
627 // ofdpa cannot match on ALL portnumber, so we need to use separate
628 // rules for each port.
Charles Chan14967c22015-12-07 11:11:50 -0800629 List<PortNumber> portnums = new ArrayList<>();
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530630 if (portCriterion != null) {
631 if (PortNumber.ALL.equals(portCriterion.port())) {
632 for (Port port : deviceService.getPorts(deviceId)) {
633 if (port.number().toLong() > 0 && port.number().toLong() < OFPP_MAX) {
634 portnums.add(port.number());
635 }
Saurav Das822c4e22015-10-23 10:51:11 -0700636 }
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530637 } else {
638 portnums.add(portCriterion.port());
Saurav Das822c4e22015-10-23 10:51:11 -0700639 }
640 } else {
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530641 log.warn("Filtering Objective missing Port Criterion . " +
642 "VLAN Table cannot be programmed for {}",
643 deviceId);
Saurav Das822c4e22015-10-23 10:51:11 -0700644 }
Saurav Das4f980082015-11-05 13:39:15 -0800645
Saurav Das822c4e22015-10-23 10:51:11 -0700646 for (PortNumber pnum : portnums) {
Saurav Das4f980082015-11-05 13:39:15 -0800647 // create rest of flowrule
Saurav Das822c4e22015-10-23 10:51:11 -0700648 selector.matchInPort(pnum);
649 FlowRule rule = DefaultFlowRule.builder()
650 .forDevice(deviceId)
651 .withSelector(selector.build())
652 .withTreatment(treatment.build())
653 .withPriority(DEFAULT_PRIORITY)
654 .fromApp(applicationId)
655 .makePermanent()
656 .forTable(VLAN_TABLE).build();
Charles Chan14967c22015-12-07 11:11:50 -0800657
658 if (preSelector != null) {
659 preSelector.matchInPort(pnum);
660 FlowRule preRule = DefaultFlowRule.builder()
661 .forDevice(deviceId)
662 .withSelector(preSelector.build())
663 .withTreatment(preTreatment.build())
664 .withPriority(DEFAULT_PRIORITY)
665 .fromApp(applicationId)
666 .makePermanent()
667 .forTable(VLAN_TABLE).build();
668 rules.add(preRule);
669 }
670
Saurav Das822c4e22015-10-23 10:51:11 -0700671 rules.add(rule);
672 }
673 return rules;
674 }
675
676 /**
677 * Allows routed packets with correct destination MAC to be directed
678 * to unicast-IP routing table or MPLS forwarding table.
Saurav Das822c4e22015-10-23 10:51:11 -0700679 *
680 * @param portCriterion port on device for which this filter is programmed
681 * @param ethCriterion dstMac of device for which is filter is programmed
682 * @param vidCriterion vlan assigned to port, or NONE for untagged
Saurav Das0e99e2b2015-10-28 12:39:42 -0700683 * @param assignedVlan assigned vlan-id for untagged packets
Saurav Das822c4e22015-10-23 10:51:11 -0700684 * @param applicationId for application programming this filter
685 * @return list of FlowRule for port-vlan filters
686
687 */
688 protected List<FlowRule> processEthDstFilter(PortCriterion portCriterion,
689 EthCriterion ethCriterion,
690 VlanIdCriterion vidCriterion,
Saurav Das0e99e2b2015-10-28 12:39:42 -0700691 VlanId assignedVlan,
Saurav Das822c4e22015-10-23 10:51:11 -0700692 ApplicationId applicationId) {
Charles Chan5270ed02016-01-30 23:22:37 -0800693 // Consider PortNumber.ANY as wildcard. Match ETH_DST only
Sivachidambaram Subramanianc598d062017-05-16 22:42:06 +0530694 if (portCriterion != null && PortNumber.ANY.equals(portCriterion.port())) {
Charles Chan5270ed02016-01-30 23:22:37 -0800695 return processEthDstOnlyFilter(ethCriterion, applicationId);
696 }
697
Charles Chan5b9df8d2016-03-28 22:21:40 -0700698 // Multicast MAC
699 if (ethCriterion.mask() != null) {
Charles Chanb4879a52017-10-20 19:09:16 -0700700 return processMcastEthDstFilter(ethCriterion, assignedVlan, applicationId);
Charles Chan5b9df8d2016-03-28 22:21:40 -0700701 }
702
Saurav Das822c4e22015-10-23 10:51:11 -0700703 //handling untagged packets via assigned VLAN
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530704 if (vidCriterion != null && vidCriterion.vlanId() == VlanId.NONE) {
Saurav Das0e99e2b2015-10-28 12:39:42 -0700705 vidCriterion = (VlanIdCriterion) Criteria.matchVlanId(assignedVlan);
Saurav Das822c4e22015-10-23 10:51:11 -0700706 }
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530707 List<FlowRule> rules = new ArrayList<>();
708 OfdpaMatchVlanVid ofdpaMatchVlanVid = null;
709 if (vidCriterion != null && requireVlanExtensions()) {
710 ofdpaMatchVlanVid = new OfdpaMatchVlanVid(vidCriterion.vlanId());
711 }
Saurav Das822c4e22015-10-23 10:51:11 -0700712 // ofdpa cannot match on ALL portnumber, so we need to use separate
713 // rules for each port.
Yuta HIGUCHI2dce08a2017-04-20 21:57:48 -0700714 List<PortNumber> portnums = new ArrayList<>();
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530715 if (portCriterion != null) {
716 if (PortNumber.ALL.equals(portCriterion.port())) {
717 for (Port port : deviceService.getPorts(deviceId)) {
718 if (port.number().toLong() > 0 && port.number().toLong() < OFPP_MAX) {
719 portnums.add(port.number());
720 }
Saurav Das822c4e22015-10-23 10:51:11 -0700721 }
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530722 } else {
723 portnums.add(portCriterion.port());
724 }
725 for (PortNumber pnum : portnums) {
726 rules.add(buildTmacRuleForIpv4(ethCriterion,
727 vidCriterion,
728 ofdpaMatchVlanVid,
729 applicationId,
730 pnum));
731 rules.add(buildTmacRuleForMpls(ethCriterion,
732 vidCriterion,
733 ofdpaMatchVlanVid,
734 applicationId,
735 pnum));
736 rules.add(buildTmacRuleForIpv6(ethCriterion,
737 vidCriterion,
738 ofdpaMatchVlanVid,
739 applicationId,
740 pnum));
Saurav Das822c4e22015-10-23 10:51:11 -0700741 }
742 } else {
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530743 rules.add(buildTmacRuleForIpv4(ethCriterion,
744 vidCriterion,
745 ofdpaMatchVlanVid,
746 applicationId,
747 null));
748 rules.add(buildTmacRuleForMpls(ethCriterion,
749 vidCriterion,
750 ofdpaMatchVlanVid,
751 applicationId,
752 null));
753 rules.add(buildTmacRuleForIpv6(ethCriterion,
754 vidCriterion,
755 ofdpaMatchVlanVid,
756 applicationId,
757 null));
Saurav Das822c4e22015-10-23 10:51:11 -0700758 }
759 return rules;
760 }
761
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530762 /**
763 * Builds TMAC rules for IPv4 packets.
764 *
Charles Chanc550f2e2017-12-19 19:55:57 -0800765 * @param ethCriterion dst mac matching
766 * @param vidCriterion vlan id assigned to the port
767 * @param ofdpaMatchVlanVid OFDPA vlan id matching
768 * @param applicationId application id
769 * @param pnum port number
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530770 * @return TMAC rule for IPV4 packets
771 */
772 private FlowRule buildTmacRuleForIpv4(EthCriterion ethCriterion,
773 VlanIdCriterion vidCriterion,
774 OfdpaMatchVlanVid ofdpaMatchVlanVid,
775 ApplicationId applicationId,
776 PortNumber pnum) {
777 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
778 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
779 if (pnum != null) {
780 if (matchInPortTmacTable()) {
781 selector.matchInPort(pnum);
782 } else {
Saurav Dasf14d9ef2017-12-05 15:00:23 -0800783 log.debug("Pipeline does not support IN_PORT matching in TMAC table, " +
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530784 "ignoring the IN_PORT criteria");
785 }
786 }
787 if (vidCriterion != null) {
788 if (requireVlanExtensions()) {
789 selector.extension(ofdpaMatchVlanVid, deviceId);
790 } else {
791 selector.matchVlanId(vidCriterion.vlanId());
792 }
793 }
794 selector.matchEthType(Ethernet.TYPE_IPV4);
795 selector.matchEthDst(ethCriterion.mac());
796 treatment.transition(UNICAST_ROUTING_TABLE);
797 return DefaultFlowRule.builder()
798 .forDevice(deviceId)
799 .withSelector(selector.build())
800 .withTreatment(treatment.build())
801 .withPriority(DEFAULT_PRIORITY)
802 .fromApp(applicationId)
803 .makePermanent()
804 .forTable(TMAC_TABLE).build();
805 }
806
807 /**
808 * Builds TMAC rule for MPLS packets.
809 *
Charles Chanc550f2e2017-12-19 19:55:57 -0800810 * @param ethCriterion dst mac matching
811 * @param vidCriterion vlan id assigned to the port
812 * @param ofdpaMatchVlanVid OFDPA vlan id matching
813 * @param applicationId application id
814 * @param pnum port number
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530815 * @return TMAC rule for MPLS packets
816 */
817 private FlowRule buildTmacRuleForMpls(EthCriterion ethCriterion,
818 VlanIdCriterion vidCriterion,
819 OfdpaMatchVlanVid ofdpaMatchVlanVid,
820 ApplicationId applicationId,
821 PortNumber pnum) {
822 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
823 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
824 if (pnum != null) {
825 if (matchInPortTmacTable()) {
826 selector.matchInPort(pnum);
827 } else {
Saurav Dasf14d9ef2017-12-05 15:00:23 -0800828 log.debug("Pipeline does not support IN_PORT matching in TMAC table, " +
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530829 "ignoring the IN_PORT criteria");
830 }
831 }
832 if (vidCriterion != null) {
833 if (requireVlanExtensions()) {
834 selector.extension(ofdpaMatchVlanVid, deviceId);
835 } else {
836 selector.matchVlanId(vidCriterion.vlanId());
837 }
838 }
839 selector.matchEthType(Ethernet.MPLS_UNICAST);
840 selector.matchEthDst(ethCriterion.mac());
841 treatment.transition(MPLS_TABLE_0);
842 return DefaultFlowRule.builder()
843 .forDevice(deviceId)
844 .withSelector(selector.build())
845 .withTreatment(treatment.build())
846 .withPriority(DEFAULT_PRIORITY)
847 .fromApp(applicationId)
848 .makePermanent()
849 .forTable(TMAC_TABLE).build();
850 }
851
852 /**
853 * Builds TMAC rules for IPv6 packets.
854 *
Charles Chanc550f2e2017-12-19 19:55:57 -0800855 * @param ethCriterion dst mac matching
856 * @param vidCriterion vlan id assigned to the port
857 * @param ofdpaMatchVlanVid OFDPA vlan id matching
858 * @param applicationId application id
859 * @param pnum port number
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530860 * @return TMAC rule for IPV6 packets
861 */
862 private FlowRule buildTmacRuleForIpv6(EthCriterion ethCriterion,
863 VlanIdCriterion vidCriterion,
864 OfdpaMatchVlanVid ofdpaMatchVlanVid,
865 ApplicationId applicationId,
866 PortNumber pnum) {
867 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
868 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
869 if (pnum != null) {
870 if (matchInPortTmacTable()) {
871 selector.matchInPort(pnum);
872 } else {
Saurav Dasf14d9ef2017-12-05 15:00:23 -0800873 log.debug("Pipeline does not support IN_PORT matching in TMAC table, " +
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530874 "ignoring the IN_PORT criteria");
875 }
876 }
877 if (vidCriterion != null) {
878 if (requireVlanExtensions()) {
879 selector.extension(ofdpaMatchVlanVid, deviceId);
880 } else {
881 selector.matchVlanId(vidCriterion.vlanId());
882 }
883 }
884 selector.matchEthType(Ethernet.TYPE_IPV6);
885 selector.matchEthDst(ethCriterion.mac());
886 treatment.transition(UNICAST_ROUTING_TABLE);
887 return DefaultFlowRule.builder()
888 .forDevice(deviceId)
889 .withSelector(selector.build())
890 .withTreatment(treatment.build())
891 .withPriority(DEFAULT_PRIORITY)
892 .fromApp(applicationId)
893 .makePermanent()
894 .forTable(TMAC_TABLE).build();
895 }
896
Charles Chan5270ed02016-01-30 23:22:37 -0800897 protected List<FlowRule> processEthDstOnlyFilter(EthCriterion ethCriterion,
Yi Tsengef19de12017-04-24 11:33:05 -0700898 ApplicationId applicationId) {
Pier Luigi0e358632017-01-31 09:35:05 -0800899 ImmutableList.Builder<FlowRule> builder = ImmutableList.builder();
900
Charles Chan5270ed02016-01-30 23:22:37 -0800901 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
902 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
903 selector.matchEthType(Ethernet.TYPE_IPV4);
904 selector.matchEthDst(ethCriterion.mac());
905 treatment.transition(UNICAST_ROUTING_TABLE);
906 FlowRule rule = DefaultFlowRule.builder()
907 .forDevice(deviceId)
908 .withSelector(selector.build())
909 .withTreatment(treatment.build())
910 .withPriority(DEFAULT_PRIORITY)
911 .fromApp(applicationId)
912 .makePermanent()
913 .forTable(TMAC_TABLE).build();
Pier Luigi0e358632017-01-31 09:35:05 -0800914 builder.add(rule);
915
916 selector = DefaultTrafficSelector.builder();
917 treatment = DefaultTrafficTreatment.builder();
918 selector.matchEthType(Ethernet.TYPE_IPV6);
919 selector.matchEthDst(ethCriterion.mac());
920 treatment.transition(UNICAST_ROUTING_TABLE);
921 rule = DefaultFlowRule.builder()
922 .forDevice(deviceId)
923 .withSelector(selector.build())
924 .withTreatment(treatment.build())
925 .withPriority(DEFAULT_PRIORITY)
926 .fromApp(applicationId)
927 .makePermanent()
928 .forTable(TMAC_TABLE).build();
929 return builder.add(rule).build();
Charles Chan5270ed02016-01-30 23:22:37 -0800930 }
931
Charles Chan5b9df8d2016-03-28 22:21:40 -0700932 protected List<FlowRule> processMcastEthDstFilter(EthCriterion ethCriterion,
Charles Chanb4879a52017-10-20 19:09:16 -0700933 VlanId assignedVlan,
Yi Tsengef19de12017-04-24 11:33:05 -0700934 ApplicationId applicationId) {
Julia Ferguson65428c32017-08-10 18:15:24 +0000935 ImmutableList.Builder<FlowRule> builder = ImmutableList.builder();
Charles Chan5b9df8d2016-03-28 22:21:40 -0700936 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
937 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
Charles Chanb4879a52017-10-20 19:09:16 -0700938 FlowRule rule;
Julia Ferguson65428c32017-08-10 18:15:24 +0000939
Charles Chanb4879a52017-10-20 19:09:16 -0700940 if (IPV4_MULTICAST.equals(ethCriterion.mac())) {
941 selector.matchEthType(Ethernet.TYPE_IPV4);
942 selector.matchEthDstMasked(ethCriterion.mac(), ethCriterion.mask());
943 selector.matchVlanId(assignedVlan);
944 treatment.transition(MULTICAST_ROUTING_TABLE);
945 rule = DefaultFlowRule.builder()
946 .forDevice(deviceId)
947 .withSelector(selector.build())
948 .withTreatment(treatment.build())
949 .withPriority(DEFAULT_PRIORITY)
950 .fromApp(applicationId)
951 .makePermanent()
952 .forTable(TMAC_TABLE).build();
953 builder.add(rule);
954 }
955
956 if (IPV6_MULTICAST.equals(ethCriterion.mac())) {
957 selector = DefaultTrafficSelector.builder();
958 treatment = DefaultTrafficTreatment.builder();
959 selector.matchEthType(Ethernet.TYPE_IPV6);
960 selector.matchEthDstMasked(ethCriterion.mac(), ethCriterion.mask());
961 selector.matchVlanId(assignedVlan);
962 treatment.transition(MULTICAST_ROUTING_TABLE);
963 rule = DefaultFlowRule.builder()
964 .forDevice(deviceId)
965 .withSelector(selector.build())
966 .withTreatment(treatment.build())
967 .withPriority(DEFAULT_PRIORITY)
968 .fromApp(applicationId)
969 .makePermanent()
970 .forTable(TMAC_TABLE).build();
971 builder.add(rule);
972 }
973 return builder.build();
Charles Chan5b9df8d2016-03-28 22:21:40 -0700974 }
975
Saurav Das822c4e22015-10-23 10:51:11 -0700976 private Collection<FlowRule> processForward(ForwardingObjective fwd) {
977 switch (fwd.flag()) {
978 case SPECIFIC:
979 return processSpecific(fwd);
980 case VERSATILE:
981 return processVersatile(fwd);
982 default:
983 fail(fwd, ObjectiveError.UNKNOWN);
984 log.warn("Unknown forwarding flag {}", fwd.flag());
985 }
986 return Collections.emptySet();
987 }
988
989 /**
990 * In the OF-DPA 2.0 pipeline, versatile forwarding objectives go to the
991 * ACL table.
992 * @param fwd the forwarding objective of type 'versatile'
993 * @return a collection of flow rules to be sent to the switch. An empty
994 * collection may be returned if there is a problem in processing
995 * the flow rule
996 */
Saurav Das52025962016-01-28 22:30:01 -0800997 protected Collection<FlowRule> processVersatile(ForwardingObjective fwd) {
Saurav Das018605f2017-02-18 14:05:44 -0800998 log.debug("Processing versatile forwarding objective:{} in dev:{}",
Saurav Dasd2fded02016-12-02 15:43:47 -0800999 fwd.id(), deviceId);
Saurav Das822c4e22015-10-23 10:51:11 -07001000
1001 EthTypeCriterion ethType =
Saurav Das77b5e902016-01-27 17:01:59 -08001002 (EthTypeCriterion) fwd.selector().getCriterion(Criterion.Type.ETH_TYPE);
Saurav Das822c4e22015-10-23 10:51:11 -07001003 if (ethType == null) {
Saurav Dasd2fded02016-12-02 15:43:47 -08001004 log.error("Versatile forwarding objective:{} must include ethType",
1005 fwd.id());
Saurav Das822c4e22015-10-23 10:51:11 -07001006 fail(fwd, ObjectiveError.BADPARAMS);
1007 return Collections.emptySet();
1008 }
1009 if (fwd.nextId() == null && fwd.treatment() == null) {
1010 log.error("Forwarding objective {} from {} must contain "
1011 + "nextId or Treatment", fwd.selector(), fwd.appId());
Zsolt Haraszti9faab752016-02-17 15:55:20 -08001012 fail(fwd, ObjectiveError.BADPARAMS);
Saurav Das822c4e22015-10-23 10:51:11 -07001013 return Collections.emptySet();
1014 }
Saurav Das49cb5a12016-01-16 22:54:07 -08001015
Saurav Das77b5e902016-01-27 17:01:59 -08001016 TrafficSelector.Builder sbuilder = DefaultTrafficSelector.builder();
1017 fwd.selector().criteria().forEach(criterion -> {
1018 if (criterion instanceof VlanIdCriterion) {
1019 VlanId vlanId = ((VlanIdCriterion) criterion).vlanId();
1020 // ensure that match does not include vlan = NONE as OF-DPA does not
1021 // match untagged packets this way in the ACL table.
1022 if (vlanId.equals(VlanId.NONE)) {
1023 return;
1024 }
Charles Chand1172632017-03-15 17:33:09 -07001025 if (requireVlanExtensions()) {
1026 OfdpaMatchVlanVid ofdpaMatchVlanVid = new OfdpaMatchVlanVid(vlanId);
1027 sbuilder.extension(ofdpaMatchVlanVid, deviceId);
1028 } else {
1029 sbuilder.matchVlanId(vlanId);
1030 }
Charles Chan09bf2692018-01-11 11:48:18 -08001031 } else if (criterion instanceof Icmpv6TypeCriterion) {
1032 byte icmpv6Type = (byte) ((Icmpv6TypeCriterion) criterion).icmpv6Type();
1033 sbuilder.matchIcmpv6Type(icmpv6Type);
1034 } else if (criterion instanceof Icmpv6CodeCriterion) {
1035 byte icmpv6Code = (byte) ((Icmpv6CodeCriterion) criterion).icmpv6Code();
1036 sbuilder.matchIcmpv6Type(icmpv6Code);
Charles Chand9e47c62017-10-05 15:17:15 -07001037 } else if (criterion instanceof TcpPortCriterion || criterion instanceof UdpPortCriterion) {
1038 // FIXME: QMX switches do not support L4 dst port matching in ACL table.
1039 // Currently L4 dst port matching is only used by DHCP relay feature
1040 // and therefore is safe to be replaced with L4 src port matching.
1041 // We need to revisit this if L4 dst port is used for other purpose in the future.
1042 if (!supportIpv6L4Dst() && isIpv6(fwd.selector())) {
Charles Chand9e47c62017-10-05 15:17:15 -07001043 switch (criterion.type()) {
1044 case UDP_DST:
Charles Chand9e47c62017-10-05 15:17:15 -07001045 case UDP_DST_MASKED:
Charles Chand9e47c62017-10-05 15:17:15 -07001046 case TCP_DST:
Charles Chand9e47c62017-10-05 15:17:15 -07001047 case TCP_DST_MASKED:
Charles Chand9e47c62017-10-05 15:17:15 -07001048 break;
1049 default:
1050 sbuilder.add(criterion);
1051 }
1052 } else {
1053 sbuilder.add(criterion);
1054 }
Saurav Das77b5e902016-01-27 17:01:59 -08001055 } else {
1056 sbuilder.add(criterion);
1057 }
1058 });
1059
Saurav Das822c4e22015-10-23 10:51:11 -07001060 // XXX driver does not currently do type checking as per Tables 65-67 in
1061 // OFDPA 2.0 spec. The only allowed treatment is a punt to the controller.
Saurav Das49cb5a12016-01-16 22:54:07 -08001062 TrafficTreatment.Builder ttBuilder = DefaultTrafficTreatment.builder();
1063 if (fwd.treatment() != null) {
1064 for (Instruction ins : fwd.treatment().allInstructions()) {
1065 if (ins instanceof OutputInstruction) {
1066 OutputInstruction o = (OutputInstruction) ins;
Ray Milkeyfe0e0852018-01-18 11:14:05 -08001067 if (PortNumber.CONTROLLER.equals(o.port())) {
Saurav Das49cb5a12016-01-16 22:54:07 -08001068 ttBuilder.add(o);
1069 } else {
1070 log.warn("Only allowed treatments in versatile forwarding "
1071 + "objectives are punts to the controller");
1072 }
1073 } else {
1074 log.warn("Cannot process instruction in versatile fwd {}", ins);
1075 }
Saurav Das822c4e22015-10-23 10:51:11 -07001076 }
Charles Chan2df0e8a2017-01-09 11:45:08 -08001077 if (fwd.treatment().clearedDeferred()) {
1078 ttBuilder.wipeDeferred();
1079 }
Saurav Das822c4e22015-10-23 10:51:11 -07001080 }
Saurav Das822c4e22015-10-23 10:51:11 -07001081 if (fwd.nextId() != null) {
Saurav Das49cb5a12016-01-16 22:54:07 -08001082 // overide case
1083 NextGroup next = getGroupForNextObjective(fwd.nextId());
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +05301084 if (next == null) {
1085 fail(fwd, ObjectiveError.BADPARAMS);
1086 return Collections.emptySet();
1087 }
Saurav Das49cb5a12016-01-16 22:54:07 -08001088 List<Deque<GroupKey>> gkeys = appKryo.deserialize(next.data());
1089 // we only need the top level group's key to point the flow to it
1090 Group group = groupService.getGroup(deviceId, gkeys.get(0).peekFirst());
1091 if (group == null) {
1092 log.warn("Group with key:{} for next-id:{} not found in dev:{}",
1093 gkeys.get(0).peekFirst(), fwd.nextId(), deviceId);
1094 fail(fwd, ObjectiveError.GROUPMISSING);
1095 return Collections.emptySet();
1096 }
1097 ttBuilder.deferred().group(group.id());
Saurav Das822c4e22015-10-23 10:51:11 -07001098 }
Saurav Das49cb5a12016-01-16 22:54:07 -08001099
1100 FlowRule.Builder ruleBuilder = DefaultFlowRule.builder()
1101 .fromApp(fwd.appId())
1102 .withPriority(fwd.priority())
1103 .forDevice(deviceId)
Saurav Das77b5e902016-01-27 17:01:59 -08001104 .withSelector(sbuilder.build())
Saurav Das49cb5a12016-01-16 22:54:07 -08001105 .withTreatment(ttBuilder.build())
1106 .makePermanent()
1107 .forTable(ACL_TABLE);
1108 return Collections.singletonList(ruleBuilder.build());
Saurav Das822c4e22015-10-23 10:51:11 -07001109 }
1110
1111 /**
1112 * In the OF-DPA 2.0 pipeline, specific forwarding refers to the IP table
Saurav Das8a0732e2015-11-20 15:27:53 -08001113 * (unicast or multicast) or the L2 table (mac + vlan) or the MPLS table.
Saurav Das822c4e22015-10-23 10:51:11 -07001114 *
1115 * @param fwd the forwarding objective of type 'specific'
1116 * @return a collection of flow rules. Typically there will be only one
1117 * for this type of forwarding objective. An empty set may be
1118 * returned if there is an issue in processing the objective.
1119 */
Saurav Das8a0732e2015-11-20 15:27:53 -08001120 protected Collection<FlowRule> processSpecific(ForwardingObjective fwd) {
Saurav Das25190812016-05-27 13:54:07 -07001121 log.debug("Processing specific fwd objective:{} in dev:{} with next:{}",
Saurav Das4ce45962015-11-24 23:21:05 -08001122 fwd.id(), deviceId, fwd.nextId());
1123 boolean isEthTypeObj = isSupportedEthTypeObjective(fwd);
1124 boolean isEthDstObj = isSupportedEthDstObjective(fwd);
1125
1126 if (isEthTypeObj) {
1127 return processEthTypeSpecific(fwd);
1128 } else if (isEthDstObj) {
1129 return processEthDstSpecific(fwd);
1130 } else {
1131 log.warn("processSpecific: Unsupported forwarding objective "
1132 + "criteria fwd:{} in dev:{}", fwd.nextId(), deviceId);
Saurav Das822c4e22015-10-23 10:51:11 -07001133 fail(fwd, ObjectiveError.UNSUPPORTED);
1134 return Collections.emptySet();
1135 }
Saurav Das4ce45962015-11-24 23:21:05 -08001136 }
1137
Saurav Das4ce45962015-11-24 23:21:05 -08001138 /**
1139 * Handles forwarding rules to the IP and MPLS tables.
1140 *
1141 * @param fwd the forwarding objective
1142 * @return A collection of flow rules, or an empty set
1143 */
1144 protected Collection<FlowRule> processEthTypeSpecific(ForwardingObjective fwd) {
Charles Chancad338a2016-09-16 18:03:11 -07001145 return processEthTypeSpecificInternal(fwd, false, ACL_TABLE);
Charles Chanf9e98652016-09-07 16:54:23 -07001146 }
1147
1148 /**
1149 * Internal implementation of processEthTypeSpecific.
1150 * <p>
1151 * Wildcarded IPv4_DST is not supported in OFDPA i12. Therefore, we break
1152 * the rule into 0.0.0.0/1 and 128.0.0.0/1.
1153 * The allowDefaultRoute must be set to false for OFDPA i12.
1154 * </p>
1155 *
1156 * @param fwd the forwarding objective
1157 * @param allowDefaultRoute allow wildcarded IPv4_DST or not
Ray Milkey0bb1e102016-11-10 14:51:27 -08001158 * @param mplsNextTable next MPLS table
Charles Chanf9e98652016-09-07 16:54:23 -07001159 * @return A collection of flow rules, or an empty set
1160 */
1161 protected Collection<FlowRule> processEthTypeSpecificInternal(ForwardingObjective fwd,
Charles Chancad338a2016-09-16 18:03:11 -07001162 boolean allowDefaultRoute,
1163 int mplsNextTable) {
Saurav Das4ce45962015-11-24 23:21:05 -08001164 TrafficSelector selector = fwd.selector();
1165 EthTypeCriterion ethType =
1166 (EthTypeCriterion) selector.getCriterion(Criterion.Type.ETH_TYPE);
Flavio Castroe10fa242016-01-15 12:43:51 -08001167 boolean popMpls = false;
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001168 boolean emptyGroup = false;
Charles Chan188ebf52015-12-23 00:15:11 -08001169 int forTableId;
Saurav Das8a0732e2015-11-20 15:27:53 -08001170 TrafficSelector.Builder filteredSelector = DefaultTrafficSelector.builder();
Charles Chan14967c22015-12-07 11:11:50 -08001171 TrafficTreatment.Builder tb = DefaultTrafficTreatment.builder();
Flavio Castroe10fa242016-01-15 12:43:51 -08001172 TrafficSelector.Builder complementarySelector = DefaultTrafficSelector.builder();
Charles Chan14967c22015-12-07 11:11:50 -08001173
Saurav Das8a0732e2015-11-20 15:27:53 -08001174 if (ethType.ethType().toShort() == Ethernet.TYPE_IPV4) {
Pier Luigi3bfe32c2017-01-30 09:47:36 -08001175 if (buildIpv4Selector(filteredSelector, complementarySelector, fwd, allowDefaultRoute) < 0) {
1176 return Collections.emptyList();
1177 }
1178 // We need to set properly the next table
Flavio Castroe10fa242016-01-15 12:43:51 -08001179 IpPrefix ipv4Dst = ((IPCriterion) selector.getCriterion(Criterion.Type.IPV4_DST)).ip();
Charles Chan5b9df8d2016-03-28 22:21:40 -07001180 if (ipv4Dst.isMulticast()) {
Charles Chan5b9df8d2016-03-28 22:21:40 -07001181 forTableId = MULTICAST_ROUTING_TABLE;
Flavio Castroe10fa242016-01-15 12:43:51 -08001182 } else {
Charles Chan5b9df8d2016-03-28 22:21:40 -07001183 forTableId = UNICAST_ROUTING_TABLE;
Flavio Castroe10fa242016-01-15 12:43:51 -08001184 }
Charles Chan14967c22015-12-07 11:11:50 -08001185 if (fwd.treatment() != null) {
1186 for (Instruction instr : fwd.treatment().allInstructions()) {
1187 if (instr instanceof L3ModificationInstruction &&
1188 ((L3ModificationInstruction) instr).subtype() == L3SubType.DEC_TTL) {
Saurav Das9c705342017-01-06 11:36:01 -08001189 // XXX decrementing IP ttl is done automatically for routing, this
1190 // action is ignored or rejected in ofdpa as it is not fully implemented
1191 //tb.deferred().add(instr);
Charles Chan14967c22015-12-07 11:11:50 -08001192 }
1193 }
1194 }
1195
Pier Ventree0ae7a32016-11-23 09:57:42 -08001196 } else if (ethType.ethType().toShort() == Ethernet.TYPE_IPV6) {
1197 if (buildIpv6Selector(filteredSelector, fwd) < 0) {
1198 return Collections.emptyList();
1199 }
Kalhee Kim3d26bf02017-10-04 14:59:11 +00001200 //We need to set the proper next table
1201 IpPrefix ipv6Dst = ((IPCriterion) selector.getCriterion(Criterion.Type.IPV6_DST)).ip();
1202 if (ipv6Dst.isMulticast()) {
1203 forTableId = MULTICAST_ROUTING_TABLE;
1204 } else {
1205 forTableId = UNICAST_ROUTING_TABLE;
1206 }
1207
Pier Ventree0ae7a32016-11-23 09:57:42 -08001208 if (fwd.treatment() != null) {
1209 for (Instruction instr : fwd.treatment().allInstructions()) {
1210 if (instr instanceof L3ModificationInstruction &&
1211 ((L3ModificationInstruction) instr).subtype() == L3SubType.DEC_TTL) {
1212 // XXX decrementing IP ttl is done automatically for routing, this
1213 // action is ignored or rejected in ofdpa as it is not fully implemented
1214 //tb.deferred().add(instr);
1215 }
1216 }
1217 }
Saurav Das8a0732e2015-11-20 15:27:53 -08001218 } else {
1219 filteredSelector
1220 .matchEthType(Ethernet.MPLS_UNICAST)
1221 .matchMplsLabel(((MplsCriterion)
1222 selector.getCriterion(Criterion.Type.MPLS_LABEL)).label());
1223 MplsBosCriterion bos = (MplsBosCriterion) selector
Pier Ventre140a8942016-11-02 07:26:38 -07001224 .getCriterion(MPLS_BOS);
Alex Yashchuk4caa8e82017-12-08 17:40:05 +02001225 if (bos != null && requireMplsBosMatch()) {
Saurav Das8a0732e2015-11-20 15:27:53 -08001226 filteredSelector.matchMplsBos(bos.mplsBos());
1227 }
1228 forTableId = MPLS_TABLE_1;
Saurav Das4ce45962015-11-24 23:21:05 -08001229 log.debug("processing MPLS specific forwarding objective {} -> next:{}"
1230 + " in dev {}", fwd.id(), fwd.nextId(), deviceId);
Saurav Das822c4e22015-10-23 10:51:11 -07001231
Charles Chan14967c22015-12-07 11:11:50 -08001232 if (fwd.treatment() != null) {
1233 for (Instruction instr : fwd.treatment().allInstructions()) {
1234 if (instr instanceof L2ModificationInstruction &&
1235 ((L2ModificationInstruction) instr).subtype() == L2SubType.MPLS_POP) {
Saurav Das8a0732e2015-11-20 15:27:53 -08001236 popMpls = true;
Pier Luigi3bfe32c2017-01-30 09:47:36 -08001237 // OF-DPA does not pop in MPLS table in some cases. For the L3 VPN, it requires
Saurav Das9c705342017-01-06 11:36:01 -08001238 // setting the MPLS_TYPE so pop can happen down the pipeline
Alex Yashchuk4caa8e82017-12-08 17:40:05 +02001239 if (requireMplsPop()) {
1240 if (mplsNextTable == MPLS_TYPE_TABLE && isNotMplsBos(selector)) {
1241 tb.immediate().popMpls();
1242 }
1243 } else {
1244 // Skip mpls pop action for mpls_unicast label
1245 if (instr instanceof ModMplsHeaderInstruction &&
1246 !((ModMplsHeaderInstruction) instr).ethernetType()
1247 .equals(EtherType.MPLS_UNICAST.ethType())) {
1248 tb.immediate().add(instr);
1249 }
Pier Luigi3bfe32c2017-01-30 09:47:36 -08001250 }
Charles Chan14967c22015-12-07 11:11:50 -08001251 }
Alex Yashchuk4caa8e82017-12-08 17:40:05 +02001252
1253 if (requireMplsTtlModification()) {
1254 if (instr instanceof L3ModificationInstruction &&
1255 ((L3ModificationInstruction) instr).subtype() == L3SubType.DEC_TTL) {
1256 // FIXME Should modify the app to send the correct DEC_MPLS_TTL instruction
1257 tb.immediate().decMplsTtl();
1258 }
1259 if (instr instanceof L3ModificationInstruction &&
1260 ((L3ModificationInstruction) instr).subtype() == L3SubType.TTL_IN) {
1261 tb.immediate().add(instr);
1262 }
Charles Chan14967c22015-12-07 11:11:50 -08001263 }
Saurav Das8a0732e2015-11-20 15:27:53 -08001264 }
1265 }
1266 }
Saurav Das822c4e22015-10-23 10:51:11 -07001267
1268 if (fwd.nextId() != null) {
Saurav Das8a0732e2015-11-20 15:27:53 -08001269 if (forTableId == MPLS_TABLE_1 && !popMpls) {
1270 log.warn("SR CONTINUE case cannot be handled as MPLS ECMP "
Saurav Das25190812016-05-27 13:54:07 -07001271 + "is not implemented in OF-DPA yet. Aborting this flow {} -> next:{}"
1272 + "in this device {}", fwd.id(), fwd.nextId(), deviceId);
Pier Ventree0ae7a32016-11-23 09:57:42 -08001273 // XXX We could convert to forwarding to a single-port, via a MPLS interface,
1274 // or a MPLS SWAP (with-same) but that would have to be handled in the next-objective.
1275 // Also the pop-mpls logic used here won't work in non-BoS case.
Saurav Das4ce45962015-11-24 23:21:05 -08001276 fail(fwd, ObjectiveError.FLOWINSTALLATIONFAILED);
Saurav Das8a0732e2015-11-20 15:27:53 -08001277 return Collections.emptySet();
1278 }
1279
Saurav Das423fe2b2015-12-04 10:52:59 -08001280 NextGroup next = getGroupForNextObjective(fwd.nextId());
1281 if (next != null) {
1282 List<Deque<GroupKey>> gkeys = appKryo.deserialize(next.data());
1283 // we only need the top level group's key to point the flow to it
1284 Group group = groupService.getGroup(deviceId, gkeys.get(0).peekFirst());
Pier Luigi075f1012018-02-01 10:23:12 +01001285 if (isNotMplsBos(selector) && group.type().equals(SELECT)) {
Pier Ventre140a8942016-11-02 07:26:38 -07001286 log.warn("SR CONTINUE case cannot be handled as MPLS ECMP "
1287 + "is not implemented in OF-DPA yet. Aborting this flow {} -> next:{}"
1288 + "in this device {}", fwd.id(), fwd.nextId(), deviceId);
1289 fail(fwd, ObjectiveError.FLOWINSTALLATIONFAILED);
1290 return Collections.emptySet();
1291 }
Saurav Das423fe2b2015-12-04 10:52:59 -08001292 if (group == null) {
1293 log.warn("Group with key:{} for next-id:{} not found in dev:{}",
1294 gkeys.get(0).peekFirst(), fwd.nextId(), deviceId);
1295 fail(fwd, ObjectiveError.GROUPMISSING);
1296 return Collections.emptySet();
1297 }
1298 tb.deferred().group(group.id());
Saurav Dasc568c342018-01-25 09:49:01 -08001299 // retrying flows may be necessary due to bug CORD-554
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001300 if (gkeys.size() == 1 && gkeys.get(0).size() == 1) {
Saurav Dasc568c342018-01-25 09:49:01 -08001301 if (shouldRetry()) {
1302 log.warn("Found empty group 0x{} in dev:{} .. will retry fwd:{}",
1303 Integer.toHexString(group.id().id()), deviceId,
1304 fwd.id());
1305 emptyGroup = true;
1306 }
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001307 }
Saurav Das25190812016-05-27 13:54:07 -07001308 } else {
1309 log.warn("Cannot find group for nextId:{} in dev:{}. Aborting fwd:{}",
1310 fwd.nextId(), deviceId, fwd.id());
1311 fail(fwd, ObjectiveError.FLOWINSTALLATIONFAILED);
1312 return Collections.emptySet();
Saurav Das822c4e22015-10-23 10:51:11 -07001313 }
Saurav Das822c4e22015-10-23 10:51:11 -07001314 }
Charles Chancad338a2016-09-16 18:03:11 -07001315
1316 if (forTableId == MPLS_TABLE_1) {
Pier Ventre140a8942016-11-02 07:26:38 -07001317 if (mplsNextTable == MPLS_L3_TYPE_TABLE) {
Charles Chancad338a2016-09-16 18:03:11 -07001318 Ofdpa3SetMplsType setMplsType = new Ofdpa3SetMplsType(Ofdpa3MplsType.L3_PHP);
Saurav Das9c705342017-01-06 11:36:01 -08001319 // set mpls type as apply_action
1320 tb.immediate().extension(setMplsType, deviceId);
Charles Chancad338a2016-09-16 18:03:11 -07001321 }
1322 tb.transition(mplsNextTable);
1323 } else {
1324 tb.transition(ACL_TABLE);
1325 }
1326
Saurav Das822c4e22015-10-23 10:51:11 -07001327 FlowRule.Builder ruleBuilder = DefaultFlowRule.builder()
1328 .fromApp(fwd.appId())
1329 .withPriority(fwd.priority())
1330 .forDevice(deviceId)
Saurav Das8a0732e2015-11-20 15:27:53 -08001331 .withSelector(filteredSelector.build())
1332 .withTreatment(tb.build())
1333 .forTable(forTableId);
Saurav Das822c4e22015-10-23 10:51:11 -07001334
1335 if (fwd.permanent()) {
1336 ruleBuilder.makePermanent();
1337 } else {
1338 ruleBuilder.makeTemporary(fwd.timeout());
1339 }
Flavio Castroe10fa242016-01-15 12:43:51 -08001340 Collection<FlowRule> flowRuleCollection = new ArrayList<>();
1341 flowRuleCollection.add(ruleBuilder.build());
Pier Luigi3bfe32c2017-01-30 09:47:36 -08001342 if (!allowDefaultRoute) {
Pier Ventree0ae7a32016-11-23 09:57:42 -08001343 flowRuleCollection.add(
1344 defaultRoute(fwd, complementarySelector, forTableId, tb)
1345 );
Flavio Castroe10fa242016-01-15 12:43:51 -08001346 log.debug("Default rule 0.0.0.0/0 is being installed two rules");
1347 }
Saurav Dasc568c342018-01-25 09:49:01 -08001348
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001349 if (emptyGroup) {
1350 executorService.schedule(new RetryFlows(fwd, flowRuleCollection),
1351 RETRY_MS, TimeUnit.MILLISECONDS);
1352 }
Flavio Castroe10fa242016-01-15 12:43:51 -08001353 return flowRuleCollection;
Saurav Das822c4e22015-10-23 10:51:11 -07001354 }
1355
Pier Luigi3bfe32c2017-01-30 09:47:36 -08001356 protected int buildIpv4Selector(TrafficSelector.Builder builderToUpdate,
1357 TrafficSelector.Builder extBuilder,
1358 ForwardingObjective fwd,
1359 boolean allowDefaultRoute) {
1360 TrafficSelector selector = fwd.selector();
1361
1362 IpPrefix ipv4Dst = ((IPCriterion) selector.getCriterion(Criterion.Type.IPV4_DST)).ip();
1363 if (ipv4Dst.isMulticast()) {
1364 if (ipv4Dst.prefixLength() != 32) {
1365 log.warn("Multicast specific forwarding objective can only be /32");
1366 fail(fwd, ObjectiveError.BADPARAMS);
1367 return -1;
1368 }
1369 VlanId assignedVlan = readVlanFromSelector(fwd.meta());
1370 if (assignedVlan == null) {
1371 log.warn("VLAN ID required by multicast specific fwd obj is missing. Abort.");
1372 fail(fwd, ObjectiveError.BADPARAMS);
1373 return -1;
1374 }
Charles Chand1172632017-03-15 17:33:09 -07001375 if (requireVlanExtensions()) {
1376 OfdpaMatchVlanVid ofdpaMatchVlanVid = new OfdpaMatchVlanVid(assignedVlan);
1377 builderToUpdate.extension(ofdpaMatchVlanVid, deviceId);
1378 } else {
1379 builderToUpdate.matchVlanId(assignedVlan);
1380 }
Pier Luigi3bfe32c2017-01-30 09:47:36 -08001381 builderToUpdate.matchEthType(Ethernet.TYPE_IPV4).matchIPDst(ipv4Dst);
1382 log.debug("processing IPv4 multicast specific forwarding objective {} -> next:{}"
1383 + " in dev:{}", fwd.id(), fwd.nextId(), deviceId);
1384 } else {
1385 if (ipv4Dst.prefixLength() == 0) {
1386 if (allowDefaultRoute) {
1387 // The entire IPV4_DST field is wildcarded intentionally
1388 builderToUpdate.matchEthType(Ethernet.TYPE_IPV4);
1389 } else {
1390 // NOTE: The switch does not support matching 0.0.0.0/0
1391 // Split it into 0.0.0.0/1 and 128.0.0.0/1
1392 builderToUpdate.matchEthType(Ethernet.TYPE_IPV4)
1393 .matchIPDst(IpPrefix.valueOf("0.0.0.0/1"));
1394 extBuilder.matchEthType(Ethernet.TYPE_IPV4)
1395 .matchIPDst(IpPrefix.valueOf("128.0.0.0/1"));
1396 }
1397 } else {
1398 builderToUpdate.matchEthType(Ethernet.TYPE_IPV4).matchIPDst(ipv4Dst);
1399 }
1400 log.debug("processing IPv4 unicast specific forwarding objective {} -> next:{}"
1401 + " in dev:{}", fwd.id(), fwd.nextId(), deviceId);
1402 }
1403 return 0;
1404 }
1405
1406 /**
1407 * Helper method to build Ipv6 selector using the selector provided by
1408 * a forwarding objective.
1409 *
1410 * @param builderToUpdate the builder to update
1411 * @param fwd the selector to read
1412 * @return 0 if the update ends correctly. -1 if the matches
1413 * are not yet supported
1414 */
1415 protected int buildIpv6Selector(TrafficSelector.Builder builderToUpdate,
1416 ForwardingObjective fwd) {
1417
1418 TrafficSelector selector = fwd.selector();
1419
1420 IpPrefix ipv6Dst = ((IPCriterion) selector.getCriterion(Criterion.Type.IPV6_DST)).ip();
1421 if (ipv6Dst.isMulticast()) {
Julia Ferguson65428c32017-08-10 18:15:24 +00001422 if (ipv6Dst.prefixLength() != IpAddress.INET6_BIT_LENGTH) {
1423 log.warn("Multicast specific forwarding objective can only be /128");
1424 fail(fwd, ObjectiveError.BADPARAMS);
1425 return -1;
1426 }
1427 VlanId assignedVlan = readVlanFromSelector(fwd.meta());
1428 if (assignedVlan == null) {
1429 log.warn("VLAN ID required by multicast specific fwd obj is missing. Abort.");
1430 fail(fwd, ObjectiveError.BADPARAMS);
1431 return -1;
1432 }
1433 if (requireVlanExtensions()) {
1434 OfdpaMatchVlanVid ofdpaMatchVlanVid = new OfdpaMatchVlanVid(assignedVlan);
1435 builderToUpdate.extension(ofdpaMatchVlanVid, deviceId);
1436 } else {
1437 builderToUpdate.matchVlanId(assignedVlan);
1438 }
1439 builderToUpdate.matchEthType(Ethernet.TYPE_IPV6).matchIPv6Dst(ipv6Dst);
1440 log.debug("processing IPv6 multicast specific forwarding objective {} -> next:{}"
1441 + " in dev:{}", fwd.id(), fwd.nextId(), deviceId);
1442 } else {
1443 if (ipv6Dst.prefixLength() != 0) {
1444 builderToUpdate.matchIPv6Dst(ipv6Dst);
1445 }
Pier Luigi3bfe32c2017-01-30 09:47:36 -08001446 builderToUpdate.matchEthType(Ethernet.TYPE_IPV6);
1447 log.debug("processing IPv6 unicast specific forwarding objective {} -> next:{}"
1448 + " in dev:{}", fwd.id(), fwd.nextId(), deviceId);
Julia Ferguson65428c32017-08-10 18:15:24 +00001449 }
Pier Luigi3bfe32c2017-01-30 09:47:36 -08001450 return 0;
1451 }
1452
Pier Ventree0ae7a32016-11-23 09:57:42 -08001453 protected FlowRule defaultRoute(ForwardingObjective fwd,
1454 TrafficSelector.Builder complementarySelector,
1455 int forTableId,
1456 TrafficTreatment.Builder tb) {
1457 FlowRule.Builder rule = DefaultFlowRule.builder()
1458 .fromApp(fwd.appId())
1459 .withPriority(fwd.priority())
1460 .forDevice(deviceId)
1461 .withSelector(complementarySelector.build())
1462 .withTreatment(tb.build())
1463 .forTable(forTableId);
1464 if (fwd.permanent()) {
1465 rule.makePermanent();
1466 } else {
1467 rule.makeTemporary(fwd.timeout());
1468 }
1469 return rule.build();
1470 }
1471
Saurav Das4ce45962015-11-24 23:21:05 -08001472 /**
1473 * Handles forwarding rules to the L2 bridging table. Flow actions are not
1474 * allowed in the bridging table - instead we use L2 Interface group or
1475 * L2 flood group
1476 *
1477 * @param fwd the forwarding objective
1478 * @return A collection of flow rules, or an empty set
1479 */
1480 protected Collection<FlowRule> processEthDstSpecific(ForwardingObjective fwd) {
1481 List<FlowRule> rules = new ArrayList<>();
1482
1483 // Build filtered selector
1484 TrafficSelector selector = fwd.selector();
1485 EthCriterion ethCriterion = (EthCriterion) selector
1486 .getCriterion(Criterion.Type.ETH_DST);
1487 VlanIdCriterion vlanIdCriterion = (VlanIdCriterion) selector
1488 .getCriterion(Criterion.Type.VLAN_VID);
1489
1490 if (vlanIdCriterion == null) {
1491 log.warn("Forwarding objective for bridging requires vlan. Not "
1492 + "installing fwd:{} in dev:{}", fwd.id(), deviceId);
1493 fail(fwd, ObjectiveError.BADPARAMS);
1494 return Collections.emptySet();
1495 }
1496
1497 TrafficSelector.Builder filteredSelectorBuilder =
1498 DefaultTrafficSelector.builder();
Yi Tseng3a77b4f2017-02-06 15:02:17 -08001499
1500 if (!ethCriterion.mac().equals(NONE) &&
1501 !ethCriterion.mac().equals(BROADCAST)) {
Saurav Das4ce45962015-11-24 23:21:05 -08001502 filteredSelectorBuilder.matchEthDst(ethCriterion.mac());
1503 log.debug("processing L2 forwarding objective:{} -> next:{} in dev:{}",
1504 fwd.id(), fwd.nextId(), deviceId);
1505 } else {
Yi Tseng3a77b4f2017-02-06 15:02:17 -08001506 // Use wildcard DST_MAC if the MacAddress is None or Broadcast
Saurav Das4ce45962015-11-24 23:21:05 -08001507 log.debug("processing L2 Broadcast forwarding objective:{} -> next:{} "
1508 + "in dev:{} for vlan:{}",
1509 fwd.id(), fwd.nextId(), deviceId, vlanIdCriterion.vlanId());
1510 }
Charles Chand1172632017-03-15 17:33:09 -07001511 if (requireVlanExtensions()) {
1512 OfdpaMatchVlanVid ofdpaMatchVlanVid = new OfdpaMatchVlanVid(vlanIdCriterion.vlanId());
1513 filteredSelectorBuilder.extension(ofdpaMatchVlanVid, deviceId);
1514 } else {
1515 filteredSelectorBuilder.matchVlanId(vlanIdCriterion.vlanId());
1516 }
Saurav Das4ce45962015-11-24 23:21:05 -08001517 TrafficSelector filteredSelector = filteredSelectorBuilder.build();
1518
1519 if (fwd.treatment() != null) {
1520 log.warn("Ignoring traffic treatment in fwd rule {} meant for L2 table"
1521 + "for dev:{}. Expecting only nextId", fwd.id(), deviceId);
1522 }
1523
1524 TrafficTreatment.Builder treatmentBuilder = DefaultTrafficTreatment.builder();
1525 if (fwd.nextId() != null) {
Saurav Das423fe2b2015-12-04 10:52:59 -08001526 NextGroup next = getGroupForNextObjective(fwd.nextId());
Saurav Das4ce45962015-11-24 23:21:05 -08001527 if (next != null) {
1528 List<Deque<GroupKey>> gkeys = appKryo.deserialize(next.data());
1529 // we only need the top level group's key to point the flow to it
1530 Group group = groupService.getGroup(deviceId, gkeys.get(0).peekFirst());
1531 if (group != null) {
1532 treatmentBuilder.deferred().group(group.id());
1533 } else {
1534 log.warn("Group with key:{} for next-id:{} not found in dev:{}",
1535 gkeys.get(0).peekFirst(), fwd.nextId(), deviceId);
1536 fail(fwd, ObjectiveError.GROUPMISSING);
1537 return Collections.emptySet();
1538 }
1539 }
1540 }
1541 treatmentBuilder.immediate().transition(ACL_TABLE);
1542 TrafficTreatment filteredTreatment = treatmentBuilder.build();
1543
1544 // Build bridging table entries
1545 FlowRule.Builder flowRuleBuilder = DefaultFlowRule.builder();
1546 flowRuleBuilder.fromApp(fwd.appId())
1547 .withPriority(fwd.priority())
1548 .forDevice(deviceId)
1549 .withSelector(filteredSelector)
1550 .withTreatment(filteredTreatment)
1551 .forTable(BRIDGING_TABLE);
1552 if (fwd.permanent()) {
1553 flowRuleBuilder.makePermanent();
1554 } else {
1555 flowRuleBuilder.makeTemporary(fwd.timeout());
1556 }
1557 rules.add(flowRuleBuilder.build());
1558 return rules;
1559 }
1560
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001561 //////////////////////////////////////
1562 // Helper Methods and Classes
1563 //////////////////////////////////////
1564
1565 private boolean isSupportedEthTypeObjective(ForwardingObjective fwd) {
1566 TrafficSelector selector = fwd.selector();
1567 EthTypeCriterion ethType = (EthTypeCriterion) selector
1568 .getCriterion(Criterion.Type.ETH_TYPE);
1569 return !((ethType == null) ||
1570 ((ethType.ethType().toShort() != Ethernet.TYPE_IPV4) &&
Pier Ventree0ae7a32016-11-23 09:57:42 -08001571 (ethType.ethType().toShort() != Ethernet.MPLS_UNICAST)) &&
1572 (ethType.ethType().toShort() != Ethernet.TYPE_IPV6));
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001573 }
1574
1575 private boolean isSupportedEthDstObjective(ForwardingObjective fwd) {
1576 TrafficSelector selector = fwd.selector();
1577 EthCriterion ethDst = (EthCriterion) selector
1578 .getCriterion(Criterion.Type.ETH_DST);
1579 VlanIdCriterion vlanId = (VlanIdCriterion) selector
1580 .getCriterion(Criterion.Type.VLAN_VID);
1581 return !(ethDst == null && vlanId == null);
1582 }
1583
Saurav Das423fe2b2015-12-04 10:52:59 -08001584 protected NextGroup getGroupForNextObjective(Integer nextId) {
1585 NextGroup next = flowObjectiveStore.getNextGroup(nextId);
1586 if (next != null) {
1587 List<Deque<GroupKey>> gkeys = appKryo.deserialize(next.data());
1588 if (gkeys != null && !gkeys.isEmpty()) {
1589 return next;
1590 } else {
1591 log.warn("Empty next group found in FlowObjective store for "
1592 + "next-id:{} in dev:{}", nextId, deviceId);
1593 }
1594 } else {
1595 log.warn("next-id {} not found in Flow objective store for dev:{}",
1596 nextId, deviceId);
1597 }
1598 return null;
1599 }
1600
Charles Chan188ebf52015-12-23 00:15:11 -08001601 protected static void pass(Objective obj) {
Sho SHIMIZUef7e2902016-02-12 18:38:29 -08001602 obj.context().ifPresent(context -> context.onSuccess(obj));
Saurav Das822c4e22015-10-23 10:51:11 -07001603 }
1604
Charles Chan188ebf52015-12-23 00:15:11 -08001605 protected static void fail(Objective obj, ObjectiveError error) {
Sho SHIMIZUef7e2902016-02-12 18:38:29 -08001606 obj.context().ifPresent(context -> context.onError(obj, error));
Saurav Das822c4e22015-10-23 10:51:11 -07001607 }
Saurav Das24431192016-03-07 19:13:00 -08001608
Saurav Das24431192016-03-07 19:13:00 -08001609 @Override
1610 public List<String> getNextMappings(NextGroup nextGroup) {
1611 List<String> mappings = new ArrayList<>();
1612 List<Deque<GroupKey>> gkeys = appKryo.deserialize(nextGroup.data());
1613 for (Deque<GroupKey> gkd : gkeys) {
1614 Group lastGroup = null;
Yuta HIGUCHI2dce08a2017-04-20 21:57:48 -07001615 StringBuilder gchain = new StringBuilder();
Saurav Das24431192016-03-07 19:13:00 -08001616 for (GroupKey gk : gkd) {
1617 Group g = groupService.getGroup(deviceId, gk);
Saurav Das8be4e3a2016-03-11 17:19:07 -08001618 if (g == null) {
Saurav Das25190812016-05-27 13:54:07 -07001619 gchain.append(" NoGrp").append(" -->");
Saurav Das8be4e3a2016-03-11 17:19:07 -08001620 continue;
1621 }
1622 gchain.append(" 0x").append(Integer.toHexString(g.id().id()))
1623 .append(" -->");
Saurav Das24431192016-03-07 19:13:00 -08001624 lastGroup = g;
1625 }
1626 // add port information for last group in group-chain
Yuta HIGUCHI2dce08a2017-04-20 21:57:48 -07001627 List<Instruction> lastGroupIns = new ArrayList<>();
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001628 if (lastGroup != null && !lastGroup.buckets().buckets().isEmpty()) {
Saurav Das25190812016-05-27 13:54:07 -07001629 lastGroupIns = lastGroup.buckets().buckets().get(0)
1630 .treatment().allInstructions();
1631 }
1632 for (Instruction i: lastGroupIns) {
Saurav Das24431192016-03-07 19:13:00 -08001633 if (i instanceof OutputInstruction) {
Saurav Das8be4e3a2016-03-11 17:19:07 -08001634 gchain.append(" port:").append(((OutputInstruction) i).port());
Saurav Das24431192016-03-07 19:13:00 -08001635 }
1636 }
Saurav Das8be4e3a2016-03-11 17:19:07 -08001637 mappings.add(gchain.toString());
Saurav Das24431192016-03-07 19:13:00 -08001638 }
1639 return mappings;
1640 }
Charles Chan5b9df8d2016-03-28 22:21:40 -07001641
Pier Ventre140a8942016-11-02 07:26:38 -07001642 static boolean isMplsBos(TrafficSelector selector) {
1643 MplsBosCriterion bosCriterion = (MplsBosCriterion) selector.getCriterion(MPLS_BOS);
1644 return bosCriterion != null && bosCriterion.mplsBos();
1645 }
1646
1647 static boolean isNotMplsBos(TrafficSelector selector) {
1648 MplsBosCriterion bosCriterion = (MplsBosCriterion) selector.getCriterion(MPLS_BOS);
1649 return bosCriterion != null && !bosCriterion.mplsBos();
1650 }
1651
Charles Chand9e47c62017-10-05 15:17:15 -07001652 private static boolean isIpv6(TrafficSelector selector) {
1653 EthTypeCriterion ethTypeCriterion = (EthTypeCriterion) selector.getCriterion(ETH_TYPE);
1654 return ethTypeCriterion != null && ethTypeCriterion.ethType().toShort() == Ethernet.TYPE_IPV6;
1655 }
1656
Alex Yashchuk4caa8e82017-12-08 17:40:05 +02001657 public static VlanId readVlanFromSelector(TrafficSelector selector) {
Saurav Das59232cf2016-04-27 18:35:50 -07001658 if (selector == null) {
1659 return null;
1660 }
Charles Chan5b9df8d2016-03-28 22:21:40 -07001661 Criterion criterion = selector.getCriterion(Criterion.Type.VLAN_VID);
1662 return (criterion == null)
1663 ? null : ((VlanIdCriterion) criterion).vlanId();
1664 }
1665
Charles Chand9e47c62017-10-05 15:17:15 -07001666 static IpPrefix readIpDstFromSelector(TrafficSelector selector) {
Saurav Das59232cf2016-04-27 18:35:50 -07001667 if (selector == null) {
1668 return null;
1669 }
Charles Chan5b9df8d2016-03-28 22:21:40 -07001670 Criterion criterion = selector.getCriterion(Criterion.Type.IPV4_DST);
1671 return (criterion == null) ? null : ((IPCriterion) criterion).ip();
1672 }
Charles Chand55e84d2016-03-30 17:54:24 -07001673
1674 private static VlanId readVlanFromTreatment(TrafficTreatment treatment) {
Saurav Das59232cf2016-04-27 18:35:50 -07001675 if (treatment == null) {
1676 return null;
1677 }
Charles Chand55e84d2016-03-30 17:54:24 -07001678 for (Instruction i : treatment.allInstructions()) {
1679 if (i instanceof ModVlanIdInstruction) {
1680 return ((ModVlanIdInstruction) i).vlanId();
1681 }
1682 }
1683 return null;
1684 }
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001685
1686 /**
1687 * Utility class that retries sending flows a fixed number of times, even if
1688 * some of the attempts are successful. Used only for forwarding objectives.
1689 */
Alex Yashchuk4caa8e82017-12-08 17:40:05 +02001690 public final class RetryFlows implements Runnable {
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001691 int attempts = MAX_RETRY_ATTEMPTS;
1692 private Collection<FlowRule> retryFlows;
1693 private ForwardingObjective fwd;
1694
Alex Yashchuk4caa8e82017-12-08 17:40:05 +02001695 public RetryFlows(ForwardingObjective fwd, Collection<FlowRule> retryFlows) {
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001696 this.fwd = fwd;
1697 this.retryFlows = retryFlows;
1698 }
1699
1700 @Override
1701 public void run() {
1702 log.info("RETRY FLOWS ATTEMPT# {} for fwd:{} rules:{}",
1703 MAX_RETRY_ATTEMPTS - attempts, fwd.id(), retryFlows.size());
1704 sendForward(fwd, retryFlows);
1705 if (--attempts > 0) {
1706 executorService.schedule(this, RETRY_MS, TimeUnit.MILLISECONDS);
1707 }
1708 }
1709 }
1710
Saurav Das822c4e22015-10-23 10:51:11 -07001711}