blob: 430d4241bb15a44beb7a761a4670fb4a66c51cee [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;
Julia Fergusond8f145e2017-08-10 18:15:24 +000022import org.onlab.packet.IpAddress;
Charles Chan14967c22015-12-07 11:11:50 -080023import org.onlab.packet.IpPrefix;
Saurav Das822c4e22015-10-23 10:51:11 -070024import org.onlab.packet.VlanId;
25import org.onlab.util.KryoNamespace;
26import org.onosproject.core.ApplicationId;
27import org.onosproject.core.CoreService;
Charles Chancad338a2016-09-16 18:03:11 -070028import org.onosproject.driver.extensions.Ofdpa3MplsType;
29import org.onosproject.driver.extensions.Ofdpa3SetMplsType;
Charles Chan14967c22015-12-07 11:11:50 -080030import org.onosproject.driver.extensions.OfdpaMatchVlanVid;
31import org.onosproject.driver.extensions.OfdpaSetVlanVid;
Saurav Das822c4e22015-10-23 10:51:11 -070032import org.onosproject.net.DeviceId;
33import org.onosproject.net.Port;
34import org.onosproject.net.PortNumber;
35import org.onosproject.net.behaviour.NextGroup;
36import org.onosproject.net.behaviour.Pipeliner;
37import org.onosproject.net.behaviour.PipelinerContext;
38import org.onosproject.net.device.DeviceService;
39import org.onosproject.net.driver.AbstractHandlerBehaviour;
40import org.onosproject.net.flow.DefaultFlowRule;
41import org.onosproject.net.flow.DefaultTrafficSelector;
42import org.onosproject.net.flow.DefaultTrafficTreatment;
43import org.onosproject.net.flow.FlowRule;
44import org.onosproject.net.flow.FlowRuleOperations;
45import org.onosproject.net.flow.FlowRuleOperationsContext;
46import org.onosproject.net.flow.FlowRuleService;
47import org.onosproject.net.flow.TrafficSelector;
48import org.onosproject.net.flow.TrafficTreatment;
49import org.onosproject.net.flow.criteria.Criteria;
50import org.onosproject.net.flow.criteria.Criterion;
51import org.onosproject.net.flow.criteria.EthCriterion;
52import org.onosproject.net.flow.criteria.EthTypeCriterion;
Charles Chan14967c22015-12-07 11:11:50 -080053import org.onosproject.net.flow.criteria.ExtensionCriterion;
Saurav Das822c4e22015-10-23 10:51:11 -070054import org.onosproject.net.flow.criteria.IPCriterion;
Pier Ventree0ae7a32016-11-23 09:57:42 -080055import org.onosproject.net.flow.criteria.Icmpv6CodeCriterion;
56import org.onosproject.net.flow.criteria.Icmpv6TypeCriterion;
Saurav Das8a0732e2015-11-20 15:27:53 -080057import org.onosproject.net.flow.criteria.MplsBosCriterion;
58import org.onosproject.net.flow.criteria.MplsCriterion;
Saurav Das822c4e22015-10-23 10:51:11 -070059import org.onosproject.net.flow.criteria.PortCriterion;
Charles Chan30122a72017-10-05 15:17:15 -070060import org.onosproject.net.flow.criteria.TcpPortCriterion;
61import org.onosproject.net.flow.criteria.UdpPortCriterion;
Saurav Das822c4e22015-10-23 10:51:11 -070062import org.onosproject.net.flow.criteria.VlanIdCriterion;
63import org.onosproject.net.flow.instructions.Instruction;
64import org.onosproject.net.flow.instructions.Instructions.OutputInstruction;
65import org.onosproject.net.flow.instructions.L2ModificationInstruction;
Saurav Das8a0732e2015-11-20 15:27:53 -080066import org.onosproject.net.flow.instructions.L2ModificationInstruction.L2SubType;
Saurav Das822c4e22015-10-23 10:51:11 -070067import org.onosproject.net.flow.instructions.L2ModificationInstruction.ModVlanIdInstruction;
Charles Chan14967c22015-12-07 11:11:50 -080068import org.onosproject.net.flow.instructions.L3ModificationInstruction;
69import org.onosproject.net.flow.instructions.L3ModificationInstruction.L3SubType;
Saurav Das822c4e22015-10-23 10:51:11 -070070import org.onosproject.net.flowobjective.FilteringObjective;
71import org.onosproject.net.flowobjective.FlowObjectiveStore;
72import org.onosproject.net.flowobjective.ForwardingObjective;
73import org.onosproject.net.flowobjective.NextObjective;
74import org.onosproject.net.flowobjective.Objective;
75import org.onosproject.net.flowobjective.ObjectiveError;
Saurav Das822c4e22015-10-23 10:51:11 -070076import org.onosproject.net.group.DefaultGroupKey;
77import org.onosproject.net.group.Group;
Saurav Das822c4e22015-10-23 10:51:11 -070078import org.onosproject.net.group.GroupKey;
Saurav Das822c4e22015-10-23 10:51:11 -070079import org.onosproject.net.group.GroupService;
Saurav Das822c4e22015-10-23 10:51:11 -070080import org.onosproject.store.serializers.KryoNamespaces;
81import org.slf4j.Logger;
82
Pier Ventree0ae7a32016-11-23 09:57:42 -080083import java.util.ArrayDeque;
84import java.util.ArrayList;
85import java.util.Collection;
86import java.util.Collections;
87import java.util.Deque;
88import java.util.List;
89import java.util.Objects;
Pier Ventree0ae7a32016-11-23 09:57:42 -080090import java.util.concurrent.ScheduledExecutorService;
91import java.util.concurrent.TimeUnit;
92
93import static java.util.concurrent.Executors.newScheduledThreadPool;
Yi Tseng3a77b4f2017-02-06 15:02:17 -080094import static org.onlab.packet.MacAddress.BROADCAST;
Charles Chan206cac02017-10-20 19:09:16 -070095import static org.onlab.packet.MacAddress.IPV4_MULTICAST;
96import static org.onlab.packet.MacAddress.IPV6_MULTICAST;
Yi Tseng3a77b4f2017-02-06 15:02:17 -080097import static org.onlab.packet.MacAddress.NONE;
Pier Ventree0ae7a32016-11-23 09:57:42 -080098import static org.onlab.util.Tools.groupedThreads;
Yi Tsengef19de12017-04-24 11:33:05 -070099import static org.onosproject.driver.pipeline.ofdpa.OfdpaGroupHandlerUtility.*;
Charles Chan30122a72017-10-05 15:17:15 -0700100import static org.onosproject.net.flow.criteria.Criterion.Type.ETH_TYPE;
Pier Ventree0ae7a32016-11-23 09:57:42 -0800101import static org.slf4j.LoggerFactory.getLogger;
Pier Ventre140a8942016-11-02 07:26:38 -0700102import static org.onosproject.net.flow.criteria.Criterion.Type.MPLS_BOS;
103import static org.onosproject.net.flowobjective.NextObjective.Type.HASHED;
Pier Ventree0ae7a32016-11-23 09:57:42 -0800104
Saurav Das822c4e22015-10-23 10:51:11 -0700105/**
106 * Driver for Broadcom's OF-DPA v2.0 TTP.
Saurav Das822c4e22015-10-23 10:51:11 -0700107 */
Charles Chan361154b2016-03-24 10:23:39 -0700108public class Ofdpa2Pipeline extends AbstractHandlerBehaviour implements Pipeliner {
Pier Ventre42287df2016-11-09 14:17:26 -0800109
Saurav Das822c4e22015-10-23 10:51:11 -0700110 protected static final int PORT_TABLE = 0;
111 protected static final int VLAN_TABLE = 10;
Pier Ventre42287df2016-11-09 14:17:26 -0800112 protected static final int VLAN_1_TABLE = 11;
113 protected static final int MPLS_L2_PORT_FLOW_TABLE = 13;
114 protected static final int MPLS_L2_PORT_PCP_TRUST_FLOW_TABLE = 16;
Saurav Das822c4e22015-10-23 10:51:11 -0700115 protected static final int TMAC_TABLE = 20;
116 protected static final int UNICAST_ROUTING_TABLE = 30;
117 protected static final int MULTICAST_ROUTING_TABLE = 40;
118 protected static final int MPLS_TABLE_0 = 23;
119 protected static final int MPLS_TABLE_1 = 24;
Pier Ventre140a8942016-11-02 07:26:38 -0700120 protected static final int MPLS_L3_TYPE_TABLE = 27;
121 protected static final int MPLS_TYPE_TABLE = 29;
Saurav Das822c4e22015-10-23 10:51:11 -0700122 protected static final int BRIDGING_TABLE = 50;
123 protected static final int ACL_TABLE = 60;
124 protected static final int MAC_LEARNING_TABLE = 254;
125 protected static final long OFPP_MAX = 0xffffff00L;
126
Saurav Das52025962016-01-28 22:30:01 -0800127 protected static final int HIGHEST_PRIORITY = 0xffff;
Saurav Das2857f382015-11-03 14:39:27 -0800128 protected static final int DEFAULT_PRIORITY = 0x8000;
Saurav Das822c4e22015-10-23 10:51:11 -0700129 protected static final int LOWEST_PRIORITY = 0x0;
130
Pier Ventre42287df2016-11-09 14:17:26 -0800131 protected static final int MPLS_L2_PORT_PRIORITY = 2;
132
133 protected static final int MPLS_TUNNEL_ID_BASE = 0x10000;
134 protected static final int MPLS_TUNNEL_ID_MAX = 0x1FFFF;
135
Pier Ventre70d53ba2016-11-17 22:26:29 -0800136 protected static final int MPLS_UNI_PORT_MAX = 0x0000FFFF;
137
138 protected static final int MPLS_NNI_PORT_BASE = 0x00020000;
139 protected static final int MPLS_NNI_PORT_MAX = 0x0002FFFF;
140
Saurav Das822c4e22015-10-23 10:51:11 -0700141 private final Logger log = getLogger(getClass());
Charles Chan425854b2016-04-11 15:32:12 -0700142 protected ServiceDirectory serviceDirectory;
Saurav Das822c4e22015-10-23 10:51:11 -0700143 protected FlowRuleService flowRuleService;
Charles Chan425854b2016-04-11 15:32:12 -0700144 protected CoreService coreService;
Saurav Das8a0732e2015-11-20 15:27:53 -0800145 protected GroupService groupService;
146 protected FlowObjectiveStore flowObjectiveStore;
Saurav Das822c4e22015-10-23 10:51:11 -0700147 protected DeviceId deviceId;
148 protected ApplicationId driverId;
Saurav Das822c4e22015-10-23 10:51:11 -0700149 protected DeviceService deviceService;
Charles Chan188ebf52015-12-23 00:15:11 -0800150 protected static KryoNamespace appKryo = new KryoNamespace.Builder()
Yi Tsengef19de12017-04-24 11:33:05 -0700151 .register(KryoNamespaces.API)
152 .register(GroupKey.class)
153 .register(DefaultGroupKey.class)
154 .register(OfdpaNextGroup.class)
155 .register(ArrayDeque.class)
156 .build("Ofdpa2Pipeline");
Saurav Das822c4e22015-10-23 10:51:11 -0700157
Charles Chan425854b2016-04-11 15:32:12 -0700158 protected Ofdpa2GroupHandler groupHandler;
Saurav Das822c4e22015-10-23 10:51:11 -0700159
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700160 // flows installations to be retried
161 protected ScheduledExecutorService executorService
162 = newScheduledThreadPool(5, groupedThreads("OfdpaPipeliner", "retry-%d", log));
163 protected static final int MAX_RETRY_ATTEMPTS = 10;
164 protected static final int RETRY_MS = 1000;
Saurav Das4f980082015-11-05 13:39:15 -0800165
Saurav Das822c4e22015-10-23 10:51:11 -0700166 @Override
167 public void init(DeviceId deviceId, PipelinerContext context) {
Saurav Das822c4e22015-10-23 10:51:11 -0700168 this.deviceId = deviceId;
169
Charles Chan425854b2016-04-11 15:32:12 -0700170 serviceDirectory = context.directory();
Saurav Das822c4e22015-10-23 10:51:11 -0700171 coreService = serviceDirectory.get(CoreService.class);
172 flowRuleService = serviceDirectory.get(FlowRuleService.class);
173 groupService = serviceDirectory.get(GroupService.class);
174 flowObjectiveStore = context.store();
Saurav Das822c4e22015-10-23 10:51:11 -0700175 deviceService = serviceDirectory.get(DeviceService.class);
Saurav Das822c4e22015-10-23 10:51:11 -0700176
Charles Chan40132b32017-01-22 00:19:37 -0800177 initDriverId();
178 initGroupHander(context);
Saurav Das822c4e22015-10-23 10:51:11 -0700179
Saurav Das822c4e22015-10-23 10:51:11 -0700180 initializePipeline();
Saurav Das822c4e22015-10-23 10:51:11 -0700181 }
182
Charles Chan40132b32017-01-22 00:19:37 -0800183 protected void initDriverId() {
184 driverId = coreService.registerApplication(
185 "org.onosproject.driver.Ofdpa2Pipeline");
186 }
187
188 protected void initGroupHander(PipelinerContext context) {
189 groupHandler = new Ofdpa2GroupHandler();
190 groupHandler.init(deviceId, context);
191 }
192
Saurav Das822c4e22015-10-23 10:51:11 -0700193 protected void initializePipeline() {
Charles Chan188ebf52015-12-23 00:15:11 -0800194 // OF-DPA does not require initializing the pipeline as it puts default
195 // rules automatically in the hardware. However emulation of OFDPA in
196 // software switches does require table-miss-entries.
Saurav Das822c4e22015-10-23 10:51:11 -0700197 }
198
Charles Chand1172632017-03-15 17:33:09 -0700199 /**
200 * Determines whether this pipeline requires OFDPA match and set VLAN extensions.
201 *
202 * @return true to use the extensions
203 */
204 protected boolean requireVlanExtensions() {
205 return true;
206 }
207
Saurav Das86d13e82017-04-28 17:03:48 -0700208 /**
209 * Determines whether in-port should be matched on in TMAC table rules.
210 *
211 * @return true if match on in-port should be programmed
212 */
213 protected boolean matchInPortTmacTable() {
214 return true;
215 }
216
Charles Chan30122a72017-10-05 15:17:15 -0700217 /**
218 * Determines whether matching L4 destination port on IPv6 packets is supported in ACL table.
219 *
220 * @return true if matching L4 destination port on IPv6 packets is supported in ACL table.
221 */
222 protected boolean supportIpv6L4Dst() {
223 return true;
224 }
225
Saurav Das822c4e22015-10-23 10:51:11 -0700226 //////////////////////////////////////
227 // Flow Objectives
228 //////////////////////////////////////
229
230 @Override
231 public void filter(FilteringObjective filteringObjective) {
232 if (filteringObjective.type() == FilteringObjective.Type.PERMIT) {
233 processFilter(filteringObjective,
234 filteringObjective.op() == Objective.Operation.ADD,
235 filteringObjective.appId());
236 } else {
237 // Note that packets that don't match the PERMIT filter are
238 // automatically denied. The DENY filter is used to deny packets
239 // that are otherwise permitted by the PERMIT filter.
240 // Use ACL table flow rules here for DENY filtering objectives
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530241 log.warn("filter objective other than PERMIT currently not supported");
Saurav Das822c4e22015-10-23 10:51:11 -0700242 fail(filteringObjective, ObjectiveError.UNSUPPORTED);
243 }
244 }
245
246 @Override
247 public void forward(ForwardingObjective fwd) {
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700248 Collection<FlowRule> rules = processForward(fwd);
Saurav Das25190812016-05-27 13:54:07 -0700249 if (rules == null || rules.isEmpty()) {
250 // Assumes fail message has already been generated to the objective
251 // context. Returning here prevents spurious pass message to be
252 // generated by FlowRule service for empty flowOps.
253 return;
254 }
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700255 sendForward(fwd, rules);
256 }
257
258 protected void sendForward(ForwardingObjective fwd, Collection<FlowRule> rules) {
259 FlowRuleOperations.Builder flowOpsBuilder = FlowRuleOperations.builder();
Saurav Das822c4e22015-10-23 10:51:11 -0700260 switch (fwd.op()) {
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700261 case ADD:
262 rules.stream()
263 .filter(Objects::nonNull)
264 .forEach(flowOpsBuilder::add);
Saurav Dasd2fded02016-12-02 15:43:47 -0800265 log.debug("Applying a add fwd-obj {} to sw:{}", fwd.id(), deviceId);
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700266 break;
267 case REMOVE:
268 rules.stream()
269 .filter(Objects::nonNull)
270 .forEach(flowOpsBuilder::remove);
Pier Ventre42287df2016-11-09 14:17:26 -0800271 log.debug("Deleting a flow rule to sw:{}", deviceId);
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700272 break;
273 default:
274 fail(fwd, ObjectiveError.UNKNOWN);
275 log.warn("Unknown forwarding type {}", fwd.op());
Saurav Das822c4e22015-10-23 10:51:11 -0700276 }
277
Saurav Das822c4e22015-10-23 10:51:11 -0700278 flowRuleService.apply(flowOpsBuilder.build(new FlowRuleOperationsContext() {
279 @Override
280 public void onSuccess(FlowRuleOperations ops) {
281 pass(fwd);
282 }
283
284 @Override
285 public void onError(FlowRuleOperations ops) {
286 fail(fwd, ObjectiveError.FLOWINSTALLATIONFAILED);
287 }
288 }));
Saurav Das822c4e22015-10-23 10:51:11 -0700289 }
290
291 @Override
292 public void next(NextObjective nextObjective) {
Saurav Das8a0732e2015-11-20 15:27:53 -0800293 NextGroup nextGroup = flowObjectiveStore.getNextGroup(nextObjective.id());
294 switch (nextObjective.op()) {
295 case ADD:
Saurav Das4f980082015-11-05 13:39:15 -0800296 if (nextGroup != null) {
Saurav Das8a0732e2015-11-20 15:27:53 -0800297 log.warn("Cannot add next {} that already exists in device {}",
298 nextObjective.id(), deviceId);
299 return;
300 }
Saurav Das1547b3f2017-05-05 17:01:08 -0700301 log.debug("Processing NextObjective id {} in dev {} - add group",
Saurav Das8a0732e2015-11-20 15:27:53 -0800302 nextObjective.id(), deviceId);
Charles Chan425854b2016-04-11 15:32:12 -0700303 groupHandler.addGroup(nextObjective);
Saurav Das8a0732e2015-11-20 15:27:53 -0800304 break;
305 case ADD_TO_EXISTING:
306 if (nextGroup != null) {
Saurav Das1547b3f2017-05-05 17:01:08 -0700307 log.debug("Processing NextObjective id {} in dev {} - add bucket",
Saurav Das8a0732e2015-11-20 15:27:53 -0800308 nextObjective.id(), deviceId);
Charles Chan425854b2016-04-11 15:32:12 -0700309 groupHandler.addBucketToGroup(nextObjective, nextGroup);
Saurav Das4f980082015-11-05 13:39:15 -0800310 } else {
Saurav Das8a0732e2015-11-20 15:27:53 -0800311 // it is possible that group-chain has not been fully created yet
Saurav Das423fe2b2015-12-04 10:52:59 -0800312 log.debug("Waiting to add bucket to group for next-id:{} in dev:{}",
313 nextObjective.id(), deviceId);
Yi Tseng47f82dc2017-03-05 22:48:39 -0800314
315 // by design multiple pending bucket is allowed for the group
316 groupHandler.pendingBuckets.compute(nextObjective.id(), (nextId, pendBkts) -> {
317 if (pendBkts == null) {
318 pendBkts = Sets.newHashSet();
319 }
320 pendBkts.add(nextObjective);
321 return pendBkts;
322 });
Saurav Das4f980082015-11-05 13:39:15 -0800323 }
Saurav Das8a0732e2015-11-20 15:27:53 -0800324 break;
325 case REMOVE:
326 if (nextGroup == null) {
327 log.warn("Cannot remove next {} that does not exist in device {}",
328 nextObjective.id(), deviceId);
329 return;
330 }
Saurav Das1547b3f2017-05-05 17:01:08 -0700331 log.debug("Processing NextObjective id {} in dev {} - remove group",
Saurav Das8a0732e2015-11-20 15:27:53 -0800332 nextObjective.id(), deviceId);
Charles Chan425854b2016-04-11 15:32:12 -0700333 groupHandler.removeGroup(nextObjective, nextGroup);
Saurav Das8a0732e2015-11-20 15:27:53 -0800334 break;
335 case REMOVE_FROM_EXISTING:
336 if (nextGroup == null) {
337 log.warn("Cannot remove from next {} that does not exist in device {}",
338 nextObjective.id(), deviceId);
339 return;
340 }
Saurav Das1547b3f2017-05-05 17:01:08 -0700341 log.debug("Processing NextObjective id {} in dev {} - remove bucket",
Saurav Das8a0732e2015-11-20 15:27:53 -0800342 nextObjective.id(), deviceId);
Charles Chan425854b2016-04-11 15:32:12 -0700343 groupHandler.removeBucketFromGroup(nextObjective, nextGroup);
Saurav Das8a0732e2015-11-20 15:27:53 -0800344 break;
Saurav Dasceccf242017-08-03 18:30:35 -0700345 case VERIFY:
346 if (nextGroup == null) {
347 log.warn("Cannot verify next {} that does not exist in device {}",
348 nextObjective.id(), deviceId);
349 return;
350 }
351 log.debug("Processing NextObjective id {} in dev {} - verify",
352 nextObjective.id(), deviceId);
353 groupHandler.verifyGroup(nextObjective, nextGroup);
354 break;
Saurav Das8a0732e2015-11-20 15:27:53 -0800355 default:
Saurav Das4f980082015-11-05 13:39:15 -0800356 log.warn("Unsupported operation {}", nextObjective.op());
Saurav Das822c4e22015-10-23 10:51:11 -0700357 }
358 }
359
360 //////////////////////////////////////
361 // Flow handling
362 //////////////////////////////////////
363
364 /**
Yi Tseng0fdc07e2017-04-24 19:17:08 -0700365 * As per OFDPA 2.0 TTP, filtering of VLAN ids and MAC addresses (for routing)
366 * configured on switch ports happen in different tables.
Saurav Das822c4e22015-10-23 10:51:11 -0700367 *
368 * @param filt the filtering objective
369 * @param install indicates whether to add or remove the objective
370 * @param applicationId the application that sent this objective
371 */
Saurav Das52025962016-01-28 22:30:01 -0800372 protected void processFilter(FilteringObjective filt,
373 boolean install, ApplicationId applicationId) {
Saurav Das822c4e22015-10-23 10:51:11 -0700374 // This driver only processes filtering criteria defined with switch
375 // ports as the key
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530376 PortCriterion portCriterion = null;
Saurav Das822c4e22015-10-23 10:51:11 -0700377 EthCriterion ethCriterion = null;
378 VlanIdCriterion vidCriterion = null;
Saurav Das822c4e22015-10-23 10:51:11 -0700379 if (!filt.key().equals(Criteria.dummy()) &&
380 filt.key().type() == Criterion.Type.IN_PORT) {
381 portCriterion = (PortCriterion) filt.key();
Saurav Das822c4e22015-10-23 10:51:11 -0700382 }
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530383 if (portCriterion == null) {
384 log.debug("No IN_PORT defined in filtering objective from app: {}",
385 applicationId);
386 } else {
387 log.debug("Received filtering objective for dev/port: {}/{}", deviceId,
388 portCriterion.port());
389 }
Saurav Das822c4e22015-10-23 10:51:11 -0700390 // convert filtering conditions for switch-intfs into flowrules
391 FlowRuleOperations.Builder ops = FlowRuleOperations.builder();
392 for (Criterion criterion : filt.conditions()) {
Yi Tseng0fdc07e2017-04-24 19:17:08 -0700393 switch (criterion.type()) {
394 case ETH_DST:
395 case ETH_DST_MASKED:
396 ethCriterion = (EthCriterion) criterion;
397 break;
398 case VLAN_VID:
399 vidCriterion = (VlanIdCriterion) criterion;
400 break;
401 default:
402 log.warn("Unsupported filter {}", criterion);
403 fail(filt, ObjectiveError.UNSUPPORTED);
404 return;
Saurav Das822c4e22015-10-23 10:51:11 -0700405 }
406 }
407
Saurav Das0e99e2b2015-10-28 12:39:42 -0700408 VlanId assignedVlan = null;
Charles Chane849c192016-01-11 18:28:54 -0800409 if (vidCriterion != null) {
Charles Chand55e84d2016-03-30 17:54:24 -0700410 // Use the VLAN in criterion if metadata is not present and the traffic is tagged
Charles Chan0d6db512017-12-19 19:55:57 -0800411 if (!vidCriterion.vlanId().equals(VlanId.NONE)) {
Charles Chane849c192016-01-11 18:28:54 -0800412 assignedVlan = vidCriterion.vlanId();
Charles Chan0d6db512017-12-19 19:55:57 -0800413 } else if (filt.meta() != null) {
414 assignedVlan = readVlanFromTreatment(filt.meta());
Charles Chand55e84d2016-03-30 17:54:24 -0700415 }
Charles Chane849c192016-01-11 18:28:54 -0800416
Charles Chand55e84d2016-03-30 17:54:24 -0700417 if (assignedVlan == null) {
418 log.error("Driver fails to extract VLAN information. "
Frank Wang3a98e0a2017-08-11 11:09:30 +0800419 + "Not processing VLAN filters on device {}.", deviceId);
Charles Chand55e84d2016-03-30 17:54:24 -0700420 log.debug("VLAN ID in criterion={}, metadata={}",
421 readVlanFromTreatment(filt.meta()), vidCriterion.vlanId());
422 fail(filt, ObjectiveError.BADPARAMS);
423 return;
Saurav Das0e99e2b2015-10-28 12:39:42 -0700424 }
425 }
426
Yi Tseng3a77b4f2017-02-06 15:02:17 -0800427 if (ethCriterion == null || ethCriterion.mac().equals(NONE)) {
Charles Chan985b12e2016-05-11 19:47:22 -0700428 // NOTE: it is possible that a filtering objective only has vidCriterion
Saurav Das961beb22017-03-29 19:09:17 -0700429 log.warn("filtering objective missing dstMac, cannot program TMAC table");
Saurav Das822c4e22015-10-23 10:51:11 -0700430 } else {
431 for (FlowRule tmacRule : processEthDstFilter(portCriterion, ethCriterion,
Saurav Das0e99e2b2015-10-28 12:39:42 -0700432 vidCriterion, assignedVlan,
433 applicationId)) {
Saurav Das961beb22017-03-29 19:09:17 -0700434 log.trace("{} MAC filtering rules in TMAC table: {} for dev: {}",
Saurav Das018605f2017-02-18 14:05:44 -0800435 (install) ? "adding" : "removing", tmacRule, deviceId);
Charles Chan0d6db512017-12-19 19:55:57 -0800436
437 if (install) {
438 ops = ops.add(tmacRule);
439 } else {
440 // NOTE: Only remove TMAC flow when there is no more enabled port within the
441 // same VLAN on this device if TMAC doesn't support matching on in_port.
442 if (matchInPortTmacTable() || (filt.meta() != null && filt.meta().clearedDeferred())) {
443 ops = ops.remove(tmacRule);
444 } else {
445 log.debug("Abort TMAC flow removal on {}. Some other ports still share this TMAC flow");
446 }
447 }
Saurav Das822c4e22015-10-23 10:51:11 -0700448 }
449 }
450
Charles Chan985b12e2016-05-11 19:47:22 -0700451 if (vidCriterion == null) {
452 // NOTE: it is possible that a filtering objective only has ethCriterion
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530453 log.info("filtering objective missing VLAN, cannot program VLAN Table");
Saurav Das822c4e22015-10-23 10:51:11 -0700454 } else {
Charles Chan14967c22015-12-07 11:11:50 -0800455 /*
456 * NOTE: Separate vlan filtering rules and assignment rules
457 * into different stage in order to guarantee that filtering rules
Saurav Das52025962016-01-28 22:30:01 -0800458 * always go first, as required by ofdpa.
Charles Chan14967c22015-12-07 11:11:50 -0800459 */
460 List<FlowRule> allRules = processVlanIdFilter(
461 portCriterion, vidCriterion, assignedVlan, applicationId);
462 List<FlowRule> filteringRules = new ArrayList<>();
463 List<FlowRule> assignmentRules = new ArrayList<>();
464
465 allRules.forEach(flowRule -> {
Charles Chand1172632017-03-15 17:33:09 -0700466 VlanId vlanId;
467 if (requireVlanExtensions()) {
468 ExtensionCriterion extCriterion =
469 (ExtensionCriterion) flowRule.selector().getCriterion(Criterion.Type.EXTENSION);
470 vlanId = ((OfdpaMatchVlanVid) extCriterion.extensionSelector()).vlanId();
471 } else {
472 VlanIdCriterion vlanIdCriterion =
473 (VlanIdCriterion) flowRule.selector().getCriterion(Criterion.Type.VLAN_VID);
474 vlanId = vlanIdCriterion.vlanId();
475 }
Charles Chanbe8aea42016-02-24 12:04:47 -0800476 if (!vlanId.equals(VlanId.NONE)) {
Charles Chan14967c22015-12-07 11:11:50 -0800477 filteringRules.add(flowRule);
478 } else {
479 assignmentRules.add(flowRule);
480 }
481 });
482
483 for (FlowRule filteringRule : filteringRules) {
Saurav Das961beb22017-03-29 19:09:17 -0700484 log.trace("{} VLAN filtering rule in VLAN table: {} for dev: {}",
Saurav Das018605f2017-02-18 14:05:44 -0800485 (install) ? "adding" : "removing", filteringRule, deviceId);
Charles Chan14967c22015-12-07 11:11:50 -0800486 ops = install ? ops.add(filteringRule) : ops.remove(filteringRule);
487 }
488
489 ops.newStage();
490
491 for (FlowRule assignmentRule : assignmentRules) {
Saurav Das961beb22017-03-29 19:09:17 -0700492 log.trace("{} VLAN assignment rule in VLAN table: {} for dev: {}",
Saurav Das018605f2017-02-18 14:05:44 -0800493 (install) ? "adding" : "removing", assignmentRule, deviceId);
Charles Chan14967c22015-12-07 11:11:50 -0800494 ops = install ? ops.add(assignmentRule) : ops.remove(assignmentRule);
Saurav Das822c4e22015-10-23 10:51:11 -0700495 }
496 }
497
Saurav Das822c4e22015-10-23 10:51:11 -0700498 // apply filtering flow rules
499 flowRuleService.apply(ops.build(new FlowRuleOperationsContext() {
500 @Override
501 public void onSuccess(FlowRuleOperations ops) {
Saurav Das018605f2017-02-18 14:05:44 -0800502 log.debug("Applied {} filtering rules in device {}",
Saurav Das822c4e22015-10-23 10:51:11 -0700503 ops.stages().get(0).size(), deviceId);
504 pass(filt);
505 }
506
507 @Override
508 public void onError(FlowRuleOperations ops) {
509 log.info("Failed to apply all filtering rules in dev {}", deviceId);
510 fail(filt, ObjectiveError.FLOWINSTALLATIONFAILED);
511 }
512 }));
513
514 }
515
516 /**
Charles Chand1172632017-03-15 17:33:09 -0700517 * Internal implementation of processVlanIdFilter.
518 * <p>
519 * The is_present bit in set_vlan_vid action is required to be 0 in OFDPA i12.
520 * Since it is non-OF spec, we need an extension treatment for that.
521 * The useVlanExtension must be set to false for OFDPA i12.
522 * </p>
Saurav Das0e99e2b2015-10-28 12:39:42 -0700523 *
Charles Chanf9e98652016-09-07 16:54:23 -0700524 * @param portCriterion port on device for which this filter is programmed
525 * @param vidCriterion vlan assigned to port, or NONE for untagged
526 * @param assignedVlan assigned vlan-id for untagged packets
527 * @param applicationId for application programming this filter
Saurav Das822c4e22015-10-23 10:51:11 -0700528 * @return list of FlowRule for port-vlan filters
529 */
530 protected List<FlowRule> processVlanIdFilter(PortCriterion portCriterion,
Yi Tsengef19de12017-04-24 11:33:05 -0700531 VlanIdCriterion vidCriterion,
532 VlanId assignedVlan,
533 ApplicationId applicationId) {
Charles Chan14967c22015-12-07 11:11:50 -0800534 List<FlowRule> rules = new ArrayList<>();
Saurav Das822c4e22015-10-23 10:51:11 -0700535 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
536 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
Charles Chan14967c22015-12-07 11:11:50 -0800537 TrafficSelector.Builder preSelector = null;
538 TrafficTreatment.Builder preTreatment = null;
539
Saurav Das4f980082015-11-05 13:39:15 -0800540 treatment.transition(TMAC_TABLE);
541
Saurav Das822c4e22015-10-23 10:51:11 -0700542 if (vidCriterion.vlanId() == VlanId.NONE) {
543 // untagged packets are assigned vlans
Charles Chand1172632017-03-15 17:33:09 -0700544 preSelector = DefaultTrafficSelector.builder();
545 if (requireVlanExtensions()) {
546 OfdpaMatchVlanVid ofdpaMatchVlanVid = new OfdpaMatchVlanVid(VlanId.NONE);
547 selector.extension(ofdpaMatchVlanVid, deviceId);
Charles Chanf9e98652016-09-07 16:54:23 -0700548 OfdpaSetVlanVid ofdpaSetVlanVid = new OfdpaSetVlanVid(assignedVlan);
549 treatment.extension(ofdpaSetVlanVid, deviceId);
Charles Chand1172632017-03-15 17:33:09 -0700550
551 OfdpaMatchVlanVid preOfdpaMatchVlanVid = new OfdpaMatchVlanVid(assignedVlan);
552 preSelector.extension(preOfdpaMatchVlanVid, deviceId);
Charles Chanf9e98652016-09-07 16:54:23 -0700553 } else {
Charles Chand1172632017-03-15 17:33:09 -0700554 selector.matchVlanId(VlanId.NONE);
Charles Chanf9e98652016-09-07 16:54:23 -0700555 treatment.setVlanId(assignedVlan);
Charles Chand1172632017-03-15 17:33:09 -0700556
557 preSelector.matchVlanId(assignedVlan);
558 }
559 preTreatment = DefaultTrafficTreatment.builder().transition(TMAC_TABLE);
560 } else {
561 if (requireVlanExtensions()) {
562 OfdpaMatchVlanVid ofdpaMatchVlanVid = new OfdpaMatchVlanVid(vidCriterion.vlanId());
563 selector.extension(ofdpaMatchVlanVid, deviceId);
564 } else {
565 selector.matchVlanId(vidCriterion.vlanId());
Charles Chanf9e98652016-09-07 16:54:23 -0700566 }
Charles Chan14967c22015-12-07 11:11:50 -0800567
Charles Chand55e84d2016-03-30 17:54:24 -0700568 if (!assignedVlan.equals(vidCriterion.vlanId())) {
Charles Chand1172632017-03-15 17:33:09 -0700569 if (requireVlanExtensions()) {
Charles Chanc03782d2017-01-31 13:57:55 -0800570 OfdpaSetVlanVid ofdpaSetVlanVid = new OfdpaSetVlanVid(assignedVlan);
571 treatment.extension(ofdpaSetVlanVid, deviceId);
572 } else {
573 treatment.setVlanId(assignedVlan);
574 }
Charles Chand55e84d2016-03-30 17:54:24 -0700575 }
Saurav Das822c4e22015-10-23 10:51:11 -0700576 }
Saurav Das822c4e22015-10-23 10:51:11 -0700577
578 // ofdpa cannot match on ALL portnumber, so we need to use separate
579 // rules for each port.
Charles Chan14967c22015-12-07 11:11:50 -0800580 List<PortNumber> portnums = new ArrayList<>();
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530581 if (portCriterion != null) {
582 if (PortNumber.ALL.equals(portCriterion.port())) {
583 for (Port port : deviceService.getPorts(deviceId)) {
584 if (port.number().toLong() > 0 && port.number().toLong() < OFPP_MAX) {
585 portnums.add(port.number());
586 }
Saurav Das822c4e22015-10-23 10:51:11 -0700587 }
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530588 } else {
589 portnums.add(portCriterion.port());
Saurav Das822c4e22015-10-23 10:51:11 -0700590 }
591 } else {
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530592 log.warn("Filtering Objective missing Port Criterion . " +
593 "VLAN Table cannot be programmed for {}",
594 deviceId);
Saurav Das822c4e22015-10-23 10:51:11 -0700595 }
Saurav Das4f980082015-11-05 13:39:15 -0800596
Saurav Das822c4e22015-10-23 10:51:11 -0700597 for (PortNumber pnum : portnums) {
Saurav Das4f980082015-11-05 13:39:15 -0800598 // create rest of flowrule
Saurav Das822c4e22015-10-23 10:51:11 -0700599 selector.matchInPort(pnum);
600 FlowRule rule = DefaultFlowRule.builder()
601 .forDevice(deviceId)
602 .withSelector(selector.build())
603 .withTreatment(treatment.build())
604 .withPriority(DEFAULT_PRIORITY)
605 .fromApp(applicationId)
606 .makePermanent()
607 .forTable(VLAN_TABLE).build();
Charles Chan14967c22015-12-07 11:11:50 -0800608
609 if (preSelector != null) {
610 preSelector.matchInPort(pnum);
611 FlowRule preRule = DefaultFlowRule.builder()
612 .forDevice(deviceId)
613 .withSelector(preSelector.build())
614 .withTreatment(preTreatment.build())
615 .withPriority(DEFAULT_PRIORITY)
616 .fromApp(applicationId)
617 .makePermanent()
618 .forTable(VLAN_TABLE).build();
619 rules.add(preRule);
620 }
621
Saurav Das822c4e22015-10-23 10:51:11 -0700622 rules.add(rule);
623 }
624 return rules;
625 }
626
627 /**
628 * Allows routed packets with correct destination MAC to be directed
629 * to unicast-IP routing table or MPLS forwarding table.
Saurav Das822c4e22015-10-23 10:51:11 -0700630 *
631 * @param portCriterion port on device for which this filter is programmed
632 * @param ethCriterion dstMac of device for which is filter is programmed
633 * @param vidCriterion vlan assigned to port, or NONE for untagged
Saurav Das0e99e2b2015-10-28 12:39:42 -0700634 * @param assignedVlan assigned vlan-id for untagged packets
Saurav Das822c4e22015-10-23 10:51:11 -0700635 * @param applicationId for application programming this filter
636 * @return list of FlowRule for port-vlan filters
637
638 */
639 protected List<FlowRule> processEthDstFilter(PortCriterion portCriterion,
640 EthCriterion ethCriterion,
641 VlanIdCriterion vidCriterion,
Saurav Das0e99e2b2015-10-28 12:39:42 -0700642 VlanId assignedVlan,
Saurav Das822c4e22015-10-23 10:51:11 -0700643 ApplicationId applicationId) {
Charles Chan5270ed02016-01-30 23:22:37 -0800644 // Consider PortNumber.ANY as wildcard. Match ETH_DST only
Sivachidambaram Subramanianc598d062017-05-16 22:42:06 +0530645 if (portCriterion != null && PortNumber.ANY.equals(portCriterion.port())) {
Charles Chan5270ed02016-01-30 23:22:37 -0800646 return processEthDstOnlyFilter(ethCriterion, applicationId);
647 }
648
Charles Chan5b9df8d2016-03-28 22:21:40 -0700649 // Multicast MAC
650 if (ethCriterion.mask() != null) {
Charles Chan206cac02017-10-20 19:09:16 -0700651 return processMcastEthDstFilter(ethCriterion, assignedVlan, applicationId);
Charles Chan5b9df8d2016-03-28 22:21:40 -0700652 }
653
Saurav Das822c4e22015-10-23 10:51:11 -0700654 //handling untagged packets via assigned VLAN
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530655 if (vidCriterion != null && vidCriterion.vlanId() == VlanId.NONE) {
Saurav Das0e99e2b2015-10-28 12:39:42 -0700656 vidCriterion = (VlanIdCriterion) Criteria.matchVlanId(assignedVlan);
Saurav Das822c4e22015-10-23 10:51:11 -0700657 }
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530658 List<FlowRule> rules = new ArrayList<>();
659 OfdpaMatchVlanVid ofdpaMatchVlanVid = null;
660 if (vidCriterion != null && requireVlanExtensions()) {
661 ofdpaMatchVlanVid = new OfdpaMatchVlanVid(vidCriterion.vlanId());
662 }
Saurav Das822c4e22015-10-23 10:51:11 -0700663 // ofdpa cannot match on ALL portnumber, so we need to use separate
664 // rules for each port.
Yuta HIGUCHI2dce08a2017-04-20 21:57:48 -0700665 List<PortNumber> portnums = new ArrayList<>();
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530666 if (portCriterion != null) {
667 if (PortNumber.ALL.equals(portCriterion.port())) {
668 for (Port port : deviceService.getPorts(deviceId)) {
669 if (port.number().toLong() > 0 && port.number().toLong() < OFPP_MAX) {
670 portnums.add(port.number());
671 }
Saurav Das822c4e22015-10-23 10:51:11 -0700672 }
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530673 } else {
674 portnums.add(portCriterion.port());
675 }
676 for (PortNumber pnum : portnums) {
677 rules.add(buildTmacRuleForIpv4(ethCriterion,
678 vidCriterion,
679 ofdpaMatchVlanVid,
680 applicationId,
681 pnum));
682 rules.add(buildTmacRuleForMpls(ethCriterion,
683 vidCriterion,
684 ofdpaMatchVlanVid,
685 applicationId,
686 pnum));
687 rules.add(buildTmacRuleForIpv6(ethCriterion,
688 vidCriterion,
689 ofdpaMatchVlanVid,
690 applicationId,
691 pnum));
Saurav Das822c4e22015-10-23 10:51:11 -0700692 }
693 } else {
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530694 rules.add(buildTmacRuleForIpv4(ethCriterion,
695 vidCriterion,
696 ofdpaMatchVlanVid,
697 applicationId,
698 null));
699 rules.add(buildTmacRuleForMpls(ethCriterion,
700 vidCriterion,
701 ofdpaMatchVlanVid,
702 applicationId,
703 null));
704 rules.add(buildTmacRuleForIpv6(ethCriterion,
705 vidCriterion,
706 ofdpaMatchVlanVid,
707 applicationId,
708 null));
Saurav Das822c4e22015-10-23 10:51:11 -0700709 }
710 return rules;
711 }
712
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530713 /**
714 * Builds TMAC rules for IPv4 packets.
715 *
Charles Chan0d6db512017-12-19 19:55:57 -0800716 * @param ethCriterion dst mac matching
717 * @param vidCriterion vlan id assigned to the port
718 * @param ofdpaMatchVlanVid OFDPA vlan id matching
719 * @param applicationId application id
720 * @param pnum port number
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530721 * @return TMAC rule for IPV4 packets
722 */
723 private FlowRule buildTmacRuleForIpv4(EthCriterion ethCriterion,
724 VlanIdCriterion vidCriterion,
725 OfdpaMatchVlanVid ofdpaMatchVlanVid,
726 ApplicationId applicationId,
727 PortNumber pnum) {
728 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
729 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
730 if (pnum != null) {
731 if (matchInPortTmacTable()) {
732 selector.matchInPort(pnum);
733 } else {
Saurav Das9aa28bd2017-12-05 15:00:23 -0800734 log.debug("Pipeline does not support IN_PORT matching in TMAC table, " +
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530735 "ignoring the IN_PORT criteria");
736 }
737 }
738 if (vidCriterion != null) {
739 if (requireVlanExtensions()) {
740 selector.extension(ofdpaMatchVlanVid, deviceId);
741 } else {
742 selector.matchVlanId(vidCriterion.vlanId());
743 }
744 }
745 selector.matchEthType(Ethernet.TYPE_IPV4);
746 selector.matchEthDst(ethCriterion.mac());
747 treatment.transition(UNICAST_ROUTING_TABLE);
748 return DefaultFlowRule.builder()
749 .forDevice(deviceId)
750 .withSelector(selector.build())
751 .withTreatment(treatment.build())
752 .withPriority(DEFAULT_PRIORITY)
753 .fromApp(applicationId)
754 .makePermanent()
755 .forTable(TMAC_TABLE).build();
756 }
757
758 /**
759 * Builds TMAC rule for MPLS packets.
760 *
Charles Chan0d6db512017-12-19 19:55:57 -0800761 * @param ethCriterion dst mac matching
762 * @param vidCriterion vlan id assigned to the port
763 * @param ofdpaMatchVlanVid OFDPA vlan id matching
764 * @param applicationId application id
765 * @param pnum port number
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530766 * @return TMAC rule for MPLS packets
767 */
768 private FlowRule buildTmacRuleForMpls(EthCriterion ethCriterion,
769 VlanIdCriterion vidCriterion,
770 OfdpaMatchVlanVid ofdpaMatchVlanVid,
771 ApplicationId applicationId,
772 PortNumber pnum) {
773 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
774 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
775 if (pnum != null) {
776 if (matchInPortTmacTable()) {
777 selector.matchInPort(pnum);
778 } else {
Saurav Das9aa28bd2017-12-05 15:00:23 -0800779 log.debug("Pipeline does not support IN_PORT matching in TMAC table, " +
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530780 "ignoring the IN_PORT criteria");
781 }
782 }
783 if (vidCriterion != null) {
784 if (requireVlanExtensions()) {
785 selector.extension(ofdpaMatchVlanVid, deviceId);
786 } else {
787 selector.matchVlanId(vidCriterion.vlanId());
788 }
789 }
790 selector.matchEthType(Ethernet.MPLS_UNICAST);
791 selector.matchEthDst(ethCriterion.mac());
792 treatment.transition(MPLS_TABLE_0);
793 return DefaultFlowRule.builder()
794 .forDevice(deviceId)
795 .withSelector(selector.build())
796 .withTreatment(treatment.build())
797 .withPriority(DEFAULT_PRIORITY)
798 .fromApp(applicationId)
799 .makePermanent()
800 .forTable(TMAC_TABLE).build();
801 }
802
803 /**
804 * Builds TMAC rules for IPv6 packets.
805 *
Charles Chan0d6db512017-12-19 19:55:57 -0800806 * @param ethCriterion dst mac matching
807 * @param vidCriterion vlan id assigned to the port
808 * @param ofdpaMatchVlanVid OFDPA vlan id matching
809 * @param applicationId application id
810 * @param pnum port number
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530811 * @return TMAC rule for IPV6 packets
812 */
813 private FlowRule buildTmacRuleForIpv6(EthCriterion ethCriterion,
814 VlanIdCriterion vidCriterion,
815 OfdpaMatchVlanVid ofdpaMatchVlanVid,
816 ApplicationId applicationId,
817 PortNumber pnum) {
818 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
819 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
820 if (pnum != null) {
821 if (matchInPortTmacTable()) {
822 selector.matchInPort(pnum);
823 } else {
Saurav Das9aa28bd2017-12-05 15:00:23 -0800824 log.debug("Pipeline does not support IN_PORT matching in TMAC table, " +
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530825 "ignoring the IN_PORT criteria");
826 }
827 }
828 if (vidCriterion != null) {
829 if (requireVlanExtensions()) {
830 selector.extension(ofdpaMatchVlanVid, deviceId);
831 } else {
832 selector.matchVlanId(vidCriterion.vlanId());
833 }
834 }
835 selector.matchEthType(Ethernet.TYPE_IPV6);
836 selector.matchEthDst(ethCriterion.mac());
837 treatment.transition(UNICAST_ROUTING_TABLE);
838 return DefaultFlowRule.builder()
839 .forDevice(deviceId)
840 .withSelector(selector.build())
841 .withTreatment(treatment.build())
842 .withPriority(DEFAULT_PRIORITY)
843 .fromApp(applicationId)
844 .makePermanent()
845 .forTable(TMAC_TABLE).build();
846 }
847
Charles Chan5270ed02016-01-30 23:22:37 -0800848 protected List<FlowRule> processEthDstOnlyFilter(EthCriterion ethCriterion,
Yi Tsengef19de12017-04-24 11:33:05 -0700849 ApplicationId applicationId) {
Pier Luigi0e358632017-01-31 09:35:05 -0800850 ImmutableList.Builder<FlowRule> builder = ImmutableList.builder();
851
Charles Chan5270ed02016-01-30 23:22:37 -0800852 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
853 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
854 selector.matchEthType(Ethernet.TYPE_IPV4);
855 selector.matchEthDst(ethCriterion.mac());
856 treatment.transition(UNICAST_ROUTING_TABLE);
857 FlowRule rule = DefaultFlowRule.builder()
858 .forDevice(deviceId)
859 .withSelector(selector.build())
860 .withTreatment(treatment.build())
861 .withPriority(DEFAULT_PRIORITY)
862 .fromApp(applicationId)
863 .makePermanent()
864 .forTable(TMAC_TABLE).build();
Pier Luigi0e358632017-01-31 09:35:05 -0800865 builder.add(rule);
866
867 selector = DefaultTrafficSelector.builder();
868 treatment = DefaultTrafficTreatment.builder();
869 selector.matchEthType(Ethernet.TYPE_IPV6);
870 selector.matchEthDst(ethCriterion.mac());
871 treatment.transition(UNICAST_ROUTING_TABLE);
872 rule = DefaultFlowRule.builder()
873 .forDevice(deviceId)
874 .withSelector(selector.build())
875 .withTreatment(treatment.build())
876 .withPriority(DEFAULT_PRIORITY)
877 .fromApp(applicationId)
878 .makePermanent()
879 .forTable(TMAC_TABLE).build();
880 return builder.add(rule).build();
Charles Chan5270ed02016-01-30 23:22:37 -0800881 }
882
Charles Chan5b9df8d2016-03-28 22:21:40 -0700883 protected List<FlowRule> processMcastEthDstFilter(EthCriterion ethCriterion,
Charles Chan206cac02017-10-20 19:09:16 -0700884 VlanId assignedVlan,
Yi Tsengef19de12017-04-24 11:33:05 -0700885 ApplicationId applicationId) {
Julia Fergusond8f145e2017-08-10 18:15:24 +0000886 ImmutableList.Builder<FlowRule> builder = ImmutableList.builder();
Charles Chan5b9df8d2016-03-28 22:21:40 -0700887 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
888 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
Charles Chan206cac02017-10-20 19:09:16 -0700889 FlowRule rule;
Julia Fergusond8f145e2017-08-10 18:15:24 +0000890
Charles Chan206cac02017-10-20 19:09:16 -0700891 if (IPV4_MULTICAST.equals(ethCriterion.mac())) {
892 selector.matchEthType(Ethernet.TYPE_IPV4);
893 selector.matchEthDstMasked(ethCriterion.mac(), ethCriterion.mask());
894 selector.matchVlanId(assignedVlan);
895 treatment.transition(MULTICAST_ROUTING_TABLE);
896 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();
904 builder.add(rule);
905 }
906
907 if (IPV6_MULTICAST.equals(ethCriterion.mac())) {
908 selector = DefaultTrafficSelector.builder();
909 treatment = DefaultTrafficTreatment.builder();
910 selector.matchEthType(Ethernet.TYPE_IPV6);
911 selector.matchEthDstMasked(ethCriterion.mac(), ethCriterion.mask());
912 selector.matchVlanId(assignedVlan);
913 treatment.transition(MULTICAST_ROUTING_TABLE);
914 rule = DefaultFlowRule.builder()
915 .forDevice(deviceId)
916 .withSelector(selector.build())
917 .withTreatment(treatment.build())
918 .withPriority(DEFAULT_PRIORITY)
919 .fromApp(applicationId)
920 .makePermanent()
921 .forTable(TMAC_TABLE).build();
922 builder.add(rule);
923 }
924 return builder.build();
Charles Chan5b9df8d2016-03-28 22:21:40 -0700925 }
926
Saurav Das822c4e22015-10-23 10:51:11 -0700927 private Collection<FlowRule> processForward(ForwardingObjective fwd) {
928 switch (fwd.flag()) {
929 case SPECIFIC:
930 return processSpecific(fwd);
931 case VERSATILE:
932 return processVersatile(fwd);
933 default:
934 fail(fwd, ObjectiveError.UNKNOWN);
935 log.warn("Unknown forwarding flag {}", fwd.flag());
936 }
937 return Collections.emptySet();
938 }
939
940 /**
941 * In the OF-DPA 2.0 pipeline, versatile forwarding objectives go to the
942 * ACL table.
943 * @param fwd the forwarding objective of type 'versatile'
944 * @return a collection of flow rules to be sent to the switch. An empty
945 * collection may be returned if there is a problem in processing
946 * the flow rule
947 */
Saurav Das52025962016-01-28 22:30:01 -0800948 protected Collection<FlowRule> processVersatile(ForwardingObjective fwd) {
Saurav Das018605f2017-02-18 14:05:44 -0800949 log.debug("Processing versatile forwarding objective:{} in dev:{}",
Saurav Dasd2fded02016-12-02 15:43:47 -0800950 fwd.id(), deviceId);
Saurav Das822c4e22015-10-23 10:51:11 -0700951
952 EthTypeCriterion ethType =
Saurav Das77b5e902016-01-27 17:01:59 -0800953 (EthTypeCriterion) fwd.selector().getCriterion(Criterion.Type.ETH_TYPE);
Saurav Das822c4e22015-10-23 10:51:11 -0700954 if (ethType == null) {
Saurav Dasd2fded02016-12-02 15:43:47 -0800955 log.error("Versatile forwarding objective:{} must include ethType",
956 fwd.id());
Saurav Das822c4e22015-10-23 10:51:11 -0700957 fail(fwd, ObjectiveError.BADPARAMS);
958 return Collections.emptySet();
959 }
960 if (fwd.nextId() == null && fwd.treatment() == null) {
961 log.error("Forwarding objective {} from {} must contain "
962 + "nextId or Treatment", fwd.selector(), fwd.appId());
Zsolt Haraszti9faab752016-02-17 15:55:20 -0800963 fail(fwd, ObjectiveError.BADPARAMS);
Saurav Das822c4e22015-10-23 10:51:11 -0700964 return Collections.emptySet();
965 }
Saurav Das49cb5a12016-01-16 22:54:07 -0800966
Saurav Das77b5e902016-01-27 17:01:59 -0800967 TrafficSelector.Builder sbuilder = DefaultTrafficSelector.builder();
968 fwd.selector().criteria().forEach(criterion -> {
969 if (criterion instanceof VlanIdCriterion) {
970 VlanId vlanId = ((VlanIdCriterion) criterion).vlanId();
971 // ensure that match does not include vlan = NONE as OF-DPA does not
972 // match untagged packets this way in the ACL table.
973 if (vlanId.equals(VlanId.NONE)) {
974 return;
975 }
Charles Chand1172632017-03-15 17:33:09 -0700976 if (requireVlanExtensions()) {
977 OfdpaMatchVlanVid ofdpaMatchVlanVid = new OfdpaMatchVlanVid(vlanId);
978 sbuilder.extension(ofdpaMatchVlanVid, deviceId);
979 } else {
980 sbuilder.matchVlanId(vlanId);
981 }
Pier Ventree0ae7a32016-11-23 09:57:42 -0800982 } else if (criterion instanceof Icmpv6TypeCriterion ||
983 criterion instanceof Icmpv6CodeCriterion) {
984 /*
985 * We silenty discard these criterions, our current
986 * OFDPA platform does not support these matches on
987 * the ACL table.
988 */
989 log.warn("ICMPv6 Type and ICMPv6 Code are not supported");
Charles Chan30122a72017-10-05 15:17:15 -0700990 } else if (criterion instanceof TcpPortCriterion || criterion instanceof UdpPortCriterion) {
991 // FIXME: QMX switches do not support L4 dst port matching in ACL table.
992 // Currently L4 dst port matching is only used by DHCP relay feature
993 // and therefore is safe to be replaced with L4 src port matching.
994 // We need to revisit this if L4 dst port is used for other purpose in the future.
995 if (!supportIpv6L4Dst() && isIpv6(fwd.selector())) {
Charles Chan30122a72017-10-05 15:17:15 -0700996 switch (criterion.type()) {
997 case UDP_DST:
Charles Chan30122a72017-10-05 15:17:15 -0700998 case UDP_DST_MASKED:
Charles Chan30122a72017-10-05 15:17:15 -0700999 case TCP_DST:
Charles Chan30122a72017-10-05 15:17:15 -07001000 case TCP_DST_MASKED:
Charles Chan30122a72017-10-05 15:17:15 -07001001 break;
1002 default:
1003 sbuilder.add(criterion);
1004 }
1005 } else {
1006 sbuilder.add(criterion);
1007 }
Saurav Das77b5e902016-01-27 17:01:59 -08001008 } else {
1009 sbuilder.add(criterion);
1010 }
1011 });
1012
Saurav Das822c4e22015-10-23 10:51:11 -07001013 // XXX driver does not currently do type checking as per Tables 65-67 in
1014 // OFDPA 2.0 spec. The only allowed treatment is a punt to the controller.
Saurav Das49cb5a12016-01-16 22:54:07 -08001015 TrafficTreatment.Builder ttBuilder = DefaultTrafficTreatment.builder();
1016 if (fwd.treatment() != null) {
1017 for (Instruction ins : fwd.treatment().allInstructions()) {
1018 if (ins instanceof OutputInstruction) {
1019 OutputInstruction o = (OutputInstruction) ins;
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +05301020 if (o != null && PortNumber.CONTROLLER.equals(o.port())) {
Saurav Das49cb5a12016-01-16 22:54:07 -08001021 ttBuilder.add(o);
1022 } else {
1023 log.warn("Only allowed treatments in versatile forwarding "
1024 + "objectives are punts to the controller");
1025 }
1026 } else {
1027 log.warn("Cannot process instruction in versatile fwd {}", ins);
1028 }
Saurav Das822c4e22015-10-23 10:51:11 -07001029 }
Charles Chan2df0e8a2017-01-09 11:45:08 -08001030 if (fwd.treatment().clearedDeferred()) {
1031 ttBuilder.wipeDeferred();
1032 }
Saurav Das822c4e22015-10-23 10:51:11 -07001033 }
Saurav Das822c4e22015-10-23 10:51:11 -07001034 if (fwd.nextId() != null) {
Saurav Das49cb5a12016-01-16 22:54:07 -08001035 // overide case
1036 NextGroup next = getGroupForNextObjective(fwd.nextId());
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +05301037 if (next == null) {
1038 fail(fwd, ObjectiveError.BADPARAMS);
1039 return Collections.emptySet();
1040 }
Saurav Das49cb5a12016-01-16 22:54:07 -08001041 List<Deque<GroupKey>> gkeys = appKryo.deserialize(next.data());
1042 // we only need the top level group's key to point the flow to it
1043 Group group = groupService.getGroup(deviceId, gkeys.get(0).peekFirst());
1044 if (group == null) {
1045 log.warn("Group with key:{} for next-id:{} not found in dev:{}",
1046 gkeys.get(0).peekFirst(), fwd.nextId(), deviceId);
1047 fail(fwd, ObjectiveError.GROUPMISSING);
1048 return Collections.emptySet();
1049 }
1050 ttBuilder.deferred().group(group.id());
Saurav Das822c4e22015-10-23 10:51:11 -07001051 }
Saurav Das49cb5a12016-01-16 22:54:07 -08001052
1053 FlowRule.Builder ruleBuilder = DefaultFlowRule.builder()
1054 .fromApp(fwd.appId())
1055 .withPriority(fwd.priority())
1056 .forDevice(deviceId)
Saurav Das77b5e902016-01-27 17:01:59 -08001057 .withSelector(sbuilder.build())
Saurav Das49cb5a12016-01-16 22:54:07 -08001058 .withTreatment(ttBuilder.build())
1059 .makePermanent()
1060 .forTable(ACL_TABLE);
1061 return Collections.singletonList(ruleBuilder.build());
Saurav Das822c4e22015-10-23 10:51:11 -07001062 }
1063
1064 /**
1065 * In the OF-DPA 2.0 pipeline, specific forwarding refers to the IP table
Saurav Das8a0732e2015-11-20 15:27:53 -08001066 * (unicast or multicast) or the L2 table (mac + vlan) or the MPLS table.
Saurav Das822c4e22015-10-23 10:51:11 -07001067 *
1068 * @param fwd the forwarding objective of type 'specific'
1069 * @return a collection of flow rules. Typically there will be only one
1070 * for this type of forwarding objective. An empty set may be
1071 * returned if there is an issue in processing the objective.
1072 */
Saurav Das8a0732e2015-11-20 15:27:53 -08001073 protected Collection<FlowRule> processSpecific(ForwardingObjective fwd) {
Saurav Das25190812016-05-27 13:54:07 -07001074 log.debug("Processing specific fwd objective:{} in dev:{} with next:{}",
Saurav Das4ce45962015-11-24 23:21:05 -08001075 fwd.id(), deviceId, fwd.nextId());
1076 boolean isEthTypeObj = isSupportedEthTypeObjective(fwd);
1077 boolean isEthDstObj = isSupportedEthDstObjective(fwd);
1078
1079 if (isEthTypeObj) {
1080 return processEthTypeSpecific(fwd);
1081 } else if (isEthDstObj) {
1082 return processEthDstSpecific(fwd);
1083 } else {
1084 log.warn("processSpecific: Unsupported forwarding objective "
1085 + "criteria fwd:{} in dev:{}", fwd.nextId(), deviceId);
Saurav Das822c4e22015-10-23 10:51:11 -07001086 fail(fwd, ObjectiveError.UNSUPPORTED);
1087 return Collections.emptySet();
1088 }
Saurav Das4ce45962015-11-24 23:21:05 -08001089 }
1090
Saurav Das4ce45962015-11-24 23:21:05 -08001091 /**
1092 * Handles forwarding rules to the IP and MPLS tables.
1093 *
1094 * @param fwd the forwarding objective
1095 * @return A collection of flow rules, or an empty set
1096 */
1097 protected Collection<FlowRule> processEthTypeSpecific(ForwardingObjective fwd) {
Charles Chancad338a2016-09-16 18:03:11 -07001098 return processEthTypeSpecificInternal(fwd, false, ACL_TABLE);
Charles Chanf9e98652016-09-07 16:54:23 -07001099 }
1100
1101 /**
1102 * Internal implementation of processEthTypeSpecific.
1103 * <p>
1104 * Wildcarded IPv4_DST is not supported in OFDPA i12. Therefore, we break
1105 * the rule into 0.0.0.0/1 and 128.0.0.0/1.
1106 * The allowDefaultRoute must be set to false for OFDPA i12.
1107 * </p>
1108 *
1109 * @param fwd the forwarding objective
1110 * @param allowDefaultRoute allow wildcarded IPv4_DST or not
Ray Milkey0bb1e102016-11-10 14:51:27 -08001111 * @param mplsNextTable next MPLS table
Charles Chanf9e98652016-09-07 16:54:23 -07001112 * @return A collection of flow rules, or an empty set
1113 */
1114 protected Collection<FlowRule> processEthTypeSpecificInternal(ForwardingObjective fwd,
Charles Chancad338a2016-09-16 18:03:11 -07001115 boolean allowDefaultRoute,
1116 int mplsNextTable) {
Saurav Das4ce45962015-11-24 23:21:05 -08001117 TrafficSelector selector = fwd.selector();
1118 EthTypeCriterion ethType =
1119 (EthTypeCriterion) selector.getCriterion(Criterion.Type.ETH_TYPE);
Flavio Castroe10fa242016-01-15 12:43:51 -08001120 boolean popMpls = false;
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001121 boolean emptyGroup = false;
Charles Chan188ebf52015-12-23 00:15:11 -08001122 int forTableId;
Saurav Das8a0732e2015-11-20 15:27:53 -08001123 TrafficSelector.Builder filteredSelector = DefaultTrafficSelector.builder();
Charles Chan14967c22015-12-07 11:11:50 -08001124 TrafficTreatment.Builder tb = DefaultTrafficTreatment.builder();
Flavio Castroe10fa242016-01-15 12:43:51 -08001125 TrafficSelector.Builder complementarySelector = DefaultTrafficSelector.builder();
Charles Chan14967c22015-12-07 11:11:50 -08001126
Saurav Das8a0732e2015-11-20 15:27:53 -08001127 if (ethType.ethType().toShort() == Ethernet.TYPE_IPV4) {
Pier Luigi3bfe32c2017-01-30 09:47:36 -08001128 if (buildIpv4Selector(filteredSelector, complementarySelector, fwd, allowDefaultRoute) < 0) {
1129 return Collections.emptyList();
1130 }
1131 // We need to set properly the next table
Flavio Castroe10fa242016-01-15 12:43:51 -08001132 IpPrefix ipv4Dst = ((IPCriterion) selector.getCriterion(Criterion.Type.IPV4_DST)).ip();
Charles Chan5b9df8d2016-03-28 22:21:40 -07001133 if (ipv4Dst.isMulticast()) {
Charles Chan5b9df8d2016-03-28 22:21:40 -07001134 forTableId = MULTICAST_ROUTING_TABLE;
Flavio Castroe10fa242016-01-15 12:43:51 -08001135 } else {
Charles Chan5b9df8d2016-03-28 22:21:40 -07001136 forTableId = UNICAST_ROUTING_TABLE;
Flavio Castroe10fa242016-01-15 12:43:51 -08001137 }
Charles Chan14967c22015-12-07 11:11:50 -08001138 if (fwd.treatment() != null) {
1139 for (Instruction instr : fwd.treatment().allInstructions()) {
1140 if (instr instanceof L3ModificationInstruction &&
1141 ((L3ModificationInstruction) instr).subtype() == L3SubType.DEC_TTL) {
Saurav Das9c705342017-01-06 11:36:01 -08001142 // XXX decrementing IP ttl is done automatically for routing, this
1143 // action is ignored or rejected in ofdpa as it is not fully implemented
1144 //tb.deferred().add(instr);
Charles Chan14967c22015-12-07 11:11:50 -08001145 }
1146 }
1147 }
1148
Pier Ventree0ae7a32016-11-23 09:57:42 -08001149 } else if (ethType.ethType().toShort() == Ethernet.TYPE_IPV6) {
1150 if (buildIpv6Selector(filteredSelector, fwd) < 0) {
1151 return Collections.emptyList();
1152 }
Kalhee Kim7c6f4f82017-10-04 14:59:11 +00001153 //We need to set the proper next table
1154 IpPrefix ipv6Dst = ((IPCriterion) selector.getCriterion(Criterion.Type.IPV6_DST)).ip();
1155 if (ipv6Dst.isMulticast()) {
1156 forTableId = MULTICAST_ROUTING_TABLE;
1157 } else {
1158 forTableId = UNICAST_ROUTING_TABLE;
1159 }
1160
Pier Ventree0ae7a32016-11-23 09:57:42 -08001161 if (fwd.treatment() != null) {
1162 for (Instruction instr : fwd.treatment().allInstructions()) {
1163 if (instr instanceof L3ModificationInstruction &&
1164 ((L3ModificationInstruction) instr).subtype() == L3SubType.DEC_TTL) {
1165 // XXX decrementing IP ttl is done automatically for routing, this
1166 // action is ignored or rejected in ofdpa as it is not fully implemented
1167 //tb.deferred().add(instr);
1168 }
1169 }
1170 }
Saurav Das8a0732e2015-11-20 15:27:53 -08001171 } else {
1172 filteredSelector
1173 .matchEthType(Ethernet.MPLS_UNICAST)
1174 .matchMplsLabel(((MplsCriterion)
1175 selector.getCriterion(Criterion.Type.MPLS_LABEL)).label());
1176 MplsBosCriterion bos = (MplsBosCriterion) selector
Pier Ventre140a8942016-11-02 07:26:38 -07001177 .getCriterion(MPLS_BOS);
Saurav Das8a0732e2015-11-20 15:27:53 -08001178 if (bos != null) {
1179 filteredSelector.matchMplsBos(bos.mplsBos());
1180 }
1181 forTableId = MPLS_TABLE_1;
Saurav Das4ce45962015-11-24 23:21:05 -08001182 log.debug("processing MPLS specific forwarding objective {} -> next:{}"
1183 + " in dev {}", fwd.id(), fwd.nextId(), deviceId);
Saurav Das822c4e22015-10-23 10:51:11 -07001184
Charles Chan14967c22015-12-07 11:11:50 -08001185 if (fwd.treatment() != null) {
1186 for (Instruction instr : fwd.treatment().allInstructions()) {
1187 if (instr instanceof L2ModificationInstruction &&
1188 ((L2ModificationInstruction) instr).subtype() == L2SubType.MPLS_POP) {
Saurav Das8a0732e2015-11-20 15:27:53 -08001189 popMpls = true;
Pier Luigi3bfe32c2017-01-30 09:47:36 -08001190 // OF-DPA does not pop in MPLS table in some cases. For the L3 VPN, it requires
Saurav Das9c705342017-01-06 11:36:01 -08001191 // setting the MPLS_TYPE so pop can happen down the pipeline
Pier Luigi3bfe32c2017-01-30 09:47:36 -08001192 if (mplsNextTable == MPLS_TYPE_TABLE && isNotMplsBos(selector)) {
1193 tb.immediate().popMpls();
1194 }
Charles Chan14967c22015-12-07 11:11:50 -08001195 }
1196 if (instr instanceof L3ModificationInstruction &&
1197 ((L3ModificationInstruction) instr).subtype() == L3SubType.DEC_TTL) {
1198 // FIXME Should modify the app to send the correct DEC_MPLS_TTL instruction
1199 tb.immediate().decMplsTtl();
1200 }
1201 if (instr instanceof L3ModificationInstruction &&
1202 ((L3ModificationInstruction) instr).subtype() == L3SubType.TTL_IN) {
1203 tb.immediate().add(instr);
1204 }
Saurav Das8a0732e2015-11-20 15:27:53 -08001205 }
1206 }
1207 }
Saurav Das822c4e22015-10-23 10:51:11 -07001208
1209 if (fwd.nextId() != null) {
Saurav Das8a0732e2015-11-20 15:27:53 -08001210 if (forTableId == MPLS_TABLE_1 && !popMpls) {
1211 log.warn("SR CONTINUE case cannot be handled as MPLS ECMP "
Saurav Das25190812016-05-27 13:54:07 -07001212 + "is not implemented in OF-DPA yet. Aborting this flow {} -> next:{}"
1213 + "in this device {}", fwd.id(), fwd.nextId(), deviceId);
Pier Ventree0ae7a32016-11-23 09:57:42 -08001214 // XXX We could convert to forwarding to a single-port, via a MPLS interface,
1215 // or a MPLS SWAP (with-same) but that would have to be handled in the next-objective.
1216 // Also the pop-mpls logic used here won't work in non-BoS case.
Saurav Das4ce45962015-11-24 23:21:05 -08001217 fail(fwd, ObjectiveError.FLOWINSTALLATIONFAILED);
Saurav Das8a0732e2015-11-20 15:27:53 -08001218 return Collections.emptySet();
1219 }
1220
Saurav Das423fe2b2015-12-04 10:52:59 -08001221 NextGroup next = getGroupForNextObjective(fwd.nextId());
1222 if (next != null) {
1223 List<Deque<GroupKey>> gkeys = appKryo.deserialize(next.data());
1224 // we only need the top level group's key to point the flow to it
1225 Group group = groupService.getGroup(deviceId, gkeys.get(0).peekFirst());
Pier Ventre140a8942016-11-02 07:26:38 -07001226 if (isNotMplsBos(selector) && group.type().equals(HASHED)) {
1227 log.warn("SR CONTINUE case cannot be handled as MPLS ECMP "
1228 + "is not implemented in OF-DPA yet. Aborting this flow {} -> next:{}"
1229 + "in this device {}", fwd.id(), fwd.nextId(), deviceId);
1230 fail(fwd, ObjectiveError.FLOWINSTALLATIONFAILED);
1231 return Collections.emptySet();
1232 }
Saurav Das423fe2b2015-12-04 10:52:59 -08001233 if (group == null) {
1234 log.warn("Group with key:{} for next-id:{} not found in dev:{}",
1235 gkeys.get(0).peekFirst(), fwd.nextId(), deviceId);
1236 fail(fwd, ObjectiveError.GROUPMISSING);
1237 return Collections.emptySet();
1238 }
1239 tb.deferred().group(group.id());
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001240 // check if group is empty
1241 if (gkeys.size() == 1 && gkeys.get(0).size() == 1) {
1242 log.warn("Found empty group 0x{} in dev:{} .. will retry fwd:{}",
1243 Integer.toHexString(group.id().id()), deviceId, fwd.id());
1244 emptyGroup = true;
1245 }
Saurav Das25190812016-05-27 13:54:07 -07001246 } else {
1247 log.warn("Cannot find group for nextId:{} in dev:{}. Aborting fwd:{}",
1248 fwd.nextId(), deviceId, fwd.id());
1249 fail(fwd, ObjectiveError.FLOWINSTALLATIONFAILED);
1250 return Collections.emptySet();
Saurav Das822c4e22015-10-23 10:51:11 -07001251 }
Saurav Das822c4e22015-10-23 10:51:11 -07001252 }
Charles Chancad338a2016-09-16 18:03:11 -07001253
1254 if (forTableId == MPLS_TABLE_1) {
Pier Ventre140a8942016-11-02 07:26:38 -07001255 if (mplsNextTable == MPLS_L3_TYPE_TABLE) {
Charles Chancad338a2016-09-16 18:03:11 -07001256 Ofdpa3SetMplsType setMplsType = new Ofdpa3SetMplsType(Ofdpa3MplsType.L3_PHP);
Saurav Das9c705342017-01-06 11:36:01 -08001257 // set mpls type as apply_action
1258 tb.immediate().extension(setMplsType, deviceId);
Charles Chancad338a2016-09-16 18:03:11 -07001259 }
1260 tb.transition(mplsNextTable);
1261 } else {
1262 tb.transition(ACL_TABLE);
1263 }
1264
Saurav Das822c4e22015-10-23 10:51:11 -07001265 FlowRule.Builder ruleBuilder = DefaultFlowRule.builder()
1266 .fromApp(fwd.appId())
1267 .withPriority(fwd.priority())
1268 .forDevice(deviceId)
Saurav Das8a0732e2015-11-20 15:27:53 -08001269 .withSelector(filteredSelector.build())
1270 .withTreatment(tb.build())
1271 .forTable(forTableId);
Saurav Das822c4e22015-10-23 10:51:11 -07001272
1273 if (fwd.permanent()) {
1274 ruleBuilder.makePermanent();
1275 } else {
1276 ruleBuilder.makeTemporary(fwd.timeout());
1277 }
Flavio Castroe10fa242016-01-15 12:43:51 -08001278 Collection<FlowRule> flowRuleCollection = new ArrayList<>();
1279 flowRuleCollection.add(ruleBuilder.build());
Pier Luigi3bfe32c2017-01-30 09:47:36 -08001280 if (!allowDefaultRoute) {
Pier Ventree0ae7a32016-11-23 09:57:42 -08001281 flowRuleCollection.add(
1282 defaultRoute(fwd, complementarySelector, forTableId, tb)
1283 );
Flavio Castroe10fa242016-01-15 12:43:51 -08001284 log.debug("Default rule 0.0.0.0/0 is being installed two rules");
1285 }
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001286 // XXX retrying flows may be necessary due to bug CORD-554
1287 if (emptyGroup) {
1288 executorService.schedule(new RetryFlows(fwd, flowRuleCollection),
1289 RETRY_MS, TimeUnit.MILLISECONDS);
1290 }
Flavio Castroe10fa242016-01-15 12:43:51 -08001291 return flowRuleCollection;
Saurav Das822c4e22015-10-23 10:51:11 -07001292 }
1293
Pier Luigi3bfe32c2017-01-30 09:47:36 -08001294 protected int buildIpv4Selector(TrafficSelector.Builder builderToUpdate,
1295 TrafficSelector.Builder extBuilder,
1296 ForwardingObjective fwd,
1297 boolean allowDefaultRoute) {
1298 TrafficSelector selector = fwd.selector();
1299
1300 IpPrefix ipv4Dst = ((IPCriterion) selector.getCriterion(Criterion.Type.IPV4_DST)).ip();
1301 if (ipv4Dst.isMulticast()) {
1302 if (ipv4Dst.prefixLength() != 32) {
1303 log.warn("Multicast specific forwarding objective can only be /32");
1304 fail(fwd, ObjectiveError.BADPARAMS);
1305 return -1;
1306 }
1307 VlanId assignedVlan = readVlanFromSelector(fwd.meta());
1308 if (assignedVlan == null) {
1309 log.warn("VLAN ID required by multicast specific fwd obj is missing. Abort.");
1310 fail(fwd, ObjectiveError.BADPARAMS);
1311 return -1;
1312 }
Charles Chand1172632017-03-15 17:33:09 -07001313 if (requireVlanExtensions()) {
1314 OfdpaMatchVlanVid ofdpaMatchVlanVid = new OfdpaMatchVlanVid(assignedVlan);
1315 builderToUpdate.extension(ofdpaMatchVlanVid, deviceId);
1316 } else {
1317 builderToUpdate.matchVlanId(assignedVlan);
1318 }
Pier Luigi3bfe32c2017-01-30 09:47:36 -08001319 builderToUpdate.matchEthType(Ethernet.TYPE_IPV4).matchIPDst(ipv4Dst);
1320 log.debug("processing IPv4 multicast specific forwarding objective {} -> next:{}"
1321 + " in dev:{}", fwd.id(), fwd.nextId(), deviceId);
1322 } else {
1323 if (ipv4Dst.prefixLength() == 0) {
1324 if (allowDefaultRoute) {
1325 // The entire IPV4_DST field is wildcarded intentionally
1326 builderToUpdate.matchEthType(Ethernet.TYPE_IPV4);
1327 } else {
1328 // NOTE: The switch does not support matching 0.0.0.0/0
1329 // Split it into 0.0.0.0/1 and 128.0.0.0/1
1330 builderToUpdate.matchEthType(Ethernet.TYPE_IPV4)
1331 .matchIPDst(IpPrefix.valueOf("0.0.0.0/1"));
1332 extBuilder.matchEthType(Ethernet.TYPE_IPV4)
1333 .matchIPDst(IpPrefix.valueOf("128.0.0.0/1"));
1334 }
1335 } else {
1336 builderToUpdate.matchEthType(Ethernet.TYPE_IPV4).matchIPDst(ipv4Dst);
1337 }
1338 log.debug("processing IPv4 unicast specific forwarding objective {} -> next:{}"
1339 + " in dev:{}", fwd.id(), fwd.nextId(), deviceId);
1340 }
1341 return 0;
1342 }
1343
1344 /**
1345 * Helper method to build Ipv6 selector using the selector provided by
1346 * a forwarding objective.
1347 *
1348 * @param builderToUpdate the builder to update
1349 * @param fwd the selector to read
1350 * @return 0 if the update ends correctly. -1 if the matches
1351 * are not yet supported
1352 */
1353 protected int buildIpv6Selector(TrafficSelector.Builder builderToUpdate,
1354 ForwardingObjective fwd) {
1355
1356 TrafficSelector selector = fwd.selector();
1357
1358 IpPrefix ipv6Dst = ((IPCriterion) selector.getCriterion(Criterion.Type.IPV6_DST)).ip();
1359 if (ipv6Dst.isMulticast()) {
Julia Fergusond8f145e2017-08-10 18:15:24 +00001360 if (ipv6Dst.prefixLength() != IpAddress.INET6_BIT_LENGTH) {
1361 log.warn("Multicast specific forwarding objective can only be /128");
1362 fail(fwd, ObjectiveError.BADPARAMS);
1363 return -1;
1364 }
1365 VlanId assignedVlan = readVlanFromSelector(fwd.meta());
1366 if (assignedVlan == null) {
1367 log.warn("VLAN ID required by multicast specific fwd obj is missing. Abort.");
1368 fail(fwd, ObjectiveError.BADPARAMS);
1369 return -1;
1370 }
1371 if (requireVlanExtensions()) {
1372 OfdpaMatchVlanVid ofdpaMatchVlanVid = new OfdpaMatchVlanVid(assignedVlan);
1373 builderToUpdate.extension(ofdpaMatchVlanVid, deviceId);
1374 } else {
1375 builderToUpdate.matchVlanId(assignedVlan);
1376 }
1377 builderToUpdate.matchEthType(Ethernet.TYPE_IPV6).matchIPv6Dst(ipv6Dst);
1378 log.debug("processing IPv6 multicast specific forwarding objective {} -> next:{}"
1379 + " in dev:{}", fwd.id(), fwd.nextId(), deviceId);
1380 } else {
1381 if (ipv6Dst.prefixLength() != 0) {
1382 builderToUpdate.matchIPv6Dst(ipv6Dst);
1383 }
Pier Luigi3bfe32c2017-01-30 09:47:36 -08001384 builderToUpdate.matchEthType(Ethernet.TYPE_IPV6);
1385 log.debug("processing IPv6 unicast specific forwarding objective {} -> next:{}"
1386 + " in dev:{}", fwd.id(), fwd.nextId(), deviceId);
Julia Fergusond8f145e2017-08-10 18:15:24 +00001387 }
Pier Luigi3bfe32c2017-01-30 09:47:36 -08001388 return 0;
1389 }
1390
Pier Ventree0ae7a32016-11-23 09:57:42 -08001391 protected FlowRule defaultRoute(ForwardingObjective fwd,
1392 TrafficSelector.Builder complementarySelector,
1393 int forTableId,
1394 TrafficTreatment.Builder tb) {
1395 FlowRule.Builder rule = DefaultFlowRule.builder()
1396 .fromApp(fwd.appId())
1397 .withPriority(fwd.priority())
1398 .forDevice(deviceId)
1399 .withSelector(complementarySelector.build())
1400 .withTreatment(tb.build())
1401 .forTable(forTableId);
1402 if (fwd.permanent()) {
1403 rule.makePermanent();
1404 } else {
1405 rule.makeTemporary(fwd.timeout());
1406 }
1407 return rule.build();
1408 }
1409
Saurav Das4ce45962015-11-24 23:21:05 -08001410 /**
1411 * Handles forwarding rules to the L2 bridging table. Flow actions are not
1412 * allowed in the bridging table - instead we use L2 Interface group or
1413 * L2 flood group
1414 *
1415 * @param fwd the forwarding objective
1416 * @return A collection of flow rules, or an empty set
1417 */
1418 protected Collection<FlowRule> processEthDstSpecific(ForwardingObjective fwd) {
1419 List<FlowRule> rules = new ArrayList<>();
1420
1421 // Build filtered selector
1422 TrafficSelector selector = fwd.selector();
1423 EthCriterion ethCriterion = (EthCriterion) selector
1424 .getCriterion(Criterion.Type.ETH_DST);
1425 VlanIdCriterion vlanIdCriterion = (VlanIdCriterion) selector
1426 .getCriterion(Criterion.Type.VLAN_VID);
1427
1428 if (vlanIdCriterion == null) {
1429 log.warn("Forwarding objective for bridging requires vlan. Not "
1430 + "installing fwd:{} in dev:{}", fwd.id(), deviceId);
1431 fail(fwd, ObjectiveError.BADPARAMS);
1432 return Collections.emptySet();
1433 }
1434
1435 TrafficSelector.Builder filteredSelectorBuilder =
1436 DefaultTrafficSelector.builder();
Yi Tseng3a77b4f2017-02-06 15:02:17 -08001437
1438 if (!ethCriterion.mac().equals(NONE) &&
1439 !ethCriterion.mac().equals(BROADCAST)) {
Saurav Das4ce45962015-11-24 23:21:05 -08001440 filteredSelectorBuilder.matchEthDst(ethCriterion.mac());
1441 log.debug("processing L2 forwarding objective:{} -> next:{} in dev:{}",
1442 fwd.id(), fwd.nextId(), deviceId);
1443 } else {
Yi Tseng3a77b4f2017-02-06 15:02:17 -08001444 // Use wildcard DST_MAC if the MacAddress is None or Broadcast
Saurav Das4ce45962015-11-24 23:21:05 -08001445 log.debug("processing L2 Broadcast forwarding objective:{} -> next:{} "
1446 + "in dev:{} for vlan:{}",
1447 fwd.id(), fwd.nextId(), deviceId, vlanIdCriterion.vlanId());
1448 }
Charles Chand1172632017-03-15 17:33:09 -07001449 if (requireVlanExtensions()) {
1450 OfdpaMatchVlanVid ofdpaMatchVlanVid = new OfdpaMatchVlanVid(vlanIdCriterion.vlanId());
1451 filteredSelectorBuilder.extension(ofdpaMatchVlanVid, deviceId);
1452 } else {
1453 filteredSelectorBuilder.matchVlanId(vlanIdCriterion.vlanId());
1454 }
Saurav Das4ce45962015-11-24 23:21:05 -08001455 TrafficSelector filteredSelector = filteredSelectorBuilder.build();
1456
1457 if (fwd.treatment() != null) {
1458 log.warn("Ignoring traffic treatment in fwd rule {} meant for L2 table"
1459 + "for dev:{}. Expecting only nextId", fwd.id(), deviceId);
1460 }
1461
1462 TrafficTreatment.Builder treatmentBuilder = DefaultTrafficTreatment.builder();
1463 if (fwd.nextId() != null) {
Saurav Das423fe2b2015-12-04 10:52:59 -08001464 NextGroup next = getGroupForNextObjective(fwd.nextId());
Saurav Das4ce45962015-11-24 23:21:05 -08001465 if (next != null) {
1466 List<Deque<GroupKey>> gkeys = appKryo.deserialize(next.data());
1467 // we only need the top level group's key to point the flow to it
1468 Group group = groupService.getGroup(deviceId, gkeys.get(0).peekFirst());
1469 if (group != null) {
1470 treatmentBuilder.deferred().group(group.id());
1471 } else {
1472 log.warn("Group with key:{} for next-id:{} not found in dev:{}",
1473 gkeys.get(0).peekFirst(), fwd.nextId(), deviceId);
1474 fail(fwd, ObjectiveError.GROUPMISSING);
1475 return Collections.emptySet();
1476 }
1477 }
1478 }
1479 treatmentBuilder.immediate().transition(ACL_TABLE);
1480 TrafficTreatment filteredTreatment = treatmentBuilder.build();
1481
1482 // Build bridging table entries
1483 FlowRule.Builder flowRuleBuilder = DefaultFlowRule.builder();
1484 flowRuleBuilder.fromApp(fwd.appId())
1485 .withPriority(fwd.priority())
1486 .forDevice(deviceId)
1487 .withSelector(filteredSelector)
1488 .withTreatment(filteredTreatment)
1489 .forTable(BRIDGING_TABLE);
1490 if (fwd.permanent()) {
1491 flowRuleBuilder.makePermanent();
1492 } else {
1493 flowRuleBuilder.makeTemporary(fwd.timeout());
1494 }
1495 rules.add(flowRuleBuilder.build());
1496 return rules;
1497 }
1498
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001499 //////////////////////////////////////
1500 // Helper Methods and Classes
1501 //////////////////////////////////////
1502
1503 private boolean isSupportedEthTypeObjective(ForwardingObjective fwd) {
1504 TrafficSelector selector = fwd.selector();
1505 EthTypeCriterion ethType = (EthTypeCriterion) selector
1506 .getCriterion(Criterion.Type.ETH_TYPE);
1507 return !((ethType == null) ||
1508 ((ethType.ethType().toShort() != Ethernet.TYPE_IPV4) &&
Pier Ventree0ae7a32016-11-23 09:57:42 -08001509 (ethType.ethType().toShort() != Ethernet.MPLS_UNICAST)) &&
1510 (ethType.ethType().toShort() != Ethernet.TYPE_IPV6));
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001511 }
1512
1513 private boolean isSupportedEthDstObjective(ForwardingObjective fwd) {
1514 TrafficSelector selector = fwd.selector();
1515 EthCriterion ethDst = (EthCriterion) selector
1516 .getCriterion(Criterion.Type.ETH_DST);
1517 VlanIdCriterion vlanId = (VlanIdCriterion) selector
1518 .getCriterion(Criterion.Type.VLAN_VID);
1519 return !(ethDst == null && vlanId == null);
1520 }
1521
Saurav Das423fe2b2015-12-04 10:52:59 -08001522 protected NextGroup getGroupForNextObjective(Integer nextId) {
1523 NextGroup next = flowObjectiveStore.getNextGroup(nextId);
1524 if (next != null) {
1525 List<Deque<GroupKey>> gkeys = appKryo.deserialize(next.data());
1526 if (gkeys != null && !gkeys.isEmpty()) {
1527 return next;
1528 } else {
1529 log.warn("Empty next group found in FlowObjective store for "
1530 + "next-id:{} in dev:{}", nextId, deviceId);
1531 }
1532 } else {
1533 log.warn("next-id {} not found in Flow objective store for dev:{}",
1534 nextId, deviceId);
1535 }
1536 return null;
1537 }
1538
Charles Chan188ebf52015-12-23 00:15:11 -08001539 protected static void pass(Objective obj) {
Sho SHIMIZUef7e2902016-02-12 18:38:29 -08001540 obj.context().ifPresent(context -> context.onSuccess(obj));
Saurav Das822c4e22015-10-23 10:51:11 -07001541 }
1542
Charles Chan188ebf52015-12-23 00:15:11 -08001543 protected static void fail(Objective obj, ObjectiveError error) {
Sho SHIMIZUef7e2902016-02-12 18:38:29 -08001544 obj.context().ifPresent(context -> context.onError(obj, error));
Saurav Das822c4e22015-10-23 10:51:11 -07001545 }
Saurav Das24431192016-03-07 19:13:00 -08001546
Saurav Das24431192016-03-07 19:13:00 -08001547 @Override
1548 public List<String> getNextMappings(NextGroup nextGroup) {
1549 List<String> mappings = new ArrayList<>();
1550 List<Deque<GroupKey>> gkeys = appKryo.deserialize(nextGroup.data());
1551 for (Deque<GroupKey> gkd : gkeys) {
1552 Group lastGroup = null;
Yuta HIGUCHI2dce08a2017-04-20 21:57:48 -07001553 StringBuilder gchain = new StringBuilder();
Saurav Das24431192016-03-07 19:13:00 -08001554 for (GroupKey gk : gkd) {
1555 Group g = groupService.getGroup(deviceId, gk);
Saurav Das8be4e3a2016-03-11 17:19:07 -08001556 if (g == null) {
Saurav Das25190812016-05-27 13:54:07 -07001557 gchain.append(" NoGrp").append(" -->");
Saurav Das8be4e3a2016-03-11 17:19:07 -08001558 continue;
1559 }
1560 gchain.append(" 0x").append(Integer.toHexString(g.id().id()))
1561 .append(" -->");
Saurav Das24431192016-03-07 19:13:00 -08001562 lastGroup = g;
1563 }
1564 // add port information for last group in group-chain
Yuta HIGUCHI2dce08a2017-04-20 21:57:48 -07001565 List<Instruction> lastGroupIns = new ArrayList<>();
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001566 if (lastGroup != null && !lastGroup.buckets().buckets().isEmpty()) {
Saurav Das25190812016-05-27 13:54:07 -07001567 lastGroupIns = lastGroup.buckets().buckets().get(0)
1568 .treatment().allInstructions();
1569 }
1570 for (Instruction i: lastGroupIns) {
Saurav Das24431192016-03-07 19:13:00 -08001571 if (i instanceof OutputInstruction) {
Saurav Das8be4e3a2016-03-11 17:19:07 -08001572 gchain.append(" port:").append(((OutputInstruction) i).port());
Saurav Das24431192016-03-07 19:13:00 -08001573 }
1574 }
Saurav Das8be4e3a2016-03-11 17:19:07 -08001575 mappings.add(gchain.toString());
Saurav Das24431192016-03-07 19:13:00 -08001576 }
1577 return mappings;
1578 }
Charles Chan5b9df8d2016-03-28 22:21:40 -07001579
Pier Ventre140a8942016-11-02 07:26:38 -07001580 static boolean isMplsBos(TrafficSelector selector) {
1581 MplsBosCriterion bosCriterion = (MplsBosCriterion) selector.getCriterion(MPLS_BOS);
1582 return bosCriterion != null && bosCriterion.mplsBos();
1583 }
1584
1585 static boolean isNotMplsBos(TrafficSelector selector) {
1586 MplsBosCriterion bosCriterion = (MplsBosCriterion) selector.getCriterion(MPLS_BOS);
1587 return bosCriterion != null && !bosCriterion.mplsBos();
1588 }
1589
Charles Chan30122a72017-10-05 15:17:15 -07001590 private static boolean isIpv6(TrafficSelector selector) {
1591 EthTypeCriterion ethTypeCriterion = (EthTypeCriterion) selector.getCriterion(ETH_TYPE);
1592 return ethTypeCriterion != null && ethTypeCriterion.ethType().toShort() == Ethernet.TYPE_IPV6;
1593 }
1594
1595 static VlanId readVlanFromSelector(TrafficSelector selector) {
Saurav Das59232cf2016-04-27 18:35:50 -07001596 if (selector == null) {
1597 return null;
1598 }
Charles Chan5b9df8d2016-03-28 22:21:40 -07001599 Criterion criterion = selector.getCriterion(Criterion.Type.VLAN_VID);
1600 return (criterion == null)
1601 ? null : ((VlanIdCriterion) criterion).vlanId();
1602 }
1603
Charles Chan30122a72017-10-05 15:17:15 -07001604 static IpPrefix readIpDstFromSelector(TrafficSelector selector) {
Saurav Das59232cf2016-04-27 18:35:50 -07001605 if (selector == null) {
1606 return null;
1607 }
Charles Chan5b9df8d2016-03-28 22:21:40 -07001608 Criterion criterion = selector.getCriterion(Criterion.Type.IPV4_DST);
1609 return (criterion == null) ? null : ((IPCriterion) criterion).ip();
1610 }
Charles Chand55e84d2016-03-30 17:54:24 -07001611
1612 private static VlanId readVlanFromTreatment(TrafficTreatment treatment) {
Saurav Das59232cf2016-04-27 18:35:50 -07001613 if (treatment == null) {
1614 return null;
1615 }
Charles Chand55e84d2016-03-30 17:54:24 -07001616 for (Instruction i : treatment.allInstructions()) {
1617 if (i instanceof ModVlanIdInstruction) {
1618 return ((ModVlanIdInstruction) i).vlanId();
1619 }
1620 }
1621 return null;
1622 }
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001623
1624 /**
1625 * Utility class that retries sending flows a fixed number of times, even if
1626 * some of the attempts are successful. Used only for forwarding objectives.
1627 */
1628 protected final class RetryFlows implements Runnable {
1629 int attempts = MAX_RETRY_ATTEMPTS;
1630 private Collection<FlowRule> retryFlows;
1631 private ForwardingObjective fwd;
1632
1633 RetryFlows(ForwardingObjective fwd, Collection<FlowRule> retryFlows) {
1634 this.fwd = fwd;
1635 this.retryFlows = retryFlows;
1636 }
1637
1638 @Override
1639 public void run() {
1640 log.info("RETRY FLOWS ATTEMPT# {} for fwd:{} rules:{}",
1641 MAX_RETRY_ATTEMPTS - attempts, fwd.id(), retryFlows.size());
1642 sendForward(fwd, retryFlows);
1643 if (--attempts > 0) {
1644 executorService.schedule(this, RETRY_MS, TimeUnit.MILLISECONDS);
1645 }
1646 }
1647 }
1648
Saurav Das822c4e22015-10-23 10:51:11 -07001649}