blob: 0c4a85282586139135d799ac003e6fa9c5a77e0e [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 Das95047002018-01-25 09:49:01 -0800226 /**
227 * Determines whether this driver should continue to retry flows that point
228 * to empty groups. See CORD-554.
229 *
230 * @return true if the driver should retry flows
231 */
232 protected boolean shouldRetry() {
233 return true;
234 }
235
Saurav Das822c4e22015-10-23 10:51:11 -0700236 //////////////////////////////////////
237 // Flow Objectives
238 //////////////////////////////////////
239
240 @Override
241 public void filter(FilteringObjective filteringObjective) {
242 if (filteringObjective.type() == FilteringObjective.Type.PERMIT) {
243 processFilter(filteringObjective,
244 filteringObjective.op() == Objective.Operation.ADD,
245 filteringObjective.appId());
246 } else {
247 // Note that packets that don't match the PERMIT filter are
248 // automatically denied. The DENY filter is used to deny packets
249 // that are otherwise permitted by the PERMIT filter.
250 // Use ACL table flow rules here for DENY filtering objectives
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530251 log.warn("filter objective other than PERMIT currently not supported");
Saurav Das822c4e22015-10-23 10:51:11 -0700252 fail(filteringObjective, ObjectiveError.UNSUPPORTED);
253 }
254 }
255
256 @Override
257 public void forward(ForwardingObjective fwd) {
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700258 Collection<FlowRule> rules = processForward(fwd);
Saurav Das25190812016-05-27 13:54:07 -0700259 if (rules == null || rules.isEmpty()) {
260 // Assumes fail message has already been generated to the objective
261 // context. Returning here prevents spurious pass message to be
262 // generated by FlowRule service for empty flowOps.
263 return;
264 }
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700265 sendForward(fwd, rules);
266 }
267
268 protected void sendForward(ForwardingObjective fwd, Collection<FlowRule> rules) {
269 FlowRuleOperations.Builder flowOpsBuilder = FlowRuleOperations.builder();
Saurav Das822c4e22015-10-23 10:51:11 -0700270 switch (fwd.op()) {
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700271 case ADD:
272 rules.stream()
273 .filter(Objects::nonNull)
274 .forEach(flowOpsBuilder::add);
Saurav Dasd2fded02016-12-02 15:43:47 -0800275 log.debug("Applying a add fwd-obj {} to sw:{}", fwd.id(), deviceId);
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700276 break;
277 case REMOVE:
278 rules.stream()
279 .filter(Objects::nonNull)
280 .forEach(flowOpsBuilder::remove);
Pier Ventre42287df2016-11-09 14:17:26 -0800281 log.debug("Deleting a flow rule to sw:{}", deviceId);
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700282 break;
283 default:
284 fail(fwd, ObjectiveError.UNKNOWN);
285 log.warn("Unknown forwarding type {}", fwd.op());
Saurav Das822c4e22015-10-23 10:51:11 -0700286 }
287
Saurav Das822c4e22015-10-23 10:51:11 -0700288 flowRuleService.apply(flowOpsBuilder.build(new FlowRuleOperationsContext() {
289 @Override
290 public void onSuccess(FlowRuleOperations ops) {
291 pass(fwd);
292 }
293
294 @Override
295 public void onError(FlowRuleOperations ops) {
296 fail(fwd, ObjectiveError.FLOWINSTALLATIONFAILED);
297 }
298 }));
Saurav Das822c4e22015-10-23 10:51:11 -0700299 }
300
301 @Override
302 public void next(NextObjective nextObjective) {
Saurav Das8a0732e2015-11-20 15:27:53 -0800303 NextGroup nextGroup = flowObjectiveStore.getNextGroup(nextObjective.id());
304 switch (nextObjective.op()) {
305 case ADD:
Saurav Das4f980082015-11-05 13:39:15 -0800306 if (nextGroup != null) {
Saurav Das8a0732e2015-11-20 15:27:53 -0800307 log.warn("Cannot add next {} that already exists in device {}",
308 nextObjective.id(), deviceId);
309 return;
310 }
Saurav Das1547b3f2017-05-05 17:01:08 -0700311 log.debug("Processing NextObjective id {} in dev {} - add group",
Saurav Das8a0732e2015-11-20 15:27:53 -0800312 nextObjective.id(), deviceId);
Charles Chan425854b2016-04-11 15:32:12 -0700313 groupHandler.addGroup(nextObjective);
Saurav Das8a0732e2015-11-20 15:27:53 -0800314 break;
315 case ADD_TO_EXISTING:
316 if (nextGroup != null) {
Saurav Das1547b3f2017-05-05 17:01:08 -0700317 log.debug("Processing NextObjective id {} in dev {} - add bucket",
Saurav Das8a0732e2015-11-20 15:27:53 -0800318 nextObjective.id(), deviceId);
Charles Chan425854b2016-04-11 15:32:12 -0700319 groupHandler.addBucketToGroup(nextObjective, nextGroup);
Saurav Das4f980082015-11-05 13:39:15 -0800320 } else {
Saurav Das8a0732e2015-11-20 15:27:53 -0800321 // it is possible that group-chain has not been fully created yet
Saurav Das423fe2b2015-12-04 10:52:59 -0800322 log.debug("Waiting to add bucket to group for next-id:{} in dev:{}",
323 nextObjective.id(), deviceId);
Yi Tseng47f82dc2017-03-05 22:48:39 -0800324
325 // by design multiple pending bucket is allowed for the group
326 groupHandler.pendingBuckets.compute(nextObjective.id(), (nextId, pendBkts) -> {
327 if (pendBkts == null) {
328 pendBkts = Sets.newHashSet();
329 }
330 pendBkts.add(nextObjective);
331 return pendBkts;
332 });
Saurav Das4f980082015-11-05 13:39:15 -0800333 }
Saurav Das8a0732e2015-11-20 15:27:53 -0800334 break;
335 case REMOVE:
336 if (nextGroup == null) {
337 log.warn("Cannot remove 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 group",
Saurav Das8a0732e2015-11-20 15:27:53 -0800342 nextObjective.id(), deviceId);
Charles Chan425854b2016-04-11 15:32:12 -0700343 groupHandler.removeGroup(nextObjective, nextGroup);
Saurav Das8a0732e2015-11-20 15:27:53 -0800344 break;
345 case REMOVE_FROM_EXISTING:
346 if (nextGroup == null) {
347 log.warn("Cannot remove from next {} that does not exist in device {}",
348 nextObjective.id(), deviceId);
349 return;
350 }
Saurav Das1547b3f2017-05-05 17:01:08 -0700351 log.debug("Processing NextObjective id {} in dev {} - remove bucket",
Saurav Das8a0732e2015-11-20 15:27:53 -0800352 nextObjective.id(), deviceId);
Charles Chan425854b2016-04-11 15:32:12 -0700353 groupHandler.removeBucketFromGroup(nextObjective, nextGroup);
Saurav Das8a0732e2015-11-20 15:27:53 -0800354 break;
Saurav Dasceccf242017-08-03 18:30:35 -0700355 case VERIFY:
356 if (nextGroup == null) {
357 log.warn("Cannot verify next {} that does not exist in device {}",
358 nextObjective.id(), deviceId);
359 return;
360 }
361 log.debug("Processing NextObjective id {} in dev {} - verify",
362 nextObjective.id(), deviceId);
363 groupHandler.verifyGroup(nextObjective, nextGroup);
364 break;
Saurav Das8a0732e2015-11-20 15:27:53 -0800365 default:
Saurav Das4f980082015-11-05 13:39:15 -0800366 log.warn("Unsupported operation {}", nextObjective.op());
Saurav Das822c4e22015-10-23 10:51:11 -0700367 }
368 }
369
370 //////////////////////////////////////
371 // Flow handling
372 //////////////////////////////////////
373
374 /**
Yi Tseng0fdc07e2017-04-24 19:17:08 -0700375 * As per OFDPA 2.0 TTP, filtering of VLAN ids and MAC addresses (for routing)
376 * configured on switch ports happen in different tables.
Saurav Das822c4e22015-10-23 10:51:11 -0700377 *
378 * @param filt the filtering objective
379 * @param install indicates whether to add or remove the objective
380 * @param applicationId the application that sent this objective
381 */
Saurav Das52025962016-01-28 22:30:01 -0800382 protected void processFilter(FilteringObjective filt,
383 boolean install, ApplicationId applicationId) {
Saurav Das822c4e22015-10-23 10:51:11 -0700384 // This driver only processes filtering criteria defined with switch
385 // ports as the key
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530386 PortCriterion portCriterion = null;
Saurav Das822c4e22015-10-23 10:51:11 -0700387 EthCriterion ethCriterion = null;
388 VlanIdCriterion vidCriterion = null;
Saurav Das822c4e22015-10-23 10:51:11 -0700389 if (!filt.key().equals(Criteria.dummy()) &&
390 filt.key().type() == Criterion.Type.IN_PORT) {
391 portCriterion = (PortCriterion) filt.key();
Saurav Das822c4e22015-10-23 10:51:11 -0700392 }
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530393 if (portCriterion == null) {
394 log.debug("No IN_PORT defined in filtering objective from app: {}",
395 applicationId);
396 } else {
397 log.debug("Received filtering objective for dev/port: {}/{}", deviceId,
398 portCriterion.port());
399 }
Saurav Das822c4e22015-10-23 10:51:11 -0700400 // convert filtering conditions for switch-intfs into flowrules
401 FlowRuleOperations.Builder ops = FlowRuleOperations.builder();
402 for (Criterion criterion : filt.conditions()) {
Yi Tseng0fdc07e2017-04-24 19:17:08 -0700403 switch (criterion.type()) {
404 case ETH_DST:
405 case ETH_DST_MASKED:
406 ethCriterion = (EthCriterion) criterion;
407 break;
408 case VLAN_VID:
409 vidCriterion = (VlanIdCriterion) criterion;
410 break;
411 default:
412 log.warn("Unsupported filter {}", criterion);
413 fail(filt, ObjectiveError.UNSUPPORTED);
414 return;
Saurav Das822c4e22015-10-23 10:51:11 -0700415 }
416 }
417
Saurav Das0e99e2b2015-10-28 12:39:42 -0700418 VlanId assignedVlan = null;
Charles Chane849c192016-01-11 18:28:54 -0800419 if (vidCriterion != null) {
Charles Chand55e84d2016-03-30 17:54:24 -0700420 // Use the VLAN in criterion if metadata is not present and the traffic is tagged
Charles Chan0d6db512017-12-19 19:55:57 -0800421 if (!vidCriterion.vlanId().equals(VlanId.NONE)) {
Charles Chane849c192016-01-11 18:28:54 -0800422 assignedVlan = vidCriterion.vlanId();
Charles Chan0d6db512017-12-19 19:55:57 -0800423 } else if (filt.meta() != null) {
424 assignedVlan = readVlanFromTreatment(filt.meta());
Charles Chand55e84d2016-03-30 17:54:24 -0700425 }
Charles Chane849c192016-01-11 18:28:54 -0800426
Charles Chand55e84d2016-03-30 17:54:24 -0700427 if (assignedVlan == null) {
428 log.error("Driver fails to extract VLAN information. "
Frank Wang3a98e0a2017-08-11 11:09:30 +0800429 + "Not processing VLAN filters on device {}.", deviceId);
Charles Chand55e84d2016-03-30 17:54:24 -0700430 log.debug("VLAN ID in criterion={}, metadata={}",
431 readVlanFromTreatment(filt.meta()), vidCriterion.vlanId());
432 fail(filt, ObjectiveError.BADPARAMS);
433 return;
Saurav Das0e99e2b2015-10-28 12:39:42 -0700434 }
435 }
436
Yi Tseng3a77b4f2017-02-06 15:02:17 -0800437 if (ethCriterion == null || ethCriterion.mac().equals(NONE)) {
Charles Chan985b12e2016-05-11 19:47:22 -0700438 // NOTE: it is possible that a filtering objective only has vidCriterion
Saurav Das961beb22017-03-29 19:09:17 -0700439 log.warn("filtering objective missing dstMac, cannot program TMAC table");
Saurav Das822c4e22015-10-23 10:51:11 -0700440 } else {
441 for (FlowRule tmacRule : processEthDstFilter(portCriterion, ethCriterion,
Saurav Das0e99e2b2015-10-28 12:39:42 -0700442 vidCriterion, assignedVlan,
443 applicationId)) {
Saurav Das961beb22017-03-29 19:09:17 -0700444 log.trace("{} MAC filtering rules in TMAC table: {} for dev: {}",
Saurav Das018605f2017-02-18 14:05:44 -0800445 (install) ? "adding" : "removing", tmacRule, deviceId);
Charles Chan0d6db512017-12-19 19:55:57 -0800446
447 if (install) {
448 ops = ops.add(tmacRule);
449 } else {
450 // NOTE: Only remove TMAC flow when there is no more enabled port within the
451 // same VLAN on this device if TMAC doesn't support matching on in_port.
452 if (matchInPortTmacTable() || (filt.meta() != null && filt.meta().clearedDeferred())) {
453 ops = ops.remove(tmacRule);
454 } else {
455 log.debug("Abort TMAC flow removal on {}. Some other ports still share this TMAC flow");
456 }
457 }
Saurav Das822c4e22015-10-23 10:51:11 -0700458 }
459 }
460
Charles Chan985b12e2016-05-11 19:47:22 -0700461 if (vidCriterion == null) {
462 // NOTE: it is possible that a filtering objective only has ethCriterion
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530463 log.info("filtering objective missing VLAN, cannot program VLAN Table");
Saurav Das822c4e22015-10-23 10:51:11 -0700464 } else {
Charles Chan14967c22015-12-07 11:11:50 -0800465 /*
466 * NOTE: Separate vlan filtering rules and assignment rules
467 * into different stage in order to guarantee that filtering rules
Saurav Das52025962016-01-28 22:30:01 -0800468 * always go first, as required by ofdpa.
Charles Chan14967c22015-12-07 11:11:50 -0800469 */
470 List<FlowRule> allRules = processVlanIdFilter(
471 portCriterion, vidCriterion, assignedVlan, applicationId);
472 List<FlowRule> filteringRules = new ArrayList<>();
473 List<FlowRule> assignmentRules = new ArrayList<>();
474
475 allRules.forEach(flowRule -> {
Charles Chand1172632017-03-15 17:33:09 -0700476 VlanId vlanId;
477 if (requireVlanExtensions()) {
478 ExtensionCriterion extCriterion =
479 (ExtensionCriterion) flowRule.selector().getCriterion(Criterion.Type.EXTENSION);
480 vlanId = ((OfdpaMatchVlanVid) extCriterion.extensionSelector()).vlanId();
481 } else {
482 VlanIdCriterion vlanIdCriterion =
483 (VlanIdCriterion) flowRule.selector().getCriterion(Criterion.Type.VLAN_VID);
484 vlanId = vlanIdCriterion.vlanId();
485 }
Charles Chanbe8aea42016-02-24 12:04:47 -0800486 if (!vlanId.equals(VlanId.NONE)) {
Charles Chan14967c22015-12-07 11:11:50 -0800487 filteringRules.add(flowRule);
488 } else {
489 assignmentRules.add(flowRule);
490 }
491 });
492
493 for (FlowRule filteringRule : filteringRules) {
Saurav Das961beb22017-03-29 19:09:17 -0700494 log.trace("{} VLAN filtering rule in VLAN table: {} for dev: {}",
Saurav Das018605f2017-02-18 14:05:44 -0800495 (install) ? "adding" : "removing", filteringRule, deviceId);
Charles Chan14967c22015-12-07 11:11:50 -0800496 ops = install ? ops.add(filteringRule) : ops.remove(filteringRule);
497 }
498
499 ops.newStage();
500
501 for (FlowRule assignmentRule : assignmentRules) {
Saurav Das961beb22017-03-29 19:09:17 -0700502 log.trace("{} VLAN assignment rule in VLAN table: {} for dev: {}",
Saurav Das018605f2017-02-18 14:05:44 -0800503 (install) ? "adding" : "removing", assignmentRule, deviceId);
Charles Chan14967c22015-12-07 11:11:50 -0800504 ops = install ? ops.add(assignmentRule) : ops.remove(assignmentRule);
Saurav Das822c4e22015-10-23 10:51:11 -0700505 }
506 }
507
Saurav Das822c4e22015-10-23 10:51:11 -0700508 // apply filtering flow rules
509 flowRuleService.apply(ops.build(new FlowRuleOperationsContext() {
510 @Override
511 public void onSuccess(FlowRuleOperations ops) {
Saurav Das018605f2017-02-18 14:05:44 -0800512 log.debug("Applied {} filtering rules in device {}",
Saurav Das822c4e22015-10-23 10:51:11 -0700513 ops.stages().get(0).size(), deviceId);
514 pass(filt);
515 }
516
517 @Override
518 public void onError(FlowRuleOperations ops) {
519 log.info("Failed to apply all filtering rules in dev {}", deviceId);
520 fail(filt, ObjectiveError.FLOWINSTALLATIONFAILED);
521 }
522 }));
523
524 }
525
526 /**
Charles Chand1172632017-03-15 17:33:09 -0700527 * Internal implementation of processVlanIdFilter.
528 * <p>
529 * The is_present bit in set_vlan_vid action is required to be 0 in OFDPA i12.
530 * Since it is non-OF spec, we need an extension treatment for that.
531 * The useVlanExtension must be set to false for OFDPA i12.
532 * </p>
Saurav Das0e99e2b2015-10-28 12:39:42 -0700533 *
Charles Chanf9e98652016-09-07 16:54:23 -0700534 * @param portCriterion port on device for which this filter is programmed
535 * @param vidCriterion vlan assigned to port, or NONE for untagged
536 * @param assignedVlan assigned vlan-id for untagged packets
537 * @param applicationId for application programming this filter
Saurav Das822c4e22015-10-23 10:51:11 -0700538 * @return list of FlowRule for port-vlan filters
539 */
540 protected List<FlowRule> processVlanIdFilter(PortCriterion portCriterion,
Yi Tsengef19de12017-04-24 11:33:05 -0700541 VlanIdCriterion vidCriterion,
542 VlanId assignedVlan,
543 ApplicationId applicationId) {
Charles Chan14967c22015-12-07 11:11:50 -0800544 List<FlowRule> rules = new ArrayList<>();
Saurav Das822c4e22015-10-23 10:51:11 -0700545 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
546 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
Charles Chan14967c22015-12-07 11:11:50 -0800547 TrafficSelector.Builder preSelector = null;
548 TrafficTreatment.Builder preTreatment = null;
549
Saurav Das4f980082015-11-05 13:39:15 -0800550 treatment.transition(TMAC_TABLE);
551
Saurav Das822c4e22015-10-23 10:51:11 -0700552 if (vidCriterion.vlanId() == VlanId.NONE) {
553 // untagged packets are assigned vlans
Charles Chand1172632017-03-15 17:33:09 -0700554 preSelector = DefaultTrafficSelector.builder();
555 if (requireVlanExtensions()) {
556 OfdpaMatchVlanVid ofdpaMatchVlanVid = new OfdpaMatchVlanVid(VlanId.NONE);
557 selector.extension(ofdpaMatchVlanVid, deviceId);
Charles Chanf9e98652016-09-07 16:54:23 -0700558 OfdpaSetVlanVid ofdpaSetVlanVid = new OfdpaSetVlanVid(assignedVlan);
559 treatment.extension(ofdpaSetVlanVid, deviceId);
Charles Chand1172632017-03-15 17:33:09 -0700560
561 OfdpaMatchVlanVid preOfdpaMatchVlanVid = new OfdpaMatchVlanVid(assignedVlan);
562 preSelector.extension(preOfdpaMatchVlanVid, deviceId);
Charles Chanf9e98652016-09-07 16:54:23 -0700563 } else {
Charles Chand1172632017-03-15 17:33:09 -0700564 selector.matchVlanId(VlanId.NONE);
Charles Chanf9e98652016-09-07 16:54:23 -0700565 treatment.setVlanId(assignedVlan);
Charles Chand1172632017-03-15 17:33:09 -0700566
567 preSelector.matchVlanId(assignedVlan);
568 }
569 preTreatment = DefaultTrafficTreatment.builder().transition(TMAC_TABLE);
570 } else {
571 if (requireVlanExtensions()) {
572 OfdpaMatchVlanVid ofdpaMatchVlanVid = new OfdpaMatchVlanVid(vidCriterion.vlanId());
573 selector.extension(ofdpaMatchVlanVid, deviceId);
574 } else {
575 selector.matchVlanId(vidCriterion.vlanId());
Charles Chanf9e98652016-09-07 16:54:23 -0700576 }
Charles Chan14967c22015-12-07 11:11:50 -0800577
Charles Chand55e84d2016-03-30 17:54:24 -0700578 if (!assignedVlan.equals(vidCriterion.vlanId())) {
Charles Chand1172632017-03-15 17:33:09 -0700579 if (requireVlanExtensions()) {
Charles Chanc03782d2017-01-31 13:57:55 -0800580 OfdpaSetVlanVid ofdpaSetVlanVid = new OfdpaSetVlanVid(assignedVlan);
581 treatment.extension(ofdpaSetVlanVid, deviceId);
582 } else {
583 treatment.setVlanId(assignedVlan);
584 }
Charles Chand55e84d2016-03-30 17:54:24 -0700585 }
Saurav Das822c4e22015-10-23 10:51:11 -0700586 }
Saurav Das822c4e22015-10-23 10:51:11 -0700587
588 // ofdpa cannot match on ALL portnumber, so we need to use separate
589 // rules for each port.
Charles Chan14967c22015-12-07 11:11:50 -0800590 List<PortNumber> portnums = new ArrayList<>();
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530591 if (portCriterion != null) {
592 if (PortNumber.ALL.equals(portCriterion.port())) {
593 for (Port port : deviceService.getPorts(deviceId)) {
594 if (port.number().toLong() > 0 && port.number().toLong() < OFPP_MAX) {
595 portnums.add(port.number());
596 }
Saurav Das822c4e22015-10-23 10:51:11 -0700597 }
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530598 } else {
599 portnums.add(portCriterion.port());
Saurav Das822c4e22015-10-23 10:51:11 -0700600 }
601 } else {
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530602 log.warn("Filtering Objective missing Port Criterion . " +
603 "VLAN Table cannot be programmed for {}",
604 deviceId);
Saurav Das822c4e22015-10-23 10:51:11 -0700605 }
Saurav Das4f980082015-11-05 13:39:15 -0800606
Saurav Das822c4e22015-10-23 10:51:11 -0700607 for (PortNumber pnum : portnums) {
Saurav Das4f980082015-11-05 13:39:15 -0800608 // create rest of flowrule
Saurav Das822c4e22015-10-23 10:51:11 -0700609 selector.matchInPort(pnum);
610 FlowRule rule = DefaultFlowRule.builder()
611 .forDevice(deviceId)
612 .withSelector(selector.build())
613 .withTreatment(treatment.build())
614 .withPriority(DEFAULT_PRIORITY)
615 .fromApp(applicationId)
616 .makePermanent()
617 .forTable(VLAN_TABLE).build();
Charles Chan14967c22015-12-07 11:11:50 -0800618
619 if (preSelector != null) {
620 preSelector.matchInPort(pnum);
621 FlowRule preRule = DefaultFlowRule.builder()
622 .forDevice(deviceId)
623 .withSelector(preSelector.build())
624 .withTreatment(preTreatment.build())
625 .withPriority(DEFAULT_PRIORITY)
626 .fromApp(applicationId)
627 .makePermanent()
628 .forTable(VLAN_TABLE).build();
629 rules.add(preRule);
630 }
631
Saurav Das822c4e22015-10-23 10:51:11 -0700632 rules.add(rule);
633 }
634 return rules;
635 }
636
637 /**
638 * Allows routed packets with correct destination MAC to be directed
639 * to unicast-IP routing table or MPLS forwarding table.
Saurav Das822c4e22015-10-23 10:51:11 -0700640 *
641 * @param portCriterion port on device for which this filter is programmed
642 * @param ethCriterion dstMac of device for which is filter is programmed
643 * @param vidCriterion vlan assigned to port, or NONE for untagged
Saurav Das0e99e2b2015-10-28 12:39:42 -0700644 * @param assignedVlan assigned vlan-id for untagged packets
Saurav Das822c4e22015-10-23 10:51:11 -0700645 * @param applicationId for application programming this filter
646 * @return list of FlowRule for port-vlan filters
647
648 */
649 protected List<FlowRule> processEthDstFilter(PortCriterion portCriterion,
650 EthCriterion ethCriterion,
651 VlanIdCriterion vidCriterion,
Saurav Das0e99e2b2015-10-28 12:39:42 -0700652 VlanId assignedVlan,
Saurav Das822c4e22015-10-23 10:51:11 -0700653 ApplicationId applicationId) {
Charles Chan5270ed02016-01-30 23:22:37 -0800654 // Consider PortNumber.ANY as wildcard. Match ETH_DST only
Sivachidambaram Subramanianc598d062017-05-16 22:42:06 +0530655 if (portCriterion != null && PortNumber.ANY.equals(portCriterion.port())) {
Charles Chan5270ed02016-01-30 23:22:37 -0800656 return processEthDstOnlyFilter(ethCriterion, applicationId);
657 }
658
Charles Chan5b9df8d2016-03-28 22:21:40 -0700659 // Multicast MAC
660 if (ethCriterion.mask() != null) {
Charles Chan206cac02017-10-20 19:09:16 -0700661 return processMcastEthDstFilter(ethCriterion, assignedVlan, applicationId);
Charles Chan5b9df8d2016-03-28 22:21:40 -0700662 }
663
Saurav Das822c4e22015-10-23 10:51:11 -0700664 //handling untagged packets via assigned VLAN
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530665 if (vidCriterion != null && vidCriterion.vlanId() == VlanId.NONE) {
Saurav Das0e99e2b2015-10-28 12:39:42 -0700666 vidCriterion = (VlanIdCriterion) Criteria.matchVlanId(assignedVlan);
Saurav Das822c4e22015-10-23 10:51:11 -0700667 }
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530668 List<FlowRule> rules = new ArrayList<>();
669 OfdpaMatchVlanVid ofdpaMatchVlanVid = null;
670 if (vidCriterion != null && requireVlanExtensions()) {
671 ofdpaMatchVlanVid = new OfdpaMatchVlanVid(vidCriterion.vlanId());
672 }
Saurav Das822c4e22015-10-23 10:51:11 -0700673 // ofdpa cannot match on ALL portnumber, so we need to use separate
674 // rules for each port.
Yuta HIGUCHI2dce08a2017-04-20 21:57:48 -0700675 List<PortNumber> portnums = new ArrayList<>();
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530676 if (portCriterion != null) {
677 if (PortNumber.ALL.equals(portCriterion.port())) {
678 for (Port port : deviceService.getPorts(deviceId)) {
679 if (port.number().toLong() > 0 && port.number().toLong() < OFPP_MAX) {
680 portnums.add(port.number());
681 }
Saurav Das822c4e22015-10-23 10:51:11 -0700682 }
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530683 } else {
684 portnums.add(portCriterion.port());
685 }
686 for (PortNumber pnum : portnums) {
687 rules.add(buildTmacRuleForIpv4(ethCriterion,
688 vidCriterion,
689 ofdpaMatchVlanVid,
690 applicationId,
691 pnum));
692 rules.add(buildTmacRuleForMpls(ethCriterion,
693 vidCriterion,
694 ofdpaMatchVlanVid,
695 applicationId,
696 pnum));
697 rules.add(buildTmacRuleForIpv6(ethCriterion,
698 vidCriterion,
699 ofdpaMatchVlanVid,
700 applicationId,
701 pnum));
Saurav Das822c4e22015-10-23 10:51:11 -0700702 }
703 } else {
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530704 rules.add(buildTmacRuleForIpv4(ethCriterion,
705 vidCriterion,
706 ofdpaMatchVlanVid,
707 applicationId,
708 null));
709 rules.add(buildTmacRuleForMpls(ethCriterion,
710 vidCriterion,
711 ofdpaMatchVlanVid,
712 applicationId,
713 null));
714 rules.add(buildTmacRuleForIpv6(ethCriterion,
715 vidCriterion,
716 ofdpaMatchVlanVid,
717 applicationId,
718 null));
Saurav Das822c4e22015-10-23 10:51:11 -0700719 }
720 return rules;
721 }
722
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530723 /**
724 * Builds TMAC rules for IPv4 packets.
725 *
Charles Chan0d6db512017-12-19 19:55:57 -0800726 * @param ethCriterion dst mac matching
727 * @param vidCriterion vlan id assigned to the port
728 * @param ofdpaMatchVlanVid OFDPA vlan id matching
729 * @param applicationId application id
730 * @param pnum port number
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530731 * @return TMAC rule for IPV4 packets
732 */
733 private FlowRule buildTmacRuleForIpv4(EthCriterion ethCriterion,
734 VlanIdCriterion vidCriterion,
735 OfdpaMatchVlanVid ofdpaMatchVlanVid,
736 ApplicationId applicationId,
737 PortNumber pnum) {
738 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
739 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
740 if (pnum != null) {
741 if (matchInPortTmacTable()) {
742 selector.matchInPort(pnum);
743 } else {
Saurav Das9aa28bd2017-12-05 15:00:23 -0800744 log.debug("Pipeline does not support IN_PORT matching in TMAC table, " +
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530745 "ignoring the IN_PORT criteria");
746 }
747 }
748 if (vidCriterion != null) {
749 if (requireVlanExtensions()) {
750 selector.extension(ofdpaMatchVlanVid, deviceId);
751 } else {
752 selector.matchVlanId(vidCriterion.vlanId());
753 }
754 }
755 selector.matchEthType(Ethernet.TYPE_IPV4);
756 selector.matchEthDst(ethCriterion.mac());
757 treatment.transition(UNICAST_ROUTING_TABLE);
758 return DefaultFlowRule.builder()
759 .forDevice(deviceId)
760 .withSelector(selector.build())
761 .withTreatment(treatment.build())
762 .withPriority(DEFAULT_PRIORITY)
763 .fromApp(applicationId)
764 .makePermanent()
765 .forTable(TMAC_TABLE).build();
766 }
767
768 /**
769 * Builds TMAC rule for MPLS packets.
770 *
Charles Chan0d6db512017-12-19 19:55:57 -0800771 * @param ethCriterion dst mac matching
772 * @param vidCriterion vlan id assigned to the port
773 * @param ofdpaMatchVlanVid OFDPA vlan id matching
774 * @param applicationId application id
775 * @param pnum port number
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530776 * @return TMAC rule for MPLS packets
777 */
778 private FlowRule buildTmacRuleForMpls(EthCriterion ethCriterion,
779 VlanIdCriterion vidCriterion,
780 OfdpaMatchVlanVid ofdpaMatchVlanVid,
781 ApplicationId applicationId,
782 PortNumber pnum) {
783 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
784 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
785 if (pnum != null) {
786 if (matchInPortTmacTable()) {
787 selector.matchInPort(pnum);
788 } else {
Saurav Das9aa28bd2017-12-05 15:00:23 -0800789 log.debug("Pipeline does not support IN_PORT matching in TMAC table, " +
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530790 "ignoring the IN_PORT criteria");
791 }
792 }
793 if (vidCriterion != null) {
794 if (requireVlanExtensions()) {
795 selector.extension(ofdpaMatchVlanVid, deviceId);
796 } else {
797 selector.matchVlanId(vidCriterion.vlanId());
798 }
799 }
800 selector.matchEthType(Ethernet.MPLS_UNICAST);
801 selector.matchEthDst(ethCriterion.mac());
802 treatment.transition(MPLS_TABLE_0);
803 return DefaultFlowRule.builder()
804 .forDevice(deviceId)
805 .withSelector(selector.build())
806 .withTreatment(treatment.build())
807 .withPriority(DEFAULT_PRIORITY)
808 .fromApp(applicationId)
809 .makePermanent()
810 .forTable(TMAC_TABLE).build();
811 }
812
813 /**
814 * Builds TMAC rules for IPv6 packets.
815 *
Charles Chan0d6db512017-12-19 19:55:57 -0800816 * @param ethCriterion dst mac matching
817 * @param vidCriterion vlan id assigned to the port
818 * @param ofdpaMatchVlanVid OFDPA vlan id matching
819 * @param applicationId application id
820 * @param pnum port number
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530821 * @return TMAC rule for IPV6 packets
822 */
823 private FlowRule buildTmacRuleForIpv6(EthCriterion ethCriterion,
824 VlanIdCriterion vidCriterion,
825 OfdpaMatchVlanVid ofdpaMatchVlanVid,
826 ApplicationId applicationId,
827 PortNumber pnum) {
828 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
829 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
830 if (pnum != null) {
831 if (matchInPortTmacTable()) {
832 selector.matchInPort(pnum);
833 } else {
Saurav Das9aa28bd2017-12-05 15:00:23 -0800834 log.debug("Pipeline does not support IN_PORT matching in TMAC table, " +
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530835 "ignoring the IN_PORT criteria");
836 }
837 }
838 if (vidCriterion != null) {
839 if (requireVlanExtensions()) {
840 selector.extension(ofdpaMatchVlanVid, deviceId);
841 } else {
842 selector.matchVlanId(vidCriterion.vlanId());
843 }
844 }
845 selector.matchEthType(Ethernet.TYPE_IPV6);
846 selector.matchEthDst(ethCriterion.mac());
847 treatment.transition(UNICAST_ROUTING_TABLE);
848 return DefaultFlowRule.builder()
849 .forDevice(deviceId)
850 .withSelector(selector.build())
851 .withTreatment(treatment.build())
852 .withPriority(DEFAULT_PRIORITY)
853 .fromApp(applicationId)
854 .makePermanent()
855 .forTable(TMAC_TABLE).build();
856 }
857
Charles Chan5270ed02016-01-30 23:22:37 -0800858 protected List<FlowRule> processEthDstOnlyFilter(EthCriterion ethCriterion,
Yi Tsengef19de12017-04-24 11:33:05 -0700859 ApplicationId applicationId) {
Pier Luigi0e358632017-01-31 09:35:05 -0800860 ImmutableList.Builder<FlowRule> builder = ImmutableList.builder();
861
Charles Chan5270ed02016-01-30 23:22:37 -0800862 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
863 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
864 selector.matchEthType(Ethernet.TYPE_IPV4);
865 selector.matchEthDst(ethCriterion.mac());
866 treatment.transition(UNICAST_ROUTING_TABLE);
867 FlowRule rule = DefaultFlowRule.builder()
868 .forDevice(deviceId)
869 .withSelector(selector.build())
870 .withTreatment(treatment.build())
871 .withPriority(DEFAULT_PRIORITY)
872 .fromApp(applicationId)
873 .makePermanent()
874 .forTable(TMAC_TABLE).build();
Pier Luigi0e358632017-01-31 09:35:05 -0800875 builder.add(rule);
876
877 selector = DefaultTrafficSelector.builder();
878 treatment = DefaultTrafficTreatment.builder();
879 selector.matchEthType(Ethernet.TYPE_IPV6);
880 selector.matchEthDst(ethCriterion.mac());
881 treatment.transition(UNICAST_ROUTING_TABLE);
882 rule = DefaultFlowRule.builder()
883 .forDevice(deviceId)
884 .withSelector(selector.build())
885 .withTreatment(treatment.build())
886 .withPriority(DEFAULT_PRIORITY)
887 .fromApp(applicationId)
888 .makePermanent()
889 .forTable(TMAC_TABLE).build();
890 return builder.add(rule).build();
Charles Chan5270ed02016-01-30 23:22:37 -0800891 }
892
Charles Chan5b9df8d2016-03-28 22:21:40 -0700893 protected List<FlowRule> processMcastEthDstFilter(EthCriterion ethCriterion,
Charles Chan206cac02017-10-20 19:09:16 -0700894 VlanId assignedVlan,
Yi Tsengef19de12017-04-24 11:33:05 -0700895 ApplicationId applicationId) {
Julia Fergusond8f145e2017-08-10 18:15:24 +0000896 ImmutableList.Builder<FlowRule> builder = ImmutableList.builder();
Charles Chan5b9df8d2016-03-28 22:21:40 -0700897 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
898 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
Charles Chan206cac02017-10-20 19:09:16 -0700899 FlowRule rule;
Julia Fergusond8f145e2017-08-10 18:15:24 +0000900
Charles Chan206cac02017-10-20 19:09:16 -0700901 if (IPV4_MULTICAST.equals(ethCriterion.mac())) {
902 selector.matchEthType(Ethernet.TYPE_IPV4);
903 selector.matchEthDstMasked(ethCriterion.mac(), ethCriterion.mask());
904 selector.matchVlanId(assignedVlan);
905 treatment.transition(MULTICAST_ROUTING_TABLE);
906 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();
914 builder.add(rule);
915 }
916
917 if (IPV6_MULTICAST.equals(ethCriterion.mac())) {
918 selector = DefaultTrafficSelector.builder();
919 treatment = DefaultTrafficTreatment.builder();
920 selector.matchEthType(Ethernet.TYPE_IPV6);
921 selector.matchEthDstMasked(ethCriterion.mac(), ethCriterion.mask());
922 selector.matchVlanId(assignedVlan);
923 treatment.transition(MULTICAST_ROUTING_TABLE);
924 rule = DefaultFlowRule.builder()
925 .forDevice(deviceId)
926 .withSelector(selector.build())
927 .withTreatment(treatment.build())
928 .withPriority(DEFAULT_PRIORITY)
929 .fromApp(applicationId)
930 .makePermanent()
931 .forTable(TMAC_TABLE).build();
932 builder.add(rule);
933 }
934 return builder.build();
Charles Chan5b9df8d2016-03-28 22:21:40 -0700935 }
936
Saurav Das822c4e22015-10-23 10:51:11 -0700937 private Collection<FlowRule> processForward(ForwardingObjective fwd) {
938 switch (fwd.flag()) {
939 case SPECIFIC:
940 return processSpecific(fwd);
941 case VERSATILE:
942 return processVersatile(fwd);
943 default:
944 fail(fwd, ObjectiveError.UNKNOWN);
945 log.warn("Unknown forwarding flag {}", fwd.flag());
946 }
947 return Collections.emptySet();
948 }
949
950 /**
951 * In the OF-DPA 2.0 pipeline, versatile forwarding objectives go to the
952 * ACL table.
953 * @param fwd the forwarding objective of type 'versatile'
954 * @return a collection of flow rules to be sent to the switch. An empty
955 * collection may be returned if there is a problem in processing
956 * the flow rule
957 */
Saurav Das52025962016-01-28 22:30:01 -0800958 protected Collection<FlowRule> processVersatile(ForwardingObjective fwd) {
Saurav Das018605f2017-02-18 14:05:44 -0800959 log.debug("Processing versatile forwarding objective:{} in dev:{}",
Saurav Dasd2fded02016-12-02 15:43:47 -0800960 fwd.id(), deviceId);
Saurav Das822c4e22015-10-23 10:51:11 -0700961
962 EthTypeCriterion ethType =
Saurav Das77b5e902016-01-27 17:01:59 -0800963 (EthTypeCriterion) fwd.selector().getCriterion(Criterion.Type.ETH_TYPE);
Saurav Das822c4e22015-10-23 10:51:11 -0700964 if (ethType == null) {
Saurav Dasd2fded02016-12-02 15:43:47 -0800965 log.error("Versatile forwarding objective:{} must include ethType",
966 fwd.id());
Saurav Das822c4e22015-10-23 10:51:11 -0700967 fail(fwd, ObjectiveError.BADPARAMS);
968 return Collections.emptySet();
969 }
970 if (fwd.nextId() == null && fwd.treatment() == null) {
971 log.error("Forwarding objective {} from {} must contain "
972 + "nextId or Treatment", fwd.selector(), fwd.appId());
Zsolt Haraszti9faab752016-02-17 15:55:20 -0800973 fail(fwd, ObjectiveError.BADPARAMS);
Saurav Das822c4e22015-10-23 10:51:11 -0700974 return Collections.emptySet();
975 }
Saurav Das49cb5a12016-01-16 22:54:07 -0800976
Saurav Das77b5e902016-01-27 17:01:59 -0800977 TrafficSelector.Builder sbuilder = DefaultTrafficSelector.builder();
978 fwd.selector().criteria().forEach(criterion -> {
979 if (criterion instanceof VlanIdCriterion) {
980 VlanId vlanId = ((VlanIdCriterion) criterion).vlanId();
981 // ensure that match does not include vlan = NONE as OF-DPA does not
982 // match untagged packets this way in the ACL table.
983 if (vlanId.equals(VlanId.NONE)) {
984 return;
985 }
Charles Chand1172632017-03-15 17:33:09 -0700986 if (requireVlanExtensions()) {
987 OfdpaMatchVlanVid ofdpaMatchVlanVid = new OfdpaMatchVlanVid(vlanId);
988 sbuilder.extension(ofdpaMatchVlanVid, deviceId);
989 } else {
990 sbuilder.matchVlanId(vlanId);
991 }
Pier Ventree0ae7a32016-11-23 09:57:42 -0800992 } else if (criterion instanceof Icmpv6TypeCriterion ||
993 criterion instanceof Icmpv6CodeCriterion) {
994 /*
995 * We silenty discard these criterions, our current
996 * OFDPA platform does not support these matches on
997 * the ACL table.
998 */
999 log.warn("ICMPv6 Type and ICMPv6 Code are not supported");
Charles Chan30122a72017-10-05 15:17:15 -07001000 } else if (criterion instanceof TcpPortCriterion || criterion instanceof UdpPortCriterion) {
1001 // FIXME: QMX switches do not support L4 dst port matching in ACL table.
1002 // Currently L4 dst port matching is only used by DHCP relay feature
1003 // and therefore is safe to be replaced with L4 src port matching.
1004 // We need to revisit this if L4 dst port is used for other purpose in the future.
1005 if (!supportIpv6L4Dst() && isIpv6(fwd.selector())) {
Charles Chan30122a72017-10-05 15:17:15 -07001006 switch (criterion.type()) {
1007 case UDP_DST:
Charles Chan30122a72017-10-05 15:17:15 -07001008 case UDP_DST_MASKED:
Charles Chan30122a72017-10-05 15:17:15 -07001009 case TCP_DST:
Charles Chan30122a72017-10-05 15:17:15 -07001010 case TCP_DST_MASKED:
Charles Chan30122a72017-10-05 15:17:15 -07001011 break;
1012 default:
1013 sbuilder.add(criterion);
1014 }
1015 } else {
1016 sbuilder.add(criterion);
1017 }
Saurav Das77b5e902016-01-27 17:01:59 -08001018 } else {
1019 sbuilder.add(criterion);
1020 }
1021 });
1022
Saurav Das822c4e22015-10-23 10:51:11 -07001023 // XXX driver does not currently do type checking as per Tables 65-67 in
1024 // OFDPA 2.0 spec. The only allowed treatment is a punt to the controller.
Saurav Das49cb5a12016-01-16 22:54:07 -08001025 TrafficTreatment.Builder ttBuilder = DefaultTrafficTreatment.builder();
1026 if (fwd.treatment() != null) {
1027 for (Instruction ins : fwd.treatment().allInstructions()) {
1028 if (ins instanceof OutputInstruction) {
1029 OutputInstruction o = (OutputInstruction) ins;
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +05301030 if (o != null && PortNumber.CONTROLLER.equals(o.port())) {
Saurav Das49cb5a12016-01-16 22:54:07 -08001031 ttBuilder.add(o);
1032 } else {
1033 log.warn("Only allowed treatments in versatile forwarding "
1034 + "objectives are punts to the controller");
1035 }
1036 } else {
1037 log.warn("Cannot process instruction in versatile fwd {}", ins);
1038 }
Saurav Das822c4e22015-10-23 10:51:11 -07001039 }
Charles Chan2df0e8a2017-01-09 11:45:08 -08001040 if (fwd.treatment().clearedDeferred()) {
1041 ttBuilder.wipeDeferred();
1042 }
Saurav Das822c4e22015-10-23 10:51:11 -07001043 }
Saurav Das822c4e22015-10-23 10:51:11 -07001044 if (fwd.nextId() != null) {
Saurav Das49cb5a12016-01-16 22:54:07 -08001045 // overide case
1046 NextGroup next = getGroupForNextObjective(fwd.nextId());
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +05301047 if (next == null) {
1048 fail(fwd, ObjectiveError.BADPARAMS);
1049 return Collections.emptySet();
1050 }
Saurav Das49cb5a12016-01-16 22:54:07 -08001051 List<Deque<GroupKey>> gkeys = appKryo.deserialize(next.data());
1052 // we only need the top level group's key to point the flow to it
1053 Group group = groupService.getGroup(deviceId, gkeys.get(0).peekFirst());
1054 if (group == null) {
1055 log.warn("Group with key:{} for next-id:{} not found in dev:{}",
1056 gkeys.get(0).peekFirst(), fwd.nextId(), deviceId);
1057 fail(fwd, ObjectiveError.GROUPMISSING);
1058 return Collections.emptySet();
1059 }
1060 ttBuilder.deferred().group(group.id());
Saurav Das822c4e22015-10-23 10:51:11 -07001061 }
Saurav Das49cb5a12016-01-16 22:54:07 -08001062
1063 FlowRule.Builder ruleBuilder = DefaultFlowRule.builder()
1064 .fromApp(fwd.appId())
1065 .withPriority(fwd.priority())
1066 .forDevice(deviceId)
Saurav Das77b5e902016-01-27 17:01:59 -08001067 .withSelector(sbuilder.build())
Saurav Das49cb5a12016-01-16 22:54:07 -08001068 .withTreatment(ttBuilder.build())
1069 .makePermanent()
1070 .forTable(ACL_TABLE);
1071 return Collections.singletonList(ruleBuilder.build());
Saurav Das822c4e22015-10-23 10:51:11 -07001072 }
1073
1074 /**
1075 * In the OF-DPA 2.0 pipeline, specific forwarding refers to the IP table
Saurav Das8a0732e2015-11-20 15:27:53 -08001076 * (unicast or multicast) or the L2 table (mac + vlan) or the MPLS table.
Saurav Das822c4e22015-10-23 10:51:11 -07001077 *
1078 * @param fwd the forwarding objective of type 'specific'
1079 * @return a collection of flow rules. Typically there will be only one
1080 * for this type of forwarding objective. An empty set may be
1081 * returned if there is an issue in processing the objective.
1082 */
Saurav Das8a0732e2015-11-20 15:27:53 -08001083 protected Collection<FlowRule> processSpecific(ForwardingObjective fwd) {
Saurav Das25190812016-05-27 13:54:07 -07001084 log.debug("Processing specific fwd objective:{} in dev:{} with next:{}",
Saurav Das4ce45962015-11-24 23:21:05 -08001085 fwd.id(), deviceId, fwd.nextId());
1086 boolean isEthTypeObj = isSupportedEthTypeObjective(fwd);
1087 boolean isEthDstObj = isSupportedEthDstObjective(fwd);
1088
1089 if (isEthTypeObj) {
1090 return processEthTypeSpecific(fwd);
1091 } else if (isEthDstObj) {
1092 return processEthDstSpecific(fwd);
1093 } else {
1094 log.warn("processSpecific: Unsupported forwarding objective "
1095 + "criteria fwd:{} in dev:{}", fwd.nextId(), deviceId);
Saurav Das822c4e22015-10-23 10:51:11 -07001096 fail(fwd, ObjectiveError.UNSUPPORTED);
1097 return Collections.emptySet();
1098 }
Saurav Das4ce45962015-11-24 23:21:05 -08001099 }
1100
Saurav Das4ce45962015-11-24 23:21:05 -08001101 /**
1102 * Handles forwarding rules to the IP and MPLS tables.
1103 *
1104 * @param fwd the forwarding objective
1105 * @return A collection of flow rules, or an empty set
1106 */
1107 protected Collection<FlowRule> processEthTypeSpecific(ForwardingObjective fwd) {
Charles Chancad338a2016-09-16 18:03:11 -07001108 return processEthTypeSpecificInternal(fwd, false, ACL_TABLE);
Charles Chanf9e98652016-09-07 16:54:23 -07001109 }
1110
1111 /**
1112 * Internal implementation of processEthTypeSpecific.
1113 * <p>
1114 * Wildcarded IPv4_DST is not supported in OFDPA i12. Therefore, we break
1115 * the rule into 0.0.0.0/1 and 128.0.0.0/1.
1116 * The allowDefaultRoute must be set to false for OFDPA i12.
1117 * </p>
1118 *
1119 * @param fwd the forwarding objective
1120 * @param allowDefaultRoute allow wildcarded IPv4_DST or not
Ray Milkey0bb1e102016-11-10 14:51:27 -08001121 * @param mplsNextTable next MPLS table
Charles Chanf9e98652016-09-07 16:54:23 -07001122 * @return A collection of flow rules, or an empty set
1123 */
1124 protected Collection<FlowRule> processEthTypeSpecificInternal(ForwardingObjective fwd,
Charles Chancad338a2016-09-16 18:03:11 -07001125 boolean allowDefaultRoute,
1126 int mplsNextTable) {
Saurav Das4ce45962015-11-24 23:21:05 -08001127 TrafficSelector selector = fwd.selector();
1128 EthTypeCriterion ethType =
1129 (EthTypeCriterion) selector.getCriterion(Criterion.Type.ETH_TYPE);
Flavio Castroe10fa242016-01-15 12:43:51 -08001130 boolean popMpls = false;
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001131 boolean emptyGroup = false;
Charles Chan188ebf52015-12-23 00:15:11 -08001132 int forTableId;
Saurav Das8a0732e2015-11-20 15:27:53 -08001133 TrafficSelector.Builder filteredSelector = DefaultTrafficSelector.builder();
Charles Chan14967c22015-12-07 11:11:50 -08001134 TrafficTreatment.Builder tb = DefaultTrafficTreatment.builder();
Flavio Castroe10fa242016-01-15 12:43:51 -08001135 TrafficSelector.Builder complementarySelector = DefaultTrafficSelector.builder();
Charles Chan14967c22015-12-07 11:11:50 -08001136
Saurav Das8a0732e2015-11-20 15:27:53 -08001137 if (ethType.ethType().toShort() == Ethernet.TYPE_IPV4) {
Pier Luigi3bfe32c2017-01-30 09:47:36 -08001138 if (buildIpv4Selector(filteredSelector, complementarySelector, fwd, allowDefaultRoute) < 0) {
1139 return Collections.emptyList();
1140 }
1141 // We need to set properly the next table
Flavio Castroe10fa242016-01-15 12:43:51 -08001142 IpPrefix ipv4Dst = ((IPCriterion) selector.getCriterion(Criterion.Type.IPV4_DST)).ip();
Charles Chan5b9df8d2016-03-28 22:21:40 -07001143 if (ipv4Dst.isMulticast()) {
Charles Chan5b9df8d2016-03-28 22:21:40 -07001144 forTableId = MULTICAST_ROUTING_TABLE;
Flavio Castroe10fa242016-01-15 12:43:51 -08001145 } else {
Charles Chan5b9df8d2016-03-28 22:21:40 -07001146 forTableId = UNICAST_ROUTING_TABLE;
Flavio Castroe10fa242016-01-15 12:43:51 -08001147 }
Charles Chan14967c22015-12-07 11:11:50 -08001148 if (fwd.treatment() != null) {
1149 for (Instruction instr : fwd.treatment().allInstructions()) {
1150 if (instr instanceof L3ModificationInstruction &&
1151 ((L3ModificationInstruction) instr).subtype() == L3SubType.DEC_TTL) {
Saurav Das9c705342017-01-06 11:36:01 -08001152 // XXX decrementing IP ttl is done automatically for routing, this
1153 // action is ignored or rejected in ofdpa as it is not fully implemented
1154 //tb.deferred().add(instr);
Charles Chan14967c22015-12-07 11:11:50 -08001155 }
1156 }
1157 }
1158
Pier Ventree0ae7a32016-11-23 09:57:42 -08001159 } else if (ethType.ethType().toShort() == Ethernet.TYPE_IPV6) {
1160 if (buildIpv6Selector(filteredSelector, fwd) < 0) {
1161 return Collections.emptyList();
1162 }
Kalhee Kim7c6f4f82017-10-04 14:59:11 +00001163 //We need to set the proper next table
1164 IpPrefix ipv6Dst = ((IPCriterion) selector.getCriterion(Criterion.Type.IPV6_DST)).ip();
1165 if (ipv6Dst.isMulticast()) {
1166 forTableId = MULTICAST_ROUTING_TABLE;
1167 } else {
1168 forTableId = UNICAST_ROUTING_TABLE;
1169 }
1170
Pier Ventree0ae7a32016-11-23 09:57:42 -08001171 if (fwd.treatment() != null) {
1172 for (Instruction instr : fwd.treatment().allInstructions()) {
1173 if (instr instanceof L3ModificationInstruction &&
1174 ((L3ModificationInstruction) instr).subtype() == L3SubType.DEC_TTL) {
1175 // XXX decrementing IP ttl is done automatically for routing, this
1176 // action is ignored or rejected in ofdpa as it is not fully implemented
1177 //tb.deferred().add(instr);
1178 }
1179 }
1180 }
Saurav Das8a0732e2015-11-20 15:27:53 -08001181 } else {
1182 filteredSelector
1183 .matchEthType(Ethernet.MPLS_UNICAST)
1184 .matchMplsLabel(((MplsCriterion)
1185 selector.getCriterion(Criterion.Type.MPLS_LABEL)).label());
1186 MplsBosCriterion bos = (MplsBosCriterion) selector
Pier Ventre140a8942016-11-02 07:26:38 -07001187 .getCriterion(MPLS_BOS);
Saurav Das8a0732e2015-11-20 15:27:53 -08001188 if (bos != null) {
1189 filteredSelector.matchMplsBos(bos.mplsBos());
1190 }
1191 forTableId = MPLS_TABLE_1;
Saurav Das4ce45962015-11-24 23:21:05 -08001192 log.debug("processing MPLS specific forwarding objective {} -> next:{}"
1193 + " in dev {}", fwd.id(), fwd.nextId(), deviceId);
Saurav Das822c4e22015-10-23 10:51:11 -07001194
Charles Chan14967c22015-12-07 11:11:50 -08001195 if (fwd.treatment() != null) {
1196 for (Instruction instr : fwd.treatment().allInstructions()) {
1197 if (instr instanceof L2ModificationInstruction &&
1198 ((L2ModificationInstruction) instr).subtype() == L2SubType.MPLS_POP) {
Saurav Das8a0732e2015-11-20 15:27:53 -08001199 popMpls = true;
Pier Luigi3bfe32c2017-01-30 09:47:36 -08001200 // OF-DPA does not pop in MPLS table in some cases. For the L3 VPN, it requires
Saurav Das9c705342017-01-06 11:36:01 -08001201 // setting the MPLS_TYPE so pop can happen down the pipeline
Pier Luigi3bfe32c2017-01-30 09:47:36 -08001202 if (mplsNextTable == MPLS_TYPE_TABLE && isNotMplsBos(selector)) {
1203 tb.immediate().popMpls();
1204 }
Charles Chan14967c22015-12-07 11:11:50 -08001205 }
1206 if (instr instanceof L3ModificationInstruction &&
1207 ((L3ModificationInstruction) instr).subtype() == L3SubType.DEC_TTL) {
1208 // FIXME Should modify the app to send the correct DEC_MPLS_TTL instruction
1209 tb.immediate().decMplsTtl();
1210 }
1211 if (instr instanceof L3ModificationInstruction &&
1212 ((L3ModificationInstruction) instr).subtype() == L3SubType.TTL_IN) {
1213 tb.immediate().add(instr);
1214 }
Saurav Das8a0732e2015-11-20 15:27:53 -08001215 }
1216 }
1217 }
Saurav Das822c4e22015-10-23 10:51:11 -07001218
1219 if (fwd.nextId() != null) {
Saurav Das8a0732e2015-11-20 15:27:53 -08001220 if (forTableId == MPLS_TABLE_1 && !popMpls) {
1221 log.warn("SR CONTINUE case cannot be handled as MPLS ECMP "
Saurav Das25190812016-05-27 13:54:07 -07001222 + "is not implemented in OF-DPA yet. Aborting this flow {} -> next:{}"
1223 + "in this device {}", fwd.id(), fwd.nextId(), deviceId);
Pier Ventree0ae7a32016-11-23 09:57:42 -08001224 // XXX We could convert to forwarding to a single-port, via a MPLS interface,
1225 // or a MPLS SWAP (with-same) but that would have to be handled in the next-objective.
1226 // Also the pop-mpls logic used here won't work in non-BoS case.
Saurav Das4ce45962015-11-24 23:21:05 -08001227 fail(fwd, ObjectiveError.FLOWINSTALLATIONFAILED);
Saurav Das8a0732e2015-11-20 15:27:53 -08001228 return Collections.emptySet();
1229 }
1230
Saurav Das423fe2b2015-12-04 10:52:59 -08001231 NextGroup next = getGroupForNextObjective(fwd.nextId());
1232 if (next != null) {
1233 List<Deque<GroupKey>> gkeys = appKryo.deserialize(next.data());
1234 // we only need the top level group's key to point the flow to it
1235 Group group = groupService.getGroup(deviceId, gkeys.get(0).peekFirst());
Pier Ventre140a8942016-11-02 07:26:38 -07001236 if (isNotMplsBos(selector) && group.type().equals(HASHED)) {
1237 log.warn("SR CONTINUE case cannot be handled as MPLS ECMP "
1238 + "is not implemented in OF-DPA yet. Aborting this flow {} -> next:{}"
1239 + "in this device {}", fwd.id(), fwd.nextId(), deviceId);
1240 fail(fwd, ObjectiveError.FLOWINSTALLATIONFAILED);
1241 return Collections.emptySet();
1242 }
Saurav Das423fe2b2015-12-04 10:52:59 -08001243 if (group == null) {
1244 log.warn("Group with key:{} for next-id:{} not found in dev:{}",
1245 gkeys.get(0).peekFirst(), fwd.nextId(), deviceId);
1246 fail(fwd, ObjectiveError.GROUPMISSING);
1247 return Collections.emptySet();
1248 }
1249 tb.deferred().group(group.id());
Saurav Das95047002018-01-25 09:49:01 -08001250 // retrying flows may be necessary due to bug CORD-554
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001251 if (gkeys.size() == 1 && gkeys.get(0).size() == 1) {
Saurav Das95047002018-01-25 09:49:01 -08001252 if (shouldRetry()) {
1253 log.warn("Found empty group 0x{} in dev:{} .. will retry fwd:{}",
1254 Integer.toHexString(group.id().id()), deviceId,
1255 fwd.id());
1256 emptyGroup = true;
1257 }
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001258 }
Saurav Das25190812016-05-27 13:54:07 -07001259 } else {
1260 log.warn("Cannot find group for nextId:{} in dev:{}. Aborting fwd:{}",
1261 fwd.nextId(), deviceId, fwd.id());
1262 fail(fwd, ObjectiveError.FLOWINSTALLATIONFAILED);
1263 return Collections.emptySet();
Saurav Das822c4e22015-10-23 10:51:11 -07001264 }
Saurav Das822c4e22015-10-23 10:51:11 -07001265 }
Charles Chancad338a2016-09-16 18:03:11 -07001266
1267 if (forTableId == MPLS_TABLE_1) {
Pier Ventre140a8942016-11-02 07:26:38 -07001268 if (mplsNextTable == MPLS_L3_TYPE_TABLE) {
Charles Chancad338a2016-09-16 18:03:11 -07001269 Ofdpa3SetMplsType setMplsType = new Ofdpa3SetMplsType(Ofdpa3MplsType.L3_PHP);
Saurav Das9c705342017-01-06 11:36:01 -08001270 // set mpls type as apply_action
1271 tb.immediate().extension(setMplsType, deviceId);
Charles Chancad338a2016-09-16 18:03:11 -07001272 }
1273 tb.transition(mplsNextTable);
1274 } else {
1275 tb.transition(ACL_TABLE);
1276 }
1277
Saurav Das822c4e22015-10-23 10:51:11 -07001278 FlowRule.Builder ruleBuilder = DefaultFlowRule.builder()
1279 .fromApp(fwd.appId())
1280 .withPriority(fwd.priority())
1281 .forDevice(deviceId)
Saurav Das8a0732e2015-11-20 15:27:53 -08001282 .withSelector(filteredSelector.build())
1283 .withTreatment(tb.build())
1284 .forTable(forTableId);
Saurav Das822c4e22015-10-23 10:51:11 -07001285
1286 if (fwd.permanent()) {
1287 ruleBuilder.makePermanent();
1288 } else {
1289 ruleBuilder.makeTemporary(fwd.timeout());
1290 }
Flavio Castroe10fa242016-01-15 12:43:51 -08001291 Collection<FlowRule> flowRuleCollection = new ArrayList<>();
1292 flowRuleCollection.add(ruleBuilder.build());
Pier Luigi3bfe32c2017-01-30 09:47:36 -08001293 if (!allowDefaultRoute) {
Pier Ventree0ae7a32016-11-23 09:57:42 -08001294 flowRuleCollection.add(
1295 defaultRoute(fwd, complementarySelector, forTableId, tb)
1296 );
Flavio Castroe10fa242016-01-15 12:43:51 -08001297 log.debug("Default rule 0.0.0.0/0 is being installed two rules");
1298 }
Saurav Das95047002018-01-25 09:49:01 -08001299
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001300 if (emptyGroup) {
1301 executorService.schedule(new RetryFlows(fwd, flowRuleCollection),
1302 RETRY_MS, TimeUnit.MILLISECONDS);
1303 }
Flavio Castroe10fa242016-01-15 12:43:51 -08001304 return flowRuleCollection;
Saurav Das822c4e22015-10-23 10:51:11 -07001305 }
1306
Pier Luigi3bfe32c2017-01-30 09:47:36 -08001307 protected int buildIpv4Selector(TrafficSelector.Builder builderToUpdate,
1308 TrafficSelector.Builder extBuilder,
1309 ForwardingObjective fwd,
1310 boolean allowDefaultRoute) {
1311 TrafficSelector selector = fwd.selector();
1312
1313 IpPrefix ipv4Dst = ((IPCriterion) selector.getCriterion(Criterion.Type.IPV4_DST)).ip();
1314 if (ipv4Dst.isMulticast()) {
1315 if (ipv4Dst.prefixLength() != 32) {
1316 log.warn("Multicast specific forwarding objective can only be /32");
1317 fail(fwd, ObjectiveError.BADPARAMS);
1318 return -1;
1319 }
1320 VlanId assignedVlan = readVlanFromSelector(fwd.meta());
1321 if (assignedVlan == null) {
1322 log.warn("VLAN ID required by multicast specific fwd obj is missing. Abort.");
1323 fail(fwd, ObjectiveError.BADPARAMS);
1324 return -1;
1325 }
Charles Chand1172632017-03-15 17:33:09 -07001326 if (requireVlanExtensions()) {
1327 OfdpaMatchVlanVid ofdpaMatchVlanVid = new OfdpaMatchVlanVid(assignedVlan);
1328 builderToUpdate.extension(ofdpaMatchVlanVid, deviceId);
1329 } else {
1330 builderToUpdate.matchVlanId(assignedVlan);
1331 }
Pier Luigi3bfe32c2017-01-30 09:47:36 -08001332 builderToUpdate.matchEthType(Ethernet.TYPE_IPV4).matchIPDst(ipv4Dst);
1333 log.debug("processing IPv4 multicast specific forwarding objective {} -> next:{}"
1334 + " in dev:{}", fwd.id(), fwd.nextId(), deviceId);
1335 } else {
1336 if (ipv4Dst.prefixLength() == 0) {
1337 if (allowDefaultRoute) {
1338 // The entire IPV4_DST field is wildcarded intentionally
1339 builderToUpdate.matchEthType(Ethernet.TYPE_IPV4);
1340 } else {
1341 // NOTE: The switch does not support matching 0.0.0.0/0
1342 // Split it into 0.0.0.0/1 and 128.0.0.0/1
1343 builderToUpdate.matchEthType(Ethernet.TYPE_IPV4)
1344 .matchIPDst(IpPrefix.valueOf("0.0.0.0/1"));
1345 extBuilder.matchEthType(Ethernet.TYPE_IPV4)
1346 .matchIPDst(IpPrefix.valueOf("128.0.0.0/1"));
1347 }
1348 } else {
1349 builderToUpdate.matchEthType(Ethernet.TYPE_IPV4).matchIPDst(ipv4Dst);
1350 }
1351 log.debug("processing IPv4 unicast specific forwarding objective {} -> next:{}"
1352 + " in dev:{}", fwd.id(), fwd.nextId(), deviceId);
1353 }
1354 return 0;
1355 }
1356
1357 /**
1358 * Helper method to build Ipv6 selector using the selector provided by
1359 * a forwarding objective.
1360 *
1361 * @param builderToUpdate the builder to update
1362 * @param fwd the selector to read
1363 * @return 0 if the update ends correctly. -1 if the matches
1364 * are not yet supported
1365 */
1366 protected int buildIpv6Selector(TrafficSelector.Builder builderToUpdate,
1367 ForwardingObjective fwd) {
1368
1369 TrafficSelector selector = fwd.selector();
1370
1371 IpPrefix ipv6Dst = ((IPCriterion) selector.getCriterion(Criterion.Type.IPV6_DST)).ip();
1372 if (ipv6Dst.isMulticast()) {
Julia Fergusond8f145e2017-08-10 18:15:24 +00001373 if (ipv6Dst.prefixLength() != IpAddress.INET6_BIT_LENGTH) {
1374 log.warn("Multicast specific forwarding objective can only be /128");
1375 fail(fwd, ObjectiveError.BADPARAMS);
1376 return -1;
1377 }
1378 VlanId assignedVlan = readVlanFromSelector(fwd.meta());
1379 if (assignedVlan == null) {
1380 log.warn("VLAN ID required by multicast specific fwd obj is missing. Abort.");
1381 fail(fwd, ObjectiveError.BADPARAMS);
1382 return -1;
1383 }
1384 if (requireVlanExtensions()) {
1385 OfdpaMatchVlanVid ofdpaMatchVlanVid = new OfdpaMatchVlanVid(assignedVlan);
1386 builderToUpdate.extension(ofdpaMatchVlanVid, deviceId);
1387 } else {
1388 builderToUpdate.matchVlanId(assignedVlan);
1389 }
1390 builderToUpdate.matchEthType(Ethernet.TYPE_IPV6).matchIPv6Dst(ipv6Dst);
1391 log.debug("processing IPv6 multicast specific forwarding objective {} -> next:{}"
1392 + " in dev:{}", fwd.id(), fwd.nextId(), deviceId);
1393 } else {
1394 if (ipv6Dst.prefixLength() != 0) {
1395 builderToUpdate.matchIPv6Dst(ipv6Dst);
1396 }
Pier Luigi3bfe32c2017-01-30 09:47:36 -08001397 builderToUpdate.matchEthType(Ethernet.TYPE_IPV6);
1398 log.debug("processing IPv6 unicast specific forwarding objective {} -> next:{}"
1399 + " in dev:{}", fwd.id(), fwd.nextId(), deviceId);
Julia Fergusond8f145e2017-08-10 18:15:24 +00001400 }
Pier Luigi3bfe32c2017-01-30 09:47:36 -08001401 return 0;
1402 }
1403
Pier Ventree0ae7a32016-11-23 09:57:42 -08001404 protected FlowRule defaultRoute(ForwardingObjective fwd,
1405 TrafficSelector.Builder complementarySelector,
1406 int forTableId,
1407 TrafficTreatment.Builder tb) {
1408 FlowRule.Builder rule = DefaultFlowRule.builder()
1409 .fromApp(fwd.appId())
1410 .withPriority(fwd.priority())
1411 .forDevice(deviceId)
1412 .withSelector(complementarySelector.build())
1413 .withTreatment(tb.build())
1414 .forTable(forTableId);
1415 if (fwd.permanent()) {
1416 rule.makePermanent();
1417 } else {
1418 rule.makeTemporary(fwd.timeout());
1419 }
1420 return rule.build();
1421 }
1422
Saurav Das4ce45962015-11-24 23:21:05 -08001423 /**
1424 * Handles forwarding rules to the L2 bridging table. Flow actions are not
1425 * allowed in the bridging table - instead we use L2 Interface group or
1426 * L2 flood group
1427 *
1428 * @param fwd the forwarding objective
1429 * @return A collection of flow rules, or an empty set
1430 */
1431 protected Collection<FlowRule> processEthDstSpecific(ForwardingObjective fwd) {
1432 List<FlowRule> rules = new ArrayList<>();
1433
1434 // Build filtered selector
1435 TrafficSelector selector = fwd.selector();
1436 EthCriterion ethCriterion = (EthCriterion) selector
1437 .getCriterion(Criterion.Type.ETH_DST);
1438 VlanIdCriterion vlanIdCriterion = (VlanIdCriterion) selector
1439 .getCriterion(Criterion.Type.VLAN_VID);
1440
1441 if (vlanIdCriterion == null) {
1442 log.warn("Forwarding objective for bridging requires vlan. Not "
1443 + "installing fwd:{} in dev:{}", fwd.id(), deviceId);
1444 fail(fwd, ObjectiveError.BADPARAMS);
1445 return Collections.emptySet();
1446 }
1447
1448 TrafficSelector.Builder filteredSelectorBuilder =
1449 DefaultTrafficSelector.builder();
Yi Tseng3a77b4f2017-02-06 15:02:17 -08001450
1451 if (!ethCriterion.mac().equals(NONE) &&
1452 !ethCriterion.mac().equals(BROADCAST)) {
Saurav Das4ce45962015-11-24 23:21:05 -08001453 filteredSelectorBuilder.matchEthDst(ethCriterion.mac());
1454 log.debug("processing L2 forwarding objective:{} -> next:{} in dev:{}",
1455 fwd.id(), fwd.nextId(), deviceId);
1456 } else {
Yi Tseng3a77b4f2017-02-06 15:02:17 -08001457 // Use wildcard DST_MAC if the MacAddress is None or Broadcast
Saurav Das4ce45962015-11-24 23:21:05 -08001458 log.debug("processing L2 Broadcast forwarding objective:{} -> next:{} "
1459 + "in dev:{} for vlan:{}",
1460 fwd.id(), fwd.nextId(), deviceId, vlanIdCriterion.vlanId());
1461 }
Charles Chand1172632017-03-15 17:33:09 -07001462 if (requireVlanExtensions()) {
1463 OfdpaMatchVlanVid ofdpaMatchVlanVid = new OfdpaMatchVlanVid(vlanIdCriterion.vlanId());
1464 filteredSelectorBuilder.extension(ofdpaMatchVlanVid, deviceId);
1465 } else {
1466 filteredSelectorBuilder.matchVlanId(vlanIdCriterion.vlanId());
1467 }
Saurav Das4ce45962015-11-24 23:21:05 -08001468 TrafficSelector filteredSelector = filteredSelectorBuilder.build();
1469
1470 if (fwd.treatment() != null) {
1471 log.warn("Ignoring traffic treatment in fwd rule {} meant for L2 table"
1472 + "for dev:{}. Expecting only nextId", fwd.id(), deviceId);
1473 }
1474
1475 TrafficTreatment.Builder treatmentBuilder = DefaultTrafficTreatment.builder();
1476 if (fwd.nextId() != null) {
Saurav Das423fe2b2015-12-04 10:52:59 -08001477 NextGroup next = getGroupForNextObjective(fwd.nextId());
Saurav Das4ce45962015-11-24 23:21:05 -08001478 if (next != null) {
1479 List<Deque<GroupKey>> gkeys = appKryo.deserialize(next.data());
1480 // we only need the top level group's key to point the flow to it
1481 Group group = groupService.getGroup(deviceId, gkeys.get(0).peekFirst());
1482 if (group != null) {
1483 treatmentBuilder.deferred().group(group.id());
1484 } else {
1485 log.warn("Group with key:{} for next-id:{} not found in dev:{}",
1486 gkeys.get(0).peekFirst(), fwd.nextId(), deviceId);
1487 fail(fwd, ObjectiveError.GROUPMISSING);
1488 return Collections.emptySet();
1489 }
1490 }
1491 }
1492 treatmentBuilder.immediate().transition(ACL_TABLE);
1493 TrafficTreatment filteredTreatment = treatmentBuilder.build();
1494
1495 // Build bridging table entries
1496 FlowRule.Builder flowRuleBuilder = DefaultFlowRule.builder();
1497 flowRuleBuilder.fromApp(fwd.appId())
1498 .withPriority(fwd.priority())
1499 .forDevice(deviceId)
1500 .withSelector(filteredSelector)
1501 .withTreatment(filteredTreatment)
1502 .forTable(BRIDGING_TABLE);
1503 if (fwd.permanent()) {
1504 flowRuleBuilder.makePermanent();
1505 } else {
1506 flowRuleBuilder.makeTemporary(fwd.timeout());
1507 }
1508 rules.add(flowRuleBuilder.build());
1509 return rules;
1510 }
1511
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001512 //////////////////////////////////////
1513 // Helper Methods and Classes
1514 //////////////////////////////////////
1515
1516 private boolean isSupportedEthTypeObjective(ForwardingObjective fwd) {
1517 TrafficSelector selector = fwd.selector();
1518 EthTypeCriterion ethType = (EthTypeCriterion) selector
1519 .getCriterion(Criterion.Type.ETH_TYPE);
1520 return !((ethType == null) ||
1521 ((ethType.ethType().toShort() != Ethernet.TYPE_IPV4) &&
Pier Ventree0ae7a32016-11-23 09:57:42 -08001522 (ethType.ethType().toShort() != Ethernet.MPLS_UNICAST)) &&
1523 (ethType.ethType().toShort() != Ethernet.TYPE_IPV6));
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001524 }
1525
1526 private boolean isSupportedEthDstObjective(ForwardingObjective fwd) {
1527 TrafficSelector selector = fwd.selector();
1528 EthCriterion ethDst = (EthCriterion) selector
1529 .getCriterion(Criterion.Type.ETH_DST);
1530 VlanIdCriterion vlanId = (VlanIdCriterion) selector
1531 .getCriterion(Criterion.Type.VLAN_VID);
1532 return !(ethDst == null && vlanId == null);
1533 }
1534
Saurav Das423fe2b2015-12-04 10:52:59 -08001535 protected NextGroup getGroupForNextObjective(Integer nextId) {
1536 NextGroup next = flowObjectiveStore.getNextGroup(nextId);
1537 if (next != null) {
1538 List<Deque<GroupKey>> gkeys = appKryo.deserialize(next.data());
1539 if (gkeys != null && !gkeys.isEmpty()) {
1540 return next;
1541 } else {
1542 log.warn("Empty next group found in FlowObjective store for "
1543 + "next-id:{} in dev:{}", nextId, deviceId);
1544 }
1545 } else {
1546 log.warn("next-id {} not found in Flow objective store for dev:{}",
1547 nextId, deviceId);
1548 }
1549 return null;
1550 }
1551
Charles Chan188ebf52015-12-23 00:15:11 -08001552 protected static void pass(Objective obj) {
Sho SHIMIZUef7e2902016-02-12 18:38:29 -08001553 obj.context().ifPresent(context -> context.onSuccess(obj));
Saurav Das822c4e22015-10-23 10:51:11 -07001554 }
1555
Charles Chan188ebf52015-12-23 00:15:11 -08001556 protected static void fail(Objective obj, ObjectiveError error) {
Sho SHIMIZUef7e2902016-02-12 18:38:29 -08001557 obj.context().ifPresent(context -> context.onError(obj, error));
Saurav Das822c4e22015-10-23 10:51:11 -07001558 }
Saurav Das24431192016-03-07 19:13:00 -08001559
Saurav Das24431192016-03-07 19:13:00 -08001560 @Override
1561 public List<String> getNextMappings(NextGroup nextGroup) {
1562 List<String> mappings = new ArrayList<>();
1563 List<Deque<GroupKey>> gkeys = appKryo.deserialize(nextGroup.data());
1564 for (Deque<GroupKey> gkd : gkeys) {
1565 Group lastGroup = null;
Yuta HIGUCHI2dce08a2017-04-20 21:57:48 -07001566 StringBuilder gchain = new StringBuilder();
Saurav Das24431192016-03-07 19:13:00 -08001567 for (GroupKey gk : gkd) {
1568 Group g = groupService.getGroup(deviceId, gk);
Saurav Das8be4e3a2016-03-11 17:19:07 -08001569 if (g == null) {
Saurav Das25190812016-05-27 13:54:07 -07001570 gchain.append(" NoGrp").append(" -->");
Saurav Das8be4e3a2016-03-11 17:19:07 -08001571 continue;
1572 }
1573 gchain.append(" 0x").append(Integer.toHexString(g.id().id()))
1574 .append(" -->");
Saurav Das24431192016-03-07 19:13:00 -08001575 lastGroup = g;
1576 }
1577 // add port information for last group in group-chain
Yuta HIGUCHI2dce08a2017-04-20 21:57:48 -07001578 List<Instruction> lastGroupIns = new ArrayList<>();
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001579 if (lastGroup != null && !lastGroup.buckets().buckets().isEmpty()) {
Saurav Das25190812016-05-27 13:54:07 -07001580 lastGroupIns = lastGroup.buckets().buckets().get(0)
1581 .treatment().allInstructions();
1582 }
1583 for (Instruction i: lastGroupIns) {
Saurav Das24431192016-03-07 19:13:00 -08001584 if (i instanceof OutputInstruction) {
Saurav Das8be4e3a2016-03-11 17:19:07 -08001585 gchain.append(" port:").append(((OutputInstruction) i).port());
Saurav Das24431192016-03-07 19:13:00 -08001586 }
1587 }
Saurav Das8be4e3a2016-03-11 17:19:07 -08001588 mappings.add(gchain.toString());
Saurav Das24431192016-03-07 19:13:00 -08001589 }
1590 return mappings;
1591 }
Charles Chan5b9df8d2016-03-28 22:21:40 -07001592
Pier Ventre140a8942016-11-02 07:26:38 -07001593 static boolean isMplsBos(TrafficSelector selector) {
1594 MplsBosCriterion bosCriterion = (MplsBosCriterion) selector.getCriterion(MPLS_BOS);
1595 return bosCriterion != null && bosCriterion.mplsBos();
1596 }
1597
1598 static boolean isNotMplsBos(TrafficSelector selector) {
1599 MplsBosCriterion bosCriterion = (MplsBosCriterion) selector.getCriterion(MPLS_BOS);
1600 return bosCriterion != null && !bosCriterion.mplsBos();
1601 }
1602
Charles Chan30122a72017-10-05 15:17:15 -07001603 private static boolean isIpv6(TrafficSelector selector) {
1604 EthTypeCriterion ethTypeCriterion = (EthTypeCriterion) selector.getCriterion(ETH_TYPE);
1605 return ethTypeCriterion != null && ethTypeCriterion.ethType().toShort() == Ethernet.TYPE_IPV6;
1606 }
1607
1608 static VlanId readVlanFromSelector(TrafficSelector selector) {
Saurav Das59232cf2016-04-27 18:35:50 -07001609 if (selector == null) {
1610 return null;
1611 }
Charles Chan5b9df8d2016-03-28 22:21:40 -07001612 Criterion criterion = selector.getCriterion(Criterion.Type.VLAN_VID);
1613 return (criterion == null)
1614 ? null : ((VlanIdCriterion) criterion).vlanId();
1615 }
1616
Charles Chan30122a72017-10-05 15:17:15 -07001617 static IpPrefix readIpDstFromSelector(TrafficSelector selector) {
Saurav Das59232cf2016-04-27 18:35:50 -07001618 if (selector == null) {
1619 return null;
1620 }
Charles Chan5b9df8d2016-03-28 22:21:40 -07001621 Criterion criterion = selector.getCriterion(Criterion.Type.IPV4_DST);
1622 return (criterion == null) ? null : ((IPCriterion) criterion).ip();
1623 }
Charles Chand55e84d2016-03-30 17:54:24 -07001624
1625 private static VlanId readVlanFromTreatment(TrafficTreatment treatment) {
Saurav Das59232cf2016-04-27 18:35:50 -07001626 if (treatment == null) {
1627 return null;
1628 }
Charles Chand55e84d2016-03-30 17:54:24 -07001629 for (Instruction i : treatment.allInstructions()) {
1630 if (i instanceof ModVlanIdInstruction) {
1631 return ((ModVlanIdInstruction) i).vlanId();
1632 }
1633 }
1634 return null;
1635 }
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001636
1637 /**
1638 * Utility class that retries sending flows a fixed number of times, even if
1639 * some of the attempts are successful. Used only for forwarding objectives.
1640 */
1641 protected final class RetryFlows implements Runnable {
1642 int attempts = MAX_RETRY_ATTEMPTS;
1643 private Collection<FlowRule> retryFlows;
1644 private ForwardingObjective fwd;
1645
1646 RetryFlows(ForwardingObjective fwd, Collection<FlowRule> retryFlows) {
1647 this.fwd = fwd;
1648 this.retryFlows = retryFlows;
1649 }
1650
1651 @Override
1652 public void run() {
1653 log.info("RETRY FLOWS ATTEMPT# {} for fwd:{} rules:{}",
1654 MAX_RETRY_ATTEMPTS - attempts, fwd.id(), retryFlows.size());
1655 sendForward(fwd, retryFlows);
1656 if (--attempts > 0) {
1657 executorService.schedule(this, RETRY_MS, TimeUnit.MILLISECONDS);
1658 }
1659 }
1660 }
1661
Saurav Das822c4e22015-10-23 10:51:11 -07001662}